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-ot-layout-gdef-private.h"
31 #include "harfbuzz-buffer-private.h" /* XXX */
34 #define LOOKUP_ARGS_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) (LOOKUP_ARGS_DEF, unsigned int lookup_index);
55 apply_lookup_func_t apply;
59 static inline bool match_glyph (hb_codepoint_t glyph_id, const USHORT &value, char *data) {
60 return glyph_id == value;
63 static inline bool match_class (hb_codepoint_t glyph_id, const USHORT &value, char *data) {
64 const ClassDef &class_def = * (const ClassDef *) data;
65 return class_def.get_class (glyph_id) == value;
68 static inline bool match_coverage (hb_codepoint_t glyph_id, const USHORT &value, char *data) {
69 const OffsetTo<Coverage> &coverage = * (const OffsetTo<Coverage> *) &value;
70 return (data+coverage) (glyph_id) != NOT_COVERED;
74 static inline bool match_input (LOOKUP_ARGS_DEF,
75 unsigned int count, /* Including the first glyph (not matched) */
76 const USHORT input[], /* Array of input values--start with second glyph */
77 match_func_t match_func,
79 unsigned int *context_length_out)
82 unsigned int end = MIN (buffer->in_length, buffer->in_pos + context_length);
83 if (HB_UNLIKELY (buffer->in_pos + count > end))
86 for (i = 1, j = buffer->in_pos + 1; i < count; i++, j++) {
87 while (!_hb_ot_layout_check_glyph_property (layout, IN_ITEM (j), lookup_flag, &property)) {
88 if (HB_UNLIKELY (j + count - i == end))
93 if (HB_LIKELY (!match_func (IN_GLYPH(j), input[i - 1], match_data)))
97 *context_length_out = j - buffer->in_pos;
102 static inline bool match_backtrack (LOOKUP_ARGS_DEF,
104 const USHORT backtrack[],
105 match_func_t match_func,
108 if (HB_UNLIKELY (buffer->out_pos < count))
111 for (unsigned int i = 0, j = buffer->out_pos - 1; i < count; i++, j--) {
112 while (!_hb_ot_layout_check_glyph_property (layout, OUT_ITEM (j), lookup_flag, &property)) {
113 if (HB_UNLIKELY (j + 1 == count - i))
118 if (HB_LIKELY (!match_func (OUT_GLYPH(j), backtrack[i], match_data)))
125 static inline bool match_lookahead (LOOKUP_ARGS_DEF,
127 const USHORT lookahead[],
128 match_func_t match_func,
133 unsigned int end = MIN (buffer->in_length, buffer->in_pos + context_length);
134 if (HB_UNLIKELY (buffer->in_pos + offset + count > end))
137 for (i = 0, j = buffer->in_pos + offset; i < count; i++, j++) {
138 while (!_hb_ot_layout_check_glyph_property (layout, OUT_ITEM (j), lookup_flag, &property)) {
139 if (HB_UNLIKELY (j + count - i == end))
144 if (HB_LIKELY (!match_func (IN_GLYPH(j), lookahead[i], match_data)))
152 struct LookupRecord {
154 USHORT sequenceIndex; /* Index into current glyph
155 * sequence--first glyph = 0 */
156 USHORT lookupListIndex; /* Lookup to apply to that
157 * position--zero--based */
159 ASSERT_SIZE (LookupRecord, 4);
161 static inline bool apply_lookup (LOOKUP_ARGS_DEF,
162 unsigned int count, /* Including the first glyph */
163 unsigned int lookupCount,
164 const LookupRecord lookupRecord[], /* Array of LookupRecords--in design order */
165 apply_lookup_func_t apply_func)
167 unsigned int record_count = lookupCount;
168 const LookupRecord *record = lookupRecord;
170 /* XXX We have to jump non-matching glyphs when applying too, right? */
171 /* TODO We don't support lookupRecord arrays that are not increasing:
172 * Should be easy for in_place ones at least. */
173 for (unsigned int i = 0; i < count;)
175 if (record_count && i == record->sequenceIndex)
177 unsigned int old_pos = buffer->in_pos;
180 bool done = apply_func (LOOKUP_ARGS, record->lookupListIndex);
184 i += buffer->in_pos - old_pos;
192 /* No lookup applied for this index */
193 _hb_buffer_next_glyph (buffer);
202 /* Contextual lookups */
204 struct ContextLookupContext {
209 static inline bool context_lookup (LOOKUP_ARGS_DEF,
210 unsigned int inputCount, /* Including the first glyph (not matched) */
211 const USHORT input[], /* Array of input values--start with second glyph */
212 unsigned int lookupCount,
213 const LookupRecord lookupRecord[],
214 ContextLookupContext &context)
217 if (HB_UNLIKELY (buffer->in_pos + inputCount > buffer->in_length ||
218 context_length < inputCount))
221 return match_input (LOOKUP_ARGS,
223 context.funcs.match, context.match_data,
225 apply_lookup (LOOKUP_ARGS,
227 lookupCount, lookupRecord,
228 context.funcs.apply);
233 friend struct RuleSet;
236 inline bool apply (LOOKUP_ARGS_DEF, ContextLookupContext &context) const {
237 const LookupRecord *lookupRecord = (const LookupRecord *)
238 ((const char *) input +
239 sizeof (input[0]) * (inputCount ? inputCount - 1 : 0));
240 return context_lookup (LOOKUP_ARGS,
249 USHORT inputCount; /* Total number of glyphs in input
250 * glyph sequence--includes the first
252 USHORT lookupCount; /* Number of LookupRecords */
253 USHORT input[]; /* Array of match inputs--start with
255 LookupRecord lookupRecordX[]; /* Array of LookupRecords--in
258 ASSERT_SIZE (Rule, 4);
262 inline bool apply (LOOKUP_ARGS_DEF, ContextLookupContext &context) const {
264 unsigned int num_rules = rule.len;
265 for (unsigned int i = 0; i < num_rules; i++) {
266 if ((this+rule[i]).apply (LOOKUP_ARGS, context))
275 rule; /* Array of Rule tables
276 * ordered by preference */
280 struct ContextFormat1 {
282 friend struct Context;
285 inline bool apply (LOOKUP_ARGS_DEF, apply_lookup_func_t apply_func) const {
287 unsigned int index = (this+coverage) (IN_CURGLYPH ());
288 if (G_LIKELY (index == NOT_COVERED))
291 const RuleSet &rule_set = this+ruleSet[index];
292 struct ContextLookupContext context = {
293 {match_glyph, apply_func},
296 return rule_set.apply (LOOKUP_ARGS, context);
300 USHORT format; /* Format identifier--format = 1 */
302 coverage; /* Offset to Coverage table--from
303 * beginning of table */
304 OffsetArrayOf<RuleSet>
305 ruleSet; /* Array of RuleSet tables
306 * ordered by Coverage Index */
308 ASSERT_SIZE (ContextFormat1, 6);
311 struct ContextFormat2 {
313 friend struct Context;
316 inline bool apply (LOOKUP_ARGS_DEF, apply_lookup_func_t apply_func) const {
318 unsigned int index = (this+coverage) (IN_CURGLYPH ());
319 if (G_LIKELY (index == NOT_COVERED))
322 const ClassDef &class_def = this+classDef;
323 index = class_def (IN_CURGLYPH ());
324 const RuleSet &rule_set = this+ruleSet[index];
325 /* LONGTERMTODO: Old code fetches glyph classes at most once and caches
326 * them across subrule lookups. Not sure it's worth it.
328 struct ContextLookupContext context = {
329 {match_class, apply_func},
332 return rule_set.apply (LOOKUP_ARGS, context);
336 USHORT format; /* Format identifier--format = 2 */
338 coverage; /* Offset to Coverage table--from
339 * beginning of table */
341 classDef; /* Offset to glyph ClassDef table--from
342 * beginning of table */
343 OffsetArrayOf<RuleSet>
344 ruleSet; /* Array of RuleSet tables
345 * ordered by class */
347 ASSERT_SIZE (ContextFormat2, 8);
350 struct ContextFormat3 {
352 friend struct Context;
355 inline bool apply (LOOKUP_ARGS_DEF, apply_lookup_func_t apply_func) const {
357 unsigned int index = (this+coverage[0]) (IN_CURGLYPH ());
358 if (G_LIKELY (index == NOT_COVERED))
361 const LookupRecord *lookupRecord = (const LookupRecord *)
362 ((const char *) coverage +
363 sizeof (coverage[0]) * glyphCount);
364 struct ContextLookupContext context = {
365 {match_coverage, apply_func},
368 return context_lookup (LOOKUP_ARGS,
370 (const USHORT *) (coverage + 1),
377 USHORT format; /* Format identifier--format = 3 */
378 USHORT glyphCount; /* Number of glyphs in the input glyph
380 USHORT lookupCount; /* Number of LookupRecords */
382 coverage[]; /* Array of offsets to Coverage
383 * table in glyph sequence order */
384 LookupRecord lookupRecordX[]; /* Array of LookupRecords--in
387 ASSERT_SIZE (ContextFormat3, 6);
392 bool apply (LOOKUP_ARGS_DEF, apply_lookup_func_t apply_func) const {
394 case 1: return u.format1->apply (LOOKUP_ARGS, apply_func);
395 case 2: return u.format2->apply (LOOKUP_ARGS, apply_func);
396 case 3: return u.format3->apply (LOOKUP_ARGS, apply_func);
397 default:return false;
403 USHORT format; /* Format identifier */
404 ContextFormat1 format1[];
405 ContextFormat2 format2[];
406 ContextFormat3 format3[];
409 ASSERT_SIZE (Context, 2);
412 /* Chaining Contextual lookups */
414 struct ChainContextLookupContext {
419 static inline bool chain_context_lookup (LOOKUP_ARGS_DEF,
420 unsigned int backtrackCount,
421 const USHORT backtrack[],
422 unsigned int inputCount, /* Including the first glyph (not matched) */
423 const USHORT input[], /* Array of input values--start with second glyph */
424 unsigned int lookaheadCount,
425 const USHORT lookahead[],
426 unsigned int lookupCount,
427 const LookupRecord lookupRecord[],
428 ChainContextLookupContext &context)
431 if (HB_UNLIKELY (buffer->out_pos < backtrackCount ||
432 buffer->in_pos + inputCount + lookaheadCount > buffer->in_length))
436 return match_backtrack (LOOKUP_ARGS,
437 backtrackCount, backtrack,
438 context.funcs.match, context.match_data[0]) &&
439 match_input (LOOKUP_ARGS,
441 context.funcs.match, context.match_data[1],
443 match_lookahead (LOOKUP_ARGS,
444 lookaheadCount, lookahead,
445 context.funcs.match, context.match_data[2],
447 (context_length = offset, true) &&
448 apply_lookup (LOOKUP_ARGS,
450 lookupCount, lookupRecord,
451 context.funcs.apply);
456 friend struct ChainRuleSet;
459 inline bool apply (LOOKUP_ARGS_DEF, ChainContextLookupContext &context) const {
460 const HeadlessArrayOf<USHORT> &input = * (const HeadlessArrayOf<USHORT> *)
461 ((const char *) &backtrack + backtrack.get_size ());
462 const ArrayOf<USHORT> &lookahead = * (const ArrayOf<USHORT> *)
463 ((const char *) &input + input.get_size ());
464 const ArrayOf<LookupRecord> &lookup = * (const ArrayOf<LookupRecord> *)
465 ((const char *) &lookahead + lookahead.get_size ());
466 return chain_context_lookup (LOOKUP_ARGS,
482 backtrack; /* Array of backtracking values
483 * (to be matched before the input
485 HeadlessArrayOf<USHORT>
486 inputX; /* Array of input values (start with
489 lookaheadX; /* Array of lookahead values's (to be
490 * matched after the input sequence) */
491 ArrayOf<LookupRecord>
492 lookupX; /* Array of LookupRecords--in
495 ASSERT_SIZE (ChainRule, 8);
497 struct ChainRuleSet {
499 inline bool apply (LOOKUP_ARGS_DEF, ChainContextLookupContext &context) const {
501 unsigned int num_rules = rule.len;
502 for (unsigned int i = 0; i < num_rules; i++) {
503 if ((this+rule[i]).apply (LOOKUP_ARGS, context))
511 OffsetArrayOf<ChainRule>
512 rule; /* Array of ChainRule tables
513 * ordered by preference */
515 ASSERT_SIZE (ChainRuleSet, 2);
517 struct ChainContextFormat1 {
519 friend struct ChainContext;
522 inline bool apply (LOOKUP_ARGS_DEF, apply_lookup_func_t apply_func) const {
524 unsigned int index = (this+coverage) (IN_CURGLYPH ());
525 if (G_LIKELY (index == NOT_COVERED))
528 const ChainRuleSet &rule_set = this+ruleSet[index];
529 struct ChainContextLookupContext context = {
530 {match_glyph, apply_func},
533 return rule_set.apply (LOOKUP_ARGS, context);
536 USHORT format; /* Format identifier--format = 1 */
538 coverage; /* Offset to Coverage table--from
539 * beginning of table */
540 OffsetArrayOf<ChainRuleSet>
541 ruleSet; /* Array of ChainRuleSet tables
542 * ordered by Coverage Index */
544 ASSERT_SIZE (ChainContextFormat1, 6);
546 struct ChainContextFormat2 {
548 friend struct ChainContext;
551 inline bool apply (LOOKUP_ARGS_DEF, apply_lookup_func_t apply_func) const {
553 unsigned int index = (this+coverage) (IN_CURGLYPH ());
554 if (G_LIKELY (index == NOT_COVERED))
557 const ClassDef &backtrack_class_def = this+backtrackClassDef;
558 const ClassDef &input_class_def = this+inputClassDef;
559 const ClassDef &lookahead_class_def = this+lookaheadClassDef;
561 index = input_class_def (IN_CURGLYPH ());
562 const ChainRuleSet &rule_set = this+ruleSet[index];
563 /* LONGTERMTODO: Old code fetches glyph classes at most once and caches
564 * them across subrule lookups. Not sure it's worth it.
566 struct ChainContextLookupContext context = {
567 {match_class, apply_func},
568 {(char *) &backtrack_class_def,
569 (char *) &input_class_def,
570 (char *) &lookahead_class_def}
572 return rule_set.apply (LOOKUP_ARGS, context);
576 USHORT format; /* Format identifier--format = 2 */
578 coverage; /* Offset to Coverage table--from
579 * beginning of table */
581 backtrackClassDef; /* Offset to glyph ClassDef table
582 * containing backtrack sequence
583 * data--from beginning of table */
585 inputClassDef; /* Offset to glyph ClassDef
586 * table containing input sequence
587 * data--from beginning of table */
589 lookaheadClassDef; /* Offset to glyph ClassDef table
590 * containing lookahead sequence
591 * data--from beginning of table */
592 OffsetArrayOf<ChainRuleSet>
593 ruleSet; /* Array of ChainRuleSet tables
594 * ordered by class */
596 ASSERT_SIZE (ChainContextFormat2, 12);
598 struct ChainContextFormat3 {
600 friend struct ChainContext;
604 inline bool apply_coverage (LOOKUP_ARGS_DEF, apply_lookup_func_t apply_func) const {
607 inline bool apply (LOOKUP_ARGS_DEF, apply_lookup_func_t apply_func) const {
609 const OffsetArrayOf<Coverage> &input = * (const OffsetArrayOf<Coverage> *)
610 ((const char *) &backtrack + backtrack.get_size ());
612 unsigned int index = (this+input[0]) (IN_CURGLYPH ());
613 if (G_LIKELY (index == NOT_COVERED))
616 const OffsetArrayOf<Coverage> &lookahead = * (const OffsetArrayOf<Coverage> *)
617 ((const char *) &input + input.get_size ());
618 const ArrayOf<LookupRecord> &lookup = * (const ArrayOf<LookupRecord> *)
619 ((const char *) &lookahead + lookahead.get_size ());
620 struct ChainContextLookupContext context = {
621 {match_coverage, apply_func},
622 {(char *) this, (char *) this, (char *) this}
624 return chain_context_lookup (LOOKUP_ARGS,
626 (USHORT *) backtrack.array,
628 (USHORT *) input.array,
630 (USHORT *) lookahead.array,
638 USHORT format; /* Format identifier--format = 3 */
639 OffsetArrayOf<Coverage>
640 backtrack; /* Array of coverage tables
641 * in backtracking sequence, in glyph
643 OffsetArrayOf<Coverage>
644 inputX ; /* Array of coverage
645 * tables in input sequence, in glyph
647 OffsetArrayOf<Coverage>
648 lookaheadX; /* Array of coverage tables
649 * in lookahead sequence, in glyph
651 ArrayOf<LookupRecord>
652 lookupX; /* Array of LookupRecords--in
655 ASSERT_SIZE (ChainContextFormat3, 10);
657 struct ChainContext {
660 bool apply (LOOKUP_ARGS_DEF, apply_lookup_func_t apply_func) const {
662 case 1: return u.format1->apply (LOOKUP_ARGS, apply_func);
663 case 2: return u.format2->apply (LOOKUP_ARGS, apply_func);
664 case 3: return u.format3->apply (LOOKUP_ARGS, apply_func);
665 default:return false;
671 USHORT format; /* Format identifier */
672 ChainContextFormat1 format1[];
673 ChainContextFormat2 format2[];
674 ChainContextFormat3 format3[];
677 ASSERT_SIZE (ChainContext, 2);
685 static const hb_tag_t GSUBTag = HB_TAG ('G','S','U','B');
686 static const hb_tag_t GPOSTag = HB_TAG ('G','P','O','S');
688 STATIC_DEFINE_GET_FOR_DATA (GSUBGPOS);
689 /* XXX check version here? */
691 DEFINE_TAG_LIST_INTERFACE (Script, script ); /* get_script_count (), get_script (i), get_script_tag (i) */
692 DEFINE_TAG_LIST_INTERFACE (Feature, feature); /* get_feature_count(), get_feature(i), get_feature_tag(i) */
693 DEFINE_LIST_INTERFACE (Lookup, lookup ); /* get_lookup_count (), get_lookup (i) */
695 // LONGTERMTODO bsearch
696 DEFINE_TAG_FIND_INTERFACE (Script, script ); /* find_script_index (), get_script_by_tag (tag) */
697 DEFINE_TAG_FIND_INTERFACE (Feature, feature); /* find_feature_index(), get_feature_by_tag(tag) */
700 Fixed_Version version; /* Version of the GSUB/GPOS table--initially set
703 scriptList; /* ScriptList table */
704 OffsetTo<FeatureList>
705 featureList; /* FeatureList table */
707 lookupList; /* LookupList table */
709 ASSERT_SIZE (GSUBGPOS, 10);
712 #endif /* HB_OT_LAYOUT_GSUBGPOS_PRIVATE_H */