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

Code before:
            let axis = font.math().axis_height.at(size);
            let mut radicand = radicand.into_frame();
            radicand.set_baseline(radicand.height() / 2.0 + axis);
            radicand
        } else {
            radicand.into_frame()
        }
    };

    // Layout root symbol.
    let mut sqrt =
        ctx.layout_into_fragment(&SymbolElem::packed('√').spanned(span), styles)?;

    let (font, size) = sqrt.font(ctx, styles);
    let thickness = font.math().radical_rule_thickness.at(size);
    let extra_ascender = font.math().radical_extra_ascender.at(size);
    let kern_before = font.math().radical_kern_before_degree.at(size);
    let kern_after = font.math().radical_kern_after_degree.at(size);
    let raise_factor = font.math().radical_degree_bottom_raise_percent;
    let gap = match styles.get(EquationElem::size) {
        MathSize::Display => font.math().radical_display_style_vertical_gap,
        _ => font.math().radical_vertical_gap,
    }
    .at(size);

    let text_fill = styles.get_ref(TextElem::fill).as_decoration();
    let line_width = radicand.width();
    let line = FrameItem::Shape(
        Geometry::Line(Point::with_x(line_width)).stroked(FixedStroke::from_pair(
            sqrt.fill().unwrap_or(text_fill.clone()),
            thickness,
        )),
        span,
    );

    let target = radicand.height() + thickness + gap;
    sqrt.stretch_vertical(ctx, target, Abs::zero());
    let sqrt = sqrt.into_frame();

    // Layout the index.
    let sscript = EquationElem::size.set(MathSize::ScriptScript).wrap();
    let index = elem
        .index
        .get_ref(styles)
        .as_ref()
        .map(|elem| ctx.layout_into_frame(elem, styles.chain(&sscript)))
        .transpose()?;

    // TeXbook, page 443, item 11
    // Keep original gap, and then distribute any remaining free space
    // equally above and below.

Review comment (suggestion):
This should try get the fill from the sqrt MathFragment first:  `sqrt.fill().unwrap_or_else(|| ...)`

Code after:
            let axis = font.math().axis_height.at(size);
            let mut radicand = radicand.into_frame();
            radicand.set_baseline(radicand.height() / 2.0 + axis);
            radicand
        } else {
            radicand.into_frame()
        }
    };

    // Layout root symbol.
    let mut sqrt =
        ctx.layout_into_fragment(&SymbolElem::packed('√').spanned(span), styles)?;

    let (font, size) = sqrt.font(ctx, styles);
    let thickness = font.math().radical_rule_thickness.at(size);
    let extra_ascender = font.math().radical_extra_ascender.at(size);
    let kern_before = font.math().radical_kern_before_degree.at(size);
    let kern_after = font.math().radical_kern_after_degree.at(size);
    let raise_factor = font.math().radical_degree_bottom_raise_percent;
    let gap = match styles.get(EquationElem::size) {
        MathSize::Display => font.math().radical_display_style_vertical_gap,
        _ => font.math().radical_vertical_gap,
    }
    .at(size);

    let text_fill = sqrt
        .fill()
        .unwrap_or_else(|| styles.get_ref(TextElem::fill).as_decoration());
    let line_width = radicand.width();

    let target = radicand.height() + thickness + gap;
    sqrt.stretch_vertical(ctx, target, Abs::zero());
    let sqrt_stroke = sqrt.stroke();
    let sqrt = sqrt.into_frame();

    // Layout the index.
    let sscript = EquationElem::size.set(MathSize::ScriptScript).wrap();
    let index = elem
        .index
        .get_ref(styles)
        .as_ref()
        .map(|elem| ctx.layout_into_frame(elem, styles.chain(&sscript)))
        .transpose()?;

    // TeXbook, page 443, item 11
    // Keep original gap, and then distribute any remaining free space
    // equally above and below.
    let gap = gap.max((sqrt.height() - thickness - radicand.height() + gap) / 2.0);

    let sqrt_ascent = radicand.ascent() + gap + thickness;
    let descent = sqrt.height() - sqrt_ascent;