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 (SANITIZE_ARG_DEF, void *base) {
58 return SANITIZE_SELF () && SANITIZE_BASE (offset, base);
61 Tag tag; /* 4-byte Tag identifier */
63 offset; /* Offset from beginning of object holding
67 template <typename Type>
68 struct RecordArrayOf : ArrayOf<Record<Type> > {
69 inline const Tag& get_tag (unsigned int i) const
71 if (unlikely (i >= this->len)) return Null(Tag);
72 return (*this)[i].tag;
74 inline unsigned int get_tags (unsigned int start_offset,
75 unsigned int *record_count /* IN/OUT */,
76 hb_tag_t *record_tags /* OUT */) const
79 const Record<Type> *array = this->sub_array (start_offset, record_count);
80 unsigned int count = *record_count;
81 for (unsigned int i = 0; i < count; i++)
82 record_tags[i] = array[i].tag;
86 inline bool find_index (hb_tag_t tag, unsigned int *index) const
90 /* TODO: bsearch (need to sort in sanitize) */
91 const Record<Type> *a = this->array();
92 unsigned int count = this->len;
93 for (unsigned int i = 0; i < count; i++)
97 if (index) *index = i;
101 if (index) *index = NO_INDEX;
106 template <typename Type>
107 struct RecordListOf : RecordArrayOf<Type>
109 inline const Type& operator [] (unsigned int i) const
110 { return this+RecordArrayOf<Type>::operator [](i).offset; }
112 inline bool sanitize (SANITIZE_ARG_DEF) {
114 return RecordArrayOf<Type>::sanitize (SANITIZE_ARG, CharP(this));
119 struct IndexArray : ArrayOf<USHORT>
121 inline unsigned int operator [] (unsigned int i) const
123 if (unlikely (i >= this->len))
125 return this->array()[i];
127 inline unsigned int get_indexes (unsigned int start_offset,
128 unsigned int *_count /* IN/OUT */,
129 unsigned int *_indexes /* OUT */) const
132 const USHORT *array = this->sub_array (start_offset, _count);
133 unsigned int count = *_count;
134 for (unsigned int i = 0; i < count; i++)
135 _indexes[i] = array[i];
149 inline unsigned int get_feature_count (void) const
150 { return featureIndex.len; }
151 inline hb_tag_t get_feature_index (unsigned int i) const
152 { return featureIndex[i]; }
153 inline unsigned int get_feature_indexes (unsigned int start_offset,
154 unsigned int *feature_count /* IN/OUT */,
155 unsigned int *feature_indexes /* OUT */) const
156 { return featureIndex.get_indexes (start_offset, feature_count, feature_indexes); }
158 inline bool has_required_feature (void) const { return reqFeatureIndex != 0xffff; }
159 inline int get_required_feature_index (void) const
161 if (reqFeatureIndex == 0xffff)
163 return reqFeatureIndex;;
166 inline bool sanitize (SANITIZE_ARG_DEF) {
168 return SANITIZE_SELF () && SANITIZE (featureIndex);
171 Offset lookupOrder; /* = Null (reserved for an offset to a
172 * reordering table) */
173 USHORT reqFeatureIndex;/* Index of a feature required for this
174 * language system--if no required features
176 IndexArray featureIndex; /* Array of indices into the FeatureList */
178 ASSERT_SIZE (LangSys, 6);
179 DEFINE_NULL_DATA (LangSys, 6, "\0\0\xFF\xFF");
184 inline unsigned int get_lang_sys_count (void) const
185 { return langSys.len; }
186 inline const Tag& get_lang_sys_tag (unsigned int i) const
187 { return langSys.get_tag (i); }
188 inline unsigned int get_lang_sys_tags (unsigned int start_offset,
189 unsigned int *lang_sys_count /* IN/OUT */,
190 hb_tag_t *lang_sys_tags /* OUT */) const
191 { return langSys.get_tags (start_offset, lang_sys_count, lang_sys_tags); }
192 inline const LangSys& get_lang_sys (unsigned int i) const
194 if (i == NO_INDEX) return get_default_lang_sys ();
195 return this+langSys[i].offset;
197 inline bool find_lang_sys_index (hb_tag_t tag, unsigned int *index) const
198 { return langSys.find_index (tag, index); }
200 inline bool has_default_lang_sys (void) const { return defaultLangSys != 0; }
201 inline const LangSys& get_default_lang_sys (void) const { return this+defaultLangSys; }
203 inline bool sanitize (SANITIZE_ARG_DEF) {
205 return SANITIZE_THIS (defaultLangSys)
206 && SANITIZE_THIS (langSys);
211 defaultLangSys; /* Offset to DefaultLangSys table--from
212 * beginning of Script table--may be Null */
213 RecordArrayOf<LangSys>
214 langSys; /* Array of LangSysRecords--listed
215 * alphabetically by LangSysTag */
217 ASSERT_SIZE (Script, 4);
219 typedef RecordListOf<Script> ScriptList;
220 ASSERT_SIZE (ScriptList, 2);
225 inline unsigned int get_lookup_count (void) const
226 { return lookupIndex.len; }
227 inline hb_tag_t get_lookup_index (unsigned int i) const
228 { return lookupIndex[i]; }
229 inline unsigned int get_lookup_indexes (unsigned int start_index,
230 unsigned int *lookup_count /* IN/OUT */,
231 unsigned int *lookup_tags /* OUT */) const
232 { return lookupIndex.get_indexes (start_index, lookup_count, lookup_tags); }
234 inline bool sanitize (SANITIZE_ARG_DEF) {
236 return SANITIZE_SELF () && SANITIZE (lookupIndex);
239 /* LONGTERMTODO: implement get_feature_parameters() */
240 /* LONGTERMTODO: implement FeatureSize and other special features? */
241 Offset featureParams; /* Offset to Feature Parameters table (if one
242 * has been defined for the feature), relative
243 * to the beginning of the Feature Table; = Null
245 IndexArray lookupIndex; /* Array of LookupList indices */
247 ASSERT_SIZE (Feature, 4);
249 typedef RecordListOf<Feature> FeatureList;
250 ASSERT_SIZE (FeatureList, 2);
253 struct LookupFlag : USHORT
256 RightToLeft = 0x0001u,
257 IgnoreBaseGlyphs = 0x0002u,
258 IgnoreLigatures = 0x0004u,
259 IgnoreMarks = 0x0008u,
260 IgnoreFlags = 0x000Eu,
261 UseMarkFilteringSet = 0x0010u,
263 MarkAttachmentType = 0xFF00u
266 ASSERT_SIZE (LookupFlag, 2);
270 inline unsigned int get_subtable_count (void) const { return subTable.len; }
272 inline unsigned int get_type (void) const { return lookupType; }
273 inline unsigned int get_flag (void) const
275 unsigned int flag = lookupFlag;
276 if (unlikely (flag & LookupFlag::UseMarkFilteringSet))
278 const USHORT &markFilteringSet = StructAfter<USHORT> (subTable);
279 flag += (markFilteringSet << 16);
284 inline bool sanitize (SANITIZE_ARG_DEF) {
286 /* Real sanitize of the subtables is done by GSUB/GPOS/... */
287 if (!(SANITIZE_SELF () && likely (subTable.sanitize (SANITIZE_ARG)))) return false;
288 if (unlikely (lookupFlag & LookupFlag::UseMarkFilteringSet))
290 USHORT &markFilteringSet = StructAfter<USHORT> (subTable);
291 if (!SANITIZE (markFilteringSet)) return false;
296 USHORT lookupType; /* Different enumerations for GSUB and GPOS */
297 USHORT lookupFlag; /* Lookup qualifiers */
299 subTable; /* Array of SubTables */
300 USHORT markFilteringSetX[VAR]; /* Index (base 0) into GDEF mark glyph sets
301 * structure. This field is only present if bit
302 * UseMarkFilteringSet of lookup flags is set. */
304 ASSERT_SIZE_VAR (Lookup, 6, USHORT);
306 typedef OffsetListOf<Lookup> LookupList;
307 ASSERT_SIZE (LookupList, 2);
314 struct CoverageFormat1
316 friend struct Coverage;
319 inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
321 if (unlikely (glyph_id > 0xFFFF))
325 /* TODO: bsearch (need to sort in sanitize) */
326 unsigned int num_glyphs = glyphArray.len;
327 for (unsigned int i = 0; i < num_glyphs; i++)
328 if (gid == glyphArray[i])
333 inline bool sanitize (SANITIZE_ARG_DEF) {
335 return SANITIZE (glyphArray);
339 USHORT coverageFormat; /* Format identifier--format = 1 */
341 glyphArray; /* Array of GlyphIDs--in numerical order */
343 ASSERT_SIZE (CoverageFormat1, 4);
345 struct CoverageRangeRecord
347 friend struct CoverageFormat2;
349 static inline unsigned int get_size () { return sizeof (CoverageRangeRecord); }
352 inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
354 if (glyph_id >= start && glyph_id <= end)
355 return (unsigned int) startCoverageIndex + (glyph_id - start);
360 inline bool sanitize (SANITIZE_ARG_DEF) {
362 return SANITIZE_SELF ();
366 GlyphID start; /* First GlyphID in the range */
367 GlyphID end; /* Last GlyphID in the range */
368 USHORT startCoverageIndex; /* Coverage Index of first GlyphID in
371 ASSERT_SIZE (CoverageRangeRecord, 6);
372 DEFINE_NULL_DATA (CoverageRangeRecord, 6, "\000\001");
374 struct CoverageFormat2
376 friend struct Coverage;
379 inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
381 /* TODO: bsearch (need to sort in sanitize) */
382 unsigned int count = rangeRecord.len;
383 for (unsigned int i = 0; i < count; i++)
385 unsigned int coverage = rangeRecord[i].get_coverage (glyph_id);
386 if (coverage != NOT_COVERED)
392 inline bool sanitize (SANITIZE_ARG_DEF) {
394 return SANITIZE (rangeRecord);
398 USHORT coverageFormat; /* Format identifier--format = 2 */
399 ArrayOf<CoverageRangeRecord>
400 rangeRecord; /* Array of glyph ranges--ordered by
401 * Start GlyphID. rangeCount entries
404 ASSERT_SIZE (CoverageFormat2, 4);
408 inline unsigned int operator () (hb_codepoint_t glyph_id) const { return get_coverage (glyph_id); }
410 inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
413 case 1: return u.format1->get_coverage(glyph_id);
414 case 2: return u.format2->get_coverage(glyph_id);
415 default:return NOT_COVERED;
419 inline bool sanitize (SANITIZE_ARG_DEF) {
421 if (!SANITIZE (u.format)) return false;
423 case 1: return u.format1->sanitize (SANITIZE_ARG);
424 case 2: return u.format2->sanitize (SANITIZE_ARG);
431 USHORT format; /* Format identifier */
432 CoverageFormat1 format1[VAR];
433 CoverageFormat2 format2[VAR];
439 * Class Definition Table
442 struct ClassDefFormat1
444 friend struct ClassDef;
447 inline hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
449 if ((unsigned int) (glyph_id - startGlyph) < classValue.len)
450 return classValue[glyph_id - startGlyph];
454 inline bool sanitize (SANITIZE_ARG_DEF) {
456 return SANITIZE_SELF () && SANITIZE (classValue);
459 USHORT classFormat; /* Format identifier--format = 1 */
460 GlyphID startGlyph; /* First GlyphID of the classValueArray */
462 classValue; /* Array of Class Values--one per GlyphID */
464 ASSERT_SIZE (ClassDefFormat1, 6);
466 struct ClassRangeRecord
468 friend struct ClassDefFormat2;
470 static inline unsigned int get_size () { return sizeof (ClassRangeRecord); }
473 inline hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
475 if (glyph_id >= start && glyph_id <= end)
481 inline bool sanitize (SANITIZE_ARG_DEF) {
483 return SANITIZE_SELF ();
487 GlyphID start; /* First GlyphID in the range */
488 GlyphID end; /* Last GlyphID in the range */
489 USHORT classValue; /* Applied to all glyphs in the range */
491 ASSERT_SIZE (ClassRangeRecord, 6);
492 DEFINE_NULL_DATA (ClassRangeRecord, 6, "\000\001");
494 struct ClassDefFormat2
496 friend struct ClassDef;
499 inline hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
501 /* TODO: bsearch (need to sort in sanitize) */
502 unsigned int count = rangeRecord.len;
503 for (unsigned int i = 0; i < count; i++)
505 int classValue = rangeRecord[i].get_class (glyph_id);
512 inline bool sanitize (SANITIZE_ARG_DEF) {
514 return SANITIZE (rangeRecord);
517 USHORT classFormat; /* Format identifier--format = 2 */
518 ArrayOf<ClassRangeRecord>
519 rangeRecord; /* Array of glyph ranges--ordered by
522 ASSERT_SIZE (ClassDefFormat2, 4);
526 inline hb_ot_layout_class_t operator () (hb_codepoint_t glyph_id) const { return get_class (glyph_id); }
528 inline hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
531 case 1: return u.format1->get_class(glyph_id);
532 case 2: return u.format2->get_class(glyph_id);
537 inline bool sanitize (SANITIZE_ARG_DEF) {
539 if (!SANITIZE (u.format)) return false;
541 case 1: return u.format1->sanitize (SANITIZE_ARG);
542 case 2: return u.format2->sanitize (SANITIZE_ARG);
549 USHORT format; /* Format identifier */
550 ClassDefFormat1 format1[VAR];
551 ClassDefFormat2 format2[VAR];
562 inline int operator () (unsigned int ppem_size) const { return get_delta (ppem_size); }
564 inline int get_delta (unsigned int ppem_size) const
566 unsigned int f = deltaFormat;
567 if (unlikely (f < 1 || f > 3))
570 if (ppem_size < startSize || ppem_size > endSize)
573 unsigned int s = ppem_size - startSize;
575 unsigned int byte = deltaValue[s >> (4 - f)];
576 unsigned int bits = (byte >> (16 - (((s & ((1 << (4 - f)) - 1)) + 1) << f)));
577 unsigned int mask = (0xFFFF >> (16 - (1 << f)));
579 int delta = bits & mask;
581 if ((unsigned int) delta >= ((mask + 1) >> 1))
587 inline unsigned int get_size () const
589 unsigned int f = deltaFormat;
590 if (unlikely (f < 1 || f > 3 || startSize > endSize)) return 3 * USHORT::get_size ();
591 return USHORT::get_size () * (4 + ((endSize - startSize) >> (4 - f)));
594 inline bool sanitize (SANITIZE_ARG_DEF) {
596 return SANITIZE_SELF() && SANITIZE_MEM (this, this->get_size ());
600 USHORT startSize; /* Smallest size to correct--in ppem */
601 USHORT endSize; /* Largest size to correct--in ppem */
602 USHORT deltaFormat; /* Format of DeltaValue array data: 1, 2, or 3
603 * 1 Signed 2-bit value, 8 values per uint16
604 * 2 Signed 4-bit value, 4 values per uint16
605 * 3 Signed 8-bit value, 2 values per uint16
607 USHORT deltaValue[VAR]; /* Array of compressed data */
609 ASSERT_SIZE_VAR (Device, 6, USHORT);
612 #endif /* HB_OT_LAYOUT_COMMON_PRIVATE_HH */