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