aflatin2.c

Go to the documentation of this file.
00001 /***************************************************************************/
00002 /*                                                                         */
00003 /*  aflatin.c                                                              */
00004 /*                                                                         */
00005 /*    Auto-fitter hinting routines for latin script (body).                */
00006 /*                                                                         */
00007 /*  Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009 by                  */
00008 /*  David Turner, Robert Wilhelm, and Werner Lemberg.                      */
00009 /*                                                                         */
00010 /*  This file is part of the FreeType project, and may only be used,       */
00011 /*  modified, and distributed under the terms of the FreeType project      */
00012 /*  license, LICENSE.TXT.  By continuing to use, modify, or distribute     */
00013 /*  this file you indicate that you have read the license and              */
00014 /*  understand and accept it fully.                                        */
00015 /*                                                                         */
00016 /***************************************************************************/
00017 
00018 
00019 #include FT_ADVANCES_H
00020 
00021 #include "aflatin.h"
00022 #include "aflatin2.h"
00023 #include "aferrors.h"
00024 
00025 
00026 #ifdef AF_USE_WARPER
00027 #include "afwarp.h"
00028 #endif
00029 
00030   FT_LOCAL_DEF( FT_Error )
00031   af_latin2_hints_compute_segments( AF_GlyphHints  hints,
00032                                    AF_Dimension   dim );
00033 
00034   FT_LOCAL_DEF( void )
00035   af_latin2_hints_link_segments( AF_GlyphHints  hints,
00036                                  AF_Dimension   dim );
00037 
00038   /*************************************************************************/
00039   /*************************************************************************/
00040   /*****                                                               *****/
00041   /*****            L A T I N   G L O B A L   M E T R I C S            *****/
00042   /*****                                                               *****/
00043   /*************************************************************************/
00044   /*************************************************************************/
00045 
00046   FT_LOCAL_DEF( void )
00047   af_latin2_metrics_init_widths( AF_LatinMetrics  metrics,
00048                                 FT_Face          face,
00049                                 FT_ULong         charcode )
00050   {
00051     /* scan the array of segments in each direction */
00052     AF_GlyphHintsRec  hints[1];
00053 
00054 
00055     af_glyph_hints_init( hints, face->memory );
00056 
00057     metrics->axis[AF_DIMENSION_HORZ].width_count = 0;
00058     metrics->axis[AF_DIMENSION_VERT].width_count = 0;
00059 
00060     {
00061       FT_Error             error;
00062       FT_UInt              glyph_index;
00063       int                  dim;
00064       AF_LatinMetricsRec   dummy[1];
00065       AF_Scaler            scaler = &dummy->root.scaler;
00066 
00067 
00068       glyph_index = FT_Get_Char_Index( face, charcode );
00069       if ( glyph_index == 0 )
00070         goto Exit;
00071 
00072       error = FT_Load_Glyph( face, glyph_index, FT_LOAD_NO_SCALE );
00073       if ( error || face->glyph->outline.n_points <= 0 )
00074         goto Exit;
00075 
00076       FT_ZERO( dummy );
00077 
00078       dummy->units_per_em = metrics->units_per_em;
00079       scaler->x_scale     = scaler->y_scale = 0x10000L;
00080       scaler->x_delta     = scaler->y_delta = 0;
00081       scaler->face        = face;
00082       scaler->render_mode = FT_RENDER_MODE_NORMAL;
00083       scaler->flags       = 0;
00084 
00085       af_glyph_hints_rescale( hints, (AF_ScriptMetrics)dummy );
00086 
00087       error = af_glyph_hints_reload( hints, &face->glyph->outline, 0 );
00088       if ( error )
00089         goto Exit;
00090 
00091       for ( dim = 0; dim < AF_DIMENSION_MAX; dim++ )
00092       {
00093         AF_LatinAxis  axis    = &metrics->axis[dim];
00094         AF_AxisHints  axhints = &hints->axis[dim];
00095         AF_Segment    seg, limit, link;
00096         FT_UInt       num_widths = 0;
00097 
00098 
00099         error = af_latin2_hints_compute_segments( hints,
00100                                                  (AF_Dimension)dim );
00101         if ( error )
00102           goto Exit;
00103 
00104         af_latin2_hints_link_segments( hints,
00105                                       (AF_Dimension)dim );
00106 
00107         seg   = axhints->segments;
00108         limit = seg + axhints->num_segments;
00109 
00110         for ( ; seg < limit; seg++ )
00111         {
00112           link = seg->link;
00113 
00114           /* we only consider stem segments there! */
00115           if ( link && link->link == seg && link > seg )
00116           {
00117             FT_Pos  dist;
00118 
00119 
00120             dist = seg->pos - link->pos;
00121             if ( dist < 0 )
00122               dist = -dist;
00123 
00124             if ( num_widths < AF_LATIN_MAX_WIDTHS )
00125               axis->widths[ num_widths++ ].org = dist;
00126           }
00127         }
00128 
00129         af_sort_widths( num_widths, axis->widths );
00130         axis->width_count = num_widths;
00131       }
00132 
00133   Exit:
00134       for ( dim = 0; dim < AF_DIMENSION_MAX; dim++ )
00135       {
00136         AF_LatinAxis  axis = &metrics->axis[dim];
00137         FT_Pos        stdw;
00138 
00139 
00140         stdw = ( axis->width_count > 0 )
00141                  ? axis->widths[0].org
00142                  : AF_LATIN_CONSTANT( metrics, 50 );
00143 
00144         /* let's try 20% of the smallest width */
00145         axis->edge_distance_threshold = stdw / 5;
00146         axis->standard_width          = stdw;
00147         axis->extra_light             = 0;
00148       }
00149     }
00150 
00151     af_glyph_hints_done( hints );
00152   }
00153 
00154 
00155 
00156 #define AF_LATIN_MAX_TEST_CHARACTERS  12
00157 
00158 
00159   static const char af_latin2_blue_chars[AF_LATIN_MAX_BLUES][AF_LATIN_MAX_TEST_CHARACTERS+1] =
00160   {
00161     "THEZOCQS",
00162     "HEZLOCUS",
00163     "fijkdbh",
00164     "xzroesc",
00165     "xzroesc",
00166     "pqgjy"
00167   };
00168 
00169 
00170   static void
00171   af_latin2_metrics_init_blues( AF_LatinMetrics  metrics,
00172                                FT_Face          face )
00173   {
00174     FT_Pos        flats [AF_LATIN_MAX_TEST_CHARACTERS];
00175     FT_Pos        rounds[AF_LATIN_MAX_TEST_CHARACTERS];
00176     FT_Int        num_flats;
00177     FT_Int        num_rounds;
00178     FT_Int        bb;
00179     AF_LatinBlue  blue;
00180     FT_Error      error;
00181     AF_LatinAxis  axis  = &metrics->axis[AF_DIMENSION_VERT];
00182     FT_GlyphSlot  glyph = face->glyph;
00183 
00184 
00185     /* we compute the blues simply by loading each character from the    */
00186     /* 'af_latin2_blue_chars[blues]' string, then compute its top-most or */
00187     /* bottom-most points (depending on `AF_IS_TOP_BLUE')                */
00188 
00189     AF_LOG(( "blue zones computation\n" ));
00190     AF_LOG(( "------------------------------------------------\n" ));
00191 
00192     for ( bb = 0; bb < AF_LATIN_BLUE_MAX; bb++ )
00193     {
00194       const char*  p     = af_latin2_blue_chars[bb];
00195       const char*  limit = p + AF_LATIN_MAX_TEST_CHARACTERS;
00196       FT_Pos*      blue_ref;
00197       FT_Pos*      blue_shoot;
00198 
00199 
00200       AF_LOG(( "blue %3d: ", bb ));
00201 
00202       num_flats  = 0;
00203       num_rounds = 0;
00204 
00205       for ( ; p < limit && *p; p++ )
00206       {
00207         FT_UInt     glyph_index;
00208         FT_Int      best_point, best_y, best_first, best_last;
00209         FT_Vector*  points;
00210         FT_Bool     round;
00211 
00212 
00213         AF_LOG(( "'%c'", *p ));
00214 
00215         /* load the character in the face -- skip unknown or empty ones */
00216         glyph_index = FT_Get_Char_Index( face, (FT_UInt)*p );
00217         if ( glyph_index == 0 )
00218           continue;
00219 
00220         error = FT_Load_Glyph( face, glyph_index, FT_LOAD_NO_SCALE );
00221         if ( error || glyph->outline.n_points <= 0 )
00222           continue;
00223 
00224         /* now compute min or max point indices and coordinates */
00225         points      = glyph->outline.points;
00226         best_point  = -1;
00227         best_y      = 0;  /* make compiler happy */
00228         best_first  = 0;  /* ditto */
00229         best_last   = 0;  /* ditto */
00230 
00231         {
00232           FT_Int  nn;
00233           FT_Int  first = 0;
00234           FT_Int  last  = -1;
00235 
00236 
00237           for ( nn = 0; nn < glyph->outline.n_contours; first = last+1, nn++ )
00238           {
00239             FT_Int  old_best_point = best_point;
00240             FT_Int  pp;
00241 
00242 
00243             last = glyph->outline.contours[nn];
00244 
00245             /* Avoid single-point contours since they are never rasterized. */
00246             /* In some fonts, they correspond to mark attachment points     */
00247             /* which are way outside of the glyph's real outline.           */
00248             if ( last == first )
00249                 continue;
00250 
00251             if ( AF_LATIN_IS_TOP_BLUE( bb ) )
00252             {
00253               for ( pp = first; pp <= last; pp++ )
00254                 if ( best_point < 0 || points[pp].y > best_y )
00255                 {
00256                   best_point = pp;
00257                   best_y     = points[pp].y;
00258                 }
00259             }
00260             else
00261             {
00262               for ( pp = first; pp <= last; pp++ )
00263                 if ( best_point < 0 || points[pp].y < best_y )
00264                 {
00265                   best_point = pp;
00266                   best_y     = points[pp].y;
00267                 }
00268             }
00269 
00270             if ( best_point != old_best_point )
00271             {
00272               best_first = first;
00273               best_last  = last;
00274             }
00275           }
00276           AF_LOG(( "%5d", best_y ));
00277         }
00278 
00279         /* now check whether the point belongs to a straight or round   */
00280         /* segment; we first need to find in which contour the extremum */
00281         /* lies, then inspect its previous and next points              */
00282         {
00283           FT_Int  start, end, prev, next;
00284           FT_Pos  dist;
00285 
00286 
00287           /* now look for the previous and next points that are not on the */
00288           /* same Y coordinate.  Threshold the `closeness'...              */
00289           start = end = best_point;
00290 
00291           do
00292           {
00293             prev = start-1;
00294             if ( prev < best_first )
00295               prev = best_last;
00296 
00297             dist = points[prev].y - best_y;
00298             if ( dist < -5 || dist > 5 )
00299               break;
00300 
00301             start = prev;
00302 
00303           } while ( start != best_point );
00304 
00305           do
00306           {
00307             next = end+1;
00308             if ( next > best_last )
00309               next = best_first;
00310 
00311             dist = points[next].y - best_y;
00312             if ( dist < -5 || dist > 5 )
00313               break;
00314 
00315             end = next;
00316 
00317           } while ( end != best_point );
00318 
00319           /* now, set the `round' flag depending on the segment's kind */
00320           round = FT_BOOL(
00321             FT_CURVE_TAG( glyph->outline.tags[start] ) != FT_CURVE_TAG_ON ||
00322             FT_CURVE_TAG( glyph->outline.tags[ end ] ) != FT_CURVE_TAG_ON );
00323 
00324           AF_LOG(( "%c ", round ? 'r' : 'f' ));
00325         }
00326 
00327         if ( round )
00328           rounds[num_rounds++] = best_y;
00329         else
00330           flats[num_flats++]   = best_y;
00331       }
00332 
00333       AF_LOG(( "\n" ));
00334 
00335       if ( num_flats == 0 && num_rounds == 0 )
00336       {
00337         /*
00338          *  we couldn't find a single glyph to compute this blue zone,
00339          *  we will simply ignore it then
00340          */
00341         AF_LOG(( "empty\n" ));
00342         continue;
00343       }
00344 
00345       /* we have computed the contents of the `rounds' and `flats' tables, */
00346       /* now determine the reference and overshoot position of the blue -- */
00347       /* we simply take the median value after a simple sort               */
00348       af_sort_pos( num_rounds, rounds );
00349       af_sort_pos( num_flats,  flats );
00350 
00351       blue       = & axis->blues[axis->blue_count];
00352       blue_ref   = & blue->ref.org;
00353       blue_shoot = & blue->shoot.org;
00354 
00355       axis->blue_count++;
00356 
00357       if ( num_flats == 0 )
00358       {
00359         *blue_ref   =
00360         *blue_shoot = rounds[num_rounds / 2];
00361       }
00362       else if ( num_rounds == 0 )
00363       {
00364         *blue_ref   =
00365         *blue_shoot = flats[num_flats / 2];
00366       }
00367       else
00368       {
00369         *blue_ref   = flats[num_flats / 2];
00370         *blue_shoot = rounds[num_rounds / 2];
00371       }
00372 
00373       /* there are sometimes problems: if the overshoot position of top     */
00374       /* zones is under its reference position, or the opposite for bottom  */
00375       /* zones.  We must thus check everything there and correct the errors */
00376       if ( *blue_shoot != *blue_ref )
00377       {
00378         FT_Pos   ref      = *blue_ref;
00379         FT_Pos   shoot    = *blue_shoot;
00380         FT_Bool  over_ref = FT_BOOL( shoot > ref );
00381 
00382 
00383         if ( AF_LATIN_IS_TOP_BLUE( bb ) ^ over_ref )
00384           *blue_shoot = *blue_ref = ( shoot + ref ) / 2;
00385       }
00386 
00387       blue->flags = 0;
00388       if ( AF_LATIN_IS_TOP_BLUE( bb ) )
00389         blue->flags |= AF_LATIN_BLUE_TOP;
00390 
00391       /*
00392        * The following flags is used later to adjust the y and x scales
00393        * in order to optimize the pixel grid alignment of the top of small
00394        * letters.
00395        */
00396       if ( bb == AF_LATIN_BLUE_SMALL_TOP )
00397         blue->flags |= AF_LATIN_BLUE_ADJUSTMENT;
00398 
00399       AF_LOG(( "-- ref = %ld, shoot = %ld\n", *blue_ref, *blue_shoot ));
00400     }
00401 
00402     return;
00403   }
00404 
00405 
00406   FT_LOCAL_DEF( void )
00407   af_latin2_metrics_check_digits( AF_LatinMetrics  metrics,
00408                                   FT_Face          face )
00409   {
00410     FT_UInt   i;
00411     FT_Bool   started = 0, same_width = 1;
00412     FT_Fixed  advance, old_advance = 0;
00413 
00414 
00415     /* check whether all ASCII digits have the same advance width; */
00416     /* digit `0' is 0x30 in all supported charmaps                 */
00417     for ( i = 0x30; i <= 0x39; i++ )
00418     {
00419       FT_UInt  glyph_index;
00420 
00421 
00422       glyph_index = FT_Get_Char_Index( face, i );
00423       if ( glyph_index == 0 )
00424         continue;
00425 
00426       if ( FT_Get_Advance( face, glyph_index,
00427                            FT_LOAD_NO_SCALE         |
00428                            FT_LOAD_NO_HINTING       |
00429                            FT_LOAD_IGNORE_TRANSFORM,
00430                            &advance ) )
00431         continue;
00432 
00433       if ( started )
00434       {
00435         if ( advance != old_advance )
00436         {
00437           same_width = 0;
00438           break;
00439         }
00440       }
00441       else
00442       {
00443         old_advance = advance;
00444         started     = 1;
00445       }
00446     }
00447 
00448     metrics->root.digits_have_same_width = same_width;
00449   }
00450 
00451 
00452   FT_LOCAL_DEF( FT_Error )
00453   af_latin2_metrics_init( AF_LatinMetrics  metrics,
00454                          FT_Face          face )
00455   {
00456     FT_Error    error = AF_Err_Ok;
00457     FT_CharMap  oldmap = face->charmap;
00458     FT_UInt     ee;
00459 
00460     static const FT_Encoding  latin_encodings[] =
00461     {
00462       FT_ENCODING_UNICODE,
00463       FT_ENCODING_APPLE_ROMAN,
00464       FT_ENCODING_ADOBE_STANDARD,
00465       FT_ENCODING_ADOBE_LATIN_1,
00466       FT_ENCODING_NONE  /* end of list */
00467     };
00468 
00469 
00470     metrics->units_per_em = face->units_per_EM;
00471 
00472     /* do we have a latin charmap in there? */
00473     for ( ee = 0; latin_encodings[ee] != FT_ENCODING_NONE; ee++ )
00474     {
00475       error = FT_Select_Charmap( face, latin_encodings[ee] );
00476       if ( !error )
00477         break;
00478     }
00479 
00480     if ( !error )
00481     {
00482       /* For now, compute the standard width and height from the `o'. */
00483       af_latin2_metrics_init_widths( metrics, face, 'o' );
00484       af_latin2_metrics_init_blues( metrics, face );
00485       af_latin2_metrics_check_digits( metrics, face );
00486     }
00487 
00488     FT_Set_Charmap( face, oldmap );
00489     return AF_Err_Ok;
00490   }
00491 
00492 
00493   static void
00494   af_latin2_metrics_scale_dim( AF_LatinMetrics  metrics,
00495                               AF_Scaler        scaler,
00496                               AF_Dimension     dim )
00497   {
00498     FT_Fixed      scale;
00499     FT_Pos        delta;
00500     AF_LatinAxis  axis;
00501     FT_UInt       nn;
00502 
00503 
00504     if ( dim == AF_DIMENSION_HORZ )
00505     {
00506       scale = scaler->x_scale;
00507       delta = scaler->x_delta;
00508     }
00509     else
00510     {
00511       scale = scaler->y_scale;
00512       delta = scaler->y_delta;
00513     }
00514 
00515     axis = &metrics->axis[dim];
00516 
00517     if ( axis->org_scale == scale && axis->org_delta == delta )
00518       return;
00519 
00520     axis->org_scale = scale;
00521     axis->org_delta = delta;
00522 
00523     /*
00524      * correct Y scale to optimize the alignment of the top of small
00525      * letters to the pixel grid
00526      */
00527     if ( dim == AF_DIMENSION_VERT )
00528     {
00529       AF_LatinAxis  vaxis = &metrics->axis[AF_DIMENSION_VERT];
00530       AF_LatinBlue  blue = NULL;
00531 
00532 
00533       for ( nn = 0; nn < vaxis->blue_count; nn++ )
00534       {
00535         if ( vaxis->blues[nn].flags & AF_LATIN_BLUE_ADJUSTMENT )
00536         {
00537           blue = &vaxis->blues[nn];
00538           break;
00539         }
00540       }
00541 
00542       if ( blue )
00543       {
00544         FT_Pos  scaled = FT_MulFix( blue->shoot.org, scaler->y_scale );
00545         FT_Pos  fitted = ( scaled + 40 ) & ~63;
00546 
00547 #if 1
00548         if ( scaled != fitted ) {
00549           scale = FT_MulDiv( scale, fitted, scaled );
00550           AF_LOG(( "== scaled x-top = %.2g  fitted = %.2g, scaling = %.4g\n", scaled/64.0, fitted/64.0, (fitted*1.0)/scaled ));
00551         }
00552 #endif
00553       }
00554     }
00555 
00556     axis->scale = scale;
00557     axis->delta = delta;
00558 
00559     if ( dim == AF_DIMENSION_HORZ )
00560     {
00561       metrics->root.scaler.x_scale = scale;
00562       metrics->root.scaler.x_delta = delta;
00563     }
00564     else
00565     {
00566       metrics->root.scaler.y_scale = scale;
00567       metrics->root.scaler.y_delta = delta;
00568     }
00569 
00570     /* scale the standard widths */
00571     for ( nn = 0; nn < axis->width_count; nn++ )
00572     {
00573       AF_Width  width = axis->widths + nn;
00574 
00575 
00576       width->cur = FT_MulFix( width->org, scale );
00577       width->fit = width->cur;
00578     }
00579 
00580     /* an extra-light axis corresponds to a standard width that is */
00581     /* smaller than 0.75 pixels                                    */
00582     axis->extra_light =
00583       (FT_Bool)( FT_MulFix( axis->standard_width, scale ) < 32 + 8 );
00584 
00585     if ( dim == AF_DIMENSION_VERT )
00586     {
00587       /* scale the blue zones */
00588       for ( nn = 0; nn < axis->blue_count; nn++ )
00589       {
00590         AF_LatinBlue  blue = &axis->blues[nn];
00591         FT_Pos        dist;
00592 
00593 
00594         blue->ref.cur   = FT_MulFix( blue->ref.org, scale ) + delta;
00595         blue->ref.fit   = blue->ref.cur;
00596         blue->shoot.cur = FT_MulFix( blue->shoot.org, scale ) + delta;
00597         blue->shoot.fit = blue->shoot.cur;
00598         blue->flags    &= ~AF_LATIN_BLUE_ACTIVE;
00599 
00600         /* a blue zone is only active if it is less than 3/4 pixels tall */
00601         dist = FT_MulFix( blue->ref.org - blue->shoot.org, scale );
00602         if ( dist <= 48 && dist >= -48 )
00603         {
00604           FT_Pos  delta1, delta2;
00605 
00606           delta1 = blue->shoot.org - blue->ref.org;
00607           delta2 = delta1;
00608           if ( delta1 < 0 )
00609             delta2 = -delta2;
00610 
00611           delta2 = FT_MulFix( delta2, scale );
00612 
00613           if ( delta2 < 32 )
00614             delta2 = 0;
00615           else if ( delta2 < 64 )
00616             delta2 = 32 + ( ( ( delta2 - 32 ) + 16 ) & ~31 );
00617           else
00618             delta2 = FT_PIX_ROUND( delta2 );
00619 
00620           if ( delta1 < 0 )
00621             delta2 = -delta2;
00622 
00623           blue->ref.fit   = FT_PIX_ROUND( blue->ref.cur );
00624           blue->shoot.fit = blue->ref.fit + delta2;
00625 
00626           AF_LOG(( ">> activating blue zone %d:  ref.cur=%.2g ref.fit=%.2g shoot.cur=%.2g shoot.fit=%.2g\n",
00627                    nn, blue->ref.cur/64.0, blue->ref.fit/64.0,
00628                    blue->shoot.cur/64.0, blue->shoot.fit/64.0 ));
00629 
00630           blue->flags |= AF_LATIN_BLUE_ACTIVE;
00631         }
00632       }
00633     }
00634   }
00635 
00636 
00637   FT_LOCAL_DEF( void )
00638   af_latin2_metrics_scale( AF_LatinMetrics  metrics,
00639                           AF_Scaler        scaler )
00640   {
00641     metrics->root.scaler.render_mode = scaler->render_mode;
00642     metrics->root.scaler.face        = scaler->face;
00643 
00644     af_latin2_metrics_scale_dim( metrics, scaler, AF_DIMENSION_HORZ );
00645     af_latin2_metrics_scale_dim( metrics, scaler, AF_DIMENSION_VERT );
00646   }
00647 
00648 
00649   /*************************************************************************/
00650   /*************************************************************************/
00651   /*****                                                               *****/
00652   /*****           L A T I N   G L Y P H   A N A L Y S I S             *****/
00653   /*****                                                               *****/
00654   /*************************************************************************/
00655   /*************************************************************************/
00656 
00657 #define  SORT_SEGMENTS
00658 
00659   FT_LOCAL_DEF( FT_Error )
00660   af_latin2_hints_compute_segments( AF_GlyphHints  hints,
00661                                    AF_Dimension   dim )
00662   {
00663     AF_AxisHints  axis          = &hints->axis[dim];
00664     FT_Memory     memory        = hints->memory;
00665     FT_Error      error         = AF_Err_Ok;
00666     AF_Segment    segment       = NULL;
00667     AF_SegmentRec seg0;
00668     AF_Point*     contour       = hints->contours;
00669     AF_Point*     contour_limit = contour + hints->num_contours;
00670     AF_Direction  major_dir, segment_dir;
00671 
00672 
00673     FT_ZERO( &seg0 );
00674     seg0.score = 32000;
00675     seg0.flags = AF_EDGE_NORMAL;
00676 
00677     major_dir   = (AF_Direction)FT_ABS( axis->major_dir );
00678     segment_dir = major_dir;
00679 
00680     axis->num_segments = 0;
00681 
00682     /* set up (u,v) in each point */
00683     if ( dim == AF_DIMENSION_HORZ )
00684     {
00685       AF_Point  point = hints->points;
00686       AF_Point  limit = point + hints->num_points;
00687 
00688 
00689       for ( ; point < limit; point++ )
00690       {
00691         point->u = point->fx;
00692         point->v = point->fy;
00693       }
00694     }
00695     else
00696     {
00697       AF_Point  point = hints->points;
00698       AF_Point  limit = point + hints->num_points;
00699 
00700 
00701       for ( ; point < limit; point++ )
00702       {
00703         point->u = point->fy;
00704         point->v = point->fx;
00705       }
00706     }
00707 
00708     /* do each contour separately */
00709     for ( ; contour < contour_limit; contour++ )
00710     {
00711       AF_Point  point   =  contour[0];
00712       AF_Point  start   =  point;
00713       AF_Point  last    =  point->prev;
00714 
00715 
00716       if ( point == last )  /* skip singletons -- just in case */
00717         continue;
00718 
00719       /* already on an edge ?, backtrack to find its start */
00720       if ( FT_ABS( point->in_dir ) == major_dir )
00721       {
00722         point = point->prev;
00723 
00724         while ( point->in_dir == start->in_dir )
00725           point = point->prev;
00726       }
00727       else  /* otherwise, find first segment start, if any */
00728       {
00729         while ( FT_ABS( point->out_dir ) != major_dir )
00730         {
00731           point = point->next;
00732 
00733           if ( point == start )
00734             goto NextContour;
00735         }
00736       }
00737 
00738       start = point;
00739 
00740       for  (;;)
00741       {
00742         AF_Point  first;
00743         FT_Pos    min_u, min_v, max_u, max_v;
00744 
00745         /* we're at the start of a new segment */
00746         FT_ASSERT( FT_ABS( point->out_dir ) == major_dir &&
00747                            point->in_dir != point->out_dir );
00748         first = point;
00749 
00750         min_u = max_u = point->u;
00751         min_v = max_v = point->v;
00752 
00753         point = point->next;
00754 
00755         while ( point->out_dir == first->out_dir )
00756         {
00757           point = point->next;
00758 
00759           if ( point->u < min_u )
00760             min_u = point->u;
00761 
00762           if ( point->u > max_u )
00763             max_u = point->u;
00764         }
00765 
00766         if ( point->v < min_v )
00767           min_v = point->v;
00768 
00769         if ( point->v > max_v )
00770           max_v = point->v;
00771 
00772         /* record new segment */
00773         error = af_axis_hints_new_segment( axis, memory, &segment );
00774         if ( error )
00775           goto Exit;
00776 
00777         segment[0]         = seg0;
00778         segment->dir       = first->out_dir;
00779         segment->first     = first;
00780         segment->last      = point;
00781         segment->contour   = contour;
00782         segment->pos       = (FT_Short)(( min_u + max_u ) >> 1);
00783         segment->min_coord = (FT_Short) min_v;
00784         segment->max_coord = (FT_Short) max_v;
00785         segment->height    = (FT_Short)(max_v - min_v);
00786 
00787         /* a segment is round if it doesn't have successive */
00788         /* on-curve points.                                 */
00789         {
00790           AF_Point  pt   = first;
00791           AF_Point  last = point;
00792           AF_Flags  f0   = (AF_Flags)(pt->flags & AF_FLAG_CONTROL);
00793           AF_Flags  f1;
00794 
00795 
00796           segment->flags &= ~AF_EDGE_ROUND;
00797 
00798           for ( ; pt != last; f0 = f1 )
00799           {
00800             pt = pt->next;
00801             f1 = (AF_Flags)(pt->flags & AF_FLAG_CONTROL);
00802 
00803             if ( !f0 && !f1 )
00804               break;
00805 
00806             if ( pt == last )
00807               segment->flags |= AF_EDGE_ROUND;
00808           }
00809         }
00810 
00811        /* this can happen in the case of a degenerate contour
00812         * e.g. a 2-point vertical contour
00813         */
00814         if ( point == start )
00815           break;
00816 
00817         /* jump to the start of the next segment, if any */
00818         while ( FT_ABS(point->out_dir) != major_dir )
00819         {
00820           point = point->next;
00821 
00822           if ( point == start )
00823             goto NextContour;
00824         }
00825       }
00826 
00827     NextContour:
00828       ;
00829     } /* contours */
00830 
00831     /* now slightly increase the height of segments when this makes */
00832     /* sense -- this is used to better detect and ignore serifs     */
00833     {
00834       AF_Segment  segments     = axis->segments;
00835       AF_Segment  segments_end = segments + axis->num_segments;
00836 
00837 
00838       for ( segment = segments; segment < segments_end; segment++ )
00839       {
00840         AF_Point  first   = segment->first;
00841         AF_Point  last    = segment->last;
00842         AF_Point  p;
00843         FT_Pos    first_v = first->v;
00844         FT_Pos    last_v  = last->v;
00845 
00846 
00847         if ( first == last )
00848           continue;
00849 
00850         if ( first_v < last_v )
00851         {
00852           p = first->prev;
00853           if ( p->v < first_v )
00854             segment->height = (FT_Short)( segment->height +
00855                                           ( ( first_v - p->v ) >> 1 ) );
00856 
00857           p = last->next;
00858           if ( p->v > last_v )
00859             segment->height = (FT_Short)( segment->height +
00860                                           ( ( p->v - last_v ) >> 1 ) );
00861         }
00862         else
00863         {
00864           p = first->prev;
00865           if ( p->v > first_v )
00866             segment->height = (FT_Short)( segment->height +
00867                                           ( ( p->v - first_v ) >> 1 ) );
00868 
00869           p = last->next;
00870           if ( p->v < last_v )
00871             segment->height = (FT_Short)( segment->height +
00872                                           ( ( last_v - p->v ) >> 1 ) );
00873         }
00874       }
00875     }
00876 
00877 #ifdef AF_SORT_SEGMENTS
00878    /* place all segments with a negative direction to the start
00879     * of the array, used to speed up segment linking later...
00880     */
00881     {
00882       AF_Segment  segments = axis->segments;
00883       FT_UInt     count    = axis->num_segments;
00884       FT_UInt     ii, jj;
00885 
00886       for (ii = 0; ii < count; ii++)
00887       {
00888         if ( segments[ii].dir > 0 )
00889         {
00890           for (jj = ii+1; jj < count; jj++)
00891           {
00892             if ( segments[jj].dir < 0 )
00893             {
00894               AF_SegmentRec  tmp;
00895 
00896               tmp          = segments[ii];
00897               segments[ii] = segments[jj];
00898               segments[jj] = tmp;
00899 
00900               break;
00901             }
00902           }
00903 
00904           if ( jj == count )
00905             break;
00906         }
00907       }
00908       axis->mid_segments = ii;
00909     }
00910 #endif
00911 
00912   Exit:
00913     return error;
00914   }
00915 
00916 
00917   FT_LOCAL_DEF( void )
00918   af_latin2_hints_link_segments( AF_GlyphHints  hints,
00919                                 AF_Dimension   dim )
00920   {
00921     AF_AxisHints  axis          = &hints->axis[dim];
00922     AF_Segment    segments      = axis->segments;
00923     AF_Segment    segment_limit = segments + axis->num_segments;
00924 #ifdef AF_SORT_SEGMENTS
00925     AF_Segment    segment_mid   = segments + axis->mid_segments;
00926 #endif
00927     FT_Pos        len_threshold, len_score;
00928     AF_Segment    seg1, seg2;
00929 
00930 
00931     len_threshold = AF_LATIN_CONSTANT( hints->metrics, 8 );
00932     if ( len_threshold == 0 )
00933       len_threshold = 1;
00934 
00935     len_score = AF_LATIN_CONSTANT( hints->metrics, 6000 );
00936 
00937 #ifdef AF_SORT_SEGMENTS
00938     for ( seg1 = segments; seg1 < segment_mid; seg1++ )
00939     {
00940       if ( seg1->dir != axis->major_dir || seg1->first == seg1->last )
00941         continue;
00942 
00943       for ( seg2 = segment_mid; seg2 < segment_limit; seg2++ )
00944 #else
00945     /* now compare each segment to the others */
00946     for ( seg1 = segments; seg1 < segment_limit; seg1++ )
00947     {
00948       /* the fake segments are introduced to hint the metrics -- */
00949       /* we must never link them to anything                     */
00950       if ( seg1->dir != axis->major_dir || seg1->first == seg1->last )
00951         continue;
00952 
00953       for ( seg2 = segments; seg2 < segment_limit; seg2++ )
00954         if ( seg1->dir + seg2->dir == 0 && seg2->pos > seg1->pos )
00955 #endif
00956         {
00957           FT_Pos  pos1 = seg1->pos;
00958           FT_Pos  pos2 = seg2->pos;
00959           FT_Pos  dist = pos2 - pos1;
00960 
00961 
00962           if ( dist < 0 )
00963             continue;
00964 
00965           {
00966             FT_Pos  min = seg1->min_coord;
00967             FT_Pos  max = seg1->max_coord;
00968             FT_Pos  len, score;
00969 
00970 
00971             if ( min < seg2->min_coord )
00972               min = seg2->min_coord;
00973 
00974             if ( max > seg2->max_coord )
00975               max = seg2->max_coord;
00976 
00977             len = max - min;
00978             if ( len >= len_threshold )
00979             {
00980               score = dist + len_score / len;
00981               if ( score < seg1->score )
00982               {
00983                 seg1->score = score;
00984                 seg1->link  = seg2;
00985               }
00986 
00987               if ( score < seg2->score )
00988               {
00989                 seg2->score = score;
00990                 seg2->link  = seg1;
00991               }
00992             }
00993           }
00994         }
00995     }
00996 #if 0
00997     }
00998 #endif
00999 
01000     /* now, compute the `serif' segments */
01001     for ( seg1 = segments; seg1 < segment_limit; seg1++ )
01002     {
01003       seg2 = seg1->link;
01004 
01005       if ( seg2 )
01006       {
01007         if ( seg2->link != seg1 )
01008         {
01009           seg1->link  = 0;
01010           seg1->serif = seg2->link;
01011         }
01012       }
01013     }
01014   }
01015 
01016 
01017   FT_LOCAL_DEF( FT_Error )
01018   af_latin2_hints_compute_edges( AF_GlyphHints  hints,
01019                                 AF_Dimension   dim )
01020   {
01021     AF_AxisHints  axis   = &hints->axis[dim];
01022     FT_Error      error  = AF_Err_Ok;
01023     FT_Memory     memory = hints->memory;
01024     AF_LatinAxis  laxis  = &((AF_LatinMetrics)hints->metrics)->axis[dim];
01025 
01026     AF_Segment    segments      = axis->segments;
01027     AF_Segment    segment_limit = segments + axis->num_segments;
01028     AF_Segment    seg;
01029 
01030     AF_Direction  up_dir;
01031     FT_Fixed      scale;
01032     FT_Pos        edge_distance_threshold;
01033     FT_Pos        segment_length_threshold;
01034 
01035 
01036     axis->num_edges = 0;
01037 
01038     scale = ( dim == AF_DIMENSION_HORZ ) ? hints->x_scale
01039                                          : hints->y_scale;
01040 
01041     up_dir = ( dim == AF_DIMENSION_HORZ ) ? AF_DIR_UP
01042                                           : AF_DIR_RIGHT;
01043 
01044     /*
01045      *  We want to ignore very small (mostly serif) segments, we do that
01046      *  by ignoring those that whose length is less than a given fraction
01047      *  of the standard width. If there is no standard width, we ignore
01048      *  those that are less than a given size in pixels
01049      *
01050      *  also, unlink serif segments that are linked to segments farther
01051      *  than 50% of the standard width
01052      */
01053     if ( dim == AF_DIMENSION_HORZ )
01054     {
01055       if ( laxis->width_count > 0 )
01056         segment_length_threshold = (laxis->standard_width * 10 ) >> 4;
01057       else
01058         segment_length_threshold = FT_DivFix( 64, hints->y_scale );
01059     }
01060     else
01061       segment_length_threshold = 0;
01062 
01063     /*********************************************************************/
01064     /*                                                                   */
01065     /* We will begin by generating a sorted table of edges for the       */
01066     /* current direction.  To do so, we simply scan each segment and try */
01067     /* to find an edge in our table that corresponds to its position.    */
01068     /*                                                                   */
01069     /* If no edge is found, we create and insert a new edge in the       */
01070     /* sorted table.  Otherwise, we simply add the segment to the edge's */
01071     /* list which will be processed in the second step to compute the    */
01072     /* edge's properties.                                                */
01073     /*                                                                   */
01074     /* Note that the edges table is sorted along the segment/edge        */
01075     /* position.                                                         */
01076     /*                                                                   */
01077     /*********************************************************************/
01078 
01079     edge_distance_threshold = FT_MulFix( laxis->edge_distance_threshold,
01080                                          scale );
01081     if ( edge_distance_threshold > 64 / 4 )
01082       edge_distance_threshold = 64 / 4;
01083 
01084     edge_distance_threshold = FT_DivFix( edge_distance_threshold,
01085                                          scale );
01086 
01087     for ( seg = segments; seg < segment_limit; seg++ )
01088     {
01089       AF_Edge  found = 0;
01090       FT_Int   ee;
01091 
01092 
01093       if ( seg->height < segment_length_threshold )
01094         continue;
01095 
01096       /* A special case for serif edges: If they are smaller than */
01097       /* 1.5 pixels we ignore them.                               */
01098       if ( seg->serif )
01099       {
01100         FT_Pos  dist = seg->serif->pos - seg->pos;
01101 
01102         if (dist < 0)
01103           dist = -dist;
01104 
01105         if (dist >= laxis->standard_width >> 1)
01106         {
01107           /* unlink this serif, it is too distant from its reference stem */
01108           seg->serif = NULL;
01109         }
01110         else if ( 2*seg->height < 3 * segment_length_threshold )
01111           continue;
01112       }
01113 
01114       /* look for an edge corresponding to the segment */
01115       for ( ee = 0; ee < axis->num_edges; ee++ )
01116       {
01117         AF_Edge  edge = axis->edges + ee;
01118         FT_Pos   dist;
01119 
01120 
01121         dist = seg->pos - edge->fpos;
01122         if ( dist < 0 )
01123           dist = -dist;
01124 
01125         if ( dist < edge_distance_threshold && edge->dir == seg->dir )
01126         {
01127           found = edge;
01128           break;
01129         }
01130       }
01131 
01132       if ( !found )
01133       {
01134         AF_Edge   edge;
01135 
01136 
01137         /* insert a new edge in the list and */
01138         /* sort according to the position    */
01139         error = af_axis_hints_new_edge( axis, seg->pos, seg->dir, memory, &edge );
01140         if ( error )
01141           goto Exit;
01142 
01143         /* add the segment to the new edge's list */
01144         FT_ZERO( edge );
01145 
01146         edge->first    = seg;
01147         edge->last     = seg;
01148         edge->fpos     = seg->pos;
01149         edge->dir      = seg->dir;
01150         edge->opos     = edge->pos = FT_MulFix( seg->pos, scale );
01151         seg->edge_next = seg;
01152       }
01153       else
01154       {
01155         /* if an edge was found, simply add the segment to the edge's */
01156         /* list                                                       */
01157         seg->edge_next         = found->first;
01158         found->last->edge_next = seg;
01159         found->last            = seg;
01160       }
01161     }
01162 
01163 
01164     /*********************************************************************/
01165     /*                                                                   */
01166     /* Good, we will now compute each edge's properties according to     */
01167     /* segments found on its position.  Basically, these are:            */
01168     /*                                                                   */
01169     /*  - edge's main direction                                          */
01170     /*  - stem edge, serif edge or both (which defaults to stem then)    */
01171     /*  - rounded edge, straight or both (which defaults to straight)    */
01172     /*  - link for edge                                                  */
01173     /*                                                                   */
01174     /*********************************************************************/
01175 
01176     /* first of all, set the `edge' field in each segment -- this is */
01177     /* required in order to compute edge links                       */
01178 
01179     /*
01180      * Note that removing this loop and setting the `edge' field of each
01181      * segment directly in the code above slows down execution speed for
01182      * some reasons on platforms like the Sun.
01183      */
01184     {
01185       AF_Edge  edges      = axis->edges;
01186       AF_Edge  edge_limit = edges + axis->num_edges;
01187       AF_Edge  edge;
01188 
01189 
01190       for ( edge = edges; edge < edge_limit; edge++ )
01191       {
01192         seg = edge->first;
01193         if ( seg )
01194           do
01195           {
01196             seg->edge = edge;
01197             seg       = seg->edge_next;
01198 
01199           } while ( seg != edge->first );
01200       }
01201 
01202       /* now, compute each edge properties */
01203       for ( edge = edges; edge < edge_limit; edge++ )
01204       {
01205         FT_Int  is_round    = 0;  /* does it contain round segments?    */
01206         FT_Int  is_straight = 0;  /* does it contain straight segments? */
01207         FT_Pos  ups         = 0;  /* number of upwards segments         */
01208         FT_Pos  downs       = 0;  /* number of downwards segments       */
01209 
01210 
01211         seg = edge->first;
01212 
01213         do
01214         {
01215           FT_Bool  is_serif;
01216 
01217 
01218           /* check for roundness of segment */
01219           if ( seg->flags & AF_EDGE_ROUND )
01220             is_round++;
01221           else
01222             is_straight++;
01223 
01224           /* check for segment direction */
01225           if ( seg->dir == up_dir )
01226             ups   += seg->max_coord-seg->min_coord;
01227           else
01228             downs += seg->max_coord-seg->min_coord;
01229 
01230           /* check for links -- if seg->serif is set, then seg->link must */
01231           /* be ignored                                                   */
01232           is_serif = (FT_Bool)( seg->serif               &&
01233                                 seg->serif->edge         &&
01234                                 seg->serif->edge != edge );
01235 
01236           if ( ( seg->link && seg->link->edge != NULL ) || is_serif )
01237           {
01238             AF_Edge     edge2;
01239             AF_Segment  seg2;
01240 
01241 
01242             edge2 = edge->link;
01243             seg2  = seg->link;
01244 
01245             if ( is_serif )
01246             {
01247               seg2  = seg->serif;
01248               edge2 = edge->serif;
01249             }
01250 
01251             if ( edge2 )
01252             {
01253               FT_Pos  edge_delta;
01254               FT_Pos  seg_delta;
01255 
01256 
01257               edge_delta = edge->fpos - edge2->fpos;
01258               if ( edge_delta < 0 )
01259                 edge_delta = -edge_delta;
01260 
01261               seg_delta = seg->pos - seg2->pos;
01262               if ( seg_delta < 0 )
01263                 seg_delta = -seg_delta;
01264 
01265               if ( seg_delta < edge_delta )
01266                 edge2 = seg2->edge;
01267             }
01268             else
01269               edge2 = seg2->edge;
01270 
01271             if ( is_serif )
01272             {
01273               edge->serif   = edge2;
01274               edge2->flags |= AF_EDGE_SERIF;
01275             }
01276             else
01277               edge->link  = edge2;
01278           }
01279 
01280           seg = seg->edge_next;
01281 
01282         } while ( seg != edge->first );
01283 
01284         /* set the round/straight flags */
01285         edge->flags = AF_EDGE_NORMAL;
01286 
01287         if ( is_round > 0 && is_round >= is_straight )
01288           edge->flags |= AF_EDGE_ROUND;
01289 
01290 #if 0
01291         /* set the edge's main direction */
01292         edge->dir = AF_DIR_NONE;
01293 
01294         if ( ups > downs )
01295           edge->dir = (FT_Char)up_dir;
01296 
01297         else if ( ups < downs )
01298           edge->dir = (FT_Char)-up_dir;
01299 
01300         else if ( ups == downs )
01301           edge->dir = 0;  /* both up and down! */
01302 #endif
01303 
01304         /* gets rid of serifs if link is set                */
01305         /* XXX: This gets rid of many unpleasant artefacts! */
01306         /*      Example: the `c' in cour.pfa at size 13     */
01307 
01308         if ( edge->serif && edge->link )
01309           edge->serif = 0;
01310       }
01311     }
01312 
01313   Exit:
01314     return error;
01315   }
01316 
01317 
01318   FT_LOCAL_DEF( FT_Error )
01319   af_latin2_hints_detect_features( AF_GlyphHints  hints,
01320                                   AF_Dimension   dim )
01321   {
01322     FT_Error  error;
01323 
01324 
01325     error = af_latin2_hints_compute_segments( hints, dim );
01326     if ( !error )
01327     {
01328       af_latin2_hints_link_segments( hints, dim );
01329 
01330       error = af_latin2_hints_compute_edges( hints, dim );
01331     }
01332     return error;
01333   }
01334 
01335 
01336   FT_LOCAL_DEF( void )
01337   af_latin2_hints_compute_blue_edges( AF_GlyphHints    hints,
01338                                      AF_LatinMetrics  metrics )
01339   {
01340     AF_AxisHints  axis       = &hints->axis[ AF_DIMENSION_VERT ];
01341     AF_Edge       edge       = axis->edges;
01342     AF_Edge       edge_limit = edge + axis->num_edges;
01343     AF_LatinAxis  latin      = &metrics->axis[ AF_DIMENSION_VERT ];
01344     FT_Fixed      scale      = latin->scale;
01345     FT_Pos        best_dist0;  /* initial threshold */
01346 
01347 
01348     /* compute the initial threshold as a fraction of the EM size */
01349     best_dist0 = FT_MulFix( metrics->units_per_em / 40, scale );
01350 
01351     if ( best_dist0 > 64 / 2 )
01352       best_dist0 = 64 / 2;
01353 
01354     /* compute which blue zones are active, i.e. have their scaled */
01355     /* size < 3/4 pixels                                           */
01356 
01357     /* for each horizontal edge search the blue zone which is closest */
01358     for ( ; edge < edge_limit; edge++ )
01359     {
01360       FT_Int    bb;
01361       AF_Width  best_blue = NULL;
01362       FT_Pos    best_dist = best_dist0;
01363 
01364       for ( bb = 0; bb < AF_LATIN_BLUE_MAX; bb++ )
01365       {
01366         AF_LatinBlue  blue = latin->blues + bb;
01367         FT_Bool       is_top_blue, is_major_dir;
01368 
01369 
01370         /* skip inactive blue zones (i.e., those that are too small) */
01371         if ( !( blue->flags & AF_LATIN_BLUE_ACTIVE ) )
01372           continue;
01373 
01374         /* if it is a top zone, check for right edges -- if it is a bottom */
01375         /* zone, check for left edges                                      */
01376         /*                                                                 */
01377         /* of course, that's for TrueType                                  */
01378         is_top_blue  = (FT_Byte)( ( blue->flags & AF_LATIN_BLUE_TOP ) != 0 );
01379         is_major_dir = FT_BOOL( edge->dir == axis->major_dir );
01380 
01381         /* if it is a top zone, the edge must be against the major    */
01382         /* direction; if it is a bottom zone, it must be in the major */
01383         /* direction                                                  */
01384         if ( is_top_blue ^ is_major_dir )
01385         {
01386           FT_Pos     dist;
01387           AF_Width   compare;
01388 
01389 
01390           /* if it's a rounded edge, compare it to the overshoot position */
01391           /* if it's a flat edge, compare it to the reference position    */
01392           if ( edge->flags & AF_EDGE_ROUND )
01393             compare = &blue->shoot;
01394           else
01395             compare = &blue->ref;
01396 
01397           dist = edge->fpos - compare->org;
01398           if (dist < 0)
01399             dist = -dist;
01400 
01401           dist = FT_MulFix( dist, scale );
01402           if ( dist < best_dist )
01403           {
01404             best_dist = dist;
01405             best_blue = compare;
01406           }
01407 
01408 #if 0
01409           /* now, compare it to the overshoot position if the edge is     */
01410           /* rounded, and if the edge is over the reference position of a */
01411           /* top zone, or under the reference position of a bottom zone   */
01412           if ( edge->flags & AF_EDGE_ROUND && dist != 0 )
01413           {
01414             FT_Bool  is_under_ref = FT_BOOL( edge->fpos < blue->ref.org );
01415 
01416 
01417             if ( is_top_blue ^ is_under_ref )
01418             {
01419               blue = latin->blues + bb;
01420               dist = edge->fpos - blue->shoot.org;
01421               if ( dist < 0 )
01422                 dist = -dist;
01423 
01424               dist = FT_MulFix( dist, scale );
01425               if ( dist < best_dist )
01426               {
01427                 best_dist = dist;
01428                 best_blue = & blue->shoot;
01429               }
01430             }
01431           }
01432 #endif
01433         }
01434       }
01435 
01436       if ( best_blue )
01437         edge->blue_edge = best_blue;
01438     }
01439   }
01440 
01441 
01442   static FT_Error
01443   af_latin2_hints_init( AF_GlyphHints    hints,
01444                        AF_LatinMetrics  metrics )
01445   {
01446     FT_Render_Mode  mode;
01447     FT_UInt32       scaler_flags, other_flags;
01448     FT_Face         face = metrics->root.scaler.face;
01449 
01450 
01451     af_glyph_hints_rescale( hints, (AF_ScriptMetrics)metrics );
01452 
01453     /*
01454      *  correct x_scale and y_scale if needed, since they may have
01455      *  been modified `af_latin2_metrics_scale_dim' above
01456      */
01457     hints->x_scale = metrics->axis[AF_DIMENSION_HORZ].scale;
01458     hints->x_delta = metrics->axis[AF_DIMENSION_HORZ].delta;
01459     hints->y_scale = metrics->axis[AF_DIMENSION_VERT].scale;
01460     hints->y_delta = metrics->axis[AF_DIMENSION_VERT].delta;
01461 
01462     /* compute flags depending on render mode, etc. */
01463     mode = metrics->root.scaler.render_mode;
01464 
01465 #if 0 /* #ifdef AF_USE_WARPER */
01466     if ( mode == FT_RENDER_MODE_LCD || mode == FT_RENDER_MODE_LCD_V )
01467     {
01468       metrics->root.scaler.render_mode = mode = FT_RENDER_MODE_NORMAL;
01469     }
01470 #endif
01471 
01472     scaler_flags = hints->scaler_flags;
01473     other_flags  = 0;
01474 
01475     /*
01476      *  We snap the width of vertical stems for the monochrome and
01477      *  horizontal LCD rendering targets only.
01478      */
01479     if ( mode == FT_RENDER_MODE_MONO || mode == FT_RENDER_MODE_LCD )
01480       other_flags |= AF_LATIN_HINTS_HORZ_SNAP;
01481 
01482     /*
01483      *  We snap the width of horizontal stems for the monochrome and
01484      *  vertical LCD rendering targets only.
01485      */
01486     if ( mode == FT_RENDER_MODE_MONO || mode == FT_RENDER_MODE_LCD_V )
01487       other_flags |= AF_LATIN_HINTS_VERT_SNAP;
01488 
01489     /*
01490      *  We adjust stems to full pixels only if we don't use the `light' mode.
01491      */
01492     if ( mode != FT_RENDER_MODE_LIGHT )
01493       other_flags |= AF_LATIN_HINTS_STEM_ADJUST;
01494 
01495     if ( mode == FT_RENDER_MODE_MONO )
01496       other_flags |= AF_LATIN_HINTS_MONO;
01497 
01498     /*
01499      *  In `light' hinting mode we disable horizontal hinting completely.
01500      *  We also do it if the face is italic.
01501      */
01502     if ( mode == FT_RENDER_MODE_LIGHT                    ||
01503          (face->style_flags & FT_STYLE_FLAG_ITALIC) != 0 )
01504       scaler_flags |= AF_SCALER_FLAG_NO_HORIZONTAL;
01505 
01506     hints->scaler_flags = scaler_flags;
01507     hints->other_flags  = other_flags;
01508 
01509     return 0;
01510   }
01511 
01512 
01513   /*************************************************************************/
01514   /*************************************************************************/
01515   /*****                                                               *****/
01516   /*****        L A T I N   G L Y P H   G R I D - F I T T I N G        *****/
01517   /*****                                                               *****/
01518   /*************************************************************************/
01519   /*************************************************************************/
01520 
01521   /* snap a given width in scaled coordinates to one of the */
01522   /* current standard widths                                */
01523 
01524   static FT_Pos
01525   af_latin2_snap_width( AF_Width  widths,
01526                        FT_Int    count,
01527                        FT_Pos    width )
01528   {
01529     int     n;
01530     FT_Pos  best      = 64 + 32 + 2;
01531     FT_Pos  reference = width;
01532     FT_Pos  scaled;
01533 
01534 
01535     for ( n = 0; n < count; n++ )
01536     {
01537       FT_Pos  w;
01538       FT_Pos  dist;
01539 
01540 
01541       w = widths[n].cur;
01542       dist = width - w;
01543       if ( dist < 0 )
01544         dist = -dist;
01545       if ( dist < best )
01546       {
01547         best      = dist;
01548         reference = w;
01549       }
01550     }
01551 
01552     scaled = FT_PIX_ROUND( reference );
01553 
01554     if ( width >= reference )
01555     {
01556       if ( width < scaled + 48 )
01557         width = reference;
01558     }
01559     else
01560     {
01561       if ( width > scaled - 48 )
01562         width = reference;
01563     }
01564 
01565     return width;
01566   }
01567 
01568 
01569   /* compute the snapped width of a given stem */
01570 
01571   static FT_Pos
01572   af_latin2_compute_stem_width( AF_GlyphHints  hints,
01573                                AF_Dimension   dim,
01574                                FT_Pos         width,
01575                                AF_Edge_Flags  base_flags,
01576                                AF_Edge_Flags  stem_flags )
01577   {
01578     AF_LatinMetrics  metrics  = (AF_LatinMetrics) hints->metrics;
01579     AF_LatinAxis     axis     = & metrics->axis[dim];
01580     FT_Pos           dist     = width;
01581     FT_Int           sign     = 0;
01582     FT_Int           vertical = ( dim == AF_DIMENSION_VERT );
01583 
01584 
01585     FT_UNUSED(base_flags);
01586 
01587     if ( !AF_LATIN_HINTS_DO_STEM_ADJUST( hints ) ||
01588           axis->extra_light                      )
01589       return width;
01590 
01591     if ( dist < 0 )
01592     {
01593       dist = -width;
01594       sign = 1;
01595     }
01596 
01597     if ( (  vertical && !AF_LATIN_HINTS_DO_VERT_SNAP( hints ) ) ||
01598          ( !vertical && !AF_LATIN_HINTS_DO_HORZ_SNAP( hints ) ) )
01599     {
01600       /* smooth hinting process: very lightly quantize the stem width */
01601 
01602       /* leave the widths of serifs alone */
01603 
01604       if ( ( stem_flags & AF_EDGE_SERIF ) && vertical && ( dist < 3 * 64 ) )
01605         goto Done_Width;
01606 
01607 #if 0
01608       else if ( ( base_flags & AF_EDGE_ROUND ) )
01609       {
01610         if ( dist < 80 )
01611           dist = 64;
01612       }
01613       else if ( dist < 56 )
01614         dist = 56;
01615 #endif
01616       if ( axis->width_count > 0 )
01617       {
01618         FT_Pos  delta;
01619 
01620 
01621         /* compare to standard width */
01622         if ( axis->width_count > 0 )
01623         {
01624           delta = dist - axis->widths[0].cur;
01625 
01626           if ( delta < 0 )
01627             delta = -delta;
01628 
01629           if ( delta < 40 )
01630           {
01631             dist = axis->widths[0].cur;
01632             if ( dist < 48 )
01633               dist = 48;
01634 
01635             goto Done_Width;
01636           }
01637         }
01638 
01639         if ( dist < 3 * 64 )
01640         {
01641           delta  = dist & 63;
01642           dist  &= -64;
01643 
01644           if ( delta < 10 )
01645             dist += delta;
01646 
01647           else if ( delta < 32 )
01648             dist += 10;
01649 
01650           else if ( delta < 54 )
01651             dist += 54;
01652 
01653           else
01654             dist += delta;
01655         }
01656         else
01657           dist = ( dist + 32 ) & ~63;
01658       }
01659     }
01660     else
01661     {
01662       /* strong hinting process: snap the stem width to integer pixels */
01663       FT_Pos  org_dist = dist;
01664 
01665 
01666       dist = af_latin2_snap_width( axis->widths, axis->width_count, dist );
01667 
01668       if ( vertical )
01669       {
01670         /* in the case of vertical hinting, always round */
01671         /* the stem heights to integer pixels            */
01672 
01673         if ( dist >= 64 )
01674           dist = ( dist + 16 ) & ~63;
01675         else
01676           dist = 64;
01677       }
01678       else
01679       {
01680         if ( AF_LATIN_HINTS_DO_MONO( hints ) )
01681         {
01682           /* monochrome horizontal hinting: snap widths to integer pixels */
01683           /* with a different threshold                                   */
01684 
01685           if ( dist < 64 )
01686             dist = 64;
01687           else
01688             dist = ( dist + 32 ) & ~63;
01689         }
01690         else
01691         {
01692           /* for horizontal anti-aliased hinting, we adopt a more subtle */
01693           /* approach: we strengthen small stems, round stems whose size */
01694           /* is between 1 and 2 pixels to an integer, otherwise nothing  */
01695 
01696           if ( dist < 48 )
01697             dist = ( dist + 64 ) >> 1;
01698 
01699           else if ( dist < 128 )
01700           {
01701             /* We only round to an integer width if the corresponding */
01702             /* distortion is less than 1/4 pixel.  Otherwise this     */
01703             /* makes everything worse since the diagonals, which are  */
01704             /* not hinted, appear a lot bolder or thinner than the    */
01705             /* vertical stems.                                        */
01706 
01707             FT_Int  delta;
01708 
01709 
01710             dist = ( dist + 22 ) & ~63;
01711             delta = dist - org_dist;
01712             if ( delta < 0 )
01713               delta = -delta;
01714 
01715             if (delta >= 16)
01716             {
01717               dist = org_dist;
01718               if ( dist < 48 )
01719                 dist = ( dist + 64 ) >> 1;
01720             }
01721           }
01722           else
01723             /* round otherwise to prevent color fringes in LCD mode */
01724             dist = ( dist + 32 ) & ~63;
01725         }
01726       }
01727     }
01728 
01729   Done_Width:
01730     if ( sign )
01731       dist = -dist;
01732 
01733     return dist;
01734   }
01735 
01736 
01737   /* align one stem edge relative to the previous stem edge */
01738 
01739   static void
01740   af_latin2_align_linked_edge( AF_GlyphHints  hints,
01741                               AF_Dimension   dim,
01742                               AF_Edge        base_edge,
01743                               AF_Edge        stem_edge )
01744   {
01745     FT_Pos  dist = stem_edge->opos - base_edge->opos;
01746 
01747     FT_Pos  fitted_width = af_latin2_compute_stem_width(
01748                              hints, dim, dist,
01749                              (AF_Edge_Flags)base_edge->flags,
01750                              (AF_Edge_Flags)stem_edge->flags );
01751 
01752 
01753     stem_edge->pos = base_edge->pos + fitted_width;
01754 
01755     AF_LOG(( "LINK: edge %d (opos=%.2f) linked to (%.2f), "
01756              "dist was %.2f, now %.2f\n",
01757              stem_edge-hints->axis[dim].edges, stem_edge->opos / 64.0,
01758              stem_edge->pos / 64.0, dist / 64.0, fitted_width / 64.0 ));
01759   }
01760 
01761 
01762   static void
01763   af_latin2_align_serif_edge( AF_GlyphHints  hints,
01764                              AF_Edge        base,
01765                              AF_Edge        serif )
01766   {
01767     FT_UNUSED( hints );
01768 
01769     serif->pos = base->pos + (serif->opos - base->opos);
01770   }
01771 
01772 
01773   /*************************************************************************/
01774   /*************************************************************************/
01775   /*************************************************************************/
01776   /****                                                                 ****/
01777   /****                    E D G E   H I N T I N G                      ****/
01778   /****                                                                 ****/
01779   /*************************************************************************/
01780   /*************************************************************************/
01781   /*************************************************************************/
01782 
01783 
01784   FT_LOCAL_DEF( void )
01785   af_latin2_hint_edges( AF_GlyphHints  hints,
01786                        AF_Dimension   dim )
01787   {
01788     AF_AxisHints  axis       = &hints->axis[dim];
01789     AF_Edge       edges      = axis->edges;
01790     AF_Edge       edge_limit = edges + axis->num_edges;
01791     AF_Edge       edge;
01792     AF_Edge       anchor     = 0;
01793     FT_Int        has_serifs = 0;
01794     FT_Pos        anchor_drift = 0;
01795 
01796 
01797 
01798     AF_LOG(( "==== hinting %s edges =====\n", dim == AF_DIMENSION_HORZ ? "vertical" : "horizontal" ));
01799 
01800     /* we begin by aligning all stems relative to the blue zone */
01801     /* if needed -- that's only for horizontal edges            */
01802 
01803     if ( dim == AF_DIMENSION_VERT && AF_HINTS_DO_BLUES( hints ) )
01804     {
01805       for ( edge = edges; edge < edge_limit; edge++ )
01806       {
01807         AF_Width  blue;
01808         AF_Edge   edge1, edge2;
01809 
01810 
01811         if ( edge->flags & AF_EDGE_DONE )
01812           continue;
01813 
01814         blue  = edge->blue_edge;
01815         edge1 = NULL;
01816         edge2 = edge->link;
01817 
01818         if ( blue )
01819         {
01820           edge1 = edge;
01821         }
01822         else if ( edge2 && edge2->blue_edge )
01823         {
01824           blue  = edge2->blue_edge;
01825           edge1 = edge2;
01826           edge2 = edge;
01827         }
01828 
01829         if ( !edge1 )
01830           continue;
01831 
01832         AF_LOG(( "BLUE: edge %d (opos=%.2f) snapped to (%.2f), "
01833                  "was (%.2f)\n",
01834                  edge1-edges, edge1->opos / 64.0, blue->fit / 64.0,
01835                  edge1->pos / 64.0 ));
01836 
01837         edge1->pos    = blue->fit;
01838         edge1->flags |= AF_EDGE_DONE;
01839 
01840         if ( edge2 && !edge2->blue_edge )
01841         {
01842           af_latin2_align_linked_edge( hints, dim, edge1, edge2 );
01843           edge2->flags |= AF_EDGE_DONE;
01844         }
01845 
01846         if ( !anchor )
01847         {
01848           anchor = edge;
01849 
01850           anchor_drift = (anchor->pos - anchor->opos);
01851           if (edge2)
01852             anchor_drift = (anchor_drift + (edge2->pos - edge2->opos)) >> 1;
01853         }
01854       }
01855     }
01856 
01857     /* now we will align all stem edges, trying to maintain the */
01858     /* relative order of stems in the glyph                     */
01859     for ( edge = edges; edge < edge_limit; edge++ )
01860     {
01861       AF_Edge  edge2;
01862 
01863 
01864       if ( edge->flags & AF_EDGE_DONE )
01865         continue;
01866 
01867       /* skip all non-stem edges */
01868       edge2 = edge->link;
01869       if ( !edge2 )
01870       {
01871         has_serifs++;
01872         continue;
01873       }
01874 
01875       /* now align the stem */
01876 
01877       /* this should not happen, but it's better to be safe */
01878       if ( edge2->blue_edge )
01879       {
01880         AF_LOG(( "ASSERTION FAILED for edge %d\n", edge2-edges ));
01881 
01882         af_latin2_align_linked_edge( hints, dim, edge2, edge );
01883         edge->flags |= AF_EDGE_DONE;
01884         continue;
01885       }
01886 
01887       if ( !anchor )
01888       {
01889         FT_Pos  org_len, org_center, cur_len;
01890         FT_Pos  cur_pos1, error1, error2, u_off, d_off;
01891 
01892 
01893         org_len = edge2->opos - edge->opos;
01894         cur_len = af_latin2_compute_stem_width(
01895                     hints, dim, org_len,
01896                     (AF_Edge_Flags)edge->flags,
01897                     (AF_Edge_Flags)edge2->flags );
01898         if ( cur_len <= 64 )
01899           u_off = d_off = 32;
01900         else
01901         {
01902           u_off = 38;
01903           d_off = 26;
01904         }
01905 
01906         if ( cur_len < 96 )
01907         {
01908           org_center = edge->opos + ( org_len >> 1 );
01909 
01910           cur_pos1   = FT_PIX_ROUND( org_center );
01911 
01912           error1 = org_center - ( cur_pos1 - u_off );
01913           if ( error1 < 0 )
01914             error1 = -error1;
01915 
01916           error2 = org_center - ( cur_pos1 + d_off );
01917           if ( error2 < 0 )
01918             error2 = -error2;
01919 
01920           if ( error1 < error2 )
01921             cur_pos1 -= u_off;
01922           else
01923             cur_pos1 += d_off;
01924 
01925           edge->pos  = cur_pos1 - cur_len / 2;
01926           edge2->pos = edge->pos + cur_len;
01927         }
01928         else
01929           edge->pos = FT_PIX_ROUND( edge->opos );
01930 
01931         AF_LOG(( "ANCHOR: edge %d (opos=%.2f) and %d (opos=%.2f) "
01932                  "snapped to (%.2f) (%.2f)\n",
01933                  edge-edges, edge->opos / 64.0,
01934                  edge2-edges, edge2->opos / 64.0,
01935                  edge->pos / 64.0, edge2->pos / 64.0 ));
01936         anchor = edge;
01937 
01938         edge->flags |= AF_EDGE_DONE;
01939 
01940         af_latin2_align_linked_edge( hints, dim, edge, edge2 );
01941 
01942         edge2->flags |= AF_EDGE_DONE;
01943 
01944         anchor_drift = ( (anchor->pos - anchor->opos) +
01945                          (edge2->pos - edge2->opos)) >> 1;
01946 
01947         AF_LOG(( "DRIFT: %.2f\n", anchor_drift/64.0 ));
01948       }
01949       else
01950       {
01951         FT_Pos   org_pos, org_len, org_center, cur_center, cur_len;
01952         FT_Pos   org_left, org_right;
01953 
01954 
01955         org_pos    = edge->opos + anchor_drift;
01956         org_len    = edge2->opos - edge->opos;
01957         org_center = org_pos + ( org_len >> 1 );
01958 
01959         cur_len = af_latin2_compute_stem_width(
01960                    hints, dim, org_len,
01961                    (AF_Edge_Flags)edge->flags,
01962                    (AF_Edge_Flags)edge2->flags );
01963 
01964         org_left  = org_pos + ((org_len - cur_len) >> 1);
01965         org_right = org_pos + ((org_len + cur_len) >> 1);
01966 
01967         AF_LOG(( "ALIGN: left=%.2f right=%.2f ", org_left/64.0, org_right/64.0 ));
01968         cur_center = org_center;
01969 
01970         if ( edge2->flags & AF_EDGE_DONE )
01971         {
01972           AF_LOG(( "\n" ));
01973           edge->pos = edge2->pos - cur_len;
01974         }
01975         else
01976         {
01977          /* we want to compare several displacement, and choose
01978           * the one that increases fitness while minimizing
01979           * distortion as well
01980           */
01981           FT_Pos   displacements[6], scores[6], org, fit, delta;
01982           FT_UInt  count = 0;
01983 
01984           /* note: don't even try to fit tiny stems */
01985           if ( cur_len < 32 )
01986           {
01987             AF_LOG(( "tiny stem\n" ));
01988             goto AlignStem;
01989           }
01990 
01991           /* if the span is within a single pixel, don't touch it */
01992           if ( FT_PIX_FLOOR(org_left) == FT_PIX_CEIL(org_right) )
01993           {
01994             AF_LOG(( "single pixel stem\n" ));
01995             goto AlignStem;
01996           }
01997 
01998           if (cur_len <= 96)
01999           {
02000            /* we want to avoid the absolute worst case which is
02001             * when the left and right edges of the span each represent
02002             * about 50% of the gray. we'd better want to change this
02003             * to 25/75%, since this is much more pleasant to the eye with
02004             * very acceptable distortion
02005             */
02006             FT_Pos  frac_left  = (org_left) & 63;
02007             FT_Pos  frac_right = (org_right) & 63;
02008 
02009             if ( frac_left  >= 22 && frac_left  <= 42 &&
02010                  frac_right >= 22 && frac_right <= 42 )
02011             {
02012               org = frac_left;
02013               fit = (org <= 32) ? 16 : 48;
02014               delta = FT_ABS(fit - org);
02015               displacements[count] = fit - org;
02016               scores[count++]      = delta;
02017               AF_LOG(( "dispA=%.2f (%d) ", (fit - org)/64.0, delta ));
02018 
02019               org = frac_right;
02020               fit = (org <= 32) ? 16 : 48;
02021               delta = FT_ABS(fit - org);
02022               displacements[count] = fit - org;
02023               scores[count++]     = delta;
02024               AF_LOG(( "dispB=%.2f (%d) ", (fit - org)/64.0, delta ));
02025             }
02026           }
02027 
02028           /* snapping the left edge to the grid */
02029           org   = org_left;
02030           fit   = FT_PIX_ROUND(org);
02031           delta = FT_ABS(fit - org);
02032           displacements[count] = fit - org;
02033           scores[count++]      = delta;
02034           AF_LOG(( "dispC=%.2f (%d) ", (fit - org)/64.0, delta ));
02035 
02036           /* snapping the right edge to the grid */
02037           org   = org_right;
02038           fit   = FT_PIX_ROUND(org);
02039           delta = FT_ABS(fit - org);
02040           displacements[count] = fit - org;
02041           scores[count++]      = delta;
02042           AF_LOG(( "dispD=%.2f (%d) ", (fit - org)/64.0, delta ));
02043 
02044           /* now find the best displacement */
02045           {
02046             FT_Pos  best_score = scores[0];
02047             FT_Pos  best_disp  = displacements[0];
02048             FT_UInt nn;
02049 
02050             for (nn = 1; nn < count; nn++)
02051             {
02052               if (scores[nn] < best_score)
02053               {
02054                 best_score = scores[nn];
02055                 best_disp  = displacements[nn];
02056               }
02057             }
02058 
02059             cur_center = org_center + best_disp;
02060           }
02061           AF_LOG(( "\n" ));
02062         }
02063 
02064       AlignStem:
02065         edge->pos  = cur_center - (cur_len >> 1);
02066         edge2->pos = edge->pos + cur_len;
02067 
02068         AF_LOG(( "STEM1: %d (opos=%.2f) to %d (opos=%.2f) "
02069                  "snapped to (%.2f) and (%.2f), org_len = %.2f cur_len=%.2f\n",
02070                  edge-edges, edge->opos / 64.0,
02071                  edge2-edges, edge2->opos / 64.0,
02072                  edge->pos / 64.0, edge2->pos / 64.0,
02073                  org_len / 64.0, cur_len / 64.0 ));
02074 
02075         edge->flags  |= AF_EDGE_DONE;
02076         edge2->flags |= AF_EDGE_DONE;
02077 
02078         if ( edge > edges && edge->pos < edge[-1].pos )
02079         {
02080           AF_LOG(( "BOUND: %d (pos=%.2f) to (%.2f)\n",
02081                    edge-edges, edge->pos / 64.0, edge[-1].pos / 64.0 ));
02082           edge->pos = edge[-1].pos;
02083         }
02084       }
02085     }
02086 
02087     /* make sure that lowercase m's maintain their symmetry */
02088 
02089     /* In general, lowercase m's have six vertical edges if they are sans */
02090     /* serif, or twelve if they are with serifs.  This implementation is  */
02091     /* based on that assumption, and seems to work very well with most    */
02092     /* faces.  However, if for a certain face this assumption is not      */
02093     /* true, the m is just rendered like before.  In addition, any stem   */
02094     /* correction will only be applied to symmetrical glyphs (even if the */
02095     /* glyph is not an m), so the potential for unwanted distortion is    */
02096     /* relatively low.                                                    */
02097 
02098     /* We don't handle horizontal edges since we can't easily assure that */
02099     /* the third (lowest) stem aligns with the base line; it might end up */
02100     /* one pixel higher or lower.                                         */
02101 
02102 #if 0
02103     {
02104       FT_Int  n_edges = edge_limit - edges;
02105 
02106 
02107       if ( dim == AF_DIMENSION_HORZ && ( n_edges == 6 || n_edges == 12 ) )
02108       {
02109         AF_Edge  edge1, edge2, edge3;
02110         FT_Pos   dist1, dist2, span, delta;
02111 
02112 
02113         if ( n_edges == 6 )
02114         {
02115           edge1 = edges;
02116           edge2 = edges + 2;
02117           edge3 = edges + 4;
02118         }
02119         else
02120         {
02121           edge1 = edges + 1;
02122           edge2 = edges + 5;
02123           edge3 = edges + 9;
02124         }
02125 
02126         dist1 = edge2->opos - edge1->opos;
02127         dist2 = edge3->opos - edge2->opos;
02128 
02129         span = dist1 - dist2;
02130         if ( span < 0 )
02131           span = -span;
02132 
02133         if ( span < 8 )
02134         {
02135           delta = edge3->pos - ( 2 * edge2->pos - edge1->pos );
02136           edge3->pos -= delta;
02137           if ( edge3->link )
02138             edge3->link->pos -= delta;
02139 
02140           /* move the serifs along with the stem */
02141           if ( n_edges == 12 )
02142           {
02143             ( edges + 8 )->pos -= delta;
02144             ( edges + 11 )->pos -= delta;
02145           }
02146 
02147           edge3->flags |= AF_EDGE_DONE;
02148           if ( edge3->link )
02149             edge3->link->flags |= AF_EDGE_DONE;
02150         }
02151       }
02152     }
02153 #endif
02154 
02155     if ( has_serifs || !anchor )
02156     {
02157       /*
02158        *  now hint the remaining edges (serifs and single) in order
02159        *  to complete our processing
02160        */
02161       for ( edge = edges; edge < edge_limit; edge++ )
02162       {
02163         FT_Pos  delta;
02164 
02165 
02166         if ( edge->flags & AF_EDGE_DONE )
02167           continue;
02168 
02169         delta = 1000;
02170 
02171         if ( edge->serif )
02172         {
02173           delta = edge->serif->opos - edge->opos;
02174           if ( delta < 0 )
02175             delta = -delta;
02176         }
02177 
02178         if ( delta < 64 + 16 )
02179         {
02180           af_latin2_align_serif_edge( hints, edge->serif, edge );
02181           AF_LOG(( "SERIF: edge %d (opos=%.2f) serif to %d (opos=%.2f) "
02182                    "aligned to (%.2f)\n",
02183                    edge-edges, edge->opos / 64.0,
02184                    edge->serif - edges, edge->serif->opos / 64.0,
02185                    edge->pos / 64.0 ));
02186         }
02187         else if ( !anchor )
02188         {
02189           AF_LOG(( "SERIF_ANCHOR: edge %d (opos=%.2f) snapped to (%.2f)\n",
02190                    edge-edges, edge->opos / 64.0, edge->pos / 64.0 ));
02191           edge->pos = FT_PIX_ROUND( edge->opos );
02192           anchor    = edge;
02193         }
02194         else
02195         {
02196           AF_Edge  before, after;
02197 
02198 
02199           for ( before = edge - 1; before >= edges; before-- )
02200             if ( before->flags & AF_EDGE_DONE )
02201               break;
02202 
02203           for ( after = edge + 1; after < edge_limit; after++ )
02204             if ( after->flags & AF_EDGE_DONE )
02205               break;
02206 
02207           if ( before >= edges && before < edge   &&
02208                after < edge_limit && after > edge )
02209           {
02210             if ( after->opos == before->opos )
02211               edge->pos = before->pos;
02212             else
02213               edge->pos = before->pos +
02214                           FT_MulDiv( edge->opos - before->opos,
02215                                      after->pos - before->pos,
02216                                      after->opos - before->opos );
02217             AF_LOG(( "SERIF_LINK1: edge %d (opos=%.2f) snapped to (%.2f) from %d (opos=%.2f)\n",
02218                      edge-edges, edge->opos / 64.0, edge->pos / 64.0, before - edges, before->opos / 64.0 ));
02219           }
02220           else
02221           {
02222               edge->pos = anchor->pos + (( edge->opos - anchor->opos + 16) & ~31);
02223 
02224             AF_LOG(( "SERIF_LINK2: edge %d (opos=%.2f) snapped to (%.2f)\n",
02225                      edge-edges, edge->opos / 64.0, edge->pos / 64.0 ));
02226           }
02227         }
02228 
02229         edge->flags |= AF_EDGE_DONE;
02230 
02231         if ( edge > edges && edge->pos < edge[-1].pos )
02232           edge->pos = edge[-1].pos;
02233 
02234         if ( edge + 1 < edge_limit        &&
02235              edge[1].flags & AF_EDGE_DONE &&
02236              edge->pos > edge[1].pos      )
02237           edge->pos = edge[1].pos;
02238       }
02239     }
02240   }
02241 
02242 
02243   static FT_Error
02244   af_latin2_hints_apply( AF_GlyphHints    hints,
02245                         FT_Outline*      outline,
02246                         AF_LatinMetrics  metrics )
02247   {
02248     FT_Error  error;
02249     int       dim;
02250 
02251 
02252     error = af_glyph_hints_reload( hints, outline, 1 );
02253     if ( error )
02254       goto Exit;
02255 
02256     /* analyze glyph outline */
02257 #ifdef AF_USE_WARPER
02258     if ( metrics->root.scaler.render_mode == FT_RENDER_MODE_LIGHT ||
02259          AF_HINTS_DO_HORIZONTAL( hints ) )
02260 #else
02261     if ( AF_HINTS_DO_HORIZONTAL( hints ) )
02262 #endif
02263     {
02264       error = af_latin2_hints_detect_features( hints, AF_DIMENSION_HORZ );
02265       if ( error )
02266         goto Exit;
02267     }
02268 
02269     if ( AF_HINTS_DO_VERTICAL( hints ) )
02270     {
02271       error = af_latin2_hints_detect_features( hints, AF_DIMENSION_VERT );
02272       if ( error )
02273         goto Exit;
02274 
02275       af_latin2_hints_compute_blue_edges( hints, metrics );
02276     }
02277 
02278     /* grid-fit the outline */
02279     for ( dim = 0; dim < AF_DIMENSION_MAX; dim++ )
02280     {
02281 #ifdef AF_USE_WARPER
02282       if ( ( dim == AF_DIMENSION_HORZ &&
02283              metrics->root.scaler.render_mode == FT_RENDER_MODE_LIGHT ) )
02284       {
02285         AF_WarperRec  warper;
02286         FT_Fixed      scale;
02287         FT_Pos        delta;
02288 
02289 
02290         af_warper_compute( &warper, hints, dim, &scale, &delta );
02291         af_glyph_hints_scale_dim( hints, dim, scale, delta );
02292         continue;
02293       }
02294 #endif
02295 
02296       if ( ( dim == AF_DIMENSION_HORZ && AF_HINTS_DO_HORIZONTAL( hints ) ) ||
02297            ( dim == AF_DIMENSION_VERT && AF_HINTS_DO_VERTICAL( hints ) )   )
02298       {
02299         af_latin2_hint_edges( hints, (AF_Dimension)dim );
02300         af_glyph_hints_align_edge_points( hints, (AF_Dimension)dim );
02301         af_glyph_hints_align_strong_points( hints, (AF_Dimension)dim );
02302         af_glyph_hints_align_weak_points( hints, (AF_Dimension)dim );
02303       }
02304     }
02305     af_glyph_hints_save( hints, outline );
02306 
02307   Exit:
02308     return error;
02309   }
02310 
02311 
02312   /*************************************************************************/
02313   /*************************************************************************/
02314   /*****                                                               *****/
02315   /*****              L A T I N   S C R I P T   C L A S S              *****/
02316   /*****                                                               *****/
02317   /*************************************************************************/
02318   /*************************************************************************/
02319 
02320 
02321   static const AF_Script_UniRangeRec  af_latin2_uniranges[] =
02322   {
02323     AF_UNIRANGE_REC( 32UL,  127UL ),    /* XXX: TODO: Add new Unicode ranges here! */
02324     AF_UNIRANGE_REC( 160UL, 255UL ),
02325     AF_UNIRANGE_REC( 0UL,   0UL )
02326   };
02327 
02328 
02329   AF_DEFINE_SCRIPT_CLASS(af_latin2_script_class,
02330     AF_SCRIPT_LATIN2,
02331     af_latin2_uniranges,
02332 
02333     sizeof( AF_LatinMetricsRec ),
02334 
02335     (AF_Script_InitMetricsFunc) af_latin2_metrics_init,
02336     (AF_Script_ScaleMetricsFunc)af_latin2_metrics_scale,
02337     (AF_Script_DoneMetricsFunc) NULL,
02338 
02339     (AF_Script_InitHintsFunc)   af_latin2_hints_init,
02340     (AF_Script_ApplyHintsFunc)  af_latin2_hints_apply
02341   )
02342 
02343 
02344 /* END */

Generated on Tue Jul 5 14:13:41 2011 for ROOT_528-00b_version by  doxygen 1.5.1