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