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.hh"
30 
31 #ifndef HB_NO_OT_SHAPE
32 
33 #ifdef HB_NO_OT_LAYOUT
34 #error "Cannot compile 'ot' shaper with HB_NO_OT_LAYOUT."
35 #endif
36 
37 #include "hb-shaper-impl.hh"
38 
39 #include "hb-ot-shape.hh"
40 #include "hb-ot-shape-complex.hh"
41 #include "hb-ot-shape-fallback.hh"
42 #include "hb-ot-shape-normalize.hh"
43 
44 #include "hb-ot-face.hh"
45 
46 #include "hb-set.hh"
47 
48 #include "hb-aat-layout.hh"
49 
50 
51 #ifndef HB_NO_AAT_SHAPE
52 static inline bool
_hb_apply_morx(hb_face_t * face,const hb_segment_properties_t * props)53 _hb_apply_morx (hb_face_t *face, const hb_segment_properties_t *props)
54 {
55   /* https://github.com/harfbuzz/harfbuzz/issues/2124 */
56   return hb_aat_layout_has_substitution (face) &&
57 	 (HB_DIRECTION_IS_HORIZONTAL (props->direction) || !hb_ot_layout_has_substitution (face));
58 }
59 #endif
60 
61 /**
62  * SECTION:hb-ot-shape
63  * @title: hb-ot-shape
64  * @short_description: OpenType shaping support
65  * @include: hb-ot.h
66  *
67  * Support functions for OpenType shaping related queries.
68  **/
69 
70 
71 static void
72 hb_ot_shape_collect_features (hb_ot_shape_planner_t          *planner,
73 			      const hb_feature_t             *user_features,
74 			      unsigned int                    num_user_features);
75 
hb_ot_shape_planner_t(hb_face_t * face,const hb_segment_properties_t * props)76 hb_ot_shape_planner_t::hb_ot_shape_planner_t (hb_face_t                     *face,
77 					      const hb_segment_properties_t *props) :
78 						face (face),
79 						props (*props),
80 						map (face, props),
81 						aat_map (face, props)
82 #ifndef HB_NO_AAT_SHAPE
83 						, apply_morx (_hb_apply_morx (face, props))
84 #endif
85 {
86   shaper = hb_ot_shape_complex_categorize (this);
87 
88   script_zero_marks = shaper->zero_width_marks != HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE;
89   script_fallback_mark_positioning = shaper->fallback_position;
90 
91   /* https://github.com/harfbuzz/harfbuzz/issues/1528 */
92   if (apply_morx && shaper != &_hb_ot_complex_shaper_default)
93     shaper = &_hb_ot_complex_shaper_dumber;
94 }
95 
96 void
compile(hb_ot_shape_plan_t & plan,const hb_ot_shape_plan_key_t & key)97 hb_ot_shape_planner_t::compile (hb_ot_shape_plan_t           &plan,
98 				const hb_ot_shape_plan_key_t &key)
99 {
100   plan.props = props;
101   plan.shaper = shaper;
102   map.compile (plan.map, key);
103 #ifndef HB_NO_AAT_SHAPE
104   if (apply_morx)
105     aat_map.compile (plan.aat_map);
106 #endif
107 
108 #ifndef HB_NO_OT_SHAPE_FRACTIONS
109   plan.frac_mask = plan.map.get_1_mask (HB_TAG ('f','r','a','c'));
110   plan.numr_mask = plan.map.get_1_mask (HB_TAG ('n','u','m','r'));
111   plan.dnom_mask = plan.map.get_1_mask (HB_TAG ('d','n','o','m'));
112   plan.has_frac = plan.frac_mask || (plan.numr_mask && plan.dnom_mask);
113 #endif
114 
115   plan.rtlm_mask = plan.map.get_1_mask (HB_TAG ('r','t','l','m'));
116   plan.has_vert = !!plan.map.get_1_mask (HB_TAG ('v','e','r','t'));
117 
118   hb_tag_t kern_tag = HB_DIRECTION_IS_HORIZONTAL (props.direction) ?
119 		      HB_TAG ('k','e','r','n') : HB_TAG ('v','k','r','n');
120 #ifndef HB_NO_OT_KERN
121   plan.kern_mask = plan.map.get_mask (kern_tag);
122   plan.requested_kerning = !!plan.kern_mask;
123 #endif
124 #ifndef HB_NO_AAT_SHAPE
125   plan.trak_mask = plan.map.get_mask (HB_TAG ('t','r','a','k'));
126   plan.requested_tracking = !!plan.trak_mask;
127 #endif
128 
129   bool has_gpos_kern = plan.map.get_feature_index (1, kern_tag) != HB_OT_LAYOUT_NO_FEATURE_INDEX;
130   bool disable_gpos = plan.shaper->gpos_tag &&
131 		      plan.shaper->gpos_tag != plan.map.chosen_script[1];
132 
133   /*
134    * Decide who provides glyph classes. GDEF or Unicode.
135    */
136 
137   if (!hb_ot_layout_has_glyph_classes (face))
138     plan.fallback_glyph_classes = true;
139 
140   /*
141    * Decide who does substitutions. GSUB, morx, or fallback.
142    */
143 
144 #ifndef HB_NO_AAT_SHAPE
145   plan.apply_morx = apply_morx;
146 #endif
147 
148   /*
149    * Decide who does positioning. GPOS, kerx, kern, or fallback.
150    */
151 
152 #ifndef HB_NO_AAT_SHAPE
153   bool has_kerx = hb_aat_layout_has_positioning (face);
154   bool has_gsub = !apply_morx && hb_ot_layout_has_substitution (face);
155 #endif
156   bool has_gpos = !disable_gpos && hb_ot_layout_has_positioning (face);
157   if (false)
158     ;
159 #ifndef HB_NO_AAT_SHAPE
160   /* Prefer GPOS over kerx if GSUB is present;
161    * https://github.com/harfbuzz/harfbuzz/issues/3008 */
162   else if (has_kerx && !(has_gsub && has_gpos))
163     plan.apply_kerx = true;
164 #endif
165   else if (has_gpos)
166     plan.apply_gpos = true;
167 
168   if (!plan.apply_kerx && (!has_gpos_kern || !plan.apply_gpos))
169   {
170 #ifndef HB_NO_AAT_SHAPE
171     if (has_kerx)
172       plan.apply_kerx = true;
173     else
174 #endif
175 #ifndef HB_NO_OT_KERN
176     if (hb_ot_layout_has_kerning (face))
177       plan.apply_kern = true;
178 #endif
179   }
180 
181   plan.apply_fallback_kern = !(plan.apply_gpos || plan.apply_kerx || plan.apply_kern);
182 
183   plan.zero_marks = script_zero_marks &&
184 		    !plan.apply_kerx &&
185 		    (!plan.apply_kern
186 #ifndef HB_NO_OT_KERN
187 		     || !hb_ot_layout_has_machine_kerning (face)
188 #endif
189 		    );
190   plan.has_gpos_mark = !!plan.map.get_1_mask (HB_TAG ('m','a','r','k'));
191 
192   plan.adjust_mark_positioning_when_zeroing = !plan.apply_gpos &&
193 					      !plan.apply_kerx &&
194 					      (!plan.apply_kern
195 #ifndef HB_NO_OT_KERN
196 					       || !hb_ot_layout_has_cross_kerning (face)
197 #endif
198 					      );
199 
200   plan.fallback_mark_positioning = plan.adjust_mark_positioning_when_zeroing &&
201 				   script_fallback_mark_positioning;
202 
203 #ifndef HB_NO_AAT_SHAPE
204   /* If we're using morx shaping, we cancel mark position adjustment because
205      Apple Color Emoji assumes this will NOT be done when forming emoji sequences;
206      https://github.com/harfbuzz/harfbuzz/issues/2967. */
207   if (plan.apply_morx)
208     plan.adjust_mark_positioning_when_zeroing = false;
209 
210   /* Currently we always apply trak. */
211   plan.apply_trak = plan.requested_tracking && hb_aat_layout_has_tracking (face);
212 #endif
213 }
214 
215 bool
init0(hb_face_t * face,const hb_shape_plan_key_t * key)216 hb_ot_shape_plan_t::init0 (hb_face_t                     *face,
217 			   const hb_shape_plan_key_t     *key)
218 {
219   map.init ();
220 #ifndef HB_NO_AAT_SHAPE
221   aat_map.init ();
222 #endif
223 
224   hb_ot_shape_planner_t planner (face,
225 				 &key->props);
226 
227   hb_ot_shape_collect_features (&planner,
228 				key->user_features,
229 				key->num_user_features);
230 
231   planner.compile (*this, key->ot);
232 
233   if (shaper->data_create)
234   {
235     data = shaper->data_create (this);
236     if (unlikely (!data))
237     {
238       map.fini ();
239 #ifndef HB_NO_AAT_SHAPE
240       aat_map.fini ();
241 #endif
242       return false;
243     }
244   }
245 
246   return true;
247 }
248 
249 void
fini()250 hb_ot_shape_plan_t::fini ()
251 {
252   if (shaper->data_destroy)
253     shaper->data_destroy (const_cast<void *> (data));
254 
255   map.fini ();
256 #ifndef HB_NO_AAT_SHAPE
257   aat_map.fini ();
258 #endif
259 }
260 
261 void
substitute(hb_font_t * font,hb_buffer_t * buffer) const262 hb_ot_shape_plan_t::substitute (hb_font_t   *font,
263 				hb_buffer_t *buffer) const
264 {
265 #ifndef HB_NO_AAT_SHAPE
266   if (unlikely (apply_morx))
267     hb_aat_layout_substitute (this, font, buffer);
268   else
269 #endif
270     map.substitute (this, font, buffer);
271 }
272 
273 void
position(hb_font_t * font,hb_buffer_t * buffer) const274 hb_ot_shape_plan_t::position (hb_font_t   *font,
275 			      hb_buffer_t *buffer) const
276 {
277   if (this->apply_gpos)
278     map.position (this, font, buffer);
279 #ifndef HB_NO_AAT_SHAPE
280   else if (this->apply_kerx)
281     hb_aat_layout_position (this, font, buffer);
282 #endif
283 
284 #ifndef HB_NO_OT_KERN
285   if (this->apply_kern)
286     hb_ot_layout_kern (this, font, buffer);
287 #endif
288   else if (this->apply_fallback_kern)
289     _hb_ot_shape_fallback_kern (this, font, buffer);
290 
291 #ifndef HB_NO_AAT_SHAPE
292   if (this->apply_trak)
293     hb_aat_layout_track (this, font, buffer);
294 #endif
295 }
296 
297 
298 static const hb_ot_map_feature_t
299 common_features[] =
300 {
301   {HB_TAG('a','b','v','m'), F_GLOBAL},
302   {HB_TAG('b','l','w','m'), F_GLOBAL},
303   {HB_TAG('c','c','m','p'), F_GLOBAL},
304   {HB_TAG('l','o','c','l'), F_GLOBAL},
305   {HB_TAG('m','a','r','k'), F_GLOBAL_MANUAL_JOINERS},
306   {HB_TAG('m','k','m','k'), F_GLOBAL_MANUAL_JOINERS},
307   {HB_TAG('r','l','i','g'), F_GLOBAL},
308 };
309 
310 
311 static const hb_ot_map_feature_t
312 horizontal_features[] =
313 {
314   {HB_TAG('c','a','l','t'), F_GLOBAL},
315   {HB_TAG('c','l','i','g'), F_GLOBAL},
316   {HB_TAG('c','u','r','s'), F_GLOBAL},
317   {HB_TAG('d','i','s','t'), F_GLOBAL},
318   {HB_TAG('k','e','r','n'), F_GLOBAL_HAS_FALLBACK},
319   {HB_TAG('l','i','g','a'), F_GLOBAL},
320   {HB_TAG('r','c','l','t'), F_GLOBAL},
321 };
322 
323 static void
hb_ot_shape_collect_features(hb_ot_shape_planner_t * planner,const hb_feature_t * user_features,unsigned int num_user_features)324 hb_ot_shape_collect_features (hb_ot_shape_planner_t *planner,
325 			      const hb_feature_t    *user_features,
326 			      unsigned int           num_user_features)
327 {
328   hb_ot_map_builder_t *map = &planner->map;
329 
330   map->enable_feature (HB_TAG('r','v','r','n'));
331   map->add_gsub_pause (nullptr);
332 
333   switch (planner->props.direction)
334   {
335     case HB_DIRECTION_LTR:
336       map->enable_feature (HB_TAG ('l','t','r','a'));
337       map->enable_feature (HB_TAG ('l','t','r','m'));
338       break;
339     case HB_DIRECTION_RTL:
340       map->enable_feature (HB_TAG ('r','t','l','a'));
341       map->add_feature (HB_TAG ('r','t','l','m'));
342       break;
343     case HB_DIRECTION_TTB:
344     case HB_DIRECTION_BTT:
345     case HB_DIRECTION_INVALID:
346     default:
347       break;
348   }
349 
350 #ifndef HB_NO_OT_SHAPE_FRACTIONS
351   /* Automatic fractions. */
352   map->add_feature (HB_TAG ('f','r','a','c'));
353   map->add_feature (HB_TAG ('n','u','m','r'));
354   map->add_feature (HB_TAG ('d','n','o','m'));
355 #endif
356 
357   /* Random! */
358   map->enable_feature (HB_TAG ('r','a','n','d'), F_RANDOM, HB_OT_MAP_MAX_VALUE);
359 
360 #ifndef HB_NO_AAT_SHAPE
361   /* Tracking.  We enable dummy feature here just to allow disabling
362    * AAT 'trak' table using features.
363    * https://github.com/harfbuzz/harfbuzz/issues/1303 */
364   map->enable_feature (HB_TAG ('t','r','a','k'), F_HAS_FALLBACK);
365 #endif
366 
367   map->enable_feature (HB_TAG ('H','a','r','f')); /* Considered required. */
368   map->enable_feature (HB_TAG ('H','A','R','F')); /* Considered discretionary. */
369 
370   if (planner->shaper->collect_features)
371     planner->shaper->collect_features (planner);
372 
373   map->enable_feature (HB_TAG ('B','u','z','z')); /* Considered required. */
374   map->enable_feature (HB_TAG ('B','U','Z','Z')); /* Considered discretionary. */
375 
376   for (unsigned int i = 0; i < ARRAY_LENGTH (common_features); i++)
377     map->add_feature (common_features[i]);
378 
379   if (HB_DIRECTION_IS_HORIZONTAL (planner->props.direction))
380     for (unsigned int i = 0; i < ARRAY_LENGTH (horizontal_features); i++)
381       map->add_feature (horizontal_features[i]);
382   else
383   {
384     /* We only apply `vert` feature. See:
385      * https://github.com/harfbuzz/harfbuzz/commit/d71c0df2d17f4590d5611239577a6cb532c26528
386      * https://lists.freedesktop.org/archives/harfbuzz/2013-August/003490.html */
387 
388     /* We really want to find a 'vert' feature if there's any in the font, no
389      * matter which script/langsys it is listed (or not) under.
390      * See various bugs referenced from:
391      * https://github.com/harfbuzz/harfbuzz/issues/63 */
392     map->enable_feature (HB_TAG ('v','e','r','t'), F_GLOBAL_SEARCH);
393   }
394 
395   for (unsigned int i = 0; i < num_user_features; i++)
396   {
397     const hb_feature_t *feature = &user_features[i];
398     map->add_feature (feature->tag,
399 		      (feature->start == HB_FEATURE_GLOBAL_START &&
400 		       feature->end == HB_FEATURE_GLOBAL_END) ?  F_GLOBAL : F_NONE,
401 		      feature->value);
402   }
403 
404 #ifndef HB_NO_AAT_SHAPE
405   if (planner->apply_morx)
406   {
407     hb_aat_map_builder_t *aat_map = &planner->aat_map;
408     for (unsigned int i = 0; i < num_user_features; i++)
409     {
410       const hb_feature_t *feature = &user_features[i];
411       aat_map->add_feature (feature->tag, feature->value);
412     }
413   }
414 #endif
415 
416   if (planner->shaper->override_features)
417     planner->shaper->override_features (planner);
418 }
419 
420 
421 /*
422  * shaper face data
423  */
424 
425 struct hb_ot_face_data_t {};
426 
427 hb_ot_face_data_t *
_hb_ot_shaper_face_data_create(hb_face_t * face)428 _hb_ot_shaper_face_data_create (hb_face_t *face)
429 {
430   return (hb_ot_face_data_t *) HB_SHAPER_DATA_SUCCEEDED;
431 }
432 
433 void
_hb_ot_shaper_face_data_destroy(hb_ot_face_data_t * data)434 _hb_ot_shaper_face_data_destroy (hb_ot_face_data_t *data)
435 {
436 }
437 
438 
439 /*
440  * shaper font data
441  */
442 
443 struct hb_ot_font_data_t {};
444 
445 hb_ot_font_data_t *
_hb_ot_shaper_font_data_create(hb_font_t * font HB_UNUSED)446 _hb_ot_shaper_font_data_create (hb_font_t *font HB_UNUSED)
447 {
448   return (hb_ot_font_data_t *) HB_SHAPER_DATA_SUCCEEDED;
449 }
450 
451 void
_hb_ot_shaper_font_data_destroy(hb_ot_font_data_t * data HB_UNUSED)452 _hb_ot_shaper_font_data_destroy (hb_ot_font_data_t *data HB_UNUSED)
453 {
454 }
455 
456 
457 /*
458  * shaper
459  */
460 
461 struct hb_ot_shape_context_t
462 {
463   hb_ot_shape_plan_t *plan;
464   hb_font_t *font;
465   hb_face_t *face;
466   hb_buffer_t  *buffer;
467   const hb_feature_t *user_features;
468   unsigned int        num_user_features;
469 
470   /* Transient stuff */
471   hb_direction_t target_direction;
472 };
473 
474 
475 
476 /* Main shaper */
477 
478 
479 /* Prepare */
480 
481 static void
hb_set_unicode_props(hb_buffer_t * buffer)482 hb_set_unicode_props (hb_buffer_t *buffer)
483 {
484   /* Implement enough of Unicode Graphemes here that shaping
485    * in reverse-direction wouldn't break graphemes.  Namely,
486    * we mark all marks and ZWJ and ZWJ,Extended_Pictographic
487    * sequences as continuations.  The foreach_grapheme()
488    * macro uses this bit.
489    *
490    * https://www.unicode.org/reports/tr29/#Regex_Definitions
491    */
492   unsigned int count = buffer->len;
493   hb_glyph_info_t *info = buffer->info;
494   for (unsigned int i = 0; i < count; i++)
495   {
496     _hb_glyph_info_set_unicode_props (&info[i], buffer);
497 
498     /* Marks are already set as continuation by the above line.
499      * Handle Emoji_Modifier and ZWJ-continuation. */
500     if (unlikely (_hb_glyph_info_get_general_category (&info[i]) == HB_UNICODE_GENERAL_CATEGORY_MODIFIER_SYMBOL &&
501 		  hb_in_range<hb_codepoint_t> (info[i].codepoint, 0x1F3FBu, 0x1F3FFu)))
502     {
503       _hb_glyph_info_set_continuation (&info[i]);
504     }
505     /* Regional_Indicators are hairy as hell...
506      * https://github.com/harfbuzz/harfbuzz/issues/2265 */
507     else if (unlikely (i && hb_in_range<hb_codepoint_t> (info[i].codepoint, 0x1F1E6u, 0x1F1FFu)))
508     {
509       if (hb_in_range<hb_codepoint_t> (info[i - 1].codepoint, 0x1F1E6u, 0x1F1FFu) &&
510 	  !_hb_glyph_info_is_continuation (&info[i - 1]))
511 	_hb_glyph_info_set_continuation (&info[i]);
512     }
513 #ifndef HB_NO_EMOJI_SEQUENCES
514     else if (unlikely (_hb_glyph_info_is_zwj (&info[i])))
515     {
516       _hb_glyph_info_set_continuation (&info[i]);
517       if (i + 1 < count &&
518 	  _hb_unicode_is_emoji_Extended_Pictographic (info[i + 1].codepoint))
519       {
520 	i++;
521 	_hb_glyph_info_set_unicode_props (&info[i], buffer);
522 	_hb_glyph_info_set_continuation (&info[i]);
523       }
524     }
525 #endif
526     /* Or part of the Other_Grapheme_Extend that is not marks.
527      * As of Unicode 11 that is just:
528      *
529      * 200C          ; Other_Grapheme_Extend # Cf       ZERO WIDTH NON-JOINER
530      * FF9E..FF9F    ; Other_Grapheme_Extend # Lm   [2] HALFWIDTH KATAKANA VOICED SOUND MARK..HALFWIDTH KATAKANA SEMI-VOICED SOUND MARK
531      * E0020..E007F  ; Other_Grapheme_Extend # Cf  [96] TAG SPACE..CANCEL TAG
532      *
533      * ZWNJ is special, we don't want to merge it as there's no need, and keeping
534      * it separate results in more granular clusters.  Ignore Katakana for now.
535      * Tags are used for Emoji sub-region flag sequences:
536      * https://github.com/harfbuzz/harfbuzz/issues/1556
537      */
538     else if (unlikely (hb_in_range<hb_codepoint_t> (info[i].codepoint, 0xE0020u, 0xE007Fu)))
539       _hb_glyph_info_set_continuation (&info[i]);
540   }
541 }
542 
543 static void
hb_insert_dotted_circle(hb_buffer_t * buffer,hb_font_t * font)544 hb_insert_dotted_circle (hb_buffer_t *buffer, hb_font_t *font)
545 {
546   if (unlikely (buffer->flags & HB_BUFFER_FLAG_DO_NOT_INSERT_DOTTED_CIRCLE))
547     return;
548 
549   if (!(buffer->flags & HB_BUFFER_FLAG_BOT) ||
550       buffer->context_len[0] ||
551       !_hb_glyph_info_is_unicode_mark (&buffer->info[0]))
552     return;
553 
554   if (!font->has_glyph (0x25CCu))
555     return;
556 
557   hb_glyph_info_t dottedcircle = {0};
558   dottedcircle.codepoint = 0x25CCu;
559   _hb_glyph_info_set_unicode_props (&dottedcircle, buffer);
560 
561   buffer->clear_output ();
562 
563   buffer->idx = 0;
564   hb_glyph_info_t info = dottedcircle;
565   info.cluster = buffer->cur().cluster;
566   info.mask = buffer->cur().mask;
567   (void) buffer->output_info (info);
568 
569   buffer->sync ();
570 }
571 
572 static void
hb_form_clusters(hb_buffer_t * buffer)573 hb_form_clusters (hb_buffer_t *buffer)
574 {
575   if (!(buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_NON_ASCII))
576     return;
577 
578   if (buffer->cluster_level == HB_BUFFER_CLUSTER_LEVEL_MONOTONE_GRAPHEMES)
579     foreach_grapheme (buffer, start, end)
580       buffer->merge_clusters (start, end);
581   else
582     foreach_grapheme (buffer, start, end)
583       buffer->unsafe_to_break (start, end);
584 }
585 
586 static void
hb_ensure_native_direction(hb_buffer_t * buffer)587 hb_ensure_native_direction (hb_buffer_t *buffer)
588 {
589   hb_direction_t direction = buffer->props.direction;
590   hb_direction_t horiz_dir = hb_script_get_horizontal_direction (buffer->props.script);
591 
592   /* Numeric runs in natively-RTL scripts are actually native-LTR, so we reset
593    * the horiz_dir if the run contains at least one decimal-number char, and no
594    * letter chars (ideally we should be checking for chars with strong
595    * directionality but hb-unicode currently lacks bidi categories).
596    *
597    * This allows digit sequences in Arabic etc to be shaped in "native"
598    * direction, so that features like ligatures will work as intended.
599    *
600    * https://github.com/harfbuzz/harfbuzz/issues/501
601    */
602   if (unlikely (horiz_dir == HB_DIRECTION_RTL && direction == HB_DIRECTION_LTR))
603   {
604     bool found_number = false, found_letter = false;
605     const auto* info = buffer->info;
606     const auto count = buffer->len;
607     for (unsigned i = 0; i < count; i++)
608     {
609       auto gc = _hb_glyph_info_get_general_category (&info[i]);
610       if (gc == HB_UNICODE_GENERAL_CATEGORY_DECIMAL_NUMBER)
611         found_number = true;
612       else if (HB_UNICODE_GENERAL_CATEGORY_IS_LETTER (gc))
613       {
614         found_letter = true;
615         break;
616       }
617     }
618     if (found_number && !found_letter)
619       horiz_dir = HB_DIRECTION_LTR;
620   }
621 
622   /* TODO vertical:
623    * The only BTT vertical script is Ogham, but it's not clear to me whether OpenType
624    * Ogham fonts are supposed to be implemented BTT or not.  Need to research that
625    * first. */
626   if ((HB_DIRECTION_IS_HORIZONTAL (direction) &&
627        direction != horiz_dir && horiz_dir != HB_DIRECTION_INVALID) ||
628       (HB_DIRECTION_IS_VERTICAL   (direction) &&
629        direction != HB_DIRECTION_TTB))
630   {
631     _hb_ot_layout_reverse_graphemes (buffer);
632     buffer->props.direction = HB_DIRECTION_REVERSE (buffer->props.direction);
633   }
634 }
635 
636 
637 /*
638  * Substitute
639  */
640 
641 #ifndef HB_NO_VERTICAL
642 static hb_codepoint_t
hb_vert_char_for(hb_codepoint_t u)643 hb_vert_char_for (hb_codepoint_t u)
644 {
645   switch (u >> 8)
646   {
647     case 0x20: switch (u) {
648       case 0x2013u: return 0xfe32u; // EN DASH
649       case 0x2014u: return 0xfe31u; // EM DASH
650       case 0x2025u: return 0xfe30u; // TWO DOT LEADER
651       case 0x2026u: return 0xfe19u; // HORIZONTAL ELLIPSIS
652     } break;
653     case 0x30: switch (u) {
654       case 0x3001u: return 0xfe11u; // IDEOGRAPHIC COMMA
655       case 0x3002u: return 0xfe12u; // IDEOGRAPHIC FULL STOP
656       case 0x3008u: return 0xfe3fu; // LEFT ANGLE BRACKET
657       case 0x3009u: return 0xfe40u; // RIGHT ANGLE BRACKET
658       case 0x300au: return 0xfe3du; // LEFT DOUBLE ANGLE BRACKET
659       case 0x300bu: return 0xfe3eu; // RIGHT DOUBLE ANGLE BRACKET
660       case 0x300cu: return 0xfe41u; // LEFT CORNER BRACKET
661       case 0x300du: return 0xfe42u; // RIGHT CORNER BRACKET
662       case 0x300eu: return 0xfe43u; // LEFT WHITE CORNER BRACKET
663       case 0x300fu: return 0xfe44u; // RIGHT WHITE CORNER BRACKET
664       case 0x3010u: return 0xfe3bu; // LEFT BLACK LENTICULAR BRACKET
665       case 0x3011u: return 0xfe3cu; // RIGHT BLACK LENTICULAR BRACKET
666       case 0x3014u: return 0xfe39u; // LEFT TORTOISE SHELL BRACKET
667       case 0x3015u: return 0xfe3au; // RIGHT TORTOISE SHELL BRACKET
668       case 0x3016u: return 0xfe17u; // LEFT WHITE LENTICULAR BRACKET
669       case 0x3017u: return 0xfe18u; // RIGHT WHITE LENTICULAR BRACKET
670     } break;
671     case 0xfe: switch (u) {
672       case 0xfe4fu: return 0xfe34u; // WAVY LOW LINE
673     } break;
674     case 0xff: switch (u) {
675       case 0xff01u: return 0xfe15u; // FULLWIDTH EXCLAMATION MARK
676       case 0xff08u: return 0xfe35u; // FULLWIDTH LEFT PARENTHESIS
677       case 0xff09u: return 0xfe36u; // FULLWIDTH RIGHT PARENTHESIS
678       case 0xff0cu: return 0xfe10u; // FULLWIDTH COMMA
679       case 0xff1au: return 0xfe13u; // FULLWIDTH COLON
680       case 0xff1bu: return 0xfe14u; // FULLWIDTH SEMICOLON
681       case 0xff1fu: return 0xfe16u; // FULLWIDTH QUESTION MARK
682       case 0xff3bu: return 0xfe47u; // FULLWIDTH LEFT SQUARE BRACKET
683       case 0xff3du: return 0xfe48u; // FULLWIDTH RIGHT SQUARE BRACKET
684       case 0xff3fu: return 0xfe33u; // FULLWIDTH LOW LINE
685       case 0xff5bu: return 0xfe37u; // FULLWIDTH LEFT CURLY BRACKET
686       case 0xff5du: return 0xfe38u; // FULLWIDTH RIGHT CURLY BRACKET
687     } break;
688   }
689 
690   return u;
691 }
692 #endif
693 
694 static inline void
hb_ot_rotate_chars(const hb_ot_shape_context_t * c)695 hb_ot_rotate_chars (const hb_ot_shape_context_t *c)
696 {
697   hb_buffer_t *buffer = c->buffer;
698   unsigned int count = buffer->len;
699   hb_glyph_info_t *info = buffer->info;
700 
701   if (HB_DIRECTION_IS_BACKWARD (c->target_direction))
702   {
703     hb_unicode_funcs_t *unicode = buffer->unicode;
704     hb_mask_t rtlm_mask = c->plan->rtlm_mask;
705 
706     for (unsigned int i = 0; i < count; i++) {
707       hb_codepoint_t codepoint = unicode->mirroring (info[i].codepoint);
708       if (unlikely (codepoint != info[i].codepoint && c->font->has_glyph (codepoint)))
709 	info[i].codepoint = codepoint;
710       else
711 	info[i].mask |= rtlm_mask;
712     }
713   }
714 
715 #ifndef HB_NO_VERTICAL
716   if (HB_DIRECTION_IS_VERTICAL (c->target_direction) && !c->plan->has_vert)
717   {
718     for (unsigned int i = 0; i < count; i++) {
719       hb_codepoint_t codepoint = hb_vert_char_for (info[i].codepoint);
720       if (unlikely (codepoint != info[i].codepoint && c->font->has_glyph (codepoint)))
721 	info[i].codepoint = codepoint;
722     }
723   }
724 #endif
725 }
726 
727 static inline void
hb_ot_shape_setup_masks_fraction(const hb_ot_shape_context_t * c)728 hb_ot_shape_setup_masks_fraction (const hb_ot_shape_context_t *c)
729 {
730 #ifdef HB_NO_OT_SHAPE_FRACTIONS
731   return;
732 #endif
733 
734   if (!(c->buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_NON_ASCII) ||
735       !c->plan->has_frac)
736     return;
737 
738   hb_buffer_t *buffer = c->buffer;
739 
740   hb_mask_t pre_mask, post_mask;
741   if (HB_DIRECTION_IS_FORWARD (buffer->props.direction))
742   {
743     pre_mask = c->plan->numr_mask | c->plan->frac_mask;
744     post_mask = c->plan->frac_mask | c->plan->dnom_mask;
745   }
746   else
747   {
748     pre_mask = c->plan->frac_mask | c->plan->dnom_mask;
749     post_mask = c->plan->numr_mask | c->plan->frac_mask;
750   }
751 
752   unsigned int count = buffer->len;
753   hb_glyph_info_t *info = buffer->info;
754   for (unsigned int i = 0; i < count; i++)
755   {
756     if (info[i].codepoint == 0x2044u) /* FRACTION SLASH */
757     {
758       unsigned int start = i, end = i + 1;
759       while (start &&
760 	     _hb_glyph_info_get_general_category (&info[start - 1]) ==
761 	     HB_UNICODE_GENERAL_CATEGORY_DECIMAL_NUMBER)
762 	start--;
763       while (end < count &&
764 	     _hb_glyph_info_get_general_category (&info[end]) ==
765 	     HB_UNICODE_GENERAL_CATEGORY_DECIMAL_NUMBER)
766 	end++;
767 
768       buffer->unsafe_to_break (start, end);
769 
770       for (unsigned int j = start; j < i; j++)
771 	info[j].mask |= pre_mask;
772       info[i].mask |= c->plan->frac_mask;
773       for (unsigned int j = i + 1; j < end; j++)
774 	info[j].mask |= post_mask;
775 
776       i = end - 1;
777     }
778   }
779 }
780 
781 static inline void
hb_ot_shape_initialize_masks(const hb_ot_shape_context_t * c)782 hb_ot_shape_initialize_masks (const hb_ot_shape_context_t *c)
783 {
784   hb_ot_map_t *map = &c->plan->map;
785   hb_buffer_t *buffer = c->buffer;
786 
787   hb_mask_t global_mask = map->get_global_mask ();
788   buffer->reset_masks (global_mask);
789 }
790 
791 static inline void
hb_ot_shape_setup_masks(const hb_ot_shape_context_t * c)792 hb_ot_shape_setup_masks (const hb_ot_shape_context_t *c)
793 {
794   hb_ot_map_t *map = &c->plan->map;
795   hb_buffer_t *buffer = c->buffer;
796 
797   hb_ot_shape_setup_masks_fraction (c);
798 
799   if (c->plan->shaper->setup_masks)
800     c->plan->shaper->setup_masks (c->plan, buffer, c->font);
801 
802   for (unsigned int i = 0; i < c->num_user_features; i++)
803   {
804     const hb_feature_t *feature = &c->user_features[i];
805     if (!(feature->start == HB_FEATURE_GLOBAL_START && feature->end == HB_FEATURE_GLOBAL_END)) {
806       unsigned int shift;
807       hb_mask_t mask = map->get_mask (feature->tag, &shift);
808       buffer->set_masks (feature->value << shift, mask, feature->start, feature->end);
809     }
810   }
811 }
812 
813 static void
hb_ot_zero_width_default_ignorables(const hb_buffer_t * buffer)814 hb_ot_zero_width_default_ignorables (const hb_buffer_t *buffer)
815 {
816   if (!(buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_DEFAULT_IGNORABLES) ||
817       (buffer->flags & HB_BUFFER_FLAG_PRESERVE_DEFAULT_IGNORABLES) ||
818       (buffer->flags & HB_BUFFER_FLAG_REMOVE_DEFAULT_IGNORABLES))
819     return;
820 
821   unsigned int count = buffer->len;
822   hb_glyph_info_t *info = buffer->info;
823   hb_glyph_position_t *pos = buffer->pos;
824   unsigned int i = 0;
825   for (i = 0; i < count; i++)
826     if (unlikely (_hb_glyph_info_is_default_ignorable (&info[i])))
827       pos[i].x_advance = pos[i].y_advance = pos[i].x_offset = pos[i].y_offset = 0;
828 }
829 
830 static void
hb_ot_hide_default_ignorables(hb_buffer_t * buffer,hb_font_t * font)831 hb_ot_hide_default_ignorables (hb_buffer_t *buffer,
832 			       hb_font_t   *font)
833 {
834   if (!(buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_DEFAULT_IGNORABLES) ||
835       (buffer->flags & HB_BUFFER_FLAG_PRESERVE_DEFAULT_IGNORABLES))
836     return;
837 
838   unsigned int count = buffer->len;
839   hb_glyph_info_t *info = buffer->info;
840 
841   hb_codepoint_t invisible = buffer->invisible;
842   if (!(buffer->flags & HB_BUFFER_FLAG_REMOVE_DEFAULT_IGNORABLES) &&
843       (invisible || font->get_nominal_glyph (' ', &invisible)))
844   {
845     /* Replace default-ignorables with a zero-advance invisible glyph. */
846     for (unsigned int i = 0; i < count; i++)
847     {
848       if (_hb_glyph_info_is_default_ignorable (&info[i]))
849 	info[i].codepoint = invisible;
850     }
851   }
852   else
853     hb_ot_layout_delete_glyphs_inplace (buffer, _hb_glyph_info_is_default_ignorable);
854 }
855 
856 
857 static inline void
hb_ot_map_glyphs_fast(hb_buffer_t * buffer)858 hb_ot_map_glyphs_fast (hb_buffer_t  *buffer)
859 {
860   /* Normalization process sets up glyph_index(), we just copy it. */
861   unsigned int count = buffer->len;
862   hb_glyph_info_t *info = buffer->info;
863   for (unsigned int i = 0; i < count; i++)
864     info[i].codepoint = info[i].glyph_index();
865 
866   buffer->content_type = HB_BUFFER_CONTENT_TYPE_GLYPHS;
867 }
868 
869 static inline void
hb_synthesize_glyph_classes(hb_buffer_t * buffer)870 hb_synthesize_glyph_classes (hb_buffer_t *buffer)
871 {
872   unsigned int count = buffer->len;
873   hb_glyph_info_t *info = buffer->info;
874   for (unsigned int i = 0; i < count; i++)
875   {
876     hb_ot_layout_glyph_props_flags_t klass;
877 
878     /* Never mark default-ignorables as marks.
879      * They won't get in the way of lookups anyway,
880      * but having them as mark will cause them to be skipped
881      * over if the lookup-flag says so, but at least for the
882      * Mongolian variation selectors, looks like Uniscribe
883      * marks them as non-mark.  Some Mongolian fonts without
884      * GDEF rely on this.  Another notable character that
885      * this applies to is COMBINING GRAPHEME JOINER. */
886     klass = (_hb_glyph_info_get_general_category (&info[i]) !=
887 	     HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK ||
888 	     _hb_glyph_info_is_default_ignorable (&info[i])) ?
889 	    HB_OT_LAYOUT_GLYPH_PROPS_BASE_GLYPH :
890 	    HB_OT_LAYOUT_GLYPH_PROPS_MARK;
891     _hb_glyph_info_set_glyph_props (&info[i], klass);
892   }
893 }
894 
895 static inline void
hb_ot_substitute_default(const hb_ot_shape_context_t * c)896 hb_ot_substitute_default (const hb_ot_shape_context_t *c)
897 {
898   hb_buffer_t *buffer = c->buffer;
899 
900   hb_ot_rotate_chars (c);
901 
902   HB_BUFFER_ALLOCATE_VAR (buffer, glyph_index);
903 
904   _hb_ot_shape_normalize (c->plan, buffer, c->font);
905 
906   hb_ot_shape_setup_masks (c);
907 
908   /* This is unfortunate to go here, but necessary... */
909   if (c->plan->fallback_mark_positioning)
910     _hb_ot_shape_fallback_mark_position_recategorize_marks (c->plan, c->font, buffer);
911 
912   hb_ot_map_glyphs_fast (buffer);
913 
914   HB_BUFFER_DEALLOCATE_VAR (buffer, glyph_index);
915 }
916 
917 static inline void
hb_ot_substitute_complex(const hb_ot_shape_context_t * c)918 hb_ot_substitute_complex (const hb_ot_shape_context_t *c)
919 {
920   hb_buffer_t *buffer = c->buffer;
921 
922   hb_ot_layout_substitute_start (c->font, buffer);
923 
924   if (c->plan->fallback_glyph_classes)
925     hb_synthesize_glyph_classes (c->buffer);
926 
927   c->plan->substitute (c->font, buffer);
928 }
929 
930 static inline void
hb_ot_substitute_pre(const hb_ot_shape_context_t * c)931 hb_ot_substitute_pre (const hb_ot_shape_context_t *c)
932 {
933   hb_ot_substitute_default (c);
934 
935   _hb_buffer_allocate_gsubgpos_vars (c->buffer);
936 
937   hb_ot_substitute_complex (c);
938 }
939 
940 static inline void
hb_ot_substitute_post(const hb_ot_shape_context_t * c)941 hb_ot_substitute_post (const hb_ot_shape_context_t *c)
942 {
943   hb_ot_hide_default_ignorables (c->buffer, c->font);
944 #ifndef HB_NO_AAT_SHAPE
945   if (c->plan->apply_morx)
946     hb_aat_layout_remove_deleted_glyphs (c->buffer);
947 #endif
948 
949   if (c->plan->shaper->postprocess_glyphs &&
950     c->buffer->message(c->font, "start postprocess-glyphs")) {
951     c->plan->shaper->postprocess_glyphs (c->plan, c->buffer, c->font);
952     (void) c->buffer->message(c->font, "end postprocess-glyphs");
953   }
954 }
955 
956 
957 /*
958  * Position
959  */
960 
961 static inline void
adjust_mark_offsets(hb_glyph_position_t * pos)962 adjust_mark_offsets (hb_glyph_position_t *pos)
963 {
964   pos->x_offset -= pos->x_advance;
965   pos->y_offset -= pos->y_advance;
966 }
967 
968 static inline void
zero_mark_width(hb_glyph_position_t * pos)969 zero_mark_width (hb_glyph_position_t *pos)
970 {
971   pos->x_advance = 0;
972   pos->y_advance = 0;
973 }
974 
975 static inline void
zero_mark_widths_by_gdef(hb_buffer_t * buffer,bool adjust_offsets)976 zero_mark_widths_by_gdef (hb_buffer_t *buffer, bool adjust_offsets)
977 {
978   unsigned int count = buffer->len;
979   hb_glyph_info_t *info = buffer->info;
980   for (unsigned int i = 0; i < count; i++)
981     if (_hb_glyph_info_is_mark (&info[i]))
982     {
983       if (adjust_offsets)
984 	adjust_mark_offsets (&buffer->pos[i]);
985       zero_mark_width (&buffer->pos[i]);
986     }
987 }
988 
989 static inline void
hb_ot_position_default(const hb_ot_shape_context_t * c)990 hb_ot_position_default (const hb_ot_shape_context_t *c)
991 {
992   hb_direction_t direction = c->buffer->props.direction;
993   unsigned int count = c->buffer->len;
994   hb_glyph_info_t *info = c->buffer->info;
995   hb_glyph_position_t *pos = c->buffer->pos;
996 
997   if (HB_DIRECTION_IS_HORIZONTAL (direction))
998   {
999     c->font->get_glyph_h_advances (count, &info[0].codepoint, sizeof(info[0]),
1000 				   &pos[0].x_advance, sizeof(pos[0]));
1001     /* The nil glyph_h_origin() func returns 0, so no need to apply it. */
1002     if (c->font->has_glyph_h_origin_func ())
1003       for (unsigned int i = 0; i < count; i++)
1004 	c->font->subtract_glyph_h_origin (info[i].codepoint,
1005 					  &pos[i].x_offset,
1006 					  &pos[i].y_offset);
1007   }
1008   else
1009   {
1010     c->font->get_glyph_v_advances (count, &info[0].codepoint, sizeof(info[0]),
1011 				   &pos[0].y_advance, sizeof(pos[0]));
1012     for (unsigned int i = 0; i < count; i++)
1013     {
1014       c->font->subtract_glyph_v_origin (info[i].codepoint,
1015 					&pos[i].x_offset,
1016 					&pos[i].y_offset);
1017     }
1018   }
1019   if (c->buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_SPACE_FALLBACK)
1020     _hb_ot_shape_fallback_spaces (c->plan, c->font, c->buffer);
1021 }
1022 
1023 static inline void
hb_ot_position_complex(const hb_ot_shape_context_t * c)1024 hb_ot_position_complex (const hb_ot_shape_context_t *c)
1025 {
1026   unsigned int count = c->buffer->len;
1027   hb_glyph_info_t *info = c->buffer->info;
1028   hb_glyph_position_t *pos = c->buffer->pos;
1029 
1030   /* If the font has no GPOS and direction is forward, then when
1031    * zeroing mark widths, we shift the mark with it, such that the
1032    * mark is positioned hanging over the previous glyph.  When
1033    * direction is backward we don't shift and it will end up
1034    * hanging over the next glyph after the final reordering.
1035    *
1036    * Note: If fallback positinoing happens, we don't care about
1037    * this as it will be overridden.
1038    */
1039   bool adjust_offsets_when_zeroing = c->plan->adjust_mark_positioning_when_zeroing &&
1040 				     HB_DIRECTION_IS_FORWARD (c->buffer->props.direction);
1041 
1042   /* We change glyph origin to what GPOS expects (horizontal), apply GPOS, change it back. */
1043 
1044   /* The nil glyph_h_origin() func returns 0, so no need to apply it. */
1045   if (c->font->has_glyph_h_origin_func ())
1046     for (unsigned int i = 0; i < count; i++)
1047       c->font->add_glyph_h_origin (info[i].codepoint,
1048 				   &pos[i].x_offset,
1049 				   &pos[i].y_offset);
1050 
1051   hb_ot_layout_position_start (c->font, c->buffer);
1052 
1053   if (c->plan->zero_marks)
1054     switch (c->plan->shaper->zero_width_marks)
1055     {
1056       case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_EARLY:
1057 	zero_mark_widths_by_gdef (c->buffer, adjust_offsets_when_zeroing);
1058 	break;
1059 
1060       default:
1061       case HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE:
1062       case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_LATE:
1063 	break;
1064     }
1065 
1066   c->plan->position (c->font, c->buffer);
1067 
1068   if (c->plan->zero_marks)
1069     switch (c->plan->shaper->zero_width_marks)
1070     {
1071       case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_LATE:
1072 	zero_mark_widths_by_gdef (c->buffer, adjust_offsets_when_zeroing);
1073 	break;
1074 
1075       default:
1076       case HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE:
1077       case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_EARLY:
1078 	break;
1079     }
1080 
1081   /* Finish off.  Has to follow a certain order. */
1082   hb_ot_layout_position_finish_advances (c->font, c->buffer);
1083   hb_ot_zero_width_default_ignorables (c->buffer);
1084 #ifndef HB_NO_AAT_SHAPE
1085   if (c->plan->apply_morx)
1086     hb_aat_layout_zero_width_deleted_glyphs (c->buffer);
1087 #endif
1088   hb_ot_layout_position_finish_offsets (c->font, c->buffer);
1089 
1090   /* The nil glyph_h_origin() func returns 0, so no need to apply it. */
1091   if (c->font->has_glyph_h_origin_func ())
1092     for (unsigned int i = 0; i < count; i++)
1093       c->font->subtract_glyph_h_origin (info[i].codepoint,
1094 					&pos[i].x_offset,
1095 					&pos[i].y_offset);
1096 
1097   if (c->plan->fallback_mark_positioning)
1098     _hb_ot_shape_fallback_mark_position (c->plan, c->font, c->buffer,
1099 					 adjust_offsets_when_zeroing);
1100 }
1101 
1102 static inline void
hb_ot_position(const hb_ot_shape_context_t * c)1103 hb_ot_position (const hb_ot_shape_context_t *c)
1104 {
1105   c->buffer->clear_positions ();
1106 
1107   hb_ot_position_default (c);
1108 
1109   hb_ot_position_complex (c);
1110 
1111   if (HB_DIRECTION_IS_BACKWARD (c->buffer->props.direction))
1112     hb_buffer_reverse (c->buffer);
1113 
1114   _hb_buffer_deallocate_gsubgpos_vars (c->buffer);
1115 }
1116 
1117 static inline void
hb_propagate_flags(hb_buffer_t * buffer)1118 hb_propagate_flags (hb_buffer_t *buffer)
1119 {
1120   /* Propagate cluster-level glyph flags to be the same on all cluster glyphs.
1121    * Simplifies using them. */
1122 
1123   if (!(buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_GLYPH_FLAGS))
1124     return;
1125 
1126   hb_glyph_info_t *info = buffer->info;
1127 
1128   foreach_cluster (buffer, start, end)
1129   {
1130     unsigned int mask = 0;
1131     for (unsigned int i = start; i < end; i++)
1132       mask |= info[i].mask & HB_GLYPH_FLAG_DEFINED;
1133     if (mask)
1134       for (unsigned int i = start; i < end; i++)
1135 	info[i].mask |= mask;
1136   }
1137 }
1138 
1139 /* Pull it all together! */
1140 
1141 static void
hb_ot_shape_internal(hb_ot_shape_context_t * c)1142 hb_ot_shape_internal (hb_ot_shape_context_t *c)
1143 {
1144   c->buffer->enter ();
1145 
1146   /* Save the original direction, we use it later. */
1147   c->target_direction = c->buffer->props.direction;
1148 
1149   _hb_buffer_allocate_unicode_vars (c->buffer);
1150 
1151   hb_ot_shape_initialize_masks (c);
1152   hb_set_unicode_props (c->buffer);
1153   hb_insert_dotted_circle (c->buffer, c->font);
1154 
1155   hb_form_clusters (c->buffer);
1156 
1157   hb_ensure_native_direction (c->buffer);
1158 
1159   if (c->plan->shaper->preprocess_text &&
1160       c->buffer->message(c->font, "start preprocess-text"))
1161   {
1162     c->plan->shaper->preprocess_text (c->plan, c->buffer, c->font);
1163     (void) c->buffer->message(c->font, "end preprocess-text");
1164   }
1165 
1166   hb_ot_substitute_pre (c);
1167   hb_ot_position (c);
1168   hb_ot_substitute_post (c);
1169 
1170   hb_propagate_flags (c->buffer);
1171 
1172   _hb_buffer_deallocate_unicode_vars (c->buffer);
1173 
1174   c->buffer->props.direction = c->target_direction;
1175 
1176   c->buffer->leave ();
1177 }
1178 
1179 
1180 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)1181 _hb_ot_shape (hb_shape_plan_t    *shape_plan,
1182 	      hb_font_t          *font,
1183 	      hb_buffer_t        *buffer,
1184 	      const hb_feature_t *features,
1185 	      unsigned int        num_features)
1186 {
1187   hb_ot_shape_context_t c = {&shape_plan->ot, font, font->face, buffer, features, num_features};
1188   hb_ot_shape_internal (&c);
1189 
1190   return true;
1191 }
1192 
1193 
1194 /**
1195  * hb_ot_shape_plan_collect_lookups:
1196  * @shape_plan: #hb_shape_plan_t to query
1197  * @table_tag: GSUB or GPOS
1198  * @lookup_indexes: (out): The #hb_set_t set of lookups returned
1199  *
1200  * Computes the complete set of GSUB or GPOS lookups that are applicable
1201  * under a given @shape_plan.
1202  *
1203  * Since: 0.9.7
1204  **/
1205 void
hb_ot_shape_plan_collect_lookups(hb_shape_plan_t * shape_plan,hb_tag_t table_tag,hb_set_t * lookup_indexes)1206 hb_ot_shape_plan_collect_lookups (hb_shape_plan_t *shape_plan,
1207 				  hb_tag_t         table_tag,
1208 				  hb_set_t        *lookup_indexes /* OUT */)
1209 {
1210   shape_plan->ot.collect_lookups (table_tag, lookup_indexes);
1211 }
1212 
1213 
1214 /* TODO Move this to hb-ot-shape-normalize, make it do decompose, and make it public. */
1215 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)1216 add_char (hb_font_t          *font,
1217 	  hb_unicode_funcs_t *unicode,
1218 	  hb_bool_t           mirror,
1219 	  hb_codepoint_t      u,
1220 	  hb_set_t           *glyphs)
1221 {
1222   hb_codepoint_t glyph;
1223   if (font->get_nominal_glyph (u, &glyph))
1224     glyphs->add (glyph);
1225   if (mirror)
1226   {
1227     hb_codepoint_t m = unicode->mirroring (u);
1228     if (m != u && font->get_nominal_glyph (m, &glyph))
1229       glyphs->add (glyph);
1230   }
1231 }
1232 
1233 
1234 /**
1235  * hb_ot_shape_glyphs_closure:
1236  * @font: #hb_font_t to work upon
1237  * @buffer: The input buffer to compute from
1238  * @features: (array length=num_features): The features enabled on the buffer
1239  * @num_features: The number of features enabled on the buffer
1240  * @glyphs: (out): The #hb_set_t set of glyphs comprising the transitive closure of the query
1241  *
1242  * Computes the transitive closure of glyphs needed for a specified
1243  * input buffer under the given font and feature list. The closure is
1244  * computed as a set, not as a list.
1245  *
1246  * Since: 0.9.2
1247  **/
1248 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)1249 hb_ot_shape_glyphs_closure (hb_font_t          *font,
1250 			    hb_buffer_t        *buffer,
1251 			    const hb_feature_t *features,
1252 			    unsigned int        num_features,
1253 			    hb_set_t           *glyphs)
1254 {
1255   const char *shapers[] = {"ot", nullptr};
1256   hb_shape_plan_t *shape_plan = hb_shape_plan_create_cached (font->face, &buffer->props,
1257 							     features, num_features, shapers);
1258 
1259   bool mirror = hb_script_get_horizontal_direction (buffer->props.script) == HB_DIRECTION_RTL;
1260 
1261   unsigned int count = buffer->len;
1262   hb_glyph_info_t *info = buffer->info;
1263   for (unsigned int i = 0; i < count; i++)
1264     add_char (font, buffer->unicode, mirror, info[i].codepoint, glyphs);
1265 
1266   hb_set_t *lookups = hb_set_create ();
1267   hb_ot_shape_plan_collect_lookups (shape_plan, HB_OT_TAG_GSUB, lookups);
1268   hb_ot_layout_lookups_substitute_closure (font->face, lookups, glyphs);
1269 
1270   hb_set_destroy (lookups);
1271 
1272   hb_shape_plan_destroy (shape_plan);
1273 }
1274 
1275 
1276 #endif
1277