2 * Copyright © 2007,2008,2009 Red Hat, Inc.
3 * Copyright © 2010 Google, Inc.
5 * This is part of HarfBuzz, a text shaping library.
7 * Permission is hereby granted, without written agreement and without
8 * license or royalty fees, to use, copy, modify, and distribute this
9 * software and its documentation for any purpose, provided that the
10 * above copyright notice and the following two paragraphs appear in
11 * all copies of this software.
13 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
14 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
15 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
16 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
19 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
20 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
22 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
23 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
25 * Red Hat Author(s): Behdad Esfahbod
26 * Google Author(s): Behdad Esfahbod
29 #ifndef HB_OT_LAYOUT_COMMON_PRIVATE_HH
30 #define HB_OT_LAYOUT_COMMON_PRIVATE_HH
32 #include "hb-ot-layout-private.hh"
34 #include "hb-open-type-private.hh"
37 #define NO_CONTEXT ((unsigned int) 0x110000)
38 #define NOT_COVERED ((unsigned int) 0x110000)
39 #define MAX_NESTING_LEVEL 8
47 * OpenType Layout Common Table Formats
53 * Script, ScriptList, LangSys, Feature, FeatureList, Lookup, LookupList
56 template <typename Type>
59 inline int cmp (hb_tag_t a) const {
63 inline bool sanitize (hb_sanitize_context_t *c, void *base) {
65 return c->check_struct (this)
66 && offset.sanitize (c, base);
69 Tag tag; /* 4-byte Tag identifier */
71 offset; /* Offset from beginning of object holding
74 DEFINE_SIZE_STATIC (6);
77 template <typename Type>
78 struct RecordArrayOf : SortedArrayOf<Record<Type> > {
79 inline const Tag& get_tag (unsigned int i) const
81 /* We cheat slightly and don't define separate Null objects
82 * for Record types. Instead, we return the correct Null(Tag)
84 if (unlikely (i >= this->len)) return Null(Tag);
85 return (*this)[i].tag;
87 inline unsigned int get_tags (unsigned int start_offset,
88 unsigned int *record_count /* IN/OUT */,
89 hb_tag_t *record_tags /* OUT */) const
92 const Record<Type> *arr = this->sub_array (start_offset, record_count);
93 unsigned int count = *record_count;
94 for (unsigned int i = 0; i < count; i++)
95 record_tags[i] = arr[i].tag;
99 inline bool find_index (hb_tag_t tag, unsigned int *index) const
101 int i = this->search (tag);
103 if (index) *index = i;
106 if (index) *index = Index::NOT_FOUND_INDEX;
112 template <typename Type>
113 struct RecordListOf : RecordArrayOf<Type>
115 inline const Type& operator [] (unsigned int i) const
116 { return this+RecordArrayOf<Type>::operator [](i).offset; }
118 inline bool sanitize (hb_sanitize_context_t *c) {
120 return RecordArrayOf<Type>::sanitize (c, this);
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 *arr = this->sub_array (start_offset, _count);
154 unsigned int count = *_count;
155 for (unsigned int i = 0; i < count; i++)
156 _indexes[i] = arr[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 Offset featureParams; /* Offset to Feature Parameters table (if one
264 * has been defined for the feature), relative
265 * to the beginning of the Feature Table; = Null
267 IndexArray lookupIndex; /* Array of LookupList indices */
269 DEFINE_SIZE_ARRAY (4, lookupIndex);
272 typedef RecordListOf<Feature> FeatureList;
275 struct LookupFlag : USHORT
278 RightToLeft = 0x0001u,
279 IgnoreBaseGlyphs = 0x0002u,
280 IgnoreLigatures = 0x0004u,
281 IgnoreMarks = 0x0008u,
282 IgnoreFlags = 0x000Eu,
283 UseMarkFilteringSet = 0x0010u,
285 MarkAttachmentType = 0xFF00u
288 DEFINE_SIZE_STATIC (2);
293 inline unsigned int get_subtable_count (void) const { return subTable.len; }
295 inline unsigned int get_type (void) const { return lookupType; }
297 /* lookup_props is a 32-bit integer where the lower 16-bit is LookupFlag and
298 * higher 16-bit is mark-filtering-set if the lookup uses one.
299 * Not to be confused with glyph_props which is very similar. */
300 inline uint32_t get_props (void) const
302 unsigned int flag = lookupFlag;
303 if (unlikely (flag & LookupFlag::UseMarkFilteringSet))
305 const USHORT &markFilteringSet = StructAfter<USHORT> (subTable);
306 flag += (markFilteringSet << 16);
311 inline bool sanitize (hb_sanitize_context_t *c) {
313 /* Real sanitize of the subtables is done by GSUB/GPOS/... */
314 if (!(c->check_struct (this)
315 && subTable.sanitize (c))) return false;
316 if (unlikely (lookupFlag & LookupFlag::UseMarkFilteringSet))
318 USHORT &markFilteringSet = StructAfter<USHORT> (subTable);
319 if (!markFilteringSet.sanitize (c)) return false;
324 USHORT lookupType; /* Different enumerations for GSUB and GPOS */
325 USHORT lookupFlag; /* Lookup qualifiers */
327 subTable; /* Array of SubTables */
328 USHORT markFilteringSetX[VAR]; /* Index (base 0) into GDEF mark glyph sets
329 * structure. This field is only present if bit
330 * UseMarkFilteringSet of lookup flags is set. */
332 DEFINE_SIZE_ARRAY2 (6, subTable, markFilteringSetX);
335 typedef OffsetListOf<Lookup> LookupList;
342 struct CoverageFormat1
344 friend struct Coverage;
347 inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
349 int i = glyphArray.search (glyph_id);
355 inline bool sanitize (hb_sanitize_context_t *c) {
357 return glyphArray.sanitize (c);
361 USHORT coverageFormat; /* Format identifier--format = 1 */
362 SortedArrayOf<GlyphID>
363 glyphArray; /* Array of GlyphIDs--in numerical order */
365 DEFINE_SIZE_ARRAY (4, glyphArray);
368 struct CoverageFormat2
370 friend struct Coverage;
373 inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
375 int i = rangeRecord.search (glyph_id);
377 const RangeRecord &range = rangeRecord[i];
378 return (unsigned int) range.value + (glyph_id - range.start);
383 inline bool sanitize (hb_sanitize_context_t *c) {
385 return rangeRecord.sanitize (c);
389 USHORT coverageFormat; /* Format identifier--format = 2 */
390 SortedArrayOf<RangeRecord>
391 rangeRecord; /* Array of glyph ranges--ordered by
392 * Start GlyphID. rangeCount entries
395 DEFINE_SIZE_ARRAY (4, rangeRecord);
400 inline unsigned int operator () (hb_codepoint_t glyph_id) const { return get_coverage (glyph_id); }
402 inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
405 case 1: return u.format1.get_coverage(glyph_id);
406 case 2: return u.format2.get_coverage(glyph_id);
407 default:return NOT_COVERED;
411 inline bool sanitize (hb_sanitize_context_t *c) {
413 if (!u.format.sanitize (c)) return false;
415 case 1: return u.format1.sanitize (c);
416 case 2: return u.format2.sanitize (c);
423 USHORT format; /* Format identifier */
424 CoverageFormat1 format1;
425 CoverageFormat2 format2;
428 DEFINE_SIZE_UNION (2, format);
433 * Class Definition Table
436 struct ClassDefFormat1
438 friend struct ClassDef;
441 inline unsigned int get_class (hb_codepoint_t glyph_id) const
443 if ((unsigned int) (glyph_id - startGlyph) < classValue.len)
444 return classValue[glyph_id - startGlyph];
448 inline bool sanitize (hb_sanitize_context_t *c) {
450 return c->check_struct (this)
451 && classValue.sanitize (c);
454 USHORT classFormat; /* Format identifier--format = 1 */
455 GlyphID startGlyph; /* First GlyphID of the classValueArray */
457 classValue; /* Array of Class Values--one per GlyphID */
459 DEFINE_SIZE_ARRAY (6, classValue);
462 struct ClassDefFormat2
464 friend struct ClassDef;
467 inline unsigned int get_class (hb_codepoint_t glyph_id) const
469 int i = rangeRecord.search (glyph_id);
471 return rangeRecord[i].value;
475 inline bool sanitize (hb_sanitize_context_t *c) {
477 return rangeRecord.sanitize (c);
480 USHORT classFormat; /* Format identifier--format = 2 */
481 SortedArrayOf<RangeRecord>
482 rangeRecord; /* Array of glyph ranges--ordered by
485 DEFINE_SIZE_ARRAY (4, rangeRecord);
490 inline unsigned int operator () (hb_codepoint_t glyph_id) const { return get_class (glyph_id); }
492 inline unsigned int get_class (hb_codepoint_t glyph_id) const
495 case 1: return u.format1.get_class(glyph_id);
496 case 2: return u.format2.get_class(glyph_id);
501 inline bool sanitize (hb_sanitize_context_t *c) {
503 if (!u.format.sanitize (c)) return false;
505 case 1: return u.format1.sanitize (c);
506 case 2: return u.format2.sanitize (c);
513 USHORT format; /* Format identifier */
514 ClassDefFormat1 format1;
515 ClassDefFormat2 format2;
518 DEFINE_SIZE_UNION (2, format);
529 inline hb_position_t get_x_delta (hb_font_t *font) const
530 { return get_delta (font->x_ppem, font->x_scale); }
532 inline hb_position_t get_y_delta (hb_font_t *font) const
533 { return get_delta (font->y_ppem, font->y_scale); }
535 inline int get_delta (unsigned int ppem, int scale) const
539 int pixels = get_delta_pixels (ppem);
541 if (!pixels) return 0;
543 return pixels * (int64_t) scale / ppem;
547 inline int get_delta_pixels (unsigned int ppem_size) const
549 unsigned int f = deltaFormat;
550 if (unlikely (f < 1 || f > 3))
553 if (ppem_size < startSize || ppem_size > endSize)
556 unsigned int s = ppem_size - startSize;
558 unsigned int byte = deltaValue[s >> (4 - f)];
559 unsigned int bits = (byte >> (16 - (((s & ((1 << (4 - f)) - 1)) + 1) << f)));
560 unsigned int mask = (0xFFFF >> (16 - (1 << f)));
562 int delta = bits & mask;
564 if ((unsigned int) delta >= ((mask + 1) >> 1))
570 inline unsigned int get_size (void) const
572 unsigned int f = deltaFormat;
573 if (unlikely (f < 1 || f > 3 || startSize > endSize)) return 3 * USHORT::static_size;
574 return USHORT::static_size * (4 + ((endSize - startSize) >> (4 - f)));
577 inline bool sanitize (hb_sanitize_context_t *c) {
579 return c->check_struct (this)
580 && c->check_range (this, this->get_size ());
584 USHORT startSize; /* Smallest size to correct--in ppem */
585 USHORT endSize; /* Largest size to correct--in ppem */
586 USHORT deltaFormat; /* Format of DeltaValue array data: 1, 2, or 3
587 * 1 Signed 2-bit value, 8 values per uint16
588 * 2 Signed 4-bit value, 4 values per uint16
589 * 3 Signed 8-bit value, 2 values per uint16
591 USHORT deltaValue[VAR]; /* Array of compressed data */
593 DEFINE_SIZE_ARRAY (6, deltaValue);
600 #endif /* HB_OT_LAYOUT_COMMON_PRIVATE_HH */