hb-ot-layout-gsubgpos-private.hh 49.0 KB
Newer Older
1
/*
B
Behdad Esfahbod 已提交
2
 * Copyright © 2007,2008,2009,2010  Red Hat, Inc.
B
Behdad Esfahbod 已提交
3
 * Copyright © 2010,2012  Google, Inc.
4
 *
B
Behdad Esfahbod 已提交
5
 *  This is part of HarfBuzz, a text shaping library.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 * Permission is hereby granted, without written agreement and without
 * license or royalty fees, to use, copy, modify, and distribute this
 * software and its documentation for any purpose, provided that the
 * above copyright notice and the following two paragraphs appear in
 * all copies of this software.
 *
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 *
 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 *
 * Red Hat Author(s): Behdad Esfahbod
26
 * Google Author(s): Behdad Esfahbod
27 28
 */

29 30
#ifndef HB_OT_LAYOUT_GSUBGPOS_PRIVATE_HH
#define HB_OT_LAYOUT_GSUBGPOS_PRIVATE_HH
31

32
#include "hb-buffer-private.hh"
33
#include "hb-ot-layout-gdef-table.hh"
34

35

B
Behdad Esfahbod 已提交
36

37 38 39 40 41
#ifndef HB_DEBUG_CLOSURE
#define HB_DEBUG_CLOSURE (HB_DEBUG+0)
#endif

#define TRACE_CLOSURE() \
42
	hb_auto_trace_t<HB_DEBUG_CLOSURE> trace (&c->debug_depth, "CLOSURE", this, HB_FUNC, "");
43 44


45 46 47 48 49 50 51
/* TODO Add TRACE_RETURN annotation to gsub. */
#ifndef HB_DEBUG_WOULD_APPLY
#define HB_DEBUG_WOULD_APPLY (HB_DEBUG+0)
#endif

#define TRACE_WOULD_APPLY() \
	hb_auto_trace_t<HB_DEBUG_WOULD_APPLY> trace (&c->debug_depth, "WOULD_APPLY", this, HB_FUNC, "first %u second %u", c->first, c->second);
B
Minor  
Behdad Esfahbod 已提交
52

53 54 55 56

struct hb_closure_context_t
{
  hb_face_t *face;
57
  hb_set_t *glyphs;
58 59 60 61 62
  unsigned int nesting_level_left;
  unsigned int debug_depth;


  hb_closure_context_t (hb_face_t *face_,
63
			hb_set_t *glyphs_,
64
		        unsigned int nesting_level_left_ = MAX_NESTING_LEVEL) :
65 66
			  face (face_),
			  glyphs (glyphs_),
67 68 69 70 71 72
			  nesting_level_left (nesting_level_left_),
			  debug_depth (0) {}
};



73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90

struct hb_would_apply_context_t
{
  hb_face_t *face;
  hb_codepoint_t first;
  hb_codepoint_t second;
  unsigned int len;
  unsigned int debug_depth;

  hb_would_apply_context_t (hb_face_t *face_,
			    hb_codepoint_t first_,
			    hb_codepoint_t second_ = -1) :
			      face (face_),
			      first (first_), second (second_), len (second == (hb_codepoint_t) -1 ? 1 : 2),
			      debug_depth (0) {};
};


B
Behdad Esfahbod 已提交
91
#ifndef HB_DEBUG_APPLY
92
#define HB_DEBUG_APPLY (HB_DEBUG+0)
B
Behdad Esfahbod 已提交
93 94
#endif

B
Behdad Esfahbod 已提交
95
#define TRACE_APPLY() \
96
	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);
97

B
Behdad Esfahbod 已提交
98

B
Behdad Esfahbod 已提交
99

100 101
struct hb_apply_context_t
{
102 103
  hb_font_t *font;
  hb_face_t *face;
B
Behdad Esfahbod 已提交
104
  hb_buffer_t *buffer;
105
  hb_direction_t direction;
B
Behdad Esfahbod 已提交
106
  hb_mask_t lookup_mask;
107
  unsigned int nesting_level_left;
108
  unsigned int lookup_props;
109
  unsigned int property; /* propety of first glyph */
110
  unsigned int debug_depth;
111
  bool has_glyph_classes;
112

113 114 115 116

  hb_apply_context_t (hb_font_t *font_,
		      hb_face_t *face_,
		      hb_buffer_t *buffer_,
B
Minor  
Behdad Esfahbod 已提交
117
		      hb_mask_t lookup_mask_) :
118 119 120
			font (font_), face (face_), buffer (buffer_),
			direction (buffer_->props.direction),
			lookup_mask (lookup_mask_),
B
Minor  
Behdad Esfahbod 已提交
121
			nesting_level_left (MAX_NESTING_LEVEL),
122 123
			lookup_props (0), property (0), debug_depth (0),
			has_glyph_classes (hb_ot_layout_has_glyph_classes (face_)) {}
124

B
Behdad Esfahbod 已提交
125
  void set_lookup (const Lookup &l) {
126 127 128
    lookup_props = l.get_props ();
  }

B
Behdad Esfahbod 已提交
129 130 131 132
  struct mark_skipping_forward_iterator_t
  {
    inline mark_skipping_forward_iterator_t (hb_apply_context_t *c_,
					     unsigned int start_index_,
133
					     unsigned int num_items_,
B
Minor  
Behdad Esfahbod 已提交
134
					     bool context_match = false)
B
Behdad Esfahbod 已提交
135 136 137 138
    {
      c = c_;
      idx = start_index_;
      num_items = num_items_;
B
Minor  
Behdad Esfahbod 已提交
139
      mask = context_match ? -1 : c->lookup_mask;
140
      syllable = context_match ? 0 : c->buffer->cur().syllable ();
141
      end = c->buffer->len;
B
Behdad Esfahbod 已提交
142 143 144 145 146
    }
    inline bool has_no_chance (void) const
    {
      return unlikely (num_items && idx + num_items >= end);
    }
B
Minor  
Behdad Esfahbod 已提交
147
    inline void reject (void)
148 149 150
    {
      num_items++;
    }
B
Behdad Esfahbod 已提交
151
    inline bool next (unsigned int *property_out,
B
Minor  
Behdad Esfahbod 已提交
152
		      unsigned int  lookup_props)
B
Behdad Esfahbod 已提交
153
    {
154
      assert (num_items > 0);
B
Behdad Esfahbod 已提交
155 156 157 158
      do
      {
	if (has_no_chance ())
	  return false;
B
Behdad Esfahbod 已提交
159
	idx++;
B
Behdad Esfahbod 已提交
160 161
      } while (_hb_ot_layout_skip_mark (c->face, &c->buffer->info[idx], lookup_props, property_out));
      num_items--;
162
      return (c->buffer->info[idx].mask & mask) && (!syllable || syllable == c->buffer->info[idx].syllable ());
B
Behdad Esfahbod 已提交
163 164 165 166 167 168 169
    }
    inline bool next (unsigned int *property_out = NULL)
    {
      return next (property_out, c->lookup_props);
    }

    unsigned int idx;
170
    protected:
B
Behdad Esfahbod 已提交
171 172
    hb_apply_context_t *c;
    unsigned int num_items;
173
    hb_mask_t mask;
174
    uint8_t syllable;
B
Behdad Esfahbod 已提交
175 176 177 178 179 180 181
    unsigned int end;
  };

  struct mark_skipping_backward_iterator_t
  {
    inline mark_skipping_backward_iterator_t (hb_apply_context_t *c_,
					      unsigned int start_index_,
182
					      unsigned int num_items_,
183
					      hb_mask_t mask_ = 0,
B
Minor  
Behdad Esfahbod 已提交
184
					      bool match_syllable_ = true)
B
Behdad Esfahbod 已提交
185 186 187 188
    {
      c = c_;
      idx = start_index_;
      num_items = num_items_;
189
      mask = mask_ ? mask_ : c->lookup_mask;
190
      syllable = match_syllable_ ? c->buffer->cur().syllable () : 0;
B
Behdad Esfahbod 已提交
191 192 193
    }
    inline bool has_no_chance (void) const
    {
194
      return unlikely (idx < num_items);
B
Behdad Esfahbod 已提交
195
    }
B
Minor  
Behdad Esfahbod 已提交
196
    inline void reject (void)
197 198 199
    {
      num_items++;
    }
B
Behdad Esfahbod 已提交
200
    inline bool prev (unsigned int *property_out,
B
Minor  
Behdad Esfahbod 已提交
201
		      unsigned int  lookup_props)
B
Behdad Esfahbod 已提交
202
    {
203
      assert (num_items > 0);
B
Behdad Esfahbod 已提交
204 205 206 207 208 209 210
      do
      {
	if (has_no_chance ())
	  return false;
	idx--;
      } while (_hb_ot_layout_skip_mark (c->face, &c->buffer->out_info[idx], lookup_props, property_out));
      num_items--;
211
      return (c->buffer->out_info[idx].mask & mask) && (!syllable || syllable == c->buffer->out_info[idx].syllable ());
B
Behdad Esfahbod 已提交
212 213 214 215 216 217 218
    }
    inline bool prev (unsigned int *property_out = NULL)
    {
      return prev (property_out, c->lookup_props);
    }

    unsigned int idx;
219
    protected:
B
Behdad Esfahbod 已提交
220 221
    hb_apply_context_t *c;
    unsigned int num_items;
222
    hb_mask_t mask;
223
    uint8_t syllable;
B
Behdad Esfahbod 已提交
224 225
  };

226 227
  inline bool should_mark_skip_current_glyph (void) const
  {
B
Behdad Esfahbod 已提交
228 229
    unsigned int property;
    return _hb_ot_layout_skip_mark (face, &buffer->cur(), lookup_props, &property);
230
  }
B
Behdad Esfahbod 已提交
231

232 233 234 235
  inline void set_klass_guess (unsigned int klass_guess) const
  {
    buffer->cur().props_cache() = has_glyph_classes ? 0 : klass_guess;
  }
B
Behdad Esfahbod 已提交
236

237
  inline void output_glyph (hb_codepoint_t glyph_index,
238
			    unsigned int klass_guess = 0) const
239
  {
240
    set_klass_guess (klass_guess);
241 242
    buffer->output_glyph (glyph_index);
  }
243
  inline void replace_glyph (hb_codepoint_t glyph_index,
244
			     unsigned int klass_guess = 0) const
245
  {
246
    set_klass_guess (klass_guess);
247 248
    buffer->replace_glyph (glyph_index);
  }
B
Minor  
Behdad Esfahbod 已提交
249 250 251
  inline void replace_glyphs (unsigned int num_in,
			      unsigned int num_out,
			      hb_codepoint_t *glyph_data,
252
			      unsigned int klass_guess = 0) const
B
Minor  
Behdad Esfahbod 已提交
253
  {
254
    set_klass_guess (klass_guess);
B
Minor  
Behdad Esfahbod 已提交
255 256
    buffer->replace_glyphs (num_in, num_out, glyph_data);
  }
257 258
};

259

B
Behdad Esfahbod 已提交
260

261
typedef bool (*intersects_func_t) (hb_set_t *glyphs, const USHORT &value, const void *data);
B
Behdad Esfahbod 已提交
262
typedef bool (*match_func_t) (hb_codepoint_t glyph_id, const USHORT &value, const void *data);
B
Behdad Esfahbod 已提交
263
typedef void (*closure_lookup_func_t) (hb_closure_context_t *c, unsigned int lookup_index);
B
Behdad Esfahbod 已提交
264
typedef bool (*apply_lookup_func_t) (hb_apply_context_t *c, unsigned int lookup_index);
265

266 267 268 269 270 271
struct ContextClosureFuncs
{
  intersects_func_t intersects;
  closure_lookup_func_t closure;
};
struct ContextApplyFuncs
B
Behdad Esfahbod 已提交
272
{
273 274
  match_func_t match;
  apply_lookup_func_t apply;
275 276
};

277
static inline bool intersects_glyph (hb_set_t *glyphs, const USHORT &value, const void *data HB_UNUSED)
278 279 280
{
  return glyphs->has (value);
}
281
static inline bool intersects_class (hb_set_t *glyphs, const USHORT &value, const void *data)
282 283 284 285
{
  const ClassDef &class_def = *reinterpret_cast<const ClassDef *>(data);
  return class_def.intersects_class (glyphs, value);
}
286
static inline bool intersects_coverage (hb_set_t *glyphs, const USHORT &value, const void *data)
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
{
  const OffsetTo<Coverage> &coverage = (const OffsetTo<Coverage>&)value;
  return (data+coverage).intersects (glyphs);
}

static inline bool intersects_array (hb_closure_context_t *c,
				     unsigned int count,
				     const USHORT values[],
				     intersects_func_t intersects_func,
				     const void *intersects_data)
{
  for (unsigned int i = 0; i < count; i++)
    if (likely (!intersects_func (c->glyphs, values[i], intersects_data)))
      return false;
  return true;
}

304

B
Behdad Esfahbod 已提交
305
static inline bool match_glyph (hb_codepoint_t glyph_id, const USHORT &value, const void *data HB_UNUSED)
B
Behdad Esfahbod 已提交
306
{
307 308
  return glyph_id == value;
}
B
Behdad Esfahbod 已提交
309
static inline bool match_class (hb_codepoint_t glyph_id, const USHORT &value, const void *data)
B
Behdad Esfahbod 已提交
310
{
B
Behdad Esfahbod 已提交
311
  const ClassDef &class_def = *reinterpret_cast<const ClassDef *>(data);
312 313
  return class_def.get_class (glyph_id) == value;
}
B
Behdad Esfahbod 已提交
314
static inline bool match_coverage (hb_codepoint_t glyph_id, const USHORT &value, const void *data)
B
Behdad Esfahbod 已提交
315
{
316
  const OffsetTo<Coverage> &coverage = (const OffsetTo<Coverage>&)value;
317
  return (data+coverage).get_coverage (glyph_id) != NOT_COVERED;
318 319
}

320

321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
static inline bool would_match_input (hb_would_apply_context_t *c,
				      unsigned int count, /* Including the first glyph (not matched) */
				      const USHORT input[], /* Array of input values--start with second glyph */
				      match_func_t match_func,
				      const void *match_data)
{
  if (count != c->len)
    return false;

  for (unsigned int i = 1; i < count; i++)
    if (likely (!match_func (c->second, input[i - 1], match_data)))
      return false;

  return true;
}
B
Behdad Esfahbod 已提交
336
static inline bool match_input (hb_apply_context_t *c,
B
Behdad Esfahbod 已提交
337 338 339
				unsigned int count, /* Including the first glyph (not matched) */
				const USHORT input[], /* Array of input values--start with second glyph */
				match_func_t match_func,
B
Behdad Esfahbod 已提交
340
				const void *match_data,
341
				unsigned int *end_offset = NULL)
B
Behdad Esfahbod 已提交
342
{
B
Behdad Esfahbod 已提交
343 344
  hb_apply_context_t::mark_skipping_forward_iterator_t skippy_iter (c, c->buffer->idx, count - 1);
  if (skippy_iter.has_no_chance ())
B
Behdad Esfahbod 已提交
345
    return false;
B
Behdad Esfahbod 已提交
346

B
Minor  
Behdad Esfahbod 已提交
347
  for (unsigned int i = 1; i < count; i++)
B
Behdad Esfahbod 已提交
348
  {
B
Behdad Esfahbod 已提交
349 350
    if (!skippy_iter.next ())
      return false;
B
Behdad Esfahbod 已提交
351

B
Behdad Esfahbod 已提交
352
    if (likely (!match_func (c->buffer->info[skippy_iter.idx].codepoint, input[i - 1], match_data)))
B
Behdad Esfahbod 已提交
353 354 355
      return false;
  }

356 357
  if (end_offset)
    *end_offset = skippy_iter.idx - c->buffer->idx + 1;
B
Behdad Esfahbod 已提交
358 359 360 361

  return true;
}

B
Behdad Esfahbod 已提交
362
static inline bool match_backtrack (hb_apply_context_t *c,
363 364 365
				    unsigned int count,
				    const USHORT backtrack[],
				    match_func_t match_func,
B
Behdad Esfahbod 已提交
366
				    const void *match_data)
367
{
B
Minor  
Behdad Esfahbod 已提交
368
  hb_apply_context_t::mark_skipping_backward_iterator_t skippy_iter (c, c->buffer->backtrack_len (), count, true);
B
Behdad Esfahbod 已提交
369 370
  if (skippy_iter.has_no_chance ())
    return false;
371

372
  for (unsigned int i = 0; i < count; i++)
B
Behdad Esfahbod 已提交
373
  {
B
Behdad Esfahbod 已提交
374 375
    if (!skippy_iter.prev ())
      return false;
376

B
Behdad Esfahbod 已提交
377
    if (likely (!match_func (c->buffer->out_info[skippy_iter.idx].codepoint, backtrack[i], match_data)))
378 379 380 381 382 383
      return false;
  }

  return true;
}

B
Behdad Esfahbod 已提交
384
static inline bool match_lookahead (hb_apply_context_t *c,
B
Behdad Esfahbod 已提交
385 386 387
				    unsigned int count,
				    const USHORT lookahead[],
				    match_func_t match_func,
B
Behdad Esfahbod 已提交
388
				    const void *match_data,
B
Behdad Esfahbod 已提交
389
				    unsigned int offset)
390
{
B
Minor  
Behdad Esfahbod 已提交
391
  hb_apply_context_t::mark_skipping_forward_iterator_t skippy_iter (c, c->buffer->idx + offset - 1, count, true);
B
Behdad Esfahbod 已提交
392 393
  if (skippy_iter.has_no_chance ())
    return false;
394

B
Minor  
Behdad Esfahbod 已提交
395
  for (unsigned int i = 0; i < count; i++)
B
Behdad Esfahbod 已提交
396
  {
B
Behdad Esfahbod 已提交
397 398
    if (!skippy_iter.next ())
      return false;
399

B
Behdad Esfahbod 已提交
400
    if (likely (!match_func (c->buffer->info[skippy_iter.idx].codepoint, lookahead[i], match_data)))
401 402 403
      return false;
  }

404 405 406
  return true;
}

B
Behdad Esfahbod 已提交
407

408

B
Behdad Esfahbod 已提交
409 410
struct LookupRecord
{
B
Behdad Esfahbod 已提交
411
  inline bool sanitize (hb_sanitize_context_t *c) {
412
    TRACE_SANITIZE ();
413
    return TRACE_RETURN (c->check_struct (this));
B
Behdad Esfahbod 已提交
414 415
  }

416 417 418 419
  USHORT	sequenceIndex;		/* Index into current glyph
					 * sequence--first glyph = 0 */
  USHORT	lookupListIndex;	/* Lookup to apply to that
					 * position--zero--based */
B
Behdad Esfahbod 已提交
420 421
  public:
  DEFINE_SIZE_STATIC (4);
422 423
};

B
Behdad Esfahbod 已提交
424

B
Behdad Esfahbod 已提交
425
static inline void closure_lookup (hb_closure_context_t *c,
426 427 428 429 430
				   unsigned int lookupCount,
				   const LookupRecord lookupRecord[], /* Array of LookupRecords--in design order */
				   closure_lookup_func_t closure_func)
{
  for (unsigned int i = 0; i < lookupCount; i++)
B
Behdad Esfahbod 已提交
431
    closure_func (c, lookupRecord->lookupListIndex);
432
}
B
Behdad Esfahbod 已提交
433

B
Behdad Esfahbod 已提交
434
static inline bool apply_lookup (hb_apply_context_t *c,
435 436
				 unsigned int count, /* Including the first glyph */
				 unsigned int lookupCount,
437 438 439
				 const LookupRecord lookupRecord[], /* Array of LookupRecords--in design order */
				 apply_lookup_func_t apply_func)
{
440
  unsigned int end = c->buffer->len;
441
  if (unlikely (count == 0 || c->buffer->idx + count > end))
442
    return false;
443

B
Behdad Esfahbod 已提交
444 445
  /* TODO We don't support lookupRecord arrays that are not increasing:
   *      Should be easy for in_place ones at least. */
B
Behdad Esfahbod 已提交
446

447
  /* Note: If sublookup is reverse, it will underflow after the first loop
B
Behdad Esfahbod 已提交
448 449 450
   * and we jump out of it.  Not entirely disastrous.  So we don't check
   * for reverse lookup here.
   */
B
Behdad Esfahbod 已提交
451
  for (unsigned int i = 0; i < count; /* NOP */)
B
Behdad Esfahbod 已提交
452
  {
453 454
    if (unlikely (c->buffer->idx == end))
      return true;
455
    while (c->should_mark_skip_current_glyph ())
B
Behdad Esfahbod 已提交
456
    {
457
      /* No lookup applied for this index */
B
Behdad Esfahbod 已提交
458
      c->buffer->next_glyph ();
459 460
      if (unlikely (c->buffer->idx == end))
	return true;
461 462
    }

B
Behdad Esfahbod 已提交
463
    if (lookupCount && i == lookupRecord->sequenceIndex)
464
    {
465
      unsigned int old_pos = c->buffer->idx;
466 467

      /* Apply a lookup */
B
Behdad Esfahbod 已提交
468
      bool done = apply_func (c, lookupRecord->lookupListIndex);
469

B
Behdad Esfahbod 已提交
470 471
      lookupRecord++;
      lookupCount--;
B
Behdad Esfahbod 已提交
472
      /* Err, this is wrong if the lookup jumped over some glyphs */
473 474
      i += c->buffer->idx - old_pos;
      if (unlikely (c->buffer->idx == end))
475
	return true;
476 477 478 479 480 481 482 483

      if (!done)
	goto not_applied;
    }
    else
    {
    not_applied:
      /* No lookup applied for this index */
B
Behdad Esfahbod 已提交
484
      c->buffer->next_glyph ();
485 486 487 488 489 490
      i++;
    }
  }

  return true;
}
491

B
Behdad Esfahbod 已提交
492

493 494 495

/* Contextual lookups */

496 497 498 499 500 501 502
struct ContextClosureLookupContext
{
  ContextClosureFuncs funcs;
  const void *intersects_data;
};

struct ContextApplyLookupContext
B
Behdad Esfahbod 已提交
503
{
504
  ContextApplyFuncs funcs;
B
Behdad Esfahbod 已提交
505
  const void *match_data;
506 507
};

B
Behdad Esfahbod 已提交
508
static inline void context_closure_lookup (hb_closure_context_t *c,
509 510 511 512 513 514
					   unsigned int inputCount, /* Including the first glyph (not matched) */
					   const USHORT input[], /* Array of input values--start with second glyph */
					   unsigned int lookupCount,
					   const LookupRecord lookupRecord[],
					   ContextClosureLookupContext &lookup_context)
{
B
Behdad Esfahbod 已提交
515 516 517 518 519 520
  if (intersects_array (c,
			inputCount ? inputCount - 1 : 0, input,
			lookup_context.funcs.intersects, lookup_context.intersects_data))
    closure_lookup (c,
		    lookupCount, lookupRecord,
		    lookup_context.funcs.closure);
521 522 523
}


524 525 526 527 528 529 530 531 532 533 534
static inline bool context_would_apply_lookup (hb_would_apply_context_t *c,
					       unsigned int inputCount, /* Including the first glyph (not matched) */
					       const USHORT input[], /* Array of input values--start with second glyph */
					       unsigned int lookupCount,
					       const LookupRecord lookupRecord[],
					       ContextApplyLookupContext &lookup_context)
{
  return would_match_input (c,
			    inputCount, input,
			    lookup_context.funcs.match, lookup_context.match_data);
}
535 536 537 538 539 540
static inline bool context_apply_lookup (hb_apply_context_t *c,
					 unsigned int inputCount, /* Including the first glyph (not matched) */
					 const USHORT input[], /* Array of input values--start with second glyph */
					 unsigned int lookupCount,
					 const LookupRecord lookupRecord[],
					 ContextApplyLookupContext &lookup_context)
541
{
B
Behdad Esfahbod 已提交
542
  return match_input (c,
B
Behdad Esfahbod 已提交
543
		      inputCount, input,
544 545
		      lookup_context.funcs.match, lookup_context.match_data)
      && apply_lookup (c,
B
Behdad Esfahbod 已提交
546 547 548
		       inputCount,
		       lookupCount, lookupRecord,
		       lookup_context.funcs.apply);
549 550
}

B
Behdad Esfahbod 已提交
551 552
struct Rule
{
553 554 555
  friend struct RuleSet;

  private:
556

B
Behdad Esfahbod 已提交
557
  inline void closure (hb_closure_context_t *c, ContextClosureLookupContext &lookup_context) const
558 559 560
  {
    TRACE_CLOSURE ();
    const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (input, input[0].static_size * (inputCount ? inputCount - 1 : 0));
B
Behdad Esfahbod 已提交
561 562 563 564
    context_closure_lookup (c,
			    inputCount, input,
			    lookupCount, lookupRecord,
			    lookup_context);
565 566
  }

567 568 569 570 571 572 573
  inline bool would_apply (hb_would_apply_context_t *c, ContextApplyLookupContext &lookup_context) const
  {
    TRACE_WOULD_APPLY ();
    const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (input, input[0].static_size * (inputCount ? inputCount - 1 : 0));
    return TRACE_RETURN (context_would_apply_lookup (c, inputCount, input, lookupCount, lookupRecord, lookup_context));
  }

574
  inline bool apply (hb_apply_context_t *c, ContextApplyLookupContext &lookup_context) const
B
Behdad Esfahbod 已提交
575
  {
576
    TRACE_APPLY ();
B
Behdad Esfahbod 已提交
577
    const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (input, input[0].static_size * (inputCount ? inputCount - 1 : 0));
578
    return TRACE_RETURN (context_apply_lookup (c, inputCount, input, lookupCount, lookupRecord, lookup_context));
579 580
  }

B
Behdad Esfahbod 已提交
581
  public:
B
Behdad Esfahbod 已提交
582
  inline bool sanitize (hb_sanitize_context_t *c) {
583
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
584 585 586
    return inputCount.sanitize (c)
	&& lookupCount.sanitize (c)
	&& c->check_range (input,
587 588
			   input[0].static_size * inputCount
			   + lookupRecordX[0].static_size * lookupCount);
B
Behdad Esfahbod 已提交
589 590
  }

591
  protected:
592
  USHORT	inputCount;		/* Total number of glyphs in input
593
					 * glyph sequence--includes the first
594
					 * glyph */
595
  USHORT	lookupCount;		/* Number of LookupRecords */
B
Behdad Esfahbod 已提交
596
  USHORT	input[VAR];		/* Array of match inputs--start with
597
					 * second glyph */
B
Behdad Esfahbod 已提交
598
  LookupRecord	lookupRecordX[VAR];	/* Array of LookupRecords--in
599
					 * design order */
B
Behdad Esfahbod 已提交
600
  public:
601
  DEFINE_SIZE_ARRAY2 (4, input, lookupRecordX);
602 603
};

B
Behdad Esfahbod 已提交
604 605
struct RuleSet
{
B
Behdad Esfahbod 已提交
606
  inline void closure (hb_closure_context_t *c, ContextClosureLookupContext &lookup_context) const
607 608 609 610
  {
    TRACE_CLOSURE ();
    unsigned int num_rules = rule.len;
    for (unsigned int i = 0; i < num_rules; i++)
B
Behdad Esfahbod 已提交
611
      (this+rule[i]).closure (c, lookup_context);
612 613
  }

614 615 616 617 618 619 620 621 622 623 624 625
  inline bool would_apply (hb_would_apply_context_t *c, ContextApplyLookupContext &lookup_context) const
  {
    TRACE_WOULD_APPLY ();
    unsigned int num_rules = rule.len;
    for (unsigned int i = 0; i < num_rules; i++)
    {
      if ((this+rule[i]).would_apply (c, lookup_context))
        return TRACE_RETURN (true);
    }
    return TRACE_RETURN (false);
  }

626
  inline bool apply (hb_apply_context_t *c, ContextApplyLookupContext &lookup_context) const
B
Behdad Esfahbod 已提交
627
  {
628
    TRACE_APPLY ();
629
    unsigned int num_rules = rule.len;
B
Behdad Esfahbod 已提交
630 631
    for (unsigned int i = 0; i < num_rules; i++)
    {
B
Behdad Esfahbod 已提交
632
      if ((this+rule[i]).apply (c, lookup_context))
633
        return TRACE_RETURN (true);
634
    }
635
    return TRACE_RETURN (false);
636 637
  }

B
Behdad Esfahbod 已提交
638
  inline bool sanitize (hb_sanitize_context_t *c) {
639
    TRACE_SANITIZE ();
640
    return TRACE_RETURN (rule.sanitize (c, this));
B
Behdad Esfahbod 已提交
641 642
  }

643
  protected:
644
  OffsetArrayOf<Rule>
645
		rule;			/* Array of Rule tables
646
					 * ordered by preference */
B
Behdad Esfahbod 已提交
647
  public:
648
  DEFINE_SIZE_ARRAY (2, rule);
649 650 651
};


B
Behdad Esfahbod 已提交
652 653
struct ContextFormat1
{
654 655 656
  friend struct Context;

  private:
657

B
Behdad Esfahbod 已提交
658
  inline void closure (hb_closure_context_t *c, closure_lookup_func_t closure_func) const
659 660
  {
    TRACE_CLOSURE ();
661 662 663 664 665 666 667 668 669 670 671 672

    const Coverage &cov = (this+coverage);

    struct ContextClosureLookupContext lookup_context = {
      {intersects_glyph, closure_func},
      NULL
    };

    unsigned int count = ruleSet.len;
    for (unsigned int i = 0; i < count; i++)
      if (cov.intersects_coverage (c->glyphs, i)) {
	const RuleSet &rule_set = this+ruleSet[i];
B
Behdad Esfahbod 已提交
673
	rule_set.closure (c, lookup_context);
674
      }
675 676
  }

677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
  inline bool would_apply (hb_would_apply_context_t *c) const
  {
    TRACE_WOULD_APPLY ();
    unsigned int index = (this+coverage) (c->first);
    if (likely (index == NOT_COVERED))
      return TRACE_RETURN (false);

    const RuleSet &rule_set = this+ruleSet[index];
    struct ContextApplyLookupContext lookup_context = {
      {match_glyph, NULL},
      NULL
    };
    return TRACE_RETURN (rule_set.would_apply (c, lookup_context));
  }

B
Behdad Esfahbod 已提交
692
  inline bool apply (hb_apply_context_t *c, apply_lookup_func_t apply_func) const
B
Behdad Esfahbod 已提交
693
  {
694
    TRACE_APPLY ();
695
    unsigned int index = (this+coverage) (c->buffer->cur().codepoint);
696
    if (likely (index == NOT_COVERED))
697
      return TRACE_RETURN (false);
698

699
    const RuleSet &rule_set = this+ruleSet[index];
700
    struct ContextApplyLookupContext lookup_context = {
701 702
      {match_glyph, apply_func},
      NULL
703
    };
704
    return TRACE_RETURN (rule_set.apply (c, lookup_context));
705 706
  }

B
Behdad Esfahbod 已提交
707
  inline bool sanitize (hb_sanitize_context_t *c) {
708
    TRACE_SANITIZE ();
709
    return TRACE_RETURN (coverage.sanitize (c, this) && ruleSet.sanitize (c, this));
B
Behdad Esfahbod 已提交
710 711
  }

712
  protected:
713 714 715 716 717 718 719
  USHORT	format;			/* Format identifier--format = 1 */
  OffsetTo<Coverage>
		coverage;		/* Offset to Coverage table--from
					 * beginning of table */
  OffsetArrayOf<RuleSet>
		ruleSet;		/* Array of RuleSet tables
					 * ordered by Coverage Index */
720
  public:
721
  DEFINE_SIZE_ARRAY (6, ruleSet);
722 723 724
};


B
Behdad Esfahbod 已提交
725 726
struct ContextFormat2
{
727 728 729
  friend struct Context;

  private:
730

B
Behdad Esfahbod 已提交
731
  inline void closure (hb_closure_context_t *c, closure_lookup_func_t closure_func) const
732 733
  {
    TRACE_CLOSURE ();
734
    if (!(this+coverage).intersects (c->glyphs))
B
Behdad Esfahbod 已提交
735
      return;
736 737 738 739 740 741 742 743 744 745 746 747

    const ClassDef &class_def = this+classDef;

    struct ContextClosureLookupContext lookup_context = {
      {intersects_class, closure_func},
      NULL
    };

    unsigned int count = ruleSet.len;
    for (unsigned int i = 0; i < count; i++)
      if (class_def.intersects_class (c->glyphs, i)) {
	const RuleSet &rule_set = this+ruleSet[i];
B
Behdad Esfahbod 已提交
748
	rule_set.closure (c, lookup_context);
749
      }
750 751
  }

752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767
  inline bool would_apply (hb_would_apply_context_t *c) const
  {
    TRACE_WOULD_APPLY ();
    unsigned int index = (this+coverage) (c->first);
    if (likely (index == NOT_COVERED)) return TRACE_RETURN (false);

    const ClassDef &class_def = this+classDef;
    index = class_def (c->first);
    const RuleSet &rule_set = this+ruleSet[index];
    struct ContextApplyLookupContext lookup_context = {
      {match_class, NULL},
      &class_def
    };
    return TRACE_RETURN (rule_set.would_apply (c, lookup_context));
  }

B
Behdad Esfahbod 已提交
768
  inline bool apply (hb_apply_context_t *c, apply_lookup_func_t apply_func) const
B
Behdad Esfahbod 已提交
769
  {
770
    TRACE_APPLY ();
771
    unsigned int index = (this+coverage) (c->buffer->cur().codepoint);
772
    if (likely (index == NOT_COVERED)) return TRACE_RETURN (false);
773 774

    const ClassDef &class_def = this+classDef;
775
    index = class_def (c->buffer->cur().codepoint);
776
    const RuleSet &rule_set = this+ruleSet[index];
777
    struct ContextApplyLookupContext lookup_context = {
B
Behdad Esfahbod 已提交
778 779
      {match_class, apply_func},
      &class_def
780
    };
781
    return TRACE_RETURN (rule_set.apply (c, lookup_context));
782 783
  }

B
Behdad Esfahbod 已提交
784
  inline bool sanitize (hb_sanitize_context_t *c) {
785
    TRACE_SANITIZE ();
786
    return TRACE_RETURN (coverage.sanitize (c, this) && classDef.sanitize (c, this) && ruleSet.sanitize (c, this));
B
Behdad Esfahbod 已提交
787 788
  }

789
  protected:
790 791 792 793 794 795 796 797 798 799
  USHORT	format;			/* Format identifier--format = 2 */
  OffsetTo<Coverage>
		coverage;		/* Offset to Coverage table--from
					 * beginning of table */
  OffsetTo<ClassDef>
		classDef;		/* Offset to glyph ClassDef table--from
					 * beginning of table */
  OffsetArrayOf<RuleSet>
		ruleSet;		/* Array of RuleSet tables
					 * ordered by class */
800
  public:
801
  DEFINE_SIZE_ARRAY (8, ruleSet);
802 803 804
};


B
Behdad Esfahbod 已提交
805 806
struct ContextFormat3
{
807 808 809
  friend struct Context;

  private:
810

B
Behdad Esfahbod 已提交
811
  inline void closure (hb_closure_context_t *c, closure_lookup_func_t closure_func) const
812 813
  {
    TRACE_CLOSURE ();
814
    if (!(this+coverage[0]).intersects (c->glyphs))
B
Behdad Esfahbod 已提交
815
      return;
816 817 818 819 820 821

    const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (coverage, coverage[0].static_size * glyphCount);
    struct ContextClosureLookupContext lookup_context = {
      {intersects_coverage, closure_func},
      this
    };
B
Behdad Esfahbod 已提交
822 823 824 825
    context_closure_lookup (c,
			    glyphCount, (const USHORT *) (coverage + 1),
			    lookupCount, lookupRecord,
			    lookup_context);
826 827
  }

828 829 830 831 832 833 834 835 836 837 838 839 840 841
  inline bool would_apply (hb_would_apply_context_t *c) const
  {
    TRACE_WOULD_APPLY ();
    unsigned int index = (this+coverage[0]) (c->first);
    if (likely (index == NOT_COVERED)) return TRACE_RETURN (false);

    const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (coverage, coverage[0].static_size * glyphCount);
    struct ContextApplyLookupContext lookup_context = {
      {match_coverage, NULL},
      this
    };
    return TRACE_RETURN (context_would_apply_lookup (c, glyphCount, (const USHORT *) (coverage + 1), lookupCount, lookupRecord, lookup_context));
  }

B
Behdad Esfahbod 已提交
842
  inline bool apply (hb_apply_context_t *c, apply_lookup_func_t apply_func) const
B
Behdad Esfahbod 已提交
843
  {
844
    TRACE_APPLY ();
845
    unsigned int index = (this+coverage[0]) (c->buffer->cur().codepoint);
846
    if (likely (index == NOT_COVERED)) return TRACE_RETURN (false);
847

B
Behdad Esfahbod 已提交
848
    const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (coverage, coverage[0].static_size * glyphCount);
849
    struct ContextApplyLookupContext lookup_context = {
850
      {match_coverage, apply_func},
B
Behdad Esfahbod 已提交
851
      this
852
    };
853
    return TRACE_RETURN (context_apply_lookup (c, glyphCount, (const USHORT *) (coverage + 1), lookupCount, lookupRecord, lookup_context));
854 855
  }

B
Behdad Esfahbod 已提交
856
  inline bool sanitize (hb_sanitize_context_t *c) {
857
    TRACE_SANITIZE ();
858
    if (!c->check_struct (this)) return TRACE_RETURN (false);
B
Behdad Esfahbod 已提交
859
    unsigned int count = glyphCount;
860
    if (!c->check_array (coverage, coverage[0].static_size, count)) return TRACE_RETURN (false);
B
Behdad Esfahbod 已提交
861
    for (unsigned int i = 0; i < count; i++)
862
      if (!coverage[i].sanitize (c, this)) return TRACE_RETURN (false);
B
Behdad Esfahbod 已提交
863
    LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (coverage, coverage[0].static_size * count);
864
    return TRACE_RETURN (c->check_array (lookupRecord, lookupRecord[0].static_size, lookupCount));
B
Behdad Esfahbod 已提交
865 866
  }

867
  protected:
868 869 870
  USHORT	format;			/* Format identifier--format = 3 */
  USHORT	glyphCount;		/* Number of glyphs in the input glyph
					 * sequence */
871
  USHORT	lookupCount;		/* Number of LookupRecords */
872
  OffsetTo<Coverage>
B
Behdad Esfahbod 已提交
873
		coverage[VAR];		/* Array of offsets to Coverage
874
					 * table in glyph sequence order */
B
Behdad Esfahbod 已提交
875
  LookupRecord	lookupRecordX[VAR];	/* Array of LookupRecords--in
876
					 * design order */
B
Behdad Esfahbod 已提交
877
  public:
878
  DEFINE_SIZE_ARRAY2 (6, coverage, lookupRecordX);
879 880
};

B
Behdad Esfahbod 已提交
881 882
struct Context
{
883
  protected:
884

B
Behdad Esfahbod 已提交
885
  inline void closure (hb_closure_context_t *c, closure_lookup_func_t closure_func) const
886 887 888
  {
    TRACE_CLOSURE ();
    switch (u.format) {
B
Behdad Esfahbod 已提交
889 890 891 892
    case 1: u.format1.closure (c, closure_func); break;
    case 2: u.format2.closure (c, closure_func); break;
    case 3: u.format3.closure (c, closure_func); break;
    default:                                     break;
893 894 895
    }
  }

896 897 898 899 900 901 902 903 904 905
  inline bool would_apply (hb_would_apply_context_t *c) const
  {
    switch (u.format) {
    case 1: return u.format1.would_apply (c);
    case 2: return u.format2.would_apply (c);
    case 3: return u.format3.would_apply (c);
    default:return false;
    }
  }

B
Behdad Esfahbod 已提交
906
  inline bool apply (hb_apply_context_t *c, apply_lookup_func_t apply_func) const
B
Behdad Esfahbod 已提交
907
  {
908
    TRACE_APPLY ();
909
    switch (u.format) {
910 911 912 913
    case 1: return TRACE_RETURN (u.format1.apply (c, apply_func));
    case 2: return TRACE_RETURN (u.format2.apply (c, apply_func));
    case 3: return TRACE_RETURN (u.format3.apply (c, apply_func));
    default:return TRACE_RETURN (false);
914 915 916
    }
  }

B
Behdad Esfahbod 已提交
917
  inline bool sanitize (hb_sanitize_context_t *c) {
918
    TRACE_SANITIZE ();
919
    if (!u.format.sanitize (c)) return TRACE_RETURN (false);
B
Behdad Esfahbod 已提交
920
    switch (u.format) {
921 922 923 924
    case 1: return TRACE_RETURN (u.format1.sanitize (c));
    case 2: return TRACE_RETURN (u.format2.sanitize (c));
    case 3: return TRACE_RETURN (u.format3.sanitize (c));
    default:return TRACE_RETURN (true);
B
Behdad Esfahbod 已提交
925 926 927
    }
  }

928
  protected:
929
  union {
B
Behdad Esfahbod 已提交
930
  USHORT		format;		/* Format identifier */
B
Behdad Esfahbod 已提交
931 932 933
  ContextFormat1	format1;
  ContextFormat2	format2;
  ContextFormat3	format3;
934 935 936
  } u;
};

937 938 939

/* Chaining Contextual lookups */

940
struct ChainContextClosureLookupContext
B
Behdad Esfahbod 已提交
941
{
942 943 944 945 946 947 948
  ContextClosureFuncs funcs;
  const void *intersects_data[3];
};

struct ChainContextApplyLookupContext
{
  ContextApplyFuncs funcs;
B
Behdad Esfahbod 已提交
949
  const void *match_data[3];
950 951
};

B
Behdad Esfahbod 已提交
952
static inline void chain_context_closure_lookup (hb_closure_context_t *c,
953 954 955 956 957 958 959 960 961 962
						 unsigned int backtrackCount,
						 const USHORT backtrack[],
						 unsigned int inputCount, /* Including the first glyph (not matched) */
						 const USHORT input[], /* Array of input values--start with second glyph */
						 unsigned int lookaheadCount,
						 const USHORT lookahead[],
						 unsigned int lookupCount,
						 const LookupRecord lookupRecord[],
						 ChainContextClosureLookupContext &lookup_context)
{
B
Behdad Esfahbod 已提交
963 964 965 966 967 968 969 970 971 972 973 974
  if (intersects_array (c,
			backtrackCount, backtrack,
			lookup_context.funcs.intersects, lookup_context.intersects_data[0])
   && intersects_array (c,
			inputCount ? inputCount - 1 : 0, input,
			lookup_context.funcs.intersects, lookup_context.intersects_data[1])
  && intersects_array (c,
		       lookaheadCount, lookahead,
		       lookup_context.funcs.intersects, lookup_context.intersects_data[2]))
    closure_lookup (c,
		    lookupCount, lookupRecord,
		    lookup_context.funcs.closure);
975 976
}

977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994
static inline bool chain_context_would_apply_lookup (hb_would_apply_context_t *c,
						     unsigned int backtrackCount,
						     const USHORT backtrack[],
						     unsigned int inputCount, /* Including the first glyph (not matched) */
						     const USHORT input[], /* Array of input values--start with second glyph */
						     unsigned int lookaheadCount,
						     const USHORT lookahead[],
						     unsigned int lookupCount,
						     const LookupRecord lookupRecord[],
						     ChainContextApplyLookupContext &lookup_context)
{
  return !backtrackCount
      && !lookaheadCount
      && would_match_input (c,
			    inputCount, input,
			    lookup_context.funcs.match, lookup_context.match_data[1]);
}

995 996 997 998 999 1000 1001 1002 1003 1004
static inline bool chain_context_apply_lookup (hb_apply_context_t *c,
					       unsigned int backtrackCount,
					       const USHORT backtrack[],
					       unsigned int inputCount, /* Including the first glyph (not matched) */
					       const USHORT input[], /* Array of input values--start with second glyph */
					       unsigned int lookaheadCount,
					       const USHORT lookahead[],
					       unsigned int lookupCount,
					       const LookupRecord lookupRecord[],
					       ChainContextApplyLookupContext &lookup_context)
1005
{
1006
  unsigned int lookahead_offset;
B
Behdad Esfahbod 已提交
1007
  return match_input (c,
B
Behdad Esfahbod 已提交
1008 1009
		      inputCount, input,
		      lookup_context.funcs.match, lookup_context.match_data[1],
1010
		      &lookahead_offset)
B
Behdad Esfahbod 已提交
1011 1012 1013
      && match_backtrack (c,
			  backtrackCount, backtrack,
			  lookup_context.funcs.match, lookup_context.match_data[0])
B
Behdad Esfahbod 已提交
1014
      && match_lookahead (c,
B
Behdad Esfahbod 已提交
1015 1016
			  lookaheadCount, lookahead,
			  lookup_context.funcs.match, lookup_context.match_data[2],
1017 1018
			  lookahead_offset)
      && apply_lookup (c,
B
Behdad Esfahbod 已提交
1019 1020 1021
		       inputCount,
		       lookupCount, lookupRecord,
		       lookup_context.funcs.apply);
1022
}
1023

B
Behdad Esfahbod 已提交
1024 1025
struct ChainRule
{
1026 1027 1028
  friend struct ChainRuleSet;

  private:
1029

B
Behdad Esfahbod 已提交
1030
  inline void closure (hb_closure_context_t *c, ChainContextClosureLookupContext &lookup_context) const
1031 1032 1033 1034 1035
  {
    TRACE_CLOSURE ();
    const HeadlessArrayOf<USHORT> &input = StructAfter<HeadlessArrayOf<USHORT> > (backtrack);
    const ArrayOf<USHORT> &lookahead = StructAfter<ArrayOf<USHORT> > (input);
    const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
B
Behdad Esfahbod 已提交
1036 1037 1038 1039 1040 1041
    chain_context_closure_lookup (c,
				  backtrack.len, backtrack.array,
				  input.len, input.array,
				  lookahead.len, lookahead.array,
				  lookup.len, lookup.array,
				  lookup_context);
1042 1043
  }

1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056
  inline bool would_apply (hb_would_apply_context_t *c, ChainContextApplyLookupContext &lookup_context) const
  {
    TRACE_WOULD_APPLY ();
    const HeadlessArrayOf<USHORT> &input = StructAfter<HeadlessArrayOf<USHORT> > (backtrack);
    const ArrayOf<USHORT> &lookahead = StructAfter<ArrayOf<USHORT> > (input);
    const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
    return TRACE_RETURN (chain_context_would_apply_lookup (c,
							   backtrack.len, backtrack.array,
							   input.len, input.array,
							   lookahead.len, lookahead.array, lookup.len,
							   lookup.array, lookup_context));
  }

1057
  inline bool apply (hb_apply_context_t *c, ChainContextApplyLookupContext &lookup_context) const
B
Behdad Esfahbod 已提交
1058
  {
1059
    TRACE_APPLY ();
1060 1061 1062
    const HeadlessArrayOf<USHORT> &input = StructAfter<HeadlessArrayOf<USHORT> > (backtrack);
    const ArrayOf<USHORT> &lookahead = StructAfter<ArrayOf<USHORT> > (input);
    const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
1063 1064 1065 1066 1067
    return TRACE_RETURN (chain_context_apply_lookup (c,
						     backtrack.len, backtrack.array,
						     input.len, input.array,
						     lookahead.len, lookahead.array, lookup.len,
						     lookup.array, lookup_context));
1068 1069
  }

B
Behdad Esfahbod 已提交
1070
  public:
B
Behdad Esfahbod 已提交
1071
  inline bool sanitize (hb_sanitize_context_t *c) {
1072
    TRACE_SANITIZE ();
1073
    if (!backtrack.sanitize (c)) return TRACE_RETURN (false);
1074
    HeadlessArrayOf<USHORT> &input = StructAfter<HeadlessArrayOf<USHORT> > (backtrack);
1075
    if (!input.sanitize (c)) return TRACE_RETURN (false);
1076
    ArrayOf<USHORT> &lookahead = StructAfter<ArrayOf<USHORT> > (input);
1077
    if (!lookahead.sanitize (c)) return TRACE_RETURN (false);
1078
    ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
1079
    return TRACE_RETURN (lookup.sanitize (c));
B
Behdad Esfahbod 已提交
1080
  }
1081

1082
  protected:
B
Behdad Esfahbod 已提交
1083 1084
  ArrayOf<USHORT>
		backtrack;		/* Array of backtracking values
1085 1086
					 * (to be matched before the input
					 * sequence) */
B
Behdad Esfahbod 已提交
1087 1088
  HeadlessArrayOf<USHORT>
		inputX;			/* Array of input values (start with
1089
					 * second glyph) */
B
Behdad Esfahbod 已提交
1090 1091
  ArrayOf<USHORT>
		lookaheadX;		/* Array of lookahead values's (to be
1092
					 * matched after the input sequence) */
B
Behdad Esfahbod 已提交
1093
  ArrayOf<LookupRecord>
1094
		lookupX;		/* Array of LookupRecords--in
1095
					 * design order) */
1096
  public:
B
Behdad Esfahbod 已提交
1097
  DEFINE_SIZE_MIN (8);
1098 1099
};

B
Behdad Esfahbod 已提交
1100 1101
struct ChainRuleSet
{
B
Behdad Esfahbod 已提交
1102
  inline void closure (hb_closure_context_t *c, ChainContextClosureLookupContext &lookup_context) const
1103 1104 1105 1106
  {
    TRACE_CLOSURE ();
    unsigned int num_rules = rule.len;
    for (unsigned int i = 0; i < num_rules; i++)
B
Behdad Esfahbod 已提交
1107
      (this+rule[i]).closure (c, lookup_context);
1108 1109
  }

1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120
  inline bool would_apply (hb_would_apply_context_t *c, ChainContextApplyLookupContext &lookup_context) const
  {
    TRACE_WOULD_APPLY ();
    unsigned int num_rules = rule.len;
    for (unsigned int i = 0; i < num_rules; i++)
      if ((this+rule[i]).would_apply (c, lookup_context))
        return TRACE_RETURN (true);

    return TRACE_RETURN (false);
  }

1121
  inline bool apply (hb_apply_context_t *c, ChainContextApplyLookupContext &lookup_context) const
B
Behdad Esfahbod 已提交
1122
  {
1123
    TRACE_APPLY ();
1124
    unsigned int num_rules = rule.len;
B
Behdad Esfahbod 已提交
1125
    for (unsigned int i = 0; i < num_rules; i++)
B
Behdad Esfahbod 已提交
1126
      if ((this+rule[i]).apply (c, lookup_context))
1127
        return TRACE_RETURN (true);
1128

1129
    return TRACE_RETURN (false);
1130
  }
1131

B
Behdad Esfahbod 已提交
1132
  inline bool sanitize (hb_sanitize_context_t *c) {
1133
    TRACE_SANITIZE ();
1134
    return TRACE_RETURN (rule.sanitize (c, this));
B
Behdad Esfahbod 已提交
1135 1136
  }

1137
  protected:
1138 1139 1140
  OffsetArrayOf<ChainRule>
		rule;			/* Array of ChainRule tables
					 * ordered by preference */
1141
  public:
1142
  DEFINE_SIZE_ARRAY (2, rule);
1143 1144
};

B
Behdad Esfahbod 已提交
1145 1146
struct ChainContextFormat1
{
1147 1148 1149
  friend struct ChainContext;

  private:
1150

B
Behdad Esfahbod 已提交
1151
  inline void closure (hb_closure_context_t *c, closure_lookup_func_t closure_func) const
1152 1153
  {
    TRACE_CLOSURE ();
1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164
    const Coverage &cov = (this+coverage);

    struct ChainContextClosureLookupContext lookup_context = {
      {intersects_glyph, closure_func},
      {NULL, NULL, NULL}
    };

    unsigned int count = ruleSet.len;
    for (unsigned int i = 0; i < count; i++)
      if (cov.intersects_coverage (c->glyphs, i)) {
	const ChainRuleSet &rule_set = this+ruleSet[i];
B
Behdad Esfahbod 已提交
1165
	rule_set.closure (c, lookup_context);
1166
      }
1167 1168
  }

1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182
  inline bool would_apply (hb_would_apply_context_t *c) const
  {
    TRACE_WOULD_APPLY ();
    unsigned int index = (this+coverage) (c->first);
    if (likely (index == NOT_COVERED)) return TRACE_RETURN (false);

    const ChainRuleSet &rule_set = this+ruleSet[index];
    struct ChainContextApplyLookupContext lookup_context = {
      {match_glyph, NULL},
      {NULL, NULL, NULL}
    };
    return TRACE_RETURN (rule_set.would_apply (c, lookup_context));
  }

B
Behdad Esfahbod 已提交
1183
  inline bool apply (hb_apply_context_t *c, apply_lookup_func_t apply_func) const
B
Behdad Esfahbod 已提交
1184
  {
1185
    TRACE_APPLY ();
1186
    unsigned int index = (this+coverage) (c->buffer->cur().codepoint);
1187
    if (likely (index == NOT_COVERED)) return TRACE_RETURN (false);
1188

1189
    const ChainRuleSet &rule_set = this+ruleSet[index];
1190
    struct ChainContextApplyLookupContext lookup_context = {
1191 1192 1193
      {match_glyph, apply_func},
      {NULL, NULL, NULL}
    };
1194
    return TRACE_RETURN (rule_set.apply (c, lookup_context));
1195
  }
B
Behdad Esfahbod 已提交
1196

B
Behdad Esfahbod 已提交
1197
  inline bool sanitize (hb_sanitize_context_t *c) {
1198
    TRACE_SANITIZE ();
1199
    return TRACE_RETURN (coverage.sanitize (c, this) && ruleSet.sanitize (c, this));
B
Behdad Esfahbod 已提交
1200 1201
  }

1202
  protected:
1203
  USHORT	format;			/* Format identifier--format = 1 */
1204 1205
  OffsetTo<Coverage>
		coverage;		/* Offset to Coverage table--from
1206
					 * beginning of table */
1207 1208 1209
  OffsetArrayOf<ChainRuleSet>
		ruleSet;		/* Array of ChainRuleSet tables
					 * ordered by Coverage Index */
1210
  public:
1211
  DEFINE_SIZE_ARRAY (6, ruleSet);
1212 1213
};

B
Behdad Esfahbod 已提交
1214 1215
struct ChainContextFormat2
{
1216 1217 1218
  friend struct ChainContext;

  private:
1219

B
Behdad Esfahbod 已提交
1220
  inline void closure (hb_closure_context_t *c, closure_lookup_func_t closure_func) const
1221 1222
  {
    TRACE_CLOSURE ();
1223
    if (!(this+coverage).intersects (c->glyphs))
B
Behdad Esfahbod 已提交
1224
      return;
1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240

    const ClassDef &backtrack_class_def = this+backtrackClassDef;
    const ClassDef &input_class_def = this+inputClassDef;
    const ClassDef &lookahead_class_def = this+lookaheadClassDef;

    struct ChainContextClosureLookupContext lookup_context = {
      {intersects_class, closure_func},
      {&backtrack_class_def,
       &input_class_def,
       &lookahead_class_def}
    };

    unsigned int count = ruleSet.len;
    for (unsigned int i = 0; i < count; i++)
      if (input_class_def.intersects_class (c->glyphs, i)) {
	const ChainRuleSet &rule_set = this+ruleSet[i];
B
Behdad Esfahbod 已提交
1241
	rule_set.closure (c, lookup_context);
1242
      }
1243 1244
  }

1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261
  inline bool would_apply (hb_would_apply_context_t *c) const
  {
    TRACE_WOULD_APPLY ();
    unsigned int index = (this+coverage) (c->first);
    if (likely (index == NOT_COVERED)) return TRACE_RETURN (false);

    const ClassDef &input_class_def = this+inputClassDef;

    index = input_class_def (c->first);
    const ChainRuleSet &rule_set = this+ruleSet[index];
    struct ChainContextApplyLookupContext lookup_context = {
      {match_class, NULL},
      {NULL, &input_class_def, NULL}
    };
    return TRACE_RETURN (rule_set.would_apply (c, lookup_context));
  }

B
Behdad Esfahbod 已提交
1262
  inline bool apply (hb_apply_context_t *c, apply_lookup_func_t apply_func) const
B
Behdad Esfahbod 已提交
1263
  {
1264
    TRACE_APPLY ();
1265
    unsigned int index = (this+coverage) (c->buffer->cur().codepoint);
1266
    if (likely (index == NOT_COVERED)) return TRACE_RETURN (false);
1267 1268 1269 1270 1271

    const ClassDef &backtrack_class_def = this+backtrackClassDef;
    const ClassDef &input_class_def = this+inputClassDef;
    const ClassDef &lookahead_class_def = this+lookaheadClassDef;

1272
    index = input_class_def (c->buffer->cur().codepoint);
1273
    const ChainRuleSet &rule_set = this+ruleSet[index];
1274
    struct ChainContextApplyLookupContext lookup_context = {
B
Behdad Esfahbod 已提交
1275 1276 1277 1278
      {match_class, apply_func},
      {&backtrack_class_def,
       &input_class_def,
       &lookahead_class_def}
1279
    };
1280
    return TRACE_RETURN (rule_set.apply (c, lookup_context));
1281 1282
  }

B
Behdad Esfahbod 已提交
1283
  inline bool sanitize (hb_sanitize_context_t *c) {
1284
    TRACE_SANITIZE ();
1285 1286 1287
    return TRACE_RETURN (coverage.sanitize (c, this) && backtrackClassDef.sanitize (c, this) &&
			 inputClassDef.sanitize (c, this) && lookaheadClassDef.sanitize (c, this) &&
			 ruleSet.sanitize (c, this));
B
Behdad Esfahbod 已提交
1288 1289
  }

1290
  protected:
1291
  USHORT	format;			/* Format identifier--format = 2 */
1292 1293
  OffsetTo<Coverage>
		coverage;		/* Offset to Coverage table--from
1294
					 * beginning of table */
1295 1296
  OffsetTo<ClassDef>
		backtrackClassDef;	/* Offset to glyph ClassDef table
1297 1298
					 * containing backtrack sequence
					 * data--from beginning of table */
1299 1300
  OffsetTo<ClassDef>
		inputClassDef;		/* Offset to glyph ClassDef
1301 1302
					 * table containing input sequence
					 * data--from beginning of table */
1303 1304
  OffsetTo<ClassDef>
		lookaheadClassDef;	/* Offset to glyph ClassDef table
1305 1306
					 * containing lookahead sequence
					 * data--from beginning of table */
1307 1308 1309
  OffsetArrayOf<ChainRuleSet>
		ruleSet;		/* Array of ChainRuleSet tables
					 * ordered by class */
1310
  public:
1311
  DEFINE_SIZE_ARRAY (12, ruleSet);
1312 1313
};

B
Behdad Esfahbod 已提交
1314 1315
struct ChainContextFormat3
{
1316 1317 1318
  friend struct ChainContext;

  private:
1319

B
Behdad Esfahbod 已提交
1320
  inline void closure (hb_closure_context_t *c, closure_lookup_func_t closure_func) const
1321 1322
  {
    TRACE_CLOSURE ();
B
Behdad Esfahbod 已提交
1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339
    const OffsetArrayOf<Coverage> &input = StructAfter<OffsetArrayOf<Coverage> > (backtrack);

    if (!(this+input[0]).intersects (c->glyphs))
      return;

    const OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (input);
    const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
    struct ChainContextClosureLookupContext lookup_context = {
      {intersects_coverage, closure_func},
      {this, this, this}
    };
    chain_context_closure_lookup (c,
				  backtrack.len, (const USHORT *) backtrack.array,
				  input.len, (const USHORT *) input.array + 1,
				  lookahead.len, (const USHORT *) lookahead.array,
				  lookup.len, lookup.array,
				  lookup_context);
1340 1341
  }

1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362
  inline bool would_apply (hb_would_apply_context_t *c) const
  {
    TRACE_WOULD_APPLY ();
    const OffsetArrayOf<Coverage> &input = StructAfter<OffsetArrayOf<Coverage> > (backtrack);

    unsigned int index = (this+input[0]) (c->first);
    if (likely (index == NOT_COVERED)) return TRACE_RETURN (false);

    const OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (input);
    const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
    struct ChainContextApplyLookupContext lookup_context = {
      {match_coverage, NULL},
      {this, this, this}
    };
    return TRACE_RETURN (chain_context_would_apply_lookup (c,
							   backtrack.len, (const USHORT *) backtrack.array,
							   input.len, (const USHORT *) input.array + 1,
							   lookahead.len, (const USHORT *) lookahead.array,
							   lookup.len, lookup.array, lookup_context));
  }

B
Behdad Esfahbod 已提交
1363
  inline bool apply (hb_apply_context_t *c, apply_lookup_func_t apply_func) const
B
Behdad Esfahbod 已提交
1364
  {
1365
    TRACE_APPLY ();
1366
    const OffsetArrayOf<Coverage> &input = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
1367

1368
    unsigned int index = (this+input[0]) (c->buffer->cur().codepoint);
1369
    if (likely (index == NOT_COVERED)) return TRACE_RETURN (false);
1370

1371 1372
    const OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (input);
    const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
1373
    struct ChainContextApplyLookupContext lookup_context = {
1374
      {match_coverage, apply_func},
B
Behdad Esfahbod 已提交
1375
      {this, this, this}
1376
    };
1377 1378 1379 1380 1381
    return TRACE_RETURN (chain_context_apply_lookup (c,
						     backtrack.len, (const USHORT *) backtrack.array,
						     input.len, (const USHORT *) input.array + 1,
						     lookahead.len, (const USHORT *) lookahead.array,
						     lookup.len, lookup.array, lookup_context));
1382 1383
  }

B
Behdad Esfahbod 已提交
1384
  inline bool sanitize (hb_sanitize_context_t *c) {
1385
    TRACE_SANITIZE ();
1386
    if (!backtrack.sanitize (c, this)) return TRACE_RETURN (false);
1387
    OffsetArrayOf<Coverage> &input = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
1388
    if (!input.sanitize (c, this)) return TRACE_RETURN (false);
1389
    OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (input);
1390
    if (!lookahead.sanitize (c, this)) return TRACE_RETURN (false);
1391
    ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
1392
    return TRACE_RETURN (lookup.sanitize (c));
B
Behdad Esfahbod 已提交
1393 1394
  }

1395
  protected:
1396
  USHORT	format;			/* Format identifier--format = 3 */
B
Behdad Esfahbod 已提交
1397
  OffsetArrayOf<Coverage>
1398
		backtrack;		/* Array of coverage tables
1399 1400
					 * in backtracking sequence, in  glyph
					 * sequence order */
B
Behdad Esfahbod 已提交
1401
  OffsetArrayOf<Coverage>
1402
		inputX		;	/* Array of coverage
1403 1404
					 * tables in input sequence, in glyph
					 * sequence order */
B
Behdad Esfahbod 已提交
1405
  OffsetArrayOf<Coverage>
1406
		lookaheadX;		/* Array of coverage tables
1407 1408
					 * in lookahead sequence, in glyph
					 * sequence order */
B
Behdad Esfahbod 已提交
1409
  ArrayOf<LookupRecord>
1410
		lookupX;		/* Array of LookupRecords--in
B
Behdad Esfahbod 已提交
1411
					 * design order) */
1412
  public:
B
Behdad Esfahbod 已提交
1413
  DEFINE_SIZE_MIN (10);
1414 1415
};

B
Behdad Esfahbod 已提交
1416 1417
struct ChainContext
{
1418
  protected:
1419

B
Behdad Esfahbod 已提交
1420
  inline void closure (hb_closure_context_t *c, closure_lookup_func_t closure_func) const
1421 1422 1423
  {
    TRACE_CLOSURE ();
    switch (u.format) {
B
Behdad Esfahbod 已提交
1424 1425 1426 1427
    case 1: u.format1.closure (c, closure_func); break;
    case 2: u.format2.closure (c, closure_func); break;
    case 3: u.format3.closure (c, closure_func); break;
    default:                                     break;
1428 1429 1430
    }
  }

1431 1432 1433 1434 1435 1436 1437 1438 1439 1440
  inline bool would_apply (hb_would_apply_context_t *c) const
  {
    switch (u.format) {
    case 1: return u.format1.would_apply (c);
    case 2: return u.format2.would_apply (c);
    case 3: return u.format3.would_apply (c);
    default:return false;
    }
  }

B
Behdad Esfahbod 已提交
1441
  inline bool apply (hb_apply_context_t *c, apply_lookup_func_t apply_func) const
B
Behdad Esfahbod 已提交
1442
  {
1443
    TRACE_APPLY ();
1444
    switch (u.format) {
1445 1446 1447 1448
    case 1: return TRACE_RETURN (u.format1.apply (c, apply_func));
    case 2: return TRACE_RETURN (u.format2.apply (c, apply_func));
    case 3: return TRACE_RETURN (u.format3.apply (c, apply_func));
    default:return TRACE_RETURN (false);
1449 1450 1451
    }
  }

B
Behdad Esfahbod 已提交
1452
  inline bool sanitize (hb_sanitize_context_t *c) {
1453
    TRACE_SANITIZE ();
1454
    if (!u.format.sanitize (c)) return TRACE_RETURN (false);
B
Behdad Esfahbod 已提交
1455
    switch (u.format) {
1456 1457 1458 1459
    case 1: return TRACE_RETURN (u.format1.sanitize (c));
    case 2: return TRACE_RETURN (u.format2.sanitize (c));
    case 3: return TRACE_RETURN (u.format3.sanitize (c));
    default:return TRACE_RETURN (true);
B
Behdad Esfahbod 已提交
1460 1461 1462
    }
  }

1463
  protected:
1464 1465
  union {
  USHORT		format;	/* Format identifier */
B
Behdad Esfahbod 已提交
1466 1467 1468
  ChainContextFormat1	format1;
  ChainContextFormat2	format2;
  ChainContextFormat3	format3;
1469 1470 1471 1472
  } u;
};


1473 1474
struct ExtensionFormat1
{
1475
  friend struct Extension;
1476

B
Behdad Esfahbod 已提交
1477
  protected:
1478
  inline unsigned int get_type (void) const { return extensionLookupType; }
B
Behdad Esfahbod 已提交
1479
  inline unsigned int get_offset (void) const { return extensionOffset; }
1480

B
Behdad Esfahbod 已提交
1481
  inline bool sanitize (hb_sanitize_context_t *c) {
1482
    TRACE_SANITIZE ();
1483
    return TRACE_RETURN (c->check_struct (this));
B
Behdad Esfahbod 已提交
1484 1485
  }

1486
  protected:
1487 1488 1489 1490
  USHORT	format;			/* Format identifier. Set to 1. */
  USHORT	extensionLookupType;	/* Lookup type of subtable referenced
					 * by ExtensionOffset (i.e. the
					 * extension subtable). */
1491 1492
  ULONG		extensionOffset;	/* Offset to the extension subtable,
					 * of lookup type subtable. */
1493 1494
  public:
  DEFINE_SIZE_STATIC (8);
1495 1496 1497 1498 1499 1500 1501
};

struct Extension
{
  inline unsigned int get_type (void) const
  {
    switch (u.format) {
B
Behdad Esfahbod 已提交
1502
    case 1: return u.format1.get_type ();
1503 1504 1505
    default:return 0;
    }
  }
B
Behdad Esfahbod 已提交
1506
  inline unsigned int get_offset (void) const
1507 1508
  {
    switch (u.format) {
B
Behdad Esfahbod 已提交
1509
    case 1: return u.format1.get_offset ();
B
Behdad Esfahbod 已提交
1510
    default:return 0;
1511 1512 1513
    }
  }

B
Behdad Esfahbod 已提交
1514
  inline bool sanitize (hb_sanitize_context_t *c) {
1515
    TRACE_SANITIZE ();
1516
    if (!u.format.sanitize (c)) return TRACE_RETURN (false);
B
Behdad Esfahbod 已提交
1517
    switch (u.format) {
1518 1519
    case 1: return TRACE_RETURN (u.format1.sanitize (c));
    default:return TRACE_RETURN (true);
B
Behdad Esfahbod 已提交
1520 1521 1522
    }
  }

1523
  protected:
1524 1525
  union {
  USHORT		format;		/* Format identifier */
1526
  ExtensionFormat1	format1;
1527 1528 1529 1530
  } u;
};


B
Behdad Esfahbod 已提交
1531 1532 1533 1534
/*
 * GSUB/GPOS Common
 */

B
Behdad Esfahbod 已提交
1535 1536
struct GSUBGPOS
{
B
Behdad Esfahbod 已提交
1537 1538
  static const hb_tag_t GSUBTag	= HB_OT_TAG_GSUB;
  static const hb_tag_t GPOSTag	= HB_OT_TAG_GPOS;
B
Behdad Esfahbod 已提交
1539

1540 1541 1542 1543
  inline unsigned int get_script_count (void) const
  { return (this+scriptList).len; }
  inline const Tag& get_script_tag (unsigned int i) const
  { return (this+scriptList).get_tag (i); }
B
Behdad Esfahbod 已提交
1544 1545 1546 1547
  inline unsigned int get_script_tags (unsigned int start_offset,
				       unsigned int *script_count /* IN/OUT */,
				       hb_tag_t     *script_tags /* OUT */) const
  { return (this+scriptList).get_tags (start_offset, script_count, script_tags); }
1548 1549 1550 1551 1552 1553 1554 1555 1556
  inline const Script& get_script (unsigned int i) const
  { return (this+scriptList)[i]; }
  inline bool find_script_index (hb_tag_t tag, unsigned int *index) const
  { return (this+scriptList).find_index (tag, index); }

  inline unsigned int get_feature_count (void) const
  { return (this+featureList).len; }
  inline const Tag& get_feature_tag (unsigned int i) const
  { return (this+featureList).get_tag (i); }
B
Behdad Esfahbod 已提交
1557 1558 1559 1560
  inline unsigned int get_feature_tags (unsigned int start_offset,
					unsigned int *feature_count /* IN/OUT */,
					hb_tag_t     *feature_tags /* OUT */) const
  { return (this+featureList).get_tags (start_offset, feature_count, feature_tags); }
1561 1562 1563 1564 1565 1566 1567 1568 1569
  inline const Feature& get_feature (unsigned int i) const
  { return (this+featureList)[i]; }
  inline bool find_feature_index (hb_tag_t tag, unsigned int *index) const
  { return (this+featureList).find_index (tag, index); }

  inline unsigned int get_lookup_count (void) const
  { return (this+lookupList).len; }
  inline const Lookup& get_lookup (unsigned int i) const
  { return (this+lookupList)[i]; }
B
Behdad Esfahbod 已提交
1570

B
Behdad Esfahbod 已提交
1571
  inline bool sanitize (hb_sanitize_context_t *c) {
1572
    TRACE_SANITIZE ();
1573 1574 1575 1576
    return TRACE_RETURN (version.sanitize (c) && likely (version.major == 1) &&
			 scriptList.sanitize (c, this) &&
			 featureList.sanitize (c, this) &&
			 lookupList.sanitize (c, this));
B
Behdad Esfahbod 已提交
1577 1578
  }

1579
  protected:
B
Behdad Esfahbod 已提交
1580
  FixedVersion	version;	/* Version of the GSUB/GPOS table--initially set
B
Behdad Esfahbod 已提交
1581 1582 1583 1584 1585 1586 1587
				 * to 0x00010000 */
  OffsetTo<ScriptList>
		scriptList;  	/* ScriptList table */
  OffsetTo<FeatureList>
		featureList; 	/* FeatureList table */
  OffsetTo<LookupList>
		lookupList; 	/* LookupList table */
1588 1589
  public:
  DEFINE_SIZE_STATIC (10);
B
Behdad Esfahbod 已提交
1590
};
1591

1592

B
Behdad Esfahbod 已提交
1593

1594
#endif /* HB_OT_LAYOUT_GSUBGPOS_PRIVATE_HH */