2 * Copyright © 2007,2008,2009,2010 Red Hat, Inc.
3 * Copyright © 2010,2012 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"
36 struct SingleSubstFormat1
38 friend struct SingleSubst;
42 inline void closure (hb_closure_context_t *c) const
46 for (iter.init (this+coverage); iter.more (); iter.next ()) {
47 hb_codepoint_t glyph_id = iter.get_glyph ();
48 if (c->glyphs->has (glyph_id))
49 c->glyphs->add ((glyph_id + deltaGlyphID) & 0xFFFF);
53 inline const Coverage &get_coverage (void) const
58 inline bool apply (hb_apply_context_t *c) const
61 hb_codepoint_t glyph_id = c->buffer->cur().codepoint;
62 unsigned int index = (this+coverage) (glyph_id);
63 if (likely (index == NOT_COVERED)) return TRACE_RETURN (false);
65 /* According to the Adobe Annotated OpenType Suite, result is always
66 * limited to 16bit. */
67 glyph_id = (glyph_id + deltaGlyphID) & 0xFFFF;
68 c->replace_glyph (glyph_id);
70 return TRACE_RETURN (true);
73 inline bool sanitize (hb_sanitize_context_t *c) {
75 return TRACE_RETURN (coverage.sanitize (c, this) && deltaGlyphID.sanitize (c));
79 USHORT format; /* Format identifier--format = 1 */
81 coverage; /* Offset to Coverage table--from
82 * beginning of Substitution table */
83 SHORT deltaGlyphID; /* Add to original GlyphID to get
84 * substitute GlyphID */
86 DEFINE_SIZE_STATIC (6);
89 struct SingleSubstFormat2
91 friend struct SingleSubst;
95 inline void closure (hb_closure_context_t *c) const
99 for (iter.init (this+coverage); iter.more (); iter.next ()) {
100 if (c->glyphs->has (iter.get_glyph ()))
101 c->glyphs->add (substitute[iter.get_coverage ()]);
105 inline const Coverage &get_coverage (void) const
107 return this+coverage;
110 inline bool apply (hb_apply_context_t *c) const
113 hb_codepoint_t glyph_id = c->buffer->cur().codepoint;
114 unsigned int index = (this+coverage) (glyph_id);
115 if (likely (index == NOT_COVERED)) return TRACE_RETURN (false);
117 if (unlikely (index >= substitute.len)) return TRACE_RETURN (false);
119 glyph_id = substitute[index];
120 c->replace_glyph (glyph_id);
122 return TRACE_RETURN (true);
125 inline bool sanitize (hb_sanitize_context_t *c) {
127 return TRACE_RETURN (coverage.sanitize (c, this) && substitute.sanitize (c));
131 USHORT format; /* Format identifier--format = 2 */
133 coverage; /* Offset to Coverage table--from
134 * beginning of Substitution table */
136 substitute; /* Array of substitute
137 * GlyphIDs--ordered by Coverage Index */
139 DEFINE_SIZE_ARRAY (6, substitute);
144 friend struct SubstLookupSubTable;
148 inline void closure (hb_closure_context_t *c) const
152 case 1: u.format1.closure (c); break;
153 case 2: u.format2.closure (c); break;
158 inline const Coverage &get_coverage (void) const
161 case 1: return u.format1.get_coverage ();
162 case 2: return u.format2.get_coverage ();
163 default:return Null(Coverage);
167 inline bool apply (hb_apply_context_t *c) const
171 case 1: return TRACE_RETURN (u.format1.apply (c));
172 case 2: return TRACE_RETURN (u.format2.apply (c));
173 default:return TRACE_RETURN (false);
177 inline bool sanitize (hb_sanitize_context_t *c) {
179 if (!u.format.sanitize (c)) return TRACE_RETURN (false);
181 case 1: return TRACE_RETURN (u.format1.sanitize (c));
182 case 2: return TRACE_RETURN (u.format2.sanitize (c));
183 default:return TRACE_RETURN (true);
189 USHORT format; /* Format identifier */
190 SingleSubstFormat1 format1;
191 SingleSubstFormat2 format2;
198 friend struct MultipleSubstFormat1;
202 inline void closure (hb_closure_context_t *c) const
205 unsigned int count = substitute.len;
206 for (unsigned int i = 0; i < count; i++)
207 c->glyphs->add (substitute[i]);
210 inline bool apply (hb_apply_context_t *c) const
213 if (unlikely (!substitute.len)) return TRACE_RETURN (false);
215 unsigned int klass = c->property & HB_OT_LAYOUT_GLYPH_CLASS_LIGATURE ? HB_OT_LAYOUT_GLYPH_CLASS_BASE_GLYPH : 0;
216 unsigned int count = substitute.len;
217 for (unsigned int i = 0; i < count; i++) {
218 set_lig_props_for_component (c->buffer->cur(), i);
219 c->output_glyph (substitute.array[i], klass);
221 c->buffer->skip_glyph ();
223 return TRACE_RETURN (true);
227 inline bool sanitize (hb_sanitize_context_t *c) {
229 return TRACE_RETURN (substitute.sanitize (c));
234 substitute; /* String of GlyphIDs to substitute */
236 DEFINE_SIZE_ARRAY (2, substitute);
239 struct MultipleSubstFormat1
241 friend struct MultipleSubst;
245 inline void closure (hb_closure_context_t *c) const
249 for (iter.init (this+coverage); iter.more (); iter.next ()) {
250 if (c->glyphs->has (iter.get_glyph ()))
251 (this+sequence[iter.get_coverage ()]).closure (c);
255 inline const Coverage &get_coverage (void) const
257 return this+coverage;
260 inline bool apply (hb_apply_context_t *c) const
264 unsigned int index = (this+coverage) (c->buffer->cur().codepoint);
265 if (likely (index == NOT_COVERED)) return TRACE_RETURN (false);
267 return TRACE_RETURN ((this+sequence[index]).apply (c));
270 inline bool sanitize (hb_sanitize_context_t *c) {
272 return TRACE_RETURN (coverage.sanitize (c, this) && sequence.sanitize (c, this));
276 USHORT format; /* Format identifier--format = 1 */
278 coverage; /* Offset to Coverage table--from
279 * beginning of Substitution table */
280 OffsetArrayOf<Sequence>
281 sequence; /* Array of Sequence tables
282 * ordered by Coverage Index */
284 DEFINE_SIZE_ARRAY (6, sequence);
289 friend struct SubstLookupSubTable;
293 inline void closure (hb_closure_context_t *c) const
297 case 1: u.format1.closure (c); break;
302 inline const Coverage &get_coverage (void) const
305 case 1: return u.format1.get_coverage ();
306 default:return Null(Coverage);
310 inline bool apply (hb_apply_context_t *c) const
314 case 1: return TRACE_RETURN (u.format1.apply (c));
315 default:return TRACE_RETURN (false);
319 inline bool sanitize (hb_sanitize_context_t *c) {
321 if (!u.format.sanitize (c)) return TRACE_RETURN (false);
323 case 1: return TRACE_RETURN (u.format1.sanitize (c));
324 default:return TRACE_RETURN (true);
330 USHORT format; /* Format identifier */
331 MultipleSubstFormat1 format1;
336 typedef ArrayOf<GlyphID> AlternateSet; /* Array of alternate GlyphIDs--in
339 struct AlternateSubstFormat1
341 friend struct AlternateSubst;
345 inline void closure (hb_closure_context_t *c) const
349 for (iter.init (this+coverage); iter.more (); iter.next ()) {
350 if (c->glyphs->has (iter.get_glyph ())) {
351 const AlternateSet &alt_set = this+alternateSet[iter.get_coverage ()];
352 unsigned int count = alt_set.len;
353 for (unsigned int i = 0; i < count; i++)
354 c->glyphs->add (alt_set[i]);
359 inline const Coverage &get_coverage (void) const
361 return this+coverage;
364 inline bool apply (hb_apply_context_t *c) const
367 hb_codepoint_t glyph_id = c->buffer->cur().codepoint;
369 unsigned int index = (this+coverage) (glyph_id);
370 if (likely (index == NOT_COVERED)) return TRACE_RETURN (false);
372 const AlternateSet &alt_set = this+alternateSet[index];
374 if (unlikely (!alt_set.len)) return TRACE_RETURN (false);
376 hb_mask_t glyph_mask = c->buffer->cur().mask;
377 hb_mask_t lookup_mask = c->lookup_mask;
379 /* Note: This breaks badly if two features enabled this lookup together. */
380 unsigned int shift = _hb_ctz (lookup_mask);
381 unsigned int alt_index = ((lookup_mask & glyph_mask) >> shift);
383 if (unlikely (alt_index > alt_set.len || alt_index == 0)) return TRACE_RETURN (false);
385 glyph_id = alt_set[alt_index - 1];
387 c->replace_glyph (glyph_id);
389 return TRACE_RETURN (true);
392 inline bool sanitize (hb_sanitize_context_t *c) {
394 return TRACE_RETURN (coverage.sanitize (c, this) && alternateSet.sanitize (c, this));
398 USHORT format; /* Format identifier--format = 1 */
400 coverage; /* Offset to Coverage table--from
401 * beginning of Substitution table */
402 OffsetArrayOf<AlternateSet>
403 alternateSet; /* Array of AlternateSet tables
404 * ordered by Coverage Index */
406 DEFINE_SIZE_ARRAY (6, alternateSet);
409 struct AlternateSubst
411 friend struct SubstLookupSubTable;
415 inline void closure (hb_closure_context_t *c) const
419 case 1: u.format1.closure (c); break;
424 inline const Coverage &get_coverage (void) const
427 case 1: return u.format1.get_coverage ();
428 default:return Null(Coverage);
432 inline bool apply (hb_apply_context_t *c) const
436 case 1: return TRACE_RETURN (u.format1.apply (c));
437 default:return TRACE_RETURN (false);
441 inline bool sanitize (hb_sanitize_context_t *c) {
443 if (!u.format.sanitize (c)) return TRACE_RETURN (false);
445 case 1: return TRACE_RETURN (u.format1.sanitize (c));
446 default:return TRACE_RETURN (true);
452 USHORT format; /* Format identifier */
453 AlternateSubstFormat1 format1;
460 friend struct LigatureSet;
464 inline void closure (hb_closure_context_t *c) const
467 unsigned int count = component.len;
468 for (unsigned int i = 1; i < count; i++)
469 if (!c->glyphs->has (component[i]))
471 c->glyphs->add (ligGlyph);
474 inline bool would_apply (hb_would_apply_context_t *c) const
476 if (c->len != component.len)
479 for (unsigned int i = 1; i < c->len; i++)
480 if (likely (c->glyphs[i] != component[i]))
486 inline bool apply (hb_apply_context_t *c) const
489 unsigned int count = component.len;
490 if (unlikely (count < 1)) return TRACE_RETURN (false);
492 hb_apply_context_t::mark_skipping_forward_iterator_t skippy_iter (c, c->buffer->idx, count - 1);
493 if (skippy_iter.has_no_chance ()) return TRACE_RETURN (false);
496 * This is perhaps the trickiest part of GSUB... Remarks:
498 * - If all components of the ligature were marks, we call this a mark ligature.
500 * - If there is no GDEF, and the ligature is NOT a mark ligature, we categorize
501 * it as a ligature glyph. Though, really, this will not really be used...
503 * - If it *is* a mark ligature, we don't allocate a new ligature id, and leave
504 * the ligature to keep its old ligature id. This will allow it to attach to
505 * a base ligature in GPOS. Eg. if the sequence is: LAM,LAM,SHADDA,FATHA,HEH,
506 * and LAM,LAM,HEH for a ligature, they will leave SHADDA and FATHA wit a
507 * ligature id and component value of 2. Then if SHADDA,FATHA form a ligature
508 * later, we don't want them to lose their ligature id/component, otherwise
509 * GPOS will fail to correctly position the mark ligature on top of the
510 * LAM,LAM,HEH ligature. See:
511 * https://bugzilla.gnome.org/show_bug.cgi?id=676343
513 * - If a ligature is formed of components that some of which are also ligatures
514 * themselves, and those ligature components had marks attached to *their*
515 * components, we have to attach the marks to the new ligature component
516 * positions! Now *that*'s tricky! And these marks may be following the
517 * last component of the whole sequence, so we should loop forward looking
518 * for them and update them.
520 * Eg. the sequence is LAM,LAM,SHADDA,FATHA,HEH, and the font first forms a
521 * 'calt' ligature of LAM,HEH, leaving the SHADDA and FATHA with a ligature
522 * id and component == 1. Now, during 'liga', the LAM and the LAM-HEH ligature
523 * form a LAM-LAM-HEH ligature. We need to reassign the SHADDA and FATHA to
524 * the new ligature with a component value of 2.
526 * This in fact happened to a font... See:
527 * https://bugzilla.gnome.org/show_bug.cgi?id=437633
529 * - Ligatures cannot be formed across glyphs attached to different components
530 * of previous ligatures. Eg. the sequence is LAM,SHADDA,LAM,FATHA,HEH, and
531 * LAM,LAM,HEH form a ligature, leaving SHADDA,FATHA next to eachother.
532 * However, it would be wrong to ligate that SHADDA,FATHA sequence.o
533 * There is an exception to this: If a ligature tries ligating with marks that
534 * belong to it itself, go ahead, assuming that the font designer knows what
535 * they are doing (otherwise it can break Indic stuff when a matra wants to
536 * ligate with a conjunct...)
539 bool is_mark_ligature = !!(c->property & HB_OT_LAYOUT_GLYPH_CLASS_MARK);
541 unsigned int total_component_count = 0;
542 total_component_count += get_lig_num_comps (c->buffer->cur());
544 unsigned int first_lig_id = get_lig_id (c->buffer->cur());
545 unsigned int first_lig_comp = get_lig_comp (c->buffer->cur());
547 for (unsigned int i = 1; i < count; i++)
549 unsigned int property;
551 if (!skippy_iter.next (&property)) return TRACE_RETURN (false);
553 if (likely (c->buffer->info[skippy_iter.idx].codepoint != component[i])) return TRACE_RETURN (false);
555 unsigned int this_lig_id = get_lig_id (c->buffer->info[skippy_iter.idx]);
556 unsigned int this_lig_comp = get_lig_comp (c->buffer->info[skippy_iter.idx]);
558 if (first_lig_id && first_lig_comp) {
559 /* If first component was attached to a previous ligature component,
560 * all subsequent components should be attached to the same ligature
561 * component, otherwise we shouldn't ligate them. */
562 if (first_lig_id != this_lig_id || first_lig_comp != this_lig_comp)
563 return TRACE_RETURN (false);
565 /* If first component was NOT attached to a previous ligature component,
566 * all subsequent components should also NOT be attached to any ligature
567 * component, unless they are attached to the first component itself! */
568 if (this_lig_id && this_lig_comp && (this_lig_id != first_lig_id))
569 return TRACE_RETURN (false);
572 is_mark_ligature = is_mark_ligature && (property & HB_OT_LAYOUT_GLYPH_CLASS_MARK);
573 total_component_count += get_lig_num_comps (c->buffer->info[skippy_iter.idx]);
576 /* Deal, we are forming the ligature. */
577 c->buffer->merge_clusters (c->buffer->idx, skippy_iter.idx + 1);
579 unsigned int klass = is_mark_ligature ? 0 : HB_OT_LAYOUT_GLYPH_CLASS_LIGATURE;
580 unsigned int lig_id = is_mark_ligature ? 0 : allocate_lig_id (c->buffer);
581 unsigned int last_lig_id = get_lig_id (c->buffer->cur());
582 unsigned int last_num_components = get_lig_num_comps (c->buffer->cur());
583 unsigned int components_so_far = last_num_components;
585 if (!is_mark_ligature)
586 set_lig_props_for_ligature (c->buffer->cur(), lig_id, total_component_count);
587 c->replace_glyph (ligGlyph, klass);
589 for (unsigned int i = 1; i < count; i++)
591 while (c->should_mark_skip_current_glyph ())
593 if (!is_mark_ligature) {
594 unsigned int new_lig_comp = components_so_far - last_num_components +
595 MIN (MAX (get_lig_comp (c->buffer->cur()), 1u), last_num_components);
596 set_lig_props_for_mark (c->buffer->cur(), lig_id, new_lig_comp);
598 c->buffer->next_glyph ();
601 last_lig_id = get_lig_id (c->buffer->cur());
602 last_num_components = get_lig_num_comps (c->buffer->cur());
603 components_so_far += last_num_components;
605 /* Skip the base glyph */
609 if (!is_mark_ligature && last_lig_id) {
610 /* Re-adjust components for any marks following. */
611 for (unsigned int i = c->buffer->idx; i < c->buffer->len; i++) {
612 if (last_lig_id == get_lig_id (c->buffer->info[i])) {
613 unsigned int new_lig_comp = components_so_far - last_num_components +
614 MIN (MAX (get_lig_comp (c->buffer->info[i]), 1u), last_num_components);
615 set_lig_props_for_mark (c->buffer->info[i], lig_id, new_lig_comp);
621 return TRACE_RETURN (true);
625 inline bool sanitize (hb_sanitize_context_t *c) {
627 return TRACE_RETURN (ligGlyph.sanitize (c) && component.sanitize (c));
631 GlyphID ligGlyph; /* GlyphID of ligature to substitute */
632 HeadlessArrayOf<GlyphID>
633 component; /* Array of component GlyphIDs--start
634 * with the second component--ordered
635 * in writing direction */
637 DEFINE_SIZE_ARRAY (4, component);
642 friend struct LigatureSubstFormat1;
646 inline void closure (hb_closure_context_t *c) const
649 unsigned int num_ligs = ligature.len;
650 for (unsigned int i = 0; i < num_ligs; i++)
651 (this+ligature[i]).closure (c);
654 inline bool would_apply (hb_would_apply_context_t *c) const
656 unsigned int num_ligs = ligature.len;
657 for (unsigned int i = 0; i < num_ligs; i++)
659 const Ligature &lig = this+ligature[i];
660 if (lig.would_apply (c))
666 inline bool apply (hb_apply_context_t *c) const
669 unsigned int num_ligs = ligature.len;
670 for (unsigned int i = 0; i < num_ligs; i++)
672 const Ligature &lig = this+ligature[i];
673 if (lig.apply (c)) return TRACE_RETURN (true);
676 return TRACE_RETURN (false);
680 inline bool sanitize (hb_sanitize_context_t *c) {
682 return TRACE_RETURN (ligature.sanitize (c, this));
686 OffsetArrayOf<Ligature>
687 ligature; /* Array LigatureSet tables
688 * ordered by preference */
690 DEFINE_SIZE_ARRAY (2, ligature);
693 struct LigatureSubstFormat1
695 friend struct LigatureSubst;
699 inline void closure (hb_closure_context_t *c) const
703 for (iter.init (this+coverage); iter.more (); iter.next ()) {
704 if (c->glyphs->has (iter.get_glyph ()))
705 (this+ligatureSet[iter.get_coverage ()]).closure (c);
709 inline const Coverage &get_coverage (void) const
711 return this+coverage;
714 inline bool would_apply (hb_would_apply_context_t *c) const
716 return (this+ligatureSet[(this+coverage) (c->glyphs[0])]).would_apply (c);
719 inline bool apply (hb_apply_context_t *c) const
722 hb_codepoint_t glyph_id = c->buffer->cur().codepoint;
724 unsigned int index = (this+coverage) (glyph_id);
725 if (likely (index == NOT_COVERED)) return TRACE_RETURN (false);
727 const LigatureSet &lig_set = this+ligatureSet[index];
728 return TRACE_RETURN (lig_set.apply (c));
731 inline bool sanitize (hb_sanitize_context_t *c) {
733 return TRACE_RETURN (coverage.sanitize (c, this) && ligatureSet.sanitize (c, this));
737 USHORT format; /* Format identifier--format = 1 */
739 coverage; /* Offset to Coverage table--from
740 * beginning of Substitution table */
741 OffsetArrayOf<LigatureSet>
742 ligatureSet; /* Array LigatureSet tables
743 * ordered by Coverage Index */
745 DEFINE_SIZE_ARRAY (6, ligatureSet);
750 friend struct SubstLookupSubTable;
754 inline void closure (hb_closure_context_t *c) const
758 case 1: u.format1.closure (c); break;
763 inline const Coverage &get_coverage (void) const
766 case 1: return u.format1.get_coverage ();
767 default:return Null(Coverage);
771 inline bool would_apply (hb_would_apply_context_t *c) const
774 case 1: return u.format1.would_apply (c);
775 default:return false;
779 inline bool apply (hb_apply_context_t *c) const
783 case 1: return TRACE_RETURN (u.format1.apply (c));
784 default:return TRACE_RETURN (false);
788 inline bool sanitize (hb_sanitize_context_t *c) {
790 if (!u.format.sanitize (c)) return TRACE_RETURN (false);
792 case 1: return TRACE_RETURN (u.format1.sanitize (c));
793 default:return TRACE_RETURN (true);
799 USHORT format; /* Format identifier */
800 LigatureSubstFormat1 format1;
805 static inline bool substitute_lookup (hb_apply_context_t *c, unsigned int lookup_index);
806 static inline void closure_lookup (hb_closure_context_t *c, unsigned int lookup_index);
808 struct ContextSubst : Context
810 friend struct SubstLookupSubTable;
814 inline void closure (hb_closure_context_t *c) const
817 return Context::closure (c, closure_lookup);
820 inline bool apply (hb_apply_context_t *c) const
823 return TRACE_RETURN (Context::apply (c, substitute_lookup));
827 struct ChainContextSubst : ChainContext
829 friend struct SubstLookupSubTable;
833 inline void closure (hb_closure_context_t *c) const
836 return ChainContext::closure (c, closure_lookup);
839 inline bool apply (hb_apply_context_t *c) const
842 return TRACE_RETURN (ChainContext::apply (c, substitute_lookup));
847 struct ExtensionSubst : Extension
849 friend struct SubstLookupSubTable;
850 friend struct SubstLookup;
853 inline const struct SubstLookupSubTable& get_subtable (void) const
855 unsigned int offset = get_offset ();
856 if (unlikely (!offset)) return Null(SubstLookupSubTable);
857 return StructAtOffset<SubstLookupSubTable> (this, offset);
860 inline void closure (hb_closure_context_t *c) const;
862 inline const Coverage &get_coverage (void) const;
864 inline bool would_apply (hb_would_apply_context_t *c) const;
866 inline bool apply (hb_apply_context_t *c) const;
868 inline bool sanitize (hb_sanitize_context_t *c);
870 inline bool is_reverse (void) const;
874 struct ReverseChainSingleSubstFormat1
876 friend struct ReverseChainSingleSubst;
880 inline void closure (hb_closure_context_t *c) const
883 const OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
887 count = backtrack.len;
888 for (unsigned int i = 0; i < count; i++)
889 if (!(this+backtrack[i]).intersects (c->glyphs))
892 count = lookahead.len;
893 for (unsigned int i = 0; i < count; i++)
894 if (!(this+lookahead[i]).intersects (c->glyphs))
897 const ArrayOf<GlyphID> &substitute = StructAfter<ArrayOf<GlyphID> > (lookahead);
899 for (iter.init (this+coverage); iter.more (); iter.next ()) {
900 if (c->glyphs->has (iter.get_glyph ()))
901 c->glyphs->add (substitute[iter.get_coverage ()]);
905 inline const Coverage &get_coverage (void) const
907 return this+coverage;
910 inline bool apply (hb_apply_context_t *c) const
913 if (unlikely (c->nesting_level_left != MAX_NESTING_LEVEL))
914 return TRACE_RETURN (false); /* No chaining to this type */
916 unsigned int index = (this+coverage) (c->buffer->cur().codepoint);
917 if (likely (index == NOT_COVERED)) return TRACE_RETURN (false);
919 const OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
920 const ArrayOf<GlyphID> &substitute = StructAfter<ArrayOf<GlyphID> > (lookahead);
922 if (match_backtrack (c,
923 backtrack.len, (USHORT *) backtrack.array,
924 match_coverage, this) &&
926 lookahead.len, (USHORT *) lookahead.array,
927 match_coverage, this,
930 c->replace_glyph_inplace (substitute[index]);
931 c->buffer->idx--; /* Reverse! */
932 return TRACE_RETURN (true);
935 return TRACE_RETURN (false);
938 inline bool sanitize (hb_sanitize_context_t *c) {
940 if (!(coverage.sanitize (c, this) && backtrack.sanitize (c, this)))
941 return TRACE_RETURN (false);
942 OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
943 if (!lookahead.sanitize (c, this))
944 return TRACE_RETURN (false);
945 ArrayOf<GlyphID> &substitute = StructAfter<ArrayOf<GlyphID> > (lookahead);
946 return TRACE_RETURN (substitute.sanitize (c));
950 USHORT format; /* Format identifier--format = 1 */
952 coverage; /* Offset to Coverage table--from
953 * beginning of table */
954 OffsetArrayOf<Coverage>
955 backtrack; /* Array of coverage tables
956 * in backtracking sequence, in glyph
958 OffsetArrayOf<Coverage>
959 lookaheadX; /* Array of coverage tables
960 * in lookahead sequence, in glyph
963 substituteX; /* Array of substitute
964 * GlyphIDs--ordered by Coverage Index */
966 DEFINE_SIZE_MIN (10);
969 struct ReverseChainSingleSubst
971 friend struct SubstLookupSubTable;
975 inline void closure (hb_closure_context_t *c) const
979 case 1: u.format1.closure (c); break;
984 inline const Coverage &get_coverage (void) const
987 case 1: return u.format1.get_coverage ();
988 default:return Null(Coverage);
992 inline bool apply (hb_apply_context_t *c) const
996 case 1: return TRACE_RETURN (u.format1.apply (c));
997 default:return TRACE_RETURN (false);
1001 inline bool sanitize (hb_sanitize_context_t *c) {
1003 if (!u.format.sanitize (c)) return TRACE_RETURN (false);
1005 case 1: return TRACE_RETURN (u.format1.sanitize (c));
1006 default:return TRACE_RETURN (true);
1012 USHORT format; /* Format identifier */
1013 ReverseChainSingleSubstFormat1 format1;
1023 struct SubstLookupSubTable
1025 friend struct SubstLookup;
1035 ReverseChainSingle = 8
1038 inline void closure (hb_closure_context_t *c,
1039 unsigned int lookup_type) const
1042 switch (lookup_type) {
1043 case Single: u.single.closure (c); break;
1044 case Multiple: u.multiple.closure (c); break;
1045 case Alternate: u.alternate.closure (c); break;
1046 case Ligature: u.ligature.closure (c); break;
1047 case Context: u.context.closure (c); break;
1048 case ChainContext: u.chainContext.closure (c); break;
1049 case Extension: u.extension.closure (c); break;
1050 case ReverseChainSingle: u.reverseChainContextSingle.closure (c); break;
1055 inline const Coverage &get_coverage (unsigned int lookup_type) const
1057 switch (lookup_type) {
1058 case Single: return u.single.get_coverage ();
1059 case Multiple: return u.multiple.get_coverage ();
1060 case Alternate: return u.alternate.get_coverage ();
1061 case Ligature: return u.ligature.get_coverage ();
1062 case Context: return u.context.get_coverage ();
1063 case ChainContext: return u.chainContext.get_coverage ();
1064 case Extension: return u.extension.get_coverage ();
1065 case ReverseChainSingle: return u.reverseChainContextSingle.get_coverage ();
1066 default: return Null(Coverage);
1070 inline bool would_apply (hb_would_apply_context_t *c,
1071 unsigned int lookup_type) const
1073 TRACE_WOULD_APPLY ();
1074 if (get_coverage (lookup_type).get_coverage (c->glyphs[0]) == NOT_COVERED) return false;
1076 switch (lookup_type) {
1080 case ReverseChainSingle:
1085 /* Only need to look further for lookups that support substitutions
1086 * of input longer than 1. */
1087 switch (lookup_type) {
1088 case Ligature: return u.ligature.would_apply (c);
1089 case Context: return u.context.would_apply (c);
1090 case ChainContext: return u.chainContext.would_apply (c);
1091 case Extension: return u.extension.would_apply (c);
1092 default: return false;
1096 inline bool apply (hb_apply_context_t *c, unsigned int lookup_type) const
1099 switch (lookup_type) {
1100 case Single: return TRACE_RETURN (u.single.apply (c));
1101 case Multiple: return TRACE_RETURN (u.multiple.apply (c));
1102 case Alternate: return TRACE_RETURN (u.alternate.apply (c));
1103 case Ligature: return TRACE_RETURN (u.ligature.apply (c));
1104 case Context: return TRACE_RETURN (u.context.apply (c));
1105 case ChainContext: return TRACE_RETURN (u.chainContext.apply (c));
1106 case Extension: return TRACE_RETURN (u.extension.apply (c));
1107 case ReverseChainSingle: return TRACE_RETURN (u.reverseChainContextSingle.apply (c));
1108 default: return TRACE_RETURN (false);
1112 inline bool sanitize (hb_sanitize_context_t *c, unsigned int lookup_type) {
1114 if (!u.header.sub_format.sanitize (c))
1115 return TRACE_RETURN (false);
1116 switch (lookup_type) {
1117 case Single: return TRACE_RETURN (u.single.sanitize (c));
1118 case Multiple: return TRACE_RETURN (u.multiple.sanitize (c));
1119 case Alternate: return TRACE_RETURN (u.alternate.sanitize (c));
1120 case Ligature: return TRACE_RETURN (u.ligature.sanitize (c));
1121 case Context: return TRACE_RETURN (u.context.sanitize (c));
1122 case ChainContext: return TRACE_RETURN (u.chainContext.sanitize (c));
1123 case Extension: return TRACE_RETURN (u.extension.sanitize (c));
1124 case ReverseChainSingle: return TRACE_RETURN (u.reverseChainContextSingle.sanitize (c));
1125 default: return TRACE_RETURN (true);
1135 MultipleSubst multiple;
1136 AlternateSubst alternate;
1137 LigatureSubst ligature;
1138 ContextSubst context;
1139 ChainContextSubst chainContext;
1140 ExtensionSubst extension;
1141 ReverseChainSingleSubst reverseChainContextSingle;
1144 DEFINE_SIZE_UNION (2, header.sub_format);
1148 struct SubstLookup : Lookup
1150 inline const SubstLookupSubTable& get_subtable (unsigned int i) const
1151 { return this+CastR<OffsetArrayOf<SubstLookupSubTable> > (subTable)[i]; }
1153 inline static bool lookup_type_is_reverse (unsigned int lookup_type)
1154 { return lookup_type == SubstLookupSubTable::ReverseChainSingle; }
1156 inline bool is_reverse (void) const
1158 unsigned int type = get_type ();
1159 if (unlikely (type == SubstLookupSubTable::Extension))
1160 return CastR<ExtensionSubst> (get_subtable(0)).is_reverse ();
1161 return lookup_type_is_reverse (type);
1164 inline void closure (hb_closure_context_t *c) const
1166 unsigned int lookup_type = get_type ();
1167 unsigned int count = get_subtable_count ();
1168 for (unsigned int i = 0; i < count; i++)
1169 get_subtable (i).closure (c, lookup_type);
1172 template <typename set_t>
1173 inline void add_coverage (set_t *glyphs) const
1175 const Coverage *last = NULL;
1176 unsigned int count = get_subtable_count ();
1177 for (unsigned int i = 0; i < count; i++) {
1178 const Coverage *c = &get_subtable (i).get_coverage (get_type ());
1180 c->add_coverage (glyphs);
1186 inline bool would_apply (hb_would_apply_context_t *c) const
1188 if (unlikely (!c->len)) return false;
1189 if (!c->digest.may_have (c->glyphs[0])) return false;
1190 unsigned int lookup_type = get_type ();
1191 unsigned int count = get_subtable_count ();
1192 for (unsigned int i = 0; i < count; i++)
1193 if (get_subtable (i).would_apply (c, lookup_type))
1198 inline bool apply_once (hb_apply_context_t *c) const
1200 unsigned int lookup_type = get_type ();
1202 if (!c->check_glyph_property (&c->buffer->cur(), c->lookup_props, &c->property))
1205 unsigned int count = get_subtable_count ();
1206 for (unsigned int i = 0; i < count; i++)
1207 if (get_subtable (i).apply (c, lookup_type))
1213 inline bool apply_string (hb_apply_context_t *c) const
1217 if (unlikely (!c->buffer->len))
1220 c->set_lookup (*this);
1222 if (likely (!is_reverse ()))
1224 /* in/out forward substitution */
1225 c->buffer->clear_output ();
1228 while (c->buffer->idx < c->buffer->len)
1230 if ((c->buffer->cur().mask & c->lookup_mask) &&
1231 c->digest.may_have (c->buffer->cur().codepoint) &&
1235 c->buffer->next_glyph ();
1238 c->buffer->swap_buffers ();
1242 /* in-place backward substitution */
1243 c->buffer->idx = c->buffer->len - 1;
1246 if ((c->buffer->cur().mask & c->lookup_mask) &&
1247 c->digest.may_have (c->buffer->cur().codepoint) &&
1254 while ((int) c->buffer->idx >= 0);
1260 inline bool sanitize (hb_sanitize_context_t *c) {
1262 if (unlikely (!Lookup::sanitize (c))) return TRACE_RETURN (false);
1263 OffsetArrayOf<SubstLookupSubTable> &list = CastR<OffsetArrayOf<SubstLookupSubTable> > (subTable);
1264 if (unlikely (!list.sanitize (c, this, get_type ()))) return TRACE_RETURN (false);
1266 if (unlikely (get_type () == SubstLookupSubTable::Extension))
1268 /* The spec says all subtables of an Extension lookup should
1269 * have the same type. This is specially important if one has
1272 * We just check that they are all either forward, or reverse. */
1273 unsigned int type = get_subtable (0).u.extension.get_type ();
1274 unsigned int count = get_subtable_count ();
1275 for (unsigned int i = 1; i < count; i++)
1276 if (get_subtable (i).u.extension.get_type () != type)
1277 return TRACE_RETURN (false);
1279 return TRACE_RETURN (true);
1283 typedef OffsetListOf<SubstLookup> SubstLookupList;
1286 * GSUB -- The Glyph Substitution Table
1289 struct GSUB : GSUBGPOS
1291 static const hb_tag_t Tag = HB_OT_TAG_GSUB;
1293 inline const SubstLookup& get_lookup (unsigned int i) const
1294 { return CastR<SubstLookup> (GSUBGPOS::get_lookup (i)); }
1296 template <typename set_t>
1297 inline void add_coverage (set_t *glyphs, unsigned int lookup_index) const
1298 { get_lookup (lookup_index).add_coverage (glyphs); }
1300 inline bool would_substitute_lookup (hb_would_apply_context_t *c, unsigned int lookup_index) const
1301 { return get_lookup (lookup_index).would_apply (c); }
1303 inline bool substitute_lookup (hb_apply_context_t *c, unsigned int lookup_index) const
1304 { return get_lookup (lookup_index).apply_string (c); }
1306 static inline void substitute_start (hb_font_t *font, hb_buffer_t *buffer);
1307 static inline void substitute_finish (hb_font_t *font, hb_buffer_t *buffer);
1309 inline void closure_lookup (hb_closure_context_t *c,
1310 unsigned int lookup_index) const
1311 { return get_lookup (lookup_index).closure (c); }
1313 inline bool sanitize (hb_sanitize_context_t *c) {
1315 if (unlikely (!GSUBGPOS::sanitize (c))) return TRACE_RETURN (false);
1316 OffsetTo<SubstLookupList> &list = CastR<OffsetTo<SubstLookupList> > (lookupList);
1317 return TRACE_RETURN (list.sanitize (c, this));
1320 DEFINE_SIZE_STATIC (10);
1325 GSUB::substitute_start (hb_font_t *font, hb_buffer_t *buffer)
1327 HB_BUFFER_ALLOCATE_VAR (buffer, glyph_props);
1328 HB_BUFFER_ALLOCATE_VAR (buffer, lig_props);
1329 HB_BUFFER_ALLOCATE_VAR (buffer, syllable);
1331 const GDEF &gdef = *hb_ot_layout_from_face (font->face)->gdef;
1332 unsigned int count = buffer->len;
1333 for (unsigned int i = 0; i < count; i++) {
1334 buffer->info[i].lig_props() = buffer->info[i].syllable() = 0;
1335 buffer->info[i].glyph_props() = gdef.get_glyph_props (buffer->info[i].codepoint);
1340 GSUB::substitute_finish (hb_font_t *font HB_UNUSED, hb_buffer_t *buffer HB_UNUSED)
1345 /* Out-of-class implementation for methods recursing */
1347 inline void ExtensionSubst::closure (hb_closure_context_t *c) const
1349 get_subtable ().closure (c, get_type ());
1352 inline const Coverage & ExtensionSubst::get_coverage (void) const
1354 return get_subtable ().get_coverage (get_type ());
1357 inline bool ExtensionSubst::would_apply (hb_would_apply_context_t *c) const
1359 return get_subtable ().would_apply (c, get_type ());
1362 inline bool ExtensionSubst::apply (hb_apply_context_t *c) const
1365 return TRACE_RETURN (get_subtable ().apply (c, get_type ()));
1368 inline bool ExtensionSubst::sanitize (hb_sanitize_context_t *c)
1371 if (unlikely (!Extension::sanitize (c))) return TRACE_RETURN (false);
1372 unsigned int offset = get_offset ();
1373 if (unlikely (!offset)) return TRACE_RETURN (true);
1374 return TRACE_RETURN (StructAtOffset<SubstLookupSubTable> (this, offset).sanitize (c, get_type ()));
1377 inline bool ExtensionSubst::is_reverse (void) const
1379 unsigned int type = get_type ();
1380 if (unlikely (type == SubstLookupSubTable::Extension))
1381 return CastR<ExtensionSubst> (get_subtable()).is_reverse ();
1382 return SubstLookup::lookup_type_is_reverse (type);
1385 static inline void closure_lookup (hb_closure_context_t *c, unsigned int lookup_index)
1387 const GSUB &gsub = *(hb_ot_layout_from_face (c->face)->gsub);
1388 const SubstLookup &l = gsub.get_lookup (lookup_index);
1390 if (unlikely (c->nesting_level_left == 0))
1393 c->nesting_level_left--;
1395 c->nesting_level_left++;
1398 static inline bool substitute_lookup (hb_apply_context_t *c, unsigned int lookup_index)
1400 const GSUB &gsub = *(hb_ot_layout_from_face (c->face)->gsub);
1401 const SubstLookup &l = gsub.get_lookup (lookup_index);
1403 if (unlikely (c->nesting_level_left == 0))
1406 hb_apply_context_t new_c (*c);
1407 new_c.nesting_level_left--;
1408 new_c.set_lookup (l);
1409 return l.apply_once (&new_c);
1414 #endif /* HB_OT_LAYOUT_GSUB_TABLE_HH */