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 08f1d93f9e
commit ba3f525624
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::foundations::{Packed, StyleChain};
use typst_library::layout::{Em, Frame, Point, Size};
use typst_library::layout::{Frame, Point, Size};
use typst_library::math::AccentElem;
use super::{
@ -8,9 +8,6 @@ use super::{
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(
@ -45,11 +42,8 @@ pub fn layout_accent(
let mut glyph =
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
// in many cases.
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 width = elem.size.get_ref(styles).resolve(ctx.engine, styles, base.width())?;
glyph.stretch_horizontal(ctx, width);
let accent_attach = glyph.accent_attach.0;
let accent = glyph.into_frame();

View File

@ -6,9 +6,21 @@ use icu_provider::AsDeserializingBufferProvider;
use icu_provider_blob::BlobDataProvider;
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.
///
@ -59,11 +71,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%)$
/// ```
#[default(Rel::one())]
pub size: Rel<Length>,
#[default(<default_accent_size>::data().into())]
pub size: StretchSize,
/// Whether to remove the dot on top of lowercase i and j when adding a top
/// accent.
@ -140,8 +155,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<Rel<Length>>,
size: Option<StretchSize>,
/// Whether to remove the dot on top of lowercase i and j when
/// adding a top accent.
#[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 ---
// 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))$
$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 ---
// 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 ---
// 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 ---
// Test nested top and bottom accents.