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