summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsitandr <60141933+sitandr@users.noreply.github.com>2023-05-19 16:33:15 +0300
committerGitHub <noreply@github.com>2023-05-19 15:33:15 +0200
commit42c3a6fa72be91c115e28e3866012b95b4d63cf4 (patch)
treedd147f27466bffbc0d7498e6d55181702967c643
parent84b9d9c9906a2f39718b6b19e40c9f5f200d7f96 (diff)
Fixed symbol style reset in stretching (#1195)
-rw-r--r--library/src/math/fragment.rs50
-rw-r--r--library/src/math/stretch.rs8
-rw-r--r--tests/ref/math/delimited.pngbin11282 -> 22993 bytes
-rw-r--r--tests/ref/math/underover.pngbin4877 -> 4944 bytes
-rw-r--r--tests/typ/math/delimited.typ7
5 files changed, 43 insertions, 22 deletions
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));
}
diff --git a/tests/ref/math/delimited.png b/tests/ref/math/delimited.png
index 0670337f..e9f6e2c1 100644
--- a/tests/ref/math/delimited.png
+++ b/tests/ref/math/delimited.png
Binary files differ
diff --git a/tests/ref/math/underover.png b/tests/ref/math/underover.png
index d090057d..7c92c982 100644
--- a/tests/ref/math/underover.png
+++ b/tests/ref/math/underover.png
Binary files differ
diff --git a/tests/typ/math/delimited.typ b/tests/typ/math/delimited.typ
index d22b76c0..24374b8c 100644
--- a/tests/typ/math/delimited.typ
+++ b/tests/typ/math/delimited.typ
@@ -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)
+ ) $