2 * Copyright © 2007,2008,2009,2010 Red Hat, Inc.
3 * Copyright © 2010,2012 Google, Inc.
5 * This is part of HarfBuzz, a text shaping library.
7 * Permission is hereby granted, without written agreement and without
8 * license or royalty fees, to use, copy, modify, and distribute this
9 * software and its documentation for any purpose, provided that the
10 * above copyright notice and the following two paragraphs appear in
11 * all copies of this software.
13 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
14 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
15 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
16 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
19 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
20 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
22 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
23 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
25 * Red Hat Author(s): Behdad Esfahbod
26 * Google Author(s): Behdad Esfahbod
29 #ifndef HB_OT_LAYOUT_GSUBGPOS_PRIVATE_HH
30 #define HB_OT_LAYOUT_GSUBGPOS_PRIVATE_HH
32 #include "hb-buffer-private.hh"
33 #include "hb-ot-layout-gdef-table.hh"
34 #include "hb-set-private.hh"
41 #define TRACE_PROCESS(this) \
42 hb_auto_trace_t<context_t::max_debug_depth, typename context_t::return_t> trace \
43 (&c->debug_depth, c->get_name (), this, HB_FUNC, \
47 #ifndef HB_DEBUG_CLOSURE
48 #define HB_DEBUG_CLOSURE (HB_DEBUG+0)
51 #define TRACE_CLOSURE(this) \
52 hb_auto_trace_t<HB_DEBUG_CLOSURE, hb_void_t> trace \
53 (&c->debug_depth, c->get_name (), this, HB_FUNC, \
56 struct hb_closure_context_t
58 inline const char *get_name (void) { return "CLOSURE"; }
59 static const unsigned int max_debug_depth = HB_DEBUG_CLOSURE;
60 typedef hb_void_t return_t;
61 typedef return_t (*recurse_func_t) (hb_closure_context_t *c, unsigned int lookup_index);
63 inline return_t process (const T &obj) { obj.closure (this); return HB_VOID; }
64 static return_t default_return_value (void) { return HB_VOID; }
65 bool stop_sublookup_iteration (return_t r HB_UNUSED) const { return false; }
66 return_t recurse (unsigned int lookup_index)
68 if (unlikely (nesting_level_left == 0 || !recurse_func))
69 return default_return_value ();
72 recurse_func (this, lookup_index);
79 recurse_func_t recurse_func;
80 unsigned int nesting_level_left;
81 unsigned int debug_depth;
83 hb_closure_context_t (hb_face_t *face_,
85 unsigned int nesting_level_left_ = MAX_NESTING_LEVEL) :
89 nesting_level_left (nesting_level_left_),
92 void set_recurse_func (recurse_func_t func) { recurse_func = func; }
97 #ifndef HB_DEBUG_WOULD_APPLY
98 #define HB_DEBUG_WOULD_APPLY (HB_DEBUG+0)
101 #define TRACE_WOULD_APPLY(this) \
102 hb_auto_trace_t<HB_DEBUG_WOULD_APPLY, bool> trace \
103 (&c->debug_depth, c->get_name (), this, HB_FUNC, \
104 "%d glyphs", c->len);
106 struct hb_would_apply_context_t
108 inline const char *get_name (void) { return "WOULD_APPLY"; }
109 static const unsigned int max_debug_depth = HB_DEBUG_WOULD_APPLY;
110 typedef bool return_t;
111 template <typename T>
112 inline return_t process (const T &obj) { return obj.would_apply (this); }
113 static return_t default_return_value (void) { return false; }
114 bool stop_sublookup_iteration (return_t r) const { return r; }
117 const hb_codepoint_t *glyphs;
120 unsigned int debug_depth;
122 hb_would_apply_context_t (hb_face_t *face_,
123 const hb_codepoint_t *glyphs_,
125 bool zero_context_) :
129 zero_context (zero_context_),
135 #ifndef HB_DEBUG_COLLECT_GLYPHS
136 #define HB_DEBUG_COLLECT_GLYPHS (HB_DEBUG+0)
139 #define TRACE_COLLECT_GLYPHS(this) \
140 hb_auto_trace_t<HB_DEBUG_COLLECT_GLYPHS, hb_void_t> trace \
141 (&c->debug_depth, c->get_name (), this, HB_FUNC, \
144 struct hb_collect_glyphs_context_t
146 inline const char *get_name (void) { return "COLLECT_GLYPHS"; }
147 static const unsigned int max_debug_depth = HB_DEBUG_COLLECT_GLYPHS;
148 typedef hb_void_t return_t;
149 typedef return_t (*recurse_func_t) (hb_collect_glyphs_context_t *c, unsigned int lookup_index);
150 template <typename T>
151 inline return_t process (const T &obj) { obj.collect_glyphs (this); return HB_VOID; }
152 static return_t default_return_value (void) { return HB_VOID; }
153 bool stop_sublookup_iteration (return_t r HB_UNUSED) const { return false; }
154 return_t recurse (unsigned int lookup_index)
156 if (unlikely (nesting_level_left == 0 || !recurse_func))
157 return default_return_value ();
159 /* Note that GPOS sets recurse_func to NULL already, so it doesn't get
160 * past the previous check. For GSUB, we only want to collect the output
161 * glyphs in the recursion. If output is not requested, we can go home now. */
163 if (output == hb_set_get_empty ())
166 hb_set_t *old_before = before;
167 hb_set_t *old_input = input;
168 hb_set_t *old_after = after;
169 before = input = after = hb_set_get_empty ();
171 nesting_level_left--;
172 recurse_func (this, lookup_index);
173 nesting_level_left++;
187 recurse_func_t recurse_func;
188 unsigned int nesting_level_left;
189 unsigned int debug_depth;
191 hb_collect_glyphs_context_t (hb_face_t *face_,
192 hb_set_t *glyphs_before, /* OUT. May be NULL */
193 hb_set_t *glyphs_input, /* OUT. May be NULL */
194 hb_set_t *glyphs_after, /* OUT. May be NULL */
195 hb_set_t *glyphs_output, /* OUT. May be NULL */
196 unsigned int nesting_level_left_ = MAX_NESTING_LEVEL) :
198 before (glyphs_before ? glyphs_before : hb_set_get_empty ()),
199 input (glyphs_input ? glyphs_input : hb_set_get_empty ()),
200 after (glyphs_after ? glyphs_after : hb_set_get_empty ()),
201 output (glyphs_output ? glyphs_output : hb_set_get_empty ()),
203 nesting_level_left (nesting_level_left_),
206 void set_recurse_func (recurse_func_t func) { recurse_func = func; }
211 struct hb_get_coverage_context_t
213 inline const char *get_name (void) { return "GET_COVERAGE"; }
214 static const unsigned int max_debug_depth = 0;
215 typedef const Coverage &return_t;
216 template <typename T>
217 inline return_t process (const T &obj) { return obj.get_coverage (); }
218 static return_t default_return_value (void) { return Null(Coverage); }
220 hb_get_coverage_context_t (void) :
223 unsigned int debug_depth;
228 #ifndef HB_DEBUG_APPLY
229 #define HB_DEBUG_APPLY (HB_DEBUG+0)
232 #define TRACE_APPLY(this) \
233 hb_auto_trace_t<HB_DEBUG_APPLY, bool> trace \
234 (&c->debug_depth, c->get_name (), this, HB_FUNC, \
235 "idx %d codepoint %u", c->buffer->idx, c->buffer->cur().codepoint);
237 struct hb_apply_context_t
239 inline const char *get_name (void) { return "APPLY"; }
240 static const unsigned int max_debug_depth = HB_DEBUG_APPLY;
241 typedef bool return_t;
242 typedef return_t (*recurse_func_t) (hb_apply_context_t *c, unsigned int lookup_index);
243 template <typename T>
244 inline return_t process (const T &obj) { return obj.apply (this); }
245 static return_t default_return_value (void) { return false; }
246 bool stop_sublookup_iteration (return_t r) const { return r; }
247 return_t recurse (unsigned int lookup_index)
249 if (unlikely (nesting_level_left == 0 || !recurse_func))
250 return default_return_value ();
252 nesting_level_left--;
253 bool ret = recurse_func (this, lookup_index);
254 nesting_level_left++;
261 hb_direction_t direction;
262 hb_mask_t lookup_mask;
263 recurse_func_t recurse_func;
264 unsigned int nesting_level_left;
265 unsigned int lookup_props;
266 unsigned int property; /* propety of first glyph */
268 bool has_glyph_classes;
269 unsigned int debug_depth;
272 hb_apply_context_t (hb_font_t *font_,
273 hb_buffer_t *buffer_,
274 hb_mask_t lookup_mask_) :
275 font (font_), face (font->face), buffer (buffer_),
276 direction (buffer_->props.direction),
277 lookup_mask (lookup_mask_),
279 nesting_level_left (MAX_NESTING_LEVEL),
280 lookup_props (0), property (0),
281 gdef (*hb_ot_layout_from_face (face)->gdef),
282 has_glyph_classes (gdef.has_glyph_classes ()),
285 void set_recurse_func (recurse_func_t func) { recurse_func = func; }
286 void set_lookup_props (unsigned int lookup_props_) { lookup_props = lookup_props_; }
287 void set_lookup (const Lookup &l) { lookup_props = l.get_props (); }
289 struct mark_skipping_forward_iterator_t
291 inline mark_skipping_forward_iterator_t (hb_apply_context_t *c_,
292 unsigned int start_index_,
293 unsigned int num_items_,
294 bool context_match = false)
298 num_items = num_items_;
299 mask = context_match ? -1 : c->lookup_mask;
300 syllable = context_match ? 0 : c->buffer->cur().syllable ();
301 end = c->buffer->len;
303 inline bool has_no_chance (void) const
305 return unlikely (num_items && idx + num_items >= end);
307 inline void reject (void)
311 inline bool next (unsigned int *property_out,
312 unsigned int lookup_props)
314 assert (num_items > 0);
317 if (has_no_chance ())
320 } while (c->should_skip_mark (&c->buffer->info[idx], lookup_props, property_out));
322 return (c->buffer->info[idx].mask & mask) && (!syllable || syllable == c->buffer->info[idx].syllable ());
324 inline bool next (unsigned int *property_out = NULL)
326 return next (property_out, c->lookup_props);
331 hb_apply_context_t *c;
332 unsigned int num_items;
338 struct mark_skipping_backward_iterator_t
340 inline mark_skipping_backward_iterator_t (hb_apply_context_t *c_,
341 unsigned int start_index_,
342 unsigned int num_items_,
344 bool match_syllable_ = true)
348 num_items = num_items_;
349 mask = mask_ ? mask_ : c->lookup_mask;
350 syllable = match_syllable_ ? c->buffer->cur().syllable () : 0;
352 inline bool has_no_chance (void) const
354 return unlikely (idx < num_items);
356 inline void reject (void)
360 inline bool prev (unsigned int *property_out,
361 unsigned int lookup_props)
363 assert (num_items > 0);
366 if (has_no_chance ())
369 } while (c->should_skip_mark (&c->buffer->out_info[idx], lookup_props, property_out));
371 return (c->buffer->out_info[idx].mask & mask) && (!syllable || syllable == c->buffer->out_info[idx].syllable ());
373 inline bool prev (unsigned int *property_out = NULL)
375 return prev (property_out, c->lookup_props);
380 hb_apply_context_t *c;
381 unsigned int num_items;
387 match_properties_mark (hb_codepoint_t glyph,
388 unsigned int glyph_props,
389 unsigned int lookup_props) const
391 /* If using mark filtering sets, the high short of
392 * lookup_props has the set index.
394 if (lookup_props & LookupFlag::UseMarkFilteringSet)
395 return gdef.mark_set_covers (lookup_props >> 16, glyph);
397 /* The second byte of lookup_props has the meaning
398 * "ignore marks of attachment type different than
399 * the attachment type specified."
401 if (lookup_props & LookupFlag::MarkAttachmentType)
402 return (lookup_props & LookupFlag::MarkAttachmentType) == (glyph_props & LookupFlag::MarkAttachmentType);
408 match_properties (hb_codepoint_t glyph,
409 unsigned int glyph_props,
410 unsigned int lookup_props) const
412 /* Not covered, if, for example, glyph class is ligature and
413 * lookup_props includes LookupFlags::IgnoreLigatures
415 if (glyph_props & lookup_props & LookupFlag::IgnoreFlags)
418 if (unlikely (glyph_props & HB_OT_LAYOUT_GLYPH_PROPS_MARK))
419 return match_properties_mark (glyph, glyph_props, lookup_props);
425 check_glyph_property (hb_glyph_info_t *info,
426 unsigned int lookup_props,
427 unsigned int *property_out) const
429 unsigned int property;
431 property = info->glyph_props();
432 *property_out = property;
434 return match_properties (info->codepoint, property, lookup_props);
438 should_skip_mark (hb_glyph_info_t *info,
439 unsigned int lookup_props,
440 unsigned int *property_out) const
442 unsigned int property;
444 property = info->glyph_props();
446 *property_out = property;
448 /* If it's a mark, skip it if we don't accept it. */
449 if (unlikely (property & HB_OT_LAYOUT_GLYPH_PROPS_MARK))
450 return !match_properties (info->codepoint, property, lookup_props);
452 /* If not a mark, don't skip. */
457 inline bool should_mark_skip_current_glyph (void) const
459 return should_skip_mark (&buffer->cur(), lookup_props, NULL);
462 inline void set_class (hb_codepoint_t glyph_index, unsigned int class_guess) const
464 if (likely (has_glyph_classes))
465 buffer->cur().glyph_props() = gdef.get_glyph_props (glyph_index);
466 else if (class_guess)
467 buffer->cur().glyph_props() = class_guess;
470 inline void output_glyph (hb_codepoint_t glyph_index,
471 unsigned int class_guess = 0) const
473 set_class (glyph_index, class_guess);
474 buffer->output_glyph (glyph_index);
476 inline void replace_glyph (hb_codepoint_t glyph_index,
477 unsigned int class_guess = 0) const
479 set_class (glyph_index, class_guess);
480 buffer->replace_glyph (glyph_index);
482 inline void replace_glyph_inplace (hb_codepoint_t glyph_index,
483 unsigned int class_guess = 0) const
485 set_class (glyph_index, class_guess);
486 buffer->cur().codepoint = glyph_index;
492 typedef bool (*intersects_func_t) (hb_set_t *glyphs, const USHORT &value, const void *data);
493 typedef void (*collect_glyphs_func_t) (hb_set_t *glyphs, const USHORT &value, const void *data);
494 typedef bool (*match_func_t) (hb_codepoint_t glyph_id, const USHORT &value, const void *data);
496 struct ContextClosureFuncs
498 intersects_func_t intersects;
500 struct ContextCollectGlyphsFuncs
502 collect_glyphs_func_t collect;
504 struct ContextApplyFuncs
510 static inline bool intersects_glyph (hb_set_t *glyphs, const USHORT &value, const void *data HB_UNUSED)
512 return glyphs->has (value);
514 static inline bool intersects_class (hb_set_t *glyphs, const USHORT &value, const void *data)
516 const ClassDef &class_def = *reinterpret_cast<const ClassDef *>(data);
517 return class_def.intersects_class (glyphs, value);
519 static inline bool intersects_coverage (hb_set_t *glyphs, const USHORT &value, const void *data)
521 const OffsetTo<Coverage> &coverage = (const OffsetTo<Coverage>&)value;
522 return (data+coverage).intersects (glyphs);
525 static inline bool intersects_array (hb_closure_context_t *c,
527 const USHORT values[],
528 intersects_func_t intersects_func,
529 const void *intersects_data)
531 for (unsigned int i = 0; i < count; i++)
532 if (likely (!intersects_func (c->glyphs, values[i], intersects_data)))
538 static inline void collect_glyph (hb_set_t *glyphs, const USHORT &value, const void *data HB_UNUSED)
542 static inline void collect_class (hb_set_t *glyphs, const USHORT &value, const void *data)
544 const ClassDef &class_def = *reinterpret_cast<const ClassDef *>(data);
545 class_def.add_class (glyphs, value);
547 static inline void collect_coverage (hb_set_t *glyphs, const USHORT &value, const void *data)
549 const OffsetTo<Coverage> &coverage = (const OffsetTo<Coverage>&)value;
550 (data+coverage).add_coverage (glyphs);
552 static inline void collect_array (hb_collect_glyphs_context_t *c HB_UNUSED,
555 const USHORT values[],
556 collect_glyphs_func_t collect_func,
557 const void *collect_data)
559 for (unsigned int i = 0; i < count; i++)
560 collect_func (glyphs, values[i], collect_data);
564 static inline bool match_glyph (hb_codepoint_t glyph_id, const USHORT &value, const void *data HB_UNUSED)
566 return glyph_id == value;
568 static inline bool match_class (hb_codepoint_t glyph_id, const USHORT &value, const void *data)
570 const ClassDef &class_def = *reinterpret_cast<const ClassDef *>(data);
571 return class_def.get_class (glyph_id) == value;
573 static inline bool match_coverage (hb_codepoint_t glyph_id, const USHORT &value, const void *data)
575 const OffsetTo<Coverage> &coverage = (const OffsetTo<Coverage>&)value;
576 return (data+coverage).get_coverage (glyph_id) != NOT_COVERED;
579 static inline bool would_match_input (hb_would_apply_context_t *c,
580 unsigned int count, /* Including the first glyph (not matched) */
581 const USHORT input[], /* Array of input values--start with second glyph */
582 match_func_t match_func,
583 const void *match_data)
588 for (unsigned int i = 1; i < count; i++)
589 if (likely (!match_func (c->glyphs[i], input[i - 1], match_data)))
594 static inline bool match_input (hb_apply_context_t *c,
595 unsigned int count, /* Including the first glyph (not matched) */
596 const USHORT input[], /* Array of input values--start with second glyph */
597 match_func_t match_func,
598 const void *match_data,
599 unsigned int *end_offset = NULL,
600 bool *p_is_mark_ligature = NULL,
601 unsigned int *p_total_component_count = NULL)
605 hb_apply_context_t::mark_skipping_forward_iterator_t skippy_iter (c, c->buffer->idx, count - 1);
606 if (skippy_iter.has_no_chance ()) return TRACE_RETURN (false);
609 * This is perhaps the trickiest part of OpenType... Remarks:
611 * - If all components of the ligature were marks, we call this a mark ligature.
613 * - If there is no GDEF, and the ligature is NOT a mark ligature, we categorize
614 * it as a ligature glyph.
616 * - Ligatures cannot be formed across glyphs attached to different components
617 * of previous ligatures. Eg. the sequence is LAM,SHADDA,LAM,FATHA,HEH, and
618 * LAM,LAM,HEH form a ligature, leaving SHADDA,FATHA next to eachother.
619 * However, it would be wrong to ligate that SHADDA,FATHA sequence.o
620 * There is an exception to this: If a ligature tries ligating with marks that
621 * belong to it itself, go ahead, assuming that the font designer knows what
622 * they are doing (otherwise it can break Indic stuff when a matra wants to
623 * ligate with a conjunct...)
626 bool is_mark_ligature = !!(c->property & HB_OT_LAYOUT_GLYPH_PROPS_MARK);
628 unsigned int total_component_count = 0;
629 total_component_count += get_lig_num_comps (c->buffer->cur());
631 unsigned int first_lig_id = get_lig_id (c->buffer->cur());
632 unsigned int first_lig_comp = get_lig_comp (c->buffer->cur());
634 for (unsigned int i = 1; i < count; i++)
636 unsigned int property;
638 if (!skippy_iter.next (&property)) return TRACE_RETURN (false);
640 if (likely (!match_func (c->buffer->info[skippy_iter.idx].codepoint, input[i - 1], match_data))) return TRACE_RETURN (false);
642 unsigned int this_lig_id = get_lig_id (c->buffer->info[skippy_iter.idx]);
643 unsigned int this_lig_comp = get_lig_comp (c->buffer->info[skippy_iter.idx]);
645 if (first_lig_id && first_lig_comp) {
646 /* If first component was attached to a previous ligature component,
647 * all subsequent components should be attached to the same ligature
648 * component, otherwise we shouldn't ligate them. */
649 if (first_lig_id != this_lig_id || first_lig_comp != this_lig_comp)
650 return TRACE_RETURN (false);
652 /* If first component was NOT attached to a previous ligature component,
653 * all subsequent components should also NOT be attached to any ligature
654 * component, unless they are attached to the first component itself! */
655 if (this_lig_id && this_lig_comp && (this_lig_id != first_lig_id))
656 return TRACE_RETURN (false);
659 is_mark_ligature = is_mark_ligature && (property & HB_OT_LAYOUT_GLYPH_PROPS_MARK);
660 total_component_count += get_lig_num_comps (c->buffer->info[skippy_iter.idx]);
664 *end_offset = skippy_iter.idx - c->buffer->idx + 1;
666 if (p_is_mark_ligature)
667 *p_is_mark_ligature = is_mark_ligature;
669 if (p_total_component_count)
670 *p_total_component_count = total_component_count;
672 return TRACE_RETURN (true);
674 static inline void ligate_input (hb_apply_context_t *c,
675 unsigned int count, /* Including the first glyph (not matched) */
676 const USHORT input[] HB_UNUSED, /* Array of input values--start with second glyph */
677 hb_codepoint_t lig_glyph,
678 match_func_t match_func HB_UNUSED,
679 const void *match_data HB_UNUSED,
680 bool is_mark_ligature,
681 unsigned int total_component_count)
684 * - If it *is* a mark ligature, we don't allocate a new ligature id, and leave
685 * the ligature to keep its old ligature id. This will allow it to attach to
686 * a base ligature in GPOS. Eg. if the sequence is: LAM,LAM,SHADDA,FATHA,HEH,
687 * and LAM,LAM,HEH for a ligature, they will leave SHADDA and FATHA wit a
688 * ligature id and component value of 2. Then if SHADDA,FATHA form a ligature
689 * later, we don't want them to lose their ligature id/component, otherwise
690 * GPOS will fail to correctly position the mark ligature on top of the
691 * LAM,LAM,HEH ligature. See:
692 * https://bugzilla.gnome.org/show_bug.cgi?id=676343
694 * - If a ligature is formed of components that some of which are also ligatures
695 * themselves, and those ligature components had marks attached to *their*
696 * components, we have to attach the marks to the new ligature component
697 * positions! Now *that*'s tricky! And these marks may be following the
698 * last component of the whole sequence, so we should loop forward looking
699 * for them and update them.
701 * Eg. the sequence is LAM,LAM,SHADDA,FATHA,HEH, and the font first forms a
702 * 'calt' ligature of LAM,HEH, leaving the SHADDA and FATHA with a ligature
703 * id and component == 1. Now, during 'liga', the LAM and the LAM-HEH ligature
704 * form a LAM-LAM-HEH ligature. We need to reassign the SHADDA and FATHA to
705 * the new ligature with a component value of 2.
707 * This in fact happened to a font... See:
708 * https://bugzilla.gnome.org/show_bug.cgi?id=437633
711 unsigned int klass = is_mark_ligature ? 0 : HB_OT_LAYOUT_GLYPH_PROPS_LIGATURE;
712 unsigned int lig_id = is_mark_ligature ? 0 : allocate_lig_id (c->buffer);
713 unsigned int last_lig_id = get_lig_id (c->buffer->cur());
714 unsigned int last_num_components = get_lig_num_comps (c->buffer->cur());
715 unsigned int components_so_far = last_num_components;
717 if (!is_mark_ligature)
718 set_lig_props_for_ligature (c->buffer->cur(), lig_id, total_component_count);
719 c->replace_glyph (lig_glyph, klass);
721 for (unsigned int i = 1; i < count; i++)
723 while (c->should_mark_skip_current_glyph ())
725 if (!is_mark_ligature) {
726 unsigned int new_lig_comp = components_so_far - last_num_components +
727 MIN (MAX (get_lig_comp (c->buffer->cur()), 1u), last_num_components);
728 set_lig_props_for_mark (c->buffer->cur(), lig_id, new_lig_comp);
730 c->buffer->next_glyph ();
733 last_lig_id = get_lig_id (c->buffer->cur());
734 last_num_components = get_lig_num_comps (c->buffer->cur());
735 components_so_far += last_num_components;
737 /* Skip the base glyph */
741 if (!is_mark_ligature && last_lig_id) {
742 /* Re-adjust components for any marks following. */
743 for (unsigned int i = c->buffer->idx; i < c->buffer->len; i++) {
744 if (last_lig_id == get_lig_id (c->buffer->info[i])) {
745 unsigned int new_lig_comp = components_so_far - last_num_components +
746 MIN (MAX (get_lig_comp (c->buffer->info[i]), 1u), last_num_components);
747 set_lig_props_for_mark (c->buffer->info[i], lig_id, new_lig_comp);
754 static inline bool match_backtrack (hb_apply_context_t *c,
756 const USHORT backtrack[],
757 match_func_t match_func,
758 const void *match_data)
762 hb_apply_context_t::mark_skipping_backward_iterator_t skippy_iter (c, c->buffer->backtrack_len (), count, true);
763 if (skippy_iter.has_no_chance ())
764 return TRACE_RETURN (false);
766 for (unsigned int i = 0; i < count; i++)
768 if (!skippy_iter.prev ())
769 return TRACE_RETURN (false);
771 if (likely (!match_func (c->buffer->out_info[skippy_iter.idx].codepoint, backtrack[i], match_data)))
772 return TRACE_RETURN (false);
775 return TRACE_RETURN (true);
778 static inline bool match_lookahead (hb_apply_context_t *c,
780 const USHORT lookahead[],
781 match_func_t match_func,
782 const void *match_data,
787 hb_apply_context_t::mark_skipping_forward_iterator_t skippy_iter (c, c->buffer->idx + offset - 1, count, true);
788 if (skippy_iter.has_no_chance ())
789 return TRACE_RETURN (false);
791 for (unsigned int i = 0; i < count; i++)
793 if (!skippy_iter.next ())
794 return TRACE_RETURN (false);
796 if (likely (!match_func (c->buffer->info[skippy_iter.idx].codepoint, lookahead[i], match_data)))
797 return TRACE_RETURN (false);
800 return TRACE_RETURN (true);
807 inline bool sanitize (hb_sanitize_context_t *c) {
808 TRACE_SANITIZE (this);
809 return TRACE_RETURN (c->check_struct (this));
812 USHORT sequenceIndex; /* Index into current glyph
813 * sequence--first glyph = 0 */
814 USHORT lookupListIndex; /* Lookup to apply to that
815 * position--zero--based */
817 DEFINE_SIZE_STATIC (4);
821 template <typename context_t>
822 static inline void recurse_lookups (context_t *c,
823 unsigned int lookupCount,
824 const LookupRecord lookupRecord[] /* Array of LookupRecords--in design order */)
826 for (unsigned int i = 0; i < lookupCount; i++)
827 c->recurse (lookupRecord->lookupListIndex);
830 static inline bool apply_lookup (hb_apply_context_t *c,
831 unsigned int count, /* Including the first glyph */
832 unsigned int lookupCount,
833 const LookupRecord lookupRecord[] /* Array of LookupRecords--in design order */)
837 unsigned int end = c->buffer->len;
838 if (unlikely (count == 0 || c->buffer->idx + count > end))
839 return TRACE_RETURN (false);
841 /* TODO We don't support lookupRecord arrays that are not increasing:
842 * Should be easy for in_place ones at least. */
844 /* Note: If sublookup is reverse, it will underflow after the first loop
845 * and we jump out of it. Not entirely disastrous. So we don't check
846 * for reverse lookup here.
848 for (unsigned int i = 0; i < count; /* NOP */)
850 if (unlikely (c->buffer->idx == end))
851 return TRACE_RETURN (true);
852 while (c->should_mark_skip_current_glyph ())
854 /* No lookup applied for this index */
855 c->buffer->next_glyph ();
856 if (unlikely (c->buffer->idx == end))
857 return TRACE_RETURN (true);
860 if (lookupCount && i == lookupRecord->sequenceIndex)
862 unsigned int old_pos = c->buffer->idx;
865 bool done = c->recurse (lookupRecord->lookupListIndex);
869 /* Err, this is wrong if the lookup jumped over some glyphs */
870 i += c->buffer->idx - old_pos;
871 if (unlikely (c->buffer->idx == end))
872 return TRACE_RETURN (true);
880 /* No lookup applied for this index */
881 c->buffer->next_glyph ();
886 return TRACE_RETURN (true);
891 /* Contextual lookups */
893 struct ContextClosureLookupContext
895 ContextClosureFuncs funcs;
896 const void *intersects_data;
899 struct ContextCollectGlyphsLookupContext
901 ContextCollectGlyphsFuncs funcs;
902 const void *collect_data;
905 struct ContextApplyLookupContext
907 ContextApplyFuncs funcs;
908 const void *match_data;
911 static inline void context_closure_lookup (hb_closure_context_t *c,
912 unsigned int inputCount, /* Including the first glyph (not matched) */
913 const USHORT input[], /* Array of input values--start with second glyph */
914 unsigned int lookupCount,
915 const LookupRecord lookupRecord[],
916 ContextClosureLookupContext &lookup_context)
918 if (intersects_array (c,
919 inputCount ? inputCount - 1 : 0, input,
920 lookup_context.funcs.intersects, lookup_context.intersects_data))
922 lookupCount, lookupRecord);
925 static inline void context_collect_glyphs_lookup (hb_collect_glyphs_context_t *c,
926 unsigned int inputCount, /* Including the first glyph (not matched) */
927 const USHORT input[], /* Array of input values--start with second glyph */
928 unsigned int lookupCount,
929 const LookupRecord lookupRecord[],
930 ContextCollectGlyphsLookupContext &lookup_context)
932 collect_array (c, c->input,
933 inputCount ? inputCount - 1 : 0, input,
934 lookup_context.funcs.collect, lookup_context.collect_data);
936 lookupCount, lookupRecord);
939 static inline bool context_would_apply_lookup (hb_would_apply_context_t *c,
940 unsigned int inputCount, /* Including the first glyph (not matched) */
941 const USHORT input[], /* Array of input values--start with second glyph */
942 unsigned int lookupCount HB_UNUSED,
943 const LookupRecord lookupRecord[] HB_UNUSED,
944 ContextApplyLookupContext &lookup_context)
946 return would_match_input (c,
948 lookup_context.funcs.match, lookup_context.match_data);
950 static inline bool context_apply_lookup (hb_apply_context_t *c,
951 unsigned int inputCount, /* Including the first glyph (not matched) */
952 const USHORT input[], /* Array of input values--start with second glyph */
953 unsigned int lookupCount,
954 const LookupRecord lookupRecord[],
955 ContextApplyLookupContext &lookup_context)
957 return match_input (c,
959 lookup_context.funcs.match, lookup_context.match_data)
962 lookupCount, lookupRecord);
967 inline void closure (hb_closure_context_t *c, ContextClosureLookupContext &lookup_context) const
969 TRACE_CLOSURE (this);
970 const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (input, input[0].static_size * (inputCount ? inputCount - 1 : 0));
971 context_closure_lookup (c,
973 lookupCount, lookupRecord,
977 inline void collect_glyphs (hb_collect_glyphs_context_t *c, ContextCollectGlyphsLookupContext &lookup_context) const
979 TRACE_COLLECT_GLYPHS (this);
980 const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (input, input[0].static_size * (inputCount ? inputCount - 1 : 0));
981 context_collect_glyphs_lookup (c,
983 lookupCount, lookupRecord,
987 inline bool would_apply (hb_would_apply_context_t *c, ContextApplyLookupContext &lookup_context) const
989 TRACE_WOULD_APPLY (this);
990 const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (input, input[0].static_size * (inputCount ? inputCount - 1 : 0));
991 return TRACE_RETURN (context_would_apply_lookup (c, inputCount, input, lookupCount, lookupRecord, lookup_context));
994 inline bool apply (hb_apply_context_t *c, ContextApplyLookupContext &lookup_context) const
997 const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (input, input[0].static_size * (inputCount ? inputCount - 1 : 0));
998 return TRACE_RETURN (context_apply_lookup (c, inputCount, input, lookupCount, lookupRecord, lookup_context));
1002 inline bool sanitize (hb_sanitize_context_t *c) {
1003 TRACE_SANITIZE (this);
1004 return inputCount.sanitize (c)
1005 && lookupCount.sanitize (c)
1006 && c->check_range (input,
1007 input[0].static_size * inputCount
1008 + lookupRecordX[0].static_size * lookupCount);
1012 USHORT inputCount; /* Total number of glyphs in input
1013 * glyph sequence--includes the first
1015 USHORT lookupCount; /* Number of LookupRecords */
1016 USHORT input[VAR]; /* Array of match inputs--start with
1018 LookupRecord lookupRecordX[VAR]; /* Array of LookupRecords--in
1021 DEFINE_SIZE_ARRAY2 (4, input, lookupRecordX);
1026 inline void closure (hb_closure_context_t *c, ContextClosureLookupContext &lookup_context) const
1028 TRACE_CLOSURE (this);
1029 unsigned int num_rules = rule.len;
1030 for (unsigned int i = 0; i < num_rules; i++)
1031 (this+rule[i]).closure (c, lookup_context);
1034 inline void collect_glyphs (hb_collect_glyphs_context_t *c, ContextCollectGlyphsLookupContext &lookup_context) const
1036 TRACE_COLLECT_GLYPHS (this);
1037 unsigned int num_rules = rule.len;
1038 for (unsigned int i = 0; i < num_rules; i++)
1039 (this+rule[i]).collect_glyphs (c, lookup_context);
1042 inline bool would_apply (hb_would_apply_context_t *c, ContextApplyLookupContext &lookup_context) const
1044 TRACE_WOULD_APPLY (this);
1045 unsigned int num_rules = rule.len;
1046 for (unsigned int i = 0; i < num_rules; i++)
1048 if ((this+rule[i]).would_apply (c, lookup_context))
1049 return TRACE_RETURN (true);
1051 return TRACE_RETURN (false);
1054 inline bool apply (hb_apply_context_t *c, ContextApplyLookupContext &lookup_context) const
1057 unsigned int num_rules = rule.len;
1058 for (unsigned int i = 0; i < num_rules; i++)
1060 if ((this+rule[i]).apply (c, lookup_context))
1061 return TRACE_RETURN (true);
1063 return TRACE_RETURN (false);
1066 inline bool sanitize (hb_sanitize_context_t *c) {
1067 TRACE_SANITIZE (this);
1068 return TRACE_RETURN (rule.sanitize (c, this));
1073 rule; /* Array of Rule tables
1074 * ordered by preference */
1076 DEFINE_SIZE_ARRAY (2, rule);
1080 struct ContextFormat1
1082 inline void closure (hb_closure_context_t *c) const
1084 TRACE_CLOSURE (this);
1086 const Coverage &cov = (this+coverage);
1088 struct ContextClosureLookupContext lookup_context = {
1093 unsigned int count = ruleSet.len;
1094 for (unsigned int i = 0; i < count; i++)
1095 if (cov.intersects_coverage (c->glyphs, i)) {
1096 const RuleSet &rule_set = this+ruleSet[i];
1097 rule_set.closure (c, lookup_context);
1101 inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
1103 TRACE_COLLECT_GLYPHS (this);
1104 (this+coverage).add_coverage (c->input);
1106 struct ContextCollectGlyphsLookupContext lookup_context = {
1111 unsigned int count = ruleSet.len;
1112 for (unsigned int i = 0; i < count; i++)
1113 (this+ruleSet[i]).collect_glyphs (c, lookup_context);
1116 inline bool would_apply (hb_would_apply_context_t *c) const
1118 TRACE_WOULD_APPLY (this);
1120 const RuleSet &rule_set = this+ruleSet[(this+coverage).get_coverage (c->glyphs[0])];
1121 struct ContextApplyLookupContext lookup_context = {
1125 return TRACE_RETURN (rule_set.would_apply (c, lookup_context));
1128 inline const Coverage &get_coverage (void) const
1130 return this+coverage;
1133 inline bool apply (hb_apply_context_t *c) const
1136 unsigned int index = (this+coverage).get_coverage (c->buffer->cur().codepoint);
1137 if (likely (index == NOT_COVERED))
1138 return TRACE_RETURN (false);
1140 const RuleSet &rule_set = this+ruleSet[index];
1141 struct ContextApplyLookupContext lookup_context = {
1145 return TRACE_RETURN (rule_set.apply (c, lookup_context));
1148 inline bool sanitize (hb_sanitize_context_t *c) {
1149 TRACE_SANITIZE (this);
1150 return TRACE_RETURN (coverage.sanitize (c, this) && ruleSet.sanitize (c, this));
1154 USHORT format; /* Format identifier--format = 1 */
1156 coverage; /* Offset to Coverage table--from
1157 * beginning of table */
1158 OffsetArrayOf<RuleSet>
1159 ruleSet; /* Array of RuleSet tables
1160 * ordered by Coverage Index */
1162 DEFINE_SIZE_ARRAY (6, ruleSet);
1166 struct ContextFormat2
1168 inline void closure (hb_closure_context_t *c) const
1170 TRACE_CLOSURE (this);
1171 if (!(this+coverage).intersects (c->glyphs))
1174 const ClassDef &class_def = this+classDef;
1176 struct ContextClosureLookupContext lookup_context = {
1181 unsigned int count = ruleSet.len;
1182 for (unsigned int i = 0; i < count; i++)
1183 if (class_def.intersects_class (c->glyphs, i)) {
1184 const RuleSet &rule_set = this+ruleSet[i];
1185 rule_set.closure (c, lookup_context);
1189 inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
1191 TRACE_COLLECT_GLYPHS (this);
1192 (this+coverage).add_coverage (c->input);
1194 const ClassDef &class_def = this+classDef;
1195 struct ContextCollectGlyphsLookupContext lookup_context = {
1200 unsigned int count = ruleSet.len;
1201 for (unsigned int i = 0; i < count; i++)
1202 (this+ruleSet[i]).collect_glyphs (c, lookup_context);
1205 inline bool would_apply (hb_would_apply_context_t *c) const
1207 TRACE_WOULD_APPLY (this);
1209 const ClassDef &class_def = this+classDef;
1210 unsigned int index = class_def.get_class (c->glyphs[0]);
1211 const RuleSet &rule_set = this+ruleSet[index];
1212 struct ContextApplyLookupContext lookup_context = {
1216 return TRACE_RETURN (rule_set.would_apply (c, lookup_context));
1219 inline const Coverage &get_coverage (void) const
1221 return this+coverage;
1224 inline bool apply (hb_apply_context_t *c) const
1227 unsigned int index = (this+coverage).get_coverage (c->buffer->cur().codepoint);
1228 if (likely (index == NOT_COVERED)) return TRACE_RETURN (false);
1230 const ClassDef &class_def = this+classDef;
1231 index = class_def.get_class (c->buffer->cur().codepoint);
1232 const RuleSet &rule_set = this+ruleSet[index];
1233 struct ContextApplyLookupContext lookup_context = {
1237 return TRACE_RETURN (rule_set.apply (c, lookup_context));
1240 inline bool sanitize (hb_sanitize_context_t *c) {
1241 TRACE_SANITIZE (this);
1242 return TRACE_RETURN (coverage.sanitize (c, this) && classDef.sanitize (c, this) && ruleSet.sanitize (c, this));
1246 USHORT format; /* Format identifier--format = 2 */
1248 coverage; /* Offset to Coverage table--from
1249 * beginning of table */
1251 classDef; /* Offset to glyph ClassDef table--from
1252 * beginning of table */
1253 OffsetArrayOf<RuleSet>
1254 ruleSet; /* Array of RuleSet tables
1255 * ordered by class */
1257 DEFINE_SIZE_ARRAY (8, ruleSet);
1261 struct ContextFormat3
1263 inline void closure (hb_closure_context_t *c) const
1265 TRACE_CLOSURE (this);
1266 if (!(this+coverage[0]).intersects (c->glyphs))
1269 const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (coverage, coverage[0].static_size * glyphCount);
1270 struct ContextClosureLookupContext lookup_context = {
1271 {intersects_coverage},
1274 context_closure_lookup (c,
1275 glyphCount, (const USHORT *) (coverage + 1),
1276 lookupCount, lookupRecord,
1280 inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
1282 TRACE_COLLECT_GLYPHS (this);
1283 (this+coverage[0]).add_coverage (c->input);
1285 const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (coverage, coverage[0].static_size * glyphCount);
1286 struct ContextCollectGlyphsLookupContext lookup_context = {
1291 context_collect_glyphs_lookup (c,
1292 glyphCount, (const USHORT *) (coverage + 1),
1293 lookupCount, lookupRecord,
1297 inline bool would_apply (hb_would_apply_context_t *c) const
1299 TRACE_WOULD_APPLY (this);
1301 const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (coverage, coverage[0].static_size * glyphCount);
1302 struct ContextApplyLookupContext lookup_context = {
1306 return TRACE_RETURN (context_would_apply_lookup (c, glyphCount, (const USHORT *) (coverage + 1), lookupCount, lookupRecord, lookup_context));
1309 inline const Coverage &get_coverage (void) const
1311 return this+coverage[0];
1314 inline bool apply (hb_apply_context_t *c) const
1317 unsigned int index = (this+coverage[0]).get_coverage (c->buffer->cur().codepoint);
1318 if (likely (index == NOT_COVERED)) return TRACE_RETURN (false);
1320 const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (coverage, coverage[0].static_size * glyphCount);
1321 struct ContextApplyLookupContext lookup_context = {
1325 return TRACE_RETURN (context_apply_lookup (c, glyphCount, (const USHORT *) (coverage + 1), lookupCount, lookupRecord, lookup_context));
1328 inline bool sanitize (hb_sanitize_context_t *c) {
1329 TRACE_SANITIZE (this);
1330 if (!c->check_struct (this)) return TRACE_RETURN (false);
1331 unsigned int count = glyphCount;
1332 if (!c->check_array (coverage, coverage[0].static_size, count)) return TRACE_RETURN (false);
1333 for (unsigned int i = 0; i < count; i++)
1334 if (!coverage[i].sanitize (c, this)) return TRACE_RETURN (false);
1335 LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (coverage, coverage[0].static_size * count);
1336 return TRACE_RETURN (c->check_array (lookupRecord, lookupRecord[0].static_size, lookupCount));
1340 USHORT format; /* Format identifier--format = 3 */
1341 USHORT glyphCount; /* Number of glyphs in the input glyph
1343 USHORT lookupCount; /* Number of LookupRecords */
1345 coverage[VAR]; /* Array of offsets to Coverage
1346 * table in glyph sequence order */
1347 LookupRecord lookupRecordX[VAR]; /* Array of LookupRecords--in
1350 DEFINE_SIZE_ARRAY2 (6, coverage, lookupRecordX);
1355 template <typename context_t>
1356 inline typename context_t::return_t process (context_t *c) const
1358 TRACE_PROCESS (this);
1360 case 1: return TRACE_RETURN (c->process (u.format1));
1361 case 2: return TRACE_RETURN (c->process (u.format2));
1362 case 3: return TRACE_RETURN (c->process (u.format3));
1363 default:return TRACE_RETURN (c->default_return_value ());
1367 inline bool sanitize (hb_sanitize_context_t *c) {
1368 TRACE_SANITIZE (this);
1369 if (!u.format.sanitize (c)) return TRACE_RETURN (false);
1371 case 1: return TRACE_RETURN (u.format1.sanitize (c));
1372 case 2: return TRACE_RETURN (u.format2.sanitize (c));
1373 case 3: return TRACE_RETURN (u.format3.sanitize (c));
1374 default:return TRACE_RETURN (true);
1380 USHORT format; /* Format identifier */
1381 ContextFormat1 format1;
1382 ContextFormat2 format2;
1383 ContextFormat3 format3;
1388 /* Chaining Contextual lookups */
1390 struct ChainContextClosureLookupContext
1392 ContextClosureFuncs funcs;
1393 const void *intersects_data[3];
1396 struct ChainContextCollectGlyphsLookupContext
1398 ContextCollectGlyphsFuncs funcs;
1399 const void *collect_data[3];
1402 struct ChainContextApplyLookupContext
1404 ContextApplyFuncs funcs;
1405 const void *match_data[3];
1408 static inline void chain_context_closure_lookup (hb_closure_context_t *c,
1409 unsigned int backtrackCount,
1410 const USHORT backtrack[],
1411 unsigned int inputCount, /* Including the first glyph (not matched) */
1412 const USHORT input[], /* Array of input values--start with second glyph */
1413 unsigned int lookaheadCount,
1414 const USHORT lookahead[],
1415 unsigned int lookupCount,
1416 const LookupRecord lookupRecord[],
1417 ChainContextClosureLookupContext &lookup_context)
1419 if (intersects_array (c,
1420 backtrackCount, backtrack,
1421 lookup_context.funcs.intersects, lookup_context.intersects_data[0])
1422 && intersects_array (c,
1423 inputCount ? inputCount - 1 : 0, input,
1424 lookup_context.funcs.intersects, lookup_context.intersects_data[1])
1425 && intersects_array (c,
1426 lookaheadCount, lookahead,
1427 lookup_context.funcs.intersects, lookup_context.intersects_data[2]))
1429 lookupCount, lookupRecord);
1432 static inline void chain_context_collect_glyphs_lookup (hb_collect_glyphs_context_t *c,
1433 unsigned int backtrackCount,
1434 const USHORT backtrack[],
1435 unsigned int inputCount, /* Including the first glyph (not matched) */
1436 const USHORT input[], /* Array of input values--start with second glyph */
1437 unsigned int lookaheadCount,
1438 const USHORT lookahead[],
1439 unsigned int lookupCount,
1440 const LookupRecord lookupRecord[],
1441 ChainContextCollectGlyphsLookupContext &lookup_context)
1443 collect_array (c, c->before,
1444 backtrackCount, backtrack,
1445 lookup_context.funcs.collect, lookup_context.collect_data[0]);
1446 collect_array (c, c->input,
1447 inputCount ? inputCount - 1 : 0, input,
1448 lookup_context.funcs.collect, lookup_context.collect_data[1]);
1449 collect_array (c, c->after,
1450 lookaheadCount, lookahead,
1451 lookup_context.funcs.collect, lookup_context.collect_data[2]);
1453 lookupCount, lookupRecord);
1456 static inline bool chain_context_would_apply_lookup (hb_would_apply_context_t *c,
1457 unsigned int backtrackCount,
1458 const USHORT backtrack[] HB_UNUSED,
1459 unsigned int inputCount, /* Including the first glyph (not matched) */
1460 const USHORT input[], /* Array of input values--start with second glyph */
1461 unsigned int lookaheadCount,
1462 const USHORT lookahead[] HB_UNUSED,
1463 unsigned int lookupCount HB_UNUSED,
1464 const LookupRecord lookupRecord[] HB_UNUSED,
1465 ChainContextApplyLookupContext &lookup_context)
1467 return (c->zero_context ? !backtrackCount && !lookaheadCount : true)
1468 && would_match_input (c,
1470 lookup_context.funcs.match, lookup_context.match_data[1]);
1473 static inline bool chain_context_apply_lookup (hb_apply_context_t *c,
1474 unsigned int backtrackCount,
1475 const USHORT backtrack[],
1476 unsigned int inputCount, /* Including the first glyph (not matched) */
1477 const USHORT input[], /* Array of input values--start with second glyph */
1478 unsigned int lookaheadCount,
1479 const USHORT lookahead[],
1480 unsigned int lookupCount,
1481 const LookupRecord lookupRecord[],
1482 ChainContextApplyLookupContext &lookup_context)
1484 unsigned int lookahead_offset = 0;
1485 return match_input (c,
1487 lookup_context.funcs.match, lookup_context.match_data[1],
1489 && match_backtrack (c,
1490 backtrackCount, backtrack,
1491 lookup_context.funcs.match, lookup_context.match_data[0])
1492 && match_lookahead (c,
1493 lookaheadCount, lookahead,
1494 lookup_context.funcs.match, lookup_context.match_data[2],
1498 lookupCount, lookupRecord);
1503 inline void closure (hb_closure_context_t *c, ChainContextClosureLookupContext &lookup_context) const
1505 TRACE_CLOSURE (this);
1506 const HeadlessArrayOf<USHORT> &input = StructAfter<HeadlessArrayOf<USHORT> > (backtrack);
1507 const ArrayOf<USHORT> &lookahead = StructAfter<ArrayOf<USHORT> > (input);
1508 const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
1509 chain_context_closure_lookup (c,
1510 backtrack.len, backtrack.array,
1511 input.len, input.array,
1512 lookahead.len, lookahead.array,
1513 lookup.len, lookup.array,
1517 inline void collect_glyphs (hb_collect_glyphs_context_t *c, ChainContextCollectGlyphsLookupContext &lookup_context) const
1519 TRACE_COLLECT_GLYPHS (this);
1520 const HeadlessArrayOf<USHORT> &input = StructAfter<HeadlessArrayOf<USHORT> > (backtrack);
1521 const ArrayOf<USHORT> &lookahead = StructAfter<ArrayOf<USHORT> > (input);
1522 const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
1523 chain_context_collect_glyphs_lookup (c,
1524 backtrack.len, backtrack.array,
1525 input.len, input.array,
1526 lookahead.len, lookahead.array,
1527 lookup.len, lookup.array,
1531 inline bool would_apply (hb_would_apply_context_t *c, ChainContextApplyLookupContext &lookup_context) const
1533 TRACE_WOULD_APPLY (this);
1534 const HeadlessArrayOf<USHORT> &input = StructAfter<HeadlessArrayOf<USHORT> > (backtrack);
1535 const ArrayOf<USHORT> &lookahead = StructAfter<ArrayOf<USHORT> > (input);
1536 const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
1537 return TRACE_RETURN (chain_context_would_apply_lookup (c,
1538 backtrack.len, backtrack.array,
1539 input.len, input.array,
1540 lookahead.len, lookahead.array, lookup.len,
1541 lookup.array, lookup_context));
1544 inline bool apply (hb_apply_context_t *c, ChainContextApplyLookupContext &lookup_context) const
1547 const HeadlessArrayOf<USHORT> &input = StructAfter<HeadlessArrayOf<USHORT> > (backtrack);
1548 const ArrayOf<USHORT> &lookahead = StructAfter<ArrayOf<USHORT> > (input);
1549 const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
1550 return TRACE_RETURN (chain_context_apply_lookup (c,
1551 backtrack.len, backtrack.array,
1552 input.len, input.array,
1553 lookahead.len, lookahead.array, lookup.len,
1554 lookup.array, lookup_context));
1557 inline bool sanitize (hb_sanitize_context_t *c) {
1558 TRACE_SANITIZE (this);
1559 if (!backtrack.sanitize (c)) return TRACE_RETURN (false);
1560 HeadlessArrayOf<USHORT> &input = StructAfter<HeadlessArrayOf<USHORT> > (backtrack);
1561 if (!input.sanitize (c)) return TRACE_RETURN (false);
1562 ArrayOf<USHORT> &lookahead = StructAfter<ArrayOf<USHORT> > (input);
1563 if (!lookahead.sanitize (c)) return TRACE_RETURN (false);
1564 ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
1565 return TRACE_RETURN (lookup.sanitize (c));
1570 backtrack; /* Array of backtracking values
1571 * (to be matched before the input
1573 HeadlessArrayOf<USHORT>
1574 inputX; /* Array of input values (start with
1577 lookaheadX; /* Array of lookahead values's (to be
1578 * matched after the input sequence) */
1579 ArrayOf<LookupRecord>
1580 lookupX; /* Array of LookupRecords--in
1583 DEFINE_SIZE_MIN (8);
1588 inline void closure (hb_closure_context_t *c, ChainContextClosureLookupContext &lookup_context) const
1590 TRACE_CLOSURE (this);
1591 unsigned int num_rules = rule.len;
1592 for (unsigned int i = 0; i < num_rules; i++)
1593 (this+rule[i]).closure (c, lookup_context);
1596 inline void collect_glyphs (hb_collect_glyphs_context_t *c, ChainContextCollectGlyphsLookupContext &lookup_context) const
1598 TRACE_COLLECT_GLYPHS (this);
1599 unsigned int num_rules = rule.len;
1600 for (unsigned int i = 0; i < num_rules; i++)
1601 (this+rule[i]).collect_glyphs (c, lookup_context);
1604 inline bool would_apply (hb_would_apply_context_t *c, ChainContextApplyLookupContext &lookup_context) const
1606 TRACE_WOULD_APPLY (this);
1607 unsigned int num_rules = rule.len;
1608 for (unsigned int i = 0; i < num_rules; i++)
1609 if ((this+rule[i]).would_apply (c, lookup_context))
1610 return TRACE_RETURN (true);
1612 return TRACE_RETURN (false);
1615 inline bool apply (hb_apply_context_t *c, ChainContextApplyLookupContext &lookup_context) const
1618 unsigned int num_rules = rule.len;
1619 for (unsigned int i = 0; i < num_rules; i++)
1620 if ((this+rule[i]).apply (c, lookup_context))
1621 return TRACE_RETURN (true);
1623 return TRACE_RETURN (false);
1626 inline bool sanitize (hb_sanitize_context_t *c) {
1627 TRACE_SANITIZE (this);
1628 return TRACE_RETURN (rule.sanitize (c, this));
1632 OffsetArrayOf<ChainRule>
1633 rule; /* Array of ChainRule tables
1634 * ordered by preference */
1636 DEFINE_SIZE_ARRAY (2, rule);
1639 struct ChainContextFormat1
1641 inline void closure (hb_closure_context_t *c) const
1643 TRACE_CLOSURE (this);
1644 const Coverage &cov = (this+coverage);
1646 struct ChainContextClosureLookupContext lookup_context = {
1651 unsigned int count = ruleSet.len;
1652 for (unsigned int i = 0; i < count; i++)
1653 if (cov.intersects_coverage (c->glyphs, i)) {
1654 const ChainRuleSet &rule_set = this+ruleSet[i];
1655 rule_set.closure (c, lookup_context);
1659 inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
1661 TRACE_COLLECT_GLYPHS (this);
1662 (this+coverage).add_coverage (c->input);
1664 struct ChainContextCollectGlyphsLookupContext lookup_context = {
1669 unsigned int count = ruleSet.len;
1670 for (unsigned int i = 0; i < count; i++)
1671 (this+ruleSet[i]).collect_glyphs (c, lookup_context);
1674 inline bool would_apply (hb_would_apply_context_t *c) const
1676 TRACE_WOULD_APPLY (this);
1678 const ChainRuleSet &rule_set = this+ruleSet[(this+coverage).get_coverage (c->glyphs[0])];
1679 struct ChainContextApplyLookupContext lookup_context = {
1683 return TRACE_RETURN (rule_set.would_apply (c, lookup_context));
1686 inline const Coverage &get_coverage (void) const
1688 return this+coverage;
1691 inline bool apply (hb_apply_context_t *c) const
1694 unsigned int index = (this+coverage).get_coverage (c->buffer->cur().codepoint);
1695 if (likely (index == NOT_COVERED)) return TRACE_RETURN (false);
1697 const ChainRuleSet &rule_set = this+ruleSet[index];
1698 struct ChainContextApplyLookupContext lookup_context = {
1702 return TRACE_RETURN (rule_set.apply (c, lookup_context));
1705 inline bool sanitize (hb_sanitize_context_t *c) {
1706 TRACE_SANITIZE (this);
1707 return TRACE_RETURN (coverage.sanitize (c, this) && ruleSet.sanitize (c, this));
1711 USHORT format; /* Format identifier--format = 1 */
1713 coverage; /* Offset to Coverage table--from
1714 * beginning of table */
1715 OffsetArrayOf<ChainRuleSet>
1716 ruleSet; /* Array of ChainRuleSet tables
1717 * ordered by Coverage Index */
1719 DEFINE_SIZE_ARRAY (6, ruleSet);
1722 struct ChainContextFormat2
1724 inline void closure (hb_closure_context_t *c) const
1726 TRACE_CLOSURE (this);
1727 if (!(this+coverage).intersects (c->glyphs))
1730 const ClassDef &backtrack_class_def = this+backtrackClassDef;
1731 const ClassDef &input_class_def = this+inputClassDef;
1732 const ClassDef &lookahead_class_def = this+lookaheadClassDef;
1734 struct ChainContextClosureLookupContext lookup_context = {
1736 {&backtrack_class_def,
1738 &lookahead_class_def}
1741 unsigned int count = ruleSet.len;
1742 for (unsigned int i = 0; i < count; i++)
1743 if (input_class_def.intersects_class (c->glyphs, i)) {
1744 const ChainRuleSet &rule_set = this+ruleSet[i];
1745 rule_set.closure (c, lookup_context);
1749 inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
1751 TRACE_COLLECT_GLYPHS (this);
1752 (this+coverage).add_coverage (c->input);
1754 const ClassDef &backtrack_class_def = this+backtrackClassDef;
1755 const ClassDef &input_class_def = this+inputClassDef;
1756 const ClassDef &lookahead_class_def = this+lookaheadClassDef;
1758 struct ChainContextCollectGlyphsLookupContext lookup_context = {
1760 {&backtrack_class_def,
1762 &lookahead_class_def}
1765 unsigned int count = ruleSet.len;
1766 for (unsigned int i = 0; i < count; i++)
1767 (this+ruleSet[i]).collect_glyphs (c, lookup_context);
1770 inline bool would_apply (hb_would_apply_context_t *c) const
1772 TRACE_WOULD_APPLY (this);
1774 const ClassDef &backtrack_class_def = this+backtrackClassDef;
1775 const ClassDef &input_class_def = this+inputClassDef;
1776 const ClassDef &lookahead_class_def = this+lookaheadClassDef;
1778 unsigned int index = input_class_def.get_class (c->glyphs[0]);
1779 const ChainRuleSet &rule_set = this+ruleSet[index];
1780 struct ChainContextApplyLookupContext lookup_context = {
1782 {&backtrack_class_def,
1784 &lookahead_class_def}
1786 return TRACE_RETURN (rule_set.would_apply (c, lookup_context));
1789 inline const Coverage &get_coverage (void) const
1791 return this+coverage;
1794 inline bool apply (hb_apply_context_t *c) const
1797 unsigned int index = (this+coverage).get_coverage (c->buffer->cur().codepoint);
1798 if (likely (index == NOT_COVERED)) return TRACE_RETURN (false);
1800 const ClassDef &backtrack_class_def = this+backtrackClassDef;
1801 const ClassDef &input_class_def = this+inputClassDef;
1802 const ClassDef &lookahead_class_def = this+lookaheadClassDef;
1804 index = input_class_def.get_class (c->buffer->cur().codepoint);
1805 const ChainRuleSet &rule_set = this+ruleSet[index];
1806 struct ChainContextApplyLookupContext lookup_context = {
1808 {&backtrack_class_def,
1810 &lookahead_class_def}
1812 return TRACE_RETURN (rule_set.apply (c, lookup_context));
1815 inline bool sanitize (hb_sanitize_context_t *c) {
1816 TRACE_SANITIZE (this);
1817 return TRACE_RETURN (coverage.sanitize (c, this) && backtrackClassDef.sanitize (c, this) &&
1818 inputClassDef.sanitize (c, this) && lookaheadClassDef.sanitize (c, this) &&
1819 ruleSet.sanitize (c, this));
1823 USHORT format; /* Format identifier--format = 2 */
1825 coverage; /* Offset to Coverage table--from
1826 * beginning of table */
1828 backtrackClassDef; /* Offset to glyph ClassDef table
1829 * containing backtrack sequence
1830 * data--from beginning of table */
1832 inputClassDef; /* Offset to glyph ClassDef
1833 * table containing input sequence
1834 * data--from beginning of table */
1836 lookaheadClassDef; /* Offset to glyph ClassDef table
1837 * containing lookahead sequence
1838 * data--from beginning of table */
1839 OffsetArrayOf<ChainRuleSet>
1840 ruleSet; /* Array of ChainRuleSet tables
1841 * ordered by class */
1843 DEFINE_SIZE_ARRAY (12, ruleSet);
1846 struct ChainContextFormat3
1848 inline void closure (hb_closure_context_t *c) const
1850 TRACE_CLOSURE (this);
1851 const OffsetArrayOf<Coverage> &input = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
1853 if (!(this+input[0]).intersects (c->glyphs))
1856 const OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (input);
1857 const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
1858 struct ChainContextClosureLookupContext lookup_context = {
1859 {intersects_coverage},
1862 chain_context_closure_lookup (c,
1863 backtrack.len, (const USHORT *) backtrack.array,
1864 input.len, (const USHORT *) input.array + 1,
1865 lookahead.len, (const USHORT *) lookahead.array,
1866 lookup.len, lookup.array,
1870 inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
1872 TRACE_COLLECT_GLYPHS (this);
1873 const OffsetArrayOf<Coverage> &input = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
1875 (this+input[0]).add_coverage (c->input);
1877 const OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (input);
1878 const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
1879 struct ChainContextCollectGlyphsLookupContext lookup_context = {
1883 chain_context_collect_glyphs_lookup (c,
1884 backtrack.len, (const USHORT *) backtrack.array,
1885 input.len, (const USHORT *) input.array + 1,
1886 lookahead.len, (const USHORT *) lookahead.array,
1887 lookup.len, lookup.array,
1891 inline bool would_apply (hb_would_apply_context_t *c) const
1893 TRACE_WOULD_APPLY (this);
1895 const OffsetArrayOf<Coverage> &input = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
1896 const OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (input);
1897 const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
1898 struct ChainContextApplyLookupContext lookup_context = {
1902 return TRACE_RETURN (chain_context_would_apply_lookup (c,
1903 backtrack.len, (const USHORT *) backtrack.array,
1904 input.len, (const USHORT *) input.array + 1,
1905 lookahead.len, (const USHORT *) lookahead.array,
1906 lookup.len, lookup.array, lookup_context));
1909 inline const Coverage &get_coverage (void) const
1911 const OffsetArrayOf<Coverage> &input = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
1912 return this+input[0];
1915 inline bool apply (hb_apply_context_t *c) const
1918 const OffsetArrayOf<Coverage> &input = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
1920 unsigned int index = (this+input[0]).get_coverage (c->buffer->cur().codepoint);
1921 if (likely (index == NOT_COVERED)) return TRACE_RETURN (false);
1923 const OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (input);
1924 const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
1925 struct ChainContextApplyLookupContext lookup_context = {
1929 return TRACE_RETURN (chain_context_apply_lookup (c,
1930 backtrack.len, (const USHORT *) backtrack.array,
1931 input.len, (const USHORT *) input.array + 1,
1932 lookahead.len, (const USHORT *) lookahead.array,
1933 lookup.len, lookup.array, lookup_context));
1936 inline bool sanitize (hb_sanitize_context_t *c) {
1937 TRACE_SANITIZE (this);
1938 if (!backtrack.sanitize (c, this)) return TRACE_RETURN (false);
1939 OffsetArrayOf<Coverage> &input = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
1940 if (!input.sanitize (c, this)) return TRACE_RETURN (false);
1941 OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (input);
1942 if (!lookahead.sanitize (c, this)) return TRACE_RETURN (false);
1943 ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
1944 return TRACE_RETURN (lookup.sanitize (c));
1948 USHORT format; /* Format identifier--format = 3 */
1949 OffsetArrayOf<Coverage>
1950 backtrack; /* Array of coverage tables
1951 * in backtracking sequence, in glyph
1953 OffsetArrayOf<Coverage>
1954 inputX ; /* Array of coverage
1955 * tables in input sequence, in glyph
1957 OffsetArrayOf<Coverage>
1958 lookaheadX; /* Array of coverage tables
1959 * in lookahead sequence, in glyph
1961 ArrayOf<LookupRecord>
1962 lookupX; /* Array of LookupRecords--in
1965 DEFINE_SIZE_MIN (10);
1970 template <typename context_t>
1971 inline typename context_t::return_t process (context_t *c) const
1973 TRACE_PROCESS (this);
1975 case 1: return TRACE_RETURN (c->process (u.format1));
1976 case 2: return TRACE_RETURN (c->process (u.format2));
1977 case 3: return TRACE_RETURN (c->process (u.format3));
1978 default:return TRACE_RETURN (c->default_return_value ());
1982 inline bool sanitize (hb_sanitize_context_t *c) {
1983 TRACE_SANITIZE (this);
1984 if (!u.format.sanitize (c)) return TRACE_RETURN (false);
1986 case 1: return TRACE_RETURN (u.format1.sanitize (c));
1987 case 2: return TRACE_RETURN (u.format2.sanitize (c));
1988 case 3: return TRACE_RETURN (u.format3.sanitize (c));
1989 default:return TRACE_RETURN (true);
1995 USHORT format; /* Format identifier */
1996 ChainContextFormat1 format1;
1997 ChainContextFormat2 format2;
1998 ChainContextFormat3 format3;
2003 struct ExtensionFormat1
2005 inline unsigned int get_type (void) const { return extensionLookupType; }
2006 inline unsigned int get_offset (void) const { return extensionOffset; }
2008 inline bool sanitize (hb_sanitize_context_t *c) {
2009 TRACE_SANITIZE (this);
2010 return TRACE_RETURN (c->check_struct (this));
2014 USHORT format; /* Format identifier. Set to 1. */
2015 USHORT extensionLookupType; /* Lookup type of subtable referenced
2016 * by ExtensionOffset (i.e. the
2017 * extension subtable). */
2018 ULONG extensionOffset; /* Offset to the extension subtable,
2019 * of lookup type subtable. */
2021 DEFINE_SIZE_STATIC (8);
2024 template <typename T>
2027 inline unsigned int get_type (void) const
2030 case 1: return u.format1.get_type ();
2034 inline unsigned int get_offset (void) const
2037 case 1: return u.format1.get_offset ();
2042 template <typename X>
2043 inline const X& get_subtable (void) const
2045 unsigned int offset = get_offset ();
2046 if (unlikely (!offset)) return Null(typename T::LookupSubTable);
2047 return StructAtOffset<typename T::LookupSubTable> (this, offset);
2050 template <typename context_t>
2051 inline typename context_t::return_t process (context_t *c) const
2053 return get_subtable<typename T::LookupSubTable> ().process (c, get_type ());
2056 inline bool sanitize_self (hb_sanitize_context_t *c) {
2057 TRACE_SANITIZE (this);
2058 if (!u.format.sanitize (c)) return TRACE_RETURN (false);
2060 case 1: return TRACE_RETURN (u.format1.sanitize (c));
2061 default:return TRACE_RETURN (true);
2065 inline bool sanitize (hb_sanitize_context_t *c) {
2066 TRACE_SANITIZE (this);
2067 if (!sanitize_self (c)) return TRACE_RETURN (false);
2068 unsigned int offset = get_offset ();
2069 if (unlikely (!offset)) return TRACE_RETURN (true);
2070 return TRACE_RETURN (StructAtOffset<typename T::LookupSubTable> (this, offset).sanitize (c, get_type ()));
2075 USHORT format; /* Format identifier */
2076 ExtensionFormat1 format1;
2087 static const hb_tag_t GSUBTag = HB_OT_TAG_GSUB;
2088 static const hb_tag_t GPOSTag = HB_OT_TAG_GPOS;
2090 inline unsigned int get_script_count (void) const
2091 { return (this+scriptList).len; }
2092 inline const Tag& get_script_tag (unsigned int i) const
2093 { return (this+scriptList).get_tag (i); }
2094 inline unsigned int get_script_tags (unsigned int start_offset,
2095 unsigned int *script_count /* IN/OUT */,
2096 hb_tag_t *script_tags /* OUT */) const
2097 { return (this+scriptList).get_tags (start_offset, script_count, script_tags); }
2098 inline const Script& get_script (unsigned int i) const
2099 { return (this+scriptList)[i]; }
2100 inline bool find_script_index (hb_tag_t tag, unsigned int *index) const
2101 { return (this+scriptList).find_index (tag, index); }
2103 inline unsigned int get_feature_count (void) const
2104 { return (this+featureList).len; }
2105 inline const Tag& get_feature_tag (unsigned int i) const
2106 { return (this+featureList).get_tag (i); }
2107 inline unsigned int get_feature_tags (unsigned int start_offset,
2108 unsigned int *feature_count /* IN/OUT */,
2109 hb_tag_t *feature_tags /* OUT */) const
2110 { return (this+featureList).get_tags (start_offset, feature_count, feature_tags); }
2111 inline const Feature& get_feature (unsigned int i) const
2112 { return (this+featureList)[i]; }
2113 inline bool find_feature_index (hb_tag_t tag, unsigned int *index) const
2114 { return (this+featureList).find_index (tag, index); }
2116 inline unsigned int get_lookup_count (void) const
2117 { return (this+lookupList).len; }
2118 inline const Lookup& get_lookup (unsigned int i) const
2119 { return (this+lookupList)[i]; }
2121 inline bool sanitize (hb_sanitize_context_t *c) {
2122 TRACE_SANITIZE (this);
2123 return TRACE_RETURN (version.sanitize (c) && likely (version.major == 1) &&
2124 scriptList.sanitize (c, this) &&
2125 featureList.sanitize (c, this) &&
2126 lookupList.sanitize (c, this));
2130 FixedVersion version; /* Version of the GSUB/GPOS table--initially set
2132 OffsetTo<ScriptList>
2133 scriptList; /* ScriptList table */
2134 OffsetTo<FeatureList>
2135 featureList; /* FeatureList table */
2136 OffsetTo<LookupList>
2137 lookupList; /* LookupList table */
2139 DEFINE_SIZE_STATIC (10);
2143 } /* namespace OT */
2146 #endif /* HB_OT_LAYOUT_GSUBGPOS_PRIVATE_HH */