Coverage Report

Created: 2026-01-17 07:01

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ttf-parser/src/tables/vvar.rs
Line
Count
Source
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
916
    pub fn parse(data: &'a [u8]) -> Option<Self> {
24
916
        let mut s = Stream::new(data);
25
26
916
        let version = s.read::<u32>()?;
27
911
        if version != 0x00010000 {
28
203
            return None;
29
708
        }
30
31
708
        let variation_store_offset = s.read::<Offset32>()?;
32
703
        let var_store_s = Stream::new_at(data, variation_store_offset.to_usize())?;
33
612
        let variation_store = ItemVariationStore::parse(var_store_s)?;
34
35
        Some(Table {
36
347
            data,
37
347
            variation_store,
38
347
            advance_height_mapping_offset: s.read::<Option<Offset32>>()?,
39
336
            tsb_mapping_offset: s.read::<Option<Offset32>>()?,
40
323
            bsb_mapping_offset: s.read::<Option<Offset32>>()?,
41
312
            vorg_mapping_offset: s.read::<Option<Offset32>>()?,
42
        })
43
916
    }
44
45
    /// Returns the advance height offset for a glyph.
46
    #[inline]
47
11
    pub fn advance_offset(
48
11
        &self,
49
11
        glyph_id: GlyphId,
50
11
        coordinates: &[NormalizedCoordinate],
51
11
    ) -> Option<f32> {
52
11
        let (outer_idx, inner_idx) = if let Some(offset) = self.advance_height_mapping_offset {
53
11
            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
            (0, glyph_id.0)
60
        };
61
62
10
        self.variation_store
63
10
            .parse_delta(outer_idx, inner_idx, coordinates)
64
11
    }
<ttf_parser::tables::vvar::Table>::advance_offset
Line
Count
Source
47
11
    pub fn advance_offset(
48
11
        &self,
49
11
        glyph_id: GlyphId,
50
11
        coordinates: &[NormalizedCoordinate],
51
11
    ) -> Option<f32> {
52
11
        let (outer_idx, inner_idx) = if let Some(offset) = self.advance_height_mapping_offset {
53
11
            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
            (0, glyph_id.0)
60
        };
61
62
10
        self.variation_store
63
10
            .parse_delta(outer_idx, inner_idx, coordinates)
64
11
    }
Unexecuted instantiation: <ttf_parser::tables::vvar::Table>::advance_offset
65
66
    /// Returns the top side bearing offset for a glyph.
67
    #[inline]
68
11
    pub fn top_side_bearing_offset(
69
11
        &self,
70
11
        glyph_id: GlyphId,
71
11
        coordinates: &[NormalizedCoordinate],
72
11
    ) -> Option<f32> {
73
11
        let set_data = self.data.get(self.tsb_mapping_offset?.to_usize()..)?;
74
2
        self.side_bearing_offset(glyph_id, coordinates, set_data)
75
11
    }
<ttf_parser::tables::vvar::Table>::top_side_bearing_offset
Line
Count
Source
68
11
    pub fn top_side_bearing_offset(
69
11
        &self,
70
11
        glyph_id: GlyphId,
71
11
        coordinates: &[NormalizedCoordinate],
72
11
    ) -> Option<f32> {
73
11
        let set_data = self.data.get(self.tsb_mapping_offset?.to_usize()..)?;
74
2
        self.side_bearing_offset(glyph_id, coordinates, set_data)
75
11
    }
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
11
    pub fn bottom_side_bearing_offset(
80
11
        &self,
81
11
        glyph_id: GlyphId,
82
11
        coordinates: &[NormalizedCoordinate],
83
11
    ) -> Option<f32> {
84
11
        let set_data = self.data.get(self.bsb_mapping_offset?.to_usize()..)?;
85
2
        self.side_bearing_offset(glyph_id, coordinates, set_data)
86
11
    }
87
88
    /// Returns the vertical origin offset for a glyph.
89
    #[inline]
90
11
    pub fn vertical_origin_offset(
91
11
        &self,
92
11
        glyph_id: GlyphId,
93
11
        coordinates: &[NormalizedCoordinate],
94
11
    ) -> Option<f32> {
95
11
        let set_data = self.data.get(self.vorg_mapping_offset?.to_usize()..)?;
96
10
        self.side_bearing_offset(glyph_id, coordinates, set_data)
97
11
    }
Unexecuted instantiation: <ttf_parser::tables::vvar::Table>::vertical_origin_offset
<ttf_parser::tables::vvar::Table>::vertical_origin_offset
Line
Count
Source
90
11
    pub fn vertical_origin_offset(
91
11
        &self,
92
11
        glyph_id: GlyphId,
93
11
        coordinates: &[NormalizedCoordinate],
94
11
    ) -> Option<f32> {
95
11
        let set_data = self.data.get(self.vorg_mapping_offset?.to_usize()..)?;
96
10
        self.side_bearing_offset(glyph_id, coordinates, set_data)
97
11
    }
98
99
14
    fn side_bearing_offset(
100
14
        &self,
101
14
        glyph_id: GlyphId,
102
14
        coordinates: &[NormalizedCoordinate],
103
14
        set_data: &[u8],
104
14
    ) -> Option<f32> {
105
14
        let (outer_idx, inner_idx) = DeltaSetIndexMap::new(set_data).map(glyph_id.0 as u32)?;
106
8
        self.variation_store
107
8
            .parse_delta(outer_idx, inner_idx, coordinates)
108
14
    }
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
}