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