1 : /*
2 : * Copyright © 2009,2010 Red Hat, Inc.
3 : * Copyright © 2010,2011 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-shape-private.hh"
32 :
33 :
34 :
35 : void
36 0 : hb_ot_map_t::add_lookups (hb_face_t *face,
37 : unsigned int table_index,
38 : unsigned int feature_index,
39 : hb_mask_t mask)
40 : {
41 : unsigned int lookup_indices[32];
42 : unsigned int offset, len;
43 :
44 0 : offset = 0;
45 0 : do {
46 0 : len = ARRAY_LENGTH (lookup_indices);
47 : hb_ot_layout_feature_get_lookup_indexes (face,
48 : table_tags[table_index],
49 : feature_index,
50 : offset, &len,
51 0 : lookup_indices);
52 :
53 0 : for (unsigned int i = 0; i < len; i++) {
54 0 : hb_ot_map_t::lookup_map_t *lookup = lookups[table_index].push ();
55 0 : if (unlikely (!lookup))
56 0 : return;
57 0 : lookup->mask = mask;
58 0 : lookup->index = lookup_indices[i];
59 : }
60 :
61 0 : offset += len;
62 : } while (len == ARRAY_LENGTH (lookup_indices));
63 : }
64 :
65 :
66 0 : void hb_ot_map_builder_t::add_feature (hb_tag_t tag, unsigned int value, bool global)
67 : {
68 0 : feature_info_t *info = feature_infos.push();
69 0 : if (unlikely (!info)) return;
70 0 : info->tag = tag;
71 0 : info->seq = feature_infos.len;
72 0 : info->max_value = value;
73 0 : info->global = global;
74 0 : info->default_value = global ? value : 0;
75 0 : info->stage[0] = current_stage[0];
76 0 : info->stage[1] = current_stage[1];
77 : }
78 :
79 0 : void hb_ot_map_t::apply (unsigned int table_index,
80 : hb_ot_map_t::apply_lookup_func_t apply_lookup_func,
81 : void *face_or_font,
82 : hb_buffer_t *buffer) const
83 : {
84 0 : unsigned int i = 0;
85 :
86 0 : for (unsigned int pause_index = 0; pause_index < pauses[table_index].len; pause_index++) {
87 0 : const pause_map_t *pause = &pauses[table_index][pause_index];
88 0 : for (; i < pause->num_lookups; i++)
89 0 : apply_lookup_func (face_or_font, buffer, lookups[table_index][i].index, lookups[table_index][i].mask);
90 :
91 0 : pause->callback.func (this, face_or_font, buffer, pause->callback.user_data);
92 : }
93 :
94 0 : for (; i < lookups[table_index].len; i++)
95 0 : apply_lookup_func (face_or_font, buffer, lookups[table_index][i].index, lookups[table_index][i].mask);
96 0 : }
97 :
98 :
99 0 : void hb_ot_map_builder_t::add_pause (unsigned int table_index, hb_ot_map_t::pause_func_t pause_func, void *user_data)
100 : {
101 0 : if (pause_func) {
102 0 : pause_info_t *p = pauses[table_index].push ();
103 0 : if (likely (p)) {
104 0 : p->stage = current_stage[table_index];
105 0 : p->callback.func = pause_func;
106 0 : p->callback.user_data = user_data;
107 : }
108 : }
109 :
110 0 : current_stage[table_index]++;
111 0 : }
112 :
113 : void
114 0 : hb_ot_map_builder_t::compile (hb_face_t *face,
115 : const hb_segment_properties_t *props,
116 : hb_ot_map_t &m)
117 : {
118 0 : m.global_mask = 1;
119 :
120 0 : if (!feature_infos.len)
121 0 : return;
122 :
123 :
124 : /* Fetch script/language indices for GSUB/GPOS. We need these later to skip
125 : * features not available in either table and not waste precious bits for them. */
126 :
127 0 : hb_tag_t script_tags[3] = {HB_TAG_NONE};
128 : hb_tag_t language_tag;
129 :
130 0 : hb_ot_tags_from_script (props->script, &script_tags[0], &script_tags[1]);
131 0 : language_tag = hb_ot_tag_from_language (props->language);
132 :
133 : unsigned int script_index[2], language_index[2];
134 0 : for (unsigned int table_index = 0; table_index < 2; table_index++) {
135 0 : hb_tag_t table_tag = table_tags[table_index];
136 0 : hb_ot_layout_table_choose_script (face, table_tag, script_tags, &script_index[table_index], &m.chosen_script[table_index]);
137 0 : hb_ot_layout_script_find_language (face, table_tag, script_index[table_index], language_tag, &language_index[table_index]);
138 : }
139 :
140 :
141 : /* Sort features and merge duplicates */
142 : {
143 0 : feature_infos.sort ();
144 0 : unsigned int j = 0;
145 0 : for (unsigned int i = 1; i < feature_infos.len; i++)
146 0 : if (feature_infos[i].tag != feature_infos[j].tag)
147 0 : feature_infos[++j] = feature_infos[i];
148 : else {
149 0 : if (feature_infos[i].global) {
150 0 : feature_infos[j].global = true;
151 0 : feature_infos[j].max_value = feature_infos[i].max_value;
152 0 : feature_infos[j].default_value = feature_infos[i].default_value;
153 : } else {
154 0 : feature_infos[j].global = false;
155 0 : feature_infos[j].max_value = MAX (feature_infos[j].max_value, feature_infos[i].max_value);
156 : }
157 0 : feature_infos[j].stage[0] = MIN (feature_infos[j].stage[0], feature_infos[i].stage[0]);
158 0 : feature_infos[j].stage[1] = MIN (feature_infos[j].stage[1], feature_infos[i].stage[1]);
159 : /* Inherit default_value from j */
160 : }
161 0 : feature_infos.shrink (j + 1);
162 : }
163 :
164 :
165 : /* Allocate bits now */
166 0 : unsigned int next_bit = 1;
167 0 : for (unsigned int i = 0; i < feature_infos.len; i++) {
168 0 : const feature_info_t *info = &feature_infos[i];
169 :
170 : unsigned int bits_needed;
171 :
172 0 : if (info->global && info->max_value == 1)
173 : /* Uses the global bit */
174 0 : bits_needed = 0;
175 : else
176 0 : bits_needed = _hb_bit_storage (info->max_value);
177 :
178 0 : if (!info->max_value || next_bit + bits_needed > 8 * sizeof (hb_mask_t))
179 0 : continue; /* Feature disabled, or not enough bits. */
180 :
181 :
182 0 : bool found = false;
183 : unsigned int feature_index[2];
184 0 : for (unsigned int table_index = 0; table_index < 2; table_index++)
185 : found |= hb_ot_layout_language_find_feature (face,
186 : table_tags[table_index],
187 : script_index[table_index],
188 : language_index[table_index],
189 : info->tag,
190 0 : &feature_index[table_index]);
191 0 : if (!found)
192 0 : continue;
193 :
194 :
195 0 : hb_ot_map_t::feature_map_t *map = m.features.push ();
196 0 : if (unlikely (!map))
197 0 : break;
198 :
199 0 : map->tag = info->tag;
200 0 : map->index[0] = feature_index[0];
201 0 : map->index[1] = feature_index[1];
202 0 : map->stage[0] = info->stage[0];
203 0 : map->stage[1] = info->stage[1];
204 0 : if (info->global && info->max_value == 1) {
205 : /* Uses the global bit */
206 0 : map->shift = 0;
207 0 : map->mask = 1;
208 : } else {
209 0 : map->shift = next_bit;
210 0 : map->mask = (1 << (next_bit + bits_needed)) - (1 << next_bit);
211 0 : next_bit += bits_needed;
212 0 : if (info->global)
213 0 : m.global_mask |= (info->default_value << map->shift) & map->mask;
214 : }
215 0 : map->_1_mask = (1 << map->shift) & map->mask;
216 :
217 : }
218 0 : feature_infos.shrink (0); /* Done with these */
219 :
220 :
221 0 : add_gsub_pause (NULL, NULL);
222 0 : add_gpos_pause (NULL, NULL);
223 :
224 0 : for (unsigned int table_index = 0; table_index < 2; table_index++) {
225 0 : hb_tag_t table_tag = table_tags[table_index];
226 :
227 : /* Collect lookup indices for features */
228 :
229 : unsigned int required_feature_index;
230 0 : if (hb_ot_layout_language_get_required_feature_index (face,
231 : table_tag,
232 : script_index[table_index],
233 : language_index[table_index],
234 0 : &required_feature_index))
235 0 : m.add_lookups (face, table_index, required_feature_index, 1);
236 :
237 0 : unsigned int pause_index = 0;
238 0 : unsigned int last_num_lookups = 0;
239 0 : for (unsigned stage = 0; stage < current_stage[table_index]; stage++)
240 : {
241 0 : for (unsigned i = 0; i < m.features.len; i++)
242 0 : if (m.features[i].stage[table_index] == stage)
243 0 : m.add_lookups (face, table_index, m.features[i].index[table_index], m.features[i].mask);
244 :
245 : /* Sort lookups and merge duplicates */
246 0 : if (last_num_lookups < m.lookups[table_index].len)
247 : {
248 0 : m.lookups[table_index].sort (last_num_lookups, m.lookups[table_index].len);
249 :
250 0 : unsigned int j = last_num_lookups;
251 0 : for (unsigned int i = j + 1; i < m.lookups[table_index].len; i++)
252 0 : if (m.lookups[table_index][i].index != m.lookups[table_index][j].index)
253 0 : m.lookups[table_index][++j] = m.lookups[table_index][i];
254 : else
255 0 : m.lookups[table_index][j].mask |= m.lookups[table_index][i].mask;
256 0 : m.lookups[table_index].shrink (j + 1);
257 : }
258 :
259 0 : last_num_lookups = m.lookups[table_index].len;
260 :
261 0 : if (pause_index < pauses[table_index].len && pauses[table_index][pause_index].stage == stage) {
262 0 : hb_ot_map_t::pause_map_t *pause_map = m.pauses[table_index].push ();
263 0 : if (likely (pause_map)) {
264 0 : pause_map->num_lookups = last_num_lookups;
265 0 : pause_map->callback = pauses[table_index][pause_index].callback;
266 : }
267 :
268 0 : pause_index++;
269 : }
270 : }
271 : }
272 : }
273 :
274 :
|