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.h"
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
42 * OpenType Layout Common Table Formats
48 * Script, ScriptList, LangSys, Feature, FeatureList, Lookup, LookupList
51 template <typename Type>
54 static inline unsigned int get_size () { return sizeof (Record<Type>); }
56 inline bool sanitize (hb_sanitize_context_t *context, void *base) {
58 return context->check_struct (this)
59 && offset.sanitize (context, base);
62 Tag tag; /* 4-byte Tag identifier */
64 offset; /* Offset from beginning of object holding
68 template <typename Type>
69 struct RecordArrayOf : ArrayOf<Record<Type> > {
70 inline const Tag& get_tag (unsigned int i) const
72 if (unlikely (i >= this->len)) return Null(Tag);
73 return (*this)[i].tag;
75 inline unsigned int get_tags (unsigned int start_offset,
76 unsigned int *record_count /* IN/OUT */,
77 hb_tag_t *record_tags /* OUT */) const
80 const Record<Type> *array = this->sub_array (start_offset, record_count);
81 unsigned int count = *record_count;
82 for (unsigned int i = 0; i < count; i++)
83 record_tags[i] = array[i].tag;
87 inline bool find_index (hb_tag_t tag, unsigned int *index) const
91 /* TODO: bsearch (need to sort in sanitize) */
92 const Record<Type> *a = this->array();
93 unsigned int count = this->len;
94 for (unsigned int i = 0; i < count; i++)
98 if (index) *index = i;
102 if (index) *index = NO_INDEX;
107 template <typename Type>
108 struct RecordListOf : RecordArrayOf<Type>
110 inline const Type& operator [] (unsigned int i) const
111 { return this+RecordArrayOf<Type>::operator [](i).offset; }
113 inline bool sanitize (hb_sanitize_context_t *context) {
115 return RecordArrayOf<Type>::sanitize (context, CharP(this));
120 struct IndexArray : ArrayOf<USHORT>
122 inline unsigned int operator [] (unsigned int i) const
124 if (unlikely (i >= this->len))
126 return this->array()[i];
128 inline unsigned int get_indexes (unsigned int start_offset,
129 unsigned int *_count /* IN/OUT */,
130 unsigned int *_indexes /* OUT */) const
133 const USHORT *array = this->sub_array (start_offset, _count);
134 unsigned int count = *_count;
135 for (unsigned int i = 0; i < count; i++)
136 _indexes[i] = array[i];
150 inline unsigned int get_feature_count (void) const
151 { return featureIndex.len; }
152 inline hb_tag_t get_feature_index (unsigned int i) const
153 { return featureIndex[i]; }
154 inline unsigned int get_feature_indexes (unsigned int start_offset,
155 unsigned int *feature_count /* IN/OUT */,
156 unsigned int *feature_indexes /* OUT */) const
157 { return featureIndex.get_indexes (start_offset, feature_count, feature_indexes); }
159 inline bool has_required_feature (void) const { return reqFeatureIndex != 0xffff; }
160 inline int get_required_feature_index (void) const
162 if (reqFeatureIndex == 0xffff)
164 return reqFeatureIndex;;
167 inline bool sanitize (hb_sanitize_context_t *context) {
169 return context->check_struct (this)
170 && featureIndex.sanitize (context);
173 Offset lookupOrder; /* = Null (reserved for an offset to a
174 * reordering table) */
175 USHORT reqFeatureIndex;/* Index of a feature required for this
176 * language system--if no required features
178 IndexArray featureIndex; /* Array of indices into the FeatureList */
180 ASSERT_SIZE (LangSys, 6);
181 DEFINE_NULL_DATA (LangSys, 6, "\0\0\xFF\xFF");
186 inline unsigned int get_lang_sys_count (void) const
187 { return langSys.len; }
188 inline const Tag& get_lang_sys_tag (unsigned int i) const
189 { return langSys.get_tag (i); }
190 inline unsigned int get_lang_sys_tags (unsigned int start_offset,
191 unsigned int *lang_sys_count /* IN/OUT */,
192 hb_tag_t *lang_sys_tags /* OUT */) const
193 { return langSys.get_tags (start_offset, lang_sys_count, lang_sys_tags); }
194 inline const LangSys& get_lang_sys (unsigned int i) const
196 if (i == NO_INDEX) return get_default_lang_sys ();
197 return this+langSys[i].offset;
199 inline bool find_lang_sys_index (hb_tag_t tag, unsigned int *index) const
200 { return langSys.find_index (tag, index); }
202 inline bool has_default_lang_sys (void) const { return defaultLangSys != 0; }
203 inline const LangSys& get_default_lang_sys (void) const { return this+defaultLangSys; }
205 inline bool sanitize (hb_sanitize_context_t *context) {
207 return defaultLangSys.sanitize (context, this)
208 && langSys.sanitize (context, this);
213 defaultLangSys; /* Offset to DefaultLangSys table--from
214 * beginning of Script table--may be Null */
215 RecordArrayOf<LangSys>
216 langSys; /* Array of LangSysRecords--listed
217 * alphabetically by LangSysTag */
219 ASSERT_SIZE (Script, 4);
221 typedef RecordListOf<Script> ScriptList;
222 ASSERT_SIZE (ScriptList, 2);
227 inline unsigned int get_lookup_count (void) const
228 { return lookupIndex.len; }
229 inline hb_tag_t get_lookup_index (unsigned int i) const
230 { return lookupIndex[i]; }
231 inline unsigned int get_lookup_indexes (unsigned int start_index,
232 unsigned int *lookup_count /* IN/OUT */,
233 unsigned int *lookup_tags /* OUT */) const
234 { return lookupIndex.get_indexes (start_index, lookup_count, lookup_tags); }
236 inline bool sanitize (hb_sanitize_context_t *context) {
238 return context->check_struct (this)
239 && lookupIndex.sanitize (context);
242 /* LONGTERMTODO: implement get_feature_parameters() */
243 /* LONGTERMTODO: implement FeatureSize and other special features? */
244 Offset featureParams; /* Offset to Feature Parameters table (if one
245 * has been defined for the feature), relative
246 * to the beginning of the Feature Table; = Null
248 IndexArray lookupIndex; /* Array of LookupList indices */
250 ASSERT_SIZE (Feature, 4);
252 typedef RecordListOf<Feature> FeatureList;
253 ASSERT_SIZE (FeatureList, 2);
256 struct LookupFlag : USHORT
259 RightToLeft = 0x0001u,
260 IgnoreBaseGlyphs = 0x0002u,
261 IgnoreLigatures = 0x0004u,
262 IgnoreMarks = 0x0008u,
263 IgnoreFlags = 0x000Eu,
264 UseMarkFilteringSet = 0x0010u,
266 MarkAttachmentType = 0xFF00u
269 ASSERT_SIZE (LookupFlag, 2);
273 inline unsigned int get_subtable_count (void) const { return subTable.len; }
275 inline unsigned int get_type (void) const { return lookupType; }
276 inline unsigned int get_flag (void) const
278 unsigned int flag = lookupFlag;
279 if (unlikely (flag & LookupFlag::UseMarkFilteringSet))
281 const USHORT &markFilteringSet = StructAfter<USHORT> (subTable);
282 flag += (markFilteringSet << 16);
287 inline bool sanitize (hb_sanitize_context_t *context) {
289 /* Real sanitize of the subtables is done by GSUB/GPOS/... */
290 if (!(context->check_struct (this)
291 && likely (subTable.sanitize (context)))) return false;
292 if (unlikely (lookupFlag & LookupFlag::UseMarkFilteringSet))
294 USHORT &markFilteringSet = StructAfter<USHORT> (subTable);
295 if (!markFilteringSet.sanitize (context)) return false;
300 USHORT lookupType; /* Different enumerations for GSUB and GPOS */
301 USHORT lookupFlag; /* Lookup qualifiers */
303 subTable; /* Array of SubTables */
304 USHORT markFilteringSetX[VAR]; /* Index (base 0) into GDEF mark glyph sets
305 * structure. This field is only present if bit
306 * UseMarkFilteringSet of lookup flags is set. */
308 ASSERT_SIZE_VAR (Lookup, 6, USHORT);
310 typedef OffsetListOf<Lookup> LookupList;
311 ASSERT_SIZE (LookupList, 2);
318 struct CoverageFormat1
320 friend struct Coverage;
323 inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
325 if (unlikely (glyph_id > 0xFFFF))
329 /* TODO: bsearch (need to sort in sanitize) */
330 unsigned int num_glyphs = glyphArray.len;
331 for (unsigned int i = 0; i < num_glyphs; i++)
332 if (gid == glyphArray[i])
337 inline bool sanitize (hb_sanitize_context_t *context) {
339 return glyphArray.sanitize (context);
343 USHORT coverageFormat; /* Format identifier--format = 1 */
345 glyphArray; /* Array of GlyphIDs--in numerical order */
347 ASSERT_SIZE (CoverageFormat1, 4);
349 struct CoverageRangeRecord
351 friend struct CoverageFormat2;
353 static inline unsigned int get_size () { return sizeof (CoverageRangeRecord); }
356 inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
358 if (glyph_id >= start && glyph_id <= end)
359 return (unsigned int) startCoverageIndex + (glyph_id - start);
364 inline bool sanitize (hb_sanitize_context_t *context) {
366 return context->check_struct (this);
370 GlyphID start; /* First GlyphID in the range */
371 GlyphID end; /* Last GlyphID in the range */
372 USHORT startCoverageIndex; /* Coverage Index of first GlyphID in
375 ASSERT_SIZE (CoverageRangeRecord, 6);
376 DEFINE_NULL_DATA (CoverageRangeRecord, 6, "\000\001");
378 struct CoverageFormat2
380 friend struct Coverage;
383 inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
385 /* TODO: bsearch (need to sort in sanitize) */
386 unsigned int count = rangeRecord.len;
387 for (unsigned int i = 0; i < count; i++)
389 unsigned int coverage = rangeRecord[i].get_coverage (glyph_id);
390 if (coverage != NOT_COVERED)
396 inline bool sanitize (hb_sanitize_context_t *context) {
398 return rangeRecord.sanitize (context);
402 USHORT coverageFormat; /* Format identifier--format = 2 */
403 ArrayOf<CoverageRangeRecord>
404 rangeRecord; /* Array of glyph ranges--ordered by
405 * Start GlyphID. rangeCount entries
408 ASSERT_SIZE (CoverageFormat2, 4);
412 inline unsigned int operator () (hb_codepoint_t glyph_id) const { return get_coverage (glyph_id); }
414 inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
417 case 1: return u.format1->get_coverage(glyph_id);
418 case 2: return u.format2->get_coverage(glyph_id);
419 default:return NOT_COVERED;
423 inline bool sanitize (hb_sanitize_context_t *context) {
425 if (!u.format.sanitize (context)) return false;
427 case 1: return u.format1->sanitize (context);
428 case 2: return u.format2->sanitize (context);
435 USHORT format; /* Format identifier */
436 CoverageFormat1 format1[VAR];
437 CoverageFormat2 format2[VAR];
443 * Class Definition Table
446 struct ClassDefFormat1
448 friend struct ClassDef;
451 inline hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
453 if ((unsigned int) (glyph_id - startGlyph) < classValue.len)
454 return classValue[glyph_id - startGlyph];
458 inline bool sanitize (hb_sanitize_context_t *context) {
460 return context->check_struct (this)
461 && classValue.sanitize (context);
464 USHORT classFormat; /* Format identifier--format = 1 */
465 GlyphID startGlyph; /* First GlyphID of the classValueArray */
467 classValue; /* Array of Class Values--one per GlyphID */
469 ASSERT_SIZE (ClassDefFormat1, 6);
471 struct ClassRangeRecord
473 friend struct ClassDefFormat2;
475 static inline unsigned int get_size () { return sizeof (ClassRangeRecord); }
478 inline hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
480 if (glyph_id >= start && glyph_id <= end)
486 inline bool sanitize (hb_sanitize_context_t *context) {
488 return context->check_struct (this);
492 GlyphID start; /* First GlyphID in the range */
493 GlyphID end; /* Last GlyphID in the range */
494 USHORT classValue; /* Applied to all glyphs in the range */
496 ASSERT_SIZE (ClassRangeRecord, 6);
497 DEFINE_NULL_DATA (ClassRangeRecord, 6, "\000\001");
499 struct ClassDefFormat2
501 friend struct ClassDef;
504 inline hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
506 /* TODO: bsearch (need to sort in sanitize) */
507 unsigned int count = rangeRecord.len;
508 for (unsigned int i = 0; i < count; i++)
510 int classValue = rangeRecord[i].get_class (glyph_id);
517 inline bool sanitize (hb_sanitize_context_t *context) {
519 return rangeRecord.sanitize (context);
522 USHORT classFormat; /* Format identifier--format = 2 */
523 ArrayOf<ClassRangeRecord>
524 rangeRecord; /* Array of glyph ranges--ordered by
527 ASSERT_SIZE (ClassDefFormat2, 4);
531 inline hb_ot_layout_class_t operator () (hb_codepoint_t glyph_id) const { return get_class (glyph_id); }
533 inline hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
536 case 1: return u.format1->get_class(glyph_id);
537 case 2: return u.format2->get_class(glyph_id);
542 inline bool sanitize (hb_sanitize_context_t *context) {
544 if (!u.format.sanitize (context)) return false;
546 case 1: return u.format1->sanitize (context);
547 case 2: return u.format2->sanitize (context);
554 USHORT format; /* Format identifier */
555 ClassDefFormat1 format1[VAR];
556 ClassDefFormat2 format2[VAR];
567 inline int operator () (unsigned int ppem_size) const { return get_delta (ppem_size); }
569 inline int get_delta (unsigned int ppem_size) const
571 unsigned int f = deltaFormat;
572 if (unlikely (f < 1 || f > 3))
575 if (ppem_size < startSize || ppem_size > endSize)
578 unsigned int s = ppem_size - startSize;
580 unsigned int byte = deltaValue[s >> (4 - f)];
581 unsigned int bits = (byte >> (16 - (((s & ((1 << (4 - f)) - 1)) + 1) << f)));
582 unsigned int mask = (0xFFFF >> (16 - (1 << f)));
584 int delta = bits & mask;
586 if ((unsigned int) delta >= ((mask + 1) >> 1))
592 inline unsigned int get_size () const
594 unsigned int f = deltaFormat;
595 if (unlikely (f < 1 || f > 3 || startSize > endSize)) return 3 * USHORT::get_size ();
596 return USHORT::get_size () * (4 + ((endSize - startSize) >> (4 - f)));
599 inline bool sanitize (hb_sanitize_context_t *context) {
601 return context->check_struct (this)
602 && context->check_range (this, this->get_size ());
606 USHORT startSize; /* Smallest size to correct--in ppem */
607 USHORT endSize; /* Largest size to correct--in ppem */
608 USHORT deltaFormat; /* Format of DeltaValue array data: 1, 2, or 3
609 * 1 Signed 2-bit value, 8 values per uint16
610 * 2 Signed 4-bit value, 4 values per uint16
611 * 3 Signed 8-bit value, 2 values per uint16
613 USHORT deltaValue[VAR]; /* Array of compressed data */
615 ASSERT_SIZE_VAR (Device, 6, USHORT);
618 #endif /* HB_OT_LAYOUT_COMMON_PRIVATE_HH */