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