Add hb-ot-map.cc
[framework/uifw/harfbuzz.git] / src / hb-ot-map.cc
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 #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_mask_allocator_t::compile (hb_ot_shape_context_t *c)
38 {
39  global_mask = 0;
40  lookup_count[0] = lookup_count[1] = 0;
41
42   if (!feature_count)
43     return;
44
45
46   /* Fetch script/language indices for GSUB/GPOS.  We need these later to skip
47    * features not available in either table and not waste precious bits for them. */
48
49   const hb_tag_t *script_tags;
50   hb_tag_t language_tag;
51
52   script_tags = hb_ot_tags_from_script (c->buffer->props.script);
53   language_tag = hb_ot_tag_from_language (c->buffer->props.language);
54
55   unsigned int script_index[2], language_index[2];
56   for (unsigned int table_index = 0; table_index < 2; table_index++) {
57     hb_tag_t table_tag = table_tags[table_index];
58     hb_ot_layout_table_choose_script (c->face, table_tag, script_tags, &script_index[table_index]);
59     hb_ot_layout_script_find_language (c->face, table_tag, script_index[table_index], language_tag, &language_index[table_index]);
60   }
61
62
63   /* Sort features and merge duplicates */
64   qsort (feature_infos, feature_count, sizeof (feature_infos[0]), (hb_compare_func_t) feature_info_t::cmp);
65   unsigned int j = 0;
66   for (unsigned int i = 1; i < feature_count; i++)
67     if (feature_infos[i].tag != feature_infos[j].tag)
68       feature_infos[++j] = feature_infos[i];
69     else {
70       if (feature_infos[i].global)
71         feature_infos[j] = feature_infos[i];
72       else {
73         feature_infos[j].global = false;
74         feature_infos[j].value = MAX (feature_infos[j].value, feature_infos[i].value);
75       }
76     }
77   feature_count = j + 1;
78
79
80   /* Allocate bits now */
81   unsigned int next_bit = 1;
82   j = 0;
83   for (unsigned int i = 0; i < feature_count; i++) {
84     const feature_info_t *info = &feature_infos[i];
85
86     unsigned int bits_needed;
87
88     if (info->global && info->value == 1)
89       /* Uses the global bit */
90       bits_needed = 0;
91     else
92       bits_needed = _hb_bit_storage (info->value);
93
94     if (!info->value || next_bit + bits_needed > 8 * sizeof (hb_mask_t))
95       continue; /* Feature disabled, or not enough bits. */
96
97
98     bool found = false;
99     unsigned int feature_index[2];
100     for (unsigned int table_index = 0; table_index < 2; table_index++)
101       found |= hb_ot_layout_language_find_feature (c->face,
102                                                    table_tags[table_index],
103                                                    script_index[table_index],
104                                                    language_index[table_index],
105                                                    info->tag,
106                                                    &feature_index[table_index]);
107     if (!found)
108       continue;
109
110
111     feature_map_t *map = &feature_maps[j++];
112
113     map->tag = info->tag;
114     map->index[0] = feature_index[0];
115     map->index[1] = feature_index[1];
116     if (info->global && info->value == 1) {
117       /* Uses the global bit */
118       map->shift = 0;
119       map->mask = 1;
120     } else {
121       map->shift = next_bit;
122       map->mask = (1 << (next_bit + bits_needed)) - (1 << next_bit);
123       next_bit += bits_needed;
124       if (info->global)
125         global_mask |= map->mask;
126     }
127
128   }
129   feature_count = j;
130
131
132   for (unsigned int table_index = 0; table_index < 2; table_index++) {
133     hb_tag_t table_tag = table_tags[table_index];
134
135     /* Collect lookup indices for features */
136
137     unsigned int required_feature_index;
138     if (hb_ot_layout_language_get_required_feature_index (c->face,
139                                                           table_tag,
140                                                           script_index[table_index],
141                                                           language_index[table_index],
142                                                           &required_feature_index))
143       add_lookups (c, table_index, required_feature_index, 1);
144
145     for (unsigned i = 0; i < feature_count; i++)
146       add_lookups (c, table_index, feature_maps[i].index[table_index], feature_maps[i].mask);
147
148     /* Sort lookups and merge duplicates */
149     qsort (lookup_maps[table_index], lookup_count[table_index], sizeof (lookup_maps[table_index][0]), (hb_compare_func_t) lookup_map_t::cmp);
150     if (lookup_count[table_index])
151     {
152       unsigned int j = 0;
153       for (unsigned int i = 1; i < lookup_count[table_index]; i++)
154         if (lookup_maps[table_index][i].index != lookup_maps[table_index][j].index)
155           lookup_maps[table_index][++j] = lookup_maps[table_index][i];
156         else
157           lookup_maps[table_index][j].mask |= lookup_maps[table_index][i].mask;
158       j++;
159       lookup_count[table_index] = j;
160     }
161   }
162 }
163
164
165 HB_END_DECLS