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