1 /***************************************************************************/
2 /*                                                                         */
3 /*  aflatin.c                                                              */
4 /*                                                                         */
5 /*    Auto-fitter hinting routines for latin writing system (body).        */
6 /*                                                                         */
7 /*  Copyright 2003-2015 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 "afglobal.h"
24 #include "afpic.h"
25 #include "aflatin.h"
26 #include "aferrors.h"
27 
28 
29 #ifdef AF_CONFIG_OPTION_USE_WARPER
30 #include "afwarp.h"
31 #endif
32 
33 
34   /*************************************************************************/
35   /*                                                                       */
36   /* The macro FT_COMPONENT is used in trace mode.  It is an implicit      */
37   /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log  */
38   /* messages during execution.                                            */
39   /*                                                                       */
40 #undef  FT_COMPONENT
41 #define FT_COMPONENT  trace_aflatin
42 
43 
44   /* needed for computation of round vs. flat segments */
45 #define FLAT_THRESHOLD( x )  ( x / 14 )
46 
47 
48   /*************************************************************************/
49   /*************************************************************************/
50   /*****                                                               *****/
51   /*****            L A T I N   G L O B A L   M E T R I C S            *****/
52   /*****                                                               *****/
53   /*************************************************************************/
54   /*************************************************************************/
55 
56 
57   /* Find segments and links, compute all stem widths, and initialize */
58   /* standard width and height for the glyph with given charcode.     */
59 
60   FT_LOCAL_DEF( void )
af_latin_metrics_init_widths(AF_LatinMetrics metrics,FT_Face face)61   af_latin_metrics_init_widths( AF_LatinMetrics  metrics,
62                                 FT_Face          face )
63   {
64     /* scan the array of segments in each direction */
65     AF_GlyphHintsRec  hints[1];
66 
67 
68     FT_TRACE5(( "\n"
69                 "latin standard widths computation (style `%s')\n"
70                 "=====================================================\n"
71                 "\n",
72                 af_style_names[metrics->root.style_class->style] ));
73 
74     af_glyph_hints_init( hints, face->memory );
75 
76     metrics->axis[AF_DIMENSION_HORZ].width_count = 0;
77     metrics->axis[AF_DIMENSION_VERT].width_count = 0;
78 
79     {
80       FT_Error            error;
81       FT_ULong            glyph_index;
82       FT_Long             y_offset;
83       int                 dim;
84       AF_LatinMetricsRec  dummy[1];
85       AF_Scaler           scaler = &dummy->root.scaler;
86 
87 #ifdef FT_CONFIG_OPTION_PIC
88       AF_FaceGlobals  globals = metrics->root.globals;
89 #endif
90 
91       AF_StyleClass   style_class  = metrics->root.style_class;
92       AF_ScriptClass  script_class = AF_SCRIPT_CLASSES_GET
93                                        [style_class->script];
94 
95       FT_UInt32  standard_char;
96 
97 
98       /*
99        * We check more than a single standard character to catch features
100        * like `c2sc' (small caps from caps) that don't contain lowercase
101        * letters by definition, or other features that mainly operate on
102        * numerals.
103        */
104 
105       standard_char = script_class->standard_char1;
106       af_get_char_index( &metrics->root,
107                          standard_char,
108                          &glyph_index,
109                          &y_offset );
110       if ( !glyph_index )
111       {
112         if ( script_class->standard_char2 )
113         {
114           standard_char = script_class->standard_char2;
115           af_get_char_index( &metrics->root,
116                              standard_char,
117                              &glyph_index,
118                              &y_offset );
119           if ( !glyph_index )
120           {
121             if ( script_class->standard_char3 )
122             {
123               standard_char = script_class->standard_char3;
124               af_get_char_index( &metrics->root,
125                                  standard_char,
126                                  &glyph_index,
127                                  &y_offset );
128               if ( !glyph_index )
129                 goto Exit;
130             }
131             else
132               goto Exit;
133           }
134         }
135         else
136           goto Exit;
137       }
138 
139       FT_TRACE5(( "standard character: U+%04lX (glyph index %d)\n",
140                   standard_char, glyph_index ));
141 
142       error = FT_Load_Glyph( face, glyph_index, FT_LOAD_NO_SCALE );
143       if ( error || face->glyph->outline.n_points <= 0 )
144         goto Exit;
145 
146       FT_ZERO( dummy );
147 
148       dummy->units_per_em = metrics->units_per_em;
149 
150       scaler->x_scale = 0x10000L;
151       scaler->y_scale = 0x10000L;
152       scaler->x_delta = 0;
153       scaler->y_delta = 0;
154 
155       scaler->face        = face;
156       scaler->render_mode = FT_RENDER_MODE_NORMAL;
157       scaler->flags       = 0;
158 
159       af_glyph_hints_rescale( hints, (AF_StyleMetrics)dummy );
160 
161       error = af_glyph_hints_reload( hints, &face->glyph->outline );
162       if ( error )
163         goto Exit;
164 
165       for ( dim = 0; dim < AF_DIMENSION_MAX; dim++ )
166       {
167         AF_LatinAxis  axis    = &metrics->axis[dim];
168         AF_AxisHints  axhints = &hints->axis[dim];
169         AF_Segment    seg, limit, link;
170         FT_UInt       num_widths = 0;
171 
172 
173         error = af_latin_hints_compute_segments( hints,
174                                                  (AF_Dimension)dim );
175         if ( error )
176           goto Exit;
177 
178         /*
179          *  We assume that the glyphs selected for the stem width
180          *  computation are `featureless' enough so that the linking
181          *  algorithm works fine without adjustments of its scoring
182          *  function.
183          */
184         af_latin_hints_link_segments( hints,
185                                       0,
186                                       NULL,
187                                       (AF_Dimension)dim );
188 
189         seg   = axhints->segments;
190         limit = seg + axhints->num_segments;
191 
192         for ( ; seg < limit; seg++ )
193         {
194           link = seg->link;
195 
196           /* we only consider stem segments there! */
197           if ( link && link->link == seg && link > seg )
198           {
199             FT_Pos  dist;
200 
201 
202             dist = seg->pos - link->pos;
203             if ( dist < 0 )
204               dist = -dist;
205 
206             if ( num_widths < AF_LATIN_MAX_WIDTHS )
207               axis->widths[num_widths++].org = dist;
208           }
209         }
210 
211         /* this also replaces multiple almost identical stem widths */
212         /* with a single one (the value 100 is heuristic)           */
213         af_sort_and_quantize_widths( &num_widths, axis->widths,
214                                      dummy->units_per_em / 100 );
215         axis->width_count = num_widths;
216       }
217 
218     Exit:
219       for ( dim = 0; dim < AF_DIMENSION_MAX; dim++ )
220       {
221         AF_LatinAxis  axis = &metrics->axis[dim];
222         FT_Pos        stdw;
223 
224 
225         stdw = ( axis->width_count > 0 ) ? axis->widths[0].org
226                                          : AF_LATIN_CONSTANT( metrics, 50 );
227 
228         /* let's try 20% of the smallest width */
229         axis->edge_distance_threshold = stdw / 5;
230         axis->standard_width          = stdw;
231         axis->extra_light             = 0;
232 
233 #ifdef FT_DEBUG_LEVEL_TRACE
234         {
235           FT_UInt  i;
236 
237 
238           FT_TRACE5(( "%s widths:\n",
239                       dim == AF_DIMENSION_VERT ? "horizontal"
240                                                : "vertical" ));
241 
242           FT_TRACE5(( "  %d (standard)", axis->standard_width ));
243           for ( i = 1; i < axis->width_count; i++ )
244             FT_TRACE5(( " %d", axis->widths[i].org ));
245 
246           FT_TRACE5(( "\n" ));
247         }
248 #endif
249       }
250     }
251 
252     FT_TRACE5(( "\n" ));
253 
254     af_glyph_hints_done( hints );
255   }
256 
257 
258   /* Find all blue zones.  Flat segments give the reference points, */
259   /* round segments the overshoot positions.                        */
260 
261   static void
af_latin_metrics_init_blues(AF_LatinMetrics metrics,FT_Face face)262   af_latin_metrics_init_blues( AF_LatinMetrics  metrics,
263                                FT_Face          face )
264   {
265     FT_Pos        flats [AF_BLUE_STRING_MAX_LEN];
266     FT_Pos        rounds[AF_BLUE_STRING_MAX_LEN];
267 
268     FT_UInt       num_flats;
269     FT_UInt       num_rounds;
270 
271     AF_LatinBlue  blue;
272     FT_Error      error;
273     AF_LatinAxis  axis = &metrics->axis[AF_DIMENSION_VERT];
274     FT_Outline    outline;
275 
276     AF_StyleClass  sc = metrics->root.style_class;
277 
278     AF_Blue_Stringset         bss = sc->blue_stringset;
279     const AF_Blue_StringRec*  bs  = &af_blue_stringsets[bss];
280 
281     FT_Pos  flat_threshold = FLAT_THRESHOLD( metrics->units_per_em );
282 
283 
284     /* we walk over the blue character strings as specified in the */
285     /* style's entry in the `af_blue_stringset' array              */
286 
287     FT_TRACE5(( "latin blue zones computation\n"
288                 "============================\n"
289                 "\n" ));
290 
291     for ( ; bs->string != AF_BLUE_STRING_MAX; bs++ )
292     {
293       const char*  p = &af_blue_strings[bs->string];
294       FT_Pos*      blue_ref;
295       FT_Pos*      blue_shoot;
296 
297 
298 #ifdef FT_DEBUG_LEVEL_TRACE
299       {
300         FT_Bool  have_flag = 0;
301 
302 
303         FT_TRACE5(( "blue zone %d", axis->blue_count ));
304 
305         if ( bs->properties )
306         {
307           FT_TRACE5(( " (" ));
308 
309           if ( AF_LATIN_IS_TOP_BLUE( bs ) )
310           {
311             FT_TRACE5(( "top" ));
312             have_flag = 1;
313           }
314 
315           if ( AF_LATIN_IS_NEUTRAL_BLUE( bs ) )
316           {
317             if ( have_flag )
318               FT_TRACE5(( ", " ));
319             FT_TRACE5(( "neutral" ));
320             have_flag = 1;
321           }
322 
323           if ( AF_LATIN_IS_X_HEIGHT_BLUE( bs ) )
324           {
325             if ( have_flag )
326               FT_TRACE5(( ", " ));
327             FT_TRACE5(( "small top" ));
328             have_flag = 1;
329           }
330 
331           if ( AF_LATIN_IS_LONG_BLUE( bs ) )
332           {
333             if ( have_flag )
334               FT_TRACE5(( ", " ));
335             FT_TRACE5(( "long" ));
336           }
337 
338           FT_TRACE5(( ")" ));
339         }
340 
341         FT_TRACE5(( ":\n" ));
342       }
343 #endif /* FT_DEBUG_LEVEL_TRACE */
344 
345       num_flats  = 0;
346       num_rounds = 0;
347 
348       while ( *p )
349       {
350         FT_ULong    ch;
351         FT_ULong    glyph_index;
352         FT_Long     y_offset;
353         FT_Pos      best_y;                            /* same as points.y */
354         FT_Int      best_point, best_contour_first, best_contour_last;
355         FT_Vector*  points;
356         FT_Bool     round = 0;
357 
358 
359         GET_UTF8_CHAR( ch, p );
360 
361         /* load the character in the face -- skip unknown or empty ones */
362         af_get_char_index( &metrics->root, ch, &glyph_index, &y_offset );
363         if ( glyph_index == 0 )
364         {
365           FT_TRACE5(( "  U+%04lX unavailable\n", ch ));
366           continue;
367         }
368 
369         error   = FT_Load_Glyph( face, glyph_index, FT_LOAD_NO_SCALE );
370         outline = face->glyph->outline;
371         /* reject glyphs that don't produce any rendering */
372         if ( error || outline.n_points <= 2 )
373         {
374           FT_TRACE5(( "  U+%04lX contains no (usable) outlines\n", ch ));
375           continue;
376         }
377 
378         /* now compute min or max point indices and coordinates */
379         points             = outline.points;
380         best_point         = -1;
381         best_y             = 0;  /* make compiler happy */
382         best_contour_first = 0;  /* ditto */
383         best_contour_last  = 0;  /* ditto */
384 
385         {
386           FT_Int  nn;
387           FT_Int  first = 0;
388           FT_Int  last  = -1;
389 
390 
391           for ( nn = 0; nn < outline.n_contours; first = last + 1, nn++ )
392           {
393             FT_Int  old_best_point = best_point;
394             FT_Int  pp;
395 
396 
397             last = outline.contours[nn];
398 
399             /* Avoid single-point contours since they are never rasterized. */
400             /* In some fonts, they correspond to mark attachment points     */
401             /* that are way outside of the glyph's real outline.            */
402             if ( last <= first )
403               continue;
404 
405             if ( AF_LATIN_IS_TOP_BLUE( bs ) )
406             {
407               for ( pp = first; pp <= last; pp++ )
408                 if ( best_point < 0 || points[pp].y > best_y )
409                 {
410                   best_point = pp;
411                   best_y     = points[pp].y;
412                 }
413             }
414             else
415             {
416               for ( pp = first; pp <= last; pp++ )
417                 if ( best_point < 0 || points[pp].y < best_y )
418                 {
419                   best_point = pp;
420                   best_y     = points[pp].y;
421                 }
422             }
423 
424             if ( best_point != old_best_point )
425             {
426               best_contour_first = first;
427               best_contour_last  = last;
428             }
429           }
430         }
431 
432         /* now check whether the point belongs to a straight or round   */
433         /* segment; we first need to find in which contour the extremum */
434         /* lies, then inspect its previous and next points              */
435         if ( best_point >= 0 )
436         {
437           FT_Pos  best_x = points[best_point].x;
438           FT_Int  prev, next;
439           FT_Int  best_segment_first, best_segment_last;
440           FT_Int  best_on_point_first, best_on_point_last;
441           FT_Pos  dist;
442 
443 
444           best_segment_first = best_point;
445           best_segment_last  = best_point;
446 
447           if ( FT_CURVE_TAG( outline.tags[best_point] ) == FT_CURVE_TAG_ON )
448           {
449             best_on_point_first = best_point;
450             best_on_point_last  = best_point;
451           }
452           else
453           {
454             best_on_point_first = -1;
455             best_on_point_last  = -1;
456           }
457 
458           /* look for the previous and next points on the contour  */
459           /* that are not on the same Y coordinate, then threshold */
460           /* the `closeness'...                                    */
461           prev = best_point;
462           next = prev;
463 
464           do
465           {
466             if ( prev > best_contour_first )
467               prev--;
468             else
469               prev = best_contour_last;
470 
471             dist = FT_ABS( points[prev].y - best_y );
472             /* accept a small distance or a small angle (both values are */
473             /* heuristic; value 20 corresponds to approx. 2.9 degrees)   */
474             if ( dist > 5 )
475               if ( FT_ABS( points[prev].x - best_x ) <= 20 * dist )
476                 break;
477 
478             best_segment_first = prev;
479 
480             if ( FT_CURVE_TAG( outline.tags[prev] ) == FT_CURVE_TAG_ON )
481             {
482               best_on_point_first = prev;
483               if ( best_on_point_last < 0 )
484                 best_on_point_last = prev;
485             }
486 
487           } while ( prev != best_point );
488 
489           do
490           {
491             if ( next < best_contour_last )
492               next++;
493             else
494               next = best_contour_first;
495 
496             dist = FT_ABS( points[next].y - best_y );
497             if ( dist > 5 )
498               if ( FT_ABS( points[next].x - best_x ) <= 20 * dist )
499                 break;
500 
501             best_segment_last = next;
502 
503             if ( FT_CURVE_TAG( outline.tags[next] ) == FT_CURVE_TAG_ON )
504             {
505               best_on_point_last = next;
506               if ( best_on_point_first < 0 )
507                 best_on_point_first = next;
508             }
509 
510           } while ( next != best_point );
511 
512           if ( AF_LATIN_IS_LONG_BLUE( bs ) )
513           {
514             /* If this flag is set, we have an additional constraint to  */
515             /* get the blue zone distance: Find a segment of the topmost */
516             /* (or bottommost) contour that is longer than a heuristic   */
517             /* threshold.  This ensures that small bumps in the outline  */
518             /* are ignored (for example, the `vertical serifs' found in  */
519             /* many Hebrew glyph designs).                               */
520 
521             /* If this segment is long enough, we are done.  Otherwise,  */
522             /* search the segment next to the extremum that is long      */
523             /* enough, has the same direction, and a not too large       */
524             /* vertical distance from the extremum.  Note that the       */
525             /* algorithm doesn't check whether the found segment is      */
526             /* actually the one (vertically) nearest to the extremum.    */
527 
528             /* heuristic threshold value */
529             FT_Pos  length_threshold = metrics->units_per_em / 25;
530 
531 
532             dist = FT_ABS( points[best_segment_last].x -
533                              points[best_segment_first].x );
534 
535             if ( dist < length_threshold                       &&
536                  best_segment_last - best_segment_first + 2 <=
537                    best_contour_last - best_contour_first      )
538             {
539               /* heuristic threshold value */
540               FT_Pos  height_threshold = metrics->units_per_em / 4;
541 
542               FT_Int   first;
543               FT_Int   last;
544               FT_Bool  hit;
545 
546               /* we intentionally declare these two variables        */
547               /* outside of the loop since various compilers emit    */
548               /* incorrect warning messages otherwise, talking about */
549               /* `possibly uninitialized variables'                  */
550               FT_Int  p_first = 0;            /* make compiler happy */
551               FT_Int  p_last  = 0;
552 
553               FT_Bool  left2right;
554 
555 
556               /* compute direction */
557               prev = best_point;
558 
559               do
560               {
561                 if ( prev > best_contour_first )
562                   prev--;
563                 else
564                   prev = best_contour_last;
565 
566                 if ( points[prev].x != best_x )
567                   break;
568 
569               } while ( prev != best_point );
570 
571               /* skip glyph for the degenerate case */
572               if ( prev == best_point )
573                 continue;
574 
575               left2right = FT_BOOL( points[prev].x < points[best_point].x );
576 
577               first = best_segment_last;
578               last  = first;
579               hit   = 0;
580 
581               do
582               {
583                 FT_Bool  l2r;
584                 FT_Pos   d;
585 
586 
587                 if ( !hit )
588                 {
589                   /* no hit; adjust first point */
590                   first = last;
591 
592                   /* also adjust first and last on point */
593                   if ( FT_CURVE_TAG( outline.tags[first] ) ==
594                          FT_CURVE_TAG_ON )
595                   {
596                     p_first = first;
597                     p_last  = first;
598                   }
599                   else
600                   {
601                     p_first = -1;
602                     p_last  = -1;
603                   }
604 
605                   hit = 1;
606                 }
607 
608                 if ( last < best_contour_last )
609                   last++;
610                 else
611                   last = best_contour_first;
612 
613                 if ( FT_ABS( best_y - points[first].y ) > height_threshold )
614                 {
615                   /* vertical distance too large */
616                   hit = 0;
617                   continue;
618                 }
619 
620                 /* same test as above */
621                 dist = FT_ABS( points[last].y - points[first].y );
622                 if ( dist > 5 )
623                   if ( FT_ABS( points[last].x - points[first].x ) <=
624                          20 * dist )
625                   {
626                     hit = 0;
627                     continue;
628                   }
629 
630                 if ( FT_CURVE_TAG( outline.tags[last] ) == FT_CURVE_TAG_ON )
631                 {
632                   p_last = last;
633                   if ( p_first < 0 )
634                     p_first = last;
635                 }
636 
637                 l2r = FT_BOOL( points[first].x < points[last].x );
638                 d   = FT_ABS( points[last].x - points[first].x );
639 
640                 if ( l2r == left2right     &&
641                      d >= length_threshold )
642                 {
643                   /* all constraints are met; update segment after finding */
644                   /* its end                                               */
645                   do
646                   {
647                     if ( last < best_contour_last )
648                       last++;
649                     else
650                       last = best_contour_first;
651 
652                     d = FT_ABS( points[last].y - points[first].y );
653                     if ( d > 5 )
654                       if ( FT_ABS( points[next].x - points[first].x ) <=
655                              20 * dist )
656                       {
657                         if ( last > best_contour_first )
658                           last--;
659                         else
660                           last = best_contour_last;
661                         break;
662                       }
663 
664                     p_last = last;
665 
666                     if ( FT_CURVE_TAG( outline.tags[last] ) ==
667                            FT_CURVE_TAG_ON )
668                     {
669                       p_last = last;
670                       if ( p_first < 0 )
671                         p_first = last;
672                     }
673 
674                   } while ( last != best_segment_first );
675 
676                   best_y = points[first].y;
677 
678                   best_segment_first = first;
679                   best_segment_last  = last;
680 
681                   best_on_point_first = p_first;
682                   best_on_point_last  = p_last;
683 
684                   break;
685                 }
686 
687               } while ( last != best_segment_first );
688             }
689           }
690 
691           /* for computing blue zones, we add the y offset as returned */
692           /* by the currently used OpenType feature -- for example,    */
693           /* superscript glyphs might be identical to subscript glyphs */
694           /* with a vertical shift                                     */
695           best_y += y_offset;
696 
697           FT_TRACE5(( "  U+%04lX: best_y = %5ld", ch, best_y ));
698 
699           /* now set the `round' flag depending on the segment's kind: */
700           /*                                                           */
701           /* - if the horizontal distance between the first and last   */
702           /*   `on' point is larger than a heuristic threshold         */
703           /*   we have a flat segment                                  */
704           /* - if either the first or the last point of the segment is */
705           /*   an `off' point, the segment is round, otherwise it is   */
706           /*   flat                                                    */
707           if ( best_on_point_first >= 0                               &&
708                best_on_point_last >= 0                                &&
709                ( FT_ABS( points[best_on_point_last].x -
710                          points[best_on_point_first].x ) ) >
711                  flat_threshold                                       )
712             round = 0;
713           else
714             round = FT_BOOL(
715                       FT_CURVE_TAG( outline.tags[best_segment_first] ) !=
716                         FT_CURVE_TAG_ON                                   ||
717                       FT_CURVE_TAG( outline.tags[best_segment_last]  ) !=
718                         FT_CURVE_TAG_ON                                   );
719 
720           if ( round && AF_LATIN_IS_NEUTRAL_BLUE( bs ) )
721           {
722             /* only use flat segments for a neutral blue zone */
723             FT_TRACE5(( " (round, skipped)\n" ));
724             continue;
725           }
726 
727           FT_TRACE5(( " (%s)\n", round ? "round" : "flat" ));
728         }
729 
730         if ( round )
731           rounds[num_rounds++] = best_y;
732         else
733           flats[num_flats++]   = best_y;
734       }
735 
736       if ( num_flats == 0 && num_rounds == 0 )
737       {
738         /*
739          *  we couldn't find a single glyph to compute this blue zone,
740          *  we will simply ignore it then
741          */
742         FT_TRACE5(( "  empty\n" ));
743         continue;
744       }
745 
746       /* we have computed the contents of the `rounds' and `flats' tables, */
747       /* now determine the reference and overshoot position of the blue -- */
748       /* we simply take the median value after a simple sort               */
749       af_sort_pos( num_rounds, rounds );
750       af_sort_pos( num_flats,  flats );
751 
752       blue       = &axis->blues[axis->blue_count];
753       blue_ref   = &blue->ref.org;
754       blue_shoot = &blue->shoot.org;
755 
756       axis->blue_count++;
757 
758       if ( num_flats == 0 )
759       {
760         *blue_ref   =
761         *blue_shoot = rounds[num_rounds / 2];
762       }
763       else if ( num_rounds == 0 )
764       {
765         *blue_ref   =
766         *blue_shoot = flats[num_flats / 2];
767       }
768       else
769       {
770         *blue_ref   = flats [num_flats  / 2];
771         *blue_shoot = rounds[num_rounds / 2];
772       }
773 
774       /* there are sometimes problems: if the overshoot position of top     */
775       /* zones is under its reference position, or the opposite for bottom  */
776       /* zones.  We must thus check everything there and correct the errors */
777       if ( *blue_shoot != *blue_ref )
778       {
779         FT_Pos   ref      = *blue_ref;
780         FT_Pos   shoot    = *blue_shoot;
781         FT_Bool  over_ref = FT_BOOL( shoot > ref );
782 
783 
784         if ( AF_LATIN_IS_TOP_BLUE( bs ) ^ over_ref )
785         {
786           *blue_ref   =
787           *blue_shoot = ( shoot + ref ) / 2;
788 
789           FT_TRACE5(( "  [overshoot smaller than reference,"
790                       " taking mean value]\n" ));
791         }
792       }
793 
794       blue->flags = 0;
795       if ( AF_LATIN_IS_TOP_BLUE( bs ) )
796         blue->flags |= AF_LATIN_BLUE_TOP;
797       if ( AF_LATIN_IS_NEUTRAL_BLUE( bs ) )
798         blue->flags |= AF_LATIN_BLUE_NEUTRAL;
799 
800       /*
801        * The following flag is used later to adjust the y and x scales
802        * in order to optimize the pixel grid alignment of the top of small
803        * letters.
804        */
805       if ( AF_LATIN_IS_X_HEIGHT_BLUE( bs ) )
806         blue->flags |= AF_LATIN_BLUE_ADJUSTMENT;
807 
808       FT_TRACE5(( "    -> reference = %ld\n"
809                   "       overshoot = %ld\n",
810                   *blue_ref, *blue_shoot ));
811     }
812 
813     FT_TRACE5(( "\n" ));
814 
815     return;
816   }
817 
818 
819   /* Check whether all ASCII digits have the same advance width. */
820 
821   FT_LOCAL_DEF( void )
af_latin_metrics_check_digits(AF_LatinMetrics metrics,FT_Face face)822   af_latin_metrics_check_digits( AF_LatinMetrics  metrics,
823                                  FT_Face          face )
824   {
825     FT_UInt   i;
826     FT_Bool   started = 0, same_width = 1;
827     FT_Fixed  advance, old_advance = 0;
828 
829 
830     /* digit `0' is 0x30 in all supported charmaps */
831     for ( i = 0x30; i <= 0x39; i++ )
832     {
833       FT_ULong  glyph_index;
834       FT_Long   y_offset;
835 
836 
837       af_get_char_index( &metrics->root, i, &glyph_index, &y_offset );
838       if ( glyph_index == 0 )
839         continue;
840 
841       if ( FT_Get_Advance( face, glyph_index,
842                            FT_LOAD_NO_SCALE         |
843                            FT_LOAD_NO_HINTING       |
844                            FT_LOAD_IGNORE_TRANSFORM,
845                            &advance ) )
846         continue;
847 
848       if ( started )
849       {
850         if ( advance != old_advance )
851         {
852           same_width = 0;
853           break;
854         }
855       }
856       else
857       {
858         old_advance = advance;
859         started     = 1;
860       }
861     }
862 
863     metrics->root.digits_have_same_width = same_width;
864   }
865 
866 
867   /* Initialize global metrics. */
868 
869   FT_LOCAL_DEF( FT_Error )
af_latin_metrics_init(AF_LatinMetrics metrics,FT_Face face)870   af_latin_metrics_init( AF_LatinMetrics  metrics,
871                          FT_Face          face )
872   {
873     FT_CharMap  oldmap = face->charmap;
874 
875 
876     metrics->units_per_em = face->units_per_EM;
877 
878     if ( !FT_Select_Charmap( face, FT_ENCODING_UNICODE ) )
879     {
880       af_latin_metrics_init_widths( metrics, face );
881       af_latin_metrics_init_blues( metrics, face );
882       af_latin_metrics_check_digits( metrics, face );
883     }
884 
885     FT_Set_Charmap( face, oldmap );
886     return FT_Err_Ok;
887   }
888 
889 
890   /* Adjust scaling value, then scale and shift widths   */
891   /* and blue zones (if applicable) for given dimension. */
892 
893   static void
af_latin_metrics_scale_dim(AF_LatinMetrics metrics,AF_Scaler scaler,AF_Dimension dim)894   af_latin_metrics_scale_dim( AF_LatinMetrics  metrics,
895                               AF_Scaler        scaler,
896                               AF_Dimension     dim )
897   {
898     FT_Fixed      scale;
899     FT_Pos        delta;
900     AF_LatinAxis  axis;
901     FT_UInt       nn;
902 
903 
904     if ( dim == AF_DIMENSION_HORZ )
905     {
906       scale = scaler->x_scale;
907       delta = scaler->x_delta;
908     }
909     else
910     {
911       scale = scaler->y_scale;
912       delta = scaler->y_delta;
913     }
914 
915     axis = &metrics->axis[dim];
916 
917     if ( axis->org_scale == scale && axis->org_delta == delta )
918       return;
919 
920     axis->org_scale = scale;
921     axis->org_delta = delta;
922 
923     /*
924      * correct X and Y scale to optimize the alignment of the top of small
925      * letters to the pixel grid
926      */
927     {
928       AF_LatinAxis  Axis = &metrics->axis[AF_DIMENSION_VERT];
929       AF_LatinBlue  blue = NULL;
930 
931 
932       for ( nn = 0; nn < Axis->blue_count; nn++ )
933       {
934         if ( Axis->blues[nn].flags & AF_LATIN_BLUE_ADJUSTMENT )
935         {
936           blue = &Axis->blues[nn];
937           break;
938         }
939       }
940 
941       if ( blue )
942       {
943         FT_Pos   scaled;
944         FT_Pos   threshold;
945         FT_Pos   fitted;
946         FT_UInt  limit;
947         FT_UInt  ppem;
948 
949 
950         scaled    = FT_MulFix( blue->shoot.org, scaler->y_scale );
951         ppem      = metrics->root.scaler.face->size->metrics.x_ppem;
952         limit     = metrics->root.globals->increase_x_height;
953         threshold = 40;
954 
955         /* if the `increase-x-height' property is active, */
956         /* we round up much more often                    */
957         if ( limit                                 &&
958              ppem <= limit                         &&
959              ppem >= AF_PROP_INCREASE_X_HEIGHT_MIN )
960           threshold = 52;
961 
962         fitted = ( scaled + threshold ) & ~63;
963 
964         if ( scaled != fitted )
965         {
966 #if 0
967           if ( dim == AF_DIMENSION_HORZ )
968           {
969             if ( fitted < scaled )
970               scale -= scale / 50;  /* scale *= 0.98 */
971           }
972           else
973 #endif
974           if ( dim == AF_DIMENSION_VERT )
975           {
976             scale = FT_MulDiv( scale, fitted, scaled );
977 
978             FT_TRACE5((
979               "af_latin_metrics_scale_dim:"
980               " x height alignment (style `%s'):\n"
981               "                           "
982               " vertical scaling changed from %.4f to %.4f (by %d%%)\n"
983               "\n",
984               af_style_names[metrics->root.style_class->style],
985               axis->org_scale / 65536.0,
986               scale / 65536.0,
987               ( fitted - scaled ) * 100 / scaled ));
988           }
989         }
990       }
991     }
992 
993     axis->scale = scale;
994     axis->delta = delta;
995 
996     if ( dim == AF_DIMENSION_HORZ )
997     {
998       metrics->root.scaler.x_scale = scale;
999       metrics->root.scaler.x_delta = delta;
1000     }
1001     else
1002     {
1003       metrics->root.scaler.y_scale = scale;
1004       metrics->root.scaler.y_delta = delta;
1005     }
1006 
1007     FT_TRACE5(( "%s widths (style `%s')\n",
1008                 dim == AF_DIMENSION_HORZ ? "horizontal" : "vertical",
1009                 af_style_names[metrics->root.style_class->style] ));
1010 
1011     /* scale the widths */
1012     for ( nn = 0; nn < axis->width_count; nn++ )
1013     {
1014       AF_Width  width = axis->widths + nn;
1015 
1016 
1017       width->cur = FT_MulFix( width->org, scale );
1018       width->fit = width->cur;
1019 
1020       FT_TRACE5(( "  %d scaled to %.2f\n",
1021                   width->org,
1022                   width->cur / 64.0 ));
1023     }
1024 
1025     FT_TRACE5(( "\n" ));
1026 
1027     /* an extra-light axis corresponds to a standard width that is */
1028     /* smaller than 5/8 pixels                                     */
1029     axis->extra_light =
1030       (FT_Bool)( FT_MulFix( axis->standard_width, scale ) < 32 + 8 );
1031 
1032 #ifdef FT_DEBUG_LEVEL_TRACE
1033     if ( axis->extra_light )
1034       FT_TRACE5(( "`%s' style is extra light (at current resolution)\n"
1035                   "\n",
1036                   af_style_names[metrics->root.style_class->style] ));
1037 #endif
1038 
1039     if ( dim == AF_DIMENSION_VERT )
1040     {
1041 #ifdef FT_DEBUG_LEVEL_TRACE
1042       if ( axis->blue_count )
1043         FT_TRACE5(( "blue zones (style `%s')\n",
1044                     af_style_names[metrics->root.style_class->style] ));
1045 #endif
1046 
1047       /* scale the blue zones */
1048       for ( nn = 0; nn < axis->blue_count; nn++ )
1049       {
1050         AF_LatinBlue  blue = &axis->blues[nn];
1051         FT_Pos        dist;
1052 
1053 
1054         blue->ref.cur   = FT_MulFix( blue->ref.org, scale ) + delta;
1055         blue->ref.fit   = blue->ref.cur;
1056         blue->shoot.cur = FT_MulFix( blue->shoot.org, scale ) + delta;
1057         blue->shoot.fit = blue->shoot.cur;
1058         blue->flags    &= ~AF_LATIN_BLUE_ACTIVE;
1059 
1060         /* a blue zone is only active if it is less than 3/4 pixels tall */
1061         dist = FT_MulFix( blue->ref.org - blue->shoot.org, scale );
1062         if ( dist <= 48 && dist >= -48 )
1063         {
1064 #if 0
1065           FT_Pos  delta1;
1066 #endif
1067           FT_Pos  delta2;
1068 
1069 
1070           /* use discrete values for blue zone widths */
1071 
1072 #if 0
1073 
1074           /* generic, original code */
1075           delta1 = blue->shoot.org - blue->ref.org;
1076           delta2 = delta1;
1077           if ( delta1 < 0 )
1078             delta2 = -delta2;
1079 
1080           delta2 = FT_MulFix( delta2, scale );
1081 
1082           if ( delta2 < 32 )
1083             delta2 = 0;
1084           else if ( delta2 < 64 )
1085             delta2 = 32 + ( ( ( delta2 - 32 ) + 16 ) & ~31 );
1086           else
1087             delta2 = FT_PIX_ROUND( delta2 );
1088 
1089           if ( delta1 < 0 )
1090             delta2 = -delta2;
1091 
1092           blue->ref.fit   = FT_PIX_ROUND( blue->ref.cur );
1093           blue->shoot.fit = blue->ref.fit + delta2;
1094 
1095 #else
1096 
1097           /* simplified version due to abs(dist) <= 48 */
1098           delta2 = dist;
1099           if ( dist < 0 )
1100             delta2 = -delta2;
1101 
1102           if ( delta2 < 32 )
1103             delta2 = 0;
1104           else if ( delta2 < 48 )
1105             delta2 = 32;
1106           else
1107             delta2 = 64;
1108 
1109           if ( dist < 0 )
1110             delta2 = -delta2;
1111 
1112           blue->ref.fit   = FT_PIX_ROUND( blue->ref.cur );
1113           blue->shoot.fit = blue->ref.fit - delta2;
1114 
1115 #endif
1116 
1117           blue->flags |= AF_LATIN_BLUE_ACTIVE;
1118 
1119           FT_TRACE5(( "  reference %d: %d scaled to %.2f%s\n"
1120                       "  overshoot %d: %d scaled to %.2f%s\n",
1121                       nn,
1122                       blue->ref.org,
1123                       blue->ref.fit / 64.0,
1124                       blue->flags & AF_LATIN_BLUE_ACTIVE ? ""
1125                                                          : " (inactive)",
1126                       nn,
1127                       blue->shoot.org,
1128                       blue->shoot.fit / 64.0,
1129                       blue->flags & AF_LATIN_BLUE_ACTIVE ? ""
1130                                                          : " (inactive)" ));
1131         }
1132       }
1133     }
1134   }
1135 
1136 
1137   /* Scale global values in both directions. */
1138 
1139   FT_LOCAL_DEF( void )
af_latin_metrics_scale(AF_LatinMetrics metrics,AF_Scaler scaler)1140   af_latin_metrics_scale( AF_LatinMetrics  metrics,
1141                           AF_Scaler        scaler )
1142   {
1143     metrics->root.scaler.render_mode = scaler->render_mode;
1144     metrics->root.scaler.face        = scaler->face;
1145     metrics->root.scaler.flags       = scaler->flags;
1146 
1147     af_latin_metrics_scale_dim( metrics, scaler, AF_DIMENSION_HORZ );
1148     af_latin_metrics_scale_dim( metrics, scaler, AF_DIMENSION_VERT );
1149   }
1150 
1151 
1152   /*************************************************************************/
1153   /*************************************************************************/
1154   /*****                                                               *****/
1155   /*****           L A T I N   G L Y P H   A N A L Y S I S             *****/
1156   /*****                                                               *****/
1157   /*************************************************************************/
1158   /*************************************************************************/
1159 
1160 
1161   /* Walk over all contours and compute its segments. */
1162 
1163   FT_LOCAL_DEF( FT_Error )
af_latin_hints_compute_segments(AF_GlyphHints hints,AF_Dimension dim)1164   af_latin_hints_compute_segments( AF_GlyphHints  hints,
1165                                    AF_Dimension   dim )
1166   {
1167     AF_LatinMetrics  metrics       = (AF_LatinMetrics)hints->metrics;
1168     AF_AxisHints     axis          = &hints->axis[dim];
1169     FT_Memory        memory        = hints->memory;
1170     FT_Error         error         = FT_Err_Ok;
1171     AF_Segment       segment       = NULL;
1172     AF_SegmentRec    seg0;
1173     AF_Point*        contour       = hints->contours;
1174     AF_Point*        contour_limit = contour + hints->num_contours;
1175     AF_Direction     major_dir, segment_dir;
1176 
1177     FT_Pos  flat_threshold = FLAT_THRESHOLD( metrics->units_per_em );
1178 
1179 
1180     FT_ZERO( &seg0 );
1181     seg0.score = 32000;
1182     seg0.flags = AF_EDGE_NORMAL;
1183 
1184     major_dir   = (AF_Direction)FT_ABS( axis->major_dir );
1185     segment_dir = major_dir;
1186 
1187     axis->num_segments = 0;
1188 
1189     /* set up (u,v) in each point */
1190     if ( dim == AF_DIMENSION_HORZ )
1191     {
1192       AF_Point  point = hints->points;
1193       AF_Point  limit = point + hints->num_points;
1194 
1195 
1196       for ( ; point < limit; point++ )
1197       {
1198         point->u = point->fx;
1199         point->v = point->fy;
1200       }
1201     }
1202     else
1203     {
1204       AF_Point  point = hints->points;
1205       AF_Point  limit = point + hints->num_points;
1206 
1207 
1208       for ( ; point < limit; point++ )
1209       {
1210         point->u = point->fy;
1211         point->v = point->fx;
1212       }
1213     }
1214 
1215     /* do each contour separately */
1216     for ( ; contour < contour_limit; contour++ )
1217     {
1218       AF_Point  point      =  contour[0];
1219       AF_Point  last       =  point->prev;
1220       int       on_edge    =  0;
1221       FT_Pos    min_pos    =  32000;  /* minimum segment pos != min_coord */
1222       FT_Pos    max_pos    = -32000;  /* maximum segment pos != max_coord */
1223       FT_Pos    min_on_pos =  32000;
1224       FT_Pos    max_on_pos = -32000;
1225       FT_Bool   passed;
1226 
1227 
1228       if ( point == last )  /* skip singletons -- just in case */
1229         continue;
1230 
1231       if ( FT_ABS( last->out_dir )  == major_dir &&
1232            FT_ABS( point->out_dir ) == major_dir )
1233       {
1234         /* we are already on an edge, try to locate its start */
1235         last = point;
1236 
1237         for (;;)
1238         {
1239           point = point->prev;
1240           if ( FT_ABS( point->out_dir ) != major_dir )
1241           {
1242             point = point->next;
1243             break;
1244           }
1245           if ( point == last )
1246             break;
1247         }
1248       }
1249 
1250       last   = point;
1251       passed = 0;
1252 
1253       for (;;)
1254       {
1255         FT_Pos  u, v;
1256 
1257 
1258         if ( on_edge )
1259         {
1260           u = point->u;
1261           if ( u < min_pos )
1262             min_pos = u;
1263           if ( u > max_pos )
1264             max_pos = u;
1265 
1266           /* get minimum and maximum coordinate of on points */
1267           if ( !( point->flags & AF_FLAG_CONTROL ) )
1268           {
1269             v = point->v;
1270             if ( v < min_on_pos )
1271               min_on_pos = v;
1272             if ( v > max_on_pos )
1273               max_on_pos = v;
1274           }
1275 
1276           if ( point->out_dir != segment_dir || point == last )
1277           {
1278             /* we are just leaving an edge; record a new segment! */
1279             segment->last = point;
1280             segment->pos  = (FT_Short)( ( min_pos + max_pos ) >> 1 );
1281 
1282             /* a segment is round if either its first or last point */
1283             /* is a control point, and the length of the on points  */
1284             /* inbetween doesn't exceed a heuristic limit           */
1285             if ( ( segment->first->flags | point->flags ) & AF_FLAG_CONTROL &&
1286                  ( max_on_pos - min_on_pos ) < flat_threshold               )
1287               segment->flags |= AF_EDGE_ROUND;
1288 
1289             /* compute segment size */
1290             min_pos = max_pos = point->v;
1291 
1292             v = segment->first->v;
1293             if ( v < min_pos )
1294               min_pos = v;
1295             if ( v > max_pos )
1296               max_pos = v;
1297 
1298             segment->min_coord = (FT_Short)min_pos;
1299             segment->max_coord = (FT_Short)max_pos;
1300             segment->height    = (FT_Short)( segment->max_coord -
1301                                              segment->min_coord );
1302 
1303             on_edge = 0;
1304             segment = NULL;
1305             /* fall through */
1306           }
1307         }
1308 
1309         /* now exit if we are at the start/end point */
1310         if ( point == last )
1311         {
1312           if ( passed )
1313             break;
1314           passed = 1;
1315         }
1316 
1317         if ( !on_edge && FT_ABS( point->out_dir ) == major_dir )
1318         {
1319           /* this is the start of a new segment! */
1320           segment_dir = (AF_Direction)point->out_dir;
1321 
1322           error = af_axis_hints_new_segment( axis, memory, &segment );
1323           if ( error )
1324             goto Exit;
1325 
1326           /* clear all segment fields */
1327           segment[0] = seg0;
1328 
1329           segment->dir   = (FT_Char)segment_dir;
1330           segment->first = point;
1331           segment->last  = point;
1332 
1333           min_pos = max_pos = point->u;
1334 
1335           if ( point->flags & AF_FLAG_CONTROL )
1336           {
1337             min_on_pos =  32000;
1338             max_on_pos = -32000;
1339           }
1340           else
1341             min_on_pos = max_on_pos = point->v;
1342 
1343           on_edge = 1;
1344         }
1345 
1346         point = point->next;
1347       }
1348 
1349     } /* contours */
1350 
1351 
1352     /* now slightly increase the height of segments if this makes */
1353     /* sense -- this is used to better detect and ignore serifs   */
1354     {
1355       AF_Segment  segments     = axis->segments;
1356       AF_Segment  segments_end = segments + axis->num_segments;
1357 
1358 
1359       for ( segment = segments; segment < segments_end; segment++ )
1360       {
1361         AF_Point  first   = segment->first;
1362         AF_Point  last    = segment->last;
1363         FT_Pos    first_v = first->v;
1364         FT_Pos    last_v  = last->v;
1365 
1366 
1367         if ( first_v < last_v )
1368         {
1369           AF_Point  p;
1370 
1371 
1372           p = first->prev;
1373           if ( p->v < first_v )
1374             segment->height = (FT_Short)( segment->height +
1375                                           ( ( first_v - p->v ) >> 1 ) );
1376 
1377           p = last->next;
1378           if ( p->v > last_v )
1379             segment->height = (FT_Short)( segment->height +
1380                                           ( ( p->v - last_v ) >> 1 ) );
1381         }
1382         else
1383         {
1384           AF_Point  p;
1385 
1386 
1387           p = first->prev;
1388           if ( p->v > first_v )
1389             segment->height = (FT_Short)( segment->height +
1390                                           ( ( p->v - first_v ) >> 1 ) );
1391 
1392           p = last->next;
1393           if ( p->v < last_v )
1394             segment->height = (FT_Short)( segment->height +
1395                                           ( ( last_v - p->v ) >> 1 ) );
1396         }
1397       }
1398     }
1399 
1400   Exit:
1401     return error;
1402   }
1403 
1404 
1405   /* Link segments to form stems and serifs.  If `width_count' and      */
1406   /* `widths' are non-zero, use them to fine-tune the scoring function. */
1407 
1408   FT_LOCAL_DEF( void )
af_latin_hints_link_segments(AF_GlyphHints hints,FT_UInt width_count,AF_WidthRec * widths,AF_Dimension dim)1409   af_latin_hints_link_segments( AF_GlyphHints  hints,
1410                                 FT_UInt        width_count,
1411                                 AF_WidthRec*   widths,
1412                                 AF_Dimension   dim )
1413   {
1414     AF_AxisHints  axis          = &hints->axis[dim];
1415     AF_Segment    segments      = axis->segments;
1416     AF_Segment    segment_limit = segments + axis->num_segments;
1417     FT_Pos        len_threshold, len_score, dist_score, max_width;
1418     AF_Segment    seg1, seg2;
1419 
1420 
1421     if ( width_count )
1422       max_width = widths[width_count - 1].org;
1423     else
1424       max_width = 0;
1425 
1426     /* a heuristic value to set up a minimum value for overlapping */
1427     len_threshold = AF_LATIN_CONSTANT( hints->metrics, 8 );
1428     if ( len_threshold == 0 )
1429       len_threshold = 1;
1430 
1431     /* a heuristic value to weight lengths */
1432     len_score = AF_LATIN_CONSTANT( hints->metrics, 6000 );
1433 
1434     /* a heuristic value to weight distances (no call to    */
1435     /* AF_LATIN_CONSTANT needed, since we work on multiples */
1436     /* of the stem width)                                   */
1437     dist_score = 3000;
1438 
1439     /* now compare each segment to the others */
1440     for ( seg1 = segments; seg1 < segment_limit; seg1++ )
1441     {
1442       if ( seg1->dir != axis->major_dir )
1443         continue;
1444 
1445       /* search for stems having opposite directions, */
1446       /* with seg1 to the `left' of seg2              */
1447       for ( seg2 = segments; seg2 < segment_limit; seg2++ )
1448       {
1449         FT_Pos  pos1 = seg1->pos;
1450         FT_Pos  pos2 = seg2->pos;
1451 
1452 
1453         if ( seg1->dir + seg2->dir == 0 && pos2 > pos1 )
1454         {
1455           /* compute distance between the two segments */
1456           FT_Pos  min = seg1->min_coord;
1457           FT_Pos  max = seg1->max_coord;
1458           FT_Pos  len;
1459 
1460 
1461           if ( min < seg2->min_coord )
1462             min = seg2->min_coord;
1463 
1464           if ( max > seg2->max_coord )
1465             max = seg2->max_coord;
1466 
1467           /* compute maximum coordinate difference of the two segments */
1468           /* (this is, how much they overlap)                          */
1469           len = max - min;
1470           if ( len >= len_threshold )
1471           {
1472             /*
1473              *  The score is the sum of two demerits indicating the
1474              *  `badness' of a fit, measured along the segments' main axis
1475              *  and orthogonal to it, respectively.
1476              *
1477              *  o The less overlapping along the main axis, the worse it
1478              *    is, causing a larger demerit.
1479              *
1480              *  o The nearer the orthogonal distance to a stem width, the
1481              *    better it is, causing a smaller demerit.  For simplicity,
1482              *    however, we only increase the demerit for values that
1483              *    exceed the largest stem width.
1484              */
1485 
1486             FT_Pos  dist = pos2 - pos1;
1487 
1488             FT_Pos  dist_demerit, score;
1489 
1490 
1491             if ( max_width )
1492             {
1493               /* distance demerits are based on multiples of `max_width'; */
1494               /* we scale by 1024 for getting more precision              */
1495               FT_Pos  delta = ( dist << 10 ) / max_width - ( 1 << 10 );
1496 
1497 
1498               if ( delta > 10000 )
1499                 dist_demerit = 32000;
1500               else if ( delta > 0 )
1501                 dist_demerit = delta * delta / dist_score;
1502               else
1503                 dist_demerit = 0;
1504             }
1505             else
1506               dist_demerit = dist; /* default if no widths available */
1507 
1508             score = dist_demerit + len_score / len;
1509 
1510             /* and we search for the smallest score */
1511             if ( score < seg1->score )
1512             {
1513               seg1->score = score;
1514               seg1->link  = seg2;
1515             }
1516 
1517             if ( score < seg2->score )
1518             {
1519               seg2->score = score;
1520               seg2->link  = seg1;
1521             }
1522           }
1523         }
1524       }
1525     }
1526 
1527     /* now compute the `serif' segments, cf. explanations in `afhints.h' */
1528     for ( seg1 = segments; seg1 < segment_limit; seg1++ )
1529     {
1530       seg2 = seg1->link;
1531 
1532       if ( seg2 )
1533       {
1534         if ( seg2->link != seg1 )
1535         {
1536           seg1->link  = 0;
1537           seg1->serif = seg2->link;
1538         }
1539       }
1540     }
1541   }
1542 
1543 
1544   /* Link segments to edges, using feature analysis for selection. */
1545 
1546   FT_LOCAL_DEF( FT_Error )
af_latin_hints_compute_edges(AF_GlyphHints hints,AF_Dimension dim)1547   af_latin_hints_compute_edges( AF_GlyphHints  hints,
1548                                 AF_Dimension   dim )
1549   {
1550     AF_AxisHints  axis   = &hints->axis[dim];
1551     FT_Error      error  = FT_Err_Ok;
1552     FT_Memory     memory = hints->memory;
1553     AF_LatinAxis  laxis  = &((AF_LatinMetrics)hints->metrics)->axis[dim];
1554 
1555     AF_Segment    segments      = axis->segments;
1556     AF_Segment    segment_limit = segments + axis->num_segments;
1557     AF_Segment    seg;
1558 
1559 #if 0
1560     AF_Direction  up_dir;
1561 #endif
1562     FT_Fixed      scale;
1563     FT_Pos        edge_distance_threshold;
1564     FT_Pos        segment_length_threshold;
1565 
1566 
1567     axis->num_edges = 0;
1568 
1569     scale = ( dim == AF_DIMENSION_HORZ ) ? hints->x_scale
1570                                          : hints->y_scale;
1571 
1572 #if 0
1573     up_dir = ( dim == AF_DIMENSION_HORZ ) ? AF_DIR_UP
1574                                           : AF_DIR_RIGHT;
1575 #endif
1576 
1577     /*
1578      *  We ignore all segments that are less than 1 pixel in length
1579      *  to avoid many problems with serif fonts.  We compute the
1580      *  corresponding threshold in font units.
1581      */
1582     if ( dim == AF_DIMENSION_HORZ )
1583         segment_length_threshold = FT_DivFix( 64, hints->y_scale );
1584     else
1585         segment_length_threshold = 0;
1586 
1587     /*********************************************************************/
1588     /*                                                                   */
1589     /* We begin by generating a sorted table of edges for the current    */
1590     /* direction.  To do so, we simply scan each segment and try to find */
1591     /* an edge in our table that corresponds to its position.            */
1592     /*                                                                   */
1593     /* If no edge is found, we create and insert a new edge in the       */
1594     /* sorted table.  Otherwise, we simply add the segment to the edge's */
1595     /* list which gets processed in the second step to compute the       */
1596     /* edge's properties.                                                */
1597     /*                                                                   */
1598     /* Note that the table of edges is sorted along the segment/edge     */
1599     /* position.                                                         */
1600     /*                                                                   */
1601     /*********************************************************************/
1602 
1603     /* assure that edge distance threshold is at most 0.25px */
1604     edge_distance_threshold = FT_MulFix( laxis->edge_distance_threshold,
1605                                          scale );
1606     if ( edge_distance_threshold > 64 / 4 )
1607       edge_distance_threshold = 64 / 4;
1608 
1609     edge_distance_threshold = FT_DivFix( edge_distance_threshold,
1610                                          scale );
1611 
1612     for ( seg = segments; seg < segment_limit; seg++ )
1613     {
1614       AF_Edge  found = NULL;
1615       FT_Int   ee;
1616 
1617 
1618       if ( seg->height < segment_length_threshold )
1619         continue;
1620 
1621       /* A special case for serif edges: If they are smaller than */
1622       /* 1.5 pixels we ignore them.                               */
1623       if ( seg->serif                                     &&
1624            2 * seg->height < 3 * segment_length_threshold )
1625         continue;
1626 
1627       /* look for an edge corresponding to the segment */
1628       for ( ee = 0; ee < axis->num_edges; ee++ )
1629       {
1630         AF_Edge  edge = axis->edges + ee;
1631         FT_Pos   dist;
1632 
1633 
1634         dist = seg->pos - edge->fpos;
1635         if ( dist < 0 )
1636           dist = -dist;
1637 
1638         if ( dist < edge_distance_threshold && edge->dir == seg->dir )
1639         {
1640           found = edge;
1641           break;
1642         }
1643       }
1644 
1645       if ( !found )
1646       {
1647         AF_Edge  edge;
1648 
1649 
1650         /* insert a new edge in the list and */
1651         /* sort according to the position    */
1652         error = af_axis_hints_new_edge( axis, seg->pos,
1653                                         (AF_Direction)seg->dir,
1654                                         memory, &edge );
1655         if ( error )
1656           goto Exit;
1657 
1658         /* add the segment to the new edge's list */
1659         FT_ZERO( edge );
1660 
1661         edge->first    = seg;
1662         edge->last     = seg;
1663         edge->dir      = seg->dir;
1664         edge->fpos     = seg->pos;
1665         edge->opos     = FT_MulFix( seg->pos, scale );
1666         edge->pos      = edge->opos;
1667         seg->edge_next = seg;
1668       }
1669       else
1670       {
1671         /* if an edge was found, simply add the segment to the edge's */
1672         /* list                                                       */
1673         seg->edge_next         = found->first;
1674         found->last->edge_next = seg;
1675         found->last            = seg;
1676       }
1677     }
1678 
1679 
1680     /******************************************************************/
1681     /*                                                                */
1682     /* Good, we now compute each edge's properties according to the   */
1683     /* segments found on its position.  Basically, these are          */
1684     /*                                                                */
1685     /*  - the edge's main direction                                   */
1686     /*  - stem edge, serif edge or both (which defaults to stem then) */
1687     /*  - rounded edge, straight or both (which defaults to straight) */
1688     /*  - link for edge                                               */
1689     /*                                                                */
1690     /******************************************************************/
1691 
1692     /* first of all, set the `edge' field in each segment -- this is */
1693     /* required in order to compute edge links                       */
1694 
1695     /*
1696      * Note that removing this loop and setting the `edge' field of each
1697      * segment directly in the code above slows down execution speed for
1698      * some reasons on platforms like the Sun.
1699      */
1700     {
1701       AF_Edge  edges      = axis->edges;
1702       AF_Edge  edge_limit = edges + axis->num_edges;
1703       AF_Edge  edge;
1704 
1705 
1706       for ( edge = edges; edge < edge_limit; edge++ )
1707       {
1708         seg = edge->first;
1709         if ( seg )
1710           do
1711           {
1712             seg->edge = edge;
1713             seg       = seg->edge_next;
1714 
1715           } while ( seg != edge->first );
1716       }
1717 
1718       /* now compute each edge properties */
1719       for ( edge = edges; edge < edge_limit; edge++ )
1720       {
1721         FT_Int  is_round    = 0;  /* does it contain round segments?    */
1722         FT_Int  is_straight = 0;  /* does it contain straight segments? */
1723 #if 0
1724         FT_Pos  ups         = 0;  /* number of upwards segments         */
1725         FT_Pos  downs       = 0;  /* number of downwards segments       */
1726 #endif
1727 
1728 
1729         seg = edge->first;
1730 
1731         do
1732         {
1733           FT_Bool  is_serif;
1734 
1735 
1736           /* check for roundness of segment */
1737           if ( seg->flags & AF_EDGE_ROUND )
1738             is_round++;
1739           else
1740             is_straight++;
1741 
1742 #if 0
1743           /* check for segment direction */
1744           if ( seg->dir == up_dir )
1745             ups   += seg->max_coord - seg->min_coord;
1746           else
1747             downs += seg->max_coord - seg->min_coord;
1748 #endif
1749 
1750           /* check for links -- if seg->serif is set, then seg->link must */
1751           /* be ignored                                                   */
1752           is_serif = (FT_Bool)( seg->serif               &&
1753                                 seg->serif->edge         &&
1754                                 seg->serif->edge != edge );
1755 
1756           if ( ( seg->link && seg->link->edge != NULL ) || is_serif )
1757           {
1758             AF_Edge     edge2;
1759             AF_Segment  seg2;
1760 
1761 
1762             edge2 = edge->link;
1763             seg2  = seg->link;
1764 
1765             if ( is_serif )
1766             {
1767               seg2  = seg->serif;
1768               edge2 = edge->serif;
1769             }
1770 
1771             if ( edge2 )
1772             {
1773               FT_Pos  edge_delta;
1774               FT_Pos  seg_delta;
1775 
1776 
1777               edge_delta = edge->fpos - edge2->fpos;
1778               if ( edge_delta < 0 )
1779                 edge_delta = -edge_delta;
1780 
1781               seg_delta = seg->pos - seg2->pos;
1782               if ( seg_delta < 0 )
1783                 seg_delta = -seg_delta;
1784 
1785               if ( seg_delta < edge_delta )
1786                 edge2 = seg2->edge;
1787             }
1788             else
1789               edge2 = seg2->edge;
1790 
1791             if ( is_serif )
1792             {
1793               edge->serif   = edge2;
1794               edge2->flags |= AF_EDGE_SERIF;
1795             }
1796             else
1797               edge->link  = edge2;
1798           }
1799 
1800           seg = seg->edge_next;
1801 
1802         } while ( seg != edge->first );
1803 
1804         /* set the round/straight flags */
1805         edge->flags = AF_EDGE_NORMAL;
1806 
1807         if ( is_round > 0 && is_round >= is_straight )
1808           edge->flags |= AF_EDGE_ROUND;
1809 
1810 #if 0
1811         /* set the edge's main direction */
1812         edge->dir = AF_DIR_NONE;
1813 
1814         if ( ups > downs )
1815           edge->dir = (FT_Char)up_dir;
1816 
1817         else if ( ups < downs )
1818           edge->dir = (FT_Char)-up_dir;
1819 
1820         else if ( ups == downs )
1821           edge->dir = 0;  /* both up and down! */
1822 #endif
1823 
1824         /* get rid of serifs if link is set                 */
1825         /* XXX: This gets rid of many unpleasant artefacts! */
1826         /*      Example: the `c' in cour.pfa at size 13     */
1827 
1828         if ( edge->serif && edge->link )
1829           edge->serif = NULL;
1830       }
1831     }
1832 
1833   Exit:
1834     return error;
1835   }
1836 
1837 
1838   /* Detect segments and edges for given dimension. */
1839 
1840   FT_LOCAL_DEF( FT_Error )
af_latin_hints_detect_features(AF_GlyphHints hints,FT_UInt width_count,AF_WidthRec * widths,AF_Dimension dim)1841   af_latin_hints_detect_features( AF_GlyphHints  hints,
1842                                   FT_UInt        width_count,
1843                                   AF_WidthRec*   widths,
1844                                   AF_Dimension   dim )
1845   {
1846     FT_Error  error;
1847 
1848 
1849     error = af_latin_hints_compute_segments( hints, dim );
1850     if ( !error )
1851     {
1852       af_latin_hints_link_segments( hints, width_count, widths, dim );
1853 
1854       error = af_latin_hints_compute_edges( hints, dim );
1855     }
1856 
1857     return error;
1858   }
1859 
1860 
1861   /* Compute all edges which lie within blue zones. */
1862 
1863   static void
af_latin_hints_compute_blue_edges(AF_GlyphHints hints,AF_LatinMetrics metrics)1864   af_latin_hints_compute_blue_edges( AF_GlyphHints    hints,
1865                                      AF_LatinMetrics  metrics )
1866   {
1867     AF_AxisHints  axis       = &hints->axis[AF_DIMENSION_VERT];
1868     AF_Edge       edge       = axis->edges;
1869     AF_Edge       edge_limit = edge + axis->num_edges;
1870     AF_LatinAxis  latin      = &metrics->axis[AF_DIMENSION_VERT];
1871     FT_Fixed      scale      = latin->scale;
1872 
1873 
1874     /* compute which blue zones are active, i.e. have their scaled */
1875     /* size < 3/4 pixels                                           */
1876 
1877     /* for each horizontal edge search the blue zone which is closest */
1878     for ( ; edge < edge_limit; edge++ )
1879     {
1880       FT_UInt   bb;
1881       AF_Width  best_blue            = NULL;
1882       FT_Bool   best_blue_is_neutral = 0;
1883       FT_Pos    best_dist;                 /* initial threshold */
1884 
1885 
1886       /* compute the initial threshold as a fraction of the EM size */
1887       /* (the value 40 is heuristic)                                */
1888       best_dist = FT_MulFix( metrics->units_per_em / 40, scale );
1889 
1890       /* assure a minimum distance of 0.5px */
1891       if ( best_dist > 64 / 2 )
1892         best_dist = 64 / 2;
1893 
1894       for ( bb = 0; bb < latin->blue_count; bb++ )
1895       {
1896         AF_LatinBlue  blue = latin->blues + bb;
1897         FT_Bool       is_top_blue, is_neutral_blue, is_major_dir;
1898 
1899 
1900         /* skip inactive blue zones (i.e., those that are too large) */
1901         if ( !( blue->flags & AF_LATIN_BLUE_ACTIVE ) )
1902           continue;
1903 
1904         /* if it is a top zone, check for right edges (against the major */
1905         /* direction); if it is a bottom zone, check for left edges (in  */
1906         /* the major direction) -- this assumes the TrueType convention  */
1907         /* for the orientation of contours                               */
1908         is_top_blue =
1909           (FT_Byte)( ( blue->flags & AF_LATIN_BLUE_TOP ) != 0 );
1910         is_neutral_blue =
1911           (FT_Byte)( ( blue->flags & AF_LATIN_BLUE_NEUTRAL ) != 0);
1912         is_major_dir =
1913           FT_BOOL( edge->dir == axis->major_dir );
1914 
1915         /* neutral blue zones are handled for both directions */
1916         if ( is_top_blue ^ is_major_dir || is_neutral_blue )
1917         {
1918           FT_Pos  dist;
1919 
1920 
1921           /* first of all, compare it to the reference position */
1922           dist = edge->fpos - blue->ref.org;
1923           if ( dist < 0 )
1924             dist = -dist;
1925 
1926           dist = FT_MulFix( dist, scale );
1927           if ( dist < best_dist )
1928           {
1929             best_dist            = dist;
1930             best_blue            = &blue->ref;
1931             best_blue_is_neutral = is_neutral_blue;
1932           }
1933 
1934           /* now compare it to the overshoot position and check whether */
1935           /* the edge is rounded, and whether the edge is over the      */
1936           /* reference position of a top zone, or under the reference   */
1937           /* position of a bottom zone (provided we don't have a        */
1938           /* neutral blue zone)                                         */
1939           if ( edge->flags & AF_EDGE_ROUND &&
1940                dist != 0                   &&
1941                !is_neutral_blue            )
1942           {
1943             FT_Bool  is_under_ref = FT_BOOL( edge->fpos < blue->ref.org );
1944 
1945 
1946             if ( is_top_blue ^ is_under_ref )
1947             {
1948               dist = edge->fpos - blue->shoot.org;
1949               if ( dist < 0 )
1950                 dist = -dist;
1951 
1952               dist = FT_MulFix( dist, scale );
1953               if ( dist < best_dist )
1954               {
1955                 best_dist            = dist;
1956                 best_blue            = &blue->shoot;
1957                 best_blue_is_neutral = is_neutral_blue;
1958               }
1959             }
1960           }
1961         }
1962       }
1963 
1964       if ( best_blue )
1965       {
1966         edge->blue_edge = best_blue;
1967         if ( best_blue_is_neutral )
1968           edge->flags |= AF_EDGE_NEUTRAL;
1969       }
1970     }
1971   }
1972 
1973 
1974   /* Initalize hinting engine. */
1975 
1976   static FT_Error
af_latin_hints_init(AF_GlyphHints hints,AF_LatinMetrics metrics)1977   af_latin_hints_init( AF_GlyphHints    hints,
1978                        AF_LatinMetrics  metrics )
1979   {
1980     FT_Render_Mode  mode;
1981     FT_UInt32       scaler_flags, other_flags;
1982     FT_Face         face = metrics->root.scaler.face;
1983 
1984 
1985     af_glyph_hints_rescale( hints, (AF_StyleMetrics)metrics );
1986 
1987     /*
1988      *  correct x_scale and y_scale if needed, since they may have
1989      *  been modified by `af_latin_metrics_scale_dim' above
1990      */
1991     hints->x_scale = metrics->axis[AF_DIMENSION_HORZ].scale;
1992     hints->x_delta = metrics->axis[AF_DIMENSION_HORZ].delta;
1993     hints->y_scale = metrics->axis[AF_DIMENSION_VERT].scale;
1994     hints->y_delta = metrics->axis[AF_DIMENSION_VERT].delta;
1995 
1996     /* compute flags depending on render mode, etc. */
1997     mode = metrics->root.scaler.render_mode;
1998 
1999 #if 0 /* #ifdef AF_CONFIG_OPTION_USE_WARPER */
2000     if ( mode == FT_RENDER_MODE_LCD || mode == FT_RENDER_MODE_LCD_V )
2001       metrics->root.scaler.render_mode = mode = FT_RENDER_MODE_NORMAL;
2002 #endif
2003 
2004     scaler_flags = hints->scaler_flags;
2005     other_flags  = 0;
2006 
2007     /*
2008      *  We snap the width of vertical stems for the monochrome and
2009      *  horizontal LCD rendering targets only.
2010      */
2011     if ( mode == FT_RENDER_MODE_MONO || mode == FT_RENDER_MODE_LCD )
2012       other_flags |= AF_LATIN_HINTS_HORZ_SNAP;
2013 
2014     /*
2015      *  We snap the width of horizontal stems for the monochrome and
2016      *  vertical LCD rendering targets only.
2017      */
2018     if ( mode == FT_RENDER_MODE_MONO || mode == FT_RENDER_MODE_LCD_V )
2019       other_flags |= AF_LATIN_HINTS_VERT_SNAP;
2020 
2021     /*
2022      *  We adjust stems to full pixels only if we don't use the `light' mode.
2023      */
2024     if ( mode != FT_RENDER_MODE_LIGHT )
2025       other_flags |= AF_LATIN_HINTS_STEM_ADJUST;
2026 
2027     if ( mode == FT_RENDER_MODE_MONO )
2028       other_flags |= AF_LATIN_HINTS_MONO;
2029 
2030     /*
2031      *  In `light' hinting mode we disable horizontal hinting completely.
2032      *  We also do it if the face is italic.
2033      *
2034      *  However, if warping is enabled (which only works in `light' hinting
2035      *  mode), advance widths get adjusted, too.
2036      */
2037     if ( mode == FT_RENDER_MODE_LIGHT                      ||
2038          ( face->style_flags & FT_STYLE_FLAG_ITALIC ) != 0 )
2039       scaler_flags |= AF_SCALER_FLAG_NO_HORIZONTAL;
2040 
2041 #ifdef AF_CONFIG_OPTION_USE_WARPER
2042     /* get (global) warper flag */
2043     if ( !metrics->root.globals->module->warping )
2044       scaler_flags |= AF_SCALER_FLAG_NO_WARPER;
2045 #endif
2046 
2047     hints->scaler_flags = scaler_flags;
2048     hints->other_flags  = other_flags;
2049 
2050     return FT_Err_Ok;
2051   }
2052 
2053 
2054   /*************************************************************************/
2055   /*************************************************************************/
2056   /*****                                                               *****/
2057   /*****        L A T I N   G L Y P H   G R I D - F I T T I N G        *****/
2058   /*****                                                               *****/
2059   /*************************************************************************/
2060   /*************************************************************************/
2061 
2062   /* Snap a given width in scaled coordinates to one of the */
2063   /* current standard widths.                               */
2064 
2065   static FT_Pos
af_latin_snap_width(AF_Width widths,FT_UInt count,FT_Pos width)2066   af_latin_snap_width( AF_Width  widths,
2067                        FT_UInt   count,
2068                        FT_Pos    width )
2069   {
2070     FT_UInt  n;
2071     FT_Pos   best      = 64 + 32 + 2;
2072     FT_Pos   reference = width;
2073     FT_Pos   scaled;
2074 
2075 
2076     for ( n = 0; n < count; n++ )
2077     {
2078       FT_Pos  w;
2079       FT_Pos  dist;
2080 
2081 
2082       w = widths[n].cur;
2083       dist = width - w;
2084       if ( dist < 0 )
2085         dist = -dist;
2086       if ( dist < best )
2087       {
2088         best      = dist;
2089         reference = w;
2090       }
2091     }
2092 
2093     scaled = FT_PIX_ROUND( reference );
2094 
2095     if ( width >= reference )
2096     {
2097       if ( width < scaled + 48 )
2098         width = reference;
2099     }
2100     else
2101     {
2102       if ( width > scaled - 48 )
2103         width = reference;
2104     }
2105 
2106     return width;
2107   }
2108 
2109 
2110   /* Compute the snapped width of a given stem, ignoring very thin ones. */
2111   /* There is a lot of voodoo in this function; changing the hard-coded  */
2112   /* parameters influence the whole hinting process.                     */
2113 
2114   static FT_Pos
af_latin_compute_stem_width(AF_GlyphHints hints,AF_Dimension dim,FT_Pos width,FT_UInt base_flags,FT_UInt stem_flags)2115   af_latin_compute_stem_width( AF_GlyphHints  hints,
2116                                AF_Dimension   dim,
2117                                FT_Pos         width,
2118                                FT_UInt        base_flags,
2119                                FT_UInt        stem_flags )
2120   {
2121     AF_LatinMetrics  metrics  = (AF_LatinMetrics)hints->metrics;
2122     AF_LatinAxis     axis     = &metrics->axis[dim];
2123     FT_Pos           dist     = width;
2124     FT_Int           sign     = 0;
2125     FT_Int           vertical = ( dim == AF_DIMENSION_VERT );
2126 
2127 
2128     if ( !AF_LATIN_HINTS_DO_STEM_ADJUST( hints ) ||
2129          axis->extra_light                       )
2130       return width;
2131 
2132     if ( dist < 0 )
2133     {
2134       dist = -width;
2135       sign = 1;
2136     }
2137 
2138     if ( (  vertical && !AF_LATIN_HINTS_DO_VERT_SNAP( hints ) ) ||
2139          ( !vertical && !AF_LATIN_HINTS_DO_HORZ_SNAP( hints ) ) )
2140     {
2141       /* smooth hinting process: very lightly quantize the stem width */
2142 
2143       /* leave the widths of serifs alone */
2144       if ( ( stem_flags & AF_EDGE_SERIF ) &&
2145            vertical                       &&
2146            ( dist < 3 * 64 )              )
2147         goto Done_Width;
2148 
2149       else if ( base_flags & AF_EDGE_ROUND )
2150       {
2151         if ( dist < 80 )
2152           dist = 64;
2153       }
2154       else if ( dist < 56 )
2155         dist = 56;
2156 
2157       if ( axis->width_count > 0 )
2158       {
2159         FT_Pos  delta;
2160 
2161 
2162         /* compare to standard width */
2163         delta = dist - axis->widths[0].cur;
2164 
2165         if ( delta < 0 )
2166           delta = -delta;
2167 
2168         if ( delta < 40 )
2169         {
2170           dist = axis->widths[0].cur;
2171           if ( dist < 48 )
2172             dist = 48;
2173 
2174           goto Done_Width;
2175         }
2176 
2177         if ( dist < 3 * 64 )
2178         {
2179           delta  = dist & 63;
2180           dist  &= -64;
2181 
2182           if ( delta < 10 )
2183             dist += delta;
2184 
2185           else if ( delta < 32 )
2186             dist += 10;
2187 
2188           else if ( delta < 54 )
2189             dist += 54;
2190 
2191           else
2192             dist += delta;
2193         }
2194         else
2195           dist = ( dist + 32 ) & ~63;
2196       }
2197     }
2198     else
2199     {
2200       /* strong hinting process: snap the stem width to integer pixels */
2201 
2202       FT_Pos  org_dist = dist;
2203 
2204 
2205       dist = af_latin_snap_width( axis->widths, axis->width_count, dist );
2206 
2207       if ( vertical )
2208       {
2209         /* in the case of vertical hinting, always round */
2210         /* the stem heights to integer pixels            */
2211 
2212         if ( dist >= 64 )
2213           dist = ( dist + 16 ) & ~63;
2214         else
2215           dist = 64;
2216       }
2217       else
2218       {
2219         if ( AF_LATIN_HINTS_DO_MONO( hints ) )
2220         {
2221           /* monochrome horizontal hinting: snap widths to integer pixels */
2222           /* with a different threshold                                   */
2223 
2224           if ( dist < 64 )
2225             dist = 64;
2226           else
2227             dist = ( dist + 32 ) & ~63;
2228         }
2229         else
2230         {
2231           /* for horizontal anti-aliased hinting, we adopt a more subtle */
2232           /* approach: we strengthen small stems, round stems whose size */
2233           /* is between 1 and 2 pixels to an integer, otherwise nothing  */
2234 
2235           if ( dist < 48 )
2236             dist = ( dist + 64 ) >> 1;
2237 
2238           else if ( dist < 128 )
2239           {
2240             /* We only round to an integer width if the corresponding */
2241             /* distortion is less than 1/4 pixel.  Otherwise this     */
2242             /* makes everything worse since the diagonals, which are  */
2243             /* not hinted, appear a lot bolder or thinner than the    */
2244             /* vertical stems.                                        */
2245 
2246             FT_Pos  delta;
2247 
2248 
2249             dist = ( dist + 22 ) & ~63;
2250             delta = dist - org_dist;
2251             if ( delta < 0 )
2252               delta = -delta;
2253 
2254             if ( delta >= 16 )
2255             {
2256               dist = org_dist;
2257               if ( dist < 48 )
2258                 dist = ( dist + 64 ) >> 1;
2259             }
2260           }
2261           else
2262             /* round otherwise to prevent color fringes in LCD mode */
2263             dist = ( dist + 32 ) & ~63;
2264         }
2265       }
2266     }
2267 
2268   Done_Width:
2269     if ( sign )
2270       dist = -dist;
2271 
2272     return dist;
2273   }
2274 
2275 
2276   /* Align one stem edge relative to the previous stem edge. */
2277 
2278   static void
af_latin_align_linked_edge(AF_GlyphHints hints,AF_Dimension dim,AF_Edge base_edge,AF_Edge stem_edge)2279   af_latin_align_linked_edge( AF_GlyphHints  hints,
2280                               AF_Dimension   dim,
2281                               AF_Edge        base_edge,
2282                               AF_Edge        stem_edge )
2283   {
2284     FT_Pos  dist = stem_edge->opos - base_edge->opos;
2285 
2286     FT_Pos  fitted_width = af_latin_compute_stem_width( hints, dim, dist,
2287                                                         base_edge->flags,
2288                                                         stem_edge->flags );
2289 
2290 
2291     stem_edge->pos = base_edge->pos + fitted_width;
2292 
2293     FT_TRACE5(( "  LINK: edge %d (opos=%.2f) linked to %.2f,"
2294                 " dist was %.2f, now %.2f\n",
2295                 stem_edge - hints->axis[dim].edges, stem_edge->opos / 64.0,
2296                 stem_edge->pos / 64.0, dist / 64.0, fitted_width / 64.0 ));
2297   }
2298 
2299 
2300   /* Shift the coordinates of the `serif' edge by the same amount */
2301   /* as the corresponding `base' edge has been moved already.     */
2302 
2303   static void
af_latin_align_serif_edge(AF_GlyphHints hints,AF_Edge base,AF_Edge serif)2304   af_latin_align_serif_edge( AF_GlyphHints  hints,
2305                              AF_Edge        base,
2306                              AF_Edge        serif )
2307   {
2308     FT_UNUSED( hints );
2309 
2310     serif->pos = base->pos + ( serif->opos - base->opos );
2311   }
2312 
2313 
2314   /*************************************************************************/
2315   /*************************************************************************/
2316   /*************************************************************************/
2317   /****                                                                 ****/
2318   /****                    E D G E   H I N T I N G                      ****/
2319   /****                                                                 ****/
2320   /*************************************************************************/
2321   /*************************************************************************/
2322   /*************************************************************************/
2323 
2324 
2325   /* The main grid-fitting routine. */
2326 
2327   static void
af_latin_hint_edges(AF_GlyphHints hints,AF_Dimension dim)2328   af_latin_hint_edges( AF_GlyphHints  hints,
2329                        AF_Dimension   dim )
2330   {
2331     AF_AxisHints  axis       = &hints->axis[dim];
2332     AF_Edge       edges      = axis->edges;
2333     AF_Edge       edge_limit = edges + axis->num_edges;
2334     FT_PtrDist    n_edges;
2335     AF_Edge       edge;
2336     AF_Edge       anchor     = NULL;
2337     FT_Int        has_serifs = 0;
2338 
2339 #ifdef FT_DEBUG_LEVEL_TRACE
2340     FT_UInt       num_actions = 0;
2341 #endif
2342 
2343 
2344     FT_TRACE5(( "latin %s edge hinting (style `%s')\n",
2345                 dim == AF_DIMENSION_VERT ? "horizontal" : "vertical",
2346                 af_style_names[hints->metrics->style_class->style] ));
2347 
2348     /* we begin by aligning all stems relative to the blue zone */
2349     /* if needed -- that's only for horizontal edges            */
2350 
2351     if ( dim == AF_DIMENSION_VERT && AF_HINTS_DO_BLUES( hints ) )
2352     {
2353       for ( edge = edges; edge < edge_limit; edge++ )
2354       {
2355         AF_Width  blue;
2356         AF_Edge   edge1, edge2; /* these edges form the stem to check */
2357 
2358 
2359         if ( edge->flags & AF_EDGE_DONE )
2360           continue;
2361 
2362         edge1 = NULL;
2363         edge2 = edge->link;
2364 
2365         /*
2366          *  If a stem contains both a neutral and a non-neutral blue zone,
2367          *  skip the neutral one.  Otherwise, outlines with different
2368          *  directions might be incorrectly aligned at the same vertical
2369          *  position.
2370          *
2371          *  If we have two neutral blue zones, skip one of them.
2372          *
2373          */
2374         if ( edge->blue_edge && edge2 && edge2->blue_edge )
2375         {
2376           FT_Byte  neutral  = edge->flags  & AF_EDGE_NEUTRAL;
2377           FT_Byte  neutral2 = edge2->flags & AF_EDGE_NEUTRAL;
2378 
2379 
2380           if ( neutral2 )
2381           {
2382             edge2->blue_edge = NULL;
2383             edge2->flags    &= ~AF_EDGE_NEUTRAL;
2384           }
2385           else if ( neutral )
2386           {
2387             edge->blue_edge = NULL;
2388             edge->flags    &= ~AF_EDGE_NEUTRAL;
2389           }
2390         }
2391 
2392         blue = edge->blue_edge;
2393         if ( blue )
2394           edge1 = edge;
2395 
2396         /* flip edges if the other edge is aligned to a blue zone */
2397         else if ( edge2 && edge2->blue_edge )
2398         {
2399           blue  = edge2->blue_edge;
2400           edge1 = edge2;
2401           edge2 = edge;
2402         }
2403 
2404         if ( !edge1 )
2405           continue;
2406 
2407 #ifdef FT_DEBUG_LEVEL_TRACE
2408         if ( !anchor )
2409           FT_TRACE5(( "  BLUE_ANCHOR: edge %d (opos=%.2f) snapped to %.2f,"
2410                       " was %.2f (anchor=edge %d)\n",
2411                       edge1 - edges, edge1->opos / 64.0, blue->fit / 64.0,
2412                       edge1->pos / 64.0, edge - edges ));
2413         else
2414           FT_TRACE5(( "  BLUE: edge %d (opos=%.2f) snapped to %.2f,"
2415                       " was %.2f\n",
2416                       edge1 - edges, edge1->opos / 64.0, blue->fit / 64.0,
2417                       edge1->pos / 64.0 ));
2418 
2419         num_actions++;
2420 #endif
2421 
2422         edge1->pos    = blue->fit;
2423         edge1->flags |= AF_EDGE_DONE;
2424 
2425         if ( edge2 && !edge2->blue_edge )
2426         {
2427           af_latin_align_linked_edge( hints, dim, edge1, edge2 );
2428           edge2->flags |= AF_EDGE_DONE;
2429 
2430 #ifdef FT_DEBUG_LEVEL_TRACE
2431           num_actions++;
2432 #endif
2433         }
2434 
2435         if ( !anchor )
2436           anchor = edge;
2437       }
2438     }
2439 
2440     /* now we align all other stem edges, trying to maintain the */
2441     /* relative order of stems in the glyph                      */
2442     for ( edge = edges; edge < edge_limit; edge++ )
2443     {
2444       AF_Edge  edge2;
2445 
2446 
2447       if ( edge->flags & AF_EDGE_DONE )
2448         continue;
2449 
2450       /* skip all non-stem edges */
2451       edge2 = edge->link;
2452       if ( !edge2 )
2453       {
2454         has_serifs++;
2455         continue;
2456       }
2457 
2458       /* now align the stem */
2459 
2460       /* this should not happen, but it's better to be safe */
2461       if ( edge2->blue_edge )
2462       {
2463         FT_TRACE5(( "  ASSERTION FAILED for edge %d\n", edge2 - edges ));
2464 
2465         af_latin_align_linked_edge( hints, dim, edge2, edge );
2466         edge->flags |= AF_EDGE_DONE;
2467 
2468 #ifdef FT_DEBUG_LEVEL_TRACE
2469         num_actions++;
2470 #endif
2471         continue;
2472       }
2473 
2474       if ( !anchor )
2475       {
2476         /* if we reach this if clause, no stem has been aligned yet */
2477 
2478         FT_Pos  org_len, org_center, cur_len;
2479         FT_Pos  cur_pos1, error1, error2, u_off, d_off;
2480 
2481 
2482         org_len = edge2->opos - edge->opos;
2483         cur_len = af_latin_compute_stem_width( hints, dim, org_len,
2484                                                edge->flags,
2485                                                edge2->flags );
2486 
2487         /* some voodoo to specially round edges for small stem widths; */
2488         /* the idea is to align the center of a stem, then shifting    */
2489         /* the stem edges to suitable positions                        */
2490         if ( cur_len <= 64 )
2491         {
2492           /* width <= 1px */
2493           u_off = 32;
2494           d_off = 32;
2495         }
2496         else
2497         {
2498           /* 1px < width < 1.5px */
2499           u_off = 38;
2500           d_off = 26;
2501         }
2502 
2503         if ( cur_len < 96 )
2504         {
2505           org_center = edge->opos + ( org_len >> 1 );
2506           cur_pos1   = FT_PIX_ROUND( org_center );
2507 
2508           error1 = org_center - ( cur_pos1 - u_off );
2509           if ( error1 < 0 )
2510             error1 = -error1;
2511 
2512           error2 = org_center - ( cur_pos1 + d_off );
2513           if ( error2 < 0 )
2514             error2 = -error2;
2515 
2516           if ( error1 < error2 )
2517             cur_pos1 -= u_off;
2518           else
2519             cur_pos1 += d_off;
2520 
2521           edge->pos  = cur_pos1 - cur_len / 2;
2522           edge2->pos = edge->pos + cur_len;
2523         }
2524         else
2525           edge->pos = FT_PIX_ROUND( edge->opos );
2526 
2527         anchor       = edge;
2528         edge->flags |= AF_EDGE_DONE;
2529 
2530         FT_TRACE5(( "  ANCHOR: edge %d (opos=%.2f) and %d (opos=%.2f)"
2531                     " snapped to %.2f and %.2f\n",
2532                     edge - edges, edge->opos / 64.0,
2533                     edge2 - edges, edge2->opos / 64.0,
2534                     edge->pos / 64.0, edge2->pos / 64.0 ));
2535 
2536         af_latin_align_linked_edge( hints, dim, edge, edge2 );
2537 
2538 #ifdef FT_DEBUG_LEVEL_TRACE
2539         num_actions += 2;
2540 #endif
2541       }
2542       else
2543       {
2544         FT_Pos  org_pos, org_len, org_center, cur_len;
2545         FT_Pos  cur_pos1, cur_pos2, delta1, delta2;
2546 
2547 
2548         org_pos    = anchor->pos + ( edge->opos - anchor->opos );
2549         org_len    = edge2->opos - edge->opos;
2550         org_center = org_pos + ( org_len >> 1 );
2551 
2552         cur_len = af_latin_compute_stem_width( hints, dim, org_len,
2553                                                edge->flags,
2554                                                edge2->flags );
2555 
2556         if ( edge2->flags & AF_EDGE_DONE )
2557         {
2558           FT_TRACE5(( "  ADJUST: edge %d (pos=%.2f) moved to %.2f\n",
2559                       edge - edges, edge->pos / 64.0,
2560                       ( edge2->pos - cur_len ) / 64.0 ));
2561 
2562           edge->pos = edge2->pos - cur_len;
2563         }
2564 
2565         else if ( cur_len < 96 )
2566         {
2567           FT_Pos  u_off, d_off;
2568 
2569 
2570           cur_pos1 = FT_PIX_ROUND( org_center );
2571 
2572           if ( cur_len <= 64 )
2573           {
2574             u_off = 32;
2575             d_off = 32;
2576           }
2577           else
2578           {
2579             u_off = 38;
2580             d_off = 26;
2581           }
2582 
2583           delta1 = org_center - ( cur_pos1 - u_off );
2584           if ( delta1 < 0 )
2585             delta1 = -delta1;
2586 
2587           delta2 = org_center - ( cur_pos1 + d_off );
2588           if ( delta2 < 0 )
2589             delta2 = -delta2;
2590 
2591           if ( delta1 < delta2 )
2592             cur_pos1 -= u_off;
2593           else
2594             cur_pos1 += d_off;
2595 
2596           edge->pos  = cur_pos1 - cur_len / 2;
2597           edge2->pos = cur_pos1 + cur_len / 2;
2598 
2599           FT_TRACE5(( "  STEM: edge %d (opos=%.2f) linked to %d (opos=%.2f)"
2600                       " snapped to %.2f and %.2f\n",
2601                       edge - edges, edge->opos / 64.0,
2602                       edge2 - edges, edge2->opos / 64.0,
2603                       edge->pos / 64.0, edge2->pos / 64.0 ));
2604         }
2605 
2606         else
2607         {
2608           org_pos    = anchor->pos + ( edge->opos - anchor->opos );
2609           org_len    = edge2->opos - edge->opos;
2610           org_center = org_pos + ( org_len >> 1 );
2611 
2612           cur_len    = af_latin_compute_stem_width( hints, dim, org_len,
2613                                                     edge->flags,
2614                                                     edge2->flags );
2615 
2616           cur_pos1 = FT_PIX_ROUND( org_pos );
2617           delta1   = cur_pos1 + ( cur_len >> 1 ) - org_center;
2618           if ( delta1 < 0 )
2619             delta1 = -delta1;
2620 
2621           cur_pos2 = FT_PIX_ROUND( org_pos + org_len ) - cur_len;
2622           delta2   = cur_pos2 + ( cur_len >> 1 ) - org_center;
2623           if ( delta2 < 0 )
2624             delta2 = -delta2;
2625 
2626           edge->pos  = ( delta1 < delta2 ) ? cur_pos1 : cur_pos2;
2627           edge2->pos = edge->pos + cur_len;
2628 
2629           FT_TRACE5(( "  STEM: edge %d (opos=%.2f) linked to %d (opos=%.2f)"
2630                       " snapped to %.2f and %.2f\n",
2631                       edge - edges, edge->opos / 64.0,
2632                       edge2 - edges, edge2->opos / 64.0,
2633                       edge->pos / 64.0, edge2->pos / 64.0 ));
2634         }
2635 
2636 #ifdef FT_DEBUG_LEVEL_TRACE
2637         num_actions++;
2638 #endif
2639 
2640         edge->flags  |= AF_EDGE_DONE;
2641         edge2->flags |= AF_EDGE_DONE;
2642 
2643         if ( edge > edges && edge->pos < edge[-1].pos )
2644         {
2645 #ifdef FT_DEBUG_LEVEL_TRACE
2646           FT_TRACE5(( "  BOUND: edge %d (pos=%.2f) moved to %.2f\n",
2647                       edge - edges, edge->pos / 64.0, edge[-1].pos / 64.0 ));
2648 
2649           num_actions++;
2650 #endif
2651 
2652           edge->pos = edge[-1].pos;
2653         }
2654       }
2655     }
2656 
2657     /* make sure that lowercase m's maintain their symmetry */
2658 
2659     /* In general, lowercase m's have six vertical edges if they are sans */
2660     /* serif, or twelve if they are with serifs.  This implementation is  */
2661     /* based on that assumption, and seems to work very well with most    */
2662     /* faces.  However, if for a certain face this assumption is not      */
2663     /* true, the m is just rendered like before.  In addition, any stem   */
2664     /* correction will only be applied to symmetrical glyphs (even if the */
2665     /* glyph is not an m), so the potential for unwanted distortion is    */
2666     /* relatively low.                                                    */
2667 
2668     /* We don't handle horizontal edges since we can't easily assure that */
2669     /* the third (lowest) stem aligns with the base line; it might end up */
2670     /* one pixel higher or lower.                                         */
2671 
2672     n_edges = edge_limit - edges;
2673     if ( dim == AF_DIMENSION_HORZ && ( n_edges == 6 || n_edges == 12 ) )
2674     {
2675       AF_Edge  edge1, edge2, edge3;
2676       FT_Pos   dist1, dist2, span, delta;
2677 
2678 
2679       if ( n_edges == 6 )
2680       {
2681         edge1 = edges;
2682         edge2 = edges + 2;
2683         edge3 = edges + 4;
2684       }
2685       else
2686       {
2687         edge1 = edges + 1;
2688         edge2 = edges + 5;
2689         edge3 = edges + 9;
2690       }
2691 
2692       dist1 = edge2->opos - edge1->opos;
2693       dist2 = edge3->opos - edge2->opos;
2694 
2695       span = dist1 - dist2;
2696       if ( span < 0 )
2697         span = -span;
2698 
2699       if ( span < 8 )
2700       {
2701         delta = edge3->pos - ( 2 * edge2->pos - edge1->pos );
2702         edge3->pos -= delta;
2703         if ( edge3->link )
2704           edge3->link->pos -= delta;
2705 
2706         /* move the serifs along with the stem */
2707         if ( n_edges == 12 )
2708         {
2709           ( edges + 8 )->pos -= delta;
2710           ( edges + 11 )->pos -= delta;
2711         }
2712 
2713         edge3->flags |= AF_EDGE_DONE;
2714         if ( edge3->link )
2715           edge3->link->flags |= AF_EDGE_DONE;
2716       }
2717     }
2718 
2719     if ( has_serifs || !anchor )
2720     {
2721       /*
2722        *  now hint the remaining edges (serifs and single) in order
2723        *  to complete our processing
2724        */
2725       for ( edge = edges; edge < edge_limit; edge++ )
2726       {
2727         FT_Pos  delta;
2728 
2729 
2730         if ( edge->flags & AF_EDGE_DONE )
2731           continue;
2732 
2733         delta = 1000;
2734 
2735         if ( edge->serif )
2736         {
2737           delta = edge->serif->opos - edge->opos;
2738           if ( delta < 0 )
2739             delta = -delta;
2740         }
2741 
2742         if ( delta < 64 + 16 )
2743         {
2744           af_latin_align_serif_edge( hints, edge->serif, edge );
2745           FT_TRACE5(( "  SERIF: edge %d (opos=%.2f) serif to %d (opos=%.2f)"
2746                       " aligned to %.2f\n",
2747                       edge - edges, edge->opos / 64.0,
2748                       edge->serif - edges, edge->serif->opos / 64.0,
2749                       edge->pos / 64.0 ));
2750         }
2751         else if ( !anchor )
2752         {
2753           edge->pos = FT_PIX_ROUND( edge->opos );
2754           anchor    = edge;
2755           FT_TRACE5(( "  SERIF_ANCHOR: edge %d (opos=%.2f)"
2756                       " snapped to %.2f\n",
2757                       edge-edges, edge->opos / 64.0, edge->pos / 64.0 ));
2758         }
2759         else
2760         {
2761           AF_Edge  before, after;
2762 
2763 
2764           for ( before = edge - 1; before >= edges; before-- )
2765             if ( before->flags & AF_EDGE_DONE )
2766               break;
2767 
2768           for ( after = edge + 1; after < edge_limit; after++ )
2769             if ( after->flags & AF_EDGE_DONE )
2770               break;
2771 
2772           if ( before >= edges && before < edge   &&
2773                after < edge_limit && after > edge )
2774           {
2775             if ( after->opos == before->opos )
2776               edge->pos = before->pos;
2777             else
2778               edge->pos = before->pos +
2779                           FT_MulDiv( edge->opos - before->opos,
2780                                      after->pos - before->pos,
2781                                      after->opos - before->opos );
2782 
2783             FT_TRACE5(( "  SERIF_LINK1: edge %d (opos=%.2f) snapped to %.2f"
2784                         " from %d (opos=%.2f)\n",
2785                         edge - edges, edge->opos / 64.0,
2786                         edge->pos / 64.0,
2787                         before - edges, before->opos / 64.0 ));
2788           }
2789           else
2790           {
2791             edge->pos = anchor->pos +
2792                         ( ( edge->opos - anchor->opos + 16 ) & ~31 );
2793             FT_TRACE5(( "  SERIF_LINK2: edge %d (opos=%.2f)"
2794                         " snapped to %.2f\n",
2795                         edge - edges, edge->opos / 64.0, edge->pos / 64.0 ));
2796           }
2797         }
2798 
2799 #ifdef FT_DEBUG_LEVEL_TRACE
2800         num_actions++;
2801 #endif
2802         edge->flags |= AF_EDGE_DONE;
2803 
2804         if ( edge > edges && edge->pos < edge[-1].pos )
2805         {
2806 #ifdef FT_DEBUG_LEVEL_TRACE
2807           FT_TRACE5(( "  BOUND: edge %d (pos=%.2f) moved to %.2f\n",
2808                       edge - edges, edge->pos / 64.0, edge[-1].pos / 64.0 ));
2809 
2810           num_actions++;
2811 #endif
2812           edge->pos = edge[-1].pos;
2813         }
2814 
2815         if ( edge + 1 < edge_limit        &&
2816              edge[1].flags & AF_EDGE_DONE &&
2817              edge->pos > edge[1].pos      )
2818         {
2819 #ifdef FT_DEBUG_LEVEL_TRACE
2820           FT_TRACE5(( "  BOUND: edge %d (pos=%.2f) moved to %.2f\n",
2821                       edge - edges, edge->pos / 64.0, edge[1].pos / 64.0 ));
2822 
2823           num_actions++;
2824 #endif
2825 
2826           edge->pos = edge[1].pos;
2827         }
2828       }
2829     }
2830 
2831 #ifdef FT_DEBUG_LEVEL_TRACE
2832     if ( !num_actions )
2833       FT_TRACE5(( "  (none)\n" ));
2834     FT_TRACE5(( "\n" ));
2835 #endif
2836   }
2837 
2838 
2839   /* Apply the complete hinting algorithm to a latin glyph. */
2840 
2841   static FT_Error
af_latin_hints_apply(FT_UInt glyph_index,AF_GlyphHints hints,FT_Outline * outline,AF_LatinMetrics metrics)2842   af_latin_hints_apply( FT_UInt          glyph_index,
2843                         AF_GlyphHints    hints,
2844                         FT_Outline*      outline,
2845                         AF_LatinMetrics  metrics )
2846   {
2847     FT_Error  error;
2848     int       dim;
2849 
2850     AF_LatinAxis  axis;
2851 
2852 
2853     error = af_glyph_hints_reload( hints, outline );
2854     if ( error )
2855       goto Exit;
2856 
2857     /* analyze glyph outline */
2858 #ifdef AF_CONFIG_OPTION_USE_WARPER
2859     if ( ( metrics->root.scaler.render_mode == FT_RENDER_MODE_LIGHT &&
2860            AF_HINTS_DO_WARP( hints )                                ) ||
2861          AF_HINTS_DO_HORIZONTAL( hints )                              )
2862 #else
2863     if ( AF_HINTS_DO_HORIZONTAL( hints ) )
2864 #endif
2865     {
2866       axis  = &metrics->axis[AF_DIMENSION_HORZ];
2867       error = af_latin_hints_detect_features( hints,
2868                                               axis->width_count,
2869                                               axis->widths,
2870                                               AF_DIMENSION_HORZ );
2871       if ( error )
2872         goto Exit;
2873     }
2874 
2875     if ( AF_HINTS_DO_VERTICAL( hints ) )
2876     {
2877       axis  = &metrics->axis[AF_DIMENSION_VERT];
2878       error = af_latin_hints_detect_features( hints,
2879                                               axis->width_count,
2880                                               axis->widths,
2881                                               AF_DIMENSION_VERT );
2882       if ( error )
2883         goto Exit;
2884 
2885       /* apply blue zones to base characters only */
2886       if ( !( metrics->root.globals->glyph_styles[glyph_index] & AF_NONBASE ) )
2887         af_latin_hints_compute_blue_edges( hints, metrics );
2888     }
2889 
2890     /* grid-fit the outline */
2891     for ( dim = 0; dim < AF_DIMENSION_MAX; dim++ )
2892     {
2893 #ifdef AF_CONFIG_OPTION_USE_WARPER
2894       if ( dim == AF_DIMENSION_HORZ                                 &&
2895            metrics->root.scaler.render_mode == FT_RENDER_MODE_LIGHT &&
2896            AF_HINTS_DO_WARP( hints )                                )
2897       {
2898         AF_WarperRec  warper;
2899         FT_Fixed      scale;
2900         FT_Pos        delta;
2901 
2902 
2903         af_warper_compute( &warper, hints, (AF_Dimension)dim,
2904                            &scale, &delta );
2905         af_glyph_hints_scale_dim( hints, (AF_Dimension)dim,
2906                                   scale, delta );
2907         continue;
2908       }
2909 #endif /* AF_CONFIG_OPTION_USE_WARPER */
2910 
2911       if ( ( dim == AF_DIMENSION_HORZ && AF_HINTS_DO_HORIZONTAL( hints ) ) ||
2912            ( dim == AF_DIMENSION_VERT && AF_HINTS_DO_VERTICAL( hints ) )   )
2913       {
2914         af_latin_hint_edges( hints, (AF_Dimension)dim );
2915         af_glyph_hints_align_edge_points( hints, (AF_Dimension)dim );
2916         af_glyph_hints_align_strong_points( hints, (AF_Dimension)dim );
2917         af_glyph_hints_align_weak_points( hints, (AF_Dimension)dim );
2918       }
2919     }
2920 
2921     af_glyph_hints_save( hints, outline );
2922 
2923   Exit:
2924     return error;
2925   }
2926 
2927 
2928   /*************************************************************************/
2929   /*************************************************************************/
2930   /*****                                                               *****/
2931   /*****              L A T I N   S C R I P T   C L A S S              *****/
2932   /*****                                                               *****/
2933   /*************************************************************************/
2934   /*************************************************************************/
2935 
2936 
2937   AF_DEFINE_WRITING_SYSTEM_CLASS(
2938     af_latin_writing_system_class,
2939 
2940     AF_WRITING_SYSTEM_LATIN,
2941 
2942     sizeof ( AF_LatinMetricsRec ),
2943 
2944     (AF_WritingSystem_InitMetricsFunc) af_latin_metrics_init,
2945     (AF_WritingSystem_ScaleMetricsFunc)af_latin_metrics_scale,
2946     (AF_WritingSystem_DoneMetricsFunc) NULL,
2947 
2948     (AF_WritingSystem_InitHintsFunc)   af_latin_hints_init,
2949     (AF_WritingSystem_ApplyHintsFunc)  af_latin_hints_apply
2950   )
2951 
2952 
2953 /* END */
2954