michael@0: /* michael@0: * Copyright © 2009,2010 Red Hat, Inc. michael@0: * Copyright © 2010,2011,2012,2013 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_MAP_PRIVATE_HH michael@0: #define HB_OT_MAP_PRIVATE_HH michael@0: michael@0: #include "hb-buffer-private.hh" michael@0: michael@0: michael@0: struct hb_ot_shape_plan_t; michael@0: michael@0: static const hb_tag_t table_tags[2] = {HB_OT_TAG_GSUB, HB_OT_TAG_GPOS}; michael@0: michael@0: struct hb_ot_map_t michael@0: { michael@0: friend struct hb_ot_map_builder_t; michael@0: michael@0: public: michael@0: michael@0: struct feature_map_t { michael@0: hb_tag_t tag; /* should be first for our bsearch to work */ michael@0: unsigned int index[2]; /* GSUB/GPOS */ michael@0: unsigned int stage[2]; /* GSUB/GPOS */ michael@0: unsigned int shift; michael@0: hb_mask_t mask; michael@0: hb_mask_t _1_mask; /* mask for value=1, for quick access */ michael@0: unsigned int needs_fallback : 1; michael@0: unsigned int auto_zwj : 1; michael@0: michael@0: static int cmp (const feature_map_t *a, const feature_map_t *b) michael@0: { return a->tag < b->tag ? -1 : a->tag > b->tag ? 1 : 0; } michael@0: }; michael@0: michael@0: struct lookup_map_t { michael@0: unsigned short index; michael@0: unsigned short auto_zwj : 1; michael@0: hb_mask_t mask; michael@0: michael@0: static int cmp (const lookup_map_t *a, const lookup_map_t *b) michael@0: { return a->index < b->index ? -1 : a->index > b->index ? 1 : 0; } michael@0: }; michael@0: michael@0: typedef void (*pause_func_t) (const struct hb_ot_shape_plan_t *plan, hb_font_t *font, hb_buffer_t *buffer); michael@0: michael@0: struct stage_map_t { michael@0: unsigned int last_lookup; /* Cumulative */ michael@0: pause_func_t pause_func; michael@0: }; michael@0: michael@0: michael@0: hb_ot_map_t (void) { memset (this, 0, sizeof (*this)); } michael@0: michael@0: inline hb_mask_t get_global_mask (void) const { return global_mask; } michael@0: michael@0: inline hb_mask_t get_mask (hb_tag_t feature_tag, unsigned int *shift = NULL) const { michael@0: const feature_map_t *map = features.bsearch (&feature_tag); michael@0: if (shift) *shift = map ? map->shift : 0; michael@0: return map ? map->mask : 0; michael@0: } michael@0: michael@0: inline bool needs_fallback (hb_tag_t feature_tag) const { michael@0: const feature_map_t *map = features.bsearch (&feature_tag); michael@0: return map ? map->needs_fallback : false; michael@0: } michael@0: michael@0: inline hb_mask_t get_1_mask (hb_tag_t feature_tag) const { michael@0: const feature_map_t *map = features.bsearch (&feature_tag); michael@0: return map ? map->_1_mask : 0; michael@0: } michael@0: michael@0: inline unsigned int get_feature_index (unsigned int table_index, hb_tag_t feature_tag) const { michael@0: const feature_map_t *map = features.bsearch (&feature_tag); michael@0: return map ? map->index[table_index] : HB_OT_LAYOUT_NO_FEATURE_INDEX; michael@0: } michael@0: michael@0: inline unsigned int get_feature_stage (unsigned int table_index, hb_tag_t feature_tag) const { michael@0: const feature_map_t *map = features.bsearch (&feature_tag); michael@0: return map ? map->stage[table_index] : (unsigned int) -1; michael@0: } michael@0: michael@0: inline void get_stage_lookups (unsigned int table_index, unsigned int stage, michael@0: const struct lookup_map_t **plookups, unsigned int *lookup_count) const { michael@0: if (unlikely (stage == (unsigned int) -1)) { michael@0: *plookups = NULL; michael@0: *lookup_count = 0; michael@0: return; michael@0: } michael@0: assert (stage <= stages[table_index].len); michael@0: unsigned int start = stage ? stages[table_index][stage - 1].last_lookup : 0; michael@0: unsigned int end = stage < stages[table_index].len ? stages[table_index][stage].last_lookup : lookups[table_index].len; michael@0: *plookups = &lookups[table_index][start]; michael@0: *lookup_count = end - start; michael@0: } michael@0: michael@0: HB_INTERNAL void collect_lookups (unsigned int table_index, hb_set_t *lookups) const; michael@0: template michael@0: HB_INTERNAL inline void apply (const Proxy &proxy, michael@0: const struct hb_ot_shape_plan_t *plan, hb_font_t *font, hb_buffer_t *buffer) const; michael@0: HB_INTERNAL void substitute (const struct hb_ot_shape_plan_t *plan, hb_font_t *font, hb_buffer_t *buffer) const; michael@0: HB_INTERNAL void position (const struct hb_ot_shape_plan_t *plan, hb_font_t *font, hb_buffer_t *buffer) const; michael@0: michael@0: inline void finish (void) { michael@0: features.finish (); michael@0: for (unsigned int table_index = 0; table_index < 2; table_index++) michael@0: { michael@0: lookups[table_index].finish (); michael@0: stages[table_index].finish (); michael@0: } michael@0: } michael@0: michael@0: public: michael@0: hb_tag_t chosen_script[2]; michael@0: bool found_script[2]; michael@0: michael@0: private: michael@0: michael@0: HB_INTERNAL void add_lookups (hb_face_t *face, michael@0: unsigned int table_index, michael@0: unsigned int feature_index, michael@0: hb_mask_t mask, michael@0: bool auto_zwj); michael@0: michael@0: hb_mask_t global_mask; michael@0: michael@0: hb_prealloced_array_t features; michael@0: hb_prealloced_array_t lookups[2]; /* GSUB/GPOS */ michael@0: hb_prealloced_array_t stages[2]; /* GSUB/GPOS */ michael@0: }; michael@0: michael@0: enum hb_ot_map_feature_flags_t { michael@0: F_NONE = 0x0000, michael@0: F_GLOBAL = 0x0001, michael@0: F_HAS_FALLBACK = 0x0002, michael@0: F_MANUAL_ZWJ = 0x0004 michael@0: }; michael@0: /* Macro version for where const is desired. */ michael@0: #define F_COMBINE(l,r) (hb_ot_map_feature_flags_t ((unsigned int) (l) | (unsigned int) (r))) michael@0: inline hb_ot_map_feature_flags_t michael@0: operator | (hb_ot_map_feature_flags_t l, hb_ot_map_feature_flags_t r) michael@0: { return hb_ot_map_feature_flags_t ((unsigned int) l | (unsigned int) r); } michael@0: inline hb_ot_map_feature_flags_t michael@0: operator & (hb_ot_map_feature_flags_t l, hb_ot_map_feature_flags_t r) michael@0: { return hb_ot_map_feature_flags_t ((unsigned int) l & (unsigned int) r); } michael@0: inline hb_ot_map_feature_flags_t michael@0: operator ~ (hb_ot_map_feature_flags_t r) michael@0: { return hb_ot_map_feature_flags_t (~(unsigned int) r); } michael@0: inline hb_ot_map_feature_flags_t& michael@0: operator |= (hb_ot_map_feature_flags_t &l, hb_ot_map_feature_flags_t r) michael@0: { l = l | r; return l; } michael@0: inline hb_ot_map_feature_flags_t& michael@0: operator &= (hb_ot_map_feature_flags_t& l, hb_ot_map_feature_flags_t r) michael@0: { l = l & r; return l; } michael@0: michael@0: michael@0: struct hb_ot_map_builder_t michael@0: { michael@0: public: michael@0: michael@0: HB_INTERNAL hb_ot_map_builder_t (hb_face_t *face_, michael@0: const hb_segment_properties_t *props_); michael@0: michael@0: HB_INTERNAL void add_feature (hb_tag_t tag, unsigned int value, michael@0: hb_ot_map_feature_flags_t flags); michael@0: michael@0: inline void add_global_bool_feature (hb_tag_t tag) michael@0: { add_feature (tag, 1, F_GLOBAL); } michael@0: michael@0: inline void add_gsub_pause (hb_ot_map_t::pause_func_t pause_func) michael@0: { add_pause (0, pause_func); } michael@0: inline void add_gpos_pause (hb_ot_map_t::pause_func_t pause_func) michael@0: { add_pause (1, pause_func); } michael@0: michael@0: HB_INTERNAL void compile (struct hb_ot_map_t &m); michael@0: michael@0: inline void finish (void) { michael@0: feature_infos.finish (); michael@0: for (unsigned int table_index = 0; table_index < 2; table_index++) michael@0: { michael@0: stages[table_index].finish (); michael@0: } michael@0: } michael@0: michael@0: private: michael@0: michael@0: struct feature_info_t { michael@0: hb_tag_t tag; michael@0: unsigned int seq; /* sequence#, used for stable sorting only */ michael@0: unsigned int max_value; michael@0: hb_ot_map_feature_flags_t flags; michael@0: unsigned int default_value; /* for non-global features, what should the unset glyphs take */ michael@0: unsigned int stage[2]; /* GSUB/GPOS */ michael@0: michael@0: static int cmp (const feature_info_t *a, const feature_info_t *b) michael@0: { return (a->tag != b->tag) ? (a->tag < b->tag ? -1 : 1) : (a->seq < b->seq ? -1 : 1); } michael@0: }; michael@0: michael@0: struct stage_info_t { michael@0: unsigned int index; michael@0: hb_ot_map_t::pause_func_t pause_func; michael@0: }; michael@0: michael@0: HB_INTERNAL void add_pause (unsigned int table_index, hb_ot_map_t::pause_func_t pause_func); michael@0: michael@0: public: michael@0: michael@0: hb_face_t *face; michael@0: hb_segment_properties_t props; michael@0: michael@0: hb_tag_t chosen_script[2]; michael@0: bool found_script[2]; michael@0: unsigned int script_index[2], language_index[2]; michael@0: michael@0: private: michael@0: michael@0: unsigned int current_stage[2]; /* GSUB/GPOS */ michael@0: hb_prealloced_array_t feature_infos; michael@0: hb_prealloced_array_t stages[2]; /* GSUB/GPOS */ michael@0: }; michael@0: michael@0: michael@0: michael@0: #endif /* HB_OT_MAP_PRIVATE_HH */