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