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