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