ttf_parser/tables/
hmtx.rs

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::GlyphId;
7use crate::parser::{Stream, FromData, LazyArray16};
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::new(data);
24        Some(Metrics {
25            advance: s.read::<u16>()?,
26            side_bearing: s.read::<i16>()?,
27        })
28    }
29}
30
31
32/// A [Horizontal/Vertical Metrics Table](
33/// https://docs.microsoft.com/en-us/typography/opentype/spec/hmtx).
34///
35/// `hmtx` and `vmtx` tables has the same structure, so we're reusing the same struct for both.
36#[derive(Clone, Copy, Debug)]
37pub struct Table<'a> {
38    /// A list of metrics indexed by glyph ID.
39    pub metrics: LazyArray16<'a, Metrics>,
40    /// Side bearings for glyph IDs greater than or equal to the number of `metrics` values.
41    pub bearings: LazyArray16<'a, i16>,
42    /// Sum of long metrics + bearings.
43    pub number_of_metrics: u16,
44}
45
46impl<'a> Table<'a> {
47    /// Parses a table from raw data.
48    ///
49    /// - `number_of_metrics` is from the `hhea`/`vhea` table.
50    /// - `number_of_glyphs` is from the `maxp` table.
51    pub fn parse(
52        mut number_of_metrics: u16,
53        number_of_glyphs: NonZeroU16,
54        data: &'a [u8],
55    ) -> Option<Self> {
56        if number_of_metrics == 0 {
57            return None;
58        }
59
60        let mut s = Stream::new(data);
61        let metrics = s.read_array16::<Metrics>(number_of_metrics)?;
62
63        // 'If the number_of_metrics is less than the total number of glyphs,
64        // then that array is followed by an array for the left side bearing values
65        // of the remaining glyphs.'
66        let bearings_count = number_of_glyphs.get().checked_sub(number_of_metrics);
67        let bearings = if let Some(count) = bearings_count {
68            number_of_metrics += count;
69            s.read_array16::<i16>(count)?
70        } else {
71            LazyArray16::default()
72        };
73
74        Some(Table {
75            metrics,
76            bearings,
77            number_of_metrics,
78        })
79    }
80
81    /// Returns advance for a glyph.
82    #[inline]
83    pub fn advance(&self, glyph_id: GlyphId) -> Option<u16> {
84        if glyph_id.0 >= self.number_of_metrics {
85            return None;
86        }
87
88        if let Some(metrics) = self.metrics.get(glyph_id.0) {
89            Some(metrics.advance)
90        } else {
91            // 'As an optimization, the number of records can be less than the number of glyphs,
92            // in which case the advance value of the last record applies
93            // to all remaining glyph IDs.'
94            self.metrics.last().map(|m| m.advance)
95        }
96    }
97
98    /// Returns side bearing for a glyph.
99    #[inline]
100    pub fn side_bearing(&self, glyph_id: GlyphId) -> Option<i16> {
101        if let Some(metrics) = self.metrics.get(glyph_id.0) {
102            Some(metrics.side_bearing)
103        } else {
104            // 'If the number_of_metrics is less than the total number of glyphs,
105            // then that array is followed by an array for the side bearing values
106            // of the remaining glyphs.'
107            self.bearings.get(glyph_id.0.checked_sub(self.metrics.len())?)
108        }
109    }
110}