Remove the space_width field from MathContext

This commit is contained in:
mkorje 2025-05-28 14:54:59 +10:00
parent 439ebcfe0d
commit 3f9f742368
No known key found for this signature in database
3 changed files with 12 additions and 14 deletions

View File

@ -1044,9 +1044,8 @@ fn calculate_adjustability(ctx: &mut ShapingContext, lang: Lang, region: Option<
/// Difference between non-breaking and normal space.
fn nbsp_delta(font: &Font) -> Option<Em> {
let space = font.ttf().glyph_index(' ')?.0;
let nbsp = font.ttf().glyph_index('\u{00A0}')?.0;
Some(font.advance(nbsp)? - font.advance(space)?)
Some(font.advance(nbsp)? - font.space_width()?)
}
/// Returns true if all glyphs in `glyphs` have ranges within the range `range`.

View File

@ -373,7 +373,6 @@ struct MathContext<'a, 'v, 'e> {
// Font-related.
font: &'a Font,
constants: ttf_parser::math::Constants<'a>,
space_width: Em,
// Mutable.
fragments: Vec<MathFragment>,
}
@ -386,15 +385,10 @@ impl<'a, 'v, 'e> MathContext<'a, 'v, 'e> {
base: Size,
font: &'a Font,
) -> Self {
let math_table = font.ttf().tables().math.unwrap();
let constants = math_table.constants.unwrap();
let ttf = font.ttf();
let space_width = ttf
.glyph_index(' ')
.and_then(|id| ttf.glyph_hor_advance(id))
.map(|advance| font.to_em(advance))
.unwrap_or(THICK);
// These unwraps are safe as the font given is one returned by the
// find_math_font function, which only returns fonts that have a math
// constants table.
let constants = font.ttf().tables().math.unwrap().constants.unwrap();
Self {
engine,
@ -402,7 +396,6 @@ impl<'a, 'v, 'e> MathContext<'a, 'v, 'e> {
region: Region::new(base, Axes::splat(false)),
font,
constants,
space_width,
fragments: vec![],
}
}
@ -501,7 +494,8 @@ fn layout_realized(
if let Some(elem) = elem.to_packed::<TagElem>() {
ctx.push(MathFragment::Tag(elem.tag.clone()));
} else if elem.is::<SpaceElem>() {
ctx.push(MathFragment::Space(ctx.space_width.resolve(styles)));
let space_width = ctx.font.space_width().unwrap_or(THICK);
ctx.push(MathFragment::Space(space_width.resolve(styles)));
} else if elem.is::<LinebreakElem>() {
ctx.push(MathFragment::Linebreak);
} else if let Some(elem) = elem.to_packed::<HElem>() {

View File

@ -113,6 +113,11 @@ impl Font {
.map(|units| self.to_em(units))
}
/// Look up the width of a space.
pub fn space_width(&self) -> Option<Em> {
self.0.ttf.glyph_index(' ').and_then(|id| self.advance(id.0))
}
/// Lookup a name by id.
pub fn find_name(&self, id: u16) -> Option<String> {
find_name(&self.0.ttf, id)