2 * Copyright © 2009,2010 Red Hat, Inc.
3 * Copyright © 2010,2011,2013 Google, Inc.
5 * This is part of HarfBuzz, a text shaping library.
7 * Permission is hereby granted, without written agreement and without
8 * license or royalty fees, to use, copy, modify, and distribute this
9 * software and its documentation for any purpose, provided that the
10 * above copyright notice and the following two paragraphs appear in
11 * all copies of this software.
13 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
14 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
15 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
16 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
19 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
20 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
22 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
23 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
25 * Red Hat Author(s): Behdad Esfahbod
26 * Google Author(s): Behdad Esfahbod
31 #ifndef HB_NO_OT_SHAPE
33 #include "hb-ot-map.hh"
34 #include "hb-ot-shape.hh"
35 #include "hb-ot-layout.hh"
38 void hb_ot_map_t::collect_lookups (unsigned int table_index, hb_set_t *lookups_out) const
40 for (unsigned int i = 0; i < lookups[table_index].length; i++)
41 lookups_out->add (lookups[table_index][i].index);
45 hb_ot_map_builder_t::hb_ot_map_builder_t (hb_face_t *face_,
46 const hb_segment_properties_t *props_)
48 memset (this, 0, sizeof (*this));
50 feature_infos.init ();
51 for (unsigned int table_index = 0; table_index < 2; table_index++)
52 stages[table_index].init ();
57 /* Fetch script/language indices for GSUB/GPOS. We need these later to skip
58 * features not available in either table and not waste precious bits for them. */
60 unsigned int script_count = HB_OT_MAX_TAGS_PER_SCRIPT;
61 unsigned int language_count = HB_OT_MAX_TAGS_PER_LANGUAGE;
62 hb_tag_t script_tags[HB_OT_MAX_TAGS_PER_SCRIPT];
63 hb_tag_t language_tags[HB_OT_MAX_TAGS_PER_LANGUAGE];
65 hb_ot_tags_from_script_and_language (props.script,
72 for (unsigned int table_index = 0; table_index < 2; table_index++)
74 hb_tag_t table_tag = table_tags[table_index];
75 found_script[table_index] = (bool) hb_ot_layout_table_select_script (face,
79 &script_index[table_index],
80 &chosen_script[table_index]);
81 hb_ot_layout_script_select_language (face,
83 script_index[table_index],
86 &language_index[table_index]);
90 hb_ot_map_builder_t::~hb_ot_map_builder_t ()
92 feature_infos.fini ();
93 for (unsigned int table_index = 0; table_index < 2; table_index++)
94 stages[table_index].fini ();
97 void hb_ot_map_builder_t::add_feature (hb_tag_t tag,
98 hb_ot_map_feature_flags_t flags,
101 if (unlikely (!tag)) return;
102 feature_info_t *info = feature_infos.push();
104 info->seq = feature_infos.length;
105 info->max_value = value;
107 info->default_value = (flags & F_GLOBAL) ? value : 0;
108 info->stage[0] = current_stage[0];
109 info->stage[1] = current_stage[1];
113 hb_ot_map_builder_t::add_lookups (hb_ot_map_t &m,
114 unsigned int table_index,
115 unsigned int feature_index,
116 unsigned int variations_index,
122 unsigned int lookup_indices[32];
123 unsigned int offset, len;
124 unsigned int table_lookup_count;
126 table_lookup_count = hb_ot_layout_table_get_lookup_count (face, table_tags[table_index]);
130 len = ARRAY_LENGTH (lookup_indices);
131 hb_ot_layout_feature_with_variations_get_lookups (face,
132 table_tags[table_index],
138 for (unsigned int i = 0; i < len; i++)
140 if (lookup_indices[i] >= table_lookup_count)
142 hb_ot_map_t::lookup_map_t *lookup = m.lookups[table_index].push ();
144 lookup->index = lookup_indices[i];
145 lookup->auto_zwnj = auto_zwnj;
146 lookup->auto_zwj = auto_zwj;
147 lookup->random = random;
151 } while (len == ARRAY_LENGTH (lookup_indices));
155 void hb_ot_map_builder_t::add_pause (unsigned int table_index, hb_ot_map_t::pause_func_t pause_func)
157 stage_info_t *s = stages[table_index].push ();
158 s->index = current_stage[table_index];
159 s->pause_func = pause_func;
161 current_stage[table_index]++;
165 hb_ot_map_builder_t::compile (hb_ot_map_t &m,
166 const hb_ot_shape_plan_key_t &key)
168 unsigned int global_bit_shift = 8 * sizeof (hb_mask_t) - 1;
169 unsigned int global_bit_mask = 1u << global_bit_shift;
171 m.global_mask = global_bit_mask;
173 unsigned int required_feature_index[2];
174 hb_tag_t required_feature_tag[2];
175 /* We default to applying required feature in stage 0. If the required
176 * feature has a tag that is known to the shaper, we apply required feature
177 * in the stage for that tag.
179 unsigned int required_feature_stage[2] = {0, 0};
181 for (unsigned int table_index = 0; table_index < 2; table_index++)
183 m.chosen_script[table_index] = chosen_script[table_index];
184 m.found_script[table_index] = found_script[table_index];
186 hb_ot_layout_language_get_required_feature (face,
187 table_tags[table_index],
188 script_index[table_index],
189 language_index[table_index],
190 &required_feature_index[table_index],
191 &required_feature_tag[table_index]);
194 /* Sort features and merge duplicates */
195 if (feature_infos.length)
197 feature_infos.qsort ();
199 for (unsigned int i = 1; i < feature_infos.length; i++)
200 if (feature_infos[i].tag != feature_infos[j].tag)
201 feature_infos[++j] = feature_infos[i];
203 if (feature_infos[i].flags & F_GLOBAL) {
204 feature_infos[j].flags |= F_GLOBAL;
205 feature_infos[j].max_value = feature_infos[i].max_value;
206 feature_infos[j].default_value = feature_infos[i].default_value;
208 if (feature_infos[j].flags & F_GLOBAL)
209 feature_infos[j].flags ^= F_GLOBAL;
210 feature_infos[j].max_value = hb_max (feature_infos[j].max_value, feature_infos[i].max_value);
211 /* Inherit default_value from j */
213 feature_infos[j].flags |= (feature_infos[i].flags & F_HAS_FALLBACK);
214 feature_infos[j].stage[0] = hb_min (feature_infos[j].stage[0], feature_infos[i].stage[0]);
215 feature_infos[j].stage[1] = hb_min (feature_infos[j].stage[1], feature_infos[i].stage[1]);
217 feature_infos.shrink (j + 1);
221 /* Allocate bits now */
222 static_assert ((!(HB_GLYPH_FLAG_DEFINED & (HB_GLYPH_FLAG_DEFINED + 1))), "");
223 unsigned int next_bit = hb_popcount (HB_GLYPH_FLAG_DEFINED) + 1;
225 for (unsigned int i = 0; i < feature_infos.length; i++)
227 const feature_info_t *info = &feature_infos[i];
229 unsigned int bits_needed;
231 if ((info->flags & F_GLOBAL) && info->max_value == 1)
232 /* Uses the global bit */
235 /* Limit bits per feature. */
236 bits_needed = hb_min (HB_OT_MAP_MAX_BITS, hb_bit_storage (info->max_value));
238 if (!info->max_value || next_bit + bits_needed >= global_bit_shift)
239 continue; /* Feature disabled, or not enough bits. */
243 unsigned int feature_index[2];
244 for (unsigned int table_index = 0; table_index < 2; table_index++)
246 if (required_feature_tag[table_index] == info->tag)
247 required_feature_stage[table_index] = info->stage[table_index];
249 found |= (bool) hb_ot_layout_language_find_feature (face,
250 table_tags[table_index],
251 script_index[table_index],
252 language_index[table_index],
254 &feature_index[table_index]);
256 if (!found && (info->flags & F_GLOBAL_SEARCH))
258 for (unsigned int table_index = 0; table_index < 2; table_index++)
260 found |= (bool) hb_ot_layout_table_find_feature (face,
261 table_tags[table_index],
263 &feature_index[table_index]);
266 if (!found && !(info->flags & F_HAS_FALLBACK))
270 hb_ot_map_t::feature_map_t *map = m.features.push ();
272 map->tag = info->tag;
273 map->index[0] = feature_index[0];
274 map->index[1] = feature_index[1];
275 map->stage[0] = info->stage[0];
276 map->stage[1] = info->stage[1];
277 map->auto_zwnj = !(info->flags & F_MANUAL_ZWNJ);
278 map->auto_zwj = !(info->flags & F_MANUAL_ZWJ);
279 map->random = !!(info->flags & F_RANDOM);
280 if ((info->flags & F_GLOBAL) && info->max_value == 1) {
281 /* Uses the global bit */
282 map->shift = global_bit_shift;
283 map->mask = global_bit_mask;
285 map->shift = next_bit;
286 map->mask = (1u << (next_bit + bits_needed)) - (1u << next_bit);
287 next_bit += bits_needed;
288 m.global_mask |= (info->default_value << map->shift) & map->mask;
290 map->_1_mask = (1u << map->shift) & map->mask;
291 map->needs_fallback = !found;
293 feature_infos.shrink (0); /* Done with these */
296 add_gsub_pause (nullptr);
297 add_gpos_pause (nullptr);
299 for (unsigned int table_index = 0; table_index < 2; table_index++)
301 /* Collect lookup indices for features */
303 unsigned int stage_index = 0;
304 unsigned int last_num_lookups = 0;
305 for (unsigned stage = 0; stage < current_stage[table_index]; stage++)
307 if (required_feature_index[table_index] != HB_OT_LAYOUT_NO_FEATURE_INDEX &&
308 required_feature_stage[table_index] == stage)
309 add_lookups (m, table_index,
310 required_feature_index[table_index],
311 key.variations_index[table_index],
314 for (unsigned i = 0; i < m.features.length; i++)
315 if (m.features[i].stage[table_index] == stage)
316 add_lookups (m, table_index,
317 m.features[i].index[table_index],
318 key.variations_index[table_index],
320 m.features[i].auto_zwnj,
321 m.features[i].auto_zwj,
322 m.features[i].random);
324 /* Sort lookups and merge duplicates */
325 if (last_num_lookups < m.lookups[table_index].length)
327 m.lookups[table_index].qsort (last_num_lookups, m.lookups[table_index].length);
329 unsigned int j = last_num_lookups;
330 for (unsigned int i = j + 1; i < m.lookups[table_index].length; i++)
331 if (m.lookups[table_index][i].index != m.lookups[table_index][j].index)
332 m.lookups[table_index][++j] = m.lookups[table_index][i];
335 m.lookups[table_index][j].mask |= m.lookups[table_index][i].mask;
336 m.lookups[table_index][j].auto_zwnj &= m.lookups[table_index][i].auto_zwnj;
337 m.lookups[table_index][j].auto_zwj &= m.lookups[table_index][i].auto_zwj;
339 m.lookups[table_index].shrink (j + 1);
342 last_num_lookups = m.lookups[table_index].length;
344 if (stage_index < stages[table_index].length && stages[table_index][stage_index].index == stage) {
345 hb_ot_map_t::stage_map_t *stage_map = m.stages[table_index].push ();
346 stage_map->last_lookup = last_num_lookups;
347 stage_map->pause_func = stages[table_index][stage_index].pause_func;