diff options
Diffstat (limited to 'gfx/ots/src/kern.cc')
-rw-r--r-- | gfx/ots/src/kern.cc | 131 |
1 files changed, 50 insertions, 81 deletions
diff --git a/gfx/ots/src/kern.cc b/gfx/ots/src/kern.cc index d4ae8fcc4..ec41dfb9c 100644 --- a/gfx/ots/src/kern.cc +++ b/gfx/ots/src/kern.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Copyright (c) 2009-2017 The OTS Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -7,75 +7,61 @@ // kern - Kerning // http://www.microsoft.com/typography/otspec/kern.htm -#define TABLE_NAME "kern" - -#define DROP_THIS_TABLE(msg_) \ - do { \ - OTS_FAILURE_MSG(msg_ ", table discarded"); \ - delete font->kern; \ - font->kern = 0; \ - } while (0) - namespace ots { -bool ots_kern_parse(Font *font, const uint8_t *data, size_t length) { +bool OpenTypeKERN::Parse(const uint8_t *data, size_t length) { Buffer table(data, length); - OpenTypeKERN *kern = new OpenTypeKERN; - font->kern = kern; - uint16_t num_tables = 0; - if (!table.ReadU16(&kern->version) || + if (!table.ReadU16(&this->version) || !table.ReadU16(&num_tables)) { - return OTS_FAILURE_MSG("Failed to read kern header"); + return Error("Failed to read table header"); } - if (kern->version > 0) { - DROP_THIS_TABLE("bad table version"); - return true; + if (this->version > 0) { + return Drop("Unsupported table version: %d", this->version); } if (num_tables == 0) { - DROP_THIS_TABLE("num_tables is zero"); - return true; + return Drop("nTables is zero"); } - kern->subtables.reserve(num_tables); + this->subtables.reserve(num_tables); for (unsigned i = 0; i < num_tables; ++i) { OpenTypeKERNFormat0 subtable; uint16_t sub_length = 0; if (!table.ReadU16(&subtable.version) || !table.ReadU16(&sub_length)) { - return OTS_FAILURE_MSG("Failed to read kern subtable %d header", i); + return Error("Failed to read subtable %d header", i); } if (subtable.version > 0) { - OTS_WARNING("Bad subtable version: %d", subtable.version); + Warning("Ignoring subtable %d with unsupported version: %d", + i, subtable.version); continue; } const size_t current_offset = table.offset(); if (current_offset - 4 + sub_length > length) { - return OTS_FAILURE_MSG("Bad kern subtable %d offset %ld", i, current_offset); + return Error("Bad subtable %d offset %ld", i, current_offset); } if (!table.ReadU16(&subtable.coverage)) { - return OTS_FAILURE_MSG("Cailed to read kern subtable %d coverage", i); + return Error("Failed to read subtable %d coverage", i); } if (!(subtable.coverage & 0x1)) { - OTS_WARNING( + Warning( "We don't support vertical data as the renderer doesn't support it."); continue; } if (subtable.coverage & 0xF0) { - DROP_THIS_TABLE("Reserved fields should zero-filled"); - return true; + return Drop("Reserved fields should be zero"); } const uint32_t format = (subtable.coverage & 0xFF00) >> 8; if (format != 0) { - OTS_WARNING("Format %d is not supported.", format); + Warning("Ignoring subtable %d with unsupported format: %d", i, format); continue; } @@ -85,12 +71,11 @@ bool ots_kern_parse(Font *font, const uint8_t *data, size_t length) { !table.ReadU16(&subtable.search_range) || !table.ReadU16(&subtable.entry_selector) || !table.ReadU16(&subtable.range_shift)) { - return OTS_FAILURE_MSG("Failed to read kern subtable %d format 0 fields", i); + return Error("Failed to read subtable %d format 0 fields", i); } if (!num_pairs) { - DROP_THIS_TABLE("Zero length subtable is found"); - return true; + return Drop("Zero length subtable is found"); } // Sanity checks for search_range, entry_selector, and range_shift. See the @@ -98,8 +83,7 @@ bool ots_kern_parse(Font *font, const uint8_t *data, size_t length) { const size_t kFormat0PairSize = 6; // left, right, and value. 2 bytes each. if (num_pairs > (65536 / kFormat0PairSize)) { // Some fonts (e.g. calibri.ttf, pykes_peak_zero.ttf) have pairs >= 10923. - DROP_THIS_TABLE("Too large subtable"); - return true; + return Drop("Too large subtable"); } unsigned max_pow2 = 0; while (1u << (max_pow2 + 1) <= num_pairs) { @@ -107,16 +91,16 @@ bool ots_kern_parse(Font *font, const uint8_t *data, size_t length) { } const uint16_t expected_search_range = (1u << max_pow2) * kFormat0PairSize; if (subtable.search_range != expected_search_range) { - OTS_WARNING("bad search range"); + Warning("bad search range"); subtable.search_range = expected_search_range; } if (subtable.entry_selector != max_pow2) { - return OTS_FAILURE_MSG("Bad subtable %d entry selector %d", i, subtable.entry_selector); + return Error("Bad subtable %d entry selector %d", i, subtable.entry_selector); } const uint16_t expected_range_shift = kFormat0PairSize * num_pairs - subtable.search_range; if (subtable.range_shift != expected_range_shift) { - OTS_WARNING("bad range shift"); + Warning("bad range shift"); subtable.range_shift = expected_range_shift; } @@ -128,64 +112,55 @@ bool ots_kern_parse(Font *font, const uint8_t *data, size_t length) { if (!table.ReadU16(&kerning_pair.left) || !table.ReadU16(&kerning_pair.right) || !table.ReadS16(&kerning_pair.value)) { - return OTS_FAILURE_MSG("Failed to read subtable %d kerning pair %d", i, j); + return Error("Failed to read subtable %d kerning pair %d", i, j); } const uint32_t current_pair = (kerning_pair.left << 16) + kerning_pair.right; if (j != 0 && current_pair <= last_pair) { // Many free fonts don't follow this rule, so we don't call OTS_FAILURE // in order to support these fonts. - DROP_THIS_TABLE("Kerning pairs are not sorted"); - return true; + return Drop("Kerning pairs are not sorted"); } last_pair = current_pair; subtable.pairs.push_back(kerning_pair); } - kern->subtables.push_back(subtable); + this->subtables.push_back(subtable); } - if (!kern->subtables.size()) { - DROP_THIS_TABLE("All subtables are removed"); - return true; + if (!this->subtables.size()) { + return Drop("All subtables were removed"); } return true; } -bool ots_kern_should_serialise(Font *font) { - if (!font->glyf) return false; // this table is not for CFF fonts. - return font->kern != NULL; -} - -bool ots_kern_serialise(OTSStream *out, Font *font) { - const OpenTypeKERN *kern = font->kern; - - const uint16_t num_subtables = static_cast<uint16_t>(kern->subtables.size()); - if (num_subtables != kern->subtables.size() || - !out->WriteU16(kern->version) || +bool OpenTypeKERN::Serialize(OTSStream *out) { + const uint16_t num_subtables = static_cast<uint16_t>(this->subtables.size()); + if (num_subtables != this->subtables.size() || + !out->WriteU16(this->version) || !out->WriteU16(num_subtables)) { - return OTS_FAILURE_MSG("Can't write kern table header"); + return Error("Failed to write kern table header"); } for (uint16_t i = 0; i < num_subtables; ++i) { - const size_t length = 14 + (6 * kern->subtables[i].pairs.size()); + const size_t length = 14 + (6 * this->subtables[i].pairs.size()); if (length > std::numeric_limits<uint16_t>::max() || - !out->WriteU16(kern->subtables[i].version) || + !out->WriteU16(this->subtables[i].version) || !out->WriteU16(static_cast<uint16_t>(length)) || - !out->WriteU16(kern->subtables[i].coverage) || + !out->WriteU16(this->subtables[i].coverage) || !out->WriteU16( - static_cast<uint16_t>(kern->subtables[i].pairs.size())) || - !out->WriteU16(kern->subtables[i].search_range) || - !out->WriteU16(kern->subtables[i].entry_selector) || - !out->WriteU16(kern->subtables[i].range_shift)) { - return OTS_FAILURE_MSG("Failed to write kern subtable %d", i); - } - for (unsigned j = 0; j < kern->subtables[i].pairs.size(); ++j) { - if (!out->WriteU16(kern->subtables[i].pairs[j].left) || - !out->WriteU16(kern->subtables[i].pairs[j].right) || - !out->WriteS16(kern->subtables[i].pairs[j].value)) { - return OTS_FAILURE_MSG("Failed to write kern pair %d for subtable %d", j, i); + static_cast<uint16_t>(this->subtables[i].pairs.size())) || + !out->WriteU16(this->subtables[i].search_range) || + !out->WriteU16(this->subtables[i].entry_selector) || + !out->WriteU16(this->subtables[i].range_shift)) { + return Error("Failed to write kern subtable %d", i); + } + for (unsigned j = 0; j < this->subtables[i].pairs.size(); ++j) { + if (!out->WriteU16(this->subtables[i].pairs[j].left) || + !out->WriteU16(this->subtables[i].pairs[j].right) || + !out->WriteS16(this->subtables[i].pairs[j].value)) { + return Error("Failed to write kern pair %d for subtable %d", j, i); } } } @@ -193,16 +168,10 @@ bool ots_kern_serialise(OTSStream *out, Font *font) { return true; } -void ots_kern_reuse(Font *font, Font *other) { - font->kern = other->kern; - font->kern_reused = true; -} - -void ots_kern_free(Font *font) { - delete font->kern; +bool OpenTypeKERN::ShouldSerialize() { + return Table::ShouldSerialize() && + // this table is not for CFF fonts. + GetFont()->GetTable(OTS_TAG_GLYF) != NULL; } } // namespace ots - -#undef TABLE_NAME -#undef DROP_THIS_TABLE |