gfx/harfbuzz/src/hb-ot-map.cc

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /*
michael@0 2 * Copyright © 2009,2010 Red Hat, Inc.
michael@0 3 * Copyright © 2010,2011,2013 Google, Inc.
michael@0 4 *
michael@0 5 * This is part of HarfBuzz, a text shaping library.
michael@0 6 *
michael@0 7 * Permission is hereby granted, without written agreement and without
michael@0 8 * license or royalty fees, to use, copy, modify, and distribute this
michael@0 9 * software and its documentation for any purpose, provided that the
michael@0 10 * above copyright notice and the following two paragraphs appear in
michael@0 11 * all copies of this software.
michael@0 12 *
michael@0 13 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
michael@0 14 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
michael@0 15 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
michael@0 16 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
michael@0 17 * DAMAGE.
michael@0 18 *
michael@0 19 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
michael@0 20 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
michael@0 21 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
michael@0 22 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
michael@0 23 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
michael@0 24 *
michael@0 25 * Red Hat Author(s): Behdad Esfahbod
michael@0 26 * Google Author(s): Behdad Esfahbod
michael@0 27 */
michael@0 28
michael@0 29 #include "hb-ot-map-private.hh"
michael@0 30
michael@0 31 #include "hb-ot-layout-private.hh"
michael@0 32
michael@0 33
michael@0 34 void
michael@0 35 hb_ot_map_t::add_lookups (hb_face_t *face,
michael@0 36 unsigned int table_index,
michael@0 37 unsigned int feature_index,
michael@0 38 hb_mask_t mask,
michael@0 39 bool auto_zwj)
michael@0 40 {
michael@0 41 unsigned int lookup_indices[32];
michael@0 42 unsigned int offset, len;
michael@0 43 unsigned int table_lookup_count;
michael@0 44
michael@0 45 table_lookup_count = hb_ot_layout_table_get_lookup_count (face, table_tags[table_index]);
michael@0 46
michael@0 47 offset = 0;
michael@0 48 do {
michael@0 49 len = ARRAY_LENGTH (lookup_indices);
michael@0 50 hb_ot_layout_feature_get_lookups (face,
michael@0 51 table_tags[table_index],
michael@0 52 feature_index,
michael@0 53 offset, &len,
michael@0 54 lookup_indices);
michael@0 55
michael@0 56 for (unsigned int i = 0; i < len; i++)
michael@0 57 {
michael@0 58 if (lookup_indices[i] >= table_lookup_count)
michael@0 59 continue;
michael@0 60 hb_ot_map_t::lookup_map_t *lookup = lookups[table_index].push ();
michael@0 61 if (unlikely (!lookup))
michael@0 62 return;
michael@0 63 lookup->mask = mask;
michael@0 64 lookup->index = lookup_indices[i];
michael@0 65 lookup->auto_zwj = auto_zwj;
michael@0 66 }
michael@0 67
michael@0 68 offset += len;
michael@0 69 } while (len == ARRAY_LENGTH (lookup_indices));
michael@0 70 }
michael@0 71
michael@0 72 hb_ot_map_builder_t::hb_ot_map_builder_t (hb_face_t *face_,
michael@0 73 const hb_segment_properties_t *props_)
michael@0 74 {
michael@0 75 memset (this, 0, sizeof (*this));
michael@0 76
michael@0 77 face = face_;
michael@0 78 props = *props_;
michael@0 79
michael@0 80
michael@0 81 /* Fetch script/language indices for GSUB/GPOS. We need these later to skip
michael@0 82 * features not available in either table and not waste precious bits for them. */
michael@0 83
michael@0 84 hb_tag_t script_tags[3] = {HB_TAG_NONE, HB_TAG_NONE, HB_TAG_NONE};
michael@0 85 hb_tag_t language_tag;
michael@0 86
michael@0 87 hb_ot_tags_from_script (props.script, &script_tags[0], &script_tags[1]);
michael@0 88 language_tag = hb_ot_tag_from_language (props.language);
michael@0 89
michael@0 90 for (unsigned int table_index = 0; table_index < 2; table_index++) {
michael@0 91 hb_tag_t table_tag = table_tags[table_index];
michael@0 92 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 93 hb_ot_layout_script_find_language (face, table_tag, script_index[table_index], language_tag, &language_index[table_index]);
michael@0 94 }
michael@0 95 }
michael@0 96
michael@0 97 void hb_ot_map_builder_t::add_feature (hb_tag_t tag, unsigned int value,
michael@0 98 hb_ot_map_feature_flags_t flags)
michael@0 99 {
michael@0 100 feature_info_t *info = feature_infos.push();
michael@0 101 if (unlikely (!info)) return;
michael@0 102 info->tag = tag;
michael@0 103 info->seq = feature_infos.len;
michael@0 104 info->max_value = value;
michael@0 105 info->flags = flags;
michael@0 106 info->default_value = (flags & F_GLOBAL) ? value : 0;
michael@0 107 info->stage[0] = current_stage[0];
michael@0 108 info->stage[1] = current_stage[1];
michael@0 109 }
michael@0 110
michael@0 111
michael@0 112 void hb_ot_map_t::collect_lookups (unsigned int table_index, hb_set_t *lookups_out) const
michael@0 113 {
michael@0 114 for (unsigned int i = 0; i < lookups[table_index].len; i++)
michael@0 115 hb_set_add (lookups_out, lookups[table_index][i].index);
michael@0 116 }
michael@0 117
michael@0 118 void hb_ot_map_builder_t::add_pause (unsigned int table_index, hb_ot_map_t::pause_func_t pause_func)
michael@0 119 {
michael@0 120 stage_info_t *s = stages[table_index].push ();
michael@0 121 if (likely (s)) {
michael@0 122 s->index = current_stage[table_index];
michael@0 123 s->pause_func = pause_func;
michael@0 124 }
michael@0 125
michael@0 126 current_stage[table_index]++;
michael@0 127 }
michael@0 128
michael@0 129 void
michael@0 130 hb_ot_map_builder_t::compile (hb_ot_map_t &m)
michael@0 131 {
michael@0 132 m.global_mask = 1;
michael@0 133
michael@0 134 for (unsigned int table_index = 0; table_index < 2; table_index++) {
michael@0 135 m.chosen_script[table_index] = chosen_script[table_index];
michael@0 136 m.found_script[table_index] = found_script[table_index];
michael@0 137 }
michael@0 138
michael@0 139 if (!feature_infos.len)
michael@0 140 return;
michael@0 141
michael@0 142 /* Sort features and merge duplicates */
michael@0 143 {
michael@0 144 feature_infos.sort ();
michael@0 145 unsigned int j = 0;
michael@0 146 for (unsigned int i = 1; i < feature_infos.len; i++)
michael@0 147 if (feature_infos[i].tag != feature_infos[j].tag)
michael@0 148 feature_infos[++j] = feature_infos[i];
michael@0 149 else {
michael@0 150 if (feature_infos[i].flags & F_GLOBAL) {
michael@0 151 feature_infos[j].flags |= F_GLOBAL;
michael@0 152 feature_infos[j].max_value = feature_infos[i].max_value;
michael@0 153 feature_infos[j].default_value = feature_infos[i].default_value;
michael@0 154 } else {
michael@0 155 feature_infos[j].flags &= ~F_GLOBAL;
michael@0 156 feature_infos[j].max_value = MAX (feature_infos[j].max_value, feature_infos[i].max_value);
michael@0 157 /* Inherit default_value from j */
michael@0 158 }
michael@0 159 feature_infos[j].flags |= (feature_infos[i].flags & F_HAS_FALLBACK);
michael@0 160 feature_infos[j].stage[0] = MIN (feature_infos[j].stage[0], feature_infos[i].stage[0]);
michael@0 161 feature_infos[j].stage[1] = MIN (feature_infos[j].stage[1], feature_infos[i].stage[1]);
michael@0 162 }
michael@0 163 feature_infos.shrink (j + 1);
michael@0 164 }
michael@0 165
michael@0 166
michael@0 167 /* Allocate bits now */
michael@0 168 unsigned int next_bit = 1;
michael@0 169 for (unsigned int i = 0; i < feature_infos.len; i++) {
michael@0 170 const feature_info_t *info = &feature_infos[i];
michael@0 171
michael@0 172 unsigned int bits_needed;
michael@0 173
michael@0 174 if ((info->flags & F_GLOBAL) && info->max_value == 1)
michael@0 175 /* Uses the global bit */
michael@0 176 bits_needed = 0;
michael@0 177 else
michael@0 178 bits_needed = _hb_bit_storage (info->max_value);
michael@0 179
michael@0 180 if (!info->max_value || next_bit + bits_needed > 8 * sizeof (hb_mask_t))
michael@0 181 continue; /* Feature disabled, or not enough bits. */
michael@0 182
michael@0 183
michael@0 184 hb_bool_t found = false;
michael@0 185 unsigned int feature_index[2];
michael@0 186 for (unsigned int table_index = 0; table_index < 2; table_index++)
michael@0 187 found |= hb_ot_layout_language_find_feature (face,
michael@0 188 table_tags[table_index],
michael@0 189 script_index[table_index],
michael@0 190 language_index[table_index],
michael@0 191 info->tag,
michael@0 192 &feature_index[table_index]);
michael@0 193 if (!found && !(info->flags & F_HAS_FALLBACK))
michael@0 194 continue;
michael@0 195
michael@0 196
michael@0 197 hb_ot_map_t::feature_map_t *map = m.features.push ();
michael@0 198 if (unlikely (!map))
michael@0 199 break;
michael@0 200
michael@0 201 map->tag = info->tag;
michael@0 202 map->index[0] = feature_index[0];
michael@0 203 map->index[1] = feature_index[1];
michael@0 204 map->stage[0] = info->stage[0];
michael@0 205 map->stage[1] = info->stage[1];
michael@0 206 map->auto_zwj = !(info->flags & F_MANUAL_ZWJ);
michael@0 207 if ((info->flags & F_GLOBAL) && info->max_value == 1) {
michael@0 208 /* Uses the global bit */
michael@0 209 map->shift = 0;
michael@0 210 map->mask = 1;
michael@0 211 } else {
michael@0 212 map->shift = next_bit;
michael@0 213 map->mask = (1 << (next_bit + bits_needed)) - (1 << next_bit);
michael@0 214 next_bit += bits_needed;
michael@0 215 m.global_mask |= (info->default_value << map->shift) & map->mask;
michael@0 216 }
michael@0 217 map->_1_mask = (1 << map->shift) & map->mask;
michael@0 218 map->needs_fallback = !found;
michael@0 219
michael@0 220 }
michael@0 221 feature_infos.shrink (0); /* Done with these */
michael@0 222
michael@0 223
michael@0 224 add_gsub_pause (NULL);
michael@0 225 add_gpos_pause (NULL);
michael@0 226
michael@0 227 for (unsigned int table_index = 0; table_index < 2; table_index++) {
michael@0 228 hb_tag_t table_tag = table_tags[table_index];
michael@0 229
michael@0 230 /* Collect lookup indices for features */
michael@0 231
michael@0 232 unsigned int required_feature_index;
michael@0 233 if (hb_ot_layout_language_get_required_feature_index (face,
michael@0 234 table_tag,
michael@0 235 script_index[table_index],
michael@0 236 language_index[table_index],
michael@0 237 &required_feature_index))
michael@0 238 m.add_lookups (face, table_index, required_feature_index, 1, true);
michael@0 239
michael@0 240 unsigned int stage_index = 0;
michael@0 241 unsigned int last_num_lookups = 0;
michael@0 242 for (unsigned stage = 0; stage < current_stage[table_index]; stage++)
michael@0 243 {
michael@0 244 for (unsigned i = 0; i < m.features.len; i++)
michael@0 245 if (m.features[i].stage[table_index] == stage)
michael@0 246 m.add_lookups (face, table_index,
michael@0 247 m.features[i].index[table_index],
michael@0 248 m.features[i].mask,
michael@0 249 m.features[i].auto_zwj);
michael@0 250
michael@0 251 /* Sort lookups and merge duplicates */
michael@0 252 if (last_num_lookups < m.lookups[table_index].len)
michael@0 253 {
michael@0 254 m.lookups[table_index].sort (last_num_lookups, m.lookups[table_index].len);
michael@0 255
michael@0 256 unsigned int j = last_num_lookups;
michael@0 257 for (unsigned int i = j + 1; i < m.lookups[table_index].len; i++)
michael@0 258 if (m.lookups[table_index][i].index != m.lookups[table_index][j].index)
michael@0 259 m.lookups[table_index][++j] = m.lookups[table_index][i];
michael@0 260 else
michael@0 261 {
michael@0 262 m.lookups[table_index][j].mask |= m.lookups[table_index][i].mask;
michael@0 263 m.lookups[table_index][j].auto_zwj &= m.lookups[table_index][i].auto_zwj;
michael@0 264 }
michael@0 265 m.lookups[table_index].shrink (j + 1);
michael@0 266 }
michael@0 267
michael@0 268 last_num_lookups = m.lookups[table_index].len;
michael@0 269
michael@0 270 if (stage_index < stages[table_index].len && stages[table_index][stage_index].index == stage) {
michael@0 271 hb_ot_map_t::stage_map_t *stage_map = m.stages[table_index].push ();
michael@0 272 if (likely (stage_map)) {
michael@0 273 stage_map->last_lookup = last_num_lookups;
michael@0 274 stage_map->pause_func = stages[table_index][stage_index].pause_func;
michael@0 275 }
michael@0 276
michael@0 277 stage_index++;
michael@0 278 }
michael@0 279 }
michael@0 280 }
michael@0 281 }

mercurial