hb-ot-layout-gsub-private.hh 23.5 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 144 145
  USHORT		format;		/* Format identifier */
  SingleSubstFormat1	format1[];
  SingleSubstFormat2	format2[];
146
  } u;
B
Behdad Esfahbod 已提交
147
};
B
Behdad Esfahbod 已提交
148
ASSERT_SIZE (SingleSubst, 2);
149

B
Behdad Esfahbod 已提交
150

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

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

B
Behdad Esfahbod 已提交
162 163 164
    _hb_buffer_add_output_glyphs (buffer, 1,
				  substitute.len, (const uint16_t *) substitute.array,
				  0xFFFF, 0xFFFF);
B
Behdad Esfahbod 已提交
165

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

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

    return true;
  }

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

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

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

  private:
B
Behdad Esfahbod 已提交
197

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

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

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

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

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

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

  private:
B
Behdad Esfahbod 已提交
230

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

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

249 250
  private:
  union {
B
Behdad Esfahbod 已提交
251 252
  USHORT		format;		/* Format identifier */
  MultipleSubstFormat1	format1[];
253 254
  } u;
};
B
Behdad Esfahbod 已提交
255
ASSERT_SIZE (MultipleSubst, 2);
256

B
Behdad Esfahbod 已提交
257

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

B
Behdad Esfahbod 已提交
262 263
struct AlternateSubstFormat1
{
264 265 266
  friend struct AlternateSubst;

  private:
B
Behdad Esfahbod 已提交
267

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

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

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

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

    unsigned int alt_index = 0;

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

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

    glyph_id = alt_set[alt_index];

B
Behdad Esfahbod 已提交
296
    _hb_buffer_replace_glyph (buffer, glyph_id);
297

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

    return true;
  }
B
Behdad Esfahbod 已提交
304

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

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

B
Behdad Esfahbod 已提交
321 322
struct AlternateSubst
{
323 324 325
  friend struct SubstLookupSubTable;

  private:
B
Behdad Esfahbod 已提交
326

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

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

345 346
  private:
  union {
B
Behdad Esfahbod 已提交
347 348
  USHORT		format;		/* Format identifier */
  AlternateSubstFormat1	format1[];
349 350
  } u;
};
B
Behdad Esfahbod 已提交
351
ASSERT_SIZE (AlternateSubst, 2);
352

353

B
Behdad Esfahbod 已提交
354 355
struct Ligature
{
356 357 358
  friend struct LigatureSet;

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

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

377
      if (!(property & HB_OT_LAYOUT_GLYPH_CLASS_MARK))
378 379
	is_mark = FALSE;

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

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

	(buffer->in_pos)++;
      }

B
Behdad Esfahbod 已提交
417
      /* TODO We should possibly reassign lig_id and component for any
418 419 420 421 422 423
       * components of a previous ligature that s now being removed as part of
       * this ligature. */
    }

    return true;
  }
424

B
Behdad Esfahbod 已提交
425 426
  public:
  inline bool sanitize (SANITIZE_ARG_DEF) {
427
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
428 429 430
    return SANITIZE2 (ligGlyph, component);
  }

431 432
  private:
  GlyphID	ligGlyph;		/* GlyphID of ligature to substitute */
B
Behdad Esfahbod 已提交
433 434
  HeadlessArrayOf<GlyphID>
		component;		/* Array of component GlyphIDs--start
435 436 437
					 * with the second  component--ordered
					 * in writing direction */
};
438
ASSERT_SIZE (Ligature, 4);
B
Behdad Esfahbod 已提交
439

B
Behdad Esfahbod 已提交
440 441
struct LigatureSet
{
442 443 444
  friend struct LigatureSubstFormat1;

  private:
B
Behdad Esfahbod 已提交
445 446
  inline bool apply (APPLY_ARG_DEF, bool is_mark) const
  {
447
    TRACE_APPLY ();
B
Behdad Esfahbod 已提交
448
    unsigned int num_ligs = ligature.len;
B
Behdad Esfahbod 已提交
449 450
    for (unsigned int i = 0; i < num_ligs; i++)
    {
B
Behdad Esfahbod 已提交
451
      const Ligature &lig = this+ligature[i];
452
      if (lig.apply (APPLY_ARG, is_mark))
453 454 455 456 457
        return true;
    }

    return false;
  }
B
Behdad Esfahbod 已提交
458

B
Behdad Esfahbod 已提交
459 460
  public:
  inline bool sanitize (SANITIZE_ARG_DEF) {
461
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
462 463 464
    return SANITIZE_THIS (ligature);
  }

B
Behdad Esfahbod 已提交
465
  private:
B
Behdad Esfahbod 已提交
466 467 468
  OffsetArrayOf<Ligature>
		ligature;		/* Array LigatureSet tables
					 * ordered by preference */
B
Behdad Esfahbod 已提交
469
};
470
ASSERT_SIZE (LigatureSet, 2);
B
Behdad Esfahbod 已提交
471

B
Behdad Esfahbod 已提交
472 473
struct LigatureSubstFormat1
{
474 475 476
  friend struct LigatureSubst;

  private:
B
Behdad Esfahbod 已提交
477 478
  inline bool apply (APPLY_ARG_DEF) const
  {
479
    TRACE_APPLY ();
480 481
    hb_codepoint_t glyph_id = IN_CURGLYPH ();

482
    bool first_is_mark = !!(property & HB_OT_LAYOUT_GLYPH_CLASS_MARK);
483

B
Behdad Esfahbod 已提交
484
    unsigned int index = (this+coverage) (glyph_id);
B
Behdad Esfahbod 已提交
485
    if (HB_LIKELY (index == NOT_COVERED))
486 487
      return false;

B
Behdad Esfahbod 已提交
488
    const LigatureSet &lig_set = this+ligatureSet[index];
489
    return lig_set.apply (APPLY_ARG, first_is_mark);
490
  }
B
Behdad Esfahbod 已提交
491

B
Behdad Esfahbod 已提交
492
  inline bool sanitize (SANITIZE_ARG_DEF) {
493
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
494 495 496
    return SANITIZE_THIS2 (coverage, ligatureSet);
  }

B
Behdad Esfahbod 已提交
497
  private:
B
Behdad Esfahbod 已提交
498
  USHORT	format;			/* Format identifier--format = 1 */
B
Behdad Esfahbod 已提交
499 500
  OffsetTo<Coverage>
		coverage;		/* Offset to Coverage table--from
501
					 * beginning of Substitution table */
B
Behdad Esfahbod 已提交
502
  OffsetArrayOf<LigatureSet>
B
Behdad Esfahbod 已提交
503 504
		ligatureSet;		/* Array LigatureSet tables
					 * ordered by Coverage Index */
B
Behdad Esfahbod 已提交
505
};
506 507
ASSERT_SIZE (LigatureSubstFormat1, 6);

B
Behdad Esfahbod 已提交
508 509
struct LigatureSubst
{
510 511 512
  friend struct SubstLookupSubTable;

  private:
B
Behdad Esfahbod 已提交
513 514
  inline bool apply (APPLY_ARG_DEF) const
  {
515
    TRACE_APPLY ();
B
Behdad Esfahbod 已提交
516
    switch (u.format) {
517
    case 1: return u.format1->apply (APPLY_ARG);
518 519 520 521
    default:return false;
    }
  }

B
Behdad Esfahbod 已提交
522
  inline bool sanitize (SANITIZE_ARG_DEF) {
523
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
524 525 526 527 528 529 530
    if (!SANITIZE (u.format)) return false;
    switch (u.format) {
    case 1: return u.format1->sanitize (SANITIZE_ARG);
    default:return true;
    }
  }

531 532
  private:
  union {
B
Behdad Esfahbod 已提交
533 534
  USHORT		format;		/* Format identifier */
  LigatureSubstFormat1	format1[];
535 536
  } u;
};
B
Behdad Esfahbod 已提交
537
ASSERT_SIZE (LigatureSubst, 2);
538

B
Behdad Esfahbod 已提交
539

540

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

B
Behdad Esfahbod 已提交
543 544
struct ContextSubst : Context
{
545 546 547
  friend struct SubstLookupSubTable;

  private:
B
Behdad Esfahbod 已提交
548
  inline bool apply (APPLY_ARG_DEF) const
B
Behdad Esfahbod 已提交
549
  {
550
    TRACE_APPLY ();
B
Behdad Esfahbod 已提交
551 552
    return Context::apply (APPLY_ARG, substitute_lookup);
  }
553
};
B
Behdad Esfahbod 已提交
554
ASSERT_SIZE (ContextSubst, 2);
555

B
Behdad Esfahbod 已提交
556 557
struct ChainContextSubst : ChainContext
{
558 559 560
  friend struct SubstLookupSubTable;

  private:
B
Behdad Esfahbod 已提交
561
  inline bool apply (APPLY_ARG_DEF) const
B
Behdad Esfahbod 已提交
562
  {
563
    TRACE_APPLY ();
B
Behdad Esfahbod 已提交
564 565
    return ChainContext::apply (APPLY_ARG, substitute_lookup);
  }
B
Behdad Esfahbod 已提交
566
};
B
Behdad Esfahbod 已提交
567
ASSERT_SIZE (ChainContextSubst, 2);
B
Behdad Esfahbod 已提交
568

569

570
struct ExtensionSubst : Extension
B
Behdad Esfahbod 已提交
571
{
572 573 574
  friend struct SubstLookupSubTable;

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

578
  inline bool apply (APPLY_ARG_DEF) const;
B
Behdad Esfahbod 已提交
579 580

  inline bool sanitize (SANITIZE_ARG_DEF);
581
};
B
Behdad Esfahbod 已提交
582
ASSERT_SIZE (ExtensionSubst, 2);
583 584


B
Behdad Esfahbod 已提交
585 586
struct ReverseChainSingleSubstFormat1
{
587 588 589
  friend struct ReverseChainSingleSubst;

  private:
B
Behdad Esfahbod 已提交
590 591
  inline bool apply (APPLY_ARG_DEF) const
  {
592
    TRACE_APPLY ();
593 594 595 596 597 598 599
    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 已提交
600 601
    const OffsetArrayOf<Coverage> &lookahead = CONST_NEXT (OffsetArrayOf<Coverage>, backtrack);
    const ArrayOf<GlyphID> &substitute = CONST_NEXT (ArrayOf<GlyphID>, lookahead);
602

603
    if (match_backtrack (APPLY_ARG,
604
			 backtrack.len, (USHORT *) backtrack.array,
B
Behdad Esfahbod 已提交
605
			 match_coverage, DECONST_CHARP(this)) &&
606
        match_lookahead (APPLY_ARG,
607
			 lookahead.len, (USHORT *) lookahead.array,
B
Behdad Esfahbod 已提交
608
			 match_coverage, DECONST_CHARP(this),
609 610
			 1))
    {
611
      IN_CURGLYPH () = substitute[index];
612 613 614 615
      buffer->in_pos--; /* Reverse! */
      return true;
    }

616 617
    return false;
  }
B
Behdad Esfahbod 已提交
618

B
Behdad Esfahbod 已提交
619
  inline bool sanitize (SANITIZE_ARG_DEF) {
620
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
621 622
    if (!SANITIZE_THIS2 (coverage, backtrack))
      return false;
B
Behdad Esfahbod 已提交
623
    OffsetArrayOf<Coverage> &lookahead = NEXT (OffsetArrayOf<Coverage>, backtrack);
B
Behdad Esfahbod 已提交
624 625
    if (!SANITIZE_THIS (lookahead))
      return false;
B
Behdad Esfahbod 已提交
626
    ArrayOf<GlyphID> &substitute = NEXT (ArrayOf<GlyphID>, lookahead);
B
Behdad Esfahbod 已提交
627
    return SANITIZE (substitute);
B
Behdad Esfahbod 已提交
628 629
  }

B
Behdad Esfahbod 已提交
630
  private:
B
Behdad Esfahbod 已提交
631
  USHORT	format;			/* Format identifier--format = 1 */
632 633 634 635 636
  OffsetTo<Coverage>
		coverage;		/* Offset to Coverage table--from
					 * beginning of table */
  OffsetArrayOf<Coverage>
		backtrack;		/* Array of coverage tables
B
Behdad Esfahbod 已提交
637 638
					 * in backtracking sequence, in  glyph
					 * sequence order */
639 640 641
  OffsetArrayOf<Coverage>
		lookaheadX;		/* Array of coverage tables
					 * in lookahead sequence, in glyph
B
Behdad Esfahbod 已提交
642
					 * sequence order */
643 644 645
  ArrayOf<GlyphID>
		substituteX;		/* Array of substitute
					 * GlyphIDs--ordered by Coverage Index */
B
Behdad Esfahbod 已提交
646 647 648
};
ASSERT_SIZE (ReverseChainSingleSubstFormat1, 10);

B
Behdad Esfahbod 已提交
649 650
struct ReverseChainSingleSubst
{
651 652 653
  friend struct SubstLookupSubTable;

  private:
B
Behdad Esfahbod 已提交
654 655
  inline bool apply (APPLY_ARG_DEF) const
  {
656
    TRACE_APPLY ();
657
    switch (u.format) {
658
    case 1: return u.format1->apply (APPLY_ARG);
659 660 661 662
    default:return false;
    }
  }

B
Behdad Esfahbod 已提交
663
  inline bool sanitize (SANITIZE_ARG_DEF) {
664
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
665 666 667 668 669 670 671
    if (!SANITIZE (u.format)) return false;
    switch (u.format) {
    case 1: return u.format1->sanitize (SANITIZE_ARG);
    default:return true;
    }
  }

672 673 674 675 676 677 678 679 680 681
  private:
  union {
  USHORT				format;		/* Format identifier */
  ReverseChainSingleSubstFormat1	format1[];
  } u;
};
ASSERT_SIZE (ReverseChainSingleSubst, 2);



B
Behdad Esfahbod 已提交
682 683 684 685
/*
 * SubstLookup
 */

B
Behdad Esfahbod 已提交
686 687
struct SubstLookupSubTable
{
B
Behdad Esfahbod 已提交
688 689
  friend struct SubstLookup;

690 691 692 693 694 695 696 697
  enum {
    Single		= 1,
    Multiple		= 2,
    Alternate		= 3,
    Ligature		= 4,
    Context		= 5,
    ChainContext	= 6,
    Extension		= 7,
698
    ReverseChainSingle	= 8
699 700
  };

B
Behdad Esfahbod 已提交
701
  inline bool apply (APPLY_ARG_DEF, unsigned int lookup_type) const
B
Behdad Esfahbod 已提交
702
  {
703
    TRACE_APPLY ();
704
    switch (lookup_type) {
705 706 707 708 709 710 711 712
    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);
713 714
    default:return false;
    }
B
Behdad Esfahbod 已提交
715 716
  }

B
Behdad Esfahbod 已提交
717
  inline bool sanitize (SANITIZE_ARG_DEF) {
718
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
719 720 721 722 723 724 725 726 727 728 729 730 731 732
    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 已提交
733 734
  private:
  union {
735 736 737 738 739 740
  USHORT			format;
  SingleSubst			single[];
  MultipleSubst			multiple[];
  AlternateSubst		alternate[];
  LigatureSubst			ligature[];
  ContextSubst			context[];
B
Behdad Esfahbod 已提交
741
  ChainContextSubst		chainContext[];
742 743
  ExtensionSubst		extension[];
  ReverseChainSingleSubst	reverseChainContextSingle[];
B
Behdad Esfahbod 已提交
744 745
  } u;
};
B
Behdad Esfahbod 已提交
746
ASSERT_SIZE (SubstLookupSubTable, 2);
B
Behdad Esfahbod 已提交
747

748

B
Behdad Esfahbod 已提交
749 750 751
struct SubstLookup : Lookup
{
  inline const SubstLookupSubTable& get_subtable (unsigned int i) const
B
Behdad Esfahbod 已提交
752
  { return CONST_CAST (SubstLookupSubTable, Lookup::get_subtable (i), 0); }
B
Behdad Esfahbod 已提交
753 754

  /* Like get_type(), but looks through extension lookups.
755
   * Never returns Extension */
B
Behdad Esfahbod 已提交
756 757
  inline unsigned int get_effective_type (void) const
  {
B
Behdad Esfahbod 已提交
758 759
    unsigned int type = get_type ();

760
    if (HB_UNLIKELY (type == SubstLookupSubTable::Extension))
B
Behdad Esfahbod 已提交
761
    {
762
      unsigned int count = get_subtable_count ();
B
Behdad Esfahbod 已提交
763
      type = get_subtable(0).u.extension->get_type ();
764 765 766 767 768
      /* 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 已提交
769 770 771 772 773
    }

    return type;
  }

B
Behdad Esfahbod 已提交
774
  inline bool is_reverse (void) const
B
Behdad Esfahbod 已提交
775
  { return HB_UNLIKELY (get_effective_type () == SubstLookupSubTable::ReverseChainSingle); }
B
Behdad Esfahbod 已提交
776

777
  inline bool apply_once (hb_ot_layout_context_t *context,
778 779 780
			  hb_buffer_t    *buffer,
			  unsigned int    context_length,
			  unsigned int    nesting_level_left) const
B
Behdad Esfahbod 已提交
781
  {
B
Behdad Esfahbod 已提交
782
    unsigned int lookup_type = get_type ();
B
Behdad Esfahbod 已提交
783
    unsigned int lookup_flag = get_flag ();
784 785
    unsigned int property;

786
    if (!_hb_ot_layout_check_glyph_property (context->face, IN_CURINFO (), lookup_flag, &property))
787
      return false;
B
Behdad Esfahbod 已提交
788

B
Behdad Esfahbod 已提交
789 790
    unsigned int count = get_subtable_count ();
    for (unsigned int i = 0; i < count; i++)
B
Behdad Esfahbod 已提交
791
      if (get_subtable (i).apply (APPLY_ARG_INIT, lookup_type))
B
Behdad Esfahbod 已提交
792
	return true;
B
Behdad Esfahbod 已提交
793

B
Behdad Esfahbod 已提交
794 795
    return false;
  }
796

B
Behdad Esfahbod 已提交
797 798 799
  inline bool apply_string (hb_ot_layout_context_t *context,
			    hb_buffer_t *buffer,
			    hb_mask_t    mask) const
B
Behdad Esfahbod 已提交
800
  {
801 802
    bool ret = false;

B
Behdad Esfahbod 已提交
803 804 805
    if (HB_UNLIKELY (!buffer->in_length))
      return false;

B
Behdad Esfahbod 已提交
806 807
    if (HB_LIKELY (!is_reverse ()))
    {
808 809 810
	/* in/out forward substitution */
	_hb_buffer_clear_output (buffer);
	buffer->in_pos = 0;
B
Behdad Esfahbod 已提交
811 812
	while (buffer->in_pos < buffer->in_length)
	{
813
	  if ((~IN_MASK (buffer->in_pos) & mask) &&
814
	      apply_once (context, buffer, NO_CONTEXT, MAX_NESTING_LEVEL))
815 816
	    ret = true;
	  else
B
Behdad Esfahbod 已提交
817
	    _hb_buffer_next_glyph (buffer);
818 819 820 821

	}
	if (ret)
	  _hb_buffer_swap (buffer);
B
Behdad Esfahbod 已提交
822 823 824
    }
    else
    {
825 826
	/* in-place backward substitution */
	buffer->in_pos = buffer->in_length - 1;
B
Behdad Esfahbod 已提交
827 828
	do
	{
829
	  if ((~IN_MASK (buffer->in_pos) & mask) &&
830
	      apply_once (context, buffer, NO_CONTEXT, MAX_NESTING_LEVEL))
831 832 833 834
	    ret = true;
	  else
	    buffer->in_pos--;

B
Behdad Esfahbod 已提交
835 836
	}
	while ((int) buffer->in_pos >= 0);
837 838 839 840
    }

    return ret;
  }
B
Behdad Esfahbod 已提交
841 842

  inline bool sanitize (SANITIZE_ARG_DEF) {
843
    TRACE_SANITIZE ();
844
    if (!Lookup::sanitize (SANITIZE_ARG)) return false;
B
Behdad Esfahbod 已提交
845 846 847
    OffsetArrayOf<SubstLookupSubTable> &list = (OffsetArrayOf<SubstLookupSubTable> &) subTable;
    return SANITIZE_THIS (list);
  }
B
Behdad Esfahbod 已提交
848
};
B
Behdad Esfahbod 已提交
849
ASSERT_SIZE (SubstLookup, 6);
B
Behdad Esfahbod 已提交
850

B
Behdad Esfahbod 已提交
851 852
typedef OffsetListOf<SubstLookup> SubstLookupList;
ASSERT_SIZE (SubstLookupList, 2);
853

B
Minor  
Behdad Esfahbod 已提交
854 855 856 857
/*
 * GSUB
 */

B
Behdad Esfahbod 已提交
858 859
struct GSUB : GSUBGPOS
{
B
Behdad Esfahbod 已提交
860
  static const hb_tag_t Tag	= HB_OT_TAG_GSUB;
B
Minor  
Behdad Esfahbod 已提交
861

862
  static inline const GSUB& get_for_data (const char *data)
B
Behdad Esfahbod 已提交
863
  { return (const GSUB&) GSUBGPOS::get_for_data (data); }
B
Behdad Esfahbod 已提交
864

B
Behdad Esfahbod 已提交
865
  inline const SubstLookup& get_lookup (unsigned int i) const
B
Behdad Esfahbod 已提交
866
  { return (const SubstLookup&) GSUBGPOS::get_lookup (i); }
B
Behdad Esfahbod 已提交
867

868
  inline bool substitute_lookup (hb_ot_layout_context_t *context,
869 870 871
				 hb_buffer_t  *buffer,
			         unsigned int  lookup_index,
				 hb_mask_t     mask) const
872
  { return get_lookup (lookup_index).apply_string (context, buffer, mask); }
B
Behdad Esfahbod 已提交
873

B
Behdad Esfahbod 已提交
874

B
Behdad Esfahbod 已提交
875
  inline bool sanitize (SANITIZE_ARG_DEF) {
876
    TRACE_SANITIZE ();
877
    if (!GSUBGPOS::sanitize (SANITIZE_ARG)) return false;
B
Behdad Esfahbod 已提交
878
    OffsetTo<SubstLookupList> &list = CAST(OffsetTo<SubstLookupList>, lookupList, 0);
B
Behdad Esfahbod 已提交
879 880
    return SANITIZE_THIS (list);
  }
B
Minor  
Behdad Esfahbod 已提交
881
};
B
Behdad Esfahbod 已提交
882
ASSERT_SIZE (GSUB, 10);
883 884


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

887
inline bool ExtensionSubst::apply (APPLY_ARG_DEF) const
B
Behdad Esfahbod 已提交
888
{
889
  TRACE_APPLY ();
890 891
  unsigned int lookup_type = get_type ();

892
  if (HB_UNLIKELY (lookup_type == SubstLookupSubTable::Extension))
893 894
    return false;

B
Behdad Esfahbod 已提交
895 896 897 898 899
  return get_subtable ().apply (APPLY_ARG, lookup_type);
}

inline bool ExtensionSubst::sanitize (SANITIZE_ARG_DEF)
{
900
  TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
901
  return Extension::sanitize (SANITIZE_ARG) &&
B
Behdad Esfahbod 已提交
902
	 (&(Extension::get_subtable ()) == &Null(LookupSubTable) ||
903
	  get_type () == SubstLookupSubTable::Extension ||
B
Behdad Esfahbod 已提交
904
	  DECONST_CAST (SubstLookupSubTable, get_subtable (), 0).sanitize (SANITIZE_ARG));
905 906
}

B
Behdad Esfahbod 已提交
907 908
static inline bool substitute_lookup (APPLY_ARG_DEF, unsigned int lookup_index)
{
909
  const GSUB &gsub = *(context->face->ot_layout.gsub);
910
  const SubstLookup &l = gsub.get_lookup (lookup_index);
911

912 913 914 915 916 917 918
  if (HB_UNLIKELY (nesting_level_left == 0))
    return false;
  nesting_level_left--;

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

919
  return l.apply_once (context, buffer, context_length, nesting_level_left);
920 921 922
}


923
#endif /* HB_OT_LAYOUT_GSUB_PRIVATE_HH */