PageRenderTime 98ms CodeModel.GetById 3ms app.highlight 75ms RepoModel.GetById 1ms app.codeStats 1ms

/src/freetype/src/autofit/aflatin.c

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

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