gfx/ots/src/gpos.cc

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
michael@0 2 // Use of this source code is governed by a BSD-style license that can be
michael@0 3 // found in the LICENSE file.
michael@0 4
michael@0 5 #include "gpos.h"
michael@0 6
michael@0 7 #include <limits>
michael@0 8 #include <vector>
michael@0 9
michael@0 10 #include "layout.h"
michael@0 11 #include "maxp.h"
michael@0 12
michael@0 13 // GPOS - The Glyph Positioning Table
michael@0 14 // http://www.microsoft.com/typography/otspec/gpos.htm
michael@0 15
michael@0 16 #define TABLE_NAME "GPOS"
michael@0 17
michael@0 18 namespace {
michael@0 19
michael@0 20 enum GPOS_TYPE {
michael@0 21 GPOS_TYPE_SINGLE_ADJUSTMENT = 1,
michael@0 22 GPOS_TYPE_PAIR_ADJUSTMENT = 2,
michael@0 23 GPOS_TYPE_CURSIVE_ATTACHMENT = 3,
michael@0 24 GPOS_TYPE_MARK_TO_BASE_ATTACHMENT = 4,
michael@0 25 GPOS_TYPE_MARK_TO_LIGATURE_ATTACHMENT = 5,
michael@0 26 GPOS_TYPE_MARK_TO_MARK_ATTACHMENT = 6,
michael@0 27 GPOS_TYPE_CONTEXT_POSITIONING = 7,
michael@0 28 GPOS_TYPE_CHAINED_CONTEXT_POSITIONING = 8,
michael@0 29 GPOS_TYPE_EXTENSION_POSITIONING = 9,
michael@0 30 GPOS_TYPE_RESERVED = 10
michael@0 31 };
michael@0 32
michael@0 33 // The size of gpos header.
michael@0 34 const unsigned kGposHeaderSize = 10;
michael@0 35 // The maximum format number for anchor tables.
michael@0 36 const uint16_t kMaxAnchorFormat = 3;
michael@0 37 // The maximum number of class value.
michael@0 38 const uint16_t kMaxClassDefValue = 0xFFFF;
michael@0 39
michael@0 40 // Lookup type parsers.
michael@0 41 bool ParseSingleAdjustment(const ots::OpenTypeFile *file,
michael@0 42 const uint8_t *data, const size_t length);
michael@0 43 bool ParsePairAdjustment(const ots::OpenTypeFile *file,
michael@0 44 const uint8_t *data, const size_t length);
michael@0 45 bool ParseCursiveAttachment(const ots::OpenTypeFile *file,
michael@0 46 const uint8_t *data, const size_t length);
michael@0 47 bool ParseMarkToBaseAttachment(const ots::OpenTypeFile *file,
michael@0 48 const uint8_t *data, const size_t length);
michael@0 49 bool ParseMarkToLigatureAttachment(const ots::OpenTypeFile *file,
michael@0 50 const uint8_t *data, const size_t length);
michael@0 51 bool ParseMarkToMarkAttachment(const ots::OpenTypeFile *file,
michael@0 52 const uint8_t *data, const size_t length);
michael@0 53 bool ParseContextPositioning(const ots::OpenTypeFile *file,
michael@0 54 const uint8_t *data, const size_t length);
michael@0 55 bool ParseChainedContextPositioning(const ots::OpenTypeFile *file,
michael@0 56 const uint8_t *data, const size_t length);
michael@0 57 bool ParseExtensionPositioning(const ots::OpenTypeFile *file,
michael@0 58 const uint8_t *data, const size_t length);
michael@0 59
michael@0 60 const ots::LookupSubtableParser::TypeParser kGposTypeParsers[] = {
michael@0 61 {GPOS_TYPE_SINGLE_ADJUSTMENT, ParseSingleAdjustment},
michael@0 62 {GPOS_TYPE_PAIR_ADJUSTMENT, ParsePairAdjustment},
michael@0 63 {GPOS_TYPE_CURSIVE_ATTACHMENT, ParseCursiveAttachment},
michael@0 64 {GPOS_TYPE_MARK_TO_BASE_ATTACHMENT, ParseMarkToBaseAttachment},
michael@0 65 {GPOS_TYPE_MARK_TO_LIGATURE_ATTACHMENT, ParseMarkToLigatureAttachment},
michael@0 66 {GPOS_TYPE_MARK_TO_MARK_ATTACHMENT, ParseMarkToMarkAttachment},
michael@0 67 {GPOS_TYPE_CONTEXT_POSITIONING, ParseContextPositioning},
michael@0 68 {GPOS_TYPE_CHAINED_CONTEXT_POSITIONING, ParseChainedContextPositioning},
michael@0 69 {GPOS_TYPE_EXTENSION_POSITIONING, ParseExtensionPositioning}
michael@0 70 };
michael@0 71
michael@0 72 const ots::LookupSubtableParser kGposLookupSubtableParser = {
michael@0 73 arraysize(kGposTypeParsers),
michael@0 74 GPOS_TYPE_EXTENSION_POSITIONING, kGposTypeParsers
michael@0 75 };
michael@0 76
michael@0 77 // Shared Tables: ValueRecord, Anchor Table, and MarkArray
michael@0 78
michael@0 79 bool ParseValueRecord(const ots::OpenTypeFile *file,
michael@0 80 ots::Buffer* subtable, const uint8_t *data,
michael@0 81 const size_t length, const uint16_t value_format) {
michael@0 82 // Check existence of adjustment fields.
michael@0 83 for (unsigned i = 0; i < 4; ++i) {
michael@0 84 if ((value_format >> i) & 0x1) {
michael@0 85 // Just read the field since these fileds could take an arbitrary values.
michael@0 86 if (!subtable->Skip(2)) {
michael@0 87 return OTS_FAILURE_MSG("Failed to read value reacord component");
michael@0 88 }
michael@0 89 }
michael@0 90 }
michael@0 91
michael@0 92 // Check existence of offsets to device table.
michael@0 93 for (unsigned i = 0; i < 4; ++i) {
michael@0 94 if ((value_format >> (i + 4)) & 0x1) {
michael@0 95 uint16_t offset = 0;
michael@0 96 if (!subtable->ReadU16(&offset)) {
michael@0 97 return OTS_FAILURE_MSG("Failed to read value record offset");
michael@0 98 }
michael@0 99 if (offset) {
michael@0 100 // TODO(bashi): Is it possible that device tables locate before
michael@0 101 // this record? No fonts contain such offset AKAIF.
michael@0 102 if (offset >= length) {
michael@0 103 return OTS_FAILURE_MSG("Value record offset too high %d >= %ld", offset, length);
michael@0 104 }
michael@0 105 if (!ots::ParseDeviceTable(file, data + offset, length - offset)) {
michael@0 106 return OTS_FAILURE_MSG("Failed to parse device table in value record");
michael@0 107 }
michael@0 108 }
michael@0 109 }
michael@0 110 }
michael@0 111 return true;
michael@0 112 }
michael@0 113
michael@0 114 bool ParseAnchorTable(const ots::OpenTypeFile *file,
michael@0 115 const uint8_t *data, const size_t length) {
michael@0 116 ots::Buffer subtable(data, length);
michael@0 117
michael@0 118 uint16_t format = 0;
michael@0 119 // Read format and skip 2 2-byte fields that could be arbitrary values.
michael@0 120 if (!subtable.ReadU16(&format) ||
michael@0 121 !subtable.Skip(4)) {
michael@0 122 return OTS_FAILURE_MSG("Faled to read anchor table");
michael@0 123 }
michael@0 124
michael@0 125 if (format == 0 || format > kMaxAnchorFormat) {
michael@0 126 return OTS_FAILURE_MSG("Bad Anchor table format %d", format);
michael@0 127 }
michael@0 128
michael@0 129 // Format 2 and 3 has additional fields.
michael@0 130 if (format == 2) {
michael@0 131 // Format 2 provides an index to a glyph contour point, which will take
michael@0 132 // arbitrary value.
michael@0 133 uint16_t anchor_point = 0;
michael@0 134 if (!subtable.ReadU16(&anchor_point)) {
michael@0 135 return OTS_FAILURE_MSG("Failed to read anchor point in format 2 Anchor Table");
michael@0 136 }
michael@0 137 } else if (format == 3) {
michael@0 138 uint16_t offset_x_device = 0;
michael@0 139 uint16_t offset_y_device = 0;
michael@0 140 if (!subtable.ReadU16(&offset_x_device) ||
michael@0 141 !subtable.ReadU16(&offset_y_device)) {
michael@0 142 return OTS_FAILURE_MSG("Failed to read device table offsets in format 3 anchor table");
michael@0 143 }
michael@0 144 const unsigned format_end = static_cast<unsigned>(10);
michael@0 145 if (offset_x_device) {
michael@0 146 if (offset_x_device < format_end || offset_x_device >= length) {
michael@0 147 return OTS_FAILURE_MSG("Bad x device table offset %d", offset_x_device);
michael@0 148 }
michael@0 149 if (!ots::ParseDeviceTable(file, data + offset_x_device,
michael@0 150 length - offset_x_device)) {
michael@0 151 return OTS_FAILURE_MSG("Failed to parse device table in anchor table");
michael@0 152 }
michael@0 153 }
michael@0 154 if (offset_y_device) {
michael@0 155 if (offset_y_device < format_end || offset_y_device >= length) {
michael@0 156 return OTS_FAILURE_MSG("Bad y device table offset %d", offset_y_device);
michael@0 157 }
michael@0 158 if (!ots::ParseDeviceTable(file, data + offset_y_device,
michael@0 159 length - offset_y_device)) {
michael@0 160 return OTS_FAILURE_MSG("Failed to parse device table in anchor table");
michael@0 161 }
michael@0 162 }
michael@0 163 }
michael@0 164 return true;
michael@0 165 }
michael@0 166
michael@0 167 bool ParseMarkArrayTable(const ots::OpenTypeFile *file,
michael@0 168 const uint8_t *data, const size_t length,
michael@0 169 const uint16_t class_count) {
michael@0 170 ots::Buffer subtable(data, length);
michael@0 171
michael@0 172 uint16_t mark_count = 0;
michael@0 173 if (!subtable.ReadU16(&mark_count)) {
michael@0 174 return OTS_FAILURE_MSG("Can't read mark table length");
michael@0 175 }
michael@0 176
michael@0 177 // MarkRecord consists of 4-bytes.
michael@0 178 const unsigned mark_records_end = 4 * static_cast<unsigned>(mark_count) + 2;
michael@0 179 if (mark_records_end > std::numeric_limits<uint16_t>::max()) {
michael@0 180 return OTS_FAILURE_MSG("Bad mark table length");
michael@0 181 }
michael@0 182 for (unsigned i = 0; i < mark_count; ++i) {
michael@0 183 uint16_t class_value = 0;
michael@0 184 uint16_t offset_mark_anchor = 0;
michael@0 185 if (!subtable.ReadU16(&class_value) ||
michael@0 186 !subtable.ReadU16(&offset_mark_anchor)) {
michael@0 187 return OTS_FAILURE_MSG("Can't read mark table %d", i);
michael@0 188 }
michael@0 189 // |class_value| may take arbitrary values including 0 here so we don't
michael@0 190 // check the value.
michael@0 191 if (offset_mark_anchor < mark_records_end ||
michael@0 192 offset_mark_anchor >= length) {
michael@0 193 return OTS_FAILURE_MSG("Bad mark anchor offset %d for mark table %d", offset_mark_anchor, i);
michael@0 194 }
michael@0 195 if (!ParseAnchorTable(file, data + offset_mark_anchor,
michael@0 196 length - offset_mark_anchor)) {
michael@0 197 return OTS_FAILURE_MSG("Faled to parse anchor table for mark table %d", i);
michael@0 198 }
michael@0 199 }
michael@0 200
michael@0 201 return true;
michael@0 202 }
michael@0 203
michael@0 204 // Lookup Type 1:
michael@0 205 // Single Adjustment Positioning Subtable
michael@0 206 bool ParseSingleAdjustment(const ots::OpenTypeFile *file, const uint8_t *data,
michael@0 207 const size_t length) {
michael@0 208 ots::Buffer subtable(data, length);
michael@0 209
michael@0 210 uint16_t format = 0;
michael@0 211 uint16_t offset_coverage = 0;
michael@0 212 uint16_t value_format = 0;
michael@0 213 if (!subtable.ReadU16(&format) ||
michael@0 214 !subtable.ReadU16(&offset_coverage) ||
michael@0 215 !subtable.ReadU16(&value_format)) {
michael@0 216 return OTS_FAILURE_MSG("Can't read single adjustment information");
michael@0 217 }
michael@0 218
michael@0 219 if (format == 1) {
michael@0 220 // Format 1 exactly one value record.
michael@0 221 if (!ParseValueRecord(file, &subtable, data, length, value_format)) {
michael@0 222 return OTS_FAILURE_MSG("Failed to parse format 1 single adjustment table");
michael@0 223 }
michael@0 224 } else if (format == 2) {
michael@0 225 uint16_t value_count = 0;
michael@0 226 if (!subtable.ReadU16(&value_count)) {
michael@0 227 return OTS_FAILURE_MSG("Failed to parse format 2 single adjustment table");
michael@0 228 }
michael@0 229 for (unsigned i = 0; i < value_count; ++i) {
michael@0 230 if (!ParseValueRecord(file, &subtable, data, length, value_format)) {
michael@0 231 return OTS_FAILURE_MSG("Failed to parse value record %d in format 2 single adjustment table", i);
michael@0 232 }
michael@0 233 }
michael@0 234 } else {
michael@0 235 return OTS_FAILURE_MSG("Bad format %d in single adjustment table", format);
michael@0 236 }
michael@0 237
michael@0 238 if (offset_coverage < subtable.offset() || offset_coverage >= length) {
michael@0 239 return OTS_FAILURE_MSG("Bad coverage offset %d in single adjustment table", offset_coverage);
michael@0 240 }
michael@0 241
michael@0 242 if (!ots::ParseCoverageTable(file, data + offset_coverage,
michael@0 243 length - offset_coverage,
michael@0 244 file->maxp->num_glyphs)) {
michael@0 245 return OTS_FAILURE_MSG("Failed to parse coverage table in single adjustment table");
michael@0 246 }
michael@0 247
michael@0 248 return true;
michael@0 249 }
michael@0 250
michael@0 251 bool ParsePairSetTable(const ots::OpenTypeFile *file,
michael@0 252 const uint8_t *data, const size_t length,
michael@0 253 const uint16_t value_format1,
michael@0 254 const uint16_t value_format2,
michael@0 255 const uint16_t num_glyphs) {
michael@0 256 ots::Buffer subtable(data, length);
michael@0 257
michael@0 258 uint16_t value_count = 0;
michael@0 259 if (!subtable.ReadU16(&value_count)) {
michael@0 260 return OTS_FAILURE_MSG("Failed to read pair set table structure");
michael@0 261 }
michael@0 262 for (unsigned i = 0; i < value_count; ++i) {
michael@0 263 // Check pair value record.
michael@0 264 uint16_t glyph_id = 0;
michael@0 265 if (!subtable.ReadU16(&glyph_id)) {
michael@0 266 return OTS_FAILURE_MSG("Failed to read glyph in pair value record %d", i);
michael@0 267 }
michael@0 268 if (glyph_id >= num_glyphs) {
michael@0 269 return OTS_FAILURE_MSG("glyph id %d too high >= %d", glyph_id, num_glyphs);
michael@0 270 }
michael@0 271 if (!ParseValueRecord(file, &subtable, data, length, value_format1)) {
michael@0 272 return OTS_FAILURE_MSG("Failed to parse value record in format 1 pair set table");
michael@0 273 }
michael@0 274 if (!ParseValueRecord(file, &subtable, data, length, value_format2)) {
michael@0 275 return OTS_FAILURE_MSG("Failed to parse value record in format 2 pair set table");
michael@0 276 }
michael@0 277 }
michael@0 278 return true;
michael@0 279 }
michael@0 280
michael@0 281 bool ParsePairPosFormat1(const ots::OpenTypeFile *file,
michael@0 282 const uint8_t *data, const size_t length,
michael@0 283 const uint16_t value_format1,
michael@0 284 const uint16_t value_format2,
michael@0 285 const uint16_t num_glyphs) {
michael@0 286 ots::Buffer subtable(data, length);
michael@0 287
michael@0 288 // Skip 8 bytes that are already read before.
michael@0 289 if (!subtable.Skip(8)) {
michael@0 290 return OTS_FAILURE_MSG("Failed to read pair pos table structure");
michael@0 291 }
michael@0 292
michael@0 293 uint16_t pair_set_count = 0;
michael@0 294 if (!subtable.ReadU16(&pair_set_count)) {
michael@0 295 return OTS_FAILURE_MSG("Failed to read pair pos set count");
michael@0 296 }
michael@0 297
michael@0 298 const unsigned pair_pos_end = 2 * static_cast<unsigned>(pair_set_count) + 10;
michael@0 299 if (pair_pos_end > std::numeric_limits<uint16_t>::max()) {
michael@0 300 return OTS_FAILURE_MSG("Bad pair set length %d", pair_pos_end);
michael@0 301 }
michael@0 302 for (unsigned i = 0; i < pair_set_count; ++i) {
michael@0 303 uint16_t pair_set_offset = 0;
michael@0 304 if (!subtable.ReadU16(&pair_set_offset)) {
michael@0 305 return OTS_FAILURE_MSG("Failed to read pair set offset for pair set %d", i);
michael@0 306 }
michael@0 307 if (pair_set_offset < pair_pos_end || pair_set_offset >= length) {
michael@0 308 return OTS_FAILURE_MSG("Bad pair set offset %d for pair set %d", pair_set_offset, i);
michael@0 309 }
michael@0 310 // Check pair set tables
michael@0 311 if (!ParsePairSetTable(file, data + pair_set_offset, length - pair_set_offset,
michael@0 312 value_format1, value_format2,
michael@0 313 num_glyphs)) {
michael@0 314 return OTS_FAILURE_MSG("Failed to parse pair set table %d", i);
michael@0 315 }
michael@0 316 }
michael@0 317
michael@0 318 return true;
michael@0 319 }
michael@0 320
michael@0 321 bool ParsePairPosFormat2(const ots::OpenTypeFile *file,
michael@0 322 const uint8_t *data, const size_t length,
michael@0 323 const uint16_t value_format1,
michael@0 324 const uint16_t value_format2,
michael@0 325 const uint16_t num_glyphs) {
michael@0 326 ots::Buffer subtable(data, length);
michael@0 327
michael@0 328 // Skip 8 bytes that are already read before.
michael@0 329 if (!subtable.Skip(8)) {
michael@0 330 return OTS_FAILURE_MSG("Failed to read pair pos format 2 structure");
michael@0 331 }
michael@0 332
michael@0 333 uint16_t offset_class_def1 = 0;
michael@0 334 uint16_t offset_class_def2 = 0;
michael@0 335 uint16_t class1_count = 0;
michael@0 336 uint16_t class2_count = 0;
michael@0 337 if (!subtable.ReadU16(&offset_class_def1) ||
michael@0 338 !subtable.ReadU16(&offset_class_def2) ||
michael@0 339 !subtable.ReadU16(&class1_count) ||
michael@0 340 !subtable.ReadU16(&class2_count)) {
michael@0 341 return OTS_FAILURE_MSG("Failed to read pair pos format 2 data");
michael@0 342 }
michael@0 343
michael@0 344 // Check class 1 records.
michael@0 345 for (unsigned i = 0; i < class1_count; ++i) {
michael@0 346 // Check class 2 records.
michael@0 347 for (unsigned j = 0; j < class2_count; ++j) {
michael@0 348 if (value_format1 && !ParseValueRecord(file, &subtable, data, length,
michael@0 349 value_format1)) {
michael@0 350 return OTS_FAILURE_MSG("Failed to parse value record 1 %d and %d", j, i);
michael@0 351 }
michael@0 352 if (value_format2 && !ParseValueRecord(file, &subtable, data, length,
michael@0 353 value_format2)) {
michael@0 354 return OTS_FAILURE_MSG("Falied to parse value record 2 %d and %d", j, i);
michael@0 355 }
michael@0 356 }
michael@0 357 }
michael@0 358
michael@0 359 // Check class definition tables.
michael@0 360 if (offset_class_def1 < subtable.offset() || offset_class_def1 >= length ||
michael@0 361 offset_class_def2 < subtable.offset() || offset_class_def2 >= length) {
michael@0 362 return OTS_FAILURE_MSG("Bad class definition table offsets %d or %d", offset_class_def1, offset_class_def2);
michael@0 363 }
michael@0 364 if (!ots::ParseClassDefTable(file, data + offset_class_def1,
michael@0 365 length - offset_class_def1,
michael@0 366 num_glyphs, kMaxClassDefValue)) {
michael@0 367 return OTS_FAILURE_MSG("Failed to parse class definition table 1");
michael@0 368 }
michael@0 369 if (!ots::ParseClassDefTable(file, data + offset_class_def2,
michael@0 370 length - offset_class_def2,
michael@0 371 num_glyphs, kMaxClassDefValue)) {
michael@0 372 return OTS_FAILURE_MSG("Failed to parse class definition table 2");
michael@0 373 }
michael@0 374
michael@0 375 return true;
michael@0 376 }
michael@0 377
michael@0 378 // Lookup Type 2:
michael@0 379 // Pair Adjustment Positioning Subtable
michael@0 380 bool ParsePairAdjustment(const ots::OpenTypeFile *file, const uint8_t *data,
michael@0 381 const size_t length) {
michael@0 382 ots::Buffer subtable(data, length);
michael@0 383
michael@0 384 uint16_t format = 0;
michael@0 385 uint16_t offset_coverage = 0;
michael@0 386 uint16_t value_format1 = 0;
michael@0 387 uint16_t value_format2 = 0;
michael@0 388 if (!subtable.ReadU16(&format) ||
michael@0 389 !subtable.ReadU16(&offset_coverage) ||
michael@0 390 !subtable.ReadU16(&value_format1) ||
michael@0 391 !subtable.ReadU16(&value_format2)) {
michael@0 392 return OTS_FAILURE_MSG("Failed to read pair adjustment structure");
michael@0 393 }
michael@0 394
michael@0 395 if (format == 1) {
michael@0 396 if (!ParsePairPosFormat1(file, data, length, value_format1, value_format2,
michael@0 397 file->maxp->num_glyphs)) {
michael@0 398 return OTS_FAILURE_MSG("Failed to parse pair pos format 1");
michael@0 399 }
michael@0 400 } else if (format == 2) {
michael@0 401 if (!ParsePairPosFormat2(file, data, length, value_format1, value_format2,
michael@0 402 file->maxp->num_glyphs)) {
michael@0 403 return OTS_FAILURE_MSG("Failed to parse pair format 2");
michael@0 404 }
michael@0 405 } else {
michael@0 406 return OTS_FAILURE_MSG("Bad pos pair format %d", format);
michael@0 407 }
michael@0 408
michael@0 409 if (offset_coverage < subtable.offset() || offset_coverage >= length) {
michael@0 410 return OTS_FAILURE_MSG("Bad pair pos offset coverage %d", offset_coverage);
michael@0 411 }
michael@0 412 if (!ots::ParseCoverageTable(file, data + offset_coverage,
michael@0 413 length - offset_coverage,
michael@0 414 file->maxp->num_glyphs)) {
michael@0 415 return OTS_FAILURE_MSG("Failed to parse coverage table");
michael@0 416 }
michael@0 417
michael@0 418 return true;
michael@0 419 }
michael@0 420
michael@0 421 // Lookup Type 3
michael@0 422 // Cursive Attachment Positioning Subtable
michael@0 423 bool ParseCursiveAttachment(const ots::OpenTypeFile *file, const uint8_t *data,
michael@0 424 const size_t length) {
michael@0 425 ots::Buffer subtable(data, length);
michael@0 426
michael@0 427 uint16_t format = 0;
michael@0 428 uint16_t offset_coverage = 0;
michael@0 429 uint16_t entry_exit_count = 0;
michael@0 430 if (!subtable.ReadU16(&format) ||
michael@0 431 !subtable.ReadU16(&offset_coverage) ||
michael@0 432 !subtable.ReadU16(&entry_exit_count)) {
michael@0 433 return OTS_FAILURE_MSG("Failed to read cursive attachment structure");
michael@0 434 }
michael@0 435
michael@0 436 if (format != 1) {
michael@0 437 return OTS_FAILURE_MSG("Bad cursive attachment format %d", format);
michael@0 438 }
michael@0 439
michael@0 440 // Check entry exit records.
michael@0 441 const unsigned entry_exit_records_end =
michael@0 442 2 * static_cast<unsigned>(entry_exit_count) + 6;
michael@0 443 if (entry_exit_records_end > std::numeric_limits<uint16_t>::max()) {
michael@0 444 return OTS_FAILURE_MSG("Bad entry exit record end %d", entry_exit_records_end);
michael@0 445 }
michael@0 446 for (unsigned i = 0; i < entry_exit_count; ++i) {
michael@0 447 uint16_t offset_entry_anchor = 0;
michael@0 448 uint16_t offset_exit_anchor = 0;
michael@0 449 if (!subtable.ReadU16(&offset_entry_anchor) ||
michael@0 450 !subtable.ReadU16(&offset_exit_anchor)) {
michael@0 451 return OTS_FAILURE_MSG("Can't read entry exit record %d", i);
michael@0 452 }
michael@0 453 // These offsets could be NULL.
michael@0 454 if (offset_entry_anchor) {
michael@0 455 if (offset_entry_anchor < entry_exit_records_end ||
michael@0 456 offset_entry_anchor >= length) {
michael@0 457 return OTS_FAILURE_MSG("Bad entry anchor offset %d in entry exit record %d", offset_entry_anchor, i);
michael@0 458 }
michael@0 459 if (!ParseAnchorTable(file, data + offset_entry_anchor,
michael@0 460 length - offset_entry_anchor)) {
michael@0 461 return OTS_FAILURE_MSG("Failed to parse entry anchor table in entry exit record %d", i);
michael@0 462 }
michael@0 463 }
michael@0 464 if (offset_exit_anchor) {
michael@0 465 if (offset_exit_anchor < entry_exit_records_end ||
michael@0 466 offset_exit_anchor >= length) {
michael@0 467 return OTS_FAILURE_MSG("Bad exit anchor offset %d in entry exit record %d", offset_exit_anchor, i);
michael@0 468 }
michael@0 469 if (!ParseAnchorTable(file, data + offset_exit_anchor,
michael@0 470 length - offset_exit_anchor)) {
michael@0 471 return OTS_FAILURE_MSG("Failed to parse exit anchor table in entry exit record %d", i);
michael@0 472 }
michael@0 473 }
michael@0 474 }
michael@0 475
michael@0 476 if (offset_coverage < subtable.offset() || offset_coverage >= length) {
michael@0 477 return OTS_FAILURE_MSG("Bad coverage offset in cursive attachment %d", offset_coverage);
michael@0 478 }
michael@0 479 if (!ots::ParseCoverageTable(file, data + offset_coverage,
michael@0 480 length - offset_coverage,
michael@0 481 file->maxp->num_glyphs)) {
michael@0 482 return OTS_FAILURE_MSG("Failed to parse coverage table in cursive attachment");
michael@0 483 }
michael@0 484
michael@0 485 return true;
michael@0 486 }
michael@0 487
michael@0 488 bool ParseAnchorArrayTable(const ots::OpenTypeFile *file,
michael@0 489 const uint8_t *data, const size_t length,
michael@0 490 const uint16_t class_count) {
michael@0 491 ots::Buffer subtable(data, length);
michael@0 492
michael@0 493 uint16_t record_count = 0;
michael@0 494 if (!subtable.ReadU16(&record_count)) {
michael@0 495 return OTS_FAILURE_MSG("Can't read anchor array length");
michael@0 496 }
michael@0 497
michael@0 498 const unsigned anchor_array_end = 2 * static_cast<unsigned>(record_count) *
michael@0 499 static_cast<unsigned>(class_count) + 2;
michael@0 500 if (anchor_array_end > std::numeric_limits<uint16_t>::max()) {
michael@0 501 return OTS_FAILURE_MSG("Bad end of anchor array %d", anchor_array_end);
michael@0 502 }
michael@0 503 for (unsigned i = 0; i < record_count; ++i) {
michael@0 504 for (unsigned j = 0; j < class_count; ++j) {
michael@0 505 uint16_t offset_record = 0;
michael@0 506 if (!subtable.ReadU16(&offset_record)) {
michael@0 507 return OTS_FAILURE_MSG("Can't read anchor array record offset for class %d and record %d", j, i);
michael@0 508 }
michael@0 509 // |offset_record| could be NULL.
michael@0 510 if (offset_record) {
michael@0 511 if (offset_record < anchor_array_end || offset_record >= length) {
michael@0 512 return OTS_FAILURE_MSG("Bad record offset %d in class %d, record %d", offset_record, j, i);
michael@0 513 }
michael@0 514 if (!ParseAnchorTable(file, data + offset_record,
michael@0 515 length - offset_record)) {
michael@0 516 return OTS_FAILURE_MSG("Failed to parse anchor table for class %d, record %d", j, i);
michael@0 517 }
michael@0 518 }
michael@0 519 }
michael@0 520 }
michael@0 521 return true;
michael@0 522 }
michael@0 523
michael@0 524 bool ParseLigatureArrayTable(const ots::OpenTypeFile *file,
michael@0 525 const uint8_t *data, const size_t length,
michael@0 526 const uint16_t class_count) {
michael@0 527 ots::Buffer subtable(data, length);
michael@0 528
michael@0 529 uint16_t ligature_count = 0;
michael@0 530 if (!subtable.ReadU16(&ligature_count)) {
michael@0 531 return OTS_FAILURE_MSG("Failed to read ligature count");
michael@0 532 }
michael@0 533 for (unsigned i = 0; i < ligature_count; ++i) {
michael@0 534 uint16_t offset_ligature_attach = 0;
michael@0 535 if (!subtable.ReadU16(&offset_ligature_attach)) {
michael@0 536 return OTS_FAILURE_MSG("Can't read ligature offset %d", i);
michael@0 537 }
michael@0 538 if (offset_ligature_attach < 2 || offset_ligature_attach >= length) {
michael@0 539 return OTS_FAILURE_MSG("Bad ligature attachment offset %d in ligature %d", offset_ligature_attach, i);
michael@0 540 }
michael@0 541 if (!ParseAnchorArrayTable(file, data + offset_ligature_attach,
michael@0 542 length - offset_ligature_attach, class_count)) {
michael@0 543 return OTS_FAILURE_MSG("Failed to parse anchor table for ligature %d", i);
michael@0 544 }
michael@0 545 }
michael@0 546 return true;
michael@0 547 }
michael@0 548
michael@0 549 // Common parser for Lookup Type 4, 5 and 6.
michael@0 550 bool ParseMarkToAttachmentSubtables(const ots::OpenTypeFile *file,
michael@0 551 const uint8_t *data, const size_t length,
michael@0 552 const GPOS_TYPE type) {
michael@0 553 ots::Buffer subtable(data, length);
michael@0 554
michael@0 555 uint16_t format = 0;
michael@0 556 uint16_t offset_coverage1 = 0;
michael@0 557 uint16_t offset_coverage2 = 0;
michael@0 558 uint16_t class_count = 0;
michael@0 559 uint16_t offset_mark_array = 0;
michael@0 560 uint16_t offset_type_specific_array = 0;
michael@0 561 if (!subtable.ReadU16(&format) ||
michael@0 562 !subtable.ReadU16(&offset_coverage1) ||
michael@0 563 !subtable.ReadU16(&offset_coverage2) ||
michael@0 564 !subtable.ReadU16(&class_count) ||
michael@0 565 !subtable.ReadU16(&offset_mark_array) ||
michael@0 566 !subtable.ReadU16(&offset_type_specific_array)) {
michael@0 567 return OTS_FAILURE_MSG("Failed to read mark attachment subtable header");
michael@0 568 }
michael@0 569
michael@0 570 if (format != 1) {
michael@0 571 return OTS_FAILURE_MSG("bad mark attachment subtable format %d", format);
michael@0 572 }
michael@0 573
michael@0 574 const unsigned header_end = static_cast<unsigned>(subtable.offset());
michael@0 575 if (header_end > std::numeric_limits<uint16_t>::max()) {
michael@0 576 return OTS_FAILURE_MSG("Bad mark attachment subtable size ending at %d", header_end);
michael@0 577 }
michael@0 578 if (offset_coverage1 < header_end || offset_coverage1 >= length) {
michael@0 579 return OTS_FAILURE_MSG("Bad coverage 1 offset %d", offset_coverage1);
michael@0 580 }
michael@0 581 if (!ots::ParseCoverageTable(file, data + offset_coverage1,
michael@0 582 length - offset_coverage1,
michael@0 583 file->maxp->num_glyphs)) {
michael@0 584 return OTS_FAILURE_MSG("Failed to parse converge 1 table");
michael@0 585 }
michael@0 586 if (offset_coverage2 < header_end || offset_coverage2 >= length) {
michael@0 587 return OTS_FAILURE_MSG("Bad coverage 2 offset %d", offset_coverage2);
michael@0 588 }
michael@0 589 if (!ots::ParseCoverageTable(file, data + offset_coverage2,
michael@0 590 length - offset_coverage2,
michael@0 591 file->maxp->num_glyphs)) {
michael@0 592 return OTS_FAILURE_MSG("Failed to parse coverage table 2");
michael@0 593 }
michael@0 594
michael@0 595 if (offset_mark_array < header_end || offset_mark_array >= length) {
michael@0 596 return OTS_FAILURE_MSG("Bad mark array offset %d", offset_mark_array);
michael@0 597 }
michael@0 598 if (!ParseMarkArrayTable(file, data + offset_mark_array,
michael@0 599 length - offset_mark_array, class_count)) {
michael@0 600 return OTS_FAILURE_MSG("Failed to parse mark array");
michael@0 601 }
michael@0 602
michael@0 603 if (offset_type_specific_array < header_end ||
michael@0 604 offset_type_specific_array >= length) {
michael@0 605 return OTS_FAILURE_MSG("Bad type specific array offset %d", offset_type_specific_array);
michael@0 606 }
michael@0 607 if (type == GPOS_TYPE_MARK_TO_BASE_ATTACHMENT ||
michael@0 608 type == GPOS_TYPE_MARK_TO_MARK_ATTACHMENT) {
michael@0 609 if (!ParseAnchorArrayTable(file, data + offset_type_specific_array,
michael@0 610 length - offset_type_specific_array,
michael@0 611 class_count)) {
michael@0 612 return OTS_FAILURE_MSG("Failed to parse anchor array");
michael@0 613 }
michael@0 614 } else if (type == GPOS_TYPE_MARK_TO_LIGATURE_ATTACHMENT) {
michael@0 615 if (!ParseLigatureArrayTable(file, data + offset_type_specific_array,
michael@0 616 length - offset_type_specific_array,
michael@0 617 class_count)) {
michael@0 618 return OTS_FAILURE_MSG("Failed to parse ligature array");
michael@0 619 }
michael@0 620 } else {
michael@0 621 return OTS_FAILURE_MSG("Bad attachment type %d", type);
michael@0 622 }
michael@0 623
michael@0 624 return true;
michael@0 625 }
michael@0 626
michael@0 627 // Lookup Type 4:
michael@0 628 // MarkToBase Attachment Positioning Subtable
michael@0 629 bool ParseMarkToBaseAttachment(const ots::OpenTypeFile *file,
michael@0 630 const uint8_t *data, const size_t length) {
michael@0 631 return ParseMarkToAttachmentSubtables(file, data, length,
michael@0 632 GPOS_TYPE_MARK_TO_BASE_ATTACHMENT);
michael@0 633 }
michael@0 634
michael@0 635 // Lookup Type 5:
michael@0 636 // MarkToLigature Attachment Positioning Subtable
michael@0 637 bool ParseMarkToLigatureAttachment(const ots::OpenTypeFile *file,
michael@0 638 const uint8_t *data, const size_t length) {
michael@0 639 return ParseMarkToAttachmentSubtables(file, data, length,
michael@0 640 GPOS_TYPE_MARK_TO_LIGATURE_ATTACHMENT);
michael@0 641 }
michael@0 642
michael@0 643 // Lookup Type 6:
michael@0 644 // MarkToMark Attachment Positioning Subtable
michael@0 645 bool ParseMarkToMarkAttachment(const ots::OpenTypeFile *file,
michael@0 646 const uint8_t *data, const size_t length) {
michael@0 647 return ParseMarkToAttachmentSubtables(file, data, length,
michael@0 648 GPOS_TYPE_MARK_TO_MARK_ATTACHMENT);
michael@0 649 }
michael@0 650
michael@0 651 // Lookup Type 7:
michael@0 652 // Contextual Positioning Subtables
michael@0 653 bool ParseContextPositioning(const ots::OpenTypeFile *file,
michael@0 654 const uint8_t *data, const size_t length) {
michael@0 655 return ots::ParseContextSubtable(file, data, length, file->maxp->num_glyphs,
michael@0 656 file->gpos->num_lookups);
michael@0 657 }
michael@0 658
michael@0 659 // Lookup Type 8:
michael@0 660 // Chaining Contexual Positioning Subtable
michael@0 661 bool ParseChainedContextPositioning(const ots::OpenTypeFile *file,
michael@0 662 const uint8_t *data, const size_t length) {
michael@0 663 return ots::ParseChainingContextSubtable(file, data, length,
michael@0 664 file->maxp->num_glyphs,
michael@0 665 file->gpos->num_lookups);
michael@0 666 }
michael@0 667
michael@0 668 // Lookup Type 9:
michael@0 669 // Extension Positioning
michael@0 670 bool ParseExtensionPositioning(const ots::OpenTypeFile *file,
michael@0 671 const uint8_t *data, const size_t length) {
michael@0 672 return ots::ParseExtensionSubtable(file, data, length,
michael@0 673 &kGposLookupSubtableParser);
michael@0 674 }
michael@0 675
michael@0 676 } // namespace
michael@0 677
michael@0 678 #define DROP_THIS_TABLE(msg_) \
michael@0 679 do { \
michael@0 680 file->gpos->data = 0; \
michael@0 681 file->gpos->length = 0; \
michael@0 682 OTS_FAILURE_MSG(msg_ ", table discarded"); \
michael@0 683 } while (0)
michael@0 684
michael@0 685 namespace ots {
michael@0 686
michael@0 687 // As far as I checked, following fonts contain invalid GPOS table and
michael@0 688 // OTS will drop their GPOS table.
michael@0 689 //
michael@0 690 // # invalid delta format in device table
michael@0 691 // samanata.ttf
michael@0 692 //
michael@0 693 // # bad size range in device table
michael@0 694 // Sarai_07.ttf
michael@0 695 //
michael@0 696 // # bad offset to PairSetTable
michael@0 697 // chandas1-2.ttf
michael@0 698 //
michael@0 699 // # bad offset to FeatureTable
michael@0 700 // glrso12.ttf
michael@0 701 // gllr12.ttf
michael@0 702 // glbo12.ttf
michael@0 703 // glb12.ttf
michael@0 704 // glro12.ttf
michael@0 705 // glbso12.ttf
michael@0 706 // glrc12.ttf
michael@0 707 // glrsc12.ttf
michael@0 708 // glbs12.ttf
michael@0 709 // glrs12.ttf
michael@0 710 // glr12.ttf
michael@0 711 //
michael@0 712 // # ScriptRecords aren't sorted by tag
michael@0 713 // Garogier_unhinted.otf
michael@0 714 //
michael@0 715 // # bad start coverage index in CoverageFormat2
michael@0 716 // AndBasR.ttf
michael@0 717 // CharisSILB.ttf
michael@0 718 // CharisSILBI.ttf
michael@0 719 // CharisSILI.ttf
michael@0 720 // CharisSILR.ttf
michael@0 721 // DoulosSILR.ttf
michael@0 722 // GenBasBI.ttf
michael@0 723 // GenBasI.ttf
michael@0 724 // GenBkBasI.ttf
michael@0 725 // GenBkBasB.ttf
michael@0 726 // GenBkBasR.ttf
michael@0 727 // Padauk-Bold.ttf
michael@0 728 // Padauk.ttf
michael@0 729 //
michael@0 730 // # Contour point indexes aren't sorted
michael@0 731 // Arial Unicode.ttf
michael@0 732
michael@0 733 bool ots_gpos_parse(OpenTypeFile *file, const uint8_t *data, size_t length) {
michael@0 734 // Parsing GPOS table requires num_glyphs which is contained in maxp table.
michael@0 735 if (!file->maxp) {
michael@0 736 return OTS_FAILURE_MSG("missing maxp table needed in GPOS");
michael@0 737 }
michael@0 738
michael@0 739 Buffer table(data, length);
michael@0 740
michael@0 741 OpenTypeGPOS *gpos = new OpenTypeGPOS;
michael@0 742 file->gpos = gpos;
michael@0 743
michael@0 744 uint32_t version = 0;
michael@0 745 uint16_t offset_script_list = 0;
michael@0 746 uint16_t offset_feature_list = 0;
michael@0 747 uint16_t offset_lookup_list = 0;
michael@0 748 if (!table.ReadU32(&version) ||
michael@0 749 !table.ReadU16(&offset_script_list) ||
michael@0 750 !table.ReadU16(&offset_feature_list) ||
michael@0 751 !table.ReadU16(&offset_lookup_list)) {
michael@0 752 DROP_THIS_TABLE("Incomplete table");
michael@0 753 return true;
michael@0 754 }
michael@0 755
michael@0 756 if (version != 0x00010000) {
michael@0 757 DROP_THIS_TABLE("Bad version");
michael@0 758 return true;
michael@0 759 }
michael@0 760 if ((offset_script_list < kGposHeaderSize ||
michael@0 761 offset_script_list >= length) ||
michael@0 762 (offset_feature_list < kGposHeaderSize ||
michael@0 763 offset_feature_list >= length) ||
michael@0 764 (offset_lookup_list < kGposHeaderSize ||
michael@0 765 offset_lookup_list >= length)) {
michael@0 766 DROP_THIS_TABLE("Bad offset in table header");
michael@0 767 return true;
michael@0 768 }
michael@0 769
michael@0 770 if (!ParseLookupListTable(file, data + offset_lookup_list,
michael@0 771 length - offset_lookup_list,
michael@0 772 &kGposLookupSubtableParser,
michael@0 773 &gpos->num_lookups)) {
michael@0 774 DROP_THIS_TABLE("Failed to parse lookup list table");
michael@0 775 return true;
michael@0 776 }
michael@0 777
michael@0 778 uint16_t num_features = 0;
michael@0 779 if (!ParseFeatureListTable(file, data + offset_feature_list,
michael@0 780 length - offset_feature_list, gpos->num_lookups,
michael@0 781 &num_features)) {
michael@0 782 DROP_THIS_TABLE("Failed to parse feature list table");
michael@0 783 return true;
michael@0 784 }
michael@0 785
michael@0 786 if (!ParseScriptListTable(file, data + offset_script_list,
michael@0 787 length - offset_script_list, num_features)) {
michael@0 788 DROP_THIS_TABLE("Failed to parse script list table");
michael@0 789 return true;
michael@0 790 }
michael@0 791
michael@0 792 gpos->data = data;
michael@0 793 gpos->length = length;
michael@0 794 return true;
michael@0 795 }
michael@0 796
michael@0 797 bool ots_gpos_should_serialise(OpenTypeFile *file) {
michael@0 798 return file->gpos != NULL && file->gpos->data != NULL;
michael@0 799 }
michael@0 800
michael@0 801 bool ots_gpos_serialise(OTSStream *out, OpenTypeFile *file) {
michael@0 802 if (!out->Write(file->gpos->data, file->gpos->length)) {
michael@0 803 return OTS_FAILURE_MSG("Failed to write GPOS table");
michael@0 804 }
michael@0 805
michael@0 806 return true;
michael@0 807 }
michael@0 808
michael@0 809 void ots_gpos_free(OpenTypeFile *file) {
michael@0 810 delete file->gpos;
michael@0 811 }
michael@0 812
michael@0 813 } // namespace ots
michael@0 814

mercurial