Allow a function as an argument to size in accent

The short fall is now only applied in the default for `accent`
(`x => x - 0.5em`). This is the reason for the updated tests.
This commit is contained in:
mkorje 2025-04-25 22:36:21 +10:00
parent 3506ef446d
commit 92d06fbd9a
No known key found for this signature in database
5 changed files with 34 additions and 15 deletions

View File

@ -1,13 +1,10 @@
use typst_library::diag::SourceResult; use typst_library::diag::SourceResult;
use typst_library::foundations::{Packed, StyleChain}; use typst_library::foundations::{Packed, StyleChain};
use typst_library::layout::{Em, Frame, Point, Size}; use typst_library::layout::{Abs, Frame, Point, Size};
use typst_library::math::{Accent, AccentElem}; use typst_library::math::{Accent, AccentElem};
use super::{style_cramped, FrameFragment, GlyphFragment, MathContext, MathFragment}; 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`]. /// Lays out an [`AccentElem`].
#[typst_macros::time(name = "math.accent", span = elem.span())] #[typst_macros::time(name = "math.accent", span = elem.span())]
pub fn layout_accent( pub fn layout_accent(
@ -29,7 +26,7 @@ pub fn layout_accent(
let base_class = base.class(); let base_class = base.class();
let base_attach = base.accent_attach(); 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 Accent(c) = elem.accent;
let mut glyph = GlyphFragment::new(ctx, styles, c, elem.span()); 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 // Forcing the accent to be at least as large as the base makes it too
// wide in many case. // wide in many case.
let short_fall = ACCENT_SHORT_FALL.at(glyph.font_size); let variant = glyph.stretch_horizontal(ctx, width, Abs::zero());
let variant = glyph.stretch_horizontal(ctx, width, short_fall);
let accent = variant.frame; let accent = variant.frame;
let accent_attach = variant.accent_attach; let accent_attach = variant.accent_attach;

View File

@ -1,7 +1,19 @@
use crate::diag::bail; use crate::diag::bail;
use crate::foundations::{cast, elem, func, Content, NativeElement, SymbolElem}; use crate::foundations::{
use crate::layout::{Length, Rel}; cast, elem, func, Content, NativeElement, NativeFunc, SymbolElem,
use crate::math::Mathy; };
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. /// 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. /// 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 /// ```example
/// $dash(A, size: #150%)$ /// $dash(A, size: #150%)$
/// ``` /// ```
#[resolve] #[default(<default_accent_size>::data().into())]
#[default(Rel::one())] pub size: StretchSize,
pub size: Rel<Length>,
/// Whether to remove the dot on top of lowercase i and j when adding a top /// Whether to remove the dot on top of lowercase i and j when adding a top
/// accent. /// accent.
@ -116,8 +130,11 @@ macro_rules! accents {
/// The base to which the accent is applied. /// The base to which the accent is applied.
base: Content, base: Content,
/// The size of the accent, relative to the width of the base. /// 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] #[named]
size: Option<Rel<Length>>, size: Option<StretchSize>,
/// Whether to remove the dot on top of lowercase i and j when /// Whether to remove the dot on top of lowercase i and j when
/// adding a top accent. /// adding a top accent.
#[named] #[named]

Binary file not shown.

After

Width:  |  Height:  |  Size: 417 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 331 B

After

Width:  |  Height:  |  Size: 355 B

View File

@ -30,12 +30,18 @@ $ tilde(integral), tilde(integral)_a^b, tilde(integral_a^b) $
--- math-accent-sized --- --- math-accent-sized ---
// Test accent size. // 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 --- --- 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-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 --- --- math-accent-dotless ---
// Test dotless glyph variants. // Test dotless glyph variants.
#let test(c) = $grave(#c), acute(sans(#c)), hat(frak(#c)), tilde(mono(#c)), #let test(c) = $grave(#c), acute(sans(#c)), hat(frak(#c)), tilde(mono(#c)),