hb-ot-layout-gsub-private.h 20.7 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

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

    glyph_id += deltaGlyphID;
B
Behdad Esfahbod 已提交
57 58 59 60 61 62 63
    _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 已提交
64 65

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

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

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

  private:

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

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

    glyph_id = substitute[index];
B
Behdad Esfahbod 已提交
95 96 97 98 99 100 101 102
    _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);
    }

103 104
    return true;
  }
B
Behdad Esfahbod 已提交
105 106

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

B
Behdad Esfahbod 已提交
117 118
struct SingleSubst
{
119 120
  friend struct SubstLookupSubTable;

121 122
  private:

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

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

B
Behdad Esfahbod 已提交
141

B
Behdad Esfahbod 已提交
142 143
struct Sequence
{
144 145 146 147
  friend struct MultipleSubstFormat1;

  private:

B
Behdad Esfahbod 已提交
148 149
  inline void set_glyph_class (hb_ot_layout_t *layout, unsigned int property) const
  {
B
Behdad Esfahbod 已提交
150
    unsigned int count = substitute.len;
151
    for (unsigned int n = 0; n < count; n++)
152 153
      _hb_ot_layout_set_glyph_property (layout, substitute[n], property);
  }
B
Behdad Esfahbod 已提交
154

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

    _hb_buffer_add_output_glyph_ids (buffer, 1,
B
Behdad Esfahbod 已提交
161
				     substitute.len, substitute.array,
B
Behdad Esfahbod 已提交
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
				     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;

      set_glyph_class (layout, property);
    }

    return true;
  }

B
Behdad Esfahbod 已提交
177
  private:
B
Behdad Esfahbod 已提交
178 179
  ArrayOf<GlyphID>
		substitute;		/* String of GlyphIDs to substitute */
B
Behdad Esfahbod 已提交
180
};
181
ASSERT_SIZE (Sequence, 2);
B
Behdad Esfahbod 已提交
182

B
Behdad Esfahbod 已提交
183 184
struct MultipleSubstFormat1
{
185 186 187 188
  friend struct MultipleSubst;

  private:

B
Behdad Esfahbod 已提交
189 190
  inline bool apply (APPLY_ARG_DEF) const
  {
191

B
Behdad Esfahbod 已提交
192
    unsigned int index = (this+coverage) (IN_CURGLYPH ());
B
Behdad Esfahbod 已提交
193
    if (HB_LIKELY (index == NOT_COVERED))
194 195
      return false;

196
    return (this+sequence[index]).apply (APPLY_ARG);
197
  }
B
Behdad Esfahbod 已提交
198 199

  private:
B
Behdad Esfahbod 已提交
200
  USHORT	format;			/* Format identifier--format = 1 */
B
Behdad Esfahbod 已提交
201 202
  OffsetTo<Coverage>
		coverage;		/* Offset to Coverage table--from
B
Behdad Esfahbod 已提交
203
					 * beginning of Substitution table */
B
Behdad Esfahbod 已提交
204 205 206
  OffsetArrayOf<Sequence>
		sequence;		/* Array of Sequence tables
					 * ordered by Coverage Index */
B
Behdad Esfahbod 已提交
207
};
208 209
ASSERT_SIZE (MultipleSubstFormat1, 6);

B
Behdad Esfahbod 已提交
210 211
struct MultipleSubst
{
212 213 214 215
  friend struct SubstLookupSubTable;

  private:

B
Behdad Esfahbod 已提交
216 217
  inline bool apply (APPLY_ARG_DEF) const
  {
B
Behdad Esfahbod 已提交
218
    switch (u.format) {
219
    case 1: return u.format1->apply (APPLY_ARG);
220 221 222 223 224 225
    default:return false;
    }
  }

  private:
  union {
B
Behdad Esfahbod 已提交
226 227
  USHORT		format;		/* Format identifier */
  MultipleSubstFormat1	format1[];
228 229
  } u;
};
B
Behdad Esfahbod 已提交
230
ASSERT_SIZE (MultipleSubst, 2);
231

B
Behdad Esfahbod 已提交
232

B
Behdad Esfahbod 已提交
233
typedef ArrayOf<GlyphID> AlternateSet;	/* Array of alternate GlyphIDs--in
B
Behdad Esfahbod 已提交
234
					 * arbitrary order */
235
ASSERT_SIZE (AlternateSet, 2);
B
Behdad Esfahbod 已提交
236

B
Behdad Esfahbod 已提交
237 238
struct AlternateSubstFormat1
{
239 240 241 242
  friend struct AlternateSubst;

  private:

B
Behdad Esfahbod 已提交
243 244
  inline bool apply (APPLY_ARG_DEF) const
  {
245
    hb_codepoint_t glyph_id = IN_CURGLYPH ();
246

B
Behdad Esfahbod 已提交
247
    unsigned int index = (this+coverage) (glyph_id);
B
Behdad Esfahbod 已提交
248
    if (HB_LIKELY (index == NOT_COVERED))
249 250
      return false;

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

B
Behdad Esfahbod 已提交
253
    if (HB_UNLIKELY (!alt_set.len))
254 255 256 257 258
      return false;

    unsigned int alt_index = 0;

    /* XXX callback to user to choose alternate
B
Behdad Esfahbod 已提交
259 260 261 262
    if (layout->altfunc)
      alt_index = (layout->altfunc)(layout, buffer,
				    buffer->out_pos, glyph_id,
				    alt_set.len, alt_set.array);
263 264
				   */

B
Behdad Esfahbod 已提交
265
    if (HB_UNLIKELY (alt_index >= alt_set.len))
266 267 268 269
      return false;

    glyph_id = alt_set[alt_index];

B
Behdad Esfahbod 已提交
270
    _hb_buffer_replace_glyph (buffer, glyph_id);
271 272 273 274 275 276 277 278 279

    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 已提交
280 281

  private:
B
Behdad Esfahbod 已提交
282
  USHORT	format;			/* Format identifier--format = 1 */
B
Behdad Esfahbod 已提交
283 284
  OffsetTo<Coverage>
		coverage;		/* Offset to Coverage table--from
B
Behdad Esfahbod 已提交
285
					 * beginning of Substitution table */
B
Behdad Esfahbod 已提交
286 287 288
  OffsetArrayOf<AlternateSet>
		alternateSet;		/* Array of AlternateSet tables
					 * ordered by Coverage Index */
B
Behdad Esfahbod 已提交
289
};
290 291
ASSERT_SIZE (AlternateSubstFormat1, 6);

B
Behdad Esfahbod 已提交
292 293
struct AlternateSubst
{
294 295 296 297
  friend struct SubstLookupSubTable;

  private:

B
Behdad Esfahbod 已提交
298 299
  inline bool apply (APPLY_ARG_DEF) const
  {
B
Behdad Esfahbod 已提交
300
    switch (u.format) {
301
    case 1: return u.format1->apply (APPLY_ARG);
302 303 304 305 306 307
    default:return false;
    }
  }

  private:
  union {
B
Behdad Esfahbod 已提交
308 309
  USHORT		format;		/* Format identifier */
  AlternateSubstFormat1	format1[];
310 311
  } u;
};
B
Behdad Esfahbod 已提交
312
ASSERT_SIZE (AlternateSubst, 2);
313

314

B
Behdad Esfahbod 已提交
315 316
struct Ligature
{
317 318 319
  friend struct LigatureSet;

  private:
B
Behdad Esfahbod 已提交
320 321
  inline bool apply (APPLY_ARG_DEF, bool is_mark) const
  {
322
    unsigned int i, j;
B
Behdad Esfahbod 已提交
323
    unsigned int count = component.len;
324 325 326
    unsigned int end = MIN (buffer->in_length, buffer->in_pos + context_length);
    if (HB_UNLIKELY (buffer->in_pos + count > end))
      return false;
327

B
Behdad Esfahbod 已提交
328 329 330 331
    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))
      {
332
	if (HB_UNLIKELY (j + count - i == end))
333 334 335 336 337 338 339 340
	  return false;
	j++;
      }

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

B
Behdad Esfahbod 已提交
341
      if (HB_LIKELY (IN_GLYPH(j) != component[i]))
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
        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 已提交
370
      for ( i = 1; i < count; i++ )
371 372
      {
	while (!_hb_ot_layout_check_glyph_property (layout, IN_CURITEM(), lookup_flag, &property))
B
Behdad Esfahbod 已提交
373
	  _hb_buffer_add_output_glyph (buffer, IN_CURGLYPH(), i - 1, lig_id);
374 375 376 377

	(buffer->in_pos)++;
      }

B
Behdad Esfahbod 已提交
378
      /* TODO We should possibly reassign lig_id and component for any
379 380 381 382 383 384
       * components of a previous ligature that s now being removed as part of
       * this ligature. */
    }

    return true;
  }
385 386 387

  private:
  GlyphID	ligGlyph;		/* GlyphID of ligature to substitute */
B
Behdad Esfahbod 已提交
388 389
  HeadlessArrayOf<GlyphID>
		component;		/* Array of component GlyphIDs--start
390 391 392
					 * with the second  component--ordered
					 * in writing direction */
};
393
ASSERT_SIZE (Ligature, 4);
B
Behdad Esfahbod 已提交
394

B
Behdad Esfahbod 已提交
395 396
struct LigatureSet
{
397 398 399 400
  friend struct LigatureSubstFormat1;

  private:

B
Behdad Esfahbod 已提交
401 402
  inline bool apply (APPLY_ARG_DEF, bool is_mark) const
  {
B
Behdad Esfahbod 已提交
403
    unsigned int num_ligs = ligature.len;
B
Behdad Esfahbod 已提交
404 405
    for (unsigned int i = 0; i < num_ligs; i++)
    {
B
Behdad Esfahbod 已提交
406
      const Ligature &lig = this+ligature[i];
407
      if (lig.apply (APPLY_ARG, is_mark))
408 409 410 411 412
        return true;
    }

    return false;
  }
B
Behdad Esfahbod 已提交
413 414

  private:
B
Behdad Esfahbod 已提交
415 416 417
  OffsetArrayOf<Ligature>
		ligature;		/* Array LigatureSet tables
					 * ordered by preference */
B
Behdad Esfahbod 已提交
418
};
419
ASSERT_SIZE (LigatureSet, 2);
B
Behdad Esfahbod 已提交
420

B
Behdad Esfahbod 已提交
421 422
struct LigatureSubstFormat1
{
423 424 425 426
  friend struct LigatureSubst;

  private:

B
Behdad Esfahbod 已提交
427 428
  inline bool apply (APPLY_ARG_DEF) const
  {
429 430 431 432 433
    hb_codepoint_t glyph_id = IN_CURGLYPH ();

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

B
Behdad Esfahbod 已提交
434
    unsigned int index = (this+coverage) (glyph_id);
B
Behdad Esfahbod 已提交
435
    if (HB_LIKELY (index == NOT_COVERED))
436 437
      return false;

B
Behdad Esfahbod 已提交
438
    const LigatureSet &lig_set = this+ligatureSet[index];
439
    return lig_set.apply (APPLY_ARG, first_is_mark);
440
  }
B
Behdad Esfahbod 已提交
441 442

  private:
B
Behdad Esfahbod 已提交
443
  USHORT	format;			/* Format identifier--format = 1 */
B
Behdad Esfahbod 已提交
444 445
  OffsetTo<Coverage>
		coverage;		/* Offset to Coverage table--from
446
					 * beginning of Substitution table */
B
Behdad Esfahbod 已提交
447
  OffsetArrayOf<LigatureSet>
B
Behdad Esfahbod 已提交
448 449
		ligatureSet;		/* Array LigatureSet tables
					 * ordered by Coverage Index */
B
Behdad Esfahbod 已提交
450
};
451 452
ASSERT_SIZE (LigatureSubstFormat1, 6);

B
Behdad Esfahbod 已提交
453 454
struct LigatureSubst
{
455 456 457 458
  friend struct SubstLookupSubTable;

  private:

B
Behdad Esfahbod 已提交
459 460
  inline bool apply (APPLY_ARG_DEF) const
  {
B
Behdad Esfahbod 已提交
461
    switch (u.format) {
462
    case 1: return u.format1->apply (APPLY_ARG);
463 464 465 466 467 468
    default:return false;
    }
  }

  private:
  union {
B
Behdad Esfahbod 已提交
469 470
  USHORT		format;		/* Format identifier */
  LigatureSubstFormat1	format1[];
471 472
  } u;
};
B
Behdad Esfahbod 已提交
473
ASSERT_SIZE (LigatureSubst, 2);
474

B
Behdad Esfahbod 已提交
475

476

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

B
Behdad Esfahbod 已提交
479 480 481 482
struct ContextSubst : Context
{
  inline bool apply (APPLY_ARG_DEF) const
  {
483
    return Context::apply (APPLY_ARG, substitute_lookup);
484 485
  }
};
B
Behdad Esfahbod 已提交
486
ASSERT_SIZE (ContextSubst, 2);
487

B
Behdad Esfahbod 已提交
488 489 490 491
struct ChainContextSubst : ChainContext
{
  inline bool apply (APPLY_ARG_DEF) const
  {
492
    return ChainContext::apply (APPLY_ARG, substitute_lookup);
B
Behdad Esfahbod 已提交
493 494
  }
};
B
Behdad Esfahbod 已提交
495
ASSERT_SIZE (ChainContextSubst, 2);
B
Behdad Esfahbod 已提交
496

497

B
Behdad Esfahbod 已提交
498 499
struct ExtensionSubstFormat1
{
500 501 502 503
  friend struct ExtensionSubst;

  private:
  inline unsigned int get_type (void) const { return extensionLookupType; }
B
Behdad Esfahbod 已提交
504
  inline unsigned int get_offset (void) const { return (extensionOffset[0] << 16) + extensionOffset[1]; }
505
  inline bool apply (APPLY_ARG_DEF) const;
B
Behdad Esfahbod 已提交
506 507

  private:
B
Behdad Esfahbod 已提交
508
  USHORT	format;			/* Format identifier. Set to 1. */
B
Behdad Esfahbod 已提交
509 510 511
  USHORT	extensionLookupType;	/* Lookup type of subtable referenced
					 * by ExtensionOffset (i.e. the
					 * extension subtable). */
B
Behdad Esfahbod 已提交
512 513 514 515
  USHORT	extensionOffset[2];	/* Offset to the extension subtable,
					 * of lookup type subtable.
					 * Defined as two shorts to avoid
					 * alignment requirements. */
B
Behdad Esfahbod 已提交
516 517 518
};
ASSERT_SIZE (ExtensionSubstFormat1, 8);

B
Behdad Esfahbod 已提交
519 520
struct ExtensionSubst
{
521 522 523 524 525
  friend struct SubstLookup;
  friend struct SubstLookupSubTable;

  private:

B
Behdad Esfahbod 已提交
526 527
  inline unsigned int get_type (void) const
  {
B
Behdad Esfahbod 已提交
528 529
    switch (u.format) {
    case 1: return u.format1->get_type ();
530 531 532 533
    default:return 0;
    }
  }

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

  private:
  union {
B
Behdad Esfahbod 已提交
544 545
  USHORT		format;		/* Format identifier */
  ExtensionSubstFormat1	format1[];
546 547
  } u;
};
B
Behdad Esfahbod 已提交
548
ASSERT_SIZE (ExtensionSubst, 2);
549 550


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

  private:
B
Behdad Esfahbod 已提交
556 557
  inline bool apply (APPLY_ARG_DEF) const
  {
558 559 560 561 562 563 564
    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;

565 566 567 568
    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 ());
569

570
    if (match_backtrack (APPLY_ARG,
571 572
			 backtrack.len, (USHORT *) backtrack.array,
			 match_coverage, (char *) this) &&
573
        match_lookahead (APPLY_ARG,
574 575 576 577 578 579 580 581 582
			 lookahead.len, (USHORT *) lookahead.array,
			 match_coverage, (char *) this,
			 1))
    {
      IN_CURGLYPH() = substitute[index];
      buffer->in_pos--; /* Reverse! */
      return true;
    }

583 584
    return false;
  }
B
Behdad Esfahbod 已提交
585 586

  private:
B
Behdad Esfahbod 已提交
587
  USHORT	format;			/* Format identifier--format = 1 */
588 589 590 591 592
  OffsetTo<Coverage>
		coverage;		/* Offset to Coverage table--from
					 * beginning of table */
  OffsetArrayOf<Coverage>
		backtrack;		/* Array of coverage tables
B
Behdad Esfahbod 已提交
593 594
					 * in backtracking sequence, in  glyph
					 * sequence order */
595 596 597
  OffsetArrayOf<Coverage>
		lookaheadX;		/* Array of coverage tables
					 * in lookahead sequence, in glyph
B
Behdad Esfahbod 已提交
598
					 * sequence order */
599 600 601
  ArrayOf<GlyphID>
		substituteX;		/* Array of substitute
					 * GlyphIDs--ordered by Coverage Index */
B
Behdad Esfahbod 已提交
602 603 604
};
ASSERT_SIZE (ReverseChainSingleSubstFormat1, 10);

B
Behdad Esfahbod 已提交
605 606
struct ReverseChainSingleSubst
{
607 608 609 610
  friend struct SubstLookupSubTable;

  private:

B
Behdad Esfahbod 已提交
611 612
  inline bool apply (APPLY_ARG_DEF) const
  {
613
    switch (u.format) {
614
    case 1: return u.format1->apply (APPLY_ARG);
615 616 617 618 619 620 621 622 623 624 625 626 627 628
    default:return false;
    }
  }

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



B
Behdad Esfahbod 已提交
629 630 631 632
/*
 * SubstLookup
 */

B
Behdad Esfahbod 已提交
633 634
struct SubstLookupSubTable
{
B
Behdad Esfahbod 已提交
635 636
  friend struct SubstLookup;

637 638 639 640 641 642 643 644 645 646 647
  enum {
    Single		= 1,
    Multiple		= 2,
    Alternate		= 3,
    Ligature		= 4,
    Context		= 5,
    ChainContext	= 6,
    Extension		= 7,
    ReverseChainSingle	= 8,
  };

B
Behdad Esfahbod 已提交
648 649
  inline bool apply (APPLY_ARG_DEF, unsigned int lookup_type) const
  {
650
    switch (lookup_type) {
651 652 653 654 655 656 657 658
    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);
659 660
    default:return false;
    }
B
Behdad Esfahbod 已提交
661 662 663 664
  }

  private:
  union {
665 666 667 668 669 670
  USHORT			format;
  SingleSubst			single[];
  MultipleSubst			multiple[];
  AlternateSubst		alternate[];
  LigatureSubst			ligature[];
  ContextSubst			context[];
B
Behdad Esfahbod 已提交
671
  ChainContextSubst		chainContext[];
672 673
  ExtensionSubst		extension[];
  ReverseChainSingleSubst	reverseChainContextSingle[];
B
Behdad Esfahbod 已提交
674 675
  } u;
};
B
Behdad Esfahbod 已提交
676
ASSERT_SIZE (SubstLookupSubTable, 2);
B
Behdad Esfahbod 已提交
677

678

B
Behdad Esfahbod 已提交
679 680 681 682
struct SubstLookup : Lookup
{
  inline const SubstLookupSubTable& get_subtable (unsigned int i) const
  {
B
Behdad Esfahbod 已提交
683
    return (const SubstLookupSubTable&) Lookup::get_subtable (i);
B
Behdad Esfahbod 已提交
684 685 686
  }

  /* Like get_type(), but looks through extension lookups.
687
   * Never returns Extension */
B
Behdad Esfahbod 已提交
688 689
  inline unsigned int get_effective_type (void) const
  {
B
Behdad Esfahbod 已提交
690 691
    unsigned int type = get_type ();

692
    if (HB_UNLIKELY (type == SubstLookupSubTable::Extension))
B
Behdad Esfahbod 已提交
693
    {
694
      unsigned int count = get_subtable_count ();
B
Behdad Esfahbod 已提交
695
      type = get_subtable(0).u.extension->get_type ();
696 697 698 699 700
      /* 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 已提交
701 702 703 704 705
    }

    return type;
  }

B
Behdad Esfahbod 已提交
706 707
  inline bool is_reverse (void) const
  {
708
    return HB_UNLIKELY (get_effective_type () == SubstLookupSubTable::ReverseChainSingle);
B
Behdad Esfahbod 已提交
709 710
  }

711 712 713 714
  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 已提交
715 716
			       unsigned int    property) const
  {
B
Behdad Esfahbod 已提交
717
    unsigned int lookup_type = get_type ();
B
Behdad Esfahbod 已提交
718
    unsigned int lookup_flag = get_flag ();
B
Behdad Esfahbod 已提交
719 720

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

B
Behdad Esfahbod 已提交
724 725
    return false;
  }
726

B
Behdad Esfahbod 已提交
727 728
  inline bool apply_once (hb_ot_layout_t *layout, hb_buffer_t *buffer) const
  {
729 730 731 732 733 734
    unsigned int lookup_flag = get_flag ();

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

735
    return apply_subtables (layout, buffer, NO_CONTEXT, MAX_NESTING_LEVEL, property);
736 737
  }

738 739
  bool apply_string (hb_ot_layout_t *layout,
		     hb_buffer_t    *buffer,
B
Behdad Esfahbod 已提交
740 741
		     hb_ot_layout_feature_mask_t mask) const
  {
742 743
    bool ret = false;

B
Behdad Esfahbod 已提交
744 745 746
    if (HB_UNLIKELY (!buffer->in_length))
      return false;

B
Behdad Esfahbod 已提交
747 748
    if (HB_LIKELY (!is_reverse ()))
    {
749 750 751
	/* in/out forward substitution */
	_hb_buffer_clear_output (buffer);
	buffer->in_pos = 0;
B
Behdad Esfahbod 已提交
752 753
	while (buffer->in_pos < buffer->in_length)
	{
754
	  if ((~IN_PROPERTIES (buffer->in_pos) & mask) &&
755
	      apply_once (layout, buffer))
756 757
	    ret = true;
	  else
B
Behdad Esfahbod 已提交
758
	    _hb_buffer_next_glyph (buffer);
759 760 761 762 763

	}
	if (ret)
	  _hb_buffer_swap (buffer);

B
Behdad Esfahbod 已提交
764 765 766
    }
    else
    {
767 768 769

	/* in-place backward substitution */
	buffer->in_pos = buffer->in_length - 1;
B
Behdad Esfahbod 已提交
770 771
	do
	{
772
	  if ((~IN_PROPERTIES (buffer->in_pos) & mask) &&
773
	      apply_once (layout, buffer))
774 775 776 777
	    ret = true;
	  else
	    buffer->in_pos--;

B
Behdad Esfahbod 已提交
778 779
	}
	while ((int) buffer->in_pos >= 0);
780 781 782 783
    }

    return ret;
  }
B
Behdad Esfahbod 已提交
784
};
B
Behdad Esfahbod 已提交
785
ASSERT_SIZE (SubstLookup, 6);
B
Behdad Esfahbod 已提交
786

787

B
Minor  
Behdad Esfahbod 已提交
788 789 790 791
/*
 * GSUB
 */

B
Behdad Esfahbod 已提交
792 793
struct GSUB : GSUBGPOS
{
B
Minor  
Behdad Esfahbod 已提交
794 795 796 797
  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 已提交
798

B
Behdad Esfahbod 已提交
799 800 801
  inline const SubstLookup& get_lookup (unsigned int i) const
  {
    return (const SubstLookup&) GSUBGPOS::get_lookup (i);
B
Behdad Esfahbod 已提交
802 803
  }

804 805 806
  inline bool substitute_lookup (hb_ot_layout_t *layout,
				 hb_buffer_t    *buffer,
			         unsigned int    lookup_index,
B
Behdad Esfahbod 已提交
807 808
				 hb_ot_layout_feature_mask_t  mask) const
  {
809
    return get_lookup (lookup_index).apply_string (layout, buffer, mask);
810
  }
B
Behdad Esfahbod 已提交
811

B
Minor  
Behdad Esfahbod 已提交
812
};
B
Behdad Esfahbod 已提交
813
ASSERT_SIZE (GSUB, 10);
814 815


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

B
Behdad Esfahbod 已提交
818 819
inline bool ExtensionSubstFormat1::apply (APPLY_ARG_DEF) const
{
820 821
  unsigned int lookup_type = get_type ();

822
  if (HB_UNLIKELY (lookup_type ==  SubstLookupSubTable::Extension))
823 824
    return false;

825
  return ((SubstLookupSubTable&)*(((char *) this) + get_offset ())).apply (APPLY_ARG, lookup_type);
826 827
}

B
Behdad Esfahbod 已提交
828 829
static inline bool substitute_lookup (APPLY_ARG_DEF, unsigned int lookup_index)
{
830
  const GSUB &gsub = *(layout->gsub);
831
  const SubstLookup &l = gsub.get_lookup (lookup_index);
832

833 834 835 836 837 838 839
  if (HB_UNLIKELY (nesting_level_left == 0))
    return false;
  nesting_level_left--;

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

B
Behdad Esfahbod 已提交
840
  /* XXX This should be apply_one I guess */
841
  return l.apply_subtables (layout, buffer, context_length, nesting_level_left, property);
842 843 844
}


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