blob: 43856fa37eeb9997d99544776e610567fb852cb1 [file] [log] [blame]
Behdad Esfahbod5a2b0b32010-10-08 20:14:57 -04001/*
Behdad Esfahbod2409d5f2011-04-21 17:14:28 -04002 * Copyright © 2009,2010 Red Hat, Inc.
Behdad Esfahbod06a44e82013-04-21 15:13:08 -04003 * Copyright © 2010,2011,2013 Google, Inc.
Behdad Esfahbod5a2b0b32010-10-08 20:14:57 -04004 *
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
Behdad Esfahbod2265be02013-05-02 14:25:09 -040031#include "hb-ot-layout-private.hh"
32
Behdad Esfahbod5a2b0b32010-10-08 20:14:57 -040033
Behdad Esfahbod5a2b0b32010-10-08 20:14:57 -040034void
Behdad Esfahbod895fb5d2010-10-12 16:00:21 -040035hb_ot_map_t::add_lookups (hb_face_t *face,
Behdad Esfahbodd2ba0162010-10-12 15:35:45 -040036 unsigned int table_index,
37 unsigned int feature_index,
Behdad Esfahbodcfc507c2013-02-14 10:40:12 -050038 hb_mask_t mask,
Behdad Esfahboda8cf7b42013-03-19 05:53:26 -040039 bool auto_zwj)
Behdad Esfahbodd2ba0162010-10-12 15:35:45 -040040{
Behdad Esfahbod265ac612011-05-05 14:38:16 -040041 unsigned int lookup_indices[32];
42 unsigned int offset, len;
Behdad Esfahbodd2ba0162010-10-12 15:35:45 -040043
Behdad Esfahbod265ac612011-05-05 14:38:16 -040044 offset = 0;
45 do {
46 len = ARRAY_LENGTH (lookup_indices);
Behdad Esfahbodf3064102012-11-15 18:39:46 -080047 hb_ot_layout_feature_get_lookups (face,
48 table_tags[table_index],
49 feature_index,
50 offset, &len,
51 lookup_indices);
Behdad Esfahbodd2ba0162010-10-12 15:35:45 -040052
Behdad Esfahbod265ac612011-05-05 14:38:16 -040053 for (unsigned int i = 0; i < len; i++) {
Behdad Esfahbod90645fb2011-05-27 18:13:31 -040054 hb_ot_map_t::lookup_map_t *lookup = lookups[table_index].push ();
Behdad Esfahbod265ac612011-05-05 14:38:16 -040055 if (unlikely (!lookup))
56 return;
57 lookup->mask = mask;
58 lookup->index = lookup_indices[i];
Behdad Esfahboda8cf7b42013-03-19 05:53:26 -040059 lookup->auto_zwj = auto_zwj;
Behdad Esfahbod265ac612011-05-05 14:38:16 -040060 }
Behdad Esfahbodd2ba0162010-10-12 15:35:45 -040061
Behdad Esfahbod265ac612011-05-05 14:38:16 -040062 offset += len;
63 } while (len == ARRAY_LENGTH (lookup_indices));
Behdad Esfahbodd2ba0162010-10-12 15:35:45 -040064}
65
Behdad Esfahbod6fddf2d2012-11-12 17:57:24 -080066hb_ot_map_builder_t::hb_ot_map_builder_t (hb_face_t *face_,
67 const hb_segment_properties_t *props_)
68{
69 memset (this, 0, sizeof (*this));
70
71 face = face_;
72 props = *props_;
73
74
75 /* Fetch script/language indices for GSUB/GPOS. We need these later to skip
76 * features not available in either table and not waste precious bits for them. */
77
78 hb_tag_t script_tags[3] = {HB_TAG_NONE, HB_TAG_NONE, HB_TAG_NONE};
79 hb_tag_t language_tag;
80
81 hb_ot_tags_from_script (props.script, &script_tags[0], &script_tags[1]);
82 language_tag = hb_ot_tag_from_language (props.language);
83
84 for (unsigned int table_index = 0; table_index < 2; table_index++) {
85 hb_tag_t table_tag = table_tags[table_index];
Behdad Esfahbod851784f2012-11-14 16:24:05 -080086 found_script[table_index] = hb_ot_layout_table_choose_script (face, table_tag, script_tags, &script_index[table_index], &chosen_script[table_index]);
Behdad Esfahbod6fddf2d2012-11-12 17:57:24 -080087 hb_ot_layout_script_find_language (face, table_tag, script_index[table_index], language_tag, &language_index[table_index]);
88 }
89}
Behdad Esfahbodd2ba0162010-10-12 15:35:45 -040090
Behdad Esfahbodec544862013-02-14 11:25:10 -050091void hb_ot_map_builder_t::add_feature (hb_tag_t tag, unsigned int value,
92 hb_ot_map_feature_flags_t flags)
Behdad Esfahbod51881a62011-05-27 18:15:56 -040093{
94 feature_info_t *info = feature_infos.push();
95 if (unlikely (!info)) return;
96 info->tag = tag;
97 info->seq = feature_infos.len;
98 info->max_value = value;
Behdad Esfahbodec544862013-02-14 11:25:10 -050099 info->flags = flags;
100 info->default_value = (flags & F_GLOBAL) ? value : 0;
Behdad Esfahbodb70c96d2011-07-07 21:07:41 -0400101 info->stage[0] = current_stage[0];
102 info->stage[1] = current_stage[1];
103}
104
Behdad Esfahbod3276c352013-05-02 15:16:59 -0400105
Behdad Esfahbodf3064102012-11-15 18:39:46 -0800106void hb_ot_map_t::collect_lookups (unsigned int table_index, hb_set_t *lookups_out) const
Behdad Esfahbodd2984a22012-04-23 17:21:14 -0400107{
Behdad Esfahbodd9b204d2012-08-23 16:22:28 -0400108 for (unsigned int i = 0; i < lookups[table_index].len; i++)
Behdad Esfahbodf3064102012-11-15 18:39:46 -0800109 hb_set_add (lookups_out, lookups[table_index][i].index);
Behdad Esfahbodd2984a22012-04-23 17:21:14 -0400110}
Behdad Esfahbodb70c96d2011-07-07 21:07:41 -0400111
Behdad Esfahbod3e38c0f2012-08-02 09:44:18 -0400112void hb_ot_map_builder_t::add_pause (unsigned int table_index, hb_ot_map_t::pause_func_t pause_func)
Behdad Esfahbodb70c96d2011-07-07 21:07:41 -0400113{
Behdad Esfahbod8ac3c9c2013-04-21 15:19:38 -0400114 stage_info_t *s = stages[table_index].push ();
115 if (likely (s)) {
116 s->index = current_stage[table_index];
Behdad Esfahbod893f57b2013-04-21 15:21:49 -0400117 s->pause_func = pause_func;
Behdad Esfahbodb70c96d2011-07-07 21:07:41 -0400118 }
119
120 current_stage[table_index]++;
Behdad Esfahbod51881a62011-05-27 18:15:56 -0400121}
122
Behdad Esfahbodd2ba0162010-10-12 15:35:45 -0400123void
Behdad Esfahbod6fddf2d2012-11-12 17:57:24 -0800124hb_ot_map_builder_t::compile (hb_ot_map_t &m)
Behdad Esfahbod5a2b0b32010-10-08 20:14:57 -0400125{
Behdad Esfahbod6fddf2d2012-11-12 17:57:24 -0800126 m.global_mask = 1;
127
Behdad Esfahbod851784f2012-11-14 16:24:05 -0800128 for (unsigned int table_index = 0; table_index < 2; table_index++) {
Behdad Esfahbod6fddf2d2012-11-12 17:57:24 -0800129 m.chosen_script[table_index] = chosen_script[table_index];
Behdad Esfahbod851784f2012-11-14 16:24:05 -0800130 m.found_script[table_index] = found_script[table_index];
131 }
Behdad Esfahbod5a2b0b32010-10-08 20:14:57 -0400132
Behdad Esfahbod44b0a4d2011-05-05 13:42:19 -0400133 if (!feature_infos.len)
Behdad Esfahbod5a2b0b32010-10-08 20:14:57 -0400134 return;
135
Behdad Esfahbod5a2b0b32010-10-08 20:14:57 -0400136 /* Sort features and merge duplicates */
Behdad Esfahbod31f18ab2011-06-15 09:49:58 -0400137 {
138 feature_infos.sort ();
139 unsigned int j = 0;
140 for (unsigned int i = 1; i < feature_infos.len; i++)
141 if (feature_infos[i].tag != feature_infos[j].tag)
142 feature_infos[++j] = feature_infos[i];
Behdad Esfahbod5a2b0b32010-10-08 20:14:57 -0400143 else {
Behdad Esfahbodec544862013-02-14 11:25:10 -0500144 if (feature_infos[i].flags & F_GLOBAL) {
145 feature_infos[j].flags |= F_GLOBAL;
Behdad Esfahbodb70c96d2011-07-07 21:07:41 -0400146 feature_infos[j].max_value = feature_infos[i].max_value;
147 feature_infos[j].default_value = feature_infos[i].default_value;
148 } else {
Behdad Esfahbodec544862013-02-14 11:25:10 -0500149 feature_infos[j].flags &= ~F_GLOBAL;
Behdad Esfahbod31f18ab2011-06-15 09:49:58 -0400150 feature_infos[j].max_value = MAX (feature_infos[j].max_value, feature_infos[i].max_value);
Behdad Esfahbod398238a2013-02-15 07:40:10 -0500151 /* Inherit default_value from j */
Behdad Esfahbod31f18ab2011-06-15 09:49:58 -0400152 }
Behdad Esfahbodec544862013-02-14 11:25:10 -0500153 feature_infos[j].flags |= (feature_infos[i].flags & F_HAS_FALLBACK);
Behdad Esfahbodb70c96d2011-07-07 21:07:41 -0400154 feature_infos[j].stage[0] = MIN (feature_infos[j].stage[0], feature_infos[i].stage[0]);
155 feature_infos[j].stage[1] = MIN (feature_infos[j].stage[1], feature_infos[i].stage[1]);
Behdad Esfahbod5a2b0b32010-10-08 20:14:57 -0400156 }
Behdad Esfahbod31f18ab2011-06-15 09:49:58 -0400157 feature_infos.shrink (j + 1);
158 }
Behdad Esfahbod5a2b0b32010-10-08 20:14:57 -0400159
160
161 /* Allocate bits now */
162 unsigned int next_bit = 1;
Behdad Esfahbod68435692011-05-05 14:12:37 -0400163 for (unsigned int i = 0; i < feature_infos.len; i++) {
Behdad Esfahbod5a2b0b32010-10-08 20:14:57 -0400164 const feature_info_t *info = &feature_infos[i];
165
166 unsigned int bits_needed;
167
Behdad Esfahbodec544862013-02-14 11:25:10 -0500168 if ((info->flags & F_GLOBAL) && info->max_value == 1)
Behdad Esfahbod5a2b0b32010-10-08 20:14:57 -0400169 /* Uses the global bit */
170 bits_needed = 0;
171 else
Behdad Esfahbod852912f2010-10-13 15:34:50 -0400172 bits_needed = _hb_bit_storage (info->max_value);
Behdad Esfahbod5a2b0b32010-10-08 20:14:57 -0400173
Behdad Esfahbod852912f2010-10-13 15:34:50 -0400174 if (!info->max_value || next_bit + bits_needed > 8 * sizeof (hb_mask_t))
Behdad Esfahbod5a2b0b32010-10-08 20:14:57 -0400175 continue; /* Feature disabled, or not enough bits. */
176
177
178 bool found = false;
179 unsigned int feature_index[2];
180 for (unsigned int table_index = 0; table_index < 2; table_index++)
Behdad Esfahbod895fb5d2010-10-12 16:00:21 -0400181 found |= hb_ot_layout_language_find_feature (face,
Behdad Esfahbod5a2b0b32010-10-08 20:14:57 -0400182 table_tags[table_index],
183 script_index[table_index],
184 language_index[table_index],
185 info->tag,
186 &feature_index[table_index]);
Behdad Esfahbodec544862013-02-14 11:25:10 -0500187 if (!found && !(info->flags & F_HAS_FALLBACK))
Behdad Esfahbod5a2b0b32010-10-08 20:14:57 -0400188 continue;
189
190
Behdad Esfahbod90645fb2011-05-27 18:13:31 -0400191 hb_ot_map_t::feature_map_t *map = m.features.push ();
Behdad Esfahbod68435692011-05-05 14:12:37 -0400192 if (unlikely (!map))
193 break;
Behdad Esfahbod5a2b0b32010-10-08 20:14:57 -0400194
195 map->tag = info->tag;
196 map->index[0] = feature_index[0];
197 map->index[1] = feature_index[1];
Behdad Esfahbodb70c96d2011-07-07 21:07:41 -0400198 map->stage[0] = info->stage[0];
199 map->stage[1] = info->stage[1];
Behdad Esfahboda8cf7b42013-03-19 05:53:26 -0400200 map->auto_zwj = !(info->flags & F_MANUAL_ZWJ);
Behdad Esfahbodec544862013-02-14 11:25:10 -0500201 if ((info->flags & F_GLOBAL) && info->max_value == 1) {
Behdad Esfahbod5a2b0b32010-10-08 20:14:57 -0400202 /* Uses the global bit */
203 map->shift = 0;
204 map->mask = 1;
205 } else {
206 map->shift = next_bit;
207 map->mask = (1 << (next_bit + bits_needed)) - (1 << next_bit);
208 next_bit += bits_needed;
Behdad Esfahbod398238a2013-02-15 07:40:10 -0500209 m.global_mask |= (info->default_value << map->shift) & map->mask;
Behdad Esfahbod5a2b0b32010-10-08 20:14:57 -0400210 }
Behdad Esfahbod39dede92010-10-13 15:54:06 -0400211 map->_1_mask = (1 << map->shift) & map->mask;
Behdad Esfahbodfabd3112012-09-05 22:19:28 -0400212 map->needs_fallback = !found;
Behdad Esfahbod5a2b0b32010-10-08 20:14:57 -0400213
214 }
Behdad Esfahbod68435692011-05-05 14:12:37 -0400215 feature_infos.shrink (0); /* Done with these */
Behdad Esfahbod5a2b0b32010-10-08 20:14:57 -0400216
217
Behdad Esfahbod3e38c0f2012-08-02 09:44:18 -0400218 add_gsub_pause (NULL);
219 add_gpos_pause (NULL);
Behdad Esfahbodb70c96d2011-07-07 21:07:41 -0400220
Behdad Esfahbod5a2b0b32010-10-08 20:14:57 -0400221 for (unsigned int table_index = 0; table_index < 2; table_index++) {
222 hb_tag_t table_tag = table_tags[table_index];
223
224 /* Collect lookup indices for features */
225
226 unsigned int required_feature_index;
Behdad Esfahbod895fb5d2010-10-12 16:00:21 -0400227 if (hb_ot_layout_language_get_required_feature_index (face,
Behdad Esfahbod5a2b0b32010-10-08 20:14:57 -0400228 table_tag,
229 script_index[table_index],
230 language_index[table_index],
231 &required_feature_index))
Behdad Esfahbodcfc507c2013-02-14 10:40:12 -0500232 m.add_lookups (face, table_index, required_feature_index, 1, true);
Behdad Esfahbod5a2b0b32010-10-08 20:14:57 -0400233
Behdad Esfahbod8ac3c9c2013-04-21 15:19:38 -0400234 unsigned int stage_index = 0;
Behdad Esfahbodb70c96d2011-07-07 21:07:41 -0400235 unsigned int last_num_lookups = 0;
236 for (unsigned stage = 0; stage < current_stage[table_index]; stage++)
Behdad Esfahbod5a2b0b32010-10-08 20:14:57 -0400237 {
Behdad Esfahbodb70c96d2011-07-07 21:07:41 -0400238 for (unsigned i = 0; i < m.features.len; i++)
239 if (m.features[i].stage[table_index] == stage)
Behdad Esfahbodcfc507c2013-02-14 10:40:12 -0500240 m.add_lookups (face, table_index,
241 m.features[i].index[table_index],
242 m.features[i].mask,
Behdad Esfahboda8cf7b42013-03-19 05:53:26 -0400243 m.features[i].auto_zwj);
Behdad Esfahbodb70c96d2011-07-07 21:07:41 -0400244
245 /* Sort lookups and merge duplicates */
246 if (last_num_lookups < m.lookups[table_index].len)
247 {
248 m.lookups[table_index].sort (last_num_lookups, m.lookups[table_index].len);
249
250 unsigned int j = last_num_lookups;
251 for (unsigned int i = j + 1; i < m.lookups[table_index].len; i++)
252 if (m.lookups[table_index][i].index != m.lookups[table_index][j].index)
253 m.lookups[table_index][++j] = m.lookups[table_index][i];
254 else
Behdad Esfahbodcfc507c2013-02-14 10:40:12 -0500255 {
Behdad Esfahbodb70c96d2011-07-07 21:07:41 -0400256 m.lookups[table_index][j].mask |= m.lookups[table_index][i].mask;
Behdad Esfahboda8cf7b42013-03-19 05:53:26 -0400257 m.lookups[table_index][j].auto_zwj &= m.lookups[table_index][i].auto_zwj;
Behdad Esfahbodcfc507c2013-02-14 10:40:12 -0500258 }
Behdad Esfahbodb70c96d2011-07-07 21:07:41 -0400259 m.lookups[table_index].shrink (j + 1);
260 }
261
262 last_num_lookups = m.lookups[table_index].len;
263
Behdad Esfahbod8ac3c9c2013-04-21 15:19:38 -0400264 if (stage_index < stages[table_index].len && stages[table_index][stage_index].index == stage) {
Behdad Esfahbod893f57b2013-04-21 15:21:49 -0400265 hb_ot_map_t::stage_map_t *stage_map = m.stages[table_index].push ();
266 if (likely (stage_map)) {
267 stage_map->last_lookup = last_num_lookups;
268 stage_map->pause_func = stages[table_index][stage_index].pause_func;
Behdad Esfahbodb70c96d2011-07-07 21:07:41 -0400269 }
270
Behdad Esfahbod8ac3c9c2013-04-21 15:19:38 -0400271 stage_index++;
Behdad Esfahbodb70c96d2011-07-07 21:07:41 -0400272 }
Behdad Esfahbod5a2b0b32010-10-08 20:14:57 -0400273 }
274 }
275}