From 3d86835849b3b30104ec0e2cdba2b9fbbebdc87e Mon Sep 17 00:00:00 2001 From: Ingolf Wagner Date: Thu, 8 Apr 2021 22:06:39 +0200 Subject: [PATCH 1/7] feat: add asteroids.rs --- pool/asteroids/ship1.svg | 34 +++++++++++++++ pool/asteroids/ship2.svg | 34 +++++++++++++++ pool/asteroids/ship3.svg | 34 +++++++++++++++ src/bin/asteroids.rs | 91 ++++++++++++++++++++++++++++++++++++++++ src/context.rs | 18 ++++++-- 5 files changed, 207 insertions(+), 4 deletions(-) create mode 100644 pool/asteroids/ship1.svg create mode 100644 pool/asteroids/ship2.svg create mode 100644 pool/asteroids/ship3.svg create mode 100644 src/bin/asteroids.rs diff --git a/pool/asteroids/ship1.svg b/pool/asteroids/ship1.svg new file mode 100644 index 0000000..7837460 --- /dev/null +++ b/pool/asteroids/ship1.svg @@ -0,0 +1,34 @@ + + + + + + + image/svg+xml + + + + + + + + + diff --git a/pool/asteroids/ship2.svg b/pool/asteroids/ship2.svg new file mode 100644 index 0000000..580b04c --- /dev/null +++ b/pool/asteroids/ship2.svg @@ -0,0 +1,34 @@ + + + + + + + image/svg+xml + + + + + + + + + diff --git a/pool/asteroids/ship3.svg b/pool/asteroids/ship3.svg new file mode 100644 index 0000000..439e017 --- /dev/null +++ b/pool/asteroids/ship3.svg @@ -0,0 +1,34 @@ + + + + + + + image/svg+xml + + + + + + + + + diff --git a/src/bin/asteroids.rs b/src/bin/asteroids.rs new file mode 100644 index 0000000..e3ed65f --- /dev/null +++ b/src/bin/asteroids.rs @@ -0,0 +1,91 @@ +use geo::algorithm::intersects::Intersects; +use geo::algorithm::rotate::Rotate; +use geo::prelude::Translate; +use geo::rotate::RotatePoint; +use geo::{Coordinate, MultiPolygon, Point, Polygon}; +use geo::{LineString}; +use geo_clipper::Clipper; +use polygon_art::MultiPolygonExt; +use polygon_art::{load_multipolgon_from_svg, Context}; +use rand::prelude::StdRng; +use rand::Rng; + +fn main() { + let mut context = Context::create(); + let mut rng = context.gen_rng(); + + let ship_width = 100.; + let center_point = MultiPolygon(vec![create_asteroid(&mut rng, 20, 2)]) + .scale_to_width(2.5 * ship_width) + .translate(context.width / 2., context.height / 2.); + + // create asteroid + let mut asteroids: Vec> = Vec::new(); + for _ in 0..40 { + let without_orbit = rng.gen_range(40..140) as f64; + let with_orbit = without_orbit * 1.32; + let x = rng.gen_range(0..(context.width as i32)) as f64; + let y = rng.gen_range(0..(context.height as i32)) as f64; + let asteroid = MultiPolygon(vec![create_asteroid(&mut rng, 20, 15)]) + .scale_to_width(with_orbit) + .translate(x, y); + + if !center_point.intersects(&asteroid) && asteroids + .iter() + .find(|&m| *&m.intersects(&asteroid)) + .is_none() + { + asteroids.push(asteroid.scale_to_width(without_orbit)); + } + if asteroids.len() > 23 { + break; + } + } + + // load ships + let rotation = rng.gen_range(0..360); + let ship = load_multipolgon_from_svg("pool/asteroids/ship3.svg") + .unwrap() + .scale_to_width(ship_width) + .translate_center() + .rotate(rotation as f64); + context.translate_center(); + context.draw_multipolygon(&ship); + context.restore(); + + // drawing and clipping + let frame_space = 10.; + let window = context.frame(frame_space, frame_space, frame_space, frame_space); + context.draw_polygon(&window); + + for asteroid in asteroids.iter() { + context.draw_multipolygon(&asteroid.intersection(&window, 100.)); + } + + context.render(); +} + +fn create_asteroid(rng: &mut StdRng, subdivisions: i32, radius_randomization: i32) -> Polygon { + let mut asteroid_points: Vec> = Vec::new(); + for _ in 0..subdivisions { + let change = rng.gen_range(0..(radius_randomization * 2)); + asteroid_points.push(Point(Coordinate { + x: 0.0, + y: 100.0 + (change - radius_randomization) as f64, + })); + for point in asteroid_points.iter_mut() { + let (x, y) = point + .rotate_around_point( + 360. / subdivisions as f64, + Point(Coordinate { x: 0., y: 0. }), + ) + .x_y(); + point.set_x(x); + point.set_y(y); + } + } + Polygon::new( + LineString(asteroid_points.iter().map(|point| point.0).collect()), + Vec::new(), + ) +} diff --git a/src/context.rs b/src/context.rs index ab115d7..8983be5 100644 --- a/src/context.rs +++ b/src/context.rs @@ -1,6 +1,6 @@ use cairo::Format; use cairo::{ImageSurface, SvgSurface}; -use geo_types::{LineString, MultiPolygon, Polygon}; +use geo::{polygon, LineString, MultiPolygon, Polygon}; use rand::rngs::StdRng; use rand::{Rng, SeedableRng}; @@ -22,6 +22,16 @@ pub struct Context { } impl Context { + /// generate a frame for the image + pub fn frame(&mut self, left: f64, bottom: f64, right: f64, top: f64) -> Polygon { + polygon![ + (x: left, y: top), + (x: self.width - right, y: top), + (x: self.width - right, y: self.height - bottom), + (x: left , y: self.height - bottom), + ] + } + pub fn draw_multipolygon(&self, multi_polygon: &MultiPolygon) { for polygon in multi_polygon.iter() { self.draw_polygon(polygon); @@ -48,10 +58,7 @@ impl Context { } context.stroke(); } -} -// cairo facades -impl Context { /// restore state which was saved before by the context pub fn restore(&self) { self.render_container.context().restore(); @@ -81,6 +88,9 @@ impl Context { self.render_container.render(); } + pub fn gen_rng(&mut self) -> StdRng { + StdRng::seed_from_u64(self.pseudo_random_number_generator.gen()) + } pub fn create() -> Context { let opt = Opt::get_command_line_arguments(); let height = f64::from(opt.height.clone()); From 4af31ce1854d3b818db58c3abaf433e210366af7 Mon Sep 17 00:00:00 2001 From: Ingolf Wagner Date: Fri, 9 Apr 2021 08:19:09 +0200 Subject: [PATCH 2/7] feat: add predefined formats --- src/bin/asteroids.rs | 44 +++++++++++++++++++++++++------------- src/context.rs | 26 +++++++++++++++++----- src/context/commandline.rs | 19 +++++++++++++++- 3 files changed, 68 insertions(+), 21 deletions(-) diff --git a/src/bin/asteroids.rs b/src/bin/asteroids.rs index e3ed65f..076ff93 100644 --- a/src/bin/asteroids.rs +++ b/src/bin/asteroids.rs @@ -2,8 +2,8 @@ use geo::algorithm::intersects::Intersects; use geo::algorithm::rotate::Rotate; use geo::prelude::Translate; use geo::rotate::RotatePoint; +use geo::LineString; use geo::{Coordinate, MultiPolygon, Point, Polygon}; -use geo::{LineString}; use geo_clipper::Clipper; use polygon_art::MultiPolygonExt; use polygon_art::{load_multipolgon_from_svg, Context}; @@ -14,15 +14,28 @@ fn main() { let mut context = Context::create(); let mut rng = context.gen_rng(); - let ship_width = 100.; - let center_point = MultiPolygon(vec![create_asteroid(&mut rng, 20, 2)]) - .scale_to_width(2.5 * ship_width) - .translate(context.width / 2., context.height / 2.); + let width = context.width; + let height = context.height; + let diagonal = f64::sqrt(width * width + height * height); + + // todo : it does not make to much sense to define all parameters like this + // todo : it makes more sense to create a static (diagonal / width /height ratio ) setup and scale it up + let frame_offset = diagonal * 0.01; + let max_asteroid = (diagonal / 40.) as usize; + + let min_asteroid_size = diagonal / 40.; + let max_asteroid_size = diagonal / 12.; + let ship_length = diagonal / 23.; + let ship_orbit = 3.0; + + let center_point = MultiPolygon(vec![create_asteroid(&mut rng, 8, 2)]) + .scale_to_width(ship_orbit * ship_length) + .translate(width / 2., height / 2.); // create asteroid let mut asteroids: Vec> = Vec::new(); - for _ in 0..40 { - let without_orbit = rng.gen_range(40..140) as f64; + for _ in 0..(max_asteroid * 2) { + let without_orbit = rng.gen_range(min_asteroid_size..max_asteroid_size) as f64; let with_orbit = without_orbit * 1.32; let x = rng.gen_range(0..(context.width as i32)) as f64; let y = rng.gen_range(0..(context.height as i32)) as f64; @@ -30,14 +43,15 @@ fn main() { .scale_to_width(with_orbit) .translate(x, y); - if !center_point.intersects(&asteroid) && asteroids - .iter() - .find(|&m| *&m.intersects(&asteroid)) - .is_none() + if (!center_point.intersects(&asteroid)) + && asteroids + .iter() + .find(|&m| *&m.intersects(&asteroid)) + .is_none() { asteroids.push(asteroid.scale_to_width(without_orbit)); } - if asteroids.len() > 23 { + if asteroids.len() > max_asteroid { break; } } @@ -46,7 +60,8 @@ fn main() { let rotation = rng.gen_range(0..360); let ship = load_multipolgon_from_svg("pool/asteroids/ship3.svg") .unwrap() - .scale_to_width(ship_width) + .rotate(90.) // rotate to scale ship with width + .scale_to_width(ship_length) .translate_center() .rotate(rotation as f64); context.translate_center(); @@ -54,8 +69,7 @@ fn main() { context.restore(); // drawing and clipping - let frame_space = 10.; - let window = context.frame(frame_space, frame_space, frame_space, frame_space); + let window = context.frame(frame_offset, frame_offset, frame_offset, frame_offset); context.draw_polygon(&window); for asteroid in asteroids.iter() { diff --git a/src/context.rs b/src/context.rs index 8983be5..0919b70 100644 --- a/src/context.rs +++ b/src/context.rs @@ -91,12 +91,29 @@ impl Context { pub fn gen_rng(&mut self) -> StdRng { StdRng::seed_from_u64(self.pseudo_random_number_generator.gen()) } + pub fn create() -> Context { let opt = Opt::get_command_line_arguments(); - let height = f64::from(opt.height.clone()); - let width = f64::from(opt.width.clone()); let line_size = opt.line_size.clone(); + let mm = 3.779527547619048; // calculated with inkscape 1587.40157 (px) / 420 (mm) (from a3) + let (mut height, mut width) = match opt.format { + None => (f64::from(opt.height.clone()), f64::from(opt.width.clone())), + Some(format) => match &format[..] { + "a3" => (297. * mm, 420. * mm), + "a4" => (210. * mm, 297. * mm), + "a5" => (148. * mm, 210. * mm), + "x220" => (768., 1366.), + _ => (f64::from(opt.height.clone()), f64::from(opt.width.clone())), + }, + }; + + if opt.orientation == "portrait" { + let cache = height; + height = width; + width = cache; + } + let random_seed = match opt.random_seed { Some(random_seed) => random_seed, None => match opt.random_seed_date { @@ -128,9 +145,8 @@ impl Context { .expect("Can't svg surface"); Box::new(SvgRenderContainer::new(svg_surface, palette)) as Box } else if opt.output_type == "png" { - let png_surface = - ImageSurface::create(Format::Rgb24, opt.width.clone(), opt.height.clone()) - .expect("Can't create png surface"); + let png_surface = ImageSurface::create(Format::Rgb24, width as i32, height as i32) + .expect("Can't create png surface"); Box::new(PngRenderContainer::new( opt.output.to_string_lossy().to_string(), png_surface, diff --git a/src/context/commandline.rs b/src/context/commandline.rs index f019227..b246a6c 100644 --- a/src/context/commandline.rs +++ b/src/context/commandline.rs @@ -13,13 +13,30 @@ pub struct Opt { pub output: PathBuf, /// define width in pixels - #[structopt(long, default_value = "400")] + #[structopt(long, default_value = "600")] pub width: i32, /// define height in pixels #[structopt(long, default_value = "400")] pub height: i32, + /// use a predefined format + #[structopt( + long, + conflicts_with="height", + conflicts_with="width", + possible_values=&["a3", "a4", "a5", "x220"] + )] + pub format: Option, + + /// change orientation + #[structopt( + long, + default_value="landscape", + possible_values=&["portrait", "landscape"] + )] + pub orientation: String, + /// define line size in pixels, all lines will be this thick #[structopt(long, default_value = "1.0")] pub line_size: f64, From 65ff198dba4d6806baaa43db8be623cec2195517 Mon Sep 17 00:00:00 2001 From: Ingolf Wagner Date: Sat, 10 Apr 2021 03:25:03 +0200 Subject: [PATCH 3/7] 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(); From 54fa1b222d9efc6de48bfbc59ec69a12ab358bcc Mon Sep 17 00:00:00 2001 From: Ingolf Wagner Date: Sat, 10 Apr 2021 13:33:56 +0200 Subject: [PATCH 4/7] feat: introduce simple font from asteroid game --- Cargo.lock | 192 +++++++++++++++++++++++++++++++++++++------ Cargo.toml | 2 +- examples/clipping.rs | 2 +- examples/font.rs | 47 +++++++++++ examples/svg.rs | 1 - shell.nix | 4 +- src/bin/asteroids.rs | 1 + src/context.rs | 17 ++-- src/font.rs | 133 ++++++++++++++++++++++++++++++ src/lib.rs | 4 + 10 files changed, 371 insertions(+), 32 deletions(-) create mode 100644 examples/font.rs create mode 100644 src/font.rs diff --git a/Cargo.lock b/Cargo.lock index 6ecd8ad..c5ec21a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -29,6 +29,11 @@ dependencies = [ "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "anyhow" +version = "1.0.40" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "approx" version = "0.3.2" @@ -119,25 +124,26 @@ dependencies = [ [[package]] name = "cairo-rs" -version = "0.8.1" +version = "0.9.1" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)", - "cairo-sys-rs 0.9.2 (registry+https://github.com/rust-lang/crates.io-index)", - "glib 0.9.3 (registry+https://github.com/rust-lang/crates.io-index)", - "glib-sys 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)", - "gobject-sys 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)", + "cairo-sys-rs 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)", + "glib 0.10.3 (registry+https://github.com/rust-lang/crates.io-index)", + "glib-sys 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)", + "gobject-sys 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)", + "thiserror 1.0.24 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "cairo-sys-rs" -version = "0.9.2" +version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "glib-sys 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)", + "glib-sys 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)", - "pkg-config 0.3.17 (registry+https://github.com/rust-lang/crates.io-index)", + "system-deps 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -454,7 +460,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "glib" -version = "0.9.3" +version = "0.10.3" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -463,19 +469,35 @@ dependencies = [ "futures-executor 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", "futures-task 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", "futures-util 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", - "glib-sys 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)", - "gobject-sys 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)", - "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "glib-macros 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)", + "glib-sys 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)", + "gobject-sys 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)", + "once_cell 1.7.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "glib-macros" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "anyhow 1.0.40 (registry+https://github.com/rust-lang/crates.io-index)", + "heck 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "itertools 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", + "proc-macro-crate 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", + "proc-macro-error 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", + "proc-macro2 1.0.26 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 1.0.68 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "glib-sys" -version = "0.9.1" +version = "0.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)", - "pkg-config 0.3.17 (registry+https://github.com/rust-lang/crates.io-index)", + "system-deps 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -485,12 +507,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "gobject-sys" -version = "0.9.1" +version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "glib-sys 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)", + "glib-sys 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)", - "pkg-config 0.3.17 (registry+https://github.com/rust-lang/crates.io-index)", + "system-deps 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -552,6 +574,14 @@ dependencies = [ "either 1.6.1 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "itertools" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "either 1.6.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "lazy_static" version = "1.4.0" @@ -629,6 +659,11 @@ name = "object" version = "0.22.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "once_cell" +version = "1.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "palette" version = "0.5.0" @@ -709,7 +744,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" name = "polygon-art" version = "1.0.0" dependencies = [ - "cairo-rs 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", + "cairo-rs 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)", "geo 0.16.0 (registry+https://github.com/rust-lang/crates.io-index)", "geo-clipper 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "geo-svg-io 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -726,6 +761,14 @@ name = "ppv-lite86" version = "0.2.10" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "proc-macro-crate" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "toml 0.5.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "proc-macro-error" version = "0.4.9" @@ -738,6 +781,18 @@ dependencies = [ "syn 1.0.68 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "proc-macro-error" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro-error-attr 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", + "proc-macro2 1.0.26 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 1.0.68 (registry+https://github.com/rust-lang/crates.io-index)", + "version_check 0.9.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "proc-macro-error-attr" version = "0.4.9" @@ -750,6 +805,16 @@ dependencies = [ "syn-mid 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "proc-macro-error-attr" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 1.0.26 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "version_check 0.9.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "proc-macro-hack" version = "0.5.11" @@ -939,6 +1004,11 @@ dependencies = [ "syn 1.0.68 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "serde" +version = "1.0.125" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "shlex" version = "0.1.1" @@ -996,6 +1066,22 @@ dependencies = [ "syn 1.0.68 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "strum" +version = "0.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "strum_macros" +version = "0.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "heck 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "proc-macro2 1.0.26 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 1.0.68 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "svg" version = "0.9.1" @@ -1041,6 +1127,20 @@ dependencies = [ "unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "system-deps" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "heck 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "pkg-config 0.3.17 (registry+https://github.com/rust-lang/crates.io-index)", + "strum 0.18.0 (registry+https://github.com/rust-lang/crates.io-index)", + "strum_macros 0.18.0 (registry+https://github.com/rust-lang/crates.io-index)", + "thiserror 1.0.24 (registry+https://github.com/rust-lang/crates.io-index)", + "toml 0.5.8 (registry+https://github.com/rust-lang/crates.io-index)", + "version-compare 0.0.10 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "termcolor" version = "1.1.2" @@ -1057,6 +1157,24 @@ dependencies = [ "unicode-width 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "thiserror" +version = "1.0.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "thiserror-impl 1.0.24 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 1.0.26 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 1.0.68 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "threadpool" version = "1.8.1" @@ -1065,6 +1183,14 @@ dependencies = [ "num_cpus 1.13.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "toml" +version = "0.5.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "serde 1.0.125 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "typenum" version = "1.13.0" @@ -1090,6 +1216,11 @@ name = "vec_map" version = "0.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "version-compare" +version = "0.0.10" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "version_check" version = "0.9.3" @@ -1150,6 +1281,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum adler 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" "checksum aho-corasick 0.7.15 (registry+https://github.com/rust-lang/crates.io-index)" = "7404febffaa47dac81aa44dba71523c9d069b1bdc50a77db41195149e17f68e5" "checksum ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b" +"checksum anyhow 1.0.40 (registry+https://github.com/rust-lang/crates.io-index)" = "28b2cd92db5cbd74e8e5028f7e27dd7aa3090e89e4f2a197cc7c8dfb69c7063b" "checksum approx 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f0e60b75072ecd4168020818c0107f2857bb6c4e64252d8d3983f6263b40a5c3" "checksum as-slice 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "45403b49e3954a4b8428a0ac21a4b7afadccf92bfd96273f1a58cd4812496ae0" "checksum atty 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)" = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" @@ -1159,8 +1291,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" "checksum byteorder 1.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" "checksum c2-chacha 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "214238caa1bf3a496ec3392968969cab8549f96ff30652c9e56885329315f6bb" -"checksum cairo-rs 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "157049ba9618aa3a61c39d5d785102c04d3b1f40632a706c621a9aedc21e6084" -"checksum cairo-sys-rs 0.9.2 (registry+https://github.com/rust-lang/crates.io-index)" = "ff65ba02cac715be836f63429ab00a767d48336efc5497c5637afb53b4f14d63" +"checksum cairo-rs 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c5c0f2e047e8ca53d0ff249c54ae047931d7a6ebe05d00af73e0ffeb6e34bdb8" +"checksum cairo-sys-rs 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2ed2639b9ad5f1d6efa76de95558e11339e7318426d84ac4890b86c03e828ca7" "checksum cc 1.0.67 (registry+https://github.com/rust-lang/crates.io-index)" = "e3c69b077ad434294d3ce9f1f6143a2a4b89a8a2d54ef813d85003a4fd1137fd" "checksum cexpr 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f4aedb84272dbe89af497cf81375129abda4fc0a9e7c5d317498c15cc30c0d27" "checksum cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)" = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" @@ -1196,10 +1328,11 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum getrandom 0.1.14 (registry+https://github.com/rust-lang/crates.io-index)" = "7abc8dd8451921606d809ba32e95b6111925cd2906060d2dcc29c070220503eb" "checksum getrandom 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "c9495705279e7140bf035dde1f6e750c162df8b625267cd52cc44e0b156732c8" "checksum gimli 0.23.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f6503fe142514ca4799d4c26297c4248239fe8838d827db6bd6065c6ed29a6ce" -"checksum glib 0.9.3 (registry+https://github.com/rust-lang/crates.io-index)" = "40fb573a09841b6386ddf15fd4bc6655b4f5b106ca962f57ecaecde32a0061c0" -"checksum glib-sys 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)" = "95856f3802f446c05feffa5e24859fe6a183a7cb849c8449afc35c86b1e316e2" +"checksum glib 0.10.3 (registry+https://github.com/rust-lang/crates.io-index)" = "0c685013b7515e668f1b57a165b009d4d28cb139a8a989bbd699c10dad29d0c5" +"checksum glib-macros 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)" = "41486a26d1366a8032b160b59065a59fb528530a46a49f627e7048fb8c064039" +"checksum glib-sys 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c7e9b997a66e9a23d073f2b1abb4dbfc3925e0b8952f67efd8d9b6e168e4cdc1" "checksum glob 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574" -"checksum gobject-sys 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)" = "31d1a804f62034eccf370006ccaef3708a71c31d561fee88564abe71177553d9" +"checksum gobject-sys 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "952133b60c318a62bf82ee75b93acc7e84028a093e06b9e27981c2b6fe68218c" "checksum hash32 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d4041af86e63ac4298ce40e5cca669066e75b6f1aa3390fe2561ffa5e1d9f4cc" "checksum heapless 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)" = "74911a68a1658cfcfb61bc0ccfbd536e3b6e906f8c2f7883ee50157e3e2184f1" "checksum heck 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "20564e78d53d2bb135c343b3f47714a56af2061f1c928fdb541dc7b9fdd94205" @@ -1207,6 +1340,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum humantime 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "df004cfca50ef23c36850aaaa59ad52cc70d0e90243c3c7737a4dd32dc7a3c4f" "checksum iso8601 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "cee08a007a59a8adfc96f69738ddf59e374888dfd84b49c4b916543067644d58" "checksum itertools 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f56a2d0bc861f9165be4eb3442afd3c236d8a98afd426f65d92324ae1091a484" +"checksum itertools 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "284f18f85651fe11e8a991b2adb42cb078325c996ed026d994719efcfca1d54b" "checksum lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" "checksum lazycell 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55" "checksum libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)" = "d515b1f41455adea1313a4a2ac8a8a477634fbae63cc6100e3aebb207ce61558" @@ -1218,6 +1352,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum num-traits 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)" = "9a64b1ec5cda2586e284722486d802acf1f7dbdc623e2bfc57e65ca1cd099290" "checksum num_cpus 1.13.0 (registry+https://github.com/rust-lang/crates.io-index)" = "05499f3756671c15885fee9034446956fff3f243d6077b91e5767df161f766b3" "checksum object 0.22.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8d3b63360ec3cb337817c2dbd47ab4a0f170d285d8e5a2064600f3def1402397" +"checksum once_cell 1.7.2 (registry+https://github.com/rust-lang/crates.io-index)" = "af8b08b04175473088b46763e51ee54da5f9a164bc162f615b91bc179dbf15a3" "checksum palette 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a05c0334468e62a4dfbda34b29110aa7d70d58c7fdb2c9857b5874dd9827cc59" "checksum palette_derive 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "0b4b5f600e60dd3a147fb57b4547033d382d1979eb087af310e91cb45a63b1f4" "checksum pdqselect 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4ec91767ecc0a0bbe558ce8c9da33c068066c57ecc8bb8477ef8c1ad3ef77c27" @@ -1229,8 +1364,11 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum pin-utils 0.1.0-alpha.4 (registry+https://github.com/rust-lang/crates.io-index)" = "5894c618ce612a3fa23881b152b608bafb8c56cfc22f434a3ba3120b40f7b587" "checksum pkg-config 0.3.17 (registry+https://github.com/rust-lang/crates.io-index)" = "05da548ad6865900e60eaba7f589cc0783590a92e940c26953ff81ddbab2d677" "checksum ppv-lite86 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)" = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857" +"checksum proc-macro-crate 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "1d6ea3c4595b96363c13943497db34af4460fb474a95c43f4446ad341b8c9785" "checksum proc-macro-error 0.4.9 (registry+https://github.com/rust-lang/crates.io-index)" = "052b3c9af39c7e5e94245f820530487d19eb285faedcb40e0c3275132293f242" +"checksum proc-macro-error 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" "checksum proc-macro-error-attr 0.4.9 (registry+https://github.com/rust-lang/crates.io-index)" = "d175bef481c7902e63e3165627123fff3502f06ac043d3ef42d08c1246da9253" +"checksum proc-macro-error-attr 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" "checksum proc-macro-hack 0.5.11 (registry+https://github.com/rust-lang/crates.io-index)" = "ecd45702f76d6d3c75a80564378ae228a85f0b59d2f3ed43c91b4a69eb2ebfc5" "checksum proc-macro-nested 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "369a6ed065f249a159e06c45752c780bda2fb53c995718f9e484d08daa9eb42e" "checksum proc-macro2 1.0.26 (registry+https://github.com/rust-lang/crates.io-index)" = "a152013215dca273577e18d2bf00fa862b89b24169fb78c4c95aeb07992c9cec" @@ -1255,6 +1393,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum rustc-demangle 0.1.18 (registry+https://github.com/rust-lang/crates.io-index)" = "6e3bad0ee36814ca07d7968269dd4b7ec89ec2da10c4bb613928d3077083c232" "checksum rustc-hash 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" "checksum rustversion 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "b3bba175698996010c4f6dce5e7f173b6eb781fce25d2cfc45e27091ce0b79f6" +"checksum serde 1.0.125 (registry+https://github.com/rust-lang/crates.io-index)" = "558dc50e1a5a5fa7112ca2ce4effcb321b0300c0d4ccf0776a9f60cd89031171" "checksum shlex 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7fdf1b9db47230893d76faad238fd6097fd6d6a9245cd7a4d90dbd639536bbd2" "checksum siphasher 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "0b8de496cf83d4ed58b6be86c3a275b8602f6ffe98d3024a869e124147a9a3ac" "checksum siphasher 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "cbce6d4507c7e4a3962091436e56e95290cb71fa302d0d270e32130b75fbff27" @@ -1264,19 +1403,26 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum strsim 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" "checksum structopt 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "a1bcbed7d48956fcbb5d80c6b95aedb553513de0a1b451ea92679d999c010e98" "checksum structopt-derive 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "095064aa1f5b94d14e635d0a5684cf140c43ae40a0fd990708d38f5d669e5f64" +"checksum strum 0.18.0 (registry+https://github.com/rust-lang/crates.io-index)" = "57bd81eb48f4c437cadc685403cad539345bf703d78e63707418431cecd4522b" +"checksum strum_macros 0.18.0 (registry+https://github.com/rust-lang/crates.io-index)" = "87c85aa3f8ea653bfd3ddf25f7ee357ee4d204731f6aa9ad04002306f6e2774c" "checksum svg 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a95656b5b37251de210aa2751d538b6f31ce375bacf725a32223bfa6344e0776" "checksum svgtypes 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9c536faaff1a10837cfe373142583f6e27d81e96beba339147e77b67c9f260ff" "checksum syn 1.0.68 (registry+https://github.com/rust-lang/crates.io-index)" = "3ce15dd3ed8aa2f8eeac4716d6ef5ab58b6b9256db41d7e1a0224c2788e8fd87" "checksum syn-mid 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7be3539f6c128a931cf19dcee741c1af532c7fd387baa739c03dd2e96479338a" "checksum synstructure 0.12.4 (registry+https://github.com/rust-lang/crates.io-index)" = "b834f2d66f734cb897113e34aaff2f1ab4719ca946f9a7358dba8f8064148701" +"checksum system-deps 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "0f3ecc17269a19353b3558b313bba738b25d82993e30d62a18406a24aba4649b" "checksum termcolor 1.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "2dfed899f0eb03f32ee8c6a0aabdb8a7949659e3466561fc0adf54e26d88c5f4" "checksum textwrap 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" +"checksum thiserror 1.0.24 (registry+https://github.com/rust-lang/crates.io-index)" = "e0f4a65597094d4483ddaed134f409b2cb7c1beccf25201a9f73c719254fa98e" +"checksum thiserror-impl 1.0.24 (registry+https://github.com/rust-lang/crates.io-index)" = "7765189610d8241a44529806d6fd1f2e0a08734313a35d5b3a556f92b381f3c0" "checksum threadpool 1.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d050e60b33d41c19108b32cea32164033a9013fe3b46cbd4457559bfbf77afaa" +"checksum toml 0.5.8 (registry+https://github.com/rust-lang/crates.io-index)" = "a31142970826733df8241ef35dc040ef98c679ab14d7c3e54d827099b3acecaa" "checksum typenum 1.13.0 (registry+https://github.com/rust-lang/crates.io-index)" = "879f6906492a7cd215bfa4cf595b600146ccfac0c79bcbd1f3000162af5e8b06" "checksum unicode-segmentation 1.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e83e153d1053cbb5a118eeff7fd5be06ed99153f00dbcd8ae310c5fb2b22edc0" "checksum unicode-width 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "caaa9d531767d1ff2150b9332433f32a24622147e5ebb1f26409d5da67afd479" "checksum unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c" "checksum vec_map 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "05c78687fb1a80548ae3250346c3db86a80a7cdd77bda190189f2d0a0987c81a" +"checksum version-compare 0.0.10 (registry+https://github.com/rust-lang/crates.io-index)" = "d63556a25bae6ea31b52e640d7c41d1ab27faba4ccb600013837a3d0b3994ca1" "checksum version_check 0.9.3 (registry+https://github.com/rust-lang/crates.io-index)" = "5fecdca9a5291cc2b8dcf7dc02453fee791a280f3743cb0905f8822ae463b3fe" "checksum wasi 0.10.2+wasi-snapshot-preview1 (registry+https://github.com/rust-lang/crates.io-index)" = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6" "checksum wasi 0.9.0+wasi-snapshot-preview1 (registry+https://github.com/rust-lang/crates.io-index)" = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" diff --git a/Cargo.toml b/Cargo.toml index acb5b65..5994e77 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,7 +17,7 @@ maintenance = { status = "actively-developed" } exclude = [ "shell.nix", "default.nix" ] [dependencies] -cairo-rs = { version = "0.8", features = [ "png", "svg"] } +cairo-rs = { version = "0.9", features = [ "png", "svg" ] } geo = "0.16" geo-clipper = "0.4.0" geo-svg-io = "0.1.1" diff --git a/examples/clipping.rs b/examples/clipping.rs index 8d02833..03e4666 100644 --- a/examples/clipping.rs +++ b/examples/clipping.rs @@ -37,6 +37,7 @@ fn main() { context.draw_multipolygon(union); // intersection + context.save(); context.translate(0.0, 250.); let intersection = &polygon_a.intersection(&polygon_b, 10.0); context.draw_multipolygon(intersection); @@ -51,7 +52,6 @@ fn main() { context.translate(0., 250.); let difference = &polygon_b.difference(&polygon_a, 10.0); context.draw_multipolygon(difference); - context.restore(); context.render(); } diff --git a/examples/font.rs b/examples/font.rs new file mode 100644 index 0000000..0ddfad1 --- /dev/null +++ b/examples/font.rs @@ -0,0 +1,47 @@ +//! Shows how to use buildin font + +use geo::line_string; +use polygon_art::{AsteroidFont, Context, FontDirection}; +use FontDirection::{LeftRight, TopDown}; + +fn main() { + let context = Context::create(); + context.save(); + + context.translate(70., 50.); + // context.scale(8.); + + let font = AsteroidFont::new(); + let line = font.get_text("Hey there!".to_string(), 12., LeftRight); + for char in line.iter() { + context.draw_line_string(char); + } + context.draw_line_string(&line_string![(x : 0., y:14.), (x: 1000. , y : 14.)]); + + context.translate(0.0, 18.); + let line = font.get_text("How are you?".to_string(), 22., LeftRight); + for char in line.iter() { + context.draw_line_string(char); + } + + context.translate(0.0, 28.); + let line = font.get_text("Here a bitter text!".to_string(), 50., LeftRight); + for char in line.iter() { + context.draw_line_string(char); + } + + context.translate(0.0, 56.); + let line = font.get_text("0123456789".to_string(), 50., LeftRight); + for char in line.iter() { + context.draw_line_string(char); + } + + context.restore(); + context.translate(30., 50.); + let line = font.get_text("0123456789".to_string(), 30., TopDown); + for char in line.iter() { + context.draw_line_string(char); + } + + context.render(); +} diff --git a/examples/svg.rs b/examples/svg.rs index f45ee6f..5543e4d 100644 --- a/examples/svg.rs +++ b/examples/svg.rs @@ -43,6 +43,5 @@ fn main() { renderer.draw_multipolygon(&svg); } - renderer.restore(); renderer.render(); } diff --git a/shell.nix b/shell.nix index bd398cb..c01c489 100644 --- a/shell.nix +++ b/shell.nix @@ -12,6 +12,7 @@ pkgs.mkShell { clang llvm + pkg-config (pkgs.writers.writeBashBin "reformat" '' for file in `find ${toString ./.} -type f | egrep "\.rs$"` @@ -24,7 +25,8 @@ pkgs.mkShell { # run this on start # ----------------- shellHook = '' - export NIX_ENFORCE_PURITY=0 + # export NIX_ENFORCE_PURITY=0 + # Needed so bindgen can find libclang.so export LIBCLANG_PATH="${pkgs.llvmPackages.libclang}/lib"; ''; diff --git a/src/bin/asteroids.rs b/src/bin/asteroids.rs index 778bada..fc023f2 100644 --- a/src/bin/asteroids.rs +++ b/src/bin/asteroids.rs @@ -71,6 +71,7 @@ fn main() { .scale_to_width(ship_length) .translate_center() .rotate(rotation as f64); + context.save(); context.translate_center(); context.draw_multipolygon(&ship); context.restore(); diff --git a/src/context.rs b/src/context.rs index 0919b70..195dc0e 100644 --- a/src/context.rs +++ b/src/context.rs @@ -32,6 +32,11 @@ impl Context { ] } + /// a fall back operation, if you need a function not provided by this struct. + pub fn cairo_context(&self) -> &cairo::Context { + self.render_container.context() + } + pub fn draw_multipolygon(&self, multi_polygon: &MultiPolygon) { for polygon in multi_polygon.iter() { self.draw_polygon(polygon); @@ -39,13 +44,13 @@ impl Context { } pub fn draw_polygon(&self, polygon: &Polygon) { - self.plot_line_string(polygon.exterior()); + self.draw_line_string(polygon.exterior()); for interior in polygon.interiors() { - self.plot_line_string(interior); + self.draw_line_string(interior); } } - fn plot_line_string(&self, line_string: &LineString) { + pub fn draw_line_string(&self, line_string: &LineString) { let context = self.render_container.context(); let mut init = true; for point in line_string.clone().into_points() { @@ -64,10 +69,13 @@ impl Context { self.render_container.context().restore(); } + /// save state to restore later again + pub fn save(&self) { + self.render_container.context().save(); + } /// scale, but line with stays pub fn scale(&self, factor: f64) { let context = self.render_container.context(); - context.save(); context.scale(factor, factor); context.set_line_width(1. / factor); } @@ -80,7 +88,6 @@ impl Context { /// translate to a position pub fn translate(&self, tx: f64, ty: f64) { let context = self.render_container.context(); - context.save(); context.translate(tx, ty); } diff --git a/src/font.rs b/src/font.rs new file mode 100644 index 0000000..2b61642 --- /dev/null +++ b/src/font.rs @@ -0,0 +1,133 @@ +// props to https://trmm.net/Asteroids_font +// https://github.com/osresearch/vst/blob/master/teensyv/asteroids_font.c + +use geo::algorithm::translate::Translate; +use geo::{Coordinate, LineString}; +use std::collections::HashMap; + +pub struct AsteroidFont { + letters: HashMap>>, +} + +impl AsteroidFont { + #[rustfmt::skip] + pub fn new() -> Self { + let mut letters = HashMap::new(); + letters.insert('0', vec![vec![(0, 0), (8, 0), (8, 12), (0, 12), (0, 0), (8, 12)]]); + letters.insert('1', vec![vec![(4, 0), (4, 12), (3, 10)]]); + letters.insert('2', vec![vec![(0, 12), (8, 12), (8, 7), (0, 5), (0, 0), (8, 0)]]); + letters.insert('3', vec![vec![(0, 12), (8, 12), (8, 0), (0, 0), ], vec![(0, 6), (8, 6)]]); + letters.insert('4', vec![vec![(0, 12), (0, 6), (8, 6), ], vec![(8, 12), (8, 0)]]); + letters.insert('5', vec![vec![(0, 0), (8, 0), (8, 6), (0, 7), (0, 12), (8, 12)]]); + letters.insert('6', vec![vec![(0, 12), (0, 0), (8, 0), (8, 5), (0, 7)]]); + letters.insert('7', vec![vec![(0, 12), (8, 12), (8, 6), (4, 0)]]); + letters.insert('8', vec![vec![(0, 0), (8, 0), (8, 12), (0, 12), (0, 0), ], vec![(0, 6), (8, 6)]]); + letters.insert('9', vec![vec![(8, 0), (8, 12), (0, 12), (0, 7), (8, 5)]]); + letters.insert(' ', vec![]); + letters.insert('.', vec![vec![(3, 0), (4, 0)]]); + letters.insert(',', vec![vec![(2, 0), (4, 2)]]); + letters.insert('-', vec![vec![(2, 6), (6, 6)]]); + letters.insert('+', vec![vec![(1, 6), (7, 6), ], vec![(4, 9), (4, 3)]]); + letters.insert('!', vec![vec![(4, 0), (3, 2), (5, 2), (4, 0), ], vec![(4, 4), (4, 12)]]); + letters.insert('#', vec![vec![(0, 4), (8, 4), (6, 2), (6, 10), (8, 8), (0, 8), (2, 10), (2, 2)]]); + letters.insert('^', vec![vec![(2, 6), (4, 12), (6, 6)]]); + letters.insert('=', vec![vec![(1, 4), (7, 4), ], vec![(1, 8), (7, 8)]]); + letters.insert('*', vec![vec![(0, 0), (4, 12), (8, 0), (0, 8), (8, 8), (0, 0)]]); + letters.insert('_', vec![vec![(0, 0), (8, 0)]]); + letters.insert('/', vec![vec![(0, 0), (8, 12)]]); + letters.insert('\\', vec![vec![(0, 12), (8, 0)]]); + letters.insert('@', vec![vec![(8, 4), (4, 0), (0, 4), (0, 8), (4, 12), (8, 8), (4, 4), (3, 6)]]); + letters.insert('$', vec![vec![(6, 2), (2, 6), (6, 10), ], vec![(4, 12), (4, 0)]]); + letters.insert('&', vec![vec![(8, 0), (4, 12), (8, 8), (0, 4), (4, 0), (8, 4)]]); + letters.insert('[', vec![vec![(6, 0), (2, 0), (2, 12), (6, 12)]]); + letters.insert(']', vec![vec![(2, 0), (6, 0), (6, 12), (2, 12)]]); + letters.insert('(', vec![vec![(6, 0), (2, 4), (2, 8), (6, 12)]]); + letters.insert(')', vec![vec![(2, 0), (6, 4), (6, 8), (2, 12)]]); + letters.insert('{', vec![vec![(6, 0), (4, 2), (4, 10), (6, 12), ], vec![(2, 6), (4, 6)]]); + letters.insert('}', vec![vec![(4, 0), (6, 2), (6, 10), (4, 12), ], vec![(6, 6), (8, 6)]]); + letters.insert('%', vec![vec![(0, 0), (8, 12), ], vec![(2, 10), (2, 8), ], vec![(6, 4), (6, 2)]]); + letters.insert('<', vec![vec![(6, 0), (2, 6), (6, 12)]]); + letters.insert('>', vec![vec![(2, 0), (6, 6), (2, 12)]]); + letters.insert('|', vec![vec![(4, 0), (4, 5), ], vec![(4, 6), (4, 12)]]); + letters.insert(':', vec![vec![(4, 9), (4, 7), ], vec![(4, 5), (4, 3)]]); + letters.insert(';', vec![vec![(4, 9), (4, 7), ], vec![(4, 5), (1, 2)]]); + letters.insert('"', vec![vec![(2, 10), (2, 6), ], vec![(6, 10), (6, 6)]]); + letters.insert('\'', vec![vec![(2, 6), (6, 10)]]); + letters.insert('`', vec![vec![(2, 10), (6, 6)]]); + letters.insert('~', vec![vec![(0, 4), (2, 8), (6, 4), (8, 8)]]); + letters.insert('?', vec![vec![(0, 8), (4, 12), (8, 8), (4, 4), ], vec![(4, 1), (4, 0)]]); + letters.insert('a', vec![vec![(0, 0), (0, 8), (4, 12), (8, 8), (8, 0), ], vec![(0, 4), (8, 4)]]); + letters.insert('b', vec![vec![(0, 0), (0, 12), (4, 12), (8, 10), (4, 6), (8, 2), (4, 0), (0, 0)]]); + letters.insert('c', vec![vec![(8, 0), (0, 0), (0, 12), (8, 12)]]); + letters.insert('d', vec![vec![(0, 0), (0, 12), (4, 12), (8, 8), (8, 4), (4, 0), (0, 0)]]); + letters.insert('e', vec![vec![(8, 0), (0, 0), (0, 12), (8, 12), ], vec![(0, 6), (6, 6)]]); + letters.insert('f', vec![vec![(0, 0), (0, 12), (8, 12), ], vec![(0, 6), (6, 6)]]); + letters.insert('g', vec![vec![(6, 6), (8, 4), (8, 0), (0, 0), (0, 12), (8, 12)]]); + letters.insert('h', vec![vec![(0, 0), (0, 12), ], vec![(0, 6), (8, 6), ], vec![(8, 12), (8, 0)]]); + letters.insert('i', vec![vec![(0, 0), (8, 0), ], vec![(4, 0), (4, 12), ], vec![(0, 12), (8, 12)]]); + letters.insert('j', vec![vec![(0, 4), (4, 0), (8, 0), (8, 12)]]); + letters.insert('k', vec![vec![(0, 0), (0, 12), ], vec![(8, 12), (0, 6), (6, 0)]]); + letters.insert('l', vec![vec![(8, 0), (0, 0), (0, 12)]]); + letters.insert('m', vec![vec![(0, 0), (0, 12), (4, 8), (8, 12), (8, 0)]]); + letters.insert('n', vec![vec![(0, 0), (0, 12), (8, 0), (8, 12)]]); + letters.insert('o', vec![vec![(0, 0), (0, 12), (8, 12), (8, 0), (0, 0)]]); + letters.insert('p', vec![vec![(0, 0), (0, 12), (8, 12), (8, 6), (0, 5)]]); + letters.insert('q', vec![vec![(0, 0), (0, 12), (8, 12), (8, 4), (0, 0), ], vec![(4, 4), (8, 0)]]); + letters.insert('r', vec![vec![(0, 0), (0, 12), (8, 12), (8, 6), (0, 5), ], vec![(4, 5), (8, 0)]]); + letters.insert('s', vec![vec![(0, 2), (2, 0), (8, 0), (8, 5), (0, 7), (0, 12), (6, 12), (8, 10)]]); + letters.insert('t', vec![vec![(0, 12), (8, 12), ], vec![(4, 12), (4, 0)]]); + letters.insert('u', vec![vec![(0, 12), (0, 2), (4, 0), (8, 2), (8, 12)]]); + letters.insert('v', vec![vec![(0, 12), (4, 0), (8, 12)]]); + letters.insert('w', vec![vec![(0, 12), (2, 0), (4, 4), (6, 0), (8, 12)]]); + letters.insert('x', vec![vec![(0, 0), (8, 12), ], vec![(0, 12), (8, 0)]]); + letters.insert('y', vec![vec![(0, 12), (4, 6), (8, 12), ], vec![(4, 6), (4, 0)]]); + letters.insert('z', vec![vec![(0, 12), (8, 12), (0, 0), (8, 0), ], vec![(2, 6), (6, 6)]]); + AsteroidFont {letters } + } + + pub fn get_letter(&self, letter: &char, size: f64) -> Vec> { + let letter_coordinates = self.letters.get(&letter.to_ascii_lowercase()).unwrap(); + let mut polygons: Vec> = Vec::new(); + for letter_part in letter_coordinates.iter() { + let coordinates: Vec> = letter_part + .iter() + .map(|&(x, y)| Coordinate { + x: x as f64 * size / 12., + y: (12. - y as f64) * size / 12., + }) + .collect(); + polygons.push(LineString(coordinates)); + } + polygons + } + + pub fn get_text( + &self, + text: String, + size: f64, + direction: FontDirection, + ) -> Vec> { + let mut letters: Vec> = Vec::new(); + let mut count = 0.; + for char in text.chars() { + let letter = self.get_letter(&char, size); + for character in letter.iter() { + match direction { + FontDirection::LeftRight => { + letters.push(character.translate(count * 10.0 * size / 12., 0.)) + } + FontDirection::TopDown => { + letters.push(character.translate(0., count * 14.0 * size / 12.)) + } + } + } + count = count + 1.; + } + letters + } +} + +pub enum FontDirection { + LeftRight, + TopDown, +} diff --git a/src/lib.rs b/src/lib.rs index 045802e..c1aecd4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,3 +7,7 @@ pub use crate::multipolygon_ext::MultiPolygonExt; mod svg; pub use crate::svg::load_multipolygon_from_svg; pub use crate::svg::load_multipolygon_from_svg_include; + +mod font; +pub use crate::font::AsteroidFont; +pub use crate::font::FontDirection; From e1694569582ca316804c9b214ded489d269a12b4 Mon Sep 17 00:00:00 2001 From: Ingolf Wagner Date: Mon, 12 Apr 2021 20:59:15 +0200 Subject: [PATCH 5/7] feat: add score board and life board --- examples/font.rs | 22 ++++-------------- src/bin/asteroids.rs | 55 +++++++++++++++++++++++++++++++++++++++----- src/context.rs | 8 +++++++ src/font.rs | 11 +++++++-- 4 files changed, 71 insertions(+), 25 deletions(-) diff --git a/examples/font.rs b/examples/font.rs index 0ddfad1..22b4cc5 100644 --- a/examples/font.rs +++ b/examples/font.rs @@ -7,41 +7,29 @@ use FontDirection::{LeftRight, TopDown}; fn main() { let context = Context::create(); context.save(); - context.translate(70., 50.); - // context.scale(8.); let font = AsteroidFont::new(); let line = font.get_text("Hey there!".to_string(), 12., LeftRight); - for char in line.iter() { - context.draw_line_string(char); - } + context.draw_line_strings(&line); context.draw_line_string(&line_string![(x : 0., y:14.), (x: 1000. , y : 14.)]); context.translate(0.0, 18.); let line = font.get_text("How are you?".to_string(), 22., LeftRight); - for char in line.iter() { - context.draw_line_string(char); - } + context.draw_line_strings(&line); context.translate(0.0, 28.); let line = font.get_text("Here a bitter text!".to_string(), 50., LeftRight); - for char in line.iter() { - context.draw_line_string(char); - } + context.draw_line_strings(&line); context.translate(0.0, 56.); let line = font.get_text("0123456789".to_string(), 50., LeftRight); - for char in line.iter() { - context.draw_line_string(char); - } + context.draw_line_strings(&line); context.restore(); context.translate(30., 50.); let line = font.get_text("0123456789".to_string(), 30., TopDown); - for char in line.iter() { - context.draw_line_string(char); - } + context.draw_line_strings(&line); context.render(); } diff --git a/src/bin/asteroids.rs b/src/bin/asteroids.rs index fc023f2..9f01cc2 100644 --- a/src/bin/asteroids.rs +++ b/src/bin/asteroids.rs @@ -3,10 +3,10 @@ use geo::algorithm::rotate::Rotate; use geo::prelude::Translate; use geo::rotate::RotatePoint; use geo::LineString; -use geo::{Coordinate, MultiPolygon, Point, Polygon}; +use geo::{polygon, Coordinate, MultiPolygon, Point, Polygon}; use geo_clipper::Clipper; -use polygon_art::MultiPolygonExt; use polygon_art::{load_multipolygon_from_svg_include, Context}; +use polygon_art::{AsteroidFont, FontDirection, MultiPolygonExt}; use rand::prelude::StdRng; use rand::Rng; @@ -65,9 +65,11 @@ fn main() { 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]) + let raw_ship = load_multipolygon_from_svg_include(ship_yard[chosen_ship]) .unwrap() - .rotate(90.) // rotate to scale ship with width + // rotate to scale ship with width + .rotate(90.); + let ship = raw_ship .scale_to_width(ship_length) .translate_center() .rotate(rotation as f64); @@ -76,9 +78,50 @@ fn main() { context.draw_multipolygon(&ship); context.restore(); - // drawing and clipping + // draw score board + let font = AsteroidFont::new(); + let random_seed_string = context.seed.to_string(); + let font_size = 20.; + let (x_font_size, y_font_size) = font.get_text_scale_factor(font_size); + let score_board_letters = random_seed_string.len(); let window = context.frame(frame_offset, frame_offset, frame_offset, frame_offset); - context.draw_polygon(&window); + let window_height = frame_offset + y_font_size; + let window_width = frame_offset + score_board_letters as f64 * x_font_size; + let score_board: Polygon = polygon![(x:0., y:0.), (x: window_width, y:0.), (x: window_width, y: window_height), (x: 0., y: window_height)]; + let score_board_lines = font.get_text(random_seed_string, font_size, FontDirection::LeftRight); + context.save(); + context.translate(frame_offset, frame_offset); + context.draw_line_strings(&score_board_lines); + context.restore(); + + // draw life board + let life = rng.gen_range(2..7) as f64; + let life_board: Polygon = polygon![ + (x:width, y:0.), + (x: width - frame_offset - life * x_font_size - 3., y:0.), + (x: width - frame_offset - life * x_font_size - 3., y:frame_offset + y_font_size + 3.), + (x: width , y:frame_offset + y_font_size + 3.) + ]; + // todo translate_top + // todo translate_left + let life_ship = raw_ship + .scale_to_width(font_size) + .rotate(-90.) + .translate_center_y(); + for l in 1..(1 + life as i32) { + context.save(); + context.translate( + width - frame_offset - (x_font_size * l as f64) as f64, + frame_offset + y_font_size / 2., + ); + context.draw_multipolygon(&life_ship); + context.restore(); + } + + // draw window + let window = window.difference(&score_board, 100.); + let window = window.difference(&life_board, 100.); + context.draw_multipolygon(&window); for asteroid in asteroids.iter() { context.draw_multipolygon(&asteroid.intersection(&window, 100.)); diff --git a/src/context.rs b/src/context.rs index 195dc0e..5b46bec 100644 --- a/src/context.rs +++ b/src/context.rs @@ -19,6 +19,7 @@ pub struct Context { pub width: f64, pub line_size: f64, pub pseudo_random_number_generator: Box, + pub seed: u64, } impl Context { @@ -64,6 +65,12 @@ impl Context { context.stroke(); } + pub fn draw_line_strings(&self, line_strings: &Vec>) { + for lines in line_strings { + self.draw_line_string(&lines); + } + } + /// restore state which was saved before by the context pub fn restore(&self) { self.render_container.context().restore(); @@ -171,6 +178,7 @@ impl Context { line_size, pseudo_random_number_generator: Box::new(rng), render_container, + seed: random_seed, } } } diff --git a/src/font.rs b/src/font.rs index 2b61642..34e5915 100644 --- a/src/font.rs +++ b/src/font.rs @@ -101,6 +101,12 @@ impl AsteroidFont { polygons } + /// size of space (per letter) + pub fn get_text_scale_factor(&self, size: f64) -> (f64, f64) { + let scale_factor = size / 12.; + (10.2 * scale_factor, 14.2 * scale_factor) + } + pub fn get_text( &self, text: String, @@ -111,13 +117,14 @@ impl AsteroidFont { let mut count = 0.; for char in text.chars() { let letter = self.get_letter(&char, size); + let (x_translate_factor, y_translate_factor) = self.get_text_scale_factor(size); for character in letter.iter() { match direction { FontDirection::LeftRight => { - letters.push(character.translate(count * 10.0 * size / 12., 0.)) + letters.push(character.translate(count * x_translate_factor, 0.)) } FontDirection::TopDown => { - letters.push(character.translate(0., count * 14.0 * size / 12.)) + letters.push(character.translate(0., count * y_translate_factor)) } } } From 1574ca725fa86110352a92571eebddd985a4be12 Mon Sep 17 00:00:00 2001 From: Ingolf Wagner Date: Tue, 13 Apr 2021 07:47:29 +0200 Subject: [PATCH 6/7] feat: create translate_ext.rs --- examples/font.rs | 10 +-- examples/svg.rs | 1 + src/bin/asteroids.rs | 12 ++-- src/context.rs | 8 ++- src/extensions.rs | 2 + src/{ => extensions}/multipolygon_ext.rs | 28 -------- src/extensions/translate_ext.rs | 87 ++++++++++++++++++++++++ src/font.rs | 6 +- src/lib.rs | 5 +- src/svg.rs | 6 +- 10 files changed, 117 insertions(+), 48 deletions(-) create mode 100644 src/extensions.rs rename src/{ => extensions}/multipolygon_ext.rs (66%) create mode 100644 src/extensions/translate_ext.rs diff --git a/examples/font.rs b/examples/font.rs index 22b4cc5..6475bdf 100644 --- a/examples/font.rs +++ b/examples/font.rs @@ -11,25 +11,25 @@ fn main() { let font = AsteroidFont::new(); let line = font.get_text("Hey there!".to_string(), 12., LeftRight); - context.draw_line_strings(&line); + context.draw_multiline_string(&line); context.draw_line_string(&line_string![(x : 0., y:14.), (x: 1000. , y : 14.)]); context.translate(0.0, 18.); let line = font.get_text("How are you?".to_string(), 22., LeftRight); - context.draw_line_strings(&line); + context.draw_multiline_string(&line); context.translate(0.0, 28.); let line = font.get_text("Here a bitter text!".to_string(), 50., LeftRight); - context.draw_line_strings(&line); + context.draw_multiline_string(&line); context.translate(0.0, 56.); let line = font.get_text("0123456789".to_string(), 50., LeftRight); - context.draw_line_strings(&line); + context.draw_multiline_string(&line); context.restore(); context.translate(30., 50.); let line = font.get_text("0123456789".to_string(), 30., TopDown); - context.draw_line_strings(&line); + context.draw_multiline_string(&line); context.render(); } diff --git a/examples/svg.rs b/examples/svg.rs index 5543e4d..8b0eef9 100644 --- a/examples/svg.rs +++ b/examples/svg.rs @@ -6,6 +6,7 @@ use geo::{Coordinate, Point}; use polygon_art::load_multipolygon_from_svg; use polygon_art::Context; use polygon_art::MultiPolygonExt; +use polygon_art::TranslateExt; use rand::Rng; fn main() { diff --git a/src/bin/asteroids.rs b/src/bin/asteroids.rs index 9f01cc2..a302754 100644 --- a/src/bin/asteroids.rs +++ b/src/bin/asteroids.rs @@ -6,7 +6,7 @@ use geo::LineString; use geo::{polygon, Coordinate, MultiPolygon, Point, Polygon}; use geo_clipper::Clipper; use polygon_art::{load_multipolygon_from_svg_include, Context}; -use polygon_art::{AsteroidFont, FontDirection, MultiPolygonExt}; +use polygon_art::{AsteroidFont, FontDirection, MultiPolygonExt, TranslateExt}; use rand::prelude::StdRng; use rand::Rng; @@ -91,7 +91,7 @@ fn main() { let score_board_lines = font.get_text(random_seed_string, font_size, FontDirection::LeftRight); context.save(); context.translate(frame_offset, frame_offset); - context.draw_line_strings(&score_board_lines); + context.draw_multiline_string(&score_board_lines); context.restore(); // draw life board @@ -102,17 +102,17 @@ fn main() { (x: width - frame_offset - life * x_font_size - 3., y:frame_offset + y_font_size + 3.), (x: width , y:frame_offset + y_font_size + 3.) ]; - // todo translate_top - // todo translate_left + let life_ship = raw_ship .scale_to_width(font_size) .rotate(-90.) - .translate_center_y(); + .translate_center_y() + .translate_top(); for l in 1..(1 + life as i32) { context.save(); context.translate( width - frame_offset - (x_font_size * l as f64) as f64, - frame_offset + y_font_size / 2., + frame_offset, ); context.draw_multipolygon(&life_ship); context.restore(); diff --git a/src/context.rs b/src/context.rs index 5b46bec..cdc64eb 100644 --- a/src/context.rs +++ b/src/context.rs @@ -1,6 +1,6 @@ use cairo::Format; use cairo::{ImageSurface, SvgSurface}; -use geo::{polygon, LineString, MultiPolygon, Polygon}; +use geo::{polygon, LineString, MultiLineString, MultiPolygon, Polygon}; use rand::rngs::StdRng; use rand::{Rng, SeedableRng}; @@ -71,6 +71,12 @@ impl Context { } } + pub fn draw_multiline_string(&self, multilinestring: &MultiLineString) { + for line in multilinestring.iter() { + self.draw_line_string(line); + } + } + /// restore state which was saved before by the context pub fn restore(&self) { self.render_container.context().restore(); diff --git a/src/extensions.rs b/src/extensions.rs new file mode 100644 index 0000000..76f3e22 --- /dev/null +++ b/src/extensions.rs @@ -0,0 +1,2 @@ +pub(crate) mod multipolygon_ext; +pub(crate) mod translate_ext; diff --git a/src/multipolygon_ext.rs b/src/extensions/multipolygon_ext.rs similarity index 66% rename from src/multipolygon_ext.rs rename to src/extensions/multipolygon_ext.rs index f31e86f..8b79290 100644 --- a/src/multipolygon_ext.rs +++ b/src/extensions/multipolygon_ext.rs @@ -4,41 +4,13 @@ use geo::Polygon; use geo_types::{Coordinate, LineString, MultiPolygon}; pub trait MultiPolygonExt { - fn translate_center(&self) -> Self; - fn translate_center_x(&self) -> Self; - fn translate_center_y(&self) -> Self; fn scale_to_width(&self, size: f64) -> Self; fn scale(&self, factor: f64) -> Self; } impl MultiPolygonExt for MultiPolygon { - fn translate_center(&self) -> Self { - let option = self.bounding_rect().unwrap(); - let y_diff = option.max().y - option.min().y; - let y_shift = y_diff / 2.0; - let x_diff = option.max().x - option.min().x; - let x_shift = x_diff / 2.0; - self.translate(-(option.min().x + x_shift), -(option.min().y + y_shift)) - } - - fn translate_center_x(&self) -> Self { - let option = self.bounding_rect().unwrap(); - let x_diff = option.max().x - option.min().x; - let x_shift = x_diff / 2.0; - self.translate(-(option.min().x + x_shift), 0.0) - } - - fn translate_center_y(&self) -> Self { - let option = self.bounding_rect().unwrap(); - let y_diff = option.max().y - option.min().y; - let y_shift = y_diff / 2.0; - self.translate(0.0, -(option.min().y + y_shift)) - } - fn scale_to_width(&self, size: f64) -> Self { - // let ((x_min, y_min), (x_max, _)) = self.get_min_max_rectangle(); let option = self.bounding_rect().unwrap(); - let diff = f64::abs(option.min().x - option.max().x); let scale = size / diff; self.scale(scale) diff --git a/src/extensions/translate_ext.rs b/src/extensions/translate_ext.rs new file mode 100644 index 0000000..1dee99f --- /dev/null +++ b/src/extensions/translate_ext.rs @@ -0,0 +1,87 @@ +use geo::algorithm::bounding_rect::BoundingRect; +use geo::algorithm::translate::Translate; +use geo::{CoordinateType, LineString, MultiLineString, MultiPolygon, Polygon, Rect}; + +/// Translate Extensions +pub trait TranslateExt { + /// translate the object to it's center + fn translate_center(&self) -> Self; + /// translate the object to it's center on the x aches + fn translate_center_x(&self) -> Self; + /// translate the object to it's center on the y aches + fn translate_center_y(&self) -> Self; + /// translate the object, so it touches the y aches + fn translate_left(&self) -> Self; + /// translate the object, so it touches the x aches + fn translate_top(&self) -> Self; +} + +/// wrapper for BoundingRect because of the Output type +pub trait ExplicitBoundingRect +where + T: CoordinateType, +{ + fn bounding(&self) -> Option>; +} + +impl ExplicitBoundingRect for Polygon { + fn bounding(&self) -> Option> { + self.bounding_rect() + } +} + +impl ExplicitBoundingRect for MultiPolygon { + fn bounding(&self) -> Option> { + self.bounding_rect() + } +} + +impl ExplicitBoundingRect for LineString { + fn bounding(&self) -> Option> { + self.bounding_rect() + } +} + +impl ExplicitBoundingRect for MultiLineString { + fn bounding(&self) -> Option> { + self.bounding_rect() + } +} + +impl TranslateExt for B +where + B: ExplicitBoundingRect + Translate, +{ + fn translate_center(&self) -> Self { + let option = self.bounding().unwrap(); + let y_diff = option.max().y - option.min().y; + let y_shift = y_diff / 2.0; + let x_diff = option.max().x - option.min().x; + let x_shift = x_diff / 2.0; + self.translate(-(option.min().x + x_shift), -(option.min().y + y_shift)) + } + + fn translate_center_x(&self) -> Self { + let option = self.bounding().unwrap(); + let x_diff = option.max().x - option.min().x; + let x_shift = x_diff / 2.0; + self.translate(-(option.min().x + x_shift), 0.0) + } + + fn translate_center_y(&self) -> Self { + let option = self.bounding().unwrap(); + let y_diff = option.max().y - option.min().y; + let y_shift = y_diff / 2.0; + self.translate(0.0, -(option.min().y + y_shift)) + } + + fn translate_left(&self) -> Self { + let bounding = self.bounding().unwrap(); + self.translate(-bounding.min().x, 0.) + } + + fn translate_top(&self) -> Self { + let bounding = self.bounding().unwrap(); + self.translate(0., -bounding.min().y) + } +} diff --git a/src/font.rs b/src/font.rs index 34e5915..bbc7aa8 100644 --- a/src/font.rs +++ b/src/font.rs @@ -2,7 +2,7 @@ // https://github.com/osresearch/vst/blob/master/teensyv/asteroids_font.c use geo::algorithm::translate::Translate; -use geo::{Coordinate, LineString}; +use geo::{Coordinate, LineString, MultiLineString}; use std::collections::HashMap; pub struct AsteroidFont { @@ -112,7 +112,7 @@ impl AsteroidFont { text: String, size: f64, direction: FontDirection, - ) -> Vec> { + ) -> MultiLineString { let mut letters: Vec> = Vec::new(); let mut count = 0.; for char in text.chars() { @@ -130,7 +130,7 @@ impl AsteroidFont { } count = count + 1.; } - letters + MultiLineString(letters) } } diff --git a/src/lib.rs b/src/lib.rs index c1aecd4..18a3ad2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,8 +1,9 @@ mod context; pub use crate::context::Context; -mod multipolygon_ext; -pub use crate::multipolygon_ext::MultiPolygonExt; +mod extensions; +pub use crate::extensions::multipolygon_ext::MultiPolygonExt; +pub use crate::extensions::translate_ext::TranslateExt; mod svg; pub use crate::svg::load_multipolygon_from_svg; diff --git a/src/svg.rs b/src/svg.rs index 287bfae..d3b3158 100644 --- a/src/svg.rs +++ b/src/svg.rs @@ -9,7 +9,7 @@ use svg::Parser; 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) + parse_svg_to_multipolygon(parser) } /// load a multipolygon statically at compiletime @@ -18,11 +18,11 @@ pub fn load_multipolygon_from_svg_include( content: &str, ) -> Result, Box> { let parser = svg::read(content)?; - parse_svg(parser) + parse_svg_to_multipolygon(parser) } #[allow(non_upper_case_globals)] -fn parse_svg(parser: Parser) -> Result, Box> { +fn parse_svg_to_multipolygon(parser: Parser) -> Result, Box> { let mut result: Vec> = Vec::new(); for event in parser { match event { From f008f92bfca73e68d20337e8d6b281d5bf82fadd Mon Sep 17 00:00:00 2001 From: Ingolf Wagner Date: Tue, 13 Apr 2021 20:24:04 +0200 Subject: [PATCH 7/7] feat: add merge_ext.rs and scale_ext.rs --- examples/svg.rs | 3 +- src/bin/asteroids.rs | 30 ++++++------- src/extensions.rs | 3 +- src/extensions/merge_ext.rs | 35 +++++++++++++++ src/extensions/multipolygon_ext.rs | 51 ---------------------- src/extensions/scale_ext.rs | 68 ++++++++++++++++++++++++++++++ src/extensions/translate_ext.rs | 28 ++++++------ src/lib.rs | 3 +- 8 files changed, 134 insertions(+), 87 deletions(-) create mode 100644 src/extensions/merge_ext.rs delete mode 100644 src/extensions/multipolygon_ext.rs create mode 100644 src/extensions/scale_ext.rs diff --git a/examples/svg.rs b/examples/svg.rs index 8b0eef9..07f1f71 100644 --- a/examples/svg.rs +++ b/examples/svg.rs @@ -3,10 +3,9 @@ use geo::rotate::RotatePoint; use geo::translate::Translate; use geo::{Coordinate, Point}; -use polygon_art::load_multipolygon_from_svg; use polygon_art::Context; -use polygon_art::MultiPolygonExt; use polygon_art::TranslateExt; +use polygon_art::{load_multipolygon_from_svg, ScaleExt}; use rand::Rng; fn main() { diff --git a/src/bin/asteroids.rs b/src/bin/asteroids.rs index a302754..ff754e1 100644 --- a/src/bin/asteroids.rs +++ b/src/bin/asteroids.rs @@ -5,8 +5,8 @@ use geo::rotate::RotatePoint; use geo::LineString; use geo::{polygon, Coordinate, MultiPolygon, Point, Polygon}; use geo_clipper::Clipper; -use polygon_art::{load_multipolygon_from_svg_include, Context}; -use polygon_art::{AsteroidFont, FontDirection, MultiPolygonExt, TranslateExt}; +use polygon_art::{load_multipolygon_from_svg_include, Context, MergeExt, ScaleExt}; +use polygon_art::{AsteroidFont, FontDirection, TranslateExt}; use rand::prelude::StdRng; use rand::Rng; @@ -19,12 +19,10 @@ fn main() { let height = context.height; let diagonal = f64::sqrt(width * width + height * height); - // todo : it does not make to much sense to define all parameters like this - // todo : it makes more sense to create a static (diagonal / width /height ratio ) setup and scale it up let frame_offset = diagonal * 0.01; - let max_asteroid = (diagonal / 40.) as usize; + let max_asteroid = (diagonal / 80.) as usize; - let min_asteroid_size = diagonal / 40.; + let min_asteroid_size = diagonal / 50.; let max_asteroid_size = diagonal / 12.; let ship_length = diagonal / 23.; let ship_orbit = 3.0; @@ -34,7 +32,7 @@ fn main() { .translate(width / 2., height / 2.); // create asteroid - let mut asteroids: Vec> = Vec::new(); + let mut asteroids: MultiPolygon = MultiPolygon(Vec::new()); for _ in 0..(max_asteroid * 2) { let without_orbit = rng.gen_range(min_asteroid_size..max_asteroid_size) as f64; let with_orbit = without_orbit * 1.32; @@ -44,15 +42,11 @@ fn main() { .scale_to_width(with_orbit) .translate(x, y); - if (!center_point.intersects(&asteroid)) - && asteroids - .iter() - .find(|&m| *&m.intersects(&asteroid)) - .is_none() - { - asteroids.push(asteroid.scale_to_width(without_orbit)); + if (!center_point.intersects(&asteroid)) && !asteroids.intersects(&asteroid) { + // asteroids.push(asteroid.scale_to_width(without_orbit)); + asteroids.merge_destructive(&mut asteroid.scale_to_width(without_orbit)) } - if asteroids.len() > max_asteroid { + if asteroids.0.len() > max_asteroid { break; } } @@ -123,10 +117,10 @@ fn main() { let window = window.difference(&life_board, 100.); context.draw_multipolygon(&window); - for asteroid in asteroids.iter() { - context.draw_multipolygon(&asteroid.intersection(&window, 100.)); - } + // draw asteroids + context.draw_multipolygon(&asteroids.intersection(&window, 100.)); + // render image context.render(); } diff --git a/src/extensions.rs b/src/extensions.rs index 76f3e22..f2166c7 100644 --- a/src/extensions.rs +++ b/src/extensions.rs @@ -1,2 +1,3 @@ -pub(crate) mod multipolygon_ext; +pub(crate) mod merge_ext; +pub(crate) mod scale_ext; pub(crate) mod translate_ext; diff --git a/src/extensions/merge_ext.rs b/src/extensions/merge_ext.rs new file mode 100644 index 0000000..8aab680 --- /dev/null +++ b/src/extensions/merge_ext.rs @@ -0,0 +1,35 @@ +use geo::{LineString, MultiLineString, MultiPolygon}; + +pub trait MergeExt { + /// merge Container object + fn merge(&mut self, other: &Self); + /// merge container object, empty the other object + fn merge_destructive(&mut self, other: &mut Self); +} + +impl MergeExt for LineString { + fn merge(&mut self, other: &Self) { + self.0.append(&mut other.0.clone()); + } + fn merge_destructive(&mut self, other: &mut Self) { + self.0.append(&mut other.0); + } +} + +impl MergeExt for MultiLineString { + fn merge(&mut self, other: &Self) { + self.0.append(&mut other.0.clone()); + } + fn merge_destructive(&mut self, other: &mut Self) { + self.0.append(&mut other.0); + } +} + +impl MergeExt for MultiPolygon { + fn merge(&mut self, other: &Self) { + self.0.append(&mut other.0.clone()); + } + fn merge_destructive(&mut self, other: &mut Self) { + self.0.append(&mut other.0); + } +} diff --git a/src/extensions/multipolygon_ext.rs b/src/extensions/multipolygon_ext.rs deleted file mode 100644 index 8b79290..0000000 --- a/src/extensions/multipolygon_ext.rs +++ /dev/null @@ -1,51 +0,0 @@ -use geo::algorithm::bounding_rect::BoundingRect; -use geo::algorithm::translate::Translate; -use geo::Polygon; -use geo_types::{Coordinate, LineString, MultiPolygon}; - -pub trait MultiPolygonExt { - fn scale_to_width(&self, size: f64) -> Self; - fn scale(&self, factor: f64) -> Self; -} - -impl MultiPolygonExt for MultiPolygon { - fn scale_to_width(&self, size: f64) -> Self { - let option = self.bounding_rect().unwrap(); - let diff = f64::abs(option.min().x - option.max().x); - let scale = size / diff; - self.scale(scale) - } - - fn scale(&self, factor: f64) -> Self { - let bounding_rectangle = self.bounding_rect().unwrap(); - let zeroed_multi_polygons = - self.translate(-bounding_rectangle.min().x, -bounding_rectangle.min().y); - let mut new_polygons: Vec> = Vec::new(); - for polygon in zeroed_multi_polygons.iter() { - // map interiors - let mut interiors: Vec> = Vec::new(); - for interior_line_string in polygon.interiors().to_vec() { - let mut new_line_string: Vec> = Vec::new(); - for point in interior_line_string.into_points() { - new_line_string.push(Coordinate { - x: point.x() * factor, - y: point.y() * factor, - }); - } - interiors.push(LineString::from(new_line_string)); - } - // map exterior - let mut new_line_string: Vec> = Vec::new(); - for point in polygon.exterior().clone().into_points() { - new_line_string.push(Coordinate { - x: point.x() * factor, - y: point.y() * factor, - }); - } - let exterior = LineString::from(new_line_string); - new_polygons.push(Polygon::new(exterior, interiors)); - } - MultiPolygon::from(new_polygons) - .translate(bounding_rectangle.min().x, bounding_rectangle.min().y) - } -} diff --git a/src/extensions/scale_ext.rs b/src/extensions/scale_ext.rs new file mode 100644 index 0000000..dfd159f --- /dev/null +++ b/src/extensions/scale_ext.rs @@ -0,0 +1,68 @@ +use crate::extensions::translate_ext::ExplicitBoundingRect; +use geo::algorithm::map_coords::MapCoords; +use geo::algorithm::translate::Translate; +use geo::{CoordinateType, LineString, MultiLineString, MultiPolygon, Polygon}; + +pub trait ExplicitMapCoords { + fn map_coordinates(&self, func: impl Fn(&(T, T)) -> (T, T) + Copy) -> Self; +} + +impl ExplicitMapCoords for LineString { + fn map_coordinates(&self, func: impl Fn(&(T, T)) -> (T, T) + Copy) -> Self { + self.map_coords(func) + } +} + +impl ExplicitMapCoords for MultiLineString { + fn map_coordinates(&self, func: impl Fn(&(T, T)) -> (T, T) + Copy) -> Self { + self.map_coords(func) + } +} + +impl ExplicitMapCoords for MultiPolygon { + fn map_coordinates(&self, func: impl Fn(&(T, T)) -> (T, T) + Copy) -> Self { + self.map_coords(func) + } +} + +impl ExplicitMapCoords for Polygon { + fn map_coordinates(&self, func: impl Fn(&(T, T)) -> (T, T) + Copy) -> Self { + self.map_coords(func) + } +} + +pub trait ScaleExt { + /// scale object + fn scale(&self, scale_x: f64, scale_y: f64) -> Self; + /// scale object to given width (in 1:1 ratio) + fn scale_to_width(&self, size: f64) -> Self; + /// scale object to given height (in 1:1 ratio) + fn scale_to_height(&self, size: f64) -> Self; +} + +impl ScaleExt for V +where + V: ExplicitBoundingRect + ExplicitMapCoords + Translate, +{ + fn scale(&self, scale_x: f64, scale_y: f64) -> Self { + let bounding_rectangle = self.bounding().unwrap(); + let minimum_coordinates = bounding_rectangle.min(); + self.translate(-minimum_coordinates.x, -minimum_coordinates.y) + .map_coordinates(|&(x, y)| (x * scale_x, y * scale_y)) + .translate(minimum_coordinates.x, minimum_coordinates.y) + } + + fn scale_to_width(&self, size: f64) -> Self { + let option = self.bounding().unwrap(); + let diff = f64::abs(option.min().x - option.max().x); + let scale = size / diff; + self.scale(scale, scale) + } + + fn scale_to_height(&self, size: f64) -> Self { + let option = self.bounding().unwrap(); + let diff = f64::abs(option.min().y - option.max().y); + let scale = size / diff; + self.scale(scale, scale) + } +} diff --git a/src/extensions/translate_ext.rs b/src/extensions/translate_ext.rs index 1dee99f..8d43461 100644 --- a/src/extensions/translate_ext.rs +++ b/src/extensions/translate_ext.rs @@ -2,20 +2,6 @@ use geo::algorithm::bounding_rect::BoundingRect; use geo::algorithm::translate::Translate; use geo::{CoordinateType, LineString, MultiLineString, MultiPolygon, Polygon, Rect}; -/// Translate Extensions -pub trait TranslateExt { - /// translate the object to it's center - fn translate_center(&self) -> Self; - /// translate the object to it's center on the x aches - fn translate_center_x(&self) -> Self; - /// translate the object to it's center on the y aches - fn translate_center_y(&self) -> Self; - /// translate the object, so it touches the y aches - fn translate_left(&self) -> Self; - /// translate the object, so it touches the x aches - fn translate_top(&self) -> Self; -} - /// wrapper for BoundingRect because of the Output type pub trait ExplicitBoundingRect where @@ -48,6 +34,20 @@ impl ExplicitBoundingRect for MultiLineString { } } +/// Translate Extensions +pub trait TranslateExt { + /// translate the object to it's center + fn translate_center(&self) -> Self; + /// translate the object to it's center on the x aches + fn translate_center_x(&self) -> Self; + /// translate the object to it's center on the y aches + fn translate_center_y(&self) -> Self; + /// translate the object, so it touches the y aches + fn translate_left(&self) -> Self; + /// translate the object, so it touches the x aches + fn translate_top(&self) -> Self; +} + impl TranslateExt for B where B: ExplicitBoundingRect + Translate, diff --git a/src/lib.rs b/src/lib.rs index 18a3ad2..6923347 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,7 +2,8 @@ mod context; pub use crate::context::Context; mod extensions; -pub use crate::extensions::multipolygon_ext::MultiPolygonExt; +pub use crate::extensions::merge_ext::MergeExt; +pub use crate::extensions::scale_ext::ScaleExt; pub use crate::extensions::translate_ext::TranslateExt; mod svg;