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