2 * Copyright © 2007,2008,2009,2010 Red Hat, Inc.
3 * Copyright © 2010,2012,2013 Google, Inc.
5 * This is part of HarfBuzz, a text shaping library.
7 * Permission is hereby granted, without written agreement and without
8 * license or royalty fees, to use, copy, modify, and distribute this
9 * software and its documentation for any purpose, provided that the
10 * above copyright notice and the following two paragraphs appear in
11 * all copies of this software.
13 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
14 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
15 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
16 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
19 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
20 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
22 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
23 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
25 * Red Hat Author(s): Behdad Esfahbod
26 * Google Author(s): Behdad Esfahbod
29 #ifndef HB_OT_LAYOUT_GSUB_TABLE_HH
30 #define HB_OT_LAYOUT_GSUB_TABLE_HH
32 #include "hb-ot-layout-gsubgpos-private.hh"
38 struct SingleSubstFormat1
40 inline void closure (hb_closure_context_t *c) const
44 for (iter.init (this+coverage); iter.more (); iter.next ())
46 /* TODO Switch to range-based API to work around malicious fonts.
47 * https://github.com/harfbuzz/harfbuzz/issues/363 */
48 hb_codepoint_t glyph_id = iter.get_glyph ();
49 if (c->glyphs->has (glyph_id))
50 c->glyphs->add ((glyph_id + deltaGlyphID) & 0xFFFFu);
54 inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
56 TRACE_COLLECT_GLYPHS (this);
57 if (unlikely (!(this+coverage).add_coverage (c->input))) return;
59 for (iter.init (this+coverage); iter.more (); iter.next ())
61 /* 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);
68 inline const Coverage &get_coverage (void) const
73 inline bool would_apply (hb_would_apply_context_t *c) const
75 TRACE_WOULD_APPLY (this);
76 return_trace (c->len == 1 && (this+coverage).get_coverage (c->glyphs[0]) != NOT_COVERED);
79 inline bool apply (hb_ot_apply_context_t *c) const
82 hb_codepoint_t glyph_id = c->buffer->cur().codepoint;
83 unsigned int index = (this+coverage).get_coverage (glyph_id);
84 if (likely (index == NOT_COVERED)) return_trace (false);
86 /* According to the Adobe Annotated OpenType Suite, result is always
87 * limited to 16bit. */
88 glyph_id = (glyph_id + deltaGlyphID) & 0xFFFFu;
89 c->replace_glyph (glyph_id);
94 inline bool serialize (hb_serialize_context_t *c,
95 Supplier<GlyphID> &glyphs,
96 unsigned int num_glyphs,
99 TRACE_SERIALIZE (this);
100 if (unlikely (!c->extend_min (*this))) return_trace (false);
101 if (unlikely (!coverage.serialize (c, this).serialize (c, glyphs, num_glyphs))) return_trace (false);
102 deltaGlyphID.set (delta); /* TODO(serilaize) overflow? */
106 inline bool sanitize (hb_sanitize_context_t *c) const
108 TRACE_SANITIZE (this);
109 return_trace (coverage.sanitize (c, this) && deltaGlyphID.sanitize (c));
113 HBUINT16 format; /* Format identifier--format = 1 */
115 coverage; /* Offset to Coverage table--from
116 * beginning of Substitution table */
117 HBINT16 deltaGlyphID; /* Add to original GlyphID to get
118 * substitute GlyphID */
120 DEFINE_SIZE_STATIC (6);
123 struct SingleSubstFormat2
125 inline void closure (hb_closure_context_t *c) const
127 TRACE_CLOSURE (this);
129 unsigned int count = substitute.len;
130 for (iter.init (this+coverage); iter.more (); iter.next ())
132 if (unlikely (iter.get_coverage () >= count))
133 break; /* Work around malicious fonts. https://github.com/harfbuzz/harfbuzz/issues/363 */
134 if (c->glyphs->has (iter.get_glyph ()))
135 c->glyphs->add (substitute[iter.get_coverage ()]);
139 inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
141 TRACE_COLLECT_GLYPHS (this);
142 if (unlikely (!(this+coverage).add_coverage (c->input))) return;
144 unsigned int count = substitute.len;
145 for (iter.init (this+coverage); iter.more (); iter.next ())
147 if (unlikely (iter.get_coverage () >= count))
148 break; /* Work around malicious fonts. https://github.com/harfbuzz/harfbuzz/issues/363 */
149 c->output->add (substitute[iter.get_coverage ()]);
153 inline const Coverage &get_coverage (void) const
155 return this+coverage;
158 inline bool would_apply (hb_would_apply_context_t *c) const
160 TRACE_WOULD_APPLY (this);
161 return_trace (c->len == 1 && (this+coverage).get_coverage (c->glyphs[0]) != NOT_COVERED);
164 inline bool apply (hb_ot_apply_context_t *c) const
167 hb_codepoint_t glyph_id = c->buffer->cur().codepoint;
168 unsigned int index = (this+coverage).get_coverage (glyph_id);
169 if (likely (index == NOT_COVERED)) return_trace (false);
171 if (unlikely (index >= substitute.len)) return_trace (false);
173 glyph_id = substitute[index];
174 c->replace_glyph (glyph_id);
179 inline bool serialize (hb_serialize_context_t *c,
180 Supplier<GlyphID> &glyphs,
181 Supplier<GlyphID> &substitutes,
182 unsigned int num_glyphs)
184 TRACE_SERIALIZE (this);
185 if (unlikely (!c->extend_min (*this))) return_trace (false);
186 if (unlikely (!substitute.serialize (c, substitutes, num_glyphs))) return_trace (false);
187 if (unlikely (!coverage.serialize (c, this).serialize (c, glyphs, num_glyphs))) return_trace (false);
191 inline bool sanitize (hb_sanitize_context_t *c) const
193 TRACE_SANITIZE (this);
194 return_trace (coverage.sanitize (c, this) && substitute.sanitize (c));
198 HBUINT16 format; /* Format identifier--format = 2 */
200 coverage; /* Offset to Coverage table--from
201 * beginning of Substitution table */
203 substitute; /* Array of substitute
204 * GlyphIDs--ordered by Coverage Index */
206 DEFINE_SIZE_ARRAY (6, substitute);
211 inline bool serialize (hb_serialize_context_t *c,
212 Supplier<GlyphID> &glyphs,
213 Supplier<GlyphID> &substitutes,
214 unsigned int num_glyphs)
216 TRACE_SERIALIZE (this);
217 if (unlikely (!c->extend_min (u.format))) return_trace (false);
218 unsigned int format = 2;
222 /* TODO(serialize) check for wrap-around */
223 delta = substitutes[0] - glyphs[0];
224 for (unsigned int i = 1; i < num_glyphs; i++)
225 if (delta != substitutes[i] - glyphs[i]) {
230 u.format.set (format);
232 case 1: return_trace (u.format1.serialize (c, glyphs, num_glyphs, delta));
233 case 2: return_trace (u.format2.serialize (c, glyphs, substitutes, num_glyphs));
234 default:return_trace (false);
238 template <typename context_t>
239 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 ());
244 case 1: return_trace (c->dispatch (u.format1));
245 case 2: return_trace (c->dispatch (u.format2));
246 default:return_trace (c->default_return_value ());
252 HBUINT16 format; /* Format identifier */
253 SingleSubstFormat1 format1;
254 SingleSubstFormat2 format2;
261 inline void closure (hb_closure_context_t *c) const
263 TRACE_CLOSURE (this);
264 unsigned int count = substitute.len;
265 for (unsigned int i = 0; i < count; i++)
266 c->glyphs->add (substitute[i]);
269 inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
271 TRACE_COLLECT_GLYPHS (this);
272 c->output->add_array (substitute.arrayZ, substitute.len);
275 inline bool apply (hb_ot_apply_context_t *c) const
278 unsigned int count = substitute.len;
280 /* Special-case to make it in-place and not consider this
281 * as a "multiplied" substitution. */
282 if (unlikely (count == 1))
284 c->replace_glyph (substitute.arrayZ[0]);
287 /* Spec disallows this, but Uniscribe allows it.
288 * https://github.com/harfbuzz/harfbuzz/issues/253 */
289 else if (unlikely (count == 0))
291 c->buffer->delete_glyph ();
295 unsigned int klass = _hb_glyph_info_is_ligature (&c->buffer->cur()) ?
296 HB_OT_LAYOUT_GLYPH_PROPS_BASE_GLYPH : 0;
298 for (unsigned int i = 0; i < count; i++) {
299 _hb_glyph_info_set_lig_props_for_component (&c->buffer->cur(), i);
300 c->output_glyph_for_component (substitute.arrayZ[i], klass);
302 c->buffer->skip_glyph ();
307 inline bool serialize (hb_serialize_context_t *c,
308 Supplier<GlyphID> &glyphs,
309 unsigned int num_glyphs)
311 TRACE_SERIALIZE (this);
312 if (unlikely (!c->extend_min (*this))) return_trace (false);
313 if (unlikely (!substitute.serialize (c, glyphs, num_glyphs))) return_trace (false);
317 inline bool sanitize (hb_sanitize_context_t *c) const
319 TRACE_SANITIZE (this);
320 return_trace (substitute.sanitize (c));
325 substitute; /* String of GlyphIDs to substitute */
327 DEFINE_SIZE_ARRAY (2, substitute);
330 struct MultipleSubstFormat1
332 inline void closure (hb_closure_context_t *c) const
334 TRACE_CLOSURE (this);
336 unsigned int count = sequence.len;
337 for (iter.init (this+coverage); iter.more (); iter.next ())
339 if (unlikely (iter.get_coverage () >= count))
340 break; /* Work around malicious fonts. https://github.com/harfbuzz/harfbuzz/issues/363 */
341 if (c->glyphs->has (iter.get_glyph ()))
342 (this+sequence[iter.get_coverage ()]).closure (c);
346 inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
348 TRACE_COLLECT_GLYPHS (this);
349 if (unlikely (!(this+coverage).add_coverage (c->input))) return;
350 unsigned int count = sequence.len;
351 for (unsigned int i = 0; i < count; i++)
352 (this+sequence[i]).collect_glyphs (c);
355 inline const Coverage &get_coverage (void) const
357 return this+coverage;
360 inline bool would_apply (hb_would_apply_context_t *c) const
362 TRACE_WOULD_APPLY (this);
363 return_trace (c->len == 1 && (this+coverage).get_coverage (c->glyphs[0]) != NOT_COVERED);
366 inline bool apply (hb_ot_apply_context_t *c) const
370 unsigned int index = (this+coverage).get_coverage (c->buffer->cur().codepoint);
371 if (likely (index == NOT_COVERED)) return_trace (false);
373 return_trace ((this+sequence[index]).apply (c));
376 inline bool serialize (hb_serialize_context_t *c,
377 Supplier<GlyphID> &glyphs,
378 Supplier<unsigned int> &substitute_len_list,
379 unsigned int num_glyphs,
380 Supplier<GlyphID> &substitute_glyphs_list)
382 TRACE_SERIALIZE (this);
383 if (unlikely (!c->extend_min (*this))) return_trace (false);
384 if (unlikely (!sequence.serialize (c, num_glyphs))) return_trace (false);
385 for (unsigned int i = 0; i < num_glyphs; i++)
386 if (unlikely (!sequence[i].serialize (c, this).serialize (c,
387 substitute_glyphs_list,
388 substitute_len_list[i]))) return_trace (false);
389 substitute_len_list += num_glyphs;
390 if (unlikely (!coverage.serialize (c, this).serialize (c, glyphs, num_glyphs))) return_trace (false);
394 inline bool sanitize (hb_sanitize_context_t *c) const
396 TRACE_SANITIZE (this);
397 return_trace (coverage.sanitize (c, this) && sequence.sanitize (c, this));
401 HBUINT16 format; /* Format identifier--format = 1 */
403 coverage; /* Offset to Coverage table--from
404 * beginning of Substitution table */
405 OffsetArrayOf<Sequence>
406 sequence; /* Array of Sequence tables
407 * ordered by Coverage Index */
409 DEFINE_SIZE_ARRAY (6, sequence);
414 inline bool serialize (hb_serialize_context_t *c,
415 Supplier<GlyphID> &glyphs,
416 Supplier<unsigned int> &substitute_len_list,
417 unsigned int num_glyphs,
418 Supplier<GlyphID> &substitute_glyphs_list)
420 TRACE_SERIALIZE (this);
421 if (unlikely (!c->extend_min (u.format))) return_trace (false);
422 unsigned int format = 1;
423 u.format.set (format);
425 case 1: return_trace (u.format1.serialize (c, glyphs, substitute_len_list, num_glyphs, substitute_glyphs_list));
426 default:return_trace (false);
430 template <typename context_t>
431 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 ());
436 case 1: return_trace (c->dispatch (u.format1));
437 default:return_trace (c->default_return_value ());
443 HBUINT16 format; /* Format identifier */
444 MultipleSubstFormat1 format1;
449 typedef ArrayOf<GlyphID> AlternateSet; /* Array of alternate GlyphIDs--in
452 struct AlternateSubstFormat1
454 inline void closure (hb_closure_context_t *c) const
456 TRACE_CLOSURE (this);
458 unsigned int count = alternateSet.len;
459 for (iter.init (this+coverage); iter.more (); iter.next ())
461 if (unlikely (iter.get_coverage () >= count))
462 break; /* Work around malicious fonts. https://github.com/harfbuzz/harfbuzz/issues/363 */
463 if (c->glyphs->has (iter.get_glyph ())) {
464 const AlternateSet &alt_set = this+alternateSet[iter.get_coverage ()];
465 unsigned int count = alt_set.len;
466 for (unsigned int i = 0; i < count; i++)
467 c->glyphs->add (alt_set[i]);
472 inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
474 TRACE_COLLECT_GLYPHS (this);
475 if (unlikely (!(this+coverage).add_coverage (c->input))) return;
477 unsigned int count = alternateSet.len;
478 for (iter.init (this+coverage); iter.more (); iter.next ())
480 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.arrayZ, alt_set.len);
487 inline const Coverage &get_coverage (void) const
489 return this+coverage;
492 inline bool would_apply (hb_would_apply_context_t *c) const
494 TRACE_WOULD_APPLY (this);
495 return_trace (c->len == 1 && (this+coverage).get_coverage (c->glyphs[0]) != NOT_COVERED);
498 inline bool apply (hb_ot_apply_context_t *c) const
501 hb_codepoint_t glyph_id = c->buffer->cur().codepoint;
503 unsigned int index = (this+coverage).get_coverage (glyph_id);
504 if (likely (index == NOT_COVERED)) return_trace (false);
506 const AlternateSet &alt_set = this+alternateSet[index];
508 if (unlikely (!alt_set.len)) return_trace (false);
510 hb_mask_t glyph_mask = c->buffer->cur().mask;
511 hb_mask_t lookup_mask = c->lookup_mask;
513 /* Note: This breaks badly if two features enabled this lookup together. */
514 unsigned int shift = _hb_ctz (lookup_mask);
515 unsigned int alt_index = ((lookup_mask & glyph_mask) >> shift);
517 if (unlikely (alt_index > alt_set.len || alt_index == 0)) return_trace (false);
519 glyph_id = alt_set[alt_index - 1];
521 c->replace_glyph (glyph_id);
526 inline bool serialize (hb_serialize_context_t *c,
527 Supplier<GlyphID> &glyphs,
528 Supplier<unsigned int> &alternate_len_list,
529 unsigned int num_glyphs,
530 Supplier<GlyphID> &alternate_glyphs_list)
532 TRACE_SERIALIZE (this);
533 if (unlikely (!c->extend_min (*this))) return_trace (false);
534 if (unlikely (!alternateSet.serialize (c, num_glyphs))) return_trace (false);
535 for (unsigned int i = 0; i < num_glyphs; i++)
536 if (unlikely (!alternateSet[i].serialize (c, this).serialize (c,
537 alternate_glyphs_list,
538 alternate_len_list[i]))) return_trace (false);
539 alternate_len_list += num_glyphs;
540 if (unlikely (!coverage.serialize (c, this).serialize (c, glyphs, num_glyphs))) return_trace (false);
544 inline bool sanitize (hb_sanitize_context_t *c) const
546 TRACE_SANITIZE (this);
547 return_trace (coverage.sanitize (c, this) && alternateSet.sanitize (c, this));
551 HBUINT16 format; /* Format identifier--format = 1 */
553 coverage; /* Offset to Coverage table--from
554 * beginning of Substitution table */
555 OffsetArrayOf<AlternateSet>
556 alternateSet; /* Array of AlternateSet tables
557 * ordered by Coverage Index */
559 DEFINE_SIZE_ARRAY (6, alternateSet);
562 struct AlternateSubst
564 inline bool serialize (hb_serialize_context_t *c,
565 Supplier<GlyphID> &glyphs,
566 Supplier<unsigned int> &alternate_len_list,
567 unsigned int num_glyphs,
568 Supplier<GlyphID> &alternate_glyphs_list)
570 TRACE_SERIALIZE (this);
571 if (unlikely (!c->extend_min (u.format))) return_trace (false);
572 unsigned int format = 1;
573 u.format.set (format);
575 case 1: return_trace (u.format1.serialize (c, glyphs, alternate_len_list, num_glyphs, alternate_glyphs_list));
576 default:return_trace (false);
580 template <typename context_t>
581 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 ());
586 case 1: return_trace (c->dispatch (u.format1));
587 default:return_trace (c->default_return_value ());
593 HBUINT16 format; /* Format identifier */
594 AlternateSubstFormat1 format1;
601 inline void closure (hb_closure_context_t *c) const
603 TRACE_CLOSURE (this);
604 unsigned int count = component.len;
605 for (unsigned int i = 1; i < count; i++)
606 if (!c->glyphs->has (component[i]))
608 c->glyphs->add (ligGlyph);
611 inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
613 TRACE_COLLECT_GLYPHS (this);
614 c->input->add_array (component.arrayZ, component.len ? component.len - 1 : 0);
615 c->output->add (ligGlyph);
618 inline bool would_apply (hb_would_apply_context_t *c) const
620 TRACE_WOULD_APPLY (this);
621 if (c->len != component.len)
622 return_trace (false);
624 for (unsigned int i = 1; i < c->len; i++)
625 if (likely (c->glyphs[i] != component[i]))
626 return_trace (false);
631 inline bool apply (hb_ot_apply_context_t *c) const
634 unsigned int count = component.len;
636 if (unlikely (!count)) return_trace (false);
638 /* Special-case to make it in-place and not consider this
639 * as a "ligated" substitution. */
640 if (unlikely (count == 1))
642 c->replace_glyph (ligGlyph);
646 bool is_mark_ligature = false;
647 unsigned int total_component_count = 0;
649 unsigned int match_length = 0;
650 unsigned int match_positions[HB_MAX_CONTEXT_LENGTH];
652 if (likely (!match_input (c, count,
659 &total_component_count)))
660 return_trace (false);
668 total_component_count);
673 inline bool serialize (hb_serialize_context_t *c,
675 Supplier<GlyphID> &components, /* Starting from second */
676 unsigned int num_components /* Including first component */)
678 TRACE_SERIALIZE (this);
679 if (unlikely (!c->extend_min (*this))) return_trace (false);
681 if (unlikely (!component.serialize (c, components, num_components))) return_trace (false);
686 inline bool sanitize (hb_sanitize_context_t *c) const
688 TRACE_SANITIZE (this);
689 return_trace (ligGlyph.sanitize (c) && component.sanitize (c));
693 GlyphID ligGlyph; /* GlyphID of ligature to substitute */
694 HeadlessArrayOf<GlyphID>
695 component; /* Array of component GlyphIDs--start
696 * with the second component--ordered
697 * in writing direction */
699 DEFINE_SIZE_ARRAY (4, component);
704 inline void closure (hb_closure_context_t *c) const
706 TRACE_CLOSURE (this);
707 unsigned int num_ligs = ligature.len;
708 for (unsigned int i = 0; i < num_ligs; i++)
709 (this+ligature[i]).closure (c);
712 inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
714 TRACE_COLLECT_GLYPHS (this);
715 unsigned int num_ligs = ligature.len;
716 for (unsigned int i = 0; i < num_ligs; i++)
717 (this+ligature[i]).collect_glyphs (c);
720 inline bool would_apply (hb_would_apply_context_t *c) const
722 TRACE_WOULD_APPLY (this);
723 unsigned int num_ligs = ligature.len;
724 for (unsigned int i = 0; i < num_ligs; i++)
726 const Ligature &lig = this+ligature[i];
727 if (lig.would_apply (c))
730 return_trace (false);
733 inline bool apply (hb_ot_apply_context_t *c) const
736 unsigned int num_ligs = ligature.len;
737 for (unsigned int i = 0; i < num_ligs; i++)
739 const Ligature &lig = this+ligature[i];
740 if (lig.apply (c)) return_trace (true);
743 return_trace (false);
746 inline bool serialize (hb_serialize_context_t *c,
747 Supplier<GlyphID> &ligatures,
748 Supplier<unsigned int> &component_count_list,
749 unsigned int num_ligatures,
750 Supplier<GlyphID> &component_list /* Starting from second for each ligature */)
752 TRACE_SERIALIZE (this);
753 if (unlikely (!c->extend_min (*this))) return_trace (false);
754 if (unlikely (!ligature.serialize (c, num_ligatures))) return_trace (false);
755 for (unsigned int i = 0; i < num_ligatures; i++)
756 if (unlikely (!ligature[i].serialize (c, this).serialize (c,
759 component_count_list[i]))) return_trace (false);
760 ligatures += num_ligatures;
761 component_count_list += num_ligatures;
765 inline bool sanitize (hb_sanitize_context_t *c) const
767 TRACE_SANITIZE (this);
768 return_trace (ligature.sanitize (c, this));
772 OffsetArrayOf<Ligature>
773 ligature; /* Array LigatureSet tables
774 * ordered by preference */
776 DEFINE_SIZE_ARRAY (2, ligature);
779 struct LigatureSubstFormat1
781 inline void closure (hb_closure_context_t *c) const
783 TRACE_CLOSURE (this);
785 unsigned int count = ligatureSet.len;
786 for (iter.init (this+coverage); iter.more (); iter.next ())
788 if (unlikely (iter.get_coverage () >= count))
789 break; /* Work around malicious fonts. https://github.com/harfbuzz/harfbuzz/issues/363 */
790 if (c->glyphs->has (iter.get_glyph ()))
791 (this+ligatureSet[iter.get_coverage ()]).closure (c);
795 inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
797 TRACE_COLLECT_GLYPHS (this);
798 if (unlikely (!(this+coverage).add_coverage (c->input))) return;
800 unsigned int count = ligatureSet.len;
801 for (iter.init (this+coverage); iter.more (); iter.next ())
803 if (unlikely (iter.get_coverage () >= count))
804 break; /* Work around malicious fonts. https://github.com/harfbuzz/harfbuzz/issues/363 */
805 (this+ligatureSet[iter.get_coverage ()]).collect_glyphs (c);
809 inline const Coverage &get_coverage (void) const
811 return this+coverage;
814 inline bool would_apply (hb_would_apply_context_t *c) const
816 TRACE_WOULD_APPLY (this);
817 unsigned int index = (this+coverage).get_coverage (c->glyphs[0]);
818 if (likely (index == NOT_COVERED)) return_trace (false);
820 const LigatureSet &lig_set = this+ligatureSet[index];
821 return_trace (lig_set.would_apply (c));
824 inline bool apply (hb_ot_apply_context_t *c) const
827 hb_codepoint_t glyph_id = c->buffer->cur().codepoint;
829 unsigned int index = (this+coverage).get_coverage (glyph_id);
830 if (likely (index == NOT_COVERED)) return_trace (false);
832 const LigatureSet &lig_set = this+ligatureSet[index];
833 return_trace (lig_set.apply (c));
836 inline bool serialize (hb_serialize_context_t *c,
837 Supplier<GlyphID> &first_glyphs,
838 Supplier<unsigned int> &ligature_per_first_glyph_count_list,
839 unsigned int num_first_glyphs,
840 Supplier<GlyphID> &ligatures_list,
841 Supplier<unsigned int> &component_count_list,
842 Supplier<GlyphID> &component_list /* Starting from second for each ligature */)
844 TRACE_SERIALIZE (this);
845 if (unlikely (!c->extend_min (*this))) return_trace (false);
846 if (unlikely (!ligatureSet.serialize (c, num_first_glyphs))) return_trace (false);
847 for (unsigned int i = 0; i < num_first_glyphs; i++)
848 if (unlikely (!ligatureSet[i].serialize (c, this).serialize (c,
850 component_count_list,
851 ligature_per_first_glyph_count_list[i],
852 component_list))) return_trace (false);
853 ligature_per_first_glyph_count_list += num_first_glyphs;
854 if (unlikely (!coverage.serialize (c, this).serialize (c, first_glyphs, num_first_glyphs))) return_trace (false);
858 inline bool sanitize (hb_sanitize_context_t *c) const
860 TRACE_SANITIZE (this);
861 return_trace (coverage.sanitize (c, this) && ligatureSet.sanitize (c, this));
865 HBUINT16 format; /* Format identifier--format = 1 */
867 coverage; /* Offset to Coverage table--from
868 * beginning of Substitution table */
869 OffsetArrayOf<LigatureSet>
870 ligatureSet; /* Array LigatureSet tables
871 * ordered by Coverage Index */
873 DEFINE_SIZE_ARRAY (6, ligatureSet);
878 inline bool serialize (hb_serialize_context_t *c,
879 Supplier<GlyphID> &first_glyphs,
880 Supplier<unsigned int> &ligature_per_first_glyph_count_list,
881 unsigned int num_first_glyphs,
882 Supplier<GlyphID> &ligatures_list,
883 Supplier<unsigned int> &component_count_list,
884 Supplier<GlyphID> &component_list /* Starting from second for each ligature */)
886 TRACE_SERIALIZE (this);
887 if (unlikely (!c->extend_min (u.format))) return_trace (false);
888 unsigned int format = 1;
889 u.format.set (format);
891 case 1: return_trace (u.format1.serialize (c,
893 ligature_per_first_glyph_count_list,
896 component_count_list,
898 default:return_trace (false);
902 template <typename context_t>
903 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 ());
908 case 1: return_trace (c->dispatch (u.format1));
909 default:return_trace (c->default_return_value ());
915 HBUINT16 format; /* Format identifier */
916 LigatureSubstFormat1 format1;
921 struct ContextSubst : Context {};
923 struct ChainContextSubst : ChainContext {};
925 struct ExtensionSubst : Extension<ExtensionSubst>
927 typedef struct SubstLookupSubTable LookupSubTable;
929 inline bool is_reverse (void) const;
933 struct ReverseChainSingleSubstFormat1
935 inline void closure (hb_closure_context_t *c) const
937 TRACE_CLOSURE (this);
938 const OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
942 count = backtrack.len;
943 for (unsigned int i = 0; i < count; i++)
944 if (!(this+backtrack[i]).intersects (c->glyphs))
947 count = lookahead.len;
948 for (unsigned int i = 0; i < count; i++)
949 if (!(this+lookahead[i]).intersects (c->glyphs))
952 const ArrayOf<GlyphID> &substitute = StructAfter<ArrayOf<GlyphID> > (lookahead);
954 count = substitute.len;
955 for (iter.init (this+coverage); iter.more (); iter.next ())
957 if (unlikely (iter.get_coverage () >= count))
958 break; /* Work around malicious fonts. https://github.com/harfbuzz/harfbuzz/issues/363 */
959 if (c->glyphs->has (iter.get_glyph ()))
960 c->glyphs->add (substitute[iter.get_coverage ()]);
964 inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
966 TRACE_COLLECT_GLYPHS (this);
967 if (unlikely (!(this+coverage).add_coverage (c->input))) return;
971 count = backtrack.len;
972 for (unsigned int i = 0; i < count; i++)
973 if (unlikely (!(this+backtrack[i]).add_coverage (c->before))) return;
975 const OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
976 count = lookahead.len;
977 for (unsigned int i = 0; i < count; i++)
978 if (unlikely (!(this+lookahead[i]).add_coverage (c->after))) return;
980 const ArrayOf<GlyphID> &substitute = StructAfter<ArrayOf<GlyphID> > (lookahead);
981 count = substitute.len;
982 c->output->add_array (substitute.arrayZ, substitute.len);
985 inline const Coverage &get_coverage (void) const
987 return this+coverage;
990 inline bool would_apply (hb_would_apply_context_t *c) const
992 TRACE_WOULD_APPLY (this);
993 return_trace (c->len == 1 && (this+coverage).get_coverage (c->glyphs[0]) != NOT_COVERED);
996 inline bool apply (hb_ot_apply_context_t *c) const
999 if (unlikely (c->nesting_level_left != HB_MAX_NESTING_LEVEL))
1000 return_trace (false); /* No chaining to this type */
1002 unsigned int index = (this+coverage).get_coverage (c->buffer->cur().codepoint);
1003 if (likely (index == NOT_COVERED)) return_trace (false);
1005 const OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
1006 const ArrayOf<GlyphID> &substitute = StructAfter<ArrayOf<GlyphID> > (lookahead);
1008 unsigned int start_index = 0, end_index = 0;
1009 if (match_backtrack (c,
1010 backtrack.len, (HBUINT16 *) backtrack.arrayZ,
1011 match_coverage, this,
1014 lookahead.len, (HBUINT16 *) lookahead.arrayZ,
1015 match_coverage, this,
1018 c->buffer->unsafe_to_break_from_outbuffer (start_index, end_index);
1019 c->replace_glyph_inplace (substitute[index]);
1020 /* Note: We DON'T decrease buffer->idx. The main loop does it
1021 * for us. This is useful for preventing surprises if someone
1022 * calls us through a Context lookup. */
1023 return_trace (true);
1026 return_trace (false);
1029 inline bool sanitize (hb_sanitize_context_t *c) const
1031 TRACE_SANITIZE (this);
1032 if (!(coverage.sanitize (c, this) && backtrack.sanitize (c, this)))
1033 return_trace (false);
1034 const OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
1035 if (!lookahead.sanitize (c, this))
1036 return_trace (false);
1037 const ArrayOf<GlyphID> &substitute = StructAfter<ArrayOf<GlyphID> > (lookahead);
1038 return_trace (substitute.sanitize (c));
1042 HBUINT16 format; /* Format identifier--format = 1 */
1044 coverage; /* Offset to Coverage table--from
1045 * beginning of table */
1046 OffsetArrayOf<Coverage>
1047 backtrack; /* Array of coverage tables
1048 * in backtracking sequence, in glyph
1050 OffsetArrayOf<Coverage>
1051 lookaheadX; /* Array of coverage tables
1052 * in lookahead sequence, in glyph
1055 substituteX; /* Array of substitute
1056 * GlyphIDs--ordered by Coverage Index */
1058 DEFINE_SIZE_MIN (10);
1061 struct ReverseChainSingleSubst
1063 template <typename context_t>
1064 inline typename context_t::return_t dispatch (context_t *c) const
1066 TRACE_DISPATCH (this, u.format);
1067 if (unlikely (!c->may_dispatch (this, &u.format))) return_trace (c->no_dispatch_return_value ());
1069 case 1: return_trace (c->dispatch (u.format1));
1070 default:return_trace (c->default_return_value ());
1076 HBUINT16 format; /* Format identifier */
1077 ReverseChainSingleSubstFormat1 format1;
1087 struct SubstLookupSubTable
1089 friend struct SubstLookup;
1099 ReverseChainSingle = 8
1102 template <typename context_t>
1103 inline typename context_t::return_t dispatch (context_t *c, unsigned int lookup_type) const
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) {
1108 case Single: return_trace (u.single.dispatch (c));
1109 case Multiple: return_trace (u.multiple.dispatch (c));
1110 case Alternate: return_trace (u.alternate.dispatch (c));
1111 case Ligature: return_trace (u.ligature.dispatch (c));
1112 case Context: return_trace (u.context.dispatch (c));
1113 case ChainContext: return_trace (u.chainContext.dispatch (c));
1114 case Extension: return_trace (u.extension.dispatch (c));
1115 case ReverseChainSingle: return_trace (u.reverseChainContextSingle.dispatch (c));
1116 default: return_trace (c->default_return_value ());
1122 HBUINT16 sub_format;
1124 MultipleSubst multiple;
1125 AlternateSubst alternate;
1126 LigatureSubst ligature;
1127 ContextSubst context;
1128 ChainContextSubst chainContext;
1129 ExtensionSubst extension;
1130 ReverseChainSingleSubst reverseChainContextSingle;
1133 DEFINE_SIZE_UNION (2, sub_format);
1137 struct SubstLookup : Lookup
1139 inline const SubstLookupSubTable& get_subtable (unsigned int i) const
1140 { return Lookup::get_subtable<SubstLookupSubTable> (i); }
1142 inline static bool lookup_type_is_reverse (unsigned int lookup_type)
1143 { return lookup_type == SubstLookupSubTable::ReverseChainSingle; }
1145 inline bool is_reverse (void) const
1147 unsigned int type = get_type ();
1148 if (unlikely (type == SubstLookupSubTable::Extension))
1149 return CastR<ExtensionSubst> (get_subtable(0)).is_reverse ();
1150 return lookup_type_is_reverse (type);
1153 inline bool apply (hb_ot_apply_context_t *c) const
1156 return_trace (dispatch (c));
1159 inline hb_closure_context_t::return_t closure (hb_closure_context_t *c, unsigned int this_index) const
1161 TRACE_CLOSURE (this);
1162 if (!c->should_visit_lookup (this_index))
1163 return_trace (HB_VOID);
1165 c->set_recurse_func (dispatch_closure_recurse_func);
1166 return_trace (dispatch (c));
1169 inline hb_collect_glyphs_context_t::return_t collect_glyphs (hb_collect_glyphs_context_t *c) const
1171 TRACE_COLLECT_GLYPHS (this);
1172 c->set_recurse_func (dispatch_recurse_func<hb_collect_glyphs_context_t>);
1173 return_trace (dispatch (c));
1176 template <typename set_t>
1177 inline void add_coverage (set_t *glyphs) const
1179 hb_add_coverage_context_t<set_t> c (glyphs);
1183 inline bool would_apply (hb_would_apply_context_t *c,
1184 const hb_ot_layout_lookup_accelerator_t *accel) const
1186 TRACE_WOULD_APPLY (this);
1187 if (unlikely (!c->len)) return_trace (false);
1188 if (!accel->may_have (c->glyphs[0])) return_trace (false);
1189 return_trace (dispatch (c));
1192 static bool apply_recurse_func (hb_ot_apply_context_t *c, unsigned int lookup_index);
1194 inline SubstLookupSubTable& serialize_subtable (hb_serialize_context_t *c,
1196 { return get_subtables<SubstLookupSubTable> ()[i].serialize (c, this); }
1198 inline bool serialize_single (hb_serialize_context_t *c,
1199 uint32_t lookup_props,
1200 Supplier<GlyphID> &glyphs,
1201 Supplier<GlyphID> &substitutes,
1202 unsigned int num_glyphs)
1204 TRACE_SERIALIZE (this);
1205 if (unlikely (!Lookup::serialize (c, SubstLookupSubTable::Single, lookup_props, 1))) return_trace (false);
1206 return_trace (serialize_subtable (c, 0).u.single.serialize (c, glyphs, substitutes, num_glyphs));
1209 inline bool serialize_multiple (hb_serialize_context_t *c,
1210 uint32_t lookup_props,
1211 Supplier<GlyphID> &glyphs,
1212 Supplier<unsigned int> &substitute_len_list,
1213 unsigned int num_glyphs,
1214 Supplier<GlyphID> &substitute_glyphs_list)
1216 TRACE_SERIALIZE (this);
1217 if (unlikely (!Lookup::serialize (c, SubstLookupSubTable::Multiple, lookup_props, 1))) return_trace (false);
1218 return_trace (serialize_subtable (c, 0).u.multiple.serialize (c,
1220 substitute_len_list,
1222 substitute_glyphs_list));
1225 inline bool serialize_alternate (hb_serialize_context_t *c,
1226 uint32_t lookup_props,
1227 Supplier<GlyphID> &glyphs,
1228 Supplier<unsigned int> &alternate_len_list,
1229 unsigned int num_glyphs,
1230 Supplier<GlyphID> &alternate_glyphs_list)
1232 TRACE_SERIALIZE (this);
1233 if (unlikely (!Lookup::serialize (c, SubstLookupSubTable::Alternate, lookup_props, 1))) return_trace (false);
1234 return_trace (serialize_subtable (c, 0).u.alternate.serialize (c,
1238 alternate_glyphs_list));
1241 inline bool serialize_ligature (hb_serialize_context_t *c,
1242 uint32_t lookup_props,
1243 Supplier<GlyphID> &first_glyphs,
1244 Supplier<unsigned int> &ligature_per_first_glyph_count_list,
1245 unsigned int num_first_glyphs,
1246 Supplier<GlyphID> &ligatures_list,
1247 Supplier<unsigned int> &component_count_list,
1248 Supplier<GlyphID> &component_list /* Starting from second for each ligature */)
1250 TRACE_SERIALIZE (this);
1251 if (unlikely (!Lookup::serialize (c, SubstLookupSubTable::Ligature, lookup_props, 1))) return_trace (false);
1252 return_trace (serialize_subtable (c, 0).u.ligature.serialize (c,
1254 ligature_per_first_glyph_count_list,
1257 component_count_list,
1261 template <typename context_t>
1262 static inline typename context_t::return_t dispatch_recurse_func (context_t *c, unsigned int lookup_index);
1264 static inline hb_closure_context_t::return_t dispatch_closure_recurse_func (hb_closure_context_t *c, unsigned int lookup_index)
1266 if (!c->should_visit_lookup (lookup_index))
1268 return dispatch_recurse_func (c, lookup_index);
1271 template <typename context_t>
1272 inline typename context_t::return_t dispatch (context_t *c) const
1273 { return Lookup::dispatch<SubstLookupSubTable> (c); }
1275 inline bool sanitize (hb_sanitize_context_t *c) const
1277 TRACE_SANITIZE (this);
1278 if (unlikely (!Lookup::sanitize (c))) return_trace (false);
1279 if (unlikely (!dispatch (c))) return_trace (false);
1281 if (unlikely (get_type () == SubstLookupSubTable::Extension))
1283 /* The spec says all subtables of an Extension lookup should
1284 * have the same type, which shall not be the Extension type
1285 * itself (but we already checked for that).
1286 * This is specially important if one has a reverse type! */
1287 unsigned int type = get_subtable (0).u.extension.get_type ();
1288 unsigned int count = get_subtable_count ();
1289 for (unsigned int i = 1; i < count; i++)
1290 if (get_subtable (i).u.extension.get_type () != type)
1291 return_trace (false);
1293 return_trace (true);
1297 typedef OffsetListOf<SubstLookup> SubstLookupList;
1300 * GSUB -- Glyph Substitution
1301 * https://docs.microsoft.com/en-us/typography/opentype/spec/gsub
1304 struct GSUB : GSUBGPOS
1306 static const hb_tag_t tableTag = HB_OT_TAG_GSUB;
1308 inline const SubstLookup& get_lookup (unsigned int i) const
1309 { return CastR<SubstLookup> (GSUBGPOS::get_lookup (i)); }
1311 static inline void substitute_start (hb_font_t *font, hb_buffer_t *buffer);
1313 inline bool sanitize (hb_sanitize_context_t *c) const
1315 TRACE_SANITIZE (this);
1316 if (unlikely (!GSUBGPOS::sanitize (c))) return_trace (false);
1317 const OffsetTo<SubstLookupList> &list = CastR<OffsetTo<SubstLookupList> > (lookupList);
1318 return_trace (list.sanitize (c, this));
1324 GSUB::substitute_start (hb_font_t *font, hb_buffer_t *buffer)
1326 _hb_buffer_assert_gsubgpos_vars (buffer);
1328 const GDEF &gdef = *hb_ot_layout_from_face (font->face)->gdef;
1329 unsigned int count = buffer->len;
1330 for (unsigned int i = 0; i < count; i++)
1332 _hb_glyph_info_set_glyph_props (&buffer->info[i], gdef.get_glyph_props (buffer->info[i].codepoint));
1333 _hb_glyph_info_clear_lig_props (&buffer->info[i]);
1334 buffer->info[i].syllable() = 0;
1339 /* Out-of-class implementation for methods recursing */
1341 /*static*/ inline bool ExtensionSubst::is_reverse (void) const
1343 unsigned int type = get_type ();
1344 if (unlikely (type == SubstLookupSubTable::Extension))
1345 return CastR<ExtensionSubst> (get_subtable<LookupSubTable>()).is_reverse ();
1346 return SubstLookup::lookup_type_is_reverse (type);
1349 template <typename context_t>
1350 /*static*/ inline typename context_t::return_t SubstLookup::dispatch_recurse_func (context_t *c, unsigned int lookup_index)
1352 const GSUB &gsub = *(hb_ot_layout_from_face (c->face)->gsub);
1353 const SubstLookup &l = gsub.get_lookup (lookup_index);
1354 return l.dispatch (c);
1357 /*static*/ inline bool SubstLookup::apply_recurse_func (hb_ot_apply_context_t *c, unsigned int lookup_index)
1359 const GSUB &gsub = *(hb_ot_layout_from_face (c->face)->gsub);
1360 const SubstLookup &l = gsub.get_lookup (lookup_index);
1361 unsigned int saved_lookup_props = c->lookup_props;
1362 unsigned int saved_lookup_index = c->lookup_index;
1363 c->set_lookup_index (lookup_index);
1364 c->set_lookup_props (l.get_props ());
1365 bool ret = l.dispatch (c);
1366 c->set_lookup_index (saved_lookup_index);
1367 c->set_lookup_props (saved_lookup_props);
1372 } /* namespace OT */
1375 #endif /* HB_OT_LAYOUT_GSUB_TABLE_HH */