1
/***************************************************************************/
5
/* Auto-fitter hinting routines for latin writing system (body). */
7
/* Copyright 2003-2014 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
29
#ifdef AF_CONFIG_OPTION_USE_WARPER
34
/*************************************************************************/
36
/* The macro FT_COMPONENT is used in trace mode. It is an implicit */
37
/* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
38
/* messages during execution. */
41
#define FT_COMPONENT trace_aflatin
44
/*************************************************************************/
45
/*************************************************************************/
47
/***** L A T I N G L O B A L M E T R I C S *****/
49
/*************************************************************************/
50
/*************************************************************************/
53
/* Find segments and links, compute all stem widths, and initialize */
54
/* standard width and height for the glyph with given charcode. */
57
af_latin_metrics_init_widths( AF_LatinMetrics metrics,
60
/* scan the array of segments in each direction */
61
AF_GlyphHintsRec hints[1];
65
"latin standard widths computation (style `%s')\n"
66
"=====================================================\n"
68
af_style_names[metrics->root.style_class->style] ));
70
af_glyph_hints_init( hints, face->memory );
72
metrics->axis[AF_DIMENSION_HORZ].width_count = 0;
73
metrics->axis[AF_DIMENSION_VERT].width_count = 0;
80
AF_LatinMetricsRec dummy[1];
81
AF_Scaler scaler = &dummy->root.scaler;
83
#ifdef FT_CONFIG_OPTION_PIC
84
AF_FaceGlobals globals = metrics->root.globals;
87
AF_StyleClass style_class = metrics->root.style_class;
88
AF_ScriptClass script_class = AF_SCRIPT_CLASSES_GET
89
[style_class->script];
91
FT_UInt32 standard_char;
95
* We check more than a single standard character to catch features
96
* like `c2sc' (small caps from caps) that don't contain lowercase
97
* letters by definition, or other features that mainly operate on
101
standard_char = script_class->standard_char1;
102
af_get_char_index( &metrics->root,
108
if ( script_class->standard_char2 )
110
standard_char = script_class->standard_char2;
111
af_get_char_index( &metrics->root,
117
if ( script_class->standard_char3 )
119
standard_char = script_class->standard_char3;
120
af_get_char_index( &metrics->root,
135
FT_TRACE5(( "standard character: U+%04lX (glyph index %d)\n",
136
standard_char, glyph_index ));
138
error = FT_Load_Glyph( face, glyph_index, FT_LOAD_NO_SCALE );
139
if ( error || face->glyph->outline.n_points <= 0 )
144
dummy->units_per_em = metrics->units_per_em;
146
scaler->x_scale = 0x10000L;
147
scaler->y_scale = 0x10000L;
152
scaler->render_mode = FT_RENDER_MODE_NORMAL;
155
af_glyph_hints_rescale( hints, (AF_StyleMetrics)dummy );
157
error = af_glyph_hints_reload( hints, &face->glyph->outline );
161
for ( dim = 0; dim < AF_DIMENSION_MAX; dim++ )
163
AF_LatinAxis axis = &metrics->axis[dim];
164
AF_AxisHints axhints = &hints->axis[dim];
165
AF_Segment seg, limit, link;
166
FT_UInt num_widths = 0;
169
error = af_latin_hints_compute_segments( hints,
174
af_latin_hints_link_segments( hints,
177
seg = axhints->segments;
178
limit = seg + axhints->num_segments;
180
for ( ; seg < limit; seg++ )
184
/* we only consider stem segments there! */
185
if ( link && link->link == seg && link > seg )
190
dist = seg->pos - link->pos;
194
if ( num_widths < AF_LATIN_MAX_WIDTHS )
195
axis->widths[num_widths++].org = dist;
199
/* this also replaces multiple almost identical stem widths */
200
/* with a single one (the value 100 is heuristic) */
201
af_sort_and_quantize_widths( &num_widths, axis->widths,
202
dummy->units_per_em / 100 );
203
axis->width_count = num_widths;
207
for ( dim = 0; dim < AF_DIMENSION_MAX; dim++ )
209
AF_LatinAxis axis = &metrics->axis[dim];
213
stdw = ( axis->width_count > 0 ) ? axis->widths[0].org
214
: AF_LATIN_CONSTANT( metrics, 50 );
216
/* let's try 20% of the smallest width */
217
axis->edge_distance_threshold = stdw / 5;
218
axis->standard_width = stdw;
219
axis->extra_light = 0;
221
#ifdef FT_DEBUG_LEVEL_TRACE
226
FT_TRACE5(( "%s widths:\n",
227
dim == AF_DIMENSION_VERT ? "horizontal"
230
FT_TRACE5(( " %d (standard)", axis->standard_width ));
231
for ( i = 1; i < axis->width_count; i++ )
232
FT_TRACE5(( " %d", axis->widths[i].org ));
242
af_glyph_hints_done( hints );
246
/* Find all blue zones. Flat segments give the reference points, */
247
/* round segments the overshoot positions. */
250
af_latin_metrics_init_blues( AF_LatinMetrics metrics,
253
FT_Pos flats [AF_BLUE_STRING_MAX_LEN];
254
FT_Pos rounds[AF_BLUE_STRING_MAX_LEN];
261
AF_LatinAxis axis = &metrics->axis[AF_DIMENSION_VERT];
264
AF_StyleClass sc = metrics->root.style_class;
266
AF_Blue_Stringset bss = sc->blue_stringset;
267
const AF_Blue_StringRec* bs = &af_blue_stringsets[bss];
270
/* we walk over the blue character strings as specified in the */
271
/* style's entry in the `af_blue_stringset' array */
273
FT_TRACE5(( "latin blue zones computation\n"
274
"============================\n"
277
for ( ; bs->string != AF_BLUE_STRING_MAX; bs++ )
279
const char* p = &af_blue_strings[bs->string];
284
#ifdef FT_DEBUG_LEVEL_TRACE
286
FT_Bool have_flag = 0;
289
FT_TRACE5(( "blue zone %d", axis->blue_count ));
291
if ( bs->properties )
295
if ( AF_LATIN_IS_TOP_BLUE( bs ) )
297
FT_TRACE5(( "top" ));
301
if ( AF_LATIN_IS_X_HEIGHT_BLUE( bs ) )
305
FT_TRACE5(( "small top" ));
309
if ( AF_LATIN_IS_LONG_BLUE( bs ) )
313
FT_TRACE5(( "long" ));
319
FT_TRACE5(( ":\n" ));
321
#endif /* FT_DEBUG_LEVEL_TRACE */
329
FT_ULong glyph_index;
331
FT_Pos best_y; /* same as points.y */
332
FT_Int best_point, best_contour_first, best_contour_last;
337
GET_UTF8_CHAR( ch, p );
339
/* load the character in the face -- skip unknown or empty ones */
340
af_get_char_index( &metrics->root, ch, &glyph_index, &y_offset );
341
if ( glyph_index == 0 )
343
FT_TRACE5(( " U+%04lX unavailable\n", ch ));
347
error = FT_Load_Glyph( face, glyph_index, FT_LOAD_NO_SCALE );
348
outline = face->glyph->outline;
349
if ( error || outline.n_points <= 0 )
351
FT_TRACE5(( " U+%04lX contains no outlines\n", ch ));
355
/* now compute min or max point indices and coordinates */
356
points = outline.points;
358
best_y = 0; /* make compiler happy */
359
best_contour_first = 0; /* ditto */
360
best_contour_last = 0; /* ditto */
368
for ( nn = 0; nn < outline.n_contours; first = last + 1, nn++ )
370
FT_Int old_best_point = best_point;
374
last = outline.contours[nn];
376
/* Avoid single-point contours since they are never rasterized. */
377
/* In some fonts, they correspond to mark attachment points */
378
/* that are way outside of the glyph's real outline. */
382
if ( AF_LATIN_IS_TOP_BLUE( bs ) )
384
for ( pp = first; pp <= last; pp++ )
385
if ( best_point < 0 || points[pp].y > best_y )
388
best_y = points[pp].y;
393
for ( pp = first; pp <= last; pp++ )
394
if ( best_point < 0 || points[pp].y < best_y )
397
best_y = points[pp].y;
401
if ( best_point != old_best_point )
403
best_contour_first = first;
404
best_contour_last = last;
409
/* now check whether the point belongs to a straight or round */
410
/* segment; we first need to find in which contour the extremum */
411
/* lies, then inspect its previous and next points */
412
if ( best_point >= 0 )
414
FT_Pos best_x = points[best_point].x;
416
FT_Int best_segment_first, best_segment_last;
417
FT_Int best_on_point_first, best_on_point_last;
421
best_segment_first = best_point;
422
best_segment_last = best_point;
424
if ( FT_CURVE_TAG( outline.tags[best_point] ) == FT_CURVE_TAG_ON )
426
best_on_point_first = best_point;
427
best_on_point_last = best_point;
431
best_on_point_first = -1;
432
best_on_point_last = -1;
435
/* look for the previous and next points on the contour */
436
/* that are not on the same Y coordinate, then threshold */
437
/* the `closeness'... */
443
if ( prev > best_contour_first )
446
prev = best_contour_last;
448
dist = FT_ABS( points[prev].y - best_y );
449
/* accept a small distance or a small angle (both values are */
450
/* heuristic; value 20 corresponds to approx. 2.9 degrees) */
452
if ( FT_ABS( points[prev].x - best_x ) <= 20 * dist )
455
best_segment_first = prev;
457
if ( FT_CURVE_TAG( outline.tags[prev] ) == FT_CURVE_TAG_ON )
459
best_on_point_first = prev;
460
if ( best_on_point_last < 0 )
461
best_on_point_last = prev;
464
} while ( prev != best_point );
468
if ( next < best_contour_last )
471
next = best_contour_first;
473
dist = FT_ABS( points[next].y - best_y );
475
if ( FT_ABS( points[next].x - best_x ) <= 20 * dist )
478
best_segment_last = next;
480
if ( FT_CURVE_TAG( outline.tags[next] ) == FT_CURVE_TAG_ON )
482
best_on_point_last = next;
483
if ( best_on_point_first < 0 )
484
best_on_point_first = next;
487
} while ( next != best_point );
489
if ( AF_LATIN_IS_LONG_BLUE( bs ) )
491
/* If this flag is set, we have an additional constraint to */
492
/* get the blue zone distance: Find a segment of the topmost */
493
/* (or bottommost) contour that is longer than a heuristic */
494
/* threshold. This ensures that small bumps in the outline */
495
/* are ignored (for example, the `vertical serifs' found in */
496
/* many Hebrew glyph designs). */
498
/* If this segment is long enough, we are done. Otherwise, */
499
/* search the segment next to the extremum that is long */
500
/* enough, has the same direction, and a not too large */
501
/* vertical distance from the extremum. Note that the */
502
/* algorithm doesn't check whether the found segment is */
503
/* actually the one (vertically) nearest to the extremum. */
505
/* heuristic threshold value */
506
FT_Pos length_threshold = metrics->units_per_em / 25;
509
dist = FT_ABS( points[best_segment_last].x -
510
points[best_segment_first].x );
512
if ( dist < length_threshold &&
513
best_segment_last - best_segment_first + 2 <=
514
best_contour_last - best_contour_first )
516
/* heuristic threshold value */
517
FT_Pos height_threshold = metrics->units_per_em / 4;
526
/* compute direction */
531
if ( prev > best_contour_first )
534
prev = best_contour_last;
536
if ( points[prev].x != best_x )
539
} while ( prev != best_point );
541
/* skip glyph for the degenerate case */
542
if ( prev == best_point )
545
left2right = FT_BOOL( points[prev].x < points[best_point].x );
547
first = best_segment_last;
555
FT_Int p_first, p_last;
560
/* no hit; adjust first point */
563
/* also adjust first and last on point */
564
if ( FT_CURVE_TAG( outline.tags[first] ) ==
579
if ( last < best_contour_last )
582
last = best_contour_first;
584
if ( FT_ABS( best_y - points[first].y ) > height_threshold )
586
/* vertical distance too large */
591
/* same test as above */
592
dist = FT_ABS( points[last].y - points[first].y );
594
if ( FT_ABS( points[last].x - points[first].x ) <=
601
if ( FT_CURVE_TAG( outline.tags[last] ) == FT_CURVE_TAG_ON )
608
l2r = FT_BOOL( points[first].x < points[last].x );
609
d = FT_ABS( points[last].x - points[first].x );
611
if ( l2r == left2right &&
612
d >= length_threshold )
614
/* all constraints are met; update segment after finding */
618
if ( last < best_contour_last )
621
last = best_contour_first;
623
d = FT_ABS( points[last].y - points[first].y );
625
if ( FT_ABS( points[next].x - points[first].x ) <=
628
if ( last > best_contour_first )
631
last = best_contour_last;
637
if ( FT_CURVE_TAG( outline.tags[last] ) ==
645
} while ( last != best_segment_first );
647
best_y = points[first].y;
649
best_segment_first = first;
650
best_segment_last = last;
652
best_on_point_first = p_first;
653
best_on_point_last = p_last;
658
} while ( last != best_segment_first );
662
/* for computing blue zones, we add the y offset as returned */
663
/* by the currently used OpenType feature -- for example, */
664
/* superscript glyphs might be identical to subscript glyphs */
665
/* with a vertical shift */
668
FT_TRACE5(( " U+%04lX: best_y = %5ld", ch, best_y ));
670
/* now set the `round' flag depending on the segment's kind: */
672
/* - if the horizontal distance between the first and last */
673
/* `on' point is larger than upem/8 (value 8 is heuristic) */
674
/* we have a flat segment */
675
/* - if either the first or the last point of the segment is */
676
/* an `off' point, the segment is round, otherwise it is */
678
if ( best_on_point_first >= 0 &&
679
best_on_point_last >= 0 &&
680
(FT_UInt)( FT_ABS( points[best_on_point_last].x -
681
points[best_on_point_first].x ) ) >
682
metrics->units_per_em / 8 )
686
FT_CURVE_TAG( outline.tags[best_segment_first] ) !=
688
FT_CURVE_TAG( outline.tags[best_segment_last] ) !=
691
FT_TRACE5(( " (%s)\n", round ? "round" : "flat" ));
695
rounds[num_rounds++] = best_y;
697
flats[num_flats++] = best_y;
700
if ( num_flats == 0 && num_rounds == 0 )
703
* we couldn't find a single glyph to compute this blue zone,
704
* we will simply ignore it then
706
FT_TRACE5(( " empty\n" ));
710
/* we have computed the contents of the `rounds' and `flats' tables, */
711
/* now determine the reference and overshoot position of the blue -- */
712
/* we simply take the median value after a simple sort */
713
af_sort_pos( num_rounds, rounds );
714
af_sort_pos( num_flats, flats );
716
blue = &axis->blues[axis->blue_count];
717
blue_ref = &blue->ref.org;
718
blue_shoot = &blue->shoot.org;
722
if ( num_flats == 0 )
725
*blue_shoot = rounds[num_rounds / 2];
727
else if ( num_rounds == 0 )
730
*blue_shoot = flats[num_flats / 2];
734
*blue_ref = flats [num_flats / 2];
735
*blue_shoot = rounds[num_rounds / 2];
738
/* there are sometimes problems: if the overshoot position of top */
739
/* zones is under its reference position, or the opposite for bottom */
740
/* zones. We must thus check everything there and correct the errors */
741
if ( *blue_shoot != *blue_ref )
743
FT_Pos ref = *blue_ref;
744
FT_Pos shoot = *blue_shoot;
745
FT_Bool over_ref = FT_BOOL( shoot > ref );
748
if ( AF_LATIN_IS_TOP_BLUE( bs ) ^ over_ref )
751
*blue_shoot = ( shoot + ref ) / 2;
753
FT_TRACE5(( " [overshoot smaller than reference,"
754
" taking mean value]\n" ));
759
if ( AF_LATIN_IS_TOP_BLUE( bs ) )
760
blue->flags |= AF_LATIN_BLUE_TOP;
763
* The following flag is used later to adjust the y and x scales
764
* in order to optimize the pixel grid alignment of the top of small
767
if ( AF_LATIN_IS_X_HEIGHT_BLUE( bs ) )
768
blue->flags |= AF_LATIN_BLUE_ADJUSTMENT;
770
FT_TRACE5(( " -> reference = %ld\n"
771
" overshoot = %ld\n",
772
*blue_ref, *blue_shoot ));
781
/* Check whether all ASCII digits have the same advance width. */
784
af_latin_metrics_check_digits( AF_LatinMetrics metrics,
788
FT_Bool started = 0, same_width = 1;
789
FT_Fixed advance, old_advance = 0;
792
/* digit `0' is 0x30 in all supported charmaps */
793
for ( i = 0x30; i <= 0x39; i++ )
795
FT_ULong glyph_index;
799
af_get_char_index( &metrics->root, i, &glyph_index, &y_offset );
800
if ( glyph_index == 0 )
803
if ( FT_Get_Advance( face, glyph_index,
806
FT_LOAD_IGNORE_TRANSFORM,
812
if ( advance != old_advance )
820
old_advance = advance;
825
metrics->root.digits_have_same_width = same_width;
829
/* Initialize global metrics. */
831
FT_LOCAL_DEF( FT_Error )
832
af_latin_metrics_init( AF_LatinMetrics metrics,
835
FT_CharMap oldmap = face->charmap;
838
metrics->units_per_em = face->units_per_EM;
840
if ( !FT_Select_Charmap( face, FT_ENCODING_UNICODE ) )
842
af_latin_metrics_init_widths( metrics, face );
843
af_latin_metrics_init_blues( metrics, face );
844
af_latin_metrics_check_digits( metrics, face );
847
FT_Set_Charmap( face, oldmap );
852
/* Adjust scaling value, then scale and shift widths */
853
/* and blue zones (if applicable) for given dimension. */
856
af_latin_metrics_scale_dim( AF_LatinMetrics metrics,
866
if ( dim == AF_DIMENSION_HORZ )
868
scale = scaler->x_scale;
869
delta = scaler->x_delta;
873
scale = scaler->y_scale;
874
delta = scaler->y_delta;
877
axis = &metrics->axis[dim];
879
if ( axis->org_scale == scale && axis->org_delta == delta )
882
axis->org_scale = scale;
883
axis->org_delta = delta;
886
* correct X and Y scale to optimize the alignment of the top of small
887
* letters to the pixel grid
890
AF_LatinAxis Axis = &metrics->axis[AF_DIMENSION_VERT];
891
AF_LatinBlue blue = NULL;
894
for ( nn = 0; nn < Axis->blue_count; nn++ )
896
if ( Axis->blues[nn].flags & AF_LATIN_BLUE_ADJUSTMENT )
898
blue = &Axis->blues[nn];
912
scaled = FT_MulFix( blue->shoot.org, scaler->y_scale );
913
ppem = metrics->root.scaler.face->size->metrics.x_ppem;
914
limit = metrics->root.globals->increase_x_height;
917
/* if the `increase-x-height' property is active, */
918
/* we round up much more often */
921
ppem >= AF_PROP_INCREASE_X_HEIGHT_MIN )
924
fitted = ( scaled + threshold ) & ~63;
926
if ( scaled != fitted )
929
if ( dim == AF_DIMENSION_HORZ )
931
if ( fitted < scaled )
932
scale -= scale / 50; /* scale *= 0.98 */
936
if ( dim == AF_DIMENSION_VERT )
938
scale = FT_MulDiv( scale, fitted, scaled );
941
"af_latin_metrics_scale_dim:"
942
" x height alignment (style `%s'):\n"
944
" vertical scaling changed from %.4f to %.4f (by %d%%)\n"
946
af_style_names[metrics->root.style_class->style],
947
axis->org_scale / 65536.0,
949
( fitted - scaled ) * 100 / scaled ));
958
if ( dim == AF_DIMENSION_HORZ )
960
metrics->root.scaler.x_scale = scale;
961
metrics->root.scaler.x_delta = delta;
965
metrics->root.scaler.y_scale = scale;
966
metrics->root.scaler.y_delta = delta;
969
FT_TRACE5(( "%s widths (style `%s')\n",
970
dim == AF_DIMENSION_HORZ ? "horizontal" : "vertical",
971
af_style_names[metrics->root.style_class->style] ));
973
/* scale the widths */
974
for ( nn = 0; nn < axis->width_count; nn++ )
976
AF_Width width = axis->widths + nn;
979
width->cur = FT_MulFix( width->org, scale );
980
width->fit = width->cur;
982
FT_TRACE5(( " %d scaled to %.2f\n",
984
width->cur / 64.0 ));
989
/* an extra-light axis corresponds to a standard width that is */
990
/* smaller than 5/8 pixels */
992
(FT_Bool)( FT_MulFix( axis->standard_width, scale ) < 32 + 8 );
994
#ifdef FT_DEBUG_LEVEL_TRACE
995
if ( axis->extra_light )
996
FT_TRACE5(( "`%s' style is extra light (at current resolution)\n"
998
af_style_names[metrics->root.style_class->style] ));
1001
if ( dim == AF_DIMENSION_VERT )
1003
FT_TRACE5(( "blue zones (style `%s')\n",
1004
af_style_names[metrics->root.style_class->style] ));
1006
/* scale the blue zones */
1007
for ( nn = 0; nn < axis->blue_count; nn++ )
1009
AF_LatinBlue blue = &axis->blues[nn];
1013
blue->ref.cur = FT_MulFix( blue->ref.org, scale ) + delta;
1014
blue->ref.fit = blue->ref.cur;
1015
blue->shoot.cur = FT_MulFix( blue->shoot.org, scale ) + delta;
1016
blue->shoot.fit = blue->shoot.cur;
1017
blue->flags &= ~AF_LATIN_BLUE_ACTIVE;
1019
/* a blue zone is only active if it is less than 3/4 pixels tall */
1020
dist = FT_MulFix( blue->ref.org - blue->shoot.org, scale );
1021
if ( dist <= 48 && dist >= -48 )
1029
/* use discrete values for blue zone widths */
1033
/* generic, original code */
1034
delta1 = blue->shoot.org - blue->ref.org;
1039
delta2 = FT_MulFix( delta2, scale );
1043
else if ( delta2 < 64 )
1044
delta2 = 32 + ( ( ( delta2 - 32 ) + 16 ) & ~31 );
1046
delta2 = FT_PIX_ROUND( delta2 );
1051
blue->ref.fit = FT_PIX_ROUND( blue->ref.cur );
1052
blue->shoot.fit = blue->ref.fit + delta2;
1056
/* simplified version due to abs(dist) <= 48 */
1063
else if ( delta2 < 48 )
1071
blue->ref.fit = FT_PIX_ROUND( blue->ref.cur );
1072
blue->shoot.fit = blue->ref.fit - delta2;
1076
blue->flags |= AF_LATIN_BLUE_ACTIVE;
1078
FT_TRACE5(( " reference %d: %d scaled to %.2f%s\n"
1079
" overshoot %d: %d scaled to %.2f%s\n",
1082
blue->ref.fit / 64.0,
1083
blue->flags & AF_LATIN_BLUE_ACTIVE ? ""
1087
blue->shoot.fit / 64.0,
1088
blue->flags & AF_LATIN_BLUE_ACTIVE ? ""
1096
/* Scale global values in both directions. */
1098
FT_LOCAL_DEF( void )
1099
af_latin_metrics_scale( AF_LatinMetrics metrics,
1102
metrics->root.scaler.render_mode = scaler->render_mode;
1103
metrics->root.scaler.face = scaler->face;
1104
metrics->root.scaler.flags = scaler->flags;
1106
af_latin_metrics_scale_dim( metrics, scaler, AF_DIMENSION_HORZ );
1107
af_latin_metrics_scale_dim( metrics, scaler, AF_DIMENSION_VERT );
1111
/*************************************************************************/
1112
/*************************************************************************/
1114
/***** L A T I N G L Y P H A N A L Y S I S *****/
1116
/*************************************************************************/
1117
/*************************************************************************/
1120
/* Walk over all contours and compute its segments. */
1122
FT_LOCAL_DEF( FT_Error )
1123
af_latin_hints_compute_segments( AF_GlyphHints hints,
1126
AF_AxisHints axis = &hints->axis[dim];
1127
FT_Memory memory = hints->memory;
1128
FT_Error error = FT_Err_Ok;
1129
AF_Segment segment = NULL;
1131
AF_Point* contour = hints->contours;
1132
AF_Point* contour_limit = contour + hints->num_contours;
1133
AF_Direction major_dir, segment_dir;
1138
seg0.flags = AF_EDGE_NORMAL;
1140
major_dir = (AF_Direction)FT_ABS( axis->major_dir );
1141
segment_dir = major_dir;
1143
axis->num_segments = 0;
1145
/* set up (u,v) in each point */
1146
if ( dim == AF_DIMENSION_HORZ )
1148
AF_Point point = hints->points;
1149
AF_Point limit = point + hints->num_points;
1152
for ( ; point < limit; point++ )
1154
point->u = point->fx;
1155
point->v = point->fy;
1160
AF_Point point = hints->points;
1161
AF_Point limit = point + hints->num_points;
1164
for ( ; point < limit; point++ )
1166
point->u = point->fy;
1167
point->v = point->fx;
1171
/* do each contour separately */
1172
for ( ; contour < contour_limit; contour++ )
1174
AF_Point point = contour[0];
1175
AF_Point last = point->prev;
1177
FT_Pos min_pos = 32000; /* minimum segment pos != min_coord */
1178
FT_Pos max_pos = -32000; /* maximum segment pos != max_coord */
1182
if ( point == last ) /* skip singletons -- just in case */
1185
if ( FT_ABS( last->out_dir ) == major_dir &&
1186
FT_ABS( point->out_dir ) == major_dir )
1188
/* we are already on an edge, try to locate its start */
1193
point = point->prev;
1194
if ( FT_ABS( point->out_dir ) != major_dir )
1196
point = point->next;
1199
if ( point == last )
1220
if ( point->out_dir != segment_dir || point == last )
1222
/* we are just leaving an edge; record a new segment! */
1223
segment->last = point;
1224
segment->pos = (FT_Short)( ( min_pos + max_pos ) >> 1 );
1226
/* a segment is round if either its first or last point */
1227
/* is a control point */
1228
if ( ( segment->first->flags | point->flags ) &
1230
segment->flags |= AF_EDGE_ROUND;
1232
/* compute segment size */
1233
min_pos = max_pos = point->v;
1235
v = segment->first->v;
1241
segment->min_coord = (FT_Short)min_pos;
1242
segment->max_coord = (FT_Short)max_pos;
1243
segment->height = (FT_Short)( segment->max_coord -
1244
segment->min_coord );
1252
/* now exit if we are at the start/end point */
1253
if ( point == last )
1260
if ( !on_edge && FT_ABS( point->out_dir ) == major_dir )
1262
/* this is the start of a new segment! */
1263
segment_dir = (AF_Direction)point->out_dir;
1265
/* clear all segment fields */
1266
error = af_axis_hints_new_segment( axis, memory, &segment );
1271
segment->dir = (FT_Char)segment_dir;
1272
min_pos = max_pos = point->u;
1273
segment->first = point;
1274
segment->last = point;
1278
point = point->next;
1284
/* now slightly increase the height of segments if this makes */
1285
/* sense -- this is used to better detect and ignore serifs */
1287
AF_Segment segments = axis->segments;
1288
AF_Segment segments_end = segments + axis->num_segments;
1291
for ( segment = segments; segment < segments_end; segment++ )
1293
AF_Point first = segment->first;
1294
AF_Point last = segment->last;
1295
FT_Pos first_v = first->v;
1296
FT_Pos last_v = last->v;
1299
if ( first == last )
1302
if ( first_v < last_v )
1308
if ( p->v < first_v )
1309
segment->height = (FT_Short)( segment->height +
1310
( ( first_v - p->v ) >> 1 ) );
1313
if ( p->v > last_v )
1314
segment->height = (FT_Short)( segment->height +
1315
( ( p->v - last_v ) >> 1 ) );
1323
if ( p->v > first_v )
1324
segment->height = (FT_Short)( segment->height +
1325
( ( p->v - first_v ) >> 1 ) );
1328
if ( p->v < last_v )
1329
segment->height = (FT_Short)( segment->height +
1330
( ( last_v - p->v ) >> 1 ) );
1340
/* Link segments to form stems and serifs. */
1342
FT_LOCAL_DEF( void )
1343
af_latin_hints_link_segments( AF_GlyphHints hints,
1346
AF_AxisHints axis = &hints->axis[dim];
1347
AF_Segment segments = axis->segments;
1348
AF_Segment segment_limit = segments + axis->num_segments;
1349
FT_Pos len_threshold, len_score;
1350
AF_Segment seg1, seg2;
1353
len_threshold = AF_LATIN_CONSTANT( hints->metrics, 8 );
1354
if ( len_threshold == 0 )
1357
len_score = AF_LATIN_CONSTANT( hints->metrics, 6000 );
1359
/* now compare each segment to the others */
1360
for ( seg1 = segments; seg1 < segment_limit; seg1++ )
1362
/* the fake segments are introduced to hint the metrics -- */
1363
/* we must never link them to anything */
1364
if ( seg1->dir != axis->major_dir || seg1->first == seg1->last )
1367
/* search for stems having opposite directions, */
1368
/* with seg1 to the `left' of seg2 */
1369
for ( seg2 = segments; seg2 < segment_limit; seg2++ )
1371
FT_Pos pos1 = seg1->pos;
1372
FT_Pos pos2 = seg2->pos;
1375
if ( seg1->dir + seg2->dir == 0 && pos2 > pos1 )
1377
/* compute distance between the two segments */
1378
FT_Pos dist = pos2 - pos1;
1379
FT_Pos min = seg1->min_coord;
1380
FT_Pos max = seg1->max_coord;
1384
if ( min < seg2->min_coord )
1385
min = seg2->min_coord;
1387
if ( max > seg2->max_coord )
1388
max = seg2->max_coord;
1390
/* compute maximum coordinate difference of the two segments */
1392
if ( len >= len_threshold )
1394
/* small coordinate differences cause a higher score, and */
1395
/* segments with a greater distance cause a higher score also */
1396
score = dist + len_score / len;
1398
/* and we search for the smallest score */
1399
/* of the sum of the two values */
1400
if ( score < seg1->score )
1402
seg1->score = score;
1406
if ( score < seg2->score )
1408
seg2->score = score;
1416
/* now compute the `serif' segments, cf. explanations in `afhints.h' */
1417
for ( seg1 = segments; seg1 < segment_limit; seg1++ )
1423
if ( seg2->link != seg1 )
1426
seg1->serif = seg2->link;
1433
/* Link segments to edges, using feature analysis for selection. */
1435
FT_LOCAL_DEF( FT_Error )
1436
af_latin_hints_compute_edges( AF_GlyphHints hints,
1439
AF_AxisHints axis = &hints->axis[dim];
1440
FT_Error error = FT_Err_Ok;
1441
FT_Memory memory = hints->memory;
1442
AF_LatinAxis laxis = &((AF_LatinMetrics)hints->metrics)->axis[dim];
1444
AF_Segment segments = axis->segments;
1445
AF_Segment segment_limit = segments + axis->num_segments;
1449
AF_Direction up_dir;
1452
FT_Pos edge_distance_threshold;
1453
FT_Pos segment_length_threshold;
1456
axis->num_edges = 0;
1458
scale = ( dim == AF_DIMENSION_HORZ ) ? hints->x_scale
1462
up_dir = ( dim == AF_DIMENSION_HORZ ) ? AF_DIR_UP
1467
* We ignore all segments that are less than 1 pixel in length
1468
* to avoid many problems with serif fonts. We compute the
1469
* corresponding threshold in font units.
1471
if ( dim == AF_DIMENSION_HORZ )
1472
segment_length_threshold = FT_DivFix( 64, hints->y_scale );
1474
segment_length_threshold = 0;
1476
/*********************************************************************/
1478
/* We begin by generating a sorted table of edges for the current */
1479
/* direction. To do so, we simply scan each segment and try to find */
1480
/* an edge in our table that corresponds to its position. */
1482
/* If no edge is found, we create and insert a new edge in the */
1483
/* sorted table. Otherwise, we simply add the segment to the edge's */
1484
/* list which gets processed in the second step to compute the */
1485
/* edge's properties. */
1487
/* Note that the table of edges is sorted along the segment/edge */
1490
/*********************************************************************/
1492
/* assure that edge distance threshold is at most 0.25px */
1493
edge_distance_threshold = FT_MulFix( laxis->edge_distance_threshold,
1495
if ( edge_distance_threshold > 64 / 4 )
1496
edge_distance_threshold = 64 / 4;
1498
edge_distance_threshold = FT_DivFix( edge_distance_threshold,
1501
for ( seg = segments; seg < segment_limit; seg++ )
1503
AF_Edge found = NULL;
1507
if ( seg->height < segment_length_threshold )
1510
/* A special case for serif edges: If they are smaller than */
1511
/* 1.5 pixels we ignore them. */
1513
2 * seg->height < 3 * segment_length_threshold )
1516
/* look for an edge corresponding to the segment */
1517
for ( ee = 0; ee < axis->num_edges; ee++ )
1519
AF_Edge edge = axis->edges + ee;
1523
dist = seg->pos - edge->fpos;
1527
if ( dist < edge_distance_threshold && edge->dir == seg->dir )
1539
/* insert a new edge in the list and */
1540
/* sort according to the position */
1541
error = af_axis_hints_new_edge( axis, seg->pos,
1542
(AF_Direction)seg->dir,
1547
/* add the segment to the new edge's list */
1552
edge->dir = seg->dir;
1553
edge->fpos = seg->pos;
1554
edge->opos = FT_MulFix( seg->pos, scale );
1555
edge->pos = edge->opos;
1556
seg->edge_next = seg;
1560
/* if an edge was found, simply add the segment to the edge's */
1562
seg->edge_next = found->first;
1563
found->last->edge_next = seg;
1569
/******************************************************************/
1571
/* Good, we now compute each edge's properties according to the */
1572
/* segments found on its position. Basically, these are */
1574
/* - the edge's main direction */
1575
/* - stem edge, serif edge or both (which defaults to stem then) */
1576
/* - rounded edge, straight or both (which defaults to straight) */
1577
/* - link for edge */
1579
/******************************************************************/
1581
/* first of all, set the `edge' field in each segment -- this is */
1582
/* required in order to compute edge links */
1585
* Note that removing this loop and setting the `edge' field of each
1586
* segment directly in the code above slows down execution speed for
1587
* some reasons on platforms like the Sun.
1590
AF_Edge edges = axis->edges;
1591
AF_Edge edge_limit = edges + axis->num_edges;
1595
for ( edge = edges; edge < edge_limit; edge++ )
1602
seg = seg->edge_next;
1604
} while ( seg != edge->first );
1607
/* now compute each edge properties */
1608
for ( edge = edges; edge < edge_limit; edge++ )
1610
FT_Int is_round = 0; /* does it contain round segments? */
1611
FT_Int is_straight = 0; /* does it contain straight segments? */
1613
FT_Pos ups = 0; /* number of upwards segments */
1614
FT_Pos downs = 0; /* number of downwards segments */
1625
/* check for roundness of segment */
1626
if ( seg->flags & AF_EDGE_ROUND )
1632
/* check for segment direction */
1633
if ( seg->dir == up_dir )
1634
ups += seg->max_coord - seg->min_coord;
1636
downs += seg->max_coord - seg->min_coord;
1639
/* check for links -- if seg->serif is set, then seg->link must */
1641
is_serif = (FT_Bool)( seg->serif &&
1643
seg->serif->edge != edge );
1645
if ( ( seg->link && seg->link->edge != NULL ) || is_serif )
1657
edge2 = edge->serif;
1666
edge_delta = edge->fpos - edge2->fpos;
1667
if ( edge_delta < 0 )
1668
edge_delta = -edge_delta;
1670
seg_delta = seg->pos - seg2->pos;
1671
if ( seg_delta < 0 )
1672
seg_delta = -seg_delta;
1674
if ( seg_delta < edge_delta )
1682
edge->serif = edge2;
1683
edge2->flags |= AF_EDGE_SERIF;
1689
seg = seg->edge_next;
1691
} while ( seg != edge->first );
1693
/* set the round/straight flags */
1694
edge->flags = AF_EDGE_NORMAL;
1696
if ( is_round > 0 && is_round >= is_straight )
1697
edge->flags |= AF_EDGE_ROUND;
1700
/* set the edge's main direction */
1701
edge->dir = AF_DIR_NONE;
1704
edge->dir = (FT_Char)up_dir;
1706
else if ( ups < downs )
1707
edge->dir = (FT_Char)-up_dir;
1709
else if ( ups == downs )
1710
edge->dir = 0; /* both up and down! */
1713
/* get rid of serifs if link is set */
1714
/* XXX: This gets rid of many unpleasant artefacts! */
1715
/* Example: the `c' in cour.pfa at size 13 */
1717
if ( edge->serif && edge->link )
1727
/* Detect segments and edges for given dimension. */
1729
FT_LOCAL_DEF( FT_Error )
1730
af_latin_hints_detect_features( AF_GlyphHints hints,
1736
error = af_latin_hints_compute_segments( hints, dim );
1739
af_latin_hints_link_segments( hints, dim );
1741
error = af_latin_hints_compute_edges( hints, dim );
1748
/* Compute all edges which lie within blue zones. */
1750
FT_LOCAL_DEF( void )
1751
af_latin_hints_compute_blue_edges( AF_GlyphHints hints,
1752
AF_LatinMetrics metrics )
1754
AF_AxisHints axis = &hints->axis[AF_DIMENSION_VERT];
1755
AF_Edge edge = axis->edges;
1756
AF_Edge edge_limit = edge + axis->num_edges;
1757
AF_LatinAxis latin = &metrics->axis[AF_DIMENSION_VERT];
1758
FT_Fixed scale = latin->scale;
1761
/* compute which blue zones are active, i.e. have their scaled */
1762
/* size < 3/4 pixels */
1764
/* for each horizontal edge search the blue zone which is closest */
1765
for ( ; edge < edge_limit; edge++ )
1768
AF_Width best_blue = NULL;
1769
FT_Pos best_dist; /* initial threshold */
1772
/* compute the initial threshold as a fraction of the EM size */
1773
/* (the value 40 is heuristic) */
1774
best_dist = FT_MulFix( metrics->units_per_em / 40, scale );
1776
/* assure a minimum distance of 0.5px */
1777
if ( best_dist > 64 / 2 )
1780
for ( bb = 0; bb < latin->blue_count; bb++ )
1782
AF_LatinBlue blue = latin->blues + bb;
1783
FT_Bool is_top_blue, is_major_dir;
1786
/* skip inactive blue zones (i.e., those that are too large) */
1787
if ( !( blue->flags & AF_LATIN_BLUE_ACTIVE ) )
1790
/* if it is a top zone, check for right edges -- if it is a bottom */
1791
/* zone, check for left edges */
1793
/* of course, that's for TrueType */
1794
is_top_blue = (FT_Byte)( ( blue->flags & AF_LATIN_BLUE_TOP ) != 0 );
1795
is_major_dir = FT_BOOL( edge->dir == axis->major_dir );
1797
/* if it is a top zone, the edge must be against the major */
1798
/* direction; if it is a bottom zone, it must be in the major */
1800
if ( is_top_blue ^ is_major_dir )
1805
/* first of all, compare it to the reference position */
1806
dist = edge->fpos - blue->ref.org;
1810
dist = FT_MulFix( dist, scale );
1811
if ( dist < best_dist )
1814
best_blue = &blue->ref;
1817
/* now compare it to the overshoot position and check whether */
1818
/* the edge is rounded, and whether the edge is over the */
1819
/* reference position of a top zone, or under the reference */
1820
/* position of a bottom zone */
1821
if ( edge->flags & AF_EDGE_ROUND && dist != 0 )
1823
FT_Bool is_under_ref = FT_BOOL( edge->fpos < blue->ref.org );
1826
if ( is_top_blue ^ is_under_ref )
1828
dist = edge->fpos - blue->shoot.org;
1832
dist = FT_MulFix( dist, scale );
1833
if ( dist < best_dist )
1836
best_blue = &blue->shoot;
1844
edge->blue_edge = best_blue;
1849
/* Initalize hinting engine. */
1852
af_latin_hints_init( AF_GlyphHints hints,
1853
AF_LatinMetrics metrics )
1855
FT_Render_Mode mode;
1856
FT_UInt32 scaler_flags, other_flags;
1857
FT_Face face = metrics->root.scaler.face;
1860
af_glyph_hints_rescale( hints, (AF_StyleMetrics)metrics );
1863
* correct x_scale and y_scale if needed, since they may have
1864
* been modified by `af_latin_metrics_scale_dim' above
1866
hints->x_scale = metrics->axis[AF_DIMENSION_HORZ].scale;
1867
hints->x_delta = metrics->axis[AF_DIMENSION_HORZ].delta;
1868
hints->y_scale = metrics->axis[AF_DIMENSION_VERT].scale;
1869
hints->y_delta = metrics->axis[AF_DIMENSION_VERT].delta;
1871
/* compute flags depending on render mode, etc. */
1872
mode = metrics->root.scaler.render_mode;
1874
#if 0 /* #ifdef AF_CONFIG_OPTION_USE_WARPER */
1875
if ( mode == FT_RENDER_MODE_LCD || mode == FT_RENDER_MODE_LCD_V )
1876
metrics->root.scaler.render_mode = mode = FT_RENDER_MODE_NORMAL;
1879
scaler_flags = hints->scaler_flags;
1883
* We snap the width of vertical stems for the monochrome and
1884
* horizontal LCD rendering targets only.
1886
if ( mode == FT_RENDER_MODE_MONO || mode == FT_RENDER_MODE_LCD )
1887
other_flags |= AF_LATIN_HINTS_HORZ_SNAP;
1890
* We snap the width of horizontal stems for the monochrome and
1891
* vertical LCD rendering targets only.
1893
if ( mode == FT_RENDER_MODE_MONO || mode == FT_RENDER_MODE_LCD_V )
1894
other_flags |= AF_LATIN_HINTS_VERT_SNAP;
1897
* We adjust stems to full pixels only if we don't use the `light' mode.
1899
if ( mode != FT_RENDER_MODE_LIGHT )
1900
other_flags |= AF_LATIN_HINTS_STEM_ADJUST;
1902
if ( mode == FT_RENDER_MODE_MONO )
1903
other_flags |= AF_LATIN_HINTS_MONO;
1906
* In `light' hinting mode we disable horizontal hinting completely.
1907
* We also do it if the face is italic.
1909
if ( mode == FT_RENDER_MODE_LIGHT ||
1910
( face->style_flags & FT_STYLE_FLAG_ITALIC ) != 0 )
1911
scaler_flags |= AF_SCALER_FLAG_NO_HORIZONTAL;
1913
hints->scaler_flags = scaler_flags;
1914
hints->other_flags = other_flags;
1920
/*************************************************************************/
1921
/*************************************************************************/
1923
/***** L A T I N G L Y P H G R I D - F I T T I N G *****/
1925
/*************************************************************************/
1926
/*************************************************************************/
1928
/* Snap a given width in scaled coordinates to one of the */
1929
/* current standard widths. */
1932
af_latin_snap_width( AF_Width widths,
1937
FT_Pos best = 64 + 32 + 2;
1938
FT_Pos reference = width;
1942
for ( n = 0; n < count; n++ )
1959
scaled = FT_PIX_ROUND( reference );
1961
if ( width >= reference )
1963
if ( width < scaled + 48 )
1968
if ( width > scaled - 48 )
1976
/* Compute the snapped width of a given stem, ignoring very thin ones. */
1977
/* There is a lot of voodoo in this function; changing the hard-coded */
1978
/* parameters influence the whole hinting process. */
1981
af_latin_compute_stem_width( AF_GlyphHints hints,
1984
AF_Edge_Flags base_flags,
1985
AF_Edge_Flags stem_flags )
1987
AF_LatinMetrics metrics = (AF_LatinMetrics)hints->metrics;
1988
AF_LatinAxis axis = &metrics->axis[dim];
1989
FT_Pos dist = width;
1991
FT_Int vertical = ( dim == AF_DIMENSION_VERT );
1994
if ( !AF_LATIN_HINTS_DO_STEM_ADJUST( hints ) ||
2004
if ( ( vertical && !AF_LATIN_HINTS_DO_VERT_SNAP( hints ) ) ||
2005
( !vertical && !AF_LATIN_HINTS_DO_HORZ_SNAP( hints ) ) )
2007
/* smooth hinting process: very lightly quantize the stem width */
2009
/* leave the widths of serifs alone */
2010
if ( ( stem_flags & AF_EDGE_SERIF ) &&
2015
else if ( base_flags & AF_EDGE_ROUND )
2020
else if ( dist < 56 )
2023
if ( axis->width_count > 0 )
2028
/* compare to standard width */
2029
delta = dist - axis->widths[0].cur;
2036
dist = axis->widths[0].cur;
2043
if ( dist < 3 * 64 )
2051
else if ( delta < 32 )
2054
else if ( delta < 54 )
2061
dist = ( dist + 32 ) & ~63;
2066
/* strong hinting process: snap the stem width to integer pixels */
2068
FT_Pos org_dist = dist;
2071
dist = af_latin_snap_width( axis->widths, axis->width_count, dist );
2075
/* in the case of vertical hinting, always round */
2076
/* the stem heights to integer pixels */
2079
dist = ( dist + 16 ) & ~63;
2085
if ( AF_LATIN_HINTS_DO_MONO( hints ) )
2087
/* monochrome horizontal hinting: snap widths to integer pixels */
2088
/* with a different threshold */
2093
dist = ( dist + 32 ) & ~63;
2097
/* for horizontal anti-aliased hinting, we adopt a more subtle */
2098
/* approach: we strengthen small stems, round stems whose size */
2099
/* is between 1 and 2 pixels to an integer, otherwise nothing */
2102
dist = ( dist + 64 ) >> 1;
2104
else if ( dist < 128 )
2106
/* We only round to an integer width if the corresponding */
2107
/* distortion is less than 1/4 pixel. Otherwise this */
2108
/* makes everything worse since the diagonals, which are */
2109
/* not hinted, appear a lot bolder or thinner than the */
2110
/* vertical stems. */
2115
dist = ( dist + 22 ) & ~63;
2116
delta = dist - org_dist;
2124
dist = ( dist + 64 ) >> 1;
2128
/* round otherwise to prevent color fringes in LCD mode */
2129
dist = ( dist + 32 ) & ~63;
2142
/* Align one stem edge relative to the previous stem edge. */
2145
af_latin_align_linked_edge( AF_GlyphHints hints,
2150
FT_Pos dist = stem_edge->opos - base_edge->opos;
2152
FT_Pos fitted_width = af_latin_compute_stem_width(
2154
(AF_Edge_Flags)base_edge->flags,
2155
(AF_Edge_Flags)stem_edge->flags );
2158
stem_edge->pos = base_edge->pos + fitted_width;
2160
FT_TRACE5(( " LINK: edge %d (opos=%.2f) linked to %.2f,"
2161
" dist was %.2f, now %.2f\n",
2162
stem_edge-hints->axis[dim].edges, stem_edge->opos / 64.0,
2163
stem_edge->pos / 64.0, dist / 64.0, fitted_width / 64.0 ));
2167
/* Shift the coordinates of the `serif' edge by the same amount */
2168
/* as the corresponding `base' edge has been moved already. */
2171
af_latin_align_serif_edge( AF_GlyphHints hints,
2177
serif->pos = base->pos + ( serif->opos - base->opos );
2181
/*************************************************************************/
2182
/*************************************************************************/
2183
/*************************************************************************/
2185
/**** E D G E H I N T I N G ****/
2187
/*************************************************************************/
2188
/*************************************************************************/
2189
/*************************************************************************/
2192
/* The main grid-fitting routine. */
2194
FT_LOCAL_DEF( void )
2195
af_latin_hint_edges( AF_GlyphHints hints,
2198
AF_AxisHints axis = &hints->axis[dim];
2199
AF_Edge edges = axis->edges;
2200
AF_Edge edge_limit = edges + axis->num_edges;
2203
AF_Edge anchor = NULL;
2204
FT_Int has_serifs = 0;
2206
#ifdef FT_DEBUG_LEVEL_TRACE
2207
FT_UInt num_actions = 0;
2211
FT_TRACE5(( "latin %s edge hinting (style `%s')\n",
2212
dim == AF_DIMENSION_VERT ? "horizontal" : "vertical",
2213
af_style_names[hints->metrics->style_class->style] ));
2215
/* we begin by aligning all stems relative to the blue zone */
2216
/* if needed -- that's only for horizontal edges */
2218
if ( dim == AF_DIMENSION_VERT && AF_HINTS_DO_BLUES( hints ) )
2220
for ( edge = edges; edge < edge_limit; edge++ )
2223
AF_Edge edge1, edge2; /* these edges form the stem to check */
2226
if ( edge->flags & AF_EDGE_DONE )
2229
blue = edge->blue_edge;
2236
/* flip edges if the other stem is aligned to a blue zone */
2237
else if ( edge2 && edge2->blue_edge )
2239
blue = edge2->blue_edge;
2247
#ifdef FT_DEBUG_LEVEL_TRACE
2249
FT_TRACE5(( " BLUE_ANCHOR: edge %d (opos=%.2f) snapped to %.2f,"
2250
" was %.2f (anchor=edge %d)\n",
2251
edge1 - edges, edge1->opos / 64.0, blue->fit / 64.0,
2252
edge1->pos / 64.0, edge - edges ));
2254
FT_TRACE5(( " BLUE: edge %d (opos=%.2f) snapped to %.2f,"
2256
edge1 - edges, edge1->opos / 64.0, blue->fit / 64.0,
2257
edge1->pos / 64.0 ));
2262
edge1->pos = blue->fit;
2263
edge1->flags |= AF_EDGE_DONE;
2265
if ( edge2 && !edge2->blue_edge )
2267
af_latin_align_linked_edge( hints, dim, edge1, edge2 );
2268
edge2->flags |= AF_EDGE_DONE;
2270
#ifdef FT_DEBUG_LEVEL_TRACE
2280
/* now we align all other stem edges, trying to maintain the */
2281
/* relative order of stems in the glyph */
2282
for ( edge = edges; edge < edge_limit; edge++ )
2287
if ( edge->flags & AF_EDGE_DONE )
2290
/* skip all non-stem edges */
2298
/* now align the stem */
2300
/* this should not happen, but it's better to be safe */
2301
if ( edge2->blue_edge )
2303
FT_TRACE5(( " ASSERTION FAILED for edge %d\n", edge2-edges ));
2305
af_latin_align_linked_edge( hints, dim, edge2, edge );
2306
edge->flags |= AF_EDGE_DONE;
2308
#ifdef FT_DEBUG_LEVEL_TRACE
2316
/* if we reach this if clause, no stem has been aligned yet */
2318
FT_Pos org_len, org_center, cur_len;
2319
FT_Pos cur_pos1, error1, error2, u_off, d_off;
2322
org_len = edge2->opos - edge->opos;
2323
cur_len = af_latin_compute_stem_width(
2324
hints, dim, org_len,
2325
(AF_Edge_Flags)edge->flags,
2326
(AF_Edge_Flags)edge2->flags );
2328
/* some voodoo to specially round edges for small stem widths; */
2329
/* the idea is to align the center of a stem, then shifting */
2330
/* the stem edges to suitable positions */
2331
if ( cur_len <= 64 )
2339
/* 1px < width < 1.5px */
2346
org_center = edge->opos + ( org_len >> 1 );
2347
cur_pos1 = FT_PIX_ROUND( org_center );
2349
error1 = org_center - ( cur_pos1 - u_off );
2353
error2 = org_center - ( cur_pos1 + d_off );
2357
if ( error1 < error2 )
2362
edge->pos = cur_pos1 - cur_len / 2;
2363
edge2->pos = edge->pos + cur_len;
2366
edge->pos = FT_PIX_ROUND( edge->opos );
2369
edge->flags |= AF_EDGE_DONE;
2371
FT_TRACE5(( " ANCHOR: edge %d (opos=%.2f) and %d (opos=%.2f)"
2372
" snapped to %.2f and %.2f\n",
2373
edge - edges, edge->opos / 64.0,
2374
edge2 - edges, edge2->opos / 64.0,
2375
edge->pos / 64.0, edge2->pos / 64.0 ));
2377
af_latin_align_linked_edge( hints, dim, edge, edge2 );
2379
#ifdef FT_DEBUG_LEVEL_TRACE
2385
FT_Pos org_pos, org_len, org_center, cur_len;
2386
FT_Pos cur_pos1, cur_pos2, delta1, delta2;
2389
org_pos = anchor->pos + ( edge->opos - anchor->opos );
2390
org_len = edge2->opos - edge->opos;
2391
org_center = org_pos + ( org_len >> 1 );
2393
cur_len = af_latin_compute_stem_width(
2394
hints, dim, org_len,
2395
(AF_Edge_Flags)edge->flags,
2396
(AF_Edge_Flags)edge2->flags );
2398
if ( edge2->flags & AF_EDGE_DONE )
2400
FT_TRACE5(( " ADJUST: edge %d (pos=%.2f) moved to %.2f\n",
2401
edge - edges, edge->pos / 64.0,
2402
( edge2->pos - cur_len ) / 64.0 ));
2404
edge->pos = edge2->pos - cur_len;
2407
else if ( cur_len < 96 )
2409
FT_Pos u_off, d_off;
2412
cur_pos1 = FT_PIX_ROUND( org_center );
2414
if ( cur_len <= 64 )
2425
delta1 = org_center - ( cur_pos1 - u_off );
2429
delta2 = org_center - ( cur_pos1 + d_off );
2433
if ( delta1 < delta2 )
2438
edge->pos = cur_pos1 - cur_len / 2;
2439
edge2->pos = cur_pos1 + cur_len / 2;
2441
FT_TRACE5(( " STEM: edge %d (opos=%.2f) linked to %d (opos=%.2f)"
2442
" snapped to %.2f and %.2f\n",
2443
edge - edges, edge->opos / 64.0,
2444
edge2 - edges, edge2->opos / 64.0,
2445
edge->pos / 64.0, edge2->pos / 64.0 ));
2450
org_pos = anchor->pos + ( edge->opos - anchor->opos );
2451
org_len = edge2->opos - edge->opos;
2452
org_center = org_pos + ( org_len >> 1 );
2454
cur_len = af_latin_compute_stem_width(
2455
hints, dim, org_len,
2456
(AF_Edge_Flags)edge->flags,
2457
(AF_Edge_Flags)edge2->flags );
2459
cur_pos1 = FT_PIX_ROUND( org_pos );
2460
delta1 = cur_pos1 + ( cur_len >> 1 ) - org_center;
2464
cur_pos2 = FT_PIX_ROUND( org_pos + org_len ) - cur_len;
2465
delta2 = cur_pos2 + ( cur_len >> 1 ) - org_center;
2469
edge->pos = ( delta1 < delta2 ) ? cur_pos1 : cur_pos2;
2470
edge2->pos = edge->pos + cur_len;
2472
FT_TRACE5(( " STEM: edge %d (opos=%.2f) linked to %d (opos=%.2f)"
2473
" snapped to %.2f and %.2f\n",
2474
edge - edges, edge->opos / 64.0,
2475
edge2 - edges, edge2->opos / 64.0,
2476
edge->pos / 64.0, edge2->pos / 64.0 ));
2479
#ifdef FT_DEBUG_LEVEL_TRACE
2483
edge->flags |= AF_EDGE_DONE;
2484
edge2->flags |= AF_EDGE_DONE;
2486
if ( edge > edges && edge->pos < edge[-1].pos )
2488
#ifdef FT_DEBUG_LEVEL_TRACE
2489
FT_TRACE5(( " BOUND: edge %d (pos=%.2f) moved to %.2f\n",
2490
edge - edges, edge->pos / 64.0, edge[-1].pos / 64.0 ));
2495
edge->pos = edge[-1].pos;
2500
/* make sure that lowercase m's maintain their symmetry */
2502
/* In general, lowercase m's have six vertical edges if they are sans */
2503
/* serif, or twelve if they are with serifs. This implementation is */
2504
/* based on that assumption, and seems to work very well with most */
2505
/* faces. However, if for a certain face this assumption is not */
2506
/* true, the m is just rendered like before. In addition, any stem */
2507
/* correction will only be applied to symmetrical glyphs (even if the */
2508
/* glyph is not an m), so the potential for unwanted distortion is */
2509
/* relatively low. */
2511
/* We don't handle horizontal edges since we can't easily assure that */
2512
/* the third (lowest) stem aligns with the base line; it might end up */
2513
/* one pixel higher or lower. */
2515
n_edges = edge_limit - edges;
2516
if ( dim == AF_DIMENSION_HORZ && ( n_edges == 6 || n_edges == 12 ) )
2518
AF_Edge edge1, edge2, edge3;
2519
FT_Pos dist1, dist2, span, delta;
2535
dist1 = edge2->opos - edge1->opos;
2536
dist2 = edge3->opos - edge2->opos;
2538
span = dist1 - dist2;
2544
delta = edge3->pos - ( 2 * edge2->pos - edge1->pos );
2545
edge3->pos -= delta;
2547
edge3->link->pos -= delta;
2549
/* move the serifs along with the stem */
2550
if ( n_edges == 12 )
2552
( edges + 8 )->pos -= delta;
2553
( edges + 11 )->pos -= delta;
2556
edge3->flags |= AF_EDGE_DONE;
2558
edge3->link->flags |= AF_EDGE_DONE;
2562
if ( has_serifs || !anchor )
2565
* now hint the remaining edges (serifs and single) in order
2566
* to complete our processing
2568
for ( edge = edges; edge < edge_limit; edge++ )
2573
if ( edge->flags & AF_EDGE_DONE )
2580
delta = edge->serif->opos - edge->opos;
2585
if ( delta < 64 + 16 )
2587
af_latin_align_serif_edge( hints, edge->serif, edge );
2588
FT_TRACE5(( " SERIF: edge %d (opos=%.2f) serif to %d (opos=%.2f)"
2589
" aligned to %.2f\n",
2590
edge - edges, edge->opos / 64.0,
2591
edge->serif - edges, edge->serif->opos / 64.0,
2592
edge->pos / 64.0 ));
2596
edge->pos = FT_PIX_ROUND( edge->opos );
2598
FT_TRACE5(( " SERIF_ANCHOR: edge %d (opos=%.2f)"
2599
" snapped to %.2f\n",
2600
edge-edges, edge->opos / 64.0, edge->pos / 64.0 ));
2604
AF_Edge before, after;
2607
for ( before = edge - 1; before >= edges; before-- )
2608
if ( before->flags & AF_EDGE_DONE )
2611
for ( after = edge + 1; after < edge_limit; after++ )
2612
if ( after->flags & AF_EDGE_DONE )
2615
if ( before >= edges && before < edge &&
2616
after < edge_limit && after > edge )
2618
if ( after->opos == before->opos )
2619
edge->pos = before->pos;
2621
edge->pos = before->pos +
2622
FT_MulDiv( edge->opos - before->opos,
2623
after->pos - before->pos,
2624
after->opos - before->opos );
2626
FT_TRACE5(( " SERIF_LINK1: edge %d (opos=%.2f) snapped to %.2f"
2627
" from %d (opos=%.2f)\n",
2628
edge - edges, edge->opos / 64.0,
2630
before - edges, before->opos / 64.0 ));
2634
edge->pos = anchor->pos +
2635
( ( edge->opos - anchor->opos + 16 ) & ~31 );
2636
FT_TRACE5(( " SERIF_LINK2: edge %d (opos=%.2f)"
2637
" snapped to %.2f\n",
2638
edge - edges, edge->opos / 64.0, edge->pos / 64.0 ));
2642
#ifdef FT_DEBUG_LEVEL_TRACE
2645
edge->flags |= AF_EDGE_DONE;
2647
if ( edge > edges && edge->pos < edge[-1].pos )
2649
#ifdef FT_DEBUG_LEVEL_TRACE
2650
FT_TRACE5(( " BOUND: edge %d (pos=%.2f) moved to %.2f\n",
2651
edge - edges, edge->pos / 64.0, edge[-1].pos / 64.0 ));
2655
edge->pos = edge[-1].pos;
2658
if ( edge + 1 < edge_limit &&
2659
edge[1].flags & AF_EDGE_DONE &&
2660
edge->pos > edge[1].pos )
2662
#ifdef FT_DEBUG_LEVEL_TRACE
2663
FT_TRACE5(( " BOUND: edge %d (pos=%.2f) moved to %.2f\n",
2664
edge - edges, edge->pos / 64.0, edge[1].pos / 64.0 ));
2669
edge->pos = edge[1].pos;
2674
#ifdef FT_DEBUG_LEVEL_TRACE
2676
FT_TRACE5(( " (none)\n" ));
2677
FT_TRACE5(( "\n" ));
2682
/* Apply the complete hinting algorithm to a latin glyph. */
2685
af_latin_hints_apply( AF_GlyphHints hints,
2686
FT_Outline* outline,
2687
AF_LatinMetrics metrics )
2693
error = af_glyph_hints_reload( hints, outline );
2697
/* analyze glyph outline */
2698
#ifdef AF_CONFIG_OPTION_USE_WARPER
2699
if ( metrics->root.scaler.render_mode == FT_RENDER_MODE_LIGHT ||
2700
AF_HINTS_DO_HORIZONTAL( hints ) )
2702
if ( AF_HINTS_DO_HORIZONTAL( hints ) )
2705
error = af_latin_hints_detect_features( hints, AF_DIMENSION_HORZ );
2710
if ( AF_HINTS_DO_VERTICAL( hints ) )
2712
error = af_latin_hints_detect_features( hints, AF_DIMENSION_VERT );
2716
af_latin_hints_compute_blue_edges( hints, metrics );
2719
/* grid-fit the outline */
2720
for ( dim = 0; dim < AF_DIMENSION_MAX; dim++ )
2722
#ifdef AF_CONFIG_OPTION_USE_WARPER
2723
if ( dim == AF_DIMENSION_HORZ &&
2724
metrics->root.scaler.render_mode == FT_RENDER_MODE_LIGHT )
2726
AF_WarperRec warper;
2731
af_warper_compute( &warper, hints, (AF_Dimension)dim,
2733
af_glyph_hints_scale_dim( hints, (AF_Dimension)dim,
2739
if ( ( dim == AF_DIMENSION_HORZ && AF_HINTS_DO_HORIZONTAL( hints ) ) ||
2740
( dim == AF_DIMENSION_VERT && AF_HINTS_DO_VERTICAL( hints ) ) )
2742
af_latin_hint_edges( hints, (AF_Dimension)dim );
2743
af_glyph_hints_align_edge_points( hints, (AF_Dimension)dim );
2744
af_glyph_hints_align_strong_points( hints, (AF_Dimension)dim );
2745
af_glyph_hints_align_weak_points( hints, (AF_Dimension)dim );
2749
af_glyph_hints_save( hints, outline );
2756
/*************************************************************************/
2757
/*************************************************************************/
2759
/***** L A T I N S C R I P T C L A S S *****/
2761
/*************************************************************************/
2762
/*************************************************************************/
2765
AF_DEFINE_WRITING_SYSTEM_CLASS(
2766
af_latin_writing_system_class,
2768
AF_WRITING_SYSTEM_LATIN,
2770
sizeof ( AF_LatinMetricsRec ),
2772
(AF_WritingSystem_InitMetricsFunc) af_latin_metrics_init,
2773
(AF_WritingSystem_ScaleMetricsFunc)af_latin_metrics_scale,
2774
(AF_WritingSystem_DoneMetricsFunc) NULL,
2776
(AF_WritingSystem_InitHintsFunc) af_latin_hints_init,
2777
(AF_WritingSystem_ApplyHintsFunc) af_latin_hints_apply