1 /***************************************************************************/
5 /* Auto-fitter hinting routines for latin writing system (body). */
7 /* Copyright 2003-2016 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 /***************************************************************************/
19 #include FT_ADVANCES_H
27 #ifdef AF_CONFIG_OPTION_USE_WARPER
32 /*************************************************************************/
34 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
35 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
36 /* messages during execution. */
39 #define FT_COMPONENT trace_aflatin2
42 FT_LOCAL_DEF( FT_Error
)
43 af_latin2_hints_compute_segments( AF_GlyphHints hints
,
47 af_latin2_hints_link_segments( AF_GlyphHints hints
,
50 /*************************************************************************/
51 /*************************************************************************/
53 /***** L A T I N G L O B A L M E T R I C S *****/
55 /*************************************************************************/
56 /*************************************************************************/
59 af_latin2_metrics_init_widths( AF_LatinMetrics metrics
,
62 /* scan the array of segments in each direction */
63 AF_GlyphHintsRec hints
[1];
66 af_glyph_hints_init( hints
, face
->memory
);
68 metrics
->axis
[AF_DIMENSION_HORZ
].width_count
= 0;
69 metrics
->axis
[AF_DIMENSION_VERT
].width_count
= 0;
75 AF_LatinMetricsRec dummy
[1];
76 AF_Scaler scaler
= &dummy
->root
.scaler
;
79 glyph_index
= FT_Get_Char_Index(
81 metrics
->root
.style_class
->standard_char
);
82 if ( glyph_index
== 0 )
85 error
= FT_Load_Glyph( face
, glyph_index
, FT_LOAD_NO_SCALE
);
86 if ( error
|| face
->glyph
->outline
.n_points
<= 0 )
91 dummy
->units_per_em
= metrics
->units_per_em
;
92 scaler
->x_scale
= scaler
->y_scale
= 0x10000L
;
93 scaler
->x_delta
= scaler
->y_delta
= 0;
95 scaler
->render_mode
= FT_RENDER_MODE_NORMAL
;
98 af_glyph_hints_rescale( hints
, (AF_StyleMetrics
)dummy
);
100 error
= af_glyph_hints_reload( hints
, &face
->glyph
->outline
);
104 for ( dim
= 0; dim
< AF_DIMENSION_MAX
; dim
++ )
106 AF_LatinAxis axis
= &metrics
->axis
[dim
];
107 AF_AxisHints axhints
= &hints
->axis
[dim
];
108 AF_Segment seg
, limit
, link
;
109 FT_UInt num_widths
= 0;
112 error
= af_latin2_hints_compute_segments( hints
,
117 af_latin2_hints_link_segments( hints
,
120 seg
= axhints
->segments
;
121 limit
= seg
+ axhints
->num_segments
;
123 for ( ; seg
< limit
; seg
++ )
127 /* we only consider stem segments there! */
128 if ( link
&& link
->link
== seg
&& link
> seg
)
133 dist
= seg
->pos
- link
->pos
;
137 if ( num_widths
< AF_LATIN_MAX_WIDTHS
)
138 axis
->widths
[num_widths
++].org
= dist
;
142 af_sort_widths( num_widths
, axis
->widths
);
143 axis
->width_count
= num_widths
;
147 for ( dim
= 0; dim
< AF_DIMENSION_MAX
; dim
++ )
149 AF_LatinAxis axis
= &metrics
->axis
[dim
];
153 stdw
= ( axis
->width_count
> 0 )
154 ? axis
->widths
[0].org
155 : AF_LATIN_CONSTANT( metrics
, 50 );
157 /* let's try 20% of the smallest width */
158 axis
->edge_distance_threshold
= stdw
/ 5;
159 axis
->standard_width
= stdw
;
160 axis
->extra_light
= 0;
164 af_glyph_hints_done( hints
);
169 #define AF_LATIN_MAX_TEST_CHARACTERS 12
172 static const char af_latin2_blue_chars
[AF_LATIN_MAX_BLUES
]
173 [AF_LATIN_MAX_TEST_CHARACTERS
+1] =
185 af_latin2_metrics_init_blues( AF_LatinMetrics metrics
,
188 FT_Pos flats
[AF_LATIN_MAX_TEST_CHARACTERS
];
189 FT_Pos rounds
[AF_LATIN_MAX_TEST_CHARACTERS
];
195 AF_LatinAxis axis
= &metrics
->axis
[AF_DIMENSION_VERT
];
196 FT_GlyphSlot glyph
= face
->glyph
;
199 /* we compute the blues simply by loading each character from the */
200 /* 'af_latin2_blue_chars[blues]' string, then compute its top-most or */
201 /* bottom-most points (depending on `AF_IS_TOP_BLUE') */
203 FT_TRACE5(( "blue zones computation\n"
204 "======================\n\n" ));
206 for ( bb
= 0; bb
< AF_LATIN_BLUE_MAX
; bb
++ )
208 const char* p
= af_latin2_blue_chars
[bb
];
209 const char* limit
= p
+ AF_LATIN_MAX_TEST_CHARACTERS
;
214 FT_TRACE5(( "blue zone %d:\n", bb
));
219 for ( ; p
< limit
&& *p
; p
++ )
222 FT_Int best_point
, best_y
, best_first
, best_last
;
227 /* load the character in the face -- skip unknown or empty ones */
228 glyph_index
= FT_Get_Char_Index( face
, (FT_UInt
)*p
);
229 if ( glyph_index
== 0 )
232 error
= FT_Load_Glyph( face
, glyph_index
, FT_LOAD_NO_SCALE
);
233 if ( error
|| glyph
->outline
.n_points
<= 0 )
236 /* now compute min or max point indices and coordinates */
237 points
= glyph
->outline
.points
;
239 best_y
= 0; /* make compiler happy */
240 best_first
= 0; /* ditto */
241 best_last
= 0; /* ditto */
249 for ( nn
= 0; nn
< glyph
->outline
.n_contours
; first
= last
+1, nn
++ )
251 FT_Int old_best_point
= best_point
;
255 last
= glyph
->outline
.contours
[nn
];
257 /* Avoid single-point contours since they are never rasterized. */
258 /* In some fonts, they correspond to mark attachment points */
259 /* which are way outside of the glyph's real outline. */
263 if ( AF_LATIN_IS_TOP_BLUE( bb
) )
265 for ( pp
= first
; pp
<= last
; pp
++ )
266 if ( best_point
< 0 || points
[pp
].y
> best_y
)
269 best_y
= points
[pp
].y
;
274 for ( pp
= first
; pp
<= last
; pp
++ )
275 if ( best_point
< 0 || points
[pp
].y
< best_y
)
278 best_y
= points
[pp
].y
;
282 if ( best_point
!= old_best_point
)
288 FT_TRACE5(( " %c %d", *p
, best_y
));
291 /* now check whether the point belongs to a straight or round */
292 /* segment; we first need to find in which contour the extremum */
293 /* lies, then inspect its previous and next points */
295 FT_Pos best_x
= points
[best_point
].x
;
296 FT_Int start
, end
, prev
, next
;
300 /* now look for the previous and next points that are not on the */
301 /* same Y coordinate. Threshold the `closeness'... */
302 start
= end
= best_point
;
307 if ( prev
< best_first
)
310 dist
= FT_ABS( points
[prev
].y
- best_y
);
311 /* accept a small distance or a small angle (both values are */
312 /* heuristic; value 20 corresponds to approx. 2.9 degrees) */
314 if ( FT_ABS( points
[prev
].x
- best_x
) <= 20 * dist
)
319 } while ( start
!= best_point
);
324 if ( next
> best_last
)
327 dist
= FT_ABS( points
[next
].y
- best_y
);
329 if ( FT_ABS( points
[next
].x
- best_x
) <= 20 * dist
)
334 } while ( end
!= best_point
);
336 /* now, set the `round' flag depending on the segment's kind */
338 FT_CURVE_TAG( glyph
->outline
.tags
[start
] ) != FT_CURVE_TAG_ON
||
339 FT_CURVE_TAG( glyph
->outline
.tags
[ end
] ) != FT_CURVE_TAG_ON
);
341 FT_TRACE5(( " (%s)\n", round
? "round" : "flat" ));
345 rounds
[num_rounds
++] = best_y
;
347 flats
[num_flats
++] = best_y
;
350 if ( num_flats
== 0 && num_rounds
== 0 )
353 * we couldn't find a single glyph to compute this blue zone,
354 * we will simply ignore it then
356 FT_TRACE5(( " empty\n" ));
360 /* we have computed the contents of the `rounds' and `flats' tables, */
361 /* now determine the reference and overshoot position of the blue -- */
362 /* we simply take the median value after a simple sort */
363 af_sort_pos( num_rounds
, rounds
);
364 af_sort_pos( num_flats
, flats
);
366 blue
= & axis
->blues
[axis
->blue_count
];
367 blue_ref
= & blue
->ref
.org
;
368 blue_shoot
= & blue
->shoot
.org
;
372 if ( num_flats
== 0 )
375 *blue_shoot
= rounds
[num_rounds
/ 2];
377 else if ( num_rounds
== 0 )
380 *blue_shoot
= flats
[num_flats
/ 2];
384 *blue_ref
= flats
[num_flats
/ 2];
385 *blue_shoot
= rounds
[num_rounds
/ 2];
388 /* there are sometimes problems: if the overshoot position of top */
389 /* zones is under its reference position, or the opposite for bottom */
390 /* zones. We must thus check everything there and correct the errors */
391 if ( *blue_shoot
!= *blue_ref
)
393 FT_Pos ref
= *blue_ref
;
394 FT_Pos shoot
= *blue_shoot
;
395 FT_Bool over_ref
= FT_BOOL( shoot
> ref
);
398 if ( AF_LATIN_IS_TOP_BLUE( bb
) ^ over_ref
)
401 *blue_shoot
= ( shoot
+ ref
) / 2;
403 FT_TRACE5(( " [overshoot smaller than reference,"
404 " taking mean value]\n" ));
409 if ( AF_LATIN_IS_TOP_BLUE( bb
) )
410 blue
->flags
|= AF_LATIN_BLUE_TOP
;
413 * The following flag is used later to adjust the y and x scales
414 * in order to optimize the pixel grid alignment of the top of small
417 if ( AF_LATIN_IS_X_HEIGHT_BLUE( bb
) )
418 blue
->flags
|= AF_LATIN_BLUE_ADJUSTMENT
;
420 FT_TRACE5(( " -> reference = %ld\n"
421 " overshoot = %ld\n",
422 *blue_ref
, *blue_shoot
));
430 af_latin2_metrics_check_digits( AF_LatinMetrics metrics
,
434 FT_Bool started
= 0, same_width
= 1;
435 FT_Fixed advance
, old_advance
= 0;
438 /* check whether all ASCII digits have the same advance width; */
439 /* digit `0' is 0x30 in all supported charmaps */
440 for ( i
= 0x30; i
<= 0x39; i
++ )
445 glyph_index
= FT_Get_Char_Index( face
, i
);
446 if ( glyph_index
== 0 )
449 if ( FT_Get_Advance( face
, glyph_index
,
452 FT_LOAD_IGNORE_TRANSFORM
,
458 if ( advance
!= old_advance
)
466 old_advance
= advance
;
471 metrics
->root
.digits_have_same_width
= same_width
;
475 FT_LOCAL_DEF( FT_Error
)
476 af_latin2_metrics_init( AF_LatinMetrics metrics
,
479 FT_Error error
= FT_Err_Ok
;
480 FT_CharMap oldmap
= face
->charmap
;
483 static const FT_Encoding latin_encodings
[] =
486 FT_ENCODING_APPLE_ROMAN
,
487 FT_ENCODING_ADOBE_STANDARD
,
488 FT_ENCODING_ADOBE_LATIN_1
,
489 FT_ENCODING_NONE
/* end of list */
493 metrics
->units_per_em
= face
->units_per_EM
;
495 /* do we have a latin charmap in there? */
496 for ( ee
= 0; latin_encodings
[ee
] != FT_ENCODING_NONE
; ee
++ )
498 error
= FT_Select_Charmap( face
, latin_encodings
[ee
] );
505 af_latin2_metrics_init_widths( metrics
, face
);
506 af_latin2_metrics_init_blues( metrics
, face
);
507 af_latin2_metrics_check_digits( metrics
, face
);
510 FT_Set_Charmap( face
, oldmap
);
516 af_latin2_metrics_scale_dim( AF_LatinMetrics metrics
,
526 if ( dim
== AF_DIMENSION_HORZ
)
528 scale
= scaler
->x_scale
;
529 delta
= scaler
->x_delta
;
533 scale
= scaler
->y_scale
;
534 delta
= scaler
->y_delta
;
537 axis
= &metrics
->axis
[dim
];
539 if ( axis
->org_scale
== scale
&& axis
->org_delta
== delta
)
542 axis
->org_scale
= scale
;
543 axis
->org_delta
= delta
;
546 * correct Y scale to optimize the alignment of the top of small
547 * letters to the pixel grid
549 if ( dim
== AF_DIMENSION_VERT
)
551 AF_LatinAxis vaxis
= &metrics
->axis
[AF_DIMENSION_VERT
];
552 AF_LatinBlue blue
= NULL
;
555 for ( nn
= 0; nn
< vaxis
->blue_count
; nn
++ )
557 if ( vaxis
->blues
[nn
].flags
& AF_LATIN_BLUE_ADJUSTMENT
)
559 blue
= &vaxis
->blues
[nn
];
573 scaled
= FT_MulFix( blue
->shoot
.org
, scaler
->y_scale
);
574 ppem
= metrics
->root
.scaler
.face
->size
->metrics
.x_ppem
;
575 limit
= metrics
->root
.globals
->increase_x_height
;
578 /* if the `increase-x-height' property is active, */
579 /* we round up much more often */
582 ppem
>= AF_PROP_INCREASE_X_HEIGHT_MIN
)
585 fitted
= ( scaled
+ threshold
) & ~63;
588 if ( scaled
!= fitted
)
590 scale
= FT_MulDiv( scale
, fitted
, scaled
);
591 FT_TRACE5(( "== scaled x-top = %.2g"
592 " fitted = %.2g, scaling = %.4g\n",
593 scaled
/ 64.0, fitted
/ 64.0,
594 ( fitted
* 1.0 ) / scaled
));
603 if ( dim
== AF_DIMENSION_HORZ
)
605 metrics
->root
.scaler
.x_scale
= scale
;
606 metrics
->root
.scaler
.x_delta
= delta
;
610 metrics
->root
.scaler
.y_scale
= scale
;
611 metrics
->root
.scaler
.y_delta
= delta
;
614 /* scale the standard widths */
615 for ( nn
= 0; nn
< axis
->width_count
; nn
++ )
617 AF_Width width
= axis
->widths
+ nn
;
620 width
->cur
= FT_MulFix( width
->org
, scale
);
621 width
->fit
= width
->cur
;
624 /* an extra-light axis corresponds to a standard width that is */
625 /* smaller than 5/8 pixels */
627 (FT_Bool
)( FT_MulFix( axis
->standard_width
, scale
) < 32 + 8 );
629 if ( dim
== AF_DIMENSION_VERT
)
631 /* scale the blue zones */
632 for ( nn
= 0; nn
< axis
->blue_count
; nn
++ )
634 AF_LatinBlue blue
= &axis
->blues
[nn
];
638 blue
->ref
.cur
= FT_MulFix( blue
->ref
.org
, scale
) + delta
;
639 blue
->ref
.fit
= blue
->ref
.cur
;
640 blue
->shoot
.cur
= FT_MulFix( blue
->shoot
.org
, scale
) + delta
;
641 blue
->shoot
.fit
= blue
->shoot
.cur
;
642 blue
->flags
&= ~AF_LATIN_BLUE_ACTIVE
;
644 /* a blue zone is only active if it is less than 3/4 pixels tall */
645 dist
= FT_MulFix( blue
->ref
.org
- blue
->shoot
.org
, scale
);
646 if ( dist
<= 48 && dist
>= -48 )
648 FT_Pos delta1
, delta2
;
650 delta1
= blue
->shoot
.org
- blue
->ref
.org
;
655 delta2
= FT_MulFix( delta2
, scale
);
659 else if ( delta2
< 64 )
660 delta2
= 32 + ( ( ( delta2
- 32 ) + 16 ) & ~31 );
662 delta2
= FT_PIX_ROUND( delta2
);
667 blue
->ref
.fit
= FT_PIX_ROUND( blue
->ref
.cur
);
668 blue
->shoot
.fit
= blue
->ref
.fit
+ delta2
;
670 FT_TRACE5(( ">> activating blue zone %d:"
671 " ref.cur=%.2g ref.fit=%.2g"
672 " shoot.cur=%.2g shoot.fit=%.2g\n",
673 nn
, blue
->ref
.cur
/ 64.0, blue
->ref
.fit
/ 64.0,
674 blue
->shoot
.cur
/ 64.0, blue
->shoot
.fit
/ 64.0 ));
676 blue
->flags
|= AF_LATIN_BLUE_ACTIVE
;
684 af_latin2_metrics_scale( AF_LatinMetrics metrics
,
687 metrics
->root
.scaler
.render_mode
= scaler
->render_mode
;
688 metrics
->root
.scaler
.face
= scaler
->face
;
689 metrics
->root
.scaler
.flags
= scaler
->flags
;
691 af_latin2_metrics_scale_dim( metrics
, scaler
, AF_DIMENSION_HORZ
);
692 af_latin2_metrics_scale_dim( metrics
, scaler
, AF_DIMENSION_VERT
);
696 /* Extract standard_width from writing system/script specific */
700 af_latin2_get_standard_widths( AF_LatinMetrics metrics
,
705 *stdHW
= metrics
->axis
[AF_DIMENSION_VERT
].standard_width
;
708 *stdVW
= metrics
->axis
[AF_DIMENSION_HORZ
].standard_width
;
712 /*************************************************************************/
713 /*************************************************************************/
715 /***** L A T I N G L Y P H A N A L Y S I S *****/
717 /*************************************************************************/
718 /*************************************************************************/
720 #define SORT_SEGMENTS
722 FT_LOCAL_DEF( FT_Error
)
723 af_latin2_hints_compute_segments( AF_GlyphHints hints
,
726 AF_AxisHints axis
= &hints
->axis
[dim
];
727 FT_Memory memory
= hints
->memory
;
728 FT_Error error
= FT_Err_Ok
;
729 AF_Segment segment
= NULL
;
731 AF_Point
* contour
= hints
->contours
;
732 AF_Point
* contour_limit
= contour
+ hints
->num_contours
;
733 AF_Direction major_dir
, segment_dir
;
738 seg0
.flags
= AF_EDGE_NORMAL
;
740 major_dir
= (AF_Direction
)FT_ABS( axis
->major_dir
);
741 segment_dir
= major_dir
;
743 axis
->num_segments
= 0;
745 /* set up (u,v) in each point */
746 if ( dim
== AF_DIMENSION_HORZ
)
748 AF_Point point
= hints
->points
;
749 AF_Point limit
= point
+ hints
->num_points
;
752 for ( ; point
< limit
; point
++ )
754 point
->u
= point
->fx
;
755 point
->v
= point
->fy
;
760 AF_Point point
= hints
->points
;
761 AF_Point limit
= point
+ hints
->num_points
;
764 for ( ; point
< limit
; point
++ )
766 point
->u
= point
->fy
;
767 point
->v
= point
->fx
;
771 /* do each contour separately */
772 for ( ; contour
< contour_limit
; contour
++ )
774 AF_Point point
= contour
[0];
775 AF_Point start
= point
;
776 AF_Point last
= point
->prev
;
779 if ( point
== last
) /* skip singletons -- just in case */
782 /* already on an edge ?, backtrack to find its start */
783 if ( FT_ABS( point
->in_dir
) == major_dir
)
787 while ( point
->in_dir
== start
->in_dir
)
790 else /* otherwise, find first segment start, if any */
792 while ( FT_ABS( point
->out_dir
) != major_dir
)
796 if ( point
== start
)
806 FT_Pos min_u
, min_v
, max_u
, max_v
;
808 /* we're at the start of a new segment */
809 FT_ASSERT( FT_ABS( point
->out_dir
) == major_dir
&&
810 point
->in_dir
!= point
->out_dir
);
813 min_u
= max_u
= point
->u
;
814 min_v
= max_v
= point
->v
;
818 while ( point
->out_dir
== first
->out_dir
)
822 if ( point
->u
< min_u
)
825 if ( point
->u
> max_u
)
829 if ( point
->v
< min_v
)
832 if ( point
->v
> max_v
)
835 /* record new segment */
836 error
= af_axis_hints_new_segment( axis
, memory
, &segment
);
841 segment
->dir
= first
->out_dir
;
842 segment
->first
= first
;
843 segment
->last
= point
;
844 segment
->pos
= (FT_Short
)( ( min_u
+ max_u
) >> 1 );
845 segment
->min_coord
= (FT_Short
) min_v
;
846 segment
->max_coord
= (FT_Short
) max_v
;
847 segment
->height
= (FT_Short
)( max_v
- min_v
);
849 /* a segment is round if it doesn't have successive */
850 /* on-curve points. */
853 AF_Point last
= point
;
854 FT_UInt f0
= pt
->flags
& AF_FLAG_CONTROL
;
858 segment
->flags
&= ~AF_EDGE_ROUND
;
860 for ( ; pt
!= last
; f0
= f1
)
863 f1
= pt
->flags
& AF_FLAG_CONTROL
;
869 segment
->flags
|= AF_EDGE_ROUND
;
873 /* this can happen in the case of a degenerate contour
874 * e.g. a 2-point vertical contour
876 if ( point
== start
)
879 /* jump to the start of the next segment, if any */
880 while ( FT_ABS( point
->out_dir
) != major_dir
)
884 if ( point
== start
)
893 /* now slightly increase the height of segments when this makes */
894 /* sense -- this is used to better detect and ignore serifs */
896 AF_Segment segments
= axis
->segments
;
897 AF_Segment segments_end
= segments
+ axis
->num_segments
;
900 for ( segment
= segments
; segment
< segments_end
; segment
++ )
902 AF_Point first
= segment
->first
;
903 AF_Point last
= segment
->last
;
905 FT_Pos first_v
= first
->v
;
906 FT_Pos last_v
= last
->v
;
909 if ( first_v
< last_v
)
912 if ( p
->v
< first_v
)
913 segment
->height
= (FT_Short
)( segment
->height
+
914 ( ( first_v
- p
->v
) >> 1 ) );
918 segment
->height
= (FT_Short
)( segment
->height
+
919 ( ( p
->v
- last_v
) >> 1 ) );
924 if ( p
->v
> first_v
)
925 segment
->height
= (FT_Short
)( segment
->height
+
926 ( ( p
->v
- first_v
) >> 1 ) );
930 segment
->height
= (FT_Short
)( segment
->height
+
931 ( ( last_v
- p
->v
) >> 1 ) );
936 #ifdef AF_SORT_SEGMENTS
937 /* place all segments with a negative direction to the start
938 * of the array, used to speed up segment linking later...
941 AF_Segment segments
= axis
->segments
;
942 FT_UInt count
= axis
->num_segments
;
945 for ( ii
= 0; ii
< count
; ii
++ )
947 if ( segments
[ii
].dir
> 0 )
949 for ( jj
= ii
+ 1; jj
< count
; jj
++ )
951 if ( segments
[jj
].dir
< 0 )
957 segments
[ii
] = segments
[jj
];
968 axis
->mid_segments
= ii
;
978 af_latin2_hints_link_segments( AF_GlyphHints hints
,
981 AF_AxisHints axis
= &hints
->axis
[dim
];
982 AF_Segment segments
= axis
->segments
;
983 AF_Segment segment_limit
= segments
+ axis
->num_segments
;
984 #ifdef AF_SORT_SEGMENTS
985 AF_Segment segment_mid
= segments
+ axis
->mid_segments
;
987 FT_Pos len_threshold
, len_score
;
988 AF_Segment seg1
, seg2
;
991 len_threshold
= AF_LATIN_CONSTANT( hints
->metrics
, 8 );
992 if ( len_threshold
== 0 )
995 len_score
= AF_LATIN_CONSTANT( hints
->metrics
, 6000 );
997 #ifdef AF_SORT_SEGMENTS
998 for ( seg1
= segments
; seg1
< segment_mid
; seg1
++ )
1000 if ( seg1
->dir
!= axis
->major_dir
)
1003 for ( seg2
= segment_mid
; seg2
< segment_limit
; seg2
++ )
1005 /* now compare each segment to the others */
1006 for ( seg1
= segments
; seg1
< segment_limit
; seg1
++ )
1008 if ( seg1
->dir
!= axis
->major_dir
)
1011 for ( seg2
= segments
; seg2
< segment_limit
; seg2
++ )
1012 if ( seg1
->dir
+ seg2
->dir
== 0 && seg2
->pos
> seg1
->pos
)
1015 FT_Pos pos1
= seg1
->pos
;
1016 FT_Pos pos2
= seg2
->pos
;
1017 FT_Pos dist
= pos2
- pos1
;
1024 FT_Pos min
= seg1
->min_coord
;
1025 FT_Pos max
= seg1
->max_coord
;
1029 if ( min
< seg2
->min_coord
)
1030 min
= seg2
->min_coord
;
1032 if ( max
> seg2
->max_coord
)
1033 max
= seg2
->max_coord
;
1036 if ( len
>= len_threshold
)
1038 score
= dist
+ len_score
/ len
;
1039 if ( score
< seg1
->score
)
1041 seg1
->score
= score
;
1045 if ( score
< seg2
->score
)
1047 seg2
->score
= score
;
1058 /* now, compute the `serif' segments */
1059 for ( seg1
= segments
; seg1
< segment_limit
; seg1
++ )
1065 if ( seg2
->link
!= seg1
)
1068 seg1
->serif
= seg2
->link
;
1075 FT_LOCAL_DEF( FT_Error
)
1076 af_latin2_hints_compute_edges( AF_GlyphHints hints
,
1079 AF_AxisHints axis
= &hints
->axis
[dim
];
1080 FT_Error error
= FT_Err_Ok
;
1081 FT_Memory memory
= hints
->memory
;
1082 AF_LatinAxis laxis
= &((AF_LatinMetrics
)hints
->metrics
)->axis
[dim
];
1084 AF_Segment segments
= axis
->segments
;
1085 AF_Segment segment_limit
= segments
+ axis
->num_segments
;
1088 AF_Direction up_dir
;
1090 FT_Pos edge_distance_threshold
;
1091 FT_Pos segment_length_threshold
;
1094 axis
->num_edges
= 0;
1096 scale
= ( dim
== AF_DIMENSION_HORZ
) ? hints
->x_scale
1099 up_dir
= ( dim
== AF_DIMENSION_HORZ
) ? AF_DIR_UP
1103 * We want to ignore very small (mostly serif) segments, we do that
1104 * by ignoring those that whose length is less than a given fraction
1105 * of the standard width. If there is no standard width, we ignore
1106 * those that are less than a given size in pixels
1108 * also, unlink serif segments that are linked to segments farther
1109 * than 50% of the standard width
1111 if ( dim
== AF_DIMENSION_HORZ
)
1113 if ( laxis
->width_count
> 0 )
1114 segment_length_threshold
= ( laxis
->standard_width
* 10 ) >> 4;
1116 segment_length_threshold
= FT_DivFix( 64, hints
->y_scale
);
1119 segment_length_threshold
= 0;
1121 /*********************************************************************/
1123 /* We will begin by generating a sorted table of edges for the */
1124 /* current direction. To do so, we simply scan each segment and try */
1125 /* to find an edge in our table that corresponds to its position. */
1127 /* If no edge is found, we create and insert a new edge in the */
1128 /* sorted table. Otherwise, we simply add the segment to the edge's */
1129 /* list which will be processed in the second step to compute the */
1130 /* edge's properties. */
1132 /* Note that the edges table is sorted along the segment/edge */
1135 /*********************************************************************/
1137 edge_distance_threshold
= FT_MulFix( laxis
->edge_distance_threshold
,
1139 if ( edge_distance_threshold
> 64 / 4 )
1140 edge_distance_threshold
= 64 / 4;
1142 edge_distance_threshold
= FT_DivFix( edge_distance_threshold
,
1145 for ( seg
= segments
; seg
< segment_limit
; seg
++ )
1147 AF_Edge found
= NULL
;
1151 if ( seg
->height
< segment_length_threshold
)
1154 /* A special case for serif edges: If they are smaller than */
1155 /* 1.5 pixels we ignore them. */
1158 FT_Pos dist
= seg
->serif
->pos
- seg
->pos
;
1164 if ( dist
>= laxis
->standard_width
>> 1 )
1166 /* unlink this serif, it is too distant from its reference stem */
1169 else if ( 2*seg
->height
< 3 * segment_length_threshold
)
1173 /* look for an edge corresponding to the segment */
1174 for ( ee
= 0; ee
< axis
->num_edges
; ee
++ )
1176 AF_Edge edge
= axis
->edges
+ ee
;
1180 dist
= seg
->pos
- edge
->fpos
;
1184 if ( dist
< edge_distance_threshold
&& edge
->dir
== seg
->dir
)
1196 /* insert a new edge in the list and */
1197 /* sort according to the position */
1198 error
= af_axis_hints_new_edge( axis
, seg
->pos
, seg
->dir
, 0,
1203 /* add the segment to the new edge's list */
1208 edge
->dir
= seg
->dir
;
1209 edge
->fpos
= seg
->pos
;
1210 edge
->opos
= FT_MulFix( seg
->pos
, scale
);
1211 edge
->pos
= edge
->opos
;
1212 seg
->edge_next
= seg
;
1216 /* if an edge was found, simply add the segment to the edge's */
1218 seg
->edge_next
= found
->first
;
1219 found
->last
->edge_next
= seg
;
1225 /*********************************************************************/
1227 /* Good, we will now compute each edge's properties according to */
1228 /* segments found on its position. Basically, these are: */
1230 /* - edge's main direction */
1231 /* - stem edge, serif edge or both (which defaults to stem then) */
1232 /* - rounded edge, straight or both (which defaults to straight) */
1233 /* - link for edge */
1235 /*********************************************************************/
1237 /* first of all, set the `edge' field in each segment -- this is */
1238 /* required in order to compute edge links */
1241 * Note that removing this loop and setting the `edge' field of each
1242 * segment directly in the code above slows down execution speed for
1243 * some reasons on platforms like the Sun.
1246 AF_Edge edges
= axis
->edges
;
1247 AF_Edge edge_limit
= edges
+ axis
->num_edges
;
1251 for ( edge
= edges
; edge
< edge_limit
; edge
++ )
1258 seg
= seg
->edge_next
;
1260 } while ( seg
!= edge
->first
);
1263 /* now, compute each edge properties */
1264 for ( edge
= edges
; edge
< edge_limit
; edge
++ )
1266 FT_Int is_round
= 0; /* does it contain round segments? */
1267 FT_Int is_straight
= 0; /* does it contain straight segments? */
1269 FT_Pos ups
= 0; /* number of upwards segments */
1270 FT_Pos downs
= 0; /* number of downwards segments */
1281 /* check for roundness of segment */
1282 if ( seg
->flags
& AF_EDGE_ROUND
)
1288 /* check for segment direction */
1289 if ( seg
->dir
== up_dir
)
1290 ups
+= seg
->max_coord
-seg
->min_coord
;
1292 downs
+= seg
->max_coord
-seg
->min_coord
;
1295 /* check for links -- if seg->serif is set, then seg->link must */
1297 is_serif
= (FT_Bool
)( seg
->serif
&&
1299 seg
->serif
->edge
!= edge
);
1301 if ( ( seg
->link
&& seg
->link
->edge
!= NULL
) || is_serif
)
1313 edge2
= edge
->serif
;
1322 edge_delta
= edge
->fpos
- edge2
->fpos
;
1323 if ( edge_delta
< 0 )
1324 edge_delta
= -edge_delta
;
1326 seg_delta
= seg
->pos
- seg2
->pos
;
1327 if ( seg_delta
< 0 )
1328 seg_delta
= -seg_delta
;
1330 if ( seg_delta
< edge_delta
)
1338 edge
->serif
= edge2
;
1339 edge2
->flags
|= AF_EDGE_SERIF
;
1345 seg
= seg
->edge_next
;
1347 } while ( seg
!= edge
->first
);
1349 /* set the round/straight flags */
1350 edge
->flags
= AF_EDGE_NORMAL
;
1352 if ( is_round
> 0 && is_round
>= is_straight
)
1353 edge
->flags
|= AF_EDGE_ROUND
;
1356 /* set the edge's main direction */
1357 edge
->dir
= AF_DIR_NONE
;
1360 edge
->dir
= (FT_Char
)up_dir
;
1362 else if ( ups
< downs
)
1363 edge
->dir
= (FT_Char
)-up_dir
;
1365 else if ( ups
== downs
)
1366 edge
->dir
= 0; /* both up and down! */
1369 /* gets rid of serifs if link is set */
1370 /* XXX: This gets rid of many unpleasant artefacts! */
1371 /* Example: the `c' in cour.pfa at size 13 */
1373 if ( edge
->serif
&& edge
->link
)
1383 FT_LOCAL_DEF( FT_Error
)
1384 af_latin2_hints_detect_features( AF_GlyphHints hints
,
1390 error
= af_latin2_hints_compute_segments( hints
, dim
);
1393 af_latin2_hints_link_segments( hints
, dim
);
1395 error
= af_latin2_hints_compute_edges( hints
, dim
);
1402 af_latin2_hints_compute_blue_edges( AF_GlyphHints hints
,
1403 AF_LatinMetrics metrics
)
1405 AF_AxisHints axis
= &hints
->axis
[AF_DIMENSION_VERT
];
1406 AF_Edge edge
= axis
->edges
;
1407 AF_Edge edge_limit
= edge
+ axis
->num_edges
;
1408 AF_LatinAxis latin
= &metrics
->axis
[AF_DIMENSION_VERT
];
1409 FT_Fixed scale
= latin
->scale
;
1410 FT_Pos best_dist0
; /* initial threshold */
1413 /* compute the initial threshold as a fraction of the EM size */
1414 best_dist0
= FT_MulFix( metrics
->units_per_em
/ 40, scale
);
1416 if ( best_dist0
> 64 / 2 )
1417 best_dist0
= 64 / 2;
1419 /* compute which blue zones are active, i.e. have their scaled */
1420 /* size < 3/4 pixels */
1422 /* for each horizontal edge search the blue zone which is closest */
1423 for ( ; edge
< edge_limit
; edge
++ )
1426 AF_Width best_blue
= NULL
;
1427 FT_Pos best_dist
= best_dist0
;
1429 for ( bb
= 0; bb
< AF_LATIN_BLUE_MAX
; bb
++ )
1431 AF_LatinBlue blue
= latin
->blues
+ bb
;
1432 FT_Bool is_top_blue
, is_major_dir
;
1435 /* skip inactive blue zones (i.e., those that are too small) */
1436 if ( !( blue
->flags
& AF_LATIN_BLUE_ACTIVE
) )
1439 /* if it is a top zone, check for right edges -- if it is a bottom */
1440 /* zone, check for left edges */
1442 /* of course, that's for TrueType */
1443 is_top_blue
= (FT_Byte
)( ( blue
->flags
& AF_LATIN_BLUE_TOP
) != 0 );
1444 is_major_dir
= FT_BOOL( edge
->dir
== axis
->major_dir
);
1446 /* if it is a top zone, the edge must be against the major */
1447 /* direction; if it is a bottom zone, it must be in the major */
1449 if ( is_top_blue
^ is_major_dir
)
1455 /* if it's a rounded edge, compare it to the overshoot position */
1456 /* if it's a flat edge, compare it to the reference position */
1457 if ( edge
->flags
& AF_EDGE_ROUND
)
1458 compare
= &blue
->shoot
;
1460 compare
= &blue
->ref
;
1462 dist
= edge
->fpos
- compare
->org
;
1466 dist
= FT_MulFix( dist
, scale
);
1467 if ( dist
< best_dist
)
1470 best_blue
= compare
;
1474 /* now, compare it to the overshoot position if the edge is */
1475 /* rounded, and if the edge is over the reference position of a */
1476 /* top zone, or under the reference position of a bottom zone */
1477 if ( edge
->flags
& AF_EDGE_ROUND
&& dist
!= 0 )
1479 FT_Bool is_under_ref
= FT_BOOL( edge
->fpos
< blue
->ref
.org
);
1482 if ( is_top_blue
^ is_under_ref
)
1484 blue
= latin
->blues
+ bb
;
1485 dist
= edge
->fpos
- blue
->shoot
.org
;
1489 dist
= FT_MulFix( dist
, scale
);
1490 if ( dist
< best_dist
)
1493 best_blue
= & blue
->shoot
;
1502 edge
->blue_edge
= best_blue
;
1508 af_latin2_hints_init( AF_GlyphHints hints
,
1509 AF_LatinMetrics metrics
)
1511 FT_Render_Mode mode
;
1512 FT_UInt32 scaler_flags
, other_flags
;
1513 FT_Face face
= metrics
->root
.scaler
.face
;
1516 af_glyph_hints_rescale( hints
, (AF_StyleMetrics
)metrics
);
1519 * correct x_scale and y_scale if needed, since they may have
1520 * been modified `af_latin2_metrics_scale_dim' above
1522 hints
->x_scale
= metrics
->axis
[AF_DIMENSION_HORZ
].scale
;
1523 hints
->x_delta
= metrics
->axis
[AF_DIMENSION_HORZ
].delta
;
1524 hints
->y_scale
= metrics
->axis
[AF_DIMENSION_VERT
].scale
;
1525 hints
->y_delta
= metrics
->axis
[AF_DIMENSION_VERT
].delta
;
1527 /* compute flags depending on render mode, etc. */
1528 mode
= metrics
->root
.scaler
.render_mode
;
1530 #if 0 /* #ifdef AF_CONFIG_OPTION_USE_WARPER */
1531 if ( mode
== FT_RENDER_MODE_LCD
|| mode
== FT_RENDER_MODE_LCD_V
)
1532 metrics
->root
.scaler
.render_mode
= mode
= FT_RENDER_MODE_NORMAL
;
1535 scaler_flags
= hints
->scaler_flags
;
1539 * We snap the width of vertical stems for the monochrome and
1540 * horizontal LCD rendering targets only.
1542 if ( mode
== FT_RENDER_MODE_MONO
|| mode
== FT_RENDER_MODE_LCD
)
1543 other_flags
|= AF_LATIN_HINTS_HORZ_SNAP
;
1546 * We snap the width of horizontal stems for the monochrome and
1547 * vertical LCD rendering targets only.
1549 if ( mode
== FT_RENDER_MODE_MONO
|| mode
== FT_RENDER_MODE_LCD_V
)
1550 other_flags
|= AF_LATIN_HINTS_VERT_SNAP
;
1553 * We adjust stems to full pixels only if we don't use the `light' mode.
1555 if ( mode
!= FT_RENDER_MODE_LIGHT
)
1556 other_flags
|= AF_LATIN_HINTS_STEM_ADJUST
;
1558 if ( mode
== FT_RENDER_MODE_MONO
)
1559 other_flags
|= AF_LATIN_HINTS_MONO
;
1562 * In `light' hinting mode we disable horizontal hinting completely.
1563 * We also do it if the face is italic.
1565 if ( mode
== FT_RENDER_MODE_LIGHT
||
1566 ( face
->style_flags
& FT_STYLE_FLAG_ITALIC
) != 0 )
1567 scaler_flags
|= AF_SCALER_FLAG_NO_HORIZONTAL
;
1569 #ifdef AF_CONFIG_OPTION_USE_WARPER
1570 /* get (global) warper flag */
1571 if ( !metrics
->root
.globals
->module
->warping
)
1572 scaler_flags
|= AF_SCALER_FLAG_NO_WARPER
;
1575 hints
->scaler_flags
= scaler_flags
;
1576 hints
->other_flags
= other_flags
;
1582 /*************************************************************************/
1583 /*************************************************************************/
1585 /***** L A T I N G L Y P H G R I D - F I T T I N G *****/
1587 /*************************************************************************/
1588 /*************************************************************************/
1590 /* snap a given width in scaled coordinates to one of the */
1591 /* current standard widths */
1594 af_latin2_snap_width( AF_Width widths
,
1599 FT_Pos best
= 64 + 32 + 2;
1600 FT_Pos reference
= width
;
1604 for ( n
= 0; n
< count
; n
++ )
1621 scaled
= FT_PIX_ROUND( reference
);
1623 if ( width
>= reference
)
1625 if ( width
< scaled
+ 48 )
1630 if ( width
> scaled
- 48 )
1638 /* compute the snapped width of a given stem */
1641 af_latin2_compute_stem_width( AF_GlyphHints hints
,
1645 FT_UInt stem_flags
)
1647 AF_LatinMetrics metrics
= (AF_LatinMetrics
) hints
->metrics
;
1648 AF_LatinAxis axis
= & metrics
->axis
[dim
];
1649 FT_Pos dist
= width
;
1651 FT_Int vertical
= ( dim
== AF_DIMENSION_VERT
);
1653 FT_UNUSED( base_flags
);
1656 if ( !AF_LATIN_HINTS_DO_STEM_ADJUST( hints
) ||
1666 if ( ( vertical
&& !AF_LATIN_HINTS_DO_VERT_SNAP( hints
) ) ||
1667 ( !vertical
&& !AF_LATIN_HINTS_DO_HORZ_SNAP( hints
) ) )
1669 /* smooth hinting process: very lightly quantize the stem width */
1671 /* leave the widths of serifs alone */
1673 if ( ( stem_flags
& AF_EDGE_SERIF
) && vertical
&& ( dist
< 3 * 64 ) )
1677 else if ( ( base_flags
& AF_EDGE_ROUND
) )
1682 else if ( dist
< 56 )
1685 if ( axis
->width_count
> 0 )
1690 /* compare to standard width */
1691 if ( axis
->width_count
> 0 )
1693 delta
= dist
- axis
->widths
[0].cur
;
1700 dist
= axis
->widths
[0].cur
;
1708 if ( dist
< 3 * 64 )
1716 else if ( delta
< 32 )
1719 else if ( delta
< 54 )
1726 dist
= ( dist
+ 32 ) & ~63;
1731 /* strong hinting process: snap the stem width to integer pixels */
1732 FT_Pos org_dist
= dist
;
1735 dist
= af_latin2_snap_width( axis
->widths
, axis
->width_count
, dist
);
1739 /* in the case of vertical hinting, always round */
1740 /* the stem heights to integer pixels */
1743 dist
= ( dist
+ 16 ) & ~63;
1749 if ( AF_LATIN_HINTS_DO_MONO( hints
) )
1751 /* monochrome horizontal hinting: snap widths to integer pixels */
1752 /* with a different threshold */
1757 dist
= ( dist
+ 32 ) & ~63;
1761 /* for horizontal anti-aliased hinting, we adopt a more subtle */
1762 /* approach: we strengthen small stems, round stems whose size */
1763 /* is between 1 and 2 pixels to an integer, otherwise nothing */
1766 dist
= ( dist
+ 64 ) >> 1;
1768 else if ( dist
< 128 )
1770 /* We only round to an integer width if the corresponding */
1771 /* distortion is less than 1/4 pixel. Otherwise this */
1772 /* makes everything worse since the diagonals, which are */
1773 /* not hinted, appear a lot bolder or thinner than the */
1774 /* vertical stems. */
1779 dist
= ( dist
+ 22 ) & ~63;
1780 delta
= dist
- org_dist
;
1788 dist
= ( dist
+ 64 ) >> 1;
1792 /* round otherwise to prevent color fringes in LCD mode */
1793 dist
= ( dist
+ 32 ) & ~63;
1806 /* align one stem edge relative to the previous stem edge */
1809 af_latin2_align_linked_edge( AF_GlyphHints hints
,
1814 FT_Pos dist
= stem_edge
->opos
- base_edge
->opos
;
1816 FT_Pos fitted_width
= af_latin2_compute_stem_width( hints
, dim
, dist
,
1821 stem_edge
->pos
= base_edge
->pos
+ fitted_width
;
1823 FT_TRACE5(( "LINK: edge %d (opos=%.2f) linked to (%.2f), "
1824 "dist was %.2f, now %.2f\n",
1825 stem_edge
-hints
->axis
[dim
].edges
, stem_edge
->opos
/ 64.0,
1826 stem_edge
->pos
/ 64.0, dist
/ 64.0, fitted_width
/ 64.0 ));
1831 af_latin2_align_serif_edge( AF_GlyphHints hints
,
1837 serif
->pos
= base
->pos
+ ( serif
->opos
- base
->opos
);
1841 /*************************************************************************/
1842 /*************************************************************************/
1843 /*************************************************************************/
1845 /**** E D G E H I N T I N G ****/
1847 /*************************************************************************/
1848 /*************************************************************************/
1849 /*************************************************************************/
1853 af_latin2_hint_edges( AF_GlyphHints hints
,
1856 AF_AxisHints axis
= &hints
->axis
[dim
];
1857 AF_Edge edges
= axis
->edges
;
1858 AF_Edge edge_limit
= edges
+ axis
->num_edges
;
1860 AF_Edge anchor
= NULL
;
1861 FT_Int has_serifs
= 0;
1862 FT_Pos anchor_drift
= 0;
1866 FT_TRACE5(( "==== hinting %s edges =====\n",
1867 dim
== AF_DIMENSION_HORZ
? "vertical" : "horizontal" ));
1869 /* we begin by aligning all stems relative to the blue zone */
1870 /* if needed -- that's only for horizontal edges */
1872 if ( dim
== AF_DIMENSION_VERT
&& AF_HINTS_DO_BLUES( hints
) )
1874 for ( edge
= edges
; edge
< edge_limit
; edge
++ )
1877 AF_Edge edge1
, edge2
;
1880 if ( edge
->flags
& AF_EDGE_DONE
)
1883 blue
= edge
->blue_edge
;
1891 else if ( edge2
&& edge2
->blue_edge
)
1893 blue
= edge2
->blue_edge
;
1901 FT_TRACE5(( "BLUE: edge %d (opos=%.2f) snapped to (%.2f), "
1903 edge1
-edges
, edge1
->opos
/ 64.0, blue
->fit
/ 64.0,
1904 edge1
->pos
/ 64.0 ));
1906 edge1
->pos
= blue
->fit
;
1907 edge1
->flags
|= AF_EDGE_DONE
;
1909 if ( edge2
&& !edge2
->blue_edge
)
1911 af_latin2_align_linked_edge( hints
, dim
, edge1
, edge2
);
1912 edge2
->flags
|= AF_EDGE_DONE
;
1919 anchor_drift
= ( anchor
->pos
- anchor
->opos
);
1921 anchor_drift
= ( anchor_drift
+
1922 ( edge2
->pos
- edge2
->opos
) ) >> 1;
1927 /* now we will align all stem edges, trying to maintain the */
1928 /* relative order of stems in the glyph */
1929 for ( edge
= edges
; edge
< edge_limit
; edge
++ )
1934 if ( edge
->flags
& AF_EDGE_DONE
)
1937 /* skip all non-stem edges */
1945 /* now align the stem */
1947 /* this should not happen, but it's better to be safe */
1948 if ( edge2
->blue_edge
)
1950 FT_TRACE5(( "ASSERTION FAILED for edge %d\n", edge2
-edges
));
1952 af_latin2_align_linked_edge( hints
, dim
, edge2
, edge
);
1953 edge
->flags
|= AF_EDGE_DONE
;
1959 FT_Pos org_len
, org_center
, cur_len
;
1960 FT_Pos cur_pos1
, error1
, error2
, u_off
, d_off
;
1963 org_len
= edge2
->opos
- edge
->opos
;
1964 cur_len
= af_latin2_compute_stem_width( hints
, dim
, org_len
,
1967 if ( cur_len
<= 64 )
1977 org_center
= edge
->opos
+ ( org_len
>> 1 );
1979 cur_pos1
= FT_PIX_ROUND( org_center
);
1981 error1
= org_center
- ( cur_pos1
- u_off
);
1985 error2
= org_center
- ( cur_pos1
+ d_off
);
1989 if ( error1
< error2
)
1994 edge
->pos
= cur_pos1
- cur_len
/ 2;
1995 edge2
->pos
= edge
->pos
+ cur_len
;
1998 edge
->pos
= FT_PIX_ROUND( edge
->opos
);
2000 FT_TRACE5(( "ANCHOR: edge %d (opos=%.2f) and %d (opos=%.2f)"
2001 " snapped to (%.2f) (%.2f)\n",
2002 edge
-edges
, edge
->opos
/ 64.0,
2003 edge2
-edges
, edge2
->opos
/ 64.0,
2004 edge
->pos
/ 64.0, edge2
->pos
/ 64.0 ));
2007 edge
->flags
|= AF_EDGE_DONE
;
2009 af_latin2_align_linked_edge( hints
, dim
, edge
, edge2
);
2011 edge2
->flags
|= AF_EDGE_DONE
;
2013 anchor_drift
= ( ( anchor
->pos
- anchor
->opos
) +
2014 ( edge2
->pos
- edge2
->opos
) ) >> 1;
2016 FT_TRACE5(( "DRIFT: %.2f\n", anchor_drift
/64.0 ));
2020 FT_Pos org_pos
, org_len
, org_center
, cur_center
, cur_len
;
2021 FT_Pos org_left
, org_right
;
2024 org_pos
= edge
->opos
+ anchor_drift
;
2025 org_len
= edge2
->opos
- edge
->opos
;
2026 org_center
= org_pos
+ ( org_len
>> 1 );
2028 cur_len
= af_latin2_compute_stem_width( hints
, dim
, org_len
,
2032 org_left
= org_pos
+ ( ( org_len
- cur_len
) >> 1 );
2033 org_right
= org_pos
+ ( ( org_len
+ cur_len
) >> 1 );
2035 FT_TRACE5(( "ALIGN: left=%.2f right=%.2f ",
2036 org_left
/ 64.0, org_right
/ 64.0 ));
2037 cur_center
= org_center
;
2039 if ( edge2
->flags
& AF_EDGE_DONE
)
2041 FT_TRACE5(( "\n" ));
2042 edge
->pos
= edge2
->pos
- cur_len
;
2046 /* we want to compare several displacement, and choose
2047 * the one that increases fitness while minimizing
2048 * distortion as well
2050 FT_Pos displacements
[6], scores
[6], org
, fit
, delta
;
2053 /* note: don't even try to fit tiny stems */
2056 FT_TRACE5(( "tiny stem\n" ));
2060 /* if the span is within a single pixel, don't touch it */
2061 if ( FT_PIX_FLOOR( org_left
) == FT_PIX_CEIL( org_right
) )
2063 FT_TRACE5(( "single pixel stem\n" ));
2067 if ( cur_len
<= 96 )
2069 /* we want to avoid the absolute worst case which is
2070 * when the left and right edges of the span each represent
2071 * about 50% of the gray. we'd better want to change this
2072 * to 25/75%, since this is much more pleasant to the eye with
2073 * very acceptable distortion
2075 FT_Pos frac_left
= org_left
& 63;
2076 FT_Pos frac_right
= org_right
& 63;
2078 if ( frac_left
>= 22 && frac_left
<= 42 &&
2079 frac_right
>= 22 && frac_right
<= 42 )
2082 fit
= ( org
<= 32 ) ? 16 : 48;
2083 delta
= FT_ABS( fit
- org
);
2084 displacements
[count
] = fit
- org
;
2085 scores
[count
++] = delta
;
2086 FT_TRACE5(( "dispA=%.2f (%d) ", ( fit
- org
) / 64.0, delta
));
2089 fit
= ( org
<= 32 ) ? 16 : 48;
2090 delta
= FT_ABS( fit
- org
);
2091 displacements
[count
] = fit
- org
;
2092 scores
[count
++] = delta
;
2093 FT_TRACE5(( "dispB=%.2f (%d) ", ( fit
- org
) / 64.0, delta
));
2097 /* snapping the left edge to the grid */
2099 fit
= FT_PIX_ROUND( org
);
2100 delta
= FT_ABS( fit
- org
);
2101 displacements
[count
] = fit
- org
;
2102 scores
[count
++] = delta
;
2103 FT_TRACE5(( "dispC=%.2f (%d) ", ( fit
- org
) / 64.0, delta
));
2105 /* snapping the right edge to the grid */
2107 fit
= FT_PIX_ROUND( org
);
2108 delta
= FT_ABS( fit
- org
);
2109 displacements
[count
] = fit
- org
;
2110 scores
[count
++] = delta
;
2111 FT_TRACE5(( "dispD=%.2f (%d) ", ( fit
- org
) / 64.0, delta
));
2113 /* now find the best displacement */
2115 FT_Pos best_score
= scores
[0];
2116 FT_Pos best_disp
= displacements
[0];
2119 for ( nn
= 1; nn
< count
; nn
++ )
2121 if ( scores
[nn
] < best_score
)
2123 best_score
= scores
[nn
];
2124 best_disp
= displacements
[nn
];
2128 cur_center
= org_center
+ best_disp
;
2130 FT_TRACE5(( "\n" ));
2134 edge
->pos
= cur_center
- ( cur_len
>> 1 );
2135 edge2
->pos
= edge
->pos
+ cur_len
;
2137 FT_TRACE5(( "STEM1: %d (opos=%.2f) to %d (opos=%.2f)"
2138 " snapped to (%.2f) and (%.2f),"
2139 " org_len=%.2f cur_len=%.2f\n",
2140 edge
-edges
, edge
->opos
/ 64.0,
2141 edge2
-edges
, edge2
->opos
/ 64.0,
2142 edge
->pos
/ 64.0, edge2
->pos
/ 64.0,
2143 org_len
/ 64.0, cur_len
/ 64.0 ));
2145 edge
->flags
|= AF_EDGE_DONE
;
2146 edge2
->flags
|= AF_EDGE_DONE
;
2148 if ( edge
> edges
&& edge
->pos
< edge
[-1].pos
)
2150 FT_TRACE5(( "BOUND: %d (pos=%.2f) to (%.2f)\n",
2151 edge
-edges
, edge
->pos
/ 64.0, edge
[-1].pos
/ 64.0 ));
2152 edge
->pos
= edge
[-1].pos
;
2157 /* make sure that lowercase m's maintain their symmetry */
2159 /* In general, lowercase m's have six vertical edges if they are sans */
2160 /* serif, or twelve if they are with serifs. This implementation is */
2161 /* based on that assumption, and seems to work very well with most */
2162 /* faces. However, if for a certain face this assumption is not */
2163 /* true, the m is just rendered like before. In addition, any stem */
2164 /* correction will only be applied to symmetrical glyphs (even if the */
2165 /* glyph is not an m), so the potential for unwanted distortion is */
2166 /* relatively low. */
2168 /* We don't handle horizontal edges since we can't easily assure that */
2169 /* the third (lowest) stem aligns with the base line; it might end up */
2170 /* one pixel higher or lower. */
2174 FT_Int n_edges
= edge_limit
- edges
;
2177 if ( dim
== AF_DIMENSION_HORZ
&& ( n_edges
== 6 || n_edges
== 12 ) )
2179 AF_Edge edge1
, edge2
, edge3
;
2180 FT_Pos dist1
, dist2
, span
, delta
;
2196 dist1
= edge2
->opos
- edge1
->opos
;
2197 dist2
= edge3
->opos
- edge2
->opos
;
2199 span
= dist1
- dist2
;
2205 delta
= edge3
->pos
- ( 2 * edge2
->pos
- edge1
->pos
);
2206 edge3
->pos
-= delta
;
2208 edge3
->link
->pos
-= delta
;
2210 /* move the serifs along with the stem */
2211 if ( n_edges
== 12 )
2213 ( edges
+ 8 )->pos
-= delta
;
2214 ( edges
+ 11 )->pos
-= delta
;
2217 edge3
->flags
|= AF_EDGE_DONE
;
2219 edge3
->link
->flags
|= AF_EDGE_DONE
;
2225 if ( has_serifs
|| !anchor
)
2228 * now hint the remaining edges (serifs and single) in order
2229 * to complete our processing
2231 for ( edge
= edges
; edge
< edge_limit
; edge
++ )
2236 if ( edge
->flags
& AF_EDGE_DONE
)
2243 delta
= edge
->serif
->opos
- edge
->opos
;
2248 if ( delta
< 64 + 16 )
2250 af_latin2_align_serif_edge( hints
, edge
->serif
, edge
);
2251 FT_TRACE5(( "SERIF: edge %d (opos=%.2f) serif to %d (opos=%.2f)"
2252 " aligned to (%.2f)\n",
2253 edge
-edges
, edge
->opos
/ 64.0,
2254 edge
->serif
- edges
, edge
->serif
->opos
/ 64.0,
2255 edge
->pos
/ 64.0 ));
2259 FT_TRACE5(( "SERIF_ANCHOR: edge %d (opos=%.2f)"
2260 " snapped to (%.2f)\n",
2261 edge
-edges
, edge
->opos
/ 64.0, edge
->pos
/ 64.0 ));
2262 edge
->pos
= FT_PIX_ROUND( edge
->opos
);
2267 AF_Edge before
, after
;
2270 for ( before
= edge
- 1; before
>= edges
; before
-- )
2271 if ( before
->flags
& AF_EDGE_DONE
)
2274 for ( after
= edge
+ 1; after
< edge_limit
; after
++ )
2275 if ( after
->flags
& AF_EDGE_DONE
)
2278 if ( before
>= edges
&& before
< edge
&&
2279 after
< edge_limit
&& after
> edge
)
2281 if ( after
->opos
== before
->opos
)
2282 edge
->pos
= before
->pos
;
2284 edge
->pos
= before
->pos
+
2285 FT_MulDiv( edge
->opos
- before
->opos
,
2286 after
->pos
- before
->pos
,
2287 after
->opos
- before
->opos
);
2288 FT_TRACE5(( "SERIF_LINK1: edge %d (opos=%.2f) snapped to (%.2f)"
2289 " from %d (opos=%.2f)\n",
2290 edge
-edges
, edge
->opos
/ 64.0, edge
->pos
/ 64.0,
2291 before
- edges
, before
->opos
/ 64.0 ));
2295 edge
->pos
= anchor
->pos
+
2296 ( ( edge
->opos
- anchor
->opos
+ 16 ) & ~31 );
2298 FT_TRACE5(( "SERIF_LINK2: edge %d (opos=%.2f)"
2299 " snapped to (%.2f)\n",
2300 edge
-edges
, edge
->opos
/ 64.0, edge
->pos
/ 64.0 ));
2304 edge
->flags
|= AF_EDGE_DONE
;
2306 if ( edge
> edges
&& edge
->pos
< edge
[-1].pos
)
2307 edge
->pos
= edge
[-1].pos
;
2309 if ( edge
+ 1 < edge_limit
&&
2310 edge
[1].flags
& AF_EDGE_DONE
&&
2311 edge
->pos
> edge
[1].pos
)
2312 edge
->pos
= edge
[1].pos
;
2319 af_latin2_hints_apply( FT_UInt glyph_index
,
2320 AF_GlyphHints hints
,
2321 FT_Outline
* outline
,
2322 AF_LatinMetrics metrics
)
2327 FT_UNUSED( glyph_index
);
2330 error
= af_glyph_hints_reload( hints
, outline
);
2334 /* analyze glyph outline */
2335 #ifdef AF_CONFIG_OPTION_USE_WARPER
2336 if ( ( metrics
->root
.scaler
.render_mode
== FT_RENDER_MODE_LIGHT
&&
2337 AF_HINTS_DO_WARP( hints
) ) ||
2338 AF_HINTS_DO_HORIZONTAL( hints
) )
2340 if ( AF_HINTS_DO_HORIZONTAL( hints
) )
2343 error
= af_latin2_hints_detect_features( hints
, AF_DIMENSION_HORZ
);
2348 if ( AF_HINTS_DO_VERTICAL( hints
) )
2350 error
= af_latin2_hints_detect_features( hints
, AF_DIMENSION_VERT
);
2354 af_latin2_hints_compute_blue_edges( hints
, metrics
);
2357 /* grid-fit the outline */
2358 for ( dim
= 0; dim
< AF_DIMENSION_MAX
; dim
++ )
2360 #ifdef AF_CONFIG_OPTION_USE_WARPER
2361 if ( dim
== AF_DIMENSION_HORZ
&&
2362 metrics
->root
.scaler
.render_mode
== FT_RENDER_MODE_LIGHT
&&
2363 AF_HINTS_DO_WARP( hints
) )
2365 AF_WarperRec warper
;
2370 af_warper_compute( &warper
, hints
, dim
, &scale
, &delta
);
2371 af_glyph_hints_scale_dim( hints
, dim
, scale
, delta
);
2374 #endif /* AF_CONFIG_OPTION_USE_WARPER */
2376 if ( ( dim
== AF_DIMENSION_HORZ
&& AF_HINTS_DO_HORIZONTAL( hints
) ) ||
2377 ( dim
== AF_DIMENSION_VERT
&& AF_HINTS_DO_VERTICAL( hints
) ) )
2379 af_latin2_hint_edges( hints
, (AF_Dimension
)dim
);
2380 af_glyph_hints_align_edge_points( hints
, (AF_Dimension
)dim
);
2381 af_glyph_hints_align_strong_points( hints
, (AF_Dimension
)dim
);
2382 af_glyph_hints_align_weak_points( hints
, (AF_Dimension
)dim
);
2385 af_glyph_hints_save( hints
, outline
);
2392 /*************************************************************************/
2393 /*************************************************************************/
2395 /***** L A T I N S C R I P T C L A S S *****/
2397 /*************************************************************************/
2398 /*************************************************************************/
2401 AF_DEFINE_WRITING_SYSTEM_CLASS(
2402 af_latin2_writing_system_class
,
2404 AF_WRITING_SYSTEM_LATIN2
,
2406 sizeof ( AF_LatinMetricsRec
),
2408 (AF_WritingSystem_InitMetricsFunc
) af_latin2_metrics_init
,
2409 (AF_WritingSystem_ScaleMetricsFunc
)af_latin2_metrics_scale
,
2410 (AF_WritingSystem_DoneMetricsFunc
) NULL
,
2411 (AF_WritingSystem_GetStdWidthsFunc
)af_latin2_get_standard_widths
,
2413 (AF_WritingSystem_InitHintsFunc
) af_latin2_hints_init
,
2414 (AF_WritingSystem_ApplyHintsFunc
) af_latin2_hints_apply