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 int cmp (hb_tag_t b) const {
56 return b < a ? -1 : b == a ? 0 : -1;
59 inline bool sanitize (hb_sanitize_context_t *c, void *base) {
61 return c->check_struct (this)
62 && offset.sanitize (c, base);
65 Tag tag; /* 4-byte Tag identifier */
67 offset; /* Offset from beginning of object holding
70 DEFINE_SIZE_STATIC (6);
73 template <typename Type>
74 struct RecordArrayOf : SortedArrayOf<Record<Type> > {
75 inline const Tag& get_tag (unsigned int i) const
77 if (unlikely (i >= this->len)) return Null(Tag);
78 return (*this)[i].tag;
80 inline unsigned int get_tags (unsigned int start_offset,
81 unsigned int *record_count /* IN/OUT */,
82 hb_tag_t *record_tags /* OUT */) const
85 const Record<Type> *array = this->sub_array (start_offset, record_count);
86 unsigned int count = *record_count;
87 for (unsigned int i = 0; i < count; i++)
88 record_tags[i] = array[i].tag;
92 inline bool find_index (hb_tag_t tag, unsigned int *index) const
94 int i = this->search (tag);
96 if (index) *index = i;
99 if (index) *index = Index::NOT_FOUND_INDEX;
105 template <typename Type>
106 struct RecordListOf : RecordArrayOf<Type>
108 inline const Type& operator [] (unsigned int i) const
109 { return this+RecordArrayOf<Type>::operator [](i).offset; }
111 inline bool sanitize (hb_sanitize_context_t *c) {
113 return RecordArrayOf<Type>::sanitize (c, this);
120 inline int cmp (hb_codepoint_t g) const {
121 hb_codepoint_t a = start, b = end;
122 return g < a ? -1 : g <= b ? 0 : +1 ;
125 inline bool sanitize (hb_sanitize_context_t *c) {
127 return c->check_struct (this);
130 GlyphID start; /* First GlyphID in the range */
131 GlyphID end; /* Last GlyphID in the range */
132 USHORT value; /* Value */
134 DEFINE_SIZE_STATIC (6);
136 DEFINE_NULL_DATA (RangeRecord, "\000\001");
139 struct IndexArray : ArrayOf<Index>
141 inline unsigned int get_indexes (unsigned int start_offset,
142 unsigned int *_count /* IN/OUT */,
143 unsigned int *_indexes /* OUT */) const
146 const USHORT *array = this->sub_array (start_offset, _count);
147 unsigned int count = *_count;
148 for (unsigned int i = 0; i < count; i++)
149 _indexes[i] = array[i];
163 inline unsigned int get_feature_count (void) const
164 { return featureIndex.len; }
165 inline hb_tag_t get_feature_index (unsigned int i) const
166 { return featureIndex[i]; }
167 inline unsigned int get_feature_indexes (unsigned int start_offset,
168 unsigned int *feature_count /* IN/OUT */,
169 unsigned int *feature_indexes /* OUT */) const
170 { return featureIndex.get_indexes (start_offset, feature_count, feature_indexes); }
172 inline bool has_required_feature (void) const { return reqFeatureIndex != 0xffff; }
173 inline unsigned int get_required_feature_index (void) const
175 if (reqFeatureIndex == 0xffff)
176 return Index::NOT_FOUND_INDEX;
177 return reqFeatureIndex;;
180 inline bool sanitize (hb_sanitize_context_t *c) {
182 return c->check_struct (this)
183 && featureIndex.sanitize (c);
186 Offset lookupOrder; /* = Null (reserved for an offset to a
187 * reordering table) */
188 USHORT reqFeatureIndex;/* Index of a feature required for this
189 * language system--if no required features
191 IndexArray featureIndex; /* Array of indices into the FeatureList */
193 DEFINE_SIZE_ARRAY (6, featureIndex);
195 DEFINE_NULL_DATA (LangSys, "\0\0\xFF\xFF");
200 inline unsigned int get_lang_sys_count (void) const
201 { return langSys.len; }
202 inline const Tag& get_lang_sys_tag (unsigned int i) const
203 { return langSys.get_tag (i); }
204 inline unsigned int get_lang_sys_tags (unsigned int start_offset,
205 unsigned int *lang_sys_count /* IN/OUT */,
206 hb_tag_t *lang_sys_tags /* OUT */) const
207 { return langSys.get_tags (start_offset, lang_sys_count, lang_sys_tags); }
208 inline const LangSys& get_lang_sys (unsigned int i) const
210 if (i == Index::NOT_FOUND_INDEX) return get_default_lang_sys ();
211 return this+langSys[i].offset;
213 inline bool find_lang_sys_index (hb_tag_t tag, unsigned int *index) const
214 { return langSys.find_index (tag, index); }
216 inline bool has_default_lang_sys (void) const { return defaultLangSys != 0; }
217 inline const LangSys& get_default_lang_sys (void) const { return this+defaultLangSys; }
219 inline bool sanitize (hb_sanitize_context_t *c) {
221 return defaultLangSys.sanitize (c, this)
222 && langSys.sanitize (c, this);
227 defaultLangSys; /* Offset to DefaultLangSys table--from
228 * beginning of Script table--may be Null */
229 RecordArrayOf<LangSys>
230 langSys; /* Array of LangSysRecords--listed
231 * alphabetically by LangSysTag */
233 DEFINE_SIZE_ARRAY (4, langSys);
236 typedef RecordListOf<Script> ScriptList;
241 inline unsigned int get_lookup_count (void) const
242 { return lookupIndex.len; }
243 inline hb_tag_t get_lookup_index (unsigned int i) const
244 { return lookupIndex[i]; }
245 inline unsigned int get_lookup_indexes (unsigned int start_index,
246 unsigned int *lookup_count /* IN/OUT */,
247 unsigned int *lookup_tags /* OUT */) const
248 { return lookupIndex.get_indexes (start_index, lookup_count, lookup_tags); }
250 inline bool sanitize (hb_sanitize_context_t *c) {
252 return c->check_struct (this)
253 && lookupIndex.sanitize (c);
256 /* LONGTERMTODO: implement get_feature_parameters() */
257 /* LONGTERMTODO: implement FeatureSize and other special features? */
258 Offset featureParams; /* Offset to Feature Parameters table (if one
259 * has been defined for the feature), relative
260 * to the beginning of the Feature Table; = Null
262 IndexArray lookupIndex; /* Array of LookupList indices */
264 DEFINE_SIZE_ARRAY (4, lookupIndex);
267 typedef RecordListOf<Feature> FeatureList;
270 struct LookupFlag : USHORT
273 RightToLeft = 0x0001u,
274 IgnoreBaseGlyphs = 0x0002u,
275 IgnoreLigatures = 0x0004u,
276 IgnoreMarks = 0x0008u,
277 IgnoreFlags = 0x000Eu,
278 UseMarkFilteringSet = 0x0010u,
280 MarkAttachmentType = 0xFF00u
283 DEFINE_SIZE_STATIC (2);
288 inline unsigned int get_subtable_count (void) const { return subTable.len; }
290 inline unsigned int get_type (void) const { return lookupType; }
291 inline unsigned int get_flag (void) const
293 unsigned int flag = lookupFlag;
294 if (unlikely (flag & LookupFlag::UseMarkFilteringSet))
296 const USHORT &markFilteringSet = StructAfter<USHORT> (subTable);
297 flag += (markFilteringSet << 16);
302 inline bool sanitize (hb_sanitize_context_t *c) {
304 /* Real sanitize of the subtables is done by GSUB/GPOS/... */
305 if (!(c->check_struct (this)
306 && subTable.sanitize (c))) return false;
307 if (unlikely (lookupFlag & LookupFlag::UseMarkFilteringSet))
309 USHORT &markFilteringSet = StructAfter<USHORT> (subTable);
310 if (!markFilteringSet.sanitize (c)) return false;
315 USHORT lookupType; /* Different enumerations for GSUB and GPOS */
316 USHORT lookupFlag; /* Lookup qualifiers */
318 subTable; /* Array of SubTables */
319 USHORT markFilteringSetX[VAR]; /* Index (base 0) into GDEF mark glyph sets
320 * structure. This field is only present if bit
321 * UseMarkFilteringSet of lookup flags is set. */
323 DEFINE_SIZE_ARRAY2 (6, subTable, markFilteringSetX);
326 typedef OffsetListOf<Lookup> LookupList;
333 struct CoverageFormat1
335 friend struct Coverage;
338 inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
340 int i = glyphArray.search (glyph_id);
346 inline bool sanitize (hb_sanitize_context_t *c) {
348 return glyphArray.sanitize (c);
352 USHORT coverageFormat; /* Format identifier--format = 1 */
353 SortedArrayOf<GlyphID>
354 glyphArray; /* Array of GlyphIDs--in numerical order */
356 DEFINE_SIZE_ARRAY (4, glyphArray);
359 struct CoverageFormat2
361 friend struct Coverage;
364 inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
366 int i = rangeRecord.search (glyph_id);
368 const RangeRecord &range = rangeRecord[i];
369 return (unsigned int) range.value + (glyph_id - range.start);
374 inline bool sanitize (hb_sanitize_context_t *c) {
376 return rangeRecord.sanitize (c);
380 USHORT coverageFormat; /* Format identifier--format = 2 */
381 SortedArrayOf<RangeRecord>
382 rangeRecord; /* Array of glyph ranges--ordered by
383 * Start GlyphID. rangeCount entries
386 DEFINE_SIZE_ARRAY (4, rangeRecord);
391 inline unsigned int operator () (hb_codepoint_t glyph_id) const { return get_coverage (glyph_id); }
393 inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
396 case 1: return u.format1.get_coverage(glyph_id);
397 case 2: return u.format2.get_coverage(glyph_id);
398 default:return NOT_COVERED;
402 inline bool sanitize (hb_sanitize_context_t *c) {
404 if (!u.format.sanitize (c)) return false;
406 case 1: return u.format1.sanitize (c);
407 case 2: return u.format2.sanitize (c);
414 USHORT format; /* Format identifier */
415 CoverageFormat1 format1;
416 CoverageFormat2 format2;
419 DEFINE_SIZE_UNION (2, format);
424 * Class Definition Table
427 struct ClassDefFormat1
429 friend struct ClassDef;
432 inline hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
434 if ((unsigned int) (glyph_id - startGlyph) < classValue.len)
435 return classValue[glyph_id - startGlyph];
439 inline bool sanitize (hb_sanitize_context_t *c) {
441 return c->check_struct (this)
442 && classValue.sanitize (c);
445 USHORT classFormat; /* Format identifier--format = 1 */
446 GlyphID startGlyph; /* First GlyphID of the classValueArray */
448 classValue; /* Array of Class Values--one per GlyphID */
450 DEFINE_SIZE_ARRAY (6, classValue);
453 struct ClassDefFormat2
455 friend struct ClassDef;
458 inline hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
460 int i = rangeRecord.search (glyph_id);
462 return rangeRecord[i].value;
466 inline bool sanitize (hb_sanitize_context_t *c) {
468 return rangeRecord.sanitize (c);
471 USHORT classFormat; /* Format identifier--format = 2 */
472 SortedArrayOf<RangeRecord>
473 rangeRecord; /* Array of glyph ranges--ordered by
476 DEFINE_SIZE_ARRAY (4, rangeRecord);
481 inline hb_ot_layout_class_t operator () (hb_codepoint_t glyph_id) const { return get_class (glyph_id); }
483 inline hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
486 case 1: return u.format1.get_class(glyph_id);
487 case 2: return u.format2.get_class(glyph_id);
492 inline bool sanitize (hb_sanitize_context_t *c) {
494 if (!u.format.sanitize (c)) return false;
496 case 1: return u.format1.sanitize (c);
497 case 2: return u.format2.sanitize (c);
504 USHORT format; /* Format identifier */
505 ClassDefFormat1 format1;
506 ClassDefFormat2 format2;
509 DEFINE_SIZE_UNION (2, format);
521 inline hb_position_t get_x_delta (hb_ot_layout_context_t *c) const
522 { return c->font->x_ppem ? get_delta (c->font->x_ppem) * (uint64_t) c->font->x_scale / c->font->x_ppem : 0; }
524 inline hb_position_t get_y_delta (hb_ot_layout_context_t *c) const
525 { return c->font->y_ppem ? get_delta (c->font->y_ppem) * (uint64_t) c->font->y_scale / c->font->y_ppem : 0; }
527 inline int get_delta (unsigned int ppem_size) const
529 unsigned int f = deltaFormat;
530 if (unlikely (f < 1 || f > 3))
533 if (ppem_size < startSize || ppem_size > endSize)
536 unsigned int s = ppem_size - startSize;
538 unsigned int byte = deltaValue[s >> (4 - f)];
539 unsigned int bits = (byte >> (16 - (((s & ((1 << (4 - f)) - 1)) + 1) << f)));
540 unsigned int mask = (0xFFFF >> (16 - (1 << f)));
542 int delta = bits & mask;
544 if ((unsigned int) delta >= ((mask + 1) >> 1))
550 inline unsigned int get_size () const
552 unsigned int f = deltaFormat;
553 if (unlikely (f < 1 || f > 3 || startSize > endSize)) return 3 * USHORT::static_size;
554 return USHORT::static_size * (4 + ((endSize - startSize) >> (4 - f)));
557 inline bool sanitize (hb_sanitize_context_t *c) {
559 return c->check_struct (this)
560 && c->check_range (this, this->get_size ());
564 USHORT startSize; /* Smallest size to correct--in ppem */
565 USHORT endSize; /* Largest size to correct--in ppem */
566 USHORT deltaFormat; /* Format of DeltaValue array data: 1, 2, or 3
567 * 1 Signed 2-bit value, 8 values per uint16
568 * 2 Signed 4-bit value, 4 values per uint16
569 * 3 Signed 8-bit value, 2 values per uint16
571 USHORT deltaValue[VAR]; /* Array of compressed data */
573 DEFINE_SIZE_ARRAY (6, deltaValue);
577 #endif /* HB_OT_LAYOUT_COMMON_PRIVATE_HH */