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