michael@0: /* michael@0: * Copyright © 2009,2010 Red Hat, Inc. michael@0: * Copyright © 2010,2011,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: #include "hb-ot-map-private.hh" michael@0: michael@0: #include "hb-ot-layout-private.hh" michael@0: michael@0: michael@0: void michael@0: hb_ot_map_t::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: unsigned int lookup_indices[32]; michael@0: unsigned int offset, len; michael@0: unsigned int table_lookup_count; michael@0: michael@0: table_lookup_count = hb_ot_layout_table_get_lookup_count (face, table_tags[table_index]); michael@0: michael@0: offset = 0; michael@0: do { michael@0: len = ARRAY_LENGTH (lookup_indices); michael@0: hb_ot_layout_feature_get_lookups (face, michael@0: table_tags[table_index], michael@0: feature_index, michael@0: offset, &len, michael@0: lookup_indices); michael@0: michael@0: for (unsigned int i = 0; i < len; i++) michael@0: { michael@0: if (lookup_indices[i] >= table_lookup_count) michael@0: continue; michael@0: hb_ot_map_t::lookup_map_t *lookup = lookups[table_index].push (); michael@0: if (unlikely (!lookup)) michael@0: return; michael@0: lookup->mask = mask; michael@0: lookup->index = lookup_indices[i]; michael@0: lookup->auto_zwj = auto_zwj; michael@0: } michael@0: michael@0: offset += len; michael@0: } while (len == ARRAY_LENGTH (lookup_indices)); michael@0: } michael@0: michael@0: hb_ot_map_builder_t::hb_ot_map_builder_t (hb_face_t *face_, michael@0: const hb_segment_properties_t *props_) michael@0: { michael@0: memset (this, 0, sizeof (*this)); michael@0: michael@0: face = face_; michael@0: props = *props_; michael@0: michael@0: michael@0: /* Fetch script/language indices for GSUB/GPOS. We need these later to skip michael@0: * features not available in either table and not waste precious bits for them. */ michael@0: michael@0: hb_tag_t script_tags[3] = {HB_TAG_NONE, HB_TAG_NONE, HB_TAG_NONE}; michael@0: hb_tag_t language_tag; michael@0: michael@0: hb_ot_tags_from_script (props.script, &script_tags[0], &script_tags[1]); michael@0: language_tag = hb_ot_tag_from_language (props.language); michael@0: michael@0: for (unsigned int table_index = 0; table_index < 2; table_index++) { michael@0: hb_tag_t table_tag = table_tags[table_index]; michael@0: found_script[table_index] = hb_ot_layout_table_choose_script (face, table_tag, script_tags, &script_index[table_index], &chosen_script[table_index]); michael@0: hb_ot_layout_script_find_language (face, table_tag, script_index[table_index], language_tag, &language_index[table_index]); michael@0: } michael@0: } michael@0: michael@0: void hb_ot_map_builder_t::add_feature (hb_tag_t tag, unsigned int value, michael@0: hb_ot_map_feature_flags_t flags) michael@0: { michael@0: feature_info_t *info = feature_infos.push(); michael@0: if (unlikely (!info)) return; michael@0: info->tag = tag; michael@0: info->seq = feature_infos.len; michael@0: info->max_value = value; michael@0: info->flags = flags; michael@0: info->default_value = (flags & F_GLOBAL) ? value : 0; michael@0: info->stage[0] = current_stage[0]; michael@0: info->stage[1] = current_stage[1]; michael@0: } michael@0: michael@0: michael@0: void hb_ot_map_t::collect_lookups (unsigned int table_index, hb_set_t *lookups_out) const michael@0: { michael@0: for (unsigned int i = 0; i < lookups[table_index].len; i++) michael@0: hb_set_add (lookups_out, lookups[table_index][i].index); michael@0: } michael@0: michael@0: void hb_ot_map_builder_t::add_pause (unsigned int table_index, hb_ot_map_t::pause_func_t pause_func) michael@0: { michael@0: stage_info_t *s = stages[table_index].push (); michael@0: if (likely (s)) { michael@0: s->index = current_stage[table_index]; michael@0: s->pause_func = pause_func; michael@0: } michael@0: michael@0: current_stage[table_index]++; michael@0: } michael@0: michael@0: void michael@0: hb_ot_map_builder_t::compile (hb_ot_map_t &m) michael@0: { michael@0: m.global_mask = 1; michael@0: michael@0: for (unsigned int table_index = 0; table_index < 2; table_index++) { michael@0: m.chosen_script[table_index] = chosen_script[table_index]; michael@0: m.found_script[table_index] = found_script[table_index]; michael@0: } michael@0: michael@0: if (!feature_infos.len) michael@0: return; michael@0: michael@0: /* Sort features and merge duplicates */ michael@0: { michael@0: feature_infos.sort (); michael@0: unsigned int j = 0; michael@0: for (unsigned int i = 1; i < feature_infos.len; i++) michael@0: if (feature_infos[i].tag != feature_infos[j].tag) michael@0: feature_infos[++j] = feature_infos[i]; michael@0: else { michael@0: if (feature_infos[i].flags & F_GLOBAL) { michael@0: feature_infos[j].flags |= F_GLOBAL; michael@0: feature_infos[j].max_value = feature_infos[i].max_value; michael@0: feature_infos[j].default_value = feature_infos[i].default_value; michael@0: } else { michael@0: feature_infos[j].flags &= ~F_GLOBAL; michael@0: feature_infos[j].max_value = MAX (feature_infos[j].max_value, feature_infos[i].max_value); michael@0: /* Inherit default_value from j */ michael@0: } michael@0: feature_infos[j].flags |= (feature_infos[i].flags & F_HAS_FALLBACK); michael@0: feature_infos[j].stage[0] = MIN (feature_infos[j].stage[0], feature_infos[i].stage[0]); michael@0: feature_infos[j].stage[1] = MIN (feature_infos[j].stage[1], feature_infos[i].stage[1]); michael@0: } michael@0: feature_infos.shrink (j + 1); michael@0: } michael@0: michael@0: michael@0: /* Allocate bits now */ michael@0: unsigned int next_bit = 1; michael@0: for (unsigned int i = 0; i < feature_infos.len; i++) { michael@0: const feature_info_t *info = &feature_infos[i]; michael@0: michael@0: unsigned int bits_needed; michael@0: michael@0: if ((info->flags & F_GLOBAL) && info->max_value == 1) michael@0: /* Uses the global bit */ michael@0: bits_needed = 0; michael@0: else michael@0: bits_needed = _hb_bit_storage (info->max_value); michael@0: michael@0: if (!info->max_value || next_bit + bits_needed > 8 * sizeof (hb_mask_t)) michael@0: continue; /* Feature disabled, or not enough bits. */ michael@0: michael@0: michael@0: hb_bool_t found = false; michael@0: unsigned int feature_index[2]; michael@0: for (unsigned int table_index = 0; table_index < 2; table_index++) michael@0: found |= hb_ot_layout_language_find_feature (face, michael@0: table_tags[table_index], michael@0: script_index[table_index], michael@0: language_index[table_index], michael@0: info->tag, michael@0: &feature_index[table_index]); michael@0: if (!found && !(info->flags & F_HAS_FALLBACK)) michael@0: continue; michael@0: michael@0: michael@0: hb_ot_map_t::feature_map_t *map = m.features.push (); michael@0: if (unlikely (!map)) michael@0: break; michael@0: michael@0: map->tag = info->tag; michael@0: map->index[0] = feature_index[0]; michael@0: map->index[1] = feature_index[1]; michael@0: map->stage[0] = info->stage[0]; michael@0: map->stage[1] = info->stage[1]; michael@0: map->auto_zwj = !(info->flags & F_MANUAL_ZWJ); michael@0: if ((info->flags & F_GLOBAL) && info->max_value == 1) { michael@0: /* Uses the global bit */ michael@0: map->shift = 0; michael@0: map->mask = 1; michael@0: } else { michael@0: map->shift = next_bit; michael@0: map->mask = (1 << (next_bit + bits_needed)) - (1 << next_bit); michael@0: next_bit += bits_needed; michael@0: m.global_mask |= (info->default_value << map->shift) & map->mask; michael@0: } michael@0: map->_1_mask = (1 << map->shift) & map->mask; michael@0: map->needs_fallback = !found; michael@0: michael@0: } michael@0: feature_infos.shrink (0); /* Done with these */ michael@0: michael@0: michael@0: add_gsub_pause (NULL); michael@0: add_gpos_pause (NULL); michael@0: michael@0: for (unsigned int table_index = 0; table_index < 2; table_index++) { michael@0: hb_tag_t table_tag = table_tags[table_index]; michael@0: michael@0: /* Collect lookup indices for features */ michael@0: michael@0: unsigned int required_feature_index; michael@0: if (hb_ot_layout_language_get_required_feature_index (face, michael@0: table_tag, michael@0: script_index[table_index], michael@0: language_index[table_index], michael@0: &required_feature_index)) michael@0: m.add_lookups (face, table_index, required_feature_index, 1, true); michael@0: michael@0: unsigned int stage_index = 0; michael@0: unsigned int last_num_lookups = 0; michael@0: for (unsigned stage = 0; stage < current_stage[table_index]; stage++) michael@0: { michael@0: for (unsigned i = 0; i < m.features.len; i++) michael@0: if (m.features[i].stage[table_index] == stage) michael@0: m.add_lookups (face, table_index, michael@0: m.features[i].index[table_index], michael@0: m.features[i].mask, michael@0: m.features[i].auto_zwj); michael@0: michael@0: /* Sort lookups and merge duplicates */ michael@0: if (last_num_lookups < m.lookups[table_index].len) michael@0: { michael@0: m.lookups[table_index].sort (last_num_lookups, m.lookups[table_index].len); michael@0: michael@0: unsigned int j = last_num_lookups; michael@0: for (unsigned int i = j + 1; i < m.lookups[table_index].len; i++) michael@0: if (m.lookups[table_index][i].index != m.lookups[table_index][j].index) michael@0: m.lookups[table_index][++j] = m.lookups[table_index][i]; michael@0: else michael@0: { michael@0: m.lookups[table_index][j].mask |= m.lookups[table_index][i].mask; michael@0: m.lookups[table_index][j].auto_zwj &= m.lookups[table_index][i].auto_zwj; michael@0: } michael@0: m.lookups[table_index].shrink (j + 1); michael@0: } michael@0: michael@0: last_num_lookups = m.lookups[table_index].len; michael@0: michael@0: if (stage_index < stages[table_index].len && stages[table_index][stage_index].index == stage) { michael@0: hb_ot_map_t::stage_map_t *stage_map = m.stages[table_index].push (); michael@0: if (likely (stage_map)) { michael@0: stage_map->last_lookup = last_num_lookups; michael@0: stage_map->pause_func = stages[table_index][stage_index].pause_func; michael@0: } michael@0: michael@0: stage_index++; michael@0: } michael@0: } michael@0: } michael@0: }