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