From e6c5ec3d4225a23b0fca6ce07de2f0245cf3fd95 Mon Sep 17 00:00:00 2001 From: meehl <12648828+meehl@users.noreply.github.com> Date: Wed, 26 Aug 2026 11:18:16 +0200 Subject: [PATCH] Move font style cache into separate file --- src/fonts.rs | 1 + src/fonts/glyph_key.rs | 2 +- src/fonts/rasterizer.rs | 69 +++------------------------------------- src/fonts/style_cache.rs | 60 ++++++++++++++++++++++++++++++++++ 4 files changed, 67 insertions(+), 65 deletions(-) create mode 100644 src/fonts/style_cache.rs diff --git a/src/fonts.rs b/src/fonts.rs index f8cc2d2..1104ca0 100644 --- a/src/fonts.rs +++ b/src/fonts.rs @@ -23,6 +23,7 @@ mod glyph_key; mod layout; mod layout_cache; mod rasterizer; +mod style_cache; /// Data of a .ttf or .otf file #[derive(Clone, Debug)] diff --git a/src/fonts/glyph_key.rs b/src/fonts/glyph_key.rs index 353d25d..fcdb94b 100644 --- a/src/fonts/glyph_key.rs +++ b/src/fonts/glyph_key.rs @@ -1,7 +1,7 @@ use ordered_float::OrderedFloat; use parley::Glyph; -use crate::{dpi::PhysicalPoint, fonts::rasterizer::StyleId}; +use crate::{dpi::PhysicalPoint, fonts::style_cache::StyleId}; #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct GlyphKey { diff --git a/src/fonts/rasterizer.rs b/src/fonts/rasterizer.rs index fea7adc..3599e9f 100644 --- a/src/fonts/rasterizer.rs +++ b/src/fonts/rasterizer.rs @@ -1,52 +1,19 @@ use crate::{ color::Srgba, dpi::{LogicalVector, PhysicalPoint, PhysicalRect, PhysicalSize, PhysicalVector, ScaleFactor}, - fonts::{atlas::FontAtlas, glyph_key::GlyphKey, layout::LayoutRect}, + fonts::{atlas::FontAtlas, glyph_key::GlyphKey, layout::LayoutRect, style_cache::StyleCache}, math::Size, uv::UvRect, }; use ahash::HashMap; use image::GenericImage; -use ordered_float::OrderedFloat; use parley::{FontData, GlyphRun}; -use std::borrow::Cow; use swash::zeno; type FontBlobId = u64; type FontIndex = u32; type SwashFontOffset = u32; -pub type StyleId = u32; -#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] -struct StyleKey<'a> { - font_blob_id: FontBlobId, - font_size: OrderedFloat, - normalized_coords: Cow<'a, [i16]>, - skew: i8, -} - -impl<'a> StyleKey<'a> { - fn new( - font_blob_id: FontBlobId, - font_size: f32, - normalized_coords: &'a [i16], - skew: i8, - ) -> Self { - Self { - font_blob_id, - font_size: font_size.into(), - normalized_coords: Cow::Borrowed(normalized_coords), - skew, - } - } - - fn to_static(&self) -> StyleKey<'static> { - StyleKey { - normalized_coords: self.normalized_coords.clone().into_owned().into(), - ..*self - } - } -} #[derive(Clone, Copy, Debug, PartialEq)] pub struct CachedGlyph { pub size: PhysicalSize, @@ -87,11 +54,7 @@ impl RasterizedGlyph { pub struct GlyphRasterizer { scale_context: swash::scale::ScaleContext, swash_keys: HashMap<(FontBlobId, FontIndex), (SwashFontOffset, swash::CacheKey)>, - // Style properties (font, size, etc.) are the same for each glyph run and don't - // need to be part of each glyph key. Instead, associate each style with its own - // ID and include that in the glyph key. - style_ids: HashMap, StyleId>, - next_style_id: StyleId, + style_cache: StyleCache, cached_glyphs: HashMap>, // scratch image buffer used to write bitmap data into scratch: swash::scale::image::Image, @@ -102,8 +65,7 @@ impl GlyphRasterizer { Self { scale_context: swash::scale::ScaleContext::new(), swash_keys: Default::default(), - style_ids: Default::default(), - next_style_id: 0, + style_cache: Default::default(), cached_glyphs: Default::default(), scratch: Default::default(), } @@ -129,27 +91,6 @@ impl GlyphRasterizer { } } - fn get_style_id( - &mut self, - font_data: &FontData, - font_size: f32, - norm_coords: &[i16], - skew: i8, - ) -> StyleId { - let style_key = StyleKey::new(font_data.data.id(), font_size, norm_coords, skew); - match self.style_ids.get(&style_key) { - Some(key) => *key, - None => *self - .style_ids - .entry(style_key.to_static()) - .or_insert_with(|| { - let id = self.next_style_id; - self.next_style_id += 1; - id - }), - } - } - /// Rasterizes glyph run and returns the placement and UV for each glyph. /// Can return `None` if glyph doesn't take up any space (e.g. whitespace). pub fn rasterize_glyph_run<'slf: 'run, 'run, 'atlas>( @@ -167,8 +108,8 @@ impl GlyphRasterizer { let skew = run.synthesis().skew(); // skew angle for faux italic let font_ref = self.get_font_ref(run.font()); - let style_id = self.get_style_id( - run.font(), + let style_id = self.style_cache.get_or_insert( + run.font().data.id(), font_size, normalized_coords, // parley stores skew as i8 internally so this conversion is ok diff --git a/src/fonts/style_cache.rs b/src/fonts/style_cache.rs new file mode 100644 index 0000000..1a464de --- /dev/null +++ b/src/fonts/style_cache.rs @@ -0,0 +1,60 @@ +use ahash::HashMap; +use ordered_float::OrderedFloat; +use std::borrow::Cow; + +pub type StyleId = u32; + +#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] +struct StyleKey<'a> { + font_blob_id: u64, + font_size: OrderedFloat, + normalized_coords: Cow<'a, [i16]>, + skew: i8, +} + +impl<'a> StyleKey<'a> { + fn new(font_blob_id: u64, font_size: f32, normalized_coords: &'a [i16], skew: i8) -> Self { + Self { + font_blob_id, + font_size: font_size.into(), + normalized_coords: Cow::Borrowed(normalized_coords), + skew, + } + } + + fn to_static(&self) -> StyleKey<'static> { + StyleKey { + normalized_coords: self.normalized_coords.clone().into_owned().into(), + ..*self + } + } +} + +/// Associates font style combinations (font, size, norm coords, skew) with a small `StyleId`, so +/// `GlyphKey` never has to compare full style data per glyph, just once per run. +#[derive(Default)] +pub struct StyleCache { + ids: HashMap, StyleId>, + next_id: StyleId, +} + +impl StyleCache { + pub fn get_or_insert( + &mut self, + font_blob_id: u64, + font_size: f32, + normalized_coords: &[i16], + skew: i8, + ) -> StyleId { + let key = StyleKey::new(font_blob_id, font_size, normalized_coords, skew); + + if let Some(id) = self.ids.get(&key) { + return *id; + } + + let id = self.next_id; + self.next_id += 1; + self.ids.insert(key.to_static(), id); + id + } +}