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_GSUBGPOS_PRIVATE_H
28 #define HB_OT_LAYOUT_GSUBGPOS_PRIVATE_H
30 #include "hb-buffer-private.h"
31 #include "hb-ot-layout-gdef-private.h"
34 #define APPLY_ARG_DEF \
35 hb_ot_layout_t *layout, \
36 hb_buffer_t *buffer, \
37 unsigned int context_length HB_GNUC_UNUSED, \
38 unsigned int nesting_level_left HB_GNUC_UNUSED, \
39 unsigned int lookup_flag, \
40 unsigned int property HB_GNUC_UNUSED /* propety of first glyph */
50 typedef bool (*match_func_t) (hb_codepoint_t glyph_id, const USHORT &value, char *data);
51 typedef bool (*apply_lookup_func_t) (APPLY_ARG_DEF, unsigned int lookup_index);
56 apply_lookup_func_t apply;
60 static inline bool match_glyph (hb_codepoint_t glyph_id, const USHORT &value, char *data)
62 return glyph_id == value;
65 static inline bool match_class (hb_codepoint_t glyph_id, const USHORT &value, char *data)
67 const ClassDef &class_def = *(const ClassDef *)data;
68 return class_def.get_class (glyph_id) == value;
71 static inline bool match_coverage (hb_codepoint_t glyph_id, const USHORT &value, char *data)
73 const OffsetTo<Coverage> &coverage = (const OffsetTo<Coverage>&)value;
74 return (data+coverage) (glyph_id) != NOT_COVERED;
78 static inline bool match_input (APPLY_ARG_DEF,
79 unsigned int count, /* Including the first glyph (not matched) */
80 const USHORT input[], /* Array of input values--start with second glyph */
81 match_func_t match_func,
83 unsigned int *context_length_out)
86 unsigned int end = MIN (buffer->in_length, buffer->in_pos + context_length);
87 if (HB_UNLIKELY (buffer->in_pos + count > end))
90 for (i = 1, j = buffer->in_pos + 1; i < count; i++, j++)
92 while (!_hb_ot_layout_check_glyph_property (layout, IN_INFO (j), lookup_flag, &property))
94 if (HB_UNLIKELY (j + count - i == end))
99 if (HB_LIKELY (!match_func (IN_GLYPH(j), input[i - 1], match_data)))
103 *context_length_out = j - buffer->in_pos;
108 static inline bool match_backtrack (APPLY_ARG_DEF,
110 const USHORT backtrack[],
111 match_func_t match_func,
114 if (HB_UNLIKELY (buffer->out_pos < count))
117 for (unsigned int i = 0, j = buffer->out_pos - 1; i < count; i++, j--)
119 while (!_hb_ot_layout_check_glyph_property (layout, OUT_INFO (j), lookup_flag, &property))
121 if (HB_UNLIKELY (j + 1 == count - i))
126 if (HB_LIKELY (!match_func (OUT_GLYPH(j), backtrack[i], match_data)))
133 static inline bool match_lookahead (APPLY_ARG_DEF,
135 const USHORT lookahead[],
136 match_func_t match_func,
141 unsigned int end = MIN (buffer->in_length, buffer->in_pos + context_length);
142 if (HB_UNLIKELY (buffer->in_pos + offset + count > end))
145 for (i = 0, j = buffer->in_pos + offset; i < count; i++, j++)
147 while (!_hb_ot_layout_check_glyph_property (layout, OUT_INFO (j), lookup_flag, &property))
149 if (HB_UNLIKELY (j + count - i == end))
154 if (HB_LIKELY (!match_func (IN_GLYPH(j), lookahead[i], match_data)))
164 USHORT sequenceIndex; /* Index into current glyph
165 * sequence--first glyph = 0 */
166 USHORT lookupListIndex; /* Lookup to apply to that
167 * position--zero--based */
169 ASSERT_SIZE (LookupRecord, 4);
171 static inline bool apply_lookup (APPLY_ARG_DEF,
172 unsigned int count, /* Including the first glyph */
173 unsigned int lookupCount,
174 const LookupRecord lookupRecord[], /* Array of LookupRecords--in design order */
175 apply_lookup_func_t apply_func)
177 unsigned int end = MIN (buffer->in_length, buffer->in_pos + context_length);
178 if (HB_UNLIKELY (buffer->in_pos + count > end))
181 /* TODO We don't support lookupRecord arrays that are not increasing:
182 * Should be easy for in_place ones at least. */
183 for (unsigned int i = 0; i < count; i++)
185 while (!_hb_ot_layout_check_glyph_property (layout, IN_CURINFO (), lookup_flag, &property))
187 if (HB_UNLIKELY (buffer->in_pos == end))
189 /* No lookup applied for this index */
190 _hb_buffer_next_glyph (buffer);
193 if (lookupCount && i == lookupRecord->sequenceIndex)
195 unsigned int old_pos = buffer->in_pos;
198 bool done = apply_func (APPLY_ARG, lookupRecord->lookupListIndex);
202 i += buffer->in_pos - old_pos;
203 if (HB_UNLIKELY (buffer->in_pos == end))
212 /* No lookup applied for this index */
213 _hb_buffer_next_glyph (buffer);
222 /* Contextual lookups */
224 struct ContextLookupContext
230 static inline bool context_lookup (APPLY_ARG_DEF,
231 unsigned int inputCount, /* Including the first glyph (not matched) */
232 const USHORT input[], /* Array of input values--start with second glyph */
233 unsigned int lookupCount,
234 const LookupRecord lookupRecord[],
235 ContextLookupContext &context)
237 return match_input (APPLY_ARG,
239 context.funcs.match, context.match_data,
241 apply_lookup (APPLY_ARG,
243 lookupCount, lookupRecord,
244 context.funcs.apply);
249 friend struct RuleSet;
252 inline bool apply (APPLY_ARG_DEF, ContextLookupContext &context) const
254 const LookupRecord *lookupRecord = (const LookupRecord *)
255 ((const char *) input +
256 sizeof (input[0]) * (inputCount ? inputCount - 1 : 0));
257 return context_lookup (APPLY_ARG,
259 lookupCount, lookupRecord,
264 USHORT inputCount; /* Total number of glyphs in input
265 * glyph sequence--includes the first
267 USHORT lookupCount; /* Number of LookupRecords */
268 USHORT input[]; /* Array of match inputs--start with
270 LookupRecord lookupRecordX[]; /* Array of LookupRecords--in
273 ASSERT_SIZE (Rule, 4);
277 inline bool apply (APPLY_ARG_DEF, ContextLookupContext &context) const
279 unsigned int num_rules = rule.len;
280 for (unsigned int i = 0; i < num_rules; i++)
282 if ((this+rule[i]).apply (APPLY_ARG, context))
291 rule; /* Array of Rule tables
292 * ordered by preference */
296 struct ContextFormat1
298 friend struct Context;
301 inline bool apply (APPLY_ARG_DEF, apply_lookup_func_t apply_func) const
303 unsigned int index = (this+coverage) (IN_CURGLYPH ());
304 if (HB_LIKELY (index == NOT_COVERED))
307 const RuleSet &rule_set = this+ruleSet[index];
308 struct ContextLookupContext context = {
309 {match_glyph, apply_func},
312 return rule_set.apply (APPLY_ARG, context);
316 USHORT format; /* Format identifier--format = 1 */
318 coverage; /* Offset to Coverage table--from
319 * beginning of table */
320 OffsetArrayOf<RuleSet>
321 ruleSet; /* Array of RuleSet tables
322 * ordered by Coverage Index */
324 ASSERT_SIZE (ContextFormat1, 6);
327 struct ContextFormat2
329 friend struct Context;
332 inline bool apply (APPLY_ARG_DEF, apply_lookup_func_t apply_func) const
334 unsigned int index = (this+coverage) (IN_CURGLYPH ());
335 if (HB_LIKELY (index == NOT_COVERED))
338 const ClassDef &class_def = this+classDef;
339 index = class_def (IN_CURGLYPH ());
340 const RuleSet &rule_set = this+ruleSet[index];
341 /* LONGTERMTODO: Old code fetches glyph classes at most once and caches
342 * them across subrule lookups. Not sure it's worth it.
344 struct ContextLookupContext context = {
345 {match_class, apply_func},
348 return rule_set.apply (APPLY_ARG, context);
352 USHORT format; /* Format identifier--format = 2 */
354 coverage; /* Offset to Coverage table--from
355 * beginning of table */
357 classDef; /* Offset to glyph ClassDef table--from
358 * beginning of table */
359 OffsetArrayOf<RuleSet>
360 ruleSet; /* Array of RuleSet tables
361 * ordered by class */
363 ASSERT_SIZE (ContextFormat2, 8);
366 struct ContextFormat3
368 friend struct Context;
371 inline bool apply (APPLY_ARG_DEF, apply_lookup_func_t apply_func) const
373 unsigned int index = (this+coverage[0]) (IN_CURGLYPH ());
374 if (HB_LIKELY (index == NOT_COVERED))
377 const LookupRecord *lookupRecord = (const LookupRecord *)
378 ((const char *) coverage +
379 sizeof (coverage[0]) * glyphCount);
380 struct ContextLookupContext context = {
381 {match_coverage, apply_func},
384 return context_lookup (APPLY_ARG,
385 glyphCount, (const USHORT *) (coverage + 1),
386 lookupCount, lookupRecord,
391 USHORT format; /* Format identifier--format = 3 */
392 USHORT glyphCount; /* Number of glyphs in the input glyph
394 USHORT lookupCount; /* Number of LookupRecords */
396 coverage[]; /* Array of offsets to Coverage
397 * table in glyph sequence order */
398 LookupRecord lookupRecordX[]; /* Array of LookupRecords--in
401 ASSERT_SIZE (ContextFormat3, 6);
406 bool apply (APPLY_ARG_DEF, apply_lookup_func_t apply_func) const
409 case 1: return u.format1->apply (APPLY_ARG, apply_func);
410 case 2: return u.format2->apply (APPLY_ARG, apply_func);
411 case 3: return u.format3->apply (APPLY_ARG, apply_func);
412 default:return false;
418 USHORT format; /* Format identifier */
419 ContextFormat1 format1[];
420 ContextFormat2 format2[];
421 ContextFormat3 format3[];
424 ASSERT_SIZE (Context, 2);
427 /* Chaining Contextual lookups */
429 struct ChainContextLookupContext
435 static inline bool chain_context_lookup (APPLY_ARG_DEF,
436 unsigned int backtrackCount,
437 const USHORT backtrack[],
438 unsigned int inputCount, /* Including the first glyph (not matched) */
439 const USHORT input[], /* Array of input values--start with second glyph */
440 unsigned int lookaheadCount,
441 const USHORT lookahead[],
442 unsigned int lookupCount,
443 const LookupRecord lookupRecord[],
444 ChainContextLookupContext &context)
447 if (HB_UNLIKELY (buffer->out_pos < backtrackCount ||
448 buffer->in_pos + inputCount + lookaheadCount > buffer->in_length ||
449 inputCount + lookaheadCount > context_length))
453 return match_backtrack (APPLY_ARG,
454 backtrackCount, backtrack,
455 context.funcs.match, context.match_data[0]) &&
456 match_input (APPLY_ARG,
458 context.funcs.match, context.match_data[1],
460 match_lookahead (APPLY_ARG,
461 lookaheadCount, lookahead,
462 context.funcs.match, context.match_data[2],
464 (context_length = offset, true) &&
465 apply_lookup (APPLY_ARG,
467 lookupCount, lookupRecord,
468 context.funcs.apply);
473 friend struct ChainRuleSet;
476 inline bool apply (APPLY_ARG_DEF, ChainContextLookupContext &context) const
478 const HeadlessArrayOf<USHORT> &input = (const HeadlessArrayOf<USHORT>&)
479 *((const char *) &backtrack + backtrack.get_size ());
480 const ArrayOf<USHORT> &lookahead = (const ArrayOf<USHORT>&)
481 *((const char *) &input + input.get_size ());
482 const ArrayOf<LookupRecord> &lookup = (const ArrayOf<LookupRecord>&)
483 *((const char *) &lookahead + lookahead.get_size ());
484 return chain_context_lookup (APPLY_ARG,
485 backtrack.len, backtrack.array,
486 input.len, input.array + 1,
487 lookahead.len, lookahead.array,
488 lookup.len, lookup.array,
496 backtrack; /* Array of backtracking values
497 * (to be matched before the input
499 HeadlessArrayOf<USHORT>
500 inputX; /* Array of input values (start with
503 lookaheadX; /* Array of lookahead values's (to be
504 * matched after the input sequence) */
505 ArrayOf<LookupRecord>
506 lookupX; /* Array of LookupRecords--in
509 ASSERT_SIZE (ChainRule, 8);
513 inline bool apply (APPLY_ARG_DEF, ChainContextLookupContext &context) const
515 unsigned int num_rules = rule.len;
516 for (unsigned int i = 0; i < num_rules; i++)
518 if ((this+rule[i]).apply (APPLY_ARG, context))
526 OffsetArrayOf<ChainRule>
527 rule; /* Array of ChainRule tables
528 * ordered by preference */
530 ASSERT_SIZE (ChainRuleSet, 2);
532 struct ChainContextFormat1
534 friend struct ChainContext;
537 inline bool apply (APPLY_ARG_DEF, apply_lookup_func_t apply_func) const
539 unsigned int index = (this+coverage) (IN_CURGLYPH ());
540 if (HB_LIKELY (index == NOT_COVERED))
543 const ChainRuleSet &rule_set = this+ruleSet[index];
544 struct ChainContextLookupContext context = {
545 {match_glyph, apply_func},
548 return rule_set.apply (APPLY_ARG, context);
551 USHORT format; /* Format identifier--format = 1 */
553 coverage; /* Offset to Coverage table--from
554 * beginning of table */
555 OffsetArrayOf<ChainRuleSet>
556 ruleSet; /* Array of ChainRuleSet tables
557 * ordered by Coverage Index */
559 ASSERT_SIZE (ChainContextFormat1, 6);
561 struct ChainContextFormat2
563 friend struct ChainContext;
566 inline bool apply (APPLY_ARG_DEF, apply_lookup_func_t apply_func) const
568 unsigned int index = (this+coverage) (IN_CURGLYPH ());
569 if (HB_LIKELY (index == NOT_COVERED))
572 const ClassDef &backtrack_class_def = this+backtrackClassDef;
573 const ClassDef &input_class_def = this+inputClassDef;
574 const ClassDef &lookahead_class_def = this+lookaheadClassDef;
576 index = input_class_def (IN_CURGLYPH ());
577 const ChainRuleSet &rule_set = this+ruleSet[index];
578 /* LONGTERMTODO: Old code fetches glyph classes at most once and caches
579 * them across subrule lookups. Not sure it's worth it.
581 struct ChainContextLookupContext context = {
582 {match_class, apply_func},
583 {(char *) &backtrack_class_def,
584 (char *) &input_class_def,
585 (char *) &lookahead_class_def}
587 return rule_set.apply (APPLY_ARG, context);
591 USHORT format; /* Format identifier--format = 2 */
593 coverage; /* Offset to Coverage table--from
594 * beginning of table */
596 backtrackClassDef; /* Offset to glyph ClassDef table
597 * containing backtrack sequence
598 * data--from beginning of table */
600 inputClassDef; /* Offset to glyph ClassDef
601 * table containing input sequence
602 * data--from beginning of table */
604 lookaheadClassDef; /* Offset to glyph ClassDef table
605 * containing lookahead sequence
606 * data--from beginning of table */
607 OffsetArrayOf<ChainRuleSet>
608 ruleSet; /* Array of ChainRuleSet tables
609 * ordered by class */
611 ASSERT_SIZE (ChainContextFormat2, 12);
613 struct ChainContextFormat3
615 friend struct ChainContext;
619 inline bool apply (APPLY_ARG_DEF, apply_lookup_func_t apply_func) const
621 const OffsetArrayOf<Coverage> &input = (const OffsetArrayOf<Coverage>&)
622 *((const char *) &backtrack + backtrack.get_size ());
624 unsigned int index = (this+input[0]) (IN_CURGLYPH ());
625 if (HB_LIKELY (index == NOT_COVERED))
628 const OffsetArrayOf<Coverage> &lookahead = (const OffsetArrayOf<Coverage>&)
629 *((const char *) &input + input.get_size ());
630 const ArrayOf<LookupRecord> &lookup = (const ArrayOf<LookupRecord>&)
631 *((const char *) &lookahead + lookahead.get_size ());
632 struct ChainContextLookupContext context = {
633 {match_coverage, apply_func},
634 {(char *) this, (char *) this, (char *) this}
636 return chain_context_lookup (APPLY_ARG,
637 backtrack.len, (USHORT *) backtrack.array,
638 input.len, (USHORT *) input.array,
639 lookahead.len, (USHORT *) lookahead.array,
640 lookup.len, lookup.array,
646 USHORT format; /* Format identifier--format = 3 */
647 OffsetArrayOf<Coverage>
648 backtrack; /* Array of coverage tables
649 * in backtracking sequence, in glyph
651 OffsetArrayOf<Coverage>
652 inputX ; /* Array of coverage
653 * tables in input sequence, in glyph
655 OffsetArrayOf<Coverage>
656 lookaheadX; /* Array of coverage tables
657 * in lookahead sequence, in glyph
659 ArrayOf<LookupRecord>
660 lookupX; /* Array of LookupRecords--in
663 ASSERT_SIZE (ChainContextFormat3, 10);
668 bool apply (APPLY_ARG_DEF, apply_lookup_func_t apply_func) const
671 case 1: return u.format1->apply (APPLY_ARG, apply_func);
672 case 2: return u.format2->apply (APPLY_ARG, apply_func);
673 case 3: return u.format3->apply (APPLY_ARG, apply_func);
674 default:return false;
680 USHORT format; /* Format identifier */
681 ChainContextFormat1 format1[];
682 ChainContextFormat2 format2[];
683 ChainContextFormat3 format3[];
686 ASSERT_SIZE (ChainContext, 2);
689 struct ExtensionFormat1
691 friend struct Extension;
694 inline unsigned int get_type (void) const { return extensionLookupType; }
695 inline unsigned int get_offset (void) const { return (extensionOffset[0] << 16) + extensionOffset[1]; }
696 inline const LookupSubTable& get_subtable (void) const
698 unsigned int offset = get_offset ();
699 if (HB_UNLIKELY (!offset)) return Null(LookupSubTable);
700 return *(LookupSubTable*)(((char *) this) + offset);
704 USHORT format; /* Format identifier. Set to 1. */
705 USHORT extensionLookupType; /* Lookup type of subtable referenced
706 * by ExtensionOffset (i.e. the
707 * extension subtable). */
708 USHORT extensionOffset[2]; /* Offset to the extension subtable,
709 * of lookup type subtable.
710 * Defined as two shorts to avoid
711 * alignment requirements. */
713 ASSERT_SIZE (ExtensionFormat1, 8);
717 inline unsigned int get_type (void) const
720 case 1: return u.format1->get_type ();
724 inline const LookupSubTable& get_subtable (void) const
727 case 1: return u.format1->get_subtable ();
728 default:return Null(LookupSubTable);
734 USHORT format; /* Format identifier */
735 ExtensionFormat1 format1[];
738 ASSERT_SIZE (Extension, 2);
747 static const hb_tag_t GSUBTag = HB_TAG ('G','S','U','B');
748 static const hb_tag_t GPOSTag = HB_TAG ('G','P','O','S');
750 STATIC_DEFINE_GET_FOR_DATA_CHECK_MAJOR_VERSION (GSUBGPOS, 1);
752 DEFINE_TAG_LIST_INTERFACE (Script, script ); /* get_script_count (), get_script (i), get_script_tag (i) */
753 DEFINE_TAG_LIST_INTERFACE (Feature, feature); /* get_feature_count(), get_feature(i), get_feature_tag(i) */
754 DEFINE_LIST_INTERFACE (Lookup, lookup ); /* get_lookup_count (), get_lookup (i) */
756 // LONGTERMTODO bsearch
757 DEFINE_TAG_FIND_INTERFACE (Script, script ); /* find_script_index (), get_script_by_tag (tag) */
758 DEFINE_TAG_FIND_INTERFACE (Feature, feature); /* find_feature_index(), get_feature_by_tag(tag) */
761 FixedVersion version; /* Version of the GSUB/GPOS table--initially set
764 scriptList; /* ScriptList table */
765 OffsetTo<FeatureList>
766 featureList; /* FeatureList table */
768 lookupList; /* LookupList table */
770 ASSERT_SIZE (GSUBGPOS, 10);
773 #endif /* HB_OT_LAYOUT_GSUBGPOS_PRIVATE_H */