1 /***************************************************************************/
5 /* Auto-fitter hinting routines for latin script (body). */
7 /* Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009 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
30 FT_LOCAL_DEF( FT_Error
)
31 af_latin2_hints_compute_segments( AF_GlyphHints hints
,
35 af_latin2_hints_link_segments( AF_GlyphHints hints
,
38 /*************************************************************************/
39 /*************************************************************************/
41 /***** L A T I N G L O B A L M E T R I C S *****/
43 /*************************************************************************/
44 /*************************************************************************/
47 af_latin2_metrics_init_widths( AF_LatinMetrics metrics
,
51 /* scan the array of segments in each direction */
52 AF_GlyphHintsRec hints
[1];
55 af_glyph_hints_init( hints
, face
->memory
);
57 metrics
->axis
[AF_DIMENSION_HORZ
].width_count
= 0;
58 metrics
->axis
[AF_DIMENSION_VERT
].width_count
= 0;
64 AF_LatinMetricsRec dummy
[1];
65 AF_Scaler scaler
= &dummy
->root
.scaler
;
68 glyph_index
= FT_Get_Char_Index( face
, charcode
);
69 if ( glyph_index
== 0 )
72 error
= FT_Load_Glyph( face
, glyph_index
, FT_LOAD_NO_SCALE
);
73 if ( error
|| face
->glyph
->outline
.n_points
<= 0 )
78 dummy
->units_per_em
= metrics
->units_per_em
;
79 scaler
->x_scale
= scaler
->y_scale
= 0x10000L
;
80 scaler
->x_delta
= scaler
->y_delta
= 0;
82 scaler
->render_mode
= FT_RENDER_MODE_NORMAL
;
85 af_glyph_hints_rescale( hints
, (AF_ScriptMetrics
)dummy
);
87 error
= af_glyph_hints_reload( hints
, &face
->glyph
->outline
, 0 );
91 for ( dim
= 0; dim
< AF_DIMENSION_MAX
; dim
++ )
93 AF_LatinAxis axis
= &metrics
->axis
[dim
];
94 AF_AxisHints axhints
= &hints
->axis
[dim
];
95 AF_Segment seg
, limit
, link
;
96 FT_UInt num_widths
= 0;
99 error
= af_latin2_hints_compute_segments( hints
,
104 af_latin2_hints_link_segments( hints
,
107 seg
= axhints
->segments
;
108 limit
= seg
+ axhints
->num_segments
;
110 for ( ; seg
< limit
; seg
++ )
114 /* we only consider stem segments there! */
115 if ( link
&& link
->link
== seg
&& link
> seg
)
120 dist
= seg
->pos
- link
->pos
;
124 if ( num_widths
< AF_LATIN_MAX_WIDTHS
)
125 axis
->widths
[ num_widths
++ ].org
= dist
;
129 af_sort_widths( num_widths
, axis
->widths
);
130 axis
->width_count
= num_widths
;
134 for ( dim
= 0; dim
< AF_DIMENSION_MAX
; dim
++ )
136 AF_LatinAxis axis
= &metrics
->axis
[dim
];
140 stdw
= ( axis
->width_count
> 0 )
141 ? axis
->widths
[0].org
142 : AF_LATIN_CONSTANT( metrics
, 50 );
144 /* let's try 20% of the smallest width */
145 axis
->edge_distance_threshold
= stdw
/ 5;
146 axis
->standard_width
= stdw
;
147 axis
->extra_light
= 0;
151 af_glyph_hints_done( hints
);
156 #define AF_LATIN_MAX_TEST_CHARACTERS 12
159 static const char af_latin2_blue_chars
[AF_LATIN_MAX_BLUES
][AF_LATIN_MAX_TEST_CHARACTERS
+1] =
171 af_latin2_metrics_init_blues( AF_LatinMetrics metrics
,
174 FT_Pos flats
[AF_LATIN_MAX_TEST_CHARACTERS
];
175 FT_Pos rounds
[AF_LATIN_MAX_TEST_CHARACTERS
];
181 AF_LatinAxis axis
= &metrics
->axis
[AF_DIMENSION_VERT
];
182 FT_GlyphSlot glyph
= face
->glyph
;
185 /* we compute the blues simply by loading each character from the */
186 /* 'af_latin2_blue_chars[blues]' string, then compute its top-most or */
187 /* bottom-most points (depending on `AF_IS_TOP_BLUE') */
189 AF_LOG(( "blue zones computation\n" ));
190 AF_LOG(( "------------------------------------------------\n" ));
192 for ( bb
= 0; bb
< AF_LATIN_BLUE_MAX
; bb
++ )
194 const char* p
= af_latin2_blue_chars
[bb
];
195 const char* limit
= p
+ AF_LATIN_MAX_TEST_CHARACTERS
;
200 AF_LOG(( "blue %3d: ", bb
));
205 for ( ; p
< limit
&& *p
; p
++ )
208 FT_Int best_point
, best_y
, best_first
, best_last
;
213 AF_LOG(( "'%c'", *p
));
215 /* load the character in the face -- skip unknown or empty ones */
216 glyph_index
= FT_Get_Char_Index( face
, (FT_UInt
)*p
);
217 if ( glyph_index
== 0 )
220 error
= FT_Load_Glyph( face
, glyph_index
, FT_LOAD_NO_SCALE
);
221 if ( error
|| glyph
->outline
.n_points
<= 0 )
224 /* now compute min or max point indices and coordinates */
225 points
= glyph
->outline
.points
;
227 best_y
= 0; /* make compiler happy */
228 best_first
= 0; /* ditto */
229 best_last
= 0; /* ditto */
237 for ( nn
= 0; nn
< glyph
->outline
.n_contours
; first
= last
+1, nn
++ )
239 FT_Int old_best_point
= best_point
;
243 last
= glyph
->outline
.contours
[nn
];
245 /* Avoid single-point contours since they are never rasterized. */
246 /* In some fonts, they correspond to mark attachment points */
247 /* which are way outside of the glyph's real outline. */
251 if ( AF_LATIN_IS_TOP_BLUE( bb
) )
253 for ( pp
= first
; pp
<= last
; pp
++ )
254 if ( best_point
< 0 || points
[pp
].y
> best_y
)
257 best_y
= points
[pp
].y
;
262 for ( pp
= first
; pp
<= last
; pp
++ )
263 if ( best_point
< 0 || points
[pp
].y
< best_y
)
266 best_y
= points
[pp
].y
;
270 if ( best_point
!= old_best_point
)
276 AF_LOG(( "%5d", best_y
));
279 /* now check whether the point belongs to a straight or round */
280 /* segment; we first need to find in which contour the extremum */
281 /* lies, then inspect its previous and next points */
283 FT_Int start
, end
, prev
, next
;
287 /* now look for the previous and next points that are not on the */
288 /* same Y coordinate. Threshold the `closeness'... */
289 start
= end
= best_point
;
294 if ( prev
< best_first
)
297 dist
= points
[prev
].y
- best_y
;
298 if ( dist
< -5 || dist
> 5 )
303 } while ( start
!= best_point
);
308 if ( next
> best_last
)
311 dist
= points
[next
].y
- best_y
;
312 if ( dist
< -5 || dist
> 5 )
317 } while ( end
!= best_point
);
319 /* now, set the `round' flag depending on the segment's kind */
321 FT_CURVE_TAG( glyph
->outline
.tags
[start
] ) != FT_CURVE_TAG_ON
||
322 FT_CURVE_TAG( glyph
->outline
.tags
[ end
] ) != FT_CURVE_TAG_ON
);
324 AF_LOG(( "%c ", round
? 'r' : 'f' ));
328 rounds
[num_rounds
++] = best_y
;
330 flats
[num_flats
++] = best_y
;
335 if ( num_flats
== 0 && num_rounds
== 0 )
338 * we couldn't find a single glyph to compute this blue zone,
339 * we will simply ignore it then
341 AF_LOG(( "empty\n" ));
345 /* we have computed the contents of the `rounds' and `flats' tables, */
346 /* now determine the reference and overshoot position of the blue -- */
347 /* we simply take the median value after a simple sort */
348 af_sort_pos( num_rounds
, rounds
);
349 af_sort_pos( num_flats
, flats
);
351 blue
= & axis
->blues
[axis
->blue_count
];
352 blue_ref
= & blue
->ref
.org
;
353 blue_shoot
= & blue
->shoot
.org
;
357 if ( num_flats
== 0 )
360 *blue_shoot
= rounds
[num_rounds
/ 2];
362 else if ( num_rounds
== 0 )
365 *blue_shoot
= flats
[num_flats
/ 2];
369 *blue_ref
= flats
[num_flats
/ 2];
370 *blue_shoot
= rounds
[num_rounds
/ 2];
373 /* there are sometimes problems: if the overshoot position of top */
374 /* zones is under its reference position, or the opposite for bottom */
375 /* zones. We must thus check everything there and correct the errors */
376 if ( *blue_shoot
!= *blue_ref
)
378 FT_Pos ref
= *blue_ref
;
379 FT_Pos shoot
= *blue_shoot
;
380 FT_Bool over_ref
= FT_BOOL( shoot
> ref
);
383 if ( AF_LATIN_IS_TOP_BLUE( bb
) ^ over_ref
)
384 *blue_shoot
= *blue_ref
= ( shoot
+ ref
) / 2;
388 if ( AF_LATIN_IS_TOP_BLUE( bb
) )
389 blue
->flags
|= AF_LATIN_BLUE_TOP
;
392 * The following flags is used later to adjust the y and x scales
393 * in order to optimize the pixel grid alignment of the top of small
396 if ( bb
== AF_LATIN_BLUE_SMALL_TOP
)
397 blue
->flags
|= AF_LATIN_BLUE_ADJUSTMENT
;
399 AF_LOG(( "-- ref = %ld, shoot = %ld\n", *blue_ref
, *blue_shoot
));
407 af_latin2_metrics_check_digits( AF_LatinMetrics metrics
,
411 FT_Bool started
= 0, same_width
= 1;
412 FT_Fixed advance
, old_advance
= 0;
415 /* check whether all ASCII digits have the same advance width; */
416 /* digit `0' is 0x30 in all supported charmaps */
417 for ( i
= 0x30; i
<= 0x39; i
++ )
422 glyph_index
= FT_Get_Char_Index( face
, i
);
423 if ( glyph_index
== 0 )
426 if ( FT_Get_Advance( face
, glyph_index
,
429 FT_LOAD_IGNORE_TRANSFORM
,
435 if ( advance
!= old_advance
)
443 old_advance
= advance
;
448 metrics
->root
.digits_have_same_width
= same_width
;
452 FT_LOCAL_DEF( FT_Error
)
453 af_latin2_metrics_init( AF_LatinMetrics metrics
,
456 FT_Error error
= AF_Err_Ok
;
457 FT_CharMap oldmap
= face
->charmap
;
460 static const FT_Encoding latin_encodings
[] =
463 FT_ENCODING_APPLE_ROMAN
,
464 FT_ENCODING_ADOBE_STANDARD
,
465 FT_ENCODING_ADOBE_LATIN_1
,
466 FT_ENCODING_NONE
/* end of list */
470 metrics
->units_per_em
= face
->units_per_EM
;
472 /* do we have a latin charmap in there? */
473 for ( ee
= 0; latin_encodings
[ee
] != FT_ENCODING_NONE
; ee
++ )
475 error
= FT_Select_Charmap( face
, latin_encodings
[ee
] );
482 /* For now, compute the standard width and height from the `o'. */
483 af_latin2_metrics_init_widths( metrics
, face
, 'o' );
484 af_latin2_metrics_init_blues( metrics
, face
);
485 af_latin2_metrics_check_digits( metrics
, face
);
488 FT_Set_Charmap( face
, oldmap
);
494 af_latin2_metrics_scale_dim( AF_LatinMetrics metrics
,
504 if ( dim
== AF_DIMENSION_HORZ
)
506 scale
= scaler
->x_scale
;
507 delta
= scaler
->x_delta
;
511 scale
= scaler
->y_scale
;
512 delta
= scaler
->y_delta
;
515 axis
= &metrics
->axis
[dim
];
517 if ( axis
->org_scale
== scale
&& axis
->org_delta
== delta
)
520 axis
->org_scale
= scale
;
521 axis
->org_delta
= delta
;
524 * correct Y scale to optimize the alignment of the top of small
525 * letters to the pixel grid
527 if ( dim
== AF_DIMENSION_VERT
)
529 AF_LatinAxis vaxis
= &metrics
->axis
[AF_DIMENSION_VERT
];
530 AF_LatinBlue blue
= NULL
;
533 for ( nn
= 0; nn
< vaxis
->blue_count
; nn
++ )
535 if ( vaxis
->blues
[nn
].flags
& AF_LATIN_BLUE_ADJUSTMENT
)
537 blue
= &vaxis
->blues
[nn
];
544 FT_Pos scaled
= FT_MulFix( blue
->shoot
.org
, scaler
->y_scale
);
545 FT_Pos fitted
= ( scaled
+ 40 ) & ~63;
548 if ( scaled
!= fitted
) {
549 scale
= FT_MulDiv( scale
, fitted
, scaled
);
550 AF_LOG(( "== scaled x-top = %.2g fitted = %.2g, scaling = %.4g\n", scaled
/64.0, fitted
/64.0, (fitted
*1.0)/scaled
));
559 if ( dim
== AF_DIMENSION_HORZ
)
561 metrics
->root
.scaler
.x_scale
= scale
;
562 metrics
->root
.scaler
.x_delta
= delta
;
566 metrics
->root
.scaler
.y_scale
= scale
;
567 metrics
->root
.scaler
.y_delta
= delta
;
570 /* scale the standard widths */
571 for ( nn
= 0; nn
< axis
->width_count
; nn
++ )
573 AF_Width width
= axis
->widths
+ nn
;
576 width
->cur
= FT_MulFix( width
->org
, scale
);
577 width
->fit
= width
->cur
;
580 /* an extra-light axis corresponds to a standard width that is */
581 /* smaller than 0.75 pixels */
583 (FT_Bool
)( FT_MulFix( axis
->standard_width
, scale
) < 32 + 8 );
585 if ( dim
== AF_DIMENSION_VERT
)
587 /* scale the blue zones */
588 for ( nn
= 0; nn
< axis
->blue_count
; nn
++ )
590 AF_LatinBlue blue
= &axis
->blues
[nn
];
594 blue
->ref
.cur
= FT_MulFix( blue
->ref
.org
, scale
) + delta
;
595 blue
->ref
.fit
= blue
->ref
.cur
;
596 blue
->shoot
.cur
= FT_MulFix( blue
->shoot
.org
, scale
) + delta
;
597 blue
->shoot
.fit
= blue
->shoot
.cur
;
598 blue
->flags
&= ~AF_LATIN_BLUE_ACTIVE
;
600 /* a blue zone is only active if it is less than 3/4 pixels tall */
601 dist
= FT_MulFix( blue
->ref
.org
- blue
->shoot
.org
, scale
);
602 if ( dist
<= 48 && dist
>= -48 )
604 FT_Pos delta1
, delta2
;
606 delta1
= blue
->shoot
.org
- blue
->ref
.org
;
611 delta2
= FT_MulFix( delta2
, scale
);
615 else if ( delta2
< 64 )
616 delta2
= 32 + ( ( ( delta2
- 32 ) + 16 ) & ~31 );
618 delta2
= FT_PIX_ROUND( delta2
);
623 blue
->ref
.fit
= FT_PIX_ROUND( blue
->ref
.cur
);
624 blue
->shoot
.fit
= blue
->ref
.fit
+ delta2
;
626 AF_LOG(( ">> activating blue zone %d: ref.cur=%.2g ref.fit=%.2g shoot.cur=%.2g shoot.fit=%.2g\n",
627 nn
, blue
->ref
.cur
/64.0, blue
->ref
.fit
/64.0,
628 blue
->shoot
.cur
/64.0, blue
->shoot
.fit
/64.0 ));
630 blue
->flags
|= AF_LATIN_BLUE_ACTIVE
;
638 af_latin2_metrics_scale( AF_LatinMetrics metrics
,
641 metrics
->root
.scaler
.render_mode
= scaler
->render_mode
;
642 metrics
->root
.scaler
.face
= scaler
->face
;
644 af_latin2_metrics_scale_dim( metrics
, scaler
, AF_DIMENSION_HORZ
);
645 af_latin2_metrics_scale_dim( metrics
, scaler
, AF_DIMENSION_VERT
);
649 /*************************************************************************/
650 /*************************************************************************/
652 /***** L A T I N G L Y P H A N A L Y S I S *****/
654 /*************************************************************************/
655 /*************************************************************************/
657 #define SORT_SEGMENTS
659 FT_LOCAL_DEF( FT_Error
)
660 af_latin2_hints_compute_segments( AF_GlyphHints hints
,
663 AF_AxisHints axis
= &hints
->axis
[dim
];
664 FT_Memory memory
= hints
->memory
;
665 FT_Error error
= AF_Err_Ok
;
666 AF_Segment segment
= NULL
;
668 AF_Point
* contour
= hints
->contours
;
669 AF_Point
* contour_limit
= contour
+ hints
->num_contours
;
670 AF_Direction major_dir
, segment_dir
;
675 seg0
.flags
= AF_EDGE_NORMAL
;
677 major_dir
= (AF_Direction
)FT_ABS( axis
->major_dir
);
678 segment_dir
= major_dir
;
680 axis
->num_segments
= 0;
682 /* set up (u,v) in each point */
683 if ( dim
== AF_DIMENSION_HORZ
)
685 AF_Point point
= hints
->points
;
686 AF_Point limit
= point
+ hints
->num_points
;
689 for ( ; point
< limit
; point
++ )
691 point
->u
= point
->fx
;
692 point
->v
= point
->fy
;
697 AF_Point point
= hints
->points
;
698 AF_Point limit
= point
+ hints
->num_points
;
701 for ( ; point
< limit
; point
++ )
703 point
->u
= point
->fy
;
704 point
->v
= point
->fx
;
708 /* do each contour separately */
709 for ( ; contour
< contour_limit
; contour
++ )
711 AF_Point point
= contour
[0];
712 AF_Point start
= point
;
713 AF_Point last
= point
->prev
;
716 if ( point
== last
) /* skip singletons -- just in case */
719 /* already on an edge ?, backtrack to find its start */
720 if ( FT_ABS( point
->in_dir
) == major_dir
)
724 while ( point
->in_dir
== start
->in_dir
)
727 else /* otherwise, find first segment start, if any */
729 while ( FT_ABS( point
->out_dir
) != major_dir
)
733 if ( point
== start
)
743 FT_Pos min_u
, min_v
, max_u
, max_v
;
745 /* we're at the start of a new segment */
746 FT_ASSERT( FT_ABS( point
->out_dir
) == major_dir
&&
747 point
->in_dir
!= point
->out_dir
);
750 min_u
= max_u
= point
->u
;
751 min_v
= max_v
= point
->v
;
755 while ( point
->out_dir
== first
->out_dir
)
759 if ( point
->u
< min_u
)
762 if ( point
->u
> max_u
)
766 if ( point
->v
< min_v
)
769 if ( point
->v
> max_v
)
772 /* record new segment */
773 error
= af_axis_hints_new_segment( axis
, memory
, &segment
);
778 segment
->dir
= first
->out_dir
;
779 segment
->first
= first
;
780 segment
->last
= point
;
781 segment
->contour
= contour
;
782 segment
->pos
= (FT_Short
)(( min_u
+ max_u
) >> 1);
783 segment
->min_coord
= (FT_Short
) min_v
;
784 segment
->max_coord
= (FT_Short
) max_v
;
785 segment
->height
= (FT_Short
)(max_v
- min_v
);
787 /* a segment is round if it doesn't have successive */
788 /* on-curve points. */
791 AF_Point last
= point
;
792 AF_Flags f0
= (AF_Flags
)(pt
->flags
& AF_FLAG_CONTROL
);
796 segment
->flags
&= ~AF_EDGE_ROUND
;
798 for ( ; pt
!= last
; f0
= f1
)
801 f1
= (AF_Flags
)(pt
->flags
& AF_FLAG_CONTROL
);
807 segment
->flags
|= AF_EDGE_ROUND
;
811 /* this can happen in the case of a degenerate contour
812 * e.g. a 2-point vertical contour
814 if ( point
== start
)
817 /* jump to the start of the next segment, if any */
818 while ( FT_ABS(point
->out_dir
) != major_dir
)
822 if ( point
== start
)
831 /* now slightly increase the height of segments when this makes */
832 /* sense -- this is used to better detect and ignore serifs */
834 AF_Segment segments
= axis
->segments
;
835 AF_Segment segments_end
= segments
+ axis
->num_segments
;
838 for ( segment
= segments
; segment
< segments_end
; segment
++ )
840 AF_Point first
= segment
->first
;
841 AF_Point last
= segment
->last
;
843 FT_Pos first_v
= first
->v
;
844 FT_Pos last_v
= last
->v
;
850 if ( first_v
< last_v
)
853 if ( p
->v
< first_v
)
854 segment
->height
= (FT_Short
)( segment
->height
+
855 ( ( first_v
- p
->v
) >> 1 ) );
859 segment
->height
= (FT_Short
)( segment
->height
+
860 ( ( p
->v
- last_v
) >> 1 ) );
865 if ( p
->v
> first_v
)
866 segment
->height
= (FT_Short
)( segment
->height
+
867 ( ( p
->v
- first_v
) >> 1 ) );
871 segment
->height
= (FT_Short
)( segment
->height
+
872 ( ( last_v
- p
->v
) >> 1 ) );
877 #ifdef AF_SORT_SEGMENTS
878 /* place all segments with a negative direction to the start
879 * of the array, used to speed up segment linking later...
882 AF_Segment segments
= axis
->segments
;
883 FT_UInt count
= axis
->num_segments
;
886 for (ii
= 0; ii
< count
; ii
++)
888 if ( segments
[ii
].dir
> 0 )
890 for (jj
= ii
+1; jj
< count
; jj
++)
892 if ( segments
[jj
].dir
< 0 )
897 segments
[ii
] = segments
[jj
];
908 axis
->mid_segments
= ii
;
918 af_latin2_hints_link_segments( AF_GlyphHints hints
,
921 AF_AxisHints axis
= &hints
->axis
[dim
];
922 AF_Segment segments
= axis
->segments
;
923 AF_Segment segment_limit
= segments
+ axis
->num_segments
;
924 #ifdef AF_SORT_SEGMENTS
925 AF_Segment segment_mid
= segments
+ axis
->mid_segments
;
927 FT_Pos len_threshold
, len_score
;
928 AF_Segment seg1
, seg2
;
931 len_threshold
= AF_LATIN_CONSTANT( hints
->metrics
, 8 );
932 if ( len_threshold
== 0 )
935 len_score
= AF_LATIN_CONSTANT( hints
->metrics
, 6000 );
937 #ifdef AF_SORT_SEGMENTS
938 for ( seg1
= segments
; seg1
< segment_mid
; seg1
++ )
940 if ( seg1
->dir
!= axis
->major_dir
|| seg1
->first
== seg1
->last
)
943 for ( seg2
= segment_mid
; seg2
< segment_limit
; seg2
++ )
945 /* now compare each segment to the others */
946 for ( seg1
= segments
; seg1
< segment_limit
; seg1
++ )
948 /* the fake segments are introduced to hint the metrics -- */
949 /* we must never link them to anything */
950 if ( seg1
->dir
!= axis
->major_dir
|| seg1
->first
== seg1
->last
)
953 for ( seg2
= segments
; seg2
< segment_limit
; seg2
++ )
954 if ( seg1
->dir
+ seg2
->dir
== 0 && seg2
->pos
> seg1
->pos
)
957 FT_Pos pos1
= seg1
->pos
;
958 FT_Pos pos2
= seg2
->pos
;
959 FT_Pos dist
= pos2
- pos1
;
966 FT_Pos min
= seg1
->min_coord
;
967 FT_Pos max
= seg1
->max_coord
;
971 if ( min
< seg2
->min_coord
)
972 min
= seg2
->min_coord
;
974 if ( max
> seg2
->max_coord
)
975 max
= seg2
->max_coord
;
978 if ( len
>= len_threshold
)
980 score
= dist
+ len_score
/ len
;
981 if ( score
< seg1
->score
)
987 if ( score
< seg2
->score
)
1000 /* now, compute the `serif' segments */
1001 for ( seg1
= segments
; seg1
< segment_limit
; seg1
++ )
1007 if ( seg2
->link
!= seg1
)
1010 seg1
->serif
= seg2
->link
;
1017 FT_LOCAL_DEF( FT_Error
)
1018 af_latin2_hints_compute_edges( AF_GlyphHints hints
,
1021 AF_AxisHints axis
= &hints
->axis
[dim
];
1022 FT_Error error
= AF_Err_Ok
;
1023 FT_Memory memory
= hints
->memory
;
1024 AF_LatinAxis laxis
= &((AF_LatinMetrics
)hints
->metrics
)->axis
[dim
];
1026 AF_Segment segments
= axis
->segments
;
1027 AF_Segment segment_limit
= segments
+ axis
->num_segments
;
1030 AF_Direction up_dir
;
1032 FT_Pos edge_distance_threshold
;
1033 FT_Pos segment_length_threshold
;
1036 axis
->num_edges
= 0;
1038 scale
= ( dim
== AF_DIMENSION_HORZ
) ? hints
->x_scale
1041 up_dir
= ( dim
== AF_DIMENSION_HORZ
) ? AF_DIR_UP
1045 * We want to ignore very small (mostly serif) segments, we do that
1046 * by ignoring those that whose length is less than a given fraction
1047 * of the standard width. If there is no standard width, we ignore
1048 * those that are less than a given size in pixels
1050 * also, unlink serif segments that are linked to segments farther
1051 * than 50% of the standard width
1053 if ( dim
== AF_DIMENSION_HORZ
)
1055 if ( laxis
->width_count
> 0 )
1056 segment_length_threshold
= (laxis
->standard_width
* 10 ) >> 4;
1058 segment_length_threshold
= FT_DivFix( 64, hints
->y_scale
);
1061 segment_length_threshold
= 0;
1063 /*********************************************************************/
1065 /* We will begin by generating a sorted table of edges for the */
1066 /* current direction. To do so, we simply scan each segment and try */
1067 /* to find an edge in our table that corresponds to its position. */
1069 /* If no edge is found, we create and insert a new edge in the */
1070 /* sorted table. Otherwise, we simply add the segment to the edge's */
1071 /* list which will be processed in the second step to compute the */
1072 /* edge's properties. */
1074 /* Note that the edges table is sorted along the segment/edge */
1077 /*********************************************************************/
1079 edge_distance_threshold
= FT_MulFix( laxis
->edge_distance_threshold
,
1081 if ( edge_distance_threshold
> 64 / 4 )
1082 edge_distance_threshold
= 64 / 4;
1084 edge_distance_threshold
= FT_DivFix( edge_distance_threshold
,
1087 for ( seg
= segments
; seg
< segment_limit
; seg
++ )
1093 if ( seg
->height
< segment_length_threshold
)
1096 /* A special case for serif edges: If they are smaller than */
1097 /* 1.5 pixels we ignore them. */
1100 FT_Pos dist
= seg
->serif
->pos
- seg
->pos
;
1105 if (dist
>= laxis
->standard_width
>> 1)
1107 /* unlink this serif, it is too distant from its reference stem */
1110 else if ( 2*seg
->height
< 3 * segment_length_threshold
)
1114 /* look for an edge corresponding to the segment */
1115 for ( ee
= 0; ee
< axis
->num_edges
; ee
++ )
1117 AF_Edge edge
= axis
->edges
+ ee
;
1121 dist
= seg
->pos
- edge
->fpos
;
1125 if ( dist
< edge_distance_threshold
&& edge
->dir
== seg
->dir
)
1137 /* insert a new edge in the list and */
1138 /* sort according to the position */
1139 error
= af_axis_hints_new_edge( axis
, seg
->pos
, seg
->dir
, memory
, &edge
);
1143 /* add the segment to the new edge's list */
1148 edge
->fpos
= seg
->pos
;
1149 edge
->dir
= seg
->dir
;
1150 edge
->opos
= edge
->pos
= FT_MulFix( seg
->pos
, scale
);
1151 seg
->edge_next
= seg
;
1155 /* if an edge was found, simply add the segment to the edge's */
1157 seg
->edge_next
= found
->first
;
1158 found
->last
->edge_next
= seg
;
1164 /*********************************************************************/
1166 /* Good, we will now compute each edge's properties according to */
1167 /* segments found on its position. Basically, these are: */
1169 /* - edge's main direction */
1170 /* - stem edge, serif edge or both (which defaults to stem then) */
1171 /* - rounded edge, straight or both (which defaults to straight) */
1172 /* - link for edge */
1174 /*********************************************************************/
1176 /* first of all, set the `edge' field in each segment -- this is */
1177 /* required in order to compute edge links */
1180 * Note that removing this loop and setting the `edge' field of each
1181 * segment directly in the code above slows down execution speed for
1182 * some reasons on platforms like the Sun.
1185 AF_Edge edges
= axis
->edges
;
1186 AF_Edge edge_limit
= edges
+ axis
->num_edges
;
1190 for ( edge
= edges
; edge
< edge_limit
; edge
++ )
1197 seg
= seg
->edge_next
;
1199 } while ( seg
!= edge
->first
);
1202 /* now, compute each edge properties */
1203 for ( edge
= edges
; edge
< edge_limit
; edge
++ )
1205 FT_Int is_round
= 0; /* does it contain round segments? */
1206 FT_Int is_straight
= 0; /* does it contain straight segments? */
1207 FT_Pos ups
= 0; /* number of upwards segments */
1208 FT_Pos downs
= 0; /* number of downwards segments */
1218 /* check for roundness of segment */
1219 if ( seg
->flags
& AF_EDGE_ROUND
)
1224 /* check for segment direction */
1225 if ( seg
->dir
== up_dir
)
1226 ups
+= seg
->max_coord
-seg
->min_coord
;
1228 downs
+= seg
->max_coord
-seg
->min_coord
;
1230 /* check for links -- if seg->serif is set, then seg->link must */
1232 is_serif
= (FT_Bool
)( seg
->serif
&&
1234 seg
->serif
->edge
!= edge
);
1236 if ( ( seg
->link
&& seg
->link
->edge
!= NULL
) || is_serif
)
1248 edge2
= edge
->serif
;
1257 edge_delta
= edge
->fpos
- edge2
->fpos
;
1258 if ( edge_delta
< 0 )
1259 edge_delta
= -edge_delta
;
1261 seg_delta
= seg
->pos
- seg2
->pos
;
1262 if ( seg_delta
< 0 )
1263 seg_delta
= -seg_delta
;
1265 if ( seg_delta
< edge_delta
)
1273 edge
->serif
= edge2
;
1274 edge2
->flags
|= AF_EDGE_SERIF
;
1280 seg
= seg
->edge_next
;
1282 } while ( seg
!= edge
->first
);
1284 /* set the round/straight flags */
1285 edge
->flags
= AF_EDGE_NORMAL
;
1287 if ( is_round
> 0 && is_round
>= is_straight
)
1288 edge
->flags
|= AF_EDGE_ROUND
;
1291 /* set the edge's main direction */
1292 edge
->dir
= AF_DIR_NONE
;
1295 edge
->dir
= (FT_Char
)up_dir
;
1297 else if ( ups
< downs
)
1298 edge
->dir
= (FT_Char
)-up_dir
;
1300 else if ( ups
== downs
)
1301 edge
->dir
= 0; /* both up and down! */
1304 /* gets rid of serifs if link is set */
1305 /* XXX: This gets rid of many unpleasant artefacts! */
1306 /* Example: the `c' in cour.pfa at size 13 */
1308 if ( edge
->serif
&& edge
->link
)
1318 FT_LOCAL_DEF( FT_Error
)
1319 af_latin2_hints_detect_features( AF_GlyphHints hints
,
1325 error
= af_latin2_hints_compute_segments( hints
, dim
);
1328 af_latin2_hints_link_segments( hints
, dim
);
1330 error
= af_latin2_hints_compute_edges( hints
, dim
);
1336 FT_LOCAL_DEF( void )
1337 af_latin2_hints_compute_blue_edges( AF_GlyphHints hints
,
1338 AF_LatinMetrics metrics
)
1340 AF_AxisHints axis
= &hints
->axis
[ AF_DIMENSION_VERT
];
1341 AF_Edge edge
= axis
->edges
;
1342 AF_Edge edge_limit
= edge
+ axis
->num_edges
;
1343 AF_LatinAxis latin
= &metrics
->axis
[ AF_DIMENSION_VERT
];
1344 FT_Fixed scale
= latin
->scale
;
1345 FT_Pos best_dist0
; /* initial threshold */
1348 /* compute the initial threshold as a fraction of the EM size */
1349 best_dist0
= FT_MulFix( metrics
->units_per_em
/ 40, scale
);
1351 if ( best_dist0
> 64 / 2 )
1352 best_dist0
= 64 / 2;
1354 /* compute which blue zones are active, i.e. have their scaled */
1355 /* size < 3/4 pixels */
1357 /* for each horizontal edge search the blue zone which is closest */
1358 for ( ; edge
< edge_limit
; edge
++ )
1361 AF_Width best_blue
= NULL
;
1362 FT_Pos best_dist
= best_dist0
;
1364 for ( bb
= 0; bb
< AF_LATIN_BLUE_MAX
; bb
++ )
1366 AF_LatinBlue blue
= latin
->blues
+ bb
;
1367 FT_Bool is_top_blue
, is_major_dir
;
1370 /* skip inactive blue zones (i.e., those that are too small) */
1371 if ( !( blue
->flags
& AF_LATIN_BLUE_ACTIVE
) )
1374 /* if it is a top zone, check for right edges -- if it is a bottom */
1375 /* zone, check for left edges */
1377 /* of course, that's for TrueType */
1378 is_top_blue
= (FT_Byte
)( ( blue
->flags
& AF_LATIN_BLUE_TOP
) != 0 );
1379 is_major_dir
= FT_BOOL( edge
->dir
== axis
->major_dir
);
1381 /* if it is a top zone, the edge must be against the major */
1382 /* direction; if it is a bottom zone, it must be in the major */
1384 if ( is_top_blue
^ is_major_dir
)
1390 /* if it's a rounded edge, compare it to the overshoot position */
1391 /* if it's a flat edge, compare it to the reference position */
1392 if ( edge
->flags
& AF_EDGE_ROUND
)
1393 compare
= &blue
->shoot
;
1395 compare
= &blue
->ref
;
1397 dist
= edge
->fpos
- compare
->org
;
1401 dist
= FT_MulFix( dist
, scale
);
1402 if ( dist
< best_dist
)
1405 best_blue
= compare
;
1409 /* now, compare it to the overshoot position if the edge is */
1410 /* rounded, and if the edge is over the reference position of a */
1411 /* top zone, or under the reference position of a bottom zone */
1412 if ( edge
->flags
& AF_EDGE_ROUND
&& dist
!= 0 )
1414 FT_Bool is_under_ref
= FT_BOOL( edge
->fpos
< blue
->ref
.org
);
1417 if ( is_top_blue
^ is_under_ref
)
1419 blue
= latin
->blues
+ bb
;
1420 dist
= edge
->fpos
- blue
->shoot
.org
;
1424 dist
= FT_MulFix( dist
, scale
);
1425 if ( dist
< best_dist
)
1428 best_blue
= & blue
->shoot
;
1437 edge
->blue_edge
= best_blue
;
1443 af_latin2_hints_init( AF_GlyphHints hints
,
1444 AF_LatinMetrics metrics
)
1446 FT_Render_Mode mode
;
1447 FT_UInt32 scaler_flags
, other_flags
;
1448 FT_Face face
= metrics
->root
.scaler
.face
;
1451 af_glyph_hints_rescale( hints
, (AF_ScriptMetrics
)metrics
);
1454 * correct x_scale and y_scale if needed, since they may have
1455 * been modified `af_latin2_metrics_scale_dim' above
1457 hints
->x_scale
= metrics
->axis
[AF_DIMENSION_HORZ
].scale
;
1458 hints
->x_delta
= metrics
->axis
[AF_DIMENSION_HORZ
].delta
;
1459 hints
->y_scale
= metrics
->axis
[AF_DIMENSION_VERT
].scale
;
1460 hints
->y_delta
= metrics
->axis
[AF_DIMENSION_VERT
].delta
;
1462 /* compute flags depending on render mode, etc. */
1463 mode
= metrics
->root
.scaler
.render_mode
;
1465 #if 0 /* #ifdef AF_USE_WARPER */
1466 if ( mode
== FT_RENDER_MODE_LCD
|| mode
== FT_RENDER_MODE_LCD_V
)
1468 metrics
->root
.scaler
.render_mode
= mode
= FT_RENDER_MODE_NORMAL
;
1472 scaler_flags
= hints
->scaler_flags
;
1476 * We snap the width of vertical stems for the monochrome and
1477 * horizontal LCD rendering targets only.
1479 if ( mode
== FT_RENDER_MODE_MONO
|| mode
== FT_RENDER_MODE_LCD
)
1480 other_flags
|= AF_LATIN_HINTS_HORZ_SNAP
;
1483 * We snap the width of horizontal stems for the monochrome and
1484 * vertical LCD rendering targets only.
1486 if ( mode
== FT_RENDER_MODE_MONO
|| mode
== FT_RENDER_MODE_LCD_V
)
1487 other_flags
|= AF_LATIN_HINTS_VERT_SNAP
;
1490 * We adjust stems to full pixels only if we don't use the `light' mode.
1492 if ( mode
!= FT_RENDER_MODE_LIGHT
)
1493 other_flags
|= AF_LATIN_HINTS_STEM_ADJUST
;
1495 if ( mode
== FT_RENDER_MODE_MONO
)
1496 other_flags
|= AF_LATIN_HINTS_MONO
;
1499 * In `light' hinting mode we disable horizontal hinting completely.
1500 * We also do it if the face is italic.
1502 if ( mode
== FT_RENDER_MODE_LIGHT
||
1503 (face
->style_flags
& FT_STYLE_FLAG_ITALIC
) != 0 )
1504 scaler_flags
|= AF_SCALER_FLAG_NO_HORIZONTAL
;
1506 hints
->scaler_flags
= scaler_flags
;
1507 hints
->other_flags
= other_flags
;
1513 /*************************************************************************/
1514 /*************************************************************************/
1516 /***** L A T I N G L Y P H G R I D - F I T T I N G *****/
1518 /*************************************************************************/
1519 /*************************************************************************/
1521 /* snap a given width in scaled coordinates to one of the */
1522 /* current standard widths */
1525 af_latin2_snap_width( AF_Width widths
,
1530 FT_Pos best
= 64 + 32 + 2;
1531 FT_Pos reference
= width
;
1535 for ( n
= 0; n
< count
; n
++ )
1552 scaled
= FT_PIX_ROUND( reference
);
1554 if ( width
>= reference
)
1556 if ( width
< scaled
+ 48 )
1561 if ( width
> scaled
- 48 )
1569 /* compute the snapped width of a given stem */
1572 af_latin2_compute_stem_width( AF_GlyphHints hints
,
1575 AF_Edge_Flags base_flags
,
1576 AF_Edge_Flags stem_flags
)
1578 AF_LatinMetrics metrics
= (AF_LatinMetrics
) hints
->metrics
;
1579 AF_LatinAxis axis
= & metrics
->axis
[dim
];
1580 FT_Pos dist
= width
;
1582 FT_Int vertical
= ( dim
== AF_DIMENSION_VERT
);
1585 FT_UNUSED(base_flags
);
1587 if ( !AF_LATIN_HINTS_DO_STEM_ADJUST( hints
) ||
1597 if ( ( vertical
&& !AF_LATIN_HINTS_DO_VERT_SNAP( hints
) ) ||
1598 ( !vertical
&& !AF_LATIN_HINTS_DO_HORZ_SNAP( hints
) ) )
1600 /* smooth hinting process: very lightly quantize the stem width */
1602 /* leave the widths of serifs alone */
1604 if ( ( stem_flags
& AF_EDGE_SERIF
) && vertical
&& ( dist
< 3 * 64 ) )
1608 else if ( ( base_flags
& AF_EDGE_ROUND
) )
1613 else if ( dist
< 56 )
1616 if ( axis
->width_count
> 0 )
1621 /* compare to standard width */
1622 if ( axis
->width_count
> 0 )
1624 delta
= dist
- axis
->widths
[0].cur
;
1631 dist
= axis
->widths
[0].cur
;
1639 if ( dist
< 3 * 64 )
1647 else if ( delta
< 32 )
1650 else if ( delta
< 54 )
1657 dist
= ( dist
+ 32 ) & ~63;
1662 /* strong hinting process: snap the stem width to integer pixels */
1663 FT_Pos org_dist
= dist
;
1666 dist
= af_latin2_snap_width( axis
->widths
, axis
->width_count
, dist
);
1670 /* in the case of vertical hinting, always round */
1671 /* the stem heights to integer pixels */
1674 dist
= ( dist
+ 16 ) & ~63;
1680 if ( AF_LATIN_HINTS_DO_MONO( hints
) )
1682 /* monochrome horizontal hinting: snap widths to integer pixels */
1683 /* with a different threshold */
1688 dist
= ( dist
+ 32 ) & ~63;
1692 /* for horizontal anti-aliased hinting, we adopt a more subtle */
1693 /* approach: we strengthen small stems, round stems whose size */
1694 /* is between 1 and 2 pixels to an integer, otherwise nothing */
1697 dist
= ( dist
+ 64 ) >> 1;
1699 else if ( dist
< 128 )
1701 /* We only round to an integer width if the corresponding */
1702 /* distortion is less than 1/4 pixel. Otherwise this */
1703 /* makes everything worse since the diagonals, which are */
1704 /* not hinted, appear a lot bolder or thinner than the */
1705 /* vertical stems. */
1710 dist
= ( dist
+ 22 ) & ~63;
1711 delta
= dist
- org_dist
;
1719 dist
= ( dist
+ 64 ) >> 1;
1723 /* round otherwise to prevent color fringes in LCD mode */
1724 dist
= ( dist
+ 32 ) & ~63;
1737 /* align one stem edge relative to the previous stem edge */
1740 af_latin2_align_linked_edge( AF_GlyphHints hints
,
1745 FT_Pos dist
= stem_edge
->opos
- base_edge
->opos
;
1747 FT_Pos fitted_width
= af_latin2_compute_stem_width(
1749 (AF_Edge_Flags
)base_edge
->flags
,
1750 (AF_Edge_Flags
)stem_edge
->flags
);
1753 stem_edge
->pos
= base_edge
->pos
+ fitted_width
;
1755 AF_LOG(( "LINK: edge %d (opos=%.2f) linked to (%.2f), "
1756 "dist was %.2f, now %.2f\n",
1757 stem_edge
-hints
->axis
[dim
].edges
, stem_edge
->opos
/ 64.0,
1758 stem_edge
->pos
/ 64.0, dist
/ 64.0, fitted_width
/ 64.0 ));
1763 af_latin2_align_serif_edge( AF_GlyphHints hints
,
1769 serif
->pos
= base
->pos
+ (serif
->opos
- base
->opos
);
1773 /*************************************************************************/
1774 /*************************************************************************/
1775 /*************************************************************************/
1777 /**** E D G E H I N T I N G ****/
1779 /*************************************************************************/
1780 /*************************************************************************/
1781 /*************************************************************************/
1784 FT_LOCAL_DEF( void )
1785 af_latin2_hint_edges( AF_GlyphHints hints
,
1788 AF_AxisHints axis
= &hints
->axis
[dim
];
1789 AF_Edge edges
= axis
->edges
;
1790 AF_Edge edge_limit
= edges
+ axis
->num_edges
;
1793 FT_Int has_serifs
= 0;
1794 FT_Pos anchor_drift
= 0;
1798 AF_LOG(( "==== hinting %s edges =====\n", dim
== AF_DIMENSION_HORZ
? "vertical" : "horizontal" ));
1800 /* we begin by aligning all stems relative to the blue zone */
1801 /* if needed -- that's only for horizontal edges */
1803 if ( dim
== AF_DIMENSION_VERT
&& AF_HINTS_DO_BLUES( hints
) )
1805 for ( edge
= edges
; edge
< edge_limit
; edge
++ )
1808 AF_Edge edge1
, edge2
;
1811 if ( edge
->flags
& AF_EDGE_DONE
)
1814 blue
= edge
->blue_edge
;
1822 else if ( edge2
&& edge2
->blue_edge
)
1824 blue
= edge2
->blue_edge
;
1832 AF_LOG(( "BLUE: edge %d (opos=%.2f) snapped to (%.2f), "
1834 edge1
-edges
, edge1
->opos
/ 64.0, blue
->fit
/ 64.0,
1835 edge1
->pos
/ 64.0 ));
1837 edge1
->pos
= blue
->fit
;
1838 edge1
->flags
|= AF_EDGE_DONE
;
1840 if ( edge2
&& !edge2
->blue_edge
)
1842 af_latin2_align_linked_edge( hints
, dim
, edge1
, edge2
);
1843 edge2
->flags
|= AF_EDGE_DONE
;
1850 anchor_drift
= (anchor
->pos
- anchor
->opos
);
1852 anchor_drift
= (anchor_drift
+ (edge2
->pos
- edge2
->opos
)) >> 1;
1857 /* now we will align all stem edges, trying to maintain the */
1858 /* relative order of stems in the glyph */
1859 for ( edge
= edges
; edge
< edge_limit
; edge
++ )
1864 if ( edge
->flags
& AF_EDGE_DONE
)
1867 /* skip all non-stem edges */
1875 /* now align the stem */
1877 /* this should not happen, but it's better to be safe */
1878 if ( edge2
->blue_edge
)
1880 AF_LOG(( "ASSERTION FAILED for edge %d\n", edge2
-edges
));
1882 af_latin2_align_linked_edge( hints
, dim
, edge2
, edge
);
1883 edge
->flags
|= AF_EDGE_DONE
;
1889 FT_Pos org_len
, org_center
, cur_len
;
1890 FT_Pos cur_pos1
, error1
, error2
, u_off
, d_off
;
1893 org_len
= edge2
->opos
- edge
->opos
;
1894 cur_len
= af_latin2_compute_stem_width(
1895 hints
, dim
, org_len
,
1896 (AF_Edge_Flags
)edge
->flags
,
1897 (AF_Edge_Flags
)edge2
->flags
);
1898 if ( cur_len
<= 64 )
1908 org_center
= edge
->opos
+ ( org_len
>> 1 );
1910 cur_pos1
= FT_PIX_ROUND( org_center
);
1912 error1
= org_center
- ( cur_pos1
- u_off
);
1916 error2
= org_center
- ( cur_pos1
+ d_off
);
1920 if ( error1
< error2
)
1925 edge
->pos
= cur_pos1
- cur_len
/ 2;
1926 edge2
->pos
= edge
->pos
+ cur_len
;
1929 edge
->pos
= FT_PIX_ROUND( edge
->opos
);
1931 AF_LOG(( "ANCHOR: edge %d (opos=%.2f) and %d (opos=%.2f) "
1932 "snapped to (%.2f) (%.2f)\n",
1933 edge
-edges
, edge
->opos
/ 64.0,
1934 edge2
-edges
, edge2
->opos
/ 64.0,
1935 edge
->pos
/ 64.0, edge2
->pos
/ 64.0 ));
1938 edge
->flags
|= AF_EDGE_DONE
;
1940 af_latin2_align_linked_edge( hints
, dim
, edge
, edge2
);
1942 edge2
->flags
|= AF_EDGE_DONE
;
1944 anchor_drift
= ( (anchor
->pos
- anchor
->opos
) +
1945 (edge2
->pos
- edge2
->opos
)) >> 1;
1947 AF_LOG(( "DRIFT: %.2f\n", anchor_drift
/64.0 ));
1951 FT_Pos org_pos
, org_len
, org_center
, cur_center
, cur_len
;
1952 FT_Pos org_left
, org_right
;
1955 org_pos
= edge
->opos
+ anchor_drift
;
1956 org_len
= edge2
->opos
- edge
->opos
;
1957 org_center
= org_pos
+ ( org_len
>> 1 );
1959 cur_len
= af_latin2_compute_stem_width(
1960 hints
, dim
, org_len
,
1961 (AF_Edge_Flags
)edge
->flags
,
1962 (AF_Edge_Flags
)edge2
->flags
);
1964 org_left
= org_pos
+ ((org_len
- cur_len
) >> 1);
1965 org_right
= org_pos
+ ((org_len
+ cur_len
) >> 1);
1967 AF_LOG(( "ALIGN: left=%.2f right=%.2f ", org_left
/64.0, org_right
/64.0 ));
1968 cur_center
= org_center
;
1970 if ( edge2
->flags
& AF_EDGE_DONE
)
1973 edge
->pos
= edge2
->pos
- cur_len
;
1977 /* we want to compare several displacement, and choose
1978 * the one that increases fitness while minimizing
1979 * distortion as well
1981 FT_Pos displacements
[6], scores
[6], org
, fit
, delta
;
1984 /* note: don't even try to fit tiny stems */
1987 AF_LOG(( "tiny stem\n" ));
1991 /* if the span is within a single pixel, don't touch it */
1992 if ( FT_PIX_FLOOR(org_left
) == FT_PIX_CEIL(org_right
) )
1994 AF_LOG(( "single pixel stem\n" ));
2000 /* we want to avoid the absolute worst case which is
2001 * when the left and right edges of the span each represent
2002 * about 50% of the gray. we'd better want to change this
2003 * to 25/75%, since this is much more pleasant to the eye with
2004 * very acceptable distortion
2006 FT_Pos frac_left
= (org_left
) & 63;
2007 FT_Pos frac_right
= (org_right
) & 63;
2009 if ( frac_left
>= 22 && frac_left
<= 42 &&
2010 frac_right
>= 22 && frac_right
<= 42 )
2013 fit
= (org
<= 32) ? 16 : 48;
2014 delta
= FT_ABS(fit
- org
);
2015 displacements
[count
] = fit
- org
;
2016 scores
[count
++] = delta
;
2017 AF_LOG(( "dispA=%.2f (%d) ", (fit
- org
)/64.0, delta
));
2020 fit
= (org
<= 32) ? 16 : 48;
2021 delta
= FT_ABS(fit
- org
);
2022 displacements
[count
] = fit
- org
;
2023 scores
[count
++] = delta
;
2024 AF_LOG(( "dispB=%.2f (%d) ", (fit
- org
)/64.0, delta
));
2028 /* snapping the left edge to the grid */
2030 fit
= FT_PIX_ROUND(org
);
2031 delta
= FT_ABS(fit
- org
);
2032 displacements
[count
] = fit
- org
;
2033 scores
[count
++] = delta
;
2034 AF_LOG(( "dispC=%.2f (%d) ", (fit
- org
)/64.0, delta
));
2036 /* snapping the right edge to the grid */
2038 fit
= FT_PIX_ROUND(org
);
2039 delta
= FT_ABS(fit
- org
);
2040 displacements
[count
] = fit
- org
;
2041 scores
[count
++] = delta
;
2042 AF_LOG(( "dispD=%.2f (%d) ", (fit
- org
)/64.0, delta
));
2044 /* now find the best displacement */
2046 FT_Pos best_score
= scores
[0];
2047 FT_Pos best_disp
= displacements
[0];
2050 for (nn
= 1; nn
< count
; nn
++)
2052 if (scores
[nn
] < best_score
)
2054 best_score
= scores
[nn
];
2055 best_disp
= displacements
[nn
];
2059 cur_center
= org_center
+ best_disp
;
2065 edge
->pos
= cur_center
- (cur_len
>> 1);
2066 edge2
->pos
= edge
->pos
+ cur_len
;
2068 AF_LOG(( "STEM1: %d (opos=%.2f) to %d (opos=%.2f) "
2069 "snapped to (%.2f) and (%.2f), org_len = %.2f cur_len=%.2f\n",
2070 edge
-edges
, edge
->opos
/ 64.0,
2071 edge2
-edges
, edge2
->opos
/ 64.0,
2072 edge
->pos
/ 64.0, edge2
->pos
/ 64.0,
2073 org_len
/ 64.0, cur_len
/ 64.0 ));
2075 edge
->flags
|= AF_EDGE_DONE
;
2076 edge2
->flags
|= AF_EDGE_DONE
;
2078 if ( edge
> edges
&& edge
->pos
< edge
[-1].pos
)
2080 AF_LOG(( "BOUND: %d (pos=%.2f) to (%.2f)\n",
2081 edge
-edges
, edge
->pos
/ 64.0, edge
[-1].pos
/ 64.0 ));
2082 edge
->pos
= edge
[-1].pos
;
2087 /* make sure that lowercase m's maintain their symmetry */
2089 /* In general, lowercase m's have six vertical edges if they are sans */
2090 /* serif, or twelve if they are with serifs. This implementation is */
2091 /* based on that assumption, and seems to work very well with most */
2092 /* faces. However, if for a certain face this assumption is not */
2093 /* true, the m is just rendered like before. In addition, any stem */
2094 /* correction will only be applied to symmetrical glyphs (even if the */
2095 /* glyph is not an m), so the potential for unwanted distortion is */
2096 /* relatively low. */
2098 /* We don't handle horizontal edges since we can't easily assure that */
2099 /* the third (lowest) stem aligns with the base line; it might end up */
2100 /* one pixel higher or lower. */
2104 FT_Int n_edges
= edge_limit
- edges
;
2107 if ( dim
== AF_DIMENSION_HORZ
&& ( n_edges
== 6 || n_edges
== 12 ) )
2109 AF_Edge edge1
, edge2
, edge3
;
2110 FT_Pos dist1
, dist2
, span
, delta
;
2126 dist1
= edge2
->opos
- edge1
->opos
;
2127 dist2
= edge3
->opos
- edge2
->opos
;
2129 span
= dist1
- dist2
;
2135 delta
= edge3
->pos
- ( 2 * edge2
->pos
- edge1
->pos
);
2136 edge3
->pos
-= delta
;
2138 edge3
->link
->pos
-= delta
;
2140 /* move the serifs along with the stem */
2141 if ( n_edges
== 12 )
2143 ( edges
+ 8 )->pos
-= delta
;
2144 ( edges
+ 11 )->pos
-= delta
;
2147 edge3
->flags
|= AF_EDGE_DONE
;
2149 edge3
->link
->flags
|= AF_EDGE_DONE
;
2155 if ( has_serifs
|| !anchor
)
2158 * now hint the remaining edges (serifs and single) in order
2159 * to complete our processing
2161 for ( edge
= edges
; edge
< edge_limit
; edge
++ )
2166 if ( edge
->flags
& AF_EDGE_DONE
)
2173 delta
= edge
->serif
->opos
- edge
->opos
;
2178 if ( delta
< 64 + 16 )
2180 af_latin2_align_serif_edge( hints
, edge
->serif
, edge
);
2181 AF_LOG(( "SERIF: edge %d (opos=%.2f) serif to %d (opos=%.2f) "
2182 "aligned to (%.2f)\n",
2183 edge
-edges
, edge
->opos
/ 64.0,
2184 edge
->serif
- edges
, edge
->serif
->opos
/ 64.0,
2185 edge
->pos
/ 64.0 ));
2189 AF_LOG(( "SERIF_ANCHOR: edge %d (opos=%.2f) snapped to (%.2f)\n",
2190 edge
-edges
, edge
->opos
/ 64.0, edge
->pos
/ 64.0 ));
2191 edge
->pos
= FT_PIX_ROUND( edge
->opos
);
2196 AF_Edge before
, after
;
2199 for ( before
= edge
- 1; before
>= edges
; before
-- )
2200 if ( before
->flags
& AF_EDGE_DONE
)
2203 for ( after
= edge
+ 1; after
< edge_limit
; after
++ )
2204 if ( after
->flags
& AF_EDGE_DONE
)
2207 if ( before
>= edges
&& before
< edge
&&
2208 after
< edge_limit
&& after
> edge
)
2210 if ( after
->opos
== before
->opos
)
2211 edge
->pos
= before
->pos
;
2213 edge
->pos
= before
->pos
+
2214 FT_MulDiv( edge
->opos
- before
->opos
,
2215 after
->pos
- before
->pos
,
2216 after
->opos
- before
->opos
);
2217 AF_LOG(( "SERIF_LINK1: edge %d (opos=%.2f) snapped to (%.2f) from %d (opos=%.2f)\n",
2218 edge
-edges
, edge
->opos
/ 64.0, edge
->pos
/ 64.0, before
- edges
, before
->opos
/ 64.0 ));
2222 edge
->pos
= anchor
->pos
+ (( edge
->opos
- anchor
->opos
+ 16) & ~31);
2224 AF_LOG(( "SERIF_LINK2: edge %d (opos=%.2f) snapped to (%.2f)\n",
2225 edge
-edges
, edge
->opos
/ 64.0, edge
->pos
/ 64.0 ));
2229 edge
->flags
|= AF_EDGE_DONE
;
2231 if ( edge
> edges
&& edge
->pos
< edge
[-1].pos
)
2232 edge
->pos
= edge
[-1].pos
;
2234 if ( edge
+ 1 < edge_limit
&&
2235 edge
[1].flags
& AF_EDGE_DONE
&&
2236 edge
->pos
> edge
[1].pos
)
2237 edge
->pos
= edge
[1].pos
;
2244 af_latin2_hints_apply( AF_GlyphHints hints
,
2245 FT_Outline
* outline
,
2246 AF_LatinMetrics metrics
)
2252 error
= af_glyph_hints_reload( hints
, outline
, 1 );
2256 /* analyze glyph outline */
2257 #ifdef AF_USE_WARPER
2258 if ( metrics
->root
.scaler
.render_mode
== FT_RENDER_MODE_LIGHT
||
2259 AF_HINTS_DO_HORIZONTAL( hints
) )
2261 if ( AF_HINTS_DO_HORIZONTAL( hints
) )
2264 error
= af_latin2_hints_detect_features( hints
, AF_DIMENSION_HORZ
);
2269 if ( AF_HINTS_DO_VERTICAL( hints
) )
2271 error
= af_latin2_hints_detect_features( hints
, AF_DIMENSION_VERT
);
2275 af_latin2_hints_compute_blue_edges( hints
, metrics
);
2278 /* grid-fit the outline */
2279 for ( dim
= 0; dim
< AF_DIMENSION_MAX
; dim
++ )
2281 #ifdef AF_USE_WARPER
2282 if ( ( dim
== AF_DIMENSION_HORZ
&&
2283 metrics
->root
.scaler
.render_mode
== FT_RENDER_MODE_LIGHT
) )
2285 AF_WarperRec warper
;
2290 af_warper_compute( &warper
, hints
, dim
, &scale
, &delta
);
2291 af_glyph_hints_scale_dim( hints
, dim
, scale
, delta
);
2296 if ( ( dim
== AF_DIMENSION_HORZ
&& AF_HINTS_DO_HORIZONTAL( hints
) ) ||
2297 ( dim
== AF_DIMENSION_VERT
&& AF_HINTS_DO_VERTICAL( hints
) ) )
2299 af_latin2_hint_edges( hints
, (AF_Dimension
)dim
);
2300 af_glyph_hints_align_edge_points( hints
, (AF_Dimension
)dim
);
2301 af_glyph_hints_align_strong_points( hints
, (AF_Dimension
)dim
);
2302 af_glyph_hints_align_weak_points( hints
, (AF_Dimension
)dim
);
2305 af_glyph_hints_save( hints
, outline
);
2312 /*************************************************************************/
2313 /*************************************************************************/
2315 /***** L A T I N S C R I P T C L A S S *****/
2317 /*************************************************************************/
2318 /*************************************************************************/
2321 static const AF_Script_UniRangeRec af_latin2_uniranges
[] =
2323 AF_UNIRANGE_REC( 32UL, 127UL ), /* XXX: TODO: Add new Unicode ranges here! */
2324 AF_UNIRANGE_REC( 160UL, 255UL ),
2325 AF_UNIRANGE_REC( 0UL, 0UL )
2329 AF_DEFINE_SCRIPT_CLASS(af_latin2_script_class
,
2331 af_latin2_uniranges
,
2333 sizeof( AF_LatinMetricsRec
),
2335 (AF_Script_InitMetricsFunc
) af_latin2_metrics_init
,
2336 (AF_Script_ScaleMetricsFunc
)af_latin2_metrics_scale
,
2337 (AF_Script_DoneMetricsFunc
) NULL
,
2339 (AF_Script_InitHintsFunc
) af_latin2_hints_init
,
2340 (AF_Script_ApplyHintsFunc
) af_latin2_hints_apply