1 /*
2  * Copyright © 2009,2010  Red Hat, Inc.
3  * Copyright © 2010,2011,2013  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.hh"
30 
31 #ifndef HB_NO_OT_SHAPE
32 
33 #include "hb-ot-map.hh"
34 #include "hb-ot-shape.hh"
35 #include "hb-ot-layout.hh"
36 
37 
collect_lookups(unsigned int table_index,hb_set_t * lookups_out) const38 void hb_ot_map_t::collect_lookups (unsigned int table_index, hb_set_t *lookups_out) const
39 {
40   for (unsigned int i = 0; i < lookups[table_index].length; i++)
41     lookups_out->add (lookups[table_index][i].index);
42 }
43 
44 
hb_ot_map_builder_t(hb_face_t * face_,const hb_segment_properties_t * props_)45 hb_ot_map_builder_t::hb_ot_map_builder_t (hb_face_t *face_,
46 					  const hb_segment_properties_t *props_)
47 {
48   memset (this, 0, sizeof (*this));
49 
50   feature_infos.init ();
51   for (unsigned int table_index = 0; table_index < 2; table_index++)
52     stages[table_index].init ();
53 
54   face = face_;
55   props = *props_;
56 
57 
58   /* Fetch script/language indices for GSUB/GPOS.  We need these later to skip
59    * features not available in either table and not waste precious bits for them. */
60 
61   unsigned int script_count = HB_OT_MAX_TAGS_PER_SCRIPT;
62   unsigned int language_count = HB_OT_MAX_TAGS_PER_LANGUAGE;
63   hb_tag_t script_tags[HB_OT_MAX_TAGS_PER_SCRIPT];
64   hb_tag_t language_tags[HB_OT_MAX_TAGS_PER_LANGUAGE];
65 
66   hb_ot_tags_from_script_and_language (props.script, props.language, &script_count, script_tags, &language_count, language_tags);
67 
68   for (unsigned int table_index = 0; table_index < 2; table_index++) {
69     hb_tag_t table_tag = table_tags[table_index];
70     found_script[table_index] = (bool) hb_ot_layout_table_select_script (face, table_tag, script_count, script_tags, &script_index[table_index], &chosen_script[table_index]);
71     hb_ot_layout_script_select_language (face, table_tag, script_index[table_index], language_count, language_tags, &language_index[table_index]);
72   }
73 }
74 
~hb_ot_map_builder_t()75 hb_ot_map_builder_t::~hb_ot_map_builder_t ()
76 {
77   feature_infos.fini ();
78   for (unsigned int table_index = 0; table_index < 2; table_index++)
79     stages[table_index].fini ();
80 }
81 
add_feature(hb_tag_t tag,hb_ot_map_feature_flags_t flags,unsigned int value)82 void hb_ot_map_builder_t::add_feature (hb_tag_t tag,
83 				       hb_ot_map_feature_flags_t flags,
84 				       unsigned int value)
85 {
86   if (unlikely (!tag)) return;
87   feature_info_t *info = feature_infos.push();
88   info->tag = tag;
89   info->seq = feature_infos.length;
90   info->max_value = value;
91   info->flags = flags;
92   info->default_value = (flags & F_GLOBAL) ? value : 0;
93   info->stage[0] = current_stage[0];
94   info->stage[1] = current_stage[1];
95 }
96 
97 void
add_lookups(hb_ot_map_t & m,unsigned int table_index,unsigned int feature_index,unsigned int variations_index,hb_mask_t mask,bool auto_zwnj,bool auto_zwj,bool random)98 hb_ot_map_builder_t::add_lookups (hb_ot_map_t  &m,
99 				  unsigned int  table_index,
100 				  unsigned int  feature_index,
101 				  unsigned int  variations_index,
102 				  hb_mask_t     mask,
103 				  bool          auto_zwnj,
104 				  bool          auto_zwj,
105 				  bool          random)
106 {
107   unsigned int lookup_indices[32];
108   unsigned int offset, len;
109   unsigned int table_lookup_count;
110 
111   table_lookup_count = hb_ot_layout_table_get_lookup_count (face, table_tags[table_index]);
112 
113   offset = 0;
114   do {
115     len = ARRAY_LENGTH (lookup_indices);
116     hb_ot_layout_feature_with_variations_get_lookups (face,
117 						      table_tags[table_index],
118 						      feature_index,
119 						      variations_index,
120 						      offset, &len,
121 						      lookup_indices);
122 
123     for (unsigned int i = 0; i < len; i++)
124     {
125       if (lookup_indices[i] >= table_lookup_count)
126 	continue;
127       hb_ot_map_t::lookup_map_t *lookup = m.lookups[table_index].push ();
128       lookup->mask = mask;
129       lookup->index = lookup_indices[i];
130       lookup->auto_zwnj = auto_zwnj;
131       lookup->auto_zwj = auto_zwj;
132       lookup->random = random;
133     }
134 
135     offset += len;
136   } while (len == ARRAY_LENGTH (lookup_indices));
137 }
138 
139 
add_pause(unsigned int table_index,hb_ot_map_t::pause_func_t pause_func)140 void hb_ot_map_builder_t::add_pause (unsigned int table_index, hb_ot_map_t::pause_func_t pause_func)
141 {
142   stage_info_t *s = stages[table_index].push ();
143   s->index = current_stage[table_index];
144   s->pause_func = pause_func;
145 
146   current_stage[table_index]++;
147 }
148 
149 void
compile(hb_ot_map_t & m,const hb_ot_shape_plan_key_t & key)150 hb_ot_map_builder_t::compile (hb_ot_map_t                  &m,
151 			      const hb_ot_shape_plan_key_t &key)
152 {
153   static_assert ((!(HB_GLYPH_FLAG_DEFINED & (HB_GLYPH_FLAG_DEFINED + 1))), "");
154   unsigned int global_bit_mask = HB_GLYPH_FLAG_DEFINED + 1;
155   unsigned int global_bit_shift = hb_popcount (HB_GLYPH_FLAG_DEFINED);
156 
157   m.global_mask = global_bit_mask;
158 
159   unsigned int required_feature_index[2];
160   hb_tag_t required_feature_tag[2];
161   /* We default to applying required feature in stage 0.  If the required
162    * feature has a tag that is known to the shaper, we apply required feature
163    * in the stage for that tag.
164    */
165   unsigned int required_feature_stage[2] = {0, 0};
166 
167   for (unsigned int table_index = 0; table_index < 2; table_index++)
168   {
169     m.chosen_script[table_index] = chosen_script[table_index];
170     m.found_script[table_index] = found_script[table_index];
171 
172     hb_ot_layout_language_get_required_feature (face,
173 						table_tags[table_index],
174 						script_index[table_index],
175 						language_index[table_index],
176 						&required_feature_index[table_index],
177 						&required_feature_tag[table_index]);
178   }
179 
180   /* Sort features and merge duplicates */
181   if (feature_infos.length)
182   {
183     feature_infos.qsort ();
184     unsigned int j = 0;
185     for (unsigned int i = 1; i < feature_infos.length; i++)
186       if (feature_infos[i].tag != feature_infos[j].tag)
187 	feature_infos[++j] = feature_infos[i];
188       else {
189 	if (feature_infos[i].flags & F_GLOBAL) {
190 	  feature_infos[j].flags |= F_GLOBAL;
191 	  feature_infos[j].max_value = feature_infos[i].max_value;
192 	  feature_infos[j].default_value = feature_infos[i].default_value;
193 	} else {
194 	  if (feature_infos[j].flags & F_GLOBAL)
195 	    feature_infos[j].flags ^= F_GLOBAL;
196 	  feature_infos[j].max_value = hb_max (feature_infos[j].max_value, feature_infos[i].max_value);
197 	  /* Inherit default_value from j */
198 	}
199 	feature_infos[j].flags |= (feature_infos[i].flags & F_HAS_FALLBACK);
200 	feature_infos[j].stage[0] = hb_min (feature_infos[j].stage[0], feature_infos[i].stage[0]);
201 	feature_infos[j].stage[1] = hb_min (feature_infos[j].stage[1], feature_infos[i].stage[1]);
202       }
203     feature_infos.shrink (j + 1);
204   }
205 
206 
207   /* Allocate bits now */
208   unsigned int next_bit = global_bit_shift + 1;
209 
210   for (unsigned int i = 0; i < feature_infos.length; i++)
211   {
212     const feature_info_t *info = &feature_infos[i];
213 
214     unsigned int bits_needed;
215 
216     if ((info->flags & F_GLOBAL) && info->max_value == 1)
217       /* Uses the global bit */
218       bits_needed = 0;
219     else
220       /* Limit bits per feature. */
221       bits_needed = hb_min (HB_OT_MAP_MAX_BITS, hb_bit_storage (info->max_value));
222 
223     if (!info->max_value || next_bit + bits_needed > 8 * sizeof (hb_mask_t))
224       continue; /* Feature disabled, or not enough bits. */
225 
226 
227     bool found = false;
228     unsigned int feature_index[2];
229     for (unsigned int table_index = 0; table_index < 2; table_index++)
230     {
231       if (required_feature_tag[table_index] == info->tag)
232 	required_feature_stage[table_index] = info->stage[table_index];
233 
234       found |= (bool) hb_ot_layout_language_find_feature (face,
235 							  table_tags[table_index],
236 							  script_index[table_index],
237 							  language_index[table_index],
238 							  info->tag,
239 							  &feature_index[table_index]);
240     }
241     if (!found && (info->flags & F_GLOBAL_SEARCH))
242     {
243       for (unsigned int table_index = 0; table_index < 2; table_index++)
244       {
245 	found |= (bool) hb_ot_layout_table_find_feature (face,
246 							 table_tags[table_index],
247 							 info->tag,
248 							 &feature_index[table_index]);
249       }
250     }
251     if (!found && !(info->flags & F_HAS_FALLBACK))
252       continue;
253 
254 
255     hb_ot_map_t::feature_map_t *map = m.features.push ();
256 
257     map->tag = info->tag;
258     map->index[0] = feature_index[0];
259     map->index[1] = feature_index[1];
260     map->stage[0] = info->stage[0];
261     map->stage[1] = info->stage[1];
262     map->auto_zwnj = !(info->flags & F_MANUAL_ZWNJ);
263     map->auto_zwj = !(info->flags & F_MANUAL_ZWJ);
264     map->random = !!(info->flags & F_RANDOM);
265     if ((info->flags & F_GLOBAL) && info->max_value == 1) {
266       /* Uses the global bit */
267       map->shift = global_bit_shift;
268       map->mask = global_bit_mask;
269     } else {
270       map->shift = next_bit;
271       map->mask = (1u << (next_bit + bits_needed)) - (1u << next_bit);
272       next_bit += bits_needed;
273       m.global_mask |= (info->default_value << map->shift) & map->mask;
274     }
275     map->_1_mask = (1u << map->shift) & map->mask;
276     map->needs_fallback = !found;
277 
278   }
279   feature_infos.shrink (0); /* Done with these */
280 
281 
282   add_gsub_pause (nullptr);
283   add_gpos_pause (nullptr);
284 
285   for (unsigned int table_index = 0; table_index < 2; table_index++)
286   {
287     /* Collect lookup indices for features */
288 
289     unsigned int stage_index = 0;
290     unsigned int last_num_lookups = 0;
291     for (unsigned stage = 0; stage < current_stage[table_index]; stage++)
292     {
293       if (required_feature_index[table_index] != HB_OT_LAYOUT_NO_FEATURE_INDEX &&
294 	  required_feature_stage[table_index] == stage)
295 	add_lookups (m, table_index,
296 		     required_feature_index[table_index],
297 		     key.variations_index[table_index],
298 		     global_bit_mask);
299 
300       for (unsigned i = 0; i < m.features.length; i++)
301 	if (m.features[i].stage[table_index] == stage)
302 	  add_lookups (m, table_index,
303 		       m.features[i].index[table_index],
304 		       key.variations_index[table_index],
305 		       m.features[i].mask,
306 		       m.features[i].auto_zwnj,
307 		       m.features[i].auto_zwj,
308 		       m.features[i].random);
309 
310       /* Sort lookups and merge duplicates */
311       if (last_num_lookups < m.lookups[table_index].length)
312       {
313 	m.lookups[table_index].qsort (last_num_lookups, m.lookups[table_index].length);
314 
315 	unsigned int j = last_num_lookups;
316 	for (unsigned int i = j + 1; i < m.lookups[table_index].length; i++)
317 	  if (m.lookups[table_index][i].index != m.lookups[table_index][j].index)
318 	    m.lookups[table_index][++j] = m.lookups[table_index][i];
319 	  else
320 	  {
321 	    m.lookups[table_index][j].mask |= m.lookups[table_index][i].mask;
322 	    m.lookups[table_index][j].auto_zwnj &= m.lookups[table_index][i].auto_zwnj;
323 	    m.lookups[table_index][j].auto_zwj &= m.lookups[table_index][i].auto_zwj;
324 	  }
325 	m.lookups[table_index].shrink (j + 1);
326       }
327 
328       last_num_lookups = m.lookups[table_index].length;
329 
330       if (stage_index < stages[table_index].length && stages[table_index][stage_index].index == stage) {
331 	hb_ot_map_t::stage_map_t *stage_map = m.stages[table_index].push ();
332 	stage_map->last_lookup = last_num_lookups;
333 	stage_map->pause_func = stages[table_index][stage_index].pause_func;
334 
335 	stage_index++;
336       }
337     }
338   }
339 }
340 
341 
342 #endif
343