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