• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2009,2010  Red Hat, Inc.
3  * Copyright © 2010,2011,2012  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-shaper-impl.hh"
30 
31 #include "hb-ot-shape.hh"
32 #include "hb-ot-shape-complex.hh"
33 #include "hb-ot-shape-fallback.hh"
34 #include "hb-ot-shape-normalize.hh"
35 
36 #include "hb-ot-face.hh"
37 
38 #include "hb-set.hh"
39 
40 #include "hb-aat-layout.hh"
41 
42 
43 /**
44  * SECTION:hb-ot-shape
45  * @title: hb-ot-shape
46  * @short_description: OpenType shaping support
47  * @include: hb-ot.h
48  *
49  * Support functions for OpenType shaping related queries.
50  **/
51 
52 
53 static void
54 hb_ot_shape_collect_features (hb_ot_shape_planner_t          *planner,
55 			      const hb_feature_t             *user_features,
56 			      unsigned int                    num_user_features);
57 
58 static bool
_hb_apply_morx(hb_face_t * face)59 _hb_apply_morx (hb_face_t *face)
60 {
61   if (hb_options ().aat &&
62       hb_aat_layout_has_substitution (face))
63     return true;
64 
65   /* Ignore empty GSUB tables. */
66   return (!hb_ot_layout_has_substitution (face) ||
67 	  !hb_ot_layout_table_get_script_tags (face,
68 					       HB_OT_TAG_GSUB,
69 					       0, nullptr, nullptr)) &&
70 	 hb_aat_layout_has_substitution (face);
71 }
72 
hb_ot_shape_planner_t(hb_face_t * face,const hb_segment_properties_t * props)73 hb_ot_shape_planner_t::hb_ot_shape_planner_t (hb_face_t                     *face,
74 					      const hb_segment_properties_t *props) :
75 						face (face),
76 						props (*props),
77 						map (face, props),
78 						aat_map (face, props),
79 						apply_morx (_hb_apply_morx (face))
80 {
81   shaper = hb_ot_shape_complex_categorize (this);
82 
83   script_zero_marks = shaper->zero_width_marks != HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE;
84   script_fallback_mark_positioning = shaper->fallback_position;
85 
86   if (apply_morx)
87     shaper = &_hb_ot_complex_shaper_default;
88 }
89 
90 void
compile(hb_ot_shape_plan_t & plan,const hb_ot_shape_plan_key_t & key)91 hb_ot_shape_planner_t::compile (hb_ot_shape_plan_t           &plan,
92 				const hb_ot_shape_plan_key_t &key)
93 {
94   plan.props = props;
95   plan.shaper = shaper;
96   map.compile (plan.map, key);
97   if (apply_morx)
98     aat_map.compile (plan.aat_map);
99 
100   plan.frac_mask = plan.map.get_1_mask (HB_TAG ('f','r','a','c'));
101   plan.numr_mask = plan.map.get_1_mask (HB_TAG ('n','u','m','r'));
102   plan.dnom_mask = plan.map.get_1_mask (HB_TAG ('d','n','o','m'));
103   plan.has_frac = plan.frac_mask || (plan.numr_mask && plan.dnom_mask);
104   plan.rtlm_mask = plan.map.get_1_mask (HB_TAG ('r','t','l','m'));
105   hb_tag_t kern_tag = HB_DIRECTION_IS_HORIZONTAL (props.direction) ?
106 		      HB_TAG ('k','e','r','n') : HB_TAG ('v','k','r','n');
107   plan.kern_mask = plan.map.get_mask (kern_tag);
108   plan.trak_mask = plan.map.get_mask (HB_TAG ('t','r','a','k'));
109 
110   plan.requested_kerning = !!plan.kern_mask;
111   plan.requested_tracking = !!plan.trak_mask;
112   bool has_gpos_kern = plan.map.get_feature_index (1, kern_tag) != HB_OT_LAYOUT_NO_FEATURE_INDEX;
113   bool disable_gpos = plan.shaper->gpos_tag &&
114 		      plan.shaper->gpos_tag != plan.map.chosen_script[1];
115 
116   /*
117    * Decide who provides glyph classes. GDEF or Unicode.
118    */
119 
120   if (!hb_ot_layout_has_glyph_classes (face))
121     plan.fallback_glyph_classes = true;
122 
123   /*
124    * Decide who does substitutions. GSUB, morx, or fallback.
125    */
126 
127   plan.apply_morx = apply_morx;
128 
129   /*
130    * Decide who does positioning. GPOS, kerx, kern, or fallback.
131    */
132 
133   if (hb_options ().aat && hb_aat_layout_has_positioning (face))
134     plan.apply_kerx = true;
135   else if (!apply_morx && !disable_gpos && hb_ot_layout_has_positioning (face))
136     plan.apply_gpos = true;
137   else if (hb_aat_layout_has_positioning (face))
138     plan.apply_kerx = true;
139 
140   if (!plan.apply_kerx && !has_gpos_kern)
141   {
142     /* Apparently Apple applies kerx if GPOS kern was not applied. */
143     if (hb_aat_layout_has_positioning (face))
144       plan.apply_kerx = true;
145     else if (hb_ot_layout_has_kerning (face))
146       plan.apply_kern = true;
147   }
148 
149   plan.zero_marks = script_zero_marks &&
150 		    !plan.apply_kerx &&
151 		    (!plan.apply_kern || !hb_ot_layout_has_machine_kerning (face));
152   plan.has_gpos_mark = !!plan.map.get_1_mask (HB_TAG ('m','a','r','k'));
153 
154   plan.adjust_mark_positioning_when_zeroing = !plan.apply_gpos &&
155 					      !plan.apply_kerx &&
156 					      (!plan.apply_kern || !hb_ot_layout_has_cross_kerning (face));
157 
158   plan.fallback_mark_positioning = plan.adjust_mark_positioning_when_zeroing &&
159 				   script_fallback_mark_positioning;
160 
161   /* Currently we always apply trak. */
162   plan.apply_trak = plan.requested_tracking && hb_aat_layout_has_tracking (face);
163 }
164 
165 bool
init0(hb_face_t * face,const hb_shape_plan_key_t * key)166 hb_ot_shape_plan_t::init0 (hb_face_t                     *face,
167 			   const hb_shape_plan_key_t     *key)
168 {
169   map.init ();
170   aat_map.init ();
171 
172   hb_ot_shape_planner_t planner (face,
173 				 &key->props);
174 
175   hb_ot_shape_collect_features (&planner,
176 				key->user_features,
177 				key->num_user_features);
178 
179   planner.compile (*this, key->ot);
180 
181   if (shaper->data_create)
182   {
183     data = shaper->data_create (this);
184     if (unlikely (!data))
185       return false;
186   }
187 
188   return true;
189 }
190 
191 void
fini()192 hb_ot_shape_plan_t::fini ()
193 {
194   if (shaper->data_destroy)
195     shaper->data_destroy (const_cast<void *> (data));
196 
197   map.fini ();
198   aat_map.fini ();
199 }
200 
201 void
substitute(hb_font_t * font,hb_buffer_t * buffer) const202 hb_ot_shape_plan_t::substitute (hb_font_t   *font,
203 				hb_buffer_t *buffer) const
204 {
205   if (unlikely (apply_morx))
206     hb_aat_layout_substitute (this, font, buffer);
207   else
208     map.substitute (this, font, buffer);
209 }
210 
211 void
position(hb_font_t * font,hb_buffer_t * buffer) const212 hb_ot_shape_plan_t::position (hb_font_t   *font,
213 			      hb_buffer_t *buffer) const
214 {
215   if (this->apply_gpos)
216     map.position (this, font, buffer);
217   else if (this->apply_kerx)
218     hb_aat_layout_position (this, font, buffer);
219   else if (this->apply_kern)
220     hb_ot_layout_kern (this, font, buffer);
221   else
222     _hb_ot_shape_fallback_kern (this, font, buffer);
223 
224   if (this->apply_trak)
225     hb_aat_layout_track (this, font, buffer);
226 }
227 
228 
229 static const hb_ot_map_feature_t
230 common_features[] =
231 {
232   {HB_TAG('c','c','m','p'), F_GLOBAL},
233   {HB_TAG('l','o','c','l'), F_GLOBAL},
234   {HB_TAG('m','a','r','k'), F_GLOBAL_MANUAL_JOINERS},
235   {HB_TAG('m','k','m','k'), F_GLOBAL_MANUAL_JOINERS},
236   {HB_TAG('r','l','i','g'), F_GLOBAL},
237 };
238 
239 
240 static const hb_ot_map_feature_t
241 horizontal_features[] =
242 {
243   {HB_TAG('c','a','l','t'), F_GLOBAL},
244   {HB_TAG('c','l','i','g'), F_GLOBAL},
245   {HB_TAG('c','u','r','s'), F_GLOBAL},
246   {HB_TAG('k','e','r','n'), F_GLOBAL_HAS_FALLBACK},
247   {HB_TAG('l','i','g','a'), F_GLOBAL},
248   {HB_TAG('r','c','l','t'), F_GLOBAL},
249 };
250 
251 static void
hb_ot_shape_collect_features(hb_ot_shape_planner_t * planner,const hb_feature_t * user_features,unsigned int num_user_features)252 hb_ot_shape_collect_features (hb_ot_shape_planner_t          *planner,
253 			      const hb_feature_t             *user_features,
254 			      unsigned int                    num_user_features)
255 {
256   hb_ot_map_builder_t *map = &planner->map;
257 
258   map->enable_feature (HB_TAG('r','v','r','n'));
259   map->add_gsub_pause (nullptr);
260 
261   switch (planner->props.direction) {
262     case HB_DIRECTION_LTR:
263       map->enable_feature (HB_TAG ('l','t','r','a'));
264       map->enable_feature (HB_TAG ('l','t','r','m'));
265       break;
266     case HB_DIRECTION_RTL:
267       map->enable_feature (HB_TAG ('r','t','l','a'));
268       map->add_feature (HB_TAG ('r','t','l','m'));
269       break;
270     case HB_DIRECTION_TTB:
271     case HB_DIRECTION_BTT:
272     case HB_DIRECTION_INVALID:
273     default:
274       break;
275   }
276 
277   /* Automatic fractions. */
278   map->add_feature (HB_TAG ('f','r','a','c'));
279   map->add_feature (HB_TAG ('n','u','m','r'));
280   map->add_feature (HB_TAG ('d','n','o','m'));
281 
282   /* Random! */
283   map->enable_feature (HB_TAG ('r','a','n','d'), F_RANDOM, HB_OT_MAP_MAX_VALUE);
284 
285   /* Tracking.  We enable dummy feature here just to allow disabling
286    * AAT 'trak' table using features.
287    * https://github.com/harfbuzz/harfbuzz/issues/1303 */
288   map->enable_feature (HB_TAG ('t','r','a','k'), F_HAS_FALLBACK);
289 
290   map->enable_feature (HB_TAG ('H','A','R','F'));
291 
292   if (planner->shaper->collect_features)
293     planner->shaper->collect_features (planner);
294 
295   map->enable_feature (HB_TAG ('B','U','Z','Z'));
296 
297   for (unsigned int i = 0; i < ARRAY_LENGTH (common_features); i++)
298     map->add_feature (common_features[i]);
299 
300   if (HB_DIRECTION_IS_HORIZONTAL (planner->props.direction))
301     for (unsigned int i = 0; i < ARRAY_LENGTH (horizontal_features); i++)
302       map->add_feature (horizontal_features[i]);
303   else
304   {
305     /* We really want to find a 'vert' feature if there's any in the font, no
306      * matter which script/langsys it is listed (or not) under.
307      * See various bugs referenced from:
308      * https://github.com/harfbuzz/harfbuzz/issues/63 */
309     map->enable_feature (HB_TAG ('v','e','r','t'), F_GLOBAL_SEARCH);
310   }
311 
312   for (unsigned int i = 0; i < num_user_features; i++)
313   {
314     const hb_feature_t *feature = &user_features[i];
315     map->add_feature (feature->tag,
316 		      (feature->start == HB_FEATURE_GLOBAL_START &&
317 		       feature->end == HB_FEATURE_GLOBAL_END) ?  F_GLOBAL : F_NONE,
318 		      feature->value);
319   }
320 
321   if (planner->apply_morx)
322   {
323     hb_aat_map_builder_t *aat_map = &planner->aat_map;
324     for (unsigned int i = 0; i < num_user_features; i++)
325     {
326       const hb_feature_t *feature = &user_features[i];
327       aat_map->add_feature (feature->tag, feature->value);
328     }
329   }
330 
331   if (planner->shaper->override_features)
332     planner->shaper->override_features (planner);
333 }
334 
335 
336 /*
337  * shaper face data
338  */
339 
340 struct hb_ot_face_data_t {};
341 
342 hb_ot_face_data_t *
_hb_ot_shaper_face_data_create(hb_face_t * face)343 _hb_ot_shaper_face_data_create (hb_face_t *face)
344 {
345   return (hb_ot_face_data_t *) HB_SHAPER_DATA_SUCCEEDED;
346 }
347 
348 void
_hb_ot_shaper_face_data_destroy(hb_ot_face_data_t * data)349 _hb_ot_shaper_face_data_destroy (hb_ot_face_data_t *data)
350 {
351 }
352 
353 
354 /*
355  * shaper font data
356  */
357 
358 struct hb_ot_font_data_t {};
359 
360 hb_ot_font_data_t *
_hb_ot_shaper_font_data_create(hb_font_t * font HB_UNUSED)361 _hb_ot_shaper_font_data_create (hb_font_t *font HB_UNUSED)
362 {
363   return (hb_ot_font_data_t *) HB_SHAPER_DATA_SUCCEEDED;
364 }
365 
366 void
_hb_ot_shaper_font_data_destroy(hb_ot_font_data_t * data HB_UNUSED)367 _hb_ot_shaper_font_data_destroy (hb_ot_font_data_t *data HB_UNUSED)
368 {
369 }
370 
371 
372 /*
373  * shaper
374  */
375 
376 struct hb_ot_shape_context_t
377 {
378   hb_ot_shape_plan_t *plan;
379   hb_font_t *font;
380   hb_face_t *face;
381   hb_buffer_t  *buffer;
382   const hb_feature_t *user_features;
383   unsigned int        num_user_features;
384 
385   /* Transient stuff */
386   hb_direction_t target_direction;
387 };
388 
389 
390 
391 /* Main shaper */
392 
393 
394 /* Prepare */
395 
396 static void
hb_set_unicode_props(hb_buffer_t * buffer)397 hb_set_unicode_props (hb_buffer_t *buffer)
398 {
399   /* Implement enough of Unicode Graphemes here that shaping
400    * in reverse-direction wouldn't break graphemes.  Namely,
401    * we mark all marks and ZWJ and ZWJ,Extended_Pictographic
402    * sequences as continuations.  The foreach_grapheme()
403    * macro uses this bit.
404    *
405    * https://www.unicode.org/reports/tr29/#Regex_Definitions
406    */
407   unsigned int count = buffer->len;
408   hb_glyph_info_t *info = buffer->info;
409   for (unsigned int i = 0; i < count; i++)
410   {
411     _hb_glyph_info_set_unicode_props (&info[i], buffer);
412 
413     /* Marks are already set as continuation by the above line.
414      * Handle Emoji_Modifier and ZWJ-continuation. */
415     if (unlikely (_hb_glyph_info_get_general_category (&info[i]) == HB_UNICODE_GENERAL_CATEGORY_MODIFIER_SYMBOL &&
416 		  hb_in_range<hb_codepoint_t> (info[i].codepoint, 0x1F3FBu, 0x1F3FFu)))
417     {
418 	_hb_glyph_info_set_continuation (&info[i]);
419     }
420     else if (unlikely (_hb_glyph_info_is_zwj (&info[i])))
421     {
422       _hb_glyph_info_set_continuation (&info[i]);
423       if (i + 1 < count &&
424 	  _hb_unicode_is_emoji_Extended_Pictographic (info[i + 1].codepoint))
425       {
426         i++;
427 	_hb_glyph_info_set_unicode_props (&info[i], buffer);
428 	_hb_glyph_info_set_continuation (&info[i]);
429       }
430     }
431   }
432 }
433 
434 static void
hb_insert_dotted_circle(hb_buffer_t * buffer,hb_font_t * font)435 hb_insert_dotted_circle (hb_buffer_t *buffer, hb_font_t *font)
436 {
437   if (!(buffer->flags & HB_BUFFER_FLAG_BOT) ||
438       buffer->context_len[0] ||
439       !_hb_glyph_info_is_unicode_mark (&buffer->info[0]))
440     return;
441 
442   if (!font->has_glyph (0x25CCu))
443     return;
444 
445   hb_glyph_info_t dottedcircle = {0};
446   dottedcircle.codepoint = 0x25CCu;
447   _hb_glyph_info_set_unicode_props (&dottedcircle, buffer);
448 
449   buffer->clear_output ();
450 
451   buffer->idx = 0;
452   hb_glyph_info_t info = dottedcircle;
453   info.cluster = buffer->cur().cluster;
454   info.mask = buffer->cur().mask;
455   buffer->output_info (info);
456   while (buffer->idx < buffer->len && buffer->successful)
457     buffer->next_glyph ();
458   buffer->swap_buffers ();
459 }
460 
461 static void
hb_form_clusters(hb_buffer_t * buffer)462 hb_form_clusters (hb_buffer_t *buffer)
463 {
464   if (!(buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_NON_ASCII))
465     return;
466 
467   if (buffer->cluster_level == HB_BUFFER_CLUSTER_LEVEL_MONOTONE_GRAPHEMES)
468     foreach_grapheme (buffer, start, end)
469       buffer->merge_clusters (start, end);
470   else
471     foreach_grapheme (buffer, start, end)
472       buffer->unsafe_to_break (start, end);
473 }
474 
475 static void
hb_ensure_native_direction(hb_buffer_t * buffer)476 hb_ensure_native_direction (hb_buffer_t *buffer)
477 {
478   hb_direction_t direction = buffer->props.direction;
479   hb_direction_t horiz_dir = hb_script_get_horizontal_direction (buffer->props.script);
480 
481   /* TODO vertical:
482    * The only BTT vertical script is Ogham, but it's not clear to me whether OpenType
483    * Ogham fonts are supposed to be implemented BTT or not.  Need to research that
484    * first. */
485   if ((HB_DIRECTION_IS_HORIZONTAL (direction) &&
486        direction != horiz_dir && horiz_dir != HB_DIRECTION_INVALID) ||
487       (HB_DIRECTION_IS_VERTICAL   (direction) &&
488        direction != HB_DIRECTION_TTB))
489   {
490 
491     if (buffer->cluster_level == HB_BUFFER_CLUSTER_LEVEL_MONOTONE_CHARACTERS)
492       foreach_grapheme (buffer, start, end)
493       {
494 	buffer->merge_clusters (start, end);
495 	buffer->reverse_range (start, end);
496       }
497     else
498       foreach_grapheme (buffer, start, end)
499 	/* form_clusters() merged clusters already, we don't merge. */
500 	buffer->reverse_range (start, end);
501 
502     buffer->reverse ();
503 
504     buffer->props.direction = HB_DIRECTION_REVERSE (buffer->props.direction);
505   }
506 }
507 
508 
509 /*
510  * Substitute
511  */
512 
513 static inline void
hb_ot_mirror_chars(const hb_ot_shape_context_t * c)514 hb_ot_mirror_chars (const hb_ot_shape_context_t *c)
515 {
516   if (HB_DIRECTION_IS_FORWARD (c->target_direction))
517     return;
518 
519   hb_buffer_t *buffer = c->buffer;
520   hb_unicode_funcs_t *unicode = buffer->unicode;
521   hb_mask_t rtlm_mask = c->plan->rtlm_mask;
522 
523   unsigned int count = buffer->len;
524   hb_glyph_info_t *info = buffer->info;
525   for (unsigned int i = 0; i < count; i++) {
526     hb_codepoint_t codepoint = unicode->mirroring (info[i].codepoint);
527     if (likely (codepoint == info[i].codepoint || !c->font->has_glyph (codepoint)))
528       info[i].mask |= rtlm_mask;
529     else
530       info[i].codepoint = codepoint;
531   }
532 }
533 
534 static inline void
hb_ot_shape_setup_masks_fraction(const hb_ot_shape_context_t * c)535 hb_ot_shape_setup_masks_fraction (const hb_ot_shape_context_t *c)
536 {
537   if (!(c->buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_NON_ASCII) ||
538       !c->plan->has_frac)
539     return;
540 
541   hb_buffer_t *buffer = c->buffer;
542 
543   hb_mask_t pre_mask, post_mask;
544   if (HB_DIRECTION_IS_FORWARD (buffer->props.direction))
545   {
546     pre_mask = c->plan->numr_mask | c->plan->frac_mask;
547     post_mask = c->plan->frac_mask | c->plan->dnom_mask;
548   }
549   else
550   {
551     pre_mask = c->plan->frac_mask | c->plan->dnom_mask;
552     post_mask = c->plan->numr_mask | c->plan->frac_mask;
553   }
554 
555   unsigned int count = buffer->len;
556   hb_glyph_info_t *info = buffer->info;
557   for (unsigned int i = 0; i < count; i++)
558   {
559     if (info[i].codepoint == 0x2044u) /* FRACTION SLASH */
560     {
561       unsigned int start = i, end = i + 1;
562       while (start &&
563 	     _hb_glyph_info_get_general_category (&info[start - 1]) ==
564 	     HB_UNICODE_GENERAL_CATEGORY_DECIMAL_NUMBER)
565         start--;
566       while (end < count &&
567 	     _hb_glyph_info_get_general_category (&info[end]) ==
568 	     HB_UNICODE_GENERAL_CATEGORY_DECIMAL_NUMBER)
569         end++;
570 
571       buffer->unsafe_to_break (start, end);
572 
573       for (unsigned int j = start; j < i; j++)
574         info[j].mask |= pre_mask;
575       info[i].mask |= c->plan->frac_mask;
576       for (unsigned int j = i + 1; j < end; j++)
577         info[j].mask |= post_mask;
578 
579       i = end - 1;
580     }
581   }
582 }
583 
584 static inline void
hb_ot_shape_initialize_masks(const hb_ot_shape_context_t * c)585 hb_ot_shape_initialize_masks (const hb_ot_shape_context_t *c)
586 {
587   hb_ot_map_t *map = &c->plan->map;
588   hb_buffer_t *buffer = c->buffer;
589 
590   hb_mask_t global_mask = map->get_global_mask ();
591   buffer->reset_masks (global_mask);
592 }
593 
594 static inline void
hb_ot_shape_setup_masks(const hb_ot_shape_context_t * c)595 hb_ot_shape_setup_masks (const hb_ot_shape_context_t *c)
596 {
597   hb_ot_map_t *map = &c->plan->map;
598   hb_buffer_t *buffer = c->buffer;
599 
600   hb_ot_shape_setup_masks_fraction (c);
601 
602   if (c->plan->shaper->setup_masks)
603     c->plan->shaper->setup_masks (c->plan, buffer, c->font);
604 
605   for (unsigned int i = 0; i < c->num_user_features; i++)
606   {
607     const hb_feature_t *feature = &c->user_features[i];
608     if (!(feature->start == 0 && feature->end == (unsigned int)-1)) {
609       unsigned int shift;
610       hb_mask_t mask = map->get_mask (feature->tag, &shift);
611       buffer->set_masks (feature->value << shift, mask, feature->start, feature->end);
612     }
613   }
614 }
615 
616 static void
hb_ot_zero_width_default_ignorables(const hb_buffer_t * buffer)617 hb_ot_zero_width_default_ignorables (const hb_buffer_t *buffer)
618 {
619   if (!(buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_DEFAULT_IGNORABLES) ||
620       (buffer->flags & HB_BUFFER_FLAG_PRESERVE_DEFAULT_IGNORABLES) ||
621       (buffer->flags & HB_BUFFER_FLAG_REMOVE_DEFAULT_IGNORABLES))
622     return;
623 
624   unsigned int count = buffer->len;
625   hb_glyph_info_t *info = buffer->info;
626   hb_glyph_position_t *pos = buffer->pos;
627   unsigned int i = 0;
628   for (i = 0; i < count; i++)
629     if (unlikely (_hb_glyph_info_is_default_ignorable (&info[i])))
630       pos[i].x_advance = pos[i].y_advance = pos[i].x_offset = pos[i].y_offset = 0;
631 }
632 
633 static void
hb_ot_hide_default_ignorables(hb_buffer_t * buffer,hb_font_t * font)634 hb_ot_hide_default_ignorables (hb_buffer_t *buffer,
635 			       hb_font_t   *font)
636 {
637   if (!(buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_DEFAULT_IGNORABLES) ||
638       (buffer->flags & HB_BUFFER_FLAG_PRESERVE_DEFAULT_IGNORABLES))
639     return;
640 
641   unsigned int count = buffer->len;
642   hb_glyph_info_t *info = buffer->info;
643 
644   hb_codepoint_t invisible = buffer->invisible;
645   if (!(buffer->flags & HB_BUFFER_FLAG_REMOVE_DEFAULT_IGNORABLES) &&
646       (invisible || font->get_nominal_glyph (' ', &invisible)))
647   {
648     /* Replace default-ignorables with a zero-advance invisible glyph. */
649     for (unsigned int i = 0; i < count; i++)
650     {
651       if (_hb_glyph_info_is_default_ignorable (&info[i]))
652 	info[i].codepoint = invisible;
653     }
654   }
655   else
656     hb_ot_layout_delete_glyphs_inplace (buffer, _hb_glyph_info_is_default_ignorable);
657 }
658 
659 
660 static inline void
hb_ot_map_glyphs_fast(hb_buffer_t * buffer)661 hb_ot_map_glyphs_fast (hb_buffer_t  *buffer)
662 {
663   /* Normalization process sets up glyph_index(), we just copy it. */
664   unsigned int count = buffer->len;
665   hb_glyph_info_t *info = buffer->info;
666   for (unsigned int i = 0; i < count; i++)
667     info[i].codepoint = info[i].glyph_index();
668 
669   buffer->content_type = HB_BUFFER_CONTENT_TYPE_GLYPHS;
670 }
671 
672 static inline void
hb_synthesize_glyph_classes(hb_buffer_t * buffer)673 hb_synthesize_glyph_classes (hb_buffer_t *buffer)
674 {
675   unsigned int count = buffer->len;
676   hb_glyph_info_t *info = buffer->info;
677   for (unsigned int i = 0; i < count; i++)
678   {
679     hb_ot_layout_glyph_props_flags_t klass;
680 
681     /* Never mark default-ignorables as marks.
682      * They won't get in the way of lookups anyway,
683      * but having them as mark will cause them to be skipped
684      * over if the lookup-flag says so, but at least for the
685      * Mongolian variation selectors, looks like Uniscribe
686      * marks them as non-mark.  Some Mongolian fonts without
687      * GDEF rely on this.  Another notable character that
688      * this applies to is COMBINING GRAPHEME JOINER. */
689     klass = (_hb_glyph_info_get_general_category (&info[i]) !=
690 	     HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK ||
691 	     _hb_glyph_info_is_default_ignorable (&info[i])) ?
692 	    HB_OT_LAYOUT_GLYPH_PROPS_BASE_GLYPH :
693 	    HB_OT_LAYOUT_GLYPH_PROPS_MARK;
694     _hb_glyph_info_set_glyph_props (&info[i], klass);
695   }
696 }
697 
698 static inline void
hb_ot_substitute_default(const hb_ot_shape_context_t * c)699 hb_ot_substitute_default (const hb_ot_shape_context_t *c)
700 {
701   hb_buffer_t *buffer = c->buffer;
702 
703   hb_ot_mirror_chars (c);
704 
705   HB_BUFFER_ALLOCATE_VAR (buffer, glyph_index);
706 
707   _hb_ot_shape_normalize (c->plan, buffer, c->font);
708 
709   hb_ot_shape_setup_masks (c);
710 
711   /* This is unfortunate to go here, but necessary... */
712   if (c->plan->fallback_mark_positioning)
713     _hb_ot_shape_fallback_mark_position_recategorize_marks (c->plan, c->font, buffer);
714 
715   hb_ot_map_glyphs_fast (buffer);
716 
717   HB_BUFFER_DEALLOCATE_VAR (buffer, glyph_index);
718 }
719 
720 static inline void
hb_ot_substitute_complex(const hb_ot_shape_context_t * c)721 hb_ot_substitute_complex (const hb_ot_shape_context_t *c)
722 {
723   hb_buffer_t *buffer = c->buffer;
724 
725   hb_ot_layout_substitute_start (c->font, buffer);
726 
727   if (c->plan->fallback_glyph_classes)
728     hb_synthesize_glyph_classes (c->buffer);
729 
730   c->plan->substitute (c->font, buffer);
731 }
732 
733 static inline void
hb_ot_substitute_pre(const hb_ot_shape_context_t * c)734 hb_ot_substitute_pre (const hb_ot_shape_context_t *c)
735 {
736   hb_ot_substitute_default (c);
737 
738   _hb_buffer_allocate_gsubgpos_vars (c->buffer);
739 
740   hb_ot_substitute_complex (c);
741 }
742 
743 static inline void
hb_ot_substitute_post(const hb_ot_shape_context_t * c)744 hb_ot_substitute_post (const hb_ot_shape_context_t *c)
745 {
746   hb_ot_hide_default_ignorables (c->buffer, c->font);
747   if (c->plan->apply_morx)
748     hb_aat_layout_remove_deleted_glyphs (c->buffer);
749 
750   if (c->plan->shaper->postprocess_glyphs)
751     c->plan->shaper->postprocess_glyphs (c->plan, c->buffer, c->font);
752 }
753 
754 
755 /*
756  * Position
757  */
758 
759 static inline void
adjust_mark_offsets(hb_glyph_position_t * pos)760 adjust_mark_offsets (hb_glyph_position_t *pos)
761 {
762   pos->x_offset -= pos->x_advance;
763   pos->y_offset -= pos->y_advance;
764 }
765 
766 static inline void
zero_mark_width(hb_glyph_position_t * pos)767 zero_mark_width (hb_glyph_position_t *pos)
768 {
769   pos->x_advance = 0;
770   pos->y_advance = 0;
771 }
772 
773 static inline void
zero_mark_widths_by_gdef(hb_buffer_t * buffer,bool adjust_offsets)774 zero_mark_widths_by_gdef (hb_buffer_t *buffer, bool adjust_offsets)
775 {
776   unsigned int count = buffer->len;
777   hb_glyph_info_t *info = buffer->info;
778   for (unsigned int i = 0; i < count; i++)
779     if (_hb_glyph_info_is_mark (&info[i]))
780     {
781       if (adjust_offsets)
782         adjust_mark_offsets (&buffer->pos[i]);
783       zero_mark_width (&buffer->pos[i]);
784     }
785 }
786 
787 static inline void
hb_ot_position_default(const hb_ot_shape_context_t * c)788 hb_ot_position_default (const hb_ot_shape_context_t *c)
789 {
790   hb_direction_t direction = c->buffer->props.direction;
791   unsigned int count = c->buffer->len;
792   hb_glyph_info_t *info = c->buffer->info;
793   hb_glyph_position_t *pos = c->buffer->pos;
794 
795   if (HB_DIRECTION_IS_HORIZONTAL (direction))
796   {
797     c->font->get_glyph_h_advances (count, &info[0].codepoint, sizeof(info[0]),
798                                    &pos[0].x_advance, sizeof(pos[0]));
799     /* The nil glyph_h_origin() func returns 0, so no need to apply it. */
800     if (c->font->has_glyph_h_origin_func ())
801       for (unsigned int i = 0; i < count; i++)
802 	c->font->subtract_glyph_h_origin (info[i].codepoint,
803 					  &pos[i].x_offset,
804 					  &pos[i].y_offset);
805   }
806   else
807   {
808     c->font->get_glyph_v_advances (count, &info[0].codepoint, sizeof(info[0]),
809                                    &pos[0].y_advance, sizeof(pos[0]));
810     for (unsigned int i = 0; i < count; i++)
811     {
812       c->font->subtract_glyph_v_origin (info[i].codepoint,
813 					&pos[i].x_offset,
814 					&pos[i].y_offset);
815     }
816   }
817   if (c->buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_SPACE_FALLBACK)
818     _hb_ot_shape_fallback_spaces (c->plan, c->font, c->buffer);
819 }
820 
821 static inline void
hb_ot_position_complex(const hb_ot_shape_context_t * c)822 hb_ot_position_complex (const hb_ot_shape_context_t *c)
823 {
824   unsigned int count = c->buffer->len;
825   hb_glyph_info_t *info = c->buffer->info;
826   hb_glyph_position_t *pos = c->buffer->pos;
827 
828   /* If the font has no GPOS and direction is forward, then when
829    * zeroing mark widths, we shift the mark with it, such that the
830    * mark is positioned hanging over the previous glyph.  When
831    * direction is backward we don't shift and it will end up
832    * hanging over the next glyph after the final reordering.
833    *
834    * Note: If fallback positinoing happens, we don't care about
835    * this as it will be overriden.
836    */
837   bool adjust_offsets_when_zeroing = c->plan->adjust_mark_positioning_when_zeroing &&
838 				     HB_DIRECTION_IS_FORWARD (c->buffer->props.direction);
839 
840   /* We change glyph origin to what GPOS expects (horizontal), apply GPOS, change it back. */
841 
842   /* The nil glyph_h_origin() func returns 0, so no need to apply it. */
843   if (c->font->has_glyph_h_origin_func ())
844     for (unsigned int i = 0; i < count; i++)
845       c->font->add_glyph_h_origin (info[i].codepoint,
846 				   &pos[i].x_offset,
847 				   &pos[i].y_offset);
848 
849   hb_ot_layout_position_start (c->font, c->buffer);
850 
851   if (c->plan->zero_marks)
852     switch (c->plan->shaper->zero_width_marks)
853     {
854       case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_EARLY:
855 	zero_mark_widths_by_gdef (c->buffer, adjust_offsets_when_zeroing);
856 	break;
857 
858       default:
859       case HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE:
860       case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_LATE:
861 	break;
862     }
863 
864   c->plan->position (c->font, c->buffer);
865 
866   if (c->plan->zero_marks)
867     switch (c->plan->shaper->zero_width_marks)
868     {
869       case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_LATE:
870 	zero_mark_widths_by_gdef (c->buffer, adjust_offsets_when_zeroing);
871 	break;
872 
873       default:
874       case HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE:
875       case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_EARLY:
876 	break;
877     }
878 
879   /* Finish off.  Has to follow a certain order. */
880   hb_ot_layout_position_finish_advances (c->font, c->buffer);
881   hb_ot_zero_width_default_ignorables (c->buffer);
882   if (c->plan->apply_morx)
883     hb_aat_layout_zero_width_deleted_glyphs (c->buffer);
884   hb_ot_layout_position_finish_offsets (c->font, c->buffer);
885 
886   /* The nil glyph_h_origin() func returns 0, so no need to apply it. */
887   if (c->font->has_glyph_h_origin_func ())
888     for (unsigned int i = 0; i < count; i++)
889       c->font->subtract_glyph_h_origin (info[i].codepoint,
890 					&pos[i].x_offset,
891 					&pos[i].y_offset);
892 
893   if (c->plan->fallback_mark_positioning)
894     _hb_ot_shape_fallback_mark_position (c->plan, c->font, c->buffer);
895 }
896 
897 static inline void
hb_ot_position(const hb_ot_shape_context_t * c)898 hb_ot_position (const hb_ot_shape_context_t *c)
899 {
900   c->buffer->clear_positions ();
901 
902   hb_ot_position_default (c);
903 
904   hb_ot_position_complex (c);
905 
906   if (HB_DIRECTION_IS_BACKWARD (c->buffer->props.direction))
907     hb_buffer_reverse (c->buffer);
908 
909   _hb_buffer_deallocate_gsubgpos_vars (c->buffer);
910 }
911 
912 static inline void
hb_propagate_flags(hb_buffer_t * buffer)913 hb_propagate_flags (hb_buffer_t *buffer)
914 {
915   /* Propagate cluster-level glyph flags to be the same on all cluster glyphs.
916    * Simplifies using them. */
917 
918   if (!(buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_UNSAFE_TO_BREAK))
919     return;
920 
921   hb_glyph_info_t *info = buffer->info;
922 
923   foreach_cluster (buffer, start, end)
924   {
925     unsigned int mask = 0;
926     for (unsigned int i = start; i < end; i++)
927       if (info[i].mask & HB_GLYPH_FLAG_UNSAFE_TO_BREAK)
928       {
929 	 mask = HB_GLYPH_FLAG_UNSAFE_TO_BREAK;
930 	 break;
931       }
932     if (mask)
933       for (unsigned int i = start; i < end; i++)
934 	info[i].mask |= mask;
935   }
936 }
937 
938 /* Pull it all together! */
939 
940 static void
hb_ot_shape_internal(hb_ot_shape_context_t * c)941 hb_ot_shape_internal (hb_ot_shape_context_t *c)
942 {
943   c->buffer->deallocate_var_all ();
944   c->buffer->scratch_flags = HB_BUFFER_SCRATCH_FLAG_DEFAULT;
945   if (likely (!hb_unsigned_mul_overflows (c->buffer->len, HB_BUFFER_MAX_LEN_FACTOR)))
946   {
947     c->buffer->max_len = MAX (c->buffer->len * HB_BUFFER_MAX_LEN_FACTOR,
948 			      (unsigned) HB_BUFFER_MAX_LEN_MIN);
949   }
950   if (likely (!hb_unsigned_mul_overflows (c->buffer->len, HB_BUFFER_MAX_OPS_FACTOR)))
951   {
952     c->buffer->max_ops = MAX (c->buffer->len * HB_BUFFER_MAX_OPS_FACTOR,
953 			      (unsigned) HB_BUFFER_MAX_OPS_MIN);
954   }
955 
956   /* Save the original direction, we use it later. */
957   c->target_direction = c->buffer->props.direction;
958 
959   _hb_buffer_allocate_unicode_vars (c->buffer);
960 
961   c->buffer->clear_output ();
962 
963   hb_ot_shape_initialize_masks (c);
964   hb_set_unicode_props (c->buffer);
965   hb_insert_dotted_circle (c->buffer, c->font);
966 
967   hb_form_clusters (c->buffer);
968 
969   hb_ensure_native_direction (c->buffer);
970 
971   if (c->plan->shaper->preprocess_text)
972     c->plan->shaper->preprocess_text (c->plan, c->buffer, c->font);
973 
974   hb_ot_substitute_pre (c);
975   hb_ot_position (c);
976   hb_ot_substitute_post (c);
977 
978   hb_propagate_flags (c->buffer);
979 
980   _hb_buffer_deallocate_unicode_vars (c->buffer);
981 
982   c->buffer->props.direction = c->target_direction;
983 
984   c->buffer->max_len = HB_BUFFER_MAX_LEN_DEFAULT;
985   c->buffer->max_ops = HB_BUFFER_MAX_OPS_DEFAULT;
986   c->buffer->deallocate_var_all ();
987 }
988 
989 
990 hb_bool_t
_hb_ot_shape(hb_shape_plan_t * shape_plan,hb_font_t * font,hb_buffer_t * buffer,const hb_feature_t * features,unsigned int num_features)991 _hb_ot_shape (hb_shape_plan_t    *shape_plan,
992 	      hb_font_t          *font,
993 	      hb_buffer_t        *buffer,
994 	      const hb_feature_t *features,
995 	      unsigned int        num_features)
996 {
997   hb_ot_shape_context_t c = {&shape_plan->ot, font, font->face, buffer, features, num_features};
998   hb_ot_shape_internal (&c);
999 
1000   return true;
1001 }
1002 
1003 
1004 /**
1005  * hb_ot_shape_plan_collect_lookups:
1006  *
1007  * Since: 0.9.7
1008  **/
1009 void
hb_ot_shape_plan_collect_lookups(hb_shape_plan_t * shape_plan,hb_tag_t table_tag,hb_set_t * lookup_indexes)1010 hb_ot_shape_plan_collect_lookups (hb_shape_plan_t *shape_plan,
1011 				  hb_tag_t         table_tag,
1012 				  hb_set_t        *lookup_indexes /* OUT */)
1013 {
1014   shape_plan->ot.collect_lookups (table_tag, lookup_indexes);
1015 }
1016 
1017 
1018 /* TODO Move this to hb-ot-shape-normalize, make it do decompose, and make it public. */
1019 static void
add_char(hb_font_t * font,hb_unicode_funcs_t * unicode,hb_bool_t mirror,hb_codepoint_t u,hb_set_t * glyphs)1020 add_char (hb_font_t          *font,
1021 	  hb_unicode_funcs_t *unicode,
1022 	  hb_bool_t           mirror,
1023 	  hb_codepoint_t      u,
1024 	  hb_set_t           *glyphs)
1025 {
1026   hb_codepoint_t glyph;
1027   if (font->get_nominal_glyph (u, &glyph))
1028     glyphs->add (glyph);
1029   if (mirror)
1030   {
1031     hb_codepoint_t m = unicode->mirroring (u);
1032     if (m != u && font->get_nominal_glyph (m, &glyph))
1033       glyphs->add (glyph);
1034   }
1035 }
1036 
1037 
1038 /**
1039  * hb_ot_shape_glyphs_closure:
1040  *
1041  * Since: 0.9.2
1042  **/
1043 void
hb_ot_shape_glyphs_closure(hb_font_t * font,hb_buffer_t * buffer,const hb_feature_t * features,unsigned int num_features,hb_set_t * glyphs)1044 hb_ot_shape_glyphs_closure (hb_font_t          *font,
1045 			    hb_buffer_t        *buffer,
1046 			    const hb_feature_t *features,
1047 			    unsigned int        num_features,
1048 			    hb_set_t           *glyphs)
1049 {
1050   const char *shapers[] = {"ot", nullptr};
1051   hb_shape_plan_t *shape_plan = hb_shape_plan_create_cached (font->face, &buffer->props,
1052 							     features, num_features, shapers);
1053 
1054   bool mirror = hb_script_get_horizontal_direction (buffer->props.script) == HB_DIRECTION_RTL;
1055 
1056   unsigned int count = buffer->len;
1057   hb_glyph_info_t *info = buffer->info;
1058   for (unsigned int i = 0; i < count; i++)
1059     add_char (font, buffer->unicode, mirror, info[i].codepoint, glyphs);
1060 
1061   hb_set_t *lookups = hb_set_create ();
1062   hb_ot_shape_plan_collect_lookups (shape_plan, HB_OT_TAG_GSUB, lookups);
1063   hb_ot_layout_lookups_substitute_closure (font->face, lookups, glyphs);
1064 
1065   hb_set_destroy (lookups);
1066 
1067   hb_shape_plan_destroy (shape_plan);
1068 }
1069