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 | 24df67d | 2021-01-26 18:45:34 -0500 | [diff] [blame] | 14 | #include "modules/skresources/include/SkResources.h" |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 15 | #include "modules/svg/include/SkSVGDOM.h" |
Florin Malita | 24df67d | 2021-01-26 18:45:34 -0500 | [diff] [blame] | 16 | #include "src/utils/SkOSPath.h" |
Florin Malita | 3bfeab7 | 2020-10-12 11:26:56 -0400 | [diff] [blame] | 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 | |
| 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 | |
Florin Malita | 24df67d | 2021-01-26 18:45:34 -0500 | [diff] [blame] | 44 | auto rp = skresources::DataURIResourceProviderProxy::Make( |
| 45 | skresources::FileResourceProvider::Make(SkOSPath::Dirname(FLAGS_input[0]), |
| 46 | /*predecode=*/true), |
| 47 | /*predecode=*/true); |
Florin Malita | 7006e15 | 2020-11-10 15:24:59 -0500 | [diff] [blame] | 48 | |
| 49 | auto svg_dom = SkSVGDOM::Builder() |
| 50 | .setFontManager(SkFontMgr::RefDefault()) |
Florin Malita | 24df67d | 2021-01-26 18:45:34 -0500 | [diff] [blame] | 51 | .setResourceProvider(std::move(rp)) |
Florin Malita | 7006e15 | 2020-11-10 15:24:59 -0500 | [diff] [blame] | 52 | .make(in); |
Florin Malita | 3bfeab7 | 2020-10-12 11:26:56 -0400 | [diff] [blame] | 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 | } |