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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
//! `OS/2` — Windows metrics: weight/width classes, Unicode ranges, metrics.
use crate::error::{ParseError, RawTag};
use crate::reader::{be_i16_at, be_u16_at, be_u32_at};
pub(crate) const TAG: RawTag = *b"OS/2";
/// Weight class constants (usWeightClass).
pub mod weight {
/// Thin (100).
pub const THIN: u16 = 100;
/// ExtraLight (200).
pub const EXTRA_LIGHT: u16 = 200;
/// Light (300).
pub const LIGHT: u16 = 300;
/// Regular (400).
pub const REGULAR: u16 = 400;
/// Medium (500).
pub const MEDIUM: u16 = 500;
/// SemiBold (600).
pub const SEMI_BOLD: u16 = 600;
/// Bold (700).
pub const BOLD: u16 = 700;
/// ExtraBold (800).
pub const EXTRA_BOLD: u16 = 800;
/// Black (900).
pub const BLACK: u16 = 900;
}
/// Width class constants (usWidthClass).
pub mod width {
/// Ultra-condensed (1).
pub const ULTRA_CONDENSED: u16 = 1;
/// Extra-condensed (2).
pub const EXTRA_CONDENSED: u16 = 2;
/// Condensed (3).
pub const CONDENSED: u16 = 3;
/// Semi-condensed (4).
pub const SEMI_CONDENSED: u16 = 4;
/// Medium/normal (5).
pub const MEDIUM: u16 = 5;
/// Semi-expanded (6).
pub const SEMI_EXPANDED: u16 = 6;
/// Expanded (7).
pub const EXPANDED: u16 = 7;
/// Extra-expanded (8).
pub const EXTRA_EXPANDED: u16 = 8;
/// Ultra-expanded (9).
pub const ULTRA_EXPANDED: u16 = 9;
}
/// Zero-copy view over the `OS/2` table.
///
/// Field availability is length-driven (v0 = 78 bytes, v1 = 86, v2 = 96,
/// v5 = 100), so partial tables parse instead of failing wholesale.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Os2Table<'a> {
data: &'a [u8],
}
impl<'a> Os2Table<'a> {
/// Parse and validate the `OS/2` table bytes.
///
/// # Errors
/// [`ParseError::TruncatedTable`] when shorter than the version-0 size
/// (78 bytes); [`ParseError::InvalidSfnt`] for an unknown version.
pub fn parse(data: &'a [u8]) -> Result<Self, ParseError> {
if data.len() < 78 {
return Err(ParseError::TruncatedTable {
tag: TAG,
expected: 78,
got: data.len(),
});
}
match be_u16_at(data, 0, TAG)? {
0..=5 => {}
_ => return Err(ParseError::InvalidSfnt),
}
Ok(Self { data })
}
/// Raw table bytes.
#[must_use]
pub fn as_bytes(&self) -> &'a [u8] {
self.data
}
/// Table version (0-5).
///
/// # Errors
/// Truncation (impossible post-[`parse`](Self::parse)).
pub fn version(&self) -> Result<u16, ParseError> {
be_u16_at(self.data, 0, TAG)
}
/// Average character width.
///
/// # Errors
/// Truncation (impossible post-[`parse`](Self::parse)).
pub fn avg_char_width(&self) -> Result<i16, ParseError> {
be_i16_at(self.data, 2, TAG)
}
/// Weight class (100-900; see [`weight`]).
///
/// # Errors
/// Truncation (impossible post-[`parse`](Self::parse)).
pub fn weight_class(&self) -> Result<u16, ParseError> {
be_u16_at(self.data, 4, TAG)
}
/// Width class (1-9; see [`width`]).
///
/// # Errors
/// Truncation (impossible post-[`parse`](Self::parse)).
pub fn width_class(&self) -> Result<u16, ParseError> {
be_u16_at(self.data, 6, TAG)
}
/// Embedding permissions (`fsType`).
///
/// # Errors
/// Truncation (impossible post-[`parse`](Self::parse)).
pub fn fs_type(&self) -> Result<u16, ParseError> {
be_u16_at(self.data, 8, TAG)
}
/// Classification id (`sFamilyClass`).
///
/// # Errors
/// Truncation (impossible post-[`parse`](Self::parse)).
pub fn family_class(&self) -> Result<i16, ParseError> {
be_i16_at(self.data, 30, TAG)
}
/// PANOSE classification bytes (10 bytes at offset 32).
///
/// # Errors
/// Truncation (impossible post-[`parse`](Self::parse)).
pub fn panose(&self) -> Result<&'a [u8], ParseError> {
crate::reader::slice_at(self.data, 32, 10, TAG)
}
/// Unicode range bits (`ulUnicodeRange1..4`) as four raw `u32` words;
/// bit `n` of word `w` marks range `w*32 + n`.
///
/// # Errors
/// Truncation (impossible post-[`parse`](Self::parse)).
pub fn unicode_ranges(&self) -> Result<[u32; 4], ParseError> {
Ok([
be_u32_at(self.data, 42, TAG)?,
be_u32_at(self.data, 46, TAG)?,
be_u32_at(self.data, 50, TAG)?,
be_u32_at(self.data, 54, TAG)?,
])
}
/// Four-character vendor id.
///
/// # Errors
/// Truncation (impossible post-[`parse`](Self::parse)).
pub fn vendor_id(&self) -> Result<&'a [u8], ParseError> {
crate::reader::slice_at(self.data, 58, 4, TAG)
}
/// `fsSelection` — italic/bold/regular/… selection bits.
///
/// # Errors
/// Truncation (impossible post-[`parse`](Self::parse)).
pub fn fs_selection(&self) -> Result<u16, ParseError> {
be_u16_at(self.data, 62, TAG)
}
/// Typographic ascender (v0+).
///
/// # Errors
/// [`ParseError::TruncatedTable`] on a table shorter than 78 bytes
/// (impossible post-[`parse`](Self::parse)).
pub fn typo_ascender(&self) -> Result<i16, ParseError> {
be_i16_at(self.data, 68, TAG)
}
/// Typographic descender (v0+).
///
/// # Errors
/// Truncation (impossible post-[`parse`](Self::parse)).
pub fn typo_descender(&self) -> Result<i16, ParseError> {
be_i16_at(self.data, 70, TAG)
}
/// Typographic line gap (v0+).
///
/// # Errors
/// Truncation (impossible post-[`parse`](Self::parse)).
pub fn typo_line_gap(&self) -> Result<i16, ParseError> {
be_i16_at(self.data, 72, TAG)
}
/// Windows ascent (v0+).
///
/// # Errors
/// Truncation (impossible post-[`parse`](Self::parse)).
pub fn win_ascent(&self) -> Result<u16, ParseError> {
be_u16_at(self.data, 74, TAG)
}
/// Windows descent (v0+).
///
/// # Errors
/// Truncation (impossible post-[`parse`](Self::parse)).
pub fn win_descent(&self) -> Result<u16, ParseError> {
be_u16_at(self.data, 76, TAG)
}
/// Code page range bits (`ulCodePageRange1..2`, v1+).
///
/// # Errors
/// [`ParseError::TruncatedTable`] on a pre-v1 (78-byte) table.
pub fn code_page_ranges(&self) -> Result<[u32; 2], ParseError> {
if self.data.len() < 86 {
return Err(ParseError::TruncatedTable {
tag: TAG,
expected: 86,
got: self.data.len(),
});
}
Ok([
be_u32_at(self.data, 78, TAG)?,
be_u32_at(self.data, 82, TAG)?,
])
}
/// x-height (`sxHeight`, v2+).
///
/// # Errors
/// [`ParseError::TruncatedTable`] on a pre-v2 table.
pub fn x_height(&self) -> Result<i16, ParseError> {
if self.data.len() < 96 {
return Err(ParseError::TruncatedTable {
tag: TAG,
expected: 96,
got: self.data.len(),
});
}
be_i16_at(self.data, 86, TAG)
}
/// Cap height (`sCapHeight`, v2+).
///
/// # Errors
/// [`ParseError::TruncatedTable`] on a pre-v2 table.
pub fn cap_height(&self) -> Result<i16, ParseError> {
if self.data.len() < 96 {
return Err(ParseError::TruncatedTable {
tag: TAG,
expected: 96,
got: self.data.len(),
});
}
be_i16_at(self.data, 88, TAG)
}
}
#[cfg(test)]
mod tests {
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::indexing_slicing)]
use super::Os2Table;
use crate::error::ParseError;
use alloc::vec;
use alloc::vec::Vec;
// Test harness: assertions legitimately panic; the lib target stays
// lint-clean.
fn sample(len: usize) -> Vec<u8> {
let mut d = vec![0u8; len];
d[0..2].copy_from_slice(&2u16.to_be_bytes()); // v2
d[2..4].copy_from_slice(&450i16.to_be_bytes());
d[4..6].copy_from_slice(&700u16.to_be_bytes());
d[6..8].copy_from_slice(&5u16.to_be_bytes());
d[8..10].copy_from_slice(&8u16.to_be_bytes()); // installable
d[30..32].copy_from_slice(&0x0401i16.to_be_bytes());
d[32..42].copy_from_slice(&[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
d[42..46].copy_from_slice(&0b11u32.to_be_bytes()); // ranges bit 0/1
d[58..62].copy_from_slice(b"TEST");
d[62..64].copy_from_slice(&0x40u16.to_be_bytes()); // bold
d[68..70].copy_from_slice(&800i16.to_be_bytes());
d[70..72].copy_from_slice(&(-200i16).to_be_bytes());
d[72..74].copy_from_slice(&50i16.to_be_bytes());
d[74..76].copy_from_slice(&850u16.to_be_bytes());
d[76..78].copy_from_slice(&250u16.to_be_bytes());
if len >= 86 {
d[78..82].copy_from_slice(&0x1u32.to_be_bytes());
d[82..86].copy_from_slice(&0x2u32.to_be_bytes());
}
if len >= 96 {
d[86..88].copy_from_slice(&500i16.to_be_bytes());
d[88..90].copy_from_slice(&700i16.to_be_bytes());
}
d
}
#[test]
fn v2_fields() {
let d = sample(96);
let t = Os2Table::parse(&d).unwrap();
assert_eq!(t.version().unwrap(), 2);
assert_eq!(t.avg_char_width().unwrap(), 450);
assert_eq!(t.weight_class().unwrap(), super::weight::BOLD);
assert_eq!(t.width_class().unwrap(), super::width::MEDIUM);
assert_eq!(t.fs_type().unwrap(), 8);
assert_eq!(t.family_class().unwrap(), 0x0401);
assert_eq!(t.panose().unwrap(), &[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
assert_eq!(t.unicode_ranges().unwrap()[0], 0b11);
assert_eq!(t.vendor_id().unwrap(), b"TEST");
assert_eq!(t.fs_selection().unwrap(), 0x40);
assert_eq!(t.typo_ascender().unwrap(), 800);
assert_eq!(t.typo_descender().unwrap(), -200);
assert_eq!(t.typo_line_gap().unwrap(), 50);
assert_eq!(t.win_ascent().unwrap(), 850);
assert_eq!(t.win_descent().unwrap(), 250);
assert_eq!(t.code_page_ranges().unwrap(), [1, 2]);
assert_eq!(t.x_height().unwrap(), 500);
assert_eq!(t.cap_height().unwrap(), 700);
}
#[test]
fn version_gating() {
// v0-sized table: codepage and x-height unavailable.
let d = sample(78);
let t = Os2Table::parse(&d).unwrap();
assert!(t.code_page_ranges().is_err());
assert!(t.x_height().is_err());
assert_eq!(t.typo_ascender().unwrap(), 800);
// v1-sized table.
let d = sample(86);
let t = Os2Table::parse(&d).unwrap();
assert_eq!(t.code_page_ranges().unwrap(), [1, 2]);
assert!(t.x_height().is_err());
}
#[test]
fn errors() {
assert_eq!(
Os2Table::parse(&[0u8; 77]),
Err(ParseError::TruncatedTable {
tag: *b"OS/2",
expected: 78,
got: 77
})
);
let mut d = sample(96);
d[0..2].copy_from_slice(&9u16.to_be_bytes());
assert_eq!(Os2Table::parse(&d), Err(ParseError::InvalidSfnt));
}
}