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