OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "gpos.h" | 5 #include "gpos.h" |
6 | 6 |
7 #include <limits> | 7 #include <limits> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "gdef.h" | |
11 #include "gsub.h" | |
12 #include "layout.h" | 10 #include "layout.h" |
13 #include "maxp.h" | 11 #include "maxp.h" |
14 | 12 |
15 // GPOS - The Glyph Positioning Table | 13 // GPOS - The Glyph Positioning Table |
16 // http://www.microsoft.com/typography/otspec/gpos.htm | 14 // http://www.microsoft.com/typography/otspec/gpos.htm |
17 | 15 |
| 16 #define TABLE_NAME "GPOS" |
| 17 |
18 namespace { | 18 namespace { |
19 | 19 |
20 enum GPOS_TYPE { | 20 enum GPOS_TYPE { |
21 GPOS_TYPE_SINGLE_ADJUSTMENT = 1, | 21 GPOS_TYPE_SINGLE_ADJUSTMENT = 1, |
22 GPOS_TYPE_PAIR_ADJUSTMENT = 2, | 22 GPOS_TYPE_PAIR_ADJUSTMENT = 2, |
23 GPOS_TYPE_CURSIVE_ATTACHMENT = 3, | 23 GPOS_TYPE_CURSIVE_ATTACHMENT = 3, |
24 GPOS_TYPE_MARK_TO_BASE_ATTACHMENT = 4, | 24 GPOS_TYPE_MARK_TO_BASE_ATTACHMENT = 4, |
25 GPOS_TYPE_MARK_TO_LIGATURE_ATTACHMENT = 5, | 25 GPOS_TYPE_MARK_TO_LIGATURE_ATTACHMENT = 5, |
26 GPOS_TYPE_MARK_TO_MARK_ATTACHMENT = 6, | 26 GPOS_TYPE_MARK_TO_MARK_ATTACHMENT = 6, |
27 GPOS_TYPE_CONTEXT_POSITIONING = 7, | 27 GPOS_TYPE_CONTEXT_POSITIONING = 7, |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
69 {GPOS_TYPE_EXTENSION_POSITIONING, ParseExtensionPositioning} | 69 {GPOS_TYPE_EXTENSION_POSITIONING, ParseExtensionPositioning} |
70 }; | 70 }; |
71 | 71 |
72 const ots::LookupSubtableParser kGposLookupSubtableParser = { | 72 const ots::LookupSubtableParser kGposLookupSubtableParser = { |
73 arraysize(kGposTypeParsers), | 73 arraysize(kGposTypeParsers), |
74 GPOS_TYPE_EXTENSION_POSITIONING, kGposTypeParsers | 74 GPOS_TYPE_EXTENSION_POSITIONING, kGposTypeParsers |
75 }; | 75 }; |
76 | 76 |
77 // Shared Tables: ValueRecord, Anchor Table, and MarkArray | 77 // Shared Tables: ValueRecord, Anchor Table, and MarkArray |
78 | 78 |
79 bool ParseValueRecord(ots::Buffer* subtable, const uint8_t *data, | 79 bool ParseValueRecord(const ots::OpenTypeFile *file, |
| 80 ots::Buffer* subtable, const uint8_t *data, |
80 const size_t length, const uint16_t value_format) { | 81 const size_t length, const uint16_t value_format) { |
81 // Check existence of adjustment fields. | 82 // Check existence of adjustment fields. |
82 for (unsigned i = 0; i < 4; ++i) { | 83 for (unsigned i = 0; i < 4; ++i) { |
83 if ((value_format >> i) & 0x1) { | 84 if ((value_format >> i) & 0x1) { |
84 // Just read the field since these fileds could take an arbitrary values. | 85 // Just read the field since these fileds could take an arbitrary values. |
85 if (!subtable->Skip(2)) { | 86 if (!subtable->Skip(2)) { |
86 return OTS_FAILURE(); | 87 return OTS_FAILURE_MSG("Failed to read value reacord component"); |
87 } | 88 } |
88 } | 89 } |
89 } | 90 } |
90 | 91 |
91 // Check existence of offsets to device table. | 92 // Check existence of offsets to device table. |
92 for (unsigned i = 0; i < 4; ++i) { | 93 for (unsigned i = 0; i < 4; ++i) { |
93 if ((value_format >> (i + 4)) & 0x1) { | 94 if ((value_format >> (i + 4)) & 0x1) { |
94 uint16_t offset = 0; | 95 uint16_t offset = 0; |
95 if (!subtable->ReadU16(&offset)) { | 96 if (!subtable->ReadU16(&offset)) { |
96 return OTS_FAILURE(); | 97 return OTS_FAILURE_MSG("Failed to read value record offset"); |
97 } | 98 } |
98 if (offset) { | 99 if (offset) { |
99 // TODO(bashi): Is it possible that device tables locate before | 100 // TODO(bashi): Is it possible that device tables locate before |
100 // this record? No fonts contain such offset AKAIF. | 101 // this record? No fonts contain such offset AKAIF. |
101 if (offset >= length) { | 102 if (offset >= length) { |
102 return OTS_FAILURE(); | 103 return OTS_FAILURE_MSG("Value record offset too high %d >= %ld", offse
t, length); |
103 } | 104 } |
104 if (!ots::ParseDeviceTable(data + offset, length - offset)) { | 105 if (!ots::ParseDeviceTable(file, data + offset, length - offset)) { |
105 return OTS_FAILURE(); | 106 return OTS_FAILURE_MSG("Failed to parse device table in value record")
; |
106 } | 107 } |
107 } | 108 } |
108 } | 109 } |
109 } | 110 } |
110 return true; | 111 return true; |
111 } | 112 } |
112 | 113 |
113 bool ParseAnchorTable(const uint8_t *data, const size_t length) { | 114 bool ParseAnchorTable(const ots::OpenTypeFile *file, |
| 115 const uint8_t *data, const size_t length) { |
114 ots::Buffer subtable(data, length); | 116 ots::Buffer subtable(data, length); |
115 | 117 |
116 uint16_t format = 0; | 118 uint16_t format = 0; |
117 // Read format and skip 2 2-byte fields that could be arbitrary values. | 119 // Read format and skip 2 2-byte fields that could be arbitrary values. |
118 if (!subtable.ReadU16(&format) || | 120 if (!subtable.ReadU16(&format) || |
119 !subtable.Skip(4)) { | 121 !subtable.Skip(4)) { |
120 return OTS_FAILURE(); | 122 return OTS_FAILURE_MSG("Faled to read anchor table"); |
121 } | 123 } |
122 | 124 |
123 if (format == 0 || format > kMaxAnchorFormat) { | 125 if (format == 0 || format > kMaxAnchorFormat) { |
124 return OTS_FAILURE(); | 126 return OTS_FAILURE_MSG("Bad Anchor table format %d", format); |
125 } | 127 } |
126 | 128 |
127 // Format 2 and 3 has additional fields. | 129 // Format 2 and 3 has additional fields. |
128 if (format == 2) { | 130 if (format == 2) { |
129 // Format 2 provides an index to a glyph contour point, which will take | 131 // Format 2 provides an index to a glyph contour point, which will take |
130 // arbitrary value. | 132 // arbitrary value. |
131 uint16_t anchor_point = 0; | 133 uint16_t anchor_point = 0; |
132 if (!subtable.ReadU16(&anchor_point)) { | 134 if (!subtable.ReadU16(&anchor_point)) { |
133 return OTS_FAILURE(); | 135 return OTS_FAILURE_MSG("Failed to read anchor point in format 2 Anchor Tab
le"); |
134 } | 136 } |
135 } else if (format == 3) { | 137 } else if (format == 3) { |
136 uint16_t offset_x_device = 0; | 138 uint16_t offset_x_device = 0; |
137 uint16_t offset_y_device = 0; | 139 uint16_t offset_y_device = 0; |
138 if (!subtable.ReadU16(&offset_x_device) || | 140 if (!subtable.ReadU16(&offset_x_device) || |
139 !subtable.ReadU16(&offset_y_device)) { | 141 !subtable.ReadU16(&offset_y_device)) { |
140 return OTS_FAILURE(); | 142 return OTS_FAILURE_MSG("Failed to read device table offsets in format 3 an
chor table"); |
141 } | 143 } |
142 const unsigned format_end = static_cast<unsigned>(10); | 144 const unsigned format_end = static_cast<unsigned>(10); |
143 if (offset_x_device) { | 145 if (offset_x_device) { |
144 if (offset_x_device < format_end || offset_x_device >= length) { | 146 if (offset_x_device < format_end || offset_x_device >= length) { |
145 return OTS_FAILURE(); | 147 return OTS_FAILURE_MSG("Bad x device table offset %d", offset_x_device); |
146 } | 148 } |
147 if (!ots::ParseDeviceTable(data + offset_x_device, | 149 if (!ots::ParseDeviceTable(file, data + offset_x_device, |
148 length - offset_x_device)) { | 150 length - offset_x_device)) { |
149 return OTS_FAILURE(); | 151 return OTS_FAILURE_MSG("Failed to parse device table in anchor table"); |
150 } | 152 } |
151 } | 153 } |
152 if (offset_y_device) { | 154 if (offset_y_device) { |
153 if (offset_y_device < format_end || offset_y_device >= length) { | 155 if (offset_y_device < format_end || offset_y_device >= length) { |
154 return OTS_FAILURE(); | 156 return OTS_FAILURE_MSG("Bad y device table offset %d", offset_y_device); |
155 } | 157 } |
156 if (!ots::ParseDeviceTable(data + offset_y_device, | 158 if (!ots::ParseDeviceTable(file, data + offset_y_device, |
157 length - offset_y_device)) { | 159 length - offset_y_device)) { |
158 return OTS_FAILURE(); | 160 return OTS_FAILURE_MSG("Failed to parse device table in anchor table"); |
159 } | 161 } |
160 } | 162 } |
161 } | 163 } |
162 return true; | 164 return true; |
163 } | 165 } |
164 | 166 |
165 bool ParseMarkArrayTable(const uint8_t *data, const size_t length, | 167 bool ParseMarkArrayTable(const ots::OpenTypeFile *file, |
| 168 const uint8_t *data, const size_t length, |
166 const uint16_t class_count) { | 169 const uint16_t class_count) { |
167 ots::Buffer subtable(data, length); | 170 ots::Buffer subtable(data, length); |
168 | 171 |
169 uint16_t mark_count = 0; | 172 uint16_t mark_count = 0; |
170 if (!subtable.ReadU16(&mark_count)) { | 173 if (!subtable.ReadU16(&mark_count)) { |
171 return OTS_FAILURE(); | 174 return OTS_FAILURE_MSG("Can't read mark table length"); |
172 } | 175 } |
173 | 176 |
174 // MarkRecord consists of 4-bytes. | 177 // MarkRecord consists of 4-bytes. |
175 const unsigned mark_records_end = 4 * static_cast<unsigned>(mark_count) + 2; | 178 const unsigned mark_records_end = 4 * static_cast<unsigned>(mark_count) + 2; |
176 if (mark_records_end > std::numeric_limits<uint16_t>::max()) { | 179 if (mark_records_end > std::numeric_limits<uint16_t>::max()) { |
177 return OTS_FAILURE(); | 180 return OTS_FAILURE_MSG("Bad mark table length"); |
178 } | 181 } |
179 for (unsigned i = 0; i < mark_count; ++i) { | 182 for (unsigned i = 0; i < mark_count; ++i) { |
180 uint16_t class_value = 0; | 183 uint16_t class_value = 0; |
181 uint16_t offset_mark_anchor = 0; | 184 uint16_t offset_mark_anchor = 0; |
182 if (!subtable.ReadU16(&class_value) || | 185 if (!subtable.ReadU16(&class_value) || |
183 !subtable.ReadU16(&offset_mark_anchor)) { | 186 !subtable.ReadU16(&offset_mark_anchor)) { |
184 return OTS_FAILURE(); | 187 return OTS_FAILURE_MSG("Can't read mark table %d", i); |
185 } | 188 } |
186 // |class_value| may take arbitrary values including 0 here so we don't | 189 // |class_value| may take arbitrary values including 0 here so we don't |
187 // check the value. | 190 // check the value. |
188 if (offset_mark_anchor < mark_records_end || | 191 if (offset_mark_anchor < mark_records_end || |
189 offset_mark_anchor >= length) { | 192 offset_mark_anchor >= length) { |
190 return OTS_FAILURE(); | 193 return OTS_FAILURE_MSG("Bad mark anchor offset %d for mark table %d", offs
et_mark_anchor, i); |
191 } | 194 } |
192 if (!ParseAnchorTable(data + offset_mark_anchor, | 195 if (!ParseAnchorTable(file, data + offset_mark_anchor, |
193 length - offset_mark_anchor)) { | 196 length - offset_mark_anchor)) { |
194 return OTS_FAILURE(); | 197 return OTS_FAILURE_MSG("Faled to parse anchor table for mark table %d", i)
; |
195 } | 198 } |
196 } | 199 } |
197 | 200 |
198 return true; | 201 return true; |
199 } | 202 } |
200 | 203 |
201 // Lookup Type 1: | 204 // Lookup Type 1: |
202 // Single Adjustment Positioning Subtable | 205 // Single Adjustment Positioning Subtable |
203 bool ParseSingleAdjustment(const ots::OpenTypeFile *file, const uint8_t *data, | 206 bool ParseSingleAdjustment(const ots::OpenTypeFile *file, const uint8_t *data, |
204 const size_t length) { | 207 const size_t length) { |
205 ots::Buffer subtable(data, length); | 208 ots::Buffer subtable(data, length); |
206 | 209 |
207 uint16_t format = 0; | 210 uint16_t format = 0; |
208 uint16_t offset_coverage = 0; | 211 uint16_t offset_coverage = 0; |
209 uint16_t value_format = 0; | 212 uint16_t value_format = 0; |
210 if (!subtable.ReadU16(&format) || | 213 if (!subtable.ReadU16(&format) || |
211 !subtable.ReadU16(&offset_coverage) || | 214 !subtable.ReadU16(&offset_coverage) || |
212 !subtable.ReadU16(&value_format)) { | 215 !subtable.ReadU16(&value_format)) { |
213 return OTS_FAILURE(); | 216 return OTS_FAILURE_MSG("Can't read single adjustment information"); |
214 } | 217 } |
215 | 218 |
216 if (format == 1) { | 219 if (format == 1) { |
217 // Format 1 exactly one value record. | 220 // Format 1 exactly one value record. |
218 if (!ParseValueRecord(&subtable, data, length, value_format)) { | 221 if (!ParseValueRecord(file, &subtable, data, length, value_format)) { |
219 return OTS_FAILURE(); | 222 return OTS_FAILURE_MSG("Failed to parse format 1 single adjustment table")
; |
220 } | 223 } |
221 } else if (format == 2) { | 224 } else if (format == 2) { |
222 uint16_t value_count = 0; | 225 uint16_t value_count = 0; |
223 if (!subtable.ReadU16(&value_count)) { | 226 if (!subtable.ReadU16(&value_count)) { |
224 return OTS_FAILURE(); | 227 return OTS_FAILURE_MSG("Failed to parse format 2 single adjustment table")
; |
225 } | 228 } |
226 for (unsigned i = 0; i < value_count; ++i) { | 229 for (unsigned i = 0; i < value_count; ++i) { |
227 if (!ParseValueRecord(&subtable, data, length, value_format)) { | 230 if (!ParseValueRecord(file, &subtable, data, length, value_format)) { |
228 return OTS_FAILURE(); | 231 return OTS_FAILURE_MSG("Failed to parse value record %d in format 2 sing
le adjustment table", i); |
229 } | 232 } |
230 } | 233 } |
231 } else { | 234 } else { |
232 return OTS_FAILURE(); | 235 return OTS_FAILURE_MSG("Bad format %d in single adjustment table", format); |
233 } | 236 } |
234 | 237 |
235 if (offset_coverage < subtable.offset() || offset_coverage >= length) { | 238 if (offset_coverage < subtable.offset() || offset_coverage >= length) { |
236 return OTS_FAILURE(); | 239 return OTS_FAILURE_MSG("Bad coverage offset %d in single adjustment table",
offset_coverage); |
237 } | 240 } |
238 | 241 |
239 if (!ots::ParseCoverageTable(data + offset_coverage, | 242 if (!ots::ParseCoverageTable(file, data + offset_coverage, |
240 length - offset_coverage, | 243 length - offset_coverage, |
241 file->maxp->num_glyphs)) { | 244 file->maxp->num_glyphs)) { |
242 return OTS_FAILURE(); | 245 return OTS_FAILURE_MSG("Failed to parse coverage table in single adjustment
table"); |
243 } | 246 } |
244 | 247 |
245 return true; | 248 return true; |
246 } | 249 } |
247 | 250 |
248 bool ParsePairSetTable(const uint8_t *data, const size_t length, | 251 bool ParsePairSetTable(const ots::OpenTypeFile *file, |
| 252 const uint8_t *data, const size_t length, |
249 const uint16_t value_format1, | 253 const uint16_t value_format1, |
250 const uint16_t value_format2, | 254 const uint16_t value_format2, |
251 const uint16_t num_glyphs) { | 255 const uint16_t num_glyphs) { |
252 ots::Buffer subtable(data, length); | 256 ots::Buffer subtable(data, length); |
253 | 257 |
254 uint16_t value_count = 0; | 258 uint16_t value_count = 0; |
255 if (!subtable.ReadU16(&value_count)) { | 259 if (!subtable.ReadU16(&value_count)) { |
256 return OTS_FAILURE(); | 260 return OTS_FAILURE_MSG("Failed to read pair set table structure"); |
257 } | 261 } |
258 for (unsigned i = 0; i < value_count; ++i) { | 262 for (unsigned i = 0; i < value_count; ++i) { |
259 // Check pair value record. | 263 // Check pair value record. |
260 uint16_t glyph_id = 0; | 264 uint16_t glyph_id = 0; |
261 if (!subtable.ReadU16(&glyph_id)) { | 265 if (!subtable.ReadU16(&glyph_id)) { |
262 return OTS_FAILURE(); | 266 return OTS_FAILURE_MSG("Failed to read glyph in pair value record %d", i); |
263 } | 267 } |
264 if (glyph_id >= num_glyphs) { | 268 if (glyph_id >= num_glyphs) { |
265 return OTS_FAILURE(); | 269 return OTS_FAILURE_MSG("glyph id %d too high >= %d", glyph_id, num_glyphs)
; |
266 } | 270 } |
267 if (!ParseValueRecord(&subtable, data, length, value_format1)) { | 271 if (!ParseValueRecord(file, &subtable, data, length, value_format1)) { |
268 return OTS_FAILURE(); | 272 return OTS_FAILURE_MSG("Failed to parse value record in format 1 pair set
table"); |
269 } | 273 } |
270 if (!ParseValueRecord(&subtable, data, length, value_format2)) { | 274 if (!ParseValueRecord(file, &subtable, data, length, value_format2)) { |
271 return OTS_FAILURE(); | 275 return OTS_FAILURE_MSG("Failed to parse value record in format 2 pair set
table"); |
272 } | 276 } |
273 } | 277 } |
274 return true; | 278 return true; |
275 } | 279 } |
276 | 280 |
277 bool ParsePairPosFormat1(const uint8_t *data, const size_t length, | 281 bool ParsePairPosFormat1(const ots::OpenTypeFile *file, |
| 282 const uint8_t *data, const size_t length, |
278 const uint16_t value_format1, | 283 const uint16_t value_format1, |
279 const uint16_t value_format2, | 284 const uint16_t value_format2, |
280 const uint16_t num_glyphs) { | 285 const uint16_t num_glyphs) { |
281 ots::Buffer subtable(data, length); | 286 ots::Buffer subtable(data, length); |
282 | 287 |
283 // Skip 8 bytes that are already read before. | 288 // Skip 8 bytes that are already read before. |
284 if (!subtable.Skip(8)) { | 289 if (!subtable.Skip(8)) { |
285 return OTS_FAILURE(); | 290 return OTS_FAILURE_MSG("Failed to read pair pos table structure"); |
286 } | 291 } |
287 | 292 |
288 uint16_t pair_set_count = 0; | 293 uint16_t pair_set_count = 0; |
289 if (!subtable.ReadU16(&pair_set_count)) { | 294 if (!subtable.ReadU16(&pair_set_count)) { |
290 return OTS_FAILURE(); | 295 return OTS_FAILURE_MSG("Failed to read pair pos set count"); |
291 } | 296 } |
292 | 297 |
293 const unsigned pair_pos_end = 2 * static_cast<unsigned>(pair_set_count) + 10; | 298 const unsigned pair_pos_end = 2 * static_cast<unsigned>(pair_set_count) + 10; |
294 if (pair_pos_end > std::numeric_limits<uint16_t>::max()) { | 299 if (pair_pos_end > std::numeric_limits<uint16_t>::max()) { |
295 return OTS_FAILURE(); | 300 return OTS_FAILURE_MSG("Bad pair set length %d", pair_pos_end); |
296 } | 301 } |
297 for (unsigned i = 0; i < pair_set_count; ++i) { | 302 for (unsigned i = 0; i < pair_set_count; ++i) { |
298 uint16_t pair_set_offset = 0; | 303 uint16_t pair_set_offset = 0; |
299 if (!subtable.ReadU16(&pair_set_offset)) { | 304 if (!subtable.ReadU16(&pair_set_offset)) { |
300 return OTS_FAILURE(); | 305 return OTS_FAILURE_MSG("Failed to read pair set offset for pair set %d", i
); |
301 } | 306 } |
302 if (pair_set_offset < pair_pos_end || pair_set_offset >= length) { | 307 if (pair_set_offset < pair_pos_end || pair_set_offset >= length) { |
303 return OTS_FAILURE(); | 308 return OTS_FAILURE_MSG("Bad pair set offset %d for pair set %d", pair_set_
offset, i); |
304 } | 309 } |
305 // Check pair set tables | 310 // Check pair set tables |
306 if (!ParsePairSetTable(data + pair_set_offset, length - pair_set_offset, | 311 if (!ParsePairSetTable(file, data + pair_set_offset, length - pair_set_offse
t, |
307 value_format1, value_format2, | 312 value_format1, value_format2, |
308 num_glyphs)) { | 313 num_glyphs)) { |
309 return OTS_FAILURE(); | 314 return OTS_FAILURE_MSG("Failed to parse pair set table %d", i); |
310 } | 315 } |
311 } | 316 } |
312 | 317 |
313 return true; | 318 return true; |
314 } | 319 } |
315 | 320 |
316 bool ParsePairPosFormat2(const uint8_t *data, const size_t length, | 321 bool ParsePairPosFormat2(const ots::OpenTypeFile *file, |
| 322 const uint8_t *data, const size_t length, |
317 const uint16_t value_format1, | 323 const uint16_t value_format1, |
318 const uint16_t value_format2, | 324 const uint16_t value_format2, |
319 const uint16_t num_glyphs) { | 325 const uint16_t num_glyphs) { |
320 ots::Buffer subtable(data, length); | 326 ots::Buffer subtable(data, length); |
321 | 327 |
322 // Skip 8 bytes that are already read before. | 328 // Skip 8 bytes that are already read before. |
323 if (!subtable.Skip(8)) { | 329 if (!subtable.Skip(8)) { |
324 return OTS_FAILURE(); | 330 return OTS_FAILURE_MSG("Failed to read pair pos format 2 structure"); |
325 } | 331 } |
326 | 332 |
327 uint16_t offset_class_def1 = 0; | 333 uint16_t offset_class_def1 = 0; |
328 uint16_t offset_class_def2 = 0; | 334 uint16_t offset_class_def2 = 0; |
329 uint16_t class1_count = 0; | 335 uint16_t class1_count = 0; |
330 uint16_t class2_count = 0; | 336 uint16_t class2_count = 0; |
331 if (!subtable.ReadU16(&offset_class_def1) || | 337 if (!subtable.ReadU16(&offset_class_def1) || |
332 !subtable.ReadU16(&offset_class_def2) || | 338 !subtable.ReadU16(&offset_class_def2) || |
333 !subtable.ReadU16(&class1_count) || | 339 !subtable.ReadU16(&class1_count) || |
334 !subtable.ReadU16(&class2_count)) { | 340 !subtable.ReadU16(&class2_count)) { |
335 return OTS_FAILURE(); | 341 return OTS_FAILURE_MSG("Failed to read pair pos format 2 data"); |
336 } | 342 } |
337 | 343 |
338 // Check class 1 records. | 344 // Check class 1 records. |
339 for (unsigned i = 0; i < class1_count; ++i) { | 345 for (unsigned i = 0; i < class1_count; ++i) { |
340 // Check class 2 records. | 346 // Check class 2 records. |
341 for (unsigned j = 0; j < class2_count; ++j) { | 347 for (unsigned j = 0; j < class2_count; ++j) { |
342 if (value_format1 && !ParseValueRecord(&subtable, data, length, | 348 if (value_format1 && !ParseValueRecord(file, &subtable, data, length, |
343 value_format1)) { | 349 value_format1)) { |
344 return OTS_FAILURE(); | 350 return OTS_FAILURE_MSG("Failed to parse value record 1 %d and %d", j, i)
; |
345 } | 351 } |
346 if (value_format2 && !ParseValueRecord(&subtable, data, length, | 352 if (value_format2 && !ParseValueRecord(file, &subtable, data, length, |
347 value_format2)) { | 353 value_format2)) { |
348 return OTS_FAILURE(); | 354 return OTS_FAILURE_MSG("Falied to parse value record 2 %d and %d", j, i)
; |
349 } | 355 } |
350 } | 356 } |
351 } | 357 } |
352 | 358 |
353 // Check class definition tables. | 359 // Check class definition tables. |
354 if (offset_class_def1 < subtable.offset() || offset_class_def1 >= length || | 360 if (offset_class_def1 < subtable.offset() || offset_class_def1 >= length || |
355 offset_class_def2 < subtable.offset() || offset_class_def2 >= length) { | 361 offset_class_def2 < subtable.offset() || offset_class_def2 >= length) { |
356 return OTS_FAILURE(); | 362 return OTS_FAILURE_MSG("Bad class definition table offsets %d or %d", offset
_class_def1, offset_class_def2); |
357 } | 363 } |
358 if (!ots::ParseClassDefTable(data + offset_class_def1, | 364 if (!ots::ParseClassDefTable(file, data + offset_class_def1, |
359 length - offset_class_def1, | 365 length - offset_class_def1, |
360 num_glyphs, kMaxClassDefValue)) { | 366 num_glyphs, kMaxClassDefValue)) { |
361 return OTS_FAILURE(); | 367 return OTS_FAILURE_MSG("Failed to parse class definition table 1"); |
362 } | 368 } |
363 if (!ots::ParseClassDefTable(data + offset_class_def2, | 369 if (!ots::ParseClassDefTable(file, data + offset_class_def2, |
364 length - offset_class_def2, | 370 length - offset_class_def2, |
365 num_glyphs, kMaxClassDefValue)) { | 371 num_glyphs, kMaxClassDefValue)) { |
366 return OTS_FAILURE(); | 372 return OTS_FAILURE_MSG("Failed to parse class definition table 2"); |
367 } | 373 } |
368 | 374 |
369 return true; | 375 return true; |
370 } | 376 } |
371 | 377 |
372 // Lookup Type 2: | 378 // Lookup Type 2: |
373 // Pair Adjustment Positioning Subtable | 379 // Pair Adjustment Positioning Subtable |
374 bool ParsePairAdjustment(const ots::OpenTypeFile *file, const uint8_t *data, | 380 bool ParsePairAdjustment(const ots::OpenTypeFile *file, const uint8_t *data, |
375 const size_t length) { | 381 const size_t length) { |
376 ots::Buffer subtable(data, length); | 382 ots::Buffer subtable(data, length); |
377 | 383 |
378 uint16_t format = 0; | 384 uint16_t format = 0; |
379 uint16_t offset_coverage = 0; | 385 uint16_t offset_coverage = 0; |
380 uint16_t value_format1 = 0; | 386 uint16_t value_format1 = 0; |
381 uint16_t value_format2 = 0; | 387 uint16_t value_format2 = 0; |
382 if (!subtable.ReadU16(&format) || | 388 if (!subtable.ReadU16(&format) || |
383 !subtable.ReadU16(&offset_coverage) || | 389 !subtable.ReadU16(&offset_coverage) || |
384 !subtable.ReadU16(&value_format1) || | 390 !subtable.ReadU16(&value_format1) || |
385 !subtable.ReadU16(&value_format2)) { | 391 !subtable.ReadU16(&value_format2)) { |
386 return OTS_FAILURE(); | 392 return OTS_FAILURE_MSG("Failed to read pair adjustment structure"); |
387 } | 393 } |
388 | 394 |
389 if (format == 1) { | 395 if (format == 1) { |
390 if (!ParsePairPosFormat1(data, length, value_format1, value_format2, | 396 if (!ParsePairPosFormat1(file, data, length, value_format1, value_format2, |
391 file->maxp->num_glyphs)) { | 397 file->maxp->num_glyphs)) { |
392 return OTS_FAILURE(); | 398 return OTS_FAILURE_MSG("Failed to parse pair pos format 1"); |
393 } | 399 } |
394 } else if (format == 2) { | 400 } else if (format == 2) { |
395 if (!ParsePairPosFormat2(data, length, value_format1, value_format2, | 401 if (!ParsePairPosFormat2(file, data, length, value_format1, value_format2, |
396 file->maxp->num_glyphs)) { | 402 file->maxp->num_glyphs)) { |
397 return OTS_FAILURE(); | 403 return OTS_FAILURE_MSG("Failed to parse pair format 2"); |
398 } | 404 } |
399 } else { | 405 } else { |
400 return OTS_FAILURE(); | 406 return OTS_FAILURE_MSG("Bad pos pair format %d", format); |
401 } | 407 } |
402 | 408 |
403 if (offset_coverage < subtable.offset() || offset_coverage >= length) { | 409 if (offset_coverage < subtable.offset() || offset_coverage >= length) { |
404 return OTS_FAILURE(); | 410 return OTS_FAILURE_MSG("Bad pair pos offset coverage %d", offset_coverage); |
405 } | 411 } |
406 if (!ots::ParseCoverageTable(data + offset_coverage, | 412 if (!ots::ParseCoverageTable(file, data + offset_coverage, |
407 length - offset_coverage, | 413 length - offset_coverage, |
408 file->maxp->num_glyphs)) { | 414 file->maxp->num_glyphs)) { |
409 return OTS_FAILURE(); | 415 return OTS_FAILURE_MSG("Failed to parse coverage table"); |
410 } | 416 } |
411 | 417 |
412 return true; | 418 return true; |
413 } | 419 } |
414 | 420 |
415 // Lookup Type 3 | 421 // Lookup Type 3 |
416 // Cursive Attachment Positioning Subtable | 422 // Cursive Attachment Positioning Subtable |
417 bool ParseCursiveAttachment(const ots::OpenTypeFile *file, const uint8_t *data, | 423 bool ParseCursiveAttachment(const ots::OpenTypeFile *file, const uint8_t *data, |
418 const size_t length) { | 424 const size_t length) { |
419 ots::Buffer subtable(data, length); | 425 ots::Buffer subtable(data, length); |
420 | 426 |
421 uint16_t format = 0; | 427 uint16_t format = 0; |
422 uint16_t offset_coverage = 0; | 428 uint16_t offset_coverage = 0; |
423 uint16_t entry_exit_count = 0; | 429 uint16_t entry_exit_count = 0; |
424 if (!subtable.ReadU16(&format) || | 430 if (!subtable.ReadU16(&format) || |
425 !subtable.ReadU16(&offset_coverage) || | 431 !subtable.ReadU16(&offset_coverage) || |
426 !subtable.ReadU16(&entry_exit_count)) { | 432 !subtable.ReadU16(&entry_exit_count)) { |
427 return OTS_FAILURE(); | 433 return OTS_FAILURE_MSG("Failed to read cursive attachment structure"); |
428 } | 434 } |
429 | 435 |
430 if (format != 1) { | 436 if (format != 1) { |
431 return OTS_FAILURE(); | 437 return OTS_FAILURE_MSG("Bad cursive attachment format %d", format); |
432 } | 438 } |
433 | 439 |
434 // Check entry exit records. | 440 // Check entry exit records. |
435 const unsigned entry_exit_records_end = | 441 const unsigned entry_exit_records_end = |
436 2 * static_cast<unsigned>(entry_exit_count) + 6; | 442 2 * static_cast<unsigned>(entry_exit_count) + 6; |
437 if (entry_exit_records_end > std::numeric_limits<uint16_t>::max()) { | 443 if (entry_exit_records_end > std::numeric_limits<uint16_t>::max()) { |
438 return OTS_FAILURE(); | 444 return OTS_FAILURE_MSG("Bad entry exit record end %d", entry_exit_records_en
d); |
439 } | 445 } |
440 for (unsigned i = 0; i < entry_exit_count; ++i) { | 446 for (unsigned i = 0; i < entry_exit_count; ++i) { |
441 uint16_t offset_entry_anchor = 0; | 447 uint16_t offset_entry_anchor = 0; |
442 uint16_t offset_exit_anchor = 0; | 448 uint16_t offset_exit_anchor = 0; |
443 if (!subtable.ReadU16(&offset_entry_anchor) || | 449 if (!subtable.ReadU16(&offset_entry_anchor) || |
444 !subtable.ReadU16(&offset_exit_anchor)) { | 450 !subtable.ReadU16(&offset_exit_anchor)) { |
445 return OTS_FAILURE(); | 451 return OTS_FAILURE_MSG("Can't read entry exit record %d", i); |
446 } | 452 } |
447 // These offsets could be NULL. | 453 // These offsets could be NULL. |
448 if (offset_entry_anchor) { | 454 if (offset_entry_anchor) { |
449 if (offset_entry_anchor < entry_exit_records_end || | 455 if (offset_entry_anchor < entry_exit_records_end || |
450 offset_entry_anchor >= length) { | 456 offset_entry_anchor >= length) { |
451 return OTS_FAILURE(); | 457 return OTS_FAILURE_MSG("Bad entry anchor offset %d in entry exit record
%d", offset_entry_anchor, i); |
452 } | 458 } |
453 if (!ParseAnchorTable(data + offset_entry_anchor, | 459 if (!ParseAnchorTable(file, data + offset_entry_anchor, |
454 length - offset_entry_anchor)) { | 460 length - offset_entry_anchor)) { |
455 return OTS_FAILURE(); | 461 return OTS_FAILURE_MSG("Failed to parse entry anchor table in entry exit
record %d", i); |
456 } | 462 } |
457 } | 463 } |
458 if (offset_exit_anchor) { | 464 if (offset_exit_anchor) { |
459 if (offset_exit_anchor < entry_exit_records_end || | 465 if (offset_exit_anchor < entry_exit_records_end || |
460 offset_exit_anchor >= length) { | 466 offset_exit_anchor >= length) { |
461 return OTS_FAILURE(); | 467 return OTS_FAILURE_MSG("Bad exit anchor offset %d in entry exit record %
d", offset_exit_anchor, i); |
462 } | 468 } |
463 if (!ParseAnchorTable(data + offset_exit_anchor, | 469 if (!ParseAnchorTable(file, data + offset_exit_anchor, |
464 length - offset_exit_anchor)) { | 470 length - offset_exit_anchor)) { |
465 return OTS_FAILURE(); | 471 return OTS_FAILURE_MSG("Failed to parse exit anchor table in entry exit
record %d", i); |
466 } | 472 } |
467 } | 473 } |
468 } | 474 } |
469 | 475 |
470 if (offset_coverage < subtable.offset() || offset_coverage >= length) { | 476 if (offset_coverage < subtable.offset() || offset_coverage >= length) { |
471 return OTS_FAILURE(); | 477 return OTS_FAILURE_MSG("Bad coverage offset in cursive attachment %d", offse
t_coverage); |
472 } | 478 } |
473 if (!ots::ParseCoverageTable(data + offset_coverage, | 479 if (!ots::ParseCoverageTable(file, data + offset_coverage, |
474 length - offset_coverage, | 480 length - offset_coverage, |
475 file->maxp->num_glyphs)) { | 481 file->maxp->num_glyphs)) { |
476 return OTS_FAILURE(); | 482 return OTS_FAILURE_MSG("Failed to parse coverage table in cursive attachment
"); |
477 } | 483 } |
478 | 484 |
479 return true; | 485 return true; |
480 } | 486 } |
481 | 487 |
482 bool ParseAnchorArrayTable(const uint8_t *data, const size_t length, | 488 bool ParseAnchorArrayTable(const ots::OpenTypeFile *file, |
| 489 const uint8_t *data, const size_t length, |
483 const uint16_t class_count) { | 490 const uint16_t class_count) { |
484 ots::Buffer subtable(data, length); | 491 ots::Buffer subtable(data, length); |
485 | 492 |
486 uint16_t record_count = 0; | 493 uint16_t record_count = 0; |
487 if (!subtable.ReadU16(&record_count)) { | 494 if (!subtable.ReadU16(&record_count)) { |
488 return OTS_FAILURE(); | 495 return OTS_FAILURE_MSG("Can't read anchor array length"); |
489 } | 496 } |
490 | 497 |
491 const unsigned anchor_array_end = 2 * static_cast<unsigned>(record_count) * | 498 const unsigned anchor_array_end = 2 * static_cast<unsigned>(record_count) * |
492 static_cast<unsigned>(class_count) + 2; | 499 static_cast<unsigned>(class_count) + 2; |
493 if (anchor_array_end > std::numeric_limits<uint16_t>::max()) { | 500 if (anchor_array_end > std::numeric_limits<uint16_t>::max()) { |
494 return OTS_FAILURE(); | 501 return OTS_FAILURE_MSG("Bad end of anchor array %d", anchor_array_end); |
495 } | 502 } |
496 for (unsigned i = 0; i < record_count; ++i) { | 503 for (unsigned i = 0; i < record_count; ++i) { |
497 for (unsigned j = 0; j < class_count; ++j) { | 504 for (unsigned j = 0; j < class_count; ++j) { |
498 uint16_t offset_record = 0; | 505 uint16_t offset_record = 0; |
499 if (!subtable.ReadU16(&offset_record)) { | 506 if (!subtable.ReadU16(&offset_record)) { |
500 return OTS_FAILURE(); | 507 return OTS_FAILURE_MSG("Can't read anchor array record offset for class
%d and record %d", j, i); |
501 } | 508 } |
502 // |offset_record| could be NULL. | 509 // |offset_record| could be NULL. |
503 if (offset_record) { | 510 if (offset_record) { |
504 if (offset_record < anchor_array_end || offset_record >= length) { | 511 if (offset_record < anchor_array_end || offset_record >= length) { |
505 return OTS_FAILURE(); | 512 return OTS_FAILURE_MSG("Bad record offset %d in class %d, record %d",
offset_record, j, i); |
506 } | 513 } |
507 if (!ParseAnchorTable(data + offset_record, | 514 if (!ParseAnchorTable(file, data + offset_record, |
508 length - offset_record)) { | 515 length - offset_record)) { |
509 return OTS_FAILURE(); | 516 return OTS_FAILURE_MSG("Failed to parse anchor table for class %d, rec
ord %d", j, i); |
510 } | 517 } |
511 } | 518 } |
512 } | 519 } |
513 } | 520 } |
514 return true; | 521 return true; |
515 } | 522 } |
516 | 523 |
517 bool ParseLigatureArrayTable(const uint8_t *data, const size_t length, | 524 bool ParseLigatureArrayTable(const ots::OpenTypeFile *file, |
| 525 const uint8_t *data, const size_t length, |
518 const uint16_t class_count) { | 526 const uint16_t class_count) { |
519 ots::Buffer subtable(data, length); | 527 ots::Buffer subtable(data, length); |
520 | 528 |
521 uint16_t ligature_count = 0; | 529 uint16_t ligature_count = 0; |
522 if (!subtable.ReadU16(&ligature_count)) { | 530 if (!subtable.ReadU16(&ligature_count)) { |
523 return OTS_FAILURE(); | 531 return OTS_FAILURE_MSG("Failed to read ligature count"); |
524 } | 532 } |
525 for (unsigned i = 0; i < ligature_count; ++i) { | 533 for (unsigned i = 0; i < ligature_count; ++i) { |
526 uint16_t offset_ligature_attach = 0; | 534 uint16_t offset_ligature_attach = 0; |
527 if (!subtable.ReadU16(&offset_ligature_attach)) { | 535 if (!subtable.ReadU16(&offset_ligature_attach)) { |
528 return OTS_FAILURE(); | 536 return OTS_FAILURE_MSG("Can't read ligature offset %d", i); |
529 } | 537 } |
530 if (offset_ligature_attach < 2 || offset_ligature_attach >= length) { | 538 if (offset_ligature_attach < 2 || offset_ligature_attach >= length) { |
531 return OTS_FAILURE(); | 539 return OTS_FAILURE_MSG("Bad ligature attachment offset %d in ligature %d",
offset_ligature_attach, i); |
532 } | 540 } |
533 if (!ParseAnchorArrayTable(data + offset_ligature_attach, | 541 if (!ParseAnchorArrayTable(file, data + offset_ligature_attach, |
534 length - offset_ligature_attach, class_count)) { | 542 length - offset_ligature_attach, class_count)) { |
535 return OTS_FAILURE(); | 543 return OTS_FAILURE_MSG("Failed to parse anchor table for ligature %d", i); |
536 } | 544 } |
537 } | 545 } |
538 return true; | 546 return true; |
539 } | 547 } |
540 | 548 |
541 // Common parser for Lookup Type 4, 5 and 6. | 549 // Common parser for Lookup Type 4, 5 and 6. |
542 bool ParseMarkToAttachmentSubtables(const ots::OpenTypeFile *file, | 550 bool ParseMarkToAttachmentSubtables(const ots::OpenTypeFile *file, |
543 const uint8_t *data, const size_t length, | 551 const uint8_t *data, const size_t length, |
544 const GPOS_TYPE type) { | 552 const GPOS_TYPE type) { |
545 ots::Buffer subtable(data, length); | 553 ots::Buffer subtable(data, length); |
546 | 554 |
547 uint16_t format = 0; | 555 uint16_t format = 0; |
548 uint16_t offset_coverage1 = 0; | 556 uint16_t offset_coverage1 = 0; |
549 uint16_t offset_coverage2 = 0; | 557 uint16_t offset_coverage2 = 0; |
550 uint16_t class_count = 0; | 558 uint16_t class_count = 0; |
551 uint16_t offset_mark_array = 0; | 559 uint16_t offset_mark_array = 0; |
552 uint16_t offset_type_specific_array = 0; | 560 uint16_t offset_type_specific_array = 0; |
553 if (!subtable.ReadU16(&format) || | 561 if (!subtable.ReadU16(&format) || |
554 !subtable.ReadU16(&offset_coverage1) || | 562 !subtable.ReadU16(&offset_coverage1) || |
555 !subtable.ReadU16(&offset_coverage2) || | 563 !subtable.ReadU16(&offset_coverage2) || |
556 !subtable.ReadU16(&class_count) || | 564 !subtable.ReadU16(&class_count) || |
557 !subtable.ReadU16(&offset_mark_array) || | 565 !subtable.ReadU16(&offset_mark_array) || |
558 !subtable.ReadU16(&offset_type_specific_array)) { | 566 !subtable.ReadU16(&offset_type_specific_array)) { |
559 return OTS_FAILURE(); | 567 return OTS_FAILURE_MSG("Failed to read mark attachment subtable header"); |
560 } | 568 } |
561 | 569 |
562 if (format != 1) { | 570 if (format != 1) { |
563 return OTS_FAILURE(); | 571 return OTS_FAILURE_MSG("bad mark attachment subtable format %d", format); |
564 } | 572 } |
565 | 573 |
566 const unsigned header_end = static_cast<unsigned>(subtable.offset()); | 574 const unsigned header_end = static_cast<unsigned>(subtable.offset()); |
567 if (header_end > std::numeric_limits<uint16_t>::max()) { | 575 if (header_end > std::numeric_limits<uint16_t>::max()) { |
568 return OTS_FAILURE(); | 576 return OTS_FAILURE_MSG("Bad mark attachment subtable size ending at %d", hea
der_end); |
569 } | 577 } |
570 if (offset_coverage1 < header_end || offset_coverage1 >= length) { | 578 if (offset_coverage1 < header_end || offset_coverage1 >= length) { |
571 return OTS_FAILURE(); | 579 return OTS_FAILURE_MSG("Bad coverage 1 offset %d", offset_coverage1); |
572 } | 580 } |
573 if (!ots::ParseCoverageTable(data + offset_coverage1, | 581 if (!ots::ParseCoverageTable(file, data + offset_coverage1, |
574 length - offset_coverage1, | 582 length - offset_coverage1, |
575 file->maxp->num_glyphs)) { | 583 file->maxp->num_glyphs)) { |
576 return OTS_FAILURE(); | 584 return OTS_FAILURE_MSG("Failed to parse converge 1 table"); |
577 } | 585 } |
578 if (offset_coverage2 < header_end || offset_coverage2 >= length) { | 586 if (offset_coverage2 < header_end || offset_coverage2 >= length) { |
579 return OTS_FAILURE(); | 587 return OTS_FAILURE_MSG("Bad coverage 2 offset %d", offset_coverage2); |
580 } | 588 } |
581 if (!ots::ParseCoverageTable(data + offset_coverage2, | 589 if (!ots::ParseCoverageTable(file, data + offset_coverage2, |
582 length - offset_coverage2, | 590 length - offset_coverage2, |
583 file->maxp->num_glyphs)) { | 591 file->maxp->num_glyphs)) { |
584 return OTS_FAILURE(); | 592 return OTS_FAILURE_MSG("Failed to parse coverage table 2"); |
585 } | 593 } |
586 | 594 |
587 if (offset_mark_array < header_end || offset_mark_array >= length) { | 595 if (offset_mark_array < header_end || offset_mark_array >= length) { |
588 return OTS_FAILURE(); | 596 return OTS_FAILURE_MSG("Bad mark array offset %d", offset_mark_array); |
589 } | 597 } |
590 if (!ParseMarkArrayTable(data + offset_mark_array, | 598 if (!ParseMarkArrayTable(file, data + offset_mark_array, |
591 length - offset_mark_array, class_count)) { | 599 length - offset_mark_array, class_count)) { |
592 return OTS_FAILURE(); | 600 return OTS_FAILURE_MSG("Failed to parse mark array"); |
593 } | 601 } |
594 | 602 |
595 if (offset_type_specific_array < header_end || | 603 if (offset_type_specific_array < header_end || |
596 offset_type_specific_array >= length) { | 604 offset_type_specific_array >= length) { |
597 return OTS_FAILURE(); | 605 return OTS_FAILURE_MSG("Bad type specific array offset %d", offset_type_spec
ific_array); |
598 } | 606 } |
599 if (type == GPOS_TYPE_MARK_TO_BASE_ATTACHMENT || | 607 if (type == GPOS_TYPE_MARK_TO_BASE_ATTACHMENT || |
600 type == GPOS_TYPE_MARK_TO_MARK_ATTACHMENT) { | 608 type == GPOS_TYPE_MARK_TO_MARK_ATTACHMENT) { |
601 if (!ParseAnchorArrayTable(data + offset_type_specific_array, | 609 if (!ParseAnchorArrayTable(file, data + offset_type_specific_array, |
602 length - offset_type_specific_array, | 610 length - offset_type_specific_array, |
603 class_count)) { | 611 class_count)) { |
604 return OTS_FAILURE(); | 612 return OTS_FAILURE_MSG("Failed to parse anchor array"); |
605 } | 613 } |
606 } else if (type == GPOS_TYPE_MARK_TO_LIGATURE_ATTACHMENT) { | 614 } else if (type == GPOS_TYPE_MARK_TO_LIGATURE_ATTACHMENT) { |
607 if (!ParseLigatureArrayTable(data + offset_type_specific_array, | 615 if (!ParseLigatureArrayTable(file, data + offset_type_specific_array, |
608 length - offset_type_specific_array, | 616 length - offset_type_specific_array, |
609 class_count)) { | 617 class_count)) { |
610 return OTS_FAILURE(); | 618 return OTS_FAILURE_MSG("Failed to parse ligature array"); |
611 } | 619 } |
612 } else { | 620 } else { |
613 return OTS_FAILURE(); | 621 return OTS_FAILURE_MSG("Bad attachment type %d", type); |
614 } | 622 } |
615 | 623 |
616 return true; | 624 return true; |
617 } | 625 } |
618 | 626 |
619 // Lookup Type 4: | 627 // Lookup Type 4: |
620 // MarkToBase Attachment Positioning Subtable | 628 // MarkToBase Attachment Positioning Subtable |
621 bool ParseMarkToBaseAttachment(const ots::OpenTypeFile *file, | 629 bool ParseMarkToBaseAttachment(const ots::OpenTypeFile *file, |
622 const uint8_t *data, const size_t length) { | 630 const uint8_t *data, const size_t length) { |
623 return ParseMarkToAttachmentSubtables(file, data, length, | 631 return ParseMarkToAttachmentSubtables(file, data, length, |
(...skipping 13 matching lines...) Expand all Loading... |
637 bool ParseMarkToMarkAttachment(const ots::OpenTypeFile *file, | 645 bool ParseMarkToMarkAttachment(const ots::OpenTypeFile *file, |
638 const uint8_t *data, const size_t length) { | 646 const uint8_t *data, const size_t length) { |
639 return ParseMarkToAttachmentSubtables(file, data, length, | 647 return ParseMarkToAttachmentSubtables(file, data, length, |
640 GPOS_TYPE_MARK_TO_MARK_ATTACHMENT); | 648 GPOS_TYPE_MARK_TO_MARK_ATTACHMENT); |
641 } | 649 } |
642 | 650 |
643 // Lookup Type 7: | 651 // Lookup Type 7: |
644 // Contextual Positioning Subtables | 652 // Contextual Positioning Subtables |
645 bool ParseContextPositioning(const ots::OpenTypeFile *file, | 653 bool ParseContextPositioning(const ots::OpenTypeFile *file, |
646 const uint8_t *data, const size_t length) { | 654 const uint8_t *data, const size_t length) { |
647 return ots::ParseContextSubtable(data, length, file->maxp->num_glyphs, | 655 return ots::ParseContextSubtable(file, data, length, file->maxp->num_glyphs, |
648 file->gpos->num_lookups); | 656 file->gpos->num_lookups); |
649 } | 657 } |
650 | 658 |
651 // Lookup Type 8: | 659 // Lookup Type 8: |
652 // Chaining Contexual Positioning Subtable | 660 // Chaining Contexual Positioning Subtable |
653 bool ParseChainedContextPositioning(const ots::OpenTypeFile *file, | 661 bool ParseChainedContextPositioning(const ots::OpenTypeFile *file, |
654 const uint8_t *data, const size_t length) { | 662 const uint8_t *data, const size_t length) { |
655 return ots::ParseChainingContextSubtable(data, length, | 663 return ots::ParseChainingContextSubtable(file, data, length, |
656 file->maxp->num_glyphs, | 664 file->maxp->num_glyphs, |
657 file->gpos->num_lookups); | 665 file->gpos->num_lookups); |
658 } | 666 } |
659 | 667 |
660 // Lookup Type 9: | 668 // Lookup Type 9: |
661 // Extension Positioning | 669 // Extension Positioning |
662 bool ParseExtensionPositioning(const ots::OpenTypeFile *file, | 670 bool ParseExtensionPositioning(const ots::OpenTypeFile *file, |
663 const uint8_t *data, const size_t length) { | 671 const uint8_t *data, const size_t length) { |
664 return ots::ParseExtensionSubtable(file, data, length, | 672 return ots::ParseExtensionSubtable(file, data, length, |
665 &kGposLookupSubtableParser); | 673 &kGposLookupSubtableParser); |
666 } | 674 } |
667 | 675 |
668 } // namespace | 676 } // namespace |
669 | 677 |
670 #define DROP_THIS_TABLE \ | 678 #define DROP_THIS_TABLE(msg_) \ |
671 do { file->gpos->data = 0; file->gpos->length = 0; } while (0) | 679 do { \ |
| 680 file->gpos->data = 0; \ |
| 681 file->gpos->length = 0; \ |
| 682 OTS_FAILURE_MSG(msg_ ", table discarded"); \ |
| 683 } while (0) |
672 | 684 |
673 namespace ots { | 685 namespace ots { |
674 | 686 |
675 // As far as I checked, following fonts contain invalid GPOS table and | 687 // As far as I checked, following fonts contain invalid GPOS table and |
676 // OTS will drop their GPOS table. | 688 // OTS will drop their GPOS table. |
677 // | 689 // |
678 // # invalid delta format in device table | 690 // # invalid delta format in device table |
679 // samanata.ttf | 691 // samanata.ttf |
680 // | 692 // |
681 // # bad size range in device table | 693 // # bad size range in device table |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
714 // GenBkBasR.ttf | 726 // GenBkBasR.ttf |
715 // Padauk-Bold.ttf | 727 // Padauk-Bold.ttf |
716 // Padauk.ttf | 728 // Padauk.ttf |
717 // | 729 // |
718 // # Contour point indexes aren't sorted | 730 // # Contour point indexes aren't sorted |
719 // Arial Unicode.ttf | 731 // Arial Unicode.ttf |
720 | 732 |
721 bool ots_gpos_parse(OpenTypeFile *file, const uint8_t *data, size_t length) { | 733 bool ots_gpos_parse(OpenTypeFile *file, const uint8_t *data, size_t length) { |
722 // Parsing GPOS table requires num_glyphs which is contained in maxp table. | 734 // Parsing GPOS table requires num_glyphs which is contained in maxp table. |
723 if (!file->maxp) { | 735 if (!file->maxp) { |
724 return OTS_FAILURE(); | 736 return OTS_FAILURE_MSG("missing maxp table needed in GPOS"); |
725 } | 737 } |
726 | 738 |
727 Buffer table(data, length); | 739 Buffer table(data, length); |
728 | 740 |
729 OpenTypeGPOS *gpos = new OpenTypeGPOS; | 741 OpenTypeGPOS *gpos = new OpenTypeGPOS; |
730 file->gpos = gpos; | 742 file->gpos = gpos; |
731 | 743 |
732 uint32_t version = 0; | 744 uint32_t version = 0; |
733 uint16_t offset_script_list = 0; | 745 uint16_t offset_script_list = 0; |
734 uint16_t offset_feature_list = 0; | 746 uint16_t offset_feature_list = 0; |
735 uint16_t offset_lookup_list = 0; | 747 uint16_t offset_lookup_list = 0; |
736 if (!table.ReadU32(&version) || | 748 if (!table.ReadU32(&version) || |
737 !table.ReadU16(&offset_script_list) || | 749 !table.ReadU16(&offset_script_list) || |
738 !table.ReadU16(&offset_feature_list) || | 750 !table.ReadU16(&offset_feature_list) || |
739 !table.ReadU16(&offset_lookup_list)) { | 751 !table.ReadU16(&offset_lookup_list)) { |
740 return OTS_FAILURE(); | 752 DROP_THIS_TABLE("Incomplete table"); |
| 753 return true; |
741 } | 754 } |
742 | 755 |
743 if (version != 0x00010000) { | 756 if (version != 0x00010000) { |
744 OTS_WARNING("bad GPOS version"); | 757 DROP_THIS_TABLE("Bad version"); |
745 DROP_THIS_TABLE; | |
746 return true; | |
747 } | |
748 if ((offset_script_list < kGposHeaderSize || | |
749 offset_script_list >= length) || | |
750 (offset_feature_list < kGposHeaderSize || | |
751 offset_feature_list >= length) || | |
752 (offset_lookup_list < kGposHeaderSize || | |
753 offset_lookup_list >= length)) { | |
754 OTS_WARNING("bad offset in GPOS header"); | |
755 DROP_THIS_TABLE; | |
756 return true; | 758 return true; |
757 } | 759 } |
758 | 760 |
759 if (!ParseLookupListTable(file, data + offset_lookup_list, | 761 if (offset_lookup_list) { |
760 length - offset_lookup_list, | 762 if (offset_lookup_list < kGposHeaderSize || offset_lookup_list >= length) { |
761 &kGposLookupSubtableParser, | 763 DROP_THIS_TABLE("Bad lookup list offset in table header"); |
762 &gpos->num_lookups)) { | 764 return true; |
763 OTS_WARNING("faild to parse lookup list table"); | 765 } |
764 DROP_THIS_TABLE; | 766 |
765 return true; | 767 if (!ParseLookupListTable(file, data + offset_lookup_list, |
| 768 length - offset_lookup_list, |
| 769 &kGposLookupSubtableParser, |
| 770 &gpos->num_lookups)) { |
| 771 DROP_THIS_TABLE("Failed to parse lookup list table"); |
| 772 return true; |
| 773 } |
766 } | 774 } |
767 | 775 |
768 uint16_t num_features = 0; | 776 uint16_t num_features = 0; |
769 if (!ParseFeatureListTable(data + offset_feature_list, | 777 if (offset_feature_list) { |
770 length - offset_feature_list, gpos->num_lookups, | 778 if (offset_feature_list < kGposHeaderSize || offset_feature_list >= length)
{ |
771 &num_features)) { | 779 DROP_THIS_TABLE("Bad feature list offset in table header"); |
772 OTS_WARNING("faild to parse feature list table"); | 780 return true; |
773 DROP_THIS_TABLE; | 781 } |
774 return true; | 782 |
| 783 if (!ParseFeatureListTable(file, data + offset_feature_list, |
| 784 length - offset_feature_list, gpos->num_lookups, |
| 785 &num_features)) { |
| 786 DROP_THIS_TABLE("Failed to parse feature list table"); |
| 787 return true; |
| 788 } |
775 } | 789 } |
776 | 790 |
777 if (!ParseScriptListTable(data + offset_script_list, | 791 if (offset_script_list) { |
778 length - offset_script_list, num_features)) { | 792 if (offset_script_list < kGposHeaderSize || offset_script_list >= length) { |
779 OTS_WARNING("faild to parse script list table"); | 793 DROP_THIS_TABLE("Bad script list offset in table header"); |
780 DROP_THIS_TABLE; | 794 return true; |
781 return true; | 795 } |
| 796 |
| 797 if (!ParseScriptListTable(file, data + offset_script_list, |
| 798 length - offset_script_list, num_features)) { |
| 799 DROP_THIS_TABLE("Failed to parse script list table"); |
| 800 return true; |
| 801 } |
782 } | 802 } |
783 | 803 |
784 gpos->data = data; | 804 gpos->data = data; |
785 gpos->length = length; | 805 gpos->length = length; |
786 return true; | 806 return true; |
787 } | 807 } |
788 | 808 |
789 bool ots_gpos_should_serialise(OpenTypeFile *file) { | 809 bool ots_gpos_should_serialise(OpenTypeFile *file) { |
790 const bool needed_tables_dropped = | 810 return file->gpos != NULL && file->gpos->data != NULL; |
791 (file->gdef && file->gdef->data == NULL) || | |
792 (file->gsub && file->gsub->data == NULL); | |
793 return file->gpos != NULL && file->gpos->data != NULL && | |
794 !needed_tables_dropped; | |
795 } | 811 } |
796 | 812 |
797 bool ots_gpos_serialise(OTSStream *out, OpenTypeFile *file) { | 813 bool ots_gpos_serialise(OTSStream *out, OpenTypeFile *file) { |
798 if (!out->Write(file->gpos->data, file->gpos->length)) { | 814 if (!out->Write(file->gpos->data, file->gpos->length)) { |
799 return OTS_FAILURE(); | 815 return OTS_FAILURE_MSG("Failed to write GPOS table"); |
800 } | 816 } |
801 | 817 |
802 return true; | 818 return true; |
803 } | 819 } |
804 | 820 |
805 void ots_gpos_free(OpenTypeFile *file) { | 821 void ots_gpos_free(OpenTypeFile *file) { |
806 delete file->gpos; | 822 delete file->gpos; |
807 } | 823 } |
808 | 824 |
809 } // namespace ots | 825 } // namespace ots |
810 | 826 |
| 827 #undef TABLE_NAME |
| 828 #undef DROP_THIS_TABLE |
OLD | NEW |