|
| 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 "experimental/svg/model/SkSVGDOM.h" |
| 11 | +#include "include/core/SkMatrix.h" |
| 12 | +#include "include/core/SkStream.h" |
| 13 | +#include "include/core/SkSurface.h" |
| 14 | +#include "include/encode/SkPngEncoder.h" |
| 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 | + |
| 42 | + auto svg_dom = SkSVGDOM::MakeFromStream(in); |
| 43 | + if (!svg_dom) { |
| 44 | + std::cerr << "Could not parse " << FLAGS_input[0] << "\n"; |
| 45 | + return 1; |
| 46 | + } |
| 47 | + |
| 48 | + auto surface = SkSurface::MakeRasterN32Premul(FLAGS_width, FLAGS_height); |
| 49 | + |
| 50 | + svg_dom->setContainerSize(SkSize::Make(FLAGS_width, FLAGS_height)); |
| 51 | + svg_dom->render(surface->getCanvas()); |
| 52 | + |
| 53 | + SkPixmap pixmap; |
| 54 | + surface->peekPixels(&pixmap); |
| 55 | + |
| 56 | + SkFILEWStream out(FLAGS_output[0]); |
| 57 | + if (!out.isValid()) { |
| 58 | + std::cerr << "Could not open " << FLAGS_output[0] << " for writing.\n"; |
| 59 | + return 1; |
| 60 | + } |
| 61 | + |
| 62 | + // Use default encoding options. |
| 63 | + SkPngEncoder::Options png_options; |
| 64 | + |
| 65 | + if (!SkPngEncoder::Encode(&out, pixmap, png_options)) { |
| 66 | + std::cerr << "PNG encoding failed.\n"; |
| 67 | + return 1; |
| 68 | + } |
| 69 | + |
| 70 | + return 0; |
| 71 | +} |
0 commit comments