hb-ot-layout-gsub-private.hh 23.1 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
 *
 *  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
 */

27 28
#ifndef HB_OT_LAYOUT_GSUB_PRIVATE_HH
#define HB_OT_LAYOUT_GSUB_PRIVATE_HH
B
Behdad Esfahbod 已提交
29

30
#include "hb-ot-layout-gsubgpos-private.hh"
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

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

    glyph_id += deltaGlyphID;
B
Behdad Esfahbod 已提交
48 49
    _hb_buffer_replace_glyph (buffer, glyph_id);

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

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

B
Behdad Esfahbod 已提交
57
  inline bool sanitize (SANITIZE_ARG_DEF) {
58
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
59 60 61
    return SANITIZE_THIS (coverage) && SANITIZE (deltaGlyphID);
  }

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

B
Behdad Esfahbod 已提交
72 73
struct SingleSubstFormat2
{
74 75 76
  friend struct SingleSubst;

  private:
B
Behdad Esfahbod 已提交
77

B
Behdad Esfahbod 已提交
78 79
  inline bool apply (APPLY_ARG_DEF) const
  {
80
    TRACE_APPLY ();
B
Behdad Esfahbod 已提交
81
    hb_codepoint_t glyph_id = IN_CURGLYPH ();
B
Behdad Esfahbod 已提交
82
    unsigned int index = (this+coverage) (glyph_id);
B
Behdad Esfahbod 已提交
83
    if (HB_LIKELY (index == NOT_COVERED))
84
      return false;
85

B
Behdad Esfahbod 已提交
86
    if (HB_UNLIKELY (index >= substitute.len))
87 88 89
      return false;

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

92
    /* We inherit the old glyph class to the substituted glyph */
93 94
    if (_hb_ot_layout_has_new_glyph_classes (context->face))
      _hb_ot_layout_set_glyph_property (context->face, glyph_id, property);
B
Behdad Esfahbod 已提交
95

96 97
    return true;
  }
B
Behdad Esfahbod 已提交
98

B
Behdad Esfahbod 已提交
99
  inline bool sanitize (SANITIZE_ARG_DEF) {
100
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
101 102 103
    return SANITIZE_THIS (coverage) && SANITIZE (substitute);
  }

B
Behdad Esfahbod 已提交
104
  private:
B
Behdad Esfahbod 已提交
105
  USHORT	format;			/* Format identifier--format = 2 */
B
Behdad Esfahbod 已提交
106 107
  OffsetTo<Coverage>
		coverage;		/* Offset to Coverage table--from
B
Behdad Esfahbod 已提交
108
					 * beginning of Substitution table */
B
Behdad Esfahbod 已提交
109 110 111
  ArrayOf<GlyphID>
		substitute;		/* Array of substitute
					 * GlyphIDs--ordered by Coverage Index */
B
Behdad Esfahbod 已提交
112 113 114
};
ASSERT_SIZE (SingleSubstFormat2, 6);

B
Behdad Esfahbod 已提交
115 116
struct SingleSubst
{
117 118
  friend struct SubstLookupSubTable;

119
  private:
B
Behdad Esfahbod 已提交
120

B
Behdad Esfahbod 已提交
121 122
  inline bool apply (APPLY_ARG_DEF) const
  {
123
    TRACE_APPLY ();
B
Behdad Esfahbod 已提交
124
    switch (u.format) {
B
Behdad Esfahbod 已提交
125 126
    case 1: return u.format1->apply (APPLY_ARG);
    case 2: return u.format2->apply (APPLY_ARG);
B
Behdad Esfahbod 已提交
127 128 129 130
    default:return false;
    }
  }

B
Behdad Esfahbod 已提交
131
  inline bool sanitize (SANITIZE_ARG_DEF) {
132
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
133 134 135 136 137 138 139 140
    if (!SANITIZE (u.format)) return false;
    switch (u.format) {
    case 1: return u.format1->sanitize (SANITIZE_ARG);
    case 2: return u.format2->sanitize (SANITIZE_ARG);
    default:return true;
    }
  }

141 142
  private:
  union {
B
Behdad Esfahbod 已提交
143
  USHORT		format;		/* Format identifier */
B
Behdad Esfahbod 已提交
144 145
  SingleSubstFormat1	format1[VAR];
  SingleSubstFormat2	format2[VAR];
146
  } u;
B
Behdad Esfahbod 已提交
147
};
148

B
Behdad Esfahbod 已提交
149

B
Behdad Esfahbod 已提交
150 151
struct Sequence
{
152 153 154
  friend struct MultipleSubstFormat1;

  private:
B
Behdad Esfahbod 已提交
155 156
  inline bool apply (APPLY_ARG_DEF) const
  {
157
    TRACE_APPLY ();
B
Behdad Esfahbod 已提交
158
    if (HB_UNLIKELY (!substitute.len))
B
Behdad Esfahbod 已提交
159 160
      return false;

161 162 163
    _hb_buffer_add_output_glyphs_be16 (buffer, 1,
				       substitute.len, (const uint16_t *) substitute.const_array(),
				       0xFFFF, 0xFFFF);
B
Behdad Esfahbod 已提交
164

165
    /* This is a guess only ... */
166
    if (_hb_ot_layout_has_new_glyph_classes (context->face))
B
Behdad Esfahbod 已提交
167
    {
168
      if (property == HB_OT_LAYOUT_GLYPH_CLASS_LIGATURE)
B
Behdad Esfahbod 已提交
169 170
        property = HB_OT_LAYOUT_GLYPH_CLASS_BASE_GLYPH;

171 172
      unsigned int count = substitute.len;
      for (unsigned int n = 0; n < count; n++)
173
	_hb_ot_layout_set_glyph_property (context->face, substitute[n], property);
B
Behdad Esfahbod 已提交
174 175 176 177 178
    }

    return true;
  }

B
Behdad Esfahbod 已提交
179 180
  public:
  inline bool sanitize (SANITIZE_ARG_DEF) {
181
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
182 183 184
    return SANITIZE (substitute);
  }

B
Behdad Esfahbod 已提交
185
  private:
B
Behdad Esfahbod 已提交
186 187
  ArrayOf<GlyphID>
		substitute;		/* String of GlyphIDs to substitute */
B
Behdad Esfahbod 已提交
188
};
189
ASSERT_SIZE (Sequence, 2);
B
Behdad Esfahbod 已提交
190

B
Behdad Esfahbod 已提交
191 192
struct MultipleSubstFormat1
{
193 194 195
  friend struct MultipleSubst;

  private:
B
Behdad Esfahbod 已提交
196

B
Behdad Esfahbod 已提交
197 198
  inline bool apply (APPLY_ARG_DEF) const
  {
199
    TRACE_APPLY ();
200

B
Behdad Esfahbod 已提交
201
    unsigned int index = (this+coverage) (IN_CURGLYPH ());
B
Behdad Esfahbod 已提交
202
    if (HB_LIKELY (index == NOT_COVERED))
203 204
      return false;

205
    return (this+sequence[index]).apply (APPLY_ARG);
206
  }
B
Behdad Esfahbod 已提交
207

B
Behdad Esfahbod 已提交
208
  inline bool sanitize (SANITIZE_ARG_DEF) {
209
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
210 211 212
    return SANITIZE_THIS2 (coverage, sequence);
  }

B
Behdad Esfahbod 已提交
213
  private:
B
Behdad Esfahbod 已提交
214
  USHORT	format;			/* Format identifier--format = 1 */
B
Behdad Esfahbod 已提交
215 216
  OffsetTo<Coverage>
		coverage;		/* Offset to Coverage table--from
B
Behdad Esfahbod 已提交
217
					 * beginning of Substitution table */
B
Behdad Esfahbod 已提交
218 219 220
  OffsetArrayOf<Sequence>
		sequence;		/* Array of Sequence tables
					 * ordered by Coverage Index */
B
Behdad Esfahbod 已提交
221
};
222 223
ASSERT_SIZE (MultipleSubstFormat1, 6);

B
Behdad Esfahbod 已提交
224 225
struct MultipleSubst
{
226 227 228
  friend struct SubstLookupSubTable;

  private:
B
Behdad Esfahbod 已提交
229

B
Behdad Esfahbod 已提交
230 231
  inline bool apply (APPLY_ARG_DEF) const
  {
232
    TRACE_APPLY ();
B
Behdad Esfahbod 已提交
233
    switch (u.format) {
234
    case 1: return u.format1->apply (APPLY_ARG);
235 236 237 238
    default:return false;
    }
  }

B
Behdad Esfahbod 已提交
239
  inline bool sanitize (SANITIZE_ARG_DEF) {
240
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
241 242 243 244 245 246 247
    if (!SANITIZE (u.format)) return false;
    switch (u.format) {
    case 1: return u.format1->sanitize (SANITIZE_ARG);
    default:return true;
    }
  }

248 249
  private:
  union {
B
Behdad Esfahbod 已提交
250
  USHORT		format;		/* Format identifier */
B
Behdad Esfahbod 已提交
251
  MultipleSubstFormat1	format1[VAR];
252 253 254
  } u;
};

B
Behdad Esfahbod 已提交
255

B
Behdad Esfahbod 已提交
256
typedef ArrayOf<GlyphID> AlternateSet;	/* Array of alternate GlyphIDs--in
B
Behdad Esfahbod 已提交
257
					 * arbitrary order */
258
ASSERT_SIZE (AlternateSet, 2);
B
Behdad Esfahbod 已提交
259

B
Behdad Esfahbod 已提交
260 261
struct AlternateSubstFormat1
{
262 263 264
  friend struct AlternateSubst;

  private:
B
Behdad Esfahbod 已提交
265

B
Behdad Esfahbod 已提交
266 267
  inline bool apply (APPLY_ARG_DEF) const
  {
268
    TRACE_APPLY ();
269
    hb_codepoint_t glyph_id = IN_CURGLYPH ();
270

B
Behdad Esfahbod 已提交
271
    unsigned int index = (this+coverage) (glyph_id);
B
Behdad Esfahbod 已提交
272
    if (HB_LIKELY (index == NOT_COVERED))
273 274
      return false;

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

B
Behdad Esfahbod 已提交
277
    if (HB_UNLIKELY (!alt_set.len))
278 279 280 281 282
      return false;

    unsigned int alt_index = 0;

    /* XXX callback to user to choose alternate
283 284
    if (context->face->altfunc)
      alt_index = (context->face->altfunc)(context->layout, buffer,
B
Behdad Esfahbod 已提交
285 286
				    buffer->out_pos, glyph_id,
				    alt_set.len, alt_set.array);
287 288
				   */

B
Behdad Esfahbod 已提交
289
    if (HB_UNLIKELY (alt_index >= alt_set.len))
290 291 292 293
      return false;

    glyph_id = alt_set[alt_index];

B
Behdad Esfahbod 已提交
294
    _hb_buffer_replace_glyph (buffer, glyph_id);
295

296
    /* We inherit the old glyph class to the substituted glyph */
297 298
    if (_hb_ot_layout_has_new_glyph_classes (context->face))
      _hb_ot_layout_set_glyph_property (context->face, glyph_id, property);
299 300 301

    return true;
  }
B
Behdad Esfahbod 已提交
302

B
Behdad Esfahbod 已提交
303
  inline bool sanitize (SANITIZE_ARG_DEF) {
304
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
305 306 307
    return SANITIZE_THIS2 (coverage, alternateSet);
  }

B
Behdad Esfahbod 已提交
308
  private:
B
Behdad Esfahbod 已提交
309
  USHORT	format;			/* Format identifier--format = 1 */
B
Behdad Esfahbod 已提交
310 311
  OffsetTo<Coverage>
		coverage;		/* Offset to Coverage table--from
B
Behdad Esfahbod 已提交
312
					 * beginning of Substitution table */
B
Behdad Esfahbod 已提交
313 314 315
  OffsetArrayOf<AlternateSet>
		alternateSet;		/* Array of AlternateSet tables
					 * ordered by Coverage Index */
B
Behdad Esfahbod 已提交
316
};
317 318
ASSERT_SIZE (AlternateSubstFormat1, 6);

B
Behdad Esfahbod 已提交
319 320
struct AlternateSubst
{
321 322 323
  friend struct SubstLookupSubTable;

  private:
B
Behdad Esfahbod 已提交
324

B
Behdad Esfahbod 已提交
325 326
  inline bool apply (APPLY_ARG_DEF) const
  {
327
    TRACE_APPLY ();
B
Behdad Esfahbod 已提交
328
    switch (u.format) {
329
    case 1: return u.format1->apply (APPLY_ARG);
330 331 332 333
    default:return false;
    }
  }

B
Behdad Esfahbod 已提交
334
  inline bool sanitize (SANITIZE_ARG_DEF) {
335
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
336 337 338 339 340 341 342
    if (!SANITIZE (u.format)) return false;
    switch (u.format) {
    case 1: return u.format1->sanitize (SANITIZE_ARG);
    default:return true;
    }
  }

343 344
  private:
  union {
B
Behdad Esfahbod 已提交
345
  USHORT		format;		/* Format identifier */
B
Behdad Esfahbod 已提交
346
  AlternateSubstFormat1	format1[VAR];
347 348 349
  } u;
};

350

B
Behdad Esfahbod 已提交
351 352
struct Ligature
{
353 354 355
  friend struct LigatureSet;

  private:
B
Behdad Esfahbod 已提交
356 357
  inline bool apply (APPLY_ARG_DEF, bool is_mark) const
  {
358
    TRACE_APPLY ();
359
    unsigned int i, j;
B
Behdad Esfahbod 已提交
360
    unsigned int count = component.len;
361 362 363
    unsigned int end = MIN (buffer->in_length, buffer->in_pos + context_length);
    if (HB_UNLIKELY (buffer->in_pos + count > end))
      return false;
364

B
Behdad Esfahbod 已提交
365 366
    for (i = 1, j = buffer->in_pos + 1; i < count; i++, j++)
    {
367
      while (_hb_ot_layout_skip_mark (context->face, IN_INFO (j), lookup_flag, &property))
B
Behdad Esfahbod 已提交
368
      {
369
	if (HB_UNLIKELY (j + count - i == end))
370 371 372 373
	  return false;
	j++;
      }

374
      if (!(property & HB_OT_LAYOUT_GLYPH_CLASS_MARK))
B
Behdad Esfahbod 已提交
375
	is_mark = false;
376

377
      if (HB_LIKELY (IN_GLYPH (j) != component[i]))
378 379
        return false;
    }
380
    /* This is just a guess ... */
381 382
    if (_hb_ot_layout_has_new_glyph_classes (context->face))
      _hb_ot_layout_set_glyph_class (context->face, ligGlyph,
383 384
				     is_mark ? HB_OT_LAYOUT_GLYPH_CLASS_MARK
					     : HB_OT_LAYOUT_GLYPH_CLASS_LIGATURE);
385 386 387 388

    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. */
389 390
      _hb_buffer_add_output_glyphs_be16 (buffer, i,
					 1, (const uint16_t *) &ligGlyph,
391 392
					 0,
					 IN_LIGID (buffer->in_pos) && !IN_COMPONENT (buffer->in_pos) ?
393
					 0xFFFF : _hb_buffer_allocate_lig_id (buffer));
394 395
    else
    {
B
Behdad Esfahbod 已提交
396
      unsigned int lig_id = _hb_buffer_allocate_lig_id (buffer);
397 398 399 400 401 402 403 404 405
      _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 已提交
406
      for ( i = 1; i < count; i++ )
407
      {
408
	while (_hb_ot_layout_skip_mark (context->face, IN_CURINFO (), lookup_flag, NULL))
409
	  _hb_buffer_add_output_glyph (buffer, IN_CURGLYPH (), i, lig_id);
410 411 412 413 414 415 416

	(buffer->in_pos)++;
      }
    }

    return true;
  }
417

B
Behdad Esfahbod 已提交
418 419
  public:
  inline bool sanitize (SANITIZE_ARG_DEF) {
420
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
421 422 423
    return SANITIZE2 (ligGlyph, component);
  }

424 425
  private:
  GlyphID	ligGlyph;		/* GlyphID of ligature to substitute */
B
Behdad Esfahbod 已提交
426 427
  HeadlessArrayOf<GlyphID>
		component;		/* Array of component GlyphIDs--start
428 429 430
					 * with the second  component--ordered
					 * in writing direction */
};
431
ASSERT_SIZE (Ligature, 4);
B
Behdad Esfahbod 已提交
432

B
Behdad Esfahbod 已提交
433 434
struct LigatureSet
{
435 436 437
  friend struct LigatureSubstFormat1;

  private:
B
Behdad Esfahbod 已提交
438 439
  inline bool apply (APPLY_ARG_DEF, bool is_mark) const
  {
440
    TRACE_APPLY ();
B
Behdad Esfahbod 已提交
441
    unsigned int num_ligs = ligature.len;
B
Behdad Esfahbod 已提交
442 443
    for (unsigned int i = 0; i < num_ligs; i++)
    {
B
Behdad Esfahbod 已提交
444
      const Ligature &lig = this+ligature[i];
445
      if (lig.apply (APPLY_ARG, is_mark))
446 447 448 449 450
        return true;
    }

    return false;
  }
B
Behdad Esfahbod 已提交
451

B
Behdad Esfahbod 已提交
452 453
  public:
  inline bool sanitize (SANITIZE_ARG_DEF) {
454
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
455 456 457
    return SANITIZE_THIS (ligature);
  }

B
Behdad Esfahbod 已提交
458
  private:
B
Behdad Esfahbod 已提交
459 460 461
  OffsetArrayOf<Ligature>
		ligature;		/* Array LigatureSet tables
					 * ordered by preference */
B
Behdad Esfahbod 已提交
462
};
463
ASSERT_SIZE (LigatureSet, 2);
B
Behdad Esfahbod 已提交
464

B
Behdad Esfahbod 已提交
465 466
struct LigatureSubstFormat1
{
467 468 469
  friend struct LigatureSubst;

  private:
B
Behdad Esfahbod 已提交
470 471
  inline bool apply (APPLY_ARG_DEF) const
  {
472
    TRACE_APPLY ();
473 474
    hb_codepoint_t glyph_id = IN_CURGLYPH ();

475
    bool first_is_mark = !!(property & HB_OT_LAYOUT_GLYPH_CLASS_MARK);
476

B
Behdad Esfahbod 已提交
477
    unsigned int index = (this+coverage) (glyph_id);
B
Behdad Esfahbod 已提交
478
    if (HB_LIKELY (index == NOT_COVERED))
479 480
      return false;

B
Behdad Esfahbod 已提交
481
    const LigatureSet &lig_set = this+ligatureSet[index];
482
    return lig_set.apply (APPLY_ARG, first_is_mark);
483
  }
B
Behdad Esfahbod 已提交
484

B
Behdad Esfahbod 已提交
485
  inline bool sanitize (SANITIZE_ARG_DEF) {
486
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
487 488 489
    return SANITIZE_THIS2 (coverage, ligatureSet);
  }

B
Behdad Esfahbod 已提交
490
  private:
B
Behdad Esfahbod 已提交
491
  USHORT	format;			/* Format identifier--format = 1 */
B
Behdad Esfahbod 已提交
492 493
  OffsetTo<Coverage>
		coverage;		/* Offset to Coverage table--from
494
					 * beginning of Substitution table */
B
Behdad Esfahbod 已提交
495
  OffsetArrayOf<LigatureSet>
B
Behdad Esfahbod 已提交
496 497
		ligatureSet;		/* Array LigatureSet tables
					 * ordered by Coverage Index */
B
Behdad Esfahbod 已提交
498
};
499 500
ASSERT_SIZE (LigatureSubstFormat1, 6);

B
Behdad Esfahbod 已提交
501 502
struct LigatureSubst
{
503 504 505
  friend struct SubstLookupSubTable;

  private:
B
Behdad Esfahbod 已提交
506 507
  inline bool apply (APPLY_ARG_DEF) const
  {
508
    TRACE_APPLY ();
B
Behdad Esfahbod 已提交
509
    switch (u.format) {
510
    case 1: return u.format1->apply (APPLY_ARG);
511 512 513 514
    default:return false;
    }
  }

B
Behdad Esfahbod 已提交
515
  inline bool sanitize (SANITIZE_ARG_DEF) {
516
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
517 518 519 520 521 522 523
    if (!SANITIZE (u.format)) return false;
    switch (u.format) {
    case 1: return u.format1->sanitize (SANITIZE_ARG);
    default:return true;
    }
  }

524 525
  private:
  union {
B
Behdad Esfahbod 已提交
526
  USHORT		format;		/* Format identifier */
B
Behdad Esfahbod 已提交
527
  LigatureSubstFormat1	format1[VAR];
528 529 530
  } u;
};

B
Behdad Esfahbod 已提交
531

532

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

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

  private:
B
Behdad Esfahbod 已提交
540
  inline bool apply (APPLY_ARG_DEF) const
B
Behdad Esfahbod 已提交
541
  {
542
    TRACE_APPLY ();
B
Behdad Esfahbod 已提交
543 544
    return Context::apply (APPLY_ARG, substitute_lookup);
  }
545
};
546

B
Behdad Esfahbod 已提交
547 548
struct ChainContextSubst : ChainContext
{
549 550 551
  friend struct SubstLookupSubTable;

  private:
B
Behdad Esfahbod 已提交
552
  inline bool apply (APPLY_ARG_DEF) const
B
Behdad Esfahbod 已提交
553
  {
554
    TRACE_APPLY ();
B
Behdad Esfahbod 已提交
555 556
    return ChainContext::apply (APPLY_ARG, substitute_lookup);
  }
B
Behdad Esfahbod 已提交
557 558
};

559

560
struct ExtensionSubst : Extension
B
Behdad Esfahbod 已提交
561
{
562 563 564
  friend struct SubstLookupSubTable;

  private:
B
Behdad Esfahbod 已提交
565
  inline const struct SubstLookupSubTable& get_subtable (void) const
B
Behdad Esfahbod 已提交
566
  { return CONST_CAST (SubstLookupSubTable, Extension::get_subtable (), 0); }
B
Behdad Esfahbod 已提交
567

568
  inline bool apply (APPLY_ARG_DEF) const;
B
Behdad Esfahbod 已提交
569 570

  inline bool sanitize (SANITIZE_ARG_DEF);
571 572 573
};


B
Behdad Esfahbod 已提交
574 575
struct ReverseChainSingleSubstFormat1
{
576 577 578
  friend struct ReverseChainSingleSubst;

  private:
B
Behdad Esfahbod 已提交
579 580
  inline bool apply (APPLY_ARG_DEF) const
  {
581
    TRACE_APPLY ();
582 583 584 585 586 587 588
    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;

B
Behdad Esfahbod 已提交
589 590
    const OffsetArrayOf<Coverage> &lookahead = CONST_NEXT (OffsetArrayOf<Coverage>, backtrack);
    const ArrayOf<GlyphID> &substitute = CONST_NEXT (ArrayOf<GlyphID>, lookahead);
591

592
    if (match_backtrack (APPLY_ARG,
B
Behdad Esfahbod 已提交
593
			 backtrack.len, (USHORT *) backtrack.const_array(),
B
Behdad Esfahbod 已提交
594
			 match_coverage, CONST_CHARP(this)) &&
595
        match_lookahead (APPLY_ARG,
B
Behdad Esfahbod 已提交
596
			 lookahead.len, (USHORT *) lookahead.const_array(),
B
Behdad Esfahbod 已提交
597
			 match_coverage, CONST_CHARP(this),
598 599
			 1))
    {
600
      IN_CURGLYPH () = substitute[index];
601 602 603 604
      buffer->in_pos--; /* Reverse! */
      return true;
    }

605 606
    return false;
  }
B
Behdad Esfahbod 已提交
607

B
Behdad Esfahbod 已提交
608
  inline bool sanitize (SANITIZE_ARG_DEF) {
609
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
610 611
    if (!SANITIZE_THIS2 (coverage, backtrack))
      return false;
B
Behdad Esfahbod 已提交
612
    OffsetArrayOf<Coverage> &lookahead = NEXT (OffsetArrayOf<Coverage>, backtrack);
B
Behdad Esfahbod 已提交
613 614
    if (!SANITIZE_THIS (lookahead))
      return false;
B
Behdad Esfahbod 已提交
615
    ArrayOf<GlyphID> &substitute = NEXT (ArrayOf<GlyphID>, lookahead);
B
Behdad Esfahbod 已提交
616
    return SANITIZE (substitute);
B
Behdad Esfahbod 已提交
617 618
  }

B
Behdad Esfahbod 已提交
619
  private:
B
Behdad Esfahbod 已提交
620
  USHORT	format;			/* Format identifier--format = 1 */
621 622 623 624 625
  OffsetTo<Coverage>
		coverage;		/* Offset to Coverage table--from
					 * beginning of table */
  OffsetArrayOf<Coverage>
		backtrack;		/* Array of coverage tables
B
Behdad Esfahbod 已提交
626 627
					 * in backtracking sequence, in  glyph
					 * sequence order */
628 629 630
  OffsetArrayOf<Coverage>
		lookaheadX;		/* Array of coverage tables
					 * in lookahead sequence, in glyph
B
Behdad Esfahbod 已提交
631
					 * sequence order */
632 633 634
  ArrayOf<GlyphID>
		substituteX;		/* Array of substitute
					 * GlyphIDs--ordered by Coverage Index */
B
Behdad Esfahbod 已提交
635 636 637
};
ASSERT_SIZE (ReverseChainSingleSubstFormat1, 10);

B
Behdad Esfahbod 已提交
638 639
struct ReverseChainSingleSubst
{
640 641 642
  friend struct SubstLookupSubTable;

  private:
B
Behdad Esfahbod 已提交
643 644
  inline bool apply (APPLY_ARG_DEF) const
  {
645
    TRACE_APPLY ();
646
    switch (u.format) {
647
    case 1: return u.format1->apply (APPLY_ARG);
648 649 650 651
    default:return false;
    }
  }

B
Behdad Esfahbod 已提交
652
  inline bool sanitize (SANITIZE_ARG_DEF) {
653
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
654 655 656 657 658 659 660
    if (!SANITIZE (u.format)) return false;
    switch (u.format) {
    case 1: return u.format1->sanitize (SANITIZE_ARG);
    default:return true;
    }
  }

661 662 663
  private:
  union {
  USHORT				format;		/* Format identifier */
B
Behdad Esfahbod 已提交
664
  ReverseChainSingleSubstFormat1	format1[VAR];
665 666 667 668 669
  } u;
};



B
Behdad Esfahbod 已提交
670 671 672 673
/*
 * SubstLookup
 */

B
Behdad Esfahbod 已提交
674 675
struct SubstLookupSubTable
{
B
Behdad Esfahbod 已提交
676 677
  friend struct SubstLookup;

678 679 680 681 682 683 684 685
  enum {
    Single		= 1,
    Multiple		= 2,
    Alternate		= 3,
    Ligature		= 4,
    Context		= 5,
    ChainContext	= 6,
    Extension		= 7,
686
    ReverseChainSingle	= 8
687 688
  };

B
Behdad Esfahbod 已提交
689
  inline bool apply (APPLY_ARG_DEF, unsigned int lookup_type) const
B
Behdad Esfahbod 已提交
690
  {
691
    TRACE_APPLY ();
692
    switch (lookup_type) {
693 694 695 696 697 698 699 700
    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);
701 702
    default:return false;
    }
B
Behdad Esfahbod 已提交
703 704
  }

B
Behdad Esfahbod 已提交
705
  inline bool sanitize (SANITIZE_ARG_DEF) {
706
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
707 708 709 710 711 712 713 714 715 716 717 718 719 720
    if (!SANITIZE (u.format)) return false;
    switch (u.format) {
    case Single:		return u.single->sanitize (SANITIZE_ARG);
    case Multiple:		return u.multiple->sanitize (SANITIZE_ARG);
    case Alternate:		return u.alternate->sanitize (SANITIZE_ARG);
    case Ligature:		return u.ligature->sanitize (SANITIZE_ARG);
    case Context:		return u.context->sanitize (SANITIZE_ARG);
    case ChainContext:		return u.chainContext->sanitize (SANITIZE_ARG);
    case Extension:		return u.extension->sanitize (SANITIZE_ARG);
    case ReverseChainSingle:	return u.reverseChainContextSingle->sanitize (SANITIZE_ARG);
    default:return true;
    }
  }

B
Behdad Esfahbod 已提交
721 722
  private:
  union {
723
  USHORT			format;
B
Behdad Esfahbod 已提交
724 725 726 727 728 729 730 731
  SingleSubst			single[VAR];
  MultipleSubst			multiple[VAR];
  AlternateSubst		alternate[VAR];
  LigatureSubst			ligature[VAR];
  ContextSubst			context[VAR];
  ChainContextSubst		chainContext[VAR];
  ExtensionSubst		extension[VAR];
  ReverseChainSingleSubst	reverseChainContextSingle[VAR];
B
Behdad Esfahbod 已提交
732 733 734
  } u;
};

735

B
Behdad Esfahbod 已提交
736 737 738
struct SubstLookup : Lookup
{
  inline const SubstLookupSubTable& get_subtable (unsigned int i) const
B
Behdad Esfahbod 已提交
739
  { return CONST_CAST (SubstLookupSubTable, Lookup::get_subtable (i), 0); }
B
Behdad Esfahbod 已提交
740 741

  /* Like get_type(), but looks through extension lookups.
742
   * Never returns Extension */
B
Behdad Esfahbod 已提交
743 744
  inline unsigned int get_effective_type (void) const
  {
B
Behdad Esfahbod 已提交
745 746
    unsigned int type = get_type ();

747
    if (HB_UNLIKELY (type == SubstLookupSubTable::Extension))
B
Behdad Esfahbod 已提交
748
    {
749
      unsigned int count = get_subtable_count ();
B
Behdad Esfahbod 已提交
750
      type = get_subtable(0).u.extension->get_type ();
751 752 753 754 755
      /* 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 已提交
756 757 758 759 760
    }

    return type;
  }

B
Behdad Esfahbod 已提交
761
  inline bool is_reverse (void) const
B
Behdad Esfahbod 已提交
762
  { return HB_UNLIKELY (get_effective_type () == SubstLookupSubTable::ReverseChainSingle); }
B
Behdad Esfahbod 已提交
763

764
  inline bool apply_once (hb_ot_layout_context_t *context,
765 766 767
			  hb_buffer_t    *buffer,
			  unsigned int    context_length,
			  unsigned int    nesting_level_left) const
B
Behdad Esfahbod 已提交
768
  {
B
Behdad Esfahbod 已提交
769
    unsigned int lookup_type = get_type ();
B
Behdad Esfahbod 已提交
770
    unsigned int lookup_flag = get_flag ();
771 772
    unsigned int property;

773
    if (!_hb_ot_layout_check_glyph_property (context->face, IN_CURINFO (), lookup_flag, &property))
774
      return false;
B
Behdad Esfahbod 已提交
775

B
Behdad Esfahbod 已提交
776 777
    unsigned int count = get_subtable_count ();
    for (unsigned int i = 0; i < count; i++)
B
Behdad Esfahbod 已提交
778
      if (get_subtable (i).apply (APPLY_ARG_INIT, lookup_type))
B
Behdad Esfahbod 已提交
779
	return true;
B
Behdad Esfahbod 已提交
780

B
Behdad Esfahbod 已提交
781 782
    return false;
  }
783

B
Behdad Esfahbod 已提交
784 785 786
  inline bool apply_string (hb_ot_layout_context_t *context,
			    hb_buffer_t *buffer,
			    hb_mask_t    mask) const
B
Behdad Esfahbod 已提交
787
  {
788 789
    bool ret = false;

B
Behdad Esfahbod 已提交
790 791 792
    if (HB_UNLIKELY (!buffer->in_length))
      return false;

B
Behdad Esfahbod 已提交
793 794
    if (HB_LIKELY (!is_reverse ()))
    {
795 796 797
	/* in/out forward substitution */
	_hb_buffer_clear_output (buffer);
	buffer->in_pos = 0;
B
Behdad Esfahbod 已提交
798 799
	while (buffer->in_pos < buffer->in_length)
	{
800
	  if ((~IN_MASK (buffer->in_pos) & mask) &&
801
	      apply_once (context, buffer, NO_CONTEXT, MAX_NESTING_LEVEL))
802 803
	    ret = true;
	  else
B
Behdad Esfahbod 已提交
804
	    _hb_buffer_next_glyph (buffer);
805 806 807 808

	}
	if (ret)
	  _hb_buffer_swap (buffer);
B
Behdad Esfahbod 已提交
809 810 811
    }
    else
    {
812 813
	/* in-place backward substitution */
	buffer->in_pos = buffer->in_length - 1;
B
Behdad Esfahbod 已提交
814 815
	do
	{
816
	  if ((~IN_MASK (buffer->in_pos) & mask) &&
817
	      apply_once (context, buffer, NO_CONTEXT, MAX_NESTING_LEVEL))
818 819 820 821
	    ret = true;
	  else
	    buffer->in_pos--;

B
Behdad Esfahbod 已提交
822 823
	}
	while ((int) buffer->in_pos >= 0);
824 825 826 827
    }

    return ret;
  }
B
Behdad Esfahbod 已提交
828 829

  inline bool sanitize (SANITIZE_ARG_DEF) {
830
    TRACE_SANITIZE ();
831
    if (!Lookup::sanitize (SANITIZE_ARG)) return false;
B
Behdad Esfahbod 已提交
832 833 834
    OffsetArrayOf<SubstLookupSubTable> &list = (OffsetArrayOf<SubstLookupSubTable> &) subTable;
    return SANITIZE_THIS (list);
  }
B
Behdad Esfahbod 已提交
835 836
};

B
Behdad Esfahbod 已提交
837 838
typedef OffsetListOf<SubstLookup> SubstLookupList;
ASSERT_SIZE (SubstLookupList, 2);
839

B
Minor  
Behdad Esfahbod 已提交
840 841 842 843
/*
 * GSUB
 */

B
Behdad Esfahbod 已提交
844 845
struct GSUB : GSUBGPOS
{
B
Behdad Esfahbod 已提交
846
  static const hb_tag_t Tag	= HB_OT_TAG_GSUB;
B
Minor  
Behdad Esfahbod 已提交
847

848
  static inline const GSUB& get_for_data (const char *data)
B
Behdad Esfahbod 已提交
849
  { return (const GSUB&) GSUBGPOS::get_for_data (data); }
B
Behdad Esfahbod 已提交
850

B
Behdad Esfahbod 已提交
851
  inline const SubstLookup& get_lookup (unsigned int i) const
B
Behdad Esfahbod 已提交
852
  { return (const SubstLookup&) GSUBGPOS::get_lookup (i); }
B
Behdad Esfahbod 已提交
853

854
  inline bool substitute_lookup (hb_ot_layout_context_t *context,
855 856 857
				 hb_buffer_t  *buffer,
			         unsigned int  lookup_index,
				 hb_mask_t     mask) const
858
  { return get_lookup (lookup_index).apply_string (context, buffer, mask); }
B
Behdad Esfahbod 已提交
859

B
Behdad Esfahbod 已提交
860

B
Behdad Esfahbod 已提交
861
  inline bool sanitize (SANITIZE_ARG_DEF) {
862
    TRACE_SANITIZE ();
863
    if (!GSUBGPOS::sanitize (SANITIZE_ARG)) return false;
B
Behdad Esfahbod 已提交
864
    OffsetTo<SubstLookupList> &list = CAST(OffsetTo<SubstLookupList>, lookupList, 0);
B
Behdad Esfahbod 已提交
865 866
    return SANITIZE_THIS (list);
  }
B
Minor  
Behdad Esfahbod 已提交
867
};
B
Behdad Esfahbod 已提交
868
ASSERT_SIZE (GSUB, 10);
869 870


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

873
inline bool ExtensionSubst::apply (APPLY_ARG_DEF) const
B
Behdad Esfahbod 已提交
874
{
875
  TRACE_APPLY ();
876 877
  unsigned int lookup_type = get_type ();

878
  if (HB_UNLIKELY (lookup_type == SubstLookupSubTable::Extension))
879 880
    return false;

B
Behdad Esfahbod 已提交
881 882 883 884 885
  return get_subtable ().apply (APPLY_ARG, lookup_type);
}

inline bool ExtensionSubst::sanitize (SANITIZE_ARG_DEF)
{
886
  TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
887
  return Extension::sanitize (SANITIZE_ARG) &&
B
Behdad Esfahbod 已提交
888
	 (&(Extension::get_subtable ()) == &Null(LookupSubTable) ||
889
	  get_type () == SubstLookupSubTable::Extension ||
B
Behdad Esfahbod 已提交
890
	  DECONST_CAST (SubstLookupSubTable, get_subtable (), 0).sanitize (SANITIZE_ARG));
891 892
}

B
Behdad Esfahbod 已提交
893 894
static inline bool substitute_lookup (APPLY_ARG_DEF, unsigned int lookup_index)
{
895
  const GSUB &gsub = *(context->face->ot_layout.gsub);
896
  const SubstLookup &l = gsub.get_lookup (lookup_index);
897

898 899 900 901 902 903 904
  if (HB_UNLIKELY (nesting_level_left == 0))
    return false;
  nesting_level_left--;

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

905
  return l.apply_once (context, buffer, context_length, nesting_level_left);
906 907 908
}


909
#endif /* HB_OT_LAYOUT_GSUB_PRIVATE_HH */