2 * Copyright (C) 2007,2008,2009,2010 Red Hat, Inc.
4 * This is part of HarfBuzz, a text shaping library.
6 * Permission is hereby granted, without written agreement and without
7 * license or royalty fees, to use, copy, modify, and distribute this
8 * software and its documentation for any purpose, provided that the
9 * above copyright notice and the following two paragraphs appear in
10 * all copies of this software.
12 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
18 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
21 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
24 * Red Hat Author(s): Behdad Esfahbod
27 #ifndef HB_OT_LAYOUT_GSUB_PRIVATE_HH
28 #define HB_OT_LAYOUT_GSUB_PRIVATE_HH
30 #include "hb-ot-layout-gsubgpos-private.hh"
35 struct SingleSubstFormat1
37 friend struct SingleSubst;
41 inline bool apply (hb_apply_context_t *c) const
44 hb_codepoint_t glyph_id = c->buffer->info[c->buffer->i].codepoint;
45 unsigned int index = (this+coverage) (glyph_id);
46 if (likely (index == NOT_COVERED))
49 glyph_id += deltaGlyphID;
50 c->buffer->replace_glyph (glyph_id);
52 /* We inherit the old glyph class to the substituted glyph */
53 if (_hb_ot_layout_has_new_glyph_classes (c->layout->face))
54 _hb_ot_layout_set_glyph_property (c->layout->face, glyph_id, c->property);
59 inline bool sanitize (hb_sanitize_context_t *c) {
61 return coverage.sanitize (c, this)
62 && deltaGlyphID.sanitize (c);
66 USHORT format; /* Format identifier--format = 1 */
68 coverage; /* Offset to Coverage table--from
69 * beginning of Substitution table */
70 SHORT deltaGlyphID; /* Add to original GlyphID to get
71 * substitute GlyphID */
73 DEFINE_SIZE_STATIC (6);
76 struct SingleSubstFormat2
78 friend struct SingleSubst;
82 inline bool apply (hb_apply_context_t *c) const
85 hb_codepoint_t glyph_id = c->buffer->info[c->buffer->i].codepoint;
86 unsigned int index = (this+coverage) (glyph_id);
87 if (likely (index == NOT_COVERED))
90 if (unlikely (index >= substitute.len))
93 glyph_id = substitute[index];
94 c->buffer->replace_glyph (glyph_id);
96 /* We inherit the old glyph class to the substituted glyph */
97 if (_hb_ot_layout_has_new_glyph_classes (c->layout->face))
98 _hb_ot_layout_set_glyph_property (c->layout->face, glyph_id, c->property);
103 inline bool sanitize (hb_sanitize_context_t *c) {
105 return coverage.sanitize (c, this)
106 && substitute.sanitize (c);
110 USHORT format; /* Format identifier--format = 2 */
112 coverage; /* Offset to Coverage table--from
113 * beginning of Substitution table */
115 substitute; /* Array of substitute
116 * GlyphIDs--ordered by Coverage Index */
118 DEFINE_SIZE_ARRAY (6, substitute);
123 friend struct SubstLookupSubTable;
127 inline bool apply (hb_apply_context_t *c) const
131 case 1: return u.format1.apply (c);
132 case 2: return u.format2.apply (c);
133 default:return false;
137 inline bool sanitize (hb_sanitize_context_t *c) {
139 if (!u.format.sanitize (c)) return false;
141 case 1: return u.format1.sanitize (c);
142 case 2: return u.format2.sanitize (c);
149 USHORT format; /* Format identifier */
150 SingleSubstFormat1 format1;
151 SingleSubstFormat2 format2;
158 friend struct MultipleSubstFormat1;
161 inline bool apply (hb_apply_context_t *c) const
164 if (unlikely (!substitute.len))
167 c->buffer->add_output_glyphs_be16 (1,
168 substitute.len, (const uint16_t *) substitute.array,
171 /* This is a guess only ... */
172 if (_hb_ot_layout_has_new_glyph_classes (c->layout->face))
174 unsigned int property = c->property;
175 if (property == HB_OT_LAYOUT_GLYPH_CLASS_LIGATURE)
176 property = HB_OT_LAYOUT_GLYPH_CLASS_BASE_GLYPH;
178 unsigned int count = substitute.len;
179 for (unsigned int n = 0; n < count; n++)
180 _hb_ot_layout_set_glyph_property (c->layout->face, substitute[n], property);
187 inline bool sanitize (hb_sanitize_context_t *c) {
189 return substitute.sanitize (c);
194 substitute; /* String of GlyphIDs to substitute */
196 DEFINE_SIZE_ARRAY (2, substitute);
199 struct MultipleSubstFormat1
201 friend struct MultipleSubst;
205 inline bool apply (hb_apply_context_t *c) const
209 unsigned int index = (this+coverage) (c->buffer->info[c->buffer->i].codepoint);
210 if (likely (index == NOT_COVERED))
213 return (this+sequence[index]).apply (c);
216 inline bool sanitize (hb_sanitize_context_t *c) {
218 return coverage.sanitize (c, this)
219 && sequence.sanitize (c, this);
223 USHORT format; /* Format identifier--format = 1 */
225 coverage; /* Offset to Coverage table--from
226 * beginning of Substitution table */
227 OffsetArrayOf<Sequence>
228 sequence; /* Array of Sequence tables
229 * ordered by Coverage Index */
231 DEFINE_SIZE_ARRAY (6, sequence);
236 friend struct SubstLookupSubTable;
240 inline bool apply (hb_apply_context_t *c) const
244 case 1: return u.format1.apply (c);
245 default:return false;
249 inline bool sanitize (hb_sanitize_context_t *c) {
251 if (!u.format.sanitize (c)) return false;
253 case 1: return u.format1.sanitize (c);
260 USHORT format; /* Format identifier */
261 MultipleSubstFormat1 format1;
266 typedef ArrayOf<GlyphID> AlternateSet; /* Array of alternate GlyphIDs--in
269 struct AlternateSubstFormat1
271 friend struct AlternateSubst;
275 inline bool apply (hb_apply_context_t *c) const
278 hb_codepoint_t glyph_id = c->buffer->info[c->buffer->i].codepoint;
279 hb_mask_t glyph_mask = c->buffer->info[c->buffer->i].mask;
280 hb_mask_t lookup_mask = c->lookup_mask;
282 unsigned int index = (this+coverage) (glyph_id);
283 if (likely (index == NOT_COVERED))
286 const AlternateSet &alt_set = this+alternateSet[index];
288 if (unlikely (!alt_set.len))
291 /* Note: This breaks badly if two features enabled this lookup together. */
292 unsigned int shift = _hb_ctz (lookup_mask);
293 unsigned int alt_index = ((lookup_mask & glyph_mask) >> shift);
295 if (unlikely (alt_index > alt_set.len || alt_index == 0))
298 glyph_id = alt_set[alt_index - 1];
300 c->buffer->replace_glyph (glyph_id);
302 /* We inherit the old glyph class to the substituted glyph */
303 if (_hb_ot_layout_has_new_glyph_classes (c->layout->face))
304 _hb_ot_layout_set_glyph_property (c->layout->face, glyph_id, c->property);
309 inline bool sanitize (hb_sanitize_context_t *c) {
311 return coverage.sanitize (c, this)
312 && alternateSet.sanitize (c, this);
316 USHORT format; /* Format identifier--format = 1 */
318 coverage; /* Offset to Coverage table--from
319 * beginning of Substitution table */
320 OffsetArrayOf<AlternateSet>
321 alternateSet; /* Array of AlternateSet tables
322 * ordered by Coverage Index */
324 DEFINE_SIZE_ARRAY (6, alternateSet);
327 struct AlternateSubst
329 friend struct SubstLookupSubTable;
333 inline bool apply (hb_apply_context_t *c) const
337 case 1: return u.format1.apply (c);
338 default:return false;
342 inline bool sanitize (hb_sanitize_context_t *c) {
344 if (!u.format.sanitize (c)) return false;
346 case 1: return u.format1.sanitize (c);
353 USHORT format; /* Format identifier */
354 AlternateSubstFormat1 format1;
361 friend struct LigatureSet;
364 inline bool apply (hb_apply_context_t *c, bool is_mark) const
368 unsigned int count = component.len;
369 unsigned int end = MIN (c->buffer->len, c->buffer->i + c->context_length);
370 if (unlikely (c->buffer->i + count > end))
373 for (i = 1, j = c->buffer->i + 1; i < count; i++, j++)
375 unsigned int property;
376 while (_hb_ot_layout_skip_mark (c->layout->face, &c->buffer->info[j], c->lookup_flag, &property))
378 if (unlikely (j + count - i == end))
383 if (!(property & HB_OT_LAYOUT_GLYPH_CLASS_MARK))
386 if (likely (c->buffer->info[j].codepoint != component[i]))
389 /* This is just a guess ... */
390 if (_hb_ot_layout_has_new_glyph_classes (c->layout->face))
391 _hb_ot_layout_set_glyph_class (c->layout->face, ligGlyph,
392 is_mark ? HB_OT_LAYOUT_GLYPH_CLASS_MARK
393 : HB_OT_LAYOUT_GLYPH_CLASS_LIGATURE);
395 if (j == c->buffer->i + i) /* No input glyphs skipped */
396 c->buffer->add_output_glyphs_be16 (i,
397 1, (const uint16_t *) &ligGlyph,
399 c->buffer->allocate_lig_id ());
402 unsigned int lig_id = c->buffer->allocate_lig_id ();
403 c->buffer->add_output_glyph (ligGlyph, 0, lig_id);
405 /* Now we must do a second loop to copy the skipped glyphs to
406 `out' and assign component values to it. We start with the
407 glyph after the first component. Glyphs between component
408 i and i+1 belong to component i. Together with the lig_id
409 value it is later possible to check whether a specific
410 component value really belongs to a given ligature. */
412 for ( i = 1; i < count; i++ )
414 while (_hb_ot_layout_skip_mark (c->layout->face, &c->buffer->info[c->buffer->i], c->lookup_flag, NULL))
415 c->buffer->add_output_glyph (c->buffer->info[c->buffer->i].codepoint, i, lig_id);
417 /* Skip the base glyph */
426 inline bool sanitize (hb_sanitize_context_t *c) {
428 return ligGlyph.sanitize (c)
429 && component.sanitize (c);
433 GlyphID ligGlyph; /* GlyphID of ligature to substitute */
434 HeadlessArrayOf<GlyphID>
435 component; /* Array of component GlyphIDs--start
436 * with the second component--ordered
437 * in writing direction */
439 DEFINE_SIZE_ARRAY (4, component);
444 friend struct LigatureSubstFormat1;
447 inline bool apply (hb_apply_context_t *c, bool is_mark) const
450 unsigned int num_ligs = ligature.len;
451 for (unsigned int i = 0; i < num_ligs; i++)
453 const Ligature &lig = this+ligature[i];
454 if (lig.apply (c, is_mark))
462 inline bool sanitize (hb_sanitize_context_t *c) {
464 return ligature.sanitize (c, this);
468 OffsetArrayOf<Ligature>
469 ligature; /* Array LigatureSet tables
470 * ordered by preference */
472 DEFINE_SIZE_ARRAY (2, ligature);
475 struct LigatureSubstFormat1
477 friend struct LigatureSubst;
480 inline bool apply (hb_apply_context_t *c) const
483 hb_codepoint_t glyph_id = c->buffer->info[c->buffer->i].codepoint;
485 bool first_is_mark = !!(c->property & HB_OT_LAYOUT_GLYPH_CLASS_MARK);
487 unsigned int index = (this+coverage) (glyph_id);
488 if (likely (index == NOT_COVERED))
491 const LigatureSet &lig_set = this+ligatureSet[index];
492 return lig_set.apply (c, first_is_mark);
495 inline bool sanitize (hb_sanitize_context_t *c) {
497 return coverage.sanitize (c, this)
498 && ligatureSet.sanitize (c, this);
502 USHORT format; /* Format identifier--format = 1 */
504 coverage; /* Offset to Coverage table--from
505 * beginning of Substitution table */
506 OffsetArrayOf<LigatureSet>
507 ligatureSet; /* Array LigatureSet tables
508 * ordered by Coverage Index */
510 DEFINE_SIZE_ARRAY (6, ligatureSet);
515 friend struct SubstLookupSubTable;
518 inline bool apply (hb_apply_context_t *c) const
522 case 1: return u.format1.apply (c);
523 default:return false;
527 inline bool sanitize (hb_sanitize_context_t *c) {
529 if (!u.format.sanitize (c)) return false;
531 case 1: return u.format1.sanitize (c);
538 USHORT format; /* Format identifier */
539 LigatureSubstFormat1 format1;
545 static inline bool substitute_lookup (hb_apply_context_t *c, unsigned int lookup_index);
548 struct ContextSubst : Context
550 friend struct SubstLookupSubTable;
553 inline bool apply (hb_apply_context_t *c) const
556 return Context::apply (c, substitute_lookup);
560 struct ChainContextSubst : ChainContext
562 friend struct SubstLookupSubTable;
565 inline bool apply (hb_apply_context_t *c) const
568 return ChainContext::apply (c, substitute_lookup);
573 struct ExtensionSubst : Extension
575 friend struct SubstLookupSubTable;
576 friend struct SubstLookup;
579 inline const struct SubstLookupSubTable& get_subtable (void) const
581 unsigned int offset = get_offset ();
582 if (unlikely (!offset)) return Null(SubstLookupSubTable);
583 return StructAtOffset<SubstLookupSubTable> (this, offset);
586 inline bool apply (hb_apply_context_t *c) const;
588 inline bool sanitize (hb_sanitize_context_t *c);
590 inline bool is_reverse (void) const;
594 struct ReverseChainSingleSubstFormat1
596 friend struct ReverseChainSingleSubst;
599 inline bool apply (hb_apply_context_t *c) const
602 if (unlikely (c->context_length != NO_CONTEXT))
603 return false; /* No chaining to this type */
605 unsigned int index = (this+coverage) (c->buffer->info[c->buffer->i].codepoint);
606 if (likely (index == NOT_COVERED))
609 const OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
610 const ArrayOf<GlyphID> &substitute = StructAfter<ArrayOf<GlyphID> > (lookahead);
612 if (match_backtrack (c,
613 backtrack.len, (USHORT *) backtrack.array,
614 match_coverage, this) &&
616 lookahead.len, (USHORT *) lookahead.array,
617 match_coverage, this,
620 c->buffer->info[c->buffer->i].codepoint = substitute[index];
621 c->buffer->i--; /* Reverse! */
628 inline bool sanitize (hb_sanitize_context_t *c) {
630 if (!(coverage.sanitize (c, this)
631 && backtrack.sanitize (c, this)))
633 OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
634 if (!lookahead.sanitize (c, this))
636 ArrayOf<GlyphID> &substitute = StructAfter<ArrayOf<GlyphID> > (lookahead);
637 return substitute.sanitize (c);
641 USHORT format; /* Format identifier--format = 1 */
643 coverage; /* Offset to Coverage table--from
644 * beginning of table */
645 OffsetArrayOf<Coverage>
646 backtrack; /* Array of coverage tables
647 * in backtracking sequence, in glyph
649 OffsetArrayOf<Coverage>
650 lookaheadX; /* Array of coverage tables
651 * in lookahead sequence, in glyph
654 substituteX; /* Array of substitute
655 * GlyphIDs--ordered by Coverage Index */
657 DEFINE_SIZE_MIN (10);
660 struct ReverseChainSingleSubst
662 friend struct SubstLookupSubTable;
665 inline bool apply (hb_apply_context_t *c) const
669 case 1: return u.format1.apply (c);
670 default:return false;
674 inline bool sanitize (hb_sanitize_context_t *c) {
676 if (!u.format.sanitize (c)) return false;
678 case 1: return u.format1.sanitize (c);
685 USHORT format; /* Format identifier */
686 ReverseChainSingleSubstFormat1 format1;
696 struct SubstLookupSubTable
698 friend struct SubstLookup;
708 ReverseChainSingle = 8
711 inline bool apply (hb_apply_context_t *c, unsigned int lookup_type) const
714 switch (lookup_type) {
715 case Single: return u.single.apply (c);
716 case Multiple: return u.multiple.apply (c);
717 case Alternate: return u.alternate.apply (c);
718 case Ligature: return u.ligature.apply (c);
719 case Context: return u.c.apply (c);
720 case ChainContext: return u.chainContext.apply (c);
721 case Extension: return u.extension.apply (c);
722 case ReverseChainSingle: return u.reverseChainContextSingle.apply (c);
723 default:return false;
727 inline bool sanitize (hb_sanitize_context_t *c, unsigned int lookup_type) {
729 switch (lookup_type) {
730 case Single: return u.single.sanitize (c);
731 case Multiple: return u.multiple.sanitize (c);
732 case Alternate: return u.alternate.sanitize (c);
733 case Ligature: return u.ligature.sanitize (c);
734 case Context: return u.c.sanitize (c);
735 case ChainContext: return u.chainContext.sanitize (c);
736 case Extension: return u.extension.sanitize (c);
737 case ReverseChainSingle: return u.reverseChainContextSingle.sanitize (c);
746 MultipleSubst multiple;
747 AlternateSubst alternate;
748 LigatureSubst ligature;
750 ChainContextSubst chainContext;
751 ExtensionSubst extension;
752 ReverseChainSingleSubst reverseChainContextSingle;
755 DEFINE_SIZE_UNION (2, sub_format);
759 struct SubstLookup : Lookup
761 inline const SubstLookupSubTable& get_subtable (unsigned int i) const
762 { return this+CastR<OffsetArrayOf<SubstLookupSubTable> > (subTable)[i]; }
764 inline static bool lookup_type_is_reverse (unsigned int lookup_type)
765 { return lookup_type == SubstLookupSubTable::ReverseChainSingle; }
767 inline bool is_reverse (void) const
769 unsigned int type = get_type ();
770 if (unlikely (type == SubstLookupSubTable::Extension))
771 return CastR<ExtensionSubst> (get_subtable(0)).is_reverse ();
772 return lookup_type_is_reverse (type);
776 inline bool apply_once (hb_ot_layout_context_t *layout,
778 hb_mask_t lookup_mask,
779 unsigned int context_length,
780 unsigned int nesting_level_left) const
782 unsigned int lookup_type = get_type ();
783 hb_apply_context_t c[1] = {{0}};
787 c->lookup_mask = lookup_mask;
788 c->context_length = context_length;
789 c->nesting_level_left = nesting_level_left;
790 c->lookup_flag = get_flag ();
792 if (!_hb_ot_layout_check_glyph_property (c->layout->face, &c->buffer->info[c->buffer->i], c->lookup_flag, &c->property))
795 if (unlikely (lookup_type == SubstLookupSubTable::Extension))
797 /* The spec says all subtables should have the same type.
798 * This is specially important if one has a reverse type!
800 * This is rather slow to do this here for every glyph,
801 * but it's easiest, and who uses extension lookups anyway?!*/
802 unsigned int count = get_subtable_count ();
803 unsigned int type = get_subtable(0).u.extension.get_type ();
804 for (unsigned int i = 1; i < count; i++)
805 if (get_subtable(i).u.extension.get_type () != type)
809 unsigned int count = get_subtable_count ();
810 for (unsigned int i = 0; i < count; i++)
811 if (get_subtable (i).apply (c, lookup_type))
817 inline bool apply_string (hb_ot_layout_context_t *layout,
819 hb_mask_t mask) const
823 if (unlikely (!buffer->len))
826 if (likely (!is_reverse ()))
828 /* in/out forward substitution */
829 buffer->clear_output ();
831 while (buffer->i < buffer->len)
833 if ((buffer->info[buffer->i].mask & mask) &&
834 apply_once (layout, buffer, mask, NO_CONTEXT, MAX_NESTING_LEVEL))
837 buffer->next_glyph ();
845 /* in-place backward substitution */
846 buffer->i = buffer->len - 1;
849 if ((buffer->info[buffer->i].mask & mask) &&
850 apply_once (layout, buffer, mask, NO_CONTEXT, MAX_NESTING_LEVEL))
856 while ((int) buffer->i >= 0);
862 inline bool sanitize (hb_sanitize_context_t *c) {
864 if (unlikely (!Lookup::sanitize (c))) return false;
865 OffsetArrayOf<SubstLookupSubTable> &list = CastR<OffsetArrayOf<SubstLookupSubTable> > (subTable);
866 return list.sanitize (c, this, get_type ());
870 typedef OffsetListOf<SubstLookup> SubstLookupList;
876 struct GSUB : GSUBGPOS
878 static const hb_tag_t Tag = HB_OT_TAG_GSUB;
880 inline const SubstLookup& get_lookup (unsigned int i) const
881 { return CastR<SubstLookup> (GSUBGPOS::get_lookup (i)); }
883 inline bool substitute_lookup (hb_ot_layout_context_t *layout,
885 unsigned int lookup_index,
886 hb_mask_t mask) const
887 { return get_lookup (lookup_index).apply_string (layout, buffer, mask); }
889 inline bool sanitize (hb_sanitize_context_t *c) {
891 if (unlikely (!GSUBGPOS::sanitize (c))) return false;
892 OffsetTo<SubstLookupList> &list = CastR<OffsetTo<SubstLookupList> > (lookupList);
893 return list.sanitize (c, this);
896 DEFINE_SIZE_STATIC (10);
900 /* Out-of-class implementation for methods recursing */
902 inline bool ExtensionSubst::apply (hb_apply_context_t *c) const
905 return get_subtable ().apply (c, get_type ());
908 inline bool ExtensionSubst::sanitize (hb_sanitize_context_t *c)
911 if (unlikely (!Extension::sanitize (c))) return false;
912 unsigned int offset = get_offset ();
913 if (unlikely (!offset)) return true;
914 return StructAtOffset<SubstLookupSubTable> (this, offset).sanitize (c, get_type ());
917 inline bool ExtensionSubst::is_reverse (void) const
919 unsigned int type = get_type ();
920 if (unlikely (type == SubstLookupSubTable::Extension))
921 return CastR<ExtensionSubst> (get_subtable()).is_reverse ();
922 return SubstLookup::lookup_type_is_reverse (type);
925 static inline bool substitute_lookup (hb_apply_context_t *c, unsigned int lookup_index)
927 const GSUB &gsub = *(c->layout->face->ot_layout->gsub);
928 const SubstLookup &l = gsub.get_lookup (lookup_index);
930 if (unlikely (c->nesting_level_left == 0))
933 if (unlikely (c->context_length < 1))
936 return l.apply_once (c->layout, c->buffer, c->lookup_mask, c->context_length, c->nesting_level_left - 1);
942 #endif /* HB_OT_LAYOUT_GSUB_PRIVATE_HH */