Fix array query 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 #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       unsigned int count = MIN (MIN (0, (unsigned int) this->len - start_offset), *record_count);
78       const Record<Type> *array = this->const_array() + start_offset;
79       for (unsigned int i = 0; i < count; i++)
80         record_tags[i] = array[i].tag;
81
82       *record_count = this->len;
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       unsigned int count = MIN (MIN (0, (unsigned int) this->len - start_offset), *_count);
132       const USHORT *array = this->const_array() + start_offset;
133       for (unsigned int i = 0; i < count; i++)
134         _indexes[i] = array[i];
135
136       *_count = this->len;
137     }
138     return this->len;
139   }
140 };
141
142
143 struct Script;
144 struct LangSys;
145 struct Feature;
146
147
148 struct LangSys
149 {
150   inline unsigned int get_feature_count (void) const
151   { return featureIndex.len; }
152   inline hb_tag_t get_feature_index (unsigned int i) const
153   { return featureIndex[i]; }
154   inline unsigned int get_feature_indexes (unsigned int start_offset,
155                                            unsigned int *feature_count /* IN/OUT */,
156                                            unsigned int *feature_indexes /* OUT */) const
157   { return featureIndex.get_indexes (start_offset, feature_count, feature_indexes); }
158
159   inline bool has_required_feature (void) const { return reqFeatureIndex != 0xffff; }
160   inline int get_required_feature_index (void) const
161   {
162     if (reqFeatureIndex == 0xffff)
163       return NO_INDEX;
164    return reqFeatureIndex;;
165   }
166
167   inline bool sanitize (SANITIZE_ARG_DEF) {
168     TRACE_SANITIZE ();
169     return SANITIZE_SELF () && SANITIZE (featureIndex);
170   }
171
172   Offset        lookupOrder;    /* = Null (reserved for an offset to a
173                                  * reordering table) */
174   USHORT        reqFeatureIndex;/* Index of a feature required for this
175                                  * language system--if no required features
176                                  * = 0xFFFF */
177   IndexArray    featureIndex;   /* Array of indices into the FeatureList */
178 };
179 ASSERT_SIZE_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_THIS (defaultLangSys) && SANITIZE_THIS (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   /* TODO: implement get_feature_parameters() */
239   /* TODO: 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 LookupSubTable
268 {
269   inline bool sanitize (SANITIZE_ARG_DEF) {
270     TRACE_SANITIZE ();
271     return SANITIZE_SELF ();
272   }
273
274   private:
275   USHORT        format;         /* Subtable format.  Different for GSUB and GPOS */
276 };
277 ASSERT_SIZE (LookupSubTable, 2);
278
279 struct Lookup
280 {
281   inline const LookupSubTable& get_subtable (unsigned int i) const { return this+subTable[i]; }
282   inline unsigned int get_subtable_count (void) const { return subTable.len; }
283
284   inline unsigned int get_type (void) const { return lookupType; }
285   inline unsigned int get_flag (void) const
286   {
287     unsigned int flag = lookupFlag;
288     if (HB_UNLIKELY (flag & LookupFlag::UseMarkFilteringSet))
289     {
290       const USHORT &markFilteringSet = CONST_NEXT (USHORT, subTable);
291       flag += (markFilteringSet << 16);
292     }
293     return flag;
294   }
295
296   inline bool sanitize (SANITIZE_ARG_DEF) {
297     TRACE_SANITIZE ();
298     if (!(SANITIZE_SELF/*XXXXXXXXXXXXXX*/ () && SANITIZE_THIS (subTable))) return false;
299     if (HB_UNLIKELY (lookupFlag & LookupFlag::UseMarkFilteringSet))
300     {
301       USHORT &markFilteringSet = NEXT (USHORT, subTable);
302       if (!SANITIZE (markFilteringSet)) return false;
303     }
304     return true;
305   }
306
307   USHORT        lookupType;             /* Different enumerations for GSUB and GPOS */
308   USHORT        lookupFlag;             /* Lookup qualifiers */
309   OffsetArrayOf<LookupSubTable>
310                 subTable;               /* Array of SubTables */
311   USHORT        markFilteringSetX[VAR]; /* Index (base 0) into GDEF mark glyph sets
312                                          * structure. This field is only present if bit
313                                          * UseMarkFilteringSet of lookup flags is set. */
314 };
315 ASSERT_SIZE_VAR (Lookup, 6, USHORT);
316
317 typedef OffsetListOf<Lookup> LookupList;
318 ASSERT_SIZE (LookupList, 2);
319
320
321 /*
322  * Coverage Table
323  */
324
325 struct CoverageFormat1
326 {
327   friend struct Coverage;
328
329   private:
330   inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
331   {
332     if (HB_UNLIKELY (glyph_id > 0xFFFF))
333       return NOT_COVERED;
334     GlyphID gid;
335     gid = glyph_id;
336     // TODO: bsearch
337     unsigned int num_glyphs = glyphArray.len;
338     for (unsigned int i = 0; i < num_glyphs; i++)
339       if (gid == glyphArray[i])
340         return i;
341     return NOT_COVERED;
342   }
343
344   inline bool sanitize (SANITIZE_ARG_DEF) {
345     TRACE_SANITIZE ();
346     return SANITIZE (glyphArray);
347   }
348
349   private:
350   USHORT        coverageFormat; /* Format identifier--format = 1 */
351   ArrayOf<GlyphID>
352                 glyphArray;     /* Array of GlyphIDs--in numerical order */
353 };
354 ASSERT_SIZE (CoverageFormat1, 4);
355
356 struct CoverageRangeRecord
357 {
358   friend struct CoverageFormat2;
359
360   private:
361   inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
362   {
363     if (glyph_id >= start && glyph_id <= end)
364       return (unsigned int) startCoverageIndex + (glyph_id - start);
365     return NOT_COVERED;
366   }
367
368   public:
369   inline bool sanitize (SANITIZE_ARG_DEF) {
370     TRACE_SANITIZE ();
371     return SANITIZE_SELF ();
372   }
373
374   private:
375   GlyphID       start;                  /* First GlyphID in the range */
376   GlyphID       end;                    /* Last GlyphID in the range */
377   USHORT        startCoverageIndex;     /* Coverage Index of first GlyphID in
378                                          * range */
379 };
380 ASSERT_SIZE_DATA (CoverageRangeRecord, 6, "\000\001");
381
382 struct CoverageFormat2
383 {
384   friend struct Coverage;
385
386   private:
387   inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
388   {
389     // TODO: bsearch
390     unsigned int count = rangeRecord.len;
391     for (unsigned int i = 0; i < count; i++)
392     {
393       unsigned int coverage = rangeRecord[i].get_coverage (glyph_id);
394       if (coverage != NOT_COVERED)
395         return coverage;
396     }
397     return NOT_COVERED;
398   }
399
400   inline bool sanitize (SANITIZE_ARG_DEF) {
401     TRACE_SANITIZE ();
402     return SANITIZE (rangeRecord);
403   }
404
405   private:
406   USHORT        coverageFormat; /* Format identifier--format = 2 */
407   ArrayOf<CoverageRangeRecord>
408                 rangeRecord;    /* Array of glyph ranges--ordered by
409                                  * Start GlyphID. rangeCount entries
410                                  * long */
411 };
412 ASSERT_SIZE (CoverageFormat2, 4);
413
414 struct Coverage
415 {
416   inline unsigned int operator() (hb_codepoint_t glyph_id) const { return get_coverage (glyph_id); }
417
418   inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
419   {
420     switch (u.format) {
421     case 1: return u.format1->get_coverage(glyph_id);
422     case 2: return u.format2->get_coverage(glyph_id);
423     default:return NOT_COVERED;
424     }
425   }
426
427   inline bool sanitize (SANITIZE_ARG_DEF) {
428     TRACE_SANITIZE ();
429     if (!SANITIZE (u.format)) return false;
430     switch (u.format) {
431     case 1: return u.format1->sanitize (SANITIZE_ARG);
432     case 2: return u.format2->sanitize (SANITIZE_ARG);
433     default:return true;
434     }
435   }
436
437   private:
438   union {
439   USHORT                format;         /* Format identifier */
440   CoverageFormat1       format1[VAR];
441   CoverageFormat2       format2[VAR];
442   } u;
443 };
444
445
446 /*
447  * Class Definition Table
448  */
449
450 struct ClassDefFormat1
451 {
452   friend struct ClassDef;
453
454   private:
455   inline hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
456   {
457     if ((unsigned int) (glyph_id - startGlyph) < classValue.len)
458       return classValue[glyph_id - startGlyph];
459     return 0;
460   }
461
462   inline bool sanitize (SANITIZE_ARG_DEF) {
463     TRACE_SANITIZE ();
464     return SANITIZE_SELF () && SANITIZE (classValue);
465   }
466
467   USHORT        classFormat;            /* Format identifier--format = 1 */
468   GlyphID       startGlyph;             /* First GlyphID of the classValueArray */
469   ArrayOf<USHORT>
470                 classValue;             /* Array of Class Values--one per GlyphID */
471 };
472 ASSERT_SIZE (ClassDefFormat1, 6);
473
474 struct ClassRangeRecord
475 {
476   friend struct ClassDefFormat2;
477
478   private:
479   inline hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
480   {
481     if (glyph_id >= start && glyph_id <= end)
482       return classValue;
483     return 0;
484   }
485
486   public:
487   inline bool sanitize (SANITIZE_ARG_DEF) {
488     TRACE_SANITIZE ();
489     return SANITIZE_SELF ();
490   }
491
492   private:
493   GlyphID       start;          /* First GlyphID in the range */
494   GlyphID       end;            /* Last GlyphID in the range */
495   USHORT        classValue;     /* Applied to all glyphs in the range */
496 };
497 ASSERT_SIZE_DATA (ClassRangeRecord, 6, "\000\001");
498
499 struct ClassDefFormat2
500 {
501   friend struct ClassDef;
502
503   private:
504   inline hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
505   {
506     // TODO: bsearch
507     unsigned int count = rangeRecord.len;
508     for (unsigned int i = 0; i < count; i++)
509     {
510       int classValue = rangeRecord[i].get_class (glyph_id);
511       if (classValue > 0)
512         return classValue;
513     }
514     return 0;
515   }
516
517   inline bool sanitize (SANITIZE_ARG_DEF) {
518     TRACE_SANITIZE ();
519     return SANITIZE (rangeRecord);
520   }
521
522   USHORT        classFormat;    /* Format identifier--format = 2 */
523   ArrayOf<ClassRangeRecord>
524                 rangeRecord;    /* Array of glyph ranges--ordered by
525                                  * Start GlyphID */
526 };
527 ASSERT_SIZE (ClassDefFormat2, 4);
528
529 struct ClassDef
530 {
531   inline unsigned int operator() (hb_codepoint_t glyph_id) const { return get_class (glyph_id); }
532
533   inline hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
534   {
535     switch (u.format) {
536     case 1: return u.format1->get_class(glyph_id);
537     case 2: return u.format2->get_class(glyph_id);
538     default:return 0;
539     }
540   }
541
542   inline bool sanitize (SANITIZE_ARG_DEF) {
543     TRACE_SANITIZE ();
544     if (!SANITIZE (u.format)) return false;
545     switch (u.format) {
546     case 1: return u.format1->sanitize (SANITIZE_ARG);
547     case 2: return u.format2->sanitize (SANITIZE_ARG);
548     default:return true;
549     }
550   }
551
552   private:
553   union {
554   USHORT                format;         /* Format identifier */
555   ClassDefFormat1       format1[VAR];
556   ClassDefFormat2       format2[VAR];
557   } u;
558 };
559
560
561 /*
562  * Device Tables
563  */
564
565 struct Device
566 {
567   inline int operator() (unsigned int ppem_size) const { return get_delta (ppem_size); }
568
569   inline int get_delta (unsigned int ppem_size) const
570   {
571     unsigned int f = deltaFormat;
572     if (HB_UNLIKELY (f < 1 || f > 3))
573       return 0;
574
575     if (ppem_size < startSize || ppem_size > endSize)
576       return 0;
577
578     unsigned int s = ppem_size - startSize;
579
580     unsigned int byte = deltaValue[s >> (4 - f)];
581     unsigned int bits = (byte >> (16 - (((s & ((1 << (4 - f)) - 1)) + 1) << f)));
582     unsigned int mask = (0xFFFF >> (16 - (1 << f)));
583
584     int delta = bits & mask;
585
586     if ((unsigned int) delta >= ((mask + 1) >> 1))
587       delta -= mask + 1;
588
589     return delta;
590   }
591
592   inline unsigned int get_size () const
593   {
594     unsigned int f = deltaFormat;
595     if (HB_UNLIKELY (f < 1 || f > 3 || startSize > endSize)) return sizeof (*this);
596     return sizeof (*this) + ((endSize - startSize + (1 << (4 - f)) - 1) >> (4 - f));
597   }
598
599   inline bool sanitize (SANITIZE_ARG_DEF) {
600     TRACE_SANITIZE ();
601     return SANITIZE_GET_SIZE ();
602   }
603
604   private:
605   USHORT        startSize;      /* Smallest size to correct--in ppem */
606   USHORT        endSize;        /* Largest size to correct--in ppem */
607   USHORT        deltaFormat;    /* Format of DeltaValue array data: 1, 2, or 3 */
608   USHORT        deltaValue[VAR];        /* Array of compressed data */
609 };
610 ASSERT_SIZE_VAR (Device, 6, USHORT);
611
612
613 #endif /* HB_OT_LAYOUT_COMMON_PRIVATE_HH */