1 /***************************************************************************/ 2 /* */ 3 /* aflatin.c */ 4 /* */ 5 /* Auto-fitter hinting routines for latin writing system (body). */ 6 /* */ 7 /* Copyright 2003-2015 by */ 8 /* David Turner, Robert Wilhelm, and Werner Lemberg. */ 9 /* */ 10 /* This file is part of the FreeType project, and may only be used, */ 11 /* modified, and distributed under the terms of the FreeType project */ 12 /* license, LICENSE.TXT. By continuing to use, modify, or distribute */ 13 /* this file you indicate that you have read the license and */ 14 /* understand and accept it fully. */ 15 /* */ 16 /***************************************************************************/ 17 18 19 #include <ft2build.h> 20 #include FT_ADVANCES_H 21 #include FT_INTERNAL_DEBUG_H 22 23 #include "afglobal.h" 24 #include "afpic.h" 25 #include "aflatin.h" 26 #include "aferrors.h" 27 28 29 #ifdef AF_CONFIG_OPTION_USE_WARPER 30 #include "afwarp.h" 31 #endif 32 33 34 /*************************************************************************/ 35 /* */ 36 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */ 37 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */ 38 /* messages during execution. */ 39 /* */ 40 #undef FT_COMPONENT 41 #define FT_COMPONENT trace_aflatin 42 43 44 /*************************************************************************/ 45 /*************************************************************************/ 46 /***** *****/ 47 /***** L A T I N G L O B A L M E T R I C S *****/ 48 /***** *****/ 49 /*************************************************************************/ 50 /*************************************************************************/ 51 52 53 /* Find segments and links, compute all stem widths, and initialize */ 54 /* standard width and height for the glyph with given charcode. */ 55 56 FT_LOCAL_DEF( void ) 57 af_latin_metrics_init_widths( AF_LatinMetrics metrics, 58 FT_Face face ) 59 { 60 /* scan the array of segments in each direction */ 61 AF_GlyphHintsRec hints[1]; 62 63 64 FT_TRACE5(( "\n" 65 "latin standard widths computation (style `%s')\n" 66 "=====================================================\n" 67 "\n", 68 af_style_names[metrics->root.style_class->style] )); 69 70 af_glyph_hints_init( hints, face->memory ); 71 72 metrics->axis[AF_DIMENSION_HORZ].width_count = 0; 73 metrics->axis[AF_DIMENSION_VERT].width_count = 0; 74 75 { 76 FT_Error error; 77 FT_ULong glyph_index; 78 FT_Long y_offset; 79 int dim; 80 AF_LatinMetricsRec dummy[1]; 81 AF_Scaler scaler = &dummy->root.scaler; 82 83 #ifdef FT_CONFIG_OPTION_PIC 84 AF_FaceGlobals globals = metrics->root.globals; 85 #endif 86 87 AF_StyleClass style_class = metrics->root.style_class; 88 AF_ScriptClass script_class = AF_SCRIPT_CLASSES_GET 89 [style_class->script]; 90 91 FT_UInt32 standard_char; 92 93 94 /* 95 * We check more than a single standard character to catch features 96 * like `c2sc' (small caps from caps) that don't contain lowercase 97 * letters by definition, or other features that mainly operate on 98 * numerals. 99 */ 100 101 standard_char = script_class->standard_char1; 102 af_get_char_index( &metrics->root, 103 standard_char, 104 &glyph_index, 105 &y_offset ); 106 if ( !glyph_index ) 107 { 108 if ( script_class->standard_char2 ) 109 { 110 standard_char = script_class->standard_char2; 111 af_get_char_index( &metrics->root, 112 standard_char, 113 &glyph_index, 114 &y_offset ); 115 if ( !glyph_index ) 116 { 117 if ( script_class->standard_char3 ) 118 { 119 standard_char = script_class->standard_char3; 120 af_get_char_index( &metrics->root, 121 standard_char, 122 &glyph_index, 123 &y_offset ); 124 if ( !glyph_index ) 125 goto Exit; 126 } 127 else 128 goto Exit; 129 } 130 } 131 else 132 goto Exit; 133 } 134 135 FT_TRACE5(( "standard character: U+%04lX (glyph index %d)\n", 136 standard_char, glyph_index )); 137 138 error = FT_Load_Glyph( face, glyph_index, FT_LOAD_NO_SCALE ); 139 if ( error || face->glyph->outline.n_points <= 0 ) 140 goto Exit; 141 142 FT_ZERO( dummy ); 143 144 dummy->units_per_em = metrics->units_per_em; 145 146 scaler->x_scale = 0x10000L; 147 scaler->y_scale = 0x10000L; 148 scaler->x_delta = 0; 149 scaler->y_delta = 0; 150 151 scaler->face = face; 152 scaler->render_mode = FT_RENDER_MODE_NORMAL; 153 scaler->flags = 0; 154 155 af_glyph_hints_rescale( hints, (AF_StyleMetrics)dummy ); 156 157 error = af_glyph_hints_reload( hints, &face->glyph->outline ); 158 if ( error ) 159 goto Exit; 160 161 for ( dim = 0; dim < AF_DIMENSION_MAX; dim++ ) 162 { 163 AF_LatinAxis axis = &metrics->axis[dim]; 164 AF_AxisHints axhints = &hints->axis[dim]; 165 AF_Segment seg, limit, link; 166 FT_UInt num_widths = 0; 167 168 169 error = af_latin_hints_compute_segments( hints, 170 (AF_Dimension)dim ); 171 if ( error ) 172 goto Exit; 173 174 /* 175 * We assume that the glyphs selected for the stem width 176 * computation are `featureless' enough so that the linking 177 * algorithm works fine without adjustments of its scoring 178 * function. 179 */ 180 af_latin_hints_link_segments( hints, 181 0, 182 NULL, 183 (AF_Dimension)dim ); 184 185 seg = axhints->segments; 186 limit = seg + axhints->num_segments; 187 188 for ( ; seg < limit; seg++ ) 189 { 190 link = seg->link; 191 192 /* we only consider stem segments there! */ 193 if ( link && link->link == seg && link > seg ) 194 { 195 FT_Pos dist; 196 197 198 dist = seg->pos - link->pos; 199 if ( dist < 0 ) 200 dist = -dist; 201 202 if ( num_widths < AF_LATIN_MAX_WIDTHS ) 203 axis->widths[num_widths++].org = dist; 204 } 205 } 206 207 /* this also replaces multiple almost identical stem widths */ 208 /* with a single one (the value 100 is heuristic) */ 209 af_sort_and_quantize_widths( &num_widths, axis->widths, 210 dummy->units_per_em / 100 ); 211 axis->width_count = num_widths; 212 } 213 214 Exit: 215 for ( dim = 0; dim < AF_DIMENSION_MAX; dim++ ) 216 { 217 AF_LatinAxis axis = &metrics->axis[dim]; 218 FT_Pos stdw; 219 220 221 stdw = ( axis->width_count > 0 ) ? axis->widths[0].org 222 : AF_LATIN_CONSTANT( metrics, 50 ); 223 224 /* let's try 20% of the smallest width */ 225 axis->edge_distance_threshold = stdw / 5; 226 axis->standard_width = stdw; 227 axis->extra_light = 0; 228 229 #ifdef FT_DEBUG_LEVEL_TRACE 230 { 231 FT_UInt i; 232 233 234 FT_TRACE5(( "%s widths:\n", 235 dim == AF_DIMENSION_VERT ? "horizontal" 236 : "vertical" )); 237 238 FT_TRACE5(( " %d (standard)", axis->standard_width )); 239 for ( i = 1; i < axis->width_count; i++ ) 240 FT_TRACE5(( " %d", axis->widths[i].org )); 241 242 FT_TRACE5(( "\n" )); 243 } 244 #endif 245 } 246 } 247 248 FT_TRACE5(( "\n" )); 249 250 af_glyph_hints_done( hints ); 251 } 252 253 254 /* Find all blue zones. Flat segments give the reference points, */ 255 /* round segments the overshoot positions. */ 256 257 static void 258 af_latin_metrics_init_blues( AF_LatinMetrics metrics, 259 FT_Face face ) 260 { 261 FT_Pos flats [AF_BLUE_STRING_MAX_LEN]; 262 FT_Pos rounds[AF_BLUE_STRING_MAX_LEN]; 263 264 FT_UInt num_flats; 265 FT_UInt num_rounds; 266 267 AF_LatinBlue blue; 268 FT_Error error; 269 AF_LatinAxis axis = &metrics->axis[AF_DIMENSION_VERT]; 270 FT_Outline outline; 271 272 AF_StyleClass sc = metrics->root.style_class; 273 274 AF_Blue_Stringset bss = sc->blue_stringset; 275 const AF_Blue_StringRec* bs = &af_blue_stringsets[bss]; 276 277 278 /* we walk over the blue character strings as specified in the */ 279 /* style's entry in the `af_blue_stringset' array */ 280 281 FT_TRACE5(( "latin blue zones computation\n" 282 "============================\n" 283 "\n" )); 284 285 for ( ; bs->string != AF_BLUE_STRING_MAX; bs++ ) 286 { 287 const char* p = &af_blue_strings[bs->string]; 288 FT_Pos* blue_ref; 289 FT_Pos* blue_shoot; 290 291 292 #ifdef FT_DEBUG_LEVEL_TRACE 293 { 294 FT_Bool have_flag = 0; 295 296 297 FT_TRACE5(( "blue zone %d", axis->blue_count )); 298 299 if ( bs->properties ) 300 { 301 FT_TRACE5(( " (" )); 302 303 if ( AF_LATIN_IS_TOP_BLUE( bs ) ) 304 { 305 FT_TRACE5(( "top" )); 306 have_flag = 1; 307 } 308 309 if ( AF_LATIN_IS_NEUTRAL_BLUE( bs ) ) 310 { 311 if ( have_flag ) 312 FT_TRACE5(( ", " )); 313 FT_TRACE5(( "neutral" )); 314 have_flag = 1; 315 } 316 317 if ( AF_LATIN_IS_X_HEIGHT_BLUE( bs ) ) 318 { 319 if ( have_flag ) 320 FT_TRACE5(( ", " )); 321 FT_TRACE5(( "small top" )); 322 have_flag = 1; 323 } 324 325 if ( AF_LATIN_IS_LONG_BLUE( bs ) ) 326 { 327 if ( have_flag ) 328 FT_TRACE5(( ", " )); 329 FT_TRACE5(( "long" )); 330 } 331 332 FT_TRACE5(( ")" )); 333 } 334 335 FT_TRACE5(( ":\n" )); 336 } 337 #endif /* FT_DEBUG_LEVEL_TRACE */ 338 339 num_flats = 0; 340 num_rounds = 0; 341 342 while ( *p ) 343 { 344 FT_ULong ch; 345 FT_ULong glyph_index; 346 FT_Long y_offset; 347 FT_Pos best_y; /* same as points.y */ 348 FT_Int best_point, best_contour_first, best_contour_last; 349 FT_Vector* points; 350 FT_Bool round = 0; 351 352 353 GET_UTF8_CHAR( ch, p ); 354 355 /* load the character in the face -- skip unknown or empty ones */ 356 af_get_char_index( &metrics->root, ch, &glyph_index, &y_offset ); 357 if ( glyph_index == 0 ) 358 { 359 FT_TRACE5(( " U+%04lX unavailable\n", ch )); 360 continue; 361 } 362 363 error = FT_Load_Glyph( face, glyph_index, FT_LOAD_NO_SCALE ); 364 outline = face->glyph->outline; 365 /* reject glyphs that don't produce any rendering */ 366 if ( error || outline.n_points <= 2 ) 367 { 368 FT_TRACE5(( " U+%04lX contains no (usable) outlines\n", ch )); 369 continue; 370 } 371 372 /* now compute min or max point indices and coordinates */ 373 points = outline.points; 374 best_point = -1; 375 best_y = 0; /* make compiler happy */ 376 best_contour_first = 0; /* ditto */ 377 best_contour_last = 0; /* ditto */ 378 379 { 380 FT_Int nn; 381 FT_Int first = 0; 382 FT_Int last = -1; 383 384 385 for ( nn = 0; nn < outline.n_contours; first = last + 1, nn++ ) 386 { 387 FT_Int old_best_point = best_point; 388 FT_Int pp; 389 390 391 last = outline.contours[nn]; 392 393 /* Avoid single-point contours since they are never rasterized. */ 394 /* In some fonts, they correspond to mark attachment points */ 395 /* that are way outside of the glyph's real outline. */ 396 if ( last <= first ) 397 continue; 398 399 if ( AF_LATIN_IS_TOP_BLUE( bs ) ) 400 { 401 for ( pp = first; pp <= last; pp++ ) 402 if ( best_point < 0 || points[pp].y > best_y ) 403 { 404 best_point = pp; 405 best_y = points[pp].y; 406 } 407 } 408 else 409 { 410 for ( pp = first; pp <= last; pp++ ) 411 if ( best_point < 0 || points[pp].y < best_y ) 412 { 413 best_point = pp; 414 best_y = points[pp].y; 415 } 416 } 417 418 if ( best_point != old_best_point ) 419 { 420 best_contour_first = first; 421 best_contour_last = last; 422 } 423 } 424 } 425 426 /* now check whether the point belongs to a straight or round */ 427 /* segment; we first need to find in which contour the extremum */ 428 /* lies, then inspect its previous and next points */ 429 if ( best_point >= 0 ) 430 { 431 FT_Pos best_x = points[best_point].x; 432 FT_Int prev, next; 433 FT_Int best_segment_first, best_segment_last; 434 FT_Int best_on_point_first, best_on_point_last; 435 FT_Pos dist; 436 437 438 best_segment_first = best_point; 439 best_segment_last = best_point; 440 441 if ( FT_CURVE_TAG( outline.tags[best_point] ) == FT_CURVE_TAG_ON ) 442 { 443 best_on_point_first = best_point; 444 best_on_point_last = best_point; 445 } 446 else 447 { 448 best_on_point_first = -1; 449 best_on_point_last = -1; 450 } 451 452 /* look for the previous and next points on the contour */ 453 /* that are not on the same Y coordinate, then threshold */ 454 /* the `closeness'... */ 455 prev = best_point; 456 next = prev; 457 458 do 459 { 460 if ( prev > best_contour_first ) 461 prev--; 462 else 463 prev = best_contour_last; 464 465 dist = FT_ABS( points[prev].y - best_y ); 466 /* accept a small distance or a small angle (both values are */ 467 /* heuristic; value 20 corresponds to approx. 2.9 degrees) */ 468 if ( dist > 5 ) 469 if ( FT_ABS( points[prev].x - best_x ) <= 20 * dist ) 470 break; 471 472 best_segment_first = prev; 473 474 if ( FT_CURVE_TAG( outline.tags[prev] ) == FT_CURVE_TAG_ON ) 475 { 476 best_on_point_first = prev; 477 if ( best_on_point_last < 0 ) 478 best_on_point_last = prev; 479 } 480 481 } while ( prev != best_point ); 482 483 do 484 { 485 if ( next < best_contour_last ) 486 next++; 487 else 488 next = best_contour_first; 489 490 dist = FT_ABS( points[next].y - best_y ); 491 if ( dist > 5 ) 492 if ( FT_ABS( points[next].x - best_x ) <= 20 * dist ) 493 break; 494 495 best_segment_last = next; 496 497 if ( FT_CURVE_TAG( outline.tags[next] ) == FT_CURVE_TAG_ON ) 498 { 499 best_on_point_last = next; 500 if ( best_on_point_first < 0 ) 501 best_on_point_first = next; 502 } 503 504 } while ( next != best_point ); 505 506 if ( AF_LATIN_IS_LONG_BLUE( bs ) ) 507 { 508 /* If this flag is set, we have an additional constraint to */ 509 /* get the blue zone distance: Find a segment of the topmost */ 510 /* (or bottommost) contour that is longer than a heuristic */ 511 /* threshold. This ensures that small bumps in the outline */ 512 /* are ignored (for example, the `vertical serifs' found in */ 513 /* many Hebrew glyph designs). */ 514 515 /* If this segment is long enough, we are done. Otherwise, */ 516 /* search the segment next to the extremum that is long */ 517 /* enough, has the same direction, and a not too large */ 518 /* vertical distance from the extremum. Note that the */ 519 /* algorithm doesn't check whether the found segment is */ 520 /* actually the one (vertically) nearest to the extremum. */ 521 522 /* heuristic threshold value */ 523 FT_Pos length_threshold = metrics->units_per_em / 25; 524 525 526 dist = FT_ABS( points[best_segment_last].x - 527 points[best_segment_first].x ); 528 529 if ( dist < length_threshold && 530 best_segment_last - best_segment_first + 2 <= 531 best_contour_last - best_contour_first ) 532 { 533 /* heuristic threshold value */ 534 FT_Pos height_threshold = metrics->units_per_em / 4; 535 536 FT_Int first; 537 FT_Int last; 538 FT_Bool hit; 539 540 /* we intentionally declare these two variables */ 541 /* outside of the loop since various compilers emit */ 542 /* incorrect warning messages otherwise, talking about */ 543 /* `possibly uninitialized variables' */ 544 FT_Int p_first = 0; /* make compiler happy */ 545 FT_Int p_last = 0; 546 547 FT_Bool left2right; 548 549 550 /* compute direction */ 551 prev = best_point; 552 553 do 554 { 555 if ( prev > best_contour_first ) 556 prev--; 557 else 558 prev = best_contour_last; 559 560 if ( points[prev].x != best_x ) 561 break; 562 563 } while ( prev != best_point ); 564 565 /* skip glyph for the degenerate case */ 566 if ( prev == best_point ) 567 continue; 568 569 left2right = FT_BOOL( points[prev].x < points[best_point].x ); 570 571 first = best_segment_last; 572 last = first; 573 hit = 0; 574 575 do 576 { 577 FT_Bool l2r; 578 FT_Pos d; 579 580 581 if ( !hit ) 582 { 583 /* no hit; adjust first point */ 584 first = last; 585 586 /* also adjust first and last on point */ 587 if ( FT_CURVE_TAG( outline.tags[first] ) == 588 FT_CURVE_TAG_ON ) 589 { 590 p_first = first; 591 p_last = first; 592 } 593 else 594 { 595 p_first = -1; 596 p_last = -1; 597 } 598 599 hit = 1; 600 } 601 602 if ( last < best_contour_last ) 603 last++; 604 else 605 last = best_contour_first; 606 607 if ( FT_ABS( best_y - points[first].y ) > height_threshold ) 608 { 609 /* vertical distance too large */ 610 hit = 0; 611 continue; 612 } 613 614 /* same test as above */ 615 dist = FT_ABS( points[last].y - points[first].y ); 616 if ( dist > 5 ) 617 if ( FT_ABS( points[last].x - points[first].x ) <= 618 20 * dist ) 619 { 620 hit = 0; 621 continue; 622 } 623 624 if ( FT_CURVE_TAG( outline.tags[last] ) == FT_CURVE_TAG_ON ) 625 { 626 p_last = last; 627 if ( p_first < 0 ) 628 p_first = last; 629 } 630 631 l2r = FT_BOOL( points[first].x < points[last].x ); 632 d = FT_ABS( points[last].x - points[first].x ); 633 634 if ( l2r == left2right && 635 d >= length_threshold ) 636 { 637 /* all constraints are met; update segment after finding */ 638 /* its end */ 639 do 640 { 641 if ( last < best_contour_last ) 642 last++; 643 else 644 last = best_contour_first; 645 646 d = FT_ABS( points[last].y - points[first].y ); 647 if ( d > 5 ) 648 if ( FT_ABS( points[next].x - points[first].x ) <= 649 20 * dist ) 650 { 651 if ( last > best_contour_first ) 652 last--; 653 else 654 last = best_contour_last; 655 break; 656 } 657 658 p_last = last; 659 660 if ( FT_CURVE_TAG( outline.tags[last] ) == 661 FT_CURVE_TAG_ON ) 662 { 663 p_last = last; 664 if ( p_first < 0 ) 665 p_first = last; 666 } 667 668 } while ( last != best_segment_first ); 669 670 best_y = points[first].y; 671 672 best_segment_first = first; 673 best_segment_last = last; 674 675 best_on_point_first = p_first; 676 best_on_point_last = p_last; 677 678 break; 679 } 680 681 } while ( last != best_segment_first ); 682 } 683 } 684 685 /* for computing blue zones, we add the y offset as returned */ 686 /* by the currently used OpenType feature -- for example, */ 687 /* superscript glyphs might be identical to subscript glyphs */ 688 /* with a vertical shift */ 689 best_y += y_offset; 690 691 FT_TRACE5(( " U+%04lX: best_y = %5ld", ch, best_y )); 692 693 /* now set the `round' flag depending on the segment's kind: */ 694 /* */ 695 /* - if the horizontal distance between the first and last */ 696 /* `on' point is larger than upem/8 (value 8 is heuristic) */ 697 /* we have a flat segment */ 698 /* - if either the first or the last point of the segment is */ 699 /* an `off' point, the segment is round, otherwise it is */ 700 /* flat */ 701 if ( best_on_point_first >= 0 && 702 best_on_point_last >= 0 && 703 (FT_UInt)( FT_ABS( points[best_on_point_last].x - 704 points[best_on_point_first].x ) ) > 705 metrics->units_per_em / 8 ) 706 round = 0; 707 else 708 round = FT_BOOL( 709 FT_CURVE_TAG( outline.tags[best_segment_first] ) != 710 FT_CURVE_TAG_ON || 711 FT_CURVE_TAG( outline.tags[best_segment_last] ) != 712 FT_CURVE_TAG_ON ); 713 714 if ( round && AF_LATIN_IS_NEUTRAL_BLUE( bs ) ) 715 { 716 /* only use flat segments for a neutral blue zone */ 717 FT_TRACE5(( " (round, skipped)\n" )); 718 continue; 719 } 720 721 FT_TRACE5(( " (%s)\n", round ? "round" : "flat" )); 722 } 723 724 if ( round ) 725 rounds[num_rounds++] = best_y; 726 else 727 flats[num_flats++] = best_y; 728 } 729 730 if ( num_flats == 0 && num_rounds == 0 ) 731 { 732 /* 733 * we couldn't find a single glyph to compute this blue zone, 734 * we will simply ignore it then 735 */ 736 FT_TRACE5(( " empty\n" )); 737 continue; 738 } 739 740 /* we have computed the contents of the `rounds' and `flats' tables, */ 741 /* now determine the reference and overshoot position of the blue -- */ 742 /* we simply take the median value after a simple sort */ 743 af_sort_pos( num_rounds, rounds ); 744 af_sort_pos( num_flats, flats ); 745 746 blue = &axis->blues[axis->blue_count]; 747 blue_ref = &blue->ref.org; 748 blue_shoot = &blue->shoot.org; 749 750 axis->blue_count++; 751 752 if ( num_flats == 0 ) 753 { 754 *blue_ref = 755 *blue_shoot = rounds[num_rounds / 2]; 756 } 757 else if ( num_rounds == 0 ) 758 { 759 *blue_ref = 760 *blue_shoot = flats[num_flats / 2]; 761 } 762 else 763 { 764 *blue_ref = flats [num_flats / 2]; 765 *blue_shoot = rounds[num_rounds / 2]; 766 } 767 768 /* there are sometimes problems: if the overshoot position of top */ 769 /* zones is under its reference position, or the opposite for bottom */ 770 /* zones. We must thus check everything there and correct the errors */ 771 if ( *blue_shoot != *blue_ref ) 772 { 773 FT_Pos ref = *blue_ref; 774 FT_Pos shoot = *blue_shoot; 775 FT_Bool over_ref = FT_BOOL( shoot > ref ); 776 777 778 if ( AF_LATIN_IS_TOP_BLUE( bs ) ^ over_ref ) 779 { 780 *blue_ref = 781 *blue_shoot = ( shoot + ref ) / 2; 782 783 FT_TRACE5(( " [overshoot smaller than reference," 784 " taking mean value]\n" )); 785 } 786 } 787 788 blue->flags = 0; 789 if ( AF_LATIN_IS_TOP_BLUE( bs ) ) 790 blue->flags |= AF_LATIN_BLUE_TOP; 791 if ( AF_LATIN_IS_NEUTRAL_BLUE( bs ) ) 792 blue->flags |= AF_LATIN_BLUE_NEUTRAL; 793 794 /* 795 * The following flag is used later to adjust the y and x scales 796 * in order to optimize the pixel grid alignment of the top of small 797 * letters. 798 */ 799 if ( AF_LATIN_IS_X_HEIGHT_BLUE( bs ) ) 800 blue->flags |= AF_LATIN_BLUE_ADJUSTMENT; 801 802 FT_TRACE5(( " -> reference = %ld\n" 803 " overshoot = %ld\n", 804 *blue_ref, *blue_shoot )); 805 } 806 807 FT_TRACE5(( "\n" )); 808 809 return; 810 } 811 812 813 /* Check whether all ASCII digits have the same advance width. */ 814 815 FT_LOCAL_DEF( void ) 816 af_latin_metrics_check_digits( AF_LatinMetrics metrics, 817 FT_Face face ) 818 { 819 FT_UInt i; 820 FT_Bool started = 0, same_width = 1; 821 FT_Fixed advance, old_advance = 0; 822 823 824 /* digit `0' is 0x30 in all supported charmaps */ 825 for ( i = 0x30; i <= 0x39; i++ ) 826 { 827 FT_ULong glyph_index; 828 FT_Long y_offset; 829 830 831 af_get_char_index( &metrics->root, i, &glyph_index, &y_offset ); 832 if ( glyph_index == 0 ) 833 continue; 834 835 if ( FT_Get_Advance( face, glyph_index, 836 FT_LOAD_NO_SCALE | 837 FT_LOAD_NO_HINTING | 838 FT_LOAD_IGNORE_TRANSFORM, 839 &advance ) ) 840 continue; 841 842 if ( started ) 843 { 844 if ( advance != old_advance ) 845 { 846 same_width = 0; 847 break; 848 } 849 } 850 else 851 { 852 old_advance = advance; 853 started = 1; 854 } 855 } 856 857 metrics->root.digits_have_same_width = same_width; 858 } 859 860 861 /* Initialize global metrics. */ 862 863 FT_LOCAL_DEF( FT_Error ) 864 af_latin_metrics_init( AF_LatinMetrics metrics, 865 FT_Face face ) 866 { 867 FT_CharMap oldmap = face->charmap; 868 869 870 metrics->units_per_em = face->units_per_EM; 871 872 if ( !FT_Select_Charmap( face, FT_ENCODING_UNICODE ) ) 873 { 874 af_latin_metrics_init_widths( metrics, face ); 875 af_latin_metrics_init_blues( metrics, face ); 876 af_latin_metrics_check_digits( metrics, face ); 877 } 878 879 FT_Set_Charmap( face, oldmap ); 880 return FT_Err_Ok; 881 } 882 883 884 /* Adjust scaling value, then scale and shift widths */ 885 /* and blue zones (if applicable) for given dimension. */ 886 887 static void 888 af_latin_metrics_scale_dim( AF_LatinMetrics metrics, 889 AF_Scaler scaler, 890 AF_Dimension dim ) 891 { 892 FT_Fixed scale; 893 FT_Pos delta; 894 AF_LatinAxis axis; 895 FT_UInt nn; 896 897 898 if ( dim == AF_DIMENSION_HORZ ) 899 { 900 scale = scaler->x_scale; 901 delta = scaler->x_delta; 902 } 903 else 904 { 905 scale = scaler->y_scale; 906 delta = scaler->y_delta; 907 } 908 909 axis = &metrics->axis[dim]; 910 911 if ( axis->org_scale == scale && axis->org_delta == delta ) 912 return; 913 914 axis->org_scale = scale; 915 axis->org_delta = delta; 916 917 /* 918 * correct X and Y scale to optimize the alignment of the top of small 919 * letters to the pixel grid 920 */ 921 { 922 AF_LatinAxis Axis = &metrics->axis[AF_DIMENSION_VERT]; 923 AF_LatinBlue blue = NULL; 924 925 926 for ( nn = 0; nn < Axis->blue_count; nn++ ) 927 { 928 if ( Axis->blues[nn].flags & AF_LATIN_BLUE_ADJUSTMENT ) 929 { 930 blue = &Axis->blues[nn]; 931 break; 932 } 933 } 934 935 if ( blue ) 936 { 937 FT_Pos scaled; 938 FT_Pos threshold; 939 FT_Pos fitted; 940 FT_UInt limit; 941 FT_UInt ppem; 942 943 944 scaled = FT_MulFix( blue->shoot.org, scaler->y_scale ); 945 ppem = metrics->root.scaler.face->size->metrics.x_ppem; 946 limit = metrics->root.globals->increase_x_height; 947 threshold = 40; 948 949 /* if the `increase-x-height' property is active, */ 950 /* we round up much more often */ 951 if ( limit && 952 ppem <= limit && 953 ppem >= AF_PROP_INCREASE_X_HEIGHT_MIN ) 954 threshold = 52; 955 956 fitted = ( scaled + threshold ) & ~63; 957 958 if ( scaled != fitted ) 959 { 960 #if 0 961 if ( dim == AF_DIMENSION_HORZ ) 962 { 963 if ( fitted < scaled ) 964 scale -= scale / 50; /* scale *= 0.98 */ 965 } 966 else 967 #endif 968 if ( dim == AF_DIMENSION_VERT ) 969 { 970 scale = FT_MulDiv( scale, fitted, scaled ); 971 972 FT_TRACE5(( 973 "af_latin_metrics_scale_dim:" 974 " x height alignment (style `%s'):\n" 975 " " 976 " vertical scaling changed from %.4f to %.4f (by %d%%)\n" 977 "\n", 978 af_style_names[metrics->root.style_class->style], 979 axis->org_scale / 65536.0, 980 scale / 65536.0, 981 ( fitted - scaled ) * 100 / scaled )); 982 } 983 } 984 } 985 } 986 987 axis->scale = scale; 988 axis->delta = delta; 989 990 if ( dim == AF_DIMENSION_HORZ ) 991 { 992 metrics->root.scaler.x_scale = scale; 993 metrics->root.scaler.x_delta = delta; 994 } 995 else 996 { 997 metrics->root.scaler.y_scale = scale; 998 metrics->root.scaler.y_delta = delta; 999 } 1000 1001 FT_TRACE5(( "%s widths (style `%s')\n", 1002 dim == AF_DIMENSION_HORZ ? "horizontal" : "vertical", 1003 af_style_names[metrics->root.style_class->style] )); 1004 1005 /* scale the widths */ 1006 for ( nn = 0; nn < axis->width_count; nn++ ) 1007 { 1008 AF_Width width = axis->widths + nn; 1009 1010 1011 width->cur = FT_MulFix( width->org, scale ); 1012 width->fit = width->cur; 1013 1014 FT_TRACE5(( " %d scaled to %.2f\n", 1015 width->org, 1016 width->cur / 64.0 )); 1017 } 1018 1019 FT_TRACE5(( "\n" )); 1020 1021 /* an extra-light axis corresponds to a standard width that is */ 1022 /* smaller than 5/8 pixels */ 1023 axis->extra_light = 1024 (FT_Bool)( FT_MulFix( axis->standard_width, scale ) < 32 + 8 ); 1025 1026 #ifdef FT_DEBUG_LEVEL_TRACE 1027 if ( axis->extra_light ) 1028 FT_TRACE5(( "`%s' style is extra light (at current resolution)\n" 1029 "\n", 1030 af_style_names[metrics->root.style_class->style] )); 1031 #endif 1032 1033 if ( dim == AF_DIMENSION_VERT ) 1034 { 1035 FT_TRACE5(( "blue zones (style `%s')\n", 1036 af_style_names[metrics->root.style_class->style] )); 1037 1038 /* scale the blue zones */ 1039 for ( nn = 0; nn < axis->blue_count; nn++ ) 1040 { 1041 AF_LatinBlue blue = &axis->blues[nn]; 1042 FT_Pos dist; 1043 1044 1045 blue->ref.cur = FT_MulFix( blue->ref.org, scale ) + delta; 1046 blue->ref.fit = blue->ref.cur; 1047 blue->shoot.cur = FT_MulFix( blue->shoot.org, scale ) + delta; 1048 blue->shoot.fit = blue->shoot.cur; 1049 blue->flags &= ~AF_LATIN_BLUE_ACTIVE; 1050 1051 /* a blue zone is only active if it is less than 3/4 pixels tall */ 1052 dist = FT_MulFix( blue->ref.org - blue->shoot.org, scale ); 1053 if ( dist <= 48 && dist >= -48 ) 1054 { 1055 #if 0 1056 FT_Pos delta1; 1057 #endif 1058 FT_Pos delta2; 1059 1060 1061 /* use discrete values for blue zone widths */ 1062 1063 #if 0 1064 1065 /* generic, original code */ 1066 delta1 = blue->shoot.org - blue->ref.org; 1067 delta2 = delta1; 1068 if ( delta1 < 0 ) 1069 delta2 = -delta2; 1070 1071 delta2 = FT_MulFix( delta2, scale ); 1072 1073 if ( delta2 < 32 ) 1074 delta2 = 0; 1075 else if ( delta2 < 64 ) 1076 delta2 = 32 + ( ( ( delta2 - 32 ) + 16 ) & ~31 ); 1077 else 1078 delta2 = FT_PIX_ROUND( delta2 ); 1079 1080 if ( delta1 < 0 ) 1081 delta2 = -delta2; 1082 1083 blue->ref.fit = FT_PIX_ROUND( blue->ref.cur ); 1084 blue->shoot.fit = blue->ref.fit + delta2; 1085 1086 #else 1087 1088 /* simplified version due to abs(dist) <= 48 */ 1089 delta2 = dist; 1090 if ( dist < 0 ) 1091 delta2 = -delta2; 1092 1093 if ( delta2 < 32 ) 1094 delta2 = 0; 1095 else if ( delta2 < 48 ) 1096 delta2 = 32; 1097 else 1098 delta2 = 64; 1099 1100 if ( dist < 0 ) 1101 delta2 = -delta2; 1102 1103 blue->ref.fit = FT_PIX_ROUND( blue->ref.cur ); 1104 blue->shoot.fit = blue->ref.fit - delta2; 1105 1106 #endif 1107 1108 blue->flags |= AF_LATIN_BLUE_ACTIVE; 1109 1110 FT_TRACE5(( " reference %d: %d scaled to %.2f%s\n" 1111 " overshoot %d: %d scaled to %.2f%s\n", 1112 nn, 1113 blue->ref.org, 1114 blue->ref.fit / 64.0, 1115 blue->flags & AF_LATIN_BLUE_ACTIVE ? "" 1116 : " (inactive)", 1117 nn, 1118 blue->shoot.org, 1119 blue->shoot.fit / 64.0, 1120 blue->flags & AF_LATIN_BLUE_ACTIVE ? "" 1121 : " (inactive)" )); 1122 } 1123 } 1124 } 1125 } 1126 1127 1128 /* Scale global values in both directions. */ 1129 1130 FT_LOCAL_DEF( void ) 1131 af_latin_metrics_scale( AF_LatinMetrics metrics, 1132 AF_Scaler scaler ) 1133 { 1134 metrics->root.scaler.render_mode = scaler->render_mode; 1135 metrics->root.scaler.face = scaler->face; 1136 metrics->root.scaler.flags = scaler->flags; 1137 1138 af_latin_metrics_scale_dim( metrics, scaler, AF_DIMENSION_HORZ ); 1139 af_latin_metrics_scale_dim( metrics, scaler, AF_DIMENSION_VERT ); 1140 } 1141 1142 1143 /*************************************************************************/ 1144 /*************************************************************************/ 1145 /***** *****/ 1146 /***** L A T I N G L Y P H A N A L Y S I S *****/ 1147 /***** *****/ 1148 /*************************************************************************/ 1149 /*************************************************************************/ 1150 1151 1152 /* Walk over all contours and compute its segments. */ 1153 1154 FT_LOCAL_DEF( FT_Error ) 1155 af_latin_hints_compute_segments( AF_GlyphHints hints, 1156 AF_Dimension dim ) 1157 { 1158 AF_AxisHints axis = &hints->axis[dim]; 1159 FT_Memory memory = hints->memory; 1160 FT_Error error = FT_Err_Ok; 1161 AF_Segment segment = NULL; 1162 AF_SegmentRec seg0; 1163 AF_Point* contour = hints->contours; 1164 AF_Point* contour_limit = contour + hints->num_contours; 1165 AF_Direction major_dir, segment_dir; 1166 1167 1168 FT_ZERO( &seg0 ); 1169 seg0.score = 32000; 1170 seg0.flags = AF_EDGE_NORMAL; 1171 1172 major_dir = (AF_Direction)FT_ABS( axis->major_dir ); 1173 segment_dir = major_dir; 1174 1175 axis->num_segments = 0; 1176 1177 /* set up (u,v) in each point */ 1178 if ( dim == AF_DIMENSION_HORZ ) 1179 { 1180 AF_Point point = hints->points; 1181 AF_Point limit = point + hints->num_points; 1182 1183 1184 for ( ; point < limit; point++ ) 1185 { 1186 point->u = point->fx; 1187 point->v = point->fy; 1188 } 1189 } 1190 else 1191 { 1192 AF_Point point = hints->points; 1193 AF_Point limit = point + hints->num_points; 1194 1195 1196 for ( ; point < limit; point++ ) 1197 { 1198 point->u = point->fy; 1199 point->v = point->fx; 1200 } 1201 } 1202 1203 /* do each contour separately */ 1204 for ( ; contour < contour_limit; contour++ ) 1205 { 1206 AF_Point point = contour[0]; 1207 AF_Point last = point->prev; 1208 int on_edge = 0; 1209 FT_Pos min_pos = 32000; /* minimum segment pos != min_coord */ 1210 FT_Pos max_pos = -32000; /* maximum segment pos != max_coord */ 1211 FT_Bool passed; 1212 1213 1214 if ( point == last ) /* skip singletons -- just in case */ 1215 continue; 1216 1217 if ( FT_ABS( last->out_dir ) == major_dir && 1218 FT_ABS( point->out_dir ) == major_dir ) 1219 { 1220 /* we are already on an edge, try to locate its start */ 1221 last = point; 1222 1223 for (;;) 1224 { 1225 point = point->prev; 1226 if ( FT_ABS( point->out_dir ) != major_dir ) 1227 { 1228 point = point->next; 1229 break; 1230 } 1231 if ( point == last ) 1232 break; 1233 } 1234 } 1235 1236 last = point; 1237 passed = 0; 1238 1239 for (;;) 1240 { 1241 FT_Pos u, v; 1242 1243 1244 if ( on_edge ) 1245 { 1246 u = point->u; 1247 if ( u < min_pos ) 1248 min_pos = u; 1249 if ( u > max_pos ) 1250 max_pos = u; 1251 1252 if ( point->out_dir != segment_dir || point == last ) 1253 { 1254 /* we are just leaving an edge; record a new segment! */ 1255 segment->last = point; 1256 segment->pos = (FT_Short)( ( min_pos + max_pos ) >> 1 ); 1257 1258 /* a segment is round if either its first or last point */ 1259 /* is a control point */ 1260 if ( ( segment->first->flags | point->flags ) & 1261 AF_FLAG_CONTROL ) 1262 segment->flags |= AF_EDGE_ROUND; 1263 1264 /* compute segment size */ 1265 min_pos = max_pos = point->v; 1266 1267 v = segment->first->v; 1268 if ( v < min_pos ) 1269 min_pos = v; 1270 if ( v > max_pos ) 1271 max_pos = v; 1272 1273 segment->min_coord = (FT_Short)min_pos; 1274 segment->max_coord = (FT_Short)max_pos; 1275 segment->height = (FT_Short)( segment->max_coord - 1276 segment->min_coord ); 1277 1278 on_edge = 0; 1279 segment = NULL; 1280 /* fall through */ 1281 } 1282 } 1283 1284 /* now exit if we are at the start/end point */ 1285 if ( point == last ) 1286 { 1287 if ( passed ) 1288 break; 1289 passed = 1; 1290 } 1291 1292 if ( !on_edge && FT_ABS( point->out_dir ) == major_dir ) 1293 { 1294 /* this is the start of a new segment! */ 1295 segment_dir = (AF_Direction)point->out_dir; 1296 1297 error = af_axis_hints_new_segment( axis, memory, &segment ); 1298 if ( error ) 1299 goto Exit; 1300 1301 /* clear all segment fields */ 1302 segment[0] = seg0; 1303 1304 segment->dir = (FT_Char)segment_dir; 1305 min_pos = max_pos = point->u; 1306 segment->first = point; 1307 segment->last = point; 1308 1309 on_edge = 1; 1310 } 1311 1312 point = point->next; 1313 } 1314 1315 } /* contours */ 1316 1317 1318 /* now slightly increase the height of segments if this makes */ 1319 /* sense -- this is used to better detect and ignore serifs */ 1320 { 1321 AF_Segment segments = axis->segments; 1322 AF_Segment segments_end = segments + axis->num_segments; 1323 1324 1325 for ( segment = segments; segment < segments_end; segment++ ) 1326 { 1327 AF_Point first = segment->first; 1328 AF_Point last = segment->last; 1329 FT_Pos first_v = first->v; 1330 FT_Pos last_v = last->v; 1331 1332 1333 if ( first_v < last_v ) 1334 { 1335 AF_Point p; 1336 1337 1338 p = first->prev; 1339 if ( p->v < first_v ) 1340 segment->height = (FT_Short)( segment->height + 1341 ( ( first_v - p->v ) >> 1 ) ); 1342 1343 p = last->next; 1344 if ( p->v > last_v ) 1345 segment->height = (FT_Short)( segment->height + 1346 ( ( p->v - last_v ) >> 1 ) ); 1347 } 1348 else 1349 { 1350 AF_Point p; 1351 1352 1353 p = first->prev; 1354 if ( p->v > first_v ) 1355 segment->height = (FT_Short)( segment->height + 1356 ( ( p->v - first_v ) >> 1 ) ); 1357 1358 p = last->next; 1359 if ( p->v < last_v ) 1360 segment->height = (FT_Short)( segment->height + 1361 ( ( last_v - p->v ) >> 1 ) ); 1362 } 1363 } 1364 } 1365 1366 Exit: 1367 return error; 1368 } 1369 1370 1371 /* Link segments to form stems and serifs. If `width_count' and */ 1372 /* `widths' are non-zero, use them to fine-tune the scoring function. */ 1373 1374 FT_LOCAL_DEF( void ) 1375 af_latin_hints_link_segments( AF_GlyphHints hints, 1376 FT_UInt width_count, 1377 AF_WidthRec* widths, 1378 AF_Dimension dim ) 1379 { 1380 AF_AxisHints axis = &hints->axis[dim]; 1381 AF_Segment segments = axis->segments; 1382 AF_Segment segment_limit = segments + axis->num_segments; 1383 FT_Pos len_threshold, len_score, dist_score, max_width; 1384 AF_Segment seg1, seg2; 1385 1386 1387 if ( width_count ) 1388 max_width = widths[width_count - 1].org; 1389 else 1390 max_width = 0; 1391 1392 /* a heuristic value to set up a minimum value for overlapping */ 1393 len_threshold = AF_LATIN_CONSTANT( hints->metrics, 8 ); 1394 if ( len_threshold == 0 ) 1395 len_threshold = 1; 1396 1397 /* a heuristic value to weight lengths */ 1398 len_score = AF_LATIN_CONSTANT( hints->metrics, 6000 ); 1399 1400 /* a heuristic value to weight distances (no call to */ 1401 /* AF_LATIN_CONSTANT needed, since we work on multiples */ 1402 /* of the stem width) */ 1403 dist_score = 3000; 1404 1405 /* now compare each segment to the others */ 1406 for ( seg1 = segments; seg1 < segment_limit; seg1++ ) 1407 { 1408 if ( seg1->dir != axis->major_dir ) 1409 continue; 1410 1411 /* search for stems having opposite directions, */ 1412 /* with seg1 to the `left' of seg2 */ 1413 for ( seg2 = segments; seg2 < segment_limit; seg2++ ) 1414 { 1415 FT_Pos pos1 = seg1->pos; 1416 FT_Pos pos2 = seg2->pos; 1417 1418 1419 if ( seg1->dir + seg2->dir == 0 && pos2 > pos1 ) 1420 { 1421 /* compute distance between the two segments */ 1422 FT_Pos min = seg1->min_coord; 1423 FT_Pos max = seg1->max_coord; 1424 FT_Pos len; 1425 1426 1427 if ( min < seg2->min_coord ) 1428 min = seg2->min_coord; 1429 1430 if ( max > seg2->max_coord ) 1431 max = seg2->max_coord; 1432 1433 /* compute maximum coordinate difference of the two segments */ 1434 /* (this is, how much they overlap) */ 1435 len = max - min; 1436 if ( len >= len_threshold ) 1437 { 1438 /* 1439 * The score is the sum of two demerits indicating the 1440 * `badness' of a fit, measured along the segments' main axis 1441 * and orthogonal to it, respectively. 1442 * 1443 * o The less overlapping along the main axis, the worse it 1444 * is, causing a larger demerit. 1445 * 1446 * o The nearer the orthogonal distance to a stem width, the 1447 * better it is, causing a smaller demerit. For simplicity, 1448 * however, we only increase the demerit for values that 1449 * exceed the largest stem width. 1450 */ 1451 1452 FT_Pos dist = pos2 - pos1; 1453 1454 FT_Pos dist_demerit, score; 1455 1456 1457 if ( max_width ) 1458 { 1459 /* distance demerits are based on multiples of `max_width'; */ 1460 /* we scale by 1024 for getting more precision */ 1461 FT_Pos delta = ( dist << 10 ) / max_width - ( 1 << 10 ); 1462 1463 1464 if ( delta > 10000 ) 1465 dist_demerit = 32000; 1466 else if ( delta > 0 ) 1467 dist_demerit = delta * delta / dist_score; 1468 else 1469 dist_demerit = 0; 1470 } 1471 else 1472 dist_demerit = dist; /* default if no widths available */ 1473 1474 score = dist_demerit + len_score / len; 1475 1476 /* and we search for the smallest score */ 1477 if ( score < seg1->score ) 1478 { 1479 seg1->score = score; 1480 seg1->link = seg2; 1481 } 1482 1483 if ( score < seg2->score ) 1484 { 1485 seg2->score = score; 1486 seg2->link = seg1; 1487 } 1488 } 1489 } 1490 } 1491 } 1492 1493 /* now compute the `serif' segments, cf. explanations in `afhints.h' */ 1494 for ( seg1 = segments; seg1 < segment_limit; seg1++ ) 1495 { 1496 seg2 = seg1->link; 1497 1498 if ( seg2 ) 1499 { 1500 if ( seg2->link != seg1 ) 1501 { 1502 seg1->link = 0; 1503 seg1->serif = seg2->link; 1504 } 1505 } 1506 } 1507 } 1508 1509 1510 /* Link segments to edges, using feature analysis for selection. */ 1511 1512 FT_LOCAL_DEF( FT_Error ) 1513 af_latin_hints_compute_edges( AF_GlyphHints hints, 1514 AF_Dimension dim ) 1515 { 1516 AF_AxisHints axis = &hints->axis[dim]; 1517 FT_Error error = FT_Err_Ok; 1518 FT_Memory memory = hints->memory; 1519 AF_LatinAxis laxis = &((AF_LatinMetrics)hints->metrics)->axis[dim]; 1520 1521 AF_Segment segments = axis->segments; 1522 AF_Segment segment_limit = segments + axis->num_segments; 1523 AF_Segment seg; 1524 1525 #if 0 1526 AF_Direction up_dir; 1527 #endif 1528 FT_Fixed scale; 1529 FT_Pos edge_distance_threshold; 1530 FT_Pos segment_length_threshold; 1531 1532 1533 axis->num_edges = 0; 1534 1535 scale = ( dim == AF_DIMENSION_HORZ ) ? hints->x_scale 1536 : hints->y_scale; 1537 1538 #if 0 1539 up_dir = ( dim == AF_DIMENSION_HORZ ) ? AF_DIR_UP 1540 : AF_DIR_RIGHT; 1541 #endif 1542 1543 /* 1544 * We ignore all segments that are less than 1 pixel in length 1545 * to avoid many problems with serif fonts. We compute the 1546 * corresponding threshold in font units. 1547 */ 1548 if ( dim == AF_DIMENSION_HORZ ) 1549 segment_length_threshold = FT_DivFix( 64, hints->y_scale ); 1550 else 1551 segment_length_threshold = 0; 1552 1553 /*********************************************************************/ 1554 /* */ 1555 /* We begin by generating a sorted table of edges for the current */ 1556 /* direction. To do so, we simply scan each segment and try to find */ 1557 /* an edge in our table that corresponds to its position. */ 1558 /* */ 1559 /* If no edge is found, we create and insert a new edge in the */ 1560 /* sorted table. Otherwise, we simply add the segment to the edge's */ 1561 /* list which gets processed in the second step to compute the */ 1562 /* edge's properties. */ 1563 /* */ 1564 /* Note that the table of edges is sorted along the segment/edge */ 1565 /* position. */ 1566 /* */ 1567 /*********************************************************************/ 1568 1569 /* assure that edge distance threshold is at most 0.25px */ 1570 edge_distance_threshold = FT_MulFix( laxis->edge_distance_threshold, 1571 scale ); 1572 if ( edge_distance_threshold > 64 / 4 ) 1573 edge_distance_threshold = 64 / 4; 1574 1575 edge_distance_threshold = FT_DivFix( edge_distance_threshold, 1576 scale ); 1577 1578 for ( seg = segments; seg < segment_limit; seg++ ) 1579 { 1580 AF_Edge found = NULL; 1581 FT_Int ee; 1582 1583 1584 if ( seg->height < segment_length_threshold ) 1585 continue; 1586 1587 /* A special case for serif edges: If they are smaller than */ 1588 /* 1.5 pixels we ignore them. */ 1589 if ( seg->serif && 1590 2 * seg->height < 3 * segment_length_threshold ) 1591 continue; 1592 1593 /* look for an edge corresponding to the segment */ 1594 for ( ee = 0; ee < axis->num_edges; ee++ ) 1595 { 1596 AF_Edge edge = axis->edges + ee; 1597 FT_Pos dist; 1598 1599 1600 dist = seg->pos - edge->fpos; 1601 if ( dist < 0 ) 1602 dist = -dist; 1603 1604 if ( dist < edge_distance_threshold && edge->dir == seg->dir ) 1605 { 1606 found = edge; 1607 break; 1608 } 1609 } 1610 1611 if ( !found ) 1612 { 1613 AF_Edge edge; 1614 1615 1616 /* insert a new edge in the list and */ 1617 /* sort according to the position */ 1618 error = af_axis_hints_new_edge( axis, seg->pos, 1619 (AF_Direction)seg->dir, 1620 memory, &edge ); 1621 if ( error ) 1622 goto Exit; 1623 1624 /* add the segment to the new edge's list */ 1625 FT_ZERO( edge ); 1626 1627 edge->first = seg; 1628 edge->last = seg; 1629 edge->dir = seg->dir; 1630 edge->fpos = seg->pos; 1631 edge->opos = FT_MulFix( seg->pos, scale ); 1632 edge->pos = edge->opos; 1633 seg->edge_next = seg; 1634 } 1635 else 1636 { 1637 /* if an edge was found, simply add the segment to the edge's */ 1638 /* list */ 1639 seg->edge_next = found->first; 1640 found->last->edge_next = seg; 1641 found->last = seg; 1642 } 1643 } 1644 1645 1646 /******************************************************************/ 1647 /* */ 1648 /* Good, we now compute each edge's properties according to the */ 1649 /* segments found on its position. Basically, these are */ 1650 /* */ 1651 /* - the edge's main direction */ 1652 /* - stem edge, serif edge or both (which defaults to stem then) */ 1653 /* - rounded edge, straight or both (which defaults to straight) */ 1654 /* - link for edge */ 1655 /* */ 1656 /******************************************************************/ 1657 1658 /* first of all, set the `edge' field in each segment -- this is */ 1659 /* required in order to compute edge links */ 1660 1661 /* 1662 * Note that removing this loop and setting the `edge' field of each 1663 * segment directly in the code above slows down execution speed for 1664 * some reasons on platforms like the Sun. 1665 */ 1666 { 1667 AF_Edge edges = axis->edges; 1668 AF_Edge edge_limit = edges + axis->num_edges; 1669 AF_Edge edge; 1670 1671 1672 for ( edge = edges; edge < edge_limit; edge++ ) 1673 { 1674 seg = edge->first; 1675 if ( seg ) 1676 do 1677 { 1678 seg->edge = edge; 1679 seg = seg->edge_next; 1680 1681 } while ( seg != edge->first ); 1682 } 1683 1684 /* now compute each edge properties */ 1685 for ( edge = edges; edge < edge_limit; edge++ ) 1686 { 1687 FT_Int is_round = 0; /* does it contain round segments? */ 1688 FT_Int is_straight = 0; /* does it contain straight segments? */ 1689 #if 0 1690 FT_Pos ups = 0; /* number of upwards segments */ 1691 FT_Pos downs = 0; /* number of downwards segments */ 1692 #endif 1693 1694 1695 seg = edge->first; 1696 1697 do 1698 { 1699 FT_Bool is_serif; 1700 1701 1702 /* check for roundness of segment */ 1703 if ( seg->flags & AF_EDGE_ROUND ) 1704 is_round++; 1705 else 1706 is_straight++; 1707 1708 #if 0 1709 /* check for segment direction */ 1710 if ( seg->dir == up_dir ) 1711 ups += seg->max_coord - seg->min_coord; 1712 else 1713 downs += seg->max_coord - seg->min_coord; 1714 #endif 1715 1716 /* check for links -- if seg->serif is set, then seg->link must */ 1717 /* be ignored */ 1718 is_serif = (FT_Bool)( seg->serif && 1719 seg->serif->edge && 1720 seg->serif->edge != edge ); 1721 1722 if ( ( seg->link && seg->link->edge != NULL ) || is_serif ) 1723 { 1724 AF_Edge edge2; 1725 AF_Segment seg2; 1726 1727 1728 edge2 = edge->link; 1729 seg2 = seg->link; 1730 1731 if ( is_serif ) 1732 { 1733 seg2 = seg->serif; 1734 edge2 = edge->serif; 1735 } 1736 1737 if ( edge2 ) 1738 { 1739 FT_Pos edge_delta; 1740 FT_Pos seg_delta; 1741 1742 1743 edge_delta = edge->fpos - edge2->fpos; 1744 if ( edge_delta < 0 ) 1745 edge_delta = -edge_delta; 1746 1747 seg_delta = seg->pos - seg2->pos; 1748 if ( seg_delta < 0 ) 1749 seg_delta = -seg_delta; 1750 1751 if ( seg_delta < edge_delta ) 1752 edge2 = seg2->edge; 1753 } 1754 else 1755 edge2 = seg2->edge; 1756 1757 if ( is_serif ) 1758 { 1759 edge->serif = edge2; 1760 edge2->flags |= AF_EDGE_SERIF; 1761 } 1762 else 1763 edge->link = edge2; 1764 } 1765 1766 seg = seg->edge_next; 1767 1768 } while ( seg != edge->first ); 1769 1770 /* set the round/straight flags */ 1771 edge->flags = AF_EDGE_NORMAL; 1772 1773 if ( is_round > 0 && is_round >= is_straight ) 1774 edge->flags |= AF_EDGE_ROUND; 1775 1776 #if 0 1777 /* set the edge's main direction */ 1778 edge->dir = AF_DIR_NONE; 1779 1780 if ( ups > downs ) 1781 edge->dir = (FT_Char)up_dir; 1782 1783 else if ( ups < downs ) 1784 edge->dir = (FT_Char)-up_dir; 1785 1786 else if ( ups == downs ) 1787 edge->dir = 0; /* both up and down! */ 1788 #endif 1789 1790 /* get rid of serifs if link is set */ 1791 /* XXX: This gets rid of many unpleasant artefacts! */ 1792 /* Example: the `c' in cour.pfa at size 13 */ 1793 1794 if ( edge->serif && edge->link ) 1795 edge->serif = NULL; 1796 } 1797 } 1798 1799 Exit: 1800 return error; 1801 } 1802 1803 1804 /* Detect segments and edges for given dimension. */ 1805 1806 FT_LOCAL_DEF( FT_Error ) 1807 af_latin_hints_detect_features( AF_GlyphHints hints, 1808 FT_UInt width_count, 1809 AF_WidthRec* widths, 1810 AF_Dimension dim ) 1811 { 1812 FT_Error error; 1813 1814 1815 error = af_latin_hints_compute_segments( hints, dim ); 1816 if ( !error ) 1817 { 1818 af_latin_hints_link_segments( hints, width_count, widths, dim ); 1819 1820 error = af_latin_hints_compute_edges( hints, dim ); 1821 } 1822 1823 return error; 1824 } 1825 1826 1827 /* Compute all edges which lie within blue zones. */ 1828 1829 static void 1830 af_latin_hints_compute_blue_edges( AF_GlyphHints hints, 1831 AF_LatinMetrics metrics ) 1832 { 1833 AF_AxisHints axis = &hints->axis[AF_DIMENSION_VERT]; 1834 AF_Edge edge = axis->edges; 1835 AF_Edge edge_limit = edge + axis->num_edges; 1836 AF_LatinAxis latin = &metrics->axis[AF_DIMENSION_VERT]; 1837 FT_Fixed scale = latin->scale; 1838 1839 1840 /* compute which blue zones are active, i.e. have their scaled */ 1841 /* size < 3/4 pixels */ 1842 1843 /* for each horizontal edge search the blue zone which is closest */ 1844 for ( ; edge < edge_limit; edge++ ) 1845 { 1846 FT_UInt bb; 1847 AF_Width best_blue = NULL; 1848 FT_Bool best_blue_is_neutral = 0; 1849 FT_Pos best_dist; /* initial threshold */ 1850 1851 1852 /* compute the initial threshold as a fraction of the EM size */ 1853 /* (the value 40 is heuristic) */ 1854 best_dist = FT_MulFix( metrics->units_per_em / 40, scale ); 1855 1856 /* assure a minimum distance of 0.5px */ 1857 if ( best_dist > 64 / 2 ) 1858 best_dist = 64 / 2; 1859 1860 for ( bb = 0; bb < latin->blue_count; bb++ ) 1861 { 1862 AF_LatinBlue blue = latin->blues + bb; 1863 FT_Bool is_top_blue, is_neutral_blue, is_major_dir; 1864 1865 1866 /* skip inactive blue zones (i.e., those that are too large) */ 1867 if ( !( blue->flags & AF_LATIN_BLUE_ACTIVE ) ) 1868 continue; 1869 1870 /* if it is a top zone, check for right edges (against the major */ 1871 /* direction); if it is a bottom zone, check for left edges (in */ 1872 /* the major direction) -- this assumes the TrueType convention */ 1873 /* for the orientation of contours */ 1874 is_top_blue = 1875 (FT_Byte)( ( blue->flags & AF_LATIN_BLUE_TOP ) != 0 ); 1876 is_neutral_blue = 1877 (FT_Byte)( ( blue->flags & AF_LATIN_BLUE_NEUTRAL ) != 0); 1878 is_major_dir = 1879 FT_BOOL( edge->dir == axis->major_dir ); 1880 1881 /* neutral blue zones are handled for both directions */ 1882 if ( is_top_blue ^ is_major_dir || is_neutral_blue ) 1883 { 1884 FT_Pos dist; 1885 1886 1887 /* first of all, compare it to the reference position */ 1888 dist = edge->fpos - blue->ref.org; 1889 if ( dist < 0 ) 1890 dist = -dist; 1891 1892 dist = FT_MulFix( dist, scale ); 1893 if ( dist < best_dist ) 1894 { 1895 best_dist = dist; 1896 best_blue = &blue->ref; 1897 best_blue_is_neutral = is_neutral_blue; 1898 } 1899 1900 /* now compare it to the overshoot position and check whether */ 1901 /* the edge is rounded, and whether the edge is over the */ 1902 /* reference position of a top zone, or under the reference */ 1903 /* position of a bottom zone (provided we don't have a */ 1904 /* neutral blue zone) */ 1905 if ( edge->flags & AF_EDGE_ROUND && 1906 dist != 0 && 1907 !is_neutral_blue ) 1908 { 1909 FT_Bool is_under_ref = FT_BOOL( edge->fpos < blue->ref.org ); 1910 1911 1912 if ( is_top_blue ^ is_under_ref ) 1913 { 1914 dist = edge->fpos - blue->shoot.org; 1915 if ( dist < 0 ) 1916 dist = -dist; 1917 1918 dist = FT_MulFix( dist, scale ); 1919 if ( dist < best_dist ) 1920 { 1921 best_dist = dist; 1922 best_blue = &blue->shoot; 1923 best_blue_is_neutral = is_neutral_blue; 1924 } 1925 } 1926 } 1927 } 1928 } 1929 1930 if ( best_blue ) 1931 { 1932 edge->blue_edge = best_blue; 1933 if ( best_blue_is_neutral ) 1934 edge->flags |= AF_EDGE_NEUTRAL; 1935 } 1936 } 1937 } 1938 1939 1940 /* Initalize hinting engine. */ 1941 1942 static FT_Error 1943 af_latin_hints_init( AF_GlyphHints hints, 1944 AF_LatinMetrics metrics ) 1945 { 1946 FT_Render_Mode mode; 1947 FT_UInt32 scaler_flags, other_flags; 1948 FT_Face face = metrics->root.scaler.face; 1949 1950 1951 af_glyph_hints_rescale( hints, (AF_StyleMetrics)metrics ); 1952 1953 /* 1954 * correct x_scale and y_scale if needed, since they may have 1955 * been modified by `af_latin_metrics_scale_dim' above 1956 */ 1957 hints->x_scale = metrics->axis[AF_DIMENSION_HORZ].scale; 1958 hints->x_delta = metrics->axis[AF_DIMENSION_HORZ].delta; 1959 hints->y_scale = metrics->axis[AF_DIMENSION_VERT].scale; 1960 hints->y_delta = metrics->axis[AF_DIMENSION_VERT].delta; 1961 1962 /* compute flags depending on render mode, etc. */ 1963 mode = metrics->root.scaler.render_mode; 1964 1965 #if 0 /* #ifdef AF_CONFIG_OPTION_USE_WARPER */ 1966 if ( mode == FT_RENDER_MODE_LCD || mode == FT_RENDER_MODE_LCD_V ) 1967 metrics->root.scaler.render_mode = mode = FT_RENDER_MODE_NORMAL; 1968 #endif 1969 1970 scaler_flags = hints->scaler_flags; 1971 other_flags = 0; 1972 1973 /* 1974 * We snap the width of vertical stems for the monochrome and 1975 * horizontal LCD rendering targets only. 1976 */ 1977 if ( mode == FT_RENDER_MODE_MONO || mode == FT_RENDER_MODE_LCD ) 1978 other_flags |= AF_LATIN_HINTS_HORZ_SNAP; 1979 1980 /* 1981 * We snap the width of horizontal stems for the monochrome and 1982 * vertical LCD rendering targets only. 1983 */ 1984 if ( mode == FT_RENDER_MODE_MONO || mode == FT_RENDER_MODE_LCD_V ) 1985 other_flags |= AF_LATIN_HINTS_VERT_SNAP; 1986 1987 /* 1988 * We adjust stems to full pixels only if we don't use the `light' mode. 1989 */ 1990 if ( mode != FT_RENDER_MODE_LIGHT ) 1991 other_flags |= AF_LATIN_HINTS_STEM_ADJUST; 1992 1993 if ( mode == FT_RENDER_MODE_MONO ) 1994 other_flags |= AF_LATIN_HINTS_MONO; 1995 1996 /* 1997 * In `light' hinting mode we disable horizontal hinting completely. 1998 * We also do it if the face is italic. 1999 * 2000 * However, if warping is enabled (which only works in `light' hinting 2001 * mode), advance widths get adjusted, too. 2002 */ 2003 if ( mode == FT_RENDER_MODE_LIGHT || 2004 ( face->style_flags & FT_STYLE_FLAG_ITALIC ) != 0 ) 2005 scaler_flags |= AF_SCALER_FLAG_NO_HORIZONTAL; 2006 2007 #ifdef AF_CONFIG_OPTION_USE_WARPER 2008 /* get (global) warper flag */ 2009 if ( !metrics->root.globals->module->warping ) 2010 scaler_flags |= AF_SCALER_FLAG_NO_WARPER; 2011 #endif 2012 2013 hints->scaler_flags = scaler_flags; 2014 hints->other_flags = other_flags; 2015 2016 return FT_Err_Ok; 2017 } 2018 2019 2020 /*************************************************************************/ 2021 /*************************************************************************/ 2022 /***** *****/ 2023 /***** L A T I N G L Y P H G R I D - F I T T I N G *****/ 2024 /***** *****/ 2025 /*************************************************************************/ 2026 /*************************************************************************/ 2027 2028 /* Snap a given width in scaled coordinates to one of the */ 2029 /* current standard widths. */ 2030 2031 static FT_Pos 2032 af_latin_snap_width( AF_Width widths, 2033 FT_UInt count, 2034 FT_Pos width ) 2035 { 2036 FT_UInt n; 2037 FT_Pos best = 64 + 32 + 2; 2038 FT_Pos reference = width; 2039 FT_Pos scaled; 2040 2041 2042 for ( n = 0; n < count; n++ ) 2043 { 2044 FT_Pos w; 2045 FT_Pos dist; 2046 2047 2048 w = widths[n].cur; 2049 dist = width - w; 2050 if ( dist < 0 ) 2051 dist = -dist; 2052 if ( dist < best ) 2053 { 2054 best = dist; 2055 reference = w; 2056 } 2057 } 2058 2059 scaled = FT_PIX_ROUND( reference ); 2060 2061 if ( width >= reference ) 2062 { 2063 if ( width < scaled + 48 ) 2064 width = reference; 2065 } 2066 else 2067 { 2068 if ( width > scaled - 48 ) 2069 width = reference; 2070 } 2071 2072 return width; 2073 } 2074 2075 2076 /* Compute the snapped width of a given stem, ignoring very thin ones. */ 2077 /* There is a lot of voodoo in this function; changing the hard-coded */ 2078 /* parameters influence the whole hinting process. */ 2079 2080 static FT_Pos 2081 af_latin_compute_stem_width( AF_GlyphHints hints, 2082 AF_Dimension dim, 2083 FT_Pos width, 2084 FT_UInt base_flags, 2085 FT_UInt stem_flags ) 2086 { 2087 AF_LatinMetrics metrics = (AF_LatinMetrics)hints->metrics; 2088 AF_LatinAxis axis = &metrics->axis[dim]; 2089 FT_Pos dist = width; 2090 FT_Int sign = 0; 2091 FT_Int vertical = ( dim == AF_DIMENSION_VERT ); 2092 2093 2094 if ( !AF_LATIN_HINTS_DO_STEM_ADJUST( hints ) || 2095 axis->extra_light ) 2096 return width; 2097 2098 if ( dist < 0 ) 2099 { 2100 dist = -width; 2101 sign = 1; 2102 } 2103 2104 if ( ( vertical && !AF_LATIN_HINTS_DO_VERT_SNAP( hints ) ) || 2105 ( !vertical && !AF_LATIN_HINTS_DO_HORZ_SNAP( hints ) ) ) 2106 { 2107 /* smooth hinting process: very lightly quantize the stem width */ 2108 2109 /* leave the widths of serifs alone */ 2110 if ( ( stem_flags & AF_EDGE_SERIF ) && 2111 vertical && 2112 ( dist < 3 * 64 ) ) 2113 goto Done_Width; 2114 2115 else if ( base_flags & AF_EDGE_ROUND ) 2116 { 2117 if ( dist < 80 ) 2118 dist = 64; 2119 } 2120 else if ( dist < 56 ) 2121 dist = 56; 2122 2123 if ( axis->width_count > 0 ) 2124 { 2125 FT_Pos delta; 2126 2127 2128 /* compare to standard width */ 2129 delta = dist - axis->widths[0].cur; 2130 2131 if ( delta < 0 ) 2132 delta = -delta; 2133 2134 if ( delta < 40 ) 2135 { 2136 dist = axis->widths[0].cur; 2137 if ( dist < 48 ) 2138 dist = 48; 2139 2140 goto Done_Width; 2141 } 2142 2143 if ( dist < 3 * 64 ) 2144 { 2145 delta = dist & 63; 2146 dist &= -64; 2147 2148 if ( delta < 10 ) 2149 dist += delta; 2150 2151 else if ( delta < 32 ) 2152 dist += 10; 2153 2154 else if ( delta < 54 ) 2155 dist += 54; 2156 2157 else 2158 dist += delta; 2159 } 2160 else 2161 dist = ( dist + 32 ) & ~63; 2162 } 2163 } 2164 else 2165 { 2166 /* strong hinting process: snap the stem width to integer pixels */ 2167 2168 FT_Pos org_dist = dist; 2169 2170 2171 dist = af_latin_snap_width( axis->widths, axis->width_count, dist ); 2172 2173 if ( vertical ) 2174 { 2175 /* in the case of vertical hinting, always round */ 2176 /* the stem heights to integer pixels */ 2177 2178 if ( dist >= 64 ) 2179 dist = ( dist + 16 ) & ~63; 2180 else 2181 dist = 64; 2182 } 2183 else 2184 { 2185 if ( AF_LATIN_HINTS_DO_MONO( hints ) ) 2186 { 2187 /* monochrome horizontal hinting: snap widths to integer pixels */ 2188 /* with a different threshold */ 2189 2190 if ( dist < 64 ) 2191 dist = 64; 2192 else 2193 dist = ( dist + 32 ) & ~63; 2194 } 2195 else 2196 { 2197 /* for horizontal anti-aliased hinting, we adopt a more subtle */ 2198 /* approach: we strengthen small stems, round stems whose size */ 2199 /* is between 1 and 2 pixels to an integer, otherwise nothing */ 2200 2201 if ( dist < 48 ) 2202 dist = ( dist + 64 ) >> 1; 2203 2204 else if ( dist < 128 ) 2205 { 2206 /* We only round to an integer width if the corresponding */ 2207 /* distortion is less than 1/4 pixel. Otherwise this */ 2208 /* makes everything worse since the diagonals, which are */ 2209 /* not hinted, appear a lot bolder or thinner than the */ 2210 /* vertical stems. */ 2211 2212 FT_Pos delta; 2213 2214 2215 dist = ( dist + 22 ) & ~63; 2216 delta = dist - org_dist; 2217 if ( delta < 0 ) 2218 delta = -delta; 2219 2220 if ( delta >= 16 ) 2221 { 2222 dist = org_dist; 2223 if ( dist < 48 ) 2224 dist = ( dist + 64 ) >> 1; 2225 } 2226 } 2227 else 2228 /* round otherwise to prevent color fringes in LCD mode */ 2229 dist = ( dist + 32 ) & ~63; 2230 } 2231 } 2232 } 2233 2234 Done_Width: 2235 if ( sign ) 2236 dist = -dist; 2237 2238 return dist; 2239 } 2240 2241 2242 /* Align one stem edge relative to the previous stem edge. */ 2243 2244 static void 2245 af_latin_align_linked_edge( AF_GlyphHints hints, 2246 AF_Dimension dim, 2247 AF_Edge base_edge, 2248 AF_Edge stem_edge ) 2249 { 2250 FT_Pos dist = stem_edge->opos - base_edge->opos; 2251 2252 FT_Pos fitted_width = af_latin_compute_stem_width( hints, dim, dist, 2253 base_edge->flags, 2254 stem_edge->flags ); 2255 2256 2257 stem_edge->pos = base_edge->pos + fitted_width; 2258 2259 FT_TRACE5(( " LINK: edge %d (opos=%.2f) linked to %.2f," 2260 " dist was %.2f, now %.2f\n", 2261 stem_edge - hints->axis[dim].edges, stem_edge->opos / 64.0, 2262 stem_edge->pos / 64.0, dist / 64.0, fitted_width / 64.0 )); 2263 } 2264 2265 2266 /* Shift the coordinates of the `serif' edge by the same amount */ 2267 /* as the corresponding `base' edge has been moved already. */ 2268 2269 static void 2270 af_latin_align_serif_edge( AF_GlyphHints hints, 2271 AF_Edge base, 2272 AF_Edge serif ) 2273 { 2274 FT_UNUSED( hints ); 2275 2276 serif->pos = base->pos + ( serif->opos - base->opos ); 2277 } 2278 2279 2280 /*************************************************************************/ 2281 /*************************************************************************/ 2282 /*************************************************************************/ 2283 /**** ****/ 2284 /**** E D G E H I N T I N G ****/ 2285 /**** ****/ 2286 /*************************************************************************/ 2287 /*************************************************************************/ 2288 /*************************************************************************/ 2289 2290 2291 /* The main grid-fitting routine. */ 2292 2293 static void 2294 af_latin_hint_edges( AF_GlyphHints hints, 2295 AF_Dimension dim ) 2296 { 2297 AF_AxisHints axis = &hints->axis[dim]; 2298 AF_Edge edges = axis->edges; 2299 AF_Edge edge_limit = edges + axis->num_edges; 2300 FT_PtrDist n_edges; 2301 AF_Edge edge; 2302 AF_Edge anchor = NULL; 2303 FT_Int has_serifs = 0; 2304 2305 #ifdef FT_DEBUG_LEVEL_TRACE 2306 FT_UInt num_actions = 0; 2307 #endif 2308 2309 2310 FT_TRACE5(( "latin %s edge hinting (style `%s')\n", 2311 dim == AF_DIMENSION_VERT ? "horizontal" : "vertical", 2312 af_style_names[hints->metrics->style_class->style] )); 2313 2314 /* we begin by aligning all stems relative to the blue zone */ 2315 /* if needed -- that's only for horizontal edges */ 2316 2317 if ( dim == AF_DIMENSION_VERT && AF_HINTS_DO_BLUES( hints ) ) 2318 { 2319 for ( edge = edges; edge < edge_limit; edge++ ) 2320 { 2321 AF_Width blue; 2322 AF_Edge edge1, edge2; /* these edges form the stem to check */ 2323 2324 2325 if ( edge->flags & AF_EDGE_DONE ) 2326 continue; 2327 2328 edge1 = NULL; 2329 edge2 = edge->link; 2330 2331 /* 2332 * If a stem contains both a neutral and a non-neutral blue zone, 2333 * skip the neutral one. Otherwise, outlines with different 2334 * directions might be incorrectly aligned at the same vertical 2335 * position. 2336 * 2337 * If we have two neutral blue zones, skip one of them. 2338 * 2339 */ 2340 if ( edge->blue_edge && edge2 && edge2->blue_edge ) 2341 { 2342 FT_Byte neutral = edge->flags & AF_EDGE_NEUTRAL; 2343 FT_Byte neutral2 = edge2->flags & AF_EDGE_NEUTRAL; 2344 2345 2346 if ( neutral2 ) 2347 { 2348 edge2->blue_edge = NULL; 2349 edge2->flags &= ~AF_EDGE_NEUTRAL; 2350 } 2351 else if ( neutral ) 2352 { 2353 edge->blue_edge = NULL; 2354 edge->flags &= ~AF_EDGE_NEUTRAL; 2355 } 2356 } 2357 2358 blue = edge->blue_edge; 2359 if ( blue ) 2360 edge1 = edge; 2361 2362 /* flip edges if the other edge is aligned to a blue zone */ 2363 else if ( edge2 && edge2->blue_edge ) 2364 { 2365 blue = edge2->blue_edge; 2366 edge1 = edge2; 2367 edge2 = edge; 2368 } 2369 2370 if ( !edge1 ) 2371 continue; 2372 2373 #ifdef FT_DEBUG_LEVEL_TRACE 2374 if ( !anchor ) 2375 FT_TRACE5(( " BLUE_ANCHOR: edge %d (opos=%.2f) snapped to %.2f," 2376 " was %.2f (anchor=edge %d)\n", 2377 edge1 - edges, edge1->opos / 64.0, blue->fit / 64.0, 2378 edge1->pos / 64.0, edge - edges )); 2379 else 2380 FT_TRACE5(( " BLUE: edge %d (opos=%.2f) snapped to %.2f," 2381 " was %.2f\n", 2382 edge1 - edges, edge1->opos / 64.0, blue->fit / 64.0, 2383 edge1->pos / 64.0 )); 2384 2385 num_actions++; 2386 #endif 2387 2388 edge1->pos = blue->fit; 2389 edge1->flags |= AF_EDGE_DONE; 2390 2391 if ( edge2 && !edge2->blue_edge ) 2392 { 2393 af_latin_align_linked_edge( hints, dim, edge1, edge2 ); 2394 edge2->flags |= AF_EDGE_DONE; 2395 2396 #ifdef FT_DEBUG_LEVEL_TRACE 2397 num_actions++; 2398 #endif 2399 } 2400 2401 if ( !anchor ) 2402 anchor = edge; 2403 } 2404 } 2405 2406 /* now we align all other stem edges, trying to maintain the */ 2407 /* relative order of stems in the glyph */ 2408 for ( edge = edges; edge < edge_limit; edge++ ) 2409 { 2410 AF_Edge edge2; 2411 2412 2413 if ( edge->flags & AF_EDGE_DONE ) 2414 continue; 2415 2416 /* skip all non-stem edges */ 2417 edge2 = edge->link; 2418 if ( !edge2 ) 2419 { 2420 has_serifs++; 2421 continue; 2422 } 2423 2424 /* now align the stem */ 2425 2426 /* this should not happen, but it's better to be safe */ 2427 if ( edge2->blue_edge ) 2428 { 2429 FT_TRACE5(( " ASSERTION FAILED for edge %d\n", edge2 - edges )); 2430 2431 af_latin_align_linked_edge( hints, dim, edge2, edge ); 2432 edge->flags |= AF_EDGE_DONE; 2433 2434 #ifdef FT_DEBUG_LEVEL_TRACE 2435 num_actions++; 2436 #endif 2437 continue; 2438 } 2439 2440 if ( !anchor ) 2441 { 2442 /* if we reach this if clause, no stem has been aligned yet */ 2443 2444 FT_Pos org_len, org_center, cur_len; 2445 FT_Pos cur_pos1, error1, error2, u_off, d_off; 2446 2447 2448 org_len = edge2->opos - edge->opos; 2449 cur_len = af_latin_compute_stem_width( hints, dim, org_len, 2450 edge->flags, 2451 edge2->flags ); 2452 2453 /* some voodoo to specially round edges for small stem widths; */ 2454 /* the idea is to align the center of a stem, then shifting */ 2455 /* the stem edges to suitable positions */ 2456 if ( cur_len <= 64 ) 2457 { 2458 /* width <= 1px */ 2459 u_off = 32; 2460 d_off = 32; 2461 } 2462 else 2463 { 2464 /* 1px < width < 1.5px */ 2465 u_off = 38; 2466 d_off = 26; 2467 } 2468 2469 if ( cur_len < 96 ) 2470 { 2471 org_center = edge->opos + ( org_len >> 1 ); 2472 cur_pos1 = FT_PIX_ROUND( org_center ); 2473 2474 error1 = org_center - ( cur_pos1 - u_off ); 2475 if ( error1 < 0 ) 2476 error1 = -error1; 2477 2478 error2 = org_center - ( cur_pos1 + d_off ); 2479 if ( error2 < 0 ) 2480 error2 = -error2; 2481 2482 if ( error1 < error2 ) 2483 cur_pos1 -= u_off; 2484 else 2485 cur_pos1 += d_off; 2486 2487 edge->pos = cur_pos1 - cur_len / 2; 2488 edge2->pos = edge->pos + cur_len; 2489 } 2490 else 2491 edge->pos = FT_PIX_ROUND( edge->opos ); 2492 2493 anchor = edge; 2494 edge->flags |= AF_EDGE_DONE; 2495 2496 FT_TRACE5(( " ANCHOR: edge %d (opos=%.2f) and %d (opos=%.2f)" 2497 " snapped to %.2f and %.2f\n", 2498 edge - edges, edge->opos / 64.0, 2499 edge2 - edges, edge2->opos / 64.0, 2500 edge->pos / 64.0, edge2->pos / 64.0 )); 2501 2502 af_latin_align_linked_edge( hints, dim, edge, edge2 ); 2503 2504 #ifdef FT_DEBUG_LEVEL_TRACE 2505 num_actions += 2; 2506 #endif 2507 } 2508 else 2509 { 2510 FT_Pos org_pos, org_len, org_center, cur_len; 2511 FT_Pos cur_pos1, cur_pos2, delta1, delta2; 2512 2513 2514 org_pos = anchor->pos + ( edge->opos - anchor->opos ); 2515 org_len = edge2->opos - edge->opos; 2516 org_center = org_pos + ( org_len >> 1 ); 2517 2518 cur_len = af_latin_compute_stem_width( hints, dim, org_len, 2519 edge->flags, 2520 edge2->flags ); 2521 2522 if ( edge2->flags & AF_EDGE_DONE ) 2523 { 2524 FT_TRACE5(( " ADJUST: edge %d (pos=%.2f) moved to %.2f\n", 2525 edge - edges, edge->pos / 64.0, 2526 ( edge2->pos - cur_len ) / 64.0 )); 2527 2528 edge->pos = edge2->pos - cur_len; 2529 } 2530 2531 else if ( cur_len < 96 ) 2532 { 2533 FT_Pos u_off, d_off; 2534 2535 2536 cur_pos1 = FT_PIX_ROUND( org_center ); 2537 2538 if ( cur_len <= 64 ) 2539 { 2540 u_off = 32; 2541 d_off = 32; 2542 } 2543 else 2544 { 2545 u_off = 38; 2546 d_off = 26; 2547 } 2548 2549 delta1 = org_center - ( cur_pos1 - u_off ); 2550 if ( delta1 < 0 ) 2551 delta1 = -delta1; 2552 2553 delta2 = org_center - ( cur_pos1 + d_off ); 2554 if ( delta2 < 0 ) 2555 delta2 = -delta2; 2556 2557 if ( delta1 < delta2 ) 2558 cur_pos1 -= u_off; 2559 else 2560 cur_pos1 += d_off; 2561 2562 edge->pos = cur_pos1 - cur_len / 2; 2563 edge2->pos = cur_pos1 + cur_len / 2; 2564 2565 FT_TRACE5(( " STEM: edge %d (opos=%.2f) linked to %d (opos=%.2f)" 2566 " snapped to %.2f and %.2f\n", 2567 edge - edges, edge->opos / 64.0, 2568 edge2 - edges, edge2->opos / 64.0, 2569 edge->pos / 64.0, edge2->pos / 64.0 )); 2570 } 2571 2572 else 2573 { 2574 org_pos = anchor->pos + ( edge->opos - anchor->opos ); 2575 org_len = edge2->opos - edge->opos; 2576 org_center = org_pos + ( org_len >> 1 ); 2577 2578 cur_len = af_latin_compute_stem_width( hints, dim, org_len, 2579 edge->flags, 2580 edge2->flags ); 2581 2582 cur_pos1 = FT_PIX_ROUND( org_pos ); 2583 delta1 = cur_pos1 + ( cur_len >> 1 ) - org_center; 2584 if ( delta1 < 0 ) 2585 delta1 = -delta1; 2586 2587 cur_pos2 = FT_PIX_ROUND( org_pos + org_len ) - cur_len; 2588 delta2 = cur_pos2 + ( cur_len >> 1 ) - org_center; 2589 if ( delta2 < 0 ) 2590 delta2 = -delta2; 2591 2592 edge->pos = ( delta1 < delta2 ) ? cur_pos1 : cur_pos2; 2593 edge2->pos = edge->pos + cur_len; 2594 2595 FT_TRACE5(( " STEM: edge %d (opos=%.2f) linked to %d (opos=%.2f)" 2596 " snapped to %.2f and %.2f\n", 2597 edge - edges, edge->opos / 64.0, 2598 edge2 - edges, edge2->opos / 64.0, 2599 edge->pos / 64.0, edge2->pos / 64.0 )); 2600 } 2601 2602 #ifdef FT_DEBUG_LEVEL_TRACE 2603 num_actions++; 2604 #endif 2605 2606 edge->flags |= AF_EDGE_DONE; 2607 edge2->flags |= AF_EDGE_DONE; 2608 2609 if ( edge > edges && edge->pos < edge[-1].pos ) 2610 { 2611 #ifdef FT_DEBUG_LEVEL_TRACE 2612 FT_TRACE5(( " BOUND: edge %d (pos=%.2f) moved to %.2f\n", 2613 edge - edges, edge->pos / 64.0, edge[-1].pos / 64.0 )); 2614 2615 num_actions++; 2616 #endif 2617 2618 edge->pos = edge[-1].pos; 2619 } 2620 } 2621 } 2622 2623 /* make sure that lowercase m's maintain their symmetry */ 2624 2625 /* In general, lowercase m's have six vertical edges if they are sans */ 2626 /* serif, or twelve if they are with serifs. This implementation is */ 2627 /* based on that assumption, and seems to work very well with most */ 2628 /* faces. However, if for a certain face this assumption is not */ 2629 /* true, the m is just rendered like before. In addition, any stem */ 2630 /* correction will only be applied to symmetrical glyphs (even if the */ 2631 /* glyph is not an m), so the potential for unwanted distortion is */ 2632 /* relatively low. */ 2633 2634 /* We don't handle horizontal edges since we can't easily assure that */ 2635 /* the third (lowest) stem aligns with the base line; it might end up */ 2636 /* one pixel higher or lower. */ 2637 2638 n_edges = edge_limit - edges; 2639 if ( dim == AF_DIMENSION_HORZ && ( n_edges == 6 || n_edges == 12 ) ) 2640 { 2641 AF_Edge edge1, edge2, edge3; 2642 FT_Pos dist1, dist2, span, delta; 2643 2644 2645 if ( n_edges == 6 ) 2646 { 2647 edge1 = edges; 2648 edge2 = edges + 2; 2649 edge3 = edges + 4; 2650 } 2651 else 2652 { 2653 edge1 = edges + 1; 2654 edge2 = edges + 5; 2655 edge3 = edges + 9; 2656 } 2657 2658 dist1 = edge2->opos - edge1->opos; 2659 dist2 = edge3->opos - edge2->opos; 2660 2661 span = dist1 - dist2; 2662 if ( span < 0 ) 2663 span = -span; 2664 2665 if ( span < 8 ) 2666 { 2667 delta = edge3->pos - ( 2 * edge2->pos - edge1->pos ); 2668 edge3->pos -= delta; 2669 if ( edge3->link ) 2670 edge3->link->pos -= delta; 2671 2672 /* move the serifs along with the stem */ 2673 if ( n_edges == 12 ) 2674 { 2675 ( edges + 8 )->pos -= delta; 2676 ( edges + 11 )->pos -= delta; 2677 } 2678 2679 edge3->flags |= AF_EDGE_DONE; 2680 if ( edge3->link ) 2681 edge3->link->flags |= AF_EDGE_DONE; 2682 } 2683 } 2684 2685 if ( has_serifs || !anchor ) 2686 { 2687 /* 2688 * now hint the remaining edges (serifs and single) in order 2689 * to complete our processing 2690 */ 2691 for ( edge = edges; edge < edge_limit; edge++ ) 2692 { 2693 FT_Pos delta; 2694 2695 2696 if ( edge->flags & AF_EDGE_DONE ) 2697 continue; 2698 2699 delta = 1000; 2700 2701 if ( edge->serif ) 2702 { 2703 delta = edge->serif->opos - edge->opos; 2704 if ( delta < 0 ) 2705 delta = -delta; 2706 } 2707 2708 if ( delta < 64 + 16 ) 2709 { 2710 af_latin_align_serif_edge( hints, edge->serif, edge ); 2711 FT_TRACE5(( " SERIF: edge %d (opos=%.2f) serif to %d (opos=%.2f)" 2712 " aligned to %.2f\n", 2713 edge - edges, edge->opos / 64.0, 2714 edge->serif - edges, edge->serif->opos / 64.0, 2715 edge->pos / 64.0 )); 2716 } 2717 else if ( !anchor ) 2718 { 2719 edge->pos = FT_PIX_ROUND( edge->opos ); 2720 anchor = edge; 2721 FT_TRACE5(( " SERIF_ANCHOR: edge %d (opos=%.2f)" 2722 " snapped to %.2f\n", 2723 edge-edges, edge->opos / 64.0, edge->pos / 64.0 )); 2724 } 2725 else 2726 { 2727 AF_Edge before, after; 2728 2729 2730 for ( before = edge - 1; before >= edges; before-- ) 2731 if ( before->flags & AF_EDGE_DONE ) 2732 break; 2733 2734 for ( after = edge + 1; after < edge_limit; after++ ) 2735 if ( after->flags & AF_EDGE_DONE ) 2736 break; 2737 2738 if ( before >= edges && before < edge && 2739 after < edge_limit && after > edge ) 2740 { 2741 if ( after->opos == before->opos ) 2742 edge->pos = before->pos; 2743 else 2744 edge->pos = before->pos + 2745 FT_MulDiv( edge->opos - before->opos, 2746 after->pos - before->pos, 2747 after->opos - before->opos ); 2748 2749 FT_TRACE5(( " SERIF_LINK1: edge %d (opos=%.2f) snapped to %.2f" 2750 " from %d (opos=%.2f)\n", 2751 edge - edges, edge->opos / 64.0, 2752 edge->pos / 64.0, 2753 before - edges, before->opos / 64.0 )); 2754 } 2755 else 2756 { 2757 edge->pos = anchor->pos + 2758 ( ( edge->opos - anchor->opos + 16 ) & ~31 ); 2759 FT_TRACE5(( " SERIF_LINK2: edge %d (opos=%.2f)" 2760 " snapped to %.2f\n", 2761 edge - edges, edge->opos / 64.0, edge->pos / 64.0 )); 2762 } 2763 } 2764 2765 #ifdef FT_DEBUG_LEVEL_TRACE 2766 num_actions++; 2767 #endif 2768 edge->flags |= AF_EDGE_DONE; 2769 2770 if ( edge > edges && edge->pos < edge[-1].pos ) 2771 { 2772 #ifdef FT_DEBUG_LEVEL_TRACE 2773 FT_TRACE5(( " BOUND: edge %d (pos=%.2f) moved to %.2f\n", 2774 edge - edges, edge->pos / 64.0, edge[-1].pos / 64.0 )); 2775 2776 num_actions++; 2777 #endif 2778 edge->pos = edge[-1].pos; 2779 } 2780 2781 if ( edge + 1 < edge_limit && 2782 edge[1].flags & AF_EDGE_DONE && 2783 edge->pos > edge[1].pos ) 2784 { 2785 #ifdef FT_DEBUG_LEVEL_TRACE 2786 FT_TRACE5(( " BOUND: edge %d (pos=%.2f) moved to %.2f\n", 2787 edge - edges, edge->pos / 64.0, edge[1].pos / 64.0 )); 2788 2789 num_actions++; 2790 #endif 2791 2792 edge->pos = edge[1].pos; 2793 } 2794 } 2795 } 2796 2797 #ifdef FT_DEBUG_LEVEL_TRACE 2798 if ( !num_actions ) 2799 FT_TRACE5(( " (none)\n" )); 2800 FT_TRACE5(( "\n" )); 2801 #endif 2802 } 2803 2804 2805 /* Apply the complete hinting algorithm to a latin glyph. */ 2806 2807 static FT_Error 2808 af_latin_hints_apply( AF_GlyphHints hints, 2809 FT_Outline* outline, 2810 AF_LatinMetrics metrics ) 2811 { 2812 FT_Error error; 2813 int dim; 2814 2815 AF_LatinAxis axis; 2816 2817 2818 error = af_glyph_hints_reload( hints, outline ); 2819 if ( error ) 2820 goto Exit; 2821 2822 /* analyze glyph outline */ 2823 #ifdef AF_CONFIG_OPTION_USE_WARPER 2824 if ( ( metrics->root.scaler.render_mode == FT_RENDER_MODE_LIGHT && 2825 AF_HINTS_DO_WARP( hints ) ) || 2826 AF_HINTS_DO_HORIZONTAL( hints ) ) 2827 #else 2828 if ( AF_HINTS_DO_HORIZONTAL( hints ) ) 2829 #endif 2830 { 2831 axis = &metrics->axis[AF_DIMENSION_HORZ]; 2832 error = af_latin_hints_detect_features( hints, 2833 axis->width_count, 2834 axis->widths, 2835 AF_DIMENSION_HORZ ); 2836 if ( error ) 2837 goto Exit; 2838 } 2839 2840 if ( AF_HINTS_DO_VERTICAL( hints ) ) 2841 { 2842 axis = &metrics->axis[AF_DIMENSION_VERT]; 2843 error = af_latin_hints_detect_features( hints, 2844 axis->width_count, 2845 axis->widths, 2846 AF_DIMENSION_VERT ); 2847 if ( error ) 2848 goto Exit; 2849 2850 af_latin_hints_compute_blue_edges( hints, metrics ); 2851 } 2852 2853 /* grid-fit the outline */ 2854 for ( dim = 0; dim < AF_DIMENSION_MAX; dim++ ) 2855 { 2856 #ifdef AF_CONFIG_OPTION_USE_WARPER 2857 if ( dim == AF_DIMENSION_HORZ && 2858 metrics->root.scaler.render_mode == FT_RENDER_MODE_LIGHT && 2859 AF_HINTS_DO_WARP( hints ) ) 2860 { 2861 AF_WarperRec warper; 2862 FT_Fixed scale; 2863 FT_Pos delta; 2864 2865 2866 af_warper_compute( &warper, hints, (AF_Dimension)dim, 2867 &scale, &delta ); 2868 af_glyph_hints_scale_dim( hints, (AF_Dimension)dim, 2869 scale, delta ); 2870 continue; 2871 } 2872 #endif /* AF_CONFIG_OPTION_USE_WARPER */ 2873 2874 if ( ( dim == AF_DIMENSION_HORZ && AF_HINTS_DO_HORIZONTAL( hints ) ) || 2875 ( dim == AF_DIMENSION_VERT && AF_HINTS_DO_VERTICAL( hints ) ) ) 2876 { 2877 af_latin_hint_edges( hints, (AF_Dimension)dim ); 2878 af_glyph_hints_align_edge_points( hints, (AF_Dimension)dim ); 2879 af_glyph_hints_align_strong_points( hints, (AF_Dimension)dim ); 2880 af_glyph_hints_align_weak_points( hints, (AF_Dimension)dim ); 2881 } 2882 } 2883 2884 af_glyph_hints_save( hints, outline ); 2885 2886 Exit: 2887 return error; 2888 } 2889 2890 2891 /*************************************************************************/ 2892 /*************************************************************************/ 2893 /***** *****/ 2894 /***** L A T I N S C R I P T C L A S S *****/ 2895 /***** *****/ 2896 /*************************************************************************/ 2897 /*************************************************************************/ 2898 2899 2900 AF_DEFINE_WRITING_SYSTEM_CLASS( 2901 af_latin_writing_system_class, 2902 2903 AF_WRITING_SYSTEM_LATIN, 2904 2905 sizeof ( AF_LatinMetricsRec ), 2906 2907 (AF_WritingSystem_InitMetricsFunc) af_latin_metrics_init, 2908 (AF_WritingSystem_ScaleMetricsFunc)af_latin_metrics_scale, 2909 (AF_WritingSystem_DoneMetricsFunc) NULL, 2910 2911 (AF_WritingSystem_InitHintsFunc) af_latin_hints_init, 2912 (AF_WritingSystem_ApplyHintsFunc) af_latin_hints_apply 2913 ) 2914 2915 2916 /* END */ 2917