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