Add GDEF support.
Adds src/gdef.{h,cc} for GDEF table support.
This CL also adds src/layout.{h,cc} that implement utility functions for OpenType layout common table formats.
BUG=27131
TEST=ran test_{,un}malicious_fonts.sh for 4500+ OpenType fonts.
git-svn-id: http://ots.googlecode.com/svn/trunk@49 a4e77c2c-9104-11de-800e-5b313e0d2bf3
diff --git a/ots.gyp b/ots.gyp
index d6004d6..e7c89cc 100644
--- a/ots.gyp
+++ b/ots.gyp
@@ -26,6 +26,8 @@
'src/fpgm.h',
'src/gasp.cc',
'src/gasp.h',
+ 'src/gdef.cc',
+ 'src/gdef.h',
'src/glyf.cc',
'src/glyf.h',
'src/hdmx.cc',
@@ -38,6 +40,8 @@
'src/hmtx.h',
'src/kern.cc',
'src/kern.h',
+ 'src/layout.cc',
+ 'src/layout.h',
'src/loca.cc',
'src/loca.h',
'src/ltsh.cc',
diff --git a/src/gdef.cc b/src/gdef.cc
new file mode 100644
index 0000000..6be74c2
--- /dev/null
+++ b/src/gdef.cc
@@ -0,0 +1,369 @@
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "gdef.h"
+
+#include <vector>
+
+#include "layout.h"
+#include "maxp.h"
+
+// GDEF - The Glyph Definition Table
+// http://www.microsoft.com/typography/otspec/gdef.htm
+
+namespace {
+
+// The maximum class value in class definition tables.
+const uint16_t kMaxClassDefValue = 0xFFFF;
+// The maximum class value in the glyph class definision table.
+const uint16_t kMaxGlyphClassDefValue = 4;
+// The maximum format number of caret value tables.
+// We don't support format 3 for now. See the comment in
+// ParseLigCaretListTable() for the reason.
+const uint16_t kMaxCaretValueFormat = 2;
+
+bool ParseGlyphClassDefTable(ots::OpenTypeFile *file, const uint8_t *data,
+ size_t length, const uint16_t num_glyphs) {
+ return ots::ParseClassDefTable(data, length, num_glyphs,
+ kMaxGlyphClassDefValue);
+}
+
+bool ParseAttachListTable(ots::OpenTypeFile *file, const uint8_t *data,
+ size_t length, const uint16_t num_glyphs) {
+ ots::Buffer subtable(data, length);
+
+ uint16_t offset_coverage = 0;
+ uint16_t glyph_count = 0;
+ if (!subtable.ReadU16(&offset_coverage) ||
+ !subtable.ReadU16(&glyph_count)) {
+ return OTS_FAILURE();
+ }
+ const unsigned attach_points_end = static_cast<unsigned>(4) + 2*glyph_count;
+ if (offset_coverage == 0 || offset_coverage >= length ||
+ offset_coverage < attach_points_end) {
+ return OTS_FAILURE();
+ }
+ if (glyph_count > num_glyphs) {
+ OTS_WARNING("bad glyph count: %u", glyph_count);
+ return OTS_FAILURE();
+ }
+
+ std::vector<uint16_t> attach_points;
+ attach_points.resize(glyph_count);
+ for (unsigned i = 0; i < glyph_count; ++i) {
+ if (!subtable.ReadU16(&attach_points[i])) {
+ return OTS_FAILURE();
+ }
+ if (attach_points[i] >= length ||
+ attach_points[i] < attach_points_end) {
+ return OTS_FAILURE();
+ }
+ }
+
+ // Parse coverage table
+ if (!ots::ParseCoverageTable(data + offset_coverage,
+ length - offset_coverage, num_glyphs)) {
+ return OTS_FAILURE();
+ }
+
+ // Parse attach point table
+ for (unsigned i = 0; i < attach_points.size(); ++i) {
+ subtable.set_offset(attach_points[i]);
+ uint16_t point_count = 0;
+ if (!subtable.ReadU16(&point_count)) {
+ return OTS_FAILURE();
+ }
+ if (point_count == 0) {
+ return OTS_FAILURE();
+ }
+ uint16_t last_point_index = 0;
+ uint16_t point_index = 0;
+ for (unsigned j = 0; j < point_count; ++j) {
+ if (!subtable.ReadU16(&point_index)) {
+ return OTS_FAILURE();
+ }
+ // Contour point indeces are in increasing numerical order
+ if (last_point_index != 0 && last_point_index >= point_index) {
+ OTS_WARNING("bad contour indeces: %u >= %u",
+ last_point_index, point_index);
+ return OTS_FAILURE();
+ }
+ last_point_index = point_index;
+ }
+ }
+ return true;
+}
+
+bool ParseLigCaretListTable(ots::OpenTypeFile *file, const uint8_t *data,
+ size_t length, const uint16_t num_glyphs) {
+ ots::Buffer subtable(data, length);
+ uint16_t offset_coverage = 0;
+ uint16_t lig_glyph_count = 0;
+ if (!subtable.ReadU16(&offset_coverage) ||
+ !subtable.ReadU16(&lig_glyph_count)) {
+ return OTS_FAILURE();
+ }
+ const unsigned lig_glyphs_end = static_cast<unsigned>(4) + 2*lig_glyph_count;
+ if (offset_coverage == 0 || offset_coverage >= length ||
+ offset_coverage < lig_glyphs_end) {
+ return OTS_FAILURE();
+ }
+ if (lig_glyph_count > num_glyphs) {
+ OTS_WARNING("bad ligature glyph count: %u", lig_glyph_count);
+ return OTS_FAILURE();
+ }
+
+ std::vector<uint16_t> lig_glyphs;
+ lig_glyphs.resize(lig_glyph_count);
+ for (unsigned i = 0; i < lig_glyph_count; ++i) {
+ if (!subtable.ReadU16(&lig_glyphs[i])) {
+ return OTS_FAILURE();
+ }
+ if (lig_glyphs[i] >= length || lig_glyphs[i] < lig_glyphs_end) {
+ return OTS_FAILURE();
+ }
+ }
+
+ // Parse coverage table
+ if (!ots::ParseCoverageTable(data + offset_coverage,
+ length - offset_coverage, num_glyphs)) {
+ return OTS_FAILURE();
+ }
+
+ // Parse ligature glyph table
+ for (unsigned i = 0; i < lig_glyphs.size(); ++i) {
+ subtable.set_offset(lig_glyphs[i]);
+ uint16_t caret_count = 0;
+ if (!subtable.ReadU16(&caret_count)) {
+ return OTS_FAILURE();
+ }
+ if (caret_count == 0) {
+ OTS_WARNING("bad caret value count: %u", caret_count);
+ return OTS_FAILURE();
+ }
+
+ std::vector<uint16_t> caret_values;
+ caret_values.resize(caret_count);
+ uint16_t last_offset_caret = 0;
+ unsigned caret_values_end = static_cast<unsigned>(2) * 2*caret_count;
+ for (unsigned j = 0; j < caret_count; ++j) {
+ if (!subtable.ReadU16(&caret_values[j])) {
+ return OTS_FAILURE();
+ }
+ if (caret_values[j] >= length || caret_values[j] < caret_values_end) {
+ return OTS_FAILURE();
+ }
+ // Caret offsets are in increasing coordinate order
+ if (last_offset_caret != 0 && last_offset_caret >= caret_values[j]) {
+ OTS_WARNING("offset isn't in increasing coordinate order: %u >= %u",
+ last_offset_caret, caret_values[j]);
+ return OTS_FAILURE();
+ }
+ last_offset_caret = caret_values[j];
+ }
+
+ // Parse caret values table
+ for (unsigned j = 0; j < caret_count; ++j) {
+ subtable.set_offset(lig_glyphs[i] + caret_values[j]);
+ uint16_t caret_format = 0;
+ if (!subtable.ReadU16(&caret_format)) {
+ return OTS_FAILURE();
+ }
+ // TODO(bashi): We only support caret value format 1 and 2 for now
+ // because there are no fonts which contain caret value format 3
+ // as far as we investigated.
+ if (caret_format == 0 || caret_format > kMaxCaretValueFormat) {
+ OTS_WARNING("bad caret value format: %u", caret_format);
+ return OTS_FAILURE();
+ }
+ // CaretValueFormats contain a 2-byte field which could be
+ // arbitrary value.
+ if (!subtable.Skip(2)) {
+ return OTS_FAILURE();
+ }
+ }
+ }
+ return true;
+}
+
+bool ParseMarkAttachClassDefTable(ots::OpenTypeFile *file, const uint8_t *data,
+ size_t length, const uint16_t num_glyphs) {
+ return ots::ParseClassDefTable(data, length, num_glyphs, kMaxClassDefValue);
+}
+
+bool ParseMarkGlyphSetsDefTable(ots::OpenTypeFile *file, const uint8_t *data,
+ size_t length, const uint16_t num_glyphs) {
+ ots::Buffer subtable(data, length);
+ uint16_t format = 0;
+ uint16_t mark_set_count = 0;
+ if (!subtable.ReadU16(&format) ||
+ !subtable.ReadU16(&mark_set_count)) {
+ return OTS_FAILURE();
+ }
+ if (format != 1) {
+ OTS_WARNING("bad mark glyph set table format: %u", format);
+ return OTS_FAILURE();
+ }
+
+ const unsigned mark_sets_end = static_cast<unsigned>(4) + 2*mark_set_count;
+ for (unsigned i = 0; i < mark_set_count; ++i) {
+ uint32_t offset_coverage = 0;
+ if (!subtable.ReadU32(&offset_coverage)) {
+ return OTS_FAILURE();
+ }
+ if (offset_coverage >= length ||
+ offset_coverage < mark_sets_end) {
+ return OTS_FAILURE();
+ }
+ if (!ots::ParseCoverageTable(data + offset_coverage,
+ length - offset_coverage, num_glyphs)) {
+ return OTS_FAILURE();
+ }
+ }
+ return true;
+}
+
+} // namespace
+
+#define DROP_THIS_TABLE \
+ do { delete file->gdef; file->gdef = 0; } while (0)
+
+namespace ots {
+
+bool ots_gdef_parse(OpenTypeFile *file, const uint8_t *data, size_t length) {
+ // Grab the number of glyphs in the file from the maxp table to check
+ // GlyphIDs in GDEF table.
+ if (!file->maxp) {
+ return OTS_FAILURE();
+ }
+ const uint16_t num_glyphs = file->maxp->num_glyphs;
+
+ Buffer table(data, length);
+
+ OpenTypeGDEF *gdef = new OpenTypeGDEF;
+ file->gdef = gdef;
+
+ uint32_t version = 0;
+ if (!table.ReadU32(&version)) {
+ return OTS_FAILURE();
+ }
+ if (version < 0x00010000 || version == 0x00010001) {
+ OTS_WARNING("bad GDEF version");
+ DROP_THIS_TABLE;
+ return true;
+ }
+
+ if (version >= 0x00010002) {
+ gdef->version_2 = true;
+ }
+
+ uint16_t offset_glyph_class_def = 0;
+ uint16_t offset_attach_list = 0;
+ uint16_t offset_lig_caret_list = 0;
+ uint16_t offset_mark_attach_class_def = 0;
+ if (!table.ReadU16(&offset_glyph_class_def) ||
+ !table.ReadU16(&offset_attach_list) ||
+ !table.ReadU16(&offset_lig_caret_list) ||
+ !table.ReadU16(&offset_mark_attach_class_def)) {
+ return OTS_FAILURE();
+ }
+ uint16_t offset_mark_glyph_sets_def = 0;
+ if (gdef->version_2) {
+ if (!table.ReadU16(&offset_mark_glyph_sets_def)) {
+ return OTS_FAILURE();
+ }
+ }
+
+ const unsigned gdef_header_end = static_cast<unsigned>(8) +
+ gdef->version_2 ? static_cast<unsigned>(2) : static_cast<unsigned>(0);
+ // Parse subtables
+ if (offset_glyph_class_def) {
+ if (offset_glyph_class_def >= length ||
+ offset_glyph_class_def < gdef_header_end) {
+ return OTS_FAILURE();
+ }
+ if (!ParseGlyphClassDefTable(file, data + offset_glyph_class_def,
+ length - offset_glyph_class_def,
+ num_glyphs)) {
+ DROP_THIS_TABLE;
+ return true;
+ }
+ }
+
+ if (offset_attach_list) {
+ if (offset_attach_list >= length ||
+ offset_attach_list < gdef_header_end) {
+ return OTS_FAILURE();
+ }
+ if (!ParseAttachListTable(file, data + offset_attach_list,
+ length - offset_attach_list,
+ num_glyphs)) {
+ DROP_THIS_TABLE;
+ return true;
+ }
+ }
+
+ if (offset_lig_caret_list) {
+ if (offset_lig_caret_list >= length ||
+ offset_lig_caret_list < gdef_header_end) {
+ return OTS_FAILURE();
+ }
+ if (!ParseLigCaretListTable(file, data + offset_lig_caret_list,
+ length - offset_lig_caret_list,
+ num_glyphs)) {
+ DROP_THIS_TABLE;
+ return true;
+ }
+ }
+
+ if (offset_mark_attach_class_def) {
+ if (offset_mark_attach_class_def >= length ||
+ offset_mark_attach_class_def < gdef_header_end) {
+ return OTS_FAILURE();
+ }
+ if (!ParseMarkAttachClassDefTable(file,
+ data + offset_mark_attach_class_def,
+ length - offset_mark_attach_class_def,
+ num_glyphs)) {
+ DROP_THIS_TABLE;
+ return true;
+ }
+ }
+
+ if (offset_mark_glyph_sets_def) {
+ if (offset_mark_glyph_sets_def >= length ||
+ offset_mark_glyph_sets_def < gdef_header_end) {
+ return OTS_FAILURE();
+ }
+ if (!ParseMarkGlyphSetsDefTable(file,
+ data + offset_mark_glyph_sets_def,
+ length - offset_mark_glyph_sets_def,
+ num_glyphs)) {
+ DROP_THIS_TABLE;
+ return true;
+ }
+ }
+ gdef->data = data;
+ gdef->length = length;
+ return true;
+}
+
+bool ots_gdef_should_serialise(OpenTypeFile *file) {
+ return file->gdef;
+}
+
+bool ots_gdef_serialise(OTSStream *out, OpenTypeFile *file) {
+ if (!out->Write(file->gdef->data, file->gdef->length)) {
+ return OTS_FAILURE();
+ }
+
+ return true;
+}
+
+void ots_gdef_free(OpenTypeFile *file) {
+ delete file->gdef;
+}
+
+} // namespace ots
+
diff --git a/src/gdef.h b/src/gdef.h
new file mode 100644
index 0000000..5ee06d7
--- /dev/null
+++ b/src/gdef.h
@@ -0,0 +1,27 @@
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef OTS_GDEF_H_
+#define OTS_GDEF_H_
+
+#include "ots.h"
+
+namespace ots {
+
+struct OpenTypeGDEF {
+ OpenTypeGDEF()
+ : version_2(false),
+ data(NULL),
+ length(0) {
+ }
+
+ bool version_2;
+ const uint8_t *data;
+ size_t length;
+};
+
+} // namespace ots
+
+#endif
+
diff --git a/src/layout.cc b/src/layout.cc
new file mode 100644
index 0000000..0391419
--- /dev/null
+++ b/src/layout.cc
@@ -0,0 +1,202 @@
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "layout.h"
+
+// OpenType Layout Common Table Formats
+// http://www.microsoft.com/typography/otspec/chapter2.htm
+
+namespace {
+
+bool ParseClassDefFormat1(const uint8_t *data, size_t length,
+ const uint16_t num_glyphs,
+ const uint16_t num_classes) {
+ ots::Buffer subtable(data, length);
+
+ // Skip format field.
+ if (!subtable.Skip(2)) {
+ return OTS_FAILURE();
+ }
+
+ uint16_t start_glyph = 0;
+ if (!subtable.ReadU16(&start_glyph)) {
+ return OTS_FAILURE();
+ }
+ if (start_glyph > num_glyphs) {
+ OTS_WARNING("bad start glyph ID: %u", start_glyph);
+ return OTS_FAILURE();
+ }
+
+ uint16_t glyph_count = 0;
+ if (!subtable.ReadU16(&glyph_count)) {
+ return OTS_FAILURE();
+ }
+ if (glyph_count > num_glyphs) {
+ OTS_WARNING("bad glyph count: %u", glyph_count);
+ return OTS_FAILURE();
+ }
+ for (unsigned i = 0; i < glyph_count; ++i) {
+ uint16_t class_value = 0;
+ if (!subtable.ReadU16(&class_value)) {
+ return OTS_FAILURE();
+ }
+ if (class_value == 0 || class_value > num_classes) {
+ OTS_WARNING("bad class value: %u", class_value);
+ return OTS_FAILURE();
+ }
+ }
+
+ return true;
+}
+
+bool ParseClassDefFormat2(const uint8_t *data, size_t length,
+ const uint16_t num_glyphs,
+ const uint16_t num_classes) {
+ ots::Buffer subtable(data, length);
+
+ // Skip format field.
+ if (!subtable.Skip(2)) {
+ return OTS_FAILURE();
+ }
+
+ uint16_t range_count = 0;
+ if (!subtable.ReadU16(&range_count)) {
+ return OTS_FAILURE();
+ }
+ if (range_count > num_glyphs) {
+ OTS_WARNING("bad range count: %u", range_count);
+ return OTS_FAILURE();
+ }
+
+ uint16_t last_end = 0;
+ for (unsigned i = 0; i < range_count; ++i) {
+ uint16_t start = 0;
+ uint16_t end = 0;
+ uint16_t class_value = 0;
+ if (!subtable.ReadU16(&start) ||
+ !subtable.ReadU16(&end) ||
+ !subtable.ReadU16(&class_value)) {
+ return OTS_FAILURE();
+ }
+ if (start > end || (last_end && start <= last_end)) {
+ OTS_WARNING("glyph range is overlapping.");
+ return OTS_FAILURE();
+ }
+ if (class_value == 0 || class_value > num_classes) {
+ OTS_WARNING("bad class value: %u", class_value);
+ return OTS_FAILURE();
+ }
+ }
+
+ return true;
+}
+
+bool ParseCoverageFormat1(const uint8_t *data, size_t length,
+ const uint16_t num_glyphs) {
+ ots::Buffer subtable(data, length);
+
+ // Skip format field.
+ if (!subtable.Skip(2)) {
+ return OTS_FAILURE();
+ }
+
+ uint16_t glyph_count = 0;
+ if (!subtable.ReadU16(&glyph_count)) {
+ return OTS_FAILURE();
+ }
+ if (glyph_count > num_glyphs) {
+ OTS_WARNING("bad glyph count: %u", glyph_count);
+ return OTS_FAILURE();
+ }
+ for (unsigned i = 0; i < glyph_count; ++i) {
+ uint16_t glyph = 0;
+ if (!subtable.ReadU16(&glyph)) {
+ return OTS_FAILURE();
+ }
+ if (glyph > num_glyphs) {
+ OTS_WARNING("bad glyph ID: %u", glyph);
+ return OTS_FAILURE();
+ }
+ }
+
+ return true;
+}
+
+bool ParseCoverageFormat2(const uint8_t *data, size_t length,
+ const uint16_t num_glyphs) {
+ ots::Buffer subtable(data, length);
+
+ // Skip format field.
+ if (!subtable.Skip(2)) {
+ return OTS_FAILURE();
+ }
+
+ uint16_t range_count = 0;
+ if (!subtable.ReadU16(&range_count)) {
+ return OTS_FAILURE();
+ }
+ if (range_count >= num_glyphs) {
+ OTS_WARNING("bad range count: %u", range_count);
+ return OTS_FAILURE();
+ }
+ uint16_t last_end = 0;
+ for (unsigned i = 0; i < range_count; ++i) {
+ uint16_t start = 0;
+ uint16_t end = 0;
+ uint16_t start_coverage_index = 0;
+ if (!subtable.ReadU16(&start) ||
+ !subtable.ReadU16(&end) ||
+ !subtable.ReadU16(&start_coverage_index)) {
+ return OTS_FAILURE();
+ }
+ if (start > end || (last_end && start <= last_end)) {
+ OTS_WARNING("glyph range is overlapping.");
+ return OTS_FAILURE();
+ }
+ }
+
+ return true;
+}
+
+} // namespace
+
+namespace ots {
+
+bool ParseClassDefTable(const uint8_t *data, size_t length,
+ const uint16_t num_glyphs,
+ const uint16_t num_classes) {
+ Buffer subtable(data, length);
+
+ uint16_t format = 0;
+ if (!subtable.ReadU16(&format)) {
+ return OTS_FAILURE();
+ }
+ if (format == 1) {
+ return ParseClassDefFormat1(data, length, num_glyphs, num_classes);
+ } else if (format == 2) {
+ return ParseClassDefFormat2(data, length, num_glyphs, num_classes);
+ }
+
+ return OTS_FAILURE();
+}
+
+bool ParseCoverageTable(const uint8_t *data, size_t length,
+ const uint16_t num_glyphs) {
+ Buffer subtable(data, length);
+
+ uint16_t format = 0;
+ if (!subtable.ReadU16(&format)) {
+ return OTS_FAILURE();
+ }
+ if (format == 1) {
+ return ParseCoverageFormat1(data, length, num_glyphs);
+ } else if (format == 2) {
+ return ParseCoverageFormat2(data, length, num_glyphs);
+ }
+
+ return OTS_FAILURE();
+}
+
+} // namespace ots
+
diff --git a/src/layout.h b/src/layout.h
new file mode 100644
index 0000000..a658cd4
--- /dev/null
+++ b/src/layout.h
@@ -0,0 +1,25 @@
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef OTS_LAYOUT_H_
+#define OTS_LAYOUT_H_
+
+#include "ots.h"
+
+// Utility functions for OpenType layout common table formats.
+// http://www.microsoft.com/typography/otspec/chapter2.htm
+
+namespace ots {
+
+bool ParseClassDefTable(const uint8_t *data, size_t length,
+ const uint16_t num_glyphs,
+ const uint16_t num_classes);
+
+bool ParseCoverageTable(const uint8_t *data, size_t length,
+ const uint16_t num_glyphs);
+
+} // namespace ots
+
+#endif // OTS_LAYOUT_H_
+
diff --git a/src/ots.cc b/src/ots.cc
index 9a16463..df8e342 100644
--- a/src/ots.cc
+++ b/src/ots.cc
@@ -142,7 +142,9 @@
ots::ots_vorg_should_serialise, ots::ots_vorg_free, false },
{ Tag("kern"), ots::ots_kern_parse, ots::ots_kern_serialise,
ots::ots_kern_should_serialise, ots::ots_kern_free, false },
- // TODO(yusukes): Support GDEF, GPOS, GSUB, mort, base, and jstf tables.
+ { Tag("GDEF"), ots::ots_gdef_parse, ots::ots_gdef_serialise,
+ ots::ots_gdef_should_serialise, ots::ots_gdef_free, false },
+ // TODO(bashi): Support GPOS, GSUB, mort, base, and jstf tables.
{ 0, NULL, NULL, NULL, NULL, false },
};
diff --git a/src/ots.h b/src/ots.h
index 873d463..1601d2b 100644
--- a/src/ots.h
+++ b/src/ots.h
@@ -162,6 +162,7 @@
F(cvt, CVT) \
F(fpgm, FPGM) \
F(gasp, GASP) \
+ F(gdef, GDEF) \
F(glyf, GLYF) \
F(hdmx, HDMX) \
F(head, HEAD) \
diff --git a/test/SConstruct b/test/SConstruct
index c45f43f..131f81b 100644
--- a/test/SConstruct
+++ b/test/SConstruct
@@ -18,12 +18,14 @@
'../src/cvt.cc',
'../src/fpgm.cc',
'../src/gasp.cc',
+ '../src/gdef.cc',
'../src/glyf.cc',
'../src/hdmx.cc',
'../src/head.cc',
'../src/hhea.cc',
'../src/hmtx.cc',
'../src/kern.cc',
+ '../src/layout.cc',
'../src/loca.cc',
'../src/ltsh.cc',
'../src/maxp.cc',