hb-ot-layout-gsubgpos-private.hh 41.4 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);
    }
147 148 149 150
    inline bool reject (void)
    {
      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
    }
196 197 198 199
    inline bool reject (void)
    {
      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

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

316

B
Behdad Esfahbod 已提交
317
static inline bool match_input (hb_apply_context_t *c,
B
Behdad Esfahbod 已提交
318 319 320
				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 已提交
321
				const void *match_data,
322
				unsigned int *end_offset = NULL)
B
Behdad Esfahbod 已提交
323
{
B
Behdad Esfahbod 已提交
324 325
  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 已提交
326
    return false;
B
Behdad Esfahbod 已提交
327

B
Minor  
Behdad Esfahbod 已提交
328
  for (unsigned int i = 1; i < count; i++)
B
Behdad Esfahbod 已提交
329
  {
B
Behdad Esfahbod 已提交
330 331
    if (!skippy_iter.next ())
      return false;
B
Behdad Esfahbod 已提交
332

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

337 338
  if (end_offset)
    *end_offset = skippy_iter.idx - c->buffer->idx + 1;
B
Behdad Esfahbod 已提交
339 340 341 342

  return true;
}

B
Behdad Esfahbod 已提交
343
static inline bool match_backtrack (hb_apply_context_t *c,
344 345 346
				    unsigned int count,
				    const USHORT backtrack[],
				    match_func_t match_func,
B
Behdad Esfahbod 已提交
347
				    const void *match_data)
348
{
B
Minor  
Behdad Esfahbod 已提交
349
  hb_apply_context_t::mark_skipping_backward_iterator_t skippy_iter (c, c->buffer->backtrack_len (), count, true);
B
Behdad Esfahbod 已提交
350 351
  if (skippy_iter.has_no_chance ())
    return false;
352

353
  for (unsigned int i = 0; i < count; i++)
B
Behdad Esfahbod 已提交
354
  {
B
Behdad Esfahbod 已提交
355 356
    if (!skippy_iter.prev ())
      return false;
357

B
Behdad Esfahbod 已提交
358
    if (likely (!match_func (c->buffer->out_info[skippy_iter.idx].codepoint, backtrack[i], match_data)))
359 360 361 362 363 364
      return false;
  }

  return true;
}

B
Behdad Esfahbod 已提交
365
static inline bool match_lookahead (hb_apply_context_t *c,
B
Behdad Esfahbod 已提交
366 367 368
				    unsigned int count,
				    const USHORT lookahead[],
				    match_func_t match_func,
B
Behdad Esfahbod 已提交
369
				    const void *match_data,
B
Behdad Esfahbod 已提交
370
				    unsigned int offset)
371
{
B
Minor  
Behdad Esfahbod 已提交
372
  hb_apply_context_t::mark_skipping_forward_iterator_t skippy_iter (c, c->buffer->idx + offset - 1, count, true);
B
Behdad Esfahbod 已提交
373 374
  if (skippy_iter.has_no_chance ())
    return false;
375

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

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

385 386 387
  return true;
}

B
Behdad Esfahbod 已提交
388

389

B
Behdad Esfahbod 已提交
390 391
struct LookupRecord
{
B
Behdad Esfahbod 已提交
392
  inline bool sanitize (hb_sanitize_context_t *c) {
393
    TRACE_SANITIZE ();
394
    return TRACE_RETURN (c->check_struct (this));
B
Behdad Esfahbod 已提交
395 396
  }

397 398 399 400
  USHORT	sequenceIndex;		/* Index into current glyph
					 * sequence--first glyph = 0 */
  USHORT	lookupListIndex;	/* Lookup to apply to that
					 * position--zero--based */
B
Behdad Esfahbod 已提交
401 402
  public:
  DEFINE_SIZE_STATIC (4);
403 404
};

B
Behdad Esfahbod 已提交
405

B
Behdad Esfahbod 已提交
406
static inline void closure_lookup (hb_closure_context_t *c,
407 408 409 410 411
				   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 已提交
412
    closure_func (c, lookupRecord->lookupListIndex);
413
}
B
Behdad Esfahbod 已提交
414

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

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

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

B
Behdad Esfahbod 已提交
444
    if (lookupCount && i == lookupRecord->sequenceIndex)
445
    {
446
      unsigned int old_pos = c->buffer->idx;
447 448

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

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

      if (!done)
	goto not_applied;
    }
    else
    {
    not_applied:
      /* No lookup applied for this index */
B
Behdad Esfahbod 已提交
465
      c->buffer->next_glyph ();
466 467 468 469 470 471
      i++;
    }
  }

  return true;
}
472

B
Behdad Esfahbod 已提交
473

474 475 476

/* Contextual lookups */

477 478 479 480 481 482 483
struct ContextClosureLookupContext
{
  ContextClosureFuncs funcs;
  const void *intersects_data;
};

struct ContextApplyLookupContext
B
Behdad Esfahbod 已提交
484
{
485
  ContextApplyFuncs funcs;
B
Behdad Esfahbod 已提交
486
  const void *match_data;
487 488
};

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


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)
511
{
B
Behdad Esfahbod 已提交
512
  return match_input (c,
B
Behdad Esfahbod 已提交
513
		      inputCount, input,
514 515
		      lookup_context.funcs.match, lookup_context.match_data)
      && apply_lookup (c,
B
Behdad Esfahbod 已提交
516 517 518
		       inputCount,
		       lookupCount, lookupRecord,
		       lookup_context.funcs.apply);
519 520
}

B
Behdad Esfahbod 已提交
521 522
struct Rule
{
523 524 525
  friend struct RuleSet;

  private:
526

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

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

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

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

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

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

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

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


B
Behdad Esfahbod 已提交
603 604
struct ContextFormat1
{
605 606 607
  friend struct Context;

  private:
608

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

    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 已提交
624
	rule_set.closure (c, lookup_context);
625
      }
626 627
  }

B
Behdad Esfahbod 已提交
628
  inline bool apply (hb_apply_context_t *c, apply_lookup_func_t apply_func) const
B
Behdad Esfahbod 已提交
629
  {
630
    TRACE_APPLY ();
631
    unsigned int index = (this+coverage) (c->buffer->cur().codepoint);
632
    if (likely (index == NOT_COVERED))
633
      return TRACE_RETURN (false);
634

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

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

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


B
Behdad Esfahbod 已提交
661 662
struct ContextFormat2
{
663 664 665
  friend struct Context;

  private:
666

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

    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 已提交
684
	rule_set.closure (c, lookup_context);
685
      }
686 687
  }

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

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

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

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


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

  private:
730

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

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

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

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

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

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

B
Behdad Esfahbod 已提交
787 788
struct Context
{
789
  protected:
790

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

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

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

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

833 834 835

/* Chaining Contextual lookups */

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

struct ChainContextApplyLookupContext
{
  ContextApplyFuncs funcs;
B
Behdad Esfahbod 已提交
845
  const void *match_data[3];
846 847
};

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

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)
883
{
884
  unsigned int lookahead_offset;
B
Behdad Esfahbod 已提交
885
  return match_input (c,
B
Behdad Esfahbod 已提交
886 887
		      inputCount, input,
		      lookup_context.funcs.match, lookup_context.match_data[1],
888
		      &lookahead_offset)
B
Behdad Esfahbod 已提交
889 890 891
      && match_backtrack (c,
			  backtrackCount, backtrack,
			  lookup_context.funcs.match, lookup_context.match_data[0])
B
Behdad Esfahbod 已提交
892
      && match_lookahead (c,
B
Behdad Esfahbod 已提交
893 894
			  lookaheadCount, lookahead,
			  lookup_context.funcs.match, lookup_context.match_data[2],
895 896
			  lookahead_offset)
      && apply_lookup (c,
B
Behdad Esfahbod 已提交
897 898 899
		       inputCount,
		       lookupCount, lookupRecord,
		       lookup_context.funcs.apply);
900
}
901

B
Behdad Esfahbod 已提交
902 903
struct ChainRule
{
904 905 906
  friend struct ChainRuleSet;

  private:
907

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

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

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

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

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

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

983
    return TRACE_RETURN (false);
984
  }
985

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

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

B
Behdad Esfahbod 已提交
999 1000
struct ChainContextFormat1
{
1001 1002 1003
  friend struct ChainContext;

  private:
1004

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

B
Behdad Esfahbod 已提交
1023
  inline bool apply (hb_apply_context_t *c, apply_lookup_func_t apply_func) const
B
Behdad Esfahbod 已提交
1024
  {
1025
    TRACE_APPLY ();
1026
    unsigned int index = (this+coverage) (c->buffer->cur().codepoint);
1027
    if (likely (index == NOT_COVERED)) return TRACE_RETURN (false);
1028

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

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

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

B
Behdad Esfahbod 已提交
1054 1055
struct ChainContextFormat2
{
1056 1057 1058
  friend struct ChainContext;

  private:
1059

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

    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 已提交
1081
	rule_set.closure (c, lookup_context);
1082
      }
1083 1084
  }

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

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

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

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

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

B
Behdad Esfahbod 已提交
1137 1138
struct ChainContextFormat3
{
1139 1140 1141
  friend struct ChainContext;

  private:
1142

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

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

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

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

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

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

B
Behdad Esfahbod 已提交
1218 1219
struct ChainContext
{
1220
  protected:
1221

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

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

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

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


1265 1266 1267 1268
struct ExtensionFormat1
{
  friend struct Extension;

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

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

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

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

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

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


B
Behdad Esfahbod 已提交
1323 1324 1325 1326
/*
 * GSUB/GPOS Common
 */

B
Behdad Esfahbod 已提交
1327 1328
struct GSUBGPOS
{
B
Behdad Esfahbod 已提交
1329 1330
  static const hb_tag_t GSUBTag	= HB_OT_TAG_GSUB;
  static const hb_tag_t GPOSTag	= HB_OT_TAG_GPOS;
B
Behdad Esfahbod 已提交
1331

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

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

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

1384

B
Behdad Esfahbod 已提交
1385

1386
#endif /* HB_OT_LAYOUT_GSUBGPOS_PRIVATE_HH */