1 /***************************************************************************/ 2 /* */ 3 /* aflatin.c */ 4 /* */ 5 /* Auto-fitter hinting routines for latin script (body). */ 6 /* */ 7 /* Copyright 2003, 2004, 2005, 2006, 2007, 2008 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 "aflatin.h" 20 #include "aferrors.h" 21 22 23 #ifdef AF_USE_WARPER 24 #include "afwarp.h" 25 #endif 26 27 28 /*************************************************************************/ 29 /*************************************************************************/ 30 /***** *****/ 31 /***** L A T I N G L O B A L M E T R I C S *****/ 32 /***** *****/ 33 /*************************************************************************/ 34 /*************************************************************************/ 35 36 FT_LOCAL_DEF( void ) af_latin_metrics_init_widths(AF_LatinMetrics metrics,FT_Face face,FT_ULong charcode)37 af_latin_metrics_init_widths( AF_LatinMetrics metrics, 38 FT_Face face, 39 FT_ULong charcode ) 40 { 41 /* scan the array of segments in each direction */ 42 AF_GlyphHintsRec hints[1]; 43 44 45 af_glyph_hints_init( hints, face->memory ); 46 47 metrics->axis[AF_DIMENSION_HORZ].width_count = 0; 48 metrics->axis[AF_DIMENSION_VERT].width_count = 0; 49 50 { 51 FT_Error error; 52 FT_UInt glyph_index; 53 int dim; 54 AF_LatinMetricsRec dummy[1]; 55 AF_Scaler scaler = &dummy->root.scaler; 56 57 58 glyph_index = FT_Get_Char_Index( face, charcode ); 59 if ( glyph_index == 0 ) 60 goto Exit; 61 62 error = FT_Load_Glyph( face, glyph_index, FT_LOAD_NO_SCALE ); 63 if ( error || face->glyph->outline.n_points <= 0 ) 64 goto Exit; 65 66 FT_ZERO( dummy ); 67 68 dummy->units_per_em = metrics->units_per_em; 69 scaler->x_scale = scaler->y_scale = 0x10000L; 70 scaler->x_delta = scaler->y_delta = 0; 71 scaler->face = face; 72 scaler->render_mode = FT_RENDER_MODE_NORMAL; 73 scaler->flags = 0; 74 75 af_glyph_hints_rescale( hints, (AF_ScriptMetrics)dummy ); 76 77 error = af_glyph_hints_reload( hints, &face->glyph->outline, 0 ); 78 if ( error ) 79 goto Exit; 80 81 for ( dim = 0; dim < AF_DIMENSION_MAX; dim++ ) 82 { 83 AF_LatinAxis axis = &metrics->axis[dim]; 84 AF_AxisHints axhints = &hints->axis[dim]; 85 AF_Segment seg, limit, link; 86 FT_UInt num_widths = 0; 87 88 89 error = af_latin_hints_compute_segments( hints, 90 (AF_Dimension)dim ); 91 if ( error ) 92 goto Exit; 93 94 af_latin_hints_link_segments( hints, 95 (AF_Dimension)dim ); 96 97 seg = axhints->segments; 98 limit = seg + axhints->num_segments; 99 100 for ( ; seg < limit; seg++ ) 101 { 102 link = seg->link; 103 104 /* we only consider stem segments there! */ 105 if ( link && link->link == seg && link > seg ) 106 { 107 FT_Pos dist; 108 109 110 dist = seg->pos - link->pos; 111 if ( dist < 0 ) 112 dist = -dist; 113 114 if ( num_widths < AF_LATIN_MAX_WIDTHS ) 115 axis->widths[ num_widths++ ].org = dist; 116 } 117 } 118 119 af_sort_widths( num_widths, axis->widths ); 120 axis->width_count = num_widths; 121 } 122 123 Exit: 124 for ( dim = 0; dim < AF_DIMENSION_MAX; dim++ ) 125 { 126 AF_LatinAxis axis = &metrics->axis[dim]; 127 FT_Pos stdw; 128 129 130 stdw = ( axis->width_count > 0 ) 131 ? axis->widths[0].org 132 : AF_LATIN_CONSTANT( metrics, 50 ); 133 134 /* let's try 20% of the smallest width */ 135 axis->edge_distance_threshold = stdw / 5; 136 axis->standard_width = stdw; 137 axis->extra_light = 0; 138 } 139 } 140 141 af_glyph_hints_done( hints ); 142 } 143 144 145 146 #define AF_LATIN_MAX_TEST_CHARACTERS 12 147 148 149 static const char* const af_latin_blue_chars[AF_LATIN_MAX_BLUES] = 150 { 151 "THEZOCQS", 152 "HEZLOCUS", 153 "fijkdbh", 154 "xzroesc", 155 "xzroesc", 156 "pqgjy" 157 }; 158 159 160 static void af_latin_metrics_init_blues(AF_LatinMetrics metrics,FT_Face face)161 af_latin_metrics_init_blues( AF_LatinMetrics metrics, 162 FT_Face face ) 163 { 164 FT_Pos flats [AF_LATIN_MAX_TEST_CHARACTERS]; 165 FT_Pos rounds[AF_LATIN_MAX_TEST_CHARACTERS]; 166 FT_Int num_flats; 167 FT_Int num_rounds; 168 FT_Int bb; 169 AF_LatinBlue blue; 170 FT_Error error; 171 AF_LatinAxis axis = &metrics->axis[AF_DIMENSION_VERT]; 172 FT_GlyphSlot glyph = face->glyph; 173 174 175 /* we compute the blues simply by loading each character from the */ 176 /* 'af_latin_blue_chars[blues]' string, then compute its top-most or */ 177 /* bottom-most points (depending on `AF_IS_TOP_BLUE') */ 178 179 AF_LOG(( "blue zones computation\n" )); 180 AF_LOG(( "------------------------------------------------\n" )); 181 182 for ( bb = 0; bb < AF_LATIN_BLUE_MAX; bb++ ) 183 { 184 const char* p = af_latin_blue_chars[bb]; 185 const char* limit = p + AF_LATIN_MAX_TEST_CHARACTERS; 186 FT_Pos* blue_ref; 187 FT_Pos* blue_shoot; 188 189 190 AF_LOG(( "blue %3d: ", bb )); 191 192 num_flats = 0; 193 num_rounds = 0; 194 195 for ( ; p < limit && *p; p++ ) 196 { 197 FT_UInt glyph_index; 198 FT_Int best_point, best_y, best_first, best_last; 199 FT_Vector* points; 200 FT_Bool round = 0; 201 202 203 AF_LOG(( "'%c'", *p )); 204 205 /* load the character in the face -- skip unknown or empty ones */ 206 glyph_index = FT_Get_Char_Index( face, (FT_UInt)*p ); 207 if ( glyph_index == 0 ) 208 continue; 209 210 error = FT_Load_Glyph( face, glyph_index, FT_LOAD_NO_SCALE ); 211 if ( error || glyph->outline.n_points <= 0 ) 212 continue; 213 214 /* now compute min or max point indices and coordinates */ 215 points = glyph->outline.points; 216 best_point = -1; 217 best_y = 0; /* make compiler happy */ 218 best_first = 0; /* ditto */ 219 best_last = 0; /* ditto */ 220 221 { 222 FT_Int nn; 223 FT_Int first = 0; 224 FT_Int last = -1; 225 226 227 for ( nn = 0; nn < glyph->outline.n_contours; first = last+1, nn++ ) 228 { 229 FT_Int old_best_point = best_point; 230 FT_Int pp; 231 232 233 last = glyph->outline.contours[nn]; 234 235 /* Avoid single-point contours since they are never rasterized. */ 236 /* In some fonts, they correspond to mark attachment points */ 237 /* which are way outside of the glyph's real outline. */ 238 if ( last <= first ) 239 continue; 240 241 if ( AF_LATIN_IS_TOP_BLUE( bb ) ) 242 { 243 for ( pp = first; pp <= last; pp++ ) 244 if ( best_point < 0 || points[pp].y > best_y ) 245 { 246 best_point = pp; 247 best_y = points[pp].y; 248 } 249 } 250 else 251 { 252 for ( pp = first; pp <= last; pp++ ) 253 if ( best_point < 0 || points[pp].y < best_y ) 254 { 255 best_point = pp; 256 best_y = points[pp].y; 257 } 258 } 259 260 if ( best_point != old_best_point ) 261 { 262 best_first = first; 263 best_last = last; 264 } 265 } 266 AF_LOG(( "%5d", best_y )); 267 } 268 269 /* now check whether the point belongs to a straight or round */ 270 /* segment; we first need to find in which contour the extremum */ 271 /* lies, then inspect its previous and next points */ 272 if ( best_point >= 0 ) 273 { 274 FT_Int prev, next; 275 FT_Pos dist; 276 277 278 /* now look for the previous and next points that are not on the */ 279 /* same Y coordinate. Threshold the `closeness'... */ 280 prev = best_point; 281 next = prev; 282 283 do 284 { 285 if ( prev > best_first ) 286 prev--; 287 else 288 prev = best_last; 289 290 dist = points[prev].y - best_y; 291 if ( dist < -5 || dist > 5 ) 292 break; 293 294 } while ( prev != best_point ); 295 296 do 297 { 298 if ( next < best_last ) 299 next++; 300 else 301 next = best_first; 302 303 dist = points[next].y - best_y; 304 if ( dist < -5 || dist > 5 ) 305 break; 306 307 } while ( next != best_point ); 308 309 /* now, set the `round' flag depending on the segment's kind */ 310 round = FT_BOOL( 311 FT_CURVE_TAG( glyph->outline.tags[prev] ) != FT_CURVE_TAG_ON || 312 FT_CURVE_TAG( glyph->outline.tags[next] ) != FT_CURVE_TAG_ON ); 313 314 AF_LOG(( "%c ", round ? 'r' : 'f' )); 315 } 316 317 if ( round ) 318 rounds[num_rounds++] = best_y; 319 else 320 flats[num_flats++] = best_y; 321 } 322 323 AF_LOG(( "\n" )); 324 325 if ( num_flats == 0 && num_rounds == 0 ) 326 { 327 /* 328 * we couldn't find a single glyph to compute this blue zone, 329 * we will simply ignore it then 330 */ 331 AF_LOG(( "empty!\n" )); 332 continue; 333 } 334 335 /* we have computed the contents of the `rounds' and `flats' tables, */ 336 /* now determine the reference and overshoot position of the blue -- */ 337 /* we simply take the median value after a simple sort */ 338 af_sort_pos( num_rounds, rounds ); 339 af_sort_pos( num_flats, flats ); 340 341 blue = & axis->blues[axis->blue_count]; 342 blue_ref = & blue->ref.org; 343 blue_shoot = & blue->shoot.org; 344 345 axis->blue_count++; 346 347 if ( num_flats == 0 ) 348 { 349 *blue_ref = 350 *blue_shoot = rounds[num_rounds / 2]; 351 } 352 else if ( num_rounds == 0 ) 353 { 354 *blue_ref = 355 *blue_shoot = flats[num_flats / 2]; 356 } 357 else 358 { 359 *blue_ref = flats[num_flats / 2]; 360 *blue_shoot = rounds[num_rounds / 2]; 361 } 362 363 /* there are sometimes problems: if the overshoot position of top */ 364 /* zones is under its reference position, or the opposite for bottom */ 365 /* zones. We must thus check everything there and correct the errors */ 366 if ( *blue_shoot != *blue_ref ) 367 { 368 FT_Pos ref = *blue_ref; 369 FT_Pos shoot = *blue_shoot; 370 FT_Bool over_ref = FT_BOOL( shoot > ref ); 371 372 373 if ( AF_LATIN_IS_TOP_BLUE( bb ) ^ over_ref ) 374 *blue_shoot = *blue_ref = ( shoot + ref ) / 2; 375 } 376 377 blue->flags = 0; 378 if ( AF_LATIN_IS_TOP_BLUE( bb ) ) 379 blue->flags |= AF_LATIN_BLUE_TOP; 380 381 /* 382 * The following flags is used later to adjust the y and x scales 383 * in order to optimize the pixel grid alignment of the top of small 384 * letters. 385 */ 386 if ( bb == AF_LATIN_BLUE_SMALL_TOP ) 387 blue->flags |= AF_LATIN_BLUE_ADJUSTMENT; 388 389 AF_LOG(( "-- ref = %ld, shoot = %ld\n", *blue_ref, *blue_shoot )); 390 } 391 392 return; 393 } 394 395 396 FT_LOCAL_DEF( FT_Error ) af_latin_metrics_init(AF_LatinMetrics metrics,FT_Face face)397 af_latin_metrics_init( AF_LatinMetrics metrics, 398 FT_Face face ) 399 { 400 FT_Error error = AF_Err_Ok; 401 FT_CharMap oldmap = face->charmap; 402 FT_UInt ee; 403 404 static const FT_Encoding latin_encodings[] = 405 { 406 FT_ENCODING_UNICODE, 407 FT_ENCODING_APPLE_ROMAN, 408 FT_ENCODING_ADOBE_STANDARD, 409 FT_ENCODING_ADOBE_LATIN_1, 410 FT_ENCODING_NONE /* end of list */ 411 }; 412 413 414 metrics->units_per_em = face->units_per_EM; 415 416 /* do we have a latin charmap in there? */ 417 for ( ee = 0; latin_encodings[ee] != FT_ENCODING_NONE; ee++ ) 418 { 419 error = FT_Select_Charmap( face, latin_encodings[ee] ); 420 if ( !error ) 421 break; 422 } 423 424 if ( !error ) 425 { 426 /* For now, compute the standard width and height from the `o'. */ 427 af_latin_metrics_init_widths( metrics, face, 'o' ); 428 af_latin_metrics_init_blues( metrics, face ); 429 } 430 431 FT_Set_Charmap( face, oldmap ); 432 return AF_Err_Ok; 433 } 434 435 436 static void af_latin_metrics_scale_dim(AF_LatinMetrics metrics,AF_Scaler scaler,AF_Dimension dim)437 af_latin_metrics_scale_dim( AF_LatinMetrics metrics, 438 AF_Scaler scaler, 439 AF_Dimension dim ) 440 { 441 FT_Fixed scale; 442 FT_Pos delta; 443 AF_LatinAxis axis; 444 FT_UInt nn; 445 446 447 if ( dim == AF_DIMENSION_HORZ ) 448 { 449 scale = scaler->x_scale; 450 delta = scaler->x_delta; 451 } 452 else 453 { 454 scale = scaler->y_scale; 455 delta = scaler->y_delta; 456 } 457 458 axis = &metrics->axis[dim]; 459 460 if ( axis->org_scale == scale && axis->org_delta == delta ) 461 return; 462 463 axis->org_scale = scale; 464 axis->org_delta = delta; 465 466 /* 467 * correct X and Y scale to optimize the alignment of the top of small 468 * letters to the pixel grid 469 */ 470 { 471 AF_LatinAxis Axis = &metrics->axis[AF_DIMENSION_VERT]; 472 AF_LatinBlue blue = NULL; 473 474 475 for ( nn = 0; nn < Axis->blue_count; nn++ ) 476 { 477 if ( Axis->blues[nn].flags & AF_LATIN_BLUE_ADJUSTMENT ) 478 { 479 blue = &Axis->blues[nn]; 480 break; 481 } 482 } 483 484 if ( blue ) 485 { 486 FT_Pos scaled = FT_MulFix( blue->shoot.org, scaler->y_scale ); 487 FT_Pos fitted = ( scaled + 40 ) & ~63; 488 489 490 if ( scaled != fitted ) 491 { 492 #if 0 493 if ( dim == AF_DIMENSION_HORZ ) 494 { 495 if ( fitted < scaled ) 496 scale -= scale / 50; /* scale *= 0.98 */ 497 } 498 else 499 #endif 500 if ( dim == AF_DIMENSION_VERT ) 501 { 502 scale = FT_MulDiv( scale, fitted, scaled ); 503 } 504 } 505 } 506 } 507 508 axis->scale = scale; 509 axis->delta = delta; 510 511 if ( dim == AF_DIMENSION_HORZ ) 512 { 513 metrics->root.scaler.x_scale = scale; 514 metrics->root.scaler.x_delta = delta; 515 } 516 else 517 { 518 metrics->root.scaler.y_scale = scale; 519 metrics->root.scaler.y_delta = delta; 520 } 521 522 /* scale the standard widths */ 523 for ( nn = 0; nn < axis->width_count; nn++ ) 524 { 525 AF_Width width = axis->widths + nn; 526 527 528 width->cur = FT_MulFix( width->org, scale ); 529 width->fit = width->cur; 530 } 531 532 /* an extra-light axis corresponds to a standard width that is */ 533 /* smaller than 0.75 pixels */ 534 axis->extra_light = 535 (FT_Bool)( FT_MulFix( axis->standard_width, scale ) < 32 + 8 ); 536 537 if ( dim == AF_DIMENSION_VERT ) 538 { 539 /* scale the blue zones */ 540 for ( nn = 0; nn < axis->blue_count; nn++ ) 541 { 542 AF_LatinBlue blue = &axis->blues[nn]; 543 FT_Pos dist; 544 545 546 blue->ref.cur = FT_MulFix( blue->ref.org, scale ) + delta; 547 blue->ref.fit = blue->ref.cur; 548 blue->shoot.cur = FT_MulFix( blue->shoot.org, scale ) + delta; 549 blue->shoot.fit = blue->shoot.cur; 550 blue->flags &= ~AF_LATIN_BLUE_ACTIVE; 551 552 /* a blue zone is only active if it is less than 3/4 pixels tall */ 553 dist = FT_MulFix( blue->ref.org - blue->shoot.org, scale ); 554 if ( dist <= 48 && dist >= -48 ) 555 { 556 FT_Pos delta1, delta2; 557 558 559 delta1 = blue->shoot.org - blue->ref.org; 560 delta2 = delta1; 561 if ( delta1 < 0 ) 562 delta2 = -delta2; 563 564 delta2 = FT_MulFix( delta2, scale ); 565 566 if ( delta2 < 32 ) 567 delta2 = 0; 568 else if ( delta2 < 64 ) 569 delta2 = 32 + ( ( ( delta2 - 32 ) + 16 ) & ~31 ); 570 else 571 delta2 = FT_PIX_ROUND( delta2 ); 572 573 if ( delta1 < 0 ) 574 delta2 = -delta2; 575 576 blue->ref.fit = FT_PIX_ROUND( blue->ref.cur ); 577 blue->shoot.fit = blue->ref.fit + delta2; 578 579 blue->flags |= AF_LATIN_BLUE_ACTIVE; 580 } 581 } 582 } 583 } 584 585 586 FT_LOCAL_DEF( void ) af_latin_metrics_scale(AF_LatinMetrics metrics,AF_Scaler scaler)587 af_latin_metrics_scale( AF_LatinMetrics metrics, 588 AF_Scaler scaler ) 589 { 590 metrics->root.scaler.render_mode = scaler->render_mode; 591 metrics->root.scaler.face = scaler->face; 592 593 af_latin_metrics_scale_dim( metrics, scaler, AF_DIMENSION_HORZ ); 594 af_latin_metrics_scale_dim( metrics, scaler, AF_DIMENSION_VERT ); 595 } 596 597 598 /*************************************************************************/ 599 /*************************************************************************/ 600 /***** *****/ 601 /***** L A T I N G L Y P H A N A L Y S I S *****/ 602 /***** *****/ 603 /*************************************************************************/ 604 /*************************************************************************/ 605 606 FT_LOCAL_DEF( FT_Error ) af_latin_hints_compute_segments(AF_GlyphHints hints,AF_Dimension dim)607 af_latin_hints_compute_segments( AF_GlyphHints hints, 608 AF_Dimension dim ) 609 { 610 AF_AxisHints axis = &hints->axis[dim]; 611 FT_Memory memory = hints->memory; 612 FT_Error error = AF_Err_Ok; 613 AF_Segment segment = NULL; 614 AF_SegmentRec seg0; 615 AF_Point* contour = hints->contours; 616 AF_Point* contour_limit = contour + hints->num_contours; 617 AF_Direction major_dir, segment_dir; 618 619 620 FT_ZERO( &seg0 ); 621 seg0.score = 32000; 622 seg0.flags = AF_EDGE_NORMAL; 623 624 major_dir = (AF_Direction)FT_ABS( axis->major_dir ); 625 segment_dir = major_dir; 626 627 axis->num_segments = 0; 628 629 /* set up (u,v) in each point */ 630 if ( dim == AF_DIMENSION_HORZ ) 631 { 632 AF_Point point = hints->points; 633 AF_Point limit = point + hints->num_points; 634 635 636 for ( ; point < limit; point++ ) 637 { 638 point->u = point->fx; 639 point->v = point->fy; 640 } 641 } 642 else 643 { 644 AF_Point point = hints->points; 645 AF_Point limit = point + hints->num_points; 646 647 648 for ( ; point < limit; point++ ) 649 { 650 point->u = point->fy; 651 point->v = point->fx; 652 } 653 } 654 655 /* do each contour separately */ 656 for ( ; contour < contour_limit; contour++ ) 657 { 658 AF_Point point = contour[0]; 659 AF_Point last = point->prev; 660 int on_edge = 0; 661 FT_Pos min_pos = 32000; /* minimum segment pos != min_coord */ 662 FT_Pos max_pos = -32000; /* maximum segment pos != max_coord */ 663 FT_Bool passed; 664 665 666 if ( point == last ) /* skip singletons -- just in case */ 667 continue; 668 669 if ( FT_ABS( last->out_dir ) == major_dir && 670 FT_ABS( point->out_dir ) == major_dir ) 671 { 672 /* we are already on an edge, try to locate its start */ 673 last = point; 674 675 for (;;) 676 { 677 point = point->prev; 678 if ( FT_ABS( point->out_dir ) != major_dir ) 679 { 680 point = point->next; 681 break; 682 } 683 if ( point == last ) 684 break; 685 } 686 } 687 688 last = point; 689 passed = 0; 690 691 for (;;) 692 { 693 FT_Pos u, v; 694 695 696 if ( on_edge ) 697 { 698 u = point->u; 699 if ( u < min_pos ) 700 min_pos = u; 701 if ( u > max_pos ) 702 max_pos = u; 703 704 if ( point->out_dir != segment_dir || point == last ) 705 { 706 /* we are just leaving an edge; record a new segment! */ 707 segment->last = point; 708 segment->pos = (FT_Short)( ( min_pos + max_pos ) >> 1 ); 709 710 /* a segment is round if either its first or last point */ 711 /* is a control point */ 712 if ( ( segment->first->flags | point->flags ) & 713 AF_FLAG_CONTROL ) 714 segment->flags |= AF_EDGE_ROUND; 715 716 /* compute segment size */ 717 min_pos = max_pos = point->v; 718 719 v = segment->first->v; 720 if ( v < min_pos ) 721 min_pos = v; 722 if ( v > max_pos ) 723 max_pos = v; 724 725 segment->min_coord = (FT_Short)min_pos; 726 segment->max_coord = (FT_Short)max_pos; 727 segment->height = (FT_Short)( segment->max_coord - 728 segment->min_coord ); 729 730 on_edge = 0; 731 segment = NULL; 732 /* fallthrough */ 733 } 734 } 735 736 /* now exit if we are at the start/end point */ 737 if ( point == last ) 738 { 739 if ( passed ) 740 break; 741 passed = 1; 742 } 743 744 if ( !on_edge && FT_ABS( point->out_dir ) == major_dir ) 745 { 746 /* this is the start of a new segment! */ 747 segment_dir = (AF_Direction)point->out_dir; 748 749 /* clear all segment fields */ 750 error = af_axis_hints_new_segment( axis, memory, &segment ); 751 if ( error ) 752 goto Exit; 753 754 segment[0] = seg0; 755 segment->dir = (FT_Char)segment_dir; 756 min_pos = max_pos = point->u; 757 segment->first = point; 758 segment->last = point; 759 segment->contour = contour; 760 on_edge = 1; 761 } 762 763 point = point->next; 764 } 765 766 } /* contours */ 767 768 769 /* now slightly increase the height of segments when this makes */ 770 /* sense -- this is used to better detect and ignore serifs */ 771 { 772 AF_Segment segments = axis->segments; 773 AF_Segment segments_end = segments + axis->num_segments; 774 775 776 for ( segment = segments; segment < segments_end; segment++ ) 777 { 778 AF_Point first = segment->first; 779 AF_Point last = segment->last; 780 FT_Pos first_v = first->v; 781 FT_Pos last_v = last->v; 782 783 784 if ( first == last ) 785 continue; 786 787 if ( first_v < last_v ) 788 { 789 AF_Point p; 790 791 792 p = first->prev; 793 if ( p->v < first_v ) 794 segment->height = (FT_Short)( segment->height + 795 ( ( first_v - p->v ) >> 1 ) ); 796 797 p = last->next; 798 if ( p->v > last_v ) 799 segment->height = (FT_Short)( segment->height + 800 ( ( p->v - last_v ) >> 1 ) ); 801 } 802 else 803 { 804 AF_Point p; 805 806 807 p = first->prev; 808 if ( p->v > first_v ) 809 segment->height = (FT_Short)( segment->height + 810 ( ( p->v - first_v ) >> 1 ) ); 811 812 p = last->next; 813 if ( p->v < last_v ) 814 segment->height = (FT_Short)( segment->height + 815 ( ( last_v - p->v ) >> 1 ) ); 816 } 817 } 818 } 819 820 Exit: 821 return error; 822 } 823 824 825 FT_LOCAL_DEF( void ) af_latin_hints_link_segments(AF_GlyphHints hints,AF_Dimension dim)826 af_latin_hints_link_segments( AF_GlyphHints hints, 827 AF_Dimension dim ) 828 { 829 AF_AxisHints axis = &hints->axis[dim]; 830 AF_Segment segments = axis->segments; 831 AF_Segment segment_limit = segments + axis->num_segments; 832 FT_Pos len_threshold, len_score; 833 AF_Segment seg1, seg2; 834 835 836 len_threshold = AF_LATIN_CONSTANT( hints->metrics, 8 ); 837 if ( len_threshold == 0 ) 838 len_threshold = 1; 839 840 len_score = AF_LATIN_CONSTANT( hints->metrics, 6000 ); 841 842 /* now compare each segment to the others */ 843 for ( seg1 = segments; seg1 < segment_limit; seg1++ ) 844 { 845 /* the fake segments are introduced to hint the metrics -- */ 846 /* we must never link them to anything */ 847 if ( seg1->dir != axis->major_dir || seg1->first == seg1->last ) 848 continue; 849 850 for ( seg2 = segments; seg2 < segment_limit; seg2++ ) 851 if ( seg1->dir + seg2->dir == 0 && seg2->pos > seg1->pos ) 852 { 853 FT_Pos pos1 = seg1->pos; 854 FT_Pos pos2 = seg2->pos; 855 FT_Pos dist = pos2 - pos1; 856 857 858 if ( dist < 0 ) 859 dist = -dist; 860 861 { 862 FT_Pos min = seg1->min_coord; 863 FT_Pos max = seg1->max_coord; 864 FT_Pos len, score; 865 866 867 if ( min < seg2->min_coord ) 868 min = seg2->min_coord; 869 870 if ( max > seg2->max_coord ) 871 max = seg2->max_coord; 872 873 len = max - min; 874 if ( len >= len_threshold ) 875 { 876 score = dist + len_score / len; 877 878 if ( score < seg1->score ) 879 { 880 seg1->score = score; 881 seg1->link = seg2; 882 } 883 884 if ( score < seg2->score ) 885 { 886 seg2->score = score; 887 seg2->link = seg1; 888 } 889 } 890 } 891 } 892 } 893 894 /* now, compute the `serif' segments */ 895 for ( seg1 = segments; seg1 < segment_limit; seg1++ ) 896 { 897 seg2 = seg1->link; 898 899 if ( seg2 ) 900 { 901 if ( seg2->link != seg1 ) 902 { 903 seg1->link = 0; 904 seg1->serif = seg2->link; 905 } 906 } 907 } 908 } 909 910 911 FT_LOCAL_DEF( FT_Error ) af_latin_hints_compute_edges(AF_GlyphHints hints,AF_Dimension dim)912 af_latin_hints_compute_edges( AF_GlyphHints hints, 913 AF_Dimension dim ) 914 { 915 AF_AxisHints axis = &hints->axis[dim]; 916 FT_Error error = AF_Err_Ok; 917 FT_Memory memory = hints->memory; 918 AF_LatinAxis laxis = &((AF_LatinMetrics)hints->metrics)->axis[dim]; 919 920 AF_Segment segments = axis->segments; 921 AF_Segment segment_limit = segments + axis->num_segments; 922 AF_Segment seg; 923 924 AF_Direction up_dir; 925 FT_Fixed scale; 926 FT_Pos edge_distance_threshold; 927 FT_Pos segment_length_threshold; 928 929 930 axis->num_edges = 0; 931 932 scale = ( dim == AF_DIMENSION_HORZ ) ? hints->x_scale 933 : hints->y_scale; 934 935 up_dir = ( dim == AF_DIMENSION_HORZ ) ? AF_DIR_UP 936 : AF_DIR_RIGHT; 937 938 /* 939 * We ignore all segments that are less than 1 pixels in length, 940 * to avoid many problems with serif fonts. We compute the 941 * corresponding threshold in font units. 942 */ 943 if ( dim == AF_DIMENSION_HORZ ) 944 segment_length_threshold = FT_DivFix( 64, hints->y_scale ); 945 else 946 segment_length_threshold = 0; 947 948 /*********************************************************************/ 949 /* */ 950 /* We will begin by generating a sorted table of edges for the */ 951 /* current direction. To do so, we simply scan each segment and try */ 952 /* to find an edge in our table that corresponds to its position. */ 953 /* */ 954 /* If no edge is found, we create and insert a new edge in the */ 955 /* sorted table. Otherwise, we simply add the segment to the edge's */ 956 /* list which will be processed in the second step to compute the */ 957 /* edge's properties. */ 958 /* */ 959 /* Note that the edges table is sorted along the segment/edge */ 960 /* position. */ 961 /* */ 962 /*********************************************************************/ 963 964 edge_distance_threshold = FT_MulFix( laxis->edge_distance_threshold, 965 scale ); 966 if ( edge_distance_threshold > 64 / 4 ) 967 edge_distance_threshold = 64 / 4; 968 969 edge_distance_threshold = FT_DivFix( edge_distance_threshold, 970 scale ); 971 972 for ( seg = segments; seg < segment_limit; seg++ ) 973 { 974 AF_Edge found = 0; 975 FT_Int ee; 976 977 978 if ( seg->height < segment_length_threshold ) 979 continue; 980 981 /* A special case for serif edges: If they are smaller than */ 982 /* 1.5 pixels we ignore them. */ 983 if ( seg->serif && 984 2 * seg->height < 3 * segment_length_threshold ) 985 continue; 986 987 /* look for an edge corresponding to the segment */ 988 for ( ee = 0; ee < axis->num_edges; ee++ ) 989 { 990 AF_Edge edge = axis->edges + ee; 991 FT_Pos dist; 992 993 994 dist = seg->pos - edge->fpos; 995 if ( dist < 0 ) 996 dist = -dist; 997 998 if ( dist < edge_distance_threshold && edge->dir == seg->dir ) 999 { 1000 found = edge; 1001 break; 1002 } 1003 } 1004 1005 if ( !found ) 1006 { 1007 AF_Edge edge; 1008 1009 1010 /* insert a new edge in the list and */ 1011 /* sort according to the position */ 1012 error = af_axis_hints_new_edge( axis, seg->pos, 1013 (AF_Direction)seg->dir, 1014 memory, &edge ); 1015 if ( error ) 1016 goto Exit; 1017 1018 /* add the segment to the new edge's list */ 1019 FT_ZERO( edge ); 1020 1021 edge->first = seg; 1022 edge->last = seg; 1023 edge->fpos = seg->pos; 1024 edge->dir = seg->dir; 1025 edge->opos = edge->pos = FT_MulFix( seg->pos, scale ); 1026 seg->edge_next = seg; 1027 } 1028 else 1029 { 1030 /* if an edge was found, simply add the segment to the edge's */ 1031 /* list */ 1032 seg->edge_next = found->first; 1033 found->last->edge_next = seg; 1034 found->last = seg; 1035 } 1036 } 1037 1038 1039 /*********************************************************************/ 1040 /* */ 1041 /* Good, we will now compute each edge's properties according to */ 1042 /* segments found on its position. Basically, these are: */ 1043 /* */ 1044 /* - edge's main direction */ 1045 /* - stem edge, serif edge or both (which defaults to stem then) */ 1046 /* - rounded edge, straight or both (which defaults to straight) */ 1047 /* - link for edge */ 1048 /* */ 1049 /*********************************************************************/ 1050 1051 /* first of all, set the `edge' field in each segment -- this is */ 1052 /* required in order to compute edge links */ 1053 1054 /* 1055 * Note that removing this loop and setting the `edge' field of each 1056 * segment directly in the code above slows down execution speed for 1057 * some reasons on platforms like the Sun. 1058 */ 1059 { 1060 AF_Edge edges = axis->edges; 1061 AF_Edge edge_limit = edges + axis->num_edges; 1062 AF_Edge edge; 1063 1064 1065 for ( edge = edges; edge < edge_limit; edge++ ) 1066 { 1067 seg = edge->first; 1068 if ( seg ) 1069 do 1070 { 1071 seg->edge = edge; 1072 seg = seg->edge_next; 1073 1074 } while ( seg != edge->first ); 1075 } 1076 1077 /* now, compute each edge properties */ 1078 for ( edge = edges; edge < edge_limit; edge++ ) 1079 { 1080 FT_Int is_round = 0; /* does it contain round segments? */ 1081 FT_Int is_straight = 0; /* does it contain straight segments? */ 1082 FT_Pos ups = 0; /* number of upwards segments */ 1083 FT_Pos downs = 0; /* number of downwards segments */ 1084 1085 1086 seg = edge->first; 1087 1088 do 1089 { 1090 FT_Bool is_serif; 1091 1092 1093 /* check for roundness of segment */ 1094 if ( seg->flags & AF_EDGE_ROUND ) 1095 is_round++; 1096 else 1097 is_straight++; 1098 1099 /* check for segment direction */ 1100 if ( seg->dir == up_dir ) 1101 ups += seg->max_coord-seg->min_coord; 1102 else 1103 downs += seg->max_coord-seg->min_coord; 1104 1105 /* check for links -- if seg->serif is set, then seg->link must */ 1106 /* be ignored */ 1107 is_serif = (FT_Bool)( seg->serif && 1108 seg->serif->edge && 1109 seg->serif->edge != edge ); 1110 1111 if ( ( seg->link && seg->link->edge != NULL ) || is_serif ) 1112 { 1113 AF_Edge edge2; 1114 AF_Segment seg2; 1115 1116 1117 edge2 = edge->link; 1118 seg2 = seg->link; 1119 1120 if ( is_serif ) 1121 { 1122 seg2 = seg->serif; 1123 edge2 = edge->serif; 1124 } 1125 1126 if ( edge2 ) 1127 { 1128 FT_Pos edge_delta; 1129 FT_Pos seg_delta; 1130 1131 1132 edge_delta = edge->fpos - edge2->fpos; 1133 if ( edge_delta < 0 ) 1134 edge_delta = -edge_delta; 1135 1136 seg_delta = seg->pos - seg2->pos; 1137 if ( seg_delta < 0 ) 1138 seg_delta = -seg_delta; 1139 1140 if ( seg_delta < edge_delta ) 1141 edge2 = seg2->edge; 1142 } 1143 else 1144 edge2 = seg2->edge; 1145 1146 if ( is_serif ) 1147 { 1148 edge->serif = edge2; 1149 edge2->flags |= AF_EDGE_SERIF; 1150 } 1151 else 1152 edge->link = edge2; 1153 } 1154 1155 seg = seg->edge_next; 1156 1157 } while ( seg != edge->first ); 1158 1159 /* set the round/straight flags */ 1160 edge->flags = AF_EDGE_NORMAL; 1161 1162 if ( is_round > 0 && is_round >= is_straight ) 1163 edge->flags |= AF_EDGE_ROUND; 1164 1165 #if 0 1166 /* set the edge's main direction */ 1167 edge->dir = AF_DIR_NONE; 1168 1169 if ( ups > downs ) 1170 edge->dir = (FT_Char)up_dir; 1171 1172 else if ( ups < downs ) 1173 edge->dir = (FT_Char)-up_dir; 1174 1175 else if ( ups == downs ) 1176 edge->dir = 0; /* both up and down! */ 1177 #endif 1178 1179 /* gets rid of serifs if link is set */ 1180 /* XXX: This gets rid of many unpleasant artefacts! */ 1181 /* Example: the `c' in cour.pfa at size 13 */ 1182 1183 if ( edge->serif && edge->link ) 1184 edge->serif = 0; 1185 } 1186 } 1187 1188 Exit: 1189 return error; 1190 } 1191 1192 1193 FT_LOCAL_DEF( FT_Error ) af_latin_hints_detect_features(AF_GlyphHints hints,AF_Dimension dim)1194 af_latin_hints_detect_features( AF_GlyphHints hints, 1195 AF_Dimension dim ) 1196 { 1197 FT_Error error; 1198 1199 1200 error = af_latin_hints_compute_segments( hints, dim ); 1201 if ( !error ) 1202 { 1203 af_latin_hints_link_segments( hints, dim ); 1204 1205 error = af_latin_hints_compute_edges( hints, dim ); 1206 } 1207 return error; 1208 } 1209 1210 1211 FT_LOCAL_DEF( void ) af_latin_hints_compute_blue_edges(AF_GlyphHints hints,AF_LatinMetrics metrics)1212 af_latin_hints_compute_blue_edges( AF_GlyphHints hints, 1213 AF_LatinMetrics metrics ) 1214 { 1215 AF_AxisHints axis = &hints->axis[ AF_DIMENSION_VERT ]; 1216 AF_Edge edge = axis->edges; 1217 AF_Edge edge_limit = edge + axis->num_edges; 1218 AF_LatinAxis latin = &metrics->axis[ AF_DIMENSION_VERT ]; 1219 FT_Fixed scale = latin->scale; 1220 1221 1222 /* compute which blue zones are active, i.e. have their scaled */ 1223 /* size < 3/4 pixels */ 1224 1225 /* for each horizontal edge search the blue zone which is closest */ 1226 for ( ; edge < edge_limit; edge++ ) 1227 { 1228 FT_Int bb; 1229 AF_Width best_blue = NULL; 1230 FT_Pos best_dist; /* initial threshold */ 1231 1232 1233 /* compute the initial threshold as a fraction of the EM size */ 1234 best_dist = FT_MulFix( metrics->units_per_em / 40, scale ); 1235 1236 if ( best_dist > 64 / 2 ) 1237 best_dist = 64 / 2; 1238 1239 for ( bb = 0; bb < AF_LATIN_BLUE_MAX; bb++ ) 1240 { 1241 AF_LatinBlue blue = latin->blues + bb; 1242 FT_Bool is_top_blue, is_major_dir; 1243 1244 1245 /* skip inactive blue zones (i.e., those that are too small) */ 1246 if ( !( blue->flags & AF_LATIN_BLUE_ACTIVE ) ) 1247 continue; 1248 1249 /* if it is a top zone, check for right edges -- if it is a bottom */ 1250 /* zone, check for left edges */ 1251 /* */ 1252 /* of course, that's for TrueType */ 1253 is_top_blue = (FT_Byte)( ( blue->flags & AF_LATIN_BLUE_TOP ) != 0 ); 1254 is_major_dir = FT_BOOL( edge->dir == axis->major_dir ); 1255 1256 /* if it is a top zone, the edge must be against the major */ 1257 /* direction; if it is a bottom zone, it must be in the major */ 1258 /* direction */ 1259 if ( is_top_blue ^ is_major_dir ) 1260 { 1261 FT_Pos dist; 1262 1263 1264 /* first of all, compare it to the reference position */ 1265 dist = edge->fpos - blue->ref.org; 1266 if ( dist < 0 ) 1267 dist = -dist; 1268 1269 dist = FT_MulFix( dist, scale ); 1270 if ( dist < best_dist ) 1271 { 1272 best_dist = dist; 1273 best_blue = & blue->ref; 1274 } 1275 1276 /* now, compare it to the overshoot position if the edge is */ 1277 /* rounded, and if the edge is over the reference position of a */ 1278 /* top zone, or under the reference position of a bottom zone */ 1279 if ( edge->flags & AF_EDGE_ROUND && dist != 0 ) 1280 { 1281 FT_Bool is_under_ref = FT_BOOL( edge->fpos < blue->ref.org ); 1282 1283 1284 if ( is_top_blue ^ is_under_ref ) 1285 { 1286 blue = latin->blues + bb; 1287 dist = edge->fpos - blue->shoot.org; 1288 if ( dist < 0 ) 1289 dist = -dist; 1290 1291 dist = FT_MulFix( dist, scale ); 1292 if ( dist < best_dist ) 1293 { 1294 best_dist = dist; 1295 best_blue = & blue->shoot; 1296 } 1297 } 1298 } 1299 } 1300 } 1301 1302 if ( best_blue ) 1303 edge->blue_edge = best_blue; 1304 } 1305 } 1306 1307 1308 static FT_Error af_latin_hints_init(AF_GlyphHints hints,AF_LatinMetrics metrics)1309 af_latin_hints_init( AF_GlyphHints hints, 1310 AF_LatinMetrics metrics ) 1311 { 1312 FT_Render_Mode mode; 1313 FT_UInt32 scaler_flags, other_flags; 1314 FT_Face face = metrics->root.scaler.face; 1315 1316 1317 af_glyph_hints_rescale( hints, (AF_ScriptMetrics)metrics ); 1318 1319 /* 1320 * correct x_scale and y_scale if needed, since they may have 1321 * been modified `af_latin_metrics_scale_dim' above 1322 */ 1323 hints->x_scale = metrics->axis[AF_DIMENSION_HORZ].scale; 1324 hints->x_delta = metrics->axis[AF_DIMENSION_HORZ].delta; 1325 hints->y_scale = metrics->axis[AF_DIMENSION_VERT].scale; 1326 hints->y_delta = metrics->axis[AF_DIMENSION_VERT].delta; 1327 1328 /* compute flags depending on render mode, etc. */ 1329 mode = metrics->root.scaler.render_mode; 1330 1331 #if 0 /* #ifdef AF_USE_WARPER */ 1332 if ( mode == FT_RENDER_MODE_LCD || mode == FT_RENDER_MODE_LCD_V ) 1333 { 1334 metrics->root.scaler.render_mode = mode = FT_RENDER_MODE_NORMAL; 1335 } 1336 #endif 1337 1338 scaler_flags = hints->scaler_flags; 1339 other_flags = 0; 1340 1341 /* 1342 * We snap the width of vertical stems for the monochrome and 1343 * horizontal LCD rendering targets only. 1344 */ 1345 if ( mode == FT_RENDER_MODE_MONO || mode == FT_RENDER_MODE_LCD ) 1346 other_flags |= AF_LATIN_HINTS_HORZ_SNAP; 1347 1348 /* 1349 * We snap the width of horizontal stems for the monochrome and 1350 * vertical LCD rendering targets only. 1351 */ 1352 if ( mode == FT_RENDER_MODE_MONO || mode == FT_RENDER_MODE_LCD_V ) 1353 other_flags |= AF_LATIN_HINTS_VERT_SNAP; 1354 1355 /* 1356 * We adjust stems to full pixels only if we don't use the `light' mode. 1357 */ 1358 if ( mode != FT_RENDER_MODE_LIGHT ) 1359 other_flags |= AF_LATIN_HINTS_STEM_ADJUST; 1360 1361 if ( mode == FT_RENDER_MODE_MONO ) 1362 other_flags |= AF_LATIN_HINTS_MONO; 1363 1364 /* 1365 * In `light' hinting mode we disable horizontal hinting completely. 1366 * We also do it if the face is italic. 1367 */ 1368 if ( mode == FT_RENDER_MODE_LIGHT || 1369 (face->style_flags & FT_STYLE_FLAG_ITALIC) != 0 ) 1370 scaler_flags |= AF_SCALER_FLAG_NO_HORIZONTAL; 1371 1372 hints->scaler_flags = scaler_flags; 1373 hints->other_flags = other_flags; 1374 1375 return 0; 1376 } 1377 1378 1379 /*************************************************************************/ 1380 /*************************************************************************/ 1381 /***** *****/ 1382 /***** L A T I N G L Y P H G R I D - F I T T I N G *****/ 1383 /***** *****/ 1384 /*************************************************************************/ 1385 /*************************************************************************/ 1386 1387 /* snap a given width in scaled coordinates to one of the */ 1388 /* current standard widths */ 1389 1390 static FT_Pos af_latin_snap_width(AF_Width widths,FT_Int count,FT_Pos width)1391 af_latin_snap_width( AF_Width widths, 1392 FT_Int count, 1393 FT_Pos width ) 1394 { 1395 int n; 1396 FT_Pos best = 64 + 32 + 2; 1397 FT_Pos reference = width; 1398 FT_Pos scaled; 1399 1400 1401 for ( n = 0; n < count; n++ ) 1402 { 1403 FT_Pos w; 1404 FT_Pos dist; 1405 1406 1407 w = widths[n].cur; 1408 dist = width - w; 1409 if ( dist < 0 ) 1410 dist = -dist; 1411 if ( dist < best ) 1412 { 1413 best = dist; 1414 reference = w; 1415 } 1416 } 1417 1418 scaled = FT_PIX_ROUND( reference ); 1419 1420 if ( width >= reference ) 1421 { 1422 if ( width < scaled + 48 ) 1423 width = reference; 1424 } 1425 else 1426 { 1427 if ( width > scaled - 48 ) 1428 width = reference; 1429 } 1430 1431 return width; 1432 } 1433 1434 1435 /* compute the snapped width of a given stem */ 1436 1437 static FT_Pos af_latin_compute_stem_width(AF_GlyphHints hints,AF_Dimension dim,FT_Pos width,AF_Edge_Flags base_flags,AF_Edge_Flags stem_flags)1438 af_latin_compute_stem_width( AF_GlyphHints hints, 1439 AF_Dimension dim, 1440 FT_Pos width, 1441 AF_Edge_Flags base_flags, 1442 AF_Edge_Flags stem_flags ) 1443 { 1444 AF_LatinMetrics metrics = (AF_LatinMetrics) hints->metrics; 1445 AF_LatinAxis axis = & metrics->axis[dim]; 1446 FT_Pos dist = width; 1447 FT_Int sign = 0; 1448 FT_Int vertical = ( dim == AF_DIMENSION_VERT ); 1449 1450 1451 if ( !AF_LATIN_HINTS_DO_STEM_ADJUST( hints ) || 1452 axis->extra_light ) 1453 return width; 1454 1455 if ( dist < 0 ) 1456 { 1457 dist = -width; 1458 sign = 1; 1459 } 1460 1461 if ( ( vertical && !AF_LATIN_HINTS_DO_VERT_SNAP( hints ) ) || 1462 ( !vertical && !AF_LATIN_HINTS_DO_HORZ_SNAP( hints ) ) ) 1463 { 1464 /* smooth hinting process: very lightly quantize the stem width */ 1465 1466 /* leave the widths of serifs alone */ 1467 1468 if ( ( stem_flags & AF_EDGE_SERIF ) && vertical && ( dist < 3 * 64 ) ) 1469 goto Done_Width; 1470 1471 else if ( ( base_flags & AF_EDGE_ROUND ) ) 1472 { 1473 if ( dist < 80 ) 1474 dist = 64; 1475 } 1476 else if ( dist < 56 ) 1477 dist = 56; 1478 1479 if ( axis->width_count > 0 ) 1480 { 1481 FT_Pos delta; 1482 1483 1484 /* compare to standard width */ 1485 if ( axis->width_count > 0 ) 1486 { 1487 delta = dist - axis->widths[0].cur; 1488 1489 if ( delta < 0 ) 1490 delta = -delta; 1491 1492 if ( delta < 40 ) 1493 { 1494 dist = axis->widths[0].cur; 1495 if ( dist < 48 ) 1496 dist = 48; 1497 1498 goto Done_Width; 1499 } 1500 } 1501 1502 if ( dist < 3 * 64 ) 1503 { 1504 delta = dist & 63; 1505 dist &= -64; 1506 1507 if ( delta < 10 ) 1508 dist += delta; 1509 1510 else if ( delta < 32 ) 1511 dist += 10; 1512 1513 else if ( delta < 54 ) 1514 dist += 54; 1515 1516 else 1517 dist += delta; 1518 } 1519 else 1520 dist = ( dist + 32 ) & ~63; 1521 } 1522 } 1523 else 1524 { 1525 /* strong hinting process: snap the stem width to integer pixels */ 1526 FT_Pos org_dist = dist; 1527 1528 1529 dist = af_latin_snap_width( axis->widths, axis->width_count, dist ); 1530 1531 if ( vertical ) 1532 { 1533 /* in the case of vertical hinting, always round */ 1534 /* the stem heights to integer pixels */ 1535 1536 if ( dist >= 64 ) 1537 dist = ( dist + 16 ) & ~63; 1538 else 1539 dist = 64; 1540 } 1541 else 1542 { 1543 if ( AF_LATIN_HINTS_DO_MONO( hints ) ) 1544 { 1545 /* monochrome horizontal hinting: snap widths to integer pixels */ 1546 /* with a different threshold */ 1547 1548 if ( dist < 64 ) 1549 dist = 64; 1550 else 1551 dist = ( dist + 32 ) & ~63; 1552 } 1553 else 1554 { 1555 /* for horizontal anti-aliased hinting, we adopt a more subtle */ 1556 /* approach: we strengthen small stems, round stems whose size */ 1557 /* is between 1 and 2 pixels to an integer, otherwise nothing */ 1558 1559 if ( dist < 48 ) 1560 dist = ( dist + 64 ) >> 1; 1561 1562 else if ( dist < 128 ) 1563 { 1564 /* We only round to an integer width if the corresponding */ 1565 /* distortion is less than 1/4 pixel. Otherwise this */ 1566 /* makes everything worse since the diagonals, which are */ 1567 /* not hinted, appear a lot bolder or thinner than the */ 1568 /* vertical stems. */ 1569 1570 FT_Int delta; 1571 1572 1573 dist = ( dist + 22 ) & ~63; 1574 delta = dist - org_dist; 1575 if ( delta < 0 ) 1576 delta = -delta; 1577 1578 if (delta >= 16) 1579 { 1580 dist = org_dist; 1581 if ( dist < 48 ) 1582 dist = ( dist + 64 ) >> 1; 1583 } 1584 } 1585 else 1586 /* round otherwise to prevent color fringes in LCD mode */ 1587 dist = ( dist + 32 ) & ~63; 1588 } 1589 } 1590 } 1591 1592 Done_Width: 1593 if ( sign ) 1594 dist = -dist; 1595 1596 return dist; 1597 } 1598 1599 1600 /* align one stem edge relative to the previous stem edge */ 1601 1602 static void af_latin_align_linked_edge(AF_GlyphHints hints,AF_Dimension dim,AF_Edge base_edge,AF_Edge stem_edge)1603 af_latin_align_linked_edge( AF_GlyphHints hints, 1604 AF_Dimension dim, 1605 AF_Edge base_edge, 1606 AF_Edge stem_edge ) 1607 { 1608 FT_Pos dist = stem_edge->opos - base_edge->opos; 1609 1610 FT_Pos fitted_width = af_latin_compute_stem_width( 1611 hints, dim, dist, 1612 (AF_Edge_Flags)base_edge->flags, 1613 (AF_Edge_Flags)stem_edge->flags ); 1614 1615 1616 stem_edge->pos = base_edge->pos + fitted_width; 1617 1618 AF_LOG(( "LINK: edge %d (opos=%.2f) linked to (%.2f), " 1619 "dist was %.2f, now %.2f\n", 1620 stem_edge-hints->axis[dim].edges, stem_edge->opos / 64.0, 1621 stem_edge->pos / 64.0, dist / 64.0, fitted_width / 64.0 )); 1622 } 1623 1624 1625 static void af_latin_align_serif_edge(AF_GlyphHints hints,AF_Edge base,AF_Edge serif)1626 af_latin_align_serif_edge( AF_GlyphHints hints, 1627 AF_Edge base, 1628 AF_Edge serif ) 1629 { 1630 FT_UNUSED( hints ); 1631 1632 serif->pos = base->pos + (serif->opos - base->opos); 1633 } 1634 1635 1636 /*************************************************************************/ 1637 /*************************************************************************/ 1638 /*************************************************************************/ 1639 /**** ****/ 1640 /**** E D G E H I N T I N G ****/ 1641 /**** ****/ 1642 /*************************************************************************/ 1643 /*************************************************************************/ 1644 /*************************************************************************/ 1645 1646 1647 FT_LOCAL_DEF( void ) af_latin_hint_edges(AF_GlyphHints hints,AF_Dimension dim)1648 af_latin_hint_edges( AF_GlyphHints hints, 1649 AF_Dimension dim ) 1650 { 1651 AF_AxisHints axis = &hints->axis[dim]; 1652 AF_Edge edges = axis->edges; 1653 AF_Edge edge_limit = edges + axis->num_edges; 1654 FT_Int n_edges; 1655 AF_Edge edge; 1656 AF_Edge anchor = 0; 1657 FT_Int has_serifs = 0; 1658 1659 1660 /* we begin by aligning all stems relative to the blue zone */ 1661 /* if needed -- that's only for horizontal edges */ 1662 1663 if ( dim == AF_DIMENSION_VERT && AF_HINTS_DO_BLUES( hints ) ) 1664 { 1665 for ( edge = edges; edge < edge_limit; edge++ ) 1666 { 1667 AF_Width blue; 1668 AF_Edge edge1, edge2; 1669 1670 1671 if ( edge->flags & AF_EDGE_DONE ) 1672 continue; 1673 1674 blue = edge->blue_edge; 1675 edge1 = NULL; 1676 edge2 = edge->link; 1677 1678 if ( blue ) 1679 { 1680 edge1 = edge; 1681 } 1682 else if ( edge2 && edge2->blue_edge ) 1683 { 1684 blue = edge2->blue_edge; 1685 edge1 = edge2; 1686 edge2 = edge; 1687 } 1688 1689 if ( !edge1 ) 1690 continue; 1691 1692 AF_LOG(( "BLUE: edge %d (opos=%.2f) snapped to (%.2f), " 1693 "was (%.2f)\n", 1694 edge1-edges, edge1->opos / 64.0, blue->fit / 64.0, 1695 edge1->pos / 64.0 )); 1696 1697 edge1->pos = blue->fit; 1698 edge1->flags |= AF_EDGE_DONE; 1699 1700 if ( edge2 && !edge2->blue_edge ) 1701 { 1702 af_latin_align_linked_edge( hints, dim, edge1, edge2 ); 1703 edge2->flags |= AF_EDGE_DONE; 1704 } 1705 1706 if ( !anchor ) 1707 anchor = edge; 1708 } 1709 } 1710 1711 /* now we will align all stem edges, trying to maintain the */ 1712 /* relative order of stems in the glyph */ 1713 for ( edge = edges; edge < edge_limit; edge++ ) 1714 { 1715 AF_Edge edge2; 1716 1717 1718 if ( edge->flags & AF_EDGE_DONE ) 1719 continue; 1720 1721 /* skip all non-stem edges */ 1722 edge2 = edge->link; 1723 if ( !edge2 ) 1724 { 1725 has_serifs++; 1726 continue; 1727 } 1728 1729 /* now align the stem */ 1730 1731 /* this should not happen, but it's better to be safe */ 1732 if ( edge2->blue_edge ) 1733 { 1734 AF_LOG(( "ASSERTION FAILED for edge %d\n", edge2-edges )); 1735 1736 af_latin_align_linked_edge( hints, dim, edge2, edge ); 1737 edge->flags |= AF_EDGE_DONE; 1738 continue; 1739 } 1740 1741 if ( !anchor ) 1742 { 1743 FT_Pos org_len, org_center, cur_len; 1744 FT_Pos cur_pos1, error1, error2, u_off, d_off; 1745 1746 1747 org_len = edge2->opos - edge->opos; 1748 cur_len = af_latin_compute_stem_width( 1749 hints, dim, org_len, 1750 (AF_Edge_Flags)edge->flags, 1751 (AF_Edge_Flags)edge2->flags ); 1752 if ( cur_len <= 64 ) 1753 u_off = d_off = 32; 1754 else 1755 { 1756 u_off = 38; 1757 d_off = 26; 1758 } 1759 1760 if ( cur_len < 96 ) 1761 { 1762 org_center = edge->opos + ( org_len >> 1 ); 1763 1764 cur_pos1 = FT_PIX_ROUND( org_center ); 1765 1766 error1 = org_center - ( cur_pos1 - u_off ); 1767 if ( error1 < 0 ) 1768 error1 = -error1; 1769 1770 error2 = org_center - ( cur_pos1 + d_off ); 1771 if ( error2 < 0 ) 1772 error2 = -error2; 1773 1774 if ( error1 < error2 ) 1775 cur_pos1 -= u_off; 1776 else 1777 cur_pos1 += d_off; 1778 1779 edge->pos = cur_pos1 - cur_len / 2; 1780 edge2->pos = edge->pos + cur_len; 1781 } 1782 else 1783 edge->pos = FT_PIX_ROUND( edge->opos ); 1784 1785 AF_LOG(( "ANCHOR: edge %d (opos=%.2f) and %d (opos=%.2f) " 1786 "snapped to (%.2f) (%.2f)\n", 1787 edge-edges, edge->opos / 64.0, 1788 edge2-edges, edge2->opos / 64.0, 1789 edge->pos / 64.0, edge2->pos / 64.0 )); 1790 anchor = edge; 1791 1792 edge->flags |= AF_EDGE_DONE; 1793 1794 af_latin_align_linked_edge( hints, dim, edge, edge2 ); 1795 } 1796 else 1797 { 1798 FT_Pos org_pos, org_len, org_center, cur_len; 1799 FT_Pos cur_pos1, cur_pos2, delta1, delta2; 1800 1801 1802 org_pos = anchor->pos + ( edge->opos - anchor->opos ); 1803 org_len = edge2->opos - edge->opos; 1804 org_center = org_pos + ( org_len >> 1 ); 1805 1806 cur_len = af_latin_compute_stem_width( 1807 hints, dim, org_len, 1808 (AF_Edge_Flags)edge->flags, 1809 (AF_Edge_Flags)edge2->flags ); 1810 1811 if ( edge2->flags & AF_EDGE_DONE ) 1812 edge->pos = edge2->pos - cur_len; 1813 1814 else if ( cur_len < 96 ) 1815 { 1816 FT_Pos u_off, d_off; 1817 1818 1819 cur_pos1 = FT_PIX_ROUND( org_center ); 1820 1821 if (cur_len <= 64 ) 1822 u_off = d_off = 32; 1823 else 1824 { 1825 u_off = 38; 1826 d_off = 26; 1827 } 1828 1829 delta1 = org_center - ( cur_pos1 - u_off ); 1830 if ( delta1 < 0 ) 1831 delta1 = -delta1; 1832 1833 delta2 = org_center - ( cur_pos1 + d_off ); 1834 if ( delta2 < 0 ) 1835 delta2 = -delta2; 1836 1837 if ( delta1 < delta2 ) 1838 cur_pos1 -= u_off; 1839 else 1840 cur_pos1 += d_off; 1841 1842 edge->pos = cur_pos1 - cur_len / 2; 1843 edge2->pos = cur_pos1 + cur_len / 2; 1844 1845 AF_LOG(( "STEM: %d (opos=%.2f) to %d (opos=%.2f) " 1846 "snapped to (%.2f) and (%.2f)\n", 1847 edge-edges, edge->opos / 64.0, 1848 edge2-edges, edge2->opos / 64.0, 1849 edge->pos / 64.0, edge2->pos / 64.0 )); 1850 } 1851 else 1852 { 1853 org_pos = anchor->pos + ( edge->opos - anchor->opos ); 1854 org_len = edge2->opos - edge->opos; 1855 org_center = org_pos + ( org_len >> 1 ); 1856 1857 cur_len = af_latin_compute_stem_width( 1858 hints, dim, org_len, 1859 (AF_Edge_Flags)edge->flags, 1860 (AF_Edge_Flags)edge2->flags ); 1861 1862 cur_pos1 = FT_PIX_ROUND( org_pos ); 1863 delta1 = cur_pos1 + ( cur_len >> 1 ) - org_center; 1864 if ( delta1 < 0 ) 1865 delta1 = -delta1; 1866 1867 cur_pos2 = FT_PIX_ROUND( org_pos + org_len ) - cur_len; 1868 delta2 = cur_pos2 + ( cur_len >> 1 ) - org_center; 1869 if ( delta2 < 0 ) 1870 delta2 = -delta2; 1871 1872 edge->pos = ( delta1 < delta2 ) ? cur_pos1 : cur_pos2; 1873 edge2->pos = edge->pos + cur_len; 1874 1875 AF_LOG(( "STEM: %d (opos=%.2f) to %d (opos=%.2f) " 1876 "snapped to (%.2f) and (%.2f)\n", 1877 edge-edges, edge->opos / 64.0, 1878 edge2-edges, edge2->opos / 64.0, 1879 edge->pos / 64.0, edge2->pos / 64.0 )); 1880 } 1881 1882 edge->flags |= AF_EDGE_DONE; 1883 edge2->flags |= AF_EDGE_DONE; 1884 1885 if ( edge > edges && edge->pos < edge[-1].pos ) 1886 { 1887 AF_LOG(( "BOUND: %d (pos=%.2f) to (%.2f)\n", 1888 edge-edges, edge->pos / 64.0, edge[-1].pos / 64.0 )); 1889 edge->pos = edge[-1].pos; 1890 } 1891 } 1892 } 1893 1894 /* make sure that lowercase m's maintain their symmetry */ 1895 1896 /* In general, lowercase m's have six vertical edges if they are sans */ 1897 /* serif, or twelve if they are with serifs. This implementation is */ 1898 /* based on that assumption, and seems to work very well with most */ 1899 /* faces. However, if for a certain face this assumption is not */ 1900 /* true, the m is just rendered like before. In addition, any stem */ 1901 /* correction will only be applied to symmetrical glyphs (even if the */ 1902 /* glyph is not an m), so the potential for unwanted distortion is */ 1903 /* relatively low. */ 1904 1905 /* We don't handle horizontal edges since we can't easily assure that */ 1906 /* the third (lowest) stem aligns with the base line; it might end up */ 1907 /* one pixel higher or lower. */ 1908 1909 n_edges = edge_limit - edges; 1910 if ( dim == AF_DIMENSION_HORZ && ( n_edges == 6 || n_edges == 12 ) ) 1911 { 1912 AF_Edge edge1, edge2, edge3; 1913 FT_Pos dist1, dist2, span, delta; 1914 1915 1916 if ( n_edges == 6 ) 1917 { 1918 edge1 = edges; 1919 edge2 = edges + 2; 1920 edge3 = edges + 4; 1921 } 1922 else 1923 { 1924 edge1 = edges + 1; 1925 edge2 = edges + 5; 1926 edge3 = edges + 9; 1927 } 1928 1929 dist1 = edge2->opos - edge1->opos; 1930 dist2 = edge3->opos - edge2->opos; 1931 1932 span = dist1 - dist2; 1933 if ( span < 0 ) 1934 span = -span; 1935 1936 if ( span < 8 ) 1937 { 1938 delta = edge3->pos - ( 2 * edge2->pos - edge1->pos ); 1939 edge3->pos -= delta; 1940 if ( edge3->link ) 1941 edge3->link->pos -= delta; 1942 1943 /* move the serifs along with the stem */ 1944 if ( n_edges == 12 ) 1945 { 1946 ( edges + 8 )->pos -= delta; 1947 ( edges + 11 )->pos -= delta; 1948 } 1949 1950 edge3->flags |= AF_EDGE_DONE; 1951 if ( edge3->link ) 1952 edge3->link->flags |= AF_EDGE_DONE; 1953 } 1954 } 1955 1956 if ( has_serifs || !anchor ) 1957 { 1958 /* 1959 * now hint the remaining edges (serifs and single) in order 1960 * to complete our processing 1961 */ 1962 for ( edge = edges; edge < edge_limit; edge++ ) 1963 { 1964 FT_Pos delta; 1965 1966 1967 if ( edge->flags & AF_EDGE_DONE ) 1968 continue; 1969 1970 delta = 1000; 1971 1972 if ( edge->serif ) 1973 { 1974 delta = edge->serif->opos - edge->opos; 1975 if ( delta < 0 ) 1976 delta = -delta; 1977 } 1978 1979 if ( delta < 64 + 16 ) 1980 { 1981 af_latin_align_serif_edge( hints, edge->serif, edge ); 1982 AF_LOG(( "SERIF: edge %d (opos=%.2f) serif to %d (opos=%.2f) " 1983 "aligned to (%.2f)\n", 1984 edge-edges, edge->opos / 64.0, 1985 edge->serif - edges, edge->serif->opos / 64.0, 1986 edge->pos / 64.0 )); 1987 } 1988 else if ( !anchor ) 1989 { 1990 AF_LOG(( "SERIF_ANCHOR: edge %d (opos=%.2f) snapped to (%.2f)\n", 1991 edge-edges, edge->opos / 64.0, edge->pos / 64.0 )); 1992 edge->pos = FT_PIX_ROUND( edge->opos ); 1993 anchor = edge; 1994 } 1995 else 1996 { 1997 AF_Edge before, after; 1998 1999 2000 for ( before = edge - 1; before >= edges; before-- ) 2001 if ( before->flags & AF_EDGE_DONE ) 2002 break; 2003 2004 for ( after = edge + 1; after < edge_limit; after++ ) 2005 if ( after->flags & AF_EDGE_DONE ) 2006 break; 2007 2008 if ( before >= edges && before < edge && 2009 after < edge_limit && after > edge ) 2010 { 2011 if ( after->opos == before->opos ) 2012 edge->pos = before->pos; 2013 else 2014 edge->pos = before->pos + 2015 FT_MulDiv( edge->opos - before->opos, 2016 after->pos - before->pos, 2017 after->opos - before->opos ); 2018 AF_LOG(( "SERIF_LINK1: edge %d (opos=%.2f) snapped to (%.2f) " 2019 "from %d (opos=%.2f)\n", 2020 edge-edges, edge->opos / 64.0, 2021 edge->pos / 64.0, before - edges, 2022 before->opos / 64.0 )); 2023 } 2024 else 2025 { 2026 edge->pos = anchor->pos + 2027 ( ( edge->opos - anchor->opos + 16 ) & ~31 ); 2028 AF_LOG(( "SERIF_LINK2: edge %d (opos=%.2f) snapped to (%.2f)\n", 2029 edge-edges, edge->opos / 64.0, edge->pos / 64.0 )); 2030 } 2031 } 2032 2033 edge->flags |= AF_EDGE_DONE; 2034 2035 if ( edge > edges && edge->pos < edge[-1].pos ) 2036 edge->pos = edge[-1].pos; 2037 2038 if ( edge + 1 < edge_limit && 2039 edge[1].flags & AF_EDGE_DONE && 2040 edge->pos > edge[1].pos ) 2041 edge->pos = edge[1].pos; 2042 } 2043 } 2044 } 2045 2046 2047 static FT_Error af_latin_hints_apply(AF_GlyphHints hints,FT_Outline * outline,AF_LatinMetrics metrics)2048 af_latin_hints_apply( AF_GlyphHints hints, 2049 FT_Outline* outline, 2050 AF_LatinMetrics metrics ) 2051 { 2052 FT_Error error; 2053 int dim; 2054 2055 2056 error = af_glyph_hints_reload( hints, outline, 1 ); 2057 if ( error ) 2058 goto Exit; 2059 2060 /* analyze glyph outline */ 2061 #ifdef AF_USE_WARPER 2062 if ( metrics->root.scaler.render_mode == FT_RENDER_MODE_LIGHT || 2063 AF_HINTS_DO_HORIZONTAL( hints ) ) 2064 #else 2065 if ( AF_HINTS_DO_HORIZONTAL( hints ) ) 2066 #endif 2067 { 2068 error = af_latin_hints_detect_features( hints, AF_DIMENSION_HORZ ); 2069 if ( error ) 2070 goto Exit; 2071 } 2072 2073 if ( AF_HINTS_DO_VERTICAL( hints ) ) 2074 { 2075 error = af_latin_hints_detect_features( hints, AF_DIMENSION_VERT ); 2076 if ( error ) 2077 goto Exit; 2078 2079 af_latin_hints_compute_blue_edges( hints, metrics ); 2080 } 2081 2082 /* grid-fit the outline */ 2083 for ( dim = 0; dim < AF_DIMENSION_MAX; dim++ ) 2084 { 2085 #ifdef AF_USE_WARPER 2086 if ( ( dim == AF_DIMENSION_HORZ && 2087 metrics->root.scaler.render_mode == FT_RENDER_MODE_LIGHT ) ) 2088 { 2089 AF_WarperRec warper; 2090 FT_Fixed scale; 2091 FT_Pos delta; 2092 2093 2094 af_warper_compute( &warper, hints, dim, &scale, &delta ); 2095 af_glyph_hints_scale_dim( hints, dim, scale, delta ); 2096 continue; 2097 } 2098 #endif 2099 2100 if ( ( dim == AF_DIMENSION_HORZ && AF_HINTS_DO_HORIZONTAL( hints ) ) || 2101 ( dim == AF_DIMENSION_VERT && AF_HINTS_DO_VERTICAL( hints ) ) ) 2102 { 2103 af_latin_hint_edges( hints, (AF_Dimension)dim ); 2104 af_glyph_hints_align_edge_points( hints, (AF_Dimension)dim ); 2105 af_glyph_hints_align_strong_points( hints, (AF_Dimension)dim ); 2106 af_glyph_hints_align_weak_points( hints, (AF_Dimension)dim ); 2107 } 2108 } 2109 af_glyph_hints_save( hints, outline ); 2110 2111 Exit: 2112 return error; 2113 } 2114 2115 2116 /*************************************************************************/ 2117 /*************************************************************************/ 2118 /***** *****/ 2119 /***** L A T I N S C R I P T C L A S S *****/ 2120 /***** *****/ 2121 /*************************************************************************/ 2122 /*************************************************************************/ 2123 2124 2125 /* XXX: this should probably fine tuned to differentiate better between */ 2126 /* scripts... */ 2127 2128 static const AF_Script_UniRangeRec af_latin_uniranges[] = 2129 { 2130 { 0x0020 , 0x007F }, /* Basic Latin (no control chars) */ 2131 { 0x00A0 , 0x00FF }, /* Latin-1 Supplement (no control chars) */ 2132 { 0x0100 , 0x017F }, /* Latin Extended-A */ 2133 { 0x0180 , 0x024F }, /* Latin Extended-B */ 2134 { 0x0250 , 0x02AF }, /* IPA Extensions */ 2135 { 0x02B0 , 0x02FF }, /* Spacing Modifier Letters */ 2136 { 0x0300 , 0x036F }, /* Combining Diacritical Marks */ 2137 { 0x0370 , 0x03FF }, /* Greek and Coptic */ 2138 { 0x0400 , 0x04FF }, /* Cyrillic */ 2139 { 0x0500 , 0x052F }, /* Cyrillic Supplement */ 2140 { 0x1D00 , 0x1D7F }, /* Phonetic Extensions */ 2141 { 0x1D80 , 0x1DBF }, /* Phonetic Extensions Supplement */ 2142 { 0x1DC0 , 0x1DFF }, /* Combining Diacritical Marks Supplement */ 2143 { 0x1E00 , 0x1EFF }, /* Latin Extended Additional */ 2144 { 0x1F00 , 0x1FFF }, /* Greek Extended */ 2145 { 0x2000 , 0x206F }, /* General Punctuation */ 2146 { 0x2070 , 0x209F }, /* Superscripts and Subscripts */ 2147 { 0x20A0 , 0x20CF }, /* Currency Symbols */ 2148 { 0x2150 , 0x218F }, /* Number Forms */ 2149 { 0x2460 , 0x24FF }, /* Enclosed Alphanumerics */ 2150 { 0x2C60 , 0x2C7F }, /* Latin Extended-C */ 2151 { 0x2DE0 , 0x2DFF }, /* Cyrillic Extended-A */ 2152 { 0xA640U , 0xA69FU }, /* Cyrillic Extended-B */ 2153 { 0xA720U , 0xA7FFU }, /* Latin Extended-D */ 2154 { 0xFB00U , 0xFB06U }, /* Alphab. Present. Forms (Latin Ligs) */ 2155 { 0x1D400UL, 0x1D7FFUL }, /* Mathematical Alphanumeric Symbols */ 2156 { 0 , 0 } 2157 }; 2158 2159 2160 FT_CALLBACK_TABLE_DEF const AF_ScriptClassRec 2161 af_latin_script_class = 2162 { 2163 AF_SCRIPT_LATIN, 2164 af_latin_uniranges, 2165 2166 sizeof( AF_LatinMetricsRec ), 2167 2168 (AF_Script_InitMetricsFunc) af_latin_metrics_init, 2169 (AF_Script_ScaleMetricsFunc)af_latin_metrics_scale, 2170 (AF_Script_DoneMetricsFunc) NULL, 2171 2172 (AF_Script_InitHintsFunc) af_latin_hints_init, 2173 (AF_Script_ApplyHintsFunc) af_latin_hints_apply 2174 }; 2175 2176 2177 /* END */ 2178