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) 0x110000)
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 GlyphID start; /* First GlyphID in the range */
138 GlyphID end; /* Last GlyphID in the range */
139 USHORT value; /* Value */
141 DEFINE_SIZE_STATIC (6);
143 DEFINE_NULL_DATA (RangeRecord, "\000\001");
146 struct IndexArray : ArrayOf<Index>
148 inline unsigned int get_indexes (unsigned int start_offset,
149 unsigned int *_count /* IN/OUT */,
150 unsigned int *_indexes /* OUT */) const
153 const USHORT *arr = this->sub_array (start_offset, _count);
154 unsigned int count = *_count;
155 for (unsigned int i = 0; i < count; i++)
156 _indexes[i] = arr[i];
170 inline unsigned int get_feature_count (void) const
171 { return featureIndex.len; }
172 inline hb_tag_t get_feature_index (unsigned int i) const
173 { return featureIndex[i]; }
174 inline unsigned int get_feature_indexes (unsigned int start_offset,
175 unsigned int *feature_count /* IN/OUT */,
176 unsigned int *feature_indexes /* OUT */) const
177 { return featureIndex.get_indexes (start_offset, feature_count, feature_indexes); }
179 inline bool has_required_feature (void) const { return reqFeatureIndex != 0xffff; }
180 inline unsigned int get_required_feature_index (void) const
182 if (reqFeatureIndex == 0xffff)
183 return Index::NOT_FOUND_INDEX;
184 return reqFeatureIndex;;
187 inline bool sanitize (hb_sanitize_context_t *c) {
189 return TRACE_RETURN (c->check_struct (this) && featureIndex.sanitize (c));
192 Offset lookupOrder; /* = Null (reserved for an offset to a
193 * reordering table) */
194 USHORT reqFeatureIndex;/* Index of a feature required for this
195 * language system--if no required features
197 IndexArray featureIndex; /* Array of indices into the FeatureList */
199 DEFINE_SIZE_ARRAY (6, featureIndex);
201 DEFINE_NULL_DATA (LangSys, "\0\0\xFF\xFF");
206 inline unsigned int get_lang_sys_count (void) const
207 { return langSys.len; }
208 inline const Tag& get_lang_sys_tag (unsigned int i) const
209 { return langSys.get_tag (i); }
210 inline unsigned int get_lang_sys_tags (unsigned int start_offset,
211 unsigned int *lang_sys_count /* IN/OUT */,
212 hb_tag_t *lang_sys_tags /* OUT */) const
213 { return langSys.get_tags (start_offset, lang_sys_count, lang_sys_tags); }
214 inline const LangSys& get_lang_sys (unsigned int i) const
216 if (i == Index::NOT_FOUND_INDEX) return get_default_lang_sys ();
217 return this+langSys[i].offset;
219 inline bool find_lang_sys_index (hb_tag_t tag, unsigned int *index) const
220 { return langSys.find_index (tag, index); }
222 inline bool has_default_lang_sys (void) const { return defaultLangSys != 0; }
223 inline const LangSys& get_default_lang_sys (void) const { return this+defaultLangSys; }
225 inline bool sanitize (hb_sanitize_context_t *c) {
227 return TRACE_RETURN (defaultLangSys.sanitize (c, this) && langSys.sanitize (c, this));
232 defaultLangSys; /* Offset to DefaultLangSys table--from
233 * beginning of Script table--may be Null */
234 RecordArrayOf<LangSys>
235 langSys; /* Array of LangSysRecords--listed
236 * alphabetically by LangSysTag */
238 DEFINE_SIZE_ARRAY (4, langSys);
241 typedef RecordListOf<Script> ScriptList;
246 inline unsigned int get_lookup_count (void) const
247 { return lookupIndex.len; }
248 inline hb_tag_t get_lookup_index (unsigned int i) const
249 { return lookupIndex[i]; }
250 inline unsigned int get_lookup_indexes (unsigned int start_index,
251 unsigned int *lookup_count /* IN/OUT */,
252 unsigned int *lookup_tags /* OUT */) const
253 { return lookupIndex.get_indexes (start_index, lookup_count, lookup_tags); }
255 inline bool sanitize (hb_sanitize_context_t *c) {
257 return TRACE_RETURN (c->check_struct (this) && lookupIndex.sanitize (c));
260 Offset featureParams; /* Offset to Feature Parameters table (if one
261 * has been defined for the feature), relative
262 * to the beginning of the Feature Table; = Null
264 IndexArray lookupIndex; /* Array of LookupList indices */
266 DEFINE_SIZE_ARRAY (4, lookupIndex);
269 typedef RecordListOf<Feature> FeatureList;
272 struct LookupFlag : USHORT
275 RightToLeft = 0x0001u,
276 IgnoreBaseGlyphs = 0x0002u,
277 IgnoreLigatures = 0x0004u,
278 IgnoreMarks = 0x0008u,
279 IgnoreFlags = 0x000Eu,
280 UseMarkFilteringSet = 0x0010u,
282 MarkAttachmentType = 0xFF00u
285 DEFINE_SIZE_STATIC (2);
290 inline unsigned int get_subtable_count (void) const { return subTable.len; }
292 inline unsigned int get_type (void) const { return lookupType; }
294 /* lookup_props is a 32-bit integer where the lower 16-bit is LookupFlag and
295 * higher 16-bit is mark-filtering-set if the lookup uses one.
296 * Not to be confused with glyph_props which is very similar. */
297 inline uint32_t get_props (void) const
299 unsigned int flag = lookupFlag;
300 if (unlikely (flag & LookupFlag::UseMarkFilteringSet))
302 const USHORT &markFilteringSet = StructAfter<USHORT> (subTable);
303 flag += (markFilteringSet << 16);
308 inline bool sanitize (hb_sanitize_context_t *c) {
310 /* Real sanitize of the subtables is done by GSUB/GPOS/... */
311 if (!(c->check_struct (this) && subTable.sanitize (c))) return TRACE_RETURN (false);
312 if (unlikely (lookupFlag & LookupFlag::UseMarkFilteringSet))
314 USHORT &markFilteringSet = StructAfter<USHORT> (subTable);
315 if (!markFilteringSet.sanitize (c)) return TRACE_RETURN (false);
317 return TRACE_RETURN (true);
320 USHORT lookupType; /* Different enumerations for GSUB and GPOS */
321 USHORT lookupFlag; /* Lookup qualifiers */
323 subTable; /* Array of SubTables */
324 USHORT markFilteringSetX[VAR]; /* Index (base 0) into GDEF mark glyph sets
325 * structure. This field is only present if bit
326 * UseMarkFilteringSet of lookup flags is set. */
328 DEFINE_SIZE_ARRAY2 (6, subTable, markFilteringSetX);
331 typedef OffsetListOf<Lookup> LookupList;
338 struct CoverageFormat1
340 friend struct Coverage;
343 inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
345 int i = glyphArray.search (glyph_id);
351 inline bool sanitize (hb_sanitize_context_t *c) {
353 return TRACE_RETURN (glyphArray.sanitize (c));
356 inline bool intersects_coverage (const hb_set_t *glyphs, unsigned int index) const {
357 return glyphs->has (glyphArray[index]);
361 inline void init (const struct CoverageFormat1 &c_) { c = &c_; i = 0; };
362 inline bool more (void) { return i < c->glyphArray.len; }
363 inline void next (void) { i++; }
364 inline uint16_t get_glyph (void) { return c->glyphArray[i]; }
365 inline uint16_t get_coverage (void) { return i; }
368 const struct CoverageFormat1 *c;
373 USHORT coverageFormat; /* Format identifier--format = 1 */
374 SortedArrayOf<GlyphID>
375 glyphArray; /* Array of GlyphIDs--in numerical order */
377 DEFINE_SIZE_ARRAY (4, glyphArray);
380 struct CoverageFormat2
382 friend struct Coverage;
385 inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
387 int i = rangeRecord.search (glyph_id);
389 const RangeRecord &range = rangeRecord[i];
390 return (unsigned int) range.value + (glyph_id - range.start);
395 inline bool sanitize (hb_sanitize_context_t *c) {
397 return TRACE_RETURN (rangeRecord.sanitize (c));
400 inline bool intersects_coverage (const hb_set_t *glyphs, unsigned int index) const {
402 unsigned int count = rangeRecord.len;
403 for (i = 0; i < count; i++) {
404 const RangeRecord &range = rangeRecord[i];
405 if (range.value <= index &&
406 index < (unsigned int) range.value + (range.end - range.start) &&
407 range.intersects (glyphs))
409 else if (index < range.value)
416 inline void init (const CoverageFormat2 &c_) {
420 j = c->rangeRecord.len ? c_.rangeRecord[0].start : 0;
422 inline bool more (void) { return i < c->rangeRecord.len; }
423 inline void next (void) {
425 if (j == c->rangeRecord[i].end) {
428 j = c->rangeRecord[i].start;
433 inline uint16_t get_glyph (void) { return j; }
434 inline uint16_t get_coverage (void) { return coverage; }
437 const struct CoverageFormat2 *c;
438 unsigned int i, j, coverage;
442 USHORT coverageFormat; /* Format identifier--format = 2 */
443 SortedArrayOf<RangeRecord>
444 rangeRecord; /* Array of glyph ranges--ordered by
445 * Start GlyphID. rangeCount entries
448 DEFINE_SIZE_ARRAY (4, rangeRecord);
453 inline unsigned int operator () (hb_codepoint_t glyph_id) const { return get_coverage (glyph_id); }
455 inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
458 case 1: return u.format1.get_coverage(glyph_id);
459 case 2: return u.format2.get_coverage(glyph_id);
460 default:return NOT_COVERED;
464 inline bool sanitize (hb_sanitize_context_t *c) {
466 if (!u.format.sanitize (c)) return TRACE_RETURN (false);
468 case 1: return TRACE_RETURN (u.format1.sanitize (c));
469 case 2: return TRACE_RETURN (u.format2.sanitize (c));
470 default:return TRACE_RETURN (true);
474 inline bool intersects (const hb_set_t *glyphs) const {
475 /* TODO speed this up */
477 for (iter.init (*this); iter.more (); iter.next ()) {
478 if (glyphs->has (iter.get_glyph ()))
484 inline bool intersects_coverage (const hb_set_t *glyphs, unsigned int index) const {
486 case 1: return u.format1.intersects_coverage (glyphs, index);
487 case 2: return u.format2.intersects_coverage (glyphs, index);
488 default:return false;
493 Iter (void) : format (0) {};
494 inline void init (const Coverage &c_) {
495 format = c_.u.format;
497 case 1: return u.format1.init (c_.u.format1);
498 case 2: return u.format2.init (c_.u.format2);
502 inline bool more (void) {
504 case 1: return u.format1.more ();
505 case 2: return u.format2.more ();
509 inline void next (void) {
511 case 1: u.format1.next (); break;
512 case 2: u.format2.next (); break;
516 inline uint16_t get_glyph (void) {
518 case 1: return u.format1.get_glyph ();
519 case 2: return u.format2.get_glyph ();
523 inline uint16_t get_coverage (void) {
525 case 1: return u.format1.get_coverage ();
526 case 2: return u.format2.get_coverage ();
534 CoverageFormat1::Iter format1;
535 CoverageFormat2::Iter format2;
541 USHORT format; /* Format identifier */
542 CoverageFormat1 format1;
543 CoverageFormat2 format2;
546 DEFINE_SIZE_UNION (2, format);
551 * Class Definition Table
554 struct ClassDefFormat1
556 friend struct ClassDef;
559 inline unsigned int get_class (hb_codepoint_t glyph_id) const
561 if ((unsigned int) (glyph_id - startGlyph) < classValue.len)
562 return classValue[glyph_id - startGlyph];
566 inline bool sanitize (hb_sanitize_context_t *c) {
568 return TRACE_RETURN (c->check_struct (this) && classValue.sanitize (c));
571 inline bool intersects_class (const hb_set_t *glyphs, unsigned int klass) const {
572 unsigned int count = classValue.len;
573 for (unsigned int i = 0; i < count; i++)
574 if (classValue[i] == klass && glyphs->has (startGlyph + i))
579 USHORT classFormat; /* Format identifier--format = 1 */
580 GlyphID startGlyph; /* First GlyphID of the classValueArray */
582 classValue; /* Array of Class Values--one per GlyphID */
584 DEFINE_SIZE_ARRAY (6, classValue);
587 struct ClassDefFormat2
589 friend struct ClassDef;
592 inline unsigned int get_class (hb_codepoint_t glyph_id) const
594 int i = rangeRecord.search (glyph_id);
596 return rangeRecord[i].value;
600 inline bool sanitize (hb_sanitize_context_t *c) {
602 return TRACE_RETURN (rangeRecord.sanitize (c));
605 inline bool intersects_class (const hb_set_t *glyphs, unsigned int klass) const {
606 unsigned int count = rangeRecord.len;
607 for (unsigned int i = 0; i < count; i++)
608 if (rangeRecord[i].value == klass && rangeRecord[i].intersects (glyphs))
613 USHORT classFormat; /* Format identifier--format = 2 */
614 SortedArrayOf<RangeRecord>
615 rangeRecord; /* Array of glyph ranges--ordered by
618 DEFINE_SIZE_ARRAY (4, rangeRecord);
623 inline unsigned int operator () (hb_codepoint_t glyph_id) const { return get_class (glyph_id); }
625 inline unsigned int get_class (hb_codepoint_t glyph_id) const
628 case 1: return u.format1.get_class(glyph_id);
629 case 2: return u.format2.get_class(glyph_id);
634 inline bool sanitize (hb_sanitize_context_t *c) {
636 if (!u.format.sanitize (c)) return TRACE_RETURN (false);
638 case 1: return TRACE_RETURN (u.format1.sanitize (c));
639 case 2: return TRACE_RETURN (u.format2.sanitize (c));
640 default:return TRACE_RETURN (true);
644 inline bool intersects_class (const hb_set_t *glyphs, unsigned int klass) const {
646 case 1: return u.format1.intersects_class (glyphs, klass);
647 case 2: return u.format2.intersects_class (glyphs, klass);
648 default:return false;
654 USHORT format; /* Format identifier */
655 ClassDefFormat1 format1;
656 ClassDefFormat2 format2;
659 DEFINE_SIZE_UNION (2, format);
670 inline hb_position_t get_x_delta (hb_font_t *font) const
671 { return get_delta (font->x_ppem, font->x_scale); }
673 inline hb_position_t get_y_delta (hb_font_t *font) const
674 { return get_delta (font->y_ppem, font->y_scale); }
676 inline int get_delta (unsigned int ppem, int scale) const
680 int pixels = get_delta_pixels (ppem);
682 if (!pixels) return 0;
684 return pixels * (int64_t) scale / ppem;
688 inline int get_delta_pixels (unsigned int ppem_size) const
690 unsigned int f = deltaFormat;
691 if (unlikely (f < 1 || f > 3))
694 if (ppem_size < startSize || ppem_size > endSize)
697 unsigned int s = ppem_size - startSize;
699 unsigned int byte = deltaValue[s >> (4 - f)];
700 unsigned int bits = (byte >> (16 - (((s & ((1 << (4 - f)) - 1)) + 1) << f)));
701 unsigned int mask = (0xFFFF >> (16 - (1 << f)));
703 int delta = bits & mask;
705 if ((unsigned int) delta >= ((mask + 1) >> 1))
711 inline unsigned int get_size (void) const
713 unsigned int f = deltaFormat;
714 if (unlikely (f < 1 || f > 3 || startSize > endSize)) return 3 * USHORT::static_size;
715 return USHORT::static_size * (4 + ((endSize - startSize) >> (4 - f)));
718 inline bool sanitize (hb_sanitize_context_t *c) {
720 return TRACE_RETURN (c->check_struct (this) && c->check_range (this, this->get_size ()));
724 USHORT startSize; /* Smallest size to correct--in ppem */
725 USHORT endSize; /* Largest size to correct--in ppem */
726 USHORT deltaFormat; /* Format of DeltaValue array data: 1, 2, or 3
727 * 1 Signed 2-bit value, 8 values per uint16
728 * 2 Signed 4-bit value, 4 values per uint16
729 * 3 Signed 8-bit value, 2 values per uint16
731 USHORT deltaValue[VAR]; /* Array of compressed data */
733 DEFINE_SIZE_ARRAY (6, deltaValue);
738 #endif /* HB_OT_LAYOUT_COMMON_PRIVATE_HH */