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