2 * Copyright © 2009,2010 Red Hat, Inc.
3 * Copyright © 2010 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"
38 hb_tag_t default_features[] = {
39 HB_TAG('c','a','l','t'),
40 HB_TAG('c','c','m','p'),
41 HB_TAG('c','l','i','g'),
42 HB_TAG('c','u','r','s'),
43 HB_TAG('k','e','r','n'),
44 HB_TAG('l','i','g','a'),
45 HB_TAG('l','o','c','l'),
46 HB_TAG('m','a','r','k'),
47 HB_TAG('m','k','m','k'),
48 HB_TAG('r','l','i','g')
52 hb_ot_shape_collect_features (hb_ot_shape_plan_t *plan,
53 const hb_segment_properties_t *props,
54 const hb_feature_t *user_features,
55 unsigned int num_user_features)
57 switch (props->direction) {
58 case HB_DIRECTION_LTR:
59 plan->map.add_bool_feature (HB_TAG ('l','t','r','a'));
60 plan->map.add_bool_feature (HB_TAG ('l','t','r','m'));
62 case HB_DIRECTION_RTL:
63 plan->map.add_bool_feature (HB_TAG ('r','t','l','a'));
64 plan->map.add_bool_feature (HB_TAG ('r','t','l','m'), false);
66 case HB_DIRECTION_TTB:
67 case HB_DIRECTION_BTT:
68 case HB_DIRECTION_INVALID:
73 for (unsigned int i = 0; i < ARRAY_LENGTH (default_features); i++)
74 plan->map.add_bool_feature (default_features[i]);
76 hb_ot_shape_complex_collect_features (plan, props);
78 for (unsigned int i = 0; i < num_user_features; i++) {
79 const hb_feature_t *feature = &user_features[i];
80 plan->map.add_feature (feature->tag, feature->value, (feature->start == 0 && feature->end == (unsigned int) -1));
86 hb_ot_shape_setup_masks (hb_ot_shape_context_t *c)
88 hb_mask_t global_mask = c->plan->map.get_global_mask ();
89 c->buffer->reset_masks (global_mask);
91 hb_ot_shape_complex_setup_masks (c); /* BUFFER: Clobbers var2 */
93 for (unsigned int i = 0; i < c->num_user_features; i++)
95 const hb_feature_t *feature = &c->user_features[i];
96 if (!(feature->start == 0 && feature->end == (unsigned int)-1)) {
98 hb_mask_t mask = c->plan->map.get_mask (feature->tag, &shift);
99 c->buffer->set_masks (feature->value << shift, mask, feature->start, feature->end);
106 hb_ot_substitute_complex (hb_ot_shape_context_t *c)
108 if (!hb_ot_layout_has_substitution (c->face))
111 c->plan->map.substitute (c->face, c->buffer);
113 c->applied_substitute_complex = TRUE;
118 hb_ot_position_complex (hb_ot_shape_context_t *c)
121 if (!hb_ot_layout_has_positioning (c->face))
124 unsigned int count = c->buffer->len;
125 for (unsigned int i = 0; i < count; i++) {
126 hb_font_add_glyph_origin_for_direction (c->font, c->buffer->info[i].codepoint,
128 &c->buffer->pos[i].x_offset,
129 &c->buffer->pos[i].y_offset);
132 c->plan->map.position (c->font, c->face, c->buffer);
134 for (unsigned int i = 0; i < count; i++) {
135 hb_font_subtract_glyph_origin_for_direction (c->font, c->buffer->info[i].codepoint,
137 &c->buffer->pos[i].x_offset,
138 &c->buffer->pos[i].y_offset);
141 hb_ot_layout_position_finish (c->buffer);
143 c->applied_position_complex = TRUE;
152 static inline hb_bool_t
153 is_variation_selector (hb_codepoint_t unicode)
155 return unlikely ((unicode >= 0x180B && unicode <= 0x180D) || /* MONGOLIAN FREE VARIATION SELECTOR ONE..THREE */
156 (unicode >= 0xFE00 && unicode <= 0xFE0F) || /* VARIATION SELECTOR-1..16 */
157 (unicode >= 0xE0100 && unicode <= 0xE01EF)); /* VARIATION SELECTOR-17..256 */
161 hb_set_unicode_props (hb_ot_shape_context_t *c)
163 hb_unicode_funcs_t *unicode = c->buffer->unicode;
164 hb_glyph_info_t *info = c->buffer->info;
166 unsigned int count = c->buffer->len;
167 for (unsigned int i = 1; i < count; i++) {
168 info[i].general_category() = unicode->get_general_category (info[i].codepoint);
169 info[i].combining_class() = unicode->get_combining_class (info[i].codepoint);
174 hb_form_clusters (hb_ot_shape_context_t *c)
176 unsigned int count = c->buffer->len;
177 for (unsigned int i = 1; i < count; i++)
178 if (c->buffer->info[i].general_category() == HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK)
179 c->buffer->info[i].cluster = c->buffer->info[i - 1].cluster;
183 hb_ensure_native_direction (hb_ot_shape_context_t *c)
185 hb_direction_t direction = c->buffer->props.direction;
188 * The only BTT vertical script is Ogham, but it's not clear to me whether OpenType
189 * Ogham fonts are supposed to be implemented BTT or not. Need to research that
191 if ((HB_DIRECTION_IS_HORIZONTAL (direction) && direction != hb_script_get_horizontal_direction (c->buffer->props.script)) ||
192 (HB_DIRECTION_IS_VERTICAL (direction) && direction != HB_DIRECTION_TTB))
194 hb_buffer_reverse_clusters (c->buffer);
195 c->buffer->props.direction = HB_DIRECTION_REVERSE (c->buffer->props.direction);
200 hb_reset_glyph_infos (hb_ot_shape_context_t *c)
202 unsigned int count = c->buffer->len;
203 for (unsigned int i = 0; i < count; i++)
204 c->buffer->info[i].var1.u32 = c->buffer->info[i].var2.u32 = 0;
211 hb_mirror_chars (hb_ot_shape_context_t *c)
213 hb_unicode_funcs_t *unicode = c->buffer->unicode;
215 if (HB_DIRECTION_IS_FORWARD (c->target_direction))
218 hb_mask_t rtlm_mask = c->plan->map.get_1_mask (HB_TAG ('r','t','l','m'));
220 unsigned int count = c->buffer->len;
221 for (unsigned int i = 0; i < count; i++) {
222 hb_codepoint_t codepoint = unicode->get_mirroring (c->buffer->info[i].codepoint);
223 if (likely (codepoint == c->buffer->info[i].codepoint))
224 c->buffer->info[i].mask |= rtlm_mask; /* XXX this should be moved to before setting user-feature masks */
226 c->buffer->info[i].codepoint = codepoint;
231 hb_map_glyphs (hb_font_t *font,
234 if (unlikely (!buffer->len))
237 hb_codepoint_t glyph;
238 buffer->clear_output ();
239 unsigned int count = buffer->len - 1;
240 for (buffer->i = 0; buffer->i < count;) {
241 if (unlikely (is_variation_selector (buffer->info[buffer->i + 1].codepoint))) {
242 hb_font_get_glyph (font, buffer->info[buffer->i].codepoint, buffer->info[buffer->i + 1].codepoint, &glyph);
243 buffer->replace_glyph (glyph);
246 hb_font_get_glyph (font, buffer->info[buffer->i].codepoint, 0, &glyph);
247 buffer->replace_glyph (glyph);
250 if (likely (buffer->i < buffer->len)) {
251 hb_font_get_glyph (font, buffer->info[buffer->i].codepoint, 0, &glyph);
252 buffer->replace_glyph (glyph);
258 hb_substitute_default (hb_ot_shape_context_t *c)
260 hb_map_glyphs (c->font, c->buffer);
264 hb_substitute_complex_fallback (hb_ot_shape_context_t *c HB_UNUSED)
273 hb_position_default (hb_ot_shape_context_t *c)
275 hb_ot_layout_position_start (c->buffer);
277 unsigned int count = c->buffer->len;
278 for (unsigned int i = 0; i < count; i++) {
279 hb_font_get_glyph_advance_for_direction (c->font, c->buffer->info[i].codepoint,
280 c->buffer->props.direction,
281 &c->buffer->pos[i].x_advance,
282 &c->buffer->pos[i].y_advance);
283 hb_font_subtract_glyph_origin_for_direction (c->font, c->buffer->info[i].codepoint,
284 c->buffer->props.direction,
285 &c->buffer->pos[i].x_offset,
286 &c->buffer->pos[i].y_offset);
291 hb_position_complex_fallback (hb_ot_shape_context_t *c HB_UNUSED)
297 hb_truetype_kern (hb_ot_shape_context_t *c)
299 /* TODO Check for kern=0 */
300 unsigned int count = c->buffer->len;
301 for (unsigned int i = 1; i < count; i++) {
302 hb_position_t x_kern, y_kern, kern1, kern2;
303 hb_font_get_glyph_kerning_for_direction (c->font,
304 c->buffer->info[i - 1].codepoint, c->buffer->info[i].codepoint,
305 c->buffer->props.direction,
309 kern2 = x_kern - kern1;
310 c->buffer->pos[i - 1].x_advance += kern1;
311 c->buffer->pos[i].x_advance += kern2;
312 c->buffer->pos[i].x_offset += kern2;
315 kern2 = y_kern - kern1;
316 c->buffer->pos[i - 1].y_advance += kern1;
317 c->buffer->pos[i].y_advance += kern2;
318 c->buffer->pos[i].y_offset += kern2;
323 hb_position_complex_fallback_visual (hb_ot_shape_context_t *c)
325 hb_truetype_kern (c);
332 hb_ot_shape_execute_internal (hb_ot_shape_context_t *c)
334 /* Save the original direction, we use it later. */
335 c->target_direction = c->buffer->props.direction;
337 hb_reset_glyph_infos (c); /* BUFFER: Clear buffer var1 and var2 */
339 hb_set_unicode_props (c); /* BUFFER: Set general_category and combining_class in var1 */
341 hb_ensure_native_direction (c);
343 hb_form_clusters (c);
345 hb_ot_shape_setup_masks (c); /* BUFFER: Clobbers var2 */
349 /* Mirroring needs to see the original direction */
352 hb_substitute_default (c);
354 hb_ot_substitute_complex (c);
356 if (!c->applied_substitute_complex)
357 hb_substitute_complex_fallback (c);
362 hb_position_default (c);
364 hb_ot_position_complex (c);
366 hb_bool_t position_fallback = !c->applied_position_complex;
367 if (position_fallback)
368 hb_position_complex_fallback (c);
370 if (HB_DIRECTION_IS_BACKWARD (c->buffer->props.direction))
371 hb_buffer_reverse (c->buffer);
373 if (position_fallback)
374 hb_position_complex_fallback_visual (c);
377 c->buffer->props.direction = c->target_direction;
381 hb_ot_shape_plan_internal (hb_ot_shape_plan_t *plan,
383 const hb_segment_properties_t *props,
384 const hb_feature_t *user_features,
385 unsigned int num_user_features)
387 plan->shaper = hb_ot_shape_complex_categorize (props);
389 hb_ot_shape_collect_features (plan, props, user_features, num_user_features);
391 plan->map.compile (face, props);
395 hb_ot_shape_execute (hb_ot_shape_plan_t *plan,
398 const hb_feature_t *user_features,
399 unsigned int num_user_features)
401 hb_ot_shape_context_t c = {plan, font, font->face, buffer, user_features, num_user_features};
402 hb_ot_shape_execute_internal (&c);
406 hb_ot_shape (hb_font_t *font,
408 const hb_feature_t *features,
409 unsigned int num_features)
411 hb_ot_shape_plan_t plan;
413 hb_ot_shape_plan_internal (&plan, font->face, &buffer->props, features, num_features);
414 hb_ot_shape_execute (&plan, font, buffer, features, num_features);