- add sources.
[platform/framework/web/crosswalk.git] / src / third_party / harfbuzz-ng / src / hb-ot-layout-common-private.hh
1 /*
2  * Copyright © 2007,2008,2009  Red Hat, Inc.
3  * Copyright © 2010,2012  Google, Inc.
4  *
5  *  This is part of HarfBuzz, a text shaping library.
6  *
7  * Permission is hereby granted, without written agreement and without
8  * license or royalty fees, to use, copy, modify, and distribute this
9  * software and its documentation for any purpose, provided that the
10  * above copyright notice and the following two paragraphs appear in
11  * all copies of this software.
12  *
13  * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
14  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
15  * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
16  * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
17  * DAMAGE.
18  *
19  * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
20  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21  * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
22  * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
23  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
24  *
25  * Red Hat Author(s): Behdad Esfahbod
26  * Google Author(s): Behdad Esfahbod
27  */
28
29 #ifndef HB_OT_LAYOUT_COMMON_PRIVATE_HH
30 #define HB_OT_LAYOUT_COMMON_PRIVATE_HH
31
32 #include "hb-ot-layout-private.hh"
33 #include "hb-open-type-private.hh"
34 #include "hb-set-private.hh"
35
36
37 namespace OT {
38
39
40 #define NOT_COVERED             ((unsigned int) -1)
41 #define MAX_NESTING_LEVEL       8
42
43
44
45 /*
46  *
47  * OpenType Layout Common Table Formats
48  *
49  */
50
51
52 /*
53  * Script, ScriptList, LangSys, Feature, FeatureList, Lookup, LookupList
54  */
55
56 template <typename Type>
57 struct Record
58 {
59   inline int cmp (hb_tag_t a) const {
60     return tag.cmp (a);
61   }
62
63   struct sanitize_closure_t {
64     hb_tag_t tag;
65     void *list_base;
66   };
67   inline bool sanitize (hb_sanitize_context_t *c, void *base) {
68     TRACE_SANITIZE (this);
69     const sanitize_closure_t closure = {tag, base};
70     return TRACE_RETURN (c->check_struct (this) && offset.sanitize (c, base, &closure));
71   }
72
73   Tag           tag;            /* 4-byte Tag identifier */
74   OffsetTo<Type>
75                 offset;         /* Offset from beginning of object holding
76                                  * the Record */
77   public:
78   DEFINE_SIZE_STATIC (6);
79 };
80
81 template <typename Type>
82 struct RecordArrayOf : SortedArrayOf<Record<Type> > {
83   inline const Tag& get_tag (unsigned int i) const
84   {
85     /* We cheat slightly and don't define separate Null objects
86      * for Record types.  Instead, we return the correct Null(Tag)
87      * here. */
88     if (unlikely (i >= this->len)) return Null(Tag);
89     return (*this)[i].tag;
90   }
91   inline unsigned int get_tags (unsigned int start_offset,
92                                 unsigned int *record_count /* IN/OUT */,
93                                 hb_tag_t     *record_tags /* OUT */) const
94   {
95     if (record_count) {
96       const Record<Type> *arr = this->sub_array (start_offset, record_count);
97       unsigned int count = *record_count;
98       for (unsigned int i = 0; i < count; i++)
99         record_tags[i] = arr[i].tag;
100     }
101     return this->len;
102   }
103   inline bool find_index (hb_tag_t tag, unsigned int *index) const
104   {
105     int i = this->search (tag);
106     if (i != -1) {
107         if (index) *index = i;
108         return true;
109     } else {
110       if (index) *index = Index::NOT_FOUND_INDEX;
111       return false;
112     }
113   }
114 };
115
116 template <typename Type>
117 struct RecordListOf : RecordArrayOf<Type>
118 {
119   inline const Type& operator [] (unsigned int i) const
120   { return this+RecordArrayOf<Type>::operator [](i).offset; }
121
122   inline bool sanitize (hb_sanitize_context_t *c) {
123     TRACE_SANITIZE (this);
124     return TRACE_RETURN (RecordArrayOf<Type>::sanitize (c, this));
125   }
126 };
127
128
129 struct RangeRecord
130 {
131   inline int cmp (hb_codepoint_t g) const {
132     return g < start ? -1 : g <= end ? 0 : +1 ;
133   }
134
135   inline bool sanitize (hb_sanitize_context_t *c) {
136     TRACE_SANITIZE (this);
137     return TRACE_RETURN (c->check_struct (this));
138   }
139
140   inline bool intersects (const hb_set_t *glyphs) const {
141     return glyphs->intersects (start, end);
142   }
143
144   template <typename set_t>
145   inline void add_coverage (set_t *glyphs) const {
146     glyphs->add_range (start, end);
147   }
148
149   GlyphID       start;          /* First GlyphID in the range */
150   GlyphID       end;            /* Last GlyphID in the range */
151   USHORT        value;          /* Value */
152   public:
153   DEFINE_SIZE_STATIC (6);
154 };
155 DEFINE_NULL_DATA (RangeRecord, "\000\001");
156
157
158 struct IndexArray : ArrayOf<Index>
159 {
160   inline unsigned int get_indexes (unsigned int start_offset,
161                                    unsigned int *_count /* IN/OUT */,
162                                    unsigned int *_indexes /* OUT */) const
163   {
164     if (_count) {
165       const USHORT *arr = this->sub_array (start_offset, _count);
166       unsigned int count = *_count;
167       for (unsigned int i = 0; i < count; i++)
168         _indexes[i] = arr[i];
169     }
170     return this->len;
171   }
172 };
173
174
175 struct Script;
176 struct LangSys;
177 struct Feature;
178
179
180 struct LangSys
181 {
182   inline unsigned int get_feature_count (void) const
183   { return featureIndex.len; }
184   inline hb_tag_t get_feature_index (unsigned int i) const
185   { return featureIndex[i]; }
186   inline unsigned int get_feature_indexes (unsigned int start_offset,
187                                            unsigned int *feature_count /* IN/OUT */,
188                                            unsigned int *feature_indexes /* OUT */) const
189   { return featureIndex.get_indexes (start_offset, feature_count, feature_indexes); }
190
191   inline bool has_required_feature (void) const { return reqFeatureIndex != 0xffff; }
192   inline unsigned int get_required_feature_index (void) const
193   {
194     if (reqFeatureIndex == 0xffff)
195       return Index::NOT_FOUND_INDEX;
196    return reqFeatureIndex;;
197   }
198
199   inline bool sanitize (hb_sanitize_context_t *c,
200                         const Record<LangSys>::sanitize_closure_t * = NULL) {
201     TRACE_SANITIZE (this);
202     return TRACE_RETURN (c->check_struct (this) && featureIndex.sanitize (c));
203   }
204
205   Offset        lookupOrder;    /* = Null (reserved for an offset to a
206                                  * reordering table) */
207   USHORT        reqFeatureIndex;/* Index of a feature required for this
208                                  * language system--if no required features
209                                  * = 0xFFFF */
210   IndexArray    featureIndex;   /* Array of indices into the FeatureList */
211   public:
212   DEFINE_SIZE_ARRAY (6, featureIndex);
213 };
214 DEFINE_NULL_DATA (LangSys, "\0\0\xFF\xFF");
215
216
217 struct Script
218 {
219   inline unsigned int get_lang_sys_count (void) const
220   { return langSys.len; }
221   inline const Tag& get_lang_sys_tag (unsigned int i) const
222   { return langSys.get_tag (i); }
223   inline unsigned int get_lang_sys_tags (unsigned int start_offset,
224                                          unsigned int *lang_sys_count /* IN/OUT */,
225                                          hb_tag_t     *lang_sys_tags /* OUT */) const
226   { return langSys.get_tags (start_offset, lang_sys_count, lang_sys_tags); }
227   inline const LangSys& get_lang_sys (unsigned int i) const
228   {
229     if (i == Index::NOT_FOUND_INDEX) return get_default_lang_sys ();
230     return this+langSys[i].offset;
231   }
232   inline bool find_lang_sys_index (hb_tag_t tag, unsigned int *index) const
233   { return langSys.find_index (tag, index); }
234
235   inline bool has_default_lang_sys (void) const { return defaultLangSys != 0; }
236   inline const LangSys& get_default_lang_sys (void) const { return this+defaultLangSys; }
237
238   inline bool sanitize (hb_sanitize_context_t *c,
239                         const Record<Script>::sanitize_closure_t * = NULL) {
240     TRACE_SANITIZE (this);
241     return TRACE_RETURN (defaultLangSys.sanitize (c, this) && langSys.sanitize (c, this));
242   }
243
244   protected:
245   OffsetTo<LangSys>
246                 defaultLangSys; /* Offset to DefaultLangSys table--from
247                                  * beginning of Script table--may be Null */
248   RecordArrayOf<LangSys>
249                 langSys;        /* Array of LangSysRecords--listed
250                                  * alphabetically by LangSysTag */
251   public:
252   DEFINE_SIZE_ARRAY (4, langSys);
253 };
254
255 typedef RecordListOf<Script> ScriptList;
256
257
258 /* http://www.microsoft.com/typography/otspec/features_pt.htm#size */
259 struct FeatureParamsSize
260 {
261   inline bool sanitize (hb_sanitize_context_t *c) {
262     TRACE_SANITIZE (this);
263     if (unlikely (!c->check_struct (this))) return TRACE_RETURN (false);
264
265     /* This subtable has some "history", if you will.  Some earlier versions of
266      * Adobe tools calculated the offset of the FeatureParams sutable from the
267      * beginning of the FeatureList table!  Now, that is dealt with in the
268      * Feature implementation.  But we still need to be able to tell junk from
269      * real data.  Note: We don't check that the nameID actually exists.
270      *
271      * Read Roberts wrote on 9/15/06 on opentype-list@indx.co.uk :
272      *
273      * Yes, it is correct that a new version of the AFDKO (version 2.0) will be
274      * coming out soon, and that the makeotf program will build a font with a
275      * 'size' feature that is correct by the specification.
276      *
277      * The specification for this feature tag is in the "OpenType Layout Tag
278      * Registry". You can see a copy of this at:
279      * http://partners.adobe.com/public/developer/opentype/index_tag8.html#size
280      *
281      * Here is one set of rules to determine if the 'size' feature is built
282      * correctly, or as by the older versions of MakeOTF. You may be able to do
283      * better.
284      *
285      * Assume that the offset to the size feature is according to specification,
286      * and make the following value checks. If it fails, assume the the size
287      * feature is calculated as versions of MakeOTF before the AFDKO 2.0 built it.
288      * If this fails, reject the 'size' feature. The older makeOTF's calculated the
289      * offset from the beginning of the FeatureList table, rather than from the
290      * beginning of the 'size' Feature table.
291      *
292      * If "design size" == 0:
293      *     fails check
294      *
295      * Else if ("subfamily identifier" == 0 and
296      *     "range start" == 0 and
297      *     "range end" == 0 and
298      *     "range start" == 0 and
299      *     "menu name ID" == 0)
300      *     passes check: this is the format used when there is a design size
301      * specified, but there is no recommended size range.
302      *
303      * Else if ("design size" <  "range start" or
304      *     "design size" >   "range end" or
305      *     "range end" <= "range start" or
306      *     "menu name ID"  < 256 or
307      *     "menu name ID"  > 32767 or
308      *     menu name ID is not a name ID which is actually in the name table)
309      *     fails test
310      * Else
311      *     passes test.
312      */
313
314     if (!designSize)
315       return TRACE_RETURN (false);
316     else if (subfamilyID == 0 &&
317              subfamilyNameID == 0 &&
318              rangeStart == 0 &&
319              rangeEnd == 0)
320       return TRACE_RETURN (true);
321     else if (designSize < rangeStart ||
322              designSize > rangeEnd ||
323              subfamilyNameID < 256 ||
324              subfamilyNameID > 32767)
325       return TRACE_RETURN (false);
326     else
327       return TRACE_RETURN (true);
328   }
329
330   USHORT        designSize;     /* Represents the design size in 720/inch
331                                  * units (decipoints).  The design size entry
332                                  * must be non-zero.  When there is a design
333                                  * size but no recommended size range, the
334                                  * rest of the array will consist of zeros. */
335   USHORT        subfamilyID;    /* Has no independent meaning, but serves
336                                  * as an identifier that associates fonts
337                                  * in a subfamily. All fonts which share a
338                                  * Preferred or Font Family name and which
339                                  * differ only by size range shall have the
340                                  * same subfamily value, and no fonts which
341                                  * differ in weight or style shall have the
342                                  * same subfamily value. If this value is
343                                  * zero, the remaining fields in the array
344                                  * will be ignored. */
345   USHORT        subfamilyNameID;/* If the preceding value is non-zero, this
346                                  * value must be set in the range 256 - 32767
347                                  * (inclusive). It records the value of a
348                                  * field in the name table, which must
349                                  * contain English-language strings encoded
350                                  * in Windows Unicode and Macintosh Roman,
351                                  * and may contain additional strings
352                                  * localized to other scripts and languages.
353                                  * Each of these strings is the name an
354                                  * application should use, in combination
355                                  * with the family name, to represent the
356                                  * subfamily in a menu.  Applications will
357                                  * choose the appropriate version based on
358                                  * their selection criteria. */
359   USHORT        rangeStart;     /* Large end of the recommended usage range
360                                  * (inclusive), stored in 720/inch units
361                                  * (decipoints). */
362   USHORT        rangeEnd;       /* Small end of the recommended usage range
363                                    (exclusive), stored in 720/inch units
364                                  * (decipoints). */
365   public:
366   DEFINE_SIZE_STATIC (10);
367 };
368
369 /* http://www.microsoft.com/typography/otspec/features_pt.htm#ssxx */
370 struct FeatureParamsStylisticSet
371 {
372   inline bool sanitize (hb_sanitize_context_t *c) {
373     TRACE_SANITIZE (this);
374     /* Right now minorVersion is at zero.  Which means, any table supports
375      * the uiNameID field. */
376     return TRACE_RETURN (c->check_struct (this));
377   }
378
379   USHORT        minorVersion;   /* (set to 0): This corresponds to a “minor”
380                                  * version number. Additional data may be
381                                  * added to the end of this Feature Parameters
382                                  * table in the future. */
383
384   USHORT        uiNameID;       /* The 'name' table name ID that specifies a
385                                  * string (or strings, for multiple languages)
386                                  * for a user-interface label for this
387                                  * feature.  The values of uiLabelNameId and
388                                  * sampleTextNameId are expected to be in the
389                                  * font-specific name ID range (256-32767),
390                                  * though that is not a requirement in this
391                                  * Feature Parameters specification. The
392                                  * user-interface label for the feature can
393                                  * be provided in multiple languages. An
394                                  * English string should be included as a
395                                  * fallback. The string should be kept to a
396                                  * minimal length to fit comfortably with
397                                  * different application interfaces. */
398   public:
399   DEFINE_SIZE_STATIC (4);
400 };
401
402 struct FeatureParamsCharacterVariants
403 {
404   inline bool sanitize (hb_sanitize_context_t *c) {
405     TRACE_SANITIZE (this);
406     return TRACE_RETURN (c->check_struct (this) &&
407                          characters.sanitize (c));
408   }
409
410   USHORT        format;                 /* Format number is set to 0. */
411   USHORT        featUILableNameID;      /* The ‘name’ table name ID that
412                                          * specifies a string (or strings,
413                                          * for multiple languages) for a
414                                          * user-interface label for this
415                                          * feature. (May be NULL.) */
416   USHORT        featUITooltipTextNameID;/* The ‘name’ table name ID that
417                                          * specifies a string (or strings,
418                                          * for multiple languages) that an
419                                          * application can use for tooltip
420                                          * text for this feature. (May be
421                                          * NULL.) */
422   USHORT        sampleTextNameID;       /* The ‘name’ table name ID that
423                                          * specifies sample text that
424                                          * illustrates the effect of this
425                                          * feature. (May be NULL.) */
426   USHORT        numNamedParameters;     /* Number of named parameters. (May
427                                          * be zero.) */
428   USHORT        firstParamUILabelNameID;/* The first ‘name’ table name ID
429                                          * used to specify strings for
430                                          * user-interface labels for the
431                                          * feature parameters. (Must be zero
432                                          * if numParameters is zero.) */
433   ArrayOf<UINT24>
434                 characters;             /* Array of the Unicode Scalar Value
435                                          * of the characters for which this
436                                          * feature provides glyph variants.
437                                          * (May be zero.) */
438   public:
439   DEFINE_SIZE_ARRAY (14, characters);
440 };
441
442 struct FeatureParams
443 {
444   inline bool sanitize (hb_sanitize_context_t *c, hb_tag_t tag) {
445     TRACE_SANITIZE (this);
446     if (tag == HB_TAG ('s','i','z','e'))
447       return TRACE_RETURN (u.size.sanitize (c));
448     if ((tag & 0xFFFF0000) == HB_TAG ('s','s','\0','\0')) /* ssXX */
449       return TRACE_RETURN (u.stylisticSet.sanitize (c));
450     if ((tag & 0xFFFF0000) == HB_TAG ('c','v','\0','\0')) /* cvXX */
451       return TRACE_RETURN (u.characterVariants.sanitize (c));
452     return TRACE_RETURN (true);
453   }
454
455   inline const FeatureParamsSize& get_size_params (hb_tag_t tag) const
456   {
457     if (tag == HB_TAG ('s','i','z','e'))
458       return u.size;
459     return Null(FeatureParamsSize);
460   }
461
462   private:
463   union {
464   FeatureParamsSize                     size;
465   FeatureParamsStylisticSet             stylisticSet;
466   FeatureParamsCharacterVariants        characterVariants;
467   } u;
468   DEFINE_SIZE_STATIC (17);
469 };
470
471 struct Feature
472 {
473   inline unsigned int get_lookup_count (void) const
474   { return lookupIndex.len; }
475   inline hb_tag_t get_lookup_index (unsigned int i) const
476   { return lookupIndex[i]; }
477   inline unsigned int get_lookup_indexes (unsigned int start_index,
478                                           unsigned int *lookup_count /* IN/OUT */,
479                                           unsigned int *lookup_tags /* OUT */) const
480   { return lookupIndex.get_indexes (start_index, lookup_count, lookup_tags); }
481
482   inline const FeatureParams &get_feature_params (void) const
483   { return this+featureParams; }
484
485   inline bool sanitize (hb_sanitize_context_t *c,
486                         const Record<Feature>::sanitize_closure_t *closure) {
487     TRACE_SANITIZE (this);
488     if (unlikely (!(c->check_struct (this) && lookupIndex.sanitize (c))))
489       return TRACE_RETURN (false);
490
491     /* Some earlier versions of Adobe tools calculated the offset of the
492      * FeatureParams subtable from the beginning of the FeatureList table!
493      *
494      * If sanitizing "failed" for the FeatureParams subtable, try it with the
495      * alternative location.  We would know sanitize "failed" if old value
496      * of the offset was non-zero, but it's zeroed now.
497      *
498      * Only do this for the 'size' feature, since at the time of the faulty
499      * Adobe tools, only the 'size' feature had FeatureParams defined.
500      */
501
502     Offset orig_offset = featureParams;
503     if (unlikely (!featureParams.sanitize (c, this, closure ? closure->tag : HB_TAG_NONE)))
504       return TRACE_RETURN (false);
505
506     if (likely (!orig_offset))
507       return TRACE_RETURN (true);
508
509     if (featureParams == 0 && closure &&
510         closure->tag == HB_TAG ('s','i','z','e') &&
511         closure->list_base && closure->list_base < this)
512     {
513       unsigned int new_offset_int = (unsigned int) orig_offset -
514                                     ((char *) this - (char *) closure->list_base);
515
516       Offset new_offset;
517       /* Check that it did not overflow. */
518       new_offset.set (new_offset_int);
519       if (new_offset == new_offset_int &&
520           featureParams.try_set (c, new_offset) &&
521           !featureParams.sanitize (c, this, closure ? closure->tag : HB_TAG_NONE))
522         return TRACE_RETURN (false);
523     }
524
525     return TRACE_RETURN (true);
526   }
527
528   OffsetTo<FeatureParams>
529                  featureParams; /* Offset to Feature Parameters table (if one
530                                  * has been defined for the feature), relative
531                                  * to the beginning of the Feature Table; = Null
532                                  * if not required */
533   IndexArray     lookupIndex;   /* Array of LookupList indices */
534   public:
535   DEFINE_SIZE_ARRAY (4, lookupIndex);
536 };
537
538 typedef RecordListOf<Feature> FeatureList;
539
540
541 struct LookupFlag : USHORT
542 {
543   enum Flags {
544     RightToLeft         = 0x0001u,
545     IgnoreBaseGlyphs    = 0x0002u,
546     IgnoreLigatures     = 0x0004u,
547     IgnoreMarks         = 0x0008u,
548     IgnoreFlags         = 0x000Eu,
549     UseMarkFilteringSet = 0x0010u,
550     Reserved            = 0x00E0u,
551     MarkAttachmentType  = 0xFF00u
552   };
553   public:
554   DEFINE_SIZE_STATIC (2);
555 };
556
557 struct Lookup
558 {
559   inline unsigned int get_subtable_count (void) const { return subTable.len; }
560
561   inline unsigned int get_type (void) const { return lookupType; }
562
563   /* lookup_props is a 32-bit integer where the lower 16-bit is LookupFlag and
564    * higher 16-bit is mark-filtering-set if the lookup uses one.
565    * Not to be confused with glyph_props which is very similar. */
566   inline uint32_t get_props (void) const
567   {
568     unsigned int flag = lookupFlag;
569     if (unlikely (flag & LookupFlag::UseMarkFilteringSet))
570     {
571       const USHORT &markFilteringSet = StructAfter<USHORT> (subTable);
572       flag += (markFilteringSet << 16);
573     }
574     return flag;
575   }
576
577   inline bool serialize (hb_serialize_context_t *c,
578                          unsigned int lookup_type,
579                          uint32_t lookup_props,
580                          unsigned int num_subtables)
581   {
582     TRACE_SERIALIZE (this);
583     if (unlikely (!c->extend_min (*this))) return TRACE_RETURN (false);
584     lookupType.set (lookup_type);
585     lookupFlag.set (lookup_props & 0xFFFF);
586     if (unlikely (!subTable.serialize (c, num_subtables))) return TRACE_RETURN (false);
587     if (lookupFlag & LookupFlag::UseMarkFilteringSet)
588     {
589       USHORT &markFilteringSet = StructAfter<USHORT> (subTable);
590       markFilteringSet.set (lookup_props >> 16);
591     }
592     return TRACE_RETURN (true);
593   }
594
595   inline bool sanitize (hb_sanitize_context_t *c) {
596     TRACE_SANITIZE (this);
597     /* Real sanitize of the subtables is done by GSUB/GPOS/... */
598     if (!(c->check_struct (this) && subTable.sanitize (c))) return TRACE_RETURN (false);
599     if (lookupFlag & LookupFlag::UseMarkFilteringSet)
600     {
601       USHORT &markFilteringSet = StructAfter<USHORT> (subTable);
602       if (!markFilteringSet.sanitize (c)) return TRACE_RETURN (false);
603     }
604     return TRACE_RETURN (true);
605   }
606
607   USHORT        lookupType;             /* Different enumerations for GSUB and GPOS */
608   USHORT        lookupFlag;             /* Lookup qualifiers */
609   ArrayOf<Offset>
610                 subTable;               /* Array of SubTables */
611   USHORT        markFilteringSetX[VAR]; /* Index (base 0) into GDEF mark glyph sets
612                                          * structure. This field is only present if bit
613                                          * UseMarkFilteringSet of lookup flags is set. */
614   public:
615   DEFINE_SIZE_ARRAY2 (6, subTable, markFilteringSetX);
616 };
617
618 typedef OffsetListOf<Lookup> LookupList;
619
620
621 /*
622  * Coverage Table
623  */
624
625 struct CoverageFormat1
626 {
627   friend struct Coverage;
628
629   private:
630   inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
631   {
632     int i = glyphArray.search (glyph_id);
633     ASSERT_STATIC (((unsigned int) -1) == NOT_COVERED);
634     return i;
635   }
636
637   inline bool serialize (hb_serialize_context_t *c,
638                          Supplier<GlyphID> &glyphs,
639                          unsigned int num_glyphs)
640   {
641     TRACE_SERIALIZE (this);
642     if (unlikely (!c->extend_min (*this))) return TRACE_RETURN (false);
643     glyphArray.len.set (num_glyphs);
644     if (unlikely (!c->extend (glyphArray))) return TRACE_RETURN (false);
645     for (unsigned int i = 0; i < num_glyphs; i++)
646       glyphArray[i] = glyphs[i];
647     glyphs.advance (num_glyphs);
648     return TRACE_RETURN (true);
649   }
650
651   inline bool sanitize (hb_sanitize_context_t *c) {
652     TRACE_SANITIZE (this);
653     return TRACE_RETURN (glyphArray.sanitize (c));
654   }
655
656   inline bool intersects_coverage (const hb_set_t *glyphs, unsigned int index) const {
657     return glyphs->has (glyphArray[index]);
658   }
659
660   template <typename set_t>
661   inline void add_coverage (set_t *glyphs) const {
662     unsigned int count = glyphArray.len;
663     for (unsigned int i = 0; i < count; i++)
664       glyphs->add (glyphArray[i]);
665   }
666
667   public:
668   /* Older compilers need this to be public. */
669   struct Iter {
670     inline void init (const struct CoverageFormat1 &c_) { c = &c_; i = 0; };
671     inline bool more (void) { return i < c->glyphArray.len; }
672     inline void next (void) { i++; }
673     inline uint16_t get_glyph (void) { return c->glyphArray[i]; }
674     inline uint16_t get_coverage (void) { return i; }
675
676     private:
677     const struct CoverageFormat1 *c;
678     unsigned int i;
679   };
680   private:
681
682   protected:
683   USHORT        coverageFormat; /* Format identifier--format = 1 */
684   SortedArrayOf<GlyphID>
685                 glyphArray;     /* Array of GlyphIDs--in numerical order */
686   public:
687   DEFINE_SIZE_ARRAY (4, glyphArray);
688 };
689
690 struct CoverageFormat2
691 {
692   friend struct Coverage;
693
694   private:
695   inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
696   {
697     int i = rangeRecord.search (glyph_id);
698     if (i != -1) {
699       const RangeRecord &range = rangeRecord[i];
700       return (unsigned int) range.value + (glyph_id - range.start);
701     }
702     return NOT_COVERED;
703   }
704
705   inline bool serialize (hb_serialize_context_t *c,
706                          Supplier<GlyphID> &glyphs,
707                          unsigned int num_glyphs)
708   {
709     TRACE_SERIALIZE (this);
710     if (unlikely (!c->extend_min (*this))) return TRACE_RETURN (false);
711
712     if (unlikely (!num_glyphs)) return TRACE_RETURN (true);
713
714     unsigned int num_ranges = 1;
715     for (unsigned int i = 1; i < num_glyphs; i++)
716       if (glyphs[i - 1] + 1 != glyphs[i])
717         num_ranges++;
718     rangeRecord.len.set (num_ranges);
719     if (unlikely (!c->extend (rangeRecord))) return TRACE_RETURN (false);
720
721     unsigned int range = 0;
722     rangeRecord[range].start = glyphs[0];
723     rangeRecord[range].value.set (0);
724     for (unsigned int i = 1; i < num_glyphs; i++)
725       if (glyphs[i - 1] + 1 != glyphs[i]) {
726         range++;
727         rangeRecord[range].start = glyphs[i];
728         rangeRecord[range].value.set (i);
729         rangeRecord[range].end = glyphs[i];
730       } else {
731         rangeRecord[range].end = glyphs[i];
732       }
733     glyphs.advance (num_glyphs);
734     return TRACE_RETURN (true);
735   }
736
737   inline bool sanitize (hb_sanitize_context_t *c) {
738     TRACE_SANITIZE (this);
739     return TRACE_RETURN (rangeRecord.sanitize (c));
740   }
741
742   inline bool intersects_coverage (const hb_set_t *glyphs, unsigned int index) const {
743     unsigned int i;
744     unsigned int count = rangeRecord.len;
745     for (i = 0; i < count; i++) {
746       const RangeRecord &range = rangeRecord[i];
747       if (range.value <= index &&
748           index < (unsigned int) range.value + (range.end - range.start) &&
749           range.intersects (glyphs))
750         return true;
751       else if (index < range.value)
752         return false;
753     }
754     return false;
755   }
756
757   template <typename set_t>
758   inline void add_coverage (set_t *glyphs) const {
759     unsigned int count = rangeRecord.len;
760     for (unsigned int i = 0; i < count; i++)
761       rangeRecord[i].add_coverage (glyphs);
762   }
763
764   public:
765   /* Older compilers need this to be public. */
766   struct Iter {
767     inline void init (const CoverageFormat2 &c_) {
768       c = &c_;
769       coverage = 0;
770       i = 0;
771       j = c->rangeRecord.len ? c_.rangeRecord[0].start : 0;
772     }
773     inline bool more (void) { return i < c->rangeRecord.len; }
774     inline void next (void) {
775       coverage++;
776       if (j == c->rangeRecord[i].end) {
777         i++;
778         if (more ())
779           j = c->rangeRecord[i].start;
780         return;
781       }
782       j++;
783     }
784     inline uint16_t get_glyph (void) { return j; }
785     inline uint16_t get_coverage (void) { return coverage; }
786
787     private:
788     const struct CoverageFormat2 *c;
789     unsigned int i, j, coverage;
790   };
791   private:
792
793   protected:
794   USHORT        coverageFormat; /* Format identifier--format = 2 */
795   SortedArrayOf<RangeRecord>
796                 rangeRecord;    /* Array of glyph ranges--ordered by
797                                  * Start GlyphID. rangeCount entries
798                                  * long */
799   public:
800   DEFINE_SIZE_ARRAY (4, rangeRecord);
801 };
802
803 struct Coverage
804 {
805   inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
806   {
807     switch (u.format) {
808     case 1: return u.format1.get_coverage(glyph_id);
809     case 2: return u.format2.get_coverage(glyph_id);
810     default:return NOT_COVERED;
811     }
812   }
813
814   inline bool serialize (hb_serialize_context_t *c,
815                          Supplier<GlyphID> &glyphs,
816                          unsigned int num_glyphs)
817   {
818     TRACE_SERIALIZE (this);
819     if (unlikely (!c->extend_min (*this))) return TRACE_RETURN (false);
820     unsigned int num_ranges = 1;
821     for (unsigned int i = 1; i < num_glyphs; i++)
822       if (glyphs[i - 1] + 1 != glyphs[i])
823         num_ranges++;
824     u.format.set (num_glyphs * 2 < num_ranges * 3 ? 1 : 2);
825     switch (u.format) {
826     case 1: return TRACE_RETURN (u.format1.serialize (c, glyphs, num_glyphs));
827     case 2: return TRACE_RETURN (u.format2.serialize (c, glyphs, num_glyphs));
828     default:return TRACE_RETURN (false);
829     }
830   }
831
832   inline bool sanitize (hb_sanitize_context_t *c) {
833     TRACE_SANITIZE (this);
834     if (!u.format.sanitize (c)) return TRACE_RETURN (false);
835     switch (u.format) {
836     case 1: return TRACE_RETURN (u.format1.sanitize (c));
837     case 2: return TRACE_RETURN (u.format2.sanitize (c));
838     default:return TRACE_RETURN (true);
839     }
840   }
841
842   inline bool intersects (const hb_set_t *glyphs) const {
843     /* TODO speed this up */
844     Coverage::Iter iter;
845     for (iter.init (*this); iter.more (); iter.next ()) {
846       if (glyphs->has (iter.get_glyph ()))
847         return true;
848     }
849     return false;
850   }
851
852   inline bool intersects_coverage (const hb_set_t *glyphs, unsigned int index) const {
853     switch (u.format) {
854     case 1: return u.format1.intersects_coverage (glyphs, index);
855     case 2: return u.format2.intersects_coverage (glyphs, index);
856     default:return false;
857     }
858   }
859
860   template <typename set_t>
861   inline void add_coverage (set_t *glyphs) const {
862     switch (u.format) {
863     case 1: u.format1.add_coverage (glyphs); break;
864     case 2: u.format2.add_coverage (glyphs); break;
865     default:                                 break;
866     }
867   }
868
869   struct Iter {
870     Iter (void) : format (0) {};
871     inline void init (const Coverage &c_) {
872       format = c_.u.format;
873       switch (format) {
874       case 1: return u.format1.init (c_.u.format1);
875       case 2: return u.format2.init (c_.u.format2);
876       default:return;
877       }
878     }
879     inline bool more (void) {
880       switch (format) {
881       case 1: return u.format1.more ();
882       case 2: return u.format2.more ();
883       default:return true;
884       }
885     }
886     inline void next (void) {
887       switch (format) {
888       case 1: u.format1.next (); break;
889       case 2: u.format2.next (); break;
890       default:                   break;
891       }
892     }
893     inline uint16_t get_glyph (void) {
894       switch (format) {
895       case 1: return u.format1.get_glyph ();
896       case 2: return u.format2.get_glyph ();
897       default:return true;
898       }
899     }
900     inline uint16_t get_coverage (void) {
901       switch (format) {
902       case 1: return u.format1.get_coverage ();
903       case 2: return u.format2.get_coverage ();
904       default:return true;
905       }
906     }
907
908     private:
909     unsigned int format;
910     union {
911     CoverageFormat1::Iter       format1;
912     CoverageFormat2::Iter       format2;
913     } u;
914   };
915
916   protected:
917   union {
918   USHORT                format;         /* Format identifier */
919   CoverageFormat1       format1;
920   CoverageFormat2       format2;
921   } u;
922   public:
923   DEFINE_SIZE_UNION (2, format);
924 };
925
926
927 /*
928  * Class Definition Table
929  */
930
931 struct ClassDefFormat1
932 {
933   friend struct ClassDef;
934
935   private:
936   inline unsigned int get_class (hb_codepoint_t glyph_id) const
937   {
938     if (unlikely ((unsigned int) (glyph_id - startGlyph) < classValue.len))
939       return classValue[glyph_id - startGlyph];
940     return 0;
941   }
942
943   inline bool sanitize (hb_sanitize_context_t *c) {
944     TRACE_SANITIZE (this);
945     return TRACE_RETURN (c->check_struct (this) && classValue.sanitize (c));
946   }
947
948   template <typename set_t>
949   inline void add_class (set_t *glyphs, unsigned int klass) const {
950     unsigned int count = classValue.len;
951     for (unsigned int i = 0; i < count; i++)
952       if (classValue[i] == klass)
953         glyphs->add (startGlyph + i);
954   }
955
956   inline bool intersects_class (const hb_set_t *glyphs, unsigned int klass) const {
957     unsigned int count = classValue.len;
958     for (unsigned int i = 0; i < count; i++)
959       if (classValue[i] == klass && glyphs->has (startGlyph + i))
960         return true;
961     return false;
962   }
963
964   protected:
965   USHORT        classFormat;            /* Format identifier--format = 1 */
966   GlyphID       startGlyph;             /* First GlyphID of the classValueArray */
967   ArrayOf<USHORT>
968                 classValue;             /* Array of Class Values--one per GlyphID */
969   public:
970   DEFINE_SIZE_ARRAY (6, classValue);
971 };
972
973 struct ClassDefFormat2
974 {
975   friend struct ClassDef;
976
977   private:
978   inline unsigned int get_class (hb_codepoint_t glyph_id) const
979   {
980     int i = rangeRecord.search (glyph_id);
981     if (i != -1)
982       return rangeRecord[i].value;
983     return 0;
984   }
985
986   inline bool sanitize (hb_sanitize_context_t *c) {
987     TRACE_SANITIZE (this);
988     return TRACE_RETURN (rangeRecord.sanitize (c));
989   }
990
991   template <typename set_t>
992   inline void add_class (set_t *glyphs, unsigned int klass) const {
993     unsigned int count = rangeRecord.len;
994     for (unsigned int i = 0; i < count; i++)
995       if (rangeRecord[i].value == klass)
996         rangeRecord[i].add_coverage (glyphs);
997   }
998
999   inline bool intersects_class (const hb_set_t *glyphs, unsigned int klass) const {
1000     unsigned int count = rangeRecord.len;
1001     for (unsigned int i = 0; i < count; i++)
1002       if (rangeRecord[i].value == klass && rangeRecord[i].intersects (glyphs))
1003         return true;
1004     return false;
1005   }
1006
1007   protected:
1008   USHORT        classFormat;    /* Format identifier--format = 2 */
1009   SortedArrayOf<RangeRecord>
1010                 rangeRecord;    /* Array of glyph ranges--ordered by
1011                                  * Start GlyphID */
1012   public:
1013   DEFINE_SIZE_ARRAY (4, rangeRecord);
1014 };
1015
1016 struct ClassDef
1017 {
1018   inline unsigned int get_class (hb_codepoint_t glyph_id) const
1019   {
1020     switch (u.format) {
1021     case 1: return u.format1.get_class(glyph_id);
1022     case 2: return u.format2.get_class(glyph_id);
1023     default:return 0;
1024     }
1025   }
1026
1027   inline bool sanitize (hb_sanitize_context_t *c) {
1028     TRACE_SANITIZE (this);
1029     if (!u.format.sanitize (c)) return TRACE_RETURN (false);
1030     switch (u.format) {
1031     case 1: return TRACE_RETURN (u.format1.sanitize (c));
1032     case 2: return TRACE_RETURN (u.format2.sanitize (c));
1033     default:return TRACE_RETURN (true);
1034     }
1035   }
1036
1037   inline void add_class (hb_set_t *glyphs, unsigned int klass) const {
1038     switch (u.format) {
1039     case 1: u.format1.add_class (glyphs, klass); return;
1040     case 2: u.format2.add_class (glyphs, klass); return;
1041     default:return;
1042     }
1043   }
1044
1045   inline bool intersects_class (const hb_set_t *glyphs, unsigned int klass) const {
1046     switch (u.format) {
1047     case 1: return u.format1.intersects_class (glyphs, klass);
1048     case 2: return u.format2.intersects_class (glyphs, klass);
1049     default:return false;
1050     }
1051   }
1052
1053   protected:
1054   union {
1055   USHORT                format;         /* Format identifier */
1056   ClassDefFormat1       format1;
1057   ClassDefFormat2       format2;
1058   } u;
1059   public:
1060   DEFINE_SIZE_UNION (2, format);
1061 };
1062
1063
1064 /*
1065  * Device Tables
1066  */
1067
1068 struct Device
1069 {
1070
1071   inline hb_position_t get_x_delta (hb_font_t *font) const
1072   { return get_delta (font->x_ppem, font->x_scale); }
1073
1074   inline hb_position_t get_y_delta (hb_font_t *font) const
1075   { return get_delta (font->y_ppem, font->y_scale); }
1076
1077   inline int get_delta (unsigned int ppem, int scale) const
1078   {
1079     if (!ppem) return 0;
1080
1081     int pixels = get_delta_pixels (ppem);
1082
1083     if (!pixels) return 0;
1084
1085     return pixels * (int64_t) scale / ppem;
1086   }
1087
1088
1089   inline int get_delta_pixels (unsigned int ppem_size) const
1090   {
1091     unsigned int f = deltaFormat;
1092     if (unlikely (f < 1 || f > 3))
1093       return 0;
1094
1095     if (ppem_size < startSize || ppem_size > endSize)
1096       return 0;
1097
1098     unsigned int s = ppem_size - startSize;
1099
1100     unsigned int byte = deltaValue[s >> (4 - f)];
1101     unsigned int bits = (byte >> (16 - (((s & ((1 << (4 - f)) - 1)) + 1) << f)));
1102     unsigned int mask = (0xFFFF >> (16 - (1 << f)));
1103
1104     int delta = bits & mask;
1105
1106     if ((unsigned int) delta >= ((mask + 1) >> 1))
1107       delta -= mask + 1;
1108
1109     return delta;
1110   }
1111
1112   inline unsigned int get_size (void) const
1113   {
1114     unsigned int f = deltaFormat;
1115     if (unlikely (f < 1 || f > 3 || startSize > endSize)) return 3 * USHORT::static_size;
1116     return USHORT::static_size * (4 + ((endSize - startSize) >> (4 - f)));
1117   }
1118
1119   inline bool sanitize (hb_sanitize_context_t *c) {
1120     TRACE_SANITIZE (this);
1121     return TRACE_RETURN (c->check_struct (this) && c->check_range (this, this->get_size ()));
1122   }
1123
1124   protected:
1125   USHORT        startSize;              /* Smallest size to correct--in ppem */
1126   USHORT        endSize;                /* Largest size to correct--in ppem */
1127   USHORT        deltaFormat;            /* Format of DeltaValue array data: 1, 2, or 3
1128                                          * 1    Signed 2-bit value, 8 values per uint16
1129                                          * 2    Signed 4-bit value, 4 values per uint16
1130                                          * 3    Signed 8-bit value, 2 values per uint16
1131                                          */
1132   USHORT        deltaValue[VAR];        /* Array of compressed data */
1133   public:
1134   DEFINE_SIZE_ARRAY (6, deltaValue);
1135 };
1136
1137
1138 } /* namespace OT */
1139
1140
1141 #endif /* HB_OT_LAYOUT_COMMON_PRIVATE_HH */