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`.
This commit is contained in:
mkorje 2025-04-27 21:04:39 +10:00
parent 5738c98ff5
commit 8c811110f3
No known key found for this signature in database
11 changed files with 99 additions and 26 deletions

View File

@ -1,14 +1,13 @@
use typst_library::diag::SourceResult; use typst_library::diag::SourceResult;
use typst_library::foundations::{Content, Packed, Resolve, StyleChain, SymbolElem}; use typst_library::foundations::{Content, Packed, Resolve, StyleChain, SymbolElem};
use typst_library::layout::{Em, Frame, FrameItem, Point, Size}; use typst_library::layout::{Em, Frame, FrameItem, Point, Size};
use typst_library::math::{BinomElem, FracElem}; use typst_library::math::{BinomElem, DELIM_SHORT_FALL, FracElem};
use typst_library::text::TextElem; use typst_library::text::TextElem;
use typst_library::visualize::{FixedStroke, Geometry}; use typst_library::visualize::{FixedStroke, Geometry};
use typst_syntax::Span; use typst_syntax::Span;
use super::{ use super::{
DELIM_SHORT_FALL, FrameFragment, GlyphFragment, MathContext, style_for_denominator, FrameFragment, GlyphFragment, MathContext, style_for_denominator, style_for_numerator,
style_for_numerator,
}; };
const FRAC_AROUND: Em = Em::new(0.1); const FRAC_AROUND: Em = Em::new(0.1);
@ -49,7 +48,7 @@ fn layout_frac_like(
binom: bool, binom: bool,
span: Span, span: Span,
) -> SourceResult<()> { ) -> 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 axis = scaled!(ctx, styles, axis_height);
let thickness = scaled!(ctx, styles, fraction_rule_thickness); let thickness = scaled!(ctx, styles, fraction_rule_thickness);
let shift_up = scaled!( let shift_up = scaled!(

View File

@ -1,19 +1,20 @@
use typst_library::diag::{SourceResult, bail, warning}; use typst_library::diag::{SourceResult, bail, warning};
use typst_library::foundations::{Content, Packed, Resolve, StyleChain}; use typst_library::foundations::{Content, Packed, Resolve, StyleChain};
use typst_library::layout::{ 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::text::TextElem;
use typst_library::visualize::{FillRule, FixedStroke, Geometry, LineCap, Shape}; use typst_library::visualize::{FillRule, FixedStroke, Geometry, LineCap, Shape};
use typst_syntax::Span; use typst_syntax::Span;
use super::{ use super::{
AlignmentResult, DELIM_SHORT_FALL, FrameFragment, GlyphFragment, LeftRightAlternator, AlignmentResult, FrameFragment, GlyphFragment, LeftRightAlternator, MathContext,
MathContext, alignments, style_for_denominator, alignments, style_for_denominator,
}; };
const VERTICAL_PADDING: Ratio = Ratio::new(0.1);
const DEFAULT_STROKE_THICKNESS: Em = Em::new(0.05); const DEFAULT_STROKE_THICKNESS: Em = Em::new(0.05);
/// Lays out a [`VecElem`]. /// Lays out a [`VecElem`].
@ -39,7 +40,15 @@ pub fn layout_vec(
)?; )?;
let delim = elem.delim.get(styles); 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`]. /// Lays out a [`CasesElem`].
@ -70,7 +79,15 @@ pub fn layout_cases(
} else { } else {
(delim.open(), None) (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`]. /// Lays out a [`MatElem`].
@ -128,7 +145,15 @@ pub fn layout_mat(
)?; )?;
let delim = elem.delim.get(styles); 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. /// Layout the inner contents of a matrix, vector, or cases.
@ -309,19 +334,20 @@ fn layout_delimiters(
ctx: &mut MathContext, ctx: &mut MathContext,
styles: StyleChain, styles: StyleChain,
mut frame: Frame, mut frame: Frame,
size: &StretchSize,
left: Option<char>, left: Option<char>,
right: Option<char>, right: Option<char>,
span: Span, span: Span,
) -> SourceResult<()> { ) -> SourceResult<()> {
let short_fall = DELIM_SHORT_FALL.resolve(styles);
let axis = scaled!(ctx, styles, axis_height); let axis = scaled!(ctx, styles, axis_height);
let height = frame.height(); let height = frame.height();
let target = height + VERTICAL_PADDING.of(height);
frame.set_baseline(height / 2.0 + axis); frame.set_baseline(height / 2.0 + axis);
let target = size.resolve(ctx.engine, styles, height)?;
if let Some(left_c) = left { if let Some(left_c) = left {
let mut left = GlyphFragment::new_char(ctx.font, styles, left_c, span)?; 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(); left.center_on_axis();
ctx.push(left); ctx.push(left);
} }
@ -330,7 +356,7 @@ fn layout_delimiters(
if let Some(right_c) = right { if let Some(right_c) = right {
let mut right = GlyphFragment::new_char(ctx.font, styles, right_c, span)?; 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(); right.center_on_axis();
ctx.push(right); ctx.push(right);
} }

View File

@ -1,7 +1,7 @@
use ttf_parser::Tag; use ttf_parser::Tag;
use ttf_parser::math::MathValue; use ttf_parser::math::MathValue;
use typst_library::foundations::{Style, StyleChain}; 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::math::{EquationElem, MathSize};
use typst_library::text::{FontFeatures, TextElem}; use typst_library::text::{FontFeatures, TextElem};
use typst_utils::LazyHash; 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. /// Converts some unit to an absolute length with the current font & font size.
pub trait Scaled { pub trait Scaled {
fn scaled(self, ctx: &MathContext, font_size: Abs) -> Abs; fn scaled(self, ctx: &MathContext, font_size: Abs) -> Abs;

View File

@ -2,7 +2,7 @@ use crate::foundations::{Content, NativeElement, NativeFunc, SymbolElem, elem, f
use crate::layout::{Em, Length, Ratio, Rel}; use crate::layout::{Em, Length, Ratio, Rel};
use crate::math::{Mathy, StretchSize}; 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")] #[func(name = "x => x - 0.1em")]
pub const fn default_lr_size(base: Length) -> Rel { pub const fn default_lr_size(base: Length) -> Rel {

View File

@ -5,15 +5,27 @@ use unicode_math_class::MathClass;
use crate::diag::{At, HintedStrResult, StrResult, bail}; use crate::diag::{At, HintedStrResult, StrResult, bail};
use crate::foundations::{ use crate::foundations::{
Array, Content, Dict, Fold, NoneValue, Resolve, Smart, StyleChain, Symbol, Value, Array, Content, Dict, Fold, NativeFunc, NoneValue, Resolve, Smart, StyleChain,
array, cast, dict, elem, Symbol, Value, array, cast, dict, elem, func,
}; };
use crate::layout::{Abs, Em, HAlignment, Length, Rel}; use crate::layout::{Abs, Em, HAlignment, Length, Ratio, Rel};
use crate::math::Mathy; use crate::math::{DELIM_SHORT_FALL, Mathy, StretchSize};
use crate::visualize::Stroke; use crate::visualize::Stroke;
const DEFAULT_ROW_GAP: Em = Em::new(0.2); const DEFAULT_ROW_GAP: Em = Em::new(0.2);
const DEFAULT_COL_GAP: Em = Em::new(0.5); 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. /// A column vector.
/// ///
@ -40,6 +52,13 @@ pub struct VecElem {
#[default(DelimiterPair::PAREN)] #[default(DelimiterPair::PAREN)]
pub delim: DelimiterPair, 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(<default_mat_size>::data().into())]
pub delim_size: StretchSize,
/// The horizontal alignment that each element should have. /// The horizontal alignment that each element should have.
/// ///
/// ```example /// ```example
@ -99,6 +118,13 @@ pub struct MatElem {
#[default(DelimiterPair::PAREN)] #[default(DelimiterPair::PAREN)]
pub delim: DelimiterPair, 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(<default_mat_size>::data().into())]
pub delim_size: StretchSize,
/// The horizontal alignment that each cell should have. /// The horizontal alignment that each cell should have.
/// ///
/// ```example /// ```example
@ -238,6 +264,13 @@ pub struct CasesElem {
#[default(DelimiterPair::BRACE)] #[default(DelimiterPair::BRACE)]
pub delim: DelimiterPair, 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(<default_mat_size>::data().into())]
pub delim_size: StretchSize,
/// Whether the direction of cases should be reversed. /// Whether the direction of cases should be reversed.
/// ///
/// ```example /// ```example

Binary file not shown.

After

Width:  |  Height:  |  Size: 1005 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

View File

@ -16,6 +16,12 @@ $ x = cases(1, 2) $
#set math.cases(delim: sym.angle.l) #set math.cases(delim: sym.angle.l)
$ cases(a, b, c) $ $ 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 --- --- math-cases-linebreaks ---
// Warning: 40-49 linebreaks are ignored in branches // Warning: 40-49 linebreaks are ignored in branches
// Hint: 40-49 use commas instead to separate each line // Hint: 40-49 use commas instead to separate each line

View File

@ -54,6 +54,12 @@ $ a + mat(delim: #none, 1, 2; 3, 4) + b $
$ mat(1, 2; 3, 4; delim: "[") $, $ 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 --- --- math-mat-spread ---
// Test argument spreading in matrix. // Test argument spreading in matrix.
$ mat(..#range(1, 5).chunks(2)) $ mat(..#range(1, 5).chunks(2))

View File

@ -50,6 +50,12 @@ $ vec(1, 2) $
// Error: 22-33 invalid delimiter: "%" // Error: 22-33 invalid delimiter: "%"
#set math.vec(delim: (none, "%")) #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 --- --- math-vec-linebreaks ---
// Warning: 20-29 linebreaks are ignored in elements // Warning: 20-29 linebreaks are ignored in elements
// Hint: 20-29 use commas instead to separate each line // Hint: 20-29 use commas instead to separate each line