michael@0: // Copyright (c) 2011 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: #include "gpos.h" michael@0: michael@0: #include michael@0: #include michael@0: michael@0: #include "layout.h" michael@0: #include "maxp.h" michael@0: michael@0: // GPOS - The Glyph Positioning Table michael@0: // http://www.microsoft.com/typography/otspec/gpos.htm michael@0: michael@0: #define TABLE_NAME "GPOS" michael@0: michael@0: namespace { michael@0: michael@0: enum GPOS_TYPE { michael@0: GPOS_TYPE_SINGLE_ADJUSTMENT = 1, michael@0: GPOS_TYPE_PAIR_ADJUSTMENT = 2, michael@0: GPOS_TYPE_CURSIVE_ATTACHMENT = 3, michael@0: GPOS_TYPE_MARK_TO_BASE_ATTACHMENT = 4, michael@0: GPOS_TYPE_MARK_TO_LIGATURE_ATTACHMENT = 5, michael@0: GPOS_TYPE_MARK_TO_MARK_ATTACHMENT = 6, michael@0: GPOS_TYPE_CONTEXT_POSITIONING = 7, michael@0: GPOS_TYPE_CHAINED_CONTEXT_POSITIONING = 8, michael@0: GPOS_TYPE_EXTENSION_POSITIONING = 9, michael@0: GPOS_TYPE_RESERVED = 10 michael@0: }; michael@0: michael@0: // The size of gpos header. michael@0: const unsigned kGposHeaderSize = 10; michael@0: // The maximum format number for anchor tables. michael@0: const uint16_t kMaxAnchorFormat = 3; michael@0: // The maximum number of class value. michael@0: const uint16_t kMaxClassDefValue = 0xFFFF; michael@0: michael@0: // Lookup type parsers. michael@0: bool ParseSingleAdjustment(const ots::OpenTypeFile *file, michael@0: const uint8_t *data, const size_t length); michael@0: bool ParsePairAdjustment(const ots::OpenTypeFile *file, michael@0: const uint8_t *data, const size_t length); michael@0: bool ParseCursiveAttachment(const ots::OpenTypeFile *file, michael@0: const uint8_t *data, const size_t length); michael@0: bool ParseMarkToBaseAttachment(const ots::OpenTypeFile *file, michael@0: const uint8_t *data, const size_t length); michael@0: bool ParseMarkToLigatureAttachment(const ots::OpenTypeFile *file, michael@0: const uint8_t *data, const size_t length); michael@0: bool ParseMarkToMarkAttachment(const ots::OpenTypeFile *file, michael@0: const uint8_t *data, const size_t length); michael@0: bool ParseContextPositioning(const ots::OpenTypeFile *file, michael@0: const uint8_t *data, const size_t length); michael@0: bool ParseChainedContextPositioning(const ots::OpenTypeFile *file, michael@0: const uint8_t *data, const size_t length); michael@0: bool ParseExtensionPositioning(const ots::OpenTypeFile *file, michael@0: const uint8_t *data, const size_t length); michael@0: michael@0: const ots::LookupSubtableParser::TypeParser kGposTypeParsers[] = { michael@0: {GPOS_TYPE_SINGLE_ADJUSTMENT, ParseSingleAdjustment}, michael@0: {GPOS_TYPE_PAIR_ADJUSTMENT, ParsePairAdjustment}, michael@0: {GPOS_TYPE_CURSIVE_ATTACHMENT, ParseCursiveAttachment}, michael@0: {GPOS_TYPE_MARK_TO_BASE_ATTACHMENT, ParseMarkToBaseAttachment}, michael@0: {GPOS_TYPE_MARK_TO_LIGATURE_ATTACHMENT, ParseMarkToLigatureAttachment}, michael@0: {GPOS_TYPE_MARK_TO_MARK_ATTACHMENT, ParseMarkToMarkAttachment}, michael@0: {GPOS_TYPE_CONTEXT_POSITIONING, ParseContextPositioning}, michael@0: {GPOS_TYPE_CHAINED_CONTEXT_POSITIONING, ParseChainedContextPositioning}, michael@0: {GPOS_TYPE_EXTENSION_POSITIONING, ParseExtensionPositioning} michael@0: }; michael@0: michael@0: const ots::LookupSubtableParser kGposLookupSubtableParser = { michael@0: arraysize(kGposTypeParsers), michael@0: GPOS_TYPE_EXTENSION_POSITIONING, kGposTypeParsers michael@0: }; michael@0: michael@0: // Shared Tables: ValueRecord, Anchor Table, and MarkArray michael@0: michael@0: bool ParseValueRecord(const ots::OpenTypeFile *file, michael@0: ots::Buffer* subtable, const uint8_t *data, michael@0: const size_t length, const uint16_t value_format) { michael@0: // Check existence of adjustment fields. michael@0: for (unsigned i = 0; i < 4; ++i) { michael@0: if ((value_format >> i) & 0x1) { michael@0: // Just read the field since these fileds could take an arbitrary values. michael@0: if (!subtable->Skip(2)) { michael@0: return OTS_FAILURE_MSG("Failed to read value reacord component"); michael@0: } michael@0: } michael@0: } michael@0: michael@0: // Check existence of offsets to device table. michael@0: for (unsigned i = 0; i < 4; ++i) { michael@0: if ((value_format >> (i + 4)) & 0x1) { michael@0: uint16_t offset = 0; michael@0: if (!subtable->ReadU16(&offset)) { michael@0: return OTS_FAILURE_MSG("Failed to read value record offset"); michael@0: } michael@0: if (offset) { michael@0: // TODO(bashi): Is it possible that device tables locate before michael@0: // this record? No fonts contain such offset AKAIF. michael@0: if (offset >= length) { michael@0: return OTS_FAILURE_MSG("Value record offset too high %d >= %ld", offset, length); michael@0: } michael@0: if (!ots::ParseDeviceTable(file, data + offset, length - offset)) { michael@0: return OTS_FAILURE_MSG("Failed to parse device table in value record"); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: bool ParseAnchorTable(const ots::OpenTypeFile *file, michael@0: const uint8_t *data, const size_t length) { michael@0: ots::Buffer subtable(data, length); michael@0: michael@0: uint16_t format = 0; michael@0: // Read format and skip 2 2-byte fields that could be arbitrary values. michael@0: if (!subtable.ReadU16(&format) || michael@0: !subtable.Skip(4)) { michael@0: return OTS_FAILURE_MSG("Faled to read anchor table"); michael@0: } michael@0: michael@0: if (format == 0 || format > kMaxAnchorFormat) { michael@0: return OTS_FAILURE_MSG("Bad Anchor table format %d", format); michael@0: } michael@0: michael@0: // Format 2 and 3 has additional fields. michael@0: if (format == 2) { michael@0: // Format 2 provides an index to a glyph contour point, which will take michael@0: // arbitrary value. michael@0: uint16_t anchor_point = 0; michael@0: if (!subtable.ReadU16(&anchor_point)) { michael@0: return OTS_FAILURE_MSG("Failed to read anchor point in format 2 Anchor Table"); michael@0: } michael@0: } else if (format == 3) { michael@0: uint16_t offset_x_device = 0; michael@0: uint16_t offset_y_device = 0; michael@0: if (!subtable.ReadU16(&offset_x_device) || michael@0: !subtable.ReadU16(&offset_y_device)) { michael@0: return OTS_FAILURE_MSG("Failed to read device table offsets in format 3 anchor table"); michael@0: } michael@0: const unsigned format_end = static_cast(10); michael@0: if (offset_x_device) { michael@0: if (offset_x_device < format_end || offset_x_device >= length) { michael@0: return OTS_FAILURE_MSG("Bad x device table offset %d", offset_x_device); michael@0: } michael@0: if (!ots::ParseDeviceTable(file, data + offset_x_device, michael@0: length - offset_x_device)) { michael@0: return OTS_FAILURE_MSG("Failed to parse device table in anchor table"); michael@0: } michael@0: } michael@0: if (offset_y_device) { michael@0: if (offset_y_device < format_end || offset_y_device >= length) { michael@0: return OTS_FAILURE_MSG("Bad y device table offset %d", offset_y_device); michael@0: } michael@0: if (!ots::ParseDeviceTable(file, data + offset_y_device, michael@0: length - offset_y_device)) { michael@0: return OTS_FAILURE_MSG("Failed to parse device table in anchor table"); michael@0: } michael@0: } michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: bool ParseMarkArrayTable(const ots::OpenTypeFile *file, michael@0: const uint8_t *data, const size_t length, michael@0: const uint16_t class_count) { michael@0: ots::Buffer subtable(data, length); michael@0: michael@0: uint16_t mark_count = 0; michael@0: if (!subtable.ReadU16(&mark_count)) { michael@0: return OTS_FAILURE_MSG("Can't read mark table length"); michael@0: } michael@0: michael@0: // MarkRecord consists of 4-bytes. michael@0: const unsigned mark_records_end = 4 * static_cast(mark_count) + 2; michael@0: if (mark_records_end > std::numeric_limits::max()) { michael@0: return OTS_FAILURE_MSG("Bad mark table length"); michael@0: } michael@0: for (unsigned i = 0; i < mark_count; ++i) { michael@0: uint16_t class_value = 0; michael@0: uint16_t offset_mark_anchor = 0; michael@0: if (!subtable.ReadU16(&class_value) || michael@0: !subtable.ReadU16(&offset_mark_anchor)) { michael@0: return OTS_FAILURE_MSG("Can't read mark table %d", i); michael@0: } michael@0: // |class_value| may take arbitrary values including 0 here so we don't michael@0: // check the value. michael@0: if (offset_mark_anchor < mark_records_end || michael@0: offset_mark_anchor >= length) { michael@0: return OTS_FAILURE_MSG("Bad mark anchor offset %d for mark table %d", offset_mark_anchor, i); michael@0: } michael@0: if (!ParseAnchorTable(file, data + offset_mark_anchor, michael@0: length - offset_mark_anchor)) { michael@0: return OTS_FAILURE_MSG("Faled to parse anchor table for mark table %d", i); michael@0: } michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: // Lookup Type 1: michael@0: // Single Adjustment Positioning Subtable michael@0: bool ParseSingleAdjustment(const ots::OpenTypeFile *file, const uint8_t *data, michael@0: const size_t length) { michael@0: ots::Buffer subtable(data, length); michael@0: michael@0: uint16_t format = 0; michael@0: uint16_t offset_coverage = 0; michael@0: uint16_t value_format = 0; michael@0: if (!subtable.ReadU16(&format) || michael@0: !subtable.ReadU16(&offset_coverage) || michael@0: !subtable.ReadU16(&value_format)) { michael@0: return OTS_FAILURE_MSG("Can't read single adjustment information"); michael@0: } michael@0: michael@0: if (format == 1) { michael@0: // Format 1 exactly one value record. michael@0: if (!ParseValueRecord(file, &subtable, data, length, value_format)) { michael@0: return OTS_FAILURE_MSG("Failed to parse format 1 single adjustment table"); michael@0: } michael@0: } else if (format == 2) { michael@0: uint16_t value_count = 0; michael@0: if (!subtable.ReadU16(&value_count)) { michael@0: return OTS_FAILURE_MSG("Failed to parse format 2 single adjustment table"); michael@0: } michael@0: for (unsigned i = 0; i < value_count; ++i) { michael@0: if (!ParseValueRecord(file, &subtable, data, length, value_format)) { michael@0: return OTS_FAILURE_MSG("Failed to parse value record %d in format 2 single adjustment table", i); michael@0: } michael@0: } michael@0: } else { michael@0: return OTS_FAILURE_MSG("Bad format %d in single adjustment table", format); michael@0: } michael@0: michael@0: if (offset_coverage < subtable.offset() || offset_coverage >= length) { michael@0: return OTS_FAILURE_MSG("Bad coverage offset %d in single adjustment table", offset_coverage); michael@0: } michael@0: michael@0: if (!ots::ParseCoverageTable(file, data + offset_coverage, michael@0: length - offset_coverage, michael@0: file->maxp->num_glyphs)) { michael@0: return OTS_FAILURE_MSG("Failed to parse coverage table in single adjustment table"); michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: bool ParsePairSetTable(const ots::OpenTypeFile *file, michael@0: const uint8_t *data, const size_t length, michael@0: const uint16_t value_format1, michael@0: const uint16_t value_format2, michael@0: const uint16_t num_glyphs) { michael@0: ots::Buffer subtable(data, length); michael@0: michael@0: uint16_t value_count = 0; michael@0: if (!subtable.ReadU16(&value_count)) { michael@0: return OTS_FAILURE_MSG("Failed to read pair set table structure"); michael@0: } michael@0: for (unsigned i = 0; i < value_count; ++i) { michael@0: // Check pair value record. michael@0: uint16_t glyph_id = 0; michael@0: if (!subtable.ReadU16(&glyph_id)) { michael@0: return OTS_FAILURE_MSG("Failed to read glyph in pair value record %d", i); michael@0: } michael@0: if (glyph_id >= num_glyphs) { michael@0: return OTS_FAILURE_MSG("glyph id %d too high >= %d", glyph_id, num_glyphs); michael@0: } michael@0: if (!ParseValueRecord(file, &subtable, data, length, value_format1)) { michael@0: return OTS_FAILURE_MSG("Failed to parse value record in format 1 pair set table"); michael@0: } michael@0: if (!ParseValueRecord(file, &subtable, data, length, value_format2)) { michael@0: return OTS_FAILURE_MSG("Failed to parse value record in format 2 pair set table"); michael@0: } michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: bool ParsePairPosFormat1(const ots::OpenTypeFile *file, michael@0: const uint8_t *data, const size_t length, michael@0: const uint16_t value_format1, michael@0: const uint16_t value_format2, michael@0: const uint16_t num_glyphs) { michael@0: ots::Buffer subtable(data, length); michael@0: michael@0: // Skip 8 bytes that are already read before. michael@0: if (!subtable.Skip(8)) { michael@0: return OTS_FAILURE_MSG("Failed to read pair pos table structure"); michael@0: } michael@0: michael@0: uint16_t pair_set_count = 0; michael@0: if (!subtable.ReadU16(&pair_set_count)) { michael@0: return OTS_FAILURE_MSG("Failed to read pair pos set count"); michael@0: } michael@0: michael@0: const unsigned pair_pos_end = 2 * static_cast(pair_set_count) + 10; michael@0: if (pair_pos_end > std::numeric_limits::max()) { michael@0: return OTS_FAILURE_MSG("Bad pair set length %d", pair_pos_end); michael@0: } michael@0: for (unsigned i = 0; i < pair_set_count; ++i) { michael@0: uint16_t pair_set_offset = 0; michael@0: if (!subtable.ReadU16(&pair_set_offset)) { michael@0: return OTS_FAILURE_MSG("Failed to read pair set offset for pair set %d", i); michael@0: } michael@0: if (pair_set_offset < pair_pos_end || pair_set_offset >= length) { michael@0: return OTS_FAILURE_MSG("Bad pair set offset %d for pair set %d", pair_set_offset, i); michael@0: } michael@0: // Check pair set tables michael@0: if (!ParsePairSetTable(file, data + pair_set_offset, length - pair_set_offset, michael@0: value_format1, value_format2, michael@0: num_glyphs)) { michael@0: return OTS_FAILURE_MSG("Failed to parse pair set table %d", i); michael@0: } michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: bool ParsePairPosFormat2(const ots::OpenTypeFile *file, michael@0: const uint8_t *data, const size_t length, michael@0: const uint16_t value_format1, michael@0: const uint16_t value_format2, michael@0: const uint16_t num_glyphs) { michael@0: ots::Buffer subtable(data, length); michael@0: michael@0: // Skip 8 bytes that are already read before. michael@0: if (!subtable.Skip(8)) { michael@0: return OTS_FAILURE_MSG("Failed to read pair pos format 2 structure"); michael@0: } michael@0: michael@0: uint16_t offset_class_def1 = 0; michael@0: uint16_t offset_class_def2 = 0; michael@0: uint16_t class1_count = 0; michael@0: uint16_t class2_count = 0; michael@0: if (!subtable.ReadU16(&offset_class_def1) || michael@0: !subtable.ReadU16(&offset_class_def2) || michael@0: !subtable.ReadU16(&class1_count) || michael@0: !subtable.ReadU16(&class2_count)) { michael@0: return OTS_FAILURE_MSG("Failed to read pair pos format 2 data"); michael@0: } michael@0: michael@0: // Check class 1 records. michael@0: for (unsigned i = 0; i < class1_count; ++i) { michael@0: // Check class 2 records. michael@0: for (unsigned j = 0; j < class2_count; ++j) { michael@0: if (value_format1 && !ParseValueRecord(file, &subtable, data, length, michael@0: value_format1)) { michael@0: return OTS_FAILURE_MSG("Failed to parse value record 1 %d and %d", j, i); michael@0: } michael@0: if (value_format2 && !ParseValueRecord(file, &subtable, data, length, michael@0: value_format2)) { michael@0: return OTS_FAILURE_MSG("Falied to parse value record 2 %d and %d", j, i); michael@0: } michael@0: } michael@0: } michael@0: michael@0: // Check class definition tables. michael@0: if (offset_class_def1 < subtable.offset() || offset_class_def1 >= length || michael@0: offset_class_def2 < subtable.offset() || offset_class_def2 >= length) { michael@0: return OTS_FAILURE_MSG("Bad class definition table offsets %d or %d", offset_class_def1, offset_class_def2); michael@0: } michael@0: if (!ots::ParseClassDefTable(file, data + offset_class_def1, michael@0: length - offset_class_def1, michael@0: num_glyphs, kMaxClassDefValue)) { michael@0: return OTS_FAILURE_MSG("Failed to parse class definition table 1"); michael@0: } michael@0: if (!ots::ParseClassDefTable(file, data + offset_class_def2, michael@0: length - offset_class_def2, michael@0: num_glyphs, kMaxClassDefValue)) { michael@0: return OTS_FAILURE_MSG("Failed to parse class definition table 2"); michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: // Lookup Type 2: michael@0: // Pair Adjustment Positioning Subtable michael@0: bool ParsePairAdjustment(const ots::OpenTypeFile *file, const uint8_t *data, michael@0: const size_t length) { michael@0: ots::Buffer subtable(data, length); michael@0: michael@0: uint16_t format = 0; michael@0: uint16_t offset_coverage = 0; michael@0: uint16_t value_format1 = 0; michael@0: uint16_t value_format2 = 0; michael@0: if (!subtable.ReadU16(&format) || michael@0: !subtable.ReadU16(&offset_coverage) || michael@0: !subtable.ReadU16(&value_format1) || michael@0: !subtable.ReadU16(&value_format2)) { michael@0: return OTS_FAILURE_MSG("Failed to read pair adjustment structure"); michael@0: } michael@0: michael@0: if (format == 1) { michael@0: if (!ParsePairPosFormat1(file, data, length, value_format1, value_format2, michael@0: file->maxp->num_glyphs)) { michael@0: return OTS_FAILURE_MSG("Failed to parse pair pos format 1"); michael@0: } michael@0: } else if (format == 2) { michael@0: if (!ParsePairPosFormat2(file, data, length, value_format1, value_format2, michael@0: file->maxp->num_glyphs)) { michael@0: return OTS_FAILURE_MSG("Failed to parse pair format 2"); michael@0: } michael@0: } else { michael@0: return OTS_FAILURE_MSG("Bad pos pair format %d", format); michael@0: } michael@0: michael@0: if (offset_coverage < subtable.offset() || offset_coverage >= length) { michael@0: return OTS_FAILURE_MSG("Bad pair pos offset coverage %d", offset_coverage); michael@0: } michael@0: if (!ots::ParseCoverageTable(file, data + offset_coverage, michael@0: length - offset_coverage, michael@0: file->maxp->num_glyphs)) { michael@0: return OTS_FAILURE_MSG("Failed to parse coverage table"); michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: // Lookup Type 3 michael@0: // Cursive Attachment Positioning Subtable michael@0: bool ParseCursiveAttachment(const ots::OpenTypeFile *file, const uint8_t *data, michael@0: const size_t length) { michael@0: ots::Buffer subtable(data, length); michael@0: michael@0: uint16_t format = 0; michael@0: uint16_t offset_coverage = 0; michael@0: uint16_t entry_exit_count = 0; michael@0: if (!subtable.ReadU16(&format) || michael@0: !subtable.ReadU16(&offset_coverage) || michael@0: !subtable.ReadU16(&entry_exit_count)) { michael@0: return OTS_FAILURE_MSG("Failed to read cursive attachment structure"); michael@0: } michael@0: michael@0: if (format != 1) { michael@0: return OTS_FAILURE_MSG("Bad cursive attachment format %d", format); michael@0: } michael@0: michael@0: // Check entry exit records. michael@0: const unsigned entry_exit_records_end = michael@0: 2 * static_cast(entry_exit_count) + 6; michael@0: if (entry_exit_records_end > std::numeric_limits::max()) { michael@0: return OTS_FAILURE_MSG("Bad entry exit record end %d", entry_exit_records_end); michael@0: } michael@0: for (unsigned i = 0; i < entry_exit_count; ++i) { michael@0: uint16_t offset_entry_anchor = 0; michael@0: uint16_t offset_exit_anchor = 0; michael@0: if (!subtable.ReadU16(&offset_entry_anchor) || michael@0: !subtable.ReadU16(&offset_exit_anchor)) { michael@0: return OTS_FAILURE_MSG("Can't read entry exit record %d", i); michael@0: } michael@0: // These offsets could be NULL. michael@0: if (offset_entry_anchor) { michael@0: if (offset_entry_anchor < entry_exit_records_end || michael@0: offset_entry_anchor >= length) { michael@0: return OTS_FAILURE_MSG("Bad entry anchor offset %d in entry exit record %d", offset_entry_anchor, i); michael@0: } michael@0: if (!ParseAnchorTable(file, data + offset_entry_anchor, michael@0: length - offset_entry_anchor)) { michael@0: return OTS_FAILURE_MSG("Failed to parse entry anchor table in entry exit record %d", i); michael@0: } michael@0: } michael@0: if (offset_exit_anchor) { michael@0: if (offset_exit_anchor < entry_exit_records_end || michael@0: offset_exit_anchor >= length) { michael@0: return OTS_FAILURE_MSG("Bad exit anchor offset %d in entry exit record %d", offset_exit_anchor, i); michael@0: } michael@0: if (!ParseAnchorTable(file, data + offset_exit_anchor, michael@0: length - offset_exit_anchor)) { michael@0: return OTS_FAILURE_MSG("Failed to parse exit anchor table in entry exit record %d", i); michael@0: } michael@0: } michael@0: } michael@0: michael@0: if (offset_coverage < subtable.offset() || offset_coverage >= length) { michael@0: return OTS_FAILURE_MSG("Bad coverage offset in cursive attachment %d", offset_coverage); michael@0: } michael@0: if (!ots::ParseCoverageTable(file, data + offset_coverage, michael@0: length - offset_coverage, michael@0: file->maxp->num_glyphs)) { michael@0: return OTS_FAILURE_MSG("Failed to parse coverage table in cursive attachment"); michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: bool ParseAnchorArrayTable(const ots::OpenTypeFile *file, michael@0: const uint8_t *data, const size_t length, michael@0: const uint16_t class_count) { michael@0: ots::Buffer subtable(data, length); michael@0: michael@0: uint16_t record_count = 0; michael@0: if (!subtable.ReadU16(&record_count)) { michael@0: return OTS_FAILURE_MSG("Can't read anchor array length"); michael@0: } michael@0: michael@0: const unsigned anchor_array_end = 2 * static_cast(record_count) * michael@0: static_cast(class_count) + 2; michael@0: if (anchor_array_end > std::numeric_limits::max()) { michael@0: return OTS_FAILURE_MSG("Bad end of anchor array %d", anchor_array_end); michael@0: } michael@0: for (unsigned i = 0; i < record_count; ++i) { michael@0: for (unsigned j = 0; j < class_count; ++j) { michael@0: uint16_t offset_record = 0; michael@0: if (!subtable.ReadU16(&offset_record)) { michael@0: return OTS_FAILURE_MSG("Can't read anchor array record offset for class %d and record %d", j, i); michael@0: } michael@0: // |offset_record| could be NULL. michael@0: if (offset_record) { michael@0: if (offset_record < anchor_array_end || offset_record >= length) { michael@0: return OTS_FAILURE_MSG("Bad record offset %d in class %d, record %d", offset_record, j, i); michael@0: } michael@0: if (!ParseAnchorTable(file, data + offset_record, michael@0: length - offset_record)) { michael@0: return OTS_FAILURE_MSG("Failed to parse anchor table for class %d, record %d", j, i); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: bool ParseLigatureArrayTable(const ots::OpenTypeFile *file, michael@0: const uint8_t *data, const size_t length, michael@0: const uint16_t class_count) { michael@0: ots::Buffer subtable(data, length); michael@0: michael@0: uint16_t ligature_count = 0; michael@0: if (!subtable.ReadU16(&ligature_count)) { michael@0: return OTS_FAILURE_MSG("Failed to read ligature count"); michael@0: } michael@0: for (unsigned i = 0; i < ligature_count; ++i) { michael@0: uint16_t offset_ligature_attach = 0; michael@0: if (!subtable.ReadU16(&offset_ligature_attach)) { michael@0: return OTS_FAILURE_MSG("Can't read ligature offset %d", i); michael@0: } michael@0: if (offset_ligature_attach < 2 || offset_ligature_attach >= length) { michael@0: return OTS_FAILURE_MSG("Bad ligature attachment offset %d in ligature %d", offset_ligature_attach, i); michael@0: } michael@0: if (!ParseAnchorArrayTable(file, data + offset_ligature_attach, michael@0: length - offset_ligature_attach, class_count)) { michael@0: return OTS_FAILURE_MSG("Failed to parse anchor table for ligature %d", i); michael@0: } michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: // Common parser for Lookup Type 4, 5 and 6. michael@0: bool ParseMarkToAttachmentSubtables(const ots::OpenTypeFile *file, michael@0: const uint8_t *data, const size_t length, michael@0: const GPOS_TYPE type) { michael@0: ots::Buffer subtable(data, length); michael@0: michael@0: uint16_t format = 0; michael@0: uint16_t offset_coverage1 = 0; michael@0: uint16_t offset_coverage2 = 0; michael@0: uint16_t class_count = 0; michael@0: uint16_t offset_mark_array = 0; michael@0: uint16_t offset_type_specific_array = 0; michael@0: if (!subtable.ReadU16(&format) || michael@0: !subtable.ReadU16(&offset_coverage1) || michael@0: !subtable.ReadU16(&offset_coverage2) || michael@0: !subtable.ReadU16(&class_count) || michael@0: !subtable.ReadU16(&offset_mark_array) || michael@0: !subtable.ReadU16(&offset_type_specific_array)) { michael@0: return OTS_FAILURE_MSG("Failed to read mark attachment subtable header"); michael@0: } michael@0: michael@0: if (format != 1) { michael@0: return OTS_FAILURE_MSG("bad mark attachment subtable format %d", format); michael@0: } michael@0: michael@0: const unsigned header_end = static_cast(subtable.offset()); michael@0: if (header_end > std::numeric_limits::max()) { michael@0: return OTS_FAILURE_MSG("Bad mark attachment subtable size ending at %d", header_end); michael@0: } michael@0: if (offset_coverage1 < header_end || offset_coverage1 >= length) { michael@0: return OTS_FAILURE_MSG("Bad coverage 1 offset %d", offset_coverage1); michael@0: } michael@0: if (!ots::ParseCoverageTable(file, data + offset_coverage1, michael@0: length - offset_coverage1, michael@0: file->maxp->num_glyphs)) { michael@0: return OTS_FAILURE_MSG("Failed to parse converge 1 table"); michael@0: } michael@0: if (offset_coverage2 < header_end || offset_coverage2 >= length) { michael@0: return OTS_FAILURE_MSG("Bad coverage 2 offset %d", offset_coverage2); michael@0: } michael@0: if (!ots::ParseCoverageTable(file, data + offset_coverage2, michael@0: length - offset_coverage2, michael@0: file->maxp->num_glyphs)) { michael@0: return OTS_FAILURE_MSG("Failed to parse coverage table 2"); michael@0: } michael@0: michael@0: if (offset_mark_array < header_end || offset_mark_array >= length) { michael@0: return OTS_FAILURE_MSG("Bad mark array offset %d", offset_mark_array); michael@0: } michael@0: if (!ParseMarkArrayTable(file, data + offset_mark_array, michael@0: length - offset_mark_array, class_count)) { michael@0: return OTS_FAILURE_MSG("Failed to parse mark array"); michael@0: } michael@0: michael@0: if (offset_type_specific_array < header_end || michael@0: offset_type_specific_array >= length) { michael@0: return OTS_FAILURE_MSG("Bad type specific array offset %d", offset_type_specific_array); michael@0: } michael@0: if (type == GPOS_TYPE_MARK_TO_BASE_ATTACHMENT || michael@0: type == GPOS_TYPE_MARK_TO_MARK_ATTACHMENT) { michael@0: if (!ParseAnchorArrayTable(file, data + offset_type_specific_array, michael@0: length - offset_type_specific_array, michael@0: class_count)) { michael@0: return OTS_FAILURE_MSG("Failed to parse anchor array"); michael@0: } michael@0: } else if (type == GPOS_TYPE_MARK_TO_LIGATURE_ATTACHMENT) { michael@0: if (!ParseLigatureArrayTable(file, data + offset_type_specific_array, michael@0: length - offset_type_specific_array, michael@0: class_count)) { michael@0: return OTS_FAILURE_MSG("Failed to parse ligature array"); michael@0: } michael@0: } else { michael@0: return OTS_FAILURE_MSG("Bad attachment type %d", type); michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: // Lookup Type 4: michael@0: // MarkToBase Attachment Positioning Subtable michael@0: bool ParseMarkToBaseAttachment(const ots::OpenTypeFile *file, michael@0: const uint8_t *data, const size_t length) { michael@0: return ParseMarkToAttachmentSubtables(file, data, length, michael@0: GPOS_TYPE_MARK_TO_BASE_ATTACHMENT); michael@0: } michael@0: michael@0: // Lookup Type 5: michael@0: // MarkToLigature Attachment Positioning Subtable michael@0: bool ParseMarkToLigatureAttachment(const ots::OpenTypeFile *file, michael@0: const uint8_t *data, const size_t length) { michael@0: return ParseMarkToAttachmentSubtables(file, data, length, michael@0: GPOS_TYPE_MARK_TO_LIGATURE_ATTACHMENT); michael@0: } michael@0: michael@0: // Lookup Type 6: michael@0: // MarkToMark Attachment Positioning Subtable michael@0: bool ParseMarkToMarkAttachment(const ots::OpenTypeFile *file, michael@0: const uint8_t *data, const size_t length) { michael@0: return ParseMarkToAttachmentSubtables(file, data, length, michael@0: GPOS_TYPE_MARK_TO_MARK_ATTACHMENT); michael@0: } michael@0: michael@0: // Lookup Type 7: michael@0: // Contextual Positioning Subtables michael@0: bool ParseContextPositioning(const ots::OpenTypeFile *file, michael@0: const uint8_t *data, const size_t length) { michael@0: return ots::ParseContextSubtable(file, data, length, file->maxp->num_glyphs, michael@0: file->gpos->num_lookups); michael@0: } michael@0: michael@0: // Lookup Type 8: michael@0: // Chaining Contexual Positioning Subtable michael@0: bool ParseChainedContextPositioning(const ots::OpenTypeFile *file, michael@0: const uint8_t *data, const size_t length) { michael@0: return ots::ParseChainingContextSubtable(file, data, length, michael@0: file->maxp->num_glyphs, michael@0: file->gpos->num_lookups); michael@0: } michael@0: michael@0: // Lookup Type 9: michael@0: // Extension Positioning michael@0: bool ParseExtensionPositioning(const ots::OpenTypeFile *file, michael@0: const uint8_t *data, const size_t length) { michael@0: return ots::ParseExtensionSubtable(file, data, length, michael@0: &kGposLookupSubtableParser); michael@0: } michael@0: michael@0: } // namespace michael@0: michael@0: #define DROP_THIS_TABLE(msg_) \ michael@0: do { \ michael@0: file->gpos->data = 0; \ michael@0: file->gpos->length = 0; \ michael@0: OTS_FAILURE_MSG(msg_ ", table discarded"); \ michael@0: } while (0) michael@0: michael@0: namespace ots { michael@0: michael@0: // As far as I checked, following fonts contain invalid GPOS table and michael@0: // OTS will drop their GPOS table. michael@0: // michael@0: // # invalid delta format in device table michael@0: // samanata.ttf michael@0: // michael@0: // # bad size range in device table michael@0: // Sarai_07.ttf michael@0: // michael@0: // # bad offset to PairSetTable michael@0: // chandas1-2.ttf michael@0: // michael@0: // # bad offset to FeatureTable michael@0: // glrso12.ttf michael@0: // gllr12.ttf michael@0: // glbo12.ttf michael@0: // glb12.ttf michael@0: // glro12.ttf michael@0: // glbso12.ttf michael@0: // glrc12.ttf michael@0: // glrsc12.ttf michael@0: // glbs12.ttf michael@0: // glrs12.ttf michael@0: // glr12.ttf michael@0: // michael@0: // # ScriptRecords aren't sorted by tag michael@0: // Garogier_unhinted.otf michael@0: // michael@0: // # bad start coverage index in CoverageFormat2 michael@0: // AndBasR.ttf michael@0: // CharisSILB.ttf michael@0: // CharisSILBI.ttf michael@0: // CharisSILI.ttf michael@0: // CharisSILR.ttf michael@0: // DoulosSILR.ttf michael@0: // GenBasBI.ttf michael@0: // GenBasI.ttf michael@0: // GenBkBasI.ttf michael@0: // GenBkBasB.ttf michael@0: // GenBkBasR.ttf michael@0: // Padauk-Bold.ttf michael@0: // Padauk.ttf michael@0: // michael@0: // # Contour point indexes aren't sorted michael@0: // Arial Unicode.ttf michael@0: michael@0: bool ots_gpos_parse(OpenTypeFile *file, const uint8_t *data, size_t length) { michael@0: // Parsing GPOS table requires num_glyphs which is contained in maxp table. michael@0: if (!file->maxp) { michael@0: return OTS_FAILURE_MSG("missing maxp table needed in GPOS"); michael@0: } michael@0: michael@0: Buffer table(data, length); michael@0: michael@0: OpenTypeGPOS *gpos = new OpenTypeGPOS; michael@0: file->gpos = gpos; michael@0: michael@0: uint32_t version = 0; michael@0: uint16_t offset_script_list = 0; michael@0: uint16_t offset_feature_list = 0; michael@0: uint16_t offset_lookup_list = 0; michael@0: if (!table.ReadU32(&version) || michael@0: !table.ReadU16(&offset_script_list) || michael@0: !table.ReadU16(&offset_feature_list) || michael@0: !table.ReadU16(&offset_lookup_list)) { michael@0: DROP_THIS_TABLE("Incomplete table"); michael@0: return true; michael@0: } michael@0: michael@0: if (version != 0x00010000) { michael@0: DROP_THIS_TABLE("Bad version"); michael@0: return true; michael@0: } michael@0: if ((offset_script_list < kGposHeaderSize || michael@0: offset_script_list >= length) || michael@0: (offset_feature_list < kGposHeaderSize || michael@0: offset_feature_list >= length) || michael@0: (offset_lookup_list < kGposHeaderSize || michael@0: offset_lookup_list >= length)) { michael@0: DROP_THIS_TABLE("Bad offset in table header"); michael@0: return true; michael@0: } michael@0: michael@0: if (!ParseLookupListTable(file, data + offset_lookup_list, michael@0: length - offset_lookup_list, michael@0: &kGposLookupSubtableParser, michael@0: &gpos->num_lookups)) { michael@0: DROP_THIS_TABLE("Failed to parse lookup list table"); michael@0: return true; michael@0: } michael@0: michael@0: uint16_t num_features = 0; michael@0: if (!ParseFeatureListTable(file, data + offset_feature_list, michael@0: length - offset_feature_list, gpos->num_lookups, michael@0: &num_features)) { michael@0: DROP_THIS_TABLE("Failed to parse feature list table"); michael@0: return true; michael@0: } michael@0: michael@0: if (!ParseScriptListTable(file, data + offset_script_list, michael@0: length - offset_script_list, num_features)) { michael@0: DROP_THIS_TABLE("Failed to parse script list table"); michael@0: return true; michael@0: } michael@0: michael@0: gpos->data = data; michael@0: gpos->length = length; michael@0: return true; michael@0: } michael@0: michael@0: bool ots_gpos_should_serialise(OpenTypeFile *file) { michael@0: return file->gpos != NULL && file->gpos->data != NULL; michael@0: } michael@0: michael@0: bool ots_gpos_serialise(OTSStream *out, OpenTypeFile *file) { michael@0: if (!out->Write(file->gpos->data, file->gpos->length)) { michael@0: return OTS_FAILURE_MSG("Failed to write GPOS table"); michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: void ots_gpos_free(OpenTypeFile *file) { michael@0: delete file->gpos; michael@0: } michael@0: michael@0: } // namespace ots michael@0: