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 /***************************************************************************/
20 #include FT_ADVANCES_H
31 /*************************************************************************/
32 /*************************************************************************/
34 /***** L A T I N G L O B A L M E T R I C S *****/
36 /*************************************************************************/
37 /*************************************************************************/
40 af_latin_metrics_init_widths( AF_LatinMetrics metrics
,
44 /* scan the array of segments in each direction */
45 AF_GlyphHintsRec hints
[1];
48 af_glyph_hints_init( hints
, face
->memory
);
50 metrics
->axis
[AF_DIMENSION_HORZ
].width_count
= 0;
51 metrics
->axis
[AF_DIMENSION_VERT
].width_count
= 0;
57 AF_LatinMetricsRec dummy
[1];
58 AF_Scaler scaler
= &dummy
->root
.scaler
;
61 glyph_index
= FT_Get_Char_Index( face
, charcode
);
62 if ( glyph_index
== 0 )
65 error
= FT_Load_Glyph( face
, glyph_index
, FT_LOAD_NO_SCALE
);
66 if ( error
|| face
->glyph
->outline
.n_points
<= 0 )
71 dummy
->units_per_em
= metrics
->units_per_em
;
72 scaler
->x_scale
= scaler
->y_scale
= 0x10000L
;
73 scaler
->x_delta
= scaler
->y_delta
= 0;
75 scaler
->render_mode
= FT_RENDER_MODE_NORMAL
;
78 af_glyph_hints_rescale( hints
, (AF_ScriptMetrics
)dummy
);
80 error
= af_glyph_hints_reload( hints
, &face
->glyph
->outline
, 0 );
84 for ( dim
= 0; dim
< AF_DIMENSION_MAX
; dim
++ )
86 AF_LatinAxis axis
= &metrics
->axis
[dim
];
87 AF_AxisHints axhints
= &hints
->axis
[dim
];
88 AF_Segment seg
, limit
, link
;
89 FT_UInt num_widths
= 0;
92 error
= af_latin_hints_compute_segments( hints
,
97 af_latin_hints_link_segments( hints
,
100 seg
= axhints
->segments
;
101 limit
= seg
+ axhints
->num_segments
;
103 for ( ; seg
< limit
; seg
++ )
107 /* we only consider stem segments there! */
108 if ( link
&& link
->link
== seg
&& link
> seg
)
113 dist
= seg
->pos
- link
->pos
;
117 if ( num_widths
< AF_LATIN_MAX_WIDTHS
)
118 axis
->widths
[ num_widths
++ ].org
= dist
;
122 af_sort_widths( num_widths
, axis
->widths
);
123 axis
->width_count
= num_widths
;
127 for ( dim
= 0; dim
< AF_DIMENSION_MAX
; dim
++ )
129 AF_LatinAxis axis
= &metrics
->axis
[dim
];
133 stdw
= ( axis
->width_count
> 0 )
134 ? axis
->widths
[0].org
135 : AF_LATIN_CONSTANT( metrics
, 50 );
137 /* let's try 20% of the smallest width */
138 axis
->edge_distance_threshold
= stdw
/ 5;
139 axis
->standard_width
= stdw
;
140 axis
->extra_light
= 0;
144 af_glyph_hints_done( hints
);
149 #define AF_LATIN_MAX_TEST_CHARACTERS 12
152 static const char af_latin_blue_chars
[AF_LATIN_MAX_BLUES
]
153 [AF_LATIN_MAX_TEST_CHARACTERS
+ 1] =
165 af_latin_metrics_init_blues( AF_LatinMetrics metrics
,
168 FT_Pos flats
[AF_LATIN_MAX_TEST_CHARACTERS
];
169 FT_Pos rounds
[AF_LATIN_MAX_TEST_CHARACTERS
];
175 AF_LatinAxis axis
= &metrics
->axis
[AF_DIMENSION_VERT
];
176 FT_GlyphSlot glyph
= face
->glyph
;
179 /* we compute the blues simply by loading each character from the */
180 /* 'af_latin_blue_chars[blues]' string, then compute its top-most or */
181 /* bottom-most points (depending on `AF_IS_TOP_BLUE') */
183 AF_LOG(( "blue zones computation\n" ));
184 AF_LOG(( "------------------------------------------------\n" ));
186 for ( bb
= 0; bb
< AF_LATIN_BLUE_MAX
; bb
++ )
188 const char* p
= af_latin_blue_chars
[bb
];
189 const char* limit
= p
+ AF_LATIN_MAX_TEST_CHARACTERS
;
194 AF_LOG(( "blue %3d: ", bb
));
199 for ( ; p
< limit
&& *p
; p
++ )
202 FT_Pos best_y
; /* same as points.y */
203 FT_Int best_point
, best_first
, best_last
;
208 AF_LOG(( "'%c'", *p
));
210 /* load the character in the face -- skip unknown or empty ones */
211 glyph_index
= FT_Get_Char_Index( face
, (FT_UInt
)*p
);
212 if ( glyph_index
== 0 )
215 error
= FT_Load_Glyph( face
, glyph_index
, FT_LOAD_NO_SCALE
);
216 if ( error
|| glyph
->outline
.n_points
<= 0 )
219 /* now compute min or max point indices and coordinates */
220 points
= glyph
->outline
.points
;
222 best_y
= 0; /* make compiler happy */
223 best_first
= 0; /* ditto */
224 best_last
= 0; /* ditto */
232 for ( nn
= 0; nn
< glyph
->outline
.n_contours
; first
= last
+1, nn
++ )
234 FT_Int old_best_point
= best_point
;
238 last
= glyph
->outline
.contours
[nn
];
240 /* Avoid single-point contours since they are never rasterized. */
241 /* In some fonts, they correspond to mark attachment points */
242 /* which are way outside of the glyph's real outline. */
246 if ( AF_LATIN_IS_TOP_BLUE( bb
) )
248 for ( pp
= first
; pp
<= last
; pp
++ )
249 if ( best_point
< 0 || points
[pp
].y
> best_y
)
252 best_y
= points
[pp
].y
;
257 for ( pp
= first
; pp
<= last
; pp
++ )
258 if ( best_point
< 0 || points
[pp
].y
< best_y
)
261 best_y
= points
[pp
].y
;
265 if ( best_point
!= old_best_point
)
271 AF_LOG(( "%5d", best_y
));
274 /* now check whether the point belongs to a straight or round */
275 /* segment; we first need to find in which contour the extremum */
276 /* lies, then inspect its previous and next points */
277 if ( best_point
>= 0 )
283 /* now look for the previous and next points that are not on the */
284 /* same Y coordinate. Threshold the `closeness'... */
290 if ( prev
> best_first
)
295 dist
= points
[prev
].y
- best_y
;
296 if ( dist
< -5 || dist
> 5 )
299 } while ( prev
!= best_point
);
303 if ( next
< best_last
)
308 dist
= points
[next
].y
- best_y
;
309 if ( dist
< -5 || dist
> 5 )
312 } while ( next
!= best_point
);
314 /* now, set the `round' flag depending on the segment's kind */
316 FT_CURVE_TAG( glyph
->outline
.tags
[prev
] ) != FT_CURVE_TAG_ON
||
317 FT_CURVE_TAG( glyph
->outline
.tags
[next
] ) != FT_CURVE_TAG_ON
);
319 AF_LOG(( "%c ", round
? 'r' : 'f' ));
323 rounds
[num_rounds
++] = best_y
;
325 flats
[num_flats
++] = best_y
;
330 if ( num_flats
== 0 && num_rounds
== 0 )
333 * we couldn't find a single glyph to compute this blue zone,
334 * we will simply ignore it then
336 AF_LOG(( "empty\n" ));
340 /* we have computed the contents of the `rounds' and `flats' tables, */
341 /* now determine the reference and overshoot position of the blue -- */
342 /* we simply take the median value after a simple sort */
343 af_sort_pos( num_rounds
, rounds
);
344 af_sort_pos( num_flats
, flats
);
346 blue
= & axis
->blues
[axis
->blue_count
];
347 blue_ref
= & blue
->ref
.org
;
348 blue_shoot
= & blue
->shoot
.org
;
352 if ( num_flats
== 0 )
355 *blue_shoot
= rounds
[num_rounds
/ 2];
357 else if ( num_rounds
== 0 )
360 *blue_shoot
= flats
[num_flats
/ 2];
364 *blue_ref
= flats
[num_flats
/ 2];
365 *blue_shoot
= rounds
[num_rounds
/ 2];
368 /* there are sometimes problems: if the overshoot position of top */
369 /* zones is under its reference position, or the opposite for bottom */
370 /* zones. We must thus check everything there and correct the errors */
371 if ( *blue_shoot
!= *blue_ref
)
373 FT_Pos ref
= *blue_ref
;
374 FT_Pos shoot
= *blue_shoot
;
375 FT_Bool over_ref
= FT_BOOL( shoot
> ref
);
378 if ( AF_LATIN_IS_TOP_BLUE( bb
) ^ over_ref
)
379 *blue_shoot
= *blue_ref
= ( shoot
+ ref
) / 2;
383 if ( AF_LATIN_IS_TOP_BLUE( bb
) )
384 blue
->flags
|= AF_LATIN_BLUE_TOP
;
387 * The following flag is used later to adjust the y and x scales
388 * in order to optimize the pixel grid alignment of the top of small
391 if ( bb
== AF_LATIN_BLUE_SMALL_TOP
)
392 blue
->flags
|= AF_LATIN_BLUE_ADJUSTMENT
;
394 AF_LOG(( "-- ref = %ld, shoot = %ld\n", *blue_ref
, *blue_shoot
));
402 af_latin_metrics_check_digits( AF_LatinMetrics metrics
,
406 FT_Bool started
= 0, same_width
= 1;
407 FT_Fixed advance
, old_advance
= 0;
410 /* check whether all ASCII digits have the same advance width; */
411 /* digit `0' is 0x30 in all supported charmaps */
412 for ( i
= 0x30; i
<= 0x39; i
++ )
417 glyph_index
= FT_Get_Char_Index( face
, i
);
418 if ( glyph_index
== 0 )
421 if ( FT_Get_Advance( face
, glyph_index
,
424 FT_LOAD_IGNORE_TRANSFORM
,
430 if ( advance
!= old_advance
)
438 old_advance
= advance
;
443 metrics
->root
.digits_have_same_width
= same_width
;
447 FT_LOCAL_DEF( FT_Error
)
448 af_latin_metrics_init( AF_LatinMetrics metrics
,
451 FT_Error error
= AF_Err_Ok
;
452 FT_CharMap oldmap
= face
->charmap
;
455 static const FT_Encoding latin_encodings
[] =
458 FT_ENCODING_APPLE_ROMAN
,
459 FT_ENCODING_ADOBE_STANDARD
,
460 FT_ENCODING_ADOBE_LATIN_1
,
461 FT_ENCODING_NONE
/* end of list */
465 metrics
->units_per_em
= face
->units_per_EM
;
467 /* do we have a latin charmap in there? */
468 for ( ee
= 0; latin_encodings
[ee
] != FT_ENCODING_NONE
; ee
++ )
470 error
= FT_Select_Charmap( face
, latin_encodings
[ee
] );
477 /* For now, compute the standard width and height from the `o'. */
478 af_latin_metrics_init_widths( metrics
, face
, 'o' );
479 af_latin_metrics_init_blues( metrics
, face
);
480 af_latin_metrics_check_digits( metrics
, face
);
483 FT_Set_Charmap( face
, oldmap
);
489 af_latin_metrics_scale_dim( AF_LatinMetrics metrics
,
499 if ( dim
== AF_DIMENSION_HORZ
)
501 scale
= scaler
->x_scale
;
502 delta
= scaler
->x_delta
;
506 scale
= scaler
->y_scale
;
507 delta
= scaler
->y_delta
;
510 axis
= &metrics
->axis
[dim
];
512 if ( axis
->org_scale
== scale
&& axis
->org_delta
== delta
)
515 axis
->org_scale
= scale
;
516 axis
->org_delta
= delta
;
519 * correct X and Y scale to optimize the alignment of the top of small
520 * letters to the pixel grid
523 AF_LatinAxis Axis
= &metrics
->axis
[AF_DIMENSION_VERT
];
524 AF_LatinBlue blue
= NULL
;
527 for ( nn
= 0; nn
< Axis
->blue_count
; nn
++ )
529 if ( Axis
->blues
[nn
].flags
& AF_LATIN_BLUE_ADJUSTMENT
)
531 blue
= &Axis
->blues
[nn
];
538 FT_Pos scaled
= FT_MulFix( blue
->shoot
.org
, scaler
->y_scale
);
539 FT_Pos fitted
= ( scaled
+ 40 ) & ~63;
542 if ( scaled
!= fitted
)
545 if ( dim
== AF_DIMENSION_HORZ
)
547 if ( fitted
< scaled
)
548 scale
-= scale
/ 50; /* scale *= 0.98 */
552 if ( dim
== AF_DIMENSION_VERT
)
554 scale
= FT_MulDiv( scale
, fitted
, scaled
);
563 if ( dim
== AF_DIMENSION_HORZ
)
565 metrics
->root
.scaler
.x_scale
= scale
;
566 metrics
->root
.scaler
.x_delta
= delta
;
570 metrics
->root
.scaler
.y_scale
= scale
;
571 metrics
->root
.scaler
.y_delta
= delta
;
574 /* scale the standard widths */
575 for ( nn
= 0; nn
< axis
->width_count
; nn
++ )
577 AF_Width width
= axis
->widths
+ nn
;
580 width
->cur
= FT_MulFix( width
->org
, scale
);
581 width
->fit
= width
->cur
;
584 /* an extra-light axis corresponds to a standard width that is */
585 /* smaller than 0.75 pixels */
587 (FT_Bool
)( FT_MulFix( axis
->standard_width
, scale
) < 32 + 8 );
589 if ( dim
== AF_DIMENSION_VERT
)
591 /* scale the blue zones */
592 for ( nn
= 0; nn
< axis
->blue_count
; nn
++ )
594 AF_LatinBlue blue
= &axis
->blues
[nn
];
598 blue
->ref
.cur
= FT_MulFix( blue
->ref
.org
, scale
) + delta
;
599 blue
->ref
.fit
= blue
->ref
.cur
;
600 blue
->shoot
.cur
= FT_MulFix( blue
->shoot
.org
, scale
) + delta
;
601 blue
->shoot
.fit
= blue
->shoot
.cur
;
602 blue
->flags
&= ~AF_LATIN_BLUE_ACTIVE
;
604 /* a blue zone is only active if it is less than 3/4 pixels tall */
605 dist
= FT_MulFix( blue
->ref
.org
- blue
->shoot
.org
, scale
);
606 if ( dist
<= 48 && dist
>= -48 )
608 FT_Pos delta1
, delta2
;
611 delta1
= blue
->shoot
.org
- blue
->ref
.org
;
616 delta2
= FT_MulFix( delta2
, scale
);
620 else if ( delta2
< 64 )
621 delta2
= 32 + ( ( ( delta2
- 32 ) + 16 ) & ~31 );
623 delta2
= FT_PIX_ROUND( delta2
);
628 blue
->ref
.fit
= FT_PIX_ROUND( blue
->ref
.cur
);
629 blue
->shoot
.fit
= blue
->ref
.fit
+ delta2
;
631 blue
->flags
|= AF_LATIN_BLUE_ACTIVE
;
639 af_latin_metrics_scale( AF_LatinMetrics metrics
,
642 metrics
->root
.scaler
.render_mode
= scaler
->render_mode
;
643 metrics
->root
.scaler
.face
= scaler
->face
;
645 af_latin_metrics_scale_dim( metrics
, scaler
, AF_DIMENSION_HORZ
);
646 af_latin_metrics_scale_dim( metrics
, scaler
, AF_DIMENSION_VERT
);
650 /*************************************************************************/
651 /*************************************************************************/
653 /***** L A T I N G L Y P H A N A L Y S I S *****/
655 /*************************************************************************/
656 /*************************************************************************/
658 FT_LOCAL_DEF( FT_Error
)
659 af_latin_hints_compute_segments( AF_GlyphHints hints
,
662 AF_AxisHints axis
= &hints
->axis
[dim
];
663 FT_Memory memory
= hints
->memory
;
664 FT_Error error
= AF_Err_Ok
;
665 AF_Segment segment
= NULL
;
667 AF_Point
* contour
= hints
->contours
;
668 AF_Point
* contour_limit
= contour
+ hints
->num_contours
;
669 AF_Direction major_dir
, segment_dir
;
674 seg0
.flags
= AF_EDGE_NORMAL
;
676 major_dir
= (AF_Direction
)FT_ABS( axis
->major_dir
);
677 segment_dir
= major_dir
;
679 axis
->num_segments
= 0;
681 /* set up (u,v) in each point */
682 if ( dim
== AF_DIMENSION_HORZ
)
684 AF_Point point
= hints
->points
;
685 AF_Point limit
= point
+ hints
->num_points
;
688 for ( ; point
< limit
; point
++ )
690 point
->u
= point
->fx
;
691 point
->v
= point
->fy
;
696 AF_Point point
= hints
->points
;
697 AF_Point limit
= point
+ hints
->num_points
;
700 for ( ; point
< limit
; point
++ )
702 point
->u
= point
->fy
;
703 point
->v
= point
->fx
;
707 /* do each contour separately */
708 for ( ; contour
< contour_limit
; contour
++ )
710 AF_Point point
= contour
[0];
711 AF_Point last
= point
->prev
;
713 FT_Pos min_pos
= 32000; /* minimum segment pos != min_coord */
714 FT_Pos max_pos
= -32000; /* maximum segment pos != max_coord */
718 if ( point
== last
) /* skip singletons -- just in case */
721 if ( FT_ABS( last
->out_dir
) == major_dir
&&
722 FT_ABS( point
->out_dir
) == major_dir
)
724 /* we are already on an edge, try to locate its start */
730 if ( FT_ABS( point
->out_dir
) != major_dir
)
756 if ( point
->out_dir
!= segment_dir
|| point
== last
)
758 /* we are just leaving an edge; record a new segment! */
759 segment
->last
= point
;
760 segment
->pos
= (FT_Short
)( ( min_pos
+ max_pos
) >> 1 );
762 /* a segment is round if either its first or last point */
763 /* is a control point */
764 if ( ( segment
->first
->flags
| point
->flags
) &
766 segment
->flags
|= AF_EDGE_ROUND
;
768 /* compute segment size */
769 min_pos
= max_pos
= point
->v
;
771 v
= segment
->first
->v
;
777 segment
->min_coord
= (FT_Short
)min_pos
;
778 segment
->max_coord
= (FT_Short
)max_pos
;
779 segment
->height
= (FT_Short
)( segment
->max_coord
-
780 segment
->min_coord
);
788 /* now exit if we are at the start/end point */
796 if ( !on_edge
&& FT_ABS( point
->out_dir
) == major_dir
)
798 /* this is the start of a new segment! */
799 segment_dir
= (AF_Direction
)point
->out_dir
;
801 /* clear all segment fields */
802 error
= af_axis_hints_new_segment( axis
, memory
, &segment
);
807 segment
->dir
= (FT_Char
)segment_dir
;
808 min_pos
= max_pos
= point
->u
;
809 segment
->first
= point
;
810 segment
->last
= point
;
811 segment
->contour
= contour
;
821 /* now slightly increase the height of segments when this makes */
822 /* sense -- this is used to better detect and ignore serifs */
824 AF_Segment segments
= axis
->segments
;
825 AF_Segment segments_end
= segments
+ axis
->num_segments
;
828 for ( segment
= segments
; segment
< segments_end
; segment
++ )
830 AF_Point first
= segment
->first
;
831 AF_Point last
= segment
->last
;
832 FT_Pos first_v
= first
->v
;
833 FT_Pos last_v
= last
->v
;
839 if ( first_v
< last_v
)
845 if ( p
->v
< first_v
)
846 segment
->height
= (FT_Short
)( segment
->height
+
847 ( ( first_v
- p
->v
) >> 1 ) );
851 segment
->height
= (FT_Short
)( segment
->height
+
852 ( ( p
->v
- last_v
) >> 1 ) );
860 if ( p
->v
> first_v
)
861 segment
->height
= (FT_Short
)( segment
->height
+
862 ( ( p
->v
- first_v
) >> 1 ) );
866 segment
->height
= (FT_Short
)( segment
->height
+
867 ( ( last_v
- p
->v
) >> 1 ) );
878 af_latin_hints_link_segments( AF_GlyphHints hints
,
881 AF_AxisHints axis
= &hints
->axis
[dim
];
882 AF_Segment segments
= axis
->segments
;
883 AF_Segment segment_limit
= segments
+ axis
->num_segments
;
884 FT_Pos len_threshold
, len_score
;
885 AF_Segment seg1
, seg2
;
888 len_threshold
= AF_LATIN_CONSTANT( hints
->metrics
, 8 );
889 if ( len_threshold
== 0 )
892 len_score
= AF_LATIN_CONSTANT( hints
->metrics
, 6000 );
894 /* now compare each segment to the others */
895 for ( seg1
= segments
; seg1
< segment_limit
; seg1
++ )
897 /* the fake segments are introduced to hint the metrics -- */
898 /* we must never link them to anything */
899 if ( seg1
->dir
!= axis
->major_dir
|| seg1
->first
== seg1
->last
)
902 for ( seg2
= segments
; seg2
< segment_limit
; seg2
++ )
903 if ( seg1
->dir
+ seg2
->dir
== 0 && seg2
->pos
> seg1
->pos
)
905 FT_Pos pos1
= seg1
->pos
;
906 FT_Pos pos2
= seg2
->pos
;
907 FT_Pos dist
= pos2
- pos1
;
914 FT_Pos min
= seg1
->min_coord
;
915 FT_Pos max
= seg1
->max_coord
;
919 if ( min
< seg2
->min_coord
)
920 min
= seg2
->min_coord
;
922 if ( max
> seg2
->max_coord
)
923 max
= seg2
->max_coord
;
926 if ( len
>= len_threshold
)
928 score
= dist
+ len_score
/ len
;
930 if ( score
< seg1
->score
)
936 if ( score
< seg2
->score
)
946 /* now, compute the `serif' segments */
947 for ( seg1
= segments
; seg1
< segment_limit
; seg1
++ )
953 if ( seg2
->link
!= seg1
)
956 seg1
->serif
= seg2
->link
;
963 FT_LOCAL_DEF( FT_Error
)
964 af_latin_hints_compute_edges( AF_GlyphHints hints
,
967 AF_AxisHints axis
= &hints
->axis
[dim
];
968 FT_Error error
= AF_Err_Ok
;
969 FT_Memory memory
= hints
->memory
;
970 AF_LatinAxis laxis
= &((AF_LatinMetrics
)hints
->metrics
)->axis
[dim
];
972 AF_Segment segments
= axis
->segments
;
973 AF_Segment segment_limit
= segments
+ axis
->num_segments
;
978 FT_Pos edge_distance_threshold
;
979 FT_Pos segment_length_threshold
;
984 scale
= ( dim
== AF_DIMENSION_HORZ
) ? hints
->x_scale
987 up_dir
= ( dim
== AF_DIMENSION_HORZ
) ? AF_DIR_UP
991 * We ignore all segments that are less than 1 pixels in length,
992 * to avoid many problems with serif fonts. We compute the
993 * corresponding threshold in font units.
995 if ( dim
== AF_DIMENSION_HORZ
)
996 segment_length_threshold
= FT_DivFix( 64, hints
->y_scale
);
998 segment_length_threshold
= 0;
1000 /*********************************************************************/
1002 /* We will begin by generating a sorted table of edges for the */
1003 /* current direction. To do so, we simply scan each segment and try */
1004 /* to find an edge in our table that corresponds to its position. */
1006 /* If no edge is found, we create and insert a new edge in the */
1007 /* sorted table. Otherwise, we simply add the segment to the edge's */
1008 /* list which will be processed in the second step to compute the */
1009 /* edge's properties. */
1011 /* Note that the edges table is sorted along the segment/edge */
1014 /*********************************************************************/
1016 edge_distance_threshold
= FT_MulFix( laxis
->edge_distance_threshold
,
1018 if ( edge_distance_threshold
> 64 / 4 )
1019 edge_distance_threshold
= 64 / 4;
1021 edge_distance_threshold
= FT_DivFix( edge_distance_threshold
,
1024 for ( seg
= segments
; seg
< segment_limit
; seg
++ )
1030 if ( seg
->height
< segment_length_threshold
)
1033 /* A special case for serif edges: If they are smaller than */
1034 /* 1.5 pixels we ignore them. */
1036 2 * seg
->height
< 3 * segment_length_threshold
)
1039 /* look for an edge corresponding to the segment */
1040 for ( ee
= 0; ee
< axis
->num_edges
; ee
++ )
1042 AF_Edge edge
= axis
->edges
+ ee
;
1046 dist
= seg
->pos
- edge
->fpos
;
1050 if ( dist
< edge_distance_threshold
&& edge
->dir
== seg
->dir
)
1062 /* insert a new edge in the list and */
1063 /* sort according to the position */
1064 error
= af_axis_hints_new_edge( axis
, seg
->pos
,
1065 (AF_Direction
)seg
->dir
,
1070 /* add the segment to the new edge's list */
1075 edge
->fpos
= seg
->pos
;
1076 edge
->dir
= seg
->dir
;
1077 edge
->opos
= edge
->pos
= FT_MulFix( seg
->pos
, scale
);
1078 seg
->edge_next
= seg
;
1082 /* if an edge was found, simply add the segment to the edge's */
1084 seg
->edge_next
= found
->first
;
1085 found
->last
->edge_next
= seg
;
1091 /*********************************************************************/
1093 /* Good, we will now compute each edge's properties according to */
1094 /* segments found on its position. Basically, these are: */
1096 /* - edge's main direction */
1097 /* - stem edge, serif edge or both (which defaults to stem then) */
1098 /* - rounded edge, straight or both (which defaults to straight) */
1099 /* - link for edge */
1101 /*********************************************************************/
1103 /* first of all, set the `edge' field in each segment -- this is */
1104 /* required in order to compute edge links */
1107 * Note that removing this loop and setting the `edge' field of each
1108 * segment directly in the code above slows down execution speed for
1109 * some reasons on platforms like the Sun.
1112 AF_Edge edges
= axis
->edges
;
1113 AF_Edge edge_limit
= edges
+ axis
->num_edges
;
1117 for ( edge
= edges
; edge
< edge_limit
; edge
++ )
1124 seg
= seg
->edge_next
;
1126 } while ( seg
!= edge
->first
);
1129 /* now, compute each edge properties */
1130 for ( edge
= edges
; edge
< edge_limit
; edge
++ )
1132 FT_Int is_round
= 0; /* does it contain round segments? */
1133 FT_Int is_straight
= 0; /* does it contain straight segments? */
1134 FT_Pos ups
= 0; /* number of upwards segments */
1135 FT_Pos downs
= 0; /* number of downwards segments */
1145 /* check for roundness of segment */
1146 if ( seg
->flags
& AF_EDGE_ROUND
)
1151 /* check for segment direction */
1152 if ( seg
->dir
== up_dir
)
1153 ups
+= seg
->max_coord
-seg
->min_coord
;
1155 downs
+= seg
->max_coord
-seg
->min_coord
;
1157 /* check for links -- if seg->serif is set, then seg->link must */
1159 is_serif
= (FT_Bool
)( seg
->serif
&&
1161 seg
->serif
->edge
!= edge
);
1163 if ( ( seg
->link
&& seg
->link
->edge
!= NULL
) || is_serif
)
1175 edge2
= edge
->serif
;
1184 edge_delta
= edge
->fpos
- edge2
->fpos
;
1185 if ( edge_delta
< 0 )
1186 edge_delta
= -edge_delta
;
1188 seg_delta
= seg
->pos
- seg2
->pos
;
1189 if ( seg_delta
< 0 )
1190 seg_delta
= -seg_delta
;
1192 if ( seg_delta
< edge_delta
)
1200 edge
->serif
= edge2
;
1201 edge2
->flags
|= AF_EDGE_SERIF
;
1207 seg
= seg
->edge_next
;
1209 } while ( seg
!= edge
->first
);
1211 /* set the round/straight flags */
1212 edge
->flags
= AF_EDGE_NORMAL
;
1214 if ( is_round
> 0 && is_round
>= is_straight
)
1215 edge
->flags
|= AF_EDGE_ROUND
;
1218 /* set the edge's main direction */
1219 edge
->dir
= AF_DIR_NONE
;
1222 edge
->dir
= (FT_Char
)up_dir
;
1224 else if ( ups
< downs
)
1225 edge
->dir
= (FT_Char
)-up_dir
;
1227 else if ( ups
== downs
)
1228 edge
->dir
= 0; /* both up and down! */
1231 /* gets rid of serifs if link is set */
1232 /* XXX: This gets rid of many unpleasant artefacts! */
1233 /* Example: the `c' in cour.pfa at size 13 */
1235 if ( edge
->serif
&& edge
->link
)
1245 FT_LOCAL_DEF( FT_Error
)
1246 af_latin_hints_detect_features( AF_GlyphHints hints
,
1252 error
= af_latin_hints_compute_segments( hints
, dim
);
1255 af_latin_hints_link_segments( hints
, dim
);
1257 error
= af_latin_hints_compute_edges( hints
, dim
);
1263 FT_LOCAL_DEF( void )
1264 af_latin_hints_compute_blue_edges( AF_GlyphHints hints
,
1265 AF_LatinMetrics metrics
)
1267 AF_AxisHints axis
= &hints
->axis
[ AF_DIMENSION_VERT
];
1268 AF_Edge edge
= axis
->edges
;
1269 AF_Edge edge_limit
= edge
+ axis
->num_edges
;
1270 AF_LatinAxis latin
= &metrics
->axis
[ AF_DIMENSION_VERT
];
1271 FT_Fixed scale
= latin
->scale
;
1274 /* compute which blue zones are active, i.e. have their scaled */
1275 /* size < 3/4 pixels */
1277 /* for each horizontal edge search the blue zone which is closest */
1278 for ( ; edge
< edge_limit
; edge
++ )
1281 AF_Width best_blue
= NULL
;
1282 FT_Pos best_dist
; /* initial threshold */
1285 /* compute the initial threshold as a fraction of the EM size */
1286 best_dist
= FT_MulFix( metrics
->units_per_em
/ 40, scale
);
1288 if ( best_dist
> 64 / 2 )
1291 for ( bb
= 0; bb
< AF_LATIN_BLUE_MAX
; bb
++ )
1293 AF_LatinBlue blue
= latin
->blues
+ bb
;
1294 FT_Bool is_top_blue
, is_major_dir
;
1297 /* skip inactive blue zones (i.e., those that are too small) */
1298 if ( !( blue
->flags
& AF_LATIN_BLUE_ACTIVE
) )
1301 /* if it is a top zone, check for right edges -- if it is a bottom */
1302 /* zone, check for left edges */
1304 /* of course, that's for TrueType */
1305 is_top_blue
= (FT_Byte
)( ( blue
->flags
& AF_LATIN_BLUE_TOP
) != 0 );
1306 is_major_dir
= FT_BOOL( edge
->dir
== axis
->major_dir
);
1308 /* if it is a top zone, the edge must be against the major */
1309 /* direction; if it is a bottom zone, it must be in the major */
1311 if ( is_top_blue
^ is_major_dir
)
1316 /* first of all, compare it to the reference position */
1317 dist
= edge
->fpos
- blue
->ref
.org
;
1321 dist
= FT_MulFix( dist
, scale
);
1322 if ( dist
< best_dist
)
1325 best_blue
= & blue
->ref
;
1328 /* now, compare it to the overshoot position if the edge is */
1329 /* rounded, and if the edge is over the reference position of a */
1330 /* top zone, or under the reference position of a bottom zone */
1331 if ( edge
->flags
& AF_EDGE_ROUND
&& dist
!= 0 )
1333 FT_Bool is_under_ref
= FT_BOOL( edge
->fpos
< blue
->ref
.org
);
1336 if ( is_top_blue
^ is_under_ref
)
1338 blue
= latin
->blues
+ bb
;
1339 dist
= edge
->fpos
- blue
->shoot
.org
;
1343 dist
= FT_MulFix( dist
, scale
);
1344 if ( dist
< best_dist
)
1347 best_blue
= & blue
->shoot
;
1355 edge
->blue_edge
= best_blue
;
1361 af_latin_hints_init( AF_GlyphHints hints
,
1362 AF_LatinMetrics metrics
)
1364 FT_Render_Mode mode
;
1365 FT_UInt32 scaler_flags
, other_flags
;
1366 FT_Face face
= metrics
->root
.scaler
.face
;
1369 af_glyph_hints_rescale( hints
, (AF_ScriptMetrics
)metrics
);
1372 * correct x_scale and y_scale if needed, since they may have
1373 * been modified `af_latin_metrics_scale_dim' above
1375 hints
->x_scale
= metrics
->axis
[AF_DIMENSION_HORZ
].scale
;
1376 hints
->x_delta
= metrics
->axis
[AF_DIMENSION_HORZ
].delta
;
1377 hints
->y_scale
= metrics
->axis
[AF_DIMENSION_VERT
].scale
;
1378 hints
->y_delta
= metrics
->axis
[AF_DIMENSION_VERT
].delta
;
1380 /* compute flags depending on render mode, etc. */
1381 mode
= metrics
->root
.scaler
.render_mode
;
1383 #if 0 /* #ifdef AF_USE_WARPER */
1384 if ( mode
== FT_RENDER_MODE_LCD
|| mode
== FT_RENDER_MODE_LCD_V
)
1386 metrics
->root
.scaler
.render_mode
= mode
= FT_RENDER_MODE_NORMAL
;
1390 scaler_flags
= hints
->scaler_flags
;
1394 * We snap the width of vertical stems for the monochrome and
1395 * horizontal LCD rendering targets only.
1397 if ( mode
== FT_RENDER_MODE_MONO
|| mode
== FT_RENDER_MODE_LCD
)
1398 other_flags
|= AF_LATIN_HINTS_HORZ_SNAP
;
1401 * We snap the width of horizontal stems for the monochrome and
1402 * vertical LCD rendering targets only.
1404 if ( mode
== FT_RENDER_MODE_MONO
|| mode
== FT_RENDER_MODE_LCD_V
)
1405 other_flags
|= AF_LATIN_HINTS_VERT_SNAP
;
1408 * We adjust stems to full pixels only if we don't use the `light' mode.
1410 if ( mode
!= FT_RENDER_MODE_LIGHT
)
1411 other_flags
|= AF_LATIN_HINTS_STEM_ADJUST
;
1413 if ( mode
== FT_RENDER_MODE_MONO
)
1414 other_flags
|= AF_LATIN_HINTS_MONO
;
1417 * In `light' hinting mode we disable horizontal hinting completely.
1418 * We also do it if the face is italic.
1420 if ( mode
== FT_RENDER_MODE_LIGHT
||
1421 (face
->style_flags
& FT_STYLE_FLAG_ITALIC
) != 0 )
1422 scaler_flags
|= AF_SCALER_FLAG_NO_HORIZONTAL
;
1424 hints
->scaler_flags
= scaler_flags
;
1425 hints
->other_flags
= other_flags
;
1431 /*************************************************************************/
1432 /*************************************************************************/
1434 /***** L A T I N G L Y P H G R I D - F I T T I N G *****/
1436 /*************************************************************************/
1437 /*************************************************************************/
1439 /* snap a given width in scaled coordinates to one of the */
1440 /* current standard widths */
1443 af_latin_snap_width( AF_Width widths
,
1448 FT_Pos best
= 64 + 32 + 2;
1449 FT_Pos reference
= width
;
1453 for ( n
= 0; n
< count
; n
++ )
1470 scaled
= FT_PIX_ROUND( reference
);
1472 if ( width
>= reference
)
1474 if ( width
< scaled
+ 48 )
1479 if ( width
> scaled
- 48 )
1487 /* compute the snapped width of a given stem */
1490 af_latin_compute_stem_width( AF_GlyphHints hints
,
1493 AF_Edge_Flags base_flags
,
1494 AF_Edge_Flags stem_flags
)
1496 AF_LatinMetrics metrics
= (AF_LatinMetrics
) hints
->metrics
;
1497 AF_LatinAxis axis
= & metrics
->axis
[dim
];
1498 FT_Pos dist
= width
;
1500 FT_Int vertical
= ( dim
== AF_DIMENSION_VERT
);
1503 if ( !AF_LATIN_HINTS_DO_STEM_ADJUST( hints
) ||
1513 if ( ( vertical
&& !AF_LATIN_HINTS_DO_VERT_SNAP( hints
) ) ||
1514 ( !vertical
&& !AF_LATIN_HINTS_DO_HORZ_SNAP( hints
) ) )
1516 /* smooth hinting process: very lightly quantize the stem width */
1518 /* leave the widths of serifs alone */
1520 if ( ( stem_flags
& AF_EDGE_SERIF
) && vertical
&& ( dist
< 3 * 64 ) )
1523 else if ( ( base_flags
& AF_EDGE_ROUND
) )
1528 else if ( dist
< 56 )
1531 if ( axis
->width_count
> 0 )
1536 /* compare to standard width */
1537 if ( axis
->width_count
> 0 )
1539 delta
= dist
- axis
->widths
[0].cur
;
1546 dist
= axis
->widths
[0].cur
;
1554 if ( dist
< 3 * 64 )
1562 else if ( delta
< 32 )
1565 else if ( delta
< 54 )
1572 dist
= ( dist
+ 32 ) & ~63;
1577 /* strong hinting process: snap the stem width to integer pixels */
1578 FT_Pos org_dist
= dist
;
1581 dist
= af_latin_snap_width( axis
->widths
, axis
->width_count
, dist
);
1585 /* in the case of vertical hinting, always round */
1586 /* the stem heights to integer pixels */
1589 dist
= ( dist
+ 16 ) & ~63;
1595 if ( AF_LATIN_HINTS_DO_MONO( hints
) )
1597 /* monochrome horizontal hinting: snap widths to integer pixels */
1598 /* with a different threshold */
1603 dist
= ( dist
+ 32 ) & ~63;
1607 /* for horizontal anti-aliased hinting, we adopt a more subtle */
1608 /* approach: we strengthen small stems, round stems whose size */
1609 /* is between 1 and 2 pixels to an integer, otherwise nothing */
1612 dist
= ( dist
+ 64 ) >> 1;
1614 else if ( dist
< 128 )
1616 /* We only round to an integer width if the corresponding */
1617 /* distortion is less than 1/4 pixel. Otherwise this */
1618 /* makes everything worse since the diagonals, which are */
1619 /* not hinted, appear a lot bolder or thinner than the */
1620 /* vertical stems. */
1625 dist
= ( dist
+ 22 ) & ~63;
1626 delta
= dist
- org_dist
;
1634 dist
= ( dist
+ 64 ) >> 1;
1638 /* round otherwise to prevent color fringes in LCD mode */
1639 dist
= ( dist
+ 32 ) & ~63;
1652 /* align one stem edge relative to the previous stem edge */
1655 af_latin_align_linked_edge( AF_GlyphHints hints
,
1660 FT_Pos dist
= stem_edge
->opos
- base_edge
->opos
;
1662 FT_Pos fitted_width
= af_latin_compute_stem_width(
1664 (AF_Edge_Flags
)base_edge
->flags
,
1665 (AF_Edge_Flags
)stem_edge
->flags
);
1668 stem_edge
->pos
= base_edge
->pos
+ fitted_width
;
1670 AF_LOG(( "LINK: edge %d (opos=%.2f) linked to (%.2f), "
1671 "dist was %.2f, now %.2f\n",
1672 stem_edge
-hints
->axis
[dim
].edges
, stem_edge
->opos
/ 64.0,
1673 stem_edge
->pos
/ 64.0, dist
/ 64.0, fitted_width
/ 64.0 ));
1678 af_latin_align_serif_edge( AF_GlyphHints hints
,
1684 serif
->pos
= base
->pos
+ (serif
->opos
- base
->opos
);
1688 /*************************************************************************/
1689 /*************************************************************************/
1690 /*************************************************************************/
1692 /**** E D G E H I N T I N G ****/
1694 /*************************************************************************/
1695 /*************************************************************************/
1696 /*************************************************************************/
1699 FT_LOCAL_DEF( void )
1700 af_latin_hint_edges( AF_GlyphHints hints
,
1703 AF_AxisHints axis
= &hints
->axis
[dim
];
1704 AF_Edge edges
= axis
->edges
;
1705 AF_Edge edge_limit
= edges
+ axis
->num_edges
;
1709 FT_Int has_serifs
= 0;
1712 /* we begin by aligning all stems relative to the blue zone */
1713 /* if needed -- that's only for horizontal edges */
1715 if ( dim
== AF_DIMENSION_VERT
&& AF_HINTS_DO_BLUES( hints
) )
1717 for ( edge
= edges
; edge
< edge_limit
; edge
++ )
1720 AF_Edge edge1
, edge2
;
1723 if ( edge
->flags
& AF_EDGE_DONE
)
1726 blue
= edge
->blue_edge
;
1734 else if ( edge2
&& edge2
->blue_edge
)
1736 blue
= edge2
->blue_edge
;
1744 AF_LOG(( "BLUE: edge %d (opos=%.2f) snapped to (%.2f), "
1746 edge1
-edges
, edge1
->opos
/ 64.0, blue
->fit
/ 64.0,
1747 edge1
->pos
/ 64.0 ));
1749 edge1
->pos
= blue
->fit
;
1750 edge1
->flags
|= AF_EDGE_DONE
;
1752 if ( edge2
&& !edge2
->blue_edge
)
1754 af_latin_align_linked_edge( hints
, dim
, edge1
, edge2
);
1755 edge2
->flags
|= AF_EDGE_DONE
;
1763 /* now we will align all stem edges, trying to maintain the */
1764 /* relative order of stems in the glyph */
1765 for ( edge
= edges
; edge
< edge_limit
; edge
++ )
1770 if ( edge
->flags
& AF_EDGE_DONE
)
1773 /* skip all non-stem edges */
1781 /* now align the stem */
1783 /* this should not happen, but it's better to be safe */
1784 if ( edge2
->blue_edge
)
1786 AF_LOG(( "ASSERTION FAILED for edge %d\n", edge2
-edges
));
1788 af_latin_align_linked_edge( hints
, dim
, edge2
, edge
);
1789 edge
->flags
|= AF_EDGE_DONE
;
1795 FT_Pos org_len
, org_center
, cur_len
;
1796 FT_Pos cur_pos1
, error1
, error2
, u_off
, d_off
;
1799 org_len
= edge2
->opos
- edge
->opos
;
1800 cur_len
= af_latin_compute_stem_width(
1801 hints
, dim
, org_len
,
1802 (AF_Edge_Flags
)edge
->flags
,
1803 (AF_Edge_Flags
)edge2
->flags
);
1804 if ( cur_len
<= 64 )
1814 org_center
= edge
->opos
+ ( org_len
>> 1 );
1816 cur_pos1
= FT_PIX_ROUND( org_center
);
1818 error1
= org_center
- ( cur_pos1
- u_off
);
1822 error2
= org_center
- ( cur_pos1
+ d_off
);
1826 if ( error1
< error2
)
1831 edge
->pos
= cur_pos1
- cur_len
/ 2;
1832 edge2
->pos
= edge
->pos
+ cur_len
;
1835 edge
->pos
= FT_PIX_ROUND( edge
->opos
);
1837 AF_LOG(( "ANCHOR: edge %d (opos=%.2f) and %d (opos=%.2f) "
1838 "snapped to (%.2f) (%.2f)\n",
1839 edge
-edges
, edge
->opos
/ 64.0,
1840 edge2
-edges
, edge2
->opos
/ 64.0,
1841 edge
->pos
/ 64.0, edge2
->pos
/ 64.0 ));
1844 edge
->flags
|= AF_EDGE_DONE
;
1846 af_latin_align_linked_edge( hints
, dim
, edge
, edge2
);
1850 FT_Pos org_pos
, org_len
, org_center
, cur_len
;
1851 FT_Pos cur_pos1
, cur_pos2
, delta1
, delta2
;
1854 org_pos
= anchor
->pos
+ ( edge
->opos
- anchor
->opos
);
1855 org_len
= edge2
->opos
- edge
->opos
;
1856 org_center
= org_pos
+ ( org_len
>> 1 );
1858 cur_len
= af_latin_compute_stem_width(
1859 hints
, dim
, org_len
,
1860 (AF_Edge_Flags
)edge
->flags
,
1861 (AF_Edge_Flags
)edge2
->flags
);
1863 if ( edge2
->flags
& AF_EDGE_DONE
)
1864 edge
->pos
= edge2
->pos
- cur_len
;
1866 else if ( cur_len
< 96 )
1868 FT_Pos u_off
, d_off
;
1871 cur_pos1
= FT_PIX_ROUND( org_center
);
1881 delta1
= org_center
- ( cur_pos1
- u_off
);
1885 delta2
= org_center
- ( cur_pos1
+ d_off
);
1889 if ( delta1
< delta2
)
1894 edge
->pos
= cur_pos1
- cur_len
/ 2;
1895 edge2
->pos
= cur_pos1
+ cur_len
/ 2;
1897 AF_LOG(( "STEM: %d (opos=%.2f) to %d (opos=%.2f) "
1898 "snapped to (%.2f) and (%.2f)\n",
1899 edge
-edges
, edge
->opos
/ 64.0,
1900 edge2
-edges
, edge2
->opos
/ 64.0,
1901 edge
->pos
/ 64.0, edge2
->pos
/ 64.0 ));
1905 org_pos
= anchor
->pos
+ ( edge
->opos
- anchor
->opos
);
1906 org_len
= edge2
->opos
- edge
->opos
;
1907 org_center
= org_pos
+ ( org_len
>> 1 );
1909 cur_len
= af_latin_compute_stem_width(
1910 hints
, dim
, org_len
,
1911 (AF_Edge_Flags
)edge
->flags
,
1912 (AF_Edge_Flags
)edge2
->flags
);
1914 cur_pos1
= FT_PIX_ROUND( org_pos
);
1915 delta1
= cur_pos1
+ ( cur_len
>> 1 ) - org_center
;
1919 cur_pos2
= FT_PIX_ROUND( org_pos
+ org_len
) - cur_len
;
1920 delta2
= cur_pos2
+ ( cur_len
>> 1 ) - org_center
;
1924 edge
->pos
= ( delta1
< delta2
) ? cur_pos1
: cur_pos2
;
1925 edge2
->pos
= edge
->pos
+ cur_len
;
1927 AF_LOG(( "STEM: %d (opos=%.2f) to %d (opos=%.2f) "
1928 "snapped to (%.2f) and (%.2f)\n",
1929 edge
-edges
, edge
->opos
/ 64.0,
1930 edge2
-edges
, edge2
->opos
/ 64.0,
1931 edge
->pos
/ 64.0, edge2
->pos
/ 64.0 ));
1934 edge
->flags
|= AF_EDGE_DONE
;
1935 edge2
->flags
|= AF_EDGE_DONE
;
1937 if ( edge
> edges
&& edge
->pos
< edge
[-1].pos
)
1939 AF_LOG(( "BOUND: %d (pos=%.2f) to (%.2f)\n",
1940 edge
-edges
, edge
->pos
/ 64.0, edge
[-1].pos
/ 64.0 ));
1941 edge
->pos
= edge
[-1].pos
;
1946 /* make sure that lowercase m's maintain their symmetry */
1948 /* In general, lowercase m's have six vertical edges if they are sans */
1949 /* serif, or twelve if they are with serifs. This implementation is */
1950 /* based on that assumption, and seems to work very well with most */
1951 /* faces. However, if for a certain face this assumption is not */
1952 /* true, the m is just rendered like before. In addition, any stem */
1953 /* correction will only be applied to symmetrical glyphs (even if the */
1954 /* glyph is not an m), so the potential for unwanted distortion is */
1955 /* relatively low. */
1957 /* We don't handle horizontal edges since we can't easily assure that */
1958 /* the third (lowest) stem aligns with the base line; it might end up */
1959 /* one pixel higher or lower. */
1961 n_edges
= edge_limit
- edges
;
1962 if ( dim
== AF_DIMENSION_HORZ
&& ( n_edges
== 6 || n_edges
== 12 ) )
1964 AF_Edge edge1
, edge2
, edge3
;
1965 FT_Pos dist1
, dist2
, span
, delta
;
1981 dist1
= edge2
->opos
- edge1
->opos
;
1982 dist2
= edge3
->opos
- edge2
->opos
;
1984 span
= dist1
- dist2
;
1990 delta
= edge3
->pos
- ( 2 * edge2
->pos
- edge1
->pos
);
1991 edge3
->pos
-= delta
;
1993 edge3
->link
->pos
-= delta
;
1995 /* move the serifs along with the stem */
1996 if ( n_edges
== 12 )
1998 ( edges
+ 8 )->pos
-= delta
;
1999 ( edges
+ 11 )->pos
-= delta
;
2002 edge3
->flags
|= AF_EDGE_DONE
;
2004 edge3
->link
->flags
|= AF_EDGE_DONE
;
2008 if ( has_serifs
|| !anchor
)
2011 * now hint the remaining edges (serifs and single) in order
2012 * to complete our processing
2014 for ( edge
= edges
; edge
< edge_limit
; edge
++ )
2019 if ( edge
->flags
& AF_EDGE_DONE
)
2026 delta
= edge
->serif
->opos
- edge
->opos
;
2031 if ( delta
< 64 + 16 )
2033 af_latin_align_serif_edge( hints
, edge
->serif
, edge
);
2034 AF_LOG(( "SERIF: edge %d (opos=%.2f) serif to %d (opos=%.2f) "
2035 "aligned to (%.2f)\n",
2036 edge
-edges
, edge
->opos
/ 64.0,
2037 edge
->serif
- edges
, edge
->serif
->opos
/ 64.0,
2038 edge
->pos
/ 64.0 ));
2042 AF_LOG(( "SERIF_ANCHOR: edge %d (opos=%.2f) snapped to (%.2f)\n",
2043 edge
-edges
, edge
->opos
/ 64.0, edge
->pos
/ 64.0 ));
2044 edge
->pos
= FT_PIX_ROUND( edge
->opos
);
2049 AF_Edge before
, after
;
2052 for ( before
= edge
- 1; before
>= edges
; before
-- )
2053 if ( before
->flags
& AF_EDGE_DONE
)
2056 for ( after
= edge
+ 1; after
< edge_limit
; after
++ )
2057 if ( after
->flags
& AF_EDGE_DONE
)
2060 if ( before
>= edges
&& before
< edge
&&
2061 after
< edge_limit
&& after
> edge
)
2063 if ( after
->opos
== before
->opos
)
2064 edge
->pos
= before
->pos
;
2066 edge
->pos
= before
->pos
+
2067 FT_MulDiv( edge
->opos
- before
->opos
,
2068 after
->pos
- before
->pos
,
2069 after
->opos
- before
->opos
);
2070 AF_LOG(( "SERIF_LINK1: edge %d (opos=%.2f) snapped to (%.2f) "
2071 "from %d (opos=%.2f)\n",
2072 edge
-edges
, edge
->opos
/ 64.0,
2073 edge
->pos
/ 64.0, before
- edges
,
2074 before
->opos
/ 64.0 ));
2078 edge
->pos
= anchor
->pos
+
2079 ( ( edge
->opos
- anchor
->opos
+ 16 ) & ~31 );
2080 AF_LOG(( "SERIF_LINK2: edge %d (opos=%.2f) snapped to (%.2f)\n",
2081 edge
-edges
, edge
->opos
/ 64.0, edge
->pos
/ 64.0 ));
2085 edge
->flags
|= AF_EDGE_DONE
;
2087 if ( edge
> edges
&& edge
->pos
< edge
[-1].pos
)
2088 edge
->pos
= edge
[-1].pos
;
2090 if ( edge
+ 1 < edge_limit
&&
2091 edge
[1].flags
& AF_EDGE_DONE
&&
2092 edge
->pos
> edge
[1].pos
)
2093 edge
->pos
= edge
[1].pos
;
2100 af_latin_hints_apply( AF_GlyphHints hints
,
2101 FT_Outline
* outline
,
2102 AF_LatinMetrics metrics
)
2108 error
= af_glyph_hints_reload( hints
, outline
, 1 );
2112 /* analyze glyph outline */
2113 #ifdef AF_USE_WARPER
2114 if ( metrics
->root
.scaler
.render_mode
== FT_RENDER_MODE_LIGHT
||
2115 AF_HINTS_DO_HORIZONTAL( hints
) )
2117 if ( AF_HINTS_DO_HORIZONTAL( hints
) )
2120 error
= af_latin_hints_detect_features( hints
, AF_DIMENSION_HORZ
);
2125 if ( AF_HINTS_DO_VERTICAL( hints
) )
2127 error
= af_latin_hints_detect_features( hints
, AF_DIMENSION_VERT
);
2131 af_latin_hints_compute_blue_edges( hints
, metrics
);
2134 /* grid-fit the outline */
2135 for ( dim
= 0; dim
< AF_DIMENSION_MAX
; dim
++ )
2137 #ifdef AF_USE_WARPER
2138 if ( ( dim
== AF_DIMENSION_HORZ
&&
2139 metrics
->root
.scaler
.render_mode
== FT_RENDER_MODE_LIGHT
) )
2141 AF_WarperRec warper
;
2146 af_warper_compute( &warper
, hints
, dim
, &scale
, &delta
);
2147 af_glyph_hints_scale_dim( hints
, dim
, scale
, delta
);
2152 if ( ( dim
== AF_DIMENSION_HORZ
&& AF_HINTS_DO_HORIZONTAL( hints
) ) ||
2153 ( dim
== AF_DIMENSION_VERT
&& AF_HINTS_DO_VERTICAL( hints
) ) )
2155 af_latin_hint_edges( hints
, (AF_Dimension
)dim
);
2156 af_glyph_hints_align_edge_points( hints
, (AF_Dimension
)dim
);
2157 af_glyph_hints_align_strong_points( hints
, (AF_Dimension
)dim
);
2158 af_glyph_hints_align_weak_points( hints
, (AF_Dimension
)dim
);
2161 af_glyph_hints_save( hints
, outline
);
2168 /*************************************************************************/
2169 /*************************************************************************/
2171 /***** L A T I N S C R I P T C L A S S *****/
2173 /*************************************************************************/
2174 /*************************************************************************/
2177 /* XXX: this should probably fine tuned to differentiate better between */
2180 static const AF_Script_UniRangeRec af_latin_uniranges
[] =
2182 AF_UNIRANGE_REC( 0x0020UL
, 0x007FUL
), /* Basic Latin (no control chars) */
2183 AF_UNIRANGE_REC( 0x00A0UL
, 0x00FFUL
), /* Latin-1 Supplement (no control chars) */
2184 AF_UNIRANGE_REC( 0x0100UL
, 0x017FUL
), /* Latin Extended-A */
2185 AF_UNIRANGE_REC( 0x0180UL
, 0x024FUL
), /* Latin Extended-B */
2186 AF_UNIRANGE_REC( 0x0250UL
, 0x02AFUL
), /* IPA Extensions */
2187 AF_UNIRANGE_REC( 0x02B0UL
, 0x02FFUL
), /* Spacing Modifier Letters */
2188 AF_UNIRANGE_REC( 0x0300UL
, 0x036FUL
), /* Combining Diacritical Marks */
2189 AF_UNIRANGE_REC( 0x0370UL
, 0x03FFUL
), /* Greek and Coptic */
2190 AF_UNIRANGE_REC( 0x0400UL
, 0x04FFUL
), /* Cyrillic */
2191 AF_UNIRANGE_REC( 0x0500UL
, 0x052FUL
), /* Cyrillic Supplement */
2192 AF_UNIRANGE_REC( 0x1D00UL
, 0x1D7FUL
), /* Phonetic Extensions */
2193 AF_UNIRANGE_REC( 0x1D80UL
, 0x1DBFUL
), /* Phonetic Extensions Supplement */
2194 AF_UNIRANGE_REC( 0x1DC0UL
, 0x1DFFUL
), /* Combining Diacritical Marks Supplement */
2195 AF_UNIRANGE_REC( 0x1E00UL
, 0x1EFFUL
), /* Latin Extended Additional */
2196 AF_UNIRANGE_REC( 0x1F00UL
, 0x1FFFUL
), /* Greek Extended */
2197 AF_UNIRANGE_REC( 0x2000UL
, 0x206FUL
), /* General Punctuation */
2198 AF_UNIRANGE_REC( 0x2070UL
, 0x209FUL
), /* Superscripts and Subscripts */
2199 AF_UNIRANGE_REC( 0x20A0UL
, 0x20CFUL
), /* Currency Symbols */
2200 AF_UNIRANGE_REC( 0x2150UL
, 0x218FUL
), /* Number Forms */
2201 AF_UNIRANGE_REC( 0x2460UL
, 0x24FFUL
), /* Enclosed Alphanumerics */
2202 AF_UNIRANGE_REC( 0x2C60UL
, 0x2C7FUL
), /* Latin Extended-C */
2203 AF_UNIRANGE_REC( 0x2DE0UL
, 0x2DFFUL
), /* Cyrillic Extended-A */
2204 AF_UNIRANGE_REC( 0xA640UL
, 0xA69FUL
), /* Cyrillic Extended-B */
2205 AF_UNIRANGE_REC( 0xA720UL
, 0xA7FFUL
), /* Latin Extended-D */
2206 AF_UNIRANGE_REC( 0xFB00UL
, 0xFB06UL
), /* Alphab. Present. Forms (Latin Ligs) */
2207 AF_UNIRANGE_REC( 0x1D400UL
, 0x1D7FFUL
), /* Mathematical Alphanumeric Symbols */
2208 AF_UNIRANGE_REC( 0UL, 0UL )
2212 AF_DEFINE_SCRIPT_CLASS(af_latin_script_class
,
2216 sizeof( AF_LatinMetricsRec
),
2218 (AF_Script_InitMetricsFunc
) af_latin_metrics_init
,
2219 (AF_Script_ScaleMetricsFunc
)af_latin_metrics_scale
,
2220 (AF_Script_DoneMetricsFunc
) NULL
,
2222 (AF_Script_InitHintsFunc
) af_latin_hints_init
,
2223 (AF_Script_ApplyHintsFunc
) af_latin_hints_apply