hb-ot-layout-gsub-table.hh 41.8 KB
Newer Older
B
Behdad Esfahbod 已提交
1
/*
B
Behdad Esfahbod 已提交
2
 * Copyright © 2007,2008,2009,2010  Red Hat, Inc.
3
 * Copyright © 2010,2012,2013  Google, Inc.
B
Behdad Esfahbod 已提交
4
 *
B
Behdad Esfahbod 已提交
5
 *  This is part of HarfBuzz, a text shaping library.
B
Behdad Esfahbod 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 * 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
26
 * Google Author(s): Behdad Esfahbod
B
Behdad Esfahbod 已提交
27 28
 */

29 30
#ifndef HB_OT_LAYOUT_GSUB_TABLE_HH
#define HB_OT_LAYOUT_GSUB_TABLE_HH
B
Behdad Esfahbod 已提交
31

32
#include "hb-ot-layout-gsubgpos-private.hh"
B
Behdad Esfahbod 已提交
33

B
Behdad Esfahbod 已提交
34

35 36
namespace OT {

37

B
Behdad Esfahbod 已提交
38 39
struct SingleSubstFormat1
{
B
Behdad Esfahbod 已提交
40
  inline void closure (hb_closure_context_t *c) const
41
  {
B
Behdad Esfahbod 已提交
42
    TRACE_CLOSURE (this);
B
Behdad Esfahbod 已提交
43
    Coverage::Iter iter;
44 45 46
    for (iter.init (this+coverage); iter.more (); iter.next ())
    {
      /* TODO Switch to range-based API to work around malicious fonts.
47
       * https://github.com/harfbuzz/harfbuzz/issues/363 */
B
Behdad Esfahbod 已提交
48 49
      hb_codepoint_t glyph_id = iter.get_glyph ();
      if (c->glyphs->has (glyph_id))
50
	c->glyphs->add ((glyph_id + deltaGlyphID) & 0xFFFFu);
B
Behdad Esfahbod 已提交
51
    }
52 53
  }

54 55
  inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
  {
B
Behdad Esfahbod 已提交
56
    TRACE_COLLECT_GLYPHS (this);
57
    if (unlikely (!(this+coverage).add_coverage (c->input))) return;
58
    Coverage::Iter iter;
59 60 61
    for (iter.init (this+coverage); iter.more (); iter.next ())
    {
      /* TODO Switch to range-based API to work around malicious fonts.
62
       * https://github.com/harfbuzz/harfbuzz/issues/363 */
63
      hb_codepoint_t glyph_id = iter.get_glyph ();
64
      c->output->add ((glyph_id + deltaGlyphID) & 0xFFFFu);
65 66 67
    }
  }

68 69 70 71 72
  inline const Coverage &get_coverage (void) const
  {
    return this+coverage;
  }

73 74
  inline bool would_apply (hb_would_apply_context_t *c) const
  {
B
Behdad Esfahbod 已提交
75
    TRACE_WOULD_APPLY (this);
B
Behdad Esfahbod 已提交
76
    return_trace (c->len == 1 && (this+coverage).get_coverage (c->glyphs[0]) != NOT_COVERED);
77 78
  }

79
  inline bool apply (hb_ot_apply_context_t *c) const
B
Behdad Esfahbod 已提交
80
  {
B
Behdad Esfahbod 已提交
81
    TRACE_APPLY (this);
82
    hb_codepoint_t glyph_id = c->buffer->cur().codepoint;
83
    unsigned int index = (this+coverage).get_coverage (glyph_id);
B
Behdad Esfahbod 已提交
84
    if (likely (index == NOT_COVERED)) return_trace (false);
B
Behdad Esfahbod 已提交
85

86 87
    /* According to the Adobe Annotated OpenType Suite, result is always
     * limited to 16bit. */
88
    glyph_id = (glyph_id + deltaGlyphID) & 0xFFFFu;
89
    c->replace_glyph (glyph_id);
B
Behdad Esfahbod 已提交
90

B
Behdad Esfahbod 已提交
91
    return_trace (true);
92
  }
B
Behdad Esfahbod 已提交
93

94
  inline bool serialize (hb_serialize_context_t *c,
95
			 Supplier<GlyphID> &glyphs,
96
			 unsigned int num_glyphs,
B
Minor  
Behdad Esfahbod 已提交
97
			 int delta)
98
  {
B
Behdad Esfahbod 已提交
99
    TRACE_SERIALIZE (this);
B
Behdad Esfahbod 已提交
100 101
    if (unlikely (!c->extend_min (*this))) return_trace (false);
    if (unlikely (!coverage.serialize (c, this).serialize (c, glyphs, num_glyphs))) return_trace (false);
B
Minor  
Behdad Esfahbod 已提交
102
    deltaGlyphID.set (delta); /* TODO(serilaize) overflow? */
B
Behdad Esfahbod 已提交
103
    return_trace (true);
104 105
  }

B
Behdad Esfahbod 已提交
106 107
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
B
Behdad Esfahbod 已提交
108
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
109
    return_trace (coverage.sanitize (c, this) && deltaGlyphID.sanitize (c));
B
Behdad Esfahbod 已提交
110 111
  }

112
  protected:
B
Behdad Esfahbod 已提交
113
  HBUINT16	format;			/* Format identifier--format = 1 */
B
Behdad Esfahbod 已提交
114 115
  OffsetTo<Coverage>
		coverage;		/* Offset to Coverage table--from
B
Behdad Esfahbod 已提交
116
					 * beginning of Substitution table */
B
Behdad Esfahbod 已提交
117
  HBINT16	deltaGlyphID;		/* Add to original GlyphID to get
B
Behdad Esfahbod 已提交
118
					 * substitute GlyphID */
119 120
  public:
  DEFINE_SIZE_STATIC (6);
B
Behdad Esfahbod 已提交
121 122
};

B
Behdad Esfahbod 已提交
123 124
struct SingleSubstFormat2
{
B
Behdad Esfahbod 已提交
125
  inline void closure (hb_closure_context_t *c) const
126
  {
B
Behdad Esfahbod 已提交
127
    TRACE_CLOSURE (this);
B
Behdad Esfahbod 已提交
128
    Coverage::Iter iter;
129 130 131 132
    unsigned int count = substitute.len;
    for (iter.init (this+coverage); iter.more (); iter.next ())
    {
      if (unlikely (iter.get_coverage () >= count))
133
        break; /* Work around malicious fonts. https://github.com/harfbuzz/harfbuzz/issues/363 */
B
Behdad Esfahbod 已提交
134
      if (c->glyphs->has (iter.get_glyph ()))
B
Behdad Esfahbod 已提交
135
	c->glyphs->add (substitute[iter.get_coverage ()]);
B
Behdad Esfahbod 已提交
136
    }
137 138
  }

139 140
  inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
  {
B
Behdad Esfahbod 已提交
141
    TRACE_COLLECT_GLYPHS (this);
142
    if (unlikely (!(this+coverage).add_coverage (c->input))) return;
143
    Coverage::Iter iter;
144 145 146 147
    unsigned int count = substitute.len;
    for (iter.init (this+coverage); iter.more (); iter.next ())
    {
      if (unlikely (iter.get_coverage () >= count))
148
        break; /* Work around malicious fonts. https://github.com/harfbuzz/harfbuzz/issues/363 */
B
Minor  
Behdad Esfahbod 已提交
149
      c->output->add (substitute[iter.get_coverage ()]);
150 151 152
    }
  }

153 154 155 156 157
  inline const Coverage &get_coverage (void) const
  {
    return this+coverage;
  }

158 159
  inline bool would_apply (hb_would_apply_context_t *c) const
  {
B
Behdad Esfahbod 已提交
160
    TRACE_WOULD_APPLY (this);
B
Behdad Esfahbod 已提交
161
    return_trace (c->len == 1 && (this+coverage).get_coverage (c->glyphs[0]) != NOT_COVERED);
162 163
  }

164
  inline bool apply (hb_ot_apply_context_t *c) const
B
Behdad Esfahbod 已提交
165
  {
B
Behdad Esfahbod 已提交
166
    TRACE_APPLY (this);
167
    hb_codepoint_t glyph_id = c->buffer->cur().codepoint;
168
    unsigned int index = (this+coverage).get_coverage (glyph_id);
B
Behdad Esfahbod 已提交
169
    if (likely (index == NOT_COVERED)) return_trace (false);
170

B
Behdad Esfahbod 已提交
171
    if (unlikely (index >= substitute.len)) return_trace (false);
172 173

    glyph_id = substitute[index];
174
    c->replace_glyph (glyph_id);
B
Behdad Esfahbod 已提交
175

B
Behdad Esfahbod 已提交
176
    return_trace (true);
177
  }
B
Behdad Esfahbod 已提交
178

B
Behdad Esfahbod 已提交
179
  inline bool serialize (hb_serialize_context_t *c,
180 181
			 Supplier<GlyphID> &glyphs,
			 Supplier<GlyphID> &substitutes,
B
Behdad Esfahbod 已提交
182 183
			 unsigned int num_glyphs)
  {
B
Behdad Esfahbod 已提交
184
    TRACE_SERIALIZE (this);
B
Behdad Esfahbod 已提交
185 186 187 188
    if (unlikely (!c->extend_min (*this))) return_trace (false);
    if (unlikely (!substitute.serialize (c, substitutes, num_glyphs))) return_trace (false);
    if (unlikely (!coverage.serialize (c, this).serialize (c, glyphs, num_glyphs))) return_trace (false);
    return_trace (true);
B
Behdad Esfahbod 已提交
189 190
  }

B
Behdad Esfahbod 已提交
191 192
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
B
Behdad Esfahbod 已提交
193
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
194
    return_trace (coverage.sanitize (c, this) && substitute.sanitize (c));
B
Behdad Esfahbod 已提交
195 196
  }

197
  protected:
B
Behdad Esfahbod 已提交
198
  HBUINT16	format;			/* Format identifier--format = 2 */
B
Behdad Esfahbod 已提交
199 200
  OffsetTo<Coverage>
		coverage;		/* Offset to Coverage table--from
B
Behdad Esfahbod 已提交
201
					 * beginning of Substitution table */
B
Behdad Esfahbod 已提交
202 203 204
  ArrayOf<GlyphID>
		substitute;		/* Array of substitute
					 * GlyphIDs--ordered by Coverage Index */
205
  public:
206
  DEFINE_SIZE_ARRAY (6, substitute);
B
Behdad Esfahbod 已提交
207 208
};

B
Behdad Esfahbod 已提交
209 210
struct SingleSubst
{
B
Behdad Esfahbod 已提交
211
  inline bool serialize (hb_serialize_context_t *c,
212 213
			 Supplier<GlyphID> &glyphs,
			 Supplier<GlyphID> &substitutes,
B
Behdad Esfahbod 已提交
214 215
			 unsigned int num_glyphs)
  {
B
Behdad Esfahbod 已提交
216
    TRACE_SERIALIZE (this);
B
Behdad Esfahbod 已提交
217
    if (unlikely (!c->extend_min (u.format))) return_trace (false);
B
Behdad Esfahbod 已提交
218
    unsigned int format = 2;
B
Behdad Esfahbod 已提交
219
    int delta = 0;
B
Behdad Esfahbod 已提交
220 221
    if (num_glyphs) {
      format = 1;
B
Minor  
Behdad Esfahbod 已提交
222
      /* TODO(serialize) check for wrap-around */
B
Behdad Esfahbod 已提交
223 224 225 226 227 228 229 230 231
      delta = substitutes[0] - glyphs[0];
      for (unsigned int i = 1; i < num_glyphs; i++)
	if (delta != substitutes[i] - glyphs[i]) {
	  format = 2;
	  break;
	}
    }
    u.format.set (format);
    switch (u.format) {
B
Behdad Esfahbod 已提交
232 233 234
    case 1: return_trace (u.format1.serialize (c, glyphs, num_glyphs, delta));
    case 2: return_trace (u.format2.serialize (c, glyphs, substitutes, num_glyphs));
    default:return_trace (false);
B
Behdad Esfahbod 已提交
235 236 237
    }
  }

B
Behdad Esfahbod 已提交
238 239 240
  template <typename context_t>
  inline typename context_t::return_t dispatch (context_t *c) const
  {
241
    TRACE_DISPATCH (this, u.format);
242
    if (unlikely (!c->may_dispatch (this, &u.format))) return_trace (c->no_dispatch_return_value ());
B
Behdad Esfahbod 已提交
243
    switch (u.format) {
B
Behdad Esfahbod 已提交
244 245 246
    case 1: return_trace (c->dispatch (u.format1));
    case 2: return_trace (c->dispatch (u.format2));
    default:return_trace (c->default_return_value ());
B
Behdad Esfahbod 已提交
247 248 249
    }
  }

250
  protected:
251
  union {
B
Behdad Esfahbod 已提交
252
  HBUINT16		format;		/* Format identifier */
B
Behdad Esfahbod 已提交
253 254
  SingleSubstFormat1	format1;
  SingleSubstFormat2	format2;
255
  } u;
B
Behdad Esfahbod 已提交
256
};
257

B
Behdad Esfahbod 已提交
258

B
Behdad Esfahbod 已提交
259 260
struct Sequence
{
B
Behdad Esfahbod 已提交
261
  inline void closure (hb_closure_context_t *c) const
262
  {
B
Behdad Esfahbod 已提交
263
    TRACE_CLOSURE (this);
B
Behdad Esfahbod 已提交
264 265
    unsigned int count = substitute.len;
    for (unsigned int i = 0; i < count; i++)
B
Behdad Esfahbod 已提交
266
      c->glyphs->add (substitute[i]);
267 268
  }

269 270
  inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
  {
B
Behdad Esfahbod 已提交
271
    TRACE_COLLECT_GLYPHS (this);
272
    c->output->add_array (substitute.array, substitute.len);
273 274
  }

275
  inline bool apply (hb_ot_apply_context_t *c) const
B
Behdad Esfahbod 已提交
276
  {
B
Behdad Esfahbod 已提交
277
    TRACE_APPLY (this);
B
Behdad Esfahbod 已提交
278
    unsigned int count = substitute.len;
279

B
Behdad Esfahbod 已提交
280 281 282
    /* Special-case to make it in-place and not consider this
     * as a "multiplied" substitution. */
    if (unlikely (count == 1))
283 284
    {
      c->replace_glyph (substitute.array[0]);
B
Behdad Esfahbod 已提交
285
      return_trace (true);
286
    }
287
    /* Spec disallows this, but Uniscribe allows it.
288
     * https://github.com/harfbuzz/harfbuzz/issues/253 */
289 290 291 292 293
    else if (unlikely (count == 0))
    {
      c->buffer->delete_glyph ();
      return_trace (true);
    }
B
Behdad Esfahbod 已提交
294 295 296 297 298 299

    unsigned int klass = _hb_glyph_info_is_ligature (&c->buffer->cur()) ?
			 HB_OT_LAYOUT_GLYPH_PROPS_BASE_GLYPH : 0;

    for (unsigned int i = 0; i < count; i++) {
      _hb_glyph_info_set_lig_props_for_component (&c->buffer->cur(), i);
300
      c->output_glyph_for_component (substitute.array[i], klass);
301
    }
B
Behdad Esfahbod 已提交
302
    c->buffer->skip_glyph ();
B
Behdad Esfahbod 已提交
303

B
Behdad Esfahbod 已提交
304
    return_trace (true);
B
Behdad Esfahbod 已提交
305 306
  }

307
  inline bool serialize (hb_serialize_context_t *c,
308
			 Supplier<GlyphID> &glyphs,
309 310
			 unsigned int num_glyphs)
  {
B
Behdad Esfahbod 已提交
311
    TRACE_SERIALIZE (this);
B
Behdad Esfahbod 已提交
312 313 314
    if (unlikely (!c->extend_min (*this))) return_trace (false);
    if (unlikely (!substitute.serialize (c, glyphs, num_glyphs))) return_trace (false);
    return_trace (true);
315 316
  }

B
Behdad Esfahbod 已提交
317 318
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
B
Behdad Esfahbod 已提交
319
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
320
    return_trace (substitute.sanitize (c));
B
Behdad Esfahbod 已提交
321 322
  }

323
  protected:
B
Behdad Esfahbod 已提交
324 325
  ArrayOf<GlyphID>
		substitute;		/* String of GlyphIDs to substitute */
326
  public:
327
  DEFINE_SIZE_ARRAY (2, substitute);
B
Behdad Esfahbod 已提交
328 329
};

B
Behdad Esfahbod 已提交
330 331
struct MultipleSubstFormat1
{
B
Behdad Esfahbod 已提交
332
  inline void closure (hb_closure_context_t *c) const
333
  {
B
Behdad Esfahbod 已提交
334
    TRACE_CLOSURE (this);
B
Behdad Esfahbod 已提交
335
    Coverage::Iter iter;
336 337 338 339
    unsigned int count = sequence.len;
    for (iter.init (this+coverage); iter.more (); iter.next ())
    {
      if (unlikely (iter.get_coverage () >= count))
340
        break; /* Work around malicious fonts. https://github.com/harfbuzz/harfbuzz/issues/363 */
B
Behdad Esfahbod 已提交
341
      if (c->glyphs->has (iter.get_glyph ()))
B
Behdad Esfahbod 已提交
342
	(this+sequence[iter.get_coverage ()]).closure (c);
B
Behdad Esfahbod 已提交
343
    }
344 345
  }

346 347
  inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
  {
B
Behdad Esfahbod 已提交
348
    TRACE_COLLECT_GLYPHS (this);
349
    if (unlikely (!(this+coverage).add_coverage (c->input))) return;
350 351 352 353 354
    unsigned int count = sequence.len;
    for (unsigned int i = 0; i < count; i++)
	(this+sequence[i]).collect_glyphs (c);
  }

355 356 357 358 359
  inline const Coverage &get_coverage (void) const
  {
    return this+coverage;
  }

360 361
  inline bool would_apply (hb_would_apply_context_t *c) const
  {
B
Behdad Esfahbod 已提交
362
    TRACE_WOULD_APPLY (this);
B
Behdad Esfahbod 已提交
363
    return_trace (c->len == 1 && (this+coverage).get_coverage (c->glyphs[0]) != NOT_COVERED);
364 365
  }

366
  inline bool apply (hb_ot_apply_context_t *c) const
B
Behdad Esfahbod 已提交
367
  {
B
Behdad Esfahbod 已提交
368
    TRACE_APPLY (this);
369

370
    unsigned int index = (this+coverage).get_coverage (c->buffer->cur().codepoint);
B
Behdad Esfahbod 已提交
371
    if (likely (index == NOT_COVERED)) return_trace (false);
372

B
Behdad Esfahbod 已提交
373
    return_trace ((this+sequence[index]).apply (c));
374
  }
B
Behdad Esfahbod 已提交
375

376
  inline bool serialize (hb_serialize_context_t *c,
377 378
			 Supplier<GlyphID> &glyphs,
			 Supplier<unsigned int> &substitute_len_list,
379
			 unsigned int num_glyphs,
380
			 Supplier<GlyphID> &substitute_glyphs_list)
381
  {
B
Behdad Esfahbod 已提交
382
    TRACE_SERIALIZE (this);
B
Behdad Esfahbod 已提交
383 384
    if (unlikely (!c->extend_min (*this))) return_trace (false);
    if (unlikely (!sequence.serialize (c, num_glyphs))) return_trace (false);
385 386 387
    for (unsigned int i = 0; i < num_glyphs; i++)
      if (unlikely (!sequence[i].serialize (c, this).serialize (c,
								substitute_glyphs_list,
B
Behdad Esfahbod 已提交
388
								substitute_len_list[i]))) return_trace (false);
389
    substitute_len_list += num_glyphs;
B
Behdad Esfahbod 已提交
390 391
    if (unlikely (!coverage.serialize (c, this).serialize (c, glyphs, num_glyphs))) return_trace (false);
    return_trace (true);
392 393
  }

B
Behdad Esfahbod 已提交
394 395
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
B
Behdad Esfahbod 已提交
396
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
397
    return_trace (coverage.sanitize (c, this) && sequence.sanitize (c, this));
B
Behdad Esfahbod 已提交
398 399
  }

400
  protected:
B
Behdad Esfahbod 已提交
401
  HBUINT16	format;			/* Format identifier--format = 1 */
B
Behdad Esfahbod 已提交
402 403
  OffsetTo<Coverage>
		coverage;		/* Offset to Coverage table--from
B
Behdad Esfahbod 已提交
404
					 * beginning of Substitution table */
B
Behdad Esfahbod 已提交
405 406 407
  OffsetArrayOf<Sequence>
		sequence;		/* Array of Sequence tables
					 * ordered by Coverage Index */
408
  public:
409
  DEFINE_SIZE_ARRAY (6, sequence);
B
Behdad Esfahbod 已提交
410
};
411

B
Behdad Esfahbod 已提交
412 413
struct MultipleSubst
{
414
  inline bool serialize (hb_serialize_context_t *c,
415 416
			 Supplier<GlyphID> &glyphs,
			 Supplier<unsigned int> &substitute_len_list,
417
			 unsigned int num_glyphs,
418
			 Supplier<GlyphID> &substitute_glyphs_list)
419
  {
B
Behdad Esfahbod 已提交
420
    TRACE_SERIALIZE (this);
B
Behdad Esfahbod 已提交
421
    if (unlikely (!c->extend_min (u.format))) return_trace (false);
422 423 424
    unsigned int format = 1;
    u.format.set (format);
    switch (u.format) {
B
Behdad Esfahbod 已提交
425 426
    case 1: return_trace (u.format1.serialize (c, glyphs, substitute_len_list, num_glyphs, substitute_glyphs_list));
    default:return_trace (false);
427 428 429
    }
  }

B
Behdad Esfahbod 已提交
430 431 432
  template <typename context_t>
  inline typename context_t::return_t dispatch (context_t *c) const
  {
433
    TRACE_DISPATCH (this, u.format);
434
    if (unlikely (!c->may_dispatch (this, &u.format))) return_trace (c->no_dispatch_return_value ());
B
Behdad Esfahbod 已提交
435
    switch (u.format) {
B
Behdad Esfahbod 已提交
436 437
    case 1: return_trace (c->dispatch (u.format1));
    default:return_trace (c->default_return_value ());
B
Behdad Esfahbod 已提交
438 439 440
    }
  }

441
  protected:
442
  union {
B
Behdad Esfahbod 已提交
443
  HBUINT16		format;		/* Format identifier */
B
Behdad Esfahbod 已提交
444
  MultipleSubstFormat1	format1;
445 446 447
  } u;
};

B
Behdad Esfahbod 已提交
448

B
Behdad Esfahbod 已提交
449
typedef ArrayOf<GlyphID> AlternateSet;	/* Array of alternate GlyphIDs--in
B
Behdad Esfahbod 已提交
450 451
					 * arbitrary order */

B
Behdad Esfahbod 已提交
452 453
struct AlternateSubstFormat1
{
B
Behdad Esfahbod 已提交
454
  inline void closure (hb_closure_context_t *c) const
455
  {
B
Behdad Esfahbod 已提交
456
    TRACE_CLOSURE (this);
B
Behdad Esfahbod 已提交
457
    Coverage::Iter iter;
458 459 460 461
    unsigned int count = alternateSet.len;
    for (iter.init (this+coverage); iter.more (); iter.next ())
    {
      if (unlikely (iter.get_coverage () >= count))
462
        break; /* Work around malicious fonts. https://github.com/harfbuzz/harfbuzz/issues/363 */
B
Behdad Esfahbod 已提交
463 464 465 466
      if (c->glyphs->has (iter.get_glyph ())) {
	const AlternateSet &alt_set = this+alternateSet[iter.get_coverage ()];
	unsigned int count = alt_set.len;
	for (unsigned int i = 0; i < count; i++)
B
Behdad Esfahbod 已提交
467
	  c->glyphs->add (alt_set[i]);
B
Behdad Esfahbod 已提交
468 469
      }
    }
470 471
  }

472 473
  inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
  {
B
Behdad Esfahbod 已提交
474
    TRACE_COLLECT_GLYPHS (this);
475
    if (unlikely (!(this+coverage).add_coverage (c->input))) return;
476
    Coverage::Iter iter;
477 478 479 480
    unsigned int count = alternateSet.len;
    for (iter.init (this+coverage); iter.more (); iter.next ())
    {
      if (unlikely (iter.get_coverage () >= count))
481
        break; /* Work around malicious fonts. https://github.com/harfbuzz/harfbuzz/issues/363 */
482
      const AlternateSet &alt_set = this+alternateSet[iter.get_coverage ()];
483
      c->output->add_array (alt_set.array, alt_set.len);
484 485 486
    }
  }

487 488 489 490 491
  inline const Coverage &get_coverage (void) const
  {
    return this+coverage;
  }

492 493
  inline bool would_apply (hb_would_apply_context_t *c) const
  {
B
Behdad Esfahbod 已提交
494
    TRACE_WOULD_APPLY (this);
B
Behdad Esfahbod 已提交
495
    return_trace (c->len == 1 && (this+coverage).get_coverage (c->glyphs[0]) != NOT_COVERED);
496 497
  }

498
  inline bool apply (hb_ot_apply_context_t *c) const
B
Behdad Esfahbod 已提交
499
  {
B
Behdad Esfahbod 已提交
500
    TRACE_APPLY (this);
501
    hb_codepoint_t glyph_id = c->buffer->cur().codepoint;
502

503
    unsigned int index = (this+coverage).get_coverage (glyph_id);
B
Behdad Esfahbod 已提交
504
    if (likely (index == NOT_COVERED)) return_trace (false);
505

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

B
Behdad Esfahbod 已提交
508
    if (unlikely (!alt_set.len)) return_trace (false);
509

510
    hb_mask_t glyph_mask = c->buffer->cur().mask;
B
Behdad Esfahbod 已提交
511 512
    hb_mask_t lookup_mask = c->lookup_mask;

B
Behdad Esfahbod 已提交
513
    /* Note: This breaks badly if two features enabled this lookup together. */
B
Behdad Esfahbod 已提交
514
    unsigned int shift = _hb_ctz (lookup_mask);
B
Behdad Esfahbod 已提交
515
    unsigned int alt_index = ((lookup_mask & glyph_mask) >> shift);
516

B
Behdad Esfahbod 已提交
517
    if (unlikely (alt_index > alt_set.len || alt_index == 0)) return_trace (false);
518

B
Behdad Esfahbod 已提交
519
    glyph_id = alt_set[alt_index - 1];
520

521
    c->replace_glyph (glyph_id);
522

B
Behdad Esfahbod 已提交
523
    return_trace (true);
524
  }
B
Behdad Esfahbod 已提交
525

526
  inline bool serialize (hb_serialize_context_t *c,
527 528
			 Supplier<GlyphID> &glyphs,
			 Supplier<unsigned int> &alternate_len_list,
529
			 unsigned int num_glyphs,
530
			 Supplier<GlyphID> &alternate_glyphs_list)
531
  {
B
Behdad Esfahbod 已提交
532
    TRACE_SERIALIZE (this);
B
Behdad Esfahbod 已提交
533 534
    if (unlikely (!c->extend_min (*this))) return_trace (false);
    if (unlikely (!alternateSet.serialize (c, num_glyphs))) return_trace (false);
535 536 537
    for (unsigned int i = 0; i < num_glyphs; i++)
      if (unlikely (!alternateSet[i].serialize (c, this).serialize (c,
								    alternate_glyphs_list,
B
Behdad Esfahbod 已提交
538
								    alternate_len_list[i]))) return_trace (false);
539
    alternate_len_list += num_glyphs;
B
Behdad Esfahbod 已提交
540 541
    if (unlikely (!coverage.serialize (c, this).serialize (c, glyphs, num_glyphs))) return_trace (false);
    return_trace (true);
542 543
  }

B
Behdad Esfahbod 已提交
544 545
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
B
Behdad Esfahbod 已提交
546
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
547
    return_trace (coverage.sanitize (c, this) && alternateSet.sanitize (c, this));
B
Behdad Esfahbod 已提交
548 549
  }

550
  protected:
B
Behdad Esfahbod 已提交
551
  HBUINT16	format;			/* Format identifier--format = 1 */
B
Behdad Esfahbod 已提交
552 553
  OffsetTo<Coverage>
		coverage;		/* Offset to Coverage table--from
B
Behdad Esfahbod 已提交
554
					 * beginning of Substitution table */
B
Behdad Esfahbod 已提交
555 556 557
  OffsetArrayOf<AlternateSet>
		alternateSet;		/* Array of AlternateSet tables
					 * ordered by Coverage Index */
558
  public:
559
  DEFINE_SIZE_ARRAY (6, alternateSet);
B
Behdad Esfahbod 已提交
560
};
561

B
Behdad Esfahbod 已提交
562 563
struct AlternateSubst
{
564
  inline bool serialize (hb_serialize_context_t *c,
565 566
			 Supplier<GlyphID> &glyphs,
			 Supplier<unsigned int> &alternate_len_list,
567
			 unsigned int num_glyphs,
568
			 Supplier<GlyphID> &alternate_glyphs_list)
569
  {
B
Behdad Esfahbod 已提交
570
    TRACE_SERIALIZE (this);
B
Behdad Esfahbod 已提交
571
    if (unlikely (!c->extend_min (u.format))) return_trace (false);
572 573 574
    unsigned int format = 1;
    u.format.set (format);
    switch (u.format) {
B
Behdad Esfahbod 已提交
575 576
    case 1: return_trace (u.format1.serialize (c, glyphs, alternate_len_list, num_glyphs, alternate_glyphs_list));
    default:return_trace (false);
577 578 579
    }
  }

B
Behdad Esfahbod 已提交
580 581 582
  template <typename context_t>
  inline typename context_t::return_t dispatch (context_t *c) const
  {
583
    TRACE_DISPATCH (this, u.format);
584
    if (unlikely (!c->may_dispatch (this, &u.format))) return_trace (c->no_dispatch_return_value ());
B
Behdad Esfahbod 已提交
585
    switch (u.format) {
B
Behdad Esfahbod 已提交
586 587
    case 1: return_trace (c->dispatch (u.format1));
    default:return_trace (c->default_return_value ());
B
Behdad Esfahbod 已提交
588 589 590
    }
  }

591
  protected:
592
  union {
B
Behdad Esfahbod 已提交
593
  HBUINT16		format;		/* Format identifier */
B
Behdad Esfahbod 已提交
594
  AlternateSubstFormat1	format1;
595 596 597
  } u;
};

598

B
Behdad Esfahbod 已提交
599 600
struct Ligature
{
B
Behdad Esfahbod 已提交
601
  inline void closure (hb_closure_context_t *c) const
602
  {
B
Behdad Esfahbod 已提交
603
    TRACE_CLOSURE (this);
B
Behdad Esfahbod 已提交
604 605 606
    unsigned int count = component.len;
    for (unsigned int i = 1; i < count; i++)
      if (!c->glyphs->has (component[i]))
B
Behdad Esfahbod 已提交
607 608
        return;
    c->glyphs->add (ligGlyph);
609 610
  }

611 612
  inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
  {
B
Behdad Esfahbod 已提交
613
    TRACE_COLLECT_GLYPHS (this);
614
    c->input->add_array (component.array, component.len ? component.len - 1 : 0);
B
Minor  
Behdad Esfahbod 已提交
615
    c->output->add (ligGlyph);
616 617
  }

618
  inline bool would_apply (hb_would_apply_context_t *c) const
B
Behdad Esfahbod 已提交
619
  {
B
Behdad Esfahbod 已提交
620
    TRACE_WOULD_APPLY (this);
621
    if (c->len != component.len)
B
Behdad Esfahbod 已提交
622
      return_trace (false);
623 624 625

    for (unsigned int i = 1; i < c->len; i++)
      if (likely (c->glyphs[i] != component[i]))
B
Behdad Esfahbod 已提交
626
	return_trace (false);
627

B
Behdad Esfahbod 已提交
628
    return_trace (true);
B
Behdad Esfahbod 已提交
629 630
  }

631
  inline bool apply (hb_ot_apply_context_t *c) const
B
Behdad Esfahbod 已提交
632
  {
B
Behdad Esfahbod 已提交
633
    TRACE_APPLY (this);
B
Behdad Esfahbod 已提交
634
    unsigned int count = component.len;
B
Behdad Esfahbod 已提交
635

B
Behdad Esfahbod 已提交
636
    if (unlikely (!count)) return_trace (false);
B
Behdad Esfahbod 已提交
637

638 639 640 641 642
    /* Special-case to make it in-place and not consider this
     * as a "ligated" substitution. */
    if (unlikely (count == 1))
    {
      c->replace_glyph (ligGlyph);
B
Behdad Esfahbod 已提交
643
      return_trace (true);
644 645
    }

B
Behdad Esfahbod 已提交
646 647
    bool is_mark_ligature = false;
    unsigned int total_component_count = 0;
648

649
    unsigned int match_length = 0;
650
    unsigned int match_positions[HB_MAX_CONTEXT_LENGTH];
651

652 653 654
    if (likely (!match_input (c, count,
			      &component[1],
			      match_glyph,
B
Behdad Esfahbod 已提交
655
			      nullptr,
656 657
			      &match_length,
			      match_positions,
658 659
			      &is_mark_ligature,
			      &total_component_count)))
B
Behdad Esfahbod 已提交
660
      return_trace (false);
661

662 663
    ligate_input (c,
		  count,
664 665
		  match_positions,
		  match_length,
666
		  ligGlyph,
667 668
		  is_mark_ligature,
		  total_component_count);
669

B
Behdad Esfahbod 已提交
670
    return_trace (true);
671
  }
672

673 674 675 676 677
  inline bool serialize (hb_serialize_context_t *c,
			 GlyphID ligature,
			 Supplier<GlyphID> &components, /* Starting from second */
			 unsigned int num_components /* Including first component */)
  {
B
Behdad Esfahbod 已提交
678
    TRACE_SERIALIZE (this);
B
Behdad Esfahbod 已提交
679
    if (unlikely (!c->extend_min (*this))) return_trace (false);
680
    ligGlyph = ligature;
B
Behdad Esfahbod 已提交
681 682
    if (unlikely (!component.serialize (c, components, num_components))) return_trace (false);
    return_trace (true);
683 684
  }

B
Behdad Esfahbod 已提交
685
  public:
B
Behdad Esfahbod 已提交
686 687
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
B
Behdad Esfahbod 已提交
688
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
689
    return_trace (ligGlyph.sanitize (c) && component.sanitize (c));
B
Behdad Esfahbod 已提交
690 691
  }

692
  protected:
693
  GlyphID	ligGlyph;		/* GlyphID of ligature to substitute */
B
Behdad Esfahbod 已提交
694 695
  HeadlessArrayOf<GlyphID>
		component;		/* Array of component GlyphIDs--start
696 697
					 * with the second  component--ordered
					 * in writing direction */
698
  public:
699
  DEFINE_SIZE_ARRAY (4, component);
700
};
B
Behdad Esfahbod 已提交
701

B
Behdad Esfahbod 已提交
702 703
struct LigatureSet
{
B
Behdad Esfahbod 已提交
704
  inline void closure (hb_closure_context_t *c) const
705
  {
B
Behdad Esfahbod 已提交
706
    TRACE_CLOSURE (this);
B
Behdad Esfahbod 已提交
707 708
    unsigned int num_ligs = ligature.len;
    for (unsigned int i = 0; i < num_ligs; i++)
B
Behdad Esfahbod 已提交
709
      (this+ligature[i]).closure (c);
710 711
  }

712 713
  inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
  {
B
Behdad Esfahbod 已提交
714
    TRACE_COLLECT_GLYPHS (this);
715 716 717 718 719
    unsigned int num_ligs = ligature.len;
    for (unsigned int i = 0; i < num_ligs; i++)
      (this+ligature[i]).collect_glyphs (c);
  }

720
  inline bool would_apply (hb_would_apply_context_t *c) const
B
Behdad Esfahbod 已提交
721
  {
B
Behdad Esfahbod 已提交
722
    TRACE_WOULD_APPLY (this);
B
Behdad Esfahbod 已提交
723 724 725 726
    unsigned int num_ligs = ligature.len;
    for (unsigned int i = 0; i < num_ligs; i++)
    {
      const Ligature &lig = this+ligature[i];
727
      if (lig.would_apply (c))
B
Behdad Esfahbod 已提交
728
        return_trace (true);
B
Behdad Esfahbod 已提交
729
    }
B
Behdad Esfahbod 已提交
730
    return_trace (false);
B
Behdad Esfahbod 已提交
731 732
  }

733
  inline bool apply (hb_ot_apply_context_t *c) const
B
Behdad Esfahbod 已提交
734
  {
B
Behdad Esfahbod 已提交
735
    TRACE_APPLY (this);
B
Behdad Esfahbod 已提交
736
    unsigned int num_ligs = ligature.len;
B
Behdad Esfahbod 已提交
737 738
    for (unsigned int i = 0; i < num_ligs; i++)
    {
B
Behdad Esfahbod 已提交
739
      const Ligature &lig = this+ligature[i];
B
Behdad Esfahbod 已提交
740
      if (lig.apply (c)) return_trace (true);
741 742
    }

B
Behdad Esfahbod 已提交
743
    return_trace (false);
744
  }
B
Behdad Esfahbod 已提交
745

746 747 748 749 750 751
  inline bool serialize (hb_serialize_context_t *c,
			 Supplier<GlyphID> &ligatures,
			 Supplier<unsigned int> &component_count_list,
			 unsigned int num_ligatures,
			 Supplier<GlyphID> &component_list /* Starting from second for each ligature */)
  {
B
Behdad Esfahbod 已提交
752
    TRACE_SERIALIZE (this);
B
Behdad Esfahbod 已提交
753 754
    if (unlikely (!c->extend_min (*this))) return_trace (false);
    if (unlikely (!ligature.serialize (c, num_ligatures))) return_trace (false);
755 756 757 758
    for (unsigned int i = 0; i < num_ligatures; i++)
      if (unlikely (!ligature[i].serialize (c, this).serialize (c,
								ligatures[i],
								component_list,
B
Behdad Esfahbod 已提交
759
								component_count_list[i]))) return_trace (false);
760 761
    ligatures += num_ligatures;
    component_count_list += num_ligatures;
B
Behdad Esfahbod 已提交
762
    return_trace (true);
763 764
  }

B
Behdad Esfahbod 已提交
765 766
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
B
Behdad Esfahbod 已提交
767
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
768
    return_trace (ligature.sanitize (c, this));
B
Behdad Esfahbod 已提交
769 770
  }

771
  protected:
B
Behdad Esfahbod 已提交
772 773 774
  OffsetArrayOf<Ligature>
		ligature;		/* Array LigatureSet tables
					 * ordered by preference */
775
  public:
776
  DEFINE_SIZE_ARRAY (2, ligature);
B
Behdad Esfahbod 已提交
777 778
};

B
Behdad Esfahbod 已提交
779 780
struct LigatureSubstFormat1
{
B
Behdad Esfahbod 已提交
781
  inline void closure (hb_closure_context_t *c) const
782
  {
B
Behdad Esfahbod 已提交
783
    TRACE_CLOSURE (this);
B
Behdad Esfahbod 已提交
784
    Coverage::Iter iter;
785 786 787 788
    unsigned int count = ligatureSet.len;
    for (iter.init (this+coverage); iter.more (); iter.next ())
    {
      if (unlikely (iter.get_coverage () >= count))
789
        break; /* Work around malicious fonts. https://github.com/harfbuzz/harfbuzz/issues/363 */
B
Behdad Esfahbod 已提交
790
      if (c->glyphs->has (iter.get_glyph ()))
B
Behdad Esfahbod 已提交
791
	(this+ligatureSet[iter.get_coverage ()]).closure (c);
B
Behdad Esfahbod 已提交
792
    }
793 794
  }

795 796
  inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
  {
B
Behdad Esfahbod 已提交
797
    TRACE_COLLECT_GLYPHS (this);
798
    if (unlikely (!(this+coverage).add_coverage (c->input))) return;
799
    Coverage::Iter iter;
800 801 802 803
    unsigned int count = ligatureSet.len;
    for (iter.init (this+coverage); iter.more (); iter.next ())
    {
      if (unlikely (iter.get_coverage () >= count))
804
        break; /* Work around malicious fonts. https://github.com/harfbuzz/harfbuzz/issues/363 */
805 806 807 808
      (this+ligatureSet[iter.get_coverage ()]).collect_glyphs (c);
    }
  }

809 810 811 812 813
  inline const Coverage &get_coverage (void) const
  {
    return this+coverage;
  }

814
  inline bool would_apply (hb_would_apply_context_t *c) const
B
Behdad Esfahbod 已提交
815
  {
B
Behdad Esfahbod 已提交
816
    TRACE_WOULD_APPLY (this);
817
    unsigned int index = (this+coverage).get_coverage (c->glyphs[0]);
B
Behdad Esfahbod 已提交
818
    if (likely (index == NOT_COVERED)) return_trace (false);
819 820

    const LigatureSet &lig_set = this+ligatureSet[index];
B
Behdad Esfahbod 已提交
821
    return_trace (lig_set.would_apply (c));
B
Behdad Esfahbod 已提交
822 823
  }

824
  inline bool apply (hb_ot_apply_context_t *c) const
B
Behdad Esfahbod 已提交
825
  {
B
Behdad Esfahbod 已提交
826
    TRACE_APPLY (this);
827
    hb_codepoint_t glyph_id = c->buffer->cur().codepoint;
828

829
    unsigned int index = (this+coverage).get_coverage (glyph_id);
B
Behdad Esfahbod 已提交
830
    if (likely (index == NOT_COVERED)) return_trace (false);
831

B
Behdad Esfahbod 已提交
832
    const LigatureSet &lig_set = this+ligatureSet[index];
B
Behdad Esfahbod 已提交
833
    return_trace (lig_set.apply (c));
834
  }
B
Behdad Esfahbod 已提交
835

836 837 838 839 840 841 842 843
  inline bool serialize (hb_serialize_context_t *c,
			 Supplier<GlyphID> &first_glyphs,
			 Supplier<unsigned int> &ligature_per_first_glyph_count_list,
			 unsigned int num_first_glyphs,
			 Supplier<GlyphID> &ligatures_list,
			 Supplier<unsigned int> &component_count_list,
			 Supplier<GlyphID> &component_list /* Starting from second for each ligature */)
  {
B
Behdad Esfahbod 已提交
844
    TRACE_SERIALIZE (this);
B
Behdad Esfahbod 已提交
845 846
    if (unlikely (!c->extend_min (*this))) return_trace (false);
    if (unlikely (!ligatureSet.serialize (c, num_first_glyphs))) return_trace (false);
847 848 849 850 851
    for (unsigned int i = 0; i < num_first_glyphs; i++)
      if (unlikely (!ligatureSet[i].serialize (c, this).serialize (c,
								   ligatures_list,
								   component_count_list,
								   ligature_per_first_glyph_count_list[i],
B
Behdad Esfahbod 已提交
852
								   component_list))) return_trace (false);
853
    ligature_per_first_glyph_count_list += num_first_glyphs;
B
Behdad Esfahbod 已提交
854 855
    if (unlikely (!coverage.serialize (c, this).serialize (c, first_glyphs, num_first_glyphs))) return_trace (false);
    return_trace (true);
856 857
  }

B
Behdad Esfahbod 已提交
858 859
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
B
Behdad Esfahbod 已提交
860
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
861
    return_trace (coverage.sanitize (c, this) && ligatureSet.sanitize (c, this));
B
Behdad Esfahbod 已提交
862 863
  }

864
  protected:
B
Behdad Esfahbod 已提交
865
  HBUINT16	format;			/* Format identifier--format = 1 */
B
Behdad Esfahbod 已提交
866 867
  OffsetTo<Coverage>
		coverage;		/* Offset to Coverage table--from
868
					 * beginning of Substitution table */
B
Behdad Esfahbod 已提交
869
  OffsetArrayOf<LigatureSet>
B
Behdad Esfahbod 已提交
870 871
		ligatureSet;		/* Array LigatureSet tables
					 * ordered by Coverage Index */
872
  public:
873
  DEFINE_SIZE_ARRAY (6, ligatureSet);
B
Behdad Esfahbod 已提交
874
};
875

B
Behdad Esfahbod 已提交
876 877
struct LigatureSubst
{
878 879 880 881 882 883 884 885
  inline bool serialize (hb_serialize_context_t *c,
			 Supplier<GlyphID> &first_glyphs,
			 Supplier<unsigned int> &ligature_per_first_glyph_count_list,
			 unsigned int num_first_glyphs,
			 Supplier<GlyphID> &ligatures_list,
			 Supplier<unsigned int> &component_count_list,
			 Supplier<GlyphID> &component_list /* Starting from second for each ligature */)
  {
B
Behdad Esfahbod 已提交
886
    TRACE_SERIALIZE (this);
B
Behdad Esfahbod 已提交
887
    if (unlikely (!c->extend_min (u.format))) return_trace (false);
888 889 890
    unsigned int format = 1;
    u.format.set (format);
    switch (u.format) {
B
Behdad Esfahbod 已提交
891 892 893 894 895 896 897 898
    case 1: return_trace (u.format1.serialize (c,
					       first_glyphs,
					       ligature_per_first_glyph_count_list,
					       num_first_glyphs,
					       ligatures_list,
					       component_count_list,
					       component_list));
    default:return_trace (false);
899 900 901
    }
  }

B
Behdad Esfahbod 已提交
902 903 904
  template <typename context_t>
  inline typename context_t::return_t dispatch (context_t *c) const
  {
905
    TRACE_DISPATCH (this, u.format);
906
    if (unlikely (!c->may_dispatch (this, &u.format))) return_trace (c->no_dispatch_return_value ());
B
Behdad Esfahbod 已提交
907
    switch (u.format) {
B
Behdad Esfahbod 已提交
908 909
    case 1: return_trace (c->dispatch (u.format1));
    default:return_trace (c->default_return_value ());
B
Behdad Esfahbod 已提交
910 911 912
    }
  }

913
  protected:
914
  union {
B
Behdad Esfahbod 已提交
915
  HBUINT16		format;		/* Format identifier */
B
Behdad Esfahbod 已提交
916
  LigatureSubstFormat1	format1;
917 918 919
  } u;
};

B
Behdad Esfahbod 已提交
920

B
Minor  
Behdad Esfahbod 已提交
921
struct ContextSubst : Context {};
B
Behdad Esfahbod 已提交
922

B
Minor  
Behdad Esfahbod 已提交
923
struct ChainContextSubst : ChainContext {};
924

B
Behdad Esfahbod 已提交
925
struct ExtensionSubst : Extension<ExtensionSubst>
B
Behdad Esfahbod 已提交
926
{
927
  typedef struct SubstLookupSubTable LookupSubTable;
B
Behdad Esfahbod 已提交
928

B
Behdad Esfahbod 已提交
929
  inline bool is_reverse (void) const;
930 931 932
};


B
Behdad Esfahbod 已提交
933 934
struct ReverseChainSingleSubstFormat1
{
B
Behdad Esfahbod 已提交
935
  inline void closure (hb_closure_context_t *c) const
936
  {
B
Behdad Esfahbod 已提交
937
    TRACE_CLOSURE (this);
B
Behdad Esfahbod 已提交
938 939 940 941 942 943 944
    const OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (backtrack);

    unsigned int count;

    count = backtrack.len;
    for (unsigned int i = 0; i < count; i++)
      if (!(this+backtrack[i]).intersects (c->glyphs))
B
Behdad Esfahbod 已提交
945
        return;
B
Behdad Esfahbod 已提交
946 947 948 949

    count = lookahead.len;
    for (unsigned int i = 0; i < count; i++)
      if (!(this+lookahead[i]).intersects (c->glyphs))
B
Behdad Esfahbod 已提交
950
        return;
B
Behdad Esfahbod 已提交
951 952 953

    const ArrayOf<GlyphID> &substitute = StructAfter<ArrayOf<GlyphID> > (lookahead);
    Coverage::Iter iter;
954 955 956 957
    count = substitute.len;
    for (iter.init (this+coverage); iter.more (); iter.next ())
    {
      if (unlikely (iter.get_coverage () >= count))
958
        break; /* Work around malicious fonts. https://github.com/harfbuzz/harfbuzz/issues/363 */
B
Behdad Esfahbod 已提交
959
      if (c->glyphs->has (iter.get_glyph ()))
B
Behdad Esfahbod 已提交
960
	c->glyphs->add (substitute[iter.get_coverage ()]);
B
Behdad Esfahbod 已提交
961
    }
962 963
  }

964 965
  inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
  {
B
Behdad Esfahbod 已提交
966
    TRACE_COLLECT_GLYPHS (this);
967
    if (unlikely (!(this+coverage).add_coverage (c->input))) return;
968 969 970 971 972

    unsigned int count;

    count = backtrack.len;
    for (unsigned int i = 0; i < count; i++)
973
      if (unlikely (!(this+backtrack[i]).add_coverage (c->before))) return;
974

975
    const OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
976 977
    count = lookahead.len;
    for (unsigned int i = 0; i < count; i++)
978
      if (unlikely (!(this+lookahead[i]).add_coverage (c->after))) return;
979 980 981

    const ArrayOf<GlyphID> &substitute = StructAfter<ArrayOf<GlyphID> > (lookahead);
    count = substitute.len;
982
    c->output->add_array (substitute.array, substitute.len);
983 984
  }

985 986 987 988 989
  inline const Coverage &get_coverage (void) const
  {
    return this+coverage;
  }

990 991
  inline bool would_apply (hb_would_apply_context_t *c) const
  {
B
Behdad Esfahbod 已提交
992
    TRACE_WOULD_APPLY (this);
B
Behdad Esfahbod 已提交
993
    return_trace (c->len == 1 && (this+coverage).get_coverage (c->glyphs[0]) != NOT_COVERED);
994 995
  }

996
  inline bool apply (hb_ot_apply_context_t *c) const
B
Behdad Esfahbod 已提交
997
  {
B
Behdad Esfahbod 已提交
998
    TRACE_APPLY (this);
999
    if (unlikely (c->nesting_level_left != HB_MAX_NESTING_LEVEL))
B
Behdad Esfahbod 已提交
1000
      return_trace (false); /* No chaining to this type */
1001

1002
    unsigned int index = (this+coverage).get_coverage (c->buffer->cur().codepoint);
B
Behdad Esfahbod 已提交
1003
    if (likely (index == NOT_COVERED)) return_trace (false);
1004

1005 1006
    const OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
    const ArrayOf<GlyphID> &substitute = StructAfter<ArrayOf<GlyphID> > (lookahead);
1007

1008
  unsigned int start_index = 0, end_index = 0;
B
Behdad Esfahbod 已提交
1009
    if (match_backtrack (c,
B
Behdad Esfahbod 已提交
1010
			 backtrack.len, (HBUINT16 *) backtrack.array,
1011 1012
			 match_coverage, this,
			 &start_index) &&
B
Behdad Esfahbod 已提交
1013
        match_lookahead (c,
B
Behdad Esfahbod 已提交
1014
			 lookahead.len, (HBUINT16 *) lookahead.array,
B
Behdad Esfahbod 已提交
1015
			 match_coverage, this,
1016
			 1, &end_index))
1017
    {
1018
      c->buffer->unsafe_to_break_from_outbuffer (start_index, end_index);
B
Behdad Esfahbod 已提交
1019
      c->replace_glyph_inplace (substitute[index]);
1020 1021 1022
      /* Note: We DON'T decrease buffer->idx.  The main loop does it
       * for us.  This is useful for preventing surprises if someone
       * calls us through a Context lookup. */
B
Behdad Esfahbod 已提交
1023
      return_trace (true);
1024 1025
    }

B
Behdad Esfahbod 已提交
1026
    return_trace (false);
1027
  }
B
Behdad Esfahbod 已提交
1028

B
Behdad Esfahbod 已提交
1029 1030
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
B
Behdad Esfahbod 已提交
1031
    TRACE_SANITIZE (this);
1032
    if (!(coverage.sanitize (c, this) && backtrack.sanitize (c, this)))
B
Behdad Esfahbod 已提交
1033
      return_trace (false);
B
Behdad Esfahbod 已提交
1034
    const OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
B
Behdad Esfahbod 已提交
1035
    if (!lookahead.sanitize (c, this))
B
Behdad Esfahbod 已提交
1036
      return_trace (false);
B
Behdad Esfahbod 已提交
1037
    const ArrayOf<GlyphID> &substitute = StructAfter<ArrayOf<GlyphID> > (lookahead);
B
Behdad Esfahbod 已提交
1038
    return_trace (substitute.sanitize (c));
B
Behdad Esfahbod 已提交
1039 1040
  }

1041
  protected:
B
Behdad Esfahbod 已提交
1042
  HBUINT16	format;			/* Format identifier--format = 1 */
1043 1044 1045 1046 1047
  OffsetTo<Coverage>
		coverage;		/* Offset to Coverage table--from
					 * beginning of table */
  OffsetArrayOf<Coverage>
		backtrack;		/* Array of coverage tables
B
Behdad Esfahbod 已提交
1048 1049
					 * in backtracking sequence, in  glyph
					 * sequence order */
1050 1051 1052
  OffsetArrayOf<Coverage>
		lookaheadX;		/* Array of coverage tables
					 * in lookahead sequence, in glyph
B
Behdad Esfahbod 已提交
1053
					 * sequence order */
1054 1055 1056
  ArrayOf<GlyphID>
		substituteX;		/* Array of substitute
					 * GlyphIDs--ordered by Coverage Index */
1057
  public:
B
Behdad Esfahbod 已提交
1058
  DEFINE_SIZE_MIN (10);
B
Behdad Esfahbod 已提交
1059 1060
};

B
Behdad Esfahbod 已提交
1061 1062
struct ReverseChainSingleSubst
{
1063
  template <typename context_t>
1064
  inline typename context_t::return_t dispatch (context_t *c) const
1065
  {
1066
    TRACE_DISPATCH (this, u.format);
1067
    if (unlikely (!c->may_dispatch (this, &u.format))) return_trace (c->no_dispatch_return_value ());
1068
    switch (u.format) {
B
Behdad Esfahbod 已提交
1069 1070
    case 1: return_trace (c->dispatch (u.format1));
    default:return_trace (c->default_return_value ());
1071 1072 1073
    }
  }

1074
  protected:
1075
  union {
B
Behdad Esfahbod 已提交
1076
  HBUINT16				format;		/* Format identifier */
B
Behdad Esfahbod 已提交
1077
  ReverseChainSingleSubstFormat1	format1;
1078 1079 1080 1081 1082
  } u;
};



B
Behdad Esfahbod 已提交
1083 1084 1085 1086
/*
 * SubstLookup
 */

B
Behdad Esfahbod 已提交
1087 1088
struct SubstLookupSubTable
{
B
Behdad Esfahbod 已提交
1089 1090
  friend struct SubstLookup;

B
Behdad Esfahbod 已提交
1091
  enum Type {
1092 1093 1094 1095 1096 1097 1098
    Single		= 1,
    Multiple		= 2,
    Alternate		= 3,
    Ligature		= 4,
    Context		= 5,
    ChainContext	= 6,
    Extension		= 7,
1099
    ReverseChainSingle	= 8
1100 1101
  };

1102
  template <typename context_t>
1103
  inline typename context_t::return_t dispatch (context_t *c, unsigned int lookup_type) const
1104
  {
1105
    TRACE_DISPATCH (this, lookup_type);
1106
    if (unlikely (!c->may_dispatch (this, &u.sub_format))) return_trace (c->no_dispatch_return_value ());
1107
    switch (lookup_type) {
B
Behdad Esfahbod 已提交
1108 1109 1110 1111 1112 1113 1114 1115 1116
    case Single:		return_trace (u.single.dispatch (c));
    case Multiple:		return_trace (u.multiple.dispatch (c));
    case Alternate:		return_trace (u.alternate.dispatch (c));
    case Ligature:		return_trace (u.ligature.dispatch (c));
    case Context:		return_trace (u.context.dispatch (c));
    case ChainContext:		return_trace (u.chainContext.dispatch (c));
    case Extension:		return_trace (u.extension.dispatch (c));
    case ReverseChainSingle:	return_trace (u.reverseChainContextSingle.dispatch (c));
    default:			return_trace (c->default_return_value ());
1117 1118 1119
    }
  }

1120
  protected:
B
Behdad Esfahbod 已提交
1121
  union {
B
Behdad Esfahbod 已提交
1122
  HBUINT16			sub_format;
B
Behdad Esfahbod 已提交
1123 1124 1125 1126
  SingleSubst			single;
  MultipleSubst			multiple;
  AlternateSubst		alternate;
  LigatureSubst			ligature;
1127
  ContextSubst			context;
B
Behdad Esfahbod 已提交
1128 1129 1130
  ChainContextSubst		chainContext;
  ExtensionSubst		extension;
  ReverseChainSingleSubst	reverseChainContextSingle;
B
Behdad Esfahbod 已提交
1131
  } u;
B
Behdad Esfahbod 已提交
1132
  public:
B
Minor  
Behdad Esfahbod 已提交
1133
  DEFINE_SIZE_UNION (2, sub_format);
B
Behdad Esfahbod 已提交
1134 1135
};

1136

B
Behdad Esfahbod 已提交
1137 1138
struct SubstLookup : Lookup
{
1139
  inline const SubstLookupSubTable& get_subtable (unsigned int i) const
1140
  { return Lookup::get_subtable<SubstLookupSubTable> (i); }
B
Behdad Esfahbod 已提交
1141

B
Behdad Esfahbod 已提交
1142
  inline static bool lookup_type_is_reverse (unsigned int lookup_type)
1143
  { return lookup_type == SubstLookupSubTable::ReverseChainSingle; }
B
Behdad Esfahbod 已提交
1144 1145

  inline bool is_reverse (void) const
B
Behdad Esfahbod 已提交
1146
  {
B
Behdad Esfahbod 已提交
1147
    unsigned int type = get_type ();
1148
    if (unlikely (type == SubstLookupSubTable::Extension))
1149
      return CastR<ExtensionSubst> (get_subtable(0)).is_reverse ();
B
Behdad Esfahbod 已提交
1150
    return lookup_type_is_reverse (type);
B
Behdad Esfahbod 已提交
1151
  }
1152

1153
  inline bool apply (hb_ot_apply_context_t *c) const
B
Behdad Esfahbod 已提交
1154 1155
  {
    TRACE_APPLY (this);
B
Behdad Esfahbod 已提交
1156
    return_trace (dispatch (c));
B
Behdad Esfahbod 已提交
1157 1158
  }

1159 1160
  inline hb_closure_context_t::return_t closure (hb_closure_context_t *c) const
  {
B
Behdad Esfahbod 已提交
1161
    TRACE_CLOSURE (this);
1162
    c->set_recurse_func (dispatch_recurse_func<hb_closure_context_t>);
B
Behdad Esfahbod 已提交
1163
    return_trace (dispatch (c));
B
Behdad Esfahbod 已提交
1164 1165
  }

B
Behdad Esfahbod 已提交
1166
  inline hb_collect_glyphs_context_t::return_t collect_glyphs (hb_collect_glyphs_context_t *c) const
B
Behdad Esfahbod 已提交
1167 1168
  {
    TRACE_COLLECT_GLYPHS (this);
1169
    c->set_recurse_func (dispatch_recurse_func<hb_collect_glyphs_context_t>);
B
Behdad Esfahbod 已提交
1170
    return_trace (dispatch (c));
1171 1172
  }

B
Behdad Esfahbod 已提交
1173 1174 1175
  template <typename set_t>
  inline void add_coverage (set_t *glyphs) const
  {
1176 1177
    hb_add_coverage_context_t<set_t> c (glyphs);
    dispatch (&c);
B
Behdad Esfahbod 已提交
1178 1179
  }

1180 1181
  inline bool would_apply (hb_would_apply_context_t *c,
			   const hb_ot_layout_lookup_accelerator_t *accel) const
B
Behdad Esfahbod 已提交
1182
  {
B
Behdad Esfahbod 已提交
1183
    TRACE_WOULD_APPLY (this);
B
Behdad Esfahbod 已提交
1184 1185 1186
    if (unlikely (!c->len))  return_trace (false);
    if (!accel->may_have (c->glyphs[0]))  return_trace (false);
      return_trace (dispatch (c));
B
Behdad Esfahbod 已提交
1187 1188
  }

1189
  static bool apply_recurse_func (hb_ot_apply_context_t *c, unsigned int lookup_index);
B
Behdad Esfahbod 已提交
1190

1191 1192
  inline SubstLookupSubTable& serialize_subtable (hb_serialize_context_t *c,
						  unsigned int i)
1193
  { return get_subtables<SubstLookupSubTable> ()[i].serialize (c, this); }
1194 1195 1196 1197 1198 1199 1200

  inline bool serialize_single (hb_serialize_context_t *c,
				uint32_t lookup_props,
			        Supplier<GlyphID> &glyphs,
			        Supplier<GlyphID> &substitutes,
			        unsigned int num_glyphs)
  {
B
Behdad Esfahbod 已提交
1201
    TRACE_SERIALIZE (this);
B
Behdad Esfahbod 已提交
1202 1203
    if (unlikely (!Lookup::serialize (c, SubstLookupSubTable::Single, lookup_props, 1))) return_trace (false);
    return_trace (serialize_subtable (c, 0).u.single.serialize (c, glyphs, substitutes, num_glyphs));
1204 1205 1206 1207 1208 1209 1210 1211 1212
  }

  inline bool serialize_multiple (hb_serialize_context_t *c,
				  uint32_t lookup_props,
				  Supplier<GlyphID> &glyphs,
				  Supplier<unsigned int> &substitute_len_list,
				  unsigned int num_glyphs,
				  Supplier<GlyphID> &substitute_glyphs_list)
  {
B
Behdad Esfahbod 已提交
1213
    TRACE_SERIALIZE (this);
B
Behdad Esfahbod 已提交
1214 1215 1216 1217 1218 1219
    if (unlikely (!Lookup::serialize (c, SubstLookupSubTable::Multiple, lookup_props, 1))) return_trace (false);
    return_trace (serialize_subtable (c, 0).u.multiple.serialize (c,
								  glyphs,
								  substitute_len_list,
								  num_glyphs,
								  substitute_glyphs_list));
1220 1221 1222 1223 1224 1225 1226 1227 1228
  }

  inline bool serialize_alternate (hb_serialize_context_t *c,
				   uint32_t lookup_props,
				   Supplier<GlyphID> &glyphs,
				   Supplier<unsigned int> &alternate_len_list,
				   unsigned int num_glyphs,
				   Supplier<GlyphID> &alternate_glyphs_list)
  {
B
Behdad Esfahbod 已提交
1229
    TRACE_SERIALIZE (this);
B
Behdad Esfahbod 已提交
1230 1231 1232 1233 1234 1235
    if (unlikely (!Lookup::serialize (c, SubstLookupSubTable::Alternate, lookup_props, 1))) return_trace (false);
    return_trace (serialize_subtable (c, 0).u.alternate.serialize (c,
								   glyphs,
								   alternate_len_list,
								   num_glyphs,
								   alternate_glyphs_list));
1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246
  }

  inline bool serialize_ligature (hb_serialize_context_t *c,
				  uint32_t lookup_props,
				  Supplier<GlyphID> &first_glyphs,
				  Supplier<unsigned int> &ligature_per_first_glyph_count_list,
				  unsigned int num_first_glyphs,
				  Supplier<GlyphID> &ligatures_list,
				  Supplier<unsigned int> &component_count_list,
				  Supplier<GlyphID> &component_list /* Starting from second for each ligature */)
  {
B
Behdad Esfahbod 已提交
1247
    TRACE_SERIALIZE (this);
B
Behdad Esfahbod 已提交
1248 1249 1250 1251 1252 1253 1254 1255
    if (unlikely (!Lookup::serialize (c, SubstLookupSubTable::Ligature, lookup_props, 1))) return_trace (false);
    return_trace (serialize_subtable (c, 0).u.ligature.serialize (c,
								  first_glyphs,
								  ligature_per_first_glyph_count_list,
								  num_first_glyphs,
								  ligatures_list,
								  component_count_list,
								  component_list));
1256 1257
  }

B
Behdad Esfahbod 已提交
1258 1259 1260 1261 1262
  template <typename context_t>
  static inline typename context_t::return_t dispatch_recurse_func (context_t *c, unsigned int lookup_index);

  template <typename context_t>
  inline typename context_t::return_t dispatch (context_t *c) const
1263
  { return Lookup::dispatch<SubstLookupSubTable> (c); }
B
Behdad Esfahbod 已提交
1264

B
Behdad Esfahbod 已提交
1265
  inline bool sanitize (hb_sanitize_context_t *c) const
1266
  {
B
Behdad Esfahbod 已提交
1267
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
1268 1269
    if (unlikely (!Lookup::sanitize (c))) return_trace (false);
    if (unlikely (!dispatch (c))) return_trace (false);
B
Behdad Esfahbod 已提交
1270 1271 1272 1273

    if (unlikely (get_type () == SubstLookupSubTable::Extension))
    {
      /* The spec says all subtables of an Extension lookup should
1274
       * have the same type, which shall not be the Extension type
1275 1276
       * itself (but we already checked for that).
       * This is specially important if one has a reverse type! */
B
Behdad Esfahbod 已提交
1277 1278 1279 1280
      unsigned int type = get_subtable (0).u.extension.get_type ();
      unsigned int count = get_subtable_count ();
      for (unsigned int i = 1; i < count; i++)
        if (get_subtable (i).u.extension.get_type () != type)
B
Behdad Esfahbod 已提交
1281
	  return_trace (false);
B
Behdad Esfahbod 已提交
1282
    }
B
Behdad Esfahbod 已提交
1283
    return_trace (true);
B
Behdad Esfahbod 已提交
1284
  }
B
Behdad Esfahbod 已提交
1285 1286
};

B
Behdad Esfahbod 已提交
1287
typedef OffsetListOf<SubstLookup> SubstLookupList;
1288

B
Minor  
Behdad Esfahbod 已提交
1289
/*
B
Behdad Esfahbod 已提交
1290
 * GSUB -- The Glyph Substitution Table
B
Minor  
Behdad Esfahbod 已提交
1291 1292
 */

B
Behdad Esfahbod 已提交
1293 1294
struct GSUB : GSUBGPOS
{
1295
  static const hb_tag_t tableTag	= HB_OT_TAG_GSUB;
B
Minor  
Behdad Esfahbod 已提交
1296

B
Behdad Esfahbod 已提交
1297
  inline const SubstLookup& get_lookup (unsigned int i) const
1298
  { return CastR<SubstLookup> (GSUBGPOS::get_lookup (i)); }
B
Behdad Esfahbod 已提交
1299

1300
  static inline void substitute_start (hb_font_t *font, hb_buffer_t *buffer);
B
Behdad Esfahbod 已提交
1301

B
Behdad Esfahbod 已提交
1302 1303
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
B
Behdad Esfahbod 已提交
1304
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
1305
    if (unlikely (!GSUBGPOS::sanitize (c))) return_trace (false);
B
Behdad Esfahbod 已提交
1306
    const OffsetTo<SubstLookupList> &list = CastR<OffsetTo<SubstLookupList> > (lookupList);
B
Behdad Esfahbod 已提交
1307
    return_trace (list.sanitize (c, this));
B
Behdad Esfahbod 已提交
1308
  }
B
Minor  
Behdad Esfahbod 已提交
1309
};
1310 1311


B
Behdad Esfahbod 已提交
1312
void
1313
GSUB::substitute_start (hb_font_t *font, hb_buffer_t *buffer)
B
Behdad Esfahbod 已提交
1314
{
1315
  _hb_buffer_assert_gsubgpos_vars (buffer);
1316

1317
  const GDEF &gdef = *hb_ot_layout_from_face (font->face)->gdef;
B
Behdad Esfahbod 已提交
1318
  unsigned int count = buffer->len;
B
Behdad Esfahbod 已提交
1319 1320
  for (unsigned int i = 0; i < count; i++)
  {
1321 1322
    _hb_glyph_info_set_glyph_props (&buffer->info[i], gdef.get_glyph_props (buffer->info[i].codepoint));
    _hb_glyph_info_clear_lig_props (&buffer->info[i]);
B
Behdad Esfahbod 已提交
1323
    buffer->info[i].syllable() = 0;
1324
  }
B
Behdad Esfahbod 已提交
1325 1326 1327
}


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

1330
/*static*/ inline bool ExtensionSubst::is_reverse (void) const
B
Behdad Esfahbod 已提交
1331 1332
{
  unsigned int type = get_type ();
1333
  if (unlikely (type == SubstLookupSubTable::Extension))
1334
    return CastR<ExtensionSubst> (get_subtable<LookupSubTable>()).is_reverse ();
B
Behdad Esfahbod 已提交
1335 1336 1337
  return SubstLookup::lookup_type_is_reverse (type);
}

1338
template <typename context_t>
1339
/*static*/ inline typename context_t::return_t SubstLookup::dispatch_recurse_func (context_t *c, unsigned int lookup_index)
1340 1341 1342
{
  const GSUB &gsub = *(hb_ot_layout_from_face (c->face)->gsub);
  const SubstLookup &l = gsub.get_lookup (lookup_index);
1343
  return l.dispatch (c);
1344 1345
}

1346
/*static*/ inline bool SubstLookup::apply_recurse_func (hb_ot_apply_context_t *c, unsigned int lookup_index)
B
Behdad Esfahbod 已提交
1347
{
1348
  const GSUB &gsub = *(hb_ot_layout_from_face (c->face)->gsub);
1349
  const SubstLookup &l = gsub.get_lookup (lookup_index);
1350
  unsigned int saved_lookup_props = c->lookup_props;
1351 1352 1353
  unsigned int saved_lookup_index = c->lookup_index;
  c->set_lookup_index (lookup_index);
  c->set_lookup_props (l.get_props ());
1354
  bool ret = l.dispatch (c);
1355
  c->set_lookup_index (saved_lookup_index);
1356
  c->set_lookup_props (saved_lookup_props);
1357
  return ret;
1358 1359 1360
}


B
Behdad Esfahbod 已提交
1361
} /* namespace OT */
1362

B
Behdad Esfahbod 已提交
1363

1364
#endif /* HB_OT_LAYOUT_GSUB_TABLE_HH */