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