Skip to main content

harfrust/
scale.rs

1use crate::GlyphExtents;
2use read_fonts::types::{BoundingBox, F48Dot16};
3
4// libm used for f32::floor() and f32::ceil()
5#[cfg(not(feature = "std"))]
6#[allow(unused_imports)]
7use core_maths::CoreFloat as _;
8
9#[derive(Copy, Clone)]
10/// Converts font units to the configured font scale.
11///
12/// The conversion uses the scale set on [`ShaperFont`](crate::ShaperFont)
13/// and follows HarfBuzz's rounding rules for glyph metrics and shaping.
14#[derive(Debug)]
15pub struct Scale {
16    x_mult: i64,
17    y_mult: i64,
18    x_multf: f32,
19    y_multf: f32,
20}
21
22impl Default for Scale {
23    fn default() -> Self {
24        Self {
25            x_mult: 1 << 16,
26            y_mult: 1 << 16,
27            x_multf: 1.0,
28            y_multf: 1.0,
29        }
30    }
31}
32
33// Various conversions between f32 and i32
34#[allow(clippy::cast_precision_loss)]
35impl Scale {
36    /// Creates a conversion from font units to the requested scale.
37    ///
38    /// Returns an identity conversion if `scale` is `None` or `upem` is zero.
39    pub fn new(scale: Option<(i32, i32)>, upem: i32) -> Self {
40        let (Some((x_scale, y_scale)), true) = (scale, upem != 0) else {
41            // When scale is not configured, or upem is zero, return results
42            // in font units.
43            return Self::default();
44        };
45        let [x_mult, y_mult] = [x_scale, y_scale].map(|s| Self::mult_from_scale(s, upem));
46        let upem = upem as f32;
47        Self {
48            x_mult,
49            y_mult,
50            x_multf: x_scale as f32 / upem,
51            y_multf: y_scale as f32 / upem,
52        }
53    }
54
55    /// Converts a horizontal distance from font units.
56    #[inline(always)]
57    pub fn scale_x(&self, x: i32) -> i32 {
58        Self::scale_by_mult(x, self.x_mult)
59    }
60
61    /// Converts a vertical distance from font units.
62    #[inline(always)]
63    pub fn scale_y(&self, y: i32) -> i32 {
64        Self::scale_by_mult(y, self.y_mult)
65    }
66
67    /// Scales a fractional (font-unit) value, matching HarfBuzz's `em_scalef`
68    /// (`roundf(v * scale / upem)`).
69    #[inline(always)]
70    pub(crate) fn scale_x_f(&self, x: f32) -> i32 {
71        (x * self.x_multf).round() as i32
72    }
73
74    #[inline(always)]
75    pub(crate) fn scale_y_f(&self, y: f32) -> i32 {
76        (y * self.y_multf).round() as i32
77    }
78
79    /// Clamps a scaled advance to the sign of its scale. This applies the
80    /// unsigned font-unit bound while preserving deliberately negative scales.
81    pub(crate) fn clamp_x_advance(&self, advance: i32) -> i32 {
82        Self::clamp_advance(advance, self.x_multf)
83    }
84
85    pub(crate) fn clamp_y_advance(&self, advance: i32) -> i32 {
86        Self::clamp_advance(advance, self.y_multf)
87    }
88
89    fn clamp_advance(advance: i32, scale: f32) -> i32 {
90        if scale < 0.0 {
91            advance.min(0)
92        } else {
93            advance.max(0)
94        }
95    }
96
97    /// Scales glyph extents using HarfBuzz's corner rounding.
98    ///
99    /// Floors the near corners and ceils the far corners before calculating
100    /// width and height.
101    pub fn scale_extents(&self, mut extents: GlyphExtents) -> GlyphExtents {
102        let x1 = extents.x_bearing as f32 * self.x_multf;
103        let y1 = extents.y_bearing as f32 * self.y_multf;
104        let x2 = (i64::from(extents.x_bearing) + i64::from(extents.width)) as f32 * self.x_multf;
105        let y2 = (i64::from(extents.y_bearing) + i64::from(extents.height)) as f32 * self.y_multf;
106        let rx1 = x1.floor();
107        let ry1 = y1.floor();
108        let rx2 = x2.ceil();
109        let ry2 = y2.ceil();
110        extents.x_bearing = rx1 as i32;
111        extents.y_bearing = ry1 as i32;
112        extents.width = (f64::from(rx2) - f64::from(rx1)) as i32;
113        extents.height = (f64::from(ry2) - f64::from(ry1)) as i32;
114        extents
115    }
116
117    #[inline(always)]
118    fn mult_from_scale(scale: i32, upem: i32) -> i64 {
119        if scale < 0 {
120            -((-(scale as i64)) << 16) / upem as i64
121        } else {
122            ((scale as i64) << 16) / upem as i64
123        }
124    }
125
126    #[inline(always)]
127    fn scale_by_mult(value: i32, mult: i64) -> i32 {
128        ((i64::from(value) * mult + 32768) >> 16) as i32
129    }
130}
131
132impl read_fonts::model::metrics::Scale for Scale {
133    type Value = i32;
134
135    fn add(a: i32, b: i32) -> i32 {
136        a.saturating_add(b)
137    }
138
139    fn sub(a: i32, b: i32) -> i32 {
140        a.saturating_sub(b)
141    }
142
143    fn half(value: i32) -> i32 {
144        value >> 1
145    }
146
147    fn scale_x(&self, value: F48Dot16) -> i32 {
148        if value.to_bits().trailing_zeros() >= 16 {
149            self.scale_x(value.to_i32())
150        } else {
151            self.scale_x_f(value.to_f32())
152        }
153    }
154
155    fn scale_y(&self, value: F48Dot16) -> i32 {
156        if value.to_bits().trailing_zeros() >= 16 {
157            self.scale_y(value.to_i32())
158        } else {
159            self.scale_y_f(value.to_f32())
160        }
161    }
162
163    fn scale_glyph_extents(
164        &self,
165        extents: read_fonts::model::metrics::GlyphExtents<F48Dot16>,
166    ) -> read_fonts::model::metrics::GlyphExtents<i32> {
167        let left = (extents.x_bearing.to_f32() * self.x_multf).floor();
168        let top = (extents.y_bearing.to_f32() * self.y_multf).floor();
169        let right = ((extents.x_bearing + extents.width).to_f32() * self.x_multf).ceil();
170        let bottom = ((extents.y_bearing - extents.height).to_f32() * self.y_multf).ceil();
171        read_fonts::model::metrics::GlyphExtents {
172            x_bearing: left as i32,
173            y_bearing: top as i32,
174            width: (f64::from(right) - f64::from(left)) as i32,
175            height: (f64::from(top) - f64::from(bottom)) as i32,
176        }
177    }
178
179    fn scale_rect(&self, bounds: BoundingBox<F48Dot16>) -> BoundingBox<i32> {
180        BoundingBox {
181            x_min: self.scale_x_f(bounds.x_min.to_f32()),
182            y_min: self.scale_y_f(bounds.y_min.to_f32()),
183            x_max: self.scale_x_f(bounds.x_max.to_f32()),
184            y_max: self.scale_y_f(bounds.y_max.to_f32()),
185        }
186    }
187}