Florin Malita | 3bfeab7 | 2020-10-12 11:26:56 -0400 | [diff] [blame] | 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 | |
Florin Malita | 3bfeab7 | 2020-10-12 11:26:56 -0400 | [diff] [blame] | 10 | #include "include/core/SkMatrix.h" |
| 11 | #include "include/core/SkStream.h" |
| 12 | #include "include/core/SkSurface.h" |
| 13 | #include "include/encode/SkPngEncoder.h" |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 14 | #include "modules/svg/include/SkSVGDOM.h" |
Florin Malita | 3bfeab7 | 2020-10-12 11:26:56 -0400 | [diff] [blame] | 15 | #include "tools/flags/CommandLineFlags.h" |
| 16 | |
| 17 | static DEFINE_string2(input , i, nullptr, "Input SVG file."); |
| 18 | static DEFINE_string2(output, o, nullptr, "Output PNG file."); |
| 19 | |
| 20 | static DEFINE_int(width , 1024, "Output width."); |
| 21 | static DEFINE_int(height, 1024, "Output height."); |
| 22 | |
| 23 | int 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 Malita | 7006e15 | 2020-11-10 15:24:59 -0500 | [diff] [blame^] | 42 | |
| 43 | auto svg_dom = SkSVGDOM::Builder() |
| 44 | .setFontManager(SkFontMgr::RefDefault()) |
| 45 | .make(in); |
Florin Malita | 3bfeab7 | 2020-10-12 11:26:56 -0400 | [diff] [blame] | 46 | 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 | } |