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