1 /***************************************************************************/
5 /* Routines used to load and analyze a given glyph before hinting */
8 /* Copyright 2000-2001 Catharon Productions Inc. */
9 /* Author: David Turner */
11 /* This file is part of the Catharon Typography Project and shall only */
12 /* be used, modified, and distributed under the terms of the Catharon */
13 /* Open Source License that should come with this file under the name */
14 /* `CatharonLicense.txt'. By continuing to use, modify, or distribute */
15 /* this file you indicate that you have read the license and */
16 /* understand and accept it fully. */
18 /* Note that this license is compatible with the FreeType license. */
20 /***************************************************************************/
36 ah_dump_edges( AH_Outline* outline )
44 edges = outline->horz_edges;
45 edge_limit = edges + outline->num_hedges;
46 segments = outline->horz_segments;
48 for ( dimension = 1; dimension >= 0; dimension-- )
53 printf ( "Table of %s edges:\n",
54 !dimension ? "vertical" : "horizontal" );
55 printf ( " [ index | pos | dir | link |"
56 " serif | blue | opos | pos ]\n" );
58 for ( edge = edges; edge < edge_limit; edge++ )
60 printf ( " [ %5d | %4d | %5s | %4d | %5d | %c | %5.2f | %5.2f ]\n",
63 edge->dir == ah_dir_up
65 : ( edge->dir == ah_dir_down
67 : ( edge->dir == ah_dir_left
69 : ( edge->dir == ah_dir_right
72 edge->link ? ( edge->link - edges ) : -1,
73 edge->serif ? ( edge->serif - edges ) : -1,
74 edge->blue_edge ? 'y' : 'n',
79 edges = outline->vert_edges;
80 edge_limit = edges + outline->num_vedges;
81 segments = outline->vert_segments;
86 /* A function used to dump the array of linked segments */
88 ah_dump_segments( AH_Outline* outline )
91 AH_Segment* segment_limit;
96 points = outline->points;
97 segments = outline->horz_segments;
98 segment_limit = segments + outline->num_hsegments;
100 for ( dimension = 1; dimension >= 0; dimension-- )
105 printf ( "Table of %s segments:\n",
106 !dimension ? "vertical" : "horizontal" );
107 printf ( " [ index | pos | dir | link | serif |"
108 " numl | first | start ]\n" );
110 for ( seg = segments; seg < segment_limit; seg++ )
112 printf ( " [ %5d | %4d | %5s | %4d | %5d | %4d | %5d | %5d ]\n",
115 seg->dir == ah_dir_up
117 : ( seg->dir == ah_dir_down
119 : ( seg->dir == ah_dir_left
121 : ( seg->dir == ah_dir_right
124 seg->link ? (seg->link-segments) : -1,
125 seg->serif ? (seg->serif-segments) : -1,
126 (int)seg->num_linked,
128 seg->last - points );
131 segments = outline->vert_segments;
132 segment_limit = segments + outline->num_vsegments;
136 #endif /* AH_DEBUG */
139 /* compute the direction value of a given vector.. */
141 ah_compute_direction( FT_Pos dx,
145 FT_Pos ax = ABS( dx );
146 FT_Pos ay = ABS( dy );
151 /* test for vertical direction */
154 dir = dy > 0 ? ah_dir_up : ah_dir_down;
156 /* test for horizontal direction */
157 else if ( ay * 12 < ax )
159 dir = dx > 0 ? ah_dir_right : ah_dir_left;
166 /* this function is used by ah_get_orientation (see below) to test */
167 /* the fill direction of a given bbox extrema */
169 ah_test_extrema( FT_Outline* outline,
172 FT_Vector *prev, *cur, *next;
174 FT_Int first, last, c;
177 /* we need to compute the `previous' and `next' point */
178 /* for these extrema */
179 cur = outline->points + n;
184 for ( c = 0; c < outline->n_contours; c++ )
186 last = outline->contours[c];
189 prev = outline->points + last;
192 next = outline->points + first;
197 product = FT_MulDiv( cur->x - prev->x, /* in.x */
198 next->y - cur->y, /* out.y */
201 FT_MulDiv( cur->y - prev->y, /* in.y */
202 next->x - cur->x, /* out.x */
206 product = product > 0 ? 2 : 1;
212 /* Compute the orientation of path filling. It differs between TrueType */
213 /* and Type1 formats. We could use the `ft_outline_reverse_fill' flag, */
214 /* but it is better to re-compute it directly (it seems that this flag */
215 /* isn't correctly set for some weird composite glyphs currently). */
217 /* We do this by computing bounding box points, and computing their */
220 /* The function returns either 1 or -1. */
223 ah_get_orientation( FT_Outline* outline )
235 box.xMin = box.yMin = 32767L;
236 box.xMax = box.yMax = -32768L;
239 if ( outline->n_contours < 1 )
242 last = outline->contours[outline->n_contours - 1];
244 for ( n = 0; n <= last; n++ )
249 x = outline->points[n].x;
261 y = outline->points[n].y;
274 /* test orientation of the xmin */
275 n = ah_test_extrema( outline, indices.xMin );
279 n = ah_test_extrema( outline, indices.yMin );
283 n = ah_test_extrema( outline, indices.xMax );
287 n = ah_test_extrema( outline, indices.yMax );
296 /*************************************************************************/
302 /* Creates a new and empty AH_Outline object. */
304 FT_LOCAL_DEF FT_Error
305 ah_outline_new( FT_Memory memory,
306 AH_Outline** aoutline )
312 if ( !ALLOC( outline, sizeof ( *outline ) ) )
314 outline->memory = memory;
322 /*************************************************************************/
325 /* ah_outline_done */
328 /* Destroys a given AH_Outline object. */
331 ah_outline_done( AH_Outline* outline )
333 FT_Memory memory = outline->memory;
336 FREE( outline->horz_edges );
337 FREE( outline->horz_segments );
338 FREE( outline->contours );
339 FREE( outline->points );
345 /*************************************************************************/
348 /* ah_outline_save */
351 /* Saves the content of a given AH_Outline object into a face's glyph */
355 ah_outline_save( AH_Outline* outline,
358 AH_Point* point = outline->points;
359 AH_Point* point_limit = point + outline->num_points;
360 FT_Vector* vec = gloader->current.outline.points;
361 char* tag = gloader->current.outline.tags;
364 /* we assume that the glyph loader has already been checked for storage */
365 for ( ; point < point_limit; point++, vec++, tag++ )
370 if ( point->flags & ah_flah_conic )
371 tag[0] = FT_Curve_Tag_Conic;
372 else if ( point->flags & ah_flah_cubic )
373 tag[0] = FT_Curve_Tag_Cubic;
375 tag[0] = FT_Curve_Tag_On;
380 /*************************************************************************/
383 /* ah_outline_load */
386 /* Loads an unscaled outline from a glyph slot into an AH_Outline */
389 FT_LOCAL_DEF FT_Error
390 ah_outline_load( AH_Outline* outline,
393 FT_Memory memory = outline->memory;
394 FT_Error error = AH_Err_Ok;
395 FT_Outline* source = &face->glyph->outline;
396 FT_Int num_points = source->n_points;
397 FT_Int num_contours = source->n_contours;
401 /* check arguments */
404 face->glyph->format != ft_glyph_format_outline )
405 return AH_Err_Invalid_Argument;
407 /* first of all, reallocate the contours array if necessary */
408 if ( num_contours > outline->max_contours )
410 FT_Int new_contours = ( num_contours + 3 ) & -4;
413 if ( REALLOC_ARRAY( outline->contours, outline->max_contours,
414 new_contours, AH_Point* ) )
417 outline->max_contours = new_contours;
420 /* then, reallocate the points, segments & edges arrays if needed -- */
421 /* note that we reserved two additional point positions, used to */
422 /* hint metrics appropriately */
424 if ( num_points + 2 > outline->max_points )
426 FT_Int news = ( num_points + 2 + 7 ) & -8;
427 FT_Int max = outline->max_points;
430 if ( REALLOC_ARRAY( outline->points,
431 max, news, AH_Point ) ||
432 REALLOC_ARRAY( outline->horz_edges,
433 max * 2, news * 2, AH_Edge ) ||
434 REALLOC_ARRAY( outline->horz_segments,
435 max * 2, news * 2, AH_Segment ) )
438 /* readjust some pointers */
439 outline->vert_edges = outline->horz_edges + news;
440 outline->vert_segments = outline->horz_segments + news;
441 outline->max_points = news;
444 outline->num_points = num_points;
445 outline->num_contours = num_contours;
447 outline->num_hedges = 0;
448 outline->num_vedges = 0;
449 outline->num_hsegments = 0;
450 outline->num_vsegments = 0;
452 /* We can't rely on the value of `FT_Outline.flags' to know the fill */
453 /* direction used for a glyph, given that some fonts are broken (e.g. */
454 /* the Arphic ones). We thus recompute it each time we need to. */
456 outline->vert_major_dir = ah_dir_up;
457 outline->horz_major_dir = ah_dir_left;
459 if ( ah_get_orientation( source ) > 1 )
461 outline->vert_major_dir = ah_dir_down;
462 outline->horz_major_dir = ah_dir_right;
465 outline->x_scale = face->size->metrics.x_scale;
466 outline->y_scale = face->size->metrics.y_scale;
468 points = outline->points;
469 if ( outline->num_points == 0 )
473 /* do one thing at a time -- it is easier to understand, and */
474 /* the code is clearer */
476 AH_Point* point_limit = points + outline->num_points;
479 /* compute coordinates */
481 FT_Vector* vec = source->points;
482 FT_Fixed x_scale = outline->x_scale;
483 FT_Fixed y_scale = outline->y_scale;
486 for ( point = points; point < point_limit; vec++, point++ )
490 point->ox = point->x = FT_MulFix( vec->x, x_scale );
491 point->oy = point->y = FT_MulFix( vec->y, y_scale );
497 /* compute Bezier flags */
499 char* tag = source->tags;
502 for ( point = points; point < point_limit; point++, tag++ )
504 switch ( FT_CURVE_TAG( *tag ) )
506 case FT_Curve_Tag_Conic:
507 point->flags = ah_flah_conic; break;
508 case FT_Curve_Tag_Cubic:
509 point->flags = ah_flah_cubic; break;
516 /* compute `next' and `prev' */
518 FT_Int contour_index;
527 end = points + source->contours[0];
530 for ( point = points; point < point_limit; point++ )
535 point->next = point + 1;
542 if ( point + 1 < point_limit )
544 end = points + source->contours[contour_index];
552 /* set-up the contours array */
554 AH_Point** contour = outline->contours;
555 AH_Point** contour_limit = contour + outline->num_contours;
556 short* end = source->contours;
560 for ( ; contour < contour_limit; contour++, end++ )
562 contour[0] = points + index;
563 index = (short)( end[0] + 1 );
567 /* compute directions of in & out vectors */
569 for ( point = points; point < point_limit; point++ )
577 vec.x = point->fx - prev->fx;
578 vec.y = point->fy - prev->fy;
580 point->in_dir = ah_compute_direction( vec.x, vec.y );
582 #ifndef AH_OPTION_NO_WEAK_INTERPOLATION
583 point->in_angle = ah_angle( &vec );
587 vec.x = next->fx - point->fx;
588 vec.y = next->fy - point->fy;
590 point->out_dir = ah_compute_direction( vec.x, vec.y );
592 #ifndef AH_OPTION_NO_WEAK_INTERPOLATION
593 point->out_angle = ah_angle( &vec );
596 AH_Angle delta = point->in_angle - point->out_angle;
602 point->flags |= ah_flah_weak_interpolation;
606 if ( point->flags & ( ah_flah_conic | ah_flah_cubic ) )
607 point->flags |= ah_flah_weak_interpolation;
610 #endif /* !AH_OPTION_NO_WEAK_INTERPOLATION */
612 #ifdef AH_OPTION_NO_STRONG_INTERPOLATION
613 point->flags |= ah_flah_weak_interpolation;
625 ah_setup_uv( AH_Outline* outline,
628 AH_Point* point = outline->points;
629 AH_Point* point_limit = point + outline->num_points;
632 for ( ; point < point_limit; point++ )
679 ah_outline_compute_segments( AH_Outline* outline )
682 AH_Segment* segments;
683 FT_Int* p_num_segments;
684 AH_Direction segment_dir;
685 AH_Direction major_dir;
688 segments = outline->horz_segments;
689 p_num_segments = &outline->num_hsegments;
690 major_dir = ah_dir_right; /* This value must be positive! */
691 segment_dir = major_dir;
693 /* set up (u,v) in each point */
694 ah_setup_uv( outline, ah_uv_fyx );
696 for ( dimension = 1; dimension >= 0; dimension-- )
698 AH_Point** contour = outline->contours;
699 AH_Point** contour_limit = contour + outline->num_contours;
700 AH_Segment* segment = segments;
701 FT_Int num_segments = 0;
703 #ifdef AH_HINT_METRICS
704 AH_Point* min_point = 0;
705 AH_Point* max_point = 0;
706 FT_Pos min_coord = 32000;
707 FT_Pos max_coord = -32000;
711 /* do each contour separately */
712 for ( ; contour < contour_limit; contour++ )
714 AH_Point* point = contour[0];
715 AH_Point* last = point->prev;
717 FT_Pos min_pos = +32000; /* minimum segment pos != min_coord */
718 FT_Pos max_pos = -32000; /* maximum segment pos != max_coord */
722 #ifdef AH_HINT_METRICS
723 if ( point->u < min_coord )
725 min_coord = point->u;
728 if ( point->u > max_coord )
730 max_coord = point->u;
735 if ( point == last ) /* skip singletons -- just in case? */
738 if ( ABS( last->out_dir ) == major_dir &&
739 ABS( point->out_dir ) == major_dir )
741 /* we are already on an edge, try to locate its start */
747 if ( ABS( point->out_dir ) != major_dir )
774 if ( point->out_dir != segment_dir || point == last )
776 /* we are just leaving an edge; record a new segment! */
777 segment->last = point;
778 segment->pos = ( min_pos + max_pos ) >> 1;
780 /* a segment is round if either its first or last point */
781 /* is a control point */
782 if ( ( segment->first->flags | point->flags ) &
784 segment->flags |= ah_edge_round;
786 /* compute segment size */
787 min_pos = max_pos = point->v;
789 v = segment->first->v;
795 segment->min_coord = min_pos;
796 segment->max_coord = max_pos;
805 /* now exit if we are at the start/end point */
813 if ( !on_edge && ABS( point->out_dir ) == major_dir )
815 /* this is the start of a new segment! */
816 segment_dir = point->out_dir;
818 /* clear all segment fields */
819 memset( segment, 0, sizeof ( *segment ) );
821 segment->dir = segment_dir;
822 segment->flags = ah_edge_normal;
823 min_pos = max_pos = point->u;
824 segment->first = point;
825 segment->last = point;
826 segment->contour = contour;
829 #ifdef AH_HINT_METRICS
830 if ( point == max_point )
833 if ( point == min_point )
843 #ifdef AH_HINT_METRICS
844 /* we need to ensure that there are edges on the left-most and */
845 /* right-most points of the glyph in order to hint the metrics; */
846 /* we do this by inserting fake segments when needed */
847 if ( dimension == 0 )
849 AH_Point* point = outline->points;
850 AH_Point* point_limit = point + outline->num_points;
852 FT_Pos min_pos = 32000;
853 FT_Pos max_pos = -32000;
859 /* compute minimum and maximum points */
860 for ( ; point < point_limit; point++ )
862 FT_Pos x = point->fx;
877 /* insert minimum segment */
880 /* clear all segment fields */
881 memset( segment, 0, sizeof ( *segment ) );
883 segment->dir = segment_dir;
884 segment->flags = ah_edge_normal;
885 segment->first = min_point;
886 segment->last = min_point;
887 segment->pos = min_pos;
893 /* insert maximum segment */
896 /* clear all segment fields */
897 memset( segment, 0, sizeof ( *segment ) );
899 segment->dir = segment_dir;
900 segment->flags = ah_edge_normal;
901 segment->first = max_point;
902 segment->last = max_point;
903 segment->pos = max_pos;
909 #endif /* AH_HINT_METRICS */
911 *p_num_segments = num_segments;
913 segments = outline->vert_segments;
914 major_dir = ah_dir_up;
915 p_num_segments = &outline->num_vsegments;
916 ah_setup_uv( outline, ah_uv_fxy );
922 ah_outline_link_segments( AH_Outline* outline )
924 AH_Segment* segments;
925 AH_Segment* segment_limit;
929 ah_setup_uv( outline, ah_uv_fyx );
931 segments = outline->horz_segments;
932 segment_limit = segments + outline->num_hsegments;
934 for ( dimension = 1; dimension >= 0; dimension-- )
940 /* now compare each segment to the others */
941 for ( seg1 = segments; seg1 < segment_limit; seg1++ )
943 FT_Pos best_score = 32000;
944 AH_Segment* best_segment = 0;
947 /* the fake segments are introduced to hint the metrics -- */
948 /* we must never link them to anything */
949 if ( seg1->first == seg1->last )
952 for ( seg2 = segments; seg2 < segment_limit; seg2++ )
953 if ( seg1 != seg2 && seg1->dir + seg2->dir == 0 )
955 FT_Pos pos1 = seg1->pos;
956 FT_Pos pos2 = seg2->pos;
961 /* check that the segments are correctly oriented and */
962 /* positioned to form a black distance */
964 is_dir = (FT_Bool)( seg1->dir == outline->horz_major_dir ||
965 seg1->dir == outline->vert_major_dir );
966 is_pos = (FT_Bool)( pos1 > pos2 );
968 if ( pos1 == pos2 || !(is_dir ^ is_pos) )
971 /* Check the two segments. We now have a better algorithm */
972 /* that doesn't rely on the segment points themselves but */
973 /* on their relative position. This gets rids of many */
974 /* unpleasant artefacts and incorrect stem/serifs */
977 /* first of all, compute the size of the `common' height */
979 FT_Pos min = seg1->min_coord;
980 FT_Pos max = seg1->max_coord;
986 size2 = seg2->max_coord - seg2->min_coord;
988 if ( min < seg2->min_coord )
989 min = seg2->min_coord;
991 if ( max < seg2->max_coord )
992 max = seg2->max_coord;
995 score = seg2->pos - seg1->pos;
999 /* before comparing the scores, take care that the segments */
1000 /* are really facing each other (often not for italics..) */
1001 if ( 4 * len >= size1 && 4 * len >= size2 )
1002 if ( score < best_score )
1005 best_segment = seg2;
1012 seg1->link = best_segment;
1013 seg1->score = best_score;
1015 best_segment->num_linked++;
1021 /* now, compute the `serif' segments */
1022 for ( seg1 = segments; seg1 < segment_limit; seg1++ )
1026 if ( seg2 && seg2->link != seg1 )
1029 seg1->serif = seg2->link;
1033 ah_setup_uv( outline, ah_uv_fxy );
1035 segments = outline->vert_segments;
1036 segment_limit = segments + outline->num_vsegments;
1042 ah_outline_compute_edges( AH_Outline* outline )
1045 AH_Segment* segments;
1046 AH_Segment* segment_limit;
1047 AH_Direction up_dir;
1048 FT_Int* p_num_edges;
1051 FT_Pos edge_distance_threshold;
1054 edges = outline->horz_edges;
1055 segments = outline->horz_segments;
1056 segment_limit = segments + outline->num_hsegments;
1057 p_num_edges = &outline->num_hedges;
1058 up_dir = ah_dir_right;
1059 scale = outline->y_scale;
1061 for ( dimension = 1; dimension >= 0; dimension-- )
1064 AH_Edge* edge_limit; /* really == edge + num_edges */
1068 /*********************************************************************/
1070 /* We will begin by generating a sorted table of edges for the */
1071 /* current direction. To do so, we simply scan each segment and try */
1072 /* to find an edge in our table that corresponds to its position. */
1074 /* If no edge is found, we create and insert a new edge in the */
1075 /* sorted table. Otherwise, we simply add the segment to the edge's */
1076 /* list which will be processed in the second step to compute the */
1077 /* edge's properties. */
1079 /* Note that the edges table is sorted along the segment/edge */
1082 /*********************************************************************/
1084 edge_distance_threshold = FT_MulFix( outline->edge_distance_threshold,
1086 if ( edge_distance_threshold > 64 / 4 )
1087 edge_distance_threshold = 64 / 4;
1090 for ( seg = segments; seg < segment_limit; seg++ )
1095 /* look for an edge corresponding to the segment */
1096 for ( edge = edges; edge < edge_limit; edge++ )
1101 dist = seg->pos - edge->fpos;
1105 dist = FT_MulFix( dist, scale );
1106 if ( dist < edge_distance_threshold )
1115 /* insert a new edge in the list and */
1116 /* sort according to the position */
1117 while ( edge > edges && edge[-1].fpos > seg->pos )
1124 /* clear all edge fields */
1125 memset( edge, 0, sizeof ( *edge ) );
1127 /* add the segment to the new edge's list */
1130 edge->fpos = seg->pos;
1131 edge->opos = edge->pos = FT_MulFix( seg->pos, scale );
1132 seg->edge_next = seg;
1136 /* if an edge was found, simply add the segment to the edge's */
1138 seg->edge_next = edge->first;
1139 edge->last->edge_next = seg;
1144 *p_num_edges = (FT_Int)( edge_limit - edges );
1147 /*********************************************************************/
1149 /* Good, we will now compute each edge's properties according to */
1150 /* segments found on its position. Basically, these are: */
1152 /* - edge's main direction */
1153 /* - stem edge, serif edge or both (which defaults to stem then) */
1154 /* - rounded edge, straigth or both (which defaults to straight) */
1155 /* - link for edge */
1157 /*********************************************************************/
1159 /* first of all, set the `edge' field in each segment -- this is */
1160 /* required in order to compute edge links */
1161 for ( edge = edges; edge < edge_limit; edge++ )
1168 seg = seg->edge_next;
1170 while ( seg != edge->first );
1173 /* now, compute each edge properties */
1174 for ( edge = edges; edge < edge_limit; edge++ )
1176 int is_round = 0; /* does it contain round segments? */
1177 int is_straight = 0; /* does it contain straight segments? */
1178 int ups = 0; /* number of upwards segments */
1179 int downs = 0; /* number of downwards segments */
1189 /* check for roundness of segment */
1190 if ( seg->flags & ah_edge_round )
1195 /* check for segment direction */
1196 if ( seg->dir == up_dir )
1197 ups += seg->max_coord-seg->min_coord;
1199 downs += seg->max_coord-seg->min_coord;
1201 /* check for links -- if seg->serif is set, then seg->link must */
1203 is_serif = (FT_Bool)( seg->serif && seg->serif->edge != edge );
1205 if ( seg->link || is_serif )
1217 edge2 = edge->serif;
1226 edge_delta = edge->fpos - edge2->fpos;
1227 if ( edge_delta < 0 )
1228 edge_delta = -edge_delta;
1230 seg_delta = seg->pos - seg2->pos;
1231 if ( seg_delta < 0 )
1232 seg_delta = -seg_delta;
1234 if ( seg_delta < edge_delta )
1241 edge->serif = edge2;
1246 seg = seg->edge_next;
1248 } while ( seg != edge->first );
1250 /* set the round/straight flags */
1251 edge->flags = ah_edge_normal;
1253 if ( is_round > 0 && is_round >= is_straight )
1254 edge->flags |= ah_edge_round;
1256 /* set the edge's main direction */
1257 edge->dir = ah_dir_none;
1262 else if ( ups < downs )
1263 edge->dir = - up_dir;
1265 else if ( ups == downs )
1266 edge->dir = 0; /* both up and down !! */
1268 /* gets rid of serifs if link is set */
1269 /* XXX: This gets rid of many unpleasant artefacts! */
1270 /* Example: the `c' in cour.pfa at size 13 */
1272 if ( edge->serif && edge->link )
1276 edges = outline->vert_edges;
1277 segments = outline->vert_segments;
1278 segment_limit = segments + outline->num_vsegments;
1279 p_num_edges = &outline->num_vedges;
1281 scale = outline->x_scale;
1286 /*************************************************************************/
1289 /* ah_outline_detect_features */
1292 /* Performs feature detection on a given AH_Outline object. */
1295 ah_outline_detect_features( AH_Outline* outline )
1297 ah_outline_compute_segments( outline );
1298 ah_outline_link_segments ( outline );
1299 ah_outline_compute_edges ( outline );
1303 /*************************************************************************/
1306 /* ah_outline_compute_blue_edges */
1309 /* Computes the `blue edges' in a given outline (i.e. those that must */
1310 /* be snapped to a blue zone edge (top or bottom). */
1313 ah_outline_compute_blue_edges( AH_Outline* outline,
1314 AH_Face_Globals* face_globals )
1316 AH_Edge* edge = outline->horz_edges;
1317 AH_Edge* edge_limit = edge + outline->num_hedges;
1318 AH_Globals* globals = &face_globals->design;
1319 FT_Fixed y_scale = outline->y_scale;
1321 FT_Bool blue_active[ah_blue_max];
1324 /* compute which blue zones are active, i.e. have their scaled */
1325 /* size < 3/4 pixels */
1331 for ( blue = ah_blue_capital_top; blue < ah_blue_max; blue++ )
1333 FT_Pos ref, shoot, dist;
1336 ref = globals->blue_refs[blue];
1337 shoot = globals->blue_shoots[blue];
1342 blue_active[blue] = 0;
1344 if ( FT_MulFix( dist, y_scale ) < 48 )
1346 blue_active[blue] = 1;
1351 /* return immediately if no blue zone is active */
1356 /* compute for each horizontal edge, which blue zone is closer */
1357 for ( ; edge < edge_limit; edge++ )
1360 FT_Pos* best_blue = 0;
1361 FT_Pos best_dist; /* initial threshold */
1364 /* compute the initial threshold as a fraction of the EM size */
1365 best_dist = FT_MulFix( face_globals->face->units_per_EM / 40, y_scale );
1366 if ( best_dist > 64 / 4 )
1369 for ( blue = ah_blue_capital_top; blue < ah_blue_max; blue++ )
1371 /* if it is a top zone, check for right edges -- if it is a bottom */
1372 /* zone, check for left edges */
1374 /* of course, that's for TrueType XXX */
1375 FT_Bool is_top_blue =
1376 FT_BOOL( AH_IS_TOP_BLUE( blue ) );
1377 FT_Bool is_major_dir =
1378 FT_BOOL( edge->dir == outline->horz_major_dir );
1380 if ( !blue_active[blue] )
1383 /* if it is a top zone, the edge must be against the major */
1384 /* direction; if it is a bottom zone, it must be in the major */
1386 if ( is_top_blue ^ is_major_dir )
1389 FT_Pos* blue_pos = globals->blue_refs + blue;
1392 /* first of all, compare it to the reference position */
1393 dist = edge->fpos - *blue_pos;
1397 dist = FT_MulFix( dist, y_scale );
1398 if ( dist < best_dist )
1401 best_blue = blue_pos;
1404 /* now, compare it to the overshoot position if the edge is */
1405 /* rounded, and if the edge is over the reference position of a */
1406 /* top zone, or under the reference position of a bottom zone */
1407 if ( edge->flags & ah_edge_round && dist != 0 )
1409 FT_Bool is_under_ref = FT_BOOL( edge->fpos < *blue_pos );
1412 if ( is_top_blue ^ is_under_ref )
1414 blue_pos = globals->blue_shoots + blue;
1415 dist = edge->fpos - *blue_pos;
1419 dist = FT_MulFix( dist, y_scale );
1420 if ( dist < best_dist )
1423 best_blue = blue_pos;
1431 edge->blue_edge = best_blue;
1436 /*************************************************************************/
1439 /* ah_outline_scale_blue_edges */
1442 /* This functions must be called before hinting in order to re-adjust */
1443 /* the contents of the detected edges (basically change the `blue */
1444 /* edge' pointer from `design units' to `scaled ones'). */
1447 ah_outline_scale_blue_edges( AH_Outline* outline,
1448 AH_Face_Globals* globals )
1450 AH_Edge* edge = outline->horz_edges;
1451 AH_Edge* edge_limit = edge + outline->num_hedges;
1455 delta = globals->scaled.blue_refs - globals->design.blue_refs;
1457 for ( ; edge < edge_limit; edge++ )
1459 if ( edge->blue_edge )
1460 edge->blue_edge += delta;