Repository: typst/typst
File: crates/typst/src/layout/length.rs (Rust)

Code before:

    /// Resolve this length to an absolute length.
    #[func]
    pub fn resolve(
        &self,
        /// The styles with which to measure the length.
        styles: Styles,
    ) -> StrResult<Length> {
        // Shortcircuit to avoid doing a stylechain lookup if we're already
        // absolute.
        if self.is_absolute() {
            return Ok(*self);
        }

        let styles = StyleChain::new(&styles);
        Ok(Length {
            abs: self.abs + self.em.resolve(styles),
            em: Em::zero(),
        })
    }

    /// Returns whether this length is absolute (that is, has no `em` units).
    #[func]
    pub fn is_absolute(&self) -> bool {
        self.em.is_zero()
    }
}

impl Debug for Length {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        match (self.abs.is_zero(), self.em.is_zero()) {
            (false, false) => write!(f, "{:?} + {:?}", self.abs, self.em),
            (true, false) => self.em.fmt(f),
            (_, true) => self.abs.fmt(f),
        }
    }
}

impl Repr for Length {
    fn repr(&self) -> EcoString {
        match (self.abs.is_zero(), self.em.is_zero()) {
            (false, false) => eco_format!("{} + {}", self.abs.repr(), self.em.repr()),
            (true, false) => self.em.repr(),
            (_, true) => self.abs.repr(),
        }
    }
}

impl Numeric for Length {
    fn zero() -> Self {
        Self::zero()

Review comment (suggestion):
I don't think this method should be added. It's trivial to write in Typst, too, by accessing the `em` field.

Code after:

    /// Resolve this length to an absolute length.
    ///
    /// ```example
    /// #set text(size: 12pt)
    /// #style(styles => [
    ///   #(6pt).to-absolute(styles) \
    ///   #(6pt + 10em).to-absolute(styles) \
    ///   #(10em).to-absolute(styles)
    /// ])
    ///
    /// #set text(size: 6pt)
    /// #style(styles => [
    ///   #(6pt).to-absolute(styles) \
    ///   #(6pt + 10em).to-absolute(styles) \
    ///   #(10em).to-absolute(styles)
    /// ])
    /// ```
    #[func]
    pub fn to_absolute(
        &self,
        /// The styles to resolve the length with.
        ///
        /// Since a length can use font-relative em units, resolving it to an
        /// absolute length requires knowledge of the font size. This size is
        /// provided through these styles. You can obtain the styles using
        /// the [`style`]($style) function.
        styles: Styles,
    ) -> Length {
        let styles = StyleChain::new(&styles);
        self.resolve(styles).into()
    }
}

impl Debug for Length {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        match (self.abs.is_zero(), self.em.is_zero()) {
            (false, false) => write!(f, "{:?} + {:?}", self.abs, self.em),
            (true, false) => self.em.fmt(f),
            (_, true) => self.abs.fmt(f),
        }
    }
}

impl Repr for Length {
    fn repr(&self) -> EcoString {
        match (self.abs.is_zero(), self.em.is_zero()) {
            (false, false) => eco_format!("{} + {}", self.abs.repr(), self.em.repr()),
            (true, false) => self.em.repr(),
            (_, true) => self.abs.repr(),
        }