hb-ot-shape.cc revision a4385f0b0a6949e2ce49e6a147ad4beaa724f6c3
1/* 2 * Copyright © 2009,2010 Red Hat, Inc. 3 * Copyright © 2010,2011 Google, Inc. 4 * 5 * This is part of HarfBuzz, a text shaping library. 6 * 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. 12 * 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 17 * DAMAGE. 18 * 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. 24 * 25 * Red Hat Author(s): Behdad Esfahbod 26 * Google Author(s): Behdad Esfahbod 27 */ 28 29#include "hb-ot-shape-private.hh" 30#include "hb-ot-shape-normalize-private.hh" 31 32#include "hb-font-private.hh" 33 34 35 36hb_tag_t common_features[] = { 37 HB_TAG('c','c','m','p'), 38 HB_TAG('l','i','g','a'), 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'), 43}; 44 45hb_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}; 51 52/* Note: 53 * Technically speaking, vrt2 and vert are mutually exclusive. 54 * According to the spec, valt and vpal are also mutually exclusive. 55 * But we apply them all for now. 56 */ 57hb_tag_t vertical_features[] = { 58 HB_TAG('v','a','l','t'), 59 HB_TAG('v','e','r','t'), 60 HB_TAG('v','k','r','n'), 61 HB_TAG('v','p','a','l'), 62 HB_TAG('v','r','t','2'), 63}; 64 65 66 67struct hb_ot_shape_planner_t 68{ 69 hb_ot_map_builder_t map; 70 hb_ot_complex_shaper_t shaper; 71 72 hb_ot_shape_planner_t (void) : map () {} 73 ~hb_ot_shape_planner_t (void) { map.finish (); } 74 75 inline void compile (hb_face_t *face, 76 const hb_segment_properties_t *props, 77 struct hb_ot_shape_plan_t &plan) 78 { 79 plan.shaper = shaper; 80 map.compile (face, props, plan.map); 81 } 82 83 private: 84 NO_COPY (hb_ot_shape_planner_t); 85}; 86 87static void 88hb_ot_shape_collect_features (hb_ot_shape_planner_t *planner, 89 const hb_segment_properties_t *props, 90 const hb_feature_t *user_features, 91 unsigned int num_user_features) 92{ 93 switch (props->direction) { 94 case HB_DIRECTION_LTR: 95 planner->map.add_bool_feature (HB_TAG ('l','t','r','a')); 96 planner->map.add_bool_feature (HB_TAG ('l','t','r','m')); 97 break; 98 case HB_DIRECTION_RTL: 99 planner->map.add_bool_feature (HB_TAG ('r','t','l','a')); 100 planner->map.add_bool_feature (HB_TAG ('r','t','l','m'), false); 101 break; 102 case HB_DIRECTION_TTB: 103 case HB_DIRECTION_BTT: 104 case HB_DIRECTION_INVALID: 105 default: 106 break; 107 } 108 109#define ADD_FEATURES(array) \ 110 HB_STMT_START { \ 111 for (unsigned int i = 0; i < ARRAY_LENGTH (array); i++) \ 112 planner->map.add_bool_feature (array[i]); \ 113 } HB_STMT_END 114 115 hb_ot_shape_complex_collect_features (planner->shaper, &planner->map, props); 116 117 ADD_FEATURES (common_features); 118 119 if (HB_DIRECTION_IS_HORIZONTAL (props->direction)) 120 ADD_FEATURES (horizontal_features); 121 else 122 ADD_FEATURES (vertical_features); 123 124#undef ADD_FEATURES 125 126 for (unsigned int i = 0; i < num_user_features; i++) { 127 const hb_feature_t *feature = &user_features[i]; 128 planner->map.add_feature (feature->tag, feature->value, (feature->start == 0 && feature->end == (unsigned int) -1)); 129 } 130} 131 132 133struct hb_ot_shape_context_t 134{ 135 /* Input to hb_ot_shape_execute() */ 136 hb_ot_shape_plan_t *plan; 137 hb_font_t *font; 138 hb_face_t *face; 139 hb_buffer_t *buffer; 140 const hb_feature_t *user_features; 141 unsigned int num_user_features; 142 143 /* Transient stuff */ 144 hb_direction_t target_direction; 145 hb_bool_t applied_position_complex; 146}; 147 148static void 149hb_ot_shape_setup_masks (hb_ot_shape_context_t *c) 150{ 151 hb_mask_t global_mask = c->plan->map.get_global_mask (); 152 c->buffer->reset_masks (global_mask); 153 154 hb_ot_shape_complex_setup_masks (c->plan->shaper, &c->plan->map, c->buffer, c->font); 155 156 for (unsigned int i = 0; i < c->num_user_features; i++) 157 { 158 const hb_feature_t *feature = &c->user_features[i]; 159 if (!(feature->start == 0 && feature->end == (unsigned int)-1)) { 160 unsigned int shift; 161 hb_mask_t mask = c->plan->map.get_mask (feature->tag, &shift); 162 c->buffer->set_masks (feature->value << shift, mask, feature->start, feature->end); 163 } 164 } 165} 166 167 168/* Main shaper */ 169 170/* Prepare */ 171 172static inline void 173set_unicode_props (hb_glyph_info_t *info, hb_unicode_funcs_t *unicode) 174{ 175 info->general_category() = hb_unicode_general_category (unicode, info->codepoint); 176 info->combining_class() = _hb_unicode_modified_combining_class (unicode, info->codepoint); 177} 178 179static void 180hb_set_unicode_props (hb_buffer_t *buffer) 181{ 182 unsigned int count = buffer->len; 183 for (unsigned int i = 0; i < count; i++) 184 set_unicode_props (&buffer->info[i], buffer->unicode); 185} 186 187static void 188hb_form_clusters (hb_buffer_t *buffer) 189{ 190 unsigned int count = buffer->len; 191 for (unsigned int i = 1; i < count; i++) 192 if (FLAG (buffer->info[i].general_category()) & 193 (FLAG (HB_UNICODE_GENERAL_CATEGORY_SPACING_MARK) | 194 FLAG (HB_UNICODE_GENERAL_CATEGORY_ENCLOSING_MARK) | 195 FLAG (HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK))) 196 buffer->info[i].cluster = buffer->info[i - 1].cluster; /* XXX do the min() here */ 197} 198 199static void 200hb_ensure_native_direction (hb_buffer_t *buffer) 201{ 202 hb_direction_t direction = buffer->props.direction; 203 204 /* TODO vertical: 205 * The only BTT vertical script is Ogham, but it's not clear to me whether OpenType 206 * Ogham fonts are supposed to be implemented BTT or not. Need to research that 207 * first. */ 208 if ((HB_DIRECTION_IS_HORIZONTAL (direction) && direction != hb_script_get_horizontal_direction (buffer->props.script)) || 209 (HB_DIRECTION_IS_VERTICAL (direction) && direction != HB_DIRECTION_TTB)) 210 { 211 hb_buffer_reverse_clusters (buffer); 212 buffer->props.direction = HB_DIRECTION_REVERSE (buffer->props.direction); 213 } 214} 215 216 217/* Substitute */ 218 219static void 220hb_mirror_chars (hb_ot_shape_context_t *c) 221{ 222 hb_unicode_funcs_t *unicode = c->buffer->unicode; 223 224 if (HB_DIRECTION_IS_FORWARD (c->target_direction)) 225 return; 226 227 hb_mask_t rtlm_mask = c->plan->map.get_1_mask (HB_TAG ('r','t','l','m')); 228 229 unsigned int count = c->buffer->len; 230 for (unsigned int i = 0; i < count; i++) { 231 hb_codepoint_t codepoint = hb_unicode_mirroring (unicode, c->buffer->info[i].codepoint); 232 if (likely (codepoint == c->buffer->info[i].codepoint)) 233 c->buffer->info[i].mask |= rtlm_mask; /* XXX this should be moved to before setting user-feature masks */ 234 else 235 c->buffer->info[i].codepoint = codepoint; 236 } 237} 238 239static void 240hb_map_glyphs (hb_font_t *font, 241 hb_buffer_t *buffer) 242{ 243 hb_codepoint_t glyph; 244 245 if (unlikely (!buffer->len)) 246 return; 247 248 buffer->clear_output (); 249 250 unsigned int count = buffer->len - 1; 251 for (buffer->idx = 0; buffer->idx < count;) { 252 if (unlikely (_hb_unicode_is_variation_selector (buffer->info[buffer->idx + 1].codepoint))) { 253 hb_font_get_glyph (font, buffer->info[buffer->idx].codepoint, buffer->info[buffer->idx + 1].codepoint, &glyph); 254 buffer->replace_glyphs (2, 1, &glyph); 255 } else { 256 hb_font_get_glyph (font, buffer->info[buffer->idx].codepoint, 0, &glyph); 257 buffer->replace_glyph (glyph); 258 } 259 } 260 if (likely (buffer->idx < buffer->len)) { 261 hb_font_get_glyph (font, buffer->info[buffer->idx].codepoint, 0, &glyph); 262 buffer->replace_glyph (glyph); 263 } 264 buffer->swap_buffers (); 265} 266 267static void 268hb_substitute_default (hb_ot_shape_context_t *c) 269{ 270 hb_ot_layout_substitute_start (c->buffer); 271 272 hb_mirror_chars (c); 273 274 hb_map_glyphs (c->font, c->buffer); 275} 276 277static void 278hb_ot_substitute_complex (hb_ot_shape_context_t *c) 279{ 280 if (hb_ot_layout_has_substitution (c->face)) { 281 c->plan->map.substitute (c->face, c->buffer); 282 } 283 284 hb_ot_layout_substitute_finish (c->buffer); 285 286 return; 287} 288 289 290/* Position */ 291 292static void 293hb_position_default (hb_ot_shape_context_t *c) 294{ 295 hb_ot_layout_position_start (c->buffer); 296 297 unsigned int count = c->buffer->len; 298 for (unsigned int i = 0; i < count; i++) { 299 hb_font_get_glyph_advance_for_direction (c->font, c->buffer->info[i].codepoint, 300 c->buffer->props.direction, 301 &c->buffer->pos[i].x_advance, 302 &c->buffer->pos[i].y_advance); 303 hb_font_subtract_glyph_origin_for_direction (c->font, c->buffer->info[i].codepoint, 304 c->buffer->props.direction, 305 &c->buffer->pos[i].x_offset, 306 &c->buffer->pos[i].y_offset); 307 } 308} 309 310static void 311hb_ot_position_complex (hb_ot_shape_context_t *c) 312{ 313 314 if (hb_ot_layout_has_positioning (c->face)) 315 { 316 /* Change glyph origin to what GPOS expects, apply GPOS, change it back. */ 317 318 unsigned int count = c->buffer->len; 319 for (unsigned int i = 0; i < count; i++) { 320 hb_font_add_glyph_origin_for_direction (c->font, c->buffer->info[i].codepoint, 321 HB_DIRECTION_LTR, 322 &c->buffer->pos[i].x_offset, 323 &c->buffer->pos[i].y_offset); 324 } 325 326 c->plan->map.position (c->font, c->buffer); 327 328 for (unsigned int i = 0; i < count; i++) { 329 hb_font_subtract_glyph_origin_for_direction (c->font, c->buffer->info[i].codepoint, 330 HB_DIRECTION_LTR, 331 &c->buffer->pos[i].x_offset, 332 &c->buffer->pos[i].y_offset); 333 } 334 335 c->applied_position_complex = TRUE; 336 } 337 338 hb_ot_layout_position_finish (c->buffer); 339 340 return; 341} 342 343static void 344hb_position_complex_fallback (hb_ot_shape_context_t *c HB_UNUSED) 345{ 346 /* TODO Mark pos */ 347} 348 349static void 350hb_truetype_kern (hb_ot_shape_context_t *c) 351{ 352 /* TODO Check for kern=0 */ 353 unsigned int count = c->buffer->len; 354 for (unsigned int i = 1; i < count; i++) { 355 hb_position_t x_kern, y_kern, kern1, kern2; 356 hb_font_get_glyph_kerning_for_direction (c->font, 357 c->buffer->info[i - 1].codepoint, c->buffer->info[i].codepoint, 358 c->buffer->props.direction, 359 &x_kern, &y_kern); 360 361 kern1 = x_kern >> 1; 362 kern2 = x_kern - kern1; 363 c->buffer->pos[i - 1].x_advance += kern1; 364 c->buffer->pos[i].x_advance += kern2; 365 c->buffer->pos[i].x_offset += kern2; 366 367 kern1 = y_kern >> 1; 368 kern2 = y_kern - kern1; 369 c->buffer->pos[i - 1].y_advance += kern1; 370 c->buffer->pos[i].y_advance += kern2; 371 c->buffer->pos[i].y_offset += kern2; 372 } 373} 374 375static void 376hb_position_complex_fallback_visual (hb_ot_shape_context_t *c) 377{ 378 hb_truetype_kern (c); 379} 380 381 382/* Do it! */ 383 384static void 385hb_ot_shape_execute_internal (hb_ot_shape_context_t *c) 386{ 387 c->buffer->deallocate_var_all (); 388 389 /* Save the original direction, we use it later. */ 390 c->target_direction = c->buffer->props.direction; 391 392 HB_BUFFER_ALLOCATE_VAR (c->buffer, general_category); 393 HB_BUFFER_ALLOCATE_VAR (c->buffer, combining_class); 394 395 hb_set_unicode_props (c->buffer); /* BUFFER: Set general_category and combining_class */ 396 397 hb_form_clusters (c->buffer); 398 399 hb_ensure_native_direction (c->buffer); 400 401 _hb_ot_shape_normalize (c->font, c->buffer, hb_ot_shape_complex_normalization_preference (c->plan->shaper)); 402 403 hb_ot_shape_setup_masks (c); 404 405 /* SUBSTITUTE */ 406 { 407 hb_substitute_default (c); 408 409 hb_ot_substitute_complex (c); 410 } 411 412 /* POSITION */ 413 { 414 hb_position_default (c); 415 416 hb_ot_position_complex (c); 417 418 hb_bool_t position_fallback = !c->applied_position_complex; 419 if (position_fallback) 420 hb_position_complex_fallback (c); 421 422 if (HB_DIRECTION_IS_BACKWARD (c->buffer->props.direction)) 423 hb_buffer_reverse (c->buffer); 424 425 if (position_fallback) 426 hb_position_complex_fallback_visual (c); 427 } 428 429 HB_BUFFER_DEALLOCATE_VAR (c->buffer, combining_class); 430 HB_BUFFER_DEALLOCATE_VAR (c->buffer, general_category); 431 432 c->buffer->props.direction = c->target_direction; 433 434 c->buffer->deallocate_var_all (); 435} 436 437static void 438hb_ot_shape_plan_internal (hb_ot_shape_plan_t *plan, 439 hb_face_t *face, 440 const hb_segment_properties_t *props, 441 const hb_feature_t *user_features, 442 unsigned int num_user_features) 443{ 444 hb_ot_shape_planner_t planner; 445 446 assert (HB_DIRECTION_IS_VALID (props->direction)); 447 448 planner.shaper = hb_ot_shape_complex_categorize (props); 449 450 hb_ot_shape_collect_features (&planner, props, user_features, num_user_features); 451 452 planner.compile (face, props, *plan); 453} 454 455static void 456hb_ot_shape_execute (hb_ot_shape_plan_t *plan, 457 hb_font_t *font, 458 hb_buffer_t *buffer, 459 const hb_feature_t *user_features, 460 unsigned int num_user_features) 461{ 462 hb_ot_shape_context_t c = {plan, font, font->face, buffer, user_features, num_user_features}; 463 hb_ot_shape_execute_internal (&c); 464} 465 466hb_bool_t 467_hb_ot_shape (hb_font_t *font, 468 hb_buffer_t *buffer, 469 const hb_feature_t *features, 470 unsigned int num_features) 471{ 472 hb_ot_shape_plan_t plan; 473 474 buffer->guess_properties (); 475 476 hb_ot_shape_plan_internal (&plan, font->face, &buffer->props, features, num_features); 477 hb_ot_shape_execute (&plan, font, buffer, features, num_features); 478 479 return TRUE; 480} 481