1 /***************************************************************************/
5 /* Auto-fitter hinting routines for latin writing system (body). */
7 /* Copyright 2003-2016 by */
8 /* David Turner, Robert Wilhelm, and Werner Lemberg. */
10 /* This file is part of the FreeType project, and may only be used, */
11 /* modified, and distributed under the terms of the FreeType project */
12 /* license, LICENSE.TXT. By continuing to use, modify, or distribute */
13 /* this file you indicate that you have read the license and */
14 /* understand and accept it fully. */
16 /***************************************************************************/
20 #include FT_ADVANCES_H
21 #include FT_INTERNAL_DEBUG_H
29 #ifdef AF_CONFIG_OPTION_USE_WARPER
34 /*************************************************************************/
36 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
37 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
38 /* messages during execution. */
41 #define FT_COMPONENT trace_aflatin
44 /* needed for computation of round vs. flat segments */
45 #define FLAT_THRESHOLD( x ) ( x / 14 )
48 /*************************************************************************/
49 /*************************************************************************/
51 /***** L A T I N G L O B A L M E T R I C S *****/
53 /*************************************************************************/
54 /*************************************************************************/
57 /* Find segments and links, compute all stem widths, and initialize */
58 /* standard width and height for the glyph with given charcode. */
61 af_latin_metrics_init_widths( AF_LatinMetrics metrics
,
64 /* scan the array of segments in each direction */
66 AF_GlyphHintsRec
*hints
= malloc(sizeof(AF_GlyphHintsRec
));
68 AF_GlyphHintsRec hints
[1];
73 "latin standard widths computation (style `%s')\n"
74 "=====================================================\n"
76 af_style_names
[metrics
->root
.style_class
->style
] ));
78 af_glyph_hints_init( hints
, face
->memory
);
80 metrics
->axis
[AF_DIMENSION_HORZ
].width_count
= 0;
81 metrics
->axis
[AF_DIMENSION_VERT
].width_count
= 0;
88 AF_LatinMetricsRec
*dummy
= malloc(sizeof(AF_LatinMetricsRec
));
90 AF_LatinMetricsRec dummy
[1];
92 AF_Scaler scaler
= &dummy
->root
.scaler
;
94 #ifdef FT_CONFIG_OPTION_PIC
95 AF_FaceGlobals globals
= metrics
->root
.globals
;
98 AF_StyleClass style_class
= metrics
->root
.style_class
;
99 AF_ScriptClass script_class
= AF_SCRIPT_CLASSES_GET
100 [style_class
->script
];
105 #ifdef FT_DEBUG_LEVEL_TRACE
109 p
= script_class
->standard_charstring
;
110 shaper_buf
= af_shaper_buf_create( face
);
113 * We check a list of standard characters to catch features like
114 * `c2sc' (small caps from caps) that don't contain lowercase letters
115 * by definition, or other features that mainly operate on numerals.
116 * The first match wins.
122 unsigned int num_idx
;
124 #ifdef FT_DEBUG_LEVEL_TRACE
132 #ifdef FT_DEBUG_LEVEL_TRACE
134 GET_UTF8_CHAR( ch
, p_old
);
137 /* reject input that maps to more than a single glyph */
138 p
= af_shaper_get_cluster( p
, &metrics
->root
, shaper_buf
, &num_idx
);
142 /* otherwise exit loop if we have a result */
143 glyph_index
= af_shaper_get_elem( &metrics
->root
,
152 af_shaper_buf_destroy( face
, shaper_buf
);
157 FT_TRACE5(( "standard character: U+%04lX (glyph index %d)\n",
160 error
= FT_Load_Glyph( face
, glyph_index
, FT_LOAD_NO_SCALE
);
161 if ( error
|| face
->glyph
->outline
.n_points
<= 0 )
166 dummy
->units_per_em
= metrics
->units_per_em
;
168 scaler
->x_scale
= 0x10000L
;
169 scaler
->y_scale
= 0x10000L
;
174 scaler
->render_mode
= FT_RENDER_MODE_NORMAL
;
177 af_glyph_hints_rescale( hints
, (AF_StyleMetrics
)dummy
);
179 error
= af_glyph_hints_reload( hints
, &face
->glyph
->outline
);
183 for ( dim
= 0; dim
< AF_DIMENSION_MAX
; dim
++ )
185 AF_LatinAxis axis
= &metrics
->axis
[dim
];
186 AF_AxisHints axhints
= &hints
->axis
[dim
];
187 AF_Segment seg
, limit
, link
;
188 FT_UInt num_widths
= 0;
191 error
= af_latin_hints_compute_segments( hints
,
197 * We assume that the glyphs selected for the stem width
198 * computation are `featureless' enough so that the linking
199 * algorithm works fine without adjustments of its scoring
202 af_latin_hints_link_segments( hints
,
207 seg
= axhints
->segments
;
208 limit
= seg
+ axhints
->num_segments
;
210 for ( ; seg
< limit
; seg
++ )
214 /* we only consider stem segments there! */
215 if ( link
&& link
->link
== seg
&& link
> seg
)
220 dist
= seg
->pos
- link
->pos
;
224 if ( num_widths
< AF_LATIN_MAX_WIDTHS
)
225 axis
->widths
[num_widths
++].org
= dist
;
229 /* this also replaces multiple almost identical stem widths */
230 /* with a single one (the value 100 is heuristic) */
231 af_sort_and_quantize_widths( &num_widths
, axis
->widths
,
232 dummy
->units_per_em
/ 100 );
233 axis
->width_count
= num_widths
;
237 for ( dim
= 0; dim
< AF_DIMENSION_MAX
; dim
++ )
239 AF_LatinAxis axis
= &metrics
->axis
[dim
];
243 stdw
= ( axis
->width_count
> 0 ) ? axis
->widths
[0].org
244 : AF_LATIN_CONSTANT( metrics
, 50 );
246 /* let's try 20% of the smallest width */
247 axis
->edge_distance_threshold
= stdw
/ 5;
248 axis
->standard_width
= stdw
;
249 axis
->extra_light
= 0;
251 #ifdef FT_DEBUG_LEVEL_TRACE
256 FT_TRACE5(( "%s widths:\n",
257 dim
== AF_DIMENSION_VERT
? "horizontal"
260 FT_TRACE5(( " %d (standard)", axis
->standard_width
));
261 for ( i
= 1; i
< axis
->width_count
; i
++ )
262 FT_TRACE5(( " %d", axis
->widths
[i
].org
));
275 af_glyph_hints_done( hints
);
284 /* Find all blue zones. Flat segments give the reference points, */
285 /* round segments the overshoot positions. */
288 af_latin_metrics_init_blues( AF_LatinMetrics metrics
,
291 FT_Pos flats
[AF_BLUE_STRING_MAX_LEN
];
292 FT_Pos rounds
[AF_BLUE_STRING_MAX_LEN
];
299 AF_LatinAxis axis
= &metrics
->axis
[AF_DIMENSION_VERT
];
302 AF_StyleClass sc
= metrics
->root
.style_class
;
304 AF_Blue_Stringset bss
= sc
->blue_stringset
;
305 const AF_Blue_StringRec
* bs
= &af_blue_stringsets
[bss
];
307 FT_Pos flat_threshold
= FLAT_THRESHOLD( metrics
->units_per_em
);
312 /* we walk over the blue character strings as specified in the */
313 /* style's entry in the `af_blue_stringset' array */
315 FT_TRACE5(( "latin blue zones computation\n"
316 "============================\n"
319 shaper_buf
= af_shaper_buf_create( face
);
321 for ( ; bs
->string
!= AF_BLUE_STRING_MAX
; bs
++ )
323 const char* p
= &af_blue_strings
[bs
->string
];
330 #ifdef FT_DEBUG_LEVEL_TRACE
332 FT_Bool have_flag
= 0;
335 FT_TRACE5(( "blue zone %d", axis
->blue_count
));
337 if ( bs
->properties
)
341 if ( AF_LATIN_IS_TOP_BLUE( bs
) )
343 FT_TRACE5(( "top" ));
346 else if ( AF_LATIN_IS_SUB_TOP_BLUE( bs
) )
348 FT_TRACE5(( "sub top" ));
352 if ( AF_LATIN_IS_NEUTRAL_BLUE( bs
) )
356 FT_TRACE5(( "neutral" ));
360 if ( AF_LATIN_IS_X_HEIGHT_BLUE( bs
) )
364 FT_TRACE5(( "small top" ));
368 if ( AF_LATIN_IS_LONG_BLUE( bs
) )
372 FT_TRACE5(( "long" ));
378 FT_TRACE5(( ":\n" ));
380 #endif /* FT_DEBUG_LEVEL_TRACE */
389 FT_ULong glyph_index
;
391 FT_Int best_point
, best_contour_first
, best_contour_last
;
394 FT_Pos best_y_extremum
; /* same as points.y */
395 FT_Bool best_round
= 0;
397 unsigned int i
, num_idx
;
399 #ifdef FT_DEBUG_LEVEL_TRACE
408 #ifdef FT_DEBUG_LEVEL_TRACE
410 GET_UTF8_CHAR( ch
, p_old
);
413 p
= af_shaper_get_cluster( p
, &metrics
->root
, shaper_buf
, &num_idx
);
417 FT_TRACE5(( " U+%04lX unavailable\n", ch
));
421 if ( AF_LATIN_IS_TOP_BLUE( bs
) )
422 best_y_extremum
= FT_INT_MIN
;
424 best_y_extremum
= FT_INT_MAX
;
426 /* iterate over all glyph elements of the character cluster */
427 /* and get the data of the `biggest' one */
428 for ( i
= 0; i
< num_idx
; i
++ )
434 /* load the character in the face -- skip unknown or empty ones */
435 glyph_index
= af_shaper_get_elem( &metrics
->root
,
440 if ( glyph_index
== 0 )
442 FT_TRACE5(( " U+%04lX unavailable\n", ch
));
446 error
= FT_Load_Glyph( face
, glyph_index
, FT_LOAD_NO_SCALE
);
447 outline
= face
->glyph
->outline
;
448 /* reject glyphs that don't produce any rendering */
449 if ( error
|| outline
.n_points
<= 2 )
451 #ifdef FT_DEBUG_LEVEL_TRACE
453 FT_TRACE5(( " U+%04lX contains no (usable) outlines\n", ch
));
455 FT_TRACE5(( " component %d of cluster starting with U+%04lX"
456 " contains no (usable) outlines\n", i
, ch
));
461 /* now compute min or max point indices and coordinates */
462 points
= outline
.points
;
464 best_y
= 0; /* make compiler happy */
465 best_contour_first
= 0; /* ditto */
466 best_contour_last
= 0; /* ditto */
474 for ( nn
= 0; nn
< outline
.n_contours
; first
= last
+ 1, nn
++ )
476 FT_Int old_best_point
= best_point
;
480 last
= outline
.contours
[nn
];
482 /* Avoid single-point contours since they are never */
483 /* rasterized. In some fonts, they correspond to mark */
484 /* attachment points that are way outside of the glyph's */
489 if ( AF_LATIN_IS_TOP_BLUE( bs
) ||
490 AF_LATIN_IS_SUB_TOP_BLUE( bs
) )
492 for ( pp
= first
; pp
<= last
; pp
++ )
494 if ( best_point
< 0 || points
[pp
].y
> best_y
)
497 best_y
= points
[pp
].y
;
498 ascender
= FT_MAX( ascender
, best_y
+ y_offset
);
501 descender
= FT_MIN( descender
, points
[pp
].y
+ y_offset
);
506 for ( pp
= first
; pp
<= last
; pp
++ )
508 if ( best_point
< 0 || points
[pp
].y
< best_y
)
511 best_y
= points
[pp
].y
;
512 descender
= FT_MIN( descender
, best_y
+ y_offset
);
515 ascender
= FT_MAX( ascender
, points
[pp
].y
+ y_offset
);
519 if ( best_point
!= old_best_point
)
521 best_contour_first
= first
;
522 best_contour_last
= last
;
527 /* now check whether the point belongs to a straight or round */
528 /* segment; we first need to find in which contour the extremum */
529 /* lies, then inspect its previous and next points */
530 if ( best_point
>= 0 )
532 FT_Pos best_x
= points
[best_point
].x
;
534 FT_Int best_segment_first
, best_segment_last
;
535 FT_Int best_on_point_first
, best_on_point_last
;
539 best_segment_first
= best_point
;
540 best_segment_last
= best_point
;
542 if ( FT_CURVE_TAG( outline
.tags
[best_point
] ) == FT_CURVE_TAG_ON
)
544 best_on_point_first
= best_point
;
545 best_on_point_last
= best_point
;
549 best_on_point_first
= -1;
550 best_on_point_last
= -1;
553 /* look for the previous and next points on the contour */
554 /* that are not on the same Y coordinate, then threshold */
555 /* the `closeness'... */
561 if ( prev
> best_contour_first
)
564 prev
= best_contour_last
;
566 dist
= FT_ABS( points
[prev
].y
- best_y
);
567 /* accept a small distance or a small angle (both values are */
568 /* heuristic; value 20 corresponds to approx. 2.9 degrees) */
570 if ( FT_ABS( points
[prev
].x
- best_x
) <= 20 * dist
)
573 best_segment_first
= prev
;
575 if ( FT_CURVE_TAG( outline
.tags
[prev
] ) == FT_CURVE_TAG_ON
)
577 best_on_point_first
= prev
;
578 if ( best_on_point_last
< 0 )
579 best_on_point_last
= prev
;
582 } while ( prev
!= best_point
);
586 if ( next
< best_contour_last
)
589 next
= best_contour_first
;
591 dist
= FT_ABS( points
[next
].y
- best_y
);
593 if ( FT_ABS( points
[next
].x
- best_x
) <= 20 * dist
)
596 best_segment_last
= next
;
598 if ( FT_CURVE_TAG( outline
.tags
[next
] ) == FT_CURVE_TAG_ON
)
600 best_on_point_last
= next
;
601 if ( best_on_point_first
< 0 )
602 best_on_point_first
= next
;
605 } while ( next
!= best_point
);
607 if ( AF_LATIN_IS_LONG_BLUE( bs
) )
609 /* If this flag is set, we have an additional constraint to */
610 /* get the blue zone distance: Find a segment of the topmost */
611 /* (or bottommost) contour that is longer than a heuristic */
612 /* threshold. This ensures that small bumps in the outline */
613 /* are ignored (for example, the `vertical serifs' found in */
614 /* many Hebrew glyph designs). */
616 /* If this segment is long enough, we are done. Otherwise, */
617 /* search the segment next to the extremum that is long */
618 /* enough, has the same direction, and a not too large */
619 /* vertical distance from the extremum. Note that the */
620 /* algorithm doesn't check whether the found segment is */
621 /* actually the one (vertically) nearest to the extremum. */
623 /* heuristic threshold value */
624 FT_Pos length_threshold
= metrics
->units_per_em
/ 25;
627 dist
= FT_ABS( points
[best_segment_last
].x
-
628 points
[best_segment_first
].x
);
630 if ( dist
< length_threshold
&&
631 best_segment_last
- best_segment_first
+ 2 <=
632 best_contour_last
- best_contour_first
)
634 /* heuristic threshold value */
635 FT_Pos height_threshold
= metrics
->units_per_em
/ 4;
641 /* we intentionally declare these two variables */
642 /* outside of the loop since various compilers emit */
643 /* incorrect warning messages otherwise, talking about */
644 /* `possibly uninitialized variables' */
645 FT_Int p_first
= 0; /* make compiler happy */
651 /* compute direction */
656 if ( prev
> best_contour_first
)
659 prev
= best_contour_last
;
661 if ( points
[prev
].x
!= best_x
)
664 } while ( prev
!= best_point
);
666 /* skip glyph for the degenerate case */
667 if ( prev
== best_point
)
670 left2right
= FT_BOOL( points
[prev
].x
< points
[best_point
].x
);
672 first
= best_segment_last
;
684 /* no hit; adjust first point */
687 /* also adjust first and last on point */
688 if ( FT_CURVE_TAG( outline
.tags
[first
] ) ==
703 if ( last
< best_contour_last
)
706 last
= best_contour_first
;
708 if ( FT_ABS( best_y
- points
[first
].y
) > height_threshold
)
710 /* vertical distance too large */
715 /* same test as above */
716 dist
= FT_ABS( points
[last
].y
- points
[first
].y
);
718 if ( FT_ABS( points
[last
].x
- points
[first
].x
) <=
725 if ( FT_CURVE_TAG( outline
.tags
[last
] ) == FT_CURVE_TAG_ON
)
732 l2r
= FT_BOOL( points
[first
].x
< points
[last
].x
);
733 d
= FT_ABS( points
[last
].x
- points
[first
].x
);
735 if ( l2r
== left2right
&&
736 d
>= length_threshold
)
738 /* all constraints are met; update segment after */
739 /* finding its end */
742 if ( last
< best_contour_last
)
745 last
= best_contour_first
;
747 d
= FT_ABS( points
[last
].y
- points
[first
].y
);
749 if ( FT_ABS( points
[next
].x
- points
[first
].x
) <=
752 if ( last
> best_contour_first
)
755 last
= best_contour_last
;
761 if ( FT_CURVE_TAG( outline
.tags
[last
] ) ==
769 } while ( last
!= best_segment_first
);
771 best_y
= points
[first
].y
;
773 best_segment_first
= first
;
774 best_segment_last
= last
;
776 best_on_point_first
= p_first
;
777 best_on_point_last
= p_last
;
782 } while ( last
!= best_segment_first
);
786 /* for computing blue zones, we add the y offset as returned */
787 /* by the currently used OpenType feature -- for example, */
788 /* superscript glyphs might be identical to subscript glyphs */
789 /* with a vertical shift */
792 #ifdef FT_DEBUG_LEVEL_TRACE
794 FT_TRACE5(( " U+%04lX: best_y = %5ld", ch
, best_y
));
796 FT_TRACE5(( " component %d of cluster starting with U+%04lX:"
797 " best_y = %5ld", i
, ch
, best_y
));
800 /* now set the `round' flag depending on the segment's kind: */
802 /* - if the horizontal distance between the first and last */
803 /* `on' point is larger than a heuristic threshold */
804 /* we have a flat segment */
805 /* - if either the first or the last point of the segment is */
806 /* an `off' point, the segment is round, otherwise it is */
808 if ( best_on_point_first
>= 0 &&
809 best_on_point_last
>= 0 &&
810 ( FT_ABS( points
[best_on_point_last
].x
-
811 points
[best_on_point_first
].x
) ) >
816 FT_CURVE_TAG( outline
.tags
[best_segment_first
] ) !=
818 FT_CURVE_TAG( outline
.tags
[best_segment_last
] ) !=
821 if ( round
&& AF_LATIN_IS_NEUTRAL_BLUE( bs
) )
823 /* only use flat segments for a neutral blue zone */
824 FT_TRACE5(( " (round, skipped)\n" ));
828 FT_TRACE5(( " (%s)\n", round
? "round" : "flat" ));
831 if ( AF_LATIN_IS_TOP_BLUE( bs
) )
833 if ( best_y
> best_y_extremum
)
835 best_y_extremum
= best_y
;
841 if ( best_y
< best_y_extremum
)
843 best_y_extremum
= best_y
;
850 if ( !( best_y_extremum
== FT_INT_MIN
||
851 best_y_extremum
== FT_INT_MAX
) )
854 rounds
[num_rounds
++] = best_y_extremum
;
856 flats
[num_flats
++] = best_y_extremum
;
859 } /* end while loop */
861 if ( num_flats
== 0 && num_rounds
== 0 )
864 * we couldn't find a single glyph to compute this blue zone,
865 * we will simply ignore it then
867 FT_TRACE5(( " empty\n" ));
871 /* we have computed the contents of the `rounds' and `flats' tables, */
872 /* now determine the reference and overshoot position of the blue -- */
873 /* we simply take the median value after a simple sort */
874 af_sort_pos( num_rounds
, rounds
);
875 af_sort_pos( num_flats
, flats
);
877 blue
= &axis
->blues
[axis
->blue_count
];
878 blue_ref
= &blue
->ref
.org
;
879 blue_shoot
= &blue
->shoot
.org
;
883 if ( num_flats
== 0 )
886 *blue_shoot
= rounds
[num_rounds
/ 2];
888 else if ( num_rounds
== 0 )
891 *blue_shoot
= flats
[num_flats
/ 2];
895 *blue_ref
= flats
[num_flats
/ 2];
896 *blue_shoot
= rounds
[num_rounds
/ 2];
899 /* there are sometimes problems: if the overshoot position of top */
900 /* zones is under its reference position, or the opposite for bottom */
901 /* zones. We must thus check everything there and correct the errors */
902 if ( *blue_shoot
!= *blue_ref
)
904 FT_Pos ref
= *blue_ref
;
905 FT_Pos shoot
= *blue_shoot
;
906 FT_Bool over_ref
= FT_BOOL( shoot
> ref
);
909 if ( ( AF_LATIN_IS_TOP_BLUE( bs
) ||
910 AF_LATIN_IS_SUB_TOP_BLUE( bs
) ) ^ over_ref
)
913 *blue_shoot
= ( shoot
+ ref
) / 2;
915 FT_TRACE5(( " [overshoot smaller than reference,"
916 " taking mean value]\n" ));
920 blue
->ascender
= ascender
;
921 blue
->descender
= descender
;
924 if ( AF_LATIN_IS_TOP_BLUE( bs
) )
925 blue
->flags
|= AF_LATIN_BLUE_TOP
;
926 if ( AF_LATIN_IS_SUB_TOP_BLUE( bs
) )
927 blue
->flags
|= AF_LATIN_BLUE_SUB_TOP
;
928 if ( AF_LATIN_IS_NEUTRAL_BLUE( bs
) )
929 blue
->flags
|= AF_LATIN_BLUE_NEUTRAL
;
932 * The following flag is used later to adjust the y and x scales
933 * in order to optimize the pixel grid alignment of the top of small
936 if ( AF_LATIN_IS_X_HEIGHT_BLUE( bs
) )
937 blue
->flags
|= AF_LATIN_BLUE_ADJUSTMENT
;
939 FT_TRACE5(( " -> reference = %ld\n"
940 " overshoot = %ld\n",
941 *blue_ref
, *blue_shoot
));
945 af_shaper_buf_destroy( face
, shaper_buf
);
953 /* Check whether all ASCII digits have the same advance width. */
956 af_latin_metrics_check_digits( AF_LatinMetrics metrics
,
959 FT_Bool started
= 0, same_width
= 1;
960 FT_Fixed advance
, old_advance
= 0;
964 /* in all supported charmaps, digits have character codes 0x30-0x39 */
965 const char digits
[] = "0 1 2 3 4 5 6 7 8 9";
970 shaper_buf
= af_shaper_buf_create( face
);
974 FT_ULong glyph_index
;
975 unsigned int num_idx
;
978 /* reject input that maps to more than a single glyph */
979 p
= af_shaper_get_cluster( p
, &metrics
->root
, shaper_buf
, &num_idx
);
983 glyph_index
= af_shaper_get_elem( &metrics
->root
,
993 if ( advance
!= old_advance
)
1001 old_advance
= advance
;
1006 af_shaper_buf_destroy( face
, shaper_buf
);
1008 metrics
->root
.digits_have_same_width
= same_width
;
1012 /* Initialize global metrics. */
1014 FT_LOCAL_DEF( FT_Error
)
1015 af_latin_metrics_init( AF_LatinMetrics metrics
,
1018 FT_CharMap oldmap
= face
->charmap
;
1021 metrics
->units_per_em
= face
->units_per_EM
;
1023 if ( !FT_Select_Charmap( face
, FT_ENCODING_UNICODE
) )
1025 af_latin_metrics_init_widths( metrics
, face
);
1026 af_latin_metrics_init_blues( metrics
, face
);
1027 af_latin_metrics_check_digits( metrics
, face
);
1030 FT_Set_Charmap( face
, oldmap
);
1035 /* Adjust scaling value, then scale and shift widths */
1036 /* and blue zones (if applicable) for given dimension. */
1039 af_latin_metrics_scale_dim( AF_LatinMetrics metrics
,
1049 if ( dim
== AF_DIMENSION_HORZ
)
1051 scale
= scaler
->x_scale
;
1052 delta
= scaler
->x_delta
;
1056 scale
= scaler
->y_scale
;
1057 delta
= scaler
->y_delta
;
1060 axis
= &metrics
->axis
[dim
];
1062 if ( axis
->org_scale
== scale
&& axis
->org_delta
== delta
)
1065 axis
->org_scale
= scale
;
1066 axis
->org_delta
= delta
;
1069 * correct X and Y scale to optimize the alignment of the top of small
1070 * letters to the pixel grid
1073 AF_LatinAxis Axis
= &metrics
->axis
[AF_DIMENSION_VERT
];
1074 AF_LatinBlue blue
= NULL
;
1077 for ( nn
= 0; nn
< Axis
->blue_count
; nn
++ )
1079 if ( Axis
->blues
[nn
].flags
& AF_LATIN_BLUE_ADJUSTMENT
)
1081 blue
= &Axis
->blues
[nn
];
1095 scaled
= FT_MulFix( blue
->shoot
.org
, scaler
->y_scale
);
1096 ppem
= metrics
->root
.scaler
.face
->size
->metrics
.x_ppem
;
1097 limit
= metrics
->root
.globals
->increase_x_height
;
1100 /* if the `increase-x-height' property is active, */
1101 /* we round up much more often */
1104 ppem
>= AF_PROP_INCREASE_X_HEIGHT_MIN
)
1107 fitted
= ( scaled
+ threshold
) & ~63;
1109 if ( scaled
!= fitted
)
1112 if ( dim
== AF_DIMENSION_HORZ
)
1114 if ( fitted
< scaled
)
1115 scale
-= scale
/ 50; /* scale *= 0.98 */
1119 if ( dim
== AF_DIMENSION_VERT
)
1126 new_scale
= FT_MulDiv( scale
, fitted
, scaled
);
1128 /* the scaling should not change the result by more than two pixels */
1129 max_height
= metrics
->units_per_em
;
1131 for ( nn
= 0; nn
< Axis
->blue_count
; nn
++ )
1133 max_height
= FT_MAX( max_height
, Axis
->blues
[nn
].ascender
);
1134 max_height
= FT_MAX( max_height
, -Axis
->blues
[nn
].descender
);
1137 dist
= FT_ABS( FT_MulFix( max_height
, new_scale
- scale
) );
1145 "af_latin_metrics_scale_dim:"
1146 " x height alignment (style `%s'):\n"
1148 " vertical scaling changed from %.4f to %.4f (by %d%%)\n"
1150 af_style_names
[metrics
->root
.style_class
->style
],
1151 axis
->org_scale
/ 65536.0,
1153 ( fitted
- scaled
) * 100 / scaled
));
1155 #ifdef FT_DEBUG_LEVEL_TRACE
1159 "af_latin_metrics_scale_dim:"
1160 " x height alignment (style `%s'):\n"
1162 " excessive vertical scaling abandoned\n"
1164 af_style_names
[metrics
->root
.style_class
->style
] ));
1172 axis
->scale
= scale
;
1173 axis
->delta
= delta
;
1175 if ( dim
== AF_DIMENSION_HORZ
)
1177 metrics
->root
.scaler
.x_scale
= scale
;
1178 metrics
->root
.scaler
.x_delta
= delta
;
1182 metrics
->root
.scaler
.y_scale
= scale
;
1183 metrics
->root
.scaler
.y_delta
= delta
;
1186 FT_TRACE5(( "%s widths (style `%s')\n",
1187 dim
== AF_DIMENSION_HORZ
? "horizontal" : "vertical",
1188 af_style_names
[metrics
->root
.style_class
->style
] ));
1190 /* scale the widths */
1191 for ( nn
= 0; nn
< axis
->width_count
; nn
++ )
1193 AF_Width width
= axis
->widths
+ nn
;
1196 width
->cur
= FT_MulFix( width
->org
, scale
);
1197 width
->fit
= width
->cur
;
1199 FT_TRACE5(( " %d scaled to %.2f\n",
1201 width
->cur
/ 64.0 ));
1204 FT_TRACE5(( "\n" ));
1206 /* an extra-light axis corresponds to a standard width that is */
1207 /* smaller than 5/8 pixels */
1209 (FT_Bool
)( FT_MulFix( axis
->standard_width
, scale
) < 32 + 8 );
1211 #ifdef FT_DEBUG_LEVEL_TRACE
1212 if ( axis
->extra_light
)
1213 FT_TRACE5(( "`%s' style is extra light (at current resolution)\n"
1215 af_style_names
[metrics
->root
.style_class
->style
] ));
1218 if ( dim
== AF_DIMENSION_VERT
)
1220 #ifdef FT_DEBUG_LEVEL_TRACE
1221 if ( axis
->blue_count
)
1222 FT_TRACE5(( "blue zones (style `%s')\n",
1223 af_style_names
[metrics
->root
.style_class
->style
] ));
1226 /* scale the blue zones */
1227 for ( nn
= 0; nn
< axis
->blue_count
; nn
++ )
1229 AF_LatinBlue blue
= &axis
->blues
[nn
];
1233 blue
->ref
.cur
= FT_MulFix( blue
->ref
.org
, scale
) + delta
;
1234 blue
->ref
.fit
= blue
->ref
.cur
;
1235 blue
->shoot
.cur
= FT_MulFix( blue
->shoot
.org
, scale
) + delta
;
1236 blue
->shoot
.fit
= blue
->shoot
.cur
;
1237 blue
->flags
&= ~AF_LATIN_BLUE_ACTIVE
;
1239 /* a blue zone is only active if it is less than 3/4 pixels tall */
1240 dist
= FT_MulFix( blue
->ref
.org
- blue
->shoot
.org
, scale
);
1241 if ( dist
<= 48 && dist
>= -48 )
1249 /* use discrete values for blue zone widths */
1253 /* generic, original code */
1254 delta1
= blue
->shoot
.org
- blue
->ref
.org
;
1259 delta2
= FT_MulFix( delta2
, scale
);
1263 else if ( delta2
< 64 )
1264 delta2
= 32 + ( ( ( delta2
- 32 ) + 16 ) & ~31 );
1266 delta2
= FT_PIX_ROUND( delta2
);
1271 blue
->ref
.fit
= FT_PIX_ROUND( blue
->ref
.cur
);
1272 blue
->shoot
.fit
= blue
->ref
.fit
+ delta2
;
1276 /* simplified version due to abs(dist) <= 48 */
1283 else if ( delta2
< 48 )
1291 blue
->ref
.fit
= FT_PIX_ROUND( blue
->ref
.cur
);
1292 blue
->shoot
.fit
= blue
->ref
.fit
- delta2
;
1296 blue
->flags
|= AF_LATIN_BLUE_ACTIVE
;
1300 /* use sub-top blue zone only if it doesn't overlap with */
1301 /* another (non-sup-top) blue zone; otherwise, the */
1302 /* effect would be similar to a neutral blue zone, which */
1303 /* is not desired here */
1304 for ( nn
= 0; nn
< axis
->blue_count
; nn
++ )
1306 AF_LatinBlue blue
= &axis
->blues
[nn
];
1310 if ( !( blue
->flags
& AF_LATIN_BLUE_SUB_TOP
) )
1312 if ( !( blue
->flags
& AF_LATIN_BLUE_ACTIVE
) )
1315 for ( i
= 0; i
< axis
->blue_count
; i
++ )
1317 AF_LatinBlue b
= &axis
->blues
[i
];
1320 if ( b
->flags
& AF_LATIN_BLUE_SUB_TOP
)
1322 if ( !( b
->flags
& AF_LATIN_BLUE_ACTIVE
) )
1325 if ( b
->ref
.fit
<= blue
->shoot
.fit
&&
1326 b
->shoot
.fit
>= blue
->ref
.fit
)
1328 blue
->flags
&= ~AF_LATIN_BLUE_ACTIVE
;
1334 #ifdef FT_DEBUG_LEVEL_TRACE
1335 for ( nn
= 0; nn
< axis
->blue_count
; nn
++ )
1337 AF_LatinBlue blue
= &axis
->blues
[nn
];
1340 FT_TRACE5(( " reference %d: %d scaled to %.2f%s\n"
1341 " overshoot %d: %d scaled to %.2f%s\n",
1344 blue
->ref
.fit
/ 64.0,
1345 blue
->flags
& AF_LATIN_BLUE_ACTIVE
? ""
1349 blue
->shoot
.fit
/ 64.0,
1350 blue
->flags
& AF_LATIN_BLUE_ACTIVE
? ""
1358 /* Scale global values in both directions. */
1360 FT_LOCAL_DEF( void )
1361 af_latin_metrics_scale( AF_LatinMetrics metrics
,
1364 metrics
->root
.scaler
.render_mode
= scaler
->render_mode
;
1365 metrics
->root
.scaler
.face
= scaler
->face
;
1366 metrics
->root
.scaler
.flags
= scaler
->flags
;
1368 af_latin_metrics_scale_dim( metrics
, scaler
, AF_DIMENSION_HORZ
);
1369 af_latin_metrics_scale_dim( metrics
, scaler
, AF_DIMENSION_VERT
);
1373 /* Extract standard_width from writing system/script specific */
1374 /* metrics class. */
1376 FT_LOCAL_DEF( void )
1377 af_latin_get_standard_widths( AF_LatinMetrics metrics
,
1382 *stdHW
= metrics
->axis
[AF_DIMENSION_VERT
].standard_width
;
1385 *stdVW
= metrics
->axis
[AF_DIMENSION_HORZ
].standard_width
;
1389 /*************************************************************************/
1390 /*************************************************************************/
1392 /***** L A T I N G L Y P H A N A L Y S I S *****/
1394 /*************************************************************************/
1395 /*************************************************************************/
1398 /* Walk over all contours and compute its segments. */
1400 FT_LOCAL_DEF( FT_Error
)
1401 af_latin_hints_compute_segments( AF_GlyphHints hints
,
1404 AF_LatinMetrics metrics
= (AF_LatinMetrics
)hints
->metrics
;
1405 AF_AxisHints axis
= &hints
->axis
[dim
];
1406 FT_Memory memory
= hints
->memory
;
1407 FT_Error error
= FT_Err_Ok
;
1408 AF_Segment segment
= NULL
;
1410 AF_Point
* contour
= hints
->contours
;
1411 AF_Point
* contour_limit
= contour
+ hints
->num_contours
;
1412 AF_Direction major_dir
, segment_dir
;
1414 FT_Pos flat_threshold
= FLAT_THRESHOLD( metrics
->units_per_em
);
1419 seg0
.flags
= AF_EDGE_NORMAL
;
1421 major_dir
= (AF_Direction
)FT_ABS( axis
->major_dir
);
1422 segment_dir
= major_dir
;
1424 axis
->num_segments
= 0;
1426 /* set up (u,v) in each point */
1427 if ( dim
== AF_DIMENSION_HORZ
)
1429 AF_Point point
= hints
->points
;
1430 AF_Point limit
= point
+ hints
->num_points
;
1433 for ( ; point
< limit
; point
++ )
1435 point
->u
= point
->fx
;
1436 point
->v
= point
->fy
;
1441 AF_Point point
= hints
->points
;
1442 AF_Point limit
= point
+ hints
->num_points
;
1445 for ( ; point
< limit
; point
++ )
1447 point
->u
= point
->fy
;
1448 point
->v
= point
->fx
;
1452 /* do each contour separately */
1453 for ( ; contour
< contour_limit
; contour
++ )
1455 AF_Point point
= contour
[0];
1456 AF_Point last
= point
->prev
;
1458 FT_Pos min_pos
= 32000; /* minimum segment pos != min_coord */
1459 FT_Pos max_pos
= -32000; /* maximum segment pos != max_coord */
1460 FT_Pos min_on_pos
= 32000;
1461 FT_Pos max_on_pos
= -32000;
1465 if ( point
== last
) /* skip singletons -- just in case */
1468 if ( FT_ABS( last
->out_dir
) == major_dir
&&
1469 FT_ABS( point
->out_dir
) == major_dir
)
1471 /* we are already on an edge, try to locate its start */
1476 point
= point
->prev
;
1477 if ( FT_ABS( point
->out_dir
) != major_dir
)
1479 point
= point
->next
;
1482 if ( point
== last
)
1503 /* get minimum and maximum coordinate of on points */
1504 if ( !( point
->flags
& AF_FLAG_CONTROL
) )
1507 if ( v
< min_on_pos
)
1509 if ( v
> max_on_pos
)
1513 if ( point
->out_dir
!= segment_dir
|| point
== last
)
1515 /* we are just leaving an edge; record a new segment! */
1516 segment
->last
= point
;
1517 segment
->pos
= (FT_Short
)( ( min_pos
+ max_pos
) >> 1 );
1519 /* a segment is round if either its first or last point */
1520 /* is a control point, and the length of the on points */
1521 /* inbetween doesn't exceed a heuristic limit */
1522 if ( ( segment
->first
->flags
| point
->flags
) & AF_FLAG_CONTROL
&&
1523 ( max_on_pos
- min_on_pos
) < flat_threshold
)
1524 segment
->flags
|= AF_EDGE_ROUND
;
1526 /* compute segment size */
1527 min_pos
= max_pos
= point
->v
;
1529 v
= segment
->first
->v
;
1535 segment
->min_coord
= (FT_Short
)min_pos
;
1536 segment
->max_coord
= (FT_Short
)max_pos
;
1537 segment
->height
= (FT_Short
)( segment
->max_coord
-
1538 segment
->min_coord
);
1546 /* now exit if we are at the start/end point */
1547 if ( point
== last
)
1554 if ( !on_edge
&& FT_ABS( point
->out_dir
) == major_dir
)
1556 /* this is the start of a new segment! */
1557 segment_dir
= (AF_Direction
)point
->out_dir
;
1559 error
= af_axis_hints_new_segment( axis
, memory
, &segment
);
1563 /* clear all segment fields */
1566 segment
->dir
= (FT_Char
)segment_dir
;
1567 segment
->first
= point
;
1568 segment
->last
= point
;
1570 min_pos
= max_pos
= point
->u
;
1572 if ( point
->flags
& AF_FLAG_CONTROL
)
1575 max_on_pos
= -32000;
1578 min_on_pos
= max_on_pos
= point
->v
;
1583 point
= point
->next
;
1589 /* now slightly increase the height of segments if this makes */
1590 /* sense -- this is used to better detect and ignore serifs */
1592 AF_Segment segments
= axis
->segments
;
1593 AF_Segment segments_end
= segments
+ axis
->num_segments
;
1596 for ( segment
= segments
; segment
< segments_end
; segment
++ )
1598 AF_Point first
= segment
->first
;
1599 AF_Point last
= segment
->last
;
1600 FT_Pos first_v
= first
->v
;
1601 FT_Pos last_v
= last
->v
;
1604 if ( first_v
< last_v
)
1610 if ( p
->v
< first_v
)
1611 segment
->height
= (FT_Short
)( segment
->height
+
1612 ( ( first_v
- p
->v
) >> 1 ) );
1615 if ( p
->v
> last_v
)
1616 segment
->height
= (FT_Short
)( segment
->height
+
1617 ( ( p
->v
- last_v
) >> 1 ) );
1625 if ( p
->v
> first_v
)
1626 segment
->height
= (FT_Short
)( segment
->height
+
1627 ( ( p
->v
- first_v
) >> 1 ) );
1630 if ( p
->v
< last_v
)
1631 segment
->height
= (FT_Short
)( segment
->height
+
1632 ( ( last_v
- p
->v
) >> 1 ) );
1642 /* Link segments to form stems and serifs. If `width_count' and */
1643 /* `widths' are non-zero, use them to fine-tune the scoring function. */
1645 FT_LOCAL_DEF( void )
1646 af_latin_hints_link_segments( AF_GlyphHints hints
,
1647 FT_UInt width_count
,
1648 AF_WidthRec
* widths
,
1651 AF_AxisHints axis
= &hints
->axis
[dim
];
1652 AF_Segment segments
= axis
->segments
;
1653 AF_Segment segment_limit
= segments
+ axis
->num_segments
;
1654 FT_Pos len_threshold
, len_score
, dist_score
, max_width
;
1655 AF_Segment seg1
, seg2
;
1659 max_width
= widths
[width_count
- 1].org
;
1663 /* a heuristic value to set up a minimum value for overlapping */
1664 len_threshold
= AF_LATIN_CONSTANT( hints
->metrics
, 8 );
1665 if ( len_threshold
== 0 )
1668 /* a heuristic value to weight lengths */
1669 len_score
= AF_LATIN_CONSTANT( hints
->metrics
, 6000 );
1671 /* a heuristic value to weight distances (no call to */
1672 /* AF_LATIN_CONSTANT needed, since we work on multiples */
1673 /* of the stem width) */
1676 /* now compare each segment to the others */
1677 for ( seg1
= segments
; seg1
< segment_limit
; seg1
++ )
1679 if ( seg1
->dir
!= axis
->major_dir
)
1682 /* search for stems having opposite directions, */
1683 /* with seg1 to the `left' of seg2 */
1684 for ( seg2
= segments
; seg2
< segment_limit
; seg2
++ )
1686 FT_Pos pos1
= seg1
->pos
;
1687 FT_Pos pos2
= seg2
->pos
;
1690 if ( seg1
->dir
+ seg2
->dir
== 0 && pos2
> pos1
)
1692 /* compute distance between the two segments */
1693 FT_Pos min
= seg1
->min_coord
;
1694 FT_Pos max
= seg1
->max_coord
;
1698 if ( min
< seg2
->min_coord
)
1699 min
= seg2
->min_coord
;
1701 if ( max
> seg2
->max_coord
)
1702 max
= seg2
->max_coord
;
1704 /* compute maximum coordinate difference of the two segments */
1705 /* (this is, how much they overlap) */
1707 if ( len
>= len_threshold
)
1710 * The score is the sum of two demerits indicating the
1711 * `badness' of a fit, measured along the segments' main axis
1712 * and orthogonal to it, respectively.
1714 * o The less overlapping along the main axis, the worse it
1715 * is, causing a larger demerit.
1717 * o The nearer the orthogonal distance to a stem width, the
1718 * better it is, causing a smaller demerit. For simplicity,
1719 * however, we only increase the demerit for values that
1720 * exceed the largest stem width.
1723 FT_Pos dist
= pos2
- pos1
;
1725 FT_Pos dist_demerit
, score
;
1730 /* distance demerits are based on multiples of `max_width'; */
1731 /* we scale by 1024 for getting more precision */
1732 FT_Pos delta
= ( dist
<< 10 ) / max_width
- ( 1 << 10 );
1735 if ( delta
> 10000 )
1736 dist_demerit
= 32000;
1737 else if ( delta
> 0 )
1738 dist_demerit
= delta
* delta
/ dist_score
;
1743 dist_demerit
= dist
; /* default if no widths available */
1745 score
= dist_demerit
+ len_score
/ len
;
1747 /* and we search for the smallest score */
1748 if ( score
< seg1
->score
)
1750 seg1
->score
= score
;
1754 if ( score
< seg2
->score
)
1756 seg2
->score
= score
;
1764 /* now compute the `serif' segments, cf. explanations in `afhints.h' */
1765 for ( seg1
= segments
; seg1
< segment_limit
; seg1
++ )
1771 if ( seg2
->link
!= seg1
)
1774 seg1
->serif
= seg2
->link
;
1781 /* Link segments to edges, using feature analysis for selection. */
1783 FT_LOCAL_DEF( FT_Error
)
1784 af_latin_hints_compute_edges( AF_GlyphHints hints
,
1787 AF_AxisHints axis
= &hints
->axis
[dim
];
1788 FT_Error error
= FT_Err_Ok
;
1789 FT_Memory memory
= hints
->memory
;
1790 AF_LatinAxis laxis
= &((AF_LatinMetrics
)hints
->metrics
)->axis
[dim
];
1792 AF_StyleClass style_class
= hints
->metrics
->style_class
;
1793 AF_ScriptClass script_class
= AF_SCRIPT_CLASSES_GET
1794 [style_class
->script
];
1796 FT_Bool top_to_bottom_hinting
= 0;
1798 AF_Segment segments
= axis
->segments
;
1799 AF_Segment segment_limit
= segments
+ axis
->num_segments
;
1803 AF_Direction up_dir
;
1806 FT_Pos edge_distance_threshold
;
1807 FT_Pos segment_length_threshold
;
1810 axis
->num_edges
= 0;
1812 scale
= ( dim
== AF_DIMENSION_HORZ
) ? hints
->x_scale
1816 up_dir
= ( dim
== AF_DIMENSION_HORZ
) ? AF_DIR_UP
1820 if ( dim
== AF_DIMENSION_VERT
)
1821 top_to_bottom_hinting
= script_class
->top_to_bottom_hinting
;
1824 * We ignore all segments that are less than 1 pixel in length
1825 * to avoid many problems with serif fonts. We compute the
1826 * corresponding threshold in font units.
1828 if ( dim
== AF_DIMENSION_HORZ
)
1829 segment_length_threshold
= FT_DivFix( 64, hints
->y_scale
);
1831 segment_length_threshold
= 0;
1833 /*********************************************************************/
1835 /* We begin by generating a sorted table of edges for the current */
1836 /* direction. To do so, we simply scan each segment and try to find */
1837 /* an edge in our table that corresponds to its position. */
1839 /* If no edge is found, we create and insert a new edge in the */
1840 /* sorted table. Otherwise, we simply add the segment to the edge's */
1841 /* list which gets processed in the second step to compute the */
1842 /* edge's properties. */
1844 /* Note that the table of edges is sorted along the segment/edge */
1847 /*********************************************************************/
1849 /* assure that edge distance threshold is at most 0.25px */
1850 edge_distance_threshold
= FT_MulFix( laxis
->edge_distance_threshold
,
1852 if ( edge_distance_threshold
> 64 / 4 )
1853 edge_distance_threshold
= 64 / 4;
1855 edge_distance_threshold
= FT_DivFix( edge_distance_threshold
,
1858 for ( seg
= segments
; seg
< segment_limit
; seg
++ )
1860 AF_Edge found
= NULL
;
1864 if ( seg
->height
< segment_length_threshold
)
1867 /* A special case for serif edges: If they are smaller than */
1868 /* 1.5 pixels we ignore them. */
1870 2 * seg
->height
< 3 * segment_length_threshold
)
1873 /* look for an edge corresponding to the segment */
1874 for ( ee
= 0; ee
< axis
->num_edges
; ee
++ )
1876 AF_Edge edge
= axis
->edges
+ ee
;
1880 dist
= seg
->pos
- edge
->fpos
;
1884 if ( dist
< edge_distance_threshold
&& edge
->dir
== seg
->dir
)
1896 /* insert a new edge in the list and */
1897 /* sort according to the position */
1898 error
= af_axis_hints_new_edge( axis
, seg
->pos
,
1899 (AF_Direction
)seg
->dir
,
1900 top_to_bottom_hinting
,
1905 /* add the segment to the new edge's list */
1910 edge
->dir
= seg
->dir
;
1911 edge
->fpos
= seg
->pos
;
1912 edge
->opos
= FT_MulFix( seg
->pos
, scale
);
1913 edge
->pos
= edge
->opos
;
1914 seg
->edge_next
= seg
;
1918 /* if an edge was found, simply add the segment to the edge's */
1920 seg
->edge_next
= found
->first
;
1921 found
->last
->edge_next
= seg
;
1927 /******************************************************************/
1929 /* Good, we now compute each edge's properties according to the */
1930 /* segments found on its position. Basically, these are */
1932 /* - the edge's main direction */
1933 /* - stem edge, serif edge or both (which defaults to stem then) */
1934 /* - rounded edge, straight or both (which defaults to straight) */
1935 /* - link for edge */
1937 /******************************************************************/
1939 /* first of all, set the `edge' field in each segment -- this is */
1940 /* required in order to compute edge links */
1943 * Note that removing this loop and setting the `edge' field of each
1944 * segment directly in the code above slows down execution speed for
1945 * some reasons on platforms like the Sun.
1948 AF_Edge edges
= axis
->edges
;
1949 AF_Edge edge_limit
= edges
+ axis
->num_edges
;
1953 for ( edge
= edges
; edge
< edge_limit
; edge
++ )
1960 seg
= seg
->edge_next
;
1962 } while ( seg
!= edge
->first
);
1965 /* now compute each edge properties */
1966 for ( edge
= edges
; edge
< edge_limit
; edge
++ )
1968 FT_Int is_round
= 0; /* does it contain round segments? */
1969 FT_Int is_straight
= 0; /* does it contain straight segments? */
1971 FT_Pos ups
= 0; /* number of upwards segments */
1972 FT_Pos downs
= 0; /* number of downwards segments */
1983 /* check for roundness of segment */
1984 if ( seg
->flags
& AF_EDGE_ROUND
)
1990 /* check for segment direction */
1991 if ( seg
->dir
== up_dir
)
1992 ups
+= seg
->max_coord
- seg
->min_coord
;
1994 downs
+= seg
->max_coord
- seg
->min_coord
;
1997 /* check for links -- if seg->serif is set, then seg->link must */
1999 is_serif
= (FT_Bool
)( seg
->serif
&&
2001 seg
->serif
->edge
!= edge
);
2003 if ( ( seg
->link
&& seg
->link
->edge
!= NULL
) || is_serif
)
2015 edge2
= edge
->serif
;
2024 edge_delta
= edge
->fpos
- edge2
->fpos
;
2025 if ( edge_delta
< 0 )
2026 edge_delta
= -edge_delta
;
2028 seg_delta
= seg
->pos
- seg2
->pos
;
2029 if ( seg_delta
< 0 )
2030 seg_delta
= -seg_delta
;
2032 if ( seg_delta
< edge_delta
)
2040 edge
->serif
= edge2
;
2041 edge2
->flags
|= AF_EDGE_SERIF
;
2047 seg
= seg
->edge_next
;
2049 } while ( seg
!= edge
->first
);
2051 /* set the round/straight flags */
2052 edge
->flags
= AF_EDGE_NORMAL
;
2054 if ( is_round
> 0 && is_round
>= is_straight
)
2055 edge
->flags
|= AF_EDGE_ROUND
;
2058 /* set the edge's main direction */
2059 edge
->dir
= AF_DIR_NONE
;
2062 edge
->dir
= (FT_Char
)up_dir
;
2064 else if ( ups
< downs
)
2065 edge
->dir
= (FT_Char
)-up_dir
;
2067 else if ( ups
== downs
)
2068 edge
->dir
= 0; /* both up and down! */
2071 /* get rid of serifs if link is set */
2072 /* XXX: This gets rid of many unpleasant artefacts! */
2073 /* Example: the `c' in cour.pfa at size 13 */
2075 if ( edge
->serif
&& edge
->link
)
2085 /* Detect segments and edges for given dimension. */
2087 FT_LOCAL_DEF( FT_Error
)
2088 af_latin_hints_detect_features( AF_GlyphHints hints
,
2089 FT_UInt width_count
,
2090 AF_WidthRec
* widths
,
2096 error
= af_latin_hints_compute_segments( hints
, dim
);
2099 af_latin_hints_link_segments( hints
, width_count
, widths
, dim
);
2101 error
= af_latin_hints_compute_edges( hints
, dim
);
2108 /* Compute all edges which lie within blue zones. */
2111 af_latin_hints_compute_blue_edges( AF_GlyphHints hints
,
2112 AF_LatinMetrics metrics
)
2114 AF_AxisHints axis
= &hints
->axis
[AF_DIMENSION_VERT
];
2115 AF_Edge edge
= axis
->edges
;
2116 AF_Edge edge_limit
= edge
+ axis
->num_edges
;
2117 AF_LatinAxis latin
= &metrics
->axis
[AF_DIMENSION_VERT
];
2118 FT_Fixed scale
= latin
->scale
;
2121 /* compute which blue zones are active, i.e. have their scaled */
2122 /* size < 3/4 pixels */
2124 /* for each horizontal edge search the blue zone which is closest */
2125 for ( ; edge
< edge_limit
; edge
++ )
2128 AF_Width best_blue
= NULL
;
2129 FT_Bool best_blue_is_neutral
= 0;
2130 FT_Pos best_dist
; /* initial threshold */
2133 /* compute the initial threshold as a fraction of the EM size */
2134 /* (the value 40 is heuristic) */
2135 best_dist
= FT_MulFix( metrics
->units_per_em
/ 40, scale
);
2137 /* assure a minimum distance of 0.5px */
2138 if ( best_dist
> 64 / 2 )
2141 for ( bb
= 0; bb
< latin
->blue_count
; bb
++ )
2143 AF_LatinBlue blue
= latin
->blues
+ bb
;
2144 FT_Bool is_top_blue
, is_neutral_blue
, is_major_dir
;
2147 /* skip inactive blue zones (i.e., those that are too large) */
2148 if ( !( blue
->flags
& AF_LATIN_BLUE_ACTIVE
) )
2151 /* if it is a top zone, check for right edges (against the major */
2152 /* direction); if it is a bottom zone, check for left edges (in */
2153 /* the major direction) -- this assumes the TrueType convention */
2154 /* for the orientation of contours */
2156 (FT_Byte
)( ( blue
->flags
& ( AF_LATIN_BLUE_TOP
|
2157 AF_LATIN_BLUE_SUB_TOP
) ) != 0 );
2159 (FT_Byte
)( ( blue
->flags
& AF_LATIN_BLUE_NEUTRAL
) != 0);
2161 FT_BOOL( edge
->dir
== axis
->major_dir
);
2163 /* neutral blue zones are handled for both directions */
2164 if ( is_top_blue
^ is_major_dir
|| is_neutral_blue
)
2169 /* first of all, compare it to the reference position */
2170 dist
= edge
->fpos
- blue
->ref
.org
;
2174 dist
= FT_MulFix( dist
, scale
);
2175 if ( dist
< best_dist
)
2178 best_blue
= &blue
->ref
;
2179 best_blue_is_neutral
= is_neutral_blue
;
2182 /* now compare it to the overshoot position and check whether */
2183 /* the edge is rounded, and whether the edge is over the */
2184 /* reference position of a top zone, or under the reference */
2185 /* position of a bottom zone (provided we don't have a */
2186 /* neutral blue zone) */
2187 if ( edge
->flags
& AF_EDGE_ROUND
&&
2191 FT_Bool is_under_ref
= FT_BOOL( edge
->fpos
< blue
->ref
.org
);
2194 if ( is_top_blue
^ is_under_ref
)
2196 dist
= edge
->fpos
- blue
->shoot
.org
;
2200 dist
= FT_MulFix( dist
, scale
);
2201 if ( dist
< best_dist
)
2204 best_blue
= &blue
->shoot
;
2205 best_blue_is_neutral
= is_neutral_blue
;
2214 edge
->blue_edge
= best_blue
;
2215 if ( best_blue_is_neutral
)
2216 edge
->flags
|= AF_EDGE_NEUTRAL
;
2222 /* Initalize hinting engine. */
2225 af_latin_hints_init( AF_GlyphHints hints
,
2226 AF_LatinMetrics metrics
)
2228 FT_Render_Mode mode
;
2229 FT_UInt32 scaler_flags
, other_flags
;
2230 FT_Face face
= metrics
->root
.scaler
.face
;
2233 af_glyph_hints_rescale( hints
, (AF_StyleMetrics
)metrics
);
2236 * correct x_scale and y_scale if needed, since they may have
2237 * been modified by `af_latin_metrics_scale_dim' above
2239 hints
->x_scale
= metrics
->axis
[AF_DIMENSION_HORZ
].scale
;
2240 hints
->x_delta
= metrics
->axis
[AF_DIMENSION_HORZ
].delta
;
2241 hints
->y_scale
= metrics
->axis
[AF_DIMENSION_VERT
].scale
;
2242 hints
->y_delta
= metrics
->axis
[AF_DIMENSION_VERT
].delta
;
2244 /* compute flags depending on render mode, etc. */
2245 mode
= metrics
->root
.scaler
.render_mode
;
2247 #if 0 /* #ifdef AF_CONFIG_OPTION_USE_WARPER */
2248 if ( mode
== FT_RENDER_MODE_LCD
|| mode
== FT_RENDER_MODE_LCD_V
)
2249 metrics
->root
.scaler
.render_mode
= mode
= FT_RENDER_MODE_NORMAL
;
2252 scaler_flags
= hints
->scaler_flags
;
2256 * We snap the width of vertical stems for the monochrome and
2257 * horizontal LCD rendering targets only.
2259 if ( mode
== FT_RENDER_MODE_MONO
|| mode
== FT_RENDER_MODE_LCD
)
2260 other_flags
|= AF_LATIN_HINTS_HORZ_SNAP
;
2263 * We snap the width of horizontal stems for the monochrome and
2264 * vertical LCD rendering targets only.
2266 if ( mode
== FT_RENDER_MODE_MONO
|| mode
== FT_RENDER_MODE_LCD_V
)
2267 other_flags
|= AF_LATIN_HINTS_VERT_SNAP
;
2270 * We adjust stems to full pixels only if we don't use the `light' mode.
2272 if ( mode
!= FT_RENDER_MODE_LIGHT
)
2273 other_flags
|= AF_LATIN_HINTS_STEM_ADJUST
;
2275 if ( mode
== FT_RENDER_MODE_MONO
)
2276 other_flags
|= AF_LATIN_HINTS_MONO
;
2279 * In `light' hinting mode we disable horizontal hinting completely.
2280 * We also do it if the face is italic.
2282 * However, if warping is enabled (which only works in `light' hinting
2283 * mode), advance widths get adjusted, too.
2285 if ( mode
== FT_RENDER_MODE_LIGHT
||
2286 ( face
->style_flags
& FT_STYLE_FLAG_ITALIC
) != 0 )
2287 scaler_flags
|= AF_SCALER_FLAG_NO_HORIZONTAL
;
2289 #ifdef AF_CONFIG_OPTION_USE_WARPER
2290 /* get (global) warper flag */
2291 if ( !metrics
->root
.globals
->module
->warping
)
2292 scaler_flags
|= AF_SCALER_FLAG_NO_WARPER
;
2295 hints
->scaler_flags
= scaler_flags
;
2296 hints
->other_flags
= other_flags
;
2302 /*************************************************************************/
2303 /*************************************************************************/
2305 /***** L A T I N G L Y P H G R I D - F I T T I N G *****/
2307 /*************************************************************************/
2308 /*************************************************************************/
2310 /* Snap a given width in scaled coordinates to one of the */
2311 /* current standard widths. */
2314 af_latin_snap_width( AF_Width widths
,
2319 FT_Pos best
= 64 + 32 + 2;
2320 FT_Pos reference
= width
;
2324 for ( n
= 0; n
< count
; n
++ )
2341 scaled
= FT_PIX_ROUND( reference
);
2343 if ( width
>= reference
)
2345 if ( width
< scaled
+ 48 )
2350 if ( width
> scaled
- 48 )
2358 /* Compute the snapped width of a given stem, ignoring very thin ones. */
2359 /* There is a lot of voodoo in this function; changing the hard-coded */
2360 /* parameters influence the whole hinting process. */
2363 af_latin_compute_stem_width( AF_GlyphHints hints
,
2367 FT_UInt stem_flags
)
2369 AF_LatinMetrics metrics
= (AF_LatinMetrics
)hints
->metrics
;
2370 AF_LatinAxis axis
= &metrics
->axis
[dim
];
2371 FT_Pos dist
= width
;
2373 FT_Int vertical
= ( dim
== AF_DIMENSION_VERT
);
2376 if ( !AF_LATIN_HINTS_DO_STEM_ADJUST( hints
) ||
2386 if ( ( vertical
&& !AF_LATIN_HINTS_DO_VERT_SNAP( hints
) ) ||
2387 ( !vertical
&& !AF_LATIN_HINTS_DO_HORZ_SNAP( hints
) ) )
2389 /* smooth hinting process: very lightly quantize the stem width */
2391 /* leave the widths of serifs alone */
2392 if ( ( stem_flags
& AF_EDGE_SERIF
) &&
2397 else if ( base_flags
& AF_EDGE_ROUND
)
2402 else if ( dist
< 56 )
2405 if ( axis
->width_count
> 0 )
2410 /* compare to standard width */
2411 delta
= dist
- axis
->widths
[0].cur
;
2418 dist
= axis
->widths
[0].cur
;
2425 if ( dist
< 3 * 64 )
2433 else if ( delta
< 32 )
2436 else if ( delta
< 54 )
2443 dist
= ( dist
+ 32 ) & ~63;
2448 /* strong hinting process: snap the stem width to integer pixels */
2450 FT_Pos org_dist
= dist
;
2453 dist
= af_latin_snap_width( axis
->widths
, axis
->width_count
, dist
);
2457 /* in the case of vertical hinting, always round */
2458 /* the stem heights to integer pixels */
2461 dist
= ( dist
+ 16 ) & ~63;
2467 if ( AF_LATIN_HINTS_DO_MONO( hints
) )
2469 /* monochrome horizontal hinting: snap widths to integer pixels */
2470 /* with a different threshold */
2475 dist
= ( dist
+ 32 ) & ~63;
2479 /* for horizontal anti-aliased hinting, we adopt a more subtle */
2480 /* approach: we strengthen small stems, round stems whose size */
2481 /* is between 1 and 2 pixels to an integer, otherwise nothing */
2484 dist
= ( dist
+ 64 ) >> 1;
2486 else if ( dist
< 128 )
2488 /* We only round to an integer width if the corresponding */
2489 /* distortion is less than 1/4 pixel. Otherwise this */
2490 /* makes everything worse since the diagonals, which are */
2491 /* not hinted, appear a lot bolder or thinner than the */
2492 /* vertical stems. */
2497 dist
= ( dist
+ 22 ) & ~63;
2498 delta
= dist
- org_dist
;
2506 dist
= ( dist
+ 64 ) >> 1;
2510 /* round otherwise to prevent color fringes in LCD mode */
2511 dist
= ( dist
+ 32 ) & ~63;
2524 /* Align one stem edge relative to the previous stem edge. */
2527 af_latin_align_linked_edge( AF_GlyphHints hints
,
2532 FT_Pos dist
= stem_edge
->opos
- base_edge
->opos
;
2534 FT_Pos fitted_width
= af_latin_compute_stem_width( hints
, dim
, dist
,
2539 stem_edge
->pos
= base_edge
->pos
+ fitted_width
;
2541 FT_TRACE5(( " LINK: edge %d (opos=%.2f) linked to %.2f,"
2542 " dist was %.2f, now %.2f\n",
2543 stem_edge
- hints
->axis
[dim
].edges
, stem_edge
->opos
/ 64.0,
2544 stem_edge
->pos
/ 64.0, dist
/ 64.0, fitted_width
/ 64.0 ));
2548 /* Shift the coordinates of the `serif' edge by the same amount */
2549 /* as the corresponding `base' edge has been moved already. */
2552 af_latin_align_serif_edge( AF_GlyphHints hints
,
2558 serif
->pos
= base
->pos
+ ( serif
->opos
- base
->opos
);
2562 /*************************************************************************/
2563 /*************************************************************************/
2564 /*************************************************************************/
2566 /**** E D G E H I N T I N G ****/
2568 /*************************************************************************/
2569 /*************************************************************************/
2570 /*************************************************************************/
2573 /* The main grid-fitting routine. */
2576 af_latin_hint_edges( AF_GlyphHints hints
,
2579 AF_AxisHints axis
= &hints
->axis
[dim
];
2580 AF_Edge edges
= axis
->edges
;
2581 AF_Edge edge_limit
= edges
+ axis
->num_edges
;
2584 AF_Edge anchor
= NULL
;
2585 FT_Int has_serifs
= 0;
2587 AF_StyleClass style_class
= hints
->metrics
->style_class
;
2588 AF_ScriptClass script_class
= AF_SCRIPT_CLASSES_GET
2589 [style_class
->script
];
2591 FT_Bool top_to_bottom_hinting
= 0;
2593 #ifdef FT_DEBUG_LEVEL_TRACE
2594 FT_UInt num_actions
= 0;
2598 FT_TRACE5(( "latin %s edge hinting (style `%s')\n",
2599 dim
== AF_DIMENSION_VERT
? "horizontal" : "vertical",
2600 af_style_names
[hints
->metrics
->style_class
->style
] ));
2602 if ( dim
== AF_DIMENSION_VERT
)
2603 top_to_bottom_hinting
= script_class
->top_to_bottom_hinting
;
2605 /* we begin by aligning all stems relative to the blue zone */
2606 /* if needed -- that's only for horizontal edges */
2608 if ( dim
== AF_DIMENSION_VERT
&& AF_HINTS_DO_BLUES( hints
) )
2610 for ( edge
= edges
; edge
< edge_limit
; edge
++ )
2613 AF_Edge edge1
, edge2
; /* these edges form the stem to check */
2616 if ( edge
->flags
& AF_EDGE_DONE
)
2623 * If a stem contains both a neutral and a non-neutral blue zone,
2624 * skip the neutral one. Otherwise, outlines with different
2625 * directions might be incorrectly aligned at the same vertical
2628 * If we have two neutral blue zones, skip one of them.
2631 if ( edge
->blue_edge
&& edge2
&& edge2
->blue_edge
)
2633 FT_Byte neutral
= edge
->flags
& AF_EDGE_NEUTRAL
;
2634 FT_Byte neutral2
= edge2
->flags
& AF_EDGE_NEUTRAL
;
2639 edge2
->blue_edge
= NULL
;
2640 edge2
->flags
&= ~AF_EDGE_NEUTRAL
;
2644 edge
->blue_edge
= NULL
;
2645 edge
->flags
&= ~AF_EDGE_NEUTRAL
;
2649 blue
= edge
->blue_edge
;
2653 /* flip edges if the other edge is aligned to a blue zone */
2654 else if ( edge2
&& edge2
->blue_edge
)
2656 blue
= edge2
->blue_edge
;
2664 #ifdef FT_DEBUG_LEVEL_TRACE
2666 FT_TRACE5(( " BLUE_ANCHOR: edge %d (opos=%.2f) snapped to %.2f,"
2667 " was %.2f (anchor=edge %d)\n",
2668 edge1
- edges
, edge1
->opos
/ 64.0, blue
->fit
/ 64.0,
2669 edge1
->pos
/ 64.0, edge
- edges
));
2671 FT_TRACE5(( " BLUE: edge %d (opos=%.2f) snapped to %.2f,"
2673 edge1
- edges
, edge1
->opos
/ 64.0, blue
->fit
/ 64.0,
2674 edge1
->pos
/ 64.0 ));
2679 edge1
->pos
= blue
->fit
;
2680 edge1
->flags
|= AF_EDGE_DONE
;
2682 if ( edge2
&& !edge2
->blue_edge
)
2684 af_latin_align_linked_edge( hints
, dim
, edge1
, edge2
);
2685 edge2
->flags
|= AF_EDGE_DONE
;
2687 #ifdef FT_DEBUG_LEVEL_TRACE
2697 /* now we align all other stem edges, trying to maintain the */
2698 /* relative order of stems in the glyph */
2699 for ( edge
= edges
; edge
< edge_limit
; edge
++ )
2704 if ( edge
->flags
& AF_EDGE_DONE
)
2707 /* skip all non-stem edges */
2715 /* now align the stem */
2717 /* this should not happen, but it's better to be safe */
2718 if ( edge2
->blue_edge
)
2720 FT_TRACE5(( " ASSERTION FAILED for edge %d\n", edge2
- edges
));
2722 af_latin_align_linked_edge( hints
, dim
, edge2
, edge
);
2723 edge
->flags
|= AF_EDGE_DONE
;
2725 #ifdef FT_DEBUG_LEVEL_TRACE
2733 /* if we reach this if clause, no stem has been aligned yet */
2735 FT_Pos org_len
, org_center
, cur_len
;
2736 FT_Pos cur_pos1
, error1
, error2
, u_off
, d_off
;
2739 org_len
= edge2
->opos
- edge
->opos
;
2740 cur_len
= af_latin_compute_stem_width( hints
, dim
, org_len
,
2744 /* some voodoo to specially round edges for small stem widths; */
2745 /* the idea is to align the center of a stem, then shifting */
2746 /* the stem edges to suitable positions */
2747 if ( cur_len
<= 64 )
2755 /* 1px < width < 1.5px */
2762 org_center
= edge
->opos
+ ( org_len
>> 1 );
2763 cur_pos1
= FT_PIX_ROUND( org_center
);
2765 error1
= org_center
- ( cur_pos1
- u_off
);
2769 error2
= org_center
- ( cur_pos1
+ d_off
);
2773 if ( error1
< error2
)
2778 edge
->pos
= cur_pos1
- cur_len
/ 2;
2779 edge2
->pos
= edge
->pos
+ cur_len
;
2782 edge
->pos
= FT_PIX_ROUND( edge
->opos
);
2785 edge
->flags
|= AF_EDGE_DONE
;
2787 FT_TRACE5(( " ANCHOR: edge %d (opos=%.2f) and %d (opos=%.2f)"
2788 " snapped to %.2f and %.2f\n",
2789 edge
- edges
, edge
->opos
/ 64.0,
2790 edge2
- edges
, edge2
->opos
/ 64.0,
2791 edge
->pos
/ 64.0, edge2
->pos
/ 64.0 ));
2793 af_latin_align_linked_edge( hints
, dim
, edge
, edge2
);
2795 #ifdef FT_DEBUG_LEVEL_TRACE
2801 FT_Pos org_pos
, org_len
, org_center
, cur_len
;
2802 FT_Pos cur_pos1
, cur_pos2
, delta1
, delta2
;
2805 org_pos
= anchor
->pos
+ ( edge
->opos
- anchor
->opos
);
2806 org_len
= edge2
->opos
- edge
->opos
;
2807 org_center
= org_pos
+ ( org_len
>> 1 );
2809 cur_len
= af_latin_compute_stem_width( hints
, dim
, org_len
,
2813 if ( edge2
->flags
& AF_EDGE_DONE
)
2815 FT_TRACE5(( " ADJUST: edge %d (pos=%.2f) moved to %.2f\n",
2816 edge
- edges
, edge
->pos
/ 64.0,
2817 ( edge2
->pos
- cur_len
) / 64.0 ));
2819 edge
->pos
= edge2
->pos
- cur_len
;
2822 else if ( cur_len
< 96 )
2824 FT_Pos u_off
, d_off
;
2827 cur_pos1
= FT_PIX_ROUND( org_center
);
2829 if ( cur_len
<= 64 )
2840 delta1
= org_center
- ( cur_pos1
- u_off
);
2844 delta2
= org_center
- ( cur_pos1
+ d_off
);
2848 if ( delta1
< delta2
)
2853 edge
->pos
= cur_pos1
- cur_len
/ 2;
2854 edge2
->pos
= cur_pos1
+ cur_len
/ 2;
2856 FT_TRACE5(( " STEM: edge %d (opos=%.2f) linked to %d (opos=%.2f)"
2857 " snapped to %.2f and %.2f\n",
2858 edge
- edges
, edge
->opos
/ 64.0,
2859 edge2
- edges
, edge2
->opos
/ 64.0,
2860 edge
->pos
/ 64.0, edge2
->pos
/ 64.0 ));
2865 org_pos
= anchor
->pos
+ ( edge
->opos
- anchor
->opos
);
2866 org_len
= edge2
->opos
- edge
->opos
;
2867 org_center
= org_pos
+ ( org_len
>> 1 );
2869 cur_len
= af_latin_compute_stem_width( hints
, dim
, org_len
,
2873 cur_pos1
= FT_PIX_ROUND( org_pos
);
2874 delta1
= cur_pos1
+ ( cur_len
>> 1 ) - org_center
;
2878 cur_pos2
= FT_PIX_ROUND( org_pos
+ org_len
) - cur_len
;
2879 delta2
= cur_pos2
+ ( cur_len
>> 1 ) - org_center
;
2883 edge
->pos
= ( delta1
< delta2
) ? cur_pos1
: cur_pos2
;
2884 edge2
->pos
= edge
->pos
+ cur_len
;
2886 FT_TRACE5(( " STEM: edge %d (opos=%.2f) linked to %d (opos=%.2f)"
2887 " snapped to %.2f and %.2f\n",
2888 edge
- edges
, edge
->opos
/ 64.0,
2889 edge2
- edges
, edge2
->opos
/ 64.0,
2890 edge
->pos
/ 64.0, edge2
->pos
/ 64.0 ));
2893 #ifdef FT_DEBUG_LEVEL_TRACE
2897 edge
->flags
|= AF_EDGE_DONE
;
2898 edge2
->flags
|= AF_EDGE_DONE
;
2900 if ( edge
> edges
&&
2901 ( top_to_bottom_hinting
? ( edge
->pos
> edge
[-1].pos
)
2902 : ( edge
->pos
< edge
[-1].pos
) ) )
2904 #ifdef FT_DEBUG_LEVEL_TRACE
2905 FT_TRACE5(( " BOUND: edge %d (pos=%.2f) moved to %.2f\n",
2906 edge
- edges
, edge
->pos
/ 64.0, edge
[-1].pos
/ 64.0 ));
2911 edge
->pos
= edge
[-1].pos
;
2916 /* make sure that lowercase m's maintain their symmetry */
2918 /* In general, lowercase m's have six vertical edges if they are sans */
2919 /* serif, or twelve if they are with serifs. This implementation is */
2920 /* based on that assumption, and seems to work very well with most */
2921 /* faces. However, if for a certain face this assumption is not */
2922 /* true, the m is just rendered like before. In addition, any stem */
2923 /* correction will only be applied to symmetrical glyphs (even if the */
2924 /* glyph is not an m), so the potential for unwanted distortion is */
2925 /* relatively low. */
2927 /* We don't handle horizontal edges since we can't easily assure that */
2928 /* the third (lowest) stem aligns with the base line; it might end up */
2929 /* one pixel higher or lower. */
2931 n_edges
= edge_limit
- edges
;
2932 if ( dim
== AF_DIMENSION_HORZ
&& ( n_edges
== 6 || n_edges
== 12 ) )
2934 AF_Edge edge1
, edge2
, edge3
;
2935 FT_Pos dist1
, dist2
, span
, delta
;
2951 dist1
= edge2
->opos
- edge1
->opos
;
2952 dist2
= edge3
->opos
- edge2
->opos
;
2954 span
= dist1
- dist2
;
2960 delta
= edge3
->pos
- ( 2 * edge2
->pos
- edge1
->pos
);
2961 edge3
->pos
-= delta
;
2963 edge3
->link
->pos
-= delta
;
2965 /* move the serifs along with the stem */
2966 if ( n_edges
== 12 )
2968 ( edges
+ 8 )->pos
-= delta
;
2969 ( edges
+ 11 )->pos
-= delta
;
2972 edge3
->flags
|= AF_EDGE_DONE
;
2974 edge3
->link
->flags
|= AF_EDGE_DONE
;
2978 if ( has_serifs
|| !anchor
)
2981 * now hint the remaining edges (serifs and single) in order
2982 * to complete our processing
2984 for ( edge
= edges
; edge
< edge_limit
; edge
++ )
2989 if ( edge
->flags
& AF_EDGE_DONE
)
2996 delta
= edge
->serif
->opos
- edge
->opos
;
3001 if ( delta
< 64 + 16 )
3003 af_latin_align_serif_edge( hints
, edge
->serif
, edge
);
3004 FT_TRACE5(( " SERIF: edge %d (opos=%.2f) serif to %d (opos=%.2f)"
3005 " aligned to %.2f\n",
3006 edge
- edges
, edge
->opos
/ 64.0,
3007 edge
->serif
- edges
, edge
->serif
->opos
/ 64.0,
3008 edge
->pos
/ 64.0 ));
3012 edge
->pos
= FT_PIX_ROUND( edge
->opos
);
3014 FT_TRACE5(( " SERIF_ANCHOR: edge %d (opos=%.2f)"
3015 " snapped to %.2f\n",
3016 edge
-edges
, edge
->opos
/ 64.0, edge
->pos
/ 64.0 ));
3020 AF_Edge before
, after
;
3023 for ( before
= edge
- 1; before
>= edges
; before
-- )
3024 if ( before
->flags
& AF_EDGE_DONE
)
3027 for ( after
= edge
+ 1; after
< edge_limit
; after
++ )
3028 if ( after
->flags
& AF_EDGE_DONE
)
3031 if ( before
>= edges
&& before
< edge
&&
3032 after
< edge_limit
&& after
> edge
)
3034 if ( after
->opos
== before
->opos
)
3035 edge
->pos
= before
->pos
;
3037 edge
->pos
= before
->pos
+
3038 FT_MulDiv( edge
->opos
- before
->opos
,
3039 after
->pos
- before
->pos
,
3040 after
->opos
- before
->opos
);
3042 FT_TRACE5(( " SERIF_LINK1: edge %d (opos=%.2f) snapped to %.2f"
3043 " from %d (opos=%.2f)\n",
3044 edge
- edges
, edge
->opos
/ 64.0,
3046 before
- edges
, before
->opos
/ 64.0 ));
3050 edge
->pos
= anchor
->pos
+
3051 ( ( edge
->opos
- anchor
->opos
+ 16 ) & ~31 );
3052 FT_TRACE5(( " SERIF_LINK2: edge %d (opos=%.2f)"
3053 " snapped to %.2f\n",
3054 edge
- edges
, edge
->opos
/ 64.0, edge
->pos
/ 64.0 ));
3058 #ifdef FT_DEBUG_LEVEL_TRACE
3061 edge
->flags
|= AF_EDGE_DONE
;
3063 if ( edge
> edges
&&
3064 ( top_to_bottom_hinting
? ( edge
->pos
> edge
[-1].pos
)
3065 : ( edge
->pos
< edge
[-1].pos
) ) )
3067 #ifdef FT_DEBUG_LEVEL_TRACE
3068 FT_TRACE5(( " BOUND: edge %d (pos=%.2f) moved to %.2f\n",
3069 edge
- edges
, edge
->pos
/ 64.0, edge
[-1].pos
/ 64.0 ));
3073 edge
->pos
= edge
[-1].pos
;
3076 if ( edge
+ 1 < edge_limit
&&
3077 edge
[1].flags
& AF_EDGE_DONE
&&
3078 ( top_to_bottom_hinting
? ( edge
->pos
< edge
[1].pos
)
3079 : ( edge
->pos
> edge
[1].pos
) ) )
3081 #ifdef FT_DEBUG_LEVEL_TRACE
3082 FT_TRACE5(( " BOUND: edge %d (pos=%.2f) moved to %.2f\n",
3083 edge
- edges
, edge
->pos
/ 64.0, edge
[1].pos
/ 64.0 ));
3088 edge
->pos
= edge
[1].pos
;
3093 #ifdef FT_DEBUG_LEVEL_TRACE
3095 FT_TRACE5(( " (none)\n" ));
3096 FT_TRACE5(( "\n" ));
3101 /* Apply the complete hinting algorithm to a latin glyph. */
3104 af_latin_hints_apply( FT_UInt glyph_index
,
3105 AF_GlyphHints hints
,
3106 FT_Outline
* outline
,
3107 AF_LatinMetrics metrics
)
3115 error
= af_glyph_hints_reload( hints
, outline
);
3119 /* analyze glyph outline */
3120 #ifdef AF_CONFIG_OPTION_USE_WARPER
3121 if ( ( metrics
->root
.scaler
.render_mode
== FT_RENDER_MODE_LIGHT
&&
3122 AF_HINTS_DO_WARP( hints
) ) ||
3123 AF_HINTS_DO_HORIZONTAL( hints
) )
3125 if ( AF_HINTS_DO_HORIZONTAL( hints
) )
3128 axis
= &metrics
->axis
[AF_DIMENSION_HORZ
];
3129 error
= af_latin_hints_detect_features( hints
,
3132 AF_DIMENSION_HORZ
);
3137 if ( AF_HINTS_DO_VERTICAL( hints
) )
3139 axis
= &metrics
->axis
[AF_DIMENSION_VERT
];
3140 error
= af_latin_hints_detect_features( hints
,
3143 AF_DIMENSION_VERT
);
3147 /* apply blue zones to base characters only */
3148 if ( !( metrics
->root
.globals
->glyph_styles
[glyph_index
] & AF_NONBASE
) )
3149 af_latin_hints_compute_blue_edges( hints
, metrics
);
3152 /* grid-fit the outline */
3153 for ( dim
= 0; dim
< AF_DIMENSION_MAX
; dim
++ )
3155 #ifdef AF_CONFIG_OPTION_USE_WARPER
3156 if ( dim
== AF_DIMENSION_HORZ
&&
3157 metrics
->root
.scaler
.render_mode
== FT_RENDER_MODE_LIGHT
&&
3158 AF_HINTS_DO_WARP( hints
) )
3160 AF_WarperRec warper
;
3165 af_warper_compute( &warper
, hints
, (AF_Dimension
)dim
,
3167 af_glyph_hints_scale_dim( hints
, (AF_Dimension
)dim
,
3171 #endif /* AF_CONFIG_OPTION_USE_WARPER */
3173 if ( ( dim
== AF_DIMENSION_HORZ
&& AF_HINTS_DO_HORIZONTAL( hints
) ) ||
3174 ( dim
== AF_DIMENSION_VERT
&& AF_HINTS_DO_VERTICAL( hints
) ) )
3176 af_latin_hint_edges( hints
, (AF_Dimension
)dim
);
3177 af_glyph_hints_align_edge_points( hints
, (AF_Dimension
)dim
);
3178 af_glyph_hints_align_strong_points( hints
, (AF_Dimension
)dim
);
3179 af_glyph_hints_align_weak_points( hints
, (AF_Dimension
)dim
);
3183 af_glyph_hints_save( hints
, outline
);
3190 /*************************************************************************/
3191 /*************************************************************************/
3193 /***** L A T I N S C R I P T C L A S S *****/
3195 /*************************************************************************/
3196 /*************************************************************************/
3199 AF_DEFINE_WRITING_SYSTEM_CLASS(
3200 af_latin_writing_system_class
,
3202 AF_WRITING_SYSTEM_LATIN
,
3204 sizeof ( AF_LatinMetricsRec
),
3206 (AF_WritingSystem_InitMetricsFunc
) af_latin_metrics_init
,
3207 (AF_WritingSystem_ScaleMetricsFunc
)af_latin_metrics_scale
,
3208 (AF_WritingSystem_DoneMetricsFunc
) NULL
,
3209 (AF_WritingSystem_GetStdWidthsFunc
)af_latin_get_standard_widths
,
3211 (AF_WritingSystem_InitHintsFunc
) af_latin_hints_init
,
3212 (AF_WritingSystem_ApplyHintsFunc
) af_latin_hints_apply