1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/harfbuzz/src/hb-ot-map.cc Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,281 @@ 1.4 +/* 1.5 + * Copyright © 2009,2010 Red Hat, Inc. 1.6 + * Copyright © 2010,2011,2013 Google, Inc. 1.7 + * 1.8 + * This is part of HarfBuzz, a text shaping library. 1.9 + * 1.10 + * Permission is hereby granted, without written agreement and without 1.11 + * license or royalty fees, to use, copy, modify, and distribute this 1.12 + * software and its documentation for any purpose, provided that the 1.13 + * above copyright notice and the following two paragraphs appear in 1.14 + * all copies of this software. 1.15 + * 1.16 + * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR 1.17 + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 1.18 + * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN 1.19 + * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH 1.20 + * DAMAGE. 1.21 + * 1.22 + * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, 1.23 + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 1.24 + * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS 1.25 + * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO 1.26 + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. 1.27 + * 1.28 + * Red Hat Author(s): Behdad Esfahbod 1.29 + * Google Author(s): Behdad Esfahbod 1.30 + */ 1.31 + 1.32 +#include "hb-ot-map-private.hh" 1.33 + 1.34 +#include "hb-ot-layout-private.hh" 1.35 + 1.36 + 1.37 +void 1.38 +hb_ot_map_t::add_lookups (hb_face_t *face, 1.39 + unsigned int table_index, 1.40 + unsigned int feature_index, 1.41 + hb_mask_t mask, 1.42 + bool auto_zwj) 1.43 +{ 1.44 + unsigned int lookup_indices[32]; 1.45 + unsigned int offset, len; 1.46 + unsigned int table_lookup_count; 1.47 + 1.48 + table_lookup_count = hb_ot_layout_table_get_lookup_count (face, table_tags[table_index]); 1.49 + 1.50 + offset = 0; 1.51 + do { 1.52 + len = ARRAY_LENGTH (lookup_indices); 1.53 + hb_ot_layout_feature_get_lookups (face, 1.54 + table_tags[table_index], 1.55 + feature_index, 1.56 + offset, &len, 1.57 + lookup_indices); 1.58 + 1.59 + for (unsigned int i = 0; i < len; i++) 1.60 + { 1.61 + if (lookup_indices[i] >= table_lookup_count) 1.62 + continue; 1.63 + hb_ot_map_t::lookup_map_t *lookup = lookups[table_index].push (); 1.64 + if (unlikely (!lookup)) 1.65 + return; 1.66 + lookup->mask = mask; 1.67 + lookup->index = lookup_indices[i]; 1.68 + lookup->auto_zwj = auto_zwj; 1.69 + } 1.70 + 1.71 + offset += len; 1.72 + } while (len == ARRAY_LENGTH (lookup_indices)); 1.73 +} 1.74 + 1.75 +hb_ot_map_builder_t::hb_ot_map_builder_t (hb_face_t *face_, 1.76 + const hb_segment_properties_t *props_) 1.77 +{ 1.78 + memset (this, 0, sizeof (*this)); 1.79 + 1.80 + face = face_; 1.81 + props = *props_; 1.82 + 1.83 + 1.84 + /* Fetch script/language indices for GSUB/GPOS. We need these later to skip 1.85 + * features not available in either table and not waste precious bits for them. */ 1.86 + 1.87 + hb_tag_t script_tags[3] = {HB_TAG_NONE, HB_TAG_NONE, HB_TAG_NONE}; 1.88 + hb_tag_t language_tag; 1.89 + 1.90 + hb_ot_tags_from_script (props.script, &script_tags[0], &script_tags[1]); 1.91 + language_tag = hb_ot_tag_from_language (props.language); 1.92 + 1.93 + for (unsigned int table_index = 0; table_index < 2; table_index++) { 1.94 + hb_tag_t table_tag = table_tags[table_index]; 1.95 + found_script[table_index] = hb_ot_layout_table_choose_script (face, table_tag, script_tags, &script_index[table_index], &chosen_script[table_index]); 1.96 + hb_ot_layout_script_find_language (face, table_tag, script_index[table_index], language_tag, &language_index[table_index]); 1.97 + } 1.98 +} 1.99 + 1.100 +void hb_ot_map_builder_t::add_feature (hb_tag_t tag, unsigned int value, 1.101 + hb_ot_map_feature_flags_t flags) 1.102 +{ 1.103 + feature_info_t *info = feature_infos.push(); 1.104 + if (unlikely (!info)) return; 1.105 + info->tag = tag; 1.106 + info->seq = feature_infos.len; 1.107 + info->max_value = value; 1.108 + info->flags = flags; 1.109 + info->default_value = (flags & F_GLOBAL) ? value : 0; 1.110 + info->stage[0] = current_stage[0]; 1.111 + info->stage[1] = current_stage[1]; 1.112 +} 1.113 + 1.114 + 1.115 +void hb_ot_map_t::collect_lookups (unsigned int table_index, hb_set_t *lookups_out) const 1.116 +{ 1.117 + for (unsigned int i = 0; i < lookups[table_index].len; i++) 1.118 + hb_set_add (lookups_out, lookups[table_index][i].index); 1.119 +} 1.120 + 1.121 +void hb_ot_map_builder_t::add_pause (unsigned int table_index, hb_ot_map_t::pause_func_t pause_func) 1.122 +{ 1.123 + stage_info_t *s = stages[table_index].push (); 1.124 + if (likely (s)) { 1.125 + s->index = current_stage[table_index]; 1.126 + s->pause_func = pause_func; 1.127 + } 1.128 + 1.129 + current_stage[table_index]++; 1.130 +} 1.131 + 1.132 +void 1.133 +hb_ot_map_builder_t::compile (hb_ot_map_t &m) 1.134 +{ 1.135 + m.global_mask = 1; 1.136 + 1.137 + for (unsigned int table_index = 0; table_index < 2; table_index++) { 1.138 + m.chosen_script[table_index] = chosen_script[table_index]; 1.139 + m.found_script[table_index] = found_script[table_index]; 1.140 + } 1.141 + 1.142 + if (!feature_infos.len) 1.143 + return; 1.144 + 1.145 + /* Sort features and merge duplicates */ 1.146 + { 1.147 + feature_infos.sort (); 1.148 + unsigned int j = 0; 1.149 + for (unsigned int i = 1; i < feature_infos.len; i++) 1.150 + if (feature_infos[i].tag != feature_infos[j].tag) 1.151 + feature_infos[++j] = feature_infos[i]; 1.152 + else { 1.153 + if (feature_infos[i].flags & F_GLOBAL) { 1.154 + feature_infos[j].flags |= F_GLOBAL; 1.155 + feature_infos[j].max_value = feature_infos[i].max_value; 1.156 + feature_infos[j].default_value = feature_infos[i].default_value; 1.157 + } else { 1.158 + feature_infos[j].flags &= ~F_GLOBAL; 1.159 + feature_infos[j].max_value = MAX (feature_infos[j].max_value, feature_infos[i].max_value); 1.160 + /* Inherit default_value from j */ 1.161 + } 1.162 + feature_infos[j].flags |= (feature_infos[i].flags & F_HAS_FALLBACK); 1.163 + feature_infos[j].stage[0] = MIN (feature_infos[j].stage[0], feature_infos[i].stage[0]); 1.164 + feature_infos[j].stage[1] = MIN (feature_infos[j].stage[1], feature_infos[i].stage[1]); 1.165 + } 1.166 + feature_infos.shrink (j + 1); 1.167 + } 1.168 + 1.169 + 1.170 + /* Allocate bits now */ 1.171 + unsigned int next_bit = 1; 1.172 + for (unsigned int i = 0; i < feature_infos.len; i++) { 1.173 + const feature_info_t *info = &feature_infos[i]; 1.174 + 1.175 + unsigned int bits_needed; 1.176 + 1.177 + if ((info->flags & F_GLOBAL) && info->max_value == 1) 1.178 + /* Uses the global bit */ 1.179 + bits_needed = 0; 1.180 + else 1.181 + bits_needed = _hb_bit_storage (info->max_value); 1.182 + 1.183 + if (!info->max_value || next_bit + bits_needed > 8 * sizeof (hb_mask_t)) 1.184 + continue; /* Feature disabled, or not enough bits. */ 1.185 + 1.186 + 1.187 + hb_bool_t found = false; 1.188 + unsigned int feature_index[2]; 1.189 + for (unsigned int table_index = 0; table_index < 2; table_index++) 1.190 + found |= hb_ot_layout_language_find_feature (face, 1.191 + table_tags[table_index], 1.192 + script_index[table_index], 1.193 + language_index[table_index], 1.194 + info->tag, 1.195 + &feature_index[table_index]); 1.196 + if (!found && !(info->flags & F_HAS_FALLBACK)) 1.197 + continue; 1.198 + 1.199 + 1.200 + hb_ot_map_t::feature_map_t *map = m.features.push (); 1.201 + if (unlikely (!map)) 1.202 + break; 1.203 + 1.204 + map->tag = info->tag; 1.205 + map->index[0] = feature_index[0]; 1.206 + map->index[1] = feature_index[1]; 1.207 + map->stage[0] = info->stage[0]; 1.208 + map->stage[1] = info->stage[1]; 1.209 + map->auto_zwj = !(info->flags & F_MANUAL_ZWJ); 1.210 + if ((info->flags & F_GLOBAL) && info->max_value == 1) { 1.211 + /* Uses the global bit */ 1.212 + map->shift = 0; 1.213 + map->mask = 1; 1.214 + } else { 1.215 + map->shift = next_bit; 1.216 + map->mask = (1 << (next_bit + bits_needed)) - (1 << next_bit); 1.217 + next_bit += bits_needed; 1.218 + m.global_mask |= (info->default_value << map->shift) & map->mask; 1.219 + } 1.220 + map->_1_mask = (1 << map->shift) & map->mask; 1.221 + map->needs_fallback = !found; 1.222 + 1.223 + } 1.224 + feature_infos.shrink (0); /* Done with these */ 1.225 + 1.226 + 1.227 + add_gsub_pause (NULL); 1.228 + add_gpos_pause (NULL); 1.229 + 1.230 + for (unsigned int table_index = 0; table_index < 2; table_index++) { 1.231 + hb_tag_t table_tag = table_tags[table_index]; 1.232 + 1.233 + /* Collect lookup indices for features */ 1.234 + 1.235 + unsigned int required_feature_index; 1.236 + if (hb_ot_layout_language_get_required_feature_index (face, 1.237 + table_tag, 1.238 + script_index[table_index], 1.239 + language_index[table_index], 1.240 + &required_feature_index)) 1.241 + m.add_lookups (face, table_index, required_feature_index, 1, true); 1.242 + 1.243 + unsigned int stage_index = 0; 1.244 + unsigned int last_num_lookups = 0; 1.245 + for (unsigned stage = 0; stage < current_stage[table_index]; stage++) 1.246 + { 1.247 + for (unsigned i = 0; i < m.features.len; i++) 1.248 + if (m.features[i].stage[table_index] == stage) 1.249 + m.add_lookups (face, table_index, 1.250 + m.features[i].index[table_index], 1.251 + m.features[i].mask, 1.252 + m.features[i].auto_zwj); 1.253 + 1.254 + /* Sort lookups and merge duplicates */ 1.255 + if (last_num_lookups < m.lookups[table_index].len) 1.256 + { 1.257 + m.lookups[table_index].sort (last_num_lookups, m.lookups[table_index].len); 1.258 + 1.259 + unsigned int j = last_num_lookups; 1.260 + for (unsigned int i = j + 1; i < m.lookups[table_index].len; i++) 1.261 + if (m.lookups[table_index][i].index != m.lookups[table_index][j].index) 1.262 + m.lookups[table_index][++j] = m.lookups[table_index][i]; 1.263 + else 1.264 + { 1.265 + m.lookups[table_index][j].mask |= m.lookups[table_index][i].mask; 1.266 + m.lookups[table_index][j].auto_zwj &= m.lookups[table_index][i].auto_zwj; 1.267 + } 1.268 + m.lookups[table_index].shrink (j + 1); 1.269 + } 1.270 + 1.271 + last_num_lookups = m.lookups[table_index].len; 1.272 + 1.273 + if (stage_index < stages[table_index].len && stages[table_index][stage_index].index == stage) { 1.274 + hb_ot_map_t::stage_map_t *stage_map = m.stages[table_index].push (); 1.275 + if (likely (stage_map)) { 1.276 + stage_map->last_lookup = last_num_lookups; 1.277 + stage_map->pause_func = stages[table_index][stage_index].pause_func; 1.278 + } 1.279 + 1.280 + stage_index++; 1.281 + } 1.282 + } 1.283 + } 1.284 +}