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-set-private.hh"
43 static hb_tag_t common_features[] = {
44 HB_TAG('c','c','m','p'),
45 HB_TAG('l','i','g','a'),
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('r','c','l','t'),
62 * Technically speaking, vrt2 and vert are mutually exclusive.
63 * According to the spec, valt and vpal are also mutually exclusive.
64 * But we apply them all for now.
66 static hb_tag_t vertical_features[] = {
67 HB_TAG('v','a','l','t'),
68 HB_TAG('v','e','r','t'),
69 HB_TAG('v','k','r','n'),
70 HB_TAG('v','p','a','l'),
71 HB_TAG('v','r','t','2'),
77 hb_ot_shape_collect_features (hb_ot_shape_planner_t *planner,
78 const hb_segment_properties_t *props,
79 const hb_feature_t *user_features,
80 unsigned int num_user_features)
82 hb_ot_map_builder_t *map = &planner->map;
84 switch (props->direction) {
85 case HB_DIRECTION_LTR:
86 map->add_bool_feature (HB_TAG ('l','t','r','a'));
87 map->add_bool_feature (HB_TAG ('l','t','r','m'));
89 case HB_DIRECTION_RTL:
90 map->add_bool_feature (HB_TAG ('r','t','l','a'));
91 map->add_bool_feature (HB_TAG ('r','t','l','m'), false);
93 case HB_DIRECTION_TTB:
94 case HB_DIRECTION_BTT:
95 case HB_DIRECTION_INVALID:
100 #define ADD_FEATURES(array) \
102 for (unsigned int i = 0; i < ARRAY_LENGTH (array); i++) \
103 map->add_bool_feature (array[i]); \
106 if (planner->shaper->collect_features)
107 planner->shaper->collect_features (planner);
109 ADD_FEATURES (common_features);
111 if (HB_DIRECTION_IS_HORIZONTAL (props->direction))
112 ADD_FEATURES (horizontal_features);
114 ADD_FEATURES (vertical_features);
116 if (planner->shaper->override_features)
117 planner->shaper->override_features (planner);
121 for (unsigned int i = 0; i < num_user_features; i++) {
122 const hb_feature_t *feature = &user_features[i];
123 map->add_feature (feature->tag, feature->value, (feature->start == 0 && feature->end == (unsigned int) -1));
132 hb_ot_shaper_face_data_t *
133 _hb_ot_shaper_face_data_create (hb_face_t *face)
135 return _hb_ot_layout_create (face);
139 _hb_ot_shaper_face_data_destroy (hb_ot_shaper_face_data_t *data)
141 _hb_ot_layout_destroy (data);
149 struct hb_ot_shaper_font_data_t {};
151 hb_ot_shaper_font_data_t *
152 _hb_ot_shaper_font_data_create (hb_font_t *font)
154 return (hb_ot_shaper_font_data_t *) HB_SHAPER_DATA_SUCCEEDED;
158 _hb_ot_shaper_font_data_destroy (hb_ot_shaper_font_data_t *data)
164 * shaper shape_plan data
167 hb_ot_shaper_shape_plan_data_t *
168 _hb_ot_shaper_shape_plan_data_create (hb_shape_plan_t *shape_plan,
169 const hb_feature_t *user_features,
170 unsigned int num_user_features)
172 hb_ot_shape_plan_t *plan = (hb_ot_shape_plan_t *) calloc (1, sizeof (hb_ot_shape_plan_t));
173 if (unlikely (!plan))
176 hb_ot_shape_planner_t planner (shape_plan);
178 planner.shaper = hb_ot_shape_complex_categorize (&shape_plan->props);
180 hb_ot_shape_collect_features (&planner, &shape_plan->props, user_features, num_user_features);
182 planner.compile (*plan);
184 if (plan->shaper->data_create) {
185 plan->data = plan->shaper->data_create (plan);
186 if (unlikely (!plan->data))
194 _hb_ot_shaper_shape_plan_data_destroy (hb_ot_shaper_shape_plan_data_t *plan)
196 if (plan->shaper->data_destroy)
197 plan->shaper->data_destroy (const_cast<void *> (plan->data));
209 struct hb_ot_shape_context_t
211 hb_ot_shape_plan_t *plan;
215 const hb_feature_t *user_features;
216 unsigned int num_user_features;
218 /* Transient stuff */
219 hb_direction_t target_direction;
230 hb_set_unicode_props (hb_buffer_t *buffer)
232 unsigned int count = buffer->len;
233 for (unsigned int i = 0; i < count; i++)
234 _hb_glyph_info_set_unicode_props (&buffer->info[i], buffer->unicode);
238 hb_form_clusters (hb_buffer_t *buffer)
240 unsigned int count = buffer->len;
241 for (unsigned int i = 1; i < count; i++)
242 if (FLAG (_hb_glyph_info_get_general_category (&buffer->info[i])) &
243 (FLAG (HB_UNICODE_GENERAL_CATEGORY_SPACING_MARK) |
244 FLAG (HB_UNICODE_GENERAL_CATEGORY_ENCLOSING_MARK) |
245 FLAG (HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK)))
246 buffer->merge_clusters (i - 1, i + 1);
250 hb_ensure_native_direction (hb_buffer_t *buffer)
252 hb_direction_t direction = buffer->props.direction;
255 * The only BTT vertical script is Ogham, but it's not clear to me whether OpenType
256 * Ogham fonts are supposed to be implemented BTT or not. Need to research that
258 if ((HB_DIRECTION_IS_HORIZONTAL (direction) && direction != hb_script_get_horizontal_direction (buffer->props.script)) ||
259 (HB_DIRECTION_IS_VERTICAL (direction) && direction != HB_DIRECTION_TTB))
261 hb_buffer_reverse_clusters (buffer);
262 buffer->props.direction = HB_DIRECTION_REVERSE (buffer->props.direction);
270 hb_ot_mirror_chars (hb_ot_shape_context_t *c)
272 if (HB_DIRECTION_IS_FORWARD (c->target_direction))
275 hb_unicode_funcs_t *unicode = c->buffer->unicode;
276 hb_mask_t rtlm_mask = c->plan->map.get_1_mask (HB_TAG ('r','t','l','m'));
278 unsigned int count = c->buffer->len;
279 for (unsigned int i = 0; i < count; i++) {
280 hb_codepoint_t codepoint = unicode->mirroring (c->buffer->info[i].codepoint);
281 if (likely (codepoint == c->buffer->info[i].codepoint))
282 c->buffer->info[i].mask |= rtlm_mask;
284 c->buffer->info[i].codepoint = codepoint;
289 hb_ot_shape_setup_masks (hb_ot_shape_context_t *c)
291 hb_ot_map_t *map = &c->plan->map;
293 hb_mask_t global_mask = map->get_global_mask ();
294 c->buffer->reset_masks (global_mask);
296 if (c->plan->shaper->setup_masks)
297 c->plan->shaper->setup_masks (c->plan, c->buffer, c->font);
299 for (unsigned int i = 0; i < c->num_user_features; i++)
301 const hb_feature_t *feature = &c->user_features[i];
302 if (!(feature->start == 0 && feature->end == (unsigned int)-1)) {
304 hb_mask_t mask = map->get_mask (feature->tag, &shift);
305 c->buffer->set_masks (feature->value << shift, mask, feature->start, feature->end);
311 hb_ot_map_glyphs_fast (hb_buffer_t *buffer)
313 /* Normalization process sets up glyph_index(), we just copy it. */
314 unsigned int count = buffer->len;
315 for (unsigned int i = 0; i < count; i++)
316 buffer->info[i].codepoint = buffer->info[i].glyph_index();
320 hb_ot_substitute_default (hb_ot_shape_context_t *c)
322 if (c->plan->shaper->preprocess_text)
323 c->plan->shaper->preprocess_text (c->plan, c->buffer, c->font);
325 hb_ot_mirror_chars (c);
327 HB_BUFFER_ALLOCATE_VAR (c->buffer, glyph_index);
329 _hb_ot_shape_normalize (c->font, c->buffer,
330 c->plan->shaper->normalization_preference ?
331 c->plan->shaper->normalization_preference (c->plan) :
332 HB_OT_SHAPE_NORMALIZATION_MODE_DEFAULT);
334 hb_ot_shape_setup_masks (c);
336 hb_ot_map_glyphs_fast (c->buffer);
338 HB_BUFFER_DEALLOCATE_VAR (c->buffer, glyph_index);
342 hb_synthesize_glyph_classes (hb_ot_shape_context_t *c)
344 unsigned int count = c->buffer->len;
345 for (unsigned int i = 0; i < count; i++)
346 c->buffer->info[i].glyph_props() = _hb_glyph_info_get_general_category (&c->buffer->info[i]) == HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK ?
347 HB_OT_LAYOUT_GLYPH_CLASS_MARK :
348 HB_OT_LAYOUT_GLYPH_CLASS_BASE_GLYPH;
353 hb_ot_substitute_complex (hb_ot_shape_context_t *c)
355 hb_ot_layout_substitute_start (c->font, c->buffer);
357 if (!hb_ot_layout_has_glyph_classes (c->face))
358 hb_synthesize_glyph_classes (c);
360 if (hb_ot_layout_has_substitution (c->face))
361 c->plan->substitute (c->font, c->buffer);
363 hb_ot_layout_substitute_finish (c->font, c->buffer);
369 hb_ot_substitute (hb_ot_shape_context_t *c)
371 hb_ot_substitute_default (c);
372 hb_ot_substitute_complex (c);
378 hb_ot_position_default (hb_ot_shape_context_t *c)
380 hb_ot_layout_position_start (c->font, c->buffer);
382 unsigned int count = c->buffer->len;
383 for (unsigned int i = 0; i < count; i++) {
384 c->font->get_glyph_advance_for_direction (c->buffer->info[i].codepoint,
385 c->buffer->props.direction,
386 &c->buffer->pos[i].x_advance,
387 &c->buffer->pos[i].y_advance);
388 c->font->subtract_glyph_origin_for_direction (c->buffer->info[i].codepoint,
389 c->buffer->props.direction,
390 &c->buffer->pos[i].x_offset,
391 &c->buffer->pos[i].y_offset);
396 hb_ot_position_complex (hb_ot_shape_context_t *c)
400 if (hb_ot_layout_has_positioning (c->face))
402 /* Change glyph origin to what GPOS expects, apply GPOS, change it back. */
404 unsigned int count = c->buffer->len;
405 for (unsigned int i = 0; i < count; i++) {
406 c->font->add_glyph_origin_for_direction (c->buffer->info[i].codepoint,
408 &c->buffer->pos[i].x_offset,
409 &c->buffer->pos[i].y_offset);
412 c->plan->position (c->font, c->buffer);
414 for (unsigned int i = 0; i < count; i++) {
415 c->font->subtract_glyph_origin_for_direction (c->buffer->info[i].codepoint,
417 &c->buffer->pos[i].x_offset,
418 &c->buffer->pos[i].y_offset);
424 hb_ot_layout_position_finish (c->font, c->buffer, c->plan->shaper->zero_width_attached_marks);
430 hb_ot_position_complex_fallback (hb_ot_shape_context_t *c)
432 _hb_ot_shape_fallback_position (c->plan, c->font, c->buffer);
436 hb_ot_truetype_kern (hb_ot_shape_context_t *c)
438 /* TODO Check for kern=0 */
439 unsigned int count = c->buffer->len;
440 for (unsigned int i = 1; i < count; i++) {
441 hb_position_t x_kern, y_kern, kern1, kern2;
442 c->font->get_glyph_kerning_for_direction (c->buffer->info[i - 1].codepoint, c->buffer->info[i].codepoint,
443 c->buffer->props.direction,
447 kern2 = x_kern - kern1;
448 c->buffer->pos[i - 1].x_advance += kern1;
449 c->buffer->pos[i].x_advance += kern2;
450 c->buffer->pos[i].x_offset += kern2;
453 kern2 = y_kern - kern1;
454 c->buffer->pos[i - 1].y_advance += kern1;
455 c->buffer->pos[i].y_advance += kern2;
456 c->buffer->pos[i].y_offset += kern2;
461 hb_position_complex_fallback_visual (hb_ot_shape_context_t *c)
463 hb_ot_truetype_kern (c);
467 hb_ot_position (hb_ot_shape_context_t *c)
469 hb_ot_position_default (c);
471 hb_bool_t fallback = !hb_ot_position_complex (c);
474 hb_ot_position_complex_fallback (c);
476 if (HB_DIRECTION_IS_BACKWARD (c->buffer->props.direction))
477 hb_buffer_reverse (c->buffer);
480 hb_position_complex_fallback_visual (c);
487 hb_ot_hide_zerowidth (hb_ot_shape_context_t *c)
489 hb_codepoint_t space;
490 if (!c->font->get_glyph (' ', 0, &space))
491 return; /* No point! */
493 unsigned int count = c->buffer->len;
494 for (unsigned int i = 0; i < count; i++)
495 if (unlikely (!is_a_ligature (c->buffer->info[i]) &&
496 _hb_glyph_info_is_zero_width (&c->buffer->info[i]))) {
497 c->buffer->info[i].codepoint = space;
498 c->buffer->pos[i].x_advance = 0;
499 c->buffer->pos[i].y_advance = 0;
504 /* Pull it all together! */
507 hb_ot_shape_internal (hb_ot_shape_context_t *c)
509 c->buffer->deallocate_var_all ();
511 /* Save the original direction, we use it later. */
512 c->target_direction = c->buffer->props.direction;
514 HB_BUFFER_ALLOCATE_VAR (c->buffer, unicode_props0);
515 HB_BUFFER_ALLOCATE_VAR (c->buffer, unicode_props1);
517 c->buffer->clear_output ();
519 hb_set_unicode_props (c->buffer);
520 hb_form_clusters (c->buffer);
522 hb_ensure_native_direction (c->buffer);
524 hb_ot_substitute (c);
527 hb_ot_hide_zerowidth (c);
529 HB_BUFFER_DEALLOCATE_VAR (c->buffer, unicode_props1);
530 HB_BUFFER_DEALLOCATE_VAR (c->buffer, unicode_props0);
532 c->buffer->props.direction = c->target_direction;
534 c->buffer->deallocate_var_all ();
539 _hb_ot_shape (hb_shape_plan_t *shape_plan,
542 const hb_feature_t *features,
543 unsigned int num_features)
545 hb_ot_shape_context_t c = {HB_SHAPER_DATA_GET (shape_plan), font, font->face, buffer, features, num_features};
546 hb_ot_shape_internal (&c);
554 hb_ot_map_glyphs_dumb (hb_font_t *font,
557 unsigned int count = buffer->len;
558 for (unsigned int i = 0; i < count; i++)
559 font->get_glyph (buffer->cur().codepoint, 0, &buffer->cur().codepoint);
563 hb_ot_shape_glyphs_closure (hb_font_t *font,
565 const hb_feature_t *features,
566 unsigned int num_features,
569 hb_ot_shape_plan_t plan;
571 buffer->guess_properties ();
573 /* TODO cache / ensure correct backend, etc. */
574 hb_shape_plan_t *shape_plan = hb_shape_plan_create (font->face, &buffer->props, features, num_features, NULL);
576 /* TODO: normalization? have shapers do closure()? */
577 /* TODO: Deal with mirrored chars? */
578 hb_ot_map_glyphs_dumb (font, buffer);
580 /* Seed it. It's user's responsibility to have cleard glyphs
581 * if that's what they desire. */
582 unsigned int count = buffer->len;
583 for (unsigned int i = 0; i < count; i++)
584 glyphs->add (buffer->info[i].codepoint);
586 /* And find transitive closure. */
592 HB_SHAPER_DATA_GET (shape_plan)->substitute_closure (font->face, glyphs);
593 } while (!copy.equal (glyphs));
595 hb_shape_plan_destroy (shape_plan);