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