hb-ot-layout-gsub-private.h 19.0 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 313 314 315 316
	  return false;
	j++;
      }

      if (!(property == HB_OT_LAYOUT_GLYPH_CLASS_MARK ||
	    property &  LookupFlag::MarkAttachmentType))
	is_mark = FALSE;

B
Behdad Esfahbod 已提交
317
      if (HB_LIKELY (IN_GLYPH(j) != component[i]))
318 319 320 321 322 323 324 325 326 327 328
        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 已提交
329 330 331 332
      _hb_buffer_add_output_glyphs (buffer, i,
				    1, (const uint16_t *) &ligGlyph,
				    0xFFFF,
				    IN_LIGID (buffer->in_pos) ?
B
Behdad Esfahbod 已提交
333
				    0xFFFF : _hb_buffer_allocate_lig_id (buffer));
334 335
    else
    {
B
Behdad Esfahbod 已提交
336
      unsigned int lig_id = _hb_buffer_allocate_lig_id (buffer);
337 338 339 340 341 342 343 344 345
      _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 已提交
346
      for ( i = 1; i < count; i++ )
347
      {
B
Behdad Esfahbod 已提交
348
	while (!_hb_ot_layout_check_glyph_property (layout, IN_CURINFO(), lookup_flag, &property))
B
Behdad Esfahbod 已提交
349
	  _hb_buffer_add_output_glyph (buffer, IN_CURGLYPH(), i - 1, lig_id);
350 351 352 353

	(buffer->in_pos)++;
      }

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

    return true;
  }
361 362 363

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

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

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

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

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

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

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

    bool first_is_mark = (property == HB_OT_LAYOUT_GLYPH_CLASS_MARK ||
			  property &  LookupFlag::MarkAttachmentType);

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

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

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

B
Behdad Esfahbod 已提交
427 428
struct LigatureSubst
{
429 430 431
  friend struct SubstLookupSubTable;

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

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

B
Behdad Esfahbod 已提交
448

449

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

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

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

B
Behdad Esfahbod 已提交
464 465
struct ChainContextSubst : ChainContext
{
466 467 468
  friend struct SubstLookupSubTable;

  private:
B
Behdad Esfahbod 已提交
469 470
  inline bool apply (APPLY_ARG_DEF) const
  {
471
    return ChainContext::apply (APPLY_ARG, substitute_lookup);
B
Behdad Esfahbod 已提交
472 473
  }
};
B
Behdad Esfahbod 已提交
474
ASSERT_SIZE (ChainContextSubst, 2);
B
Behdad Esfahbod 已提交
475

476

477
struct ExtensionSubst : Extension
B
Behdad Esfahbod 已提交
478
{
479 480 481
  friend struct SubstLookupSubTable;

  private:
482
  inline bool apply (APPLY_ARG_DEF) const;
483
};
B
Behdad Esfahbod 已提交
484
ASSERT_SIZE (ExtensionSubst, 2);
485 486


B
Behdad Esfahbod 已提交
487 488
struct ReverseChainSingleSubstFormat1
{
489 490 491
  friend struct ReverseChainSingleSubst;

  private:
B
Behdad Esfahbod 已提交
492 493
  inline bool apply (APPLY_ARG_DEF) const
  {
494 495 496 497 498 499 500
    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;

501 502 503 504
    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 ());
505

506
    if (match_backtrack (APPLY_ARG,
507 508
			 backtrack.len, (USHORT *) backtrack.array,
			 match_coverage, (char *) this) &&
509
        match_lookahead (APPLY_ARG,
510 511 512 513 514 515 516 517 518
			 lookahead.len, (USHORT *) lookahead.array,
			 match_coverage, (char *) this,
			 1))
    {
      IN_CURGLYPH() = substitute[index];
      buffer->in_pos--; /* Reverse! */
      return true;
    }

519 520
    return false;
  }
B
Behdad Esfahbod 已提交
521 522

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

B
Behdad Esfahbod 已提交
541 542
struct ReverseChainSingleSubst
{
543 544 545
  friend struct SubstLookupSubTable;

  private:
B
Behdad Esfahbod 已提交
546 547
  inline bool apply (APPLY_ARG_DEF) const
  {
548
    switch (u.format) {
549
    case 1: return u.format1->apply (APPLY_ARG);
550 551 552 553 554 555 556 557 558 559 560 561 562 563
    default:return false;
    }
  }

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



B
Behdad Esfahbod 已提交
564 565 566 567
/*
 * SubstLookup
 */

B
Behdad Esfahbod 已提交
568 569
struct SubstLookupSubTable
{
B
Behdad Esfahbod 已提交
570 571
  friend struct SubstLookup;

572 573 574 575 576 577 578 579 580 581 582
  enum {
    Single		= 1,
    Multiple		= 2,
    Alternate		= 3,
    Ligature		= 4,
    Context		= 5,
    ChainContext	= 6,
    Extension		= 7,
    ReverseChainSingle	= 8,
  };

583
  bool apply (APPLY_ARG_DEF, unsigned int lookup_type) const
B
Behdad Esfahbod 已提交
584
  {
585
    switch (lookup_type) {
586 587 588 589 590 591 592 593
    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);
594 595
    default:return false;
    }
B
Behdad Esfahbod 已提交
596 597 598 599
  }

  private:
  union {
600 601 602 603 604 605
  USHORT			format;
  SingleSubst			single[];
  MultipleSubst			multiple[];
  AlternateSubst		alternate[];
  LigatureSubst			ligature[];
  ContextSubst			context[];
B
Behdad Esfahbod 已提交
606
  ChainContextSubst		chainContext[];
607 608
  ExtensionSubst		extension[];
  ReverseChainSingleSubst	reverseChainContextSingle[];
B
Behdad Esfahbod 已提交
609 610
  } u;
};
B
Behdad Esfahbod 已提交
611
ASSERT_SIZE (SubstLookupSubTable, 2);
B
Behdad Esfahbod 已提交
612

613

B
Behdad Esfahbod 已提交
614 615 616 617
struct SubstLookup : Lookup
{
  inline const SubstLookupSubTable& get_subtable (unsigned int i) const
  {
B
Behdad Esfahbod 已提交
618
    return (const SubstLookupSubTable&) Lookup::get_subtable (i);
B
Behdad Esfahbod 已提交
619 620 621
  }

  /* Like get_type(), but looks through extension lookups.
622
   * Never returns Extension */
B
Behdad Esfahbod 已提交
623 624
  inline unsigned int get_effective_type (void) const
  {
B
Behdad Esfahbod 已提交
625 626
    unsigned int type = get_type ();

627
    if (HB_UNLIKELY (type == SubstLookupSubTable::Extension))
B
Behdad Esfahbod 已提交
628
    {
629
      unsigned int count = get_subtable_count ();
B
Behdad Esfahbod 已提交
630
      type = get_subtable(0).u.extension->get_type ();
631 632 633 634 635
      /* 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 已提交
636 637 638 639 640
    }

    return type;
  }

B
Behdad Esfahbod 已提交
641 642
  inline bool is_reverse (void) const
  {
643
    return HB_UNLIKELY (get_effective_type () == SubstLookupSubTable::ReverseChainSingle);
B
Behdad Esfahbod 已提交
644 645
  }

646 647 648 649
  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 已提交
650
  {
B
Behdad Esfahbod 已提交
651
    unsigned int lookup_type = get_type ();
B
Behdad Esfahbod 已提交
652
    unsigned int lookup_flag = get_flag ();
653 654
    unsigned int property;

B
Behdad Esfahbod 已提交
655
    if (!_hb_ot_layout_check_glyph_property (layout, IN_CURINFO (), lookup_flag, &property))
656
      return false;
B
Behdad Esfahbod 已提交
657 658

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

B
Behdad Esfahbod 已提交
662 663
    return false;
  }
664

665 666
  bool apply_string (hb_ot_layout_t *layout,
		     hb_buffer_t    *buffer,
B
Behdad Esfahbod 已提交
667 668
		     hb_ot_layout_feature_mask_t mask) const
  {
669 670
    bool ret = false;

B
Behdad Esfahbod 已提交
671 672 673
    if (HB_UNLIKELY (!buffer->in_length))
      return false;

B
Behdad Esfahbod 已提交
674 675
    if (HB_LIKELY (!is_reverse ()))
    {
676 677 678
	/* in/out forward substitution */
	_hb_buffer_clear_output (buffer);
	buffer->in_pos = 0;
B
Behdad Esfahbod 已提交
679 680
	while (buffer->in_pos < buffer->in_length)
	{
681
	  if ((~IN_PROPERTIES (buffer->in_pos) & mask) &&
682
	      apply_once (layout, buffer, NO_CONTEXT, MAX_NESTING_LEVEL))
683 684
	    ret = true;
	  else
B
Behdad Esfahbod 已提交
685
	    _hb_buffer_next_glyph (buffer);
686 687 688 689 690

	}
	if (ret)
	  _hb_buffer_swap (buffer);

B
Behdad Esfahbod 已提交
691 692 693
    }
    else
    {
694 695 696

	/* in-place backward substitution */
	buffer->in_pos = buffer->in_length - 1;
B
Behdad Esfahbod 已提交
697 698
	do
	{
699
	  if ((~IN_PROPERTIES (buffer->in_pos) & mask) &&
700
	      apply_once (layout, buffer, NO_CONTEXT, MAX_NESTING_LEVEL))
701 702 703 704
	    ret = true;
	  else
	    buffer->in_pos--;

B
Behdad Esfahbod 已提交
705 706
	}
	while ((int) buffer->in_pos >= 0);
707 708 709 710
    }

    return ret;
  }
B
Behdad Esfahbod 已提交
711
};
B
Behdad Esfahbod 已提交
712
ASSERT_SIZE (SubstLookup, 6);
B
Behdad Esfahbod 已提交
713

714

B
Minor  
Behdad Esfahbod 已提交
715 716 717 718
/*
 * GSUB
 */

B
Behdad Esfahbod 已提交
719 720
struct GSUB : GSUBGPOS
{
B
Minor  
Behdad Esfahbod 已提交
721 722
  static const hb_tag_t Tag		= HB_TAG ('G','S','U','B');

723 724 725 726
  static inline const GSUB& get_for_data (const char *data)
  {
    return (const GSUB&) GSUBGPOS::get_for_data (data);
  }
B
Behdad Esfahbod 已提交
727

B
Behdad Esfahbod 已提交
728 729 730
  inline const SubstLookup& get_lookup (unsigned int i) const
  {
    return (const SubstLookup&) GSUBGPOS::get_lookup (i);
B
Behdad Esfahbod 已提交
731 732
  }

733 734 735
  inline bool substitute_lookup (hb_ot_layout_t *layout,
				 hb_buffer_t    *buffer,
			         unsigned int    lookup_index,
B
Behdad Esfahbod 已提交
736 737
				 hb_ot_layout_feature_mask_t  mask) const
  {
738
    return get_lookup (lookup_index).apply_string (layout, buffer, mask);
739
  }
B
Behdad Esfahbod 已提交
740

B
Minor  
Behdad Esfahbod 已提交
741
};
B
Behdad Esfahbod 已提交
742
ASSERT_SIZE (GSUB, 10);
743 744


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

747
inline bool ExtensionSubst::apply (APPLY_ARG_DEF) const
B
Behdad Esfahbod 已提交
748
{
749 750
  unsigned int lookup_type = get_type ();

751
  if (HB_UNLIKELY (lookup_type == SubstLookupSubTable::Extension))
752 753
    return false;

754
  return ((SubstLookupSubTable&) get_subtable ()).apply (APPLY_ARG, lookup_type);
755 756
}

B
Behdad Esfahbod 已提交
757 758
static inline bool substitute_lookup (APPLY_ARG_DEF, unsigned int lookup_index)
{
759
  const GSUB &gsub = *(layout->gsub);
760
  const SubstLookup &l = gsub.get_lookup (lookup_index);
761

762 763 764 765 766 767 768
  if (HB_UNLIKELY (nesting_level_left == 0))
    return false;
  nesting_level_left--;

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

769
  return l.apply_once (layout, buffer, context_length, nesting_level_left);
770 771 772
}


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