2 * Copyright (C) 2007,2008,2009 Red Hat, Inc.
4 * This is part of HarfBuzz, an OpenType Layout engine 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.h"
32 #include "hb-open-types-private.hh"
37 * OpenType Layout Common Table Formats
43 * Script, ScriptList, LangSys, Feature, FeatureList, Lookup, LookupList
46 template <typename Type>
49 inline bool sanitize (SANITIZE_ARG_DEF, const char *base) {
50 /* Note: Doesn't sanitize referenced object */
51 /* Only accept ASCII-visible tags (mind DEL) */
52 return (tag & 0x80808080) == 0 && offset.sanitize (SANITIZE_ARG, base);
55 Tag tag; /* 4-byte Tag identifier */
57 offset; /* Offset from beginning of object holding
61 template <typename Type>
62 struct RecordArrayOf : ArrayOf<Record<Type> >
64 inline bool sanitize (SANITIZE_ARG_DEF, const char *base) {
65 if (!(SANITIZE (this->len) && SANITIZE_GET_SIZE())) return false;
66 unsigned int count = this->len;
67 for (unsigned int i = 0; i < count; i++)
68 if (!SANITIZE_THIS (this->array[i]))
73 template <typename Type>
74 struct RecordListOf : RecordArrayOf<Type>
76 inline const Type& operator [] (unsigned int i) const
78 if (HB_UNLIKELY (i >= this->len)) return Null(Type);
79 return this+this->array[i].offset;
81 inline const Tag& get_tag (unsigned int i) const
83 if (HB_UNLIKELY (i >= this->len)) return Null(Tag);
84 return this->array[i].tag;
87 inline bool sanitize (SANITIZE_ARG_DEF) {
88 return RecordArrayOf<Type>::sanitize (SANITIZE_ARG, (const char *) this);
100 inline const unsigned int get_feature_index (unsigned int i) const { return featureIndex[i]; }
101 inline unsigned int get_feature_count (void) const { return featureIndex.len; }
103 inline bool has_required_feature (void) const { return reqFeatureIndex != 0xffff; }
104 inline int get_required_feature_index (void) const
106 if (reqFeatureIndex == 0xffff)
108 return reqFeatureIndex;;
111 inline bool sanitize (SANITIZE_ARG_DEF) {
112 return SANITIZE_SELF () && SANITIZE (featureIndex);
115 Offset lookupOrder; /* = Null (reserved for an offset to a
116 * reordering table) */
117 USHORT reqFeatureIndex;/* Index of a feature required for this
118 * language system--if no required features
121 featureIndex; /* Array of indices into the FeatureList */
123 ASSERT_SIZE_DATA (LangSys, 6, "\0\0\xFF\xFF");
128 inline const LangSys& get_lang_sys (unsigned int i) const
130 if (i == NO_INDEX) return get_default_lang_sys ();
131 return this+langSys[i].offset;
133 inline unsigned int get_lang_sys_count (void) const { return langSys.len; }
134 inline const Tag& get_lang_sys_tag (unsigned int i) const { return langSys[i].tag; }
136 // LONGTERMTODO bsearch
137 DEFINE_TAG_FIND_INTERFACE (LangSys, lang_sys); /* find_lang_sys_index (), get_lang_sys_by_tag (tag) */
139 inline bool has_default_lang_sys (void) const { return defaultLangSys != 0; }
140 inline const LangSys& get_default_lang_sys (void) const { return this+defaultLangSys; }
142 inline bool sanitize (SANITIZE_ARG_DEF) {
143 return SANITIZE_THIS (defaultLangSys) && SANITIZE_THIS (langSys);
148 defaultLangSys; /* Offset to DefaultLangSys table--from
149 * beginning of Script table--may be Null */
150 RecordArrayOf<LangSys>
151 langSys; /* Array of LangSysRecords--listed
152 * alphabetically by LangSysTag */
154 ASSERT_SIZE (Script, 4);
156 typedef RecordListOf<Script> ScriptList;
157 ASSERT_SIZE (ScriptList, 2);
162 inline const unsigned int get_lookup_index (unsigned int i) const { return lookupIndex[i]; }
163 inline unsigned int get_lookup_count (void) const { return lookupIndex.len; }
165 inline bool sanitize (SANITIZE_ARG_DEF) {
166 return SANITIZE_SELF () && SANITIZE (lookupIndex);
169 /* TODO: implement get_feature_parameters() */
170 /* TODO: implement FeatureSize and other special features? */
171 Offset featureParams; /* Offset to Feature Parameters table (if one
172 * has been defined for the feature), relative
173 * to the beginning of the Feature Table; = Null
176 lookupIndex; /* Array of LookupList indices */
178 ASSERT_SIZE (Feature, 4);
180 typedef RecordListOf<Feature> FeatureList;
181 ASSERT_SIZE (FeatureList, 2);
184 struct LookupFlag : USHORT
187 RightToLeft = 0x0001u,
188 IgnoreBaseGlyphs = 0x0002u,
189 IgnoreLigatures = 0x0004u,
190 IgnoreMarks = 0x0008u,
191 UseMarkFilteringSet = 0x0010u,
193 MarkAttachmentType = 0xFF00u,
196 ASSERT_SIZE (LookupFlag, 2);
198 struct LookupSubTable
200 inline bool sanitize (SANITIZE_ARG_DEF) {
201 return SANITIZE_SELF ();
205 USHORT format; /* Subtable format. Different for GSUB and GPOS */
207 ASSERT_SIZE (LookupSubTable, 2);
211 inline const LookupSubTable& get_subtable (unsigned int i) const { return this+subTable[i]; }
212 inline unsigned int get_subtable_count (void) const { return subTable.len; }
214 inline unsigned int get_type (void) const { return lookupType; }
215 inline unsigned int get_flag (void) const
217 unsigned int flag = lookupFlag;
218 if (HB_UNLIKELY (flag & LookupFlag::UseMarkFilteringSet))
220 const USHORT &markFilteringSet = *(const USHORT*)
221 ((const char *) &subTable + subTable.get_size ());
222 flag += (markFilteringSet << 16);
227 inline bool sanitize (SANITIZE_ARG_DEF) {
228 if (!(SANITIZE_SELF () && SANITIZE_THIS (subTable))) return false;
229 if (HB_UNLIKELY (lookupFlag & LookupFlag::UseMarkFilteringSet))
231 USHORT &markFilteringSet = *(USHORT*) ((char *) &subTable + subTable.get_size ());
232 if (!SANITIZE (markFilteringSet)) return false;
237 USHORT lookupType; /* Different enumerations for GSUB and GPOS */
238 USHORT lookupFlag; /* Lookup qualifiers */
239 OffsetArrayOf<LookupSubTable>
240 subTable; /* Array of SubTables */
241 USHORT markFilteringSetX[0]; /* Index (base 0) into GDEF mark glyph sets
242 * structure. This field is only present if bit
243 * UseMarkFilteringSet of lookup flags is set. */
245 ASSERT_SIZE (Lookup, 6);
247 template <typename Type>
248 struct OffsetListOf : OffsetArrayOf<Type>
250 inline const Type& operator [] (unsigned int i) const
252 if (HB_UNLIKELY (i >= this->len)) return Null(Type);
253 return this+this->array[i];
256 inline bool sanitize (SANITIZE_ARG_DEF) {
257 return OffsetArrayOf<Type>::sanitize (SANITIZE_ARG, (const char *) this);
261 typedef OffsetListOf<Lookup> LookupList;
262 ASSERT_SIZE (LookupList, 2);
269 struct CoverageFormat1
271 friend struct Coverage;
274 inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
276 if (HB_UNLIKELY (glyph_id > 0xFFFF))
281 unsigned int num_glyphs = glyphArray.len;
282 for (unsigned int i = 0; i < num_glyphs; i++)
283 if (gid == glyphArray[i])
288 inline bool sanitize (SANITIZE_ARG_DEF) {
289 return SANITIZE (glyphArray);
293 USHORT coverageFormat; /* Format identifier--format = 1 */
295 glyphArray; /* Array of GlyphIDs--in numerical order */
297 ASSERT_SIZE (CoverageFormat1, 4);
299 struct CoverageRangeRecord
301 friend struct CoverageFormat2;
304 inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
306 if (glyph_id >= start && glyph_id <= end)
307 return (unsigned int) startCoverageIndex + (glyph_id - start);
312 inline bool sanitize (SANITIZE_ARG_DEF) {
313 return SANITIZE_SELF ();
317 GlyphID start; /* First GlyphID in the range */
318 GlyphID end; /* Last GlyphID in the range */
319 USHORT startCoverageIndex; /* Coverage Index of first GlyphID in
322 ASSERT_SIZE_DATA (CoverageRangeRecord, 6, "\000\001");
324 struct CoverageFormat2
326 friend struct Coverage;
329 inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
332 unsigned int count = rangeRecord.len;
333 for (unsigned int i = 0; i < count; i++)
335 unsigned int coverage = rangeRecord[i].get_coverage (glyph_id);
336 if (coverage != NOT_COVERED)
342 inline bool sanitize (SANITIZE_ARG_DEF) {
343 return SANITIZE (rangeRecord);
347 USHORT coverageFormat; /* Format identifier--format = 2 */
348 ArrayOf<CoverageRangeRecord>
349 rangeRecord; /* Array of glyph ranges--ordered by
350 * Start GlyphID. rangeCount entries
353 ASSERT_SIZE (CoverageFormat2, 4);
357 inline unsigned int operator() (hb_codepoint_t glyph_id) const { return get_coverage (glyph_id); }
359 unsigned int get_coverage (hb_codepoint_t glyph_id) const
362 case 1: return u.format1->get_coverage(glyph_id);
363 case 2: return u.format2->get_coverage(glyph_id);
364 default:return NOT_COVERED;
368 inline bool sanitize (SANITIZE_ARG_DEF) {
369 if (!SANITIZE (u.format)) return false;
371 case 1: return u.format1->sanitize (SANITIZE_ARG);
372 case 2: return u.format2->sanitize (SANITIZE_ARG);
379 USHORT format; /* Format identifier */
380 CoverageFormat1 format1[];
381 CoverageFormat2 format2[];
384 ASSERT_SIZE (Coverage, 2);
388 * Class Definition Table
391 struct ClassDefFormat1
393 friend struct ClassDef;
396 inline hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
398 if ((unsigned int) (glyph_id - startGlyph) < classValue.len)
399 return classValue[glyph_id - startGlyph];
403 inline bool sanitize (SANITIZE_ARG_DEF) {
404 return SANITIZE_SELF () && SANITIZE (classValue);
407 USHORT classFormat; /* Format identifier--format = 1 */
408 GlyphID startGlyph; /* First GlyphID of the classValueArray */
410 classValue; /* Array of Class Values--one per GlyphID */
412 ASSERT_SIZE (ClassDefFormat1, 6);
414 struct ClassRangeRecord
416 friend struct ClassDefFormat2;
419 inline hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
421 if (glyph_id >= start && glyph_id <= end)
427 inline bool sanitize (SANITIZE_ARG_DEF) {
428 return SANITIZE_SELF ();
432 GlyphID start; /* First GlyphID in the range */
433 GlyphID end; /* Last GlyphID in the range */
434 USHORT classValue; /* Applied to all glyphs in the range */
436 ASSERT_SIZE_DATA (ClassRangeRecord, 6, "\000\001");
438 struct ClassDefFormat2
440 friend struct ClassDef;
443 inline hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
446 unsigned int count = rangeRecord.len;
447 for (unsigned int i = 0; i < count; i++)
449 int classValue = rangeRecord[i].get_class (glyph_id);
456 inline bool sanitize (SANITIZE_ARG_DEF) {
457 return SANITIZE (rangeRecord);
460 USHORT classFormat; /* Format identifier--format = 2 */
461 ArrayOf<ClassRangeRecord>
462 rangeRecord; /* Array of glyph ranges--ordered by
465 ASSERT_SIZE (ClassDefFormat2, 4);
469 inline unsigned int operator() (hb_codepoint_t glyph_id) const { return get_class (glyph_id); }
471 hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
474 case 1: return u.format1->get_class(glyph_id);
475 case 2: return u.format2->get_class(glyph_id);
480 inline bool sanitize (SANITIZE_ARG_DEF) {
481 if (!SANITIZE (u.format)) return false;
483 case 1: return u.format1->sanitize (SANITIZE_ARG);
484 case 2: return u.format2->sanitize (SANITIZE_ARG);
491 USHORT format; /* Format identifier */
492 ClassDefFormat1 format1[];
493 ClassDefFormat2 format2[];
496 ASSERT_SIZE (ClassDef, 2);
505 int get_delta (unsigned int ppem_size) const
507 unsigned int f = deltaFormat;
508 if (HB_UNLIKELY (f < 1 || f > 3))
511 if (ppem_size < startSize || ppem_size > endSize)
514 unsigned int s = ppem_size - startSize;
516 unsigned int byte = deltaValue[s >> (4 - f)];
517 unsigned int bits = (byte >> (16 - (((s & ((1 << (4 - f)) - 1)) + 1) << f)));
518 unsigned int mask = (0xFFFF >> (16 - (1 << f)));
520 int delta = bits & mask;
522 if (delta >= ((mask + 1) >> 1))
528 inline int operator() (unsigned int ppem_size) const { return get_delta (ppem_size); }
531 USHORT startSize; /* Smallest size to correct--in ppem */
532 USHORT endSize; /* Largest size to correct--in ppem */
533 USHORT deltaFormat; /* Format of DeltaValue array data: 1, 2, or 3 */
534 USHORT deltaValue[]; /* Array of compressed data */
536 ASSERT_SIZE (Device, 6);
539 #endif /* HB_OT_LAYOUT_COMMON_PRIVATE_HH */