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-type-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 void *base) {
51 return SANITIZE (tag) && SANITIZE_BASE (offset, base);
54 Tag tag; /* 4-byte Tag identifier */
56 offset; /* Offset from beginning of object holding
60 template <typename Type>
61 struct RecordArrayOf : ArrayOf<Record<Type> > {};
63 template <typename Type>
64 struct RecordListOf : RecordArrayOf<Type>
66 inline const Type& operator [] (unsigned int i) const
68 if (HB_UNLIKELY (i >= this->len)) return Null(Type);
69 return this+this->array[i].offset;
71 inline const Tag& get_tag (unsigned int i) const
73 if (HB_UNLIKELY (i >= this->len)) return Null(Tag);
74 return this->array[i].tag;
77 inline bool sanitize (SANITIZE_ARG_DEF) {
79 return RecordArrayOf<Type>::sanitize (SANITIZE_ARG, CONST_CHARP(this));
91 inline unsigned int get_feature_index (unsigned int i) const { return featureIndex[i]; }
92 inline unsigned int get_feature_count (void) const { return featureIndex.len; }
94 inline bool has_required_feature (void) const { return reqFeatureIndex != 0xffff; }
95 inline int get_required_feature_index (void) const
97 if (reqFeatureIndex == 0xffff)
99 return reqFeatureIndex;;
102 inline bool sanitize (SANITIZE_ARG_DEF) {
104 return SANITIZE_SELF () && SANITIZE (featureIndex);
107 Offset lookupOrder; /* = Null (reserved for an offset to a
108 * reordering table) */
109 USHORT reqFeatureIndex;/* Index of a feature required for this
110 * language system--if no required features
113 featureIndex; /* Array of indices into the FeatureList */
115 ASSERT_SIZE_DATA (LangSys, 6, "\0\0\xFF\xFF");
120 inline const LangSys& get_lang_sys (unsigned int i) const
122 if (i == NO_INDEX) return get_default_lang_sys ();
123 return this+langSys[i].offset;
125 inline unsigned int get_lang_sys_count (void) const { return langSys.len; }
126 inline const Tag& get_lang_sys_tag (unsigned int i) const { return langSys[i].tag; }
128 // LONGTERMTODO bsearch
129 DEFINE_TAG_FIND_INTERFACE (LangSys, lang_sys); /* find_lang_sys_index (), get_lang_sys_by_tag (tag) */
131 inline bool has_default_lang_sys (void) const { return defaultLangSys != 0; }
132 inline const LangSys& get_default_lang_sys (void) const { return this+defaultLangSys; }
134 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 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) {
160 return SANITIZE_SELF () && SANITIZE (lookupIndex);
163 /* TODO: implement get_feature_parameters() */
164 /* TODO: implement FeatureSize and other special features? */
165 Offset featureParams; /* Offset to Feature Parameters table (if one
166 * has been defined for the feature), relative
167 * to the beginning of the Feature Table; = Null
170 lookupIndex; /* Array of LookupList indices */
172 ASSERT_SIZE (Feature, 4);
174 typedef RecordListOf<Feature> FeatureList;
175 ASSERT_SIZE (FeatureList, 2);
178 struct LookupFlag : USHORT
181 RightToLeft = 0x0001u,
182 IgnoreBaseGlyphs = 0x0002u,
183 IgnoreLigatures = 0x0004u,
184 IgnoreMarks = 0x0008u,
185 UseMarkFilteringSet = 0x0010u,
187 MarkAttachmentType = 0xFF00u,
190 ASSERT_SIZE (LookupFlag, 2);
192 struct LookupSubTable
194 inline bool sanitize (SANITIZE_ARG_DEF) {
196 return SANITIZE_SELF ();
200 USHORT format; /* Subtable format. Different for GSUB and GPOS */
202 ASSERT_SIZE (LookupSubTable, 2);
206 inline const LookupSubTable& get_subtable (unsigned int i) const { return this+subTable[i]; }
207 inline unsigned int get_subtable_count (void) const { return subTable.len; }
209 inline unsigned int get_type (void) const { return lookupType; }
210 inline unsigned int get_flag (void) const
212 unsigned int flag = lookupFlag;
213 if (HB_UNLIKELY (flag & LookupFlag::UseMarkFilteringSet))
215 const USHORT &markFilteringSet = CONST_CAST (USHORT, subTable, subTable.get_size ());
216 flag += (markFilteringSet << 16);
221 inline bool sanitize (SANITIZE_ARG_DEF) {
223 if (!(SANITIZE_SELF () && SANITIZE_THIS (subTable))) return false;
224 if (HB_UNLIKELY (lookupFlag & LookupFlag::UseMarkFilteringSet))
226 USHORT &markFilteringSet = CAST (USHORT, subTable, subTable.get_size ());
227 if (!SANITIZE (markFilteringSet)) return false;
232 USHORT lookupType; /* Different enumerations for GSUB and GPOS */
233 USHORT lookupFlag; /* Lookup qualifiers */
234 OffsetArrayOf<LookupSubTable>
235 subTable; /* Array of SubTables */
236 USHORT markFilteringSetX[0]; /* Index (base 0) into GDEF mark glyph sets
237 * structure. This field is only present if bit
238 * UseMarkFilteringSet of lookup flags is set. */
240 ASSERT_SIZE (Lookup, 6);
242 template <typename Type>
243 struct OffsetListOf : OffsetArrayOf<Type>
245 inline const Type& operator [] (unsigned int i) const
247 if (HB_UNLIKELY (i >= this->len)) return Null(Type);
248 return this+this->array[i];
251 inline bool sanitize (SANITIZE_ARG_DEF) {
253 return OffsetArrayOf<Type>::sanitize (SANITIZE_ARG, CONST_CHARP(this));
257 typedef OffsetListOf<Lookup> LookupList;
258 ASSERT_SIZE (LookupList, 2);
265 struct CoverageFormat1
267 friend struct Coverage;
270 inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
272 if (HB_UNLIKELY (glyph_id > 0xFFFF))
277 unsigned int num_glyphs = glyphArray.len;
278 for (unsigned int i = 0; i < num_glyphs; i++)
279 if (gid == glyphArray[i])
284 inline bool sanitize (SANITIZE_ARG_DEF) {
286 return SANITIZE (glyphArray);
290 USHORT coverageFormat; /* Format identifier--format = 1 */
292 glyphArray; /* Array of GlyphIDs--in numerical order */
294 ASSERT_SIZE (CoverageFormat1, 4);
296 struct CoverageRangeRecord
298 friend struct CoverageFormat2;
301 inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
303 if (glyph_id >= start && glyph_id <= end)
304 return (unsigned int) startCoverageIndex + (glyph_id - start);
309 inline bool sanitize (SANITIZE_ARG_DEF) {
311 return SANITIZE_SELF ();
315 GlyphID start; /* First GlyphID in the range */
316 GlyphID end; /* Last GlyphID in the range */
317 USHORT startCoverageIndex; /* Coverage Index of first GlyphID in
320 ASSERT_SIZE_DATA (CoverageRangeRecord, 6, "\000\001");
322 struct CoverageFormat2
324 friend struct Coverage;
327 inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
330 unsigned int count = rangeRecord.len;
331 for (unsigned int i = 0; i < count; i++)
333 unsigned int coverage = rangeRecord[i].get_coverage (glyph_id);
334 if (coverage != NOT_COVERED)
340 inline bool sanitize (SANITIZE_ARG_DEF) {
342 return SANITIZE (rangeRecord);
346 USHORT coverageFormat; /* Format identifier--format = 2 */
347 ArrayOf<CoverageRangeRecord>
348 rangeRecord; /* Array of glyph ranges--ordered by
349 * Start GlyphID. rangeCount entries
352 ASSERT_SIZE (CoverageFormat2, 4);
356 inline unsigned int operator() (hb_codepoint_t glyph_id) const { return get_coverage (glyph_id); }
358 unsigned int get_coverage (hb_codepoint_t glyph_id) const
361 case 1: return u.format1->get_coverage(glyph_id);
362 case 2: return u.format2->get_coverage(glyph_id);
363 default:return NOT_COVERED;
367 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) {
405 return SANITIZE_SELF () && SANITIZE (classValue);
408 USHORT classFormat; /* Format identifier--format = 1 */
409 GlyphID startGlyph; /* First GlyphID of the classValueArray */
411 classValue; /* Array of Class Values--one per GlyphID */
413 ASSERT_SIZE (ClassDefFormat1, 6);
415 struct ClassRangeRecord
417 friend struct ClassDefFormat2;
420 inline hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
422 if (glyph_id >= start && glyph_id <= end)
428 inline bool sanitize (SANITIZE_ARG_DEF) {
430 return SANITIZE_SELF ();
434 GlyphID start; /* First GlyphID in the range */
435 GlyphID end; /* Last GlyphID in the range */
436 USHORT classValue; /* Applied to all glyphs in the range */
438 ASSERT_SIZE_DATA (ClassRangeRecord, 6, "\000\001");
440 struct ClassDefFormat2
442 friend struct ClassDef;
445 inline hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
448 unsigned int count = rangeRecord.len;
449 for (unsigned int i = 0; i < count; i++)
451 int classValue = rangeRecord[i].get_class (glyph_id);
458 inline bool sanitize (SANITIZE_ARG_DEF) {
460 return SANITIZE (rangeRecord);
463 USHORT classFormat; /* Format identifier--format = 2 */
464 ArrayOf<ClassRangeRecord>
465 rangeRecord; /* Array of glyph ranges--ordered by
468 ASSERT_SIZE (ClassDefFormat2, 4);
472 inline unsigned int operator() (hb_codepoint_t glyph_id) const { return get_class (glyph_id); }
474 hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
477 case 1: return u.format1->get_class(glyph_id);
478 case 2: return u.format2->get_class(glyph_id);
483 bool sanitize (SANITIZE_ARG_DEF) {
485 if (!SANITIZE (u.format)) return false;
487 case 1: return u.format1->sanitize (SANITIZE_ARG);
488 case 2: return u.format2->sanitize (SANITIZE_ARG);
495 USHORT format; /* Format identifier */
496 ClassDefFormat1 format1[];
497 ClassDefFormat2 format2[];
500 ASSERT_SIZE (ClassDef, 2);
509 inline int operator() (unsigned int ppem_size) const { return get_delta (ppem_size); }
511 int get_delta (unsigned int ppem_size) const
513 unsigned int f = deltaFormat;
514 if (HB_UNLIKELY (f < 1 || f > 3))
517 if (ppem_size < startSize || ppem_size > endSize)
520 unsigned int s = ppem_size - startSize;
522 unsigned int byte = deltaValue[s >> (4 - f)];
523 unsigned int bits = (byte >> (16 - (((s & ((1 << (4 - f)) - 1)) + 1) << f)));
524 unsigned int mask = (0xFFFF >> (16 - (1 << f)));
526 int delta = bits & mask;
528 if ((unsigned int) delta >= ((mask + 1) >> 1))
534 inline unsigned int get_size () const
536 unsigned int f = deltaFormat;
537 if (HB_UNLIKELY (f < 1 || f > 3 || startSize > endSize)) return sizeof (*this);
538 return sizeof (*this) + ((endSize - startSize + (1 << (4 - f)) - 1) >> (4 - f));
541 bool sanitize (SANITIZE_ARG_DEF) {
543 return SANITIZE_GET_SIZE ();
547 USHORT startSize; /* Smallest size to correct--in ppem */
548 USHORT endSize; /* Largest size to correct--in ppem */
549 USHORT deltaFormat; /* Format of DeltaValue array data: 1, 2, or 3 */
550 USHORT deltaValue[]; /* Array of compressed data */
552 ASSERT_SIZE (Device, 6);
555 #endif /* HB_OT_LAYOUT_COMMON_PRIVATE_HH */