feat: show how to inject svg into binary

This commit is contained in:
Ingolf Wagner 2021-04-10 03:25:03 +02:00
parent 4af31ce185
commit 65ff198dba
No known key found for this signature in database
GPG key ID: 76BF5F1928B9618B
4 changed files with 32 additions and 8 deletions

View file

@ -3,7 +3,7 @@
use geo::rotate::RotatePoint; use geo::rotate::RotatePoint;
use geo::translate::Translate; use geo::translate::Translate;
use geo::{Coordinate, Point}; 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::Context;
use polygon_art::MultiPolygonExt; use polygon_art::MultiPolygonExt;
use rand::Rng; use rand::Rng;
@ -12,7 +12,7 @@ fn main() {
let mut renderer = Context::create(); let mut renderer = Context::create();
let path = "pool/examples/example.svg"; 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.; let item_space = 110.;

View file

@ -6,10 +6,11 @@ use geo::LineString;
use geo::{Coordinate, MultiPolygon, Point, Polygon}; use geo::{Coordinate, MultiPolygon, Point, Polygon};
use geo_clipper::Clipper; use geo_clipper::Clipper;
use polygon_art::MultiPolygonExt; 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::prelude::StdRng;
use rand::Rng; use rand::Rng;
/// A more or less complete example that illustrates various technics
fn main() { fn main() {
let mut context = Context::create(); let mut context = Context::create();
let mut rng = context.gen_rng(); let mut rng = context.gen_rng();
@ -58,7 +59,13 @@ fn main() {
// load ships // load ships
let rotation = rng.gen_range(0..360); 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() .unwrap()
.rotate(90.) // rotate to scale ship with width .rotate(90.) // rotate to scale ship with width
.scale_to_width(ship_length) .scale_to_width(ship_length)

View file

@ -5,4 +5,5 @@ mod multipolygon_ext;
pub use crate::multipolygon_ext::MultiPolygonExt; pub use crate::multipolygon_ext::MultiPolygonExt;
mod svg; 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;

View file

@ -3,12 +3,28 @@ use geo_types::{Coordinate, LineString, MultiPolygon, Polygon};
use std::error::Error; use std::error::Error;
use svg::node::element::tag::Path; use svg::node::element::tag::Path;
use svg::parser::Event; use svg::parser::Event;
use svg::Parser;
/// load a multipolygon dynamically at runtime
pub fn load_multipolygon_from_svg(path: &str) -> Result<MultiPolygon<f64>, Box<dyn Error>> {
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<MultiPolygon<f64>, Box<dyn Error>> {
let parser = svg::read(content)?;
parse_svg(parser)
}
#[allow(non_upper_case_globals)] #[allow(non_upper_case_globals)]
pub fn load_multipolgon_from_svg(path: &str) -> Result<MultiPolygon<f64>, Box<dyn Error>> { fn parse_svg(parser: Parser) -> Result<MultiPolygon<f64>, Box<dyn Error>> {
let mut content = String::new();
let mut result: Vec<Polygon<f64>> = Vec::new(); let mut result: Vec<Polygon<f64>> = Vec::new();
for event in svg::open(path, &mut content)? { for event in parser {
match event { match event {
Event::Tag(Path, _, attributes) => { Event::Tag(Path, _, attributes) => {
let data = attributes.get("d").unwrap(); let data = attributes.get("d").unwrap();