2 * Copyright © 2009,2010 Red Hat, Inc.
3 * Copyright © 2010,2011 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
29 #include "hb-ot-map-private.hh"
31 #include "hb-ot-shape-private.hh"
36 hb_ot_map_t::add_lookups (hb_face_t *face,
37 unsigned int table_index,
38 unsigned int feature_index,
41 unsigned int lookup_indices[32];
42 unsigned int offset, len;
46 len = ARRAY_LENGTH (lookup_indices);
47 hb_ot_layout_feature_get_lookup_indexes (face,
48 table_tags[table_index],
53 for (unsigned int i = 0; i < len; i++) {
54 hb_ot_map_t::lookup_map_t *lookup = lookups[table_index].push ();
55 if (unlikely (!lookup))
58 lookup->index = lookup_indices[i];
62 } while (len == ARRAY_LENGTH (lookup_indices));
66 void hb_ot_map_builder_t::add_feature (hb_tag_t tag, unsigned int value, bool global)
68 feature_info_t *info = feature_infos.push();
69 if (unlikely (!info)) return;
71 info->seq = feature_infos.len;
72 info->max_value = value;
73 info->global = global;
74 info->default_value = global ? value : 0;
75 info->stage[0] = current_stage[0];
76 info->stage[1] = current_stage[1];
79 void hb_ot_map_t::apply (unsigned int table_index,
80 hb_ot_map_t::apply_lookup_func_t apply_lookup_func,
82 hb_buffer_t *buffer) const
86 for (unsigned int pause_index = 0; pause_index < pauses[table_index].len; pause_index++) {
87 const pause_map_t *pause = &pauses[table_index][pause_index];
88 for (; i < pause->num_lookups; i++)
89 apply_lookup_func (face_or_font, buffer, lookups[table_index][i].index, lookups[table_index][i].mask);
91 pause->callback.func (this, face_or_font, buffer, pause->callback.user_data);
94 for (; i < lookups[table_index].len; i++)
95 apply_lookup_func (face_or_font, buffer, lookups[table_index][i].index, lookups[table_index][i].mask);
98 void hb_ot_map_t::substitute_closure (hb_face_t *face,
99 hb_set_t *glyphs) const
101 unsigned int table_index = 0;
104 for (unsigned int pause_index = 0; pause_index < pauses[table_index].len; pause_index++) {
105 const pause_map_t *pause = &pauses[table_index][pause_index];
106 for (; i < pause->num_lookups; i++)
107 hb_ot_layout_substitute_closure_lookup (face, glyphs, lookups[table_index][i].index);
110 for (; i < lookups[table_index].len; i++)
111 hb_ot_layout_substitute_closure_lookup (face, glyphs, lookups[table_index][i].index);
114 void hb_ot_map_builder_t::add_pause (unsigned int table_index, hb_ot_map_t::pause_func_t pause_func, void *user_data)
117 pause_info_t *p = pauses[table_index].push ();
119 p->stage = current_stage[table_index];
120 p->callback.func = pause_func;
121 p->callback.user_data = user_data;
125 current_stage[table_index]++;
129 hb_ot_map_builder_t::compile (hb_face_t *face,
130 const hb_segment_properties_t *props,
135 if (!feature_infos.len)
139 /* Fetch script/language indices for GSUB/GPOS. We need these later to skip
140 * features not available in either table and not waste precious bits for them. */
142 hb_tag_t script_tags[3] = {HB_TAG_NONE};
143 hb_tag_t language_tag;
145 hb_ot_tags_from_script (props->script, &script_tags[0], &script_tags[1]);
146 language_tag = hb_ot_tag_from_language (props->language);
148 unsigned int script_index[2], language_index[2];
149 for (unsigned int table_index = 0; table_index < 2; table_index++) {
150 hb_tag_t table_tag = table_tags[table_index];
151 hb_ot_layout_table_choose_script (face, table_tag, script_tags, &script_index[table_index], &m.chosen_script[table_index]);
152 hb_ot_layout_script_find_language (face, table_tag, script_index[table_index], language_tag, &language_index[table_index]);
156 /* Sort features and merge duplicates */
158 feature_infos.sort ();
160 for (unsigned int i = 1; i < feature_infos.len; i++)
161 if (feature_infos[i].tag != feature_infos[j].tag)
162 feature_infos[++j] = feature_infos[i];
164 if (feature_infos[i].global) {
165 feature_infos[j].global = true;
166 feature_infos[j].max_value = feature_infos[i].max_value;
167 feature_infos[j].default_value = feature_infos[i].default_value;
169 feature_infos[j].global = false;
170 feature_infos[j].max_value = MAX (feature_infos[j].max_value, feature_infos[i].max_value);
172 feature_infos[j].stage[0] = MIN (feature_infos[j].stage[0], feature_infos[i].stage[0]);
173 feature_infos[j].stage[1] = MIN (feature_infos[j].stage[1], feature_infos[i].stage[1]);
174 /* Inherit default_value from j */
176 feature_infos.shrink (j + 1);
180 /* Allocate bits now */
181 unsigned int next_bit = 1;
182 for (unsigned int i = 0; i < feature_infos.len; i++) {
183 const feature_info_t *info = &feature_infos[i];
185 unsigned int bits_needed;
187 if (info->global && info->max_value == 1)
188 /* Uses the global bit */
191 bits_needed = _hb_bit_storage (info->max_value);
193 if (!info->max_value || next_bit + bits_needed > 8 * sizeof (hb_mask_t))
194 continue; /* Feature disabled, or not enough bits. */
198 unsigned int feature_index[2];
199 for (unsigned int table_index = 0; table_index < 2; table_index++)
200 found |= hb_ot_layout_language_find_feature (face,
201 table_tags[table_index],
202 script_index[table_index],
203 language_index[table_index],
205 &feature_index[table_index]);
210 hb_ot_map_t::feature_map_t *map = m.features.push ();
214 map->tag = info->tag;
215 map->index[0] = feature_index[0];
216 map->index[1] = feature_index[1];
217 map->stage[0] = info->stage[0];
218 map->stage[1] = info->stage[1];
219 if (info->global && info->max_value == 1) {
220 /* Uses the global bit */
224 map->shift = next_bit;
225 map->mask = (1 << (next_bit + bits_needed)) - (1 << next_bit);
226 next_bit += bits_needed;
228 m.global_mask |= (info->default_value << map->shift) & map->mask;
230 map->_1_mask = (1 << map->shift) & map->mask;
233 feature_infos.shrink (0); /* Done with these */
236 add_gsub_pause (NULL, NULL);
237 add_gpos_pause (NULL, NULL);
239 for (unsigned int table_index = 0; table_index < 2; table_index++) {
240 hb_tag_t table_tag = table_tags[table_index];
242 /* Collect lookup indices for features */
244 unsigned int required_feature_index;
245 if (hb_ot_layout_language_get_required_feature_index (face,
247 script_index[table_index],
248 language_index[table_index],
249 &required_feature_index))
250 m.add_lookups (face, table_index, required_feature_index, 1);
252 unsigned int pause_index = 0;
253 unsigned int last_num_lookups = 0;
254 for (unsigned stage = 0; stage < current_stage[table_index]; stage++)
256 for (unsigned i = 0; i < m.features.len; i++)
257 if (m.features[i].stage[table_index] == stage)
258 m.add_lookups (face, table_index, m.features[i].index[table_index], m.features[i].mask);
260 /* Sort lookups and merge duplicates */
261 if (last_num_lookups < m.lookups[table_index].len)
263 m.lookups[table_index].sort (last_num_lookups, m.lookups[table_index].len);
265 unsigned int j = last_num_lookups;
266 for (unsigned int i = j + 1; i < m.lookups[table_index].len; i++)
267 if (m.lookups[table_index][i].index != m.lookups[table_index][j].index)
268 m.lookups[table_index][++j] = m.lookups[table_index][i];
270 m.lookups[table_index][j].mask |= m.lookups[table_index][i].mask;
271 m.lookups[table_index].shrink (j + 1);
274 last_num_lookups = m.lookups[table_index].len;
276 if (pause_index < pauses[table_index].len && pauses[table_index][pause_index].stage == stage) {
277 hb_ot_map_t::pause_map_t *pause_map = m.pauses[table_index].push ();
278 if (likely (pause_map)) {
279 pause_map->num_lookups = last_num_lookups;
280 pause_map->callback = pauses[table_index][pause_index].callback;