From 38dc60fbeaab11ba47387f33fae01896f50bd3cc Mon Sep 17 00:00:00 2001 From: Michael Kirk Date: Mon, 1 Feb 2021 18:24:56 -0600 Subject: [PATCH] Add method to GeomBatch to prefer cached flavor of loading svg. (#493) It was my intention when introducing the cached vs uncached flavors that we'd prefer the cached flavor for anything referencing bytes from a file, like icons and UI glyphs, and that we'd only use the uncached flavor for dynamic things like "face" generation. --- game/src/info/person.rs | 9 +++++---- santa/src/before_level.rs | 9 ++++++--- widgetry/src/geom.rs | 24 +++++++++++++++++++----- widgetry_demo/src/lib.rs | 2 +- 4 files changed, 31 insertions(+), 13 deletions(-) diff --git a/game/src/info/person.rs b/game/src/info/person.rs index baaf23b6bb..a65f7f7b44 100644 --- a/game/src/info/person.rs +++ b/game/src/info/person.rs @@ -191,9 +191,10 @@ pub fn trips( }, { // TODO Maybe generalize ImageSource::Bytes beyond just buttons - let mut icon = GeomBatch::from_uncached_svg_contents(include_bytes!( - "../../../widgetry/icons/arrow_drop_down.svg" - )) + let mut icon = GeomBatch::load_svg_bytes( + &ctx.prerender, + widgetry::include_labeled_bytes!("../../../widgetry/icons/arrow_drop_down.svg"), + ) .autocrop() .color(RewriteColor::ChangeAll(Color::WHITE)) .scale(1.5); @@ -276,7 +277,7 @@ pub fn bio( let mut svg_data = Vec::new(); svg_face::generate_face(&mut svg_data, &mut rng).unwrap(); - let batch = GeomBatch::from_uncached_svg_contents(&svg_data).autocrop(); + let batch = GeomBatch::load_svg_bytes_uncached(&svg_data).autocrop(); let dims = batch.get_dims(); let batch = batch.scale((200.0 / dims.width).min(200.0 / dims.height)); rows.push(Widget::draw_batch(ctx, batch).centered_horiz()); diff --git a/santa/src/before_level.rs b/santa/src/before_level.rs index 455e935078..472b79bf5c 100644 --- a/santa/src/before_level.rs +++ b/santa/src/before_level.rs @@ -77,9 +77,12 @@ impl Picker { Widget::row(vec![ Widget::draw_batch( ctx, - GeomBatch::from_uncached_svg_contents(include_bytes!( - "../../widgetry/icons/arrow_keys.svg" - )), + GeomBatch::load_svg_bytes( + &ctx.prerender, + widgetry::include_labeled_bytes!( + "../../widgetry/icons/arrow_keys.svg" + ), + ), ), Text::from_all(vec![ Line("arrow keys").fg(ctx.style().hotkey_color), diff --git a/widgetry/src/geom.rs b/widgetry/src/geom.rs index 807f4de363..2a463c2041 100644 --- a/widgetry/src/geom.rs +++ b/widgetry/src/geom.rs @@ -135,16 +135,30 @@ impl GeomBatch { ScreenDims::new(bounds.width(), bounds.height()) } - /// Returns a batch containing a parsed SVG string. - pub fn from_uncached_svg_contents(raw: &[u8]) -> GeomBatch { - svg::load_svg_from_bytes_uncached(raw).unwrap().0 - } - /// Returns a batch containing an SVG from a file. pub fn load_svg>(prerender: &P, filename: &str) -> GeomBatch { svg::load_svg(prerender.as_ref(), filename).0 } + /// Returns a GeomBatch from the bytes of a utf8 encoded SVG string. + pub fn load_svg_bytes>( + prerender: &P, + labeled_bytes: (&str, &[u8]), + ) -> GeomBatch { + svg::load_svg_bytes(prerender.as_ref(), labeled_bytes.0, labeled_bytes.1) + .expect("invalid svg bytes") + .0 + } + + /// Returns a GeomBatch from the bytes of a utf8 encoded SVG string. + /// + /// Prefer to use `load_svg_bytes`, which caches the parsed SVG, unless + /// the SVG was dynamically generated, or is otherwise unlikely to be + /// reused. + pub fn load_svg_bytes_uncached(raw: &[u8]) -> GeomBatch { + svg::load_svg_from_bytes_uncached(raw).unwrap().0 + } + /// Transforms all colors in a batch. pub fn color(mut self, transformation: RewriteColor) -> GeomBatch { for (fancy, _, _) in &mut self.list { diff --git a/widgetry_demo/src/lib.rs b/widgetry_demo/src/lib.rs index 4f5c21850b..9638904d97 100644 --- a/widgetry_demo/src/lib.rs +++ b/widgetry_demo/src/lib.rs @@ -289,7 +289,7 @@ fn setup_scrollable_canvas(ctx: &mut EventCtx) -> Drawable { for i in 0..10 { let mut svg_data = Vec::new(); svg_face::generate_face(&mut svg_data, &mut rng).unwrap(); - let face = GeomBatch::from_uncached_svg_contents(&svg_data).autocrop(); + let face = GeomBatch::load_svg_bytes_uncached(&svg_data).autocrop(); let dims = face.get_dims(); batch.append( face.scale((200.0 / dims.width).min(200.0 / dims.height))