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