Minor
[framework/uifw/harfbuzz.git] / src / hb-ot-layout-gsubgpos-private.hh
1 /*
2  * Copyright (C) 2007,2008,2009  Red Hat, Inc.
3  *
4  *  This is part of HarfBuzz, a text shaping library.
5  *
6  * Permission is hereby granted, without written agreement and without
7  * license or royalty fees, to use, copy, modify, and distribute this
8  * software and its documentation for any purpose, provided that the
9  * above copyright notice and the following two paragraphs appear in
10  * all copies of this software.
11  *
12  * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14  * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15  * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
16  * DAMAGE.
17  *
18  * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20  * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
21  * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
23  *
24  * Red Hat Author(s): Behdad Esfahbod
25  */
26
27 #ifndef HB_OT_LAYOUT_GSUBGPOS_PRIVATE_HH
28 #define HB_OT_LAYOUT_GSUBGPOS_PRIVATE_HH
29
30 #include "hb-buffer-private.h"
31 #include "hb-ot-layout-gdef-private.hh"
32
33
34 #ifndef HB_DEBUG_APPLY
35 #define HB_DEBUG_APPLY HB_DEBUG+0
36 #endif
37
38 #define TRACE_APPLY() \
39         HB_STMT_START { \
40           if (HB_DEBUG_APPLY) \
41                   _hb_trace ("APPLY", HB_FUNC, this, apply_depth, HB_DEBUG_APPLY); \
42         } HB_STMT_END
43
44
45 #define APPLY_ARG_DEF \
46         hb_apply_context_t *context, \
47         hb_ot_layout_context_t *layout_context, \
48         hb_buffer_t *buffer, \
49         unsigned int context_length HB_UNUSED, \
50         unsigned int apply_depth HB_UNUSED
51 #define APPLY_ARG \
52         context, \
53         layout_context, \
54         buffer, \
55         context_length, \
56         (HB_DEBUG_APPLY ? apply_depth + 1 : 0)
57
58 struct hb_apply_context_t
59 {
60   unsigned int nesting_level_left;
61   unsigned int lookup_flag;
62   unsigned int property; /* propety of first glyph (TODO remove) */
63 };
64
65
66 typedef bool (*match_func_t) (hb_codepoint_t glyph_id, const USHORT &value, const char *data);
67 typedef bool (*apply_lookup_func_t) (APPLY_ARG_DEF, unsigned int lookup_index);
68
69 struct ContextFuncs
70 {
71   match_func_t match;
72   apply_lookup_func_t apply;
73 };
74
75
76 static inline bool match_glyph (hb_codepoint_t glyph_id, const USHORT &value, const char *data HB_UNUSED)
77 {
78   return glyph_id == value;
79 }
80
81 static inline bool match_class (hb_codepoint_t glyph_id, const USHORT &value, const char *data)
82 {
83   const ClassDef &class_def = *reinterpret_cast<const ClassDef *>(data);
84   return class_def.get_class (glyph_id) == value;
85 }
86
87 static inline bool match_coverage (hb_codepoint_t glyph_id, const USHORT &value, const char *data)
88 {
89   const OffsetTo<Coverage> &coverage = (const OffsetTo<Coverage>&)value;
90   return (data+coverage) (glyph_id) != NOT_COVERED;
91 }
92
93
94 static inline bool match_input (APPLY_ARG_DEF,
95                                 unsigned int count, /* Including the first glyph (not matched) */
96                                 const USHORT input[], /* Array of input values--start with second glyph */
97                                 match_func_t match_func,
98                                 const char *match_data,
99                                 unsigned int *context_length_out)
100 {
101   unsigned int i, j;
102   unsigned int end = MIN (buffer->in_length, buffer->in_pos + context_length);
103   if (unlikely (buffer->in_pos + count > end))
104     return false;
105
106   for (i = 1, j = buffer->in_pos + 1; i < count; i++, j++)
107   {
108     while (_hb_ot_layout_skip_mark (layout_context->face, IN_INFO (j), context->lookup_flag, NULL))
109     {
110       if (unlikely (j + count - i == end))
111         return false;
112       j++;
113     }
114
115     if (likely (!match_func (IN_GLYPH (j), input[i - 1], match_data)))
116       return false;
117   }
118
119   *context_length_out = j - buffer->in_pos;
120
121   return true;
122 }
123
124 static inline bool match_backtrack (APPLY_ARG_DEF,
125                                     unsigned int count,
126                                     const USHORT backtrack[],
127                                     match_func_t match_func,
128                                     const char *match_data)
129 {
130   if (unlikely (buffer->out_pos < count))
131     return false;
132
133   for (unsigned int i = 0, j = buffer->out_pos - 1; i < count; i++, j--)
134   {
135     while (_hb_ot_layout_skip_mark (layout_context->face, OUT_INFO (j), context->lookup_flag, NULL))
136     {
137       if (unlikely (j + 1 == count - i))
138         return false;
139       j--;
140     }
141
142     if (likely (!match_func (OUT_GLYPH (j), backtrack[i], match_data)))
143       return false;
144   }
145
146   return true;
147 }
148
149 static inline bool match_lookahead (APPLY_ARG_DEF,
150                                     unsigned int count,
151                                     const USHORT lookahead[],
152                                     match_func_t match_func,
153                                     const char *match_data,
154                                     unsigned int offset)
155 {
156   unsigned int i, j;
157   unsigned int end = MIN (buffer->in_length, buffer->in_pos + context_length);
158   if (unlikely (buffer->in_pos + offset + count > end))
159     return false;
160
161   for (i = 0, j = buffer->in_pos + offset; i < count; i++, j++)
162   {
163     while (_hb_ot_layout_skip_mark (layout_context->face, OUT_INFO (j), context->lookup_flag, NULL))
164     {
165       if (unlikely (j + count - i == end))
166         return false;
167       j++;
168     }
169
170     if (likely (!match_func (IN_GLYPH (j), lookahead[i], match_data)))
171       return false;
172   }
173
174   return true;
175 }
176
177
178 struct LookupRecord
179 {
180   static inline unsigned int get_size () { return sizeof (LookupRecord); }
181
182   inline bool sanitize (hb_sanitize_context_t *context) {
183     TRACE_SANITIZE ();
184     return SANITIZE_SELF ();
185   }
186
187   USHORT        sequenceIndex;          /* Index into current glyph
188                                          * sequence--first glyph = 0 */
189   USHORT        lookupListIndex;        /* Lookup to apply to that
190                                          * position--zero--based */
191 };
192 ASSERT_SIZE (LookupRecord, 4);
193
194 static inline bool apply_lookup (APPLY_ARG_DEF,
195                                  unsigned int count, /* Including the first glyph */
196                                  unsigned int lookupCount,
197                                  const LookupRecord lookupRecord[], /* Array of LookupRecords--in design order */
198                                  apply_lookup_func_t apply_func)
199 {
200   unsigned int end = MIN (buffer->in_length, buffer->in_pos + context_length);
201   if (unlikely (buffer->in_pos + count > end))
202     return false;
203
204   /* TODO We don't support lookupRecord arrays that are not increasing:
205    *      Should be easy for in_place ones at least. */
206
207   /* Note: If sublookup is reverse, i will underflow after the first loop
208    * and we jump out of it.  Not entirely disastrous.  So we don't check
209    * for reverse lookup here.
210    */
211   for (unsigned int i = 0; i < count; /* NOP */)
212   {
213     while (_hb_ot_layout_skip_mark (layout_context->face, IN_CURINFO (), context->lookup_flag, NULL))
214     {
215       if (unlikely (buffer->in_pos == end))
216         return true;
217       /* No lookup applied for this index */
218       _hb_buffer_next_glyph (buffer);
219     }
220
221     if (lookupCount && i == lookupRecord->sequenceIndex)
222     {
223       unsigned int old_pos = buffer->in_pos;
224
225       /* Apply a lookup */
226       bool done = apply_func (APPLY_ARG, lookupRecord->lookupListIndex);
227
228       lookupRecord++;
229       lookupCount--;
230       /* Err, this is wrong if the lookup jumped over some glyphs */
231       i += buffer->in_pos - old_pos;
232       if (unlikely (buffer->in_pos == end))
233         return true;
234
235       if (!done)
236         goto not_applied;
237     }
238     else
239     {
240     not_applied:
241       /* No lookup applied for this index */
242       _hb_buffer_next_glyph (buffer);
243       i++;
244     }
245   }
246
247   return true;
248 }
249
250
251 /* Contextual lookups */
252
253 struct ContextLookupContext
254 {
255   ContextFuncs funcs;
256   const char *match_data;
257 };
258
259 static inline bool context_lookup (APPLY_ARG_DEF,
260                                    unsigned int inputCount, /* Including the first glyph (not matched) */
261                                    const USHORT input[], /* Array of input values--start with second glyph */
262                                    unsigned int lookupCount,
263                                    const LookupRecord lookupRecord[],
264                                    ContextLookupContext &lookup_context)
265 {
266   return match_input (APPLY_ARG,
267                       inputCount, input,
268                       lookup_context.funcs.match, lookup_context.match_data,
269                       &context_length) &&
270          apply_lookup (APPLY_ARG,
271                        inputCount,
272                        lookupCount, lookupRecord,
273                        lookup_context.funcs.apply);
274 }
275
276 struct Rule
277 {
278   friend struct RuleSet;
279
280   private:
281   inline bool apply (APPLY_ARG_DEF, ContextLookupContext &lookup_context) const
282   {
283     TRACE_APPLY ();
284     const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (input, input[0].get_size () * (inputCount ? inputCount - 1 : 0));
285     return context_lookup (APPLY_ARG,
286                            inputCount, input,
287                            lookupCount, lookupRecord,
288                            lookup_context);
289   }
290
291   public:
292   inline bool sanitize (hb_sanitize_context_t *context) {
293     TRACE_SANITIZE ();
294     if (!(SANITIZE (inputCount) && SANITIZE (lookupCount))) return false;
295     return SANITIZE_MEM (input,
296                          input[0].get_size () * inputCount +
297                          lookupRecordX[0].get_size () * lookupCount);
298   }
299
300   private:
301   USHORT        inputCount;             /* Total number of glyphs in input
302                                          * glyph sequence--includes the  first
303                                          * glyph */
304   USHORT        lookupCount;            /* Number of LookupRecords */
305   USHORT        input[VAR];             /* Array of match inputs--start with
306                                          * second glyph */
307   LookupRecord  lookupRecordX[VAR];     /* Array of LookupRecords--in
308                                          * design order */
309 };
310 ASSERT_SIZE_VAR2 (Rule, 4, USHORT, LookupRecord);
311
312 struct RuleSet
313 {
314   inline bool apply (APPLY_ARG_DEF, ContextLookupContext &lookup_context) const
315   {
316     TRACE_APPLY ();
317     unsigned int num_rules = rule.len;
318     for (unsigned int i = 0; i < num_rules; i++)
319     {
320       if ((this+rule[i]).apply (APPLY_ARG, lookup_context))
321         return true;
322     }
323
324     return false;
325   }
326
327   inline bool sanitize (hb_sanitize_context_t *context) {
328     TRACE_SANITIZE ();
329     return SANITIZE_WITH_BASE (this, rule);
330   }
331
332   private:
333   OffsetArrayOf<Rule>
334                 rule;                   /* Array of Rule tables
335                                          * ordered by preference */
336 };
337
338
339 struct ContextFormat1
340 {
341   friend struct Context;
342
343   private:
344   inline bool apply (APPLY_ARG_DEF, apply_lookup_func_t apply_func) const
345   {
346     TRACE_APPLY ();
347     unsigned int index = (this+coverage) (IN_CURGLYPH ());
348     if (likely (index == NOT_COVERED))
349       return false;
350
351     const RuleSet &rule_set = this+ruleSet[index];
352     struct ContextLookupContext lookup_context = {
353       {match_glyph, apply_func},
354       NULL
355     };
356     return rule_set.apply (APPLY_ARG, lookup_context);
357   }
358
359   inline bool sanitize (hb_sanitize_context_t *context) {
360     TRACE_SANITIZE ();
361     return SANITIZE_WITH_BASE (this, coverage)
362         && SANITIZE_WITH_BASE (this, ruleSet);
363   }
364
365   private:
366   USHORT        format;                 /* Format identifier--format = 1 */
367   OffsetTo<Coverage>
368                 coverage;               /* Offset to Coverage table--from
369                                          * beginning of table */
370   OffsetArrayOf<RuleSet>
371                 ruleSet;                /* Array of RuleSet tables
372                                          * ordered by Coverage Index */
373 };
374 ASSERT_SIZE (ContextFormat1, 6);
375
376
377 struct ContextFormat2
378 {
379   friend struct Context;
380
381   private:
382   inline bool apply (APPLY_ARG_DEF, apply_lookup_func_t apply_func) const
383   {
384     TRACE_APPLY ();
385     unsigned int index = (this+coverage) (IN_CURGLYPH ());
386     if (likely (index == NOT_COVERED))
387       return false;
388
389     const ClassDef &class_def = this+classDef;
390     index = class_def (IN_CURGLYPH ());
391     const RuleSet &rule_set = this+ruleSet[index];
392     /* LONGTERMTODO: Old code fetches glyph classes at most once and caches
393      * them across subrule lookups.  Not sure it's worth it.
394      */
395     struct ContextLookupContext lookup_context = {
396      {match_class, apply_func},
397       CharP(&class_def)
398     };
399     return rule_set.apply (APPLY_ARG, lookup_context);
400   }
401
402   inline bool sanitize (hb_sanitize_context_t *context) {
403     TRACE_SANITIZE ();
404     return SANITIZE_WITH_BASE (this, coverage)
405         && SANITIZE_WITH_BASE (this, classDef)
406         && SANITIZE_WITH_BASE (this, ruleSet);
407   }
408
409   private:
410   USHORT        format;                 /* Format identifier--format = 2 */
411   OffsetTo<Coverage>
412                 coverage;               /* Offset to Coverage table--from
413                                          * beginning of table */
414   OffsetTo<ClassDef>
415                 classDef;               /* Offset to glyph ClassDef table--from
416                                          * beginning of table */
417   OffsetArrayOf<RuleSet>
418                 ruleSet;                /* Array of RuleSet tables
419                                          * ordered by class */
420 };
421 ASSERT_SIZE (ContextFormat2, 8);
422
423
424 struct ContextFormat3
425 {
426   friend struct Context;
427
428   private:
429   inline bool apply (APPLY_ARG_DEF, apply_lookup_func_t apply_func) const
430   {
431     TRACE_APPLY ();
432     unsigned int index = (this+coverage[0]) (IN_CURGLYPH ());
433     if (likely (index == NOT_COVERED))
434       return false;
435
436     const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (coverage, coverage[0].get_size () * glyphCount);
437     struct ContextLookupContext lookup_context = {
438       {match_coverage, apply_func},
439        CharP(this)
440     };
441     return context_lookup (APPLY_ARG,
442                            glyphCount, (const USHORT *) (coverage + 1),
443                            lookupCount, lookupRecord,
444                            lookup_context);
445   }
446
447   inline bool sanitize (hb_sanitize_context_t *context) {
448     TRACE_SANITIZE ();
449     if (!SANITIZE_SELF ()) return false;
450     unsigned int count = glyphCount;
451     if (!SANITIZE_ARRAY (coverage, OffsetTo<Coverage>::get_size (), glyphCount)) return false;
452     for (unsigned int i = 0; i < count; i++)
453       if (!SANITIZE_WITH_BASE (this, coverage[i])) return false;
454     LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (coverage, OffsetTo<Coverage>::get_size () * glyphCount);
455     return SANITIZE_ARRAY (lookupRecord, LookupRecord::get_size (), lookupCount);
456   }
457
458   private:
459   USHORT        format;                 /* Format identifier--format = 3 */
460   USHORT        glyphCount;             /* Number of glyphs in the input glyph
461                                          * sequence */
462   USHORT        lookupCount;            /* Number of LookupRecords */
463   OffsetTo<Coverage>
464                 coverage[VAR];          /* Array of offsets to Coverage
465                                          * table in glyph sequence order */
466   LookupRecord  lookupRecordX[VAR];     /* Array of LookupRecords--in
467                                          * design order */
468 };
469 ASSERT_SIZE_VAR2 (ContextFormat3, 6, OffsetTo<Coverage>, LookupRecord);
470
471 struct Context
472 {
473   protected:
474   inline bool apply (APPLY_ARG_DEF, apply_lookup_func_t apply_func) const
475   {
476     TRACE_APPLY ();
477     switch (u.format) {
478     case 1: return u.format1->apply (APPLY_ARG, apply_func);
479     case 2: return u.format2->apply (APPLY_ARG, apply_func);
480     case 3: return u.format3->apply (APPLY_ARG, apply_func);
481     default:return false;
482     }
483   }
484
485   inline bool sanitize (hb_sanitize_context_t *context) {
486     TRACE_SANITIZE ();
487     if (!SANITIZE (u.format)) return false;
488     switch (u.format) {
489     case 1: return u.format1->sanitize (context);
490     case 2: return u.format2->sanitize (context);
491     case 3: return u.format3->sanitize (context);
492     default:return true;
493     }
494   }
495
496   private:
497   union {
498   USHORT                format;         /* Format identifier */
499   ContextFormat1        format1[VAR];
500   ContextFormat2        format2[VAR];
501   ContextFormat3        format3[VAR];
502   } u;
503 };
504
505
506 /* Chaining Contextual lookups */
507
508 struct ChainContextLookupContext
509 {
510   ContextFuncs funcs;
511   const char *match_data[3];
512 };
513
514 static inline bool chain_context_lookup (APPLY_ARG_DEF,
515                                          unsigned int backtrackCount,
516                                          const USHORT backtrack[],
517                                          unsigned int inputCount, /* Including the first glyph (not matched) */
518                                          const USHORT input[], /* Array of input values--start with second glyph */
519                                          unsigned int lookaheadCount,
520                                          const USHORT lookahead[],
521                                          unsigned int lookupCount,
522                                          const LookupRecord lookupRecord[],
523                                          ChainContextLookupContext &lookup_context)
524 {
525   /* First guess */
526   if (unlikely (buffer->out_pos < backtrackCount ||
527                    buffer->in_pos + inputCount + lookaheadCount > buffer->in_length ||
528                    inputCount + lookaheadCount > context_length))
529     return false;
530
531   unsigned int offset;
532   return match_backtrack (APPLY_ARG,
533                           backtrackCount, backtrack,
534                           lookup_context.funcs.match, lookup_context.match_data[0]) &&
535          match_input (APPLY_ARG,
536                       inputCount, input,
537                       lookup_context.funcs.match, lookup_context.match_data[1],
538                       &offset) &&
539          match_lookahead (APPLY_ARG,
540                           lookaheadCount, lookahead,
541                           lookup_context.funcs.match, lookup_context.match_data[2],
542                           offset) &&
543          (context_length = offset, true) &&
544          apply_lookup (APPLY_ARG,
545                        inputCount,
546                        lookupCount, lookupRecord,
547                        lookup_context.funcs.apply);
548 }
549
550 struct ChainRule
551 {
552   friend struct ChainRuleSet;
553
554   private:
555   inline bool apply (APPLY_ARG_DEF, ChainContextLookupContext &lookup_context) const
556   {
557     TRACE_APPLY ();
558     const HeadlessArrayOf<USHORT> &input = StructAfter<HeadlessArrayOf<USHORT> > (backtrack);
559     const ArrayOf<USHORT> &lookahead = StructAfter<ArrayOf<USHORT> > (input);
560     const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
561     return chain_context_lookup (APPLY_ARG,
562                                  backtrack.len, backtrack.array(),
563                                  input.len, input.array(),
564                                  lookahead.len, lookahead.array(),
565                                  lookup.len, lookup.array(),
566                                  lookup_context);
567     return false;
568   }
569
570   public:
571   inline bool sanitize (hb_sanitize_context_t *context) {
572     TRACE_SANITIZE ();
573     if (!SANITIZE (backtrack)) return false;
574     HeadlessArrayOf<USHORT> &input = StructAfter<HeadlessArrayOf<USHORT> > (backtrack);
575     if (!SANITIZE (input)) return false;
576     ArrayOf<USHORT> &lookahead = StructAfter<ArrayOf<USHORT> > (input);
577     if (!SANITIZE (lookahead)) return false;
578     ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
579     return SANITIZE (lookup);
580   }
581
582   private:
583   ArrayOf<USHORT>
584                 backtrack;              /* Array of backtracking values
585                                          * (to be matched before the input
586                                          * sequence) */
587   HeadlessArrayOf<USHORT>
588                 inputX;                 /* Array of input values (start with
589                                          * second glyph) */
590   ArrayOf<USHORT>
591                 lookaheadX;             /* Array of lookahead values's (to be
592                                          * matched after the input sequence) */
593   ArrayOf<LookupRecord>
594                 lookupX;                /* Array of LookupRecords--in
595                                          * design order) */
596 };
597 ASSERT_SIZE (ChainRule, 8);
598
599 struct ChainRuleSet
600 {
601   inline bool apply (APPLY_ARG_DEF, ChainContextLookupContext &lookup_context) const
602   {
603     TRACE_APPLY ();
604     unsigned int num_rules = rule.len;
605     for (unsigned int i = 0; i < num_rules; i++)
606     {
607       if ((this+rule[i]).apply (APPLY_ARG, lookup_context))
608         return true;
609     }
610
611     return false;
612   }
613
614   inline bool sanitize (hb_sanitize_context_t *context) {
615     TRACE_SANITIZE ();
616     return SANITIZE_WITH_BASE (this, rule);
617   }
618
619   private:
620   OffsetArrayOf<ChainRule>
621                 rule;                   /* Array of ChainRule tables
622                                          * ordered by preference */
623 };
624 ASSERT_SIZE (ChainRuleSet, 2);
625
626 struct ChainContextFormat1
627 {
628   friend struct ChainContext;
629
630   private:
631   inline bool apply (APPLY_ARG_DEF, apply_lookup_func_t apply_func) const
632   {
633     TRACE_APPLY ();
634     unsigned int index = (this+coverage) (IN_CURGLYPH ());
635     if (likely (index == NOT_COVERED))
636       return false;
637
638     const ChainRuleSet &rule_set = this+ruleSet[index];
639     struct ChainContextLookupContext lookup_context = {
640       {match_glyph, apply_func},
641       {NULL, NULL, NULL}
642     };
643     return rule_set.apply (APPLY_ARG, lookup_context);
644   }
645
646   inline bool sanitize (hb_sanitize_context_t *context) {
647     TRACE_SANITIZE ();
648     return SANITIZE_WITH_BASE (this, coverage)
649         && SANITIZE_WITH_BASE (this, ruleSet);
650   }
651
652   private:
653   USHORT        format;                 /* Format identifier--format = 1 */
654   OffsetTo<Coverage>
655                 coverage;               /* Offset to Coverage table--from
656                                          * beginning of table */
657   OffsetArrayOf<ChainRuleSet>
658                 ruleSet;                /* Array of ChainRuleSet tables
659                                          * ordered by Coverage Index */
660 };
661 ASSERT_SIZE (ChainContextFormat1, 6);
662
663 struct ChainContextFormat2
664 {
665   friend struct ChainContext;
666
667   private:
668   inline bool apply (APPLY_ARG_DEF, apply_lookup_func_t apply_func) const
669   {
670     TRACE_APPLY ();
671     unsigned int index = (this+coverage) (IN_CURGLYPH ());
672     if (likely (index == NOT_COVERED))
673       return false;
674
675     const ClassDef &backtrack_class_def = this+backtrackClassDef;
676     const ClassDef &input_class_def = this+inputClassDef;
677     const ClassDef &lookahead_class_def = this+lookaheadClassDef;
678
679     index = input_class_def (IN_CURGLYPH ());
680     const ChainRuleSet &rule_set = this+ruleSet[index];
681     /* LONGTERMTODO: Old code fetches glyph classes at most once and caches
682      * them across subrule lookups.  Not sure it's worth it.
683      */
684     struct ChainContextLookupContext lookup_context = {
685      {match_class, apply_func},
686      {CharP(&backtrack_class_def),
687       CharP(&input_class_def),
688       CharP(&lookahead_class_def)}
689     };
690     return rule_set.apply (APPLY_ARG, lookup_context);
691   }
692
693   inline bool sanitize (hb_sanitize_context_t *context) {
694     TRACE_SANITIZE ();
695     return SANITIZE_WITH_BASE (this, coverage)
696         && SANITIZE_WITH_BASE (this, backtrackClassDef)
697         && SANITIZE_WITH_BASE (this, inputClassDef)
698         && SANITIZE_WITH_BASE (this, lookaheadClassDef)
699         && SANITIZE_WITH_BASE (this, ruleSet);
700   }
701
702   private:
703   USHORT        format;                 /* Format identifier--format = 2 */
704   OffsetTo<Coverage>
705                 coverage;               /* Offset to Coverage table--from
706                                          * beginning of table */
707   OffsetTo<ClassDef>
708                 backtrackClassDef;      /* Offset to glyph ClassDef table
709                                          * containing backtrack sequence
710                                          * data--from beginning of table */
711   OffsetTo<ClassDef>
712                 inputClassDef;          /* Offset to glyph ClassDef
713                                          * table containing input sequence
714                                          * data--from beginning of table */
715   OffsetTo<ClassDef>
716                 lookaheadClassDef;      /* Offset to glyph ClassDef table
717                                          * containing lookahead sequence
718                                          * data--from beginning of table */
719   OffsetArrayOf<ChainRuleSet>
720                 ruleSet;                /* Array of ChainRuleSet tables
721                                          * ordered by class */
722 };
723 ASSERT_SIZE (ChainContextFormat2, 12);
724
725 struct ChainContextFormat3
726 {
727   friend struct ChainContext;
728
729   private:
730
731   inline bool apply (APPLY_ARG_DEF, apply_lookup_func_t apply_func) const
732   {
733     TRACE_APPLY ();
734     const OffsetArrayOf<Coverage> &input = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
735
736     unsigned int index = (this+input[0]) (IN_CURGLYPH ());
737     if (likely (index == NOT_COVERED))
738       return false;
739
740     const OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (input);
741     const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
742     struct ChainContextLookupContext lookup_context = {
743       {match_coverage, apply_func},
744       {CharP(this), CharP(this), CharP(this)}
745     };
746     return chain_context_lookup (APPLY_ARG,
747                                  backtrack.len, (const USHORT *) backtrack.array(),
748                                  input.len, (const USHORT *) input.array() + 1,
749                                  lookahead.len, (const USHORT *) lookahead.array(),
750                                  lookup.len, lookup.array(),
751                                  lookup_context);
752     return false;
753   }
754
755   inline bool sanitize (hb_sanitize_context_t *context) {
756     TRACE_SANITIZE ();
757     if (!SANITIZE_WITH_BASE (this, backtrack)) return false;
758     OffsetArrayOf<Coverage> &input = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
759     if (!SANITIZE_WITH_BASE (this, input)) return false;
760     OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (input);
761     if (!SANITIZE_WITH_BASE (this, lookahead)) return false;
762     ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
763     return SANITIZE (lookup);
764   }
765
766   private:
767   USHORT        format;                 /* Format identifier--format = 3 */
768   OffsetArrayOf<Coverage>
769                 backtrack;              /* Array of coverage tables
770                                          * in backtracking sequence, in  glyph
771                                          * sequence order */
772   OffsetArrayOf<Coverage>
773                 inputX          ;       /* Array of coverage
774                                          * tables in input sequence, in glyph
775                                          * sequence order */
776   OffsetArrayOf<Coverage>
777                 lookaheadX;             /* Array of coverage tables
778                                          * in lookahead sequence, in glyph
779                                          * sequence order */
780   ArrayOf<LookupRecord>
781                 lookupX;                /* Array of LookupRecords--in
782                                          * design order) */
783 };
784 ASSERT_SIZE (ChainContextFormat3, 10);
785
786 struct ChainContext
787 {
788   protected:
789   inline bool apply (APPLY_ARG_DEF, apply_lookup_func_t apply_func) const
790   {
791     TRACE_APPLY ();
792     switch (u.format) {
793     case 1: return u.format1->apply (APPLY_ARG, apply_func);
794     case 2: return u.format2->apply (APPLY_ARG, apply_func);
795     case 3: return u.format3->apply (APPLY_ARG, apply_func);
796     default:return false;
797     }
798   }
799
800   inline bool sanitize (hb_sanitize_context_t *context) {
801     TRACE_SANITIZE ();
802     if (!SANITIZE (u.format)) return false;
803     switch (u.format) {
804     case 1: return u.format1->sanitize (context);
805     case 2: return u.format2->sanitize (context);
806     case 3: return u.format3->sanitize (context);
807     default:return true;
808     }
809   }
810
811   private:
812   union {
813   USHORT                format; /* Format identifier */
814   ChainContextFormat1   format1[VAR];
815   ChainContextFormat2   format2[VAR];
816   ChainContextFormat3   format3[VAR];
817   } u;
818 };
819
820
821 struct ExtensionFormat1
822 {
823   friend struct Extension;
824
825   protected:
826   inline unsigned int get_type (void) const { return extensionLookupType; }
827   inline unsigned int get_offset (void) const { return extensionOffset; }
828
829   inline bool sanitize (hb_sanitize_context_t *context) {
830     TRACE_SANITIZE ();
831     return SANITIZE_SELF ();
832   }
833
834   private:
835   USHORT        format;                 /* Format identifier. Set to 1. */
836   USHORT        extensionLookupType;    /* Lookup type of subtable referenced
837                                          * by ExtensionOffset (i.e. the
838                                          * extension subtable). */
839   ULONG         extensionOffset;        /* Offset to the extension subtable,
840                                          * of lookup type subtable. */
841 };
842 ASSERT_SIZE (ExtensionFormat1, 8);
843
844 struct Extension
845 {
846   inline unsigned int get_type (void) const
847   {
848     switch (u.format) {
849     case 1: return u.format1->get_type ();
850     default:return 0;
851     }
852   }
853   inline unsigned int get_offset (void) const
854   {
855     switch (u.format) {
856     case 1: return u.format1->get_offset ();
857     default:return 0;
858     }
859   }
860
861   inline bool sanitize (hb_sanitize_context_t *context) {
862     TRACE_SANITIZE ();
863     if (!SANITIZE (u.format)) return false;
864     switch (u.format) {
865     case 1: return u.format1->sanitize (context);
866     default:return true;
867     }
868   }
869
870   private:
871   union {
872   USHORT                format;         /* Format identifier */
873   ExtensionFormat1      format1[VAR];
874   } u;
875 };
876
877
878 /*
879  * GSUB/GPOS Common
880  */
881
882 struct GSUBGPOS
883 {
884   static const hb_tag_t GSUBTag = HB_OT_TAG_GSUB;
885   static const hb_tag_t GPOSTag = HB_OT_TAG_GPOS;
886
887   inline unsigned int get_script_count (void) const
888   { return (this+scriptList).len; }
889   inline const Tag& get_script_tag (unsigned int i) const
890   { return (this+scriptList).get_tag (i); }
891   inline unsigned int get_script_tags (unsigned int start_offset,
892                                        unsigned int *script_count /* IN/OUT */,
893                                        hb_tag_t     *script_tags /* OUT */) const
894   { return (this+scriptList).get_tags (start_offset, script_count, script_tags); }
895   inline const Script& get_script (unsigned int i) const
896   { return (this+scriptList)[i]; }
897   inline bool find_script_index (hb_tag_t tag, unsigned int *index) const
898   { return (this+scriptList).find_index (tag, index); }
899
900   inline unsigned int get_feature_count (void) const
901   { return (this+featureList).len; }
902   inline const Tag& get_feature_tag (unsigned int i) const
903   { return (this+featureList).get_tag (i); }
904   inline unsigned int get_feature_tags (unsigned int start_offset,
905                                         unsigned int *feature_count /* IN/OUT */,
906                                         hb_tag_t     *feature_tags /* OUT */) const
907   { return (this+featureList).get_tags (start_offset, feature_count, feature_tags); }
908   inline const Feature& get_feature (unsigned int i) const
909   { return (this+featureList)[i]; }
910   inline bool find_feature_index (hb_tag_t tag, unsigned int *index) const
911   { return (this+featureList).find_index (tag, index); }
912
913   inline unsigned int get_lookup_count (void) const
914   { return (this+lookupList).len; }
915   inline const Lookup& get_lookup (unsigned int i) const
916   { return (this+lookupList)[i]; }
917
918   inline bool sanitize (hb_sanitize_context_t *context) {
919     TRACE_SANITIZE ();
920     return SANITIZE (version) && likely (version.major == 1)
921         && SANITIZE_WITH_BASE (this, scriptList)
922         && SANITIZE_WITH_BASE (this, featureList)
923         && SANITIZE_WITH_BASE (this, lookupList);
924   }
925
926   protected:
927   FixedVersion  version;        /* Version of the GSUB/GPOS table--initially set
928                                  * to 0x00010000 */
929   OffsetTo<ScriptList>
930                 scriptList;     /* ScriptList table */
931   OffsetTo<FeatureList>
932                 featureList;    /* FeatureList table */
933   OffsetTo<LookupList>
934                 lookupList;     /* LookupList table */
935 };
936 ASSERT_SIZE (GSUBGPOS, 10);
937
938
939 #endif /* HB_OT_LAYOUT_GSUBGPOS_PRIVATE_HH */