1 /***************************************************************************/ 2 /* */ 3 /* afcjk.c */ 4 /* */ 5 /* Auto-fitter hinting routines for CJK writing system (body). */ 6 /* */ 7 /* Copyright 2006-2015 by */ 8 /* David Turner, Robert Wilhelm, and Werner Lemberg. */ 9 /* */ 10 /* This file is part of the FreeType project, and may only be used, */ 11 /* modified, and distributed under the terms of the FreeType project */ 12 /* license, LICENSE.TXT. By continuing to use, modify, or distribute */ 13 /* this file you indicate that you have read the license and */ 14 /* understand and accept it fully. */ 15 /* */ 16 /***************************************************************************/ 17 18 /* 19 * The algorithm is based on akito's autohint patch, available here: 20 * 21 * http://www.kde.gr.jp/~akito/patch/freetype2/ 22 * 23 */ 24 25 #include <ft2build.h> 26 #include FT_ADVANCES_H 27 #include FT_INTERNAL_DEBUG_H 28 29 #include "afglobal.h" 30 #include "afpic.h" 31 #include "aflatin.h" 32 33 34 #ifdef AF_CONFIG_OPTION_CJK 35 36 #undef AF_CONFIG_OPTION_CJK_BLUE_HANI_VERT 37 38 #include "afcjk.h" 39 #include "aferrors.h" 40 41 42 #ifdef AF_CONFIG_OPTION_USE_WARPER 43 #include "afwarp.h" 44 #endif 45 46 47 /*************************************************************************/ 48 /* */ 49 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */ 50 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */ 51 /* messages during execution. */ 52 /* */ 53 #undef FT_COMPONENT 54 #define FT_COMPONENT trace_afcjk 55 56 57 /*************************************************************************/ 58 /*************************************************************************/ 59 /***** *****/ 60 /***** C J K G L O B A L M E T R I C S *****/ 61 /***** *****/ 62 /*************************************************************************/ 63 /*************************************************************************/ 64 65 66 /* Basically the Latin version with AF_CJKMetrics */ 67 /* to replace AF_LatinMetrics. */ 68 69 FT_LOCAL_DEF( void ) af_cjk_metrics_init_widths(AF_CJKMetrics metrics,FT_Face face)70 af_cjk_metrics_init_widths( AF_CJKMetrics metrics, 71 FT_Face face ) 72 { 73 /* scan the array of segments in each direction */ 74 AF_GlyphHintsRec hints[1]; 75 76 77 FT_TRACE5(( "\n" 78 "cjk standard widths computation (style `%s')\n" 79 "===================================================\n" 80 "\n", 81 af_style_names[metrics->root.style_class->style] )); 82 83 af_glyph_hints_init( hints, face->memory ); 84 85 metrics->axis[AF_DIMENSION_HORZ].width_count = 0; 86 metrics->axis[AF_DIMENSION_VERT].width_count = 0; 87 88 { 89 FT_Error error; 90 FT_ULong glyph_index; 91 int dim; 92 AF_CJKMetricsRec dummy[1]; 93 AF_Scaler scaler = &dummy->root.scaler; 94 95 #ifdef FT_CONFIG_OPTION_PIC 96 AF_FaceGlobals globals = metrics->root.globals; 97 #endif 98 99 AF_StyleClass style_class = metrics->root.style_class; 100 AF_ScriptClass script_class = AF_SCRIPT_CLASSES_GET 101 [style_class->script]; 102 103 void* shaper_buf; 104 const char* p; 105 106 #ifdef FT_DEBUG_LEVEL_TRACE 107 FT_ULong ch; 108 #endif 109 110 p = script_class->standard_charstring; 111 shaper_buf = af_shaper_buf_create( face ); 112 113 /* We check a list of standard characters. The first match wins. */ 114 115 glyph_index = 0; 116 while ( *p ) 117 { 118 unsigned int num_idx; 119 120 #ifdef FT_DEBUG_LEVEL_TRACE 121 const char* p_old; 122 #endif 123 124 125 while ( *p == ' ' ) 126 p++; 127 128 #ifdef FT_DEBUG_LEVEL_TRACE 129 p_old = p; 130 GET_UTF8_CHAR( ch, p_old ); 131 #endif 132 133 /* reject input that maps to more than a single glyph */ 134 p = af_shaper_get_cluster( p, &metrics->root, shaper_buf, &num_idx ); 135 if ( num_idx > 1 ) 136 continue; 137 138 /* otherwise exit loop if we have a result */ 139 glyph_index = af_shaper_get_elem( &metrics->root, 140 shaper_buf, 141 0, 142 NULL, 143 NULL ); 144 if ( glyph_index ) 145 break; 146 } 147 148 af_shaper_buf_destroy( face, shaper_buf ); 149 150 if ( !glyph_index ) 151 goto Exit; 152 153 if ( !glyph_index ) 154 goto Exit; 155 156 FT_TRACE5(( "standard character: U+%04lX (glyph index %d)\n", 157 ch, glyph_index )); 158 159 error = FT_Load_Glyph( face, glyph_index, FT_LOAD_NO_SCALE ); 160 if ( error || face->glyph->outline.n_points <= 0 ) 161 goto Exit; 162 163 FT_ZERO( dummy ); 164 165 dummy->units_per_em = metrics->units_per_em; 166 167 scaler->x_scale = 0x10000L; 168 scaler->y_scale = 0x10000L; 169 scaler->x_delta = 0; 170 scaler->y_delta = 0; 171 172 scaler->face = face; 173 scaler->render_mode = FT_RENDER_MODE_NORMAL; 174 scaler->flags = 0; 175 176 af_glyph_hints_rescale( hints, (AF_StyleMetrics)dummy ); 177 178 error = af_glyph_hints_reload( hints, &face->glyph->outline ); 179 if ( error ) 180 goto Exit; 181 182 for ( dim = 0; dim < AF_DIMENSION_MAX; dim++ ) 183 { 184 AF_CJKAxis axis = &metrics->axis[dim]; 185 AF_AxisHints axhints = &hints->axis[dim]; 186 AF_Segment seg, limit, link; 187 FT_UInt num_widths = 0; 188 189 190 error = af_latin_hints_compute_segments( hints, 191 (AF_Dimension)dim ); 192 if ( error ) 193 goto Exit; 194 195 /* 196 * We assume that the glyphs selected for the stem width 197 * computation are `featureless' enough so that the linking 198 * algorithm works fine without adjustments of its scoring 199 * function. 200 */ 201 af_latin_hints_link_segments( hints, 202 0, 203 NULL, 204 (AF_Dimension)dim ); 205 206 seg = axhints->segments; 207 limit = seg + axhints->num_segments; 208 209 for ( ; seg < limit; seg++ ) 210 { 211 link = seg->link; 212 213 /* we only consider stem segments there! */ 214 if ( link && link->link == seg && link > seg ) 215 { 216 FT_Pos dist; 217 218 219 dist = seg->pos - link->pos; 220 if ( dist < 0 ) 221 dist = -dist; 222 223 if ( num_widths < AF_CJK_MAX_WIDTHS ) 224 axis->widths[num_widths++].org = dist; 225 } 226 } 227 228 /* this also replaces multiple almost identical stem widths */ 229 /* with a single one (the value 100 is heuristic) */ 230 af_sort_and_quantize_widths( &num_widths, axis->widths, 231 dummy->units_per_em / 100 ); 232 axis->width_count = num_widths; 233 } 234 235 Exit: 236 for ( dim = 0; dim < AF_DIMENSION_MAX; dim++ ) 237 { 238 AF_CJKAxis axis = &metrics->axis[dim]; 239 FT_Pos stdw; 240 241 242 stdw = ( axis->width_count > 0 ) ? axis->widths[0].org 243 : AF_LATIN_CONSTANT( metrics, 50 ); 244 245 /* let's try 20% of the smallest width */ 246 axis->edge_distance_threshold = stdw / 5; 247 axis->standard_width = stdw; 248 axis->extra_light = 0; 249 250 #ifdef FT_DEBUG_LEVEL_TRACE 251 { 252 FT_UInt i; 253 254 255 FT_TRACE5(( "%s widths:\n", 256 dim == AF_DIMENSION_VERT ? "horizontal" 257 : "vertical" )); 258 259 FT_TRACE5(( " %d (standard)", axis->standard_width )); 260 for ( i = 1; i < axis->width_count; i++ ) 261 FT_TRACE5(( " %d", axis->widths[i].org )); 262 263 FT_TRACE5(( "\n" )); 264 } 265 #endif 266 } 267 } 268 269 FT_TRACE5(( "\n" )); 270 271 af_glyph_hints_done( hints ); 272 } 273 274 275 /* Find all blue zones. */ 276 277 static void af_cjk_metrics_init_blues(AF_CJKMetrics metrics,FT_Face face)278 af_cjk_metrics_init_blues( AF_CJKMetrics metrics, 279 FT_Face face ) 280 { 281 FT_Pos fills[AF_BLUE_STRING_MAX_LEN]; 282 FT_Pos flats[AF_BLUE_STRING_MAX_LEN]; 283 284 FT_UInt num_fills; 285 FT_UInt num_flats; 286 287 FT_Bool fill; 288 289 AF_CJKBlue blue; 290 FT_Error error; 291 AF_CJKAxis axis; 292 FT_Outline outline; 293 294 AF_StyleClass sc = metrics->root.style_class; 295 296 AF_Blue_Stringset bss = sc->blue_stringset; 297 const AF_Blue_StringRec* bs = &af_blue_stringsets[bss]; 298 299 void* shaper_buf; 300 301 302 /* we walk over the blue character strings as specified in the */ 303 /* style's entry in the `af_blue_stringset' array, computing its */ 304 /* extremum points (depending on the string properties) */ 305 306 FT_TRACE5(( "cjk blue zones computation\n" 307 "==========================\n" 308 "\n" )); 309 310 shaper_buf = af_shaper_buf_create( face ); 311 312 for ( ; bs->string != AF_BLUE_STRING_MAX; bs++ ) 313 { 314 const char* p = &af_blue_strings[bs->string]; 315 FT_Pos* blue_ref; 316 FT_Pos* blue_shoot; 317 318 319 if ( AF_CJK_IS_HORIZ_BLUE( bs ) ) 320 axis = &metrics->axis[AF_DIMENSION_HORZ]; 321 else 322 axis = &metrics->axis[AF_DIMENSION_VERT]; 323 324 #ifdef FT_DEBUG_LEVEL_TRACE 325 { 326 FT_String* cjk_blue_name[4] = 327 { 328 (FT_String*)"bottom", /* -- , -- */ 329 (FT_String*)"top", /* -- , TOP */ 330 (FT_String*)"left", /* HORIZ, -- */ 331 (FT_String*)"right" /* HORIZ, TOP */ 332 }; 333 334 335 FT_TRACE5(( "blue zone %d (%s):\n", 336 axis->blue_count, 337 cjk_blue_name[AF_CJK_IS_HORIZ_BLUE( bs ) | 338 AF_CJK_IS_TOP_BLUE( bs ) ] )); 339 } 340 #endif /* FT_DEBUG_LEVEL_TRACE */ 341 342 num_fills = 0; 343 num_flats = 0; 344 345 fill = 1; /* start with characters that define fill values */ 346 FT_TRACE5(( " [overshoot values]\n" )); 347 348 while ( *p ) 349 { 350 FT_ULong glyph_index; 351 FT_Pos best_pos; /* same as points.y or points.x, resp. */ 352 FT_Int best_point; 353 FT_Vector* points; 354 355 unsigned int num_idx; 356 357 #ifdef FT_DEBUG_LEVEL_TRACE 358 const char* p_old; 359 FT_ULong ch; 360 #endif 361 362 363 while ( *p == ' ' ) 364 p++; 365 366 #ifdef FT_DEBUG_LEVEL_TRACE 367 p_old = p; 368 GET_UTF8_CHAR( ch, p_old ); 369 #endif 370 371 /* switch to characters that define flat values */ 372 if ( *p == '|' ) 373 { 374 fill = 0; 375 FT_TRACE5(( " [reference values]\n" )); 376 p++; 377 continue; 378 } 379 380 /* reject input that maps to more than a single glyph */ 381 p = af_shaper_get_cluster( p, &metrics->root, shaper_buf, &num_idx ); 382 if ( num_idx > 1 ) 383 continue; 384 385 /* load the character in the face -- skip unknown or empty ones */ 386 glyph_index = af_shaper_get_elem( &metrics->root, 387 shaper_buf, 388 0, 389 NULL, 390 NULL ); 391 if ( glyph_index == 0 ) 392 { 393 FT_TRACE5(( " U+%04lX unavailable\n", ch )); 394 continue; 395 } 396 397 error = FT_Load_Glyph( face, glyph_index, FT_LOAD_NO_SCALE ); 398 outline = face->glyph->outline; 399 if ( error || outline.n_points <= 2 ) 400 { 401 FT_TRACE5(( " U+%04lX contains no (usable) outlines\n", ch )); 402 continue; 403 } 404 405 /* now compute min or max point indices and coordinates */ 406 points = outline.points; 407 best_point = -1; 408 best_pos = 0; /* make compiler happy */ 409 410 { 411 FT_Int nn; 412 FT_Int first = 0; 413 FT_Int last = -1; 414 415 416 for ( nn = 0; nn < outline.n_contours; first = last + 1, nn++ ) 417 { 418 FT_Int pp; 419 420 421 last = outline.contours[nn]; 422 423 /* Avoid single-point contours since they are never rasterized. */ 424 /* In some fonts, they correspond to mark attachment points */ 425 /* which are way outside of the glyph's real outline. */ 426 if ( last <= first ) 427 continue; 428 429 if ( AF_CJK_IS_HORIZ_BLUE( bs ) ) 430 { 431 if ( AF_CJK_IS_RIGHT_BLUE( bs ) ) 432 { 433 for ( pp = first; pp <= last; pp++ ) 434 if ( best_point < 0 || points[pp].x > best_pos ) 435 { 436 best_point = pp; 437 best_pos = points[pp].x; 438 } 439 } 440 else 441 { 442 for ( pp = first; pp <= last; pp++ ) 443 if ( best_point < 0 || points[pp].x < best_pos ) 444 { 445 best_point = pp; 446 best_pos = points[pp].x; 447 } 448 } 449 } 450 else 451 { 452 if ( AF_CJK_IS_TOP_BLUE( bs ) ) 453 { 454 for ( pp = first; pp <= last; pp++ ) 455 if ( best_point < 0 || points[pp].y > best_pos ) 456 { 457 best_point = pp; 458 best_pos = points[pp].y; 459 } 460 } 461 else 462 { 463 for ( pp = first; pp <= last; pp++ ) 464 if ( best_point < 0 || points[pp].y < best_pos ) 465 { 466 best_point = pp; 467 best_pos = points[pp].y; 468 } 469 } 470 } 471 } 472 473 FT_TRACE5(( " U+%04lX: best_pos = %5ld\n", ch, best_pos )); 474 } 475 476 if ( fill ) 477 fills[num_fills++] = best_pos; 478 else 479 flats[num_flats++] = best_pos; 480 481 } /* end while loop */ 482 483 if ( num_flats == 0 && num_fills == 0 ) 484 { 485 /* 486 * we couldn't find a single glyph to compute this blue zone, 487 * we will simply ignore it then 488 */ 489 FT_TRACE5(( " empty\n" )); 490 continue; 491 } 492 493 /* we have computed the contents of the `fill' and `flats' tables, */ 494 /* now determine the reference and overshoot position of the blue -- */ 495 /* we simply take the median value after a simple sort */ 496 af_sort_pos( num_fills, fills ); 497 af_sort_pos( num_flats, flats ); 498 499 blue = &axis->blues[axis->blue_count]; 500 blue_ref = &blue->ref.org; 501 blue_shoot = &blue->shoot.org; 502 503 axis->blue_count++; 504 505 if ( num_flats == 0 ) 506 { 507 *blue_ref = 508 *blue_shoot = fills[num_fills / 2]; 509 } 510 else if ( num_fills == 0 ) 511 { 512 *blue_ref = 513 *blue_shoot = flats[num_flats / 2]; 514 } 515 else 516 { 517 *blue_ref = fills[num_fills / 2]; 518 *blue_shoot = flats[num_flats / 2]; 519 } 520 521 /* make sure blue_ref >= blue_shoot for top/right or */ 522 /* vice versa for bottom/left */ 523 if ( *blue_shoot != *blue_ref ) 524 { 525 FT_Pos ref = *blue_ref; 526 FT_Pos shoot = *blue_shoot; 527 FT_Bool under_ref = FT_BOOL( shoot < ref ); 528 529 530 /* AF_CJK_IS_TOP_BLUE covers `right' and `top' */ 531 if ( AF_CJK_IS_TOP_BLUE( bs ) ^ under_ref ) 532 { 533 *blue_ref = 534 *blue_shoot = ( shoot + ref ) / 2; 535 536 FT_TRACE5(( " [reference smaller than overshoot," 537 " taking mean value]\n" )); 538 } 539 } 540 541 blue->flags = 0; 542 if ( AF_CJK_IS_TOP_BLUE( bs ) ) 543 blue->flags |= AF_CJK_BLUE_TOP; 544 545 FT_TRACE5(( " -> reference = %ld\n" 546 " overshoot = %ld\n", 547 *blue_ref, *blue_shoot )); 548 549 } /* end for loop */ 550 551 af_shaper_buf_destroy( face, shaper_buf ); 552 553 FT_TRACE5(( "\n" )); 554 555 return; 556 } 557 558 559 /* Basically the Latin version with type AF_CJKMetrics for metrics. */ 560 561 FT_LOCAL_DEF( void ) af_cjk_metrics_check_digits(AF_CJKMetrics metrics,FT_Face face)562 af_cjk_metrics_check_digits( AF_CJKMetrics metrics, 563 FT_Face face ) 564 { 565 FT_Bool started = 0, same_width = 1; 566 FT_Fixed advance, old_advance = 0; 567 568 void* shaper_buf; 569 570 /* in all supported charmaps, digits have character codes 0x30-0x39 */ 571 const char digits[] = "0 1 2 3 4 5 6 7 8 9"; 572 const char* p; 573 574 575 p = digits; 576 shaper_buf = af_shaper_buf_create( face ); 577 578 while ( *p ) 579 { 580 FT_ULong glyph_index; 581 unsigned int num_idx; 582 583 584 /* reject input that maps to more than a single glyph */ 585 p = af_shaper_get_cluster( p, &metrics->root, shaper_buf, &num_idx ); 586 if ( num_idx > 1 ) 587 continue; 588 589 glyph_index = af_shaper_get_elem( &metrics->root, 590 shaper_buf, 591 0, 592 &advance, 593 NULL ); 594 if ( !glyph_index ) 595 continue; 596 597 if ( started ) 598 { 599 if ( advance != old_advance ) 600 { 601 same_width = 0; 602 break; 603 } 604 } 605 else 606 { 607 old_advance = advance; 608 started = 1; 609 } 610 } 611 612 af_shaper_buf_destroy( face, shaper_buf ); 613 614 metrics->root.digits_have_same_width = same_width; 615 } 616 617 618 /* Initialize global metrics. */ 619 620 FT_LOCAL_DEF( FT_Error ) af_cjk_metrics_init(AF_CJKMetrics metrics,FT_Face face)621 af_cjk_metrics_init( AF_CJKMetrics metrics, 622 FT_Face face ) 623 { 624 FT_CharMap oldmap = face->charmap; 625 626 627 metrics->units_per_em = face->units_per_EM; 628 629 if ( !FT_Select_Charmap( face, FT_ENCODING_UNICODE ) ) 630 { 631 af_cjk_metrics_init_widths( metrics, face ); 632 af_cjk_metrics_init_blues( metrics, face ); 633 af_cjk_metrics_check_digits( metrics, face ); 634 } 635 636 FT_Set_Charmap( face, oldmap ); 637 return FT_Err_Ok; 638 } 639 640 641 /* Adjust scaling value, then scale and shift widths */ 642 /* and blue zones (if applicable) for given dimension. */ 643 644 static void af_cjk_metrics_scale_dim(AF_CJKMetrics metrics,AF_Scaler scaler,AF_Dimension dim)645 af_cjk_metrics_scale_dim( AF_CJKMetrics metrics, 646 AF_Scaler scaler, 647 AF_Dimension dim ) 648 { 649 FT_Fixed scale; 650 FT_Pos delta; 651 AF_CJKAxis axis; 652 FT_UInt nn; 653 654 655 if ( dim == AF_DIMENSION_HORZ ) 656 { 657 scale = scaler->x_scale; 658 delta = scaler->x_delta; 659 } 660 else 661 { 662 scale = scaler->y_scale; 663 delta = scaler->y_delta; 664 } 665 666 axis = &metrics->axis[dim]; 667 668 if ( axis->org_scale == scale && axis->org_delta == delta ) 669 return; 670 671 axis->org_scale = scale; 672 axis->org_delta = delta; 673 674 axis->scale = scale; 675 axis->delta = delta; 676 677 /* scale the blue zones */ 678 for ( nn = 0; nn < axis->blue_count; nn++ ) 679 { 680 AF_CJKBlue blue = &axis->blues[nn]; 681 FT_Pos dist; 682 683 684 blue->ref.cur = FT_MulFix( blue->ref.org, scale ) + delta; 685 blue->ref.fit = blue->ref.cur; 686 blue->shoot.cur = FT_MulFix( blue->shoot.org, scale ) + delta; 687 blue->shoot.fit = blue->shoot.cur; 688 blue->flags &= ~AF_CJK_BLUE_ACTIVE; 689 690 /* a blue zone is only active if it is less than 3/4 pixels tall */ 691 dist = FT_MulFix( blue->ref.org - blue->shoot.org, scale ); 692 if ( dist <= 48 && dist >= -48 ) 693 { 694 FT_Pos delta1, delta2; 695 696 697 blue->ref.fit = FT_PIX_ROUND( blue->ref.cur ); 698 699 /* shoot is under shoot for cjk */ 700 delta1 = FT_DivFix( blue->ref.fit, scale ) - blue->shoot.org; 701 delta2 = delta1; 702 if ( delta1 < 0 ) 703 delta2 = -delta2; 704 705 delta2 = FT_MulFix( delta2, scale ); 706 707 FT_TRACE5(( "delta: %d", delta1 )); 708 if ( delta2 < 32 ) 709 delta2 = 0; 710 #if 0 711 else if ( delta2 < 64 ) 712 delta2 = 32 + ( ( ( delta2 - 32 ) + 16 ) & ~31 ); 713 #endif 714 else 715 delta2 = FT_PIX_ROUND( delta2 ); 716 FT_TRACE5(( "/%d\n", delta2 )); 717 718 if ( delta1 < 0 ) 719 delta2 = -delta2; 720 721 blue->shoot.fit = blue->ref.fit - delta2; 722 723 FT_TRACE5(( ">> active cjk blue zone %c%d[%ld/%ld]:\n" 724 " ref: cur=%.2f fit=%.2f\n" 725 " shoot: cur=%.2f fit=%.2f\n", 726 ( dim == AF_DIMENSION_HORZ ) ? 'H' : 'V', 727 nn, blue->ref.org, blue->shoot.org, 728 blue->ref.cur / 64.0, blue->ref.fit / 64.0, 729 blue->shoot.cur / 64.0, blue->shoot.fit / 64.0 )); 730 731 blue->flags |= AF_CJK_BLUE_ACTIVE; 732 } 733 } 734 } 735 736 737 /* Scale global values in both directions. */ 738 739 FT_LOCAL_DEF( void ) af_cjk_metrics_scale(AF_CJKMetrics metrics,AF_Scaler scaler)740 af_cjk_metrics_scale( AF_CJKMetrics metrics, 741 AF_Scaler scaler ) 742 { 743 /* we copy the whole structure since the x and y scaling values */ 744 /* are not modified, contrary to e.g. the `latin' auto-hinter */ 745 metrics->root.scaler = *scaler; 746 747 af_cjk_metrics_scale_dim( metrics, scaler, AF_DIMENSION_HORZ ); 748 af_cjk_metrics_scale_dim( metrics, scaler, AF_DIMENSION_VERT ); 749 } 750 751 752 /* Extract standard_width from writing system/script specific */ 753 /* metrics class. */ 754 755 FT_LOCAL_DEF( void ) af_cjk_get_standard_widths(AF_CJKMetrics metrics,FT_Pos * stdHW,FT_Pos * stdVW)756 af_cjk_get_standard_widths( AF_CJKMetrics metrics, 757 FT_Pos* stdHW, 758 FT_Pos* stdVW ) 759 { 760 if ( stdHW ) 761 *stdHW = metrics->axis[AF_DIMENSION_VERT].standard_width; 762 763 if ( stdVW ) 764 *stdVW = metrics->axis[AF_DIMENSION_HORZ].standard_width; 765 } 766 767 768 /*************************************************************************/ 769 /*************************************************************************/ 770 /***** *****/ 771 /***** C J K G L Y P H A N A L Y S I S *****/ 772 /***** *****/ 773 /*************************************************************************/ 774 /*************************************************************************/ 775 776 777 /* Walk over all contours and compute its segments. */ 778 779 static FT_Error af_cjk_hints_compute_segments(AF_GlyphHints hints,AF_Dimension dim)780 af_cjk_hints_compute_segments( AF_GlyphHints hints, 781 AF_Dimension dim ) 782 { 783 AF_AxisHints axis = &hints->axis[dim]; 784 AF_Segment segments = axis->segments; 785 AF_Segment segment_limit = segments + axis->num_segments; 786 FT_Error error; 787 AF_Segment seg; 788 789 790 error = af_latin_hints_compute_segments( hints, dim ); 791 if ( error ) 792 return error; 793 794 /* a segment is round if it doesn't have successive */ 795 /* on-curve points. */ 796 for ( seg = segments; seg < segment_limit; seg++ ) 797 { 798 AF_Point pt = seg->first; 799 AF_Point last = seg->last; 800 FT_UInt f0 = pt->flags & AF_FLAG_CONTROL; 801 FT_UInt f1; 802 803 804 seg->flags &= ~AF_EDGE_ROUND; 805 806 for ( ; pt != last; f0 = f1 ) 807 { 808 pt = pt->next; 809 f1 = pt->flags & AF_FLAG_CONTROL; 810 811 if ( !f0 && !f1 ) 812 break; 813 814 if ( pt == last ) 815 seg->flags |= AF_EDGE_ROUND; 816 } 817 } 818 819 return FT_Err_Ok; 820 } 821 822 823 static void af_cjk_hints_link_segments(AF_GlyphHints hints,AF_Dimension dim)824 af_cjk_hints_link_segments( AF_GlyphHints hints, 825 AF_Dimension dim ) 826 { 827 AF_AxisHints axis = &hints->axis[dim]; 828 AF_Segment segments = axis->segments; 829 AF_Segment segment_limit = segments + axis->num_segments; 830 AF_Direction major_dir = axis->major_dir; 831 AF_Segment seg1, seg2; 832 FT_Pos len_threshold; 833 FT_Pos dist_threshold; 834 835 836 len_threshold = AF_LATIN_CONSTANT( hints->metrics, 8 ); 837 838 dist_threshold = ( dim == AF_DIMENSION_HORZ ) ? hints->x_scale 839 : hints->y_scale; 840 dist_threshold = FT_DivFix( 64 * 3, dist_threshold ); 841 842 /* now compare each segment to the others */ 843 for ( seg1 = segments; seg1 < segment_limit; seg1++ ) 844 { 845 if ( seg1->dir != major_dir ) 846 continue; 847 848 for ( seg2 = segments; seg2 < segment_limit; seg2++ ) 849 if ( seg2 != seg1 && seg1->dir + seg2->dir == 0 ) 850 { 851 FT_Pos dist = seg2->pos - seg1->pos; 852 853 854 if ( dist < 0 ) 855 continue; 856 857 { 858 FT_Pos min = seg1->min_coord; 859 FT_Pos max = seg1->max_coord; 860 FT_Pos len; 861 862 863 if ( min < seg2->min_coord ) 864 min = seg2->min_coord; 865 866 if ( max > seg2->max_coord ) 867 max = seg2->max_coord; 868 869 len = max - min; 870 if ( len >= len_threshold ) 871 { 872 if ( dist * 8 < seg1->score * 9 && 873 ( dist * 8 < seg1->score * 7 || seg1->len < len ) ) 874 { 875 seg1->score = dist; 876 seg1->len = len; 877 seg1->link = seg2; 878 } 879 880 if ( dist * 8 < seg2->score * 9 && 881 ( dist * 8 < seg2->score * 7 || seg2->len < len ) ) 882 { 883 seg2->score = dist; 884 seg2->len = len; 885 seg2->link = seg1; 886 } 887 } 888 } 889 } 890 } 891 892 /* 893 * now compute the `serif' segments 894 * 895 * In Hanzi, some strokes are wider on one or both of the ends. 896 * We either identify the stems on the ends as serifs or remove 897 * the linkage, depending on the length of the stems. 898 * 899 */ 900 901 { 902 AF_Segment link1, link2; 903 904 905 for ( seg1 = segments; seg1 < segment_limit; seg1++ ) 906 { 907 link1 = seg1->link; 908 if ( !link1 || link1->link != seg1 || link1->pos <= seg1->pos ) 909 continue; 910 911 if ( seg1->score >= dist_threshold ) 912 continue; 913 914 for ( seg2 = segments; seg2 < segment_limit; seg2++ ) 915 { 916 if ( seg2->pos > seg1->pos || seg1 == seg2 ) 917 continue; 918 919 link2 = seg2->link; 920 if ( !link2 || link2->link != seg2 || link2->pos < link1->pos ) 921 continue; 922 923 if ( seg1->pos == seg2->pos && link1->pos == link2->pos ) 924 continue; 925 926 if ( seg2->score <= seg1->score || seg1->score * 4 <= seg2->score ) 927 continue; 928 929 /* seg2 < seg1 < link1 < link2 */ 930 931 if ( seg1->len >= seg2->len * 3 ) 932 { 933 AF_Segment seg; 934 935 936 for ( seg = segments; seg < segment_limit; seg++ ) 937 { 938 AF_Segment link = seg->link; 939 940 941 if ( link == seg2 ) 942 { 943 seg->link = NULL; 944 seg->serif = link1; 945 } 946 else if ( link == link2 ) 947 { 948 seg->link = NULL; 949 seg->serif = seg1; 950 } 951 } 952 } 953 else 954 { 955 seg1->link = link1->link = NULL; 956 957 break; 958 } 959 } 960 } 961 } 962 963 for ( seg1 = segments; seg1 < segment_limit; seg1++ ) 964 { 965 seg2 = seg1->link; 966 967 if ( seg2 ) 968 { 969 seg2->num_linked++; 970 if ( seg2->link != seg1 ) 971 { 972 seg1->link = NULL; 973 974 if ( seg2->score < dist_threshold || seg1->score < seg2->score * 4 ) 975 seg1->serif = seg2->link; 976 else 977 seg2->num_linked--; 978 } 979 } 980 } 981 } 982 983 984 static FT_Error af_cjk_hints_compute_edges(AF_GlyphHints hints,AF_Dimension dim)985 af_cjk_hints_compute_edges( AF_GlyphHints hints, 986 AF_Dimension dim ) 987 { 988 AF_AxisHints axis = &hints->axis[dim]; 989 FT_Error error = FT_Err_Ok; 990 FT_Memory memory = hints->memory; 991 AF_CJKAxis laxis = &((AF_CJKMetrics)hints->metrics)->axis[dim]; 992 993 AF_Segment segments = axis->segments; 994 AF_Segment segment_limit = segments + axis->num_segments; 995 AF_Segment seg; 996 997 FT_Fixed scale; 998 FT_Pos edge_distance_threshold; 999 1000 1001 axis->num_edges = 0; 1002 1003 scale = ( dim == AF_DIMENSION_HORZ ) ? hints->x_scale 1004 : hints->y_scale; 1005 1006 /*********************************************************************/ 1007 /* */ 1008 /* We begin by generating a sorted table of edges for the current */ 1009 /* direction. To do so, we simply scan each segment and try to find */ 1010 /* an edge in our table that corresponds to its position. */ 1011 /* */ 1012 /* If no edge is found, we create and insert a new edge in the */ 1013 /* sorted table. Otherwise, we simply add the segment to the edge's */ 1014 /* list which is then processed in the second step to compute the */ 1015 /* edge's properties. */ 1016 /* */ 1017 /* Note that the edges table is sorted along the segment/edge */ 1018 /* position. */ 1019 /* */ 1020 /*********************************************************************/ 1021 1022 edge_distance_threshold = FT_MulFix( laxis->edge_distance_threshold, 1023 scale ); 1024 if ( edge_distance_threshold > 64 / 4 ) 1025 edge_distance_threshold = FT_DivFix( 64 / 4, scale ); 1026 else 1027 edge_distance_threshold = laxis->edge_distance_threshold; 1028 1029 for ( seg = segments; seg < segment_limit; seg++ ) 1030 { 1031 AF_Edge found = NULL; 1032 FT_Pos best = 0xFFFFU; 1033 FT_Int ee; 1034 1035 1036 /* look for an edge corresponding to the segment */ 1037 for ( ee = 0; ee < axis->num_edges; ee++ ) 1038 { 1039 AF_Edge edge = axis->edges + ee; 1040 FT_Pos dist; 1041 1042 1043 if ( edge->dir != seg->dir ) 1044 continue; 1045 1046 dist = seg->pos - edge->fpos; 1047 if ( dist < 0 ) 1048 dist = -dist; 1049 1050 if ( dist < edge_distance_threshold && dist < best ) 1051 { 1052 AF_Segment link = seg->link; 1053 1054 1055 /* check whether all linked segments of the candidate edge */ 1056 /* can make a single edge. */ 1057 if ( link ) 1058 { 1059 AF_Segment seg1 = edge->first; 1060 FT_Pos dist2 = 0; 1061 1062 1063 do 1064 { 1065 AF_Segment link1 = seg1->link; 1066 1067 1068 if ( link1 ) 1069 { 1070 dist2 = AF_SEGMENT_DIST( link, link1 ); 1071 if ( dist2 >= edge_distance_threshold ) 1072 break; 1073 } 1074 1075 } while ( ( seg1 = seg1->edge_next ) != edge->first ); 1076 1077 if ( dist2 >= edge_distance_threshold ) 1078 continue; 1079 } 1080 1081 best = dist; 1082 found = edge; 1083 } 1084 } 1085 1086 if ( !found ) 1087 { 1088 AF_Edge edge; 1089 1090 1091 /* insert a new edge in the list and */ 1092 /* sort according to the position */ 1093 error = af_axis_hints_new_edge( axis, seg->pos, 1094 (AF_Direction)seg->dir, 1095 memory, &edge ); 1096 if ( error ) 1097 goto Exit; 1098 1099 /* add the segment to the new edge's list */ 1100 FT_ZERO( edge ); 1101 1102 edge->first = seg; 1103 edge->last = seg; 1104 edge->dir = seg->dir; 1105 edge->fpos = seg->pos; 1106 edge->opos = FT_MulFix( seg->pos, scale ); 1107 edge->pos = edge->opos; 1108 seg->edge_next = seg; 1109 } 1110 else 1111 { 1112 /* if an edge was found, simply add the segment to the edge's */ 1113 /* list */ 1114 seg->edge_next = found->first; 1115 found->last->edge_next = seg; 1116 found->last = seg; 1117 } 1118 } 1119 1120 /******************************************************************/ 1121 /* */ 1122 /* Good, we now compute each edge's properties according to the */ 1123 /* segments found on its position. Basically, these are */ 1124 /* */ 1125 /* - the edge's main direction */ 1126 /* - stem edge, serif edge or both (which defaults to stem then) */ 1127 /* - rounded edge, straight or both (which defaults to straight) */ 1128 /* - link for edge */ 1129 /* */ 1130 /******************************************************************/ 1131 1132 /* first of all, set the `edge' field in each segment -- this is */ 1133 /* required in order to compute edge links */ 1134 1135 /* 1136 * Note that removing this loop and setting the `edge' field of each 1137 * segment directly in the code above slows down execution speed for 1138 * some reasons on platforms like the Sun. 1139 */ 1140 { 1141 AF_Edge edges = axis->edges; 1142 AF_Edge edge_limit = edges + axis->num_edges; 1143 AF_Edge edge; 1144 1145 1146 for ( edge = edges; edge < edge_limit; edge++ ) 1147 { 1148 seg = edge->first; 1149 if ( seg ) 1150 do 1151 { 1152 seg->edge = edge; 1153 seg = seg->edge_next; 1154 1155 } while ( seg != edge->first ); 1156 } 1157 1158 /* now compute each edge properties */ 1159 for ( edge = edges; edge < edge_limit; edge++ ) 1160 { 1161 FT_Int is_round = 0; /* does it contain round segments? */ 1162 FT_Int is_straight = 0; /* does it contain straight segments? */ 1163 1164 1165 seg = edge->first; 1166 1167 do 1168 { 1169 FT_Bool is_serif; 1170 1171 1172 /* check for roundness of segment */ 1173 if ( seg->flags & AF_EDGE_ROUND ) 1174 is_round++; 1175 else 1176 is_straight++; 1177 1178 /* check for links -- if seg->serif is set, then seg->link must */ 1179 /* be ignored */ 1180 is_serif = (FT_Bool)( seg->serif && seg->serif->edge != edge ); 1181 1182 if ( seg->link || is_serif ) 1183 { 1184 AF_Edge edge2; 1185 AF_Segment seg2; 1186 1187 1188 edge2 = edge->link; 1189 seg2 = seg->link; 1190 1191 if ( is_serif ) 1192 { 1193 seg2 = seg->serif; 1194 edge2 = edge->serif; 1195 } 1196 1197 if ( edge2 ) 1198 { 1199 FT_Pos edge_delta; 1200 FT_Pos seg_delta; 1201 1202 1203 edge_delta = edge->fpos - edge2->fpos; 1204 if ( edge_delta < 0 ) 1205 edge_delta = -edge_delta; 1206 1207 seg_delta = AF_SEGMENT_DIST( seg, seg2 ); 1208 1209 if ( seg_delta < edge_delta ) 1210 edge2 = seg2->edge; 1211 } 1212 else 1213 edge2 = seg2->edge; 1214 1215 if ( is_serif ) 1216 { 1217 edge->serif = edge2; 1218 edge2->flags |= AF_EDGE_SERIF; 1219 } 1220 else 1221 edge->link = edge2; 1222 } 1223 1224 seg = seg->edge_next; 1225 1226 } while ( seg != edge->first ); 1227 1228 /* set the round/straight flags */ 1229 edge->flags = AF_EDGE_NORMAL; 1230 1231 if ( is_round > 0 && is_round >= is_straight ) 1232 edge->flags |= AF_EDGE_ROUND; 1233 1234 /* get rid of serifs if link is set */ 1235 /* XXX: This gets rid of many unpleasant artefacts! */ 1236 /* Example: the `c' in cour.pfa at size 13 */ 1237 1238 if ( edge->serif && edge->link ) 1239 edge->serif = NULL; 1240 } 1241 } 1242 1243 Exit: 1244 return error; 1245 } 1246 1247 1248 /* Detect segments and edges for given dimension. */ 1249 1250 static FT_Error af_cjk_hints_detect_features(AF_GlyphHints hints,AF_Dimension dim)1251 af_cjk_hints_detect_features( AF_GlyphHints hints, 1252 AF_Dimension dim ) 1253 { 1254 FT_Error error; 1255 1256 1257 error = af_cjk_hints_compute_segments( hints, dim ); 1258 if ( !error ) 1259 { 1260 af_cjk_hints_link_segments( hints, dim ); 1261 1262 error = af_cjk_hints_compute_edges( hints, dim ); 1263 } 1264 return error; 1265 } 1266 1267 1268 /* Compute all edges which lie within blue zones. */ 1269 1270 static void af_cjk_hints_compute_blue_edges(AF_GlyphHints hints,AF_CJKMetrics metrics,AF_Dimension dim)1271 af_cjk_hints_compute_blue_edges( AF_GlyphHints hints, 1272 AF_CJKMetrics metrics, 1273 AF_Dimension dim ) 1274 { 1275 AF_AxisHints axis = &hints->axis[dim]; 1276 AF_Edge edge = axis->edges; 1277 AF_Edge edge_limit = edge + axis->num_edges; 1278 AF_CJKAxis cjk = &metrics->axis[dim]; 1279 FT_Fixed scale = cjk->scale; 1280 FT_Pos best_dist0; /* initial threshold */ 1281 1282 1283 /* compute the initial threshold as a fraction of the EM size */ 1284 best_dist0 = FT_MulFix( metrics->units_per_em / 40, scale ); 1285 1286 if ( best_dist0 > 64 / 2 ) /* maximum 1/2 pixel */ 1287 best_dist0 = 64 / 2; 1288 1289 /* compute which blue zones are active, i.e. have their scaled */ 1290 /* size < 3/4 pixels */ 1291 1292 /* If the distant between an edge and a blue zone is shorter than */ 1293 /* best_dist0, set the blue zone for the edge. Then search for */ 1294 /* the blue zone with the smallest best_dist to the edge. */ 1295 1296 for ( ; edge < edge_limit; edge++ ) 1297 { 1298 FT_UInt bb; 1299 AF_Width best_blue = NULL; 1300 FT_Pos best_dist = best_dist0; 1301 1302 1303 for ( bb = 0; bb < cjk->blue_count; bb++ ) 1304 { 1305 AF_CJKBlue blue = cjk->blues + bb; 1306 FT_Bool is_top_right_blue, is_major_dir; 1307 1308 1309 /* skip inactive blue zones (i.e., those that are too small) */ 1310 if ( !( blue->flags & AF_CJK_BLUE_ACTIVE ) ) 1311 continue; 1312 1313 /* if it is a top zone, check for right edges -- if it is a bottom */ 1314 /* zone, check for left edges */ 1315 /* */ 1316 /* of course, that's for TrueType */ 1317 is_top_right_blue = 1318 (FT_Byte)( ( blue->flags & AF_CJK_BLUE_TOP ) != 0 ); 1319 is_major_dir = 1320 FT_BOOL( edge->dir == axis->major_dir ); 1321 1322 /* if it is a top zone, the edge must be against the major */ 1323 /* direction; if it is a bottom zone, it must be in the major */ 1324 /* direction */ 1325 if ( is_top_right_blue ^ is_major_dir ) 1326 { 1327 FT_Pos dist; 1328 AF_Width compare; 1329 1330 1331 /* Compare the edge to the closest blue zone type */ 1332 if ( FT_ABS( edge->fpos - blue->ref.org ) > 1333 FT_ABS( edge->fpos - blue->shoot.org ) ) 1334 compare = &blue->shoot; 1335 else 1336 compare = &blue->ref; 1337 1338 dist = edge->fpos - compare->org; 1339 if ( dist < 0 ) 1340 dist = -dist; 1341 1342 dist = FT_MulFix( dist, scale ); 1343 if ( dist < best_dist ) 1344 { 1345 best_dist = dist; 1346 best_blue = compare; 1347 } 1348 } 1349 } 1350 1351 if ( best_blue ) 1352 edge->blue_edge = best_blue; 1353 } 1354 } 1355 1356 1357 /* Initalize hinting engine. */ 1358 1359 FT_LOCAL_DEF( FT_Error ) af_cjk_hints_init(AF_GlyphHints hints,AF_CJKMetrics metrics)1360 af_cjk_hints_init( AF_GlyphHints hints, 1361 AF_CJKMetrics metrics ) 1362 { 1363 FT_Render_Mode mode; 1364 FT_UInt32 scaler_flags, other_flags; 1365 1366 1367 af_glyph_hints_rescale( hints, (AF_StyleMetrics)metrics ); 1368 1369 /* 1370 * correct x_scale and y_scale when needed, since they may have 1371 * been modified af_cjk_scale_dim above 1372 */ 1373 hints->x_scale = metrics->axis[AF_DIMENSION_HORZ].scale; 1374 hints->x_delta = metrics->axis[AF_DIMENSION_HORZ].delta; 1375 hints->y_scale = metrics->axis[AF_DIMENSION_VERT].scale; 1376 hints->y_delta = metrics->axis[AF_DIMENSION_VERT].delta; 1377 1378 /* compute flags depending on render mode, etc. */ 1379 mode = metrics->root.scaler.render_mode; 1380 1381 #if 0 /* AF_CONFIG_OPTION_USE_WARPER */ 1382 if ( mode == FT_RENDER_MODE_LCD || mode == FT_RENDER_MODE_LCD_V ) 1383 metrics->root.scaler.render_mode = mode = FT_RENDER_MODE_NORMAL; 1384 #endif 1385 1386 scaler_flags = hints->scaler_flags; 1387 other_flags = 0; 1388 1389 /* 1390 * We snap the width of vertical stems for the monochrome and 1391 * horizontal LCD rendering targets only. 1392 */ 1393 if ( mode == FT_RENDER_MODE_MONO || mode == FT_RENDER_MODE_LCD ) 1394 other_flags |= AF_LATIN_HINTS_HORZ_SNAP; 1395 1396 /* 1397 * We snap the width of horizontal stems for the monochrome and 1398 * vertical LCD rendering targets only. 1399 */ 1400 if ( mode == FT_RENDER_MODE_MONO || mode == FT_RENDER_MODE_LCD_V ) 1401 other_flags |= AF_LATIN_HINTS_VERT_SNAP; 1402 1403 /* 1404 * We adjust stems to full pixels only if we don't use the `light' mode. 1405 */ 1406 if ( mode != FT_RENDER_MODE_LIGHT ) 1407 other_flags |= AF_LATIN_HINTS_STEM_ADJUST; 1408 1409 if ( mode == FT_RENDER_MODE_MONO ) 1410 other_flags |= AF_LATIN_HINTS_MONO; 1411 1412 scaler_flags |= AF_SCALER_FLAG_NO_ADVANCE; 1413 1414 #ifdef AF_CONFIG_OPTION_USE_WARPER 1415 /* get (global) warper flag */ 1416 if ( !metrics->root.globals->module->warping ) 1417 scaler_flags |= AF_SCALER_FLAG_NO_WARPER; 1418 #endif 1419 1420 hints->scaler_flags = scaler_flags; 1421 hints->other_flags = other_flags; 1422 1423 return FT_Err_Ok; 1424 } 1425 1426 1427 /*************************************************************************/ 1428 /*************************************************************************/ 1429 /***** *****/ 1430 /***** C J K G L Y P H G R I D - F I T T I N G *****/ 1431 /***** *****/ 1432 /*************************************************************************/ 1433 /*************************************************************************/ 1434 1435 /* Snap a given width in scaled coordinates to one of the */ 1436 /* current standard widths. */ 1437 1438 static FT_Pos af_cjk_snap_width(AF_Width widths,FT_UInt count,FT_Pos width)1439 af_cjk_snap_width( AF_Width widths, 1440 FT_UInt count, 1441 FT_Pos width ) 1442 { 1443 FT_UInt n; 1444 FT_Pos best = 64 + 32 + 2; 1445 FT_Pos reference = width; 1446 FT_Pos scaled; 1447 1448 1449 for ( n = 0; n < count; n++ ) 1450 { 1451 FT_Pos w; 1452 FT_Pos dist; 1453 1454 1455 w = widths[n].cur; 1456 dist = width - w; 1457 if ( dist < 0 ) 1458 dist = -dist; 1459 if ( dist < best ) 1460 { 1461 best = dist; 1462 reference = w; 1463 } 1464 } 1465 1466 scaled = FT_PIX_ROUND( reference ); 1467 1468 if ( width >= reference ) 1469 { 1470 if ( width < scaled + 48 ) 1471 width = reference; 1472 } 1473 else 1474 { 1475 if ( width > scaled - 48 ) 1476 width = reference; 1477 } 1478 1479 return width; 1480 } 1481 1482 1483 /* Compute the snapped width of a given stem. */ 1484 /* There is a lot of voodoo in this function; changing the hard-coded */ 1485 /* parameters influence the whole hinting process. */ 1486 1487 static FT_Pos af_cjk_compute_stem_width(AF_GlyphHints hints,AF_Dimension dim,FT_Pos width,FT_UInt base_flags,FT_UInt stem_flags)1488 af_cjk_compute_stem_width( AF_GlyphHints hints, 1489 AF_Dimension dim, 1490 FT_Pos width, 1491 FT_UInt base_flags, 1492 FT_UInt stem_flags ) 1493 { 1494 AF_CJKMetrics metrics = (AF_CJKMetrics)hints->metrics; 1495 AF_CJKAxis axis = &metrics->axis[dim]; 1496 FT_Pos dist = width; 1497 FT_Int sign = 0; 1498 FT_Bool vertical = FT_BOOL( dim == AF_DIMENSION_VERT ); 1499 1500 FT_UNUSED( base_flags ); 1501 FT_UNUSED( stem_flags ); 1502 1503 1504 if ( !AF_LATIN_HINTS_DO_STEM_ADJUST( hints ) ) 1505 return width; 1506 1507 if ( dist < 0 ) 1508 { 1509 dist = -width; 1510 sign = 1; 1511 } 1512 1513 if ( ( vertical && !AF_LATIN_HINTS_DO_VERT_SNAP( hints ) ) || 1514 ( !vertical && !AF_LATIN_HINTS_DO_HORZ_SNAP( hints ) ) ) 1515 { 1516 /* smooth hinting process: very lightly quantize the stem width */ 1517 1518 if ( axis->width_count > 0 ) 1519 { 1520 if ( FT_ABS( dist - axis->widths[0].cur ) < 40 ) 1521 { 1522 dist = axis->widths[0].cur; 1523 if ( dist < 48 ) 1524 dist = 48; 1525 1526 goto Done_Width; 1527 } 1528 } 1529 1530 if ( dist < 54 ) 1531 dist += ( 54 - dist ) / 2 ; 1532 else if ( dist < 3 * 64 ) 1533 { 1534 FT_Pos delta; 1535 1536 1537 delta = dist & 63; 1538 dist &= -64; 1539 1540 if ( delta < 10 ) 1541 dist += delta; 1542 else if ( delta < 22 ) 1543 dist += 10; 1544 else if ( delta < 42 ) 1545 dist += delta; 1546 else if ( delta < 54 ) 1547 dist += 54; 1548 else 1549 dist += delta; 1550 } 1551 } 1552 else 1553 { 1554 /* strong hinting process: snap the stem width to integer pixels */ 1555 1556 dist = af_cjk_snap_width( axis->widths, axis->width_count, dist ); 1557 1558 if ( vertical ) 1559 { 1560 /* in the case of vertical hinting, always round */ 1561 /* the stem heights to integer pixels */ 1562 1563 if ( dist >= 64 ) 1564 dist = ( dist + 16 ) & ~63; 1565 else 1566 dist = 64; 1567 } 1568 else 1569 { 1570 if ( AF_LATIN_HINTS_DO_MONO( hints ) ) 1571 { 1572 /* monochrome horizontal hinting: snap widths to integer pixels */ 1573 /* with a different threshold */ 1574 1575 if ( dist < 64 ) 1576 dist = 64; 1577 else 1578 dist = ( dist + 32 ) & ~63; 1579 } 1580 else 1581 { 1582 /* for horizontal anti-aliased hinting, we adopt a more subtle */ 1583 /* approach: we strengthen small stems, round stems whose size */ 1584 /* is between 1 and 2 pixels to an integer, otherwise nothing */ 1585 1586 if ( dist < 48 ) 1587 dist = ( dist + 64 ) >> 1; 1588 1589 else if ( dist < 128 ) 1590 dist = ( dist + 22 ) & ~63; 1591 else 1592 /* round otherwise to prevent color fringes in LCD mode */ 1593 dist = ( dist + 32 ) & ~63; 1594 } 1595 } 1596 } 1597 1598 Done_Width: 1599 if ( sign ) 1600 dist = -dist; 1601 1602 return dist; 1603 } 1604 1605 1606 /* Align one stem edge relative to the previous stem edge. */ 1607 1608 static void af_cjk_align_linked_edge(AF_GlyphHints hints,AF_Dimension dim,AF_Edge base_edge,AF_Edge stem_edge)1609 af_cjk_align_linked_edge( AF_GlyphHints hints, 1610 AF_Dimension dim, 1611 AF_Edge base_edge, 1612 AF_Edge stem_edge ) 1613 { 1614 FT_Pos dist = stem_edge->opos - base_edge->opos; 1615 1616 FT_Pos fitted_width = af_cjk_compute_stem_width( hints, dim, dist, 1617 base_edge->flags, 1618 stem_edge->flags ); 1619 1620 1621 stem_edge->pos = base_edge->pos + fitted_width; 1622 1623 FT_TRACE5(( " CJKLINK: edge %d @%d (opos=%.2f) linked to %.2f," 1624 " dist was %.2f, now %.2f\n", 1625 stem_edge - hints->axis[dim].edges, stem_edge->fpos, 1626 stem_edge->opos / 64.0, stem_edge->pos / 64.0, 1627 dist / 64.0, fitted_width / 64.0 )); 1628 } 1629 1630 1631 /* Shift the coordinates of the `serif' edge by the same amount */ 1632 /* as the corresponding `base' edge has been moved already. */ 1633 1634 static void af_cjk_align_serif_edge(AF_GlyphHints hints,AF_Edge base,AF_Edge serif)1635 af_cjk_align_serif_edge( AF_GlyphHints hints, 1636 AF_Edge base, 1637 AF_Edge serif ) 1638 { 1639 FT_UNUSED( hints ); 1640 1641 serif->pos = base->pos + ( serif->opos - base->opos ); 1642 } 1643 1644 1645 /*************************************************************************/ 1646 /*************************************************************************/ 1647 /*************************************************************************/ 1648 /**** ****/ 1649 /**** E D G E H I N T I N G ****/ 1650 /**** ****/ 1651 /*************************************************************************/ 1652 /*************************************************************************/ 1653 /*************************************************************************/ 1654 1655 1656 #define AF_LIGHT_MODE_MAX_HORZ_GAP 9 1657 #define AF_LIGHT_MODE_MAX_VERT_GAP 15 1658 #define AF_LIGHT_MODE_MAX_DELTA_ABS 14 1659 1660 1661 static FT_Pos af_hint_normal_stem(AF_GlyphHints hints,AF_Edge edge,AF_Edge edge2,FT_Pos anchor,AF_Dimension dim)1662 af_hint_normal_stem( AF_GlyphHints hints, 1663 AF_Edge edge, 1664 AF_Edge edge2, 1665 FT_Pos anchor, 1666 AF_Dimension dim ) 1667 { 1668 FT_Pos org_len, cur_len, org_center; 1669 FT_Pos cur_pos1, cur_pos2; 1670 FT_Pos d_off1, u_off1, d_off2, u_off2, delta; 1671 FT_Pos offset; 1672 FT_Pos threshold = 64; 1673 1674 1675 if ( !AF_LATIN_HINTS_DO_STEM_ADJUST( hints ) ) 1676 { 1677 if ( ( edge->flags & AF_EDGE_ROUND ) && 1678 ( edge2->flags & AF_EDGE_ROUND ) ) 1679 { 1680 if ( dim == AF_DIMENSION_VERT ) 1681 threshold = 64 - AF_LIGHT_MODE_MAX_HORZ_GAP; 1682 else 1683 threshold = 64 - AF_LIGHT_MODE_MAX_VERT_GAP; 1684 } 1685 else 1686 { 1687 if ( dim == AF_DIMENSION_VERT ) 1688 threshold = 64 - AF_LIGHT_MODE_MAX_HORZ_GAP / 3; 1689 else 1690 threshold = 64 - AF_LIGHT_MODE_MAX_VERT_GAP / 3; 1691 } 1692 } 1693 1694 org_len = edge2->opos - edge->opos; 1695 cur_len = af_cjk_compute_stem_width( hints, dim, org_len, 1696 edge->flags, 1697 edge2->flags ); 1698 1699 org_center = ( edge->opos + edge2->opos ) / 2 + anchor; 1700 cur_pos1 = org_center - cur_len / 2; 1701 cur_pos2 = cur_pos1 + cur_len; 1702 d_off1 = cur_pos1 - FT_PIX_FLOOR( cur_pos1 ); 1703 d_off2 = cur_pos2 - FT_PIX_FLOOR( cur_pos2 ); 1704 u_off1 = 64 - d_off1; 1705 u_off2 = 64 - d_off2; 1706 delta = 0; 1707 1708 1709 if ( d_off1 == 0 || d_off2 == 0 ) 1710 goto Exit; 1711 1712 if ( cur_len <= threshold ) 1713 { 1714 if ( d_off2 < cur_len ) 1715 { 1716 if ( u_off1 <= d_off2 ) 1717 delta = u_off1; 1718 else 1719 delta = -d_off2; 1720 } 1721 1722 goto Exit; 1723 } 1724 1725 if ( threshold < 64 ) 1726 { 1727 if ( d_off1 >= threshold || u_off1 >= threshold || 1728 d_off2 >= threshold || u_off2 >= threshold ) 1729 goto Exit; 1730 } 1731 1732 offset = cur_len & 63; 1733 1734 if ( offset < 32 ) 1735 { 1736 if ( u_off1 <= offset || d_off2 <= offset ) 1737 goto Exit; 1738 } 1739 else 1740 offset = 64 - threshold; 1741 1742 d_off1 = threshold - u_off1; 1743 u_off1 = u_off1 - offset; 1744 u_off2 = threshold - d_off2; 1745 d_off2 = d_off2 - offset; 1746 1747 if ( d_off1 <= u_off1 ) 1748 u_off1 = -d_off1; 1749 1750 if ( d_off2 <= u_off2 ) 1751 u_off2 = -d_off2; 1752 1753 if ( FT_ABS( u_off1 ) <= FT_ABS( u_off2 ) ) 1754 delta = u_off1; 1755 else 1756 delta = u_off2; 1757 1758 Exit: 1759 1760 #if 1 1761 if ( !AF_LATIN_HINTS_DO_STEM_ADJUST( hints ) ) 1762 { 1763 if ( delta > AF_LIGHT_MODE_MAX_DELTA_ABS ) 1764 delta = AF_LIGHT_MODE_MAX_DELTA_ABS; 1765 else if ( delta < -AF_LIGHT_MODE_MAX_DELTA_ABS ) 1766 delta = -AF_LIGHT_MODE_MAX_DELTA_ABS; 1767 } 1768 #endif 1769 1770 cur_pos1 += delta; 1771 1772 if ( edge->opos < edge2->opos ) 1773 { 1774 edge->pos = cur_pos1; 1775 edge2->pos = cur_pos1 + cur_len; 1776 } 1777 else 1778 { 1779 edge->pos = cur_pos1 + cur_len; 1780 edge2->pos = cur_pos1; 1781 } 1782 1783 return delta; 1784 } 1785 1786 1787 /* The main grid-fitting routine. */ 1788 1789 static void af_cjk_hint_edges(AF_GlyphHints hints,AF_Dimension dim)1790 af_cjk_hint_edges( AF_GlyphHints hints, 1791 AF_Dimension dim ) 1792 { 1793 AF_AxisHints axis = &hints->axis[dim]; 1794 AF_Edge edges = axis->edges; 1795 AF_Edge edge_limit = edges + axis->num_edges; 1796 FT_PtrDist n_edges; 1797 AF_Edge edge; 1798 AF_Edge anchor = NULL; 1799 FT_Pos delta = 0; 1800 FT_Int skipped = 0; 1801 FT_Bool has_last_stem = FALSE; 1802 FT_Pos last_stem_pos = 0; 1803 1804 #ifdef FT_DEBUG_LEVEL_TRACE 1805 FT_UInt num_actions = 0; 1806 #endif 1807 1808 1809 FT_TRACE5(( "cjk %s edge hinting (style `%s')\n", 1810 dim == AF_DIMENSION_VERT ? "horizontal" : "vertical", 1811 af_style_names[hints->metrics->style_class->style] )); 1812 1813 /* we begin by aligning all stems relative to the blue zone */ 1814 1815 if ( AF_HINTS_DO_BLUES( hints ) ) 1816 { 1817 for ( edge = edges; edge < edge_limit; edge++ ) 1818 { 1819 AF_Width blue; 1820 AF_Edge edge1, edge2; 1821 1822 1823 if ( edge->flags & AF_EDGE_DONE ) 1824 continue; 1825 1826 blue = edge->blue_edge; 1827 edge1 = NULL; 1828 edge2 = edge->link; 1829 1830 if ( blue ) 1831 { 1832 edge1 = edge; 1833 } 1834 else if ( edge2 && edge2->blue_edge ) 1835 { 1836 blue = edge2->blue_edge; 1837 edge1 = edge2; 1838 edge2 = edge; 1839 } 1840 1841 if ( !edge1 ) 1842 continue; 1843 1844 #ifdef FT_DEBUG_LEVEL_TRACE 1845 FT_TRACE5(( " CJKBLUE: edge %d @%d (opos=%.2f) snapped to %.2f," 1846 " was %.2f\n", 1847 edge1 - edges, edge1->fpos, edge1->opos / 64.0, 1848 blue->fit / 64.0, edge1->pos / 64.0 )); 1849 1850 num_actions++; 1851 #endif 1852 1853 edge1->pos = blue->fit; 1854 edge1->flags |= AF_EDGE_DONE; 1855 1856 if ( edge2 && !edge2->blue_edge ) 1857 { 1858 af_cjk_align_linked_edge( hints, dim, edge1, edge2 ); 1859 edge2->flags |= AF_EDGE_DONE; 1860 1861 #ifdef FT_DEBUG_LEVEL_TRACE 1862 num_actions++; 1863 #endif 1864 } 1865 1866 if ( !anchor ) 1867 anchor = edge; 1868 } 1869 } 1870 1871 /* now we align all stem edges. */ 1872 for ( edge = edges; edge < edge_limit; edge++ ) 1873 { 1874 AF_Edge edge2; 1875 1876 1877 if ( edge->flags & AF_EDGE_DONE ) 1878 continue; 1879 1880 /* skip all non-stem edges */ 1881 edge2 = edge->link; 1882 if ( !edge2 ) 1883 { 1884 skipped++; 1885 continue; 1886 } 1887 1888 /* Some CJK characters have so many stems that 1889 * the hinter is likely to merge two adjacent ones. 1890 * To solve this problem, if either edge of a stem 1891 * is too close to the previous one, we avoid 1892 * aligning the two edges, but rather interpolate 1893 * their locations at the end of this function in 1894 * order to preserve the space between the stems. 1895 */ 1896 if ( has_last_stem && 1897 ( edge->pos < last_stem_pos + 64 || 1898 edge2->pos < last_stem_pos + 64 ) ) 1899 { 1900 skipped++; 1901 continue; 1902 } 1903 1904 /* now align the stem */ 1905 1906 /* this should not happen, but it's better to be safe */ 1907 if ( edge2->blue_edge ) 1908 { 1909 FT_TRACE5(( "ASSERTION FAILED for edge %d\n", edge2-edges )); 1910 1911 af_cjk_align_linked_edge( hints, dim, edge2, edge ); 1912 edge->flags |= AF_EDGE_DONE; 1913 1914 #ifdef FT_DEBUG_LEVEL_TRACE 1915 num_actions++; 1916 #endif 1917 1918 continue; 1919 } 1920 1921 if ( edge2 < edge ) 1922 { 1923 af_cjk_align_linked_edge( hints, dim, edge2, edge ); 1924 edge->flags |= AF_EDGE_DONE; 1925 1926 #ifdef FT_DEBUG_LEVEL_TRACE 1927 num_actions++; 1928 #endif 1929 1930 /* We rarely reaches here it seems; 1931 * usually the two edges belonging 1932 * to one stem are marked as DONE together 1933 */ 1934 has_last_stem = TRUE; 1935 last_stem_pos = edge->pos; 1936 continue; 1937 } 1938 1939 if ( dim != AF_DIMENSION_VERT && !anchor ) 1940 { 1941 1942 #if 0 1943 if ( fixedpitch ) 1944 { 1945 AF_Edge left = edge; 1946 AF_Edge right = edge_limit - 1; 1947 AF_EdgeRec left1, left2, right1, right2; 1948 FT_Pos target, center1, center2; 1949 FT_Pos delta1, delta2, d1, d2; 1950 1951 1952 while ( right > left && !right->link ) 1953 right--; 1954 1955 left1 = *left; 1956 left2 = *left->link; 1957 right1 = *right->link; 1958 right2 = *right; 1959 1960 delta = ( ( ( hinter->pp2.x + 32 ) & -64 ) - hinter->pp2.x ) / 2; 1961 target = left->opos + ( right->opos - left->opos ) / 2 + delta - 16; 1962 1963 delta1 = delta; 1964 delta1 += af_hint_normal_stem( hints, left, left->link, 1965 delta1, 0 ); 1966 1967 if ( left->link != right ) 1968 af_hint_normal_stem( hints, right->link, right, delta1, 0 ); 1969 1970 center1 = left->pos + ( right->pos - left->pos ) / 2; 1971 1972 if ( center1 >= target ) 1973 delta2 = delta - 32; 1974 else 1975 delta2 = delta + 32; 1976 1977 delta2 += af_hint_normal_stem( hints, &left1, &left2, delta2, 0 ); 1978 1979 if ( delta1 != delta2 ) 1980 { 1981 if ( left->link != right ) 1982 af_hint_normal_stem( hints, &right1, &right2, delta2, 0 ); 1983 1984 center2 = left1.pos + ( right2.pos - left1.pos ) / 2; 1985 1986 d1 = center1 - target; 1987 d2 = center2 - target; 1988 1989 if ( FT_ABS( d2 ) < FT_ABS( d1 ) ) 1990 { 1991 left->pos = left1.pos; 1992 left->link->pos = left2.pos; 1993 1994 if ( left->link != right ) 1995 { 1996 right->link->pos = right1.pos; 1997 right->pos = right2.pos; 1998 } 1999 2000 delta1 = delta2; 2001 } 2002 } 2003 2004 delta = delta1; 2005 right->link->flags |= AF_EDGE_DONE; 2006 right->flags |= AF_EDGE_DONE; 2007 } 2008 else 2009 2010 #endif /* 0 */ 2011 2012 delta = af_hint_normal_stem( hints, edge, edge2, 0, 2013 AF_DIMENSION_HORZ ); 2014 } 2015 else 2016 af_hint_normal_stem( hints, edge, edge2, delta, dim ); 2017 2018 #if 0 2019 printf( "stem (%d,%d) adjusted (%.1f,%.1f)\n", 2020 edge - edges, edge2 - edges, 2021 ( edge->pos - edge->opos ) / 64.0, 2022 ( edge2->pos - edge2->opos ) / 64.0 ); 2023 #endif 2024 2025 anchor = edge; 2026 edge->flags |= AF_EDGE_DONE; 2027 edge2->flags |= AF_EDGE_DONE; 2028 has_last_stem = TRUE; 2029 last_stem_pos = edge2->pos; 2030 } 2031 2032 /* make sure that lowercase m's maintain their symmetry */ 2033 2034 /* In general, lowercase m's have six vertical edges if they are sans */ 2035 /* serif, or twelve if they are with serifs. This implementation is */ 2036 /* based on that assumption, and seems to work very well with most */ 2037 /* faces. However, if for a certain face this assumption is not */ 2038 /* true, the m is just rendered like before. In addition, any stem */ 2039 /* correction will only be applied to symmetrical glyphs (even if the */ 2040 /* glyph is not an m), so the potential for unwanted distortion is */ 2041 /* relatively low. */ 2042 2043 /* We don't handle horizontal edges since we can't easily assure that */ 2044 /* the third (lowest) stem aligns with the base line; it might end up */ 2045 /* one pixel higher or lower. */ 2046 2047 n_edges = edge_limit - edges; 2048 if ( dim == AF_DIMENSION_HORZ && ( n_edges == 6 || n_edges == 12 ) ) 2049 { 2050 AF_Edge edge1, edge2, edge3; 2051 FT_Pos dist1, dist2, span; 2052 2053 2054 if ( n_edges == 6 ) 2055 { 2056 edge1 = edges; 2057 edge2 = edges + 2; 2058 edge3 = edges + 4; 2059 } 2060 else 2061 { 2062 edge1 = edges + 1; 2063 edge2 = edges + 5; 2064 edge3 = edges + 9; 2065 } 2066 2067 dist1 = edge2->opos - edge1->opos; 2068 dist2 = edge3->opos - edge2->opos; 2069 2070 span = dist1 - dist2; 2071 if ( span < 0 ) 2072 span = -span; 2073 2074 if ( edge1->link == edge1 + 1 && 2075 edge2->link == edge2 + 1 && 2076 edge3->link == edge3 + 1 && span < 8 ) 2077 { 2078 delta = edge3->pos - ( 2 * edge2->pos - edge1->pos ); 2079 edge3->pos -= delta; 2080 if ( edge3->link ) 2081 edge3->link->pos -= delta; 2082 2083 /* move the serifs along with the stem */ 2084 if ( n_edges == 12 ) 2085 { 2086 ( edges + 8 )->pos -= delta; 2087 ( edges + 11 )->pos -= delta; 2088 } 2089 2090 edge3->flags |= AF_EDGE_DONE; 2091 if ( edge3->link ) 2092 edge3->link->flags |= AF_EDGE_DONE; 2093 } 2094 } 2095 2096 if ( !skipped ) 2097 goto Exit; 2098 2099 /* 2100 * now hint the remaining edges (serifs and single) in order 2101 * to complete our processing 2102 */ 2103 for ( edge = edges; edge < edge_limit; edge++ ) 2104 { 2105 if ( edge->flags & AF_EDGE_DONE ) 2106 continue; 2107 2108 if ( edge->serif ) 2109 { 2110 af_cjk_align_serif_edge( hints, edge->serif, edge ); 2111 edge->flags |= AF_EDGE_DONE; 2112 skipped--; 2113 } 2114 } 2115 2116 if ( !skipped ) 2117 goto Exit; 2118 2119 for ( edge = edges; edge < edge_limit; edge++ ) 2120 { 2121 AF_Edge before, after; 2122 2123 2124 if ( edge->flags & AF_EDGE_DONE ) 2125 continue; 2126 2127 before = after = edge; 2128 2129 while ( --before >= edges ) 2130 if ( before->flags & AF_EDGE_DONE ) 2131 break; 2132 2133 while ( ++after < edge_limit ) 2134 if ( after->flags & AF_EDGE_DONE ) 2135 break; 2136 2137 if ( before >= edges || after < edge_limit ) 2138 { 2139 if ( before < edges ) 2140 af_cjk_align_serif_edge( hints, after, edge ); 2141 else if ( after >= edge_limit ) 2142 af_cjk_align_serif_edge( hints, before, edge ); 2143 else 2144 { 2145 if ( after->fpos == before->fpos ) 2146 edge->pos = before->pos; 2147 else 2148 edge->pos = before->pos + 2149 FT_MulDiv( edge->fpos - before->fpos, 2150 after->pos - before->pos, 2151 after->fpos - before->fpos ); 2152 } 2153 } 2154 } 2155 2156 Exit: 2157 2158 #ifdef FT_DEBUG_LEVEL_TRACE 2159 if ( !num_actions ) 2160 FT_TRACE5(( " (none)\n" )); 2161 FT_TRACE5(( "\n" )); 2162 #endif 2163 2164 return; 2165 } 2166 2167 2168 static void af_cjk_align_edge_points(AF_GlyphHints hints,AF_Dimension dim)2169 af_cjk_align_edge_points( AF_GlyphHints hints, 2170 AF_Dimension dim ) 2171 { 2172 AF_AxisHints axis = & hints->axis[dim]; 2173 AF_Edge edges = axis->edges; 2174 AF_Edge edge_limit = edges + axis->num_edges; 2175 AF_Edge edge; 2176 FT_Bool snapping; 2177 2178 2179 snapping = FT_BOOL( ( dim == AF_DIMENSION_HORZ && 2180 AF_LATIN_HINTS_DO_HORZ_SNAP( hints ) ) || 2181 ( dim == AF_DIMENSION_VERT && 2182 AF_LATIN_HINTS_DO_VERT_SNAP( hints ) ) ); 2183 2184 for ( edge = edges; edge < edge_limit; edge++ ) 2185 { 2186 /* move the points of each segment */ 2187 /* in each edge to the edge's position */ 2188 AF_Segment seg = edge->first; 2189 2190 2191 if ( snapping ) 2192 { 2193 do 2194 { 2195 AF_Point point = seg->first; 2196 2197 2198 for (;;) 2199 { 2200 if ( dim == AF_DIMENSION_HORZ ) 2201 { 2202 point->x = edge->pos; 2203 point->flags |= AF_FLAG_TOUCH_X; 2204 } 2205 else 2206 { 2207 point->y = edge->pos; 2208 point->flags |= AF_FLAG_TOUCH_Y; 2209 } 2210 2211 if ( point == seg->last ) 2212 break; 2213 2214 point = point->next; 2215 } 2216 2217 seg = seg->edge_next; 2218 2219 } while ( seg != edge->first ); 2220 } 2221 else 2222 { 2223 FT_Pos delta = edge->pos - edge->opos; 2224 2225 2226 do 2227 { 2228 AF_Point point = seg->first; 2229 2230 2231 for (;;) 2232 { 2233 if ( dim == AF_DIMENSION_HORZ ) 2234 { 2235 point->x += delta; 2236 point->flags |= AF_FLAG_TOUCH_X; 2237 } 2238 else 2239 { 2240 point->y += delta; 2241 point->flags |= AF_FLAG_TOUCH_Y; 2242 } 2243 2244 if ( point == seg->last ) 2245 break; 2246 2247 point = point->next; 2248 } 2249 2250 seg = seg->edge_next; 2251 2252 } while ( seg != edge->first ); 2253 } 2254 } 2255 } 2256 2257 2258 /* Apply the complete hinting algorithm to a CJK glyph. */ 2259 2260 FT_LOCAL_DEF( FT_Error ) af_cjk_hints_apply(FT_UInt glyph_index,AF_GlyphHints hints,FT_Outline * outline,AF_CJKMetrics metrics)2261 af_cjk_hints_apply( FT_UInt glyph_index, 2262 AF_GlyphHints hints, 2263 FT_Outline* outline, 2264 AF_CJKMetrics metrics ) 2265 { 2266 FT_Error error; 2267 int dim; 2268 2269 FT_UNUSED( metrics ); 2270 FT_UNUSED( glyph_index ); 2271 2272 2273 error = af_glyph_hints_reload( hints, outline ); 2274 if ( error ) 2275 goto Exit; 2276 2277 /* analyze glyph outline */ 2278 #ifdef AF_CONFIG_OPTION_USE_WARPER 2279 if ( ( metrics->root.scaler.render_mode == FT_RENDER_MODE_LIGHT && 2280 AF_HINTS_DO_WARP( hints ) ) || 2281 AF_HINTS_DO_HORIZONTAL( hints ) ) 2282 #else 2283 if ( AF_HINTS_DO_HORIZONTAL( hints ) ) 2284 #endif 2285 { 2286 error = af_cjk_hints_detect_features( hints, AF_DIMENSION_HORZ ); 2287 if ( error ) 2288 goto Exit; 2289 2290 af_cjk_hints_compute_blue_edges( hints, metrics, AF_DIMENSION_HORZ ); 2291 } 2292 2293 if ( AF_HINTS_DO_VERTICAL( hints ) ) 2294 { 2295 error = af_cjk_hints_detect_features( hints, AF_DIMENSION_VERT ); 2296 if ( error ) 2297 goto Exit; 2298 2299 af_cjk_hints_compute_blue_edges( hints, metrics, AF_DIMENSION_VERT ); 2300 } 2301 2302 /* grid-fit the outline */ 2303 for ( dim = 0; dim < AF_DIMENSION_MAX; dim++ ) 2304 { 2305 if ( ( dim == AF_DIMENSION_HORZ && AF_HINTS_DO_HORIZONTAL( hints ) ) || 2306 ( dim == AF_DIMENSION_VERT && AF_HINTS_DO_VERTICAL( hints ) ) ) 2307 { 2308 2309 #ifdef AF_CONFIG_OPTION_USE_WARPER 2310 if ( dim == AF_DIMENSION_HORZ && 2311 metrics->root.scaler.render_mode == FT_RENDER_MODE_LIGHT && 2312 AF_HINTS_DO_WARP( hints ) ) 2313 { 2314 AF_WarperRec warper; 2315 FT_Fixed scale; 2316 FT_Pos delta; 2317 2318 2319 af_warper_compute( &warper, hints, (AF_Dimension)dim, 2320 &scale, &delta ); 2321 af_glyph_hints_scale_dim( hints, (AF_Dimension)dim, 2322 scale, delta ); 2323 continue; 2324 } 2325 #endif /* AF_CONFIG_OPTION_USE_WARPER */ 2326 2327 af_cjk_hint_edges( hints, (AF_Dimension)dim ); 2328 af_cjk_align_edge_points( hints, (AF_Dimension)dim ); 2329 af_glyph_hints_align_strong_points( hints, (AF_Dimension)dim ); 2330 af_glyph_hints_align_weak_points( hints, (AF_Dimension)dim ); 2331 } 2332 } 2333 2334 af_glyph_hints_save( hints, outline ); 2335 2336 Exit: 2337 return error; 2338 } 2339 2340 2341 /*************************************************************************/ 2342 /*************************************************************************/ 2343 /***** *****/ 2344 /***** C J K S C R I P T C L A S S *****/ 2345 /***** *****/ 2346 /*************************************************************************/ 2347 /*************************************************************************/ 2348 2349 2350 AF_DEFINE_WRITING_SYSTEM_CLASS( 2351 af_cjk_writing_system_class, 2352 2353 AF_WRITING_SYSTEM_CJK, 2354 2355 sizeof ( AF_CJKMetricsRec ), 2356 2357 (AF_WritingSystem_InitMetricsFunc) af_cjk_metrics_init, 2358 (AF_WritingSystem_ScaleMetricsFunc)af_cjk_metrics_scale, 2359 (AF_WritingSystem_DoneMetricsFunc) NULL, 2360 (AF_WritingSystem_GetStdWidthsFunc)af_cjk_get_standard_widths, 2361 2362 (AF_WritingSystem_InitHintsFunc) af_cjk_hints_init, 2363 (AF_WritingSystem_ApplyHintsFunc) af_cjk_hints_apply 2364 ) 2365 2366 2367 #else /* !AF_CONFIG_OPTION_CJK */ 2368 2369 2370 AF_DEFINE_WRITING_SYSTEM_CLASS( 2371 af_cjk_writing_system_class, 2372 2373 AF_WRITING_SYSTEM_CJK, 2374 2375 sizeof ( AF_CJKMetricsRec ), 2376 2377 (AF_WritingSystem_InitMetricsFunc) NULL, 2378 (AF_WritingSystem_ScaleMetricsFunc)NULL, 2379 (AF_WritingSystem_DoneMetricsFunc) NULL, 2380 (AF_WritingSystem_GetStdWidthsFunc)NULL, 2381 2382 (AF_WritingSystem_InitHintsFunc) NULL, 2383 (AF_WritingSystem_ApplyHintsFunc) NULL 2384 ) 2385 2386 2387 #endif /* !AF_CONFIG_OPTION_CJK */ 2388 2389 2390 /* END */ 2391