hb-ot-layout-gsub-private.h 19.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 27 28 29
 *
 *  This is part of HarfBuzz, an OpenType Layout engine library.
 *
 * Permission is hereby granted, without written agreement and without
 * license or royalty fees, to use, copy, modify, and distribute this
 * software and its documentation for any purpose, provided that the
 * above copyright notice and the following two paragraphs appear in
 * all copies of this software.
 *
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 *
 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 *
 * Red Hat Author(s): Behdad Esfahbod
 */

#ifndef HB_OT_LAYOUT_GSUB_PRIVATE_H
#define HB_OT_LAYOUT_GSUB_PRIVATE_H

30
#include "hb-ot-layout-gsubgpos-private.h"
B
Behdad Esfahbod 已提交
31

B
Behdad Esfahbod 已提交
32
/* XXX */
33
#include "hb-buffer-private.h"
B
Behdad Esfahbod 已提交
34
HB_INTERNAL HB_Error
35 36 37
_hb_buffer_add_output_glyph_ids (hb_buffer_t *buffer,
			      unsigned int num_in,
			      unsigned int num_out,
B
Behdad Esfahbod 已提交
38
			      const GlyphID *glyph_data,
39 40
			      unsigned short component,
			      unsigned short ligID);
B
Behdad Esfahbod 已提交
41

42

B
Behdad Esfahbod 已提交
43 44
struct SingleSubstFormat1
{
B
Behdad Esfahbod 已提交
45 46 47
  friend struct SingleSubst;

  private:
B
Behdad Esfahbod 已提交
48 49 50
  inline bool apply (APPLY_ARG_DEF) const
  {
    hb_codepoint_t glyph_id = IN_CURGLYPH ();
B
Behdad Esfahbod 已提交
51
    unsigned int index = (this+coverage) (glyph_id);
B
Behdad Esfahbod 已提交
52
    if (HB_LIKELY (index == NOT_COVERED))
B
Behdad Esfahbod 已提交
53 54 55
      return false;

    glyph_id += deltaGlyphID;
B
Behdad Esfahbod 已提交
56 57 58 59 60 61 62
    _hb_buffer_replace_glyph (buffer, glyph_id);

    if ( _hb_ot_layout_has_new_glyph_classes (layout) )
    {
      /* we inherit the old glyph class to the substituted glyph */
      _hb_ot_layout_set_glyph_property (layout, glyph_id, property);
    }
B
Behdad Esfahbod 已提交
63 64

    return true;
65
  }
B
Behdad Esfahbod 已提交
66

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

B
Behdad Esfahbod 已提交
77 78
struct SingleSubstFormat2
{
79 80 81
  friend struct SingleSubst;

  private:
B
Behdad Esfahbod 已提交
82 83 84
  inline bool apply (APPLY_ARG_DEF) const
  {
    hb_codepoint_t glyph_id = IN_CURGLYPH ();
B
Behdad Esfahbod 已提交
85
    unsigned int index = (this+coverage) (glyph_id);
B
Behdad Esfahbod 已提交
86
    if (HB_LIKELY (index == NOT_COVERED))
87
      return false;
88

B
Behdad Esfahbod 已提交
89
    if (HB_UNLIKELY (index >= substitute.len))
90 91 92
      return false;

    glyph_id = substitute[index];
B
Behdad Esfahbod 已提交
93 94 95 96 97 98 99 100
    _hb_buffer_replace_glyph (buffer, glyph_id);

    if ( _hb_ot_layout_has_new_glyph_classes (layout) )
    {
      /* we inherit the old glyph class to the substituted glyph */
      _hb_ot_layout_set_glyph_property (layout, glyph_id, property);
    }

101 102
    return true;
  }
B
Behdad Esfahbod 已提交
103 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 121
  inline bool apply (APPLY_ARG_DEF) const
  {
B
Behdad Esfahbod 已提交
122
    switch (u.format) {
B
Behdad Esfahbod 已提交
123 124
    case 1: return u.format1->apply (APPLY_ARG);
    case 2: return u.format2->apply (APPLY_ARG);
B
Behdad Esfahbod 已提交
125 126 127 128
    default:return false;
    }
  }

129 130
  private:
  union {
B
Behdad Esfahbod 已提交
131 132 133
  USHORT		format;		/* Format identifier */
  SingleSubstFormat1	format1[];
  SingleSubstFormat2	format2[];
134
  } u;
B
Behdad Esfahbod 已提交
135
};
B
Behdad Esfahbod 已提交
136
ASSERT_SIZE (SingleSubst, 2);
137

B
Behdad Esfahbod 已提交
138

B
Behdad Esfahbod 已提交
139 140
struct Sequence
{
141 142 143
  friend struct MultipleSubstFormat1;

  private:
B
Behdad Esfahbod 已提交
144 145
  inline bool apply (APPLY_ARG_DEF) const
  {
B
Behdad Esfahbod 已提交
146
    if (HB_UNLIKELY (!substitute.len))
B
Behdad Esfahbod 已提交
147 148 149
      return false;

    _hb_buffer_add_output_glyph_ids (buffer, 1,
B
Behdad Esfahbod 已提交
150
				     substitute.len, substitute.array,
B
Behdad Esfahbod 已提交
151 152 153 154 155 156 157 158
				     0xFFFF, 0xFFFF);

    if ( _hb_ot_layout_has_new_glyph_classes (layout) )
    {
      /* this is a guess only ... */
      if ( property == HB_OT_LAYOUT_GLYPH_CLASS_LIGATURE )
        property = HB_OT_LAYOUT_GLYPH_CLASS_BASE_GLYPH;

159 160 161
      unsigned int count = substitute.len;
      for (unsigned int n = 0; n < count; n++)
	_hb_ot_layout_set_glyph_property (layout, substitute[n], property);
B
Behdad Esfahbod 已提交
162 163 164 165 166
    }

    return true;
  }

B
Behdad Esfahbod 已提交
167
  private:
B
Behdad Esfahbod 已提交
168 169
  ArrayOf<GlyphID>
		substitute;		/* String of GlyphIDs to substitute */
B
Behdad Esfahbod 已提交
170
};
171
ASSERT_SIZE (Sequence, 2);
B
Behdad Esfahbod 已提交
172

B
Behdad Esfahbod 已提交
173 174
struct MultipleSubstFormat1
{
175 176 177
  friend struct MultipleSubst;

  private:
B
Behdad Esfahbod 已提交
178 179
  inline bool apply (APPLY_ARG_DEF) const
  {
180

B
Behdad Esfahbod 已提交
181
    unsigned int index = (this+coverage) (IN_CURGLYPH ());
B
Behdad Esfahbod 已提交
182
    if (HB_LIKELY (index == NOT_COVERED))
183 184
      return false;

185
    return (this+sequence[index]).apply (APPLY_ARG);
186
  }
B
Behdad Esfahbod 已提交
187 188

  private:
B
Behdad Esfahbod 已提交
189
  USHORT	format;			/* Format identifier--format = 1 */
B
Behdad Esfahbod 已提交
190 191
  OffsetTo<Coverage>
		coverage;		/* Offset to Coverage table--from
B
Behdad Esfahbod 已提交
192
					 * beginning of Substitution table */
B
Behdad Esfahbod 已提交
193 194 195
  OffsetArrayOf<Sequence>
		sequence;		/* Array of Sequence tables
					 * ordered by Coverage Index */
B
Behdad Esfahbod 已提交
196
};
197 198
ASSERT_SIZE (MultipleSubstFormat1, 6);

B
Behdad Esfahbod 已提交
199 200
struct MultipleSubst
{
201 202 203
  friend struct SubstLookupSubTable;

  private:
B
Behdad Esfahbod 已提交
204 205
  inline bool apply (APPLY_ARG_DEF) const
  {
B
Behdad Esfahbod 已提交
206
    switch (u.format) {
207
    case 1: return u.format1->apply (APPLY_ARG);
208 209 210 211 212 213
    default:return false;
    }
  }

  private:
  union {
B
Behdad Esfahbod 已提交
214 215
  USHORT		format;		/* Format identifier */
  MultipleSubstFormat1	format1[];
216 217
  } u;
};
B
Behdad Esfahbod 已提交
218
ASSERT_SIZE (MultipleSubst, 2);
219

B
Behdad Esfahbod 已提交
220

B
Behdad Esfahbod 已提交
221
typedef ArrayOf<GlyphID> AlternateSet;	/* Array of alternate GlyphIDs--in
B
Behdad Esfahbod 已提交
222
					 * arbitrary order */
223
ASSERT_SIZE (AlternateSet, 2);
B
Behdad Esfahbod 已提交
224

B
Behdad Esfahbod 已提交
225 226
struct AlternateSubstFormat1
{
227 228 229
  friend struct AlternateSubst;

  private:
B
Behdad Esfahbod 已提交
230 231
  inline bool apply (APPLY_ARG_DEF) const
  {
232
    hb_codepoint_t glyph_id = IN_CURGLYPH ();
233

B
Behdad Esfahbod 已提交
234
    unsigned int index = (this+coverage) (glyph_id);
B
Behdad Esfahbod 已提交
235
    if (HB_LIKELY (index == NOT_COVERED))
236 237
      return false;

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

B
Behdad Esfahbod 已提交
240
    if (HB_UNLIKELY (!alt_set.len))
241 242 243 244 245
      return false;

    unsigned int alt_index = 0;

    /* XXX callback to user to choose alternate
B
Behdad Esfahbod 已提交
246 247 248 249
    if (layout->altfunc)
      alt_index = (layout->altfunc)(layout, buffer,
				    buffer->out_pos, glyph_id,
				    alt_set.len, alt_set.array);
250 251
				   */

B
Behdad Esfahbod 已提交
252
    if (HB_UNLIKELY (alt_index >= alt_set.len))
253 254 255 256
      return false;

    glyph_id = alt_set[alt_index];

B
Behdad Esfahbod 已提交
257
    _hb_buffer_replace_glyph (buffer, glyph_id);
258 259 260 261 262 263 264 265 266

    if ( _hb_ot_layout_has_new_glyph_classes (layout) )
    {
      /* we inherit the old glyph class to the substituted glyph */
      _hb_ot_layout_set_glyph_property (layout, glyph_id, property);
    }

    return true;
  }
B
Behdad Esfahbod 已提交
267 268

  private:
B
Behdad Esfahbod 已提交
269
  USHORT	format;			/* Format identifier--format = 1 */
B
Behdad Esfahbod 已提交
270 271
  OffsetTo<Coverage>
		coverage;		/* Offset to Coverage table--from
B
Behdad Esfahbod 已提交
272
					 * beginning of Substitution table */
B
Behdad Esfahbod 已提交
273 274 275
  OffsetArrayOf<AlternateSet>
		alternateSet;		/* Array of AlternateSet tables
					 * ordered by Coverage Index */
B
Behdad Esfahbod 已提交
276
};
277 278
ASSERT_SIZE (AlternateSubstFormat1, 6);

B
Behdad Esfahbod 已提交
279 280
struct AlternateSubst
{
281 282 283
  friend struct SubstLookupSubTable;

  private:
B
Behdad Esfahbod 已提交
284 285
  inline bool apply (APPLY_ARG_DEF) const
  {
B
Behdad Esfahbod 已提交
286
    switch (u.format) {
287
    case 1: return u.format1->apply (APPLY_ARG);
288 289 290 291 292 293
    default:return false;
    }
  }

  private:
  union {
B
Behdad Esfahbod 已提交
294 295
  USHORT		format;		/* Format identifier */
  AlternateSubstFormat1	format1[];
296 297
  } u;
};
B
Behdad Esfahbod 已提交
298
ASSERT_SIZE (AlternateSubst, 2);
299

300

B
Behdad Esfahbod 已提交
301 302
struct Ligature
{
303 304 305
  friend struct LigatureSet;

  private:
B
Behdad Esfahbod 已提交
306 307
  inline bool apply (APPLY_ARG_DEF, bool is_mark) const
  {
308
    unsigned int i, j;
B
Behdad Esfahbod 已提交
309
    unsigned int count = component.len;
310 311 312
    unsigned int end = MIN (buffer->in_length, buffer->in_pos + context_length);
    if (HB_UNLIKELY (buffer->in_pos + count > end))
      return false;
313

B
Behdad Esfahbod 已提交
314 315 316 317
    for (i = 1, j = buffer->in_pos + 1; i < count; i++, j++)
    {
      while (!_hb_ot_layout_check_glyph_property (layout, IN_ITEM (j), lookup_flag, &property))
      {
318
	if (HB_UNLIKELY (j + count - i == end))
319 320 321 322 323 324 325 326
	  return false;
	j++;
      }

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

B
Behdad Esfahbod 已提交
327
      if (HB_LIKELY (IN_GLYPH(j) != component[i]))
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
        return false;
    }
    if ( _hb_ot_layout_has_new_glyph_classes (layout) )
      /* this is just a guess ... */
      hb_ot_layout_set_glyph_class (layout, ligGlyph,
				    is_mark ? HB_OT_LAYOUT_GLYPH_CLASS_MARK
					    : HB_OT_LAYOUT_GLYPH_CLASS_LIGATURE);

    if (j == buffer->in_pos + i) /* No input glyphs skipped */
      /* We don't use a new ligature ID if there are no skipped
	 glyphs and the ligature already has an ID. */
      _hb_buffer_add_output_glyph_ids (buffer, i,
				       1, &ligGlyph,
				       0xFFFF,
				       IN_LIGID (buffer->in_pos) ?
				       0xFFFF : _hb_buffer_allocate_ligid (buffer));
    else
    {
      unsigned int lig_id = _hb_buffer_allocate_ligid (buffer);
      _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 已提交
356
      for ( i = 1; i < count; i++ )
357 358
      {
	while (!_hb_ot_layout_check_glyph_property (layout, IN_CURITEM(), lookup_flag, &property))
B
Behdad Esfahbod 已提交
359
	  _hb_buffer_add_output_glyph (buffer, IN_CURGLYPH(), i - 1, lig_id);
360 361 362 363

	(buffer->in_pos)++;
      }

B
Behdad Esfahbod 已提交
364
      /* TODO We should possibly reassign lig_id and component for any
365 366 367 368 369 370
       * components of a previous ligature that s now being removed as part of
       * this ligature. */
    }

    return true;
  }
371 372 373

  private:
  GlyphID	ligGlyph;		/* GlyphID of ligature to substitute */
B
Behdad Esfahbod 已提交
374 375
  HeadlessArrayOf<GlyphID>
		component;		/* Array of component GlyphIDs--start
376 377 378
					 * with the second  component--ordered
					 * in writing direction */
};
379
ASSERT_SIZE (Ligature, 4);
B
Behdad Esfahbod 已提交
380

B
Behdad Esfahbod 已提交
381 382
struct LigatureSet
{
383 384 385
  friend struct LigatureSubstFormat1;

  private:
B
Behdad Esfahbod 已提交
386 387
  inline bool apply (APPLY_ARG_DEF, bool is_mark) const
  {
B
Behdad Esfahbod 已提交
388
    unsigned int num_ligs = ligature.len;
B
Behdad Esfahbod 已提交
389 390
    for (unsigned int i = 0; i < num_ligs; i++)
    {
B
Behdad Esfahbod 已提交
391
      const Ligature &lig = this+ligature[i];
392
      if (lig.apply (APPLY_ARG, is_mark))
393 394 395 396 397
        return true;
    }

    return false;
  }
B
Behdad Esfahbod 已提交
398 399

  private:
B
Behdad Esfahbod 已提交
400 401 402
  OffsetArrayOf<Ligature>
		ligature;		/* Array LigatureSet tables
					 * ordered by preference */
B
Behdad Esfahbod 已提交
403
};
404
ASSERT_SIZE (LigatureSet, 2);
B
Behdad Esfahbod 已提交
405

B
Behdad Esfahbod 已提交
406 407
struct LigatureSubstFormat1
{
408 409 410
  friend struct LigatureSubst;

  private:
B
Behdad Esfahbod 已提交
411 412
  inline bool apply (APPLY_ARG_DEF) const
  {
413 414 415 416 417
    hb_codepoint_t glyph_id = IN_CURGLYPH ();

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

B
Behdad Esfahbod 已提交
418
    unsigned int index = (this+coverage) (glyph_id);
B
Behdad Esfahbod 已提交
419
    if (HB_LIKELY (index == NOT_COVERED))
420 421
      return false;

B
Behdad Esfahbod 已提交
422
    const LigatureSet &lig_set = this+ligatureSet[index];
423
    return lig_set.apply (APPLY_ARG, first_is_mark);
424
  }
B
Behdad Esfahbod 已提交
425 426

  private:
B
Behdad Esfahbod 已提交
427
  USHORT	format;			/* Format identifier--format = 1 */
B
Behdad Esfahbod 已提交
428 429
  OffsetTo<Coverage>
		coverage;		/* Offset to Coverage table--from
430
					 * beginning of Substitution table */
B
Behdad Esfahbod 已提交
431
  OffsetArrayOf<LigatureSet>
B
Behdad Esfahbod 已提交
432 433
		ligatureSet;		/* Array LigatureSet tables
					 * ordered by Coverage Index */
B
Behdad Esfahbod 已提交
434
};
435 436
ASSERT_SIZE (LigatureSubstFormat1, 6);

B
Behdad Esfahbod 已提交
437 438
struct LigatureSubst
{
439 440 441
  friend struct SubstLookupSubTable;

  private:
B
Behdad Esfahbod 已提交
442 443
  inline bool apply (APPLY_ARG_DEF) const
  {
B
Behdad Esfahbod 已提交
444
    switch (u.format) {
445
    case 1: return u.format1->apply (APPLY_ARG);
446 447 448 449 450 451
    default:return false;
    }
  }

  private:
  union {
B
Behdad Esfahbod 已提交
452 453
  USHORT		format;		/* Format identifier */
  LigatureSubstFormat1	format1[];
454 455
  } u;
};
B
Behdad Esfahbod 已提交
456
ASSERT_SIZE (LigatureSubst, 2);
457

B
Behdad Esfahbod 已提交
458

459

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

B
Behdad Esfahbod 已提交
462 463
struct ContextSubst : Context
{
464 465 466
  friend struct SubstLookupSubTable;

  private:
B
Behdad Esfahbod 已提交
467 468
  inline bool apply (APPLY_ARG_DEF) const
  {
469
    return Context::apply (APPLY_ARG, substitute_lookup);
470 471
  }
};
B
Behdad Esfahbod 已提交
472
ASSERT_SIZE (ContextSubst, 2);
473

B
Behdad Esfahbod 已提交
474 475
struct ChainContextSubst : ChainContext
{
476 477 478
  friend struct SubstLookupSubTable;

  private:
B
Behdad Esfahbod 已提交
479 480
  inline bool apply (APPLY_ARG_DEF) const
  {
481
    return ChainContext::apply (APPLY_ARG, substitute_lookup);
B
Behdad Esfahbod 已提交
482 483
  }
};
B
Behdad Esfahbod 已提交
484
ASSERT_SIZE (ChainContextSubst, 2);
B
Behdad Esfahbod 已提交
485

486

487
struct ExtensionSubst : Extension
B
Behdad Esfahbod 已提交
488
{
489 490 491
  friend struct SubstLookupSubTable;

  private:
492
  inline bool apply (APPLY_ARG_DEF) const;
493
};
B
Behdad Esfahbod 已提交
494
ASSERT_SIZE (ExtensionSubst, 2);
495 496


B
Behdad Esfahbod 已提交
497 498
struct ReverseChainSingleSubstFormat1
{
499 500 501
  friend struct ReverseChainSingleSubst;

  private:
B
Behdad Esfahbod 已提交
502 503
  inline bool apply (APPLY_ARG_DEF) const
  {
504 505 506 507 508 509 510
    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;

511 512 513 514
    const OffsetArrayOf<Coverage> &lookahead = (const OffsetArrayOf<Coverage>&)
					       *((const char *) &backtrack + backtrack.get_size ());
    const ArrayOf<GlyphID> &substitute = (const ArrayOf<GlyphID>&)
					 *((const char *) &lookahead + lookahead.get_size ());
515

516
    if (match_backtrack (APPLY_ARG,
517 518
			 backtrack.len, (USHORT *) backtrack.array,
			 match_coverage, (char *) this) &&
519
        match_lookahead (APPLY_ARG,
520 521 522 523 524 525 526 527 528
			 lookahead.len, (USHORT *) lookahead.array,
			 match_coverage, (char *) this,
			 1))
    {
      IN_CURGLYPH() = substitute[index];
      buffer->in_pos--; /* Reverse! */
      return true;
    }

529 530
    return false;
  }
B
Behdad Esfahbod 已提交
531 532

  private:
B
Behdad Esfahbod 已提交
533
  USHORT	format;			/* Format identifier--format = 1 */
534 535 536 537 538
  OffsetTo<Coverage>
		coverage;		/* Offset to Coverage table--from
					 * beginning of table */
  OffsetArrayOf<Coverage>
		backtrack;		/* Array of coverage tables
B
Behdad Esfahbod 已提交
539 540
					 * in backtracking sequence, in  glyph
					 * sequence order */
541 542 543
  OffsetArrayOf<Coverage>
		lookaheadX;		/* Array of coverage tables
					 * in lookahead sequence, in glyph
B
Behdad Esfahbod 已提交
544
					 * sequence order */
545 546 547
  ArrayOf<GlyphID>
		substituteX;		/* Array of substitute
					 * GlyphIDs--ordered by Coverage Index */
B
Behdad Esfahbod 已提交
548 549 550
};
ASSERT_SIZE (ReverseChainSingleSubstFormat1, 10);

B
Behdad Esfahbod 已提交
551 552
struct ReverseChainSingleSubst
{
553 554 555
  friend struct SubstLookupSubTable;

  private:
B
Behdad Esfahbod 已提交
556 557
  inline bool apply (APPLY_ARG_DEF) const
  {
558
    switch (u.format) {
559
    case 1: return u.format1->apply (APPLY_ARG);
560 561 562 563 564 565 566 567 568 569 570 571 572 573
    default:return false;
    }
  }

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



B
Behdad Esfahbod 已提交
574 575 576 577
/*
 * SubstLookup
 */

B
Behdad Esfahbod 已提交
578 579
struct SubstLookupSubTable
{
B
Behdad Esfahbod 已提交
580 581
  friend struct SubstLookup;

582 583 584 585 586 587 588 589 590 591 592
  enum {
    Single		= 1,
    Multiple		= 2,
    Alternate		= 3,
    Ligature		= 4,
    Context		= 5,
    ChainContext	= 6,
    Extension		= 7,
    ReverseChainSingle	= 8,
  };

B
Behdad Esfahbod 已提交
593 594
  inline bool apply (APPLY_ARG_DEF, unsigned int lookup_type) const
  {
595
    switch (lookup_type) {
596 597 598 599 600 601 602 603
    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);
604 605
    default:return false;
    }
B
Behdad Esfahbod 已提交
606 607 608 609
  }

  private:
  union {
610 611 612 613 614 615
  USHORT			format;
  SingleSubst			single[];
  MultipleSubst			multiple[];
  AlternateSubst		alternate[];
  LigatureSubst			ligature[];
  ContextSubst			context[];
B
Behdad Esfahbod 已提交
616
  ChainContextSubst		chainContext[];
617 618
  ExtensionSubst		extension[];
  ReverseChainSingleSubst	reverseChainContextSingle[];
B
Behdad Esfahbod 已提交
619 620
  } u;
};
B
Behdad Esfahbod 已提交
621
ASSERT_SIZE (SubstLookupSubTable, 2);
B
Behdad Esfahbod 已提交
622

623

B
Behdad Esfahbod 已提交
624 625 626 627
struct SubstLookup : Lookup
{
  inline const SubstLookupSubTable& get_subtable (unsigned int i) const
  {
B
Behdad Esfahbod 已提交
628
    return (const SubstLookupSubTable&) Lookup::get_subtable (i);
B
Behdad Esfahbod 已提交
629 630 631
  }

  /* Like get_type(), but looks through extension lookups.
632
   * Never returns Extension */
B
Behdad Esfahbod 已提交
633 634
  inline unsigned int get_effective_type (void) const
  {
B
Behdad Esfahbod 已提交
635 636
    unsigned int type = get_type ();

637
    if (HB_UNLIKELY (type == SubstLookupSubTable::Extension))
B
Behdad Esfahbod 已提交
638
    {
639
      unsigned int count = get_subtable_count ();
B
Behdad Esfahbod 已提交
640
      type = get_subtable(0).u.extension->get_type ();
641 642 643 644 645
      /* 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 已提交
646 647 648 649 650
    }

    return type;
  }

B
Behdad Esfahbod 已提交
651 652
  inline bool is_reverse (void) const
  {
653
    return HB_UNLIKELY (get_effective_type () == SubstLookupSubTable::ReverseChainSingle);
B
Behdad Esfahbod 已提交
654 655
  }

656 657 658 659
  inline bool apply_subtables (hb_ot_layout_t *layout,
			       hb_buffer_t    *buffer,
			       unsigned int    context_length,
			       unsigned int    nesting_level_left,
B
Behdad Esfahbod 已提交
660 661
			       unsigned int    property) const
  {
B
Behdad Esfahbod 已提交
662
    unsigned int lookup_type = get_type ();
B
Behdad Esfahbod 已提交
663
    unsigned int lookup_flag = get_flag ();
B
Behdad Esfahbod 已提交
664 665

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

B
Behdad Esfahbod 已提交
669 670
    return false;
  }
671

B
Behdad Esfahbod 已提交
672 673
  inline bool apply_once (hb_ot_layout_t *layout, hb_buffer_t *buffer) const
  {
674 675 676 677 678 679
    unsigned int lookup_flag = get_flag ();

    unsigned int property;
    if (!_hb_ot_layout_check_glyph_property (layout, IN_CURITEM (), lookup_flag, &property))
      return false;

680
    return apply_subtables (layout, buffer, NO_CONTEXT, MAX_NESTING_LEVEL, property);
681 682
  }

683 684
  bool apply_string (hb_ot_layout_t *layout,
		     hb_buffer_t    *buffer,
B
Behdad Esfahbod 已提交
685 686
		     hb_ot_layout_feature_mask_t mask) const
  {
687 688
    bool ret = false;

B
Behdad Esfahbod 已提交
689 690 691
    if (HB_UNLIKELY (!buffer->in_length))
      return false;

B
Behdad Esfahbod 已提交
692 693
    if (HB_LIKELY (!is_reverse ()))
    {
694 695 696
	/* in/out forward substitution */
	_hb_buffer_clear_output (buffer);
	buffer->in_pos = 0;
B
Behdad Esfahbod 已提交
697 698
	while (buffer->in_pos < buffer->in_length)
	{
699
	  if ((~IN_PROPERTIES (buffer->in_pos) & mask) &&
700
	      apply_once (layout, buffer))
701 702
	    ret = true;
	  else
B
Behdad Esfahbod 已提交
703
	    _hb_buffer_next_glyph (buffer);
704 705 706 707 708

	}
	if (ret)
	  _hb_buffer_swap (buffer);

B
Behdad Esfahbod 已提交
709 710 711
    }
    else
    {
712 713 714

	/* in-place backward substitution */
	buffer->in_pos = buffer->in_length - 1;
B
Behdad Esfahbod 已提交
715 716
	do
	{
717
	  if ((~IN_PROPERTIES (buffer->in_pos) & mask) &&
718
	      apply_once (layout, buffer))
719 720 721 722
	    ret = true;
	  else
	    buffer->in_pos--;

B
Behdad Esfahbod 已提交
723 724
	}
	while ((int) buffer->in_pos >= 0);
725 726 727 728
    }

    return ret;
  }
B
Behdad Esfahbod 已提交
729
};
B
Behdad Esfahbod 已提交
730
ASSERT_SIZE (SubstLookup, 6);
B
Behdad Esfahbod 已提交
731

732

B
Minor  
Behdad Esfahbod 已提交
733 734 735 736
/*
 * GSUB
 */

B
Behdad Esfahbod 已提交
737 738
struct GSUB : GSUBGPOS
{
B
Minor  
Behdad Esfahbod 已提交
739 740 741 742
  static const hb_tag_t Tag		= HB_TAG ('G','S','U','B');

  STATIC_DEFINE_GET_FOR_DATA (GSUB);
  /* XXX check version here? */
B
Behdad Esfahbod 已提交
743

B
Behdad Esfahbod 已提交
744 745 746
  inline const SubstLookup& get_lookup (unsigned int i) const
  {
    return (const SubstLookup&) GSUBGPOS::get_lookup (i);
B
Behdad Esfahbod 已提交
747 748
  }

749 750 751
  inline bool substitute_lookup (hb_ot_layout_t *layout,
				 hb_buffer_t    *buffer,
			         unsigned int    lookup_index,
B
Behdad Esfahbod 已提交
752 753
				 hb_ot_layout_feature_mask_t  mask) const
  {
754
    return get_lookup (lookup_index).apply_string (layout, buffer, mask);
755
  }
B
Behdad Esfahbod 已提交
756

B
Minor  
Behdad Esfahbod 已提交
757
};
B
Behdad Esfahbod 已提交
758
ASSERT_SIZE (GSUB, 10);
759 760


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

763
inline bool ExtensionSubst::apply (APPLY_ARG_DEF) const
B
Behdad Esfahbod 已提交
764
{
765 766
  unsigned int lookup_type = get_type ();

767
  if (HB_UNLIKELY (lookup_type == SubstLookupSubTable::Extension))
768 769
    return false;

770
  return ((SubstLookupSubTable&) get_subtable ()).apply (APPLY_ARG, lookup_type);
771 772
}

B
Behdad Esfahbod 已提交
773 774
static inline bool substitute_lookup (APPLY_ARG_DEF, unsigned int lookup_index)
{
775
  const GSUB &gsub = *(layout->gsub);
776
  const SubstLookup &l = gsub.get_lookup (lookup_index);
777

778 779 780 781 782 783 784
  if (HB_UNLIKELY (nesting_level_left == 0))
    return false;
  nesting_level_left--;

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

B
Behdad Esfahbod 已提交
785
  /* XXX This should be apply_one I guess */
786
  return l.apply_subtables (layout, buffer, context_length, nesting_level_left, property);
787 788 789
}


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