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

Code before:
    let radicand_x = sqrt_x + sqrt.width();
    let radicand_y = ascent - radicand.ascent();
    let size = Size::new(radicand_x + line_width, ascent + descent);

    // The extra "- thickness" comes from the fact that the sqrt is placed
    // in `push_frame` with respect to its top, not its baseline.
    let sqrt_pos = Point::new(sqrt_x, radicand_y - gap - thickness);
    let line_pos = Point::new(radicand_x, radicand_y - gap - (thickness / 2.0));
    let radicand_pos = Point::new(radicand_x, radicand_y);

    let mut frame = Frame::soft(size);
    frame.set_baseline(ascent);

    if let Some(index) = index {
        let index_x = -sqrt_offset.min(Abs::zero()) + kern_before;
        let index_pos = Point::new(index_x, ascent - index.ascent() - shift_up);
        frame.push_frame(index_pos, index);
    }

    frame.push_frame(sqrt_pos, sqrt);
    frame.push(line_pos, line);

    // The horizontal line of the root symbol is drawn with a geometry object,
    // not a text glyph, so the text's stroke style was not automatically applied and we
    // need to apply it by drawing around the horizontal line.
    if let Some(stroke) = styles.get_ref(TextElem::stroke) {
        let stroke_thickness = stroke.thickness.resolve(styles).unwrap_or(Abs::pt(1.0));
        let fixed_stroke = FixedStroke::from_pair(
            stroke.paint.clone().unwrap_or(text_fill.clone()),
            stroke_thickness,
        );
        let horizontal_edge = FrameItem::Shape(
            Geometry::Line(Point::with_x(line_width)).stroked(fixed_stroke.clone()),
            span,
        );
        let vertical_edge = FrameItem::Shape(
            Geometry::Line(Point::with_y(thickness + stroke_thickness))
                .stroked(fixed_stroke),
            span,
        );
        frame.push(line_pos - Point::with_y(thickness / 2.0), horizontal_edge.clone());
        frame.push(line_pos + Point::with_y(thickness / 2.0), horizontal_edge);
        frame.push(
            line_pos + Point::new(line_width, -(thickness + stroke_thickness) / 2.0),
            vertical_edge,
        );
    }

    frame.push_frame(radicand_pos, radicand);
    ctx.push(FrameFragment::new(styles, frame));


Review comment (refactor):
I think you can simplify this by making use of the functions in `typst-layout/src/shapes.rs`, e.g. the `styled_rect` function.

Code after:

    // The extra "- thickness" comes from the fact that the sqrt is placed
    // in `push_frame` with respect to its top, not its baseline.
    let sqrt_pos = Point::new(sqrt_x, radicand_y - gap - thickness);
    let line_pos = Point::new(radicand_x, radicand_y - gap - (thickness / 2.0));
    let radicand_pos = Point::new(radicand_x, radicand_y);

    let mut frame = Frame::soft(size);
    frame.set_baseline(ascent);

    if let Some(index) = index {
        let index_x = -sqrt_offset.min(Abs::zero()) + kern_before;
        let index_pos = Point::new(index_x, ascent - index.ascent() - shift_up);
        frame.push_frame(index_pos, index);
    }

    frame.push_frame(sqrt_pos, sqrt);

    // Add the horizontal line of the root symbol.
    if let Some(fixed_stroke) = sqrt_stroke {
        let sides = styled_rect(
            Size::new(line_width, thickness),
            &Corners::splat(Rel::<Abs>::zero()),
            Some(text_fill),
            &Sides::new(
                None, // Omit the left-side edge.
                Some(fixed_stroke.clone()),
                Some(fixed_stroke.clone()),
                Some(fixed_stroke),
            ),
        );
        frame.push_multiple(sides.into_iter().map(|shape| {
            (line_pos - Point::with_y(thickness / 2.0), FrameItem::Shape(shape, span))
        }));
    } else {
        let line = FrameItem::Shape(
            Geometry::Line(Point::with_x(line_width))
                .stroked(FixedStroke::from_pair(text_fill, thickness)),
            span,
        );
        frame.push(line_pos, line);
    }

    frame.push_frame(radicand_pos, radicand);
    ctx.push(FrameFragment::new(styles, frame));

    Ok(())
}