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