blob: fabb3a2a09cbe935598a1a9792e94836d93b0405 [file] [log] [blame]
Behdad Esfahbod6f20f722009-05-17 20:28:01 -04001/*
2 * Copyright (C) 2007,2008,2009 Red Hat, Inc.
3 *
4 * This is part of HarfBuzz, an OpenType Layout engine library.
5 *
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.
11 *
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
16 * DAMAGE.
17 *
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.
23 *
24 * Red Hat Author(s): Behdad Esfahbod
25 */
26
Behdad Esfahbod5f5b24f2009-08-02 20:03:12 -040027#ifndef HB_OT_LAYOUT_COMMON_PRIVATE_HH
28#define HB_OT_LAYOUT_COMMON_PRIVATE_HH
Behdad Esfahbod6f20f722009-05-17 20:28:01 -040029
Behdad Esfahbod2098a022009-08-02 19:57:00 -040030#include "hb-ot-layout-private.h"
31
Behdad Esfahbod7edb4302009-08-04 22:06:57 -040032#include "hb-open-type-private.hh"
Behdad Esfahbod6f20f722009-05-17 20:28:01 -040033
34
35/*
36 *
37 * OpenType Layout Common Table Formats
38 *
39 */
40
Behdad Esfahbod2e8fb6c2009-05-18 04:37:37 -040041
Behdad Esfahbod6f20f722009-05-17 20:28:01 -040042/*
43 * Script, ScriptList, LangSys, Feature, FeatureList, Lookup, LookupList
44 */
45
46template <typename Type>
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -040047struct Record
48{
Behdad Esfahbod2b5a59c2009-08-04 11:38:50 -040049 inline bool sanitize (SANITIZE_ARG_DEF, const void *base) {
Behdad Esfahbodb28815c2009-08-04 22:35:36 -040050 SANITIZE_DEBUG ();
Behdad Esfahbod62ed5852009-08-04 23:30:32 -040051 return SANITIZE (tag) && SANITIZE_BASE (offset, base);
Behdad Esfahbodcd3827e2009-08-04 02:09:34 -040052 }
53
Behdad Esfahbod6f20f722009-05-17 20:28:01 -040054 Tag tag; /* 4-byte Tag identifier */
55 OffsetTo<Type>
56 offset; /* Offset from beginning of object holding
57 * the Record */
58};
59
60template <typename Type>
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -040061struct RecordArrayOf : ArrayOf<Record<Type> > {
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -040062 inline const Tag& get_tag (unsigned int i) const
63 {
Behdad Esfahbod6f20f722009-05-17 20:28:01 -040064 if (HB_UNLIKELY (i >= this->len)) return Null(Tag);
65 return this->array[i].tag;
66 }
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -040067 inline bool get_tags (unsigned int *record_count /* IN/OUT */,
68 hb_tag_t *record_tags /* OUT */) const
69 {
70 unsigned int count = MIN (this->len, *record_count);
71 for (unsigned int i = 0; i < count; i++)
72 record_tags[i] = this->array[i].tag;
73
74 *record_count = this->len;
75 return !!this->len;
76 }
77 inline bool find_index (hb_tag_t tag, unsigned int *index) const
78 {
79 const Tag t = tag;
80 // TODO bsearch
81 unsigned int count = this->len;
82 for (unsigned int i = 0; i < count; i++)
83 {
84 if (t == this->array[i].tag)
85 {
86 if (index) *index = i;
87 return true;
88 }
89 }
90 if (index) *index = NO_INDEX;
91 return false;
92 }
93};
94
95template <typename Type>
96struct RecordListOf : RecordArrayOf<Type>
97{
98 inline const Type& operator [] (unsigned int i) const
99 { return this+RecordArrayOf<Type>::operator[](i).offset; }
Behdad Esfahbodcd3827e2009-08-04 02:09:34 -0400100
101 inline bool sanitize (SANITIZE_ARG_DEF) {
Behdad Esfahbodb28815c2009-08-04 22:35:36 -0400102 SANITIZE_DEBUG ();
Behdad Esfahbod196598b2009-08-04 11:04:32 -0400103 return RecordArrayOf<Type>::sanitize (SANITIZE_ARG, CONST_CHARP(this));
Behdad Esfahbodcd3827e2009-08-04 02:09:34 -0400104 }
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400105};
106
107
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -0400108struct IndexArray : ArrayOf<USHORT>
109{
110 inline unsigned int operator [] (unsigned int i) const
111 {
112 if (HB_UNLIKELY (i >= this->len))
113 return NO_INDEX;
114 return this->array[i];
115 }
116 inline bool get_indexes (unsigned int *_count /* IN/OUT */,
117 unsigned int *_indexes /* OUT */) const
118 {
119 unsigned int count = MIN (this->len, *_count);
120 for (unsigned int i = 0; i < count; i++)
121 _indexes[i] = this->array[i];
122
123 *_count = this->len;
124 return !!this->len;
125 }
126};
127
128
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400129struct Script;
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400130struct LangSys;
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400131struct Feature;
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400132
133
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400134struct LangSys
135{
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -0400136 inline unsigned int get_feature_count (void) const
137 { return featureIndex.len; }
138 inline hb_tag_t get_feature_index (unsigned int i) const
139 { return featureIndex[i]; }
140 inline bool get_feature_indexes (unsigned int *feature_count /* IN/OUT */,
141 hb_tag_t *feature_tags /* OUT */) const
142 { return featureIndex.get_indexes (feature_count, feature_tags); }
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400143
Behdad Esfahbodcc6c6442009-05-25 03:10:06 -0400144 inline bool has_required_feature (void) const { return reqFeatureIndex != 0xffff; }
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400145 inline int get_required_feature_index (void) const
146 {
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400147 if (reqFeatureIndex == 0xffff)
148 return NO_INDEX;
149 return reqFeatureIndex;;
150 }
151
Behdad Esfahbodcd3827e2009-08-04 02:09:34 -0400152 inline bool sanitize (SANITIZE_ARG_DEF) {
Behdad Esfahbodb28815c2009-08-04 22:35:36 -0400153 SANITIZE_DEBUG ();
Behdad Esfahbodcd3827e2009-08-04 02:09:34 -0400154 return SANITIZE_SELF () && SANITIZE (featureIndex);
155 }
156
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400157 Offset lookupOrder; /* = Null (reserved for an offset to a
158 * reordering table) */
159 USHORT reqFeatureIndex;/* Index of a feature required for this
160 * language system--if no required features
161 * = 0xFFFF */
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -0400162 IndexArray featureIndex; /* Array of indices into the FeatureList */
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400163};
164ASSERT_SIZE_DATA (LangSys, 6, "\0\0\xFF\xFF");
165
166
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400167struct Script
168{
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -0400169 inline unsigned int get_lang_sys_count (void) const
170 { return langSys.len; }
171 inline const Tag& get_lang_sys_tag (unsigned int i) const
172 { return langSys.get_tag (i); }
173 inline bool get_lang_sys_tags (unsigned int *lang_sys_count /* IN/OUT */,
174 hb_tag_t *lang_sys_tags /* OUT */) const
175 { return langSys.get_tags (lang_sys_count, lang_sys_tags); }
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400176 inline const LangSys& get_lang_sys (unsigned int i) const
177 {
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400178 if (i == NO_INDEX) return get_default_lang_sys ();
179 return this+langSys[i].offset;
180 }
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -0400181 inline bool find_lang_sys_index (hb_tag_t tag, unsigned int *index) const
182 { return langSys.find_index (tag, index); }
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400183
Behdad Esfahbodcc6c6442009-05-25 03:10:06 -0400184 inline bool has_default_lang_sys (void) const { return defaultLangSys != 0; }
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400185 inline const LangSys& get_default_lang_sys (void) const { return this+defaultLangSys; }
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400186
Behdad Esfahbodcd3827e2009-08-04 02:09:34 -0400187 inline bool sanitize (SANITIZE_ARG_DEF) {
Behdad Esfahbodb28815c2009-08-04 22:35:36 -0400188 SANITIZE_DEBUG ();
Behdad Esfahbodcd3827e2009-08-04 02:09:34 -0400189 return SANITIZE_THIS (defaultLangSys) && SANITIZE_THIS (langSys);
190 }
191
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400192 private:
193 OffsetTo<LangSys>
194 defaultLangSys; /* Offset to DefaultLangSys table--from
195 * beginning of Script table--may be Null */
Behdad Esfahbodcd3827e2009-08-04 02:09:34 -0400196 RecordArrayOf<LangSys>
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400197 langSys; /* Array of LangSysRecords--listed
198 * alphabetically by LangSysTag */
199};
200ASSERT_SIZE (Script, 4);
201
202typedef RecordListOf<Script> ScriptList;
203ASSERT_SIZE (ScriptList, 2);
204
205
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400206struct Feature
207{
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -0400208 inline unsigned int get_lookup_count (void) const
209 { return lookupIndex.len; }
210 inline hb_tag_t get_lookup_index (unsigned int i) const
211 { return lookupIndex[i]; }
212 inline bool get_lookup_indexes (unsigned int *lookup_count /* IN/OUT */,
213 hb_tag_t *lookup_tags /* OUT */) const
214 { return lookupIndex.get_indexes (lookup_count, lookup_tags); }
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400215
Behdad Esfahbodcd3827e2009-08-04 02:09:34 -0400216 inline bool sanitize (SANITIZE_ARG_DEF) {
Behdad Esfahbodb28815c2009-08-04 22:35:36 -0400217 SANITIZE_DEBUG ();
Behdad Esfahbodcd3827e2009-08-04 02:09:34 -0400218 return SANITIZE_SELF () && SANITIZE (lookupIndex);
219 }
220
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400221 /* TODO: implement get_feature_parameters() */
222 /* TODO: implement FeatureSize and other special features? */
223 Offset featureParams; /* Offset to Feature Parameters table (if one
224 * has been defined for the feature), relative
225 * to the beginning of the Feature Table; = Null
226 * if not required */
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -0400227 IndexArray lookupIndex; /* Array of LookupList indices */
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400228};
229ASSERT_SIZE (Feature, 4);
230
231typedef RecordListOf<Feature> FeatureList;
232ASSERT_SIZE (FeatureList, 2);
233
234
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400235struct LookupFlag : USHORT
236{
Behdad Esfahbod4fa77d32009-05-18 18:44:54 -0400237 enum {
238 RightToLeft = 0x0001u,
239 IgnoreBaseGlyphs = 0x0002u,
240 IgnoreLigatures = 0x0004u,
241 IgnoreMarks = 0x0008u,
Behdad Esfahbodd7df42d2009-05-26 13:04:59 -0400242 UseMarkFilteringSet = 0x0010u,
243 Reserved = 0x00E0u,
Behdad Esfahbod4fa77d32009-05-18 18:44:54 -0400244 MarkAttachmentType = 0xFF00u,
245 };
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400246};
247ASSERT_SIZE (LookupFlag, 2);
248
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400249struct LookupSubTable
250{
Behdad Esfahbodcd3827e2009-08-04 02:09:34 -0400251 inline bool sanitize (SANITIZE_ARG_DEF) {
Behdad Esfahbodb28815c2009-08-04 22:35:36 -0400252 SANITIZE_DEBUG ();
Behdad Esfahbodcd3827e2009-08-04 02:09:34 -0400253 return SANITIZE_SELF ();
254 }
255
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400256 private:
257 USHORT format; /* Subtable format. Different for GSUB and GPOS */
258};
259ASSERT_SIZE (LookupSubTable, 2);
260
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400261struct Lookup
262{
263 inline const LookupSubTable& get_subtable (unsigned int i) const { return this+subTable[i]; }
264 inline unsigned int get_subtable_count (void) const { return subTable.len; }
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400265
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400266 inline unsigned int get_type (void) const { return lookupType; }
Behdad Esfahbodd7df42d2009-05-26 13:04:59 -0400267 inline unsigned int get_flag (void) const
268 {
269 unsigned int flag = lookupFlag;
270 if (HB_UNLIKELY (flag & LookupFlag::UseMarkFilteringSet))
271 {
Behdad Esfahbod284899c2009-08-09 22:10:39 -0400272 const USHORT &markFilteringSet = CONST_NEXT (USHORT, subTable);
Behdad Esfahbod09c292e2009-05-26 19:48:16 -0400273 flag += (markFilteringSet << 16);
Behdad Esfahbodd7df42d2009-05-26 13:04:59 -0400274 }
275 return flag;
276 }
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400277
Behdad Esfahbodcd3827e2009-08-04 02:09:34 -0400278 inline bool sanitize (SANITIZE_ARG_DEF) {
Behdad Esfahbodb28815c2009-08-04 22:35:36 -0400279 SANITIZE_DEBUG ();
Behdad Esfahbodcd3827e2009-08-04 02:09:34 -0400280 if (!(SANITIZE_SELF () && SANITIZE_THIS (subTable))) return false;
281 if (HB_UNLIKELY (lookupFlag & LookupFlag::UseMarkFilteringSet))
282 {
Behdad Esfahbod284899c2009-08-09 22:10:39 -0400283 USHORT &markFilteringSet = NEXT (USHORT, subTable);
Behdad Esfahbodcd3827e2009-08-04 02:09:34 -0400284 if (!SANITIZE (markFilteringSet)) return false;
285 }
286 return true;
287 }
288
Behdad Esfahbodd7df42d2009-05-26 13:04:59 -0400289 USHORT lookupType; /* Different enumerations for GSUB and GPOS */
290 USHORT lookupFlag; /* Lookup qualifiers */
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400291 OffsetArrayOf<LookupSubTable>
Behdad Esfahbodd7df42d2009-05-26 13:04:59 -0400292 subTable; /* Array of SubTables */
293 USHORT markFilteringSetX[0]; /* Index (base 0) into GDEF mark glyph sets
294 * structure. This field is only present if bit
295 * UseMarkFilteringSet of lookup flags is set. */
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400296};
297ASSERT_SIZE (Lookup, 6);
298
299template <typename Type>
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400300struct OffsetListOf : OffsetArrayOf<Type>
301{
302 inline const Type& operator [] (unsigned int i) const
303 {
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400304 if (HB_UNLIKELY (i >= this->len)) return Null(Type);
305 return this+this->array[i];
306 }
Behdad Esfahbodcd3827e2009-08-04 02:09:34 -0400307
308 inline bool sanitize (SANITIZE_ARG_DEF) {
Behdad Esfahbodb28815c2009-08-04 22:35:36 -0400309 SANITIZE_DEBUG ();
Behdad Esfahbod196598b2009-08-04 11:04:32 -0400310 return OffsetArrayOf<Type>::sanitize (SANITIZE_ARG, CONST_CHARP(this));
Behdad Esfahbodcd3827e2009-08-04 02:09:34 -0400311 }
Behdad Esfahbod3564ee52009-08-14 18:32:56 -0400312 inline bool sanitize (SANITIZE_ARG_DEF, unsigned int user_data) {
313 SANITIZE_DEBUG ();
314 return OffsetArrayOf<Type>::sanitize (SANITIZE_ARG, CONST_CHARP(this), user_data);
315 }
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400316};
317
318typedef OffsetListOf<Lookup> LookupList;
319ASSERT_SIZE (LookupList, 2);
320
321
322/*
323 * Coverage Table
324 */
325
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400326struct CoverageFormat1
327{
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400328 friend struct Coverage;
329
330 private:
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400331 inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
332 {
Behdad Esfahbod2e8fb6c2009-05-18 04:37:37 -0400333 if (HB_UNLIKELY (glyph_id > 0xFFFF))
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400334 return NOT_COVERED;
Behdad Esfahbod2e8fb6c2009-05-18 04:37:37 -0400335 GlyphID gid;
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400336 gid = glyph_id;
337 // TODO: bsearch
338 unsigned int num_glyphs = glyphArray.len;
339 for (unsigned int i = 0; i < num_glyphs; i++)
340 if (gid == glyphArray[i])
341 return i;
342 return NOT_COVERED;
343 }
344
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400345 inline bool sanitize (SANITIZE_ARG_DEF) {
Behdad Esfahbodb28815c2009-08-04 22:35:36 -0400346 SANITIZE_DEBUG ();
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400347 return SANITIZE (glyphArray);
348 }
349
350 private:
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400351 USHORT coverageFormat; /* Format identifier--format = 1 */
352 ArrayOf<GlyphID>
353 glyphArray; /* Array of GlyphIDs--in numerical order */
354};
355ASSERT_SIZE (CoverageFormat1, 4);
356
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400357struct CoverageRangeRecord
358{
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400359 friend struct CoverageFormat2;
360
361 private:
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400362 inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
363 {
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400364 if (glyph_id >= start && glyph_id <= end)
Behdad Esfahbod2e8fb6c2009-05-18 04:37:37 -0400365 return (unsigned int) startCoverageIndex + (glyph_id - start);
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400366 return NOT_COVERED;
367 }
368
Behdad Esfahbodcd3827e2009-08-04 02:09:34 -0400369 public:
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400370 inline bool sanitize (SANITIZE_ARG_DEF) {
Behdad Esfahbodb28815c2009-08-04 22:35:36 -0400371 SANITIZE_DEBUG ();
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400372 return SANITIZE_SELF ();
373 }
374
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400375 private:
376 GlyphID start; /* First GlyphID in the range */
377 GlyphID end; /* Last GlyphID in the range */
378 USHORT startCoverageIndex; /* Coverage Index of first GlyphID in
379 * range */
380};
381ASSERT_SIZE_DATA (CoverageRangeRecord, 6, "\000\001");
382
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400383struct CoverageFormat2
384{
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400385 friend struct Coverage;
386
387 private:
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400388 inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
389 {
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400390 // TODO: bsearch
391 unsigned int count = rangeRecord.len;
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400392 for (unsigned int i = 0; i < count; i++)
393 {
Behdad Esfahbod2e8fb6c2009-05-18 04:37:37 -0400394 unsigned int coverage = rangeRecord[i].get_coverage (glyph_id);
395 if (coverage != NOT_COVERED)
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400396 return coverage;
397 }
398 return NOT_COVERED;
399 }
400
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400401 inline bool sanitize (SANITIZE_ARG_DEF) {
Behdad Esfahbodb28815c2009-08-04 22:35:36 -0400402 SANITIZE_DEBUG ();
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400403 return SANITIZE (rangeRecord);
404 }
405
406 private:
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400407 USHORT coverageFormat; /* Format identifier--format = 2 */
408 ArrayOf<CoverageRangeRecord>
409 rangeRecord; /* Array of glyph ranges--ordered by
410 * Start GlyphID. rangeCount entries
411 * long */
412};
413ASSERT_SIZE (CoverageFormat2, 4);
414
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400415struct Coverage
416{
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400417 inline unsigned int operator() (hb_codepoint_t glyph_id) const { return get_coverage (glyph_id); }
418
Behdad Esfahbod20b035d2009-08-10 19:00:36 -0400419 inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400420 {
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400421 switch (u.format) {
422 case 1: return u.format1->get_coverage(glyph_id);
423 case 2: return u.format2->get_coverage(glyph_id);
424 default:return NOT_COVERED;
425 }
426 }
427
Behdad Esfahbod20b035d2009-08-10 19:00:36 -0400428 inline bool sanitize (SANITIZE_ARG_DEF) {
Behdad Esfahbodb28815c2009-08-04 22:35:36 -0400429 SANITIZE_DEBUG ();
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400430 if (!SANITIZE (u.format)) return false;
431 switch (u.format) {
432 case 1: return u.format1->sanitize (SANITIZE_ARG);
433 case 2: return u.format2->sanitize (SANITIZE_ARG);
434 default:return true;
435 }
436 }
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400437
438 private:
439 union {
440 USHORT format; /* Format identifier */
441 CoverageFormat1 format1[];
442 CoverageFormat2 format2[];
443 } u;
444};
445ASSERT_SIZE (Coverage, 2);
446
447
448/*
449 * Class Definition Table
450 */
451
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400452struct ClassDefFormat1
453{
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400454 friend struct ClassDef;
455
456 private:
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400457 inline hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
458 {
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400459 if ((unsigned int) (glyph_id - startGlyph) < classValue.len)
460 return classValue[glyph_id - startGlyph];
461 return 0;
462 }
463
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400464 inline bool sanitize (SANITIZE_ARG_DEF) {
Behdad Esfahbodb28815c2009-08-04 22:35:36 -0400465 SANITIZE_DEBUG ();
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400466 return SANITIZE_SELF () && SANITIZE (classValue);
467 }
468
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400469 USHORT classFormat; /* Format identifier--format = 1 */
470 GlyphID startGlyph; /* First GlyphID of the classValueArray */
471 ArrayOf<USHORT>
472 classValue; /* Array of Class Values--one per GlyphID */
473};
474ASSERT_SIZE (ClassDefFormat1, 6);
475
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400476struct ClassRangeRecord
477{
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400478 friend struct ClassDefFormat2;
479
480 private:
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400481 inline hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
482 {
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400483 if (glyph_id >= start && glyph_id <= end)
484 return classValue;
485 return 0;
486 }
487
Behdad Esfahbodcd3827e2009-08-04 02:09:34 -0400488 public:
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400489 inline bool sanitize (SANITIZE_ARG_DEF) {
Behdad Esfahbodb28815c2009-08-04 22:35:36 -0400490 SANITIZE_DEBUG ();
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400491 return SANITIZE_SELF ();
492 }
493
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400494 private:
495 GlyphID start; /* First GlyphID in the range */
496 GlyphID end; /* Last GlyphID in the range */
497 USHORT classValue; /* Applied to all glyphs in the range */
498};
499ASSERT_SIZE_DATA (ClassRangeRecord, 6, "\000\001");
500
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400501struct ClassDefFormat2
502{
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400503 friend struct ClassDef;
504
505 private:
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400506 inline hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
507 {
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400508 // TODO: bsearch
509 unsigned int count = rangeRecord.len;
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400510 for (unsigned int i = 0; i < count; i++)
511 {
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400512 int classValue = rangeRecord[i].get_class (glyph_id);
513 if (classValue > 0)
514 return classValue;
515 }
516 return 0;
517 }
518
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400519 inline bool sanitize (SANITIZE_ARG_DEF) {
Behdad Esfahbodb28815c2009-08-04 22:35:36 -0400520 SANITIZE_DEBUG ();
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400521 return SANITIZE (rangeRecord);
522 }
523
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400524 USHORT classFormat; /* Format identifier--format = 2 */
525 ArrayOf<ClassRangeRecord>
526 rangeRecord; /* Array of glyph ranges--ordered by
527 * Start GlyphID */
528};
529ASSERT_SIZE (ClassDefFormat2, 4);
530
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400531struct ClassDef
532{
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400533 inline unsigned int operator() (hb_codepoint_t glyph_id) const { return get_class (glyph_id); }
534
Behdad Esfahbod20b035d2009-08-10 19:00:36 -0400535 inline hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400536 {
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400537 switch (u.format) {
538 case 1: return u.format1->get_class(glyph_id);
539 case 2: return u.format2->get_class(glyph_id);
540 default:return 0;
541 }
542 }
543
Behdad Esfahbod20b035d2009-08-10 19:00:36 -0400544 inline bool sanitize (SANITIZE_ARG_DEF) {
Behdad Esfahbodb28815c2009-08-04 22:35:36 -0400545 SANITIZE_DEBUG ();
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400546 if (!SANITIZE (u.format)) return false;
547 switch (u.format) {
548 case 1: return u.format1->sanitize (SANITIZE_ARG);
549 case 2: return u.format2->sanitize (SANITIZE_ARG);
550 default:return true;
551 }
552 }
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400553
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400554 private:
555 union {
556 USHORT format; /* Format identifier */
557 ClassDefFormat1 format1[];
558 ClassDefFormat2 format2[];
559 } u;
560};
561ASSERT_SIZE (ClassDef, 2);
562
563
564/*
565 * Device Tables
566 */
567
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400568struct Device
569{
Behdad Esfahboddc9c4d92009-08-04 12:26:26 -0400570 inline int operator() (unsigned int ppem_size) const { return get_delta (ppem_size); }
571
Behdad Esfahbod20b035d2009-08-10 19:00:36 -0400572 inline int get_delta (unsigned int ppem_size) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400573 {
Behdad Esfahbod056c7ec2009-05-18 19:47:52 -0400574 unsigned int f = deltaFormat;
575 if (HB_UNLIKELY (f < 1 || f > 3))
576 return 0;
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400577
Behdad Esfahbod056c7ec2009-05-18 19:47:52 -0400578 if (ppem_size < startSize || ppem_size > endSize)
579 return 0;
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400580
Behdad Esfahbod056c7ec2009-05-18 19:47:52 -0400581 unsigned int s = ppem_size - startSize;
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400582
Behdad Esfahbod056c7ec2009-05-18 19:47:52 -0400583 unsigned int byte = deltaValue[s >> (4 - f)];
Behdad Esfahbod09c292e2009-05-26 19:48:16 -0400584 unsigned int bits = (byte >> (16 - (((s & ((1 << (4 - f)) - 1)) + 1) << f)));
585 unsigned int mask = (0xFFFF >> (16 - (1 << f)));
Behdad Esfahbod056c7ec2009-05-18 19:47:52 -0400586
587 int delta = bits & mask;
588
Behdad Esfahbod15164d92009-08-04 13:57:41 -0400589 if ((unsigned int) delta >= ((mask + 1) >> 1))
Behdad Esfahbod056c7ec2009-05-18 19:47:52 -0400590 delta -= mask + 1;
591
592 return delta;
593 }
594
Behdad Esfahboddc9c4d92009-08-04 12:26:26 -0400595 inline unsigned int get_size () const
596 {
597 unsigned int f = deltaFormat;
598 if (HB_UNLIKELY (f < 1 || f > 3 || startSize > endSize)) return sizeof (*this);
599 return sizeof (*this) + ((endSize - startSize + (1 << (4 - f)) - 1) >> (4 - f));
600 }
601
Behdad Esfahbod20b035d2009-08-10 19:00:36 -0400602 inline bool sanitize (SANITIZE_ARG_DEF) {
Behdad Esfahbodb28815c2009-08-04 22:35:36 -0400603 SANITIZE_DEBUG ();
Behdad Esfahboddc9c4d92009-08-04 12:26:26 -0400604 return SANITIZE_GET_SIZE ();
605 }
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400606
607 private:
608 USHORT startSize; /* Smallest size to correct--in ppem */
609 USHORT endSize; /* Largest size to correct--in ppem */
610 USHORT deltaFormat; /* Format of DeltaValue array data: 1, 2, or 3 */
611 USHORT deltaValue[]; /* Array of compressed data */
612};
613ASSERT_SIZE (Device, 6);
614
615
Behdad Esfahbod5f5b24f2009-08-02 20:03:12 -0400616#endif /* HB_OT_LAYOUT_COMMON_PRIVATE_HH */