Skip to content

Commit 250223c

Browse files
EpicEricEElaurmaedje
authored andcommitted
Resolve lengths in math with scaled font size (#5168)
1 parent b060dd7 commit 250223c

File tree

10 files changed

+33
-21
lines changed

10 files changed

+33
-21
lines changed

crates/typst/src/layout/rel.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@ impl Rel<Length> {
8686
None
8787
}
8888
}
89+
90+
/// Convert to a relative length with the absolute part resolved at the
91+
/// given font size.
92+
pub fn at(self, font_size: Abs) -> Rel<Abs> {
93+
self.map(|abs| abs.at(font_size))
94+
}
8995
}
9096

9197
impl<T: Numeric + Debug> Debug for Rel<T> {

crates/typst/src/math/accent.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use crate::diag::{bail, SourceResult};
22
use crate::foundations::{
3-
cast, elem, func, Content, NativeElement, Packed, Resolve, Smart, StyleChain, Value,
3+
cast, elem, func, Content, NativeElement, Packed, Smart, StyleChain, Value,
44
};
55
use crate::layout::{Em, Frame, Length, Point, Rel, Size};
66
use crate::math::{
7-
style_cramped, FrameFragment, GlyphFragment, LayoutMath, MathContext, MathFragment,
8-
Scaled,
7+
scaled_font_size, style_cramped, FrameFragment, GlyphFragment, LayoutMath,
8+
MathContext, MathFragment, Scaled,
99
};
1010
use crate::text::TextElem;
1111

@@ -123,7 +123,7 @@ impl LayoutMath for Packed<AccentElem> {
123123
let width = self
124124
.size(styles)
125125
.unwrap_or(Rel::one())
126-
.resolve(styles)
126+
.at(scaled_font_size(ctx, styles))
127127
.relative_to(base.width());
128128

129129
// Forcing the accent to be at least as large as the base makes it too

crates/typst/src/math/cancel.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
use comemo::Track;
22

33
use crate::diag::{At, SourceResult};
4-
use crate::foundations::{
5-
cast, elem, Content, Context, Func, Packed, Resolve, Smart, StyleChain,
6-
};
4+
use crate::foundations::{cast, elem, Content, Context, Func, Packed, Smart, StyleChain};
75
use crate::layout::{
86
Abs, Angle, Frame, FrameItem, Length, Point, Ratio, Rel, Size, Transform,
97
};
10-
use crate::math::{FrameFragment, LayoutMath, MathContext};
8+
use crate::math::{scaled_font_size, FrameFragment, LayoutMath, MathContext};
119
use crate::syntax::Span;
1210
use crate::text::TextElem;
1311
use crate::visualize::{FixedStroke, Geometry, Stroke};
@@ -120,7 +118,7 @@ impl LayoutMath for Packed<CancelElem> {
120118
let mut body = body.into_frame();
121119
let body_size = body.size();
122120
let span = self.span();
123-
let length = self.length(styles).resolve(styles);
121+
let length = self.length(styles).at(scaled_font_size(ctx, styles));
124122

125123
let stroke = self.stroke(styles).unwrap_or(FixedStroke {
126124
paint: TextElem::fill_in(styles).as_decoration(),

crates/typst/src/math/ctx.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use unicode_segmentation::UnicodeSegmentation;
1111

1212
use crate::diag::SourceResult;
1313
use crate::engine::Engine;
14-
use crate::foundations::{Content, Packed, Resolve, StyleChain, StyleVec};
14+
use crate::foundations::{Content, Packed, StyleChain, StyleVec};
1515
use crate::introspection::{SplitLocator, TagElem};
1616
use crate::layout::{
1717
layout_frame, Abs, Axes, BoxElem, Em, Frame, HElem, PlaceElem, Region, Size, Spacing,
@@ -220,7 +220,7 @@ impl MathContext<'_, '_, '_> {
220220
if let Spacing::Rel(rel) = elem.amount() {
221221
if rel.rel.is_zero() {
222222
self.push(MathFragment::Spacing(
223-
rel.abs.resolve(styles),
223+
rel.abs.at(scaled_font_size(self, styles)),
224224
elem.weak(styles),
225225
));
226226
}

crates/typst/src/math/matrix.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ pub struct VecElem {
6363
/// #set math.vec(gap: 1em)
6464
/// $ vec(1, 2) $
6565
/// ```
66-
#[resolve]
6766
#[default(DEFAULT_ROW_GAP.into())]
6867
pub gap: Rel<Length>,
6968

@@ -81,7 +80,7 @@ impl LayoutMath for Packed<VecElem> {
8180
styles,
8281
self.children(),
8382
self.align(styles),
84-
self.gap(styles),
83+
self.gap(styles).at(scaled_font_size(ctx, styles)),
8584
LeftRightAlternator::Right,
8685
)?;
8786

@@ -182,7 +181,6 @@ pub struct MatElem {
182181
/// #set math.mat(row-gap: 1em)
183182
/// $ mat(1, 2; 3, 4) $
184183
/// ```
185-
#[resolve]
186184
#[parse(
187185
let gap = args.named("gap")?;
188186
args.named("row-gap")?.or(gap)
@@ -196,7 +194,6 @@ pub struct MatElem {
196194
/// #set math.mat(column-gap: 1em)
197195
/// $ mat(1, 2; 3, 4) $
198196
/// ```
199-
#[resolve]
200197
#[parse(args.named("column-gap")?.or(gap))]
201198
#[default(DEFAULT_COL_GAP.into())]
202199
pub column_gap: Rel<Length>,
@@ -268,14 +265,17 @@ impl LayoutMath for Packed<MatElem> {
268265
}
269266
}
270267

268+
let font_size = scaled_font_size(ctx, styles);
269+
let column_gap = self.column_gap(styles).at(font_size);
270+
let row_gap = self.row_gap(styles).at(font_size);
271271
let delim = self.delim(styles);
272272
let frame = layout_mat_body(
273273
ctx,
274274
styles,
275275
rows,
276276
self.align(styles),
277277
augment,
278-
Axes::new(self.column_gap(styles), self.row_gap(styles)),
278+
Axes::new(column_gap, row_gap),
279279
self.span(),
280280
)?;
281281

@@ -322,7 +322,6 @@ pub struct CasesElem {
322322
/// #set math.cases(gap: 1em)
323323
/// $ x = cases(1, 2) $
324324
/// ```
325-
#[resolve]
326325
#[default(DEFAULT_ROW_GAP.into())]
327326
pub gap: Rel<Length>,
328327

@@ -340,7 +339,7 @@ impl LayoutMath for Packed<CasesElem> {
340339
styles,
341340
self.children(),
342341
FixedAlignment::Start,
343-
self.gap(styles),
342+
self.gap(styles).at(scaled_font_size(ctx, styles)),
344343
LeftRightAlternator::None,
345344
)?;
346345

crates/typst/src/math/stretch.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ use ttf_parser::math::{GlyphAssembly, GlyphConstruction, GlyphPart};
22
use ttf_parser::LazyArray16;
33

44
use crate::diag::SourceResult;
5-
use crate::foundations::{elem, Content, Packed, Resolve, Smart, StyleChain};
5+
use crate::foundations::{elem, Content, Packed, Smart, StyleChain};
66
use crate::layout::{Abs, Axis, Frame, Length, Point, Rel, Size, VAlignment};
77
use crate::math::{
8-
GlyphFragment, LayoutMath, MathContext, MathFragment, Scaled, VariantFragment,
8+
scaled_font_size, GlyphFragment, LayoutMath, MathContext, MathFragment, Scaled,
9+
VariantFragment,
910
};
1011
use crate::utils::Get;
1112

@@ -91,7 +92,7 @@ pub(super) fn stretch_fragment(
9192
glyph,
9293
stretch
9394
.unwrap_or(Rel::one())
94-
.resolve(styles)
95+
.at(scaled_font_size(ctx, styles))
9596
.relative_to(relative_to_size),
9697
short_fall,
9798
axis,
331 Bytes
Loading

tests/ref/math-spacing-script.png

346 Bytes
Loading

tests/suite/math/accent.typ

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,7 @@ $ tilde(integral), tilde(integral)_a^b, tilde(integral_a^b) $
3131
--- math-accent-sized ---
3232
// Test accent size.
3333
$tilde(sum), tilde(sum, size: #50%), accent(H, hat, size: #200%)$
34+
35+
--- math-accent-sized-script ---
36+
// Test accent size in script size.
37+
$tilde(U, size: #1.1em), x^tilde(U, size: #1.1em), sscript(tilde(U, size: #1.1em))$

tests/suite/math/spacing.typ

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ $integral f(x) thin dif x$,
4949
// Both are weak, collide
5050
$integral f(x) #h(0.166em, weak: true)dif x$
5151

52+
--- math-spacing-script ---
53+
// Test spacing in script size
54+
$x^(a #h(1em) b) + x^x^(a #h(1em) b) + sscript(a #h(1em) b)$
55+
5256
--- math-spacing-ignorant ---
5357
// Test spacing with ignorant elements
5458
$#metadata(none) "text"$ \

0 commit comments

Comments
 (0)