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