summaryrefslogtreecommitdiffstats
path: root/gfx/ots/src/glat.cc
blob: 23f7dfd9a8c3243bd446eaa2436d6a884b2303e4 (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
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
// Copyright (c) 2009-2017 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 "glat.h"

#include "gloc.h"
#include "mozilla/Compression.h"
#include <list>

namespace ots {

// -----------------------------------------------------------------------------
// OpenTypeGLAT_v1
// -----------------------------------------------------------------------------

bool OpenTypeGLAT_v1::Parse(const uint8_t* data, size_t length) {
  Buffer table(data, length);
  OpenTypeGLOC* gloc = static_cast<OpenTypeGLOC*>(
      GetFont()->GetTypedTable(OTS_TAG_GLOC));
  if (!gloc) {
    return DropGraphite("Required Gloc table is missing");
  }

  if (!table.ReadU32(&this->version) || this->version >> 16 != 1) {
    return DropGraphite("Failed to read version");
  }

  const std::vector<uint32_t>& locations = gloc->GetLocations();
  if (locations.empty()) {
    return DropGraphite("No locations from Gloc table");
  }
  std::list<uint32_t> unverified(locations.begin(), locations.end());
  while (table.remaining()) {
    GlatEntry entry(this);
    if (table.offset() > unverified.front()) {
      return DropGraphite("Offset check failed for a GlatEntry");
    }
    if (table.offset() == unverified.front()) {
      unverified.pop_front();
    }
    if (unverified.empty()) {
      return DropGraphite("Expected more locations");
    }
    if (!entry.ParsePart(table)) {
      return DropGraphite("Failed to read a GlatEntry");
    }
    this->entries.push_back(entry);
  }

  if (unverified.size() != 1 || unverified.front() != table.offset()) {
    return DropGraphite("%zu location(s) could not be verified", unverified.size());
  }
  if (table.remaining()) {
    return Warning("%zu bytes unparsed", table.remaining());
  }
  return true;
}

bool OpenTypeGLAT_v1::Serialize(OTSStream* out) {
  if (!out->WriteU32(this->version) ||
      !SerializeParts(this->entries, out)) {
    return Error("Failed to write table");
  }
  return true;
}

bool OpenTypeGLAT_v1::GlatEntry::ParsePart(Buffer& table) {
  if (!table.ReadU8(&this->attNum)) {
    return parent->Error("GlatEntry: Failed to read attNum");
  }
  if (!table.ReadU8(&this->num)) {
    return parent->Error("GlatEntry: Failed to read num");
  }

  //this->attributes.resize(this->num);
  for (int i = 0; i < this->num; ++i) {
    this->attributes.emplace_back();
    if (!table.ReadS16(&this->attributes[i])) {
      return parent->Error("GlatEntry: Failed to read attribute %u", i);
    }
  }
  return true;
}

bool OpenTypeGLAT_v1::GlatEntry::SerializePart(OTSStream* out) const {
  if (!out->WriteU8(this->attNum) ||
      !out->WriteU8(this->num) ||
      !SerializeParts(this->attributes, out)) {
    return parent->Error("GlatEntry: Failed to write");
  }
  return true;
}

// -----------------------------------------------------------------------------
// OpenTypeGLAT_v2
// -----------------------------------------------------------------------------

bool OpenTypeGLAT_v2::Parse(const uint8_t* data, size_t length) {
  Buffer table(data, length);
  OpenTypeGLOC* gloc = static_cast<OpenTypeGLOC*>(
      GetFont()->GetTypedTable(OTS_TAG_GLOC));
  if (!gloc) {
    return DropGraphite("Required Gloc table is missing");
  }

  if (!table.ReadU32(&this->version) || this->version >> 16 != 1) {
    return DropGraphite("Failed to read version");
  }

  const std::vector<uint32_t>& locations = gloc->GetLocations();
  if (locations.empty()) {
    return DropGraphite("No locations from Gloc table");
  }
  std::list<uint32_t> unverified(locations.begin(), locations.end());
  while (table.remaining()) {
    GlatEntry entry(this);
    if (table.offset() > unverified.front()) {
      return DropGraphite("Offset check failed for a GlatEntry");
    }
    if (table.offset() == unverified.front()) {
      unverified.pop_front();
    }
    if (unverified.empty()) {
      return DropGraphite("Expected more locations");
    }
    if (!entry.ParsePart(table)) {
      return DropGraphite("Failed to read a GlatEntry");
    }
    this->entries.push_back(entry);
  }

  if (unverified.size() != 1 || unverified.front() != table.offset()) {
    return DropGraphite("%zu location(s) could not be verified", unverified.size());
  }
  if (table.remaining()) {
    return Warning("%zu bytes unparsed", table.remaining());
  }
  return true;
}

bool OpenTypeGLAT_v2::Serialize(OTSStream* out) {
  if (!out->WriteU32(this->version) ||
      !SerializeParts(this->entries, out)) {
    return Error("Failed to write table");
  }
  return true;
}

bool OpenTypeGLAT_v2::GlatEntry::ParsePart(Buffer& table) {
  if (!table.ReadS16(&this->attNum)) {
    return parent->Error("GlatEntry: Failed to read attNum");
  }
  if (!table.ReadS16(&this->num) || this->num < 0) {
    return parent->Error("GlatEntry: Failed to read valid num");
  }

  //this->attributes.resize(this->num);
  for (int i = 0; i < this->num; ++i) {
    this->attributes.emplace_back();
    if (!table.ReadS16(&this->attributes[i])) {
      return parent->Error("GlatEntry: Failed to read attribute %u", i);
    }
  }
  return true;
}

bool OpenTypeGLAT_v2::GlatEntry::SerializePart(OTSStream* out) const {
  if (!out->WriteS16(this->attNum) ||
      !out->WriteS16(this->num) ||
      !SerializeParts(this->attributes, out)) {
    return parent->Error("GlatEntry: Failed to write");
  }
  return true;
}

// -----------------------------------------------------------------------------
// OpenTypeGLAT_v3
// -----------------------------------------------------------------------------

bool OpenTypeGLAT_v3::Parse(const uint8_t* data, size_t length,
                            bool prevent_decompression) {
  Buffer table(data, length);
  OpenTypeGLOC* gloc = static_cast<OpenTypeGLOC*>(
      GetFont()->GetTypedTable(OTS_TAG_GLOC));
  if (!gloc) {
    return DropGraphite("Required Gloc table is missing");
  }

  if (!table.ReadU32(&this->version) || this->version >> 16 != 3) {
    return DropGraphite("Failed to read version");
  }
  if (!table.ReadU32(&this->compHead)) {
    return DropGraphite("Failed to read compression header");
  }
  switch ((this->compHead & SCHEME) >> 27) {
    case 0:  // uncompressed
      break;
    case 1: {  // lz4
      if (prevent_decompression) {
        return DropGraphite("Illegal nested compression");
      }
      size_t decompressed_size = this->compHead & FULL_SIZE;
      if (decompressed_size < length) {
        return DropGraphite("Decompressed size is less than compressed size");
      }
      if (decompressed_size == 0) {
        return DropGraphite("Decompressed size is set to 0");
      }
      // decompressed table must be <= 30MB
      if (decompressed_size > 30 * 1024 * 1024) {
        return DropGraphite("Decompressed size exceeds 30MB: %gMB",
                            decompressed_size / (1024.0 * 1024.0));
      }
      std::vector<uint8_t> decompressed(decompressed_size);
      size_t outputSize = 0;
      bool ret = mozilla::Compression::LZ4::decompressPartial(
          reinterpret_cast<const char*>(data + table.offset()),
          table.remaining(),  // input buffer size (input size + padding)
          reinterpret_cast<char*>(decompressed.data()),
          decompressed.size(),  // target output size
          &outputSize);  // return output size
      if (!ret || outputSize != decompressed.size()) {
        return DropGraphite("Decompression failed");
      }
      return this->Parse(decompressed.data(), decompressed.size(), true);
    }
    default:
      return DropGraphite("Unknown compression scheme");
  }
  if (this->compHead & RESERVED) {
    Warning("Nonzero reserved");
  }

  const std::vector<uint32_t>& locations = gloc->GetLocations();
  if (locations.empty()) {
    return DropGraphite("No locations from Gloc table");
  }
  std::list<uint32_t> unverified(locations.begin(), locations.end());
  //this->entries.resize(locations.size() - 1, this);
  for (size_t i = 0; i < locations.size() - 1; ++i) {
    this->entries.emplace_back(this);
    if (table.offset() != unverified.front()) {
      return DropGraphite("Offset check failed for a GlyphAttrs");
    }
    unverified.pop_front();
    if (!this->entries[i].ParsePart(table,
                                    unverified.front() - table.offset())) {
        // unverified.front() is guaranteed to exist because of the number of
        // iterations of this loop
      return DropGraphite("Failed to read a GlyphAttrs");
    }
  }

  if (unverified.size() != 1 || unverified.front() != table.offset()) {
    return DropGraphite("%zu location(s) could not be verified", unverified.size());
  }
  if (table.remaining()) {
    return Warning("%zu bytes unparsed", table.remaining());
  }
  return true;
}

bool OpenTypeGLAT_v3::Serialize(OTSStream* out) {
  if (!out->WriteU32(this->version) ||
      !out->WriteU32(this->compHead) ||
      !SerializeParts(this->entries, out)) {
    return Error("Failed to write table");
  }
  return true;
}

bool OpenTypeGLAT_v3::GlyphAttrs::ParsePart(Buffer& table, const size_t size) {
  size_t init_offset = table.offset();
  if (parent->compHead & OCTABOXES && !octabox.ParsePart(table)) {
    // parent->flags & 0b1: octaboxes are present flag
    return parent->Error("GlyphAttrs: Failed to read octabox");
  }

  while (table.offset() < init_offset + size) {
    GlatEntry entry(parent);
    if (!entry.ParsePart(table)) {
      return parent->Error("GlyphAttrs: Failed to read a GlatEntry");
    }
    this->entries.push_back(entry);
  }
  return true;
}

bool OpenTypeGLAT_v3::GlyphAttrs::SerializePart(OTSStream* out) const {
  if ((parent->compHead & OCTABOXES && !octabox.SerializePart(out)) ||
      !SerializeParts(this->entries, out)) {
    return parent->Error("GlyphAttrs: Failed to write");
  }
  return true;
}

bool OpenTypeGLAT_v3::GlyphAttrs::
OctaboxMetrics::ParsePart(Buffer& table) {
  if (!table.ReadU16(&this->subbox_bitmap)) {
    return parent->Error("OctaboxMetrics: Failed to read subbox_bitmap");
  }
  if (!table.ReadU8(&this->diag_neg_min)) {
    return parent->Error("OctaboxMetrics: Failed to read diag_neg_min");
  }
  if (!table.ReadU8(&this->diag_neg_max) ||
      this->diag_neg_max < this->diag_neg_min) {
    return parent->Error("OctaboxMetrics: Failed to read valid diag_neg_max");
  }
  if (!table.ReadU8(&this->diag_pos_min)) {
    return parent->Error("OctaboxMetrics: Failed to read diag_pos_min");
  }
  if (!table.ReadU8(&this->diag_pos_max) ||
      this->diag_pos_max < this->diag_pos_min) {
    return parent->Error("OctaboxMetrics: Failed to read valid diag_pos_max");
  }

  unsigned subboxes_len = 0;  // count of 1's in this->subbox_bitmap
  for (uint16_t i = this->subbox_bitmap; i; i >>= 1) {
    if (i & 0b1) {
      ++subboxes_len;
    }
  }
  //this->subboxes.resize(subboxes_len, parent);
  for (unsigned i = 0; i < subboxes_len; i++) {
    this->subboxes.emplace_back(parent);
    if (!this->subboxes[i].ParsePart(table)) {
      return parent->Error("OctaboxMetrics: Failed to read subbox[%u]", i);
    }
  }
  return true;
}

bool OpenTypeGLAT_v3::GlyphAttrs::
OctaboxMetrics::SerializePart(OTSStream* out) const {
  if (!out->WriteU16(this->subbox_bitmap) ||
      !out->WriteU8(this->diag_neg_min) ||
      !out->WriteU8(this->diag_neg_max) ||
      !out->WriteU8(this->diag_pos_min) ||
      !out->WriteU8(this->diag_pos_max) ||
      !SerializeParts(this->subboxes, out)) {
    return parent->Error("OctaboxMetrics: Failed to write");
  }
  return true;
}

bool OpenTypeGLAT_v3::GlyphAttrs::OctaboxMetrics::
SubboxEntry::ParsePart(Buffer& table) {
  if (!table.ReadU8(&this->left)) {
    return parent->Error("SubboxEntry: Failed to read left");
  }
  if (!table.ReadU8(&this->right) || this->right < this->left) {
    return parent->Error("SubboxEntry: Failed to read valid right");
  }
  if (!table.ReadU8(&this->bottom)) {
    return parent->Error("SubboxEntry: Failed to read bottom");
  }
  if (!table.ReadU8(&this->top) || this->top < this->bottom) {
    return parent->Error("SubboxEntry: Failed to read valid top");
  }
  if (!table.ReadU8(&this->diag_pos_min)) {
    return parent->Error("SubboxEntry: Failed to read diag_pos_min");
  }
  if (!table.ReadU8(&this->diag_pos_max) ||
      this->diag_pos_max < this->diag_pos_min) {
    return parent->Error("SubboxEntry: Failed to read valid diag_pos_max");
  }
  if (!table.ReadU8(&this->diag_neg_min)) {
    return parent->Error("SubboxEntry: Failed to read diag_neg_min");
  }
  if (!table.ReadU8(&this->diag_neg_max) ||
      this->diag_neg_max < this->diag_neg_min) {
    return parent->Error("SubboxEntry: Failed to read valid diag_neg_max");
  }
  return true;
}

bool OpenTypeGLAT_v3::GlyphAttrs::OctaboxMetrics::
SubboxEntry::SerializePart(OTSStream* out) const {
  if (!out->WriteU8(this->left) ||
      !out->WriteU8(this->right) ||
      !out->WriteU8(this->bottom) ||
      !out->WriteU8(this->top) ||
      !out->WriteU8(this->diag_pos_min) ||
      !out->WriteU8(this->diag_pos_max) ||
      !out->WriteU8(this->diag_neg_min) ||
      !out->WriteU8(this->diag_neg_max)) {
    return parent->Error("SubboxEntry: Failed to write");
  }
  return true;
}

bool OpenTypeGLAT_v3::GlyphAttrs::
GlatEntry::ParsePart(Buffer& table) {
  if (!table.ReadS16(&this->attNum)) {
    return parent->Error("GlatEntry: Failed to read attNum");
  }
  if (!table.ReadS16(&this->num) || this->num < 0) {
    return parent->Error("GlatEntry: Failed to read valid num");
  }

  //this->attributes.resize(this->num);
  for (int i = 0; i < this->num; ++i) {
    this->attributes.emplace_back();
    if (!table.ReadS16(&this->attributes[i])) {
      return parent->Error("GlatEntry: Failed to read attribute %u", i);
    }
  }
  return true;
}

bool OpenTypeGLAT_v3::GlyphAttrs::
GlatEntry::SerializePart(OTSStream* out) const {
  if (!out->WriteS16(this->attNum) ||
      !out->WriteS16(this->num) ||
      !SerializeParts(this->attributes, out)) {
    return parent->Error("GlatEntry: Failed to write");
  }
  return true;
}

// -----------------------------------------------------------------------------
// OpenTypeGLAT
// -----------------------------------------------------------------------------

bool OpenTypeGLAT::Parse(const uint8_t* data, size_t length) {
  if (GetFont()->dropped_graphite) {
    return Drop("Skipping Graphite table");
  }
  Buffer table(data, length);
  uint32_t version;
  if (!table.ReadU32(&version)) {
    return DropGraphite("Failed to read version");
  }
  switch (version >> 16) {
    case 1:
      this->handler = new OpenTypeGLAT_v1(this->font, this->tag);
      break;
    case 2:
      this->handler = new OpenTypeGLAT_v2(this->font, this->tag);
      break;
    case 3: {
      this->handler = new OpenTypeGLAT_v3(this->font, this->tag);
      break;
    }
    default:
      return DropGraphite("Unsupported table version: %u", version >> 16);
  }
  return this->handler->Parse(data, length);
}

bool OpenTypeGLAT::Serialize(OTSStream* out) {
  if (!this->handler) {
    return Error("No Glat table parsed");
  }
  return this->handler->Serialize(out);
}

}  // namespace ots