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-complex-private.hh"
32 #include "hb-font-private.hh"
37 hb_tag_t early_features[] = {
38 HB_TAG('c','c','m','p'),
39 HB_TAG('l','o','c','l'),
42 hb_tag_t common_features[] = {
43 HB_TAG('m','a','r','k'),
44 HB_TAG('m','k','m','k'),
45 HB_TAG('r','l','i','g'),
48 hb_tag_t horizontal_features[] = {
49 HB_TAG('c','a','l','t'),
50 HB_TAG('c','l','i','g'),
51 HB_TAG('c','u','r','s'),
52 HB_TAG('k','e','r','n'),
53 HB_TAG('l','i','g','a'),
57 * Technically speaking, vrt2 and vert are mutually exclusive.
58 * According to the spec, valt and vpal are also mutually exclusive.
59 * But we apply them all for now.
61 hb_tag_t vertical_features[] = {
62 HB_TAG('v','a','l','t'),
63 HB_TAG('v','e','r','t'),
64 HB_TAG('v','k','r','n'),
65 HB_TAG('v','p','a','l'),
66 HB_TAG('v','r','t','2'),
70 hb_ot_shape_collect_features (hb_ot_shape_planner_t *planner,
71 const hb_segment_properties_t *props,
72 const hb_feature_t *user_features,
73 unsigned int num_user_features)
75 switch (props->direction) {
76 case HB_DIRECTION_LTR:
77 planner->map.add_bool_feature (HB_TAG ('l','t','r','a'));
78 planner->map.add_bool_feature (HB_TAG ('l','t','r','m'));
80 case HB_DIRECTION_RTL:
81 planner->map.add_bool_feature (HB_TAG ('r','t','l','a'));
82 planner->map.add_bool_feature (HB_TAG ('r','t','l','m'), false);
84 case HB_DIRECTION_TTB:
85 case HB_DIRECTION_BTT:
86 case HB_DIRECTION_INVALID:
91 #define ADD_FEATURES(array) \
93 for (unsigned int i = 0; i < ARRAY_LENGTH (array); i++) \
94 planner->map.add_bool_feature (array[i]); \
97 ADD_FEATURES (early_features);
99 hb_ot_shape_complex_collect_features (planner, props);
101 ADD_FEATURES (common_features);
103 if (HB_DIRECTION_IS_HORIZONTAL (props->direction))
104 ADD_FEATURES (horizontal_features);
106 ADD_FEATURES (vertical_features);
110 for (unsigned int i = 0; i < num_user_features; i++) {
111 const hb_feature_t *feature = &user_features[i];
112 planner->map.add_feature (feature->tag, feature->value, (feature->start == 0 && feature->end == (unsigned int) -1));
118 hb_ot_shape_setup_masks (hb_ot_shape_context_t *c)
120 hb_mask_t global_mask = c->plan->map.get_global_mask ();
121 c->buffer->reset_masks (global_mask);
123 hb_ot_shape_complex_setup_masks (c); /* BUFFER: Clobbers var2 */
125 for (unsigned int i = 0; i < c->num_user_features; i++)
127 const hb_feature_t *feature = &c->user_features[i];
128 if (!(feature->start == 0 && feature->end == (unsigned int)-1)) {
130 hb_mask_t mask = c->plan->map.get_mask (feature->tag, &shift);
131 c->buffer->set_masks (feature->value << shift, mask, feature->start, feature->end);
138 hb_ot_substitute_complex (hb_ot_shape_context_t *c)
140 if (!hb_ot_layout_has_substitution (c->face))
143 c->plan->map.substitute (c->face, c->buffer);
145 c->applied_substitute_complex = TRUE;
150 hb_ot_position_complex (hb_ot_shape_context_t *c)
153 if (!hb_ot_layout_has_positioning (c->face))
156 unsigned int count = c->buffer->len;
157 for (unsigned int i = 0; i < count; i++) {
158 hb_font_add_glyph_origin_for_direction (c->font, c->buffer->info[i].codepoint,
160 &c->buffer->pos[i].x_offset,
161 &c->buffer->pos[i].y_offset);
164 c->plan->map.position (c->font, c->buffer);
166 for (unsigned int i = 0; i < count; i++) {
167 hb_font_subtract_glyph_origin_for_direction (c->font, c->buffer->info[i].codepoint,
169 &c->buffer->pos[i].x_offset,
170 &c->buffer->pos[i].y_offset);
173 hb_ot_layout_position_finish (c->buffer);
175 c->applied_position_complex = TRUE;
184 static inline hb_bool_t
185 is_variation_selector (hb_codepoint_t unicode)
187 return unlikely ((unicode >= 0x180B && unicode <= 0x180D) || /* MONGOLIAN FREE VARIATION SELECTOR ONE..THREE */
188 (unicode >= 0xFE00 && unicode <= 0xFE0F) || /* VARIATION SELECTOR-1..16 */
189 (unicode >= 0xE0100 && unicode <= 0xE01EF)); /* VARIATION SELECTOR-17..256 */
193 hb_set_unicode_props (hb_ot_shape_context_t *c)
195 hb_unicode_funcs_t *unicode = c->buffer->unicode;
196 hb_glyph_info_t *info = c->buffer->info;
198 unsigned int count = c->buffer->len;
199 for (unsigned int i = 1; i < count; i++) {
200 info[i].general_category() = unicode->get_general_category (info[i].codepoint);
201 info[i].combining_class() = unicode->get_combining_class (info[i].codepoint);
206 hb_form_clusters (hb_ot_shape_context_t *c)
208 unsigned int count = c->buffer->len;
209 for (unsigned int i = 1; i < count; i++)
210 /* TODO: Match other mark types too? */
211 if (c->buffer->info[i].general_category() == HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK)
212 c->buffer->info[i].cluster = c->buffer->info[i - 1].cluster;
216 hb_ensure_native_direction (hb_ot_shape_context_t *c)
218 hb_direction_t direction = c->buffer->props.direction;
221 * The only BTT vertical script is Ogham, but it's not clear to me whether OpenType
222 * Ogham fonts are supposed to be implemented BTT or not. Need to research that
224 if ((HB_DIRECTION_IS_HORIZONTAL (direction) && direction != hb_script_get_horizontal_direction (c->buffer->props.script)) ||
225 (HB_DIRECTION_IS_VERTICAL (direction) && direction != HB_DIRECTION_TTB))
227 hb_buffer_reverse_clusters (c->buffer);
228 c->buffer->props.direction = HB_DIRECTION_REVERSE (c->buffer->props.direction);
233 hb_reset_glyph_infos (hb_ot_shape_context_t *c)
235 unsigned int count = c->buffer->len;
236 for (unsigned int i = 0; i < count; i++)
237 c->buffer->info[i].var1.u32 = c->buffer->info[i].var2.u32 = 0;
244 hb_mirror_chars (hb_ot_shape_context_t *c)
246 hb_unicode_funcs_t *unicode = c->buffer->unicode;
248 if (HB_DIRECTION_IS_FORWARD (c->target_direction))
251 hb_mask_t rtlm_mask = c->plan->map.get_1_mask (HB_TAG ('r','t','l','m'));
253 unsigned int count = c->buffer->len;
254 for (unsigned int i = 0; i < count; i++) {
255 hb_codepoint_t codepoint = unicode->get_mirroring (c->buffer->info[i].codepoint);
256 if (likely (codepoint == c->buffer->info[i].codepoint))
257 c->buffer->info[i].mask |= rtlm_mask; /* XXX this should be moved to before setting user-feature masks */
259 c->buffer->info[i].codepoint = codepoint;
264 hb_map_glyphs (hb_font_t *font,
267 if (unlikely (!buffer->len))
270 hb_codepoint_t glyph;
271 buffer->clear_output ();
272 unsigned int count = buffer->len - 1;
273 for (buffer->i = 0; buffer->i < count;) {
274 if (unlikely (is_variation_selector (buffer->info[buffer->i + 1].codepoint))) {
275 hb_font_get_glyph (font, buffer->info[buffer->i].codepoint, buffer->info[buffer->i + 1].codepoint, &glyph);
276 buffer->replace_glyph (glyph);
279 hb_font_get_glyph (font, buffer->info[buffer->i].codepoint, 0, &glyph);
280 buffer->replace_glyph (glyph);
283 if (likely (buffer->i < buffer->len)) {
284 hb_font_get_glyph (font, buffer->info[buffer->i].codepoint, 0, &glyph);
285 buffer->replace_glyph (glyph);
291 hb_substitute_default (hb_ot_shape_context_t *c)
293 hb_map_glyphs (c->font, c->buffer);
297 hb_substitute_complex_fallback (hb_ot_shape_context_t *c HB_UNUSED)
306 hb_position_default (hb_ot_shape_context_t *c)
308 hb_ot_layout_position_start (c->buffer);
310 unsigned int count = c->buffer->len;
311 for (unsigned int i = 0; i < count; i++) {
312 hb_font_get_glyph_advance_for_direction (c->font, c->buffer->info[i].codepoint,
313 c->buffer->props.direction,
314 &c->buffer->pos[i].x_advance,
315 &c->buffer->pos[i].y_advance);
316 hb_font_subtract_glyph_origin_for_direction (c->font, c->buffer->info[i].codepoint,
317 c->buffer->props.direction,
318 &c->buffer->pos[i].x_offset,
319 &c->buffer->pos[i].y_offset);
324 hb_position_complex_fallback (hb_ot_shape_context_t *c HB_UNUSED)
330 hb_truetype_kern (hb_ot_shape_context_t *c)
332 /* TODO Check for kern=0 */
333 unsigned int count = c->buffer->len;
334 for (unsigned int i = 1; i < count; i++) {
335 hb_position_t x_kern, y_kern, kern1, kern2;
336 hb_font_get_glyph_kerning_for_direction (c->font,
337 c->buffer->info[i - 1].codepoint, c->buffer->info[i].codepoint,
338 c->buffer->props.direction,
342 kern2 = x_kern - kern1;
343 c->buffer->pos[i - 1].x_advance += kern1;
344 c->buffer->pos[i].x_advance += kern2;
345 c->buffer->pos[i].x_offset += kern2;
348 kern2 = y_kern - kern1;
349 c->buffer->pos[i - 1].y_advance += kern1;
350 c->buffer->pos[i].y_advance += kern2;
351 c->buffer->pos[i].y_offset += kern2;
356 hb_position_complex_fallback_visual (hb_ot_shape_context_t *c)
358 hb_truetype_kern (c);
365 hb_ot_shape_execute_internal (hb_ot_shape_context_t *c)
367 /* Save the original direction, we use it later. */
368 c->target_direction = c->buffer->props.direction;
370 hb_reset_glyph_infos (c); /* BUFFER: Clear buffer var1 and var2 */
372 hb_set_unicode_props (c); /* BUFFER: Set general_category and combining_class in var1 */
374 hb_ensure_native_direction (c);
376 hb_form_clusters (c);
378 hb_ot_shape_setup_masks (c); /* BUFFER: Clobbers var2 */
382 /* Mirroring needs to see the original direction */
385 hb_substitute_default (c);
387 hb_ot_substitute_complex (c);
389 if (!c->applied_substitute_complex)
390 hb_substitute_complex_fallback (c);
395 hb_position_default (c);
397 hb_ot_position_complex (c);
399 hb_bool_t position_fallback = !c->applied_position_complex;
400 if (position_fallback)
401 hb_position_complex_fallback (c);
403 if (HB_DIRECTION_IS_BACKWARD (c->buffer->props.direction))
404 hb_buffer_reverse (c->buffer);
406 if (position_fallback)
407 hb_position_complex_fallback_visual (c);
410 c->buffer->props.direction = c->target_direction;
414 hb_ot_shape_plan_internal (hb_ot_shape_plan_t *plan,
416 const hb_segment_properties_t *props,
417 const hb_feature_t *user_features,
418 unsigned int num_user_features)
420 hb_ot_shape_planner_t planner;
422 planner.shaper = hb_ot_shape_complex_categorize (props);
424 hb_ot_shape_collect_features (&planner, props, user_features, num_user_features);
426 planner.compile (face, props, *plan);
430 hb_ot_shape_execute (hb_ot_shape_plan_t *plan,
433 const hb_feature_t *user_features,
434 unsigned int num_user_features)
436 hb_ot_shape_context_t c = {plan, font, font->face, buffer, user_features, num_user_features};
437 hb_ot_shape_execute_internal (&c);
441 hb_ot_shape (hb_font_t *font,
443 const hb_feature_t *features,
444 unsigned int num_features)
446 hb_ot_shape_plan_t plan;
448 hb_ot_shape_plan_internal (&plan, font->face, &buffer->props, features, num_features);
449 hb_ot_shape_execute (&plan, font, buffer, features, num_features);