2 * Copyright © 2009,2010 Red Hat, Inc.
3 * Copyright © 2010 Google, Inc.
5 * This is part of HarfBuzz, a text shaping library.
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.
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
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.
25 * Red Hat Author(s): Behdad Esfahbod
26 * Google Author(s): Behdad Esfahbod
29 #include "hb-ot-map-private.hh"
31 #include "hb-ot-shape-private.hh"
37 hb_ot_map_t::add_lookups (hb_face_t *face,
38 unsigned int table_index,
39 unsigned int feature_index,
42 unsigned int lookup_indices[32];
43 unsigned int offset, len;
47 len = ARRAY_LENGTH (lookup_indices);
48 hb_ot_layout_feature_get_lookup_indexes (face,
49 table_tags[table_index],
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))
59 lookup->index = lookup_indices[i];
63 } while (len == ARRAY_LENGTH (lookup_indices));
67 void hb_ot_map_builder_t::add_feature (hb_tag_t tag, unsigned int value, bool global)
69 feature_info_t *info = feature_infos.push();
70 if (unlikely (!info)) return;
72 info->seq = feature_infos.len;
73 info->max_value = value;
74 info->global = global;
75 info->default_value = global ? value : 0;
79 hb_ot_map_builder_t::compile (hb_face_t *face,
80 const hb_segment_properties_t *props,
85 if (!feature_infos.len)
89 /* Fetch script/language indices for GSUB/GPOS. We need these later to skip
90 * features not available in either table and not waste precious bits for them. */
92 hb_tag_t script_tags[3] = {HB_TAG_NONE};
93 hb_tag_t language_tag;
95 hb_ot_tags_from_script (props->script, &script_tags[0], &script_tags[1]);
96 language_tag = hb_ot_tag_from_language (props->language);
98 unsigned int script_index[2], language_index[2];
99 for (unsigned int table_index = 0; table_index < 2; table_index++) {
100 hb_tag_t table_tag = table_tags[table_index];
101 hb_ot_layout_table_choose_script (face, table_tag, script_tags, &script_index[table_index]);
102 hb_ot_layout_script_find_language (face, table_tag, script_index[table_index], language_tag, &language_index[table_index]);
106 /* Sort features and merge duplicates */
108 feature_infos.sort ();
110 for (unsigned int i = 1; i < feature_infos.len; i++)
111 if (feature_infos[i].tag != feature_infos[j].tag)
112 feature_infos[++j] = feature_infos[i];
114 if (feature_infos[i].global)
115 feature_infos[j] = feature_infos[i];
117 feature_infos[j].global = false;
118 feature_infos[j].max_value = MAX (feature_infos[j].max_value, feature_infos[i].max_value);
119 /* Inherit default_value from j */
122 feature_infos.shrink (j + 1);
126 /* Allocate bits now */
127 unsigned int next_bit = 1;
128 for (unsigned int i = 0; i < feature_infos.len; i++) {
129 const feature_info_t *info = &feature_infos[i];
131 unsigned int bits_needed;
133 if (info->global && info->max_value == 1)
134 /* Uses the global bit */
137 bits_needed = _hb_bit_storage (info->max_value);
139 if (!info->max_value || next_bit + bits_needed > 8 * sizeof (hb_mask_t))
140 continue; /* Feature disabled, or not enough bits. */
144 unsigned int feature_index[2];
145 for (unsigned int table_index = 0; table_index < 2; table_index++)
146 found |= hb_ot_layout_language_find_feature (face,
147 table_tags[table_index],
148 script_index[table_index],
149 language_index[table_index],
151 &feature_index[table_index]);
156 hb_ot_map_t::feature_map_t *map = m.features.push ();
160 map->tag = info->tag;
161 map->index[0] = feature_index[0];
162 map->index[1] = feature_index[1];
163 if (info->global && info->max_value == 1) {
164 /* Uses the global bit */
168 map->shift = next_bit;
169 map->mask = (1 << (next_bit + bits_needed)) - (1 << next_bit);
170 next_bit += bits_needed;
172 m.global_mask |= (info->default_value << map->shift) & map->mask;
174 map->_1_mask = (1 << map->shift) & map->mask;
177 feature_infos.shrink (0); /* Done with these */
180 for (unsigned int table_index = 0; table_index < 2; table_index++) {
181 hb_tag_t table_tag = table_tags[table_index];
183 /* Collect lookup indices for features */
185 unsigned int required_feature_index;
186 if (hb_ot_layout_language_get_required_feature_index (face,
188 script_index[table_index],
189 language_index[table_index],
190 &required_feature_index))
191 m.add_lookups (face, table_index, required_feature_index, 1);
193 for (unsigned i = 0; i < m.features.len; i++)
194 m.add_lookups (face, table_index, m.features[i].index[table_index], m.features[i].mask);
196 /* Sort lookups and merge duplicates */
197 m.lookups[table_index].sort ();
198 if (m.lookups[table_index].len)
201 for (unsigned int i = 1; i < m.lookups[table_index].len; i++)
202 if (m.lookups[table_index][i].index != m.lookups[table_index][j].index)
203 m.lookups[table_index][++j] = m.lookups[table_index][i];
205 m.lookups[table_index][j].mask |= m.lookups[table_index][i].mask;
206 m.lookups[table_index].shrink (j + 1);