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 #include "hb-ot-map-private.hh"
31 #include "hb-ot-shape-private.hh"
37 hb_ot_map_t::add_lookups (hb_ot_shape_plan_context_t *c,
38 unsigned int table_index,
39 unsigned int feature_index,
42 unsigned int i = MAX_LOOKUPS - lookup_count[table_index];
43 lookup_map_t *lookups = lookup_maps[table_index] + lookup_count[table_index];
45 unsigned int *lookup_indices = (unsigned int *) lookups;
47 hb_ot_layout_feature_get_lookup_indexes (c->face,
48 table_tags[table_index],
53 lookup_count[table_index] += i;
56 lookups[i].mask = mask;
57 lookups[i].index = lookup_indices[i];
63 hb_ot_map_t::compile (hb_ot_shape_plan_context_t *c)
66 lookup_count[0] = lookup_count[1] = 0;
72 /* Fetch script/language indices for GSUB/GPOS. We need these later to skip
73 * features not available in either table and not waste precious bits for them. */
75 const hb_tag_t *script_tags;
76 hb_tag_t language_tag;
78 script_tags = hb_ot_tags_from_script (c->props->script);
79 language_tag = hb_ot_tag_from_language (c->props->language);
81 unsigned int script_index[2], language_index[2];
82 for (unsigned int table_index = 0; table_index < 2; table_index++) {
83 hb_tag_t table_tag = table_tags[table_index];
84 hb_ot_layout_table_choose_script (c->face, table_tag, script_tags, &script_index[table_index]);
85 hb_ot_layout_script_find_language (c->face, table_tag, script_index[table_index], language_tag, &language_index[table_index]);
89 /* Sort features and merge duplicates */
90 qsort (feature_infos, feature_count, sizeof (feature_infos[0]), (hb_compare_func_t) feature_info_t::cmp);
92 for (unsigned int i = 1; i < feature_count; i++)
93 if (feature_infos[i].tag != feature_infos[j].tag)
94 feature_infos[++j] = feature_infos[i];
96 if (feature_infos[i].global)
97 feature_infos[j] = feature_infos[i];
99 feature_infos[j].global = false;
100 feature_infos[j].value = MAX (feature_infos[j].value, feature_infos[i].value);
103 feature_count = j + 1;
106 /* Allocate bits now */
107 unsigned int next_bit = 1;
109 for (unsigned int i = 0; i < feature_count; i++) {
110 const feature_info_t *info = &feature_infos[i];
112 unsigned int bits_needed;
114 if (info->global && info->value == 1)
115 /* Uses the global bit */
118 bits_needed = _hb_bit_storage (info->value);
120 if (!info->value || next_bit + bits_needed > 8 * sizeof (hb_mask_t))
121 continue; /* Feature disabled, or not enough bits. */
125 unsigned int feature_index[2];
126 for (unsigned int table_index = 0; table_index < 2; table_index++)
127 found |= hb_ot_layout_language_find_feature (c->face,
128 table_tags[table_index],
129 script_index[table_index],
130 language_index[table_index],
132 &feature_index[table_index]);
137 feature_map_t *map = &feature_maps[j++];
139 map->tag = info->tag;
140 map->index[0] = feature_index[0];
141 map->index[1] = feature_index[1];
142 if (info->global && info->value == 1) {
143 /* Uses the global bit */
147 map->shift = next_bit;
148 map->mask = (1 << (next_bit + bits_needed)) - (1 << next_bit);
149 next_bit += bits_needed;
151 global_mask |= map->mask;
158 for (unsigned int table_index = 0; table_index < 2; table_index++) {
159 hb_tag_t table_tag = table_tags[table_index];
161 /* Collect lookup indices for features */
163 unsigned int required_feature_index;
164 if (hb_ot_layout_language_get_required_feature_index (c->face,
166 script_index[table_index],
167 language_index[table_index],
168 &required_feature_index))
169 add_lookups (c, table_index, required_feature_index, 1);
171 for (unsigned i = 0; i < feature_count; i++)
172 add_lookups (c, table_index, feature_maps[i].index[table_index], feature_maps[i].mask);
174 /* Sort lookups and merge duplicates */
175 qsort (lookup_maps[table_index], lookup_count[table_index], sizeof (lookup_maps[table_index][0]), (hb_compare_func_t) lookup_map_t::cmp);
176 if (lookup_count[table_index])
179 for (unsigned int i = 1; i < lookup_count[table_index]; i++)
180 if (lookup_maps[table_index][i].index != lookup_maps[table_index][j].index)
181 lookup_maps[table_index][++j] = lookup_maps[table_index][i];
183 lookup_maps[table_index][j].mask |= lookup_maps[table_index][i].mask;
185 lookup_count[table_index] = j;