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