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