From ac24b67300bcb86ba473e73031bdc9e0d45d7853 Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Mon, 25 Feb 2019 15:06:33 -0800 Subject: [PATCH] Add a simple SVG-subset-to-Skia-code tool, for benchmarking --- Cargo.lock | 7 +++ Cargo.toml | 1 + utils/svg-to-skia/Cargo.toml | 8 ++++ utils/svg-to-skia/src/main.rs | 87 +++++++++++++++++++++++++++++++++++ 4 files changed, 103 insertions(+) create mode 100644 utils/svg-to-skia/Cargo.toml create mode 100644 utils/svg-to-skia/src/main.rs diff --git a/Cargo.lock b/Cargo.lock index 0406dd67..2e6db887 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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" diff --git a/Cargo.toml b/Cargo.toml index 514127ed..7754f329 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,4 +10,5 @@ members = [ "utils/area-lut", "utils/gamma-lut", "utils/tile-svg", + "utils/svg-to-skia" ] diff --git a/utils/svg-to-skia/Cargo.toml b/utils/svg-to-skia/Cargo.toml new file mode 100644 index 00000000..a3a57cb4 --- /dev/null +++ b/utils/svg-to-skia/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "svg-to-skia" +version = "0.1.0" +authors = ["Patrick Walton "] +edition = "2018" + +[dependencies] +usvg = "0.4" diff --git a/utils/svg-to-skia/src/main.rs b/utils/svg-to-skia/src/main.rs new file mode 100644 index 00000000..88f5b503 --- /dev/null +++ b/utils/svg-to-skia/src/main.rs @@ -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 or the MIT license +// , 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)); + } +}