gfx/ots/src/gdef.cc

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
michael@0 2 // Use of this source code is governed by a BSD-style license that can be
michael@0 3 // found in the LICENSE file.
michael@0 4
michael@0 5 #include "gdef.h"
michael@0 6
michael@0 7 #include <limits>
michael@0 8 #include <vector>
michael@0 9
michael@0 10 #include "gpos.h"
michael@0 11 #include "gsub.h"
michael@0 12 #include "layout.h"
michael@0 13 #include "maxp.h"
michael@0 14
michael@0 15 // GDEF - The Glyph Definition Table
michael@0 16 // http://www.microsoft.com/typography/otspec/gdef.htm
michael@0 17
michael@0 18 #define TABLE_NAME "GDEF"
michael@0 19
michael@0 20 namespace {
michael@0 21
michael@0 22 // The maximum class value in class definition tables.
michael@0 23 const uint16_t kMaxClassDefValue = 0xFFFF;
michael@0 24 // The maximum class value in the glyph class definision table.
michael@0 25 const uint16_t kMaxGlyphClassDefValue = 4;
michael@0 26 // The maximum format number of caret value tables.
michael@0 27 // We don't support format 3 for now. See the comment in
michael@0 28 // ParseLigCaretListTable() for the reason.
michael@0 29 const uint16_t kMaxCaretValueFormat = 2;
michael@0 30
michael@0 31 bool ParseGlyphClassDefTable(ots::OpenTypeFile *file, const uint8_t *data,
michael@0 32 size_t length, const uint16_t num_glyphs) {
michael@0 33 return ots::ParseClassDefTable(file, data, length, num_glyphs,
michael@0 34 kMaxGlyphClassDefValue);
michael@0 35 }
michael@0 36
michael@0 37 bool ParseAttachListTable(ots::OpenTypeFile *file, const uint8_t *data,
michael@0 38 size_t length, const uint16_t num_glyphs) {
michael@0 39 ots::Buffer subtable(data, length);
michael@0 40
michael@0 41 uint16_t offset_coverage = 0;
michael@0 42 uint16_t glyph_count = 0;
michael@0 43 if (!subtable.ReadU16(&offset_coverage) ||
michael@0 44 !subtable.ReadU16(&glyph_count)) {
michael@0 45 return OTS_FAILURE_MSG("Failed to read gdef header");
michael@0 46 }
michael@0 47 const unsigned attach_points_end =
michael@0 48 2 * static_cast<unsigned>(glyph_count) + 4;
michael@0 49 if (attach_points_end > std::numeric_limits<uint16_t>::max()) {
michael@0 50 return OTS_FAILURE_MSG("Bad glyph count in gdef");
michael@0 51 }
michael@0 52 if (offset_coverage == 0 || offset_coverage >= length ||
michael@0 53 offset_coverage < attach_points_end) {
michael@0 54 return OTS_FAILURE_MSG("Bad coverage offset %d", offset_coverage);
michael@0 55 }
michael@0 56 if (glyph_count > num_glyphs) {
michael@0 57 return OTS_FAILURE_MSG("Bad glyph count %u", glyph_count);
michael@0 58 }
michael@0 59
michael@0 60 std::vector<uint16_t> attach_points;
michael@0 61 attach_points.resize(glyph_count);
michael@0 62 for (unsigned i = 0; i < glyph_count; ++i) {
michael@0 63 if (!subtable.ReadU16(&attach_points[i])) {
michael@0 64 return OTS_FAILURE_MSG("Can't read attachment point %d", i);
michael@0 65 }
michael@0 66 if (attach_points[i] >= length ||
michael@0 67 attach_points[i] < attach_points_end) {
michael@0 68 return OTS_FAILURE_MSG("Bad attachment point %d of %d", i, attach_points[i]);
michael@0 69 }
michael@0 70 }
michael@0 71
michael@0 72 // Parse coverage table
michael@0 73 if (!ots::ParseCoverageTable(file, data + offset_coverage,
michael@0 74 length - offset_coverage, num_glyphs)) {
michael@0 75 return OTS_FAILURE_MSG("Bad coverage table");
michael@0 76 }
michael@0 77
michael@0 78 // Parse attach point table
michael@0 79 for (unsigned i = 0; i < attach_points.size(); ++i) {
michael@0 80 subtable.set_offset(attach_points[i]);
michael@0 81 uint16_t point_count = 0;
michael@0 82 if (!subtable.ReadU16(&point_count)) {
michael@0 83 return OTS_FAILURE_MSG("Can't read point count %d", i);
michael@0 84 }
michael@0 85 if (point_count == 0) {
michael@0 86 return OTS_FAILURE_MSG("zero point count %d", i);
michael@0 87 }
michael@0 88 uint16_t last_point_index = 0;
michael@0 89 uint16_t point_index = 0;
michael@0 90 for (unsigned j = 0; j < point_count; ++j) {
michael@0 91 if (!subtable.ReadU16(&point_index)) {
michael@0 92 return OTS_FAILURE_MSG("Can't read point index %d in point %d", j, i);
michael@0 93 }
michael@0 94 // Contour point indeces are in increasing numerical order
michael@0 95 if (last_point_index != 0 && last_point_index >= point_index) {
michael@0 96 return OTS_FAILURE_MSG("bad contour indeces: %u >= %u",
michael@0 97 last_point_index, point_index);
michael@0 98 }
michael@0 99 last_point_index = point_index;
michael@0 100 }
michael@0 101 }
michael@0 102 return true;
michael@0 103 }
michael@0 104
michael@0 105 bool ParseLigCaretListTable(ots::OpenTypeFile *file, const uint8_t *data,
michael@0 106 size_t length, const uint16_t num_glyphs) {
michael@0 107 ots::Buffer subtable(data, length);
michael@0 108 uint16_t offset_coverage = 0;
michael@0 109 uint16_t lig_glyph_count = 0;
michael@0 110 if (!subtable.ReadU16(&offset_coverage) ||
michael@0 111 !subtable.ReadU16(&lig_glyph_count)) {
michael@0 112 return OTS_FAILURE_MSG("Can't read caret structure");
michael@0 113 }
michael@0 114 const unsigned lig_glyphs_end =
michael@0 115 2 * static_cast<unsigned>(lig_glyph_count) + 4;
michael@0 116 if (lig_glyphs_end > std::numeric_limits<uint16_t>::max()) {
michael@0 117 return OTS_FAILURE_MSG("Bad caret structure");
michael@0 118 }
michael@0 119 if (offset_coverage == 0 || offset_coverage >= length ||
michael@0 120 offset_coverage < lig_glyphs_end) {
michael@0 121 return OTS_FAILURE_MSG("Bad caret coverate offset %d", offset_coverage);
michael@0 122 }
michael@0 123 if (lig_glyph_count > num_glyphs) {
michael@0 124 return OTS_FAILURE_MSG("bad ligature glyph count: %u", lig_glyph_count);
michael@0 125 }
michael@0 126
michael@0 127 std::vector<uint16_t> lig_glyphs;
michael@0 128 lig_glyphs.resize(lig_glyph_count);
michael@0 129 for (unsigned i = 0; i < lig_glyph_count; ++i) {
michael@0 130 if (!subtable.ReadU16(&lig_glyphs[i])) {
michael@0 131 return OTS_FAILURE_MSG("Can't read ligature glyph location %d", i);
michael@0 132 }
michael@0 133 if (lig_glyphs[i] >= length || lig_glyphs[i] < lig_glyphs_end) {
michael@0 134 return OTS_FAILURE_MSG("Bad ligature glyph location %d in glyph %d", lig_glyphs[i], i);
michael@0 135 }
michael@0 136 }
michael@0 137
michael@0 138 // Parse coverage table
michael@0 139 if (!ots::ParseCoverageTable(file, data + offset_coverage,
michael@0 140 length - offset_coverage, num_glyphs)) {
michael@0 141 return OTS_FAILURE_MSG("Can't parse caret coverage table");
michael@0 142 }
michael@0 143
michael@0 144 // Parse ligature glyph table
michael@0 145 for (unsigned i = 0; i < lig_glyphs.size(); ++i) {
michael@0 146 subtable.set_offset(lig_glyphs[i]);
michael@0 147 uint16_t caret_count = 0;
michael@0 148 if (!subtable.ReadU16(&caret_count)) {
michael@0 149 return OTS_FAILURE_MSG("Can't read caret count for glyph %d", i);
michael@0 150 }
michael@0 151 if (caret_count == 0) {
michael@0 152 return OTS_FAILURE_MSG("bad caret value count: %u", caret_count);
michael@0 153 }
michael@0 154
michael@0 155 std::vector<uint16_t> caret_value_offsets;
michael@0 156 caret_value_offsets.resize(caret_count);
michael@0 157 unsigned caret_value_offsets_end = 2 * static_cast<unsigned>(caret_count) + 2;
michael@0 158 for (unsigned j = 0; j < caret_count; ++j) {
michael@0 159 if (!subtable.ReadU16(&caret_value_offsets[j])) {
michael@0 160 return OTS_FAILURE_MSG("Can't read caret offset %d for glyph %d", j, i);
michael@0 161 }
michael@0 162 if (caret_value_offsets[j] >= length || caret_value_offsets[j] < caret_value_offsets_end) {
michael@0 163 return OTS_FAILURE_MSG("Bad caret offset %d for caret %d glyph %d", caret_value_offsets[j], j, i);
michael@0 164 }
michael@0 165 }
michael@0 166
michael@0 167 // Parse caret values table
michael@0 168 for (unsigned j = 0; j < caret_count; ++j) {
michael@0 169 subtable.set_offset(lig_glyphs[i] + caret_value_offsets[j]);
michael@0 170 uint16_t caret_format = 0;
michael@0 171 if (!subtable.ReadU16(&caret_format)) {
michael@0 172 return OTS_FAILURE_MSG("Can't read caret values table %d in glyph %d", j, i);
michael@0 173 }
michael@0 174 // TODO(bashi): We only support caret value format 1 and 2 for now
michael@0 175 // because there are no fonts which contain caret value format 3
michael@0 176 // as far as we investigated.
michael@0 177 if (caret_format == 0 || caret_format > kMaxCaretValueFormat) {
michael@0 178 return OTS_FAILURE_MSG("bad caret value format: %u", caret_format);
michael@0 179 }
michael@0 180 // CaretValueFormats contain a 2-byte field which could be
michael@0 181 // arbitrary value.
michael@0 182 if (!subtable.Skip(2)) {
michael@0 183 return OTS_FAILURE_MSG("Bad caret value table structure %d in glyph %d", j, i);
michael@0 184 }
michael@0 185 }
michael@0 186 }
michael@0 187 return true;
michael@0 188 }
michael@0 189
michael@0 190 bool ParseMarkAttachClassDefTable(ots::OpenTypeFile *file, const uint8_t *data,
michael@0 191 size_t length, const uint16_t num_glyphs) {
michael@0 192 return ots::ParseClassDefTable(file, data, length, num_glyphs, kMaxClassDefValue);
michael@0 193 }
michael@0 194
michael@0 195 bool ParseMarkGlyphSetsDefTable(ots::OpenTypeFile *file, const uint8_t *data,
michael@0 196 size_t length, const uint16_t num_glyphs) {
michael@0 197 ots::Buffer subtable(data, length);
michael@0 198 uint16_t format = 0;
michael@0 199 uint16_t mark_set_count = 0;
michael@0 200 if (!subtable.ReadU16(&format) ||
michael@0 201 !subtable.ReadU16(&mark_set_count)) {
michael@0 202 return OTS_FAILURE_MSG("Can' read mark glyph table structure");
michael@0 203 }
michael@0 204 if (format != 1) {
michael@0 205 return OTS_FAILURE_MSG("bad mark glyph set table format: %u", format);
michael@0 206 }
michael@0 207
michael@0 208 const unsigned mark_sets_end = 2 * static_cast<unsigned>(mark_set_count) + 4;
michael@0 209 if (mark_sets_end > std::numeric_limits<uint16_t>::max()) {
michael@0 210 return OTS_FAILURE_MSG("Bad mark_set %d", mark_sets_end);
michael@0 211 }
michael@0 212 for (unsigned i = 0; i < mark_set_count; ++i) {
michael@0 213 uint32_t offset_coverage = 0;
michael@0 214 if (!subtable.ReadU32(&offset_coverage)) {
michael@0 215 return OTS_FAILURE_MSG("Can't read covrage location for mark set %d", i);
michael@0 216 }
michael@0 217 if (offset_coverage >= length ||
michael@0 218 offset_coverage < mark_sets_end) {
michael@0 219 return OTS_FAILURE_MSG("Bad coverage location %d for mark set %d", offset_coverage, i);
michael@0 220 }
michael@0 221 if (!ots::ParseCoverageTable(file, data + offset_coverage,
michael@0 222 length - offset_coverage, num_glyphs)) {
michael@0 223 return OTS_FAILURE_MSG("Failed to parse coverage table for mark set %d", i);
michael@0 224 }
michael@0 225 }
michael@0 226 file->gdef->num_mark_glyph_sets = mark_set_count;
michael@0 227 return true;
michael@0 228 }
michael@0 229
michael@0 230 } // namespace
michael@0 231
michael@0 232 #define DROP_THIS_TABLE(msg_) \
michael@0 233 do { \
michael@0 234 file->gdef->data = 0; \
michael@0 235 file->gdef->length = 0; \
michael@0 236 OTS_FAILURE_MSG(msg_ ", table discarded"); \
michael@0 237 } while (0)
michael@0 238
michael@0 239 namespace ots {
michael@0 240
michael@0 241 bool ots_gdef_parse(OpenTypeFile *file, const uint8_t *data, size_t length) {
michael@0 242 // Grab the number of glyphs in the file from the maxp table to check
michael@0 243 // GlyphIDs in GDEF table.
michael@0 244 if (!file->maxp) {
michael@0 245 return OTS_FAILURE_MSG("No maxp table in font, needed by GDEF");
michael@0 246 }
michael@0 247 const uint16_t num_glyphs = file->maxp->num_glyphs;
michael@0 248
michael@0 249 Buffer table(data, length);
michael@0 250
michael@0 251 OpenTypeGDEF *gdef = new OpenTypeGDEF;
michael@0 252 file->gdef = gdef;
michael@0 253
michael@0 254 uint32_t version = 0;
michael@0 255 if (!table.ReadU32(&version)) {
michael@0 256 DROP_THIS_TABLE("Incomplete table");
michael@0 257 return true;
michael@0 258 }
michael@0 259 if (version < 0x00010000 || version == 0x00010001) {
michael@0 260 DROP_THIS_TABLE("Bad version");
michael@0 261 return true;
michael@0 262 }
michael@0 263
michael@0 264 if (version >= 0x00010002) {
michael@0 265 gdef->version_2 = true;
michael@0 266 }
michael@0 267
michael@0 268 uint16_t offset_glyph_class_def = 0;
michael@0 269 uint16_t offset_attach_list = 0;
michael@0 270 uint16_t offset_lig_caret_list = 0;
michael@0 271 uint16_t offset_mark_attach_class_def = 0;
michael@0 272 if (!table.ReadU16(&offset_glyph_class_def) ||
michael@0 273 !table.ReadU16(&offset_attach_list) ||
michael@0 274 !table.ReadU16(&offset_lig_caret_list) ||
michael@0 275 !table.ReadU16(&offset_mark_attach_class_def)) {
michael@0 276 DROP_THIS_TABLE("Incomplete table");
michael@0 277 return true;
michael@0 278 }
michael@0 279 uint16_t offset_mark_glyph_sets_def = 0;
michael@0 280 if (gdef->version_2) {
michael@0 281 if (!table.ReadU16(&offset_mark_glyph_sets_def)) {
michael@0 282 DROP_THIS_TABLE("Incomplete table");
michael@0 283 return true;
michael@0 284 }
michael@0 285 }
michael@0 286
michael@0 287 unsigned gdef_header_end = 4 + 4 * 2;
michael@0 288 if (gdef->version_2)
michael@0 289 gdef_header_end += 2;
michael@0 290
michael@0 291 // Parse subtables
michael@0 292 if (offset_glyph_class_def) {
michael@0 293 if (offset_glyph_class_def >= length ||
michael@0 294 offset_glyph_class_def < gdef_header_end) {
michael@0 295 DROP_THIS_TABLE("Invalid offset to glyph classes");
michael@0 296 return true;
michael@0 297 }
michael@0 298 if (!ParseGlyphClassDefTable(file, data + offset_glyph_class_def,
michael@0 299 length - offset_glyph_class_def,
michael@0 300 num_glyphs)) {
michael@0 301 DROP_THIS_TABLE("Invalid glyph classes");
michael@0 302 return true;
michael@0 303 }
michael@0 304 gdef->has_glyph_class_def = true;
michael@0 305 }
michael@0 306
michael@0 307 if (offset_attach_list) {
michael@0 308 if (offset_attach_list >= length ||
michael@0 309 offset_attach_list < gdef_header_end) {
michael@0 310 DROP_THIS_TABLE("Invalid offset to attachment list");
michael@0 311 return true;
michael@0 312 }
michael@0 313 if (!ParseAttachListTable(file, data + offset_attach_list,
michael@0 314 length - offset_attach_list,
michael@0 315 num_glyphs)) {
michael@0 316 DROP_THIS_TABLE("Invalid attachment list");
michael@0 317 return true;
michael@0 318 }
michael@0 319 }
michael@0 320
michael@0 321 if (offset_lig_caret_list) {
michael@0 322 if (offset_lig_caret_list >= length ||
michael@0 323 offset_lig_caret_list < gdef_header_end) {
michael@0 324 DROP_THIS_TABLE("Invalid offset to ligature caret list");
michael@0 325 return true;
michael@0 326 }
michael@0 327 if (!ParseLigCaretListTable(file, data + offset_lig_caret_list,
michael@0 328 length - offset_lig_caret_list,
michael@0 329 num_glyphs)) {
michael@0 330 DROP_THIS_TABLE("Invalid ligature caret list");
michael@0 331 return true;
michael@0 332 }
michael@0 333 }
michael@0 334
michael@0 335 if (offset_mark_attach_class_def) {
michael@0 336 if (offset_mark_attach_class_def >= length ||
michael@0 337 offset_mark_attach_class_def < gdef_header_end) {
michael@0 338 return OTS_FAILURE_MSG("Invalid offset to mark attachment list");
michael@0 339 }
michael@0 340 if (!ParseMarkAttachClassDefTable(file,
michael@0 341 data + offset_mark_attach_class_def,
michael@0 342 length - offset_mark_attach_class_def,
michael@0 343 num_glyphs)) {
michael@0 344 DROP_THIS_TABLE("Invalid mark attachment list");
michael@0 345 return true;
michael@0 346 }
michael@0 347 gdef->has_mark_attachment_class_def = true;
michael@0 348 }
michael@0 349
michael@0 350 if (offset_mark_glyph_sets_def) {
michael@0 351 if (offset_mark_glyph_sets_def >= length ||
michael@0 352 offset_mark_glyph_sets_def < gdef_header_end) {
michael@0 353 return OTS_FAILURE_MSG("invalid offset to mark glyph sets");
michael@0 354 }
michael@0 355 if (!ParseMarkGlyphSetsDefTable(file,
michael@0 356 data + offset_mark_glyph_sets_def,
michael@0 357 length - offset_mark_glyph_sets_def,
michael@0 358 num_glyphs)) {
michael@0 359 DROP_THIS_TABLE("Invalid mark glyph sets");
michael@0 360 return true;
michael@0 361 }
michael@0 362 gdef->has_mark_glyph_sets_def = true;
michael@0 363 }
michael@0 364 gdef->data = data;
michael@0 365 gdef->length = length;
michael@0 366 return true;
michael@0 367 }
michael@0 368
michael@0 369 bool ots_gdef_should_serialise(OpenTypeFile *file) {
michael@0 370 return file->gdef != NULL && file->gdef->data != NULL;
michael@0 371 }
michael@0 372
michael@0 373 bool ots_gdef_serialise(OTSStream *out, OpenTypeFile *file) {
michael@0 374 if (!out->Write(file->gdef->data, file->gdef->length)) {
michael@0 375 return OTS_FAILURE_MSG("Failed to write GDEF table");
michael@0 376 }
michael@0 377
michael@0 378 return true;
michael@0 379 }
michael@0 380
michael@0 381 void ots_gdef_free(OpenTypeFile *file) {
michael@0 382 delete file->gdef;
michael@0 383 }
michael@0 384
michael@0 385 } // namespace ots
michael@0 386

mercurial