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

Code before:

    // Only exception: if the stretched slash is bigger than intended.
    let mut slash_frag = GlyphFragment::new_char(ctx.font, styles, '/', span)?;
    slash_frag.stretch_vertical(ctx, fraction_height - short_fall);
    slash_frag.center_on_axis();
    let slash_frame = slash_frag.into_frame();
    let slash_size = slash_frame.size();
    let vertical_offset = Abs::zero().max(slash_size.y - fraction_height) / 2.0;
    fraction_height = fraction_height.max(slash_size.y);

    // Build the final frame
    let mut fraction_frame = Frame::soft(Size::new(fraction_width, fraction_height));

    // Numerator
    fraction_frame.push_frame(Point::new(Abs::zero(), vertical_offset), num_frame);
    // Denominator
    fraction_frame.push_frame(
        Point::new(num_size.x + hgap, vertical_offset + num_size.y + vgap),
        denom_frame,
    );

    // Slash
    fraction_frame.push_frame(
        Point::new(
            num_size.x + hgap / 2.0 - slash_size.x / 2.0,
            fraction_height / 2.0 - slash_size.y / 2.0,
        ),
        slash_frame,
    );

    // Baseline (use axis height to center slash on the axis)
    fraction_frame.set_baseline(fraction_height / 2.0 + axis);

    // fraction_frame.mark_box_in_place();

    ctx.push(FrameFragment::new(styles, fraction_frame));

    Ok(())
}


Review comment (suggestion):
```suggestion
            Abs::zero().max(fraction_height / 2.0 - slash_size.y / 2.0),
```
Like with the `vertical_offset` above, this could end up negative and then stick out of the final frame size. 

Code after:
    let num_size = num_frame.size();
    let denom_style = style_for_denominator(styles);
    let denom_frame = ctx.layout_into_frame(denom, styles.chain(&denom_style))?;
    let denom_size = denom_frame.size();

    let short_fall = DELIM_SHORT_FALL.resolve(styles);

    // Height of the fraction frame
    // We recalculate this value below if the slash glyph overflows
    let mut fraction_height = num_size.y + denom_size.y + vgap;

    // Build the slash glyph to calculate its size
    let mut slash_frag =
        ctx.layout_into_fragment(&SymbolElem::packed('\u{2044}').spanned(span), styles)?;
    slash_frag.stretch_vertical(ctx, fraction_height - short_fall);
    slash_frag.center_on_axis();
    let slash_frame = slash_frag.into_frame();

    // Adjust the fraction height if the slash overflows
    let slash_size = slash_frame.size();
    let vertical_offset = Abs::zero().max(slash_size.y - fraction_height) / 2.0;
    fraction_height.set_max(slash_size.y);

    // Reference points for all three objects, used to place them in the frame.
    let mut slash_up_left = Point::new(num_size.x + hgap / 2.0, fraction_height / 2.0)
        - slash_size.to_point() / 2.0;
    let mut num_up_left = Point::with_y(vertical_offset);
    let mut denom_up_left = num_up_left + num_size.to_point() + Point::new(hgap, vgap);

    // Fraction width
    let fraction_width = (denom_up_left.x + denom_size.x)
        .max(slash_up_left.x + slash_size.x)
        + Abs::zero().max(-slash_up_left.x);
    // We have to shift everything right to avoid going in the negatives for
    // the x coordinate
    let horizontal_offset = Point::with_x(Abs::zero().max(-slash_up_left.x));
    slash_up_left += horizontal_offset;
    num_up_left += horizontal_offset;
    denom_up_left += horizontal_offset;

    // Build the final frame
    let mut fraction_frame = Frame::soft(Size::new(fraction_width, fraction_height));

    // Baseline (use axis height to center slash on the axis)
    fraction_frame.set_baseline(fraction_height / 2.0 + axis);

    // Numerator, Denominator, Slash
    fraction_frame.push_frame(num_up_left, num_frame);
    fraction_frame.push_frame(denom_up_left, denom_frame);
    fraction_frame.push_frame(slash_up_left, slash_frame);