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
use std::mem;
use crate::{NameID, Result, Tape, Value};
#[derive(Clone, Debug)]
pub enum NamingTable {
Format0(NamingTable0),
Format1(NamingTable1),
}
table! {
#[doc = "A naming table in format 0."]
pub NamingTable0 {
format (u16), count (u16), offset (u16), records (Vec<Record>) |this, tape| { tape.take_given(this.count as usize)
},
data (Vec<u8>) |this, tape| {
this.read_data(tape)
},
}
}
table! {
#[doc = "A naming table in format 1."]
pub NamingTable1 {
format (u16), count (u16), offset (u16), records (Vec<Record>) |this, tape| { tape.take_given(this.count as usize)
},
language_count (u16), languages (Vec<Language>) |this, tape| { tape.take_given(this.language_count as usize)
},
data (Vec<u8>) |this, tape| {
this.read_data(tape)
},
}
}
table! {
#[doc = "A record of a naming table."]
#[derive(Copy)]
#[repr(C)]
pub Record { platform_id (u16 ), encoding_id (u16 ), language_id (u16 ), name_id (NameID), length (u16 ), offset (u16 ), }
}
table! {
#[doc = "A language-tag record of a naming table."]
#[derive(Copy)]
#[repr(C)]
pub Language { length (u16), offset (u16), }
}
#[derive(Clone, Copy, Debug)]
pub enum PredefinedName {
CopyrightNotice = 0,
FontFamilyName = 1,
FontSubfamilyName = 2,
UniqueFontID = 3,
FullFontName = 4,
VersionString = 5,
PostScriptFontName = 6,
Trademark = 7,
ManufacturerName = 8,
DesignerName = 9,
Description = 10,
VendorURL = 11,
DesignerURL = 12,
LicenseDescription = 13,
LicenseURL = 14,
TypographicFamilyName = 16,
TypographicSubfamilyName = 17,
CompatibleFullFontName = 18,
SampleText = 19,
PostScriptCIDFindFontName = 20,
WWSFamilyName = 21,
WWSSubfamilyName = 22,
LightBackgroundPalette = 23,
DarkBackgroundPalette = 24,
PostScriptVariationNamePrefix = 25,
}
impl NamingTable {
pub fn get<T: Into<NameID>>(&self, name_id: T) -> Option<String> {
match self {
&NamingTable::Format0(ref table) => get(&table.records, &table.data, name_id.into()),
&NamingTable::Format1(ref table) => get(&table.records, &table.data, name_id.into()),
}
}
pub fn get_all(&self) -> Vec<(NameID, Option<String>)> {
match self {
&NamingTable::Format0(ref table) => get_all(&table.records, &table.data),
&NamingTable::Format1(ref table) => get_all(&table.records, &table.data),
}
}
}
impl Value for NamingTable {
fn read<T: Tape>(tape: &mut T) -> Result<Self> {
Ok(match tape.peek::<u16>()? {
0 => NamingTable::Format0(tape.take()?),
1 => NamingTable::Format1(tape.take()?),
_ => raise!("found an unknown format of the naming table"),
})
}
}
impl NamingTable0 {
fn read_data<T: Tape>(&self, tape: &mut T) -> Result<Vec<u8>> {
let current = tape.position()?;
let above = 3 * 2 + self.records.len() * mem::size_of::<Record>();
tape.jump(current - above as u64 + self.offset as u64)?;
tape.take_bytes(compute_length(&self.records))
}
}
impl NamingTable1 {
fn read_data<T: Tape>(&self, tape: &mut T) -> Result<Vec<u8>> {
let current = tape.position()?;
let above = 4 * 2
+ self.records.len() * mem::size_of::<Record>()
+ self.languages.len() * mem::size_of::<Language>();
tape.jump(current - above as u64 + self.offset as u64)?;
tape.take_bytes(compute_length(&self.records))
}
}
impl From<PredefinedName> for NameID {
#[inline]
fn from(name: PredefinedName) -> NameID {
name as NameID
}
}
fn compute_length(records: &[Record]) -> usize {
let mut length = 0;
for record in records {
let end = record.offset + record.length;
if end > length {
length = end;
}
}
length as usize
}
#[inline]
fn decode(record: &Record, data: &[u8]) -> Option<String> {
match record.platform_id {
0 => crate::encoding::unicode::decode(data, record.encoding_id),
1 => crate::encoding::macintosh::decode(data, record.encoding_id, record.language_id),
3 => crate::encoding::windows::decode(data, record.encoding_id),
_ => None,
}
}
fn get(records: &[Record], data: &[u8], name_id: NameID) -> Option<String> {
for record in records {
if record.name_id != name_id {
continue;
}
let (offset, length) = (record.offset as usize, record.length as usize);
let string = decode(record, &data[offset..(offset + length)]);
if string.is_none() {
continue;
}
return string;
}
None
}
fn get_all(records: &[Record], data: &[u8]) -> Vec<(NameID, Option<String>)> {
let mut names = vec![];
for record in records {
let (offset, length) = (record.offset as usize, record.length as usize);
let string = decode(record, &data[offset..(offset + length)]);
names.push((record.name_id, string));
}
names
}