2 * Copyright (C) 2009,2010 Red Hat, Inc.
3 * Copyright (C) 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 #ifndef HB_OT_MAP_PRIVATE_HH
30 #define HB_OT_MAP_PRIVATE_HH
32 #include "hb-ot-shape-private.hh"
34 #include "hb-ot-layout.h"
39 #define MAX_FEATURES 100 /* FIXME */
40 #define MAX_LOOKUPS 1000 /* FIXME */
42 static const hb_tag_t table_tags[2] = {HB_OT_TAG_GSUB, HB_OT_TAG_GPOS};
44 struct hb_mask_allocator_t {
46 struct feature_info_t {
53 cmp (const void *p1, const void *p2)
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);
59 return a->tag < b->tag ? -1 : 1;
61 return a->seq < b->seq ? -1 : 1;
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 */
72 cmp (const void *p1, const void *p2)
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);
77 return a->tag < b->tag ? -1 : a->tag > b->tag ? 1 : 0;
86 cmp (const void *p1, const void *p2)
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);
91 return a->index < b->index ? -1 : a->index > b->index ? 1 : 0;
97 add_lookups (hb_ot_shape_context_t *c,
98 unsigned int table_index,
99 unsigned int feature_index,
102 unsigned int i = MAX_LOOKUPS - lookup_count[table_index];
103 lookup_map_t *lookups = lookup_maps[table_index] + lookup_count[table_index];
105 unsigned int *lookup_indices = (unsigned int *) lookups;
107 hb_ot_layout_feature_get_lookup_indexes (c->face,
108 table_tags[table_index],
113 lookup_count[table_index] += i;
116 lookups[i].mask = mask;
117 lookups[i].index = lookup_indices[i];
124 hb_mask_allocator_t (void) : feature_count (0) {}
126 void add_feature (hb_tag_t tag,
130 feature_info_t *info = &feature_infos[feature_count++];
133 info->seq = feature_count;
134 info->global = global;
137 void compile (hb_ot_shape_context_t *c)
140 lookup_count[0] = lookup_count[1] = 0;
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. */
149 const hb_tag_t *script_tags;
150 hb_tag_t language_tag;
152 script_tags = hb_ot_tags_from_script (c->buffer->props.script);
153 language_tag = hb_ot_tag_from_language (c->buffer->props.language);
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]);
163 /* Sort the features so we can bsearch later */
164 qsort (feature_infos, feature_count, sizeof (feature_infos[0]), feature_info_t::cmp);
166 /* Remove dups, let later-occurring features override earlier ones. */
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];
172 if (feature_infos[i].global)
173 feature_infos[j] = feature_infos[i];
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);
179 feature_count = j + 1;
182 /* Allocate bits now */
183 unsigned int next_bit = 1;
185 for (unsigned int i = 0; i < feature_count; i++) {
186 const feature_info_t *info = &feature_infos[i];
188 unsigned int bits_needed;
190 if (info->global && info->value == 1)
191 /* Uses the global bit */
194 bits_needed = _hb_bit_storage (info->value);
196 if (!info->value || next_bit + bits_needed > 8 * sizeof (hb_mask_t))
197 continue; /* Feature disabled, or not enough bits. */
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],
208 &feature_index[table_index]);
213 feature_map_t *map = &feature_maps[j++];
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 */
223 map->shift = next_bit;
224 map->mask = (1 << (next_bit + bits_needed)) - (1 << next_bit);
225 next_bit += bits_needed;
227 global_mask |= map->mask;
234 for (unsigned int table_index = 0; table_index < 2; table_index++) {
235 hb_tag_t table_tag = table_tags[table_index];
237 /* Collect lookup indices for features */
239 unsigned int required_feature_index;
240 if (hb_ot_layout_language_get_required_feature_index (c->face,
242 script_index[table_index],
243 language_index[table_index],
244 &required_feature_index))
245 add_lookups (c, table_index, required_feature_index, 1);
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);
250 /* Sort lookups and merge duplicates */
252 qsort (lookup_maps[table_index], lookup_count[table_index], sizeof (lookup_maps[table_index][0]), lookup_map_t::cmp);
254 if (lookup_count[table_index])
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];
261 lookup_maps[table_index][j].mask |= lookup_maps[table_index][i].mask;
263 lookup_count[table_index] = j;
268 hb_mask_t get_global_mask (void) { return global_mask; }
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);
273 if (shift) *shift = map->shift;
276 if (shift) *shift = 0;
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);
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);
293 hb_mask_t global_mask;
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];
299 lookup_map_t lookup_maps[2][MAX_LOOKUPS]; /* GSUB/GPOS */
300 unsigned int lookup_count[2];
307 #endif /* HB_OT_MAP_PRIVATE_HH */