hb-ot-layout-gsubgpos-private.hh 27.8 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
/* 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
Minor  
Behdad Esfahbod 已提交
42 43 44 45 46 47 48
static inline uint16_t allocate_lig_id (hb_buffer_t *buffer) {
  uint16_t lig_id = buffer->next_serial ();
  if (unlikely (!lig_id)) lig_id = buffer->next_serial (); /* in case of overflow */
  return lig_id;
}


B
Behdad Esfahbod 已提交
49

B
Behdad Esfahbod 已提交
50
#ifndef HB_DEBUG_APPLY
51
#define HB_DEBUG_APPLY (HB_DEBUG+0)
B
Behdad Esfahbod 已提交
52 53
#endif

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

B
Behdad Esfahbod 已提交
57

B
Behdad Esfahbod 已提交
58 59
HB_BEGIN_DECLS

60 61
struct hb_apply_context_t
{
B
Minor  
Behdad Esfahbod 已提交
62
  unsigned int debug_depth;
63 64
  hb_font_t *font;
  hb_face_t *face;
B
Behdad Esfahbod 已提交
65
  hb_buffer_t *buffer;
66
  hb_direction_t direction;
B
Behdad Esfahbod 已提交
67
  hb_mask_t lookup_mask;
68
  unsigned int context_length;
69
  unsigned int nesting_level_left;
70
  unsigned int lookup_props;
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
  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 已提交
89
    /* XXX if ! has gdef */
90
    buffer->info[buffer->idx].props_cache() = klass;
91 92 93 94 95
  }

  private:
  inline void clear_property (void) const
  {
B
Behdad Esfahbod 已提交
96
    /* XXX if has gdef */
97
    buffer->info[buffer->idx].props_cache() = 0;
98
  }
99 100
};

101

B
Behdad Esfahbod 已提交
102

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

B
Behdad Esfahbod 已提交
106 107
struct ContextFuncs
{
108 109
  match_func_t match;
  apply_lookup_func_t apply;
110 111
};

112

B
Behdad Esfahbod 已提交
113
static inline bool match_glyph (hb_codepoint_t glyph_id, const USHORT &value, const void *data HB_UNUSED)
B
Behdad Esfahbod 已提交
114
{
115 116
  return glyph_id == value;
}
117

B
Behdad Esfahbod 已提交
118
static inline bool match_class (hb_codepoint_t glyph_id, const USHORT &value, const void *data)
B
Behdad Esfahbod 已提交
119
{
B
Behdad Esfahbod 已提交
120
  const ClassDef &class_def = *reinterpret_cast<const ClassDef *>(data);
121 122 123
  return class_def.get_class (glyph_id) == value;
}

B
Behdad Esfahbod 已提交
124
static inline bool match_coverage (hb_codepoint_t glyph_id, const USHORT &value, const void *data)
B
Behdad Esfahbod 已提交
125
{
126
  const OffsetTo<Coverage> &coverage = (const OffsetTo<Coverage>&)value;
127 128 129
  return (data+coverage) (glyph_id) != NOT_COVERED;
}

130

B
Behdad Esfahbod 已提交
131
static inline bool match_input (hb_apply_context_t *c,
B
Behdad Esfahbod 已提交
132 133 134
				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 已提交
135
				const void *match_data,
B
Behdad Esfahbod 已提交
136 137 138
				unsigned int *context_length_out)
{
  unsigned int i, j;
139 140
  unsigned int end = MIN (c->buffer->len, c->buffer->idx + c->context_length);
  if (unlikely (c->buffer->idx + count > end))
B
Behdad Esfahbod 已提交
141
    return false;
B
Behdad Esfahbod 已提交
142

143
  for (i = 1, j = c->buffer->idx + 1; i < count; i++, j++)
B
Behdad Esfahbod 已提交
144
  {
145
    while (_hb_ot_layout_skip_mark (c->face, &c->buffer->info[j], c->lookup_props, NULL))
B
Behdad Esfahbod 已提交
146
    {
147
      if (unlikely (j + count - i == end))
B
Behdad Esfahbod 已提交
148 149 150 151
	return false;
      j++;
    }

152
    if (likely (!match_func (c->buffer->info[j].codepoint, input[i - 1], match_data)))
B
Behdad Esfahbod 已提交
153 154 155
      return false;
  }

156
  *context_length_out = j - c->buffer->idx;
B
Behdad Esfahbod 已提交
157 158 159 160

  return true;
}

B
Behdad Esfahbod 已提交
161
static inline bool match_backtrack (hb_apply_context_t *c,
162 163 164
				    unsigned int count,
				    const USHORT backtrack[],
				    match_func_t match_func,
B
Behdad Esfahbod 已提交
165
				    const void *match_data)
166
{
167
  if (unlikely (c->buffer->backtrack_len () < count))
B
Behdad Esfahbod 已提交
168
    return false;
169

170
  for (unsigned int i = 0, j = c->buffer->backtrack_len () - 1; i < count; i++, j--)
B
Behdad Esfahbod 已提交
171
  {
172
    while (_hb_ot_layout_skip_mark (c->face, &c->buffer->out_info[j], c->lookup_props, NULL))
B
Behdad Esfahbod 已提交
173
    {
174
      if (unlikely (j + 1 == count - i))
175 176 177 178
	return false;
      j--;
    }

179
    if (likely (!match_func (c->buffer->out_info[j].codepoint, backtrack[i], match_data)))
180 181 182 183 184 185
      return false;
  }

  return true;
}

B
Behdad Esfahbod 已提交
186
static inline bool match_lookahead (hb_apply_context_t *c,
B
Behdad Esfahbod 已提交
187 188 189
				    unsigned int count,
				    const USHORT lookahead[],
				    match_func_t match_func,
B
Behdad Esfahbod 已提交
190
				    const void *match_data,
B
Behdad Esfahbod 已提交
191
				    unsigned int offset)
192
{
193
  unsigned int i, j;
194 195
  unsigned int end = MIN (c->buffer->len, c->buffer->idx + c->context_length);
  if (unlikely (c->buffer->idx + offset + count > end))
B
Behdad Esfahbod 已提交
196
    return false;
197

198
  for (i = 0, j = c->buffer->idx + offset; i < count; i++, j++)
B
Behdad Esfahbod 已提交
199
  {
200
    while (_hb_ot_layout_skip_mark (c->face, &c->buffer->info[j], c->lookup_props, NULL))
B
Behdad Esfahbod 已提交
201
    {
202
      if (unlikely (j + count - i == end))
203 204 205 206
	return false;
      j++;
    }

207
    if (likely (!match_func (c->buffer->info[j].codepoint, lookahead[i], match_data)))
208 209 210
      return false;
  }

211 212 213
  return true;
}

B
Behdad Esfahbod 已提交
214 215
HB_END_DECLS

216

B
Behdad Esfahbod 已提交
217 218
struct LookupRecord
{
B
Behdad Esfahbod 已提交
219
  inline bool sanitize (hb_sanitize_context_t *c) {
220
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
221
    return c->check_struct (this);
B
Behdad Esfahbod 已提交
222 223
  }

224 225 226 227
  USHORT	sequenceIndex;		/* Index into current glyph
					 * sequence--first glyph = 0 */
  USHORT	lookupListIndex;	/* Lookup to apply to that
					 * position--zero--based */
B
Behdad Esfahbod 已提交
228 229
  public:
  DEFINE_SIZE_STATIC (4);
230 231
};

B
Behdad Esfahbod 已提交
232 233 234

HB_BEGIN_DECLS

B
Behdad Esfahbod 已提交
235
static inline bool apply_lookup (hb_apply_context_t *c,
236 237
				 unsigned int count, /* Including the first glyph */
				 unsigned int lookupCount,
238 239 240
				 const LookupRecord lookupRecord[], /* Array of LookupRecords--in design order */
				 apply_lookup_func_t apply_func)
{
241 242
  unsigned int end = MIN (c->buffer->len, c->buffer->idx + c->context_length);
  if (unlikely (count == 0 || c->buffer->idx + count > end))
243
    return false;
244

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

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

B
Behdad Esfahbod 已提交
262
    if (lookupCount && i == lookupRecord->sequenceIndex)
263
    {
264
      unsigned int old_pos = c->buffer->idx;
265 266

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

B
Behdad Esfahbod 已提交
269 270
      lookupRecord++;
      lookupCount--;
B
Behdad Esfahbod 已提交
271
      /* Err, this is wrong if the lookup jumped over some glyphs */
272 273
      i += c->buffer->idx - old_pos;
      if (unlikely (c->buffer->idx == end))
274
	return true;
275 276 277 278 279 280 281 282

      if (!done)
	goto not_applied;
    }
    else
    {
    not_applied:
      /* No lookup applied for this index */
B
Behdad Esfahbod 已提交
283
      c->buffer->next_glyph ();
284 285 286 287 288 289
      i++;
    }
  }

  return true;
}
290

B
Behdad Esfahbod 已提交
291 292
HB_END_DECLS

293 294 295

/* Contextual lookups */

B
Behdad Esfahbod 已提交
296 297
struct ContextLookupContext
{
298
  ContextFuncs funcs;
B
Behdad Esfahbod 已提交
299
  const void *match_data;
300 301
};

B
Behdad Esfahbod 已提交
302
static inline bool context_lookup (hb_apply_context_t *c,
303
				   unsigned int inputCount, /* Including the first glyph (not matched) */
304
				   const USHORT input[], /* Array of input values--start with second glyph */
305
				   unsigned int lookupCount,
306
				   const LookupRecord lookupRecord[],
B
Behdad Esfahbod 已提交
307
				   ContextLookupContext &lookup_context)
308
{
B
Behdad Esfahbod 已提交
309 310
  hb_apply_context_t new_context = *c;
  return match_input (c,
B
Behdad Esfahbod 已提交
311 312 313 314 315 316 317
		      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);
318 319
}

B
Behdad Esfahbod 已提交
320 321
struct Rule
{
322 323 324
  friend struct RuleSet;

  private:
B
Behdad Esfahbod 已提交
325
  inline bool apply (hb_apply_context_t *c, ContextLookupContext &lookup_context) const
B
Behdad Esfahbod 已提交
326
  {
327
    TRACE_APPLY ();
B
Behdad Esfahbod 已提交
328
    const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (input, input[0].static_size * (inputCount ? inputCount - 1 : 0));
B
Behdad Esfahbod 已提交
329
    return context_lookup (c,
330
			   inputCount, input,
B
Minor  
Behdad Esfahbod 已提交
331
			   lookupCount, lookupRecord,
B
Behdad Esfahbod 已提交
332
			   lookup_context);
333 334
  }

B
Behdad Esfahbod 已提交
335
  public:
B
Behdad Esfahbod 已提交
336
  inline bool sanitize (hb_sanitize_context_t *c) {
337
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
338 339 340
    return inputCount.sanitize (c)
	&& lookupCount.sanitize (c)
	&& c->check_range (input,
B
Behdad Esfahbod 已提交
341 342
				 input[0].static_size * inputCount
				 + lookupRecordX[0].static_size * lookupCount);
B
Behdad Esfahbod 已提交
343 344
  }

345
  private:
346
  USHORT	inputCount;		/* Total number of glyphs in input
347 348
					 * glyph sequence--includes the  first
					 * glyph */
349
  USHORT	lookupCount;		/* Number of LookupRecords */
B
Behdad Esfahbod 已提交
350
  USHORT	input[VAR];		/* Array of match inputs--start with
351
					 * second glyph */
B
Behdad Esfahbod 已提交
352
  LookupRecord	lookupRecordX[VAR];	/* Array of LookupRecords--in
353
					 * design order */
B
Behdad Esfahbod 已提交
354
  public:
355
  DEFINE_SIZE_ARRAY2 (4, input, lookupRecordX);
356 357
};

B
Behdad Esfahbod 已提交
358 359
struct RuleSet
{
B
Behdad Esfahbod 已提交
360
  inline bool apply (hb_apply_context_t *c, ContextLookupContext &lookup_context) const
B
Behdad Esfahbod 已提交
361
  {
362
    TRACE_APPLY ();
363
    unsigned int num_rules = rule.len;
B
Behdad Esfahbod 已提交
364 365
    for (unsigned int i = 0; i < num_rules; i++)
    {
B
Behdad Esfahbod 已提交
366
      if ((this+rule[i]).apply (c, lookup_context))
367 368 369 370 371 372
        return true;
    }

    return false;
  }

B
Behdad Esfahbod 已提交
373
  inline bool sanitize (hb_sanitize_context_t *c) {
374
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
375
    return rule.sanitize (c, this);
B
Behdad Esfahbod 已提交
376 377
  }

378 379
  private:
  OffsetArrayOf<Rule>
380
		rule;			/* Array of Rule tables
381
					 * ordered by preference */
B
Behdad Esfahbod 已提交
382
  public:
383
  DEFINE_SIZE_ARRAY (2, rule);
384 385 386
};


B
Behdad Esfahbod 已提交
387 388
struct ContextFormat1
{
389 390 391
  friend struct Context;

  private:
B
Behdad Esfahbod 已提交
392
  inline bool apply (hb_apply_context_t *c, apply_lookup_func_t apply_func) const
B
Behdad Esfahbod 已提交
393
  {
394
    TRACE_APPLY ();
395
    unsigned int index = (this+coverage) (c->buffer->info[c->buffer->idx].codepoint);
396
    if (likely (index == NOT_COVERED))
397 398
      return false;

399
    const RuleSet &rule_set = this+ruleSet[index];
B
Behdad Esfahbod 已提交
400
    struct ContextLookupContext lookup_context = {
401 402
      {match_glyph, apply_func},
      NULL
403
    };
B
Behdad Esfahbod 已提交
404
    return rule_set.apply (c, lookup_context);
405 406
  }

B
Behdad Esfahbod 已提交
407
  inline bool sanitize (hb_sanitize_context_t *c) {
408
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
409 410
    return coverage.sanitize (c, this)
	&& ruleSet.sanitize (c, this);
B
Behdad Esfahbod 已提交
411 412
  }

413 414 415 416 417 418 419 420
  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 */
421
  public:
422
  DEFINE_SIZE_ARRAY (6, ruleSet);
423 424 425
};


B
Behdad Esfahbod 已提交
426 427
struct ContextFormat2
{
428 429 430
  friend struct Context;

  private:
B
Behdad Esfahbod 已提交
431
  inline bool apply (hb_apply_context_t *c, apply_lookup_func_t apply_func) const
B
Behdad Esfahbod 已提交
432
  {
433
    TRACE_APPLY ();
434
    unsigned int index = (this+coverage) (c->buffer->info[c->buffer->idx].codepoint);
435
    if (likely (index == NOT_COVERED))
436 437 438
      return false;

    const ClassDef &class_def = this+classDef;
439
    index = class_def (c->buffer->info[c->buffer->idx].codepoint);
440
    const RuleSet &rule_set = this+ruleSet[index];
B
Behdad Esfahbod 已提交
441
    struct ContextLookupContext lookup_context = {
B
Behdad Esfahbod 已提交
442 443
      {match_class, apply_func},
      &class_def
444
    };
B
Behdad Esfahbod 已提交
445
    return rule_set.apply (c, lookup_context);
446 447
  }

B
Behdad Esfahbod 已提交
448
  inline bool sanitize (hb_sanitize_context_t *c) {
449
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
450 451 452
    return coverage.sanitize (c, this)
        && classDef.sanitize (c, this)
	&& ruleSet.sanitize (c, this);
B
Behdad Esfahbod 已提交
453 454
  }

455 456 457 458 459 460 461 462 463 464 465
  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 */
466
  public:
467
  DEFINE_SIZE_ARRAY (8, ruleSet);
468 469 470
};


B
Behdad Esfahbod 已提交
471 472
struct ContextFormat3
{
473 474 475
  friend struct Context;

  private:
B
Behdad Esfahbod 已提交
476
  inline bool apply (hb_apply_context_t *c, apply_lookup_func_t apply_func) const
B
Behdad Esfahbod 已提交
477
  {
478
    TRACE_APPLY ();
479
    unsigned int index = (this+coverage[0]) (c->buffer->info[c->buffer->idx].codepoint);
480
    if (likely (index == NOT_COVERED))
481
      return false;
482

B
Behdad Esfahbod 已提交
483
    const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (coverage, coverage[0].static_size * glyphCount);
B
Behdad Esfahbod 已提交
484
    struct ContextLookupContext lookup_context = {
485
      {match_coverage, apply_func},
B
Behdad Esfahbod 已提交
486
      this
487
    };
B
Behdad Esfahbod 已提交
488
    return context_lookup (c,
489
			   glyphCount, (const USHORT *) (coverage + 1),
B
Minor  
Behdad Esfahbod 已提交
490
			   lookupCount, lookupRecord,
B
Behdad Esfahbod 已提交
491
			   lookup_context);
492 493
  }

B
Behdad Esfahbod 已提交
494
  inline bool sanitize (hb_sanitize_context_t *c) {
495
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
496
    if (!c->check_struct (this)) return false;
B
Behdad Esfahbod 已提交
497
    unsigned int count = glyphCount;
B
Behdad Esfahbod 已提交
498
    if (!c->check_array (coverage, coverage[0].static_size, count)) return false;
B
Behdad Esfahbod 已提交
499
    for (unsigned int i = 0; i < count; i++)
B
Behdad Esfahbod 已提交
500
      if (!coverage[i].sanitize (c, this)) return false;
B
Behdad Esfahbod 已提交
501
    LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (coverage, coverage[0].static_size * count);
B
Behdad Esfahbod 已提交
502
    return c->check_array (lookupRecord, lookupRecord[0].static_size, lookupCount);
B
Behdad Esfahbod 已提交
503 504
  }

505 506 507 508
  private:
  USHORT	format;			/* Format identifier--format = 3 */
  USHORT	glyphCount;		/* Number of glyphs in the input glyph
					 * sequence */
509
  USHORT	lookupCount;		/* Number of LookupRecords */
510
  OffsetTo<Coverage>
B
Behdad Esfahbod 已提交
511
		coverage[VAR];		/* Array of offsets to Coverage
512
					 * table in glyph sequence order */
B
Behdad Esfahbod 已提交
513
  LookupRecord	lookupRecordX[VAR];	/* Array of LookupRecords--in
514
					 * design order */
B
Behdad Esfahbod 已提交
515
  public:
516
  DEFINE_SIZE_ARRAY2 (6, coverage, lookupRecordX);
517 518
};

B
Behdad Esfahbod 已提交
519 520
struct Context
{
521
  protected:
B
Behdad Esfahbod 已提交
522
  inline bool apply (hb_apply_context_t *c, apply_lookup_func_t apply_func) const
B
Behdad Esfahbod 已提交
523
  {
524
    TRACE_APPLY ();
525
    switch (u.format) {
B
Behdad Esfahbod 已提交
526 527 528
    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);
529 530 531 532
    default:return false;
    }
  }

B
Behdad Esfahbod 已提交
533
  inline bool sanitize (hb_sanitize_context_t *c) {
534
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
535
    if (!u.format.sanitize (c)) return false;
B
Behdad Esfahbod 已提交
536
    switch (u.format) {
B
Behdad Esfahbod 已提交
537 538 539
    case 1: return u.format1.sanitize (c);
    case 2: return u.format2.sanitize (c);
    case 3: return u.format3.sanitize (c);
B
Behdad Esfahbod 已提交
540 541 542 543
    default:return true;
    }
  }

544 545
  private:
  union {
B
Behdad Esfahbod 已提交
546
  USHORT		format;		/* Format identifier */
B
Behdad Esfahbod 已提交
547 548 549
  ContextFormat1	format1;
  ContextFormat2	format2;
  ContextFormat3	format3;
550 551 552
  } u;
};

553 554 555

/* Chaining Contextual lookups */

B
Behdad Esfahbod 已提交
556 557
struct ChainContextLookupContext
{
558
  ContextFuncs funcs;
B
Behdad Esfahbod 已提交
559
  const void *match_data[3];
560 561
};

B
Behdad Esfahbod 已提交
562
static inline bool chain_context_lookup (hb_apply_context_t *c,
563
					 unsigned int backtrackCount,
564
					 const USHORT backtrack[],
565
					 unsigned int inputCount, /* Including the first glyph (not matched) */
566
					 const USHORT input[], /* Array of input values--start with second glyph */
567
					 unsigned int lookaheadCount,
568
					 const USHORT lookahead[],
569
					 unsigned int lookupCount,
570
					 const LookupRecord lookupRecord[],
B
Behdad Esfahbod 已提交
571
					 ChainContextLookupContext &lookup_context)
572
{
573
  /* First guess */
574
  if (unlikely (c->buffer->backtrack_len () < backtrackCount ||
575
		c->buffer->idx + inputCount + lookaheadCount > c->buffer->len ||
B
Behdad Esfahbod 已提交
576
		inputCount + lookaheadCount > c->context_length))
577 578
    return false;

B
Behdad Esfahbod 已提交
579 580
  hb_apply_context_t new_context = *c;
  return match_backtrack (c,
B
Behdad Esfahbod 已提交
581 582
			  backtrackCount, backtrack,
			  lookup_context.funcs.match, lookup_context.match_data[0])
B
Behdad Esfahbod 已提交
583
      && match_input (c,
B
Behdad Esfahbod 已提交
584 585 586
		      inputCount, input,
		      lookup_context.funcs.match, lookup_context.match_data[1],
		      &new_context.context_length)
B
Behdad Esfahbod 已提交
587
      && match_lookahead (c,
B
Behdad Esfahbod 已提交
588 589 590 591 592 593 594
			  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);
595
}
596

B
Behdad Esfahbod 已提交
597 598
struct ChainRule
{
599 600 601
  friend struct ChainRuleSet;

  private:
B
Behdad Esfahbod 已提交
602
  inline bool apply (hb_apply_context_t *c, ChainContextLookupContext &lookup_context) const
B
Behdad Esfahbod 已提交
603
  {
604
    TRACE_APPLY ();
605 606 607
    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 已提交
608
    return chain_context_lookup (c,
B
Behdad Esfahbod 已提交
609 610 611 612
				 backtrack.len, backtrack.array,
				 input.len, input.array,
				 lookahead.len, lookahead.array,
				 lookup.len, lookup.array,
B
Behdad Esfahbod 已提交
613
				 lookup_context);
614 615
  }

B
Behdad Esfahbod 已提交
616
  public:
B
Behdad Esfahbod 已提交
617
  inline bool sanitize (hb_sanitize_context_t *c) {
618
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
619
    if (!backtrack.sanitize (c)) return false;
620
    HeadlessArrayOf<USHORT> &input = StructAfter<HeadlessArrayOf<USHORT> > (backtrack);
B
Behdad Esfahbod 已提交
621
    if (!input.sanitize (c)) return false;
622
    ArrayOf<USHORT> &lookahead = StructAfter<ArrayOf<USHORT> > (input);
B
Behdad Esfahbod 已提交
623
    if (!lookahead.sanitize (c)) return false;
624
    ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
B
Behdad Esfahbod 已提交
625
    return lookup.sanitize (c);
B
Behdad Esfahbod 已提交
626
  }
627 628

  private:
B
Behdad Esfahbod 已提交
629 630
  ArrayOf<USHORT>
		backtrack;		/* Array of backtracking values
631 632
					 * (to be matched before the input
					 * sequence) */
B
Behdad Esfahbod 已提交
633 634
  HeadlessArrayOf<USHORT>
		inputX;			/* Array of input values (start with
635
					 * second glyph) */
B
Behdad Esfahbod 已提交
636 637
  ArrayOf<USHORT>
		lookaheadX;		/* Array of lookahead values's (to be
638
					 * matched after the input sequence) */
B
Behdad Esfahbod 已提交
639
  ArrayOf<LookupRecord>
640
		lookupX;		/* Array of LookupRecords--in
641
					 * design order) */
642
  public:
B
Behdad Esfahbod 已提交
643
  DEFINE_SIZE_MIN (8);
644 645
};

B
Behdad Esfahbod 已提交
646 647
struct ChainRuleSet
{
B
Behdad Esfahbod 已提交
648
  inline bool apply (hb_apply_context_t *c, ChainContextLookupContext &lookup_context) const
B
Behdad Esfahbod 已提交
649
  {
650
    TRACE_APPLY ();
651
    unsigned int num_rules = rule.len;
B
Behdad Esfahbod 已提交
652 653
    for (unsigned int i = 0; i < num_rules; i++)
    {
B
Behdad Esfahbod 已提交
654
      if ((this+rule[i]).apply (c, lookup_context))
655 656 657 658 659
        return true;
    }

    return false;
  }
660

B
Behdad Esfahbod 已提交
661
  inline bool sanitize (hb_sanitize_context_t *c) {
662
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
663
    return rule.sanitize (c, this);
B
Behdad Esfahbod 已提交
664 665
  }

666
  private:
667 668 669
  OffsetArrayOf<ChainRule>
		rule;			/* Array of ChainRule tables
					 * ordered by preference */
670
  public:
671
  DEFINE_SIZE_ARRAY (2, rule);
672 673
};

B
Behdad Esfahbod 已提交
674 675
struct ChainContextFormat1
{
676 677 678
  friend struct ChainContext;

  private:
B
Behdad Esfahbod 已提交
679
  inline bool apply (hb_apply_context_t *c, apply_lookup_func_t apply_func) const
B
Behdad Esfahbod 已提交
680
  {
681
    TRACE_APPLY ();
682
    unsigned int index = (this+coverage) (c->buffer->info[c->buffer->idx].codepoint);
683
    if (likely (index == NOT_COVERED))
684
      return false;
685

686
    const ChainRuleSet &rule_set = this+ruleSet[index];
B
Behdad Esfahbod 已提交
687
    struct ChainContextLookupContext lookup_context = {
688 689 690
      {match_glyph, apply_func},
      {NULL, NULL, NULL}
    };
B
Behdad Esfahbod 已提交
691
    return rule_set.apply (c, lookup_context);
692
  }
B
Behdad Esfahbod 已提交
693

B
Behdad Esfahbod 已提交
694
  inline bool sanitize (hb_sanitize_context_t *c) {
695
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
696 697
    return coverage.sanitize (c, this)
	&& ruleSet.sanitize (c, this);
B
Behdad Esfahbod 已提交
698 699
  }

700 701
  private:
  USHORT	format;			/* Format identifier--format = 1 */
702 703
  OffsetTo<Coverage>
		coverage;		/* Offset to Coverage table--from
704
					 * beginning of table */
705 706 707
  OffsetArrayOf<ChainRuleSet>
		ruleSet;		/* Array of ChainRuleSet tables
					 * ordered by Coverage Index */
708
  public:
709
  DEFINE_SIZE_ARRAY (6, ruleSet);
710 711
};

B
Behdad Esfahbod 已提交
712 713
struct ChainContextFormat2
{
714 715 716
  friend struct ChainContext;

  private:
B
Behdad Esfahbod 已提交
717
  inline bool apply (hb_apply_context_t *c, apply_lookup_func_t apply_func) const
B
Behdad Esfahbod 已提交
718
  {
719
    TRACE_APPLY ();
720
    unsigned int index = (this+coverage) (c->buffer->info[c->buffer->idx].codepoint);
721
    if (likely (index == NOT_COVERED))
722 723 724 725 726 727
      return false;

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

728
    index = input_class_def (c->buffer->info[c->buffer->idx].codepoint);
729
    const ChainRuleSet &rule_set = this+ruleSet[index];
B
Behdad Esfahbod 已提交
730
    struct ChainContextLookupContext lookup_context = {
B
Behdad Esfahbod 已提交
731 732 733 734
      {match_class, apply_func},
      {&backtrack_class_def,
       &input_class_def,
       &lookahead_class_def}
735
    };
B
Behdad Esfahbod 已提交
736
    return rule_set.apply (c, lookup_context);
737 738
  }

B
Behdad Esfahbod 已提交
739
  inline bool sanitize (hb_sanitize_context_t *c) {
740
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
741 742 743 744 745
    return coverage.sanitize (c, this)
	&& backtrackClassDef.sanitize (c, this)
	&& inputClassDef.sanitize (c, this)
	&& lookaheadClassDef.sanitize (c, this)
	&& ruleSet.sanitize (c, this);
B
Behdad Esfahbod 已提交
746 747
  }

748 749
  private:
  USHORT	format;			/* Format identifier--format = 2 */
750 751
  OffsetTo<Coverage>
		coverage;		/* Offset to Coverage table--from
752
					 * beginning of table */
753 754
  OffsetTo<ClassDef>
		backtrackClassDef;	/* Offset to glyph ClassDef table
755 756
					 * containing backtrack sequence
					 * data--from beginning of table */
757 758
  OffsetTo<ClassDef>
		inputClassDef;		/* Offset to glyph ClassDef
759 760
					 * table containing input sequence
					 * data--from beginning of table */
761 762
  OffsetTo<ClassDef>
		lookaheadClassDef;	/* Offset to glyph ClassDef table
763 764
					 * containing lookahead sequence
					 * data--from beginning of table */
765 766 767
  OffsetArrayOf<ChainRuleSet>
		ruleSet;		/* Array of ChainRuleSet tables
					 * ordered by class */
768
  public:
769
  DEFINE_SIZE_ARRAY (12, ruleSet);
770 771
};

B
Behdad Esfahbod 已提交
772 773
struct ChainContextFormat3
{
774 775 776
  friend struct ChainContext;

  private:
777

B
Behdad Esfahbod 已提交
778
  inline bool apply (hb_apply_context_t *c, apply_lookup_func_t apply_func) const
B
Behdad Esfahbod 已提交
779
  {
780
    TRACE_APPLY ();
781
    const OffsetArrayOf<Coverage> &input = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
782

783
    unsigned int index = (this+input[0]) (c->buffer->info[c->buffer->idx].codepoint);
784
    if (likely (index == NOT_COVERED))
785 786
      return false;

787 788
    const OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (input);
    const ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
B
Behdad Esfahbod 已提交
789
    struct ChainContextLookupContext lookup_context = {
790
      {match_coverage, apply_func},
B
Behdad Esfahbod 已提交
791
      {this, this, this}
792
    };
B
Behdad Esfahbod 已提交
793
    return chain_context_lookup (c,
B
Behdad Esfahbod 已提交
794 795 796 797
				 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 已提交
798
				 lookup_context);
799 800
  }

B
Behdad Esfahbod 已提交
801
  inline bool sanitize (hb_sanitize_context_t *c) {
802
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
803
    if (!backtrack.sanitize (c, this)) return false;
804
    OffsetArrayOf<Coverage> &input = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
B
Behdad Esfahbod 已提交
805
    if (!input.sanitize (c, this)) return false;
806
    OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (input);
B
Behdad Esfahbod 已提交
807
    if (!lookahead.sanitize (c, this)) return false;
808
    ArrayOf<LookupRecord> &lookup = StructAfter<ArrayOf<LookupRecord> > (lookahead);
B
Behdad Esfahbod 已提交
809
    return lookup.sanitize (c);
B
Behdad Esfahbod 已提交
810 811
  }

812 813
  private:
  USHORT	format;			/* Format identifier--format = 3 */
B
Behdad Esfahbod 已提交
814
  OffsetArrayOf<Coverage>
815
		backtrack;		/* Array of coverage tables
816 817
					 * in backtracking sequence, in  glyph
					 * sequence order */
B
Behdad Esfahbod 已提交
818
  OffsetArrayOf<Coverage>
819
		inputX		;	/* Array of coverage
820 821
					 * tables in input sequence, in glyph
					 * sequence order */
B
Behdad Esfahbod 已提交
822
  OffsetArrayOf<Coverage>
823
		lookaheadX;		/* Array of coverage tables
824 825
					 * in lookahead sequence, in glyph
					 * sequence order */
B
Behdad Esfahbod 已提交
826
  ArrayOf<LookupRecord>
827
		lookupX;		/* Array of LookupRecords--in
B
Behdad Esfahbod 已提交
828
					 * design order) */
829
  public:
B
Behdad Esfahbod 已提交
830
  DEFINE_SIZE_MIN (10);
831 832
};

B
Behdad Esfahbod 已提交
833 834
struct ChainContext
{
835
  protected:
B
Behdad Esfahbod 已提交
836
  inline bool apply (hb_apply_context_t *c, apply_lookup_func_t apply_func) const
B
Behdad Esfahbod 已提交
837
  {
838
    TRACE_APPLY ();
839
    switch (u.format) {
B
Behdad Esfahbod 已提交
840 841 842
    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);
843 844 845 846
    default:return false;
    }
  }

B
Behdad Esfahbod 已提交
847
  inline bool sanitize (hb_sanitize_context_t *c) {
848
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
849
    if (!u.format.sanitize (c)) return false;
B
Behdad Esfahbod 已提交
850
    switch (u.format) {
B
Behdad Esfahbod 已提交
851 852 853
    case 1: return u.format1.sanitize (c);
    case 2: return u.format2.sanitize (c);
    case 3: return u.format3.sanitize (c);
B
Behdad Esfahbod 已提交
854 855 856 857
    default:return true;
    }
  }

858 859 860
  private:
  union {
  USHORT		format;	/* Format identifier */
B
Behdad Esfahbod 已提交
861 862 863
  ChainContextFormat1	format1;
  ChainContextFormat2	format2;
  ChainContextFormat3	format3;
864 865 866 867
  } u;
};


868 869 870 871
struct ExtensionFormat1
{
  friend struct Extension;

B
Behdad Esfahbod 已提交
872
  protected:
873
  inline unsigned int get_type (void) const { return extensionLookupType; }
B
Behdad Esfahbod 已提交
874
  inline unsigned int get_offset (void) const { return extensionOffset; }
875

B
Behdad Esfahbod 已提交
876
  inline bool sanitize (hb_sanitize_context_t *c) {
877
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
878
    return c->check_struct (this);
B
Behdad Esfahbod 已提交
879 880
  }

881 882 883 884 885
  private:
  USHORT	format;			/* Format identifier. Set to 1. */
  USHORT	extensionLookupType;	/* Lookup type of subtable referenced
					 * by ExtensionOffset (i.e. the
					 * extension subtable). */
886 887
  ULONG		extensionOffset;	/* Offset to the extension subtable,
					 * of lookup type subtable. */
888 889
  public:
  DEFINE_SIZE_STATIC (8);
890 891 892 893 894 895 896
};

struct Extension
{
  inline unsigned int get_type (void) const
  {
    switch (u.format) {
B
Behdad Esfahbod 已提交
897
    case 1: return u.format1.get_type ();
898 899 900
    default:return 0;
    }
  }
B
Behdad Esfahbod 已提交
901
  inline unsigned int get_offset (void) const
902 903
  {
    switch (u.format) {
B
Behdad Esfahbod 已提交
904
    case 1: return u.format1.get_offset ();
B
Behdad Esfahbod 已提交
905
    default:return 0;
906 907 908
    }
  }

B
Behdad Esfahbod 已提交
909
  inline bool sanitize (hb_sanitize_context_t *c) {
910
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
911
    if (!u.format.sanitize (c)) return false;
B
Behdad Esfahbod 已提交
912
    switch (u.format) {
B
Behdad Esfahbod 已提交
913
    case 1: return u.format1.sanitize (c);
B
Behdad Esfahbod 已提交
914 915 916 917
    default:return true;
    }
  }

918 919 920
  private:
  union {
  USHORT		format;		/* Format identifier */
B
Behdad Esfahbod 已提交
921
  ExtensionFormat1	format1;
922 923 924 925
  } u;
};


B
Behdad Esfahbod 已提交
926 927 928 929
/*
 * GSUB/GPOS Common
 */

B
Behdad Esfahbod 已提交
930 931
struct GSUBGPOS
{
B
Behdad Esfahbod 已提交
932 933
  static const hb_tag_t GSUBTag	= HB_OT_TAG_GSUB;
  static const hb_tag_t GPOSTag	= HB_OT_TAG_GPOS;
B
Behdad Esfahbod 已提交
934

935 936 937 938
  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 已提交
939 940 941 942
  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); }
943 944 945 946 947 948 949 950 951
  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 已提交
952 953 954 955
  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); }
956 957 958 959 960 961 962 963 964
  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 已提交
965

B
Behdad Esfahbod 已提交
966
  inline bool sanitize (hb_sanitize_context_t *c) {
967
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
968 969 970 971
    return version.sanitize (c) && likely (version.major == 1)
	&& scriptList.sanitize (c, this)
	&& featureList.sanitize (c, this)
	&& lookupList.sanitize (c, this);
B
Behdad Esfahbod 已提交
972 973
  }

974
  protected:
B
Behdad Esfahbod 已提交
975
  FixedVersion	version;	/* Version of the GSUB/GPOS table--initially set
B
Behdad Esfahbod 已提交
976 977 978 979 980 981 982
				 * to 0x00010000 */
  OffsetTo<ScriptList>
		scriptList;  	/* ScriptList table */
  OffsetTo<FeatureList>
		featureList; 	/* FeatureList table */
  OffsetTo<LookupList>
		lookupList; 	/* LookupList table */
983 984
  public:
  DEFINE_SIZE_STATIC (10);
B
Behdad Esfahbod 已提交
985
};
986

987

B
Behdad Esfahbod 已提交
988 989
HB_END_DECLS

990
#endif /* HB_OT_LAYOUT_GSUBGPOS_PRIVATE_HH */