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