1/*
2 * Copyright © 2009,2010 Red Hat, Inc.
3 * Copyright © 2010,2011,2013 Google, Inc.
4 *
5 * This is part of HarfBuzz, a text shaping library.
6 *
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.
12 *
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
17 * DAMAGE.
18 *
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.
24 *
25 * Red Hat Author(s): Behdad Esfahbod
26 * Google Author(s): Behdad Esfahbod
27 */
28
29#include "hb-ot-map.hh"
30#include "hb-ot-shape.hh"
31#include "hb-ot-layout.hh"
32
33
34void hb_ot_map_t::collect_lookups (unsigned int table_index, hb_set_t *lookups_out) const
35{
36 for (unsigned int i = 0; i < lookups[table_index].length; i++)
37 hb_set_add (lookups_out, lookups[table_index][i].index);
38}
39
40
41hb_ot_map_builder_t::hb_ot_map_builder_t (hb_face_t *face_,
42 const hb_segment_properties_t *props_)
43{
44 memset (this, 0, sizeof (*this));
45
46 feature_infos.init ();
47 for (unsigned int table_index = 0; table_index < 2; table_index++)
48 stages[table_index].init ();
49
50 face = face_;
51 props = *props_;
52
53
54 /* Fetch script/language indices for GSUB/GPOS. We need these later to skip
55 * features not available in either table and not waste precious bits for them. */
56
57 unsigned int script_count = HB_OT_MAX_TAGS_PER_SCRIPT;
58 unsigned int language_count = HB_OT_MAX_TAGS_PER_LANGUAGE;
59 hb_tag_t script_tags[HB_OT_MAX_TAGS_PER_SCRIPT];
60 hb_tag_t language_tags[HB_OT_MAX_TAGS_PER_LANGUAGE];
61
62 hb_ot_tags_from_script_and_language (props.script, props.language, &script_count, script_tags, &language_count, language_tags);
63
64 for (unsigned int table_index = 0; table_index < 2; table_index++) {
65 hb_tag_t table_tag = table_tags[table_index];
66 found_script[table_index] = (bool) hb_ot_layout_table_select_script (face, table_tag, script_count, script_tags, &script_index[table_index], &chosen_script[table_index]);
67 hb_ot_layout_script_select_language (face, table_tag, script_index[table_index], language_count, language_tags, &language_index[table_index]);
68 }
69}
70
71hb_ot_map_builder_t::~hb_ot_map_builder_t ()
72{
73 feature_infos.fini ();
74 for (unsigned int table_index = 0; table_index < 2; table_index++)
75 stages[table_index].fini ();
76}
77
78void hb_ot_map_builder_t::add_feature (hb_tag_t tag,
79 hb_ot_map_feature_flags_t flags,
80 unsigned int value)
81{
82 if (unlikely (!tag)) return;
83 feature_info_t *info = feature_infos.push();
84 info->tag = tag;
85 info->seq = feature_infos.length;
86 info->max_value = value;
87 info->flags = flags;
88 info->default_value = (flags & F_GLOBAL) ? value : 0;
89 info->stage[0] = current_stage[0];
90 info->stage[1] = current_stage[1];
91}
92
93void
94hb_ot_map_builder_t::add_lookups (hb_ot_map_t &m,
95 unsigned int table_index,
96 unsigned int feature_index,
97 unsigned int variations_index,
98 hb_mask_t mask,
99 bool auto_zwnj,
100 bool auto_zwj,
101 bool random)
102{
103 unsigned int lookup_indices[32];
104 unsigned int offset, len;
105 unsigned int table_lookup_count;
106
107 table_lookup_count = hb_ot_layout_table_get_lookup_count (face, table_tags[table_index]);
108
109 offset = 0;
110 do {
111 len = ARRAY_LENGTH (lookup_indices);
112 hb_ot_layout_feature_with_variations_get_lookups (face,
113 table_tags[table_index],
114 feature_index,
115 variations_index,
116 offset, &len,
117 lookup_indices);
118
119 for (unsigned int i = 0; i < len; i++)
120 {
121 if (lookup_indices[i] >= table_lookup_count)
122 continue;
123 hb_ot_map_t::lookup_map_t *lookup = m.lookups[table_index].push ();
124 lookup->mask = mask;
125 lookup->index = lookup_indices[i];
126 lookup->auto_zwnj = auto_zwnj;
127 lookup->auto_zwj = auto_zwj;
128 lookup->random = random;
129 }
130
131 offset += len;
132 } while (len == ARRAY_LENGTH (lookup_indices));
133}
134
135
136void hb_ot_map_builder_t::add_pause (unsigned int table_index, hb_ot_map_t::pause_func_t pause_func)
137{
138 stage_info_t *s = stages[table_index].push ();
139 s->index = current_stage[table_index];
140 s->pause_func = pause_func;
141
142 current_stage[table_index]++;
143}
144
145void
146hb_ot_map_builder_t::compile (hb_ot_map_t &m,
147 const hb_ot_shape_plan_key_t &key)
148{
149 static_assert ((!(HB_GLYPH_FLAG_DEFINED & (HB_GLYPH_FLAG_DEFINED + 1))), "");
150 unsigned int global_bit_mask = HB_GLYPH_FLAG_DEFINED + 1;
151 unsigned int global_bit_shift = hb_popcount (HB_GLYPH_FLAG_DEFINED);
152
153 m.global_mask = global_bit_mask;
154
155 unsigned int required_feature_index[2];
156 hb_tag_t required_feature_tag[2];
157 /* We default to applying required feature in stage 0. If the required
158 * feature has a tag that is known to the shaper, we apply required feature
159 * in the stage for that tag.
160 */
161 unsigned int required_feature_stage[2] = {0, 0};
162
163 for (unsigned int table_index = 0; table_index < 2; table_index++)
164 {
165 m.chosen_script[table_index] = chosen_script[table_index];
166 m.found_script[table_index] = found_script[table_index];
167
168 hb_ot_layout_language_get_required_feature (face,
169 table_tags[table_index],
170 script_index[table_index],
171 language_index[table_index],
172 &required_feature_index[table_index],
173 &required_feature_tag[table_index]);
174 }
175
176 /* Sort features and merge duplicates */
177 if (feature_infos.length)
178 {
179 feature_infos.qsort ();
180 unsigned int j = 0;
181 for (unsigned int i = 1; i < feature_infos.length; i++)
182 if (feature_infos[i].tag != feature_infos[j].tag)
183 feature_infos[++j] = feature_infos[i];
184 else {
185 if (feature_infos[i].flags & F_GLOBAL) {
186 feature_infos[j].flags |= F_GLOBAL;
187 feature_infos[j].max_value = feature_infos[i].max_value;
188 feature_infos[j].default_value = feature_infos[i].default_value;
189 } else {
190 feature_infos[j].flags &= ~F_GLOBAL;
191 feature_infos[j].max_value = MAX (feature_infos[j].max_value, feature_infos[i].max_value);
192 /* Inherit default_value from j */
193 }
194 feature_infos[j].flags |= (feature_infos[i].flags & F_HAS_FALLBACK);
195 feature_infos[j].stage[0] = MIN (feature_infos[j].stage[0], feature_infos[i].stage[0]);
196 feature_infos[j].stage[1] = MIN (feature_infos[j].stage[1], feature_infos[i].stage[1]);
197 }
198 feature_infos.shrink (j + 1);
199 }
200
201
202 /* Allocate bits now */
203 unsigned int next_bit = global_bit_shift + 1;
204
205 for (unsigned int i = 0; i < feature_infos.length; i++)
206 {
207 const feature_info_t *info = &feature_infos[i];
208
209 unsigned int bits_needed;
210
211 if ((info->flags & F_GLOBAL) && info->max_value == 1)
212 /* Uses the global bit */
213 bits_needed = 0;
214 else
215 /* Limit bits per feature. */
216 bits_needed = MIN(HB_OT_MAP_MAX_BITS, hb_bit_storage (info->max_value));
217
218 if (!info->max_value || next_bit + bits_needed > 8 * sizeof (hb_mask_t))
219 continue; /* Feature disabled, or not enough bits. */
220
221
222 hb_bool_t found = false;
223 unsigned int feature_index[2];
224 for (unsigned int table_index = 0; table_index < 2; table_index++)
225 {
226 if (required_feature_tag[table_index] == info->tag)
227 required_feature_stage[table_index] = info->stage[table_index];
228
229 found |= hb_ot_layout_language_find_feature (face,
230 table_tags[table_index],
231 script_index[table_index],
232 language_index[table_index],
233 info->tag,
234 &feature_index[table_index]);
235 }
236 if (!found && (info->flags & F_GLOBAL_SEARCH))
237 {
238 for (unsigned int table_index = 0; table_index < 2; table_index++)
239 {
240 found |= hb_ot_layout_table_find_feature (face,
241 table_tags[table_index],
242 info->tag,
243 &feature_index[table_index]);
244 }
245 }
246 if (!found && !(info->flags & F_HAS_FALLBACK))
247 continue;
248
249
250 hb_ot_map_t::feature_map_t *map = m.features.push ();
251
252 map->tag = info->tag;
253 map->index[0] = feature_index[0];
254 map->index[1] = feature_index[1];
255 map->stage[0] = info->stage[0];
256 map->stage[1] = info->stage[1];
257 map->auto_zwnj = !(info->flags & F_MANUAL_ZWNJ);
258 map->auto_zwj = !(info->flags & F_MANUAL_ZWJ);
259 map->random = !!(info->flags & F_RANDOM);
260 if ((info->flags & F_GLOBAL) && info->max_value == 1) {
261 /* Uses the global bit */
262 map->shift = global_bit_shift;
263 map->mask = global_bit_mask;
264 } else {
265 map->shift = next_bit;
266 map->mask = (1u << (next_bit + bits_needed)) - (1u << next_bit);
267 next_bit += bits_needed;
268 m.global_mask |= (info->default_value << map->shift) & map->mask;
269 }
270 map->_1_mask = (1u << map->shift) & map->mask;
271 map->needs_fallback = !found;
272
273 }
274 feature_infos.shrink (0); /* Done with these */
275
276
277 add_gsub_pause (nullptr);
278 add_gpos_pause (nullptr);
279
280 for (unsigned int table_index = 0; table_index < 2; table_index++)
281 {
282 /* Collect lookup indices for features */
283
284 unsigned int stage_index = 0;
285 unsigned int last_num_lookups = 0;
286 for (unsigned stage = 0; stage < current_stage[table_index]; stage++)
287 {
288 if (required_feature_index[table_index] != HB_OT_LAYOUT_NO_FEATURE_INDEX &&
289 required_feature_stage[table_index] == stage)
290 add_lookups (m, table_index,
291 required_feature_index[table_index],
292 key.variations_index[table_index],
293 global_bit_mask);
294
295 for (unsigned i = 0; i < m.features.length; i++)
296 if (m.features[i].stage[table_index] == stage)
297 add_lookups (m, table_index,
298 m.features[i].index[table_index],
299 key.variations_index[table_index],
300 m.features[i].mask,
301 m.features[i].auto_zwnj,
302 m.features[i].auto_zwj,
303 m.features[i].random);
304
305 /* Sort lookups and merge duplicates */
306 if (last_num_lookups < m.lookups[table_index].length)
307 {
308 m.lookups[table_index].qsort (last_num_lookups, m.lookups[table_index].length);
309
310 unsigned int j = last_num_lookups;
311 for (unsigned int i = j + 1; i < m.lookups[table_index].length; i++)
312 if (m.lookups[table_index][i].index != m.lookups[table_index][j].index)
313 m.lookups[table_index][++j] = m.lookups[table_index][i];
314 else
315 {
316 m.lookups[table_index][j].mask |= m.lookups[table_index][i].mask;
317 m.lookups[table_index][j].auto_zwnj &= m.lookups[table_index][i].auto_zwnj;
318 m.lookups[table_index][j].auto_zwj &= m.lookups[table_index][i].auto_zwj;
319 }
320 m.lookups[table_index].shrink (j + 1);
321 }
322
323 last_num_lookups = m.lookups[table_index].length;
324
325 if (stage_index < stages[table_index].length && stages[table_index][stage_index].index == stage) {
326 hb_ot_map_t::stage_map_t *stage_map = m.stages[table_index].push ();
327 stage_map->last_lookup = last_num_lookups;
328 stage_map->pause_func = stages[table_index][stage_index].pause_func;
329
330 stage_index++;
331 }
332 }
333 }
334}
335