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 "gdef.h" michael@0: michael@0: #include michael@0: #include michael@0: michael@0: #include "gpos.h" michael@0: #include "gsub.h" michael@0: #include "layout.h" michael@0: #include "maxp.h" michael@0: michael@0: // GDEF - The Glyph Definition Table michael@0: // http://www.microsoft.com/typography/otspec/gdef.htm michael@0: michael@0: #define TABLE_NAME "GDEF" michael@0: michael@0: namespace { michael@0: michael@0: // The maximum class value in class definition tables. michael@0: const uint16_t kMaxClassDefValue = 0xFFFF; michael@0: // The maximum class value in the glyph class definision table. michael@0: const uint16_t kMaxGlyphClassDefValue = 4; michael@0: // The maximum format number of caret value tables. michael@0: // We don't support format 3 for now. See the comment in michael@0: // ParseLigCaretListTable() for the reason. michael@0: const uint16_t kMaxCaretValueFormat = 2; michael@0: michael@0: bool ParseGlyphClassDefTable(ots::OpenTypeFile *file, const uint8_t *data, michael@0: size_t length, const uint16_t num_glyphs) { michael@0: return ots::ParseClassDefTable(file, data, length, num_glyphs, michael@0: kMaxGlyphClassDefValue); michael@0: } michael@0: michael@0: bool ParseAttachListTable(ots::OpenTypeFile *file, const uint8_t *data, michael@0: size_t length, const uint16_t num_glyphs) { michael@0: ots::Buffer subtable(data, length); michael@0: michael@0: uint16_t offset_coverage = 0; michael@0: uint16_t glyph_count = 0; michael@0: if (!subtable.ReadU16(&offset_coverage) || michael@0: !subtable.ReadU16(&glyph_count)) { michael@0: return OTS_FAILURE_MSG("Failed to read gdef header"); michael@0: } michael@0: const unsigned attach_points_end = michael@0: 2 * static_cast(glyph_count) + 4; michael@0: if (attach_points_end > std::numeric_limits::max()) { michael@0: return OTS_FAILURE_MSG("Bad glyph count in gdef"); michael@0: } michael@0: if (offset_coverage == 0 || offset_coverage >= length || michael@0: offset_coverage < attach_points_end) { michael@0: return OTS_FAILURE_MSG("Bad coverage offset %d", offset_coverage); michael@0: } michael@0: if (glyph_count > num_glyphs) { michael@0: return OTS_FAILURE_MSG("Bad glyph count %u", glyph_count); michael@0: } michael@0: michael@0: std::vector attach_points; michael@0: attach_points.resize(glyph_count); michael@0: for (unsigned i = 0; i < glyph_count; ++i) { michael@0: if (!subtable.ReadU16(&attach_points[i])) { michael@0: return OTS_FAILURE_MSG("Can't read attachment point %d", i); michael@0: } michael@0: if (attach_points[i] >= length || michael@0: attach_points[i] < attach_points_end) { michael@0: return OTS_FAILURE_MSG("Bad attachment point %d of %d", i, attach_points[i]); michael@0: } michael@0: } michael@0: michael@0: // Parse coverage table michael@0: if (!ots::ParseCoverageTable(file, data + offset_coverage, michael@0: length - offset_coverage, num_glyphs)) { michael@0: return OTS_FAILURE_MSG("Bad coverage table"); michael@0: } michael@0: michael@0: // Parse attach point table michael@0: for (unsigned i = 0; i < attach_points.size(); ++i) { michael@0: subtable.set_offset(attach_points[i]); michael@0: uint16_t point_count = 0; michael@0: if (!subtable.ReadU16(&point_count)) { michael@0: return OTS_FAILURE_MSG("Can't read point count %d", i); michael@0: } michael@0: if (point_count == 0) { michael@0: return OTS_FAILURE_MSG("zero point count %d", i); michael@0: } michael@0: uint16_t last_point_index = 0; michael@0: uint16_t point_index = 0; michael@0: for (unsigned j = 0; j < point_count; ++j) { michael@0: if (!subtable.ReadU16(&point_index)) { michael@0: return OTS_FAILURE_MSG("Can't read point index %d in point %d", j, i); michael@0: } michael@0: // Contour point indeces are in increasing numerical order michael@0: if (last_point_index != 0 && last_point_index >= point_index) { michael@0: return OTS_FAILURE_MSG("bad contour indeces: %u >= %u", michael@0: last_point_index, point_index); michael@0: } michael@0: last_point_index = point_index; michael@0: } michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: bool ParseLigCaretListTable(ots::OpenTypeFile *file, const uint8_t *data, michael@0: size_t length, const uint16_t num_glyphs) { michael@0: ots::Buffer subtable(data, length); michael@0: uint16_t offset_coverage = 0; michael@0: uint16_t lig_glyph_count = 0; michael@0: if (!subtable.ReadU16(&offset_coverage) || michael@0: !subtable.ReadU16(&lig_glyph_count)) { michael@0: return OTS_FAILURE_MSG("Can't read caret structure"); michael@0: } michael@0: const unsigned lig_glyphs_end = michael@0: 2 * static_cast(lig_glyph_count) + 4; michael@0: if (lig_glyphs_end > std::numeric_limits::max()) { michael@0: return OTS_FAILURE_MSG("Bad caret structure"); michael@0: } michael@0: if (offset_coverage == 0 || offset_coverage >= length || michael@0: offset_coverage < lig_glyphs_end) { michael@0: return OTS_FAILURE_MSG("Bad caret coverate offset %d", offset_coverage); michael@0: } michael@0: if (lig_glyph_count > num_glyphs) { michael@0: return OTS_FAILURE_MSG("bad ligature glyph count: %u", lig_glyph_count); michael@0: } michael@0: michael@0: std::vector lig_glyphs; michael@0: lig_glyphs.resize(lig_glyph_count); michael@0: for (unsigned i = 0; i < lig_glyph_count; ++i) { michael@0: if (!subtable.ReadU16(&lig_glyphs[i])) { michael@0: return OTS_FAILURE_MSG("Can't read ligature glyph location %d", i); michael@0: } michael@0: if (lig_glyphs[i] >= length || lig_glyphs[i] < lig_glyphs_end) { michael@0: return OTS_FAILURE_MSG("Bad ligature glyph location %d in glyph %d", lig_glyphs[i], i); michael@0: } michael@0: } michael@0: michael@0: // Parse coverage table michael@0: if (!ots::ParseCoverageTable(file, data + offset_coverage, michael@0: length - offset_coverage, num_glyphs)) { michael@0: return OTS_FAILURE_MSG("Can't parse caret coverage table"); michael@0: } michael@0: michael@0: // Parse ligature glyph table michael@0: for (unsigned i = 0; i < lig_glyphs.size(); ++i) { michael@0: subtable.set_offset(lig_glyphs[i]); michael@0: uint16_t caret_count = 0; michael@0: if (!subtable.ReadU16(&caret_count)) { michael@0: return OTS_FAILURE_MSG("Can't read caret count for glyph %d", i); michael@0: } michael@0: if (caret_count == 0) { michael@0: return OTS_FAILURE_MSG("bad caret value count: %u", caret_count); michael@0: } michael@0: michael@0: std::vector caret_value_offsets; michael@0: caret_value_offsets.resize(caret_count); michael@0: unsigned caret_value_offsets_end = 2 * static_cast(caret_count) + 2; michael@0: for (unsigned j = 0; j < caret_count; ++j) { michael@0: if (!subtable.ReadU16(&caret_value_offsets[j])) { michael@0: return OTS_FAILURE_MSG("Can't read caret offset %d for glyph %d", j, i); michael@0: } michael@0: if (caret_value_offsets[j] >= length || caret_value_offsets[j] < caret_value_offsets_end) { michael@0: return OTS_FAILURE_MSG("Bad caret offset %d for caret %d glyph %d", caret_value_offsets[j], j, i); michael@0: } michael@0: } michael@0: michael@0: // Parse caret values table michael@0: for (unsigned j = 0; j < caret_count; ++j) { michael@0: subtable.set_offset(lig_glyphs[i] + caret_value_offsets[j]); michael@0: uint16_t caret_format = 0; michael@0: if (!subtable.ReadU16(&caret_format)) { michael@0: return OTS_FAILURE_MSG("Can't read caret values table %d in glyph %d", j, i); michael@0: } michael@0: // TODO(bashi): We only support caret value format 1 and 2 for now michael@0: // because there are no fonts which contain caret value format 3 michael@0: // as far as we investigated. michael@0: if (caret_format == 0 || caret_format > kMaxCaretValueFormat) { michael@0: return OTS_FAILURE_MSG("bad caret value format: %u", caret_format); michael@0: } michael@0: // CaretValueFormats contain a 2-byte field which could be michael@0: // arbitrary value. michael@0: if (!subtable.Skip(2)) { michael@0: return OTS_FAILURE_MSG("Bad caret value table structure %d in glyph %d", j, i); michael@0: } michael@0: } michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: bool ParseMarkAttachClassDefTable(ots::OpenTypeFile *file, const uint8_t *data, michael@0: size_t length, const uint16_t num_glyphs) { michael@0: return ots::ParseClassDefTable(file, data, length, num_glyphs, kMaxClassDefValue); michael@0: } michael@0: michael@0: bool ParseMarkGlyphSetsDefTable(ots::OpenTypeFile *file, const uint8_t *data, michael@0: size_t length, const uint16_t num_glyphs) { michael@0: ots::Buffer subtable(data, length); michael@0: uint16_t format = 0; michael@0: uint16_t mark_set_count = 0; michael@0: if (!subtable.ReadU16(&format) || michael@0: !subtable.ReadU16(&mark_set_count)) { michael@0: return OTS_FAILURE_MSG("Can' read mark glyph table structure"); michael@0: } michael@0: if (format != 1) { michael@0: return OTS_FAILURE_MSG("bad mark glyph set table format: %u", format); michael@0: } michael@0: michael@0: const unsigned mark_sets_end = 2 * static_cast(mark_set_count) + 4; michael@0: if (mark_sets_end > std::numeric_limits::max()) { michael@0: return OTS_FAILURE_MSG("Bad mark_set %d", mark_sets_end); michael@0: } michael@0: for (unsigned i = 0; i < mark_set_count; ++i) { michael@0: uint32_t offset_coverage = 0; michael@0: if (!subtable.ReadU32(&offset_coverage)) { michael@0: return OTS_FAILURE_MSG("Can't read covrage location for mark set %d", i); michael@0: } michael@0: if (offset_coverage >= length || michael@0: offset_coverage < mark_sets_end) { michael@0: return OTS_FAILURE_MSG("Bad coverage location %d for mark set %d", offset_coverage, i); 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 for mark set %d", i); michael@0: } michael@0: } michael@0: file->gdef->num_mark_glyph_sets = mark_set_count; 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->gdef->data = 0; \ michael@0: file->gdef->length = 0; \ michael@0: OTS_FAILURE_MSG(msg_ ", table discarded"); \ michael@0: } while (0) michael@0: michael@0: namespace ots { michael@0: michael@0: bool ots_gdef_parse(OpenTypeFile *file, const uint8_t *data, size_t length) { michael@0: // Grab the number of glyphs in the file from the maxp table to check michael@0: // GlyphIDs in GDEF table. michael@0: if (!file->maxp) { michael@0: return OTS_FAILURE_MSG("No maxp table in font, needed by GDEF"); michael@0: } michael@0: const uint16_t num_glyphs = file->maxp->num_glyphs; michael@0: michael@0: Buffer table(data, length); michael@0: michael@0: OpenTypeGDEF *gdef = new OpenTypeGDEF; michael@0: file->gdef = gdef; michael@0: michael@0: uint32_t version = 0; michael@0: if (!table.ReadU32(&version)) { michael@0: DROP_THIS_TABLE("Incomplete table"); michael@0: return true; michael@0: } michael@0: if (version < 0x00010000 || version == 0x00010001) { michael@0: DROP_THIS_TABLE("Bad version"); michael@0: return true; michael@0: } michael@0: michael@0: if (version >= 0x00010002) { michael@0: gdef->version_2 = true; michael@0: } michael@0: michael@0: uint16_t offset_glyph_class_def = 0; michael@0: uint16_t offset_attach_list = 0; michael@0: uint16_t offset_lig_caret_list = 0; michael@0: uint16_t offset_mark_attach_class_def = 0; michael@0: if (!table.ReadU16(&offset_glyph_class_def) || michael@0: !table.ReadU16(&offset_attach_list) || michael@0: !table.ReadU16(&offset_lig_caret_list) || michael@0: !table.ReadU16(&offset_mark_attach_class_def)) { michael@0: DROP_THIS_TABLE("Incomplete table"); michael@0: return true; michael@0: } michael@0: uint16_t offset_mark_glyph_sets_def = 0; michael@0: if (gdef->version_2) { michael@0: if (!table.ReadU16(&offset_mark_glyph_sets_def)) { michael@0: DROP_THIS_TABLE("Incomplete table"); michael@0: return true; michael@0: } michael@0: } michael@0: michael@0: unsigned gdef_header_end = 4 + 4 * 2; michael@0: if (gdef->version_2) michael@0: gdef_header_end += 2; michael@0: michael@0: // Parse subtables michael@0: if (offset_glyph_class_def) { michael@0: if (offset_glyph_class_def >= length || michael@0: offset_glyph_class_def < gdef_header_end) { michael@0: DROP_THIS_TABLE("Invalid offset to glyph classes"); michael@0: return true; michael@0: } michael@0: if (!ParseGlyphClassDefTable(file, data + offset_glyph_class_def, michael@0: length - offset_glyph_class_def, michael@0: num_glyphs)) { michael@0: DROP_THIS_TABLE("Invalid glyph classes"); michael@0: return true; michael@0: } michael@0: gdef->has_glyph_class_def = true; michael@0: } michael@0: michael@0: if (offset_attach_list) { michael@0: if (offset_attach_list >= length || michael@0: offset_attach_list < gdef_header_end) { michael@0: DROP_THIS_TABLE("Invalid offset to attachment list"); michael@0: return true; michael@0: } michael@0: if (!ParseAttachListTable(file, data + offset_attach_list, michael@0: length - offset_attach_list, michael@0: num_glyphs)) { michael@0: DROP_THIS_TABLE("Invalid attachment list"); michael@0: return true; michael@0: } michael@0: } michael@0: michael@0: if (offset_lig_caret_list) { michael@0: if (offset_lig_caret_list >= length || michael@0: offset_lig_caret_list < gdef_header_end) { michael@0: DROP_THIS_TABLE("Invalid offset to ligature caret list"); michael@0: return true; michael@0: } michael@0: if (!ParseLigCaretListTable(file, data + offset_lig_caret_list, michael@0: length - offset_lig_caret_list, michael@0: num_glyphs)) { michael@0: DROP_THIS_TABLE("Invalid ligature caret list"); michael@0: return true; michael@0: } michael@0: } michael@0: michael@0: if (offset_mark_attach_class_def) { michael@0: if (offset_mark_attach_class_def >= length || michael@0: offset_mark_attach_class_def < gdef_header_end) { michael@0: return OTS_FAILURE_MSG("Invalid offset to mark attachment list"); michael@0: } michael@0: if (!ParseMarkAttachClassDefTable(file, michael@0: data + offset_mark_attach_class_def, michael@0: length - offset_mark_attach_class_def, michael@0: num_glyphs)) { michael@0: DROP_THIS_TABLE("Invalid mark attachment list"); michael@0: return true; michael@0: } michael@0: gdef->has_mark_attachment_class_def = true; michael@0: } michael@0: michael@0: if (offset_mark_glyph_sets_def) { michael@0: if (offset_mark_glyph_sets_def >= length || michael@0: offset_mark_glyph_sets_def < gdef_header_end) { michael@0: return OTS_FAILURE_MSG("invalid offset to mark glyph sets"); michael@0: } michael@0: if (!ParseMarkGlyphSetsDefTable(file, michael@0: data + offset_mark_glyph_sets_def, michael@0: length - offset_mark_glyph_sets_def, michael@0: num_glyphs)) { michael@0: DROP_THIS_TABLE("Invalid mark glyph sets"); michael@0: return true; michael@0: } michael@0: gdef->has_mark_glyph_sets_def = true; michael@0: } michael@0: gdef->data = data; michael@0: gdef->length = length; michael@0: return true; michael@0: } michael@0: michael@0: bool ots_gdef_should_serialise(OpenTypeFile *file) { michael@0: return file->gdef != NULL && file->gdef->data != NULL; michael@0: } michael@0: michael@0: bool ots_gdef_serialise(OTSStream *out, OpenTypeFile *file) { michael@0: if (!out->Write(file->gdef->data, file->gdef->length)) { michael@0: return OTS_FAILURE_MSG("Failed to write GDEF table"); michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: void ots_gdef_free(OpenTypeFile *file) { michael@0: delete file->gdef; michael@0: } michael@0: michael@0: } // namespace ots michael@0: