From 42c3a6fa72be91c115e28e3866012b95b4d63cf4 Mon Sep 17 00:00:00 2001 From: sitandr <60141933+sitandr@users.noreply.github.com> Date: Fri, 19 May 2023 16:33:15 +0300 Subject: Fixed symbol style reset in stretching (#1195) --- library/src/math/fragment.rs | 50 +++++++++++++++++++++++++++----------------- library/src/math/stretch.rs | 8 ++++--- 2 files changed, 36 insertions(+), 22 deletions(-) (limited to 'library/src') diff --git a/library/src/math/fragment.rs b/library/src/math/fragment.rs index 40dca347..027ab7da 100644 --- a/library/src/math/fragment.rs +++ b/library/src/math/fragment.rs @@ -164,21 +164,7 @@ impl GlyphFragment { } pub fn with_id(ctx: &MathContext, c: char, id: GlyphId, span: Span) -> Self { - let advance = ctx.ttf.glyph_hor_advance(id).unwrap_or_default(); - let italics = italics_correction(ctx, id).unwrap_or_default(); - let bbox = ctx.ttf.glyph_bounding_box(id).unwrap_or(Rect { - x_min: 0, - y_min: 0, - x_max: 0, - y_max: 0, - }); - - let mut width = advance.scaled(ctx); - if !is_extended_shape(ctx, id) { - width += italics; - } - - Self { + let mut fragment = Self { id, c, font: ctx.font.clone(), @@ -186,17 +172,43 @@ impl GlyphFragment { fill: TextElem::fill_in(ctx.styles()), style: ctx.style, font_size: ctx.size, - width, - ascent: bbox.y_max.scaled(ctx), - descent: -bbox.y_min.scaled(ctx), - italics_correction: italics, + width: Abs::zero(), + ascent: Abs::zero(), + descent: Abs::zero(), + italics_correction: Abs::zero(), class: match c { ':' => Some(MathClass::Relation), _ => unicode_math_class::class(c), }, span, meta: MetaElem::data_in(ctx.styles()), + }; + fragment.set_id(ctx, id); + fragment + } + + /// Sets element id and boxes in appropriate way without changing other + /// styles. This is used to replace the glyph with a stretch variant. + pub fn set_id(&mut self, ctx: &MathContext, id: GlyphId) { + let advance = ctx.ttf.glyph_hor_advance(id).unwrap_or_default(); + let italics = italics_correction(ctx, id).unwrap_or_default(); + let bbox = ctx.ttf.glyph_bounding_box(id).unwrap_or(Rect { + x_min: 0, + y_min: 0, + x_max: 0, + y_max: 0, + }); + + let mut width = advance.scaled(ctx); + if !is_extended_shape(ctx, id) { + width += italics; } + + self.id = id; + self.width = width; + self.ascent = bbox.y_max.scaled(ctx); + self.descent = -bbox.y_min.scaled(ctx); + self.italics_correction = italics; } pub fn height(&self) -> Abs { diff --git a/library/src/math/stretch.rs b/library/src/math/stretch.rs index bb454022..bbb0c9c4 100644 --- a/library/src/math/stretch.rs +++ b/library/src/math/stretch.rs @@ -33,7 +33,7 @@ impl GlyphFragment { /// The resulting frame may not have the exact desired width. fn stretch_glyph( ctx: &MathContext, - base: GlyphFragment, + mut base: GlyphFragment, target: Abs, short_fall: Abs, horizontal: bool, @@ -73,7 +73,8 @@ fn stretch_glyph( // This is either good or the best we've got. if short_target <= best_advance || construction.assembly.is_none() { - return GlyphFragment::with_id(ctx, base.c, best_id, base.span).into_variant(); + base.set_id(ctx, best_id); + return base.into_variant(); } // Assemble from parts. @@ -142,7 +143,8 @@ fn assemble( advance += ratio * (max_overlap - min_overlap); } - let fragment = GlyphFragment::with_id(ctx, base.c, part.glyph_id, base.span); + let mut fragment = base.clone(); + fragment.set_id(ctx, part.glyph_id); selected.push((fragment, advance)); } -- cgit v1.2.3