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