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
52static inline bool
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
71static void
72hb_ot_shape_collect_features (hb_ot_shape_planner_t *planner,
73 const hb_feature_t *user_features,
74 unsigned int num_user_features);
75
76hb_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
96void
97hb_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
201bool
202hb_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
235void
236hb_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
247void
248hb_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
259void
260hb_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
283static const hb_ot_map_feature_t
284common_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
296static const hb_ot_map_feature_t
297horizontal_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
308static void
309hb_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
403struct hb_ot_face_data_t {};
404
405hb_ot_face_data_t *
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
411void
412_hb_ot_shaper_face_data_destroy (hb_ot_face_data_t *data)
413{
414}
415
416
417/*
418 * shaper font data
419 */
420
421struct hb_ot_font_data_t {};
422
423hb_ot_font_data_t *
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
429void
430_hb_ot_shaper_font_data_destroy (hb_ot_font_data_t *data HB_UNUSED)
431{
432}
433
434
435/*
436 * shaper
437 */
438
439struct 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
459static void
460hb_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
513static void
514hb_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 buffer->output_info (info);
538 while (buffer->idx < buffer->len && buffer->successful)
539 buffer->next_glyph ();
540 buffer->swap_buffers ();
541}
542
543static void
544hb_form_clusters (hb_buffer_t *buffer)
545{
546 if (!(buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_NON_ASCII))
547 return;
548
549 if (buffer->cluster_level == HB_BUFFER_CLUSTER_LEVEL_MONOTONE_GRAPHEMES)
550 foreach_grapheme (buffer, start, end)
551 buffer->merge_clusters (start, end);
552 else
553 foreach_grapheme (buffer, start, end)
554 buffer->unsafe_to_break (start, end);
555}
556
557static void
558hb_ensure_native_direction (hb_buffer_t *buffer)
559{
560 hb_direction_t direction = buffer->props.direction;
561 hb_direction_t horiz_dir = hb_script_get_horizontal_direction (buffer->props.script);
562
563 /* TODO vertical:
564 * The only BTT vertical script is Ogham, but it's not clear to me whether OpenType
565 * Ogham fonts are supposed to be implemented BTT or not. Need to research that
566 * first. */
567 if ((HB_DIRECTION_IS_HORIZONTAL (direction) &&
568 direction != horiz_dir && horiz_dir != HB_DIRECTION_INVALID) ||
569 (HB_DIRECTION_IS_VERTICAL (direction) &&
570 direction != HB_DIRECTION_TTB))
571 {
572
573 if (buffer->cluster_level == HB_BUFFER_CLUSTER_LEVEL_MONOTONE_CHARACTERS)
574 foreach_grapheme (buffer, start, end)
575 {
576 buffer->merge_clusters (start, end);
577 buffer->reverse_range (start, end);
578 }
579 else
580 foreach_grapheme (buffer, start, end)
581 /* form_clusters() merged clusters already, we don't merge. */
582 buffer->reverse_range (start, end);
583
584 buffer->reverse ();
585
586 buffer->props.direction = HB_DIRECTION_REVERSE (buffer->props.direction);
587 }
588}
589
590
591/*
592 * Substitute
593 */
594
595static hb_codepoint_t
596hb_vert_char_for (hb_codepoint_t u)
597{
598 switch (u >> 8)
599 {
600 case 0x20: switch (u) {
601 case 0x2013u: return 0xfe32u; // EN DASH
602 case 0x2014u: return 0xfe31u; // EM DASH
603 case 0x2025u: return 0xfe30u; // TWO DOT LEADER
604 case 0x2026u: return 0xfe19u; // HORIZONTAL ELLIPSIS
605 } break;
606 case 0x30: switch (u) {
607 case 0x3001u: return 0xfe11u; // IDEOGRAPHIC COMMA
608 case 0x3002u: return 0xfe12u; // IDEOGRAPHIC FULL STOP
609 case 0x3008u: return 0xfe3fu; // LEFT ANGLE BRACKET
610 case 0x3009u: return 0xfe40u; // RIGHT ANGLE BRACKET
611 case 0x300au: return 0xfe3du; // LEFT DOUBLE ANGLE BRACKET
612 case 0x300bu: return 0xfe3eu; // RIGHT DOUBLE ANGLE BRACKET
613 case 0x300cu: return 0xfe41u; // LEFT CORNER BRACKET
614 case 0x300du: return 0xfe42u; // RIGHT CORNER BRACKET
615 case 0x300eu: return 0xfe43u; // LEFT WHITE CORNER BRACKET
616 case 0x300fu: return 0xfe44u; // RIGHT WHITE CORNER BRACKET
617 case 0x3010u: return 0xfe3bu; // LEFT BLACK LENTICULAR BRACKET
618 case 0x3011u: return 0xfe3cu; // RIGHT BLACK LENTICULAR BRACKET
619 case 0x3014u: return 0xfe39u; // LEFT TORTOISE SHELL BRACKET
620 case 0x3015u: return 0xfe3au; // RIGHT TORTOISE SHELL BRACKET
621 case 0x3016u: return 0xfe17u; // LEFT WHITE LENTICULAR BRACKET
622 case 0x3017u: return 0xfe18u; // RIGHT WHITE LENTICULAR BRACKET
623 } break;
624 case 0xfe: switch (u) {
625 case 0xfe4fu: return 0xfe34u; // WAVY LOW LINE
626 } break;
627 case 0xff: switch (u) {
628 case 0xff01u: return 0xfe15u; // FULLWIDTH EXCLAMATION MARK
629 case 0xff08u: return 0xfe35u; // FULLWIDTH LEFT PARENTHESIS
630 case 0xff09u: return 0xfe36u; // FULLWIDTH RIGHT PARENTHESIS
631 case 0xff0cu: return 0xfe10u; // FULLWIDTH COMMA
632 case 0xff1au: return 0xfe13u; // FULLWIDTH COLON
633 case 0xff1bu: return 0xfe14u; // FULLWIDTH SEMICOLON
634 case 0xff1fu: return 0xfe16u; // FULLWIDTH QUESTION MARK
635 case 0xff3bu: return 0xfe47u; // FULLWIDTH LEFT SQUARE BRACKET
636 case 0xff3du: return 0xfe48u; // FULLWIDTH RIGHT SQUARE BRACKET
637 case 0xff3fu: return 0xfe33u; // FULLWIDTH LOW LINE
638 case 0xff5bu: return 0xfe37u; // FULLWIDTH LEFT CURLY BRACKET
639 case 0xff5du: return 0xfe38u; // FULLWIDTH RIGHT CURLY BRACKET
640 } break;
641 }
642
643 return u;
644}
645
646static inline void
647hb_ot_rotate_chars (const hb_ot_shape_context_t *c)
648{
649 hb_buffer_t *buffer = c->buffer;
650 unsigned int count = buffer->len;
651 hb_glyph_info_t *info = buffer->info;
652
653 if (HB_DIRECTION_IS_BACKWARD (c->target_direction))
654 {
655 hb_unicode_funcs_t *unicode = buffer->unicode;
656 hb_mask_t rtlm_mask = c->plan->rtlm_mask;
657
658 for (unsigned int i = 0; i < count; i++) {
659 hb_codepoint_t codepoint = unicode->mirroring (info[i].codepoint);
660 if (unlikely (codepoint != info[i].codepoint && c->font->has_glyph (codepoint)))
661 info[i].codepoint = codepoint;
662 else
663 info[i].mask |= rtlm_mask;
664 }
665 }
666
667 if (HB_DIRECTION_IS_VERTICAL (c->target_direction) && !c->plan->has_vert)
668 {
669 for (unsigned int i = 0; i < count; i++) {
670 hb_codepoint_t codepoint = hb_vert_char_for (info[i].codepoint);
671 if (unlikely (codepoint != info[i].codepoint && c->font->has_glyph (codepoint)))
672 info[i].codepoint = codepoint;
673 }
674 }
675}
676
677static inline void
678hb_ot_shape_setup_masks_fraction (const hb_ot_shape_context_t *c)
679{
680#ifdef HB_NO_OT_SHAPE_FRACTIONS
681 return;
682#endif
683
684 if (!(c->buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_NON_ASCII) ||
685 !c->plan->has_frac)
686 return;
687
688 hb_buffer_t *buffer = c->buffer;
689
690 hb_mask_t pre_mask, post_mask;
691 if (HB_DIRECTION_IS_FORWARD (buffer->props.direction))
692 {
693 pre_mask = c->plan->numr_mask | c->plan->frac_mask;
694 post_mask = c->plan->frac_mask | c->plan->dnom_mask;
695 }
696 else
697 {
698 pre_mask = c->plan->frac_mask | c->plan->dnom_mask;
699 post_mask = c->plan->numr_mask | c->plan->frac_mask;
700 }
701
702 unsigned int count = buffer->len;
703 hb_glyph_info_t *info = buffer->info;
704 for (unsigned int i = 0; i < count; i++)
705 {
706 if (info[i].codepoint == 0x2044u) /* FRACTION SLASH */
707 {
708 unsigned int start = i, end = i + 1;
709 while (start &&
710 _hb_glyph_info_get_general_category (&info[start - 1]) ==
711 HB_UNICODE_GENERAL_CATEGORY_DECIMAL_NUMBER)
712 start--;
713 while (end < count &&
714 _hb_glyph_info_get_general_category (&info[end]) ==
715 HB_UNICODE_GENERAL_CATEGORY_DECIMAL_NUMBER)
716 end++;
717
718 buffer->unsafe_to_break (start, end);
719
720 for (unsigned int j = start; j < i; j++)
721 info[j].mask |= pre_mask;
722 info[i].mask |= c->plan->frac_mask;
723 for (unsigned int j = i + 1; j < end; j++)
724 info[j].mask |= post_mask;
725
726 i = end - 1;
727 }
728 }
729}
730
731static inline void
732hb_ot_shape_initialize_masks (const hb_ot_shape_context_t *c)
733{
734 hb_ot_map_t *map = &c->plan->map;
735 hb_buffer_t *buffer = c->buffer;
736
737 hb_mask_t global_mask = map->get_global_mask ();
738 buffer->reset_masks (global_mask);
739}
740
741static inline void
742hb_ot_shape_setup_masks (const hb_ot_shape_context_t *c)
743{
744 hb_ot_map_t *map = &c->plan->map;
745 hb_buffer_t *buffer = c->buffer;
746
747 hb_ot_shape_setup_masks_fraction (c);
748
749 if (c->plan->shaper->setup_masks)
750 c->plan->shaper->setup_masks (c->plan, buffer, c->font);
751
752 for (unsigned int i = 0; i < c->num_user_features; i++)
753 {
754 const hb_feature_t *feature = &c->user_features[i];
755 if (!(feature->start == HB_FEATURE_GLOBAL_START && feature->end == HB_FEATURE_GLOBAL_END)) {
756 unsigned int shift;
757 hb_mask_t mask = map->get_mask (feature->tag, &shift);
758 buffer->set_masks (feature->value << shift, mask, feature->start, feature->end);
759 }
760 }
761}
762
763static void
764hb_ot_zero_width_default_ignorables (const hb_buffer_t *buffer)
765{
766 if (!(buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_DEFAULT_IGNORABLES) ||
767 (buffer->flags & HB_BUFFER_FLAG_PRESERVE_DEFAULT_IGNORABLES) ||
768 (buffer->flags & HB_BUFFER_FLAG_REMOVE_DEFAULT_IGNORABLES))
769 return;
770
771 unsigned int count = buffer->len;
772 hb_glyph_info_t *info = buffer->info;
773 hb_glyph_position_t *pos = buffer->pos;
774 unsigned int i = 0;
775 for (i = 0; i < count; i++)
776 if (unlikely (_hb_glyph_info_is_default_ignorable (&info[i])))
777 pos[i].x_advance = pos[i].y_advance = pos[i].x_offset = pos[i].y_offset = 0;
778}
779
780static void
781hb_ot_hide_default_ignorables (hb_buffer_t *buffer,
782 hb_font_t *font)
783{
784 if (!(buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_DEFAULT_IGNORABLES) ||
785 (buffer->flags & HB_BUFFER_FLAG_PRESERVE_DEFAULT_IGNORABLES))
786 return;
787
788 unsigned int count = buffer->len;
789 hb_glyph_info_t *info = buffer->info;
790
791 hb_codepoint_t invisible = buffer->invisible;
792 if (!(buffer->flags & HB_BUFFER_FLAG_REMOVE_DEFAULT_IGNORABLES) &&
793 (invisible || font->get_nominal_glyph (' ', &invisible)))
794 {
795 /* Replace default-ignorables with a zero-advance invisible glyph. */
796 for (unsigned int i = 0; i < count; i++)
797 {
798 if (_hb_glyph_info_is_default_ignorable (&info[i]))
799 info[i].codepoint = invisible;
800 }
801 }
802 else
803 hb_ot_layout_delete_glyphs_inplace (buffer, _hb_glyph_info_is_default_ignorable);
804}
805
806
807static inline void
808hb_ot_map_glyphs_fast (hb_buffer_t *buffer)
809{
810 /* Normalization process sets up glyph_index(), we just copy it. */
811 unsigned int count = buffer->len;
812 hb_glyph_info_t *info = buffer->info;
813 for (unsigned int i = 0; i < count; i++)
814 info[i].codepoint = info[i].glyph_index();
815
816 buffer->content_type = HB_BUFFER_CONTENT_TYPE_GLYPHS;
817}
818
819static inline void
820hb_synthesize_glyph_classes (hb_buffer_t *buffer)
821{
822 unsigned int count = buffer->len;
823 hb_glyph_info_t *info = buffer->info;
824 for (unsigned int i = 0; i < count; i++)
825 {
826 hb_ot_layout_glyph_props_flags_t klass;
827
828 /* Never mark default-ignorables as marks.
829 * They won't get in the way of lookups anyway,
830 * but having them as mark will cause them to be skipped
831 * over if the lookup-flag says so, but at least for the
832 * Mongolian variation selectors, looks like Uniscribe
833 * marks them as non-mark. Some Mongolian fonts without
834 * GDEF rely on this. Another notable character that
835 * this applies to is COMBINING GRAPHEME JOINER. */
836 klass = (_hb_glyph_info_get_general_category (&info[i]) !=
837 HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK ||
838 _hb_glyph_info_is_default_ignorable (&info[i])) ?
839 HB_OT_LAYOUT_GLYPH_PROPS_BASE_GLYPH :
840 HB_OT_LAYOUT_GLYPH_PROPS_MARK;
841 _hb_glyph_info_set_glyph_props (&info[i], klass);
842 }
843}
844
845static inline void
846hb_ot_substitute_default (const hb_ot_shape_context_t *c)
847{
848 hb_buffer_t *buffer = c->buffer;
849
850 hb_ot_rotate_chars (c);
851
852 HB_BUFFER_ALLOCATE_VAR (buffer, glyph_index);
853
854 _hb_ot_shape_normalize (c->plan, buffer, c->font);
855
856 hb_ot_shape_setup_masks (c);
857
858 /* This is unfortunate to go here, but necessary... */
859 if (c->plan->fallback_mark_positioning)
860 _hb_ot_shape_fallback_mark_position_recategorize_marks (c->plan, c->font, buffer);
861
862 hb_ot_map_glyphs_fast (buffer);
863
864 HB_BUFFER_DEALLOCATE_VAR (buffer, glyph_index);
865}
866
867static inline void
868hb_ot_substitute_complex (const hb_ot_shape_context_t *c)
869{
870 hb_buffer_t *buffer = c->buffer;
871
872 hb_ot_layout_substitute_start (c->font, buffer);
873
874 if (c->plan->fallback_glyph_classes)
875 hb_synthesize_glyph_classes (c->buffer);
876
877 c->plan->substitute (c->font, buffer);
878}
879
880static inline void
881hb_ot_substitute_pre (const hb_ot_shape_context_t *c)
882{
883 hb_ot_substitute_default (c);
884
885 _hb_buffer_allocate_gsubgpos_vars (c->buffer);
886
887 hb_ot_substitute_complex (c);
888}
889
890static inline void
891hb_ot_substitute_post (const hb_ot_shape_context_t *c)
892{
893 hb_ot_hide_default_ignorables (c->buffer, c->font);
894#ifndef HB_NO_AAT_SHAPE
895 if (c->plan->apply_morx)
896 hb_aat_layout_remove_deleted_glyphs (c->buffer);
897#endif
898
899 if (c->plan->shaper->postprocess_glyphs)
900 c->plan->shaper->postprocess_glyphs (c->plan, c->buffer, c->font);
901}
902
903
904/*
905 * Position
906 */
907
908static inline void
909adjust_mark_offsets (hb_glyph_position_t *pos)
910{
911 pos->x_offset -= pos->x_advance;
912 pos->y_offset -= pos->y_advance;
913}
914
915static inline void
916zero_mark_width (hb_glyph_position_t *pos)
917{
918 pos->x_advance = 0;
919 pos->y_advance = 0;
920}
921
922static inline void
923zero_mark_widths_by_gdef (hb_buffer_t *buffer, bool adjust_offsets)
924{
925 unsigned int count = buffer->len;
926 hb_glyph_info_t *info = buffer->info;
927 for (unsigned int i = 0; i < count; i++)
928 if (_hb_glyph_info_is_mark (&info[i]))
929 {
930 if (adjust_offsets)
931 adjust_mark_offsets (&buffer->pos[i]);
932 zero_mark_width (&buffer->pos[i]);
933 }
934}
935
936static inline void
937hb_ot_position_default (const hb_ot_shape_context_t *c)
938{
939 hb_direction_t direction = c->buffer->props.direction;
940 unsigned int count = c->buffer->len;
941 hb_glyph_info_t *info = c->buffer->info;
942 hb_glyph_position_t *pos = c->buffer->pos;
943
944 if (HB_DIRECTION_IS_HORIZONTAL (direction))
945 {
946 c->font->get_glyph_h_advances (count, &info[0].codepoint, sizeof(info[0]),
947 &pos[0].x_advance, sizeof(pos[0]));
948 /* The nil glyph_h_origin() func returns 0, so no need to apply it. */
949 if (c->font->has_glyph_h_origin_func ())
950 for (unsigned int i = 0; i < count; i++)
951 c->font->subtract_glyph_h_origin (info[i].codepoint,
952 &pos[i].x_offset,
953 &pos[i].y_offset);
954 }
955 else
956 {
957 c->font->get_glyph_v_advances (count, &info[0].codepoint, sizeof(info[0]),
958 &pos[0].y_advance, sizeof(pos[0]));
959 for (unsigned int i = 0; i < count; i++)
960 {
961 c->font->subtract_glyph_v_origin (info[i].codepoint,
962 &pos[i].x_offset,
963 &pos[i].y_offset);
964 }
965 }
966 if (c->buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_SPACE_FALLBACK)
967 _hb_ot_shape_fallback_spaces (c->plan, c->font, c->buffer);
968}
969
970static inline void
971hb_ot_position_complex (const hb_ot_shape_context_t *c)
972{
973 unsigned int count = c->buffer->len;
974 hb_glyph_info_t *info = c->buffer->info;
975 hb_glyph_position_t *pos = c->buffer->pos;
976
977 /* If the font has no GPOS and direction is forward, then when
978 * zeroing mark widths, we shift the mark with it, such that the
979 * mark is positioned hanging over the previous glyph. When
980 * direction is backward we don't shift and it will end up
981 * hanging over the next glyph after the final reordering.
982 *
983 * Note: If fallback positinoing happens, we don't care about
984 * this as it will be overriden.
985 */
986 bool adjust_offsets_when_zeroing = c->plan->adjust_mark_positioning_when_zeroing &&
987 HB_DIRECTION_IS_FORWARD (c->buffer->props.direction);
988
989 /* We change glyph origin to what GPOS expects (horizontal), apply GPOS, change it back. */
990
991 /* The nil glyph_h_origin() func returns 0, so no need to apply it. */
992 if (c->font->has_glyph_h_origin_func ())
993 for (unsigned int i = 0; i < count; i++)
994 c->font->add_glyph_h_origin (info[i].codepoint,
995 &pos[i].x_offset,
996 &pos[i].y_offset);
997
998 hb_ot_layout_position_start (c->font, c->buffer);
999
1000 if (c->plan->zero_marks)
1001 switch (c->plan->shaper->zero_width_marks)
1002 {
1003 case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_EARLY:
1004 zero_mark_widths_by_gdef (c->buffer, adjust_offsets_when_zeroing);
1005 break;
1006
1007 default:
1008 case HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE:
1009 case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_LATE:
1010 break;
1011 }
1012
1013 c->plan->position (c->font, c->buffer);
1014
1015 if (c->plan->zero_marks)
1016 switch (c->plan->shaper->zero_width_marks)
1017 {
1018 case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_LATE:
1019 zero_mark_widths_by_gdef (c->buffer, adjust_offsets_when_zeroing);
1020 break;
1021
1022 default:
1023 case HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE:
1024 case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_EARLY:
1025 break;
1026 }
1027
1028 /* Finish off. Has to follow a certain order. */
1029 hb_ot_layout_position_finish_advances (c->font, c->buffer);
1030 hb_ot_zero_width_default_ignorables (c->buffer);
1031#ifndef HB_NO_AAT_SHAPE
1032 if (c->plan->apply_morx)
1033 hb_aat_layout_zero_width_deleted_glyphs (c->buffer);
1034#endif
1035 hb_ot_layout_position_finish_offsets (c->font, c->buffer);
1036
1037 /* The nil glyph_h_origin() func returns 0, so no need to apply it. */
1038 if (c->font->has_glyph_h_origin_func ())
1039 for (unsigned int i = 0; i < count; i++)
1040 c->font->subtract_glyph_h_origin (info[i].codepoint,
1041 &pos[i].x_offset,
1042 &pos[i].y_offset);
1043
1044 if (c->plan->fallback_mark_positioning)
1045 _hb_ot_shape_fallback_mark_position (c->plan, c->font, c->buffer,
1046 adjust_offsets_when_zeroing);
1047}
1048
1049static inline void
1050hb_ot_position (const hb_ot_shape_context_t *c)
1051{
1052 c->buffer->clear_positions ();
1053
1054 hb_ot_position_default (c);
1055
1056 hb_ot_position_complex (c);
1057
1058 if (HB_DIRECTION_IS_BACKWARD (c->buffer->props.direction))
1059 hb_buffer_reverse (c->buffer);
1060
1061 _hb_buffer_deallocate_gsubgpos_vars (c->buffer);
1062}
1063
1064static inline void
1065hb_propagate_flags (hb_buffer_t *buffer)
1066{
1067 /* Propagate cluster-level glyph flags to be the same on all cluster glyphs.
1068 * Simplifies using them. */
1069
1070 if (!(buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_UNSAFE_TO_BREAK))
1071 return;
1072
1073 hb_glyph_info_t *info = buffer->info;
1074
1075 foreach_cluster (buffer, start, end)
1076 {
1077 unsigned int mask = 0;
1078 for (unsigned int i = start; i < end; i++)
1079 if (info[i].mask & HB_GLYPH_FLAG_UNSAFE_TO_BREAK)
1080 {
1081 mask = HB_GLYPH_FLAG_UNSAFE_TO_BREAK;
1082 break;
1083 }
1084 if (mask)
1085 for (unsigned int i = start; i < end; i++)
1086 info[i].mask |= mask;
1087 }
1088}
1089
1090/* Pull it all together! */
1091
1092static void
1093hb_ot_shape_internal (hb_ot_shape_context_t *c)
1094{
1095 c->buffer->deallocate_var_all ();
1096 c->buffer->scratch_flags = HB_BUFFER_SCRATCH_FLAG_DEFAULT;
1097 if (likely (!hb_unsigned_mul_overflows (c->buffer->len, HB_BUFFER_MAX_LEN_FACTOR)))
1098 {
1099 c->buffer->max_len = hb_max (c->buffer->len * HB_BUFFER_MAX_LEN_FACTOR,
1100 (unsigned) HB_BUFFER_MAX_LEN_MIN);
1101 }
1102 if (likely (!hb_unsigned_mul_overflows (c->buffer->len, HB_BUFFER_MAX_OPS_FACTOR)))
1103 {
1104 c->buffer->max_ops = hb_max (c->buffer->len * HB_BUFFER_MAX_OPS_FACTOR,
1105 (unsigned) HB_BUFFER_MAX_OPS_MIN);
1106 }
1107
1108 /* Save the original direction, we use it later. */
1109 c->target_direction = c->buffer->props.direction;
1110
1111 _hb_buffer_allocate_unicode_vars (c->buffer);
1112
1113 c->buffer->clear_output ();
1114
1115 hb_ot_shape_initialize_masks (c);
1116 hb_set_unicode_props (c->buffer);
1117 hb_insert_dotted_circle (c->buffer, c->font);
1118
1119 hb_form_clusters (c->buffer);
1120
1121 hb_ensure_native_direction (c->buffer);
1122
1123 if (c->plan->shaper->preprocess_text)
1124 c->plan->shaper->preprocess_text (c->plan, c->buffer, c->font);
1125
1126 hb_ot_substitute_pre (c);
1127 hb_ot_position (c);
1128 hb_ot_substitute_post (c);
1129
1130 hb_propagate_flags (c->buffer);
1131
1132 _hb_buffer_deallocate_unicode_vars (c->buffer);
1133
1134 c->buffer->props.direction = c->target_direction;
1135
1136 c->buffer->max_len = HB_BUFFER_MAX_LEN_DEFAULT;
1137 c->buffer->max_ops = HB_BUFFER_MAX_OPS_DEFAULT;
1138 c->buffer->deallocate_var_all ();
1139}
1140
1141
1142hb_bool_t
1143_hb_ot_shape (hb_shape_plan_t *shape_plan,
1144 hb_font_t *font,
1145 hb_buffer_t *buffer,
1146 const hb_feature_t *features,
1147 unsigned int num_features)
1148{
1149 hb_ot_shape_context_t c = {&shape_plan->ot, font, font->face, buffer, features, num_features};
1150 hb_ot_shape_internal (&c);
1151
1152 return true;
1153}
1154
1155
1156/**
1157 * hb_ot_shape_plan_collect_lookups:
1158 *
1159 * Since: 0.9.7
1160 **/
1161void
1162hb_ot_shape_plan_collect_lookups (hb_shape_plan_t *shape_plan,
1163 hb_tag_t table_tag,
1164 hb_set_t *lookup_indexes /* OUT */)
1165{
1166 shape_plan->ot.collect_lookups (table_tag, lookup_indexes);
1167}
1168
1169
1170/* TODO Move this to hb-ot-shape-normalize, make it do decompose, and make it public. */
1171static void
1172add_char (hb_font_t *font,
1173 hb_unicode_funcs_t *unicode,
1174 hb_bool_t mirror,
1175 hb_codepoint_t u,
1176 hb_set_t *glyphs)
1177{
1178 hb_codepoint_t glyph;
1179 if (font->get_nominal_glyph (u, &glyph))
1180 glyphs->add (glyph);
1181 if (mirror)
1182 {
1183 hb_codepoint_t m = unicode->mirroring (u);
1184 if (m != u && font->get_nominal_glyph (m, &glyph))
1185 glyphs->add (glyph);
1186 }
1187}
1188
1189
1190/**
1191 * hb_ot_shape_glyphs_closure:
1192 *
1193 * Since: 0.9.2
1194 **/
1195void
1196hb_ot_shape_glyphs_closure (hb_font_t *font,
1197 hb_buffer_t *buffer,
1198 const hb_feature_t *features,
1199 unsigned int num_features,
1200 hb_set_t *glyphs)
1201{
1202 const char *shapers[] = {"ot", nullptr};
1203 hb_shape_plan_t *shape_plan = hb_shape_plan_create_cached (font->face, &buffer->props,
1204 features, num_features, shapers);
1205
1206 bool mirror = hb_script_get_horizontal_direction (buffer->props.script) == HB_DIRECTION_RTL;
1207
1208 unsigned int count = buffer->len;
1209 hb_glyph_info_t *info = buffer->info;
1210 for (unsigned int i = 0; i < count; i++)
1211 add_char (font, buffer->unicode, mirror, info[i].codepoint, glyphs);
1212
1213 hb_set_t *lookups = hb_set_create ();
1214 hb_ot_shape_plan_collect_lookups (shape_plan, HB_OT_TAG_GSUB, lookups);
1215 hb_ot_layout_lookups_substitute_closure (font->face, lookups, glyphs);
1216
1217 hb_set_destroy (lookups);
1218
1219 hb_shape_plan_destroy (shape_plan);
1220}
1221
1222
1223#endif
1224