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 (const Record &other) const {
55 return cmp (other.tag);
57 inline int cmp (hb_tag_t b) const {
59 return b < a ? -1 : b == a ? 0 : -1;
62 inline bool sanitize (hb_sanitize_context_t *c, void *base) {
64 return c->check_struct (this)
65 && offset.sanitize (c, base);
68 Tag tag; /* 4-byte Tag identifier */
70 offset; /* Offset from beginning of object holding
73 DEFINE_SIZE_STATIC (6);
76 template <typename Type>
77 struct RecordArrayOf : SortedArrayOf<Record<Type> > {
78 inline const Tag& get_tag (unsigned int i) const
80 if (unlikely (i >= this->len)) return Null(Tag);
81 return (*this)[i].tag;
83 inline unsigned int get_tags (unsigned int start_offset,
84 unsigned int *record_count /* IN/OUT */,
85 hb_tag_t *record_tags /* OUT */) const
88 const Record<Type> *array = this->sub_array (start_offset, record_count);
89 unsigned int count = *record_count;
90 for (unsigned int i = 0; i < count; i++)
91 record_tags[i] = array[i].tag;
95 inline bool find_index (hb_tag_t tag, unsigned int *index) const
97 int i = this->search (tag);
99 if (index) *index = i;
102 if (index) *index = Index::NOT_FOUND_INDEX;
108 template <typename Type>
109 struct RecordListOf : RecordArrayOf<Type>
111 inline const Type& operator [] (unsigned int i) const
112 { return this+RecordArrayOf<Type>::operator [](i).offset; }
114 inline bool sanitize (hb_sanitize_context_t *c) {
116 return RecordArrayOf<Type>::sanitize (c, this);
123 inline int cmp (const RangeRecord &other) const {
124 hb_codepoint_t a = start, b = other.start;
125 return b < a ? -1 : b == a ? 0 : +1;
127 inline int cmp (hb_codepoint_t g) const {
128 hb_codepoint_t a = start, b = end;
129 return g < a ? -1 : g <= b ? 0 : +1 ;
132 inline bool sanitize (hb_sanitize_context_t *c) {
134 return c->check_struct (this);
137 GlyphID start; /* First GlyphID in the range */
138 GlyphID end; /* Last GlyphID in the range */
139 USHORT value; /* Value */
141 DEFINE_SIZE_STATIC (6);
143 DEFINE_NULL_DATA (RangeRecord, "\000\001");
146 struct IndexArray : ArrayOf<Index>
148 inline unsigned int get_indexes (unsigned int start_offset,
149 unsigned int *_count /* IN/OUT */,
150 unsigned int *_indexes /* OUT */) const
153 const USHORT *array = this->sub_array (start_offset, _count);
154 unsigned int count = *_count;
155 for (unsigned int i = 0; i < count; i++)
156 _indexes[i] = array[i];
170 inline unsigned int get_feature_count (void) const
171 { return featureIndex.len; }
172 inline hb_tag_t get_feature_index (unsigned int i) const
173 { return featureIndex[i]; }
174 inline unsigned int get_feature_indexes (unsigned int start_offset,
175 unsigned int *feature_count /* IN/OUT */,
176 unsigned int *feature_indexes /* OUT */) const
177 { return featureIndex.get_indexes (start_offset, feature_count, feature_indexes); }
179 inline bool has_required_feature (void) const { return reqFeatureIndex != 0xffff; }
180 inline unsigned int get_required_feature_index (void) const
182 if (reqFeatureIndex == 0xffff)
183 return Index::NOT_FOUND_INDEX;
184 return reqFeatureIndex;;
187 inline bool sanitize (hb_sanitize_context_t *c) {
189 return c->check_struct (this)
190 && featureIndex.sanitize (c);
193 Offset lookupOrder; /* = Null (reserved for an offset to a
194 * reordering table) */
195 USHORT reqFeatureIndex;/* Index of a feature required for this
196 * language system--if no required features
198 IndexArray featureIndex; /* Array of indices into the FeatureList */
200 DEFINE_SIZE_ARRAY (6, featureIndex);
202 DEFINE_NULL_DATA (LangSys, "\0\0\xFF\xFF");
207 inline unsigned int get_lang_sys_count (void) const
208 { return langSys.len; }
209 inline const Tag& get_lang_sys_tag (unsigned int i) const
210 { return langSys.get_tag (i); }
211 inline unsigned int get_lang_sys_tags (unsigned int start_offset,
212 unsigned int *lang_sys_count /* IN/OUT */,
213 hb_tag_t *lang_sys_tags /* OUT */) const
214 { return langSys.get_tags (start_offset, lang_sys_count, lang_sys_tags); }
215 inline const LangSys& get_lang_sys (unsigned int i) const
217 if (i == Index::NOT_FOUND_INDEX) return get_default_lang_sys ();
218 return this+langSys[i].offset;
220 inline bool find_lang_sys_index (hb_tag_t tag, unsigned int *index) const
221 { return langSys.find_index (tag, index); }
223 inline bool has_default_lang_sys (void) const { return defaultLangSys != 0; }
224 inline const LangSys& get_default_lang_sys (void) const { return this+defaultLangSys; }
226 inline bool sanitize (hb_sanitize_context_t *c) {
228 return defaultLangSys.sanitize (c, this)
229 && langSys.sanitize (c, this);
234 defaultLangSys; /* Offset to DefaultLangSys table--from
235 * beginning of Script table--may be Null */
236 RecordArrayOf<LangSys>
237 langSys; /* Array of LangSysRecords--listed
238 * alphabetically by LangSysTag */
240 DEFINE_SIZE_ARRAY (4, langSys);
243 typedef RecordListOf<Script> ScriptList;
248 inline unsigned int get_lookup_count (void) const
249 { return lookupIndex.len; }
250 inline hb_tag_t get_lookup_index (unsigned int i) const
251 { return lookupIndex[i]; }
252 inline unsigned int get_lookup_indexes (unsigned int start_index,
253 unsigned int *lookup_count /* IN/OUT */,
254 unsigned int *lookup_tags /* OUT */) const
255 { return lookupIndex.get_indexes (start_index, lookup_count, lookup_tags); }
257 inline bool sanitize (hb_sanitize_context_t *c) {
259 return c->check_struct (this)
260 && lookupIndex.sanitize (c);
263 /* LONGTERMTODO: implement get_feature_parameters() */
264 /* LONGTERMTODO: implement FeatureSize and other special features? */
265 Offset featureParams; /* Offset to Feature Parameters table (if one
266 * has been defined for the feature), relative
267 * to the beginning of the Feature Table; = Null
269 IndexArray lookupIndex; /* Array of LookupList indices */
271 DEFINE_SIZE_ARRAY (4, lookupIndex);
274 typedef RecordListOf<Feature> FeatureList;
277 struct LookupFlag : USHORT
280 RightToLeft = 0x0001u,
281 IgnoreBaseGlyphs = 0x0002u,
282 IgnoreLigatures = 0x0004u,
283 IgnoreMarks = 0x0008u,
284 IgnoreFlags = 0x000Eu,
285 UseMarkFilteringSet = 0x0010u,
287 MarkAttachmentType = 0xFF00u
290 DEFINE_SIZE_STATIC (2);
295 inline unsigned int get_subtable_count (void) const { return subTable.len; }
297 inline unsigned int get_type (void) const { return lookupType; }
298 inline unsigned int get_flag (void) const
300 unsigned int flag = lookupFlag;
301 if (unlikely (flag & LookupFlag::UseMarkFilteringSet))
303 const USHORT &markFilteringSet = StructAfter<USHORT> (subTable);
304 flag += (markFilteringSet << 16);
309 inline bool sanitize (hb_sanitize_context_t *c) {
311 /* Real sanitize of the subtables is done by GSUB/GPOS/... */
312 if (!(c->check_struct (this)
313 && subTable.sanitize (c))) return false;
314 if (unlikely (lookupFlag & LookupFlag::UseMarkFilteringSet))
316 USHORT &markFilteringSet = StructAfter<USHORT> (subTable);
317 if (!markFilteringSet.sanitize (c)) return false;
322 USHORT lookupType; /* Different enumerations for GSUB and GPOS */
323 USHORT lookupFlag; /* Lookup qualifiers */
325 subTable; /* Array of SubTables */
326 USHORT markFilteringSetX[VAR]; /* Index (base 0) into GDEF mark glyph sets
327 * structure. This field is only present if bit
328 * UseMarkFilteringSet of lookup flags is set. */
330 DEFINE_SIZE_ARRAY2 (6, subTable, markFilteringSetX);
333 typedef OffsetListOf<Lookup> LookupList;
340 struct CoverageFormat1
342 friend struct Coverage;
345 inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
347 int i = glyphArray.search (glyph_id);
353 inline bool sanitize (hb_sanitize_context_t *c) {
355 return glyphArray.sanitize (c);
359 USHORT coverageFormat; /* Format identifier--format = 1 */
360 SortedArrayOf<GlyphID>
361 glyphArray; /* Array of GlyphIDs--in numerical order */
363 DEFINE_SIZE_ARRAY (4, glyphArray);
366 struct CoverageFormat2
368 friend struct Coverage;
371 inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
373 int i = rangeRecord.search (glyph_id);
375 const RangeRecord &range = rangeRecord[i];
376 return (unsigned int) range.value + (glyph_id - range.start);
381 inline bool sanitize (hb_sanitize_context_t *c) {
383 return rangeRecord.sanitize (c);
387 USHORT coverageFormat; /* Format identifier--format = 2 */
388 SortedArrayOf<RangeRecord>
389 rangeRecord; /* Array of glyph ranges--ordered by
390 * Start GlyphID. rangeCount entries
393 DEFINE_SIZE_ARRAY (4, rangeRecord);
398 inline unsigned int operator () (hb_codepoint_t glyph_id) const { return get_coverage (glyph_id); }
400 inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
403 case 1: return u.format1.get_coverage(glyph_id);
404 case 2: return u.format2.get_coverage(glyph_id);
405 default:return NOT_COVERED;
409 inline bool sanitize (hb_sanitize_context_t *c) {
411 if (!u.format.sanitize (c)) return false;
413 case 1: return u.format1.sanitize (c);
414 case 2: return u.format2.sanitize (c);
421 USHORT format; /* Format identifier */
422 CoverageFormat1 format1;
423 CoverageFormat2 format2;
426 DEFINE_SIZE_UNION (2, format);
431 * Class Definition Table
434 struct ClassDefFormat1
436 friend struct ClassDef;
439 inline hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
441 if ((unsigned int) (glyph_id - startGlyph) < classValue.len)
442 return classValue[glyph_id - startGlyph];
446 inline bool sanitize (hb_sanitize_context_t *c) {
448 return c->check_struct (this)
449 && classValue.sanitize (c);
452 USHORT classFormat; /* Format identifier--format = 1 */
453 GlyphID startGlyph; /* First GlyphID of the classValueArray */
455 classValue; /* Array of Class Values--one per GlyphID */
457 DEFINE_SIZE_ARRAY (6, classValue);
460 struct ClassDefFormat2
462 friend struct ClassDef;
465 inline hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
467 int i = rangeRecord.search (glyph_id);
469 return rangeRecord[i].value;
473 inline bool sanitize (hb_sanitize_context_t *c) {
475 return rangeRecord.sanitize (c);
478 USHORT classFormat; /* Format identifier--format = 2 */
479 SortedArrayOf<RangeRecord>
480 rangeRecord; /* Array of glyph ranges--ordered by
483 DEFINE_SIZE_ARRAY (4, rangeRecord);
488 inline hb_ot_layout_class_t operator () (hb_codepoint_t glyph_id) const { return get_class (glyph_id); }
490 inline hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
493 case 1: return u.format1.get_class(glyph_id);
494 case 2: return u.format2.get_class(glyph_id);
499 inline bool sanitize (hb_sanitize_context_t *c) {
501 if (!u.format.sanitize (c)) return false;
503 case 1: return u.format1.sanitize (c);
504 case 2: return u.format2.sanitize (c);
511 USHORT format; /* Format identifier */
512 ClassDefFormat1 format1;
513 ClassDefFormat2 format2;
516 DEFINE_SIZE_UNION (2, format);
528 inline hb_position_t get_x_delta (hb_ot_layout_context_t *c) const
529 { return c->font->x_ppem ? get_delta (c->font->x_ppem) * (uint64_t) c->font->x_scale / c->font->x_ppem : 0; }
531 inline hb_position_t get_y_delta (hb_ot_layout_context_t *c) const
532 { return c->font->y_ppem ? get_delta (c->font->y_ppem) * (uint64_t) c->font->y_scale / c->font->y_ppem : 0; }
534 inline int get_delta (unsigned int ppem_size) const
536 unsigned int f = deltaFormat;
537 if (unlikely (f < 1 || f > 3))
540 if (ppem_size < startSize || ppem_size > endSize)
543 unsigned int s = ppem_size - startSize;
545 unsigned int byte = deltaValue[s >> (4 - f)];
546 unsigned int bits = (byte >> (16 - (((s & ((1 << (4 - f)) - 1)) + 1) << f)));
547 unsigned int mask = (0xFFFF >> (16 - (1 << f)));
549 int delta = bits & mask;
551 if ((unsigned int) delta >= ((mask + 1) >> 1))
557 inline unsigned int get_size () const
559 unsigned int f = deltaFormat;
560 if (unlikely (f < 1 || f > 3 || startSize > endSize)) return 3 * USHORT::static_size;
561 return USHORT::static_size * (4 + ((endSize - startSize) >> (4 - f)));
564 inline bool sanitize (hb_sanitize_context_t *c) {
566 return c->check_struct (this)
567 && c->check_range (this, this->get_size ());
571 USHORT startSize; /* Smallest size to correct--in ppem */
572 USHORT endSize; /* Largest size to correct--in ppem */
573 USHORT deltaFormat; /* Format of DeltaValue array data: 1, 2, or 3
574 * 1 Signed 2-bit value, 8 values per uint16
575 * 2 Signed 4-bit value, 4 values per uint16
576 * 3 Signed 8-bit value, 2 values per uint16
578 USHORT deltaValue[VAR]; /* Array of compressed data */
580 DEFINE_SIZE_ARRAY (6, deltaValue);
584 #endif /* HB_OT_LAYOUT_COMMON_PRIVATE_HH */