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