1
/***************************************************************************/
5
/* PostScript hinting algorithm (body). */
7
/* Copyright 2001, 2002, 2003, 2004 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_INTERNAL_OBJECTS_H
21
#include FT_INTERNAL_DEBUG_H
28
#define FT_COMPONENT trace_pshalgo2
32
PSH_Hint_Table ps_debug_hint_table = 0;
33
PSH_HintFunc ps_debug_hint_func = 0;
34
PSH_Glyph ps_debug_glyph = 0;
38
#define COMPUTE_INFLEXS /* compute inflection points to optimize `S' */
39
/* and similar glyphs */
40
#define STRONGER /* slightly increase the contrast of smooth */
44
/*************************************************************************/
45
/*************************************************************************/
47
/***** BASIC HINTS RECORDINGS *****/
49
/*************************************************************************/
50
/*************************************************************************/
52
/* return true if two stem hints overlap */
54
psh_hint_overlap( PSH_Hint hint1,
57
return hint1->org_pos + hint1->org_len >= hint2->org_pos &&
58
hint2->org_pos + hint2->org_len >= hint1->org_pos;
62
/* destroy hints table */
64
psh_hint_table_done( PSH_Hint_Table table,
67
FT_FREE( table->zones );
71
FT_FREE( table->sort );
72
FT_FREE( table->hints );
75
table->sort_global = 0;
79
/* deactivate all hints in a table */
81
psh_hint_table_deactivate( PSH_Hint_Table table )
83
FT_UInt count = table->max_hints;
84
PSH_Hint hint = table->hints;
87
for ( ; count > 0; count--, hint++ )
89
psh_hint_deactivate( hint );
95
/* internal function to record a new hint */
97
psh_hint_table_record( PSH_Hint_Table table,
100
PSH_Hint hint = table->hints + idx;
103
if ( idx >= table->max_hints )
105
FT_ERROR(( "psh_hint_table_record: invalid hint index %d\n", idx ));
109
/* ignore active hints */
110
if ( psh_hint_is_active( hint ) )
113
psh_hint_activate( hint );
115
/* now scan the current active hint set to check */
116
/* whether `hint' overlaps with another hint */
118
PSH_Hint* sorted = table->sort_global;
119
FT_UInt count = table->num_hints;
124
for ( ; count > 0; count--, sorted++ )
128
if ( psh_hint_overlap( hint, hint2 ) )
130
hint->parent = hint2;
136
if ( table->num_hints < table->max_hints )
137
table->sort_global[table->num_hints++] = hint;
139
FT_ERROR(( "psh_hint_table_record: too many sorted hints! BUG!\n" ));
144
psh_hint_table_record_mask( PSH_Hint_Table table,
147
FT_Int mask = 0, val = 0;
148
FT_Byte* cursor = hint_mask->bytes;
152
limit = hint_mask->num_bits;
154
for ( idx = 0; idx < limit; idx++ )
163
psh_hint_table_record( table, idx );
170
/* create hints table */
172
psh_hint_table_init( PSH_Hint_Table table,
174
PS_Mask_Table hint_masks,
175
PS_Mask_Table counter_masks,
181
FT_UNUSED( counter_masks );
184
count = hints->num_hints;
186
/* allocate our tables */
187
if ( FT_NEW_ARRAY( table->sort, 2 * count ) ||
188
FT_NEW_ARRAY( table->hints, count ) ||
189
FT_NEW_ARRAY( table->zones, 2 * count + 1 ) )
192
table->max_hints = count;
193
table->sort_global = table->sort + count;
194
table->num_hints = 0;
195
table->num_zones = 0;
198
/* initialize the `table->hints' array */
200
PSH_Hint write = table->hints;
201
PS_Hint read = hints->hints;
204
for ( ; count > 0; count--, write++, read++ )
206
write->org_pos = read->pos;
207
write->org_len = read->len;
208
write->flags = read->flags;
212
/* we now need to determine the initial `parent' stems; first */
213
/* activate the hints that are given by the initial hint masks */
216
PS_Mask mask = hint_masks->masks;
219
count = hint_masks->num_masks;
220
table->hint_masks = hint_masks;
222
for ( ; count > 0; count--, mask++ )
223
psh_hint_table_record_mask( table, mask );
226
/* finally, do a linear parse in case some hints were left alone */
227
if ( table->num_hints != table->max_hints )
232
FT_ERROR(( "psh_hint_table_init: missing/incorrect hint masks!\n" ));
234
count = table->max_hints;
235
for ( idx = 0; idx < count; idx++ )
236
psh_hint_table_record( table, idx );
245
psh_hint_table_activate_mask( PSH_Hint_Table table,
248
FT_Int mask = 0, val = 0;
249
FT_Byte* cursor = hint_mask->bytes;
250
FT_UInt idx, limit, count;
253
limit = hint_mask->num_bits;
256
psh_hint_table_deactivate( table );
258
for ( idx = 0; idx < limit; idx++ )
268
PSH_Hint hint = &table->hints[idx];
271
if ( !psh_hint_is_active( hint ) )
276
PSH_Hint* sort = table->sort;
280
for ( count2 = count; count2 > 0; count2--, sort++ )
283
if ( psh_hint_overlap( hint, hint2 ) )
284
FT_ERROR(( "psh_hint_table_activate_mask:"
285
" found overlapping hints\n" ))
293
psh_hint_activate( hint );
294
if ( count < table->max_hints )
295
table->sort[count++] = hint;
297
FT_ERROR(( "psh_hint_tableactivate_mask:"
298
" too many active hints\n" ));
305
table->num_hints = count;
307
/* now, sort the hints; they are guaranteed to not overlap */
308
/* so we can compare their "org_pos" field directly */
311
PSH_Hint hint1, hint2;
312
PSH_Hint* sort = table->sort;
315
/* a simple bubble sort will do, since in 99% of cases, the hints */
316
/* will be already sorted -- and the sort will be linear */
317
for ( i1 = 1; i1 < (FT_Int)count; i1++ )
320
for ( i2 = i1 - 1; i2 >= 0; i2-- )
324
if ( hint2->org_pos < hint1->org_pos )
327
sort[i2 + 1] = hint2;
335
/*************************************************************************/
336
/*************************************************************************/
338
/***** HINTS GRID-FITTING AND OPTIMIZATION *****/
340
/*************************************************************************/
341
/*************************************************************************/
345
psh_dimension_quantize_len( PSH_Dimension dim,
347
FT_Bool do_snapping )
353
FT_Pos delta = len - dim->stdw.widths[0].cur;
361
len = dim->stdw.widths[0].cur;
368
delta = ( len & 63 );
374
else if ( delta < 32 )
377
else if ( delta < 54 )
384
len = FT_PIX_ROUND( len );
388
len = FT_PIX_ROUND( len );
398
ps_simple_scale( PSH_Hint_Table table,
407
for ( count = 0; count < table->max_hints; count++ )
409
hint = table->hints + count;
411
hint->cur_pos = FT_MulFix( hint->org_pos, scale ) + delta;
412
hint->cur_len = FT_MulFix( hint->org_len, scale );
414
if ( ps_debug_hint_func )
415
ps_debug_hint_func( hint, dimension );
419
#endif /* DEBUG_HINTER */
423
psh_hint_snap_stem_side_delta( FT_Fixed pos,
426
FT_Fixed delta1 = FT_PIX_ROUND( pos ) - pos;
427
FT_Fixed delta2 = FT_PIX_ROUND( pos + len ) - pos - len;
430
if ( FT_ABS( delta1 ) <= FT_ABS( delta2 ) )
438
psh_hint_align( PSH_Hint hint,
443
PSH_Dimension dim = &globals->dimension[dimension];
444
FT_Fixed scale = dim->scale_mult;
445
FT_Fixed delta = dim->scale_delta;
448
if ( !psh_hint_is_fitted( hint ) )
450
FT_Pos pos = FT_MulFix( hint->org_pos, scale ) + delta;
451
FT_Pos len = FT_MulFix( hint->org_len, scale );
455
PSH_AlignmentRec align;
458
/* ignore stem alignments when requested through the hint flags */
459
if ( ( dimension == 0 && !glyph->do_horz_hints ) ||
460
( dimension == 1 && !glyph->do_vert_hints ) )
465
psh_hint_set_fitted( hint );
469
/* perform stem snapping when requested - this is necessary
470
* for monochrome and LCD hinting modes only
472
do_snapping = ( dimension == 0 && glyph->do_horz_snapping ) ||
473
( dimension == 1 && glyph->do_vert_snapping );
475
hint->cur_len = fit_len = len;
477
/* check blue zones for horizontal stems */
478
align.align = PSH_BLUE_ALIGN_NONE;
479
align.align_bot = align.align_top = 0;
481
if ( dimension == 1 )
482
psh_blues_snap_stem( &globals->blues,
483
hint->org_pos + hint->org_len,
487
switch ( align.align )
489
case PSH_BLUE_ALIGN_TOP:
490
/* the top of the stem is aligned against a blue zone */
491
hint->cur_pos = align.align_top - fit_len;
494
case PSH_BLUE_ALIGN_BOT:
495
/* the bottom of the stem is aligned against a blue zone */
496
hint->cur_pos = align.align_bot;
499
case PSH_BLUE_ALIGN_TOP | PSH_BLUE_ALIGN_BOT:
500
/* both edges of the stem are aligned against blue zones */
501
hint->cur_pos = align.align_bot;
502
hint->cur_len = align.align_top - align.align_bot;
507
PSH_Hint parent = hint->parent;
512
FT_Pos par_org_center, par_cur_center;
513
FT_Pos cur_org_center, cur_delta;
516
/* ensure that parent is already fitted */
517
if ( !psh_hint_is_fitted( parent ) )
518
psh_hint_align( parent, globals, dimension, glyph );
520
/* keep original relation between hints, this is, use the */
521
/* scaled distance between the centers of the hints to */
522
/* compute the new position */
523
par_org_center = parent->org_pos + ( parent->org_len >> 1 );
524
par_cur_center = parent->cur_pos + ( parent->cur_len >> 1 );
525
cur_org_center = hint->org_pos + ( hint->org_len >> 1 );
527
cur_delta = FT_MulFix( cur_org_center - par_org_center, scale );
528
pos = par_cur_center + cur_delta - ( len >> 1 );
532
hint->cur_len = fit_len;
534
/* Stem adjustment tries to snap stem widths to standard
535
* ones. This is important to prevent unpleasant rounding
538
if ( glyph->do_stem_adjust )
542
/* the stem is less than one pixel; we will center it
543
* around the nearest pixel center
546
pos = FT_PIX_FLOOR( pos + ( len >> 1 ) );
548
/* this seems to be a bug! */
549
pos = pos + FT_PIX_FLOOR( len >> 1 );
555
len = psh_dimension_quantize_len( dim, len, 0 );
559
/* now that we have a good hinted stem width, try to position */
560
/* the stem along a pixel grid integer coordinate */
561
hint->cur_pos = pos + psh_hint_snap_stem_side_delta( pos, len );
574
len = FT_PIX_ROUND( len );
576
switch ( align.align )
578
case PSH_BLUE_ALIGN_TOP:
579
hint->cur_pos = align.align_top - len;
583
case PSH_BLUE_ALIGN_BOT:
587
case PSH_BLUE_ALIGN_BOT | PSH_BLUE_ALIGN_TOP:
595
pos = FT_PIX_FLOOR( pos + ( len >> 1 ) ) + 32;
597
pos = FT_PIX_ROUND( pos + ( len >> 1 ) );
599
hint->cur_pos = pos - ( len >> 1 );
604
psh_hint_set_fitted( hint );
607
if ( ps_debug_hint_func )
608
ps_debug_hint_func( hint, dimension );
614
#if 0 /* not used for now, experimental */
617
* A variant to perform "light" hinting (i.e. FT_RENDER_MODE_LIGHT)
621
psh_hint_align_light( PSH_Hint hint,
626
PSH_Dimension dim = &globals->dimension[dimension];
627
FT_Fixed scale = dim->scale_mult;
628
FT_Fixed delta = dim->scale_delta;
631
if ( !psh_hint_is_fitted( hint ) )
633
FT_Pos pos = FT_MulFix( hint->org_pos, scale ) + delta;
634
FT_Pos len = FT_MulFix( hint->org_len, scale );
638
PSH_AlignmentRec align;
641
/* ignore stem alignments when requested through the hint flags */
642
if ( ( dimension == 0 && !glyph->do_horz_hints ) ||
643
( dimension == 1 && !glyph->do_vert_hints ) )
648
psh_hint_set_fitted( hint );
654
hint->cur_len = fit_len;
656
/* check blue zones for horizontal stems */
657
align.align = PSH_BLUE_ALIGN_NONE;
658
align.align_bot = align.align_top = 0;
660
if ( dimension == 1 )
661
psh_blues_snap_stem( &globals->blues,
662
hint->org_pos + hint->org_len,
666
switch ( align.align )
668
case PSH_BLUE_ALIGN_TOP:
669
/* the top of the stem is aligned against a blue zone */
670
hint->cur_pos = align.align_top - fit_len;
673
case PSH_BLUE_ALIGN_BOT:
674
/* the bottom of the stem is aligned against a blue zone */
675
hint->cur_pos = align.align_bot;
678
case PSH_BLUE_ALIGN_TOP | PSH_BLUE_ALIGN_BOT:
679
/* both edges of the stem are aligned against blue zones */
680
hint->cur_pos = align.align_bot;
681
hint->cur_len = align.align_top - align.align_bot;
686
PSH_Hint parent = hint->parent;
691
FT_Pos par_org_center, par_cur_center;
692
FT_Pos cur_org_center, cur_delta;
695
/* ensure that parent is already fitted */
696
if ( !psh_hint_is_fitted( parent ) )
697
psh_hint_align_light( parent, globals, dimension, glyph );
699
par_org_center = parent->org_pos + ( parent->org_len / 2 );
700
par_cur_center = parent->cur_pos + ( parent->cur_len / 2 );
701
cur_org_center = hint->org_pos + ( hint->org_len / 2 );
703
cur_delta = FT_MulFix( cur_org_center - par_org_center, scale );
704
pos = par_cur_center + cur_delta - ( len >> 1 );
707
/* Stems less than one pixel wide are easy -- we want to
708
* make them as dark as possible, so they must fall within
709
* one pixel. If the stem is split between two pixels
710
* then snap the edge that is nearer to the pixel boundary
711
* to the pixel boundary.
715
if ( ( pos + len + 63 ) / 64 != pos / 64 + 1 )
716
pos += psh_hint_snap_stem_side_delta ( pos, len );
719
/* Position stems other to minimize the amount of mid-grays.
720
* There are, in general, two positions that do this,
721
* illustrated as A) and B) below.
725
* A) |--------------------------------|
726
* B) |--------------------------------|
727
* C) |--------------------------------|
729
* Position A) (split the excess stem equally) should be better
730
* for stems of width N + f where f < 0.5.
732
* Position B) (split the deficiency equally) should be better
733
* for stems of width N + f where f > 0.5.
735
* It turns out though that minimizing the total number of lit
736
* pixels is also important, so position C), with one edge
737
* aligned with a pixel boundary is actually preferable
738
* to A). There are also more possibile positions for C) than
739
* for A) or B), so it involves less distortion of the overall
744
FT_Fixed frac_len = len & 63;
745
FT_Fixed center = pos + ( len >> 1 );
746
FT_Fixed delta_a, delta_b;
749
if ( ( len / 64 ) & 1 )
751
delta_a = FT_PIX_FLOOR( center ) + 32 - center;
752
delta_b = FT_PIX_ROUND( center ) - center;
756
delta_a = FT_PIX_ROUND( center ) - center;
757
delta_b = FT_PIX_FLOOR( center ) + 32 - center;
760
/* We choose between B) and C) above based on the amount
761
* of fractinal stem width; for small amounts, choose
762
* C) always, for large amounts, B) always, and inbetween,
763
* pick whichever one involves less stem movement.
767
pos += psh_hint_snap_stem_side_delta ( pos, len );
769
else if ( frac_len < 48 )
771
FT_Fixed side_delta = psh_hint_snap_stem_side_delta ( pos,
774
if ( FT_ABS( side_delta ) < FT_ABS( delta_b ) )
789
psh_hint_set_fitted( hint );
792
if ( ps_debug_hint_func )
793
ps_debug_hint_func( hint, dimension );
802
psh_hint_table_align_hints( PSH_Hint_Table table,
812
PSH_Dimension dim = &globals->dimension[dimension];
813
FT_Fixed scale = dim->scale_mult;
814
FT_Fixed delta = dim->scale_delta;
817
if ( ps_debug_no_vert_hints && dimension == 0 )
819
ps_simple_scale( table, scale, delta, dimension );
823
if ( ps_debug_no_horz_hints && dimension == 1 )
825
ps_simple_scale( table, scale, delta, dimension );
829
#endif /* DEBUG_HINTER*/
832
count = table->max_hints;
834
for ( ; count > 0; count--, hint++ )
835
psh_hint_align( hint, globals, dimension, glyph );
839
/*************************************************************************/
840
/*************************************************************************/
842
/***** POINTS INTERPOLATION ROUTINES *****/
844
/*************************************************************************/
845
/*************************************************************************/
847
#define PSH_ZONE_MIN -3200000L
848
#define PSH_ZONE_MAX +3200000L
850
#define xxDEBUG_ZONES
858
psh_print_zone( PSH_Zone zone )
860
printf( "zone [scale,delta,min,max] = [%.3f,%.3f,%d,%d]\n",
861
zone->scale / 65536.0,
869
#define psh_print_zone( x ) do { } while ( 0 )
871
#endif /* DEBUG_ZONES */
874
/*************************************************************************/
875
/*************************************************************************/
877
/***** HINTER GLYPH MANAGEMENT *****/
879
/*************************************************************************/
880
/*************************************************************************/
882
#ifdef COMPUTE_INFLEXS
884
/* compute all inflex points in a given glyph */
886
psh_glyph_compute_inflections( PSH_Glyph glyph )
891
for ( n = 0; n < glyph->num_contours; n++ )
893
PSH_Point first, start, end, before, after;
894
FT_Angle angle_in, angle_seg, angle_out;
895
FT_Angle diff_in, diff_out;
899
/* we need at least 4 points to create an inflection point */
900
if ( glyph->contours[n].count < 4 )
903
/* compute first segment in contour */
904
first = glyph->contours[n].start;
913
} while ( PSH_POINT_EQUAL_ORG( end, first ) );
915
angle_seg = PSH_POINT_ANGLE( start, end );
917
/* extend the segment start whenever possible */
924
before = before->prev;
925
if ( before == first )
928
} while ( PSH_POINT_EQUAL_ORG( before, start ) );
930
angle_in = PSH_POINT_ANGLE( before, start );
932
} while ( angle_in == angle_seg );
935
diff_in = FT_Angle_Diff( angle_in, angle_seg );
937
/* now, process all segments in the contour */
940
/* first, extend current segment's end whenever possible */
948
if ( after == first )
951
} while ( PSH_POINT_EQUAL_ORG( end, after ) );
953
angle_out = PSH_POINT_ANGLE( end, after );
955
} while ( angle_out == angle_seg );
957
diff_out = FT_Angle_Diff( angle_seg, angle_out );
959
if ( ( diff_in ^ diff_out ) < 0 )
961
/* diff_in and diff_out have different signs, we have */
962
/* inflection points here... */
966
psh_point_set_inflex( start );
969
while ( start != end );
971
psh_point_set_inflex( start );
976
angle_seg = angle_out;
979
} while ( !finished );
986
#endif /* COMPUTE_INFLEXS */
990
psh_glyph_done( PSH_Glyph glyph )
992
FT_Memory memory = glyph->memory;
995
psh_hint_table_done( &glyph->hint_tables[1], memory );
996
psh_hint_table_done( &glyph->hint_tables[0], memory );
998
FT_FREE( glyph->points );
999
FT_FREE( glyph->contours );
1001
glyph->num_points = 0;
1002
glyph->num_contours = 0;
1009
psh_compute_dir( FT_Pos dx,
1013
int result = PSH_DIR_NONE;
1016
ax = ( dx >= 0 ) ? dx : -dx;
1017
ay = ( dy >= 0 ) ? dy : -dy;
1021
/* |dy| <<< |dx| means a near-horizontal segment */
1022
result = ( dx >= 0 ) ? PSH_DIR_RIGHT : PSH_DIR_LEFT;
1024
else if ( ax * 12 < ay )
1026
/* |dx| <<< |dy| means a near-vertical segment */
1027
result = ( dy >= 0 ) ? PSH_DIR_UP : PSH_DIR_DOWN;
1034
/* load outline point coordinates into hinter glyph */
1036
psh_glyph_load_points( PSH_Glyph glyph,
1039
FT_Vector* vec = glyph->outline->points;
1040
PSH_Point point = glyph->points;
1041
FT_UInt count = glyph->num_points;
1044
for ( ; count > 0; count--, point++, vec++ )
1048
if ( dimension == 0 )
1050
point->org_u = vec->x;
1051
point->org_v = vec->y;
1055
point->org_u = vec->y;
1056
point->org_v = vec->x;
1060
point->org_x = vec->x;
1061
point->org_y = vec->y;
1068
/* save hinted point coordinates back to outline */
1070
psh_glyph_save_points( PSH_Glyph glyph,
1074
PSH_Point point = glyph->points;
1075
FT_Vector* vec = glyph->outline->points;
1076
char* tags = glyph->outline->tags;
1079
for ( n = 0; n < glyph->num_points; n++ )
1081
if ( dimension == 0 )
1082
vec[n].x = point->cur_u;
1084
vec[n].y = point->cur_u;
1086
if ( psh_point_is_strong( point ) )
1087
tags[n] |= (char)( ( dimension == 0 ) ? 32 : 64 );
1091
if ( dimension == 0 )
1093
point->cur_x = point->cur_u;
1094
point->flags_x = point->flags2 | point->flags;
1098
point->cur_y = point->cur_u;
1099
point->flags_y = point->flags2 | point->flags;
1110
psh_glyph_init( PSH_Glyph glyph,
1111
FT_Outline* outline,
1113
PSH_Globals globals )
1119
/* clear all fields */
1120
FT_MEM_ZERO( glyph, sizeof ( *glyph ) );
1122
memory = glyph->memory = globals->memory;
1124
/* allocate and setup points + contours arrays */
1125
if ( FT_NEW_ARRAY( glyph->points, outline->n_points ) ||
1126
FT_NEW_ARRAY( glyph->contours, outline->n_contours ) )
1129
glyph->num_points = outline->n_points;
1130
glyph->num_contours = outline->n_contours;
1133
FT_UInt first = 0, next, n;
1134
PSH_Point points = glyph->points;
1135
PSH_Contour contour = glyph->contours;
1138
for ( n = 0; n < glyph->num_contours; n++ )
1144
next = outline->contours[n] + 1;
1145
count = next - first;
1147
contour->start = points + first;
1148
contour->count = (FT_UInt)count;
1152
point = points + first;
1154
point->prev = points + next - 1;
1155
point->contour = contour;
1157
for ( ; count > 1; count-- )
1159
point[0].next = point + 1;
1160
point[1].prev = point;
1162
point->contour = contour;
1164
point->next = points + first;
1173
PSH_Point points = glyph->points;
1174
PSH_Point point = points;
1175
FT_Vector* vec = outline->points;
1179
for ( n = 0; n < glyph->num_points; n++, point++ )
1181
FT_Int n_prev = (FT_Int)( point->prev - points );
1182
FT_Int n_next = (FT_Int)( point->next - points );
1183
FT_Pos dxi, dyi, dxo, dyo;
1186
if ( !( outline->tags[n] & FT_CURVE_TAG_ON ) )
1187
point->flags = PSH_POINT_OFF;
1189
dxi = vec[n].x - vec[n_prev].x;
1190
dyi = vec[n].y - vec[n_prev].y;
1192
point->dir_in = (FT_Char)psh_compute_dir( dxi, dyi );
1194
dxo = vec[n_next].x - vec[n].x;
1195
dyo = vec[n_next].y - vec[n].y;
1197
point->dir_out = (FT_Char)psh_compute_dir( dxo, dyo );
1199
/* detect smooth points */
1200
if ( point->flags & PSH_POINT_OFF )
1201
point->flags |= PSH_POINT_SMOOTH;
1202
else if ( point->dir_in != PSH_DIR_NONE ||
1203
point->dir_out != PSH_DIR_NONE )
1205
if ( point->dir_in == point->dir_out )
1206
point->flags |= PSH_POINT_SMOOTH;
1210
FT_Angle angle_in, angle_out, diff;
1213
angle_in = FT_Atan2( dxi, dyi );
1214
angle_out = FT_Atan2( dxo, dyo );
1216
diff = angle_in - angle_out;
1220
if ( diff > FT_ANGLE_PI )
1221
diff = FT_ANGLE_2PI - diff;
1223
if ( diff < FT_ANGLE_PI / 16 )
1224
point->flags |= PSH_POINT_SMOOTH;
1229
glyph->outline = outline;
1230
glyph->globals = globals;
1232
#ifdef COMPUTE_INFLEXS
1233
psh_glyph_load_points( glyph, 0 );
1234
psh_glyph_compute_inflections( glyph );
1235
#endif /* COMPUTE_INFLEXS */
1237
/* now deal with hints tables */
1238
error = psh_hint_table_init( &glyph->hint_tables [0],
1239
&ps_hints->dimension[0].hints,
1240
&ps_hints->dimension[0].masks,
1241
&ps_hints->dimension[0].counters,
1246
error = psh_hint_table_init( &glyph->hint_tables [1],
1247
&ps_hints->dimension[1].hints,
1248
&ps_hints->dimension[1].masks,
1249
&ps_hints->dimension[1].counters,
1259
/* compute all extrema in a glyph for a given dimension */
1261
psh_glyph_compute_extrema( PSH_Glyph glyph )
1266
/* first of all, compute all local extrema */
1267
for ( n = 0; n < glyph->num_contours; n++ )
1269
PSH_Point first = glyph->contours[n].start;
1270
PSH_Point point, before, after;
1273
if ( glyph->contours[n].count == 0 )
1282
before = before->prev;
1283
if ( before == first )
1286
} while ( before->org_u == point->org_u );
1288
first = point = before->next;
1295
after = after->next;
1296
if ( after == first )
1299
} while ( after->org_u == point->org_u );
1301
if ( before->org_u < point->org_u )
1303
if ( after->org_u < point->org_u )
1309
else /* before->org_u > point->org_u */
1311
if ( after->org_u > point->org_u )
1317
psh_point_set_extremum( point );
1318
point = point->next;
1320
} while ( point != after );
1324
before = after->prev;
1333
/* for each extremum, determine its direction along the */
1334
/* orthogonal axis */
1335
for ( n = 0; n < glyph->num_points; n++ )
1337
PSH_Point point, before, after;
1340
point = &glyph->points[n];
1344
if ( psh_point_is_extremum( point ) )
1348
before = before->prev;
1349
if ( before == point )
1352
} while ( before->org_v == point->org_v );
1356
after = after->next;
1357
if ( after == point )
1360
} while ( after->org_v == point->org_v );
1363
if ( before->org_v < point->org_v &&
1364
after->org_v > point->org_v )
1366
psh_point_set_positive( point );
1368
else if ( before->org_v > point->org_v &&
1369
after->org_v < point->org_v )
1371
psh_point_set_negative( point );
1380
/* major_dir is the direction for points on the bottom/left of the stem; */
1381
/* Points on the top/right of the stem will have a direction of */
1385
psh_hint_table_find_strong_point( PSH_Hint_Table table,
1390
PSH_Hint* sort = table->sort;
1391
FT_UInt num_hints = table->num_hints;
1392
FT_Int point_dir = 0;
1395
if ( PSH_DIR_COMPARE( point->dir_in, major_dir ) )
1396
point_dir = point->dir_in;
1398
else if ( PSH_DIR_COMPARE( point->dir_out, major_dir ) )
1399
point_dir = point->dir_out;
1406
for ( ; num_hints > 0; num_hints--, sort++ )
1408
PSH_Hint hint = sort[0];
1412
if ( point_dir == major_dir )
1414
flag = PSH_POINT_EDGE_MIN;
1415
d = point->org_u - hint->org_pos;
1417
if ( FT_ABS( d ) < threshold )
1420
psh_point_set_strong( point );
1421
point->flags2 |= flag;
1426
else if ( point_dir == -major_dir )
1428
flag = PSH_POINT_EDGE_MAX;
1429
d = point->org_u - hint->org_pos - hint->org_len;
1431
if ( FT_ABS( d ) < threshold )
1438
else if ( psh_point_is_extremum( point ) )
1440
/* treat extrema as special cases for stem edge alignment */
1441
FT_UInt min_flag, max_flag;
1444
if ( major_dir == PSH_DIR_HORIZONTAL )
1446
min_flag = PSH_POINT_POSITIVE;
1447
max_flag = PSH_POINT_NEGATIVE;
1451
min_flag = PSH_POINT_NEGATIVE;
1452
max_flag = PSH_POINT_POSITIVE;
1455
for ( ; num_hints > 0; num_hints--, sort++ )
1457
PSH_Hint hint = sort[0];
1462
if ( point->flags2 & min_flag )
1464
flag = PSH_POINT_EDGE_MIN;
1465
d = point->org_u - hint->org_pos;
1467
if ( FT_ABS( d ) < threshold )
1470
point->flags2 |= flag;
1472
psh_point_set_strong( point );
1476
else if ( point->flags2 & max_flag )
1478
flag = PSH_POINT_EDGE_MAX;
1479
d = point->org_u - hint->org_pos - hint->org_len;
1481
if ( FT_ABS( d ) < threshold )
1485
if ( point->org_u >= hint->org_pos &&
1486
point->org_u <= hint->org_pos + hint->org_len )
1497
/* the accepted shift for strong points in fractional pixels */
1498
#define PSH_STRONG_THRESHOLD 32
1500
/* the maximum shift value in font units */
1501
#define PSH_STRONG_THRESHOLD_MAXIMUM 30
1504
/* find strong points in a glyph */
1506
psh_glyph_find_strong_points( PSH_Glyph glyph,
1509
/* a point is `strong' if it is located on a stem edge and */
1510
/* has an `in' or `out' tangent parallel to the hint's direction */
1512
PSH_Hint_Table table = &glyph->hint_tables[dimension];
1513
PS_Mask mask = table->hint_masks->masks;
1514
FT_UInt num_masks = table->hint_masks->num_masks;
1516
FT_Int major_dir = dimension == 0 ? PSH_DIR_VERTICAL
1517
: PSH_DIR_HORIZONTAL;
1518
PSH_Dimension dim = &glyph->globals->dimension[dimension];
1519
FT_Fixed scale = dim->scale_mult;
1523
threshold = (FT_Int)FT_DivFix( PSH_STRONG_THRESHOLD, scale );
1524
if ( threshold > PSH_STRONG_THRESHOLD_MAXIMUM )
1525
threshold = PSH_STRONG_THRESHOLD_MAXIMUM;
1527
/* process secondary hints to `selected' points */
1528
if ( num_masks > 1 && glyph->num_points > 0 )
1530
first = mask->end_point;
1532
for ( ; num_masks > 1; num_masks--, mask++ )
1538
next = mask->end_point;
1539
count = next - first;
1542
PSH_Point point = glyph->points + first;
1545
psh_hint_table_activate_mask( table, mask );
1547
for ( ; count > 0; count--, point++ )
1548
psh_hint_table_find_strong_point( table, point,
1549
threshold, major_dir );
1555
/* process primary hints for all points */
1556
if ( num_masks == 1 )
1558
FT_UInt count = glyph->num_points;
1559
PSH_Point point = glyph->points;
1562
psh_hint_table_activate_mask( table, table->hint_masks->masks );
1563
for ( ; count > 0; count--, point++ )
1565
if ( !psh_point_is_strong( point ) )
1566
psh_hint_table_find_strong_point( table, point,
1567
threshold, major_dir );
1571
/* now, certain points may have been attached to a hint and */
1572
/* not marked as strong; update their flags then */
1574
FT_UInt count = glyph->num_points;
1575
PSH_Point point = glyph->points;
1578
for ( ; count > 0; count--, point++ )
1579
if ( point->hint && !psh_point_is_strong( point ) )
1580
psh_point_set_strong( point );
1585
/* find points in a glyph which are in a blue zone and have `in' or */
1586
/* `out' tangents parallel to the horizontal axis */
1588
psh_glyph_find_blue_points( PSH_Blues blues,
1591
PSH_Blue_Table table;
1593
FT_UInt glyph_count = glyph->num_points;
1595
PSH_Point point = glyph->points;
1598
for ( ; glyph_count > 0; glyph_count--, point++ )
1603
/* check tangents */
1604
if ( !PSH_DIR_COMPARE( point->dir_in, PSH_DIR_HORIZONTAL ) &&
1605
!PSH_DIR_COMPARE( point->dir_out, PSH_DIR_HORIZONTAL ) )
1608
/* skip strong points */
1609
if ( psh_point_is_strong( point ) )
1614
/* look up top zones */
1615
table = &blues->normal_top;
1616
blue_count = table->count;
1617
zone = table->zones;
1619
for ( ; blue_count > 0; blue_count--, zone++ )
1621
FT_Pos delta = y - zone->org_bottom;
1624
if ( delta < -blues->blue_fuzz )
1627
if ( y <= zone->org_top + blues->blue_fuzz )
1628
if ( blues->no_overshoots || delta <= blues->blue_threshold )
1630
point->cur_u = zone->cur_bottom;
1631
psh_point_set_strong( point );
1632
psh_point_set_fitted( point );
1636
/* look up bottom zones */
1637
table = &blues->normal_bottom;
1638
blue_count = table->count;
1639
zone = table->zones + blue_count - 1;
1641
for ( ; blue_count > 0; blue_count--, zone-- )
1643
FT_Pos delta = zone->org_top - y;
1646
if ( delta < -blues->blue_fuzz )
1649
if ( y >= zone->org_bottom - blues->blue_fuzz )
1650
if ( blues->no_overshoots || delta < blues->blue_threshold )
1652
point->cur_u = zone->cur_top;
1653
psh_point_set_strong( point );
1654
psh_point_set_fitted( point );
1661
/* interpolate strong points with the help of hinted coordinates */
1663
psh_glyph_interpolate_strong_points( PSH_Glyph glyph,
1666
PSH_Dimension dim = &glyph->globals->dimension[dimension];
1667
FT_Fixed scale = dim->scale_mult;
1669
FT_UInt count = glyph->num_points;
1670
PSH_Point point = glyph->points;
1673
for ( ; count > 0; count--, point++ )
1675
PSH_Hint hint = point->hint;
1683
if ( psh_point_is_edge_min( point ) )
1684
point->cur_u = hint->cur_pos;
1686
else if ( psh_point_is_edge_max( point ) )
1687
point->cur_u = hint->cur_pos + hint->cur_len;
1691
delta = point->org_u - hint->org_pos;
1694
point->cur_u = hint->cur_pos + FT_MulFix( delta, scale );
1696
else if ( delta >= hint->org_len )
1697
point->cur_u = hint->cur_pos + hint->cur_len +
1698
FT_MulFix( delta - hint->org_len, scale );
1700
else if ( hint->org_len > 0 )
1701
point->cur_u = hint->cur_pos +
1702
FT_MulDiv( delta, hint->cur_len,
1705
point->cur_u = hint->cur_pos;
1707
psh_point_set_fitted( point );
1714
psh_glyph_interpolate_normal_points( PSH_Glyph glyph,
1719
/* first technique: a point is strong if it is a local extremum */
1721
PSH_Dimension dim = &glyph->globals->dimension[dimension];
1722
FT_Fixed scale = dim->scale_mult;
1724
FT_UInt count = glyph->num_points;
1725
PSH_Point point = glyph->points;
1728
for ( ; count > 0; count--, point++ )
1730
if ( psh_point_is_strong( point ) )
1733
/* sometimes, some local extrema are smooth points */
1734
if ( psh_point_is_smooth( point ) )
1736
if ( point->dir_in == PSH_DIR_NONE ||
1737
point->dir_in != point->dir_out )
1740
if ( !psh_point_is_extremum( point ) &&
1741
!psh_point_is_inflex( point ) )
1744
point->flags &= ~PSH_POINT_SMOOTH;
1747
/* find best enclosing point coordinates */
1749
PSH_Point before = 0;
1750
PSH_Point after = 0;
1752
FT_Pos diff_before = -32000;
1753
FT_Pos diff_after = 32000;
1754
FT_Pos u = point->org_u;
1756
FT_Int count2 = glyph->num_points;
1757
PSH_Point cur = glyph->points;
1760
for ( ; count2 > 0; count2--, cur++ )
1762
if ( psh_point_is_strong( cur ) )
1764
FT_Pos diff = cur->org_u - u;
1769
if ( diff > diff_before )
1776
else if ( diff >= 0 )
1778
if ( diff < diff_after )
1792
/* we are before the first strong point coordinate; */
1793
/* simply translate the point */
1794
point->cur_u = after->cur_u +
1795
FT_MulFix( point->org_u - after->org_u, scale );
1799
/* we are after the last strong point coordinate; */
1800
/* simply translate the point */
1801
point->cur_u = before->cur_u +
1802
FT_MulFix( point->org_u - before->org_u, scale );
1806
if ( diff_before == 0 )
1807
point->cur_u = before->cur_u;
1809
else if ( diff_after == 0 )
1810
point->cur_u = after->cur_u;
1813
point->cur_u = before->cur_u +
1814
FT_MulDiv( u - before->org_u,
1815
after->cur_u - before->cur_u,
1816
after->org_u - before->org_u );
1819
psh_point_set_fitted( point );
1828
/* interpolate other points */
1830
psh_glyph_interpolate_other_points( PSH_Glyph glyph,
1833
PSH_Dimension dim = &glyph->globals->dimension[dimension];
1834
FT_Fixed scale = dim->scale_mult;
1835
FT_Fixed delta = dim->scale_delta;
1836
PSH_Contour contour = glyph->contours;
1837
FT_UInt num_contours = glyph->num_contours;
1840
for ( ; num_contours > 0; num_contours--, contour++ )
1842
PSH_Point start = contour->start;
1843
PSH_Point first, next, point;
1847
/* count the number of strong points in this contour */
1848
next = start + contour->count;
1852
for ( point = start; point < next; point++ )
1853
if ( psh_point_is_fitted( point ) )
1861
/* if there are less than 2 fitted points in the contour, we */
1862
/* simply scale and eventually translate the contour points */
1863
if ( fit_count < 2 )
1865
if ( fit_count == 1 )
1866
delta = first->cur_u - FT_MulFix( first->org_u, scale );
1868
for ( point = start; point < next; point++ )
1869
if ( point != first )
1870
point->cur_u = FT_MulFix( point->org_u, scale ) + delta;
1875
/* there are more than 2 strong points in this contour; we */
1876
/* need to interpolate weak points between them */
1882
/* skip consecutive fitted points */
1886
if ( next == start )
1889
if ( !psh_point_is_fitted( next ) )
1895
/* find next fitted point after unfitted one */
1899
if ( psh_point_is_fitted( next ) )
1903
/* now interpolate between them */
1905
FT_Pos org_a, org_ab, cur_a, cur_ab;
1906
FT_Pos org_c, org_ac, cur_c;
1910
if ( first->org_u <= next->org_u )
1912
org_a = first->org_u;
1913
cur_a = first->cur_u;
1914
org_ab = next->org_u - org_a;
1915
cur_ab = next->cur_u - cur_a;
1919
org_a = next->org_u;
1920
cur_a = next->cur_u;
1921
org_ab = first->org_u - org_a;
1922
cur_ab = first->cur_u - cur_a;
1925
scale_ab = 0x10000L;
1927
scale_ab = FT_DivFix( cur_ab, org_ab );
1929
point = first->next;
1932
org_c = point->org_u;
1933
org_ac = org_c - org_a;
1937
/* on the left of the interpolation zone */
1938
cur_c = cur_a + FT_MulFix( org_ac, scale );
1940
else if ( org_ac >= org_ab )
1942
/* on the right on the interpolation zone */
1943
cur_c = cur_a + cur_ab + FT_MulFix( org_ac - org_ab, scale );
1947
/* within the interpolation zone */
1948
cur_c = cur_a + FT_MulFix( org_ac, scale_ab );
1951
point->cur_u = cur_c;
1953
point = point->next;
1955
} while ( point != next );
1958
/* keep going until all points in the contours have been processed */
1961
} while ( first != start );
1969
/*************************************************************************/
1970
/*************************************************************************/
1972
/***** HIGH-LEVEL INTERFACE *****/
1974
/*************************************************************************/
1975
/*************************************************************************/
1978
ps_hints_apply( PS_Hints ps_hints,
1979
FT_Outline* outline,
1980
PSH_Globals globals,
1981
FT_Render_Mode hint_mode )
1983
PSH_GlyphRec glyphrec;
1984
PSH_Glyph glyph = &glyphrec;
1992
/* something to do? */
1993
if ( outline->n_points == 0 || outline->n_contours == 0 )
1998
memory = globals->memory;
2000
if ( ps_debug_glyph )
2002
psh_glyph_done( ps_debug_glyph );
2003
FT_FREE( ps_debug_glyph );
2006
if ( FT_NEW( glyph ) )
2009
ps_debug_glyph = glyph;
2011
#endif /* DEBUG_HINTER */
2013
error = psh_glyph_init( glyph, outline, ps_hints, globals );
2017
/* try to optimize the y_scale so that the top of non-capital letters
2018
* is aligned on a pixel boundary whenever possible
2021
PSH_Dimension dim_x = &glyph->globals->dimension[0];
2022
PSH_Dimension dim_y = &glyph->globals->dimension[1];
2024
FT_Fixed x_scale = dim_x->scale_mult;
2025
FT_Fixed y_scale = dim_y->scale_mult;
2031
scaled = FT_MulFix( globals->blues.normal_top.zones->org_ref, y_scale );
2032
fitted = FT_PIX_ROUND( scaled );
2034
if (scaled != fitted ) {
2035
y_scale = FT_MulDiv( y_scale, fitted, scaled );
2037
if ( fitted < scaled )
2038
x_scale -= x_scale / 50;
2040
psh_globals_set_scale( glyph->globals, x_scale, y_scale, 0, 0 );
2044
glyph->do_horz_hints = 1;
2045
glyph->do_vert_hints = 1;
2047
glyph->do_horz_snapping = FT_BOOL( hint_mode == FT_RENDER_MODE_MONO ||
2048
hint_mode == FT_RENDER_MODE_LCD );
2050
glyph->do_vert_snapping = FT_BOOL( hint_mode == FT_RENDER_MODE_MONO ||
2051
hint_mode == FT_RENDER_MODE_LCD_V );
2053
glyph->do_stem_adjust = FT_BOOL( hint_mode != FT_RENDER_MODE_LIGHT );
2055
for ( dimension = 0; dimension < 2; dimension++ )
2057
/* load outline coordinates into glyph */
2058
psh_glyph_load_points( glyph, dimension );
2060
/* compute local extrema */
2061
psh_glyph_compute_extrema( glyph );
2063
/* compute aligned stem/hints positions */
2064
psh_hint_table_align_hints( &glyph->hint_tables[dimension],
2069
/* find strong points, align them, then interpolate others */
2070
psh_glyph_find_strong_points( glyph, dimension );
2071
if ( dimension == 1 )
2072
psh_glyph_find_blue_points( &globals->blues, glyph );
2073
psh_glyph_interpolate_strong_points( glyph, dimension );
2074
psh_glyph_interpolate_normal_points( glyph, dimension );
2075
psh_glyph_interpolate_other_points( glyph, dimension );
2077
/* save hinted coordinates back to outline */
2078
psh_glyph_save_points( glyph, dimension );
2083
#ifndef DEBUG_HINTER
2084
psh_glyph_done( glyph );