1 /***************************************************************************/
5 /* Auto-fitter hinting routines for latin script (body). */
7 /* Copyright 2003, 2004, 2005, 2006, 2007 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 /***************************************************************************/
28 FT_LOCAL_DEF( FT_Error
)
29 af_latin2_hints_compute_segments( AF_GlyphHints hints
,
33 af_latin2_hints_link_segments( AF_GlyphHints hints
,
36 /*************************************************************************/
37 /*************************************************************************/
39 /***** L A T I N G L O B A L M E T R I C S *****/
41 /*************************************************************************/
42 /*************************************************************************/
45 af_latin2_metrics_init_widths( AF_LatinMetrics metrics
,
49 /* scan the array of segments in each direction */
50 AF_GlyphHintsRec hints
[1];
53 af_glyph_hints_init( hints
, face
->memory
);
55 metrics
->axis
[AF_DIMENSION_HORZ
].width_count
= 0;
56 metrics
->axis
[AF_DIMENSION_VERT
].width_count
= 0;
62 AF_LatinMetricsRec dummy
[1];
63 AF_Scaler scaler
= &dummy
->root
.scaler
;
66 glyph_index
= FT_Get_Char_Index( face
, charcode
);
67 if ( glyph_index
== 0 )
70 error
= FT_Load_Glyph( face
, glyph_index
, FT_LOAD_NO_SCALE
);
71 if ( error
|| face
->glyph
->outline
.n_points
<= 0 )
76 dummy
->units_per_em
= metrics
->units_per_em
;
77 scaler
->x_scale
= scaler
->y_scale
= 0x10000L
;
78 scaler
->x_delta
= scaler
->y_delta
= 0;
80 scaler
->render_mode
= FT_RENDER_MODE_NORMAL
;
83 af_glyph_hints_rescale( hints
, (AF_ScriptMetrics
)dummy
);
85 error
= af_glyph_hints_reload( hints
, &face
->glyph
->outline
, 0 );
89 for ( dim
= 0; dim
< AF_DIMENSION_MAX
; dim
++ )
91 AF_LatinAxis axis
= &metrics
->axis
[dim
];
92 AF_AxisHints axhints
= &hints
->axis
[dim
];
93 AF_Segment seg
, limit
, link
;
94 FT_UInt num_widths
= 0;
97 error
= af_latin2_hints_compute_segments( hints
,
102 af_latin2_hints_link_segments( hints
,
105 seg
= axhints
->segments
;
106 limit
= seg
+ axhints
->num_segments
;
108 for ( ; seg
< limit
; seg
++ )
112 /* we only consider stem segments there! */
113 if ( link
&& link
->link
== seg
&& link
> seg
)
118 dist
= seg
->pos
- link
->pos
;
122 if ( num_widths
< AF_LATIN_MAX_WIDTHS
)
123 axis
->widths
[ num_widths
++ ].org
= dist
;
127 af_sort_widths( num_widths
, axis
->widths
);
128 axis
->width_count
= num_widths
;
132 for ( dim
= 0; dim
< AF_DIMENSION_MAX
; dim
++ )
134 AF_LatinAxis axis
= &metrics
->axis
[dim
];
138 stdw
= ( axis
->width_count
> 0 )
139 ? axis
->widths
[0].org
140 : AF_LATIN_CONSTANT( metrics
, 50 );
142 /* let's try 20% of the smallest width */
143 axis
->edge_distance_threshold
= stdw
/ 5;
144 axis
->standard_width
= stdw
;
145 axis
->extra_light
= 0;
149 af_glyph_hints_done( hints
);
154 #define AF_LATIN_MAX_TEST_CHARACTERS 12
157 static const char* const af_latin2_blue_chars
[AF_LATIN_MAX_BLUES
] =
169 af_latin2_metrics_init_blues( AF_LatinMetrics metrics
,
172 FT_Pos flats
[AF_LATIN_MAX_TEST_CHARACTERS
];
173 FT_Pos rounds
[AF_LATIN_MAX_TEST_CHARACTERS
];
179 AF_LatinAxis axis
= &metrics
->axis
[AF_DIMENSION_VERT
];
180 FT_GlyphSlot glyph
= face
->glyph
;
183 /* we compute the blues simply by loading each character from the */
184 /* 'af_latin2_blue_chars[blues]' string, then compute its top-most or */
185 /* bottom-most points (depending on `AF_IS_TOP_BLUE') */
187 AF_LOG(( "blue zones computation\n" ));
188 AF_LOG(( "------------------------------------------------\n" ));
190 for ( bb
= 0; bb
< AF_LATIN_BLUE_MAX
; bb
++ )
192 const char* p
= af_latin2_blue_chars
[bb
];
193 const char* limit
= p
+ AF_LATIN_MAX_TEST_CHARACTERS
;
198 AF_LOG(( "blue %3d: ", bb
));
203 for ( ; p
< limit
&& *p
; p
++ )
206 FT_Int best_point
, best_y
, best_first
, best_last
;
211 AF_LOG(( "'%c'", *p
));
213 /* load the character in the face -- skip unknown or empty ones */
214 glyph_index
= FT_Get_Char_Index( face
, (FT_UInt
)*p
);
215 if ( glyph_index
== 0 )
218 error
= FT_Load_Glyph( face
, glyph_index
, FT_LOAD_NO_SCALE
);
219 if ( error
|| glyph
->outline
.n_points
<= 0 )
222 /* now compute min or max point indices and coordinates */
223 points
= glyph
->outline
.points
;
225 best_y
= 0; /* make compiler happy */
226 best_first
= 0; /* ditto */
227 best_last
= 0; /* ditto */
235 for ( nn
= 0; nn
< glyph
->outline
.n_contours
; first
= last
+1, nn
++ )
237 FT_Int old_best_point
= best_point
;
241 last
= glyph
->outline
.contours
[nn
];
243 /* Avoid single-point contours since they are never rasterized. */
244 /* In some fonts, they correspond to mark attachment points */
245 /* which are way outside of the glyph's real outline. */
249 if ( AF_LATIN_IS_TOP_BLUE( bb
) )
251 for ( pp
= first
; pp
<= last
; pp
++ )
252 if ( best_point
< 0 || points
[pp
].y
> best_y
)
255 best_y
= points
[pp
].y
;
260 for ( pp
= first
; pp
<= last
; pp
++ )
261 if ( best_point
< 0 || points
[pp
].y
< best_y
)
264 best_y
= points
[pp
].y
;
268 if ( best_point
!= old_best_point
)
274 AF_LOG(( "%5d", best_y
));
277 /* now check whether the point belongs to a straight or round */
278 /* segment; we first need to find in which contour the extremum */
279 /* lies, then inspect its previous and next points */
281 FT_Int start
, end
, prev
, next
;
285 /* now look for the previous and next points that are not on the */
286 /* same Y coordinate. Threshold the `closeness'... */
287 start
= end
= best_point
;
292 if ( prev
< best_first
)
295 dist
= points
[prev
].y
- best_y
;
296 if ( dist
< -5 || dist
> 5 )
301 } while ( start
!= best_point
);
306 if ( next
> best_last
)
309 dist
= points
[next
].y
- best_y
;
310 if ( dist
< -5 || dist
> 5 )
315 } while ( end
!= best_point
);
317 /* now, set the `round' flag depending on the segment's kind */
319 FT_CURVE_TAG( glyph
->outline
.tags
[start
] ) != FT_CURVE_TAG_ON
||
320 FT_CURVE_TAG( glyph
->outline
.tags
[ end
] ) != FT_CURVE_TAG_ON
);
322 AF_LOG(( "%c ", round
? 'r' : 'f' ));
326 rounds
[num_rounds
++] = best_y
;
328 flats
[num_flats
++] = best_y
;
333 if ( num_flats
== 0 && num_rounds
== 0 )
336 * we couldn't find a single glyph to compute this blue zone,
337 * we will simply ignore it then
339 AF_LOG(( "empty!\n" ));
343 /* we have computed the contents of the `rounds' and `flats' tables, */
344 /* now determine the reference and overshoot position of the blue -- */
345 /* we simply take the median value after a simple sort */
346 af_sort_pos( num_rounds
, rounds
);
347 af_sort_pos( num_flats
, flats
);
349 blue
= & axis
->blues
[axis
->blue_count
];
350 blue_ref
= & blue
->ref
.org
;
351 blue_shoot
= & blue
->shoot
.org
;
355 if ( num_flats
== 0 )
358 *blue_shoot
= rounds
[num_rounds
/ 2];
360 else if ( num_rounds
== 0 )
363 *blue_shoot
= flats
[num_flats
/ 2];
367 *blue_ref
= flats
[num_flats
/ 2];
368 *blue_shoot
= rounds
[num_rounds
/ 2];
371 /* there are sometimes problems: if the overshoot position of top */
372 /* zones is under its reference position, or the opposite for bottom */
373 /* zones. We must thus check everything there and correct the errors */
374 if ( *blue_shoot
!= *blue_ref
)
376 FT_Pos ref
= *blue_ref
;
377 FT_Pos shoot
= *blue_shoot
;
378 FT_Bool over_ref
= FT_BOOL( shoot
> ref
);
381 if ( AF_LATIN_IS_TOP_BLUE( bb
) ^ over_ref
)
382 *blue_shoot
= *blue_ref
= ( shoot
+ ref
) / 2;
386 if ( AF_LATIN_IS_TOP_BLUE( bb
) )
387 blue
->flags
|= AF_LATIN_BLUE_TOP
;
390 * The following flags is used later to adjust the y and x scales
391 * in order to optimize the pixel grid alignment of the top of small
394 if ( bb
== AF_LATIN_BLUE_SMALL_TOP
)
395 blue
->flags
|= AF_LATIN_BLUE_ADJUSTMENT
;
397 AF_LOG(( "-- ref = %ld, shoot = %ld\n", *blue_ref
, *blue_shoot
));
404 FT_LOCAL_DEF( FT_Error
)
405 af_latin2_metrics_init( AF_LatinMetrics metrics
,
408 FT_Error error
= AF_Err_Ok
;
409 FT_CharMap oldmap
= face
->charmap
;
412 static const FT_Encoding latin_encodings
[] =
415 FT_ENCODING_APPLE_ROMAN
,
416 FT_ENCODING_ADOBE_STANDARD
,
417 FT_ENCODING_ADOBE_LATIN_1
,
418 FT_ENCODING_NONE
/* end of list */
422 metrics
->units_per_em
= face
->units_per_EM
;
424 /* do we have a latin charmap in there? */
425 for ( ee
= 0; latin_encodings
[ee
] != FT_ENCODING_NONE
; ee
++ )
427 error
= FT_Select_Charmap( face
, latin_encodings
[ee
] );
434 /* For now, compute the standard width and height from the `o'. */
435 af_latin2_metrics_init_widths( metrics
, face
, 'o' );
436 af_latin2_metrics_init_blues( metrics
, face
);
439 FT_Set_Charmap( face
, oldmap
);
445 af_latin2_metrics_scale_dim( AF_LatinMetrics metrics
,
455 if ( dim
== AF_DIMENSION_HORZ
)
457 scale
= scaler
->x_scale
;
458 delta
= scaler
->x_delta
;
462 scale
= scaler
->y_scale
;
463 delta
= scaler
->y_delta
;
466 axis
= &metrics
->axis
[dim
];
468 if ( axis
->org_scale
== scale
&& axis
->org_delta
== delta
)
471 axis
->org_scale
= scale
;
472 axis
->org_delta
= delta
;
475 * correct Y scale to optimize the alignment of the top of small
476 * letters to the pixel grid
478 if ( dim
== AF_DIMENSION_VERT
)
480 AF_LatinAxis vaxis
= &metrics
->axis
[AF_DIMENSION_VERT
];
481 AF_LatinBlue blue
= NULL
;
484 for ( nn
= 0; nn
< vaxis
->blue_count
; nn
++ )
486 if ( vaxis
->blues
[nn
].flags
& AF_LATIN_BLUE_ADJUSTMENT
)
488 blue
= &vaxis
->blues
[nn
];
495 FT_Pos scaled
= FT_MulFix( blue
->shoot
.org
, scaler
->y_scale
);
496 FT_Pos fitted
= ( scaled
+ 40 ) & ~63;
499 if ( scaled
!= fitted
) {
500 scale
= FT_MulDiv( scale
, fitted
, scaled
);
501 AF_LOG(( "== scaled x-top = %.2g fitted = %.2g, scaling = %.4g\n", scaled
/64.0, fitted
/64.0, (fitted
*1.0)/scaled
));
510 if ( dim
== AF_DIMENSION_HORZ
)
512 metrics
->root
.scaler
.x_scale
= scale
;
513 metrics
->root
.scaler
.x_delta
= delta
;
517 metrics
->root
.scaler
.y_scale
= scale
;
518 metrics
->root
.scaler
.y_delta
= delta
;
521 /* scale the standard widths */
522 for ( nn
= 0; nn
< axis
->width_count
; nn
++ )
524 AF_Width width
= axis
->widths
+ nn
;
527 width
->cur
= FT_MulFix( width
->org
, scale
);
528 width
->fit
= width
->cur
;
531 /* an extra-light axis corresponds to a standard width that is */
532 /* smaller than 0.75 pixels */
534 (FT_Bool
)( FT_MulFix( axis
->standard_width
, scale
) < 32 + 8 );
536 if ( dim
== AF_DIMENSION_VERT
)
538 /* scale the blue zones */
539 for ( nn
= 0; nn
< axis
->blue_count
; nn
++ )
541 AF_LatinBlue blue
= &axis
->blues
[nn
];
545 blue
->ref
.cur
= FT_MulFix( blue
->ref
.org
, scale
) + delta
;
546 blue
->ref
.fit
= blue
->ref
.cur
;
547 blue
->shoot
.cur
= FT_MulFix( blue
->shoot
.org
, scale
) + delta
;
548 blue
->shoot
.fit
= blue
->shoot
.cur
;
549 blue
->flags
&= ~AF_LATIN_BLUE_ACTIVE
;
551 /* a blue zone is only active if it is less than 3/4 pixels tall */
552 dist
= FT_MulFix( blue
->ref
.org
- blue
->shoot
.org
, scale
);
553 if ( dist
<= 48 && dist
>= -48 )
555 FT_Pos delta1
, delta2
;
557 delta1
= blue
->shoot
.org
- blue
->ref
.org
;
562 delta2
= FT_MulFix( delta2
, scale
);
566 else if ( delta2
< 64 )
567 delta2
= 32 + ( ( ( delta2
- 32 ) + 16 ) & ~31 );
569 delta2
= FT_PIX_ROUND( delta2
);
574 blue
->ref
.fit
= FT_PIX_ROUND( blue
->ref
.cur
);
575 blue
->shoot
.fit
= blue
->ref
.fit
+ delta2
;
577 AF_LOG(( ">> activating blue zone %d: ref.cur=%.2g ref.fit=%.2g shoot.cur=%.2g shoot.fit=%.2g\n",
578 nn
, blue
->ref
.cur
/64.0, blue
->ref
.fit
/64.0,
579 blue
->shoot
.cur
/64.0, blue
->shoot
.fit
/64.0 ));
581 blue
->flags
|= AF_LATIN_BLUE_ACTIVE
;
589 af_latin2_metrics_scale( AF_LatinMetrics metrics
,
592 metrics
->root
.scaler
.render_mode
= scaler
->render_mode
;
593 metrics
->root
.scaler
.face
= scaler
->face
;
595 af_latin2_metrics_scale_dim( metrics
, scaler
, AF_DIMENSION_HORZ
);
596 af_latin2_metrics_scale_dim( metrics
, scaler
, AF_DIMENSION_VERT
);
600 /*************************************************************************/
601 /*************************************************************************/
603 /***** L A T I N G L Y P H A N A L Y S I S *****/
605 /*************************************************************************/
606 /*************************************************************************/
608 #define SORT_SEGMENTS
610 FT_LOCAL_DEF( FT_Error
)
611 af_latin2_hints_compute_segments( AF_GlyphHints hints
,
614 AF_AxisHints axis
= &hints
->axis
[dim
];
615 FT_Memory memory
= hints
->memory
;
616 FT_Error error
= AF_Err_Ok
;
617 AF_Segment segment
= NULL
;
619 AF_Point
* contour
= hints
->contours
;
620 AF_Point
* contour_limit
= contour
+ hints
->num_contours
;
621 AF_Direction major_dir
, segment_dir
;
626 seg0
.flags
= AF_EDGE_NORMAL
;
628 major_dir
= (AF_Direction
)FT_ABS( axis
->major_dir
);
629 segment_dir
= major_dir
;
631 axis
->num_segments
= 0;
633 /* set up (u,v) in each point */
634 if ( dim
== AF_DIMENSION_HORZ
)
636 AF_Point point
= hints
->points
;
637 AF_Point limit
= point
+ hints
->num_points
;
640 for ( ; point
< limit
; point
++ )
642 point
->u
= point
->fx
;
643 point
->v
= point
->fy
;
648 AF_Point point
= hints
->points
;
649 AF_Point limit
= point
+ hints
->num_points
;
652 for ( ; point
< limit
; point
++ )
654 point
->u
= point
->fy
;
655 point
->v
= point
->fx
;
659 /* do each contour separately */
660 for ( ; contour
< contour_limit
; contour
++ )
662 AF_Point point
= contour
[0];
663 AF_Point start
= point
;
664 AF_Point last
= point
->prev
;
667 if ( point
== last
) /* skip singletons -- just in case */
670 /* already on an edge ?, backtrack to find its start */
671 if ( FT_ABS( point
->in_dir
) == major_dir
)
675 while ( point
->in_dir
== start
->in_dir
)
678 else /* otherwise, find first segment start, if any */
680 while ( FT_ABS( point
->out_dir
) != major_dir
)
684 if ( point
== start
)
694 FT_Pos min_u
, min_v
, max_u
, max_v
;
696 /* we're at the start of a new segment */
697 FT_ASSERT( FT_ABS( point
->out_dir
) == major_dir
&&
698 point
->in_dir
!= point
->out_dir
);
701 min_u
= max_u
= point
->u
;
702 min_v
= max_v
= point
->v
;
706 while ( point
->out_dir
== first
->out_dir
)
710 if ( point
->u
< min_u
)
713 if ( point
->u
> max_u
)
717 if ( point
->v
< min_v
)
720 if ( point
->v
> max_v
)
723 /* record new segment */
724 error
= af_axis_hints_new_segment( axis
, memory
, &segment
);
729 segment
->dir
= first
->out_dir
;
730 segment
->first
= first
;
731 segment
->last
= point
;
732 segment
->contour
= contour
;
733 segment
->pos
= (FT_Short
)(( min_u
+ max_u
) >> 1);
734 segment
->min_coord
= (FT_Short
) min_v
;
735 segment
->max_coord
= (FT_Short
) max_v
;
736 segment
->height
= (FT_Short
)(max_v
- min_v
);
738 /* a segment is round if it doesn't have successive */
739 /* on-curve points. */
742 AF_Point last
= point
;
743 AF_Flags f0
= (AF_Flags
)(pt
->flags
& AF_FLAG_CONTROL
);
747 segment
->flags
&= ~AF_EDGE_ROUND
;
749 for ( ; pt
!= last
; f0
= f1
)
752 f1
= (AF_Flags
)(pt
->flags
& AF_FLAG_CONTROL
);
758 segment
->flags
|= AF_EDGE_ROUND
;
762 /* this can happen in the case of a degenerate contour
763 * e.g. a 2-point vertical contour
765 if ( point
== start
)
768 /* jump to the start of the next segment, if any */
769 while ( FT_ABS(point
->out_dir
) != major_dir
)
773 if ( point
== start
)
782 /* now slightly increase the height of segments when this makes */
783 /* sense -- this is used to better detect and ignore serifs */
785 AF_Segment segments
= axis
->segments
;
786 AF_Segment segments_end
= segments
+ axis
->num_segments
;
789 for ( segment
= segments
; segment
< segments_end
; segment
++ )
791 AF_Point first
= segment
->first
;
792 AF_Point last
= segment
->last
;
794 FT_Pos first_v
= first
->v
;
795 FT_Pos last_v
= last
->v
;
801 if ( first_v
< last_v
)
804 if ( p
->v
< first_v
)
805 segment
->height
= (FT_Short
)( segment
->height
+
806 ( ( first_v
- p
->v
) >> 1 ) );
810 segment
->height
= (FT_Short
)( segment
->height
+
811 ( ( p
->v
- last_v
) >> 1 ) );
816 if ( p
->v
> first_v
)
817 segment
->height
= (FT_Short
)( segment
->height
+
818 ( ( p
->v
- first_v
) >> 1 ) );
822 segment
->height
= (FT_Short
)( segment
->height
+
823 ( ( last_v
- p
->v
) >> 1 ) );
828 #ifdef AF_SORT_SEGMENTS
829 /* place all segments with a negative direction to the start
830 * of the array, used to speed up segment linking later...
833 AF_Segment segments
= axis
->segments
;
834 FT_UInt count
= axis
->num_segments
;
837 for (ii
= 0; ii
< count
; ii
++)
839 if ( segments
[ii
].dir
> 0 )
841 for (jj
= ii
+1; jj
< count
; jj
++)
843 if ( segments
[jj
].dir
< 0 )
848 segments
[ii
] = segments
[jj
];
859 axis
->mid_segments
= ii
;
869 af_latin2_hints_link_segments( AF_GlyphHints hints
,
872 AF_AxisHints axis
= &hints
->axis
[dim
];
873 AF_Segment segments
= axis
->segments
;
874 AF_Segment segment_limit
= segments
+ axis
->num_segments
;
875 #ifdef AF_SORT_SEGMENTS
876 AF_Segment segment_mid
= segments
+ axis
->mid_segments
;
878 FT_Pos len_threshold
, len_score
;
879 AF_Segment seg1
, seg2
;
882 len_threshold
= AF_LATIN_CONSTANT( hints
->metrics
, 8 );
883 if ( len_threshold
== 0 )
886 len_score
= AF_LATIN_CONSTANT( hints
->metrics
, 6000 );
888 #ifdef AF_SORT_SEGMENTS
889 for ( seg1
= segments
; seg1
< segment_mid
; seg1
++ )
891 if ( seg1
->dir
!= axis
->major_dir
|| seg1
->first
== seg1
->last
)
894 for ( seg2
= segment_mid
; seg2
< segment_limit
; seg2
++ )
896 /* now compare each segment to the others */
897 for ( seg1
= segments
; seg1
< segment_limit
; seg1
++ )
899 /* the fake segments are introduced to hint the metrics -- */
900 /* we must never link them to anything */
901 if ( seg1
->dir
!= axis
->major_dir
|| seg1
->first
== seg1
->last
)
904 for ( seg2
= segments
; seg2
< segment_limit
; seg2
++ )
905 if ( seg1
->dir
+ seg2
->dir
== 0 && seg2
->pos
> seg1
->pos
)
908 FT_Pos pos1
= seg1
->pos
;
909 FT_Pos pos2
= seg2
->pos
;
910 FT_Pos dist
= pos2
- pos1
;
917 FT_Pos min
= seg1
->min_coord
;
918 FT_Pos max
= seg1
->max_coord
;
922 if ( min
< seg2
->min_coord
)
923 min
= seg2
->min_coord
;
925 if ( max
> seg2
->max_coord
)
926 max
= seg2
->max_coord
;
929 if ( len
>= len_threshold
)
931 score
= dist
+ len_score
/ len
;
932 if ( score
< seg1
->score
)
938 if ( score
< seg2
->score
)
948 /* now, compute the `serif' segments */
949 for ( seg1
= segments
; seg1
< segment_limit
; seg1
++ )
955 if ( seg2
->link
!= seg1
)
958 seg1
->serif
= seg2
->link
;
965 FT_LOCAL_DEF( FT_Error
)
966 af_latin2_hints_compute_edges( AF_GlyphHints hints
,
969 AF_AxisHints axis
= &hints
->axis
[dim
];
970 FT_Error error
= AF_Err_Ok
;
971 FT_Memory memory
= hints
->memory
;
972 AF_LatinAxis laxis
= &((AF_LatinMetrics
)hints
->metrics
)->axis
[dim
];
974 AF_Segment segments
= axis
->segments
;
975 AF_Segment segment_limit
= segments
+ axis
->num_segments
;
980 FT_Pos edge_distance_threshold
;
981 FT_Pos segment_length_threshold
;
986 scale
= ( dim
== AF_DIMENSION_HORZ
) ? hints
->x_scale
989 up_dir
= ( dim
== AF_DIMENSION_HORZ
) ? AF_DIR_UP
993 * We want to ignore very small (mostly serif) segments, we do that
994 * by ignoring those that whose length is less than a given fraction
995 * of the standard width. If there is no standard width, we ignore
996 * those that are less than a given size in pixels
998 * also, unlink serif segments that are linked to segments farther
999 * than 50% of the standard width
1001 if ( dim
== AF_DIMENSION_HORZ
)
1003 if ( laxis
->width_count
> 0 )
1004 segment_length_threshold
= (laxis
->standard_width
* 10 ) >> 4;
1006 segment_length_threshold
= FT_DivFix( 64, hints
->y_scale
);
1009 segment_length_threshold
= 0;
1011 /*********************************************************************/
1013 /* We will begin by generating a sorted table of edges for the */
1014 /* current direction. To do so, we simply scan each segment and try */
1015 /* to find an edge in our table that corresponds to its position. */
1017 /* If no edge is found, we create and insert a new edge in the */
1018 /* sorted table. Otherwise, we simply add the segment to the edge's */
1019 /* list which will be processed in the second step to compute the */
1020 /* edge's properties. */
1022 /* Note that the edges table is sorted along the segment/edge */
1025 /*********************************************************************/
1027 edge_distance_threshold
= FT_MulFix( laxis
->edge_distance_threshold
,
1029 if ( edge_distance_threshold
> 64 / 4 )
1030 edge_distance_threshold
= 64 / 4;
1032 edge_distance_threshold
= FT_DivFix( edge_distance_threshold
,
1035 for ( seg
= segments
; seg
< segment_limit
; seg
++ )
1041 if ( seg
->height
< segment_length_threshold
)
1044 /* A special case for serif edges: If they are smaller than */
1045 /* 1.5 pixels we ignore them. */
1048 FT_Pos dist
= seg
->serif
->pos
- seg
->pos
;
1053 if (dist
>= laxis
->standard_width
>> 1)
1055 /* unlink this serif, it is too distant from its reference stem */
1058 else if ( 2*seg
->height
< 3 * segment_length_threshold
)
1062 /* look for an edge corresponding to the segment */
1063 for ( ee
= 0; ee
< axis
->num_edges
; ee
++ )
1065 AF_Edge edge
= axis
->edges
+ ee
;
1069 dist
= seg
->pos
- edge
->fpos
;
1073 if ( dist
< edge_distance_threshold
&& edge
->dir
== seg
->dir
)
1085 /* insert a new edge in the list and */
1086 /* sort according to the position */
1087 error
= af_axis_hints_new_edge( axis
, seg
->pos
, seg
->dir
, memory
, &edge
);
1091 /* add the segment to the new edge's list */
1096 edge
->fpos
= seg
->pos
;
1097 edge
->dir
= seg
->dir
;
1098 edge
->opos
= edge
->pos
= FT_MulFix( seg
->pos
, scale
);
1099 seg
->edge_next
= seg
;
1103 /* if an edge was found, simply add the segment to the edge's */
1105 seg
->edge_next
= found
->first
;
1106 found
->last
->edge_next
= seg
;
1112 /*********************************************************************/
1114 /* Good, we will now compute each edge's properties according to */
1115 /* segments found on its position. Basically, these are: */
1117 /* - edge's main direction */
1118 /* - stem edge, serif edge or both (which defaults to stem then) */
1119 /* - rounded edge, straight or both (which defaults to straight) */
1120 /* - link for edge */
1122 /*********************************************************************/
1124 /* first of all, set the `edge' field in each segment -- this is */
1125 /* required in order to compute edge links */
1128 * Note that removing this loop and setting the `edge' field of each
1129 * segment directly in the code above slows down execution speed for
1130 * some reasons on platforms like the Sun.
1133 AF_Edge edges
= axis
->edges
;
1134 AF_Edge edge_limit
= edges
+ axis
->num_edges
;
1138 for ( edge
= edges
; edge
< edge_limit
; edge
++ )
1145 seg
= seg
->edge_next
;
1147 } while ( seg
!= edge
->first
);
1150 /* now, compute each edge properties */
1151 for ( edge
= edges
; edge
< edge_limit
; edge
++ )
1153 FT_Int is_round
= 0; /* does it contain round segments? */
1154 FT_Int is_straight
= 0; /* does it contain straight segments? */
1155 FT_Pos ups
= 0; /* number of upwards segments */
1156 FT_Pos downs
= 0; /* number of downwards segments */
1166 /* check for roundness of segment */
1167 if ( seg
->flags
& AF_EDGE_ROUND
)
1172 /* check for segment direction */
1173 if ( seg
->dir
== up_dir
)
1174 ups
+= seg
->max_coord
-seg
->min_coord
;
1176 downs
+= seg
->max_coord
-seg
->min_coord
;
1178 /* check for links -- if seg->serif is set, then seg->link must */
1180 is_serif
= (FT_Bool
)( seg
->serif
&&
1182 seg
->serif
->edge
!= edge
);
1184 if ( ( seg
->link
&& seg
->link
->edge
!= NULL
) || is_serif
)
1196 edge2
= edge
->serif
;
1205 edge_delta
= edge
->fpos
- edge2
->fpos
;
1206 if ( edge_delta
< 0 )
1207 edge_delta
= -edge_delta
;
1209 seg_delta
= seg
->pos
- seg2
->pos
;
1210 if ( seg_delta
< 0 )
1211 seg_delta
= -seg_delta
;
1213 if ( seg_delta
< edge_delta
)
1221 edge
->serif
= edge2
;
1222 edge2
->flags
|= AF_EDGE_SERIF
;
1228 seg
= seg
->edge_next
;
1230 } while ( seg
!= edge
->first
);
1232 /* set the round/straight flags */
1233 edge
->flags
= AF_EDGE_NORMAL
;
1235 if ( is_round
> 0 && is_round
>= is_straight
)
1236 edge
->flags
|= AF_EDGE_ROUND
;
1239 /* set the edge's main direction */
1240 edge
->dir
= AF_DIR_NONE
;
1243 edge
->dir
= (FT_Char
)up_dir
;
1245 else if ( ups
< downs
)
1246 edge
->dir
= (FT_Char
)-up_dir
;
1248 else if ( ups
== downs
)
1249 edge
->dir
= 0; /* both up and down! */
1252 /* gets rid of serifs if link is set */
1253 /* XXX: This gets rid of many unpleasant artefacts! */
1254 /* Example: the `c' in cour.pfa at size 13 */
1256 if ( edge
->serif
&& edge
->link
)
1266 FT_LOCAL_DEF( FT_Error
)
1267 af_latin2_hints_detect_features( AF_GlyphHints hints
,
1273 error
= af_latin2_hints_compute_segments( hints
, dim
);
1276 af_latin2_hints_link_segments( hints
, dim
);
1278 error
= af_latin2_hints_compute_edges( hints
, dim
);
1284 FT_LOCAL_DEF( void )
1285 af_latin2_hints_compute_blue_edges( AF_GlyphHints hints
,
1286 AF_LatinMetrics metrics
)
1288 AF_AxisHints axis
= &hints
->axis
[ AF_DIMENSION_VERT
];
1289 AF_Edge edge
= axis
->edges
;
1290 AF_Edge edge_limit
= edge
+ axis
->num_edges
;
1291 AF_LatinAxis latin
= &metrics
->axis
[ AF_DIMENSION_VERT
];
1292 FT_Fixed scale
= latin
->scale
;
1293 FT_Pos best_dist0
; /* initial threshold */
1296 /* compute the initial threshold as a fraction of the EM size */
1297 best_dist0
= FT_MulFix( metrics
->units_per_em
/ 40, scale
);
1299 if ( best_dist0
> 64 / 2 )
1300 best_dist0
= 64 / 2;
1302 /* compute which blue zones are active, i.e. have their scaled */
1303 /* size < 3/4 pixels */
1305 /* for each horizontal edge search the blue zone which is closest */
1306 for ( ; edge
< edge_limit
; edge
++ )
1309 AF_Width best_blue
= NULL
;
1310 FT_Pos best_dist
= best_dist0
;
1312 for ( bb
= 0; bb
< AF_LATIN_BLUE_MAX
; bb
++ )
1314 AF_LatinBlue blue
= latin
->blues
+ bb
;
1315 FT_Bool is_top_blue
, is_major_dir
;
1318 /* skip inactive blue zones (i.e., those that are too small) */
1319 if ( !( blue
->flags
& AF_LATIN_BLUE_ACTIVE
) )
1322 /* if it is a top zone, check for right edges -- if it is a bottom */
1323 /* zone, check for left edges */
1325 /* of course, that's for TrueType */
1326 is_top_blue
= (FT_Byte
)( ( blue
->flags
& AF_LATIN_BLUE_TOP
) != 0 );
1327 is_major_dir
= FT_BOOL( edge
->dir
== axis
->major_dir
);
1329 /* if it is a top zone, the edge must be against the major */
1330 /* direction; if it is a bottom zone, it must be in the major */
1332 if ( is_top_blue
^ is_major_dir
)
1338 /* if it's a rounded edge, compare it to the overshoot position */
1339 /* if it's a flat edge, compare it to the reference position */
1340 if ( edge
->flags
& AF_EDGE_ROUND
)
1341 compare
= &blue
->shoot
;
1343 compare
= &blue
->ref
;
1345 dist
= edge
->fpos
- compare
->org
;
1349 dist
= FT_MulFix( dist
, scale
);
1350 if ( dist
< best_dist
)
1353 best_blue
= compare
;
1357 /* now, compare it to the overshoot position if the edge is */
1358 /* rounded, and if the edge is over the reference position of a */
1359 /* top zone, or under the reference position of a bottom zone */
1360 if ( edge
->flags
& AF_EDGE_ROUND
&& dist
!= 0 )
1362 FT_Bool is_under_ref
= FT_BOOL( edge
->fpos
< blue
->ref
.org
);
1365 if ( is_top_blue
^ is_under_ref
)
1367 blue
= latin
->blues
+ bb
;
1368 dist
= edge
->fpos
- blue
->shoot
.org
;
1372 dist
= FT_MulFix( dist
, scale
);
1373 if ( dist
< best_dist
)
1376 best_blue
= & blue
->shoot
;
1385 edge
->blue_edge
= best_blue
;
1391 af_latin2_hints_init( AF_GlyphHints hints
,
1392 AF_LatinMetrics metrics
)
1394 FT_Render_Mode mode
;
1395 FT_UInt32 scaler_flags
, other_flags
;
1396 FT_Face face
= metrics
->root
.scaler
.face
;
1399 af_glyph_hints_rescale( hints
, (AF_ScriptMetrics
)metrics
);
1402 * correct x_scale and y_scale if needed, since they may have
1403 * been modified `af_latin2_metrics_scale_dim' above
1405 hints
->x_scale
= metrics
->axis
[AF_DIMENSION_HORZ
].scale
;
1406 hints
->x_delta
= metrics
->axis
[AF_DIMENSION_HORZ
].delta
;
1407 hints
->y_scale
= metrics
->axis
[AF_DIMENSION_VERT
].scale
;
1408 hints
->y_delta
= metrics
->axis
[AF_DIMENSION_VERT
].delta
;
1410 /* compute flags depending on render mode, etc. */
1411 mode
= metrics
->root
.scaler
.render_mode
;
1413 #if 0 /* #ifdef AF_USE_WARPER */
1414 if ( mode
== FT_RENDER_MODE_LCD
|| mode
== FT_RENDER_MODE_LCD_V
)
1416 metrics
->root
.scaler
.render_mode
= mode
= FT_RENDER_MODE_NORMAL
;
1420 scaler_flags
= hints
->scaler_flags
;
1424 * We snap the width of vertical stems for the monochrome and
1425 * horizontal LCD rendering targets only.
1427 if ( mode
== FT_RENDER_MODE_MONO
|| mode
== FT_RENDER_MODE_LCD
)
1428 other_flags
|= AF_LATIN_HINTS_HORZ_SNAP
;
1431 * We snap the width of horizontal stems for the monochrome and
1432 * vertical LCD rendering targets only.
1434 if ( mode
== FT_RENDER_MODE_MONO
|| mode
== FT_RENDER_MODE_LCD_V
)
1435 other_flags
|= AF_LATIN_HINTS_VERT_SNAP
;
1438 * We adjust stems to full pixels only if we don't use the `light' mode.
1440 if ( mode
!= FT_RENDER_MODE_LIGHT
)
1441 other_flags
|= AF_LATIN_HINTS_STEM_ADJUST
;
1443 if ( mode
== FT_RENDER_MODE_MONO
)
1444 other_flags
|= AF_LATIN_HINTS_MONO
;
1447 * In `light' hinting mode we disable horizontal hinting completely.
1448 * We also do it if the face is italic.
1450 if ( mode
== FT_RENDER_MODE_LIGHT
||
1451 (face
->style_flags
& FT_STYLE_FLAG_ITALIC
) != 0 )
1452 scaler_flags
|= AF_SCALER_FLAG_NO_HORIZONTAL
;
1454 hints
->scaler_flags
= scaler_flags
;
1455 hints
->other_flags
= other_flags
;
1461 /*************************************************************************/
1462 /*************************************************************************/
1464 /***** L A T I N G L Y P H G R I D - F I T T I N G *****/
1466 /*************************************************************************/
1467 /*************************************************************************/
1469 /* snap a given width in scaled coordinates to one of the */
1470 /* current standard widths */
1473 af_latin2_snap_width( AF_Width widths
,
1478 FT_Pos best
= 64 + 32 + 2;
1479 FT_Pos reference
= width
;
1483 for ( n
= 0; n
< count
; n
++ )
1500 scaled
= FT_PIX_ROUND( reference
);
1502 if ( width
>= reference
)
1504 if ( width
< scaled
+ 48 )
1509 if ( width
> scaled
- 48 )
1517 /* compute the snapped width of a given stem */
1520 af_latin2_compute_stem_width( AF_GlyphHints hints
,
1523 AF_Edge_Flags base_flags
,
1524 AF_Edge_Flags stem_flags
)
1526 AF_LatinMetrics metrics
= (AF_LatinMetrics
) hints
->metrics
;
1527 AF_LatinAxis axis
= & metrics
->axis
[dim
];
1528 FT_Pos dist
= width
;
1530 FT_Int vertical
= ( dim
== AF_DIMENSION_VERT
);
1533 FT_UNUSED(base_flags
);
1535 if ( !AF_LATIN_HINTS_DO_STEM_ADJUST( hints
) ||
1545 if ( ( vertical
&& !AF_LATIN_HINTS_DO_VERT_SNAP( hints
) ) ||
1546 ( !vertical
&& !AF_LATIN_HINTS_DO_HORZ_SNAP( hints
) ) )
1548 /* smooth hinting process: very lightly quantize the stem width */
1550 /* leave the widths of serifs alone */
1552 if ( ( stem_flags
& AF_EDGE_SERIF
) && vertical
&& ( dist
< 3 * 64 ) )
1556 else if ( ( base_flags
& AF_EDGE_ROUND
) )
1561 else if ( dist
< 56 )
1564 if ( axis
->width_count
> 0 )
1569 /* compare to standard width */
1570 if ( axis
->width_count
> 0 )
1572 delta
= dist
- axis
->widths
[0].cur
;
1579 dist
= axis
->widths
[0].cur
;
1587 if ( dist
< 3 * 64 )
1595 else if ( delta
< 32 )
1598 else if ( delta
< 54 )
1605 dist
= ( dist
+ 32 ) & ~63;
1610 /* strong hinting process: snap the stem width to integer pixels */
1611 FT_Pos org_dist
= dist
;
1614 dist
= af_latin2_snap_width( axis
->widths
, axis
->width_count
, dist
);
1618 /* in the case of vertical hinting, always round */
1619 /* the stem heights to integer pixels */
1622 dist
= ( dist
+ 16 ) & ~63;
1628 if ( AF_LATIN_HINTS_DO_MONO( hints
) )
1630 /* monochrome horizontal hinting: snap widths to integer pixels */
1631 /* with a different threshold */
1636 dist
= ( dist
+ 32 ) & ~63;
1640 /* for horizontal anti-aliased hinting, we adopt a more subtle */
1641 /* approach: we strengthen small stems, round stems whose size */
1642 /* is between 1 and 2 pixels to an integer, otherwise nothing */
1645 dist
= ( dist
+ 64 ) >> 1;
1647 else if ( dist
< 128 )
1649 /* We only round to an integer width if the corresponding */
1650 /* distortion is less than 1/4 pixel. Otherwise this */
1651 /* makes everything worse since the diagonals, which are */
1652 /* not hinted, appear a lot bolder or thinner than the */
1653 /* vertical stems. */
1658 dist
= ( dist
+ 22 ) & ~63;
1659 delta
= dist
- org_dist
;
1667 dist
= ( dist
+ 64 ) >> 1;
1671 /* round otherwise to prevent color fringes in LCD mode */
1672 dist
= ( dist
+ 32 ) & ~63;
1685 /* align one stem edge relative to the previous stem edge */
1688 af_latin2_align_linked_edge( AF_GlyphHints hints
,
1693 FT_Pos dist
= stem_edge
->opos
- base_edge
->opos
;
1695 FT_Pos fitted_width
= af_latin2_compute_stem_width(
1697 (AF_Edge_Flags
)base_edge
->flags
,
1698 (AF_Edge_Flags
)stem_edge
->flags
);
1701 stem_edge
->pos
= base_edge
->pos
+ fitted_width
;
1703 AF_LOG(( "LINK: edge %d (opos=%.2f) linked to (%.2f), "
1704 "dist was %.2f, now %.2f\n",
1705 stem_edge
-hints
->axis
[dim
].edges
, stem_edge
->opos
/ 64.0,
1706 stem_edge
->pos
/ 64.0, dist
/ 64.0, fitted_width
/ 64.0 ));
1711 af_latin2_align_serif_edge( AF_GlyphHints hints
,
1717 serif
->pos
= base
->pos
+ (serif
->opos
- base
->opos
);
1721 /*************************************************************************/
1722 /*************************************************************************/
1723 /*************************************************************************/
1725 /**** E D G E H I N T I N G ****/
1727 /*************************************************************************/
1728 /*************************************************************************/
1729 /*************************************************************************/
1732 FT_LOCAL_DEF( void )
1733 af_latin2_hint_edges( AF_GlyphHints hints
,
1736 AF_AxisHints axis
= &hints
->axis
[dim
];
1737 AF_Edge edges
= axis
->edges
;
1738 AF_Edge edge_limit
= edges
+ axis
->num_edges
;
1742 FT_Int has_serifs
= 0;
1743 FT_Pos anchor_drift
= 0;
1747 AF_LOG(( "==== hinting %s edges =====\n", dim
== AF_DIMENSION_HORZ
? "vertical" : "horizontal" ));
1749 /* we begin by aligning all stems relative to the blue zone */
1750 /* if needed -- that's only for horizontal edges */
1752 if ( dim
== AF_DIMENSION_VERT
&& AF_HINTS_DO_BLUES( hints
) )
1754 for ( edge
= edges
; edge
< edge_limit
; edge
++ )
1757 AF_Edge edge1
, edge2
;
1760 if ( edge
->flags
& AF_EDGE_DONE
)
1763 blue
= edge
->blue_edge
;
1771 else if ( edge2
&& edge2
->blue_edge
)
1773 blue
= edge2
->blue_edge
;
1781 AF_LOG(( "BLUE: edge %d (opos=%.2f) snapped to (%.2f), "
1783 edge1
-edges
, edge1
->opos
/ 64.0, blue
->fit
/ 64.0,
1784 edge1
->pos
/ 64.0 ));
1786 edge1
->pos
= blue
->fit
;
1787 edge1
->flags
|= AF_EDGE_DONE
;
1789 if ( edge2
&& !edge2
->blue_edge
)
1791 af_latin2_align_linked_edge( hints
, dim
, edge1
, edge2
);
1792 edge2
->flags
|= AF_EDGE_DONE
;
1799 anchor_drift
= (anchor
->pos
- anchor
->opos
);
1801 anchor_drift
= (anchor_drift
+ (edge2
->pos
- edge2
->opos
)) >> 1;
1806 /* now we will align all stem edges, trying to maintain the */
1807 /* relative order of stems in the glyph */
1808 for ( edge
= edges
; edge
< edge_limit
; edge
++ )
1813 if ( edge
->flags
& AF_EDGE_DONE
)
1816 /* skip all non-stem edges */
1824 /* now align the stem */
1826 /* this should not happen, but it's better to be safe */
1827 if ( edge2
->blue_edge
)
1829 AF_LOG(( "ASSERTION FAILED for edge %d\n", edge2
-edges
));
1831 af_latin2_align_linked_edge( hints
, dim
, edge2
, edge
);
1832 edge
->flags
|= AF_EDGE_DONE
;
1838 FT_Pos org_len
, org_center
, cur_len
;
1839 FT_Pos cur_pos1
, error1
, error2
, u_off
, d_off
;
1842 org_len
= edge2
->opos
- edge
->opos
;
1843 cur_len
= af_latin2_compute_stem_width(
1844 hints
, dim
, org_len
,
1845 (AF_Edge_Flags
)edge
->flags
,
1846 (AF_Edge_Flags
)edge2
->flags
);
1847 if ( cur_len
<= 64 )
1857 org_center
= edge
->opos
+ ( org_len
>> 1 );
1859 cur_pos1
= FT_PIX_ROUND( org_center
);
1861 error1
= org_center
- ( cur_pos1
- u_off
);
1865 error2
= org_center
- ( cur_pos1
+ d_off
);
1869 if ( error1
< error2
)
1874 edge
->pos
= cur_pos1
- cur_len
/ 2;
1875 edge2
->pos
= edge
->pos
+ cur_len
;
1878 edge
->pos
= FT_PIX_ROUND( edge
->opos
);
1880 AF_LOG(( "ANCHOR: edge %d (opos=%.2f) and %d (opos=%.2f) "
1881 "snapped to (%.2f) (%.2f)\n",
1882 edge
-edges
, edge
->opos
/ 64.0,
1883 edge2
-edges
, edge2
->opos
/ 64.0,
1884 edge
->pos
/ 64.0, edge2
->pos
/ 64.0 ));
1887 edge
->flags
|= AF_EDGE_DONE
;
1889 af_latin2_align_linked_edge( hints
, dim
, edge
, edge2
);
1891 edge2
->flags
|= AF_EDGE_DONE
;
1893 anchor_drift
= ( (anchor
->pos
- anchor
->opos
) +
1894 (edge2
->pos
- edge2
->opos
)) >> 1;
1896 AF_LOG(( "DRIFT: %.2f\n", anchor_drift
/64.0 ));
1900 FT_Pos org_pos
, org_len
, org_center
, cur_center
, cur_len
;
1901 FT_Pos org_left
, org_right
;
1904 org_pos
= edge
->opos
+ anchor_drift
;
1905 org_len
= edge2
->opos
- edge
->opos
;
1906 org_center
= org_pos
+ ( org_len
>> 1 );
1908 cur_len
= af_latin2_compute_stem_width(
1909 hints
, dim
, org_len
,
1910 (AF_Edge_Flags
)edge
->flags
,
1911 (AF_Edge_Flags
)edge2
->flags
);
1913 org_left
= org_pos
+ ((org_len
- cur_len
) >> 1);
1914 org_right
= org_pos
+ ((org_len
+ cur_len
) >> 1);
1916 AF_LOG(( "ALIGN: left=%.2f right=%.2f ", org_left
/64.0, org_right
/64.0 ));
1917 cur_center
= org_center
;
1919 if ( edge2
->flags
& AF_EDGE_DONE
)
1922 edge
->pos
= edge2
->pos
- cur_len
;
1926 /* we want to compare several displacement, and choose
1927 * the one that increases fitness while minimizing
1928 * distortion as well
1930 FT_Pos displacements
[6], scores
[6], org
, fit
, delta
;
1933 /* note: don't even try to fit tiny stems */
1936 AF_LOG(( "tiny stem\n" ));
1940 /* if the span is within a single pixel, don't touch it */
1941 if ( FT_PIX_FLOOR(org_left
) == FT_PIX_CEIL(org_right
) )
1943 AF_LOG(( "single pixel stem\n" ));
1949 /* we want to avoid the absolute worst case which is
1950 * when the left and right edges of the span each represent
1951 * about 50% of the gray. we'd better want to change this
1952 * to 25/75%, since this is much more pleasant to the eye with
1953 * very acceptable distortion
1955 FT_Pos frac_left
= (org_left
) & 63;
1956 FT_Pos frac_right
= (org_right
) & 63;
1958 if ( frac_left
>= 22 && frac_left
<= 42 &&
1959 frac_right
>= 22 && frac_right
<= 42 )
1962 fit
= (org
<= 32) ? 16 : 48;
1963 delta
= FT_ABS(fit
- org
);
1964 displacements
[count
] = fit
- org
;
1965 scores
[count
++] = delta
;
1966 AF_LOG(( "dispA=%.2f (%d) ", (fit
- org
)/64.0, delta
));
1969 fit
= (org
<= 32) ? 16 : 48;
1970 delta
= FT_ABS(fit
- org
);
1971 displacements
[count
] = fit
- org
;
1972 scores
[count
++] = delta
;
1973 AF_LOG(( "dispB=%.2f (%d) ", (fit
- org
)/64.0, delta
));
1977 /* snapping the left edge to the grid */
1979 fit
= FT_PIX_ROUND(org
);
1980 delta
= FT_ABS(fit
- org
);
1981 displacements
[count
] = fit
- org
;
1982 scores
[count
++] = delta
;
1983 AF_LOG(( "dispC=%.2f (%d) ", (fit
- org
)/64.0, delta
));
1985 /* snapping the right edge to the grid */
1987 fit
= FT_PIX_ROUND(org
);
1988 delta
= FT_ABS(fit
- org
);
1989 displacements
[count
] = fit
- org
;
1990 scores
[count
++] = delta
;
1991 AF_LOG(( "dispD=%.2f (%d) ", (fit
- org
)/64.0, delta
));
1993 /* now find the best displacement */
1995 FT_Pos best_score
= scores
[0];
1996 FT_Pos best_disp
= displacements
[0];
1999 for (nn
= 1; nn
< count
; nn
++)
2001 if (scores
[nn
] < best_score
)
2003 best_score
= scores
[nn
];
2004 best_disp
= displacements
[nn
];
2008 cur_center
= org_center
+ best_disp
;
2014 edge
->pos
= cur_center
- (cur_len
>> 1);
2015 edge2
->pos
= edge
->pos
+ cur_len
;
2017 AF_LOG(( "STEM1: %d (opos=%.2f) to %d (opos=%.2f) "
2018 "snapped to (%.2f) and (%.2f), org_len = %.2f cur_len=%.2f\n",
2019 edge
-edges
, edge
->opos
/ 64.0,
2020 edge2
-edges
, edge2
->opos
/ 64.0,
2021 edge
->pos
/ 64.0, edge2
->pos
/ 64.0,
2022 org_len
/ 64.0, cur_len
/ 64.0 ));
2024 edge
->flags
|= AF_EDGE_DONE
;
2025 edge2
->flags
|= AF_EDGE_DONE
;
2027 if ( edge
> edges
&& edge
->pos
< edge
[-1].pos
)
2029 AF_LOG(( "BOUND: %d (pos=%.2f) to (%.2f)\n",
2030 edge
-edges
, edge
->pos
/ 64.0, edge
[-1].pos
/ 64.0 ));
2031 edge
->pos
= edge
[-1].pos
;
2036 /* make sure that lowercase m's maintain their symmetry */
2038 /* In general, lowercase m's have six vertical edges if they are sans */
2039 /* serif, or twelve if they are with serifs. This implementation is */
2040 /* based on that assumption, and seems to work very well with most */
2041 /* faces. However, if for a certain face this assumption is not */
2042 /* true, the m is just rendered like before. In addition, any stem */
2043 /* correction will only be applied to symmetrical glyphs (even if the */
2044 /* glyph is not an m), so the potential for unwanted distortion is */
2045 /* relatively low. */
2047 /* We don't handle horizontal edges since we can't easily assure that */
2048 /* the third (lowest) stem aligns with the base line; it might end up */
2049 /* one pixel higher or lower. */
2051 n_edges
= edge_limit
- edges
;
2052 if ( dim
== AF_DIMENSION_HORZ
&& ( n_edges
== 6 || n_edges
== 12 ) )
2054 AF_Edge edge1
, edge2
, edge3
;
2055 FT_Pos dist1
, dist2
, span
, delta
;
2071 dist1
= edge2
->opos
- edge1
->opos
;
2072 dist2
= edge3
->opos
- edge2
->opos
;
2074 span
= dist1
- dist2
;
2080 delta
= edge3
->pos
- ( 2 * edge2
->pos
- edge1
->pos
);
2081 edge3
->pos
-= delta
;
2083 edge3
->link
->pos
-= delta
;
2085 /* move the serifs along with the stem */
2086 if ( n_edges
== 12 )
2088 ( edges
+ 8 )->pos
-= delta
;
2089 ( edges
+ 11 )->pos
-= delta
;
2092 edge3
->flags
|= AF_EDGE_DONE
;
2094 edge3
->link
->flags
|= AF_EDGE_DONE
;
2098 if ( has_serifs
|| !anchor
)
2101 * now hint the remaining edges (serifs and single) in order
2102 * to complete our processing
2104 for ( edge
= edges
; edge
< edge_limit
; edge
++ )
2109 if ( edge
->flags
& AF_EDGE_DONE
)
2116 delta
= edge
->serif
->opos
- edge
->opos
;
2121 if ( delta
< 64 + 16 )
2123 af_latin2_align_serif_edge( hints
, edge
->serif
, edge
);
2124 AF_LOG(( "SERIF: edge %d (opos=%.2f) serif to %d (opos=%.2f) "
2125 "aligned to (%.2f)\n",
2126 edge
-edges
, edge
->opos
/ 64.0,
2127 edge
->serif
- edges
, edge
->serif
->opos
/ 64.0,
2128 edge
->pos
/ 64.0 ));
2132 AF_LOG(( "SERIF_ANCHOR: edge %d (opos=%.2f) snapped to (%.2f)\n",
2133 edge
-edges
, edge
->opos
/ 64.0, edge
->pos
/ 64.0 ));
2134 edge
->pos
= FT_PIX_ROUND( edge
->opos
);
2139 AF_Edge before
, after
;
2142 for ( before
= edge
- 1; before
>= edges
; before
-- )
2143 if ( before
->flags
& AF_EDGE_DONE
)
2146 for ( after
= edge
+ 1; after
< edge_limit
; after
++ )
2147 if ( after
->flags
& AF_EDGE_DONE
)
2150 if ( before
>= edges
&& before
< edge
&&
2151 after
< edge_limit
&& after
> edge
)
2153 edge
->pos
= before
->pos
+
2154 FT_MulDiv( edge
->opos
- before
->opos
,
2155 after
->pos
- before
->pos
,
2156 after
->opos
- before
->opos
);
2157 AF_LOG(( "SERIF_LINK1: edge %d (opos=%.2f) snapped to (%.2f) from %d (opos=%.2f)\n",
2158 edge
-edges
, edge
->opos
/ 64.0, edge
->pos
/ 64.0, before
- edges
, before
->opos
/ 64.0 ));
2162 edge
->pos
= anchor
->pos
+ (( edge
->opos
- anchor
->opos
+ 16) & ~31);
2164 AF_LOG(( "SERIF_LINK2: edge %d (opos=%.2f) snapped to (%.2f)\n",
2165 edge
-edges
, edge
->opos
/ 64.0, edge
->pos
/ 64.0 ));
2169 edge
->flags
|= AF_EDGE_DONE
;
2171 if ( edge
> edges
&& edge
->pos
< edge
[-1].pos
)
2172 edge
->pos
= edge
[-1].pos
;
2174 if ( edge
+ 1 < edge_limit
&&
2175 edge
[1].flags
& AF_EDGE_DONE
&&
2176 edge
->pos
> edge
[1].pos
)
2177 edge
->pos
= edge
[1].pos
;
2184 af_latin2_hints_apply( AF_GlyphHints hints
,
2185 FT_Outline
* outline
,
2186 AF_LatinMetrics metrics
)
2192 error
= af_glyph_hints_reload( hints
, outline
, 1 );
2196 /* analyze glyph outline */
2197 #ifdef AF_USE_WARPER
2198 if ( metrics
->root
.scaler
.render_mode
== FT_RENDER_MODE_LIGHT
||
2199 AF_HINTS_DO_HORIZONTAL( hints
) )
2201 if ( AF_HINTS_DO_HORIZONTAL( hints
) )
2204 error
= af_latin2_hints_detect_features( hints
, AF_DIMENSION_HORZ
);
2209 if ( AF_HINTS_DO_VERTICAL( hints
) )
2211 error
= af_latin2_hints_detect_features( hints
, AF_DIMENSION_VERT
);
2215 af_latin2_hints_compute_blue_edges( hints
, metrics
);
2218 /* grid-fit the outline */
2219 for ( dim
= 0; dim
< AF_DIMENSION_MAX
; dim
++ )
2221 #ifdef AF_USE_WARPER
2222 if ( ( dim
== AF_DIMENSION_HORZ
&&
2223 metrics
->root
.scaler
.render_mode
== FT_RENDER_MODE_LIGHT
) )
2225 AF_WarperRec warper
;
2230 af_warper_compute( &warper
, hints
, dim
, &scale
, &delta
);
2231 af_glyph_hints_scale_dim( hints
, dim
, scale
, delta
);
2236 if ( ( dim
== AF_DIMENSION_HORZ
&& AF_HINTS_DO_HORIZONTAL( hints
) ) ||
2237 ( dim
== AF_DIMENSION_VERT
&& AF_HINTS_DO_VERTICAL( hints
) ) )
2239 af_latin2_hint_edges( hints
, (AF_Dimension
)dim
);
2240 af_glyph_hints_align_edge_points( hints
, (AF_Dimension
)dim
);
2241 af_glyph_hints_align_strong_points( hints
, (AF_Dimension
)dim
);
2242 af_glyph_hints_align_weak_points( hints
, (AF_Dimension
)dim
);
2245 af_glyph_hints_save( hints
, outline
);
2252 /*************************************************************************/
2253 /*************************************************************************/
2255 /***** L A T I N S C R I P T C L A S S *****/
2257 /*************************************************************************/
2258 /*************************************************************************/
2261 static const AF_Script_UniRangeRec af_latin2_uniranges
[] =
2263 { 32, 127 }, /* XXX: TODO: Add new Unicode ranges here! */
2269 FT_CALLBACK_TABLE_DEF
const AF_ScriptClassRec
2270 af_latin2_script_class
=
2273 af_latin2_uniranges
,
2275 sizeof( AF_LatinMetricsRec
),
2277 (AF_Script_InitMetricsFunc
) af_latin2_metrics_init
,
2278 (AF_Script_ScaleMetricsFunc
)af_latin2_metrics_scale
,
2279 (AF_Script_DoneMetricsFunc
) NULL
,
2281 (AF_Script_InitHintsFunc
) af_latin2_hints_init
,
2282 (AF_Script_ApplyHintsFunc
) af_latin2_hints_apply