2 * Copyright © 2009,2010 Red Hat, Inc.
3 * Copyright © 2010,2011 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
29 #include "hb-ot-shape-private.hh"
30 #include "hb-ot-shape-normalize-private.hh"
32 #include "hb-font-private.hh"
33 #include "hb-set-private.hh"
37 hb_tag_t common_features[] = {
38 HB_TAG('c','c','m','p'),
39 HB_TAG('l','i','g','a'),
40 HB_TAG('l','o','c','l'),
41 HB_TAG('m','a','r','k'),
42 HB_TAG('m','k','m','k'),
43 HB_TAG('r','l','i','g'),
47 hb_tag_t horizontal_features[] = {
48 HB_TAG('c','a','l','t'),
49 HB_TAG('c','l','i','g'),
50 HB_TAG('c','u','r','s'),
51 HB_TAG('k','e','r','n'),
55 * Technically speaking, vrt2 and vert are mutually exclusive.
56 * According to the spec, valt and vpal are also mutually exclusive.
57 * But we apply them all for now.
59 hb_tag_t vertical_features[] = {
60 HB_TAG('v','a','l','t'),
61 HB_TAG('v','e','r','t'),
62 HB_TAG('v','k','r','n'),
63 HB_TAG('v','p','a','l'),
64 HB_TAG('v','r','t','2'),
69 struct hb_ot_shape_planner_t
71 hb_ot_map_builder_t map;
72 hb_ot_complex_shaper_t shaper;
74 hb_ot_shape_planner_t (void) : map () {}
75 ~hb_ot_shape_planner_t (void) { map.finish (); }
77 inline void compile (hb_face_t *face,
78 const hb_segment_properties_t *props,
79 struct hb_ot_shape_plan_t &plan)
82 map.compile (face, props, plan.map);
86 NO_COPY (hb_ot_shape_planner_t);
90 hb_ot_shape_collect_features (hb_ot_shape_planner_t *planner,
91 const hb_segment_properties_t *props,
92 const hb_feature_t *user_features,
93 unsigned int num_user_features)
95 switch (props->direction) {
96 case HB_DIRECTION_LTR:
97 planner->map.add_bool_feature (HB_TAG ('l','t','r','a'));
98 planner->map.add_bool_feature (HB_TAG ('l','t','r','m'));
100 case HB_DIRECTION_RTL:
101 planner->map.add_bool_feature (HB_TAG ('r','t','l','a'));
102 planner->map.add_bool_feature (HB_TAG ('r','t','l','m'), false);
104 case HB_DIRECTION_TTB:
105 case HB_DIRECTION_BTT:
106 case HB_DIRECTION_INVALID:
111 #define ADD_FEATURES(array) \
113 for (unsigned int i = 0; i < ARRAY_LENGTH (array); i++) \
114 planner->map.add_bool_feature (array[i]); \
117 hb_ot_shape_complex_collect_features (planner->shaper, &planner->map, props);
119 ADD_FEATURES (common_features);
121 if (HB_DIRECTION_IS_HORIZONTAL (props->direction))
122 ADD_FEATURES (horizontal_features);
124 ADD_FEATURES (vertical_features);
128 for (unsigned int i = 0; i < num_user_features; i++) {
129 const hb_feature_t *feature = &user_features[i];
130 planner->map.add_feature (feature->tag, feature->value, (feature->start == 0 && feature->end == (unsigned int) -1));
135 struct hb_ot_shape_context_t
137 /* Input to hb_ot_shape_execute() */
138 hb_ot_shape_plan_t *plan;
142 const hb_feature_t *user_features;
143 unsigned int num_user_features;
145 /* Transient stuff */
146 hb_direction_t target_direction;
147 hb_bool_t applied_position_complex;
151 hb_ot_shape_setup_masks (hb_ot_shape_context_t *c)
153 hb_mask_t global_mask = c->plan->map.get_global_mask ();
154 c->buffer->reset_masks (global_mask);
156 hb_ot_shape_complex_setup_masks (c->plan->shaper, &c->plan->map, c->buffer, c->font);
158 for (unsigned int i = 0; i < c->num_user_features; i++)
160 const hb_feature_t *feature = &c->user_features[i];
161 if (!(feature->start == 0 && feature->end == (unsigned int)-1)) {
163 hb_mask_t mask = c->plan->map.get_mask (feature->tag, &shift);
164 c->buffer->set_masks (feature->value << shift, mask, feature->start, feature->end);
175 hb_set_unicode_props (hb_buffer_t *buffer)
177 unsigned int count = buffer->len;
178 for (unsigned int i = 0; i < count; i++)
179 _hb_glyph_info_set_unicode_props (&buffer->info[i], buffer->unicode);
183 hb_form_clusters (hb_buffer_t *buffer)
185 unsigned int count = buffer->len;
186 for (unsigned int i = 1; i < count; i++)
187 if (FLAG (_hb_glyph_info_get_general_category (&buffer->info[i])) &
188 (FLAG (HB_UNICODE_GENERAL_CATEGORY_SPACING_MARK) |
189 FLAG (HB_UNICODE_GENERAL_CATEGORY_ENCLOSING_MARK) |
190 FLAG (HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK)))
191 buffer->info[i].cluster = buffer->info[i - 1].cluster; /* XXX do the min() here */
195 hb_ensure_native_direction (hb_buffer_t *buffer)
197 hb_direction_t direction = buffer->props.direction;
200 * The only BTT vertical script is Ogham, but it's not clear to me whether OpenType
201 * Ogham fonts are supposed to be implemented BTT or not. Need to research that
203 if ((HB_DIRECTION_IS_HORIZONTAL (direction) && direction != hb_script_get_horizontal_direction (buffer->props.script)) ||
204 (HB_DIRECTION_IS_VERTICAL (direction) && direction != HB_DIRECTION_TTB))
206 hb_buffer_reverse_clusters (buffer);
207 buffer->props.direction = HB_DIRECTION_REVERSE (buffer->props.direction);
215 hb_mirror_chars (hb_ot_shape_context_t *c)
217 hb_unicode_funcs_t *unicode = c->buffer->unicode;
219 if (HB_DIRECTION_IS_FORWARD (c->target_direction))
222 hb_mask_t rtlm_mask = c->plan->map.get_1_mask (HB_TAG ('r','t','l','m'));
224 unsigned int count = c->buffer->len;
225 for (unsigned int i = 0; i < count; i++) {
226 hb_codepoint_t codepoint = hb_unicode_mirroring (unicode, c->buffer->info[i].codepoint);
227 if (likely (codepoint == c->buffer->info[i].codepoint))
228 c->buffer->info[i].mask |= rtlm_mask; /* XXX this should be moved to before setting user-feature masks */
230 c->buffer->info[i].codepoint = codepoint;
235 hb_map_glyphs (hb_font_t *font,
238 hb_codepoint_t glyph;
240 if (unlikely (!buffer->len))
243 buffer->clear_output ();
245 unsigned int count = buffer->len - 1;
246 for (buffer->idx = 0; buffer->idx < count;) {
247 if (unlikely (_hb_unicode_is_variation_selector (buffer->cur(+1).codepoint))) {
248 hb_font_get_glyph (font, buffer->cur().codepoint, buffer->cur(+1).codepoint, &glyph);
249 buffer->replace_glyphs (2, 1, &glyph);
251 hb_font_get_glyph (font, buffer->cur().codepoint, 0, &glyph);
252 buffer->replace_glyph (glyph);
255 if (likely (buffer->idx < buffer->len)) {
256 hb_font_get_glyph (font, buffer->cur().codepoint, 0, &glyph);
257 buffer->replace_glyph (glyph);
259 buffer->swap_buffers ();
263 hb_substitute_default (hb_ot_shape_context_t *c)
265 hb_ot_layout_substitute_start (c->buffer);
269 hb_map_glyphs (c->font, c->buffer);
273 hb_ot_substitute_complex (hb_ot_shape_context_t *c)
275 if (hb_ot_layout_has_substitution (c->face)) {
276 c->plan->map.substitute (c->face, c->buffer);
279 hb_ot_layout_substitute_finish (c->buffer);
288 hb_position_default (hb_ot_shape_context_t *c)
290 hb_ot_layout_position_start (c->buffer);
292 unsigned int count = c->buffer->len;
293 for (unsigned int i = 0; i < count; i++) {
294 hb_font_get_glyph_advance_for_direction (c->font, c->buffer->info[i].codepoint,
295 c->buffer->props.direction,
296 &c->buffer->pos[i].x_advance,
297 &c->buffer->pos[i].y_advance);
298 hb_font_subtract_glyph_origin_for_direction (c->font, c->buffer->info[i].codepoint,
299 c->buffer->props.direction,
300 &c->buffer->pos[i].x_offset,
301 &c->buffer->pos[i].y_offset);
306 hb_ot_position_complex (hb_ot_shape_context_t *c)
309 if (hb_ot_layout_has_positioning (c->face))
311 /* Change glyph origin to what GPOS expects, apply GPOS, change it back. */
313 unsigned int count = c->buffer->len;
314 for (unsigned int i = 0; i < count; i++) {
315 hb_font_add_glyph_origin_for_direction (c->font, c->buffer->info[i].codepoint,
317 &c->buffer->pos[i].x_offset,
318 &c->buffer->pos[i].y_offset);
321 c->plan->map.position (c->font, c->buffer);
323 for (unsigned int i = 0; i < count; i++) {
324 hb_font_subtract_glyph_origin_for_direction (c->font, c->buffer->info[i].codepoint,
326 &c->buffer->pos[i].x_offset,
327 &c->buffer->pos[i].y_offset);
330 c->applied_position_complex = true;
333 hb_ot_layout_position_finish (c->buffer);
339 hb_position_complex_fallback (hb_ot_shape_context_t *c HB_UNUSED)
345 hb_truetype_kern (hb_ot_shape_context_t *c)
347 /* TODO Check for kern=0 */
348 unsigned int count = c->buffer->len;
349 for (unsigned int i = 1; i < count; i++) {
350 hb_position_t x_kern, y_kern, kern1, kern2;
351 hb_font_get_glyph_kerning_for_direction (c->font,
352 c->buffer->info[i - 1].codepoint, c->buffer->info[i].codepoint,
353 c->buffer->props.direction,
357 kern2 = x_kern - kern1;
358 c->buffer->pos[i - 1].x_advance += kern1;
359 c->buffer->pos[i].x_advance += kern2;
360 c->buffer->pos[i].x_offset += kern2;
363 kern2 = y_kern - kern1;
364 c->buffer->pos[i - 1].y_advance += kern1;
365 c->buffer->pos[i].y_advance += kern2;
366 c->buffer->pos[i].y_offset += kern2;
371 hb_position_complex_fallback_visual (hb_ot_shape_context_t *c)
373 hb_truetype_kern (c);
377 hb_hide_zerowidth (hb_ot_shape_context_t *c)
379 /* TODO Save the space character in the font? */
380 hb_codepoint_t space;
381 if (!hb_font_get_glyph (c->font, ' ', 0, &space))
382 return; /* No point! */
384 unsigned int count = c->buffer->len;
385 for (unsigned int i = 0; i < count; i++)
386 if (unlikely (_hb_glyph_info_is_zero_width (&c->buffer->info[i]))) {
387 c->buffer->info[i].codepoint = space;
388 c->buffer->pos[i].x_advance = 0;
389 c->buffer->pos[i].y_advance = 0;
397 hb_ot_shape_execute_internal (hb_ot_shape_context_t *c)
399 c->buffer->deallocate_var_all ();
401 /* Save the original direction, we use it later. */
402 c->target_direction = c->buffer->props.direction;
404 HB_BUFFER_ALLOCATE_VAR (c->buffer, unicode_props0);
405 HB_BUFFER_ALLOCATE_VAR (c->buffer, unicode_props1);
407 hb_set_unicode_props (c->buffer);
409 hb_form_clusters (c->buffer);
411 hb_ensure_native_direction (c->buffer);
413 _hb_ot_shape_normalize (c->font, c->buffer, hb_ot_shape_complex_normalization_preference (c->plan->shaper));
415 hb_ot_shape_setup_masks (c);
419 hb_substitute_default (c);
421 hb_ot_substitute_complex (c);
426 hb_position_default (c);
428 hb_ot_position_complex (c);
430 hb_bool_t position_fallback = !c->applied_position_complex;
431 if (position_fallback)
432 hb_position_complex_fallback (c);
434 if (HB_DIRECTION_IS_BACKWARD (c->buffer->props.direction))
435 hb_buffer_reverse (c->buffer);
437 if (position_fallback)
438 hb_position_complex_fallback_visual (c);
441 hb_hide_zerowidth (c);
443 HB_BUFFER_DEALLOCATE_VAR (c->buffer, unicode_props1);
444 HB_BUFFER_DEALLOCATE_VAR (c->buffer, unicode_props0);
446 c->buffer->props.direction = c->target_direction;
448 c->buffer->deallocate_var_all ();
452 hb_ot_shape_plan_internal (hb_ot_shape_plan_t *plan,
454 const hb_segment_properties_t *props,
455 const hb_feature_t *user_features,
456 unsigned int num_user_features)
458 hb_ot_shape_planner_t planner;
460 assert (HB_DIRECTION_IS_VALID (props->direction));
462 planner.shaper = hb_ot_shape_complex_categorize (props);
464 hb_ot_shape_collect_features (&planner, props, user_features, num_user_features);
466 planner.compile (face, props, *plan);
470 hb_ot_shape_execute (hb_ot_shape_plan_t *plan,
473 const hb_feature_t *user_features,
474 unsigned int num_user_features)
476 hb_ot_shape_context_t c = {plan, font, font->face, buffer, user_features, num_user_features};
477 hb_ot_shape_execute_internal (&c);
481 _hb_ot_shape (hb_font_t *font,
483 const hb_feature_t *features,
484 unsigned int num_features)
486 hb_ot_shape_plan_t plan;
488 buffer->guess_properties ();
490 hb_ot_shape_plan_internal (&plan, font->face, &buffer->props, features, num_features);
491 hb_ot_shape_execute (&plan, font, buffer, features, num_features);
498 hb_ot_shape_glyphs_closure (hb_font_t *font,
500 const hb_feature_t *features,
501 unsigned int num_features,
504 hb_ot_shape_plan_t plan;
506 buffer->guess_properties ();
508 hb_ot_shape_plan_internal (&plan, font->face, &buffer->props, features, num_features);
510 /* TODO: normalization? have shapers do closure()? */
511 /* TODO: Deal with mirrored chars? */
512 hb_map_glyphs (font, buffer);
514 /* Seed it. It's user's responsibility to have cleard glyphs
515 * if that's what they desire. */
516 unsigned int count = buffer->len;
517 for (unsigned int i = 0; i < count; i++)
518 hb_set_add (glyphs, buffer->info[i].codepoint);
520 /* And find transitive closure. */
526 plan.map.substitute_closure (font->face, glyphs);
527 } while (!copy.equal (glyphs));