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
30 #define hb_ot_shaper_face_data_t hb_ot_layout_t
31 #define hb_ot_shaper_shape_plan_data_t hb_ot_shape_plan_t
32 #include "hb-shaper-impl-private.hh"
34 #include "hb-ot-shape-private.hh"
35 #include "hb-ot-shape-complex-private.hh"
36 #include "hb-ot-shape-fallback-private.hh"
37 #include "hb-ot-shape-normalize-private.hh"
39 #include "hb-ot-layout-private.hh"
40 #include "hb-unicode-private.hh"
41 #include "hb-set-private.hh"
44 static hb_tag_t common_features[] = {
45 HB_TAG('c','c','m','p'),
46 HB_TAG('l','o','c','l'),
47 HB_TAG('m','a','r','k'),
48 HB_TAG('m','k','m','k'),
49 HB_TAG('r','l','i','g'),
53 static hb_tag_t horizontal_features[] = {
54 HB_TAG('c','a','l','t'),
55 HB_TAG('c','l','i','g'),
56 HB_TAG('c','u','r','s'),
57 HB_TAG('k','e','r','n'),
58 HB_TAG('l','i','g','a'),
59 HB_TAG('r','c','l','t'),
65 hb_ot_shape_collect_features (hb_ot_shape_planner_t *planner,
66 const hb_segment_properties_t *props,
67 const hb_feature_t *user_features,
68 unsigned int num_user_features)
70 hb_ot_map_builder_t *map = &planner->map;
72 map->add_global_bool_feature (HB_TAG('r','v','r','n'));
73 map->add_gsub_pause (NULL);
75 switch (props->direction) {
76 case HB_DIRECTION_LTR:
77 map->add_global_bool_feature (HB_TAG ('l','t','r','a'));
78 map->add_global_bool_feature (HB_TAG ('l','t','r','m'));
80 case HB_DIRECTION_RTL:
81 map->add_global_bool_feature (HB_TAG ('r','t','l','a'));
82 map->add_feature (HB_TAG ('r','t','l','m'), 1, F_NONE);
84 case HB_DIRECTION_TTB:
85 case HB_DIRECTION_BTT:
86 case HB_DIRECTION_INVALID:
91 map->add_feature (HB_TAG ('f','r','a','c'), 1, F_NONE);
92 map->add_feature (HB_TAG ('n','u','m','r'), 1, F_NONE);
93 map->add_feature (HB_TAG ('d','n','o','m'), 1, F_NONE);
95 if (planner->shaper->collect_features)
96 planner->shaper->collect_features (planner);
98 for (unsigned int i = 0; i < ARRAY_LENGTH (common_features); i++)
99 map->add_global_bool_feature (common_features[i]);
101 if (HB_DIRECTION_IS_HORIZONTAL (props->direction))
102 for (unsigned int i = 0; i < ARRAY_LENGTH (horizontal_features); i++)
103 map->add_feature (horizontal_features[i], 1, F_GLOBAL |
104 (horizontal_features[i] == HB_TAG('k','e','r','n') ?
105 F_HAS_FALLBACK : F_NONE));
108 /* We really want to find a 'vert' feature if there's any in the font, no
109 * matter which script/langsys it is listed (or not) under.
110 * See various bugs referenced from:
111 * https://github.com/behdad/harfbuzz/issues/63 */
112 map->add_feature (HB_TAG ('v','e','r','t'), 1, F_GLOBAL | F_GLOBAL_SEARCH);
115 if (planner->shaper->override_features)
116 planner->shaper->override_features (planner);
118 for (unsigned int i = 0; i < num_user_features; i++) {
119 const hb_feature_t *feature = &user_features[i];
120 map->add_feature (feature->tag, feature->value,
121 (feature->start == 0 && feature->end == (unsigned int) -1) ?
131 HB_SHAPER_DATA_ENSURE_DEFINE(ot, face)
133 hb_ot_shaper_face_data_t *
134 _hb_ot_shaper_face_data_create (hb_face_t *face)
136 return _hb_ot_layout_create (face);
140 _hb_ot_shaper_face_data_destroy (hb_ot_shaper_face_data_t *data)
142 _hb_ot_layout_destroy (data);
150 HB_SHAPER_DATA_ENSURE_DEFINE(ot, font)
152 struct hb_ot_shaper_font_data_t {};
154 hb_ot_shaper_font_data_t *
155 _hb_ot_shaper_font_data_create (hb_font_t *font HB_UNUSED)
157 return (hb_ot_shaper_font_data_t *) HB_SHAPER_DATA_SUCCEEDED;
161 _hb_ot_shaper_font_data_destroy (hb_ot_shaper_font_data_t *data)
167 * shaper shape_plan data
170 hb_ot_shaper_shape_plan_data_t *
171 _hb_ot_shaper_shape_plan_data_create (hb_shape_plan_t *shape_plan,
172 const hb_feature_t *user_features,
173 unsigned int num_user_features,
175 unsigned int num_coords)
177 hb_ot_shape_plan_t *plan = (hb_ot_shape_plan_t *) calloc (1, sizeof (hb_ot_shape_plan_t));
178 if (unlikely (!plan))
181 hb_ot_shape_planner_t planner (shape_plan);
183 planner.shaper = hb_ot_shape_complex_categorize (&planner);
185 hb_ot_shape_collect_features (&planner, &shape_plan->props,
186 user_features, num_user_features);
188 planner.compile (*plan, coords, num_coords);
190 if (plan->shaper->data_create) {
191 plan->data = plan->shaper->data_create (plan);
192 if (unlikely (!plan->data))
200 _hb_ot_shaper_shape_plan_data_destroy (hb_ot_shaper_shape_plan_data_t *plan)
202 if (plan->shaper->data_destroy)
203 plan->shaper->data_destroy (const_cast<void *> (plan->data));
215 struct hb_ot_shape_context_t
217 hb_ot_shape_plan_t *plan;
221 const hb_feature_t *user_features;
222 unsigned int num_user_features;
224 /* Transient stuff */
225 bool fallback_positioning;
226 bool fallback_glyph_classes;
227 hb_direction_t target_direction;
238 hb_set_unicode_props (hb_buffer_t *buffer)
240 unsigned int count = buffer->len;
241 hb_glyph_info_t *info = buffer->info;
242 for (unsigned int i = 0; i < count; i++)
243 _hb_glyph_info_set_unicode_props (&info[i], buffer);
247 hb_insert_dotted_circle (hb_buffer_t *buffer, hb_font_t *font)
249 if (!(buffer->flags & HB_BUFFER_FLAG_BOT) ||
250 buffer->context_len[0] ||
251 _hb_glyph_info_get_general_category (&buffer->info[0]) !=
252 HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK)
255 if (!font->has_glyph (0x25CCu))
258 hb_glyph_info_t dottedcircle = {0};
259 dottedcircle.codepoint = 0x25CCu;
260 _hb_glyph_info_set_unicode_props (&dottedcircle, buffer);
262 buffer->clear_output ();
265 hb_glyph_info_t info = dottedcircle;
266 info.cluster = buffer->cur().cluster;
267 info.mask = buffer->cur().mask;
268 buffer->output_info (info);
269 while (buffer->idx < buffer->len && !buffer->in_error)
270 buffer->next_glyph ();
272 buffer->swap_buffers ();
276 hb_form_clusters (hb_buffer_t *buffer)
278 if (!(buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_NON_ASCII) ||
279 buffer->cluster_level != HB_BUFFER_CLUSTER_LEVEL_MONOTONE_GRAPHEMES)
282 /* Loop duplicated in hb_ensure_native_direction(), and in _hb-coretext.cc */
283 unsigned int base = 0;
284 unsigned int count = buffer->len;
285 hb_glyph_info_t *info = buffer->info;
286 for (unsigned int i = 1; i < count; i++)
288 if (likely (!HB_UNICODE_GENERAL_CATEGORY_IS_MARK (_hb_glyph_info_get_general_category (&info[i])) &&
289 !_hb_glyph_info_is_joiner (&info[i])))
291 buffer->merge_clusters (base, i);
295 buffer->merge_clusters (base, count);
299 hb_ensure_native_direction (hb_buffer_t *buffer)
301 hb_direction_t direction = buffer->props.direction;
304 * The only BTT vertical script is Ogham, but it's not clear to me whether OpenType
305 * Ogham fonts are supposed to be implemented BTT or not. Need to research that
307 if ((HB_DIRECTION_IS_HORIZONTAL (direction) && direction != hb_script_get_horizontal_direction (buffer->props.script)) ||
308 (HB_DIRECTION_IS_VERTICAL (direction) && direction != HB_DIRECTION_TTB))
310 /* Same loop as hb_form_clusters().
311 * Since form_clusters() merged clusters already, we don't merge. */
312 unsigned int base = 0;
313 unsigned int count = buffer->len;
314 hb_glyph_info_t *info = buffer->info;
315 for (unsigned int i = 1; i < count; i++)
317 if (likely (!HB_UNICODE_GENERAL_CATEGORY_IS_MARK (_hb_glyph_info_get_general_category (&info[i]))))
319 if (buffer->cluster_level == HB_BUFFER_CLUSTER_LEVEL_MONOTONE_CHARACTERS)
320 buffer->merge_clusters (base, i);
321 buffer->reverse_range (base, i);
326 if (buffer->cluster_level == HB_BUFFER_CLUSTER_LEVEL_MONOTONE_CHARACTERS)
327 buffer->merge_clusters (base, count);
328 buffer->reverse_range (base, count);
332 buffer->props.direction = HB_DIRECTION_REVERSE (buffer->props.direction);
340 hb_ot_mirror_chars (hb_ot_shape_context_t *c)
342 if (HB_DIRECTION_IS_FORWARD (c->target_direction))
345 hb_buffer_t *buffer = c->buffer;
346 hb_unicode_funcs_t *unicode = buffer->unicode;
347 hb_mask_t rtlm_mask = c->plan->rtlm_mask;
349 unsigned int count = buffer->len;
350 hb_glyph_info_t *info = buffer->info;
351 for (unsigned int i = 0; i < count; i++) {
352 hb_codepoint_t codepoint = unicode->mirroring (info[i].codepoint);
353 if (likely (codepoint == info[i].codepoint || !c->font->has_glyph (codepoint)))
354 info[i].mask |= rtlm_mask;
356 info[i].codepoint = codepoint;
361 hb_ot_shape_setup_masks_fraction (hb_ot_shape_context_t *c)
363 if (!(c->buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_NON_ASCII) ||
367 hb_buffer_t *buffer = c->buffer;
369 hb_mask_t pre_mask, post_mask;
370 if (HB_DIRECTION_IS_FORWARD (buffer->props.direction))
372 pre_mask = c->plan->numr_mask | c->plan->frac_mask;
373 post_mask = c->plan->frac_mask | c->plan->dnom_mask;
377 pre_mask = c->plan->frac_mask | c->plan->dnom_mask;
378 post_mask = c->plan->numr_mask | c->plan->frac_mask;
381 unsigned int count = buffer->len;
382 hb_glyph_info_t *info = buffer->info;
383 for (unsigned int i = 0; i < count; i++)
385 if (info[i].codepoint == 0x2044u) /* FRACTION SLASH */
387 unsigned int start = i, end = i + 1;
389 _hb_glyph_info_get_general_category (&info[start - 1]) ==
390 HB_UNICODE_GENERAL_CATEGORY_DECIMAL_NUMBER)
392 while (end < count &&
393 _hb_glyph_info_get_general_category (&info[end]) ==
394 HB_UNICODE_GENERAL_CATEGORY_DECIMAL_NUMBER)
397 for (unsigned int j = start; j < i; j++)
398 info[j].mask |= pre_mask;
399 info[i].mask |= c->plan->frac_mask;
400 for (unsigned int j = i + 1; j < end; j++)
401 info[j].mask |= post_mask;
409 hb_ot_shape_initialize_masks (hb_ot_shape_context_t *c)
411 hb_ot_map_t *map = &c->plan->map;
412 hb_buffer_t *buffer = c->buffer;
414 hb_mask_t global_mask = map->get_global_mask ();
415 buffer->reset_masks (global_mask);
419 hb_ot_shape_setup_masks (hb_ot_shape_context_t *c)
421 hb_ot_map_t *map = &c->plan->map;
422 hb_buffer_t *buffer = c->buffer;
424 hb_ot_shape_setup_masks_fraction (c);
426 if (c->plan->shaper->setup_masks)
427 c->plan->shaper->setup_masks (c->plan, buffer, c->font);
429 for (unsigned int i = 0; i < c->num_user_features; i++)
431 const hb_feature_t *feature = &c->user_features[i];
432 if (!(feature->start == 0 && feature->end == (unsigned int)-1)) {
434 hb_mask_t mask = map->get_mask (feature->tag, &shift);
435 buffer->set_masks (feature->value << shift, mask, feature->start, feature->end);
441 hb_ot_zero_width_default_ignorables (hb_ot_shape_context_t *c)
443 hb_buffer_t *buffer = c->buffer;
445 if (!(buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_DEFAULT_IGNORABLES) ||
446 (buffer->flags & HB_BUFFER_FLAG_PRESERVE_DEFAULT_IGNORABLES))
449 unsigned int count = buffer->len;
450 hb_glyph_info_t *info = buffer->info;
451 hb_glyph_position_t *pos = buffer->pos;
453 for (i = 0; i < count; i++)
454 if (unlikely (_hb_glyph_info_is_default_ignorable (&info[i])))
455 pos[i].x_advance = pos[i].y_advance = pos[i].x_offset = pos[i].y_offset = 0;
459 hb_ot_hide_default_ignorables (hb_ot_shape_context_t *c)
461 hb_buffer_t *buffer = c->buffer;
463 if (!(buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_DEFAULT_IGNORABLES) ||
464 (buffer->flags & HB_BUFFER_FLAG_PRESERVE_DEFAULT_IGNORABLES))
467 unsigned int count = buffer->len;
468 hb_glyph_info_t *info = buffer->info;
469 hb_glyph_position_t *pos = buffer->pos;
471 for (i = 0; i < count; i++)
473 if (unlikely (_hb_glyph_info_is_default_ignorable (&info[i])))
477 /* No default-ignorables found; return. */
481 hb_codepoint_t space;
482 if (c->font->get_nominal_glyph (' ', &space))
484 /* Replace default-ignorables with a zero-advance space glyph. */
485 for (/*continue*/; i < count; i++)
487 if (_hb_glyph_info_is_default_ignorable (&info[i]))
488 info[i].codepoint = space;
493 /* Merge clusters and delete default-ignorables.
494 * NOTE! We can't use out-buffer as we have positioning data. */
496 for (; i < count; i++)
498 if (_hb_glyph_info_is_default_ignorable (&info[i]))
501 * Same logic as buffer->delete_glyph(), but for in-place removal. */
503 unsigned int cluster = info[i].cluster;
504 if (i + 1 < count && cluster == info[i + 1].cluster)
505 continue; /* Cluster survives; do nothing. */
509 /* Merge cluster backward. */
510 if (cluster < info[j - 1].cluster)
512 unsigned int old_cluster = info[j - 1].cluster;
513 for (unsigned k = j; k && info[k - 1].cluster == old_cluster; k--)
514 info[k - 1].cluster = cluster;
520 buffer->merge_clusters (i, i + 2); /* Merge cluster forward. */
538 hb_ot_map_glyphs_fast (hb_buffer_t *buffer)
540 /* Normalization process sets up glyph_index(), we just copy it. */
541 unsigned int count = buffer->len;
542 hb_glyph_info_t *info = buffer->info;
543 for (unsigned int i = 0; i < count; i++)
544 info[i].codepoint = info[i].glyph_index();
546 buffer->content_type = HB_BUFFER_CONTENT_TYPE_GLYPHS;
550 hb_synthesize_glyph_classes (hb_ot_shape_context_t *c)
552 unsigned int count = c->buffer->len;
553 hb_glyph_info_t *info = c->buffer->info;
554 for (unsigned int i = 0; i < count; i++)
556 hb_ot_layout_glyph_props_flags_t klass;
558 /* Never mark default-ignorables as marks.
559 * They won't get in the way of lookups anyway,
560 * but having them as mark will cause them to be skipped
561 * over if the lookup-flag says so, but at least for the
562 * Mongolian variation selectors, looks like Uniscribe
563 * marks them as non-mark. Some Mongolian fonts without
564 * GDEF rely on this. Another notable character that
565 * this applies to is COMBINING GRAPHEME JOINER. */
566 klass = (_hb_glyph_info_get_general_category (&info[i]) !=
567 HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK ||
568 _hb_glyph_info_is_default_ignorable (&info[i])) ?
569 HB_OT_LAYOUT_GLYPH_PROPS_BASE_GLYPH :
570 HB_OT_LAYOUT_GLYPH_PROPS_MARK;
571 _hb_glyph_info_set_glyph_props (&info[i], klass);
576 hb_ot_substitute_default (hb_ot_shape_context_t *c)
578 hb_buffer_t *buffer = c->buffer;
580 hb_ot_shape_initialize_masks (c);
582 hb_ot_mirror_chars (c);
584 HB_BUFFER_ALLOCATE_VAR (buffer, glyph_index);
586 _hb_ot_shape_normalize (c->plan, buffer, c->font);
588 hb_ot_shape_setup_masks (c);
590 /* This is unfortunate to go here, but necessary... */
591 if (c->fallback_positioning)
592 _hb_ot_shape_fallback_position_recategorize_marks (c->plan, c->font, buffer);
594 hb_ot_map_glyphs_fast (buffer);
596 HB_BUFFER_DEALLOCATE_VAR (buffer, glyph_index);
600 hb_ot_substitute_complex (hb_ot_shape_context_t *c)
602 hb_buffer_t *buffer = c->buffer;
604 hb_ot_layout_substitute_start (c->font, buffer);
606 if (!hb_ot_layout_has_glyph_classes (c->face))
607 hb_synthesize_glyph_classes (c);
609 c->plan->substitute (c->font, buffer);
615 hb_ot_substitute (hb_ot_shape_context_t *c)
617 hb_ot_substitute_default (c);
619 _hb_buffer_allocate_gsubgpos_vars (c->buffer);
621 hb_ot_substitute_complex (c);
627 adjust_mark_offsets (hb_glyph_position_t *pos)
629 pos->x_offset -= pos->x_advance;
630 pos->y_offset -= pos->y_advance;
634 zero_mark_width (hb_glyph_position_t *pos)
641 zero_mark_widths_by_gdef (hb_buffer_t *buffer, bool adjust_offsets)
643 unsigned int count = buffer->len;
644 hb_glyph_info_t *info = buffer->info;
645 for (unsigned int i = 0; i < count; i++)
646 if (_hb_glyph_info_is_mark (&info[i]))
649 adjust_mark_offsets (&buffer->pos[i]);
650 zero_mark_width (&buffer->pos[i]);
655 hb_ot_position_default (hb_ot_shape_context_t *c)
657 hb_direction_t direction = c->buffer->props.direction;
658 unsigned int count = c->buffer->len;
659 hb_glyph_info_t *info = c->buffer->info;
660 hb_glyph_position_t *pos = c->buffer->pos;
662 if (HB_DIRECTION_IS_HORIZONTAL (direction))
664 for (unsigned int i = 0; i < count; i++)
665 pos[i].x_advance = c->font->get_glyph_h_advance (info[i].codepoint);
666 /* The nil glyph_h_origin() func returns 0, so no need to apply it. */
667 if (c->font->has_glyph_h_origin_func ())
668 for (unsigned int i = 0; i < count; i++)
669 c->font->subtract_glyph_h_origin (info[i].codepoint,
675 for (unsigned int i = 0; i < count; i++)
677 pos[i].y_advance = c->font->get_glyph_v_advance (info[i].codepoint);
678 c->font->subtract_glyph_v_origin (info[i].codepoint,
683 if (c->buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_SPACE_FALLBACK)
684 _hb_ot_shape_fallback_spaces (c->plan, c->font, c->buffer);
688 hb_ot_position_complex (hb_ot_shape_context_t *c)
690 hb_ot_layout_position_start (c->font, c->buffer);
692 unsigned int count = c->buffer->len;
694 /* If the font has no GPOS, AND, no fallback positioning will
695 * happen, AND, direction is forward, then when zeroing mark
696 * widths, we shift the mark with it, such that the mark
697 * is positioned hanging over the previous glyph. When
698 * direction is backward we don't shift and it will end up
699 * hanging over the next glyph after the final reordering.
700 * If fallback positinoing happens or GPOS is present, we don't
703 bool adjust_offsets_when_zeroing = c->fallback_positioning &&
704 !c->plan->shaper->fallback_position &&
705 HB_DIRECTION_IS_FORWARD (c->buffer->props.direction);
707 switch (c->plan->shaper->zero_width_marks)
709 case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_EARLY:
710 zero_mark_widths_by_gdef (c->buffer, adjust_offsets_when_zeroing);
714 case HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE:
715 case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_LATE:
719 if (likely (!c->fallback_positioning))
721 hb_glyph_info_t *info = c->buffer->info;
722 hb_glyph_position_t *pos = c->buffer->pos;
724 /* Change glyph origin to what GPOS expects (horizontal), apply GPOS, change it back. */
726 /* The nil glyph_h_origin() func returns 0, so no need to apply it. */
727 if (c->font->has_glyph_h_origin_func ())
728 for (unsigned int i = 0; i < count; i++)
729 c->font->add_glyph_h_origin (info[i].codepoint,
733 c->plan->position (c->font, c->buffer);
735 /* The nil glyph_h_origin() func returns 0, so no need to apply it. */
736 if (c->font->has_glyph_h_origin_func ())
737 for (unsigned int i = 0; i < count; i++)
738 c->font->subtract_glyph_h_origin (info[i].codepoint,
744 switch (c->plan->shaper->zero_width_marks)
746 case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_LATE:
747 zero_mark_widths_by_gdef (c->buffer, adjust_offsets_when_zeroing);
751 case HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE:
752 case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_EARLY:
756 /* Finishing off GPOS has to follow a certain order. */
757 hb_ot_layout_position_finish_advances (c->font, c->buffer);
758 hb_ot_zero_width_default_ignorables (c);
759 hb_ot_layout_position_finish_offsets (c->font, c->buffer);
763 hb_ot_position (hb_ot_shape_context_t *c)
765 c->buffer->clear_positions ();
767 hb_ot_position_default (c);
769 hb_ot_position_complex (c);
771 if (c->fallback_positioning && c->plan->shaper->fallback_position)
772 _hb_ot_shape_fallback_position (c->plan, c->font, c->buffer);
774 if (HB_DIRECTION_IS_BACKWARD (c->buffer->props.direction))
775 hb_buffer_reverse (c->buffer);
777 /* Visual fallback goes here. */
779 if (c->fallback_positioning)
780 _hb_ot_shape_fallback_kern (c->plan, c->font, c->buffer);
782 _hb_buffer_deallocate_gsubgpos_vars (c->buffer);
786 /* Pull it all together! */
789 hb_ot_shape_internal (hb_ot_shape_context_t *c)
791 c->buffer->deallocate_var_all ();
792 c->buffer->scratch_flags = HB_BUFFER_SCRATCH_FLAG_DEFAULT;
793 if (likely (!_hb_unsigned_int_mul_overflows (c->buffer->len, HB_BUFFER_MAX_EXPANSION_FACTOR)))
795 c->buffer->max_len = MAX (c->buffer->len * HB_BUFFER_MAX_EXPANSION_FACTOR,
796 (unsigned) HB_BUFFER_MAX_LEN_MIN);
799 bool disable_otl = c->plan->shaper->disable_otl && c->plan->shaper->disable_otl (c->plan);
800 //c->fallback_substitute = disable_otl || !hb_ot_layout_has_substitution (c->face);
801 c->fallback_positioning = disable_otl || !hb_ot_layout_has_positioning (c->face);
802 c->fallback_glyph_classes = disable_otl || !hb_ot_layout_has_glyph_classes (c->face);
804 /* Save the original direction, we use it later. */
805 c->target_direction = c->buffer->props.direction;
807 _hb_buffer_allocate_unicode_vars (c->buffer);
809 c->buffer->clear_output ();
811 hb_set_unicode_props (c->buffer);
812 hb_insert_dotted_circle (c->buffer, c->font);
813 hb_form_clusters (c->buffer);
815 hb_ensure_native_direction (c->buffer);
817 if (c->plan->shaper->preprocess_text)
818 c->plan->shaper->preprocess_text (c->plan, c->buffer, c->font);
820 hb_ot_substitute (c);
823 hb_ot_hide_default_ignorables (c);
825 if (c->plan->shaper->postprocess_glyphs)
826 c->plan->shaper->postprocess_glyphs (c->plan, c->buffer, c->font);
828 _hb_buffer_deallocate_unicode_vars (c->buffer);
830 c->buffer->props.direction = c->target_direction;
832 c->buffer->max_len = HB_BUFFER_MAX_LEN_DEFAULT;
833 c->buffer->deallocate_var_all ();
838 _hb_ot_shape (hb_shape_plan_t *shape_plan,
841 const hb_feature_t *features,
842 unsigned int num_features)
844 hb_ot_shape_context_t c = {HB_SHAPER_DATA_GET (shape_plan), font, font->face, buffer, features, num_features};
845 hb_ot_shape_internal (&c);
852 * hb_ot_shape_plan_collect_lookups:
857 hb_ot_shape_plan_collect_lookups (hb_shape_plan_t *shape_plan,
859 hb_set_t *lookup_indexes /* OUT */)
861 /* XXX Does the first part always succeed? */
862 HB_SHAPER_DATA_GET (shape_plan)->collect_lookups (table_tag, lookup_indexes);
866 /* TODO Move this to hb-ot-shape-normalize, make it do decompose, and make it public. */
868 add_char (hb_font_t *font,
869 hb_unicode_funcs_t *unicode,
874 hb_codepoint_t glyph;
875 if (font->get_nominal_glyph (u, &glyph))
879 hb_codepoint_t m = unicode->mirroring (u);
880 if (m != u && font->get_nominal_glyph (m, &glyph))
887 * hb_ot_shape_glyphs_closure:
892 hb_ot_shape_glyphs_closure (hb_font_t *font,
894 const hb_feature_t *features,
895 unsigned int num_features,
898 hb_ot_shape_plan_t plan;
900 const char *shapers[] = {"ot", NULL};
901 hb_shape_plan_t *shape_plan = hb_shape_plan_create_cached (font->face, &buffer->props,
902 features, num_features, shapers);
904 bool mirror = hb_script_get_horizontal_direction (buffer->props.script) == HB_DIRECTION_RTL;
906 unsigned int count = buffer->len;
907 hb_glyph_info_t *info = buffer->info;
908 for (unsigned int i = 0; i < count; i++)
909 add_char (font, buffer->unicode, mirror, info[i].codepoint, glyphs);
913 hb_ot_shape_plan_collect_lookups (shape_plan, HB_OT_TAG_GSUB, &lookups);
915 /* And find transitive closure. */
920 for (hb_codepoint_t lookup_index = -1; hb_set_next (&lookups, &lookup_index);)
921 hb_ot_layout_lookup_substitute_closure (font->face, lookup_index, glyphs);
922 } while (!copy.is_equal (glyphs));
924 hb_shape_plan_destroy (shape_plan);