Skip to content

Commit 3bfeab7

Browse files
fmalitaSkia Commit-Bot
authored andcommitted
Reland [svg] Add svg_tool
A simple svg -> png converter, to demonstrate basic API usage. TBR= Change-Id: Id723946e2c1cbdaf324a24202279cb6a6d7082b6 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/325460 Reviewed-by: Florin Malita <fmalita@google.com> Commit-Queue: Florin Malita <fmalita@google.com>
1 parent 0f37907 commit 3bfeab7

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed

BUILD.gn

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1901,6 +1901,7 @@ if (skia_enable_tools) {
19011901
]
19021902
}
19031903

1904+
# TODO(fmalita): componentize & relocate
19041905
test_lib("experimental_svg_model") {
19051906
if (skia_use_expat) {
19061907
sources = [
@@ -1936,6 +1937,16 @@ if (skia_enable_tools) {
19361937
}
19371938
}
19381939

1940+
if (skia_use_expat && !skia_use_wuffs) {
1941+
test_app("svg_tool") {
1942+
sources = [ "experimental/svg/utils/SvgTool.cpp" ]
1943+
deps = [
1944+
":experimental_svg_model",
1945+
":flags",
1946+
]
1947+
}
1948+
}
1949+
19391950
test_lib("experimental_xform") {
19401951
sources = [
19411952
"experimental/xform/SkShape.cpp",

experimental/svg/utils/SvgTool.cpp

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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+
}

public.bzl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -991,3 +991,14 @@ def svg_lib_hdrs():
991991

992992
def svg_lib_srcs():
993993
return native.glob(["experimental/svg/model/*.cpp"])
994+
995+
################################################################################
996+
## svg_tool
997+
################################################################################
998+
999+
SVG_TOOL_SRCS = [
1000+
"experimental/svg/utils/SvgTool.cpp",
1001+
# TODO(benjaminwagner): Add "flags" target.
1002+
"tools/flags/CommandLineFlags.cpp",
1003+
"tools/flags/CommandLineFlags.h",
1004+
]

0 commit comments

Comments
 (0)