1//! A [Horizontal/Vertical Metrics Table](
2//! https://docs.microsoft.com/en-us/typography/opentype/spec/hmtx) implementation.
3
4use core::num::NonZeroU16;
5
6use crate::parser::{FromData, LazyArray16, Stream};
7use crate::GlyphId;
8
9/// Horizontal/Vertical Metrics.
10#[derive(Clone, Copy, Debug)]
11pub struct Metrics {
12 /// Width/Height advance for `hmtx`/`vmtx`.
13 pub advance: u16,
14 /// Left/Top side bearing for `hmtx`/`vmtx`.
15 pub side_bearing: i16,
16}
17
18impl FromData for Metrics {
19 const SIZE: usize = 4;
20
21 #[inline]
22 fn parse(data: &[u8]) -> Option<Self> {
23 let mut s: Stream<'_> = Stream::new(data);
24 Some(Metrics {
25 advance: s.read::<u16>()?,
26 side_bearing: s.read::<i16>()?,
27 })
28 }
29}
30
31/// A [Horizontal/Vertical Metrics Table](
32/// https://docs.microsoft.com/en-us/typography/opentype/spec/hmtx).
33///
34/// `hmtx` and `vmtx` tables has the same structure, so we're reusing the same struct for both.
35#[derive(Clone, Copy, Debug)]
36pub struct Table<'a> {
37 /// A list of metrics indexed by glyph ID.
38 pub metrics: LazyArray16<'a, Metrics>,
39 /// Side bearings for glyph IDs greater than or equal to the number of `metrics` values.
40 pub bearings: LazyArray16<'a, i16>,
41 /// Sum of long metrics + bearings.
42 pub number_of_metrics: u16,
43}
44
45impl<'a> Table<'a> {
46 /// Parses a table from raw data.
47 ///
48 /// - `number_of_metrics` is from the `hhea`/`vhea` table.
49 /// - `number_of_glyphs` is from the `maxp` table.
50 pub fn parse(
51 mut number_of_metrics: u16,
52 number_of_glyphs: NonZeroU16,
53 data: &'a [u8],
54 ) -> Option<Self> {
55 if number_of_metrics == 0 {
56 return None;
57 }
58
59 let mut s = Stream::new(data);
60 let metrics = s.read_array16::<Metrics>(number_of_metrics)?;
61
62 // 'If the number_of_metrics is less than the total number of glyphs,
63 // then that array is followed by an array for the left side bearing values
64 // of the remaining glyphs.'
65 let bearings_count = number_of_glyphs.get().checked_sub(number_of_metrics);
66 let bearings = if let Some(count) = bearings_count {
67 number_of_metrics += count;
68 // Some malformed fonts can skip "left side bearing values"
69 // even when they are expected.
70 // Therefore if we weren't able to parser them, simply fallback to an empty array.
71 // No need to mark the whole table as malformed.
72 s.read_array16::<i16>(count).unwrap_or_default()
73 } else {
74 LazyArray16::default()
75 };
76
77 Some(Table {
78 metrics,
79 bearings,
80 number_of_metrics,
81 })
82 }
83
84 /// Returns advance for a glyph.
85 #[inline]
86 pub fn advance(&self, glyph_id: GlyphId) -> Option<u16> {
87 if glyph_id.0 >= self.number_of_metrics {
88 return None;
89 }
90
91 if let Some(metrics) = self.metrics.get(glyph_id.0) {
92 Some(metrics.advance)
93 } else {
94 // 'As an optimization, the number of records can be less than the number of glyphs,
95 // in which case the advance value of the last record applies
96 // to all remaining glyph IDs.'
97 self.metrics.last().map(|m| m.advance)
98 }
99 }
100
101 /// Returns side bearing for a glyph.
102 #[inline]
103 pub fn side_bearing(&self, glyph_id: GlyphId) -> Option<i16> {
104 if let Some(metrics) = self.metrics.get(glyph_id.0) {
105 Some(metrics.side_bearing)
106 } else {
107 // 'If the number_of_metrics is less than the total number of glyphs,
108 // then that array is followed by an array for the side bearing values
109 // of the remaining glyphs.'
110 self.bearings
111 .get(glyph_id.0.checked_sub(self.metrics.len())?)
112 }
113 }
114}
115