/* * Copyright © 2007,2008,2009 Red Hat, Inc. * Copyright © 2010,2012 Google, Inc. * * This is part of HarfBuzz, a text shaping library. * * Permission is hereby granted, without written agreement and without * license or royalty fees, to use, copy, modify, and distribute this * software and its documentation for any purpose, provided that the * above copyright notice and the following two paragraphs appear in * all copies of this software. * * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH * DAMAGE. * * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. * * Red Hat Author(s): Behdad Esfahbod * Google Author(s): Behdad Esfahbod */ #ifndef HB_OT_LAYOUT_COMMON_HH #define HB_OT_LAYOUT_COMMON_HH #include "hb.hh" #include "hb-ot-layout.hh" #include "hb-open-type.hh" #include "hb-set.hh" #include "hb-bimap.hh" #ifndef HB_MAX_NESTING_LEVEL #define HB_MAX_NESTING_LEVEL 6 #endif #ifndef HB_MAX_CONTEXT_LENGTH #define HB_MAX_CONTEXT_LENGTH 64 #endif #ifndef HB_CLOSURE_MAX_STAGES /* * The maximum number of times a lookup can be applied during shaping. * Used to limit the number of iterations of the closure algorithm. * This must be larger than the number of times add_pause() is * called in a collect_features call of any shaper. */ #define HB_CLOSURE_MAX_STAGES 32 #endif #ifndef HB_MAX_SCRIPTS #define HB_MAX_SCRIPTS 500 #endif #ifndef HB_MAX_LANGSYS #define HB_MAX_LANGSYS 2000 #endif namespace OT { #define NOT_COVERED ((unsigned int) -1) template static inline void Coverage_serialize (hb_serialize_context_t *c, Iterator it); template static inline void ClassDef_serialize (hb_serialize_context_t *c, Iterator it); static void ClassDef_remap_and_serialize (hb_serialize_context_t *c, const hb_set_t &glyphset, const hb_map_t &gid_klass_map, hb_sorted_vector_t glyphs, hb_sorted_vector_t klasses, hb_map_t *klass_map /*INOUT*/); template struct subset_offset_array_t { subset_offset_array_t (hb_subset_context_t *subset_context, OutputArray& out, const void *src_base, const void *dest_base) : _subset_context(subset_context), _out (out), _src_base (src_base), _dest_base (dest_base) {} template bool operator () (T&& offset) { auto *o = _out.serialize_append (_subset_context->serializer); if (unlikely (!o)) return false; auto snap = _subset_context->serializer->snapshot (); bool ret = o->serialize_subset (_subset_context, offset, _src_base, _dest_base); if (!ret) { _out.pop (); _subset_context->serializer->revert (snap); } return ret; } private: hb_subset_context_t *_subset_context; OutputArray &_out; const void *_src_base; const void *_dest_base; }; /* * Helper to subset an array of offsets. Subsets the thing pointed to by each offset * and discards the offset in the array if the subset operation results in an empty * thing. */ struct { template subset_offset_array_t operator () (hb_subset_context_t *subset_context, OutputArray& out, const void *src_base, const void *dest_base) const { return subset_offset_array_t (subset_context, out, src_base, dest_base); } } HB_FUNCOBJ (subset_offset_array); /* * * OpenType Layout Common Table Formats * */ /* * Script, ScriptList, LangSys, Feature, FeatureList, Lookup, LookupList */ struct Record_sanitize_closure_t { hb_tag_t tag; const void *list_base; }; struct RecordList_subset_context_t { RecordList_subset_context_t() : script_count (0), langsys_count (0) {} bool visitScript () { return script_count++ < HB_MAX_SCRIPTS; } bool visitLangSys () { return langsys_count++ < HB_MAX_LANGSYS; } private: unsigned int script_count; unsigned int langsys_count; }; template struct Record { int cmp (hb_tag_t a) const { return tag.cmp (a); } bool sanitize (hb_sanitize_context_t *c, const void *base) const { TRACE_SANITIZE (this); const Record_sanitize_closure_t closure = {tag, base}; return_trace (c->check_struct (this) && offset.sanitize (c, base, &closure)); } Tag tag; /* 4-byte Tag identifier */ OffsetTo offset; /* Offset from beginning of object holding * the Record */ public: DEFINE_SIZE_STATIC (6); }; template struct RecordArrayOf : SortedArrayOf> { const OffsetTo& get_offset (unsigned int i) const { return (*this)[i].offset; } OffsetTo& get_offset (unsigned int i) { return (*this)[i].offset; } const Tag& get_tag (unsigned int i) const { return (*this)[i].tag; } unsigned int get_tags (unsigned int start_offset, unsigned int *record_count /* IN/OUT */, hb_tag_t *record_tags /* OUT */) const { if (record_count) { const Record *arr = this->sub_array (start_offset, record_count); unsigned int count = *record_count; for (unsigned int i = 0; i < count; i++) record_tags[i] = arr[i].tag; } return this->len; } bool find_index (hb_tag_t tag, unsigned int *index) const { return this->bfind (tag, index, HB_BFIND_NOT_FOUND_STORE, Index::NOT_FOUND_INDEX); } }; template struct RecordListOf : RecordArrayOf { const Type& operator [] (unsigned int i) const { return this+this->get_offset (i); } bool subset (hb_subset_context_t *c) const { TRACE_SUBSET (this); auto *out = c->serializer->start_embed (*this); if (unlikely (!c->serializer->extend_min (out))) return_trace (false); RecordList_subset_context_t record_list_context; unsigned int count = this->len; for (unsigned int i = 0; i < count; i++) { auto *record = out->serialize_append (c->serializer); if (unlikely (!record)) return false; auto snap = c->serializer->snapshot (); if (record->offset.serialize_subset (c, this->get_offset (i), this, out, &record_list_context)) { record->tag = this->get_tag(i); continue; } out->pop (); c->serializer->revert (snap); } return_trace (true); } bool sanitize (hb_sanitize_context_t *c) const { TRACE_SANITIZE (this); return_trace (RecordArrayOf::sanitize (c, this)); } }; struct RangeRecord { int cmp (hb_codepoint_t g) const { return g < start ? -1 : g <= end ? 0 : +1; } bool sanitize (hb_sanitize_context_t *c) const { TRACE_SANITIZE (this); return_trace (c->check_struct (this)); } bool intersects (const hb_set_t *glyphs) const { return glyphs->intersects (start, end); } template bool add_coverage (set_t *glyphs) const { return glyphs->add_range (start, end); } HBGlyphID start; /* First GlyphID in the range */ HBGlyphID end; /* Last GlyphID in the range */ HBUINT16 value; /* Value */ public: DEFINE_SIZE_STATIC (6); }; DECLARE_NULL_NAMESPACE_BYTES (OT, RangeRecord); struct IndexArray : ArrayOf { unsigned int get_indexes (unsigned int start_offset, unsigned int *_count /* IN/OUT */, unsigned int *_indexes /* OUT */) const { if (_count) { const HBUINT16 *arr = this->sub_array (start_offset, _count); unsigned int count = *_count; for (unsigned int i = 0; i < count; i++) _indexes[i] = arr[i]; } return this->len; } void add_indexes_to (hb_set_t* output /* OUT */) const { output->add_array (arrayZ, len); } }; struct Script; struct LangSys; struct Feature; struct LangSys { unsigned int get_feature_count () const { return featureIndex.len; } hb_tag_t get_feature_index (unsigned int i) const { return featureIndex[i]; } unsigned int get_feature_indexes (unsigned int start_offset, unsigned int *feature_count /* IN/OUT */, unsigned int *feature_indexes /* OUT */) const { return featureIndex.get_indexes (start_offset, feature_count, feature_indexes); } void add_feature_indexes_to (hb_set_t *feature_indexes) const { featureIndex.add_indexes_to (feature_indexes); } bool has_required_feature () const { return reqFeatureIndex != 0xFFFFu; } unsigned int get_required_feature_index () const { if (reqFeatureIndex == 0xFFFFu) return Index::NOT_FOUND_INDEX; return reqFeatureIndex; } LangSys* copy (hb_serialize_context_t *c) const { TRACE_SERIALIZE (this); return_trace (c->embed (*this)); } bool sanitize (hb_sanitize_context_t *c, const Record_sanitize_closure_t * = nullptr) const { TRACE_SANITIZE (this); return_trace (c->check_struct (this) && featureIndex.sanitize (c)); } Offset16 lookupOrderZ; /* = Null (reserved for an offset to a * reordering table) */ HBUINT16 reqFeatureIndex;/* Index of a feature required for this * language system--if no required features * = 0xFFFFu */ IndexArray featureIndex; /* Array of indices into the FeatureList */ public: DEFINE_SIZE_ARRAY_SIZED (6, featureIndex); }; DECLARE_NULL_NAMESPACE_BYTES (OT, LangSys); struct Script { unsigned int get_lang_sys_count () const { return langSys.len; } const Tag& get_lang_sys_tag (unsigned int i) const { return langSys.get_tag (i); } unsigned int get_lang_sys_tags (unsigned int start_offset, unsigned int *lang_sys_count /* IN/OUT */, hb_tag_t *lang_sys_tags /* OUT */) const { return langSys.get_tags (start_offset, lang_sys_count, lang_sys_tags); } const LangSys& get_lang_sys (unsigned int i) const { if (i == Index::NOT_FOUND_INDEX) return get_default_lang_sys (); return this+langSys[i].offset; } bool find_lang_sys_index (hb_tag_t tag, unsigned int *index) const { return langSys.find_index (tag, index); } bool has_default_lang_sys () const { return defaultLangSys != 0; } const LangSys& get_default_lang_sys () const { return this+defaultLangSys; } bool subset (hb_subset_context_t *c, RecordList_subset_context_t *record_list_context) const { TRACE_SUBSET (this); if (!record_list_context->visitScript ()) return_trace (false); auto *out = c->serializer->start_embed (*this); if (unlikely (!c->serializer->extend_min (out))) return_trace (false); out->defaultLangSys.serialize_copy (c->serializer, defaultLangSys, this, out); for (const auto &src: langSys) { if (!record_list_context->visitLangSys ()) { continue; } auto snap = c->serializer->snapshot (); auto *lang_sys = c->serializer->embed (src); if (likely(lang_sys) && lang_sys->offset.serialize_copy (c->serializer, src.offset, this, out)) { out->langSys.len++; continue; } c->serializer->revert (snap); } return_trace (true); } bool sanitize (hb_sanitize_context_t *c, const Record_sanitize_closure_t * = nullptr) const { TRACE_SANITIZE (this); return_trace (defaultLangSys.sanitize (c, this) && langSys.sanitize (c, this)); } protected: OffsetTo defaultLangSys; /* Offset to DefaultLangSys table--from * beginning of Script table--may be Null */ RecordArrayOf langSys; /* Array of LangSysRecords--listed * alphabetically by LangSysTag */ public: DEFINE_SIZE_ARRAY_SIZED (4, langSys); }; typedef RecordListOf