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