• 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.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->swap_buffers ();
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 
632     if (buffer->cluster_level == HB_BUFFER_CLUSTER_LEVEL_MONOTONE_CHARACTERS)
633       foreach_grapheme (buffer, start, end)
634       {
635 	buffer->merge_clusters (start, end);
636 	buffer->reverse_range (start, end);
637       }
638     else
639       foreach_grapheme (buffer, start, end)
640 	/* form_clusters() merged clusters already, we don't merge. */
641 	buffer->reverse_range (start, end);
642 
643     buffer->reverse ();
644 
645     buffer->props.direction = HB_DIRECTION_REVERSE (buffer->props.direction);
646   }
647 }
648 
649 
650 /*
651  * Substitute
652  */
653 
654 static hb_codepoint_t
hb_vert_char_for(hb_codepoint_t u)655 hb_vert_char_for (hb_codepoint_t u)
656 {
657   switch (u >> 8)
658   {
659     case 0x20: switch (u) {
660       case 0x2013u: return 0xfe32u; // EN DASH
661       case 0x2014u: return 0xfe31u; // EM DASH
662       case 0x2025u: return 0xfe30u; // TWO DOT LEADER
663       case 0x2026u: return 0xfe19u; // HORIZONTAL ELLIPSIS
664     } break;
665     case 0x30: switch (u) {
666       case 0x3001u: return 0xfe11u; // IDEOGRAPHIC COMMA
667       case 0x3002u: return 0xfe12u; // IDEOGRAPHIC FULL STOP
668       case 0x3008u: return 0xfe3fu; // LEFT ANGLE BRACKET
669       case 0x3009u: return 0xfe40u; // RIGHT ANGLE BRACKET
670       case 0x300au: return 0xfe3du; // LEFT DOUBLE ANGLE BRACKET
671       case 0x300bu: return 0xfe3eu; // RIGHT DOUBLE ANGLE BRACKET
672       case 0x300cu: return 0xfe41u; // LEFT CORNER BRACKET
673       case 0x300du: return 0xfe42u; // RIGHT CORNER BRACKET
674       case 0x300eu: return 0xfe43u; // LEFT WHITE CORNER BRACKET
675       case 0x300fu: return 0xfe44u; // RIGHT WHITE CORNER BRACKET
676       case 0x3010u: return 0xfe3bu; // LEFT BLACK LENTICULAR BRACKET
677       case 0x3011u: return 0xfe3cu; // RIGHT BLACK LENTICULAR BRACKET
678       case 0x3014u: return 0xfe39u; // LEFT TORTOISE SHELL BRACKET
679       case 0x3015u: return 0xfe3au; // RIGHT TORTOISE SHELL BRACKET
680       case 0x3016u: return 0xfe17u; // LEFT WHITE LENTICULAR BRACKET
681       case 0x3017u: return 0xfe18u; // RIGHT WHITE LENTICULAR BRACKET
682     } break;
683     case 0xfe: switch (u) {
684       case 0xfe4fu: return 0xfe34u; // WAVY LOW LINE
685     } break;
686     case 0xff: switch (u) {
687       case 0xff01u: return 0xfe15u; // FULLWIDTH EXCLAMATION MARK
688       case 0xff08u: return 0xfe35u; // FULLWIDTH LEFT PARENTHESIS
689       case 0xff09u: return 0xfe36u; // FULLWIDTH RIGHT PARENTHESIS
690       case 0xff0cu: return 0xfe10u; // FULLWIDTH COMMA
691       case 0xff1au: return 0xfe13u; // FULLWIDTH COLON
692       case 0xff1bu: return 0xfe14u; // FULLWIDTH SEMICOLON
693       case 0xff1fu: return 0xfe16u; // FULLWIDTH QUESTION MARK
694       case 0xff3bu: return 0xfe47u; // FULLWIDTH LEFT SQUARE BRACKET
695       case 0xff3du: return 0xfe48u; // FULLWIDTH RIGHT SQUARE BRACKET
696       case 0xff3fu: return 0xfe33u; // FULLWIDTH LOW LINE
697       case 0xff5bu: return 0xfe37u; // FULLWIDTH LEFT CURLY BRACKET
698       case 0xff5du: return 0xfe38u; // FULLWIDTH RIGHT CURLY BRACKET
699     } break;
700   }
701 
702   return u;
703 }
704 
705 static inline void
hb_ot_rotate_chars(const hb_ot_shape_context_t * c)706 hb_ot_rotate_chars (const hb_ot_shape_context_t *c)
707 {
708   hb_buffer_t *buffer = c->buffer;
709   unsigned int count = buffer->len;
710   hb_glyph_info_t *info = buffer->info;
711 
712   if (HB_DIRECTION_IS_BACKWARD (c->target_direction))
713   {
714     hb_unicode_funcs_t *unicode = buffer->unicode;
715     hb_mask_t rtlm_mask = c->plan->rtlm_mask;
716 
717     for (unsigned int i = 0; i < count; i++) {
718       hb_codepoint_t codepoint = unicode->mirroring (info[i].codepoint);
719       if (unlikely (codepoint != info[i].codepoint && c->font->has_glyph (codepoint)))
720 	info[i].codepoint = codepoint;
721       else
722 	info[i].mask |= rtlm_mask;
723     }
724   }
725 
726   if (HB_DIRECTION_IS_VERTICAL (c->target_direction) && !c->plan->has_vert)
727   {
728     for (unsigned int i = 0; i < count; i++) {
729       hb_codepoint_t codepoint = hb_vert_char_for (info[i].codepoint);
730       if (unlikely (codepoint != info[i].codepoint && c->font->has_glyph (codepoint)))
731 	info[i].codepoint = codepoint;
732     }
733   }
734 }
735 
736 static inline void
hb_ot_shape_setup_masks_fraction(const hb_ot_shape_context_t * c)737 hb_ot_shape_setup_masks_fraction (const hb_ot_shape_context_t *c)
738 {
739 #ifdef HB_NO_OT_SHAPE_FRACTIONS
740   return;
741 #endif
742 
743   if (!(c->buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_NON_ASCII) ||
744       !c->plan->has_frac)
745     return;
746 
747   hb_buffer_t *buffer = c->buffer;
748 
749   hb_mask_t pre_mask, post_mask;
750   if (HB_DIRECTION_IS_FORWARD (buffer->props.direction))
751   {
752     pre_mask = c->plan->numr_mask | c->plan->frac_mask;
753     post_mask = c->plan->frac_mask | c->plan->dnom_mask;
754   }
755   else
756   {
757     pre_mask = c->plan->frac_mask | c->plan->dnom_mask;
758     post_mask = c->plan->numr_mask | c->plan->frac_mask;
759   }
760 
761   unsigned int count = buffer->len;
762   hb_glyph_info_t *info = buffer->info;
763   for (unsigned int i = 0; i < count; i++)
764   {
765     if (info[i].codepoint == 0x2044u) /* FRACTION SLASH */
766     {
767       unsigned int start = i, end = i + 1;
768       while (start &&
769 	     _hb_glyph_info_get_general_category (&info[start - 1]) ==
770 	     HB_UNICODE_GENERAL_CATEGORY_DECIMAL_NUMBER)
771 	start--;
772       while (end < count &&
773 	     _hb_glyph_info_get_general_category (&info[end]) ==
774 	     HB_UNICODE_GENERAL_CATEGORY_DECIMAL_NUMBER)
775 	end++;
776 
777       buffer->unsafe_to_break (start, end);
778 
779       for (unsigned int j = start; j < i; j++)
780 	info[j].mask |= pre_mask;
781       info[i].mask |= c->plan->frac_mask;
782       for (unsigned int j = i + 1; j < end; j++)
783 	info[j].mask |= post_mask;
784 
785       i = end - 1;
786     }
787   }
788 }
789 
790 static inline void
hb_ot_shape_initialize_masks(const hb_ot_shape_context_t * c)791 hb_ot_shape_initialize_masks (const hb_ot_shape_context_t *c)
792 {
793   hb_ot_map_t *map = &c->plan->map;
794   hb_buffer_t *buffer = c->buffer;
795 
796   hb_mask_t global_mask = map->get_global_mask ();
797   buffer->reset_masks (global_mask);
798 }
799 
800 static inline void
hb_ot_shape_setup_masks(const hb_ot_shape_context_t * c)801 hb_ot_shape_setup_masks (const hb_ot_shape_context_t *c)
802 {
803   hb_ot_map_t *map = &c->plan->map;
804   hb_buffer_t *buffer = c->buffer;
805 
806   hb_ot_shape_setup_masks_fraction (c);
807 
808   if (c->plan->shaper->setup_masks)
809     c->plan->shaper->setup_masks (c->plan, buffer, c->font);
810 
811   for (unsigned int i = 0; i < c->num_user_features; i++)
812   {
813     const hb_feature_t *feature = &c->user_features[i];
814     if (!(feature->start == HB_FEATURE_GLOBAL_START && feature->end == HB_FEATURE_GLOBAL_END)) {
815       unsigned int shift;
816       hb_mask_t mask = map->get_mask (feature->tag, &shift);
817       buffer->set_masks (feature->value << shift, mask, feature->start, feature->end);
818     }
819   }
820 }
821 
822 static void
hb_ot_zero_width_default_ignorables(const hb_buffer_t * buffer)823 hb_ot_zero_width_default_ignorables (const hb_buffer_t *buffer)
824 {
825   if (!(buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_DEFAULT_IGNORABLES) ||
826       (buffer->flags & HB_BUFFER_FLAG_PRESERVE_DEFAULT_IGNORABLES) ||
827       (buffer->flags & HB_BUFFER_FLAG_REMOVE_DEFAULT_IGNORABLES))
828     return;
829 
830   unsigned int count = buffer->len;
831   hb_glyph_info_t *info = buffer->info;
832   hb_glyph_position_t *pos = buffer->pos;
833   unsigned int i = 0;
834   for (i = 0; i < count; i++)
835     if (unlikely (_hb_glyph_info_is_default_ignorable (&info[i])))
836       pos[i].x_advance = pos[i].y_advance = pos[i].x_offset = pos[i].y_offset = 0;
837 }
838 
839 static void
hb_ot_hide_default_ignorables(hb_buffer_t * buffer,hb_font_t * font)840 hb_ot_hide_default_ignorables (hb_buffer_t *buffer,
841 			       hb_font_t   *font)
842 {
843   if (!(buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_DEFAULT_IGNORABLES) ||
844       (buffer->flags & HB_BUFFER_FLAG_PRESERVE_DEFAULT_IGNORABLES))
845     return;
846 
847   unsigned int count = buffer->len;
848   hb_glyph_info_t *info = buffer->info;
849 
850   hb_codepoint_t invisible = buffer->invisible;
851   if (!(buffer->flags & HB_BUFFER_FLAG_REMOVE_DEFAULT_IGNORABLES) &&
852       (invisible || font->get_nominal_glyph (' ', &invisible)))
853   {
854     /* Replace default-ignorables with a zero-advance invisible glyph. */
855     for (unsigned int i = 0; i < count; i++)
856     {
857       if (_hb_glyph_info_is_default_ignorable (&info[i]))
858 	info[i].codepoint = invisible;
859     }
860   }
861   else
862     hb_ot_layout_delete_glyphs_inplace (buffer, _hb_glyph_info_is_default_ignorable);
863 }
864 
865 
866 static inline void
hb_ot_map_glyphs_fast(hb_buffer_t * buffer)867 hb_ot_map_glyphs_fast (hb_buffer_t  *buffer)
868 {
869   /* Normalization process sets up glyph_index(), we just copy it. */
870   unsigned int count = buffer->len;
871   hb_glyph_info_t *info = buffer->info;
872   for (unsigned int i = 0; i < count; i++)
873     info[i].codepoint = info[i].glyph_index();
874 
875   buffer->content_type = HB_BUFFER_CONTENT_TYPE_GLYPHS;
876 }
877 
878 static inline void
hb_synthesize_glyph_classes(hb_buffer_t * buffer)879 hb_synthesize_glyph_classes (hb_buffer_t *buffer)
880 {
881   unsigned int count = buffer->len;
882   hb_glyph_info_t *info = buffer->info;
883   for (unsigned int i = 0; i < count; i++)
884   {
885     hb_ot_layout_glyph_props_flags_t klass;
886 
887     /* Never mark default-ignorables as marks.
888      * They won't get in the way of lookups anyway,
889      * but having them as mark will cause them to be skipped
890      * over if the lookup-flag says so, but at least for the
891      * Mongolian variation selectors, looks like Uniscribe
892      * marks them as non-mark.  Some Mongolian fonts without
893      * GDEF rely on this.  Another notable character that
894      * this applies to is COMBINING GRAPHEME JOINER. */
895     klass = (_hb_glyph_info_get_general_category (&info[i]) !=
896 	     HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK ||
897 	     _hb_glyph_info_is_default_ignorable (&info[i])) ?
898 	    HB_OT_LAYOUT_GLYPH_PROPS_BASE_GLYPH :
899 	    HB_OT_LAYOUT_GLYPH_PROPS_MARK;
900     _hb_glyph_info_set_glyph_props (&info[i], klass);
901   }
902 }
903 
904 static inline void
hb_ot_substitute_default(const hb_ot_shape_context_t * c)905 hb_ot_substitute_default (const hb_ot_shape_context_t *c)
906 {
907   hb_buffer_t *buffer = c->buffer;
908 
909   hb_ot_rotate_chars (c);
910 
911   HB_BUFFER_ALLOCATE_VAR (buffer, glyph_index);
912 
913   _hb_ot_shape_normalize (c->plan, buffer, c->font);
914 
915   hb_ot_shape_setup_masks (c);
916 
917   /* This is unfortunate to go here, but necessary... */
918   if (c->plan->fallback_mark_positioning)
919     _hb_ot_shape_fallback_mark_position_recategorize_marks (c->plan, c->font, buffer);
920 
921   hb_ot_map_glyphs_fast (buffer);
922 
923   HB_BUFFER_DEALLOCATE_VAR (buffer, glyph_index);
924 }
925 
926 static inline void
hb_ot_substitute_complex(const hb_ot_shape_context_t * c)927 hb_ot_substitute_complex (const hb_ot_shape_context_t *c)
928 {
929   hb_buffer_t *buffer = c->buffer;
930 
931   hb_ot_layout_substitute_start (c->font, buffer);
932 
933   if (c->plan->fallback_glyph_classes)
934     hb_synthesize_glyph_classes (c->buffer);
935 
936   c->plan->substitute (c->font, buffer);
937 }
938 
939 static inline void
hb_ot_substitute_pre(const hb_ot_shape_context_t * c)940 hb_ot_substitute_pre (const hb_ot_shape_context_t *c)
941 {
942   hb_ot_substitute_default (c);
943 
944   _hb_buffer_allocate_gsubgpos_vars (c->buffer);
945 
946   hb_ot_substitute_complex (c);
947 }
948 
949 static inline void
hb_ot_substitute_post(const hb_ot_shape_context_t * c)950 hb_ot_substitute_post (const hb_ot_shape_context_t *c)
951 {
952   hb_ot_hide_default_ignorables (c->buffer, c->font);
953 #ifndef HB_NO_AAT_SHAPE
954   if (c->plan->apply_morx)
955     hb_aat_layout_remove_deleted_glyphs (c->buffer);
956 #endif
957 
958   if (c->plan->shaper->postprocess_glyphs &&
959     c->buffer->message(c->font, "start postprocess-glyphs")) {
960     c->plan->shaper->postprocess_glyphs (c->plan, c->buffer, c->font);
961     (void) c->buffer->message(c->font, "end postprocess-glyphs");
962   }
963 }
964 
965 
966 /*
967  * Position
968  */
969 
970 static inline void
adjust_mark_offsets(hb_glyph_position_t * pos)971 adjust_mark_offsets (hb_glyph_position_t *pos)
972 {
973   pos->x_offset -= pos->x_advance;
974   pos->y_offset -= pos->y_advance;
975 }
976 
977 static inline void
zero_mark_width(hb_glyph_position_t * pos)978 zero_mark_width (hb_glyph_position_t *pos)
979 {
980   pos->x_advance = 0;
981   pos->y_advance = 0;
982 }
983 
984 static inline void
zero_mark_widths_by_gdef(hb_buffer_t * buffer,bool adjust_offsets)985 zero_mark_widths_by_gdef (hb_buffer_t *buffer, bool adjust_offsets)
986 {
987   unsigned int count = buffer->len;
988   hb_glyph_info_t *info = buffer->info;
989   for (unsigned int i = 0; i < count; i++)
990     if (_hb_glyph_info_is_mark (&info[i]))
991     {
992       if (adjust_offsets)
993 	adjust_mark_offsets (&buffer->pos[i]);
994       zero_mark_width (&buffer->pos[i]);
995     }
996 }
997 
998 static inline void
hb_ot_position_default(const hb_ot_shape_context_t * c)999 hb_ot_position_default (const hb_ot_shape_context_t *c)
1000 {
1001   hb_direction_t direction = c->buffer->props.direction;
1002   unsigned int count = c->buffer->len;
1003   hb_glyph_info_t *info = c->buffer->info;
1004   hb_glyph_position_t *pos = c->buffer->pos;
1005 
1006   if (HB_DIRECTION_IS_HORIZONTAL (direction))
1007   {
1008     c->font->get_glyph_h_advances (count, &info[0].codepoint, sizeof(info[0]),
1009 				   &pos[0].x_advance, sizeof(pos[0]));
1010     /* The nil glyph_h_origin() func returns 0, so no need to apply it. */
1011     if (c->font->has_glyph_h_origin_func ())
1012       for (unsigned int i = 0; i < count; i++)
1013 	c->font->subtract_glyph_h_origin (info[i].codepoint,
1014 					  &pos[i].x_offset,
1015 					  &pos[i].y_offset);
1016   }
1017   else
1018   {
1019     c->font->get_glyph_v_advances (count, &info[0].codepoint, sizeof(info[0]),
1020 				   &pos[0].y_advance, sizeof(pos[0]));
1021     for (unsigned int i = 0; i < count; i++)
1022     {
1023       c->font->subtract_glyph_v_origin (info[i].codepoint,
1024 					&pos[i].x_offset,
1025 					&pos[i].y_offset);
1026     }
1027   }
1028   if (c->buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_SPACE_FALLBACK)
1029     _hb_ot_shape_fallback_spaces (c->plan, c->font, c->buffer);
1030 }
1031 
1032 static inline void
hb_ot_position_complex(const hb_ot_shape_context_t * c)1033 hb_ot_position_complex (const hb_ot_shape_context_t *c)
1034 {
1035   unsigned int count = c->buffer->len;
1036   hb_glyph_info_t *info = c->buffer->info;
1037   hb_glyph_position_t *pos = c->buffer->pos;
1038 
1039   /* If the font has no GPOS and direction is forward, then when
1040    * zeroing mark widths, we shift the mark with it, such that the
1041    * mark is positioned hanging over the previous glyph.  When
1042    * direction is backward we don't shift and it will end up
1043    * hanging over the next glyph after the final reordering.
1044    *
1045    * Note: If fallback positinoing happens, we don't care about
1046    * this as it will be overriden.
1047    */
1048   bool adjust_offsets_when_zeroing = c->plan->adjust_mark_positioning_when_zeroing &&
1049 				     HB_DIRECTION_IS_FORWARD (c->buffer->props.direction);
1050 
1051   /* We change glyph origin to what GPOS expects (horizontal), apply GPOS, change it back. */
1052 
1053   /* The nil glyph_h_origin() func returns 0, so no need to apply it. */
1054   if (c->font->has_glyph_h_origin_func ())
1055     for (unsigned int i = 0; i < count; i++)
1056       c->font->add_glyph_h_origin (info[i].codepoint,
1057 				   &pos[i].x_offset,
1058 				   &pos[i].y_offset);
1059 
1060   hb_ot_layout_position_start (c->font, c->buffer);
1061 
1062   if (c->plan->zero_marks)
1063     switch (c->plan->shaper->zero_width_marks)
1064     {
1065       case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_EARLY:
1066 	zero_mark_widths_by_gdef (c->buffer, adjust_offsets_when_zeroing);
1067 	break;
1068 
1069       default:
1070       case HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE:
1071       case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_LATE:
1072 	break;
1073     }
1074 
1075   c->plan->position (c->font, c->buffer);
1076 
1077   if (c->plan->zero_marks)
1078     switch (c->plan->shaper->zero_width_marks)
1079     {
1080       case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_LATE:
1081 	zero_mark_widths_by_gdef (c->buffer, adjust_offsets_when_zeroing);
1082 	break;
1083 
1084       default:
1085       case HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE:
1086       case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_EARLY:
1087 	break;
1088     }
1089 
1090   /* Finish off.  Has to follow a certain order. */
1091   hb_ot_layout_position_finish_advances (c->font, c->buffer);
1092   hb_ot_zero_width_default_ignorables (c->buffer);
1093 #ifndef HB_NO_AAT_SHAPE
1094   if (c->plan->apply_morx)
1095     hb_aat_layout_zero_width_deleted_glyphs (c->buffer);
1096 #endif
1097   hb_ot_layout_position_finish_offsets (c->font, c->buffer);
1098 
1099   /* The nil glyph_h_origin() func returns 0, so no need to apply it. */
1100   if (c->font->has_glyph_h_origin_func ())
1101     for (unsigned int i = 0; i < count; i++)
1102       c->font->subtract_glyph_h_origin (info[i].codepoint,
1103 					&pos[i].x_offset,
1104 					&pos[i].y_offset);
1105 
1106   if (c->plan->fallback_mark_positioning)
1107     _hb_ot_shape_fallback_mark_position (c->plan, c->font, c->buffer,
1108 					 adjust_offsets_when_zeroing);
1109 }
1110 
1111 static inline void
hb_ot_position(const hb_ot_shape_context_t * c)1112 hb_ot_position (const hb_ot_shape_context_t *c)
1113 {
1114   c->buffer->clear_positions ();
1115 
1116   hb_ot_position_default (c);
1117 
1118   hb_ot_position_complex (c);
1119 
1120   if (HB_DIRECTION_IS_BACKWARD (c->buffer->props.direction))
1121     hb_buffer_reverse (c->buffer);
1122 
1123   _hb_buffer_deallocate_gsubgpos_vars (c->buffer);
1124 }
1125 
1126 static inline void
hb_propagate_flags(hb_buffer_t * buffer)1127 hb_propagate_flags (hb_buffer_t *buffer)
1128 {
1129   /* Propagate cluster-level glyph flags to be the same on all cluster glyphs.
1130    * Simplifies using them. */
1131 
1132   if (!(buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_UNSAFE_TO_BREAK))
1133     return;
1134 
1135   hb_glyph_info_t *info = buffer->info;
1136 
1137   foreach_cluster (buffer, start, end)
1138   {
1139     unsigned int mask = 0;
1140     for (unsigned int i = start; i < end; i++)
1141       if (info[i].mask & HB_GLYPH_FLAG_UNSAFE_TO_BREAK)
1142       {
1143 	 mask = HB_GLYPH_FLAG_UNSAFE_TO_BREAK;
1144 	 break;
1145       }
1146     if (mask)
1147       for (unsigned int i = start; i < end; i++)
1148 	info[i].mask |= mask;
1149   }
1150 }
1151 
1152 /* Pull it all together! */
1153 
1154 static void
hb_ot_shape_internal(hb_ot_shape_context_t * c)1155 hb_ot_shape_internal (hb_ot_shape_context_t *c)
1156 {
1157   c->buffer->deallocate_var_all ();
1158   c->buffer->scratch_flags = HB_BUFFER_SCRATCH_FLAG_DEFAULT;
1159   if (likely (!hb_unsigned_mul_overflows (c->buffer->len, HB_BUFFER_MAX_LEN_FACTOR)))
1160   {
1161     c->buffer->max_len = hb_max (c->buffer->len * HB_BUFFER_MAX_LEN_FACTOR,
1162 				 (unsigned) HB_BUFFER_MAX_LEN_MIN);
1163   }
1164   if (likely (!hb_unsigned_mul_overflows (c->buffer->len, HB_BUFFER_MAX_OPS_FACTOR)))
1165   {
1166     c->buffer->max_ops = hb_max (c->buffer->len * HB_BUFFER_MAX_OPS_FACTOR,
1167 				 (unsigned) HB_BUFFER_MAX_OPS_MIN);
1168   }
1169 
1170   /* Save the original direction, we use it later. */
1171   c->target_direction = c->buffer->props.direction;
1172 
1173   _hb_buffer_allocate_unicode_vars (c->buffer);
1174 
1175   hb_ot_shape_initialize_masks (c);
1176   hb_set_unicode_props (c->buffer);
1177   hb_insert_dotted_circle (c->buffer, c->font);
1178 
1179   hb_form_clusters (c->buffer);
1180 
1181   hb_ensure_native_direction (c->buffer);
1182 
1183   if (c->plan->shaper->preprocess_text &&
1184       c->buffer->message(c->font, "start preprocess-text"))
1185   {
1186     c->plan->shaper->preprocess_text (c->plan, c->buffer, c->font);
1187     (void) c->buffer->message(c->font, "end preprocess-text");
1188   }
1189 
1190   hb_ot_substitute_pre (c);
1191   hb_ot_position (c);
1192   hb_ot_substitute_post (c);
1193 
1194   hb_propagate_flags (c->buffer);
1195 
1196   _hb_buffer_deallocate_unicode_vars (c->buffer);
1197 
1198   c->buffer->props.direction = c->target_direction;
1199 
1200   c->buffer->max_len = HB_BUFFER_MAX_LEN_DEFAULT;
1201   c->buffer->max_ops = HB_BUFFER_MAX_OPS_DEFAULT;
1202   c->buffer->deallocate_var_all ();
1203 }
1204 
1205 
1206 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)1207 _hb_ot_shape (hb_shape_plan_t    *shape_plan,
1208 	      hb_font_t          *font,
1209 	      hb_buffer_t        *buffer,
1210 	      const hb_feature_t *features,
1211 	      unsigned int        num_features)
1212 {
1213   hb_ot_shape_context_t c = {&shape_plan->ot, font, font->face, buffer, features, num_features};
1214   hb_ot_shape_internal (&c);
1215 
1216   return true;
1217 }
1218 
1219 
1220 /**
1221  * hb_ot_shape_plan_collect_lookups:
1222  * @shape_plan: #hb_shape_plan_t to query
1223  * @table_tag: GSUB or GPOS
1224  * @lookup_indexes: (out): The #hb_set_t set of lookups returned
1225  *
1226  * Computes the complete set of GSUB or GPOS lookups that are applicable
1227  * under a given @shape_plan.
1228  *
1229  * Since: 0.9.7
1230  **/
1231 void
hb_ot_shape_plan_collect_lookups(hb_shape_plan_t * shape_plan,hb_tag_t table_tag,hb_set_t * lookup_indexes)1232 hb_ot_shape_plan_collect_lookups (hb_shape_plan_t *shape_plan,
1233 				  hb_tag_t         table_tag,
1234 				  hb_set_t        *lookup_indexes /* OUT */)
1235 {
1236   shape_plan->ot.collect_lookups (table_tag, lookup_indexes);
1237 }
1238 
1239 
1240 /* TODO Move this to hb-ot-shape-normalize, make it do decompose, and make it public. */
1241 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)1242 add_char (hb_font_t          *font,
1243 	  hb_unicode_funcs_t *unicode,
1244 	  hb_bool_t           mirror,
1245 	  hb_codepoint_t      u,
1246 	  hb_set_t           *glyphs)
1247 {
1248   hb_codepoint_t glyph;
1249   if (font->get_nominal_glyph (u, &glyph))
1250     glyphs->add (glyph);
1251   if (mirror)
1252   {
1253     hb_codepoint_t m = unicode->mirroring (u);
1254     if (m != u && font->get_nominal_glyph (m, &glyph))
1255       glyphs->add (glyph);
1256   }
1257 }
1258 
1259 
1260 /**
1261  * hb_ot_shape_glyphs_closure:
1262  * @font: #hb_font_t to work upon
1263  * @buffer: The input buffer to compute from
1264  * @features: (array length=num_features): The features enabled on the buffer
1265  * @num_features: The number of features enabled on the buffer
1266  * @glyphs: (out): The #hb_set_t set of glyphs comprising the transitive closure of the query
1267  *
1268  * Computes the transitive closure of glyphs needed for a specified
1269  * input buffer under the given font and feature list. The closure is
1270  * computed as a set, not as a list.
1271  *
1272  * Since: 0.9.2
1273  **/
1274 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)1275 hb_ot_shape_glyphs_closure (hb_font_t          *font,
1276 			    hb_buffer_t        *buffer,
1277 			    const hb_feature_t *features,
1278 			    unsigned int        num_features,
1279 			    hb_set_t           *glyphs)
1280 {
1281   const char *shapers[] = {"ot", nullptr};
1282   hb_shape_plan_t *shape_plan = hb_shape_plan_create_cached (font->face, &buffer->props,
1283 							     features, num_features, shapers);
1284 
1285   bool mirror = hb_script_get_horizontal_direction (buffer->props.script) == HB_DIRECTION_RTL;
1286 
1287   unsigned int count = buffer->len;
1288   hb_glyph_info_t *info = buffer->info;
1289   for (unsigned int i = 0; i < count; i++)
1290     add_char (font, buffer->unicode, mirror, info[i].codepoint, glyphs);
1291 
1292   hb_set_t *lookups = hb_set_create ();
1293   hb_ot_shape_plan_collect_lookups (shape_plan, HB_OT_TAG_GSUB, lookups);
1294   hb_ot_layout_lookups_substitute_closure (font->face, lookups, glyphs);
1295 
1296   hb_set_destroy (lookups);
1297 
1298   hb_shape_plan_destroy (shape_plan);
1299 }
1300 
1301 
1302 #endif
1303