1
/***************************************************************************/
5
/* Routines used to load and analyze a given glyph before hinting */
8
/* Copyright 2000-2001, 2002, 2003, 2004 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
/***************************************************************************/
35
ah_dump_edges( AH_Outline outline )
43
edges = outline->horz_edges;
44
edge_limit = edges + outline->num_hedges;
45
segments = outline->horz_segments;
47
for ( dimension = 1; dimension >= 0; dimension-- )
52
printf ( "Table of %s edges:\n",
53
!dimension ? "vertical" : "horizontal" );
54
printf ( " [ index | pos | dir | link |"
55
" serif | blue | opos | pos ]\n" );
57
for ( edge = edges; edge < edge_limit; edge++ )
59
printf ( " [ %5d | %4d | %5s | %4d | %5d | %c | %5.2f | %5.2f ]\n",
62
edge->dir == AH_DIR_UP
64
: ( edge->dir == AH_DIR_DOWN
66
: ( edge->dir == AH_DIR_LEFT
68
: ( edge->dir == AH_DIR_RIGHT
71
edge->link ? ( edge->link - edges ) : -1,
72
edge->serif ? ( edge->serif - edges ) : -1,
73
edge->blue_edge ? 'y' : 'n',
78
edges = outline->vert_edges;
79
edge_limit = edges + outline->num_vedges;
80
segments = outline->vert_segments;
85
/* A function used to dump the array of linked segments */
87
ah_dump_segments( AH_Outline outline )
90
AH_Segment segment_limit;
95
points = outline->points;
96
segments = outline->horz_segments;
97
segment_limit = segments + outline->num_hsegments;
99
for ( dimension = 1; dimension >= 0; dimension-- )
104
printf ( "Table of %s segments:\n",
105
!dimension ? "vertical" : "horizontal" );
106
printf ( " [ index | pos | dir | link | serif |"
107
" numl | first | start ]\n" );
109
for ( seg = segments; seg < segment_limit; seg++ )
111
printf ( " [ %5d | %4d | %5s | %4d | %5d | %4d | %5d | %5d ]\n",
114
seg->dir == AH_DIR_UP
116
: ( seg->dir == AH_DIR_DOWN
118
: ( seg->dir == AH_DIR_LEFT
120
: ( seg->dir == AH_DIR_RIGHT
123
seg->link ? ( seg->link - segments ) : -1,
124
seg->serif ? ( seg->serif - segments ) : -1,
125
(int)seg->num_linked,
127
seg->last - points );
130
segments = outline->vert_segments;
131
segment_limit = segments + outline->num_vsegments;
135
#endif /* AH_DEBUG */
138
/* compute the direction value of a given vector */
140
ah_compute_direction( FT_Pos dx,
144
FT_Pos ax = FT_ABS( dx );
145
FT_Pos ay = FT_ABS( dy );
150
/* atan(1/12) == 4.7 degrees */
152
/* test for vertical direction */
155
dir = dy > 0 ? AH_DIR_UP : AH_DIR_DOWN;
157
/* test for horizontal direction */
158
else if ( ay * 12 < ax )
160
dir = dx > 0 ? AH_DIR_RIGHT : AH_DIR_LEFT;
167
/* this function is used by ah_get_orientation (see below) to test */
168
/* the fill direction of given bbox extremum */
170
ah_test_extremum( FT_Outline* outline,
173
FT_Vector *prev, *cur, *next;
175
FT_Int first, last, c;
179
/* we need to compute the `previous' and `next' point */
180
/* for this extremum; we check whether the extremum */
181
/* is start or end of a contour and providing */
182
/* appropriate values if so */
183
cur = outline->points + n;
188
for ( c = 0; c < outline->n_contours; c++ )
190
last = outline->contours[c];
193
prev = outline->points + last;
196
next = outline->points + first;
201
/* compute the vectorial product -- since we know that the angle */
202
/* is <= 180 degrees (otherwise it wouldn't be an extremum) we */
203
/* can determine the filling orientation if the product is */
204
/* either positive or negative */
205
product = FT_MulDiv( cur->x - prev->x, /* in.x */
206
next->y - cur->y, /* out.y */
209
FT_MulDiv( cur->y - prev->y, /* in.y */
210
next->x - cur->x, /* out.x */
215
retval = product > 0 ? 2 : 1;
221
/* Compute the orientation of path filling. It differs between TrueType */
222
/* and Type1 formats. We could use the `FT_OUTLINE_REVERSE_FILL' flag, */
223
/* but it is better to re-compute it directly (it seems that this flag */
224
/* isn't correctly set for some weird composite glyphs currently). */
226
/* We do this by computing bounding box points, and computing their */
229
/* The function returns either 1 or 2. */
232
ah_get_orientation( FT_Outline* outline )
235
FT_Int indices_xMin, indices_yMin, indices_xMax, indices_yMax;
244
box.xMin = box.yMin = 32767L;
245
box.xMax = box.yMax = -32768L;
248
if ( outline->n_contours < 1 )
251
last = outline->contours[outline->n_contours - 1];
253
for ( n = 0; n <= last; n++ )
258
x = outline->points[n].x;
270
y = outline->points[n].y;
283
/* test orientation of the extrema */
284
n = ah_test_extremum( outline, indices_xMin );
288
n = ah_test_extremum( outline, indices_yMin );
292
n = ah_test_extremum( outline, indices_xMax );
296
n = ah_test_extremum( outline, indices_yMax );
305
/*************************************************************************/
311
/* Creates a new and empty AH_OutlineRec object. */
313
FT_LOCAL_DEF( FT_Error )
314
ah_outline_new( FT_Memory memory,
315
AH_Outline* aoutline )
321
if ( !FT_NEW( outline ) )
323
outline->memory = memory;
331
/*************************************************************************/
334
/* ah_outline_done */
337
/* Destroys a given AH_OutlineRec object. */
340
ah_outline_done( AH_Outline outline )
342
FT_Memory memory = outline->memory;
345
FT_FREE( outline->horz_edges );
346
FT_FREE( outline->horz_segments );
347
FT_FREE( outline->contours );
348
FT_FREE( outline->points );
354
/*************************************************************************/
357
/* ah_outline_save */
360
/* Saves the contents of a given AH_OutlineRec object into a face's */
364
ah_outline_save( AH_Outline outline,
367
AH_Point point = outline->points;
368
AH_Point point_limit = point + outline->num_points;
369
FT_Vector* vec = gloader->current.outline.points;
370
char* tag = gloader->current.outline.tags;
373
/* we assume that the glyph loader has already been checked for storage */
374
for ( ; point < point_limit; point++, vec++, tag++ )
379
if ( point->flags & AH_FLAG_CONIC )
380
tag[0] = FT_CURVE_TAG_CONIC;
381
else if ( point->flags & AH_FLAG_CUBIC )
382
tag[0] = FT_CURVE_TAG_CUBIC;
384
tag[0] = FT_CURVE_TAG_ON;
389
/*************************************************************************/
392
/* ah_outline_load */
395
/* Loads an unscaled outline from a glyph slot into an AH_OutlineRec */
398
FT_LOCAL_DEF( FT_Error )
399
ah_outline_load( AH_Outline outline,
404
FT_Memory memory = outline->memory;
405
FT_Error error = AH_Err_Ok;
406
FT_Outline* source = &face->glyph->outline;
407
FT_Int num_points = source->n_points;
408
FT_Int num_contours = source->n_contours;
412
/* check arguments */
415
face->glyph->format != FT_GLYPH_FORMAT_OUTLINE )
416
return AH_Err_Invalid_Argument;
418
/* first of all, reallocate the contours array if necessary */
419
if ( num_contours > outline->max_contours )
421
FT_Int new_contours = FT_PAD_CEIL( num_contours, 4 );
424
if ( FT_RENEW_ARRAY( outline->contours,
425
outline->max_contours,
429
outline->max_contours = new_contours;
432
/* then, reallocate the points, segments & edges arrays if needed -- */
433
/* note that we reserved two additional point positions, used to */
434
/* hint metrics appropriately */
436
if ( num_points + 2 > outline->max_points )
438
FT_Int news = FT_PAD_CEIL( num_points + 2, 8 );
439
FT_Int max = outline->max_points;
442
if ( FT_RENEW_ARRAY( outline->points, max, news ) ||
443
FT_RENEW_ARRAY( outline->horz_edges, max * 2, news * 2 ) ||
444
FT_RENEW_ARRAY( outline->horz_segments, max * 2, news * 2 ) )
447
/* readjust some pointers */
448
outline->vert_edges = outline->horz_edges + news;
449
outline->vert_segments = outline->horz_segments + news;
450
outline->max_points = news;
453
outline->num_points = num_points;
454
outline->num_contours = num_contours;
456
outline->num_hedges = 0;
457
outline->num_vedges = 0;
458
outline->num_hsegments = 0;
459
outline->num_vsegments = 0;
461
/* We can't rely on the value of `FT_Outline.flags' to know the fill */
462
/* direction used for a glyph, given that some fonts are broken (e.g. */
463
/* the Arphic ones). We thus recompute it each time we need to. */
465
outline->vert_major_dir = AH_DIR_UP;
466
outline->horz_major_dir = AH_DIR_LEFT;
468
if ( ah_get_orientation( source ) > 1 )
470
outline->vert_major_dir = AH_DIR_DOWN;
471
outline->horz_major_dir = AH_DIR_RIGHT;
474
outline->x_scale = x_scale;
475
outline->y_scale = y_scale;
477
points = outline->points;
478
if ( outline->num_points == 0 )
482
/* do one thing at a time -- it is easier to understand, and */
483
/* the code is clearer */
485
AH_Point point_limit = points + outline->num_points;
488
/* compute coordinates */
490
FT_Vector* vec = source->points;
493
for ( point = points; point < point_limit; vec++, point++ )
497
point->ox = point->x = FT_MulFix( vec->x, x_scale );
498
point->oy = point->y = FT_MulFix( vec->y, y_scale );
504
/* compute Bezier flags */
506
char* tag = source->tags;
509
for ( point = points; point < point_limit; point++, tag++ )
511
switch ( FT_CURVE_TAG( *tag ) )
513
case FT_CURVE_TAG_CONIC:
514
point->flags = AH_FLAG_CONIC;
516
case FT_CURVE_TAG_CUBIC:
517
point->flags = AH_FLAG_CUBIC;
525
/* compute `next' and `prev' */
527
FT_Int contour_index;
536
end = points + source->contours[0];
539
for ( point = points; point < point_limit; point++ )
544
point->next = point + 1;
551
if ( point + 1 < point_limit )
553
end = points + source->contours[contour_index];
561
/* set-up the contours array */
563
AH_Point* contour = outline->contours;
564
AH_Point* contour_limit = contour + outline->num_contours;
565
short* end = source->contours;
569
for ( ; contour < contour_limit; contour++, end++ )
571
contour[0] = points + idx;
572
idx = (short)( end[0] + 1 );
576
/* compute directions of in & out vectors */
578
for ( point = points; point < point_limit; point++ )
582
FT_Vector ivec, ovec;
586
ivec.x = point->fx - prev->fx;
587
ivec.y = point->fy - prev->fy;
589
point->in_dir = ah_compute_direction( ivec.x, ivec.y );
592
ovec.x = next->fx - point->fx;
593
ovec.y = next->fy - point->fy;
595
point->out_dir = ah_compute_direction( ovec.x, ovec.y );
597
#ifndef AH_OPTION_NO_WEAK_INTERPOLATION
598
if ( point->flags & ( AH_FLAG_CONIC | AH_FLAG_CUBIC ) )
601
point->flags |= AH_FLAG_WEAK_INTERPOLATION;
603
else if ( point->out_dir == point->in_dir )
605
AH_Angle angle_in, angle_out, delta;
608
if ( point->out_dir != AH_DIR_NONE )
611
angle_in = ah_angle( &ivec );
612
angle_out = ah_angle( &ovec );
613
delta = angle_in - angle_out;
616
delta = AH_2PI - delta;
624
else if ( point->in_dir == -point->out_dir )
637
ah_setup_uv( AH_Outline outline,
640
AH_Point point = outline->points;
641
AH_Point point_limit = point + outline->num_points;
647
for ( ; point < point_limit; point++ )
649
point->u = point->fx;
650
point->v = point->fy;
655
for ( ; point < point_limit; point++ )
657
point->u = point->fy;
658
point->v = point->fx;
663
for ( ; point < point_limit; point++ )
665
point->u = point->ox;
666
point->v = point->oy;
671
for ( ; point < point_limit; point++ )
673
point->u = point->oy;
674
point->v = point->ox;
679
for ( ; point < point_limit; point++ )
687
for ( ; point < point_limit; point++ )
690
point->v = point->ox;
695
for ( ; point < point_limit; point++ )
698
point->v = point->oy;
703
for ( ; point < point_limit; point++ )
712
/* compute all inflex points in a given glyph */
714
ah_outline_compute_inflections( AH_Outline outline )
716
AH_Point* contour = outline->contours;
717
AH_Point* contour_limit = contour + outline->num_contours;
720
/* load original coordinates in (u,v) */
721
ah_setup_uv( outline, AH_UV_FXY );
723
/* do each contour separately */
724
for ( ; contour < contour_limit; contour++ )
727
AH_Point point = contour[0];
728
AH_Point first = point;
729
AH_Point start = point;
730
AH_Point end = point;
733
AH_Angle angle_in, angle_seg, angle_out;
734
AH_Angle diff_in, diff_out;
738
/* compute first segment in contour */
748
} while ( end->u == first->u && end->v == first->v );
750
vec.x = end->u - start->u;
751
vec.y = end->v - start->v;
752
angle_seg = ah_angle( &vec );
754
/* extend the segment start whenever possible */
761
before = before->prev;
762
if ( before == first )
765
} while ( before->u == start->u && before->v == start->v );
767
vec.x = start->u - before->u;
768
vec.y = start->v - before->v;
769
angle_in = ah_angle( &vec );
771
} while ( angle_in == angle_seg );
774
diff_in = ah_angle_diff( angle_in, angle_seg );
776
/* now, process all segments in the contour */
779
/* first, extend current segment's end whenever possible */
787
if ( after == first )
790
} while ( end->u == after->u && end->v == after->v );
792
vec.x = after->u - end->u;
793
vec.y = after->v - end->v;
794
angle_out = ah_angle( &vec );
796
} while ( angle_out == angle_seg );
798
diff_out = ah_angle_diff( angle_seg, angle_out );
800
if ( ( diff_in ^ diff_out ) < 0 )
802
/* diff_in and diff_out have different signs, we have */
803
/* inflection points here... */
806
start->flags |= AH_FLAG_INFLECTION;
809
} while ( start != end );
811
start->flags |= AH_FLAG_INFLECTION;
816
angle_seg = angle_out;
819
} while ( !finished );
828
ah_outline_compute_segments( AH_Outline outline )
832
FT_Int* p_num_segments;
833
AH_Direction segment_dir;
834
AH_Direction major_dir;
837
segments = outline->horz_segments;
838
p_num_segments = &outline->num_hsegments;
839
major_dir = AH_DIR_RIGHT; /* This value must be positive! */
840
segment_dir = major_dir;
842
/* set up (u,v) in each point */
843
ah_setup_uv( outline, AH_UV_FYX );
845
for ( dimension = 1; dimension >= 0; dimension-- )
847
AH_Point* contour = outline->contours;
848
AH_Point* contour_limit = contour + outline->num_contours;
849
AH_Segment segment = segments;
850
FT_Int num_segments = 0;
852
#ifdef AH_HINT_METRICS
853
AH_Point min_point = 0;
854
AH_Point max_point = 0;
855
FT_Pos min_coord = 32000;
856
FT_Pos max_coord = -32000;
860
/* do each contour separately */
861
for ( ; contour < contour_limit; contour++ )
863
AH_Point point = contour[0];
864
AH_Point last = point->prev;
866
FT_Pos min_pos = 32000; /* minimum segment pos != min_coord */
867
FT_Pos max_pos = -32000; /* maximum segment pos != max_coord */
871
#ifdef AH_HINT_METRICS
872
if ( point->u < min_coord )
874
min_coord = point->u;
877
if ( point->u > max_coord )
879
max_coord = point->u;
884
if ( point == last ) /* skip singletons -- just in case */
887
if ( FT_ABS( last->out_dir ) == major_dir &&
888
FT_ABS( point->out_dir ) == major_dir )
890
/* we are already on an edge, try to locate its start */
896
if ( FT_ABS( point->out_dir ) != major_dir )
922
if ( point->out_dir != segment_dir || point == last )
924
/* we are just leaving an edge; record a new segment! */
925
segment->last = point;
926
segment->pos = ( min_pos + max_pos ) >> 1;
928
/* a segment is round if either its first or last point */
929
/* is a control point */
930
if ( ( segment->first->flags | point->flags ) &
932
segment->flags |= AH_EDGE_ROUND;
934
/* compute segment size */
935
min_pos = max_pos = point->v;
937
v = segment->first->v;
943
segment->min_coord = min_pos;
944
segment->max_coord = max_pos;
953
/* now exit if we are at the start/end point */
961
if ( !on_edge && FT_ABS( point->out_dir ) == major_dir )
963
/* this is the start of a new segment! */
964
segment_dir = point->out_dir;
966
/* clear all segment fields */
969
segment->dir = segment_dir;
970
segment->flags = AH_EDGE_NORMAL;
971
min_pos = max_pos = point->u;
972
segment->first = point;
973
segment->last = point;
974
segment->contour = contour;
975
segment->score = 32000;
976
segment->link = NULL;
979
#ifdef AH_HINT_METRICS
980
if ( point == max_point )
983
if ( point == min_point )
993
#ifdef AH_HINT_METRICS
994
/* we need to ensure that there are edges on the left-most and */
995
/* right-most points of the glyph in order to hint the metrics; */
996
/* we do this by inserting fake segments when needed */
997
if ( dimension == 0 )
999
AH_Point point = outline->points;
1000
AH_Point point_limit = point + outline->num_points;
1002
FT_Pos min_pos = 32000;
1003
FT_Pos max_pos = -32000;
1009
/* compute minimum and maximum points */
1010
for ( ; point < point_limit; point++ )
1012
FT_Pos x = point->fx;
1027
/* insert minimum segment */
1030
/* clear all segment fields */
1033
segment->dir = segment_dir;
1034
segment->flags = AH_EDGE_NORMAL;
1035
segment->first = min_point;
1036
segment->last = min_point;
1037
segment->pos = min_pos;
1038
segment->score = 32000;
1039
segment->link = NULL;
1045
/* insert maximum segment */
1048
/* clear all segment fields */
1051
segment->dir = segment_dir;
1052
segment->flags = AH_EDGE_NORMAL;
1053
segment->first = max_point;
1054
segment->last = max_point;
1055
segment->pos = max_pos;
1056
segment->score = 32000;
1057
segment->link = NULL;
1063
#endif /* AH_HINT_METRICS */
1065
*p_num_segments = num_segments;
1067
segments = outline->vert_segments;
1068
major_dir = AH_DIR_UP;
1069
p_num_segments = &outline->num_vsegments;
1071
ah_setup_uv( outline, AH_UV_FXY );
1076
FT_LOCAL_DEF( void )
1077
ah_outline_link_segments( AH_Outline outline )
1079
AH_Segment segments;
1080
AH_Segment segment_limit;
1081
AH_Direction major_dir;
1085
segments = outline->horz_segments;
1086
segment_limit = segments + outline->num_hsegments;
1087
major_dir = outline->horz_major_dir;
1089
for ( dimension = 1; dimension >= 0; dimension-- )
1095
/* now compare each segment to the others */
1096
for ( seg1 = segments; seg1 < segment_limit; seg1++ )
1099
AH_Segment best_segment;
1102
/* the fake segments are introduced to hint the metrics -- */
1103
/* we must never link them to anything */
1104
if ( seg1->first == seg1->last )
1107
best_segment = seg1->link;
1109
best_score = seg1->score;
1111
best_score = +32000;
1113
for ( seg2 = segments; seg2 < segment_limit; seg2++ )
1114
if ( seg1 != seg2 && seg1->dir + seg2->dir == 0 )
1116
FT_Pos pos1 = seg1->pos;
1117
FT_Pos pos2 = seg2->pos;
1122
/* check that the segments are correctly oriented and */
1123
/* positioned to form a black distance */
1125
is_dir = (FT_Bool)( seg1->dir == outline->horz_major_dir ||
1126
seg1->dir == outline->vert_major_dir );
1127
is_pos = (FT_Bool)( pos1 > pos2 );
1129
if ( pos1 == pos2 || !(is_dir ^ is_pos) )
1133
FT_Pos min = seg1->min_coord;
1134
FT_Pos max = seg1->max_coord;
1135
FT_Pos len, dist, score;
1138
if ( min < seg2->min_coord )
1139
min = seg2->min_coord;
1141
if ( max > seg2->max_coord )
1142
max = seg2->max_coord;
1147
dist = seg2->pos - seg1->pos;
1151
score = dist + 3000 / len;
1153
if ( score < best_score )
1156
best_segment = seg2;
1164
seg1->link = best_segment;
1165
seg1->score = best_score;
1166
best_segment->num_linked++;
1172
/* the following code does the same, but much faster! */
1174
/* now compare each segment to the others */
1175
for ( seg1 = segments; seg1 < segment_limit; seg1++ )
1177
/* the fake segments are introduced to hint the metrics -- */
1178
/* we must never link them to anything */
1179
if ( seg1->first == seg1->last || seg1->dir != major_dir )
1182
for ( seg2 = segments; seg2 < segment_limit; seg2++ )
1183
if ( seg2 != seg1 && seg1->dir + seg2->dir == 0 )
1185
FT_Pos pos1 = seg1->pos;
1186
FT_Pos pos2 = seg2->pos;
1187
FT_Pos dist = pos2 - pos1;
1194
FT_Pos min = seg1->min_coord;
1195
FT_Pos max = seg1->max_coord;
1199
if ( min < seg2->min_coord )
1200
min = seg2->min_coord;
1202
if ( max > seg2->max_coord )
1203
max = seg2->max_coord;
1208
score = dist + 3000 / len;
1210
if ( score < seg1->score )
1212
seg1->score = score;
1216
if ( score < seg2->score )
1218
seg2->score = score;
1227
/* now, compute the `serif' segments */
1228
for ( seg1 = segments; seg1 < segment_limit; seg1++ )
1235
if ( seg2->link != seg1 )
1238
seg1->serif = seg2->link;
1243
segments = outline->vert_segments;
1244
segment_limit = segments + outline->num_vsegments;
1245
major_dir = outline->vert_major_dir;
1251
ah_outline_compute_edges( AH_Outline outline )
1254
AH_Segment segments;
1255
AH_Segment segment_limit;
1256
AH_Direction up_dir;
1257
FT_Int* p_num_edges;
1260
FT_Pos edge_distance_threshold;
1263
edges = outline->horz_edges;
1264
segments = outline->horz_segments;
1265
segment_limit = segments + outline->num_hsegments;
1266
p_num_edges = &outline->num_hedges;
1267
up_dir = AH_DIR_RIGHT;
1268
scale = outline->y_scale;
1270
for ( dimension = 1; dimension >= 0; dimension-- )
1273
AH_Edge edge_limit; /* really == edge + num_edges */
1277
/*********************************************************************/
1279
/* We will begin by generating a sorted table of edges for the */
1280
/* current direction. To do so, we simply scan each segment and try */
1281
/* to find an edge in our table that corresponds to its position. */
1283
/* If no edge is found, we create and insert a new edge in the */
1284
/* sorted table. Otherwise, we simply add the segment to the edge's */
1285
/* list which will be processed in the second step to compute the */
1286
/* edge's properties. */
1288
/* Note that the edges table is sorted along the segment/edge */
1291
/*********************************************************************/
1293
edge_distance_threshold = FT_MulFix( outline->edge_distance_threshold,
1295
if ( edge_distance_threshold > 64 / 4 )
1296
edge_distance_threshold = 64 / 4;
1298
edge_distance_threshold = FT_DivFix( edge_distance_threshold,
1302
for ( seg = segments; seg < segment_limit; seg++ )
1307
/* look for an edge corresponding to the segment */
1308
for ( edge = edges; edge < edge_limit; edge++ )
1313
dist = seg->pos - edge->fpos;
1317
if ( dist < edge_distance_threshold )
1326
/* insert a new edge in the list and */
1327
/* sort according to the position */
1328
while ( edge > edges && edge[-1].fpos > seg->pos )
1335
/* clear all edge fields */
1336
FT_MEM_ZERO( edge, sizeof ( *edge ) );
1338
/* add the segment to the new edge's list */
1341
edge->fpos = seg->pos;
1342
edge->opos = edge->pos = FT_MulFix( seg->pos, scale );
1343
seg->edge_next = seg;
1347
/* if an edge was found, simply add the segment to the edge's */
1349
seg->edge_next = edge->first;
1350
edge->last->edge_next = seg;
1354
*p_num_edges = (FT_Int)( edge_limit - edges );
1357
/*********************************************************************/
1359
/* Good, we will now compute each edge's properties according to */
1360
/* segments found on its position. Basically, these are: */
1362
/* - edge's main direction */
1363
/* - stem edge, serif edge or both (which defaults to stem then) */
1364
/* - rounded edge, straight or both (which defaults to straight) */
1365
/* - link for edge */
1367
/*********************************************************************/
1369
/* first of all, set the `edge' field in each segment -- this is */
1370
/* required in order to compute edge links */
1372
/* Note that I've tried to remove this loop, setting
1373
* the "edge" field of each segment directly in the
1374
* code above. For some reason, it slows down execution
1375
* speed -- on a Sun.
1377
for ( edge = edges; edge < edge_limit; edge++ )
1384
seg = seg->edge_next;
1386
while ( seg != edge->first );
1389
/* now, compute each edge properties */
1390
for ( edge = edges; edge < edge_limit; edge++ )
1392
FT_Int is_round = 0; /* does it contain round segments? */
1393
FT_Int is_straight = 0; /* does it contain straight segments? */
1394
FT_Pos ups = 0; /* number of upwards segments */
1395
FT_Pos downs = 0; /* number of downwards segments */
1405
/* check for roundness of segment */
1406
if ( seg->flags & AH_EDGE_ROUND )
1411
/* check for segment direction */
1412
if ( seg->dir == up_dir )
1413
ups += seg->max_coord-seg->min_coord;
1415
downs += seg->max_coord-seg->min_coord;
1417
/* check for links -- if seg->serif is set, then seg->link must */
1419
is_serif = (FT_Bool)( seg->serif && seg->serif->edge != edge );
1421
if ( seg->link || is_serif )
1433
edge2 = edge->serif;
1442
edge_delta = edge->fpos - edge2->fpos;
1443
if ( edge_delta < 0 )
1444
edge_delta = -edge_delta;
1446
seg_delta = seg->pos - seg2->pos;
1447
if ( seg_delta < 0 )
1448
seg_delta = -seg_delta;
1450
if ( seg_delta < edge_delta )
1456
#ifdef FT_CONFIG_CHESTER_SERIF
1459
edge->serif = edge2;
1460
edge2->flags |= AH_EDGE_SERIF;
1464
#else /* !FT_CONFIG_CHESTER_SERIF */
1466
edge->serif = edge2;
1469
#endif /* !FT_CONFIG_CHESTER_SERIF */
1472
seg = seg->edge_next;
1474
} while ( seg != edge->first );
1476
/* set the round/straight flags */
1477
edge->flags = AH_EDGE_NORMAL;
1479
if ( is_round > 0 && is_round >= is_straight )
1480
edge->flags |= AH_EDGE_ROUND;
1482
/* set the edge's main direction */
1483
edge->dir = AH_DIR_NONE;
1488
else if ( ups < downs )
1489
edge->dir = -up_dir;
1491
else if ( ups == downs )
1492
edge->dir = 0; /* both up and down! */
1494
/* gets rid of serifs if link is set */
1495
/* XXX: This gets rid of many unpleasant artefacts! */
1496
/* Example: the `c' in cour.pfa at size 13 */
1498
if ( edge->serif && edge->link )
1502
edges = outline->vert_edges;
1503
segments = outline->vert_segments;
1504
segment_limit = segments + outline->num_vsegments;
1505
p_num_edges = &outline->num_vedges;
1507
scale = outline->x_scale;
1512
/*************************************************************************/
1515
/* ah_outline_detect_features */
1518
/* Performs feature detection on a given AH_OutlineRec object. */
1520
FT_LOCAL_DEF( void )
1521
ah_outline_detect_features( AH_Outline outline )
1523
ah_outline_compute_segments ( outline );
1524
ah_outline_link_segments ( outline );
1525
ah_outline_compute_edges ( outline );
1526
ah_outline_compute_inflections( outline );
1530
/*************************************************************************/
1533
/* ah_outline_compute_blue_edges */
1536
/* Computes the `blue edges' in a given outline (i.e. those that must */
1537
/* be snapped to a blue zone edge (top or bottom). */
1539
FT_LOCAL_DEF( void )
1540
ah_outline_compute_blue_edges( AH_Outline outline,
1541
AH_Face_Globals face_globals )
1543
AH_Edge edge = outline->horz_edges;
1544
AH_Edge edge_limit = edge + outline->num_hedges;
1545
AH_Globals globals = &face_globals->design;
1546
FT_Fixed y_scale = outline->y_scale;
1548
FT_Bool blue_active[AH_BLUE_MAX];
1551
/* compute which blue zones are active, i.e. have their scaled */
1552
/* size < 3/4 pixels */
1558
for ( blue = AH_BLUE_CAPITAL_TOP; blue < AH_BLUE_MAX; blue++ )
1560
FT_Pos ref, shoot, dist;
1563
ref = globals->blue_refs[blue];
1564
shoot = globals->blue_shoots[blue];
1569
blue_active[blue] = 0;
1571
if ( FT_MulFix( dist, y_scale ) < 48 )
1573
blue_active[blue] = 1;
1578
/* return immediately if no blue zone is active */
1583
/* for each horizontal edge search the blue zone which is closest */
1584
for ( ; edge < edge_limit; edge++ )
1587
FT_Pos* best_blue = 0;
1588
FT_Pos best_dist; /* initial threshold */
1591
/* compute the initial threshold as a fraction of the EM size */
1592
best_dist = FT_MulFix( face_globals->face->units_per_EM / 40, y_scale );
1594
#ifdef FT_CONFIG_CHESTER_SMALL_F
1595
if ( best_dist > 64 / 2 )
1598
if ( best_dist > 64 / 4 )
1602
for ( blue = AH_BLUE_CAPITAL_TOP; blue < AH_BLUE_MAX; blue++ )
1604
/* if it is a top zone, check for right edges -- if it is a bottom */
1605
/* zone, check for left edges */
1607
/* of course, that's for TrueType XXX */
1608
FT_Bool is_top_blue =
1609
FT_BOOL( AH_IS_TOP_BLUE( blue ) );
1610
FT_Bool is_major_dir =
1611
FT_BOOL( edge->dir == outline->horz_major_dir );
1614
if ( !blue_active[blue] )
1617
/* if it is a top zone, the edge must be against the major */
1618
/* direction; if it is a bottom zone, it must be in the major */
1620
if ( is_top_blue ^ is_major_dir )
1623
FT_Pos* blue_pos = globals->blue_refs + blue;
1626
/* first of all, compare it to the reference position */
1627
dist = edge->fpos - *blue_pos;
1631
dist = FT_MulFix( dist, y_scale );
1632
if ( dist < best_dist )
1635
best_blue = blue_pos;
1638
/* now, compare it to the overshoot position if the edge is */
1639
/* rounded, and if the edge is over the reference position of a */
1640
/* top zone, or under the reference position of a bottom zone */
1641
if ( edge->flags & AH_EDGE_ROUND && dist != 0 )
1643
FT_Bool is_under_ref = FT_BOOL( edge->fpos < *blue_pos );
1646
if ( is_top_blue ^ is_under_ref )
1648
blue_pos = globals->blue_shoots + blue;
1649
dist = edge->fpos - *blue_pos;
1653
dist = FT_MulFix( dist, y_scale );
1654
if ( dist < best_dist )
1657
best_blue = blue_pos;
1665
edge->blue_edge = best_blue;
1670
/*************************************************************************/
1673
/* ah_outline_scale_blue_edges */
1676
/* This function must be called before hinting in order to re-adjust */
1677
/* the contents of the detected edges (basically change the `blue */
1678
/* edge' pointer from `design units' to `scaled ones'). */
1680
FT_LOCAL_DEF( void )
1681
ah_outline_scale_blue_edges( AH_Outline outline,
1682
AH_Face_Globals globals )
1684
AH_Edge edge = outline->horz_edges;
1685
AH_Edge edge_limit = edge + outline->num_hedges;
1689
delta = globals->scaled.blue_refs - globals->design.blue_refs;
1691
for ( ; edge < edge_limit; edge++ )
1693
if ( edge->blue_edge )
1694
edge->blue_edge += delta;