hb-ot-layout-gsubgpos-private.hh 27.5 KB
Newer Older
1
/*
B
Behdad Esfahbod 已提交
2 3
 * Copyright © 2007,2008,2009,2010  Red Hat, Inc.
 * Copyright © 2010  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-private.hh"
34

B
Behdad Esfahbod 已提交
35 36
HB_BEGIN_DECLS

37

B
Behdad Esfahbod 已提交
38 39 40 41 42
/* buffer var allocations */
#define lig_id() var2.u16[0] /* unique ligature id */
#define lig_comp() var2.u16[1] /* component number in the ligature (0 = base) */


B
Behdad Esfahbod 已提交
43
#ifndef HB_DEBUG_APPLY
44
#define HB_DEBUG_APPLY (HB_DEBUG+0)
B
Behdad Esfahbod 已提交
45 46
#endif

B
Behdad Esfahbod 已提交
47
#define TRACE_APPLY() \
B
Behdad Esfahbod 已提交
48
	hb_trace_t<HB_DEBUG_APPLY> trace (&c->debug_depth, "APPLY", HB_FUNC, this); \
49

B
Behdad Esfahbod 已提交
50

B
Behdad Esfahbod 已提交
51 52
HB_BEGIN_DECLS

53 54
struct hb_apply_context_t
{
B
Minor  
Behdad Esfahbod 已提交
55
  unsigned int debug_depth;
56
  hb_ot_layout_context_t *layout;
B
Behdad Esfahbod 已提交
57
  hb_buffer_t *buffer;
B
Behdad Esfahbod 已提交
58
  hb_mask_t lookup_mask;
59
  unsigned int context_length;
60
  unsigned int nesting_level_left;
61
  unsigned int lookup_props;
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
  unsigned int property; /* propety of first glyph */


  inline void replace_glyph (hb_codepoint_t glyph_index) const
  {
    clear_property ();
    buffer->replace_glyph (glyph_index);
  }
  inline void replace_glyphs_be16 (unsigned int num_in,
				   unsigned int num_out,
				   const uint16_t *glyph_data_be) const
  {
    clear_property ();
    buffer->replace_glyphs_be16 (num_in, num_out, glyph_data_be);
  }

  inline void guess_glyph_class (unsigned int klass)
  {
B
Behdad Esfahbod 已提交
80 81
    /* XXX if ! has gdef */
    buffer->info[buffer->i].props_cache() = klass;
82 83 84 85 86
  }

  private:
  inline void clear_property (void) const
  {
B
Behdad Esfahbod 已提交
87
    /* XXX if has gdef */
B
Behdad Esfahbod 已提交
88
    buffer->info[buffer->i].props_cache() = 0;
89
  }
90 91
};

92

B
Behdad Esfahbod 已提交
93

B
Behdad Esfahbod 已提交
94
typedef bool (*match_func_t) (hb_codepoint_t glyph_id, const USHORT &value, const void *data);
B
Behdad Esfahbod 已提交
95
typedef bool (*apply_lookup_func_t) (hb_apply_context_t *c, unsigned int lookup_index);
96

B
Behdad Esfahbod 已提交
97 98
struct ContextFuncs
{
99 100
  match_func_t match;
  apply_lookup_func_t apply;
101 102
};

103

B
Behdad Esfahbod 已提交
104
static inline bool match_glyph (hb_codepoint_t glyph_id, const USHORT &value, const void *data HB_UNUSED)
B
Behdad Esfahbod 已提交
105
{
106 107
  return glyph_id == value;
}
108

B
Behdad Esfahbod 已提交
109
static inline bool match_class (hb_codepoint_t glyph_id, const USHORT &value, const void *data)
B
Behdad Esfahbod 已提交
110
{
B
Behdad Esfahbod 已提交
111
  const ClassDef &class_def = *reinterpret_cast<const ClassDef *>(data);
112 113 114
  return class_def.get_class (glyph_id) == value;
}

B
Behdad Esfahbod 已提交
115
static inline bool match_coverage (hb_codepoint_t glyph_id, const USHORT &value, const void *data)
B
Behdad Esfahbod 已提交
116
{
117
  const OffsetTo<Coverage> &coverage = (const OffsetTo<Coverage>&)value;
118 119 120
  return (data+coverage) (glyph_id) != NOT_COVERED;
}

121

B
Behdad Esfahbod 已提交
122
static inline bool match_input (hb_apply_context_t *c,
B
Behdad Esfahbod 已提交
123 124 125
				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 已提交
126
				const void *match_data,
B
Behdad Esfahbod 已提交
127 128 129
				unsigned int *context_length_out)
{
  unsigned int i, j;
130 131
  unsigned int end = MIN (c->buffer->len, c->buffer->i + c->context_length);
  if (unlikely (c->buffer->i + count > end))
B
Behdad Esfahbod 已提交
132
    return false;
B
Behdad Esfahbod 已提交
133

134
  for (i = 1, j = c->buffer->i + 1; i < count; i++, j++)
B
Behdad Esfahbod 已提交
135
  {
136
    while (_hb_ot_layout_skip_mark (c->layout->face, &c->buffer->info[j], c->lookup_props, NULL))
B
Behdad Esfahbod 已提交
137
    {
138
      if (unlikely (j + count - i == end))
B
Behdad Esfahbod 已提交
139 140 141 142
	return false;
      j++;
    }

143
    if (likely (!match_func (c->buffer->info[j].codepoint, input[i - 1], match_data)))
B
Behdad Esfahbod 已提交
144 145 146
      return false;
  }

147
  *context_length_out = j - c->buffer->i;
B
Behdad Esfahbod 已提交
148 149 150 151

  return true;
}

B
Behdad Esfahbod 已提交
152
static inline bool match_backtrack (hb_apply_context_t *c,
153 154 155
				    unsigned int count,
				    const USHORT backtrack[],
				    match_func_t match_func,
B
Behdad Esfahbod 已提交
156
				    const void *match_data)
157
{
158
  if (unlikely (c->buffer->backtrack_len () < count))
B
Behdad Esfahbod 已提交
159
    return false;
160

161
  for (unsigned int i = 0, j = c->buffer->backtrack_len () - 1; i < count; i++, j--)
B
Behdad Esfahbod 已提交
162
  {
163
    while (_hb_ot_layout_skip_mark (c->layout->face, &c->buffer->out_info[j], c->lookup_props, NULL))
B
Behdad Esfahbod 已提交
164
    {
165
      if (unlikely (j + 1 == count - i))
166 167 168 169
	return false;
      j--;
    }

170
    if (likely (!match_func (c->buffer->out_info[j].codepoint, backtrack[i], match_data)))
171 172 173 174 175 176
      return false;
  }

  return true;
}

B
Behdad Esfahbod 已提交
177
static inline bool match_lookahead (hb_apply_context_t *c,
B
Behdad Esfahbod 已提交
178 179 180
				    unsigned int count,
				    const USHORT lookahead[],
				    match_func_t match_func,
B
Behdad Esfahbod 已提交
181
				    const void *match_data,
B
Behdad Esfahbod 已提交
182
				    unsigned int offset)
183
{
184
  unsigned int i, j;
185 186
  unsigned int end = MIN (c->buffer->len, c->buffer->i + c->context_length);
  if (unlikely (c->buffer->i + offset + count > end))
B
Behdad Esfahbod 已提交
187
    return false;
188

189
  for (i = 0, j = c->buffer->i + offset; i < count; i++, j++)
B
Behdad Esfahbod 已提交
190
  {
191
    while (_hb_ot_layout_skip_mark (c->layout->face, &c->buffer->info[j], c->lookup_props, NULL))
B
Behdad Esfahbod 已提交
192
    {
193
      if (unlikely (j + count - i == end))
194 195 196 197
	return false;
      j++;
    }

198
    if (likely (!match_func (c->buffer->info[j].codepoint, lookahead[i], match_data)))
199 200 201
      return false;
  }

202 203 204
  return true;
}

B
Behdad Esfahbod 已提交
205 206
HB_END_DECLS

207

B
Behdad Esfahbod 已提交
208 209
struct LookupRecord
{
B
Behdad Esfahbod 已提交
210
  inline bool sanitize (hb_sanitize_context_t *c) {
211
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
212
    return c->check_struct (this);
B
Behdad Esfahbod 已提交
213 214
  }

215 216 217 218
  USHORT	sequenceIndex;		/* Index into current glyph
					 * sequence--first glyph = 0 */
  USHORT	lookupListIndex;	/* Lookup to apply to that
					 * position--zero--based */
B
Behdad Esfahbod 已提交
219 220
  public:
  DEFINE_SIZE_STATIC (4);
221 222
};

B
Behdad Esfahbod 已提交
223 224 225

HB_BEGIN_DECLS

B
Behdad Esfahbod 已提交
226
static inline bool apply_lookup (hb_apply_context_t *c,
227 228
				 unsigned int count, /* Including the first glyph */
				 unsigned int lookupCount,
229 230 231
				 const LookupRecord lookupRecord[], /* Array of LookupRecords--in design order */
				 apply_lookup_func_t apply_func)
{
232
  unsigned int end = MIN (c->buffer->len, c->buffer->i + c->context_length);
233
  if (unlikely (count == 0 || c->buffer->i + count > end))
234
    return false;
235

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

239
  /* Note: If sublookup is reverse, it will underflow after the first loop
B
Behdad Esfahbod 已提交
240 241 242
   * and we jump out of it.  Not entirely disastrous.  So we don't check
   * for reverse lookup here.
   */
B
Behdad Esfahbod 已提交
243
  for (unsigned int i = 0; i < count; /* NOP */)
B
Behdad Esfahbod 已提交
244
  {
245
    while (_hb_ot_layout_skip_mark (c->layout->face, &c->buffer->info[c->buffer->i], c->lookup_props, NULL))
B
Behdad Esfahbod 已提交
246
    {
247
      if (unlikely (c->buffer->i == end))
248 249
	return true;
      /* No lookup applied for this index */
B
Behdad Esfahbod 已提交
250
      c->buffer->next_glyph ();
251 252
    }

B
Behdad Esfahbod 已提交
253
    if (lookupCount && i == lookupRecord->sequenceIndex)
254
    {
255
      unsigned int old_pos = c->buffer->i;
256 257

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

B
Behdad Esfahbod 已提交
260 261
      lookupRecord++;
      lookupCount--;
B
Behdad Esfahbod 已提交
262
      /* Err, this is wrong if the lookup jumped over some glyphs */
263 264
      i += c->buffer->i - old_pos;
      if (unlikely (c->buffer->i == end))
265
	return true;
266 267 268 269 270 271 272 273

      if (!done)
	goto not_applied;
    }
    else
    {
    not_applied:
      /* No lookup applied for this index */
B
Behdad Esfahbod 已提交
274
      c->buffer->next_glyph ();
275 276 277 278 279 280
      i++;
    }
  }

  return true;
}
281

B
Behdad Esfahbod 已提交
282 283
HB_END_DECLS

284 285 286

/* Contextual lookups */

B
Behdad Esfahbod 已提交
287 288
struct ContextLookupContext
{
289
  ContextFuncs funcs;
B
Behdad Esfahbod 已提交
290
  const void *match_data;
291 292
};

B
Behdad Esfahbod 已提交
293
static inline bool context_lookup (hb_apply_context_t *c,
294
				   unsigned int inputCount, /* Including the first glyph (not matched) */
295
				   const USHORT input[], /* Array of input values--start with second glyph */
296
				   unsigned int lookupCount,
297
				   const LookupRecord lookupRecord[],
B
Behdad Esfahbod 已提交
298
				   ContextLookupContext &lookup_context)
299
{
B
Behdad Esfahbod 已提交
300 301
  hb_apply_context_t new_context = *c;
  return match_input (c,
B
Behdad Esfahbod 已提交
302 303 304 305 306 307 308
		      inputCount, input,
		      lookup_context.funcs.match, lookup_context.match_data,
		      &new_context.context_length)
      && apply_lookup (&new_context,
		       inputCount,
		       lookupCount, lookupRecord,
		       lookup_context.funcs.apply);
309 310
}

B
Behdad Esfahbod 已提交
311 312
struct Rule
{
313 314 315
  friend struct RuleSet;

  private:
B
Behdad Esfahbod 已提交
316
  inline bool apply (hb_apply_context_t *c, ContextLookupContext &lookup_context) const
B
Behdad Esfahbod 已提交
317
  {
318
    TRACE_APPLY ();
B
Behdad Esfahbod 已提交
319
    const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (input, input[0].static_size * (inputCount ? inputCount - 1 : 0));
B
Behdad Esfahbod 已提交
320
    return context_lookup (c,
321
			   inputCount, input,
B
Minor  
Behdad Esfahbod 已提交
322
			   lookupCount, lookupRecord,
B
Behdad Esfahbod 已提交
323
			   lookup_context);
324 325
  }

B
Behdad Esfahbod 已提交
326
  public:
B
Behdad Esfahbod 已提交
327
  inline bool sanitize (hb_sanitize_context_t *c) {
328
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
329 330 331
    return inputCount.sanitize (c)
	&& lookupCount.sanitize (c)
	&& c->check_range (input,
B
Behdad Esfahbod 已提交
332 333
				 input[0].static_size * inputCount
				 + lookupRecordX[0].static_size * lookupCount);
B
Behdad Esfahbod 已提交
334 335
  }

336
  private:
337
  USHORT	inputCount;		/* Total number of glyphs in input
338 339
					 * glyph sequence--includes the  first
					 * glyph */
340
  USHORT	lookupCount;		/* Number of LookupRecords */
B
Behdad Esfahbod 已提交
341
  USHORT	input[VAR];		/* Array of match inputs--start with
342
					 * second glyph */
B
Behdad Esfahbod 已提交
343
  LookupRecord	lookupRecordX[VAR];	/* Array of LookupRecords--in
344
					 * design order */
B
Behdad Esfahbod 已提交
345
  public:
346
  DEFINE_SIZE_ARRAY2 (4, input, lookupRecordX);
347 348
};

B
Behdad Esfahbod 已提交
349 350
struct RuleSet
{
B
Behdad Esfahbod 已提交
351
  inline bool apply (hb_apply_context_t *c, ContextLookupContext &lookup_context) const
B
Behdad Esfahbod 已提交
352
  {
353
    TRACE_APPLY ();
354
    unsigned int num_rules = rule.len;
B
Behdad Esfahbod 已提交
355 356
    for (unsigned int i = 0; i < num_rules; i++)
    {
B
Behdad Esfahbod 已提交
357
      if ((this+rule[i]).apply (c, lookup_context))
358 359 360 361 362 363
        return true;
    }

    return false;
  }

B
Behdad Esfahbod 已提交
364
  inline bool sanitize (hb_sanitize_context_t *c) {
365
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
366
    return rule.sanitize (c, this);
B
Behdad Esfahbod 已提交
367 368
  }

369 370
  private:
  OffsetArrayOf<Rule>
371
		rule;			/* Array of Rule tables
372
					 * ordered by preference */
B
Behdad Esfahbod 已提交
373
  public:
374
  DEFINE_SIZE_ARRAY (2, rule);
375 376 377
};


B
Behdad Esfahbod 已提交
378 379
struct ContextFormat1
{
380 381 382
  friend struct Context;

  private:
B
Behdad Esfahbod 已提交
383
  inline bool apply (hb_apply_context_t *c, apply_lookup_func_t apply_func) const
B
Behdad Esfahbod 已提交
384
  {
385
    TRACE_APPLY ();
386
    unsigned int index = (this+coverage) (c->buffer->info[c->buffer->i].codepoint);
387
    if (likely (index == NOT_COVERED))
388 389
      return false;

390
    const RuleSet &rule_set = this+ruleSet[index];
B
Behdad Esfahbod 已提交
391
    struct ContextLookupContext lookup_context = {
392 393
      {match_glyph, apply_func},
      NULL
394
    };
B
Behdad Esfahbod 已提交
395
    return rule_set.apply (c, lookup_context);
396 397
  }

B
Behdad Esfahbod 已提交
398
  inline bool sanitize (hb_sanitize_context_t *c) {
399
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
400 401
    return coverage.sanitize (c, this)
	&& ruleSet.sanitize (c, this);
B
Behdad Esfahbod 已提交
402 403
  }

404 405 406 407 408 409 410 411
  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 */
412
  public:
413
  DEFINE_SIZE_ARRAY (6, ruleSet);
414 415 416
};


B
Behdad Esfahbod 已提交
417 418
struct ContextFormat2
{
419 420 421
  friend struct Context;

  private:
B
Behdad Esfahbod 已提交
422
  inline bool apply (hb_apply_context_t *c, apply_lookup_func_t apply_func) const
B
Behdad Esfahbod 已提交
423
  {
424
    TRACE_APPLY ();
425
    unsigned int index = (this+coverage) (c->buffer->info[c->buffer->i].codepoint);
426
    if (likely (index == NOT_COVERED))
427 428 429
      return false;

    const ClassDef &class_def = this+classDef;
430
    index = class_def (c->buffer->info[c->buffer->i].codepoint);
431
    const RuleSet &rule_set = this+ruleSet[index];
B
Behdad Esfahbod 已提交
432
    struct ContextLookupContext lookup_context = {
B
Behdad Esfahbod 已提交
433 434
      {match_class, apply_func},
      &class_def
435
    };
B
Behdad Esfahbod 已提交
436
    return rule_set.apply (c, lookup_context);
437 438
  }

B
Behdad Esfahbod 已提交
439
  inline bool sanitize (hb_sanitize_context_t *c) {
440
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
441 442 443
    return coverage.sanitize (c, this)
        && classDef.sanitize (c, this)
	&& ruleSet.sanitize (c, this);
B
Behdad Esfahbod 已提交
444 445
  }

446 447 448 449 450 451 452 453 454 455 456
  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 */
457
  public:
458
  DEFINE_SIZE_ARRAY (8, ruleSet);
459 460 461
};


B
Behdad Esfahbod 已提交
462 463
struct ContextFormat3
{
464 465 466
  friend struct Context;

  private:
B
Behdad Esfahbod 已提交
467
  inline bool apply (hb_apply_context_t *c, apply_lookup_func_t apply_func) const
B
Behdad Esfahbod 已提交
468
  {
469
    TRACE_APPLY ();
470
    unsigned int index = (this+coverage[0]) (c->buffer->info[c->buffer->i].codepoint);
471
    if (likely (index == NOT_COVERED))
472
      return false;
473

B
Behdad Esfahbod 已提交
474
    const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (coverage, coverage[0].static_size * glyphCount);
B
Behdad Esfahbod 已提交
475
    struct ContextLookupContext lookup_context = {
476
      {match_coverage, apply_func},
B
Behdad Esfahbod 已提交
477
      this
478
    };
B
Behdad Esfahbod 已提交
479
    return context_lookup (c,
480
			   glyphCount, (const USHORT *) (coverage + 1),
B
Minor  
Behdad Esfahbod 已提交
481
			   lookupCount, lookupRecord,
B
Behdad Esfahbod 已提交
482
			   lookup_context);
483 484
  }

B
Behdad Esfahbod 已提交
485
  inline bool sanitize (hb_sanitize_context_t *c) {
486
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
487
    if (!c->check_struct (this)) return false;
B
Behdad Esfahbod 已提交
488
    unsigned int count = glyphCount;
B
Behdad Esfahbod 已提交
489
    if (!c->check_array (coverage, coverage[0].static_size, count)) return false;
B
Behdad Esfahbod 已提交
490
    for (unsigned int i = 0; i < count; i++)
B
Behdad Esfahbod 已提交
491
      if (!coverage[i].sanitize (c, this)) return false;
B
Behdad Esfahbod 已提交
492
    LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (coverage, coverage[0].static_size * count);
B
Behdad Esfahbod 已提交
493
    return c->check_array (lookupRecord, lookupRecord[0].static_size, lookupCount);
B
Behdad Esfahbod 已提交
494 495
  }

496 497 498 499
  private:
  USHORT	format;			/* Format identifier--format = 3 */
  USHORT	glyphCount;		/* Number of glyphs in the input glyph
					 * sequence */
500
  USHORT	lookupCount;		/* Number of LookupRecords */
501
  OffsetTo<Coverage>
B
Behdad Esfahbod 已提交
502
		coverage[VAR];		/* Array of offsets to Coverage
503
					 * table in glyph sequence order */
B
Behdad Esfahbod 已提交
504
  LookupRecord	lookupRecordX[VAR];	/* Array of LookupRecords--in
505
					 * design order */
B
Behdad Esfahbod 已提交
506
  public:
507
  DEFINE_SIZE_ARRAY2 (6, coverage, lookupRecordX);
508 509
};

B
Behdad Esfahbod 已提交
510 511
struct Context
{
512
  protected:
B
Behdad Esfahbod 已提交
513
  inline bool apply (hb_apply_context_t *c, apply_lookup_func_t apply_func) const
B
Behdad Esfahbod 已提交
514
  {
515
    TRACE_APPLY ();
516
    switch (u.format) {
B
Behdad Esfahbod 已提交
517 518 519
    case 1: return u.format1.apply (c, apply_func);
    case 2: return u.format2.apply (c, apply_func);
    case 3: return u.format3.apply (c, apply_func);
520 521 522 523
    default:return false;
    }
  }

B
Behdad Esfahbod 已提交
524
  inline bool sanitize (hb_sanitize_context_t *c) {
525
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
526
    if (!u.format.sanitize (c)) return false;
B
Behdad Esfahbod 已提交
527
    switch (u.format) {
B
Behdad Esfahbod 已提交
528 529 530
    case 1: return u.format1.sanitize (c);
    case 2: return u.format2.sanitize (c);
    case 3: return u.format3.sanitize (c);
B
Behdad Esfahbod 已提交
531 532 533 534
    default:return true;
    }
  }

535 536
  private:
  union {
B
Behdad Esfahbod 已提交
537
  USHORT		format;		/* Format identifier */
B
Behdad Esfahbod 已提交
538 539 540
  ContextFormat1	format1;
  ContextFormat2	format2;
  ContextFormat3	format3;
541 542 543
  } u;
};

544 545 546

/* Chaining Contextual lookups */

B
Behdad Esfahbod 已提交
547 548
struct ChainContextLookupContext
{
549
  ContextFuncs funcs;
B
Behdad Esfahbod 已提交
550
  const void *match_data[3];
551 552
};

B
Behdad Esfahbod 已提交
553
static inline bool chain_context_lookup (hb_apply_context_t *c,
554
					 unsigned int backtrackCount,
555
					 const USHORT backtrack[],
556
					 unsigned int inputCount, /* Including the first glyph (not matched) */
557
					 const USHORT input[], /* Array of input values--start with second glyph */
558
					 unsigned int lookaheadCount,
559
					 const USHORT lookahead[],
560
					 unsigned int lookupCount,
561
					 const LookupRecord lookupRecord[],
B
Behdad Esfahbod 已提交
562
					 ChainContextLookupContext &lookup_context)
563
{
564
  /* First guess */
565
  if (unlikely (c->buffer->backtrack_len () < backtrackCount ||
566
		c->buffer->i + inputCount + lookaheadCount > c->buffer->len ||
B
Behdad Esfahbod 已提交
567
		inputCount + lookaheadCount > c->context_length))
568 569
    return false;

B
Behdad Esfahbod 已提交
570 571
  hb_apply_context_t new_context = *c;
  return match_backtrack (c,
B
Behdad Esfahbod 已提交
572 573
			  backtrackCount, backtrack,
			  lookup_context.funcs.match, lookup_context.match_data[0])
B
Behdad Esfahbod 已提交
574
      && match_input (c,
B
Behdad Esfahbod 已提交
575 576 577
		      inputCount, input,
		      lookup_context.funcs.match, lookup_context.match_data[1],
		      &new_context.context_length)
B
Behdad Esfahbod 已提交
578
      && match_lookahead (c,
B
Behdad Esfahbod 已提交
579 580 581 582 583 584 585
			  lookaheadCount, lookahead,
			  lookup_context.funcs.match, lookup_context.match_data[2],
			  new_context.context_length)
      && apply_lookup (&new_context,
		       inputCount,
		       lookupCount, lookupRecord,
		       lookup_context.funcs.apply);
586
}
587

B
Behdad Esfahbod 已提交
588 589
struct ChainRule
{
590 591 592
  friend struct ChainRuleSet;

  private:
B
Behdad Esfahbod 已提交
593
  inline bool apply (hb_apply_context_t *c, ChainContextLookupContext &lookup_context) const
B
Behdad Esfahbod 已提交
594
  {
595
    TRACE_APPLY ();
596 597 598
    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 已提交
599
    return chain_context_lookup (c,
B
Behdad Esfahbod 已提交
600 601 602 603
				 backtrack.len, backtrack.array,
				 input.len, input.array,
				 lookahead.len, lookahead.array,
				 lookup.len, lookup.array,
B
Behdad Esfahbod 已提交
604
				 lookup_context);
605 606
  }

B
Behdad Esfahbod 已提交
607
  public:
B
Behdad Esfahbod 已提交
608
  inline bool sanitize (hb_sanitize_context_t *c) {
609
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
610
    if (!backtrack.sanitize (c)) return false;
611
    HeadlessArrayOf<USHORT> &input = StructAfter<HeadlessArrayOf<USHORT> > (backtrack);
B
Behdad Esfahbod 已提交
612
    if (!input.sanitize (c)) return false;
613
    ArrayOf<USHORT> &lookahead = StructAfter<ArrayOf<USHORT> > (input);
B
Behdad Esfahbod 已提交
614
    if (!lookahead.sanitize (c)) return false;
615
    ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
B
Behdad Esfahbod 已提交
616
    return lookup.sanitize (c);
B
Behdad Esfahbod 已提交
617
  }
618 619

  private:
B
Behdad Esfahbod 已提交
620 621
  ArrayOf<USHORT>
		backtrack;		/* Array of backtracking values
622 623
					 * (to be matched before the input
					 * sequence) */
B
Behdad Esfahbod 已提交
624 625
  HeadlessArrayOf<USHORT>
		inputX;			/* Array of input values (start with
626
					 * second glyph) */
B
Behdad Esfahbod 已提交
627 628
  ArrayOf<USHORT>
		lookaheadX;		/* Array of lookahead values's (to be
629
					 * matched after the input sequence) */
B
Behdad Esfahbod 已提交
630
  ArrayOf<LookupRecord>
631
		lookupX;		/* Array of LookupRecords--in
632
					 * design order) */
633
  public:
B
Behdad Esfahbod 已提交
634
  DEFINE_SIZE_MIN (8);
635 636
};

B
Behdad Esfahbod 已提交
637 638
struct ChainRuleSet
{
B
Behdad Esfahbod 已提交
639
  inline bool apply (hb_apply_context_t *c, ChainContextLookupContext &lookup_context) const
B
Behdad Esfahbod 已提交
640
  {
641
    TRACE_APPLY ();
642
    unsigned int num_rules = rule.len;
B
Behdad Esfahbod 已提交
643 644
    for (unsigned int i = 0; i < num_rules; i++)
    {
B
Behdad Esfahbod 已提交
645
      if ((this+rule[i]).apply (c, lookup_context))
646 647 648 649 650
        return true;
    }

    return false;
  }
651

B
Behdad Esfahbod 已提交
652
  inline bool sanitize (hb_sanitize_context_t *c) {
653
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
654
    return rule.sanitize (c, this);
B
Behdad Esfahbod 已提交
655 656
  }

657
  private:
658 659 660
  OffsetArrayOf<ChainRule>
		rule;			/* Array of ChainRule tables
					 * ordered by preference */
661
  public:
662
  DEFINE_SIZE_ARRAY (2, rule);
663 664
};

B
Behdad Esfahbod 已提交
665 666
struct ChainContextFormat1
{
667 668 669
  friend struct ChainContext;

  private:
B
Behdad Esfahbod 已提交
670
  inline bool apply (hb_apply_context_t *c, apply_lookup_func_t apply_func) const
B
Behdad Esfahbod 已提交
671
  {
672
    TRACE_APPLY ();
673
    unsigned int index = (this+coverage) (c->buffer->info[c->buffer->i].codepoint);
674
    if (likely (index == NOT_COVERED))
675
      return false;
676

677
    const ChainRuleSet &rule_set = this+ruleSet[index];
B
Behdad Esfahbod 已提交
678
    struct ChainContextLookupContext lookup_context = {
679 680 681
      {match_glyph, apply_func},
      {NULL, NULL, NULL}
    };
B
Behdad Esfahbod 已提交
682
    return rule_set.apply (c, lookup_context);
683
  }
B
Behdad Esfahbod 已提交
684

B
Behdad Esfahbod 已提交
685
  inline bool sanitize (hb_sanitize_context_t *c) {
686
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
687 688
    return coverage.sanitize (c, this)
	&& ruleSet.sanitize (c, this);
B
Behdad Esfahbod 已提交
689 690
  }

691 692
  private:
  USHORT	format;			/* Format identifier--format = 1 */
693 694
  OffsetTo<Coverage>
		coverage;		/* Offset to Coverage table--from
695
					 * beginning of table */
696 697 698
  OffsetArrayOf<ChainRuleSet>
		ruleSet;		/* Array of ChainRuleSet tables
					 * ordered by Coverage Index */
699
  public:
700
  DEFINE_SIZE_ARRAY (6, ruleSet);
701 702
};

B
Behdad Esfahbod 已提交
703 704
struct ChainContextFormat2
{
705 706 707
  friend struct ChainContext;

  private:
B
Behdad Esfahbod 已提交
708
  inline bool apply (hb_apply_context_t *c, apply_lookup_func_t apply_func) const
B
Behdad Esfahbod 已提交
709
  {
710
    TRACE_APPLY ();
711
    unsigned int index = (this+coverage) (c->buffer->info[c->buffer->i].codepoint);
712
    if (likely (index == NOT_COVERED))
713 714 715 716 717 718
      return false;

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

719
    index = input_class_def (c->buffer->info[c->buffer->i].codepoint);
720
    const ChainRuleSet &rule_set = this+ruleSet[index];
B
Behdad Esfahbod 已提交
721
    struct ChainContextLookupContext lookup_context = {
B
Behdad Esfahbod 已提交
722 723 724 725
      {match_class, apply_func},
      {&backtrack_class_def,
       &input_class_def,
       &lookahead_class_def}
726
    };
B
Behdad Esfahbod 已提交
727
    return rule_set.apply (c, lookup_context);
728 729
  }

B
Behdad Esfahbod 已提交
730
  inline bool sanitize (hb_sanitize_context_t *c) {
731
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
732 733 734 735 736
    return coverage.sanitize (c, this)
	&& backtrackClassDef.sanitize (c, this)
	&& inputClassDef.sanitize (c, this)
	&& lookaheadClassDef.sanitize (c, this)
	&& ruleSet.sanitize (c, this);
B
Behdad Esfahbod 已提交
737 738
  }

739 740
  private:
  USHORT	format;			/* Format identifier--format = 2 */
741 742
  OffsetTo<Coverage>
		coverage;		/* Offset to Coverage table--from
743
					 * beginning of table */
744 745
  OffsetTo<ClassDef>
		backtrackClassDef;	/* Offset to glyph ClassDef table
746 747
					 * containing backtrack sequence
					 * data--from beginning of table */
748 749
  OffsetTo<ClassDef>
		inputClassDef;		/* Offset to glyph ClassDef
750 751
					 * table containing input sequence
					 * data--from beginning of table */
752 753
  OffsetTo<ClassDef>
		lookaheadClassDef;	/* Offset to glyph ClassDef table
754 755
					 * containing lookahead sequence
					 * data--from beginning of table */
756 757 758
  OffsetArrayOf<ChainRuleSet>
		ruleSet;		/* Array of ChainRuleSet tables
					 * ordered by class */
759
  public:
760
  DEFINE_SIZE_ARRAY (12, ruleSet);
761 762
};

B
Behdad Esfahbod 已提交
763 764
struct ChainContextFormat3
{
765 766 767
  friend struct ChainContext;

  private:
768

B
Behdad Esfahbod 已提交
769
  inline bool apply (hb_apply_context_t *c, apply_lookup_func_t apply_func) const
B
Behdad Esfahbod 已提交
770
  {
771
    TRACE_APPLY ();
772
    const OffsetArrayOf<Coverage> &input = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
773

774
    unsigned int index = (this+input[0]) (c->buffer->info[c->buffer->i].codepoint);
775
    if (likely (index == NOT_COVERED))
776 777
      return false;

778 779
    const OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (input);
    const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
B
Behdad Esfahbod 已提交
780
    struct ChainContextLookupContext lookup_context = {
781
      {match_coverage, apply_func},
B
Behdad Esfahbod 已提交
782
      {this, this, this}
783
    };
B
Behdad Esfahbod 已提交
784
    return chain_context_lookup (c,
B
Behdad Esfahbod 已提交
785 786 787 788
				 backtrack.len, (const USHORT *) backtrack.array,
				 input.len, (const USHORT *) input.array + 1,
				 lookahead.len, (const USHORT *) lookahead.array,
				 lookup.len, lookup.array,
B
Behdad Esfahbod 已提交
789
				 lookup_context);
790 791
  }

B
Behdad Esfahbod 已提交
792
  inline bool sanitize (hb_sanitize_context_t *c) {
793
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
794
    if (!backtrack.sanitize (c, this)) return false;
795
    OffsetArrayOf<Coverage> &input = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
B
Behdad Esfahbod 已提交
796
    if (!input.sanitize (c, this)) return false;
797
    OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (input);
B
Behdad Esfahbod 已提交
798
    if (!lookahead.sanitize (c, this)) return false;
799
    ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
B
Behdad Esfahbod 已提交
800
    return lookup.sanitize (c);
B
Behdad Esfahbod 已提交
801 802
  }

803 804
  private:
  USHORT	format;			/* Format identifier--format = 3 */
B
Behdad Esfahbod 已提交
805
  OffsetArrayOf<Coverage>
806
		backtrack;		/* Array of coverage tables
807 808
					 * in backtracking sequence, in  glyph
					 * sequence order */
B
Behdad Esfahbod 已提交
809
  OffsetArrayOf<Coverage>
810
		inputX		;	/* Array of coverage
811 812
					 * tables in input sequence, in glyph
					 * sequence order */
B
Behdad Esfahbod 已提交
813
  OffsetArrayOf<Coverage>
814
		lookaheadX;		/* Array of coverage tables
815 816
					 * in lookahead sequence, in glyph
					 * sequence order */
B
Behdad Esfahbod 已提交
817
  ArrayOf<LookupRecord>
818
		lookupX;		/* Array of LookupRecords--in
B
Behdad Esfahbod 已提交
819
					 * design order) */
820
  public:
B
Behdad Esfahbod 已提交
821
  DEFINE_SIZE_MIN (10);
822 823
};

B
Behdad Esfahbod 已提交
824 825
struct ChainContext
{
826
  protected:
B
Behdad Esfahbod 已提交
827
  inline bool apply (hb_apply_context_t *c, apply_lookup_func_t apply_func) const
B
Behdad Esfahbod 已提交
828
  {
829
    TRACE_APPLY ();
830
    switch (u.format) {
B
Behdad Esfahbod 已提交
831 832 833
    case 1: return u.format1.apply (c, apply_func);
    case 2: return u.format2.apply (c, apply_func);
    case 3: return u.format3.apply (c, apply_func);
834 835 836 837
    default:return false;
    }
  }

B
Behdad Esfahbod 已提交
838
  inline bool sanitize (hb_sanitize_context_t *c) {
839
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
840
    if (!u.format.sanitize (c)) return false;
B
Behdad Esfahbod 已提交
841
    switch (u.format) {
B
Behdad Esfahbod 已提交
842 843 844
    case 1: return u.format1.sanitize (c);
    case 2: return u.format2.sanitize (c);
    case 3: return u.format3.sanitize (c);
B
Behdad Esfahbod 已提交
845 846 847 848
    default:return true;
    }
  }

849 850 851
  private:
  union {
  USHORT		format;	/* Format identifier */
B
Behdad Esfahbod 已提交
852 853 854
  ChainContextFormat1	format1;
  ChainContextFormat2	format2;
  ChainContextFormat3	format3;
855 856 857 858
  } u;
};


859 860 861 862
struct ExtensionFormat1
{
  friend struct Extension;

B
Behdad Esfahbod 已提交
863
  protected:
864
  inline unsigned int get_type (void) const { return extensionLookupType; }
B
Behdad Esfahbod 已提交
865
  inline unsigned int get_offset (void) const { return extensionOffset; }
866

B
Behdad Esfahbod 已提交
867
  inline bool sanitize (hb_sanitize_context_t *c) {
868
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
869
    return c->check_struct (this);
B
Behdad Esfahbod 已提交
870 871
  }

872 873 874 875 876
  private:
  USHORT	format;			/* Format identifier. Set to 1. */
  USHORT	extensionLookupType;	/* Lookup type of subtable referenced
					 * by ExtensionOffset (i.e. the
					 * extension subtable). */
877 878
  ULONG		extensionOffset;	/* Offset to the extension subtable,
					 * of lookup type subtable. */
879 880
  public:
  DEFINE_SIZE_STATIC (8);
881 882 883 884 885 886 887
};

struct Extension
{
  inline unsigned int get_type (void) const
  {
    switch (u.format) {
B
Behdad Esfahbod 已提交
888
    case 1: return u.format1.get_type ();
889 890 891
    default:return 0;
    }
  }
B
Behdad Esfahbod 已提交
892
  inline unsigned int get_offset (void) const
893 894
  {
    switch (u.format) {
B
Behdad Esfahbod 已提交
895
    case 1: return u.format1.get_offset ();
B
Behdad Esfahbod 已提交
896
    default:return 0;
897 898 899
    }
  }

B
Behdad Esfahbod 已提交
900
  inline bool sanitize (hb_sanitize_context_t *c) {
901
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
902
    if (!u.format.sanitize (c)) return false;
B
Behdad Esfahbod 已提交
903
    switch (u.format) {
B
Behdad Esfahbod 已提交
904
    case 1: return u.format1.sanitize (c);
B
Behdad Esfahbod 已提交
905 906 907 908
    default:return true;
    }
  }

909 910 911
  private:
  union {
  USHORT		format;		/* Format identifier */
B
Behdad Esfahbod 已提交
912
  ExtensionFormat1	format1;
913 914 915 916
  } u;
};


B
Behdad Esfahbod 已提交
917 918 919 920
/*
 * GSUB/GPOS Common
 */

B
Behdad Esfahbod 已提交
921 922
struct GSUBGPOS
{
B
Behdad Esfahbod 已提交
923 924
  static const hb_tag_t GSUBTag	= HB_OT_TAG_GSUB;
  static const hb_tag_t GPOSTag	= HB_OT_TAG_GPOS;
B
Behdad Esfahbod 已提交
925

926 927 928 929
  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 已提交
930 931 932 933
  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); }
934 935 936 937 938 939 940 941 942
  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 已提交
943 944 945 946
  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); }
947 948 949 950 951 952 953 954 955
  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 已提交
956

B
Behdad Esfahbod 已提交
957
  inline bool sanitize (hb_sanitize_context_t *c) {
958
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
959 960 961 962
    return version.sanitize (c) && likely (version.major == 1)
	&& scriptList.sanitize (c, this)
	&& featureList.sanitize (c, this)
	&& lookupList.sanitize (c, this);
B
Behdad Esfahbod 已提交
963 964
  }

965
  protected:
B
Behdad Esfahbod 已提交
966
  FixedVersion	version;	/* Version of the GSUB/GPOS table--initially set
B
Behdad Esfahbod 已提交
967 968 969 970 971 972 973
				 * to 0x00010000 */
  OffsetTo<ScriptList>
		scriptList;  	/* ScriptList table */
  OffsetTo<FeatureList>
		featureList; 	/* FeatureList table */
  OffsetTo<LookupList>
		lookupList; 	/* LookupList table */
974 975
  public:
  DEFINE_SIZE_STATIC (10);
B
Behdad Esfahbod 已提交
976
};
977

978

B
Behdad Esfahbod 已提交
979 980
HB_END_DECLS

981
#endif /* HB_OT_LAYOUT_GSUBGPOS_PRIVATE_HH */