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

Code before:
    let short_fall = DELIM_SHORT_FALL.resolve(styles);

    // Final size of the fraction frame, save for the one exception below
    let mut fraction_height = num_size.y + denom_size.y + vgap;
    let fraction_width = num_size.x + denom_size.x + hgap;

    // Only exception: if the stretched slash is bigger than intended.
    let mut slash_frag = GlyphFragment::new_char(ctx.font, styles, '\u{2044}', span)?;
    let pre_stretch_height = slash_frag.size.y;
    slash_frag.stretch_vertical(ctx, fraction_height - short_fall);
    // If the standard slash was not stretchable, try the fraction slash
    if slash_frag.size.y == pre_stretch_height {
        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.set_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
    fraction_frame.push_frame(Point::with_y(vertical_offset), num_frame);
```

Code after:
    let hgap = constants.skewed_fraction_horizontal_gap.resolve(styles);
    let axis = constants.axis_height.resolve(styles);

    let num_style = style_for_numerator(styles);
    let num_frame = ctx.layout_into_frame(num, styles.chain(&num_style))?;
    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);