hb-ot-layout-gsubgpos-private.hh 42.3 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->cur().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 nesting_level_left;
110
  unsigned int lookup_props;
111
  unsigned int property; /* propety of first glyph */
112
  unsigned int debug_depth;
113

114 115 116 117

  hb_apply_context_t (hb_font_t *font_,
		      hb_face_t *face_,
		      hb_buffer_t *buffer_,
B
Minor  
Behdad Esfahbod 已提交
118
		      hb_mask_t lookup_mask_) :
119 120 121
			font (font_), face (face_), buffer (buffer_),
			direction (buffer_->props.direction),
			lookup_mask (lookup_mask_),
B
Minor  
Behdad Esfahbod 已提交
122
			nesting_level_left (MAX_NESTING_LEVEL),
B
Behdad Esfahbod 已提交
123
			lookup_props (0), property (0), debug_depth (0) {}
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 152 153
    inline bool next (unsigned int *property_out,
		      unsigned int lookup_props)
    {
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 170 171 172
    }
    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;
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 201 202
    inline bool prev (unsigned int *property_out,
		      unsigned int lookup_props)
    {
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 219 220 221
    }
    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;
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 236 237 238
  inline void output_glyph (hb_codepoint_t glyph_index,
			    unsigned int klass = 0) const
  {
    buffer->cur().props_cache() = klass; /*XXX if has gdef? */
    buffer->output_glyph (glyph_index);
  }
239 240
  inline void replace_glyph (hb_codepoint_t glyph_index,
			     unsigned int klass = 0) const
241
  {
242
    buffer->cur().props_cache() = klass; /*XXX if has gdef? */
243 244
    buffer->replace_glyph (glyph_index);
  }
B
Minor  
Behdad Esfahbod 已提交
245 246 247 248 249 250 251 252
  inline void replace_glyphs (unsigned int num_in,
			      unsigned int num_out,
			      hb_codepoint_t *glyph_data,
			      unsigned int klass = 0) const
  {
    buffer->cur().props_cache() = klass; /* XXX if has gdef? */
    buffer->replace_glyphs (num_in, num_out, glyph_data);
  }
253 254
};

255

B
Behdad Esfahbod 已提交
256

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

262 263 264 265 266 267
struct ContextClosureFuncs
{
  intersects_func_t intersects;
  closure_lookup_func_t closure;
};
struct ContextApplyFuncs
B
Behdad Esfahbod 已提交
268
{
269 270
  match_func_t match;
  apply_lookup_func_t apply;
271 272
};

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

300

301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
struct match_glyph_t {
  inline bool operator() (hb_codepoint_t glyph_id, const USHORT &value, const void *data)
  {
    return glyph_id == value;
  }
};
struct match_class_t {
  inline bool operator() (hb_codepoint_t glyph_id, const USHORT &value, const void *data)
  {
    const ClassDef &class_def = *reinterpret_cast<const ClassDef *>(data);
    return class_def.get_class (glyph_id) == value;
  }
};
struct match_coverage_t {
  inline bool operator() (hb_codepoint_t glyph_id, const USHORT &value, const void *data)
  {
    const OffsetTo<Coverage> &coverage = (const OffsetTo<Coverage>&)value;
    return (data+coverage).get_coverage (glyph_id) != NOT_COVERED;
  }
};

B
Behdad Esfahbod 已提交
322
static inline bool match_glyph (hb_codepoint_t glyph_id, const USHORT &value, const void *data HB_UNUSED)
B
Behdad Esfahbod 已提交
323
{
324 325
  return glyph_id == value;
}
B
Behdad Esfahbod 已提交
326
static inline bool match_class (hb_codepoint_t glyph_id, const USHORT &value, const void *data)
B
Behdad Esfahbod 已提交
327
{
B
Behdad Esfahbod 已提交
328
  const ClassDef &class_def = *reinterpret_cast<const ClassDef *>(data);
329 330
  return class_def.get_class (glyph_id) == value;
}
B
Behdad Esfahbod 已提交
331
static inline bool match_coverage (hb_codepoint_t glyph_id, const USHORT &value, const void *data)
B
Behdad Esfahbod 已提交
332
{
333
  const OffsetTo<Coverage> &coverage = (const OffsetTo<Coverage>&)value;
334
  return (data+coverage).get_coverage (glyph_id) != NOT_COVERED;
335 336
}

337

338
template <typename match_func_t>
B
Behdad Esfahbod 已提交
339
static inline bool match_input (hb_apply_context_t *c,
B
Behdad Esfahbod 已提交
340 341 342
				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 已提交
343
				const void *match_data,
344
				unsigned int *end_offset = NULL)
B
Behdad Esfahbod 已提交
345
{
B
Behdad Esfahbod 已提交
346 347
  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 已提交
348
    return false;
B
Behdad Esfahbod 已提交
349

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

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

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

  return true;
}

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

376
  for (unsigned int i = 0; i < count; i++)
B
Behdad Esfahbod 已提交
377
  {
B
Behdad Esfahbod 已提交
378 379
    if (!skippy_iter.prev ())
      return false;
380

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

  return true;
}

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

B
Minor  
Behdad Esfahbod 已提交
400
  for (unsigned int i = 0; i < count; i++)
B
Behdad Esfahbod 已提交
401
  {
B
Behdad Esfahbod 已提交
402 403
    if (!skippy_iter.next ())
      return false;
404

B
Behdad Esfahbod 已提交
405
    if (likely (!match_func (c->buffer->info[skippy_iter.idx].codepoint, lookahead[i], match_data)))
406 407 408
      return false;
  }

409 410 411
  return true;
}

B
Behdad Esfahbod 已提交
412

413

B
Behdad Esfahbod 已提交
414 415
struct LookupRecord
{
B
Behdad Esfahbod 已提交
416
  inline bool sanitize (hb_sanitize_context_t *c) {
417
    TRACE_SANITIZE ();
418
    return TRACE_RETURN (c->check_struct (this));
B
Behdad Esfahbod 已提交
419 420
  }

421 422 423 424
  USHORT	sequenceIndex;		/* Index into current glyph
					 * sequence--first glyph = 0 */
  USHORT	lookupListIndex;	/* Lookup to apply to that
					 * position--zero--based */
B
Behdad Esfahbod 已提交
425 426
  public:
  DEFINE_SIZE_STATIC (4);
427 428
};

B
Behdad Esfahbod 已提交
429

B
Behdad Esfahbod 已提交
430
static inline void closure_lookup (hb_closure_context_t *c,
431 432 433 434 435
				   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 已提交
436
    closure_func (c, lookupRecord->lookupListIndex);
437
}
B
Behdad Esfahbod 已提交
438

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

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

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

B
Behdad Esfahbod 已提交
468
    if (lookupCount && i == lookupRecord->sequenceIndex)
469
    {
470
      unsigned int old_pos = c->buffer->idx;
471 472

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

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

      if (!done)
	goto not_applied;
    }
    else
    {
    not_applied:
      /* No lookup applied for this index */
B
Behdad Esfahbod 已提交
489
      c->buffer->next_glyph ();
490 491 492 493 494 495
      i++;
    }
  }

  return true;
}
496

B
Behdad Esfahbod 已提交
497

498 499 500

/* Contextual lookups */

501 502 503 504 505 506 507
struct ContextClosureLookupContext
{
  ContextClosureFuncs funcs;
  const void *intersects_data;
};

struct ContextApplyLookupContext
B
Behdad Esfahbod 已提交
508
{
509
  ContextApplyFuncs funcs;
B
Behdad Esfahbod 已提交
510
  const void *match_data;
511 512
};

B
Behdad Esfahbod 已提交
513
static inline void context_closure_lookup (hb_closure_context_t *c,
514 515 516 517 518 519
					   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 已提交
520 521 522 523 524 525
  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);
526 527 528 529 530 531 532 533 534
}


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)
535
{
B
Behdad Esfahbod 已提交
536
  return match_input (c,
B
Behdad Esfahbod 已提交
537
		      inputCount, input,
538 539
		      lookup_context.funcs.match, lookup_context.match_data)
      && apply_lookup (c,
B
Behdad Esfahbod 已提交
540 541 542
		       inputCount,
		       lookupCount, lookupRecord,
		       lookup_context.funcs.apply);
543 544
}

B
Behdad Esfahbod 已提交
545 546
struct Rule
{
547 548 549
  friend struct RuleSet;

  private:
550

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

  inline bool apply (hb_apply_context_t *c, ContextApplyLookupContext &lookup_context) const
B
Behdad Esfahbod 已提交
562
  {
563
    TRACE_APPLY ();
B
Behdad Esfahbod 已提交
564
    const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (input, input[0].static_size * (inputCount ? inputCount - 1 : 0));
565
    return TRACE_RETURN (context_apply_lookup (c, inputCount, input, lookupCount, lookupRecord, lookup_context));
566 567
  }

B
Behdad Esfahbod 已提交
568
  public:
B
Behdad Esfahbod 已提交
569
  inline bool sanitize (hb_sanitize_context_t *c) {
570
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
571 572 573
    return inputCount.sanitize (c)
	&& lookupCount.sanitize (c)
	&& c->check_range (input,
574 575
			   input[0].static_size * inputCount
			   + lookupRecordX[0].static_size * lookupCount);
B
Behdad Esfahbod 已提交
576 577
  }

578
  private:
579
  USHORT	inputCount;		/* Total number of glyphs in input
580
					 * glyph sequence--includes the first
581
					 * glyph */
582
  USHORT	lookupCount;		/* Number of LookupRecords */
B
Behdad Esfahbod 已提交
583
  USHORT	input[VAR];		/* Array of match inputs--start with
584
					 * second glyph */
B
Behdad Esfahbod 已提交
585
  LookupRecord	lookupRecordX[VAR];	/* Array of LookupRecords--in
586
					 * design order */
B
Behdad Esfahbod 已提交
587
  public:
588
  DEFINE_SIZE_ARRAY2 (4, input, lookupRecordX);
589 590
};

B
Behdad Esfahbod 已提交
591 592
struct RuleSet
{
B
Behdad Esfahbod 已提交
593
  inline void closure (hb_closure_context_t *c, ContextClosureLookupContext &lookup_context) const
594 595 596 597
  {
    TRACE_CLOSURE ();
    unsigned int num_rules = rule.len;
    for (unsigned int i = 0; i < num_rules; i++)
B
Behdad Esfahbod 已提交
598
      (this+rule[i]).closure (c, lookup_context);
599 600 601
  }

  inline bool apply (hb_apply_context_t *c, ContextApplyLookupContext &lookup_context) const
B
Behdad Esfahbod 已提交
602
  {
603
    TRACE_APPLY ();
604
    unsigned int num_rules = rule.len;
B
Behdad Esfahbod 已提交
605 606
    for (unsigned int i = 0; i < num_rules; i++)
    {
B
Behdad Esfahbod 已提交
607
      if ((this+rule[i]).apply (c, lookup_context))
608
        return TRACE_RETURN (true);
609
    }
610
    return TRACE_RETURN (false);
611 612
  }

B
Behdad Esfahbod 已提交
613
  inline bool sanitize (hb_sanitize_context_t *c) {
614
    TRACE_SANITIZE ();
615
    return TRACE_RETURN (rule.sanitize (c, this));
B
Behdad Esfahbod 已提交
616 617
  }

618 619
  private:
  OffsetArrayOf<Rule>
620
		rule;			/* Array of Rule tables
621
					 * ordered by preference */
B
Behdad Esfahbod 已提交
622
  public:
623
  DEFINE_SIZE_ARRAY (2, rule);
624 625 626
};


B
Behdad Esfahbod 已提交
627 628
struct ContextFormat1
{
629 630 631
  friend struct Context;

  private:
632

B
Behdad Esfahbod 已提交
633
  inline void closure (hb_closure_context_t *c, closure_lookup_func_t closure_func) const
634 635
  {
    TRACE_CLOSURE ();
636 637 638 639 640 641 642 643 644 645 646 647

    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 已提交
648
	rule_set.closure (c, lookup_context);
649
      }
650 651
  }

B
Behdad Esfahbod 已提交
652
  inline bool apply (hb_apply_context_t *c, apply_lookup_func_t apply_func) const
B
Behdad Esfahbod 已提交
653
  {
654
    TRACE_APPLY ();
655
    unsigned int index = (this+coverage) (c->buffer->cur().codepoint);
656
    if (likely (index == NOT_COVERED))
657
      return TRACE_RETURN (false);
658

659
    const RuleSet &rule_set = this+ruleSet[index];
660
    struct ContextApplyLookupContext lookup_context = {
661 662
      {match_glyph, apply_func},
      NULL
663
    };
664
    return TRACE_RETURN (rule_set.apply (c, lookup_context));
665 666
  }

B
Behdad Esfahbod 已提交
667
  inline bool sanitize (hb_sanitize_context_t *c) {
668
    TRACE_SANITIZE ();
669
    return TRACE_RETURN (coverage.sanitize (c, this) && ruleSet.sanitize (c, this));
B
Behdad Esfahbod 已提交
670 671
  }

672 673 674 675 676 677 678 679
  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 */
680
  public:
681
  DEFINE_SIZE_ARRAY (6, ruleSet);
682 683 684
};


B
Behdad Esfahbod 已提交
685 686
struct ContextFormat2
{
687 688 689
  friend struct Context;

  private:
690

B
Behdad Esfahbod 已提交
691
  inline void closure (hb_closure_context_t *c, closure_lookup_func_t closure_func) const
692 693
  {
    TRACE_CLOSURE ();
694
    if (!(this+coverage).intersects (c->glyphs))
B
Behdad Esfahbod 已提交
695
      return;
696 697 698 699 700 701 702 703 704 705 706 707

    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 已提交
708
	rule_set.closure (c, lookup_context);
709
      }
710 711
  }

B
Behdad Esfahbod 已提交
712
  inline bool apply (hb_apply_context_t *c, apply_lookup_func_t apply_func) const
B
Behdad Esfahbod 已提交
713
  {
714
    TRACE_APPLY ();
715
    unsigned int index = (this+coverage) (c->buffer->cur().codepoint);
716
    if (likely (index == NOT_COVERED)) return TRACE_RETURN (false);
717 718

    const ClassDef &class_def = this+classDef;
719
    index = class_def (c->buffer->cur().codepoint);
720
    const RuleSet &rule_set = this+ruleSet[index];
721
    struct ContextApplyLookupContext lookup_context = {
B
Behdad Esfahbod 已提交
722 723
      {match_class, apply_func},
      &class_def
724
    };
725
    return TRACE_RETURN (rule_set.apply (c, lookup_context));
726 727
  }

B
Behdad Esfahbod 已提交
728
  inline bool sanitize (hb_sanitize_context_t *c) {
729
    TRACE_SANITIZE ();
730
    return TRACE_RETURN (coverage.sanitize (c, this) && classDef.sanitize (c, this) && ruleSet.sanitize (c, this));
B
Behdad Esfahbod 已提交
731 732
  }

733 734 735 736 737 738 739 740 741 742 743
  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 */
744
  public:
745
  DEFINE_SIZE_ARRAY (8, ruleSet);
746 747 748
};


B
Behdad Esfahbod 已提交
749 750
struct ContextFormat3
{
751 752 753
  friend struct Context;

  private:
754

B
Behdad Esfahbod 已提交
755
  inline void closure (hb_closure_context_t *c, closure_lookup_func_t closure_func) const
756 757
  {
    TRACE_CLOSURE ();
758
    if (!(this+coverage[0]).intersects (c->glyphs))
B
Behdad Esfahbod 已提交
759
      return;
760 761 762 763 764 765

    const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (coverage, coverage[0].static_size * glyphCount);
    struct ContextClosureLookupContext lookup_context = {
      {intersects_coverage, closure_func},
      this
    };
B
Behdad Esfahbod 已提交
766 767 768 769
    context_closure_lookup (c,
			    glyphCount, (const USHORT *) (coverage + 1),
			    lookupCount, lookupRecord,
			    lookup_context);
770 771
  }

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

B
Behdad Esfahbod 已提交
778
    const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (coverage, coverage[0].static_size * glyphCount);
779
    struct ContextApplyLookupContext lookup_context = {
780
      {match_coverage, apply_func},
B
Behdad Esfahbod 已提交
781
      this
782
    };
783
    return TRACE_RETURN (context_apply_lookup (c, glyphCount, (const USHORT *) (coverage + 1), lookupCount, lookupRecord, lookup_context));
784 785
  }

B
Behdad Esfahbod 已提交
786
  inline bool sanitize (hb_sanitize_context_t *c) {
787
    TRACE_SANITIZE ();
788
    if (!c->check_struct (this)) return TRACE_RETURN (false);
B
Behdad Esfahbod 已提交
789
    unsigned int count = glyphCount;
790
    if (!c->check_array (coverage, coverage[0].static_size, count)) return TRACE_RETURN (false);
B
Behdad Esfahbod 已提交
791
    for (unsigned int i = 0; i < count; i++)
792
      if (!coverage[i].sanitize (c, this)) return TRACE_RETURN (false);
B
Behdad Esfahbod 已提交
793
    LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (coverage, coverage[0].static_size * count);
794
    return TRACE_RETURN (c->check_array (lookupRecord, lookupRecord[0].static_size, lookupCount));
B
Behdad Esfahbod 已提交
795 796
  }

797 798 799 800
  private:
  USHORT	format;			/* Format identifier--format = 3 */
  USHORT	glyphCount;		/* Number of glyphs in the input glyph
					 * sequence */
801
  USHORT	lookupCount;		/* Number of LookupRecords */
802
  OffsetTo<Coverage>
B
Behdad Esfahbod 已提交
803
		coverage[VAR];		/* Array of offsets to Coverage
804
					 * table in glyph sequence order */
B
Behdad Esfahbod 已提交
805
  LookupRecord	lookupRecordX[VAR];	/* Array of LookupRecords--in
806
					 * design order */
B
Behdad Esfahbod 已提交
807
  public:
808
  DEFINE_SIZE_ARRAY2 (6, coverage, lookupRecordX);
809 810
};

B
Behdad Esfahbod 已提交
811 812
struct Context
{
813
  protected:
814

B
Behdad Esfahbod 已提交
815
  inline void closure (hb_closure_context_t *c, closure_lookup_func_t closure_func) const
816 817 818
  {
    TRACE_CLOSURE ();
    switch (u.format) {
B
Behdad Esfahbod 已提交
819 820 821 822
    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;
823 824 825
    }
  }

B
Behdad Esfahbod 已提交
826
  inline bool apply (hb_apply_context_t *c, apply_lookup_func_t apply_func) const
B
Behdad Esfahbod 已提交
827
  {
828
    TRACE_APPLY ();
829
    switch (u.format) {
830 831 832 833
    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);
834 835 836
    }
  }

B
Behdad Esfahbod 已提交
837
  inline bool sanitize (hb_sanitize_context_t *c) {
838
    TRACE_SANITIZE ();
839
    if (!u.format.sanitize (c)) return TRACE_RETURN (false);
B
Behdad Esfahbod 已提交
840
    switch (u.format) {
841 842 843 844
    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 已提交
845 846 847
    }
  }

848 849
  private:
  union {
B
Behdad Esfahbod 已提交
850
  USHORT		format;		/* Format identifier */
B
Behdad Esfahbod 已提交
851 852 853
  ContextFormat1	format1;
  ContextFormat2	format2;
  ContextFormat3	format3;
854 855 856
  } u;
};

857 858 859

/* Chaining Contextual lookups */

860
struct ChainContextClosureLookupContext
B
Behdad Esfahbod 已提交
861
{
862 863 864 865 866 867 868
  ContextClosureFuncs funcs;
  const void *intersects_data[3];
};

struct ChainContextApplyLookupContext
{
  ContextApplyFuncs funcs;
B
Behdad Esfahbod 已提交
869
  const void *match_data[3];
870 871
};

B
Behdad Esfahbod 已提交
872
static inline void chain_context_closure_lookup (hb_closure_context_t *c,
873 874 875 876 877 878 879 880 881 882
						 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 已提交
883 884 885 886 887 888 889 890 891 892 893 894
  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);
895 896 897 898 899 900 901 902 903 904 905 906
}

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)
907
{
908
  unsigned int lookahead_offset;
B
Behdad Esfahbod 已提交
909
  return match_input (c,
B
Behdad Esfahbod 已提交
910 911
		      inputCount, input,
		      lookup_context.funcs.match, lookup_context.match_data[1],
912
		      &lookahead_offset)
B
Behdad Esfahbod 已提交
913 914 915
      && match_backtrack (c,
			  backtrackCount, backtrack,
			  lookup_context.funcs.match, lookup_context.match_data[0])
B
Behdad Esfahbod 已提交
916
      && match_lookahead (c,
B
Behdad Esfahbod 已提交
917 918
			  lookaheadCount, lookahead,
			  lookup_context.funcs.match, lookup_context.match_data[2],
919 920
			  lookahead_offset)
      && apply_lookup (c,
B
Behdad Esfahbod 已提交
921 922 923
		       inputCount,
		       lookupCount, lookupRecord,
		       lookup_context.funcs.apply);
924
}
925

B
Behdad Esfahbod 已提交
926 927
struct ChainRule
{
928 929 930
  friend struct ChainRuleSet;

  private:
931

B
Behdad Esfahbod 已提交
932
  inline void closure (hb_closure_context_t *c, ChainContextClosureLookupContext &lookup_context) const
933 934 935 936 937
  {
    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 已提交
938 939 940 941 942 943
    chain_context_closure_lookup (c,
				  backtrack.len, backtrack.array,
				  input.len, input.array,
				  lookahead.len, lookahead.array,
				  lookup.len, lookup.array,
				  lookup_context);
944 945 946
  }

  inline bool apply (hb_apply_context_t *c, ChainContextApplyLookupContext &lookup_context) const
B
Behdad Esfahbod 已提交
947
  {
948
    TRACE_APPLY ();
949 950 951
    const HeadlessArrayOf<USHORT> &input = StructAfter<HeadlessArrayOf<USHORT> > (backtrack);
    const ArrayOf<USHORT> &lookahead = StructAfter<ArrayOf<USHORT> > (input);
    const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
952 953 954 955 956
    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));
957 958
  }

B
Behdad Esfahbod 已提交
959
  public:
B
Behdad Esfahbod 已提交
960
  inline bool sanitize (hb_sanitize_context_t *c) {
961
    TRACE_SANITIZE ();
962
    if (!backtrack.sanitize (c)) return TRACE_RETURN (false);
963
    HeadlessArrayOf<USHORT> &input = StructAfter<HeadlessArrayOf<USHORT> > (backtrack);
964
    if (!input.sanitize (c)) return TRACE_RETURN (false);
965
    ArrayOf<USHORT> &lookahead = StructAfter<ArrayOf<USHORT> > (input);
966
    if (!lookahead.sanitize (c)) return TRACE_RETURN (false);
967
    ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
968
    return TRACE_RETURN (lookup.sanitize (c));
B
Behdad Esfahbod 已提交
969
  }
970 971

  private:
B
Behdad Esfahbod 已提交
972 973
  ArrayOf<USHORT>
		backtrack;		/* Array of backtracking values
974 975
					 * (to be matched before the input
					 * sequence) */
B
Behdad Esfahbod 已提交
976 977
  HeadlessArrayOf<USHORT>
		inputX;			/* Array of input values (start with
978
					 * second glyph) */
B
Behdad Esfahbod 已提交
979 980
  ArrayOf<USHORT>
		lookaheadX;		/* Array of lookahead values's (to be
981
					 * matched after the input sequence) */
B
Behdad Esfahbod 已提交
982
  ArrayOf<LookupRecord>
983
		lookupX;		/* Array of LookupRecords--in
984
					 * design order) */
985
  public:
B
Behdad Esfahbod 已提交
986
  DEFINE_SIZE_MIN (8);
987 988
};

B
Behdad Esfahbod 已提交
989 990
struct ChainRuleSet
{
B
Behdad Esfahbod 已提交
991
  inline void closure (hb_closure_context_t *c, ChainContextClosureLookupContext &lookup_context) const
992 993 994 995
  {
    TRACE_CLOSURE ();
    unsigned int num_rules = rule.len;
    for (unsigned int i = 0; i < num_rules; i++)
B
Behdad Esfahbod 已提交
996
      (this+rule[i]).closure (c, lookup_context);
997 998 999
  }

  inline bool apply (hb_apply_context_t *c, ChainContextApplyLookupContext &lookup_context) const
B
Behdad Esfahbod 已提交
1000
  {
1001
    TRACE_APPLY ();
1002
    unsigned int num_rules = rule.len;
B
Behdad Esfahbod 已提交
1003
    for (unsigned int i = 0; i < num_rules; i++)
B
Behdad Esfahbod 已提交
1004
      if ((this+rule[i]).apply (c, lookup_context))
1005
        return TRACE_RETURN (true);
1006

1007
    return TRACE_RETURN (false);
1008
  }
1009

B
Behdad Esfahbod 已提交
1010
  inline bool sanitize (hb_sanitize_context_t *c) {
1011
    TRACE_SANITIZE ();
1012
    return TRACE_RETURN (rule.sanitize (c, this));
B
Behdad Esfahbod 已提交
1013 1014
  }

1015
  private:
1016 1017 1018
  OffsetArrayOf<ChainRule>
		rule;			/* Array of ChainRule tables
					 * ordered by preference */
1019
  public:
1020
  DEFINE_SIZE_ARRAY (2, rule);
1021 1022
};

B
Behdad Esfahbod 已提交
1023 1024
struct ChainContextFormat1
{
1025 1026 1027
  friend struct ChainContext;

  private:
1028

B
Behdad Esfahbod 已提交
1029
  inline void closure (hb_closure_context_t *c, closure_lookup_func_t closure_func) const
1030 1031
  {
    TRACE_CLOSURE ();
1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042
    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 已提交
1043
	rule_set.closure (c, lookup_context);
1044
      }
1045 1046
  }

B
Behdad Esfahbod 已提交
1047
  inline bool apply (hb_apply_context_t *c, apply_lookup_func_t apply_func) const
B
Behdad Esfahbod 已提交
1048
  {
1049
    TRACE_APPLY ();
1050
    unsigned int index = (this+coverage) (c->buffer->cur().codepoint);
1051
    if (likely (index == NOT_COVERED)) return TRACE_RETURN (false);
1052

1053
    const ChainRuleSet &rule_set = this+ruleSet[index];
1054
    struct ChainContextApplyLookupContext lookup_context = {
1055 1056 1057
      {match_glyph, apply_func},
      {NULL, NULL, NULL}
    };
1058
    return TRACE_RETURN (rule_set.apply (c, lookup_context));
1059
  }
B
Behdad Esfahbod 已提交
1060

B
Behdad Esfahbod 已提交
1061
  inline bool sanitize (hb_sanitize_context_t *c) {
1062
    TRACE_SANITIZE ();
1063
    return TRACE_RETURN (coverage.sanitize (c, this) && ruleSet.sanitize (c, this));
B
Behdad Esfahbod 已提交
1064 1065
  }

1066 1067
  private:
  USHORT	format;			/* Format identifier--format = 1 */
1068 1069
  OffsetTo<Coverage>
		coverage;		/* Offset to Coverage table--from
1070
					 * beginning of table */
1071 1072 1073
  OffsetArrayOf<ChainRuleSet>
		ruleSet;		/* Array of ChainRuleSet tables
					 * ordered by Coverage Index */
1074
  public:
1075
  DEFINE_SIZE_ARRAY (6, ruleSet);
1076 1077
};

B
Behdad Esfahbod 已提交
1078 1079
struct ChainContextFormat2
{
1080 1081 1082
  friend struct ChainContext;

  private:
1083

B
Behdad Esfahbod 已提交
1084
  inline void closure (hb_closure_context_t *c, closure_lookup_func_t closure_func) const
1085 1086
  {
    TRACE_CLOSURE ();
1087
    if (!(this+coverage).intersects (c->glyphs))
B
Behdad Esfahbod 已提交
1088
      return;
1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104

    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 已提交
1105
	rule_set.closure (c, lookup_context);
1106
      }
1107 1108
  }

B
Behdad Esfahbod 已提交
1109
  inline bool apply (hb_apply_context_t *c, apply_lookup_func_t apply_func) const
B
Behdad Esfahbod 已提交
1110
  {
1111
    TRACE_APPLY ();
1112
    unsigned int index = (this+coverage) (c->buffer->cur().codepoint);
1113
    if (likely (index == NOT_COVERED)) return TRACE_RETURN (false);
1114 1115 1116 1117 1118

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

1119
    index = input_class_def (c->buffer->cur().codepoint);
1120
    const ChainRuleSet &rule_set = this+ruleSet[index];
1121
    struct ChainContextApplyLookupContext lookup_context = {
B
Behdad Esfahbod 已提交
1122 1123 1124 1125
      {match_class, apply_func},
      {&backtrack_class_def,
       &input_class_def,
       &lookahead_class_def}
1126
    };
1127
    return TRACE_RETURN (rule_set.apply (c, lookup_context));
1128 1129
  }

B
Behdad Esfahbod 已提交
1130
  inline bool sanitize (hb_sanitize_context_t *c) {
1131
    TRACE_SANITIZE ();
1132 1133 1134
    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 已提交
1135 1136
  }

1137 1138
  private:
  USHORT	format;			/* Format identifier--format = 2 */
1139 1140
  OffsetTo<Coverage>
		coverage;		/* Offset to Coverage table--from
1141
					 * beginning of table */
1142 1143
  OffsetTo<ClassDef>
		backtrackClassDef;	/* Offset to glyph ClassDef table
1144 1145
					 * containing backtrack sequence
					 * data--from beginning of table */
1146 1147
  OffsetTo<ClassDef>
		inputClassDef;		/* Offset to glyph ClassDef
1148 1149
					 * table containing input sequence
					 * data--from beginning of table */
1150 1151
  OffsetTo<ClassDef>
		lookaheadClassDef;	/* Offset to glyph ClassDef table
1152 1153
					 * containing lookahead sequence
					 * data--from beginning of table */
1154 1155 1156
  OffsetArrayOf<ChainRuleSet>
		ruleSet;		/* Array of ChainRuleSet tables
					 * ordered by class */
1157
  public:
1158
  DEFINE_SIZE_ARRAY (12, ruleSet);
1159 1160
};

B
Behdad Esfahbod 已提交
1161 1162
struct ChainContextFormat3
{
1163 1164 1165
  friend struct ChainContext;

  private:
1166

B
Behdad Esfahbod 已提交
1167
  inline void closure (hb_closure_context_t *c, closure_lookup_func_t closure_func) const
1168 1169
  {
    TRACE_CLOSURE ();
B
Behdad Esfahbod 已提交
1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186
    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);
1187 1188
  }

B
Behdad Esfahbod 已提交
1189
  inline bool apply (hb_apply_context_t *c, apply_lookup_func_t apply_func) const
B
Behdad Esfahbod 已提交
1190
  {
1191
    TRACE_APPLY ();
1192
    const OffsetArrayOf<Coverage> &input = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
1193

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

1197 1198
    const OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (input);
    const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
1199
    struct ChainContextApplyLookupContext lookup_context = {
1200
      {match_coverage, apply_func},
B
Behdad Esfahbod 已提交
1201
      {this, this, this}
1202
    };
1203 1204 1205 1206 1207
    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));
1208 1209
  }

B
Behdad Esfahbod 已提交
1210
  inline bool sanitize (hb_sanitize_context_t *c) {
1211
    TRACE_SANITIZE ();
1212
    if (!backtrack.sanitize (c, this)) return TRACE_RETURN (false);
1213
    OffsetArrayOf<Coverage> &input = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
1214
    if (!input.sanitize (c, this)) return TRACE_RETURN (false);
1215
    OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (input);
1216
    if (!lookahead.sanitize (c, this)) return TRACE_RETURN (false);
1217
    ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
1218
    return TRACE_RETURN (lookup.sanitize (c));
B
Behdad Esfahbod 已提交
1219 1220
  }

1221 1222
  private:
  USHORT	format;			/* Format identifier--format = 3 */
B
Behdad Esfahbod 已提交
1223
  OffsetArrayOf<Coverage>
1224
		backtrack;		/* Array of coverage tables
1225 1226
					 * in backtracking sequence, in  glyph
					 * sequence order */
B
Behdad Esfahbod 已提交
1227
  OffsetArrayOf<Coverage>
1228
		inputX		;	/* Array of coverage
1229 1230
					 * tables in input sequence, in glyph
					 * sequence order */
B
Behdad Esfahbod 已提交
1231
  OffsetArrayOf<Coverage>
1232
		lookaheadX;		/* Array of coverage tables
1233 1234
					 * in lookahead sequence, in glyph
					 * sequence order */
B
Behdad Esfahbod 已提交
1235
  ArrayOf<LookupRecord>
1236
		lookupX;		/* Array of LookupRecords--in
B
Behdad Esfahbod 已提交
1237
					 * design order) */
1238
  public:
B
Behdad Esfahbod 已提交
1239
  DEFINE_SIZE_MIN (10);
1240 1241
};

B
Behdad Esfahbod 已提交
1242 1243
struct ChainContext
{
1244
  protected:
1245

B
Behdad Esfahbod 已提交
1246
  inline void closure (hb_closure_context_t *c, closure_lookup_func_t closure_func) const
1247 1248 1249
  {
    TRACE_CLOSURE ();
    switch (u.format) {
B
Behdad Esfahbod 已提交
1250 1251 1252 1253
    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;
1254 1255 1256
    }
  }

B
Behdad Esfahbod 已提交
1257
  inline bool apply (hb_apply_context_t *c, apply_lookup_func_t apply_func) const
B
Behdad Esfahbod 已提交
1258
  {
1259
    TRACE_APPLY ();
1260
    switch (u.format) {
1261 1262 1263 1264
    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);
1265 1266 1267
    }
  }

B
Behdad Esfahbod 已提交
1268
  inline bool sanitize (hb_sanitize_context_t *c) {
1269
    TRACE_SANITIZE ();
1270
    if (!u.format.sanitize (c)) return TRACE_RETURN (false);
B
Behdad Esfahbod 已提交
1271
    switch (u.format) {
1272 1273 1274 1275
    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 已提交
1276 1277 1278
    }
  }

1279 1280 1281
  private:
  union {
  USHORT		format;	/* Format identifier */
B
Behdad Esfahbod 已提交
1282 1283 1284
  ChainContextFormat1	format1;
  ChainContextFormat2	format2;
  ChainContextFormat3	format3;
1285 1286 1287 1288
  } u;
};


1289 1290 1291
template <typename Table> struct Extension;

template <typename Table>
1292 1293
struct ExtensionFormat1
{
1294
  friend struct Extension<Table>;
1295

B
Behdad Esfahbod 已提交
1296
  protected:
1297
  inline unsigned int get_type (void) const { return extensionLookupType; }
B
Behdad Esfahbod 已提交
1298
  inline unsigned int get_offset (void) const { return extensionOffset; }
1299

B
Behdad Esfahbod 已提交
1300
  inline bool sanitize (hb_sanitize_context_t *c) {
1301
    TRACE_SANITIZE ();
1302
    return TRACE_RETURN (c->check_struct (this));
B
Behdad Esfahbod 已提交
1303 1304
  }

1305 1306 1307 1308 1309
  private:
  USHORT	format;			/* Format identifier. Set to 1. */
  USHORT	extensionLookupType;	/* Lookup type of subtable referenced
					 * by ExtensionOffset (i.e. the
					 * extension subtable). */
1310 1311
  ULONG		extensionOffset;	/* Offset to the extension subtable,
					 * of lookup type subtable. */
1312 1313
  public:
  DEFINE_SIZE_STATIC (8);
1314 1315
};

1316
template <typename Table>
1317 1318 1319 1320 1321
struct Extension
{
  inline unsigned int get_type (void) const
  {
    switch (u.format) {
B
Behdad Esfahbod 已提交
1322
    case 1: return u.format1.get_type ();
1323 1324 1325
    default:return 0;
    }
  }
B
Behdad Esfahbod 已提交
1326
  inline unsigned int get_offset (void) const
1327 1328
  {
    switch (u.format) {
B
Behdad Esfahbod 已提交
1329
    case 1: return u.format1.get_offset ();
B
Behdad Esfahbod 已提交
1330
    default:return 0;
1331 1332 1333
    }
  }

B
Behdad Esfahbod 已提交
1334
  inline bool sanitize (hb_sanitize_context_t *c) {
1335
    TRACE_SANITIZE ();
1336
    if (!u.format.sanitize (c)) return TRACE_RETURN (false);
B
Behdad Esfahbod 已提交
1337
    switch (u.format) {
1338 1339
    case 1: return TRACE_RETURN (u.format1.sanitize (c));
    default:return TRACE_RETURN (true);
B
Behdad Esfahbod 已提交
1340 1341 1342
    }
  }

1343 1344 1345
  private:
  union {
  USHORT		format;		/* Format identifier */
1346 1347
  ExtensionFormat1<Table>
			format1;
1348 1349 1350 1351
  } u;
};


B
Behdad Esfahbod 已提交
1352 1353 1354 1355
/*
 * GSUB/GPOS Common
 */

B
Behdad Esfahbod 已提交
1356 1357
struct GSUBGPOS
{
B
Behdad Esfahbod 已提交
1358 1359
  static const hb_tag_t GSUBTag	= HB_OT_TAG_GSUB;
  static const hb_tag_t GPOSTag	= HB_OT_TAG_GPOS;
B
Behdad Esfahbod 已提交
1360

1361 1362 1363 1364
  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 已提交
1365 1366 1367 1368
  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); }
1369 1370 1371 1372 1373 1374 1375 1376 1377
  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 已提交
1378 1379 1380 1381
  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); }
1382 1383 1384 1385 1386 1387 1388 1389 1390
  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 已提交
1391

B
Behdad Esfahbod 已提交
1392
  inline bool sanitize (hb_sanitize_context_t *c) {
1393
    TRACE_SANITIZE ();
1394 1395 1396 1397
    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 已提交
1398 1399
  }

1400
  protected:
B
Behdad Esfahbod 已提交
1401
  FixedVersion	version;	/* Version of the GSUB/GPOS table--initially set
B
Behdad Esfahbod 已提交
1402 1403 1404 1405 1406 1407 1408
				 * to 0x00010000 */
  OffsetTo<ScriptList>
		scriptList;  	/* ScriptList table */
  OffsetTo<FeatureList>
		featureList; 	/* FeatureList table */
  OffsetTo<LookupList>
		lookupList; 	/* LookupList table */
1409 1410
  public:
  DEFINE_SIZE_STATIC (10);
B
Behdad Esfahbod 已提交
1411
};
1412

1413

B
Behdad Esfahbod 已提交
1414

1415
#endif /* HB_OT_LAYOUT_GSUBGPOS_PRIVATE_HH */