read_fonts/
table_provider.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
//! a trait for things that can serve font tables

use types::Tag;

use crate::{tables, FontData, FontRead, ReadError};

/// A table that has an associated tag.
///
/// This is true of top-level tables, but not their various subtables.
pub trait TopLevelTable {
    /// The table's tag.
    const TAG: Tag;
}

/// An interface for accessing tables from a font (or font-like object)
pub trait TableProvider<'a> {
    fn data_for_tag(&self, tag: Tag) -> Option<FontData<'a>>;

    fn expect_data_for_tag(&self, tag: Tag) -> Result<FontData<'a>, ReadError> {
        self.data_for_tag(tag).ok_or(ReadError::TableIsMissing(tag))
    }

    fn expect_table<T: TopLevelTable + FontRead<'a>>(&self) -> Result<T, ReadError> {
        self.expect_data_for_tag(T::TAG).and_then(FontRead::read)
    }

    fn head(&self) -> Result<tables::head::Head<'a>, ReadError> {
        self.expect_table()
    }

    fn name(&self) -> Result<tables::name::Name<'a>, ReadError> {
        self.expect_table()
    }

    fn hhea(&self) -> Result<tables::hhea::Hhea<'a>, ReadError> {
        self.expect_table()
    }

    fn vhea(&self) -> Result<tables::vhea::Vhea<'a>, ReadError> {
        self.expect_table()
    }

    fn hmtx(&self) -> Result<tables::hmtx::Hmtx<'a>, ReadError> {
        //FIXME: should we make the user pass these in?
        let num_glyphs = self.maxp().map(|maxp| maxp.num_glyphs())?;
        let number_of_h_metrics = self.hhea().map(|hhea| hhea.number_of_long_metrics())?;
        let data = self.expect_data_for_tag(tables::hmtx::Hmtx::TAG)?;
        tables::hmtx::Hmtx::read(data, number_of_h_metrics, num_glyphs)
    }

    fn hdmx(&self) -> Result<tables::hdmx::Hdmx<'a>, ReadError> {
        let num_glyphs = self.maxp().map(|maxp| maxp.num_glyphs())?;
        let data = self.expect_data_for_tag(tables::hdmx::Hdmx::TAG)?;
        tables::hdmx::Hdmx::read(data, num_glyphs)
    }

    fn vmtx(&self) -> Result<tables::vmtx::Vmtx<'a>, ReadError> {
        //FIXME: should we make the user pass these in?
        let num_glyphs = self.maxp().map(|maxp| maxp.num_glyphs())?;
        let number_of_v_metrics = self.vhea().map(|vhea| vhea.number_of_long_ver_metrics())?;
        let data = self.expect_data_for_tag(tables::vmtx::Vmtx::TAG)?;
        tables::vmtx::Vmtx::read(data, number_of_v_metrics, num_glyphs)
    }

    fn vorg(&self) -> Result<tables::vorg::Vorg<'a>, ReadError> {
        self.expect_table()
    }

    fn fvar(&self) -> Result<tables::fvar::Fvar<'a>, ReadError> {
        self.expect_table()
    }

    fn avar(&self) -> Result<tables::avar::Avar<'a>, ReadError> {
        self.expect_table()
    }

    fn hvar(&self) -> Result<tables::hvar::Hvar<'a>, ReadError> {
        self.expect_table()
    }

    fn vvar(&self) -> Result<tables::vvar::Vvar<'a>, ReadError> {
        self.expect_table()
    }

    fn mvar(&self) -> Result<tables::mvar::Mvar<'a>, ReadError> {
        self.expect_table()
    }

    fn maxp(&self) -> Result<tables::maxp::Maxp<'a>, ReadError> {
        self.expect_table()
    }

    fn os2(&self) -> Result<tables::os2::Os2<'a>, ReadError> {
        self.expect_table()
    }

    fn post(&self) -> Result<tables::post::Post<'a>, ReadError> {
        self.expect_table()
    }

    fn gasp(&self) -> Result<tables::gasp::Gasp<'a>, ReadError> {
        self.expect_table()
    }

    /// is_long can be optionally provided, if known, otherwise we look it up in head.
    fn loca(&self, is_long: impl Into<Option<bool>>) -> Result<tables::loca::Loca<'a>, ReadError> {
        let is_long = match is_long.into() {
            Some(val) => val,
            None => self.head()?.index_to_loc_format() == 1,
        };
        let data = self.expect_data_for_tag(tables::loca::Loca::TAG)?;
        tables::loca::Loca::read(data, is_long)
    }

    fn glyf(&self) -> Result<tables::glyf::Glyf<'a>, ReadError> {
        self.expect_table()
    }

    fn gvar(&self) -> Result<tables::gvar::Gvar<'a>, ReadError> {
        self.expect_table()
    }

    fn cvar(&self) -> Result<tables::cvar::Cvar<'a>, ReadError> {
        self.expect_table()
    }

    fn cff(&self) -> Result<tables::cff::Cff<'a>, ReadError> {
        self.expect_table()
    }

    fn cff2(&self) -> Result<tables::cff2::Cff2<'a>, ReadError> {
        self.expect_table()
    }

    fn cmap(&self) -> Result<tables::cmap::Cmap<'a>, ReadError> {
        self.expect_table()
    }

    fn gdef(&self) -> Result<tables::gdef::Gdef<'a>, ReadError> {
        self.expect_table()
    }

    fn gpos(&self) -> Result<tables::gpos::Gpos<'a>, ReadError> {
        self.expect_table()
    }

    fn gsub(&self) -> Result<tables::gsub::Gsub<'a>, ReadError> {
        self.expect_table()
    }

    fn feat(&self) -> Result<tables::feat::Feat<'a>, ReadError> {
        self.expect_table()
    }

    fn ltag(&self) -> Result<tables::ltag::Ltag<'a>, ReadError> {
        self.expect_table()
    }

    fn ankr(&self) -> Result<tables::ankr::Ankr<'a>, ReadError> {
        self.expect_table()
    }

    fn colr(&self) -> Result<tables::colr::Colr<'a>, ReadError> {
        self.expect_table()
    }

    fn cpal(&self) -> Result<tables::cpal::Cpal<'a>, ReadError> {
        self.expect_table()
    }

    fn cblc(&self) -> Result<tables::cblc::Cblc<'a>, ReadError> {
        self.expect_table()
    }

    fn cbdt(&self) -> Result<tables::cbdt::Cbdt<'a>, ReadError> {
        self.expect_table()
    }

    fn eblc(&self) -> Result<tables::eblc::Eblc<'a>, ReadError> {
        self.expect_table()
    }

    fn ebdt(&self) -> Result<tables::ebdt::Ebdt<'a>, ReadError> {
        self.expect_table()
    }

    fn sbix(&self) -> Result<tables::sbix::Sbix<'a>, ReadError> {
        // should we make the user pass this in?
        let num_glyphs = self.maxp().map(|maxp| maxp.num_glyphs())?;
        let data = self.expect_data_for_tag(tables::sbix::Sbix::TAG)?;
        tables::sbix::Sbix::read(data, num_glyphs)
    }

    fn stat(&self) -> Result<tables::stat::Stat<'a>, ReadError> {
        self.expect_table()
    }

    fn svg(&self) -> Result<tables::svg::Svg<'a>, ReadError> {
        self.expect_table()
    }

    fn varc(&self) -> Result<tables::varc::Varc<'a>, ReadError> {
        self.expect_table()
    }

    fn ift(&self) -> Result<tables::ift::Ift<'a>, ReadError> {
        self.expect_data_for_tag(tables::ift::IFT_TAG)
            .and_then(FontRead::read)
    }

    fn iftx(&self) -> Result<tables::ift::Ift<'a>, ReadError> {
        self.expect_data_for_tag(tables::ift::IFTX_TAG)
            .and_then(FontRead::read)
    }

    fn meta(&self) -> Result<tables::meta::Meta<'a>, ReadError> {
        self.expect_table()
    }
}

#[cfg(test)]
mod tests {

    use super::*;

    /// https://github.com/googlefonts/fontations/issues/105
    #[test]
    fn bug_105() {
        // serve some dummy versions of the tables used to compute hmtx. The only
        // fields that matter are maxp::num_glyphs and hhea::number_of_h_metrics,
        // everything else is zero'd out
        struct DummyProvider;
        impl TableProvider<'static> for DummyProvider {
            fn data_for_tag(&self, tag: Tag) -> Option<FontData<'static>> {
                if tag == Tag::new(b"maxp") {
                    Some(FontData::new(&[
                        0, 0, 0x50, 0, // version 0.5
                        0, 3, // num_glyphs = 3
                    ]))
                } else if tag == Tag::new(b"hhea") {
                    Some(FontData::new(&[
                        0, 1, 0, 0, // version 1.0
                        0, 0, 0, 0, // ascender/descender
                        0, 0, 0, 0, // line gap/advance width
                        0, 0, 0, 0, // min left/right side bearing
                        0, 0, 0, 0, // x_max, caret_slope_rise
                        0, 0, 0, 0, // caret_slope_run, caret_offset
                        0, 0, 0, 0, // reserved1/2
                        0, 0, 0, 0, // reserved 3/4
                        0, 0, 0, 1, // metric format, number_of_h_metrics
                    ]))
                } else if tag == Tag::new(b"hmtx") {
                    Some(FontData::new(&[
                        0, 4, 0, 6, // LongHorMetric: 4, 6
                        0, 30, 0, 111, // two lsb entries
                    ]))
                } else {
                    None
                }
            }
        }

        let number_of_h_metrics = DummyProvider.hhea().unwrap().number_of_long_metrics();
        let num_glyphs = DummyProvider.maxp().unwrap().num_glyphs();
        let hmtx = DummyProvider.hmtx().unwrap();

        assert_eq!(number_of_h_metrics, 1);
        assert_eq!(num_glyphs, 3);
        assert_eq!(hmtx.h_metrics().len(), 1);
        assert_eq!(hmtx.left_side_bearings().len(), 2);
    }
}