diff --git a/crates/typst-layout/src/math/accent.rs b/crates/typst-layout/src/math/accent.rs index 73d821019..724e78355 100644 --- a/crates/typst-layout/src/math/accent.rs +++ b/crates/typst-layout/src/math/accent.rs @@ -1,13 +1,10 @@ use typst_library::diag::SourceResult; use typst_library::foundations::{Packed, StyleChain}; -use typst_library::layout::{Em, Frame, Point, Size}; +use typst_library::layout::{Frame, Point, Size}; use typst_library::math::{Accent, AccentElem}; use super::{style_cramped, FrameFragment, GlyphFragment, MathContext, MathFragment}; -/// How much the accent can be shorter than the base. -const ACCENT_SHORT_FALL: Em = Em::new(0.5); - /// Lays out an [`AccentElem`]. #[typst_macros::time(name = "math.accent", span = elem.span())] pub fn layout_accent( @@ -29,7 +26,7 @@ pub fn layout_accent( let base_class = base.class(); let base_attach = base.accent_attach(); - let width = elem.size(styles).relative_to(base.width()); + let width = elem.size(styles).resolve(ctx.engine, styles, base.width())?; let Accent(c) = elem.accent; let mut glyph = GlyphFragment::new(ctx, styles, c, elem.span()); @@ -42,8 +39,7 @@ pub fn layout_accent( // Forcing the accent to be at least as large as the base makes it too // wide in many case. - let short_fall = ACCENT_SHORT_FALL.at(glyph.font_size); - let variant = glyph.stretch_horizontal(ctx, width, short_fall); + let variant = glyph.stretch_horizontal(ctx, width); let accent = variant.frame; let accent_attach = variant.accent_attach; diff --git a/crates/typst-layout/src/math/attach.rs b/crates/typst-layout/src/math/attach.rs index e1d7d7c9d..bf74cc77a 100644 --- a/crates/typst-layout/src/math/attach.rs +++ b/crates/typst-layout/src/math/attach.rs @@ -1,8 +1,9 @@ use typst_library::diag::SourceResult; use typst_library::foundations::{Packed, StyleChain, SymbolElem}; -use typst_library::layout::{Abs, Axis, Corner, Frame, Point, Rel, Size}; +use typst_library::layout::{Abs, Axis, Corner, Frame, Point, Size}; use typst_library::math::{ AttachElem, EquationElem, LimitsElem, PrimesElem, ScriptsElem, StretchElem, + StretchSize, }; use typst_utils::OptionExt; @@ -63,16 +64,9 @@ pub fn layout_attach( let t = layout!(t, sup_style_chain)?; let b = layout!(b, sub_style_chain)?; if let Some(stretch) = stretch { - let relative_to_width = measure!(t, width).max(measure!(b, width)); - stretch_fragment( - ctx, - styles, - &mut base, - Some(Axis::X), - Some(relative_to_width), - stretch, - Abs::zero(), - ); + let relative_to = measure!(t, width).max(measure!(b, width)); + let width = stretch.resolve(ctx.engine, styles, relative_to)?; + stretch_fragment(ctx, styles, &mut base, Axis::X, width); } let fragments = [ @@ -155,7 +149,7 @@ pub fn layout_limits( } /// Get the size to stretch the base to. -fn stretch_size(styles: StyleChain, elem: &Packed) -> Option> { +fn stretch_size(styles: StyleChain, elem: &Packed) -> Option { // Extract from an EquationElem. let mut base = &elem.base; while let Some(equation) = base.to_packed::() { diff --git a/crates/typst-layout/src/math/frac.rs b/crates/typst-layout/src/math/frac.rs index 6d3caac45..172cdf7ad 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!( @@ -110,12 +109,12 @@ fn layout_frac_like( if binom { let mut left = GlyphFragment::new(ctx, styles, '(', span) - .stretch_vertical(ctx, height, short_fall); + .stretch_vertical(ctx, height - short_fall); left.center_on_axis(ctx); ctx.push(left); ctx.push(FrameFragment::new(styles, frame)); let mut right = GlyphFragment::new(ctx, styles, ')', span) - .stretch_vertical(ctx, height, short_fall); + .stretch_vertical(ctx, height - short_fall); right.center_on_axis(ctx); ctx.push(right); } else { diff --git a/crates/typst-layout/src/math/fragment.rs b/crates/typst-layout/src/math/fragment.rs index 1b508a349..4e5253d02 100644 --- a/crates/typst-layout/src/math/fragment.rs +++ b/crates/typst-layout/src/math/fragment.rs @@ -305,7 +305,7 @@ impl GlyphFragment { } /// Apply GSUB substitutions. - fn adjust_glyph_index(ctx: &MathContext, id: GlyphId) -> GlyphId { + pub fn adjust_glyph_index(ctx: &MathContext, id: GlyphId) -> GlyphId { if let Some(glyphwise_tables) = &ctx.glyphwise_tables { glyphwise_tables.iter().fold(id, |id, table| table.apply(id)) } else { @@ -427,13 +427,8 @@ impl GlyphFragment { } /// Try to stretch a glyph to a desired height. - pub fn stretch_vertical( - self, - ctx: &mut MathContext, - height: Abs, - short_fall: Abs, - ) -> VariantFragment { - stretch_glyph(ctx, self, height, short_fall, Axis::Y) + pub fn stretch_vertical(self, ctx: &mut MathContext, height: Abs) -> VariantFragment { + stretch_glyph(ctx, self, height, Axis::Y) } /// Try to stretch a glyph to a desired width. @@ -441,9 +436,8 @@ impl GlyphFragment { self, ctx: &mut MathContext, width: Abs, - short_fall: Abs, ) -> VariantFragment { - stretch_glyph(ctx, self, width, short_fall, Axis::X) + stretch_glyph(ctx, self, width, Axis::X) } } diff --git a/crates/typst-layout/src/math/lr.rs b/crates/typst-layout/src/math/lr.rs index bf8235411..e2e0eb8ba 100644 --- a/crates/typst-layout/src/math/lr.rs +++ b/crates/typst-layout/src/math/lr.rs @@ -1,11 +1,11 @@ use typst_library::diag::SourceResult; use typst_library::foundations::{Packed, StyleChain}; -use typst_library::layout::{Abs, Axis, Rel}; +use typst_library::layout::{Abs, Axis}; use typst_library::math::{EquationElem, LrElem, MidElem}; use typst_utils::SliceExt; use unicode_math_class::MathClass; -use super::{stretch_fragment, MathContext, MathFragment, DELIM_SHORT_FALL}; +use super::{stretch_fragment, MathContext, MathFragment}; /// Lays out an [`LrElem`]. #[typst_macros::time(name = "math.lr", span = elem.span())] @@ -22,7 +22,7 @@ pub fn layout_lr( // Extract implicit LrElem. if let Some(lr) = body.to_packed::() { - if lr.size(styles).is_one() { + if lr.size(styles).is_lr_default() { body = &lr.body; } } @@ -41,14 +41,14 @@ pub fn layout_lr( .unwrap_or_default(); let relative_to = 2.0 * max_extent; - let height = elem.size(styles); + let height = elem.size(styles).resolve(ctx.engine, styles, relative_to)?; // Scale up fragments at both ends. match inner_fragments { - [one] => scale(ctx, styles, one, relative_to, height, None), + [one] => scale(ctx, styles, one, height, None), [first, .., last] => { - scale(ctx, styles, first, relative_to, height, Some(MathClass::Opening)); - scale(ctx, styles, last, relative_to, height, Some(MathClass::Closing)); + scale(ctx, styles, first, height, Some(MathClass::Opening)); + scale(ctx, styles, last, height, Some(MathClass::Closing)); } _ => {} } @@ -58,7 +58,7 @@ pub fn layout_lr( if let MathFragment::Variant(ref mut variant) = fragment { if variant.mid_stretched == Some(false) { variant.mid_stretched = Some(true); - scale(ctx, styles, fragment, relative_to, height, Some(MathClass::Large)); + scale(ctx, styles, fragment, height, Some(MathClass::Large)); } } } @@ -119,26 +119,14 @@ fn scale( ctx: &mut MathContext, styles: StyleChain, fragment: &mut MathFragment, - relative_to: Abs, - height: Rel, + height: Abs, apply: Option, ) { if matches!( fragment.class(), MathClass::Opening | MathClass::Closing | MathClass::Fence ) { - // This unwrap doesn't really matter. If it is None, then the fragment - // won't be stretchable anyways. - let short_fall = DELIM_SHORT_FALL.at(fragment.font_size().unwrap_or_default()); - stretch_fragment( - ctx, - styles, - fragment, - Some(Axis::Y), - Some(relative_to), - height, - short_fall, - ); + stretch_fragment(ctx, styles, fragment, Axis::Y, height); if let Some(class) = apply { fragment.set_class(class); diff --git a/crates/typst-layout/src/math/mat.rs b/crates/typst-layout/src/math/mat.rs index d678f8658..3d10fde57 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, delimiter_alignment, style_for_denominator, AlignmentResult, - FrameFragment, GlyphFragment, LeftRightAlternator, MathContext, DELIM_SHORT_FALL, + FrameFragment, GlyphFragment, 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(styles); - layout_delimiters(ctx, styles, frame, delim.open(), delim.close(), span) + layout_delimiters( + ctx, + styles, + frame, + elem.delim_size(styles), + delim.open(), + delim.close(), + span, + ) } /// Lays out a [`CasesElem`]. @@ -67,7 +76,7 @@ pub fn layout_cases( let delim = elem.delim(styles); let (open, close) = if elem.reverse(styles) { (None, delim.close()) } else { (delim.open(), None) }; - layout_delimiters(ctx, styles, frame, open, close, span) + layout_delimiters(ctx, styles, frame, elem.delim_size(styles), open, close, span) } /// Lays out a [`MatElem`]. @@ -125,7 +134,15 @@ pub fn layout_mat( )?; let delim = elem.delim(styles); - layout_delimiters(ctx, styles, frame, delim.open(), delim.close(), span) + layout_delimiters( + ctx, + styles, + frame, + elem.delim_size(styles), + delim.open(), + delim.close(), + span, + ) } /// Layout the inner contents of a matrix, vector, or cases. @@ -302,19 +319,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) = left { - let mut left = GlyphFragment::new(ctx, styles, left, span) - .stretch_vertical(ctx, target, short_fall); + let mut left = + GlyphFragment::new(ctx, styles, left, span).stretch_vertical(ctx, target); left.align_on_axis(ctx, delimiter_alignment(left.c)); ctx.push(left); } @@ -322,8 +340,8 @@ fn layout_delimiters( ctx.push(FrameFragment::new(styles, frame)); if let Some(right) = right { - let mut right = GlyphFragment::new(ctx, styles, right, span) - .stretch_vertical(ctx, target, short_fall); + let mut right = + GlyphFragment::new(ctx, styles, right, span).stretch_vertical(ctx, target); right.align_on_axis(ctx, delimiter_alignment(right.c)); ctx.push(right); } diff --git a/crates/typst-layout/src/math/root.rs b/crates/typst-layout/src/math/root.rs index c7f41488e..32f527198 100644 --- a/crates/typst-layout/src/math/root.rs +++ b/crates/typst-layout/src/math/root.rs @@ -50,7 +50,7 @@ pub fn layout_root( // Layout root symbol. let target = radicand.height() + thickness + gap; let sqrt = GlyphFragment::new(ctx, styles, '√', span) - .stretch_vertical(ctx, target, Abs::zero()) + .stretch_vertical(ctx, target) .frame; // Layout the index. diff --git a/crates/typst-layout/src/math/shared.rs b/crates/typst-layout/src/math/shared.rs index 600c130d4..77d1ab4a8 100644 --- a/crates/typst-layout/src/math/shared.rs +++ b/crates/typst-layout/src/math/shared.rs @@ -1,6 +1,6 @@ use ttf_parser::math::MathValue; use typst_library::foundations::{Style, StyleChain}; -use typst_library::layout::{Abs, Em, FixedAlignment, Frame, Point, Size, VAlignment}; +use typst_library::layout::{Abs, FixedAlignment, Frame, Point, Size, VAlignment}; use typst_library::math::{EquationElem, MathSize}; use typst_utils::LazyHash; @@ -28,9 +28,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-layout/src/math/stretch.rs b/crates/typst-layout/src/math/stretch.rs index f45035e27..95ca2b6c2 100644 --- a/crates/typst-layout/src/math/stretch.rs +++ b/crates/typst-layout/src/math/stretch.rs @@ -2,7 +2,7 @@ use ttf_parser::math::{GlyphAssembly, GlyphConstruction, GlyphPart}; use ttf_parser::LazyArray16; use typst_library::diag::{warning, SourceResult}; use typst_library::foundations::{Packed, StyleChain}; -use typst_library::layout::{Abs, Axis, Frame, Point, Rel, Size}; +use typst_library::layout::{Abs, Axis, Frame, Point, Size}; use typst_library::math::StretchElem; use typst_utils::Get; @@ -23,15 +23,13 @@ pub fn layout_stretch( styles: StyleChain, ) -> SourceResult<()> { let mut fragment = ctx.layout_into_fragment(&elem.body, styles)?; - stretch_fragment( - ctx, - styles, - &mut fragment, - None, - None, - elem.size(styles), - Abs::zero(), - ); + + if let Some(axis) = stretch_axis(ctx, &fragment) { + let relative_to = fragment.size().get(axis); + let target = elem.size(styles).resolve(ctx.engine, styles, relative_to)?; + stretch_fragment(ctx, styles, &mut fragment, axis, target); + } + ctx.push(fragment); Ok(()) } @@ -41,10 +39,8 @@ pub fn stretch_fragment( ctx: &mut MathContext, styles: StyleChain, fragment: &mut MathFragment, - axis: Option, - relative_to: Option, - stretch: Rel, - short_fall: Abs, + axis: Axis, + target: Abs, ) { let glyph = match fragment { MathFragment::Glyph(glyph) => glyph.clone(), @@ -56,21 +52,12 @@ pub fn stretch_fragment( // Return if we attempt to stretch along an axis which isn't stretchable, // so that the original fragment isn't modified. - let Some(stretch_axis) = stretch_axis(ctx, &glyph) else { return }; - let axis = axis.unwrap_or(stretch_axis); + let Some(stretch_axis) = stretch_axis(ctx, fragment) else { return }; if axis != stretch_axis { return; } - let relative_to_size = relative_to.unwrap_or_else(|| fragment.size().get(axis)); - - let mut variant = stretch_glyph( - ctx, - glyph, - stretch.relative_to(relative_to_size), - short_fall, - axis, - ); + let mut variant = stretch_glyph(ctx, glyph, target, axis); if axis == Axis::Y { variant.align_on_axis(ctx, delimiter_alignment(variant.c)); @@ -81,17 +68,26 @@ pub fn stretch_fragment( /// Return whether the glyph is stretchable and if it is, along which axis it /// can be stretched. -fn stretch_axis(ctx: &mut MathContext, base: &GlyphFragment) -> Option { - let base_id = base.id; +fn stretch_axis(ctx: &mut MathContext, fragment: &MathFragment) -> Option { + let (id, span) = match fragment { + MathFragment::Glyph(glyph) => (glyph.id, glyph.span), + MathFragment::Variant(variant) => { + let id = ctx.ttf.glyph_index(variant.c).unwrap_or_default(); + let id = GlyphFragment::adjust_glyph_index(ctx, id); + (id, variant.span) + } + _ => return None, + }; + let vertical = ctx .table .variants - .and_then(|variants| variants.vertical_constructions.get(base_id)) + .and_then(|variants| variants.vertical_constructions.get(id)) .map(|_| Axis::Y); let horizontal = ctx .table .variants - .and_then(|variants| variants.horizontal_constructions.get(base_id)) + .and_then(|variants| variants.horizontal_constructions.get(id)) .map(|_| Axis::X); match (vertical, horizontal) { @@ -102,7 +98,7 @@ fn stretch_axis(ctx: &mut MathContext, base: &GlyphFragment) -> Option { // vertical and horizontal constructions. So for the time being, we // will assume that a glyph cannot have both. ctx.engine.sink.warn(warning!( - base.span, + span, "glyph has both vertical and horizontal constructions"; hint: "this is probably a font bug"; hint: "please file an issue at https://github.com/typst/typst/issues" @@ -120,7 +116,6 @@ pub fn stretch_glyph( ctx: &mut MathContext, mut base: GlyphFragment, target: Abs, - short_fall: Abs, axis: Axis, ) -> VariantFragment { // If the base glyph is good enough, use it. @@ -128,8 +123,7 @@ pub fn stretch_glyph( Axis::X => base.width, Axis::Y => base.height(), }; - let short_target = target - short_fall; - if short_target <= advance { + if target <= advance { return base.into_variant(); } @@ -153,13 +147,13 @@ pub fn stretch_glyph( for variant in construction.variants { best_id = variant.variant_glyph; best_advance = base.font.to_em(variant.advance_measurement).at(base.font_size); - if short_target <= best_advance { + if target <= best_advance { break; } } // This is either good or the best we've got. - if short_target <= best_advance || construction.assembly.is_none() { + if target <= best_advance || construction.assembly.is_none() { base.set_id(ctx, best_id); return base.into_variant(); } diff --git a/crates/typst-layout/src/math/text.rs b/crates/typst-layout/src/math/text.rs index 59ac5b089..64d167a76 100644 --- a/crates/typst-layout/src/math/text.rs +++ b/crates/typst-layout/src/math/text.rs @@ -164,7 +164,7 @@ fn layout_glyph( let mut variant = if math_size == MathSize::Display { let height = scaled!(ctx, styles, display_operator_min_height) .max(SQRT_2 * glyph.height()); - glyph.stretch_vertical(ctx, height, Abs::zero()) + glyph.stretch_vertical(ctx, height) } else { glyph.into_variant() }; diff --git a/crates/typst-layout/src/math/underover.rs b/crates/typst-layout/src/math/underover.rs index 5b6bd40eb..a24113c81 100644 --- a/crates/typst-layout/src/math/underover.rs +++ b/crates/typst-layout/src/math/underover.rs @@ -286,7 +286,7 @@ fn layout_underoverspreader( let body_class = body.class(); let body = body.into_fragment(styles); let glyph = GlyphFragment::new(ctx, styles, c, span); - let stretched = glyph.stretch_horizontal(ctx, body.width(), Abs::zero()); + let stretched = glyph.stretch_horizontal(ctx, body.width()); let mut rows = vec![]; let baseline = match position { diff --git a/crates/typst-library/src/math/accent.rs b/crates/typst-library/src/math/accent.rs index e62b63872..a6b7e29a5 100644 --- a/crates/typst-library/src/math/accent.rs +++ b/crates/typst-library/src/math/accent.rs @@ -1,7 +1,19 @@ use crate::diag::bail; -use crate::foundations::{cast, elem, func, Content, NativeElement, SymbolElem}; -use crate::layout::{Length, Rel}; -use crate::math::Mathy; +use crate::foundations::{ + cast, elem, func, Content, NativeElement, NativeFunc, SymbolElem, +}; +use crate::layout::{Em, Length, Ratio, Rel}; +use crate::math::{Mathy, StretchSize}; + +const ACCENT_SHORT_FALL: Em = Em::new(-0.5); + +#[func(name = "x => x - 0.5em")] +const fn default_accent_size(base: Length) -> Rel { + Rel { + rel: Ratio::zero(), + abs: Length { abs: base.abs, em: ACCENT_SHORT_FALL }, + } +} /// Attaches an accent to a base. /// @@ -52,12 +64,14 @@ pub struct AccentElem { /// The size of the accent, relative to the width of the base. /// + /// See the [stretch documentation]($math.stretch.size) for more + /// information on sizes. + /// /// ```example /// $dash(A, size: #150%)$ /// ``` - #[resolve] - #[default(Rel::one())] - pub size: Rel, + #[default(::data().into())] + pub size: StretchSize, /// Whether to remove the dot on top of lowercase i and j when adding a top /// accent. @@ -116,8 +130,11 @@ macro_rules! accents { /// The base to which the accent is applied. base: Content, /// The size of the accent, relative to the width of the base. + /// + /// See the [stretch documentation]($math.stretch.size) for + /// more information on sizes. #[named] - size: Option>, + size: Option, /// Whether to remove the dot on top of lowercase i and j when /// adding a top accent. #[named] diff --git a/crates/typst-library/src/math/attach.rs b/crates/typst-library/src/math/attach.rs index d526aba57..c367b6f4f 100644 --- a/crates/typst-library/src/math/attach.rs +++ b/crates/typst-library/src/math/attach.rs @@ -1,5 +1,4 @@ use crate::foundations::{elem, Content, Packed}; -use crate::layout::{Length, Rel}; use crate::math::{EquationElem, Mathy}; /// A base with optional attachments. @@ -128,31 +127,3 @@ pub struct LimitsElem { #[default(true)] pub inline: bool, } - -/// Stretches a glyph. -/// -/// This function can also be used to automatically stretch the base of an -/// attachment, so that it fits the top and bottom attachments. -/// -/// Note that only some glyphs can be stretched, and which ones can depend on -/// the math font being used. However, most math fonts are the same in this -/// regard. -/// -/// ```example -/// $ H stretch(=)^"define" U + p V $ -/// $ f : X stretch(->>, size: #150%)_"surjective" Y $ -/// $ x stretch(harpoons.ltrb, size: #3em) y -/// stretch(\[, size: #150%) z $ -/// ``` -#[elem(Mathy)] -pub struct StretchElem { - /// The glyph to stretch. - #[required] - pub body: Content, - - /// The size to stretch to, relative to the maximum size of the glyph and - /// its attachments. - #[resolve] - #[default(Rel::one())] - pub size: Rel, -} diff --git a/crates/typst-library/src/math/lr.rs b/crates/typst-library/src/math/lr.rs index 7558717af..34745bd24 100644 --- a/crates/typst-library/src/math/lr.rs +++ b/crates/typst-library/src/math/lr.rs @@ -1,6 +1,16 @@ -use crate::foundations::{elem, func, Content, NativeElement, SymbolElem}; -use crate::layout::{Length, Rel}; -use crate::math::Mathy; +use crate::foundations::{elem, func, Content, NativeElement, NativeFunc, SymbolElem}; +use crate::layout::{Em, Length, Ratio, Rel}; +use crate::math::{Mathy, StretchSize}; + +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 { + Rel { + rel: Ratio::zero(), + abs: Length { abs: base.abs, em: DELIM_SHORT_FALL }, + } +} /// Scales delimiters. /// @@ -8,10 +18,13 @@ use crate::math::Mathy; /// unmatched delimiters and to control the delimiter scaling more precisely. #[elem(title = "Left/Right", Mathy)] pub struct LrElem { - /// The size of the brackets, relative to the height of the wrapped content. - #[resolve] - #[default(Rel::one())] - pub size: Rel, + /// The size of the delimiters, relative to the height of the wrapped + /// content. + /// + /// See the [stretch documentation]($math.stretch.size) for more + /// information on sizes. + #[default(::data().into())] + pub size: StretchSize, /// The delimited content, including the delimiters. #[required] @@ -43,9 +56,13 @@ pub struct MidElem { /// ``` #[func] pub fn floor( - /// The size of the brackets, relative to the height of the wrapped content. + /// The size of the delimiters, relative to the height of the wrapped + /// content. + /// + /// See the [stretch documentation]($math.stretch.size) for more + /// information on sizes. #[named] - size: Option>, + size: Option, /// The expression to floor. body: Content, ) -> Content { @@ -59,9 +76,13 @@ pub fn floor( /// ``` #[func] pub fn ceil( - /// The size of the brackets, relative to the height of the wrapped content. + /// The size of the delimiters, relative to the height of the wrapped + /// content. + /// + /// See the [stretch documentation]($math.stretch.size) for more + /// information on sizes. #[named] - size: Option>, + size: Option, /// The expression to ceil. body: Content, ) -> Content { @@ -75,9 +96,13 @@ pub fn ceil( /// ``` #[func] pub fn round( - /// The size of the brackets, relative to the height of the wrapped content. + /// The size of the delimiters, relative to the height of the wrapped + /// content. + /// + /// See the [stretch documentation]($math.stretch.size) for more + /// information on sizes. #[named] - size: Option>, + size: Option, /// The expression to round. body: Content, ) -> Content { @@ -91,9 +116,13 @@ pub fn round( /// ``` #[func] pub fn abs( - /// The size of the brackets, relative to the height of the wrapped content. + /// The size of the delimiters, relative to the height of the wrapped + /// content. + /// + /// See the [stretch documentation]($math.stretch.size) for more + /// information on sizes. #[named] - size: Option>, + size: Option, /// The expression to take the absolute value of. body: Content, ) -> Content { @@ -107,9 +136,13 @@ pub fn abs( /// ``` #[func] pub fn norm( - /// The size of the brackets, relative to the height of the wrapped content. + /// The size of the delimiters, relative to the height of the wrapped + /// content. + /// + /// See the [stretch documentation]($math.stretch.size) for more + /// information on sizes. #[named] - size: Option>, + size: Option, /// The expression to take the norm of. body: Content, ) -> Content { @@ -120,7 +153,7 @@ fn delimited( body: Content, left: char, right: char, - size: Option>, + size: Option, ) -> Content { let span = body.span(); let mut elem = LrElem::new(Content::sequence([ diff --git a/crates/typst-library/src/math/matrix.rs b/crates/typst-library/src/math/matrix.rs index b6c4654ed..8ab19040c 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 @@ -101,6 +120,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 @@ -244,6 +270,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/crates/typst-library/src/math/mod.rs b/crates/typst-library/src/math/mod.rs index 2e6d42b13..4f4bbb03b 100644 --- a/crates/typst-library/src/math/mod.rs +++ b/crates/typst-library/src/math/mod.rs @@ -9,6 +9,7 @@ mod lr; mod matrix; mod op; mod root; +mod stretch; mod style; mod underover; @@ -21,6 +22,7 @@ pub use self::lr::*; pub use self::matrix::*; pub use self::op::*; pub use self::root::*; +pub use self::stretch::*; pub use self::style::*; pub use self::underover::*; diff --git a/crates/typst-library/src/math/stretch.rs b/crates/typst-library/src/math/stretch.rs new file mode 100644 index 000000000..899234fa9 --- /dev/null +++ b/crates/typst-library/src/math/stretch.rs @@ -0,0 +1,125 @@ +use comemo::Track; + +use crate::diag::{At, SourceResult}; +use crate::engine::Engine; +use crate::foundations::{ + cast, elem, Content, Context, Func, NativeFunc, NativeFuncData, Resolve, StyleChain, +}; +use crate::layout::{Abs, Rel}; +use crate::math::{default_lr_size, Mathy}; + +/// Stretches a glyph. +/// +/// This function can also be used to automatically stretch the base of an +/// attachment, so that it fits the top and bottom attachments. +/// +/// Note that only some glyphs can be stretched, and which ones can depend on +/// the math font being used. However, most math fonts are the same in this +/// regard. +/// +/// ```example +/// $ H stretch(=)^"define" U + p V $ +/// $ f : X stretch(->>, size: #150%)_"surjective" Y $ +/// $ x stretch(harpoons.ltrb, size: #3em) y +/// stretch(\[, size: #150%) z $ +/// ``` +#[elem(Mathy)] +pub struct StretchElem { + /// The glyph to stretch. + #[required] + pub body: Content, + + /// The size to stretch to, relative to the maximum size of the glyph and + /// its attachments. + /// + /// This value can be given as a [relative length]($relative), or a + /// [function]($function) that receives the size of the glyph as a + /// parameter (an absolute length) and should return a (relative) length. + /// For example, `{x => x * 80%}` would be equivalent to just specifying `{80%}`. + /// + /// Note that the sizes of glyphs in math fonts come about in two ways: + /// + /// - First, there are pre-made variants at specific sizes. This means you + /// will see discrete jumps in the stretched glyph's size as you increase + /// the size parameter. It is up to the font how many pre-made variants + /// there are and what their sizes are. + /// + /// - Then, if the pre-made variants are all too small, a glyph of the + /// desired size is assembled from parts. The stretched glyph's size will + /// now be the exact size requested. + /// + /// It could be the case that only one of the above exist for a glyph in + /// the font. + /// + /// The value given here is really a minimum (but if there is no assembly + /// for the glyph, this minimum may not be reached), so the actual size of + /// the stretched glyph may not match what you specified. + /// + /// ```example + /// #for i in range(0, 15) { + /// $stretch(\[, size: #(10pt + i * 2pt))$ + /// } + /// + /// #set math.stretch(size: x => x + 0.5em) + /// $x stretch(=)^"def" y$ + /// ``` + #[default(Rel::one().into())] + pub size: StretchSize, +} + +/// How to size a stretched glyph. +#[derive(Debug, Clone, PartialEq, Hash)] +pub enum StretchSize { + /// Sized by the specified length. + Rel(Rel), + /// Resolve the size for the given base size through the specified + /// function. + Func(Func), +} + +impl StretchSize { + /// Resolve the stretch size given the base size. + pub fn resolve( + &self, + engine: &mut Engine, + styles: StyleChain, + base: Abs, + ) -> SourceResult { + Ok(match self { + Self::Rel(rel) => *rel, + Self::Func(func) => func + .call(engine, Context::new(None, Some(styles)).track(), [base])? + .cast() + .at(func.span())?, + } + .resolve(styles) + .relative_to(base)) + } + + /// Whether the size is the default used by `LrElem`. + pub fn is_lr_default(self) -> bool { + self == ::data().into() + } +} + +impl From for StretchSize { + fn from(rel: Rel) -> Self { + Self::Rel(rel) + } +} + +impl From<&'static NativeFuncData> for StretchSize { + fn from(data: &'static NativeFuncData) -> Self { + Self::Func(Func::from(data)) + } +} + +cast! { + StretchSize, + self => match self { + Self::Rel(v) => v.into_value(), + Self::Func(v) => v.into_value(), + }, + v: Rel => Self::Rel(v), + v: Func => Self::Func(v), +} diff --git a/tests/ref/gradient-math-conic.png b/tests/ref/gradient-math-conic.png index ffd3e8068..9bac6c3d4 100644 Binary files a/tests/ref/gradient-math-conic.png and b/tests/ref/gradient-math-conic.png differ diff --git a/tests/ref/gradient-math-dir.png b/tests/ref/gradient-math-dir.png index 8d33f51f5..c2f5bcef5 100644 Binary files a/tests/ref/gradient-math-dir.png and b/tests/ref/gradient-math-dir.png differ diff --git a/tests/ref/gradient-math-mat.png b/tests/ref/gradient-math-mat.png index ecf953038..d003d6d03 100644 Binary files a/tests/ref/gradient-math-mat.png and b/tests/ref/gradient-math-mat.png differ diff --git a/tests/ref/gradient-math-misc.png b/tests/ref/gradient-math-misc.png index 13f5c27b3..419481384 100644 Binary files a/tests/ref/gradient-math-misc.png and b/tests/ref/gradient-math-misc.png differ diff --git a/tests/ref/gradient-math-radial.png b/tests/ref/gradient-math-radial.png index 8d0047bbe..97fb17e6f 100644 Binary files a/tests/ref/gradient-math-radial.png and b/tests/ref/gradient-math-radial.png differ diff --git a/tests/ref/issue-1617-mat-align.png b/tests/ref/issue-1617-mat-align.png index bd4ea16fe..73d8ae824 100644 Binary files a/tests/ref/issue-1617-mat-align.png and b/tests/ref/issue-1617-mat-align.png differ diff --git a/tests/ref/issue-3774-math-call-empty-2d-args.png b/tests/ref/issue-3774-math-call-empty-2d-args.png index c1bf52d00..52472d8db 100644 Binary files a/tests/ref/issue-3774-math-call-empty-2d-args.png and b/tests/ref/issue-3774-math-call-empty-2d-args.png differ diff --git a/tests/ref/math-accent-sized-function.png b/tests/ref/math-accent-sized-function.png new file mode 100644 index 000000000..9bd52a6e7 Binary files /dev/null and b/tests/ref/math-accent-sized-function.png differ diff --git a/tests/ref/math-accent-sized-script.png b/tests/ref/math-accent-sized-script.png index cf468dd15..ae5e62211 100644 Binary files a/tests/ref/math-accent-sized-script.png and b/tests/ref/math-accent-sized-script.png differ diff --git a/tests/ref/math-accent-wide-base.png b/tests/ref/math-accent-wide-base.png index af716bf45..793ab30bd 100644 Binary files a/tests/ref/math-accent-wide-base.png and b/tests/ref/math-accent-wide-base.png differ diff --git a/tests/ref/math-attach-scripts-extended-shapes.png b/tests/ref/math-attach-scripts-extended-shapes.png index c99d3c7fd..16bad1157 100644 Binary files a/tests/ref/math-attach-scripts-extended-shapes.png and b/tests/ref/math-attach-scripts-extended-shapes.png differ diff --git a/tests/ref/math-cases-delim-size.png b/tests/ref/math-cases-delim-size.png new file mode 100644 index 000000000..92231d691 Binary files /dev/null and b/tests/ref/math-cases-delim-size.png differ diff --git a/tests/ref/math-cases-gap.png b/tests/ref/math-cases-gap.png index 746572fac..6bd8e2055 100644 Binary files a/tests/ref/math-cases-gap.png and b/tests/ref/math-cases-gap.png differ diff --git a/tests/ref/math-cases-linebreaks.png b/tests/ref/math-cases-linebreaks.png index eb4971c46..65b4e4025 100644 Binary files a/tests/ref/math-cases-linebreaks.png and b/tests/ref/math-cases-linebreaks.png differ diff --git a/tests/ref/math-cases.png b/tests/ref/math-cases.png index ed0423def..34567837d 100644 Binary files a/tests/ref/math-cases.png and b/tests/ref/math-cases.png differ diff --git a/tests/ref/math-lr-mid-size-nested-equation.png b/tests/ref/math-lr-mid-size-nested-equation.png index df0106689..5fd22ae34 100644 Binary files a/tests/ref/math-lr-mid-size-nested-equation.png and b/tests/ref/math-lr-mid-size-nested-equation.png differ diff --git a/tests/ref/math-lr-mid-size.png b/tests/ref/math-lr-mid-size.png index 12b4c0868..c0e191a15 100644 Binary files a/tests/ref/math-lr-mid-size.png and b/tests/ref/math-lr-mid-size.png differ diff --git a/tests/ref/math-lr-size-function.png b/tests/ref/math-lr-size-function.png new file mode 100644 index 000000000..437bed046 Binary files /dev/null and b/tests/ref/math-lr-size-function.png differ diff --git a/tests/ref/math-lr-size.png b/tests/ref/math-lr-size.png index 09d244215..f3259ac3c 100644 Binary files a/tests/ref/math-lr-size.png and b/tests/ref/math-lr-size.png differ diff --git a/tests/ref/math-mat-align-explicit-alternating.png b/tests/ref/math-mat-align-explicit-alternating.png index 1ebcc7b68..52a51378b 100644 Binary files a/tests/ref/math-mat-align-explicit-alternating.png and b/tests/ref/math-mat-align-explicit-alternating.png differ diff --git a/tests/ref/math-mat-align-explicit-left.png b/tests/ref/math-mat-align-explicit-left.png index cb9819248..09c5cb3d4 100644 Binary files a/tests/ref/math-mat-align-explicit-left.png and b/tests/ref/math-mat-align-explicit-left.png differ diff --git a/tests/ref/math-mat-align-explicit-mixed.png b/tests/ref/math-mat-align-explicit-mixed.png index 88ccd6de7..f2b38b3eb 100644 Binary files a/tests/ref/math-mat-align-explicit-mixed.png and b/tests/ref/math-mat-align-explicit-mixed.png differ diff --git a/tests/ref/math-mat-align-explicit-right.png b/tests/ref/math-mat-align-explicit-right.png index b537e6571..5db5378f9 100644 Binary files a/tests/ref/math-mat-align-explicit-right.png and b/tests/ref/math-mat-align-explicit-right.png differ diff --git a/tests/ref/math-mat-align-implicit.png b/tests/ref/math-mat-align-implicit.png index b184d9140..cd6833155 100644 Binary files a/tests/ref/math-mat-align-implicit.png and b/tests/ref/math-mat-align-implicit.png differ diff --git a/tests/ref/math-mat-align-signed-numbers.png b/tests/ref/math-mat-align-signed-numbers.png index c92743797..4463e2fb1 100644 Binary files a/tests/ref/math-mat-align-signed-numbers.png and b/tests/ref/math-mat-align-signed-numbers.png differ diff --git a/tests/ref/math-mat-align.png b/tests/ref/math-mat-align.png index 66513dd53..eaa3233d8 100644 Binary files a/tests/ref/math-mat-align.png and b/tests/ref/math-mat-align.png differ diff --git a/tests/ref/math-mat-augment-set.png b/tests/ref/math-mat-augment-set.png index c5881b139..1a6676159 100644 Binary files a/tests/ref/math-mat-augment-set.png and b/tests/ref/math-mat-augment-set.png differ diff --git a/tests/ref/math-mat-augment.png b/tests/ref/math-mat-augment.png index 0e2a42a24..306c4b199 100644 Binary files a/tests/ref/math-mat-augment.png and b/tests/ref/math-mat-augment.png differ diff --git a/tests/ref/math-mat-baseline.png b/tests/ref/math-mat-baseline.png index d2f266213..01928f724 100644 Binary files a/tests/ref/math-mat-baseline.png and b/tests/ref/math-mat-baseline.png differ diff --git a/tests/ref/math-mat-delim-size.png b/tests/ref/math-mat-delim-size.png new file mode 100644 index 000000000..0d60d86f6 Binary files /dev/null and b/tests/ref/math-mat-delim-size.png differ diff --git a/tests/ref/math-mat-gap.png b/tests/ref/math-mat-gap.png index e4f87b59b..90525ff2e 100644 Binary files a/tests/ref/math-mat-gap.png and b/tests/ref/math-mat-gap.png differ diff --git a/tests/ref/math-mat-gaps.png b/tests/ref/math-mat-gaps.png index 405358776..95cd6cf11 100644 Binary files a/tests/ref/math-mat-gaps.png and b/tests/ref/math-mat-gaps.png differ diff --git a/tests/ref/math-mat-linebreaks.png b/tests/ref/math-mat-linebreaks.png index 52ff0a8bb..6666749da 100644 Binary files a/tests/ref/math-mat-linebreaks.png and b/tests/ref/math-mat-linebreaks.png differ diff --git a/tests/ref/math-mat-sparse.png b/tests/ref/math-mat-sparse.png index e9f0d948c..c255fe3e5 100644 Binary files a/tests/ref/math-mat-sparse.png and b/tests/ref/math-mat-sparse.png differ diff --git a/tests/ref/math-mat-spread.png b/tests/ref/math-mat-spread.png index dc8b2bf7e..b8f539cca 100644 Binary files a/tests/ref/math-mat-spread.png and b/tests/ref/math-mat-spread.png differ diff --git a/tests/ref/math-shorthands.png b/tests/ref/math-shorthands.png index 65b35acad..19bdb6be5 100644 Binary files a/tests/ref/math-shorthands.png and b/tests/ref/math-shorthands.png differ diff --git a/tests/ref/math-stretch-function.png b/tests/ref/math-stretch-function.png new file mode 100644 index 000000000..8e1daa46c Binary files /dev/null and b/tests/ref/math-stretch-function.png differ diff --git a/tests/ref/math-vec-align-explicit-alternating.png b/tests/ref/math-vec-align-explicit-alternating.png index 1ebcc7b68..52a51378b 100644 Binary files a/tests/ref/math-vec-align-explicit-alternating.png and b/tests/ref/math-vec-align-explicit-alternating.png differ diff --git a/tests/ref/math-vec-align.png b/tests/ref/math-vec-align.png index 680d0936d..07d58df72 100644 Binary files a/tests/ref/math-vec-align.png and b/tests/ref/math-vec-align.png differ diff --git a/tests/ref/math-vec-delim-size.png b/tests/ref/math-vec-delim-size.png new file mode 100644 index 000000000..03b0d2a4a Binary files /dev/null and b/tests/ref/math-vec-delim-size.png differ diff --git a/tests/ref/math-vec-gap.png b/tests/ref/math-vec-gap.png index e48b3e902..ccfb21711 100644 Binary files a/tests/ref/math-vec-gap.png and b/tests/ref/math-vec-gap.png differ diff --git a/tests/ref/math-vec-linebreaks.png b/tests/ref/math-vec-linebreaks.png index 52ff0a8bb..6666749da 100644 Binary files a/tests/ref/math-vec-linebreaks.png and b/tests/ref/math-vec-linebreaks.png differ diff --git a/tests/ref/math-vec-wide.png b/tests/ref/math-vec-wide.png index 9dc887a8c..000e3cf2a 100644 Binary files a/tests/ref/math-vec-wide.png and b/tests/ref/math-vec-wide.png differ diff --git a/tests/suite/math/accent.typ b/tests/suite/math/accent.typ index ecc0588c5..23e11a697 100644 --- a/tests/suite/math/accent.typ +++ b/tests/suite/math/accent.typ @@ -30,12 +30,18 @@ $ tilde(integral), tilde(integral)_a^b, tilde(integral_a^b) $ --- math-accent-sized --- // Test accent size. -$tilde(sum), tilde(sum, size: #50%), accent(H, hat, size: #200%)$ +$tilde(sum), tilde(sum, size: #25%), accent(H, hat, size: #125%)$ --- math-accent-sized-script --- // Test accent size in script size. $tilde(U, size: #1.1em), x^tilde(U, size: #1.1em), sscript(tilde(U, size: #1.1em))$ +--- math-accent-sized-function --- +// Test accent size with a function. +$dash(A) arrow(I) hat(L)$ \ +#set math.accent(size: x => x - 0.1em) +$dash(A) arrow(I) hat(L)$ + --- math-accent-dotless --- // Test dotless glyph variants. #let test(c) = $grave(#c), acute(sans(#c)), hat(frak(#c)), tilde(mono(#c)), 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/delimited.typ b/tests/suite/math/delimited.typ index 794ffd8aa..06e3e0d20 100644 --- a/tests/suite/math/delimited.typ +++ b/tests/suite/math/delimited.typ @@ -34,6 +34,11 @@ $ lr(a/b\]) = a = lr(\{a/b) $ $ lr(]sum_(x=1)^n x], size: #70%) < lr((1, 2), size: #200%) $ +--- math-lr-size-function --- +// Test using a function as an argument to size. +#set math.lr(size: x => if x > 10pt { 1em } else { 4 * x }) +$ (a) (1/2) $ + --- math-lr-shorthands --- // Test predefined delimiter pairings. $floor(x/2), ceil(x/2), abs(x), norm(x)$ 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/stretch.typ b/tests/suite/math/stretch.typ index d145f72a1..f5ee05af6 100644 --- a/tests/suite/math/stretch.typ +++ b/tests/suite/math/stretch.typ @@ -91,3 +91,9 @@ $ body^"text" $ } $body^"long text"$ } + +--- math-stretch-function --- +// Test using a function as an argument to size. +$stretch(<-, size: #(x => x - 0.5em))_"function"$ +#set math.stretch(size: x => x + 0.5em) +$stretch(|) |$ 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