blob: 2d3935e3098e35863605ff588b4ff19194d546db [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 Esfahbod738c54d2009-08-04 14:42:46 -040050 return SANITIZE (tag) == 0 && SANITIZE_BASE (offset, base);
Behdad Esfahbodcd3827e2009-08-04 02:09:34 -040051 }
52
Behdad Esfahbod6f20f722009-05-17 20:28:01 -040053 Tag tag; /* 4-byte Tag identifier */
54 OffsetTo<Type>
55 offset; /* Offset from beginning of object holding
56 * the Record */
57};
58
59template <typename Type>
Behdad Esfahbode6ab2c52009-08-04 10:23:01 -040060struct RecordArrayOf : ArrayOf<Record<Type> > {};
Behdad Esfahbodcd3827e2009-08-04 02:09:34 -040061
62template <typename Type>
63struct RecordListOf : RecordArrayOf<Type>
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -040064{
65 inline const Type& operator [] (unsigned int i) const
66 {
Behdad Esfahbod6f20f722009-05-17 20:28:01 -040067 if (HB_UNLIKELY (i >= this->len)) return Null(Type);
68 return this+this->array[i].offset;
69 }
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -040070 inline const Tag& get_tag (unsigned int i) const
71 {
Behdad Esfahbod6f20f722009-05-17 20:28:01 -040072 if (HB_UNLIKELY (i >= this->len)) return Null(Tag);
73 return this->array[i].tag;
74 }
Behdad Esfahbodcd3827e2009-08-04 02:09:34 -040075
76 inline bool sanitize (SANITIZE_ARG_DEF) {
Behdad Esfahbod196598b2009-08-04 11:04:32 -040077 return RecordArrayOf<Type>::sanitize (SANITIZE_ARG, CONST_CHARP(this));
Behdad Esfahbodcd3827e2009-08-04 02:09:34 -040078 }
Behdad Esfahbod6f20f722009-05-17 20:28:01 -040079};
80
81
82struct Script;
Behdad Esfahbod6f20f722009-05-17 20:28:01 -040083struct LangSys;
Behdad Esfahbod6f20f722009-05-17 20:28:01 -040084struct Feature;
Behdad Esfahbod6f20f722009-05-17 20:28:01 -040085
86
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -040087struct LangSys
88{
Behdad Esfahbod15164d92009-08-04 13:57:41 -040089 inline unsigned int get_feature_index (unsigned int i) const { return featureIndex[i]; }
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -040090 inline unsigned int get_feature_count (void) const { return featureIndex.len; }
Behdad Esfahbod6f20f722009-05-17 20:28:01 -040091
Behdad Esfahbodcc6c6442009-05-25 03:10:06 -040092 inline bool has_required_feature (void) const { return reqFeatureIndex != 0xffff; }
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -040093 inline int get_required_feature_index (void) const
94 {
Behdad Esfahbod6f20f722009-05-17 20:28:01 -040095 if (reqFeatureIndex == 0xffff)
96 return NO_INDEX;
97 return reqFeatureIndex;;
98 }
99
Behdad Esfahbodcd3827e2009-08-04 02:09:34 -0400100 inline bool sanitize (SANITIZE_ARG_DEF) {
101 return SANITIZE_SELF () && SANITIZE (featureIndex);
102 }
103
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400104 Offset lookupOrder; /* = Null (reserved for an offset to a
105 * reordering table) */
106 USHORT reqFeatureIndex;/* Index of a feature required for this
107 * language system--if no required features
108 * = 0xFFFF */
109 ArrayOf<USHORT>
110 featureIndex; /* Array of indices into the FeatureList */
111};
112ASSERT_SIZE_DATA (LangSys, 6, "\0\0\xFF\xFF");
113
114
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400115struct Script
116{
117 inline const LangSys& get_lang_sys (unsigned int i) const
118 {
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400119 if (i == NO_INDEX) return get_default_lang_sys ();
120 return this+langSys[i].offset;
121 }
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400122 inline unsigned int get_lang_sys_count (void) const { return langSys.len; }
123 inline const Tag& get_lang_sys_tag (unsigned int i) const { return langSys[i].tag; }
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400124
125 // LONGTERMTODO bsearch
126 DEFINE_TAG_FIND_INTERFACE (LangSys, lang_sys); /* find_lang_sys_index (), get_lang_sys_by_tag (tag) */
127
Behdad Esfahbodcc6c6442009-05-25 03:10:06 -0400128 inline bool has_default_lang_sys (void) const { return defaultLangSys != 0; }
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400129 inline const LangSys& get_default_lang_sys (void) const { return this+defaultLangSys; }
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400130
Behdad Esfahbodcd3827e2009-08-04 02:09:34 -0400131 inline bool sanitize (SANITIZE_ARG_DEF) {
132 return SANITIZE_THIS (defaultLangSys) && SANITIZE_THIS (langSys);
133 }
134
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400135 private:
136 OffsetTo<LangSys>
137 defaultLangSys; /* Offset to DefaultLangSys table--from
138 * beginning of Script table--may be Null */
Behdad Esfahbodcd3827e2009-08-04 02:09:34 -0400139 RecordArrayOf<LangSys>
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400140 langSys; /* Array of LangSysRecords--listed
141 * alphabetically by LangSysTag */
142};
143ASSERT_SIZE (Script, 4);
144
145typedef RecordListOf<Script> ScriptList;
146ASSERT_SIZE (ScriptList, 2);
147
148
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400149struct Feature
150{
Behdad Esfahbod15164d92009-08-04 13:57:41 -0400151 inline unsigned int get_lookup_index (unsigned int i) const { return lookupIndex[i]; }
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400152 inline unsigned int get_lookup_count (void) const { return lookupIndex.len; }
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400153
Behdad Esfahbodcd3827e2009-08-04 02:09:34 -0400154 inline bool sanitize (SANITIZE_ARG_DEF) {
155 return SANITIZE_SELF () && SANITIZE (lookupIndex);
156 }
157
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400158 /* TODO: implement get_feature_parameters() */
159 /* TODO: implement FeatureSize and other special features? */
160 Offset featureParams; /* Offset to Feature Parameters table (if one
161 * has been defined for the feature), relative
162 * to the beginning of the Feature Table; = Null
163 * if not required */
164 ArrayOf<USHORT>
165 lookupIndex; /* Array of LookupList indices */
166};
167ASSERT_SIZE (Feature, 4);
168
169typedef RecordListOf<Feature> FeatureList;
170ASSERT_SIZE (FeatureList, 2);
171
172
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400173struct LookupFlag : USHORT
174{
Behdad Esfahbod4fa77d32009-05-18 18:44:54 -0400175 enum {
176 RightToLeft = 0x0001u,
177 IgnoreBaseGlyphs = 0x0002u,
178 IgnoreLigatures = 0x0004u,
179 IgnoreMarks = 0x0008u,
Behdad Esfahbodd7df42d2009-05-26 13:04:59 -0400180 UseMarkFilteringSet = 0x0010u,
181 Reserved = 0x00E0u,
Behdad Esfahbod4fa77d32009-05-18 18:44:54 -0400182 MarkAttachmentType = 0xFF00u,
183 };
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400184};
185ASSERT_SIZE (LookupFlag, 2);
186
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400187struct LookupSubTable
188{
Behdad Esfahbodcd3827e2009-08-04 02:09:34 -0400189 inline bool sanitize (SANITIZE_ARG_DEF) {
190 return SANITIZE_SELF ();
191 }
192
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400193 private:
194 USHORT format; /* Subtable format. Different for GSUB and GPOS */
195};
196ASSERT_SIZE (LookupSubTable, 2);
197
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400198struct Lookup
199{
200 inline const LookupSubTable& get_subtable (unsigned int i) const { return this+subTable[i]; }
201 inline unsigned int get_subtable_count (void) const { return subTable.len; }
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400202
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400203 inline unsigned int get_type (void) const { return lookupType; }
Behdad Esfahbodd7df42d2009-05-26 13:04:59 -0400204 inline unsigned int get_flag (void) const
205 {
206 unsigned int flag = lookupFlag;
207 if (HB_UNLIKELY (flag & LookupFlag::UseMarkFilteringSet))
208 {
Behdad Esfahbod2b5a59c2009-08-04 11:38:50 -0400209 const USHORT &markFilteringSet = CONST_CAST (USHORT, subTable, subTable.get_size ());
Behdad Esfahbod09c292e2009-05-26 19:48:16 -0400210 flag += (markFilteringSet << 16);
Behdad Esfahbodd7df42d2009-05-26 13:04:59 -0400211 }
212 return flag;
213 }
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400214
Behdad Esfahbodcd3827e2009-08-04 02:09:34 -0400215 inline bool sanitize (SANITIZE_ARG_DEF) {
216 if (!(SANITIZE_SELF () && SANITIZE_THIS (subTable))) return false;
217 if (HB_UNLIKELY (lookupFlag & LookupFlag::UseMarkFilteringSet))
218 {
Behdad Esfahbod2b5a59c2009-08-04 11:38:50 -0400219 USHORT &markFilteringSet = CAST (USHORT, subTable, subTable.get_size ());
Behdad Esfahbodcd3827e2009-08-04 02:09:34 -0400220 if (!SANITIZE (markFilteringSet)) return false;
221 }
222 return true;
223 }
224
Behdad Esfahbodd7df42d2009-05-26 13:04:59 -0400225 USHORT lookupType; /* Different enumerations for GSUB and GPOS */
226 USHORT lookupFlag; /* Lookup qualifiers */
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400227 OffsetArrayOf<LookupSubTable>
Behdad Esfahbodd7df42d2009-05-26 13:04:59 -0400228 subTable; /* Array of SubTables */
229 USHORT markFilteringSetX[0]; /* Index (base 0) into GDEF mark glyph sets
230 * structure. This field is only present if bit
231 * UseMarkFilteringSet of lookup flags is set. */
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400232};
233ASSERT_SIZE (Lookup, 6);
234
235template <typename Type>
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400236struct OffsetListOf : OffsetArrayOf<Type>
237{
238 inline const Type& operator [] (unsigned int i) const
239 {
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400240 if (HB_UNLIKELY (i >= this->len)) return Null(Type);
241 return this+this->array[i];
242 }
Behdad Esfahbodcd3827e2009-08-04 02:09:34 -0400243
244 inline bool sanitize (SANITIZE_ARG_DEF) {
Behdad Esfahbod196598b2009-08-04 11:04:32 -0400245 return OffsetArrayOf<Type>::sanitize (SANITIZE_ARG, CONST_CHARP(this));
Behdad Esfahbodcd3827e2009-08-04 02:09:34 -0400246 }
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400247};
248
249typedef OffsetListOf<Lookup> LookupList;
250ASSERT_SIZE (LookupList, 2);
251
252
253/*
254 * Coverage Table
255 */
256
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400257struct CoverageFormat1
258{
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400259 friend struct Coverage;
260
261 private:
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400262 inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
263 {
Behdad Esfahbod2e8fb6c2009-05-18 04:37:37 -0400264 if (HB_UNLIKELY (glyph_id > 0xFFFF))
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400265 return NOT_COVERED;
Behdad Esfahbod2e8fb6c2009-05-18 04:37:37 -0400266 GlyphID gid;
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400267 gid = glyph_id;
268 // TODO: bsearch
269 unsigned int num_glyphs = glyphArray.len;
270 for (unsigned int i = 0; i < num_glyphs; i++)
271 if (gid == glyphArray[i])
272 return i;
273 return NOT_COVERED;
274 }
275
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400276 inline bool sanitize (SANITIZE_ARG_DEF) {
277 return SANITIZE (glyphArray);
278 }
279
280 private:
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400281 USHORT coverageFormat; /* Format identifier--format = 1 */
282 ArrayOf<GlyphID>
283 glyphArray; /* Array of GlyphIDs--in numerical order */
284};
285ASSERT_SIZE (CoverageFormat1, 4);
286
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400287struct CoverageRangeRecord
288{
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400289 friend struct CoverageFormat2;
290
291 private:
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400292 inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
293 {
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400294 if (glyph_id >= start && glyph_id <= end)
Behdad Esfahbod2e8fb6c2009-05-18 04:37:37 -0400295 return (unsigned int) startCoverageIndex + (glyph_id - start);
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400296 return NOT_COVERED;
297 }
298
Behdad Esfahbodcd3827e2009-08-04 02:09:34 -0400299 public:
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400300 inline bool sanitize (SANITIZE_ARG_DEF) {
301 return SANITIZE_SELF ();
302 }
303
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400304 private:
305 GlyphID start; /* First GlyphID in the range */
306 GlyphID end; /* Last GlyphID in the range */
307 USHORT startCoverageIndex; /* Coverage Index of first GlyphID in
308 * range */
309};
310ASSERT_SIZE_DATA (CoverageRangeRecord, 6, "\000\001");
311
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400312struct CoverageFormat2
313{
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400314 friend struct Coverage;
315
316 private:
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400317 inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
318 {
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400319 // TODO: bsearch
320 unsigned int count = rangeRecord.len;
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400321 for (unsigned int i = 0; i < count; i++)
322 {
Behdad Esfahbod2e8fb6c2009-05-18 04:37:37 -0400323 unsigned int coverage = rangeRecord[i].get_coverage (glyph_id);
324 if (coverage != NOT_COVERED)
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400325 return coverage;
326 }
327 return NOT_COVERED;
328 }
329
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400330 inline bool sanitize (SANITIZE_ARG_DEF) {
331 return SANITIZE (rangeRecord);
332 }
333
334 private:
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400335 USHORT coverageFormat; /* Format identifier--format = 2 */
336 ArrayOf<CoverageRangeRecord>
337 rangeRecord; /* Array of glyph ranges--ordered by
338 * Start GlyphID. rangeCount entries
339 * long */
340};
341ASSERT_SIZE (CoverageFormat2, 4);
342
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400343struct Coverage
344{
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400345 inline unsigned int operator() (hb_codepoint_t glyph_id) const { return get_coverage (glyph_id); }
346
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400347 unsigned int get_coverage (hb_codepoint_t glyph_id) const
348 {
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400349 switch (u.format) {
350 case 1: return u.format1->get_coverage(glyph_id);
351 case 2: return u.format2->get_coverage(glyph_id);
352 default:return NOT_COVERED;
353 }
354 }
355
Behdad Esfahboddc9c4d92009-08-04 12:26:26 -0400356 bool sanitize (SANITIZE_ARG_DEF) {
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400357 if (!SANITIZE (u.format)) return false;
358 switch (u.format) {
359 case 1: return u.format1->sanitize (SANITIZE_ARG);
360 case 2: return u.format2->sanitize (SANITIZE_ARG);
361 default:return true;
362 }
363 }
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400364
365 private:
366 union {
367 USHORT format; /* Format identifier */
368 CoverageFormat1 format1[];
369 CoverageFormat2 format2[];
370 } u;
371};
372ASSERT_SIZE (Coverage, 2);
373
374
375/*
376 * Class Definition Table
377 */
378
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400379struct ClassDefFormat1
380{
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400381 friend struct ClassDef;
382
383 private:
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400384 inline hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
385 {
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400386 if ((unsigned int) (glyph_id - startGlyph) < classValue.len)
387 return classValue[glyph_id - startGlyph];
388 return 0;
389 }
390
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400391 inline bool sanitize (SANITIZE_ARG_DEF) {
392 return SANITIZE_SELF () && SANITIZE (classValue);
393 }
394
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400395 USHORT classFormat; /* Format identifier--format = 1 */
396 GlyphID startGlyph; /* First GlyphID of the classValueArray */
397 ArrayOf<USHORT>
398 classValue; /* Array of Class Values--one per GlyphID */
399};
400ASSERT_SIZE (ClassDefFormat1, 6);
401
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400402struct ClassRangeRecord
403{
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400404 friend struct ClassDefFormat2;
405
406 private:
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400407 inline hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
408 {
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400409 if (glyph_id >= start && glyph_id <= end)
410 return classValue;
411 return 0;
412 }
413
Behdad Esfahbodcd3827e2009-08-04 02:09:34 -0400414 public:
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400415 inline bool sanitize (SANITIZE_ARG_DEF) {
416 return SANITIZE_SELF ();
417 }
418
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400419 private:
420 GlyphID start; /* First GlyphID in the range */
421 GlyphID end; /* Last GlyphID in the range */
422 USHORT classValue; /* Applied to all glyphs in the range */
423};
424ASSERT_SIZE_DATA (ClassRangeRecord, 6, "\000\001");
425
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400426struct ClassDefFormat2
427{
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400428 friend struct ClassDef;
429
430 private:
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400431 inline hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
432 {
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400433 // TODO: bsearch
434 unsigned int count = rangeRecord.len;
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400435 for (unsigned int i = 0; i < count; i++)
436 {
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400437 int classValue = rangeRecord[i].get_class (glyph_id);
438 if (classValue > 0)
439 return classValue;
440 }
441 return 0;
442 }
443
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400444 inline bool sanitize (SANITIZE_ARG_DEF) {
445 return SANITIZE (rangeRecord);
446 }
447
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400448 USHORT classFormat; /* Format identifier--format = 2 */
449 ArrayOf<ClassRangeRecord>
450 rangeRecord; /* Array of glyph ranges--ordered by
451 * Start GlyphID */
452};
453ASSERT_SIZE (ClassDefFormat2, 4);
454
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400455struct ClassDef
456{
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400457 inline unsigned int operator() (hb_codepoint_t glyph_id) const { return get_class (glyph_id); }
458
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400459 hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
460 {
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400461 switch (u.format) {
462 case 1: return u.format1->get_class(glyph_id);
463 case 2: return u.format2->get_class(glyph_id);
464 default:return 0;
465 }
466 }
467
Behdad Esfahboddc9c4d92009-08-04 12:26:26 -0400468 bool sanitize (SANITIZE_ARG_DEF) {
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400469 if (!SANITIZE (u.format)) return false;
470 switch (u.format) {
471 case 1: return u.format1->sanitize (SANITIZE_ARG);
472 case 2: return u.format2->sanitize (SANITIZE_ARG);
473 default:return true;
474 }
475 }
Behdad Esfahbodaa3d7ad2009-05-17 23:17:56 -0400476
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400477 private:
478 union {
479 USHORT format; /* Format identifier */
480 ClassDefFormat1 format1[];
481 ClassDefFormat2 format2[];
482 } u;
483};
484ASSERT_SIZE (ClassDef, 2);
485
486
487/*
488 * Device Tables
489 */
490
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400491struct Device
492{
Behdad Esfahboddc9c4d92009-08-04 12:26:26 -0400493 inline int operator() (unsigned int ppem_size) const { return get_delta (ppem_size); }
494
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400495 int get_delta (unsigned int ppem_size) const
496 {
Behdad Esfahbod056c7ec2009-05-18 19:47:52 -0400497 unsigned int f = deltaFormat;
498 if (HB_UNLIKELY (f < 1 || f > 3))
499 return 0;
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400500
Behdad Esfahbod056c7ec2009-05-18 19:47:52 -0400501 if (ppem_size < startSize || ppem_size > endSize)
502 return 0;
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400503
Behdad Esfahbod056c7ec2009-05-18 19:47:52 -0400504 unsigned int s = ppem_size - startSize;
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400505
Behdad Esfahbod056c7ec2009-05-18 19:47:52 -0400506 unsigned int byte = deltaValue[s >> (4 - f)];
Behdad Esfahbod09c292e2009-05-26 19:48:16 -0400507 unsigned int bits = (byte >> (16 - (((s & ((1 << (4 - f)) - 1)) + 1) << f)));
508 unsigned int mask = (0xFFFF >> (16 - (1 << f)));
Behdad Esfahbod056c7ec2009-05-18 19:47:52 -0400509
510 int delta = bits & mask;
511
Behdad Esfahbod15164d92009-08-04 13:57:41 -0400512 if ((unsigned int) delta >= ((mask + 1) >> 1))
Behdad Esfahbod056c7ec2009-05-18 19:47:52 -0400513 delta -= mask + 1;
514
515 return delta;
516 }
517
Behdad Esfahboddc9c4d92009-08-04 12:26:26 -0400518 inline unsigned int get_size () const
519 {
520 unsigned int f = deltaFormat;
521 if (HB_UNLIKELY (f < 1 || f > 3 || startSize > endSize)) return sizeof (*this);
522 return sizeof (*this) + ((endSize - startSize + (1 << (4 - f)) - 1) >> (4 - f));
523 }
524
525 bool sanitize (SANITIZE_ARG_DEF) {
526 return SANITIZE_GET_SIZE ();
527 }
Behdad Esfahbod6f20f722009-05-17 20:28:01 -0400528
529 private:
530 USHORT startSize; /* Smallest size to correct--in ppem */
531 USHORT endSize; /* Largest size to correct--in ppem */
532 USHORT deltaFormat; /* Format of DeltaValue array data: 1, 2, or 3 */
533 USHORT deltaValue[]; /* Array of compressed data */
534};
535ASSERT_SIZE (Device, 6);
536
537
Behdad Esfahbod5f5b24f2009-08-02 20:03:12 -0400538#endif /* HB_OT_LAYOUT_COMMON_PRIVATE_HH */