[HB] Start sanitize()
[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-types-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   Tag           tag;            /* 4-byte Tag identifier */
50   OffsetTo<Type>
51                 offset;         /* Offset from beginning of object holding
52                                  * the Record */
53 };
54
55 template <typename Type>
56 struct RecordListOf : ArrayOf<Record<Type> >
57 {
58   inline const Type& operator [] (unsigned int i) const
59   {
60     if (HB_UNLIKELY (i >= this->len)) return Null(Type);
61     return this+this->array[i].offset;
62   }
63   inline const Tag& get_tag (unsigned int i) const
64   {
65     if (HB_UNLIKELY (i >= this->len)) return Null(Tag);
66     return this->array[i].tag;
67   }
68 };
69
70
71 struct Script;
72 typedef Record<Script> ScriptRecord;
73 ASSERT_SIZE (ScriptRecord, 6);
74 struct LangSys;
75 typedef Record<LangSys> LangSysRecord;
76 ASSERT_SIZE (LangSysRecord, 6);
77 struct Feature;
78 typedef Record<Feature> FeatureRecord;
79 ASSERT_SIZE (FeatureRecord, 6);
80
81
82 struct LangSys
83 {
84   inline const unsigned int get_feature_index (unsigned int i) const { return featureIndex[i]; }
85   inline unsigned int get_feature_count (void) const { return featureIndex.len; }
86
87   inline bool has_required_feature (void) const { return reqFeatureIndex != 0xffff; }
88   inline int get_required_feature_index (void) const
89   {
90     if (reqFeatureIndex == 0xffff)
91       return NO_INDEX;
92    return reqFeatureIndex;;
93   }
94
95   Offset        lookupOrder;    /* = Null (reserved for an offset to a
96                                  * reordering table) */
97   USHORT        reqFeatureIndex;/* Index of a feature required for this
98                                  * language system--if no required features
99                                  * = 0xFFFF */
100   ArrayOf<USHORT>
101                 featureIndex;   /* Array of indices into the FeatureList */
102 };
103 ASSERT_SIZE_DATA (LangSys, 6, "\0\0\xFF\xFF");
104
105
106 struct Script
107 {
108   inline const LangSys& get_lang_sys (unsigned int i) const
109   {
110     if (i == NO_INDEX) return get_default_lang_sys ();
111     return this+langSys[i].offset;
112   }
113   inline unsigned int get_lang_sys_count (void) const { return langSys.len; }
114   inline const Tag& get_lang_sys_tag (unsigned int i) const { return langSys[i].tag; }
115
116   // LONGTERMTODO bsearch
117   DEFINE_TAG_FIND_INTERFACE (LangSys, lang_sys);        /* find_lang_sys_index (), get_lang_sys_by_tag (tag) */
118
119   inline bool has_default_lang_sys (void) const { return defaultLangSys != 0; }
120   inline const LangSys& get_default_lang_sys (void) const { return this+defaultLangSys; }
121
122   private:
123   OffsetTo<LangSys>
124                 defaultLangSys; /* Offset to DefaultLangSys table--from
125                                  * beginning of Script table--may be Null */
126   ArrayOf<LangSysRecord>
127                 langSys;        /* Array of LangSysRecords--listed
128                                  * alphabetically by LangSysTag */
129 };
130 ASSERT_SIZE (Script, 4);
131
132 typedef RecordListOf<Script> ScriptList;
133 ASSERT_SIZE (ScriptList, 2);
134
135
136 struct Feature
137 {
138   inline const unsigned int get_lookup_index (unsigned int i) const { return lookupIndex[i]; }
139   inline unsigned int get_lookup_count (void) const { return lookupIndex.len; }
140
141   /* TODO: implement get_feature_parameters() */
142   /* TODO: implement FeatureSize and other special features? */
143   Offset        featureParams;  /* Offset to Feature Parameters table (if one
144                                  * has been defined for the feature), relative
145                                  * to the beginning of the Feature Table; = Null
146                                  * if not required */
147   ArrayOf<USHORT>
148                 lookupIndex;    /* Array of LookupList indices */
149 };
150 ASSERT_SIZE (Feature, 4);
151
152 typedef RecordListOf<Feature> FeatureList;
153 ASSERT_SIZE (FeatureList, 2);
154
155
156 struct LookupFlag : USHORT
157 {
158   enum {
159     RightToLeft         = 0x0001u,
160     IgnoreBaseGlyphs    = 0x0002u,
161     IgnoreLigatures     = 0x0004u,
162     IgnoreMarks         = 0x0008u,
163     UseMarkFilteringSet = 0x0010u,
164     Reserved            = 0x00E0u,
165     MarkAttachmentType  = 0xFF00u,
166   };
167 };
168 ASSERT_SIZE (LookupFlag, 2);
169
170 struct LookupSubTable
171 {
172   private:
173   USHORT        format;         /* Subtable format.  Different for GSUB and GPOS */
174 };
175 ASSERT_SIZE (LookupSubTable, 2);
176
177 struct Lookup
178 {
179   inline const LookupSubTable& get_subtable (unsigned int i) const { return this+subTable[i]; }
180   inline unsigned int get_subtable_count (void) const { return subTable.len; }
181
182   inline unsigned int get_type (void) const { return lookupType; }
183   inline unsigned int get_flag (void) const
184   {
185     unsigned int flag = lookupFlag;
186     if (HB_UNLIKELY (flag & LookupFlag::UseMarkFilteringSet))
187     {
188       const USHORT &markFilteringSet = *(const USHORT*)
189                                         ((const char *) &subTable + subTable.get_size ());
190       flag += (markFilteringSet << 16);
191     }
192     return flag;
193   }
194
195   USHORT        lookupType;             /* Different enumerations for GSUB and GPOS */
196   USHORT        lookupFlag;             /* Lookup qualifiers */
197   OffsetArrayOf<LookupSubTable>
198                 subTable;               /* Array of SubTables */
199   USHORT        markFilteringSetX[0];   /* Index (base 0) into GDEF mark glyph sets
200                                          * structure. This field is only present if bit
201                                          * UseMarkFilteringSet of lookup flags is set. */
202 };
203 ASSERT_SIZE (Lookup, 6);
204
205 template <typename Type>
206 struct OffsetListOf : OffsetArrayOf<Type>
207 {
208   inline const Type& operator [] (unsigned int i) const
209   {
210     if (HB_UNLIKELY (i >= this->len)) return Null(Type);
211     return this+this->array[i];
212   }
213 };
214
215 typedef OffsetListOf<Lookup> LookupList;
216 ASSERT_SIZE (LookupList, 2);
217
218
219 /*
220  * Coverage Table
221  */
222
223 struct CoverageFormat1
224 {
225   friend struct Coverage;
226
227   private:
228   inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
229   {
230     if (HB_UNLIKELY (glyph_id > 0xFFFF))
231       return NOT_COVERED;
232     GlyphID gid;
233     gid = glyph_id;
234     // TODO: bsearch
235     unsigned int num_glyphs = glyphArray.len;
236     for (unsigned int i = 0; i < num_glyphs; i++)
237       if (gid == glyphArray[i])
238         return i;
239     return NOT_COVERED;
240   }
241
242   inline bool sanitize (SANITIZE_ARG_DEF) {
243     return SANITIZE (glyphArray);
244   }
245
246   private:
247   USHORT        coverageFormat; /* Format identifier--format = 1 */
248   ArrayOf<GlyphID>
249                 glyphArray;     /* Array of GlyphIDs--in numerical order */
250 };
251 ASSERT_SIZE (CoverageFormat1, 4);
252
253 struct CoverageRangeRecord
254 {
255   friend struct CoverageFormat2;
256
257   private:
258   inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
259   {
260     if (glyph_id >= start && glyph_id <= end)
261       return (unsigned int) startCoverageIndex + (glyph_id - start);
262     return NOT_COVERED;
263   }
264
265   inline bool sanitize (SANITIZE_ARG_DEF) {
266     return SANITIZE_SELF ();
267   }
268
269   private:
270   GlyphID       start;                  /* First GlyphID in the range */
271   GlyphID       end;                    /* Last GlyphID in the range */
272   USHORT        startCoverageIndex;     /* Coverage Index of first GlyphID in
273                                          * range */
274 };
275 ASSERT_SIZE_DATA (CoverageRangeRecord, 6, "\000\001");
276
277 struct CoverageFormat2
278 {
279   friend struct Coverage;
280
281   private:
282   inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
283   {
284     // TODO: bsearch
285     unsigned int count = rangeRecord.len;
286     for (unsigned int i = 0; i < count; i++)
287     {
288       unsigned int coverage = rangeRecord[i].get_coverage (glyph_id);
289       if (coverage != NOT_COVERED)
290         return coverage;
291     }
292     return NOT_COVERED;
293   }
294
295   inline bool sanitize (SANITIZE_ARG_DEF) {
296     return SANITIZE (rangeRecord);
297   }
298
299   private:
300   USHORT        coverageFormat; /* Format identifier--format = 2 */
301   ArrayOf<CoverageRangeRecord>
302                 rangeRecord;    /* Array of glyph ranges--ordered by
303                                  * Start GlyphID. rangeCount entries
304                                  * long */
305 };
306 ASSERT_SIZE (CoverageFormat2, 4);
307
308 struct Coverage
309 {
310   inline unsigned int operator() (hb_codepoint_t glyph_id) const { return get_coverage (glyph_id); }
311
312   unsigned int get_coverage (hb_codepoint_t glyph_id) const
313   {
314     switch (u.format) {
315     case 1: return u.format1->get_coverage(glyph_id);
316     case 2: return u.format2->get_coverage(glyph_id);
317     default:return NOT_COVERED;
318     }
319   }
320
321   inline bool sanitize (SANITIZE_ARG_DEF) {
322     if (!SANITIZE (u.format)) return false;
323     switch (u.format) {
324     case 1: return u.format1->sanitize (SANITIZE_ARG);
325     case 2: return u.format2->sanitize (SANITIZE_ARG);
326     default:return true;
327     }
328   }
329
330   private:
331   union {
332   USHORT                format;         /* Format identifier */
333   CoverageFormat1       format1[];
334   CoverageFormat2       format2[];
335   } u;
336 };
337 ASSERT_SIZE (Coverage, 2);
338
339
340 /*
341  * Class Definition Table
342  */
343
344 struct ClassDefFormat1
345 {
346   friend struct ClassDef;
347
348   private:
349   inline hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
350   {
351     if ((unsigned int) (glyph_id - startGlyph) < classValue.len)
352       return classValue[glyph_id - startGlyph];
353     return 0;
354   }
355
356   inline bool sanitize (SANITIZE_ARG_DEF) {
357     return SANITIZE_SELF () && SANITIZE (classValue);
358   }
359
360   USHORT        classFormat;            /* Format identifier--format = 1 */
361   GlyphID       startGlyph;             /* First GlyphID of the classValueArray */
362   ArrayOf<USHORT>
363                 classValue;             /* Array of Class Values--one per GlyphID */
364 };
365 ASSERT_SIZE (ClassDefFormat1, 6);
366
367 struct ClassRangeRecord
368 {
369   friend struct ClassDefFormat2;
370
371   private:
372   inline hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
373   {
374     if (glyph_id >= start && glyph_id <= end)
375       return classValue;
376     return 0;
377   }
378
379   inline bool sanitize (SANITIZE_ARG_DEF) {
380     return SANITIZE_SELF ();
381   }
382
383   private:
384   GlyphID       start;          /* First GlyphID in the range */
385   GlyphID       end;            /* Last GlyphID in the range */
386   USHORT        classValue;     /* Applied to all glyphs in the range */
387 };
388 ASSERT_SIZE_DATA (ClassRangeRecord, 6, "\000\001");
389
390 struct ClassDefFormat2
391 {
392   friend struct ClassDef;
393
394   private:
395   inline hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
396   {
397     // TODO: bsearch
398     unsigned int count = rangeRecord.len;
399     for (unsigned int i = 0; i < count; i++)
400     {
401       int classValue = rangeRecord[i].get_class (glyph_id);
402       if (classValue > 0)
403         return classValue;
404     }
405     return 0;
406   }
407
408   inline bool sanitize (SANITIZE_ARG_DEF) {
409     return SANITIZE (rangeRecord);
410   }
411
412   USHORT        classFormat;    /* Format identifier--format = 2 */
413   ArrayOf<ClassRangeRecord>
414                 rangeRecord;    /* Array of glyph ranges--ordered by
415                                  * Start GlyphID */
416 };
417 ASSERT_SIZE (ClassDefFormat2, 4);
418
419 struct ClassDef
420 {
421   inline unsigned int operator() (hb_codepoint_t glyph_id) const { return get_class (glyph_id); }
422
423   hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
424   {
425     switch (u.format) {
426     case 1: return u.format1->get_class(glyph_id);
427     case 2: return u.format2->get_class(glyph_id);
428     default:return 0;
429     }
430   }
431
432   inline bool sanitize (SANITIZE_ARG_DEF) {
433     if (!SANITIZE (u.format)) return false;
434     switch (u.format) {
435     case 1: return u.format1->sanitize (SANITIZE_ARG);
436     case 2: return u.format2->sanitize (SANITIZE_ARG);
437     default:return true;
438     }
439   }
440
441   private:
442   union {
443   USHORT                format;         /* Format identifier */
444   ClassDefFormat1       format1[];
445   ClassDefFormat2       format2[];
446   } u;
447 };
448 ASSERT_SIZE (ClassDef, 2);
449
450
451 /*
452  * Device Tables
453  */
454
455 struct Device
456 {
457   int get_delta (unsigned int ppem_size) const
458   {
459     unsigned int f = deltaFormat;
460     if (HB_UNLIKELY (f < 1 || f > 3))
461       return 0;
462
463     if (ppem_size < startSize || ppem_size > endSize)
464       return 0;
465
466     unsigned int s = ppem_size - startSize;
467
468     unsigned int byte = deltaValue[s >> (4 - f)];
469     unsigned int bits = (byte >> (16 - (((s & ((1 << (4 - f)) - 1)) + 1) << f)));
470     unsigned int mask = (0xFFFF >> (16 - (1 << f)));
471
472     int delta = bits & mask;
473
474     if (delta >= ((mask + 1) >> 1))
475       delta -= mask + 1;
476
477     return delta;
478   }
479
480   inline int operator() (unsigned int ppem_size) const { return get_delta (ppem_size); }
481
482   private:
483   USHORT        startSize;      /* Smallest size to correct--in ppem */
484   USHORT        endSize;        /* Largest size to correct--in ppem */
485   USHORT        deltaFormat;    /* Format of DeltaValue array data: 1, 2, or 3 */
486   USHORT        deltaValue[];   /* Array of compressed data */
487 };
488 ASSERT_SIZE (Device, 6);
489
490
491 #endif /* HB_OT_LAYOUT_COMMON_PRIVATE_HH */