Coverage Report

Created: 2026-10-28 07:26

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/fontations/skrifa/src/metrics.rs
Line
Count
Source
1
//! Global font and glyph specific metrics.
2
//!
3
//! Metrics are various measurements that define positioning and layout
4
//! characteristics for a font. They come in two flavors:
5
//!
6
//! * Global metrics: these are applicable to all glyphs in a font and generally
7
//!   define values that are used for the layout of a collection of glyphs. For example,
8
//!   the ascent, descent and leading values determine the position of the baseline where
9
//!   a glyph should be rendered as well as the suggested spacing above and below it.
10
//!
11
//! * Glyph metrics: these apply to single glyphs. For example, the advance
12
//!   width value describes the distance between two consecutive glyphs on a line.
13
//!
14
//! ### Selecting an "instance"
15
//! Both global and glyph specific metrics accept two additional pieces of information
16
//! to select the desired instance of a font:
17
//! * Size: represented by the [Size] type, this determines the scaling factor that is
18
//!   applied to all metrics.
19
//! * Normalized variation coordinates: represented by the [LocationRef] type,
20
//!   these define the position in design space for a variable font. For a non-variable
21
//!   font, these coordinates are ignored and you can pass [LocationRef::default()]
22
//!   as an argument for this parameter.
23
//!
24
25
use read_fonts::{
26
    tables::{
27
        glyf::Glyf, gvar::SelectedGvar, hmtx::LongMetric, hvar::Hvar, loca::Loca,
28
        os2::SelectionFlags,
29
    },
30
    types::{BigEndian, Fixed, GlyphId},
31
    FontRef, TableProvider,
32
};
33
34
use crate::{
35
    outline::{pen::ControlBoundsPen, DrawSettings},
36
    MetadataProvider,
37
};
38
39
use super::instance::{LocationRef, NormalizedCoord, Size};
40
41
/// Type for a bounding box with single precision floating point coordinates.
42
pub type BoundingBox = read_fonts::types::BoundingBox<f32>;
43
44
/// Metrics for a text decoration.
45
///
46
/// This represents the suggested offset and thickness of an underline
47
/// or strikeout text decoration.
48
#[derive(Copy, Clone, PartialEq, Default, Debug)]
49
pub struct Decoration {
50
    /// Offset to the top of the decoration from the baseline.
51
    pub offset: f32,
52
    /// Thickness of the decoration.
53
    pub thickness: f32,
54
}
55
56
/// Metrics that apply to all glyphs in a font.
57
///
58
/// These are retrieved for a specific position in the design space.
59
///
60
/// This metrics here are derived from the following tables:
61
/// * [head](https://learn.microsoft.com/en-us/typography/opentype/spec/head): `units_per_em`, `bounds`
62
/// * [maxp](https://learn.microsoft.com/en-us/typography/opentype/spec/maxp): `glyph_count`, from `MAXP`
63
///   when present (ISO/IEC 14496-22:2026)
64
/// * [post](https://learn.microsoft.com/en-us/typography/opentype/spec/post): `is_monospace`, `italic_angle`, `underline`
65
/// * [OS/2](https://learn.microsoft.com/en-us/typography/opentype/spec/os2): `average_width`, `cap_height`,
66
///   `x_height`, `strikeout`, as well as the line metrics: `ascent`, `descent`, `leading` if the `USE_TYPOGRAPHIC_METRICS`
67
///   flag is set or the `hhea` line metrics are zero (the Windows metrics are used as a last resort).
68
/// * [hhea](https://learn.microsoft.com/en-us/typography/opentype/spec/hhea): `max_width`, as well as the line metrics:
69
///   `ascent`, `descent`, `leading` if they are non-zero and the `USE_TYPOGRAPHIC_METRICS` flag is not set in the OS/2 table
70
///
71
/// For variable fonts, deltas are computed using the  [MVAR](https://learn.microsoft.com/en-us/typography/opentype/spec/MVAR)
72
/// table.
73
#[derive(Copy, Clone, PartialEq, Default, Debug)]
74
pub struct Metrics {
75
    /// Number of font design units per em unit.
76
    pub units_per_em: u16,
77
    /// Number of glyphs in the font.
78
    pub glyph_count: u32,
79
    /// True if the font is not proportionally spaced.
80
    pub is_monospace: bool,
81
    /// Italic angle in counter-clockwise degrees from the vertical. Zero for upright text,
82
    /// negative for text that leans to the right.
83
    pub italic_angle: f32,
84
    /// Height of the alignment box above the baseline. Never negative.
85
    pub ascent: f32,
86
    /// Depth of the alignment box below the baseline, as a negative value.
87
    /// Never positive.
88
    pub descent: f32,
89
    /// Recommended additional spacing between lines.
90
    pub leading: f32,
91
    /// Distance from the baseline to the top of a typical English capital.
92
    pub cap_height: Option<f32>,
93
    /// Distance from the baseline to the top of the lowercase "x" or
94
    /// similar character.
95
    pub x_height: Option<f32>,
96
    /// Average width of all non-zero width characters in the font.
97
    pub average_width: Option<f32>,
98
    /// Maximum advance width of all characters in the font.
99
    pub max_width: Option<f32>,
100
    /// Metrics for an underline decoration.
101
    pub underline: Option<Decoration>,
102
    /// Metrics for a strikeout decoration.
103
    pub strikeout: Option<Decoration>,
104
    /// Union of minimum and maximum extents for all glyphs in the font.
105
    pub bounds: Option<BoundingBox>,
106
}
107
108
impl Metrics {
109
    /// Creates new metrics for the given font, size, and location in
110
    /// normalized variation space.
111
0
    pub fn new<'a>(font: &FontRef<'a>, size: Size, location: impl Into<LocationRef<'a>>) -> Self {
112
0
        let head = font.head();
113
0
        let mut metrics = Metrics {
114
0
            units_per_em: head.map(|head| head.units_per_em()).unwrap_or_default(),
115
0
            ..Default::default()
116
        };
117
0
        let coords = location.into().effective_coords();
118
0
        let scale = size.linear_scale(metrics.units_per_em);
119
0
        if let Ok(head) = font.head() {
120
0
            metrics.bounds = Some(BoundingBox {
121
0
                x_min: head.x_min() as f32 * scale,
122
0
                y_min: head.y_min() as f32 * scale,
123
0
                x_max: head.x_max() as f32 * scale,
124
0
                y_max: head.y_max() as f32 * scale,
125
0
            });
126
0
        }
127
0
        if let Ok(maxp) = font.selected_maxp() {
128
0
            metrics.glyph_count = maxp.num_glyphs();
129
0
        }
130
0
        if let Ok(post) = font.post() {
131
0
            metrics.is_monospace = post.is_fixed_pitch() != 0;
132
0
            metrics.italic_angle = post.italic_angle().to_f64() as f32;
133
0
            metrics.underline = Some(Decoration {
134
0
                offset: post.underline_position().to_i16() as f32 * scale,
135
0
                thickness: post.underline_thickness().to_i16() as f32 * scale,
136
0
            });
137
0
        }
138
0
        let hhea = font.selected_hhea();
139
0
        if let Ok(hhea) = &hhea {
140
0
            metrics.max_width = Some(hhea.advance_width_max().to_u16() as f32 * scale);
141
0
        }
142
        // Choosing proper line metrics is a challenge due to the changing
143
        // spec, backward compatibility and broken fonts.
144
        //
145
        // We use the same strategy as FreeType:
146
        // 1. Use the OS/2 metrics if the table exists and the USE_TYPO_METRICS
147
        //    flag is set.
148
        // 2. Otherwise, use the hhea metrics.
149
        // 3. If hhea metrics are zero and the OS/2 table exists:
150
        //    3a. Use the typo metrics if they are non-zero
151
        //    3b. Otherwise, use the win metrics
152
        //
153
        // See: https://github.com/freetype/freetype/blob/5c37b6406258ec0d7ab64b8619c5ea2c19e3c69a/src/sfnt/sfobjs.c#L1311
154
0
        let os2 = font.os2().ok();
155
0
        let mut used_typo_metrics = false;
156
0
        if let Some(os2) = &os2 {
157
0
            if os2
158
0
                .fs_selection()
159
0
                .contains(SelectionFlags::USE_TYPO_METRICS)
160
0
            {
161
0
                metrics.ascent = os2.s_typo_ascender() as f32 * scale;
162
0
                metrics.descent = os2.s_typo_descender() as f32 * scale;
163
0
                metrics.leading = os2.s_typo_line_gap() as f32 * scale;
164
0
                used_typo_metrics = true;
165
0
            }
166
0
            metrics.average_width = Some(os2.x_avg_char_width() as f32 * scale);
167
0
            metrics.cap_height = os2.s_cap_height().map(|v| v as f32 * scale);
168
0
            metrics.x_height = os2.sx_height().map(|v| v as f32 * scale);
169
0
            metrics.strikeout = Some(Decoration {
170
0
                offset: os2.y_strikeout_position() as f32 * scale,
171
0
                thickness: os2.y_strikeout_size() as f32 * scale,
172
0
            });
173
0
        }
174
0
        if !used_typo_metrics {
175
0
            if let Ok(hhea) = font.selected_hhea() {
176
0
                metrics.ascent = hhea.ascender().to_i16() as f32 * scale;
177
0
                metrics.descent = hhea.descender().to_i16() as f32 * scale;
178
0
                metrics.leading = hhea.line_gap().to_i16() as f32 * scale;
179
0
            }
180
0
            if metrics.ascent == 0.0 && metrics.descent == 0.0 {
181
0
                if let Some(os2) = &os2 {
182
0
                    if os2.s_typo_ascender() != 0 || os2.s_typo_descender() != 0 {
183
0
                        metrics.ascent = os2.s_typo_ascender() as f32 * scale;
184
0
                        metrics.descent = os2.s_typo_descender() as f32 * scale;
185
0
                        metrics.leading = os2.s_typo_line_gap() as f32 * scale;
186
0
                    } else {
187
0
                        metrics.ascent = os2.us_win_ascent() as f32 * scale;
188
0
                        // Win descent is always positive while other descent values are negative. Negate it
189
0
                        // to ensure we return consistent metrics.
190
0
                        metrics.descent = -(os2.us_win_descent() as f32 * scale);
191
0
                    }
192
0
                }
193
0
            }
194
0
        }
195
0
        if let (Ok(mvar), true) = (font.mvar(), !coords.is_empty()) {
196
            use read_fonts::tables::mvar::tags::*;
197
0
            let metric_delta =
198
0
                |tag| mvar.metric_delta(tag, coords).unwrap_or_default().to_f64() as f32 * scale;
199
0
            metrics.ascent += metric_delta(HASC);
200
0
            metrics.descent += metric_delta(HDSC);
201
0
            metrics.leading += metric_delta(HLGP);
202
0
            if let Some(cap_height) = &mut metrics.cap_height {
203
0
                *cap_height += metric_delta(CPHT);
204
0
            }
205
0
            if let Some(x_height) = &mut metrics.x_height {
206
0
                *x_height += metric_delta(XHGT);
207
0
            }
208
0
            if let Some(underline) = &mut metrics.underline {
209
0
                underline.offset += metric_delta(UNDO);
210
0
                underline.thickness += metric_delta(UNDS);
211
0
            }
212
0
            if let Some(strikeout) = &mut metrics.strikeout {
213
0
                strikeout.offset += metric_delta(STRO);
214
0
                strikeout.thickness += metric_delta(STRS);
215
0
            }
216
0
        }
217
        // The ascent is above the baseline and the descent below it. Some
218
        // fonts store a descender, or an ascender, with the other sign, so
219
        // each is taken by its size alone.
220
0
        metrics.ascent = metrics.ascent.abs();
221
0
        metrics.descent = -metrics.descent.abs();
222
0
        metrics
223
0
    }
224
}
225
226
/// Glyph specific metrics.
227
#[derive(Clone)]
228
pub struct GlyphMetrics<'a> {
229
    font: FontRef<'a>,
230
    size: Size,
231
    glyph_count: u32,
232
    fixed_scale: FixedScaleFactor,
233
    h_metrics: &'a [LongMetric],
234
    default_advance_width: u16,
235
    lsbs: &'a [BigEndian<i16>],
236
    hvar: Option<Hvar<'a>>,
237
    gvar: Option<SelectedGvar<'a>>,
238
    loca_glyf: Option<(Loca<'a>, Glyf<'a>)>,
239
    coords: &'a [NormalizedCoord],
240
}
241
242
impl<'a> GlyphMetrics<'a> {
243
    /// Creates new glyph metrics from the given font, size, and location in
244
    /// normalized variation space.
245
0
    pub fn new(font: &FontRef<'a>, size: Size, location: impl Into<LocationRef<'a>>) -> Self {
246
0
        let glyph_count = font
247
0
            .selected_maxp()
248
0
            .map(|maxp| maxp.num_glyphs())
249
0
            .unwrap_or_default();
250
0
        let upem = font
251
0
            .head()
252
0
            .map(|head| head.units_per_em())
253
0
            .unwrap_or_default();
254
0
        let fixed_scale = FixedScaleFactor(size.fixed_linear_scale(upem));
255
0
        let coords = location.into().effective_coords();
256
0
        let (h_metrics, default_advance_width, lsbs) = font
257
0
            .h_glyph_metric_records()
258
0
            .map(|hmtx| {
259
0
                let h_metrics = hmtx.long_metrics();
260
0
                let default_advance_width = h_metrics.last().map(|m| m.advance.get()).unwrap_or(0);
261
0
                let lsbs = hmtx.side_bearings();
262
0
                (h_metrics, default_advance_width, lsbs)
263
0
            })
264
0
            .unwrap_or_default();
265
0
        let hvar = font.hvar().ok();
266
0
        let gvar = font.selected_gvar().ok();
267
0
        let loca_glyf = font
268
0
            .selected_glyf_loca(None)
269
0
            .ok()
270
0
            .map(|(glyf, loca)| (loca, glyf));
271
0
        Self {
272
0
            font: font.clone(),
273
0
            size,
274
0
            glyph_count,
275
0
            fixed_scale,
276
0
            h_metrics,
277
0
            default_advance_width,
278
0
            lsbs,
279
0
            hvar,
280
0
            gvar,
281
0
            loca_glyf,
282
0
            coords,
283
0
        }
284
0
    }
285
286
    /// Returns the number of available glyphs in the font.
287
0
    pub fn glyph_count(&self) -> u32 {
288
0
        self.glyph_count
289
0
    }
290
291
    /// Returns the advance width for the specified glyph.
292
    ///
293
    /// If normalized coordinates were provided when constructing glyph metrics and
294
    /// an `HVAR` table is present, applies the appropriate delta.
295
    /// A varied advance below zero is clamped to zero before scaling.
296
    ///
297
    /// Returns `None` if `glyph_id >= self.glyph_count()` or the underlying font
298
    /// data is invalid.
299
0
    pub fn advance_width(&self, glyph_id: GlyphId) -> Option<f32> {
300
0
        if glyph_id.to_u32() >= self.glyph_count {
301
0
            return None;
302
0
        }
303
0
        let mut advance = self
304
0
            .h_metrics
305
0
            .get(glyph_id.to_u32() as usize)
306
0
            .map(|metric| metric.advance())
307
0
            .unwrap_or(self.default_advance_width) as i32;
308
0
        if let Some(hvar) = &self.hvar {
309
0
            advance += hvar
310
0
                .advance_delta(glyph_id, self.coords)
311
                // The delta is exact, and this rounds it to a whole design
312
                // unit before it is added.
313
                // https://github.com/freetype/freetype/blob/7838c78f53f206ac5b8e9cefde548aa81cb00cf4/src/truetype/ttgxvar.c#L1027
314
0
                .map(|delta| delta.to_i32())
315
0
                .unwrap_or(0);
316
0
        } else if self.gvar.is_some() {
317
0
            advance += self.metric_deltas_from_gvar(glyph_id).unwrap_or_default()[1];
318
0
        }
319
0
        Some(self.fixed_scale.apply(advance.max(0)))
320
0
    }
321
322
    /// Returns the left side bearing for the specified glyph.
323
    ///
324
    /// If normalized coordinates were provided when constructing glyph metrics and
325
    /// an `HVAR` table is present, applies the appropriate delta.
326
    ///
327
    /// Returns `None` if `glyph_id >= self.glyph_count()` or the underlying font
328
    /// data is invalid.
329
0
    pub fn left_side_bearing(&self, glyph_id: GlyphId) -> Option<f32> {
330
0
        if glyph_id.to_u32() >= self.glyph_count {
331
0
            return None;
332
0
        }
333
0
        let gid_index = glyph_id.to_u32() as usize;
334
0
        let mut lsb = self
335
0
            .h_metrics
336
0
            .get(gid_index)
337
0
            .map(|metric| metric.side_bearing())
338
0
            .unwrap_or_else(|| {
339
0
                self.lsbs
340
0
                    .get(gid_index.saturating_sub(self.h_metrics.len()))
341
0
                    .map(|lsb| lsb.get())
342
0
                    .unwrap_or_default()
343
0
            }) as i32;
344
0
        if let Some(hvar) = &self.hvar {
345
0
            lsb += hvar
346
0
                .lsb_delta(glyph_id, self.coords)
347
                // The delta is exact, and this rounds it to a whole design
348
                // unit before it is added.
349
                // https://github.com/freetype/freetype/blob/7838c78f53f206ac5b8e9cefde548aa81cb00cf4/src/truetype/ttgxvar.c#L1027
350
0
                .map(|delta| delta.to_i32())
351
0
                .unwrap_or(0);
352
0
        } else if self.gvar.is_some() {
353
0
            lsb += self.metric_deltas_from_gvar(glyph_id).unwrap_or_default()[0];
354
0
        }
355
0
        Some(self.fixed_scale.apply(lsb))
356
0
    }
357
358
    /// Returns the bounding box for the specified glyph.
359
    ///
360
    /// Returns `None` if `glyph_id >= self.glyph_count()`, the underlying font
361
    /// data is invalid.
362
0
    pub fn bounds(&self, glyph_id: GlyphId) -> Option<BoundingBox> {
363
0
        if self.gvar.is_some() || self.font.cff().ok().is_some() || self.font.cff2().ok().is_some()
364
        {
365
0
            return self.bounds_from_outline(glyph_id);
366
0
        }
367
0
        let (loca, glyf) = self.loca_glyf.as_ref()?;
368
0
        Some(match loca.get(glyph_id, glyf)?.glyph() {
369
0
            Some(glyph) => BoundingBox {
370
0
                x_min: self.fixed_scale.apply(glyph.x_min() as i32),
371
0
                y_min: self.fixed_scale.apply(glyph.y_min() as i32),
372
0
                x_max: self.fixed_scale.apply(glyph.x_max() as i32),
373
0
                y_max: self.fixed_scale.apply(glyph.y_max() as i32),
374
0
            },
375
            // Empty glyphs have an empty bounding box
376
0
            None => BoundingBox::default(),
377
        })
378
0
    }
379
}
380
381
impl GlyphMetrics<'_> {
382
0
    pub(crate) fn glyf_y_min(&self, glyph_id: GlyphId) -> Option<i16> {
383
0
        let (loca, glyf) = self.loca_glyf.as_ref()?;
384
0
        let entry = loca.get(glyph_id, glyf)?;
385
0
        let glyph = entry.glyph()?;
386
0
        (glyph.number_of_contours() != 0).then(|| glyph.y_min())
387
0
    }
388
389
0
    fn metric_deltas_from_gvar(&self, glyph_id: GlyphId) -> Option<[i32; 2]> {
390
0
        let (loca, glyf) = self.loca_glyf.as_ref()?;
391
0
        let mut deltas =
392
0
            self.gvar
393
0
                .as_ref()?
394
0
                .phantom_point_deltas(glyf, loca, self.coords, glyph_id)?;
395
0
        deltas[1] -= deltas[0];
396
0
        Some([deltas[0], deltas[1]].map(|delta| delta.x.to_i32()))
397
0
    }
398
399
0
    fn bounds_from_outline(&self, glyph_id: GlyphId) -> Option<BoundingBox> {
400
0
        if let Some(outline) = self.font.outline_glyphs().get(glyph_id) {
401
0
            let settings = DrawSettings::unhinted(self.size, self.coords);
402
0
            let mut pen = ControlBoundsPen::default();
403
0
            outline.draw(settings, &mut pen).ok()?;
404
0
            pen.bounding_box()
405
        } else {
406
0
            None
407
        }
408
0
    }
409
}
410
411
#[derive(Copy, Clone)]
412
struct FixedScaleFactor(Fixed);
413
414
impl FixedScaleFactor {
415
    #[inline(always)]
416
0
    fn apply(self, value: i32) -> f32 {
417
        // Match FreeType metric scaling
418
        // <https://gitlab.freedesktop.org/freetype/freetype/-/blob/80a507a6b8e3d2906ad2c8ba69329bd2fb2a85ef/src/base/ftadvanc.c#L50>
419
0
        self.0
420
0
            .mul_div(Fixed::from_bits(value), Fixed::from_bits(64))
421
0
            .to_f32()
422
0
    }
423
}
424
425
#[cfg(test)]
426
mod tests {
427
    use super::*;
428
    use font_test_data::{NOTO_SANS_JP_CFF, SIMPLE_GLYF, VAZIRMATN_VAR};
429
    use read_fonts::FontRef;
430
431
    /// Returns Tinos with the sign of its `hhea` descender flipped, as some
432
    /// fonts store it.
433
    fn positive_descender() -> Vec<u8> {
434
        let mut data = font_test_data::TINOS_SUBSET.to_vec();
435
        let font = FontRef::new(font_test_data::TINOS_SUBSET).unwrap();
436
        let record = font
437
            .table_directory()
438
            .table_records()
439
            .iter()
440
            .find(|record| record.tag() == read_fonts::types::Tag::new(b"hhea"))
441
            .unwrap();
442
        // The descender follows the version and the ascender.
443
        let at = record.offset() as usize + 6;
444
        let descender = i16::from_be_bytes([data[at], data[at + 1]]);
445
        assert!(descender < 0);
446
        data[at..at + 2].copy_from_slice(&(-descender).to_be_bytes());
447
        data
448
    }
449
450
    // A descender stored positive is read below the baseline.
451
    #[test]
452
    fn descenders_stored_positive_are_below_the_baseline() {
453
        let flipped = positive_descender();
454
        let original = FontRef::new(font_test_data::TINOS_SUBSET).unwrap();
455
        let flipped = FontRef::new(&flipped).unwrap();
456
        let size = Size::new(16.0);
457
        let original = Metrics::new(&original, size, LocationRef::default());
458
        let flipped = Metrics::new(&flipped, size, LocationRef::default());
459
        assert!(flipped.descent < 0.0);
460
        assert_eq!(
461
            (flipped.ascent, flipped.descent, flipped.leading),
462
            (original.ascent, original.descent, original.leading)
463
        );
464
    }
465
466
    #[test]
467
    fn extended_glyph_metrics_with_any_outline_tag() {
468
        use font_test_data::extended::metrics_font;
469
        for outline in [None, Some(*b"glyf"), Some(*b"GLYF")] {
470
            for legacy in [false, true] {
471
                let data = metrics_font(true, legacy, outline);
472
                let font = FontRef::new(&data).unwrap();
473
                let global = font.metrics(Size::unscaled(), LocationRef::default());
474
                assert_eq!(global.glyph_count, 70002);
475
                assert_eq!(global.ascent, 800.0);
476
                assert_eq!(global.descent, -200.0);
477
                let metrics = font.glyph_metrics(Size::unscaled(), LocationRef::default());
478
                assert_eq!(metrics.glyph_count(), 70002);
479
                for gid in [0, 65535, 65536, 69999] {
480
                    let glyph = GlyphId::new(gid);
481
                    assert_eq!(
482
                        metrics.advance_width(glyph),
483
                        Some((1000 + gid % 100) as f32)
484
                    );
485
                    assert_eq!(
486
                        metrics.left_side_bearing(glyph),
487
                        Some(-((gid % 300) as f32))
488
                    );
489
                }
490
                assert_eq!(metrics.advance_width(GlyphId::new(70001)), Some(1099.0));
491
                assert_eq!(metrics.left_side_bearing(GlyphId::new(70001)), Some(-11.0));
492
                assert_eq!(metrics.advance_width(GlyphId::new(70002)), None);
493
            }
494
        }
495
    }
496
497
    #[test]
498
    fn metrics() {
499
        let font = FontRef::new(SIMPLE_GLYF).unwrap();
500
        let metrics = font.metrics(Size::unscaled(), LocationRef::default());
501
        let expected = Metrics {
502
            units_per_em: 1024,
503
            glyph_count: 3,
504
            bounds: Some(BoundingBox {
505
                x_min: 51.0,
506
                y_min: -250.0,
507
                x_max: 998.0,
508
                y_max: 950.0,
509
            }),
510
            average_width: Some(1275.0),
511
            max_width: None,
512
            x_height: Some(512.0),
513
            cap_height: Some(717.0),
514
            is_monospace: false,
515
            italic_angle: 0.0,
516
            ascent: 950.0,
517
            descent: -250.0,
518
            leading: 0.0,
519
            underline: None,
520
            strikeout: Some(Decoration {
521
                offset: 307.0,
522
                thickness: 51.0,
523
            }),
524
        };
525
        assert_eq!(metrics, expected);
526
    }
527
528
    #[test]
529
    fn metrics_missing_os2() {
530
        let font = FontRef::new(VAZIRMATN_VAR).unwrap();
531
        let metrics = font.metrics(Size::unscaled(), LocationRef::default());
532
        let expected = Metrics {
533
            units_per_em: 2048,
534
            glyph_count: 4,
535
            bounds: Some(BoundingBox {
536
                x_min: 29.0,
537
                y_min: 0.0,
538
                x_max: 1310.0,
539
                y_max: 1847.0,
540
            }),
541
            average_width: None,
542
            max_width: Some(1336.0),
543
            x_height: None,
544
            cap_height: None,
545
            is_monospace: false,
546
            italic_angle: 0.0,
547
            ascent: 2100.0,
548
            descent: -1100.0,
549
            leading: 0.0,
550
            underline: None,
551
            strikeout: None,
552
        };
553
        assert_eq!(metrics, expected);
554
    }
555
556
    #[test]
557
    fn glyph_metrics() {
558
        let font = FontRef::new(VAZIRMATN_VAR).unwrap();
559
        let glyph_metrics = font.glyph_metrics(Size::unscaled(), LocationRef::default());
560
        // (advance_width, lsb) in glyph order
561
        let expected = &[
562
            (908.0, 100.0),
563
            (1336.0, 29.0),
564
            (1336.0, 29.0),
565
            (633.0, 57.0),
566
        ];
567
        let result = (0..4)
568
            .map(|i| {
569
                let gid = GlyphId::new(i as u32);
570
                let advance_width = glyph_metrics.advance_width(gid).unwrap();
571
                let lsb = glyph_metrics.left_side_bearing(gid).unwrap();
572
                (advance_width, lsb)
573
            })
574
            .collect::<Vec<_>>();
575
        assert_eq!(expected, &result[..]);
576
    }
577
578
    /// Asserts that the results generated with Size::unscaled() and
579
    /// Size::new(upem) are equal.
580
    ///
581
    /// See <https://github.com/googlefonts/fontations/issues/590#issuecomment-1711595882>
582
    #[test]
583
    fn glyph_metrics_unscaled_matches_upem_scale() {
584
        let font = FontRef::new(VAZIRMATN_VAR).unwrap();
585
        let upem = font.head().unwrap().units_per_em() as f32;
586
        let unscaled_metrics = font.glyph_metrics(Size::unscaled(), LocationRef::default());
587
        let upem_metrics = font.glyph_metrics(Size::new(upem), LocationRef::default());
588
        for i in 0..unscaled_metrics.glyph_count() {
589
            let gid = GlyphId::new(i);
590
            assert_eq!(
591
                unscaled_metrics.advance_width(gid),
592
                upem_metrics.advance_width(gid)
593
            );
594
            assert_eq!(
595
                unscaled_metrics.left_side_bearing(gid),
596
                upem_metrics.left_side_bearing(gid)
597
            );
598
        }
599
    }
600
601
    #[test]
602
    fn glyph_metrics_var() {
603
        let font = FontRef::new(VAZIRMATN_VAR).unwrap();
604
        let coords = &[NormalizedCoord::from_f32(-0.8)];
605
        let glyph_metrics = font.glyph_metrics(Size::unscaled(), LocationRef::new(coords));
606
        // (advance_width, lsb) in glyph order
607
        let expected = &[
608
            (908.0, 100.0),
609
            (1246.0, 29.0),
610
            (1246.0, 29.0),
611
            (556.0, 57.0),
612
        ];
613
        let result = (0..4)
614
            .map(|i| {
615
                let gid = GlyphId::new(i as u32);
616
                let advance_width = glyph_metrics.advance_width(gid).unwrap();
617
                let lsb = glyph_metrics.left_side_bearing(gid).unwrap();
618
                (advance_width, lsb)
619
            })
620
            .collect::<Vec<_>>();
621
        assert_eq!(expected, &result[..]);
622
623
        // Check bounds
624
        let coords = &[NormalizedCoord::from_f32(-1.0)];
625
        let glyph_metrics = font.glyph_metrics(Size::unscaled(), LocationRef::new(coords));
626
        let bounds = glyph_metrics.bounds(GlyphId::new(1)).unwrap();
627
        assert_eq!(
628
            bounds,
629
            BoundingBox {
630
                x_min: 33.0,
631
                y_min: 0.0,
632
                x_max: 1189.0,
633
                y_max: 1456.0
634
            }
635
        );
636
    }
637
638
    #[test]
639
    fn negative_varied_advances_are_clamped_before_scaling() {
640
        let font = FontRef::new(font_test_data::NEGATIVE_VAR_ADVANCES).unwrap();
641
        for (coord, expected) in [
642
            (0.0, [500.0, 500.0]),
643
            // Skrifa rounds the delta before adding it to the base width.
644
            (0.5, [200.0, 551.0]),
645
            (1.0, [0.0, 601.0]),
646
        ] {
647
            let coords = [NormalizedCoord::from_f32(coord)];
648
            for size in [
649
                Size::unscaled(),
650
                Size::new(-2000.0),
651
                Size::new(0.0),
652
                Size::new(500.0),
653
                Size::new(2000.0),
654
            ] {
655
                let scale = size.linear_scale(1000);
656
                let metrics = font.glyph_metrics(size, LocationRef::new(&coords));
657
                let mut gvar_metrics = metrics.clone();
658
                gvar_metrics.hvar = None;
659
                for (i, gid) in [GlyphId::new(1), GlyphId::new(2)].into_iter().enumerate() {
660
                    assert_eq!(metrics.advance_width(gid), Some(expected[i] * scale));
661
                    assert_eq!(gvar_metrics.advance_width(gid), Some(expected[i] * scale));
662
                }
663
                assert_eq!(metrics.advance_width(GlyphId::new(3)), None);
664
            }
665
        }
666
    }
667
668
    #[test]
669
    fn glyph_metrics_cff() {
670
        let font = FontRef::new(NOTO_SANS_JP_CFF).unwrap();
671
        let glyph_metrics = font.glyph_metrics(Size::unscaled(), LocationRef::default());
672
        let bounds = glyph_metrics.bounds(GlyphId::new(34)).unwrap();
673
        assert_eq!(
674
            bounds,
675
            BoundingBox {
676
                x_min: 4.0,
677
                y_min: 0.0,
678
                x_max: 604.0,
679
                y_max: 733.0
680
            }
681
        );
682
    }
683
684
    #[test]
685
    fn glyph_metrics_missing_hvar() {
686
        let font = FontRef::new(VAZIRMATN_VAR).unwrap();
687
        let glyph_count = font.maxp().unwrap().num_glyphs();
688
        // Test a few different locations in variation space
689
        for coord in [-1.0, -0.8, 0.0, 0.75, 1.0] {
690
            let coords = &[NormalizedCoord::from_f32(coord)];
691
            let location = LocationRef::new(coords);
692
            let glyph_metrics = font.glyph_metrics(Size::unscaled(), location);
693
            let mut glyph_metrics_no_hvar = glyph_metrics.clone();
694
            // Setting hvar to None forces use of gvar for metric deltas
695
            glyph_metrics_no_hvar.hvar = None;
696
            for gid in 0..glyph_count {
697
                let gid = GlyphId::from(gid);
698
                assert_eq!(
699
                    glyph_metrics.advance_width(gid),
700
                    glyph_metrics_no_hvar.advance_width(gid)
701
                );
702
                assert_eq!(
703
                    glyph_metrics.left_side_bearing(gid),
704
                    glyph_metrics_no_hvar.left_side_bearing(gid)
705
                );
706
            }
707
        }
708
    }
709
710
    /// Ensure our fixed point scaling code matches FreeType for advances.
711
    ///
712
    /// <https://github.com/googlefonts/fontations/issues/590>
713
    #[test]
714
    fn match_freetype_glyph_metric_scaling() {
715
        // fontations:
716
        // gid: 36 advance: 15.33600044250488281250 gid: 68 advance: 13.46399974822998046875 gid: 47 advance: 12.57600021362304687500 gid: 79 advance: 6.19199991226196289062
717
        // ft:
718
        // gid: 36 advance: 15.33595275878906250000 gid: 68 advance: 13.46395874023437500000 gid: 47 advance: 12.57595825195312500000 gid: 79 advance: 6.19198608398437500000
719
        // with font.setSize(24);
720
        //
721
        // Raw advances for gids 36, 68, 47, and 79 in NotoSans-Regular
722
        let font_unit_advances = [639, 561, 524, 258];
723
        #[allow(clippy::excessive_precision)]
724
        let scaled_advances = [
725
            15.33595275878906250000,
726
            13.46395874023437500000,
727
            12.57595825195312500000,
728
            6.19198608398437500000,
729
        ];
730
        let fixed_scale = FixedScaleFactor(Size::new(24.0).fixed_linear_scale(1000));
731
        for (font_unit_advance, expected_scaled_advance) in
732
            font_unit_advances.iter().zip(scaled_advances)
733
        {
734
            let scaled_advance = fixed_scale.apply(*font_unit_advance);
735
            assert_eq!(scaled_advance, expected_scaled_advance);
736
        }
737
    }
738
}