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