Repository: typst/typst
File: crates/typst-layout/src/math/accent.rs (Rust)

Code before:
use typst_library::foundations::{Packed, StyleChain};
use typst_library::layout::{Em, Frame, Point, Rel, Size};
use typst_library::math::{Accent, AccentElem};

use super::{
    scaled_font_size, 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(
    elem: &Packed<AccentElem>,
    ctx: &mut MathContext,
    styles: StyleChain,
) -> SourceResult<()> {
    let cramped = style_cramped();
    let mut base = ctx.layout_into_fragment(elem.base(), styles.chain(&cramped))?;

    // Try replace a glyph with its dotless variant.
    if let MathFragment::Glyph(ref mut glyph) = &mut base {
        glyph.make_dotless_form(ctx)
    }

    // Preserve class to preserve automatic spacing.
    let base_class = base.class();
    let base_attach = base.accent_attach();

    let width = elem
        .size(styles)
        .unwrap_or(Rel::one())
        .at(scaled_font_size(ctx, styles))
        .relative_to(base.width());

    let Accent(c) = elem.accent();
    let mut glyph = GlyphFragment::new(ctx, styles, *c, elem.span());

    // Try replace accent glyph with flattened variant.
    let flattened_base_height = scaled!(ctx, styles, flattened_accent_base_height);
    if base.height() > flattened_base_height {
        glyph.make_flattened_accent_form(ctx)
    }

    // 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 accent = variant.frame;

Review comment (suggestion):
```suggestion
    // Try to replace a glyph with its dotless variant.
    if let MathFragment::Glyph(glyph) = &mut base {
        glyph.make_dotless_form(ctx);
    }
```

Code after:
use typst_library::foundations::{Packed, StyleChain};
use typst_library::layout::{Em, Frame, Point, Rel, Size};
use typst_library::math::{Accent, AccentElem};

use super::{
    scaled_font_size, 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(
    elem: &Packed<AccentElem>,
    ctx: &mut MathContext,
    styles: StyleChain,
) -> SourceResult<()> {
    let cramped = style_cramped();
    let mut base = ctx.layout_into_fragment(elem.base(), styles.chain(&cramped))?;

    // Try to replace a glyph with its dotless variant.
    if let MathFragment::Glyph(glyph) = &mut base {
        glyph.make_dotless_form(ctx);
    }

    // Preserve class to preserve automatic spacing.
    let base_class = base.class();
    let base_attach = base.accent_attach();

    let width = elem
        .size(styles)
        .unwrap_or(Rel::one())
        .at(scaled_font_size(ctx, styles))
        .relative_to(base.width());

    let Accent(c) = elem.accent();
    let mut glyph = GlyphFragment::new(ctx, styles, *c, elem.span());

    // Try to replace accent glyph with flattened variant.
    let flattened_base_height = scaled!(ctx, styles, flattened_accent_base_height);
    if base.height() > flattened_base_height {
        glyph.make_flattened_accent_form(ctx);
    }

    // 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 accent = variant.frame;