| 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/typography/otspec/kern.htm | 8 // http://www.microsoft.com/typography/otspec/kern.htm |
| 9 | 9 |
| 10 #define TABLE_NAME "kern" | 10 #define TABLE_NAME "kern" |
| 11 | 11 |
| 12 #define DROP_THIS_TABLE(msg_) \ | 12 #define DROP_THIS_TABLE(msg_) \ |
| 13 do { \ | 13 do { \ |
| 14 OTS_FAILURE_MSG(msg_ ", table discarded"); \ | 14 OTS_FAILURE_MSG(msg_ ", table discarded"); \ |
| 15 delete file->kern; \ | 15 delete font->kern; \ |
| 16 file->kern = 0; \ | 16 font->kern = 0; \ |
| 17 } while (0) | 17 } while (0) |
| 18 | 18 |
| 19 namespace ots { | 19 namespace ots { |
| 20 | 20 |
| 21 bool ots_kern_parse(OpenTypeFile *file, const uint8_t *data, size_t length) { | 21 bool ots_kern_parse(Font *font, const uint8_t *data, size_t length) { |
| 22 Buffer table(data, length); | 22 Buffer table(data, length); |
| 23 | 23 |
| 24 OpenTypeKERN *kern = new OpenTypeKERN; | 24 OpenTypeKERN *kern = new OpenTypeKERN; |
| 25 file->kern = kern; | 25 font->kern = kern; |
| 26 | 26 |
| 27 uint16_t num_tables = 0; | 27 uint16_t num_tables = 0; |
| 28 if (!table.ReadU16(&kern->version) || | 28 if (!table.ReadU16(&kern->version) || |
| 29 !table.ReadU16(&num_tables)) { | 29 !table.ReadU16(&num_tables)) { |
| 30 return OTS_FAILURE_MSG("Failed to read kern header"); | 30 return OTS_FAILURE_MSG("Failed to read kern header"); |
| 31 } | 31 } |
| 32 | 32 |
| 33 if (kern->version > 0) { | 33 if (kern->version > 0) { |
| 34 DROP_THIS_TABLE("bad table version"); | 34 DROP_THIS_TABLE("bad table version"); |
| 35 return true; | 35 return true; |
| (...skipping 27 matching lines...) Expand all Loading... |
| 63 if (!table.ReadU16(&subtable.coverage)) { | 63 if (!table.ReadU16(&subtable.coverage)) { |
| 64 return OTS_FAILURE_MSG("Cailed to read kern subtable %d coverage", i); | 64 return OTS_FAILURE_MSG("Cailed to read kern subtable %d coverage", i); |
| 65 } | 65 } |
| 66 | 66 |
| 67 if (!(subtable.coverage & 0x1)) { | 67 if (!(subtable.coverage & 0x1)) { |
| 68 OTS_WARNING( | 68 OTS_WARNING( |
| 69 "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."); |
| 70 continue; | 70 continue; |
| 71 } | 71 } |
| 72 if (subtable.coverage & 0xF0) { | 72 if (subtable.coverage & 0xF0) { |
| 73 DROP_THIS_TABLE("Reserved fields should zero-filled."); | 73 DROP_THIS_TABLE("Reserved fields should zero-filled"); |
| 74 return true; | 74 return true; |
| 75 } | 75 } |
| 76 const uint32_t format = (subtable.coverage & 0xFF00) >> 8; | 76 const uint32_t format = (subtable.coverage & 0xFF00) >> 8; |
| 77 if (format != 0) { | 77 if (format != 0) { |
| 78 OTS_WARNING("Format %d is not supported.", format); | 78 OTS_WARNING("Format %d is not supported.", format); |
| 79 continue; | 79 continue; |
| 80 } | 80 } |
| 81 | 81 |
| 82 // Parse the format 0 field. | 82 // Parse the format 0 field. |
| 83 uint16_t num_pairs = 0; | 83 uint16_t num_pairs = 0; |
| 84 if (!table.ReadU16(&num_pairs) || | 84 if (!table.ReadU16(&num_pairs) || |
| 85 !table.ReadU16(&subtable.search_range) || | 85 !table.ReadU16(&subtable.search_range) || |
| 86 !table.ReadU16(&subtable.entry_selector) || | 86 !table.ReadU16(&subtable.entry_selector) || |
| 87 !table.ReadU16(&subtable.range_shift)) { | 87 !table.ReadU16(&subtable.range_shift)) { |
| 88 return OTS_FAILURE_MSG("Failed to read kern subtable %d format 0 fields",
i); | 88 return OTS_FAILURE_MSG("Failed to read kern subtable %d format 0 fields",
i); |
| 89 } | 89 } |
| 90 | 90 |
| 91 if (!num_pairs) { | 91 if (!num_pairs) { |
| 92 DROP_THIS_TABLE("Zero length subtable is found."); | 92 DROP_THIS_TABLE("Zero length subtable is found"); |
| 93 return true; | 93 return true; |
| 94 } | 94 } |
| 95 | 95 |
| 96 // 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 |
| 97 // comment in ots.cc for details. | 97 // comment in ots.cc for details. |
| 98 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. |
| 99 if (num_pairs > (65536 / kFormat0PairSize)) { | 99 if (num_pairs > (65536 / kFormat0PairSize)) { |
| 100 // 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. |
| 101 DROP_THIS_TABLE("Too large subtable."); | 101 DROP_THIS_TABLE("Too large subtable"); |
| 102 return true; | 102 return true; |
| 103 } | 103 } |
| 104 unsigned max_pow2 = 0; | 104 unsigned max_pow2 = 0; |
| 105 while (1u << (max_pow2 + 1) <= num_pairs) { | 105 while (1u << (max_pow2 + 1) <= num_pairs) { |
| 106 ++max_pow2; | 106 ++max_pow2; |
| 107 } | 107 } |
| 108 const uint16_t expected_search_range = (1u << max_pow2) * kFormat0PairSize; | 108 const uint16_t expected_search_range = (1u << max_pow2) * kFormat0PairSize; |
| 109 if (subtable.search_range != expected_search_range) { | 109 if (subtable.search_range != expected_search_range) { |
| 110 OTS_WARNING("bad search range"); | 110 OTS_WARNING("bad search range"); |
| 111 subtable.search_range = expected_search_range; | 111 subtable.search_range = expected_search_range; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 128 if (!table.ReadU16(&kerning_pair.left) || | 128 if (!table.ReadU16(&kerning_pair.left) || |
| 129 !table.ReadU16(&kerning_pair.right) || | 129 !table.ReadU16(&kerning_pair.right) || |
| 130 !table.ReadS16(&kerning_pair.value)) { | 130 !table.ReadS16(&kerning_pair.value)) { |
| 131 return OTS_FAILURE_MSG("Failed to read subtable %d kerning pair %d", i,
j); | 131 return OTS_FAILURE_MSG("Failed to read subtable %d kerning pair %d", i,
j); |
| 132 } | 132 } |
| 133 const uint32_t current_pair | 133 const uint32_t current_pair |
| 134 = (kerning_pair.left << 16) + kerning_pair.right; | 134 = (kerning_pair.left << 16) + kerning_pair.right; |
| 135 if (j != 0 && current_pair <= last_pair) { | 135 if (j != 0 && current_pair <= last_pair) { |
| 136 // 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 |
| 137 // in order to support these fonts. | 137 // in order to support these fonts. |
| 138 DROP_THIS_TABLE("Kerning pairs are not sorted."); | 138 DROP_THIS_TABLE("Kerning pairs are not sorted"); |
| 139 return true; | 139 return true; |
| 140 } | 140 } |
| 141 last_pair = current_pair; | 141 last_pair = current_pair; |
| 142 subtable.pairs.push_back(kerning_pair); | 142 subtable.pairs.push_back(kerning_pair); |
| 143 } | 143 } |
| 144 | 144 |
| 145 kern->subtables.push_back(subtable); | 145 kern->subtables.push_back(subtable); |
| 146 } | 146 } |
| 147 | 147 |
| 148 if (!kern->subtables.size()) { | 148 if (!kern->subtables.size()) { |
| 149 DROP_THIS_TABLE("All subtables are removed."); | 149 DROP_THIS_TABLE("All subtables are removed"); |
| 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(Font *font) { |
| 157 if (!file->glyf) return false; // this table is not for CFF fonts. | 157 if (!font->glyf) return false; // this table is not for CFF fonts. |
| 158 return file->kern != NULL; | 158 return font->kern != NULL; |
| 159 } | 159 } |
| 160 | 160 |
| 161 bool ots_kern_serialise(OTSStream *out, OpenTypeFile *file) { | 161 bool ots_kern_serialise(OTSStream *out, Font *font) { |
| 162 const OpenTypeKERN *kern = file->kern; | 162 const OpenTypeKERN *kern = font->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_MSG("Can't write kern table header"); | 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()); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 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_MSG("Failed to write kern pair %d for subtable %d", j
, i); | 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_reuse(Font *font, Font *other) { |
| 197 delete file->kern; | 197 font->kern = other->kern; |
| 198 font->kern_reused = true; |
| 199 } |
| 200 |
| 201 void ots_kern_free(Font *font) { |
| 202 delete font->kern; |
| 198 } | 203 } |
| 199 | 204 |
| 200 } // namespace ots | 205 } // namespace ots |
| 201 | 206 |
| 202 #undef TABLE_NAME | 207 #undef TABLE_NAME |
| 203 #undef DROP_THIS_TABLE | 208 #undef DROP_THIS_TABLE |
| OLD | NEW |