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