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 end = MIN (buffer->in_length, buffer->in_pos + context_length);
168 if (HB_UNLIKELY (buffer->in_pos + count > end))
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; i++) {
174 while (!_hb_ot_layout_check_glyph_property (layout, IN_CURITEM (), lookup_flag, &property)) {
175 if (HB_UNLIKELY (buffer->in_pos == end))
177 /* No lookup applied for this index */
178 _hb_buffer_next_glyph (buffer);
181 if (lookupCount && i == lookupRecord->sequenceIndex)
183 unsigned int old_pos = buffer->in_pos;
186 bool done = apply_func (LOOKUP_ARGS, lookupRecord->lookupListIndex);
190 i += buffer->in_pos - old_pos;
198 /* No lookup applied for this index */
199 _hb_buffer_next_glyph (buffer);
208 /* Contextual lookups */
210 struct ContextLookupContext {
215 static inline bool context_lookup (LOOKUP_ARGS_DEF,
216 unsigned int inputCount, /* Including the first glyph (not matched) */
217 const USHORT input[], /* Array of input values--start with second glyph */
218 unsigned int lookupCount,
219 const LookupRecord lookupRecord[],
220 ContextLookupContext &context)
223 if (HB_UNLIKELY (buffer->in_pos + inputCount > buffer->in_length ||
224 inputCount > context_length))
227 return match_input (LOOKUP_ARGS,
229 context.funcs.match, context.match_data,
231 apply_lookup (LOOKUP_ARGS,
233 lookupCount, lookupRecord,
234 context.funcs.apply);
239 friend struct RuleSet;
242 inline bool apply (LOOKUP_ARGS_DEF, ContextLookupContext &context) const {
243 const LookupRecord *lookupRecord = (const LookupRecord *)
244 ((const char *) input +
245 sizeof (input[0]) * (inputCount ? inputCount - 1 : 0));
246 return context_lookup (LOOKUP_ARGS,
255 USHORT inputCount; /* Total number of glyphs in input
256 * glyph sequence--includes the first
258 USHORT lookupCount; /* Number of LookupRecords */
259 USHORT input[]; /* Array of match inputs--start with
261 LookupRecord lookupRecordX[]; /* Array of LookupRecords--in
264 ASSERT_SIZE (Rule, 4);
268 inline bool apply (LOOKUP_ARGS_DEF, ContextLookupContext &context) const {
270 unsigned int num_rules = rule.len;
271 for (unsigned int i = 0; i < num_rules; i++) {
272 if ((this+rule[i]).apply (LOOKUP_ARGS, context))
281 rule; /* Array of Rule tables
282 * ordered by preference */
286 struct ContextFormat1 {
288 friend struct Context;
291 inline bool apply (LOOKUP_ARGS_DEF, apply_lookup_func_t apply_func) const {
293 unsigned int index = (this+coverage) (IN_CURGLYPH ());
294 if (G_LIKELY (index == NOT_COVERED))
297 const RuleSet &rule_set = this+ruleSet[index];
298 struct ContextLookupContext context = {
299 {match_glyph, apply_func},
302 return rule_set.apply (LOOKUP_ARGS, context);
306 USHORT format; /* Format identifier--format = 1 */
308 coverage; /* Offset to Coverage table--from
309 * beginning of table */
310 OffsetArrayOf<RuleSet>
311 ruleSet; /* Array of RuleSet tables
312 * ordered by Coverage Index */
314 ASSERT_SIZE (ContextFormat1, 6);
317 struct ContextFormat2 {
319 friend struct Context;
322 inline bool apply (LOOKUP_ARGS_DEF, apply_lookup_func_t apply_func) const {
324 unsigned int index = (this+coverage) (IN_CURGLYPH ());
325 if (G_LIKELY (index == NOT_COVERED))
328 const ClassDef &class_def = this+classDef;
329 index = class_def (IN_CURGLYPH ());
330 const RuleSet &rule_set = this+ruleSet[index];
331 /* LONGTERMTODO: Old code fetches glyph classes at most once and caches
332 * them across subrule lookups. Not sure it's worth it.
334 struct ContextLookupContext context = {
335 {match_class, apply_func},
338 return rule_set.apply (LOOKUP_ARGS, context);
342 USHORT format; /* Format identifier--format = 2 */
344 coverage; /* Offset to Coverage table--from
345 * beginning of table */
347 classDef; /* Offset to glyph ClassDef table--from
348 * beginning of table */
349 OffsetArrayOf<RuleSet>
350 ruleSet; /* Array of RuleSet tables
351 * ordered by class */
353 ASSERT_SIZE (ContextFormat2, 8);
356 struct ContextFormat3 {
358 friend struct Context;
361 inline bool apply (LOOKUP_ARGS_DEF, apply_lookup_func_t apply_func) const {
363 unsigned int index = (this+coverage[0]) (IN_CURGLYPH ());
364 if (G_LIKELY (index == NOT_COVERED))
367 const LookupRecord *lookupRecord = (const LookupRecord *)
368 ((const char *) coverage +
369 sizeof (coverage[0]) * glyphCount);
370 struct ContextLookupContext context = {
371 {match_coverage, apply_func},
374 return context_lookup (LOOKUP_ARGS,
376 (const USHORT *) (coverage + 1),
383 USHORT format; /* Format identifier--format = 3 */
384 USHORT glyphCount; /* Number of glyphs in the input glyph
386 USHORT lookupCount; /* Number of LookupRecords */
388 coverage[]; /* Array of offsets to Coverage
389 * table in glyph sequence order */
390 LookupRecord lookupRecordX[]; /* Array of LookupRecords--in
393 ASSERT_SIZE (ContextFormat3, 6);
398 bool apply (LOOKUP_ARGS_DEF, apply_lookup_func_t apply_func) const {
400 case 1: return u.format1->apply (LOOKUP_ARGS, apply_func);
401 case 2: return u.format2->apply (LOOKUP_ARGS, apply_func);
402 case 3: return u.format3->apply (LOOKUP_ARGS, apply_func);
403 default:return false;
409 USHORT format; /* Format identifier */
410 ContextFormat1 format1[];
411 ContextFormat2 format2[];
412 ContextFormat3 format3[];
415 ASSERT_SIZE (Context, 2);
418 /* Chaining Contextual lookups */
420 struct ChainContextLookupContext {
425 static inline bool chain_context_lookup (LOOKUP_ARGS_DEF,
426 unsigned int backtrackCount,
427 const USHORT backtrack[],
428 unsigned int inputCount, /* Including the first glyph (not matched) */
429 const USHORT input[], /* Array of input values--start with second glyph */
430 unsigned int lookaheadCount,
431 const USHORT lookahead[],
432 unsigned int lookupCount,
433 const LookupRecord lookupRecord[],
434 ChainContextLookupContext &context)
437 if (HB_UNLIKELY (buffer->out_pos < backtrackCount ||
438 buffer->in_pos + inputCount + lookaheadCount > buffer->in_length ||
439 inputCount + lookaheadCount > context_length))
443 return match_backtrack (LOOKUP_ARGS,
444 backtrackCount, backtrack,
445 context.funcs.match, context.match_data[0]) &&
446 match_input (LOOKUP_ARGS,
448 context.funcs.match, context.match_data[1],
450 match_lookahead (LOOKUP_ARGS,
451 lookaheadCount, lookahead,
452 context.funcs.match, context.match_data[2],
454 (context_length = offset, true) &&
455 apply_lookup (LOOKUP_ARGS,
457 lookupCount, lookupRecord,
458 context.funcs.apply);
463 friend struct ChainRuleSet;
466 inline bool apply (LOOKUP_ARGS_DEF, ChainContextLookupContext &context) const {
467 const HeadlessArrayOf<USHORT> &input = * (const HeadlessArrayOf<USHORT> *)
468 ((const char *) &backtrack + backtrack.get_size ());
469 const ArrayOf<USHORT> &lookahead = * (const ArrayOf<USHORT> *)
470 ((const char *) &input + input.get_size ());
471 const ArrayOf<LookupRecord> &lookup = * (const ArrayOf<LookupRecord> *)
472 ((const char *) &lookahead + lookahead.get_size ());
473 return chain_context_lookup (LOOKUP_ARGS,
489 backtrack; /* Array of backtracking values
490 * (to be matched before the input
492 HeadlessArrayOf<USHORT>
493 inputX; /* Array of input values (start with
496 lookaheadX; /* Array of lookahead values's (to be
497 * matched after the input sequence) */
498 ArrayOf<LookupRecord>
499 lookupX; /* Array of LookupRecords--in
502 ASSERT_SIZE (ChainRule, 8);
504 struct ChainRuleSet {
506 inline bool apply (LOOKUP_ARGS_DEF, ChainContextLookupContext &context) const {
508 unsigned int num_rules = rule.len;
509 for (unsigned int i = 0; i < num_rules; i++) {
510 if ((this+rule[i]).apply (LOOKUP_ARGS, context))
518 OffsetArrayOf<ChainRule>
519 rule; /* Array of ChainRule tables
520 * ordered by preference */
522 ASSERT_SIZE (ChainRuleSet, 2);
524 struct ChainContextFormat1 {
526 friend struct ChainContext;
529 inline bool apply (LOOKUP_ARGS_DEF, apply_lookup_func_t apply_func) const {
531 unsigned int index = (this+coverage) (IN_CURGLYPH ());
532 if (G_LIKELY (index == NOT_COVERED))
535 const ChainRuleSet &rule_set = this+ruleSet[index];
536 struct ChainContextLookupContext context = {
537 {match_glyph, apply_func},
540 return rule_set.apply (LOOKUP_ARGS, context);
543 USHORT format; /* Format identifier--format = 1 */
545 coverage; /* Offset to Coverage table--from
546 * beginning of table */
547 OffsetArrayOf<ChainRuleSet>
548 ruleSet; /* Array of ChainRuleSet tables
549 * ordered by Coverage Index */
551 ASSERT_SIZE (ChainContextFormat1, 6);
553 struct ChainContextFormat2 {
555 friend struct ChainContext;
558 inline bool apply (LOOKUP_ARGS_DEF, apply_lookup_func_t apply_func) const {
560 unsigned int index = (this+coverage) (IN_CURGLYPH ());
561 if (G_LIKELY (index == NOT_COVERED))
564 const ClassDef &backtrack_class_def = this+backtrackClassDef;
565 const ClassDef &input_class_def = this+inputClassDef;
566 const ClassDef &lookahead_class_def = this+lookaheadClassDef;
568 index = input_class_def (IN_CURGLYPH ());
569 const ChainRuleSet &rule_set = this+ruleSet[index];
570 /* LONGTERMTODO: Old code fetches glyph classes at most once and caches
571 * them across subrule lookups. Not sure it's worth it.
573 struct ChainContextLookupContext context = {
574 {match_class, apply_func},
575 {(char *) &backtrack_class_def,
576 (char *) &input_class_def,
577 (char *) &lookahead_class_def}
579 return rule_set.apply (LOOKUP_ARGS, context);
583 USHORT format; /* Format identifier--format = 2 */
585 coverage; /* Offset to Coverage table--from
586 * beginning of table */
588 backtrackClassDef; /* Offset to glyph ClassDef table
589 * containing backtrack sequence
590 * data--from beginning of table */
592 inputClassDef; /* Offset to glyph ClassDef
593 * table containing input sequence
594 * data--from beginning of table */
596 lookaheadClassDef; /* Offset to glyph ClassDef table
597 * containing lookahead sequence
598 * data--from beginning of table */
599 OffsetArrayOf<ChainRuleSet>
600 ruleSet; /* Array of ChainRuleSet tables
601 * ordered by class */
603 ASSERT_SIZE (ChainContextFormat2, 12);
605 struct ChainContextFormat3 {
607 friend struct ChainContext;
611 inline bool apply_coverage (LOOKUP_ARGS_DEF, apply_lookup_func_t apply_func) const {
614 inline bool apply (LOOKUP_ARGS_DEF, apply_lookup_func_t apply_func) const {
616 const OffsetArrayOf<Coverage> &input = * (const OffsetArrayOf<Coverage> *)
617 ((const char *) &backtrack + backtrack.get_size ());
619 unsigned int index = (this+input[0]) (IN_CURGLYPH ());
620 if (G_LIKELY (index == NOT_COVERED))
623 const OffsetArrayOf<Coverage> &lookahead = * (const OffsetArrayOf<Coverage> *)
624 ((const char *) &input + input.get_size ());
625 const ArrayOf<LookupRecord> &lookup = * (const ArrayOf<LookupRecord> *)
626 ((const char *) &lookahead + lookahead.get_size ());
627 struct ChainContextLookupContext context = {
628 {match_coverage, apply_func},
629 {(char *) this, (char *) this, (char *) this}
631 return chain_context_lookup (LOOKUP_ARGS,
633 (USHORT *) backtrack.array,
635 (USHORT *) input.array,
637 (USHORT *) lookahead.array,
645 USHORT format; /* Format identifier--format = 3 */
646 OffsetArrayOf<Coverage>
647 backtrack; /* Array of coverage tables
648 * in backtracking sequence, in glyph
650 OffsetArrayOf<Coverage>
651 inputX ; /* Array of coverage
652 * tables in input sequence, in glyph
654 OffsetArrayOf<Coverage>
655 lookaheadX; /* Array of coverage tables
656 * in lookahead sequence, in glyph
658 ArrayOf<LookupRecord>
659 lookupX; /* Array of LookupRecords--in
662 ASSERT_SIZE (ChainContextFormat3, 10);
664 struct ChainContext {
667 bool apply (LOOKUP_ARGS_DEF, apply_lookup_func_t apply_func) const {
669 case 1: return u.format1->apply (LOOKUP_ARGS, apply_func);
670 case 2: return u.format2->apply (LOOKUP_ARGS, apply_func);
671 case 3: return u.format3->apply (LOOKUP_ARGS, apply_func);
672 default:return false;
678 USHORT format; /* Format identifier */
679 ChainContextFormat1 format1[];
680 ChainContextFormat2 format2[];
681 ChainContextFormat3 format3[];
684 ASSERT_SIZE (ChainContext, 2);
692 static const hb_tag_t GSUBTag = HB_TAG ('G','S','U','B');
693 static const hb_tag_t GPOSTag = HB_TAG ('G','P','O','S');
695 STATIC_DEFINE_GET_FOR_DATA (GSUBGPOS);
696 /* XXX check version here? */
698 DEFINE_TAG_LIST_INTERFACE (Script, script ); /* get_script_count (), get_script (i), get_script_tag (i) */
699 DEFINE_TAG_LIST_INTERFACE (Feature, feature); /* get_feature_count(), get_feature(i), get_feature_tag(i) */
700 DEFINE_LIST_INTERFACE (Lookup, lookup ); /* get_lookup_count (), get_lookup (i) */
702 // LONGTERMTODO bsearch
703 DEFINE_TAG_FIND_INTERFACE (Script, script ); /* find_script_index (), get_script_by_tag (tag) */
704 DEFINE_TAG_FIND_INTERFACE (Feature, feature); /* find_feature_index(), get_feature_by_tag(tag) */
707 Fixed_Version version; /* Version of the GSUB/GPOS table--initially set
710 scriptList; /* ScriptList table */
711 OffsetTo<FeatureList>
712 featureList; /* FeatureList table */
714 lookupList; /* LookupList table */
716 ASSERT_SIZE (GSUBGPOS, 10);
719 #endif /* HB_OT_LAYOUT_GSUBGPOS_PRIVATE_H */