Repository: typst/typst
File: crates/typst/src/math/stretch.rs (Rust)

Code before:
        stretch
            .unwrap_or(Rel::one())
            .resolve(styles)
            .relative_to(relative_to_size),
        short_fall,
        axis,
    );

    if axis == Axis::Y {
        variant.align_on_axis(ctx, delimiter_alignment(variant.c));
    }

    *fragment = MathFragment::Variant(variant);
}

pub(super) fn delimiter_alignment(delimiter: char) -> VAlignment {
    match delimiter {
        '\u{231c}' | '\u{231d}' => VAlignment::Top,
        '\u{231e}' | '\u{231f}' => VAlignment::Bottom,
        _ => VAlignment::Horizon,
    }
}

/// Return whether the glyph is stretchable and if it is, along which axis it
/// can be stretched.
fn is_stretchable(ctx: &MathContext, base: &GlyphFragment) -> Option<Axis> {
    let base_id = base.id;
    let vertical = ctx
        .table
        .variants
        .and_then(|variants| variants.vertical_constructions.get(base_id))
        .map(|_| Axis::Y);
    let horizontal = ctx
        .table
        .variants
        .and_then(|variants| variants.horizontal_constructions.get(base_id))
        .map(|_| Axis::X);

    match (vertical, horizontal) {
        (vertical, None) => vertical,
        (None, horizontal) => horizontal,
        _ => {
            // As far as we know, there aren't any glyphs that have both
            // vertical and horizontal constructions. So for the time being, we
            // will assume that a glyph cannot have both.
            panic!("glyph {:?} has both vertical and horizontal constructions", base.c);
        }
    }
}

impl GlyphFragment {

Review comment (suggestion):
I think the name should be changed since it doesn't return a boolean. Maybe `stretch_axis`

Code after:

pub(super) fn delimiter_alignment(delimiter: char) -> VAlignment {
    match delimiter {
        '\u{231c}' | '\u{231d}' => VAlignment::Top,
        '\u{231e}' | '\u{231f}' => VAlignment::Bottom,
        _ => VAlignment::Horizon,
    }
}

/// Return whether the glyph is stretchable and if it is, along which axis it
/// can be stretched.
fn stretch_axis(ctx: &MathContext, base: &GlyphFragment) -> Option<Axis> {
    let base_id = base.id;
    let vertical = ctx
        .table
        .variants
        .and_then(|variants| variants.vertical_constructions.get(base_id))
        .map(|_| Axis::Y);
    let horizontal = ctx
        .table
        .variants
        .and_then(|variants| variants.horizontal_constructions.get(base_id))
        .map(|_| Axis::X);

    match (vertical, horizontal) {
        (vertical, None) => vertical,
        (None, horizontal) => horizontal,
        _ => {
            // As far as we know, there aren't any glyphs that have both
            // vertical and horizontal constructions. So for the time being, we
            // will assume that a glyph cannot have both.
            panic!("glyph {:?} has both vertical and horizontal constructions", base.c);
        }
    }
}

impl GlyphFragment {
    /// Try to stretch a glyph to a desired height.
    pub fn stretch_vertical(
        self,
        ctx: &MathContext,
        height: Abs,
        short_fall: Abs,
    ) -> VariantFragment {
        stretch_glyph(ctx, self, height, short_fall, Axis::Y)
    }

    /// Try to stretch a glyph to a desired width.
    pub fn stretch_horizontal(
        self,
        ctx: &MathContext,