makepad_ttf_parser/tables/
hvar.rs

1//! A [Horizontal/Vertical Metrics Variations Table](
2//! https://docs.microsoft.com/en-us/typography/opentype/spec/hvar) implementation.
3
4use crate::delta_set::DeltaSetIndexMap;
5use crate::parser::{Offset, Offset32, Stream};
6use crate::var_store::ItemVariationStore;
7use crate::{GlyphId, NormalizedCoordinate};
8
9/// A [Horizontal/Vertical Metrics Variations Table](
10/// https://docs.microsoft.com/en-us/typography/opentype/spec/hvar).
11#[derive(Clone, Copy)]
12pub struct Table<'a> {
13    data: &'a [u8],
14    variation_store: ItemVariationStore<'a>,
15    advance_width_mapping_offset: Option<Offset32>,
16    lsb_mapping_offset: Option<Offset32>,
17}
18
19impl<'a> Table<'a> {
20    /// Parses a table from raw data.
21    pub fn parse(data: &'a [u8]) -> Option<Self> {
22        let mut s = Stream::new(data);
23
24        let version = s.read::<u32>()?;
25        if version != 0x00010000 {
26            return None;
27        }
28
29        let variation_store_offset = s.read::<Offset32>()?;
30        let var_store_s = Stream::new_at(data, variation_store_offset.to_usize())?;
31        let variation_store = ItemVariationStore::parse(var_store_s)?;
32
33        Some(Table {
34            data,
35            variation_store,
36            advance_width_mapping_offset: s.read::<Option<Offset32>>()?,
37            lsb_mapping_offset: s.read::<Option<Offset32>>()?,
38        })
39    }
40
41    /// Returns advance offset for a glyph.
42    #[inline]
43    pub fn advance_offset(
44        &self,
45        glyph_id: GlyphId,
46        coordinates: &[NormalizedCoordinate],
47    ) -> Option<f32> {
48        let (outer_idx, inner_idx) = if let Some(offset) = self.advance_width_mapping_offset {
49            DeltaSetIndexMap::new(self.data.get(offset.to_usize()..)?).map(glyph_id.0 as u32)?
50        } else {
51            // 'If there is no delta-set index mapping table for advance widths,
52            // then glyph IDs implicitly provide the indices:
53            // for a given glyph ID, the delta-set outer-level index is zero,
54            // and the glyph ID is the delta-set inner-level index.'
55            (0, glyph_id.0)
56        };
57
58        self.variation_store
59            .parse_delta(outer_idx, inner_idx, coordinates)
60    }
61
62    /// Returns side bearing offset for a glyph.
63    #[inline]
64    pub fn side_bearing_offset(
65        &self,
66        glyph_id: GlyphId,
67        coordinates: &[NormalizedCoordinate],
68    ) -> Option<f32> {
69        let set_data = self.data.get(self.lsb_mapping_offset?.to_usize()..)?;
70        let (outer_idx, inner_idx) = DeltaSetIndexMap::new(set_data).map(glyph_id.0 as u32)?;
71        self.variation_store
72            .parse_delta(outer_idx, inner_idx, coordinates)
73    }
74}
75
76impl core::fmt::Debug for Table<'_> {
77    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
78        write!(f, "Table {{ ... }}")
79    }
80}