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 "gsub.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: // GSUB - The Glyph Substitution Table michael@0: // http://www.microsoft.com/typography/otspec/gsub.htm michael@0: michael@0: #define TABLE_NAME "GSUB" michael@0: michael@0: namespace { michael@0: michael@0: // The GSUB header size michael@0: const size_t kGsubHeaderSize = 4 + 3 * 2; michael@0: michael@0: enum GSUB_TYPE { michael@0: GSUB_TYPE_SINGLE = 1, michael@0: GSUB_TYPE_MULTIPLE = 2, michael@0: GSUB_TYPE_ALTERNATE = 3, michael@0: GSUB_TYPE_LIGATURE = 4, michael@0: GSUB_TYPE_CONTEXT = 5, michael@0: GSUB_TYPE_CHANGING_CONTEXT = 6, michael@0: GSUB_TYPE_EXTENSION_SUBSTITUTION = 7, michael@0: GSUB_TYPE_REVERSE_CHAINING_CONTEXT_SINGLE = 8, michael@0: GSUB_TYPE_RESERVED = 9 michael@0: }; michael@0: michael@0: // Lookup type parsers. michael@0: bool ParseSingleSubstitution(const ots::OpenTypeFile *file, michael@0: const uint8_t *data, const size_t length); michael@0: bool ParseMutipleSubstitution(const ots::OpenTypeFile *file, michael@0: const uint8_t *data, const size_t length); michael@0: bool ParseAlternateSubstitution(const ots::OpenTypeFile *file, michael@0: const uint8_t *data, const size_t length); michael@0: bool ParseLigatureSubstitution(const ots::OpenTypeFile *file, michael@0: const uint8_t *data, const size_t length); michael@0: bool ParseContextSubstitution(const ots::OpenTypeFile *file, michael@0: const uint8_t *data, const size_t length); michael@0: bool ParseChainingContextSubstitution(const ots::OpenTypeFile *file, michael@0: const uint8_t *data, michael@0: const size_t length); michael@0: bool ParseExtensionSubstitution(const ots::OpenTypeFile *file, michael@0: const uint8_t *data, const size_t length); michael@0: bool ParseReverseChainingContextSingleSubstitution( michael@0: const ots::OpenTypeFile *file, const uint8_t *data, const size_t length); michael@0: michael@0: const ots::LookupSubtableParser::TypeParser kGsubTypeParsers[] = { michael@0: {GSUB_TYPE_SINGLE, ParseSingleSubstitution}, michael@0: {GSUB_TYPE_MULTIPLE, ParseMutipleSubstitution}, michael@0: {GSUB_TYPE_ALTERNATE, ParseAlternateSubstitution}, michael@0: {GSUB_TYPE_LIGATURE, ParseLigatureSubstitution}, michael@0: {GSUB_TYPE_CONTEXT, ParseContextSubstitution}, michael@0: {GSUB_TYPE_CHANGING_CONTEXT, ParseChainingContextSubstitution}, michael@0: {GSUB_TYPE_EXTENSION_SUBSTITUTION, ParseExtensionSubstitution}, michael@0: {GSUB_TYPE_REVERSE_CHAINING_CONTEXT_SINGLE, michael@0: ParseReverseChainingContextSingleSubstitution} michael@0: }; michael@0: michael@0: const ots::LookupSubtableParser kGsubLookupSubtableParser = { michael@0: arraysize(kGsubTypeParsers), michael@0: GSUB_TYPE_EXTENSION_SUBSTITUTION, kGsubTypeParsers michael@0: }; michael@0: michael@0: // Lookup Type 1: michael@0: // Single Substitution Subtable michael@0: bool ParseSingleSubstitution(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: uint16_t offset_coverage = 0; michael@0: michael@0: if (!subtable.ReadU16(&format) || michael@0: !subtable.ReadU16(&offset_coverage)) { michael@0: return OTS_FAILURE_MSG("Failed to read single subst table header"); michael@0: } michael@0: michael@0: const uint16_t num_glyphs = file->maxp->num_glyphs; michael@0: if (format == 1) { michael@0: // Parse SingleSubstFormat1 michael@0: int16_t delta_glyph_id = 0; michael@0: if (!subtable.ReadS16(&delta_glyph_id)) { michael@0: return OTS_FAILURE_MSG("Failed to read glyph shift from format 1 single subst table"); michael@0: } michael@0: if (std::abs(delta_glyph_id) >= num_glyphs) { michael@0: return OTS_FAILURE_MSG("bad glyph shift of %d in format 1 single subst table", delta_glyph_id); michael@0: } michael@0: } else if (format == 2) { michael@0: // Parse SingleSubstFormat2 michael@0: uint16_t glyph_count = 0; michael@0: if (!subtable.ReadU16(&glyph_count)) { michael@0: return OTS_FAILURE_MSG("Failed to read glyph cound in format 2 single subst table"); michael@0: } michael@0: if (glyph_count > num_glyphs) { michael@0: return OTS_FAILURE_MSG("Bad glyph count %d > %d in format 2 single subst table", glyph_count, num_glyphs); michael@0: } michael@0: for (unsigned i = 0; i < glyph_count; ++i) { michael@0: uint16_t substitute = 0; michael@0: if (!subtable.ReadU16(&substitute)) { michael@0: return OTS_FAILURE_MSG("Failed to read substitution %d in format 2 single subst table", i); michael@0: } michael@0: if (substitute >= num_glyphs) { michael@0: return OTS_FAILURE_MSG("too large substitute: %u", substitute); michael@0: } michael@0: } michael@0: } else { michael@0: return OTS_FAILURE_MSG("Bad single subst table format %d", format); michael@0: } michael@0: michael@0: if (offset_coverage < subtable.offset() || offset_coverage >= length) { michael@0: return OTS_FAILURE_MSG("Bad coverage offset %x", offset_coverage); michael@0: } michael@0: if (!ots::ParseCoverageTable(file, data + offset_coverage, michael@0: length - offset_coverage, 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: bool ParseSequenceTable(const ots::OpenTypeFile *file, michael@0: const uint8_t *data, const size_t length, michael@0: const uint16_t num_glyphs) { michael@0: ots::Buffer subtable(data, length); michael@0: michael@0: uint16_t glyph_count = 0; michael@0: if (!subtable.ReadU16(&glyph_count)) { michael@0: return OTS_FAILURE_MSG("Failed to read glyph count in sequence table"); michael@0: } michael@0: if (glyph_count > num_glyphs) { michael@0: return OTS_FAILURE_MSG("bad glyph count %d > %d", glyph_count, num_glyphs); michael@0: } michael@0: for (unsigned i = 0; i < glyph_count; ++i) { michael@0: uint16_t substitute = 0; michael@0: if (!subtable.ReadU16(&substitute)) { michael@0: return OTS_FAILURE_MSG("Failedt o read substitution %d in sequence table", i); michael@0: } michael@0: if (substitute >= num_glyphs) { michael@0: return OTS_FAILURE_MSG("Bad subsitution (%d) %d > %d", i, substitute, num_glyphs); michael@0: } michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: // Lookup Type 2: michael@0: // Multiple Substitution Subtable michael@0: bool ParseMutipleSubstitution(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: uint16_t offset_coverage = 0; michael@0: uint16_t sequence_count = 0; michael@0: michael@0: if (!subtable.ReadU16(&format) || michael@0: !subtable.ReadU16(&offset_coverage) || michael@0: !subtable.ReadU16(&sequence_count)) { michael@0: return OTS_FAILURE_MSG("Can't read header of multiple subst table"); michael@0: } michael@0: michael@0: if (format != 1) { michael@0: return OTS_FAILURE_MSG("Bad multiple subst table format %d", format); michael@0: } michael@0: michael@0: const uint16_t num_glyphs = file->maxp->num_glyphs; michael@0: const unsigned sequence_end = static_cast(6) + michael@0: sequence_count * 2; michael@0: if (sequence_end > std::numeric_limits::max()) { michael@0: return OTS_FAILURE_MSG("Bad segence end %d, in multiple subst", sequence_end); michael@0: } michael@0: for (unsigned i = 0; i < sequence_count; ++i) { michael@0: uint16_t offset_sequence = 0; michael@0: if (!subtable.ReadU16(&offset_sequence)) { michael@0: return OTS_FAILURE_MSG("Failed to read sequence offset for sequence %d", i); michael@0: } michael@0: if (offset_sequence < sequence_end || offset_sequence >= length) { michael@0: return OTS_FAILURE_MSG("Bad sequence offset %d for sequence %d", offset_sequence, i); michael@0: } michael@0: if (!ParseSequenceTable(file, data + offset_sequence, length - offset_sequence, michael@0: num_glyphs)) { michael@0: return OTS_FAILURE_MSG("Failed to parse sequence table %d", i); michael@0: } michael@0: } michael@0: michael@0: if (offset_coverage < sequence_end || offset_coverage >= length) { michael@0: return OTS_FAILURE_MSG("Bad coverage offset %d", offset_coverage); michael@0: } michael@0: if (!ots::ParseCoverageTable(file, data + offset_coverage, michael@0: length - offset_coverage, 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: bool ParseAlternateSetTable(const ots::OpenTypeFile *file, michael@0: const uint8_t *data, const size_t length, michael@0: const uint16_t num_glyphs) { michael@0: ots::Buffer subtable(data, length); michael@0: michael@0: uint16_t glyph_count = 0; michael@0: if (!subtable.ReadU16(&glyph_count)) { michael@0: return OTS_FAILURE_MSG("Failed to read alternate set header"); michael@0: } michael@0: if (glyph_count > num_glyphs) { michael@0: return OTS_FAILURE_MSG("Bad glyph count %d > %d in alternate set table", glyph_count, num_glyphs); michael@0: } michael@0: for (unsigned i = 0; i < glyph_count; ++i) { michael@0: uint16_t alternate = 0; michael@0: if (!subtable.ReadU16(&alternate)) { michael@0: return OTS_FAILURE_MSG("Can't read alternate %d", i); michael@0: } michael@0: if (alternate >= num_glyphs) { michael@0: return OTS_FAILURE_MSG("Too large alternate: %u", alternate); michael@0: } michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: // Lookup Type 3: michael@0: // Alternate Substitution Subtable michael@0: bool ParseAlternateSubstitution(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: uint16_t offset_coverage = 0; michael@0: uint16_t alternate_set_count = 0; michael@0: michael@0: if (!subtable.ReadU16(&format) || michael@0: !subtable.ReadU16(&offset_coverage) || michael@0: !subtable.ReadU16(&alternate_set_count)) { michael@0: return OTS_FAILURE_MSG("Can't read alternate subst header"); michael@0: } michael@0: michael@0: if (format != 1) { michael@0: return OTS_FAILURE_MSG("Bad alternate subst table format %d", format); michael@0: } michael@0: michael@0: const uint16_t num_glyphs = file->maxp->num_glyphs; michael@0: const unsigned alternate_set_end = static_cast(6) + michael@0: alternate_set_count * 2; michael@0: if (alternate_set_end > std::numeric_limits::max()) { michael@0: return OTS_FAILURE_MSG("Bad end of alternate set %d", alternate_set_end); michael@0: } michael@0: for (unsigned i = 0; i < alternate_set_count; ++i) { michael@0: uint16_t offset_alternate_set = 0; michael@0: if (!subtable.ReadU16(&offset_alternate_set)) { michael@0: return OTS_FAILURE_MSG("Can't read alternate set offset for set %d", i); michael@0: } michael@0: if (offset_alternate_set < alternate_set_end || michael@0: offset_alternate_set >= length) { michael@0: return OTS_FAILURE_MSG("Bad alternate set offset %d for set %d", offset_alternate_set, i); michael@0: } michael@0: if (!ParseAlternateSetTable(file, data + offset_alternate_set, michael@0: length - offset_alternate_set, michael@0: num_glyphs)) { michael@0: return OTS_FAILURE_MSG("Failed to parse alternate set"); michael@0: } michael@0: } michael@0: michael@0: if (offset_coverage < alternate_set_end || offset_coverage >= length) { michael@0: return OTS_FAILURE_MSG("Bad coverage offset %d", offset_coverage); michael@0: } michael@0: if (!ots::ParseCoverageTable(file, data + offset_coverage, michael@0: length - offset_coverage, 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: bool ParseLigatureTable(const ots::OpenTypeFile *file, michael@0: const uint8_t *data, const size_t length, michael@0: const uint16_t num_glyphs) { michael@0: ots::Buffer subtable(data, length); michael@0: michael@0: uint16_t lig_glyph = 0; michael@0: uint16_t comp_count = 0; michael@0: michael@0: if (!subtable.ReadU16(&lig_glyph) || michael@0: !subtable.ReadU16(&comp_count)) { michael@0: return OTS_FAILURE_MSG("Failed to read ligatuer table header"); michael@0: } michael@0: michael@0: if (lig_glyph >= num_glyphs) { michael@0: return OTS_FAILURE_MSG("too large lig_glyph: %u", lig_glyph); michael@0: } michael@0: if (comp_count == 0 || comp_count > num_glyphs) { michael@0: return OTS_FAILURE_MSG("Bad component count of %d", comp_count); michael@0: } michael@0: for (unsigned i = 0; i < comp_count - static_cast(1); ++i) { michael@0: uint16_t component = 0; michael@0: if (!subtable.ReadU16(&component)) { michael@0: return OTS_FAILURE_MSG("Can't read ligature component %d", i); michael@0: } michael@0: if (component >= num_glyphs) { michael@0: return OTS_FAILURE_MSG("Bad ligature component %d of %d", i, component); michael@0: } michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: bool ParseLigatureSetTable(const ots::OpenTypeFile *file, michael@0: const uint8_t *data, const size_t length, michael@0: const uint16_t num_glyphs) { michael@0: ots::Buffer subtable(data, length); michael@0: michael@0: uint16_t ligature_count = 0; michael@0: michael@0: if (!subtable.ReadU16(&ligature_count)) { michael@0: return OTS_FAILURE_MSG("Can't read ligature count in ligature set"); michael@0: } michael@0: michael@0: const unsigned ligature_end = static_cast(2) + ligature_count * 2; michael@0: if (ligature_end > std::numeric_limits::max()) { michael@0: return OTS_FAILURE_MSG("Bad end of ligature %d in ligature set", ligature_end); michael@0: } michael@0: for (unsigned i = 0; i < ligature_count; ++i) { michael@0: uint16_t offset_ligature = 0; michael@0: if (!subtable.ReadU16(&offset_ligature)) { michael@0: return OTS_FAILURE_MSG("Failed to read ligature offset %d", i); michael@0: } michael@0: if (offset_ligature < ligature_end || offset_ligature >= length) { michael@0: return OTS_FAILURE_MSG("Bad ligature offset %d for ligature %d", offset_ligature, i); michael@0: } michael@0: if (!ParseLigatureTable(file, data + offset_ligature, length - offset_ligature, michael@0: num_glyphs)) { michael@0: return OTS_FAILURE_MSG("Failed to parse ligature %d", i); michael@0: } michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: // Lookup Type 4: michael@0: // Ligature Substitution Subtable michael@0: bool ParseLigatureSubstitution(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: uint16_t offset_coverage = 0; michael@0: uint16_t lig_set_count = 0; michael@0: michael@0: if (!subtable.ReadU16(&format) || michael@0: !subtable.ReadU16(&offset_coverage) || michael@0: !subtable.ReadU16(&lig_set_count)) { michael@0: return OTS_FAILURE_MSG("Failed to read ligature substitution header"); michael@0: } michael@0: michael@0: if (format != 1) { michael@0: return OTS_FAILURE_MSG("Bad ligature substitution table format %d", format); michael@0: } michael@0: michael@0: const uint16_t num_glyphs = file->maxp->num_glyphs; michael@0: const unsigned ligature_set_end = static_cast(6) + michael@0: lig_set_count * 2; michael@0: if (ligature_set_end > std::numeric_limits::max()) { michael@0: return OTS_FAILURE_MSG("Bad end of ligature set %d in ligature substitution table", ligature_set_end); michael@0: } michael@0: for (unsigned i = 0; i < lig_set_count; ++i) { michael@0: uint16_t offset_ligature_set = 0; michael@0: if (!subtable.ReadU16(&offset_ligature_set)) { michael@0: return OTS_FAILURE_MSG("Can't read ligature set offset %d", i); michael@0: } michael@0: if (offset_ligature_set < ligature_set_end || michael@0: offset_ligature_set >= length) { michael@0: return OTS_FAILURE_MSG("Bad ligature set offset %d for set %d", offset_ligature_set, i); michael@0: } michael@0: if (!ParseLigatureSetTable(file, data + offset_ligature_set, michael@0: length - offset_ligature_set, num_glyphs)) { michael@0: return OTS_FAILURE_MSG("Failed to parse ligature set %d", i); michael@0: } michael@0: } michael@0: michael@0: if (offset_coverage < ligature_set_end || offset_coverage >= length) { michael@0: return OTS_FAILURE_MSG("Bad coverage offset %d", offset_coverage); michael@0: } michael@0: if (!ots::ParseCoverageTable(file, data + offset_coverage, michael@0: length - offset_coverage, 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 5: michael@0: // Contextual Substitution Subtable michael@0: bool ParseContextSubstitution(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->gsub->num_lookups); michael@0: } michael@0: michael@0: // Lookup Type 6: michael@0: // Chaining Contextual Substitution Subtable michael@0: bool ParseChainingContextSubstitution(const ots::OpenTypeFile *file, michael@0: const uint8_t *data, michael@0: const size_t length) { michael@0: return ots::ParseChainingContextSubtable(file, data, length, michael@0: file->maxp->num_glyphs, michael@0: file->gsub->num_lookups); michael@0: } michael@0: michael@0: // Lookup Type 7: michael@0: // Extension Substition michael@0: bool ParseExtensionSubstitution(const ots::OpenTypeFile *file, michael@0: const uint8_t *data, const size_t length) { michael@0: return ots::ParseExtensionSubtable(file, data, length, michael@0: &kGsubLookupSubtableParser); michael@0: } michael@0: michael@0: // Lookup Type 8: michael@0: // Reverse Chaining Contexual Single Substitution Subtable michael@0: bool ParseReverseChainingContextSingleSubstitution( michael@0: const ots::OpenTypeFile *file, 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: uint16_t offset_coverage = 0; michael@0: michael@0: if (!subtable.ReadU16(&format) || michael@0: !subtable.ReadU16(&offset_coverage)) { michael@0: return OTS_FAILURE_MSG("Failed to read reverse chaining header"); michael@0: } michael@0: michael@0: const uint16_t num_glyphs = file->maxp->num_glyphs; michael@0: michael@0: uint16_t backtrack_glyph_count = 0; michael@0: if (!subtable.ReadU16(&backtrack_glyph_count)) { michael@0: return OTS_FAILURE_MSG("Failed to read backtrack glyph count in reverse chaining table"); michael@0: } michael@0: if (backtrack_glyph_count > num_glyphs) { michael@0: return OTS_FAILURE_MSG("Bad backtrack glyph count of %d", backtrack_glyph_count); michael@0: } michael@0: std::vector offsets_backtrack; michael@0: offsets_backtrack.reserve(backtrack_glyph_count); michael@0: for (unsigned i = 0; i < backtrack_glyph_count; ++i) { michael@0: uint16_t offset = 0; michael@0: if (!subtable.ReadU16(&offset)) { michael@0: return OTS_FAILURE_MSG("Failed to read backtrack offset %d", i); michael@0: } michael@0: offsets_backtrack.push_back(offset); michael@0: } michael@0: michael@0: uint16_t lookahead_glyph_count = 0; michael@0: if (!subtable.ReadU16(&lookahead_glyph_count)) { michael@0: return OTS_FAILURE_MSG("Failed to read look ahead glyph count"); michael@0: } michael@0: if (lookahead_glyph_count > num_glyphs) { michael@0: return OTS_FAILURE_MSG("Bad look ahead glyph count %d", lookahead_glyph_count); michael@0: } michael@0: std::vector offsets_lookahead; michael@0: offsets_lookahead.reserve(lookahead_glyph_count); michael@0: for (unsigned i = 0; i < lookahead_glyph_count; ++i) { michael@0: uint16_t offset = 0; michael@0: if (!subtable.ReadU16(&offset)) { michael@0: return OTS_FAILURE_MSG("Can't read look ahead offset %d", i); michael@0: } michael@0: offsets_lookahead.push_back(offset); michael@0: } michael@0: michael@0: uint16_t glyph_count = 0; michael@0: if (!subtable.ReadU16(&glyph_count)) { michael@0: return OTS_FAILURE_MSG("Can't read glyph count in reverse chaining table"); michael@0: } michael@0: if (glyph_count > num_glyphs) { michael@0: return OTS_FAILURE_MSG("Bad glyph count of %d", glyph_count); michael@0: } michael@0: for (unsigned i = 0; i < glyph_count; ++i) { michael@0: uint16_t substitute = 0; michael@0: if (!subtable.ReadU16(&substitute)) { michael@0: return OTS_FAILURE_MSG("Failed to read substitution %d reverse chaining table", i); michael@0: } michael@0: if (substitute >= num_glyphs) { michael@0: return OTS_FAILURE_MSG("Bad substitute glyph %d in reverse chaining table substitution %d", substitute, i); michael@0: } michael@0: } michael@0: michael@0: const unsigned substitute_end = static_cast(10) + michael@0: (backtrack_glyph_count + lookahead_glyph_count + glyph_count) * 2; michael@0: if (substitute_end > std::numeric_limits::max()) { michael@0: return OTS_FAILURE_MSG("Bad substitute end offset in reverse chaining table"); michael@0: } michael@0: michael@0: if (offset_coverage < substitute_end || offset_coverage >= length) { michael@0: return OTS_FAILURE_MSG("Bad coverage offset %d in reverse chaining table", offset_coverage); michael@0: } michael@0: if (!ots::ParseCoverageTable(file, data + offset_coverage, michael@0: length - offset_coverage, num_glyphs)) { michael@0: return OTS_FAILURE_MSG("Failed to parse coverage table in reverse chaining table"); michael@0: } michael@0: michael@0: for (unsigned i = 0; i < backtrack_glyph_count; ++i) { michael@0: if (offsets_backtrack[i] < substitute_end || michael@0: offsets_backtrack[i] >= length) { michael@0: return OTS_FAILURE_MSG("Bad backtrack offset %d for backtrack %d in reverse chaining table", offsets_backtrack[i], i); michael@0: } michael@0: if (!ots::ParseCoverageTable(file, data + offsets_backtrack[i], michael@0: length - offsets_backtrack[i], num_glyphs)) { michael@0: return OTS_FAILURE_MSG("Failed to parse coverage table for backtrack %d in reverse chaining table", i); michael@0: } michael@0: } michael@0: michael@0: for (unsigned i = 0; i < lookahead_glyph_count; ++i) { michael@0: if (offsets_lookahead[i] < substitute_end || michael@0: offsets_lookahead[i] >= length) { michael@0: return OTS_FAILURE_MSG("Bad lookahead offset %d for lookahead %d in reverse chaining table", offsets_lookahead[i], i); michael@0: } michael@0: if (!ots::ParseCoverageTable(file, data + offsets_lookahead[i], michael@0: length - offsets_lookahead[i], num_glyphs)) { michael@0: return OTS_FAILURE_MSG("Failed to parse lookahead coverage table %d in reverse chaining table", i); michael@0: } michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: } // namespace michael@0: michael@0: #define DROP_THIS_TABLE(msg_) \ michael@0: do { \ michael@0: file->gsub->data = 0; \ michael@0: file->gsub->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 values in GSUB table. michael@0: // OTS will drop their GSUB table. michael@0: // michael@0: // # too large substitute (value is 0xFFFF) michael@0: // kaiu.ttf michael@0: // mingliub2.ttf michael@0: // mingliub1.ttf michael@0: // mingliub0.ttf michael@0: // GraublauWeb.otf michael@0: // GraublauWebBold.otf michael@0: // michael@0: // # too large alternate (value is 0xFFFF) michael@0: // ManchuFont.ttf michael@0: // michael@0: // # bad offset to lang sys table (NULL offset) michael@0: // DejaVuMonoSansBold.ttf michael@0: // DejaVuMonoSansBoldOblique.ttf michael@0: // DejaVuMonoSansOblique.ttf michael@0: // DejaVuSansMono-BoldOblique.ttf michael@0: // DejaVuSansMono-Oblique.ttf michael@0: // DejaVuSansMono-Bold.ttf michael@0: // michael@0: // # bad start coverage index michael@0: // GenBasBI.ttf michael@0: // GenBasI.ttf michael@0: // AndBasR.ttf michael@0: // GenBkBasI.ttf michael@0: // CharisSILR.ttf michael@0: // CharisSILBI.ttf michael@0: // CharisSILI.ttf michael@0: // CharisSILB.ttf michael@0: // DoulosSILR.ttf michael@0: // CharisSILBI.ttf michael@0: // GenBkBasB.ttf michael@0: // GenBkBasR.ttf michael@0: // GenBkBasBI.ttf michael@0: // GenBasB.ttf michael@0: // GenBasR.ttf michael@0: // michael@0: // # glyph range is overlapping michael@0: // KacstTitleL.ttf michael@0: // KacstDecorative.ttf michael@0: // KacstTitle.ttf michael@0: // KacstArt.ttf michael@0: // KacstPoster.ttf michael@0: // KacstQurn.ttf michael@0: // KacstDigital.ttf michael@0: // KacstBook.ttf michael@0: // KacstFarsi.ttf michael@0: michael@0: bool ots_gsub_parse(OpenTypeFile *file, const uint8_t *data, size_t length) { michael@0: // Parsing gsub table requires |file->maxp->num_glyphs| michael@0: if (!file->maxp) { michael@0: return OTS_FAILURE_MSG("Missing maxp table in font, needed by GSUB"); michael@0: } michael@0: michael@0: Buffer table(data, length); michael@0: michael@0: OpenTypeGSUB *gsub = new OpenTypeGSUB; michael@0: file->gsub = gsub; 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 < kGsubHeaderSize || michael@0: offset_script_list >= length) || michael@0: (offset_feature_list < kGsubHeaderSize || michael@0: offset_feature_list >= length) || michael@0: (offset_lookup_list < kGsubHeaderSize || 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: &kGsubLookupSubtableParser, michael@0: &gsub->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, gsub->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: gsub->data = data; michael@0: gsub->length = length; michael@0: return true; michael@0: } michael@0: michael@0: bool ots_gsub_should_serialise(OpenTypeFile *file) { michael@0: return file->gsub != NULL && file->gsub->data != NULL; michael@0: } michael@0: michael@0: bool ots_gsub_serialise(OTSStream *out, OpenTypeFile *file) { michael@0: if (!out->Write(file->gsub->data, file->gsub->length)) { michael@0: return OTS_FAILURE_MSG("Failed to write GSUB table"); michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: void ots_gsub_free(OpenTypeFile *file) { michael@0: delete file->gsub; michael@0: } michael@0: michael@0: } // namespace ots michael@0: