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_HH
28 #define HB_AAT_LAYOUT_COMMON_HH
30 #include "hb-aat-layout.hh"
31 #include "hb-aat-map.hh"
32 #include "hb-open-type.hh"
45 struct hb_aat_apply_context_t :
46 hb_dispatch_context_t<hb_aat_apply_context_t, bool, HB_DEBUG_APPLY>
48 const char *get_name () { return "APPLY"; }
50 return_t dispatch (const T &obj) { return obj.apply (this); }
51 static return_t default_return_value () { return false; }
52 bool stop_sublookup_iteration (return_t r) const { return r; }
54 const hb_ot_shape_plan_t *plan;
58 hb_sanitize_context_t sanitizer;
59 const ankr *ankr_table;
60 const OT::GDEF *gdef_table;
61 const hb_sorted_vector_t<hb_aat_map_t::range_flags_t> *range_flags = nullptr;
62 hb_mask_t subtable_flags = 0;
64 /* Unused. For debug tracing only. */
65 unsigned int lookup_index;
67 HB_INTERNAL hb_aat_apply_context_t (const hb_ot_shape_plan_t *plan_,
70 hb_blob_t *blob = const_cast<hb_blob_t *> (&Null (hb_blob_t)));
72 HB_INTERNAL ~hb_aat_apply_context_t ();
74 HB_INTERNAL void set_ankr_table (const AAT::ankr *ankr_table_);
76 void set_lookup_index (unsigned int i) { lookup_index = i; }
84 template <typename T> struct Lookup;
89 friend struct Lookup<T>;
92 const T* get_value (hb_codepoint_t glyph_id, unsigned int num_glyphs) const
94 if (unlikely (glyph_id >= num_glyphs)) return nullptr;
95 return &arrayZ[glyph_id];
98 bool sanitize (hb_sanitize_context_t *c) const
100 TRACE_SANITIZE (this);
101 return_trace (arrayZ.sanitize (c, c->get_num_glyphs ()));
103 bool sanitize (hb_sanitize_context_t *c, const void *base) const
105 TRACE_SANITIZE (this);
106 return_trace (arrayZ.sanitize (c, c->get_num_glyphs (), base));
110 HBUINT16 format; /* Format identifier--format = 0 */
112 arrayZ; /* Array of lookup values, indexed by glyph index. */
114 DEFINE_SIZE_UNBOUNDED (2);
118 template <typename T>
119 struct LookupSegmentSingle
121 static constexpr unsigned TerminationWordCount = 2u;
123 int cmp (hb_codepoint_t g) const
124 { return g < first ? -1 : g <= last ? 0 : +1 ; }
126 bool sanitize (hb_sanitize_context_t *c) const
128 TRACE_SANITIZE (this);
129 return_trace (c->check_struct (this) && value.sanitize (c));
131 bool sanitize (hb_sanitize_context_t *c, const void *base) const
133 TRACE_SANITIZE (this);
134 return_trace (c->check_struct (this) && value.sanitize (c, base));
137 HBGlyphID16 last; /* Last GlyphID in this segment */
138 HBGlyphID16 first; /* First GlyphID in this segment */
139 T value; /* The lookup value (only one) */
141 DEFINE_SIZE_STATIC (4 + T::static_size);
144 template <typename T>
147 friend struct Lookup<T>;
150 const T* get_value (hb_codepoint_t glyph_id) const
152 const LookupSegmentSingle<T> *v = segments.bsearch (glyph_id);
153 return v ? &v->value : nullptr;
156 bool sanitize (hb_sanitize_context_t *c) const
158 TRACE_SANITIZE (this);
159 return_trace (segments.sanitize (c));
161 bool sanitize (hb_sanitize_context_t *c, const void *base) const
163 TRACE_SANITIZE (this);
164 return_trace (segments.sanitize (c, base));
168 HBUINT16 format; /* Format identifier--format = 2 */
169 VarSizedBinSearchArrayOf<LookupSegmentSingle<T>>
170 segments; /* The actual segments. These must already be sorted,
171 * according to the first word in each one (the last
172 * glyph in each segment). */
174 DEFINE_SIZE_ARRAY (8, segments);
177 template <typename T>
178 struct LookupSegmentArray
180 static constexpr unsigned TerminationWordCount = 2u;
182 const T* get_value (hb_codepoint_t glyph_id, const void *base) const
184 return first <= glyph_id && glyph_id <= last ? &(base+valuesZ)[glyph_id - first] : nullptr;
187 int cmp (hb_codepoint_t g) const
188 { return g < first ? -1 : g <= last ? 0 : +1; }
190 bool sanitize (hb_sanitize_context_t *c, const void *base) const
192 TRACE_SANITIZE (this);
193 return_trace (c->check_struct (this) &&
195 valuesZ.sanitize (c, base, last - first + 1));
197 template <typename ...Ts>
198 bool sanitize (hb_sanitize_context_t *c, const void *base, Ts&&... ds) const
200 TRACE_SANITIZE (this);
201 return_trace (c->check_struct (this) &&
203 valuesZ.sanitize (c, base, last - first + 1, std::forward<Ts> (ds)...));
206 HBGlyphID16 last; /* Last GlyphID in this segment */
207 HBGlyphID16 first; /* First GlyphID in this segment */
208 NNOffset16To<UnsizedArrayOf<T>>
209 valuesZ; /* A 16-bit offset from the start of
210 * the table to the data. */
212 DEFINE_SIZE_STATIC (6);
215 template <typename T>
218 friend struct Lookup<T>;
221 const T* get_value (hb_codepoint_t glyph_id) const
223 const LookupSegmentArray<T> *v = segments.bsearch (glyph_id);
224 return v ? v->get_value (glyph_id, this) : nullptr;
227 bool sanitize (hb_sanitize_context_t *c) const
229 TRACE_SANITIZE (this);
230 return_trace (segments.sanitize (c, this));
232 bool sanitize (hb_sanitize_context_t *c, const void *base) const
234 TRACE_SANITIZE (this);
235 return_trace (segments.sanitize (c, this, base));
239 HBUINT16 format; /* Format identifier--format = 4 */
240 VarSizedBinSearchArrayOf<LookupSegmentArray<T>>
241 segments; /* The actual segments. These must already be sorted,
242 * according to the first word in each one (the last
243 * glyph in each segment). */
245 DEFINE_SIZE_ARRAY (8, segments);
248 template <typename T>
251 static constexpr unsigned TerminationWordCount = 1u;
253 int cmp (hb_codepoint_t g) const { return glyph.cmp (g); }
255 bool sanitize (hb_sanitize_context_t *c) const
257 TRACE_SANITIZE (this);
258 return_trace (c->check_struct (this) && value.sanitize (c));
260 bool sanitize (hb_sanitize_context_t *c, const void *base) const
262 TRACE_SANITIZE (this);
263 return_trace (c->check_struct (this) && value.sanitize (c, base));
266 HBGlyphID16 glyph; /* Last GlyphID */
267 T value; /* The lookup value (only one) */
269 DEFINE_SIZE_STATIC (2 + T::static_size);
272 template <typename T>
275 friend struct Lookup<T>;
278 const T* get_value (hb_codepoint_t glyph_id) const
280 const LookupSingle<T> *v = entries.bsearch (glyph_id);
281 return v ? &v->value : nullptr;
284 bool sanitize (hb_sanitize_context_t *c) const
286 TRACE_SANITIZE (this);
287 return_trace (entries.sanitize (c));
289 bool sanitize (hb_sanitize_context_t *c, const void *base) const
291 TRACE_SANITIZE (this);
292 return_trace (entries.sanitize (c, base));
296 HBUINT16 format; /* Format identifier--format = 6 */
297 VarSizedBinSearchArrayOf<LookupSingle<T>>
298 entries; /* The actual entries, sorted by glyph index. */
300 DEFINE_SIZE_ARRAY (8, entries);
303 template <typename T>
306 friend struct Lookup<T>;
309 const T* get_value (hb_codepoint_t glyph_id) const
311 return firstGlyph <= glyph_id && glyph_id - firstGlyph < glyphCount ?
312 &valueArrayZ[glyph_id - firstGlyph] : nullptr;
315 bool sanitize (hb_sanitize_context_t *c) const
317 TRACE_SANITIZE (this);
318 return_trace (c->check_struct (this) && valueArrayZ.sanitize (c, glyphCount));
320 bool sanitize (hb_sanitize_context_t *c, const void *base) const
322 TRACE_SANITIZE (this);
323 return_trace (c->check_struct (this) && valueArrayZ.sanitize (c, glyphCount, base));
327 HBUINT16 format; /* Format identifier--format = 8 */
328 HBGlyphID16 firstGlyph; /* First glyph index included in the trimmed array. */
329 HBUINT16 glyphCount; /* Total number of glyphs (equivalent to the last
330 * glyph minus the value of firstGlyph plus 1). */
332 valueArrayZ; /* The lookup values (indexed by the glyph index
333 * minus the value of firstGlyph). */
335 DEFINE_SIZE_ARRAY (6, valueArrayZ);
338 template <typename T>
339 struct LookupFormat10
341 friend struct Lookup<T>;
344 const typename T::type get_value_or_null (hb_codepoint_t glyph_id) const
346 if (!(firstGlyph <= glyph_id && glyph_id - firstGlyph < glyphCount))
349 const HBUINT8 *p = &valueArrayZ[(glyph_id - firstGlyph) * valueSize];
352 unsigned int count = valueSize;
353 for (unsigned int i = 0; i < count; i++)
359 bool sanitize (hb_sanitize_context_t *c) const
361 TRACE_SANITIZE (this);
362 return_trace (c->check_struct (this) &&
364 valueArrayZ.sanitize (c, glyphCount * valueSize));
368 HBUINT16 format; /* Format identifier--format = 8 */
369 HBUINT16 valueSize; /* Byte size of each value. */
370 HBGlyphID16 firstGlyph; /* First glyph index included in the trimmed array. */
371 HBUINT16 glyphCount; /* Total number of glyphs (equivalent to the last
372 * glyph minus the value of firstGlyph plus 1). */
373 UnsizedArrayOf<HBUINT8>
374 valueArrayZ; /* The lookup values (indexed by the glyph index
375 * minus the value of firstGlyph). */
377 DEFINE_SIZE_ARRAY (8, valueArrayZ);
380 template <typename T>
383 const T* get_value (hb_codepoint_t glyph_id, unsigned int num_glyphs) const
386 case 0: return u.format0.get_value (glyph_id, num_glyphs);
387 case 2: return u.format2.get_value (glyph_id);
388 case 4: return u.format4.get_value (glyph_id);
389 case 6: return u.format6.get_value (glyph_id);
390 case 8: return u.format8.get_value (glyph_id);
391 default:return nullptr;
395 const typename T::type get_value_or_null (hb_codepoint_t glyph_id, unsigned int num_glyphs) const
398 /* Format 10 cannot return a pointer. */
399 case 10: return u.format10.get_value_or_null (glyph_id);
401 const T *v = get_value (glyph_id, num_glyphs);
402 return v ? *v : Null (T);
406 typename T::type get_class (hb_codepoint_t glyph_id,
407 unsigned int num_glyphs,
408 unsigned int outOfRange) const
410 const T *v = get_value (glyph_id, num_glyphs);
411 return v ? *v : outOfRange;
414 bool sanitize (hb_sanitize_context_t *c) const
416 TRACE_SANITIZE (this);
417 if (!u.format.sanitize (c)) return_trace (false);
419 case 0: return_trace (u.format0.sanitize (c));
420 case 2: return_trace (u.format2.sanitize (c));
421 case 4: return_trace (u.format4.sanitize (c));
422 case 6: return_trace (u.format6.sanitize (c));
423 case 8: return_trace (u.format8.sanitize (c));
424 case 10: return_trace (u.format10.sanitize (c));
425 default:return_trace (true);
428 bool sanitize (hb_sanitize_context_t *c, const void *base) const
430 TRACE_SANITIZE (this);
431 if (!u.format.sanitize (c)) return_trace (false);
433 case 0: return_trace (u.format0.sanitize (c, base));
434 case 2: return_trace (u.format2.sanitize (c, base));
435 case 4: return_trace (u.format4.sanitize (c, base));
436 case 6: return_trace (u.format6.sanitize (c, base));
437 case 8: return_trace (u.format8.sanitize (c, base));
438 case 10: return_trace (false); /* We don't support format10 here currently. */
439 default:return_trace (true);
445 HBUINT16 format; /* Format identifier */
446 LookupFormat0<T> format0;
447 LookupFormat2<T> format2;
448 LookupFormat4<T> format4;
449 LookupFormat6<T> format6;
450 LookupFormat8<T> format8;
451 LookupFormat10<T> format10;
454 DEFINE_SIZE_UNION (2, format);
456 DECLARE_NULL_NAMESPACE_BYTES_TEMPLATE1 (AAT, Lookup, 2);
458 enum { DELETED_GLYPH = 0xFFFF };
461 * (Extended) State Table
464 template <typename T>
467 // This does seem like it's ever called.
468 bool sanitize (hb_sanitize_context_t *c) const
470 TRACE_SANITIZE (this);
471 /* Note, we don't recurse-sanitize data because we don't access it.
472 * That said, in our DEFINE_SIZE_STATIC we access T::static_size,
473 * which ensures that data has a simple sanitize(). To be determined
474 * if I need to remove that as well.
476 * HOWEVER! Because we are a template, our DEFINE_SIZE_STATIC
477 * assertion wouldn't be checked, hence the line below. */
478 static_assert (T::static_size, "");
480 return_trace (c->check_struct (this));
484 HBUINT16 newState; /* Byte offset from beginning of state table
485 * to the new state. Really?!?! Or just state
486 * number? The latter in morx for sure. */
487 HBUINT16 flags; /* Table specific. */
488 T data; /* Optional offsets to per-glyph tables. */
490 DEFINE_SIZE_STATIC (4 + T::static_size);
496 // This does seem like it's ever called.
497 bool sanitize (hb_sanitize_context_t *c) const
499 TRACE_SANITIZE (this);
500 return_trace (c->check_struct (this));
504 HBUINT16 newState; /* Byte offset from beginning of state table to the new state. */
505 HBUINT16 flags; /* Table specific. */
507 DEFINE_SIZE_STATIC (4);
510 template <typename Types, typename Extra>
513 typedef typename Types::HBUINT HBUINT;
514 typedef typename Types::HBUSHORT HBUSHORT;
515 typedef typename Types::ClassTypeNarrow ClassType;
519 STATE_START_OF_TEXT = 0,
520 STATE_START_OF_LINE = 1,
524 CLASS_END_OF_TEXT = 0,
525 CLASS_OUT_OF_BOUNDS = 1,
526 CLASS_DELETED_GLYPH = 2,
527 CLASS_END_OF_LINE = 3,
530 int new_state (unsigned int newState) const
531 { return Types::extended ? newState : ((int) newState - (int) stateArrayTable) / (int) nClasses; }
533 unsigned int get_class (hb_codepoint_t glyph_id, unsigned int num_glyphs) const
535 if (unlikely (glyph_id == DELETED_GLYPH)) return CLASS_DELETED_GLYPH;
536 return (this+classTable).get_class (glyph_id, num_glyphs, 1);
539 const Entry<Extra> *get_entries () const
540 { return (this+entryTable).arrayZ; }
542 const Entry<Extra> &get_entry (int state, unsigned int klass) const
544 if (unlikely (klass >= nClasses))
545 klass = StateTable::CLASS_OUT_OF_BOUNDS;
547 const HBUSHORT *states = (this+stateArrayTable).arrayZ;
548 const Entry<Extra> *entries = (this+entryTable).arrayZ;
550 unsigned int entry = states[state * nClasses + klass];
551 DEBUG_MSG (APPLY, nullptr, "e%u", entry);
553 return entries[entry];
556 bool sanitize (hb_sanitize_context_t *c,
557 unsigned int *num_entries_out = nullptr) const
559 TRACE_SANITIZE (this);
560 if (unlikely (!(c->check_struct (this) &&
561 nClasses >= 4 /* Ensure pre-defined classes fit. */ &&
562 classTable.sanitize (c, this)))) return_trace (false);
564 const HBUSHORT *states = (this+stateArrayTable).arrayZ;
565 const Entry<Extra> *entries = (this+entryTable).arrayZ;
567 unsigned int num_classes = nClasses;
568 if (unlikely (hb_unsigned_mul_overflows (num_classes, states[0].static_size)))
569 return_trace (false);
570 unsigned int row_stride = num_classes * states[0].static_size;
572 /* Apple 'kern' table has this peculiarity:
574 * "Because the stateTableOffset in the state table header is (strictly
575 * speaking) redundant, some 'kern' tables use it to record an initial
576 * state where that should not be StartOfText. To determine if this is
577 * done, calculate what the stateTableOffset should be. If it's different
578 * from the actual stateTableOffset, use it as the initial state."
580 * We implement this by calling the initial state zero, but allow *negative*
581 * states if the start state indeed was not the first state. Since the code
582 * is shared, this will also apply to 'mort' table. The 'kerx' / 'morx'
583 * tables are not affected since those address states by index, not offset.
588 unsigned int num_entries = 0;
592 unsigned int entry = 0;
593 while (min_state < state_neg || state_pos <= max_state)
595 if (min_state < state_neg)
597 /* Negative states. */
598 if (unlikely (hb_unsigned_mul_overflows (min_state, num_classes)))
599 return_trace (false);
600 if (unlikely (!c->check_range (&states[min_state * num_classes],
603 return_trace (false);
604 if ((c->max_ops -= state_neg - min_state) <= 0)
605 return_trace (false);
606 { /* Sweep new states. */
607 const HBUSHORT *stop = &states[min_state * num_classes];
608 if (unlikely (stop > states))
609 return_trace (false);
610 for (const HBUSHORT *p = states; stop < p; p--)
611 num_entries = hb_max (num_entries, *(p - 1) + 1u);
612 state_neg = min_state;
616 if (state_pos <= max_state)
618 /* Positive states. */
619 if (unlikely (!c->check_range (states,
622 return_trace (false);
623 if ((c->max_ops -= max_state - state_pos + 1) <= 0)
624 return_trace (false);
625 { /* Sweep new states. */
626 if (unlikely (hb_unsigned_mul_overflows ((max_state + 1), num_classes)))
627 return_trace (false);
628 const HBUSHORT *stop = &states[(max_state + 1) * num_classes];
629 if (unlikely (stop < states))
630 return_trace (false);
631 for (const HBUSHORT *p = &states[state_pos * num_classes]; p < stop; p++)
632 num_entries = hb_max (num_entries, *p + 1u);
633 state_pos = max_state + 1;
637 if (unlikely (!c->check_array (entries, num_entries)))
638 return_trace (false);
639 if ((c->max_ops -= num_entries - entry) <= 0)
640 return_trace (false);
641 { /* Sweep new entries. */
642 const Entry<Extra> *stop = &entries[num_entries];
643 for (const Entry<Extra> *p = &entries[entry]; p < stop; p++)
645 int newState = new_state (p->newState);
646 min_state = hb_min (min_state, newState);
647 max_state = hb_max (max_state, newState);
654 *num_entries_out = num_entries;
660 HBUINT nClasses; /* Number of classes, which is the number of indices
661 * in a single line in the state array. */
662 NNOffsetTo<ClassType, HBUINT>
663 classTable; /* Offset to the class table. */
664 NNOffsetTo<UnsizedArrayOf<HBUSHORT>, HBUINT>
665 stateArrayTable;/* Offset to the state array. */
666 NNOffsetTo<UnsizedArrayOf<Entry<Extra>>, HBUINT>
667 entryTable; /* Offset to the entry array. */
670 DEFINE_SIZE_STATIC (4 * sizeof (HBUINT));
673 template <typename HBUCHAR>
676 unsigned int get_class (hb_codepoint_t glyph_id, unsigned int outOfRange) const
678 unsigned int i = glyph_id - firstGlyph;
679 return i >= classArray.len ? outOfRange : classArray.arrayZ[i];
681 unsigned int get_class (hb_codepoint_t glyph_id,
682 unsigned int num_glyphs HB_UNUSED,
683 unsigned int outOfRange) const
685 return get_class (glyph_id, outOfRange);
687 bool sanitize (hb_sanitize_context_t *c) const
689 TRACE_SANITIZE (this);
690 return_trace (c->check_struct (this) && classArray.sanitize (c));
693 HBGlyphID16 firstGlyph; /* First glyph index included in the trimmed array. */
694 Array16Of<HBUCHAR> classArray; /* The class codes (indexed by glyph index minus
697 DEFINE_SIZE_ARRAY (4, classArray);
702 static constexpr bool extended = false;
703 typedef HBUINT16 HBUINT;
704 typedef HBUINT8 HBUSHORT;
705 typedef ClassTable<HBUINT8> ClassTypeNarrow;
706 typedef ClassTable<HBUINT16> ClassTypeWide;
708 template <typename T>
709 static unsigned int offsetToIndex (unsigned int offset,
713 /* https://github.com/harfbuzz/harfbuzz/issues/3483 */
714 /* If offset is less than base, return an offset that would
715 * result in an address half a 32bit address-space away,
716 * to make sure sanitize fails even on 32bit builds. */
717 if (unlikely (offset < unsigned ((const char *) array - (const char *) base)))
718 return INT_MAX / T::static_size;
720 /* https://github.com/harfbuzz/harfbuzz/issues/2816 */
721 return (offset - unsigned ((const char *) array - (const char *) base)) / T::static_size;
723 template <typename T>
724 static unsigned int byteOffsetToIndex (unsigned int offset,
728 return offsetToIndex (offset, base, array);
730 template <typename T>
731 static unsigned int wordOffsetToIndex (unsigned int offset,
735 return offsetToIndex (2 * offset, base, array);
740 static constexpr bool extended = true;
741 typedef HBUINT32 HBUINT;
742 typedef HBUINT16 HBUSHORT;
743 typedef Lookup<HBUINT16> ClassTypeNarrow;
744 typedef Lookup<HBUINT16> ClassTypeWide;
746 template <typename T>
747 static unsigned int offsetToIndex (unsigned int offset,
748 const void *base HB_UNUSED,
749 const T *array HB_UNUSED)
753 template <typename T>
754 static unsigned int byteOffsetToIndex (unsigned int offset,
755 const void *base HB_UNUSED,
756 const T *array HB_UNUSED)
760 template <typename T>
761 static unsigned int wordOffsetToIndex (unsigned int offset,
762 const void *base HB_UNUSED,
763 const T *array HB_UNUSED)
769 template <typename Types, typename EntryData>
770 struct StateTableDriver
772 using StateTableT = StateTable<Types, EntryData>;
773 using EntryT = Entry<EntryData>;
775 StateTableDriver (const StateTableT &machine_,
776 hb_buffer_t *buffer_,
780 num_glyphs (face_->get_num_glyphs ()) {}
782 template <typename context_t>
783 void drive (context_t *c, hb_aat_apply_context_t *ac)
786 buffer->clear_output ();
788 int state = StateTableT::STATE_START_OF_TEXT;
789 // If there's only one range, we already checked the flag.
790 auto *last_range = ac->range_flags && (ac->range_flags->length > 1) ? &(*ac->range_flags)[0] : nullptr;
791 for (buffer->idx = 0; buffer->successful;)
793 /* This block is copied in NoncontextualSubtable::apply. Keep in sync. */
796 auto *range = last_range;
797 if (buffer->idx < buffer->len)
799 unsigned cluster = buffer->cur().cluster;
800 while (cluster < range->cluster_first)
802 while (cluster > range->cluster_last)
808 if (!(range->flags & ac->subtable_flags))
810 if (buffer->idx == buffer->len || unlikely (!buffer->successful))
813 state = StateTableT::STATE_START_OF_TEXT;
814 (void) buffer->next_glyph ();
819 unsigned int klass = buffer->idx < buffer->len ?
820 machine.get_class (buffer->cur().codepoint, num_glyphs) :
821 (unsigned) StateTableT::CLASS_END_OF_TEXT;
822 DEBUG_MSG (APPLY, nullptr, "c%u at %u", klass, buffer->idx);
823 const EntryT &entry = machine.get_entry (state, klass);
824 const int next_state = machine.new_state (entry.newState);
826 /* Conditions under which it's guaranteed safe-to-break before current glyph:
828 * 1. There was no action in this transition; and
830 * 2. If we break before current glyph, the results will be the same. That
833 * 2a. We were already in start-of-text state; or
835 * 2b. We are epsilon-transitioning to start-of-text state; or
837 * 2c. Starting from start-of-text state seeing current glyph:
839 * 2c'. There won't be any actions; and
841 * 2c". We would end up in the same state that we were going to end up
842 * in now, including whether epsilon-transitioning.
846 * 3. If we break before current glyph, there won't be any end-of-text action
847 * after previous glyph.
849 * This triples the transitions we need to look up, but is worth returning
850 * granular unsafe-to-break results. See eg.:
852 * https://github.com/harfbuzz/harfbuzz/issues/2860
855 const auto is_safe_to_break_extra = [&]()
858 const auto wouldbe_entry = machine.get_entry(StateTableT::STATE_START_OF_TEXT, klass);
861 if (c->is_actionable (this, wouldbe_entry))
865 return next_state == machine.new_state(wouldbe_entry.newState)
866 && (entry.flags & context_t::DontAdvance) == (wouldbe_entry.flags & context_t::DontAdvance);
869 const auto is_safe_to_break = [&]()
872 if (c->is_actionable (this, entry))
876 // This one is meh, I know...
878 state == StateTableT::STATE_START_OF_TEXT
879 || ((entry.flags & context_t::DontAdvance) && next_state == StateTableT::STATE_START_OF_TEXT)
880 || is_safe_to_break_extra();
885 return !c->is_actionable (this, machine.get_entry (state, StateTableT::CLASS_END_OF_TEXT));
888 if (!is_safe_to_break () && buffer->backtrack_len () && buffer->idx < buffer->len)
889 buffer->unsafe_to_break_from_outbuffer (buffer->backtrack_len () - 1, buffer->idx + 1);
891 c->transition (this, entry);
894 DEBUG_MSG (APPLY, nullptr, "s%d", state);
896 if (buffer->idx == buffer->len || unlikely (!buffer->successful))
899 if (!(entry.flags & context_t::DontAdvance) || buffer->max_ops-- <= 0)
900 (void) buffer->next_glyph ();
908 const StateTableT &machine;
910 unsigned int num_glyphs;
914 } /* namespace AAT */
917 #endif /* HB_AAT_LAYOUT_COMMON_HH */