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