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