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
    _hb_buffer_replace_glyph (buffer, glyph_id);

48
    /* We inherit the old glyph class to the substituted glyph */
B
XX  
Behdad Esfahbod 已提交
49 50
    if (_hb_ot_layout_has_new_glyph_classes (context->layout))
      _hb_ot_layout_set_glyph_property (context->layout, glyph_id, property);
B
Behdad Esfahbod 已提交
51 52

    return true;
53
  }
B
Behdad Esfahbod 已提交
54

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

B
Behdad Esfahbod 已提交
65 66
struct SingleSubstFormat2
{
67 68 69
  friend struct SingleSubst;

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

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

    glyph_id = substitute[index];
B
Behdad Esfahbod 已提交
81 82
    _hb_buffer_replace_glyph (buffer, glyph_id);

83
    /* We inherit the old glyph class to the substituted glyph */
B
XX  
Behdad Esfahbod 已提交
84 85
    if (_hb_ot_layout_has_new_glyph_classes (context->layout))
      _hb_ot_layout_set_glyph_property (context->layout, glyph_id, property);
B
Behdad Esfahbod 已提交
86

87 88
    return true;
  }
B
Behdad Esfahbod 已提交
89 90

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

B
Behdad Esfahbod 已提交
101 102
struct SingleSubst
{
103 104
  friend struct SubstLookupSubTable;

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

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

B
Behdad Esfahbod 已提交
124

B
Behdad Esfahbod 已提交
125 126
struct Sequence
{
127 128 129
  friend struct MultipleSubstFormat1;

  private:
B
Behdad Esfahbod 已提交
130 131
  inline bool apply (APPLY_ARG_DEF) const
  {
B
Behdad Esfahbod 已提交
132
    if (HB_UNLIKELY (!substitute.len))
B
Behdad Esfahbod 已提交
133 134
      return false;

B
Behdad Esfahbod 已提交
135 136 137
    _hb_buffer_add_output_glyphs (buffer, 1,
				  substitute.len, (const uint16_t *) substitute.array,
				  0xFFFF, 0xFFFF);
B
Behdad Esfahbod 已提交
138

139
    /* This is a guess only ... */
B
XX  
Behdad Esfahbod 已提交
140
    if (_hb_ot_layout_has_new_glyph_classes (context->layout))
B
Behdad Esfahbod 已提交
141
    {
142
      if (property == HB_OT_LAYOUT_GLYPH_CLASS_LIGATURE)
B
Behdad Esfahbod 已提交
143 144
        property = HB_OT_LAYOUT_GLYPH_CLASS_BASE_GLYPH;

145 146
      unsigned int count = substitute.len;
      for (unsigned int n = 0; n < count; n++)
B
XX  
Behdad Esfahbod 已提交
147
	_hb_ot_layout_set_glyph_property (context->layout, substitute[n], property);
B
Behdad Esfahbod 已提交
148 149 150 151 152
    }

    return true;
  }

B
Behdad Esfahbod 已提交
153
  private:
B
Behdad Esfahbod 已提交
154 155
  ArrayOf<GlyphID>
		substitute;		/* String of GlyphIDs to substitute */
B
Behdad Esfahbod 已提交
156
};
157
ASSERT_SIZE (Sequence, 2);
B
Behdad Esfahbod 已提交
158

B
Behdad Esfahbod 已提交
159 160
struct MultipleSubstFormat1
{
161 162 163
  friend struct MultipleSubst;

  private:
B
Behdad Esfahbod 已提交
164 165
  inline bool apply (APPLY_ARG_DEF) const
  {
166

B
Behdad Esfahbod 已提交
167
    unsigned int index = (this+coverage) (IN_CURGLYPH ());
B
Behdad Esfahbod 已提交
168
    if (HB_LIKELY (index == NOT_COVERED))
169 170
      return false;

171
    return (this+sequence[index]).apply (APPLY_ARG);
172
  }
B
Behdad Esfahbod 已提交
173 174

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

B
Behdad Esfahbod 已提交
185 186
struct MultipleSubst
{
187 188 189
  friend struct SubstLookupSubTable;

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

  private:
  union {
B
Behdad Esfahbod 已提交
200 201
  USHORT		format;		/* Format identifier */
  MultipleSubstFormat1	format1[];
202 203
  } u;
};
B
Behdad Esfahbod 已提交
204
ASSERT_SIZE (MultipleSubst, 2);
205

B
Behdad Esfahbod 已提交
206

B
Behdad Esfahbod 已提交
207
typedef ArrayOf<GlyphID> AlternateSet;	/* Array of alternate GlyphIDs--in
B
Behdad Esfahbod 已提交
208
					 * arbitrary order */
209
ASSERT_SIZE (AlternateSet, 2);
B
Behdad Esfahbod 已提交
210

B
Behdad Esfahbod 已提交
211 212
struct AlternateSubstFormat1
{
213 214 215
  friend struct AlternateSubst;

  private:
B
Behdad Esfahbod 已提交
216 217
  inline bool apply (APPLY_ARG_DEF) const
  {
218
    hb_codepoint_t glyph_id = IN_CURGLYPH ();
219

B
Behdad Esfahbod 已提交
220
    unsigned int index = (this+coverage) (glyph_id);
B
Behdad Esfahbod 已提交
221
    if (HB_LIKELY (index == NOT_COVERED))
222 223
      return false;

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

B
Behdad Esfahbod 已提交
226
    if (HB_UNLIKELY (!alt_set.len))
227 228 229 230 231
      return false;

    unsigned int alt_index = 0;

    /* XXX callback to user to choose alternate
B
XX  
Behdad Esfahbod 已提交
232 233
    if (context->layout->altfunc)
      alt_index = (context->layout->altfunc)(context->layout, buffer,
B
Behdad Esfahbod 已提交
234 235
				    buffer->out_pos, glyph_id,
				    alt_set.len, alt_set.array);
236 237
				   */

B
Behdad Esfahbod 已提交
238
    if (HB_UNLIKELY (alt_index >= alt_set.len))
239 240 241 242
      return false;

    glyph_id = alt_set[alt_index];

B
Behdad Esfahbod 已提交
243
    _hb_buffer_replace_glyph (buffer, glyph_id);
244

245
    /* We inherit the old glyph class to the substituted glyph */
B
XX  
Behdad Esfahbod 已提交
246 247
    if (_hb_ot_layout_has_new_glyph_classes (context->layout))
      _hb_ot_layout_set_glyph_property (context->layout, glyph_id, property);
248 249 250

    return true;
  }
B
Behdad Esfahbod 已提交
251 252

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

B
Behdad Esfahbod 已提交
263 264
struct AlternateSubst
{
265 266 267
  friend struct SubstLookupSubTable;

  private:
B
Behdad Esfahbod 已提交
268 269
  inline bool apply (APPLY_ARG_DEF) const
  {
B
Behdad Esfahbod 已提交
270
    switch (u.format) {
271
    case 1: return u.format1->apply (APPLY_ARG);
272 273 274 275 276 277
    default:return false;
    }
  }

  private:
  union {
B
Behdad Esfahbod 已提交
278 279
  USHORT		format;		/* Format identifier */
  AlternateSubstFormat1	format1[];
280 281
  } u;
};
B
Behdad Esfahbod 已提交
282
ASSERT_SIZE (AlternateSubst, 2);
283

284

B
Behdad Esfahbod 已提交
285 286
struct Ligature
{
287 288 289
  friend struct LigatureSet;

  private:
B
Behdad Esfahbod 已提交
290 291
  inline bool apply (APPLY_ARG_DEF, bool is_mark) const
  {
292
    unsigned int i, j;
B
Behdad Esfahbod 已提交
293
    unsigned int count = component.len;
294 295 296
    unsigned int end = MIN (buffer->in_length, buffer->in_pos + context_length);
    if (HB_UNLIKELY (buffer->in_pos + count > end))
      return false;
297

B
Behdad Esfahbod 已提交
298 299
    for (i = 1, j = buffer->in_pos + 1; i < count; i++, j++)
    {
B
XX  
Behdad Esfahbod 已提交
300
      while (_hb_ot_layout_skip_mark (context->layout, IN_INFO (j), lookup_flag, &property))
B
Behdad Esfahbod 已提交
301
      {
302
	if (HB_UNLIKELY (j + count - i == end))
303 304 305 306
	  return false;
	j++;
      }

307
      if (!(property & HB_OT_LAYOUT_GLYPH_CLASS_MARK))
308 309
	is_mark = FALSE;

310
      if (HB_LIKELY (IN_GLYPH (j) != component[i]))
311 312
        return false;
    }
313
    /* This is just a guess ... */
B
XX  
Behdad Esfahbod 已提交
314 315
    if (_hb_ot_layout_has_new_glyph_classes (context->layout))
      hb_ot_layout_set_glyph_class (context->layout, ligGlyph,
316 317 318 319 320 321
				    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 已提交
322 323 324 325
      _hb_buffer_add_output_glyphs (buffer, i,
				    1, (const uint16_t *) &ligGlyph,
				    0xFFFF,
				    IN_LIGID (buffer->in_pos) ?
B
Behdad Esfahbod 已提交
326
				    0xFFFF : _hb_buffer_allocate_lig_id (buffer));
327 328
    else
    {
B
Behdad Esfahbod 已提交
329
      unsigned int lig_id = _hb_buffer_allocate_lig_id (buffer);
330 331 332 333 334 335 336 337 338
      _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 已提交
339
      for ( i = 1; i < count; i++ )
340
      {
B
XX  
Behdad Esfahbod 已提交
341
	while (_hb_ot_layout_skip_mark (context->layout, IN_CURINFO (), lookup_flag, NULL))
342
	  _hb_buffer_add_output_glyph (buffer, IN_CURGLYPH (), i - 1, lig_id);
343 344 345 346

	(buffer->in_pos)++;
      }

B
Behdad Esfahbod 已提交
347
      /* TODO We should possibly reassign lig_id and component for any
348 349 350 351 352 353
       * components of a previous ligature that s now being removed as part of
       * this ligature. */
    }

    return true;
  }
354 355 356

  private:
  GlyphID	ligGlyph;		/* GlyphID of ligature to substitute */
B
Behdad Esfahbod 已提交
357 358
  HeadlessArrayOf<GlyphID>
		component;		/* Array of component GlyphIDs--start
359 360 361
					 * with the second  component--ordered
					 * in writing direction */
};
362
ASSERT_SIZE (Ligature, 4);
B
Behdad Esfahbod 已提交
363

B
Behdad Esfahbod 已提交
364 365
struct LigatureSet
{
366 367 368
  friend struct LigatureSubstFormat1;

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

    return false;
  }
B
Behdad Esfahbod 已提交
381 382

  private:
B
Behdad Esfahbod 已提交
383 384 385
  OffsetArrayOf<Ligature>
		ligature;		/* Array LigatureSet tables
					 * ordered by preference */
B
Behdad Esfahbod 已提交
386
};
387
ASSERT_SIZE (LigatureSet, 2);
B
Behdad Esfahbod 已提交
388

B
Behdad Esfahbod 已提交
389 390
struct LigatureSubstFormat1
{
391 392 393
  friend struct LigatureSubst;

  private:
B
Behdad Esfahbod 已提交
394 395
  inline bool apply (APPLY_ARG_DEF) const
  {
396 397
    hb_codepoint_t glyph_id = IN_CURGLYPH ();

398
    bool first_is_mark = !!(property & HB_OT_LAYOUT_GLYPH_CLASS_MARK);
399

B
Behdad Esfahbod 已提交
400
    unsigned int index = (this+coverage) (glyph_id);
B
Behdad Esfahbod 已提交
401
    if (HB_LIKELY (index == NOT_COVERED))
402 403
      return false;

B
Behdad Esfahbod 已提交
404
    const LigatureSet &lig_set = this+ligatureSet[index];
405
    return lig_set.apply (APPLY_ARG, first_is_mark);
406
  }
B
Behdad Esfahbod 已提交
407 408

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

B
Behdad Esfahbod 已提交
419 420
struct LigatureSubst
{
421 422 423
  friend struct SubstLookupSubTable;

  private:
B
Behdad Esfahbod 已提交
424 425
  inline bool apply (APPLY_ARG_DEF) const
  {
B
Behdad Esfahbod 已提交
426
    switch (u.format) {
427
    case 1: return u.format1->apply (APPLY_ARG);
428 429 430 431 432 433
    default:return false;
    }
  }

  private:
  union {
B
Behdad Esfahbod 已提交
434 435
  USHORT		format;		/* Format identifier */
  LigatureSubstFormat1	format1[];
436 437
  } u;
};
B
Behdad Esfahbod 已提交
438
ASSERT_SIZE (LigatureSubst, 2);
439

B
Behdad Esfahbod 已提交
440

441

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

B
Behdad Esfahbod 已提交
444 445
struct ContextSubst : Context
{
446 447 448
  friend struct SubstLookupSubTable;

  private:
B
Behdad Esfahbod 已提交
449
  inline bool apply (APPLY_ARG_DEF) const
B
Behdad Esfahbod 已提交
450
  { return Context::apply (APPLY_ARG, substitute_lookup); }
451
};
B
Behdad Esfahbod 已提交
452
ASSERT_SIZE (ContextSubst, 2);
453

B
Behdad Esfahbod 已提交
454 455
struct ChainContextSubst : ChainContext
{
456 457 458
  friend struct SubstLookupSubTable;

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

464

465
struct ExtensionSubst : Extension
B
Behdad Esfahbod 已提交
466
{
467 468 469
  friend struct SubstLookupSubTable;

  private:
470
  inline bool apply (APPLY_ARG_DEF) const;
471
};
B
Behdad Esfahbod 已提交
472
ASSERT_SIZE (ExtensionSubst, 2);
473 474


B
Behdad Esfahbod 已提交
475 476
struct ReverseChainSingleSubstFormat1
{
477 478 479
  friend struct ReverseChainSingleSubst;

  private:
B
Behdad Esfahbod 已提交
480 481
  inline bool apply (APPLY_ARG_DEF) const
  {
482 483 484 485 486 487 488
    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;

489 490 491 492
    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 ());
493

494
    if (match_backtrack (APPLY_ARG,
495 496
			 backtrack.len, (USHORT *) backtrack.array,
			 match_coverage, (char *) this) &&
497
        match_lookahead (APPLY_ARG,
498 499 500 501
			 lookahead.len, (USHORT *) lookahead.array,
			 match_coverage, (char *) this,
			 1))
    {
502
      IN_CURGLYPH () = substitute[index];
503 504 505 506
      buffer->in_pos--; /* Reverse! */
      return true;
    }

507 508
    return false;
  }
B
Behdad Esfahbod 已提交
509 510

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

B
Behdad Esfahbod 已提交
529 530
struct ReverseChainSingleSubst
{
531 532 533
  friend struct SubstLookupSubTable;

  private:
B
Behdad Esfahbod 已提交
534 535
  inline bool apply (APPLY_ARG_DEF) const
  {
536
    switch (u.format) {
537
    case 1: return u.format1->apply (APPLY_ARG);
538 539 540 541 542 543 544 545 546 547 548 549 550 551
    default:return false;
    }
  }

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



B
Behdad Esfahbod 已提交
552 553 554 555
/*
 * SubstLookup
 */

B
Behdad Esfahbod 已提交
556 557
struct SubstLookupSubTable
{
B
Behdad Esfahbod 已提交
558 559
  friend struct SubstLookup;

560 561 562 563 564 565 566 567 568 569 570
  enum {
    Single		= 1,
    Multiple		= 2,
    Alternate		= 3,
    Ligature		= 4,
    Context		= 5,
    ChainContext	= 6,
    Extension		= 7,
    ReverseChainSingle	= 8,
  };

571
  bool apply (APPLY_ARG_DEF, unsigned int lookup_type) const
B
Behdad Esfahbod 已提交
572
  {
573
    switch (lookup_type) {
574 575 576 577 578 579 580 581
    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);
582 583
    default:return false;
    }
B
Behdad Esfahbod 已提交
584 585 586 587
  }

  private:
  union {
588 589 590 591 592 593
  USHORT			format;
  SingleSubst			single[];
  MultipleSubst			multiple[];
  AlternateSubst		alternate[];
  LigatureSubst			ligature[];
  ContextSubst			context[];
B
Behdad Esfahbod 已提交
594
  ChainContextSubst		chainContext[];
595 596
  ExtensionSubst		extension[];
  ReverseChainSingleSubst	reverseChainContextSingle[];
B
Behdad Esfahbod 已提交
597 598
  } u;
};
B
Behdad Esfahbod 已提交
599
ASSERT_SIZE (SubstLookupSubTable, 2);
B
Behdad Esfahbod 已提交
600

601

B
Behdad Esfahbod 已提交
602 603 604
struct SubstLookup : Lookup
{
  inline const SubstLookupSubTable& get_subtable (unsigned int i) const
B
Behdad Esfahbod 已提交
605
  { return (const SubstLookupSubTable&) Lookup::get_subtable (i); }
B
Behdad Esfahbod 已提交
606 607

  /* Like get_type(), but looks through extension lookups.
608
   * Never returns Extension */
B
Behdad Esfahbod 已提交
609 610
  inline unsigned int get_effective_type (void) const
  {
B
Behdad Esfahbod 已提交
611 612
    unsigned int type = get_type ();

613
    if (HB_UNLIKELY (type == SubstLookupSubTable::Extension))
B
Behdad Esfahbod 已提交
614
    {
615
      unsigned int count = get_subtable_count ();
B
Behdad Esfahbod 已提交
616
      type = get_subtable(0).u.extension->get_type ();
617 618 619 620 621
      /* 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 已提交
622 623 624 625 626
    }

    return type;
  }

B
Behdad Esfahbod 已提交
627
  inline bool is_reverse (void) const
B
Behdad Esfahbod 已提交
628
  { return HB_UNLIKELY (get_effective_type () == SubstLookupSubTable::ReverseChainSingle); }
B
Behdad Esfahbod 已提交
629

B
XX  
Behdad Esfahbod 已提交
630
  inline bool apply_once (hb_ot_layout_context_t *context,
631 632 633
			  hb_buffer_t    *buffer,
			  unsigned int    context_length,
			  unsigned int    nesting_level_left) const
B
Behdad Esfahbod 已提交
634
  {
B
Behdad Esfahbod 已提交
635
    unsigned int lookup_type = get_type ();
B
Behdad Esfahbod 已提交
636
    unsigned int lookup_flag = get_flag ();
637 638
    unsigned int property;

B
XX  
Behdad Esfahbod 已提交
639
    if (!_hb_ot_layout_check_glyph_property (context->layout, IN_CURINFO (), lookup_flag, &property))
640
      return false;
B
Behdad Esfahbod 已提交
641 642

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

B
Behdad Esfahbod 已提交
646 647
    return false;
  }
648

B
XX  
Behdad Esfahbod 已提交
649
  bool apply_string (hb_ot_layout_context_t *context,
650
		     hb_buffer_t    *buffer,
B
Behdad Esfahbod 已提交
651 652
		     hb_ot_layout_feature_mask_t mask) const
  {
653 654
    bool ret = false;

B
Behdad Esfahbod 已提交
655 656 657
    if (HB_UNLIKELY (!buffer->in_length))
      return false;

B
Behdad Esfahbod 已提交
658 659
    if (HB_LIKELY (!is_reverse ()))
    {
660 661 662
	/* in/out forward substitution */
	_hb_buffer_clear_output (buffer);
	buffer->in_pos = 0;
B
Behdad Esfahbod 已提交
663 664
	while (buffer->in_pos < buffer->in_length)
	{
665
	  if ((~IN_PROPERTIES (buffer->in_pos) & mask) &&
B
XX  
Behdad Esfahbod 已提交
666
	      apply_once (context, buffer, NO_CONTEXT, MAX_NESTING_LEVEL))
667 668
	    ret = true;
	  else
B
Behdad Esfahbod 已提交
669
	    _hb_buffer_next_glyph (buffer);
670 671 672 673 674

	}
	if (ret)
	  _hb_buffer_swap (buffer);

B
Behdad Esfahbod 已提交
675 676 677
    }
    else
    {
678 679 680

	/* in-place backward substitution */
	buffer->in_pos = buffer->in_length - 1;
B
Behdad Esfahbod 已提交
681 682
	do
	{
683
	  if ((~IN_PROPERTIES (buffer->in_pos) & mask) &&
B
XX  
Behdad Esfahbod 已提交
684
	      apply_once (context, buffer, NO_CONTEXT, MAX_NESTING_LEVEL))
685 686 687 688
	    ret = true;
	  else
	    buffer->in_pos--;

B
Behdad Esfahbod 已提交
689 690
	}
	while ((int) buffer->in_pos >= 0);
691 692 693 694
    }

    return ret;
  }
B
Behdad Esfahbod 已提交
695
};
B
Behdad Esfahbod 已提交
696
ASSERT_SIZE (SubstLookup, 6);
B
Behdad Esfahbod 已提交
697

698

B
Minor  
Behdad Esfahbod 已提交
699 700 701 702
/*
 * GSUB
 */

B
Behdad Esfahbod 已提交
703 704
struct GSUB : GSUBGPOS
{
B
Minor  
Behdad Esfahbod 已提交
705 706
  static const hb_tag_t Tag		= HB_TAG ('G','S','U','B');

707
  static inline const GSUB& get_for_data (const char *data)
B
Behdad Esfahbod 已提交
708
  { return (const GSUB&) GSUBGPOS::get_for_data (data); }
B
Behdad Esfahbod 已提交
709

B
Behdad Esfahbod 已提交
710
  inline const SubstLookup& get_lookup (unsigned int i) const
B
Behdad Esfahbod 已提交
711
  { return (const SubstLookup&) GSUBGPOS::get_lookup (i); }
B
Behdad Esfahbod 已提交
712

B
XX  
Behdad Esfahbod 已提交
713
  inline bool substitute_lookup (hb_ot_layout_context_t *context,
714 715
				 hb_buffer_t    *buffer,
			         unsigned int    lookup_index,
B
Behdad Esfahbod 已提交
716
				 hb_ot_layout_feature_mask_t  mask) const
B
XX  
Behdad Esfahbod 已提交
717
  { return get_lookup (lookup_index).apply_string (context, buffer, mask); }
B
Behdad Esfahbod 已提交
718

B
Minor  
Behdad Esfahbod 已提交
719
};
B
Behdad Esfahbod 已提交
720
ASSERT_SIZE (GSUB, 10);
721 722


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

725
inline bool ExtensionSubst::apply (APPLY_ARG_DEF) const
B
Behdad Esfahbod 已提交
726
{
727 728
  unsigned int lookup_type = get_type ();

729
  if (HB_UNLIKELY (lookup_type == SubstLookupSubTable::Extension))
730 731
    return false;

732
  return ((SubstLookupSubTable&) get_subtable ()).apply (APPLY_ARG, lookup_type);
733 734
}

B
Behdad Esfahbod 已提交
735 736
static inline bool substitute_lookup (APPLY_ARG_DEF, unsigned int lookup_index)
{
B
XX  
Behdad Esfahbod 已提交
737
  const GSUB &gsub = *(context->layout->gsub);
738
  const SubstLookup &l = gsub.get_lookup (lookup_index);
739

740 741 742 743 744 745 746
  if (HB_UNLIKELY (nesting_level_left == 0))
    return false;
  nesting_level_left--;

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

B
XX  
Behdad Esfahbod 已提交
747
  return l.apply_once (context, buffer, context_length, nesting_level_left);
748 749 750
}


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