68 #define HB_OT_TAG_glyf HB_TAG('g','l','y','f') |
69 #define HB_OT_TAG_glyf HB_TAG('g','l','y','f') |
69 |
70 |
70 |
71 |
71 struct glyf |
72 struct glyf |
72 { |
73 { |
73 static const hb_tag_t tableTag = HB_OT_TAG_glyf; |
74 static constexpr hb_tag_t tableTag = HB_OT_TAG_glyf; |
74 |
75 |
75 inline bool sanitize (hb_sanitize_context_t *c) const |
76 bool sanitize (hb_sanitize_context_t *c HB_UNUSED) const |
76 { |
77 { |
77 TRACE_SANITIZE (this); |
78 TRACE_SANITIZE (this); |
78 /* We don't check for anything specific here. The users of the |
79 /* We don't check for anything specific here. The users of the |
79 * struct do all the hard work... */ |
80 * struct do all the hard work... */ |
80 return_trace (true); |
81 return_trace (true); |
81 } |
82 } |
82 |
83 |
83 inline bool subset (hb_subset_plan_t *plan) const |
84 bool subset (hb_subset_plan_t *plan) const |
84 { |
85 { |
85 hb_blob_t *glyf_prime = nullptr; |
86 hb_blob_t *glyf_prime = nullptr; |
86 hb_blob_t *loca_prime = nullptr; |
87 hb_blob_t *loca_prime = nullptr; |
87 |
88 |
88 bool success = true; |
89 bool success = true; |
101 } |
102 } |
102 |
103 |
103 static bool |
104 static bool |
104 _add_head_and_set_loca_version (hb_subset_plan_t *plan, bool use_short_loca) |
105 _add_head_and_set_loca_version (hb_subset_plan_t *plan, bool use_short_loca) |
105 { |
106 { |
106 hb_blob_t *head_blob = OT::Sanitizer<OT::head>().sanitize (hb_face_reference_table (plan->source, HB_OT_TAG_head)); |
107 hb_blob_t *head_blob = hb_sanitize_context_t ().reference_table<head> (plan->source); |
107 hb_blob_t *head_prime_blob = hb_blob_copy_writable_or_fail (head_blob); |
108 hb_blob_t *head_prime_blob = hb_blob_copy_writable_or_fail (head_blob); |
108 hb_blob_destroy (head_blob); |
109 hb_blob_destroy (head_blob); |
109 |
110 |
110 if (unlikely (!head_prime_blob)) |
111 if (unlikely (!head_prime_blob)) |
111 return false; |
112 return false; |
112 |
113 |
113 OT::head *head_prime = (OT::head *) hb_blob_get_data_writable (head_prime_blob, nullptr); |
114 head *head_prime = (head *) hb_blob_get_data_writable (head_prime_blob, nullptr); |
114 head_prime->indexToLocFormat.set (use_short_loca ? 0 : 1); |
115 head_prime->indexToLocFormat.set (use_short_loca ? 0 : 1); |
115 bool success = plan->add_table (HB_OT_TAG_head, head_prime_blob); |
116 bool success = plan->add_table (HB_OT_TAG_head, head_prime_blob); |
116 |
117 |
117 hb_blob_destroy (head_prime_blob); |
118 hb_blob_destroy (head_prime_blob); |
118 return success; |
119 return success; |
148 SCALED_COMPONENT_OFFSET = 0x0800, |
149 SCALED_COMPONENT_OFFSET = 0x0800, |
149 UNSCALED_COMPONENT_OFFSET = 0x1000 |
150 UNSCALED_COMPONENT_OFFSET = 0x1000 |
150 }; |
151 }; |
151 |
152 |
152 HBUINT16 flags; |
153 HBUINT16 flags; |
153 HBUINT16 glyphIndex; |
154 GlyphID glyphIndex; |
154 |
155 |
155 inline unsigned int get_size (void) const |
156 unsigned int get_size () const |
156 { |
157 { |
157 unsigned int size = min_size; |
158 unsigned int size = min_size; |
158 if (flags & ARG_1_AND_2_ARE_WORDS) { |
159 // arg1 and 2 are int16 |
159 // arg1 and 2 are int16 |
160 if (flags & ARG_1_AND_2_ARE_WORDS) size += 4; |
160 size += 4; |
161 // arg1 and 2 are int8 |
161 } else { |
162 else size += 2; |
162 // arg1 and 2 are int8 |
163 |
163 size += 2; |
164 // One x 16 bit (scale) |
164 } |
165 if (flags & WE_HAVE_A_SCALE) size += 2; |
165 if (flags & WE_HAVE_A_SCALE) { |
166 // Two x 16 bit (xscale, yscale) |
166 // One x 16 bit (scale) |
167 else if (flags & WE_HAVE_AN_X_AND_Y_SCALE) size += 4; |
167 size += 2; |
168 // Four x 16 bit (xscale, scale01, scale10, yscale) |
168 } else if (flags & WE_HAVE_AN_X_AND_Y_SCALE) { |
169 else if (flags & WE_HAVE_A_TWO_BY_TWO) size += 8; |
169 // Two x 16 bit (xscale, yscale) |
170 |
170 size += 4; |
|
171 } else if (flags & WE_HAVE_A_TWO_BY_TWO) { |
|
172 // Four x 16 bit (xscale, scale01, scale10, yscale) |
|
173 size += 8; |
|
174 } |
|
175 return size; |
171 return size; |
176 } |
172 } |
177 |
173 |
178 struct Iterator |
174 struct Iterator |
179 { |
175 { |
180 const char *glyph_start; |
176 const char *glyph_start; |
181 const char *glyph_end; |
177 const char *glyph_end; |
182 const CompositeGlyphHeader *current; |
178 const CompositeGlyphHeader *current; |
183 |
179 |
184 inline bool move_to_next () |
180 bool move_to_next () |
185 { |
181 { |
186 if (current->flags & CompositeGlyphHeader::MORE_COMPONENTS) |
182 if (current->flags & CompositeGlyphHeader::MORE_COMPONENTS) |
187 { |
183 { |
188 const CompositeGlyphHeader *possible = |
184 const CompositeGlyphHeader *possible = |
189 &StructAfter<CompositeGlyphHeader, CompositeGlyphHeader> (*current); |
185 &StructAfter<CompositeGlyphHeader, CompositeGlyphHeader> (*current); |
193 return true; |
189 return true; |
194 } |
190 } |
195 return false; |
191 return false; |
196 } |
192 } |
197 |
193 |
198 inline bool in_range (const CompositeGlyphHeader *composite) const |
194 bool in_range (const CompositeGlyphHeader *composite) const |
199 { |
195 { |
200 return (const char *) composite >= glyph_start |
196 return (const char *) composite >= glyph_start |
201 && ((const char *) composite + CompositeGlyphHeader::min_size) <= glyph_end |
197 && ((const char *) composite + CompositeGlyphHeader::min_size) <= glyph_end |
202 && ((const char *) composite + composite->get_size()) <= glyph_end; |
198 && ((const char *) composite + composite->get_size ()) <= glyph_end; |
203 } |
199 } |
204 }; |
200 }; |
205 |
201 |
206 static inline bool get_iterator (const char * glyph_data, |
202 static bool get_iterator (const char * glyph_data, |
207 unsigned int length, |
203 unsigned int length, |
208 CompositeGlyphHeader::Iterator *iterator /* OUT */) |
204 CompositeGlyphHeader::Iterator *iterator /* OUT */) |
209 { |
205 { |
210 if (length < GlyphHeader::static_size) |
206 if (length < GlyphHeader::static_size) |
211 return false; /* Empty glyph; zero extents. */ |
207 return false; /* Empty glyph; zero extents. */ |
212 |
208 |
213 const GlyphHeader &glyph_header = StructAtOffset<GlyphHeader> (glyph_data, 0); |
209 const GlyphHeader &glyph_header = StructAtOffset<GlyphHeader> (glyph_data, 0); |
230 DEFINE_SIZE_MIN (4); |
226 DEFINE_SIZE_MIN (4); |
231 }; |
227 }; |
232 |
228 |
233 struct accelerator_t |
229 struct accelerator_t |
234 { |
230 { |
235 inline void init (hb_face_t *face) |
231 void init (hb_face_t *face) |
236 { |
232 { |
237 memset (this, 0, sizeof (accelerator_t)); |
233 memset (this, 0, sizeof (accelerator_t)); |
238 |
234 |
239 hb_blob_t *head_blob = Sanitizer<head>().sanitize (face->reference_table (HB_OT_TAG_head)); |
235 const OT::head &head = *face->table.head; |
240 const head *head_table = head_blob->as<head> (); |
236 if (head.indexToLocFormat > 1 || head.glyphDataFormat != 0) |
241 if (head_table == &Null(head) || (unsigned int) head_table->indexToLocFormat > 1 || head_table->glyphDataFormat != 0) |
237 /* Unknown format. Leave num_glyphs=0, that takes care of disabling us. */ |
242 { |
|
243 /* head table is not present, or in an unknown format. Leave num_glyphs=0, that takes care of disabling us. */ |
|
244 hb_blob_destroy (head_blob); |
|
245 return; |
238 return; |
246 } |
239 short_offset = 0 == head.indexToLocFormat; |
247 short_offset = 0 == head_table->indexToLocFormat; |
240 |
248 hb_blob_destroy (head_blob); |
241 loca_table = hb_sanitize_context_t ().reference_table<loca> (face); |
249 |
242 glyf_table = hb_sanitize_context_t ().reference_table<glyf> (face); |
250 loca_blob = Sanitizer<loca>().sanitize (face->reference_table (HB_OT_TAG_loca)); |
243 |
251 loca_table = loca_blob->as<loca> (); |
244 num_glyphs = MAX (1u, loca_table.get_length () / (short_offset ? 2 : 4)) - 1; |
252 glyf_blob = Sanitizer<glyf>().sanitize (face->reference_table (HB_OT_TAG_glyf)); |
245 } |
253 glyf_table = glyf_blob->as<glyf> (); |
246 |
254 |
247 void fini () |
255 num_glyphs = MAX (1u, hb_blob_get_length (loca_blob) / (short_offset ? 2 : 4)) - 1; |
248 { |
256 glyf_len = hb_blob_get_length (glyf_blob); |
249 loca_table.destroy (); |
257 } |
250 glyf_table.destroy (); |
258 |
|
259 inline void fini (void) |
|
260 { |
|
261 hb_blob_destroy (loca_blob); |
|
262 hb_blob_destroy (glyf_blob); |
|
263 } |
251 } |
264 |
252 |
265 /* |
253 /* |
266 * Returns true if the referenced glyph is a valid glyph and a composite glyph. |
254 * Returns true if the referenced glyph is a valid glyph and a composite glyph. |
267 * If true is returned a pointer to the composite glyph will be written into |
255 * If true is returned a pointer to the composite glyph will be written into |
268 * composite. |
256 * composite. |
269 */ |
257 */ |
270 inline bool get_composite (hb_codepoint_t glyph, |
258 bool get_composite (hb_codepoint_t glyph, |
271 CompositeGlyphHeader::Iterator *composite /* OUT */) const |
259 CompositeGlyphHeader::Iterator *composite /* OUT */) const |
272 { |
260 { |
273 if (this->glyf_table == &Null(glyf) || !num_glyphs) |
261 if (unlikely (!num_glyphs)) |
274 return false; |
262 return false; |
275 |
263 |
276 unsigned int start_offset, end_offset; |
264 unsigned int start_offset, end_offset; |
277 if (!get_offsets (glyph, &start_offset, &end_offset)) |
265 if (!get_offsets (glyph, &start_offset, &end_offset)) |
278 return false; /* glyph not found */ |
266 return false; /* glyph not found */ |
279 |
267 |
280 return CompositeGlyphHeader::get_iterator ((const char*) this->glyf_table + start_offset, |
268 return CompositeGlyphHeader::get_iterator ((const char *) this->glyf_table + start_offset, |
281 end_offset - start_offset, |
269 end_offset - start_offset, |
282 composite); |
270 composite); |
283 } |
271 } |
284 |
272 |
285 enum simple_glyph_flag_t { |
273 enum simple_glyph_flag_t { |
|
274 FLAG_ON_CURVE = 0x01, |
286 FLAG_X_SHORT = 0x02, |
275 FLAG_X_SHORT = 0x02, |
287 FLAG_Y_SHORT = 0x04, |
276 FLAG_Y_SHORT = 0x04, |
288 FLAG_REPEAT = 0x08, |
277 FLAG_REPEAT = 0x08, |
289 FLAG_X_SAME = 0x10, |
278 FLAG_X_SAME = 0x10, |
290 FLAG_Y_SAME = 0x20 |
279 FLAG_Y_SAME = 0x20, |
|
280 FLAG_RESERVED1 = 0x40, |
|
281 FLAG_RESERVED2 = 0x80 |
291 }; |
282 }; |
292 |
283 |
293 /* based on FontTools _g_l_y_f.py::trim */ |
284 /* based on FontTools _g_l_y_f.py::trim */ |
294 inline bool remove_padding(unsigned int start_offset, |
285 bool remove_padding (unsigned int start_offset, |
295 unsigned int *end_offset) const |
286 unsigned int *end_offset) const |
296 { |
287 { |
297 if (*end_offset - start_offset < GlyphHeader::static_size) |
288 if (*end_offset - start_offset < GlyphHeader::static_size) return true; |
298 return true; |
|
299 |
289 |
300 const char *glyph = ((const char *) glyf_table) + start_offset; |
290 const char *glyph = ((const char *) glyf_table) + start_offset; |
301 const char * const glyph_end = glyph + (*end_offset - start_offset); |
291 const char * const glyph_end = glyph + (*end_offset - start_offset); |
302 const GlyphHeader &glyph_header = StructAtOffset<GlyphHeader> (glyph, 0); |
292 const GlyphHeader &glyph_header = StructAtOffset<GlyphHeader> (glyph, 0); |
303 int16_t num_contours = (int16_t) glyph_header.numberOfContours; |
293 int16_t num_contours = (int16_t) glyph_header.numberOfContours; |
310 { |
300 { |
311 /* simple glyph w/contours, possibly trimmable */ |
301 /* simple glyph w/contours, possibly trimmable */ |
312 glyph += GlyphHeader::static_size + 2 * num_contours; |
302 glyph += GlyphHeader::static_size + 2 * num_contours; |
313 |
303 |
314 if (unlikely (glyph + 2 >= glyph_end)) return false; |
304 if (unlikely (glyph + 2 >= glyph_end)) return false; |
315 uint16_t nCoordinates = (uint16_t) StructAtOffset<HBUINT16>(glyph - 2, 0) + 1; |
305 uint16_t nCoordinates = (uint16_t) StructAtOffset<HBUINT16> (glyph - 2, 0) + 1; |
316 uint16_t nInstructions = (uint16_t) StructAtOffset<HBUINT16>(glyph, 0); |
306 uint16_t nInstructions = (uint16_t) StructAtOffset<HBUINT16> (glyph, 0); |
317 |
307 |
318 glyph += 2 + nInstructions; |
308 glyph += 2 + nInstructions; |
319 if (unlikely (glyph + 2 >= glyph_end)) return false; |
309 if (unlikely (glyph + 2 >= glyph_end)) return false; |
320 |
310 |
321 unsigned int coordBytes = 0; |
311 unsigned int coordBytes = 0; |
366 *end_offset -= glyph_end - glyph; |
352 *end_offset -= glyph_end - glyph; |
367 } |
353 } |
368 return true; |
354 return true; |
369 } |
355 } |
370 |
356 |
371 inline bool get_offsets (hb_codepoint_t glyph, |
357 bool get_offsets (hb_codepoint_t glyph, |
372 unsigned int *start_offset /* OUT */, |
358 unsigned int *start_offset /* OUT */, |
373 unsigned int *end_offset /* OUT */) const |
359 unsigned int *end_offset /* OUT */) const |
374 { |
360 { |
375 if (unlikely (glyph >= num_glyphs)) |
361 if (unlikely (glyph >= num_glyphs)) |
376 return false; |
362 return false; |
377 |
363 |
378 if (short_offset) |
364 if (short_offset) |
379 { |
365 { |
380 const HBUINT16 *offsets = (const HBUINT16 *) loca_table->dataZ; |
366 const HBUINT16 *offsets = (const HBUINT16 *) loca_table->dataZ.arrayZ; |
381 *start_offset = 2 * offsets[glyph]; |
367 *start_offset = 2 * offsets[glyph]; |
382 *end_offset = 2 * offsets[glyph + 1]; |
368 *end_offset = 2 * offsets[glyph + 1]; |
383 } |
369 } |
384 else |
370 else |
385 { |
371 { |
386 const HBUINT32 *offsets = (const HBUINT32 *) loca_table->dataZ; |
372 const HBUINT32 *offsets = (const HBUINT32 *) loca_table->dataZ.arrayZ; |
387 |
373 |
388 *start_offset = offsets[glyph]; |
374 *start_offset = offsets[glyph]; |
389 *end_offset = offsets[glyph + 1]; |
375 *end_offset = offsets[glyph + 1]; |
390 } |
376 } |
391 |
377 |
392 if (*start_offset > *end_offset || *end_offset > glyf_len) |
378 if (*start_offset > *end_offset || *end_offset > glyf_table.get_length ()) |
393 return false; |
379 return false; |
394 |
380 |
395 return true; |
381 return true; |
396 } |
382 } |
397 |
383 |
398 inline bool get_instruction_offsets(unsigned int start_offset, |
384 bool get_instruction_offsets (unsigned int start_offset, |
399 unsigned int end_offset, |
385 unsigned int end_offset, |
400 unsigned int *instruction_start /* OUT */, |
386 unsigned int *instruction_start /* OUT */, |
401 unsigned int *instruction_end /* OUT */) const |
387 unsigned int *instruction_end /* OUT */) const |
402 { |
388 { |
403 if (end_offset - start_offset < GlyphHeader::static_size) |
389 if (end_offset - start_offset < GlyphHeader::static_size) |
404 { |
390 { |
405 *instruction_start = 0; |
391 *instruction_start = 0; |
406 *instruction_end = 0; |
392 *instruction_end = 0; |
415 (const char*) this->glyf_table + start_offset, |
401 (const char*) this->glyf_table + start_offset, |
416 end_offset - start_offset, &composite_it))) return false; |
402 end_offset - start_offset, &composite_it))) return false; |
417 const CompositeGlyphHeader *last; |
403 const CompositeGlyphHeader *last; |
418 do { |
404 do { |
419 last = composite_it.current; |
405 last = composite_it.current; |
420 } while (composite_it.move_to_next()); |
406 } while (composite_it.move_to_next ()); |
421 |
407 |
422 if ( (uint16_t) last->flags & CompositeGlyphHeader::WE_HAVE_INSTRUCTIONS) |
408 if ((uint16_t) last->flags & CompositeGlyphHeader::WE_HAVE_INSTRUCTIONS) |
423 *instruction_start = ((char *) last - (char *) glyf_table->dataZ) + last->get_size(); |
409 *instruction_start = ((char *) last - (char *) glyf_table->dataZ.arrayZ) + last->get_size (); |
424 else |
410 else |
425 *instruction_start = end_offset; |
411 *instruction_start = end_offset; |
426 *instruction_end = end_offset; |
412 *instruction_end = end_offset; |
427 if (unlikely (*instruction_start > *instruction_end)) |
413 if (unlikely (*instruction_start > *instruction_end)) |
428 { |
414 { |
475 } |
460 } |
476 |
461 |
477 private: |
462 private: |
478 bool short_offset; |
463 bool short_offset; |
479 unsigned int num_glyphs; |
464 unsigned int num_glyphs; |
480 const loca *loca_table; |
465 hb_blob_ptr_t<loca> loca_table; |
481 const glyf *glyf_table; |
466 hb_blob_ptr_t<glyf> glyf_table; |
482 hb_blob_t *loca_blob; |
|
483 hb_blob_t *glyf_blob; |
|
484 unsigned int glyf_len; |
|
485 }; |
467 }; |
486 |
468 |
487 protected: |
469 protected: |
488 HBUINT8 dataZ[VAR]; /* Glyphs data. */ |
470 UnsizedArrayOf<HBUINT8> dataZ; /* Glyphs data. */ |
489 |
471 public: |
490 DEFINE_SIZE_ARRAY (0, dataZ); |
472 DEFINE_SIZE_MIN (0); /* In reality, this is UNBOUNDED() type; but since we always |
|
473 * check the size externally, allow Null() object of it by |
|
474 * defining it MIN() instead. */ |
491 }; |
475 }; |
492 |
476 |
|
477 struct glyf_accelerator_t : glyf::accelerator_t {}; |
|
478 |
493 } /* namespace OT */ |
479 } /* namespace OT */ |
494 |
480 |
495 |
481 |
496 #endif /* HB_OT_GLYF_TABLE_HH */ |
482 #endif /* HB_OT_GLYF_TABLE_HH */ |