2 * Copyright (C) 2007,2008,2009 Red Hat, Inc.
4 * This is part of HarfBuzz, an OpenType Layout engine 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 (APPLY_ARG_DEF) const
42 hb_codepoint_t glyph_id = IN_CURGLYPH ();
43 unsigned int index = (this+coverage) (glyph_id);
44 if (HB_LIKELY (index == NOT_COVERED))
47 glyph_id += deltaGlyphID;
48 _hb_buffer_replace_glyph (buffer, glyph_id);
50 /* We inherit the old glyph class to the substituted glyph */
51 if (_hb_ot_layout_has_new_glyph_classes (context->face))
52 _hb_ot_layout_set_glyph_property (context->face, glyph_id, property);
57 inline bool sanitize (SANITIZE_ARG_DEF) {
59 return SANITIZE_THIS (coverage) && SANITIZE (deltaGlyphID);
63 USHORT format; /* Format identifier--format = 1 */
65 coverage; /* Offset to Coverage table--from
66 * beginning of Substitution table */
67 SHORT deltaGlyphID; /* Add to original GlyphID to get
68 * substitute GlyphID */
70 ASSERT_SIZE (SingleSubstFormat1, 6);
72 struct SingleSubstFormat2
74 friend struct SingleSubst;
78 inline bool apply (APPLY_ARG_DEF) const
81 hb_codepoint_t glyph_id = IN_CURGLYPH ();
82 unsigned int index = (this+coverage) (glyph_id);
83 if (HB_LIKELY (index == NOT_COVERED))
86 if (HB_UNLIKELY (index >= substitute.len))
89 glyph_id = substitute[index];
90 _hb_buffer_replace_glyph (buffer, glyph_id);
92 /* We inherit the old glyph class to the substituted glyph */
93 if (_hb_ot_layout_has_new_glyph_classes (context->face))
94 _hb_ot_layout_set_glyph_property (context->face, glyph_id, property);
99 inline bool sanitize (SANITIZE_ARG_DEF) {
101 return SANITIZE_THIS (coverage) && SANITIZE (substitute);
105 USHORT format; /* Format identifier--format = 2 */
107 coverage; /* Offset to Coverage table--from
108 * beginning of Substitution table */
110 substitute; /* Array of substitute
111 * GlyphIDs--ordered by Coverage Index */
113 ASSERT_SIZE (SingleSubstFormat2, 6);
117 friend struct SubstLookupSubTable;
121 inline bool apply (APPLY_ARG_DEF) const
125 case 1: return u.format1->apply (APPLY_ARG);
126 case 2: return u.format2->apply (APPLY_ARG);
127 default:return false;
131 inline bool sanitize (SANITIZE_ARG_DEF) {
133 if (!SANITIZE (u.format)) return false;
135 case 1: return u.format1->sanitize (SANITIZE_ARG);
136 case 2: return u.format2->sanitize (SANITIZE_ARG);
143 USHORT format; /* Format identifier */
144 SingleSubstFormat1 format1[VAR];
145 SingleSubstFormat2 format2[VAR];
152 friend struct MultipleSubstFormat1;
155 inline bool apply (APPLY_ARG_DEF) const
158 if (HB_UNLIKELY (!substitute.len))
161 _hb_buffer_add_output_glyphs (buffer, 1,
162 substitute.len, (const uint16_t *) substitute.const_array(),
165 /* This is a guess only ... */
166 if (_hb_ot_layout_has_new_glyph_classes (context->face))
168 if (property == HB_OT_LAYOUT_GLYPH_CLASS_LIGATURE)
169 property = HB_OT_LAYOUT_GLYPH_CLASS_BASE_GLYPH;
171 unsigned int count = substitute.len;
172 for (unsigned int n = 0; n < count; n++)
173 _hb_ot_layout_set_glyph_property (context->face, substitute[n], property);
180 inline bool sanitize (SANITIZE_ARG_DEF) {
182 return SANITIZE (substitute);
187 substitute; /* String of GlyphIDs to substitute */
189 ASSERT_SIZE (Sequence, 2);
191 struct MultipleSubstFormat1
193 friend struct MultipleSubst;
197 inline bool apply (APPLY_ARG_DEF) const
201 unsigned int index = (this+coverage) (IN_CURGLYPH ());
202 if (HB_LIKELY (index == NOT_COVERED))
205 return (this+sequence[index]).apply (APPLY_ARG);
208 inline bool sanitize (SANITIZE_ARG_DEF) {
210 return SANITIZE_THIS2 (coverage, sequence);
214 USHORT format; /* Format identifier--format = 1 */
216 coverage; /* Offset to Coverage table--from
217 * beginning of Substitution table */
218 OffsetArrayOf<Sequence>
219 sequence; /* Array of Sequence tables
220 * ordered by Coverage Index */
222 ASSERT_SIZE (MultipleSubstFormat1, 6);
226 friend struct SubstLookupSubTable;
230 inline bool apply (APPLY_ARG_DEF) const
234 case 1: return u.format1->apply (APPLY_ARG);
235 default:return false;
239 inline bool sanitize (SANITIZE_ARG_DEF) {
241 if (!SANITIZE (u.format)) return false;
243 case 1: return u.format1->sanitize (SANITIZE_ARG);
250 USHORT format; /* Format identifier */
251 MultipleSubstFormat1 format1[VAR];
256 typedef ArrayOf<GlyphID> AlternateSet; /* Array of alternate GlyphIDs--in
258 ASSERT_SIZE (AlternateSet, 2);
260 struct AlternateSubstFormat1
262 friend struct AlternateSubst;
266 inline bool apply (APPLY_ARG_DEF) const
269 hb_codepoint_t glyph_id = IN_CURGLYPH ();
271 unsigned int index = (this+coverage) (glyph_id);
272 if (HB_LIKELY (index == NOT_COVERED))
275 const AlternateSet &alt_set = this+alternateSet[index];
277 if (HB_UNLIKELY (!alt_set.len))
280 unsigned int alt_index = 0;
282 /* XXX callback to user to choose alternate
283 if (context->face->altfunc)
284 alt_index = (context->face->altfunc)(context->layout, buffer,
285 buffer->out_pos, glyph_id,
286 alt_set.len, alt_set.array);
289 if (HB_UNLIKELY (alt_index >= alt_set.len))
292 glyph_id = alt_set[alt_index];
294 _hb_buffer_replace_glyph (buffer, glyph_id);
296 /* We inherit the old glyph class to the substituted glyph */
297 if (_hb_ot_layout_has_new_glyph_classes (context->face))
298 _hb_ot_layout_set_glyph_property (context->face, glyph_id, property);
303 inline bool sanitize (SANITIZE_ARG_DEF) {
305 return SANITIZE_THIS2 (coverage, alternateSet);
309 USHORT format; /* Format identifier--format = 1 */
311 coverage; /* Offset to Coverage table--from
312 * beginning of Substitution table */
313 OffsetArrayOf<AlternateSet>
314 alternateSet; /* Array of AlternateSet tables
315 * ordered by Coverage Index */
317 ASSERT_SIZE (AlternateSubstFormat1, 6);
319 struct AlternateSubst
321 friend struct SubstLookupSubTable;
325 inline bool apply (APPLY_ARG_DEF) const
329 case 1: return u.format1->apply (APPLY_ARG);
330 default:return false;
334 inline bool sanitize (SANITIZE_ARG_DEF) {
336 if (!SANITIZE (u.format)) return false;
338 case 1: return u.format1->sanitize (SANITIZE_ARG);
345 USHORT format; /* Format identifier */
346 AlternateSubstFormat1 format1[VAR];
353 friend struct LigatureSet;
356 inline bool apply (APPLY_ARG_DEF, bool is_mark) const
360 unsigned int count = component.len;
361 unsigned int end = MIN (buffer->in_length, buffer->in_pos + context_length);
362 if (HB_UNLIKELY (buffer->in_pos + count > end))
365 for (i = 1, j = buffer->in_pos + 1; i < count; i++, j++)
367 while (_hb_ot_layout_skip_mark (context->face, IN_INFO (j), lookup_flag, &property))
369 if (HB_UNLIKELY (j + count - i == end))
374 if (!(property & HB_OT_LAYOUT_GLYPH_CLASS_MARK))
377 if (HB_LIKELY (IN_GLYPH (j) != component[i]))
380 /* This is just a guess ... */
381 if (_hb_ot_layout_has_new_glyph_classes (context->face))
382 _hb_ot_layout_set_glyph_class (context->face, ligGlyph,
383 is_mark ? HB_OT_LAYOUT_GLYPH_CLASS_MARK
384 : HB_OT_LAYOUT_GLYPH_CLASS_LIGATURE);
386 if (j == buffer->in_pos + i) /* No input glyphs skipped */
387 /* We don't use a new ligature ID if there are no skipped
388 glyphs and the ligature already has an ID. */
389 _hb_buffer_add_output_glyphs (buffer, i,
390 1, (const uint16_t *) &ligGlyph,
392 IN_LIGID (buffer->in_pos) ?
393 0xFFFF : _hb_buffer_allocate_lig_id (buffer));
396 unsigned int lig_id = _hb_buffer_allocate_lig_id (buffer);
397 _hb_buffer_add_output_glyph (buffer, ligGlyph, 0xFFFF, lig_id);
399 /* Now we must do a second loop to copy the skipped glyphs to
400 `out' and assign component values to it. We start with the
401 glyph after the first component. Glyphs between component
402 i and i+1 belong to component i. Together with the lig_id
403 value it is later possible to check whether a specific
404 component value really belongs to a given ligature. */
406 for ( i = 1; i < count; i++ )
408 while (_hb_ot_layout_skip_mark (context->face, IN_CURINFO (), lookup_flag, NULL))
409 _hb_buffer_add_output_glyph (buffer, IN_CURGLYPH (), i - 1, lig_id);
414 /* TODO We should possibly reassign lig_id and component for any
415 * components of a previous ligature that s now being removed as part of
423 inline bool sanitize (SANITIZE_ARG_DEF) {
425 return SANITIZE2 (ligGlyph, component);
429 GlyphID ligGlyph; /* GlyphID of ligature to substitute */
430 HeadlessArrayOf<GlyphID>
431 component; /* Array of component GlyphIDs--start
432 * with the second component--ordered
433 * in writing direction */
435 ASSERT_SIZE (Ligature, 4);
439 friend struct LigatureSubstFormat1;
442 inline bool apply (APPLY_ARG_DEF, bool is_mark) const
445 unsigned int num_ligs = ligature.len;
446 for (unsigned int i = 0; i < num_ligs; i++)
448 const Ligature &lig = this+ligature[i];
449 if (lig.apply (APPLY_ARG, is_mark))
457 inline bool sanitize (SANITIZE_ARG_DEF) {
459 return SANITIZE_THIS (ligature);
463 OffsetArrayOf<Ligature>
464 ligature; /* Array LigatureSet tables
465 * ordered by preference */
467 ASSERT_SIZE (LigatureSet, 2);
469 struct LigatureSubstFormat1
471 friend struct LigatureSubst;
474 inline bool apply (APPLY_ARG_DEF) const
477 hb_codepoint_t glyph_id = IN_CURGLYPH ();
479 bool first_is_mark = !!(property & HB_OT_LAYOUT_GLYPH_CLASS_MARK);
481 unsigned int index = (this+coverage) (glyph_id);
482 if (HB_LIKELY (index == NOT_COVERED))
485 const LigatureSet &lig_set = this+ligatureSet[index];
486 return lig_set.apply (APPLY_ARG, first_is_mark);
489 inline bool sanitize (SANITIZE_ARG_DEF) {
491 return SANITIZE_THIS2 (coverage, ligatureSet);
495 USHORT format; /* Format identifier--format = 1 */
497 coverage; /* Offset to Coverage table--from
498 * beginning of Substitution table */
499 OffsetArrayOf<LigatureSet>
500 ligatureSet; /* Array LigatureSet tables
501 * ordered by Coverage Index */
503 ASSERT_SIZE (LigatureSubstFormat1, 6);
507 friend struct SubstLookupSubTable;
510 inline bool apply (APPLY_ARG_DEF) const
514 case 1: return u.format1->apply (APPLY_ARG);
515 default:return false;
519 inline bool sanitize (SANITIZE_ARG_DEF) {
521 if (!SANITIZE (u.format)) return false;
523 case 1: return u.format1->sanitize (SANITIZE_ARG);
530 USHORT format; /* Format identifier */
531 LigatureSubstFormat1 format1[VAR];
537 static inline bool substitute_lookup (APPLY_ARG_DEF, unsigned int lookup_index);
539 struct ContextSubst : Context
541 friend struct SubstLookupSubTable;
544 inline bool apply (APPLY_ARG_DEF) const
547 return Context::apply (APPLY_ARG, substitute_lookup);
551 struct ChainContextSubst : ChainContext
553 friend struct SubstLookupSubTable;
556 inline bool apply (APPLY_ARG_DEF) const
559 return ChainContext::apply (APPLY_ARG, substitute_lookup);
564 struct ExtensionSubst : Extension
566 friend struct SubstLookupSubTable;
569 inline const struct SubstLookupSubTable& get_subtable (void) const
570 { return CONST_CAST (SubstLookupSubTable, Extension::get_subtable (), 0); }
572 inline bool apply (APPLY_ARG_DEF) const;
574 inline bool sanitize (SANITIZE_ARG_DEF);
578 struct ReverseChainSingleSubstFormat1
580 friend struct ReverseChainSingleSubst;
583 inline bool apply (APPLY_ARG_DEF) const
586 if (HB_UNLIKELY (context_length != NO_CONTEXT))
587 return false; /* No chaining to this type */
589 unsigned int index = (this+coverage) (IN_CURGLYPH ());
590 if (HB_LIKELY (index == NOT_COVERED))
593 const OffsetArrayOf<Coverage> &lookahead = CONST_NEXT (OffsetArrayOf<Coverage>, backtrack);
594 const ArrayOf<GlyphID> &substitute = CONST_NEXT (ArrayOf<GlyphID>, lookahead);
596 if (match_backtrack (APPLY_ARG,
597 backtrack.len, (USHORT *) backtrack.const_array(),
598 match_coverage, DECONST_CHARP(this)) &&
599 match_lookahead (APPLY_ARG,
600 lookahead.len, (USHORT *) lookahead.const_array(),
601 match_coverage, DECONST_CHARP(this),
604 IN_CURGLYPH () = substitute[index];
605 buffer->in_pos--; /* Reverse! */
612 inline bool sanitize (SANITIZE_ARG_DEF) {
614 if (!SANITIZE_THIS2 (coverage, backtrack))
616 OffsetArrayOf<Coverage> &lookahead = NEXT (OffsetArrayOf<Coverage>, backtrack);
617 if (!SANITIZE_THIS (lookahead))
619 ArrayOf<GlyphID> &substitute = NEXT (ArrayOf<GlyphID>, lookahead);
620 return SANITIZE (substitute);
624 USHORT format; /* Format identifier--format = 1 */
626 coverage; /* Offset to Coverage table--from
627 * beginning of table */
628 OffsetArrayOf<Coverage>
629 backtrack; /* Array of coverage tables
630 * in backtracking sequence, in glyph
632 OffsetArrayOf<Coverage>
633 lookaheadX; /* Array of coverage tables
634 * in lookahead sequence, in glyph
637 substituteX; /* Array of substitute
638 * GlyphIDs--ordered by Coverage Index */
640 ASSERT_SIZE (ReverseChainSingleSubstFormat1, 10);
642 struct ReverseChainSingleSubst
644 friend struct SubstLookupSubTable;
647 inline bool apply (APPLY_ARG_DEF) const
651 case 1: return u.format1->apply (APPLY_ARG);
652 default:return false;
656 inline bool sanitize (SANITIZE_ARG_DEF) {
658 if (!SANITIZE (u.format)) return false;
660 case 1: return u.format1->sanitize (SANITIZE_ARG);
667 USHORT format; /* Format identifier */
668 ReverseChainSingleSubstFormat1 format1[VAR];
678 struct SubstLookupSubTable
680 friend struct SubstLookup;
690 ReverseChainSingle = 8
693 inline bool apply (APPLY_ARG_DEF, unsigned int lookup_type) const
696 switch (lookup_type) {
697 case Single: return u.single->apply (APPLY_ARG);
698 case Multiple: return u.multiple->apply (APPLY_ARG);
699 case Alternate: return u.alternate->apply (APPLY_ARG);
700 case Ligature: return u.ligature->apply (APPLY_ARG);
701 case Context: return u.context->apply (APPLY_ARG);
702 case ChainContext: return u.chainContext->apply (APPLY_ARG);
703 case Extension: return u.extension->apply (APPLY_ARG);
704 case ReverseChainSingle: return u.reverseChainContextSingle->apply (APPLY_ARG);
705 default:return false;
709 inline bool sanitize (SANITIZE_ARG_DEF) {
711 if (!SANITIZE (u.format)) return false;
713 case Single: return u.single->sanitize (SANITIZE_ARG);
714 case Multiple: return u.multiple->sanitize (SANITIZE_ARG);
715 case Alternate: return u.alternate->sanitize (SANITIZE_ARG);
716 case Ligature: return u.ligature->sanitize (SANITIZE_ARG);
717 case Context: return u.context->sanitize (SANITIZE_ARG);
718 case ChainContext: return u.chainContext->sanitize (SANITIZE_ARG);
719 case Extension: return u.extension->sanitize (SANITIZE_ARG);
720 case ReverseChainSingle: return u.reverseChainContextSingle->sanitize (SANITIZE_ARG);
728 SingleSubst single[VAR];
729 MultipleSubst multiple[VAR];
730 AlternateSubst alternate[VAR];
731 LigatureSubst ligature[VAR];
732 ContextSubst context[VAR];
733 ChainContextSubst chainContext[VAR];
734 ExtensionSubst extension[VAR];
735 ReverseChainSingleSubst reverseChainContextSingle[VAR];
740 struct SubstLookup : Lookup
742 inline const SubstLookupSubTable& get_subtable (unsigned int i) const
743 { return CONST_CAST (SubstLookupSubTable, Lookup::get_subtable (i), 0); }
745 /* Like get_type(), but looks through extension lookups.
746 * Never returns Extension */
747 inline unsigned int get_effective_type (void) const
749 unsigned int type = get_type ();
751 if (HB_UNLIKELY (type == SubstLookupSubTable::Extension))
753 unsigned int count = get_subtable_count ();
754 type = get_subtable(0).u.extension->get_type ();
755 /* The spec says all subtables should have the same type.
756 * This is specially important if one has a reverse type! */
757 for (unsigned int i = 1; i < count; i++)
758 if (get_subtable(i).u.extension->get_type () != type)
765 inline bool is_reverse (void) const
766 { return HB_UNLIKELY (get_effective_type () == SubstLookupSubTable::ReverseChainSingle); }
768 inline bool apply_once (hb_ot_layout_context_t *context,
770 unsigned int context_length,
771 unsigned int nesting_level_left) const
773 unsigned int lookup_type = get_type ();
774 unsigned int lookup_flag = get_flag ();
775 unsigned int property;
777 if (!_hb_ot_layout_check_glyph_property (context->face, IN_CURINFO (), lookup_flag, &property))
780 unsigned int count = get_subtable_count ();
781 for (unsigned int i = 0; i < count; i++)
782 if (get_subtable (i).apply (APPLY_ARG_INIT, lookup_type))
788 inline bool apply_string (hb_ot_layout_context_t *context,
790 hb_mask_t mask) const
794 if (HB_UNLIKELY (!buffer->in_length))
797 if (HB_LIKELY (!is_reverse ()))
799 /* in/out forward substitution */
800 _hb_buffer_clear_output (buffer);
802 while (buffer->in_pos < buffer->in_length)
804 if ((~IN_MASK (buffer->in_pos) & mask) &&
805 apply_once (context, buffer, NO_CONTEXT, MAX_NESTING_LEVEL))
808 _hb_buffer_next_glyph (buffer);
812 _hb_buffer_swap (buffer);
816 /* in-place backward substitution */
817 buffer->in_pos = buffer->in_length - 1;
820 if ((~IN_MASK (buffer->in_pos) & mask) &&
821 apply_once (context, buffer, NO_CONTEXT, MAX_NESTING_LEVEL))
827 while ((int) buffer->in_pos >= 0);
833 inline bool sanitize (SANITIZE_ARG_DEF) {
835 if (!Lookup::sanitize (SANITIZE_ARG)) return false;
836 OffsetArrayOf<SubstLookupSubTable> &list = (OffsetArrayOf<SubstLookupSubTable> &) subTable;
837 return SANITIZE_THIS (list);
841 typedef OffsetListOf<SubstLookup> SubstLookupList;
842 ASSERT_SIZE (SubstLookupList, 2);
848 struct GSUB : GSUBGPOS
850 static const hb_tag_t Tag = HB_OT_TAG_GSUB;
852 static inline const GSUB& get_for_data (const char *data)
853 { return (const GSUB&) GSUBGPOS::get_for_data (data); }
855 inline const SubstLookup& get_lookup (unsigned int i) const
856 { return (const SubstLookup&) GSUBGPOS::get_lookup (i); }
858 inline bool substitute_lookup (hb_ot_layout_context_t *context,
860 unsigned int lookup_index,
861 hb_mask_t mask) const
862 { return get_lookup (lookup_index).apply_string (context, buffer, mask); }
865 inline bool sanitize (SANITIZE_ARG_DEF) {
867 if (!GSUBGPOS::sanitize (SANITIZE_ARG)) return false;
868 OffsetTo<SubstLookupList> &list = CAST(OffsetTo<SubstLookupList>, lookupList, 0);
869 return SANITIZE_THIS (list);
872 ASSERT_SIZE (GSUB, 10);
875 /* Out-of-class implementation for methods recursing */
877 inline bool ExtensionSubst::apply (APPLY_ARG_DEF) const
880 unsigned int lookup_type = get_type ();
882 if (HB_UNLIKELY (lookup_type == SubstLookupSubTable::Extension))
885 return get_subtable ().apply (APPLY_ARG, lookup_type);
888 inline bool ExtensionSubst::sanitize (SANITIZE_ARG_DEF)
891 return Extension::sanitize (SANITIZE_ARG) &&
892 (&(Extension::get_subtable ()) == &Null(LookupSubTable) ||
893 get_type () == SubstLookupSubTable::Extension ||
894 DECONST_CAST (SubstLookupSubTable, get_subtable (), 0).sanitize (SANITIZE_ARG));
897 static inline bool substitute_lookup (APPLY_ARG_DEF, unsigned int lookup_index)
899 const GSUB &gsub = *(context->face->ot_layout.gsub);
900 const SubstLookup &l = gsub.get_lookup (lookup_index);
902 if (HB_UNLIKELY (nesting_level_left == 0))
904 nesting_level_left--;
906 if (HB_UNLIKELY (context_length < 1))
909 return l.apply_once (context, buffer, context_length, nesting_level_left);
913 #endif /* HB_OT_LAYOUT_GSUB_PRIVATE_HH */