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_H
28 #define HB_OT_LAYOUT_GSUB_PRIVATE_H
30 #include "hb-ot-layout-gsubgpos-private.h"
33 #include "harfbuzz-impl.h"
35 _hb_buffer_add_output_glyph_ids( HB_Buffer buffer,
38 const GlyphID *glyph_data,
43 struct SingleSubstFormat1 {
45 friend struct SingleSubst;
49 inline bool single_substitute (hb_codepoint_t &glyph_id) const {
51 unsigned int index = (this+coverage) (glyph_id);
52 if (G_LIKELY (index == NOT_COVERED))
55 glyph_id += deltaGlyphID;
61 USHORT format; /* Format identifier--format = 1 */
63 coverage; /* Offset to Coverage table--from
64 * beginning of Substitution table */
65 SHORT deltaGlyphID; /* Add to original GlyphID to get
66 * substitute GlyphID */
68 ASSERT_SIZE (SingleSubstFormat1, 6);
70 struct SingleSubstFormat2 {
72 friend struct SingleSubst;
76 inline bool single_substitute (hb_codepoint_t &glyph_id) const {
78 unsigned int index = (this+coverage) (glyph_id);
79 if (G_LIKELY (index == NOT_COVERED))
82 if (G_UNLIKELY (index >= substitute.len))
85 glyph_id = substitute[index];
90 USHORT format; /* Format identifier--format = 2 */
92 coverage; /* Offset to Coverage table--from
93 * beginning of Substitution table */
95 substitute; /* Array of substitute
96 * GlyphIDs--ordered by Coverage Index */
98 ASSERT_SIZE (SingleSubstFormat2, 6);
102 friend struct SubstLookupSubTable;
106 inline bool single_substitute (hb_codepoint_t &glyph_id) const {
108 case 1: return u.format1->single_substitute (glyph_id);
109 case 2: return u.format2->single_substitute (glyph_id);
110 default:return false;
114 inline bool substitute (LOOKUP_ARGS_DEF) const {
116 hb_codepoint_t glyph_id = IN_CURGLYPH ();
118 if (!single_substitute (glyph_id))
121 _hb_buffer_replace_glyph (buffer, glyph_id);
123 if ( _hb_ot_layout_has_new_glyph_classes (layout) )
125 /* we inherit the old glyph class to the substituted glyph */
126 _hb_ot_layout_set_glyph_property (layout, glyph_id, property);
134 USHORT format; /* Format identifier */
135 SingleSubstFormat1 format1[];
136 SingleSubstFormat2 format2[];
139 ASSERT_SIZE (SingleSubst, 2);
144 friend struct MultipleSubstFormat1;
148 inline void set_glyph_class (hb_ot_layout_t *layout, unsigned int property) const {
150 unsigned int count = substitute.len;
151 for (unsigned int n = 0; n < count; n++)
152 _hb_ot_layout_set_glyph_property (layout, substitute[n], property);
155 inline bool substitute_sequence (LOOKUP_ARGS_DEF) const {
157 if (HB_UNLIKELY (!substitute.len))
160 _hb_buffer_add_output_glyph_ids (buffer, 1,
161 substitute.len, substitute.array,
164 if ( _hb_ot_layout_has_new_glyph_classes (layout) )
166 /* this is a guess only ... */
168 if ( property == HB_OT_LAYOUT_GLYPH_CLASS_LIGATURE )
169 property = HB_OT_LAYOUT_GLYPH_CLASS_BASE_GLYPH;
171 set_glyph_class (layout, property);
179 substitute; /* String of GlyphIDs to substitute */
181 ASSERT_SIZE (Sequence, 2);
183 struct MultipleSubstFormat1 {
185 friend struct MultipleSubst;
189 inline bool substitute (LOOKUP_ARGS_DEF) const {
191 unsigned int index = (this+coverage) (IN_CURGLYPH ());
192 if (G_LIKELY (index == NOT_COVERED))
195 return (this+sequence[index]).substitute_sequence (LOOKUP_ARGS);
199 USHORT format; /* Format identifier--format = 1 */
201 coverage; /* Offset to Coverage table--from
202 * beginning of Substitution table */
203 OffsetArrayOf<Sequence>
204 sequence; /* Array of Sequence tables
205 * ordered by Coverage Index */
207 ASSERT_SIZE (MultipleSubstFormat1, 6);
209 struct MultipleSubst {
211 friend struct SubstLookupSubTable;
215 inline bool substitute (LOOKUP_ARGS_DEF) const {
217 case 1: return u.format1->substitute (LOOKUP_ARGS);
218 default:return false;
224 USHORT format; /* Format identifier */
225 MultipleSubstFormat1 format1[];
228 ASSERT_SIZE (MultipleSubst, 2);
231 typedef ArrayOf<GlyphID> AlternateSet; /* Array of alternate GlyphIDs--in
233 ASSERT_SIZE (AlternateSet, 2);
235 struct AlternateSubstFormat1 {
237 friend struct AlternateSubst;
241 inline bool substitute (LOOKUP_ARGS_DEF) const {
243 hb_codepoint_t glyph_id = IN_CURGLYPH ();
245 unsigned int index = (this+coverage) (glyph_id);
246 if (G_LIKELY (index == NOT_COVERED))
249 const AlternateSet &alt_set = this+alternateSet[index];
251 if (HB_UNLIKELY (!alt_set.len))
254 unsigned int alt_index = 0;
256 /* XXX callback to user to choose alternate
258 alt_index = (gsub->altfunc)( buffer->out_pos, glyph_id,
259 aset.GlyphCount, aset.Alternate,
263 if (HB_UNLIKELY (alt_index >= alt_set.len))
266 glyph_id = alt_set[alt_index];
268 _hb_buffer_replace_glyph (buffer, glyph_id);
270 if ( _hb_ot_layout_has_new_glyph_classes (layout) )
272 /* we inherit the old glyph class to the substituted glyph */
273 _hb_ot_layout_set_glyph_property (layout, glyph_id, property);
280 USHORT format; /* Format identifier--format = 1 */
282 coverage; /* Offset to Coverage table--from
283 * beginning of Substitution table */
284 OffsetArrayOf<AlternateSet>
285 alternateSet; /* Array of AlternateSet tables
286 * ordered by Coverage Index */
288 ASSERT_SIZE (AlternateSubstFormat1, 6);
290 struct AlternateSubst {
292 friend struct SubstLookupSubTable;
296 inline bool substitute (LOOKUP_ARGS_DEF) const {
298 case 1: return u.format1->substitute (LOOKUP_ARGS);
299 default:return false;
305 USHORT format; /* Format identifier */
306 AlternateSubstFormat1 format1[];
309 ASSERT_SIZE (AlternateSubst, 2);
314 friend struct LigatureSet;
317 inline bool substitute_ligature (LOOKUP_ARGS_DEF, bool is_mark) const {
320 unsigned int count = component.len;
321 unsigned int end = MIN (buffer->in_length, buffer->in_pos + context_length);
322 if (HB_UNLIKELY (buffer->in_pos + count > end))
325 for (i = 1, j = buffer->in_pos + 1; i < count; i++, j++) {
326 while (!_hb_ot_layout_check_glyph_property (layout, IN_ITEM (j), lookup_flag, &property)) {
327 if (HB_UNLIKELY (j + count - i == end))
332 if (!(property == HB_OT_LAYOUT_GLYPH_CLASS_MARK ||
333 property & LookupFlag::MarkAttachmentType))
336 if (HB_LIKELY (IN_GLYPH(j) != component[i]))
339 if ( _hb_ot_layout_has_new_glyph_classes (layout) )
340 /* this is just a guess ... */
341 hb_ot_layout_set_glyph_class (layout, ligGlyph,
342 is_mark ? HB_OT_LAYOUT_GLYPH_CLASS_MARK
343 : HB_OT_LAYOUT_GLYPH_CLASS_LIGATURE);
345 if (j == buffer->in_pos + i) /* No input glyphs skipped */
346 /* We don't use a new ligature ID if there are no skipped
347 glyphs and the ligature already has an ID. */
348 _hb_buffer_add_output_glyph_ids (buffer, i,
351 IN_LIGID (buffer->in_pos) ?
352 0xFFFF : _hb_buffer_allocate_ligid (buffer));
355 unsigned int lig_id = _hb_buffer_allocate_ligid (buffer);
356 _hb_buffer_add_output_glyph (buffer, ligGlyph, 0xFFFF, lig_id);
358 /* Now we must do a second loop to copy the skipped glyphs to
359 `out' and assign component values to it. We start with the
360 glyph after the first component. Glyphs between component
361 i and i+1 belong to component i. Together with the lig_id
362 value it is later possible to check whether a specific
363 component value really belongs to a given ligature. */
365 for ( i = 1; i < count; i++ )
367 while (!_hb_ot_layout_check_glyph_property (layout, IN_CURITEM(), lookup_flag, &property))
368 _hb_buffer_add_output_glyph (buffer, IN_CURGLYPH(), i - 1, lig_id);
373 /* XXX We should possibly reassign lig_id and component for any
374 * components of a previous ligature that s now being removed as part of
382 GlyphID ligGlyph; /* GlyphID of ligature to substitute */
383 HeadlessArrayOf<GlyphID>
384 component; /* Array of component GlyphIDs--start
385 * with the second component--ordered
386 * in writing direction */
388 ASSERT_SIZE (Ligature, 4);
392 friend struct LigatureSubstFormat1;
396 inline bool substitute_ligature (LOOKUP_ARGS_DEF, bool is_mark) const {
398 unsigned int num_ligs = ligature.len;
399 for (unsigned int i = 0; i < num_ligs; i++) {
400 const Ligature &lig = this+ligature[i];
401 if (lig.substitute_ligature (LOOKUP_ARGS, is_mark))
409 OffsetArrayOf<Ligature>
410 ligature; /* Array LigatureSet tables
411 * ordered by preference */
413 ASSERT_SIZE (LigatureSet, 2);
415 struct LigatureSubstFormat1 {
417 friend struct LigatureSubst;
421 inline bool substitute (LOOKUP_ARGS_DEF) const {
423 hb_codepoint_t glyph_id = IN_CURGLYPH ();
425 bool first_is_mark = (property == HB_OT_LAYOUT_GLYPH_CLASS_MARK ||
426 property & LookupFlag::MarkAttachmentType);
428 unsigned int index = (this+coverage) (glyph_id);
429 if (G_LIKELY (index == NOT_COVERED))
432 const LigatureSet &lig_set = this+ligatureSet[index];
433 return lig_set.substitute_ligature (LOOKUP_ARGS, first_is_mark);
437 USHORT format; /* Format identifier--format = 1 */
439 coverage; /* Offset to Coverage table--from
440 * beginning of Substitution table */
441 OffsetArrayOf<LigatureSet>\
442 ligatureSet; /* Array LigatureSet tables
443 * ordered by Coverage Index */
445 ASSERT_SIZE (LigatureSubstFormat1, 6);
447 struct LigatureSubst {
449 friend struct SubstLookupSubTable;
453 inline bool substitute (LOOKUP_ARGS_DEF) const {
455 case 1: return u.format1->substitute (LOOKUP_ARGS);
456 default:return false;
462 USHORT format; /* Format identifier */
463 LigatureSubstFormat1 format1[];
466 ASSERT_SIZE (LigatureSubst, 2);
470 static inline bool substitute_lookup (LOOKUP_ARGS_DEF, unsigned int lookup_index);
473 struct ContextSubst : Context {
475 inline bool substitute (LOOKUP_ARGS_DEF) const {
476 return this->apply (LOOKUP_ARGS, substitute_lookup);
479 ASSERT_SIZE (ContextSubst, 2);
482 struct ChainContextSubst : ChainContext {
484 inline bool substitute (LOOKUP_ARGS_DEF) const {
485 return this->apply (LOOKUP_ARGS, substitute_lookup);
488 ASSERT_SIZE (ChainContextSubst, 2);
491 struct ExtensionSubstFormat1 {
493 friend struct ExtensionSubst;
496 inline unsigned int get_type (void) const { return extensionLookupType; }
497 inline unsigned int get_offset (void) const { return (extensionOffset[0] << 16) + extensionOffset[1]; }
498 inline bool substitute (LOOKUP_ARGS_DEF) const;
501 USHORT format; /* Format identifier. Set to 1. */
502 USHORT extensionLookupType; /* Lookup type of subtable referenced
503 * by ExtensionOffset (i.e. the
504 * extension subtable). */
505 USHORT extensionOffset[2]; /* Offset to the extension subtable,
506 * of lookup type subtable.
507 * Defined as two shorts to avoid
508 * alignment requirements. */
510 ASSERT_SIZE (ExtensionSubstFormat1, 8);
512 struct ExtensionSubst {
514 friend struct SubstLookup;
515 friend struct SubstLookupSubTable;
519 inline unsigned int get_type (void) const {
521 case 1: return u.format1->get_type ();
526 inline bool substitute (LOOKUP_ARGS_DEF) const {
528 case 1: return u.format1->substitute (LOOKUP_ARGS);
529 default:return false;
535 USHORT format; /* Format identifier */
536 ExtensionSubstFormat1 format1[];
539 ASSERT_SIZE (ExtensionSubst, 2);
542 struct ReverseChainSingleSubstFormat1 {
544 inline bool substitute (LOOKUP_ARGS_DEF) const {
549 USHORT format; /* Format identifier--format = 1 */
550 Offset coverage; /* Offset to Coverage table -- from
551 * beginning of Substitution table */
552 USHORT backtrackGlyphCount; /* Number of glyphs in the backtracking
554 Offset backtrackCoverage[]; /* Array of offsets to coverage tables
555 * in backtracking sequence, in glyph
557 USHORT lookaheadGlyphCount; /* Number of glyphs in lookahead
559 Offset lookaheadCoverage[]; /* Array of offsets to coverage tables
560 * in lookahead sequence, in glyph
562 USHORT glyphCount; /* Number of GlyphIDs in the Substitute
564 GlyphID substituteGlyphs[]; /* Array of substitute
565 * GlyphIDs--ordered by Coverage Index */
567 ASSERT_SIZE (ReverseChainSingleSubstFormat1, 10);
569 struct ReverseChainSingleSubst {
571 friend struct SubstLookupSubTable;
575 inline bool substitute (LOOKUP_ARGS_DEF) const {
577 case 1: return u.format1->substitute (LOOKUP_ARGS);
578 default:return false;
584 USHORT format; /* Format identifier */
585 ReverseChainSingleSubstFormat1 format1[];
588 ASSERT_SIZE (ReverseChainSingleSubst, 2);
602 GSUB_ChainContext = 6,
604 GSUB_ReverseChainSingle = 8,
607 struct SubstLookupSubTable {
609 friend struct SubstLookup;
611 inline bool substitute (LOOKUP_ARGS_DEF,
612 unsigned int lookup_type) const {
614 switch (lookup_type) {
615 case GSUB_Single: return u.single->substitute (LOOKUP_ARGS);
616 case GSUB_Multiple: return u.multiple->substitute (LOOKUP_ARGS);
617 case GSUB_Alternate: return u.alternate->substitute (LOOKUP_ARGS);
618 case GSUB_Ligature: return u.ligature->substitute (LOOKUP_ARGS);
619 case GSUB_Context: return u.context->substitute (LOOKUP_ARGS);
620 case GSUB_ChainContext: return u.chainingContext->substitute (LOOKUP_ARGS);
621 case GSUB_Extension: return u.extension->substitute (LOOKUP_ARGS);
622 case GSUB_ReverseChainSingle: return u.reverseChainContextSingle->substitute (LOOKUP_ARGS);
623 default:return false;
630 SingleSubst single[];
631 MultipleSubst multiple[];
632 AlternateSubst alternate[];
633 LigatureSubst ligature[];
634 ContextSubst context[];
635 ChainContextSubst chainingContext[];
636 ExtensionSubst extension[];
637 ReverseChainSingleSubst reverseChainContextSingle[];
640 ASSERT_SIZE (SubstLookupSubTable, 2);
643 struct SubstLookup : Lookup {
645 inline const SubstLookupSubTable& get_subtable (unsigned int i) const {
646 return *(SubstLookupSubTable*)&(((Lookup *)this)->get_subtable (i));
649 /* Like get_type(), but looks through extension lookups.
650 * Never returns Extension */
651 inline unsigned int get_effective_type (void) const {
652 unsigned int type = get_type ();
654 if (HB_UNLIKELY (type == GSUB_Extension)) {
655 /* Return lookup type of first extension subtable.
656 * The spec says all of them should have the same type.
657 * XXX check for that in sanitize() */
658 type = get_subtable(0).u.extension->get_type ();
664 inline bool is_reverse (void) const {
665 switch (get_effective_type ()) {
666 case GSUB_ReverseChainSingle: return true;
667 default: return false;
671 inline bool substitute_subtables (hb_ot_layout_t *layout,
673 unsigned int context_length,
674 unsigned int nesting_level_left,
675 unsigned int property) const {
676 unsigned int lookup_type = get_type ();
677 unsigned int lookup_flag = get_flag ();
679 for (unsigned int i = 0; i < get_subtable_count (); i++)
680 if (get_subtable (i).substitute (LOOKUP_ARGS,
687 inline bool substitute_once (hb_ot_layout_t *layout,
688 hb_buffer_t *buffer) const {
690 unsigned int lookup_flag = get_flag ();
692 unsigned int property;
693 if (!_hb_ot_layout_check_glyph_property (layout, IN_CURITEM (), lookup_flag, &property))
696 return substitute_subtables (layout, buffer, NO_CONTEXT, MAX_NESTING_LEVEL, property);
699 bool substitute_string (hb_ot_layout_t *layout,
701 hb_ot_layout_feature_mask_t mask) const {
705 if (HB_UNLIKELY (!buffer->in_length))
708 if (HB_LIKELY (!is_reverse ())) {
710 /* in/out forward substitution */
711 _hb_buffer_clear_output (buffer);
713 while (buffer->in_pos < buffer->in_length) {
715 if ((~IN_PROPERTIES (buffer->in_pos) & mask) &&
716 substitute_once (layout, buffer))
719 _hb_buffer_next_glyph (buffer);
723 _hb_buffer_swap (buffer);
727 /* in-place backward substitution */
728 buffer->in_pos = buffer->in_length - 1;
731 if ((~IN_PROPERTIES (buffer->in_pos) & mask) &&
732 substitute_once (layout, buffer))
737 } while ((int) buffer->in_pos >= 0);
743 ASSERT_SIZE (SubstLookup, 6);
750 struct GSUB : GSUBGPOS {
751 static const hb_tag_t Tag = HB_TAG ('G','S','U','B');
753 STATIC_DEFINE_GET_FOR_DATA (GSUB);
754 /* XXX check version here? */
756 inline const SubstLookup& get_lookup (unsigned int i) const {
757 return *(SubstLookup*)&(((GSUBGPOS *)this)->get_lookup (i));
760 inline bool substitute_lookup (hb_ot_layout_t *layout,
762 unsigned int lookup_index,
763 hb_ot_layout_feature_mask_t mask) const {
764 return get_lookup (lookup_index).substitute_string (layout, buffer, mask);
768 ASSERT_SIZE (GSUB, 10);
771 /* Out-of-class implementation for methods recursing */
773 inline bool ExtensionSubstFormat1::substitute (LOOKUP_ARGS_DEF) const {
774 unsigned int lookup_type = get_type ();
776 /* TODO: belongs to sanitize() */
777 if (HB_UNLIKELY (lookup_type == GSUB_ReverseChainSingle))
780 return (*(SubstLookupSubTable *)(((char *) this) + get_offset ())).substitute (LOOKUP_ARGS,
784 static inline bool substitute_lookup (LOOKUP_ARGS_DEF, unsigned int lookup_index) {
785 const GSUB &gsub = *(layout->gsub);
786 const SubstLookup &l = gsub.get_lookup (lookup_index);
788 if (HB_UNLIKELY (nesting_level_left == 0))
790 nesting_level_left--;
792 if (HB_UNLIKELY (context_length < 1))
795 return l.substitute_subtables (layout, buffer, context_length, nesting_level_left, property);
799 #endif /* HB_OT_LAYOUT_GSUB_PRIVATE_H */