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