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 inline bool sanitize (hb_sanitize_context_t *context, void *base) {
56 return context->check_struct (this)
57 && offset.sanitize (context, base);
60 DEFINE_SIZE_STATIC (6);
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 DEFINE_SIZE_STATIC (6);
175 Offset lookupOrder; /* = Null (reserved for an offset to a
176 * reordering table) */
177 USHORT reqFeatureIndex;/* Index of a feature required for this
178 * language system--if no required features
180 IndexArray featureIndex; /* Array of indices into the FeatureList */
182 DEFINE_NULL_DATA (LangSys, "\0\0\xFF\xFF");
187 inline unsigned int get_lang_sys_count (void) const
188 { return langSys.len; }
189 inline const Tag& get_lang_sys_tag (unsigned int i) const
190 { return langSys.get_tag (i); }
191 inline unsigned int get_lang_sys_tags (unsigned int start_offset,
192 unsigned int *lang_sys_count /* IN/OUT */,
193 hb_tag_t *lang_sys_tags /* OUT */) const
194 { return langSys.get_tags (start_offset, lang_sys_count, lang_sys_tags); }
195 inline const LangSys& get_lang_sys (unsigned int i) const
197 if (i == NO_INDEX) return get_default_lang_sys ();
198 return this+langSys[i].offset;
200 inline bool find_lang_sys_index (hb_tag_t tag, unsigned int *index) const
201 { return langSys.find_index (tag, index); }
203 inline bool has_default_lang_sys (void) const { return defaultLangSys != 0; }
204 inline const LangSys& get_default_lang_sys (void) const { return this+defaultLangSys; }
206 inline bool sanitize (hb_sanitize_context_t *context) {
208 return defaultLangSys.sanitize (context, this)
209 && langSys.sanitize (context, this);
214 defaultLangSys; /* Offset to DefaultLangSys table--from
215 * beginning of Script table--may be Null */
216 RecordArrayOf<LangSys>
217 langSys; /* Array of LangSysRecords--listed
218 * alphabetically by LangSysTag */
220 ASSERT_SIZE (Script, 4);
222 typedef RecordListOf<Script> ScriptList;
223 ASSERT_SIZE (ScriptList, 2);
228 inline unsigned int get_lookup_count (void) const
229 { return lookupIndex.len; }
230 inline hb_tag_t get_lookup_index (unsigned int i) const
231 { return lookupIndex[i]; }
232 inline unsigned int get_lookup_indexes (unsigned int start_index,
233 unsigned int *lookup_count /* IN/OUT */,
234 unsigned int *lookup_tags /* OUT */) const
235 { return lookupIndex.get_indexes (start_index, lookup_count, lookup_tags); }
237 inline bool sanitize (hb_sanitize_context_t *context) {
239 return context->check_struct (this)
240 && lookupIndex.sanitize (context);
243 /* LONGTERMTODO: implement get_feature_parameters() */
244 /* LONGTERMTODO: implement FeatureSize and other special features? */
245 Offset featureParams; /* Offset to Feature Parameters table (if one
246 * has been defined for the feature), relative
247 * to the beginning of the Feature Table; = Null
249 IndexArray lookupIndex; /* Array of LookupList indices */
251 ASSERT_SIZE (Feature, 4);
253 typedef RecordListOf<Feature> FeatureList;
254 ASSERT_SIZE (FeatureList, 2);
257 struct LookupFlag : USHORT
260 RightToLeft = 0x0001u,
261 IgnoreBaseGlyphs = 0x0002u,
262 IgnoreLigatures = 0x0004u,
263 IgnoreMarks = 0x0008u,
264 IgnoreFlags = 0x000Eu,
265 UseMarkFilteringSet = 0x0010u,
267 MarkAttachmentType = 0xFF00u
270 ASSERT_SIZE (LookupFlag, 2);
274 inline unsigned int get_subtable_count (void) const { return subTable.len; }
276 inline unsigned int get_type (void) const { return lookupType; }
277 inline unsigned int get_flag (void) const
279 unsigned int flag = lookupFlag;
280 if (unlikely (flag & LookupFlag::UseMarkFilteringSet))
282 const USHORT &markFilteringSet = StructAfter<USHORT> (subTable);
283 flag += (markFilteringSet << 16);
288 inline bool sanitize (hb_sanitize_context_t *context) {
290 /* Real sanitize of the subtables is done by GSUB/GPOS/... */
291 if (!(context->check_struct (this)
292 && likely (subTable.sanitize (context)))) return false;
293 if (unlikely (lookupFlag & LookupFlag::UseMarkFilteringSet))
295 USHORT &markFilteringSet = StructAfter<USHORT> (subTable);
296 if (!markFilteringSet.sanitize (context)) return false;
301 USHORT lookupType; /* Different enumerations for GSUB and GPOS */
302 USHORT lookupFlag; /* Lookup qualifiers */
304 subTable; /* Array of SubTables */
305 USHORT markFilteringSetX[VAR]; /* Index (base 0) into GDEF mark glyph sets
306 * structure. This field is only present if bit
307 * UseMarkFilteringSet of lookup flags is set. */
309 ASSERT_SIZE_VAR (Lookup, 6, USHORT);
311 typedef OffsetListOf<Lookup> LookupList;
312 ASSERT_SIZE (LookupList, 2);
319 struct CoverageFormat1
321 friend struct Coverage;
324 inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
326 if (unlikely (glyph_id > 0xFFFF))
330 /* TODO: bsearch (need to sort in sanitize) */
331 unsigned int num_glyphs = glyphArray.len;
332 for (unsigned int i = 0; i < num_glyphs; i++)
333 if (gid == glyphArray[i])
338 inline bool sanitize (hb_sanitize_context_t *context) {
340 return glyphArray.sanitize (context);
344 USHORT coverageFormat; /* Format identifier--format = 1 */
346 glyphArray; /* Array of GlyphIDs--in numerical order */
348 ASSERT_SIZE (CoverageFormat1, 4);
350 struct CoverageRangeRecord
352 friend struct CoverageFormat2;
355 inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
357 if (glyph_id >= start && glyph_id <= end)
358 return (unsigned int) startCoverageIndex + (glyph_id - start);
363 inline bool sanitize (hb_sanitize_context_t *context) {
365 return context->check_struct (this);
368 DEFINE_SIZE_STATIC (6);
371 GlyphID start; /* First GlyphID in the range */
372 GlyphID end; /* Last GlyphID in the range */
373 USHORT startCoverageIndex; /* Coverage Index of first GlyphID in
376 DEFINE_NULL_DATA (CoverageRangeRecord, "\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;
476 inline hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
478 if (glyph_id >= start && glyph_id <= end)
484 inline bool sanitize (hb_sanitize_context_t *context) {
486 return context->check_struct (this);
489 DEFINE_SIZE_STATIC (6);
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 DEFINE_NULL_DATA (ClassRangeRecord, "\000\001");
498 struct ClassDefFormat2
500 friend struct ClassDef;
503 inline hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
505 /* TODO: bsearch (need to sort in sanitize) */
506 unsigned int count = rangeRecord.len;
507 for (unsigned int i = 0; i < count; i++)
509 int classValue = rangeRecord[i].get_class (glyph_id);
516 inline bool sanitize (hb_sanitize_context_t *context) {
518 return rangeRecord.sanitize (context);
521 USHORT classFormat; /* Format identifier--format = 2 */
522 ArrayOf<ClassRangeRecord>
523 rangeRecord; /* Array of glyph ranges--ordered by
526 ASSERT_SIZE (ClassDefFormat2, 4);
530 inline hb_ot_layout_class_t operator () (hb_codepoint_t glyph_id) const { return get_class (glyph_id); }
532 inline hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
535 case 1: return u.format1->get_class(glyph_id);
536 case 2: return u.format2->get_class(glyph_id);
541 inline bool sanitize (hb_sanitize_context_t *context) {
543 if (!u.format.sanitize (context)) return false;
545 case 1: return u.format1->sanitize (context);
546 case 2: return u.format2->sanitize (context);
553 USHORT format; /* Format identifier */
554 ClassDefFormat1 format1[VAR];
555 ClassDefFormat2 format2[VAR];
566 inline int operator () (unsigned int ppem_size) const { return get_delta (ppem_size); }
568 inline int get_delta (unsigned int ppem_size) const
570 unsigned int f = deltaFormat;
571 if (unlikely (f < 1 || f > 3))
574 if (ppem_size < startSize || ppem_size > endSize)
577 unsigned int s = ppem_size - startSize;
579 unsigned int byte = deltaValue[s >> (4 - f)];
580 unsigned int bits = (byte >> (16 - (((s & ((1 << (4 - f)) - 1)) + 1) << f)));
581 unsigned int mask = (0xFFFF >> (16 - (1 << f)));
583 int delta = bits & mask;
585 if ((unsigned int) delta >= ((mask + 1) >> 1))
591 inline unsigned int get_size () const
593 unsigned int f = deltaFormat;
594 if (unlikely (f < 1 || f > 3 || startSize > endSize)) return 3 * USHORT::get_size ();
595 return USHORT::get_size () * (4 + ((endSize - startSize) >> (4 - f)));
598 inline bool sanitize (hb_sanitize_context_t *context) {
600 return context->check_struct (this)
601 && context->check_range (this, this->get_size ());
605 USHORT startSize; /* Smallest size to correct--in ppem */
606 USHORT endSize; /* Largest size to correct--in ppem */
607 USHORT deltaFormat; /* Format of DeltaValue array data: 1, 2, or 3
608 * 1 Signed 2-bit value, 8 values per uint16
609 * 2 Signed 4-bit value, 4 values per uint16
610 * 3 Signed 8-bit value, 2 values per uint16
612 USHORT deltaValue[VAR]; /* Array of compressed data */
614 ASSERT_SIZE_VAR (Device, 6, USHORT);
617 #endif /* HB_OT_LAYOUT_COMMON_PRIVATE_HH */