Simplify DEFINE_NULL_DATA
[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   DEFINE_SIZE_STATIC (6);
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 context->check_struct (this)
170         && featureIndex.sanitize (context);
171   }
172
173   DEFINE_SIZE_STATIC (6);
174
175   Offset        lookupOrder;    /* = Null (reserved for an offset to a
176                                  * reordering table) */
177   USHORT        reqFeatureIndex;/* Index of a feature required for this
178                                  * language system--if no required features
179                                  * = 0xFFFF */
180   IndexArray    featureIndex;   /* Array of indices into the FeatureList */
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 };
220 ASSERT_SIZE (Script, 4);
221
222 typedef RecordListOf<Script> ScriptList;
223 ASSERT_SIZE (ScriptList, 2);
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 };
251 ASSERT_SIZE (Feature, 4);
252
253 typedef RecordListOf<Feature> FeatureList;
254 ASSERT_SIZE (FeatureList, 2);
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 };
270 ASSERT_SIZE (LookupFlag, 2);
271
272 struct Lookup
273 {
274   inline unsigned int get_subtable_count (void) const { return subTable.len; }
275
276   inline unsigned int get_type (void) const { return lookupType; }
277   inline unsigned int get_flag (void) const
278   {
279     unsigned int flag = lookupFlag;
280     if (unlikely (flag & LookupFlag::UseMarkFilteringSet))
281     {
282       const USHORT &markFilteringSet = StructAfter<USHORT> (subTable);
283       flag += (markFilteringSet << 16);
284     }
285     return flag;
286   }
287
288   inline bool sanitize (hb_sanitize_context_t *context) {
289     TRACE_SANITIZE ();
290     /* Real sanitize of the subtables is done by GSUB/GPOS/... */
291     if (!(context->check_struct (this)
292        && likely (subTable.sanitize (context)))) return false;
293     if (unlikely (lookupFlag & LookupFlag::UseMarkFilteringSet))
294     {
295       USHORT &markFilteringSet = StructAfter<USHORT> (subTable);
296       if (!markFilteringSet.sanitize (context)) return false;
297     }
298     return true;
299   }
300
301   USHORT        lookupType;             /* Different enumerations for GSUB and GPOS */
302   USHORT        lookupFlag;             /* Lookup qualifiers */
303   ArrayOf<Offset>
304                 subTable;               /* Array of SubTables */
305   USHORT        markFilteringSetX[VAR]; /* Index (base 0) into GDEF mark glyph sets
306                                          * structure. This field is only present if bit
307                                          * UseMarkFilteringSet of lookup flags is set. */
308 };
309 ASSERT_SIZE_VAR (Lookup, 6, USHORT);
310
311 typedef OffsetListOf<Lookup> LookupList;
312 ASSERT_SIZE (LookupList, 2);
313
314
315 /*
316  * Coverage Table
317  */
318
319 struct CoverageFormat1
320 {
321   friend struct Coverage;
322
323   private:
324   inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
325   {
326     if (unlikely (glyph_id > 0xFFFF))
327       return NOT_COVERED;
328     GlyphID gid;
329     gid.set (glyph_id);
330     /* TODO: bsearch (need to sort in sanitize) */
331     unsigned int num_glyphs = glyphArray.len;
332     for (unsigned int i = 0; i < num_glyphs; i++)
333       if (gid == glyphArray[i])
334         return i;
335     return NOT_COVERED;
336   }
337
338   inline bool sanitize (hb_sanitize_context_t *context) {
339     TRACE_SANITIZE ();
340     return glyphArray.sanitize (context);
341   }
342
343   private:
344   USHORT        coverageFormat; /* Format identifier--format = 1 */
345   ArrayOf<GlyphID>
346                 glyphArray;     /* Array of GlyphIDs--in numerical order */
347 };
348 ASSERT_SIZE (CoverageFormat1, 4);
349
350 struct CoverageRangeRecord
351 {
352   friend struct CoverageFormat2;
353
354   private:
355   inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
356   {
357     if (glyph_id >= start && glyph_id <= end)
358       return (unsigned int) startCoverageIndex + (glyph_id - start);
359     return NOT_COVERED;
360   }
361
362   public:
363   inline bool sanitize (hb_sanitize_context_t *context) {
364     TRACE_SANITIZE ();
365     return context->check_struct (this);
366   }
367
368   DEFINE_SIZE_STATIC (6);
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 };
376 DEFINE_NULL_DATA (CoverageRangeRecord, "\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 context->check_struct (this)
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   private:
476   inline hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
477   {
478     if (glyph_id >= start && glyph_id <= end)
479       return classValue;
480     return 0;
481   }
482
483   public:
484   inline bool sanitize (hb_sanitize_context_t *context) {
485     TRACE_SANITIZE ();
486     return context->check_struct (this);
487   }
488
489   DEFINE_SIZE_STATIC (6);
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 DEFINE_NULL_DATA (ClassRangeRecord, "\000\001");
497
498 struct ClassDefFormat2
499 {
500   friend struct ClassDef;
501
502   private:
503   inline hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
504   {
505     /* TODO: bsearch (need to sort in sanitize) */
506     unsigned int count = rangeRecord.len;
507     for (unsigned int i = 0; i < count; i++)
508     {
509       int classValue = rangeRecord[i].get_class (glyph_id);
510       if (classValue > 0)
511         return classValue;
512     }
513     return 0;
514   }
515
516   inline bool sanitize (hb_sanitize_context_t *context) {
517     TRACE_SANITIZE ();
518     return rangeRecord.sanitize (context);
519   }
520
521   USHORT        classFormat;    /* Format identifier--format = 2 */
522   ArrayOf<ClassRangeRecord>
523                 rangeRecord;    /* Array of glyph ranges--ordered by
524                                  * Start GlyphID */
525 };
526 ASSERT_SIZE (ClassDefFormat2, 4);
527
528 struct ClassDef
529 {
530   inline hb_ot_layout_class_t operator () (hb_codepoint_t glyph_id) const { return get_class (glyph_id); }
531
532   inline hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
533   {
534     switch (u.format) {
535     case 1: return u.format1->get_class(glyph_id);
536     case 2: return u.format2->get_class(glyph_id);
537     default:return 0;
538     }
539   }
540
541   inline bool sanitize (hb_sanitize_context_t *context) {
542     TRACE_SANITIZE ();
543     if (!u.format.sanitize (context)) return false;
544     switch (u.format) {
545     case 1: return u.format1->sanitize (context);
546     case 2: return u.format2->sanitize (context);
547     default:return true;
548     }
549   }
550
551   private:
552   union {
553   USHORT                format;         /* Format identifier */
554   ClassDefFormat1       format1[VAR];
555   ClassDefFormat2       format2[VAR];
556   } u;
557 };
558
559
560 /*
561  * Device Tables
562  */
563
564 struct Device
565 {
566   inline int operator () (unsigned int ppem_size) const { return get_delta (ppem_size); }
567
568   inline int get_delta (unsigned int ppem_size) const
569   {
570     unsigned int f = deltaFormat;
571     if (unlikely (f < 1 || f > 3))
572       return 0;
573
574     if (ppem_size < startSize || ppem_size > endSize)
575       return 0;
576
577     unsigned int s = ppem_size - startSize;
578
579     unsigned int byte = deltaValue[s >> (4 - f)];
580     unsigned int bits = (byte >> (16 - (((s & ((1 << (4 - f)) - 1)) + 1) << f)));
581     unsigned int mask = (0xFFFF >> (16 - (1 << f)));
582
583     int delta = bits & mask;
584
585     if ((unsigned int) delta >= ((mask + 1) >> 1))
586       delta -= mask + 1;
587
588     return delta;
589   }
590
591   inline unsigned int get_size () const
592   {
593     unsigned int f = deltaFormat;
594     if (unlikely (f < 1 || f > 3 || startSize > endSize)) return 3 * USHORT::get_size ();
595     return USHORT::get_size () * (4 + ((endSize - startSize) >> (4 - f)));
596   }
597
598   inline bool sanitize (hb_sanitize_context_t *context) {
599     TRACE_SANITIZE ();
600     return context->check_struct (this)
601         && context->check_range (this, this->get_size ());
602   }
603
604   private:
605   USHORT        startSize;              /* Smallest size to correct--in ppem */
606   USHORT        endSize;                /* Largest size to correct--in ppem */
607   USHORT        deltaFormat;            /* Format of DeltaValue array data: 1, 2, or 3
608                                          * 1    Signed 2-bit value, 8 values per uint16
609                                          * 2    Signed 4-bit value, 4 values per uint16
610                                          * 3    Signed 8-bit value, 2 values per uint16
611                                          */
612   USHORT        deltaValue[VAR];        /* Array of compressed data */
613 };
614 ASSERT_SIZE_VAR (Device, 6, USHORT);
615
616
617 #endif /* HB_OT_LAYOUT_COMMON_PRIVATE_HH */