Coverage Report

Created: 2025-07-02 06:56

/src/ttf-parser/src/tables/vvar.rs
Line
Count
Source (jump to first uncovered line)
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
1.64k
    pub fn parse(data: &'a [u8]) -> Option<Self> {
24
1.64k
        let mut s = Stream::new(data);
25
26
1.64k
        let version = s.read::<u32>()?;
27
1.63k
        if version != 0x00010000 {
28
139
            return None;
29
1.49k
        }
30
31
1.49k
        let variation_store_offset = s.read::<Offset32>()?;
32
1.49k
        let var_store_s = Stream::new_at(data, variation_store_offset.to_usize())?;
33
1.39k
        let variation_store = ItemVariationStore::parse(var_store_s)?;
34
35
        Some(Table {
36
1.07k
            data,
37
1.07k
            variation_store,
38
1.07k
            advance_height_mapping_offset: s.read::<Option<Offset32>>()?,
39
1.07k
            tsb_mapping_offset: s.read::<Option<Offset32>>()?,
40
1.01k
            bsb_mapping_offset: s.read::<Option<Offset32>>()?,
41
963
            vorg_mapping_offset: s.read::<Option<Offset32>>()?,
42
        })
43
1.64k
    }
44
45
    /// Returns the advance height offset for a glyph.
46
    #[inline]
47
778
    pub fn advance_offset(
48
778
        &self,
49
778
        glyph_id: GlyphId,
50
778
        coordinates: &[NormalizedCoordinate],
51
778
    ) -> Option<f32> {
52
778
        let (outer_idx, inner_idx) = if let Some(offset) = self.advance_height_mapping_offset {
53
691
            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
87
            (0, glyph_id.0)
60
        };
61
62
284
        self.variation_store
63
284
            .parse_delta(outer_idx, inner_idx, coordinates)
64
778
    }
Unexecuted instantiation: <ttf_parser::tables::vvar::Table>::advance_offset
<ttf_parser::tables::vvar::Table>::advance_offset
Line
Count
Source
47
778
    pub fn advance_offset(
48
778
        &self,
49
778
        glyph_id: GlyphId,
50
778
        coordinates: &[NormalizedCoordinate],
51
778
    ) -> Option<f32> {
52
778
        let (outer_idx, inner_idx) = if let Some(offset) = self.advance_height_mapping_offset {
53
691
            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
87
            (0, glyph_id.0)
60
        };
61
62
284
        self.variation_store
63
284
            .parse_delta(outer_idx, inner_idx, coordinates)
64
778
    }
Unexecuted instantiation: <ttf_parser::tables::vvar::Table>::advance_offset
65
66
    /// Returns the top side bearing offset for a glyph.
67
    #[inline]
68
778
    pub fn top_side_bearing_offset(
69
778
        &self,
70
778
        glyph_id: GlyphId,
71
778
        coordinates: &[NormalizedCoordinate],
72
778
    ) -> Option<f32> {
73
778
        let set_data = self.data.get(self.tsb_mapping_offset?.to_usize()..)?;
74
362
        self.side_bearing_offset(glyph_id, coordinates, set_data)
75
778
    }
Unexecuted instantiation: <ttf_parser::tables::vvar::Table>::top_side_bearing_offset
<ttf_parser::tables::vvar::Table>::top_side_bearing_offset
Line
Count
Source
68
778
    pub fn top_side_bearing_offset(
69
778
        &self,
70
778
        glyph_id: GlyphId,
71
778
        coordinates: &[NormalizedCoordinate],
72
778
    ) -> Option<f32> {
73
778
        let set_data = self.data.get(self.tsb_mapping_offset?.to_usize()..)?;
74
362
        self.side_bearing_offset(glyph_id, coordinates, set_data)
75
778
    }
Unexecuted instantiation: <ttf_parser::tables::vvar::Table>::top_side_bearing_offset
76
77
    /// Returns the bottom side bearing offset for a glyph.
78
    #[inline]
79
778
    pub fn bottom_side_bearing_offset(
80
778
        &self,
81
778
        glyph_id: GlyphId,
82
778
        coordinates: &[NormalizedCoordinate],
83
778
    ) -> Option<f32> {
84
778
        let set_data = self.data.get(self.bsb_mapping_offset?.to_usize()..)?;
85
432
        self.side_bearing_offset(glyph_id, coordinates, set_data)
86
778
    }
Unexecuted instantiation: <ttf_parser::tables::vvar::Table>::bottom_side_bearing_offset
<ttf_parser::tables::vvar::Table>::bottom_side_bearing_offset
Line
Count
Source
79
778
    pub fn bottom_side_bearing_offset(
80
778
        &self,
81
778
        glyph_id: GlyphId,
82
778
        coordinates: &[NormalizedCoordinate],
83
778
    ) -> Option<f32> {
84
778
        let set_data = self.data.get(self.bsb_mapping_offset?.to_usize()..)?;
85
432
        self.side_bearing_offset(glyph_id, coordinates, set_data)
86
778
    }
87
88
    /// Returns the vertical origin offset for a glyph.
89
    #[inline]
90
778
    pub fn vertical_origin_offset(
91
778
        &self,
92
778
        glyph_id: GlyphId,
93
778
        coordinates: &[NormalizedCoordinate],
94
778
    ) -> Option<f32> {
95
778
        let set_data = self.data.get(self.vorg_mapping_offset?.to_usize()..)?;
96
420
        self.side_bearing_offset(glyph_id, coordinates, set_data)
97
778
    }
Unexecuted instantiation: <ttf_parser::tables::vvar::Table>::vertical_origin_offset
<ttf_parser::tables::vvar::Table>::vertical_origin_offset
Line
Count
Source
90
778
    pub fn vertical_origin_offset(
91
778
        &self,
92
778
        glyph_id: GlyphId,
93
778
        coordinates: &[NormalizedCoordinate],
94
778
    ) -> Option<f32> {
95
778
        let set_data = self.data.get(self.vorg_mapping_offset?.to_usize()..)?;
96
420
        self.side_bearing_offset(glyph_id, coordinates, set_data)
97
778
    }
98
99
1.21k
    fn side_bearing_offset(
100
1.21k
        &self,
101
1.21k
        glyph_id: GlyphId,
102
1.21k
        coordinates: &[NormalizedCoordinate],
103
1.21k
        set_data: &[u8],
104
1.21k
    ) -> Option<f32> {
105
1.21k
        let (outer_idx, inner_idx) = DeltaSetIndexMap::new(set_data).map(glyph_id.0 as u32)?;
106
951
        self.variation_store
107
951
            .parse_delta(outer_idx, inner_idx, coordinates)
108
1.21k
    }
109
}
110
111
impl core::fmt::Debug for Table<'_> {
112
0
    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
113
0
        write!(f, "Table {{ ... }}")
114
0
    }
115
}