hb-ot-layout-gsub-private.h 18.9 KB
Newer Older
B
Behdad Esfahbod 已提交
1
/*
2
 * Copyright (C) 2007,2008,2009  Red Hat, Inc.
B
Behdad Esfahbod 已提交
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
 *
 *  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_GSUB_PRIVATE_H
#define HB_OT_LAYOUT_GSUB_PRIVATE_H

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

32

B
Behdad Esfahbod 已提交
33 34
struct SingleSubstFormat1
{
B
Behdad Esfahbod 已提交
35 36 37
  friend struct SingleSubst;

  private:
B
Behdad Esfahbod 已提交
38 39 40
  inline bool apply (APPLY_ARG_DEF) const
  {
    hb_codepoint_t glyph_id = IN_CURGLYPH ();
B
Behdad Esfahbod 已提交
41
    unsigned int index = (this+coverage) (glyph_id);
B
Behdad Esfahbod 已提交
42
    if (HB_LIKELY (index == NOT_COVERED))
B
Behdad Esfahbod 已提交
43 44 45
      return false;

    glyph_id += deltaGlyphID;
B
Behdad Esfahbod 已提交
46 47 48 49 50 51 52
    _hb_buffer_replace_glyph (buffer, glyph_id);

    if ( _hb_ot_layout_has_new_glyph_classes (layout) )
    {
      /* we inherit the old glyph class to the substituted glyph */
      _hb_ot_layout_set_glyph_property (layout, glyph_id, property);
    }
B
Behdad Esfahbod 已提交
53 54

    return true;
55
  }
B
Behdad Esfahbod 已提交
56

B
Behdad Esfahbod 已提交
57
  private:
B
Behdad Esfahbod 已提交
58
  USHORT	format;			/* Format identifier--format = 1 */
B
Behdad Esfahbod 已提交
59 60
  OffsetTo<Coverage>
		coverage;		/* Offset to Coverage table--from
B
Behdad Esfahbod 已提交
61 62 63 64 65 66
					 * beginning of Substitution table */
  SHORT		deltaGlyphID;		/* Add to original GlyphID to get
					 * substitute GlyphID */
};
ASSERT_SIZE (SingleSubstFormat1, 6);

B
Behdad Esfahbod 已提交
67 68
struct SingleSubstFormat2
{
69 70 71
  friend struct SingleSubst;

  private:
B
Behdad Esfahbod 已提交
72 73 74
  inline bool apply (APPLY_ARG_DEF) const
  {
    hb_codepoint_t glyph_id = IN_CURGLYPH ();
B
Behdad Esfahbod 已提交
75
    unsigned int index = (this+coverage) (glyph_id);
B
Behdad Esfahbod 已提交
76
    if (HB_LIKELY (index == NOT_COVERED))
77
      return false;
78

B
Behdad Esfahbod 已提交
79
    if (HB_UNLIKELY (index >= substitute.len))
80 81 82
      return false;

    glyph_id = substitute[index];
B
Behdad Esfahbod 已提交
83 84 85 86 87 88 89 90
    _hb_buffer_replace_glyph (buffer, glyph_id);

    if ( _hb_ot_layout_has_new_glyph_classes (layout) )
    {
      /* we inherit the old glyph class to the substituted glyph */
      _hb_ot_layout_set_glyph_property (layout, glyph_id, property);
    }

91 92
    return true;
  }
B
Behdad Esfahbod 已提交
93 94

  private:
B
Behdad Esfahbod 已提交
95
  USHORT	format;			/* Format identifier--format = 2 */
B
Behdad Esfahbod 已提交
96 97
  OffsetTo<Coverage>
		coverage;		/* Offset to Coverage table--from
B
Behdad Esfahbod 已提交
98
					 * beginning of Substitution table */
B
Behdad Esfahbod 已提交
99 100 101
  ArrayOf<GlyphID>
		substitute;		/* Array of substitute
					 * GlyphIDs--ordered by Coverage Index */
B
Behdad Esfahbod 已提交
102 103 104
};
ASSERT_SIZE (SingleSubstFormat2, 6);

B
Behdad Esfahbod 已提交
105 106
struct SingleSubst
{
107 108
  friend struct SubstLookupSubTable;

109
  private:
B
Behdad Esfahbod 已提交
110 111
  inline bool apply (APPLY_ARG_DEF) const
  {
B
Behdad Esfahbod 已提交
112
    switch (u.format) {
B
Behdad Esfahbod 已提交
113 114
    case 1: return u.format1->apply (APPLY_ARG);
    case 2: return u.format2->apply (APPLY_ARG);
B
Behdad Esfahbod 已提交
115 116 117 118
    default:return false;
    }
  }

119 120
  private:
  union {
B
Behdad Esfahbod 已提交
121 122 123
  USHORT		format;		/* Format identifier */
  SingleSubstFormat1	format1[];
  SingleSubstFormat2	format2[];
124
  } u;
B
Behdad Esfahbod 已提交
125
};
B
Behdad Esfahbod 已提交
126
ASSERT_SIZE (SingleSubst, 2);
127

B
Behdad Esfahbod 已提交
128

B
Behdad Esfahbod 已提交
129 130
struct Sequence
{
131 132 133
  friend struct MultipleSubstFormat1;

  private:
B
Behdad Esfahbod 已提交
134 135
  inline bool apply (APPLY_ARG_DEF) const
  {
B
Behdad Esfahbod 已提交
136
    if (HB_UNLIKELY (!substitute.len))
B
Behdad Esfahbod 已提交
137 138
      return false;

B
Behdad Esfahbod 已提交
139 140 141
    _hb_buffer_add_output_glyphs (buffer, 1,
				  substitute.len, (const uint16_t *) substitute.array,
				  0xFFFF, 0xFFFF);
B
Behdad Esfahbod 已提交
142 143 144 145 146 147 148

    if ( _hb_ot_layout_has_new_glyph_classes (layout) )
    {
      /* this is a guess only ... */
      if ( property == HB_OT_LAYOUT_GLYPH_CLASS_LIGATURE )
        property = HB_OT_LAYOUT_GLYPH_CLASS_BASE_GLYPH;

149 150 151
      unsigned int count = substitute.len;
      for (unsigned int n = 0; n < count; n++)
	_hb_ot_layout_set_glyph_property (layout, substitute[n], property);
B
Behdad Esfahbod 已提交
152 153 154 155 156
    }

    return true;
  }

B
Behdad Esfahbod 已提交
157
  private:
B
Behdad Esfahbod 已提交
158 159
  ArrayOf<GlyphID>
		substitute;		/* String of GlyphIDs to substitute */
B
Behdad Esfahbod 已提交
160
};
161
ASSERT_SIZE (Sequence, 2);
B
Behdad Esfahbod 已提交
162

B
Behdad Esfahbod 已提交
163 164
struct MultipleSubstFormat1
{
165 166 167
  friend struct MultipleSubst;

  private:
B
Behdad Esfahbod 已提交
168 169
  inline bool apply (APPLY_ARG_DEF) const
  {
170

B
Behdad Esfahbod 已提交
171
    unsigned int index = (this+coverage) (IN_CURGLYPH ());
B
Behdad Esfahbod 已提交
172
    if (HB_LIKELY (index == NOT_COVERED))
173 174
      return false;

175
    return (this+sequence[index]).apply (APPLY_ARG);
176
  }
B
Behdad Esfahbod 已提交
177 178

  private:
B
Behdad Esfahbod 已提交
179
  USHORT	format;			/* Format identifier--format = 1 */
B
Behdad Esfahbod 已提交
180 181
  OffsetTo<Coverage>
		coverage;		/* Offset to Coverage table--from
B
Behdad Esfahbod 已提交
182
					 * beginning of Substitution table */
B
Behdad Esfahbod 已提交
183 184 185
  OffsetArrayOf<Sequence>
		sequence;		/* Array of Sequence tables
					 * ordered by Coverage Index */
B
Behdad Esfahbod 已提交
186
};
187 188
ASSERT_SIZE (MultipleSubstFormat1, 6);

B
Behdad Esfahbod 已提交
189 190
struct MultipleSubst
{
191 192 193
  friend struct SubstLookupSubTable;

  private:
B
Behdad Esfahbod 已提交
194 195
  inline bool apply (APPLY_ARG_DEF) const
  {
B
Behdad Esfahbod 已提交
196
    switch (u.format) {
197
    case 1: return u.format1->apply (APPLY_ARG);
198 199 200 201 202 203
    default:return false;
    }
  }

  private:
  union {
B
Behdad Esfahbod 已提交
204 205
  USHORT		format;		/* Format identifier */
  MultipleSubstFormat1	format1[];
206 207
  } u;
};
B
Behdad Esfahbod 已提交
208
ASSERT_SIZE (MultipleSubst, 2);
209

B
Behdad Esfahbod 已提交
210

B
Behdad Esfahbod 已提交
211
typedef ArrayOf<GlyphID> AlternateSet;	/* Array of alternate GlyphIDs--in
B
Behdad Esfahbod 已提交
212
					 * arbitrary order */
213
ASSERT_SIZE (AlternateSet, 2);
B
Behdad Esfahbod 已提交
214

B
Behdad Esfahbod 已提交
215 216
struct AlternateSubstFormat1
{
217 218 219
  friend struct AlternateSubst;

  private:
B
Behdad Esfahbod 已提交
220 221
  inline bool apply (APPLY_ARG_DEF) const
  {
222
    hb_codepoint_t glyph_id = IN_CURGLYPH ();
223

B
Behdad Esfahbod 已提交
224
    unsigned int index = (this+coverage) (glyph_id);
B
Behdad Esfahbod 已提交
225
    if (HB_LIKELY (index == NOT_COVERED))
226 227
      return false;

B
Behdad Esfahbod 已提交
228
    const AlternateSet &alt_set = this+alternateSet[index];
229

B
Behdad Esfahbod 已提交
230
    if (HB_UNLIKELY (!alt_set.len))
231 232 233 234 235
      return false;

    unsigned int alt_index = 0;

    /* XXX callback to user to choose alternate
B
Behdad Esfahbod 已提交
236 237 238 239
    if (layout->altfunc)
      alt_index = (layout->altfunc)(layout, buffer,
				    buffer->out_pos, glyph_id,
				    alt_set.len, alt_set.array);
240 241
				   */

B
Behdad Esfahbod 已提交
242
    if (HB_UNLIKELY (alt_index >= alt_set.len))
243 244 245 246
      return false;

    glyph_id = alt_set[alt_index];

B
Behdad Esfahbod 已提交
247
    _hb_buffer_replace_glyph (buffer, glyph_id);
248 249 250 251 252 253 254 255 256

    if ( _hb_ot_layout_has_new_glyph_classes (layout) )
    {
      /* we inherit the old glyph class to the substituted glyph */
      _hb_ot_layout_set_glyph_property (layout, glyph_id, property);
    }

    return true;
  }
B
Behdad Esfahbod 已提交
257 258

  private:
B
Behdad Esfahbod 已提交
259
  USHORT	format;			/* Format identifier--format = 1 */
B
Behdad Esfahbod 已提交
260 261
  OffsetTo<Coverage>
		coverage;		/* Offset to Coverage table--from
B
Behdad Esfahbod 已提交
262
					 * beginning of Substitution table */
B
Behdad Esfahbod 已提交
263 264 265
  OffsetArrayOf<AlternateSet>
		alternateSet;		/* Array of AlternateSet tables
					 * ordered by Coverage Index */
B
Behdad Esfahbod 已提交
266
};
267 268
ASSERT_SIZE (AlternateSubstFormat1, 6);

B
Behdad Esfahbod 已提交
269 270
struct AlternateSubst
{
271 272 273
  friend struct SubstLookupSubTable;

  private:
B
Behdad Esfahbod 已提交
274 275
  inline bool apply (APPLY_ARG_DEF) const
  {
B
Behdad Esfahbod 已提交
276
    switch (u.format) {
277
    case 1: return u.format1->apply (APPLY_ARG);
278 279 280 281 282 283
    default:return false;
    }
  }

  private:
  union {
B
Behdad Esfahbod 已提交
284 285
  USHORT		format;		/* Format identifier */
  AlternateSubstFormat1	format1[];
286 287
  } u;
};
B
Behdad Esfahbod 已提交
288
ASSERT_SIZE (AlternateSubst, 2);
289

290

B
Behdad Esfahbod 已提交
291 292
struct Ligature
{
293 294 295
  friend struct LigatureSet;

  private:
B
Behdad Esfahbod 已提交
296 297
  inline bool apply (APPLY_ARG_DEF, bool is_mark) const
  {
298
    unsigned int i, j;
B
Behdad Esfahbod 已提交
299
    unsigned int count = component.len;
300 301 302
    unsigned int end = MIN (buffer->in_length, buffer->in_pos + context_length);
    if (HB_UNLIKELY (buffer->in_pos + count > end))
      return false;
303

B
Behdad Esfahbod 已提交
304 305
    for (i = 1, j = buffer->in_pos + 1; i < count; i++, j++)
    {
B
Behdad Esfahbod 已提交
306
      while (!_hb_ot_layout_check_glyph_property (layout, IN_INFO (j), lookup_flag, &property))
B
Behdad Esfahbod 已提交
307
      {
308
	if (HB_UNLIKELY (j + count - i == end))
309 310 311 312
	  return false;
	j++;
      }

313
      if (!(property & HB_OT_LAYOUT_GLYPH_CLASS_MARK))
314 315
	is_mark = FALSE;

B
Behdad Esfahbod 已提交
316
      if (HB_LIKELY (IN_GLYPH(j) != component[i]))
317 318 319 320 321 322 323 324 325 326 327
        return false;
    }
    if ( _hb_ot_layout_has_new_glyph_classes (layout) )
      /* this is just a guess ... */
      hb_ot_layout_set_glyph_class (layout, ligGlyph,
				    is_mark ? HB_OT_LAYOUT_GLYPH_CLASS_MARK
					    : HB_OT_LAYOUT_GLYPH_CLASS_LIGATURE);

    if (j == buffer->in_pos + i) /* No input glyphs skipped */
      /* We don't use a new ligature ID if there are no skipped
	 glyphs and the ligature already has an ID. */
B
Behdad Esfahbod 已提交
328 329 330 331
      _hb_buffer_add_output_glyphs (buffer, i,
				    1, (const uint16_t *) &ligGlyph,
				    0xFFFF,
				    IN_LIGID (buffer->in_pos) ?
B
Behdad Esfahbod 已提交
332
				    0xFFFF : _hb_buffer_allocate_lig_id (buffer));
333 334
    else
    {
B
Behdad Esfahbod 已提交
335
      unsigned int lig_id = _hb_buffer_allocate_lig_id (buffer);
336 337 338 339 340 341 342 343 344
      _hb_buffer_add_output_glyph (buffer, ligGlyph, 0xFFFF, lig_id);

      /* Now we must do a second loop to copy the skipped glyphs to
	 `out' and assign component values to it.  We start with the
	 glyph after the first component.  Glyphs between component
	 i and i+1 belong to component i.  Together with the lig_id
	 value it is later possible to check whether a specific
	 component value really belongs to a given ligature. */

B
Behdad Esfahbod 已提交
345
      for ( i = 1; i < count; i++ )
346
      {
B
Behdad Esfahbod 已提交
347
	while (!_hb_ot_layout_check_glyph_property (layout, IN_CURINFO(), lookup_flag, &property))
B
Behdad Esfahbod 已提交
348
	  _hb_buffer_add_output_glyph (buffer, IN_CURGLYPH(), i - 1, lig_id);
349 350 351 352

	(buffer->in_pos)++;
      }

B
Behdad Esfahbod 已提交
353
      /* TODO We should possibly reassign lig_id and component for any
354 355 356 357 358 359
       * components of a previous ligature that s now being removed as part of
       * this ligature. */
    }

    return true;
  }
360 361 362

  private:
  GlyphID	ligGlyph;		/* GlyphID of ligature to substitute */
B
Behdad Esfahbod 已提交
363 364
  HeadlessArrayOf<GlyphID>
		component;		/* Array of component GlyphIDs--start
365 366 367
					 * with the second  component--ordered
					 * in writing direction */
};
368
ASSERT_SIZE (Ligature, 4);
B
Behdad Esfahbod 已提交
369

B
Behdad Esfahbod 已提交
370 371
struct LigatureSet
{
372 373 374
  friend struct LigatureSubstFormat1;

  private:
B
Behdad Esfahbod 已提交
375 376
  inline bool apply (APPLY_ARG_DEF, bool is_mark) const
  {
B
Behdad Esfahbod 已提交
377
    unsigned int num_ligs = ligature.len;
B
Behdad Esfahbod 已提交
378 379
    for (unsigned int i = 0; i < num_ligs; i++)
    {
B
Behdad Esfahbod 已提交
380
      const Ligature &lig = this+ligature[i];
381
      if (lig.apply (APPLY_ARG, is_mark))
382 383 384 385 386
        return true;
    }

    return false;
  }
B
Behdad Esfahbod 已提交
387 388

  private:
B
Behdad Esfahbod 已提交
389 390 391
  OffsetArrayOf<Ligature>
		ligature;		/* Array LigatureSet tables
					 * ordered by preference */
B
Behdad Esfahbod 已提交
392
};
393
ASSERT_SIZE (LigatureSet, 2);
B
Behdad Esfahbod 已提交
394

B
Behdad Esfahbod 已提交
395 396
struct LigatureSubstFormat1
{
397 398 399
  friend struct LigatureSubst;

  private:
B
Behdad Esfahbod 已提交
400 401
  inline bool apply (APPLY_ARG_DEF) const
  {
402 403
    hb_codepoint_t glyph_id = IN_CURGLYPH ();

404
    bool first_is_mark = !!(property & HB_OT_LAYOUT_GLYPH_CLASS_MARK);
405

B
Behdad Esfahbod 已提交
406
    unsigned int index = (this+coverage) (glyph_id);
B
Behdad Esfahbod 已提交
407
    if (HB_LIKELY (index == NOT_COVERED))
408 409
      return false;

B
Behdad Esfahbod 已提交
410
    const LigatureSet &lig_set = this+ligatureSet[index];
411
    return lig_set.apply (APPLY_ARG, first_is_mark);
412
  }
B
Behdad Esfahbod 已提交
413 414

  private:
B
Behdad Esfahbod 已提交
415
  USHORT	format;			/* Format identifier--format = 1 */
B
Behdad Esfahbod 已提交
416 417
  OffsetTo<Coverage>
		coverage;		/* Offset to Coverage table--from
418
					 * beginning of Substitution table */
B
Behdad Esfahbod 已提交
419
  OffsetArrayOf<LigatureSet>
B
Behdad Esfahbod 已提交
420 421
		ligatureSet;		/* Array LigatureSet tables
					 * ordered by Coverage Index */
B
Behdad Esfahbod 已提交
422
};
423 424
ASSERT_SIZE (LigatureSubstFormat1, 6);

B
Behdad Esfahbod 已提交
425 426
struct LigatureSubst
{
427 428 429
  friend struct SubstLookupSubTable;

  private:
B
Behdad Esfahbod 已提交
430 431
  inline bool apply (APPLY_ARG_DEF) const
  {
B
Behdad Esfahbod 已提交
432
    switch (u.format) {
433
    case 1: return u.format1->apply (APPLY_ARG);
434 435 436 437 438 439
    default:return false;
    }
  }

  private:
  union {
B
Behdad Esfahbod 已提交
440 441
  USHORT		format;		/* Format identifier */
  LigatureSubstFormat1	format1[];
442 443
  } u;
};
B
Behdad Esfahbod 已提交
444
ASSERT_SIZE (LigatureSubst, 2);
445

B
Behdad Esfahbod 已提交
446

447

448
static inline bool substitute_lookup (APPLY_ARG_DEF, unsigned int lookup_index);
B
Behdad Esfahbod 已提交
449

B
Behdad Esfahbod 已提交
450 451
struct ContextSubst : Context
{
452 453 454
  friend struct SubstLookupSubTable;

  private:
B
Behdad Esfahbod 已提交
455
  inline bool apply (APPLY_ARG_DEF) const
B
Behdad Esfahbod 已提交
456
  { return Context::apply (APPLY_ARG, substitute_lookup); }
457
};
B
Behdad Esfahbod 已提交
458
ASSERT_SIZE (ContextSubst, 2);
459

B
Behdad Esfahbod 已提交
460 461
struct ChainContextSubst : ChainContext
{
462 463 464
  friend struct SubstLookupSubTable;

  private:
B
Behdad Esfahbod 已提交
465
  inline bool apply (APPLY_ARG_DEF) const
B
Behdad Esfahbod 已提交
466
  { return ChainContext::apply (APPLY_ARG, substitute_lookup); }
B
Behdad Esfahbod 已提交
467
};
B
Behdad Esfahbod 已提交
468
ASSERT_SIZE (ChainContextSubst, 2);
B
Behdad Esfahbod 已提交
469

470

471
struct ExtensionSubst : Extension
B
Behdad Esfahbod 已提交
472
{
473 474 475
  friend struct SubstLookupSubTable;

  private:
476
  inline bool apply (APPLY_ARG_DEF) const;
477
};
B
Behdad Esfahbod 已提交
478
ASSERT_SIZE (ExtensionSubst, 2);
479 480


B
Behdad Esfahbod 已提交
481 482
struct ReverseChainSingleSubstFormat1
{
483 484 485
  friend struct ReverseChainSingleSubst;

  private:
B
Behdad Esfahbod 已提交
486 487
  inline bool apply (APPLY_ARG_DEF) const
  {
488 489 490 491 492 493 494
    if (HB_UNLIKELY (context_length != NO_CONTEXT))
      return false; /* No chaining to this type */

    unsigned int index = (this+coverage) (IN_CURGLYPH ());
    if (HB_LIKELY (index == NOT_COVERED))
      return false;

495 496 497 498
    const OffsetArrayOf<Coverage> &lookahead = (const OffsetArrayOf<Coverage>&)
					       *((const char *) &backtrack + backtrack.get_size ());
    const ArrayOf<GlyphID> &substitute = (const ArrayOf<GlyphID>&)
					 *((const char *) &lookahead + lookahead.get_size ());
499

500
    if (match_backtrack (APPLY_ARG,
501 502
			 backtrack.len, (USHORT *) backtrack.array,
			 match_coverage, (char *) this) &&
503
        match_lookahead (APPLY_ARG,
504 505 506 507 508 509 510 511 512
			 lookahead.len, (USHORT *) lookahead.array,
			 match_coverage, (char *) this,
			 1))
    {
      IN_CURGLYPH() = substitute[index];
      buffer->in_pos--; /* Reverse! */
      return true;
    }

513 514
    return false;
  }
B
Behdad Esfahbod 已提交
515 516

  private:
B
Behdad Esfahbod 已提交
517
  USHORT	format;			/* Format identifier--format = 1 */
518 519 520 521 522
  OffsetTo<Coverage>
		coverage;		/* Offset to Coverage table--from
					 * beginning of table */
  OffsetArrayOf<Coverage>
		backtrack;		/* Array of coverage tables
B
Behdad Esfahbod 已提交
523 524
					 * in backtracking sequence, in  glyph
					 * sequence order */
525 526 527
  OffsetArrayOf<Coverage>
		lookaheadX;		/* Array of coverage tables
					 * in lookahead sequence, in glyph
B
Behdad Esfahbod 已提交
528
					 * sequence order */
529 530 531
  ArrayOf<GlyphID>
		substituteX;		/* Array of substitute
					 * GlyphIDs--ordered by Coverage Index */
B
Behdad Esfahbod 已提交
532 533 534
};
ASSERT_SIZE (ReverseChainSingleSubstFormat1, 10);

B
Behdad Esfahbod 已提交
535 536
struct ReverseChainSingleSubst
{
537 538 539
  friend struct SubstLookupSubTable;

  private:
B
Behdad Esfahbod 已提交
540 541
  inline bool apply (APPLY_ARG_DEF) const
  {
542
    switch (u.format) {
543
    case 1: return u.format1->apply (APPLY_ARG);
544 545 546 547 548 549 550 551 552 553 554 555 556 557
    default:return false;
    }
  }

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



B
Behdad Esfahbod 已提交
558 559 560 561
/*
 * SubstLookup
 */

B
Behdad Esfahbod 已提交
562 563
struct SubstLookupSubTable
{
B
Behdad Esfahbod 已提交
564 565
  friend struct SubstLookup;

566 567 568 569 570 571 572 573 574 575 576
  enum {
    Single		= 1,
    Multiple		= 2,
    Alternate		= 3,
    Ligature		= 4,
    Context		= 5,
    ChainContext	= 6,
    Extension		= 7,
    ReverseChainSingle	= 8,
  };

577
  bool apply (APPLY_ARG_DEF, unsigned int lookup_type) const
B
Behdad Esfahbod 已提交
578
  {
579
    switch (lookup_type) {
580 581 582 583 584 585 586 587
    case Single:		return u.single->apply (APPLY_ARG);
    case Multiple:		return u.multiple->apply (APPLY_ARG);
    case Alternate:		return u.alternate->apply (APPLY_ARG);
    case Ligature:		return u.ligature->apply (APPLY_ARG);
    case Context:		return u.context->apply (APPLY_ARG);
    case ChainContext:		return u.chainContext->apply (APPLY_ARG);
    case Extension:		return u.extension->apply (APPLY_ARG);
    case ReverseChainSingle:	return u.reverseChainContextSingle->apply (APPLY_ARG);
588 589
    default:return false;
    }
B
Behdad Esfahbod 已提交
590 591 592 593
  }

  private:
  union {
594 595 596 597 598 599
  USHORT			format;
  SingleSubst			single[];
  MultipleSubst			multiple[];
  AlternateSubst		alternate[];
  LigatureSubst			ligature[];
  ContextSubst			context[];
B
Behdad Esfahbod 已提交
600
  ChainContextSubst		chainContext[];
601 602
  ExtensionSubst		extension[];
  ReverseChainSingleSubst	reverseChainContextSingle[];
B
Behdad Esfahbod 已提交
603 604
  } u;
};
B
Behdad Esfahbod 已提交
605
ASSERT_SIZE (SubstLookupSubTable, 2);
B
Behdad Esfahbod 已提交
606

607

B
Behdad Esfahbod 已提交
608 609 610
struct SubstLookup : Lookup
{
  inline const SubstLookupSubTable& get_subtable (unsigned int i) const
B
Behdad Esfahbod 已提交
611
  { return (const SubstLookupSubTable&) Lookup::get_subtable (i); }
B
Behdad Esfahbod 已提交
612 613

  /* Like get_type(), but looks through extension lookups.
614
   * Never returns Extension */
B
Behdad Esfahbod 已提交
615 616
  inline unsigned int get_effective_type (void) const
  {
B
Behdad Esfahbod 已提交
617 618
    unsigned int type = get_type ();

619
    if (HB_UNLIKELY (type == SubstLookupSubTable::Extension))
B
Behdad Esfahbod 已提交
620
    {
621
      unsigned int count = get_subtable_count ();
B
Behdad Esfahbod 已提交
622
      type = get_subtable(0).u.extension->get_type ();
623 624 625 626 627
      /* The spec says all subtables should have the same type.
       * This is specially important if one has a reverse type! */
      for (unsigned int i = 1; i < count; i++)
        if (get_subtable(i).u.extension->get_type () != type)
	  return 0;
B
Behdad Esfahbod 已提交
628 629 630 631 632
    }

    return type;
  }

B
Behdad Esfahbod 已提交
633
  inline bool is_reverse (void) const
B
Behdad Esfahbod 已提交
634
  { return HB_UNLIKELY (get_effective_type () == SubstLookupSubTable::ReverseChainSingle); }
B
Behdad Esfahbod 已提交
635

636 637 638 639
  inline bool apply_once (hb_ot_layout_t *layout,
			  hb_buffer_t    *buffer,
			  unsigned int    context_length,
			  unsigned int    nesting_level_left) const
B
Behdad Esfahbod 已提交
640
  {
B
Behdad Esfahbod 已提交
641
    unsigned int lookup_type = get_type ();
B
Behdad Esfahbod 已提交
642
    unsigned int lookup_flag = get_flag ();
643 644
    unsigned int property;

B
Behdad Esfahbod 已提交
645
    if (!_hb_ot_layout_check_glyph_property (layout, IN_CURINFO (), lookup_flag, &property))
646
      return false;
B
Behdad Esfahbod 已提交
647 648

    for (unsigned int i = 0; i < get_subtable_count (); i++)
649
      if (get_subtable (i).apply (APPLY_ARG, lookup_type))
B
Behdad Esfahbod 已提交
650
	return true;
B
Behdad Esfahbod 已提交
651

B
Behdad Esfahbod 已提交
652 653
    return false;
  }
654

655 656
  bool apply_string (hb_ot_layout_t *layout,
		     hb_buffer_t    *buffer,
B
Behdad Esfahbod 已提交
657 658
		     hb_ot_layout_feature_mask_t mask) const
  {
659 660
    bool ret = false;

B
Behdad Esfahbod 已提交
661 662 663
    if (HB_UNLIKELY (!buffer->in_length))
      return false;

B
Behdad Esfahbod 已提交
664 665
    if (HB_LIKELY (!is_reverse ()))
    {
666 667 668
	/* in/out forward substitution */
	_hb_buffer_clear_output (buffer);
	buffer->in_pos = 0;
B
Behdad Esfahbod 已提交
669 670
	while (buffer->in_pos < buffer->in_length)
	{
671
	  if ((~IN_PROPERTIES (buffer->in_pos) & mask) &&
672
	      apply_once (layout, buffer, NO_CONTEXT, MAX_NESTING_LEVEL))
673 674
	    ret = true;
	  else
B
Behdad Esfahbod 已提交
675
	    _hb_buffer_next_glyph (buffer);
676 677 678 679 680

	}
	if (ret)
	  _hb_buffer_swap (buffer);

B
Behdad Esfahbod 已提交
681 682 683
    }
    else
    {
684 685 686

	/* in-place backward substitution */
	buffer->in_pos = buffer->in_length - 1;
B
Behdad Esfahbod 已提交
687 688
	do
	{
689
	  if ((~IN_PROPERTIES (buffer->in_pos) & mask) &&
690
	      apply_once (layout, buffer, NO_CONTEXT, MAX_NESTING_LEVEL))
691 692 693 694
	    ret = true;
	  else
	    buffer->in_pos--;

B
Behdad Esfahbod 已提交
695 696
	}
	while ((int) buffer->in_pos >= 0);
697 698 699 700
    }

    return ret;
  }
B
Behdad Esfahbod 已提交
701
};
B
Behdad Esfahbod 已提交
702
ASSERT_SIZE (SubstLookup, 6);
B
Behdad Esfahbod 已提交
703

704

B
Minor  
Behdad Esfahbod 已提交
705 706 707 708
/*
 * GSUB
 */

B
Behdad Esfahbod 已提交
709 710
struct GSUB : GSUBGPOS
{
B
Minor  
Behdad Esfahbod 已提交
711 712
  static const hb_tag_t Tag		= HB_TAG ('G','S','U','B');

713
  static inline const GSUB& get_for_data (const char *data)
B
Behdad Esfahbod 已提交
714
  { return (const GSUB&) GSUBGPOS::get_for_data (data); }
B
Behdad Esfahbod 已提交
715

B
Behdad Esfahbod 已提交
716
  inline const SubstLookup& get_lookup (unsigned int i) const
B
Behdad Esfahbod 已提交
717
  { return (const SubstLookup&) GSUBGPOS::get_lookup (i); }
B
Behdad Esfahbod 已提交
718

719 720 721
  inline bool substitute_lookup (hb_ot_layout_t *layout,
				 hb_buffer_t    *buffer,
			         unsigned int    lookup_index,
B
Behdad Esfahbod 已提交
722
				 hb_ot_layout_feature_mask_t  mask) const
B
Behdad Esfahbod 已提交
723
  { return get_lookup (lookup_index).apply_string (layout, buffer, mask); }
B
Behdad Esfahbod 已提交
724

B
Minor  
Behdad Esfahbod 已提交
725
};
B
Behdad Esfahbod 已提交
726
ASSERT_SIZE (GSUB, 10);
727 728


B
Behdad Esfahbod 已提交
729
/* Out-of-class implementation for methods recursing */
730

731
inline bool ExtensionSubst::apply (APPLY_ARG_DEF) const
B
Behdad Esfahbod 已提交
732
{
733 734
  unsigned int lookup_type = get_type ();

735
  if (HB_UNLIKELY (lookup_type == SubstLookupSubTable::Extension))
736 737
    return false;

738
  return ((SubstLookupSubTable&) get_subtable ()).apply (APPLY_ARG, lookup_type);
739 740
}

B
Behdad Esfahbod 已提交
741 742
static inline bool substitute_lookup (APPLY_ARG_DEF, unsigned int lookup_index)
{
743
  const GSUB &gsub = *(layout->gsub);
744
  const SubstLookup &l = gsub.get_lookup (lookup_index);
745

746 747 748 749 750 751 752
  if (HB_UNLIKELY (nesting_level_left == 0))
    return false;
  nesting_level_left--;

  if (HB_UNLIKELY (context_length < 1))
    return false;

753
  return l.apply_once (layout, buffer, context_length, nesting_level_left);
754 755 756
}


B
Behdad Esfahbod 已提交
757
#endif /* HB_OT_LAYOUT_GSUB_PRIVATE_H */