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 commit is contained in:
mkorje 2025-04-25 22:36:21 +10:00
parent 24bac30c84
commit 5738c98ff5
No known key found for this signature in database
4 changed files with 36 additions and 18 deletions

View File

@ -1,6 +1,6 @@
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::{Frame, Point, Size};
use typst_library::math::AccentElem; use typst_library::math::AccentElem;
use super::{ use super::{
@ -8,9 +8,6 @@ use super::{
style_flac, style_flac,
}; };
/// 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(
@ -45,11 +42,8 @@ pub fn layout_accent(
let mut glyph = let mut glyph =
GlyphFragment::new_char(ctx.font, accent_styles, accent.0, elem.span())?; GlyphFragment::new_char(ctx.font, accent_styles, accent.0, elem.span())?;
// Forcing the accent to be at least as large as the base makes it too wide let width = elem.size.get_ref(styles).resolve(ctx.engine, styles, base.width())?;
// in many cases. glyph.stretch_horizontal(ctx, width);
let width = elem.size.resolve(styles).relative_to(base.width());
let short_fall = ACCENT_SHORT_FALL.at(glyph.item.size);
glyph.stretch_horizontal(ctx, width - short_fall);
let accent_attach = glyph.accent_attach.0; let accent_attach = glyph.accent_attach.0;
let accent = glyph.into_frame(); let accent = glyph.into_frame();

View File

@ -6,9 +6,21 @@ use icu_provider::AsDeserializingBufferProvider;
use icu_provider_blob::BlobDataProvider; use icu_provider_blob::BlobDataProvider;
use crate::diag::bail; use crate::diag::bail;
use crate::foundations::{Content, NativeElement, SymbolElem, cast, elem, func}; use crate::foundations::{
use crate::layout::{Length, Rel}; Content, NativeElement, NativeFunc, SymbolElem, cast, elem, func,
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.
/// ///
@ -59,11 +71,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%)$
/// ``` /// ```
#[default(Rel::one())] #[default(<default_accent_size>::data().into())]
pub size: Rel<Length>, pub size: StretchSize,
/// 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.
@ -140,8 +155,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

View File

@ -30,11 +30,17 @@ $ 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: #0.6em), x^tilde(U, size: #0.6em), sscript(tilde(U, size: #0.6em))$
--- 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.
@ -85,7 +91,7 @@ $ accent(integral, \u{20EC}), accent(integral, \u{20EC})_a^b, accent(integral_a^
--- math-accent-bottom-sized --- --- math-accent-bottom-sized ---
// Test bottom accent size. // Test bottom accent size.
$accent(sum, \u{0330}), accent(sum, \u{0330}, size: #50%), accent(H, \u{032D}, size: #200%)$ $accent(sum, \u{0330}), accent(sum, \u{0330}, size: #25%), accent(H, \u{032D}, size: #125%)$
--- math-accent-nested --- --- math-accent-nested ---
// Test nested top and bottom accents. // Test nested top and bottom accents.