From 333dfd505c3145b6e6db3365bc9667ca505604b3 Mon Sep 17 00:00:00 2001 From: mkorje Date: Sun, 27 Apr 2025 21:04:39 +1000 Subject: [PATCH] Add `delim-size` parameter to `mat`, `vec`, and `cases` Takes either a function or a relative length, just like with `lr`, `stretch`, and `accent` which was changed in the previous two commits. The default is now much clearer to the user: `x => x * 1.1 - 0.1em`. --- crates/typst-layout/src/math/frac.rs | 7 ++-- crates/typst-layout/src/math/mat.rs | 48 ++++++++++++++++++------ crates/typst-layout/src/math/shared.rs | 5 +-- crates/typst-library/src/math/lr.rs | 2 +- crates/typst-library/src/math/matrix.rs | 41 ++++++++++++++++++-- tests/ref/math-cases-delim-size.png | Bin 0 -> 1005 bytes tests/ref/math-mat-delim-size.png | Bin 0 -> 1327 bytes tests/ref/math-vec-delim-size.png | Bin 0 -> 1340 bytes tests/suite/math/cases.typ | 6 +++ tests/suite/math/mat.typ | 8 +++- tests/suite/math/vec.typ | 6 +++ 11 files changed, 98 insertions(+), 25 deletions(-) create mode 100644 tests/ref/math-cases-delim-size.png create mode 100644 tests/ref/math-mat-delim-size.png create mode 100644 tests/ref/math-vec-delim-size.png diff --git a/crates/typst-layout/src/math/frac.rs b/crates/typst-layout/src/math/frac.rs index 12a2c6fd1..beb811b03 100644 --- a/crates/typst-layout/src/math/frac.rs +++ b/crates/typst-layout/src/math/frac.rs @@ -1,14 +1,13 @@ use typst_library::diag::SourceResult; use typst_library::foundations::{Content, Packed, Resolve, StyleChain, SymbolElem}; use typst_library::layout::{Em, Frame, FrameItem, Point, Size}; -use typst_library::math::{BinomElem, FracElem}; +use typst_library::math::{BinomElem, FracElem, DELIM_SHORT_FALL}; use typst_library::text::TextElem; use typst_library::visualize::{FixedStroke, Geometry}; use typst_syntax::Span; use super::{ - style_for_denominator, style_for_numerator, FrameFragment, GlyphFragment, - MathContext, DELIM_SHORT_FALL, + style_for_denominator, style_for_numerator, FrameFragment, GlyphFragment, MathContext, }; const FRAC_AROUND: Em = Em::new(0.1); @@ -49,7 +48,7 @@ fn layout_frac_like( binom: bool, span: Span, ) -> SourceResult<()> { - let short_fall = DELIM_SHORT_FALL.resolve(styles); + let short_fall = DELIM_SHORT_FALL.abs().resolve(styles); let axis = scaled!(ctx, styles, axis_height); let thickness = scaled!(ctx, styles, fraction_rule_thickness); let shift_up = scaled!( diff --git a/crates/typst-layout/src/math/mat.rs b/crates/typst-layout/src/math/mat.rs index 4a897a03e..249913999 100644 --- a/crates/typst-layout/src/math/mat.rs +++ b/crates/typst-layout/src/math/mat.rs @@ -1,19 +1,20 @@ use typst_library::diag::{bail, warning, SourceResult}; use typst_library::foundations::{Content, Packed, Resolve, StyleChain}; use typst_library::layout::{ - Abs, Axes, Em, FixedAlignment, Frame, FrameItem, Point, Ratio, Rel, Size, + Abs, Axes, Em, FixedAlignment, Frame, FrameItem, Point, Rel, Size, +}; +use typst_library::math::{ + Augment, AugmentOffsets, CasesElem, MatElem, StretchSize, VecElem, }; -use typst_library::math::{Augment, AugmentOffsets, CasesElem, MatElem, VecElem}; use typst_library::text::TextElem; use typst_library::visualize::{FillRule, FixedStroke, Geometry, LineCap, Shape}; use typst_syntax::Span; use super::{ alignments, style_for_denominator, AlignmentResult, FrameFragment, GlyphFragment, - LeftRightAlternator, MathContext, DELIM_SHORT_FALL, + LeftRightAlternator, MathContext, }; -const VERTICAL_PADDING: Ratio = Ratio::new(0.1); const DEFAULT_STROKE_THICKNESS: Em = Em::new(0.05); /// Lays out a [`VecElem`]. @@ -39,7 +40,15 @@ pub fn layout_vec( )?; let delim = elem.delim.get(styles); - layout_delimiters(ctx, styles, frame, delim.open(), delim.close(), span) + layout_delimiters( + ctx, + styles, + frame, + elem.delim_size.get_ref(styles), + delim.open(), + delim.close(), + span, + ) } /// Lays out a [`CasesElem`]. @@ -70,7 +79,15 @@ pub fn layout_cases( } else { (delim.open(), None) }; - layout_delimiters(ctx, styles, frame, open, close, span) + layout_delimiters( + ctx, + styles, + frame, + elem.delim_size.get_ref(styles), + open, + close, + span, + ) } /// Lays out a [`MatElem`]. @@ -128,7 +145,15 @@ pub fn layout_mat( )?; let delim = elem.delim.get(styles); - layout_delimiters(ctx, styles, frame, delim.open(), delim.close(), span) + layout_delimiters( + ctx, + styles, + frame, + elem.delim_size.get_ref(styles), + delim.open(), + delim.close(), + span, + ) } /// Layout the inner contents of a matrix, vector, or cases. @@ -309,19 +334,20 @@ fn layout_delimiters( ctx: &mut MathContext, styles: StyleChain, mut frame: Frame, + size: &StretchSize, left: Option, right: Option, span: Span, ) -> SourceResult<()> { - let short_fall = DELIM_SHORT_FALL.resolve(styles); let axis = scaled!(ctx, styles, axis_height); let height = frame.height(); - let target = height + VERTICAL_PADDING.of(height); frame.set_baseline(height / 2.0 + axis); + let target = size.resolve(ctx.engine, styles, height)?; + if let Some(left_c) = left { let mut left = GlyphFragment::new_char(ctx.font, styles, left_c, span)?; - left.stretch_vertical(ctx, target - short_fall); + left.stretch_vertical(ctx, target); left.center_on_axis(); ctx.push(left); } @@ -330,7 +356,7 @@ fn layout_delimiters( if let Some(right_c) = right { let mut right = GlyphFragment::new_char(ctx.font, styles, right_c, span)?; - right.stretch_vertical(ctx, target - short_fall); + right.stretch_vertical(ctx, target); right.center_on_axis(); ctx.push(right); } diff --git a/crates/typst-layout/src/math/shared.rs b/crates/typst-layout/src/math/shared.rs index c9d20aa68..bd088f7df 100644 --- a/crates/typst-layout/src/math/shared.rs +++ b/crates/typst-layout/src/math/shared.rs @@ -1,7 +1,7 @@ use ttf_parser::math::MathValue; use ttf_parser::Tag; use typst_library::foundations::{Style, StyleChain}; -use typst_library::layout::{Abs, Em, FixedAlignment, Frame, Point, Size}; +use typst_library::layout::{Abs, FixedAlignment, Frame, Point, Size}; use typst_library::math::{EquationElem, MathSize}; use typst_library::text::{FontFeatures, TextElem}; use typst_utils::LazyHash; @@ -30,9 +30,6 @@ macro_rules! percent { }; } -/// How much less high scaled delimiters can be than what they wrap. -pub const DELIM_SHORT_FALL: Em = Em::new(0.1); - /// Converts some unit to an absolute length with the current font & font size. pub trait Scaled { fn scaled(self, ctx: &MathContext, font_size: Abs) -> Abs; diff --git a/crates/typst-library/src/math/lr.rs b/crates/typst-library/src/math/lr.rs index ff234167e..d9f7c80ee 100644 --- a/crates/typst-library/src/math/lr.rs +++ b/crates/typst-library/src/math/lr.rs @@ -2,7 +2,7 @@ use crate::foundations::{elem, func, Content, NativeElement, NativeFunc, SymbolE use crate::layout::{Em, Length, Ratio, Rel}; use crate::math::{Mathy, StretchSize}; -const DELIM_SHORT_FALL: Em = Em::new(-0.1); +pub const DELIM_SHORT_FALL: Em = Em::new(-0.1); #[func(name = "x => x - 0.1em")] pub const fn default_lr_size(base: Length) -> Rel { diff --git a/crates/typst-library/src/math/matrix.rs b/crates/typst-library/src/math/matrix.rs index 2a51caefd..3eb30a662 100644 --- a/crates/typst-library/src/math/matrix.rs +++ b/crates/typst-library/src/math/matrix.rs @@ -5,15 +5,27 @@ use unicode_math_class::MathClass; use crate::diag::{bail, At, HintedStrResult, StrResult}; use crate::foundations::{ - array, cast, dict, elem, Array, Content, Dict, Fold, NoneValue, Resolve, Smart, - StyleChain, Symbol, Value, + array, cast, dict, elem, func, Array, Content, Dict, Fold, NativeFunc, NoneValue, + Resolve, Smart, StyleChain, Symbol, Value, }; -use crate::layout::{Abs, Em, HAlignment, Length, Rel}; -use crate::math::Mathy; +use crate::layout::{Abs, Em, HAlignment, Length, Ratio, Rel}; +use crate::math::{Mathy, StretchSize, DELIM_SHORT_FALL}; use crate::visualize::Stroke; const DEFAULT_ROW_GAP: Em = Em::new(0.2); const DEFAULT_COL_GAP: Em = Em::new(0.5); +const VERTICAL_PADDING: Ratio = Ratio::new(1.1); + +#[func(name = "x => x * 1.1 - 0.1em")] +const fn default_mat_size(base: Length) -> Rel { + Rel { + rel: Ratio::zero(), + abs: Length { + abs: Abs::raw(base.abs.to_raw() * VERTICAL_PADDING.get()), + em: DELIM_SHORT_FALL, + }, + } +} /// A column vector. /// @@ -40,6 +52,13 @@ pub struct VecElem { #[default(DelimiterPair::PAREN)] pub delim: DelimiterPair, + /// The size of the delimiters, relative to the elements' total height. + /// + /// See the [stretch documentation]($math.stretch.size) for more + /// information on sizes. + #[default(::data().into())] + pub delim_size: StretchSize, + /// The horizontal alignment that each element should have. /// /// ```example @@ -99,6 +118,13 @@ pub struct MatElem { #[default(DelimiterPair::PAREN)] pub delim: DelimiterPair, + /// The size of the delimiters, relative to the cells' total height. + /// + /// See the [stretch documentation]($math.stretch.size) for more + /// information on sizes. + #[default(::data().into())] + pub delim_size: StretchSize, + /// The horizontal alignment that each cell should have. /// /// ```example @@ -238,6 +264,13 @@ pub struct CasesElem { #[default(DelimiterPair::BRACE)] pub delim: DelimiterPair, + /// The size of the delimiters, relative to the branches' total height. + /// + /// See the [stretch documentation]($math.stretch.size) for more + /// information on sizes. + #[default(::data().into())] + pub delim_size: StretchSize, + /// Whether the direction of cases should be reversed. /// /// ```example diff --git a/tests/ref/math-cases-delim-size.png b/tests/ref/math-cases-delim-size.png new file mode 100644 index 0000000000000000000000000000000000000000..92231d6915396867ab906a7d7afa70f0f6173d40 GIT binary patch literal 1005 zcmVjz3W6@sgSLbbmSwSKm(6sM zGNLP4pn1a-O%cfrS6kC|-Dw^7HaqUluXASBZNeUUIlqCLefa*)`N8sG_slsn3w!AUUg8b`QM6nOMX{yvdZX~vk^7@HxH3|_JB z4KanUR)EDp2kYw8M%_A^x9`BAyG&uX6SAKvV2VtOoKi>2P!AlaWW2_oGvZ1u%kcsD za>^3z{;lI|Vc~+Pu5Nc~8$erXx7rS#505d0rIXDKRdnDs^R)10H!ZQ#jZGCy;rAAc zrI!w}6H8i|O|zb~yHNJAJlCrZymMvj10?w%fC~G$Fw&8!|4kyUBtP+s!yK=+8N+EpW1wJv>_rb8e-0H-wO3 z_2L>o%SUyA^E;j8IIwKXygovzG%ZB3F)b~vnK>-Y*G$@0;XQLz=V>O}pQFLAvOLLg z;AkPB!C9IO+q%|?2gCW>n$y$#4G$h(Od@>sQf`D)$8Oq++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DF1k2aLGBLMQ5x1VL>r$Zx;P~>e(OjEt$d|Re8}F_TfPu(E(v!2lQ`RWP`CQV_seQNU7#)AX8QinYVnnD z@M@zi2E(4Kq!{Hb8ADI}QYyx|{R0Ru9~X#c3J%|=Cr(Ia!_*6nUTAaG*~DX`5K&A| zJo>^EK>xd-*^ch*2R3m=K7^j2Cx$McYXCK8BL4v>->J8XRo%B{DvJ*pZ+EL{iDCX1 yjsT0AslWpzql1SGK}mc){n!ubys literal 0 HcmV?d00001 diff --git a/tests/suite/math/cases.typ b/tests/suite/math/cases.typ index 306c1ae80..3765561ea 100644 --- a/tests/suite/math/cases.typ +++ b/tests/suite/math/cases.typ @@ -16,6 +16,12 @@ $ x = cases(1, 2) $ #set math.cases(delim: sym.angle.l) $ cases(a, b, c) $ +--- math-cases-delim-size --- +// Test setting delimiter size. +$ cases(reverse: #true, 1, 2, 3) cases(delim-size: #100%, 1, 2, 3) $ +#set math.cases(delim-size: x => calc.max(x - 5pt, x * 0.901)) +$ cases(1, 2) cases(1, 2, 3, 4) $ + --- math-cases-linebreaks --- // Warning: 40-49 linebreaks are ignored in branches // Hint: 40-49 use commas instead to separate each line diff --git a/tests/suite/math/mat.typ b/tests/suite/math/mat.typ index 80f190605..cde0eae98 100644 --- a/tests/suite/math/mat.typ +++ b/tests/suite/math/mat.typ @@ -54,6 +54,12 @@ $ a + mat(delim: #none, 1, 2; 3, 4) + b $ $ mat(1, 2; 3, 4; delim: "[") $, ) +--- math-mat-delim-size --- +// Test setting delimiter size. +$ mat(c; c; c) mat(delim-size: #100%, c; c; c) $ +#set math.mat(delim-size: x => calc.max(x - 5pt, x * 0.901)) +$ mat(delim: "[", f; f; f; f) mat(delim: ||, x; x; x; x) $ + --- math-mat-spread --- // Test argument spreading in matrix. $ mat(..#range(1, 5).chunks(2)) @@ -263,7 +269,7 @@ $ mat(a; b; c) mat(a \ b \ c) $ --- math-mat-vec-cases-unity --- // Test that matrices, vectors, and cases are all laid out the same. $ mat(z_(n_p); a^2) - vec(z_(n_p), a^2) + vec(z_(n_p), a^2) cases(reverse: #true, delim: \(, z_(n_p), a^2) cases(delim: \(, z_(n_p), a^2) $ diff --git a/tests/suite/math/vec.typ b/tests/suite/math/vec.typ index e5ee409ec..e976ca645 100644 --- a/tests/suite/math/vec.typ +++ b/tests/suite/math/vec.typ @@ -50,6 +50,12 @@ $ vec(1, 2) $ // Error: 22-33 invalid delimiter: "%" #set math.vec(delim: (none, "%")) +--- math-vec-delim-size --- +// Test setting delimiter size. +$ vec(1, 2, 3) vec(delim-size: #100%, 1, 2, 3) $ +#set math.vec(delim-size: x => calc.max(x - 5pt, x * 0.901)) +$ vec(delim: "{", 1, 2, 3) vec(delim: "[", 1, 2, 3) $ + --- math-vec-linebreaks --- // Warning: 20-29 linebreaks are ignored in elements // Hint: 20-29 use commas instead to separate each line