One fewer cmp() implementation...
[framework/uifw/harfbuzz.git] / src / hb-ot-layout-common-private.hh
1 /*
2  * Copyright (C) 2007,2008,2009  Red Hat, Inc.
3  * Copyright (C) 2010  Google, Inc.
4  *
5  *  This is part of HarfBuzz, a text shaping library.
6  *
7  * Permission is hereby granted, without written agreement and without
8  * license or royalty fees, to use, copy, modify, and distribute this
9  * software and its documentation for any purpose, provided that the
10  * above copyright notice and the following two paragraphs appear in
11  * all copies of this software.
12  *
13  * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
14  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
15  * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
16  * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
17  * DAMAGE.
18  *
19  * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
20  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21  * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
22  * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
23  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
24  *
25  * Red Hat Author(s): Behdad Esfahbod
26  * Google Author(s): Behdad Esfahbod
27  */
28
29 #ifndef HB_OT_LAYOUT_COMMON_PRIVATE_HH
30 #define HB_OT_LAYOUT_COMMON_PRIVATE_HH
31
32 #include "hb-ot-layout-private.hh"
33
34 #include "hb-open-type-private.hh"
35
36
37 #define NO_CONTEXT              ((unsigned int) 0x110000)
38 #define NOT_COVERED             ((unsigned int) 0x110000)
39 #define MAX_NESTING_LEVEL       8
40
41 HB_BEGIN_DECLS
42 HB_END_DECLS
43
44
45 /*
46  *
47  * OpenType Layout Common Table Formats
48  *
49  */
50
51
52 /*
53  * Script, ScriptList, LangSys, Feature, FeatureList, Lookup, LookupList
54  */
55
56 template <typename Type>
57 struct Record
58 {
59   inline int cmp (hb_tag_t a) const {
60     return tag.cmp (a);
61   }
62
63   inline bool sanitize (hb_sanitize_context_t *c, void *base) {
64     TRACE_SANITIZE ();
65     return c->check_struct (this)
66         && offset.sanitize (c, base);
67   }
68
69   Tag           tag;            /* 4-byte Tag identifier */
70   OffsetTo<Type>
71                 offset;         /* Offset from beginning of object holding
72                                  * the Record */
73   public:
74   DEFINE_SIZE_STATIC (6);
75 };
76
77 template <typename Type>
78 struct RecordArrayOf : SortedArrayOf<Record<Type> > {
79   inline const Tag& get_tag (unsigned int i) const
80   {
81     if (unlikely (i >= this->len)) return Null(Tag);
82     return (*this)[i].tag;
83   }
84   inline unsigned int get_tags (unsigned int start_offset,
85                                 unsigned int *record_count /* IN/OUT */,
86                                 hb_tag_t     *record_tags /* OUT */) const
87   {
88     if (record_count) {
89       const Record<Type> *array = this->sub_array (start_offset, record_count);
90       unsigned int count = *record_count;
91       for (unsigned int i = 0; i < count; i++)
92         record_tags[i] = array[i].tag;
93     }
94     return this->len;
95   }
96   inline bool find_index (hb_tag_t tag, unsigned int *index) const
97   {
98     int i = this->search (tag);
99     if (i != -1) {
100         if (index) *index = i;
101         return true;
102     } else {
103       if (index) *index = Index::NOT_FOUND_INDEX;
104       return false;
105     }
106   }
107 };
108
109 template <typename Type>
110 struct RecordListOf : RecordArrayOf<Type>
111 {
112   inline const Type& operator [] (unsigned int i) const
113   { return this+RecordArrayOf<Type>::operator [](i).offset; }
114
115   inline bool sanitize (hb_sanitize_context_t *c) {
116     TRACE_SANITIZE ();
117     return RecordArrayOf<Type>::sanitize (c, this);
118   }
119 };
120
121
122 struct RangeRecord
123 {
124   inline int cmp (hb_codepoint_t g) const {
125     hb_codepoint_t a = start, b = end;
126     return g < a ? -1 : g <= b ? 0 : +1 ;
127   }
128
129   inline bool sanitize (hb_sanitize_context_t *c) {
130     TRACE_SANITIZE ();
131     return c->check_struct (this);
132   }
133
134   GlyphID       start;          /* First GlyphID in the range */
135   GlyphID       end;            /* Last GlyphID in the range */
136   USHORT        value;          /* Value */
137   public:
138   DEFINE_SIZE_STATIC (6);
139 };
140 DEFINE_NULL_DATA (RangeRecord, "\000\001");
141
142
143 struct IndexArray : ArrayOf<Index>
144 {
145   inline unsigned int get_indexes (unsigned int start_offset,
146                                    unsigned int *_count /* IN/OUT */,
147                                    unsigned int *_indexes /* OUT */) const
148   {
149     if (_count) {
150       const USHORT *array = this->sub_array (start_offset, _count);
151       unsigned int count = *_count;
152       for (unsigned int i = 0; i < count; i++)
153         _indexes[i] = array[i];
154     }
155     return this->len;
156   }
157 };
158
159
160 struct Script;
161 struct LangSys;
162 struct Feature;
163
164
165 struct LangSys
166 {
167   inline unsigned int get_feature_count (void) const
168   { return featureIndex.len; }
169   inline hb_tag_t get_feature_index (unsigned int i) const
170   { return featureIndex[i]; }
171   inline unsigned int get_feature_indexes (unsigned int start_offset,
172                                            unsigned int *feature_count /* IN/OUT */,
173                                            unsigned int *feature_indexes /* OUT */) const
174   { return featureIndex.get_indexes (start_offset, feature_count, feature_indexes); }
175
176   inline bool has_required_feature (void) const { return reqFeatureIndex != 0xffff; }
177   inline unsigned int get_required_feature_index (void) const
178   {
179     if (reqFeatureIndex == 0xffff)
180       return Index::NOT_FOUND_INDEX;
181    return reqFeatureIndex;;
182   }
183
184   inline bool sanitize (hb_sanitize_context_t *c) {
185     TRACE_SANITIZE ();
186     return c->check_struct (this)
187         && featureIndex.sanitize (c);
188   }
189
190   Offset        lookupOrder;    /* = Null (reserved for an offset to a
191                                  * reordering table) */
192   USHORT        reqFeatureIndex;/* Index of a feature required for this
193                                  * language system--if no required features
194                                  * = 0xFFFF */
195   IndexArray    featureIndex;   /* Array of indices into the FeatureList */
196   public:
197   DEFINE_SIZE_ARRAY (6, featureIndex);
198 };
199 DEFINE_NULL_DATA (LangSys, "\0\0\xFF\xFF");
200
201
202 struct Script
203 {
204   inline unsigned int get_lang_sys_count (void) const
205   { return langSys.len; }
206   inline const Tag& get_lang_sys_tag (unsigned int i) const
207   { return langSys.get_tag (i); }
208   inline unsigned int get_lang_sys_tags (unsigned int start_offset,
209                                          unsigned int *lang_sys_count /* IN/OUT */,
210                                          hb_tag_t     *lang_sys_tags /* OUT */) const
211   { return langSys.get_tags (start_offset, lang_sys_count, lang_sys_tags); }
212   inline const LangSys& get_lang_sys (unsigned int i) const
213   {
214     if (i == Index::NOT_FOUND_INDEX) return get_default_lang_sys ();
215     return this+langSys[i].offset;
216   }
217   inline bool find_lang_sys_index (hb_tag_t tag, unsigned int *index) const
218   { return langSys.find_index (tag, index); }
219
220   inline bool has_default_lang_sys (void) const { return defaultLangSys != 0; }
221   inline const LangSys& get_default_lang_sys (void) const { return this+defaultLangSys; }
222
223   inline bool sanitize (hb_sanitize_context_t *c) {
224     TRACE_SANITIZE ();
225     return defaultLangSys.sanitize (c, this)
226         && langSys.sanitize (c, this);
227   }
228
229   private:
230   OffsetTo<LangSys>
231                 defaultLangSys; /* Offset to DefaultLangSys table--from
232                                  * beginning of Script table--may be Null */
233   RecordArrayOf<LangSys>
234                 langSys;        /* Array of LangSysRecords--listed
235                                  * alphabetically by LangSysTag */
236   public:
237   DEFINE_SIZE_ARRAY (4, langSys);
238 };
239
240 typedef RecordListOf<Script> ScriptList;
241
242
243 struct Feature
244 {
245   inline unsigned int get_lookup_count (void) const
246   { return lookupIndex.len; }
247   inline hb_tag_t get_lookup_index (unsigned int i) const
248   { return lookupIndex[i]; }
249   inline unsigned int get_lookup_indexes (unsigned int start_index,
250                                           unsigned int *lookup_count /* IN/OUT */,
251                                           unsigned int *lookup_tags /* OUT */) const
252   { return lookupIndex.get_indexes (start_index, lookup_count, lookup_tags); }
253
254   inline bool sanitize (hb_sanitize_context_t *c) {
255     TRACE_SANITIZE ();
256     return c->check_struct (this)
257         && lookupIndex.sanitize (c);
258   }
259
260   /* LONGTERMTODO: implement get_feature_parameters() */
261   /* LONGTERMTODO: implement FeatureSize and other special features? */
262   Offset        featureParams;  /* Offset to Feature Parameters table (if one
263                                  * has been defined for the feature), relative
264                                  * to the beginning of the Feature Table; = Null
265                                  * if not required */
266   IndexArray     lookupIndex;   /* Array of LookupList indices */
267   public:
268   DEFINE_SIZE_ARRAY (4, lookupIndex);
269 };
270
271 typedef RecordListOf<Feature> FeatureList;
272
273
274 struct LookupFlag : USHORT
275 {
276   enum {
277     RightToLeft         = 0x0001u,
278     IgnoreBaseGlyphs    = 0x0002u,
279     IgnoreLigatures     = 0x0004u,
280     IgnoreMarks         = 0x0008u,
281     IgnoreFlags         = 0x000Eu,
282     UseMarkFilteringSet = 0x0010u,
283     Reserved            = 0x00E0u,
284     MarkAttachmentType  = 0xFF00u
285   };
286   public:
287   DEFINE_SIZE_STATIC (2);
288 };
289
290 struct Lookup
291 {
292   inline unsigned int get_subtable_count (void) const { return subTable.len; }
293
294   inline unsigned int get_type (void) const { return lookupType; }
295   inline unsigned int get_flag (void) const
296   {
297     unsigned int flag = lookupFlag;
298     if (unlikely (flag & LookupFlag::UseMarkFilteringSet))
299     {
300       const USHORT &markFilteringSet = StructAfter<USHORT> (subTable);
301       flag += (markFilteringSet << 16);
302     }
303     return flag;
304   }
305
306   inline bool sanitize (hb_sanitize_context_t *c) {
307     TRACE_SANITIZE ();
308     /* Real sanitize of the subtables is done by GSUB/GPOS/... */
309     if (!(c->check_struct (this)
310        && subTable.sanitize (c))) return false;
311     if (unlikely (lookupFlag & LookupFlag::UseMarkFilteringSet))
312     {
313       USHORT &markFilteringSet = StructAfter<USHORT> (subTable);
314       if (!markFilteringSet.sanitize (c)) return false;
315     }
316     return true;
317   }
318
319   USHORT        lookupType;             /* Different enumerations for GSUB and GPOS */
320   USHORT        lookupFlag;             /* Lookup qualifiers */
321   ArrayOf<Offset>
322                 subTable;               /* Array of SubTables */
323   USHORT        markFilteringSetX[VAR]; /* Index (base 0) into GDEF mark glyph sets
324                                          * structure. This field is only present if bit
325                                          * UseMarkFilteringSet of lookup flags is set. */
326   public:
327   DEFINE_SIZE_ARRAY2 (6, subTable, markFilteringSetX);
328 };
329
330 typedef OffsetListOf<Lookup> LookupList;
331
332
333 /*
334  * Coverage Table
335  */
336
337 struct CoverageFormat1
338 {
339   friend struct Coverage;
340
341   private:
342   inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
343   {
344     int i = glyphArray.search (glyph_id);
345     if (i != -1)
346         return i;
347     return NOT_COVERED;
348   }
349
350   inline bool sanitize (hb_sanitize_context_t *c) {
351     TRACE_SANITIZE ();
352     return glyphArray.sanitize (c);
353   }
354
355   private:
356   USHORT        coverageFormat; /* Format identifier--format = 1 */
357   SortedArrayOf<GlyphID>
358                 glyphArray;     /* Array of GlyphIDs--in numerical order */
359   public:
360   DEFINE_SIZE_ARRAY (4, glyphArray);
361 };
362
363 struct CoverageFormat2
364 {
365   friend struct Coverage;
366
367   private:
368   inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
369   {
370     int i = rangeRecord.search (glyph_id);
371     if (i != -1) {
372       const RangeRecord &range = rangeRecord[i];
373       return (unsigned int) range.value + (glyph_id - range.start);
374     }
375     return NOT_COVERED;
376   }
377
378   inline bool sanitize (hb_sanitize_context_t *c) {
379     TRACE_SANITIZE ();
380     return rangeRecord.sanitize (c);
381   }
382
383   private:
384   USHORT        coverageFormat; /* Format identifier--format = 2 */
385   SortedArrayOf<RangeRecord>
386                 rangeRecord;    /* Array of glyph ranges--ordered by
387                                  * Start GlyphID. rangeCount entries
388                                  * long */
389   public:
390   DEFINE_SIZE_ARRAY (4, rangeRecord);
391 };
392
393 struct Coverage
394 {
395   inline unsigned int operator () (hb_codepoint_t glyph_id) const { return get_coverage (glyph_id); }
396
397   inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
398   {
399     switch (u.format) {
400     case 1: return u.format1.get_coverage(glyph_id);
401     case 2: return u.format2.get_coverage(glyph_id);
402     default:return NOT_COVERED;
403     }
404   }
405
406   inline bool sanitize (hb_sanitize_context_t *c) {
407     TRACE_SANITIZE ();
408     if (!u.format.sanitize (c)) return false;
409     switch (u.format) {
410     case 1: return u.format1.sanitize (c);
411     case 2: return u.format2.sanitize (c);
412     default:return true;
413     }
414   }
415
416   private:
417   union {
418   USHORT                format;         /* Format identifier */
419   CoverageFormat1       format1;
420   CoverageFormat2       format2;
421   } u;
422   public:
423   DEFINE_SIZE_UNION (2, format);
424 };
425
426
427 /*
428  * Class Definition Table
429  */
430
431 struct ClassDefFormat1
432 {
433   friend struct ClassDef;
434
435   private:
436   inline hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
437   {
438     if ((unsigned int) (glyph_id - startGlyph) < classValue.len)
439       return classValue[glyph_id - startGlyph];
440     return 0;
441   }
442
443   inline bool sanitize (hb_sanitize_context_t *c) {
444     TRACE_SANITIZE ();
445     return c->check_struct (this)
446         && classValue.sanitize (c);
447   }
448
449   USHORT        classFormat;            /* Format identifier--format = 1 */
450   GlyphID       startGlyph;             /* First GlyphID of the classValueArray */
451   ArrayOf<USHORT>
452                 classValue;             /* Array of Class Values--one per GlyphID */
453   public:
454   DEFINE_SIZE_ARRAY (6, classValue);
455 };
456
457 struct ClassDefFormat2
458 {
459   friend struct ClassDef;
460
461   private:
462   inline hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
463   {
464     int i = rangeRecord.search (glyph_id);
465     if (i != -1)
466       return rangeRecord[i].value;
467     return 0;
468   }
469
470   inline bool sanitize (hb_sanitize_context_t *c) {
471     TRACE_SANITIZE ();
472     return rangeRecord.sanitize (c);
473   }
474
475   USHORT        classFormat;    /* Format identifier--format = 2 */
476   SortedArrayOf<RangeRecord>
477                 rangeRecord;    /* Array of glyph ranges--ordered by
478                                  * Start GlyphID */
479   public:
480   DEFINE_SIZE_ARRAY (4, rangeRecord);
481 };
482
483 struct ClassDef
484 {
485   inline hb_ot_layout_class_t operator () (hb_codepoint_t glyph_id) const { return get_class (glyph_id); }
486
487   inline hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
488   {
489     switch (u.format) {
490     case 1: return u.format1.get_class(glyph_id);
491     case 2: return u.format2.get_class(glyph_id);
492     default:return 0;
493     }
494   }
495
496   inline bool sanitize (hb_sanitize_context_t *c) {
497     TRACE_SANITIZE ();
498     if (!u.format.sanitize (c)) return false;
499     switch (u.format) {
500     case 1: return u.format1.sanitize (c);
501     case 2: return u.format2.sanitize (c);
502     default:return true;
503     }
504   }
505
506   private:
507   union {
508   USHORT                format;         /* Format identifier */
509   ClassDefFormat1       format1;
510   ClassDefFormat2       format2;
511   } u;
512   public:
513   DEFINE_SIZE_UNION (2, format);
514 };
515
516
517 /*
518  * Device Tables
519  */
520
521 struct Device
522 {
523
524   inline hb_position_t get_x_delta (hb_ot_layout_context_t *c) const
525   { return get_delta (c->font->x_ppem, c->font->x_scale); }
526
527   inline hb_position_t get_y_delta (hb_ot_layout_context_t *c) const
528   { return get_delta (c->font->y_ppem, c->font->y_scale); }
529
530   inline int get_delta (unsigned int ppem, unsigned int scale) const
531   {
532     if (!ppem) return 0;
533
534     int pixels = get_delta_pixels (ppem);
535
536     if (!pixels) return 0;
537
538     /* pixels is at most in the -8..7 range.  So 64-bit arithmetic is
539      * not really necessary here.  A simple cast to int may just work
540      * as well.  But since this code is not reached that often and
541      * for the sake of correctness, we do a 64bit operation. */
542     return pixels * (int64_t) scale / ppem;
543   }
544
545
546   inline int get_delta_pixels (unsigned int ppem_size) const
547   {
548     unsigned int f = deltaFormat;
549     if (unlikely (f < 1 || f > 3))
550       return 0;
551
552     if (ppem_size < startSize || ppem_size > endSize)
553       return 0;
554
555     unsigned int s = ppem_size - startSize;
556
557     unsigned int byte = deltaValue[s >> (4 - f)];
558     unsigned int bits = (byte >> (16 - (((s & ((1 << (4 - f)) - 1)) + 1) << f)));
559     unsigned int mask = (0xFFFF >> (16 - (1 << f)));
560
561     int delta = bits & mask;
562
563     if ((unsigned int) delta >= ((mask + 1) >> 1))
564       delta -= mask + 1;
565
566     return delta;
567   }
568
569   inline unsigned int get_size () const
570   {
571     unsigned int f = deltaFormat;
572     if (unlikely (f < 1 || f > 3 || startSize > endSize)) return 3 * USHORT::static_size;
573     return USHORT::static_size * (4 + ((endSize - startSize) >> (4 - f)));
574   }
575
576   inline bool sanitize (hb_sanitize_context_t *c) {
577     TRACE_SANITIZE ();
578     return c->check_struct (this)
579         && c->check_range (this, this->get_size ());
580   }
581
582   private:
583   USHORT        startSize;              /* Smallest size to correct--in ppem */
584   USHORT        endSize;                /* Largest size to correct--in ppem */
585   USHORT        deltaFormat;            /* Format of DeltaValue array data: 1, 2, or 3
586                                          * 1    Signed 2-bit value, 8 values per uint16
587                                          * 2    Signed 4-bit value, 4 values per uint16
588                                          * 3    Signed 8-bit value, 2 values per uint16
589                                          */
590   USHORT        deltaValue[VAR];        /* Array of compressed data */
591   public:
592   DEFINE_SIZE_ARRAY (6, deltaValue);
593 };
594
595
596 HB_BEGIN_DECLS
597 HB_END_DECLS
598
599 #endif /* HB_OT_LAYOUT_COMMON_PRIVATE_HH */