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