michael@0: // Copyright (c) 2009 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 "cmap.h" michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #include "maxp.h" michael@0: #include "os2.h" michael@0: michael@0: // cmap - Character To Glyph Index Mapping Table michael@0: // http://www.microsoft.com/typography/otspec/cmap.htm michael@0: michael@0: #define TABLE_NAME "cmap" michael@0: michael@0: namespace { michael@0: michael@0: struct CMAPSubtableHeader { michael@0: uint16_t platform; michael@0: uint16_t encoding; michael@0: uint32_t offset; michael@0: uint16_t format; michael@0: uint32_t length; michael@0: uint32_t language; michael@0: }; michael@0: michael@0: struct Subtable314Range { michael@0: uint16_t start_range; michael@0: uint16_t end_range; michael@0: int16_t id_delta; michael@0: uint16_t id_range_offset; michael@0: uint32_t id_range_offset_offset; michael@0: }; michael@0: michael@0: // The maximum number of groups in format 12, 13 or 14 subtables. michael@0: // Note: 0xFFFF is the maximum number of glyphs in a single font file. michael@0: const unsigned kMaxCMAPGroups = 0xFFFF; michael@0: michael@0: // Glyph array size for the Mac Roman (format 0) table. michael@0: const size_t kFormat0ArraySize = 256; michael@0: michael@0: // The upper limit of the Unicode code point. michael@0: const uint32_t kUnicodeUpperLimit = 0x10FFFF; michael@0: michael@0: // The maximum number of UVS records (See below). michael@0: const uint32_t kMaxCMAPSelectorRecords = 259; michael@0: // The range of UVSes are: michael@0: // 0x180B-0x180D (3 code points) michael@0: // 0xFE00-0xFE0F (16 code points) michael@0: // 0xE0100-0xE01EF (240 code points) michael@0: const uint32_t kMongolianVSStart = 0x180B; michael@0: const uint32_t kMongolianVSEnd = 0x180D; michael@0: const uint32_t kVSStart = 0xFE00; michael@0: const uint32_t kVSEnd = 0xFE0F; michael@0: const uint32_t kIVSStart = 0xE0100; michael@0: const uint32_t kIVSEnd = 0xE01EF; michael@0: const uint32_t kUVSUpperLimit = 0xFFFFFF; michael@0: michael@0: // Parses Format 4 tables michael@0: bool ParseFormat4(ots::OpenTypeFile *file, int platform, int encoding, michael@0: const uint8_t *data, size_t length, uint16_t num_glyphs) { michael@0: ots::Buffer subtable(data, length); michael@0: michael@0: // 0.3.4, 3.0.4 or 3.1.4 subtables are complex and, rather than expanding the michael@0: // whole thing and recompacting it, we validate it and include it verbatim michael@0: // in the output. michael@0: michael@0: if (!file->os2) { michael@0: return OTS_FAILURE_MSG("Required OS/2 table missing"); michael@0: } michael@0: michael@0: if (!subtable.Skip(4)) { michael@0: return OTS_FAILURE_MSG("Can't read 4 bytes at start of cmap format 4 subtable"); michael@0: } michael@0: uint16_t language = 0; michael@0: if (!subtable.ReadU16(&language)) { michael@0: return OTS_FAILURE_MSG("Can't read language"); michael@0: } michael@0: if (language) { michael@0: // Platform ID 3 (windows) subtables should have language '0'. michael@0: return OTS_FAILURE_MSG("Languages should be 0 (%d)", language); michael@0: } michael@0: michael@0: uint16_t segcountx2, search_range, entry_selector, range_shift; michael@0: segcountx2 = search_range = entry_selector = range_shift = 0; michael@0: if (!subtable.ReadU16(&segcountx2) || michael@0: !subtable.ReadU16(&search_range) || michael@0: !subtable.ReadU16(&entry_selector) || michael@0: !subtable.ReadU16(&range_shift)) { michael@0: return OTS_FAILURE_MSG("Failed to read subcmap structure"); michael@0: } michael@0: michael@0: if (segcountx2 & 1 || search_range & 1) { michael@0: return OTS_FAILURE_MSG("Bad subcmap structure"); michael@0: } michael@0: const uint16_t segcount = segcountx2 >> 1; michael@0: // There must be at least one segment according the spec. michael@0: if (segcount < 1) { michael@0: return OTS_FAILURE_MSG("Segcount < 1 (%d)", segcount); michael@0: } michael@0: michael@0: // log2segcount is the maximal x s.t. 2^x < segcount michael@0: unsigned log2segcount = 0; michael@0: while (1u << (log2segcount + 1) <= segcount) { michael@0: log2segcount++; michael@0: } michael@0: michael@0: const uint16_t expected_search_range = 2 * 1u << log2segcount; michael@0: if (expected_search_range != search_range) { michael@0: return OTS_FAILURE_MSG("expected search range != search range (%d != %d)", expected_search_range, search_range); michael@0: } michael@0: michael@0: if (entry_selector != log2segcount) { michael@0: return OTS_FAILURE_MSG("entry selector != log2(segement count) (%d != %d)", entry_selector, log2segcount); michael@0: } michael@0: michael@0: const uint16_t expected_range_shift = segcountx2 - search_range; michael@0: if (range_shift != expected_range_shift) { michael@0: return OTS_FAILURE_MSG("unexpected range shift (%d != %d)", range_shift, expected_range_shift); michael@0: } michael@0: michael@0: std::vector ranges(segcount); michael@0: michael@0: for (unsigned i = 0; i < segcount; ++i) { michael@0: if (!subtable.ReadU16(&ranges[i].end_range)) { michael@0: return OTS_FAILURE_MSG("Failed to read segment %d", i); michael@0: } michael@0: } michael@0: michael@0: uint16_t padding; michael@0: if (!subtable.ReadU16(&padding)) { michael@0: return OTS_FAILURE_MSG("Failed to read cmap subtable segment padding"); michael@0: } michael@0: if (padding) { michael@0: return OTS_FAILURE_MSG("Non zero cmap subtable segment padding (%d)", padding); michael@0: } michael@0: michael@0: for (unsigned i = 0; i < segcount; ++i) { michael@0: if (!subtable.ReadU16(&ranges[i].start_range)) { michael@0: return OTS_FAILURE_MSG("Failed to read segment start range %d", i); michael@0: } michael@0: } michael@0: for (unsigned i = 0; i < segcount; ++i) { michael@0: if (!subtable.ReadS16(&ranges[i].id_delta)) { michael@0: return OTS_FAILURE_MSG("Failed to read segment delta %d", i); michael@0: } michael@0: } michael@0: for (unsigned i = 0; i < segcount; ++i) { michael@0: ranges[i].id_range_offset_offset = subtable.offset(); michael@0: if (!subtable.ReadU16(&ranges[i].id_range_offset)) { michael@0: return OTS_FAILURE_MSG("Failed to read segment range offset %d", i); michael@0: } michael@0: michael@0: if (ranges[i].id_range_offset & 1) { michael@0: // Some font generators seem to put 65535 on id_range_offset michael@0: // for 0xFFFF-0xFFFF range. michael@0: // (e.g., many fonts in http://www.princexml.com/fonts/) michael@0: if (i == segcount - 1u) { michael@0: OTS_WARNING("bad id_range_offset"); michael@0: ranges[i].id_range_offset = 0; michael@0: // The id_range_offset value in the transcoded font will not change michael@0: // since this table is not actually "transcoded" yet. michael@0: } else { michael@0: return OTS_FAILURE_MSG("Bad segment offset (%d)", ranges[i].id_range_offset); michael@0: } michael@0: } michael@0: } michael@0: michael@0: // ranges must be ascending order, based on the end_code. Ranges may not michael@0: // overlap. michael@0: for (unsigned i = 1; i < segcount; ++i) { michael@0: if ((i == segcount - 1u) && michael@0: (ranges[i - 1].start_range == 0xffff) && michael@0: (ranges[i - 1].end_range == 0xffff) && michael@0: (ranges[i].start_range == 0xffff) && michael@0: (ranges[i].end_range == 0xffff)) { michael@0: // Some fonts (e.g., Germania.ttf) have multiple 0xffff terminators. michael@0: // We'll accept them as an exception. michael@0: OTS_WARNING("multiple 0xffff terminators found"); michael@0: continue; michael@0: } michael@0: michael@0: // Note: some Linux fonts (e.g., LucidaSansOblique.ttf, bsmi00lp.ttf) have michael@0: // unsorted table... michael@0: if (ranges[i].end_range <= ranges[i - 1].end_range) { michael@0: return OTS_FAILURE_MSG("Out of order end range (%d <= %d)", ranges[i].end_range, ranges[i-1].end_range); michael@0: } michael@0: if (ranges[i].start_range <= ranges[i - 1].end_range) { michael@0: return OTS_FAILURE_MSG("out of order start range (%d <= %d)", ranges[i].start_range, ranges[i-1].end_range); michael@0: } michael@0: michael@0: // On many fonts, the value of {first, last}_char_index are incorrect. michael@0: // Fix them. michael@0: if (file->os2->first_char_index != 0xFFFF && michael@0: ranges[i].start_range != 0xFFFF && michael@0: file->os2->first_char_index > ranges[i].start_range) { michael@0: file->os2->first_char_index = ranges[i].start_range; michael@0: } michael@0: if (file->os2->last_char_index != 0xFFFF && michael@0: ranges[i].end_range != 0xFFFF && michael@0: file->os2->last_char_index < ranges[i].end_range) { michael@0: file->os2->last_char_index = ranges[i].end_range; michael@0: } michael@0: } michael@0: michael@0: // The last range must end at 0xffff michael@0: if (ranges[segcount - 1].start_range != 0xffff || ranges[segcount - 1].end_range != 0xffff) { michael@0: return OTS_FAILURE_MSG("Final segment start and end must be 0xFFFF (0x%04X-0x%04X)", michael@0: ranges[segcount - 1].start_range, ranges[segcount - 1].end_range); michael@0: } michael@0: michael@0: // A format 4 CMAP subtable is complex. To be safe we simulate a lookup of michael@0: // each code-point defined in the table and make sure that they are all valid michael@0: // glyphs and that we don't access anything out-of-bounds. michael@0: for (unsigned i = 0; i < segcount; ++i) { michael@0: for (unsigned cp = ranges[i].start_range; cp <= ranges[i].end_range; ++cp) { michael@0: const uint16_t code_point = cp; michael@0: if (ranges[i].id_range_offset == 0) { michael@0: // this is explictly allowed to overflow in the spec michael@0: const uint16_t glyph = code_point + ranges[i].id_delta; michael@0: if (glyph >= num_glyphs) { michael@0: return OTS_FAILURE_MSG("Range glyph reference too high (%d > %d)", glyph, num_glyphs - 1); michael@0: } michael@0: } else { michael@0: const uint16_t range_delta = code_point - ranges[i].start_range; michael@0: // this might seem odd, but it's true. The offset is relative to the michael@0: // location of the offset value itself. michael@0: const uint32_t glyph_id_offset = ranges[i].id_range_offset_offset + michael@0: ranges[i].id_range_offset + michael@0: range_delta * 2; michael@0: // We need to be able to access a 16-bit value from this offset michael@0: if (glyph_id_offset + 1 >= length) { michael@0: return OTS_FAILURE_MSG("bad glyph id offset (%d > %ld)", glyph_id_offset, length); michael@0: } michael@0: uint16_t glyph; michael@0: std::memcpy(&glyph, data + glyph_id_offset, 2); michael@0: glyph = ntohs(glyph); michael@0: if (glyph >= num_glyphs) { michael@0: return OTS_FAILURE_MSG("Range glyph reference too high (%d > %d)", glyph, num_glyphs - 1); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: // We accept the table. michael@0: // TODO(yusukes): transcode the subtable. michael@0: if (platform == 3 && encoding == 0) { michael@0: file->cmap->subtable_3_0_4_data = data; michael@0: file->cmap->subtable_3_0_4_length = length; michael@0: } else if (platform == 3 && encoding == 1) { michael@0: file->cmap->subtable_3_1_4_data = data; michael@0: file->cmap->subtable_3_1_4_length = length; michael@0: } else if (platform == 0 && encoding == 3) { michael@0: file->cmap->subtable_0_3_4_data = data; michael@0: file->cmap->subtable_0_3_4_length = length; michael@0: } else { michael@0: return OTS_FAILURE_MSG("Unknown cmap subtable type (platform=%d, encoding=%d)", platform, encoding); michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: bool Parse31012(ots::OpenTypeFile *file, michael@0: const uint8_t *data, size_t length, uint16_t num_glyphs) { michael@0: ots::Buffer subtable(data, length); michael@0: michael@0: // Format 12 tables are simple. We parse these and fully serialise them michael@0: // later. michael@0: michael@0: if (!subtable.Skip(8)) { michael@0: return OTS_FAILURE_MSG("failed to skip the first 8 bytes of format 12 subtable"); michael@0: } michael@0: uint32_t language = 0; michael@0: if (!subtable.ReadU32(&language)) { michael@0: return OTS_FAILURE_MSG("can't read format 12 subtable language"); michael@0: } michael@0: if (language) { michael@0: return OTS_FAILURE_MSG("format 12 subtable language should be zero (%d)", language); michael@0: } michael@0: michael@0: uint32_t num_groups = 0; michael@0: if (!subtable.ReadU32(&num_groups)) { michael@0: return OTS_FAILURE_MSG("can't read number of format 12 subtable groups"); michael@0: } michael@0: if (num_groups == 0 || num_groups > kMaxCMAPGroups) { michael@0: return OTS_FAILURE_MSG("bad format 12 subtable group count %d", num_groups); michael@0: } michael@0: michael@0: std::vector &groups michael@0: = file->cmap->subtable_3_10_12; michael@0: groups.resize(num_groups); michael@0: michael@0: for (unsigned i = 0; i < num_groups; ++i) { michael@0: if (!subtable.ReadU32(&groups[i].start_range) || michael@0: !subtable.ReadU32(&groups[i].end_range) || michael@0: !subtable.ReadU32(&groups[i].start_glyph_id)) { michael@0: return OTS_FAILURE_MSG("can't read format 12 subtable group"); michael@0: } michael@0: michael@0: if (groups[i].start_range > kUnicodeUpperLimit || michael@0: groups[i].end_range > kUnicodeUpperLimit || michael@0: groups[i].start_glyph_id > 0xFFFF) { michael@0: return OTS_FAILURE_MSG("bad format 12 subtable group (startCharCode=0x%4X, endCharCode=0x%4X, startGlyphID=%d)", michael@0: groups[i].start_range, groups[i].end_range, groups[i].start_glyph_id); michael@0: } michael@0: michael@0: // [0xD800, 0xDFFF] are surrogate code points. michael@0: if (groups[i].start_range >= 0xD800 && michael@0: groups[i].start_range <= 0xDFFF) { michael@0: return OTS_FAILURE_MSG("format 12 subtable out of range group startCharCode (0x%4X)", groups[i].start_range); michael@0: } michael@0: if (groups[i].end_range >= 0xD800 && michael@0: groups[i].end_range <= 0xDFFF) { michael@0: return OTS_FAILURE_MSG("format 12 subtable out of range group endCharCode (0x%4X)", groups[i].end_range); michael@0: } michael@0: if (groups[i].start_range < 0xD800 && michael@0: groups[i].end_range > 0xDFFF) { michael@0: return OTS_FAILURE_MSG("bad format 12 subtable group startCharCode (0x%4X) or endCharCode (0x%4X)", michael@0: groups[i].start_range, groups[i].end_range); michael@0: } michael@0: michael@0: // We assert that the glyph value is within range. Because of the range michael@0: // limits, above, we don't need to worry about overflow. michael@0: if (groups[i].end_range < groups[i].start_range) { michael@0: return OTS_FAILURE_MSG("format 12 subtable group endCharCode before startCharCode (0x%4X < 0x%4X)", michael@0: groups[i].end_range, groups[i].start_range); michael@0: } michael@0: if ((groups[i].end_range - groups[i].start_range) + michael@0: groups[i].start_glyph_id > num_glyphs) { michael@0: return OTS_FAILURE_MSG("bad format 12 subtable group startGlyphID (%d)", groups[i].start_glyph_id); michael@0: } michael@0: } michael@0: michael@0: // the groups must be sorted by start code and may not overlap michael@0: for (unsigned i = 1; i < num_groups; ++i) { michael@0: if (groups[i].start_range <= groups[i - 1].start_range) { michael@0: return OTS_FAILURE_MSG("out of order format 12 subtable group (startCharCode=0x%4X <= startCharCode=0x%4X of previous group)", michael@0: groups[i].start_range, groups[i-1].start_range); michael@0: } michael@0: if (groups[i].start_range <= groups[i - 1].end_range) { michael@0: return OTS_FAILURE_MSG("overlapping format 12 subtable groups (startCharCode=0x%4X <= endCharCode=0x%4X of previous group)", michael@0: groups[i].start_range, groups[i-1].end_range); michael@0: } michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: bool Parse31013(ots::OpenTypeFile *file, michael@0: const uint8_t *data, size_t length, uint16_t num_glyphs) { michael@0: ots::Buffer subtable(data, length); michael@0: michael@0: // Format 13 tables are simple. We parse these and fully serialise them michael@0: // later. michael@0: michael@0: if (!subtable.Skip(8)) { michael@0: return OTS_FAILURE_MSG("Bad cmap subtable length"); michael@0: } michael@0: uint16_t language = 0; michael@0: if (!subtable.ReadU16(&language)) { michael@0: return OTS_FAILURE_MSG("Can't read cmap subtable language"); michael@0: } michael@0: if (language) { michael@0: return OTS_FAILURE_MSG("Cmap subtable language should be zero but is %d", language); michael@0: } michael@0: michael@0: uint32_t num_groups = 0; michael@0: if (!subtable.ReadU32(&num_groups)) { michael@0: return OTS_FAILURE_MSG("Can't read number of groups in a cmap subtable"); michael@0: } michael@0: michael@0: // We limit the number of groups in the same way as in 3.10.12 tables. See michael@0: // the comment there in michael@0: if (num_groups == 0 || num_groups > kMaxCMAPGroups) { michael@0: return OTS_FAILURE_MSG("Bad number of groups (%d) in a cmap subtable", num_groups); michael@0: } michael@0: michael@0: std::vector &groups michael@0: = file->cmap->subtable_3_10_13; michael@0: groups.resize(num_groups); michael@0: michael@0: for (unsigned i = 0; i < num_groups; ++i) { michael@0: if (!subtable.ReadU32(&groups[i].start_range) || michael@0: !subtable.ReadU32(&groups[i].end_range) || michael@0: !subtable.ReadU32(&groups[i].start_glyph_id)) { michael@0: return OTS_FAILURE_MSG("Can't read subrange structure in a cmap subtable"); michael@0: } michael@0: michael@0: // We conservatively limit all of the values to protect some parsers from michael@0: // overflows michael@0: if (groups[i].start_range > kUnicodeUpperLimit || michael@0: groups[i].end_range > kUnicodeUpperLimit || michael@0: groups[i].start_glyph_id > 0xFFFF) { michael@0: return OTS_FAILURE_MSG("Bad subrange with start_range=%d, end_range=%d, start_glyph_id=%d", groups[i].start_range, groups[i].end_range, groups[i].start_glyph_id); michael@0: } michael@0: michael@0: if (groups[i].start_glyph_id >= num_glyphs) { michael@0: return OTS_FAILURE_MSG("Subrange starting glyph id too high (%d > %d)", groups[i].start_glyph_id, num_glyphs); michael@0: } michael@0: } michael@0: michael@0: // the groups must be sorted by start code and may not overlap michael@0: for (unsigned i = 1; i < num_groups; ++i) { michael@0: if (groups[i].start_range <= groups[i - 1].start_range) { michael@0: return OTS_FAILURE_MSG("Overlapping subrange starts (%d >= %d)", groups[i]. start_range, groups[i-1].start_range); michael@0: } michael@0: if (groups[i].start_range <= groups[i - 1].end_range) { michael@0: return OTS_FAILURE_MSG("Overlapping subranges (%d <= %d)", groups[i].start_range, groups[i-1].end_range); michael@0: } michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: bool Parse0514(ots::OpenTypeFile *file, michael@0: const uint8_t *data, size_t length, uint16_t num_glyphs) { michael@0: // Unicode Variation Selector table michael@0: ots::Buffer subtable(data, length); michael@0: michael@0: // Format 14 tables are simple. We parse these and fully serialise them michael@0: // later. michael@0: michael@0: // Skip format (USHORT) and length (ULONG) michael@0: if (!subtable.Skip(6)) { michael@0: return OTS_FAILURE_MSG("Can't read start of cmap subtable"); michael@0: } michael@0: michael@0: uint32_t num_records = 0; michael@0: if (!subtable.ReadU32(&num_records)) { michael@0: return OTS_FAILURE_MSG("Can't read number of records in cmap subtable"); michael@0: } michael@0: if (num_records == 0 || num_records > kMaxCMAPSelectorRecords) { michael@0: return OTS_FAILURE_MSG("Bad number of records (%d) in cmap subtable", num_records); michael@0: } michael@0: michael@0: std::vector& records michael@0: = file->cmap->subtable_0_5_14; michael@0: records.resize(num_records); michael@0: michael@0: for (unsigned i = 0; i < num_records; ++i) { michael@0: if (!subtable.ReadU24(&records[i].var_selector) || michael@0: !subtable.ReadU32(&records[i].default_offset) || michael@0: !subtable.ReadU32(&records[i].non_default_offset)) { michael@0: return OTS_FAILURE_MSG("Can't read record structure of record %d in cmap subtale", i); michael@0: } michael@0: // Checks the value of variation selector michael@0: if (!((records[i].var_selector >= kMongolianVSStart && michael@0: records[i].var_selector <= kMongolianVSEnd) || michael@0: (records[i].var_selector >= kVSStart && michael@0: records[i].var_selector <= kVSEnd) || michael@0: (records[i].var_selector >= kIVSStart && michael@0: records[i].var_selector <= kIVSEnd))) { michael@0: return OTS_FAILURE_MSG("Bad record variation selector (%04X) in record %i", records[i].var_selector, i); michael@0: } michael@0: if (i > 0 && michael@0: records[i-1].var_selector >= records[i].var_selector) { michael@0: return OTS_FAILURE_MSG("Out of order variation selector (%04X >= %04X) in record %d", records[i-1].var_selector, records[i].var_selector, i); michael@0: } michael@0: michael@0: // Checks offsets michael@0: if (!records[i].default_offset && !records[i].non_default_offset) { michael@0: return OTS_FAILURE_MSG("No default aoffset in variation selector record %d", i); michael@0: } michael@0: if (records[i].default_offset && michael@0: records[i].default_offset >= length) { michael@0: return OTS_FAILURE_MSG("Default offset too high (%d >= %ld) in record %d", records[i].default_offset, length, i); michael@0: } michael@0: if (records[i].non_default_offset && michael@0: records[i].non_default_offset >= length) { michael@0: return OTS_FAILURE_MSG("Non default offset too high (%d >= %ld) in record %d", records[i].non_default_offset, length, i); michael@0: } michael@0: } michael@0: michael@0: for (unsigned i = 0; i < num_records; ++i) { michael@0: // Checks default UVS table michael@0: if (records[i].default_offset) { michael@0: subtable.set_offset(records[i].default_offset); michael@0: uint32_t num_ranges = 0; michael@0: if (!subtable.ReadU32(&num_ranges)) { michael@0: return OTS_FAILURE_MSG("Can't read number of ranges in record %d", i); michael@0: } michael@0: if (!num_ranges || num_ranges > kMaxCMAPGroups) { michael@0: return OTS_FAILURE_MSG("number of ranges too high (%d > %d) in record %d", num_ranges, kMaxCMAPGroups, i); michael@0: } michael@0: michael@0: uint32_t last_unicode_value = 0; michael@0: std::vector& ranges michael@0: = records[i].ranges; michael@0: ranges.resize(num_ranges); michael@0: michael@0: for (unsigned j = 0; j < num_ranges; ++j) { michael@0: if (!subtable.ReadU24(&ranges[j].unicode_value) || michael@0: !subtable.ReadU8(&ranges[j].additional_count)) { michael@0: return OTS_FAILURE_MSG("Can't read range info in variation selector record %d", i); michael@0: } michael@0: const uint32_t check_value = michael@0: ranges[j].unicode_value + ranges[j].additional_count; michael@0: if (ranges[j].unicode_value == 0 || michael@0: ranges[j].unicode_value > kUnicodeUpperLimit || michael@0: check_value > kUVSUpperLimit || michael@0: (last_unicode_value && michael@0: ranges[j].unicode_value <= last_unicode_value)) { michael@0: return OTS_FAILURE_MSG("Bad Unicode value *%04X) in variation selector range %d record %d", ranges[j].unicode_value, j, i); michael@0: } michael@0: last_unicode_value = check_value; michael@0: } michael@0: } michael@0: michael@0: // Checks non default UVS table michael@0: if (records[i].non_default_offset) { michael@0: subtable.set_offset(records[i].non_default_offset); michael@0: uint32_t num_mappings = 0; michael@0: if (!subtable.ReadU32(&num_mappings)) { michael@0: return OTS_FAILURE_MSG("Can't read number of mappings in variation selector record %d", i); michael@0: } michael@0: if (!num_mappings || num_mappings > kMaxCMAPGroups) { michael@0: return OTS_FAILURE_MSG("Number of mappings too high (%d) in variation selector record %d", num_mappings, i); michael@0: } michael@0: michael@0: uint32_t last_unicode_value = 0; michael@0: std::vector& mappings michael@0: = records[i].mappings; michael@0: mappings.resize(num_mappings); michael@0: michael@0: for (unsigned j = 0; j < num_mappings; ++j) { michael@0: if (!subtable.ReadU24(&mappings[j].unicode_value) || michael@0: !subtable.ReadU16(&mappings[j].glyph_id)) { michael@0: return OTS_FAILURE_MSG("Can't read mapping %d in variation selector record %d", j, i); michael@0: } michael@0: if (mappings[j].glyph_id == 0 || michael@0: mappings[j].unicode_value == 0 || michael@0: mappings[j].unicode_value > kUnicodeUpperLimit || michael@0: (last_unicode_value && michael@0: mappings[j].unicode_value <= last_unicode_value)) { michael@0: return OTS_FAILURE_MSG("Bad mapping (%04X -> %d) in mapping %d of variation selector %d", mappings[j].unicode_value, mappings[j].glyph_id, j, i); michael@0: } michael@0: last_unicode_value = mappings[j].unicode_value; michael@0: } michael@0: } michael@0: } michael@0: michael@0: if (subtable.offset() != length) { michael@0: return OTS_FAILURE_MSG("Bad subtable offset (%ld != %ld)", subtable.offset(), length); michael@0: } michael@0: file->cmap->subtable_0_5_14_length = subtable.offset(); michael@0: return true; michael@0: } michael@0: michael@0: bool Parse100(ots::OpenTypeFile *file, const uint8_t *data, size_t length) { michael@0: // Mac Roman table michael@0: ots::Buffer subtable(data, length); michael@0: michael@0: if (!subtable.Skip(4)) { michael@0: return OTS_FAILURE_MSG("Bad cmap subtable"); michael@0: } michael@0: uint16_t language = 0; michael@0: if (!subtable.ReadU16(&language)) { michael@0: return OTS_FAILURE_MSG("Can't read language in cmap subtable"); michael@0: } michael@0: if (language) { michael@0: // simsun.ttf has non-zero language id. michael@0: OTS_WARNING("language id should be zero: %u", language); michael@0: } michael@0: michael@0: file->cmap->subtable_1_0_0.reserve(kFormat0ArraySize); michael@0: for (size_t i = 0; i < kFormat0ArraySize; ++i) { michael@0: uint8_t glyph_id = 0; michael@0: if (!subtable.ReadU8(&glyph_id)) { michael@0: return OTS_FAILURE_MSG("Can't read glyph id at array[%ld] in cmap subtable", i); michael@0: } michael@0: file->cmap->subtable_1_0_0.push_back(glyph_id); michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: } // namespace michael@0: michael@0: namespace ots { michael@0: michael@0: bool ots_cmap_parse(OpenTypeFile *file, const uint8_t *data, size_t length) { michael@0: Buffer table(data, length); michael@0: file->cmap = new OpenTypeCMAP; michael@0: michael@0: uint16_t version = 0; michael@0: uint16_t num_tables = 0; michael@0: if (!table.ReadU16(&version) || michael@0: !table.ReadU16(&num_tables)) { michael@0: return OTS_FAILURE_MSG("Can't read structure of cmap"); michael@0: } michael@0: michael@0: if (version != 0) { michael@0: return OTS_FAILURE_MSG("Non zero cmap version (%d)", version); michael@0: } michael@0: if (!num_tables) { michael@0: return OTS_FAILURE_MSG("No subtables in cmap!"); michael@0: } michael@0: michael@0: std::vector subtable_headers; michael@0: michael@0: // read the subtable headers michael@0: subtable_headers.reserve(num_tables); michael@0: for (unsigned i = 0; i < num_tables; ++i) { michael@0: CMAPSubtableHeader subt; michael@0: michael@0: if (!table.ReadU16(&subt.platform) || michael@0: !table.ReadU16(&subt.encoding) || michael@0: !table.ReadU32(&subt.offset)) { michael@0: return OTS_FAILURE_MSG("Can't read subtable information cmap subtable %d", i); michael@0: } michael@0: michael@0: subtable_headers.push_back(subt); michael@0: } michael@0: michael@0: const size_t data_offset = table.offset(); michael@0: michael@0: // make sure that all the offsets are valid. michael@0: for (unsigned i = 0; i < num_tables; ++i) { michael@0: if (subtable_headers[i].offset > 1024 * 1024 * 1024) { michael@0: return OTS_FAILURE_MSG("Bad subtable offset in cmap subtable %d", i); michael@0: } michael@0: if (subtable_headers[i].offset < data_offset || michael@0: subtable_headers[i].offset >= length) { michael@0: return OTS_FAILURE_MSG("Bad subtable offset (%d) in cmap subtable %d", subtable_headers[i].offset, i); michael@0: } michael@0: } michael@0: michael@0: // the format of the table is the first couple of bytes in the table. The michael@0: // length of the table is stored in a format-specific way. michael@0: for (unsigned i = 0; i < num_tables; ++i) { michael@0: table.set_offset(subtable_headers[i].offset); michael@0: if (!table.ReadU16(&subtable_headers[i].format)) { michael@0: return OTS_FAILURE_MSG("Can't read cmap subtable header format %d", i); michael@0: } michael@0: michael@0: uint16_t len = 0; michael@0: uint16_t lang = 0; michael@0: switch (subtable_headers[i].format) { michael@0: case 0: michael@0: case 4: michael@0: if (!table.ReadU16(&len)) { michael@0: return OTS_FAILURE_MSG("Can't read cmap subtable %d length", i); michael@0: } michael@0: if (!table.ReadU16(&lang)) { michael@0: return OTS_FAILURE_MSG("Can't read cmap subtable %d language", i); michael@0: } michael@0: subtable_headers[i].length = len; michael@0: subtable_headers[i].language = lang; michael@0: break; michael@0: case 12: michael@0: case 13: michael@0: if (!table.Skip(2)) { michael@0: return OTS_FAILURE_MSG("Bad cmap subtable %d structure", i); michael@0: } michael@0: if (!table.ReadU32(&subtable_headers[i].length)) { michael@0: return OTS_FAILURE_MSG("Can read cmap subtable %d length", i); michael@0: } michael@0: if (!table.ReadU32(&subtable_headers[i].language)) { michael@0: return OTS_FAILURE_MSG("Can't read cmap subtable %d language", i); michael@0: } michael@0: break; michael@0: case 14: michael@0: if (!table.ReadU32(&subtable_headers[i].length)) { michael@0: return OTS_FAILURE_MSG("Can't read cmap subtable %d length", i); michael@0: } michael@0: subtable_headers[i].language = 0; michael@0: break; michael@0: default: michael@0: subtable_headers[i].length = 0; michael@0: subtable_headers[i].language = 0; michael@0: break; michael@0: } michael@0: } michael@0: michael@0: // check if the table is sorted first by platform ID, then by encoding ID. michael@0: uint32_t last_id = 0; michael@0: for (unsigned i = 0; i < num_tables; ++i) { michael@0: uint32_t current_id michael@0: = (subtable_headers[i].platform << 24) michael@0: + (subtable_headers[i].encoding << 16) michael@0: + subtable_headers[i].language; michael@0: if ((i != 0) && (last_id >= current_id)) { michael@0: return OTS_FAILURE_MSG("subtable %d with platform ID %d, encoding ID %d, language ID %d " michael@0: "following subtable with platform ID %d, encoding ID %d, language ID %d", michael@0: i, michael@0: (uint8_t)(current_id >> 24), (uint8_t)(current_id >> 16), (uint8_t)(current_id), michael@0: (uint8_t)(last_id >> 24), (uint8_t)(last_id >> 16), (uint8_t)(last_id)); michael@0: } michael@0: last_id = current_id; michael@0: } michael@0: michael@0: // Now, verify that all the lengths are sane michael@0: for (unsigned i = 0; i < num_tables; ++i) { michael@0: if (!subtable_headers[i].length) continue; michael@0: if (subtable_headers[i].length > 1024 * 1024 * 1024) { michael@0: return OTS_FAILURE_MSG("Bad cmap subtable %d length", i); michael@0: } michael@0: // We know that both the offset and length are < 1GB, so the following michael@0: // addition doesn't overflow michael@0: const uint32_t end_byte michael@0: = subtable_headers[i].offset + subtable_headers[i].length; michael@0: if (end_byte > length) { michael@0: return OTS_FAILURE_MSG("Over long cmap subtable %d @ %d for %d", i, subtable_headers[i].offset, subtable_headers[i].length); michael@0: } michael@0: } michael@0: michael@0: // check that the cmap subtables are not overlapping. michael@0: std::set > uniq_checker; michael@0: std::vector > overlap_checker; michael@0: for (unsigned i = 0; i < num_tables; ++i) { michael@0: const uint32_t end_byte michael@0: = subtable_headers[i].offset + subtable_headers[i].length; michael@0: michael@0: if (!uniq_checker.insert(std::make_pair(subtable_headers[i].offset, michael@0: end_byte)).second) { michael@0: // Sometimes Unicode table and MS table share exactly the same data. michael@0: // We'll allow this. michael@0: continue; michael@0: } michael@0: overlap_checker.push_back( michael@0: std::make_pair(subtable_headers[i].offset, michael@0: static_cast(1) /* start */)); michael@0: overlap_checker.push_back( michael@0: std::make_pair(end_byte, static_cast(0) /* end */)); michael@0: } michael@0: std::sort(overlap_checker.begin(), overlap_checker.end()); michael@0: int overlap_count = 0; michael@0: for (unsigned i = 0; i < overlap_checker.size(); ++i) { michael@0: overlap_count += (overlap_checker[i].second ? 1 : -1); michael@0: if (overlap_count > 1) { michael@0: return OTS_FAILURE_MSG("Excessive overlap count %d", overlap_count); michael@0: } michael@0: } michael@0: michael@0: // we grab the number of glyphs in the file from the maxp table to make sure michael@0: // that the character map isn't referencing anything beyound this range. michael@0: if (!file->maxp) { michael@0: return OTS_FAILURE_MSG("No maxp table in font! Needed by cmap."); michael@0: } michael@0: const uint16_t num_glyphs = file->maxp->num_glyphs; michael@0: michael@0: // We only support a subset of the possible character map tables. Microsoft michael@0: // 'strongly recommends' that everyone supports the Unicode BMP table with michael@0: // the UCS-4 table for non-BMP glyphs. We'll pass the following subtables: michael@0: // Platform ID Encoding ID Format michael@0: // 0 0 4 (Unicode Default) michael@0: // 0 3 4 (Unicode BMP) michael@0: // 0 3 12 (Unicode UCS-4) michael@0: // 0 5 14 (Unicode Variation Sequences) michael@0: // 1 0 0 (Mac Roman) michael@0: // 3 0 4 (MS Symbol) michael@0: // 3 1 4 (MS Unicode BMP) michael@0: // 3 10 12 (MS Unicode UCS-4) michael@0: // 3 10 13 (MS UCS-4 Fallback mapping) michael@0: // michael@0: // Note: michael@0: // * 0-0-4 table is (usually) written as a 3-1-4 table. If 3-1-4 table michael@0: // also exists, the 0-0-4 table is ignored. michael@0: // * Unlike 0-0-4 table, 0-3-4 table is written as a 0-3-4 table. michael@0: // Some fonts which include 0-5-14 table seems to be required 0-3-4 michael@0: // table. The 0-3-4 table will be wriiten even if 3-1-4 table also exists. michael@0: // * 0-3-12 table is written as a 3-10-12 table. If 3-10-12 table also michael@0: // exists, the 0-3-12 table is ignored. michael@0: // michael@0: michael@0: for (unsigned i = 0; i < num_tables; ++i) { michael@0: if (subtable_headers[i].platform == 0) { michael@0: // Unicode platform michael@0: michael@0: if ((subtable_headers[i].encoding == 0) && michael@0: (subtable_headers[i].format == 4)) { michael@0: // parse and output the 0-0-4 table as 3-1-4 table. Sometimes the 0-0-4 michael@0: // table actually points to MS symbol data and thus should be parsed as michael@0: // 3-0-4 table (e.g., marqueem.ttf and quixotic.ttf). This error will be michael@0: // recovered in ots_cmap_serialise(). michael@0: if (!ParseFormat4(file, 3, 1, data + subtable_headers[i].offset, michael@0: subtable_headers[i].length, num_glyphs)) { michael@0: return OTS_FAILURE_MSG("Failed to parse format 4 cmap subtable %d", i); michael@0: } michael@0: } else if ((subtable_headers[i].encoding == 3) && michael@0: (subtable_headers[i].format == 4)) { michael@0: // parse and output the 0-3-4 table as 0-3-4 table. michael@0: if (!ParseFormat4(file, 0, 3, data + subtable_headers[i].offset, michael@0: subtable_headers[i].length, num_glyphs)) { michael@0: return OTS_FAILURE_MSG("Failed to parse format 4 cmap subtable %d", i); michael@0: } michael@0: } else if ((subtable_headers[i].encoding == 3) && michael@0: (subtable_headers[i].format == 12)) { michael@0: // parse and output the 0-3-12 table as 3-10-12 table. michael@0: if (!Parse31012(file, data + subtable_headers[i].offset, michael@0: subtable_headers[i].length, num_glyphs)) { michael@0: return OTS_FAILURE_MSG("Failed to parse format 12 cmap subtable %d", i); michael@0: } michael@0: } else if ((subtable_headers[i].encoding == 5) && michael@0: (subtable_headers[i].format == 14)) { michael@0: if (!Parse0514(file, data + subtable_headers[i].offset, michael@0: subtable_headers[i].length, num_glyphs)) { michael@0: return OTS_FAILURE_MSG("Failed to parse format 14 cmap subtable %d", i); michael@0: } michael@0: } michael@0: } else if (subtable_headers[i].platform == 1) { michael@0: // Mac platform michael@0: michael@0: if ((subtable_headers[i].encoding == 0) && michael@0: (subtable_headers[i].format == 0)) { michael@0: // parse and output the 1-0-0 table. michael@0: if (!Parse100(file, data + subtable_headers[i].offset, michael@0: subtable_headers[i].length)) { michael@0: return OTS_FAILURE(); michael@0: } michael@0: } michael@0: } else if (subtable_headers[i].platform == 3) { michael@0: // MS platform michael@0: michael@0: switch (subtable_headers[i].encoding) { michael@0: case 0: michael@0: case 1: michael@0: if (subtable_headers[i].format == 4) { michael@0: // parse 3-0-4 or 3-1-4 table. michael@0: if (!ParseFormat4(file, subtable_headers[i].platform, michael@0: subtable_headers[i].encoding, michael@0: data + subtable_headers[i].offset, michael@0: subtable_headers[i].length, num_glyphs)) { michael@0: return OTS_FAILURE(); michael@0: } michael@0: } michael@0: break; michael@0: case 10: michael@0: if (subtable_headers[i].format == 12) { michael@0: file->cmap->subtable_3_10_12.clear(); michael@0: if (!Parse31012(file, data + subtable_headers[i].offset, michael@0: subtable_headers[i].length, num_glyphs)) { michael@0: return OTS_FAILURE(); michael@0: } michael@0: } else if (subtable_headers[i].format == 13) { michael@0: file->cmap->subtable_3_10_13.clear(); michael@0: if (!Parse31013(file, data + subtable_headers[i].offset, michael@0: subtable_headers[i].length, num_glyphs)) { michael@0: return OTS_FAILURE(); michael@0: } michael@0: } michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: bool ots_cmap_should_serialise(OpenTypeFile *file) { michael@0: return file->cmap != NULL; michael@0: } michael@0: michael@0: bool ots_cmap_serialise(OTSStream *out, OpenTypeFile *file) { michael@0: const bool have_034 = file->cmap->subtable_0_3_4_data != NULL; michael@0: const bool have_0514 = file->cmap->subtable_0_5_14.size() != 0; michael@0: const bool have_100 = file->cmap->subtable_1_0_0.size() != 0; michael@0: const bool have_304 = file->cmap->subtable_3_0_4_data != NULL; michael@0: // MS Symbol and MS Unicode tables should not co-exist. michael@0: // See the comment above in 0-0-4 parser. michael@0: const bool have_314 = (!have_304) && file->cmap->subtable_3_1_4_data; michael@0: const bool have_31012 = file->cmap->subtable_3_10_12.size() != 0; michael@0: const bool have_31013 = file->cmap->subtable_3_10_13.size() != 0; michael@0: const unsigned num_subtables = static_cast(have_034) + michael@0: static_cast(have_0514) + michael@0: static_cast(have_100) + michael@0: static_cast(have_304) + michael@0: static_cast(have_314) + michael@0: static_cast(have_31012) + michael@0: static_cast(have_31013); michael@0: const off_t table_start = out->Tell(); michael@0: michael@0: // Some fonts don't have 3-0-4 MS Symbol nor 3-1-4 Unicode BMP tables michael@0: // (e.g., old fonts for Mac). We don't support them. michael@0: if (!have_304 && !have_314 && !have_034) { michael@0: return OTS_FAILURE(); michael@0: } michael@0: michael@0: if (!out->WriteU16(0) || michael@0: !out->WriteU16(num_subtables)) { michael@0: return OTS_FAILURE(); michael@0: } michael@0: michael@0: const off_t record_offset = out->Tell(); michael@0: if (!out->Pad(num_subtables * 8)) { michael@0: return OTS_FAILURE(); michael@0: } michael@0: michael@0: const off_t offset_034 = out->Tell(); michael@0: if (have_034) { michael@0: if (!out->Write(file->cmap->subtable_0_3_4_data, michael@0: file->cmap->subtable_0_3_4_length)) { michael@0: return OTS_FAILURE(); michael@0: } michael@0: } michael@0: michael@0: const off_t offset_0514 = out->Tell(); michael@0: if (have_0514) { michael@0: const std::vector &records michael@0: = file->cmap->subtable_0_5_14; michael@0: const unsigned num_records = records.size(); michael@0: if (!out->WriteU16(14) || michael@0: !out->WriteU32(file->cmap->subtable_0_5_14_length) || michael@0: !out->WriteU32(num_records)) { michael@0: return OTS_FAILURE(); michael@0: } michael@0: for (unsigned i = 0; i < num_records; ++i) { michael@0: if (!out->WriteU24(records[i].var_selector) || michael@0: !out->WriteU32(records[i].default_offset) || michael@0: !out->WriteU32(records[i].non_default_offset)) { michael@0: return OTS_FAILURE(); michael@0: } michael@0: } michael@0: for (unsigned i = 0; i < num_records; ++i) { michael@0: if (records[i].default_offset) { michael@0: const std::vector &ranges michael@0: = records[i].ranges; michael@0: const unsigned num_ranges = ranges.size(); michael@0: if (!out->Seek(records[i].default_offset + offset_0514) || michael@0: !out->WriteU32(num_ranges)) { michael@0: return OTS_FAILURE(); michael@0: } michael@0: for (unsigned j = 0; j < num_ranges; ++j) { michael@0: if (!out->WriteU24(ranges[j].unicode_value) || michael@0: !out->WriteU8(ranges[j].additional_count)) { michael@0: return OTS_FAILURE(); michael@0: } michael@0: } michael@0: } michael@0: if (records[i].non_default_offset) { michael@0: const std::vector &mappings michael@0: = records[i].mappings; michael@0: const unsigned num_mappings = mappings.size(); michael@0: if (!out->Seek(records[i].non_default_offset + offset_0514) || michael@0: !out->WriteU32(num_mappings)) { michael@0: return OTS_FAILURE(); michael@0: } michael@0: for (unsigned j = 0; j < num_mappings; ++j) { michael@0: if (!out->WriteU24(mappings[j].unicode_value) || michael@0: !out->WriteU16(mappings[j].glyph_id)) { michael@0: return OTS_FAILURE(); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: const off_t offset_100 = out->Tell(); michael@0: if (have_100) { michael@0: if (!out->WriteU16(0) || // format michael@0: !out->WriteU16(6 + kFormat0ArraySize) || // length michael@0: !out->WriteU16(0)) { // language michael@0: return OTS_FAILURE(); michael@0: } michael@0: if (!out->Write(&(file->cmap->subtable_1_0_0[0]), kFormat0ArraySize)) { michael@0: return OTS_FAILURE(); michael@0: } michael@0: } michael@0: michael@0: const off_t offset_304 = out->Tell(); michael@0: if (have_304) { michael@0: if (!out->Write(file->cmap->subtable_3_0_4_data, michael@0: file->cmap->subtable_3_0_4_length)) { michael@0: return OTS_FAILURE(); michael@0: } michael@0: } michael@0: michael@0: const off_t offset_314 = out->Tell(); michael@0: if (have_314) { michael@0: if (!out->Write(file->cmap->subtable_3_1_4_data, michael@0: file->cmap->subtable_3_1_4_length)) { michael@0: return OTS_FAILURE(); michael@0: } michael@0: } michael@0: michael@0: const off_t offset_31012 = out->Tell(); michael@0: if (have_31012) { michael@0: std::vector &groups michael@0: = file->cmap->subtable_3_10_12; michael@0: const unsigned num_groups = groups.size(); michael@0: if (!out->WriteU16(12) || michael@0: !out->WriteU16(0) || michael@0: !out->WriteU32(num_groups * 12 + 16) || michael@0: !out->WriteU32(0) || michael@0: !out->WriteU32(num_groups)) { michael@0: return OTS_FAILURE(); michael@0: } michael@0: michael@0: for (unsigned i = 0; i < num_groups; ++i) { michael@0: if (!out->WriteU32(groups[i].start_range) || michael@0: !out->WriteU32(groups[i].end_range) || michael@0: !out->WriteU32(groups[i].start_glyph_id)) { michael@0: return OTS_FAILURE(); michael@0: } michael@0: } michael@0: } michael@0: michael@0: const off_t offset_31013 = out->Tell(); michael@0: if (have_31013) { michael@0: std::vector &groups michael@0: = file->cmap->subtable_3_10_13; michael@0: const unsigned num_groups = groups.size(); michael@0: if (!out->WriteU16(13) || michael@0: !out->WriteU16(0) || michael@0: !out->WriteU32(num_groups * 12 + 14) || michael@0: !out->WriteU32(0) || michael@0: !out->WriteU32(num_groups)) { michael@0: return OTS_FAILURE(); michael@0: } michael@0: michael@0: for (unsigned i = 0; i < num_groups; ++i) { michael@0: if (!out->WriteU32(groups[i].start_range) || michael@0: !out->WriteU32(groups[i].end_range) || michael@0: !out->WriteU32(groups[i].start_glyph_id)) { michael@0: return OTS_FAILURE(); michael@0: } michael@0: } michael@0: } michael@0: michael@0: const off_t table_end = out->Tell(); michael@0: // We might have hanging bytes from the above's checksum which the OTSStream michael@0: // then merges into the table of offsets. michael@0: OTSStream::ChecksumState saved_checksum = out->SaveChecksumState(); michael@0: out->ResetChecksum(); michael@0: michael@0: // Now seek back and write the table of offsets michael@0: if (!out->Seek(record_offset)) { michael@0: return OTS_FAILURE(); michael@0: } michael@0: michael@0: if (have_034) { michael@0: if (!out->WriteU16(0) || michael@0: !out->WriteU16(3) || michael@0: !out->WriteU32(offset_034 - table_start)) { michael@0: return OTS_FAILURE(); michael@0: } michael@0: } michael@0: michael@0: if (have_0514) { michael@0: if (!out->WriteU16(0) || michael@0: !out->WriteU16(5) || michael@0: !out->WriteU32(offset_0514 - table_start)) { michael@0: return OTS_FAILURE(); michael@0: } michael@0: } michael@0: michael@0: if (have_100) { michael@0: if (!out->WriteU16(1) || michael@0: !out->WriteU16(0) || michael@0: !out->WriteU32(offset_100 - table_start)) { michael@0: return OTS_FAILURE(); michael@0: } michael@0: } michael@0: michael@0: if (have_304) { michael@0: if (!out->WriteU16(3) || michael@0: !out->WriteU16(0) || michael@0: !out->WriteU32(offset_304 - table_start)) { michael@0: return OTS_FAILURE(); michael@0: } michael@0: } michael@0: michael@0: if (have_314) { michael@0: if (!out->WriteU16(3) || michael@0: !out->WriteU16(1) || michael@0: !out->WriteU32(offset_314 - table_start)) { michael@0: return OTS_FAILURE(); michael@0: } michael@0: } michael@0: michael@0: if (have_31012) { michael@0: if (!out->WriteU16(3) || michael@0: !out->WriteU16(10) || michael@0: !out->WriteU32(offset_31012 - table_start)) { michael@0: return OTS_FAILURE(); michael@0: } michael@0: } michael@0: michael@0: if (have_31013) { michael@0: if (!out->WriteU16(3) || michael@0: !out->WriteU16(10) || michael@0: !out->WriteU32(offset_31013 - table_start)) { michael@0: return OTS_FAILURE(); michael@0: } michael@0: } michael@0: michael@0: if (!out->Seek(table_end)) { michael@0: return OTS_FAILURE(); michael@0: } michael@0: out->RestoreChecksum(saved_checksum); michael@0: michael@0: return true; michael@0: } michael@0: michael@0: void ots_cmap_free(OpenTypeFile *file) { michael@0: delete file->cmap; michael@0: } michael@0: michael@0: } // namespace ots