hb-ot-layout-gsub-private.hh 23.2 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 41
  inline bool apply (APPLY_ARG_DEF) const
  {
    hb_codepoint_t glyph_id = IN_CURGLYPH ();
B
Behdad Esfahbod 已提交
42
    unsigned int index = (this+coverage) (glyph_id);
B
Behdad Esfahbod 已提交
43
    if (HB_LIKELY (index == NOT_COVERED))
B
Behdad Esfahbod 已提交
44 45 46
      return false;

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

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

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

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

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

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

  private:
B
Behdad Esfahbod 已提交
76

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

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

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

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

94 95
    return true;
  }
B
Behdad Esfahbod 已提交
96

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

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

B
Behdad Esfahbod 已提交
113 114
struct SingleSubst
{
115 116
  friend struct SubstLookupSubTable;

117
  private:
B
Behdad Esfahbod 已提交
118

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

B
Behdad Esfahbod 已提交
128
  inline bool sanitize (SANITIZE_ARG_DEF) {
129
    SANITIZE_DEBUG ();
B
Behdad Esfahbod 已提交
130 131 132 133 134 135 136 137
    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;
    }
  }

138 139
  private:
  union {
B
Behdad Esfahbod 已提交
140 141 142
  USHORT		format;		/* Format identifier */
  SingleSubstFormat1	format1[];
  SingleSubstFormat2	format2[];
143
  } u;
B
Behdad Esfahbod 已提交
144
};
B
Behdad Esfahbod 已提交
145
ASSERT_SIZE (SingleSubst, 2);
146

B
Behdad Esfahbod 已提交
147

B
Behdad Esfahbod 已提交
148 149
struct Sequence
{
150 151 152
  friend struct MultipleSubstFormat1;

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

B
Behdad Esfahbod 已提交
158 159 160
    _hb_buffer_add_output_glyphs (buffer, 1,
				  substitute.len, (const uint16_t *) substitute.array,
				  0xFFFF, 0xFFFF);
B
Behdad Esfahbod 已提交
161

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

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

    return true;
  }

B
Behdad Esfahbod 已提交
176 177
  public:
  inline bool sanitize (SANITIZE_ARG_DEF) {
178
    SANITIZE_DEBUG ();
B
Behdad Esfahbod 已提交
179 180 181
    return SANITIZE (substitute);
  }

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

B
Behdad Esfahbod 已提交
188 189
struct MultipleSubstFormat1
{
190 191 192
  friend struct MultipleSubst;

  private:
B
Behdad Esfahbod 已提交
193

B
Behdad Esfahbod 已提交
194 195
  inline bool apply (APPLY_ARG_DEF) const
  {
196

B
Behdad Esfahbod 已提交
197
    unsigned int index = (this+coverage) (IN_CURGLYPH ());
B
Behdad Esfahbod 已提交
198
    if (HB_LIKELY (index == NOT_COVERED))
199 200
      return false;

201
    return (this+sequence[index]).apply (APPLY_ARG);
202
  }
B
Behdad Esfahbod 已提交
203

B
Behdad Esfahbod 已提交
204
  inline bool sanitize (SANITIZE_ARG_DEF) {
205
    SANITIZE_DEBUG ();
B
Behdad Esfahbod 已提交
206 207 208
    return SANITIZE_THIS2 (coverage, sequence);
  }

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

B
Behdad Esfahbod 已提交
220 221
struct MultipleSubst
{
222 223 224
  friend struct SubstLookupSubTable;

  private:
B
Behdad Esfahbod 已提交
225

B
Behdad Esfahbod 已提交
226 227
  inline bool apply (APPLY_ARG_DEF) const
  {
B
Behdad Esfahbod 已提交
228
    switch (u.format) {
229
    case 1: return u.format1->apply (APPLY_ARG);
230 231 232 233
    default:return false;
    }
  }

B
Behdad Esfahbod 已提交
234
  inline bool sanitize (SANITIZE_ARG_DEF) {
235
    SANITIZE_DEBUG ();
B
Behdad Esfahbod 已提交
236 237 238 239 240 241 242
    if (!SANITIZE (u.format)) return false;
    switch (u.format) {
    case 1: return u.format1->sanitize (SANITIZE_ARG);
    default:return true;
    }
  }

243 244
  private:
  union {
B
Behdad Esfahbod 已提交
245 246
  USHORT		format;		/* Format identifier */
  MultipleSubstFormat1	format1[];
247 248
  } u;
};
B
Behdad Esfahbod 已提交
249
ASSERT_SIZE (MultipleSubst, 2);
250

B
Behdad Esfahbod 已提交
251

B
Behdad Esfahbod 已提交
252
typedef ArrayOf<GlyphID> AlternateSet;	/* Array of alternate GlyphIDs--in
B
Behdad Esfahbod 已提交
253
					 * arbitrary order */
254
ASSERT_SIZE (AlternateSet, 2);
B
Behdad Esfahbod 已提交
255

B
Behdad Esfahbod 已提交
256 257
struct AlternateSubstFormat1
{
258 259 260
  friend struct AlternateSubst;

  private:
B
Behdad Esfahbod 已提交
261

B
Behdad Esfahbod 已提交
262 263
  inline bool apply (APPLY_ARG_DEF) const
  {
264
    hb_codepoint_t glyph_id = IN_CURGLYPH ();
265

B
Behdad Esfahbod 已提交
266
    unsigned int index = (this+coverage) (glyph_id);
B
Behdad Esfahbod 已提交
267
    if (HB_LIKELY (index == NOT_COVERED))
268 269
      return false;

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

B
Behdad Esfahbod 已提交
272
    if (HB_UNLIKELY (!alt_set.len))
273 274 275 276 277
      return false;

    unsigned int alt_index = 0;

    /* XXX callback to user to choose alternate
278 279
    if (context->face->altfunc)
      alt_index = (context->face->altfunc)(context->layout, buffer,
B
Behdad Esfahbod 已提交
280 281
				    buffer->out_pos, glyph_id,
				    alt_set.len, alt_set.array);
282 283
				   */

B
Behdad Esfahbod 已提交
284
    if (HB_UNLIKELY (alt_index >= alt_set.len))
285 286 287 288
      return false;

    glyph_id = alt_set[alt_index];

B
Behdad Esfahbod 已提交
289
    _hb_buffer_replace_glyph (buffer, glyph_id);
290

291
    /* We inherit the old glyph class to the substituted glyph */
292 293
    if (_hb_ot_layout_has_new_glyph_classes (context->face))
      _hb_ot_layout_set_glyph_property (context->face, glyph_id, property);
294 295 296

    return true;
  }
B
Behdad Esfahbod 已提交
297

B
Behdad Esfahbod 已提交
298
  inline bool sanitize (SANITIZE_ARG_DEF) {
299
    SANITIZE_DEBUG ();
B
Behdad Esfahbod 已提交
300 301 302
    return SANITIZE_THIS2 (coverage, alternateSet);
  }

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

B
Behdad Esfahbod 已提交
314 315
struct AlternateSubst
{
316 317 318
  friend struct SubstLookupSubTable;

  private:
B
Behdad Esfahbod 已提交
319

B
Behdad Esfahbod 已提交
320 321
  inline bool apply (APPLY_ARG_DEF) const
  {
B
Behdad Esfahbod 已提交
322
    switch (u.format) {
323
    case 1: return u.format1->apply (APPLY_ARG);
324 325 326 327
    default:return false;
    }
  }

B
Behdad Esfahbod 已提交
328
  inline bool sanitize (SANITIZE_ARG_DEF) {
329
    SANITIZE_DEBUG ();
B
Behdad Esfahbod 已提交
330 331 332 333 334 335 336
    if (!SANITIZE (u.format)) return false;
    switch (u.format) {
    case 1: return u.format1->sanitize (SANITIZE_ARG);
    default:return true;
    }
  }

337 338
  private:
  union {
B
Behdad Esfahbod 已提交
339 340
  USHORT		format;		/* Format identifier */
  AlternateSubstFormat1	format1[];
341 342
  } u;
};
B
Behdad Esfahbod 已提交
343
ASSERT_SIZE (AlternateSubst, 2);
344

345

B
Behdad Esfahbod 已提交
346 347
struct Ligature
{
348 349 350
  friend struct LigatureSet;

  private:
B
Behdad Esfahbod 已提交
351 352
  inline bool apply (APPLY_ARG_DEF, bool is_mark) const
  {
353
    unsigned int i, j;
B
Behdad Esfahbod 已提交
354
    unsigned int count = component.len;
355 356 357
    unsigned int end = MIN (buffer->in_length, buffer->in_pos + context_length);
    if (HB_UNLIKELY (buffer->in_pos + count > end))
      return false;
358

B
Behdad Esfahbod 已提交
359 360
    for (i = 1, j = buffer->in_pos + 1; i < count; i++, j++)
    {
361
      while (_hb_ot_layout_skip_mark (context->face, IN_INFO (j), lookup_flag, &property))
B
Behdad Esfahbod 已提交
362
      {
363
	if (HB_UNLIKELY (j + count - i == end))
364 365 366 367
	  return false;
	j++;
      }

368
      if (!(property & HB_OT_LAYOUT_GLYPH_CLASS_MARK))
369 370
	is_mark = FALSE;

371
      if (HB_LIKELY (IN_GLYPH (j) != component[i]))
372 373
        return false;
    }
374
    /* This is just a guess ... */
375 376
    if (_hb_ot_layout_has_new_glyph_classes (context->face))
      _hb_ot_layout_set_glyph_class (context->face, ligGlyph,
377 378
				     is_mark ? HB_OT_LAYOUT_GLYPH_CLASS_MARK
					     : HB_OT_LAYOUT_GLYPH_CLASS_LIGATURE);
379 380 381 382

    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 已提交
383 384 385 386
      _hb_buffer_add_output_glyphs (buffer, i,
				    1, (const uint16_t *) &ligGlyph,
				    0xFFFF,
				    IN_LIGID (buffer->in_pos) ?
B
Behdad Esfahbod 已提交
387
				    0xFFFF : _hb_buffer_allocate_lig_id (buffer));
388 389
    else
    {
B
Behdad Esfahbod 已提交
390
      unsigned int lig_id = _hb_buffer_allocate_lig_id (buffer);
391 392 393 394 395 396 397 398 399
      _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 已提交
400
      for ( i = 1; i < count; i++ )
401
      {
402
	while (_hb_ot_layout_skip_mark (context->face, IN_CURINFO (), lookup_flag, NULL))
403
	  _hb_buffer_add_output_glyph (buffer, IN_CURGLYPH (), i - 1, lig_id);
404 405 406 407

	(buffer->in_pos)++;
      }

B
Behdad Esfahbod 已提交
408
      /* TODO We should possibly reassign lig_id and component for any
409 410 411 412 413 414
       * components of a previous ligature that s now being removed as part of
       * this ligature. */
    }

    return true;
  }
415

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

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

B
Behdad Esfahbod 已提交
431 432
struct LigatureSet
{
433 434 435
  friend struct LigatureSubstFormat1;

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

    return false;
  }
B
Behdad Esfahbod 已提交
448

B
Behdad Esfahbod 已提交
449 450
  public:
  inline bool sanitize (SANITIZE_ARG_DEF) {
451
    SANITIZE_DEBUG ();
B
Behdad Esfahbod 已提交
452 453 454
    return SANITIZE_THIS (ligature);
  }

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

B
Behdad Esfahbod 已提交
462 463
struct LigatureSubstFormat1
{
464 465 466
  friend struct LigatureSubst;

  private:
B
Behdad Esfahbod 已提交
467 468
  inline bool apply (APPLY_ARG_DEF) const
  {
469 470
    hb_codepoint_t glyph_id = IN_CURGLYPH ();

471
    bool first_is_mark = !!(property & HB_OT_LAYOUT_GLYPH_CLASS_MARK);
472

B
Behdad Esfahbod 已提交
473
    unsigned int index = (this+coverage) (glyph_id);
B
Behdad Esfahbod 已提交
474
    if (HB_LIKELY (index == NOT_COVERED))
475 476
      return false;

B
Behdad Esfahbod 已提交
477
    const LigatureSet &lig_set = this+ligatureSet[index];
478
    return lig_set.apply (APPLY_ARG, first_is_mark);
479
  }
B
Behdad Esfahbod 已提交
480

B
Behdad Esfahbod 已提交
481
  inline bool sanitize (SANITIZE_ARG_DEF) {
482
    SANITIZE_DEBUG ();
B
Behdad Esfahbod 已提交
483 484 485
    return SANITIZE_THIS2 (coverage, ligatureSet);
  }

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

B
Behdad Esfahbod 已提交
497 498
struct LigatureSubst
{
499 500 501
  friend struct SubstLookupSubTable;

  private:
B
Behdad Esfahbod 已提交
502 503
  inline bool apply (APPLY_ARG_DEF) const
  {
B
Behdad Esfahbod 已提交
504
    switch (u.format) {
505
    case 1: return u.format1->apply (APPLY_ARG);
506 507 508 509
    default:return false;
    }
  }

B
Behdad Esfahbod 已提交
510
  inline bool sanitize (SANITIZE_ARG_DEF) {
511
    SANITIZE_DEBUG ();
B
Behdad Esfahbod 已提交
512 513 514 515 516 517 518
    if (!SANITIZE (u.format)) return false;
    switch (u.format) {
    case 1: return u.format1->sanitize (SANITIZE_ARG);
    default:return true;
    }
  }

519 520
  private:
  union {
B
Behdad Esfahbod 已提交
521 522
  USHORT		format;		/* Format identifier */
  LigatureSubstFormat1	format1[];
523 524
  } u;
};
B
Behdad Esfahbod 已提交
525
ASSERT_SIZE (LigatureSubst, 2);
526

B
Behdad Esfahbod 已提交
527

528

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

B
Behdad Esfahbod 已提交
531 532
struct ContextSubst : Context
{
533 534 535
  friend struct SubstLookupSubTable;

  private:
B
Behdad Esfahbod 已提交
536
  inline bool apply (APPLY_ARG_DEF) const
B
Behdad Esfahbod 已提交
537
  { return Context::apply (APPLY_ARG, substitute_lookup); }
538
};
B
Behdad Esfahbod 已提交
539
ASSERT_SIZE (ContextSubst, 2);
540

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

  private:
B
Behdad Esfahbod 已提交
546
  inline bool apply (APPLY_ARG_DEF) const
B
Behdad Esfahbod 已提交
547
  { return ChainContext::apply (APPLY_ARG, substitute_lookup); }
B
Behdad Esfahbod 已提交
548
};
B
Behdad Esfahbod 已提交
549
ASSERT_SIZE (ChainContextSubst, 2);
B
Behdad Esfahbod 已提交
550

551

552
struct ExtensionSubst : Extension
B
Behdad Esfahbod 已提交
553
{
554 555 556
  friend struct SubstLookupSubTable;

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

560
  inline bool apply (APPLY_ARG_DEF) const;
B
Behdad Esfahbod 已提交
561 562

  inline bool sanitize (SANITIZE_ARG_DEF);
563
};
B
Behdad Esfahbod 已提交
564
ASSERT_SIZE (ExtensionSubst, 2);
565 566


B
Behdad Esfahbod 已提交
567 568
struct ReverseChainSingleSubstFormat1
{
569 570 571
  friend struct ReverseChainSingleSubst;

  private:
B
Behdad Esfahbod 已提交
572 573
  inline bool apply (APPLY_ARG_DEF) const
  {
574 575 576 577 578 579 580
    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 已提交
581 582
    const OffsetArrayOf<Coverage> &lookahead = CONST_NEXT (OffsetArrayOf<Coverage>, backtrack);
    const ArrayOf<GlyphID> &substitute = CONST_NEXT (ArrayOf<GlyphID>, lookahead);
583

584
    if (match_backtrack (APPLY_ARG,
585
			 backtrack.len, (USHORT *) backtrack.array,
B
Behdad Esfahbod 已提交
586
			 match_coverage, DECONST_CHARP(this)) &&
587
        match_lookahead (APPLY_ARG,
588
			 lookahead.len, (USHORT *) lookahead.array,
B
Behdad Esfahbod 已提交
589
			 match_coverage, DECONST_CHARP(this),
590 591
			 1))
    {
592
      IN_CURGLYPH () = substitute[index];
593 594 595 596
      buffer->in_pos--; /* Reverse! */
      return true;
    }

597 598
    return false;
  }
B
Behdad Esfahbod 已提交
599

B
Behdad Esfahbod 已提交
600
  inline bool sanitize (SANITIZE_ARG_DEF) {
601
    SANITIZE_DEBUG ();
B
Behdad Esfahbod 已提交
602 603
    if (!SANITIZE_THIS2 (coverage, backtrack))
      return false;
B
Behdad Esfahbod 已提交
604
    OffsetArrayOf<Coverage> &lookahead = NEXT (OffsetArrayOf<Coverage>, backtrack);
B
Behdad Esfahbod 已提交
605 606
    if (!SANITIZE_THIS (lookahead))
      return false;
B
Behdad Esfahbod 已提交
607
    ArrayOf<GlyphID> &substitute = NEXT (ArrayOf<GlyphID>, lookahead);
B
Behdad Esfahbod 已提交
608
    return SANITIZE (substitute);
B
Behdad Esfahbod 已提交
609 610
  }

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

B
Behdad Esfahbod 已提交
630 631
struct ReverseChainSingleSubst
{
632 633 634
  friend struct SubstLookupSubTable;

  private:
B
Behdad Esfahbod 已提交
635 636
  inline bool apply (APPLY_ARG_DEF) const
  {
637
    switch (u.format) {
638
    case 1: return u.format1->apply (APPLY_ARG);
639 640 641 642
    default:return false;
    }
  }

B
Behdad Esfahbod 已提交
643
  inline bool sanitize (SANITIZE_ARG_DEF) {
644
    SANITIZE_DEBUG ();
B
Behdad Esfahbod 已提交
645 646 647 648 649 650 651
    if (!SANITIZE (u.format)) return false;
    switch (u.format) {
    case 1: return u.format1->sanitize (SANITIZE_ARG);
    default:return true;
    }
  }

652 653 654 655 656 657 658 659 660 661
  private:
  union {
  USHORT				format;		/* Format identifier */
  ReverseChainSingleSubstFormat1	format1[];
  } u;
};
ASSERT_SIZE (ReverseChainSingleSubst, 2);



B
Behdad Esfahbod 已提交
662 663 664 665
/*
 * SubstLookup
 */

B
Behdad Esfahbod 已提交
666 667
struct SubstLookupSubTable
{
B
Behdad Esfahbod 已提交
668 669
  friend struct SubstLookup;

670 671 672 673 674 675 676 677 678 679 680
  enum {
    Single		= 1,
    Multiple		= 2,
    Alternate		= 3,
    Ligature		= 4,
    Context		= 5,
    ChainContext	= 6,
    Extension		= 7,
    ReverseChainSingle	= 8,
  };

B
Behdad Esfahbod 已提交
681
  inline bool apply (APPLY_ARG_DEF, unsigned int lookup_type) const
B
Behdad Esfahbod 已提交
682
  {
683
    switch (lookup_type) {
684 685 686 687 688 689 690 691
    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);
692 693
    default:return false;
    }
B
Behdad Esfahbod 已提交
694 695
  }

B
Behdad Esfahbod 已提交
696
  inline bool sanitize (SANITIZE_ARG_DEF) {
697
    SANITIZE_DEBUG ();
B
Behdad Esfahbod 已提交
698 699 700 701 702 703 704 705 706 707 708 709 710 711
    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 已提交
712 713
  private:
  union {
714 715 716 717 718 719
  USHORT			format;
  SingleSubst			single[];
  MultipleSubst			multiple[];
  AlternateSubst		alternate[];
  LigatureSubst			ligature[];
  ContextSubst			context[];
B
Behdad Esfahbod 已提交
720
  ChainContextSubst		chainContext[];
721 722
  ExtensionSubst		extension[];
  ReverseChainSingleSubst	reverseChainContextSingle[];
B
Behdad Esfahbod 已提交
723 724
  } u;
};
B
Behdad Esfahbod 已提交
725
ASSERT_SIZE (SubstLookupSubTable, 2);
B
Behdad Esfahbod 已提交
726

727

B
Behdad Esfahbod 已提交
728 729 730
struct SubstLookup : Lookup
{
  inline const SubstLookupSubTable& get_subtable (unsigned int i) const
B
Behdad Esfahbod 已提交
731
  { return CONST_CAST (SubstLookupSubTable, Lookup::get_subtable (i), 0); }
B
Behdad Esfahbod 已提交
732 733

  /* Like get_type(), but looks through extension lookups.
734
   * Never returns Extension */
B
Behdad Esfahbod 已提交
735 736
  inline unsigned int get_effective_type (void) const
  {
B
Behdad Esfahbod 已提交
737 738
    unsigned int type = get_type ();

739
    if (HB_UNLIKELY (type == SubstLookupSubTable::Extension))
B
Behdad Esfahbod 已提交
740
    {
741
      unsigned int count = get_subtable_count ();
B
Behdad Esfahbod 已提交
742
      type = get_subtable(0).u.extension->get_type ();
743 744 745 746 747
      /* 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 已提交
748 749 750 751 752
    }

    return type;
  }

B
Behdad Esfahbod 已提交
753
  inline bool is_reverse (void) const
B
Behdad Esfahbod 已提交
754
  { return HB_UNLIKELY (get_effective_type () == SubstLookupSubTable::ReverseChainSingle); }
B
Behdad Esfahbod 已提交
755

756
  inline bool apply_once (hb_ot_layout_context_t *context,
757 758 759
			  hb_buffer_t    *buffer,
			  unsigned int    context_length,
			  unsigned int    nesting_level_left) const
B
Behdad Esfahbod 已提交
760
  {
B
Behdad Esfahbod 已提交
761
    unsigned int lookup_type = get_type ();
B
Behdad Esfahbod 已提交
762
    unsigned int lookup_flag = get_flag ();
763 764
    unsigned int property;

765
    if (!_hb_ot_layout_check_glyph_property (context->face, IN_CURINFO (), lookup_flag, &property))
766
      return false;
B
Behdad Esfahbod 已提交
767

B
Behdad Esfahbod 已提交
768 769
    unsigned int count = get_subtable_count ();
    for (unsigned int i = 0; i < count; i++)
770
      if (get_subtable (i).apply (APPLY_ARG, lookup_type))
B
Behdad Esfahbod 已提交
771
	return true;
B
Behdad Esfahbod 已提交
772

B
Behdad Esfahbod 已提交
773 774
    return false;
  }
775

B
Behdad Esfahbod 已提交
776 777 778
  inline bool apply_string (hb_ot_layout_context_t *context,
			    hb_buffer_t *buffer,
			    hb_mask_t    mask) const
B
Behdad Esfahbod 已提交
779
  {
780 781
    bool ret = false;

B
Behdad Esfahbod 已提交
782 783 784
    if (HB_UNLIKELY (!buffer->in_length))
      return false;

B
Behdad Esfahbod 已提交
785 786
    if (HB_LIKELY (!is_reverse ()))
    {
787 788 789
	/* in/out forward substitution */
	_hb_buffer_clear_output (buffer);
	buffer->in_pos = 0;
B
Behdad Esfahbod 已提交
790 791
	while (buffer->in_pos < buffer->in_length)
	{
792
	  if ((~IN_MASK (buffer->in_pos) & mask) &&
793
	      apply_once (context, buffer, NO_CONTEXT, MAX_NESTING_LEVEL))
794 795
	    ret = true;
	  else
B
Behdad Esfahbod 已提交
796
	    _hb_buffer_next_glyph (buffer);
797 798 799 800 801

	}
	if (ret)
	  _hb_buffer_swap (buffer);

B
Behdad Esfahbod 已提交
802 803 804
    }
    else
    {
805 806 807

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

B
Behdad Esfahbod 已提交
816 817
	}
	while ((int) buffer->in_pos >= 0);
818 819 820 821
    }

    return ret;
  }
B
Behdad Esfahbod 已提交
822 823

  inline bool sanitize (SANITIZE_ARG_DEF) {
824
    SANITIZE_DEBUG ();
825
    if (!Lookup::sanitize (SANITIZE_ARG)) return false;
B
Behdad Esfahbod 已提交
826 827 828
    OffsetArrayOf<SubstLookupSubTable> &list = (OffsetArrayOf<SubstLookupSubTable> &) subTable;
    return SANITIZE_THIS (list);
  }
B
Behdad Esfahbod 已提交
829
};
B
Behdad Esfahbod 已提交
830
ASSERT_SIZE (SubstLookup, 6);
B
Behdad Esfahbod 已提交
831

B
Behdad Esfahbod 已提交
832 833
typedef OffsetListOf<SubstLookup> SubstLookupList;
ASSERT_SIZE (SubstLookupList, 2);
834

B
Minor  
Behdad Esfahbod 已提交
835 836 837 838
/*
 * GSUB
 */

B
Behdad Esfahbod 已提交
839 840
struct GSUB : GSUBGPOS
{
B
Behdad Esfahbod 已提交
841
  static const hb_tag_t Tag	= HB_OT_TAG_GSUB;
B
Minor  
Behdad Esfahbod 已提交
842

843
  static inline const GSUB& get_for_data (const char *data)
B
Behdad Esfahbod 已提交
844
  { return (const GSUB&) GSUBGPOS::get_for_data (data); }
B
Behdad Esfahbod 已提交
845

B
Behdad Esfahbod 已提交
846
  inline const SubstLookup& get_lookup (unsigned int i) const
B
Behdad Esfahbod 已提交
847
  { return (const SubstLookup&) GSUBGPOS::get_lookup (i); }
B
Behdad Esfahbod 已提交
848

849
  inline bool substitute_lookup (hb_ot_layout_context_t *context,
850 851 852
				 hb_buffer_t  *buffer,
			         unsigned int  lookup_index,
				 hb_mask_t     mask) const
853
  { return get_lookup (lookup_index).apply_string (context, buffer, mask); }
B
Behdad Esfahbod 已提交
854

B
Behdad Esfahbod 已提交
855

B
Behdad Esfahbod 已提交
856
  inline bool sanitize (SANITIZE_ARG_DEF) {
857
    SANITIZE_DEBUG ();
858
    if (!GSUBGPOS::sanitize (SANITIZE_ARG)) return false;
B
Behdad Esfahbod 已提交
859
    OffsetTo<SubstLookupList> &list = CAST(OffsetTo<SubstLookupList>, lookupList, 0);
B
Behdad Esfahbod 已提交
860 861
    return SANITIZE_THIS (list);
  }
B
Minor  
Behdad Esfahbod 已提交
862
};
B
Behdad Esfahbod 已提交
863
ASSERT_SIZE (GSUB, 10);
864 865


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

868
inline bool ExtensionSubst::apply (APPLY_ARG_DEF) const
B
Behdad Esfahbod 已提交
869
{
870 871
  unsigned int lookup_type = get_type ();

872
  if (HB_UNLIKELY (lookup_type == SubstLookupSubTable::Extension))
873 874
    return false;

B
Behdad Esfahbod 已提交
875 876 877 878 879
  return get_subtable ().apply (APPLY_ARG, lookup_type);
}

inline bool ExtensionSubst::sanitize (SANITIZE_ARG_DEF)
{
880
  SANITIZE_DEBUG ();
B
Behdad Esfahbod 已提交
881
  return Extension::sanitize (SANITIZE_ARG) &&
B
Behdad Esfahbod 已提交
882
	 (&(Extension::get_subtable ()) == &Null(LookupSubTable) ||
883
	  get_type () == SubstLookupSubTable::Extension ||
B
Behdad Esfahbod 已提交
884
	  DECONST_CAST (SubstLookupSubTable, get_subtable (), 0).sanitize (SANITIZE_ARG));
885 886
}

B
Behdad Esfahbod 已提交
887 888
static inline bool substitute_lookup (APPLY_ARG_DEF, unsigned int lookup_index)
{
889
  const GSUB &gsub = *(context->face->ot_layout.gsub);
890
  const SubstLookup &l = gsub.get_lookup (lookup_index);
891

892 893 894 895 896 897 898
  if (HB_UNLIKELY (nesting_level_left == 0))
    return false;
  nesting_level_left--;

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

899
  return l.apply_once (context, buffer, context_length, nesting_level_left);
900 901 902
}


903
#endif /* HB_OT_LAYOUT_GSUB_PRIVATE_HH */