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: Only accept ASCII-visible tags (mind DEL)
51 * This is one of the few times (only time?) we check
52 * for data integrity, as opposed o just boundary checks
54 return (tag & 0x80808080) == 0 && offset.sanitize (SANITIZE_ARG, base);
57 Tag tag; /* 4-byte Tag identifier */
59 offset; /* Offset from beginning of object holding
63 template <typename Type>
64 struct RecordArrayOf : ArrayOf<Record<Type> > {};
66 template <typename Type>
67 struct RecordListOf : RecordArrayOf<Type>
69 inline const Type& operator [] (unsigned int i) const
71 if (HB_UNLIKELY (i >= this->len)) return Null(Type);
72 return this+this->array[i].offset;
74 inline const Tag& get_tag (unsigned int i) const
76 if (HB_UNLIKELY (i >= this->len)) return Null(Tag);
77 return this->array[i].tag;
80 inline bool sanitize (SANITIZE_ARG_DEF) {
81 return RecordArrayOf<Type>::sanitize (SANITIZE_ARG, (const char *) this);
93 inline const unsigned int get_feature_index (unsigned int i) const { return featureIndex[i]; }
94 inline unsigned int get_feature_count (void) const { return featureIndex.len; }
96 inline bool has_required_feature (void) const { return reqFeatureIndex != 0xffff; }
97 inline int get_required_feature_index (void) const
99 if (reqFeatureIndex == 0xffff)
101 return reqFeatureIndex;;
104 inline bool sanitize (SANITIZE_ARG_DEF) {
105 return SANITIZE_SELF () && SANITIZE (featureIndex);
108 Offset lookupOrder; /* = Null (reserved for an offset to a
109 * reordering table) */
110 USHORT reqFeatureIndex;/* Index of a feature required for this
111 * language system--if no required features
114 featureIndex; /* Array of indices into the FeatureList */
116 ASSERT_SIZE_DATA (LangSys, 6, "\0\0\xFF\xFF");
121 inline const LangSys& get_lang_sys (unsigned int i) const
123 if (i == NO_INDEX) return get_default_lang_sys ();
124 return this+langSys[i].offset;
126 inline unsigned int get_lang_sys_count (void) const { return langSys.len; }
127 inline const Tag& get_lang_sys_tag (unsigned int i) const { return langSys[i].tag; }
129 // LONGTERMTODO bsearch
130 DEFINE_TAG_FIND_INTERFACE (LangSys, lang_sys); /* find_lang_sys_index (), get_lang_sys_by_tag (tag) */
132 inline bool has_default_lang_sys (void) const { return defaultLangSys != 0; }
133 inline const LangSys& get_default_lang_sys (void) const { return this+defaultLangSys; }
135 inline bool sanitize (SANITIZE_ARG_DEF) {
136 return SANITIZE_THIS (defaultLangSys) && SANITIZE_THIS (langSys);
141 defaultLangSys; /* Offset to DefaultLangSys table--from
142 * beginning of Script table--may be Null */
143 RecordArrayOf<LangSys>
144 langSys; /* Array of LangSysRecords--listed
145 * alphabetically by LangSysTag */
147 ASSERT_SIZE (Script, 4);
149 typedef RecordListOf<Script> ScriptList;
150 ASSERT_SIZE (ScriptList, 2);
155 inline const unsigned int get_lookup_index (unsigned int i) const { return lookupIndex[i]; }
156 inline unsigned int get_lookup_count (void) const { return lookupIndex.len; }
158 inline bool sanitize (SANITIZE_ARG_DEF) {
159 return SANITIZE_SELF () && SANITIZE (lookupIndex);
162 /* TODO: implement get_feature_parameters() */
163 /* TODO: implement FeatureSize and other special features? */
164 Offset featureParams; /* Offset to Feature Parameters table (if one
165 * has been defined for the feature), relative
166 * to the beginning of the Feature Table; = Null
169 lookupIndex; /* Array of LookupList indices */
171 ASSERT_SIZE (Feature, 4);
173 typedef RecordListOf<Feature> FeatureList;
174 ASSERT_SIZE (FeatureList, 2);
177 struct LookupFlag : USHORT
180 RightToLeft = 0x0001u,
181 IgnoreBaseGlyphs = 0x0002u,
182 IgnoreLigatures = 0x0004u,
183 IgnoreMarks = 0x0008u,
184 UseMarkFilteringSet = 0x0010u,
186 MarkAttachmentType = 0xFF00u,
189 ASSERT_SIZE (LookupFlag, 2);
191 struct LookupSubTable
193 inline bool sanitize (SANITIZE_ARG_DEF) {
194 return SANITIZE_SELF ();
198 USHORT format; /* Subtable format. Different for GSUB and GPOS */
200 ASSERT_SIZE (LookupSubTable, 2);
204 inline const LookupSubTable& get_subtable (unsigned int i) const { return this+subTable[i]; }
205 inline unsigned int get_subtable_count (void) const { return subTable.len; }
207 inline unsigned int get_type (void) const { return lookupType; }
208 inline unsigned int get_flag (void) const
210 unsigned int flag = lookupFlag;
211 if (HB_UNLIKELY (flag & LookupFlag::UseMarkFilteringSet))
213 const USHORT &markFilteringSet = *(const USHORT*)
214 ((const char *) &subTable + subTable.get_size ());
215 flag += (markFilteringSet << 16);
220 inline bool sanitize (SANITIZE_ARG_DEF) {
221 if (!(SANITIZE_SELF () && SANITIZE_THIS (subTable))) return false;
222 if (HB_UNLIKELY (lookupFlag & LookupFlag::UseMarkFilteringSet))
224 USHORT &markFilteringSet = *(USHORT*) ((char *) &subTable + subTable.get_size ());
225 if (!SANITIZE (markFilteringSet)) return false;
230 USHORT lookupType; /* Different enumerations for GSUB and GPOS */
231 USHORT lookupFlag; /* Lookup qualifiers */
232 OffsetArrayOf<LookupSubTable>
233 subTable; /* Array of SubTables */
234 USHORT markFilteringSetX[0]; /* Index (base 0) into GDEF mark glyph sets
235 * structure. This field is only present if bit
236 * UseMarkFilteringSet of lookup flags is set. */
238 ASSERT_SIZE (Lookup, 6);
240 template <typename Type>
241 struct OffsetListOf : OffsetArrayOf<Type>
243 inline const Type& operator [] (unsigned int i) const
245 if (HB_UNLIKELY (i >= this->len)) return Null(Type);
246 return this+this->array[i];
249 inline bool sanitize (SANITIZE_ARG_DEF) {
250 return OffsetArrayOf<Type>::sanitize (SANITIZE_ARG, (const char *) this);
254 typedef OffsetListOf<Lookup> LookupList;
255 ASSERT_SIZE (LookupList, 2);
262 struct CoverageFormat1
264 friend struct Coverage;
267 inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
269 if (HB_UNLIKELY (glyph_id > 0xFFFF))
274 unsigned int num_glyphs = glyphArray.len;
275 for (unsigned int i = 0; i < num_glyphs; i++)
276 if (gid == glyphArray[i])
281 inline bool sanitize (SANITIZE_ARG_DEF) {
282 return SANITIZE (glyphArray);
286 USHORT coverageFormat; /* Format identifier--format = 1 */
288 glyphArray; /* Array of GlyphIDs--in numerical order */
290 ASSERT_SIZE (CoverageFormat1, 4);
292 struct CoverageRangeRecord
294 friend struct CoverageFormat2;
297 inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
299 if (glyph_id >= start && glyph_id <= end)
300 return (unsigned int) startCoverageIndex + (glyph_id - start);
305 inline bool sanitize (SANITIZE_ARG_DEF) {
306 return SANITIZE_SELF ();
310 GlyphID start; /* First GlyphID in the range */
311 GlyphID end; /* Last GlyphID in the range */
312 USHORT startCoverageIndex; /* Coverage Index of first GlyphID in
315 ASSERT_SIZE_DATA (CoverageRangeRecord, 6, "\000\001");
317 struct CoverageFormat2
319 friend struct Coverage;
322 inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
325 unsigned int count = rangeRecord.len;
326 for (unsigned int i = 0; i < count; i++)
328 unsigned int coverage = rangeRecord[i].get_coverage (glyph_id);
329 if (coverage != NOT_COVERED)
335 inline bool sanitize (SANITIZE_ARG_DEF) {
336 return SANITIZE (rangeRecord);
340 USHORT coverageFormat; /* Format identifier--format = 2 */
341 ArrayOf<CoverageRangeRecord>
342 rangeRecord; /* Array of glyph ranges--ordered by
343 * Start GlyphID. rangeCount entries
346 ASSERT_SIZE (CoverageFormat2, 4);
350 inline unsigned int operator() (hb_codepoint_t glyph_id) const { return get_coverage (glyph_id); }
352 unsigned int get_coverage (hb_codepoint_t glyph_id) const
355 case 1: return u.format1->get_coverage(glyph_id);
356 case 2: return u.format2->get_coverage(glyph_id);
357 default:return NOT_COVERED;
361 inline bool sanitize (SANITIZE_ARG_DEF) {
362 if (!SANITIZE (u.format)) return false;
364 case 1: return u.format1->sanitize (SANITIZE_ARG);
365 case 2: return u.format2->sanitize (SANITIZE_ARG);
372 USHORT format; /* Format identifier */
373 CoverageFormat1 format1[];
374 CoverageFormat2 format2[];
377 ASSERT_SIZE (Coverage, 2);
381 * Class Definition Table
384 struct ClassDefFormat1
386 friend struct ClassDef;
389 inline hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
391 if ((unsigned int) (glyph_id - startGlyph) < classValue.len)
392 return classValue[glyph_id - startGlyph];
396 inline bool sanitize (SANITIZE_ARG_DEF) {
397 return SANITIZE_SELF () && SANITIZE (classValue);
400 USHORT classFormat; /* Format identifier--format = 1 */
401 GlyphID startGlyph; /* First GlyphID of the classValueArray */
403 classValue; /* Array of Class Values--one per GlyphID */
405 ASSERT_SIZE (ClassDefFormat1, 6);
407 struct ClassRangeRecord
409 friend struct ClassDefFormat2;
412 inline hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
414 if (glyph_id >= start && glyph_id <= end)
420 inline bool sanitize (SANITIZE_ARG_DEF) {
421 return SANITIZE_SELF ();
425 GlyphID start; /* First GlyphID in the range */
426 GlyphID end; /* Last GlyphID in the range */
427 USHORT classValue; /* Applied to all glyphs in the range */
429 ASSERT_SIZE_DATA (ClassRangeRecord, 6, "\000\001");
431 struct ClassDefFormat2
433 friend struct ClassDef;
436 inline hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
439 unsigned int count = rangeRecord.len;
440 for (unsigned int i = 0; i < count; i++)
442 int classValue = rangeRecord[i].get_class (glyph_id);
449 inline bool sanitize (SANITIZE_ARG_DEF) {
450 return SANITIZE (rangeRecord);
453 USHORT classFormat; /* Format identifier--format = 2 */
454 ArrayOf<ClassRangeRecord>
455 rangeRecord; /* Array of glyph ranges--ordered by
458 ASSERT_SIZE (ClassDefFormat2, 4);
462 inline unsigned int operator() (hb_codepoint_t glyph_id) const { return get_class (glyph_id); }
464 hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
467 case 1: return u.format1->get_class(glyph_id);
468 case 2: return u.format2->get_class(glyph_id);
473 inline bool sanitize (SANITIZE_ARG_DEF) {
474 if (!SANITIZE (u.format)) return false;
476 case 1: return u.format1->sanitize (SANITIZE_ARG);
477 case 2: return u.format2->sanitize (SANITIZE_ARG);
484 USHORT format; /* Format identifier */
485 ClassDefFormat1 format1[];
486 ClassDefFormat2 format2[];
489 ASSERT_SIZE (ClassDef, 2);
498 int get_delta (unsigned int ppem_size) const
500 unsigned int f = deltaFormat;
501 if (HB_UNLIKELY (f < 1 || f > 3))
504 if (ppem_size < startSize || ppem_size > endSize)
507 unsigned int s = ppem_size - startSize;
509 unsigned int byte = deltaValue[s >> (4 - f)];
510 unsigned int bits = (byte >> (16 - (((s & ((1 << (4 - f)) - 1)) + 1) << f)));
511 unsigned int mask = (0xFFFF >> (16 - (1 << f)));
513 int delta = bits & mask;
515 if (delta >= ((mask + 1) >> 1))
521 inline int operator() (unsigned int ppem_size) const { return get_delta (ppem_size); }
524 USHORT startSize; /* Smallest size to correct--in ppem */
525 USHORT endSize; /* Largest size to correct--in ppem */
526 USHORT deltaFormat; /* Format of DeltaValue array data: 1, 2, or 3 */
527 USHORT deltaValue[]; /* Array of compressed data */
529 ASSERT_SIZE (Device, 6);
532 #endif /* HB_OT_LAYOUT_COMMON_PRIVATE_HH */