[HB] Add sanitize debugging facilities
[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-type-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     SANITIZE_DEBUG ();
51     return SANITIZE (tag) == 0 && SANITIZE_BASE (offset, base);
52   }
53
54   Tag           tag;            /* 4-byte Tag identifier */
55   OffsetTo<Type>
56                 offset;         /* Offset from beginning of object holding
57                                  * the Record */
58 };
59
60 template <typename Type>
61 struct RecordArrayOf : ArrayOf<Record<Type> > {};
62
63 template <typename Type>
64 struct RecordListOf : RecordArrayOf<Type>
65 {
66   inline const Type& operator [] (unsigned int i) const
67   {
68     if (HB_UNLIKELY (i >= this->len)) return Null(Type);
69     return this+this->array[i].offset;
70   }
71   inline const Tag& get_tag (unsigned int i) const
72   {
73     if (HB_UNLIKELY (i >= this->len)) return Null(Tag);
74     return this->array[i].tag;
75   }
76
77   inline bool sanitize (SANITIZE_ARG_DEF) {
78     SANITIZE_DEBUG ();
79     return RecordArrayOf<Type>::sanitize (SANITIZE_ARG, CONST_CHARP(this));
80   }
81 };
82
83
84 struct Script;
85 struct LangSys;
86 struct Feature;
87
88
89 struct LangSys
90 {
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; }
93
94   inline bool has_required_feature (void) const { return reqFeatureIndex != 0xffff; }
95   inline int get_required_feature_index (void) const
96   {
97     if (reqFeatureIndex == 0xffff)
98       return NO_INDEX;
99    return reqFeatureIndex;;
100   }
101
102   inline bool sanitize (SANITIZE_ARG_DEF) {
103     SANITIZE_DEBUG ();
104     return SANITIZE_SELF () && SANITIZE (featureIndex);
105   }
106
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
111                                  * = 0xFFFF */
112   ArrayOf<USHORT>
113                 featureIndex;   /* Array of indices into the FeatureList */
114 };
115 ASSERT_SIZE_DATA (LangSys, 6, "\0\0\xFF\xFF");
116
117
118 struct Script
119 {
120   inline const LangSys& get_lang_sys (unsigned int i) const
121   {
122     if (i == NO_INDEX) return get_default_lang_sys ();
123     return this+langSys[i].offset;
124   }
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; }
127
128   // LONGTERMTODO bsearch
129   DEFINE_TAG_FIND_INTERFACE (LangSys, lang_sys);        /* find_lang_sys_index (), get_lang_sys_by_tag (tag) */
130
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; }
133
134   inline bool sanitize (SANITIZE_ARG_DEF) {
135     SANITIZE_DEBUG ();
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     SANITIZE_DEBUG ();
160     return SANITIZE_SELF () && SANITIZE (lookupIndex);
161   }
162
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
168                                  * if not required */
169   ArrayOf<USHORT>
170                 lookupIndex;    /* Array of LookupList indices */
171 };
172 ASSERT_SIZE (Feature, 4);
173
174 typedef RecordListOf<Feature> FeatureList;
175 ASSERT_SIZE (FeatureList, 2);
176
177
178 struct LookupFlag : USHORT
179 {
180   enum {
181     RightToLeft         = 0x0001u,
182     IgnoreBaseGlyphs    = 0x0002u,
183     IgnoreLigatures     = 0x0004u,
184     IgnoreMarks         = 0x0008u,
185     UseMarkFilteringSet = 0x0010u,
186     Reserved            = 0x00E0u,
187     MarkAttachmentType  = 0xFF00u,
188   };
189 };
190 ASSERT_SIZE (LookupFlag, 2);
191
192 struct LookupSubTable
193 {
194   inline bool sanitize (SANITIZE_ARG_DEF) {
195     SANITIZE_DEBUG ();
196     return SANITIZE_SELF ();
197   }
198
199   private:
200   USHORT        format;         /* Subtable format.  Different for GSUB and GPOS */
201 };
202 ASSERT_SIZE (LookupSubTable, 2);
203
204 struct Lookup
205 {
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; }
208
209   inline unsigned int get_type (void) const { return lookupType; }
210   inline unsigned int get_flag (void) const
211   {
212     unsigned int flag = lookupFlag;
213     if (HB_UNLIKELY (flag & LookupFlag::UseMarkFilteringSet))
214     {
215       const USHORT &markFilteringSet = CONST_CAST (USHORT, subTable, subTable.get_size ());
216       flag += (markFilteringSet << 16);
217     }
218     return flag;
219   }
220
221   inline bool sanitize (SANITIZE_ARG_DEF) {
222     SANITIZE_DEBUG ();
223     if (!(SANITIZE_SELF () && SANITIZE_THIS (subTable))) return false;
224     if (HB_UNLIKELY (lookupFlag & LookupFlag::UseMarkFilteringSet))
225     {
226       USHORT &markFilteringSet = CAST (USHORT, subTable, subTable.get_size ());
227       if (!SANITIZE (markFilteringSet)) return false;
228     }
229     return true;
230   }
231
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. */
239 };
240 ASSERT_SIZE (Lookup, 6);
241
242 template <typename Type>
243 struct OffsetListOf : OffsetArrayOf<Type>
244 {
245   inline const Type& operator [] (unsigned int i) const
246   {
247     if (HB_UNLIKELY (i >= this->len)) return Null(Type);
248     return this+this->array[i];
249   }
250
251   inline bool sanitize (SANITIZE_ARG_DEF) {
252     SANITIZE_DEBUG ();
253     return OffsetArrayOf<Type>::sanitize (SANITIZE_ARG, CONST_CHARP(this));
254   }
255 };
256
257 typedef OffsetListOf<Lookup> LookupList;
258 ASSERT_SIZE (LookupList, 2);
259
260
261 /*
262  * Coverage Table
263  */
264
265 struct CoverageFormat1
266 {
267   friend struct Coverage;
268
269   private:
270   inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
271   {
272     if (HB_UNLIKELY (glyph_id > 0xFFFF))
273       return NOT_COVERED;
274     GlyphID gid;
275     gid = glyph_id;
276     // TODO: bsearch
277     unsigned int num_glyphs = glyphArray.len;
278     for (unsigned int i = 0; i < num_glyphs; i++)
279       if (gid == glyphArray[i])
280         return i;
281     return NOT_COVERED;
282   }
283
284   inline bool sanitize (SANITIZE_ARG_DEF) {
285     SANITIZE_DEBUG ();
286     return SANITIZE (glyphArray);
287   }
288
289   private:
290   USHORT        coverageFormat; /* Format identifier--format = 1 */
291   ArrayOf<GlyphID>
292                 glyphArray;     /* Array of GlyphIDs--in numerical order */
293 };
294 ASSERT_SIZE (CoverageFormat1, 4);
295
296 struct CoverageRangeRecord
297 {
298   friend struct CoverageFormat2;
299
300   private:
301   inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
302   {
303     if (glyph_id >= start && glyph_id <= end)
304       return (unsigned int) startCoverageIndex + (glyph_id - start);
305     return NOT_COVERED;
306   }
307
308   public:
309   inline bool sanitize (SANITIZE_ARG_DEF) {
310     SANITIZE_DEBUG ();
311     return SANITIZE_SELF ();
312   }
313
314   private:
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
318                                          * range */
319 };
320 ASSERT_SIZE_DATA (CoverageRangeRecord, 6, "\000\001");
321
322 struct CoverageFormat2
323 {
324   friend struct Coverage;
325
326   private:
327   inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
328   {
329     // TODO: bsearch
330     unsigned int count = rangeRecord.len;
331     for (unsigned int i = 0; i < count; i++)
332     {
333       unsigned int coverage = rangeRecord[i].get_coverage (glyph_id);
334       if (coverage != NOT_COVERED)
335         return coverage;
336     }
337     return NOT_COVERED;
338   }
339
340   inline bool sanitize (SANITIZE_ARG_DEF) {
341     SANITIZE_DEBUG ();
342     return SANITIZE (rangeRecord);
343   }
344
345   private:
346   USHORT        coverageFormat; /* Format identifier--format = 2 */
347   ArrayOf<CoverageRangeRecord>
348                 rangeRecord;    /* Array of glyph ranges--ordered by
349                                  * Start GlyphID. rangeCount entries
350                                  * long */
351 };
352 ASSERT_SIZE (CoverageFormat2, 4);
353
354 struct Coverage
355 {
356   inline unsigned int operator() (hb_codepoint_t glyph_id) const { return get_coverage (glyph_id); }
357
358   unsigned int get_coverage (hb_codepoint_t glyph_id) const
359   {
360     switch (u.format) {
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;
364     }
365   }
366
367   bool sanitize (SANITIZE_ARG_DEF) {
368     SANITIZE_DEBUG ();
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     SANITIZE_DEBUG ();
405     return SANITIZE_SELF () && SANITIZE (classValue);
406   }
407
408   USHORT        classFormat;            /* Format identifier--format = 1 */
409   GlyphID       startGlyph;             /* First GlyphID of the classValueArray */
410   ArrayOf<USHORT>
411                 classValue;             /* Array of Class Values--one per GlyphID */
412 };
413 ASSERT_SIZE (ClassDefFormat1, 6);
414
415 struct ClassRangeRecord
416 {
417   friend struct ClassDefFormat2;
418
419   private:
420   inline hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
421   {
422     if (glyph_id >= start && glyph_id <= end)
423       return classValue;
424     return 0;
425   }
426
427   public:
428   inline bool sanitize (SANITIZE_ARG_DEF) {
429     SANITIZE_DEBUG ();
430     return SANITIZE_SELF ();
431   }
432
433   private:
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 */
437 };
438 ASSERT_SIZE_DATA (ClassRangeRecord, 6, "\000\001");
439
440 struct ClassDefFormat2
441 {
442   friend struct ClassDef;
443
444   private:
445   inline hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
446   {
447     // TODO: bsearch
448     unsigned int count = rangeRecord.len;
449     for (unsigned int i = 0; i < count; i++)
450     {
451       int classValue = rangeRecord[i].get_class (glyph_id);
452       if (classValue > 0)
453         return classValue;
454     }
455     return 0;
456   }
457
458   inline bool sanitize (SANITIZE_ARG_DEF) {
459     SANITIZE_DEBUG ();
460     return SANITIZE (rangeRecord);
461   }
462
463   USHORT        classFormat;    /* Format identifier--format = 2 */
464   ArrayOf<ClassRangeRecord>
465                 rangeRecord;    /* Array of glyph ranges--ordered by
466                                  * Start GlyphID */
467 };
468 ASSERT_SIZE (ClassDefFormat2, 4);
469
470 struct ClassDef
471 {
472   inline unsigned int operator() (hb_codepoint_t glyph_id) const { return get_class (glyph_id); }
473
474   hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
475   {
476     switch (u.format) {
477     case 1: return u.format1->get_class(glyph_id);
478     case 2: return u.format2->get_class(glyph_id);
479     default:return 0;
480     }
481   }
482
483   bool sanitize (SANITIZE_ARG_DEF) {
484     SANITIZE_DEBUG ();
485     if (!SANITIZE (u.format)) return false;
486     switch (u.format) {
487     case 1: return u.format1->sanitize (SANITIZE_ARG);
488     case 2: return u.format2->sanitize (SANITIZE_ARG);
489     default:return true;
490     }
491   }
492
493   private:
494   union {
495   USHORT                format;         /* Format identifier */
496   ClassDefFormat1       format1[];
497   ClassDefFormat2       format2[];
498   } u;
499 };
500 ASSERT_SIZE (ClassDef, 2);
501
502
503 /*
504  * Device Tables
505  */
506
507 struct Device
508 {
509   inline int operator() (unsigned int ppem_size) const { return get_delta (ppem_size); }
510
511   int get_delta (unsigned int ppem_size) const
512   {
513     unsigned int f = deltaFormat;
514     if (HB_UNLIKELY (f < 1 || f > 3))
515       return 0;
516
517     if (ppem_size < startSize || ppem_size > endSize)
518       return 0;
519
520     unsigned int s = ppem_size - startSize;
521
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)));
525
526     int delta = bits & mask;
527
528     if ((unsigned int) delta >= ((mask + 1) >> 1))
529       delta -= mask + 1;
530
531     return delta;
532   }
533
534   inline unsigned int get_size () const
535   {
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));
539   }
540
541   bool sanitize (SANITIZE_ARG_DEF) {
542     SANITIZE_DEBUG ();
543     return SANITIZE_GET_SIZE ();
544   }
545
546   private:
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 */
551 };
552 ASSERT_SIZE (Device, 6);
553
554
555 #endif /* HB_OT_LAYOUT_COMMON_PRIVATE_HH */