1 /***************************************************************************/
5 /* Auto-fitter hinting routines for latin script (body). */
7 /* Copyright 2003-2011 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 /***************************************************************************/
20 #include FT_ADVANCES_H
21 #include FT_INTERNAL_DEBUG_H
27 #ifdef AF_CONFIG_OPTION_USE_WARPER
32 /*************************************************************************/
34 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
35 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
36 /* messages during execution. */
39 #define FT_COMPONENT trace_aflatin
42 /*************************************************************************/
43 /*************************************************************************/
45 /***** L A T I N G L O B A L M E T R I C S *****/
47 /*************************************************************************/
48 /*************************************************************************/
51 /* Find segments and links, compute all stem widths, and initialize */
52 /* standard width and height for the glyph with given charcode. */
55 af_latin_metrics_init_widths( AF_LatinMetrics metrics,
59 /* scan the array of segments in each direction */
60 AF_GlyphHintsRec hints[1];
63 af_glyph_hints_init( hints, face->memory );
65 metrics->axis[AF_DIMENSION_HORZ].width_count = 0;
66 metrics->axis[AF_DIMENSION_VERT].width_count = 0;
72 AF_LatinMetricsRec dummy[1];
73 AF_Scaler scaler = &dummy->root.scaler;
76 glyph_index = FT_Get_Char_Index( face, charcode );
77 if ( glyph_index == 0 )
80 error = FT_Load_Glyph( face, glyph_index, FT_LOAD_NO_SCALE );
81 if ( error || face->glyph->outline.n_points <= 0 )
86 dummy->units_per_em = metrics->units_per_em;
88 scaler->x_scale = 0x10000L;
89 scaler->y_scale = 0x10000L;
94 scaler->render_mode = FT_RENDER_MODE_NORMAL;
97 af_glyph_hints_rescale( hints, (AF_ScriptMetrics)dummy );
99 error = af_glyph_hints_reload( hints, &face->glyph->outline );
103 for ( dim = 0; dim < AF_DIMENSION_MAX; dim++ )
105 AF_LatinAxis axis = &metrics->axis[dim];
106 AF_AxisHints axhints = &hints->axis[dim];
107 AF_Segment seg, limit, link;
108 FT_UInt num_widths = 0;
111 error = af_latin_hints_compute_segments( hints,
116 af_latin_hints_link_segments( hints,
119 seg = axhints->segments;
120 limit = seg + axhints->num_segments;
122 for ( ; seg < limit; seg++ )
126 /* we only consider stem segments there! */
127 if ( link && link->link == seg && link > seg )
132 dist = seg->pos - link->pos;
136 if ( num_widths < AF_LATIN_MAX_WIDTHS )
137 axis->widths[num_widths++].org = dist;
141 af_sort_widths( num_widths, axis->widths );
142 axis->width_count = num_widths;
146 for ( dim = 0; dim < AF_DIMENSION_MAX; dim++ )
148 AF_LatinAxis axis = &metrics->axis[dim];
152 stdw = ( axis->width_count > 0 )
153 ? axis->widths[0].org
154 : AF_LATIN_CONSTANT( metrics, 50 );
156 /* let's try 20% of the smallest width */
157 axis->edge_distance_threshold = stdw / 5;
158 axis->standard_width = stdw;
159 axis->extra_light = 0;
163 af_glyph_hints_done( hints );
168 #define AF_LATIN_MAX_TEST_CHARACTERS 12
171 static const char af_latin_blue_chars[AF_LATIN_MAX_BLUES]
172 [AF_LATIN_MAX_TEST_CHARACTERS + 1] =
183 /* Find all blue zones. Flat segments give the reference points, */
184 /* round segments the overshoot positions. */
187 af_latin_metrics_init_blues( AF_LatinMetrics metrics,
190 FT_Pos flats [AF_LATIN_MAX_TEST_CHARACTERS];
191 FT_Pos rounds[AF_LATIN_MAX_TEST_CHARACTERS];
197 AF_LatinAxis axis = &metrics->axis[AF_DIMENSION_VERT];
198 FT_GlyphSlot glyph = face->glyph;
201 /* we compute the blues simply by loading each character from the */
202 /* `af_latin_blue_chars[blues]' string, then finding its top-most or */
203 /* bottom-most points (depending on `AF_IS_TOP_BLUE') */
205 FT_TRACE5(( "blue zones computation\n" ));
206 FT_TRACE5(( "------------------------------------------------\n" ));
208 for ( bb = 0; bb < AF_LATIN_BLUE_MAX; bb++ )
210 const char* p = af_latin_blue_chars[bb];
211 const char* limit = p + AF_LATIN_MAX_TEST_CHARACTERS;
216 FT_TRACE5(( "blue %3d: ", bb ));
221 for ( ; p < limit && *p; p++ )
224 FT_Pos best_y; /* same as points.y */
225 FT_Int best_point, best_first, best_last;
230 FT_TRACE5(( "'%c'", *p ));
232 /* load the character in the face -- skip unknown or empty ones */
233 glyph_index = FT_Get_Char_Index( face, (FT_UInt)*p );
234 if ( glyph_index == 0 )
237 error = FT_Load_Glyph( face, glyph_index, FT_LOAD_NO_SCALE );
238 if ( error || glyph->outline.n_points <= 0 )
241 /* now compute min or max point indices and coordinates */
242 points = glyph->outline.points;
244 best_y = 0; /* make compiler happy */
245 best_first = 0; /* ditto */
246 best_last = 0; /* ditto */
255 nn < glyph->outline.n_contours;
256 first = last + 1, nn++ )
258 FT_Int old_best_point = best_point;
262 last = glyph->outline.contours[nn];
264 /* Avoid single-point contours since they are never rasterized. */
265 /* In some fonts, they correspond to mark attachment points */
266 /* which are way outside of the glyph's real outline. */
270 if ( AF_LATIN_IS_TOP_BLUE( bb ) )
272 for ( pp = first; pp <= last; pp++ )
273 if ( best_point < 0 || points[pp].y > best_y )
276 best_y = points[pp].y;
281 for ( pp = first; pp <= last; pp++ )
282 if ( best_point < 0 || points[pp].y < best_y )
285 best_y = points[pp].y;
289 if ( best_point != old_best_point )
295 FT_TRACE5(( "%5d", best_y ));
298 /* now check whether the point belongs to a straight or round */
299 /* segment; we first need to find in which contour the extremum */
300 /* lies, then inspect its previous and next points */
301 if ( best_point >= 0 )
307 /* now look for the previous and next points that are not on the */
308 /* same Y coordinate. Threshold the `closeness'... */
314 if ( prev > best_first )
319 dist = points[prev].y - best_y;
320 if ( dist < -5 || dist > 5 )
323 } while ( prev != best_point );
327 if ( next < best_last )
332 dist = points[next].y - best_y;
333 if ( dist < -5 || dist > 5 )
336 } while ( next != best_point );
338 /* now set the `round' flag depending on the segment's kind */
340 FT_CURVE_TAG( glyph->outline.tags[prev] ) != FT_CURVE_TAG_ON ||
341 FT_CURVE_TAG( glyph->outline.tags[next] ) != FT_CURVE_TAG_ON );
343 FT_TRACE5(( "%c ", round ? 'r' : 'f' ));
347 rounds[num_rounds++] = best_y;
349 flats[num_flats++] = best_y;
354 if ( num_flats == 0 && num_rounds == 0 )
357 * we couldn't find a single glyph to compute this blue zone,
358 * we will simply ignore it then
360 FT_TRACE5(( "empty\n" ));
364 /* we have computed the contents of the `rounds' and `flats' tables, */
365 /* now determine the reference and overshoot position of the blue -- */
366 /* we simply take the median value after a simple sort */
367 af_sort_pos( num_rounds, rounds );
368 af_sort_pos( num_flats, flats );
370 blue = &axis->blues[axis->blue_count];
371 blue_ref = &blue->ref.org;
372 blue_shoot = &blue->shoot.org;
376 if ( num_flats == 0 )
379 *blue_shoot = rounds[num_rounds / 2];
381 else if ( num_rounds == 0 )
384 *blue_shoot = flats[num_flats / 2];
388 *blue_ref = flats[num_flats / 2];
389 *blue_shoot = rounds[num_rounds / 2];
392 /* there are sometimes problems: if the overshoot position of top */
393 /* zones is under its reference position, or the opposite for bottom */
394 /* zones. We must thus check everything there and correct the errors */
395 if ( *blue_shoot != *blue_ref )
397 FT_Pos ref = *blue_ref;
398 FT_Pos shoot = *blue_shoot;
399 FT_Bool over_ref = FT_BOOL( shoot > ref );
402 if ( AF_LATIN_IS_TOP_BLUE( bb ) ^ over_ref )
404 *blue_shoot = ( shoot + ref ) / 2;
408 if ( AF_LATIN_IS_TOP_BLUE( bb ) )
409 blue->flags |= AF_LATIN_BLUE_TOP;
412 * The following flag is used later to adjust the y and x scales
413 * in order to optimize the pixel grid alignment of the top of small
416 if ( bb == AF_LATIN_BLUE_SMALL_TOP )
417 blue->flags |= AF_LATIN_BLUE_ADJUSTMENT;
419 FT_TRACE5(( "-- ref = %ld, shoot = %ld\n", *blue_ref, *blue_shoot ));
428 /* Check whether all ASCII digits have the same advance width. */
431 af_latin_metrics_check_digits( AF_LatinMetrics metrics,
435 FT_Bool started = 0, same_width = 1;
436 FT_Fixed advance, old_advance = 0;
439 /* digit `0' is 0x30 in all supported charmaps */
440 for ( i = 0x30; i <= 0x39; i++ )
445 glyph_index = FT_Get_Char_Index( face, i );
446 if ( glyph_index == 0 )
449 if ( FT_Get_Advance( face, glyph_index,
452 FT_LOAD_IGNORE_TRANSFORM,
458 if ( advance != old_advance )
466 old_advance = advance;
471 metrics->root.digits_have_same_width = same_width;
475 /* Initialize global metrics. */
477 FT_LOCAL_DEF( FT_Error )
478 af_latin_metrics_init( AF_LatinMetrics metrics,
481 FT_Error error = AF_Err_Ok;
482 FT_CharMap oldmap = face->charmap;
485 static const FT_Encoding latin_encodings[] =
488 FT_ENCODING_APPLE_ROMAN,
489 FT_ENCODING_ADOBE_STANDARD,
490 FT_ENCODING_ADOBE_LATIN_1,
492 FT_ENCODING_NONE /* end of list */
496 metrics->units_per_em = face->units_per_EM;
498 /* do we have a latin charmap in there? */
499 for ( ee = 0; latin_encodings[ee] != FT_ENCODING_NONE; ee++ )
501 error = FT_Select_Charmap( face, latin_encodings[ee] );
508 /* For now, compute the standard width and height from the `o'. */
509 af_latin_metrics_init_widths( metrics, face, 'o' );
510 af_latin_metrics_init_blues( metrics, face );
511 af_latin_metrics_check_digits( metrics, face );
514 FT_Set_Charmap( face, oldmap );
519 /* Adjust scaling value, then scale and shift widths */
520 /* and blue zones (if applicable) for given dimension. */
523 af_latin_metrics_scale_dim( AF_LatinMetrics metrics,
533 if ( dim == AF_DIMENSION_HORZ )
535 scale = scaler->x_scale;
536 delta = scaler->x_delta;
540 scale = scaler->y_scale;
541 delta = scaler->y_delta;
544 axis = &metrics->axis[dim];
546 if ( axis->org_scale == scale && axis->org_delta == delta )
549 axis->org_scale = scale;
550 axis->org_delta = delta;
553 * correct X and Y scale to optimize the alignment of the top of small
554 * letters to the pixel grid
557 AF_LatinAxis Axis = &metrics->axis[AF_DIMENSION_VERT];
558 AF_LatinBlue blue = NULL;
561 for ( nn = 0; nn < Axis->blue_count; nn++ )
563 if ( Axis->blues[nn].flags & AF_LATIN_BLUE_ADJUSTMENT )
565 blue = &Axis->blues[nn];
572 FT_Pos scaled = FT_MulFix( blue->shoot.org, scaler->y_scale );
573 FT_Pos fitted = ( scaled + 40 ) & ~63;
576 if ( scaled != fitted )
579 if ( dim == AF_DIMENSION_HORZ )
581 if ( fitted < scaled )
582 scale -= scale / 50; /* scale *= 0.98 */
586 if ( dim == AF_DIMENSION_VERT )
587 scale = FT_MulDiv( scale, fitted, scaled );
595 if ( dim == AF_DIMENSION_HORZ )
597 metrics->root.scaler.x_scale = scale;
598 metrics->root.scaler.x_delta = delta;
602 metrics->root.scaler.y_scale = scale;
603 metrics->root.scaler.y_delta = delta;
606 /* scale the widths */
607 for ( nn = 0; nn < axis->width_count; nn++ )
609 AF_Width width = axis->widths + nn;
612 width->cur = FT_MulFix( width->org, scale );
613 width->fit = width->cur;
616 /* an extra-light axis corresponds to a standard width that is */
617 /* smaller than 5/8 pixels */
619 (FT_Bool)( FT_MulFix( axis->standard_width, scale ) < 32 + 8 );
621 if ( dim == AF_DIMENSION_VERT )
623 /* scale the blue zones */
624 for ( nn = 0; nn < axis->blue_count; nn++ )
626 AF_LatinBlue blue = &axis->blues[nn];
630 blue->ref.cur = FT_MulFix( blue->ref.org, scale ) + delta;
631 blue->ref.fit = blue->ref.cur;
632 blue->shoot.cur = FT_MulFix( blue->shoot.org, scale ) + delta;
633 blue->shoot.fit = blue->shoot.cur;
634 blue->flags &= ~AF_LATIN_BLUE_ACTIVE;
636 /* a blue zone is only active if it is less than 3/4 pixels tall */
637 dist = FT_MulFix( blue->ref.org - blue->shoot.org, scale );
638 if ( dist <= 48 && dist >= -48 )
646 /* use discrete values for blue zone widths */
650 /* generic, original code */
651 delta1 = blue->shoot.org - blue->ref.org;
656 delta2 = FT_MulFix( delta2, scale );
660 else if ( delta2 < 64 )
661 delta2 = 32 + ( ( ( delta2 - 32 ) + 16 ) & ~31 );
663 delta2 = FT_PIX_ROUND( delta2 );
668 blue->ref.fit = FT_PIX_ROUND( blue->ref.cur );
669 blue->shoot.fit = blue->ref.fit + delta2;
673 /* simplified version due to abs(dist) <= 48 */
680 else if ( delta < 48 )
688 blue->ref.fit = FT_PIX_ROUND( blue->ref.cur );
689 blue->shoot.fit = blue->ref.fit - delta2;
693 blue->flags |= AF_LATIN_BLUE_ACTIVE;
700 /* Scale global values in both directions. */
703 af_latin_metrics_scale( AF_LatinMetrics metrics,
706 metrics->root.scaler.render_mode = scaler->render_mode;
707 metrics->root.scaler.face = scaler->face;
709 af_latin_metrics_scale_dim( metrics, scaler, AF_DIMENSION_HORZ );
710 af_latin_metrics_scale_dim( metrics, scaler, AF_DIMENSION_VERT );
714 /*************************************************************************/
715 /*************************************************************************/
717 /***** L A T I N G L Y P H A N A L Y S I S *****/
719 /*************************************************************************/
720 /*************************************************************************/
723 /* Walk over all contours and compute its segments. */
725 FT_LOCAL_DEF( FT_Error )
726 af_latin_hints_compute_segments( AF_GlyphHints hints,
729 AF_AxisHints axis = &hints->axis[dim];
730 FT_Memory memory = hints->memory;
731 FT_Error error = AF_Err_Ok;
732 AF_Segment segment = NULL;
734 AF_Point* contour = hints->contours;
735 AF_Point* contour_limit = contour + hints->num_contours;
736 AF_Direction major_dir, segment_dir;
741 seg0.flags = AF_EDGE_NORMAL;
743 major_dir = (AF_Direction)FT_ABS( axis->major_dir );
744 segment_dir = major_dir;
746 axis->num_segments = 0;
748 /* set up (u,v) in each point */
749 if ( dim == AF_DIMENSION_HORZ )
751 AF_Point point = hints->points;
752 AF_Point limit = point + hints->num_points;
755 for ( ; point < limit; point++ )
757 point->u = point->fx;
758 point->v = point->fy;
763 AF_Point point = hints->points;
764 AF_Point limit = point + hints->num_points;
767 for ( ; point < limit; point++ )
769 point->u = point->fy;
770 point->v = point->fx;
774 /* do each contour separately */
775 for ( ; contour < contour_limit; contour++ )
777 AF_Point point = contour[0];
778 AF_Point last = point->prev;
780 FT_Pos min_pos = 32000; /* minimum segment pos != min_coord */
781 FT_Pos max_pos = -32000; /* maximum segment pos != max_coord */
785 if ( point == last ) /* skip singletons -- just in case */
788 if ( FT_ABS( last->out_dir ) == major_dir &&
789 FT_ABS( point->out_dir ) == major_dir )
791 /* we are already on an edge, try to locate its start */
797 if ( FT_ABS( point->out_dir ) != major_dir )
823 if ( point->out_dir != segment_dir || point == last )
825 /* we are just leaving an edge; record a new segment! */
826 segment->last = point;
827 segment->pos = (FT_Short)( ( min_pos + max_pos ) >> 1 );
829 /* a segment is round if either its first or last point */
830 /* is a control point */
831 if ( ( segment->first->flags | point->flags ) &
833 segment->flags |= AF_EDGE_ROUND;
835 /* compute segment size */
836 min_pos = max_pos = point->v;
838 v = segment->first->v;
844 segment->min_coord = (FT_Short)min_pos;
845 segment->max_coord = (FT_Short)max_pos;
846 segment->height = (FT_Short)( segment->max_coord -
847 segment->min_coord );
855 /* now exit if we are at the start/end point */
863 if ( !on_edge && FT_ABS( point->out_dir ) == major_dir )
865 /* this is the start of a new segment! */
866 segment_dir = (AF_Direction)point->out_dir;
868 /* clear all segment fields */
869 error = af_axis_hints_new_segment( axis, memory, &segment );
874 segment->dir = (FT_Char)segment_dir;
875 min_pos = max_pos = point->u;
876 segment->first = point;
877 segment->last = point;
887 /* now slightly increase the height of segments when this makes */
888 /* sense -- this is used to better detect and ignore serifs */
890 AF_Segment segments = axis->segments;
891 AF_Segment segments_end = segments + axis->num_segments;
894 for ( segment = segments; segment < segments_end; segment++ )
896 AF_Point first = segment->first;
897 AF_Point last = segment->last;
898 FT_Pos first_v = first->v;
899 FT_Pos last_v = last->v;
905 if ( first_v < last_v )
911 if ( p->v < first_v )
912 segment->height = (FT_Short)( segment->height +
913 ( ( first_v - p->v ) >> 1 ) );
917 segment->height = (FT_Short)( segment->height +
918 ( ( p->v - last_v ) >> 1 ) );
926 if ( p->v > first_v )
927 segment->height = (FT_Short)( segment->height +
928 ( ( p->v - first_v ) >> 1 ) );
932 segment->height = (FT_Short)( segment->height +
933 ( ( last_v - p->v ) >> 1 ) );
943 /* Link segments to form stems and serifs. */
946 af_latin_hints_link_segments( AF_GlyphHints hints,
949 AF_AxisHints axis = &hints->axis[dim];
950 AF_Segment segments = axis->segments;
951 AF_Segment segment_limit = segments + axis->num_segments;
952 FT_Pos len_threshold, len_score;
953 AF_Segment seg1, seg2;
956 len_threshold = AF_LATIN_CONSTANT( hints->metrics, 8 );
957 if ( len_threshold == 0 )
960 len_score = AF_LATIN_CONSTANT( hints->metrics, 6000 );
962 /* now compare each segment to the others */
963 for ( seg1 = segments; seg1 < segment_limit; seg1++ )
965 /* the fake segments are introduced to hint the metrics -- */
966 /* we must never link them to anything */
967 if ( seg1->dir != axis->major_dir || seg1->first == seg1->last )
970 /* search for stems having opposite directions, */
971 /* with seg1 to the `left' of seg2 */
972 for ( seg2 = segments; seg2 < segment_limit; seg2++ )
974 FT_Pos pos1 = seg1->pos;
975 FT_Pos pos2 = seg2->pos;
978 if ( seg1->dir + seg2->dir == 0 && pos2 > pos1 )
980 /* compute distance between the two segments */
981 FT_Pos dist = pos2 - pos1;
982 FT_Pos min = seg1->min_coord;
983 FT_Pos max = seg1->max_coord;
987 if ( min < seg2->min_coord )
988 min = seg2->min_coord;
990 if ( max > seg2->max_coord )
991 max = seg2->max_coord;
993 /* compute maximum coordinate difference of the two segments */
995 if ( len >= len_threshold )
997 /* small coordinate differences cause a higher score, and */
998 /* segments with a greater distance cause a higher score also */
999 score = dist + len_score / len;
1001 /* and we search for the smallest score */
1002 /* of the sum of the two values */
1003 if ( score < seg1->score )
1005 seg1->score = score;
1009 if ( score < seg2->score )
1011 seg2->score = score;
1019 /* now compute the `serif' segments, cf. explanations in `afhints.h' */
1020 for ( seg1 = segments; seg1 < segment_limit; seg1++ )
1026 if ( seg2->link != seg1 )
1029 seg1->serif = seg2->link;
1036 /* Link segments to edges, using feature analysis for selection. */
1038 FT_LOCAL_DEF( FT_Error )
1039 af_latin_hints_compute_edges( AF_GlyphHints hints,
1042 AF_AxisHints axis = &hints->axis[dim];
1043 FT_Error error = AF_Err_Ok;
1044 FT_Memory memory = hints->memory;
1045 AF_LatinAxis laxis = &((AF_LatinMetrics)hints->metrics)->axis[dim];
1047 AF_Segment segments = axis->segments;
1048 AF_Segment segment_limit = segments + axis->num_segments;
1052 AF_Direction up_dir;
1055 FT_Pos edge_distance_threshold;
1056 FT_Pos segment_length_threshold;
1059 axis->num_edges = 0;
1061 scale = ( dim == AF_DIMENSION_HORZ ) ? hints->x_scale
1065 up_dir = ( dim == AF_DIMENSION_HORZ ) ? AF_DIR_UP
1070 * We ignore all segments that are less than 1 pixel in length
1071 * to avoid many problems with serif fonts. We compute the
1072 * corresponding threshold in font units.
1074 if ( dim == AF_DIMENSION_HORZ )
1075 segment_length_threshold = FT_DivFix( 64, hints->y_scale );
1077 segment_length_threshold = 0;
1079 /*********************************************************************/
1081 /* We begin by generating a sorted table of edges for the current */
1082 /* direction. To do so, we simply scan each segment and try to find */
1083 /* an edge in our table that corresponds to its position. */
1085 /* If no edge is found, we create and insert a new edge in the */
1086 /* sorted table. Otherwise, we simply add the segment to the edge's */
1087 /* list which gets processed in the second step to compute the */
1088 /* edge's properties. */
1090 /* Note that the table of edges is sorted along the segment/edge */
1093 /*********************************************************************/
1095 /* assure that edge distance threshold is at most 0.25px */
1096 edge_distance_threshold = FT_MulFix( laxis->edge_distance_threshold,
1098 if ( edge_distance_threshold > 64 / 4 )
1099 edge_distance_threshold = 64 / 4;
1101 edge_distance_threshold = FT_DivFix( edge_distance_threshold,
1104 for ( seg = segments; seg < segment_limit; seg++ )
1106 AF_Edge found = NULL;
1110 if ( seg->height < segment_length_threshold )
1113 /* A special case for serif edges: If they are smaller than */
1114 /* 1.5 pixels we ignore them. */
1116 2 * seg->height < 3 * segment_length_threshold )
1119 /* look for an edge corresponding to the segment */
1120 for ( ee = 0; ee < axis->num_edges; ee++ )
1122 AF_Edge edge = axis->edges + ee;
1126 dist = seg->pos - edge->fpos;
1130 if ( dist < edge_distance_threshold && edge->dir == seg->dir )
1142 /* insert a new edge in the list and */
1143 /* sort according to the position */
1144 error = af_axis_hints_new_edge( axis, seg->pos,
1145 (AF_Direction)seg->dir,
1150 /* add the segment to the new edge's list */
1155 edge->dir = seg->dir;
1156 edge->fpos = seg->pos;
1157 edge->opos = FT_MulFix( seg->pos, scale );
1158 edge->pos = edge->opos;
1159 seg->edge_next = seg;
1163 /* if an edge was found, simply add the segment to the edge's */
1165 seg->edge_next = found->first;
1166 found->last->edge_next = seg;
1172 /*********************************************************************/
1174 /* Good, we will now compute each edge's properties according to */
1175 /* the segments found on its position. Basically, these are */
1177 /* - the edge's main direction */
1178 /* - stem edge, serif edge or both (which defaults to stem then) */
1179 /* - rounded edge, straight or both (which defaults to straight) */
1180 /* - link for edge */
1182 /*********************************************************************/
1184 /* first of all, set the `edge' field in each segment -- this is */
1185 /* required in order to compute edge links */
1188 * Note that removing this loop and setting the `edge' field of each
1189 * segment directly in the code above slows down execution speed for
1190 * some reasons on platforms like the Sun.
1193 AF_Edge edges = axis->edges;
1194 AF_Edge edge_limit = edges + axis->num_edges;
1198 for ( edge = edges; edge < edge_limit; edge++ )
1205 seg = seg->edge_next;
1207 } while ( seg != edge->first );
1210 /* now compute each edge properties */
1211 for ( edge = edges; edge < edge_limit; edge++ )
1213 FT_Int is_round = 0; /* does it contain round segments? */
1214 FT_Int is_straight = 0; /* does it contain straight segments? */
1216 FT_Pos ups = 0; /* number of upwards segments */
1217 FT_Pos downs = 0; /* number of downwards segments */
1228 /* check for roundness of segment */
1229 if ( seg->flags & AF_EDGE_ROUND )
1235 /* check for segment direction */
1236 if ( seg->dir == up_dir )
1237 ups += seg->max_coord - seg->min_coord;
1239 downs += seg->max_coord - seg->min_coord;
1242 /* check for links -- if seg->serif is set, then seg->link must */
1244 is_serif = (FT_Bool)( seg->serif &&
1246 seg->serif->edge != edge );
1248 if ( ( seg->link && seg->link->edge != NULL ) || is_serif )
1260 edge2 = edge->serif;
1269 edge_delta = edge->fpos - edge2->fpos;
1270 if ( edge_delta < 0 )
1271 edge_delta = -edge_delta;
1273 seg_delta = seg->pos - seg2->pos;
1274 if ( seg_delta < 0 )
1275 seg_delta = -seg_delta;
1277 if ( seg_delta < edge_delta )
1285 edge->serif = edge2;
1286 edge2->flags |= AF_EDGE_SERIF;
1292 seg = seg->edge_next;
1294 } while ( seg != edge->first );
1296 /* set the round/straight flags */
1297 edge->flags = AF_EDGE_NORMAL;
1299 if ( is_round > 0 && is_round >= is_straight )
1300 edge->flags |= AF_EDGE_ROUND;
1303 /* set the edge's main direction */
1304 edge->dir = AF_DIR_NONE;
1307 edge->dir = (FT_Char)up_dir;
1309 else if ( ups < downs )
1310 edge->dir = (FT_Char)-up_dir;
1312 else if ( ups == downs )
1313 edge->dir = 0; /* both up and down! */
1316 /* get rid of serifs if link is set */
1317 /* XXX: This gets rid of many unpleasant artefacts! */
1318 /* Example: the `c' in cour.pfa at size 13 */
1320 if ( edge->serif && edge->link )
1330 /* Detect segments and edges for given dimension. */
1332 FT_LOCAL_DEF( FT_Error )
1333 af_latin_hints_detect_features( AF_GlyphHints hints,
1339 error = af_latin_hints_compute_segments( hints, dim );
1342 af_latin_hints_link_segments( hints, dim );
1344 error = af_latin_hints_compute_edges( hints, dim );
1351 /* Compute all edges which lie within blue zones. */
1353 FT_LOCAL_DEF( void )
1354 af_latin_hints_compute_blue_edges( AF_GlyphHints hints,
1355 AF_LatinMetrics metrics )
1357 AF_AxisHints axis = &hints->axis[AF_DIMENSION_VERT];
1358 AF_Edge edge = axis->edges;
1359 AF_Edge edge_limit = edge + axis->num_edges;
1360 AF_LatinAxis latin = &metrics->axis[AF_DIMENSION_VERT];
1361 FT_Fixed scale = latin->scale;
1364 /* compute which blue zones are active, i.e. have their scaled */
1365 /* size < 3/4 pixels */
1367 /* for each horizontal edge search the blue zone which is closest */
1368 for ( ; edge < edge_limit; edge++ )
1371 AF_Width best_blue = NULL;
1372 FT_Pos best_dist; /* initial threshold */
1375 /* compute the initial threshold as a fraction of the EM size */
1376 /* (the value 40 is heuristic) */
1377 best_dist = FT_MulFix( metrics->units_per_em / 40, scale );
1379 /* assure a minimum distance of 0.5px */
1380 if ( best_dist > 64 / 2 )
1383 for ( bb = 0; bb < AF_LATIN_BLUE_MAX; bb++ )
1385 AF_LatinBlue blue = latin->blues + bb;
1386 FT_Bool is_top_blue, is_major_dir;
1389 /* skip inactive blue zones (i.e., those that are too large) */
1390 if ( !( blue->flags & AF_LATIN_BLUE_ACTIVE ) )
1393 /* if it is a top zone, check for right edges -- if it is a bottom */
1394 /* zone, check for left edges */
1396 /* of course, that's for TrueType */
1397 is_top_blue = (FT_Byte)( ( blue->flags & AF_LATIN_BLUE_TOP ) != 0 );
1398 is_major_dir = FT_BOOL( edge->dir == axis->major_dir );
1400 /* if it is a top zone, the edge must be against the major */
1401 /* direction; if it is a bottom zone, it must be in the major */
1403 if ( is_top_blue ^ is_major_dir )
1408 /* first of all, compare it to the reference position */
1409 dist = edge->fpos - blue->ref.org;
1413 dist = FT_MulFix( dist, scale );
1414 if ( dist < best_dist )
1417 best_blue = &blue->ref;
1420 /* now compare it to the overshoot position and check whether */
1421 /* the edge is rounded, and whether the edge is over the */
1422 /* reference position of a top zone, or under the reference */
1423 /* position of a bottom zone */
1424 if ( edge->flags & AF_EDGE_ROUND && dist != 0 )
1426 FT_Bool is_under_ref = FT_BOOL( edge->fpos < blue->ref.org );
1429 if ( is_top_blue ^ is_under_ref )
1431 dist = edge->fpos - blue->shoot.org;
1435 dist = FT_MulFix( dist, scale );
1436 if ( dist < best_dist )
1439 best_blue = &blue->shoot;
1447 edge->blue_edge = best_blue;
1452 /* Initalize hinting engine. */
1455 af_latin_hints_init( AF_GlyphHints hints,
1456 AF_LatinMetrics metrics )
1458 FT_Render_Mode mode;
1459 FT_UInt32 scaler_flags, other_flags;
1460 FT_Face face = metrics->root.scaler.face;
1463 af_glyph_hints_rescale( hints, (AF_ScriptMetrics)metrics );
1466 * correct x_scale and y_scale if needed, since they may have
1467 * been modified by `af_latin_metrics_scale_dim' above
1469 hints->x_scale = metrics->axis[AF_DIMENSION_HORZ].scale;
1470 hints->x_delta = metrics->axis[AF_DIMENSION_HORZ].delta;
1471 hints->y_scale = metrics->axis[AF_DIMENSION_VERT].scale;
1472 hints->y_delta = metrics->axis[AF_DIMENSION_VERT].delta;
1474 /* compute flags depending on render mode, etc. */
1475 mode = metrics->root.scaler.render_mode;
1477 #if 0 /* #ifdef AF_CONFIG_OPTION_USE_WARPER */
1478 if ( mode == FT_RENDER_MODE_LCD || mode == FT_RENDER_MODE_LCD_V )
1480 metrics->root.scaler.render_mode = mode = FT_RENDER_MODE_NORMAL;
1484 scaler_flags = hints->scaler_flags;
1488 * We snap the width of vertical stems for the monochrome and
1489 * horizontal LCD rendering targets only.
1491 if ( mode == FT_RENDER_MODE_MONO || mode == FT_RENDER_MODE_LCD )
1492 other_flags |= AF_LATIN_HINTS_HORZ_SNAP;
1495 * We snap the width of horizontal stems for the monochrome and
1496 * vertical LCD rendering targets only.
1498 if ( mode == FT_RENDER_MODE_MONO || mode == FT_RENDER_MODE_LCD_V )
1499 other_flags |= AF_LATIN_HINTS_VERT_SNAP;
1502 * We adjust stems to full pixels only if we don't use the `light' mode.
1504 if ( mode != FT_RENDER_MODE_LIGHT )
1505 other_flags |= AF_LATIN_HINTS_STEM_ADJUST;
1507 if ( mode == FT_RENDER_MODE_MONO )
1508 other_flags |= AF_LATIN_HINTS_MONO;
1511 * In `light' hinting mode we disable horizontal hinting completely.
1512 * We also do it if the face is italic.
1514 if ( mode == FT_RENDER_MODE_LIGHT ||
1515 ( face->style_flags & FT_STYLE_FLAG_ITALIC ) != 0 )
1516 scaler_flags |= AF_SCALER_FLAG_NO_HORIZONTAL;
1518 hints->scaler_flags = scaler_flags;
1519 hints->other_flags = other_flags;
1525 /*************************************************************************/
1526 /*************************************************************************/
1528 /***** L A T I N G L Y P H G R I D - F I T T I N G *****/
1530 /*************************************************************************/
1531 /*************************************************************************/
1533 /* Snap a given width in scaled coordinates to one of the */
1534 /* current standard widths. */
1537 af_latin_snap_width( AF_Width widths,
1542 FT_Pos best = 64 + 32 + 2;
1543 FT_Pos reference = width;
1547 for ( n = 0; n < count; n++ )
1564 scaled = FT_PIX_ROUND( reference );
1566 if ( width >= reference )
1568 if ( width < scaled + 48 )
1573 if ( width > scaled - 48 )
1581 /* Compute the snapped width of a given stem, ignoring very thin ones. */
1582 /* There is a lot of voodoo in this function; changing the hard-coded */
1583 /* parameters influence the whole hinting process. */
1586 af_latin_compute_stem_width( AF_GlyphHints hints,
1589 AF_Edge_Flags base_flags,
1590 AF_Edge_Flags stem_flags )
1592 AF_LatinMetrics metrics = (AF_LatinMetrics) hints->metrics;
1593 AF_LatinAxis axis = & metrics->axis[dim];
1594 FT_Pos dist = width;
1596 FT_Int vertical = ( dim == AF_DIMENSION_VERT );
1599 if ( !AF_LATIN_HINTS_DO_STEM_ADJUST( hints ) ||
1609 if ( ( vertical && !AF_LATIN_HINTS_DO_VERT_SNAP( hints ) ) ||
1610 ( !vertical && !AF_LATIN_HINTS_DO_HORZ_SNAP( hints ) ) )
1612 /* smooth hinting process: very lightly quantize the stem width */
1614 /* leave the widths of serifs alone */
1615 if ( ( stem_flags & AF_EDGE_SERIF ) &&
1620 else if ( base_flags & AF_EDGE_ROUND )
1625 else if ( dist < 56 )
1628 if ( axis->width_count > 0 )
1633 /* compare to standard width */
1634 delta = dist - axis->widths[0].cur;
1641 dist = axis->widths[0].cur;
1648 if ( dist < 3 * 64 )
1656 else if ( delta < 32 )
1659 else if ( delta < 54 )
1666 dist = ( dist + 32 ) & ~63;
1671 /* strong hinting process: snap the stem width to integer pixels */
1673 FT_Pos org_dist = dist;
1676 dist = af_latin_snap_width( axis->widths, axis->width_count, dist );
1680 /* in the case of vertical hinting, always round */
1681 /* the stem heights to integer pixels */
1684 dist = ( dist + 16 ) & ~63;
1690 if ( AF_LATIN_HINTS_DO_MONO( hints ) )
1692 /* monochrome horizontal hinting: snap widths to integer pixels */
1693 /* with a different threshold */
1698 dist = ( dist + 32 ) & ~63;
1702 /* for horizontal anti-aliased hinting, we adopt a more subtle */
1703 /* approach: we strengthen small stems, round stems whose size */
1704 /* is between 1 and 2 pixels to an integer, otherwise nothing */
1707 dist = ( dist + 64 ) >> 1;
1709 else if ( dist < 128 )
1711 /* We only round to an integer width if the corresponding */
1712 /* distortion is less than 1/4 pixel. Otherwise this */
1713 /* makes everything worse since the diagonals, which are */
1714 /* not hinted, appear a lot bolder or thinner than the */
1715 /* vertical stems. */
1720 dist = ( dist + 22 ) & ~63;
1721 delta = dist - org_dist;
1729 dist = ( dist + 64 ) >> 1;
1733 /* round otherwise to prevent color fringes in LCD mode */
1734 dist = ( dist + 32 ) & ~63;
1747 /* Align one stem edge relative to the previous stem edge. */
1750 af_latin_align_linked_edge( AF_GlyphHints hints,
1755 FT_Pos dist = stem_edge->opos - base_edge->opos;
1757 FT_Pos fitted_width = af_latin_compute_stem_width(
1759 (AF_Edge_Flags)base_edge->flags,
1760 (AF_Edge_Flags)stem_edge->flags );
1763 stem_edge->pos = base_edge->pos + fitted_width;
1765 FT_TRACE5(( " LINK: edge %d (opos=%.2f) linked to (%.2f),"
1766 " dist was %.2f, now %.2f\n",
1767 stem_edge-hints->axis[dim].edges, stem_edge->opos / 64.0,
1768 stem_edge->pos / 64.0, dist / 64.0, fitted_width / 64.0 ));
1772 /* Shift the coordinates of the `serif' edge by the same amount */
1773 /* as the corresponding `base' edge has been moved already. */
1776 af_latin_align_serif_edge( AF_GlyphHints hints,
1782 serif->pos = base->pos + ( serif->opos - base->opos );
1786 /*************************************************************************/
1787 /*************************************************************************/
1788 /*************************************************************************/
1790 /**** E D G E H I N T I N G ****/
1792 /*************************************************************************/
1793 /*************************************************************************/
1794 /*************************************************************************/
1797 /* The main grid-fitting routine. */
1799 FT_LOCAL_DEF( void )
1800 af_latin_hint_edges( AF_GlyphHints hints,
1803 AF_AxisHints axis = &hints->axis[dim];
1804 AF_Edge edges = axis->edges;
1805 AF_Edge edge_limit = edges + axis->num_edges;
1808 AF_Edge anchor = NULL;
1809 FT_Int has_serifs = 0;
1812 FT_TRACE5(("%s edge hinting\n", dim == AF_DIMENSION_VERT ? "horizontal"
1815 /* we begin by aligning all stems relative to the blue zone */
1816 /* if needed -- that's only for horizontal edges */
1818 if ( dim == AF_DIMENSION_VERT && AF_HINTS_DO_BLUES( hints ) )
1820 for ( edge = edges; edge < edge_limit; edge++ )
1823 AF_Edge edge1, edge2; /* these edges form the stem to check */
1826 if ( edge->flags & AF_EDGE_DONE )
1829 blue = edge->blue_edge;
1836 /* flip edges if the other stem is aligned to a blue zone */
1837 else if ( edge2 && edge2->blue_edge )
1839 blue = edge2->blue_edge;
1847 FT_TRACE5(( " BLUE: edge %d (opos=%.2f) snapped to (%.2f),"
1849 edge1 - edges, edge1->opos / 64.0, blue->fit / 64.0,
1850 edge1->pos / 64.0 ));
1852 edge1->pos = blue->fit;
1853 edge1->flags |= AF_EDGE_DONE;
1855 if ( edge2 && !edge2->blue_edge )
1857 af_latin_align_linked_edge( hints, dim, edge1, edge2 );
1858 edge2->flags |= AF_EDGE_DONE;
1866 /* now we align all other stem edges, trying to maintain the */
1867 /* relative order of stems in the glyph */
1868 for ( edge = edges; edge < edge_limit; edge++ )
1873 if ( edge->flags & AF_EDGE_DONE )
1876 /* skip all non-stem edges */
1884 /* now align the stem */
1886 /* this should not happen, but it's better to be safe */
1887 if ( edge2->blue_edge )
1889 FT_TRACE5(( " ASSERTION FAILED for edge %d\n", edge2-edges ));
1891 af_latin_align_linked_edge( hints, dim, edge2, edge );
1892 edge->flags |= AF_EDGE_DONE;
1898 /* if we reach this if clause, no stem has been aligned yet */
1900 FT_Pos org_len, org_center, cur_len;
1901 FT_Pos cur_pos1, error1, error2, u_off, d_off;
1904 org_len = edge2->opos - edge->opos;
1905 cur_len = af_latin_compute_stem_width(
1906 hints, dim, org_len,
1907 (AF_Edge_Flags)edge->flags,
1908 (AF_Edge_Flags)edge2->flags );
1910 /* some voodoo to specially round edges for small stem widths; */
1911 /* the idea is to align the center of a stem, then shifting */
1912 /* the stem edges to suitable positions */
1913 if ( cur_len <= 64 )
1921 /* 1px < width < 1.5px */
1928 org_center = edge->opos + ( org_len >> 1 );
1929 cur_pos1 = FT_PIX_ROUND( org_center );
1931 error1 = org_center - ( cur_pos1 - u_off );
1935 error2 = org_center - ( cur_pos1 + d_off );
1939 if ( error1 < error2 )
1944 edge->pos = cur_pos1 - cur_len / 2;
1945 edge2->pos = edge->pos + cur_len;
1948 edge->pos = FT_PIX_ROUND( edge->opos );
1950 FT_TRACE5(( " ANCHOR: edge %d (opos=%.2f) and %d (opos=%.2f)"
1951 " snapped to (%.2f) (%.2f)\n",
1952 edge - edges, edge->opos / 64.0,
1953 edge2 - edges, edge2->opos / 64.0,
1954 edge->pos / 64.0, edge2->pos / 64.0 ));
1957 edge->flags |= AF_EDGE_DONE;
1959 af_latin_align_linked_edge( hints, dim, edge, edge2 );
1963 FT_Pos org_pos, org_len, org_center, cur_len;
1964 FT_Pos cur_pos1, cur_pos2, delta1, delta2;
1967 org_pos = anchor->pos + ( edge->opos - anchor->opos );
1968 org_len = edge2->opos - edge->opos;
1969 org_center = org_pos + ( org_len >> 1 );
1971 cur_len = af_latin_compute_stem_width(
1972 hints, dim, org_len,
1973 (AF_Edge_Flags)edge->flags,
1974 (AF_Edge_Flags)edge2->flags );
1976 if ( edge2->flags & AF_EDGE_DONE )
1978 FT_TRACE5(( " ADJUST: edge %d (pos=%.2f) moved to %.2f\n",
1979 edge - edges, edge->pos / 64.0,
1980 ( edge2->pos - cur_len ) / 64.0 ));
1982 edge->pos = edge2->pos - cur_len;
1985 else if ( cur_len < 96 )
1987 FT_Pos u_off, d_off;
1990 cur_pos1 = FT_PIX_ROUND( org_center );
2003 delta1 = org_center - ( cur_pos1 - u_off );
2007 delta2 = org_center - ( cur_pos1 + d_off );
2011 if ( delta1 < delta2 )
2016 edge->pos = cur_pos1 - cur_len / 2;
2017 edge2->pos = cur_pos1 + cur_len / 2;
2019 FT_TRACE5(( " STEM: %d (opos=%.2f) to %d (opos=%.2f)"
2020 " snapped to (%.2f) and (%.2f)\n",
2021 edge - edges, edge->opos / 64.0,
2022 edge2 - edges, edge2->opos / 64.0,
2023 edge->pos / 64.0, edge2->pos / 64.0 ));
2027 org_pos = anchor->pos + ( edge->opos - anchor->opos );
2028 org_len = edge2->opos - edge->opos;
2029 org_center = org_pos + ( org_len >> 1 );
2031 cur_len = af_latin_compute_stem_width(
2032 hints, dim, org_len,
2033 (AF_Edge_Flags)edge->flags,
2034 (AF_Edge_Flags)edge2->flags );
2036 cur_pos1 = FT_PIX_ROUND( org_pos );
2037 delta1 = cur_pos1 + ( cur_len >> 1 ) - org_center;
2041 cur_pos2 = FT_PIX_ROUND( org_pos + org_len ) - cur_len;
2042 delta2 = cur_pos2 + ( cur_len >> 1 ) - org_center;
2046 edge->pos = ( delta1 < delta2 ) ? cur_pos1 : cur_pos2;
2047 edge2->pos = edge->pos + cur_len;
2049 FT_TRACE5(( " STEM: %d (opos=%.2f) to %d (opos=%.2f)"
2050 " snapped to (%.2f) and (%.2f)\n",
2051 edge - edges, edge->opos / 64.0,
2052 edge2 - edges, edge2->opos / 64.0,
2053 edge->pos / 64.0, edge2->pos / 64.0 ));
2056 edge->flags |= AF_EDGE_DONE;
2057 edge2->flags |= AF_EDGE_DONE;
2059 if ( edge > edges && edge->pos < edge[-1].pos )
2061 FT_TRACE5(( " BOUND: %d (pos=%.2f) to (%.2f)\n",
2062 edge - edges, edge->pos / 64.0, edge[-1].pos / 64.0 ));
2063 edge->pos = edge[-1].pos;
2068 /* make sure that lowercase m's maintain their symmetry */
2070 /* In general, lowercase m's have six vertical edges if they are sans */
2071 /* serif, or twelve if they are with serifs. This implementation is */
2072 /* based on that assumption, and seems to work very well with most */
2073 /* faces. However, if for a certain face this assumption is not */
2074 /* true, the m is just rendered like before. In addition, any stem */
2075 /* correction will only be applied to symmetrical glyphs (even if the */
2076 /* glyph is not an m), so the potential for unwanted distortion is */
2077 /* relatively low. */
2079 /* We don't handle horizontal edges since we can't easily assure that */
2080 /* the third (lowest) stem aligns with the base line; it might end up */
2081 /* one pixel higher or lower. */
2083 n_edges = edge_limit - edges;
2084 if ( dim == AF_DIMENSION_HORZ && ( n_edges == 6 || n_edges == 12 ) )
2086 AF_Edge edge1, edge2, edge3;
2087 FT_Pos dist1, dist2, span, delta;
2103 dist1 = edge2->opos - edge1->opos;
2104 dist2 = edge3->opos - edge2->opos;
2106 span = dist1 - dist2;
2112 delta = edge3->pos - ( 2 * edge2->pos - edge1->pos );
2113 edge3->pos -= delta;
2115 edge3->link->pos -= delta;
2117 /* move the serifs along with the stem */
2118 if ( n_edges == 12 )
2120 ( edges + 8 )->pos -= delta;
2121 ( edges + 11 )->pos -= delta;
2124 edge3->flags |= AF_EDGE_DONE;
2126 edge3->link->flags |= AF_EDGE_DONE;
2130 if ( has_serifs || !anchor )
2133 * now hint the remaining edges (serifs and single) in order
2134 * to complete our processing
2136 for ( edge = edges; edge < edge_limit; edge++ )
2141 if ( edge->flags & AF_EDGE_DONE )
2148 delta = edge->serif->opos - edge->opos;
2153 if ( delta < 64 + 16 )
2155 af_latin_align_serif_edge( hints, edge->serif, edge );
2156 FT_TRACE5(( " SERIF: edge %d (opos=%.2f) serif to %d (opos=%.2f)"
2157 " aligned to (%.2f)\n",
2158 edge - edges, edge->opos / 64.0,
2159 edge->serif - edges, edge->serif->opos / 64.0,
2160 edge->pos / 64.0 ));
2164 edge->pos = FT_PIX_ROUND( edge->opos );
2166 FT_TRACE5(( " SERIF_ANCHOR: edge %d (opos=%.2f)"
2167 " snapped to (%.2f)\n",
2168 edge-edges, edge->opos / 64.0, edge->pos / 64.0 ));
2172 AF_Edge before, after;
2175 for ( before = edge - 1; before >= edges; before-- )
2176 if ( before->flags & AF_EDGE_DONE )
2179 for ( after = edge + 1; after < edge_limit; after++ )
2180 if ( after->flags & AF_EDGE_DONE )
2183 if ( before >= edges && before < edge &&
2184 after < edge_limit && after > edge )
2186 if ( after->opos == before->opos )
2187 edge->pos = before->pos;
2189 edge->pos = before->pos +
2190 FT_MulDiv( edge->opos - before->opos,
2191 after->pos - before->pos,
2192 after->opos - before->opos );
2194 FT_TRACE5(( " SERIF_LINK1: edge %d (opos=%.2f) snapped to (%.2f)"
2195 " from %d (opos=%.2f)\n",
2196 edge - edges, edge->opos / 64.0,
2198 before - edges, before->opos / 64.0 ));
2202 edge->pos = anchor->pos +
2203 ( ( edge->opos - anchor->opos + 16 ) & ~31 );
2205 FT_TRACE5(( " SERIF_LINK2: edge %d (opos=%.2f)"
2206 " snapped to (%.2f)\n",
2207 edge - edges, edge->opos / 64.0, edge->pos / 64.0 ));
2211 edge->flags |= AF_EDGE_DONE;
2213 if ( edge > edges && edge->pos < edge[-1].pos )
2214 edge->pos = edge[-1].pos;
2216 if ( edge + 1 < edge_limit &&
2217 edge[1].flags & AF_EDGE_DONE &&
2218 edge->pos > edge[1].pos )
2219 edge->pos = edge[1].pos;
2223 FT_TRACE5(( "\n" ));
2227 /* Apply the complete hinting algorithm to a latin glyph. */
2230 af_latin_hints_apply( AF_GlyphHints hints,
2231 FT_Outline* outline,
2232 AF_LatinMetrics metrics )
2238 error = af_glyph_hints_reload( hints, outline );
2242 /* analyze glyph outline */
2243 #ifdef AF_CONFIG_OPTION_USE_WARPER
2244 if ( metrics->root.scaler.render_mode == FT_RENDER_MODE_LIGHT ||
2245 AF_HINTS_DO_HORIZONTAL( hints ) )
2247 if ( AF_HINTS_DO_HORIZONTAL( hints ) )
2250 error = af_latin_hints_detect_features( hints, AF_DIMENSION_HORZ );
2255 if ( AF_HINTS_DO_VERTICAL( hints ) )
2257 error = af_latin_hints_detect_features( hints, AF_DIMENSION_VERT );
2261 af_latin_hints_compute_blue_edges( hints, metrics );
2264 /* grid-fit the outline */
2265 for ( dim = 0; dim < AF_DIMENSION_MAX; dim++ )
2267 #ifdef AF_CONFIG_OPTION_USE_WARPER
2268 if ( dim == AF_DIMENSION_HORZ &&
2269 metrics->root.scaler.render_mode == FT_RENDER_MODE_LIGHT )
2271 AF_WarperRec warper;
2276 af_warper_compute( &warper, hints, (AF_Dimension)dim,
2278 af_glyph_hints_scale_dim( hints, (AF_Dimension)dim,
2284 if ( ( dim == AF_DIMENSION_HORZ && AF_HINTS_DO_HORIZONTAL( hints ) ) ||
2285 ( dim == AF_DIMENSION_VERT && AF_HINTS_DO_VERTICAL( hints ) ) )
2287 af_latin_hint_edges( hints, (AF_Dimension)dim );
2288 af_glyph_hints_align_edge_points( hints, (AF_Dimension)dim );
2289 af_glyph_hints_align_strong_points( hints, (AF_Dimension)dim );
2290 af_glyph_hints_align_weak_points( hints, (AF_Dimension)dim );
2293 af_glyph_hints_save( hints, outline );
2300 /*************************************************************************/
2301 /*************************************************************************/
2303 /***** L A T I N S C R I P T C L A S S *****/
2305 /*************************************************************************/
2306 /*************************************************************************/
2309 /* XXX: this should probably fine tuned to differentiate better between */
2312 static const AF_Script_UniRangeRec af_latin_uniranges[] =
2314 AF_UNIRANGE_REC( 0x0020UL, 0x007FUL ), /* Basic Latin (no control chars) */
2315 AF_UNIRANGE_REC( 0x00A0UL, 0x00FFUL ), /* Latin-1 Supplement (no control chars) */
2316 AF_UNIRANGE_REC( 0x0100UL, 0x017FUL ), /* Latin Extended-A */
2317 AF_UNIRANGE_REC( 0x0180UL, 0x024FUL ), /* Latin Extended-B */
2318 AF_UNIRANGE_REC( 0x0250UL, 0x02AFUL ), /* IPA Extensions */
2319 AF_UNIRANGE_REC( 0x02B0UL, 0x02FFUL ), /* Spacing Modifier Letters */
2320 AF_UNIRANGE_REC( 0x0300UL, 0x036FUL ), /* Combining Diacritical Marks */
2321 AF_UNIRANGE_REC( 0x0370UL, 0x03FFUL ), /* Greek and Coptic */
2322 AF_UNIRANGE_REC( 0x0400UL, 0x04FFUL ), /* Cyrillic */
2323 AF_UNIRANGE_REC( 0x0500UL, 0x052FUL ), /* Cyrillic Supplement */
2324 AF_UNIRANGE_REC( 0x1D00UL, 0x1D7FUL ), /* Phonetic Extensions */
2325 AF_UNIRANGE_REC( 0x1D80UL, 0x1DBFUL ), /* Phonetic Extensions Supplement */
2326 AF_UNIRANGE_REC( 0x1DC0UL, 0x1DFFUL ), /* Combining Diacritical Marks Supplement */
2327 AF_UNIRANGE_REC( 0x1E00UL, 0x1EFFUL ), /* Latin Extended Additional */
2328 AF_UNIRANGE_REC( 0x1F00UL, 0x1FFFUL ), /* Greek Extended */
2329 AF_UNIRANGE_REC( 0x2000UL, 0x206FUL ), /* General Punctuation */
2330 AF_UNIRANGE_REC( 0x2070UL, 0x209FUL ), /* Superscripts and Subscripts */
2331 AF_UNIRANGE_REC( 0x20A0UL, 0x20CFUL ), /* Currency Symbols */
2332 AF_UNIRANGE_REC( 0x2150UL, 0x218FUL ), /* Number Forms */
2333 AF_UNIRANGE_REC( 0x2460UL, 0x24FFUL ), /* Enclosed Alphanumerics */
2334 AF_UNIRANGE_REC( 0x2C60UL, 0x2C7FUL ), /* Latin Extended-C */
2335 AF_UNIRANGE_REC( 0x2DE0UL, 0x2DFFUL ), /* Cyrillic Extended-A */
2336 AF_UNIRANGE_REC( 0xA640UL, 0xA69FUL ), /* Cyrillic Extended-B */
2337 AF_UNIRANGE_REC( 0xA720UL, 0xA7FFUL ), /* Latin Extended-D */
2338 AF_UNIRANGE_REC( 0xFB00UL, 0xFB06UL ), /* Alphab. Present. Forms (Latin Ligs) */
2339 AF_UNIRANGE_REC( 0x1D400UL, 0x1D7FFUL ), /* Mathematical Alphanumeric Symbols */
2340 AF_UNIRANGE_REC( 0UL, 0UL )
2344 AF_DEFINE_SCRIPT_CLASS(af_latin_script_class,
2348 sizeof( AF_LatinMetricsRec ),
2350 (AF_Script_InitMetricsFunc) af_latin_metrics_init,
2351 (AF_Script_ScaleMetricsFunc)af_latin_metrics_scale,
2352 (AF_Script_DoneMetricsFunc) NULL,
2354 (AF_Script_InitHintsFunc) af_latin_hints_init,
2355 (AF_Script_ApplyHintsFunc) af_latin_hints_apply