feat: show how to inject svg into binary
This commit is contained in:
parent
4af31ce185
commit
65ff198dba
4 changed files with 32 additions and 8 deletions
|
@ -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.;
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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;
|
||||
|
|
22
src/svg.rs
22
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<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)]
|
||||
pub fn load_multipolgon_from_svg(path: &str) -> Result<MultiPolygon<f64>, Box<dyn Error>> {
|
||||
let mut content = String::new();
|
||||
fn parse_svg(parser: Parser) -> Result<MultiPolygon<f64>, Box<dyn Error>> {
|
||||
let mut result: Vec<Polygon<f64>> = 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();
|
||||
|
|
Loading…
Reference in a new issue