Repository: typst/typst
File: crates/typst-layout/src/inline/shaping.rs (Rust)

Code before:
            self.stretchability(),
            style,
        )
    }

    /// See <https://www.w3.org/TR/clreq/#punctuation_width_adjustment>
    pub fn is_cjk_right_aligned_punctuation(&self) -> bool {
        is_cjk_right_aligned_punctuation(self.c, self.x_advance, self.stretchability())
    }

    /// See <https://www.w3.org/TR/clreq/#punctuation_width_adjustment>
    pub fn is_cjk_center_aligned_punctuation(&self, style: CjkPunctStyle) -> bool {
        is_cjk_center_aligned_punctuation(self.c, style)
    }

    /// Whether the glyph is a western letter or number.
    pub fn is_letter_or_number(&self) -> bool {
        matches!(self.c.script(), Script::Latin | Script::Greek | Script::Cyrillic)
            || matches!(self.c, '#' | '$' | '%' | '&')
            || self.c.is_ascii_digit()
    }

    pub fn base_adjustability(
        &self,
        style: CjkPunctStyle,
        justification_limits: JustificationLimits,
        font_size: Abs,
    ) -> Adjustability {
        let width = self.x_advance;

        if self.is_space() {
            Adjustability {
                stretchability: (
                    Em::zero(),
                    (width / 2.0) * justification_limits.word_max.rel.get()
                        + Em::from_length(justification_limits.word_max.abs, font_size),
                ),
                shrinkability: (
                    Em::zero(),
                    (width / 3.0) * justification_limits.word_min.rel.get()
                        + Em::from_length(justification_limits.word_min.abs, font_size),
                ),
            }
        } else if self.is_cjk_left_aligned_punctuation(style) {
            Adjustability {
                stretchability: (Em::zero(), Em::zero()),
                shrinkability: (Em::zero(), width / 2.0),
            }
        } else if self.is_cjk_right_aligned_punctuation() {
            Adjustability {
                stretchability: (Em::zero(), Em::zero()),

Review comment (suggestion):
Maybe this comment should not be deleted? As far as I understand, this comment explains the values 2.0 and 3.0 below.

Code after:
            self.stretchability(),
            style,
        )
    }

    /// See <https://www.w3.org/TR/clreq/#punctuation_width_adjustment>
    pub fn is_cjk_right_aligned_punctuation(&self) -> bool {
        is_cjk_right_aligned_punctuation(self.c, self.x_advance, self.stretchability())
    }

    /// See <https://www.w3.org/TR/clreq/#punctuation_width_adjustment>
    pub fn is_cjk_center_aligned_punctuation(&self, style: CjkPunctStyle) -> bool {
        is_cjk_center_aligned_punctuation(self.c, style)
    }

    /// Whether the glyph is a western letter or number.
    pub fn is_letter_or_number(&self) -> bool {
        matches!(self.c.script(), Script::Latin | Script::Greek | Script::Cyrillic)
            || matches!(self.c, '#' | '$' | '%' | '&')
            || self.c.is_ascii_digit()
    }

    /// The amount by which the glyph's advance width is allowed to be shrunk or
    /// stretched to improve justification.
    pub fn base_adjustability(
        &self,
        style: CjkPunctStyle,
        limits: &JustificationLimits,
        font_size: Abs,
        stretchable: bool,
    ) -> Adjustability {
        let width = self.x_advance;

        // Do not ever shrink away more than three quarters of the glyph. As the
        // width approaches zero, justification gets increasingly expensive and
        // if negative it may not terminate.
        let limited = |v: Em| v.min(width * 0.75);

        if self.is_space() {
            // To a space, both the spacing and tracking limits apply, just like
            // `text.tracking` and `text.spacing` both apply to spaces.
            let max = limits.spacing().max + limits.tracking().max;
            let min = limits.spacing().min + limits.tracking().min;
            Adjustability {
                stretchability: (
                    Em::zero(),
                    (max - Rel::one())
                        .map(|length| Em::from_length(length, font_size))
                        .relative_to(width)
                        .max(Em::zero()),
                ),