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"
33 hb_ot_map_t::add_lookups (hb_face_t *face,
34 unsigned int table_index,
35 unsigned int feature_index,
38 unsigned int lookup_indices[32];
39 unsigned int offset, len;
43 len = ARRAY_LENGTH (lookup_indices);
44 hb_ot_layout_feature_get_lookup_indexes (face,
45 table_tags[table_index],
50 for (unsigned int i = 0; i < len; i++) {
51 hb_ot_map_t::lookup_map_t *lookup = lookups[table_index].push ();
52 if (unlikely (!lookup))
55 lookup->index = lookup_indices[i];
59 } while (len == ARRAY_LENGTH (lookup_indices));
63 void hb_ot_map_builder_t::add_feature (hb_tag_t tag, unsigned int value, bool global)
65 feature_info_t *info = feature_infos.push();
66 if (unlikely (!info)) return;
68 info->seq = feature_infos.len;
69 info->max_value = value;
70 info->global = global;
71 info->default_value = global ? value : 0;
72 info->stage[0] = current_stage[0];
73 info->stage[1] = current_stage[1];
76 /* Keep the next two functions in sync. */
78 void hb_ot_map_t::substitute (const hb_ot_shape_plan_t *plan, hb_font_t *font, hb_buffer_t *buffer) const
80 const unsigned int table_index = 0;
83 for (unsigned int pause_index = 0; pause_index < pauses[table_index].len; pause_index++) {
84 const pause_map_t *pause = &pauses[table_index][pause_index];
85 for (; i < pause->num_lookups; i++)
86 hb_ot_layout_substitute_lookup (font, buffer, lookups[table_index][i].index, lookups[table_index][i].mask);
88 buffer->clear_output ();
91 pause->callback (plan, font, buffer);
94 for (; i < lookups[table_index].len; i++)
95 hb_ot_layout_substitute_lookup (font, buffer, lookups[table_index][i].index, lookups[table_index][i].mask);
98 void hb_ot_map_t::position (const hb_ot_shape_plan_t *plan, hb_font_t *font, hb_buffer_t *buffer) const
100 const unsigned int table_index = 1;
103 for (unsigned int pause_index = 0; pause_index < pauses[table_index].len; pause_index++) {
104 const pause_map_t *pause = &pauses[table_index][pause_index];
105 for (; i < pause->num_lookups; i++)
106 hb_ot_layout_position_lookup (font, buffer, lookups[table_index][i].index, lookups[table_index][i].mask);
109 pause->callback (plan, font, buffer);
112 for (; i < lookups[table_index].len; i++)
113 hb_ot_layout_position_lookup (font, buffer, lookups[table_index][i].index, lookups[table_index][i].mask);
116 void hb_ot_map_t::substitute_closure (const hb_ot_shape_plan_t *plan, hb_face_t *face, hb_set_t *glyphs) const
118 unsigned int table_index = 0;
121 for (unsigned int pause_index = 0; pause_index < pauses[table_index].len; pause_index++) {
122 const pause_map_t *pause = &pauses[table_index][pause_index];
123 for (; i < pause->num_lookups; i++)
124 hb_ot_layout_substitute_closure_lookup (face, glyphs, lookups[table_index][i].index);
127 for (; i < lookups[table_index].len; i++)
128 hb_ot_layout_substitute_closure_lookup (face, glyphs, lookups[table_index][i].index);
131 void hb_ot_map_builder_t::add_pause (unsigned int table_index, hb_ot_map_t::pause_func_t pause_func)
133 pause_info_t *p = pauses[table_index].push ();
135 p->stage = current_stage[table_index];
136 p->callback = pause_func;
139 current_stage[table_index]++;
143 hb_ot_map_builder_t::compile (hb_face_t *face,
144 const hb_segment_properties_t *props,
149 if (!feature_infos.len)
153 /* Fetch script/language indices for GSUB/GPOS. We need these later to skip
154 * features not available in either table and not waste precious bits for them. */
156 hb_tag_t script_tags[3] = {HB_TAG_NONE};
157 hb_tag_t language_tag;
159 hb_ot_tags_from_script (props->script, &script_tags[0], &script_tags[1]);
160 language_tag = hb_ot_tag_from_language (props->language);
162 unsigned int script_index[2], language_index[2];
163 for (unsigned int table_index = 0; table_index < 2; table_index++) {
164 hb_tag_t table_tag = table_tags[table_index];
165 hb_ot_layout_table_choose_script (face, table_tag, script_tags, &script_index[table_index], &m.chosen_script[table_index]);
166 hb_ot_layout_script_find_language (face, table_tag, script_index[table_index], language_tag, &language_index[table_index]);
170 /* Sort features and merge duplicates */
172 feature_infos.sort ();
174 for (unsigned int i = 1; i < feature_infos.len; i++)
175 if (feature_infos[i].tag != feature_infos[j].tag)
176 feature_infos[++j] = feature_infos[i];
178 if (feature_infos[i].global) {
179 feature_infos[j].global = true;
180 feature_infos[j].max_value = feature_infos[i].max_value;
181 feature_infos[j].default_value = feature_infos[i].default_value;
183 feature_infos[j].global = false;
184 feature_infos[j].max_value = MAX (feature_infos[j].max_value, feature_infos[i].max_value);
186 feature_infos[j].stage[0] = MIN (feature_infos[j].stage[0], feature_infos[i].stage[0]);
187 feature_infos[j].stage[1] = MIN (feature_infos[j].stage[1], feature_infos[i].stage[1]);
188 /* Inherit default_value from j */
190 feature_infos.shrink (j + 1);
194 /* Allocate bits now */
195 unsigned int next_bit = 1;
196 for (unsigned int i = 0; i < feature_infos.len; i++) {
197 const feature_info_t *info = &feature_infos[i];
199 unsigned int bits_needed;
201 if (info->global && info->max_value == 1)
202 /* Uses the global bit */
205 bits_needed = _hb_bit_storage (info->max_value);
207 if (!info->max_value || next_bit + bits_needed > 8 * sizeof (hb_mask_t))
208 continue; /* Feature disabled, or not enough bits. */
212 unsigned int feature_index[2];
213 for (unsigned int table_index = 0; table_index < 2; table_index++)
214 found |= hb_ot_layout_language_find_feature (face,
215 table_tags[table_index],
216 script_index[table_index],
217 language_index[table_index],
219 &feature_index[table_index]);
224 hb_ot_map_t::feature_map_t *map = m.features.push ();
228 map->tag = info->tag;
229 map->index[0] = feature_index[0];
230 map->index[1] = feature_index[1];
231 map->stage[0] = info->stage[0];
232 map->stage[1] = info->stage[1];
233 if (info->global && info->max_value == 1) {
234 /* Uses the global bit */
238 map->shift = next_bit;
239 map->mask = (1 << (next_bit + bits_needed)) - (1 << next_bit);
240 next_bit += bits_needed;
242 m.global_mask |= (info->default_value << map->shift) & map->mask;
244 map->_1_mask = (1 << map->shift) & map->mask;
247 feature_infos.shrink (0); /* Done with these */
250 add_gsub_pause (NULL);
251 add_gpos_pause (NULL);
253 for (unsigned int table_index = 0; table_index < 2; table_index++) {
254 hb_tag_t table_tag = table_tags[table_index];
256 /* Collect lookup indices for features */
258 unsigned int required_feature_index;
259 if (hb_ot_layout_language_get_required_feature_index (face,
261 script_index[table_index],
262 language_index[table_index],
263 &required_feature_index))
264 m.add_lookups (face, table_index, required_feature_index, 1);
266 unsigned int pause_index = 0;
267 unsigned int last_num_lookups = 0;
268 for (unsigned stage = 0; stage < current_stage[table_index]; stage++)
270 for (unsigned i = 0; i < m.features.len; i++)
271 if (m.features[i].stage[table_index] == stage)
272 m.add_lookups (face, table_index, m.features[i].index[table_index], m.features[i].mask);
274 /* Sort lookups and merge duplicates */
275 if (last_num_lookups < m.lookups[table_index].len)
277 m.lookups[table_index].sort (last_num_lookups, m.lookups[table_index].len);
279 unsigned int j = last_num_lookups;
280 for (unsigned int i = j + 1; i < m.lookups[table_index].len; i++)
281 if (m.lookups[table_index][i].index != m.lookups[table_index][j].index)
282 m.lookups[table_index][++j] = m.lookups[table_index][i];
284 m.lookups[table_index][j].mask |= m.lookups[table_index][i].mask;
285 m.lookups[table_index].shrink (j + 1);
288 last_num_lookups = m.lookups[table_index].len;
290 if (pause_index < pauses[table_index].len && pauses[table_index][pause_index].stage == stage) {
291 hb_ot_map_t::pause_map_t *pause_map = m.pauses[table_index].push ();
292 if (likely (pause_map)) {
293 pause_map->num_lookups = last_num_lookups;
294 pause_map->callback = pauses[table_index][pause_index].callback;