Rename lookup_flags to lookup_props since it's more than just flags
[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   Offset        featureParams;  /* Offset to Feature Parameters table (if one
261                                  * has been defined for the feature), relative
262                                  * to the beginning of the Feature Table; = Null
263                                  * if not required */
264   IndexArray     lookupIndex;   /* Array of LookupList indices */
265   public:
266   DEFINE_SIZE_ARRAY (4, lookupIndex);
267 };
268
269 typedef RecordListOf<Feature> FeatureList;
270
271
272 struct LookupFlag : USHORT
273 {
274   enum {
275     RightToLeft         = 0x0001u,
276     IgnoreBaseGlyphs    = 0x0002u,
277     IgnoreLigatures     = 0x0004u,
278     IgnoreMarks         = 0x0008u,
279     IgnoreFlags         = 0x000Eu,
280     UseMarkFilteringSet = 0x0010u,
281     Reserved            = 0x00E0u,
282     MarkAttachmentType  = 0xFF00u
283   };
284   public:
285   DEFINE_SIZE_STATIC (2);
286 };
287
288 struct Lookup
289 {
290   inline unsigned int get_subtable_count (void) const { return subTable.len; }
291
292   inline unsigned int get_type (void) const { return lookupType; }
293
294   /* lookup_props is a 32-bit integer where the lower 16-bit is LookupFlag and
295    * higher 16-bit is mark-filtering-set if the lookup uses one.
296    * Not to be confused with glyph_props which is very similar. */
297   inline uint32_t get_props (void) const
298   {
299     unsigned int flag = lookupFlag;
300     if (unlikely (flag & LookupFlag::UseMarkFilteringSet))
301     {
302       const USHORT &markFilteringSet = StructAfter<USHORT> (subTable);
303       flag += (markFilteringSet << 16);
304     }
305     return flag;
306   }
307
308   inline bool sanitize (hb_sanitize_context_t *c) {
309     TRACE_SANITIZE ();
310     /* Real sanitize of the subtables is done by GSUB/GPOS/... */
311     if (!(c->check_struct (this)
312        && subTable.sanitize (c))) return false;
313     if (unlikely (lookupFlag & LookupFlag::UseMarkFilteringSet))
314     {
315       USHORT &markFilteringSet = StructAfter<USHORT> (subTable);
316       if (!markFilteringSet.sanitize (c)) return false;
317     }
318     return true;
319   }
320
321   USHORT        lookupType;             /* Different enumerations for GSUB and GPOS */
322   USHORT        lookupFlag;             /* Lookup qualifiers */
323   ArrayOf<Offset>
324                 subTable;               /* Array of SubTables */
325   USHORT        markFilteringSetX[VAR]; /* Index (base 0) into GDEF mark glyph sets
326                                          * structure. This field is only present if bit
327                                          * UseMarkFilteringSet of lookup flags is set. */
328   public:
329   DEFINE_SIZE_ARRAY2 (6, subTable, markFilteringSetX);
330 };
331
332 typedef OffsetListOf<Lookup> LookupList;
333
334
335 /*
336  * Coverage Table
337  */
338
339 struct CoverageFormat1
340 {
341   friend struct Coverage;
342
343   private:
344   inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
345   {
346     int i = glyphArray.search (glyph_id);
347     if (i != -1)
348         return i;
349     return NOT_COVERED;
350   }
351
352   inline bool sanitize (hb_sanitize_context_t *c) {
353     TRACE_SANITIZE ();
354     return glyphArray.sanitize (c);
355   }
356
357   private:
358   USHORT        coverageFormat; /* Format identifier--format = 1 */
359   SortedArrayOf<GlyphID>
360                 glyphArray;     /* Array of GlyphIDs--in numerical order */
361   public:
362   DEFINE_SIZE_ARRAY (4, glyphArray);
363 };
364
365 struct CoverageFormat2
366 {
367   friend struct Coverage;
368
369   private:
370   inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
371   {
372     int i = rangeRecord.search (glyph_id);
373     if (i != -1) {
374       const RangeRecord &range = rangeRecord[i];
375       return (unsigned int) range.value + (glyph_id - range.start);
376     }
377     return NOT_COVERED;
378   }
379
380   inline bool sanitize (hb_sanitize_context_t *c) {
381     TRACE_SANITIZE ();
382     return rangeRecord.sanitize (c);
383   }
384
385   private:
386   USHORT        coverageFormat; /* Format identifier--format = 2 */
387   SortedArrayOf<RangeRecord>
388                 rangeRecord;    /* Array of glyph ranges--ordered by
389                                  * Start GlyphID. rangeCount entries
390                                  * long */
391   public:
392   DEFINE_SIZE_ARRAY (4, rangeRecord);
393 };
394
395 struct Coverage
396 {
397   inline unsigned int operator () (hb_codepoint_t glyph_id) const { return get_coverage (glyph_id); }
398
399   inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
400   {
401     switch (u.format) {
402     case 1: return u.format1.get_coverage(glyph_id);
403     case 2: return u.format2.get_coverage(glyph_id);
404     default:return NOT_COVERED;
405     }
406   }
407
408   inline bool sanitize (hb_sanitize_context_t *c) {
409     TRACE_SANITIZE ();
410     if (!u.format.sanitize (c)) return false;
411     switch (u.format) {
412     case 1: return u.format1.sanitize (c);
413     case 2: return u.format2.sanitize (c);
414     default:return true;
415     }
416   }
417
418   private:
419   union {
420   USHORT                format;         /* Format identifier */
421   CoverageFormat1       format1;
422   CoverageFormat2       format2;
423   } u;
424   public:
425   DEFINE_SIZE_UNION (2, format);
426 };
427
428
429 /*
430  * Class Definition Table
431  */
432
433 struct ClassDefFormat1
434 {
435   friend struct ClassDef;
436
437   private:
438   inline unsigned int get_class (hb_codepoint_t glyph_id) const
439   {
440     if ((unsigned int) (glyph_id - startGlyph) < classValue.len)
441       return classValue[glyph_id - startGlyph];
442     return 0;
443   }
444
445   inline bool sanitize (hb_sanitize_context_t *c) {
446     TRACE_SANITIZE ();
447     return c->check_struct (this)
448         && classValue.sanitize (c);
449   }
450
451   USHORT        classFormat;            /* Format identifier--format = 1 */
452   GlyphID       startGlyph;             /* First GlyphID of the classValueArray */
453   ArrayOf<USHORT>
454                 classValue;             /* Array of Class Values--one per GlyphID */
455   public:
456   DEFINE_SIZE_ARRAY (6, classValue);
457 };
458
459 struct ClassDefFormat2
460 {
461   friend struct ClassDef;
462
463   private:
464   inline unsigned int get_class (hb_codepoint_t glyph_id) const
465   {
466     int i = rangeRecord.search (glyph_id);
467     if (i != -1)
468       return rangeRecord[i].value;
469     return 0;
470   }
471
472   inline bool sanitize (hb_sanitize_context_t *c) {
473     TRACE_SANITIZE ();
474     return rangeRecord.sanitize (c);
475   }
476
477   USHORT        classFormat;    /* Format identifier--format = 2 */
478   SortedArrayOf<RangeRecord>
479                 rangeRecord;    /* Array of glyph ranges--ordered by
480                                  * Start GlyphID */
481   public:
482   DEFINE_SIZE_ARRAY (4, rangeRecord);
483 };
484
485 struct ClassDef
486 {
487   inline unsigned int operator () (hb_codepoint_t glyph_id) const { return get_class (glyph_id); }
488
489   inline unsigned int get_class (hb_codepoint_t glyph_id) const
490   {
491     switch (u.format) {
492     case 1: return u.format1.get_class(glyph_id);
493     case 2: return u.format2.get_class(glyph_id);
494     default:return 0;
495     }
496   }
497
498   inline bool sanitize (hb_sanitize_context_t *c) {
499     TRACE_SANITIZE ();
500     if (!u.format.sanitize (c)) return false;
501     switch (u.format) {
502     case 1: return u.format1.sanitize (c);
503     case 2: return u.format2.sanitize (c);
504     default:return true;
505     }
506   }
507
508   private:
509   union {
510   USHORT                format;         /* Format identifier */
511   ClassDefFormat1       format1;
512   ClassDefFormat2       format2;
513   } u;
514   public:
515   DEFINE_SIZE_UNION (2, format);
516 };
517
518
519 /*
520  * Device Tables
521  */
522
523 struct Device
524 {
525
526   inline hb_position_t get_x_delta (hb_ot_layout_context_t *c) const
527   { return get_delta (c->font->x_ppem, c->font->x_scale); }
528
529   inline hb_position_t get_y_delta (hb_ot_layout_context_t *c) const
530   { return get_delta (c->font->y_ppem, c->font->y_scale); }
531
532   inline int get_delta (unsigned int ppem, unsigned int scale) const
533   {
534     if (!ppem) return 0;
535
536     int pixels = get_delta_pixels (ppem);
537
538     if (!pixels) return 0;
539
540     /* pixels is at most in the -8..7 range.  So 64-bit arithmetic is
541      * not really necessary here.  A simple cast to int may just work
542      * as well.  But since this code is not reached that often and
543      * for the sake of correctness, we do a 64bit operation. */
544     return pixels * (int64_t) scale / ppem;
545   }
546
547
548   inline int get_delta_pixels (unsigned int ppem_size) const
549   {
550     unsigned int f = deltaFormat;
551     if (unlikely (f < 1 || f > 3))
552       return 0;
553
554     if (ppem_size < startSize || ppem_size > endSize)
555       return 0;
556
557     unsigned int s = ppem_size - startSize;
558
559     unsigned int byte = deltaValue[s >> (4 - f)];
560     unsigned int bits = (byte >> (16 - (((s & ((1 << (4 - f)) - 1)) + 1) << f)));
561     unsigned int mask = (0xFFFF >> (16 - (1 << f)));
562
563     int delta = bits & mask;
564
565     if ((unsigned int) delta >= ((mask + 1) >> 1))
566       delta -= mask + 1;
567
568     return delta;
569   }
570
571   inline unsigned int get_size (void) const
572   {
573     unsigned int f = deltaFormat;
574     if (unlikely (f < 1 || f > 3 || startSize > endSize)) return 3 * USHORT::static_size;
575     return USHORT::static_size * (4 + ((endSize - startSize) >> (4 - f)));
576   }
577
578   inline bool sanitize (hb_sanitize_context_t *c) {
579     TRACE_SANITIZE ();
580     return c->check_struct (this)
581         && c->check_range (this, this->get_size ());
582   }
583
584   private:
585   USHORT        startSize;              /* Smallest size to correct--in ppem */
586   USHORT        endSize;                /* Largest size to correct--in ppem */
587   USHORT        deltaFormat;            /* Format of DeltaValue array data: 1, 2, or 3
588                                          * 1    Signed 2-bit value, 8 values per uint16
589                                          * 2    Signed 4-bit value, 4 values per uint16
590                                          * 3    Signed 8-bit value, 2 values per uint16
591                                          */
592   USHORT        deltaValue[VAR];        /* Array of compressed data */
593   public:
594   DEFINE_SIZE_ARRAY (6, deltaValue);
595 };
596
597
598 HB_BEGIN_DECLS
599 HB_END_DECLS
600
601 #endif /* HB_OT_LAYOUT_COMMON_PRIVATE_HH */