PageRenderTime 222ms CodeModel.GetById 116ms app.highlight 93ms RepoModel.GetById 1ms app.codeStats 1ms

/src/compiler/android-ndk/jni/freetype/src/autofit/aflatin.c

http://ftk.googlecode.com/
C | 2197 lines | 1485 code | 456 blank | 256 comment | 373 complexity | 6f0f01e906e36ee41a58b3988807c98a MD5 | raw file

Large files files are truncated, but you can click here to view the full file

   1/***************************************************************************/
   2/*                                                                         */
   3/*  aflatin.c                                                              */
   4/*                                                                         */
   5/*    Auto-fitter hinting routines for latin script (body).                */
   6/*                                                                         */
   7/*  Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009 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
  22#include "aflatin.h"
  23#include "aferrors.h"
  24
  25
  26#ifdef AF_USE_WARPER
  27#include "afwarp.h"
  28#endif
  29
  30
  31  /*************************************************************************/
  32  /*************************************************************************/
  33  /*****                                                               *****/
  34  /*****            L A T I N   G L O B A L   M E T R I C S            *****/
  35  /*****                                                               *****/
  36  /*************************************************************************/
  37  /*************************************************************************/
  38
  39  FT_LOCAL_DEF( void )
  40  af_latin_metrics_init_widths( AF_LatinMetrics  metrics,
  41                                FT_Face          face,
  42                                FT_ULong         charcode )
  43  {
  44    /* scan the array of segments in each direction */
  45    AF_GlyphHintsRec  hints[1];
  46
  47
  48    af_glyph_hints_init( hints, face->memory );
  49
  50    metrics->axis[AF_DIMENSION_HORZ].width_count = 0;
  51    metrics->axis[AF_DIMENSION_VERT].width_count = 0;
  52
  53    {
  54      FT_Error             error;
  55      FT_UInt              glyph_index;
  56      int                  dim;
  57      AF_LatinMetricsRec   dummy[1];
  58      AF_Scaler            scaler = &dummy->root.scaler;
  59
  60
  61      glyph_index = FT_Get_Char_Index( face, charcode );
  62      if ( glyph_index == 0 )
  63        goto Exit;
  64
  65      error = FT_Load_Glyph( face, glyph_index, FT_LOAD_NO_SCALE );
  66      if ( error || face->glyph->outline.n_points <= 0 )
  67        goto Exit;
  68
  69      FT_ZERO( dummy );
  70
  71      dummy->units_per_em = metrics->units_per_em;
  72      scaler->x_scale     = scaler->y_scale = 0x10000L;
  73      scaler->x_delta     = scaler->y_delta = 0;
  74      scaler->face        = face;
  75      scaler->render_mode = FT_RENDER_MODE_NORMAL;
  76      scaler->flags       = 0;
  77
  78      af_glyph_hints_rescale( hints, (AF_ScriptMetrics)dummy );
  79
  80      error = af_glyph_hints_reload( hints, &face->glyph->outline, 0 );
  81      if ( error )
  82        goto Exit;
  83
  84      for ( dim = 0; dim < AF_DIMENSION_MAX; dim++ )
  85      {
  86        AF_LatinAxis  axis    = &metrics->axis[dim];
  87        AF_AxisHints  axhints = &hints->axis[dim];
  88        AF_Segment    seg, limit, link;
  89        FT_UInt       num_widths = 0;
  90
  91
  92        error = af_latin_hints_compute_segments( hints,
  93                                                 (AF_Dimension)dim );
  94        if ( error )
  95          goto Exit;
  96
  97        af_latin_hints_link_segments( hints,
  98                                      (AF_Dimension)dim );
  99
 100        seg   = axhints->segments;
 101        limit = seg + axhints->num_segments;
 102
 103        for ( ; seg < limit; seg++ )
 104        {
 105          link = seg->link;
 106
 107          /* we only consider stem segments there! */
 108          if ( link && link->link == seg && link > seg )
 109          {
 110            FT_Pos  dist;
 111
 112
 113            dist = seg->pos - link->pos;
 114            if ( dist < 0 )
 115              dist = -dist;
 116
 117            if ( num_widths < AF_LATIN_MAX_WIDTHS )
 118              axis->widths[ num_widths++ ].org = dist;
 119          }
 120        }
 121
 122        af_sort_widths( num_widths, axis->widths );
 123        axis->width_count = num_widths;
 124      }
 125
 126  Exit:
 127      for ( dim = 0; dim < AF_DIMENSION_MAX; dim++ )
 128      {
 129        AF_LatinAxis  axis = &metrics->axis[dim];
 130        FT_Pos        stdw;
 131
 132
 133        stdw = ( axis->width_count > 0 )
 134                 ? axis->widths[0].org
 135                 : AF_LATIN_CONSTANT( metrics, 50 );
 136
 137        /* let's try 20% of the smallest width */
 138        axis->edge_distance_threshold = stdw / 5;
 139        axis->standard_width          = stdw;
 140        axis->extra_light             = 0;
 141      }
 142    }
 143
 144    af_glyph_hints_done( hints );
 145  }
 146
 147
 148
 149#define AF_LATIN_MAX_TEST_CHARACTERS  12
 150
 151
 152  static const char af_latin_blue_chars[AF_LATIN_MAX_BLUES]
 153                                       [AF_LATIN_MAX_TEST_CHARACTERS + 1] =
 154  {
 155    "THEZOCQS",
 156    "HEZLOCUS",
 157    "fijkdbh",
 158    "xzroesc",
 159    "xzroesc",
 160    "pqgjy"
 161  };
 162
 163
 164  static void
 165  af_latin_metrics_init_blues( AF_LatinMetrics  metrics,
 166                               FT_Face          face )
 167  {
 168    FT_Pos        flats [AF_LATIN_MAX_TEST_CHARACTERS];
 169    FT_Pos        rounds[AF_LATIN_MAX_TEST_CHARACTERS];
 170    FT_Int        num_flats;
 171    FT_Int        num_rounds;
 172    FT_Int        bb;
 173    AF_LatinBlue  blue;
 174    FT_Error      error;
 175    AF_LatinAxis  axis  = &metrics->axis[AF_DIMENSION_VERT];
 176    FT_GlyphSlot  glyph = face->glyph;
 177
 178
 179    /* we compute the blues simply by loading each character from the    */
 180    /* 'af_latin_blue_chars[blues]' string, then compute its top-most or */
 181    /* bottom-most points (depending on `AF_IS_TOP_BLUE')                */
 182
 183    AF_LOG(( "blue zones computation\n" ));
 184    AF_LOG(( "------------------------------------------------\n" ));
 185
 186    for ( bb = 0; bb < AF_LATIN_BLUE_MAX; bb++ )
 187    {
 188      const char*  p     = af_latin_blue_chars[bb];
 189      const char*  limit = p + AF_LATIN_MAX_TEST_CHARACTERS;
 190      FT_Pos*      blue_ref;
 191      FT_Pos*      blue_shoot;
 192
 193
 194      AF_LOG(( "blue %3d: ", bb ));
 195
 196      num_flats  = 0;
 197      num_rounds = 0;
 198
 199      for ( ; p < limit && *p; p++ )
 200      {
 201        FT_UInt     glyph_index;
 202        FT_Pos      best_y; /* same as points.y */
 203        FT_Int      best_point, best_first, best_last;
 204        FT_Vector*  points;
 205        FT_Bool     round = 0;
 206
 207
 208        AF_LOG(( "'%c'", *p ));
 209
 210        /* load the character in the face -- skip unknown or empty ones */
 211        glyph_index = FT_Get_Char_Index( face, (FT_UInt)*p );
 212        if ( glyph_index == 0 )
 213          continue;
 214
 215        error = FT_Load_Glyph( face, glyph_index, FT_LOAD_NO_SCALE );
 216        if ( error || glyph->outline.n_points <= 0 )
 217          continue;
 218
 219        /* now compute min or max point indices and coordinates */
 220        points      = glyph->outline.points;
 221        best_point  = -1;
 222        best_y      = 0;  /* make compiler happy */
 223        best_first  = 0;  /* ditto */
 224        best_last   = 0;  /* ditto */
 225
 226        {
 227          FT_Int  nn;
 228          FT_Int  first = 0;
 229          FT_Int  last  = -1;
 230
 231
 232          for ( nn = 0; nn < glyph->outline.n_contours; first = last+1, nn++ )
 233          {
 234            FT_Int  old_best_point = best_point;
 235            FT_Int  pp;
 236
 237
 238            last = glyph->outline.contours[nn];
 239
 240            /* Avoid single-point contours since they are never rasterized. */
 241            /* In some fonts, they correspond to mark attachment points     */
 242            /* which are way outside of the glyph's real outline.           */
 243            if ( last <= first )
 244                continue;
 245
 246            if ( AF_LATIN_IS_TOP_BLUE( bb ) )
 247            {
 248              for ( pp = first; pp <= last; pp++ )
 249                if ( best_point < 0 || points[pp].y > best_y )
 250                {
 251                  best_point = pp;
 252                  best_y     = points[pp].y;
 253                }
 254            }
 255            else
 256            {
 257              for ( pp = first; pp <= last; pp++ )
 258                if ( best_point < 0 || points[pp].y < best_y )
 259                {
 260                  best_point = pp;
 261                  best_y     = points[pp].y;
 262                }
 263            }
 264
 265            if ( best_point != old_best_point )
 266            {
 267              best_first = first;
 268              best_last  = last;
 269            }
 270          }
 271          AF_LOG(( "%5d", best_y ));
 272        }
 273
 274        /* now check whether the point belongs to a straight or round   */
 275        /* segment; we first need to find in which contour the extremum */
 276        /* lies, then inspect its previous and next points              */
 277        if ( best_point >= 0 )
 278        {
 279          FT_Int  prev, next;
 280          FT_Pos  dist;
 281
 282
 283          /* now look for the previous and next points that are not on the */
 284          /* same Y coordinate.  Threshold the `closeness'...              */
 285          prev = best_point;
 286          next = prev;
 287
 288          do
 289          {
 290            if ( prev > best_first )
 291              prev--;
 292            else
 293              prev = best_last;
 294
 295            dist = points[prev].y - best_y;
 296            if ( dist < -5 || dist > 5 )
 297              break;
 298
 299          } while ( prev != best_point );
 300
 301          do
 302          {
 303            if ( next < best_last )
 304              next++;
 305            else
 306              next = best_first;
 307
 308            dist = points[next].y - best_y;
 309            if ( dist < -5 || dist > 5 )
 310              break;
 311
 312          } while ( next != best_point );
 313
 314          /* now, set the `round' flag depending on the segment's kind */
 315          round = FT_BOOL(
 316            FT_CURVE_TAG( glyph->outline.tags[prev] ) != FT_CURVE_TAG_ON ||
 317            FT_CURVE_TAG( glyph->outline.tags[next] ) != FT_CURVE_TAG_ON );
 318
 319          AF_LOG(( "%c ", round ? 'r' : 'f' ));
 320        }
 321
 322        if ( round )
 323          rounds[num_rounds++] = best_y;
 324        else
 325          flats[num_flats++]   = best_y;
 326      }
 327
 328      AF_LOG(( "\n" ));
 329
 330      if ( num_flats == 0 && num_rounds == 0 )
 331      {
 332        /*
 333         *  we couldn't find a single glyph to compute this blue zone,
 334         *  we will simply ignore it then
 335         */
 336        AF_LOG(( "empty\n" ));
 337        continue;
 338      }
 339
 340      /* we have computed the contents of the `rounds' and `flats' tables, */
 341      /* now determine the reference and overshoot position of the blue -- */
 342      /* we simply take the median value after a simple sort               */
 343      af_sort_pos( num_rounds, rounds );
 344      af_sort_pos( num_flats,  flats );
 345
 346      blue       = & axis->blues[axis->blue_count];
 347      blue_ref   = & blue->ref.org;
 348      blue_shoot = & blue->shoot.org;
 349
 350      axis->blue_count++;
 351
 352      if ( num_flats == 0 )
 353      {
 354        *blue_ref   =
 355        *blue_shoot = rounds[num_rounds / 2];
 356      }
 357      else if ( num_rounds == 0 )
 358      {
 359        *blue_ref   =
 360        *blue_shoot = flats[num_flats / 2];
 361      }
 362      else
 363      {
 364        *blue_ref   = flats[num_flats / 2];
 365        *blue_shoot = rounds[num_rounds / 2];
 366      }
 367
 368      /* there are sometimes problems: if the overshoot position of top     */
 369      /* zones is under its reference position, or the opposite for bottom  */
 370      /* zones.  We must thus check everything there and correct the errors */
 371      if ( *blue_shoot != *blue_ref )
 372      {
 373        FT_Pos   ref      = *blue_ref;
 374        FT_Pos   shoot    = *blue_shoot;
 375        FT_Bool  over_ref = FT_BOOL( shoot > ref );
 376
 377
 378        if ( AF_LATIN_IS_TOP_BLUE( bb ) ^ over_ref )
 379          *blue_shoot = *blue_ref = ( shoot + ref ) / 2;
 380      }
 381
 382      blue->flags = 0;
 383      if ( AF_LATIN_IS_TOP_BLUE( bb ) )
 384        blue->flags |= AF_LATIN_BLUE_TOP;
 385
 386      /*
 387       * The following flag is used later to adjust the y and x scales
 388       * in order to optimize the pixel grid alignment of the top of small
 389       * letters.
 390       */
 391      if ( bb == AF_LATIN_BLUE_SMALL_TOP )
 392        blue->flags |= AF_LATIN_BLUE_ADJUSTMENT;
 393
 394      AF_LOG(( "-- ref = %ld, shoot = %ld\n", *blue_ref, *blue_shoot ));
 395    }
 396
 397    return;
 398  }
 399
 400
 401  FT_LOCAL_DEF( void )
 402  af_latin_metrics_check_digits( AF_LatinMetrics  metrics,
 403                                 FT_Face          face )
 404  {
 405    FT_UInt   i;
 406    FT_Bool   started = 0, same_width = 1;
 407    FT_Fixed  advance, old_advance = 0;
 408
 409
 410    /* check whether all ASCII digits have the same advance width; */
 411    /* digit `0' is 0x30 in all supported charmaps                 */
 412    for ( i = 0x30; i <= 0x39; i++ )
 413    {
 414      FT_UInt  glyph_index;
 415
 416
 417      glyph_index = FT_Get_Char_Index( face, i );
 418      if ( glyph_index == 0 )
 419        continue;
 420
 421      if ( FT_Get_Advance( face, glyph_index,
 422                           FT_LOAD_NO_SCALE         |
 423                           FT_LOAD_NO_HINTING       |
 424                           FT_LOAD_IGNORE_TRANSFORM,
 425                           &advance ) )
 426        continue;
 427
 428      if ( started )
 429      {
 430        if ( advance != old_advance )
 431        {
 432          same_width = 0;
 433          break;
 434        }
 435      }
 436      else
 437      {
 438        old_advance = advance;
 439        started     = 1;
 440      }
 441    }
 442
 443    metrics->root.digits_have_same_width = same_width;
 444  }
 445
 446
 447  FT_LOCAL_DEF( FT_Error )
 448  af_latin_metrics_init( AF_LatinMetrics  metrics,
 449                         FT_Face          face )
 450  {
 451    FT_Error    error = AF_Err_Ok;
 452    FT_CharMap  oldmap = face->charmap;
 453    FT_UInt     ee;
 454
 455    static const FT_Encoding  latin_encodings[] =
 456    {
 457      FT_ENCODING_UNICODE,
 458      FT_ENCODING_APPLE_ROMAN,
 459      FT_ENCODING_ADOBE_STANDARD,
 460      FT_ENCODING_ADOBE_LATIN_1,
 461      FT_ENCODING_NONE  /* end of list */
 462    };
 463
 464
 465    metrics->units_per_em = face->units_per_EM;
 466
 467    /* do we have a latin charmap in there? */
 468    for ( ee = 0; latin_encodings[ee] != FT_ENCODING_NONE; ee++ )
 469    {
 470      error = FT_Select_Charmap( face, latin_encodings[ee] );
 471      if ( !error )
 472        break;
 473    }
 474
 475    if ( !error )
 476    {
 477      /* For now, compute the standard width and height from the `o'. */
 478      af_latin_metrics_init_widths( metrics, face, 'o' );
 479      af_latin_metrics_init_blues( metrics, face );
 480      af_latin_metrics_check_digits( metrics, face );
 481    }
 482
 483    FT_Set_Charmap( face, oldmap );
 484    return AF_Err_Ok;
 485  }
 486
 487
 488  static void
 489  af_latin_metrics_scale_dim( AF_LatinMetrics  metrics,
 490                              AF_Scaler        scaler,
 491                              AF_Dimension     dim )
 492  {
 493    FT_Fixed      scale;
 494    FT_Pos        delta;
 495    AF_LatinAxis  axis;
 496    FT_UInt       nn;
 497
 498
 499    if ( dim == AF_DIMENSION_HORZ )
 500    {
 501      scale = scaler->x_scale;
 502      delta = scaler->x_delta;
 503    }
 504    else
 505    {
 506      scale = scaler->y_scale;
 507      delta = scaler->y_delta;
 508    }
 509
 510    axis = &metrics->axis[dim];
 511
 512    if ( axis->org_scale == scale && axis->org_delta == delta )
 513      return;
 514
 515    axis->org_scale = scale;
 516    axis->org_delta = delta;
 517
 518    /*
 519     * correct X and Y scale to optimize the alignment of the top of small
 520     * letters to the pixel grid
 521     */
 522    {
 523      AF_LatinAxis  Axis = &metrics->axis[AF_DIMENSION_VERT];
 524      AF_LatinBlue  blue = NULL;
 525
 526
 527      for ( nn = 0; nn < Axis->blue_count; nn++ )
 528      {
 529        if ( Axis->blues[nn].flags & AF_LATIN_BLUE_ADJUSTMENT )
 530        {
 531          blue = &Axis->blues[nn];
 532          break;
 533        }
 534      }
 535
 536      if ( blue )
 537      {
 538        FT_Pos  scaled = FT_MulFix( blue->shoot.org, scaler->y_scale );
 539        FT_Pos  fitted = ( scaled + 40 ) & ~63;
 540
 541
 542        if ( scaled != fitted )
 543        {
 544#if 0
 545          if ( dim == AF_DIMENSION_HORZ )
 546          {
 547            if ( fitted < scaled )
 548              scale -= scale / 50;  /* scale *= 0.98 */
 549          }
 550          else
 551#endif
 552          if ( dim == AF_DIMENSION_VERT )
 553          {
 554            scale = FT_MulDiv( scale, fitted, scaled );
 555          }
 556        }
 557      }
 558    }
 559
 560    axis->scale = scale;
 561    axis->delta = delta;
 562
 563    if ( dim == AF_DIMENSION_HORZ )
 564    {
 565      metrics->root.scaler.x_scale = scale;
 566      metrics->root.scaler.x_delta = delta;
 567    }
 568    else
 569    {
 570      metrics->root.scaler.y_scale = scale;
 571      metrics->root.scaler.y_delta = delta;
 572    }
 573
 574    /* scale the standard widths */
 575    for ( nn = 0; nn < axis->width_count; nn++ )
 576    {
 577      AF_Width  width = axis->widths + nn;
 578
 579
 580      width->cur = FT_MulFix( width->org, scale );
 581      width->fit = width->cur;
 582    }
 583
 584    /* an extra-light axis corresponds to a standard width that is */
 585    /* smaller than 0.75 pixels                                    */
 586    axis->extra_light =
 587      (FT_Bool)( FT_MulFix( axis->standard_width, scale ) < 32 + 8 );
 588
 589    if ( dim == AF_DIMENSION_VERT )
 590    {
 591      /* scale the blue zones */
 592      for ( nn = 0; nn < axis->blue_count; nn++ )
 593      {
 594        AF_LatinBlue  blue = &axis->blues[nn];
 595        FT_Pos        dist;
 596
 597
 598        blue->ref.cur   = FT_MulFix( blue->ref.org, scale ) + delta;
 599        blue->ref.fit   = blue->ref.cur;
 600        blue->shoot.cur = FT_MulFix( blue->shoot.org, scale ) + delta;
 601        blue->shoot.fit = blue->shoot.cur;
 602        blue->flags    &= ~AF_LATIN_BLUE_ACTIVE;
 603
 604        /* a blue zone is only active if it is less than 3/4 pixels tall */
 605        dist = FT_MulFix( blue->ref.org - blue->shoot.org, scale );
 606        if ( dist <= 48 && dist >= -48 )
 607        {
 608          FT_Pos  delta1, delta2;
 609
 610
 611          delta1 = blue->shoot.org - blue->ref.org;
 612          delta2 = delta1;
 613          if ( delta1 < 0 )
 614            delta2 = -delta2;
 615
 616          delta2 = FT_MulFix( delta2, scale );
 617
 618          if ( delta2 < 32 )
 619            delta2 = 0;
 620          else if ( delta2 < 64 )
 621            delta2 = 32 + ( ( ( delta2 - 32 ) + 16 ) & ~31 );
 622          else
 623            delta2 = FT_PIX_ROUND( delta2 );
 624
 625          if ( delta1 < 0 )
 626            delta2 = -delta2;
 627
 628          blue->ref.fit   = FT_PIX_ROUND( blue->ref.cur );
 629          blue->shoot.fit = blue->ref.fit + delta2;
 630
 631          blue->flags |= AF_LATIN_BLUE_ACTIVE;
 632        }
 633      }
 634    }
 635  }
 636
 637
 638  FT_LOCAL_DEF( void )
 639  af_latin_metrics_scale( AF_LatinMetrics  metrics,
 640                          AF_Scaler        scaler )
 641  {
 642    metrics->root.scaler.render_mode = scaler->render_mode;
 643    metrics->root.scaler.face        = scaler->face;
 644
 645    af_latin_metrics_scale_dim( metrics, scaler, AF_DIMENSION_HORZ );
 646    af_latin_metrics_scale_dim( metrics, scaler, AF_DIMENSION_VERT );
 647  }
 648
 649
 650  /*************************************************************************/
 651  /*************************************************************************/
 652  /*****                                                               *****/
 653  /*****           L A T I N   G L Y P H   A N A L Y S I S             *****/
 654  /*****                                                               *****/
 655  /*************************************************************************/
 656  /*************************************************************************/
 657
 658  FT_LOCAL_DEF( FT_Error )
 659  af_latin_hints_compute_segments( AF_GlyphHints  hints,
 660                                   AF_Dimension   dim )
 661  {
 662    AF_AxisHints  axis          = &hints->axis[dim];
 663    FT_Memory     memory        = hints->memory;
 664    FT_Error      error         = AF_Err_Ok;
 665    AF_Segment    segment       = NULL;
 666    AF_SegmentRec seg0;
 667    AF_Point*     contour       = hints->contours;
 668    AF_Point*     contour_limit = contour + hints->num_contours;
 669    AF_Direction  major_dir, segment_dir;
 670
 671
 672    FT_ZERO( &seg0 );
 673    seg0.score = 32000;
 674    seg0.flags = AF_EDGE_NORMAL;
 675
 676    major_dir   = (AF_Direction)FT_ABS( axis->major_dir );
 677    segment_dir = major_dir;
 678
 679    axis->num_segments = 0;
 680
 681    /* set up (u,v) in each point */
 682    if ( dim == AF_DIMENSION_HORZ )
 683    {
 684      AF_Point  point = hints->points;
 685      AF_Point  limit = point + hints->num_points;
 686
 687
 688      for ( ; point < limit; point++ )
 689      {
 690        point->u = point->fx;
 691        point->v = point->fy;
 692      }
 693    }
 694    else
 695    {
 696      AF_Point  point = hints->points;
 697      AF_Point  limit = point + hints->num_points;
 698
 699
 700      for ( ; point < limit; point++ )
 701      {
 702        point->u = point->fy;
 703        point->v = point->fx;
 704      }
 705    }
 706
 707    /* do each contour separately */
 708    for ( ; contour < contour_limit; contour++ )
 709    {
 710      AF_Point  point   =  contour[0];
 711      AF_Point  last    =  point->prev;
 712      int       on_edge =  0;
 713      FT_Pos    min_pos =  32000;  /* minimum segment pos != min_coord */
 714      FT_Pos    max_pos = -32000;  /* maximum segment pos != max_coord */
 715      FT_Bool   passed;
 716
 717
 718      if ( point == last )  /* skip singletons -- just in case */
 719        continue;
 720
 721      if ( FT_ABS( last->out_dir )  == major_dir &&
 722           FT_ABS( point->out_dir ) == major_dir )
 723      {
 724        /* we are already on an edge, try to locate its start */
 725        last = point;
 726
 727        for (;;)
 728        {
 729          point = point->prev;
 730          if ( FT_ABS( point->out_dir ) != major_dir )
 731          {
 732            point = point->next;
 733            break;
 734          }
 735          if ( point == last )
 736            break;
 737        }
 738      }
 739
 740      last   = point;
 741      passed = 0;
 742
 743      for (;;)
 744      {
 745        FT_Pos  u, v;
 746
 747
 748        if ( on_edge )
 749        {
 750          u = point->u;
 751          if ( u < min_pos )
 752            min_pos = u;
 753          if ( u > max_pos )
 754            max_pos = u;
 755
 756          if ( point->out_dir != segment_dir || point == last )
 757          {
 758            /* we are just leaving an edge; record a new segment! */
 759            segment->last = point;
 760            segment->pos  = (FT_Short)( ( min_pos + max_pos ) >> 1 );
 761
 762            /* a segment is round if either its first or last point */
 763            /* is a control point                                   */
 764            if ( ( segment->first->flags | point->flags ) &
 765                   AF_FLAG_CONTROL                        )
 766              segment->flags |= AF_EDGE_ROUND;
 767
 768            /* compute segment size */
 769            min_pos = max_pos = point->v;
 770
 771            v = segment->first->v;
 772            if ( v < min_pos )
 773              min_pos = v;
 774            if ( v > max_pos )
 775              max_pos = v;
 776
 777            segment->min_coord = (FT_Short)min_pos;
 778            segment->max_coord = (FT_Short)max_pos;
 779            segment->height    = (FT_Short)( segment->max_coord -
 780                                             segment->min_coord );
 781
 782            on_edge = 0;
 783            segment = NULL;
 784            /* fallthrough */
 785          }
 786        }
 787
 788        /* now exit if we are at the start/end point */
 789        if ( point == last )
 790        {
 791          if ( passed )
 792            break;
 793          passed = 1;
 794        }
 795
 796        if ( !on_edge && FT_ABS( point->out_dir ) == major_dir )
 797        {
 798          /* this is the start of a new segment! */
 799          segment_dir = (AF_Direction)point->out_dir;
 800
 801          /* clear all segment fields */
 802          error = af_axis_hints_new_segment( axis, memory, &segment );
 803          if ( error )
 804            goto Exit;
 805
 806          segment[0]        = seg0;
 807          segment->dir      = (FT_Char)segment_dir;
 808          min_pos = max_pos = point->u;
 809          segment->first    = point;
 810          segment->last     = point;
 811          segment->contour  = contour;
 812          on_edge           = 1;
 813        }
 814
 815        point = point->next;
 816      }
 817
 818    } /* contours */
 819
 820
 821    /* now slightly increase the height of segments when this makes */
 822    /* sense -- this is used to better detect and ignore serifs     */
 823    {
 824      AF_Segment  segments     = axis->segments;
 825      AF_Segment  segments_end = segments + axis->num_segments;
 826
 827
 828      for ( segment = segments; segment < segments_end; segment++ )
 829      {
 830        AF_Point  first   = segment->first;
 831        AF_Point  last    = segment->last;
 832        FT_Pos    first_v = first->v;
 833        FT_Pos    last_v  = last->v;
 834
 835
 836        if ( first == last )
 837          continue;
 838
 839        if ( first_v < last_v )
 840        {
 841          AF_Point  p;
 842
 843
 844          p = first->prev;
 845          if ( p->v < first_v )
 846            segment->height = (FT_Short)( segment->height +
 847                                          ( ( first_v - p->v ) >> 1 ) );
 848
 849          p = last->next;
 850          if ( p->v > last_v )
 851            segment->height = (FT_Short)( segment->height +
 852                                          ( ( p->v - last_v ) >> 1 ) );
 853        }
 854        else
 855        {
 856          AF_Point  p;
 857
 858
 859          p = first->prev;
 860          if ( p->v > first_v )
 861            segment->height = (FT_Short)( segment->height +
 862                                          ( ( p->v - first_v ) >> 1 ) );
 863
 864          p = last->next;
 865          if ( p->v < last_v )
 866            segment->height = (FT_Short)( segment->height +
 867                                          ( ( last_v - p->v ) >> 1 ) );
 868        }
 869      }
 870    }
 871
 872  Exit:
 873    return error;
 874  }
 875
 876
 877  FT_LOCAL_DEF( void )
 878  af_latin_hints_link_segments( AF_GlyphHints  hints,
 879                                AF_Dimension   dim )
 880  {
 881    AF_AxisHints  axis          = &hints->axis[dim];
 882    AF_Segment    segments      = axis->segments;
 883    AF_Segment    segment_limit = segments + axis->num_segments;
 884    FT_Pos        len_threshold, len_score;
 885    AF_Segment    seg1, seg2;
 886
 887
 888    len_threshold = AF_LATIN_CONSTANT( hints->metrics, 8 );
 889    if ( len_threshold == 0 )
 890      len_threshold = 1;
 891
 892    len_score = AF_LATIN_CONSTANT( hints->metrics, 6000 );
 893
 894    /* now compare each segment to the others */
 895    for ( seg1 = segments; seg1 < segment_limit; seg1++ )
 896    {
 897      /* the fake segments are introduced to hint the metrics -- */
 898      /* we must never link them to anything                     */
 899      if ( seg1->dir != axis->major_dir || seg1->first == seg1->last )
 900        continue;
 901
 902      for ( seg2 = segments; seg2 < segment_limit; seg2++ )
 903        if ( seg1->dir + seg2->dir == 0 && seg2->pos > seg1->pos )
 904        {
 905          FT_Pos  pos1 = seg1->pos;
 906          FT_Pos  pos2 = seg2->pos;
 907          FT_Pos  dist = pos2 - pos1;
 908
 909
 910          if ( dist < 0 )
 911            dist = -dist;
 912
 913          {
 914            FT_Pos  min = seg1->min_coord;
 915            FT_Pos  max = seg1->max_coord;
 916            FT_Pos  len, score;
 917
 918
 919            if ( min < seg2->min_coord )
 920              min = seg2->min_coord;
 921
 922            if ( max > seg2->max_coord )
 923              max = seg2->max_coord;
 924
 925            len = max - min;
 926            if ( len >= len_threshold )
 927            {
 928              score = dist + len_score / len;
 929
 930              if ( score < seg1->score )
 931              {
 932                seg1->score = score;
 933                seg1->link  = seg2;
 934              }
 935
 936              if ( score < seg2->score )
 937              {
 938                seg2->score = score;
 939                seg2->link  = seg1;
 940              }
 941            }
 942          }
 943        }
 944    }
 945
 946    /* now, compute the `serif' segments */
 947    for ( seg1 = segments; seg1 < segment_limit; seg1++ )
 948    {
 949      seg2 = seg1->link;
 950
 951      if ( seg2 )
 952      {
 953        if ( seg2->link != seg1 )
 954        {
 955          seg1->link  = 0;
 956          seg1->serif = seg2->link;
 957        }
 958      }
 959    }
 960  }
 961
 962
 963  FT_LOCAL_DEF( FT_Error )
 964  af_latin_hints_compute_edges( AF_GlyphHints  hints,
 965                                AF_Dimension   dim )
 966  {
 967    AF_AxisHints  axis   = &hints->axis[dim];
 968    FT_Error      error  = AF_Err_Ok;
 969    FT_Memory     memory = hints->memory;
 970    AF_LatinAxis  laxis  = &((AF_LatinMetrics)hints->metrics)->axis[dim];
 971
 972    AF_Segment    segments      = axis->segments;
 973    AF_Segment    segment_limit = segments + axis->num_segments;
 974    AF_Segment    seg;
 975
 976    AF_Direction  up_dir;
 977    FT_Fixed      scale;
 978    FT_Pos        edge_distance_threshold;
 979    FT_Pos        segment_length_threshold;
 980
 981
 982    axis->num_edges = 0;
 983
 984    scale = ( dim == AF_DIMENSION_HORZ ) ? hints->x_scale
 985                                         : hints->y_scale;
 986
 987    up_dir = ( dim == AF_DIMENSION_HORZ ) ? AF_DIR_UP
 988                                          : AF_DIR_RIGHT;
 989
 990    /*
 991     *  We ignore all segments that are less than 1 pixels in length,
 992     *  to avoid many problems with serif fonts.  We compute the
 993     *  corresponding threshold in font units.
 994     */
 995    if ( dim == AF_DIMENSION_HORZ )
 996        segment_length_threshold = FT_DivFix( 64, hints->y_scale );
 997    else
 998        segment_length_threshold = 0;
 999
1000    /*********************************************************************/
1001    /*                                                                   */
1002    /* We will begin by generating a sorted table of edges for the       */
1003    /* current direction.  To do so, we simply scan each segment and try */
1004    /* to find an edge in our table that corresponds to its position.    */
1005    /*                                                                   */
1006    /* If no edge is found, we create and insert a new edge in the       */
1007    /* sorted table.  Otherwise, we simply add the segment to the edge's */
1008    /* list which will be processed in the second step to compute the    */
1009    /* edge's properties.                                                */
1010    /*                                                                   */
1011    /* Note that the edges table is sorted along the segment/edge        */
1012    /* position.                                                         */
1013    /*                                                                   */
1014    /*********************************************************************/
1015
1016    edge_distance_threshold = FT_MulFix( laxis->edge_distance_threshold,
1017                                         scale );
1018    if ( edge_distance_threshold > 64 / 4 )
1019      edge_distance_threshold = 64 / 4;
1020
1021    edge_distance_threshold = FT_DivFix( edge_distance_threshold,
1022                                         scale );
1023
1024    for ( seg = segments; seg < segment_limit; seg++ )
1025    {
1026      AF_Edge  found = 0;
1027      FT_Int   ee;
1028
1029
1030      if ( seg->height < segment_length_threshold )
1031        continue;
1032
1033      /* A special case for serif edges: If they are smaller than */
1034      /* 1.5 pixels we ignore them.                               */
1035      if ( seg->serif                                     &&
1036           2 * seg->height < 3 * segment_length_threshold )
1037        continue;
1038
1039      /* look for an edge corresponding to the segment */
1040      for ( ee = 0; ee < axis->num_edges; ee++ )
1041      {
1042        AF_Edge  edge = axis->edges + ee;
1043        FT_Pos   dist;
1044
1045
1046        dist = seg->pos - edge->fpos;
1047        if ( dist < 0 )
1048          dist = -dist;
1049
1050        if ( dist < edge_distance_threshold && edge->dir == seg->dir )
1051        {
1052          found = edge;
1053          break;
1054        }
1055      }
1056
1057      if ( !found )
1058      {
1059        AF_Edge  edge;
1060
1061
1062        /* insert a new edge in the list and */
1063        /* sort according to the position    */
1064        error = af_axis_hints_new_edge( axis, seg->pos,
1065                                        (AF_Direction)seg->dir,
1066                                        memory, &edge );
1067        if ( error )
1068          goto Exit;
1069
1070        /* add the segment to the new edge's list */
1071        FT_ZERO( edge );
1072
1073        edge->first    = seg;
1074        edge->last     = seg;
1075        edge->fpos     = seg->pos;
1076        edge->dir      = seg->dir;
1077        edge->opos     = edge->pos = FT_MulFix( seg->pos, scale );
1078        seg->edge_next = seg;
1079      }
1080      else
1081      {
1082        /* if an edge was found, simply add the segment to the edge's */
1083        /* list                                                       */
1084        seg->edge_next         = found->first;
1085        found->last->edge_next = seg;
1086        found->last            = seg;
1087      }
1088    }
1089
1090
1091    /*********************************************************************/
1092    /*                                                                   */
1093    /* Good, we will now compute each edge's properties according to     */
1094    /* segments found on its position.  Basically, these are:            */
1095    /*                                                                   */
1096    /*  - edge's main direction                                          */
1097    /*  - stem edge, serif edge or both (which defaults to stem then)    */
1098    /*  - rounded edge, straight or both (which defaults to straight)    */
1099    /*  - link for edge                                                  */
1100    /*                                                                   */
1101    /*********************************************************************/
1102
1103    /* first of all, set the `edge' field in each segment -- this is */
1104    /* required in order to compute edge links                       */
1105
1106    /*
1107     * Note that removing this loop and setting the `edge' field of each
1108     * segment directly in the code above slows down execution speed for
1109     * some reasons on platforms like the Sun.
1110     */
1111    {
1112      AF_Edge  edges      = axis->edges;
1113      AF_Edge  edge_limit = edges + axis->num_edges;
1114      AF_Edge  edge;
1115
1116
1117      for ( edge = edges; edge < edge_limit; edge++ )
1118      {
1119        seg = edge->first;
1120        if ( seg )
1121          do
1122          {
1123            seg->edge = edge;
1124            seg       = seg->edge_next;
1125
1126          } while ( seg != edge->first );
1127      }
1128
1129      /* now, compute each edge properties */
1130      for ( edge = edges; edge < edge_limit; edge++ )
1131      {
1132        FT_Int  is_round    = 0;  /* does it contain round segments?    */
1133        FT_Int  is_straight = 0;  /* does it contain straight segments? */
1134        FT_Pos  ups         = 0;  /* number of upwards segments         */
1135        FT_Pos  downs       = 0;  /* number of downwards segments       */
1136
1137
1138        seg = edge->first;
1139
1140        do
1141        {
1142          FT_Bool  is_serif;
1143
1144
1145          /* check for roundness of segment */
1146          if ( seg->flags & AF_EDGE_ROUND )
1147            is_round++;
1148          else
1149            is_straight++;
1150
1151          /* check for segment direction */
1152          if ( seg->dir == up_dir )
1153            ups   += seg->max_coord-seg->min_coord;
1154          else
1155            downs += seg->max_coord-seg->min_coord;
1156
1157          /* check for links -- if seg->serif is set, then seg->link must */
1158          /* be ignored                                                   */
1159          is_serif = (FT_Bool)( seg->serif               &&
1160                                seg->serif->edge         &&
1161                                seg->serif->edge != edge );
1162
1163          if ( ( seg->link && seg->link->edge != NULL ) || is_serif )
1164          {
1165            AF_Edge     edge2;
1166            AF_Segment  seg2;
1167
1168
1169            edge2 = edge->link;
1170            seg2  = seg->link;
1171
1172            if ( is_serif )
1173            {
1174              seg2  = seg->serif;
1175              edge2 = edge->serif;
1176            }
1177
1178            if ( edge2 )
1179            {
1180              FT_Pos  edge_delta;
1181              FT_Pos  seg_delta;
1182
1183
1184              edge_delta = edge->fpos - edge2->fpos;
1185              if ( edge_delta < 0 )
1186                edge_delta = -edge_delta;
1187
1188              seg_delta = seg->pos - seg2->pos;
1189              if ( seg_delta < 0 )
1190                seg_delta = -seg_delta;
1191
1192              if ( seg_delta < edge_delta )
1193                edge2 = seg2->edge;
1194            }
1195            else
1196              edge2 = seg2->edge;
1197
1198            if ( is_serif )
1199            {
1200              edge->serif   = edge2;
1201              edge2->flags |= AF_EDGE_SERIF;
1202            }
1203            else
1204              edge->link  = edge2;
1205          }
1206
1207          seg = seg->edge_next;
1208
1209        } while ( seg != edge->first );
1210
1211        /* set the round/straight flags */
1212        edge->flags = AF_EDGE_NORMAL;
1213
1214        if ( is_round > 0 && is_round >= is_straight )
1215          edge->flags |= AF_EDGE_ROUND;
1216
1217#if 0
1218        /* set the edge's main direction */
1219        edge->dir = AF_DIR_NONE;
1220
1221        if ( ups > downs )
1222          edge->dir = (FT_Char)up_dir;
1223
1224        else if ( ups < downs )
1225          edge->dir = (FT_Char)-up_dir;
1226
1227        else if ( ups == downs )
1228          edge->dir = 0;  /* both up and down! */
1229#endif
1230
1231        /* gets rid of serifs if link is set                */
1232        /* XXX: This gets rid of many unpleasant artefacts! */
1233        /*      Example: the `c' in cour.pfa at size 13     */
1234
1235        if ( edge->serif && edge->link )
1236          edge->serif = 0;
1237      }
1238    }
1239
1240  Exit:
1241    return error;
1242  }
1243
1244
1245  FT_LOCAL_DEF( FT_Error )
1246  af_latin_hints_detect_features( AF_GlyphHints  hints,
1247                                  AF_Dimension   dim )
1248  {
1249    FT_Error  error;
1250
1251
1252    error = af_latin_hints_compute_segments( hints, dim );
1253    if ( !error )
1254    {
1255      af_latin_hints_link_segments( hints, dim );
1256
1257      error = af_latin_hints_compute_edges( hints, dim );
1258    }
1259    return error;
1260  }
1261
1262
1263  FT_LOCAL_DEF( void )
1264  af_latin_hints_compute_blue_edges( AF_GlyphHints    hints,
1265                                     AF_LatinMetrics  metrics )
1266  {
1267    AF_AxisHints  axis       = &hints->axis[ AF_DIMENSION_VERT ];
1268    AF_Edge       edge       = axis->edges;
1269    AF_Edge       edge_limit = edge + axis->num_edges;
1270    AF_LatinAxis  latin      = &metrics->axis[ AF_DIMENSION_VERT ];
1271    FT_Fixed      scale      = latin->scale;
1272
1273
1274    /* compute which blue zones are active, i.e. have their scaled */
1275    /* size < 3/4 pixels                                           */
1276
1277    /* for each horizontal edge search the blue zone which is closest */
1278    for ( ; edge < edge_limit; edge++ )
1279    {
1280      FT_Int    bb;
1281      AF_Width  best_blue = NULL;
1282      FT_Pos    best_dist;  /* initial threshold */
1283
1284
1285      /* compute the initial threshold as a fraction of the EM size */
1286      best_dist = FT_MulFix( metrics->units_per_em / 40, scale );
1287
1288      if ( best_dist > 64 / 2 )
1289        best_dist = 64 / 2;
1290
1291      for ( bb = 0; bb < AF_LATIN_BLUE_MAX; bb++ )
1292      {
1293        AF_LatinBlue  blue = latin->blues + bb;
1294        FT_Bool       is_top_blue, is_major_dir;
1295
1296
1297        /* skip inactive blue zones (i.e., those that are too small) */
1298        if ( !( blue->flags & AF_LATIN_BLUE_ACTIVE ) )
1299          continue;
1300
1301        /* if it is a top zone, check for right edges -- if it is a bottom */
1302        /* zone, check for left edges                                      */
1303        /*                                                                 */
1304        /* of course, that's for TrueType                                  */
1305        is_top_blue  = (FT_Byte)( ( blue->flags & AF_LATIN_BLUE_TOP ) != 0 );
1306        is_major_dir = FT_BOOL( edge->dir == axis->major_dir );
1307
1308        /* if it is a top zone, the edge must be against the major    */
1309        /* direction; if it is a bottom zone, it must be in the major */
1310        /* direction                                                  */
1311        if ( is_top_blue ^ is_major_dir )
1312        {
1313          FT_Pos  dist;
1314
1315
1316          /* first of all, compare it to the reference position */
1317          dist = edge->fpos - blue->ref.org;
1318          if ( dist < 0 )
1319            dist = -dist;
1320
1321          dist = FT_MulFix( dist, scale );
1322          if ( dist < best_dist )
1323          {
1324            best_dist = dist;
1325            best_blue = & blue->ref;
1326          }
1327
1328          /* now, compare it to the overshoot position if the edge is     */
1329          /* rounded, and if the edge is over the reference position of a */
1330          /* top zone, or under the reference position of a bottom zone   */
1331          if ( edge->flags & AF_EDGE_ROUND && dist != 0 )
1332          {
1333            FT_Bool  is_under_ref = FT_BOOL( edge->fpos < blue->ref.org );
1334
1335
1336            if ( is_top_blue ^ is_under_ref )
1337            {
1338              blue = latin->blues + bb;
1339              dist = edge->fpos - blue->shoot.org;
1340              if ( dist < 0 )
1341                dist = -dist;
1342
1343              dist = FT_MulFix( dist, scale );
1344              if ( dist < best_dist )
1345              {
1346                best_dist = dist;
1347                best_blue = & blue->shoot;
1348              }
1349            }
1350          }
1351        }
1352      }
1353
1354      if ( best_blue )
1355        edge->blue_edge = best_blue;
1356    }
1357  }
1358
1359
1360  static FT_Error
1361  af_latin_hints_init( AF_GlyphHints    hints,
1362                       AF_LatinMetrics  metrics )
1363  {
1364    FT_Render_Mode  mode;
1365    FT_UInt32       scaler_flags, other_flags;
1366    FT_Face         face = metrics->root.scaler.face;
1367
1368
1369    af_glyph_hints_rescale( hints, (AF_ScriptMetrics)metrics );
1370
1371    /*
1372     *  correct x_scale and y_scale if needed, since they may have
1373     *  been modified `af_latin_metrics_scale_dim' above
1374     */
1375    hints->x_scale = metrics->axis[AF_DIMENSION_HORZ].scale;
1376    hints->x_delta = metrics->axis[AF_DIMENSION_HORZ].delta;
1377    hints->y_scale = metrics->axis[AF_DIMENSION_VERT].scale;
1378    hints->y_delta = metrics->axis[AF_DIMENSION_VERT].delta;
1379
1380    /* compute flags depending on render mode, etc. */
1381    mode = metrics->root.scaler.render_mode;
1382
1383#if 0 /* #ifdef AF_USE_WARPER */
1384    if ( mode == FT_RENDER_MODE_LCD || mode == FT_RENDER_MODE_LCD_V )
1385    {
1386      metrics->root.scaler.render_mode = mode = FT_RENDER_MODE_NORMAL;
1387    }
1388#endif
1389
1390    scaler_flags = hints->scaler_flags;
1391    other_flags  = 0;
1392
1393    /*
1394     *  We snap the width of vertical stems for the monochrome and
1395     *  horizontal LCD rendering targets only.
1396     */
1397    if ( mode == FT_RENDER_MODE_MONO || mode == FT_RENDER_MODE_LCD )
1398      other_flags |= AF_LATIN_HINTS_HORZ_SNAP;
1399
1400    /*
1401     *  We snap the width of horizontal stems for the monochrome and
1402     *  vertical LCD rendering targets only.
1403     */
1404    if ( mode == FT_RENDER_MODE_MONO || mode == FT_RENDER_MODE_LCD_V )
1405      other_flags |= AF_LATIN_HINTS_VERT_SNAP;
1406
1407    /*
1408     *  We adjust stems to full pixels only if we don't use the `light' mode.
1409     */
1410    if ( mode != FT_RENDER_MODE_LIGHT )
1411      other_flags |= AF_LATIN_HINTS_STEM_ADJUST;
1412
1413    if ( mode == FT_RENDER_MODE_MONO )
1414      other_flags |= AF_LATIN_HINTS_MONO;
1415
1416    /*
1417     *  In `light' hinting mode we disable horizontal hinting completely.
1418     *  We also do it if the face is italic.
1419     */
1420    if ( mode == FT_RENDER_MODE_LIGHT                    ||
1421         (face->style_flags & FT_STYLE_FLAG_ITALIC) != 0 )
1422      scaler_flags |= AF_SCALER_FLAG_NO_HORIZONTAL;
1423
1424    hints->scaler_flags = scaler_flags;
1425    hints->other_flags  = other_flags;
1426
1427    return 0;
1428  }
1429
1430
1431  /*************************************************************************/
1432  /*************************************************************************/
1433  /*****                                                               *****/
1434  /*****        L A T I N   G L Y P H   G R I D - F I T T I N G        *****/
1435  /*****                                                               *****/
1436  /*************************************************************************/
1437  /*************************************************************************/
1438
1439  /* snap a given width in scaled coordinates to one of the */
1440  /* current standard widths                                */
1441
1442  static FT_Pos
1443  af_latin_snap_width( AF_Width  widths,
1444                       FT_Int    count,
1445                       FT_Pos    width )
1446  {
1447    int     n;
1448    FT_Pos  best      = 64 + 32 + 2;
1449    FT_Pos  reference = width;
1450    FT_Pos  scaled;
1451
1452
1453    for ( n = 0; n < count; n++ )
1454    {
1455      FT_Pos  w;
1456      FT_Pos  dist;
1457
1458
1459      w = widths[n].cur;
1460      dist = width - w;
1461      if ( dist < 0 )
1462        dist = -dist;
1463      if ( dist < best )
1464      {
1465        best      = dist;
1466        reference = w;
1467      }
1468    }
1469
1470    scaled = FT_PIX_ROUND( reference );
1471
1472    if ( width >= reference )
1473    {
1474      if ( width < scaled + 48 )
1475        width = reference;
1476    }
1477    else
1478    {
1479      if ( width > scaled - 48 )
1480        width = reference;
1481    }
1482
1483    return width;
1484  }
1485
1486
1487  /* compute the snapped width of a given stem */
1488
1489  static FT_Pos
1490  af_latin_compute_stem_width( AF_GlyphHints  hints,
1491                               AF_Dimension   dim,
1492                               FT_Pos         width,
1493                               AF_Edge_Flags  base_flags,
1494                               AF_Edge_Flags  stem_flags )
1495  {
1496    AF_LatinMetrics  metrics  = (AF_LatinMetrics) hints->metrics;
1497    AF_LatinAxis     axis     = & metrics->axis[dim];
1498    FT_Pos           dist     = width;
1499    FT_Int           sign     = 0;
1500    FT_Int           vertical = ( dim == AF_DIMENSION_VERT );
1501
1502
1503    if ( !AF_LATIN_HINTS_DO_STEM_ADJUST( hints ) ||
1504          axis->extra_light                      )
1505      return width;
1506
1507    if ( dist < 0 )
1508    {
1509      dist = -width;
1510      sign = 1;
1511    }
1512
1513    if ( (  vertical && !AF_LATIN_HINTS_DO_VERT_SNAP( hints ) ) ||
1514         ( !vertical && !AF_LATIN_HINTS_DO_HORZ_SNAP( hints ) ) )
1515    {
1516      /* smooth hinting process: very lightly quantize the stem width */
1517
1518      /* leave the widths of serifs alone */
1519
1520      if ( ( stem_flags & AF_EDGE_SERIF ) && vertical && ( dist < 3 * 64 ) )
1521        goto Done_Width;
1522
1523      else if ( ( base_flags & AF_EDGE_ROUND ) )
1524      {
1525        if ( dist < 80 )
1526          dist = 64;
1527      }
1528      else if ( dist < 56 )
1529        dist = 56;
1530
1531      if ( axis->width_count > 0 )
1532      {
1533        FT_Pos  delta;
1534
1535
1536        /* compare to standard width */
1537        if ( axis->width_count > 0 )
1538        {
1539          delta = dist - axis->widths[0].cur;
1540
1541          if ( delta < 0 )
1542            delta = -delta;
1543
1544          if ( delta < 40 )
1545          {
1546            dist = axis->widths[0].cur;
1547            if ( dist < 48 )
1548              dist = 48;
1549
1550            goto Done_Width;
1551          }
1552        }
1553
1554        if ( dist < 3 * 64 )
1555        {
1556          delta  = dist & 63;
1557          dist  &= -64;
1558
1559          if ( delta < 10 )
1560            dist += delta;
1561
1562          else if ( delta < 32 )
1563            dist += 10;
1564
1565          else if ( delta < 54 )
1566            dist += 54;
1567
1568          else
1569            dist += delta;
1570        }
1571        else
1572          dist = ( dist + 32 ) & ~63;
1573      }
1574    }
1575    else
1576    {
1577      /* strong hinting process: snap the stem width to integer pixels */
1578      FT_Pos  org_dist = dist;
1579
1580
1581      dist = af_latin_snap_width( axis->widths, axis->width_count, dist );
1582
1583      if ( vertical )
1584      {
1585        /* in the case of vertical hinting, always round */
1586        /* the stem heights to integer pixels            */
1587
1588        if ( dist >= 64 )
1589          dist = ( dist + 16 ) & ~63;
1590        else
1591          dist = 64;
1592      }
1593      else
1594      {
1595        if ( AF_LATIN_HINTS_DO_MONO( hints ) )
1596        {
1597          /* monochrome horizontal hinting: snap widths to integer pixels */
1598          /* with a different threshold                                   */
1599
1600          if ( dist < 64 )
1601            dist = 64;
1602          else
1603            dist = ( dist + 32 ) & ~63;
1604        }
1605        else
1606        {
1607          /* for horizontal anti-aliased hinting, we adopt a more subtle */
1608          /* approach: we strengthen small stems, round stems whose size */
1609          /* is between 1 and 2 pixels to an integer, otherwise nothing  */
1610
1611          if ( dist < 48 )
1612            dist = ( dist + 64 ) >> 1;
1613
1614          else if ( dist < 128 )
1615          {
1616            /* We only round to an integer width if the corresponding */
1617            /* distortion is less than 1/4 pixel.  Otherwise this     */
1618            /* makes everything worse since the diagonals, which are  */
1619            /* not hinted, appear a lot bolder or thinner than the    */
1620            /* vertical stems.                                        */
1621
1622            FT_Pos  delta;
1623
1624
1625            dist = ( dist + 22 ) & ~63;
1626            delta = dist - org_dist;
1627            if ( delta < 0 )
1628              delta = -delta;
1629
1630            if (delta >= 16)
1631            {
1632              dist = org_dist;
1633              if ( dist < 48 )
1634                dist = ( dist + 64 ) >> 1;
1635            }
1636          }
1637          else
1638            /* round otherwise to prevent color fringes in LCD mode */
1639            dist = ( dist + 32 ) & ~63;
1640        }
1641      }
1642    }
1643
1644  Done_Width:
1645    if ( sign )
1646      dist = -dist;
1647
1648    return dist;
1649  }
1650
1651
1652  /* align one stem edge relative to the previous stem edge */
1653
1654  static void
1655  af_latin_align_linked_edge( AF_GlyphHints  hints,
1656                              AF_Dimension   dim,
1657                              AF_Edge        base_edge,
1658                              AF_Edge        stem_edge )
1659  {
1660    FT_Pos  dist = stem_edge->opos - base_edge->opos;
1661
1662    FT_Pos  fitted_width = af_latin_compute_stem_width(
1663                             hints, dim, dist,
1664                             (AF_Edge_Flags)base_edge->flags,
1665                             (AF_Edge_Flags)stem_edge->flags );
1666
1667
1668    stem_edge->pos = base_edge->pos + fitted_width;
1669
1670    AF_LOG(( "LINK: edge %d (opos=%.2f) linked to (%.2f), "
1671             "dist was %.2f, now %.2f\n",
1672             stem_edge-hints->axis[dim].edges, stem_edge->opos / 64.0,
1673             stem_edge->pos / 64.0, dist / 64.0, fitted_width / 64.0 ));
1674  }
1675
1676
1677  static void
1678  af_latin_align_serif_edge( AF_GlyphHints  hints,
1679                             AF_Edge        base,
1680                             AF_Edge        serif )
1681  {
1682    FT_UNUSED( hints );
1683
1684    serif->pos = base->pos + (serif->opos - base->opos);
1685  }
1686
1687
1688  /*************************************************************************/
1689  /*************************************************************************/
1690  /*************************************************************************/
1691  /****                                                                 ****/
1692  /****                    E D G E   H I N T I N G                      ****/
1693  /****                                                                 ****/
1694  /*************************************************************************/
1695  /*************************************************************************/
1696  /*************************************************************************/
1697
1698
1699  FT_LOCAL_DEF( void )
1700  af_latin_hint_edges( AF_GlyphHint

Large files files are truncated, but you can click here to view the full file