2 * Copyright © 2009,2010 Red Hat, Inc.
3 * Copyright © 2010,2011,2012 Google, Inc.
5 * This is part of HarfBuzz, a text shaping library.
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.
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
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.
25 * Red Hat Author(s): Behdad Esfahbod
26 * Google Author(s): Behdad Esfahbod
31 #ifndef HB_NO_OT_SHAPE
33 #ifdef HB_NO_OT_LAYOUT
34 #error "Cannot compile 'ot' shaper with HB_NO_OT_LAYOUT."
37 #include "hb-shaper-impl.hh"
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"
44 #include "hb-ot-face.hh"
48 #include "hb-aat-layout.hh"
51 #ifndef HB_NO_AAT_SHAPE
53 _hb_apply_morx (hb_face_t *face, const hb_segment_properties_t *props)
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));
64 * @short_description: OpenType shaping support
67 * Support functions for OpenType shaping related queries.
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);
76 hb_ot_shape_planner_t::hb_ot_shape_planner_t (hb_face_t *face,
77 const hb_segment_properties_t *props) :
82 #ifndef HB_NO_AAT_SHAPE
83 , apply_morx (_hb_apply_morx (face, props))
86 shaper = hb_ot_shape_complex_categorize (this);
88 script_zero_marks = shaper->zero_width_marks != HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE;
89 script_fallback_mark_positioning = shaper->fallback_position;
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;
97 hb_ot_shape_planner_t::compile (hb_ot_shape_plan_t &plan,
98 const hb_ot_shape_plan_key_t &key)
101 plan.shaper = shaper;
102 map.compile (plan.map, key);
103 #ifndef HB_NO_AAT_SHAPE
105 aat_map.compile (plan.aat_map);
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);
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'));
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;
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;
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];
134 * Decide who provides glyph classes. GDEF or Unicode.
137 if (!hb_ot_layout_has_glyph_classes (face))
138 plan.fallback_glyph_classes = true;
141 * Decide who does substitutions. GSUB, morx, or fallback.
144 #ifndef HB_NO_AAT_SHAPE
145 plan.apply_morx = apply_morx;
149 * Decide who does positioning. GPOS, kerx, kern, or fallback.
154 #ifndef HB_NO_AAT_SHAPE
155 else if (hb_aat_layout_has_positioning (face))
156 plan.apply_kerx = true;
158 else if (!apply_morx && !disable_gpos && hb_ot_layout_has_positioning (face))
159 plan.apply_gpos = true;
161 if (!plan.apply_kerx && (!has_gpos_kern || !plan.apply_gpos))
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;
169 #ifndef HB_NO_OT_KERN
170 if (hb_ot_layout_has_kerning (face))
171 plan.apply_kern = true;
175 plan.zero_marks = script_zero_marks &&
178 #ifndef HB_NO_OT_KERN
179 || !hb_ot_layout_has_machine_kerning (face)
182 plan.has_gpos_mark = !!plan.map.get_1_mask (HB_TAG ('m','a','r','k'));
184 plan.adjust_mark_positioning_when_zeroing = !plan.apply_gpos &&
187 #ifndef HB_NO_OT_KERN
188 || !hb_ot_layout_has_cross_kerning (face)
192 plan.fallback_mark_positioning = plan.adjust_mark_positioning_when_zeroing &&
193 script_fallback_mark_positioning;
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);
202 hb_ot_shape_plan_t::init0 (hb_face_t *face,
203 const hb_shape_plan_key_t *key)
206 #ifndef HB_NO_AAT_SHAPE
210 hb_ot_shape_planner_t planner (face,
213 hb_ot_shape_collect_features (&planner,
215 key->num_user_features);
217 planner.compile (*this, key->ot);
219 if (shaper->data_create)
221 data = shaper->data_create (this);
222 if (unlikely (!data))
230 hb_ot_shape_plan_t::fini ()
232 if (shaper->data_destroy)
233 shaper->data_destroy (const_cast<void *> (data));
236 #ifndef HB_NO_AAT_SHAPE
242 hb_ot_shape_plan_t::substitute (hb_font_t *font,
243 hb_buffer_t *buffer) const
245 #ifndef HB_NO_AAT_SHAPE
246 if (unlikely (apply_morx))
247 hb_aat_layout_substitute (this, font, buffer);
250 map.substitute (this, font, buffer);
254 hb_ot_shape_plan_t::position (hb_font_t *font,
255 hb_buffer_t *buffer) const
257 if (this->apply_gpos)
258 map.position (this, font, buffer);
259 #ifndef HB_NO_AAT_SHAPE
260 else if (this->apply_kerx)
261 hb_aat_layout_position (this, font, buffer);
263 #ifndef HB_NO_OT_KERN
264 else if (this->apply_kern)
265 hb_ot_layout_kern (this, font, buffer);
268 _hb_ot_shape_fallback_kern (this, font, buffer);
270 #ifndef HB_NO_AAT_SHAPE
271 if (this->apply_trak)
272 hb_aat_layout_track (this, font, buffer);
277 static const hb_ot_map_feature_t
280 {HB_TAG('a','b','v','m'), F_GLOBAL},
281 {HB_TAG('b','l','w','m'), F_GLOBAL},
282 {HB_TAG('c','c','m','p'), F_GLOBAL},
283 {HB_TAG('l','o','c','l'), F_GLOBAL},
284 {HB_TAG('m','a','r','k'), F_GLOBAL_MANUAL_JOINERS},
285 {HB_TAG('m','k','m','k'), F_GLOBAL_MANUAL_JOINERS},
286 {HB_TAG('r','l','i','g'), F_GLOBAL},
290 static const hb_ot_map_feature_t
291 horizontal_features[] =
293 {HB_TAG('c','a','l','t'), F_GLOBAL},
294 {HB_TAG('c','l','i','g'), F_GLOBAL},
295 {HB_TAG('c','u','r','s'), F_GLOBAL},
296 {HB_TAG('d','i','s','t'), F_GLOBAL},
297 {HB_TAG('k','e','r','n'), F_GLOBAL_HAS_FALLBACK},
298 {HB_TAG('l','i','g','a'), F_GLOBAL},
299 {HB_TAG('r','c','l','t'), F_GLOBAL},
303 hb_ot_shape_collect_features (hb_ot_shape_planner_t *planner,
304 const hb_feature_t *user_features,
305 unsigned int num_user_features)
307 hb_ot_map_builder_t *map = &planner->map;
309 map->enable_feature (HB_TAG('r','v','r','n'));
310 map->add_gsub_pause (nullptr);
312 switch (planner->props.direction) {
313 case HB_DIRECTION_LTR:
314 map->enable_feature (HB_TAG ('l','t','r','a'));
315 map->enable_feature (HB_TAG ('l','t','r','m'));
317 case HB_DIRECTION_RTL:
318 map->enable_feature (HB_TAG ('r','t','l','a'));
319 map->add_feature (HB_TAG ('r','t','l','m'));
321 case HB_DIRECTION_TTB:
322 case HB_DIRECTION_BTT:
323 case HB_DIRECTION_INVALID:
328 #ifndef HB_NO_OT_SHAPE_FRACTIONS
329 /* Automatic fractions. */
330 map->add_feature (HB_TAG ('f','r','a','c'));
331 map->add_feature (HB_TAG ('n','u','m','r'));
332 map->add_feature (HB_TAG ('d','n','o','m'));
336 map->enable_feature (HB_TAG ('r','a','n','d'), F_RANDOM, HB_OT_MAP_MAX_VALUE);
338 #ifndef HB_NO_AAT_SHAPE
339 /* Tracking. We enable dummy feature here just to allow disabling
340 * AAT 'trak' table using features.
341 * https://github.com/harfbuzz/harfbuzz/issues/1303 */
342 map->enable_feature (HB_TAG ('t','r','a','k'), F_HAS_FALLBACK);
345 map->enable_feature (HB_TAG ('H','A','R','F'));
347 if (planner->shaper->collect_features)
348 planner->shaper->collect_features (planner);
350 map->enable_feature (HB_TAG ('B','U','Z','Z'));
352 for (unsigned int i = 0; i < ARRAY_LENGTH (common_features); i++)
353 map->add_feature (common_features[i]);
355 if (HB_DIRECTION_IS_HORIZONTAL (planner->props.direction))
356 for (unsigned int i = 0; i < ARRAY_LENGTH (horizontal_features); i++)
357 map->add_feature (horizontal_features[i]);
360 /* We really want to find a 'vert' feature if there's any in the font, no
361 * matter which script/langsys it is listed (or not) under.
362 * See various bugs referenced from:
363 * https://github.com/harfbuzz/harfbuzz/issues/63 */
364 map->enable_feature (HB_TAG ('v','e','r','t'), F_GLOBAL_SEARCH);
367 for (unsigned int i = 0; i < num_user_features; i++)
369 const hb_feature_t *feature = &user_features[i];
370 map->add_feature (feature->tag,
371 (feature->start == HB_FEATURE_GLOBAL_START &&
372 feature->end == HB_FEATURE_GLOBAL_END) ? F_GLOBAL : F_NONE,
376 #ifndef HB_NO_AAT_SHAPE
377 if (planner->apply_morx)
379 hb_aat_map_builder_t *aat_map = &planner->aat_map;
380 for (unsigned int i = 0; i < num_user_features; i++)
382 const hb_feature_t *feature = &user_features[i];
383 aat_map->add_feature (feature->tag, feature->value);
388 if (planner->shaper->override_features)
389 planner->shaper->override_features (planner);
397 struct hb_ot_face_data_t {};
400 _hb_ot_shaper_face_data_create (hb_face_t *face)
402 return (hb_ot_face_data_t *) HB_SHAPER_DATA_SUCCEEDED;
406 _hb_ot_shaper_face_data_destroy (hb_ot_face_data_t *data)
415 struct hb_ot_font_data_t {};
418 _hb_ot_shaper_font_data_create (hb_font_t *font HB_UNUSED)
420 return (hb_ot_font_data_t *) HB_SHAPER_DATA_SUCCEEDED;
424 _hb_ot_shaper_font_data_destroy (hb_ot_font_data_t *data HB_UNUSED)
433 struct hb_ot_shape_context_t
435 hb_ot_shape_plan_t *plan;
439 const hb_feature_t *user_features;
440 unsigned int num_user_features;
442 /* Transient stuff */
443 hb_direction_t target_direction;
454 hb_set_unicode_props (hb_buffer_t *buffer)
456 /* Implement enough of Unicode Graphemes here that shaping
457 * in reverse-direction wouldn't break graphemes. Namely,
458 * we mark all marks and ZWJ and ZWJ,Extended_Pictographic
459 * sequences as continuations. The foreach_grapheme()
460 * macro uses this bit.
462 * https://www.unicode.org/reports/tr29/#Regex_Definitions
464 unsigned int count = buffer->len;
465 hb_glyph_info_t *info = buffer->info;
466 for (unsigned int i = 0; i < count; i++)
468 _hb_glyph_info_set_unicode_props (&info[i], buffer);
470 /* Marks are already set as continuation by the above line.
471 * Handle Emoji_Modifier and ZWJ-continuation. */
472 if (unlikely (_hb_glyph_info_get_general_category (&info[i]) == HB_UNICODE_GENERAL_CATEGORY_MODIFIER_SYMBOL &&
473 hb_in_range<hb_codepoint_t> (info[i].codepoint, 0x1F3FBu, 0x1F3FFu)))
475 _hb_glyph_info_set_continuation (&info[i]);
477 #ifndef HB_NO_EMOJI_SEQUENCES
478 else if (unlikely (_hb_glyph_info_is_zwj (&info[i])))
480 _hb_glyph_info_set_continuation (&info[i]);
482 _hb_unicode_is_emoji_Extended_Pictographic (info[i + 1].codepoint))
485 _hb_glyph_info_set_unicode_props (&info[i], buffer);
486 _hb_glyph_info_set_continuation (&info[i]);
490 /* Or part of the Other_Grapheme_Extend that is not marks.
491 * As of Unicode 11 that is just:
493 * 200C ; Other_Grapheme_Extend # Cf ZERO WIDTH NON-JOINER
494 * FF9E..FF9F ; Other_Grapheme_Extend # Lm [2] HALFWIDTH KATAKANA VOICED SOUND MARK..HALFWIDTH KATAKANA SEMI-VOICED SOUND MARK
495 * E0020..E007F ; Other_Grapheme_Extend # Cf [96] TAG SPACE..CANCEL TAG
497 * ZWNJ is special, we don't want to merge it as there's no need, and keeping
498 * it separate results in more granular clusters. Ignore Katakana for now.
499 * Tags are used for Emoji sub-region flag sequences:
500 * https://github.com/harfbuzz/harfbuzz/issues/1556
502 else if (unlikely (hb_in_range<hb_codepoint_t> (info[i].codepoint, 0xE0020u, 0xE007Fu)))
503 _hb_glyph_info_set_continuation (&info[i]);
508 hb_insert_dotted_circle (hb_buffer_t *buffer, hb_font_t *font)
510 if (unlikely (buffer->flags & HB_BUFFER_FLAG_DO_NOT_INSERT_DOTTED_CIRCLE))
513 if (!(buffer->flags & HB_BUFFER_FLAG_BOT) ||
514 buffer->context_len[0] ||
515 !_hb_glyph_info_is_unicode_mark (&buffer->info[0]))
518 if (!font->has_glyph (0x25CCu))
521 hb_glyph_info_t dottedcircle = {0};
522 dottedcircle.codepoint = 0x25CCu;
523 _hb_glyph_info_set_unicode_props (&dottedcircle, buffer);
525 buffer->clear_output ();
528 hb_glyph_info_t info = dottedcircle;
529 info.cluster = buffer->cur().cluster;
530 info.mask = buffer->cur().mask;
531 buffer->output_info (info);
532 while (buffer->idx < buffer->len && buffer->successful)
533 buffer->next_glyph ();
534 buffer->swap_buffers ();
538 hb_form_clusters (hb_buffer_t *buffer)
540 if (!(buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_NON_ASCII))
543 if (buffer->cluster_level == HB_BUFFER_CLUSTER_LEVEL_MONOTONE_GRAPHEMES)
544 foreach_grapheme (buffer, start, end)
545 buffer->merge_clusters (start, end);
547 foreach_grapheme (buffer, start, end)
548 buffer->unsafe_to_break (start, end);
552 hb_ensure_native_direction (hb_buffer_t *buffer)
554 hb_direction_t direction = buffer->props.direction;
555 hb_direction_t horiz_dir = hb_script_get_horizontal_direction (buffer->props.script);
558 * The only BTT vertical script is Ogham, but it's not clear to me whether OpenType
559 * Ogham fonts are supposed to be implemented BTT or not. Need to research that
561 if ((HB_DIRECTION_IS_HORIZONTAL (direction) &&
562 direction != horiz_dir && horiz_dir != HB_DIRECTION_INVALID) ||
563 (HB_DIRECTION_IS_VERTICAL (direction) &&
564 direction != HB_DIRECTION_TTB))
567 if (buffer->cluster_level == HB_BUFFER_CLUSTER_LEVEL_MONOTONE_CHARACTERS)
568 foreach_grapheme (buffer, start, end)
570 buffer->merge_clusters (start, end);
571 buffer->reverse_range (start, end);
574 foreach_grapheme (buffer, start, end)
575 /* form_clusters() merged clusters already, we don't merge. */
576 buffer->reverse_range (start, end);
580 buffer->props.direction = HB_DIRECTION_REVERSE (buffer->props.direction);
589 static hb_codepoint_t
590 hb_vert_char_for (hb_codepoint_t u)
594 case 0x20: switch (u) {
595 case 0x2013u: return 0xfe32u; // EN DASH
596 case 0x2014u: return 0xfe31u; // EM DASH
597 case 0x2025u: return 0xfe30u; // TWO DOT LEADER
598 case 0x2026u: return 0xfe19u; // HORIZONTAL ELLIPSIS
600 case 0x30: switch (u) {
601 case 0x3001u: return 0xfe11u; // IDEOGRAPHIC COMMA
602 case 0x3002u: return 0xfe12u; // IDEOGRAPHIC FULL STOP
603 case 0x3008u: return 0xfe3fu; // LEFT ANGLE BRACKET
604 case 0x3009u: return 0xfe40u; // RIGHT ANGLE BRACKET
605 case 0x300au: return 0xfe3du; // LEFT DOUBLE ANGLE BRACKET
606 case 0x300bu: return 0xfe3eu; // RIGHT DOUBLE ANGLE BRACKET
607 case 0x300cu: return 0xfe41u; // LEFT CORNER BRACKET
608 case 0x300du: return 0xfe42u; // RIGHT CORNER BRACKET
609 case 0x300eu: return 0xfe43u; // LEFT WHITE CORNER BRACKET
610 case 0x300fu: return 0xfe44u; // RIGHT WHITE CORNER BRACKET
611 case 0x3010u: return 0xfe3bu; // LEFT BLACK LENTICULAR BRACKET
612 case 0x3011u: return 0xfe3cu; // RIGHT BLACK LENTICULAR BRACKET
613 case 0x3014u: return 0xfe39u; // LEFT TORTOISE SHELL BRACKET
614 case 0x3015u: return 0xfe3au; // RIGHT TORTOISE SHELL BRACKET
615 case 0x3016u: return 0xfe17u; // LEFT WHITE LENTICULAR BRACKET
616 case 0x3017u: return 0xfe18u; // RIGHT WHITE LENTICULAR BRACKET
618 case 0xfe: switch (u) {
619 case 0xfe4fu: return 0xfe34u; // WAVY LOW LINE
621 case 0xff: switch (u) {
622 case 0xff01u: return 0xfe15u; // FULLWIDTH EXCLAMATION MARK
623 case 0xff08u: return 0xfe35u; // FULLWIDTH LEFT PARENTHESIS
624 case 0xff09u: return 0xfe36u; // FULLWIDTH RIGHT PARENTHESIS
625 case 0xff0cu: return 0xfe10u; // FULLWIDTH COMMA
626 case 0xff1au: return 0xfe13u; // FULLWIDTH COLON
627 case 0xff1bu: return 0xfe14u; // FULLWIDTH SEMICOLON
628 case 0xff1fu: return 0xfe16u; // FULLWIDTH QUESTION MARK
629 case 0xff3bu: return 0xfe47u; // FULLWIDTH LEFT SQUARE BRACKET
630 case 0xff3du: return 0xfe48u; // FULLWIDTH RIGHT SQUARE BRACKET
631 case 0xff3fu: return 0xfe33u; // FULLWIDTH LOW LINE
632 case 0xff5bu: return 0xfe37u; // FULLWIDTH LEFT CURLY BRACKET
633 case 0xff5du: return 0xfe38u; // FULLWIDTH RIGHT CURLY BRACKET
641 hb_ot_rotate_chars (const hb_ot_shape_context_t *c)
643 hb_buffer_t *buffer = c->buffer;
644 unsigned int count = buffer->len;
645 hb_glyph_info_t *info = buffer->info;
647 if (HB_DIRECTION_IS_BACKWARD (c->target_direction))
649 hb_unicode_funcs_t *unicode = buffer->unicode;
650 hb_mask_t rtlm_mask = c->plan->rtlm_mask;
652 for (unsigned int i = 0; i < count; i++) {
653 hb_codepoint_t codepoint = unicode->mirroring (info[i].codepoint);
654 if (unlikely (codepoint != info[i].codepoint && c->font->has_glyph (codepoint)))
655 info[i].codepoint = codepoint;
657 info[i].mask |= rtlm_mask;
661 if (HB_DIRECTION_IS_VERTICAL (c->target_direction) && !c->plan->has_vert)
663 for (unsigned int i = 0; i < count; i++) {
664 hb_codepoint_t codepoint = hb_vert_char_for (info[i].codepoint);
665 if (unlikely (codepoint != info[i].codepoint && c->font->has_glyph (codepoint)))
666 info[i].codepoint = codepoint;
672 hb_ot_shape_setup_masks_fraction (const hb_ot_shape_context_t *c)
674 #ifdef HB_NO_OT_SHAPE_FRACTIONS
678 if (!(c->buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_NON_ASCII) ||
682 hb_buffer_t *buffer = c->buffer;
684 hb_mask_t pre_mask, post_mask;
685 if (HB_DIRECTION_IS_FORWARD (buffer->props.direction))
687 pre_mask = c->plan->numr_mask | c->plan->frac_mask;
688 post_mask = c->plan->frac_mask | c->plan->dnom_mask;
692 pre_mask = c->plan->frac_mask | c->plan->dnom_mask;
693 post_mask = c->plan->numr_mask | c->plan->frac_mask;
696 unsigned int count = buffer->len;
697 hb_glyph_info_t *info = buffer->info;
698 for (unsigned int i = 0; i < count; i++)
700 if (info[i].codepoint == 0x2044u) /* FRACTION SLASH */
702 unsigned int start = i, end = i + 1;
704 _hb_glyph_info_get_general_category (&info[start - 1]) ==
705 HB_UNICODE_GENERAL_CATEGORY_DECIMAL_NUMBER)
707 while (end < count &&
708 _hb_glyph_info_get_general_category (&info[end]) ==
709 HB_UNICODE_GENERAL_CATEGORY_DECIMAL_NUMBER)
712 buffer->unsafe_to_break (start, end);
714 for (unsigned int j = start; j < i; j++)
715 info[j].mask |= pre_mask;
716 info[i].mask |= c->plan->frac_mask;
717 for (unsigned int j = i + 1; j < end; j++)
718 info[j].mask |= post_mask;
726 hb_ot_shape_initialize_masks (const hb_ot_shape_context_t *c)
728 hb_ot_map_t *map = &c->plan->map;
729 hb_buffer_t *buffer = c->buffer;
731 hb_mask_t global_mask = map->get_global_mask ();
732 buffer->reset_masks (global_mask);
736 hb_ot_shape_setup_masks (const hb_ot_shape_context_t *c)
738 hb_ot_map_t *map = &c->plan->map;
739 hb_buffer_t *buffer = c->buffer;
741 hb_ot_shape_setup_masks_fraction (c);
743 if (c->plan->shaper->setup_masks)
744 c->plan->shaper->setup_masks (c->plan, buffer, c->font);
746 for (unsigned int i = 0; i < c->num_user_features; i++)
748 const hb_feature_t *feature = &c->user_features[i];
749 if (!(feature->start == HB_FEATURE_GLOBAL_START && feature->end == HB_FEATURE_GLOBAL_END)) {
751 hb_mask_t mask = map->get_mask (feature->tag, &shift);
752 buffer->set_masks (feature->value << shift, mask, feature->start, feature->end);
758 hb_ot_zero_width_default_ignorables (const hb_buffer_t *buffer)
760 if (!(buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_DEFAULT_IGNORABLES) ||
761 (buffer->flags & HB_BUFFER_FLAG_PRESERVE_DEFAULT_IGNORABLES) ||
762 (buffer->flags & HB_BUFFER_FLAG_REMOVE_DEFAULT_IGNORABLES))
765 unsigned int count = buffer->len;
766 hb_glyph_info_t *info = buffer->info;
767 hb_glyph_position_t *pos = buffer->pos;
769 for (i = 0; i < count; i++)
770 if (unlikely (_hb_glyph_info_is_default_ignorable (&info[i])))
771 pos[i].x_advance = pos[i].y_advance = pos[i].x_offset = pos[i].y_offset = 0;
775 hb_ot_hide_default_ignorables (hb_buffer_t *buffer,
778 if (!(buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_DEFAULT_IGNORABLES) ||
779 (buffer->flags & HB_BUFFER_FLAG_PRESERVE_DEFAULT_IGNORABLES))
782 unsigned int count = buffer->len;
783 hb_glyph_info_t *info = buffer->info;
785 hb_codepoint_t invisible = buffer->invisible;
786 if (!(buffer->flags & HB_BUFFER_FLAG_REMOVE_DEFAULT_IGNORABLES) &&
787 (invisible || font->get_nominal_glyph (' ', &invisible)))
789 /* Replace default-ignorables with a zero-advance invisible glyph. */
790 for (unsigned int i = 0; i < count; i++)
792 if (_hb_glyph_info_is_default_ignorable (&info[i]))
793 info[i].codepoint = invisible;
797 hb_ot_layout_delete_glyphs_inplace (buffer, _hb_glyph_info_is_default_ignorable);
802 hb_ot_map_glyphs_fast (hb_buffer_t *buffer)
804 /* Normalization process sets up glyph_index(), we just copy it. */
805 unsigned int count = buffer->len;
806 hb_glyph_info_t *info = buffer->info;
807 for (unsigned int i = 0; i < count; i++)
808 info[i].codepoint = info[i].glyph_index();
810 buffer->content_type = HB_BUFFER_CONTENT_TYPE_GLYPHS;
814 hb_synthesize_glyph_classes (hb_buffer_t *buffer)
816 unsigned int count = buffer->len;
817 hb_glyph_info_t *info = buffer->info;
818 for (unsigned int i = 0; i < count; i++)
820 hb_ot_layout_glyph_props_flags_t klass;
822 /* Never mark default-ignorables as marks.
823 * They won't get in the way of lookups anyway,
824 * but having them as mark will cause them to be skipped
825 * over if the lookup-flag says so, but at least for the
826 * Mongolian variation selectors, looks like Uniscribe
827 * marks them as non-mark. Some Mongolian fonts without
828 * GDEF rely on this. Another notable character that
829 * this applies to is COMBINING GRAPHEME JOINER. */
830 klass = (_hb_glyph_info_get_general_category (&info[i]) !=
831 HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK ||
832 _hb_glyph_info_is_default_ignorable (&info[i])) ?
833 HB_OT_LAYOUT_GLYPH_PROPS_BASE_GLYPH :
834 HB_OT_LAYOUT_GLYPH_PROPS_MARK;
835 _hb_glyph_info_set_glyph_props (&info[i], klass);
840 hb_ot_substitute_default (const hb_ot_shape_context_t *c)
842 hb_buffer_t *buffer = c->buffer;
844 hb_ot_rotate_chars (c);
846 HB_BUFFER_ALLOCATE_VAR (buffer, glyph_index);
848 _hb_ot_shape_normalize (c->plan, buffer, c->font);
850 hb_ot_shape_setup_masks (c);
852 /* This is unfortunate to go here, but necessary... */
853 if (c->plan->fallback_mark_positioning)
854 _hb_ot_shape_fallback_mark_position_recategorize_marks (c->plan, c->font, buffer);
856 hb_ot_map_glyphs_fast (buffer);
858 HB_BUFFER_DEALLOCATE_VAR (buffer, glyph_index);
862 hb_ot_substitute_complex (const hb_ot_shape_context_t *c)
864 hb_buffer_t *buffer = c->buffer;
866 hb_ot_layout_substitute_start (c->font, buffer);
868 if (c->plan->fallback_glyph_classes)
869 hb_synthesize_glyph_classes (c->buffer);
871 c->plan->substitute (c->font, buffer);
875 hb_ot_substitute_pre (const hb_ot_shape_context_t *c)
877 hb_ot_substitute_default (c);
879 _hb_buffer_allocate_gsubgpos_vars (c->buffer);
881 hb_ot_substitute_complex (c);
885 hb_ot_substitute_post (const hb_ot_shape_context_t *c)
887 hb_ot_hide_default_ignorables (c->buffer, c->font);
888 #ifndef HB_NO_AAT_SHAPE
889 if (c->plan->apply_morx)
890 hb_aat_layout_remove_deleted_glyphs (c->buffer);
893 if (c->plan->shaper->postprocess_glyphs)
894 c->plan->shaper->postprocess_glyphs (c->plan, c->buffer, c->font);
903 adjust_mark_offsets (hb_glyph_position_t *pos)
905 pos->x_offset -= pos->x_advance;
906 pos->y_offset -= pos->y_advance;
910 zero_mark_width (hb_glyph_position_t *pos)
917 zero_mark_widths_by_gdef (hb_buffer_t *buffer, bool adjust_offsets)
919 unsigned int count = buffer->len;
920 hb_glyph_info_t *info = buffer->info;
921 for (unsigned int i = 0; i < count; i++)
922 if (_hb_glyph_info_is_mark (&info[i]))
925 adjust_mark_offsets (&buffer->pos[i]);
926 zero_mark_width (&buffer->pos[i]);
931 hb_ot_position_default (const hb_ot_shape_context_t *c)
933 hb_direction_t direction = c->buffer->props.direction;
934 unsigned int count = c->buffer->len;
935 hb_glyph_info_t *info = c->buffer->info;
936 hb_glyph_position_t *pos = c->buffer->pos;
938 if (HB_DIRECTION_IS_HORIZONTAL (direction))
940 c->font->get_glyph_h_advances (count, &info[0].codepoint, sizeof(info[0]),
941 &pos[0].x_advance, sizeof(pos[0]));
942 /* The nil glyph_h_origin() func returns 0, so no need to apply it. */
943 if (c->font->has_glyph_h_origin_func ())
944 for (unsigned int i = 0; i < count; i++)
945 c->font->subtract_glyph_h_origin (info[i].codepoint,
951 c->font->get_glyph_v_advances (count, &info[0].codepoint, sizeof(info[0]),
952 &pos[0].y_advance, sizeof(pos[0]));
953 for (unsigned int i = 0; i < count; i++)
955 c->font->subtract_glyph_v_origin (info[i].codepoint,
960 if (c->buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_SPACE_FALLBACK)
961 _hb_ot_shape_fallback_spaces (c->plan, c->font, c->buffer);
965 hb_ot_position_complex (const hb_ot_shape_context_t *c)
967 unsigned int count = c->buffer->len;
968 hb_glyph_info_t *info = c->buffer->info;
969 hb_glyph_position_t *pos = c->buffer->pos;
971 /* If the font has no GPOS and direction is forward, then when
972 * zeroing mark widths, we shift the mark with it, such that the
973 * mark is positioned hanging over the previous glyph. When
974 * direction is backward we don't shift and it will end up
975 * hanging over the next glyph after the final reordering.
977 * Note: If fallback positinoing happens, we don't care about
978 * this as it will be overriden.
980 bool adjust_offsets_when_zeroing = c->plan->adjust_mark_positioning_when_zeroing &&
981 HB_DIRECTION_IS_FORWARD (c->buffer->props.direction);
983 /* We change glyph origin to what GPOS expects (horizontal), apply GPOS, change it back. */
985 /* The nil glyph_h_origin() func returns 0, so no need to apply it. */
986 if (c->font->has_glyph_h_origin_func ())
987 for (unsigned int i = 0; i < count; i++)
988 c->font->add_glyph_h_origin (info[i].codepoint,
992 hb_ot_layout_position_start (c->font, c->buffer);
994 if (c->plan->zero_marks)
995 switch (c->plan->shaper->zero_width_marks)
997 case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_EARLY:
998 zero_mark_widths_by_gdef (c->buffer, adjust_offsets_when_zeroing);
1002 case HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE:
1003 case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_LATE:
1007 c->plan->position (c->font, c->buffer);
1009 if (c->plan->zero_marks)
1010 switch (c->plan->shaper->zero_width_marks)
1012 case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_LATE:
1013 zero_mark_widths_by_gdef (c->buffer, adjust_offsets_when_zeroing);
1017 case HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE:
1018 case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_EARLY:
1022 /* Finish off. Has to follow a certain order. */
1023 hb_ot_layout_position_finish_advances (c->font, c->buffer);
1024 hb_ot_zero_width_default_ignorables (c->buffer);
1025 #ifndef HB_NO_AAT_SHAPE
1026 if (c->plan->apply_morx)
1027 hb_aat_layout_zero_width_deleted_glyphs (c->buffer);
1029 hb_ot_layout_position_finish_offsets (c->font, c->buffer);
1031 /* The nil glyph_h_origin() func returns 0, so no need to apply it. */
1032 if (c->font->has_glyph_h_origin_func ())
1033 for (unsigned int i = 0; i < count; i++)
1034 c->font->subtract_glyph_h_origin (info[i].codepoint,
1038 if (c->plan->fallback_mark_positioning)
1039 _hb_ot_shape_fallback_mark_position (c->plan, c->font, c->buffer,
1040 adjust_offsets_when_zeroing);
1044 hb_ot_position (const hb_ot_shape_context_t *c)
1046 c->buffer->clear_positions ();
1048 hb_ot_position_default (c);
1050 hb_ot_position_complex (c);
1052 if (HB_DIRECTION_IS_BACKWARD (c->buffer->props.direction))
1053 hb_buffer_reverse (c->buffer);
1055 _hb_buffer_deallocate_gsubgpos_vars (c->buffer);
1059 hb_propagate_flags (hb_buffer_t *buffer)
1061 /* Propagate cluster-level glyph flags to be the same on all cluster glyphs.
1062 * Simplifies using them. */
1064 if (!(buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_UNSAFE_TO_BREAK))
1067 hb_glyph_info_t *info = buffer->info;
1069 foreach_cluster (buffer, start, end)
1071 unsigned int mask = 0;
1072 for (unsigned int i = start; i < end; i++)
1073 if (info[i].mask & HB_GLYPH_FLAG_UNSAFE_TO_BREAK)
1075 mask = HB_GLYPH_FLAG_UNSAFE_TO_BREAK;
1079 for (unsigned int i = start; i < end; i++)
1080 info[i].mask |= mask;
1084 /* Pull it all together! */
1087 hb_ot_shape_internal (hb_ot_shape_context_t *c)
1089 c->buffer->deallocate_var_all ();
1090 c->buffer->scratch_flags = HB_BUFFER_SCRATCH_FLAG_DEFAULT;
1091 if (likely (!hb_unsigned_mul_overflows (c->buffer->len, HB_BUFFER_MAX_LEN_FACTOR)))
1093 c->buffer->max_len = hb_max (c->buffer->len * HB_BUFFER_MAX_LEN_FACTOR,
1094 (unsigned) HB_BUFFER_MAX_LEN_MIN);
1096 if (likely (!hb_unsigned_mul_overflows (c->buffer->len, HB_BUFFER_MAX_OPS_FACTOR)))
1098 c->buffer->max_ops = hb_max (c->buffer->len * HB_BUFFER_MAX_OPS_FACTOR,
1099 (unsigned) HB_BUFFER_MAX_OPS_MIN);
1102 /* Save the original direction, we use it later. */
1103 c->target_direction = c->buffer->props.direction;
1105 _hb_buffer_allocate_unicode_vars (c->buffer);
1107 c->buffer->clear_output ();
1109 hb_ot_shape_initialize_masks (c);
1110 hb_set_unicode_props (c->buffer);
1111 hb_insert_dotted_circle (c->buffer, c->font);
1113 hb_form_clusters (c->buffer);
1115 hb_ensure_native_direction (c->buffer);
1117 if (c->plan->shaper->preprocess_text)
1118 c->plan->shaper->preprocess_text (c->plan, c->buffer, c->font);
1120 hb_ot_substitute_pre (c);
1122 hb_ot_substitute_post (c);
1124 hb_propagate_flags (c->buffer);
1126 _hb_buffer_deallocate_unicode_vars (c->buffer);
1128 c->buffer->props.direction = c->target_direction;
1130 c->buffer->max_len = HB_BUFFER_MAX_LEN_DEFAULT;
1131 c->buffer->max_ops = HB_BUFFER_MAX_OPS_DEFAULT;
1132 c->buffer->deallocate_var_all ();
1137 _hb_ot_shape (hb_shape_plan_t *shape_plan,
1139 hb_buffer_t *buffer,
1140 const hb_feature_t *features,
1141 unsigned int num_features)
1143 hb_ot_shape_context_t c = {&shape_plan->ot, font, font->face, buffer, features, num_features};
1144 hb_ot_shape_internal (&c);
1151 * hb_ot_shape_plan_collect_lookups:
1156 hb_ot_shape_plan_collect_lookups (hb_shape_plan_t *shape_plan,
1158 hb_set_t *lookup_indexes /* OUT */)
1160 shape_plan->ot.collect_lookups (table_tag, lookup_indexes);
1164 /* TODO Move this to hb-ot-shape-normalize, make it do decompose, and make it public. */
1166 add_char (hb_font_t *font,
1167 hb_unicode_funcs_t *unicode,
1172 hb_codepoint_t glyph;
1173 if (font->get_nominal_glyph (u, &glyph))
1174 glyphs->add (glyph);
1177 hb_codepoint_t m = unicode->mirroring (u);
1178 if (m != u && font->get_nominal_glyph (m, &glyph))
1179 glyphs->add (glyph);
1185 * hb_ot_shape_glyphs_closure:
1190 hb_ot_shape_glyphs_closure (hb_font_t *font,
1191 hb_buffer_t *buffer,
1192 const hb_feature_t *features,
1193 unsigned int num_features,
1196 const char *shapers[] = {"ot", nullptr};
1197 hb_shape_plan_t *shape_plan = hb_shape_plan_create_cached (font->face, &buffer->props,
1198 features, num_features, shapers);
1200 bool mirror = hb_script_get_horizontal_direction (buffer->props.script) == HB_DIRECTION_RTL;
1202 unsigned int count = buffer->len;
1203 hb_glyph_info_t *info = buffer->info;
1204 for (unsigned int i = 0; i < count; i++)
1205 add_char (font, buffer->unicode, mirror, info[i].codepoint, glyphs);
1207 hb_set_t *lookups = hb_set_create ();
1208 hb_ot_shape_plan_collect_lookups (shape_plan, HB_OT_TAG_GSUB, lookups);
1209 hb_ot_layout_lookups_substitute_closure (font->face, lookups, glyphs);
1211 hb_set_destroy (lookups);
1213 hb_shape_plan_destroy (shape_plan);