hb-ot-layout-gsubgpos-private.hh revision 24dd4e56743c6ce5e01cb710ca9e01b3e527af58
1/*
2 * Copyright © 2007,2008,2009,2010  Red Hat, Inc.
3 * Copyright © 2010,2012  Google, Inc.
4 *
5 *  This is part of HarfBuzz, a text shaping library.
6 *
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.
12 *
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
17 * DAMAGE.
18 *
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.
24 *
25 * Red Hat Author(s): Behdad Esfahbod
26 * Google Author(s): Behdad Esfahbod
27 */
28
29#ifndef HB_OT_LAYOUT_GSUBGPOS_PRIVATE_HH
30#define HB_OT_LAYOUT_GSUBGPOS_PRIVATE_HH
31
32#include "hb-buffer-private.hh"
33#include "hb-ot-layout-gdef-table.hh"
34#include "hb-set-private.hh"
35
36
37
38#ifndef HB_DEBUG_CLOSURE
39#define HB_DEBUG_CLOSURE (HB_DEBUG+0)
40#endif
41
42#define TRACE_CLOSURE() \
43	hb_auto_trace_t<HB_DEBUG_CLOSURE> trace (&c->debug_depth, "CLOSURE", this, HB_FUNC, "");
44
45
46struct hb_closure_context_t
47{
48  hb_face_t *face;
49  hb_set_t *glyphs;
50  unsigned int nesting_level_left;
51  unsigned int debug_depth;
52
53
54  hb_closure_context_t (hb_face_t *face_,
55			hb_set_t *glyphs_,
56		        unsigned int nesting_level_left_ = MAX_NESTING_LEVEL) :
57			  face (face_),
58			  glyphs (glyphs_),
59			  nesting_level_left (nesting_level_left_),
60			  debug_depth (0) {}
61};
62
63
64
65/* TODO Add TRACE_RETURN annotation to gsub. */
66#ifndef HB_DEBUG_WOULD_APPLY
67#define HB_DEBUG_WOULD_APPLY (HB_DEBUG+0)
68#endif
69
70#define TRACE_WOULD_APPLY() \
71	hb_auto_trace_t<HB_DEBUG_WOULD_APPLY> trace (&c->debug_depth, "WOULD_APPLY", this, HB_FUNC, "%d glyphs", c->len);
72
73
74struct hb_would_apply_context_t
75{
76  hb_face_t *face;
77  const hb_codepoint_t *glyphs;
78  unsigned int len;
79  const hb_set_digest_t digest;
80  unsigned int debug_depth;
81
82  hb_would_apply_context_t (hb_face_t *face_,
83			    const hb_codepoint_t *glyphs_,
84			    unsigned int len_,
85			    const hb_set_digest_t *digest_
86			    ) :
87			      face (face_),
88			      glyphs (glyphs_),
89			      len (len_),
90			      digest (*digest_),
91			      debug_depth (0) {};
92};
93
94
95#ifndef HB_DEBUG_APPLY
96#define HB_DEBUG_APPLY (HB_DEBUG+0)
97#endif
98
99#define TRACE_APPLY() \
100	hb_auto_trace_t<HB_DEBUG_APPLY> trace (&c->debug_depth, "APPLY", this, HB_FUNC, "idx %d codepoint %u", c->buffer->idx, c->buffer->cur().codepoint);
101
102
103struct hb_apply_context_t
104{
105  hb_font_t *font;
106  hb_face_t *face;
107  hb_buffer_t *buffer;
108  hb_direction_t direction;
109  hb_mask_t lookup_mask;
110  unsigned int nesting_level_left;
111  unsigned int lookup_props;
112  unsigned int property; /* propety of first glyph */
113  unsigned int debug_depth;
114  const GDEF &gdef;
115  bool has_glyph_classes;
116  const hb_set_digest_t digest;
117
118
119  hb_apply_context_t (hb_font_t *font_,
120		      hb_buffer_t *buffer_,
121		      hb_mask_t lookup_mask_,
122		      const hb_set_digest_t *digest_) :
123			font (font_), face (font->face), buffer (buffer_),
124			direction (buffer_->props.direction),
125			lookup_mask (lookup_mask_),
126			nesting_level_left (MAX_NESTING_LEVEL),
127			lookup_props (0), property (0), debug_depth (0),
128			gdef (*hb_ot_layout_from_face (face)->gdef),
129			has_glyph_classes (gdef.has_glyph_classes ()),
130			digest (*digest_) {}
131
132  void set_lookup (const Lookup &l) {
133    lookup_props = l.get_props ();
134  }
135
136  struct mark_skipping_forward_iterator_t
137  {
138    inline mark_skipping_forward_iterator_t (hb_apply_context_t *c_,
139					     unsigned int start_index_,
140					     unsigned int num_items_,
141					     bool context_match = false)
142    {
143      c = c_;
144      idx = start_index_;
145      num_items = num_items_;
146      mask = context_match ? -1 : c->lookup_mask;
147      syllable = context_match ? 0 : c->buffer->cur().syllable ();
148      end = c->buffer->len;
149    }
150    inline bool has_no_chance (void) const
151    {
152      return unlikely (num_items && idx + num_items >= end);
153    }
154    inline void reject (void)
155    {
156      num_items++;
157    }
158    inline bool next (unsigned int *property_out,
159		      unsigned int  lookup_props)
160    {
161      assert (num_items > 0);
162      do
163      {
164	if (has_no_chance ())
165	  return false;
166	idx++;
167      } while (c->should_skip_mark (&c->buffer->info[idx], lookup_props, property_out));
168      num_items--;
169      return (c->buffer->info[idx].mask & mask) && (!syllable || syllable == c->buffer->info[idx].syllable ());
170    }
171    inline bool next (unsigned int *property_out = NULL)
172    {
173      return next (property_out, c->lookup_props);
174    }
175
176    unsigned int idx;
177    protected:
178    hb_apply_context_t *c;
179    unsigned int num_items;
180    hb_mask_t mask;
181    uint8_t syllable;
182    unsigned int end;
183  };
184
185  struct mark_skipping_backward_iterator_t
186  {
187    inline mark_skipping_backward_iterator_t (hb_apply_context_t *c_,
188					      unsigned int start_index_,
189					      unsigned int num_items_,
190					      hb_mask_t mask_ = 0,
191					      bool match_syllable_ = true)
192    {
193      c = c_;
194      idx = start_index_;
195      num_items = num_items_;
196      mask = mask_ ? mask_ : c->lookup_mask;
197      syllable = match_syllable_ ? c->buffer->cur().syllable () : 0;
198    }
199    inline bool has_no_chance (void) const
200    {
201      return unlikely (idx < num_items);
202    }
203    inline void reject (void)
204    {
205      num_items++;
206    }
207    inline bool prev (unsigned int *property_out,
208		      unsigned int  lookup_props)
209    {
210      assert (num_items > 0);
211      do
212      {
213	if (has_no_chance ())
214	  return false;
215	idx--;
216      } while (c->should_skip_mark (&c->buffer->out_info[idx], lookup_props, property_out));
217      num_items--;
218      return (c->buffer->out_info[idx].mask & mask) && (!syllable || syllable == c->buffer->out_info[idx].syllable ());
219    }
220    inline bool prev (unsigned int *property_out = NULL)
221    {
222      return prev (property_out, c->lookup_props);
223    }
224
225    unsigned int idx;
226    protected:
227    hb_apply_context_t *c;
228    unsigned int num_items;
229    hb_mask_t mask;
230    uint8_t syllable;
231  };
232
233  inline bool
234  match_properties_mark (hb_codepoint_t  glyph,
235			 unsigned int    glyph_props,
236			 unsigned int    lookup_props) const
237  {
238    /* If using mark filtering sets, the high short of
239     * lookup_props has the set index.
240     */
241    if (lookup_props & LookupFlag::UseMarkFilteringSet)
242      return gdef.mark_set_covers (lookup_props >> 16, glyph);
243
244    /* The second byte of lookup_props has the meaning
245     * "ignore marks of attachment type different than
246     * the attachment type specified."
247     */
248    if (lookup_props & LookupFlag::MarkAttachmentType)
249      return (lookup_props & LookupFlag::MarkAttachmentType) == (glyph_props & LookupFlag::MarkAttachmentType);
250
251    return true;
252  }
253
254  inline bool
255  match_properties (hb_codepoint_t  glyph,
256		    unsigned int    glyph_props,
257		    unsigned int    lookup_props) const
258  {
259    /* Not covered, if, for example, glyph class is ligature and
260     * lookup_props includes LookupFlags::IgnoreLigatures
261     */
262    if (glyph_props & lookup_props & LookupFlag::IgnoreFlags)
263      return false;
264
265    if (unlikely (glyph_props & HB_OT_LAYOUT_GLYPH_CLASS_MARK))
266      return match_properties_mark (glyph, glyph_props, lookup_props);
267
268    return true;
269  }
270
271  inline bool
272  check_glyph_property (hb_glyph_info_t *info,
273			unsigned int  lookup_props,
274			unsigned int *property_out) const
275  {
276    unsigned int property;
277
278    property = info->glyph_props();
279    *property_out = property;
280
281    return match_properties (info->codepoint, property, lookup_props);
282  }
283
284  inline bool
285  should_skip_mark (hb_glyph_info_t *info,
286		   unsigned int  lookup_props,
287		   unsigned int *property_out) const
288  {
289    unsigned int property;
290
291    property = info->glyph_props();
292    if (property_out)
293      *property_out = property;
294
295    /* If it's a mark, skip it if we don't accept it. */
296    if (unlikely (property & HB_OT_LAYOUT_GLYPH_CLASS_MARK))
297      return !match_properties (info->codepoint, property, lookup_props);
298
299    /* If not a mark, don't skip. */
300    return false;
301  }
302
303
304  inline bool should_mark_skip_current_glyph (void) const
305  {
306    return should_skip_mark (&buffer->cur(), lookup_props, NULL);
307  }
308
309  inline void set_class (hb_codepoint_t glyph_index, unsigned int class_guess) const
310  {
311    if (likely (has_glyph_classes))
312      buffer->cur().glyph_props() = gdef.get_glyph_props (glyph_index);
313    else if (class_guess)
314      buffer->cur().glyph_props() = class_guess;
315  }
316
317  inline void output_glyph (hb_codepoint_t glyph_index,
318			    unsigned int class_guess = 0) const
319  {
320    set_class (glyph_index, class_guess);
321    buffer->output_glyph (glyph_index);
322  }
323  inline void replace_glyph (hb_codepoint_t glyph_index,
324			     unsigned int class_guess = 0) const
325  {
326    set_class (glyph_index, class_guess);
327    buffer->replace_glyph (glyph_index);
328  }
329  inline void replace_glyph_inplace (hb_codepoint_t glyph_index,
330				     unsigned int class_guess = 0) const
331  {
332    set_class (glyph_index, class_guess);
333    buffer->cur().codepoint = glyph_index;
334  }
335};
336
337
338
339typedef bool (*intersects_func_t) (hb_set_t *glyphs, const USHORT &value, const void *data);
340typedef bool (*match_func_t) (hb_codepoint_t glyph_id, const USHORT &value, const void *data);
341typedef void (*closure_lookup_func_t) (hb_closure_context_t *c, unsigned int lookup_index);
342typedef bool (*apply_lookup_func_t) (hb_apply_context_t *c, unsigned int lookup_index);
343
344struct ContextClosureFuncs
345{
346  intersects_func_t intersects;
347  closure_lookup_func_t closure;
348};
349struct ContextApplyFuncs
350{
351  match_func_t match;
352  apply_lookup_func_t apply;
353};
354
355static inline bool intersects_glyph (hb_set_t *glyphs, const USHORT &value, const void *data HB_UNUSED)
356{
357  return glyphs->has (value);
358}
359static inline bool intersects_class (hb_set_t *glyphs, const USHORT &value, const void *data)
360{
361  const ClassDef &class_def = *reinterpret_cast<const ClassDef *>(data);
362  return class_def.intersects_class (glyphs, value);
363}
364static inline bool intersects_coverage (hb_set_t *glyphs, const USHORT &value, const void *data)
365{
366  const OffsetTo<Coverage> &coverage = (const OffsetTo<Coverage>&)value;
367  return (data+coverage).intersects (glyphs);
368}
369
370static inline bool intersects_array (hb_closure_context_t *c,
371				     unsigned int count,
372				     const USHORT values[],
373				     intersects_func_t intersects_func,
374				     const void *intersects_data)
375{
376  for (unsigned int i = 0; i < count; i++)
377    if (likely (!intersects_func (c->glyphs, values[i], intersects_data)))
378      return false;
379  return true;
380}
381
382
383static inline bool match_glyph (hb_codepoint_t glyph_id, const USHORT &value, const void *data HB_UNUSED)
384{
385  return glyph_id == value;
386}
387static inline bool match_class (hb_codepoint_t glyph_id, const USHORT &value, const void *data)
388{
389  const ClassDef &class_def = *reinterpret_cast<const ClassDef *>(data);
390  return class_def.get_class (glyph_id) == value;
391}
392static inline bool match_coverage (hb_codepoint_t glyph_id, const USHORT &value, const void *data)
393{
394  const OffsetTo<Coverage> &coverage = (const OffsetTo<Coverage>&)value;
395  return (data+coverage).get_coverage (glyph_id) != NOT_COVERED;
396}
397
398
399static inline bool would_match_input (hb_would_apply_context_t *c,
400				      unsigned int count, /* Including the first glyph (not matched) */
401				      const USHORT input[], /* Array of input values--start with second glyph */
402				      match_func_t match_func,
403				      const void *match_data)
404{
405  if (count != c->len)
406    return false;
407
408  for (unsigned int i = 1; i < count; i++)
409    if (likely (!match_func (c->glyphs[i], input[i - 1], match_data)))
410      return false;
411
412  return true;
413}
414static inline bool match_input (hb_apply_context_t *c,
415				unsigned int count, /* Including the first glyph (not matched) */
416				const USHORT input[], /* Array of input values--start with second glyph */
417				match_func_t match_func,
418				const void *match_data,
419				unsigned int *end_offset = NULL)
420{
421  hb_apply_context_t::mark_skipping_forward_iterator_t skippy_iter (c, c->buffer->idx, count - 1);
422  if (skippy_iter.has_no_chance ())
423    return false;
424
425  for (unsigned int i = 1; i < count; i++)
426  {
427    if (!skippy_iter.next ())
428      return false;
429
430    if (likely (!match_func (c->buffer->info[skippy_iter.idx].codepoint, input[i - 1], match_data)))
431      return false;
432  }
433
434  if (end_offset)
435    *end_offset = skippy_iter.idx - c->buffer->idx + 1;
436
437  return true;
438}
439
440static inline bool match_backtrack (hb_apply_context_t *c,
441				    unsigned int count,
442				    const USHORT backtrack[],
443				    match_func_t match_func,
444				    const void *match_data)
445{
446  hb_apply_context_t::mark_skipping_backward_iterator_t skippy_iter (c, c->buffer->backtrack_len (), count, true);
447  if (skippy_iter.has_no_chance ())
448    return false;
449
450  for (unsigned int i = 0; i < count; i++)
451  {
452    if (!skippy_iter.prev ())
453      return false;
454
455    if (likely (!match_func (c->buffer->out_info[skippy_iter.idx].codepoint, backtrack[i], match_data)))
456      return false;
457  }
458
459  return true;
460}
461
462static inline bool match_lookahead (hb_apply_context_t *c,
463				    unsigned int count,
464				    const USHORT lookahead[],
465				    match_func_t match_func,
466				    const void *match_data,
467				    unsigned int offset)
468{
469  hb_apply_context_t::mark_skipping_forward_iterator_t skippy_iter (c, c->buffer->idx + offset - 1, count, true);
470  if (skippy_iter.has_no_chance ())
471    return false;
472
473  for (unsigned int i = 0; i < count; i++)
474  {
475    if (!skippy_iter.next ())
476      return false;
477
478    if (likely (!match_func (c->buffer->info[skippy_iter.idx].codepoint, lookahead[i], match_data)))
479      return false;
480  }
481
482  return true;
483}
484
485
486
487struct LookupRecord
488{
489  inline bool sanitize (hb_sanitize_context_t *c) {
490    TRACE_SANITIZE ();
491    return TRACE_RETURN (c->check_struct (this));
492  }
493
494  USHORT	sequenceIndex;		/* Index into current glyph
495					 * sequence--first glyph = 0 */
496  USHORT	lookupListIndex;	/* Lookup to apply to that
497					 * position--zero--based */
498  public:
499  DEFINE_SIZE_STATIC (4);
500};
501
502
503static inline void closure_lookup (hb_closure_context_t *c,
504				   unsigned int lookupCount,
505				   const LookupRecord lookupRecord[], /* Array of LookupRecords--in design order */
506				   closure_lookup_func_t closure_func)
507{
508  for (unsigned int i = 0; i < lookupCount; i++)
509    closure_func (c, lookupRecord->lookupListIndex);
510}
511
512static inline bool apply_lookup (hb_apply_context_t *c,
513				 unsigned int count, /* Including the first glyph */
514				 unsigned int lookupCount,
515				 const LookupRecord lookupRecord[], /* Array of LookupRecords--in design order */
516				 apply_lookup_func_t apply_func)
517{
518  unsigned int end = c->buffer->len;
519  if (unlikely (count == 0 || c->buffer->idx + count > end))
520    return false;
521
522  /* TODO We don't support lookupRecord arrays that are not increasing:
523   *      Should be easy for in_place ones at least. */
524
525  /* Note: If sublookup is reverse, it will underflow after the first loop
526   * and we jump out of it.  Not entirely disastrous.  So we don't check
527   * for reverse lookup here.
528   */
529  for (unsigned int i = 0; i < count; /* NOP */)
530  {
531    if (unlikely (c->buffer->idx == end))
532      return true;
533    while (c->should_mark_skip_current_glyph ())
534    {
535      /* No lookup applied for this index */
536      c->buffer->next_glyph ();
537      if (unlikely (c->buffer->idx == end))
538	return true;
539    }
540
541    if (lookupCount && i == lookupRecord->sequenceIndex)
542    {
543      unsigned int old_pos = c->buffer->idx;
544
545      /* Apply a lookup */
546      bool done = apply_func (c, lookupRecord->lookupListIndex);
547
548      lookupRecord++;
549      lookupCount--;
550      /* Err, this is wrong if the lookup jumped over some glyphs */
551      i += c->buffer->idx - old_pos;
552      if (unlikely (c->buffer->idx == end))
553	return true;
554
555      if (!done)
556	goto not_applied;
557    }
558    else
559    {
560    not_applied:
561      /* No lookup applied for this index */
562      c->buffer->next_glyph ();
563      i++;
564    }
565  }
566
567  return true;
568}
569
570
571
572/* Contextual lookups */
573
574struct ContextClosureLookupContext
575{
576  ContextClosureFuncs funcs;
577  const void *intersects_data;
578};
579
580struct ContextApplyLookupContext
581{
582  ContextApplyFuncs funcs;
583  const void *match_data;
584};
585
586static inline void context_closure_lookup (hb_closure_context_t *c,
587					   unsigned int inputCount, /* Including the first glyph (not matched) */
588					   const USHORT input[], /* Array of input values--start with second glyph */
589					   unsigned int lookupCount,
590					   const LookupRecord lookupRecord[],
591					   ContextClosureLookupContext &lookup_context)
592{
593  if (intersects_array (c,
594			inputCount ? inputCount - 1 : 0, input,
595			lookup_context.funcs.intersects, lookup_context.intersects_data))
596    closure_lookup (c,
597		    lookupCount, lookupRecord,
598		    lookup_context.funcs.closure);
599}
600
601
602static inline bool context_would_apply_lookup (hb_would_apply_context_t *c,
603					       unsigned int inputCount, /* Including the first glyph (not matched) */
604					       const USHORT input[], /* Array of input values--start with second glyph */
605					       unsigned int lookupCount,
606					       const LookupRecord lookupRecord[],
607					       ContextApplyLookupContext &lookup_context)
608{
609  return would_match_input (c,
610			    inputCount, input,
611			    lookup_context.funcs.match, lookup_context.match_data);
612}
613static inline bool context_apply_lookup (hb_apply_context_t *c,
614					 unsigned int inputCount, /* Including the first glyph (not matched) */
615					 const USHORT input[], /* Array of input values--start with second glyph */
616					 unsigned int lookupCount,
617					 const LookupRecord lookupRecord[],
618					 ContextApplyLookupContext &lookup_context)
619{
620  return match_input (c,
621		      inputCount, input,
622		      lookup_context.funcs.match, lookup_context.match_data)
623      && apply_lookup (c,
624		       inputCount,
625		       lookupCount, lookupRecord,
626		       lookup_context.funcs.apply);
627}
628
629struct Rule
630{
631  friend struct RuleSet;
632
633  private:
634
635  inline void closure (hb_closure_context_t *c, ContextClosureLookupContext &lookup_context) const
636  {
637    TRACE_CLOSURE ();
638    const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (input, input[0].static_size * (inputCount ? inputCount - 1 : 0));
639    context_closure_lookup (c,
640			    inputCount, input,
641			    lookupCount, lookupRecord,
642			    lookup_context);
643  }
644
645  inline bool would_apply (hb_would_apply_context_t *c, ContextApplyLookupContext &lookup_context) const
646  {
647    TRACE_WOULD_APPLY ();
648    const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (input, input[0].static_size * (inputCount ? inputCount - 1 : 0));
649    return TRACE_RETURN (context_would_apply_lookup (c, inputCount, input, lookupCount, lookupRecord, lookup_context));
650  }
651
652  inline bool apply (hb_apply_context_t *c, ContextApplyLookupContext &lookup_context) const
653  {
654    TRACE_APPLY ();
655    const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (input, input[0].static_size * (inputCount ? inputCount - 1 : 0));
656    return TRACE_RETURN (context_apply_lookup (c, inputCount, input, lookupCount, lookupRecord, lookup_context));
657  }
658
659  public:
660  inline bool sanitize (hb_sanitize_context_t *c) {
661    TRACE_SANITIZE ();
662    return inputCount.sanitize (c)
663	&& lookupCount.sanitize (c)
664	&& c->check_range (input,
665			   input[0].static_size * inputCount
666			   + lookupRecordX[0].static_size * lookupCount);
667  }
668
669  protected:
670  USHORT	inputCount;		/* Total number of glyphs in input
671					 * glyph sequence--includes the first
672					 * glyph */
673  USHORT	lookupCount;		/* Number of LookupRecords */
674  USHORT	input[VAR];		/* Array of match inputs--start with
675					 * second glyph */
676  LookupRecord	lookupRecordX[VAR];	/* Array of LookupRecords--in
677					 * design order */
678  public:
679  DEFINE_SIZE_ARRAY2 (4, input, lookupRecordX);
680};
681
682struct RuleSet
683{
684  inline void closure (hb_closure_context_t *c, ContextClosureLookupContext &lookup_context) const
685  {
686    TRACE_CLOSURE ();
687    unsigned int num_rules = rule.len;
688    for (unsigned int i = 0; i < num_rules; i++)
689      (this+rule[i]).closure (c, lookup_context);
690  }
691
692  inline bool would_apply (hb_would_apply_context_t *c, ContextApplyLookupContext &lookup_context) const
693  {
694    TRACE_WOULD_APPLY ();
695    unsigned int num_rules = rule.len;
696    for (unsigned int i = 0; i < num_rules; i++)
697    {
698      if ((this+rule[i]).would_apply (c, lookup_context))
699        return TRACE_RETURN (true);
700    }
701    return TRACE_RETURN (false);
702  }
703
704  inline bool apply (hb_apply_context_t *c, ContextApplyLookupContext &lookup_context) const
705  {
706    TRACE_APPLY ();
707    unsigned int num_rules = rule.len;
708    for (unsigned int i = 0; i < num_rules; i++)
709    {
710      if ((this+rule[i]).apply (c, lookup_context))
711        return TRACE_RETURN (true);
712    }
713    return TRACE_RETURN (false);
714  }
715
716  inline bool sanitize (hb_sanitize_context_t *c) {
717    TRACE_SANITIZE ();
718    return TRACE_RETURN (rule.sanitize (c, this));
719  }
720
721  protected:
722  OffsetArrayOf<Rule>
723		rule;			/* Array of Rule tables
724					 * ordered by preference */
725  public:
726  DEFINE_SIZE_ARRAY (2, rule);
727};
728
729
730struct ContextFormat1
731{
732  friend struct Context;
733
734  private:
735
736  inline void closure (hb_closure_context_t *c, closure_lookup_func_t closure_func) const
737  {
738    TRACE_CLOSURE ();
739
740    const Coverage &cov = (this+coverage);
741
742    struct ContextClosureLookupContext lookup_context = {
743      {intersects_glyph, closure_func},
744      NULL
745    };
746
747    unsigned int count = ruleSet.len;
748    for (unsigned int i = 0; i < count; i++)
749      if (cov.intersects_coverage (c->glyphs, i)) {
750	const RuleSet &rule_set = this+ruleSet[i];
751	rule_set.closure (c, lookup_context);
752      }
753  }
754
755  inline bool would_apply (hb_would_apply_context_t *c) const
756  {
757    TRACE_WOULD_APPLY ();
758
759    const RuleSet &rule_set = this+ruleSet[(this+coverage) (c->glyphs[0])];
760    struct ContextApplyLookupContext lookup_context = {
761      {match_glyph, NULL},
762      NULL
763    };
764    return TRACE_RETURN (rule_set.would_apply (c, lookup_context));
765  }
766
767  inline bool apply (hb_apply_context_t *c, apply_lookup_func_t apply_func) const
768  {
769    TRACE_APPLY ();
770    unsigned int index = (this+coverage) (c->buffer->cur().codepoint);
771    if (likely (index == NOT_COVERED))
772      return TRACE_RETURN (false);
773
774    const RuleSet &rule_set = this+ruleSet[index];
775    struct ContextApplyLookupContext lookup_context = {
776      {match_glyph, apply_func},
777      NULL
778    };
779    return TRACE_RETURN (rule_set.apply (c, lookup_context));
780  }
781
782  inline bool sanitize (hb_sanitize_context_t *c) {
783    TRACE_SANITIZE ();
784    return TRACE_RETURN (coverage.sanitize (c, this) && ruleSet.sanitize (c, this));
785  }
786
787  protected:
788  USHORT	format;			/* Format identifier--format = 1 */
789  OffsetTo<Coverage>
790		coverage;		/* Offset to Coverage table--from
791					 * beginning of table */
792  OffsetArrayOf<RuleSet>
793		ruleSet;		/* Array of RuleSet tables
794					 * ordered by Coverage Index */
795  public:
796  DEFINE_SIZE_ARRAY (6, ruleSet);
797};
798
799
800struct ContextFormat2
801{
802  friend struct Context;
803
804  private:
805
806  inline void closure (hb_closure_context_t *c, closure_lookup_func_t closure_func) const
807  {
808    TRACE_CLOSURE ();
809    if (!(this+coverage).intersects (c->glyphs))
810      return;
811
812    const ClassDef &class_def = this+classDef;
813
814    struct ContextClosureLookupContext lookup_context = {
815      {intersects_class, closure_func},
816      NULL
817    };
818
819    unsigned int count = ruleSet.len;
820    for (unsigned int i = 0; i < count; i++)
821      if (class_def.intersects_class (c->glyphs, i)) {
822	const RuleSet &rule_set = this+ruleSet[i];
823	rule_set.closure (c, lookup_context);
824      }
825  }
826
827  inline bool would_apply (hb_would_apply_context_t *c) const
828  {
829    TRACE_WOULD_APPLY ();
830
831    const ClassDef &class_def = this+classDef;
832    unsigned int index = class_def (c->glyphs[0]);
833    const RuleSet &rule_set = this+ruleSet[index];
834    struct ContextApplyLookupContext lookup_context = {
835      {match_class, NULL},
836      &class_def
837    };
838    return TRACE_RETURN (rule_set.would_apply (c, lookup_context));
839  }
840
841  inline bool apply (hb_apply_context_t *c, apply_lookup_func_t apply_func) const
842  {
843    TRACE_APPLY ();
844    unsigned int index = (this+coverage) (c->buffer->cur().codepoint);
845    if (likely (index == NOT_COVERED)) return TRACE_RETURN (false);
846
847    const ClassDef &class_def = this+classDef;
848    index = class_def (c->buffer->cur().codepoint);
849    const RuleSet &rule_set = this+ruleSet[index];
850    struct ContextApplyLookupContext lookup_context = {
851      {match_class, apply_func},
852      &class_def
853    };
854    return TRACE_RETURN (rule_set.apply (c, lookup_context));
855  }
856
857  inline bool sanitize (hb_sanitize_context_t *c) {
858    TRACE_SANITIZE ();
859    return TRACE_RETURN (coverage.sanitize (c, this) && classDef.sanitize (c, this) && ruleSet.sanitize (c, this));
860  }
861
862  protected:
863  USHORT	format;			/* Format identifier--format = 2 */
864  OffsetTo<Coverage>
865		coverage;		/* Offset to Coverage table--from
866					 * beginning of table */
867  OffsetTo<ClassDef>
868		classDef;		/* Offset to glyph ClassDef table--from
869					 * beginning of table */
870  OffsetArrayOf<RuleSet>
871		ruleSet;		/* Array of RuleSet tables
872					 * ordered by class */
873  public:
874  DEFINE_SIZE_ARRAY (8, ruleSet);
875};
876
877
878struct ContextFormat3
879{
880  friend struct Context;
881
882  private:
883
884  inline void closure (hb_closure_context_t *c, closure_lookup_func_t closure_func) const
885  {
886    TRACE_CLOSURE ();
887    if (!(this+coverage[0]).intersects (c->glyphs))
888      return;
889
890    const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (coverage, coverage[0].static_size * glyphCount);
891    struct ContextClosureLookupContext lookup_context = {
892      {intersects_coverage, closure_func},
893      this
894    };
895    context_closure_lookup (c,
896			    glyphCount, (const USHORT *) (coverage + 1),
897			    lookupCount, lookupRecord,
898			    lookup_context);
899  }
900
901  inline bool would_apply (hb_would_apply_context_t *c) const
902  {
903    TRACE_WOULD_APPLY ();
904
905    const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (coverage, coverage[0].static_size * glyphCount);
906    struct ContextApplyLookupContext lookup_context = {
907      {match_coverage, NULL},
908      this
909    };
910    return TRACE_RETURN (context_would_apply_lookup (c, glyphCount, (const USHORT *) (coverage + 1), lookupCount, lookupRecord, lookup_context));
911  }
912
913  inline bool apply (hb_apply_context_t *c, apply_lookup_func_t apply_func) const
914  {
915    TRACE_APPLY ();
916    unsigned int index = (this+coverage[0]) (c->buffer->cur().codepoint);
917    if (likely (index == NOT_COVERED)) return TRACE_RETURN (false);
918
919    const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (coverage, coverage[0].static_size * glyphCount);
920    struct ContextApplyLookupContext lookup_context = {
921      {match_coverage, apply_func},
922      this
923    };
924    return TRACE_RETURN (context_apply_lookup (c, glyphCount, (const USHORT *) (coverage + 1), lookupCount, lookupRecord, lookup_context));
925  }
926
927  inline bool sanitize (hb_sanitize_context_t *c) {
928    TRACE_SANITIZE ();
929    if (!c->check_struct (this)) return TRACE_RETURN (false);
930    unsigned int count = glyphCount;
931    if (!c->check_array (coverage, coverage[0].static_size, count)) return TRACE_RETURN (false);
932    for (unsigned int i = 0; i < count; i++)
933      if (!coverage[i].sanitize (c, this)) return TRACE_RETURN (false);
934    LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (coverage, coverage[0].static_size * count);
935    return TRACE_RETURN (c->check_array (lookupRecord, lookupRecord[0].static_size, lookupCount));
936  }
937
938  protected:
939  USHORT	format;			/* Format identifier--format = 3 */
940  USHORT	glyphCount;		/* Number of glyphs in the input glyph
941					 * sequence */
942  USHORT	lookupCount;		/* Number of LookupRecords */
943  OffsetTo<Coverage>
944		coverage[VAR];		/* Array of offsets to Coverage
945					 * table in glyph sequence order */
946  LookupRecord	lookupRecordX[VAR];	/* Array of LookupRecords--in
947					 * design order */
948  public:
949  DEFINE_SIZE_ARRAY2 (6, coverage, lookupRecordX);
950};
951
952struct Context
953{
954  protected:
955
956  inline void closure (hb_closure_context_t *c, closure_lookup_func_t closure_func) const
957  {
958    TRACE_CLOSURE ();
959    switch (u.format) {
960    case 1: u.format1.closure (c, closure_func); break;
961    case 2: u.format2.closure (c, closure_func); break;
962    case 3: u.format3.closure (c, closure_func); break;
963    default:                                     break;
964    }
965  }
966
967  inline const Coverage &get_coverage (void) const
968  {
969    switch (u.format) {
970    case 1: return this + u.format1.coverage;
971    case 2: return this + u.format2.coverage;
972    case 3: return this + u.format3.coverage[0];
973    default:return Null(Coverage);
974    }
975  }
976
977  inline bool would_apply (hb_would_apply_context_t *c) const
978  {
979    switch (u.format) {
980    case 1: return u.format1.would_apply (c);
981    case 2: return u.format2.would_apply (c);
982    case 3: return u.format3.would_apply (c);
983    default:return false;
984    }
985  }
986
987  inline bool apply (hb_apply_context_t *c, apply_lookup_func_t apply_func) const
988  {
989    TRACE_APPLY ();
990    switch (u.format) {
991    case 1: return TRACE_RETURN (u.format1.apply (c, apply_func));
992    case 2: return TRACE_RETURN (u.format2.apply (c, apply_func));
993    case 3: return TRACE_RETURN (u.format3.apply (c, apply_func));
994    default:return TRACE_RETURN (false);
995    }
996  }
997
998  inline bool sanitize (hb_sanitize_context_t *c) {
999    TRACE_SANITIZE ();
1000    if (!u.format.sanitize (c)) return TRACE_RETURN (false);
1001    switch (u.format) {
1002    case 1: return TRACE_RETURN (u.format1.sanitize (c));
1003    case 2: return TRACE_RETURN (u.format2.sanitize (c));
1004    case 3: return TRACE_RETURN (u.format3.sanitize (c));
1005    default:return TRACE_RETURN (true);
1006    }
1007  }
1008
1009  protected:
1010  union {
1011  USHORT		format;		/* Format identifier */
1012  ContextFormat1	format1;
1013  ContextFormat2	format2;
1014  ContextFormat3	format3;
1015  } u;
1016};
1017
1018
1019/* Chaining Contextual lookups */
1020
1021struct ChainContextClosureLookupContext
1022{
1023  ContextClosureFuncs funcs;
1024  const void *intersects_data[3];
1025};
1026
1027struct ChainContextApplyLookupContext
1028{
1029  ContextApplyFuncs funcs;
1030  const void *match_data[3];
1031};
1032
1033static inline void chain_context_closure_lookup (hb_closure_context_t *c,
1034						 unsigned int backtrackCount,
1035						 const USHORT backtrack[],
1036						 unsigned int inputCount, /* Including the first glyph (not matched) */
1037						 const USHORT input[], /* Array of input values--start with second glyph */
1038						 unsigned int lookaheadCount,
1039						 const USHORT lookahead[],
1040						 unsigned int lookupCount,
1041						 const LookupRecord lookupRecord[],
1042						 ChainContextClosureLookupContext &lookup_context)
1043{
1044  if (intersects_array (c,
1045			backtrackCount, backtrack,
1046			lookup_context.funcs.intersects, lookup_context.intersects_data[0])
1047   && intersects_array (c,
1048			inputCount ? inputCount - 1 : 0, input,
1049			lookup_context.funcs.intersects, lookup_context.intersects_data[1])
1050  && intersects_array (c,
1051		       lookaheadCount, lookahead,
1052		       lookup_context.funcs.intersects, lookup_context.intersects_data[2]))
1053    closure_lookup (c,
1054		    lookupCount, lookupRecord,
1055		    lookup_context.funcs.closure);
1056}
1057
1058static inline bool chain_context_would_apply_lookup (hb_would_apply_context_t *c,
1059						     unsigned int backtrackCount,
1060						     const USHORT backtrack[],
1061						     unsigned int inputCount, /* Including the first glyph (not matched) */
1062						     const USHORT input[], /* Array of input values--start with second glyph */
1063						     unsigned int lookaheadCount,
1064						     const USHORT lookahead[],
1065						     unsigned int lookupCount,
1066						     const LookupRecord lookupRecord[],
1067						     ChainContextApplyLookupContext &lookup_context)
1068{
1069  /* The MS Indic specs say "...all classifications are determined ... using context-free substitutions."
1070   * However, testing shows that MS's Malayalam shapers (both old and new), "match" even if there is no
1071   * zero-context rule.  We follow.  Hence the commented out line. */
1072  return /* !backtrackCount && !lookaheadCount && */
1073         would_match_input (c,
1074			    inputCount, input,
1075			    lookup_context.funcs.match, lookup_context.match_data[1]);
1076}
1077
1078static inline bool chain_context_apply_lookup (hb_apply_context_t *c,
1079					       unsigned int backtrackCount,
1080					       const USHORT backtrack[],
1081					       unsigned int inputCount, /* Including the first glyph (not matched) */
1082					       const USHORT input[], /* Array of input values--start with second glyph */
1083					       unsigned int lookaheadCount,
1084					       const USHORT lookahead[],
1085					       unsigned int lookupCount,
1086					       const LookupRecord lookupRecord[],
1087					       ChainContextApplyLookupContext &lookup_context)
1088{
1089  unsigned int lookahead_offset;
1090  return match_input (c,
1091		      inputCount, input,
1092		      lookup_context.funcs.match, lookup_context.match_data[1],
1093		      &lookahead_offset)
1094      && match_backtrack (c,
1095			  backtrackCount, backtrack,
1096			  lookup_context.funcs.match, lookup_context.match_data[0])
1097      && match_lookahead (c,
1098			  lookaheadCount, lookahead,
1099			  lookup_context.funcs.match, lookup_context.match_data[2],
1100			  lookahead_offset)
1101      && apply_lookup (c,
1102		       inputCount,
1103		       lookupCount, lookupRecord,
1104		       lookup_context.funcs.apply);
1105}
1106
1107struct ChainRule
1108{
1109  friend struct ChainRuleSet;
1110
1111  private:
1112
1113  inline void closure (hb_closure_context_t *c, ChainContextClosureLookupContext &lookup_context) const
1114  {
1115    TRACE_CLOSURE ();
1116    const HeadlessArrayOf<USHORT> &input = StructAfter<HeadlessArrayOf<USHORT> > (backtrack);
1117    const ArrayOf<USHORT> &lookahead = StructAfter<ArrayOf<USHORT> > (input);
1118    const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
1119    chain_context_closure_lookup (c,
1120				  backtrack.len, backtrack.array,
1121				  input.len, input.array,
1122				  lookahead.len, lookahead.array,
1123				  lookup.len, lookup.array,
1124				  lookup_context);
1125  }
1126
1127  inline bool would_apply (hb_would_apply_context_t *c, ChainContextApplyLookupContext &lookup_context) const
1128  {
1129    TRACE_WOULD_APPLY ();
1130    const HeadlessArrayOf<USHORT> &input = StructAfter<HeadlessArrayOf<USHORT> > (backtrack);
1131    const ArrayOf<USHORT> &lookahead = StructAfter<ArrayOf<USHORT> > (input);
1132    const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
1133    return TRACE_RETURN (chain_context_would_apply_lookup (c,
1134							   backtrack.len, backtrack.array,
1135							   input.len, input.array,
1136							   lookahead.len, lookahead.array, lookup.len,
1137							   lookup.array, lookup_context));
1138  }
1139
1140  inline bool apply (hb_apply_context_t *c, ChainContextApplyLookupContext &lookup_context) const
1141  {
1142    TRACE_APPLY ();
1143    const HeadlessArrayOf<USHORT> &input = StructAfter<HeadlessArrayOf<USHORT> > (backtrack);
1144    const ArrayOf<USHORT> &lookahead = StructAfter<ArrayOf<USHORT> > (input);
1145    const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
1146    return TRACE_RETURN (chain_context_apply_lookup (c,
1147						     backtrack.len, backtrack.array,
1148						     input.len, input.array,
1149						     lookahead.len, lookahead.array, lookup.len,
1150						     lookup.array, lookup_context));
1151  }
1152
1153  public:
1154  inline bool sanitize (hb_sanitize_context_t *c) {
1155    TRACE_SANITIZE ();
1156    if (!backtrack.sanitize (c)) return TRACE_RETURN (false);
1157    HeadlessArrayOf<USHORT> &input = StructAfter<HeadlessArrayOf<USHORT> > (backtrack);
1158    if (!input.sanitize (c)) return TRACE_RETURN (false);
1159    ArrayOf<USHORT> &lookahead = StructAfter<ArrayOf<USHORT> > (input);
1160    if (!lookahead.sanitize (c)) return TRACE_RETURN (false);
1161    ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
1162    return TRACE_RETURN (lookup.sanitize (c));
1163  }
1164
1165  protected:
1166  ArrayOf<USHORT>
1167		backtrack;		/* Array of backtracking values
1168					 * (to be matched before the input
1169					 * sequence) */
1170  HeadlessArrayOf<USHORT>
1171		inputX;			/* Array of input values (start with
1172					 * second glyph) */
1173  ArrayOf<USHORT>
1174		lookaheadX;		/* Array of lookahead values's (to be
1175					 * matched after the input sequence) */
1176  ArrayOf<LookupRecord>
1177		lookupX;		/* Array of LookupRecords--in
1178					 * design order) */
1179  public:
1180  DEFINE_SIZE_MIN (8);
1181};
1182
1183struct ChainRuleSet
1184{
1185  inline void closure (hb_closure_context_t *c, ChainContextClosureLookupContext &lookup_context) const
1186  {
1187    TRACE_CLOSURE ();
1188    unsigned int num_rules = rule.len;
1189    for (unsigned int i = 0; i < num_rules; i++)
1190      (this+rule[i]).closure (c, lookup_context);
1191  }
1192
1193  inline bool would_apply (hb_would_apply_context_t *c, ChainContextApplyLookupContext &lookup_context) const
1194  {
1195    TRACE_WOULD_APPLY ();
1196    unsigned int num_rules = rule.len;
1197    for (unsigned int i = 0; i < num_rules; i++)
1198      if ((this+rule[i]).would_apply (c, lookup_context))
1199        return TRACE_RETURN (true);
1200
1201    return TRACE_RETURN (false);
1202  }
1203
1204  inline bool apply (hb_apply_context_t *c, ChainContextApplyLookupContext &lookup_context) const
1205  {
1206    TRACE_APPLY ();
1207    unsigned int num_rules = rule.len;
1208    for (unsigned int i = 0; i < num_rules; i++)
1209      if ((this+rule[i]).apply (c, lookup_context))
1210        return TRACE_RETURN (true);
1211
1212    return TRACE_RETURN (false);
1213  }
1214
1215  inline bool sanitize (hb_sanitize_context_t *c) {
1216    TRACE_SANITIZE ();
1217    return TRACE_RETURN (rule.sanitize (c, this));
1218  }
1219
1220  protected:
1221  OffsetArrayOf<ChainRule>
1222		rule;			/* Array of ChainRule tables
1223					 * ordered by preference */
1224  public:
1225  DEFINE_SIZE_ARRAY (2, rule);
1226};
1227
1228struct ChainContextFormat1
1229{
1230  friend struct ChainContext;
1231
1232  private:
1233
1234  inline void closure (hb_closure_context_t *c, closure_lookup_func_t closure_func) const
1235  {
1236    TRACE_CLOSURE ();
1237    const Coverage &cov = (this+coverage);
1238
1239    struct ChainContextClosureLookupContext lookup_context = {
1240      {intersects_glyph, closure_func},
1241      {NULL, NULL, NULL}
1242    };
1243
1244    unsigned int count = ruleSet.len;
1245    for (unsigned int i = 0; i < count; i++)
1246      if (cov.intersects_coverage (c->glyphs, i)) {
1247	const ChainRuleSet &rule_set = this+ruleSet[i];
1248	rule_set.closure (c, lookup_context);
1249      }
1250  }
1251
1252  inline bool would_apply (hb_would_apply_context_t *c) const
1253  {
1254    TRACE_WOULD_APPLY ();
1255
1256    const ChainRuleSet &rule_set = this+ruleSet[(this+coverage) (c->glyphs[0])];
1257    struct ChainContextApplyLookupContext lookup_context = {
1258      {match_glyph, NULL},
1259      {NULL, NULL, NULL}
1260    };
1261    return TRACE_RETURN (rule_set.would_apply (c, lookup_context));
1262  }
1263
1264  inline bool apply (hb_apply_context_t *c, apply_lookup_func_t apply_func) const
1265  {
1266    TRACE_APPLY ();
1267    unsigned int index = (this+coverage) (c->buffer->cur().codepoint);
1268    if (likely (index == NOT_COVERED)) return TRACE_RETURN (false);
1269
1270    const ChainRuleSet &rule_set = this+ruleSet[index];
1271    struct ChainContextApplyLookupContext lookup_context = {
1272      {match_glyph, apply_func},
1273      {NULL, NULL, NULL}
1274    };
1275    return TRACE_RETURN (rule_set.apply (c, lookup_context));
1276  }
1277
1278  inline bool sanitize (hb_sanitize_context_t *c) {
1279    TRACE_SANITIZE ();
1280    return TRACE_RETURN (coverage.sanitize (c, this) && ruleSet.sanitize (c, this));
1281  }
1282
1283  protected:
1284  USHORT	format;			/* Format identifier--format = 1 */
1285  OffsetTo<Coverage>
1286		coverage;		/* Offset to Coverage table--from
1287					 * beginning of table */
1288  OffsetArrayOf<ChainRuleSet>
1289		ruleSet;		/* Array of ChainRuleSet tables
1290					 * ordered by Coverage Index */
1291  public:
1292  DEFINE_SIZE_ARRAY (6, ruleSet);
1293};
1294
1295struct ChainContextFormat2
1296{
1297  friend struct ChainContext;
1298
1299  private:
1300
1301  inline void closure (hb_closure_context_t *c, closure_lookup_func_t closure_func) const
1302  {
1303    TRACE_CLOSURE ();
1304    if (!(this+coverage).intersects (c->glyphs))
1305      return;
1306
1307    const ClassDef &backtrack_class_def = this+backtrackClassDef;
1308    const ClassDef &input_class_def = this+inputClassDef;
1309    const ClassDef &lookahead_class_def = this+lookaheadClassDef;
1310
1311    struct ChainContextClosureLookupContext lookup_context = {
1312      {intersects_class, closure_func},
1313      {&backtrack_class_def,
1314       &input_class_def,
1315       &lookahead_class_def}
1316    };
1317
1318    unsigned int count = ruleSet.len;
1319    for (unsigned int i = 0; i < count; i++)
1320      if (input_class_def.intersects_class (c->glyphs, i)) {
1321	const ChainRuleSet &rule_set = this+ruleSet[i];
1322	rule_set.closure (c, lookup_context);
1323      }
1324  }
1325
1326  inline bool would_apply (hb_would_apply_context_t *c) const
1327  {
1328    TRACE_WOULD_APPLY ();
1329
1330    const ClassDef &input_class_def = this+inputClassDef;
1331
1332    unsigned int index = input_class_def (c->glyphs[0]);
1333    const ChainRuleSet &rule_set = this+ruleSet[index];
1334    struct ChainContextApplyLookupContext lookup_context = {
1335      {match_class, NULL},
1336      {NULL, &input_class_def, NULL}
1337    };
1338    return TRACE_RETURN (rule_set.would_apply (c, lookup_context));
1339  }
1340
1341  inline bool apply (hb_apply_context_t *c, apply_lookup_func_t apply_func) const
1342  {
1343    TRACE_APPLY ();
1344    unsigned int index = (this+coverage) (c->buffer->cur().codepoint);
1345    if (likely (index == NOT_COVERED)) return TRACE_RETURN (false);
1346
1347    const ClassDef &backtrack_class_def = this+backtrackClassDef;
1348    const ClassDef &input_class_def = this+inputClassDef;
1349    const ClassDef &lookahead_class_def = this+lookaheadClassDef;
1350
1351    index = input_class_def (c->buffer->cur().codepoint);
1352    const ChainRuleSet &rule_set = this+ruleSet[index];
1353    struct ChainContextApplyLookupContext lookup_context = {
1354      {match_class, apply_func},
1355      {&backtrack_class_def,
1356       &input_class_def,
1357       &lookahead_class_def}
1358    };
1359    return TRACE_RETURN (rule_set.apply (c, lookup_context));
1360  }
1361
1362  inline bool sanitize (hb_sanitize_context_t *c) {
1363    TRACE_SANITIZE ();
1364    return TRACE_RETURN (coverage.sanitize (c, this) && backtrackClassDef.sanitize (c, this) &&
1365			 inputClassDef.sanitize (c, this) && lookaheadClassDef.sanitize (c, this) &&
1366			 ruleSet.sanitize (c, this));
1367  }
1368
1369  protected:
1370  USHORT	format;			/* Format identifier--format = 2 */
1371  OffsetTo<Coverage>
1372		coverage;		/* Offset to Coverage table--from
1373					 * beginning of table */
1374  OffsetTo<ClassDef>
1375		backtrackClassDef;	/* Offset to glyph ClassDef table
1376					 * containing backtrack sequence
1377					 * data--from beginning of table */
1378  OffsetTo<ClassDef>
1379		inputClassDef;		/* Offset to glyph ClassDef
1380					 * table containing input sequence
1381					 * data--from beginning of table */
1382  OffsetTo<ClassDef>
1383		lookaheadClassDef;	/* Offset to glyph ClassDef table
1384					 * containing lookahead sequence
1385					 * data--from beginning of table */
1386  OffsetArrayOf<ChainRuleSet>
1387		ruleSet;		/* Array of ChainRuleSet tables
1388					 * ordered by class */
1389  public:
1390  DEFINE_SIZE_ARRAY (12, ruleSet);
1391};
1392
1393struct ChainContextFormat3
1394{
1395  friend struct ChainContext;
1396
1397  private:
1398
1399  inline void closure (hb_closure_context_t *c, closure_lookup_func_t closure_func) const
1400  {
1401    TRACE_CLOSURE ();
1402    const OffsetArrayOf<Coverage> &input = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
1403
1404    if (!(this+input[0]).intersects (c->glyphs))
1405      return;
1406
1407    const OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (input);
1408    const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
1409    struct ChainContextClosureLookupContext lookup_context = {
1410      {intersects_coverage, closure_func},
1411      {this, this, this}
1412    };
1413    chain_context_closure_lookup (c,
1414				  backtrack.len, (const USHORT *) backtrack.array,
1415				  input.len, (const USHORT *) input.array + 1,
1416				  lookahead.len, (const USHORT *) lookahead.array,
1417				  lookup.len, lookup.array,
1418				  lookup_context);
1419  }
1420
1421  inline const Coverage &get_coverage (void) const
1422  {
1423    const OffsetArrayOf<Coverage> &input = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
1424    return this+input[0];
1425  }
1426
1427  inline bool would_apply (hb_would_apply_context_t *c) const
1428  {
1429    TRACE_WOULD_APPLY ();
1430
1431    const OffsetArrayOf<Coverage> &input = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
1432    const OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (input);
1433    const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
1434    struct ChainContextApplyLookupContext lookup_context = {
1435      {match_coverage, NULL},
1436      {this, this, this}
1437    };
1438    return TRACE_RETURN (chain_context_would_apply_lookup (c,
1439							   backtrack.len, (const USHORT *) backtrack.array,
1440							   input.len, (const USHORT *) input.array + 1,
1441							   lookahead.len, (const USHORT *) lookahead.array,
1442							   lookup.len, lookup.array, lookup_context));
1443  }
1444
1445  inline bool apply (hb_apply_context_t *c, apply_lookup_func_t apply_func) const
1446  {
1447    TRACE_APPLY ();
1448    const OffsetArrayOf<Coverage> &input = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
1449
1450    unsigned int index = (this+input[0]) (c->buffer->cur().codepoint);
1451    if (likely (index == NOT_COVERED)) return TRACE_RETURN (false);
1452
1453    const OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (input);
1454    const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
1455    struct ChainContextApplyLookupContext lookup_context = {
1456      {match_coverage, apply_func},
1457      {this, this, this}
1458    };
1459    return TRACE_RETURN (chain_context_apply_lookup (c,
1460						     backtrack.len, (const USHORT *) backtrack.array,
1461						     input.len, (const USHORT *) input.array + 1,
1462						     lookahead.len, (const USHORT *) lookahead.array,
1463						     lookup.len, lookup.array, lookup_context));
1464  }
1465
1466  inline bool sanitize (hb_sanitize_context_t *c) {
1467    TRACE_SANITIZE ();
1468    if (!backtrack.sanitize (c, this)) return TRACE_RETURN (false);
1469    OffsetArrayOf<Coverage> &input = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
1470    if (!input.sanitize (c, this)) return TRACE_RETURN (false);
1471    OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (input);
1472    if (!lookahead.sanitize (c, this)) return TRACE_RETURN (false);
1473    ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
1474    return TRACE_RETURN (lookup.sanitize (c));
1475  }
1476
1477  protected:
1478  USHORT	format;			/* Format identifier--format = 3 */
1479  OffsetArrayOf<Coverage>
1480		backtrack;		/* Array of coverage tables
1481					 * in backtracking sequence, in  glyph
1482					 * sequence order */
1483  OffsetArrayOf<Coverage>
1484		inputX		;	/* Array of coverage
1485					 * tables in input sequence, in glyph
1486					 * sequence order */
1487  OffsetArrayOf<Coverage>
1488		lookaheadX;		/* Array of coverage tables
1489					 * in lookahead sequence, in glyph
1490					 * sequence order */
1491  ArrayOf<LookupRecord>
1492		lookupX;		/* Array of LookupRecords--in
1493					 * design order) */
1494  public:
1495  DEFINE_SIZE_MIN (10);
1496};
1497
1498struct ChainContext
1499{
1500  protected:
1501
1502  inline void closure (hb_closure_context_t *c, closure_lookup_func_t closure_func) const
1503  {
1504    TRACE_CLOSURE ();
1505    switch (u.format) {
1506    case 1: u.format1.closure (c, closure_func); break;
1507    case 2: u.format2.closure (c, closure_func); break;
1508    case 3: u.format3.closure (c, closure_func); break;
1509    default:                                     break;
1510    }
1511  }
1512
1513  inline const Coverage &get_coverage (void) const
1514  {
1515    switch (u.format) {
1516    case 1: return this + u.format1.coverage;
1517    case 2: return this + u.format2.coverage;
1518    case 3: return u.format3.get_coverage ();
1519    default:return Null(Coverage);
1520    }
1521  }
1522
1523  inline bool would_apply (hb_would_apply_context_t *c) const
1524  {
1525    switch (u.format) {
1526    case 1: return u.format1.would_apply (c);
1527    case 2: return u.format2.would_apply (c);
1528    case 3: return u.format3.would_apply (c);
1529    default:return false;
1530    }
1531  }
1532
1533  inline bool apply (hb_apply_context_t *c, apply_lookup_func_t apply_func) const
1534  {
1535    TRACE_APPLY ();
1536    switch (u.format) {
1537    case 1: return TRACE_RETURN (u.format1.apply (c, apply_func));
1538    case 2: return TRACE_RETURN (u.format2.apply (c, apply_func));
1539    case 3: return TRACE_RETURN (u.format3.apply (c, apply_func));
1540    default:return TRACE_RETURN (false);
1541    }
1542  }
1543
1544  inline bool sanitize (hb_sanitize_context_t *c) {
1545    TRACE_SANITIZE ();
1546    if (!u.format.sanitize (c)) return TRACE_RETURN (false);
1547    switch (u.format) {
1548    case 1: return TRACE_RETURN (u.format1.sanitize (c));
1549    case 2: return TRACE_RETURN (u.format2.sanitize (c));
1550    case 3: return TRACE_RETURN (u.format3.sanitize (c));
1551    default:return TRACE_RETURN (true);
1552    }
1553  }
1554
1555  protected:
1556  union {
1557  USHORT		format;	/* Format identifier */
1558  ChainContextFormat1	format1;
1559  ChainContextFormat2	format2;
1560  ChainContextFormat3	format3;
1561  } u;
1562};
1563
1564
1565struct ExtensionFormat1
1566{
1567  friend struct Extension;
1568
1569  protected:
1570  inline unsigned int get_type (void) const { return extensionLookupType; }
1571  inline unsigned int get_offset (void) const { return extensionOffset; }
1572
1573  inline bool sanitize (hb_sanitize_context_t *c) {
1574    TRACE_SANITIZE ();
1575    return TRACE_RETURN (c->check_struct (this));
1576  }
1577
1578  protected:
1579  USHORT	format;			/* Format identifier. Set to 1. */
1580  USHORT	extensionLookupType;	/* Lookup type of subtable referenced
1581					 * by ExtensionOffset (i.e. the
1582					 * extension subtable). */
1583  ULONG		extensionOffset;	/* Offset to the extension subtable,
1584					 * of lookup type subtable. */
1585  public:
1586  DEFINE_SIZE_STATIC (8);
1587};
1588
1589struct Extension
1590{
1591  inline unsigned int get_type (void) const
1592  {
1593    switch (u.format) {
1594    case 1: return u.format1.get_type ();
1595    default:return 0;
1596    }
1597  }
1598  inline unsigned int get_offset (void) const
1599  {
1600    switch (u.format) {
1601    case 1: return u.format1.get_offset ();
1602    default:return 0;
1603    }
1604  }
1605
1606  inline bool sanitize (hb_sanitize_context_t *c) {
1607    TRACE_SANITIZE ();
1608    if (!u.format.sanitize (c)) return TRACE_RETURN (false);
1609    switch (u.format) {
1610    case 1: return TRACE_RETURN (u.format1.sanitize (c));
1611    default:return TRACE_RETURN (true);
1612    }
1613  }
1614
1615  protected:
1616  union {
1617  USHORT		format;		/* Format identifier */
1618  ExtensionFormat1	format1;
1619  } u;
1620};
1621
1622
1623/*
1624 * GSUB/GPOS Common
1625 */
1626
1627struct GSUBGPOS
1628{
1629  static const hb_tag_t GSUBTag	= HB_OT_TAG_GSUB;
1630  static const hb_tag_t GPOSTag	= HB_OT_TAG_GPOS;
1631
1632  inline unsigned int get_script_count (void) const
1633  { return (this+scriptList).len; }
1634  inline const Tag& get_script_tag (unsigned int i) const
1635  { return (this+scriptList).get_tag (i); }
1636  inline unsigned int get_script_tags (unsigned int start_offset,
1637				       unsigned int *script_count /* IN/OUT */,
1638				       hb_tag_t     *script_tags /* OUT */) const
1639  { return (this+scriptList).get_tags (start_offset, script_count, script_tags); }
1640  inline const Script& get_script (unsigned int i) const
1641  { return (this+scriptList)[i]; }
1642  inline bool find_script_index (hb_tag_t tag, unsigned int *index) const
1643  { return (this+scriptList).find_index (tag, index); }
1644
1645  inline unsigned int get_feature_count (void) const
1646  { return (this+featureList).len; }
1647  inline const Tag& get_feature_tag (unsigned int i) const
1648  { return (this+featureList).get_tag (i); }
1649  inline unsigned int get_feature_tags (unsigned int start_offset,
1650					unsigned int *feature_count /* IN/OUT */,
1651					hb_tag_t     *feature_tags /* OUT */) const
1652  { return (this+featureList).get_tags (start_offset, feature_count, feature_tags); }
1653  inline const Feature& get_feature (unsigned int i) const
1654  { return (this+featureList)[i]; }
1655  inline bool find_feature_index (hb_tag_t tag, unsigned int *index) const
1656  { return (this+featureList).find_index (tag, index); }
1657
1658  inline unsigned int get_lookup_count (void) const
1659  { return (this+lookupList).len; }
1660  inline const Lookup& get_lookup (unsigned int i) const
1661  { return (this+lookupList)[i]; }
1662
1663  inline bool sanitize (hb_sanitize_context_t *c) {
1664    TRACE_SANITIZE ();
1665    return TRACE_RETURN (version.sanitize (c) && likely (version.major == 1) &&
1666			 scriptList.sanitize (c, this) &&
1667			 featureList.sanitize (c, this) &&
1668			 lookupList.sanitize (c, this));
1669  }
1670
1671  protected:
1672  FixedVersion	version;	/* Version of the GSUB/GPOS table--initially set
1673				 * to 0x00010000 */
1674  OffsetTo<ScriptList>
1675		scriptList;  	/* ScriptList table */
1676  OffsetTo<FeatureList>
1677		featureList; 	/* FeatureList table */
1678  OffsetTo<LookupList>
1679		lookupList; 	/* LookupList table */
1680  public:
1681  DEFINE_SIZE_STATIC (10);
1682};
1683
1684
1685
1686#endif /* HB_OT_LAYOUT_GSUBGPOS_PRIVATE_HH */
1687