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