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"
33 struct SingleSubstFormat1
35 friend struct SingleSubst;
39 inline bool apply (hb_apply_context_t *c) const
42 hb_codepoint_t glyph_id = c->buffer->info[c->buffer->i].codepoint;
43 unsigned int index = (this+coverage) (glyph_id);
44 if (likely (index == NOT_COVERED))
47 glyph_id += deltaGlyphID;
48 c->buffer->replace_glyph (glyph_id);
50 /* We inherit the old glyph class to the substituted glyph */
51 if (_hb_ot_layout_has_new_glyph_classes (c->layout->face))
52 _hb_ot_layout_set_glyph_property (c->layout->face, glyph_id, c->property);
57 inline bool sanitize (hb_sanitize_context_t *c) {
59 return coverage.sanitize (c, this)
60 && deltaGlyphID.sanitize (c);
64 USHORT format; /* Format identifier--format = 1 */
66 coverage; /* Offset to Coverage table--from
67 * beginning of Substitution table */
68 SHORT deltaGlyphID; /* Add to original GlyphID to get
69 * substitute GlyphID */
71 DEFINE_SIZE_STATIC (6);
74 struct SingleSubstFormat2
76 friend struct SingleSubst;
80 inline bool apply (hb_apply_context_t *c) const
83 hb_codepoint_t glyph_id = c->buffer->info[c->buffer->i].codepoint;
84 unsigned int index = (this+coverage) (glyph_id);
85 if (likely (index == NOT_COVERED))
88 if (unlikely (index >= substitute.len))
91 glyph_id = substitute[index];
92 c->buffer->replace_glyph (glyph_id);
94 /* We inherit the old glyph class to the substituted glyph */
95 if (_hb_ot_layout_has_new_glyph_classes (c->layout->face))
96 _hb_ot_layout_set_glyph_property (c->layout->face, glyph_id, c->property);
101 inline bool sanitize (hb_sanitize_context_t *c) {
103 return coverage.sanitize (c, this)
104 && substitute.sanitize (c);
108 USHORT format; /* Format identifier--format = 2 */
110 coverage; /* Offset to Coverage table--from
111 * beginning of Substitution table */
113 substitute; /* Array of substitute
114 * GlyphIDs--ordered by Coverage Index */
116 DEFINE_SIZE_ARRAY (6, substitute);
121 friend struct SubstLookupSubTable;
125 inline bool apply (hb_apply_context_t *c) const
129 case 1: return u.format1.apply (c);
130 case 2: return u.format2.apply (c);
131 default:return false;
135 inline bool sanitize (hb_sanitize_context_t *c) {
137 if (!u.format.sanitize (c)) return false;
139 case 1: return u.format1.sanitize (c);
140 case 2: return u.format2.sanitize (c);
147 USHORT format; /* Format identifier */
148 SingleSubstFormat1 format1;
149 SingleSubstFormat2 format2;
156 friend struct MultipleSubstFormat1;
159 inline bool apply (hb_apply_context_t *c) const
162 if (unlikely (!substitute.len))
165 c->buffer->add_output_glyphs_be16 (1,
166 substitute.len, (const uint16_t *) substitute.array,
169 /* This is a guess only ... */
170 if (_hb_ot_layout_has_new_glyph_classes (c->layout->face))
172 unsigned int property = c->property;
173 if (property == HB_OT_LAYOUT_GLYPH_CLASS_LIGATURE)
174 property = HB_OT_LAYOUT_GLYPH_CLASS_BASE_GLYPH;
176 unsigned int count = substitute.len;
177 for (unsigned int n = 0; n < count; n++)
178 _hb_ot_layout_set_glyph_property (c->layout->face, substitute[n], property);
185 inline bool sanitize (hb_sanitize_context_t *c) {
187 return substitute.sanitize (c);
192 substitute; /* String of GlyphIDs to substitute */
194 DEFINE_SIZE_ARRAY (2, substitute);
197 struct MultipleSubstFormat1
199 friend struct MultipleSubst;
203 inline bool apply (hb_apply_context_t *c) const
207 unsigned int index = (this+coverage) (c->buffer->info[c->buffer->i].codepoint);
208 if (likely (index == NOT_COVERED))
211 return (this+sequence[index]).apply (c);
214 inline bool sanitize (hb_sanitize_context_t *c) {
216 return coverage.sanitize (c, this)
217 && sequence.sanitize (c, this);
221 USHORT format; /* Format identifier--format = 1 */
223 coverage; /* Offset to Coverage table--from
224 * beginning of Substitution table */
225 OffsetArrayOf<Sequence>
226 sequence; /* Array of Sequence tables
227 * ordered by Coverage Index */
229 DEFINE_SIZE_ARRAY (6, sequence);
234 friend struct SubstLookupSubTable;
238 inline bool apply (hb_apply_context_t *c) const
242 case 1: return u.format1.apply (c);
243 default:return false;
247 inline bool sanitize (hb_sanitize_context_t *c) {
249 if (!u.format.sanitize (c)) return false;
251 case 1: return u.format1.sanitize (c);
258 USHORT format; /* Format identifier */
259 MultipleSubstFormat1 format1;
264 typedef ArrayOf<GlyphID> AlternateSet; /* Array of alternate GlyphIDs--in
267 struct AlternateSubstFormat1
269 friend struct AlternateSubst;
273 inline bool apply (hb_apply_context_t *c) const
276 hb_codepoint_t glyph_id = c->buffer->info[c->buffer->i].codepoint;
278 unsigned int index = (this+coverage) (glyph_id);
279 if (likely (index == NOT_COVERED))
282 const AlternateSet &alt_set = this+alternateSet[index];
284 if (unlikely (!alt_set.len))
287 unsigned int alt_index = 0;
289 /* XXX callback to user to choose alternate
290 if (c->layout->face->altfunc)
291 alt_index = (c->layout->face->altfunc)(c->layout->layout, c->buffer,
292 c->buffer->out_len, glyph_id,
293 alt_set.len, alt_set.array);
296 if (unlikely (alt_index >= alt_set.len))
299 glyph_id = alt_set[alt_index];
301 c->buffer->replace_glyph (glyph_id);
303 /* We inherit the old glyph class to the substituted glyph */
304 if (_hb_ot_layout_has_new_glyph_classes (c->layout->face))
305 _hb_ot_layout_set_glyph_property (c->layout->face, glyph_id, c->property);
310 inline bool sanitize (hb_sanitize_context_t *c) {
312 return coverage.sanitize (c, this)
313 && alternateSet.sanitize (c, this);
317 USHORT format; /* Format identifier--format = 1 */
319 coverage; /* Offset to Coverage table--from
320 * beginning of Substitution table */
321 OffsetArrayOf<AlternateSet>
322 alternateSet; /* Array of AlternateSet tables
323 * ordered by Coverage Index */
325 DEFINE_SIZE_ARRAY (6, alternateSet);
328 struct AlternateSubst
330 friend struct SubstLookupSubTable;
334 inline bool apply (hb_apply_context_t *c) const
338 case 1: return u.format1.apply (c);
339 default:return false;
343 inline bool sanitize (hb_sanitize_context_t *c) {
345 if (!u.format.sanitize (c)) return false;
347 case 1: return u.format1.sanitize (c);
354 USHORT format; /* Format identifier */
355 AlternateSubstFormat1 format1;
362 friend struct LigatureSet;
365 inline bool apply (hb_apply_context_t *c, bool is_mark) const
369 unsigned int count = component.len;
370 unsigned int end = MIN (c->buffer->len, c->buffer->i + c->context_length);
371 if (unlikely (c->buffer->i + count > end))
374 for (i = 1, j = c->buffer->i + 1; i < count; i++, j++)
376 unsigned int property;
377 while (_hb_ot_layout_skip_mark (c->layout->face, &c->buffer->info[j], c->lookup_flag, &property))
379 if (unlikely (j + count - i == end))
384 if (!(property & HB_OT_LAYOUT_GLYPH_CLASS_MARK))
387 if (likely (c->buffer->info[j].codepoint != component[i]))
390 /* This is just a guess ... */
391 if (_hb_ot_layout_has_new_glyph_classes (c->layout->face))
392 _hb_ot_layout_set_glyph_class (c->layout->face, ligGlyph,
393 is_mark ? HB_OT_LAYOUT_GLYPH_CLASS_MARK
394 : HB_OT_LAYOUT_GLYPH_CLASS_LIGATURE);
396 if (j == c->buffer->i + i) /* No input glyphs skipped */
397 /* We don't use a new ligature ID if there are no skipped
398 glyphs and the ligature already has an ID. */
399 c->buffer->add_output_glyphs_be16 (i,
400 1, (const uint16_t *) &ligGlyph,
402 c->buffer->info[c->buffer->i].lig_id && !c->buffer->info[c->buffer->i].component ?
403 0xFFFF : c->buffer->allocate_lig_id ());
406 unsigned int lig_id = c->buffer->allocate_lig_id ();
407 c->buffer->add_output_glyph (ligGlyph, 0xFFFF, lig_id);
409 /* Now we must do a second loop to copy the skipped glyphs to
410 `out' and assign component values to it. We start with the
411 glyph after the first component. Glyphs between component
412 i and i+1 belong to component i. Together with the lig_id
413 value it is later possible to check whether a specific
414 component value really belongs to a given ligature. */
416 for ( i = 1; i < count; i++ )
418 while (_hb_ot_layout_skip_mark (c->layout->face, &c->buffer->info[c->buffer->i], c->lookup_flag, NULL))
419 c->buffer->add_output_glyph (c->buffer->info[c->buffer->i].codepoint, i, lig_id);
429 inline bool sanitize (hb_sanitize_context_t *c) {
431 return ligGlyph.sanitize (c)
432 && component.sanitize (c);
436 GlyphID ligGlyph; /* GlyphID of ligature to substitute */
437 HeadlessArrayOf<GlyphID>
438 component; /* Array of component GlyphIDs--start
439 * with the second component--ordered
440 * in writing direction */
442 DEFINE_SIZE_ARRAY (4, component);
447 friend struct LigatureSubstFormat1;
450 inline bool apply (hb_apply_context_t *c, bool is_mark) const
453 unsigned int num_ligs = ligature.len;
454 for (unsigned int i = 0; i < num_ligs; i++)
456 const Ligature &lig = this+ligature[i];
457 if (lig.apply (c, is_mark))
465 inline bool sanitize (hb_sanitize_context_t *c) {
467 return ligature.sanitize (c, this);
471 OffsetArrayOf<Ligature>
472 ligature; /* Array LigatureSet tables
473 * ordered by preference */
475 DEFINE_SIZE_ARRAY (2, ligature);
478 struct LigatureSubstFormat1
480 friend struct LigatureSubst;
483 inline bool apply (hb_apply_context_t *c) const
486 hb_codepoint_t glyph_id = c->buffer->info[c->buffer->i].codepoint;
488 bool first_is_mark = !!(c->property & HB_OT_LAYOUT_GLYPH_CLASS_MARK);
490 unsigned int index = (this+coverage) (glyph_id);
491 if (likely (index == NOT_COVERED))
494 const LigatureSet &lig_set = this+ligatureSet[index];
495 return lig_set.apply (c, first_is_mark);
498 inline bool sanitize (hb_sanitize_context_t *c) {
500 return coverage.sanitize (c, this)
501 && ligatureSet.sanitize (c, this);
505 USHORT format; /* Format identifier--format = 1 */
507 coverage; /* Offset to Coverage table--from
508 * beginning of Substitution table */
509 OffsetArrayOf<LigatureSet>
510 ligatureSet; /* Array LigatureSet tables
511 * ordered by Coverage Index */
513 DEFINE_SIZE_ARRAY (6, ligatureSet);
518 friend struct SubstLookupSubTable;
521 inline bool apply (hb_apply_context_t *c) const
525 case 1: return u.format1.apply (c);
526 default:return false;
530 inline bool sanitize (hb_sanitize_context_t *c) {
532 if (!u.format.sanitize (c)) return false;
534 case 1: return u.format1.sanitize (c);
541 USHORT format; /* Format identifier */
542 LigatureSubstFormat1 format1;
548 static inline bool substitute_lookup (hb_apply_context_t *c, unsigned int lookup_index);
550 struct ContextSubst : Context
552 friend struct SubstLookupSubTable;
555 inline bool apply (hb_apply_context_t *c) const
558 return Context::apply (c, substitute_lookup);
562 struct ChainContextSubst : ChainContext
564 friend struct SubstLookupSubTable;
567 inline bool apply (hb_apply_context_t *c) const
570 return ChainContext::apply (c, substitute_lookup);
575 struct ExtensionSubst : Extension
577 friend struct SubstLookupSubTable;
578 friend struct SubstLookup;
581 inline const struct SubstLookupSubTable& get_subtable (void) const
583 unsigned int offset = get_offset ();
584 if (unlikely (!offset)) return Null(SubstLookupSubTable);
585 return StructAtOffset<SubstLookupSubTable> (this, offset);
588 inline bool apply (hb_apply_context_t *c) const;
590 inline bool sanitize (hb_sanitize_context_t *c);
592 inline bool is_reverse (void) const;
596 struct ReverseChainSingleSubstFormat1
598 friend struct ReverseChainSingleSubst;
601 inline bool apply (hb_apply_context_t *c) const
604 if (unlikely (c->context_length != NO_CONTEXT))
605 return false; /* No chaining to this type */
607 unsigned int index = (this+coverage) (c->buffer->info[c->buffer->i].codepoint);
608 if (likely (index == NOT_COVERED))
611 const OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
612 const ArrayOf<GlyphID> &substitute = StructAfter<ArrayOf<GlyphID> > (lookahead);
614 if (match_backtrack (c,
615 backtrack.len, (USHORT *) backtrack.array,
616 match_coverage, this) &&
618 lookahead.len, (USHORT *) lookahead.array,
619 match_coverage, this,
622 c->buffer->info[c->buffer->i].codepoint = substitute[index];
623 c->buffer->i--; /* Reverse! */
630 inline bool sanitize (hb_sanitize_context_t *c) {
632 if (!(coverage.sanitize (c, this)
633 && backtrack.sanitize (c, this)))
635 OffsetArrayOf<Coverage> &lookahead = StructAfter<OffsetArrayOf<Coverage> > (backtrack);
636 if (!lookahead.sanitize (c, this))
638 ArrayOf<GlyphID> &substitute = StructAfter<ArrayOf<GlyphID> > (lookahead);
639 return substitute.sanitize (c);
643 USHORT format; /* Format identifier--format = 1 */
645 coverage; /* Offset to Coverage table--from
646 * beginning of table */
647 OffsetArrayOf<Coverage>
648 backtrack; /* Array of coverage tables
649 * in backtracking sequence, in glyph
651 OffsetArrayOf<Coverage>
652 lookaheadX; /* Array of coverage tables
653 * in lookahead sequence, in glyph
656 substituteX; /* Array of substitute
657 * GlyphIDs--ordered by Coverage Index */
659 DEFINE_SIZE_MIN (10);
662 struct ReverseChainSingleSubst
664 friend struct SubstLookupSubTable;
667 inline bool apply (hb_apply_context_t *c) const
671 case 1: return u.format1.apply (c);
672 default:return false;
676 inline bool sanitize (hb_sanitize_context_t *c) {
678 if (!u.format.sanitize (c)) return false;
680 case 1: return u.format1.sanitize (c);
687 USHORT format; /* Format identifier */
688 ReverseChainSingleSubstFormat1 format1;
698 struct SubstLookupSubTable
700 friend struct SubstLookup;
710 ReverseChainSingle = 8
713 inline bool apply (hb_apply_context_t *c, unsigned int lookup_type) const
716 switch (lookup_type) {
717 case Single: return u.single.apply (c);
718 case Multiple: return u.multiple.apply (c);
719 case Alternate: return u.alternate.apply (c);
720 case Ligature: return u.ligature.apply (c);
721 case Context: return u.c.apply (c);
722 case ChainContext: return u.chainContext.apply (c);
723 case Extension: return u.extension.apply (c);
724 case ReverseChainSingle: return u.reverseChainContextSingle.apply (c);
725 default:return false;
729 inline bool sanitize (hb_sanitize_context_t *c, unsigned int lookup_type) {
731 switch (lookup_type) {
732 case Single: return u.single.sanitize (c);
733 case Multiple: return u.multiple.sanitize (c);
734 case Alternate: return u.alternate.sanitize (c);
735 case Ligature: return u.ligature.sanitize (c);
736 case Context: return u.c.sanitize (c);
737 case ChainContext: return u.chainContext.sanitize (c);
738 case Extension: return u.extension.sanitize (c);
739 case ReverseChainSingle: return u.reverseChainContextSingle.sanitize (c);
748 MultipleSubst multiple;
749 AlternateSubst alternate;
750 LigatureSubst ligature;
752 ChainContextSubst chainContext;
753 ExtensionSubst extension;
754 ReverseChainSingleSubst reverseChainContextSingle;
757 DEFINE_SIZE_UNION (2, sub_format);
761 struct SubstLookup : Lookup
763 inline const SubstLookupSubTable& get_subtable (unsigned int i) const
764 { return this+CastR<OffsetArrayOf<SubstLookupSubTable> > (subTable)[i]; }
766 inline static bool lookup_type_is_reverse (unsigned int lookup_type)
767 { return lookup_type == SubstLookupSubTable::ReverseChainSingle; }
769 inline bool is_reverse (void) const
771 unsigned int type = get_type ();
772 if (unlikely (type == SubstLookupSubTable::Extension))
773 return CastR<ExtensionSubst> (get_subtable(0)).is_reverse ();
774 return lookup_type_is_reverse (type);
778 inline bool apply_once (hb_ot_layout_context_t *layout,
780 unsigned int context_length,
781 unsigned int nesting_level_left) const
783 unsigned int lookup_type = get_type ();
784 hb_apply_context_t c[1] = {{0}};
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, 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, 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->context_length, c->nesting_level_left - 1);
940 #endif /* HB_OT_LAYOUT_GSUB_PRIVATE_HH */