michael@0: /* michael@0: * Copyright © 2007,2008,2009 Red Hat, Inc. michael@0: * Copyright © 2010,2012 Google, Inc. michael@0: * michael@0: * This is part of HarfBuzz, a text shaping library. michael@0: * michael@0: * Permission is hereby granted, without written agreement and without michael@0: * license or royalty fees, to use, copy, modify, and distribute this michael@0: * software and its documentation for any purpose, provided that the michael@0: * above copyright notice and the following two paragraphs appear in michael@0: * all copies of this software. michael@0: * michael@0: * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR michael@0: * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES michael@0: * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN michael@0: * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH michael@0: * DAMAGE. michael@0: * michael@0: * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, michael@0: * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND michael@0: * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS michael@0: * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO michael@0: * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. michael@0: * michael@0: * Red Hat Author(s): Behdad Esfahbod michael@0: * Google Author(s): Behdad Esfahbod michael@0: */ michael@0: michael@0: #ifndef HB_OT_LAYOUT_COMMON_PRIVATE_HH michael@0: #define HB_OT_LAYOUT_COMMON_PRIVATE_HH michael@0: michael@0: #include "hb-ot-layout-private.hh" michael@0: #include "hb-open-type-private.hh" michael@0: #include "hb-set-private.hh" michael@0: michael@0: michael@0: namespace OT { michael@0: michael@0: michael@0: #define NOT_COVERED ((unsigned int) -1) michael@0: #define MAX_NESTING_LEVEL 8 michael@0: #define MAX_CONTEXT_LENGTH 64 michael@0: michael@0: michael@0: michael@0: /* michael@0: * michael@0: * OpenType Layout Common Table Formats michael@0: * michael@0: */ michael@0: michael@0: michael@0: /* michael@0: * Script, ScriptList, LangSys, Feature, FeatureList, Lookup, LookupList michael@0: */ michael@0: michael@0: template michael@0: struct Record michael@0: { michael@0: inline int cmp (hb_tag_t a) const { michael@0: return tag.cmp (a); michael@0: } michael@0: michael@0: struct sanitize_closure_t { michael@0: hb_tag_t tag; michael@0: void *list_base; michael@0: }; michael@0: inline bool sanitize (hb_sanitize_context_t *c, void *base) { michael@0: TRACE_SANITIZE (this); michael@0: const sanitize_closure_t closure = {tag, base}; michael@0: return TRACE_RETURN (c->check_struct (this) && offset.sanitize (c, base, &closure)); michael@0: } michael@0: michael@0: Tag tag; /* 4-byte Tag identifier */ michael@0: OffsetTo michael@0: offset; /* Offset from beginning of object holding michael@0: * the Record */ michael@0: public: michael@0: DEFINE_SIZE_STATIC (6); michael@0: }; michael@0: michael@0: template michael@0: struct RecordArrayOf : SortedArrayOf > { michael@0: inline const Tag& get_tag (unsigned int i) const michael@0: { michael@0: /* We cheat slightly and don't define separate Null objects michael@0: * for Record types. Instead, we return the correct Null(Tag) michael@0: * here. */ michael@0: if (unlikely (i >= this->len)) return Null(Tag); michael@0: return (*this)[i].tag; michael@0: } michael@0: inline unsigned int get_tags (unsigned int start_offset, michael@0: unsigned int *record_count /* IN/OUT */, michael@0: hb_tag_t *record_tags /* OUT */) const michael@0: { michael@0: if (record_count) { michael@0: const Record *arr = this->sub_array (start_offset, record_count); michael@0: unsigned int count = *record_count; michael@0: for (unsigned int i = 0; i < count; i++) michael@0: record_tags[i] = arr[i].tag; michael@0: } michael@0: return this->len; michael@0: } michael@0: inline bool find_index (hb_tag_t tag, unsigned int *index) const michael@0: { michael@0: int i = this->search (tag); michael@0: if (i != -1) { michael@0: if (index) *index = i; michael@0: return true; michael@0: } else { michael@0: if (index) *index = Index::NOT_FOUND_INDEX; michael@0: return false; michael@0: } michael@0: } michael@0: }; michael@0: michael@0: template michael@0: struct RecordListOf : RecordArrayOf michael@0: { michael@0: inline const Type& operator [] (unsigned int i) const michael@0: { return this+RecordArrayOf::operator [](i).offset; } michael@0: michael@0: inline bool sanitize (hb_sanitize_context_t *c) { michael@0: TRACE_SANITIZE (this); michael@0: return TRACE_RETURN (RecordArrayOf::sanitize (c, this)); michael@0: } michael@0: }; michael@0: michael@0: michael@0: struct RangeRecord michael@0: { michael@0: inline int cmp (hb_codepoint_t g) const { michael@0: return g < start ? -1 : g <= end ? 0 : +1 ; michael@0: } michael@0: michael@0: inline bool sanitize (hb_sanitize_context_t *c) { michael@0: TRACE_SANITIZE (this); michael@0: return TRACE_RETURN (c->check_struct (this)); michael@0: } michael@0: michael@0: inline bool intersects (const hb_set_t *glyphs) const { michael@0: return glyphs->intersects (start, end); michael@0: } michael@0: michael@0: template michael@0: inline void add_coverage (set_t *glyphs) const { michael@0: glyphs->add_range (start, end); michael@0: } michael@0: michael@0: GlyphID start; /* First GlyphID in the range */ michael@0: GlyphID end; /* Last GlyphID in the range */ michael@0: USHORT value; /* Value */ michael@0: public: michael@0: DEFINE_SIZE_STATIC (6); michael@0: }; michael@0: DEFINE_NULL_DATA (RangeRecord, "\000\001"); michael@0: michael@0: michael@0: struct IndexArray : ArrayOf michael@0: { michael@0: inline unsigned int get_indexes (unsigned int start_offset, michael@0: unsigned int *_count /* IN/OUT */, michael@0: unsigned int *_indexes /* OUT */) const michael@0: { michael@0: if (_count) { michael@0: const USHORT *arr = this->sub_array (start_offset, _count); michael@0: unsigned int count = *_count; michael@0: for (unsigned int i = 0; i < count; i++) michael@0: _indexes[i] = arr[i]; michael@0: } michael@0: return this->len; michael@0: } michael@0: }; michael@0: michael@0: michael@0: struct Script; michael@0: struct LangSys; michael@0: struct Feature; michael@0: michael@0: michael@0: struct LangSys michael@0: { michael@0: inline unsigned int get_feature_count (void) const michael@0: { return featureIndex.len; } michael@0: inline hb_tag_t get_feature_index (unsigned int i) const michael@0: { return featureIndex[i]; } michael@0: inline unsigned int get_feature_indexes (unsigned int start_offset, michael@0: unsigned int *feature_count /* IN/OUT */, michael@0: unsigned int *feature_indexes /* OUT */) const michael@0: { return featureIndex.get_indexes (start_offset, feature_count, feature_indexes); } michael@0: michael@0: inline bool has_required_feature (void) const { return reqFeatureIndex != 0xffff; } michael@0: inline unsigned int get_required_feature_index (void) const michael@0: { michael@0: if (reqFeatureIndex == 0xffff) michael@0: return Index::NOT_FOUND_INDEX; michael@0: return reqFeatureIndex;; michael@0: } michael@0: michael@0: inline bool sanitize (hb_sanitize_context_t *c, michael@0: const Record::sanitize_closure_t * = NULL) { michael@0: TRACE_SANITIZE (this); michael@0: return TRACE_RETURN (c->check_struct (this) && featureIndex.sanitize (c)); michael@0: } michael@0: michael@0: Offset lookupOrder; /* = Null (reserved for an offset to a michael@0: * reordering table) */ michael@0: USHORT reqFeatureIndex;/* Index of a feature required for this michael@0: * language system--if no required features michael@0: * = 0xFFFF */ michael@0: IndexArray featureIndex; /* Array of indices into the FeatureList */ michael@0: public: michael@0: DEFINE_SIZE_ARRAY (6, featureIndex); michael@0: }; michael@0: DEFINE_NULL_DATA (LangSys, "\0\0\xFF\xFF"); michael@0: michael@0: michael@0: struct Script michael@0: { michael@0: inline unsigned int get_lang_sys_count (void) const michael@0: { return langSys.len; } michael@0: inline const Tag& get_lang_sys_tag (unsigned int i) const michael@0: { return langSys.get_tag (i); } michael@0: inline unsigned int get_lang_sys_tags (unsigned int start_offset, michael@0: unsigned int *lang_sys_count /* IN/OUT */, michael@0: hb_tag_t *lang_sys_tags /* OUT */) const michael@0: { return langSys.get_tags (start_offset, lang_sys_count, lang_sys_tags); } michael@0: inline const LangSys& get_lang_sys (unsigned int i) const michael@0: { michael@0: if (i == Index::NOT_FOUND_INDEX) return get_default_lang_sys (); michael@0: return this+langSys[i].offset; michael@0: } michael@0: inline bool find_lang_sys_index (hb_tag_t tag, unsigned int *index) const michael@0: { return langSys.find_index (tag, index); } michael@0: michael@0: inline bool has_default_lang_sys (void) const { return defaultLangSys != 0; } michael@0: inline const LangSys& get_default_lang_sys (void) const { return this+defaultLangSys; } michael@0: michael@0: inline bool sanitize (hb_sanitize_context_t *c, michael@0: const Record