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 Tag tag; /* 4-byte Tag identifier */
62 offset; /* Offset from beginning of object holding
65 DEFINE_SIZE_STATIC (6);
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, this);
120 struct IndexArray : ArrayOf<USHORT>
122 inline USHORT operator [] (unsigned int i) const
124 if (unlikely (i >= this->len)) {
129 return this->array[i];
131 inline unsigned int get_indexes (unsigned int start_offset,
132 unsigned int *_count /* IN/OUT */,
133 unsigned int *_indexes /* OUT */) const
136 const USHORT *array = this->sub_array (start_offset, _count);
137 unsigned int count = *_count;
138 for (unsigned int i = 0; i < count; i++)
139 _indexes[i] = array[i];
153 inline unsigned int get_feature_count (void) const
154 { return featureIndex.len; }
155 inline hb_tag_t get_feature_index (unsigned int i) const
156 { return featureIndex[i]; }
157 inline unsigned int get_feature_indexes (unsigned int start_offset,
158 unsigned int *feature_count /* IN/OUT */,
159 unsigned int *feature_indexes /* OUT */) const
160 { return featureIndex.get_indexes (start_offset, feature_count, feature_indexes); }
162 inline bool has_required_feature (void) const { return reqFeatureIndex != 0xffff; }
163 inline int get_required_feature_index (void) const
165 if (reqFeatureIndex == 0xffff)
167 return reqFeatureIndex;;
170 inline bool sanitize (hb_sanitize_context_t *context) {
172 return context->check_struct (this)
173 && featureIndex.sanitize (context);
176 Offset lookupOrder; /* = Null (reserved for an offset to a
177 * reordering table) */
178 USHORT reqFeatureIndex;/* Index of a feature required for this
179 * language system--if no required features
181 IndexArray featureIndex; /* Array of indices into the FeatureList */
183 DEFINE_SIZE_ARRAY (6, featureIndex);
185 DEFINE_NULL_DATA (LangSys, "\0\0\xFF\xFF");
190 inline unsigned int get_lang_sys_count (void) const
191 { return langSys.len; }
192 inline const Tag& get_lang_sys_tag (unsigned int i) const
193 { return langSys.get_tag (i); }
194 inline unsigned int get_lang_sys_tags (unsigned int start_offset,
195 unsigned int *lang_sys_count /* IN/OUT */,
196 hb_tag_t *lang_sys_tags /* OUT */) const
197 { return langSys.get_tags (start_offset, lang_sys_count, lang_sys_tags); }
198 inline const LangSys& get_lang_sys (unsigned int i) const
200 if (i == NO_INDEX) return get_default_lang_sys ();
201 return this+langSys[i].offset;
203 inline bool find_lang_sys_index (hb_tag_t tag, unsigned int *index) const
204 { return langSys.find_index (tag, index); }
206 inline bool has_default_lang_sys (void) const { return defaultLangSys != 0; }
207 inline const LangSys& get_default_lang_sys (void) const { return this+defaultLangSys; }
209 inline bool sanitize (hb_sanitize_context_t *context) {
211 return defaultLangSys.sanitize (context, this)
212 && langSys.sanitize (context, this);
217 defaultLangSys; /* Offset to DefaultLangSys table--from
218 * beginning of Script table--may be Null */
219 RecordArrayOf<LangSys>
220 langSys; /* Array of LangSysRecords--listed
221 * alphabetically by LangSysTag */
223 DEFINE_SIZE_ARRAY (4, langSys);
226 typedef RecordListOf<Script> ScriptList;
231 inline unsigned int get_lookup_count (void) const
232 { return lookupIndex.len; }
233 inline hb_tag_t get_lookup_index (unsigned int i) const
234 { return lookupIndex[i]; }
235 inline unsigned int get_lookup_indexes (unsigned int start_index,
236 unsigned int *lookup_count /* IN/OUT */,
237 unsigned int *lookup_tags /* OUT */) const
238 { return lookupIndex.get_indexes (start_index, lookup_count, lookup_tags); }
240 inline bool sanitize (hb_sanitize_context_t *context) {
242 return context->check_struct (this)
243 && lookupIndex.sanitize (context);
246 /* LONGTERMTODO: implement get_feature_parameters() */
247 /* LONGTERMTODO: implement FeatureSize and other special features? */
248 Offset featureParams; /* Offset to Feature Parameters table (if one
249 * has been defined for the feature), relative
250 * to the beginning of the Feature Table; = Null
252 IndexArray lookupIndex; /* Array of LookupList indices */
254 DEFINE_SIZE_ARRAY (4, lookupIndex);
257 typedef RecordListOf<Feature> FeatureList;
260 struct LookupFlag : USHORT
263 RightToLeft = 0x0001u,
264 IgnoreBaseGlyphs = 0x0002u,
265 IgnoreLigatures = 0x0004u,
266 IgnoreMarks = 0x0008u,
267 IgnoreFlags = 0x000Eu,
268 UseMarkFilteringSet = 0x0010u,
270 MarkAttachmentType = 0xFF00u
273 DEFINE_SIZE_STATIC (2);
278 inline unsigned int get_subtable_count (void) const { return subTable.len; }
280 inline unsigned int get_type (void) const { return lookupType; }
281 inline unsigned int get_flag (void) const
283 unsigned int flag = lookupFlag;
284 if (unlikely (flag & LookupFlag::UseMarkFilteringSet))
286 const USHORT &markFilteringSet = StructAfter<USHORT> (subTable);
287 flag += (markFilteringSet << 16);
292 inline bool sanitize (hb_sanitize_context_t *context) {
294 /* Real sanitize of the subtables is done by GSUB/GPOS/... */
295 if (!(context->check_struct (this)
296 && likely (subTable.sanitize (context)))) return false;
297 if (unlikely (lookupFlag & LookupFlag::UseMarkFilteringSet))
299 USHORT &markFilteringSet = StructAfter<USHORT> (subTable);
300 if (!markFilteringSet.sanitize (context)) return false;
305 USHORT lookupType; /* Different enumerations for GSUB and GPOS */
306 USHORT lookupFlag; /* Lookup qualifiers */
308 subTable; /* Array of SubTables */
309 USHORT markFilteringSetX[VAR]; /* Index (base 0) into GDEF mark glyph sets
310 * structure. This field is only present if bit
311 * UseMarkFilteringSet of lookup flags is set. */
313 DEFINE_SIZE_ARRAY2 (6, subTable, markFilteringSetX);
316 typedef OffsetListOf<Lookup> LookupList;
323 struct CoverageFormat1
325 friend struct Coverage;
328 inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
330 if (unlikely (glyph_id > 0xFFFF))
334 /* TODO: bsearch (need to sort in sanitize) */
335 unsigned int num_glyphs = glyphArray.len;
336 for (unsigned int i = 0; i < num_glyphs; i++)
337 if (gid == glyphArray[i])
342 inline bool sanitize (hb_sanitize_context_t *context) {
344 return glyphArray.sanitize (context);
348 USHORT coverageFormat; /* Format identifier--format = 1 */
350 glyphArray; /* Array of GlyphIDs--in numerical order */
352 DEFINE_SIZE_ARRAY (4, glyphArray);
355 struct CoverageRangeRecord
357 friend struct CoverageFormat2;
360 inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
362 if (glyph_id >= start && glyph_id <= end)
363 return (unsigned int) startCoverageIndex + (glyph_id - start);
368 inline bool sanitize (hb_sanitize_context_t *context) {
370 return context->check_struct (this);
374 GlyphID start; /* First GlyphID in the range */
375 GlyphID end; /* Last GlyphID in the range */
376 USHORT startCoverageIndex; /* Coverage Index of first GlyphID in
379 DEFINE_SIZE_STATIC (6);
381 DEFINE_NULL_DATA (CoverageRangeRecord, "\000\001");
383 struct CoverageFormat2
385 friend struct Coverage;
388 inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
390 /* TODO: bsearch (need to sort in sanitize) */
391 unsigned int count = rangeRecord.len;
392 for (unsigned int i = 0; i < count; i++)
394 unsigned int coverage = rangeRecord[i].get_coverage (glyph_id);
395 if (coverage != NOT_COVERED)
401 inline bool sanitize (hb_sanitize_context_t *context) {
403 return rangeRecord.sanitize (context);
407 USHORT coverageFormat; /* Format identifier--format = 2 */
408 ArrayOf<CoverageRangeRecord>
409 rangeRecord; /* Array of glyph ranges--ordered by
410 * Start GlyphID. rangeCount entries
413 DEFINE_SIZE_ARRAY (4, rangeRecord);
418 inline unsigned int operator () (hb_codepoint_t glyph_id) const { return get_coverage (glyph_id); }
420 inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
423 case 1: return u.format1.get_coverage(glyph_id);
424 case 2: return u.format2.get_coverage(glyph_id);
425 default:return NOT_COVERED;
429 inline bool sanitize (hb_sanitize_context_t *context) {
431 if (!u.format.sanitize (context)) return false;
433 case 1: return u.format1.sanitize (context);
434 case 2: return u.format2.sanitize (context);
441 USHORT format; /* Format identifier */
442 CoverageFormat1 format1;
443 CoverageFormat2 format2;
446 DEFINE_SIZE_UNION (2, format);
451 * Class Definition Table
454 struct ClassDefFormat1
456 friend struct ClassDef;
459 inline hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
461 if ((unsigned int) (glyph_id - startGlyph) < classValue.len)
462 return classValue[glyph_id - startGlyph];
466 inline bool sanitize (hb_sanitize_context_t *context) {
468 return context->check_struct (this)
469 && classValue.sanitize (context);
472 USHORT classFormat; /* Format identifier--format = 1 */
473 GlyphID startGlyph; /* First GlyphID of the classValueArray */
475 classValue; /* Array of Class Values--one per GlyphID */
477 DEFINE_SIZE_ARRAY (6, classValue);
480 struct ClassRangeRecord
482 friend struct ClassDefFormat2;
485 inline hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
487 if (glyph_id >= start && glyph_id <= end)
493 inline bool sanitize (hb_sanitize_context_t *context) {
495 return context->check_struct (this);
499 GlyphID start; /* First GlyphID in the range */
500 GlyphID end; /* Last GlyphID in the range */
501 USHORT classValue; /* Applied to all glyphs in the range */
503 DEFINE_SIZE_STATIC (6);
505 DEFINE_NULL_DATA (ClassRangeRecord, "\000\001");
507 struct ClassDefFormat2
509 friend struct ClassDef;
512 inline hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
514 /* TODO: bsearch (need to sort in sanitize) */
515 unsigned int count = rangeRecord.len;
516 for (unsigned int i = 0; i < count; i++)
518 int classValue = rangeRecord[i].get_class (glyph_id);
525 inline bool sanitize (hb_sanitize_context_t *context) {
527 return rangeRecord.sanitize (context);
530 USHORT classFormat; /* Format identifier--format = 2 */
531 ArrayOf<ClassRangeRecord>
532 rangeRecord; /* Array of glyph ranges--ordered by
535 DEFINE_SIZE_ARRAY (4, rangeRecord);
540 inline hb_ot_layout_class_t operator () (hb_codepoint_t glyph_id) const { return get_class (glyph_id); }
542 inline hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
545 case 1: return u.format1.get_class(glyph_id);
546 case 2: return u.format2.get_class(glyph_id);
551 inline bool sanitize (hb_sanitize_context_t *context) {
553 if (!u.format.sanitize (context)) return false;
555 case 1: return u.format1.sanitize (context);
556 case 2: return u.format2.sanitize (context);
563 USHORT format; /* Format identifier */
564 ClassDefFormat1 format1;
565 ClassDefFormat2 format2;
568 DEFINE_SIZE_UNION (2, format);
578 inline int operator () (unsigned int ppem_size) const { return get_delta (ppem_size); }
580 inline int get_delta (unsigned int ppem_size) const
582 unsigned int f = deltaFormat;
583 if (unlikely (f < 1 || f > 3))
586 if (ppem_size < startSize || ppem_size > endSize)
589 unsigned int s = ppem_size - startSize;
591 unsigned int byte = deltaValue[s >> (4 - f)];
592 unsigned int bits = (byte >> (16 - (((s & ((1 << (4 - f)) - 1)) + 1) << f)));
593 unsigned int mask = (0xFFFF >> (16 - (1 << f)));
595 int delta = bits & mask;
597 if ((unsigned int) delta >= ((mask + 1) >> 1))
603 inline unsigned int get_size () const
605 unsigned int f = deltaFormat;
606 if (unlikely (f < 1 || f > 3 || startSize > endSize)) return 3 * USHORT::static_size;
607 return USHORT::static_size * (4 + ((endSize - startSize) >> (4 - f)));
610 inline bool sanitize (hb_sanitize_context_t *context) {
612 return context->check_struct (this)
613 && context->check_range (this, this->get_size ());
617 USHORT startSize; /* Smallest size to correct--in ppem */
618 USHORT endSize; /* Largest size to correct--in ppem */
619 USHORT deltaFormat; /* Format of DeltaValue array data: 1, 2, or 3
620 * 1 Signed 2-bit value, 8 values per uint16
621 * 2 Signed 4-bit value, 4 values per uint16
622 * 3 Signed 8-bit value, 2 values per uint16
624 USHORT deltaValue[VAR]; /* Array of compressed data */
626 DEFINE_SIZE_ARRAY (6, deltaValue);
630 #endif /* HB_OT_LAYOUT_COMMON_PRIVATE_HH */