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"
43 #include "hb-ot-layout-gsubgpos-private.hh"
44 //#include "hb-aat-layout-private.hh"
46 static hb_tag_t common_features[] = {
47 HB_TAG('c','c','m','p'),
48 HB_TAG('l','o','c','l'),
49 HB_TAG('m','a','r','k'),
50 HB_TAG('m','k','m','k'),
51 HB_TAG('r','l','i','g'),
55 static hb_tag_t horizontal_features[] = {
56 HB_TAG('c','a','l','t'),
57 HB_TAG('c','l','i','g'),
58 HB_TAG('c','u','r','s'),
59 HB_TAG('k','e','r','n'),
60 HB_TAG('l','i','g','a'),
61 HB_TAG('r','c','l','t'),
67 hb_ot_shape_collect_features (hb_ot_shape_planner_t *planner,
68 const hb_segment_properties_t *props,
69 const hb_feature_t *user_features,
70 unsigned int num_user_features)
72 hb_ot_map_builder_t *map = &planner->map;
74 map->add_global_bool_feature (HB_TAG('r','v','r','n'));
75 map->add_gsub_pause (nullptr);
77 switch (props->direction) {
78 case HB_DIRECTION_LTR:
79 map->add_global_bool_feature (HB_TAG ('l','t','r','a'));
80 map->add_global_bool_feature (HB_TAG ('l','t','r','m'));
82 case HB_DIRECTION_RTL:
83 map->add_global_bool_feature (HB_TAG ('r','t','l','a'));
84 map->add_feature (HB_TAG ('r','t','l','m'), 1, F_NONE);
86 case HB_DIRECTION_TTB:
87 case HB_DIRECTION_BTT:
88 case HB_DIRECTION_INVALID:
93 map->add_feature (HB_TAG ('f','r','a','c'), 1, F_NONE);
94 map->add_feature (HB_TAG ('n','u','m','r'), 1, F_NONE);
95 map->add_feature (HB_TAG ('d','n','o','m'), 1, F_NONE);
97 if (planner->shaper->collect_features)
98 planner->shaper->collect_features (planner);
100 for (unsigned int i = 0; i < ARRAY_LENGTH (common_features); i++)
101 map->add_global_bool_feature (common_features[i]);
103 if (HB_DIRECTION_IS_HORIZONTAL (props->direction))
104 for (unsigned int i = 0; i < ARRAY_LENGTH (horizontal_features); i++)
105 map->add_feature (horizontal_features[i], 1, F_GLOBAL |
106 (horizontal_features[i] == HB_TAG('k','e','r','n') ?
107 F_HAS_FALLBACK : F_NONE));
110 /* We really want to find a 'vert' feature if there's any in the font, no
111 * matter which script/langsys it is listed (or not) under.
112 * See various bugs referenced from:
113 * https://github.com/harfbuzz/harfbuzz/issues/63 */
114 map->add_feature (HB_TAG ('v','e','r','t'), 1, F_GLOBAL | F_GLOBAL_SEARCH);
117 if (planner->shaper->override_features)
118 planner->shaper->override_features (planner);
120 for (unsigned int i = 0; i < num_user_features; i++) {
121 const hb_feature_t *feature = &user_features[i];
122 map->add_feature (feature->tag, feature->value,
123 (feature->start == 0 && feature->end == (unsigned int) -1) ?
133 HB_SHAPER_DATA_ENSURE_DEFINE(ot, face)
135 hb_ot_shaper_face_data_t *
136 _hb_ot_shaper_face_data_create (hb_face_t *face)
138 return _hb_ot_layout_create (face);
142 _hb_ot_shaper_face_data_destroy (hb_ot_shaper_face_data_t *data)
144 _hb_ot_layout_destroy (data);
152 HB_SHAPER_DATA_ENSURE_DEFINE(ot, font)
154 struct hb_ot_shaper_font_data_t {};
156 hb_ot_shaper_font_data_t *
157 _hb_ot_shaper_font_data_create (hb_font_t *font HB_UNUSED)
159 return (hb_ot_shaper_font_data_t *) HB_SHAPER_DATA_SUCCEEDED;
163 _hb_ot_shaper_font_data_destroy (hb_ot_shaper_font_data_t *data)
169 * shaper shape_plan data
172 hb_ot_shaper_shape_plan_data_t *
173 _hb_ot_shaper_shape_plan_data_create (hb_shape_plan_t *shape_plan,
174 const hb_feature_t *user_features,
175 unsigned int num_user_features,
177 unsigned int num_coords)
179 hb_ot_shape_plan_t *plan = (hb_ot_shape_plan_t *) calloc (1, sizeof (hb_ot_shape_plan_t));
180 if (unlikely (!plan))
183 hb_ot_shape_planner_t planner (shape_plan);
185 planner.shaper = hb_ot_shape_complex_categorize (&planner);
187 hb_ot_shape_collect_features (&planner, &shape_plan->props,
188 user_features, num_user_features);
190 planner.compile (*plan, coords, num_coords);
192 if (plan->shaper->data_create) {
193 plan->data = plan->shaper->data_create (plan);
194 if (unlikely (!plan->data))
202 _hb_ot_shaper_shape_plan_data_destroy (hb_ot_shaper_shape_plan_data_t *plan)
204 if (plan->shaper->data_destroy)
205 plan->shaper->data_destroy (const_cast<void *> (plan->data));
217 struct hb_ot_shape_context_t
219 hb_ot_shape_plan_t *plan;
223 const hb_feature_t *user_features;
224 unsigned int num_user_features;
226 /* Transient stuff */
227 bool fallback_positioning;
228 bool fallback_glyph_classes;
229 hb_direction_t target_direction;
240 hb_set_unicode_props (hb_buffer_t *buffer)
242 unsigned int count = buffer->len;
243 hb_glyph_info_t *info = buffer->info;
244 for (unsigned int i = 0; i < count; i++)
245 _hb_glyph_info_set_unicode_props (&info[i], buffer);
249 hb_insert_dotted_circle (hb_buffer_t *buffer, hb_font_t *font)
251 if (!(buffer->flags & HB_BUFFER_FLAG_BOT) ||
252 buffer->context_len[0] ||
253 _hb_glyph_info_get_general_category (&buffer->info[0]) !=
254 HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK)
257 if (!font->has_glyph (0x25CCu))
260 hb_glyph_info_t dottedcircle = {0};
261 dottedcircle.codepoint = 0x25CCu;
262 _hb_glyph_info_set_unicode_props (&dottedcircle, buffer);
264 buffer->clear_output ();
267 hb_glyph_info_t info = dottedcircle;
268 info.cluster = buffer->cur().cluster;
269 info.mask = buffer->cur().mask;
270 buffer->output_info (info);
271 while (buffer->idx < buffer->len && !buffer->in_error)
272 buffer->next_glyph ();
274 buffer->swap_buffers ();
278 hb_form_clusters (hb_buffer_t *buffer)
280 if (!(buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_NON_ASCII))
283 /* Loop duplicated in hb_ensure_native_direction(), and in _hb-coretext.cc */
284 unsigned int base = 0;
285 unsigned int count = buffer->len;
286 hb_glyph_info_t *info = buffer->info;
287 for (unsigned int i = 1; i < count; i++)
289 if (likely (!HB_UNICODE_GENERAL_CATEGORY_IS_MARK (_hb_glyph_info_get_general_category (&info[i])) &&
290 !_hb_glyph_info_is_joiner (&info[i])))
292 if (buffer->cluster_level == HB_BUFFER_CLUSTER_LEVEL_MONOTONE_GRAPHEMES)
293 buffer->merge_clusters (base, i);
295 buffer->unsafe_to_break (base, i);
299 if (buffer->cluster_level == HB_BUFFER_CLUSTER_LEVEL_MONOTONE_GRAPHEMES)
300 buffer->merge_clusters (base, count);
302 buffer->unsafe_to_break (base, count);
306 hb_ensure_native_direction (hb_buffer_t *buffer)
308 hb_direction_t direction = buffer->props.direction;
311 * The only BTT vertical script is Ogham, but it's not clear to me whether OpenType
312 * Ogham fonts are supposed to be implemented BTT or not. Need to research that
314 if ((HB_DIRECTION_IS_HORIZONTAL (direction) && direction != hb_script_get_horizontal_direction (buffer->props.script)) ||
315 (HB_DIRECTION_IS_VERTICAL (direction) && direction != HB_DIRECTION_TTB))
317 /* Same loop as hb_form_clusters().
318 * Since form_clusters() merged clusters already, we don't merge. */
319 unsigned int base = 0;
320 unsigned int count = buffer->len;
321 hb_glyph_info_t *info = buffer->info;
322 for (unsigned int i = 1; i < count; i++)
324 if (likely (!HB_UNICODE_GENERAL_CATEGORY_IS_MARK (_hb_glyph_info_get_general_category (&info[i]))))
326 if (buffer->cluster_level == HB_BUFFER_CLUSTER_LEVEL_MONOTONE_CHARACTERS)
327 buffer->merge_clusters (base, i);
328 buffer->reverse_range (base, i);
333 if (buffer->cluster_level == HB_BUFFER_CLUSTER_LEVEL_MONOTONE_CHARACTERS)
334 buffer->merge_clusters (base, count);
335 buffer->reverse_range (base, count);
339 buffer->props.direction = HB_DIRECTION_REVERSE (buffer->props.direction);
347 hb_ot_mirror_chars (hb_ot_shape_context_t *c)
349 if (HB_DIRECTION_IS_FORWARD (c->target_direction))
352 hb_buffer_t *buffer = c->buffer;
353 hb_unicode_funcs_t *unicode = buffer->unicode;
354 hb_mask_t rtlm_mask = c->plan->rtlm_mask;
356 unsigned int count = buffer->len;
357 hb_glyph_info_t *info = buffer->info;
358 for (unsigned int i = 0; i < count; i++) {
359 hb_codepoint_t codepoint = unicode->mirroring (info[i].codepoint);
360 if (likely (codepoint == info[i].codepoint || !c->font->has_glyph (codepoint)))
361 info[i].mask |= rtlm_mask;
363 info[i].codepoint = codepoint;
368 hb_ot_shape_setup_masks_fraction (hb_ot_shape_context_t *c)
370 if (!(c->buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_NON_ASCII) ||
374 hb_buffer_t *buffer = c->buffer;
376 hb_mask_t pre_mask, post_mask;
377 if (HB_DIRECTION_IS_FORWARD (buffer->props.direction))
379 pre_mask = c->plan->numr_mask | c->plan->frac_mask;
380 post_mask = c->plan->frac_mask | c->plan->dnom_mask;
384 pre_mask = c->plan->frac_mask | c->plan->dnom_mask;
385 post_mask = c->plan->numr_mask | c->plan->frac_mask;
388 unsigned int count = buffer->len;
389 hb_glyph_info_t *info = buffer->info;
390 for (unsigned int i = 0; i < count; i++)
392 if (info[i].codepoint == 0x2044u) /* FRACTION SLASH */
394 unsigned int start = i, end = i + 1;
396 _hb_glyph_info_get_general_category (&info[start - 1]) ==
397 HB_UNICODE_GENERAL_CATEGORY_DECIMAL_NUMBER)
399 while (end < count &&
400 _hb_glyph_info_get_general_category (&info[end]) ==
401 HB_UNICODE_GENERAL_CATEGORY_DECIMAL_NUMBER)
404 buffer->unsafe_to_break (start, end);
406 for (unsigned int j = start; j < i; j++)
407 info[j].mask |= pre_mask;
408 info[i].mask |= c->plan->frac_mask;
409 for (unsigned int j = i + 1; j < end; j++)
410 info[j].mask |= post_mask;
418 hb_ot_shape_initialize_masks (hb_ot_shape_context_t *c)
420 hb_ot_map_t *map = &c->plan->map;
421 hb_buffer_t *buffer = c->buffer;
423 hb_mask_t global_mask = map->get_global_mask ();
424 buffer->reset_masks (global_mask);
428 hb_ot_shape_setup_masks (hb_ot_shape_context_t *c)
430 hb_ot_map_t *map = &c->plan->map;
431 hb_buffer_t *buffer = c->buffer;
433 hb_ot_shape_setup_masks_fraction (c);
435 if (c->plan->shaper->setup_masks)
436 c->plan->shaper->setup_masks (c->plan, buffer, c->font);
438 for (unsigned int i = 0; i < c->num_user_features; i++)
440 const hb_feature_t *feature = &c->user_features[i];
441 if (!(feature->start == 0 && feature->end == (unsigned int)-1)) {
443 hb_mask_t mask = map->get_mask (feature->tag, &shift);
444 buffer->set_masks (feature->value << shift, mask, feature->start, feature->end);
450 hb_ot_zero_width_default_ignorables (hb_ot_shape_context_t *c)
452 hb_buffer_t *buffer = c->buffer;
454 if (!(buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_DEFAULT_IGNORABLES) ||
455 (buffer->flags & HB_BUFFER_FLAG_PRESERVE_DEFAULT_IGNORABLES) ||
456 (buffer->flags & HB_BUFFER_FLAG_REMOVE_DEFAULT_IGNORABLES))
459 unsigned int count = buffer->len;
460 hb_glyph_info_t *info = buffer->info;
461 hb_glyph_position_t *pos = buffer->pos;
463 for (i = 0; i < count; i++)
464 if (unlikely (_hb_glyph_info_is_default_ignorable (&info[i])))
465 pos[i].x_advance = pos[i].y_advance = pos[i].x_offset = pos[i].y_offset = 0;
469 hb_ot_hide_default_ignorables (hb_ot_shape_context_t *c)
471 hb_buffer_t *buffer = c->buffer;
473 if (!(buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_DEFAULT_IGNORABLES) ||
474 (buffer->flags & HB_BUFFER_FLAG_PRESERVE_DEFAULT_IGNORABLES))
477 unsigned int count = buffer->len;
478 hb_glyph_info_t *info = buffer->info;
479 hb_glyph_position_t *pos = buffer->pos;
481 for (i = 0; i < count; i++)
483 if (unlikely (_hb_glyph_info_is_default_ignorable (&info[i])))
487 /* No default-ignorables found; return. */
491 hb_codepoint_t space;
492 if (!(buffer->flags & HB_BUFFER_FLAG_REMOVE_DEFAULT_IGNORABLES) &&
493 c->font->get_nominal_glyph (' ', &space))
495 /* Replace default-ignorables with a zero-advance space glyph. */
496 for (/*continue*/; i < count; i++)
498 if (_hb_glyph_info_is_default_ignorable (&info[i]))
499 info[i].codepoint = space;
504 /* Merge clusters and delete default-ignorables.
505 * NOTE! We can't use out-buffer as we have positioning data. */
507 for (; i < count; i++)
509 if (_hb_glyph_info_is_default_ignorable (&info[i]))
512 * Same logic as buffer->delete_glyph(), but for in-place removal. */
514 unsigned int cluster = info[i].cluster;
515 if (i + 1 < count && cluster == info[i + 1].cluster)
516 continue; /* Cluster survives; do nothing. */
520 /* Merge cluster backward. */
521 if (cluster < info[j - 1].cluster)
523 unsigned int mask = info[i].mask;
524 unsigned int old_cluster = info[j - 1].cluster;
525 for (unsigned k = j; k && info[k - 1].cluster == old_cluster; k--)
526 buffer->set_cluster (info[k - 1], cluster, mask);
532 buffer->merge_clusters (i, i + 2); /* Merge cluster forward. */
550 hb_ot_map_glyphs_fast (hb_buffer_t *buffer)
552 /* Normalization process sets up glyph_index(), we just copy it. */
553 unsigned int count = buffer->len;
554 hb_glyph_info_t *info = buffer->info;
555 for (unsigned int i = 0; i < count; i++)
556 info[i].codepoint = info[i].glyph_index();
558 buffer->content_type = HB_BUFFER_CONTENT_TYPE_GLYPHS;
562 hb_synthesize_glyph_classes (hb_ot_shape_context_t *c)
564 unsigned int count = c->buffer->len;
565 hb_glyph_info_t *info = c->buffer->info;
566 for (unsigned int i = 0; i < count; i++)
568 hb_ot_layout_glyph_props_flags_t klass;
570 /* Never mark default-ignorables as marks.
571 * They won't get in the way of lookups anyway,
572 * but having them as mark will cause them to be skipped
573 * over if the lookup-flag says so, but at least for the
574 * Mongolian variation selectors, looks like Uniscribe
575 * marks them as non-mark. Some Mongolian fonts without
576 * GDEF rely on this. Another notable character that
577 * this applies to is COMBINING GRAPHEME JOINER. */
578 klass = (_hb_glyph_info_get_general_category (&info[i]) !=
579 HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK ||
580 _hb_glyph_info_is_default_ignorable (&info[i])) ?
581 HB_OT_LAYOUT_GLYPH_PROPS_BASE_GLYPH :
582 HB_OT_LAYOUT_GLYPH_PROPS_MARK;
583 _hb_glyph_info_set_glyph_props (&info[i], klass);
588 hb_ot_substitute_default (hb_ot_shape_context_t *c)
590 hb_buffer_t *buffer = c->buffer;
592 hb_ot_mirror_chars (c);
594 HB_BUFFER_ALLOCATE_VAR (buffer, glyph_index);
596 _hb_ot_shape_normalize (c->plan, buffer, c->font);
598 hb_ot_shape_setup_masks (c);
600 /* This is unfortunate to go here, but necessary... */
601 if (c->fallback_positioning)
602 _hb_ot_shape_fallback_position_recategorize_marks (c->plan, c->font, buffer);
604 hb_ot_map_glyphs_fast (buffer);
606 HB_BUFFER_DEALLOCATE_VAR (buffer, glyph_index);
610 hb_ot_substitute_complex (hb_ot_shape_context_t *c)
612 hb_buffer_t *buffer = c->buffer;
614 hb_ot_layout_substitute_start (c->font, buffer);
616 if (!hb_ot_layout_has_glyph_classes (c->face))
617 hb_synthesize_glyph_classes (c);
619 c->plan->substitute (c->font, buffer);
621 /* XXX Call morx instead. */
622 //hb_aat_layout_substitute (c->font, c->buffer);
626 hb_ot_substitute (hb_ot_shape_context_t *c)
628 hb_ot_substitute_default (c);
630 _hb_buffer_allocate_gsubgpos_vars (c->buffer);
632 hb_ot_substitute_complex (c);
638 adjust_mark_offsets (hb_glyph_position_t *pos)
640 pos->x_offset -= pos->x_advance;
641 pos->y_offset -= pos->y_advance;
645 zero_mark_width (hb_glyph_position_t *pos)
652 zero_mark_widths_by_gdef (hb_buffer_t *buffer, bool adjust_offsets)
654 unsigned int count = buffer->len;
655 hb_glyph_info_t *info = buffer->info;
656 for (unsigned int i = 0; i < count; i++)
657 if (_hb_glyph_info_is_mark (&info[i]))
660 adjust_mark_offsets (&buffer->pos[i]);
661 zero_mark_width (&buffer->pos[i]);
666 hb_ot_position_default (hb_ot_shape_context_t *c)
668 hb_direction_t direction = c->buffer->props.direction;
669 unsigned int count = c->buffer->len;
670 hb_glyph_info_t *info = c->buffer->info;
671 hb_glyph_position_t *pos = c->buffer->pos;
673 if (HB_DIRECTION_IS_HORIZONTAL (direction))
675 for (unsigned int i = 0; i < count; i++)
676 pos[i].x_advance = c->font->get_glyph_h_advance (info[i].codepoint);
677 /* The nil glyph_h_origin() func returns 0, so no need to apply it. */
678 if (c->font->has_glyph_h_origin_func ())
679 for (unsigned int i = 0; i < count; i++)
680 c->font->subtract_glyph_h_origin (info[i].codepoint,
686 for (unsigned int i = 0; i < count; i++)
688 pos[i].y_advance = c->font->get_glyph_v_advance (info[i].codepoint);
689 c->font->subtract_glyph_v_origin (info[i].codepoint,
694 if (c->buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_SPACE_FALLBACK)
695 _hb_ot_shape_fallback_spaces (c->plan, c->font, c->buffer);
699 hb_ot_position_complex (hb_ot_shape_context_t *c)
701 unsigned int count = c->buffer->len;
702 hb_glyph_info_t *info = c->buffer->info;
703 hb_glyph_position_t *pos = c->buffer->pos;
705 /* If the font has no GPOS, AND, no fallback positioning will
706 * happen, AND, direction is forward, then when zeroing mark
707 * widths, we shift the mark with it, such that the mark
708 * is positioned hanging over the previous glyph. When
709 * direction is backward we don't shift and it will end up
710 * hanging over the next glyph after the final reordering.
711 * If fallback positinoing happens or GPOS is present, we don't
714 bool adjust_offsets_when_zeroing = c->fallback_positioning &&
715 !c->plan->shaper->fallback_position &&
716 HB_DIRECTION_IS_FORWARD (c->buffer->props.direction);
718 /* We change glyph origin to what GPOS expects (horizontal), apply GPOS, change it back. */
720 /* The nil glyph_h_origin() func returns 0, so no need to apply it. */
721 if (c->font->has_glyph_h_origin_func ())
722 for (unsigned int i = 0; i < count; i++)
723 c->font->add_glyph_h_origin (info[i].codepoint,
727 hb_ot_layout_position_start (c->font, c->buffer);
729 switch (c->plan->shaper->zero_width_marks)
731 case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_EARLY:
732 zero_mark_widths_by_gdef (c->buffer, adjust_offsets_when_zeroing);
736 case HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE:
737 case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_LATE:
741 if (likely (!c->fallback_positioning))
742 c->plan->position (c->font, c->buffer);
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);
761 /* The nil glyph_h_origin() func returns 0, so no need to apply it. */
762 if (c->font->has_glyph_h_origin_func ())
763 for (unsigned int i = 0; i < count; i++)
764 c->font->subtract_glyph_h_origin (info[i].codepoint,
770 hb_ot_position (hb_ot_shape_context_t *c)
772 c->buffer->clear_positions ();
774 hb_ot_position_default (c);
776 hb_ot_position_complex (c);
778 if (c->fallback_positioning && c->plan->shaper->fallback_position)
779 _hb_ot_shape_fallback_position (c->plan, c->font, c->buffer);
781 if (HB_DIRECTION_IS_BACKWARD (c->buffer->props.direction))
782 hb_buffer_reverse (c->buffer);
784 /* Visual fallback goes here. */
786 if (c->fallback_positioning)
787 _hb_ot_shape_fallback_kern (c->plan, c->font, c->buffer);
789 _hb_buffer_deallocate_gsubgpos_vars (c->buffer);
791 //hb_aat_layout_position (c->font, c->buffer);
795 hb_propagate_flags (hb_buffer_t *buffer)
797 /* Propagate cluster-level glyph flags to be the same on all cluster glyphs.
798 * Simplifies using them. */
800 if (!(buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_UNSAFE_TO_BREAK))
803 hb_glyph_info_t *info = buffer->info;
805 foreach_cluster (buffer, start, end)
807 unsigned int mask = 0;
808 for (unsigned int i = start; i < end; i++)
809 if (info[i].mask & HB_GLYPH_FLAG_UNSAFE_TO_BREAK)
811 mask = HB_GLYPH_FLAG_UNSAFE_TO_BREAK;
815 for (unsigned int i = start; i < end; i++)
816 info[i].mask |= mask;
820 /* Pull it all together! */
823 hb_ot_shape_internal (hb_ot_shape_context_t *c)
825 c->buffer->deallocate_var_all ();
826 c->buffer->scratch_flags = HB_BUFFER_SCRATCH_FLAG_DEFAULT;
827 if (likely (!_hb_unsigned_int_mul_overflows (c->buffer->len, HB_BUFFER_MAX_LEN_FACTOR)))
829 c->buffer->max_len = MAX (c->buffer->len * HB_BUFFER_MAX_LEN_FACTOR,
830 (unsigned) HB_BUFFER_MAX_LEN_MIN);
832 if (likely (!_hb_unsigned_int_mul_overflows (c->buffer->len, HB_BUFFER_MAX_OPS_FACTOR)))
834 c->buffer->max_ops = MAX (c->buffer->len * HB_BUFFER_MAX_OPS_FACTOR,
835 (unsigned) HB_BUFFER_MAX_OPS_MIN);
838 bool disable_otl = c->plan->shaper->disable_otl && c->plan->shaper->disable_otl (c->plan);
839 //c->fallback_substitute = disable_otl || !hb_ot_layout_has_substitution (c->face);
840 c->fallback_positioning = disable_otl || !hb_ot_layout_has_positioning (c->face);
841 c->fallback_glyph_classes = disable_otl || !hb_ot_layout_has_glyph_classes (c->face);
843 /* Save the original direction, we use it later. */
844 c->target_direction = c->buffer->props.direction;
846 _hb_buffer_allocate_unicode_vars (c->buffer);
848 c->buffer->clear_output ();
850 hb_ot_shape_initialize_masks (c);
851 hb_set_unicode_props (c->buffer);
852 hb_insert_dotted_circle (c->buffer, c->font);
854 hb_form_clusters (c->buffer);
856 hb_ensure_native_direction (c->buffer);
858 if (c->plan->shaper->preprocess_text)
859 c->plan->shaper->preprocess_text (c->plan, c->buffer, c->font);
861 hb_ot_substitute (c);
864 hb_ot_hide_default_ignorables (c);
866 if (c->plan->shaper->postprocess_glyphs)
867 c->plan->shaper->postprocess_glyphs (c->plan, c->buffer, c->font);
869 hb_propagate_flags (c->buffer);
871 _hb_buffer_deallocate_unicode_vars (c->buffer);
873 c->buffer->props.direction = c->target_direction;
875 c->buffer->max_len = HB_BUFFER_MAX_LEN_DEFAULT;
876 c->buffer->max_ops = HB_BUFFER_MAX_OPS_DEFAULT;
877 c->buffer->deallocate_var_all ();
882 _hb_ot_shape (hb_shape_plan_t *shape_plan,
885 const hb_feature_t *features,
886 unsigned int num_features)
888 hb_ot_shape_context_t c = {HB_SHAPER_DATA_GET (shape_plan), font, font->face, buffer, features, num_features};
889 hb_ot_shape_internal (&c);
896 * hb_ot_shape_plan_collect_lookups:
901 hb_ot_shape_plan_collect_lookups (hb_shape_plan_t *shape_plan,
903 hb_set_t *lookup_indexes /* OUT */)
905 /* XXX Does the first part always succeed? */
906 HB_SHAPER_DATA_GET (shape_plan)->collect_lookups (table_tag, lookup_indexes);
910 /* TODO Move this to hb-ot-shape-normalize, make it do decompose, and make it public. */
912 add_char (hb_font_t *font,
913 hb_unicode_funcs_t *unicode,
918 hb_codepoint_t glyph;
919 if (font->get_nominal_glyph (u, &glyph))
923 hb_codepoint_t m = unicode->mirroring (u);
924 if (m != u && font->get_nominal_glyph (m, &glyph))
931 * hb_ot_shape_glyphs_closure:
936 hb_ot_shape_glyphs_closure (hb_font_t *font,
938 const hb_feature_t *features,
939 unsigned int num_features,
942 hb_ot_shape_plan_t plan;
944 const char *shapers[] = {"ot", nullptr};
945 hb_shape_plan_t *shape_plan = hb_shape_plan_create_cached (font->face, &buffer->props,
946 features, num_features, shapers);
948 bool mirror = hb_script_get_horizontal_direction (buffer->props.script) == HB_DIRECTION_RTL;
950 unsigned int count = buffer->len;
951 hb_glyph_info_t *info = buffer->info;
952 for (unsigned int i = 0; i < count; i++)
953 add_char (font, buffer->unicode, mirror, info[i].codepoint, glyphs);
955 hb_set_t *lookups = hb_set_create ();
956 hb_ot_shape_plan_collect_lookups (shape_plan, HB_OT_TAG_GSUB, lookups);
958 /* And find transitive closure. */
959 hb_set_t *copy = hb_set_create ();
962 for (hb_codepoint_t lookup_index = HB_SET_VALUE_INVALID; hb_set_next (lookups, &lookup_index);)
963 hb_ot_layout_lookup_substitute_closure (font->face, lookup_index, glyphs);
964 } while (!copy->is_equal (glyphs));
965 hb_set_destroy (copy);
967 hb_set_destroy (lookups);
969 hb_shape_plan_destroy (shape_plan);