hb-ot-layout-gsubgpos-private.hh 41.6 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 42 43 44 45 46 47 48 49 50 51 52 53
/* unique ligature id */
/* component number in the ligature (0 = base) */
static inline void
set_lig_props (hb_glyph_info_t &info, unsigned int lig_id, unsigned int lig_comp)
{
  info.lig_props() = (lig_id << 4) | (lig_comp & 0x0F);
}
static inline unsigned int
get_lig_id (hb_glyph_info_t &info)
{
  return info.lig_props() >> 4;
}
static inline unsigned int
get_lig_comp (hb_glyph_info_t &info)
{
  return info.lig_props() & 0x0F;
}
B
Behdad Esfahbod 已提交
54

55
static inline uint8_t allocate_lig_id (hb_buffer_t *buffer) {
56 57 58
  uint8_t lig_id = buffer->next_serial () & 0x0F;
  if (unlikely (!lig_id))
    lig_id = allocate_lig_id (buffer); /* in case of overflow */
B
Minor  
Behdad Esfahbod 已提交
59 60 61 62
  return lig_id;
}


B
Behdad Esfahbod 已提交
63

64 65 66 67 68
#ifndef HB_DEBUG_CLOSURE
#define HB_DEBUG_CLOSURE (HB_DEBUG+0)
#endif

#define TRACE_CLOSURE() \
69
	hb_auto_trace_t<HB_DEBUG_CLOSURE> trace (&c->debug_depth, "CLOSURE", this, HB_FUNC, "");
70 71


B
Minor  
Behdad Esfahbod 已提交
72 73
/* TODO Add TRACE_RETURN annotation for would_apply */

74 75 76 77

struct hb_closure_context_t
{
  hb_face_t *face;
78
  hb_set_t *glyphs;
79 80 81 82 83
  unsigned int nesting_level_left;
  unsigned int debug_depth;


  hb_closure_context_t (hb_face_t *face_,
84
			hb_set_t *glyphs_,
85 86 87 88 89 90 91 92
		        unsigned int nesting_level_left_ = MAX_NESTING_LEVEL) :
			  face (face_), glyphs (glyphs_),
			  nesting_level_left (nesting_level_left_),
			  debug_depth (0) {}
};



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

B
Behdad Esfahbod 已提交
97
#define TRACE_APPLY() \
98
	hb_auto_trace_t<HB_DEBUG_APPLY> trace (&c->debug_depth, "APPLY", this, HB_FUNC, "idx %d codepoint %u", c->buffer->idx, c->buffer->info[c->buffer->idx].codepoint);
99

B
Behdad Esfahbod 已提交
100

B
Behdad Esfahbod 已提交
101

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

115 116 117 118 119 120 121 122 123 124 125 126

  hb_apply_context_t (hb_font_t *font_,
		      hb_face_t *face_,
		      hb_buffer_t *buffer_,
		      hb_mask_t lookup_mask_,
		      unsigned int context_length_ = NO_CONTEXT,
		      unsigned int nesting_level_left_ = MAX_NESTING_LEVEL) :
			font (font_), face (face_), buffer (buffer_),
			direction (buffer_->props.direction),
			lookup_mask (lookup_mask_),
			context_length (context_length_),
			nesting_level_left (nesting_level_left_),
B
Behdad Esfahbod 已提交
127
			lookup_props (0), property (0), debug_depth (0) {}
128

B
Behdad Esfahbod 已提交
129
  void set_lookup (const Lookup &l) {
130 131 132
    lookup_props = l.get_props ();
  }

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

    unsigned int idx;
    private:
    hb_apply_context_t *c;
    unsigned int num_items;
172
    hb_mask_t mask;
B
Behdad Esfahbod 已提交
173 174 175 176 177 178 179
    unsigned int end;
  };

  struct mark_skipping_backward_iterator_t
  {
    inline mark_skipping_backward_iterator_t (hb_apply_context_t *c_,
					      unsigned int start_index_,
180 181
					      unsigned int num_items_,
					      hb_mask_t mask_ = 0)
B
Behdad Esfahbod 已提交
182 183 184 185
    {
      c = c_;
      idx = start_index_;
      num_items = num_items_;
186
      mask = mask_ ? mask_ : c->lookup_mask;
B
Behdad Esfahbod 已提交
187 188 189
    }
    inline bool has_no_chance (void) const
    {
190
      return unlikely (idx < num_items);
B
Behdad Esfahbod 已提交
191 192 193 194
    }
    inline bool prev (unsigned int *property_out,
		      unsigned int lookup_props)
    {
195
      assert (num_items > 0);
B
Behdad Esfahbod 已提交
196 197 198 199 200 201 202
      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--;
203
      return !!(c->buffer->out_info[idx].mask & mask);
B
Behdad Esfahbod 已提交
204 205 206 207 208 209 210 211 212 213
    }
    inline bool prev (unsigned int *property_out = NULL)
    {
      return prev (property_out, c->lookup_props);
    }

    unsigned int idx;
    private:
    hb_apply_context_t *c;
    unsigned int num_items;
214
    hb_mask_t mask;
B
Behdad Esfahbod 已提交
215 216
  };

217 218 219 220
  inline bool should_mark_skip_current_glyph (void) const
  {
    return _hb_ot_layout_skip_mark (face, &buffer->info[buffer->idx], lookup_props, NULL);
  }
B
Behdad Esfahbod 已提交
221 222 223



224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
  inline void replace_glyph (hb_codepoint_t glyph_index) const
  {
    clear_property ();
    buffer->replace_glyph (glyph_index);
  }
  inline void replace_glyphs_be16 (unsigned int num_in,
				   unsigned int num_out,
				   const uint16_t *glyph_data_be) const
  {
    clear_property ();
    buffer->replace_glyphs_be16 (num_in, num_out, glyph_data_be);
  }

  inline void guess_glyph_class (unsigned int klass)
  {
B
Behdad Esfahbod 已提交
239
    /* XXX if ! has gdef */
240
    buffer->info[buffer->idx].props_cache() = klass;
241 242 243 244 245
  }

  private:
  inline void clear_property (void) const
  {
B
Behdad Esfahbod 已提交
246
    /* XXX if has gdef */
247
    buffer->info[buffer->idx].props_cache() = 0;
248
  }
249 250
};

251

B
Behdad Esfahbod 已提交
252

253
typedef bool (*intersects_func_t) (hb_set_t *glyphs, const USHORT &value, const void *data);
B
Behdad Esfahbod 已提交
254
typedef bool (*match_func_t) (hb_codepoint_t glyph_id, const USHORT &value, const void *data);
B
Behdad Esfahbod 已提交
255
typedef void (*closure_lookup_func_t) (hb_closure_context_t *c, unsigned int lookup_index);
B
Behdad Esfahbod 已提交
256
typedef bool (*apply_lookup_func_t) (hb_apply_context_t *c, unsigned int lookup_index);
257

258 259 260 261 262 263
struct ContextClosureFuncs
{
  intersects_func_t intersects;
  closure_lookup_func_t closure;
};
struct ContextApplyFuncs
B
Behdad Esfahbod 已提交
264
{
265 266
  match_func_t match;
  apply_lookup_func_t apply;
267 268
};

269
static inline bool intersects_glyph (hb_set_t *glyphs, const USHORT &value, const void *data HB_UNUSED)
270 271 272
{
  return glyphs->has (value);
}
273
static inline bool intersects_class (hb_set_t *glyphs, const USHORT &value, const void *data)
274 275 276 277
{
  const ClassDef &class_def = *reinterpret_cast<const ClassDef *>(data);
  return class_def.intersects_class (glyphs, value);
}
278
static inline bool intersects_coverage (hb_set_t *glyphs, const USHORT &value, const void *data)
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
{
  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;
}

296

B
Behdad Esfahbod 已提交
297
static inline bool match_glyph (hb_codepoint_t glyph_id, const USHORT &value, const void *data HB_UNUSED)
B
Behdad Esfahbod 已提交
298
{
299 300
  return glyph_id == value;
}
B
Behdad Esfahbod 已提交
301
static inline bool match_class (hb_codepoint_t glyph_id, const USHORT &value, const void *data)
B
Behdad Esfahbod 已提交
302
{
B
Behdad Esfahbod 已提交
303
  const ClassDef &class_def = *reinterpret_cast<const ClassDef *>(data);
304 305
  return class_def.get_class (glyph_id) == value;
}
B
Behdad Esfahbod 已提交
306
static inline bool match_coverage (hb_codepoint_t glyph_id, const USHORT &value, const void *data)
B
Behdad Esfahbod 已提交
307
{
308
  const OffsetTo<Coverage> &coverage = (const OffsetTo<Coverage>&)value;
309
  return (data+coverage).get_coverage (glyph_id) != NOT_COVERED;
310 311
}

312

B
Behdad Esfahbod 已提交
313
static inline bool match_input (hb_apply_context_t *c,
B
Behdad Esfahbod 已提交
314 315 316
				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 已提交
317
				const void *match_data,
B
Behdad Esfahbod 已提交
318 319
				unsigned int *context_length_out)
{
B
Behdad Esfahbod 已提交
320 321
  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 已提交
322
    return false;
B
Behdad Esfahbod 已提交
323

B
Minor  
Behdad Esfahbod 已提交
324
  for (unsigned int i = 1; i < count; i++)
B
Behdad Esfahbod 已提交
325
  {
B
Behdad Esfahbod 已提交
326 327
    if (!skippy_iter.next ())
      return false;
B
Behdad Esfahbod 已提交
328

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

B
Behdad Esfahbod 已提交
333
  *context_length_out = skippy_iter.idx - c->buffer->idx + 1;
B
Behdad Esfahbod 已提交
334 335 336 337

  return true;
}

B
Behdad Esfahbod 已提交
338
static inline bool match_backtrack (hb_apply_context_t *c,
339 340 341
				    unsigned int count,
				    const USHORT backtrack[],
				    match_func_t match_func,
B
Behdad Esfahbod 已提交
342
				    const void *match_data)
343
{
344
  hb_apply_context_t::mark_skipping_backward_iterator_t skippy_iter (c, c->buffer->backtrack_len (), count, (hb_mask_t) -1);
B
Behdad Esfahbod 已提交
345 346
  if (skippy_iter.has_no_chance ())
    return false;
347

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

B
Behdad Esfahbod 已提交
353
    if (likely (!match_func (c->buffer->out_info[skippy_iter.idx].codepoint, backtrack[i], match_data)))
354 355 356 357 358 359
      return false;
  }

  return true;
}

B
Behdad Esfahbod 已提交
360
static inline bool match_lookahead (hb_apply_context_t *c,
B
Behdad Esfahbod 已提交
361 362 363
				    unsigned int count,
				    const USHORT lookahead[],
				    match_func_t match_func,
B
Behdad Esfahbod 已提交
364
				    const void *match_data,
B
Behdad Esfahbod 已提交
365
				    unsigned int offset)
366
{
367
  hb_apply_context_t::mark_skipping_forward_iterator_t skippy_iter (c, c->buffer->idx + offset - 1, count, (hb_mask_t) -1);
B
Behdad Esfahbod 已提交
368 369
  if (skippy_iter.has_no_chance ())
    return false;
370

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

B
Behdad Esfahbod 已提交
376
    if (likely (!match_func (c->buffer->info[skippy_iter.idx].codepoint, lookahead[i], match_data)))
377 378 379
      return false;
  }

380 381 382
  return true;
}

B
Behdad Esfahbod 已提交
383

384

B
Behdad Esfahbod 已提交
385 386
struct LookupRecord
{
B
Behdad Esfahbod 已提交
387
  inline bool sanitize (hb_sanitize_context_t *c) {
388
    TRACE_SANITIZE ();
389
    return TRACE_RETURN (c->check_struct (this));
B
Behdad Esfahbod 已提交
390 391
  }

392 393 394 395
  USHORT	sequenceIndex;		/* Index into current glyph
					 * sequence--first glyph = 0 */
  USHORT	lookupListIndex;	/* Lookup to apply to that
					 * position--zero--based */
B
Behdad Esfahbod 已提交
396 397
  public:
  DEFINE_SIZE_STATIC (4);
398 399
};

B
Behdad Esfahbod 已提交
400

B
Behdad Esfahbod 已提交
401
static inline void closure_lookup (hb_closure_context_t *c,
402 403 404 405 406
				   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 已提交
407
    closure_func (c, lookupRecord->lookupListIndex);
408
}
B
Behdad Esfahbod 已提交
409

B
Behdad Esfahbod 已提交
410
static inline bool apply_lookup (hb_apply_context_t *c,
411 412
				 unsigned int count, /* Including the first glyph */
				 unsigned int lookupCount,
413 414 415
				 const LookupRecord lookupRecord[], /* Array of LookupRecords--in design order */
				 apply_lookup_func_t apply_func)
{
416 417
  unsigned int end = MIN (c->buffer->len, c->buffer->idx + c->context_length);
  if (unlikely (count == 0 || c->buffer->idx + count > end))
418
    return false;
419

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

423
  /* Note: If sublookup is reverse, it will underflow after the first loop
B
Behdad Esfahbod 已提交
424 425 426
   * and we jump out of it.  Not entirely disastrous.  So we don't check
   * for reverse lookup here.
   */
B
Behdad Esfahbod 已提交
427
  for (unsigned int i = 0; i < count; /* NOP */)
B
Behdad Esfahbod 已提交
428
  {
429 430
    if (unlikely (c->buffer->idx == end))
      return true;
431
    while (c->should_mark_skip_current_glyph ())
B
Behdad Esfahbod 已提交
432
    {
433
      /* No lookup applied for this index */
B
Behdad Esfahbod 已提交
434
      c->buffer->next_glyph ();
435 436
      if (unlikely (c->buffer->idx == end))
	return true;
437 438
    }

B
Behdad Esfahbod 已提交
439
    if (lookupCount && i == lookupRecord->sequenceIndex)
440
    {
441
      unsigned int old_pos = c->buffer->idx;
442 443

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

B
Behdad Esfahbod 已提交
446 447
      lookupRecord++;
      lookupCount--;
B
Behdad Esfahbod 已提交
448
      /* Err, this is wrong if the lookup jumped over some glyphs */
449 450
      i += c->buffer->idx - old_pos;
      if (unlikely (c->buffer->idx == end))
451
	return true;
452 453 454 455 456 457 458 459

      if (!done)
	goto not_applied;
    }
    else
    {
    not_applied:
      /* No lookup applied for this index */
B
Behdad Esfahbod 已提交
460
      c->buffer->next_glyph ();
461 462 463 464 465 466
      i++;
    }
  }

  return true;
}
467

B
Behdad Esfahbod 已提交
468

469 470 471

/* Contextual lookups */

472 473 474 475 476 477 478
struct ContextClosureLookupContext
{
  ContextClosureFuncs funcs;
  const void *intersects_data;
};

struct ContextApplyLookupContext
B
Behdad Esfahbod 已提交
479
{
480
  ContextApplyFuncs funcs;
B
Behdad Esfahbod 已提交
481
  const void *match_data;
482 483
};

B
Behdad Esfahbod 已提交
484
static inline void context_closure_lookup (hb_closure_context_t *c,
485 486 487 488 489 490
					   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 已提交
491 492 493 494 495 496
  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);
497 498 499 500 501 502 503 504 505
}


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)
506
{
B
Behdad Esfahbod 已提交
507 508
  hb_apply_context_t new_context = *c;
  return match_input (c,
B
Behdad Esfahbod 已提交
509 510 511 512 513 514 515
		      inputCount, input,
		      lookup_context.funcs.match, lookup_context.match_data,
		      &new_context.context_length)
      && apply_lookup (&new_context,
		       inputCount,
		       lookupCount, lookupRecord,
		       lookup_context.funcs.apply);
516 517
}

B
Behdad Esfahbod 已提交
518 519
struct Rule
{
520 521 522
  friend struct RuleSet;

  private:
523

B
Behdad Esfahbod 已提交
524
  inline void closure (hb_closure_context_t *c, ContextClosureLookupContext &lookup_context) const
525 526 527
  {
    TRACE_CLOSURE ();
    const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (input, input[0].static_size * (inputCount ? inputCount - 1 : 0));
B
Behdad Esfahbod 已提交
528 529 530 531
    context_closure_lookup (c,
			    inputCount, input,
			    lookupCount, lookupRecord,
			    lookup_context);
532 533 534
  }

  inline bool apply (hb_apply_context_t *c, ContextApplyLookupContext &lookup_context) const
B
Behdad Esfahbod 已提交
535
  {
536
    TRACE_APPLY ();
B
Behdad Esfahbod 已提交
537
    const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (input, input[0].static_size * (inputCount ? inputCount - 1 : 0));
538
    return TRACE_RETURN (context_apply_lookup (c, inputCount, input, lookupCount, lookupRecord, lookup_context));
539 540
  }

B
Behdad Esfahbod 已提交
541
  public:
B
Behdad Esfahbod 已提交
542
  inline bool sanitize (hb_sanitize_context_t *c) {
543
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
544 545 546
    return inputCount.sanitize (c)
	&& lookupCount.sanitize (c)
	&& c->check_range (input,
547 548
			   input[0].static_size * inputCount
			   + lookupRecordX[0].static_size * lookupCount);
B
Behdad Esfahbod 已提交
549 550
  }

551
  private:
552
  USHORT	inputCount;		/* Total number of glyphs in input
553
					 * glyph sequence--includes the first
554
					 * glyph */
555
  USHORT	lookupCount;		/* Number of LookupRecords */
B
Behdad Esfahbod 已提交
556
  USHORT	input[VAR];		/* Array of match inputs--start with
557
					 * second glyph */
B
Behdad Esfahbod 已提交
558
  LookupRecord	lookupRecordX[VAR];	/* Array of LookupRecords--in
559
					 * design order */
B
Behdad Esfahbod 已提交
560
  public:
561
  DEFINE_SIZE_ARRAY2 (4, input, lookupRecordX);
562 563
};

B
Behdad Esfahbod 已提交
564 565
struct RuleSet
{
B
Behdad Esfahbod 已提交
566
  inline void closure (hb_closure_context_t *c, ContextClosureLookupContext &lookup_context) const
567 568 569 570
  {
    TRACE_CLOSURE ();
    unsigned int num_rules = rule.len;
    for (unsigned int i = 0; i < num_rules; i++)
B
Behdad Esfahbod 已提交
571
      (this+rule[i]).closure (c, lookup_context);
572 573 574
  }

  inline bool apply (hb_apply_context_t *c, ContextApplyLookupContext &lookup_context) const
B
Behdad Esfahbod 已提交
575
  {
576
    TRACE_APPLY ();
577
    unsigned int num_rules = rule.len;
B
Behdad Esfahbod 已提交
578 579
    for (unsigned int i = 0; i < num_rules; i++)
    {
B
Behdad Esfahbod 已提交
580
      if ((this+rule[i]).apply (c, lookup_context))
581
        return TRACE_RETURN (true);
582
    }
583
    return TRACE_RETURN (false);
584 585
  }

B
Behdad Esfahbod 已提交
586
  inline bool sanitize (hb_sanitize_context_t *c) {
587
    TRACE_SANITIZE ();
588
    return TRACE_RETURN (rule.sanitize (c, this));
B
Behdad Esfahbod 已提交
589 590
  }

591 592
  private:
  OffsetArrayOf<Rule>
593
		rule;			/* Array of Rule tables
594
					 * ordered by preference */
B
Behdad Esfahbod 已提交
595
  public:
596
  DEFINE_SIZE_ARRAY (2, rule);
597 598 599
};


B
Behdad Esfahbod 已提交
600 601
struct ContextFormat1
{
602 603 604
  friend struct Context;

  private:
605

B
Behdad Esfahbod 已提交
606
  inline void closure (hb_closure_context_t *c, closure_lookup_func_t closure_func) const
607 608
  {
    TRACE_CLOSURE ();
609 610 611 612 613 614 615 616 617 618 619 620

    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 已提交
621
	rule_set.closure (c, lookup_context);
622
      }
623 624
  }

B
Behdad Esfahbod 已提交
625
  inline bool apply (hb_apply_context_t *c, apply_lookup_func_t apply_func) const
B
Behdad Esfahbod 已提交
626
  {
627
    TRACE_APPLY ();
628
    unsigned int index = (this+coverage) (c->buffer->info[c->buffer->idx].codepoint);
629
    if (likely (index == NOT_COVERED))
630
      return TRACE_RETURN (false);
631

632
    const RuleSet &rule_set = this+ruleSet[index];
633
    struct ContextApplyLookupContext lookup_context = {
634 635
      {match_glyph, apply_func},
      NULL
636
    };
637
    return TRACE_RETURN (rule_set.apply (c, lookup_context));
638 639
  }

B
Behdad Esfahbod 已提交
640
  inline bool sanitize (hb_sanitize_context_t *c) {
641
    TRACE_SANITIZE ();
642
    return TRACE_RETURN (coverage.sanitize (c, this) && ruleSet.sanitize (c, this));
B
Behdad Esfahbod 已提交
643 644
  }

645 646 647 648 649 650 651 652
  private:
  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 */
653
  public:
654
  DEFINE_SIZE_ARRAY (6, ruleSet);
655 656 657
};


B
Behdad Esfahbod 已提交
658 659
struct ContextFormat2
{
660 661 662
  friend struct Context;

  private:
663

B
Behdad Esfahbod 已提交
664
  inline void closure (hb_closure_context_t *c, closure_lookup_func_t closure_func) const
665 666
  {
    TRACE_CLOSURE ();
667
    if (!(this+coverage).intersects (c->glyphs))
B
Behdad Esfahbod 已提交
668
      return;
669 670 671 672 673 674 675 676 677 678 679 680

    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 已提交
681
	rule_set.closure (c, lookup_context);
682
      }
683 684
  }

B
Behdad Esfahbod 已提交
685
  inline bool apply (hb_apply_context_t *c, apply_lookup_func_t apply_func) const
B
Behdad Esfahbod 已提交
686
  {
687
    TRACE_APPLY ();
688
    unsigned int index = (this+coverage) (c->buffer->info[c->buffer->idx].codepoint);
689
    if (likely (index == NOT_COVERED)) return TRACE_RETURN (false);
690 691

    const ClassDef &class_def = this+classDef;
692
    index = class_def (c->buffer->info[c->buffer->idx].codepoint);
693
    const RuleSet &rule_set = this+ruleSet[index];
694
    struct ContextApplyLookupContext lookup_context = {
B
Behdad Esfahbod 已提交
695 696
      {match_class, apply_func},
      &class_def
697
    };
698
    return TRACE_RETURN (rule_set.apply (c, lookup_context));
699 700
  }

B
Behdad Esfahbod 已提交
701
  inline bool sanitize (hb_sanitize_context_t *c) {
702
    TRACE_SANITIZE ();
703
    return TRACE_RETURN (coverage.sanitize (c, this) && classDef.sanitize (c, this) && ruleSet.sanitize (c, this));
B
Behdad Esfahbod 已提交
704 705
  }

706 707 708 709 710 711 712 713 714 715 716
  private:
  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 */
717
  public:
718
  DEFINE_SIZE_ARRAY (8, ruleSet);
719 720 721
};


B
Behdad Esfahbod 已提交
722 723
struct ContextFormat3
{
724 725 726
  friend struct Context;

  private:
727

B
Behdad Esfahbod 已提交
728
  inline void closure (hb_closure_context_t *c, closure_lookup_func_t closure_func) const
729 730
  {
    TRACE_CLOSURE ();
731
    if (!(this+coverage[0]).intersects (c->glyphs))
B
Behdad Esfahbod 已提交
732
      return;
733 734 735 736 737 738

    const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (coverage, coverage[0].static_size * glyphCount);
    struct ContextClosureLookupContext lookup_context = {
      {intersects_coverage, closure_func},
      this
    };
B
Behdad Esfahbod 已提交
739 740 741 742
    context_closure_lookup (c,
			    glyphCount, (const USHORT *) (coverage + 1),
			    lookupCount, lookupRecord,
			    lookup_context);
743 744
  }

B
Behdad Esfahbod 已提交
745
  inline bool apply (hb_apply_context_t *c, apply_lookup_func_t apply_func) const
B
Behdad Esfahbod 已提交
746
  {
747
    TRACE_APPLY ();
748
    unsigned int index = (this+coverage[0]) (c->buffer->info[c->buffer->idx].codepoint);
749
    if (likely (index == NOT_COVERED)) return TRACE_RETURN (false);
750

B
Behdad Esfahbod 已提交
751
    const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (coverage, coverage[0].static_size * glyphCount);
752
    struct ContextApplyLookupContext lookup_context = {
753
      {match_coverage, apply_func},
B
Behdad Esfahbod 已提交
754
      this
755
    };
756
    return TRACE_RETURN (context_apply_lookup (c, glyphCount, (const USHORT *) (coverage + 1), lookupCount, lookupRecord, lookup_context));
757 758
  }

B
Behdad Esfahbod 已提交
759
  inline bool sanitize (hb_sanitize_context_t *c) {
760
    TRACE_SANITIZE ();
761
    if (!c->check_struct (this)) return TRACE_RETURN (false);
B
Behdad Esfahbod 已提交
762
    unsigned int count = glyphCount;
763
    if (!c->check_array (coverage, coverage[0].static_size, count)) return TRACE_RETURN (false);
B
Behdad Esfahbod 已提交
764
    for (unsigned int i = 0; i < count; i++)
765
      if (!coverage[i].sanitize (c, this)) return TRACE_RETURN (false);
B
Behdad Esfahbod 已提交
766
    LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (coverage, coverage[0].static_size * count);
767
    return TRACE_RETURN (c->check_array (lookupRecord, lookupRecord[0].static_size, lookupCount));
B
Behdad Esfahbod 已提交
768 769
  }

770 771 772 773
  private:
  USHORT	format;			/* Format identifier--format = 3 */
  USHORT	glyphCount;		/* Number of glyphs in the input glyph
					 * sequence */
774
  USHORT	lookupCount;		/* Number of LookupRecords */
775
  OffsetTo<Coverage>
B
Behdad Esfahbod 已提交
776
		coverage[VAR];		/* Array of offsets to Coverage
777
					 * table in glyph sequence order */
B
Behdad Esfahbod 已提交
778
  LookupRecord	lookupRecordX[VAR];	/* Array of LookupRecords--in
779
					 * design order */
B
Behdad Esfahbod 已提交
780
  public:
781
  DEFINE_SIZE_ARRAY2 (6, coverage, lookupRecordX);
782 783
};

B
Behdad Esfahbod 已提交
784 785
struct Context
{
786
  protected:
787

B
Behdad Esfahbod 已提交
788
  inline void closure (hb_closure_context_t *c, closure_lookup_func_t closure_func) const
789 790 791
  {
    TRACE_CLOSURE ();
    switch (u.format) {
B
Behdad Esfahbod 已提交
792 793 794 795
    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;
796 797 798
    }
  }

B
Behdad Esfahbod 已提交
799
  inline bool apply (hb_apply_context_t *c, apply_lookup_func_t apply_func) const
B
Behdad Esfahbod 已提交
800
  {
801
    TRACE_APPLY ();
802
    switch (u.format) {
803 804 805 806
    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);
807 808 809
    }
  }

B
Behdad Esfahbod 已提交
810
  inline bool sanitize (hb_sanitize_context_t *c) {
811
    TRACE_SANITIZE ();
812
    if (!u.format.sanitize (c)) return TRACE_RETURN (false);
B
Behdad Esfahbod 已提交
813
    switch (u.format) {
814 815 816 817
    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 已提交
818 819 820
    }
  }

821 822
  private:
  union {
B
Behdad Esfahbod 已提交
823
  USHORT		format;		/* Format identifier */
B
Behdad Esfahbod 已提交
824 825 826
  ContextFormat1	format1;
  ContextFormat2	format2;
  ContextFormat3	format3;
827 828 829
  } u;
};

830 831 832

/* Chaining Contextual lookups */

833
struct ChainContextClosureLookupContext
B
Behdad Esfahbod 已提交
834
{
835 836 837 838 839 840 841
  ContextClosureFuncs funcs;
  const void *intersects_data[3];
};

struct ChainContextApplyLookupContext
{
  ContextApplyFuncs funcs;
B
Behdad Esfahbod 已提交
842
  const void *match_data[3];
843 844
};

B
Behdad Esfahbod 已提交
845
static inline void chain_context_closure_lookup (hb_closure_context_t *c,
846 847 848 849 850 851 852 853 854 855
						 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 已提交
856 857 858 859 860 861 862 863 864 865 866 867
  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);
868 869 870 871 872 873 874 875 876 877 878 879
}

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)
880
{
881
  /* First guess */
882
  if (unlikely (c->buffer->backtrack_len () < backtrackCount ||
883
		c->buffer->idx + inputCount + lookaheadCount > c->buffer->len ||
B
Behdad Esfahbod 已提交
884
		inputCount + lookaheadCount > c->context_length))
885 886
    return false;

B
Behdad Esfahbod 已提交
887 888
  hb_apply_context_t new_context = *c;
  return match_backtrack (c,
B
Behdad Esfahbod 已提交
889 890
			  backtrackCount, backtrack,
			  lookup_context.funcs.match, lookup_context.match_data[0])
B
Behdad Esfahbod 已提交
891
      && match_input (c,
B
Behdad Esfahbod 已提交
892 893 894
		      inputCount, input,
		      lookup_context.funcs.match, lookup_context.match_data[1],
		      &new_context.context_length)
B
Behdad Esfahbod 已提交
895
      && match_lookahead (c,
B
Behdad Esfahbod 已提交
896 897 898 899 900 901 902
			  lookaheadCount, lookahead,
			  lookup_context.funcs.match, lookup_context.match_data[2],
			  new_context.context_length)
      && apply_lookup (&new_context,
		       inputCount,
		       lookupCount, lookupRecord,
		       lookup_context.funcs.apply);
903
}
904

B
Behdad Esfahbod 已提交
905 906
struct ChainRule
{
907 908 909
  friend struct ChainRuleSet;

  private:
910

B
Behdad Esfahbod 已提交
911
  inline void closure (hb_closure_context_t *c, ChainContextClosureLookupContext &lookup_context) const
912 913 914 915 916
  {
    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 已提交
917 918 919 920 921 922
    chain_context_closure_lookup (c,
				  backtrack.len, backtrack.array,
				  input.len, input.array,
				  lookahead.len, lookahead.array,
				  lookup.len, lookup.array,
				  lookup_context);
923 924 925
  }

  inline bool apply (hb_apply_context_t *c, ChainContextApplyLookupContext &lookup_context) const
B
Behdad Esfahbod 已提交
926
  {
927
    TRACE_APPLY ();
928 929 930
    const HeadlessArrayOf<USHORT> &input = StructAfter<HeadlessArrayOf<USHORT> > (backtrack);
    const ArrayOf<USHORT> &lookahead = StructAfter<ArrayOf<USHORT> > (input);
    const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
931 932 933 934 935
    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));
936 937
  }

B
Behdad Esfahbod 已提交
938
  public:
B
Behdad Esfahbod 已提交
939
  inline bool sanitize (hb_sanitize_context_t *c) {
940
    TRACE_SANITIZE ();
941
    if (!backtrack.sanitize (c)) return TRACE_RETURN (false);
942
    HeadlessArrayOf<USHORT> &input = StructAfter<HeadlessArrayOf<USHORT> > (backtrack);
943
    if (!input.sanitize (c)) return TRACE_RETURN (false);
944
    ArrayOf<USHORT> &lookahead = StructAfter<ArrayOf<USHORT> > (input);
945
    if (!lookahead.sanitize (c)) return TRACE_RETURN (false);
946
    ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
947
    return TRACE_RETURN (lookup.sanitize (c));
B
Behdad Esfahbod 已提交
948
  }
949 950

  private:
B
Behdad Esfahbod 已提交
951 952
  ArrayOf<USHORT>
		backtrack;		/* Array of backtracking values
953 954
					 * (to be matched before the input
					 * sequence) */
B
Behdad Esfahbod 已提交
955 956
  HeadlessArrayOf<USHORT>
		inputX;			/* Array of input values (start with
957
					 * second glyph) */
B
Behdad Esfahbod 已提交
958 959
  ArrayOf<USHORT>
		lookaheadX;		/* Array of lookahead values's (to be
960
					 * matched after the input sequence) */
B
Behdad Esfahbod 已提交
961
  ArrayOf<LookupRecord>
962
		lookupX;		/* Array of LookupRecords--in
963
					 * design order) */
964
  public:
B
Behdad Esfahbod 已提交
965
  DEFINE_SIZE_MIN (8);
966 967
};

B
Behdad Esfahbod 已提交
968 969
struct ChainRuleSet
{
B
Behdad Esfahbod 已提交
970
  inline void closure (hb_closure_context_t *c, ChainContextClosureLookupContext &lookup_context) const
971 972 973 974
  {
    TRACE_CLOSURE ();
    unsigned int num_rules = rule.len;
    for (unsigned int i = 0; i < num_rules; i++)
B
Behdad Esfahbod 已提交
975
      (this+rule[i]).closure (c, lookup_context);
976 977 978
  }

  inline bool apply (hb_apply_context_t *c, ChainContextApplyLookupContext &lookup_context) const
B
Behdad Esfahbod 已提交
979
  {
980
    TRACE_APPLY ();
981
    unsigned int num_rules = rule.len;
B
Behdad Esfahbod 已提交
982
    for (unsigned int i = 0; i < num_rules; i++)
B
Behdad Esfahbod 已提交
983
      if ((this+rule[i]).apply (c, lookup_context))
984
        return TRACE_RETURN (true);
985

986
    return TRACE_RETURN (false);
987
  }
988

B
Behdad Esfahbod 已提交
989
  inline bool sanitize (hb_sanitize_context_t *c) {
990
    TRACE_SANITIZE ();
991
    return TRACE_RETURN (rule.sanitize (c, this));
B
Behdad Esfahbod 已提交
992 993
  }

994
  private:
995 996 997
  OffsetArrayOf<ChainRule>
		rule;			/* Array of ChainRule tables
					 * ordered by preference */
998
  public:
999
  DEFINE_SIZE_ARRAY (2, rule);
1000 1001
};

B
Behdad Esfahbod 已提交
1002 1003
struct ChainContextFormat1
{
1004 1005 1006
  friend struct ChainContext;

  private:
1007

B
Behdad Esfahbod 已提交
1008
  inline void closure (hb_closure_context_t *c, closure_lookup_func_t closure_func) const
1009 1010
  {
    TRACE_CLOSURE ();
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
    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 已提交
1022
	rule_set.closure (c, lookup_context);
1023
      }
1024 1025
  }

B
Behdad Esfahbod 已提交
1026
  inline bool apply (hb_apply_context_t *c, apply_lookup_func_t apply_func) const
B
Behdad Esfahbod 已提交
1027
  {
1028
    TRACE_APPLY ();
1029
    unsigned int index = (this+coverage) (c->buffer->info[c->buffer->idx].codepoint);
1030
    if (likely (index == NOT_COVERED)) return TRACE_RETURN (false);
1031

1032
    const ChainRuleSet &rule_set = this+ruleSet[index];
1033
    struct ChainContextApplyLookupContext lookup_context = {
1034 1035 1036
      {match_glyph, apply_func},
      {NULL, NULL, NULL}
    };
1037
    return TRACE_RETURN (rule_set.apply (c, lookup_context));
1038
  }
B
Behdad Esfahbod 已提交
1039

B
Behdad Esfahbod 已提交
1040
  inline bool sanitize (hb_sanitize_context_t *c) {
1041
    TRACE_SANITIZE ();
1042
    return TRACE_RETURN (coverage.sanitize (c, this) && ruleSet.sanitize (c, this));
B
Behdad Esfahbod 已提交
1043 1044
  }

1045 1046
  private:
  USHORT	format;			/* Format identifier--format = 1 */
1047 1048
  OffsetTo<Coverage>
		coverage;		/* Offset to Coverage table--from
1049
					 * beginning of table */
1050 1051 1052
  OffsetArrayOf<ChainRuleSet>
		ruleSet;		/* Array of ChainRuleSet tables
					 * ordered by Coverage Index */
1053
  public:
1054
  DEFINE_SIZE_ARRAY (6, ruleSet);
1055 1056
};

B
Behdad Esfahbod 已提交
1057 1058
struct ChainContextFormat2
{
1059 1060 1061
  friend struct ChainContext;

  private:
1062

B
Behdad Esfahbod 已提交
1063
  inline void closure (hb_closure_context_t *c, closure_lookup_func_t closure_func) const
1064 1065
  {
    TRACE_CLOSURE ();
1066
    if (!(this+coverage).intersects (c->glyphs))
B
Behdad Esfahbod 已提交
1067
      return;
1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083

    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 已提交
1084
	rule_set.closure (c, lookup_context);
1085
      }
1086 1087
  }

B
Behdad Esfahbod 已提交
1088
  inline bool apply (hb_apply_context_t *c, apply_lookup_func_t apply_func) const
B
Behdad Esfahbod 已提交
1089
  {
1090
    TRACE_APPLY ();
1091
    unsigned int index = (this+coverage) (c->buffer->info[c->buffer->idx].codepoint);
1092
    if (likely (index == NOT_COVERED)) return TRACE_RETURN (false);
1093 1094 1095 1096 1097

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

1098
    index = input_class_def (c->buffer->info[c->buffer->idx].codepoint);
1099
    const ChainRuleSet &rule_set = this+ruleSet[index];
1100
    struct ChainContextApplyLookupContext lookup_context = {
B
Behdad Esfahbod 已提交
1101 1102 1103 1104
      {match_class, apply_func},
      {&backtrack_class_def,
       &input_class_def,
       &lookahead_class_def}
1105
    };
1106
    return TRACE_RETURN (rule_set.apply (c, lookup_context));
1107 1108
  }

B
Behdad Esfahbod 已提交
1109
  inline bool sanitize (hb_sanitize_context_t *c) {
1110
    TRACE_SANITIZE ();
1111 1112 1113
    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 已提交
1114 1115
  }

1116 1117
  private:
  USHORT	format;			/* Format identifier--format = 2 */
1118 1119
  OffsetTo<Coverage>
		coverage;		/* Offset to Coverage table--from
1120
					 * beginning of table */
1121 1122
  OffsetTo<ClassDef>
		backtrackClassDef;	/* Offset to glyph ClassDef table
1123 1124
					 * containing backtrack sequence
					 * data--from beginning of table */
1125 1126
  OffsetTo<ClassDef>
		inputClassDef;		/* Offset to glyph ClassDef
1127 1128
					 * table containing input sequence
					 * data--from beginning of table */
1129 1130
  OffsetTo<ClassDef>
		lookaheadClassDef;	/* Offset to glyph ClassDef table
1131 1132
					 * containing lookahead sequence
					 * data--from beginning of table */
1133 1134 1135
  OffsetArrayOf<ChainRuleSet>
		ruleSet;		/* Array of ChainRuleSet tables
					 * ordered by class */
1136
  public:
1137
  DEFINE_SIZE_ARRAY (12, ruleSet);
1138 1139
};

B
Behdad Esfahbod 已提交
1140 1141
struct ChainContextFormat3
{
1142 1143 1144
  friend struct ChainContext;

  private:
1145

B
Behdad Esfahbod 已提交
1146
  inline void closure (hb_closure_context_t *c, closure_lookup_func_t closure_func) const
1147 1148
  {
    TRACE_CLOSURE ();
B
Behdad Esfahbod 已提交
1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165
    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);
1166 1167
  }

B
Behdad Esfahbod 已提交
1168
  inline bool apply (hb_apply_context_t *c, apply_lookup_func_t apply_func) const
B
Behdad Esfahbod 已提交
1169
  {
1170
    TRACE_APPLY ();
1171
    const OffsetArrayOf<Coverage> &input = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
1172

1173
    unsigned int index = (this+input[0]) (c->buffer->info[c->buffer->idx].codepoint);
1174
    if (likely (index == NOT_COVERED)) return TRACE_RETURN (false);
1175

1176 1177
    const OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (input);
    const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
1178
    struct ChainContextApplyLookupContext lookup_context = {
1179
      {match_coverage, apply_func},
B
Behdad Esfahbod 已提交
1180
      {this, this, this}
1181
    };
1182 1183 1184 1185 1186
    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));
1187 1188
  }

B
Behdad Esfahbod 已提交
1189
  inline bool sanitize (hb_sanitize_context_t *c) {
1190
    TRACE_SANITIZE ();
1191
    if (!backtrack.sanitize (c, this)) return TRACE_RETURN (false);
1192
    OffsetArrayOf<Coverage> &input = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
1193
    if (!input.sanitize (c, this)) return TRACE_RETURN (false);
1194
    OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (input);
1195
    if (!lookahead.sanitize (c, this)) return TRACE_RETURN (false);
1196
    ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
1197
    return TRACE_RETURN (lookup.sanitize (c));
B
Behdad Esfahbod 已提交
1198 1199
  }

1200 1201
  private:
  USHORT	format;			/* Format identifier--format = 3 */
B
Behdad Esfahbod 已提交
1202
  OffsetArrayOf<Coverage>
1203
		backtrack;		/* Array of coverage tables
1204 1205
					 * in backtracking sequence, in  glyph
					 * sequence order */
B
Behdad Esfahbod 已提交
1206
  OffsetArrayOf<Coverage>
1207
		inputX		;	/* Array of coverage
1208 1209
					 * tables in input sequence, in glyph
					 * sequence order */
B
Behdad Esfahbod 已提交
1210
  OffsetArrayOf<Coverage>
1211
		lookaheadX;		/* Array of coverage tables
1212 1213
					 * in lookahead sequence, in glyph
					 * sequence order */
B
Behdad Esfahbod 已提交
1214
  ArrayOf<LookupRecord>
1215
		lookupX;		/* Array of LookupRecords--in
B
Behdad Esfahbod 已提交
1216
					 * design order) */
1217
  public:
B
Behdad Esfahbod 已提交
1218
  DEFINE_SIZE_MIN (10);
1219 1220
};

B
Behdad Esfahbod 已提交
1221 1222
struct ChainContext
{
1223
  protected:
1224

B
Behdad Esfahbod 已提交
1225
  inline void closure (hb_closure_context_t *c, closure_lookup_func_t closure_func) const
1226 1227 1228
  {
    TRACE_CLOSURE ();
    switch (u.format) {
B
Behdad Esfahbod 已提交
1229 1230 1231 1232
    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;
1233 1234 1235
    }
  }

B
Behdad Esfahbod 已提交
1236
  inline bool apply (hb_apply_context_t *c, apply_lookup_func_t apply_func) const
B
Behdad Esfahbod 已提交
1237
  {
1238
    TRACE_APPLY ();
1239
    switch (u.format) {
1240 1241 1242 1243
    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);
1244 1245 1246
    }
  }

B
Behdad Esfahbod 已提交
1247
  inline bool sanitize (hb_sanitize_context_t *c) {
1248
    TRACE_SANITIZE ();
1249
    if (!u.format.sanitize (c)) return TRACE_RETURN (false);
B
Behdad Esfahbod 已提交
1250
    switch (u.format) {
1251 1252 1253 1254
    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 已提交
1255 1256 1257
    }
  }

1258 1259 1260
  private:
  union {
  USHORT		format;	/* Format identifier */
B
Behdad Esfahbod 已提交
1261 1262 1263
  ChainContextFormat1	format1;
  ChainContextFormat2	format2;
  ChainContextFormat3	format3;
1264 1265 1266 1267
  } u;
};


1268 1269 1270 1271
struct ExtensionFormat1
{
  friend struct Extension;

B
Behdad Esfahbod 已提交
1272
  protected:
1273
  inline unsigned int get_type (void) const { return extensionLookupType; }
B
Behdad Esfahbod 已提交
1274
  inline unsigned int get_offset (void) const { return extensionOffset; }
1275

B
Behdad Esfahbod 已提交
1276
  inline bool sanitize (hb_sanitize_context_t *c) {
1277
    TRACE_SANITIZE ();
1278
    return TRACE_RETURN (c->check_struct (this));
B
Behdad Esfahbod 已提交
1279 1280
  }

1281 1282 1283 1284 1285
  private:
  USHORT	format;			/* Format identifier. Set to 1. */
  USHORT	extensionLookupType;	/* Lookup type of subtable referenced
					 * by ExtensionOffset (i.e. the
					 * extension subtable). */
1286 1287
  ULONG		extensionOffset;	/* Offset to the extension subtable,
					 * of lookup type subtable. */
1288 1289
  public:
  DEFINE_SIZE_STATIC (8);
1290 1291 1292 1293 1294 1295 1296
};

struct Extension
{
  inline unsigned int get_type (void) const
  {
    switch (u.format) {
B
Behdad Esfahbod 已提交
1297
    case 1: return u.format1.get_type ();
1298 1299 1300
    default:return 0;
    }
  }
B
Behdad Esfahbod 已提交
1301
  inline unsigned int get_offset (void) const
1302 1303
  {
    switch (u.format) {
B
Behdad Esfahbod 已提交
1304
    case 1: return u.format1.get_offset ();
B
Behdad Esfahbod 已提交
1305
    default:return 0;
1306 1307 1308
    }
  }

B
Behdad Esfahbod 已提交
1309
  inline bool sanitize (hb_sanitize_context_t *c) {
1310
    TRACE_SANITIZE ();
1311
    if (!u.format.sanitize (c)) return TRACE_RETURN (false);
B
Behdad Esfahbod 已提交
1312
    switch (u.format) {
1313 1314
    case 1: return TRACE_RETURN (u.format1.sanitize (c));
    default:return TRACE_RETURN (true);
B
Behdad Esfahbod 已提交
1315 1316 1317
    }
  }

1318 1319 1320
  private:
  union {
  USHORT		format;		/* Format identifier */
B
Behdad Esfahbod 已提交
1321
  ExtensionFormat1	format1;
1322 1323 1324 1325
  } u;
};


B
Behdad Esfahbod 已提交
1326 1327 1328 1329
/*
 * GSUB/GPOS Common
 */

B
Behdad Esfahbod 已提交
1330 1331
struct GSUBGPOS
{
B
Behdad Esfahbod 已提交
1332 1333
  static const hb_tag_t GSUBTag	= HB_OT_TAG_GSUB;
  static const hb_tag_t GPOSTag	= HB_OT_TAG_GPOS;
B
Behdad Esfahbod 已提交
1334

1335 1336 1337 1338
  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 已提交
1339 1340 1341 1342
  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); }
1343 1344 1345 1346 1347 1348 1349 1350 1351
  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 已提交
1352 1353 1354 1355
  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); }
1356 1357 1358 1359 1360 1361 1362 1363 1364
  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 已提交
1365

B
Behdad Esfahbod 已提交
1366
  inline bool sanitize (hb_sanitize_context_t *c) {
1367
    TRACE_SANITIZE ();
1368 1369 1370 1371
    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 已提交
1372 1373
  }

1374
  protected:
B
Behdad Esfahbod 已提交
1375
  FixedVersion	version;	/* Version of the GSUB/GPOS table--initially set
B
Behdad Esfahbod 已提交
1376 1377 1378 1379 1380 1381 1382
				 * to 0x00010000 */
  OffsetTo<ScriptList>
		scriptList;  	/* ScriptList table */
  OffsetTo<FeatureList>
		featureList; 	/* FeatureList table */
  OffsetTo<LookupList>
		lookupList; 	/* LookupList table */
1383 1384
  public:
  DEFINE_SIZE_STATIC (10);
B
Behdad Esfahbod 已提交
1385
};
1386

1387

B
Behdad Esfahbod 已提交
1388

1389
#endif /* HB_OT_LAYOUT_GSUBGPOS_PRIVATE_HH */