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.hh"
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 *c, void *base) {
56 return c->check_struct (this)
57 && offset.sanitize (c, 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 = Index::NOT_FOUND_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 *c) {
115 return RecordArrayOf<Type>::sanitize (c, this);
120 struct IndexArray : ArrayOf<Index>
122 inline unsigned int get_indexes (unsigned int start_offset,
123 unsigned int *_count /* IN/OUT */,
124 unsigned int *_indexes /* OUT */) const
127 const USHORT *array = this->sub_array (start_offset, _count);
128 unsigned int count = *_count;
129 for (unsigned int i = 0; i < count; i++)
130 _indexes[i] = array[i];
144 inline unsigned int get_feature_count (void) const
145 { return featureIndex.len; }
146 inline hb_tag_t get_feature_index (unsigned int i) const
147 { return featureIndex[i]; }
148 inline unsigned int get_feature_indexes (unsigned int start_offset,
149 unsigned int *feature_count /* IN/OUT */,
150 unsigned int *feature_indexes /* OUT */) const
151 { return featureIndex.get_indexes (start_offset, feature_count, feature_indexes); }
153 inline bool has_required_feature (void) const { return reqFeatureIndex != 0xffff; }
154 inline unsigned int get_required_feature_index (void) const
156 if (reqFeatureIndex == 0xffff)
157 return Index::NOT_FOUND_INDEX;
158 return reqFeatureIndex;;
161 inline bool sanitize (hb_sanitize_context_t *c) {
163 return c->check_struct (this)
164 && featureIndex.sanitize (c);
167 Offset lookupOrder; /* = Null (reserved for an offset to a
168 * reordering table) */
169 USHORT reqFeatureIndex;/* Index of a feature required for this
170 * language system--if no required features
172 IndexArray featureIndex; /* Array of indices into the FeatureList */
174 DEFINE_SIZE_ARRAY (6, featureIndex);
176 DEFINE_NULL_DATA (LangSys, "\0\0\xFF\xFF");
181 inline unsigned int get_lang_sys_count (void) const
182 { return langSys.len; }
183 inline const Tag& get_lang_sys_tag (unsigned int i) const
184 { return langSys.get_tag (i); }
185 inline unsigned int get_lang_sys_tags (unsigned int start_offset,
186 unsigned int *lang_sys_count /* IN/OUT */,
187 hb_tag_t *lang_sys_tags /* OUT */) const
188 { return langSys.get_tags (start_offset, lang_sys_count, lang_sys_tags); }
189 inline const LangSys& get_lang_sys (unsigned int i) const
191 if (i == Index::NOT_FOUND_INDEX) return get_default_lang_sys ();
192 return this+langSys[i].offset;
194 inline bool find_lang_sys_index (hb_tag_t tag, unsigned int *index) const
195 { return langSys.find_index (tag, index); }
197 inline bool has_default_lang_sys (void) const { return defaultLangSys != 0; }
198 inline const LangSys& get_default_lang_sys (void) const { return this+defaultLangSys; }
200 inline bool sanitize (hb_sanitize_context_t *c) {
202 return defaultLangSys.sanitize (c, this)
203 && langSys.sanitize (c, this);
208 defaultLangSys; /* Offset to DefaultLangSys table--from
209 * beginning of Script table--may be Null */
210 RecordArrayOf<LangSys>
211 langSys; /* Array of LangSysRecords--listed
212 * alphabetically by LangSysTag */
214 DEFINE_SIZE_ARRAY (4, langSys);
217 typedef RecordListOf<Script> ScriptList;
222 inline unsigned int get_lookup_count (void) const
223 { return lookupIndex.len; }
224 inline hb_tag_t get_lookup_index (unsigned int i) const
225 { return lookupIndex[i]; }
226 inline unsigned int get_lookup_indexes (unsigned int start_index,
227 unsigned int *lookup_count /* IN/OUT */,
228 unsigned int *lookup_tags /* OUT */) const
229 { return lookupIndex.get_indexes (start_index, lookup_count, lookup_tags); }
231 inline bool sanitize (hb_sanitize_context_t *c) {
233 return c->check_struct (this)
234 && lookupIndex.sanitize (c);
237 /* LONGTERMTODO: implement get_feature_parameters() */
238 /* LONGTERMTODO: implement FeatureSize and other special features? */
239 Offset featureParams; /* Offset to Feature Parameters table (if one
240 * has been defined for the feature), relative
241 * to the beginning of the Feature Table; = Null
243 IndexArray lookupIndex; /* Array of LookupList indices */
245 DEFINE_SIZE_ARRAY (4, lookupIndex);
248 typedef RecordListOf<Feature> FeatureList;
251 struct LookupFlag : USHORT
254 RightToLeft = 0x0001u,
255 IgnoreBaseGlyphs = 0x0002u,
256 IgnoreLigatures = 0x0004u,
257 IgnoreMarks = 0x0008u,
258 IgnoreFlags = 0x000Eu,
259 UseMarkFilteringSet = 0x0010u,
261 MarkAttachmentType = 0xFF00u
264 DEFINE_SIZE_STATIC (2);
269 inline unsigned int get_subtable_count (void) const { return subTable.len; }
271 inline unsigned int get_type (void) const { return lookupType; }
272 inline unsigned int get_flag (void) const
274 unsigned int flag = lookupFlag;
275 if (unlikely (flag & LookupFlag::UseMarkFilteringSet))
277 const USHORT &markFilteringSet = StructAfter<USHORT> (subTable);
278 flag += (markFilteringSet << 16);
283 inline bool sanitize (hb_sanitize_context_t *c) {
285 /* Real sanitize of the subtables is done by GSUB/GPOS/... */
286 if (!(c->check_struct (this)
287 && subTable.sanitize (c))) return false;
288 if (unlikely (lookupFlag & LookupFlag::UseMarkFilteringSet))
290 USHORT &markFilteringSet = StructAfter<USHORT> (subTable);
291 if (!markFilteringSet.sanitize (c)) 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 DEFINE_SIZE_ARRAY2 (6, subTable, markFilteringSetX);
307 typedef OffsetListOf<Lookup> LookupList;
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 (hb_sanitize_context_t *c) {
335 return glyphArray.sanitize (c);
339 USHORT coverageFormat; /* Format identifier--format = 1 */
341 glyphArray; /* Array of GlyphIDs--in numerical order */
343 DEFINE_SIZE_ARRAY (4, glyphArray);
346 struct CoverageRangeRecord
348 friend struct CoverageFormat2;
351 inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
353 if (glyph_id >= start && glyph_id <= end)
354 return (unsigned int) startCoverageIndex + (glyph_id - start);
359 inline bool sanitize (hb_sanitize_context_t *c) {
361 return c->check_struct (this);
365 GlyphID start; /* First GlyphID in the range */
366 GlyphID end; /* Last GlyphID in the range */
367 USHORT startCoverageIndex; /* Coverage Index of first GlyphID in
370 DEFINE_SIZE_STATIC (6);
372 DEFINE_NULL_DATA (CoverageRangeRecord, "\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 (hb_sanitize_context_t *c) {
394 return rangeRecord.sanitize (c);
398 USHORT coverageFormat; /* Format identifier--format = 2 */
399 ArrayOf<CoverageRangeRecord>
400 rangeRecord; /* Array of glyph ranges--ordered by
401 * Start GlyphID. rangeCount entries
404 DEFINE_SIZE_ARRAY (4, rangeRecord);
409 inline unsigned int operator () (hb_codepoint_t glyph_id) const { return get_coverage (glyph_id); }
411 inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
414 case 1: return u.format1.get_coverage(glyph_id);
415 case 2: return u.format2.get_coverage(glyph_id);
416 default:return NOT_COVERED;
420 inline bool sanitize (hb_sanitize_context_t *c) {
422 if (!u.format.sanitize (c)) return false;
424 case 1: return u.format1.sanitize (c);
425 case 2: return u.format2.sanitize (c);
432 USHORT format; /* Format identifier */
433 CoverageFormat1 format1;
434 CoverageFormat2 format2;
437 DEFINE_SIZE_UNION (2, format);
442 * Class Definition Table
445 struct ClassDefFormat1
447 friend struct ClassDef;
450 inline hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
452 if ((unsigned int) (glyph_id - startGlyph) < classValue.len)
453 return classValue[glyph_id - startGlyph];
457 inline bool sanitize (hb_sanitize_context_t *c) {
459 return c->check_struct (this)
460 && classValue.sanitize (c);
463 USHORT classFormat; /* Format identifier--format = 1 */
464 GlyphID startGlyph; /* First GlyphID of the classValueArray */
466 classValue; /* Array of Class Values--one per GlyphID */
468 DEFINE_SIZE_ARRAY (6, classValue);
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 *c) {
486 return c->check_struct (this);
490 GlyphID start; /* First GlyphID in the range */
491 GlyphID end; /* Last GlyphID in the range */
492 USHORT classValue; /* Applied to all glyphs in the range */
494 DEFINE_SIZE_STATIC (6);
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 *c) {
518 return rangeRecord.sanitize (c);
521 USHORT classFormat; /* Format identifier--format = 2 */
522 ArrayOf<ClassRangeRecord>
523 rangeRecord; /* Array of glyph ranges--ordered by
526 DEFINE_SIZE_ARRAY (4, rangeRecord);
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 *c) {
544 if (!u.format.sanitize (c)) return false;
546 case 1: return u.format1.sanitize (c);
547 case 2: return u.format2.sanitize (c);
554 USHORT format; /* Format identifier */
555 ClassDefFormat1 format1;
556 ClassDefFormat2 format2;
559 DEFINE_SIZE_UNION (2, format);
571 inline hb_position_t get_x_delta (hb_ot_layout_context_t *c) const
572 { return c->font->x_ppem ? get_delta (c->font->x_ppem) * (uint64_t) c->font->x_scale / c->font->x_ppem : 0; }
574 inline hb_position_t get_y_delta (hb_ot_layout_context_t *c) const
575 { return c->font->y_ppem ? get_delta (c->font->y_ppem) * (uint64_t) c->font->y_scale / c->font->y_ppem : 0; }
577 inline int get_delta (unsigned int ppem_size) const
579 unsigned int f = deltaFormat;
580 if (unlikely (f < 1 || f > 3))
583 if (ppem_size < startSize || ppem_size > endSize)
586 unsigned int s = ppem_size - startSize;
588 unsigned int byte = deltaValue[s >> (4 - f)];
589 unsigned int bits = (byte >> (16 - (((s & ((1 << (4 - f)) - 1)) + 1) << f)));
590 unsigned int mask = (0xFFFF >> (16 - (1 << f)));
592 int delta = bits & mask;
594 if ((unsigned int) delta >= ((mask + 1) >> 1))
600 inline unsigned int get_size () const
602 unsigned int f = deltaFormat;
603 if (unlikely (f < 1 || f > 3 || startSize > endSize)) return 3 * USHORT::static_size;
604 return USHORT::static_size * (4 + ((endSize - startSize) >> (4 - f)));
607 inline bool sanitize (hb_sanitize_context_t *c) {
609 return c->check_struct (this)
610 && c->check_range (this, this->get_size ());
614 USHORT startSize; /* Smallest size to correct--in ppem */
615 USHORT endSize; /* Largest size to correct--in ppem */
616 USHORT deltaFormat; /* Format of DeltaValue array data: 1, 2, or 3
617 * 1 Signed 2-bit value, 8 values per uint16
618 * 2 Signed 4-bit value, 4 values per uint16
619 * 3 Signed 8-bit value, 2 values per uint16
621 USHORT deltaValue[VAR]; /* Array of compressed data */
623 DEFINE_SIZE_ARRAY (6, deltaValue);
627 #endif /* HB_OT_LAYOUT_COMMON_PRIVATE_HH */