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