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