• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2020 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #include <iostream>
9 
10 #include "include/core/SkMatrix.h"
11 #include "include/core/SkStream.h"
12 #include "include/core/SkSurface.h"
13 #include "include/encode/SkPngEncoder.h"
14 #include "modules/skresources/include/SkResources.h"
15 #include "modules/svg/include/SkSVGDOM.h"
16 #include "src/utils/SkOSPath.h"
17 #include "tools/flags/CommandLineFlags.h"
18 
19 static DEFINE_string2(input , i, nullptr, "Input SVG file.");
20 static DEFINE_string2(output, o, nullptr, "Output PNG file.");
21 
22 static DEFINE_int(width , 1024, "Output width.");
23 static DEFINE_int(height, 1024, "Output height.");
24 
main(int argc,char ** argv)25 int main(int argc, char** argv) {
26     CommandLineFlags::Parse(argc, argv);
27 
28     if (FLAGS_input.isEmpty() || FLAGS_output.isEmpty()) {
29         std::cerr << "Missing required 'input' and 'output' args.\n";
30         return 1;
31     }
32 
33     if (FLAGS_width <= 0 || FLAGS_height <= 0) {
34         std::cerr << "Invalid width/height.\n";
35         return 1;
36     }
37 
38     SkFILEStream in(FLAGS_input[0]);
39     if (!in.isValid()) {
40         std::cerr << "Could not open " << FLAGS_input[0] << "\n";
41         return 1;
42     }
43 
44     auto rp = skresources::DataURIResourceProviderProxy::Make(
45                   skresources::FileResourceProvider::Make(SkOSPath::Dirname(FLAGS_input[0]),
46                                                           /*predecode=*/true),
47                   /*predecode=*/true);
48 
49     auto svg_dom = SkSVGDOM::Builder()
50                         .setFontManager(SkFontMgr::RefDefault())
51                         .setResourceProvider(std::move(rp))
52                         .make(in);
53     if (!svg_dom) {
54         std::cerr << "Could not parse " << FLAGS_input[0] << "\n";
55         return 1;
56     }
57 
58     auto surface = SkSurface::MakeRasterN32Premul(FLAGS_width, FLAGS_height);
59 
60     svg_dom->setContainerSize(SkSize::Make(FLAGS_width, FLAGS_height));
61     svg_dom->render(surface->getCanvas());
62 
63     SkPixmap pixmap;
64     surface->peekPixels(&pixmap);
65 
66     SkFILEWStream out(FLAGS_output[0]);
67     if (!out.isValid()) {
68         std::cerr << "Could not open " << FLAGS_output[0] << " for writing.\n";
69         return 1;
70     }
71 
72     // Use default encoding options.
73     SkPngEncoder::Options png_options;
74 
75     if (!SkPngEncoder::Encode(&out, pixmap, png_options)) {
76         std::cerr << "PNG encoding failed.\n";
77         return 1;
78     }
79 
80     return 0;
81 }
82