2 * Copyright © 2017 Google, Inc.
4 * This is part of HarfBuzz, a text shaping library.
6 * Permission is hereby granted, without written agreement and without
7 * license or royalty fees, to use, copy, modify, and distribute this
8 * software and its documentation for any purpose, provided that the
9 * above copyright notice and the following two paragraphs appear in
10 * all copies of this software.
12 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
18 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
21 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
24 * Google Author(s): Behdad Esfahbod
27 #ifndef HB_AAT_LAYOUT_COMMON_PRIVATE_HH
28 #define HB_AAT_LAYOUT_COMMON_PRIVATE_HH
30 #include "hb-aat-layout-private.hh"
39 * Binary Searching Tables
42 struct BinSearchHeader
45 inline bool sanitize (hb_sanitize_context_t *c) const
47 TRACE_SANITIZE (this);
48 return_trace (c->check_struct (this));
51 HBUINT16 unitSize; /* Size of a lookup unit for this search in bytes. */
52 HBUINT16 nUnits; /* Number of units of the preceding size to be searched. */
53 HBUINT16 searchRange; /* The value of unitSize times the largest power of 2
54 * that is less than or equal to the value of nUnits. */
55 HBUINT16 entrySelector; /* The log base 2 of the largest power of 2 less than
56 * or equal to the value of nUnits. */
57 HBUINT16 rangeShift; /* The value of unitSize times the difference of the
58 * value of nUnits minus the largest power of 2 less
59 * than or equal to the value of nUnits. */
61 DEFINE_SIZE_STATIC (10);
64 template <typename Type>
65 struct BinSearchArrayOf
67 inline const Type& operator [] (unsigned int i) const
69 if (unlikely (i >= header.nUnits)) return Null(Type);
70 return StructAtOffset<Type> (bytesZ, i * header.unitSize);
72 inline Type& operator [] (unsigned int i)
74 return StructAtOffset<Type> (bytesZ, i * header.unitSize);
76 inline unsigned int get_size (void) const
77 { return header.static_size + header.nUnits * header.unitSize; }
79 inline bool sanitize (hb_sanitize_context_t *c) const
81 TRACE_SANITIZE (this);
82 if (unlikely (!sanitize_shallow (c))) return_trace (false);
84 /* Note: for structs that do not reference other structs,
85 * we do not need to call their sanitize() as we already did
86 * a bound check on the aggregate array size. We just include
87 * a small unreachable expression to make sure the structs
88 * pointed to do have a simple sanitize(), ie. they do not
89 * reference other structs via offsets.
91 (void) (false && StructAtOffset<Type> (bytesZ, 0).sanitize (c));
95 inline bool sanitize (hb_sanitize_context_t *c, const void *base) const
97 TRACE_SANITIZE (this);
98 if (unlikely (!sanitize_shallow (c))) return_trace (false);
99 unsigned int count = header.nUnits;
100 for (unsigned int i = 0; i < count; i++)
101 if (unlikely (!(*this)[i].sanitize (c, base)))
102 return_trace (false);
106 template <typename T>
107 inline const Type *bsearch (const T &key) const
109 unsigned int size = header.unitSize;
110 int min = 0, max = (int) header.nUnits - 1;
113 int mid = (min + max) / 2;
114 const Type *p = (const Type *) (((const char *) bytesZ) + (mid * size));
115 int c = p->cmp (key);
127 inline bool sanitize_shallow (hb_sanitize_context_t *c) const
129 TRACE_SANITIZE (this);
130 return_trace (header.sanitize (c) &&
131 Type::static_size >= header.unitSize &&
132 c->check_array (bytesZ, header.unitSize, header.nUnits));
136 BinSearchHeader header;
139 DEFINE_SIZE_ARRAY (10, bytesZ);
147 template <typename T> struct Lookup;
149 template <typename T>
152 friend struct Lookup<T>;
155 inline const T* get_value (hb_codepoint_t glyph_id, unsigned int num_glyphs) const
157 if (unlikely (glyph_id >= num_glyphs)) return nullptr;
158 return &arrayZ[glyph_id];
161 inline bool sanitize (hb_sanitize_context_t *c) const
163 TRACE_SANITIZE (this);
164 return_trace (arrayZ.sanitize (c, c->num_glyphs));
168 HBUINT16 format; /* Format identifier--format = 0 */
170 arrayZ; /* Array of lookup values, indexed by glyph index. */
172 DEFINE_SIZE_ARRAY (2, arrayZ);
176 template <typename T>
177 struct LookupSegmentSingle
179 inline int cmp (hb_codepoint_t g) const {
180 return g < first ? -1 : g <= last ? 0 : +1 ;
183 inline bool sanitize (hb_sanitize_context_t *c) const
185 TRACE_SANITIZE (this);
186 return_trace (c->check_struct (this) && value.sanitize (c));
189 GlyphID last; /* Last GlyphID in this segment */
190 GlyphID first; /* First GlyphID in this segment */
191 T value; /* The lookup value (only one) */
193 DEFINE_SIZE_STATIC (4 + T::static_size);
196 template <typename T>
199 friend struct Lookup<T>;
202 inline const T* get_value (hb_codepoint_t glyph_id) const
204 const LookupSegmentSingle<T> *v = segments.bsearch (glyph_id);
205 return v ? &v->value : nullptr;
208 inline bool sanitize (hb_sanitize_context_t *c) const
210 TRACE_SANITIZE (this);
211 return_trace (segments.sanitize (c));
215 HBUINT16 format; /* Format identifier--format = 2 */
216 BinSearchArrayOf<LookupSegmentSingle<T> >
217 segments; /* The actual segments. These must already be sorted,
218 * according to the first word in each one (the last
219 * glyph in each segment). */
221 DEFINE_SIZE_ARRAY (8, segments);
224 template <typename T>
225 struct LookupSegmentArray
227 inline const T* get_value (hb_codepoint_t glyph_id, const void *base) const
229 return first <= glyph_id && glyph_id <= last ? &(base+valuesZ)[glyph_id - first] : nullptr;
232 inline int cmp (hb_codepoint_t g) const {
233 return g < first ? -1 : g <= last ? 0 : +1 ;
236 inline bool sanitize (hb_sanitize_context_t *c, const void *base) const
238 TRACE_SANITIZE (this);
239 return_trace (c->check_struct (this) &&
241 valuesZ.sanitize (c, base, last - first + 1));
244 GlyphID last; /* Last GlyphID in this segment */
245 GlyphID first; /* First GlyphID in this segment */
246 OffsetTo<UnsizedArrayOf<T> >
247 valuesZ; /* A 16-bit offset from the start of
248 * the table to the data. */
250 DEFINE_SIZE_STATIC (6);
253 template <typename T>
256 friend struct Lookup<T>;
259 inline const T* get_value (hb_codepoint_t glyph_id) const
261 const LookupSegmentArray<T> *v = segments.bsearch (glyph_id);
262 return v ? v->get_value (glyph_id, this) : nullptr;
265 inline bool sanitize (hb_sanitize_context_t *c) const
267 TRACE_SANITIZE (this);
268 return_trace (segments.sanitize (c, this));
272 HBUINT16 format; /* Format identifier--format = 2 */
273 BinSearchArrayOf<LookupSegmentArray<T> >
274 segments; /* The actual segments. These must already be sorted,
275 * according to the first word in each one (the last
276 * glyph in each segment). */
278 DEFINE_SIZE_ARRAY (8, segments);
281 template <typename T>
284 inline int cmp (hb_codepoint_t g) const { return glyph.cmp (g); }
286 inline bool sanitize (hb_sanitize_context_t *c) const
288 TRACE_SANITIZE (this);
289 return_trace (c->check_struct (this) && value.sanitize (c));
292 GlyphID glyph; /* Last GlyphID */
293 T value; /* The lookup value (only one) */
295 DEFINE_SIZE_STATIC (4 + T::static_size);
298 template <typename T>
301 friend struct Lookup<T>;
304 inline const T* get_value (hb_codepoint_t glyph_id) const
306 const LookupSingle<T> *v = entries.bsearch (glyph_id);
307 return v ? &v->value : nullptr;
310 inline bool sanitize (hb_sanitize_context_t *c) const
312 TRACE_SANITIZE (this);
313 return_trace (entries.sanitize (c));
317 HBUINT16 format; /* Format identifier--format = 6 */
318 BinSearchArrayOf<LookupSingle<T> >
319 entries; /* The actual entries, sorted by glyph index. */
321 DEFINE_SIZE_ARRAY (8, entries);
324 template <typename T>
327 friend struct Lookup<T>;
330 inline const T* get_value (hb_codepoint_t glyph_id) const
332 return firstGlyph <= glyph_id && glyph_id - firstGlyph < glyphCount ? &valueArrayZ[glyph_id - firstGlyph] : nullptr;
335 inline bool sanitize (hb_sanitize_context_t *c) const
337 TRACE_SANITIZE (this);
338 return_trace (c->check_struct (this) && valueArrayZ.sanitize (c, glyphCount));
342 HBUINT16 format; /* Format identifier--format = 6 */
343 GlyphID firstGlyph; /* First glyph index included in the trimmed array. */
344 HBUINT16 glyphCount; /* Total number of glyphs (equivalent to the last
345 * glyph minus the value of firstGlyph plus 1). */
347 valueArrayZ; /* The lookup values (indexed by the glyph index
348 * minus the value of firstGlyph). */
350 DEFINE_SIZE_ARRAY (6, valueArrayZ);
353 template <typename T>
356 inline const T* get_value (hb_codepoint_t glyph_id, unsigned int num_glyphs) const
359 case 0: return u.format0.get_value (glyph_id, num_glyphs);
360 case 2: return u.format2.get_value (glyph_id);
361 case 4: return u.format4.get_value (glyph_id);
362 case 6: return u.format6.get_value (glyph_id);
363 case 8: return u.format8.get_value (glyph_id);
364 default:return nullptr;
368 inline bool sanitize (hb_sanitize_context_t *c) const
370 TRACE_SANITIZE (this);
371 if (!u.format.sanitize (c)) return_trace (false);
373 case 0: return_trace (u.format0.sanitize (c));
374 case 2: return_trace (u.format2.sanitize (c));
375 case 4: return_trace (u.format4.sanitize (c));
376 case 6: return_trace (u.format6.sanitize (c));
377 case 8: return_trace (u.format8.sanitize (c));
378 default:return_trace (true);
384 HBUINT16 format; /* Format identifier */
385 LookupFormat0<T> format0;
386 LookupFormat2<T> format2;
387 LookupFormat4<T> format4;
388 LookupFormat6<T> format6;
389 LookupFormat8<T> format8;
392 DEFINE_SIZE_UNION (2, format);
397 * Extended State Table
400 template <typename T>
403 inline bool sanitize (hb_sanitize_context_t *c, unsigned int count) const
405 TRACE_SANITIZE (this);
406 /* Note, we don't recurse-sanitize data because we don't access it.
407 * That said, in our DEFINE_SIZE_STATIC we access T::static_size,
408 * which ensures that data has a simple sanitize(). To be determined
409 * if I need to remove that as well. */
410 return_trace (c->check_struct (this));
414 HBUINT16 newState; /* Byte offset from beginning of state table
415 * to the new state. Really?!?! Or just state
416 * number? The latter in morx for sure. */
417 HBUINT16 flags; /* Table specific. */
418 T data; /* Optional offsets to per-glyph tables. */
420 DEFINE_SIZE_STATIC (4 + T::static_size);
426 inline bool sanitize (hb_sanitize_context_t *c, unsigned int count) const
428 TRACE_SANITIZE (this);
429 return_trace (c->check_struct (this));
433 HBUINT16 newState; /* Byte offset from beginning of state table to the new state. */
434 HBUINT16 flags; /* Table specific. */
436 DEFINE_SIZE_STATIC (4);
439 template <typename Extra>
442 inline unsigned int get_class (hb_codepoint_t glyph_id, unsigned int num_glyphs) const
444 const HBUINT16 *v = (this+classTable).get_value (glyph_id, num_glyphs);
448 inline const Entry<Extra> *get_entries () const
450 return (this+entryTable).arrayZ;
453 inline const Entry<Extra> *get_entryZ (unsigned int state, unsigned int klass) const
455 if (unlikely (klass >= nClasses)) return nullptr;
457 const HBUINT16 *states = (this+stateArrayTable).arrayZ;
458 const Entry<Extra> *entries = (this+entryTable).arrayZ;
460 unsigned int entry = states[state * nClasses + klass];
462 return &entries[entry];
465 inline bool sanitize (hb_sanitize_context_t *c,
466 unsigned int *num_entries_out = nullptr) const
468 TRACE_SANITIZE (this);
469 if (unlikely (!(c->check_struct (this) &&
470 classTable.sanitize (c, this)))) return_trace (false);
472 const HBUINT16 *states = (this+stateArrayTable).arrayZ;
473 const Entry<Extra> *entries = (this+entryTable).arrayZ;
475 unsigned int num_states = 1;
476 unsigned int num_entries = 0;
478 unsigned int state = 0;
479 unsigned int entry = 0;
480 while (state < num_states)
482 if (unlikely (!c->check_array (states,
483 states[0].static_size * nClasses,
485 return_trace (false);
486 { /* Sweep new states. */
487 const HBUINT16 *stop = &states[num_states * nClasses];
488 for (const HBUINT16 *p = &states[state * nClasses]; p < stop; p++)
489 num_entries = MAX<unsigned int> (num_entries, *p + 1);
493 if (unlikely (!c->check_array (entries,
494 entries[0].static_size,
496 return_trace (false);
497 { /* Sweep new entries. */
498 const Entry<Extra> *stop = &entries[num_entries];
499 for (const Entry<Extra> *p = &entries[entry]; p < stop; p++)
500 num_states = MAX<unsigned int> (num_states, p->newState + 1);
506 *num_entries_out = num_entries;
512 HBUINT32 nClasses; /* Number of classes, which is the number of indices
513 * in a single line in the state array. */
514 LOffsetTo<Lookup<HBUINT16> >
515 classTable; /* Offset to the class table. */
516 LOffsetTo<UnsizedArrayOf<HBUINT16> >
517 stateArrayTable;/* Offset to the state array. */
518 LOffsetTo<UnsizedArrayOf<Entry<Extra> > >
519 entryTable; /* Offset to the entry array. */
522 DEFINE_SIZE_STATIC (16);
525 template <typename EntryData>
526 struct StateTableDriver
528 inline StateTableDriver (const StateTable<EntryData> &machine_,
529 hb_buffer_t *buffer_,
533 num_glyphs (face_->get_num_glyphs ()) {}
535 template <typename context_t>
536 inline void drive (context_t *c)
538 hb_glyph_info_t *info = buffer->info;
541 buffer->clear_output ();
543 unsigned int state = 0;
544 bool last_was_dont_advance = false;
545 for (buffer->idx = 0;;)
547 unsigned int klass = buffer->idx < buffer->len ?
548 machine.get_class (info[buffer->idx].codepoint, num_glyphs) :
550 const Entry<EntryData> *entry = machine.get_entryZ (state, klass);
551 if (unlikely (!entry))
554 /* Unsafe-to-break before this if not in state 0, as things might
555 * go differently if we start from state 0 here. */
556 if (state && buffer->idx)
558 /* If there's no action and we're just epsilon-transitioning to state 0,
560 if (c->is_actionable (this, entry) ||
561 !(entry->newState == 0 && entry->flags == context_t::DontAdvance))
562 buffer->unsafe_to_break (buffer->idx - 1, buffer->idx + 1);
565 /* Unsafe-to-break if end-of-text would kick in here. */
566 if (buffer->idx + 2 <= buffer->len)
568 const Entry<EntryData> *end_entry = machine.get_entryZ (state, 0);
569 if (c->is_actionable (this, end_entry))
570 buffer->unsafe_to_break (buffer->idx, buffer->idx + 2);
573 if (unlikely (!c->transition (this, entry)))
576 last_was_dont_advance = (entry->flags & context_t::DontAdvance) && buffer->max_ops-- > 0;
578 state = entry->newState;
580 if (buffer->idx == buffer->len)
583 if (!last_was_dont_advance)
584 buffer->next_glyph ();
589 for (; buffer->idx < buffer->len;)
590 buffer->next_glyph ();
591 buffer->swap_buffers ();
596 const StateTable<EntryData> &machine;
598 unsigned int num_glyphs;
603 struct hb_aat_apply_context_t :
604 hb_dispatch_context_t<hb_aat_apply_context_t, bool, HB_DEBUG_APPLY>
606 inline const char *get_name (void) { return "APPLY"; }
607 template <typename T>
608 inline return_t dispatch (const T &obj) { return obj.apply (this); }
609 static return_t default_return_value (void) { return false; }
610 bool stop_sublookup_iteration (return_t r) const { return r; }
615 hb_sanitize_context_t sanitizer;
617 /* Unused. For debug tracing only. */
618 unsigned int lookup_index;
619 unsigned int debug_depth;
621 inline hb_aat_apply_context_t (hb_font_t *font_,
622 hb_buffer_t *buffer_,
624 font (font_), face (font->face), buffer (buffer_),
625 sanitizer (), lookup_index (0), debug_depth (0)
627 sanitizer.init (table);
628 sanitizer.num_glyphs = face->get_num_glyphs ();
629 sanitizer.start_processing ();
632 inline void set_lookup_index (unsigned int i) { lookup_index = i; }
634 inline ~hb_aat_apply_context_t (void)
636 sanitizer.end_processing ();
641 } /* namespace AAT */
644 #endif /* HB_AAT_LAYOUT_COMMON_PRIVATE_HH */