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