Repository: typst/typst
File: crates/typst/src/math/equation.rs (Rust)

Code before:
                    frame.width() + 2.0 * full_counter_width
                };

                let height = frame.height().max(counter.height());
                let align = AlignElem::alignment_in(styles).resolve(styles).x;
                frame.resize(Size::new(width, height), Axes::splat(align));

                let dir = TextElem::dir_in(styles);
                let offset = match (align, dir) {
                    (FixedAlign::Start, Dir::RTL) => full_counter_width,
                    (FixedAlign::End, Dir::LTR) => -full_counter_width,
                    _ => Abs::zero(),
                };
                frame.translate(Point::with_x(offset));

                let x = if dir.is_positive() {
                    frame.width() - counter.width()
                } else {
                    Abs::zero()
                };
                let y = (frame.height() - counter.height()) / 2.0;

                frame.push_frame(Point::new(x, y), counter)
            }
        } else {
            adjust_for_leading(&mut frame, &font, styles);
        }

        // Apply metadata.
        frame.meta(styles, false);

        Ok(Fragment::frame(frame))
    }
}

impl Count for EquationElem {
    fn update(&self) -> Option<CounterUpdate> {
        (self.block(StyleChain::default())
            && self.numbering(StyleChain::default()).is_some())
        .then(|| CounterUpdate::Step(NonZeroUsize::ONE))
    }
}

impl LocalName for EquationElem {
    fn local_name(lang: Lang, region: Option<Region>) -> &'static str {
        match lang {
            Lang::ALBANIAN => "Ekuacion",
            Lang::ARABIC => "معادلة",
            Lang::BOKMÅL => "Ligning",
            Lang::CHINESE if option_eq(region, "TW") => "方程式",
            Lang::CHINESE => "公式",

Review comment (suggestion):
I wonder whether this `else` is dead code, since the whole `layout` call should only happen for block equations.

Code after:
    ) -> SourceResult<Fragment> {
        const NUMBER_GUTTER: Em = Em::new(0.5);

        assert!(self.block(styles));

        // Find a math font.
        let font = find_math_font(engine, styles, self.span())?;

        let mut ctx = MathContext::new(engine, styles, regions, &font, true);
        let mut frame = ctx.layout_frame(self)?;

        if let Some(numbering) = self.numbering(styles) {
            let pod = Regions::one(regions.base(), Axes::splat(false));
            let counter = Counter::of(Self::elem())
                .display(self.span(), Some(numbering), false)
                .layout(engine, styles, pod)?
                .into_frame();

            let full_counter_width = counter.width() + NUMBER_GUTTER.resolve(styles);
            let width = if regions.size.x.is_finite() {
                regions.size.x
            } else {
                frame.width() + 2.0 * full_counter_width
            };

            let height = frame.height().max(counter.height());
            let align = AlignElem::alignment_in(styles).resolve(styles).x;
            frame.resize(Size::new(width, height), Axes::splat(align));

            let dir = TextElem::dir_in(styles);
            let offset = match (align, dir) {
                (FixedAlign::Start, Dir::RTL) => full_counter_width,
                (FixedAlign::End, Dir::LTR) => -full_counter_width,
                _ => Abs::zero(),
            };
            frame.translate(Point::with_x(offset));

            let x = if dir.is_positive() {
                frame.width() - counter.width()
            } else {
                Abs::zero()
            };
            let y = (frame.height() - counter.height()) / 2.0;

            frame.push_frame(Point::new(x, y), counter)
        }

        // Apply metadata.
        frame.meta(styles, false);

        Ok(Fragment::frame(frame))