Compare commits

...

4 Commits

Author SHA1 Message Date
Max
0f1ad6dd85
Merge 53d13bae369ec3a186e2a7e3a340cb0032c48e1a into 7278d887cf05fadc9a96478830e5876739b78f53 2025-07-23 22:22:38 +08:00
Tobias Schmitz
7278d887cf
Fix bounding box computation for lines in curves (#6647)
Co-authored-by: Laurenz <laurmaedje@gmail.com>
2025-07-23 14:17:03 +00:00
mkorje
53d13bae36
Add warning 2025-07-08 20:18:14 +10:00
mkorje
7057809e23
Require parentheses in all function-like sub/superscripts 2025-07-08 20:16:45 +10:00
13 changed files with 75 additions and 27 deletions

View File

@ -1,5 +1,5 @@
use ecow::eco_format; use ecow::eco_format;
use typst_library::diag::{At, SourceResult}; use typst_library::diag::{warning, At, SourceResult};
use typst_library::foundations::{Content, NativeElement, Symbol, SymbolElem, Value}; use typst_library::foundations::{Content, NativeElement, Symbol, SymbolElem, Value};
use typst_library::math::{ use typst_library::math::{
AlignPointElem, AttachElem, FracElem, LrElem, PrimesElem, RootElem, AlignPointElem, AttachElem, FracElem, LrElem, PrimesElem, RootElem,
@ -80,7 +80,14 @@ impl Eval for ast::MathAttach<'_> {
let mut elem = AttachElem::new(base); let mut elem = AttachElem::new(base);
if let Some(expr) = self.top() { if let Some(expr) = self.top() {
elem.t.set(Some(expr.eval_display(vm)?)); let top = expr.eval(vm)?;
if let Value::Func(_) = top {
vm.engine.sink.warn(warning!(
expr.span(), "function literal used as superscript";
hint: "wrap the entire function call in parentheses",
));
}
elem.t.set(Some(top.display().spanned(self.span())));
} }
// Always attach primes in scripts style (not limits style), // Always attach primes in scripts style (not limits style),
@ -90,7 +97,14 @@ impl Eval for ast::MathAttach<'_> {
} }
if let Some(expr) = self.bottom() { if let Some(expr) = self.bottom() {
elem.b.set(Some(expr.eval_display(vm)?)); let bottom = expr.eval(vm)?;
if let Value::Func(_) = bottom {
vm.engine.sink.warn(warning!(
expr.span(), "function literal used as subscript";
hint: "wrap the entire function call in parentheses",
));
}
elem.b.set(Some(bottom.display().spanned(self.span())));
} }
Ok(elem.pack()) Ok(elem.pack())

View File

@ -476,26 +476,18 @@ impl Curve {
/// Computes the size of the bounding box of this curve. /// Computes the size of the bounding box of this curve.
pub fn bbox_size(&self) -> Size { pub fn bbox_size(&self) -> Size {
let mut min_x = Abs::inf(); let mut min = Point::splat(Abs::inf());
let mut min_y = Abs::inf(); let mut max = Point::splat(-Abs::inf());
let mut max_x = -Abs::inf();
let mut max_y = -Abs::inf();
let mut cursor = Point::zero(); let mut cursor = Point::zero();
for item in self.0.iter() { for item in self.0.iter() {
match item { match item {
CurveItem::Move(to) => { CurveItem::Move(to) => {
min_x = min_x.min(cursor.x);
min_y = min_y.min(cursor.y);
max_x = max_x.max(cursor.x);
max_y = max_y.max(cursor.y);
cursor = *to; cursor = *to;
} }
CurveItem::Line(to) => { CurveItem::Line(to) => {
min_x = min_x.min(cursor.x); min = min.min(cursor).min(*to);
min_y = min_y.min(cursor.y); max = max.max(cursor).max(*to);
max_x = max_x.max(cursor.x);
max_y = max_y.max(cursor.y);
cursor = *to; cursor = *to;
} }
CurveItem::Cubic(c0, c1, end) => { CurveItem::Cubic(c0, c1, end) => {
@ -507,17 +499,17 @@ impl Curve {
); );
let bbox = cubic.bounding_box(); let bbox = cubic.bounding_box();
min_x = min_x.min(Abs::pt(bbox.x0)).min(Abs::pt(bbox.x1)); min.x = min.x.min(Abs::pt(bbox.x0)).min(Abs::pt(bbox.x1));
min_y = min_y.min(Abs::pt(bbox.y0)).min(Abs::pt(bbox.y1)); min.y = min.y.min(Abs::pt(bbox.y0)).min(Abs::pt(bbox.y1));
max_x = max_x.max(Abs::pt(bbox.x0)).max(Abs::pt(bbox.x1)); max.x = max.x.max(Abs::pt(bbox.x0)).max(Abs::pt(bbox.x1));
max_y = max_y.max(Abs::pt(bbox.y0)).max(Abs::pt(bbox.y1)); max.y = max.y.max(Abs::pt(bbox.y0)).max(Abs::pt(bbox.y1));
cursor = *end; cursor = *end;
} }
CurveItem::Close => (), CurveItem::Close => (),
} }
} }
Size::new(max_x - min_x, max_y - min_y) Size::new(max.x - min.x, max.y - min.y)
} }
} }

View File

@ -384,10 +384,10 @@ fn math_expr_prec(p: &mut Parser, min_prec: usize, stop: SyntaxKind) {
fn math_op(kind: SyntaxKind) -> Option<(SyntaxKind, SyntaxKind, ast::Assoc, usize)> { fn math_op(kind: SyntaxKind) -> Option<(SyntaxKind, SyntaxKind, ast::Assoc, usize)> {
match kind { match kind {
SyntaxKind::Underscore => { SyntaxKind::Underscore => {
Some((SyntaxKind::MathAttach, SyntaxKind::Hat, ast::Assoc::Right, 2)) Some((SyntaxKind::MathAttach, SyntaxKind::Hat, ast::Assoc::Right, 3))
} }
SyntaxKind::Hat => { SyntaxKind::Hat => {
Some((SyntaxKind::MathAttach, SyntaxKind::Underscore, ast::Assoc::Right, 2)) Some((SyntaxKind::MathAttach, SyntaxKind::Underscore, ast::Assoc::Right, 3))
} }
SyntaxKind::Slash => { SyntaxKind::Slash => {
Some((SyntaxKind::MathFrac, SyntaxKind::End, ast::Assoc::Left, 1)) Some((SyntaxKind::MathFrac, SyntaxKind::End, ast::Assoc::Left, 1))

Binary file not shown.

After

Width:  |  Height:  |  Size: 399 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.1 KiB

After

Width:  |  Height:  |  Size: 2.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 927 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 630 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.2 KiB

After

Width:  |  Height:  |  Size: 2.8 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 9.1 KiB

After

Width:  |  Height:  |  Size: 9.3 KiB

View File

@ -34,7 +34,7 @@ $tilde(sum), tilde(sum, size: #50%), accent(H, hat, size: #200%)$
--- math-accent-sized-script --- --- math-accent-sized-script ---
// Test accent size in script size. // Test accent size in script size.
$tilde(U, size: #1.1em), x^tilde(U, size: #1.1em), sscript(tilde(U, size: #1.1em))$ $tilde(U, size: #1.1em), x^(tilde(U, size: #1.1em)), sscript(tilde(U, size: #1.1em))$
--- math-accent-dotless --- --- math-accent-dotless ---
// Test dotless glyph variants. // Test dotless glyph variants.

View File

@ -42,9 +42,41 @@ attach(a, tl: u, t: t, tr: v, bl: x, b: b, br: y)
$ $
--- math-attach-followed-by-func-call --- --- math-attach-followed-by-func-call ---
// Test function call after subscript. // Test function call after subscripts and superscripts.
$pi_1(Y), a_f(x), a^zeta (x), a^abs(b)_sqrt(c) \ #set page(width: auto)
a^subset.eq (x), a_(zeta(x)), pi_(1(Y)), a^(abs(b))_(sqrt(c))$ $a_1(x), a^10(x), a_f(x), a^zeta(x), a_"text"(x),
// Warning: 22-25 function literal used as subscript
// Hint: 22-25 wrap the entire function call in parentheses
// Warning: 32-36 function literal used as superscript
// Hint: 32-36 wrap the entire function call in parentheses
a_∂(x), a^cos(x), a_abs(x), a^root(2, x), a_subset.eq(x) \
a_(1(x)), a^(10(x)), a_(f(x)), a^(zeta(x)), a_("text"(x)),
a_(∂(x)), a^(cos(x)), a_(abs(x)), a^(root(2, x)), a_(subset.eq(x))$
--- math-attach-followed-by-func-call-complex ---
// Test function call with named arguments after scripts.
#let cross = $c$
// Warning: 4-10 function literal used as subscript
// Hint: 4-10 wrap the entire function call in parentheses
$a_cancel(x, cross: #true)$
--- math-attach-followed-by-func-call-error ---
// Error: 14-19 unknown variable: cross
// Hint: 14-19 if you meant to display multiple letters as is, try adding spaces between each letter: `c r o s s`
// Hint: 14-19 or if you meant to display this as text, try placing it in quotes: `"cross"`
// Warning: 4-10 function literal used as subscript
// Hint: 4-10 wrap the entire function call in parentheses
$a_cancel(x, cross: #true)$
--- math-attach-followed-by-func-call-warning ---
// Test warning for function literal as subscripts and superscripts.
#let myfunc(arg) = $arg$
// Warning: 4-10 function literal used as subscript
// Hint: 4-10 wrap the entire function call in parentheses
$a_myfunc(x) a_(myfunc(x))$
// Warning: 4-10 function literal used as subscript
// Hint: 4-10 wrap the entire function call in parentheses
$a_myfunc a_(myfunc)$
--- math-attach-nested --- --- math-attach-nested ---
// Test associativity and scaling. // Test associativity and scaling.

View File

@ -215,7 +215,7 @@ box[]
hello.world hello.world
hello.world() hello.world()
hello.my.world() hello.my.world()
f_zeta(x), f_zeta(x)/1 f_(zeta(x)), f_(zeta(x))/1
emph(hello.my.world()) emph(hello.my.world())
emph(hello.my().world) emph(hello.my().world)
emph(hello.my().world()) emph(hello.my().world())

View File

@ -130,6 +130,16 @@
down, up, down, up, down, down, up, down, up, down,
) )
--- curve-stroke-gradient-sharp ---
#set page(width: auto)
#let down = curve.line((40pt, 40pt), relative: true)
#let up = curve.line((40pt, -40pt), relative: true)
#curve(
stroke: 4pt + gradient.linear(red, blue).sharp(3),
down, up, down, up, down,
)
--- curve-fill-rule --- --- curve-fill-rule ---
#stack( #stack(
dir: ltr, dir: ltr,