From f652dd1a99f7a953477a5221c2e84847269f7830 Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Tue, 25 Oct 2022 19:40:17 -0600 Subject: [PATCH] Add function for getting swash image without caching --- src/swash.rs | 81 +++++++++++++++++++++++++++++----------------------- 1 file changed, 45 insertions(+), 36 deletions(-) diff --git a/src/swash.rs b/src/swash.rs index 5afcb23..cb29859 100644 --- a/src/swash.rs +++ b/src/swash.rs @@ -9,6 +9,44 @@ use crate::{CacheKey, FontMatches}; pub use swash::scale::image::Image as SwashImage; +fn swash_image(context: &mut ScaleContext, matches: &FontMatches, cache_key: CacheKey) -> Option { + let font = match matches.get_font(&cache_key.font_id) { + Some(some) => some, + None => { + log::warn!("did not find font {:?}", cache_key.font_id); + return None; + }, + }; + + // Build the scaler + let mut scaler = context + .builder(font.as_swash()) + .size(cache_key.font_size as f32) + .hint(true) + .build(); + + // Compute the fractional offset-- you'll likely want to quantize this + // in a real renderer + let offset = + Vector::new(cache_key.x_bin.as_float(), cache_key.y_bin.as_float()); + + // Select our source order + Render::new(&[ + // Color outline with the first palette + Source::ColorOutline(0), + // Color bitmap with best fit selection mode + Source::ColorBitmap(StrikeWith::BestFit), + // Standard scalable outline + Source::Outline, + ]) + // Select a subpixel format + .format(Format::Alpha) + // Apply the fractional offset + .offset(offset) + // Render the image + .render(&mut scaler, cache_key.glyph_id) +} + pub struct SwashCache { context: ScaleContext, pub image_cache: HashMap>, @@ -23,44 +61,15 @@ impl SwashCache { } } + /// Create a swash Image from a cache key, without caching results + pub fn get_image_uncached(&mut self, matches: &FontMatches, cache_key: CacheKey) -> Option { + swash_image(&mut self.context, matches, cache_key) + } + /// Create a swash Image from a cache key, caching results - pub fn get_image(&mut self, matches: &FontMatches<'_>, cache_key: CacheKey) -> &Option { + pub fn get_image(&mut self, matches: &FontMatches, cache_key: CacheKey) -> &Option { self.image_cache.entry(cache_key).or_insert_with(|| { - let font = match matches.get_font(&cache_key.font_id) { - Some(some) => some, - None => { - log::warn!("did not find font {:?}", cache_key.font_id); - return None; - }, - }; - - // Build the scaler - let mut scaler = self.context - .builder(font.as_swash()) - .size(cache_key.font_size as f32) - .hint(true) - .build(); - - // Compute the fractional offset-- you'll likely want to quantize this - // in a real renderer - let offset = - Vector::new(cache_key.x_bin.as_float(), cache_key.y_bin.as_float()); - - // Select our source order - Render::new(&[ - // Color outline with the first palette - Source::ColorOutline(0), - // Color bitmap with best fit selection mode - Source::ColorBitmap(StrikeWith::BestFit), - // Standard scalable outline - Source::Outline, - ]) - // Select a subpixel format - .format(Format::Alpha) - // Apply the fractional offset - .offset(offset) - // Render the image - .render(&mut scaler, cache_key.glyph_id) + swash_image(&mut self.context, matches, cache_key) }) }