/
lib.rs
558 lines (501 loc) · 18 KB
/
lib.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
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
mod tables;
mod utils;
use std::collections::HashMap;
use std::convert::TryFrom;
use std::io::{self, Cursor, Read};
use std::rc::Rc;
use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
use tables::{FontTable, Glyph, NamedTable};
use utils::limit_read::LimitRead;
#[derive(Debug, PartialEq, Clone)]
pub struct OpenTypeFont {
sfnt_version: SfntVersion,
os2_table: tables::os2::Os2Table,
cmap_table: tables::cmap::CmapTable,
glyf_table: tables::glyf::GlyfTable,
head_table: tables::head::HeadTable,
hhea_table: tables::hhea::HheaTable,
hmtx_table: tables::hmtx::HmtxTable,
loca_table: tables::loca::LocaTable,
maxp_table: tables::maxp::MaxpTable,
name_table: tables::name::NameTable,
post_table: tables::post::PostTable,
}
#[derive(Debug, PartialEq, Clone)]
struct CmapSubtable {
platform_id: u16,
encoding_id: u16,
subtable: Rc<tables::cmap::Subtable>,
}
impl OpenTypeFont {
pub fn from_slice(data: impl AsRef<[u8]>) -> Result<Self, io::Error> {
let mut cursor = Cursor::new(data.as_ref());
let offset_table = OffsetTable::unpack(&mut cursor, ())?;
let head_table = offset_table.unpack_required_table("head", (), &mut cursor)?;
let hhea_table = offset_table.unpack_required_table("hhea", (), &mut cursor)?;
let maxp_table = offset_table.unpack_required_table("maxp", (), &mut cursor)?;
let loca_table =
offset_table.unpack_required_table("loca", (&head_table, &maxp_table), &mut cursor)?;
Ok(OpenTypeFont {
sfnt_version: offset_table.sfnt_version,
os2_table: offset_table.unpack_required_table("OS/2", (), &mut cursor)?,
cmap_table: offset_table.unpack_required_table("cmap", (), &mut cursor)?,
glyf_table: offset_table.unpack_required_table("glyf", &loca_table, &mut cursor)?,
hmtx_table: offset_table.unpack_required_table(
"hmtx",
(&hhea_table, &maxp_table),
&mut cursor,
)?,
loca_table,
head_table,
hhea_table,
maxp_table,
name_table: offset_table.unpack_required_table("name", (), &mut cursor)?,
post_table: offset_table.unpack_required_table("post", (), &mut cursor)?,
})
}
// TODO: return u32?
pub fn glyph_id(&self, codepoint: u32) -> Option<u16> {
self.cmap_table
.encoding_records
.first()
.and_then(|record| record.subtable.glyph_id(codepoint))
}
pub fn subset(&self, text: &str) -> Self {
let subtable = match self.cmap_table.encoding_records.first() {
Some(r) => r.subtable.clone(),
// TODO: error instead?
None => return self.clone(),
};
let glyphs = text
.chars()
.filter_map(|c| {
subtable
.glyph_id(u32::from(c))
.map(|index| (index, u32::from(c)))
})
.fold(HashMap::new(), |mut glyphs, (i, c)| {
let glyph = glyphs.entry(i).or_insert_with(|| Glyph {
index: i,
code_points: Vec::with_capacity(1),
});
glyph.code_points.push(c);
glyphs
})
.into_iter()
.map(|(_, g)| g)
.collect::<Vec<_>>();
let glyphs = self.glyf_table.expand_composite_glyphs(&glyphs);
let os2_table = self.os2_table.subset(&glyphs, ()).into_owned();
let cmap_table = self.cmap_table.subset(&glyphs, ()).into_owned();
let glyf_table = self.glyf_table.subset(&glyphs, ()).into_owned();
let loca_table = self.loca_table.subset(&glyphs, &glyf_table).into_owned();
let head_table = self
.head_table
.subset(&glyphs, (&glyf_table, &loca_table))
.into_owned();
let hmtx_table = self.hmtx_table.subset(&glyphs, ()).into_owned();
let hhea_table = self
.hhea_table
.subset(&glyphs, (&head_table, &hmtx_table))
.into_owned();
let maxp_table = self.maxp_table.subset(&glyphs, ()).into_owned();
let name_table = self.name_table.subset(&glyphs, ()).into_owned();
let post_table = self.post_table.subset(&glyphs, ()).into_owned();
OpenTypeFont {
sfnt_version: self.sfnt_version,
os2_table,
cmap_table,
glyf_table,
head_table,
hhea_table,
hmtx_table,
loca_table,
maxp_table,
name_table,
post_table,
}
}
/// Note: currently skips all other tables of the font that are not known to the library.
pub fn to_writer(&self, wr: impl io::Write) -> Result<(), io::Error> {
let mut writer = FontWriter::new(10);
writer.pack(&self.os2_table)?;
writer.pack(&self.cmap_table)?;
writer.pack(&self.glyf_table)?;
let check_sum_adjustment_offset = writer.offset() + 8;
writer.pack(&self.head_table)?;
writer.pack(&self.hhea_table)?;
writer.pack(&self.hmtx_table)?;
writer.pack(&self.loca_table)?;
writer.pack(&self.maxp_table)?;
writer.pack(&self.name_table)?;
writer.pack(&self.post_table)?;
writer.finish(self.sfnt_version, check_sum_adjustment_offset, wr)?;
Ok(())
}
}
struct FontWriter {
tables: Vec<TableRecord>,
buffer: Vec<u8>,
}
impl FontWriter {
fn new(len: usize) -> Self {
FontWriter {
tables: Vec::with_capacity(len),
buffer: Vec::new(), // TODO: guess a starting size?
}
}
fn offset_table_len(&self) -> usize {
12 + self.tables.capacity() * 16
}
fn offset(&self) -> usize {
self.offset_table_len() + self.buffer.len()
}
fn pack<'a, T, UD, SD>(&mut self, table: &T) -> Result<(), io::Error>
where
T: FontTable<'a, UnpackDep = UD, SubsetDep = SD> + NamedTable,
{
if self.tables.len() == self.tables.capacity() {
return Err(io::Error::new(
io::ErrorKind::Other,
format!(
"Cannot write another table, already wrote {} tables",
self.tables.len()
),
));
}
let start = self.buffer.len();
table.pack(&mut self.buffer)?;
let len = self.buffer.len() - start;
self.buffer
.resize(self.buffer.len() + self.buffer.len() % 4, 0); // align to 4 bytes
self.tables.push(TableRecord {
tag: T::name().to_string(),
check_sum: check_sum(&self.buffer[start..]),
offset: u32::try_from(self.offset_table_len() + start)
.ok()
.unwrap_or(u32::MAX),
length: u32::try_from(len).ok().unwrap_or(u32::MAX),
});
Ok(())
}
fn finish(
mut self,
sfnt_version: SfntVersion,
check_sum_adjustment_offset: usize,
mut wr: impl io::Write,
) -> Result<(), io::Error> {
let num_tables = u16::try_from(self.tables.len()).ok().unwrap_or(u16::MAX);
let x = 2u16.pow((num_tables as f32).log2() as u32);
let search_range = x * 16;
let entry_selector = (x as f32).log2() as u16;
let range_shift = num_tables * 16 - search_range;
let check_sum_adjustment_offset = check_sum_adjustment_offset - self.offset_table_len();
let offset_table = OffsetTable {
sfnt_version,
num_tables,
search_range,
entry_selector,
range_shift,
tables: self.tables,
};
let mut offset_data = Vec::new();
offset_table.pack(&mut offset_data)?;
// calculate and write head.check_sum_adjustment
let mut check_sum_adjustment = check_sum(Cursor::new(&offset_data));
for record in &offset_table.tables {
check_sum_adjustment = check_sum_adjustment.saturating_add(record.check_sum);
}
let check_sum_adjustment = 0xB1B0AFBAu32.saturating_sub(check_sum_adjustment);
// inject check_sum_adjustment into head table data
(&mut self.buffer[check_sum_adjustment_offset..])
.write_u32::<BigEndian>(check_sum_adjustment)?;
wr.write_all(&offset_data)?;
wr.write_all(&self.buffer)?;
Ok(())
}
}
fn check_sum(mut data: impl io::Read) -> u32 {
let mut sum = 0u32;
while let Ok(n) = data.read_u32::<BigEndian>() {
sum = sum.saturating_add(n);
}
sum
}
/// This table contains a dictionary of all font tables included in the file.
/// See spec:
/// - https://docs.microsoft.com/en-us/typography/opentype/spec/otff
/// - https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6.html
#[derive(Debug, PartialEq)]
struct OffsetTable {
/// OpenType fonts that contain TrueType outlines should use the value of 0x00010000. OpenType
/// fonts containing CFF data (version 1 or 2) should use 0x4F54544F ('OTTO', when
/// re-interpreted as a Tag).
sfnt_version: SfntVersion,
/// Number of tables.
num_tables: u16,
/// (Maximum power of 2 <= numTables) x 16.
search_range: u16,
/// Log2(maximum power of 2 <= numTables).
entry_selector: u16,
/// NumTables x 16-searchRange.
range_shift: u16,
/// Table records of the front. Expected to be ordered ascending by their tag.
tables: Vec<TableRecord>,
}
impl OffsetTable {
fn get_table_record(&self, tag: &str) -> Option<&TableRecord> {
self.tables
.binary_search_by(|r| r.tag.as_str().cmp(tag))
.ok()
.and_then(|i| self.tables.get(i))
}
fn unpack_table<'a, T, R, UD, SD>(
&self,
tag: &str,
dep: UD,
cursor: &mut Cursor<R>,
) -> Result<Option<T>, io::Error>
where
R: io::Read + AsRef<[u8]>,
T: FontTable<'a, UnpackDep = UD, SubsetDep = SD>,
{
// TODO: return Option for non-required tables?
let record = match self.get_table_record(tag) {
Some(record) => record,
None => return Ok(None),
};
cursor.set_position(record.offset as u64);
let mut limit_read = Cursor::new(LimitRead::from_cursor(cursor, record.length as usize));
Ok(Some(T::unpack(&mut limit_read, dep)?))
}
fn unpack_required_table<'a, T, R, UD, SD>(
&self,
tag: &str,
dep: UD,
cursor: &mut Cursor<R>,
) -> Result<T, io::Error>
where
R: io::Read + AsRef<[u8]>,
T: FontTable<'a, UnpackDep = UD, SubsetDep = SD>,
{
self.unpack_table::<T, R, UD, SD>(tag, dep, cursor)?
.ok_or_else(|| io::Error::new(io::ErrorKind::Other, format!("{} table missing", tag)))
}
}
impl<'a> FontTable<'a> for OffsetTable {
type UnpackDep = ();
type SubsetDep = ();
fn unpack<R: io::Read + AsRef<[u8]>>(
mut rd: &mut Cursor<R>,
_: Self::UnpackDep,
) -> Result<Self, io::Error> {
let sfnt_version = SfntVersion::unpack(&mut rd, ())?;
let num_tables = rd.read_u16::<BigEndian>()?;
let search_range = rd.read_u16::<BigEndian>()?;
let entry_selector = rd.read_u16::<BigEndian>()?;
let range_shift = rd.read_u16::<BigEndian>()?;
let mut tables = Vec::with_capacity(num_tables as usize);
for _ in 0..num_tables {
tables.push(TableRecord::unpack(&mut rd, ())?);
}
Ok(OffsetTable {
sfnt_version,
num_tables,
search_range,
entry_selector,
range_shift,
tables,
})
}
fn pack<W: io::Write>(&self, mut wr: &mut W) -> Result<(), io::Error> {
self.sfnt_version.pack(&mut wr)?;
wr.write_u16::<BigEndian>(self.num_tables)?;
wr.write_u16::<BigEndian>(self.search_range)?;
wr.write_u16::<BigEndian>(self.entry_selector)?;
wr.write_u16::<BigEndian>(self.range_shift)?;
for table in &self.tables {
table.pack(&mut wr)?;
}
Ok(())
}
}
#[derive(Debug, PartialEq, Clone, Copy)]
enum SfntVersion {
TrueType,
CFF,
}
impl<'a> FontTable<'a> for SfntVersion {
type UnpackDep = ();
type SubsetDep = ();
fn unpack<R: io::Read + AsRef<[u8]>>(
rd: &mut Cursor<R>,
_: Self::UnpackDep,
) -> Result<Self, io::Error> {
match rd.read_u32::<BigEndian>()? {
0x00010000 => Ok(SfntVersion::TrueType),
0x4F54544F => Ok(SfntVersion::CFF),
v => Err(io::Error::new(
io::ErrorKind::Other,
format!("Unknown sfnt_version {}", v),
)),
}
}
fn pack<W: io::Write>(&self, wr: &mut W) -> Result<(), io::Error> {
wr.write_u32::<BigEndian>(match self {
SfntVersion::TrueType => 0x00010000,
SfntVersion::CFF => 0x4F54544F,
})?;
Ok(())
}
}
#[derive(Debug, PartialEq)]
struct TableRecord {
tag: String,
check_sum: u32,
offset: u32,
length: u32,
}
impl<'a> FontTable<'a> for TableRecord {
type UnpackDep = ();
type SubsetDep = ();
fn unpack<R: io::Read + AsRef<[u8]>>(
rd: &mut Cursor<R>,
_: Self::UnpackDep,
) -> Result<Self, io::Error> {
let mut tag = [0; 4];
rd.read_exact(&mut tag)?;
Ok(TableRecord {
tag: String::from_utf8_lossy(&tag).to_string(),
check_sum: rd.read_u32::<BigEndian>()?,
offset: rd.read_u32::<BigEndian>()?,
length: rd.read_u32::<BigEndian>()?,
})
}
fn pack<W: io::Write>(&self, wr: &mut W) -> Result<(), io::Error> {
wr.write_all(&self.tag.as_bytes())?;
wr.write_u32::<BigEndian>(self.check_sum)?;
wr.write_u32::<BigEndian>(self.offset)?;
wr.write_u32::<BigEndian>(self.length)?;
Ok(())
}
}
#[cfg(test)]
mod test {
use std::io::Cursor;
use super::*;
use pretty_assertions::assert_eq;
#[test]
fn test_offset_table_encode_decode() {
let data = include_bytes!("../tests/fonts/Iosevka/iosevka-regular.ttf").to_vec();
let table = OffsetTable::unpack(&mut Cursor::new(&data[..]), ()).unwrap();
assert_eq!(table.sfnt_version, SfntVersion::TrueType);
assert_eq!(table.num_tables, 17);
assert_eq!(table.search_range, 256);
assert_eq!(table.entry_selector, 4);
assert_eq!(table.range_shift, 16);
// should include at least the minimal necessary tables for a font to function correctly
assert!(
table.tables.iter().any(|t| t.tag == "OS/2"),
"OS/2 table missing"
);
assert!(
table.tables.iter().any(|t| t.tag == "cmap"),
"cmap table missing"
);
assert!(
table.tables.iter().any(|t| t.tag == "glyf"),
"glyf table missing"
);
assert!(
table.tables.iter().any(|t| t.tag == "head"),
"head table missing"
);
assert!(
table.tables.iter().any(|t| t.tag == "hhea"),
"hhea table missing"
);
assert!(
table.tables.iter().any(|t| t.tag == "hmtx"),
"hmtx table missing"
);
assert!(
table.tables.iter().any(|t| t.tag == "loca"),
"loca table missing"
);
assert!(
table.tables.iter().any(|t| t.tag == "maxp"),
"maxp table missing"
);
assert!(
table.tables.iter().any(|t| t.tag == "name"),
"name table missing"
);
assert!(
table.tables.iter().any(|t| t.tag == "post"),
"post table missing"
);
// re-pack and compare
let mut buffer = Vec::new();
table.pack(&mut buffer).unwrap();
assert_eq!(
OffsetTable::unpack(&mut Cursor::new(&buffer[..]), ()).unwrap(),
table
);
}
#[test]
fn test_writing_font() {
let data = include_bytes!("../tests/fonts/Iosevka/iosevka-regular.ttf");
let font = OpenTypeFont::from_slice(&data[..]).unwrap();
let mut data = Vec::new();
font.to_writer(&mut data).unwrap();
let mut rewritten_font = OpenTypeFont::from_slice(data).unwrap();
assert_ne!(
rewritten_font.head_table.check_sum_adjustment,
font.head_table.check_sum_adjustment
);
rewritten_font.head_table.check_sum_adjustment = font.head_table.check_sum_adjustment;
assert_eq!(
rewritten_font, font,
"Re-written font does not match original font"
);
}
#[test]
fn test_reparse_subset() {
let data = include_bytes!("../tests/fonts/Iosevka/iosevka-regular.ttf");
let font = OpenTypeFont::from_slice(&data[..]).unwrap();
let subset = font.subset("abA");
let mut data = Vec::new();
subset.to_writer(&mut data).unwrap();
let mut rewritten_subset = OpenTypeFont::from_slice(data).unwrap();
assert_ne!(
rewritten_subset.head_table.check_sum_adjustment,
font.head_table.check_sum_adjustment
);
rewritten_subset.head_table.check_sum_adjustment = font.head_table.check_sum_adjustment;
let OpenTypeFont {
sfnt_version,
os2_table,
cmap_table,
glyf_table,
head_table,
hhea_table,
hmtx_table,
loca_table,
maxp_table,
name_table,
post_table,
} = rewritten_subset;
assert_eq!(sfnt_version, subset.sfnt_version);
assert_eq!(os2_table, subset.os2_table);
assert_eq!(cmap_table, subset.cmap_table);
assert_eq!(glyf_table, subset.glyf_table);
assert_eq!(head_table, subset.head_table);
assert_eq!(hhea_table, subset.hhea_table);
assert_eq!(hmtx_table, subset.hmtx_table);
assert_eq!(loca_table, subset.loca_table);
assert_eq!(maxp_table, subset.maxp_table);
assert_eq!(name_table, subset.name_table);
assert_eq!(post_table, subset.post_table);
}
}