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