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 common_features[] = {
38 HB_TAG('c','c','m','p'),
39 HB_TAG('l','o','c','l'),
40 HB_TAG('m','a','r','k'),
41 HB_TAG('m','k','m','k'),
42 HB_TAG('r','l','i','g'),
45 hb_tag_t horizontal_features[] = {
46 HB_TAG('c','a','l','t'),
47 HB_TAG('c','l','i','g'),
48 HB_TAG('c','u','r','s'),
49 HB_TAG('k','e','r','n'),
50 HB_TAG('l','i','g','a'),
54 * Technically speaking, vrt2 and vert are mutually exclusive.
55 * According to the spec, valt and vpal are also mutually exclusive.
56 * But we apply them all for now.
58 hb_tag_t vertical_features[] = {
59 HB_TAG('v','a','l','t'),
60 HB_TAG('v','e','r','t'),
61 HB_TAG('v','k','r','n'),
62 HB_TAG('v','p','a','l'),
63 HB_TAG('v','r','t','2'),
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 switch (props->direction) {
73 case HB_DIRECTION_LTR:
74 planner->map.add_bool_feature (HB_TAG ('l','t','r','a'));
75 planner->map.add_bool_feature (HB_TAG ('l','t','r','m'));
77 case HB_DIRECTION_RTL:
78 planner->map.add_bool_feature (HB_TAG ('r','t','l','a'));
79 planner->map.add_bool_feature (HB_TAG ('r','t','l','m'), false);
81 case HB_DIRECTION_TTB:
82 case HB_DIRECTION_BTT:
83 case HB_DIRECTION_INVALID:
88 #define ADD_FEATURES(array) \
90 for (unsigned int i = 0; i < ARRAY_LENGTH (array); i++) \
91 planner->map.add_bool_feature (array[i]); \
94 hb_ot_shape_complex_collect_features (planner->shaper, &planner->map, props);
96 ADD_FEATURES (common_features);
98 if (HB_DIRECTION_IS_HORIZONTAL (props->direction))
99 ADD_FEATURES (horizontal_features);
101 ADD_FEATURES (vertical_features);
105 for (unsigned int i = 0; i < num_user_features; i++) {
106 const hb_feature_t *feature = &user_features[i];
107 planner->map.add_feature (feature->tag, feature->value, (feature->start == 0 && feature->end == (unsigned int) -1));
113 hb_ot_shape_setup_masks (hb_ot_shape_context_t *c)
115 hb_mask_t global_mask = c->plan->map.get_global_mask ();
116 c->buffer->reset_masks (global_mask);
118 hb_ot_shape_complex_setup_masks (c->plan->shaper, &c->plan->map, c->buffer); /* BUFFER: Clobbers var2 */
120 for (unsigned int i = 0; i < c->num_user_features; i++)
122 const hb_feature_t *feature = &c->user_features[i];
123 if (!(feature->start == 0 && feature->end == (unsigned int)-1)) {
125 hb_mask_t mask = c->plan->map.get_mask (feature->tag, &shift);
126 c->buffer->set_masks (feature->value << shift, mask, feature->start, feature->end);
133 hb_ot_substitute_complex (hb_ot_shape_context_t *c)
135 if (!hb_ot_layout_has_substitution (c->face))
138 c->plan->map.substitute (c->face, c->buffer);
140 c->applied_substitute_complex = TRUE;
145 hb_ot_position_complex (hb_ot_shape_context_t *c)
148 if (!hb_ot_layout_has_positioning (c->face))
151 unsigned int count = c->buffer->len;
152 for (unsigned int i = 0; i < count; i++) {
153 hb_font_add_glyph_origin_for_direction (c->font, c->buffer->info[i].codepoint,
155 &c->buffer->pos[i].x_offset,
156 &c->buffer->pos[i].y_offset);
159 c->plan->map.position (c->font, c->buffer);
161 for (unsigned int i = 0; i < count; i++) {
162 hb_font_subtract_glyph_origin_for_direction (c->font, c->buffer->info[i].codepoint,
164 &c->buffer->pos[i].x_offset,
165 &c->buffer->pos[i].y_offset);
168 hb_ot_layout_position_finish (c->buffer);
170 c->applied_position_complex = TRUE;
179 static inline hb_bool_t
180 is_variation_selector (hb_codepoint_t unicode)
182 return unlikely ((unicode >= 0x180B && unicode <= 0x180D) || /* MONGOLIAN FREE VARIATION SELECTOR ONE..THREE */
183 (unicode >= 0xFE00 && unicode <= 0xFE0F) || /* VARIATION SELECTOR-1..16 */
184 (unicode >= 0xE0100 && unicode <= 0xE01EF)); /* VARIATION SELECTOR-17..256 */
188 hb_set_unicode_props (hb_ot_shape_context_t *c)
190 hb_unicode_funcs_t *unicode = c->buffer->unicode;
191 hb_glyph_info_t *info = c->buffer->info;
193 unsigned int count = c->buffer->len;
194 for (unsigned int i = 1; i < count; i++) {
195 info[i].general_category() = hb_unicode_general_category (unicode, info[i].codepoint);
196 info[i].combining_class() = hb_unicode_combining_class (unicode, info[i].codepoint);
201 hb_form_clusters (hb_ot_shape_context_t *c)
203 unsigned int count = c->buffer->len;
204 for (unsigned int i = 1; i < count; i++)
205 if (FLAG (c->buffer->info[i].general_category()) &
206 (FLAG (HB_UNICODE_GENERAL_CATEGORY_SPACING_MARK) |
207 FLAG (HB_UNICODE_GENERAL_CATEGORY_ENCLOSING_MARK) |
208 FLAG (HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK)))
209 c->buffer->info[i].cluster = c->buffer->info[i - 1].cluster;
213 hb_ensure_native_direction (hb_ot_shape_context_t *c)
215 hb_direction_t direction = c->buffer->props.direction;
218 * The only BTT vertical script is Ogham, but it's not clear to me whether OpenType
219 * Ogham fonts are supposed to be implemented BTT or not. Need to research that
221 if ((HB_DIRECTION_IS_HORIZONTAL (direction) && direction != hb_script_get_horizontal_direction (c->buffer->props.script)) ||
222 (HB_DIRECTION_IS_VERTICAL (direction) && direction != HB_DIRECTION_TTB))
224 hb_buffer_reverse_clusters (c->buffer);
225 c->buffer->props.direction = HB_DIRECTION_REVERSE (c->buffer->props.direction);
230 hb_reset_glyph_infos (hb_ot_shape_context_t *c)
232 unsigned int count = c->buffer->len;
233 for (unsigned int i = 0; i < count; i++)
234 c->buffer->info[i].var1.u32 = c->buffer->info[i].var2.u32 = 0;
241 hb_mirror_chars (hb_ot_shape_context_t *c)
243 hb_unicode_funcs_t *unicode = c->buffer->unicode;
245 if (HB_DIRECTION_IS_FORWARD (c->target_direction))
248 hb_mask_t rtlm_mask = c->plan->map.get_1_mask (HB_TAG ('r','t','l','m'));
250 unsigned int count = c->buffer->len;
251 for (unsigned int i = 0; i < count; i++) {
252 hb_codepoint_t codepoint = hb_unicode_mirroring (unicode, c->buffer->info[i].codepoint);
253 if (likely (codepoint == c->buffer->info[i].codepoint))
254 c->buffer->info[i].mask |= rtlm_mask; /* XXX this should be moved to before setting user-feature masks */
256 c->buffer->info[i].codepoint = codepoint;
261 hb_map_glyphs (hb_font_t *font,
264 if (unlikely (!buffer->len))
267 hb_codepoint_t glyph;
268 buffer->clear_output ();
269 unsigned int count = buffer->len - 1;
270 for (buffer->i = 0; buffer->i < count;) {
271 if (unlikely (is_variation_selector (buffer->info[buffer->i + 1].codepoint))) {
272 hb_font_get_glyph (font, buffer->info[buffer->i].codepoint, buffer->info[buffer->i + 1].codepoint, &glyph);
273 buffer->replace_glyph (glyph);
276 hb_font_get_glyph (font, buffer->info[buffer->i].codepoint, 0, &glyph);
277 buffer->replace_glyph (glyph);
280 if (likely (buffer->i < buffer->len)) {
281 hb_font_get_glyph (font, buffer->info[buffer->i].codepoint, 0, &glyph);
282 buffer->replace_glyph (glyph);
288 hb_substitute_default (hb_ot_shape_context_t *c)
290 hb_map_glyphs (c->font, c->buffer);
294 hb_substitute_complex_fallback (hb_ot_shape_context_t *c HB_UNUSED)
303 hb_position_default (hb_ot_shape_context_t *c)
305 hb_ot_layout_position_start (c->buffer);
307 unsigned int count = c->buffer->len;
308 for (unsigned int i = 0; i < count; i++) {
309 hb_font_get_glyph_advance_for_direction (c->font, c->buffer->info[i].codepoint,
310 c->buffer->props.direction,
311 &c->buffer->pos[i].x_advance,
312 &c->buffer->pos[i].y_advance);
313 hb_font_subtract_glyph_origin_for_direction (c->font, c->buffer->info[i].codepoint,
314 c->buffer->props.direction,
315 &c->buffer->pos[i].x_offset,
316 &c->buffer->pos[i].y_offset);
321 hb_position_complex_fallback (hb_ot_shape_context_t *c HB_UNUSED)
327 hb_truetype_kern (hb_ot_shape_context_t *c)
329 /* TODO Check for kern=0 */
330 unsigned int count = c->buffer->len;
331 for (unsigned int i = 1; i < count; i++) {
332 hb_position_t x_kern, y_kern, kern1, kern2;
333 hb_font_get_glyph_kerning_for_direction (c->font,
334 c->buffer->info[i - 1].codepoint, c->buffer->info[i].codepoint,
335 c->buffer->props.direction,
339 kern2 = x_kern - kern1;
340 c->buffer->pos[i - 1].x_advance += kern1;
341 c->buffer->pos[i].x_advance += kern2;
342 c->buffer->pos[i].x_offset += kern2;
345 kern2 = y_kern - kern1;
346 c->buffer->pos[i - 1].y_advance += kern1;
347 c->buffer->pos[i].y_advance += kern2;
348 c->buffer->pos[i].y_offset += kern2;
353 hb_position_complex_fallback_visual (hb_ot_shape_context_t *c)
355 hb_truetype_kern (c);
362 hb_ot_shape_execute_internal (hb_ot_shape_context_t *c)
364 /* Save the original direction, we use it later. */
365 c->target_direction = c->buffer->props.direction;
367 hb_reset_glyph_infos (c); /* BUFFER: Clear buffer var1 and var2 */
369 hb_set_unicode_props (c); /* BUFFER: Set general_category and combining_class in var1 */
371 hb_ensure_native_direction (c);
373 hb_form_clusters (c);
375 hb_ot_shape_setup_masks (c); /* BUFFER: Clobbers var2 */
379 /* Mirroring needs to see the original direction */
382 hb_substitute_default (c);
384 hb_ot_substitute_complex (c);
386 if (!c->applied_substitute_complex)
387 hb_substitute_complex_fallback (c);
392 hb_position_default (c);
394 hb_ot_position_complex (c);
396 hb_bool_t position_fallback = !c->applied_position_complex;
397 if (position_fallback)
398 hb_position_complex_fallback (c);
400 if (HB_DIRECTION_IS_BACKWARD (c->buffer->props.direction))
401 hb_buffer_reverse (c->buffer);
403 if (position_fallback)
404 hb_position_complex_fallback_visual (c);
407 c->buffer->props.direction = c->target_direction;
411 hb_ot_shape_plan_internal (hb_ot_shape_plan_t *plan,
413 const hb_segment_properties_t *props,
414 const hb_feature_t *user_features,
415 unsigned int num_user_features)
417 hb_ot_shape_planner_t planner;
419 planner.shaper = hb_ot_shape_complex_categorize (props);
421 hb_ot_shape_collect_features (&planner, props, user_features, num_user_features);
423 planner.compile (face, props, *plan);
427 hb_ot_shape_execute (hb_ot_shape_plan_t *plan,
430 const hb_feature_t *user_features,
431 unsigned int num_user_features)
433 hb_ot_shape_context_t c = {plan, font, font->face, buffer, user_features, num_user_features};
434 hb_ot_shape_execute_internal (&c);
438 hb_ot_shape (hb_font_t *font,
440 const hb_feature_t *features,
441 unsigned int num_features)
443 hb_ot_shape_plan_t plan;
445 hb_ot_shape_plan_internal (&plan, font->face, &buffer->props, features, num_features);
446 hb_ot_shape_execute (&plan, font, buffer, features, num_features);