786a0d033faa1da93b2db47f61cf3ca0f437d04c
[framework/uifw/harfbuzz.git] / src / hb-ot-map-private.hh
1 /*
2  * Copyright (C) 2009,2010  Red Hat, Inc.
3  * Copyright (C) 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 #ifndef HB_OT_MAP_PRIVATE_HH
30 #define HB_OT_MAP_PRIVATE_HH
31
32 #include "hb-ot-shape-private.hh"
33
34 #include "hb-ot-layout.h"
35
36 HB_BEGIN_DECLS
37
38
39 #define MAX_FEATURES 100 /* FIXME */
40 #define MAX_LOOKUPS 1000 /* FIXME */
41
42 static const hb_tag_t table_tags[2] = {HB_OT_TAG_GSUB, HB_OT_TAG_GPOS};
43
44 struct hb_mask_allocator_t {
45
46   struct feature_info_t {
47     hb_tag_t tag;
48     unsigned int value;
49     unsigned int seq;
50     bool global;
51
52     static int
53     cmp (const void *p1, const void *p2)
54     {
55       const feature_info_t *a = reinterpret_cast<const feature_info_t *>(p1);
56       const feature_info_t *b = reinterpret_cast<const feature_info_t *>(p2);
57
58       if (a->tag != b->tag)
59         return a->tag < b->tag ? -1 : 1;
60
61       return a->seq < b->seq ? -1 : 1;
62     }
63   };
64
65   struct feature_map_t {
66     hb_tag_t tag; /* should be first for our bsearch to work */
67     unsigned int index[2]; /* GSUB, GPOS */
68     unsigned int shift;
69     hb_mask_t mask;
70
71     static int
72     cmp (const void *p1, const void *p2)
73     {
74       const feature_map_t *a = reinterpret_cast<const feature_map_t *>(p1);
75       const feature_map_t *b = reinterpret_cast<const feature_map_t *>(p2);
76
77       return a->tag < b->tag ? -1 : a->tag > b->tag ? 1 : 0;
78     }
79   };
80
81   struct lookup_map_t {
82     unsigned int index;
83     hb_mask_t mask;
84
85     static int
86     cmp (const void *p1, const void *p2)
87     {
88       const lookup_map_t *a = reinterpret_cast<const lookup_map_t *>(p1);
89       const lookup_map_t *b = reinterpret_cast<const lookup_map_t *>(p2);
90
91       return a->index < b->index ? -1 : a->index > b->index ? 1 : 0;
92     }
93   };
94
95
96   void
97   add_lookups (hb_ot_shape_context_t *c,
98                unsigned int  table_index,
99                unsigned int  feature_index,
100                hb_mask_t     mask)
101   {
102     unsigned int i = MAX_LOOKUPS - lookup_count[table_index];
103     lookup_map_t *lookups = lookup_maps[table_index] + lookup_count[table_index];
104
105     unsigned int *lookup_indices = (unsigned int *) lookups;
106
107     hb_ot_layout_feature_get_lookup_indexes (c->face,
108                                              table_tags[table_index],
109                                              feature_index,
110                                              0, &i,
111                                              lookup_indices);
112
113     lookup_count[table_index] += i;
114
115     while (i--) {
116       lookups[i].mask = mask;
117       lookups[i].index = lookup_indices[i];
118     }
119   }
120
121
122
123
124   hb_mask_allocator_t (void) : feature_count (0) {}
125
126   void add_feature (hb_tag_t tag,
127                     unsigned int value,
128                     bool global)
129   {
130     feature_info_t *info = &feature_infos[feature_count++];
131     info->tag = tag;
132     info->value = value;
133     info->seq = feature_count;
134     info->global = global;
135   }
136
137   void compile (hb_ot_shape_context_t *c)
138   {
139    global_mask = 0;
140    lookup_count[0] = lookup_count[1] = 0;
141
142     if (!feature_count)
143       return;
144
145
146     /* Fetch script/language indices for GSUB/GPOS.  We need these later to skip
147      * features not available in either table and not waste precious bits for them. */
148
149     const hb_tag_t *script_tags;
150     hb_tag_t language_tag;
151
152     script_tags = hb_ot_tags_from_script (c->buffer->props.script);
153     language_tag = hb_ot_tag_from_language (c->buffer->props.language);
154
155     unsigned int script_index[2], language_index[2];
156     for (unsigned int table_index = 0; table_index < 2; table_index++) {
157       hb_tag_t table_tag = table_tags[table_index];
158       hb_ot_layout_table_choose_script (c->face, table_tag, script_tags, &script_index[table_index]);
159       hb_ot_layout_script_find_language (c->face, table_tag, script_index[table_index], language_tag, &language_index[table_index]);
160     }
161
162
163     /* Sort the features so we can bsearch later */
164     qsort (feature_infos, feature_count, sizeof (feature_infos[0]), feature_info_t::cmp);
165
166     /* Remove dups, let later-occurring features override earlier ones. */
167     unsigned int j = 0;
168     for (unsigned int i = 1; i < feature_count; i++)
169       if (feature_infos[i].tag != feature_infos[j].tag)
170         feature_infos[++j] = feature_infos[i];
171       else {
172         if (feature_infos[i].global)
173           feature_infos[j] = feature_infos[i];
174         else {
175           feature_infos[j].global = feature_infos[j].global && (feature_infos[j].value == feature_infos[i].value);
176           feature_infos[j].value = MAX (feature_infos[j].value, feature_infos[i].value);
177         }
178       }
179     feature_count = j + 1;
180
181
182     /* Allocate bits now */
183     unsigned int next_bit = 1;
184     j = 0;
185     for (unsigned int i = 0; i < feature_count; i++) {
186       const feature_info_t *info = &feature_infos[i];
187
188       unsigned int bits_needed;
189
190       if (info->global && info->value == 1)
191         /* Uses the global bit */
192         bits_needed = 0;
193       else
194         bits_needed = _hb_bit_storage (info->value);
195
196       if (!info->value || next_bit + bits_needed > 8 * sizeof (hb_mask_t))
197         continue; /* Feature disabled, or not enough bits. */
198
199
200       bool found = false;
201       unsigned int feature_index[2];
202       for (unsigned int table_index = 0; table_index < 2; table_index++)
203         found |= hb_ot_layout_language_find_feature (c->face,
204                                                      table_tags[table_index],
205                                                      script_index[table_index],
206                                                      language_index[table_index],
207                                                      info->tag,
208                                                      &feature_index[table_index]);
209       if (!found)
210         continue;
211
212
213       feature_map_t *map = &feature_maps[j++];
214
215       map->tag = info->tag;
216       map->index[0] = feature_index[0];
217       map->index[1] = feature_index[1];
218       if (info->global && info->value == 1) {
219         /* Uses the global bit */
220         map->shift = 0;
221         map->mask = 1;
222       } else {
223         map->shift = next_bit;
224         map->mask = (1 << (next_bit + bits_needed)) - (1 << next_bit);
225         next_bit += bits_needed;
226         if (info->global)
227           global_mask |= map->mask;
228       }
229
230     }
231     feature_count = j;
232
233
234     for (unsigned int table_index = 0; table_index < 2; table_index++) {
235       hb_tag_t table_tag = table_tags[table_index];
236
237       /* Collect lookup indices for features */
238
239       unsigned int required_feature_index;
240       if (hb_ot_layout_language_get_required_feature_index (c->face,
241                                                             table_tag,
242                                                             script_index[table_index],
243                                                             language_index[table_index],
244                                                             &required_feature_index))
245         add_lookups (c, table_index, required_feature_index, 1);
246
247       for (unsigned i = 0; i < feature_count; i++)
248         add_lookups (c, table_index, feature_maps[i].index[table_index], feature_maps[i].mask);
249
250       /* Sort lookups and merge duplicates */
251
252       qsort (lookup_maps[table_index], lookup_count[table_index], sizeof (lookup_maps[table_index][0]), lookup_map_t::cmp);
253
254       if (lookup_count[table_index])
255       {
256         unsigned int j = 0;
257         for (unsigned int i = 1; i < lookup_count[table_index]; i++)
258           if (lookup_maps[table_index][i].index != lookup_maps[table_index][j].index)
259             lookup_maps[table_index][++j] = lookup_maps[table_index][i];
260           else
261             lookup_maps[table_index][j].mask |= lookup_maps[table_index][i].mask;
262         j++;
263         lookup_count[table_index] = j;
264       }
265     }
266   }
267
268   hb_mask_t get_global_mask (void) { return global_mask; }
269
270   hb_mask_t get_mask (hb_tag_t tag, unsigned int *shift) const {
271     const feature_map_t *map = (const feature_map_t *) bsearch (&tag, feature_maps, feature_count, sizeof (feature_maps[0]), feature_map_t::cmp);
272     if (likely (map)) {
273       if (shift) *shift = map->shift;
274       return map->mask;
275     } else {
276       if (shift) *shift = 0;
277       return 0;
278     }
279   }
280
281   void substitute (hb_ot_shape_context_t *c) const {
282     for (unsigned int i = 0; i < lookup_count[0]; i++)
283       hb_ot_layout_substitute_lookup (c->face, c->buffer, lookup_maps[0][i].index, lookup_maps[0][i].mask);
284   }
285
286   void position (hb_ot_shape_context_t *c) const {
287     for (unsigned int i = 0; i < lookup_count[1]; i++)
288       hb_ot_layout_position_lookup (c->font, c->face, c->buffer, lookup_maps[1][i].index, lookup_maps[1][i].mask);
289   }
290
291   private:
292
293   hb_mask_t global_mask;
294
295   unsigned int feature_count;
296   feature_info_t feature_infos[MAX_FEATURES]; /* used before compile() only */
297   feature_map_t feature_maps[MAX_FEATURES];
298
299   lookup_map_t lookup_maps[2][MAX_LOOKUPS]; /* GSUB/GPOS */
300   unsigned int lookup_count[2];
301 };
302
303
304
305 HB_END_DECLS
306
307 #endif /* HB_OT_MAP_PRIVATE_HH */