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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#![deny(missing_docs)]
use core::convert::TryFrom;
use alloc::vec::Vec;
use alloc::boxed::Box;
use super::{
BitDepth, Bitmap, BitmapGlyph, EncapsulatedBitmap, EncapsulatedFormat, Metrics, OriginOffset,
};
use crate::binary::read::{CheckIndex, ReadArray, ReadBinaryDep, ReadCtxt, ReadScope};
use crate::binary::U32Be;
use crate::error::ParseError;
use crate::tag;
pub struct Sbix<'a> {
pub flags: u16,
pub strikes: Vec<SbixStrike<'a>>,
}
pub struct SbixStrike<'a> {
scope: ReadScope<'a>,
pub ppem: u16,
pub ppi: u16,
glyph_data_offsets: ReadArray<'a, U32Be>,
}
pub struct SbixGlyph<'a> {
pub origin_offset_x: i16,
pub origin_offset_y: i16,
pub graphic_type: u32,
pub data: &'a [u8],
}
impl<'a> ReadBinaryDep<'a> for Sbix<'a> {
type Args = usize;
type HostType = Self;
fn read_dep(ctxt: &mut ReadCtxt<'a>, num_glyphs: usize) -> Result<Self, ParseError> {
let scope = ctxt.scope();
let version = ctxt.read_u16be()?;
ctxt.check(version == 1)?;
let flags = ctxt.read_u16be()?;
let num_strikes = usize::try_from(ctxt.read_u32be()?)?;
let strike_offsets = ctxt.read_array::<U32Be>(num_strikes)?;
let strikes = strike_offsets
.iter()
.map(|offset| {
let offset = usize::try_from(offset)?;
scope.offset(offset).read_dep::<SbixStrike<'_>>(num_glyphs)
})
.collect::<Result<Vec<_>, _>>()?;
Ok(Sbix { flags, strikes })
}
}
impl<'a> ReadBinaryDep<'a> for SbixStrike<'a> {
type Args = usize;
type HostType = Self;
fn read_dep(ctxt: &mut ReadCtxt<'a>, num_glyphs: usize) -> Result<Self, ParseError> {
let scope = ctxt.scope();
let ppem = ctxt.read_u16be()?;
let ppi = ctxt.read_u16be()?;
let glyph_data_offsets = ctxt.read_array::<U32Be>(num_glyphs + 1)?;
Ok(SbixStrike {
scope,
ppem,
ppi,
glyph_data_offsets,
})
}
}
impl<'a> ReadBinaryDep<'a> for SbixGlyph<'a> {
type Args = usize;
type HostType = Self;
fn read_dep(ctxt: &mut ReadCtxt<'a>, length: usize) -> Result<Self, ParseError> {
let origin_offset_x = ctxt.read_i16be()?;
let origin_offset_y = ctxt.read_i16be()?;
let graphic_type = ctxt.read_u32be()?;
let data = ctxt.read_slice(length - 8)?;
Ok(SbixGlyph {
origin_offset_x,
origin_offset_y,
graphic_type,
data,
})
}
}
impl<'a> Sbix<'a> {
pub fn find_strike(
&self,
glyph_index: u16,
target_ppem: u16,
_max_bit_depth: BitDepth,
) -> Option<&SbixStrike<'a>> {
let target_ppem = i32::from(target_ppem);
let candidates = self
.strikes
.iter()
.filter(|strike| strike.contains_glyph(glyph_index));
let mut best = None;
for strike in candidates {
let strike_ppem = i32::from(strike.ppem);
let difference = strike_ppem - target_ppem;
match best {
Some((current_best_difference, _)) => {
if super::bigger_or_closer_to_zero(strike_ppem, current_best_difference) {
best = Some((difference, strike))
}
}
None => best = Some((difference, strike)),
}
}
best.map(|(_, strike)| strike)
}
}
impl<'a> SbixStrike<'a> {
pub fn read_glyph(&self, glyph_index: u16) -> Result<Option<SbixGlyph<'a>>, ParseError> {
let (offset, end) = self.glyph_offset_end(glyph_index)?;
match end.checked_sub(offset) {
Some(0) => {
Ok(None)
}
Some(length) => {
let data = self.scope.offset_length(offset, length)?;
data.read_dep::<SbixGlyph<'_>>(length).map(Some)
}
None => Err(ParseError::BadOffset),
}
}
fn glyph_offset_end(&self, glyph_index: u16) -> Result<(usize, usize), ParseError> {
let glyph_index = usize::from(glyph_index);
self.glyph_data_offsets.check_index(glyph_index)?;
self.glyph_data_offsets.check_index(glyph_index + 1)?;
let offset = usize::try_from(self.glyph_data_offsets.get_item(glyph_index))?;
let end = usize::try_from(self.glyph_data_offsets.get_item(glyph_index + 1))?;
Ok((offset, end))
}
fn contains_glyph(&self, glyph_index: u16) -> bool {
self.glyph_offset_end(glyph_index)
.map(|(offset, end)| end.checked_sub(offset).unwrap_or(0) > 0)
.unwrap_or(false)
}
}
impl<'a> From<(&SbixStrike<'a>, &SbixGlyph<'a>)> for BitmapGlyph {
fn from((strike, glyph): (&SbixStrike<'a>, &SbixGlyph<'a>)) -> Self {
let encapsulated = EncapsulatedBitmap {
format: EncapsulatedFormat::from(glyph.graphic_type),
data: Box::from(glyph.data),
};
BitmapGlyph {
bitmap: Bitmap::Encapsulated(encapsulated),
metrics: Metrics::HmtxVmtx(OriginOffset::from(glyph)),
ppem_x: Some(strike.ppem),
ppem_y: Some(strike.ppem),
}
}
}
impl From<u32> for EncapsulatedFormat {
fn from(tag: u32) -> Self {
match tag {
tag::JPG => EncapsulatedFormat::Jpeg,
tag::PNG => EncapsulatedFormat::Png,
tag::TIFF => EncapsulatedFormat::Tiff,
_ => EncapsulatedFormat::Other(tag),
}
}
}
impl From<&SbixGlyph<'_>> for OriginOffset {
fn from(glyph: &SbixGlyph<'_>) -> Self {
OriginOffset {
x: glyph.origin_offset_x,
y: glyph.origin_offset_y,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::binary::read::ReadScope;
use crate::font_data::FontData;
use crate::tables::{FontTableProvider, MaxpTable};
use crate::tag;
use crate::tests::read_fixture;
#[test]
fn test_read_sbix() {
let buffer = read_fixture("tests/fonts/woff1/chromacheck-sbix.woff");
let scope = ReadScope::new(&buffer);
let font_file = scope
.read::<FontData<'_>>()
.expect("unable to parse font file");
let table_provider = font_file
.table_provider(0)
.expect("unable to create font provider");
let maxp_data = table_provider
.read_table_data(tag::MAXP)
.expect("unable to read maxp table data");
let maxp = ReadScope::new(&maxp_data).read::<MaxpTable>().unwrap();
let sbix_data = table_provider
.read_table_data(tag::SBIX)
.expect("unable to read sbix table data");
let sbix = ReadScope::new(&sbix_data)
.read_dep::<Sbix<'_>>(usize::try_from(maxp.num_glyphs).unwrap())
.unwrap();
assert_eq!(sbix.flags, 1);
assert_eq!(sbix.strikes.len(), 1);
let strike = &sbix.strikes[0];
assert_eq!(strike.ppem, 300);
assert_eq!(strike.ppi, 72);
let glyphs = (0..maxp.num_glyphs)
.map(|glyph_index| strike.read_glyph(glyph_index))
.collect::<Result<Vec<_>, _>>()
.expect("unable to read glyph");
assert_eq!(glyphs.len(), 2);
assert!(glyphs[0].is_none());
if let Some(ref glyph) = glyphs[1] {
assert_eq!(glyph.origin_offset_x, 0);
assert_eq!(glyph.origin_offset_y, 0);
assert_eq!(glyph.graphic_type, tag::PNG);
assert_eq!(glyph.data.len(), 224);
assert_eq!(*glyph.data.last().unwrap(), 0x82);
} else {
panic!("expected Some(SbixGlyph) got None");
}
}
}