| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "kern.h" | 5 #include "kern.h" |
| 6 | 6 |
| 7 // kern - Kerning | 7 // kern - Kerning |
| 8 // http://www.microsoft.com/opentype/otspec/kern.htm | 8 // http://www.microsoft.com/typography/otspec/kern.htm |
| 9 | 9 |
| 10 #define DROP_THIS_TABLE \ | 10 #define TABLE_NAME "kern" |
| 11 do { delete file->kern; file->kern = 0; } while (0) | 11 |
| 12 #define DROP_THIS_TABLE(msg_) \ |
| 13 do { \ |
| 14 delete file->kern; \ |
| 15 file->kern = 0; \ |
| 16 OTS_FAILURE_MSG(msg_ ", table discarded"); \ |
| 17 } while (0) |
| 12 | 18 |
| 13 namespace ots { | 19 namespace ots { |
| 14 | 20 |
| 15 bool ots_kern_parse(OpenTypeFile *file, const uint8_t *data, size_t length) { | 21 bool ots_kern_parse(OpenTypeFile *file, const uint8_t *data, size_t length) { |
| 16 Buffer table(data, length); | 22 Buffer table(data, length); |
| 17 | 23 |
| 18 OpenTypeKERN *kern = new OpenTypeKERN; | 24 OpenTypeKERN *kern = new OpenTypeKERN; |
| 19 file->kern = kern; | 25 file->kern = kern; |
| 20 | 26 |
| 21 uint16_t num_tables = 0; | 27 uint16_t num_tables = 0; |
| 22 if (!table.ReadU16(&kern->version) || | 28 if (!table.ReadU16(&kern->version) || |
| 23 !table.ReadU16(&num_tables)) { | 29 !table.ReadU16(&num_tables)) { |
| 24 return OTS_FAILURE(); | 30 return OTS_FAILURE_MSG("Failed to read kern header"); |
| 25 } | 31 } |
| 26 | 32 |
| 27 if (kern->version > 0) { | 33 if (kern->version > 0) { |
| 28 DROP_THIS_TABLE; | 34 DROP_THIS_TABLE("bad table version"); |
| 29 return true; | 35 return true; |
| 30 } | 36 } |
| 31 | 37 |
| 32 if (num_tables == 0) { | 38 if (num_tables == 0) { |
| 33 OTS_WARNING("num_tables is zero"); | 39 DROP_THIS_TABLE("num_tables is zero"); |
| 34 DROP_THIS_TABLE; | |
| 35 return true; | 40 return true; |
| 36 } | 41 } |
| 37 | 42 |
| 38 kern->subtables.reserve(num_tables); | 43 kern->subtables.reserve(num_tables); |
| 39 for (unsigned i = 0; i < num_tables; ++i) { | 44 for (unsigned i = 0; i < num_tables; ++i) { |
| 40 OpenTypeKERNFormat0 subtable; | 45 OpenTypeKERNFormat0 subtable; |
| 41 uint16_t sub_length = 0; | 46 uint16_t sub_length = 0; |
| 42 | 47 |
| 43 if (!table.ReadU16(&subtable.version) || | 48 if (!table.ReadU16(&subtable.version) || |
| 44 !table.ReadU16(&sub_length)) { | 49 !table.ReadU16(&sub_length)) { |
| 45 return OTS_FAILURE(); | 50 return OTS_FAILURE_MSG("Failed to read kern subtable %d header", i); |
| 46 } | 51 } |
| 47 | 52 |
| 48 if (subtable.version > 0) { | 53 if (subtable.version > 0) { |
| 49 OTS_WARNING("Bad subtable version: %d", subtable.version); | 54 OTS_WARNING("Bad subtable version: %d", subtable.version); |
| 50 continue; | 55 continue; |
| 51 } | 56 } |
| 52 | 57 |
| 53 const size_t current_offset = table.offset(); | 58 const size_t current_offset = table.offset(); |
| 54 if (current_offset - 4 + sub_length > length) { | 59 if (current_offset - 4 + sub_length > length) { |
| 55 return OTS_FAILURE(); | 60 return OTS_FAILURE_MSG("Bad kern subtable %d offset %ld", i, current_offse
t); |
| 56 } | 61 } |
| 57 | 62 |
| 58 if (!table.ReadU16(&subtable.coverage)) { | 63 if (!table.ReadU16(&subtable.coverage)) { |
| 59 return OTS_FAILURE(); | 64 return OTS_FAILURE_MSG("Cailed to read kern subtable %d coverage", i); |
| 60 } | 65 } |
| 61 | 66 |
| 62 if (!(subtable.coverage & 0x1)) { | 67 if (!(subtable.coverage & 0x1)) { |
| 63 OTS_WARNING( | 68 OTS_WARNING( |
| 64 "We don't support vertical data as the renderer doesn't support it."); | 69 "We don't support vertical data as the renderer doesn't support it."); |
| 65 continue; | 70 continue; |
| 66 } | 71 } |
| 67 if (subtable.coverage & 0xF0) { | 72 if (subtable.coverage & 0xF0) { |
| 68 OTS_WARNING("Reserved fields should zero-filled."); | 73 DROP_THIS_TABLE("Reserved fields should zero-filled."); |
| 69 DROP_THIS_TABLE; | |
| 70 return true; | 74 return true; |
| 71 } | 75 } |
| 72 const uint32_t format = (subtable.coverage & 0xFF00) >> 8; | 76 const uint32_t format = (subtable.coverage & 0xFF00) >> 8; |
| 73 if (format != 0) { | 77 if (format != 0) { |
| 74 OTS_WARNING("Format %d is not supported.", format); | 78 OTS_WARNING("Format %d is not supported.", format); |
| 75 continue; | 79 continue; |
| 76 } | 80 } |
| 77 | 81 |
| 78 // Parse the format 0 field. | 82 // Parse the format 0 field. |
| 79 uint16_t num_pairs = 0; | 83 uint16_t num_pairs = 0; |
| 80 if (!table.ReadU16(&num_pairs) || | 84 if (!table.ReadU16(&num_pairs) || |
| 81 !table.ReadU16(&subtable.search_range) || | 85 !table.ReadU16(&subtable.search_range) || |
| 82 !table.ReadU16(&subtable.entry_selector) || | 86 !table.ReadU16(&subtable.entry_selector) || |
| 83 !table.ReadU16(&subtable.range_shift)) { | 87 !table.ReadU16(&subtable.range_shift)) { |
| 84 return OTS_FAILURE(); | 88 return OTS_FAILURE_MSG("Failed to read kern subtable %d format 0 fields",
i); |
| 85 } | 89 } |
| 86 | 90 |
| 87 if (!num_pairs) { | 91 if (!num_pairs) { |
| 88 OTS_WARNING("Zero length subtable is found."); | 92 DROP_THIS_TABLE("Zero length subtable is found."); |
| 89 DROP_THIS_TABLE; | |
| 90 return true; | 93 return true; |
| 91 } | 94 } |
| 92 | 95 |
| 93 // Sanity checks for search_range, entry_selector, and range_shift. See the | 96 // Sanity checks for search_range, entry_selector, and range_shift. See the |
| 94 // comment in ots.cc for details. | 97 // comment in ots.cc for details. |
| 95 const size_t kFormat0PairSize = 6; // left, right, and value. 2 bytes each. | 98 const size_t kFormat0PairSize = 6; // left, right, and value. 2 bytes each. |
| 96 if (num_pairs > (65536 / kFormat0PairSize)) { | 99 if (num_pairs > (65536 / kFormat0PairSize)) { |
| 97 // Some fonts (e.g. calibri.ttf, pykes_peak_zero.ttf) have pairs >= 10923. | 100 // Some fonts (e.g. calibri.ttf, pykes_peak_zero.ttf) have pairs >= 10923. |
| 98 OTS_WARNING("Too large subtable."); | 101 DROP_THIS_TABLE("Too large subtable."); |
| 99 DROP_THIS_TABLE; | |
| 100 return true; | 102 return true; |
| 101 } | 103 } |
| 102 unsigned max_pow2 = 0; | 104 unsigned max_pow2 = 0; |
| 103 while (1u << (max_pow2 + 1) <= num_pairs) { | 105 while (1u << (max_pow2 + 1) <= num_pairs) { |
| 104 ++max_pow2; | 106 ++max_pow2; |
| 105 } | 107 } |
| 106 const uint16_t expected_search_range = (1u << max_pow2) * kFormat0PairSize; | 108 const uint16_t expected_search_range = (1u << max_pow2) * kFormat0PairSize; |
| 107 if (subtable.search_range != expected_search_range) { | 109 if (subtable.search_range != expected_search_range) { |
| 108 OTS_WARNING("bad search range"); | 110 OTS_WARNING("bad search range"); |
| 109 subtable.search_range = expected_search_range; | 111 subtable.search_range = expected_search_range; |
| 110 } | 112 } |
| 111 if (subtable.entry_selector != max_pow2) { | 113 if (subtable.entry_selector != max_pow2) { |
| 112 return OTS_FAILURE(); | 114 return OTS_FAILURE_MSG("Bad subtable %d entry selector %d", i, subtable.en
try_selector); |
| 113 } | 115 } |
| 114 const uint16_t expected_range_shift = | 116 const uint16_t expected_range_shift = |
| 115 kFormat0PairSize * num_pairs - subtable.search_range; | 117 kFormat0PairSize * num_pairs - subtable.search_range; |
| 116 if (subtable.range_shift != expected_range_shift) { | 118 if (subtable.range_shift != expected_range_shift) { |
| 117 OTS_WARNING("bad range shift"); | 119 OTS_WARNING("bad range shift"); |
| 118 subtable.range_shift = expected_range_shift; | 120 subtable.range_shift = expected_range_shift; |
| 119 } | 121 } |
| 120 | 122 |
| 121 // Read kerning pairs. | 123 // Read kerning pairs. |
| 122 subtable.pairs.reserve(num_pairs); | 124 subtable.pairs.reserve(num_pairs); |
| 123 uint32_t last_pair = 0; | 125 uint32_t last_pair = 0; |
| 124 for (unsigned j = 0; j < num_pairs; ++j) { | 126 for (unsigned j = 0; j < num_pairs; ++j) { |
| 125 OpenTypeKERNFormat0Pair kerning_pair; | 127 OpenTypeKERNFormat0Pair kerning_pair; |
| 126 if (!table.ReadU16(&kerning_pair.left) || | 128 if (!table.ReadU16(&kerning_pair.left) || |
| 127 !table.ReadU16(&kerning_pair.right) || | 129 !table.ReadU16(&kerning_pair.right) || |
| 128 !table.ReadS16(&kerning_pair.value)) { | 130 !table.ReadS16(&kerning_pair.value)) { |
| 129 return OTS_FAILURE(); | 131 return OTS_FAILURE_MSG("Failed to read subtable %d kerning pair %d", i,
j); |
| 130 } | 132 } |
| 131 const uint32_t current_pair | 133 const uint32_t current_pair |
| 132 = (kerning_pair.left << 16) + kerning_pair.right; | 134 = (kerning_pair.left << 16) + kerning_pair.right; |
| 133 if (j != 0 && current_pair <= last_pair) { | 135 if (j != 0 && current_pair <= last_pair) { |
| 134 OTS_WARNING("Kerning pairs are not sorted."); | |
| 135 // Many free fonts don't follow this rule, so we don't call OTS_FAILURE | 136 // Many free fonts don't follow this rule, so we don't call OTS_FAILURE |
| 136 // in order to support these fonts. | 137 // in order to support these fonts. |
| 137 DROP_THIS_TABLE; | 138 DROP_THIS_TABLE("Kerning pairs are not sorted."); |
| 138 return true; | 139 return true; |
| 139 } | 140 } |
| 140 last_pair = current_pair; | 141 last_pair = current_pair; |
| 141 subtable.pairs.push_back(kerning_pair); | 142 subtable.pairs.push_back(kerning_pair); |
| 142 } | 143 } |
| 143 | 144 |
| 144 kern->subtables.push_back(subtable); | 145 kern->subtables.push_back(subtable); |
| 145 } | 146 } |
| 146 | 147 |
| 147 if (!kern->subtables.size()) { | 148 if (!kern->subtables.size()) { |
| 148 OTS_WARNING("All subtables are removed."); | 149 DROP_THIS_TABLE("All subtables are removed."); |
| 149 DROP_THIS_TABLE; | |
| 150 return true; | 150 return true; |
| 151 } | 151 } |
| 152 | 152 |
| 153 return true; | 153 return true; |
| 154 } | 154 } |
| 155 | 155 |
| 156 bool ots_kern_should_serialise(OpenTypeFile *file) { | 156 bool ots_kern_should_serialise(OpenTypeFile *file) { |
| 157 if (!file->glyf) return false; // this table is not for CFF fonts. | 157 if (!file->glyf) return false; // this table is not for CFF fonts. |
| 158 return file->kern != NULL; | 158 return file->kern != NULL; |
| 159 } | 159 } |
| 160 | 160 |
| 161 bool ots_kern_serialise(OTSStream *out, OpenTypeFile *file) { | 161 bool ots_kern_serialise(OTSStream *out, OpenTypeFile *file) { |
| 162 const OpenTypeKERN *kern = file->kern; | 162 const OpenTypeKERN *kern = file->kern; |
| 163 | 163 |
| 164 const uint16_t num_subtables = static_cast<uint16_t>(kern->subtables.size()); | 164 const uint16_t num_subtables = static_cast<uint16_t>(kern->subtables.size()); |
| 165 if (num_subtables != kern->subtables.size() || | 165 if (num_subtables != kern->subtables.size() || |
| 166 !out->WriteU16(kern->version) || | 166 !out->WriteU16(kern->version) || |
| 167 !out->WriteU16(num_subtables)) { | 167 !out->WriteU16(num_subtables)) { |
| 168 return OTS_FAILURE(); | 168 return OTS_FAILURE_MSG("Can't write kern table header"); |
| 169 } | 169 } |
| 170 | 170 |
| 171 for (uint16_t i = 0; i < num_subtables; ++i) { | 171 for (uint16_t i = 0; i < num_subtables; ++i) { |
| 172 const size_t length = 14 + (6 * kern->subtables[i].pairs.size()); | 172 const size_t length = 14 + (6 * kern->subtables[i].pairs.size()); |
| 173 if (length > std::numeric_limits<uint16_t>::max() || | 173 if (length > std::numeric_limits<uint16_t>::max() || |
| 174 !out->WriteU16(kern->subtables[i].version) || | 174 !out->WriteU16(kern->subtables[i].version) || |
| 175 !out->WriteU16(static_cast<uint16_t>(length)) || | 175 !out->WriteU16(static_cast<uint16_t>(length)) || |
| 176 !out->WriteU16(kern->subtables[i].coverage) || | 176 !out->WriteU16(kern->subtables[i].coverage) || |
| 177 !out->WriteU16( | 177 !out->WriteU16( |
| 178 static_cast<uint16_t>(kern->subtables[i].pairs.size())) || | 178 static_cast<uint16_t>(kern->subtables[i].pairs.size())) || |
| 179 !out->WriteU16(kern->subtables[i].search_range) || | 179 !out->WriteU16(kern->subtables[i].search_range) || |
| 180 !out->WriteU16(kern->subtables[i].entry_selector) || | 180 !out->WriteU16(kern->subtables[i].entry_selector) || |
| 181 !out->WriteU16(kern->subtables[i].range_shift)) { | 181 !out->WriteU16(kern->subtables[i].range_shift)) { |
| 182 return OTS_FAILURE(); | 182 return OTS_FAILURE_MSG("Failed to write kern subtable %d", i); |
| 183 } | 183 } |
| 184 for (unsigned j = 0; j < kern->subtables[i].pairs.size(); ++j) { | 184 for (unsigned j = 0; j < kern->subtables[i].pairs.size(); ++j) { |
| 185 if (!out->WriteU16(kern->subtables[i].pairs[j].left) || | 185 if (!out->WriteU16(kern->subtables[i].pairs[j].left) || |
| 186 !out->WriteU16(kern->subtables[i].pairs[j].right) || | 186 !out->WriteU16(kern->subtables[i].pairs[j].right) || |
| 187 !out->WriteS16(kern->subtables[i].pairs[j].value)) { | 187 !out->WriteS16(kern->subtables[i].pairs[j].value)) { |
| 188 return OTS_FAILURE(); | 188 return OTS_FAILURE_MSG("Failed to write kern pair %d for subtable %d", j
, i); |
| 189 } | 189 } |
| 190 } | 190 } |
| 191 } | 191 } |
| 192 | 192 |
| 193 return true; | 193 return true; |
| 194 } | 194 } |
| 195 | 195 |
| 196 void ots_kern_free(OpenTypeFile *file) { | 196 void ots_kern_free(OpenTypeFile *file) { |
| 197 delete file->kern; | 197 delete file->kern; |
| 198 } | 198 } |
| 199 | 199 |
| 200 } // namespace ots | 200 } // namespace ots |
| 201 |
| 202 #undef TABLE_NAME |
| 203 #undef DROP_THIS_TABLE |
| OLD | NEW |