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"
54 * @short_description: OpenType shaping support
57 * Support functions for OpenType shaping related queries.
62 hb_ot_shape_collect_features (hb_ot_shape_planner_t *planner,
63 const hb_feature_t *user_features,
64 unsigned int num_user_features);
66 #ifndef HB_NO_AAT_SHAPE
68 _hb_apply_morx (hb_face_t *face)
70 if (hb_options ().aat &&
71 hb_aat_layout_has_substitution (face))
74 /* Ignore empty GSUB tables. */
75 return (!hb_ot_layout_has_substitution (face) ||
76 !hb_ot_layout_table_get_script_tags (face,
78 0, nullptr, nullptr)) &&
79 hb_aat_layout_has_substitution (face);
83 hb_ot_shape_planner_t::hb_ot_shape_planner_t (hb_face_t *face,
84 const hb_segment_properties_t *props) :
89 #ifndef HB_NO_AAT_SHAPE
90 , apply_morx (_hb_apply_morx (face))
93 shaper = hb_ot_shape_complex_categorize (this);
95 script_zero_marks = shaper->zero_width_marks != HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE;
96 script_fallback_mark_positioning = shaper->fallback_position;
99 shaper = &_hb_ot_complex_shaper_default;
103 hb_ot_shape_planner_t::compile (hb_ot_shape_plan_t &plan,
104 const hb_ot_shape_plan_key_t &key)
107 plan.shaper = shaper;
108 map.compile (plan.map, key);
109 #ifndef HB_NO_AAT_SHAPE
111 aat_map.compile (plan.aat_map);
114 #ifndef HB_NO_OT_SHAPE_FRACTIONS
115 plan.frac_mask = plan.map.get_1_mask (HB_TAG ('f','r','a','c'));
116 plan.numr_mask = plan.map.get_1_mask (HB_TAG ('n','u','m','r'));
117 plan.dnom_mask = plan.map.get_1_mask (HB_TAG ('d','n','o','m'));
118 plan.has_frac = plan.frac_mask || (plan.numr_mask && plan.dnom_mask);
121 plan.rtlm_mask = plan.map.get_1_mask (HB_TAG ('r','t','l','m'));
122 hb_tag_t kern_tag = HB_DIRECTION_IS_HORIZONTAL (props.direction) ?
123 HB_TAG ('k','e','r','n') : HB_TAG ('v','k','r','n');
124 #ifndef HB_NO_OT_KERN
125 plan.kern_mask = plan.map.get_mask (kern_tag);
126 plan.requested_kerning = !!plan.kern_mask;
128 #ifndef HB_NO_AAT_SHAPE
129 plan.trak_mask = plan.map.get_mask (HB_TAG ('t','r','a','k'));
130 plan.requested_tracking = !!plan.trak_mask;
133 bool has_gpos_kern = plan.map.get_feature_index (1, kern_tag) != HB_OT_LAYOUT_NO_FEATURE_INDEX;
134 bool disable_gpos = plan.shaper->gpos_tag &&
135 plan.shaper->gpos_tag != plan.map.chosen_script[1];
138 * Decide who provides glyph classes. GDEF or Unicode.
141 if (!hb_ot_layout_has_glyph_classes (face))
142 plan.fallback_glyph_classes = true;
145 * Decide who does substitutions. GSUB, morx, or fallback.
148 #ifndef HB_NO_AAT_SHAPE
149 plan.apply_morx = apply_morx;
153 * Decide who does positioning. GPOS, kerx, kern, or fallback.
158 #ifndef HB_NO_AAT_SHAPE
159 else if (hb_options ().aat && hb_aat_layout_has_positioning (face))
160 plan.apply_kerx = true;
162 else if (!apply_morx && !disable_gpos && hb_ot_layout_has_positioning (face))
163 plan.apply_gpos = true;
164 #ifndef HB_NO_AAT_SHAPE
165 else if (hb_aat_layout_has_positioning (face))
166 plan.apply_kerx = true;
169 if (!plan.apply_kerx && !has_gpos_kern)
171 /* Apparently Apple applies kerx if GPOS kern was not applied. */
172 #ifndef HB_NO_AAT_SHAPE
173 if (hb_aat_layout_has_positioning (face))
174 plan.apply_kerx = true;
177 #ifndef HB_NO_OT_KERN
178 if (hb_ot_layout_has_kerning (face))
179 plan.apply_kern = true;
183 plan.zero_marks = script_zero_marks &&
186 #ifndef HB_NO_OT_KERN
187 || !hb_ot_layout_has_machine_kerning (face)
190 plan.has_gpos_mark = !!plan.map.get_1_mask (HB_TAG ('m','a','r','k'));
192 plan.adjust_mark_positioning_when_zeroing = !plan.apply_gpos &&
195 #ifndef HB_NO_OT_KERN
196 || !hb_ot_layout_has_cross_kerning (face)
200 plan.fallback_mark_positioning = plan.adjust_mark_positioning_when_zeroing &&
201 script_fallback_mark_positioning;
203 #ifndef HB_NO_AAT_SHAPE
204 /* Currently we always apply trak. */
205 plan.apply_trak = plan.requested_tracking && hb_aat_layout_has_tracking (face);
210 hb_ot_shape_plan_t::init0 (hb_face_t *face,
211 const hb_shape_plan_key_t *key)
214 #ifndef HB_NO_AAT_SHAPE
218 hb_ot_shape_planner_t planner (face,
221 hb_ot_shape_collect_features (&planner,
223 key->num_user_features);
225 planner.compile (*this, key->ot);
227 if (shaper->data_create)
229 data = shaper->data_create (this);
230 if (unlikely (!data))
238 hb_ot_shape_plan_t::fini ()
240 if (shaper->data_destroy)
241 shaper->data_destroy (const_cast<void *> (data));
244 #ifndef HB_NO_AAT_SHAPE
250 hb_ot_shape_plan_t::substitute (hb_font_t *font,
251 hb_buffer_t *buffer) const
253 #ifndef HB_NO_AAT_SHAPE
254 if (unlikely (apply_morx))
255 hb_aat_layout_substitute (this, font, buffer);
258 map.substitute (this, font, buffer);
262 hb_ot_shape_plan_t::position (hb_font_t *font,
263 hb_buffer_t *buffer) const
265 if (this->apply_gpos)
266 map.position (this, font, buffer);
267 #ifndef HB_NO_AAT_SHAPE
268 else if (this->apply_kerx)
269 hb_aat_layout_position (this, font, buffer);
271 #ifndef HB_NO_OT_KERN
272 else if (this->apply_kern)
273 hb_ot_layout_kern (this, font, buffer);
276 _hb_ot_shape_fallback_kern (this, font, buffer);
278 #ifndef HB_NO_AAT_SHAPE
279 if (this->apply_trak)
280 hb_aat_layout_track (this, font, buffer);
285 static const hb_ot_map_feature_t
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},
298 static const hb_ot_map_feature_t
299 horizontal_features[] =
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},
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)
315 hb_ot_map_builder_t *map = &planner->map;
317 map->enable_feature (HB_TAG('r','v','r','n'));
318 map->add_gsub_pause (nullptr);
320 switch (planner->props.direction) {
321 case HB_DIRECTION_LTR:
322 map->enable_feature (HB_TAG ('l','t','r','a'));
323 map->enable_feature (HB_TAG ('l','t','r','m'));
325 case HB_DIRECTION_RTL:
326 map->enable_feature (HB_TAG ('r','t','l','a'));
327 map->add_feature (HB_TAG ('r','t','l','m'));
329 case HB_DIRECTION_TTB:
330 case HB_DIRECTION_BTT:
331 case HB_DIRECTION_INVALID:
336 #ifndef HB_NO_OT_SHAPE_FRACTIONS
337 /* Automatic fractions. */
338 map->add_feature (HB_TAG ('f','r','a','c'));
339 map->add_feature (HB_TAG ('n','u','m','r'));
340 map->add_feature (HB_TAG ('d','n','o','m'));
344 map->enable_feature (HB_TAG ('r','a','n','d'), F_RANDOM, HB_OT_MAP_MAX_VALUE);
346 #ifndef HB_NO_AAT_SHAPE
347 /* Tracking. We enable dummy feature here just to allow disabling
348 * AAT 'trak' table using features.
349 * https://github.com/harfbuzz/harfbuzz/issues/1303 */
350 map->enable_feature (HB_TAG ('t','r','a','k'), F_HAS_FALLBACK);
353 map->enable_feature (HB_TAG ('H','A','R','F'));
355 if (planner->shaper->collect_features)
356 planner->shaper->collect_features (planner);
358 map->enable_feature (HB_TAG ('B','U','Z','Z'));
360 for (unsigned int i = 0; i < ARRAY_LENGTH (common_features); i++)
361 map->add_feature (common_features[i]);
363 if (HB_DIRECTION_IS_HORIZONTAL (planner->props.direction))
364 for (unsigned int i = 0; i < ARRAY_LENGTH (horizontal_features); i++)
365 map->add_feature (horizontal_features[i]);
368 /* We really want to find a 'vert' feature if there's any in the font, no
369 * matter which script/langsys it is listed (or not) under.
370 * See various bugs referenced from:
371 * https://github.com/harfbuzz/harfbuzz/issues/63 */
372 map->enable_feature (HB_TAG ('v','e','r','t'), F_GLOBAL_SEARCH);
375 for (unsigned int i = 0; i < num_user_features; i++)
377 const hb_feature_t *feature = &user_features[i];
378 map->add_feature (feature->tag,
379 (feature->start == HB_FEATURE_GLOBAL_START &&
380 feature->end == HB_FEATURE_GLOBAL_END) ? F_GLOBAL : F_NONE,
384 #ifndef HB_NO_AAT_SHAPE
385 if (planner->apply_morx)
387 hb_aat_map_builder_t *aat_map = &planner->aat_map;
388 for (unsigned int i = 0; i < num_user_features; i++)
390 const hb_feature_t *feature = &user_features[i];
391 aat_map->add_feature (feature->tag, feature->value);
396 if (planner->shaper->override_features)
397 planner->shaper->override_features (planner);
405 struct hb_ot_face_data_t {};
408 _hb_ot_shaper_face_data_create (hb_face_t *face)
410 return (hb_ot_face_data_t *) HB_SHAPER_DATA_SUCCEEDED;
414 _hb_ot_shaper_face_data_destroy (hb_ot_face_data_t *data)
423 struct hb_ot_font_data_t {};
426 _hb_ot_shaper_font_data_create (hb_font_t *font HB_UNUSED)
428 return (hb_ot_font_data_t *) HB_SHAPER_DATA_SUCCEEDED;
432 _hb_ot_shaper_font_data_destroy (hb_ot_font_data_t *data HB_UNUSED)
441 struct hb_ot_shape_context_t
443 hb_ot_shape_plan_t *plan;
447 const hb_feature_t *user_features;
448 unsigned int num_user_features;
450 /* Transient stuff */
451 hb_direction_t target_direction;
462 hb_set_unicode_props (hb_buffer_t *buffer)
464 /* Implement enough of Unicode Graphemes here that shaping
465 * in reverse-direction wouldn't break graphemes. Namely,
466 * we mark all marks and ZWJ and ZWJ,Extended_Pictographic
467 * sequences as continuations. The foreach_grapheme()
468 * macro uses this bit.
470 * https://www.unicode.org/reports/tr29/#Regex_Definitions
472 unsigned int count = buffer->len;
473 hb_glyph_info_t *info = buffer->info;
474 for (unsigned int i = 0; i < count; i++)
476 _hb_glyph_info_set_unicode_props (&info[i], buffer);
478 /* Marks are already set as continuation by the above line.
479 * Handle Emoji_Modifier and ZWJ-continuation. */
480 if (unlikely (_hb_glyph_info_get_general_category (&info[i]) == HB_UNICODE_GENERAL_CATEGORY_MODIFIER_SYMBOL &&
481 hb_in_range<hb_codepoint_t> (info[i].codepoint, 0x1F3FBu, 0x1F3FFu)))
483 _hb_glyph_info_set_continuation (&info[i]);
485 #ifndef HB_NO_EMOJI_SEQUENCES
486 else if (unlikely (_hb_glyph_info_is_zwj (&info[i])))
488 _hb_glyph_info_set_continuation (&info[i]);
490 _hb_unicode_is_emoji_Extended_Pictographic (info[i + 1].codepoint))
493 _hb_glyph_info_set_unicode_props (&info[i], buffer);
494 _hb_glyph_info_set_continuation (&info[i]);
498 /* Or part of the Other_Grapheme_Extend that is not marks.
499 * As of Unicode 11 that is just:
501 * 200C ; Other_Grapheme_Extend # Cf ZERO WIDTH NON-JOINER
502 * FF9E..FF9F ; Other_Grapheme_Extend # Lm [2] HALFWIDTH KATAKANA VOICED SOUND MARK..HALFWIDTH KATAKANA SEMI-VOICED SOUND MARK
503 * E0020..E007F ; Other_Grapheme_Extend # Cf [96] TAG SPACE..CANCEL TAG
505 * ZWNJ is special, we don't want to merge it as there's no need, and keeping
506 * it separate results in more granular clusters. Ignore Katakana for now.
507 * Tags are used for Emoji sub-region flag sequences:
508 * https://github.com/harfbuzz/harfbuzz/issues/1556
510 else if (unlikely (hb_in_range<hb_codepoint_t> (info[i].codepoint, 0xE0020u, 0xE007Fu)))
511 _hb_glyph_info_set_continuation (&info[i]);
516 hb_insert_dotted_circle (hb_buffer_t *buffer, hb_font_t *font)
518 if (unlikely (buffer->flags & HB_BUFFER_FLAG_DO_NOT_INSERT_DOTTED_CIRCLE))
521 if (!(buffer->flags & HB_BUFFER_FLAG_BOT) ||
522 buffer->context_len[0] ||
523 !_hb_glyph_info_is_unicode_mark (&buffer->info[0]))
526 if (!font->has_glyph (0x25CCu))
529 hb_glyph_info_t dottedcircle = {0};
530 dottedcircle.codepoint = 0x25CCu;
531 _hb_glyph_info_set_unicode_props (&dottedcircle, buffer);
533 buffer->clear_output ();
536 hb_glyph_info_t info = dottedcircle;
537 info.cluster = buffer->cur().cluster;
538 info.mask = buffer->cur().mask;
539 buffer->output_info (info);
540 while (buffer->idx < buffer->len && buffer->successful)
541 buffer->next_glyph ();
542 buffer->swap_buffers ();
546 hb_form_clusters (hb_buffer_t *buffer)
548 if (!(buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_NON_ASCII))
551 if (buffer->cluster_level == HB_BUFFER_CLUSTER_LEVEL_MONOTONE_GRAPHEMES)
552 foreach_grapheme (buffer, start, end)
553 buffer->merge_clusters (start, end);
555 foreach_grapheme (buffer, start, end)
556 buffer->unsafe_to_break (start, end);
560 hb_ensure_native_direction (hb_buffer_t *buffer)
562 hb_direction_t direction = buffer->props.direction;
563 hb_direction_t horiz_dir = hb_script_get_horizontal_direction (buffer->props.script);
566 * The only BTT vertical script is Ogham, but it's not clear to me whether OpenType
567 * Ogham fonts are supposed to be implemented BTT or not. Need to research that
569 if ((HB_DIRECTION_IS_HORIZONTAL (direction) &&
570 direction != horiz_dir && horiz_dir != HB_DIRECTION_INVALID) ||
571 (HB_DIRECTION_IS_VERTICAL (direction) &&
572 direction != HB_DIRECTION_TTB))
575 if (buffer->cluster_level == HB_BUFFER_CLUSTER_LEVEL_MONOTONE_CHARACTERS)
576 foreach_grapheme (buffer, start, end)
578 buffer->merge_clusters (start, end);
579 buffer->reverse_range (start, end);
582 foreach_grapheme (buffer, start, end)
583 /* form_clusters() merged clusters already, we don't merge. */
584 buffer->reverse_range (start, end);
588 buffer->props.direction = HB_DIRECTION_REVERSE (buffer->props.direction);
598 hb_ot_mirror_chars (const hb_ot_shape_context_t *c)
600 if (HB_DIRECTION_IS_FORWARD (c->target_direction))
603 hb_buffer_t *buffer = c->buffer;
604 hb_unicode_funcs_t *unicode = buffer->unicode;
605 hb_mask_t rtlm_mask = c->plan->rtlm_mask;
607 unsigned int count = buffer->len;
608 hb_glyph_info_t *info = buffer->info;
609 for (unsigned int i = 0; i < count; i++) {
610 hb_codepoint_t codepoint = unicode->mirroring (info[i].codepoint);
611 if (likely (codepoint == info[i].codepoint || !c->font->has_glyph (codepoint)))
612 info[i].mask |= rtlm_mask;
614 info[i].codepoint = codepoint;
619 hb_ot_shape_setup_masks_fraction (const hb_ot_shape_context_t *c)
621 #ifdef HB_NO_OT_SHAPE_FRACTIONS
625 if (!(c->buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_NON_ASCII) ||
629 hb_buffer_t *buffer = c->buffer;
631 hb_mask_t pre_mask, post_mask;
632 if (HB_DIRECTION_IS_FORWARD (buffer->props.direction))
634 pre_mask = c->plan->numr_mask | c->plan->frac_mask;
635 post_mask = c->plan->frac_mask | c->plan->dnom_mask;
639 pre_mask = c->plan->frac_mask | c->plan->dnom_mask;
640 post_mask = c->plan->numr_mask | c->plan->frac_mask;
643 unsigned int count = buffer->len;
644 hb_glyph_info_t *info = buffer->info;
645 for (unsigned int i = 0; i < count; i++)
647 if (info[i].codepoint == 0x2044u) /* FRACTION SLASH */
649 unsigned int start = i, end = i + 1;
651 _hb_glyph_info_get_general_category (&info[start - 1]) ==
652 HB_UNICODE_GENERAL_CATEGORY_DECIMAL_NUMBER)
654 while (end < count &&
655 _hb_glyph_info_get_general_category (&info[end]) ==
656 HB_UNICODE_GENERAL_CATEGORY_DECIMAL_NUMBER)
659 buffer->unsafe_to_break (start, end);
661 for (unsigned int j = start; j < i; j++)
662 info[j].mask |= pre_mask;
663 info[i].mask |= c->plan->frac_mask;
664 for (unsigned int j = i + 1; j < end; j++)
665 info[j].mask |= post_mask;
673 hb_ot_shape_initialize_masks (const hb_ot_shape_context_t *c)
675 hb_ot_map_t *map = &c->plan->map;
676 hb_buffer_t *buffer = c->buffer;
678 hb_mask_t global_mask = map->get_global_mask ();
679 buffer->reset_masks (global_mask);
683 hb_ot_shape_setup_masks (const hb_ot_shape_context_t *c)
685 hb_ot_map_t *map = &c->plan->map;
686 hb_buffer_t *buffer = c->buffer;
688 hb_ot_shape_setup_masks_fraction (c);
690 if (c->plan->shaper->setup_masks)
691 c->plan->shaper->setup_masks (c->plan, buffer, c->font);
693 for (unsigned int i = 0; i < c->num_user_features; i++)
695 const hb_feature_t *feature = &c->user_features[i];
696 if (!(feature->start == 0 && feature->end == (unsigned int)-1)) {
698 hb_mask_t mask = map->get_mask (feature->tag, &shift);
699 buffer->set_masks (feature->value << shift, mask, feature->start, feature->end);
705 hb_ot_zero_width_default_ignorables (const hb_buffer_t *buffer)
707 if (!(buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_DEFAULT_IGNORABLES) ||
708 (buffer->flags & HB_BUFFER_FLAG_PRESERVE_DEFAULT_IGNORABLES) ||
709 (buffer->flags & HB_BUFFER_FLAG_REMOVE_DEFAULT_IGNORABLES))
712 unsigned int count = buffer->len;
713 hb_glyph_info_t *info = buffer->info;
714 hb_glyph_position_t *pos = buffer->pos;
716 for (i = 0; i < count; i++)
717 if (unlikely (_hb_glyph_info_is_default_ignorable (&info[i])))
718 pos[i].x_advance = pos[i].y_advance = pos[i].x_offset = pos[i].y_offset = 0;
722 hb_ot_hide_default_ignorables (hb_buffer_t *buffer,
725 if (!(buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_DEFAULT_IGNORABLES) ||
726 (buffer->flags & HB_BUFFER_FLAG_PRESERVE_DEFAULT_IGNORABLES))
729 unsigned int count = buffer->len;
730 hb_glyph_info_t *info = buffer->info;
732 hb_codepoint_t invisible = buffer->invisible;
733 if (!(buffer->flags & HB_BUFFER_FLAG_REMOVE_DEFAULT_IGNORABLES) &&
734 (invisible || font->get_nominal_glyph (' ', &invisible)))
736 /* Replace default-ignorables with a zero-advance invisible glyph. */
737 for (unsigned int i = 0; i < count; i++)
739 if (_hb_glyph_info_is_default_ignorable (&info[i]))
740 info[i].codepoint = invisible;
744 hb_ot_layout_delete_glyphs_inplace (buffer, _hb_glyph_info_is_default_ignorable);
749 hb_ot_map_glyphs_fast (hb_buffer_t *buffer)
751 /* Normalization process sets up glyph_index(), we just copy it. */
752 unsigned int count = buffer->len;
753 hb_glyph_info_t *info = buffer->info;
754 for (unsigned int i = 0; i < count; i++)
755 info[i].codepoint = info[i].glyph_index();
757 buffer->content_type = HB_BUFFER_CONTENT_TYPE_GLYPHS;
761 hb_synthesize_glyph_classes (hb_buffer_t *buffer)
763 unsigned int count = buffer->len;
764 hb_glyph_info_t *info = buffer->info;
765 for (unsigned int i = 0; i < count; i++)
767 hb_ot_layout_glyph_props_flags_t klass;
769 /* Never mark default-ignorables as marks.
770 * They won't get in the way of lookups anyway,
771 * but having them as mark will cause them to be skipped
772 * over if the lookup-flag says so, but at least for the
773 * Mongolian variation selectors, looks like Uniscribe
774 * marks them as non-mark. Some Mongolian fonts without
775 * GDEF rely on this. Another notable character that
776 * this applies to is COMBINING GRAPHEME JOINER. */
777 klass = (_hb_glyph_info_get_general_category (&info[i]) !=
778 HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK ||
779 _hb_glyph_info_is_default_ignorable (&info[i])) ?
780 HB_OT_LAYOUT_GLYPH_PROPS_BASE_GLYPH :
781 HB_OT_LAYOUT_GLYPH_PROPS_MARK;
782 _hb_glyph_info_set_glyph_props (&info[i], klass);
787 hb_ot_substitute_default (const hb_ot_shape_context_t *c)
789 hb_buffer_t *buffer = c->buffer;
791 hb_ot_mirror_chars (c);
793 HB_BUFFER_ALLOCATE_VAR (buffer, glyph_index);
795 _hb_ot_shape_normalize (c->plan, buffer, c->font);
797 hb_ot_shape_setup_masks (c);
799 /* This is unfortunate to go here, but necessary... */
800 if (c->plan->fallback_mark_positioning)
801 _hb_ot_shape_fallback_mark_position_recategorize_marks (c->plan, c->font, buffer);
803 hb_ot_map_glyphs_fast (buffer);
805 HB_BUFFER_DEALLOCATE_VAR (buffer, glyph_index);
809 hb_ot_substitute_complex (const hb_ot_shape_context_t *c)
811 hb_buffer_t *buffer = c->buffer;
813 hb_ot_layout_substitute_start (c->font, buffer);
815 if (c->plan->fallback_glyph_classes)
816 hb_synthesize_glyph_classes (c->buffer);
818 c->plan->substitute (c->font, buffer);
822 hb_ot_substitute_pre (const hb_ot_shape_context_t *c)
824 hb_ot_substitute_default (c);
826 _hb_buffer_allocate_gsubgpos_vars (c->buffer);
828 hb_ot_substitute_complex (c);
832 hb_ot_substitute_post (const hb_ot_shape_context_t *c)
834 hb_ot_hide_default_ignorables (c->buffer, c->font);
835 #ifndef HB_NO_AAT_SHAPE
836 if (c->plan->apply_morx)
837 hb_aat_layout_remove_deleted_glyphs (c->buffer);
840 if (c->plan->shaper->postprocess_glyphs)
841 c->plan->shaper->postprocess_glyphs (c->plan, c->buffer, c->font);
850 adjust_mark_offsets (hb_glyph_position_t *pos)
852 pos->x_offset -= pos->x_advance;
853 pos->y_offset -= pos->y_advance;
857 zero_mark_width (hb_glyph_position_t *pos)
864 zero_mark_widths_by_gdef (hb_buffer_t *buffer, bool adjust_offsets)
866 unsigned int count = buffer->len;
867 hb_glyph_info_t *info = buffer->info;
868 for (unsigned int i = 0; i < count; i++)
869 if (_hb_glyph_info_is_mark (&info[i]))
872 adjust_mark_offsets (&buffer->pos[i]);
873 zero_mark_width (&buffer->pos[i]);
878 hb_ot_position_default (const hb_ot_shape_context_t *c)
880 hb_direction_t direction = c->buffer->props.direction;
881 unsigned int count = c->buffer->len;
882 hb_glyph_info_t *info = c->buffer->info;
883 hb_glyph_position_t *pos = c->buffer->pos;
885 if (HB_DIRECTION_IS_HORIZONTAL (direction))
887 c->font->get_glyph_h_advances (count, &info[0].codepoint, sizeof(info[0]),
888 &pos[0].x_advance, sizeof(pos[0]));
889 /* The nil glyph_h_origin() func returns 0, so no need to apply it. */
890 if (c->font->has_glyph_h_origin_func ())
891 for (unsigned int i = 0; i < count; i++)
892 c->font->subtract_glyph_h_origin (info[i].codepoint,
898 c->font->get_glyph_v_advances (count, &info[0].codepoint, sizeof(info[0]),
899 &pos[0].y_advance, sizeof(pos[0]));
900 for (unsigned int i = 0; i < count; i++)
902 c->font->subtract_glyph_v_origin (info[i].codepoint,
907 if (c->buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_SPACE_FALLBACK)
908 _hb_ot_shape_fallback_spaces (c->plan, c->font, c->buffer);
912 hb_ot_position_complex (const hb_ot_shape_context_t *c)
914 unsigned int count = c->buffer->len;
915 hb_glyph_info_t *info = c->buffer->info;
916 hb_glyph_position_t *pos = c->buffer->pos;
918 /* If the font has no GPOS and direction is forward, then when
919 * zeroing mark widths, we shift the mark with it, such that the
920 * mark is positioned hanging over the previous glyph. When
921 * direction is backward we don't shift and it will end up
922 * hanging over the next glyph after the final reordering.
924 * Note: If fallback positinoing happens, we don't care about
925 * this as it will be overriden.
927 bool adjust_offsets_when_zeroing = c->plan->adjust_mark_positioning_when_zeroing &&
928 HB_DIRECTION_IS_FORWARD (c->buffer->props.direction);
930 /* We change glyph origin to what GPOS expects (horizontal), apply GPOS, change it back. */
932 /* The nil glyph_h_origin() func returns 0, so no need to apply it. */
933 if (c->font->has_glyph_h_origin_func ())
934 for (unsigned int i = 0; i < count; i++)
935 c->font->add_glyph_h_origin (info[i].codepoint,
939 hb_ot_layout_position_start (c->font, c->buffer);
941 if (c->plan->zero_marks)
942 switch (c->plan->shaper->zero_width_marks)
944 case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_EARLY:
945 zero_mark_widths_by_gdef (c->buffer, adjust_offsets_when_zeroing);
949 case HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE:
950 case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_LATE:
954 c->plan->position (c->font, c->buffer);
956 if (c->plan->zero_marks)
957 switch (c->plan->shaper->zero_width_marks)
959 case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_LATE:
960 zero_mark_widths_by_gdef (c->buffer, adjust_offsets_when_zeroing);
964 case HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE:
965 case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_EARLY:
969 /* Finish off. Has to follow a certain order. */
970 hb_ot_layout_position_finish_advances (c->font, c->buffer);
971 hb_ot_zero_width_default_ignorables (c->buffer);
972 #ifndef HB_NO_AAT_SHAPE
973 if (c->plan->apply_morx)
974 hb_aat_layout_zero_width_deleted_glyphs (c->buffer);
976 hb_ot_layout_position_finish_offsets (c->font, c->buffer);
978 /* The nil glyph_h_origin() func returns 0, so no need to apply it. */
979 if (c->font->has_glyph_h_origin_func ())
980 for (unsigned int i = 0; i < count; i++)
981 c->font->subtract_glyph_h_origin (info[i].codepoint,
985 if (c->plan->fallback_mark_positioning)
986 _hb_ot_shape_fallback_mark_position (c->plan, c->font, c->buffer,
987 adjust_offsets_when_zeroing);
991 hb_ot_position (const hb_ot_shape_context_t *c)
993 c->buffer->clear_positions ();
995 hb_ot_position_default (c);
997 hb_ot_position_complex (c);
999 if (HB_DIRECTION_IS_BACKWARD (c->buffer->props.direction))
1000 hb_buffer_reverse (c->buffer);
1002 _hb_buffer_deallocate_gsubgpos_vars (c->buffer);
1006 hb_propagate_flags (hb_buffer_t *buffer)
1008 /* Propagate cluster-level glyph flags to be the same on all cluster glyphs.
1009 * Simplifies using them. */
1011 if (!(buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_UNSAFE_TO_BREAK))
1014 hb_glyph_info_t *info = buffer->info;
1016 foreach_cluster (buffer, start, end)
1018 unsigned int mask = 0;
1019 for (unsigned int i = start; i < end; i++)
1020 if (info[i].mask & HB_GLYPH_FLAG_UNSAFE_TO_BREAK)
1022 mask = HB_GLYPH_FLAG_UNSAFE_TO_BREAK;
1026 for (unsigned int i = start; i < end; i++)
1027 info[i].mask |= mask;
1031 /* Pull it all together! */
1034 hb_ot_shape_internal (hb_ot_shape_context_t *c)
1036 c->buffer->deallocate_var_all ();
1037 c->buffer->scratch_flags = HB_BUFFER_SCRATCH_FLAG_DEFAULT;
1038 if (likely (!hb_unsigned_mul_overflows (c->buffer->len, HB_BUFFER_MAX_LEN_FACTOR)))
1040 c->buffer->max_len = hb_max (c->buffer->len * HB_BUFFER_MAX_LEN_FACTOR,
1041 (unsigned) HB_BUFFER_MAX_LEN_MIN);
1043 if (likely (!hb_unsigned_mul_overflows (c->buffer->len, HB_BUFFER_MAX_OPS_FACTOR)))
1045 c->buffer->max_ops = hb_max (c->buffer->len * HB_BUFFER_MAX_OPS_FACTOR,
1046 (unsigned) HB_BUFFER_MAX_OPS_MIN);
1049 /* Save the original direction, we use it later. */
1050 c->target_direction = c->buffer->props.direction;
1052 _hb_buffer_allocate_unicode_vars (c->buffer);
1054 c->buffer->clear_output ();
1056 hb_ot_shape_initialize_masks (c);
1057 hb_set_unicode_props (c->buffer);
1058 hb_insert_dotted_circle (c->buffer, c->font);
1060 hb_form_clusters (c->buffer);
1062 hb_ensure_native_direction (c->buffer);
1064 if (c->plan->shaper->preprocess_text)
1065 c->plan->shaper->preprocess_text (c->plan, c->buffer, c->font);
1067 hb_ot_substitute_pre (c);
1069 hb_ot_substitute_post (c);
1071 hb_propagate_flags (c->buffer);
1073 _hb_buffer_deallocate_unicode_vars (c->buffer);
1075 c->buffer->props.direction = c->target_direction;
1077 c->buffer->max_len = HB_BUFFER_MAX_LEN_DEFAULT;
1078 c->buffer->max_ops = HB_BUFFER_MAX_OPS_DEFAULT;
1079 c->buffer->deallocate_var_all ();
1084 _hb_ot_shape (hb_shape_plan_t *shape_plan,
1086 hb_buffer_t *buffer,
1087 const hb_feature_t *features,
1088 unsigned int num_features)
1090 hb_ot_shape_context_t c = {&shape_plan->ot, font, font->face, buffer, features, num_features};
1091 hb_ot_shape_internal (&c);
1098 * hb_ot_shape_plan_collect_lookups:
1103 hb_ot_shape_plan_collect_lookups (hb_shape_plan_t *shape_plan,
1105 hb_set_t *lookup_indexes /* OUT */)
1107 shape_plan->ot.collect_lookups (table_tag, lookup_indexes);
1111 /* TODO Move this to hb-ot-shape-normalize, make it do decompose, and make it public. */
1113 add_char (hb_font_t *font,
1114 hb_unicode_funcs_t *unicode,
1119 hb_codepoint_t glyph;
1120 if (font->get_nominal_glyph (u, &glyph))
1121 glyphs->add (glyph);
1124 hb_codepoint_t m = unicode->mirroring (u);
1125 if (m != u && font->get_nominal_glyph (m, &glyph))
1126 glyphs->add (glyph);
1132 * hb_ot_shape_glyphs_closure:
1137 hb_ot_shape_glyphs_closure (hb_font_t *font,
1138 hb_buffer_t *buffer,
1139 const hb_feature_t *features,
1140 unsigned int num_features,
1143 const char *shapers[] = {"ot", nullptr};
1144 hb_shape_plan_t *shape_plan = hb_shape_plan_create_cached (font->face, &buffer->props,
1145 features, num_features, shapers);
1147 bool mirror = hb_script_get_horizontal_direction (buffer->props.script) == HB_DIRECTION_RTL;
1149 unsigned int count = buffer->len;
1150 hb_glyph_info_t *info = buffer->info;
1151 for (unsigned int i = 0; i < count; i++)
1152 add_char (font, buffer->unicode, mirror, info[i].codepoint, glyphs);
1154 hb_set_t *lookups = hb_set_create ();
1155 hb_ot_shape_plan_collect_lookups (shape_plan, HB_OT_TAG_GSUB, lookups);
1156 hb_ot_layout_lookups_substitute_closure (font->face, lookups, glyphs);
1158 hb_set_destroy (lookups);
1160 hb_shape_plan_destroy (shape_plan);