Repository: typst/typst
File: crates/typst/src/model/heading.rs (Rust)

Code before:

        let elem = self.as_mut();
        elem.push_level(Smart::Custom(elem.resolve_level(styles)));
        elem.push_supplement(Smart::Custom(Some(Supplement::Content(supplement))));
        Ok(())
    }
}

impl Show for Packed<HeadingElem> {
    #[typst_macros::time(name = "heading", span = self.span())]
    fn show(&self, engine: &mut Engine, styles: StyleChain) -> SourceResult<Content> {
        let span = self.span();
        let mut realized = self.body().clone();

        let hanging_indent = self.hanging_indent(styles);

        let mut indent = match self.hanging_indent(styles) {
            Smart::Custom(length) => length.resolve(styles),
            Smart::Auto => Abs::zero(),
        };

        if let Some(numbering) = (**self).numbering(styles).as_ref() {
            let numbering = Counter::of(HeadingElem::elem())
                .display_at_loc(engine, self.location().unwrap(), styles, numbering)?
                .spanned(span)
                + HElem::new(Em::new(0.3).into()).with_weak(true).pack();

            if hanging_indent.is_auto() {
                let pod = Regions::one(Axes::splat(Abs::inf()), Axes::splat(false));
                let size = numbering.measure(engine, styles, pod)?.into_frame().size();

                let min_indent = Length { abs: size.x, em: Em::new(0.3) }.resolve(styles);
                indent = indent.max(min_indent);
            }

            realized = numbering + realized;
        }

        realized = realized.styled(ParElem::set_hanging_indent(indent.into()));

        Ok(BlockElem::new().with_body(Some(realized)).pack().spanned(span))
    }
}

impl ShowSet for Packed<HeadingElem> {
    fn show_set(&self, styles: StyleChain) -> Styles {
        let level = (**self).resolve_level(styles).get();
        let scale = match level {
            1 => 1.4,
            2 => 1.2,
            _ => 1.0,

Review comment (suggestion):
I think the HElem shouldn't be part of the numbering variable. For the measurement, it is not taken into account anyway due to `weak: true`.

Code after:
        elem.push_supplement(Smart::Custom(Some(Supplement::Content(supplement))));
        Ok(())
    }
}

impl Show for Packed<HeadingElem> {
    #[typst_macros::time(name = "heading", span = self.span())]
    fn show(&self, engine: &mut Engine, styles: StyleChain) -> SourceResult<Content> {
        const SPACING_TO_NUMBERING: Em = Em::new(0.3);

        let span = self.span();
        let mut realized = self.body().clone();

        let hanging_indent = self.hanging_indent(styles);

        let mut indent = match hanging_indent {
            Smart::Custom(length) => length.resolve(styles),
            Smart::Auto => Abs::zero(),
        };

        if let Some(numbering) = (**self).numbering(styles).as_ref() {
            let numbering = Counter::of(HeadingElem::elem())
                .display_at_loc(engine, self.location().unwrap(), styles, numbering)?
                .spanned(span);

            if hanging_indent.is_auto() {
                let pod = Regions::one(Axes::splat(Abs::inf()), Axes::splat(false));
                let size = numbering.measure(engine, styles, pod)?.into_frame().size();

                indent = size.x + SPACING_TO_NUMBERING.resolve(styles);
            }

            realized = numbering
                + HElem::new(SPACING_TO_NUMBERING.into()).with_weak(true).pack()
                + realized;
        }

        if indent != Abs::zero() {
            realized = realized.styled(ParElem::set_hanging_indent(indent.into()));
        }

        Ok(BlockElem::new().with_body(Some(realized)).pack().spanned(span))
    }
}

impl ShowSet for Packed<HeadingElem> {
    fn show_set(&self, styles: StyleChain) -> Styles {
        let level = (**self).resolve_level(styles).get();
        let scale = match level {
            1 => 1.4,
            2 => 1.2,