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 /* LONGTERMTODO: implement get_feature_parameters() */
261 /* LONGTERMTODO: implement FeatureSize and other special features? */
262 Offset featureParams; /* Offset to Feature Parameters table (if one
263 * has been defined for the feature), relative
264 * to the beginning of the Feature Table; = Null
266 IndexArray lookupIndex; /* Array of LookupList indices */
268 DEFINE_SIZE_ARRAY (4, lookupIndex);
271 typedef RecordListOf<Feature> FeatureList;
274 struct LookupFlag : USHORT
277 RightToLeft = 0x0001u,
278 IgnoreBaseGlyphs = 0x0002u,
279 IgnoreLigatures = 0x0004u,
280 IgnoreMarks = 0x0008u,
281 IgnoreFlags = 0x000Eu,
282 UseMarkFilteringSet = 0x0010u,
284 MarkAttachmentType = 0xFF00u
287 DEFINE_SIZE_STATIC (2);
292 inline unsigned int get_subtable_count (void) const { return subTable.len; }
294 inline unsigned int get_type (void) const { return lookupType; }
295 inline unsigned int get_flag (void) const
297 unsigned int flag = lookupFlag;
298 if (unlikely (flag & LookupFlag::UseMarkFilteringSet))
300 const USHORT &markFilteringSet = StructAfter<USHORT> (subTable);
301 flag += (markFilteringSet << 16);
306 inline bool sanitize (hb_sanitize_context_t *c) {
308 /* Real sanitize of the subtables is done by GSUB/GPOS/... */
309 if (!(c->check_struct (this)
310 && subTable.sanitize (c))) return false;
311 if (unlikely (lookupFlag & LookupFlag::UseMarkFilteringSet))
313 USHORT &markFilteringSet = StructAfter<USHORT> (subTable);
314 if (!markFilteringSet.sanitize (c)) return false;
319 USHORT lookupType; /* Different enumerations for GSUB and GPOS */
320 USHORT lookupFlag; /* Lookup qualifiers */
322 subTable; /* Array of SubTables */
323 USHORT markFilteringSetX[VAR]; /* Index (base 0) into GDEF mark glyph sets
324 * structure. This field is only present if bit
325 * UseMarkFilteringSet of lookup flags is set. */
327 DEFINE_SIZE_ARRAY2 (6, subTable, markFilteringSetX);
330 typedef OffsetListOf<Lookup> LookupList;
337 struct CoverageFormat1
339 friend struct Coverage;
342 inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
344 int i = glyphArray.search (glyph_id);
350 inline bool sanitize (hb_sanitize_context_t *c) {
352 return glyphArray.sanitize (c);
356 USHORT coverageFormat; /* Format identifier--format = 1 */
357 SortedArrayOf<GlyphID>
358 glyphArray; /* Array of GlyphIDs--in numerical order */
360 DEFINE_SIZE_ARRAY (4, glyphArray);
363 struct CoverageFormat2
365 friend struct Coverage;
368 inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
370 int i = rangeRecord.search (glyph_id);
372 const RangeRecord &range = rangeRecord[i];
373 return (unsigned int) range.value + (glyph_id - range.start);
378 inline bool sanitize (hb_sanitize_context_t *c) {
380 return rangeRecord.sanitize (c);
384 USHORT coverageFormat; /* Format identifier--format = 2 */
385 SortedArrayOf<RangeRecord>
386 rangeRecord; /* Array of glyph ranges--ordered by
387 * Start GlyphID. rangeCount entries
390 DEFINE_SIZE_ARRAY (4, rangeRecord);
395 inline unsigned int operator () (hb_codepoint_t glyph_id) const { return get_coverage (glyph_id); }
397 inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
400 case 1: return u.format1.get_coverage(glyph_id);
401 case 2: return u.format2.get_coverage(glyph_id);
402 default:return NOT_COVERED;
406 inline bool sanitize (hb_sanitize_context_t *c) {
408 if (!u.format.sanitize (c)) return false;
410 case 1: return u.format1.sanitize (c);
411 case 2: return u.format2.sanitize (c);
418 USHORT format; /* Format identifier */
419 CoverageFormat1 format1;
420 CoverageFormat2 format2;
423 DEFINE_SIZE_UNION (2, format);
428 * Class Definition Table
431 struct ClassDefFormat1
433 friend struct ClassDef;
436 inline hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
438 if ((unsigned int) (glyph_id - startGlyph) < classValue.len)
439 return classValue[glyph_id - startGlyph];
443 inline bool sanitize (hb_sanitize_context_t *c) {
445 return c->check_struct (this)
446 && classValue.sanitize (c);
449 USHORT classFormat; /* Format identifier--format = 1 */
450 GlyphID startGlyph; /* First GlyphID of the classValueArray */
452 classValue; /* Array of Class Values--one per GlyphID */
454 DEFINE_SIZE_ARRAY (6, classValue);
457 struct ClassDefFormat2
459 friend struct ClassDef;
462 inline hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
464 int i = rangeRecord.search (glyph_id);
466 return rangeRecord[i].value;
470 inline bool sanitize (hb_sanitize_context_t *c) {
472 return rangeRecord.sanitize (c);
475 USHORT classFormat; /* Format identifier--format = 2 */
476 SortedArrayOf<RangeRecord>
477 rangeRecord; /* Array of glyph ranges--ordered by
480 DEFINE_SIZE_ARRAY (4, rangeRecord);
485 inline hb_ot_layout_class_t operator () (hb_codepoint_t glyph_id) const { return get_class (glyph_id); }
487 inline hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
490 case 1: return u.format1.get_class(glyph_id);
491 case 2: return u.format2.get_class(glyph_id);
496 inline bool sanitize (hb_sanitize_context_t *c) {
498 if (!u.format.sanitize (c)) return false;
500 case 1: return u.format1.sanitize (c);
501 case 2: return u.format2.sanitize (c);
508 USHORT format; /* Format identifier */
509 ClassDefFormat1 format1;
510 ClassDefFormat2 format2;
513 DEFINE_SIZE_UNION (2, format);
524 inline hb_position_t get_x_delta (hb_ot_layout_context_t *c) const
525 { return get_delta (c->font->x_ppem, c->font->x_scale); }
527 inline hb_position_t get_y_delta (hb_ot_layout_context_t *c) const
528 { return get_delta (c->font->y_ppem, c->font->y_scale); }
530 inline int get_delta (unsigned int ppem, unsigned int scale) const
534 int pixels = get_delta_pixels (ppem);
536 if (!pixels) return 0;
538 /* pixels is at most in the -8..7 range. So 64-bit arithmetic is
539 * not really necessary here. A simple cast to int may just work
540 * as well. But since this code is not reached that often and
541 * for the sake of correctness, we do a 64bit operation. */
542 return pixels * (int64_t) scale / ppem;
546 inline int get_delta_pixels (unsigned int ppem_size) const
548 unsigned int f = deltaFormat;
549 if (unlikely (f < 1 || f > 3))
552 if (ppem_size < startSize || ppem_size > endSize)
555 unsigned int s = ppem_size - startSize;
557 unsigned int byte = deltaValue[s >> (4 - f)];
558 unsigned int bits = (byte >> (16 - (((s & ((1 << (4 - f)) - 1)) + 1) << f)));
559 unsigned int mask = (0xFFFF >> (16 - (1 << f)));
561 int delta = bits & mask;
563 if ((unsigned int) delta >= ((mask + 1) >> 1))
569 inline unsigned int get_size () const
571 unsigned int f = deltaFormat;
572 if (unlikely (f < 1 || f > 3 || startSize > endSize)) return 3 * USHORT::static_size;
573 return USHORT::static_size * (4 + ((endSize - startSize) >> (4 - f)));
576 inline bool sanitize (hb_sanitize_context_t *c) {
578 return c->check_struct (this)
579 && c->check_range (this, this->get_size ());
583 USHORT startSize; /* Smallest size to correct--in ppem */
584 USHORT endSize; /* Largest size to correct--in ppem */
585 USHORT deltaFormat; /* Format of DeltaValue array data: 1, 2, or 3
586 * 1 Signed 2-bit value, 8 values per uint16
587 * 2 Signed 4-bit value, 4 values per uint16
588 * 3 Signed 8-bit value, 2 values per uint16
590 USHORT deltaValue[VAR]; /* Array of compressed data */
592 DEFINE_SIZE_ARRAY (6, deltaValue);
599 #endif /* HB_OT_LAYOUT_COMMON_PRIVATE_HH */