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