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.
This commit is contained in:
Michael Kirk 2021-02-01 18:24:56 -06:00 committed by GitHub
parent 8a856d6608
commit 38dc60fbea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 13 deletions

View File

@ -191,9 +191,10 @@ pub fn trips(
}, },
{ {
// TODO Maybe generalize ImageSource::Bytes beyond just buttons // TODO Maybe generalize ImageSource::Bytes beyond just buttons
let mut icon = GeomBatch::from_uncached_svg_contents(include_bytes!( let mut icon = GeomBatch::load_svg_bytes(
"../../../widgetry/icons/arrow_drop_down.svg" &ctx.prerender,
)) widgetry::include_labeled_bytes!("../../../widgetry/icons/arrow_drop_down.svg"),
)
.autocrop() .autocrop()
.color(RewriteColor::ChangeAll(Color::WHITE)) .color(RewriteColor::ChangeAll(Color::WHITE))
.scale(1.5); .scale(1.5);
@ -276,7 +277,7 @@ pub fn bio(
let mut svg_data = Vec::new(); let mut svg_data = Vec::new();
svg_face::generate_face(&mut svg_data, &mut rng).unwrap(); 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 dims = batch.get_dims();
let batch = batch.scale((200.0 / dims.width).min(200.0 / dims.height)); let batch = batch.scale((200.0 / dims.width).min(200.0 / dims.height));
rows.push(Widget::draw_batch(ctx, batch).centered_horiz()); rows.push(Widget::draw_batch(ctx, batch).centered_horiz());

View File

@ -77,9 +77,12 @@ impl Picker {
Widget::row(vec![ Widget::row(vec![
Widget::draw_batch( Widget::draw_batch(
ctx, ctx,
GeomBatch::from_uncached_svg_contents(include_bytes!( GeomBatch::load_svg_bytes(
"../../widgetry/icons/arrow_keys.svg" &ctx.prerender,
)), widgetry::include_labeled_bytes!(
"../../widgetry/icons/arrow_keys.svg"
),
),
), ),
Text::from_all(vec![ Text::from_all(vec![
Line("arrow keys").fg(ctx.style().hotkey_color), Line("arrow keys").fg(ctx.style().hotkey_color),

View File

@ -135,16 +135,30 @@ impl GeomBatch {
ScreenDims::new(bounds.width(), bounds.height()) 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. /// Returns a batch containing an SVG from a file.
pub fn load_svg<P: AsRef<Prerender>>(prerender: &P, filename: &str) -> GeomBatch { pub fn load_svg<P: AsRef<Prerender>>(prerender: &P, filename: &str) -> GeomBatch {
svg::load_svg(prerender.as_ref(), filename).0 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<P: AsRef<Prerender>>(
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. /// Transforms all colors in a batch.
pub fn color(mut self, transformation: RewriteColor) -> GeomBatch { pub fn color(mut self, transformation: RewriteColor) -> GeomBatch {
for (fancy, _, _) in &mut self.list { for (fancy, _, _) in &mut self.list {

View File

@ -289,7 +289,7 @@ fn setup_scrollable_canvas(ctx: &mut EventCtx) -> Drawable {
for i in 0..10 { for i in 0..10 {
let mut svg_data = Vec::new(); let mut svg_data = Vec::new();
svg_face::generate_face(&mut svg_data, &mut rng).unwrap(); 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(); let dims = face.get_dims();
batch.append( batch.append(
face.scale((200.0 / dims.width).min(200.0 / dims.height)) face.scale((200.0 / dims.width).min(200.0 / dims.height))