1 /***************************************************************************/
5 /* Auto-fitter hinting routines for CJK script (body). */
7 /* Copyright 2006-2012 by */
8 /* David Turner, Robert Wilhelm, and Werner Lemberg. */
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. */
16 /***************************************************************************/
19 * The algorithm is based on akito's autohint patch, available here:
21 * http://www.kde.gr.jp/~akito/patch/freetype2/
26 #include FT_ADVANCES_H
27 #include FT_INTERNAL_DEBUG_H
33 #ifdef AF_CONFIG_OPTION_CJK
35 #undef AF_CONFIG_OPTION_CJK_BLUE_HANI_VERT
41 #ifdef AF_CONFIG_OPTION_USE_WARPER
46 /*************************************************************************/
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. */
53 #define FT_COMPONENT trace_afcjk
56 /*************************************************************************/
57 /*************************************************************************/
59 /***** C J K G L O B A L M E T R I C S *****/
61 /*************************************************************************/
62 /*************************************************************************/
65 /* Basically the Latin version with AF_CJKMetrics */
66 /* to replace AF_LatinMetrics. */
69 af_cjk_metrics_init_widths( AF_CJKMetrics metrics,
73 /* scan the array of segments in each direction */
74 AF_GlyphHintsRec hints[1];
77 af_glyph_hints_init( hints, face->memory );
79 metrics->axis[AF_DIMENSION_HORZ].width_count = 0;
80 metrics->axis[AF_DIMENSION_VERT].width_count = 0;
86 AF_CJKMetricsRec dummy[1];
87 AF_Scaler scaler = &dummy->root.scaler;
90 glyph_index = FT_Get_Char_Index( face, charcode );
91 if ( glyph_index == 0 )
94 error = FT_Load_Glyph( face, glyph_index, FT_LOAD_NO_SCALE );
95 if ( error || face->glyph->outline.n_points <= 0 )
100 dummy->units_per_em = metrics->units_per_em;
102 scaler->x_scale = 0x10000L;
103 scaler->y_scale = 0x10000L;
108 scaler->render_mode = FT_RENDER_MODE_NORMAL;
111 af_glyph_hints_rescale( hints, (AF_ScriptMetrics)dummy );
113 error = af_glyph_hints_reload( hints, &face->glyph->outline );
117 for ( dim = 0; dim < AF_DIMENSION_MAX; dim++ )
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;
125 error = af_latin_hints_compute_segments( hints, (AF_Dimension)dim );
129 af_latin_hints_link_segments( hints, (AF_Dimension)dim );
131 seg = axhints->segments;
132 limit = seg + axhints->num_segments;
134 for ( ; seg < limit; seg++ )
138 /* we only consider stem segments there! */
139 if ( link && link->link == seg && link > seg )
144 dist = seg->pos - link->pos;
148 if ( num_widths < AF_CJK_MAX_WIDTHS )
149 axis->widths[num_widths++].org = dist;
153 af_sort_widths( num_widths, axis->widths );
154 axis->width_count = num_widths;
158 for ( dim = 0; dim < AF_DIMENSION_MAX; dim++ )
160 AF_CJKAxis axis = &metrics->axis[dim];
164 stdw = ( axis->width_count > 0 ) ? axis->widths[0].org
165 : AF_LATIN_CONSTANT( metrics, 50 );
167 /* let's try 20% of the smallest width */
168 axis->edge_distance_threshold = stdw / 5;
169 axis->standard_width = stdw;
170 axis->extra_light = 0;
174 af_glyph_hints_done( hints );
178 #define AF_CJK_MAX_TEST_CHARACTERS 32
181 /* Each blue zone has two types of fill and unfill, this is, */
182 /* filling the entire glyph square or not. */
186 AF_CJK_BLUE_TYPE_FILL,
187 AF_CJK_BLUE_TYPE_UNFILL,
192 /* Put some common and representative Han Ideographs characters here. */
193 static const FT_ULong af_cjk_hani_blue_chars[AF_CJK_BLUE_MAX]
194 [AF_CJK_BLUE_TYPE_MAX]
195 [AF_CJK_MAX_TEST_CHARACTERS] =
199 0x4ED6, 0x4EEC, 0x4F60, 0x4F86, 0x5011, 0x5230, 0x548C, 0x5730,
200 0x5BF9, 0x5C0D, 0x5C31, 0x5E2D, 0x6211, 0x65F6, 0x6642, 0x6703,
201 0x6765, 0x70BA, 0x80FD, 0x8230, 0x8AAA, 0x8BF4, 0x8FD9, 0x9019,
202 0x9F4A /* top fill */
205 0x519B, 0x540C, 0x5DF2, 0x613F, 0x65E2, 0x661F, 0x662F, 0x666F,
206 0x6C11, 0x7167, 0x73B0, 0x73FE, 0x7406, 0x7528, 0x7F6E, 0x8981,
207 0x8ECD, 0x90A3, 0x914D, 0x91CC, 0x958B, 0x96F7, 0x9732, 0x9762,
208 0x987E /* top unfill */
213 0x4E2A, 0x4E3A, 0x4EBA, 0x4ED6, 0x4EE5, 0x4EEC, 0x4F60, 0x4F86,
214 0x500B, 0x5011, 0x5230, 0x548C, 0x5927, 0x5BF9, 0x5C0D, 0x5C31,
215 0x6211, 0x65F6, 0x6642, 0x6709, 0x6765, 0x70BA, 0x8981, 0x8AAA,
216 0x8BF4 /* bottom fill */
219 0x4E3B, 0x4E9B, 0x56E0, 0x5B83, 0x60F3, 0x610F, 0x7406, 0x751F,
220 0x7576, 0x770B, 0x7740, 0x7F6E, 0x8005, 0x81EA, 0x8457, 0x88E1,
221 0x8FC7, 0x8FD8, 0x8FDB, 0x9032, 0x904E, 0x9053, 0x9084, 0x91CC,
222 0x9762 /* bottom unfill */
225 #ifndef AF_CONFIG_OPTION_CJK_BLUE_HANI_VERT
226 { {0x0000}, {0x0000} },
227 { {0x0000}, {0x0000} }
231 0x4E9B, 0x4EEC, 0x4F60, 0x4F86, 0x5011, 0x5230, 0x548C, 0x5730,
232 0x5979, 0x5C06, 0x5C07, 0x5C31, 0x5E74, 0x5F97, 0x60C5, 0x6700,
233 0x6837, 0x6A23, 0x7406, 0x80FD, 0x8AAA, 0x8BF4, 0x8FD9, 0x9019,
234 0x901A /* left fill */
237 0x5373, 0x5417, 0x5427, 0x542C, 0x5462, 0x54C1, 0x54CD, 0x55CE,
238 0x5E08, 0x5E2B, 0x6536, 0x65AD, 0x65B7, 0x660E, 0x773C, 0x9593,
239 0x95F4, 0x9645, 0x9648, 0x9650, 0x9664, 0x9673, 0x968F, 0x969B,
240 0x96A8 /* left unfill */
245 0x4E8B, 0x524D, 0x5B78, 0x5C06, 0x5C07, 0x60C5, 0x60F3, 0x6216,
246 0x653F, 0x65AF, 0x65B0, 0x6837, 0x6A23, 0x6C11, 0x6C92, 0x6CA1,
247 0x7136, 0x7279, 0x73B0, 0x73FE, 0x7403, 0x7B2C, 0x7D93, 0x8C01,
248 0x8D77 /* right fill */
251 0x4F8B, 0x5225, 0x522B, 0x5236, 0x52A8, 0x52D5, 0x5417, 0x55CE,
252 0x589E, 0x6307, 0x660E, 0x671D, 0x671F, 0x6784, 0x7269, 0x786E,
253 0x79CD, 0x8ABF, 0x8C03, 0x8CBB, 0x8D39, 0x90A3, 0x90FD, 0x9593,
254 0x95F4 /* right unfill */
257 #endif /* AF_CONFIG_OPTION_CJK_BLUE_HANI_VERT */
261 /* Calculate blue zones for all the CJK_BLUE_XXX's. */
264 af_cjk_metrics_init_blues( AF_CJKMetrics metrics,
266 const FT_ULong blue_chars
268 [AF_CJK_BLUE_TYPE_MAX]
269 [AF_CJK_MAX_TEST_CHARACTERS] )
271 FT_Pos fills[AF_CJK_MAX_TEST_CHARACTERS];
272 FT_Pos flats[AF_CJK_MAX_TEST_CHARACTERS];
281 FT_GlyphSlot glyph = face->glyph;
283 #ifdef FT_DEBUG_LEVEL_TRACE
284 FT_String* cjk_blue_name[AF_CJK_BLUE_MAX] = {
286 (FT_String*)"bottom",
290 FT_String* cjk_blue_type_name[AF_CJK_BLUE_TYPE_MAX] = {
291 (FT_String*)"filled",
292 (FT_String*)"unfilled"
297 /* We compute the blues simply by loading each character from the */
298 /* `blue_chars[blues]' string, then computing its extreme points */
299 /* (depending blue zone type etc.). */
301 FT_TRACE5(( "cjk blue zones computation\n" ));
302 FT_TRACE5(( "------------------------------------------------\n" ));
304 for ( bb = 0; bb < AF_CJK_BLUE_MAX; bb++ )
314 for ( fill_type = 0; fill_type < AF_CJK_BLUE_TYPE_MAX; fill_type++ )
316 const FT_ULong* p = blue_chars[bb][fill_type];
317 const FT_ULong* limit = p + AF_CJK_MAX_TEST_CHARACTERS;
318 FT_Bool fill = FT_BOOL(
319 fill_type == AF_CJK_BLUE_TYPE_FILL );
322 FT_TRACE5(( "cjk blue %s/%s\n", cjk_blue_name[bb],
323 cjk_blue_type_name[fill_type] ));
326 for ( ; p < limit && *p; p++ )
329 FT_Pos best_pos; /* same as points.y */
334 FT_TRACE5(( " U+%lX...", *p ));
336 /* load the character in the face -- skip unknown or empty ones */
337 glyph_index = FT_Get_Char_Index( face, *p );
338 if ( glyph_index == 0 )
340 FT_TRACE5(( "unavailable\n" ));
344 error = FT_Load_Glyph( face, glyph_index, FT_LOAD_NO_SCALE );
345 if ( error || glyph->outline.n_points <= 0 )
347 FT_TRACE5(( "no outline\n" ));
351 /* now compute min or max point indices and coordinates */
352 points = glyph->outline.points;
354 best_pos = 0; /* make compiler happy */
363 nn < glyph->outline.n_contours;
364 first = last + 1, nn++ )
369 last = glyph->outline.contours[nn];
371 /* Avoid single-point contours since they are never */
372 /* rasterized. In some fonts, they correspond to mark */
373 /* attachment points which are way outside of the glyph's */
380 case AF_CJK_BLUE_TOP:
381 for ( pp = first; pp <= last; pp++ )
382 if ( best_point < 0 || points[pp].y > best_pos )
385 best_pos = points[pp].y;
389 case AF_CJK_BLUE_BOTTOM:
390 for ( pp = first; pp <= last; pp++ )
391 if ( best_point < 0 || points[pp].y < best_pos )
394 best_pos = points[pp].y;
398 case AF_CJK_BLUE_LEFT:
399 for ( pp = first; pp <= last; pp++ )
400 if ( best_point < 0 || points[pp].x < best_pos )
403 best_pos = points[pp].x;
407 case AF_CJK_BLUE_RIGHT:
408 for ( pp = first; pp <= last; pp++ )
409 if ( best_point < 0 || points[pp].x > best_pos )
412 best_pos = points[pp].x;
420 FT_TRACE5(( "best_pos=%5ld\n", best_pos ));
424 fills[num_fills++] = best_pos;
426 flats[num_flats++] = best_pos;
430 if ( num_flats == 0 && num_fills == 0 )
433 * we couldn't find a single glyph to compute this blue zone,
434 * we will simply ignore it then
436 FT_TRACE5(( "empty\n" ));
440 /* we have computed the contents of the `fill' and `flats' tables, */
441 /* now determine the reference position of the blue -- */
442 /* we simply take the median value after a simple sort */
443 af_sort_pos( num_flats, flats );
444 af_sort_pos( num_fills, fills );
446 if ( AF_CJK_BLUE_TOP == bb || AF_CJK_BLUE_BOTTOM == bb )
447 axis = &metrics->axis[AF_DIMENSION_VERT];
449 axis = &metrics->axis[AF_DIMENSION_HORZ];
451 blue = & axis->blues[axis->blue_count];
452 blue_ref = & blue->ref.org;
453 blue_shoot = & blue->shoot.org;
456 if ( num_flats == 0 )
458 *blue_ref = fills[num_fills / 2];
459 *blue_shoot = fills[num_fills / 2];
461 else if ( num_fills == 0 )
463 *blue_ref = flats[num_flats / 2];
464 *blue_shoot = flats[num_flats / 2];
468 *blue_ref = fills[num_fills / 2];
469 *blue_shoot = flats[num_flats / 2];
472 /* make sure blue_ref >= blue_shoot for top/right or */
473 /* vice versa for bottom/left */
474 if ( *blue_shoot != *blue_ref )
476 FT_Pos ref = *blue_ref;
477 FT_Pos shoot = *blue_shoot;
478 FT_Bool under_ref = FT_BOOL( shoot < ref );
481 if ( (AF_CJK_BLUE_TOP == bb || AF_CJK_BLUE_RIGHT == bb) ^ under_ref )
482 *blue_shoot = *blue_ref = ( shoot + ref ) / 2;
486 if ( AF_CJK_BLUE_TOP == bb )
487 blue->flags |= AF_CJK_BLUE_IS_TOP;
488 else if ( AF_CJK_BLUE_RIGHT == bb )
489 blue->flags |= AF_CJK_BLUE_IS_RIGHT;
491 FT_TRACE5(( "-- cjk %s bluezone ref = %ld shoot = %ld\n",
492 cjk_blue_name[bb], *blue_ref, *blue_shoot ));
499 /* Basically the Latin version with type AF_CJKMetrics for metrics. */
501 af_cjk_metrics_check_digits( AF_CJKMetrics metrics,
505 FT_Bool started = 0, same_width = 1;
506 FT_Fixed advance, old_advance = 0;
509 /* check whether all ASCII digits have the same advance width; */
510 /* digit `0' is 0x30 in all supported charmaps */
511 for ( i = 0x30; i <= 0x39; i++ )
516 glyph_index = FT_Get_Char_Index( face, i );
517 if ( glyph_index == 0 )
520 if ( FT_Get_Advance( face, glyph_index,
523 FT_LOAD_IGNORE_TRANSFORM,
529 if ( advance != old_advance )
537 old_advance = advance;
542 metrics->root.digits_have_same_width = same_width;
546 FT_LOCAL_DEF( FT_Error )
547 af_cjk_metrics_init( AF_CJKMetrics metrics,
550 FT_CharMap oldmap = face->charmap;
553 metrics->units_per_em = face->units_per_EM;
555 if ( FT_Select_Charmap( face, FT_ENCODING_UNICODE ) )
556 face->charmap = NULL;
559 af_cjk_metrics_init_widths( metrics, face, 0x7530 );
560 af_cjk_metrics_init_blues( metrics, face, af_cjk_hani_blue_chars );
561 af_cjk_metrics_check_digits( metrics, face );
564 FT_Set_Charmap( face, oldmap );
571 af_cjk_metrics_scale_dim( AF_CJKMetrics metrics,
581 axis = &metrics->axis[dim];
583 if ( dim == AF_DIMENSION_HORZ )
585 scale = scaler->x_scale;
586 delta = scaler->x_delta;
590 scale = scaler->y_scale;
591 delta = scaler->y_delta;
594 if ( axis->org_scale == scale && axis->org_delta == delta )
597 axis->org_scale = scale;
598 axis->org_delta = delta;
603 /* scale the blue zones */
604 for ( nn = 0; nn < axis->blue_count; nn++ )
606 AF_CJKBlue blue = &axis->blues[nn];
610 blue->ref.cur = FT_MulFix( blue->ref.org, scale ) + delta;
611 blue->ref.fit = blue->ref.cur;
612 blue->shoot.cur = FT_MulFix( blue->shoot.org, scale ) + delta;
613 blue->shoot.fit = blue->shoot.cur;
614 blue->flags &= ~AF_CJK_BLUE_ACTIVE;
616 /* a blue zone is only active if it is less than 3/4 pixels tall */
617 dist = FT_MulFix( blue->ref.org - blue->shoot.org, scale );
618 if ( dist <= 48 && dist >= -48 )
620 FT_Pos delta1, delta2;
623 blue->ref.fit = FT_PIX_ROUND( blue->ref.cur );
625 /* shoot is under shoot for cjk */
626 delta1 = FT_DivFix( blue->ref.fit, scale ) - blue->shoot.org;
631 delta2 = FT_MulFix( delta2, scale );
633 FT_TRACE5(( "delta: %d", delta1 ));
637 else if ( delta2 < 64 )
638 delta2 = 32 + ( ( ( delta2 - 32 ) + 16 ) & ~31 );
641 delta2 = FT_PIX_ROUND( delta2 );
642 FT_TRACE5(( "/%d\n", delta2 ));
647 blue->shoot.fit = blue->ref.fit - delta2;
649 FT_TRACE5(( ">> active cjk blue zone %c%d[%ld/%ld]: "
650 "ref: cur=%.2f fit=%.2f shoot: cur=%.2f fit=%.2f\n",
651 ( dim == AF_DIMENSION_HORZ ) ? 'H' : 'V',
652 nn, blue->ref.org, blue->shoot.org,
653 blue->ref.cur / 64.0, blue->ref.fit / 64.0,
654 blue->shoot.cur / 64.0, blue->shoot.fit / 64.0 ));
656 blue->flags |= AF_CJK_BLUE_ACTIVE;
663 af_cjk_metrics_scale( AF_CJKMetrics metrics,
666 metrics->root.scaler = *scaler;
668 af_cjk_metrics_scale_dim( metrics, scaler, AF_DIMENSION_HORZ );
669 af_cjk_metrics_scale_dim( metrics, scaler, AF_DIMENSION_VERT );
673 /*************************************************************************/
674 /*************************************************************************/
676 /***** C J K G L Y P H A N A L Y S I S *****/
678 /*************************************************************************/
679 /*************************************************************************/
682 af_cjk_hints_compute_segments( AF_GlyphHints hints,
685 AF_AxisHints axis = &hints->axis[dim];
686 AF_Segment segments = axis->segments;
687 AF_Segment segment_limit = segments + axis->num_segments;
692 error = af_latin_hints_compute_segments( hints, dim );
696 /* a segment is round if it doesn't have successive */
697 /* on-curve points. */
698 for ( seg = segments; seg < segment_limit; seg++ )
700 AF_Point pt = seg->first;
701 AF_Point last = seg->last;
702 AF_Flags f0 = (AF_Flags)(pt->flags & AF_FLAG_CONTROL);
706 seg->flags &= ~AF_EDGE_ROUND;
708 for ( ; pt != last; f0 = f1 )
711 f1 = (AF_Flags)(pt->flags & AF_FLAG_CONTROL);
717 seg->flags |= AF_EDGE_ROUND;
726 af_cjk_hints_link_segments( AF_GlyphHints hints,
729 AF_AxisHints axis = &hints->axis[dim];
730 AF_Segment segments = axis->segments;
731 AF_Segment segment_limit = segments + axis->num_segments;
732 AF_Direction major_dir = axis->major_dir;
733 AF_Segment seg1, seg2;
734 FT_Pos len_threshold;
735 FT_Pos dist_threshold;
738 len_threshold = AF_LATIN_CONSTANT( hints->metrics, 8 );
740 dist_threshold = ( dim == AF_DIMENSION_HORZ ) ? hints->x_scale
742 dist_threshold = FT_DivFix( 64 * 3, dist_threshold );
744 /* now compare each segment to the others */
745 for ( seg1 = segments; seg1 < segment_limit; seg1++ )
747 /* the fake segments are for metrics hinting only */
748 if ( seg1->first == seg1->last )
751 if ( seg1->dir != major_dir )
754 for ( seg2 = segments; seg2 < segment_limit; seg2++ )
755 if ( seg2 != seg1 && seg1->dir + seg2->dir == 0 )
757 FT_Pos dist = seg2->pos - seg1->pos;
764 FT_Pos min = seg1->min_coord;
765 FT_Pos max = seg1->max_coord;
769 if ( min < seg2->min_coord )
770 min = seg2->min_coord;
772 if ( max > seg2->max_coord )
773 max = seg2->max_coord;
776 if ( len >= len_threshold )
778 if ( dist * 8 < seg1->score * 9 &&
779 ( dist * 8 < seg1->score * 7 || seg1->len < len ) )
786 if ( dist * 8 < seg2->score * 9 &&
787 ( dist * 8 < seg2->score * 7 || seg2->len < len ) )
799 * now compute the `serif' segments
801 * In Hanzi, some strokes are wider on one or both of the ends.
802 * We either identify the stems on the ends as serifs or remove
803 * the linkage, depending on the length of the stems.
808 AF_Segment link1, link2;
811 for ( seg1 = segments; seg1 < segment_limit; seg1++ )
814 if ( !link1 || link1->link != seg1 || link1->pos <= seg1->pos )
817 if ( seg1->score >= dist_threshold )
820 for ( seg2 = segments; seg2 < segment_limit; seg2++ )
822 if ( seg2->pos > seg1->pos || seg1 == seg2 )
826 if ( !link2 || link2->link != seg2 || link2->pos < link1->pos )
829 if ( seg1->pos == seg2->pos && link1->pos == link2->pos )
832 if ( seg2->score <= seg1->score || seg1->score * 4 <= seg2->score )
835 /* seg2 < seg1 < link1 < link2 */
837 if ( seg1->len >= seg2->len * 3 )
842 for ( seg = segments; seg < segment_limit; seg++ )
844 AF_Segment link = seg->link;
852 else if ( link == link2 )
861 seg1->link = link1->link = 0;
869 for ( seg1 = segments; seg1 < segment_limit; seg1++ )
876 if ( seg2->link != seg1 )
880 if ( seg2->score < dist_threshold || seg1->score < seg2->score * 4 )
881 seg1->serif = seg2->link;
891 af_cjk_hints_compute_edges( AF_GlyphHints hints,
894 AF_AxisHints axis = &hints->axis[dim];
895 FT_Error error = AF_Err_Ok;
896 FT_Memory memory = hints->memory;
897 AF_CJKAxis laxis = &((AF_CJKMetrics)hints->metrics)->axis[dim];
899 AF_Segment segments = axis->segments;
900 AF_Segment segment_limit = segments + axis->num_segments;
904 FT_Pos edge_distance_threshold;
909 scale = ( dim == AF_DIMENSION_HORZ ) ? hints->x_scale
912 /*********************************************************************/
914 /* We begin by generating a sorted table of edges for the current */
915 /* direction. To do so, we simply scan each segment and try to find */
916 /* an edge in our table that corresponds to its position. */
918 /* If no edge is found, we create and insert a new edge in the */
919 /* sorted table. Otherwise, we simply add the segment to the edge's */
920 /* list which is then processed in the second step to compute the */
921 /* edge's properties. */
923 /* Note that the edges table is sorted along the segment/edge */
926 /*********************************************************************/
928 edge_distance_threshold = FT_MulFix( laxis->edge_distance_threshold,
930 if ( edge_distance_threshold > 64 / 4 )
931 edge_distance_threshold = FT_DivFix( 64 / 4, scale );
933 edge_distance_threshold = laxis->edge_distance_threshold;
935 for ( seg = segments; seg < segment_limit; seg++ )
938 FT_Pos best = 0xFFFFU;
942 /* look for an edge corresponding to the segment */
943 for ( ee = 0; ee < axis->num_edges; ee++ )
945 AF_Edge edge = axis->edges + ee;
949 if ( edge->dir != seg->dir )
952 dist = seg->pos - edge->fpos;
956 if ( dist < edge_distance_threshold && dist < best )
958 AF_Segment link = seg->link;
961 /* check whether all linked segments of the candidate edge */
962 /* can make a single edge. */
965 AF_Segment seg1 = edge->first;
975 dist2 = AF_SEGMENT_DIST( link, link1 );
976 if ( dist2 >= edge_distance_threshold )
980 } while ( ( seg1 = seg1->edge_next ) != edge->first );
982 if ( dist2 >= edge_distance_threshold )
996 /* insert a new edge in the list and */
997 /* sort according to the position */
998 error = af_axis_hints_new_edge( axis, seg->pos,
999 (AF_Direction)seg->dir,
1004 /* add the segment to the new edge's list */
1009 edge->fpos = seg->pos;
1010 edge->opos = edge->pos = FT_MulFix( seg->pos, scale );
1011 seg->edge_next = seg;
1012 edge->dir = seg->dir;
1016 /* if an edge was found, simply add the segment to the edge's */
1018 seg->edge_next = found->first;
1019 found->last->edge_next = seg;
1024 /*********************************************************************/
1026 /* Good, we now compute each edge's properties according to segments */
1027 /* found on its position. Basically, these are as follows. */
1029 /* - edge's main direction */
1030 /* - stem edge, serif edge or both (which defaults to stem then) */
1031 /* - rounded edge, straight or both (which defaults to straight) */
1032 /* - link for edge */
1034 /*********************************************************************/
1036 /* first of all, set the `edge' field in each segment -- this is */
1037 /* required in order to compute edge links */
1039 /* Note that removing this loop and setting the `edge' field of each */
1040 /* segment directly in the code above slows down execution speed for */
1041 /* some reasons on platforms like the Sun. */
1044 AF_Edge edges = axis->edges;
1045 AF_Edge edge_limit = edges + axis->num_edges;
1049 for ( edge = edges; edge < edge_limit; edge++ )
1056 seg = seg->edge_next;
1058 } while ( seg != edge->first );
1061 /* now compute each edge properties */
1062 for ( edge = edges; edge < edge_limit; edge++ )
1064 FT_Int is_round = 0; /* does it contain round segments? */
1065 FT_Int is_straight = 0; /* does it contain straight segments? */
1075 /* check for roundness of segment */
1076 if ( seg->flags & AF_EDGE_ROUND )
1081 /* check for links -- if seg->serif is set, then seg->link must */
1083 is_serif = (FT_Bool)( seg->serif && seg->serif->edge != edge );
1085 if ( seg->link || is_serif )
1097 edge2 = edge->serif;
1106 edge_delta = edge->fpos - edge2->fpos;
1107 if ( edge_delta < 0 )
1108 edge_delta = -edge_delta;
1110 seg_delta = AF_SEGMENT_DIST( seg, seg2 );
1112 if ( seg_delta < edge_delta )
1120 edge->serif = edge2;
1121 edge2->flags |= AF_EDGE_SERIF;
1127 seg = seg->edge_next;
1129 } while ( seg != edge->first );
1131 /* set the round/straight flags */
1132 edge->flags = AF_EDGE_NORMAL;
1134 if ( is_round > 0 && is_round >= is_straight )
1135 edge->flags |= AF_EDGE_ROUND;
1137 /* get rid of serifs if link is set */
1138 /* XXX: This gets rid of many unpleasant artefacts! */
1139 /* Example: the `c' in cour.pfa at size 13 */
1141 if ( edge->serif && edge->link )
1152 af_cjk_hints_detect_features( AF_GlyphHints hints,
1158 error = af_cjk_hints_compute_segments( hints, dim );
1161 af_cjk_hints_link_segments( hints, dim );
1163 error = af_cjk_hints_compute_edges( hints, dim );
1169 FT_LOCAL_DEF( void )
1170 af_cjk_hints_compute_blue_edges( AF_GlyphHints hints,
1171 AF_CJKMetrics metrics,
1174 AF_AxisHints axis = &hints->axis[dim];
1175 AF_Edge edge = axis->edges;
1176 AF_Edge edge_limit = edge + axis->num_edges;
1177 AF_CJKAxis cjk = &metrics->axis[dim];
1178 FT_Fixed scale = cjk->scale;
1179 FT_Pos best_dist0; /* initial threshold */
1182 /* compute the initial threshold as a fraction of the EM size */
1183 best_dist0 = FT_MulFix( metrics->units_per_em / 40, scale );
1185 if ( best_dist0 > 64 / 2 ) /* maximum 1/2 pixel */
1186 best_dist0 = 64 / 2;
1188 /* compute which blue zones are active, i.e. have their scaled */
1189 /* size < 3/4 pixels */
1191 /* If the distant between an edge and a blue zone is shorter than */
1192 /* best_dist0, set the blue zone for the edge. Then search for */
1193 /* the blue zone with the smallest best_dist to the edge. */
1195 for ( ; edge < edge_limit; edge++ )
1198 AF_Width best_blue = NULL;
1199 FT_Pos best_dist = best_dist0;
1202 for ( bb = 0; bb < cjk->blue_count; bb++ )
1204 AF_CJKBlue blue = cjk->blues + bb;
1205 FT_Bool is_top_right_blue, is_major_dir;
1208 /* skip inactive blue zones (i.e., those that are too small) */
1209 if ( !( blue->flags & AF_CJK_BLUE_ACTIVE ) )
1212 /* if it is a top zone, check for right edges -- if it is a bottom */
1213 /* zone, check for left edges */
1215 /* of course, that's for TrueType */
1217 FT_BOOL( ( ( blue->flags & AF_CJK_BLUE_IS_TOP ) != 0 ) ||
1218 ( ( blue->flags & AF_CJK_BLUE_IS_RIGHT ) != 0 ) );
1219 is_major_dir = FT_BOOL( edge->dir == axis->major_dir );
1221 /* if it is a top zone, the edge must be against the major */
1222 /* direction; if it is a bottom zone, it must be in the major */
1224 if ( is_top_right_blue ^ is_major_dir )
1230 /* Compare the edge to the closest blue zone type */
1231 if ( FT_ABS( edge->fpos - blue->ref.org ) >
1232 FT_ABS( edge->fpos - blue->shoot.org ) )
1233 compare = &blue->shoot;
1235 compare = &blue->ref;
1237 dist = edge->fpos - compare->org;
1241 dist = FT_MulFix( dist, scale );
1242 if ( dist < best_dist )
1245 best_blue = compare;
1251 edge->blue_edge = best_blue;
1256 FT_LOCAL_DEF( FT_Error )
1257 af_cjk_hints_init( AF_GlyphHints hints,
1258 AF_CJKMetrics metrics )
1260 FT_Render_Mode mode;
1261 FT_UInt32 scaler_flags, other_flags;
1264 af_glyph_hints_rescale( hints, (AF_ScriptMetrics)metrics );
1267 * correct x_scale and y_scale when needed, since they may have
1268 * been modified af_cjk_scale_dim above
1270 hints->x_scale = metrics->axis[AF_DIMENSION_HORZ].scale;
1271 hints->x_delta = metrics->axis[AF_DIMENSION_HORZ].delta;
1272 hints->y_scale = metrics->axis[AF_DIMENSION_VERT].scale;
1273 hints->y_delta = metrics->axis[AF_DIMENSION_VERT].delta;
1275 /* compute flags depending on render mode, etc. */
1276 mode = metrics->root.scaler.render_mode;
1278 #ifdef AF_CONFIG_OPTION_USE_WARPER
1279 if ( mode == FT_RENDER_MODE_LCD || mode == FT_RENDER_MODE_LCD_V )
1280 metrics->root.scaler.render_mode = mode = FT_RENDER_MODE_NORMAL;
1283 scaler_flags = hints->scaler_flags;
1287 * We snap the width of vertical stems for the monochrome and
1288 * horizontal LCD rendering targets only.
1290 if ( mode == FT_RENDER_MODE_MONO || mode == FT_RENDER_MODE_LCD )
1291 other_flags |= AF_LATIN_HINTS_HORZ_SNAP;
1294 * We snap the width of horizontal stems for the monochrome and
1295 * vertical LCD rendering targets only.
1297 if ( mode == FT_RENDER_MODE_MONO || mode == FT_RENDER_MODE_LCD_V )
1298 other_flags |= AF_LATIN_HINTS_VERT_SNAP;
1301 * We adjust stems to full pixels only if we don't use the `light' mode.
1303 if ( mode != FT_RENDER_MODE_LIGHT )
1304 other_flags |= AF_LATIN_HINTS_STEM_ADJUST;
1306 if ( mode == FT_RENDER_MODE_MONO )
1307 other_flags |= AF_LATIN_HINTS_MONO;
1309 scaler_flags |= AF_SCALER_FLAG_NO_ADVANCE;
1311 hints->scaler_flags = scaler_flags;
1312 hints->other_flags = other_flags;
1318 /*************************************************************************/
1319 /*************************************************************************/
1321 /***** C J K G L Y P H G R I D - F I T T I N G *****/
1323 /*************************************************************************/
1324 /*************************************************************************/
1326 /* snap a given width in scaled coordinates to one of the */
1327 /* current standard widths */
1330 af_cjk_snap_width( AF_Width widths,
1335 FT_Pos best = 64 + 32 + 2;
1336 FT_Pos reference = width;
1340 for ( n = 0; n < count; n++ )
1357 scaled = FT_PIX_ROUND( reference );
1359 if ( width >= reference )
1361 if ( width < scaled + 48 )
1366 if ( width > scaled - 48 )
1374 /* compute the snapped width of a given stem */
1377 af_cjk_compute_stem_width( AF_GlyphHints hints,
1380 AF_Edge_Flags base_flags,
1381 AF_Edge_Flags stem_flags )
1383 AF_CJKMetrics metrics = (AF_CJKMetrics) hints->metrics;
1384 AF_CJKAxis axis = & metrics->axis[dim];
1385 FT_Pos dist = width;
1387 FT_Bool vertical = FT_BOOL( dim == AF_DIMENSION_VERT );
1389 FT_UNUSED( base_flags );
1390 FT_UNUSED( stem_flags );
1393 if ( !AF_LATIN_HINTS_DO_STEM_ADJUST( hints ) )
1402 if ( ( vertical && !AF_LATIN_HINTS_DO_VERT_SNAP( hints ) ) ||
1403 ( !vertical && !AF_LATIN_HINTS_DO_HORZ_SNAP( hints ) ) )
1405 /* smooth hinting process: very lightly quantize the stem width */
1407 if ( axis->width_count > 0 )
1409 if ( FT_ABS( dist - axis->widths[0].cur ) < 40 )
1411 dist = axis->widths[0].cur;
1420 dist += ( 54 - dist ) / 2 ;
1421 else if ( dist < 3 * 64 )
1431 else if ( delta < 22 )
1433 else if ( delta < 42 )
1435 else if ( delta < 54 )
1443 /* strong hinting process: snap the stem width to integer pixels */
1445 dist = af_cjk_snap_width( axis->widths, axis->width_count, dist );
1449 /* in the case of vertical hinting, always round */
1450 /* the stem heights to integer pixels */
1453 dist = ( dist + 16 ) & ~63;
1459 if ( AF_LATIN_HINTS_DO_MONO( hints ) )
1461 /* monochrome horizontal hinting: snap widths to integer pixels */
1462 /* with a different threshold */
1467 dist = ( dist + 32 ) & ~63;
1471 /* for horizontal anti-aliased hinting, we adopt a more subtle */
1472 /* approach: we strengthen small stems, round stems whose size */
1473 /* is between 1 and 2 pixels to an integer, otherwise nothing */
1476 dist = ( dist + 64 ) >> 1;
1478 else if ( dist < 128 )
1479 dist = ( dist + 22 ) & ~63;
1481 /* round otherwise to prevent color fringes in LCD mode */
1482 dist = ( dist + 32 ) & ~63;
1495 /* align one stem edge relative to the previous stem edge */
1498 af_cjk_align_linked_edge( AF_GlyphHints hints,
1503 FT_Pos dist = stem_edge->opos - base_edge->opos;
1505 FT_Pos fitted_width = af_cjk_compute_stem_width(
1507 (AF_Edge_Flags)base_edge->flags,
1508 (AF_Edge_Flags)stem_edge->flags );
1511 stem_edge->pos = base_edge->pos + fitted_width;
1516 af_cjk_align_serif_edge( AF_GlyphHints hints,
1522 serif->pos = base->pos + ( serif->opos - base->opos );
1526 /*************************************************************************/
1527 /*************************************************************************/
1528 /*************************************************************************/
1530 /**** E D G E H I N T I N G ****/
1532 /*************************************************************************/
1533 /*************************************************************************/
1534 /*************************************************************************/
1537 #define AF_LIGHT_MODE_MAX_HORZ_GAP 9
1538 #define AF_LIGHT_MODE_MAX_VERT_GAP 15
1539 #define AF_LIGHT_MODE_MAX_DELTA_ABS 14
1543 af_hint_normal_stem( AF_GlyphHints hints,
1549 FT_Pos org_len, cur_len, org_center;
1550 FT_Pos cur_pos1, cur_pos2;
1551 FT_Pos d_off1, u_off1, d_off2, u_off2, delta;
1553 FT_Pos threshold = 64;
1556 if ( !AF_LATIN_HINTS_DO_STEM_ADJUST( hints ) )
1558 if ( ( edge->flags & AF_EDGE_ROUND ) &&
1559 ( edge2->flags & AF_EDGE_ROUND ) )
1561 if ( dim == AF_DIMENSION_VERT )
1562 threshold = 64 - AF_LIGHT_MODE_MAX_HORZ_GAP;
1564 threshold = 64 - AF_LIGHT_MODE_MAX_VERT_GAP;
1568 if ( dim == AF_DIMENSION_VERT )
1569 threshold = 64 - AF_LIGHT_MODE_MAX_HORZ_GAP / 3;
1571 threshold = 64 - AF_LIGHT_MODE_MAX_VERT_GAP / 3;
1575 org_len = edge2->opos - edge->opos;
1576 cur_len = af_cjk_compute_stem_width( hints, dim, org_len,
1577 (AF_Edge_Flags)edge->flags,
1578 (AF_Edge_Flags)edge2->flags );
1580 org_center = ( edge->opos + edge2->opos ) / 2 + anchor;
1581 cur_pos1 = org_center - cur_len / 2;
1582 cur_pos2 = cur_pos1 + cur_len;
1583 d_off1 = cur_pos1 - FT_PIX_FLOOR( cur_pos1 );
1584 d_off2 = cur_pos2 - FT_PIX_FLOOR( cur_pos2 );
1585 u_off1 = 64 - d_off1;
1586 u_off2 = 64 - d_off2;
1590 if ( d_off1 == 0 || d_off2 == 0 )
1593 if ( cur_len <= threshold )
1595 if ( d_off2 < cur_len )
1597 if ( u_off1 <= d_off2 )
1606 if ( threshold < 64 )
1608 if ( d_off1 >= threshold || u_off1 >= threshold ||
1609 d_off2 >= threshold || u_off2 >= threshold )
1613 offset = cur_len & 63;
1617 if ( u_off1 <= offset || d_off2 <= offset )
1621 offset = 64 - threshold;
1623 d_off1 = threshold - u_off1;
1624 u_off1 = u_off1 - offset;
1625 u_off2 = threshold - d_off2;
1626 d_off2 = d_off2 - offset;
1628 if ( d_off1 <= u_off1 )
1631 if ( d_off2 <= u_off2 )
1634 if ( FT_ABS( u_off1 ) <= FT_ABS( u_off2 ) )
1642 if ( !AF_LATIN_HINTS_DO_STEM_ADJUST( hints ) )
1644 if ( delta > AF_LIGHT_MODE_MAX_DELTA_ABS )
1645 delta = AF_LIGHT_MODE_MAX_DELTA_ABS;
1646 else if ( delta < -AF_LIGHT_MODE_MAX_DELTA_ABS )
1647 delta = -AF_LIGHT_MODE_MAX_DELTA_ABS;
1653 if ( edge->opos < edge2->opos )
1655 edge->pos = cur_pos1;
1656 edge2->pos = cur_pos1 + cur_len;
1660 edge->pos = cur_pos1 + cur_len;
1661 edge2->pos = cur_pos1;
1669 af_cjk_hint_edges( AF_GlyphHints hints,
1672 AF_AxisHints axis = &hints->axis[dim];
1673 AF_Edge edges = axis->edges;
1674 AF_Edge edge_limit = edges + axis->num_edges;
1680 FT_Bool has_last_stem = FALSE;
1681 FT_Pos last_stem_pos = 0;
1684 /* we begin by aligning all stems relative to the blue zone */
1685 FT_TRACE5(( "==== cjk hinting %s edges =====\n",
1686 dim == AF_DIMENSION_HORZ ? "vertical" : "horizontal" ));
1688 if ( AF_HINTS_DO_BLUES( hints ) )
1690 for ( edge = edges; edge < edge_limit; edge++ )
1693 AF_Edge edge1, edge2;
1696 if ( edge->flags & AF_EDGE_DONE )
1699 blue = edge->blue_edge;
1707 else if ( edge2 && edge2->blue_edge )
1709 blue = edge2->blue_edge;
1717 FT_TRACE5(( "CJKBLUE: edge %d @%d (opos=%.2f) snapped to (%.2f), "
1719 edge1-edges, edge1->fpos, edge1->opos / 64.0, blue->fit / 64.0,
1720 edge1->pos / 64.0 ));
1722 edge1->pos = blue->fit;
1723 edge1->flags |= AF_EDGE_DONE;
1725 if ( edge2 && !edge2->blue_edge )
1727 af_cjk_align_linked_edge( hints, dim, edge1, edge2 );
1728 edge2->flags |= AF_EDGE_DONE;
1736 /* now we align all stem edges. */
1737 for ( edge = edges; edge < edge_limit; edge++ )
1742 if ( edge->flags & AF_EDGE_DONE )
1745 /* skip all non-stem edges */
1753 /* Some CJK characters have so many stems that
1754 * the hinter is likely to merge two adjacent ones.
1755 * To solve this problem, if either edge of a stem
1756 * is too close to the previous one, we avoid
1757 * aligning the two edges, but rather interpolate
1758 * their locations at the end of this function in
1759 * order to preserve the space between the stems.
1761 if ( has_last_stem &&
1762 ( edge->pos < last_stem_pos + 64 ||
1763 edge2->pos < last_stem_pos + 64 ) )
1769 /* now align the stem */
1770 /* this should not happen, but it's better to be safe */
1771 if ( edge2->blue_edge )
1773 FT_TRACE5(( "ASSERTION FAILED for edge %d\n", edge2-edges ));
1775 af_cjk_align_linked_edge( hints, dim, edge2, edge );
1776 edge->flags |= AF_EDGE_DONE;
1782 af_cjk_align_linked_edge( hints, dim, edge2, edge );
1783 edge->flags |= AF_EDGE_DONE;
1784 /* We rarely reaches here it seems;
1785 * usually the two edges belonging
1786 * to one stem are marked as DONE together
1788 has_last_stem = TRUE;
1789 last_stem_pos = edge->pos;
1793 if ( dim != AF_DIMENSION_VERT && !anchor )
1799 AF_Edge left = edge;
1800 AF_Edge right = edge_limit - 1;
1801 AF_EdgeRec left1, left2, right1, right2;
1802 FT_Pos target, center1, center2;
1803 FT_Pos delta1, delta2, d1, d2;
1806 while ( right > left && !right->link )
1810 left2 = *left->link;
1811 right1 = *right->link;
1814 delta = ( ( ( hinter->pp2.x + 32 ) & -64 ) - hinter->pp2.x ) / 2;
1815 target = left->opos + ( right->opos - left->opos ) / 2 + delta - 16;
1818 delta1 += af_hint_normal_stem( hints, left, left->link,
1821 if ( left->link != right )
1822 af_hint_normal_stem( hints, right->link, right, delta1, 0 );
1824 center1 = left->pos + ( right->pos - left->pos ) / 2;
1826 if ( center1 >= target )
1827 delta2 = delta - 32;
1829 delta2 = delta + 32;
1831 delta2 += af_hint_normal_stem( hints, &left1, &left2, delta2, 0 );
1833 if ( delta1 != delta2 )
1835 if ( left->link != right )
1836 af_hint_normal_stem( hints, &right1, &right2, delta2, 0 );
1838 center2 = left1.pos + ( right2.pos - left1.pos ) / 2;
1840 d1 = center1 - target;
1841 d2 = center2 - target;
1843 if ( FT_ABS( d2 ) < FT_ABS( d1 ) )
1845 left->pos = left1.pos;
1846 left->link->pos = left2.pos;
1848 if ( left->link != right )
1850 right->link->pos = right1.pos;
1851 right->pos = right2.pos;
1859 right->link->flags |= AF_EDGE_DONE;
1860 right->flags |= AF_EDGE_DONE;
1866 delta = af_hint_normal_stem( hints, edge, edge2, 0,
1867 AF_DIMENSION_HORZ );
1870 af_hint_normal_stem( hints, edge, edge2, delta, dim );
1873 printf( "stem (%d,%d) adjusted (%.1f,%.1f)\n",
1874 edge - edges, edge2 - edges,
1875 ( edge->pos - edge->opos ) / 64.0,
1876 ( edge2->pos - edge2->opos ) / 64.0 );
1880 edge->flags |= AF_EDGE_DONE;
1881 edge2->flags |= AF_EDGE_DONE;
1882 has_last_stem = TRUE;
1883 last_stem_pos = edge2->pos;
1886 /* make sure that lowercase m's maintain their symmetry */
1888 /* In general, lowercase m's have six vertical edges if they are sans */
1889 /* serif, or twelve if they are with serifs. This implementation is */
1890 /* based on that assumption, and seems to work very well with most */
1891 /* faces. However, if for a certain face this assumption is not */
1892 /* true, the m is just rendered like before. In addition, any stem */
1893 /* correction will only be applied to symmetrical glyphs (even if the */
1894 /* glyph is not an m), so the potential for unwanted distortion is */
1895 /* relatively low. */
1897 /* We don't handle horizontal edges since we can't easily assure that */
1898 /* the third (lowest) stem aligns with the base line; it might end up */
1899 /* one pixel higher or lower. */
1901 n_edges = edge_limit - edges;
1902 if ( dim == AF_DIMENSION_HORZ && ( n_edges == 6 || n_edges == 12 ) )
1904 AF_Edge edge1, edge2, edge3;
1905 FT_Pos dist1, dist2, span;
1921 dist1 = edge2->opos - edge1->opos;
1922 dist2 = edge3->opos - edge2->opos;
1924 span = dist1 - dist2;
1928 if ( edge1->link == edge1 + 1 &&
1929 edge2->link == edge2 + 1 &&
1930 edge3->link == edge3 + 1 && span < 8 )
1932 delta = edge3->pos - ( 2 * edge2->pos - edge1->pos );
1933 edge3->pos -= delta;
1935 edge3->link->pos -= delta;
1937 /* move the serifs along with the stem */
1938 if ( n_edges == 12 )
1940 ( edges + 8 )->pos -= delta;
1941 ( edges + 11 )->pos -= delta;
1944 edge3->flags |= AF_EDGE_DONE;
1946 edge3->link->flags |= AF_EDGE_DONE;
1954 * now hint the remaining edges (serifs and single) in order
1955 * to complete our processing
1957 for ( edge = edges; edge < edge_limit; edge++ )
1959 if ( edge->flags & AF_EDGE_DONE )
1964 af_cjk_align_serif_edge( hints, edge->serif, edge );
1965 edge->flags |= AF_EDGE_DONE;
1973 for ( edge = edges; edge < edge_limit; edge++ )
1975 AF_Edge before, after;
1978 if ( edge->flags & AF_EDGE_DONE )
1981 before = after = edge;
1983 while ( --before >= edges )
1984 if ( before->flags & AF_EDGE_DONE )
1987 while ( ++after < edge_limit )
1988 if ( after->flags & AF_EDGE_DONE )
1991 if ( before >= edges || after < edge_limit )
1993 if ( before < edges )
1994 af_cjk_align_serif_edge( hints, after, edge );
1995 else if ( after >= edge_limit )
1996 af_cjk_align_serif_edge( hints, before, edge );
1999 if ( after->fpos == before->fpos )
2000 edge->pos = before->pos;
2002 edge->pos = before->pos +
2003 FT_MulDiv( edge->fpos - before->fpos,
2004 after->pos - before->pos,
2005 after->fpos - before->fpos );
2013 af_cjk_align_edge_points( AF_GlyphHints hints,
2016 AF_AxisHints axis = & hints->axis[dim];
2017 AF_Edge edges = axis->edges;
2018 AF_Edge edge_limit = edges + axis->num_edges;
2023 snapping = FT_BOOL( ( dim == AF_DIMENSION_HORZ &&
2024 AF_LATIN_HINTS_DO_HORZ_SNAP( hints ) ) ||
2025 ( dim == AF_DIMENSION_VERT &&
2026 AF_LATIN_HINTS_DO_VERT_SNAP( hints ) ) );
2028 for ( edge = edges; edge < edge_limit; edge++ )
2030 /* move the points of each segment */
2031 /* in each edge to the edge's position */
2032 AF_Segment seg = edge->first;
2039 AF_Point point = seg->first;
2044 if ( dim == AF_DIMENSION_HORZ )
2046 point->x = edge->pos;
2047 point->flags |= AF_FLAG_TOUCH_X;
2051 point->y = edge->pos;
2052 point->flags |= AF_FLAG_TOUCH_Y;
2055 if ( point == seg->last )
2058 point = point->next;
2061 seg = seg->edge_next;
2063 } while ( seg != edge->first );
2067 FT_Pos delta = edge->pos - edge->opos;
2072 AF_Point point = seg->first;
2077 if ( dim == AF_DIMENSION_HORZ )
2080 point->flags |= AF_FLAG_TOUCH_X;
2085 point->flags |= AF_FLAG_TOUCH_Y;
2088 if ( point == seg->last )
2091 point = point->next;
2094 seg = seg->edge_next;
2096 } while ( seg != edge->first );
2102 FT_LOCAL_DEF( FT_Error )
2103 af_cjk_hints_apply( AF_GlyphHints hints,
2104 FT_Outline* outline,
2105 AF_CJKMetrics metrics )
2110 FT_UNUSED( metrics );
2113 error = af_glyph_hints_reload( hints, outline );
2117 /* analyze glyph outline */
2118 if ( AF_HINTS_DO_HORIZONTAL( hints ) )
2120 error = af_cjk_hints_detect_features( hints, AF_DIMENSION_HORZ );
2124 af_cjk_hints_compute_blue_edges( hints, metrics, AF_DIMENSION_HORZ );
2127 if ( AF_HINTS_DO_VERTICAL( hints ) )
2129 error = af_cjk_hints_detect_features( hints, AF_DIMENSION_VERT );
2133 af_cjk_hints_compute_blue_edges( hints, metrics, AF_DIMENSION_VERT );
2136 /* grid-fit the outline */
2137 for ( dim = 0; dim < AF_DIMENSION_MAX; dim++ )
2139 if ( ( dim == AF_DIMENSION_HORZ && AF_HINTS_DO_HORIZONTAL( hints ) ) ||
2140 ( dim == AF_DIMENSION_VERT && AF_HINTS_DO_VERTICAL( hints ) ) )
2143 #ifdef AF_CONFIG_OPTION_USE_WARPER
2144 if ( dim == AF_DIMENSION_HORZ &&
2145 metrics->root.scaler.render_mode == FT_RENDER_MODE_NORMAL )
2147 AF_WarperRec warper;
2152 af_warper_compute( &warper, hints, (AF_Dimension)dim,
2154 af_glyph_hints_scale_dim( hints, (AF_Dimension)dim,
2158 #endif /* AF_CONFIG_OPTION_USE_WARPER */
2160 af_cjk_hint_edges( hints, (AF_Dimension)dim );
2161 af_cjk_align_edge_points( hints, (AF_Dimension)dim );
2162 af_glyph_hints_align_strong_points( hints, (AF_Dimension)dim );
2163 af_glyph_hints_align_weak_points( hints, (AF_Dimension)dim );
2168 af_glyph_hints_dump_points( hints );
2169 af_glyph_hints_dump_segments( hints );
2170 af_glyph_hints_dump_edges( hints );
2173 af_glyph_hints_save( hints, outline );
2180 /*************************************************************************/
2181 /*************************************************************************/
2183 /***** C J K S C R I P T C L A S S *****/
2185 /*************************************************************************/
2186 /*************************************************************************/
2189 /* this corresponds to Unicode 6.0 */
2191 static const AF_Script_UniRangeRec af_cjk_uniranges[] =
2193 AF_UNIRANGE_REC( 0x2E80UL, 0x2EFFUL ), /* CJK Radicals Supplement */
2194 AF_UNIRANGE_REC( 0x2F00UL, 0x2FDFUL ), /* Kangxi Radicals */
2195 AF_UNIRANGE_REC( 0x2FF0UL, 0x2FFFUL ), /* Ideographic Description Characters */
2196 AF_UNIRANGE_REC( 0x3000UL, 0x303FUL ), /* CJK Symbols and Punctuation */
2197 AF_UNIRANGE_REC( 0x3040UL, 0x309FUL ), /* Hiragana */
2198 AF_UNIRANGE_REC( 0x30A0UL, 0x30FFUL ), /* Katakana */
2199 AF_UNIRANGE_REC( 0x3100UL, 0x312FUL ), /* Bopomofo */
2200 AF_UNIRANGE_REC( 0x3130UL, 0x318FUL ), /* Hangul Compatibility Jamo */
2201 AF_UNIRANGE_REC( 0x3190UL, 0x319FUL ), /* Kanbun */
2202 AF_UNIRANGE_REC( 0x31A0UL, 0x31BFUL ), /* Bopomofo Extended */
2203 AF_UNIRANGE_REC( 0x31C0UL, 0x31EFUL ), /* CJK Strokes */
2204 AF_UNIRANGE_REC( 0x31F0UL, 0x31FFUL ), /* Katakana Phonetic Extensions */
2205 AF_UNIRANGE_REC( 0x3200UL, 0x32FFUL ), /* Enclosed CJK Letters and Months */
2206 AF_UNIRANGE_REC( 0x3300UL, 0x33FFUL ), /* CJK Compatibility */
2207 AF_UNIRANGE_REC( 0x3400UL, 0x4DBFUL ), /* CJK Unified Ideographs Extension A */
2208 AF_UNIRANGE_REC( 0x4DC0UL, 0x4DFFUL ), /* Yijing Hexagram Symbols */
2209 AF_UNIRANGE_REC( 0x4E00UL, 0x9FFFUL ), /* CJK Unified Ideographs */
2210 AF_UNIRANGE_REC( 0xA960UL, 0xA97FUL ), /* Hangul Jamo Extended-A */
2211 AF_UNIRANGE_REC( 0xAC00UL, 0xD7AFUL ), /* Hangul Syllables */
2212 AF_UNIRANGE_REC( 0xD7B0UL, 0xD7FFUL ), /* Hangul Jamo Extended-B */
2213 AF_UNIRANGE_REC( 0xF900UL, 0xFAFFUL ), /* CJK Compatibility Ideographs */
2214 AF_UNIRANGE_REC( 0xFE10UL, 0xFE1FUL ), /* Vertical forms */
2215 AF_UNIRANGE_REC( 0xFE30UL, 0xFE4FUL ), /* CJK Compatibility Forms */
2216 AF_UNIRANGE_REC( 0xFF00UL, 0xFFEFUL ), /* Halfwidth and Fullwidth Forms */
2217 AF_UNIRANGE_REC( 0x1B000UL, 0x1B0FFUL ), /* Kana Supplement */
2218 AF_UNIRANGE_REC( 0x1D300UL, 0x1D35FUL ), /* Tai Xuan Hing Symbols */
2219 AF_UNIRANGE_REC( 0x1F200UL, 0x1F2FFUL ), /* Enclosed Ideographic Supplement */
2220 AF_UNIRANGE_REC( 0x20000UL, 0x2A6DFUL ), /* CJK Unified Ideographs Extension B */
2221 AF_UNIRANGE_REC( 0x2A700UL, 0x2B73FUL ), /* CJK Unified Ideographs Extension C */
2222 AF_UNIRANGE_REC( 0x2B740UL, 0x2B81FUL ), /* CJK Unified Ideographs Extension D */
2223 AF_UNIRANGE_REC( 0x2F800UL, 0x2FA1FUL ), /* CJK Compatibility Ideographs Supplement */
2224 AF_UNIRANGE_REC( 0UL, 0UL )
2228 AF_DEFINE_SCRIPT_CLASS( af_cjk_script_class,
2232 sizeof ( AF_CJKMetricsRec ),
2234 (AF_Script_InitMetricsFunc) af_cjk_metrics_init,
2235 (AF_Script_ScaleMetricsFunc)af_cjk_metrics_scale,
2236 (AF_Script_DoneMetricsFunc) NULL,
2238 (AF_Script_InitHintsFunc) af_cjk_hints_init,
2239 (AF_Script_ApplyHintsFunc) af_cjk_hints_apply
2242 #else /* !AF_CONFIG_OPTION_CJK */
2244 static const AF_Script_UniRangeRec af_cjk_uniranges[] =
2246 AF_UNIRANGE_REC( 0UL, 0UL )
2250 AF_DEFINE_SCRIPT_CLASS( af_cjk_script_class,
2254 sizeof ( AF_CJKMetricsRec ),
2256 (AF_Script_InitMetricsFunc) NULL,
2257 (AF_Script_ScaleMetricsFunc)NULL,
2258 (AF_Script_DoneMetricsFunc) NULL,
2260 (AF_Script_InitHintsFunc) NULL,
2261 (AF_Script_ApplyHintsFunc) NULL
2264 #endif /* !AF_CONFIG_OPTION_CJK */