From 65ff198dba4d6806baaa43db8be623cec2195517 Mon Sep 17 00:00:00 2001 From: Ingolf Wagner Date: Sat, 10 Apr 2021 03:25:03 +0200 Subject: [PATCH] feat: show how to inject svg into binary --- examples/svg.rs | 4 ++-- src/bin/asteroids.rs | 11 +++++++++-- src/lib.rs | 3 ++- src/svg.rs | 22 +++++++++++++++++++--- 4 files changed, 32 insertions(+), 8 deletions(-) diff --git a/examples/svg.rs b/examples/svg.rs index 456f5f1..f45ee6f 100644 --- a/examples/svg.rs +++ b/examples/svg.rs @@ -3,7 +3,7 @@ use geo::rotate::RotatePoint; use geo::translate::Translate; use geo::{Coordinate, Point}; -use polygon_art::load_multipolgon_from_svg; +use polygon_art::load_multipolygon_from_svg; use polygon_art::Context; use polygon_art::MultiPolygonExt; use rand::Rng; @@ -12,7 +12,7 @@ fn main() { let mut renderer = Context::create(); let path = "pool/examples/example.svg"; - let svg = load_multipolgon_from_svg(path).expect("couldn't load svg"); + let svg = load_multipolygon_from_svg(path).expect("couldn't load svg"); let item_space = 110.; diff --git a/src/bin/asteroids.rs b/src/bin/asteroids.rs index 076ff93..778bada 100644 --- a/src/bin/asteroids.rs +++ b/src/bin/asteroids.rs @@ -6,10 +6,11 @@ use geo::LineString; use geo::{Coordinate, MultiPolygon, Point, Polygon}; use geo_clipper::Clipper; use polygon_art::MultiPolygonExt; -use polygon_art::{load_multipolgon_from_svg, Context}; +use polygon_art::{load_multipolygon_from_svg_include, Context}; use rand::prelude::StdRng; use rand::Rng; +/// A more or less complete example that illustrates various technics fn main() { let mut context = Context::create(); let mut rng = context.gen_rng(); @@ -58,7 +59,13 @@ fn main() { // load ships let rotation = rng.gen_range(0..360); - let ship = load_multipolgon_from_svg("pool/asteroids/ship3.svg") + let ship_yard = [ + include_str!("../../pool/asteroids/ship1.svg"), + include_str!("../../pool/asteroids/ship2.svg"), + include_str!("../../pool/asteroids/ship3.svg"), + ]; + let chosen_ship = rng.gen_range(0..3); + let ship = load_multipolygon_from_svg_include(ship_yard[chosen_ship]) .unwrap() .rotate(90.) // rotate to scale ship with width .scale_to_width(ship_length) diff --git a/src/lib.rs b/src/lib.rs index a284a6d..045802e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,4 +5,5 @@ mod multipolygon_ext; pub use crate::multipolygon_ext::MultiPolygonExt; mod svg; -pub use crate::svg::load_multipolgon_from_svg; +pub use crate::svg::load_multipolygon_from_svg; +pub use crate::svg::load_multipolygon_from_svg_include; diff --git a/src/svg.rs b/src/svg.rs index 2db2e28..287bfae 100644 --- a/src/svg.rs +++ b/src/svg.rs @@ -3,12 +3,28 @@ use geo_types::{Coordinate, LineString, MultiPolygon, Polygon}; use std::error::Error; use svg::node::element::tag::Path; use svg::parser::Event; +use svg::Parser; + +/// load a multipolygon dynamically at runtime +pub fn load_multipolygon_from_svg(path: &str) -> Result, Box> { + let mut content = String::new(); + let parser = svg::open(path, &mut content)?; + parse_svg(parser) +} + +/// load a multipolygon statically at compiletime +/// with include_str! +pub fn load_multipolygon_from_svg_include( + content: &str, +) -> Result, Box> { + let parser = svg::read(content)?; + parse_svg(parser) +} #[allow(non_upper_case_globals)] -pub fn load_multipolgon_from_svg(path: &str) -> Result, Box> { - let mut content = String::new(); +fn parse_svg(parser: Parser) -> Result, Box> { let mut result: Vec> = Vec::new(); - for event in svg::open(path, &mut content)? { + for event in parser { match event { Event::Tag(Path, _, attributes) => { let data = attributes.get("d").unwrap();