2 * Copyright (C) 2007,2008,2009 Red Hat, Inc.
4 * This is part of HarfBuzz, a text shaping 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_COMMON_PRIVATE_HH
28 #define HB_OT_LAYOUT_COMMON_PRIVATE_HH
30 #include "hb-ot-layout-private.hh"
32 #include "hb-open-type-private.hh"
35 #define NO_CONTEXT ((unsigned int) 0x110000)
36 #define NOT_COVERED ((unsigned int) 0x110000)
37 #define MAX_NESTING_LEVEL 8
45 * OpenType Layout Common Table Formats
51 * Script, ScriptList, LangSys, Feature, FeatureList, Lookup, LookupList
54 template <typename Type>
57 inline int cmp (hb_tag_t b) const {
59 return b < a ? -1 : b == a ? 0 : -1;
62 inline bool sanitize (hb_sanitize_context_t *c, void *base) {
64 return c->check_struct (this)
65 && offset.sanitize (c, base);
68 Tag tag; /* 4-byte Tag identifier */
70 offset; /* Offset from beginning of object holding
73 DEFINE_SIZE_STATIC (6);
76 template <typename Type>
77 struct RecordArrayOf : SortedArrayOf<Record<Type> > {
78 inline const Tag& get_tag (unsigned int i) const
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> *array = 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] = array[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 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 c->check_struct (this);
133 GlyphID start; /* First GlyphID in the range */
134 GlyphID end; /* Last GlyphID in the range */
135 USHORT value; /* Value */
137 DEFINE_SIZE_STATIC (6);
139 DEFINE_NULL_DATA (RangeRecord, "\000\001");
142 struct IndexArray : ArrayOf<Index>
144 inline unsigned int get_indexes (unsigned int start_offset,
145 unsigned int *_count /* IN/OUT */,
146 unsigned int *_indexes /* OUT */) const
149 const USHORT *array = this->sub_array (start_offset, _count);
150 unsigned int count = *_count;
151 for (unsigned int i = 0; i < count; i++)
152 _indexes[i] = array[i];
166 inline unsigned int get_feature_count (void) const
167 { return featureIndex.len; }
168 inline hb_tag_t get_feature_index (unsigned int i) const
169 { return featureIndex[i]; }
170 inline unsigned int get_feature_indexes (unsigned int start_offset,
171 unsigned int *feature_count /* IN/OUT */,
172 unsigned int *feature_indexes /* OUT */) const
173 { return featureIndex.get_indexes (start_offset, feature_count, feature_indexes); }
175 inline bool has_required_feature (void) const { return reqFeatureIndex != 0xffff; }
176 inline unsigned int get_required_feature_index (void) const
178 if (reqFeatureIndex == 0xffff)
179 return Index::NOT_FOUND_INDEX;
180 return reqFeatureIndex;;
183 inline bool sanitize (hb_sanitize_context_t *c) {
185 return c->check_struct (this)
186 && featureIndex.sanitize (c);
189 Offset lookupOrder; /* = Null (reserved for an offset to a
190 * reordering table) */
191 USHORT reqFeatureIndex;/* Index of a feature required for this
192 * language system--if no required features
194 IndexArray featureIndex; /* Array of indices into the FeatureList */
196 DEFINE_SIZE_ARRAY (6, featureIndex);
198 DEFINE_NULL_DATA (LangSys, "\0\0\xFF\xFF");
203 inline unsigned int get_lang_sys_count (void) const
204 { return langSys.len; }
205 inline const Tag& get_lang_sys_tag (unsigned int i) const
206 { return langSys.get_tag (i); }
207 inline unsigned int get_lang_sys_tags (unsigned int start_offset,
208 unsigned int *lang_sys_count /* IN/OUT */,
209 hb_tag_t *lang_sys_tags /* OUT */) const
210 { return langSys.get_tags (start_offset, lang_sys_count, lang_sys_tags); }
211 inline const LangSys& get_lang_sys (unsigned int i) const
213 if (i == Index::NOT_FOUND_INDEX) return get_default_lang_sys ();
214 return this+langSys[i].offset;
216 inline bool find_lang_sys_index (hb_tag_t tag, unsigned int *index) const
217 { return langSys.find_index (tag, index); }
219 inline bool has_default_lang_sys (void) const { return defaultLangSys != 0; }
220 inline const LangSys& get_default_lang_sys (void) const { return this+defaultLangSys; }
222 inline bool sanitize (hb_sanitize_context_t *c) {
224 return defaultLangSys.sanitize (c, this)
225 && langSys.sanitize (c, this);
230 defaultLangSys; /* Offset to DefaultLangSys table--from
231 * beginning of Script table--may be Null */
232 RecordArrayOf<LangSys>
233 langSys; /* Array of LangSysRecords--listed
234 * alphabetically by LangSysTag */
236 DEFINE_SIZE_ARRAY (4, langSys);
239 typedef RecordListOf<Script> ScriptList;
244 inline unsigned int get_lookup_count (void) const
245 { return lookupIndex.len; }
246 inline hb_tag_t get_lookup_index (unsigned int i) const
247 { return lookupIndex[i]; }
248 inline unsigned int get_lookup_indexes (unsigned int start_index,
249 unsigned int *lookup_count /* IN/OUT */,
250 unsigned int *lookup_tags /* OUT */) const
251 { return lookupIndex.get_indexes (start_index, lookup_count, lookup_tags); }
253 inline bool sanitize (hb_sanitize_context_t *c) {
255 return c->check_struct (this)
256 && lookupIndex.sanitize (c);
259 /* LONGTERMTODO: implement get_feature_parameters() */
260 /* LONGTERMTODO: implement FeatureSize and other special features? */
261 Offset featureParams; /* Offset to Feature Parameters table (if one
262 * has been defined for the feature), relative
263 * to the beginning of the Feature Table; = Null
265 IndexArray lookupIndex; /* Array of LookupList indices */
267 DEFINE_SIZE_ARRAY (4, lookupIndex);
270 typedef RecordListOf<Feature> FeatureList;
273 struct LookupFlag : USHORT
276 RightToLeft = 0x0001u,
277 IgnoreBaseGlyphs = 0x0002u,
278 IgnoreLigatures = 0x0004u,
279 IgnoreMarks = 0x0008u,
280 IgnoreFlags = 0x000Eu,
281 UseMarkFilteringSet = 0x0010u,
283 MarkAttachmentType = 0xFF00u
286 DEFINE_SIZE_STATIC (2);
291 inline unsigned int get_subtable_count (void) const { return subTable.len; }
293 inline unsigned int get_type (void) const { return lookupType; }
294 inline unsigned int get_flag (void) const
296 unsigned int flag = lookupFlag;
297 if (unlikely (flag & LookupFlag::UseMarkFilteringSet))
299 const USHORT &markFilteringSet = StructAfter<USHORT> (subTable);
300 flag += (markFilteringSet << 16);
305 inline bool sanitize (hb_sanitize_context_t *c) {
307 /* Real sanitize of the subtables is done by GSUB/GPOS/... */
308 if (!(c->check_struct (this)
309 && subTable.sanitize (c))) return false;
310 if (unlikely (lookupFlag & LookupFlag::UseMarkFilteringSet))
312 USHORT &markFilteringSet = StructAfter<USHORT> (subTable);
313 if (!markFilteringSet.sanitize (c)) return false;
318 USHORT lookupType; /* Different enumerations for GSUB and GPOS */
319 USHORT lookupFlag; /* Lookup qualifiers */
321 subTable; /* Array of SubTables */
322 USHORT markFilteringSetX[VAR]; /* Index (base 0) into GDEF mark glyph sets
323 * structure. This field is only present if bit
324 * UseMarkFilteringSet of lookup flags is set. */
326 DEFINE_SIZE_ARRAY2 (6, subTable, markFilteringSetX);
329 typedef OffsetListOf<Lookup> LookupList;
336 struct CoverageFormat1
338 friend struct Coverage;
341 inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
343 int i = glyphArray.search (glyph_id);
349 inline bool sanitize (hb_sanitize_context_t *c) {
351 return glyphArray.sanitize (c);
355 USHORT coverageFormat; /* Format identifier--format = 1 */
356 SortedArrayOf<GlyphID>
357 glyphArray; /* Array of GlyphIDs--in numerical order */
359 DEFINE_SIZE_ARRAY (4, glyphArray);
362 struct CoverageFormat2
364 friend struct Coverage;
367 inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
369 int i = rangeRecord.search (glyph_id);
371 const RangeRecord &range = rangeRecord[i];
372 return (unsigned int) range.value + (glyph_id - range.start);
377 inline bool sanitize (hb_sanitize_context_t *c) {
379 return rangeRecord.sanitize (c);
383 USHORT coverageFormat; /* Format identifier--format = 2 */
384 SortedArrayOf<RangeRecord>
385 rangeRecord; /* Array of glyph ranges--ordered by
386 * Start GlyphID. rangeCount entries
389 DEFINE_SIZE_ARRAY (4, rangeRecord);
394 inline unsigned int operator () (hb_codepoint_t glyph_id) const { return get_coverage (glyph_id); }
396 inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
399 case 1: return u.format1.get_coverage(glyph_id);
400 case 2: return u.format2.get_coverage(glyph_id);
401 default:return NOT_COVERED;
405 inline bool sanitize (hb_sanitize_context_t *c) {
407 if (!u.format.sanitize (c)) return false;
409 case 1: return u.format1.sanitize (c);
410 case 2: return u.format2.sanitize (c);
417 USHORT format; /* Format identifier */
418 CoverageFormat1 format1;
419 CoverageFormat2 format2;
422 DEFINE_SIZE_UNION (2, format);
427 * Class Definition Table
430 struct ClassDefFormat1
432 friend struct ClassDef;
435 inline hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
437 if ((unsigned int) (glyph_id - startGlyph) < classValue.len)
438 return classValue[glyph_id - startGlyph];
442 inline bool sanitize (hb_sanitize_context_t *c) {
444 return c->check_struct (this)
445 && classValue.sanitize (c);
448 USHORT classFormat; /* Format identifier--format = 1 */
449 GlyphID startGlyph; /* First GlyphID of the classValueArray */
451 classValue; /* Array of Class Values--one per GlyphID */
453 DEFINE_SIZE_ARRAY (6, classValue);
456 struct ClassDefFormat2
458 friend struct ClassDef;
461 inline hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
463 int i = rangeRecord.search (glyph_id);
465 return rangeRecord[i].value;
469 inline bool sanitize (hb_sanitize_context_t *c) {
471 return rangeRecord.sanitize (c);
474 USHORT classFormat; /* Format identifier--format = 2 */
475 SortedArrayOf<RangeRecord>
476 rangeRecord; /* Array of glyph ranges--ordered by
479 DEFINE_SIZE_ARRAY (4, rangeRecord);
484 inline hb_ot_layout_class_t operator () (hb_codepoint_t glyph_id) const { return get_class (glyph_id); }
486 inline hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
489 case 1: return u.format1.get_class(glyph_id);
490 case 2: return u.format2.get_class(glyph_id);
495 inline bool sanitize (hb_sanitize_context_t *c) {
497 if (!u.format.sanitize (c)) return false;
499 case 1: return u.format1.sanitize (c);
500 case 2: return u.format2.sanitize (c);
507 USHORT format; /* Format identifier */
508 ClassDefFormat1 format1;
509 ClassDefFormat2 format2;
512 DEFINE_SIZE_UNION (2, format);
524 inline hb_position_t get_x_delta (hb_ot_layout_context_t *c) const
525 { return c->font->x_ppem ? get_delta (c->font->x_ppem) * (uint64_t) c->font->x_scale / c->font->x_ppem : 0; }
527 inline hb_position_t get_y_delta (hb_ot_layout_context_t *c) const
528 { return c->font->y_ppem ? get_delta (c->font->y_ppem) * (uint64_t) c->font->y_scale / c->font->y_ppem : 0; }
530 inline int get_delta (unsigned int ppem_size) const
532 unsigned int f = deltaFormat;
533 if (unlikely (f < 1 || f > 3))
536 if (ppem_size < startSize || ppem_size > endSize)
539 unsigned int s = ppem_size - startSize;
541 unsigned int byte = deltaValue[s >> (4 - f)];
542 unsigned int bits = (byte >> (16 - (((s & ((1 << (4 - f)) - 1)) + 1) << f)));
543 unsigned int mask = (0xFFFF >> (16 - (1 << f)));
545 int delta = bits & mask;
547 if ((unsigned int) delta >= ((mask + 1) >> 1))
553 inline unsigned int get_size () const
555 unsigned int f = deltaFormat;
556 if (unlikely (f < 1 || f > 3 || startSize > endSize)) return 3 * USHORT::static_size;
557 return USHORT::static_size * (4 + ((endSize - startSize) >> (4 - f)));
560 inline bool sanitize (hb_sanitize_context_t *c) {
562 return c->check_struct (this)
563 && c->check_range (this, this->get_size ());
567 USHORT startSize; /* Smallest size to correct--in ppem */
568 USHORT endSize; /* Largest size to correct--in ppem */
569 USHORT deltaFormat; /* Format of DeltaValue array data: 1, 2, or 3
570 * 1 Signed 2-bit value, 8 values per uint16
571 * 2 Signed 4-bit value, 4 values per uint16
572 * 3 Signed 8-bit value, 2 values per uint16
574 USHORT deltaValue[VAR]; /* Array of compressed data */
576 DEFINE_SIZE_ARRAY (6, deltaValue);
583 #endif /* HB_OT_LAYOUT_COMMON_PRIVATE_HH */