Fixed symbol style reset in stretching (#1195)

This commit is contained in:
sitandr 2023-05-19 16:33:15 +03:00 committed by GitHub
parent 84b9d9c990
commit 42c3a6fa72
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 43 additions and 22 deletions

View File

@ -164,6 +164,32 @@ impl GlyphFragment {
}
pub fn with_id(ctx: &MathContext, c: char, id: GlyphId, span: Span) -> Self {
let mut fragment = Self {
id,
c,
font: ctx.font.clone(),
lang: TextElem::lang_in(ctx.styles()),
fill: TextElem::fill_in(ctx.styles()),
style: ctx.style,
font_size: ctx.size,
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 {
@ -178,25 +204,11 @@ impl GlyphFragment {
width += italics;
}
Self {
id,
c,
font: ctx.font.clone(),
lang: TextElem::lang_in(ctx.styles()),
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,
class: match c {
':' => Some(MathClass::Relation),
_ => unicode_math_class::class(c),
},
span,
meta: MetaElem::data_in(ctx.styles()),
}
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 {

View File

@ -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));
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 11 KiB

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.8 KiB

After

Width:  |  Height:  |  Size: 4.8 KiB

View File

@ -36,3 +36,10 @@ $ lr(]sum_(x=1)^n x], size: #70%)
---
// Test predefined delimiter pairings.
$floor(x/2), ceil(x/2), abs(x), norm(x)$
---
// Test colored delimiters
$ lr(
text("(", fill: #green) a/b
text(")", fill: #blue)
) $