hb-ot-layout-gsubgpos-private.h 21.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/*
 * Copyright (C) 2007,2008,2009  Red Hat, Inc.
 *
 *  This is part of HarfBuzz, an OpenType Layout engine library.
 *
 * 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
 */

#ifndef HB_OT_LAYOUT_GSUBGPOS_PRIVATE_H
#define HB_OT_LAYOUT_GSUBGPOS_PRIVATE_H

B
Behdad Esfahbod 已提交
30
#include "hb-buffer-private.h"
31 32
#include "hb-ot-layout-gdef-private.h"

33

34
#define APPLY_ARG_DEF \
35 36 37 38
	hb_ot_layout_t *layout, \
	hb_buffer_t    *buffer, \
	unsigned int    context_length HB_GNUC_UNUSED, \
	unsigned int    nesting_level_left HB_GNUC_UNUSED, \
39 40
	unsigned int    lookup_flag, \
	unsigned int    property HB_GNUC_UNUSED /* propety of first glyph */
41
#define APPLY_ARG \
42 43 44 45
	layout, \
	buffer, \
	context_length, \
	nesting_level_left, \
46 47
	lookup_flag, \
	property
48 49 50


typedef bool (*match_func_t) (hb_codepoint_t glyph_id, const USHORT &value, char *data);
51
typedef bool (*apply_lookup_func_t) (APPLY_ARG_DEF, unsigned int lookup_index);
52

B
Behdad Esfahbod 已提交
53 54
struct ContextFuncs
{
55 56
  match_func_t match;
  apply_lookup_func_t apply;
57 58
};

59

B
Behdad Esfahbod 已提交
60 61
static inline bool match_glyph (hb_codepoint_t glyph_id, const USHORT &value, char *data)
{
62 63
  return glyph_id == value;
}
64

B
Behdad Esfahbod 已提交
65 66
static inline bool match_class (hb_codepoint_t glyph_id, const USHORT &value, char *data)
{
67
  const ClassDef &class_def = *(const ClassDef *)data;
68 69 70
  return class_def.get_class (glyph_id) == value;
}

B
Behdad Esfahbod 已提交
71 72
static inline bool match_coverage (hb_codepoint_t glyph_id, const USHORT &value, char *data)
{
73
  const OffsetTo<Coverage> &coverage = (const OffsetTo<Coverage>&)value;
74 75 76
  return (data+coverage) (glyph_id) != NOT_COVERED;
}

77

78
static inline bool match_input (APPLY_ARG_DEF,
B
Behdad Esfahbod 已提交
79 80 81 82 83 84 85
				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,
				char *match_data,
				unsigned int *context_length_out)
{
  unsigned int i, j;
B
Behdad Esfahbod 已提交
86 87 88
  unsigned int end = MIN (buffer->in_length, buffer->in_pos + context_length);
  if (HB_UNLIKELY (buffer->in_pos + count > end))
    return false;
B
Behdad Esfahbod 已提交
89

B
Behdad Esfahbod 已提交
90 91
  for (i = 1, j = buffer->in_pos + 1; i < count; i++, j++)
  {
B
Behdad Esfahbod 已提交
92
    while (!_hb_ot_layout_check_glyph_property (layout, IN_INFO (j), lookup_flag, &property))
B
Behdad Esfahbod 已提交
93
    {
B
Behdad Esfahbod 已提交
94
      if (HB_UNLIKELY (j + count - i == end))
B
Behdad Esfahbod 已提交
95 96 97 98 99 100 101 102 103 104 105 106 107
	return false;
      j++;
    }

    if (HB_LIKELY (!match_func (IN_GLYPH(j), input[i - 1], match_data)))
      return false;
  }

  *context_length_out = j - buffer->in_pos;

  return true;
}

108
static inline bool match_backtrack (APPLY_ARG_DEF,
109 110 111 112 113
				    unsigned int count,
				    const USHORT backtrack[],
				    match_func_t match_func,
				    char *match_data)
{
B
Behdad Esfahbod 已提交
114 115
  if (HB_UNLIKELY (buffer->out_pos < count))
    return false;
116

B
Behdad Esfahbod 已提交
117 118
  for (unsigned int i = 0, j = buffer->out_pos - 1; i < count; i++, j--)
  {
B
Behdad Esfahbod 已提交
119
    while (!_hb_ot_layout_check_glyph_property (layout, OUT_INFO (j), lookup_flag, &property))
B
Behdad Esfahbod 已提交
120
    {
121 122 123 124 125
      if (HB_UNLIKELY (j + 1 == count - i))
	return false;
      j--;
    }

B
Behdad Esfahbod 已提交
126
    if (HB_LIKELY (!match_func (OUT_GLYPH(j), backtrack[i], match_data)))
127 128 129 130 131 132
      return false;
  }

  return true;
}

133
static inline bool match_lookahead (APPLY_ARG_DEF,
B
Behdad Esfahbod 已提交
134 135 136 137 138
				    unsigned int count,
				    const USHORT lookahead[],
				    match_func_t match_func,
				    char *match_data,
				    unsigned int offset)
139
{
140
  unsigned int i, j;
B
Behdad Esfahbod 已提交
141 142 143
  unsigned int end = MIN (buffer->in_length, buffer->in_pos + context_length);
  if (HB_UNLIKELY (buffer->in_pos + offset + count > end))
    return false;
144

B
Behdad Esfahbod 已提交
145 146
  for (i = 0, j = buffer->in_pos + offset; i < count; i++, j++)
  {
B
Behdad Esfahbod 已提交
147
    while (!_hb_ot_layout_check_glyph_property (layout, OUT_INFO (j), lookup_flag, &property))
B
Behdad Esfahbod 已提交
148
    {
B
Behdad Esfahbod 已提交
149
      if (HB_UNLIKELY (j + count - i == end))
150 151 152 153
	return false;
      j++;
    }

B
Behdad Esfahbod 已提交
154
    if (HB_LIKELY (!match_func (IN_GLYPH(j), lookahead[i], match_data)))
155 156 157
      return false;
  }

158 159 160 161
  return true;
}


B
Behdad Esfahbod 已提交
162 163
struct LookupRecord
{
164 165 166 167 168 169 170
  USHORT	sequenceIndex;		/* Index into current glyph
					 * sequence--first glyph = 0 */
  USHORT	lookupListIndex;	/* Lookup to apply to that
					 * position--zero--based */
};
ASSERT_SIZE (LookupRecord, 4);

171
static inline bool apply_lookup (APPLY_ARG_DEF,
172 173
				 unsigned int count, /* Including the first glyph */
				 unsigned int lookupCount,
174 175 176
				 const LookupRecord lookupRecord[], /* Array of LookupRecords--in design order */
				 apply_lookup_func_t apply_func)
{
177 178 179
  unsigned int end = MIN (buffer->in_length, buffer->in_pos + context_length);
  if (HB_UNLIKELY (buffer->in_pos + count > end))
    return false;
180

B
Behdad Esfahbod 已提交
181 182
  /* TODO We don't support lookupRecord arrays that are not increasing:
   *      Should be easy for in_place ones at least. */
B
Behdad Esfahbod 已提交
183 184
  for (unsigned int i = 0; i < count; i++)
  {
B
Behdad Esfahbod 已提交
185
    while (!_hb_ot_layout_check_glyph_property (layout, IN_CURINFO (), lookup_flag, &property))
B
Behdad Esfahbod 已提交
186
    {
187 188 189 190 191 192
      if (HB_UNLIKELY (buffer->in_pos == end))
	return true;
      /* No lookup applied for this index */
      _hb_buffer_next_glyph (buffer);
    }

B
Behdad Esfahbod 已提交
193
    if (lookupCount && i == lookupRecord->sequenceIndex)
194 195 196 197
    {
      unsigned int old_pos = buffer->in_pos;

      /* Apply a lookup */
198
      bool done = apply_func (APPLY_ARG, lookupRecord->lookupListIndex);
199

B
Behdad Esfahbod 已提交
200 201
      lookupRecord++;
      lookupCount--;
202
      i += buffer->in_pos - old_pos;
203 204
      if (HB_UNLIKELY (buffer->in_pos == end))
	return true;
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219

      if (!done)
	goto not_applied;
    }
    else
    {
    not_applied:
      /* No lookup applied for this index */
      _hb_buffer_next_glyph (buffer);
      i++;
    }
  }

  return true;
}
220

221 222 223

/* Contextual lookups */

B
Behdad Esfahbod 已提交
224 225
struct ContextLookupContext
{
226 227 228 229
  ContextFuncs funcs;
  char *match_data;
};

230
static inline bool context_lookup (APPLY_ARG_DEF,
231
				   unsigned int inputCount, /* Including the first glyph (not matched) */
232
				   const USHORT input[], /* Array of input values--start with second glyph */
233
				   unsigned int lookupCount,
234
				   const LookupRecord lookupRecord[],
235 236
				   ContextLookupContext &context)
{
237
  return match_input (APPLY_ARG,
238 239 240
		      inputCount, input,
		      context.funcs.match, context.match_data,
		      &context_length) &&
241
	 apply_lookup (APPLY_ARG,
242 243 244 245 246
		       inputCount,
		       lookupCount, lookupRecord,
		       context.funcs.apply);
}

B
Behdad Esfahbod 已提交
247 248
struct Rule
{
249 250 251
  friend struct RuleSet;

  private:
B
Behdad Esfahbod 已提交
252 253
  inline bool apply (APPLY_ARG_DEF, ContextLookupContext &context) const
  {
254 255 256
    const LookupRecord *lookupRecord = (const LookupRecord *)
				       ((const char *) input +
					sizeof (input[0]) * (inputCount ? inputCount - 1 : 0));
257
    return context_lookup (APPLY_ARG,
258 259
			   inputCount, input,
			   lookupCount, lookupRecord,
260 261 262 263
			   context);
  }

  private:
264
  USHORT	inputCount;		/* Total number of glyphs in input
265 266
					 * glyph sequence--includes the  first
					 * glyph */
267 268
  USHORT	lookupCount;		/* Number of LookupRecords */
  USHORT	input[];		/* Array of match inputs--start with
269
					 * second glyph */
270
  LookupRecord	lookupRecordX[];	/* Array of LookupRecords--in
271 272 273 274
					 * design order */
};
ASSERT_SIZE (Rule, 4);

B
Behdad Esfahbod 已提交
275 276 277 278
struct RuleSet
{
  inline bool apply (APPLY_ARG_DEF, ContextLookupContext &context) const
  {
279
    unsigned int num_rules = rule.len;
B
Behdad Esfahbod 已提交
280 281
    for (unsigned int i = 0; i < num_rules; i++)
    {
282
      if ((this+rule[i]).apply (APPLY_ARG, context))
283 284 285 286 287 288 289 290
        return true;
    }

    return false;
  }

  private:
  OffsetArrayOf<Rule>
291
		rule;			/* Array of Rule tables
292 293 294 295
					 * ordered by preference */
};


B
Behdad Esfahbod 已提交
296 297
struct ContextFormat1
{
298 299 300
  friend struct Context;

  private:
B
Behdad Esfahbod 已提交
301 302
  inline bool apply (APPLY_ARG_DEF, apply_lookup_func_t apply_func) const
  {
303
    unsigned int index = (this+coverage) (IN_CURGLYPH ());
B
Behdad Esfahbod 已提交
304
    if (HB_LIKELY (index == NOT_COVERED))
305 306
      return false;

307 308
    const RuleSet &rule_set = this+ruleSet[index];
    struct ContextLookupContext context = {
309 310
      {match_glyph, apply_func},
      NULL
311
    };
312
    return rule_set.apply (APPLY_ARG, context);
313 314 315 316 317 318 319 320 321 322 323 324 325 326
  }

  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 */
};
ASSERT_SIZE (ContextFormat1, 6);


B
Behdad Esfahbod 已提交
327 328
struct ContextFormat2
{
329 330 331
  friend struct Context;

  private:
B
Behdad Esfahbod 已提交
332 333
  inline bool apply (APPLY_ARG_DEF, apply_lookup_func_t apply_func) const
  {
334
    unsigned int index = (this+coverage) (IN_CURGLYPH ());
B
Behdad Esfahbod 已提交
335
    if (HB_LIKELY (index == NOT_COVERED))
336 337 338 339
      return false;

    const ClassDef &class_def = this+classDef;
    index = class_def (IN_CURGLYPH ());
340 341 342 343 344
    const RuleSet &rule_set = this+ruleSet[index];
    /* LONGTERMTODO: Old code fetches glyph classes at most once and caches
     * them across subrule lookups.  Not sure it's worth it.
     */
    struct ContextLookupContext context = {
345
     {match_class, apply_func},
346
      (char *) &class_def
347
    };
348
    return rule_set.apply (APPLY_ARG, context);
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
  }

  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 */
};
ASSERT_SIZE (ContextFormat2, 8);


B
Behdad Esfahbod 已提交
366 367
struct ContextFormat3
{
368 369 370
  friend struct Context;

  private:
B
Behdad Esfahbod 已提交
371 372
  inline bool apply (APPLY_ARG_DEF, apply_lookup_func_t apply_func) const
  {
373
    unsigned int index = (this+coverage[0]) (IN_CURGLYPH ());
B
Behdad Esfahbod 已提交
374
    if (HB_LIKELY (index == NOT_COVERED))
375
      return false;
376

377 378 379
    const LookupRecord *lookupRecord = (const LookupRecord *)
				       ((const char *) coverage +
					sizeof (coverage[0]) * glyphCount);
380
    struct ContextLookupContext context = {
381 382
      {match_coverage, apply_func},
      (char *) this
383
    };
384
    return context_lookup (APPLY_ARG,
385 386
			   glyphCount, (const USHORT *) (coverage + 1),
			   lookupCount, lookupRecord,
387 388 389 390 391 392 393
			   context);
  }

  private:
  USHORT	format;			/* Format identifier--format = 3 */
  USHORT	glyphCount;		/* Number of glyphs in the input glyph
					 * sequence */
394
  USHORT	lookupCount;		/* Number of LookupRecords */
395 396 397 398
  OffsetTo<Coverage>
		coverage[];		/* Array of offsets to Coverage
					 * table in glyph sequence order */
  LookupRecord	lookupRecordX[];	/* Array of LookupRecords--in
399 400 401 402
					 * design order */
};
ASSERT_SIZE (ContextFormat3, 6);

B
Behdad Esfahbod 已提交
403 404
struct Context
{
405
  protected:
B
Behdad Esfahbod 已提交
406 407
  bool apply (APPLY_ARG_DEF, apply_lookup_func_t apply_func) const
  {
408
    switch (u.format) {
409 410 411
    case 1: return u.format1->apply (APPLY_ARG, apply_func);
    case 2: return u.format2->apply (APPLY_ARG, apply_func);
    case 3: return u.format3->apply (APPLY_ARG, apply_func);
412 413 414 415 416 417
    default:return false;
    }
  }

  private:
  union {
B
Behdad Esfahbod 已提交
418 419 420 421
  USHORT		format;		/* Format identifier */
  ContextFormat1	format1[];
  ContextFormat2	format2[];
  ContextFormat3	format3[];
422 423
  } u;
};
B
Behdad Esfahbod 已提交
424
ASSERT_SIZE (Context, 2);
425

426 427 428

/* Chaining Contextual lookups */

B
Behdad Esfahbod 已提交
429 430
struct ChainContextLookupContext
{
431 432 433 434
  ContextFuncs funcs;
  char *match_data[3];
};

435
static inline bool chain_context_lookup (APPLY_ARG_DEF,
436
					 unsigned int backtrackCount,
437
					 const USHORT backtrack[],
438
					 unsigned int inputCount, /* Including the first glyph (not matched) */
439
					 const USHORT input[], /* Array of input values--start with second glyph */
440
					 unsigned int lookaheadCount,
441
					 const USHORT lookahead[],
442
					 unsigned int lookupCount,
443 444 445
					 const LookupRecord lookupRecord[],
					 ChainContextLookupContext &context)
{
446 447
  /* First guess */
  if (HB_UNLIKELY (buffer->out_pos < backtrackCount ||
448 449
		   buffer->in_pos + inputCount + lookaheadCount > buffer->in_length ||
		   inputCount + lookaheadCount > context_length))
450 451
    return false;

B
Behdad Esfahbod 已提交
452
  unsigned int offset;
453
  return match_backtrack (APPLY_ARG,
454 455
			  backtrackCount, backtrack,
			  context.funcs.match, context.match_data[0]) &&
456
	 match_input (APPLY_ARG,
457 458
		      inputCount, input,
		      context.funcs.match, context.match_data[1],
B
Behdad Esfahbod 已提交
459
		      &offset) &&
460
	 match_lookahead (APPLY_ARG,
B
Behdad Esfahbod 已提交
461 462 463 464
			  lookaheadCount, lookahead,
			  context.funcs.match, context.match_data[2],
			  offset) &&
	 (context_length = offset, true) &&
465
	 apply_lookup (APPLY_ARG,
466 467 468 469
		       inputCount,
		       lookupCount, lookupRecord,
		       context.funcs.apply);
}
470

B
Behdad Esfahbod 已提交
471 472
struct ChainRule
{
473 474 475
  friend struct ChainRuleSet;

  private:
B
Behdad Esfahbod 已提交
476 477
  inline bool apply (APPLY_ARG_DEF, ChainContextLookupContext &context) const
  {
B
Behdad Esfahbod 已提交
478 479 480 481 482 483
    const HeadlessArrayOf<USHORT> &input = *(const HeadlessArrayOf<USHORT>*)
					    ((const char *) &backtrack + backtrack.get_size ());
    const ArrayOf<USHORT> &lookahead = *(const ArrayOf<USHORT>*)
				        ((const char *) &input + input.get_size ());
    const ArrayOf<LookupRecord> &lookup = *(const ArrayOf<LookupRecord>*)
					   ((const char *) &lookahead + lookahead.get_size ());
484
    return chain_context_lookup (APPLY_ARG,
485 486 487 488
				 backtrack.len, backtrack.array,
				 input.len, input.array + 1,
				 lookahead.len, lookahead.array,
				 lookup.len, lookup.array,
489
				 context);
490
    return false;
491 492
  }

493 494

  private:
B
Behdad Esfahbod 已提交
495 496
  ArrayOf<USHORT>
		backtrack;		/* Array of backtracking values
497 498
					 * (to be matched before the input
					 * sequence) */
B
Behdad Esfahbod 已提交
499 500
  HeadlessArrayOf<USHORT>
		inputX;			/* Array of input values (start with
501
					 * second glyph) */
B
Behdad Esfahbod 已提交
502 503
  ArrayOf<USHORT>
		lookaheadX;		/* Array of lookahead values's (to be
504
					 * matched after the input sequence) */
B
Behdad Esfahbod 已提交
505
  ArrayOf<LookupRecord>
506
		lookupX;		/* Array of LookupRecords--in
507 508
					 * design order) */
};
509
ASSERT_SIZE (ChainRule, 8);
510

B
Behdad Esfahbod 已提交
511 512 513 514
struct ChainRuleSet
{
  inline bool apply (APPLY_ARG_DEF, ChainContextLookupContext &context) const
  {
515
    unsigned int num_rules = rule.len;
B
Behdad Esfahbod 已提交
516 517
    for (unsigned int i = 0; i < num_rules; i++)
    {
518
      if ((this+rule[i]).apply (APPLY_ARG, context))
519 520 521 522 523
        return true;
    }

    return false;
  }
524 525

  private:
526 527 528
  OffsetArrayOf<ChainRule>
		rule;			/* Array of ChainRule tables
					 * ordered by preference */
529
};
530
ASSERT_SIZE (ChainRuleSet, 2);
531

B
Behdad Esfahbod 已提交
532 533
struct ChainContextFormat1
{
534 535 536
  friend struct ChainContext;

  private:
B
Behdad Esfahbod 已提交
537 538
  inline bool apply (APPLY_ARG_DEF, apply_lookup_func_t apply_func) const
  {
539
    unsigned int index = (this+coverage) (IN_CURGLYPH ());
B
Behdad Esfahbod 已提交
540
    if (HB_LIKELY (index == NOT_COVERED))
541
      return false;
542

543 544 545 546 547
    const ChainRuleSet &rule_set = this+ruleSet[index];
    struct ChainContextLookupContext context = {
      {match_glyph, apply_func},
      {NULL, NULL, NULL}
    };
548
    return rule_set.apply (APPLY_ARG, context);
549
  }
550 551
  private:
  USHORT	format;			/* Format identifier--format = 1 */
552 553
  OffsetTo<Coverage>
		coverage;		/* Offset to Coverage table--from
554
					 * beginning of table */
555 556 557
  OffsetArrayOf<ChainRuleSet>
		ruleSet;		/* Array of ChainRuleSet tables
					 * ordered by Coverage Index */
558 559 560
};
ASSERT_SIZE (ChainContextFormat1, 6);

B
Behdad Esfahbod 已提交
561 562
struct ChainContextFormat2
{
563 564 565
  friend struct ChainContext;

  private:
B
Behdad Esfahbod 已提交
566 567
  inline bool apply (APPLY_ARG_DEF, apply_lookup_func_t apply_func) const
  {
568
    unsigned int index = (this+coverage) (IN_CURGLYPH ());
B
Behdad Esfahbod 已提交
569
    if (HB_LIKELY (index == NOT_COVERED))
570 571 572 573 574 575 576 577
      return false;

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

    index = input_class_def (IN_CURGLYPH ());
    const ChainRuleSet &rule_set = this+ruleSet[index];
578 579 580
    /* LONGTERMTODO: Old code fetches glyph classes at most once and caches
     * them across subrule lookups.  Not sure it's worth it.
     */
581 582 583 584 585 586
    struct ChainContextLookupContext context = {
     {match_class, apply_func},
     {(char *) &backtrack_class_def,
      (char *) &input_class_def,
      (char *) &lookahead_class_def}
    };
587
    return rule_set.apply (APPLY_ARG, context);
588 589 590 591
  }

  private:
  USHORT	format;			/* Format identifier--format = 2 */
592 593
  OffsetTo<Coverage>
		coverage;		/* Offset to Coverage table--from
594
					 * beginning of table */
595 596
  OffsetTo<ClassDef>
		backtrackClassDef;	/* Offset to glyph ClassDef table
597 598
					 * containing backtrack sequence
					 * data--from beginning of table */
599 600
  OffsetTo<ClassDef>
		inputClassDef;		/* Offset to glyph ClassDef
601 602
					 * table containing input sequence
					 * data--from beginning of table */
603 604
  OffsetTo<ClassDef>
		lookaheadClassDef;	/* Offset to glyph ClassDef table
605 606
					 * containing lookahead sequence
					 * data--from beginning of table */
607 608 609
  OffsetArrayOf<ChainRuleSet>
		ruleSet;		/* Array of ChainRuleSet tables
					 * ordered by class */
610 611 612
};
ASSERT_SIZE (ChainContextFormat2, 12);

B
Behdad Esfahbod 已提交
613 614
struct ChainContextFormat3
{
615 616 617
  friend struct ChainContext;

  private:
618

B
Behdad Esfahbod 已提交
619 620
  inline bool apply (APPLY_ARG_DEF, apply_lookup_func_t apply_func) const
  {
B
Behdad Esfahbod 已提交
621 622
    const OffsetArrayOf<Coverage> &input = *(const OffsetArrayOf<Coverage>*)
					    ((const char *) &backtrack + backtrack.get_size ());
623 624

    unsigned int index = (this+input[0]) (IN_CURGLYPH ());
B
Behdad Esfahbod 已提交
625
    if (HB_LIKELY (index == NOT_COVERED))
626 627
      return false;

B
Behdad Esfahbod 已提交
628 629 630 631
    const OffsetArrayOf<Coverage> &lookahead = *(const OffsetArrayOf<Coverage>*)
					        ((const char *) &input + input.get_size ());
    const ArrayOf<LookupRecord> &lookup = *(const ArrayOf<LookupRecord>*)
					   ((const char *) &lookahead + lookahead.get_size ());
632 633 634 635
    struct ChainContextLookupContext context = {
      {match_coverage, apply_func},
      {(char *) this, (char *) this, (char *) this}
    };
636
    return chain_context_lookup (APPLY_ARG,
637 638 639 640
				 backtrack.len, (USHORT *) backtrack.array,
				 input.len, (USHORT *) input.array,
				 lookahead.len, (USHORT *) lookahead.array,
				 lookup.len, lookup.array,
641
				 context);
642 643 644
    return false;
  }

645 646
  private:
  USHORT	format;			/* Format identifier--format = 3 */
B
Behdad Esfahbod 已提交
647
  OffsetArrayOf<Coverage>
648
		backtrack;		/* Array of coverage tables
649 650
					 * in backtracking sequence, in  glyph
					 * sequence order */
B
Behdad Esfahbod 已提交
651
  OffsetArrayOf<Coverage>
652
		inputX		;	/* Array of coverage
653 654
					 * tables in input sequence, in glyph
					 * sequence order */
B
Behdad Esfahbod 已提交
655
  OffsetArrayOf<Coverage>
656
		lookaheadX;		/* Array of coverage tables
657 658
					 * in lookahead sequence, in glyph
					 * sequence order */
B
Behdad Esfahbod 已提交
659
  ArrayOf<LookupRecord>
660
		lookupX;		/* Array of LookupRecords--in
B
Behdad Esfahbod 已提交
661
					 * design order) */
662 663 664
};
ASSERT_SIZE (ChainContextFormat3, 10);

B
Behdad Esfahbod 已提交
665 666
struct ChainContext
{
667
  protected:
B
Behdad Esfahbod 已提交
668 669
  bool apply (APPLY_ARG_DEF, apply_lookup_func_t apply_func) const
  {
670
    switch (u.format) {
671 672 673
    case 1: return u.format1->apply (APPLY_ARG, apply_func);
    case 2: return u.format2->apply (APPLY_ARG, apply_func);
    case 3: return u.format3->apply (APPLY_ARG, apply_func);
674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
    default:return false;
    }
  }

  private:
  union {
  USHORT		format;	/* Format identifier */
  ChainContextFormat1	format1[];
  ChainContextFormat2	format2[];
  ChainContextFormat3	format3[];
  } u;
};
ASSERT_SIZE (ChainContext, 2);


689 690 691 692 693 694 695 696 697 698 699
struct ExtensionFormat1
{
  friend struct Extension;

  private:
  inline unsigned int get_type (void) const { return extensionLookupType; }
  inline unsigned int get_offset (void) const { return (extensionOffset[0] << 16) + extensionOffset[1]; }
  inline const LookupSubTable& get_subtable (void) const
  {
    unsigned int offset = get_offset ();
    if (HB_UNLIKELY (!offset)) return Null(LookupSubTable);
700
    return *(LookupSubTable*)(((char *) this) + offset);
701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740
  }

  private:
  USHORT	format;			/* Format identifier. Set to 1. */
  USHORT	extensionLookupType;	/* Lookup type of subtable referenced
					 * by ExtensionOffset (i.e. the
					 * extension subtable). */
  USHORT	extensionOffset[2];	/* Offset to the extension subtable,
					 * of lookup type subtable.
					 * Defined as two shorts to avoid
					 * alignment requirements. */
};
ASSERT_SIZE (ExtensionFormat1, 8);

struct Extension
{
  inline unsigned int get_type (void) const
  {
    switch (u.format) {
    case 1: return u.format1->get_type ();
    default:return 0;
    }
  }
  inline const LookupSubTable& get_subtable (void) const
  {
    switch (u.format) {
    case 1: return u.format1->get_subtable ();
    default:return Null(LookupSubTable);
    }
  }

  private:
  union {
  USHORT		format;		/* Format identifier */
  ExtensionFormat1	format1[];
  } u;
};
ASSERT_SIZE (Extension, 2);


B
Behdad Esfahbod 已提交
741 742 743 744
/*
 * GSUB/GPOS Common
 */

B
Behdad Esfahbod 已提交
745 746
struct GSUBGPOS
{
B
Behdad Esfahbod 已提交
747 748 749
  static const hb_tag_t GSUBTag		= HB_TAG ('G','S','U','B');
  static const hb_tag_t GPOSTag		= HB_TAG ('G','P','O','S');

750
  STATIC_DEFINE_GET_FOR_DATA_CHECK_MAJOR_VERSION (GSUBGPOS, 1);
B
Behdad Esfahbod 已提交
751 752 753 754 755 756 757 758 759

  DEFINE_TAG_LIST_INTERFACE (Script,  script );	/* get_script_count (), get_script (i), get_script_tag (i) */
  DEFINE_TAG_LIST_INTERFACE (Feature, feature);	/* get_feature_count(), get_feature(i), get_feature_tag(i) */
  DEFINE_LIST_INTERFACE     (Lookup,  lookup );	/* get_lookup_count (), get_lookup (i) */

  // LONGTERMTODO bsearch
  DEFINE_TAG_FIND_INTERFACE (Script,  script );	/* find_script_index (), get_script_by_tag (tag) */
  DEFINE_TAG_FIND_INTERFACE (Feature, feature);	/* find_feature_index(), get_feature_by_tag(tag) */

760
  protected:
B
Behdad Esfahbod 已提交
761
  FixedVersion	version;	/* Version of the GSUB/GPOS table--initially set
B
Behdad Esfahbod 已提交
762 763 764 765 766 767 768 769 770
				 * to 0x00010000 */
  OffsetTo<ScriptList>
		scriptList;  	/* ScriptList table */
  OffsetTo<FeatureList>
		featureList; 	/* FeatureList table */
  OffsetTo<LookupList>
		lookupList; 	/* LookupList table */
};
ASSERT_SIZE (GSUBGPOS, 10);
771

772

773
#endif /* HB_OT_LAYOUT_GSUBGPOS_PRIVATE_H */