2 * Copyright (C) 2007,2008,2009 Red Hat, Inc.
3 * Copyright (C) 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 if (unlikely (i >= this->len)) return Null(Tag);
82 return (*this)[i].tag;
84 inline unsigned int get_tags (unsigned int start_offset,
85 unsigned int *record_count /* IN/OUT */,
86 hb_tag_t *record_tags /* OUT */) const
89 const Record<Type> *array = this->sub_array (start_offset, record_count);
90 unsigned int count = *record_count;
91 for (unsigned int i = 0; i < count; i++)
92 record_tags[i] = array[i].tag;
96 inline bool find_index (hb_tag_t tag, unsigned int *index) const
98 int i = this->search (tag);
100 if (index) *index = i;
103 if (index) *index = Index::NOT_FOUND_INDEX;
109 template <typename Type>
110 struct RecordListOf : RecordArrayOf<Type>
112 inline const Type& operator [] (unsigned int i) const
113 { return this+RecordArrayOf<Type>::operator [](i).offset; }
115 inline bool sanitize (hb_sanitize_context_t *c) {
117 return RecordArrayOf<Type>::sanitize (c, this);
124 inline int cmp (hb_codepoint_t g) const {
125 hb_codepoint_t a = start, b = end;
126 return g < a ? -1 : g <= b ? 0 : +1 ;
129 inline bool sanitize (hb_sanitize_context_t *c) {
131 return c->check_struct (this);
134 GlyphID start; /* First GlyphID in the range */
135 GlyphID end; /* Last GlyphID in the range */
136 USHORT value; /* Value */
138 DEFINE_SIZE_STATIC (6);
140 DEFINE_NULL_DATA (RangeRecord, "\000\001");
143 struct IndexArray : ArrayOf<Index>
145 inline unsigned int get_indexes (unsigned int start_offset,
146 unsigned int *_count /* IN/OUT */,
147 unsigned int *_indexes /* OUT */) const
150 const USHORT *array = this->sub_array (start_offset, _count);
151 unsigned int count = *_count;
152 for (unsigned int i = 0; i < count; i++)
153 _indexes[i] = array[i];
167 inline unsigned int get_feature_count (void) const
168 { return featureIndex.len; }
169 inline hb_tag_t get_feature_index (unsigned int i) const
170 { return featureIndex[i]; }
171 inline unsigned int get_feature_indexes (unsigned int start_offset,
172 unsigned int *feature_count /* IN/OUT */,
173 unsigned int *feature_indexes /* OUT */) const
174 { return featureIndex.get_indexes (start_offset, feature_count, feature_indexes); }
176 inline bool has_required_feature (void) const { return reqFeatureIndex != 0xffff; }
177 inline unsigned int get_required_feature_index (void) const
179 if (reqFeatureIndex == 0xffff)
180 return Index::NOT_FOUND_INDEX;
181 return reqFeatureIndex;;
184 inline bool sanitize (hb_sanitize_context_t *c) {
186 return c->check_struct (this)
187 && featureIndex.sanitize (c);
190 Offset lookupOrder; /* = Null (reserved for an offset to a
191 * reordering table) */
192 USHORT reqFeatureIndex;/* Index of a feature required for this
193 * language system--if no required features
195 IndexArray featureIndex; /* Array of indices into the FeatureList */
197 DEFINE_SIZE_ARRAY (6, featureIndex);
199 DEFINE_NULL_DATA (LangSys, "\0\0\xFF\xFF");
204 inline unsigned int get_lang_sys_count (void) const
205 { return langSys.len; }
206 inline const Tag& get_lang_sys_tag (unsigned int i) const
207 { return langSys.get_tag (i); }
208 inline unsigned int get_lang_sys_tags (unsigned int start_offset,
209 unsigned int *lang_sys_count /* IN/OUT */,
210 hb_tag_t *lang_sys_tags /* OUT */) const
211 { return langSys.get_tags (start_offset, lang_sys_count, lang_sys_tags); }
212 inline const LangSys& get_lang_sys (unsigned int i) const
214 if (i == Index::NOT_FOUND_INDEX) return get_default_lang_sys ();
215 return this+langSys[i].offset;
217 inline bool find_lang_sys_index (hb_tag_t tag, unsigned int *index) const
218 { return langSys.find_index (tag, index); }
220 inline bool has_default_lang_sys (void) const { return defaultLangSys != 0; }
221 inline const LangSys& get_default_lang_sys (void) const { return this+defaultLangSys; }
223 inline bool sanitize (hb_sanitize_context_t *c) {
225 return defaultLangSys.sanitize (c, this)
226 && langSys.sanitize (c, this);
231 defaultLangSys; /* Offset to DefaultLangSys table--from
232 * beginning of Script table--may be Null */
233 RecordArrayOf<LangSys>
234 langSys; /* Array of LangSysRecords--listed
235 * alphabetically by LangSysTag */
237 DEFINE_SIZE_ARRAY (4, langSys);
240 typedef RecordListOf<Script> ScriptList;
245 inline unsigned int get_lookup_count (void) const
246 { return lookupIndex.len; }
247 inline hb_tag_t get_lookup_index (unsigned int i) const
248 { return lookupIndex[i]; }
249 inline unsigned int get_lookup_indexes (unsigned int start_index,
250 unsigned int *lookup_count /* IN/OUT */,
251 unsigned int *lookup_tags /* OUT */) const
252 { return lookupIndex.get_indexes (start_index, lookup_count, lookup_tags); }
254 inline bool sanitize (hb_sanitize_context_t *c) {
256 return c->check_struct (this)
257 && lookupIndex.sanitize (c);
260 Offset featureParams; /* Offset to Feature Parameters table (if one
261 * has been defined for the feature), relative
262 * to the beginning of the Feature Table; = Null
264 IndexArray lookupIndex; /* Array of LookupList indices */
266 DEFINE_SIZE_ARRAY (4, lookupIndex);
269 typedef RecordListOf<Feature> FeatureList;
272 struct LookupFlag : USHORT
275 RightToLeft = 0x0001u,
276 IgnoreBaseGlyphs = 0x0002u,
277 IgnoreLigatures = 0x0004u,
278 IgnoreMarks = 0x0008u,
279 IgnoreFlags = 0x000Eu,
280 UseMarkFilteringSet = 0x0010u,
282 MarkAttachmentType = 0xFF00u
285 DEFINE_SIZE_STATIC (2);
290 inline unsigned int get_subtable_count (void) const { return subTable.len; }
292 inline unsigned int get_type (void) const { return lookupType; }
293 inline unsigned int get_flag (void) const
295 unsigned int flag = lookupFlag;
296 if (unlikely (flag & LookupFlag::UseMarkFilteringSet))
298 const USHORT &markFilteringSet = StructAfter<USHORT> (subTable);
299 flag += (markFilteringSet << 16);
304 inline bool sanitize (hb_sanitize_context_t *c) {
306 /* Real sanitize of the subtables is done by GSUB/GPOS/... */
307 if (!(c->check_struct (this)
308 && subTable.sanitize (c))) return false;
309 if (unlikely (lookupFlag & LookupFlag::UseMarkFilteringSet))
311 USHORT &markFilteringSet = StructAfter<USHORT> (subTable);
312 if (!markFilteringSet.sanitize (c)) return false;
317 USHORT lookupType; /* Different enumerations for GSUB and GPOS */
318 USHORT lookupFlag; /* Lookup qualifiers */
320 subTable; /* Array of SubTables */
321 USHORT markFilteringSetX[VAR]; /* Index (base 0) into GDEF mark glyph sets
322 * structure. This field is only present if bit
323 * UseMarkFilteringSet of lookup flags is set. */
325 DEFINE_SIZE_ARRAY2 (6, subTable, markFilteringSetX);
328 typedef OffsetListOf<Lookup> LookupList;
335 struct CoverageFormat1
337 friend struct Coverage;
340 inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
342 int i = glyphArray.search (glyph_id);
348 inline bool sanitize (hb_sanitize_context_t *c) {
350 return glyphArray.sanitize (c);
354 USHORT coverageFormat; /* Format identifier--format = 1 */
355 SortedArrayOf<GlyphID>
356 glyphArray; /* Array of GlyphIDs--in numerical order */
358 DEFINE_SIZE_ARRAY (4, glyphArray);
361 struct CoverageFormat2
363 friend struct Coverage;
366 inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
368 int i = rangeRecord.search (glyph_id);
370 const RangeRecord &range = rangeRecord[i];
371 return (unsigned int) range.value + (glyph_id - range.start);
376 inline bool sanitize (hb_sanitize_context_t *c) {
378 return rangeRecord.sanitize (c);
382 USHORT coverageFormat; /* Format identifier--format = 2 */
383 SortedArrayOf<RangeRecord>
384 rangeRecord; /* Array of glyph ranges--ordered by
385 * Start GlyphID. rangeCount entries
388 DEFINE_SIZE_ARRAY (4, rangeRecord);
393 inline unsigned int operator () (hb_codepoint_t glyph_id) const { return get_coverage (glyph_id); }
395 inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
398 case 1: return u.format1.get_coverage(glyph_id);
399 case 2: return u.format2.get_coverage(glyph_id);
400 default:return NOT_COVERED;
404 inline bool sanitize (hb_sanitize_context_t *c) {
406 if (!u.format.sanitize (c)) return false;
408 case 1: return u.format1.sanitize (c);
409 case 2: return u.format2.sanitize (c);
416 USHORT format; /* Format identifier */
417 CoverageFormat1 format1;
418 CoverageFormat2 format2;
421 DEFINE_SIZE_UNION (2, format);
426 * Class Definition Table
429 struct ClassDefFormat1
431 friend struct ClassDef;
434 inline unsigned int get_class (hb_codepoint_t glyph_id) const
436 if ((unsigned int) (glyph_id - startGlyph) < classValue.len)
437 return classValue[glyph_id - startGlyph];
441 inline bool sanitize (hb_sanitize_context_t *c) {
443 return c->check_struct (this)
444 && classValue.sanitize (c);
447 USHORT classFormat; /* Format identifier--format = 1 */
448 GlyphID startGlyph; /* First GlyphID of the classValueArray */
450 classValue; /* Array of Class Values--one per GlyphID */
452 DEFINE_SIZE_ARRAY (6, classValue);
455 struct ClassDefFormat2
457 friend struct ClassDef;
460 inline unsigned int get_class (hb_codepoint_t glyph_id) const
462 int i = rangeRecord.search (glyph_id);
464 return rangeRecord[i].value;
468 inline bool sanitize (hb_sanitize_context_t *c) {
470 return rangeRecord.sanitize (c);
473 USHORT classFormat; /* Format identifier--format = 2 */
474 SortedArrayOf<RangeRecord>
475 rangeRecord; /* Array of glyph ranges--ordered by
478 DEFINE_SIZE_ARRAY (4, rangeRecord);
483 inline unsigned int operator () (hb_codepoint_t glyph_id) const { return get_class (glyph_id); }
485 inline unsigned int get_class (hb_codepoint_t glyph_id) const
488 case 1: return u.format1.get_class(glyph_id);
489 case 2: return u.format2.get_class(glyph_id);
494 inline bool sanitize (hb_sanitize_context_t *c) {
496 if (!u.format.sanitize (c)) return false;
498 case 1: return u.format1.sanitize (c);
499 case 2: return u.format2.sanitize (c);
506 USHORT format; /* Format identifier */
507 ClassDefFormat1 format1;
508 ClassDefFormat2 format2;
511 DEFINE_SIZE_UNION (2, format);
522 inline hb_position_t get_x_delta (hb_ot_layout_context_t *c) const
523 { return get_delta (c->font->x_ppem, c->font->x_scale); }
525 inline hb_position_t get_y_delta (hb_ot_layout_context_t *c) const
526 { return get_delta (c->font->y_ppem, c->font->y_scale); }
528 inline int get_delta (unsigned int ppem, unsigned int scale) const
532 int pixels = get_delta_pixels (ppem);
534 if (!pixels) return 0;
536 /* pixels is at most in the -8..7 range. So 64-bit arithmetic is
537 * not really necessary here. A simple cast to int may just work
538 * as well. But since this code is not reached that often and
539 * for the sake of correctness, we do a 64bit operation. */
540 return pixels * (int64_t) scale / ppem;
544 inline int get_delta_pixels (unsigned int ppem_size) const
546 unsigned int f = deltaFormat;
547 if (unlikely (f < 1 || f > 3))
550 if (ppem_size < startSize || ppem_size > endSize)
553 unsigned int s = ppem_size - startSize;
555 unsigned int byte = deltaValue[s >> (4 - f)];
556 unsigned int bits = (byte >> (16 - (((s & ((1 << (4 - f)) - 1)) + 1) << f)));
557 unsigned int mask = (0xFFFF >> (16 - (1 << f)));
559 int delta = bits & mask;
561 if ((unsigned int) delta >= ((mask + 1) >> 1))
567 inline unsigned int get_size (void) const
569 unsigned int f = deltaFormat;
570 if (unlikely (f < 1 || f > 3 || startSize > endSize)) return 3 * USHORT::static_size;
571 return USHORT::static_size * (4 + ((endSize - startSize) >> (4 - f)));
574 inline bool sanitize (hb_sanitize_context_t *c) {
576 return c->check_struct (this)
577 && c->check_range (this, this->get_size ());
581 USHORT startSize; /* Smallest size to correct--in ppem */
582 USHORT endSize; /* Largest size to correct--in ppem */
583 USHORT deltaFormat; /* Format of DeltaValue array data: 1, 2, or 3
584 * 1 Signed 2-bit value, 8 values per uint16
585 * 2 Signed 4-bit value, 4 values per uint16
586 * 3 Signed 8-bit value, 2 values per uint16
588 USHORT deltaValue[VAR]; /* Array of compressed data */
590 DEFINE_SIZE_ARRAY (6, deltaValue);
597 #endif /* HB_OT_LAYOUT_COMMON_PRIVATE_HH */