/work/workdir/UnpackedTarball/harfbuzz/src/hb-ot-map.cc
Line | Count | Source |
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.hh" |
30 | | |
31 | | #ifndef HB_NO_OT_SHAPE |
32 | | |
33 | | #include "hb-ot-map.hh" |
34 | | #include "hb-ot-shape.hh" |
35 | | #include "hb-ot-layout.hh" |
36 | | |
37 | | |
38 | | void hb_ot_map_t::collect_lookups (unsigned int table_index, hb_set_t *lookups_out) const |
39 | 0 | { |
40 | 0 | for (unsigned int i = 0; i < lookups[table_index].length; i++) |
41 | 0 | lookups_out->add (lookups[table_index][i].index); |
42 | 0 | } |
43 | | |
44 | | |
45 | | hb_ot_map_builder_t::hb_ot_map_builder_t (hb_face_t *face_, |
46 | | const hb_segment_properties_t &props_) |
47 | 5.85k | { |
48 | 5.85k | hb_memset (this, 0, sizeof (*this)); |
49 | | |
50 | 5.85k | feature_infos.init (); |
51 | 17.5k | for (unsigned int table_index = 0; table_index < 2; table_index++) |
52 | 11.7k | stages[table_index].init (); |
53 | | |
54 | 5.85k | face = face_; |
55 | 5.85k | props = props_; |
56 | | |
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. */ |
59 | | |
60 | 5.85k | unsigned int script_count = HB_OT_MAX_TAGS_PER_SCRIPT; |
61 | 5.85k | unsigned int language_count = HB_OT_MAX_TAGS_PER_LANGUAGE; |
62 | 5.85k | hb_tag_t script_tags[HB_OT_MAX_TAGS_PER_SCRIPT]; |
63 | 5.85k | hb_tag_t language_tags[HB_OT_MAX_TAGS_PER_LANGUAGE]; |
64 | | |
65 | 5.85k | hb_ot_tags_from_script_and_language (props.script, |
66 | 5.85k | props.language, |
67 | 5.85k | &script_count, |
68 | 5.85k | script_tags, |
69 | 5.85k | &language_count, |
70 | 5.85k | language_tags); |
71 | | |
72 | 17.5k | for (unsigned int table_index = 0; table_index < 2; table_index++) |
73 | 11.7k | { |
74 | 11.7k | hb_tag_t table_tag = table_tags[table_index]; |
75 | 11.7k | found_script[table_index] = (bool) hb_ot_layout_table_select_script (face, |
76 | 11.7k | table_tag, |
77 | 11.7k | script_count, |
78 | 11.7k | script_tags, |
79 | 11.7k | &script_index[table_index], |
80 | 11.7k | &chosen_script[table_index]); |
81 | 11.7k | hb_ot_layout_script_select_language (face, |
82 | 11.7k | table_tag, |
83 | 11.7k | script_index[table_index], |
84 | 11.7k | language_count, |
85 | 11.7k | language_tags, |
86 | 11.7k | &language_index[table_index]); |
87 | 11.7k | } |
88 | 5.85k | } |
89 | | |
90 | | hb_ot_map_builder_t::~hb_ot_map_builder_t () |
91 | 5.85k | { |
92 | 5.85k | feature_infos.fini (); |
93 | 17.5k | for (unsigned int table_index = 0; table_index < 2; table_index++) |
94 | 11.7k | stages[table_index].fini (); |
95 | 5.85k | } |
96 | | |
97 | | void hb_ot_map_builder_t::add_feature (hb_tag_t tag, |
98 | | hb_ot_map_feature_flags_t flags, |
99 | | unsigned int value) |
100 | 433k | { |
101 | 433k | if (unlikely (!tag)) return; |
102 | 433k | feature_info_t *info = feature_infos.push(); |
103 | 433k | info->tag = tag; |
104 | 433k | info->seq = feature_infos.length; |
105 | 433k | info->max_value = value; |
106 | 433k | info->flags = flags; |
107 | 433k | info->default_value = (flags & F_GLOBAL) ? value : 0; |
108 | 433k | info->stage[0] = current_stage[0]; |
109 | 433k | info->stage[1] = current_stage[1]; |
110 | 433k | } |
111 | | |
112 | | bool hb_ot_map_builder_t::has_feature (hb_tag_t tag) |
113 | 145 | { |
114 | 435 | for (unsigned int table_index = 0; table_index < 2; table_index++) |
115 | 290 | { |
116 | 290 | if (hb_ot_layout_language_find_feature (face, |
117 | 290 | table_tags[table_index], |
118 | 290 | script_index[table_index], |
119 | 290 | language_index[table_index], |
120 | 290 | tag, |
121 | 290 | nullptr)) |
122 | 0 | return true; |
123 | 290 | } |
124 | 145 | return false; |
125 | 145 | } |
126 | | |
127 | | void |
128 | | hb_ot_map_builder_t::add_lookups (hb_ot_map_t &m, |
129 | | unsigned int table_index, |
130 | | unsigned int feature_index, |
131 | | const hb_ot_shape_plan_key_t &key, |
132 | | hb_mask_t mask, |
133 | | bool auto_zwnj, |
134 | | bool auto_zwj, |
135 | | bool random, |
136 | | bool per_syllable, |
137 | | hb_tag_t feature_tag) |
138 | 16.9k | { |
139 | 16.9k | unsigned int lookup_indices[32]; |
140 | 16.9k | unsigned int offset, len; |
141 | 16.9k | unsigned int table_lookup_count; |
142 | | |
143 | 16.9k | table_lookup_count = hb_ot_layout_table_get_lookup_count (face, table_tags[table_index]); |
144 | | |
145 | 16.9k | hb_array_t<const unsigned> varied_lookups; |
146 | 16.9k | if (key.get_lookup_variations (table_index, feature_index, varied_lookups)) |
147 | 0 | { |
148 | 0 | for (unsigned int lookup_index : varied_lookups) |
149 | 0 | { |
150 | 0 | if (lookup_index >= table_lookup_count) |
151 | 0 | continue; |
152 | 0 | hb_ot_map_t::lookup_map_t *lookup = m.lookups[table_index].push (); |
153 | 0 | lookup->mask = mask; |
154 | 0 | lookup->index = lookup_index; |
155 | 0 | lookup->auto_zwnj = auto_zwnj; |
156 | 0 | lookup->auto_zwj = auto_zwj; |
157 | 0 | lookup->random = random; |
158 | 0 | lookup->per_syllable = per_syllable; |
159 | 0 | lookup->feature_tag = feature_tag; |
160 | 0 | } |
161 | 0 | return; |
162 | 0 | } |
163 | | |
164 | 16.9k | offset = 0; |
165 | 16.9k | do { |
166 | 16.9k | len = ARRAY_LENGTH (lookup_indices); |
167 | 16.9k | hb_ot_layout_feature_with_variations_get_lookups (face, |
168 | 16.9k | table_tags[table_index], |
169 | 16.9k | feature_index, |
170 | 16.9k | key.variations_index[table_index], |
171 | 16.9k | offset, &len, |
172 | 16.9k | lookup_indices); |
173 | | |
174 | 31.2k | for (unsigned int i = 0; i < len; i++) |
175 | 14.2k | { |
176 | 14.2k | if (lookup_indices[i] >= table_lookup_count) |
177 | 0 | continue; |
178 | 14.2k | hb_ot_map_t::lookup_map_t *lookup = m.lookups[table_index].push (); |
179 | 14.2k | lookup->mask = mask; |
180 | 14.2k | lookup->index = lookup_indices[i]; |
181 | 14.2k | lookup->auto_zwnj = auto_zwnj; |
182 | 14.2k | lookup->auto_zwj = auto_zwj; |
183 | 14.2k | lookup->random = random; |
184 | 14.2k | lookup->per_syllable = per_syllable; |
185 | 14.2k | lookup->feature_tag = feature_tag; |
186 | 14.2k | } |
187 | | |
188 | 16.9k | offset += len; |
189 | 16.9k | } while (len == ARRAY_LENGTH (lookup_indices)); |
190 | 16.9k | } |
191 | | |
192 | | |
193 | | void hb_ot_map_builder_t::add_pause (unsigned int table_index, hb_ot_map_t::pause_func_t pause_func) |
194 | 19.4k | { |
195 | 19.4k | stage_info_t *s = stages[table_index].push (); |
196 | 19.4k | s->index = current_stage[table_index]; |
197 | 19.4k | s->pause_func = pause_func; |
198 | | |
199 | 19.4k | current_stage[table_index]++; |
200 | 19.4k | } |
201 | | |
202 | | void |
203 | | hb_ot_map_builder_t::compile (hb_ot_map_t &m, |
204 | | const hb_ot_shape_plan_key_t &key) |
205 | 5.85k | { |
206 | 5.85k | unsigned int global_bit_shift = 8 * sizeof (hb_mask_t) - 1; |
207 | 5.85k | unsigned int global_bit_mask = 1u << global_bit_shift; |
208 | | |
209 | 5.85k | m.global_mask = global_bit_mask; |
210 | | |
211 | 5.85k | unsigned int required_feature_index[2]; |
212 | 5.85k | hb_tag_t required_feature_tag[2]; |
213 | | /* We default to applying required feature in stage 0. If the required |
214 | | * feature has a tag that is known to the shaper, we apply required feature |
215 | | * in the stage for that tag. |
216 | | */ |
217 | 5.85k | unsigned int required_feature_stage[2] = {0, 0}; |
218 | | |
219 | 17.5k | for (unsigned int table_index = 0; table_index < 2; table_index++) |
220 | 11.7k | { |
221 | 11.7k | m.chosen_script[table_index] = chosen_script[table_index]; |
222 | 11.7k | m.found_script[table_index] = found_script[table_index]; |
223 | | |
224 | 11.7k | hb_ot_layout_language_get_required_feature (face, |
225 | 11.7k | table_tags[table_index], |
226 | 11.7k | script_index[table_index], |
227 | 11.7k | language_index[table_index], |
228 | 11.7k | &required_feature_index[table_index], |
229 | 11.7k | &required_feature_tag[table_index]); |
230 | 11.7k | } |
231 | | |
232 | | /* Sort features and merge duplicates */ |
233 | 5.85k | if (feature_infos.length) |
234 | 5.85k | { |
235 | 5.85k | if (!is_simple) |
236 | 5.02k | feature_infos.qsort (); |
237 | 5.85k | auto *f = feature_infos.arrayZ; |
238 | 5.85k | unsigned int j = 0; |
239 | 5.85k | unsigned count = feature_infos.length; |
240 | 433k | for (unsigned int i = 1; i < count; i++) |
241 | 427k | if (f[i].tag != f[j].tag) |
242 | 155k | f[++j] = f[i]; |
243 | 272k | else { |
244 | 272k | if (f[i].flags & F_GLOBAL) { |
245 | 271k | f[j].flags |= F_GLOBAL; |
246 | 271k | f[j].max_value = f[i].max_value; |
247 | 271k | f[j].default_value = f[i].default_value; |
248 | 271k | } else { |
249 | 155 | if (f[j].flags & F_GLOBAL) |
250 | 153 | f[j].flags ^= F_GLOBAL; |
251 | 155 | f[j].max_value = hb_max (f[j].max_value, f[i].max_value); |
252 | | /* Inherit default_value from j */ |
253 | 155 | } |
254 | 272k | f[j].flags |= (f[i].flags & F_HAS_FALLBACK); |
255 | 272k | f[j].stage[0] = hb_min (f[j].stage[0], f[i].stage[0]); |
256 | 272k | f[j].stage[1] = hb_min (f[j].stage[1], f[i].stage[1]); |
257 | 272k | } |
258 | 5.85k | feature_infos.shrink (j + 1); |
259 | 5.85k | } |
260 | | |
261 | 5.85k | hb_map_t feature_indices[2]; |
262 | 17.5k | for (unsigned int table_index = 0; table_index < 2; table_index++) |
263 | 11.7k | hb_ot_layout_collect_features_map (face, |
264 | 11.7k | table_tags[table_index], |
265 | 11.7k | script_index[table_index], |
266 | 11.7k | language_index[table_index], |
267 | 11.7k | &feature_indices[table_index]); |
268 | | |
269 | | /* Allocate bits now */ |
270 | 5.85k | static_assert ((!(HB_GLYPH_FLAG_DEFINED & (HB_GLYPH_FLAG_DEFINED + 1))), ""); |
271 | 5.85k | unsigned int next_bit = hb_popcount (HB_GLYPH_FLAG_DEFINED) + 1; |
272 | | |
273 | 5.85k | unsigned count = feature_infos.length; |
274 | 166k | for (unsigned int i = 0; i < count; i++) |
275 | 161k | { |
276 | 161k | const feature_info_t *info = &feature_infos[i]; |
277 | | |
278 | 161k | unsigned int bits_needed; |
279 | | |
280 | 161k | if ((info->flags & F_GLOBAL) && info->max_value == 1) |
281 | | /* Uses the global bit */ |
282 | 128k | bits_needed = 0; |
283 | 32.5k | else |
284 | | /* Limit bits per feature. */ |
285 | 32.5k | bits_needed = hb_min (HB_OT_MAP_MAX_BITS, hb_bit_storage (info->max_value)); |
286 | | |
287 | 161k | if (!info->max_value || next_bit + bits_needed >= global_bit_shift) |
288 | 6.11k | continue; /* Feature disabled, or not enough bits. */ |
289 | | |
290 | 154k | bool found = false; |
291 | 154k | unsigned int feature_index[2]; |
292 | 464k | for (unsigned int table_index = 0; table_index < 2; table_index++) |
293 | 309k | { |
294 | 309k | if (required_feature_tag[table_index] == info->tag) |
295 | 0 | required_feature_stage[table_index] = info->stage[table_index]; |
296 | | |
297 | 309k | hb_codepoint_t *index; |
298 | 309k | if (feature_indices[table_index].has (info->tag, &index)) |
299 | 4.46k | { |
300 | 4.46k | feature_index[table_index] = *index; |
301 | 4.46k | found = true; |
302 | 4.46k | } |
303 | 305k | else |
304 | 305k | feature_index[table_index] = HB_OT_LAYOUT_NO_FEATURE_INDEX; |
305 | 309k | } |
306 | 154k | if (!found && (info->flags & F_GLOBAL_SEARCH)) |
307 | 203 | { |
308 | 609 | for (unsigned int table_index = 0; table_index < 2; table_index++) |
309 | 406 | { |
310 | 406 | found |= (bool) hb_ot_layout_table_find_feature (face, |
311 | 406 | table_tags[table_index], |
312 | 406 | info->tag, |
313 | 406 | &feature_index[table_index]); |
314 | 406 | } |
315 | 203 | } |
316 | 154k | if (!found && !(info->flags & F_HAS_FALLBACK)) |
317 | 146k | continue; |
318 | | |
319 | | |
320 | 8.49k | hb_ot_map_t::feature_map_t *map = m.features.push (); |
321 | | |
322 | 8.49k | map->tag = info->tag; |
323 | 8.49k | map->index[0] = feature_index[0]; |
324 | 8.49k | map->index[1] = feature_index[1]; |
325 | 8.49k | map->stage[0] = info->stage[0]; |
326 | 8.49k | map->stage[1] = info->stage[1]; |
327 | 8.49k | map->auto_zwnj = !(info->flags & F_MANUAL_ZWNJ); |
328 | 8.49k | map->auto_zwj = !(info->flags & F_MANUAL_ZWJ); |
329 | 8.49k | map->random = !!(info->flags & F_RANDOM); |
330 | 8.49k | map->per_syllable = !!(info->flags & F_PER_SYLLABLE); |
331 | 8.49k | if ((info->flags & F_GLOBAL) && info->max_value == 1) { |
332 | | /* Uses the global bit */ |
333 | 7.91k | map->shift = global_bit_shift; |
334 | 7.91k | map->mask = global_bit_mask; |
335 | 7.91k | } else { |
336 | 580 | map->shift = next_bit; |
337 | 580 | map->mask = (1u << (next_bit + bits_needed)) - (1u << next_bit); |
338 | 580 | next_bit += bits_needed; |
339 | 580 | m.global_mask |= (info->default_value << map->shift) & map->mask; |
340 | 580 | } |
341 | 8.49k | map->_1_mask = (1u << map->shift) & map->mask; |
342 | 8.49k | map->needs_fallback = !found; |
343 | 8.49k | } |
344 | | //feature_infos.shrink (0); /* Done with these */ |
345 | 5.85k | if (is_simple) |
346 | 823 | m.features.qsort (); |
347 | | |
348 | 5.85k | add_gsub_pause (nullptr); |
349 | 5.85k | add_gpos_pause (nullptr); |
350 | | |
351 | 17.5k | for (unsigned int table_index = 0; table_index < 2; table_index++) |
352 | 11.7k | { |
353 | | /* Collect lookup indices for features */ |
354 | 11.7k | auto &lookups = m.lookups[table_index]; |
355 | | |
356 | 11.7k | unsigned int stage_index = 0; |
357 | 11.7k | unsigned int last_num_lookups = 0; |
358 | 31.1k | for (unsigned stage = 0; stage < current_stage[table_index]; stage++) |
359 | 19.4k | { |
360 | 19.4k | if (required_feature_index[table_index] != HB_OT_LAYOUT_NO_FEATURE_INDEX && |
361 | 0 | required_feature_stage[table_index] == stage) |
362 | 0 | add_lookups (m, table_index, |
363 | 0 | required_feature_index[table_index], |
364 | 0 | key, |
365 | 0 | global_bit_mask); |
366 | | |
367 | 19.4k | for (auto &feature : m.features) |
368 | 35.5k | { |
369 | 35.5k | if (feature.stage[table_index] == stage) |
370 | 16.9k | add_lookups (m, table_index, |
371 | 16.9k | feature.index[table_index], |
372 | 16.9k | key, |
373 | 16.9k | feature.mask, |
374 | 16.9k | feature.auto_zwnj, |
375 | 16.9k | feature.auto_zwj, |
376 | 16.9k | feature.random, |
377 | 16.9k | feature.per_syllable, |
378 | 16.9k | feature.tag); |
379 | 35.5k | } |
380 | | |
381 | | /* Sort lookups and merge duplicates */ |
382 | 19.4k | if (last_num_lookups + 1 < lookups.length) |
383 | 1.33k | { |
384 | 1.33k | lookups.as_array ().sub_array (last_num_lookups, lookups.length - last_num_lookups).qsort (); |
385 | | |
386 | 1.33k | unsigned int j = last_num_lookups; |
387 | 13.2k | for (unsigned int i = j + 1; i < lookups.length; i++) |
388 | 11.9k | if (lookups.arrayZ[i].index != lookups.arrayZ[j].index) |
389 | 11.9k | lookups.arrayZ[++j] = lookups.arrayZ[i]; |
390 | 0 | else |
391 | 0 | { |
392 | 0 | lookups.arrayZ[j].mask |= lookups.arrayZ[i].mask; |
393 | 0 | lookups.arrayZ[j].auto_zwnj &= lookups.arrayZ[i].auto_zwnj; |
394 | 0 | lookups.arrayZ[j].auto_zwj &= lookups.arrayZ[i].auto_zwj; |
395 | 0 | } |
396 | 1.33k | lookups.shrink (j + 1); |
397 | 1.33k | } |
398 | | |
399 | 19.4k | last_num_lookups = lookups.length; |
400 | | |
401 | 19.4k | if (stage_index < stages[table_index].length && stages[table_index][stage_index].index == stage) { |
402 | 19.4k | hb_ot_map_t::stage_map_t *stage_map = m.stages[table_index].push (); |
403 | 19.4k | stage_map->last_lookup = last_num_lookups; |
404 | 19.4k | stage_map->pause_func = stages[table_index][stage_index].pause_func; |
405 | | |
406 | 19.4k | stage_index++; |
407 | 19.4k | } |
408 | 19.4k | } |
409 | 11.7k | } |
410 | 5.85k | } |
411 | | |
412 | | unsigned int hb_ot_map_t::get_feature_tags (unsigned int start_offset, unsigned int *tag_count, hb_tag_t *tags) const |
413 | 0 | { |
414 | 0 | if (tag_count) |
415 | 0 | { |
416 | 0 | auto sub_features = features.as_array ().sub_array (start_offset, tag_count); |
417 | 0 | if (tags) |
418 | 0 | { |
419 | 0 | for (unsigned int i = 0; i < sub_features.length; i++) |
420 | 0 | tags[i] = sub_features[i].tag; |
421 | 0 | } |
422 | 0 | } |
423 | |
|
424 | 0 | return features.length; |
425 | 0 | } |
426 | | |
427 | | #endif |