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

Code before:
use ttf_parser::math::{GlyphAssembly, GlyphConstruction, GlyphPart};
use ttf_parser::LazyArray16;

use crate::diag::SourceResult;
use crate::foundations::{elem, Content, Packed, Resolve, Smart, StyleChain};
use crate::layout::{Abs, Frame, Length, Point, Rel, Size, VAlignment};
use crate::math::{
    GlyphFragment, LayoutMath, MathContext, MathFragment, Scaled, VariantFragment,
};

/// Maximum number of times extenders can be repeated.
const MAX_REPEATS: usize = 1024;

/// Stretches a base.
///
/// This function can also be used to automatically stretch the base of an
/// attachment, so that it fits the top and bottom attachments.
///
/// Note that only some glyphs can be stretched, and which ones can depends on
/// the math font being used. Generally however, all math fonts are the same in
/// this regard.
///
/// ```example
/// $ H stretch(=)^"define" U + p V $
/// $ f : X stretch(->>, size: #150%)_"surjective" Y $
/// $ x stretch(harpoons.ltrb, size: #3em) y
///     stretch(\[, size: #150%) z $
/// ```
#[elem(LayoutMath)]
pub struct StretchElem {
    /// The base to stretch.
    #[required]
    pub base: Content,

    /// The size to stretch to, relative to the glyph's current size.
    pub size: Smart<Rel<Length>>,

    /// Whether the size should be relative to the width of any top or bottom
    /// attachments.
    ///
    /// ```example
    /// #set math.stretch(attach: false)
    /// $ stretch(->, size: #250%)_"very long text" $
    /// ```
    #[default(true)]

Review comment (suggestion):
```suggestion
/// the math font being used. However, most math fonts are the same in
```

Code after:
use ttf_parser::math::{GlyphAssembly, GlyphConstruction, GlyphPart};
use ttf_parser::LazyArray16;

use crate::diag::SourceResult;
use crate::foundations::{elem, Content, Packed, Resolve, Smart, StyleChain};
use crate::layout::{Abs, Axis, Frame, Length, Point, Rel, Size, VAlignment};
use crate::math::{
    GlyphFragment, LayoutMath, MathContext, MathFragment, Scaled, VariantFragment,
};
use crate::utils::Get;

/// Maximum number of times extenders can be repeated.
const MAX_REPEATS: usize = 1024;

/// Stretches a glyph.
///
/// This function can also be used to automatically stretch the base of an
/// attachment, so that it fits the top and bottom attachments.
///
/// Note that only some glyphs can be stretched, and which ones can depend on
/// the math font being used. However, most math fonts are the same in this
/// regard.
///
/// ```example
/// $ H stretch(=)^"define" U + p V $
/// $ f : X stretch(->>, size: #150%)_"surjective" Y $
/// $ x stretch(harpoons.ltrb, size: #3em) y
///     stretch(\[, size: #150%) z $
/// ```
#[elem(LayoutMath)]
pub struct StretchElem {
    /// The glyph to stretch.
    #[required]
    pub body: Content,

    /// The size to stretch to, relative to the glyph's current size.
    pub size: Smart<Rel<Length>>,
}

impl LayoutMath for Packed<StretchElem> {
    #[typst_macros::time(name = "math.stretch", span = self.span())]
    fn layout_math(&self, ctx: &mut MathContext, styles: StyleChain) -> SourceResult<()> {
        let mut fragment = ctx.layout_into_fragment(self.body(), styles)?;
        stretch_fragment(
            ctx,