blob: 0eeaffb1c682fbeab58a7bd4ef3c958811042f5a (
plain)
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
|
// Copyright (c) 2018 The OTS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "stat.h"
#include "name.h"
namespace ots {
// -----------------------------------------------------------------------------
// OpenTypeSTAT
// -----------------------------------------------------------------------------
bool OpenTypeSTAT::ValidateNameId(uint16_t nameid) {
OpenTypeNAME* name = static_cast<OpenTypeNAME*>(
GetFont()->GetTypedTable(OTS_TAG_NAME));
if (!name || !name->IsValidNameId(nameid)) {
Drop("Invalid nameID: %d", nameid);
return false;
}
if ((nameid >= 26 && nameid <= 255) || nameid >= 32768) {
Warning("nameID out of range: %d", nameid);
return true;
}
return true;
}
bool OpenTypeSTAT::Parse(const uint8_t* data, size_t length) {
Buffer table(data, length);
if (!table.ReadU16(&this->majorVersion) ||
!table.ReadU16(&this->minorVersion) ||
!table.ReadU16(&this->designAxisSize) ||
!table.ReadU16(&this->designAxisCount) ||
!table.ReadU32(&this->designAxesOffset) ||
!table.ReadU16(&this->axisValueCount) ||
!table.ReadU32(&this->offsetToAxisValueOffsets) ||
!(this->minorVersion < 1 || table.ReadU16(&this->elidedFallbackNameID))) {
return Drop("Failed to read table header");
}
if (this->majorVersion != 1) {
return Drop("Unknown table version");
}
if (this->minorVersion > 2) {
Warning("Unknown minor version, downgrading to 2");
this->minorVersion = 2;
}
size_t headerEnd = table.offset();
if (this->designAxisCount == 0) {
if (this->designAxesOffset != 0) {
Warning("Unexpected non-zero designAxesOffset");
this->designAxesOffset = 0;
}
} else {
if (this->designAxisSize < sizeof(AxisRecord)) {
return Drop("Invalid designAxisSize");
}
if (this->designAxesOffset < headerEnd ||
size_t(this->designAxesOffset) > length ||
size_t(this->designAxisCount) * size_t(this->designAxisSize) >
length - size_t(this->designAxesOffset)) {
return Drop("Invalid designAxesOffset");
}
}
for (size_t i = 0; i < this->designAxisCount; i++) {
table.set_offset(this->designAxesOffset + i * this->designAxisSize);
this->designAxes.emplace_back();
auto& axis = this->designAxes[i];
if (!table.ReadU32(&axis.axisTag) ||
!table.ReadU16(&axis.axisNameID) ||
!table.ReadU16(&axis.axisOrdering)) {
return Drop("Failed to read design axis");
}
if (!CheckTag(axis.axisTag)) {
return Drop("Bad design axis tag");
}
if (!ValidateNameId(axis.axisNameID)) {
return true;
}
}
// TODO
// - check that all axes defined in fvar are covered by STAT
// - check that axisOrdering values are not duplicated (warn only)
if (this->axisValueCount == 0) {
if (this->offsetToAxisValueOffsets != 0) {
Warning("Unexpected non-zero offsetToAxisValueOffsets");
this->offsetToAxisValueOffsets = 0;
}
} else {
if (this->offsetToAxisValueOffsets < headerEnd ||
size_t(this->offsetToAxisValueOffsets) > length ||
size_t(this->axisValueCount) * sizeof(uint16_t) >
length - size_t(this->offsetToAxisValueOffsets)) {
return Drop("Invalid offsetToAxisValueOffsets");
}
}
for (size_t i = 0; i < this->axisValueCount; i++) {
table.set_offset(this->offsetToAxisValueOffsets + i * sizeof(uint16_t));
uint16_t axisValueOffset;
if (!table.ReadU16(&axisValueOffset)) {
return Drop("Failed to read axis value offset");
}
// We already checked that offsetToAxisValueOffsets doesn't exceed length,
// so this subtraction will not underflow.
if (axisValueOffset > length - this->offsetToAxisValueOffsets) {
return Drop("Invalid axis value offset");
}
table.set_offset(this->offsetToAxisValueOffsets + axisValueOffset);
uint16_t format;
if (!table.ReadU16(&format)) {
return Drop("Failed to read axis value format");
}
this->axisValues.emplace_back(format);
auto& axisValue = axisValues[i];
switch (format) {
case 1:
if (!table.ReadU16(&axisValue.format1.axisIndex) ||
!table.ReadU16(&axisValue.format1.flags) ||
!table.ReadU16(&axisValue.format1.valueNameID) ||
!table.ReadS32(&axisValue.format1.value)) {
return Drop("Failed to read axis value (format 1)");
}
if (axisValue.format1.axisIndex >= this->designAxisCount) {
return Drop("Axis index out of range");
}
if ((axisValue.format1.flags & 0xFFFCu) != 0) {
Warning("Unexpected axis value flags");
axisValue.format1.flags &= ~0xFFFCu;
}
if (!ValidateNameId(axisValue.format1.valueNameID)) {
return true;
}
break;
case 2:
if (!table.ReadU16(&axisValue.format2.axisIndex) ||
!table.ReadU16(&axisValue.format2.flags) ||
!table.ReadU16(&axisValue.format2.valueNameID) ||
!table.ReadS32(&axisValue.format2.nominalValue) ||
!table.ReadS32(&axisValue.format2.rangeMinValue) ||
!table.ReadS32(&axisValue.format2.rangeMaxValue)) {
return Drop("Failed to read axis value (format 2)");
}
if (axisValue.format2.axisIndex >= this->designAxisCount) {
return Drop("Axis index out of range");
}
if ((axisValue.format2.flags & 0xFFFCu) != 0) {
Warning("Unexpected axis value flags");
axisValue.format1.flags &= ~0xFFFCu;
}
if (!ValidateNameId(axisValue.format2.valueNameID)) {
return true;
}
if (!(axisValue.format2.rangeMinValue <= axisValue.format2.nominalValue &&
axisValue.format2.nominalValue <= axisValue.format2.rangeMaxValue)) {
Warning("Bad axis value range or nominal value");
}
break;
case 3:
if (!table.ReadU16(&axisValue.format3.axisIndex) ||
!table.ReadU16(&axisValue.format3.flags) ||
!table.ReadU16(&axisValue.format3.valueNameID) ||
!table.ReadS32(&axisValue.format3.value) ||
!table.ReadS32(&axisValue.format3.linkedValue)) {
return Drop("Failed to read axis value (format 3)");
}
if (axisValue.format3.axisIndex >= this->designAxisCount) {
return Drop("Axis index out of range");
}
if ((axisValue.format3.flags & 0xFFFCu) != 0) {
Warning("Unexpected axis value flags");
axisValue.format3.flags &= ~0xFFFCu;
}
if (!ValidateNameId(axisValue.format3.valueNameID)) {
return true;
}
break;
case 4:
if (this->minorVersion < 2) {
return Drop("Invalid table minorVersion for format 4 axis values: %d", this->minorVersion);
}
if (!table.ReadU16(&axisValue.format4.axisCount) ||
!table.ReadU16(&axisValue.format4.flags) ||
!table.ReadU16(&axisValue.format4.valueNameID)) {
return Drop("Failed to read axis value (format 4)");
}
if (axisValue.format4.axisCount > this->designAxisCount) {
return Drop("Axis count out of range");
}
if ((axisValue.format4.flags & 0xFFFCu) != 0) {
Warning("Unexpected axis value flags");
axisValue.format4.flags &= ~0xFFFCu;
}
if (!ValidateNameId(axisValue.format4.valueNameID)) {
return true;
}
for (unsigned j = 0; j < axisValue.format4.axisCount; j++) {
axisValue.format4.axisValues.emplace_back();
auto& v = axisValue.format4.axisValues[j];
if (!table.ReadU16(&v.axisIndex) ||
!table.ReadS32(&v.value)) {
return Drop("Failed to read axis value");
}
if (v.axisIndex >= this->designAxisCount) {
return Drop("Axis index out of range");
}
}
break;
default:
return Drop("Unknown axis value format");
}
}
return true;
}
bool OpenTypeSTAT::Serialize(OTSStream* out) {
off_t tableStart = out->Tell();
size_t headerSize = 5 * sizeof(uint16_t) + 2 * sizeof(uint32_t);
if (this->minorVersion >= 1) {
headerSize += sizeof(uint16_t);
}
if (this->designAxisCount == 0) {
this->designAxesOffset = 0;
} else {
this->designAxesOffset = headerSize;
}
this->designAxisSize = sizeof(AxisRecord);
if (this->axisValueCount == 0) {
this->offsetToAxisValueOffsets = 0;
} else {
if (this->designAxesOffset == 0) {
this->offsetToAxisValueOffsets = headerSize;
} else {
this->offsetToAxisValueOffsets = this->designAxesOffset + this->designAxisCount * this->designAxisSize;
}
}
if (!out->WriteU16(this->majorVersion) ||
!out->WriteU16(this->minorVersion) ||
!out->WriteU16(this->designAxisSize) ||
!out->WriteU16(this->designAxisCount) ||
!out->WriteU32(this->designAxesOffset) ||
!out->WriteU16(this->axisValueCount) ||
!out->WriteU32(this->offsetToAxisValueOffsets) ||
!(this->minorVersion < 1 || out->WriteU16(this->elidedFallbackNameID))) {
return Error("Failed to write table header");
}
if (this->designAxisCount > 0) {
if (out->Tell() - tableStart != this->designAxesOffset) {
return Error("Error computing designAxesOffset");
}
}
for (unsigned i = 0; i < this->designAxisCount; i++) {
const auto& axis = this->designAxes[i];
if (!out->WriteU32(axis.axisTag) ||
!out->WriteU16(axis.axisNameID) ||
!out->WriteU16(axis.axisOrdering)) {
return Error("Failed to write design axis");
}
}
if (this->axisValueCount > 0) {
if (out->Tell() - tableStart != this->offsetToAxisValueOffsets) {
return Error("Error computing offsetToAxisValueOffsets");
}
}
uint32_t axisValueOffset = this->axisValueCount * sizeof(uint16_t);
for (unsigned i = 0; i < this->axisValueCount; i++) {
const auto& value = this->axisValues[i];
if (!out->WriteU16(axisValueOffset)) {
return Error("Failed to write axis value offset");
}
axisValueOffset += value.Length();
}
for (unsigned i = 0; i < this->axisValueCount; i++) {
const auto& value = this->axisValues[i];
if (!out->WriteU16(value.format)) {
return Error("Failed to write axis value");
}
switch (value.format) {
case 1:
if (!out->WriteU16(value.format1.axisIndex) ||
!out->WriteU16(value.format1.flags) ||
!out->WriteU16(value.format1.valueNameID) ||
!out->WriteS32(value.format1.value)) {
return Error("Failed to write axis value");
}
break;
case 2:
if (!out->WriteU16(value.format2.axisIndex) ||
!out->WriteU16(value.format2.flags) ||
!out->WriteU16(value.format2.valueNameID) ||
!out->WriteS32(value.format2.nominalValue) ||
!out->WriteS32(value.format2.rangeMinValue) ||
!out->WriteS32(value.format2.rangeMaxValue)) {
return Error("Failed to write axis value");
}
break;
case 3:
if (!out->WriteU16(value.format3.axisIndex) ||
!out->WriteU16(value.format3.flags) ||
!out->WriteU16(value.format3.valueNameID) ||
!out->WriteS32(value.format3.value) ||
!out->WriteS32(value.format3.linkedValue)) {
return Error("Failed to write axis value");
}
break;
case 4:
if (!out->WriteU16(value.format4.axisCount) ||
!out->WriteU16(value.format4.flags) ||
!out->WriteU16(value.format4.valueNameID)) {
return Error("Failed to write axis value");
}
for (unsigned j = 0; j < value.format4.axisValues.size(); j++) {
if (!out->WriteU16(value.format4.axisValues[j].axisIndex) ||
!out->WriteS32(value.format4.axisValues[j].value)) {
return Error("Failed to write axis value");
}
}
break;
default:
return Error("Bad value format");
}
}
return true;
}
} // namespace ots
|