blob: 71ab3c3076191f42d6e329884c4b3b57da5ae20e [file] [log] [blame]
Florin Malita3bfeab72020-10-12 11:26:56 -04001/*
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
Florin Malita3bfeab72020-10-12 11:26:56 -040010#include "include/core/SkMatrix.h"
11#include "include/core/SkStream.h"
12#include "include/core/SkSurface.h"
13#include "include/encode/SkPngEncoder.h"
Florin Malitab3418102020-10-15 18:10:29 -040014#include "modules/svg/include/SkSVGDOM.h"
Florin Malita3bfeab72020-10-12 11:26:56 -040015#include "tools/flags/CommandLineFlags.h"
16
17static DEFINE_string2(input , i, nullptr, "Input SVG file.");
18static DEFINE_string2(output, o, nullptr, "Output PNG file.");
19
20static DEFINE_int(width , 1024, "Output width.");
21static DEFINE_int(height, 1024, "Output height.");
22
23int main(int argc, char** argv) {
24 CommandLineFlags::Parse(argc, argv);
25
26 if (FLAGS_input.isEmpty() || FLAGS_output.isEmpty()) {
27 std::cerr << "Missing required 'input' and 'output' args.\n";
28 return 1;
29 }
30
31 if (FLAGS_width <= 0 || FLAGS_height <= 0) {
32 std::cerr << "Invalid width/height.\n";
33 return 1;
34 }
35
36 SkFILEStream in(FLAGS_input[0]);
37 if (!in.isValid()) {
38 std::cerr << "Could not open " << FLAGS_input[0] << "\n";
39 return 1;
40 }
41
Florin Malita7006e152020-11-10 15:24:59 -050042
43 auto svg_dom = SkSVGDOM::Builder()
44 .setFontManager(SkFontMgr::RefDefault())
45 .make(in);
Florin Malita3bfeab72020-10-12 11:26:56 -040046 if (!svg_dom) {
47 std::cerr << "Could not parse " << FLAGS_input[0] << "\n";
48 return 1;
49 }
50
51 auto surface = SkSurface::MakeRasterN32Premul(FLAGS_width, FLAGS_height);
52
53 svg_dom->setContainerSize(SkSize::Make(FLAGS_width, FLAGS_height));
54 svg_dom->render(surface->getCanvas());
55
56 SkPixmap pixmap;
57 surface->peekPixels(&pixmap);
58
59 SkFILEWStream out(FLAGS_output[0]);
60 if (!out.isValid()) {
61 std::cerr << "Could not open " << FLAGS_output[0] << " for writing.\n";
62 return 1;
63 }
64
65 // Use default encoding options.
66 SkPngEncoder::Options png_options;
67
68 if (!SkPngEncoder::Encode(&out, pixmap, png_options)) {
69 std::cerr << "PNG encoding failed.\n";
70 return 1;
71 }
72
73 return 0;
74}