2 * Copyright (C) 2007,2008,2009 Red Hat, Inc.
4 * This is part of HarfBuzz, an OpenType Layout engine 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 (SANITIZE_ARG_DEF, const void *base) {
56 return SANITIZE (tag) && SANITIZE_BASE (offset, base);
59 Tag tag; /* 4-byte Tag identifier */
61 offset; /* Offset from beginning of object holding
65 template <typename Type>
66 struct RecordArrayOf : ArrayOf<Record<Type> > {
67 inline const Tag& get_tag (unsigned int i) const
69 if (HB_UNLIKELY (i >= this->len)) return Null(Tag);
70 return this->array[i].tag;
72 inline bool get_tags (unsigned int *record_count /* IN/OUT */,
73 hb_tag_t *record_tags /* OUT */) const
75 unsigned int count = MIN (this->len, *record_count);
76 for (unsigned int i = 0; i < count; i++)
77 record_tags[i] = this->array[i].tag;
79 *record_count = this->len;
82 inline bool find_index (hb_tag_t tag, unsigned int *index) const
86 unsigned int count = this->len;
87 for (unsigned int i = 0; i < count; i++)
89 if (t == this->array[i].tag)
91 if (index) *index = i;
95 if (index) *index = NO_INDEX;
100 template <typename Type>
101 struct RecordListOf : RecordArrayOf<Type>
103 inline const Type& operator [] (unsigned int i) const
104 { return this+RecordArrayOf<Type>::operator [](i).offset; }
106 inline bool sanitize (SANITIZE_ARG_DEF) {
108 return RecordArrayOf<Type>::sanitize (SANITIZE_ARG, CONST_CHARP(this));
113 struct IndexArray : ArrayOf<USHORT>
115 inline unsigned int operator [] (unsigned int i) const
117 if (HB_UNLIKELY (i >= this->len))
119 return this->array[i];
121 inline bool get_indexes (unsigned int *_count /* IN/OUT */,
122 unsigned int *_indexes /* OUT */) const
124 unsigned int count = MIN (this->len, *_count);
125 for (unsigned int i = 0; i < count; i++)
126 _indexes[i] = this->array[i];
141 inline unsigned int get_feature_count (void) const
142 { return featureIndex.len; }
143 inline hb_tag_t get_feature_index (unsigned int i) const
144 { return featureIndex[i]; }
145 inline bool get_feature_indexes (unsigned int *feature_count /* IN/OUT */,
146 unsigned int *feature_indexes /* OUT */) const
147 { return featureIndex.get_indexes (feature_count, feature_indexes); }
149 inline bool has_required_feature (void) const { return reqFeatureIndex != 0xffff; }
150 inline int get_required_feature_index (void) const
152 if (reqFeatureIndex == 0xffff)
154 return reqFeatureIndex;;
157 inline bool sanitize (SANITIZE_ARG_DEF) {
159 return SANITIZE_SELF () && SANITIZE (featureIndex);
162 Offset lookupOrder; /* = Null (reserved for an offset to a
163 * reordering table) */
164 USHORT reqFeatureIndex;/* Index of a feature required for this
165 * language system--if no required features
167 IndexArray featureIndex; /* Array of indices into the FeatureList */
169 ASSERT_SIZE_DATA (LangSys, 6, "\0\0\xFF\xFF");
174 inline unsigned int get_lang_sys_count (void) const
175 { return langSys.len; }
176 inline const Tag& get_lang_sys_tag (unsigned int i) const
177 { return langSys.get_tag (i); }
178 inline bool get_lang_sys_tags (unsigned int *lang_sys_count /* IN/OUT */,
179 hb_tag_t *lang_sys_tags /* OUT */) const
180 { return langSys.get_tags (lang_sys_count, lang_sys_tags); }
181 inline const LangSys& get_lang_sys (unsigned int i) const
183 if (i == NO_INDEX) return get_default_lang_sys ();
184 return this+langSys[i].offset;
186 inline bool find_lang_sys_index (hb_tag_t tag, unsigned int *index) const
187 { return langSys.find_index (tag, index); }
189 inline bool has_default_lang_sys (void) const { return defaultLangSys != 0; }
190 inline const LangSys& get_default_lang_sys (void) const { return this+defaultLangSys; }
192 inline bool sanitize (SANITIZE_ARG_DEF) {
194 return SANITIZE_THIS (defaultLangSys) && SANITIZE_THIS (langSys);
199 defaultLangSys; /* Offset to DefaultLangSys table--from
200 * beginning of Script table--may be Null */
201 RecordArrayOf<LangSys>
202 langSys; /* Array of LangSysRecords--listed
203 * alphabetically by LangSysTag */
205 ASSERT_SIZE (Script, 4);
207 typedef RecordListOf<Script> ScriptList;
208 ASSERT_SIZE (ScriptList, 2);
213 inline unsigned int get_lookup_count (void) const
214 { return lookupIndex.len; }
215 inline hb_tag_t get_lookup_index (unsigned int i) const
216 { return lookupIndex[i]; }
217 inline bool get_lookup_indexes (unsigned int *lookup_count /* IN/OUT */,
218 unsigned int *lookup_tags /* OUT */) const
219 { return lookupIndex.get_indexes (lookup_count, lookup_tags); }
221 inline bool sanitize (SANITIZE_ARG_DEF) {
223 return SANITIZE_SELF () && SANITIZE (lookupIndex);
226 /* TODO: implement get_feature_parameters() */
227 /* TODO: implement FeatureSize and other special features? */
228 Offset featureParams; /* Offset to Feature Parameters table (if one
229 * has been defined for the feature), relative
230 * to the beginning of the Feature Table; = Null
232 IndexArray lookupIndex; /* Array of LookupList indices */
234 ASSERT_SIZE (Feature, 4);
236 typedef RecordListOf<Feature> FeatureList;
237 ASSERT_SIZE (FeatureList, 2);
240 struct LookupFlag : USHORT
243 RightToLeft = 0x0001u,
244 IgnoreBaseGlyphs = 0x0002u,
245 IgnoreLigatures = 0x0004u,
246 IgnoreMarks = 0x0008u,
247 IgnoreFlags = 0x000Eu,
248 UseMarkFilteringSet = 0x0010u,
250 MarkAttachmentType = 0xFF00u
253 ASSERT_SIZE (LookupFlag, 2);
255 struct LookupSubTable
257 inline bool sanitize (SANITIZE_ARG_DEF) {
259 return SANITIZE_SELF ();
263 USHORT format; /* Subtable format. Different for GSUB and GPOS */
265 ASSERT_SIZE (LookupSubTable, 2);
269 inline const LookupSubTable& get_subtable (unsigned int i) const { return this+subTable[i]; }
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 (HB_UNLIKELY (flag & LookupFlag::UseMarkFilteringSet))
278 const USHORT &markFilteringSet = CONST_NEXT (USHORT, subTable);
279 flag += (markFilteringSet << 16);
284 inline bool sanitize (SANITIZE_ARG_DEF) {
286 if (!(SANITIZE_SELF () && SANITIZE_THIS (subTable))) return false;
287 if (HB_UNLIKELY (lookupFlag & LookupFlag::UseMarkFilteringSet))
289 USHORT &markFilteringSet = NEXT (USHORT, subTable);
290 if (!SANITIZE (markFilteringSet)) return false;
295 USHORT lookupType; /* Different enumerations for GSUB and GPOS */
296 USHORT lookupFlag; /* Lookup qualifiers */
297 OffsetArrayOf<LookupSubTable>
298 subTable; /* Array of SubTables */
299 USHORT markFilteringSetX[0]; /* Index (base 0) into GDEF mark glyph sets
300 * structure. This field is only present if bit
301 * UseMarkFilteringSet of lookup flags is set. */
303 ASSERT_SIZE (Lookup, 6);
305 typedef OffsetListOf<Lookup> LookupList;
306 ASSERT_SIZE (LookupList, 2);
313 struct CoverageFormat1
315 friend struct Coverage;
318 inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
320 if (HB_UNLIKELY (glyph_id > 0xFFFF))
325 unsigned int num_glyphs = glyphArray.len;
326 for (unsigned int i = 0; i < num_glyphs; i++)
327 if (gid == glyphArray[i])
332 inline bool sanitize (SANITIZE_ARG_DEF) {
334 return SANITIZE (glyphArray);
338 USHORT coverageFormat; /* Format identifier--format = 1 */
340 glyphArray; /* Array of GlyphIDs--in numerical order */
342 ASSERT_SIZE (CoverageFormat1, 4);
344 struct CoverageRangeRecord
346 friend struct CoverageFormat2;
349 inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
351 if (glyph_id >= start && glyph_id <= end)
352 return (unsigned int) startCoverageIndex + (glyph_id - start);
357 inline bool sanitize (SANITIZE_ARG_DEF) {
359 return SANITIZE_SELF ();
363 GlyphID start; /* First GlyphID in the range */
364 GlyphID end; /* Last GlyphID in the range */
365 USHORT startCoverageIndex; /* Coverage Index of first GlyphID in
368 ASSERT_SIZE_DATA (CoverageRangeRecord, 6, "\000\001");
370 struct CoverageFormat2
372 friend struct Coverage;
375 inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
378 unsigned int count = rangeRecord.len;
379 for (unsigned int i = 0; i < count; i++)
381 unsigned int coverage = rangeRecord[i].get_coverage (glyph_id);
382 if (coverage != NOT_COVERED)
388 inline bool sanitize (SANITIZE_ARG_DEF) {
390 return SANITIZE (rangeRecord);
394 USHORT coverageFormat; /* Format identifier--format = 2 */
395 ArrayOf<CoverageRangeRecord>
396 rangeRecord; /* Array of glyph ranges--ordered by
397 * Start GlyphID. rangeCount entries
400 ASSERT_SIZE (CoverageFormat2, 4);
404 inline unsigned int operator() (hb_codepoint_t glyph_id) const { return get_coverage (glyph_id); }
406 inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
409 case 1: return u.format1->get_coverage(glyph_id);
410 case 2: return u.format2->get_coverage(glyph_id);
411 default:return NOT_COVERED;
415 inline bool sanitize (SANITIZE_ARG_DEF) {
417 if (!SANITIZE (u.format)) return false;
419 case 1: return u.format1->sanitize (SANITIZE_ARG);
420 case 2: return u.format2->sanitize (SANITIZE_ARG);
427 USHORT format; /* Format identifier */
428 CoverageFormat1 format1[];
429 CoverageFormat2 format2[];
432 ASSERT_SIZE (Coverage, 2);
436 * Class Definition Table
439 struct ClassDefFormat1
441 friend struct ClassDef;
444 inline hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
446 if ((unsigned int) (glyph_id - startGlyph) < classValue.len)
447 return classValue[glyph_id - startGlyph];
451 inline bool sanitize (SANITIZE_ARG_DEF) {
453 return SANITIZE_SELF () && SANITIZE (classValue);
456 USHORT classFormat; /* Format identifier--format = 1 */
457 GlyphID startGlyph; /* First GlyphID of the classValueArray */
459 classValue; /* Array of Class Values--one per GlyphID */
461 ASSERT_SIZE (ClassDefFormat1, 6);
463 struct ClassRangeRecord
465 friend struct ClassDefFormat2;
468 inline hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
470 if (glyph_id >= start && glyph_id <= end)
476 inline bool sanitize (SANITIZE_ARG_DEF) {
478 return SANITIZE_SELF ();
482 GlyphID start; /* First GlyphID in the range */
483 GlyphID end; /* Last GlyphID in the range */
484 USHORT classValue; /* Applied to all glyphs in the range */
486 ASSERT_SIZE_DATA (ClassRangeRecord, 6, "\000\001");
488 struct ClassDefFormat2
490 friend struct ClassDef;
493 inline hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
496 unsigned int count = rangeRecord.len;
497 for (unsigned int i = 0; i < count; i++)
499 int classValue = rangeRecord[i].get_class (glyph_id);
506 inline bool sanitize (SANITIZE_ARG_DEF) {
508 return SANITIZE (rangeRecord);
511 USHORT classFormat; /* Format identifier--format = 2 */
512 ArrayOf<ClassRangeRecord>
513 rangeRecord; /* Array of glyph ranges--ordered by
516 ASSERT_SIZE (ClassDefFormat2, 4);
520 inline unsigned int operator() (hb_codepoint_t glyph_id) const { return get_class (glyph_id); }
522 inline hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
525 case 1: return u.format1->get_class(glyph_id);
526 case 2: return u.format2->get_class(glyph_id);
531 inline bool sanitize (SANITIZE_ARG_DEF) {
533 if (!SANITIZE (u.format)) return false;
535 case 1: return u.format1->sanitize (SANITIZE_ARG);
536 case 2: return u.format2->sanitize (SANITIZE_ARG);
543 USHORT format; /* Format identifier */
544 ClassDefFormat1 format1[];
545 ClassDefFormat2 format2[];
548 ASSERT_SIZE (ClassDef, 2);
557 inline int operator() (unsigned int ppem_size) const { return get_delta (ppem_size); }
559 inline int get_delta (unsigned int ppem_size) const
561 unsigned int f = deltaFormat;
562 if (HB_UNLIKELY (f < 1 || f > 3))
565 if (ppem_size < startSize || ppem_size > endSize)
568 unsigned int s = ppem_size - startSize;
570 unsigned int byte = deltaValue[s >> (4 - f)];
571 unsigned int bits = (byte >> (16 - (((s & ((1 << (4 - f)) - 1)) + 1) << f)));
572 unsigned int mask = (0xFFFF >> (16 - (1 << f)));
574 int delta = bits & mask;
576 if ((unsigned int) delta >= ((mask + 1) >> 1))
582 inline unsigned int get_size () const
584 unsigned int f = deltaFormat;
585 if (HB_UNLIKELY (f < 1 || f > 3 || startSize > endSize)) return sizeof (*this);
586 return sizeof (*this) + ((endSize - startSize + (1 << (4 - f)) - 1) >> (4 - f));
589 inline bool sanitize (SANITIZE_ARG_DEF) {
591 return SANITIZE_GET_SIZE ();
595 USHORT startSize; /* Smallest size to correct--in ppem */
596 USHORT endSize; /* Largest size to correct--in ppem */
597 USHORT deltaFormat; /* Format of DeltaValue array data: 1, 2, or 3 */
598 USHORT deltaValue[]; /* Array of compressed data */
600 ASSERT_SIZE (Device, 6);
603 #endif /* HB_OT_LAYOUT_COMMON_PRIVATE_HH */