[HB] Fix mix warnings
[framework/uifw/harfbuzz.git] / src / hb-ot-layout-common-private.hh
1 /*
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
27 #ifndef HB_OT_LAYOUT_COMMON_PRIVATE_HH
28 #define HB_OT_LAYOUT_COMMON_PRIVATE_HH
29
30 #include "hb-ot-layout-private.h"
31
32 #include "hb-open-types-private.hh"
33
34
35 /*
36  *
37  * OpenType Layout Common Table Formats
38  *
39  */
40
41
42 /*
43  * Script, ScriptList, LangSys, Feature, FeatureList, Lookup, LookupList
44  */
45
46 template <typename Type>
47 struct Record
48 {
49   inline bool sanitize (SANITIZE_ARG_DEF, const void *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
53      */
54     return (tag & 0x80808080) == 0 && offset.sanitize (SANITIZE_ARG, base);
55   }
56
57   Tag           tag;            /* 4-byte Tag identifier */
58   OffsetTo<Type>
59                 offset;         /* Offset from beginning of object holding
60                                  * the Record */
61 };
62
63 template <typename Type>
64 struct RecordArrayOf : ArrayOf<Record<Type> > {};
65
66 template <typename Type>
67 struct RecordListOf : RecordArrayOf<Type>
68 {
69   inline const Type& operator [] (unsigned int i) const
70   {
71     if (HB_UNLIKELY (i >= this->len)) return Null(Type);
72     return this+this->array[i].offset;
73   }
74   inline const Tag& get_tag (unsigned int i) const
75   {
76     if (HB_UNLIKELY (i >= this->len)) return Null(Tag);
77     return this->array[i].tag;
78   }
79
80   inline bool sanitize (SANITIZE_ARG_DEF) {
81     return RecordArrayOf<Type>::sanitize (SANITIZE_ARG, CONST_CHARP(this));
82   }
83 };
84
85
86 struct Script;
87 struct LangSys;
88 struct Feature;
89
90
91 struct LangSys
92 {
93   inline unsigned int get_feature_index (unsigned int i) const { return featureIndex[i]; }
94   inline unsigned int get_feature_count (void) const { return featureIndex.len; }
95
96   inline bool has_required_feature (void) const { return reqFeatureIndex != 0xffff; }
97   inline int get_required_feature_index (void) const
98   {
99     if (reqFeatureIndex == 0xffff)
100       return NO_INDEX;
101    return reqFeatureIndex;;
102   }
103
104   inline bool sanitize (SANITIZE_ARG_DEF) {
105     return SANITIZE_SELF () && SANITIZE (featureIndex);
106   }
107
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
112                                  * = 0xFFFF */
113   ArrayOf<USHORT>
114                 featureIndex;   /* Array of indices into the FeatureList */
115 };
116 ASSERT_SIZE_DATA (LangSys, 6, "\0\0\xFF\xFF");
117
118
119 struct Script
120 {
121   inline const LangSys& get_lang_sys (unsigned int i) const
122   {
123     if (i == NO_INDEX) return get_default_lang_sys ();
124     return this+langSys[i].offset;
125   }
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; }
128
129   // LONGTERMTODO bsearch
130   DEFINE_TAG_FIND_INTERFACE (LangSys, lang_sys);        /* find_lang_sys_index (), get_lang_sys_by_tag (tag) */
131
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; }
134
135   inline bool sanitize (SANITIZE_ARG_DEF) {
136     return SANITIZE_THIS (defaultLangSys) && SANITIZE_THIS (langSys);
137   }
138
139   private:
140   OffsetTo<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 */
146 };
147 ASSERT_SIZE (Script, 4);
148
149 typedef RecordListOf<Script> ScriptList;
150 ASSERT_SIZE (ScriptList, 2);
151
152
153 struct Feature
154 {
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; }
157
158   inline bool sanitize (SANITIZE_ARG_DEF) {
159     return SANITIZE_SELF () && SANITIZE (lookupIndex);
160   }
161
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
167                                  * if not required */
168   ArrayOf<USHORT>
169                 lookupIndex;    /* Array of LookupList indices */
170 };
171 ASSERT_SIZE (Feature, 4);
172
173 typedef RecordListOf<Feature> FeatureList;
174 ASSERT_SIZE (FeatureList, 2);
175
176
177 struct LookupFlag : USHORT
178 {
179   enum {
180     RightToLeft         = 0x0001u,
181     IgnoreBaseGlyphs    = 0x0002u,
182     IgnoreLigatures     = 0x0004u,
183     IgnoreMarks         = 0x0008u,
184     UseMarkFilteringSet = 0x0010u,
185     Reserved            = 0x00E0u,
186     MarkAttachmentType  = 0xFF00u,
187   };
188 };
189 ASSERT_SIZE (LookupFlag, 2);
190
191 struct LookupSubTable
192 {
193   inline bool sanitize (SANITIZE_ARG_DEF) {
194     return SANITIZE_SELF ();
195   }
196
197   private:
198   USHORT        format;         /* Subtable format.  Different for GSUB and GPOS */
199 };
200 ASSERT_SIZE (LookupSubTable, 2);
201
202 struct Lookup
203 {
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; }
206
207   inline unsigned int get_type (void) const { return lookupType; }
208   inline unsigned int get_flag (void) const
209   {
210     unsigned int flag = lookupFlag;
211     if (HB_UNLIKELY (flag & LookupFlag::UseMarkFilteringSet))
212     {
213       const USHORT &markFilteringSet = CONST_CAST (USHORT, subTable, subTable.get_size ());
214       flag += (markFilteringSet << 16);
215     }
216     return flag;
217   }
218
219   inline bool sanitize (SANITIZE_ARG_DEF) {
220     if (!(SANITIZE_SELF () && SANITIZE_THIS (subTable))) return false;
221     if (HB_UNLIKELY (lookupFlag & LookupFlag::UseMarkFilteringSet))
222     {
223       USHORT &markFilteringSet = CAST (USHORT, subTable, subTable.get_size ());
224       if (!SANITIZE (markFilteringSet)) return false;
225     }
226     return true;
227   }
228
229   USHORT        lookupType;             /* Different enumerations for GSUB and GPOS */
230   USHORT        lookupFlag;             /* Lookup qualifiers */
231   OffsetArrayOf<LookupSubTable>
232                 subTable;               /* Array of SubTables */
233   USHORT        markFilteringSetX[0];   /* Index (base 0) into GDEF mark glyph sets
234                                          * structure. This field is only present if bit
235                                          * UseMarkFilteringSet of lookup flags is set. */
236 };
237 ASSERT_SIZE (Lookup, 6);
238
239 template <typename Type>
240 struct OffsetListOf : OffsetArrayOf<Type>
241 {
242   inline const Type& operator [] (unsigned int i) const
243   {
244     if (HB_UNLIKELY (i >= this->len)) return Null(Type);
245     return this+this->array[i];
246   }
247
248   inline bool sanitize (SANITIZE_ARG_DEF) {
249     return OffsetArrayOf<Type>::sanitize (SANITIZE_ARG, CONST_CHARP(this));
250   }
251 };
252
253 typedef OffsetListOf<Lookup> LookupList;
254 ASSERT_SIZE (LookupList, 2);
255
256
257 /*
258  * Coverage Table
259  */
260
261 struct CoverageFormat1
262 {
263   friend struct Coverage;
264
265   private:
266   inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
267   {
268     if (HB_UNLIKELY (glyph_id > 0xFFFF))
269       return NOT_COVERED;
270     GlyphID gid;
271     gid = glyph_id;
272     // TODO: bsearch
273     unsigned int num_glyphs = glyphArray.len;
274     for (unsigned int i = 0; i < num_glyphs; i++)
275       if (gid == glyphArray[i])
276         return i;
277     return NOT_COVERED;
278   }
279
280   inline bool sanitize (SANITIZE_ARG_DEF) {
281     return SANITIZE (glyphArray);
282   }
283
284   private:
285   USHORT        coverageFormat; /* Format identifier--format = 1 */
286   ArrayOf<GlyphID>
287                 glyphArray;     /* Array of GlyphIDs--in numerical order */
288 };
289 ASSERT_SIZE (CoverageFormat1, 4);
290
291 struct CoverageRangeRecord
292 {
293   friend struct CoverageFormat2;
294
295   private:
296   inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
297   {
298     if (glyph_id >= start && glyph_id <= end)
299       return (unsigned int) startCoverageIndex + (glyph_id - start);
300     return NOT_COVERED;
301   }
302
303   public:
304   inline bool sanitize (SANITIZE_ARG_DEF) {
305     return SANITIZE_SELF ();
306   }
307
308   private:
309   GlyphID       start;                  /* First GlyphID in the range */
310   GlyphID       end;                    /* Last GlyphID in the range */
311   USHORT        startCoverageIndex;     /* Coverage Index of first GlyphID in
312                                          * range */
313 };
314 ASSERT_SIZE_DATA (CoverageRangeRecord, 6, "\000\001");
315
316 struct CoverageFormat2
317 {
318   friend struct Coverage;
319
320   private:
321   inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
322   {
323     // TODO: bsearch
324     unsigned int count = rangeRecord.len;
325     for (unsigned int i = 0; i < count; i++)
326     {
327       unsigned int coverage = rangeRecord[i].get_coverage (glyph_id);
328       if (coverage != NOT_COVERED)
329         return coverage;
330     }
331     return NOT_COVERED;
332   }
333
334   inline bool sanitize (SANITIZE_ARG_DEF) {
335     return SANITIZE (rangeRecord);
336   }
337
338   private:
339   USHORT        coverageFormat; /* Format identifier--format = 2 */
340   ArrayOf<CoverageRangeRecord>
341                 rangeRecord;    /* Array of glyph ranges--ordered by
342                                  * Start GlyphID. rangeCount entries
343                                  * long */
344 };
345 ASSERT_SIZE (CoverageFormat2, 4);
346
347 struct Coverage
348 {
349   inline unsigned int operator() (hb_codepoint_t glyph_id) const { return get_coverage (glyph_id); }
350
351   unsigned int get_coverage (hb_codepoint_t glyph_id) const
352   {
353     switch (u.format) {
354     case 1: return u.format1->get_coverage(glyph_id);
355     case 2: return u.format2->get_coverage(glyph_id);
356     default:return NOT_COVERED;
357     }
358   }
359
360   bool sanitize (SANITIZE_ARG_DEF) {
361     if (!SANITIZE (u.format)) return false;
362     switch (u.format) {
363     case 1: return u.format1->sanitize (SANITIZE_ARG);
364     case 2: return u.format2->sanitize (SANITIZE_ARG);
365     default:return true;
366     }
367   }
368
369   private:
370   union {
371   USHORT                format;         /* Format identifier */
372   CoverageFormat1       format1[];
373   CoverageFormat2       format2[];
374   } u;
375 };
376 ASSERT_SIZE (Coverage, 2);
377
378
379 /*
380  * Class Definition Table
381  */
382
383 struct ClassDefFormat1
384 {
385   friend struct ClassDef;
386
387   private:
388   inline hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
389   {
390     if ((unsigned int) (glyph_id - startGlyph) < classValue.len)
391       return classValue[glyph_id - startGlyph];
392     return 0;
393   }
394
395   inline bool sanitize (SANITIZE_ARG_DEF) {
396     return SANITIZE_SELF () && SANITIZE (classValue);
397   }
398
399   USHORT        classFormat;            /* Format identifier--format = 1 */
400   GlyphID       startGlyph;             /* First GlyphID of the classValueArray */
401   ArrayOf<USHORT>
402                 classValue;             /* Array of Class Values--one per GlyphID */
403 };
404 ASSERT_SIZE (ClassDefFormat1, 6);
405
406 struct ClassRangeRecord
407 {
408   friend struct ClassDefFormat2;
409
410   private:
411   inline hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
412   {
413     if (glyph_id >= start && glyph_id <= end)
414       return classValue;
415     return 0;
416   }
417
418   public:
419   inline bool sanitize (SANITIZE_ARG_DEF) {
420     return SANITIZE_SELF ();
421   }
422
423   private:
424   GlyphID       start;          /* First GlyphID in the range */
425   GlyphID       end;            /* Last GlyphID in the range */
426   USHORT        classValue;     /* Applied to all glyphs in the range */
427 };
428 ASSERT_SIZE_DATA (ClassRangeRecord, 6, "\000\001");
429
430 struct ClassDefFormat2
431 {
432   friend struct ClassDef;
433
434   private:
435   inline hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
436   {
437     // TODO: bsearch
438     unsigned int count = rangeRecord.len;
439     for (unsigned int i = 0; i < count; i++)
440     {
441       int classValue = rangeRecord[i].get_class (glyph_id);
442       if (classValue > 0)
443         return classValue;
444     }
445     return 0;
446   }
447
448   inline bool sanitize (SANITIZE_ARG_DEF) {
449     return SANITIZE (rangeRecord);
450   }
451
452   USHORT        classFormat;    /* Format identifier--format = 2 */
453   ArrayOf<ClassRangeRecord>
454                 rangeRecord;    /* Array of glyph ranges--ordered by
455                                  * Start GlyphID */
456 };
457 ASSERT_SIZE (ClassDefFormat2, 4);
458
459 struct ClassDef
460 {
461   inline unsigned int operator() (hb_codepoint_t glyph_id) const { return get_class (glyph_id); }
462
463   hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
464   {
465     switch (u.format) {
466     case 1: return u.format1->get_class(glyph_id);
467     case 2: return u.format2->get_class(glyph_id);
468     default:return 0;
469     }
470   }
471
472   bool sanitize (SANITIZE_ARG_DEF) {
473     if (!SANITIZE (u.format)) return false;
474     switch (u.format) {
475     case 1: return u.format1->sanitize (SANITIZE_ARG);
476     case 2: return u.format2->sanitize (SANITIZE_ARG);
477     default:return true;
478     }
479   }
480
481   private:
482   union {
483   USHORT                format;         /* Format identifier */
484   ClassDefFormat1       format1[];
485   ClassDefFormat2       format2[];
486   } u;
487 };
488 ASSERT_SIZE (ClassDef, 2);
489
490
491 /*
492  * Device Tables
493  */
494
495 struct Device
496 {
497   inline int operator() (unsigned int ppem_size) const { return get_delta (ppem_size); }
498
499   int get_delta (unsigned int ppem_size) const
500   {
501     unsigned int f = deltaFormat;
502     if (HB_UNLIKELY (f < 1 || f > 3))
503       return 0;
504
505     if (ppem_size < startSize || ppem_size > endSize)
506       return 0;
507
508     unsigned int s = ppem_size - startSize;
509
510     unsigned int byte = deltaValue[s >> (4 - f)];
511     unsigned int bits = (byte >> (16 - (((s & ((1 << (4 - f)) - 1)) + 1) << f)));
512     unsigned int mask = (0xFFFF >> (16 - (1 << f)));
513
514     int delta = bits & mask;
515
516     if ((unsigned int) delta >= ((mask + 1) >> 1))
517       delta -= mask + 1;
518
519     return delta;
520   }
521
522   inline unsigned int get_size () const
523   {
524     unsigned int f = deltaFormat;
525     if (HB_UNLIKELY (f < 1 || f > 3 || startSize > endSize)) return sizeof (*this);
526     return sizeof (*this) + ((endSize - startSize + (1 << (4 - f)) - 1) >> (4 - f));
527   }
528
529   bool sanitize (SANITIZE_ARG_DEF) {
530     return SANITIZE_GET_SIZE ();
531   }
532
533   private:
534   USHORT        startSize;      /* Smallest size to correct--in ppem */
535   USHORT        endSize;        /* Largest size to correct--in ppem */
536   USHORT        deltaFormat;    /* Format of DeltaValue array data: 1, 2, or 3 */
537   USHORT        deltaValue[];   /* Array of compressed data */
538 };
539 ASSERT_SIZE (Device, 6);
540
541
542 #endif /* HB_OT_LAYOUT_COMMON_PRIVATE_HH */