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)
83 /* XXX context_length should also be checked when skipping glyphs, right?
84 * What does context_length really mean, anyway? */
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 == buffer->in_length))
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,
110 for (i = 0, j = buffer->out_pos - 1; i < count; i++, j--) {
111 while (!_hb_ot_layout_check_glyph_property (layout, OUT_ITEM (j), lookup_flag, &property)) {
112 if (HB_UNLIKELY (j + 1 == count - i))
117 if (HB_LIKELY (!match_func (OUT_GLYPH(j), backtrack[i], match_data)))
124 static inline bool match_lookahead (LOOKUP_ARGS_DEF,
126 const USHORT lookahead[],
127 match_func_t match_func,
133 for (i = 0, j = buffer->in_pos + offset; i < count; i++, j++) {
134 while (!_hb_ot_layout_check_glyph_property (layout, OUT_ITEM (j), lookup_flag, &property)) {
135 if (HB_UNLIKELY (j + count - i == buffer->in_length))
140 if (HB_LIKELY (!match_func (IN_GLYPH(j), lookahead[i], match_data)))
148 struct LookupRecord {
150 USHORT sequenceIndex; /* Index into current glyph
151 * sequence--first glyph = 0 */
152 USHORT lookupListIndex; /* Lookup to apply to that
153 * position--zero--based */
155 ASSERT_SIZE (LookupRecord, 4);
157 static inline bool apply_lookup (LOOKUP_ARGS_DEF,
158 unsigned int count, /* Including the first glyph */
159 unsigned int lookupCount,
160 const LookupRecord lookupRecord[], /* Array of LookupRecords--in design order */
161 apply_lookup_func_t apply_func)
163 unsigned int record_count = lookupCount;
164 const LookupRecord *record = lookupRecord;
166 /* XXX We have to jump non-matching glyphs when applying too, right? */
167 /* XXX We don't support lookupRecord arrays that are not increasing:
168 * Should be easy for in_place ones at least. */
169 for (unsigned int i = 0; i < count;)
171 if (record_count && i == record->sequenceIndex)
173 unsigned int old_pos = buffer->in_pos;
176 bool done = apply_func (LOOKUP_ARGS, record->lookupListIndex);
180 i += buffer->in_pos - old_pos;
188 /* No lookup applied for this index */
189 _hb_buffer_next_glyph (buffer);
198 /* Contextual lookups */
200 struct ContextLookupContext {
205 static inline bool context_lookup (LOOKUP_ARGS_DEF,
206 unsigned int inputCount, /* Including the first glyph (not matched) */
207 const USHORT input[], /* Array of input values--start with second glyph */
208 unsigned int lookupCount,
209 const LookupRecord lookupRecord[],
210 ContextLookupContext &context)
213 if (HB_UNLIKELY (buffer->in_pos + inputCount > buffer->in_length ||
214 context_length < inputCount))
217 return match_input (LOOKUP_ARGS,
219 context.funcs.match, context.match_data,
221 apply_lookup (LOOKUP_ARGS,
223 lookupCount, lookupRecord,
224 context.funcs.apply);
229 friend struct RuleSet;
232 inline bool apply (LOOKUP_ARGS_DEF, ContextLookupContext &context) const {
233 const LookupRecord *lookupRecord = (const LookupRecord *)
234 ((const char *) input +
235 sizeof (input[0]) * (inputCount ? inputCount - 1 : 0));
236 return context_lookup (LOOKUP_ARGS,
245 USHORT inputCount; /* Total number of glyphs in input
246 * glyph sequence--includes the first
248 USHORT lookupCount; /* Number of LookupRecords */
249 USHORT input[]; /* Array of match inputs--start with
251 LookupRecord lookupRecordX[]; /* Array of LookupRecords--in
254 ASSERT_SIZE (Rule, 4);
258 inline bool apply (LOOKUP_ARGS_DEF, ContextLookupContext &context) const {
260 unsigned int num_rules = rule.len;
261 for (unsigned int i = 0; i < num_rules; i++) {
262 if ((this+rule[i]).apply (LOOKUP_ARGS, context))
271 rule; /* Array of Rule tables
272 * ordered by preference */
276 struct ContextFormat1 {
278 friend struct Context;
281 inline bool apply (LOOKUP_ARGS_DEF, apply_lookup_func_t apply_func) const {
283 unsigned int index = (this+coverage) (IN_CURGLYPH ());
284 if (G_LIKELY (index == NOT_COVERED))
287 const RuleSet &rule_set = this+ruleSet[index];
288 struct ContextLookupContext context = {
289 {match_glyph, apply_func},
292 return rule_set.apply (LOOKUP_ARGS, context);
296 USHORT format; /* Format identifier--format = 1 */
298 coverage; /* Offset to Coverage table--from
299 * beginning of table */
300 OffsetArrayOf<RuleSet>
301 ruleSet; /* Array of RuleSet tables
302 * ordered by Coverage Index */
304 ASSERT_SIZE (ContextFormat1, 6);
307 struct ContextFormat2 {
309 friend struct Context;
312 inline bool apply (LOOKUP_ARGS_DEF, apply_lookup_func_t apply_func) const {
314 unsigned int index = (this+coverage) (IN_CURGLYPH ());
315 if (G_LIKELY (index == NOT_COVERED))
318 const ClassDef &class_def = this+classDef;
319 index = class_def (IN_CURGLYPH ());
320 const RuleSet &rule_set = this+ruleSet[index];
321 /* LONGTERMTODO: Old code fetches glyph classes at most once and caches
322 * them across subrule lookups. Not sure it's worth it.
324 struct ContextLookupContext context = {
325 {match_class, apply_func},
328 return rule_set.apply (LOOKUP_ARGS, context);
332 USHORT format; /* Format identifier--format = 2 */
334 coverage; /* Offset to Coverage table--from
335 * beginning of table */
337 classDef; /* Offset to glyph ClassDef table--from
338 * beginning of table */
339 OffsetArrayOf<RuleSet>
340 ruleSet; /* Array of RuleSet tables
341 * ordered by class */
343 ASSERT_SIZE (ContextFormat2, 8);
346 struct ContextFormat3 {
348 friend struct Context;
351 inline bool apply (LOOKUP_ARGS_DEF, apply_lookup_func_t apply_func) const {
353 unsigned int index = (this+coverage[0]) (IN_CURGLYPH ());
354 if (G_LIKELY (index == NOT_COVERED))
357 const LookupRecord *lookupRecord = (const LookupRecord *)
358 ((const char *) coverage +
359 sizeof (coverage[0]) * glyphCount);
360 struct ContextLookupContext context = {
361 {match_coverage, apply_func},
364 return context_lookup (LOOKUP_ARGS,
366 (const USHORT *) (coverage + 1),
373 USHORT format; /* Format identifier--format = 3 */
374 USHORT glyphCount; /* Number of glyphs in the input glyph
376 USHORT lookupCount; /* Number of LookupRecords */
378 coverage[]; /* Array of offsets to Coverage
379 * table in glyph sequence order */
380 LookupRecord lookupRecordX[]; /* Array of LookupRecords--in
383 ASSERT_SIZE (ContextFormat3, 6);
388 bool apply (LOOKUP_ARGS_DEF, apply_lookup_func_t apply_func) const {
390 case 1: return u.format1->apply (LOOKUP_ARGS, apply_func);
391 case 2: return u.format2->apply (LOOKUP_ARGS, apply_func);
392 case 3: return u.format3->apply (LOOKUP_ARGS, apply_func);
393 default:return false;
399 USHORT format; /* Format identifier */
400 ContextFormat1 format1[];
401 ContextFormat2 format2[];
402 ContextFormat3 format3[];
405 ASSERT_SIZE (Context, 2);
408 /* Chaining Contextual lookups */
410 struct ChainContextLookupContext {
415 static inline bool chain_context_lookup (LOOKUP_ARGS_DEF,
416 unsigned int backtrackCount,
417 const USHORT backtrack[],
418 unsigned int inputCount, /* Including the first glyph (not matched) */
419 const USHORT input[], /* Array of input values--start with second glyph */
420 unsigned int lookaheadCount,
421 const USHORT lookahead[],
422 unsigned int lookupCount,
423 const LookupRecord lookupRecord[],
424 ChainContextLookupContext &context)
427 if (HB_UNLIKELY (buffer->out_pos < backtrackCount ||
428 buffer->in_pos + inputCount + lookaheadCount > buffer->in_length))
432 return match_backtrack (LOOKUP_ARGS,
433 backtrackCount, backtrack,
434 context.funcs.match, context.match_data[0]) &&
435 match_input (LOOKUP_ARGS,
437 context.funcs.match, context.match_data[1],
439 (context_length -= offset, true) &&
440 match_lookahead (LOOKUP_ARGS,
441 lookaheadCount, lookahead,
442 context.funcs.match, context.match_data[2],
444 (context_length = offset, true) &&
445 apply_lookup (LOOKUP_ARGS,
447 lookupCount, lookupRecord,
448 context.funcs.apply);
453 friend struct ChainRuleSet;
456 inline bool apply (LOOKUP_ARGS_DEF, ChainContextLookupContext &context) const {
457 const HeadlessArrayOf<USHORT> &input = * (const HeadlessArrayOf<USHORT> *)
458 ((const char *) &backtrack + backtrack.get_size ());
459 const ArrayOf<USHORT> &lookahead = * (const ArrayOf<USHORT> *)
460 ((const char *) &input + input.get_size ());
461 const ArrayOf<LookupRecord> &lookup = * (const ArrayOf<LookupRecord> *)
462 ((const char *) &lookahead + lookahead.get_size ());
463 return chain_context_lookup (LOOKUP_ARGS,
479 backtrack; /* Array of backtracking values
480 * (to be matched before the input
482 HeadlessArrayOf<USHORT>
483 inputX; /* Array of input values (start with
486 lookaheadX; /* Array of lookahead values's (to be
487 * matched after the input sequence) */
488 ArrayOf<LookupRecord>
489 lookupX; /* Array of LookupRecords--in
492 ASSERT_SIZE (ChainRule, 8);
494 struct ChainRuleSet {
496 inline bool apply (LOOKUP_ARGS_DEF, ChainContextLookupContext &context) const {
498 unsigned int num_rules = rule.len;
499 for (unsigned int i = 0; i < num_rules; i++) {
500 if ((this+rule[i]).apply (LOOKUP_ARGS, context))
508 OffsetArrayOf<ChainRule>
509 rule; /* Array of ChainRule tables
510 * ordered by preference */
512 ASSERT_SIZE (ChainRuleSet, 2);
514 struct ChainContextFormat1 {
516 friend struct ChainContext;
519 inline bool apply (LOOKUP_ARGS_DEF, apply_lookup_func_t apply_func) const {
521 unsigned int index = (this+coverage) (IN_CURGLYPH ());
522 if (G_LIKELY (index == NOT_COVERED))
525 const ChainRuleSet &rule_set = this+ruleSet[index];
526 struct ChainContextLookupContext context = {
527 {match_glyph, apply_func},
530 return rule_set.apply (LOOKUP_ARGS, context);
533 USHORT format; /* Format identifier--format = 1 */
535 coverage; /* Offset to Coverage table--from
536 * beginning of table */
537 OffsetArrayOf<ChainRuleSet>
538 ruleSet; /* Array of ChainRuleSet tables
539 * ordered by Coverage Index */
541 ASSERT_SIZE (ChainContextFormat1, 6);
543 struct ChainContextFormat2 {
545 friend struct ChainContext;
548 inline bool apply (LOOKUP_ARGS_DEF, apply_lookup_func_t apply_func) const {
550 unsigned int index = (this+coverage) (IN_CURGLYPH ());
551 if (G_LIKELY (index == NOT_COVERED))
554 const ClassDef &backtrack_class_def = this+backtrackClassDef;
555 const ClassDef &input_class_def = this+inputClassDef;
556 const ClassDef &lookahead_class_def = this+lookaheadClassDef;
558 index = input_class_def (IN_CURGLYPH ());
559 const ChainRuleSet &rule_set = this+ruleSet[index];
560 /* LONGTERMTODO: Old code fetches glyph classes at most once and caches
561 * them across subrule lookups. Not sure it's worth it.
563 struct ChainContextLookupContext context = {
564 {match_class, apply_func},
565 {(char *) &backtrack_class_def,
566 (char *) &input_class_def,
567 (char *) &lookahead_class_def}
569 return rule_set.apply (LOOKUP_ARGS, context);
573 USHORT format; /* Format identifier--format = 2 */
575 coverage; /* Offset to Coverage table--from
576 * beginning of table */
578 backtrackClassDef; /* Offset to glyph ClassDef table
579 * containing backtrack sequence
580 * data--from beginning of table */
582 inputClassDef; /* Offset to glyph ClassDef
583 * table containing input sequence
584 * data--from beginning of table */
586 lookaheadClassDef; /* Offset to glyph ClassDef table
587 * containing lookahead sequence
588 * data--from beginning of table */
589 OffsetArrayOf<ChainRuleSet>
590 ruleSet; /* Array of ChainRuleSet tables
591 * ordered by class */
593 ASSERT_SIZE (ChainContextFormat2, 12);
595 struct ChainContextFormat3 {
597 friend struct ChainContext;
601 inline bool apply_coverage (LOOKUP_ARGS_DEF, apply_lookup_func_t apply_func) const {
604 inline bool apply (LOOKUP_ARGS_DEF, apply_lookup_func_t apply_func) const {
606 const OffsetArrayOf<Coverage> &input = * (const OffsetArrayOf<Coverage> *)
607 ((const char *) &backtrack + backtrack.get_size ());
609 unsigned int index = (this+input[0]) (IN_CURGLYPH ());
610 if (G_LIKELY (index == NOT_COVERED))
613 const OffsetArrayOf<Coverage> &lookahead = * (const OffsetArrayOf<Coverage> *)
614 ((const char *) &input + input.get_size ());
615 const ArrayOf<LookupRecord> &lookup = * (const ArrayOf<LookupRecord> *)
616 ((const char *) &lookahead + lookahead.get_size ());
617 struct ChainContextLookupContext context = {
618 {match_coverage, apply_func},
619 {(char *) this, (char *) this, (char *) this}
621 return chain_context_lookup (LOOKUP_ARGS,
623 (USHORT *) backtrack.array,
625 (USHORT *) input.array,
627 (USHORT *) lookahead.array,
635 USHORT format; /* Format identifier--format = 3 */
636 OffsetArrayOf<Coverage>
637 backtrack; /* Array of coverage tables
638 * in backtracking sequence, in glyph
640 OffsetArrayOf<Coverage>
641 inputX ; /* Array of coverage
642 * tables in input sequence, in glyph
644 OffsetArrayOf<Coverage>
645 lookaheadX; /* Array of coverage tables
646 * in lookahead sequence, in glyph
648 ArrayOf<LookupRecord>
649 lookupX; /* Array of LookupRecords--in
652 ASSERT_SIZE (ChainContextFormat3, 10);
654 struct ChainContext {
657 bool apply (LOOKUP_ARGS_DEF, apply_lookup_func_t apply_func) const {
659 case 1: return u.format1->apply (LOOKUP_ARGS, apply_func);
660 case 2: return u.format2->apply (LOOKUP_ARGS, apply_func);
661 case 3: return u.format3->apply (LOOKUP_ARGS, apply_func);
662 default:return false;
668 USHORT format; /* Format identifier */
669 ChainContextFormat1 format1[];
670 ChainContextFormat2 format2[];
671 ChainContextFormat3 format3[];
674 ASSERT_SIZE (ChainContext, 2);
682 static const hb_tag_t GSUBTag = HB_TAG ('G','S','U','B');
683 static const hb_tag_t GPOSTag = HB_TAG ('G','P','O','S');
685 STATIC_DEFINE_GET_FOR_DATA (GSUBGPOS);
686 /* XXX check version here? */
688 DEFINE_TAG_LIST_INTERFACE (Script, script ); /* get_script_count (), get_script (i), get_script_tag (i) */
689 DEFINE_TAG_LIST_INTERFACE (Feature, feature); /* get_feature_count(), get_feature(i), get_feature_tag(i) */
690 DEFINE_LIST_INTERFACE (Lookup, lookup ); /* get_lookup_count (), get_lookup (i) */
692 // LONGTERMTODO bsearch
693 DEFINE_TAG_FIND_INTERFACE (Script, script ); /* find_script_index (), get_script_by_tag (tag) */
694 DEFINE_TAG_FIND_INTERFACE (Feature, feature); /* find_feature_index(), get_feature_by_tag(tag) */
697 Fixed_Version version; /* Version of the GSUB/GPOS table--initially set
700 scriptList; /* ScriptList table */
701 OffsetTo<FeatureList>
702 featureList; /* FeatureList table */
704 lookupList; /* LookupList table */
706 ASSERT_SIZE (GSUBGPOS, 10);
709 #endif /* HB_OT_LAYOUT_GSUBGPOS_PRIVATE_H */