From f0be43cbdd2f4587f1b8d8e14f1dc9c190b29918 Mon Sep 17 00:00:00 2001 From: meehl <12648828+meehl@users.noreply.github.com> Date: Wed, 26 Aug 2026 15:51:42 +0200 Subject: [PATCH] Add helper for iterating glyph runs --- src/fonts.rs | 87 ++++++++++++++++++++++++++-------------------------- 1 file changed, 43 insertions(+), 44 deletions(-) diff --git a/src/fonts.rs b/src/fonts.rs index 4e795c8..05d63cc 100644 --- a/src/fonts.rs +++ b/src/fonts.rs @@ -165,27 +165,21 @@ impl Fonts { .tree_builder(&mut self.font_context, 1.0, false, &style); builder.push_text(text); - let (mut layout, _) = builder.build(); - layout.break_all_lines(None); + let (mut parley_layout, _) = builder.build(); + parley_layout.break_all_lines(None); - for line in layout.lines() { - for item in line.items() { - let parley::PositionedLayoutItem::GlyphRun(run) = item else { - continue; - }; - - for horizontal_offset in SubpixelBin::<4>::BIN_OFFSETS { - self.glyph_rasterizer - .rasterize_glyph_run( - &mut self.atlas, - &run, - LayoutVector::new(horizontal_offset, 0.0), - ScaleFactor::identity(), - ) - .for_each(|_| {}); - } + for_each_glyph_run(&parley_layout, |run| { + for horizontal_offset in SubpixelBin::<4>::BIN_OFFSETS { + self.glyph_rasterizer + .rasterize_glyph_run( + &mut self.atlas, + &run, + LayoutVector::new(horizontal_offset, 0.0), + ScaleFactor::identity(), + ) + .for_each(|_| {}); } - } + }); } pub fn layout(&mut self, job: LayoutJob, scale_factor: ScaleFactor) -> Rc { @@ -231,7 +225,7 @@ impl Fonts { parley_layout.break_all_lines(None); // extra offset applied to each glyph to get position relative to layout origin - let mut glyph_offset = LayoutVector::new(0.0, 0.0); + let mut glyph_offset = LayoutVector::zero(); if let Some(alignment) = job.alignment { let alignment = match alignment { Alignment::Min => parley::Alignment::Start, @@ -249,31 +243,22 @@ impl Fonts { let mut glyphs = Vec::new(); - for line in parley_layout.lines() { - for item in line.items() { - let parley::PositionedLayoutItem::GlyphRun(run) = item else { - continue; - }; - - for rasterized_glyph in self.glyph_rasterizer.rasterize_glyph_run( - &mut self.atlas, - &run, - glyph_offset, - scale_factor, - ) { - let Some(glyph) = rasterized_glyph else { - continue; - }; - - glyphs.push(PositionedGlyph { - rect: glyph.rect, - uv: glyph.uv, - layer_idx: glyph.texture_layer_idx, - color: glyph.color, - }); - } + for_each_glyph_run(&parley_layout, |run| { + for rasterized in self.glyph_rasterizer.rasterize_glyph_run( + &mut self.atlas, + &run, + glyph_offset, + scale_factor, + ) { + let Some(glyph) = rasterized else { continue }; + glyphs.push(PositionedGlyph { + rect: glyph.rect, + uv: glyph.uv, + layer_idx: glyph.texture_layer_idx, + color: glyph.color, + }); } - } + }); Layout { width: parley_layout.full_width(), @@ -282,3 +267,17 @@ impl Fonts { } } } + +/// Applies a function to each glyph run in a `parley::Layout` +fn for_each_glyph_run<'a>( + layout: &'a parley::Layout, + mut f: impl FnMut(&parley::GlyphRun<'a, Srgba>), +) { + for line in layout.lines() { + for item in line.items() { + if let parley::PositionedLayoutItem::GlyphRun(run) = item { + f(&run); + } + } + } +}