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 b) const {
61 return b < a ? -1 : b == a ? 0 : -1;
64 inline bool sanitize (hb_sanitize_context_t *c, void *base) {
66 return c->check_struct (this)
67 && offset.sanitize (c, base);
70 Tag tag; /* 4-byte Tag identifier */
72 offset; /* Offset from beginning of object holding
75 DEFINE_SIZE_STATIC (6);
78 template <typename Type>
79 struct RecordArrayOf : SortedArrayOf<Record<Type> > {
80 inline const Tag& get_tag (unsigned int i) const
82 if (unlikely (i >= this->len)) return Null(Tag);
83 return (*this)[i].tag;
85 inline unsigned int get_tags (unsigned int start_offset,
86 unsigned int *record_count /* IN/OUT */,
87 hb_tag_t *record_tags /* OUT */) const
90 const Record<Type> *array = this->sub_array (start_offset, record_count);
91 unsigned int count = *record_count;
92 for (unsigned int i = 0; i < count; i++)
93 record_tags[i] = array[i].tag;
97 inline bool find_index (hb_tag_t tag, unsigned int *index) const
99 int i = this->search (tag);
101 if (index) *index = i;
104 if (index) *index = Index::NOT_FOUND_INDEX;
110 template <typename Type>
111 struct RecordListOf : RecordArrayOf<Type>
113 inline const Type& operator [] (unsigned int i) const
114 { return this+RecordArrayOf<Type>::operator [](i).offset; }
116 inline bool sanitize (hb_sanitize_context_t *c) {
118 return RecordArrayOf<Type>::sanitize (c, this);
125 inline int cmp (hb_codepoint_t g) const {
126 hb_codepoint_t a = start, b = end;
127 return g < a ? -1 : g <= b ? 0 : +1 ;
130 inline bool sanitize (hb_sanitize_context_t *c) {
132 return c->check_struct (this);
135 GlyphID start; /* First GlyphID in the range */
136 GlyphID end; /* Last GlyphID in the range */
137 USHORT value; /* Value */
139 DEFINE_SIZE_STATIC (6);
141 DEFINE_NULL_DATA (RangeRecord, "\000\001");
144 struct IndexArray : ArrayOf<Index>
146 inline unsigned int get_indexes (unsigned int start_offset,
147 unsigned int *_count /* IN/OUT */,
148 unsigned int *_indexes /* OUT */) const
151 const USHORT *array = this->sub_array (start_offset, _count);
152 unsigned int count = *_count;
153 for (unsigned int i = 0; i < count; i++)
154 _indexes[i] = array[i];
168 inline unsigned int get_feature_count (void) const
169 { return featureIndex.len; }
170 inline hb_tag_t get_feature_index (unsigned int i) const
171 { return featureIndex[i]; }
172 inline unsigned int get_feature_indexes (unsigned int start_offset,
173 unsigned int *feature_count /* IN/OUT */,
174 unsigned int *feature_indexes /* OUT */) const
175 { return featureIndex.get_indexes (start_offset, feature_count, feature_indexes); }
177 inline bool has_required_feature (void) const { return reqFeatureIndex != 0xffff; }
178 inline unsigned int get_required_feature_index (void) const
180 if (reqFeatureIndex == 0xffff)
181 return Index::NOT_FOUND_INDEX;
182 return reqFeatureIndex;;
185 inline bool sanitize (hb_sanitize_context_t *c) {
187 return c->check_struct (this)
188 && featureIndex.sanitize (c);
191 Offset lookupOrder; /* = Null (reserved for an offset to a
192 * reordering table) */
193 USHORT reqFeatureIndex;/* Index of a feature required for this
194 * language system--if no required features
196 IndexArray featureIndex; /* Array of indices into the FeatureList */
198 DEFINE_SIZE_ARRAY (6, featureIndex);
200 DEFINE_NULL_DATA (LangSys, "\0\0\xFF\xFF");
205 inline unsigned int get_lang_sys_count (void) const
206 { return langSys.len; }
207 inline const Tag& get_lang_sys_tag (unsigned int i) const
208 { return langSys.get_tag (i); }
209 inline unsigned int get_lang_sys_tags (unsigned int start_offset,
210 unsigned int *lang_sys_count /* IN/OUT */,
211 hb_tag_t *lang_sys_tags /* OUT */) const
212 { return langSys.get_tags (start_offset, lang_sys_count, lang_sys_tags); }
213 inline const LangSys& get_lang_sys (unsigned int i) const
215 if (i == Index::NOT_FOUND_INDEX) return get_default_lang_sys ();
216 return this+langSys[i].offset;
218 inline bool find_lang_sys_index (hb_tag_t tag, unsigned int *index) const
219 { return langSys.find_index (tag, index); }
221 inline bool has_default_lang_sys (void) const { return defaultLangSys != 0; }
222 inline const LangSys& get_default_lang_sys (void) const { return this+defaultLangSys; }
224 inline bool sanitize (hb_sanitize_context_t *c) {
226 return defaultLangSys.sanitize (c, this)
227 && langSys.sanitize (c, this);
232 defaultLangSys; /* Offset to DefaultLangSys table--from
233 * beginning of Script table--may be Null */
234 RecordArrayOf<LangSys>
235 langSys; /* Array of LangSysRecords--listed
236 * alphabetically by LangSysTag */
238 DEFINE_SIZE_ARRAY (4, langSys);
241 typedef RecordListOf<Script> ScriptList;
246 inline unsigned int get_lookup_count (void) const
247 { return lookupIndex.len; }
248 inline hb_tag_t get_lookup_index (unsigned int i) const
249 { return lookupIndex[i]; }
250 inline unsigned int get_lookup_indexes (unsigned int start_index,
251 unsigned int *lookup_count /* IN/OUT */,
252 unsigned int *lookup_tags /* OUT */) const
253 { return lookupIndex.get_indexes (start_index, lookup_count, lookup_tags); }
255 inline bool sanitize (hb_sanitize_context_t *c) {
257 return c->check_struct (this)
258 && lookupIndex.sanitize (c);
261 /* LONGTERMTODO: implement get_feature_parameters() */
262 /* LONGTERMTODO: implement FeatureSize and other special features? */
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; }
296 inline unsigned int get_flag (void) const
298 unsigned int flag = lookupFlag;
299 if (unlikely (flag & LookupFlag::UseMarkFilteringSet))
301 const USHORT &markFilteringSet = StructAfter<USHORT> (subTable);
302 flag += (markFilteringSet << 16);
307 inline bool sanitize (hb_sanitize_context_t *c) {
309 /* Real sanitize of the subtables is done by GSUB/GPOS/... */
310 if (!(c->check_struct (this)
311 && subTable.sanitize (c))) return false;
312 if (unlikely (lookupFlag & LookupFlag::UseMarkFilteringSet))
314 USHORT &markFilteringSet = StructAfter<USHORT> (subTable);
315 if (!markFilteringSet.sanitize (c)) return false;
320 USHORT lookupType; /* Different enumerations for GSUB and GPOS */
321 USHORT lookupFlag; /* Lookup qualifiers */
323 subTable; /* Array of SubTables */
324 USHORT markFilteringSetX[VAR]; /* Index (base 0) into GDEF mark glyph sets
325 * structure. This field is only present if bit
326 * UseMarkFilteringSet of lookup flags is set. */
328 DEFINE_SIZE_ARRAY2 (6, subTable, markFilteringSetX);
331 typedef OffsetListOf<Lookup> LookupList;
338 struct CoverageFormat1
340 friend struct Coverage;
343 inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
345 int i = glyphArray.search (glyph_id);
351 inline bool sanitize (hb_sanitize_context_t *c) {
353 return glyphArray.sanitize (c);
357 USHORT coverageFormat; /* Format identifier--format = 1 */
358 SortedArrayOf<GlyphID>
359 glyphArray; /* Array of GlyphIDs--in numerical order */
361 DEFINE_SIZE_ARRAY (4, glyphArray);
364 struct CoverageFormat2
366 friend struct Coverage;
369 inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
371 int i = rangeRecord.search (glyph_id);
373 const RangeRecord &range = rangeRecord[i];
374 return (unsigned int) range.value + (glyph_id - range.start);
379 inline bool sanitize (hb_sanitize_context_t *c) {
381 return rangeRecord.sanitize (c);
385 USHORT coverageFormat; /* Format identifier--format = 2 */
386 SortedArrayOf<RangeRecord>
387 rangeRecord; /* Array of glyph ranges--ordered by
388 * Start GlyphID. rangeCount entries
391 DEFINE_SIZE_ARRAY (4, rangeRecord);
396 inline unsigned int operator () (hb_codepoint_t glyph_id) const { return get_coverage (glyph_id); }
398 inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
401 case 1: return u.format1.get_coverage(glyph_id);
402 case 2: return u.format2.get_coverage(glyph_id);
403 default:return NOT_COVERED;
407 inline bool sanitize (hb_sanitize_context_t *c) {
409 if (!u.format.sanitize (c)) return false;
411 case 1: return u.format1.sanitize (c);
412 case 2: return u.format2.sanitize (c);
419 USHORT format; /* Format identifier */
420 CoverageFormat1 format1;
421 CoverageFormat2 format2;
424 DEFINE_SIZE_UNION (2, format);
429 * Class Definition Table
432 struct ClassDefFormat1
434 friend struct ClassDef;
437 inline hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
439 if ((unsigned int) (glyph_id - startGlyph) < classValue.len)
440 return classValue[glyph_id - startGlyph];
444 inline bool sanitize (hb_sanitize_context_t *c) {
446 return c->check_struct (this)
447 && classValue.sanitize (c);
450 USHORT classFormat; /* Format identifier--format = 1 */
451 GlyphID startGlyph; /* First GlyphID of the classValueArray */
453 classValue; /* Array of Class Values--one per GlyphID */
455 DEFINE_SIZE_ARRAY (6, classValue);
458 struct ClassDefFormat2
460 friend struct ClassDef;
463 inline hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
465 int i = rangeRecord.search (glyph_id);
467 return rangeRecord[i].value;
471 inline bool sanitize (hb_sanitize_context_t *c) {
473 return rangeRecord.sanitize (c);
476 USHORT classFormat; /* Format identifier--format = 2 */
477 SortedArrayOf<RangeRecord>
478 rangeRecord; /* Array of glyph ranges--ordered by
481 DEFINE_SIZE_ARRAY (4, rangeRecord);
486 inline hb_ot_layout_class_t operator () (hb_codepoint_t glyph_id) const { return get_class (glyph_id); }
488 inline hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
491 case 1: return u.format1.get_class(glyph_id);
492 case 2: return u.format2.get_class(glyph_id);
497 inline bool sanitize (hb_sanitize_context_t *c) {
499 if (!u.format.sanitize (c)) return false;
501 case 1: return u.format1.sanitize (c);
502 case 2: return u.format2.sanitize (c);
509 USHORT format; /* Format identifier */
510 ClassDefFormat1 format1;
511 ClassDefFormat2 format2;
514 DEFINE_SIZE_UNION (2, format);
525 inline hb_position_t get_x_delta (hb_ot_layout_context_t *c) const
526 { return get_delta (c->font->x_ppem, c->font->x_scale); }
528 inline hb_position_t get_y_delta (hb_ot_layout_context_t *c) const
529 { return get_delta (c->font->y_ppem, c->font->y_scale); }
531 inline int get_delta (unsigned int ppem, unsigned int scale) const
535 int pixels = get_delta_pixels (ppem);
537 if (!pixels) return 0;
539 /* pixels is at most in the -8..7 range. So 64-bit arithmetic is
540 * not really necessary here. A simple cast to int may just work
541 * as well. But since this code is not reached that often and
542 * for the sake of correctness, we do a 64bit operation. */
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 () 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 */