Update Copyright headers
[apps/home/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 i = MAX_LOOKUPS - lookup_count[table_index];
43   lookup_map_t *lookups = lookup_maps[table_index] + lookup_count[table_index];
44
45   unsigned int *lookup_indices = (unsigned int *) lookups;
46
47   hb_ot_layout_feature_get_lookup_indexes (face,
48                                            table_tags[table_index],
49                                            feature_index,
50                                            0, &i,
51                                            lookup_indices);
52
53   lookup_count[table_index] += i;
54
55   while (i--) {
56     lookups[i].mask = mask;
57     lookups[i].index = lookup_indices[i];
58   }
59 }
60
61
62 void
63 hb_ot_map_t::compile (hb_face_t *face,
64                       const hb_segment_properties_t *props)
65 {
66  global_mask = 1;
67  lookup_count[0] = lookup_count[1] = 0;
68
69   if (!feature_count)
70     return;
71
72
73   /* Fetch script/language indices for GSUB/GPOS.  We need these later to skip
74    * features not available in either table and not waste precious bits for them. */
75
76   hb_tag_t script_tags[3] = {HB_TAG_NONE};
77   hb_tag_t language_tag;
78
79   hb_ot_tags_from_script (props->script, &script_tags[0], &script_tags[1]);
80   language_tag = hb_ot_tag_from_language (props->language);
81
82   unsigned int script_index[2], language_index[2];
83   for (unsigned int table_index = 0; table_index < 2; table_index++) {
84     hb_tag_t table_tag = table_tags[table_index];
85     hb_ot_layout_table_choose_script (face, table_tag, script_tags, &script_index[table_index]);
86     hb_ot_layout_script_find_language (face, table_tag, script_index[table_index], language_tag, &language_index[table_index]);
87   }
88
89
90   /* Sort features and merge duplicates */
91   qsort (feature_infos, feature_count, sizeof (feature_infos[0]), (hb_compare_func_t) feature_info_t::cmp);
92   unsigned int j = 0;
93   for (unsigned int i = 1; i < feature_count; i++)
94     if (feature_infos[i].tag != feature_infos[j].tag)
95       feature_infos[++j] = feature_infos[i];
96     else {
97       if (feature_infos[i].global)
98         feature_infos[j] = feature_infos[i];
99       else {
100         feature_infos[j].global = false;
101         feature_infos[j].max_value = MAX (feature_infos[j].max_value, feature_infos[i].max_value);
102         /* Inherit default_value from j */
103       }
104     }
105   feature_count = j + 1;
106
107
108   /* Allocate bits now */
109   unsigned int next_bit = 1;
110   j = 0;
111   for (unsigned int i = 0; i < feature_count; i++) {
112     const feature_info_t *info = &feature_infos[i];
113
114     unsigned int bits_needed;
115
116     if (info->global && info->max_value == 1)
117       /* Uses the global bit */
118       bits_needed = 0;
119     else
120       bits_needed = _hb_bit_storage (info->max_value);
121
122     if (!info->max_value || next_bit + bits_needed > 8 * sizeof (hb_mask_t))
123       continue; /* Feature disabled, or not enough bits. */
124
125
126     bool found = false;
127     unsigned int feature_index[2];
128     for (unsigned int table_index = 0; table_index < 2; table_index++)
129       found |= hb_ot_layout_language_find_feature (face,
130                                                    table_tags[table_index],
131                                                    script_index[table_index],
132                                                    language_index[table_index],
133                                                    info->tag,
134                                                    &feature_index[table_index]);
135     if (!found)
136       continue;
137
138
139     feature_map_t *map = &feature_maps[j++];
140
141     map->tag = info->tag;
142     map->index[0] = feature_index[0];
143     map->index[1] = feature_index[1];
144     if (info->global && info->max_value == 1) {
145       /* Uses the global bit */
146       map->shift = 0;
147       map->mask = 1;
148     } else {
149       map->shift = next_bit;
150       map->mask = (1 << (next_bit + bits_needed)) - (1 << next_bit);
151       next_bit += bits_needed;
152       if (info->global)
153         global_mask |= (info->default_value << map->shift) & map->mask;
154     }
155     map->_1_mask = (1 << map->shift) & map->mask;
156
157   }
158   feature_count = j;
159
160
161   for (unsigned int table_index = 0; table_index < 2; table_index++) {
162     hb_tag_t table_tag = table_tags[table_index];
163
164     /* Collect lookup indices for features */
165
166     unsigned int required_feature_index;
167     if (hb_ot_layout_language_get_required_feature_index (face,
168                                                           table_tag,
169                                                           script_index[table_index],
170                                                           language_index[table_index],
171                                                           &required_feature_index))
172       add_lookups (face, table_index, required_feature_index, 1);
173
174     for (unsigned i = 0; i < feature_count; i++)
175       add_lookups (face, table_index, feature_maps[i].index[table_index], feature_maps[i].mask);
176
177     /* Sort lookups and merge duplicates */
178     qsort (lookup_maps[table_index], lookup_count[table_index], sizeof (lookup_maps[table_index][0]), (hb_compare_func_t) lookup_map_t::cmp);
179     if (lookup_count[table_index])
180     {
181       unsigned int j = 0;
182       for (unsigned int i = 1; i < lookup_count[table_index]; i++)
183         if (lookup_maps[table_index][i].index != lookup_maps[table_index][j].index)
184           lookup_maps[table_index][++j] = lookup_maps[table_index][i];
185         else
186           lookup_maps[table_index][j].mask |= lookup_maps[table_index][i].mask;
187       j++;
188       lookup_count[table_index] = j;
189     }
190   }
191 }
192
193
194 HB_END_DECLS