Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
SvgTool.cpp
Go to the documentation of this file.
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
17#include "src/utils/SkOSPath.h"
18#include "tools/CodecUtils.h"
21
22#if defined(SK_BUILD_FOR_MAC)
24#else
26#endif
27
28static DEFINE_string2(input , i, nullptr, "Input SVG file.");
29static DEFINE_string2(output, o, nullptr, "Output PNG file.");
30
31static DEFINE_int(width , 1024, "Output width.");
32static DEFINE_int(height, 1024, "Output height.");
33
34int main(int argc, char** argv) {
36
37 if (FLAGS_input.isEmpty() || FLAGS_output.isEmpty()) {
38 std::cerr << "Missing required 'input' and 'output' args.\n";
39 return 1;
40 }
41
42 if (FLAGS_width <= 0 || FLAGS_height <= 0) {
43 std::cerr << "Invalid width/height.\n";
44 return 1;
45 }
46
47 SkFILEStream in(FLAGS_input[0]);
48 if (!in.isValid()) {
49 std::cerr << "Could not open " << FLAGS_input[0] << "\n";
50 return 1;
51 }
52
53 // If necessary, clients should use a font manager that would load fonts from the system.
54#if defined(SK_BUILD_FOR_MAC)
56#else
58#endif
59
61
65 predecode,
66 fontMgr);
67
68 auto svg_dom = SkSVGDOM::Builder()
70 .setResourceProvider(std::move(rp))
72 .make(in);
73
74 if (!svg_dom) {
75 std::cerr << "Could not parse " << FLAGS_input[0] << "\n";
76 return 1;
77 }
78
79 auto surface = SkSurfaces::Raster(SkImageInfo::MakeN32Premul(FLAGS_width, FLAGS_height));
80
81 svg_dom->setContainerSize(SkSize::Make(FLAGS_width, FLAGS_height));
82 svg_dom->render(surface->getCanvas());
83
84 SkPixmap pixmap;
85 surface->peekPixels(&pixmap);
86
87 SkFILEWStream out(FLAGS_output[0]);
88 if (!out.isValid()) {
89 std::cerr << "Could not open " << FLAGS_output[0] << " for writing.\n";
90 return 1;
91 }
92
93 // Use default encoding options.
94 SkPngEncoder::Options png_options;
95
96 if (!SkPngEncoder::Encode(&out, pixmap, png_options)) {
97 std::cerr << "PNG encoding failed.\n";
98 return 1;
99 }
100
101 return 0;
102}
#define DEFINE_int(name, defaultValue, helpString)
#define DEFINE_string2(name, shortName, defaultValue, helpString)
SK_API sk_sp< SkFontMgr > SkFontMgr_New_Custom_Empty()
SK_API sk_sp< SkFontMgr > SkFontMgr_New_CoreText(CTFontCollectionRef)
static void Parse(int argc, const char *const *argv)
bool isValid() const
Definition SkStream.h:320
static SkString Dirname(const char *fullPath)
Definition SkOSPath.cpp:36
Builder & setTextShapingFactory(sk_sp< SkShapers::Factory >)
Definition SkSVGDOM.cpp:400
sk_sp< SkSVGDOM > make(SkStream &) const
Definition SkSVGDOM.cpp:405
Builder & setFontManager(sk_sp< SkFontMgr >)
Definition SkSVGDOM.cpp:390
Builder & setResourceProvider(sk_sp< skresources::ResourceProvider >)
Definition SkSVGDOM.cpp:395
static sk_sp< DataURIResourceProviderProxy > Make(sk_sp< ResourceProvider > rp, ImageDecodeStrategy=ImageDecodeStrategy::kLazyDecode, sk_sp< const SkFontMgr > fontMgr=nullptr)
static sk_sp< FileResourceProvider > Make(SkString base_dir, ImageDecodeStrategy=ImageDecodeStrategy::kLazyDecode)
VkSurfaceKHR surface
Definition main.cc:49
sk_sp< SkFontMgr > fontMgr
Definition examples.cpp:32
char ** argv
Definition library.h:9
void RegisterAllAvailable()
Definition CodecUtils.h:60
SK_API bool Encode(SkWStream *dst, const SkPixmap &src, const Options &options)
sk_sp< Factory > BestAvailable()
SK_API sk_sp< SkSurface > Raster(const SkImageInfo &imageInfo, size_t rowBytes, const SkSurfaceProps *surfaceProps)
Definition main.py:1
int32_t height
int32_t width
static SkImageInfo MakeN32Premul(int width, int height)
static constexpr SkSize Make(SkScalar w, SkScalar h)
Definition SkSize.h:56