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