Skip to content

Commit

Permalink
text: Always iterate over all characters when evaluating font
Browse files Browse the repository at this point in the history
This is a natural assumption of Font::evaluate and makes writing code easier.
Code that renders glyphs will work the same way as non-renderable characters
will produce empty glyphs, but code that evaluates the font for measurement
purposes will use this property.
  • Loading branch information
kjarosh committed Dec 12, 2024
1 parent 0ec573f commit a78cd11
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions core/src/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,9 @@ impl<'gc> Font<'gc> {
/// of transforms and glyphs which will be consumed by the `glyph_func`
/// closure. This corresponds to the series of drawing operations necessary
/// to render the text on a single horizontal line.
///
/// It's guaranteed that this function will iterate over all characters
/// from the text, irrespectively of whether they have a glyph or not.
pub fn evaluate<FGlyph>(
&self,
text: &WStr, // TODO: take an `IntoIterator<Item=char>`, to not depend on string representation?
Expand All @@ -600,6 +603,9 @@ impl<'gc> Font<'gc> {

transform.matrix.a = scale;
transform.matrix.d = scale;

// TODO [KJ] I'm not sure whether we should iterate over characters here or over code units.
// I suspect Flash Player does not support full UTF-16 when displaying and laying out text.
let mut char_indices = text.char_indices().peekable();
let has_kerning_info = self.has_kerning_info();
let mut x = Twips::ZERO;
Expand Down Expand Up @@ -631,6 +637,10 @@ impl<'gc> Font<'gc> {
// Step horizontally.
transform.matrix.tx += twips_advance;
x += twips_advance;
} else {
// No glyph, zero advance. This makes it possible to use this method for purposes
// other than rendering the font, e.g. measurement, iterating over characters.
glyph_func(pos, &transform, &Glyph::empty(), Twips::ZERO, x);
}
}
}
Expand Down Expand Up @@ -809,6 +819,15 @@ pub struct Glyph {
}

impl Glyph {
/// Returns an empty glyph with zero advance.
pub fn empty() -> Self {
Self {
shape_handle: Default::default(),
shape: GlyphShape::None,
advance: Twips::ZERO,
}
}

pub fn shape_handle(&self, renderer: &mut dyn RenderBackend) -> Option<ShapeHandle> {
self.shape_handle
.borrow_mut()
Expand Down

0 comments on commit a78cd11

Please sign in to comment.