2 * Copyright © 2007,2008,2009 Red Hat, Inc.
3 * Copyright © 2010,2012 Google, Inc.
5 * This is part of HarfBuzz, a text shaping library.
7 * Permission is hereby granted, without written agreement and without
8 * license or royalty fees, to use, copy, modify, and distribute this
9 * software and its documentation for any purpose, provided that the
10 * above copyright notice and the following two paragraphs appear in
11 * all copies of this software.
13 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
14 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
15 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
16 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
19 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
20 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
22 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
23 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
25 * Red Hat Author(s): Behdad Esfahbod
26 * Google Author(s): Behdad Esfahbod
29 #ifndef HB_OT_LAYOUT_COMMON_PRIVATE_HH
30 #define HB_OT_LAYOUT_COMMON_PRIVATE_HH
32 #include "hb-ot-layout-private.hh"
33 #include "hb-open-type-private.hh"
34 #include "hb-set-private.hh"
37 #define NOT_COVERED ((unsigned int) -1)
38 #define MAX_NESTING_LEVEL 8
44 * OpenType Layout Common Table Formats
50 * Script, ScriptList, LangSys, Feature, FeatureList, Lookup, LookupList
53 template <typename Type>
56 inline int cmp (hb_tag_t a) const {
60 inline bool sanitize (hb_sanitize_context_t *c, void *base) {
62 return TRACE_RETURN (c->check_struct (this) && offset.sanitize (c, base));
65 Tag tag; /* 4-byte Tag identifier */
67 offset; /* Offset from beginning of object holding
70 DEFINE_SIZE_STATIC (6);
73 template <typename Type>
74 struct RecordArrayOf : SortedArrayOf<Record<Type> > {
75 inline const Tag& get_tag (unsigned int i) const
77 /* We cheat slightly and don't define separate Null objects
78 * for Record types. Instead, we return the correct Null(Tag)
80 if (unlikely (i >= this->len)) return Null(Tag);
81 return (*this)[i].tag;
83 inline unsigned int get_tags (unsigned int start_offset,
84 unsigned int *record_count /* IN/OUT */,
85 hb_tag_t *record_tags /* OUT */) const
88 const Record<Type> *arr = this->sub_array (start_offset, record_count);
89 unsigned int count = *record_count;
90 for (unsigned int i = 0; i < count; i++)
91 record_tags[i] = arr[i].tag;
95 inline bool find_index (hb_tag_t tag, unsigned int *index) const
97 int i = this->search (tag);
99 if (index) *index = i;
102 if (index) *index = Index::NOT_FOUND_INDEX;
108 template <typename Type>
109 struct RecordListOf : RecordArrayOf<Type>
111 inline const Type& operator [] (unsigned int i) const
112 { return this+RecordArrayOf<Type>::operator [](i).offset; }
114 inline bool sanitize (hb_sanitize_context_t *c) {
116 return TRACE_RETURN (RecordArrayOf<Type>::sanitize (c, this));
123 inline int cmp (hb_codepoint_t g) const {
124 hb_codepoint_t a = start, b = end;
125 return g < a ? -1 : g <= b ? 0 : +1 ;
128 inline bool sanitize (hb_sanitize_context_t *c) {
130 return TRACE_RETURN (c->check_struct (this));
133 inline bool intersects (const hb_set_t *glyphs) const {
134 return glyphs->intersects (start, end);
137 template <typename set_t>
138 inline void add_coverage (set_t *glyphs) const {
139 glyphs->add_range (start, end);
142 GlyphID start; /* First GlyphID in the range */
143 GlyphID end; /* Last GlyphID in the range */
144 USHORT value; /* Value */
146 DEFINE_SIZE_STATIC (6);
148 DEFINE_NULL_DATA (RangeRecord, "\000\001");
151 struct IndexArray : ArrayOf<Index>
153 inline unsigned int get_indexes (unsigned int start_offset,
154 unsigned int *_count /* IN/OUT */,
155 unsigned int *_indexes /* OUT */) const
158 const USHORT *arr = this->sub_array (start_offset, _count);
159 unsigned int count = *_count;
160 for (unsigned int i = 0; i < count; i++)
161 _indexes[i] = arr[i];
175 inline unsigned int get_feature_count (void) const
176 { return featureIndex.len; }
177 inline hb_tag_t get_feature_index (unsigned int i) const
178 { return featureIndex[i]; }
179 inline unsigned int get_feature_indexes (unsigned int start_offset,
180 unsigned int *feature_count /* IN/OUT */,
181 unsigned int *feature_indexes /* OUT */) const
182 { return featureIndex.get_indexes (start_offset, feature_count, feature_indexes); }
184 inline bool has_required_feature (void) const { return reqFeatureIndex != 0xffff; }
185 inline unsigned int get_required_feature_index (void) const
187 if (reqFeatureIndex == 0xffff)
188 return Index::NOT_FOUND_INDEX;
189 return reqFeatureIndex;;
192 inline bool sanitize (hb_sanitize_context_t *c) {
194 return TRACE_RETURN (c->check_struct (this) && featureIndex.sanitize (c));
197 Offset lookupOrder; /* = Null (reserved for an offset to a
198 * reordering table) */
199 USHORT reqFeatureIndex;/* Index of a feature required for this
200 * language system--if no required features
202 IndexArray featureIndex; /* Array of indices into the FeatureList */
204 DEFINE_SIZE_ARRAY (6, featureIndex);
206 DEFINE_NULL_DATA (LangSys, "\0\0\xFF\xFF");
211 inline unsigned int get_lang_sys_count (void) const
212 { return langSys.len; }
213 inline const Tag& get_lang_sys_tag (unsigned int i) const
214 { return langSys.get_tag (i); }
215 inline unsigned int get_lang_sys_tags (unsigned int start_offset,
216 unsigned int *lang_sys_count /* IN/OUT */,
217 hb_tag_t *lang_sys_tags /* OUT */) const
218 { return langSys.get_tags (start_offset, lang_sys_count, lang_sys_tags); }
219 inline const LangSys& get_lang_sys (unsigned int i) const
221 if (i == Index::NOT_FOUND_INDEX) return get_default_lang_sys ();
222 return this+langSys[i].offset;
224 inline bool find_lang_sys_index (hb_tag_t tag, unsigned int *index) const
225 { return langSys.find_index (tag, index); }
227 inline bool has_default_lang_sys (void) const { return defaultLangSys != 0; }
228 inline const LangSys& get_default_lang_sys (void) const { return this+defaultLangSys; }
230 inline bool sanitize (hb_sanitize_context_t *c) {
232 return TRACE_RETURN (defaultLangSys.sanitize (c, this) && langSys.sanitize (c, this));
237 defaultLangSys; /* Offset to DefaultLangSys table--from
238 * beginning of Script table--may be Null */
239 RecordArrayOf<LangSys>
240 langSys; /* Array of LangSysRecords--listed
241 * alphabetically by LangSysTag */
243 DEFINE_SIZE_ARRAY (4, langSys);
246 typedef RecordListOf<Script> ScriptList;
251 inline unsigned int get_lookup_count (void) const
252 { return lookupIndex.len; }
253 inline hb_tag_t get_lookup_index (unsigned int i) const
254 { return lookupIndex[i]; }
255 inline unsigned int get_lookup_indexes (unsigned int start_index,
256 unsigned int *lookup_count /* IN/OUT */,
257 unsigned int *lookup_tags /* OUT */) const
258 { return lookupIndex.get_indexes (start_index, lookup_count, lookup_tags); }
260 inline bool sanitize (hb_sanitize_context_t *c) {
262 return TRACE_RETURN (c->check_struct (this) && lookupIndex.sanitize (c));
265 Offset featureParams; /* Offset to Feature Parameters table (if one
266 * has been defined for the feature), relative
267 * to the beginning of the Feature Table; = Null
269 IndexArray lookupIndex; /* Array of LookupList indices */
271 DEFINE_SIZE_ARRAY (4, lookupIndex);
274 typedef RecordListOf<Feature> FeatureList;
277 struct LookupFlag : USHORT
280 RightToLeft = 0x0001u,
281 IgnoreBaseGlyphs = 0x0002u,
282 IgnoreLigatures = 0x0004u,
283 IgnoreMarks = 0x0008u,
284 IgnoreFlags = 0x000Eu,
285 UseMarkFilteringSet = 0x0010u,
287 MarkAttachmentType = 0xFF00u
290 DEFINE_SIZE_STATIC (2);
295 inline unsigned int get_subtable_count (void) const { return subTable.len; }
297 inline unsigned int get_type (void) const { return lookupType; }
299 /* lookup_props is a 32-bit integer where the lower 16-bit is LookupFlag and
300 * higher 16-bit is mark-filtering-set if the lookup uses one.
301 * Not to be confused with glyph_props which is very similar. */
302 inline uint32_t get_props (void) const
304 unsigned int flag = lookupFlag;
305 if (unlikely (flag & LookupFlag::UseMarkFilteringSet))
307 const USHORT &markFilteringSet = StructAfter<USHORT> (subTable);
308 flag += (markFilteringSet << 16);
313 inline bool sanitize (hb_sanitize_context_t *c) {
315 /* Real sanitize of the subtables is done by GSUB/GPOS/... */
316 if (!(c->check_struct (this) && subTable.sanitize (c))) return TRACE_RETURN (false);
317 if (unlikely (lookupFlag & LookupFlag::UseMarkFilteringSet))
319 USHORT &markFilteringSet = StructAfter<USHORT> (subTable);
320 if (!markFilteringSet.sanitize (c)) return TRACE_RETURN (false);
322 return TRACE_RETURN (true);
325 USHORT lookupType; /* Different enumerations for GSUB and GPOS */
326 USHORT lookupFlag; /* Lookup qualifiers */
328 subTable; /* Array of SubTables */
329 USHORT markFilteringSetX[VAR]; /* Index (base 0) into GDEF mark glyph sets
330 * structure. This field is only present if bit
331 * UseMarkFilteringSet of lookup flags is set. */
333 DEFINE_SIZE_ARRAY2 (6, subTable, markFilteringSetX);
336 typedef OffsetListOf<Lookup> LookupList;
343 struct CoverageFormat1
345 friend struct Coverage;
348 inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
350 int i = glyphArray.search (glyph_id);
351 ASSERT_STATIC (((unsigned int) -1) == NOT_COVERED);
355 inline bool sanitize (hb_sanitize_context_t *c) {
357 return TRACE_RETURN (glyphArray.sanitize (c));
360 inline bool intersects_coverage (const hb_set_t *glyphs, unsigned int index) const {
361 return glyphs->has (glyphArray[index]);
364 template <typename set_t>
365 inline void add_coverage (set_t *glyphs) const {
366 unsigned int count = glyphArray.len;
367 for (unsigned int i = 0; i < count; i++)
368 glyphs->add (glyphArray[i]);
372 inline void init (const struct CoverageFormat1 &c_) { c = &c_; i = 0; };
373 inline bool more (void) { return i < c->glyphArray.len; }
374 inline void next (void) { i++; }
375 inline uint16_t get_glyph (void) { return c->glyphArray[i]; }
376 inline uint16_t get_coverage (void) { return i; }
379 const struct CoverageFormat1 *c;
384 USHORT coverageFormat; /* Format identifier--format = 1 */
385 SortedArrayOf<GlyphID>
386 glyphArray; /* Array of GlyphIDs--in numerical order */
388 DEFINE_SIZE_ARRAY (4, glyphArray);
391 struct CoverageFormat2
393 friend struct Coverage;
396 inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
398 int i = rangeRecord.search (glyph_id);
400 const RangeRecord &range = rangeRecord[i];
401 return (unsigned int) range.value + (glyph_id - range.start);
406 inline bool sanitize (hb_sanitize_context_t *c) {
408 return TRACE_RETURN (rangeRecord.sanitize (c));
411 inline bool intersects_coverage (const hb_set_t *glyphs, unsigned int index) const {
413 unsigned int count = rangeRecord.len;
414 for (i = 0; i < count; i++) {
415 const RangeRecord &range = rangeRecord[i];
416 if (range.value <= index &&
417 index < (unsigned int) range.value + (range.end - range.start) &&
418 range.intersects (glyphs))
420 else if (index < range.value)
426 template <typename set_t>
427 inline void add_coverage (set_t *glyphs) const {
428 unsigned int count = rangeRecord.len;
429 for (unsigned int i = 0; i < count; i++)
430 rangeRecord[i].add_coverage (glyphs);
434 inline void init (const CoverageFormat2 &c_) {
438 j = c->rangeRecord.len ? c_.rangeRecord[0].start : 0;
440 inline bool more (void) { return i < c->rangeRecord.len; }
441 inline void next (void) {
443 if (j == c->rangeRecord[i].end) {
446 j = c->rangeRecord[i].start;
451 inline uint16_t get_glyph (void) { return j; }
452 inline uint16_t get_coverage (void) { return coverage; }
455 const struct CoverageFormat2 *c;
456 unsigned int i, j, coverage;
460 USHORT coverageFormat; /* Format identifier--format = 2 */
461 SortedArrayOf<RangeRecord>
462 rangeRecord; /* Array of glyph ranges--ordered by
463 * Start GlyphID. rangeCount entries
466 DEFINE_SIZE_ARRAY (4, rangeRecord);
471 inline unsigned int operator () (hb_codepoint_t glyph_id) const { return get_coverage (glyph_id); }
473 inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
476 case 1: return u.format1.get_coverage(glyph_id);
477 case 2: return u.format2.get_coverage(glyph_id);
478 default:return NOT_COVERED;
482 inline bool sanitize (hb_sanitize_context_t *c) {
484 if (!u.format.sanitize (c)) return TRACE_RETURN (false);
486 case 1: return TRACE_RETURN (u.format1.sanitize (c));
487 case 2: return TRACE_RETURN (u.format2.sanitize (c));
488 default:return TRACE_RETURN (true);
492 inline bool intersects (const hb_set_t *glyphs) const {
493 /* TODO speed this up */
495 for (iter.init (*this); iter.more (); iter.next ()) {
496 if (glyphs->has (iter.get_glyph ()))
502 inline bool intersects_coverage (const hb_set_t *glyphs, unsigned int index) const {
504 case 1: return u.format1.intersects_coverage (glyphs, index);
505 case 2: return u.format2.intersects_coverage (glyphs, index);
506 default:return false;
510 template <typename set_t>
511 inline void add_coverage (set_t *glyphs) const {
513 case 1: u.format1.add_coverage (glyphs); break;
514 case 2: u.format2.add_coverage (glyphs); break;
520 Iter (void) : format (0) {};
521 inline void init (const Coverage &c_) {
522 format = c_.u.format;
524 case 1: return u.format1.init (c_.u.format1);
525 case 2: return u.format2.init (c_.u.format2);
529 inline bool more (void) {
531 case 1: return u.format1.more ();
532 case 2: return u.format2.more ();
536 inline void next (void) {
538 case 1: u.format1.next (); break;
539 case 2: u.format2.next (); break;
543 inline uint16_t get_glyph (void) {
545 case 1: return u.format1.get_glyph ();
546 case 2: return u.format2.get_glyph ();
550 inline uint16_t get_coverage (void) {
552 case 1: return u.format1.get_coverage ();
553 case 2: return u.format2.get_coverage ();
561 CoverageFormat1::Iter format1;
562 CoverageFormat2::Iter format2;
568 USHORT format; /* Format identifier */
569 CoverageFormat1 format1;
570 CoverageFormat2 format2;
573 DEFINE_SIZE_UNION (2, format);
578 * Class Definition Table
581 struct ClassDefFormat1
583 friend struct ClassDef;
586 inline unsigned int get_class (hb_codepoint_t glyph_id) const
588 if (unlikely ((unsigned int) (glyph_id - startGlyph) < classValue.len))
589 return classValue[glyph_id - startGlyph];
593 inline bool sanitize (hb_sanitize_context_t *c) {
595 return TRACE_RETURN (c->check_struct (this) && classValue.sanitize (c));
598 inline bool intersects_class (const hb_set_t *glyphs, unsigned int klass) const {
599 unsigned int count = classValue.len;
600 for (unsigned int i = 0; i < count; i++)
601 if (classValue[i] == klass && glyphs->has (startGlyph + i))
607 USHORT classFormat; /* Format identifier--format = 1 */
608 GlyphID startGlyph; /* First GlyphID of the classValueArray */
610 classValue; /* Array of Class Values--one per GlyphID */
612 DEFINE_SIZE_ARRAY (6, classValue);
615 struct ClassDefFormat2
617 friend struct ClassDef;
620 inline unsigned int get_class (hb_codepoint_t glyph_id) const
622 int i = rangeRecord.search (glyph_id);
624 return rangeRecord[i].value;
628 inline bool sanitize (hb_sanitize_context_t *c) {
630 return TRACE_RETURN (rangeRecord.sanitize (c));
633 inline bool intersects_class (const hb_set_t *glyphs, unsigned int klass) const {
634 unsigned int count = rangeRecord.len;
635 for (unsigned int i = 0; i < count; i++)
636 if (rangeRecord[i].value == klass && rangeRecord[i].intersects (glyphs))
642 USHORT classFormat; /* Format identifier--format = 2 */
643 SortedArrayOf<RangeRecord>
644 rangeRecord; /* Array of glyph ranges--ordered by
647 DEFINE_SIZE_ARRAY (4, rangeRecord);
652 inline unsigned int operator () (hb_codepoint_t glyph_id) const { return get_class (glyph_id); }
654 inline unsigned int get_class (hb_codepoint_t glyph_id) const
657 case 1: return u.format1.get_class(glyph_id);
658 case 2: return u.format2.get_class(glyph_id);
663 inline bool sanitize (hb_sanitize_context_t *c) {
665 if (!u.format.sanitize (c)) return TRACE_RETURN (false);
667 case 1: return TRACE_RETURN (u.format1.sanitize (c));
668 case 2: return TRACE_RETURN (u.format2.sanitize (c));
669 default:return TRACE_RETURN (true);
673 inline bool intersects_class (const hb_set_t *glyphs, unsigned int klass) const {
675 case 1: return u.format1.intersects_class (glyphs, klass);
676 case 2: return u.format2.intersects_class (glyphs, klass);
677 default:return false;
683 USHORT format; /* Format identifier */
684 ClassDefFormat1 format1;
685 ClassDefFormat2 format2;
688 DEFINE_SIZE_UNION (2, format);
699 inline hb_position_t get_x_delta (hb_font_t *font) const
700 { return get_delta (font->x_ppem, font->x_scale); }
702 inline hb_position_t get_y_delta (hb_font_t *font) const
703 { return get_delta (font->y_ppem, font->y_scale); }
705 inline int get_delta (unsigned int ppem, int scale) const
709 int pixels = get_delta_pixels (ppem);
711 if (!pixels) return 0;
713 return pixels * (int64_t) scale / ppem;
717 inline int get_delta_pixels (unsigned int ppem_size) const
719 unsigned int f = deltaFormat;
720 if (unlikely (f < 1 || f > 3))
723 if (ppem_size < startSize || ppem_size > endSize)
726 unsigned int s = ppem_size - startSize;
728 unsigned int byte = deltaValue[s >> (4 - f)];
729 unsigned int bits = (byte >> (16 - (((s & ((1 << (4 - f)) - 1)) + 1) << f)));
730 unsigned int mask = (0xFFFF >> (16 - (1 << f)));
732 int delta = bits & mask;
734 if ((unsigned int) delta >= ((mask + 1) >> 1))
740 inline unsigned int get_size (void) const
742 unsigned int f = deltaFormat;
743 if (unlikely (f < 1 || f > 3 || startSize > endSize)) return 3 * USHORT::static_size;
744 return USHORT::static_size * (4 + ((endSize - startSize) >> (4 - f)));
747 inline bool sanitize (hb_sanitize_context_t *c) {
749 return TRACE_RETURN (c->check_struct (this) && c->check_range (this, this->get_size ()));
753 USHORT startSize; /* Smallest size to correct--in ppem */
754 USHORT endSize; /* Largest size to correct--in ppem */
755 USHORT deltaFormat; /* Format of DeltaValue array data: 1, 2, or 3
756 * 1 Signed 2-bit value, 8 values per uint16
757 * 2 Signed 4-bit value, 4 values per uint16
758 * 3 Signed 8-bit value, 2 values per uint16
760 USHORT deltaValue[VAR]; /* Array of compressed data */
762 DEFINE_SIZE_ARRAY (6, deltaValue);
767 #endif /* HB_OT_LAYOUT_COMMON_PRIVATE_HH */