Add a simple SVG-subset-to-Skia-code tool, for benchmarking

This commit is contained in:
Patrick Walton 2019-02-25 15:06:33 -08:00
parent f6af769486
commit ac24b67300
4 changed files with 103 additions and 0 deletions

7
Cargo.lock generated
View File

@ -946,6 +946,13 @@ name = "strsim"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "svg-to-skia"
version = "0.1.0"
dependencies = [
"usvg 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "svgdom"
version = "0.15.0"

View File

@ -10,4 +10,5 @@ members = [
"utils/area-lut",
"utils/gamma-lut",
"utils/tile-svg",
"utils/svg-to-skia"
]

View File

@ -0,0 +1,8 @@
[package]
name = "svg-to-skia"
version = "0.1.0"
authors = ["Patrick Walton <pcwalton@mimiga.net>"]
edition = "2018"
[dependencies]
usvg = "0.4"

View File

@ -0,0 +1,87 @@
// pathfinder/utils/svg-to-skia/src/main.rs
//
// Copyright © 2019 The Pathfinder Project Developers.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use std::env;
use usvg::{Node, NodeKind, Options, Paint, PathSegment, Tree};
fn main() {
let input_path = env::args().skip(1).next().unwrap();
let tree = Tree::from_file(&input_path, &Options::default()).unwrap();
println!("#ifndef PAINT_H");
println!("#define PAINT_H");
println!("static void paint(SkCanvas *canvas) {{");
println!(" SkPaint paint;");
println!(" SkPath path;");
println!(" paint.setAntiAlias(true);");
println!(" canvas->clear(SK_ColorWHITE);");
let root = &tree.root();
match *root.borrow() {
NodeKind::Svg(_) => {
for kid in root.children() {
process_node(&kid);
}
}
_ => unreachable!(),
}
println!("}}");
println!("#endif");
}
fn process_node(node: &Node) {
match *node.borrow() {
NodeKind::Group(_) => {
for kid in node.children() {
process_node(&kid)
}
}
NodeKind::Path(ref path) => {
for segment in path.segments.iter() {
match segment {
PathSegment::MoveTo { x, y } => println!(" path.moveTo({}, {});", x, y),
PathSegment::LineTo { x, y } => println!(" path.lineTo({}, {});", x, y),
PathSegment::CurveTo { x1, y1, x2, y2, x, y } => {
println!(" path.cubicTo({}, {}, {}, {}, {}, {});",
x1, y1, x2, y2, x, y);
}
PathSegment::ClosePath => println!(" path.close();"),
}
}
if let Some(ref fill) = path.fill {
set_color(&fill.paint);
println!(" paint.setStyle(SkPaint::kFill_Style);");
println!(" canvas->drawPath(path, paint);");
}
if let Some(ref stroke) = path.stroke {
set_color(&stroke.paint);
println!(" paint.setStrokeWidth({});", stroke.width.value());
println!(" paint.setStyle(SkPaint::kStroke_Style);");
println!(" canvas->drawPath(path, paint);");
}
println!(" path.reset();");
}
_ => {}
}
}
fn set_color(paint: &Paint) {
if let Paint::Color(color) = *paint {
println!(" paint.setColor(0x{:x});",
((color.red as u32) << 16) |
((color.green as u32) << 8) |
((color.blue as u32) << 0) |
(0xff << 24));
}
}