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
, 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
) );
1143 "af_latin_metrics_scale_dim:"
1144 " x height alignment (style `%s'):\n"
1146 " vertical scaling changed from %.4f to %.4f (by %d%%)\n"
1148 af_style_names
[metrics
->root
.style_class
->style
],
1150 new_scale
/ 65536.0,
1151 ( 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
;
1459 /* we call values measured along a segment (point->v) */
1460 /* `coordinates', and values orthogonal to it (point->u) */
1462 FT_Pos min_pos
= 32000;
1463 FT_Pos max_pos
= -32000;
1464 FT_Pos min_coord
= 32000;
1465 FT_Pos max_coord
= -32000;
1466 FT_UShort min_flags
= AF_FLAG_NONE
;
1467 FT_UShort max_flags
= AF_FLAG_NONE
;
1468 FT_Pos min_on_coord
= 32000;
1469 FT_Pos max_on_coord
= -32000;
1473 AF_Segment prev_segment
= NULL
;
1475 FT_Pos prev_min_pos
= min_pos
;
1476 FT_Pos prev_max_pos
= max_pos
;
1477 FT_Pos prev_min_coord
= min_coord
;
1478 FT_Pos prev_max_coord
= max_coord
;
1479 FT_UShort prev_min_flags
= min_flags
;
1480 FT_UShort prev_max_flags
= max_flags
;
1481 FT_Pos prev_min_on_coord
= min_on_coord
;
1482 FT_Pos prev_max_on_coord
= max_on_coord
;
1485 if ( FT_ABS( last
->out_dir
) == major_dir
&&
1486 FT_ABS( point
->out_dir
) == major_dir
)
1488 /* we are already on an edge, try to locate its start */
1493 point
= point
->prev
;
1494 if ( FT_ABS( point
->out_dir
) != major_dir
)
1496 point
= point
->next
;
1499 if ( point
== last
)
1514 /* get minimum and maximum position */
1521 /* get minimum and maximum coordinate together with flags */
1523 if ( v
< min_coord
)
1526 min_flags
= point
->flags
;
1528 if ( v
> max_coord
)
1531 max_flags
= point
->flags
;
1534 /* get minimum and maximum coordinate of `on' points */
1535 if ( !( point
->flags
& AF_FLAG_CONTROL
) )
1538 if ( v
< min_on_coord
)
1540 if ( v
> max_on_coord
)
1544 if ( point
->out_dir
!= segment_dir
|| point
== last
)
1546 /* check whether the new segment's start point is identical to */
1547 /* the previous segment's end point; for example, this might */
1548 /* happen for spikes */
1550 if ( !prev_segment
|| segment
->first
!= prev_segment
->last
)
1552 /* points are different: we are just leaving an edge, thus */
1553 /* record a new segment */
1555 segment
->last
= point
;
1556 segment
->pos
= (FT_Short
)( ( min_pos
+ max_pos
) >> 1 );
1557 segment
->delta
= (FT_Short
)( ( max_pos
- min_pos
) >> 1 );
1559 /* a segment is round if either its first or last point */
1560 /* is a control point, and the length of the on points */
1561 /* inbetween doesn't exceed a heuristic limit */
1562 if ( ( min_flags
| max_flags
) & AF_FLAG_CONTROL
&&
1563 ( max_on_coord
- min_on_coord
) < flat_threshold
)
1564 segment
->flags
|= AF_EDGE_ROUND
;
1566 segment
->min_coord
= (FT_Short
)min_coord
;
1567 segment
->max_coord
= (FT_Short
)max_coord
;
1568 segment
->height
= segment
->max_coord
- segment
->min_coord
;
1570 prev_segment
= segment
;
1571 prev_min_pos
= min_pos
;
1572 prev_max_pos
= max_pos
;
1573 prev_min_coord
= min_coord
;
1574 prev_max_coord
= max_coord
;
1575 prev_min_flags
= min_flags
;
1576 prev_max_flags
= max_flags
;
1577 prev_min_on_coord
= min_on_coord
;
1578 prev_max_on_coord
= max_on_coord
;
1582 /* points are the same: we don't create a new segment but */
1583 /* merge the current segment with the previous one */
1585 if ( prev_segment
->last
->in_dir
== point
->in_dir
)
1587 /* we have identical directions (this can happen for */
1588 /* degenerate outlines that move zig-zag along the main */
1589 /* axis without changing the coordinate value of the other */
1590 /* axis, and where the segments have just been merged): */
1591 /* unify segments */
1593 /* update constraints */
1595 if ( prev_min_pos
< min_pos
)
1596 min_pos
= prev_min_pos
;
1597 if ( prev_max_pos
> max_pos
)
1598 max_pos
= prev_max_pos
;
1600 if ( prev_min_coord
< min_coord
)
1602 min_coord
= prev_min_coord
;
1603 min_flags
= prev_min_flags
;
1605 if ( prev_max_coord
> max_coord
)
1607 max_coord
= prev_max_coord
;
1608 max_flags
= prev_max_flags
;
1611 if ( prev_min_on_coord
< min_on_coord
)
1612 min_on_coord
= prev_min_on_coord
;
1613 if ( prev_max_on_coord
> max_on_coord
)
1614 max_on_coord
= prev_max_on_coord
;
1616 prev_segment
->last
= point
;
1617 prev_segment
->pos
= (FT_Short
)( ( min_pos
+
1620 if ( ( min_flags
| max_flags
) & AF_FLAG_CONTROL
&&
1621 ( max_on_coord
- min_on_coord
) < flat_threshold
)
1622 prev_segment
->flags
|= AF_EDGE_ROUND
;
1624 prev_segment
->flags
&= ~AF_EDGE_ROUND
;
1626 prev_segment
->min_coord
= (FT_Short
)min_coord
;
1627 prev_segment
->max_coord
= (FT_Short
)max_coord
;
1628 prev_segment
->height
= prev_segment
->max_coord
-
1629 prev_segment
->min_coord
;
1633 /* we have different directions; use the properties of the */
1634 /* longer segment and discard the other one */
1636 if ( FT_ABS( prev_max_coord
- prev_min_coord
) >
1637 FT_ABS( max_coord
- min_coord
) )
1639 /* discard current segment */
1641 if ( min_pos
< prev_min_pos
)
1642 prev_min_pos
= min_pos
;
1643 if ( max_pos
> prev_max_pos
)
1644 prev_max_pos
= max_pos
;
1646 prev_segment
->last
= point
;
1647 prev_segment
->pos
= (FT_Short
)( ( prev_min_pos
+
1648 prev_max_pos
) >> 1 );
1652 /* discard previous segment */
1654 if ( prev_min_pos
< min_pos
)
1655 min_pos
= prev_min_pos
;
1656 if ( prev_max_pos
> max_pos
)
1657 max_pos
= prev_max_pos
;
1659 segment
->last
= point
;
1660 segment
->pos
= (FT_Short
)( ( min_pos
+ max_pos
) >> 1 );
1662 if ( ( min_flags
| max_flags
) & AF_FLAG_CONTROL
&&
1663 ( max_on_coord
- min_on_coord
) < flat_threshold
)
1664 segment
->flags
|= AF_EDGE_ROUND
;
1666 segment
->min_coord
= (FT_Short
)min_coord
;
1667 segment
->max_coord
= (FT_Short
)max_coord
;
1668 segment
->height
= segment
->max_coord
-
1671 *prev_segment
= *segment
;
1673 prev_min_pos
= min_pos
;
1674 prev_max_pos
= max_pos
;
1675 prev_min_coord
= min_coord
;
1676 prev_max_coord
= max_coord
;
1677 prev_min_flags
= min_flags
;
1678 prev_max_flags
= max_flags
;
1679 prev_min_on_coord
= min_on_coord
;
1680 prev_max_on_coord
= max_on_coord
;
1684 axis
->num_segments
--;
1694 /* now exit if we are at the start/end point */
1695 if ( point
== last
)
1702 /* if we are not on an edge, check whether the major direction */
1703 /* coincides with the current point's `out' direction, or */
1704 /* whether we have a single-point contour */
1706 ( FT_ABS( point
->out_dir
) == major_dir
||
1707 point
== point
->prev
) )
1709 /* this is the start of a new segment! */
1710 segment_dir
= (AF_Direction
)point
->out_dir
;
1712 error
= af_axis_hints_new_segment( axis
, memory
, &segment
);
1716 /* clear all segment fields */
1719 segment
->dir
= (FT_Char
)segment_dir
;
1720 segment
->first
= point
;
1721 segment
->last
= point
;
1723 /* `af_axis_hints_new_segment' reallocates memory, */
1724 /* thus we have to refresh the `prev_segment' pointer */
1726 prev_segment
= segment
- 1;
1728 min_pos
= max_pos
= point
->u
;
1729 min_coord
= max_coord
= point
->v
;
1730 min_flags
= max_flags
= point
->flags
;
1732 if ( point
->flags
& AF_FLAG_CONTROL
)
1734 min_on_coord
= 32000;
1735 max_on_coord
= -32000;
1738 min_on_coord
= max_on_coord
= point
->v
;
1742 if ( point
== point
->prev
)
1744 /* we have a one-point segment: this is a one-point */
1745 /* contour with `in' and `out' direction set to */
1747 segment
->pos
= (FT_Short
)min_pos
;
1749 if (point
->flags
& AF_FLAG_CONTROL
)
1750 segment
->flags
|= AF_EDGE_ROUND
;
1752 segment
->min_coord
= (FT_Short
)point
->v
;
1753 segment
->max_coord
= (FT_Short
)point
->v
;
1754 segment
->height
= 0;
1761 point
= point
->next
;
1767 /* now slightly increase the height of segments if this makes */
1768 /* sense -- this is used to better detect and ignore serifs */
1770 AF_Segment segments
= axis
->segments
;
1771 AF_Segment segments_end
= segments
+ axis
->num_segments
;
1774 for ( segment
= segments
; segment
< segments_end
; segment
++ )
1776 AF_Point first
= segment
->first
;
1777 AF_Point last
= segment
->last
;
1778 FT_Pos first_v
= first
->v
;
1779 FT_Pos last_v
= last
->v
;
1782 if ( first_v
< last_v
)
1788 if ( p
->v
< first_v
)
1789 segment
->height
= (FT_Short
)( segment
->height
+
1790 ( ( first_v
- p
->v
) >> 1 ) );
1793 if ( p
->v
> last_v
)
1794 segment
->height
= (FT_Short
)( segment
->height
+
1795 ( ( p
->v
- last_v
) >> 1 ) );
1803 if ( p
->v
> first_v
)
1804 segment
->height
= (FT_Short
)( segment
->height
+
1805 ( ( p
->v
- first_v
) >> 1 ) );
1808 if ( p
->v
< last_v
)
1809 segment
->height
= (FT_Short
)( segment
->height
+
1810 ( ( last_v
- p
->v
) >> 1 ) );
1820 /* Link segments to form stems and serifs. If `width_count' and */
1821 /* `widths' are non-zero, use them to fine-tune the scoring function. */
1823 FT_LOCAL_DEF( void )
1824 af_latin_hints_link_segments( AF_GlyphHints hints
,
1825 FT_UInt width_count
,
1826 AF_WidthRec
* widths
,
1829 AF_AxisHints axis
= &hints
->axis
[dim
];
1830 AF_Segment segments
= axis
->segments
;
1831 AF_Segment segment_limit
= segments
+ axis
->num_segments
;
1832 FT_Pos len_threshold
, len_score
, dist_score
, max_width
;
1833 AF_Segment seg1
, seg2
;
1837 max_width
= widths
[width_count
- 1].org
;
1841 /* a heuristic value to set up a minimum value for overlapping */
1842 len_threshold
= AF_LATIN_CONSTANT( hints
->metrics
, 8 );
1843 if ( len_threshold
== 0 )
1846 /* a heuristic value to weight lengths */
1847 len_score
= AF_LATIN_CONSTANT( hints
->metrics
, 6000 );
1849 /* a heuristic value to weight distances (no call to */
1850 /* AF_LATIN_CONSTANT needed, since we work on multiples */
1851 /* of the stem width) */
1854 /* now compare each segment to the others */
1855 for ( seg1
= segments
; seg1
< segment_limit
; seg1
++ )
1857 if ( seg1
->dir
!= axis
->major_dir
)
1860 /* search for stems having opposite directions, */
1861 /* with seg1 to the `left' of seg2 */
1862 for ( seg2
= segments
; seg2
< segment_limit
; seg2
++ )
1864 FT_Pos pos1
= seg1
->pos
;
1865 FT_Pos pos2
= seg2
->pos
;
1868 if ( seg1
->dir
+ seg2
->dir
== 0 && pos2
> pos1
)
1870 /* compute distance between the two segments */
1871 FT_Pos min
= seg1
->min_coord
;
1872 FT_Pos max
= seg1
->max_coord
;
1876 if ( min
< seg2
->min_coord
)
1877 min
= seg2
->min_coord
;
1879 if ( max
> seg2
->max_coord
)
1880 max
= seg2
->max_coord
;
1882 /* compute maximum coordinate difference of the two segments */
1883 /* (this is, how much they overlap) */
1885 if ( len
>= len_threshold
)
1888 * The score is the sum of two demerits indicating the
1889 * `badness' of a fit, measured along the segments' main axis
1890 * and orthogonal to it, respectively.
1892 * o The less overlapping along the main axis, the worse it
1893 * is, causing a larger demerit.
1895 * o The nearer the orthogonal distance to a stem width, the
1896 * better it is, causing a smaller demerit. For simplicity,
1897 * however, we only increase the demerit for values that
1898 * exceed the largest stem width.
1901 FT_Pos dist
= pos2
- pos1
;
1903 FT_Pos dist_demerit
, score
;
1908 /* distance demerits are based on multiples of `max_width'; */
1909 /* we scale by 1024 for getting more precision */
1910 FT_Pos delta
= ( dist
<< 10 ) / max_width
- ( 1 << 10 );
1913 if ( delta
> 10000 )
1914 dist_demerit
= 32000;
1915 else if ( delta
> 0 )
1916 dist_demerit
= delta
* delta
/ dist_score
;
1921 dist_demerit
= dist
; /* default if no widths available */
1923 score
= dist_demerit
+ len_score
/ len
;
1925 /* and we search for the smallest score */
1926 if ( score
< seg1
->score
)
1928 seg1
->score
= score
;
1932 if ( score
< seg2
->score
)
1934 seg2
->score
= score
;
1942 /* now compute the `serif' segments, cf. explanations in `afhints.h' */
1943 for ( seg1
= segments
; seg1
< segment_limit
; seg1
++ )
1949 if ( seg2
->link
!= seg1
)
1952 seg1
->serif
= seg2
->link
;
1959 /* Link segments to edges, using feature analysis for selection. */
1961 FT_LOCAL_DEF( FT_Error
)
1962 af_latin_hints_compute_edges( AF_GlyphHints hints
,
1965 AF_AxisHints axis
= &hints
->axis
[dim
];
1966 FT_Error error
= FT_Err_Ok
;
1967 FT_Memory memory
= hints
->memory
;
1968 AF_LatinAxis laxis
= &((AF_LatinMetrics
)hints
->metrics
)->axis
[dim
];
1970 AF_StyleClass style_class
= hints
->metrics
->style_class
;
1971 AF_ScriptClass script_class
= AF_SCRIPT_CLASSES_GET
1972 [style_class
->script
];
1974 FT_Bool top_to_bottom_hinting
= 0;
1976 AF_Segment segments
= axis
->segments
;
1977 AF_Segment segment_limit
= segments
+ axis
->num_segments
;
1981 AF_Direction up_dir
;
1984 FT_Pos edge_distance_threshold
;
1985 FT_Pos segment_length_threshold
;
1986 FT_Pos segment_width_threshold
;
1989 axis
->num_edges
= 0;
1991 scale
= ( dim
== AF_DIMENSION_HORZ
) ? hints
->x_scale
1995 up_dir
= ( dim
== AF_DIMENSION_HORZ
) ? AF_DIR_UP
1999 if ( dim
== AF_DIMENSION_VERT
)
2000 top_to_bottom_hinting
= script_class
->top_to_bottom_hinting
;
2003 * We ignore all segments that are less than 1 pixel in length
2004 * to avoid many problems with serif fonts. We compute the
2005 * corresponding threshold in font units.
2007 if ( dim
== AF_DIMENSION_HORZ
)
2008 segment_length_threshold
= FT_DivFix( 64, hints
->y_scale
);
2010 segment_length_threshold
= 0;
2013 * Similarly, we ignore segments that have a width delta
2014 * larger than 0.5px (i.e., a width larger than 1px).
2016 segment_width_threshold
= FT_DivFix( 32, scale
);
2018 /*********************************************************************/
2020 /* We begin by generating a sorted table of edges for the current */
2021 /* direction. To do so, we simply scan each segment and try to find */
2022 /* an edge in our table that corresponds to its position. */
2024 /* If no edge is found, we create and insert a new edge in the */
2025 /* sorted table. Otherwise, we simply add the segment to the edge's */
2026 /* list which gets processed in the second step to compute the */
2027 /* edge's properties. */
2029 /* Note that the table of edges is sorted along the segment/edge */
2032 /*********************************************************************/
2034 /* assure that edge distance threshold is at most 0.25px */
2035 edge_distance_threshold
= FT_MulFix( laxis
->edge_distance_threshold
,
2037 if ( edge_distance_threshold
> 64 / 4 )
2038 edge_distance_threshold
= 64 / 4;
2040 edge_distance_threshold
= FT_DivFix( edge_distance_threshold
,
2043 for ( seg
= segments
; seg
< segment_limit
; seg
++ )
2045 AF_Edge found
= NULL
;
2049 /* ignore too short segments, too wide ones, and, in this loop, */
2050 /* one-point segments without a direction */
2051 if ( seg
->height
< segment_length_threshold
||
2052 seg
->delta
> segment_width_threshold
||
2053 seg
->dir
== AF_DIR_NONE
)
2056 /* A special case for serif edges: If they are smaller than */
2057 /* 1.5 pixels we ignore them. */
2059 2 * seg
->height
< 3 * segment_length_threshold
)
2062 /* look for an edge corresponding to the segment */
2063 for ( ee
= 0; ee
< axis
->num_edges
; ee
++ )
2065 AF_Edge edge
= axis
->edges
+ ee
;
2069 dist
= seg
->pos
- edge
->fpos
;
2073 if ( dist
< edge_distance_threshold
&& edge
->dir
== seg
->dir
)
2085 /* insert a new edge in the list and */
2086 /* sort according to the position */
2087 error
= af_axis_hints_new_edge( axis
, seg
->pos
,
2088 (AF_Direction
)seg
->dir
,
2089 top_to_bottom_hinting
,
2094 /* add the segment to the new edge's list */
2099 edge
->dir
= seg
->dir
;
2100 edge
->fpos
= seg
->pos
;
2101 edge
->opos
= FT_MulFix( seg
->pos
, scale
);
2102 edge
->pos
= edge
->opos
;
2103 seg
->edge_next
= seg
;
2107 /* if an edge was found, simply add the segment to the edge's */
2109 seg
->edge_next
= found
->first
;
2110 found
->last
->edge_next
= seg
;
2115 /* we loop again over all segments to catch one-point segments */
2116 /* without a direction: if possible, link them to existing edges */
2117 for ( seg
= segments
; seg
< segment_limit
; seg
++ )
2119 AF_Edge found
= NULL
;
2123 if ( seg
->dir
!= AF_DIR_NONE
)
2126 /* look for an edge corresponding to the segment */
2127 for ( ee
= 0; ee
< axis
->num_edges
; ee
++ )
2129 AF_Edge edge
= axis
->edges
+ ee
;
2133 dist
= seg
->pos
- edge
->fpos
;
2137 if ( dist
< edge_distance_threshold
)
2144 /* one-point segments without a match are ignored */
2147 seg
->edge_next
= found
->first
;
2148 found
->last
->edge_next
= seg
;
2154 /******************************************************************/
2156 /* Good, we now compute each edge's properties according to the */
2157 /* segments found on its position. Basically, these are */
2159 /* - the edge's main direction */
2160 /* - stem edge, serif edge or both (which defaults to stem then) */
2161 /* - rounded edge, straight or both (which defaults to straight) */
2162 /* - link for edge */
2164 /******************************************************************/
2166 /* first of all, set the `edge' field in each segment -- this is */
2167 /* required in order to compute edge links */
2170 * Note that removing this loop and setting the `edge' field of each
2171 * segment directly in the code above slows down execution speed for
2172 * some reasons on platforms like the Sun.
2175 AF_Edge edges
= axis
->edges
;
2176 AF_Edge edge_limit
= edges
+ axis
->num_edges
;
2180 for ( edge
= edges
; edge
< edge_limit
; edge
++ )
2187 seg
= seg
->edge_next
;
2189 } while ( seg
!= edge
->first
);
2192 /* now compute each edge properties */
2193 for ( edge
= edges
; edge
< edge_limit
; edge
++ )
2195 FT_Int is_round
= 0; /* does it contain round segments? */
2196 FT_Int is_straight
= 0; /* does it contain straight segments? */
2198 FT_Pos ups
= 0; /* number of upwards segments */
2199 FT_Pos downs
= 0; /* number of downwards segments */
2210 /* check for roundness of segment */
2211 if ( seg
->flags
& AF_EDGE_ROUND
)
2217 /* check for segment direction */
2218 if ( seg
->dir
== up_dir
)
2219 ups
+= seg
->max_coord
- seg
->min_coord
;
2221 downs
+= seg
->max_coord
- seg
->min_coord
;
2224 /* check for links -- if seg->serif is set, then seg->link must */
2226 is_serif
= (FT_Bool
)( seg
->serif
&&
2228 seg
->serif
->edge
!= edge
);
2230 if ( ( seg
->link
&& seg
->link
->edge
!= NULL
) || is_serif
)
2242 edge2
= edge
->serif
;
2251 edge_delta
= edge
->fpos
- edge2
->fpos
;
2252 if ( edge_delta
< 0 )
2253 edge_delta
= -edge_delta
;
2255 seg_delta
= seg
->pos
- seg2
->pos
;
2256 if ( seg_delta
< 0 )
2257 seg_delta
= -seg_delta
;
2259 if ( seg_delta
< edge_delta
)
2267 edge
->serif
= edge2
;
2268 edge2
->flags
|= AF_EDGE_SERIF
;
2274 seg
= seg
->edge_next
;
2276 } while ( seg
!= edge
->first
);
2278 /* set the round/straight flags */
2279 edge
->flags
= AF_EDGE_NORMAL
;
2281 if ( is_round
> 0 && is_round
>= is_straight
)
2282 edge
->flags
|= AF_EDGE_ROUND
;
2285 /* set the edge's main direction */
2286 edge
->dir
= AF_DIR_NONE
;
2289 edge
->dir
= (FT_Char
)up_dir
;
2291 else if ( ups
< downs
)
2292 edge
->dir
= (FT_Char
)-up_dir
;
2294 else if ( ups
== downs
)
2295 edge
->dir
= 0; /* both up and down! */
2298 /* get rid of serifs if link is set */
2299 /* XXX: This gets rid of many unpleasant artefacts! */
2300 /* Example: the `c' in cour.pfa at size 13 */
2302 if ( edge
->serif
&& edge
->link
)
2312 /* Detect segments and edges for given dimension. */
2314 FT_LOCAL_DEF( FT_Error
)
2315 af_latin_hints_detect_features( AF_GlyphHints hints
,
2316 FT_UInt width_count
,
2317 AF_WidthRec
* widths
,
2323 error
= af_latin_hints_compute_segments( hints
, dim
);
2326 af_latin_hints_link_segments( hints
, width_count
, widths
, dim
);
2328 error
= af_latin_hints_compute_edges( hints
, dim
);
2335 /* Compute all edges which lie within blue zones. */
2338 af_latin_hints_compute_blue_edges( AF_GlyphHints hints
,
2339 AF_LatinMetrics metrics
)
2341 AF_AxisHints axis
= &hints
->axis
[AF_DIMENSION_VERT
];
2342 AF_Edge edge
= axis
->edges
;
2343 AF_Edge edge_limit
= edge
+ axis
->num_edges
;
2344 AF_LatinAxis latin
= &metrics
->axis
[AF_DIMENSION_VERT
];
2345 FT_Fixed scale
= latin
->scale
;
2348 /* compute which blue zones are active, i.e. have their scaled */
2349 /* size < 3/4 pixels */
2351 /* for each horizontal edge search the blue zone which is closest */
2352 for ( ; edge
< edge_limit
; edge
++ )
2355 AF_Width best_blue
= NULL
;
2356 FT_Bool best_blue_is_neutral
= 0;
2357 FT_Pos best_dist
; /* initial threshold */
2360 /* compute the initial threshold as a fraction of the EM size */
2361 /* (the value 40 is heuristic) */
2362 best_dist
= FT_MulFix( metrics
->units_per_em
/ 40, scale
);
2364 /* assure a minimum distance of 0.5px */
2365 if ( best_dist
> 64 / 2 )
2368 for ( bb
= 0; bb
< latin
->blue_count
; bb
++ )
2370 AF_LatinBlue blue
= latin
->blues
+ bb
;
2371 FT_Bool is_top_blue
, is_neutral_blue
, is_major_dir
;
2374 /* skip inactive blue zones (i.e., those that are too large) */
2375 if ( !( blue
->flags
& AF_LATIN_BLUE_ACTIVE
) )
2378 /* if it is a top zone, check for right edges (against the major */
2379 /* direction); if it is a bottom zone, check for left edges (in */
2380 /* the major direction) -- this assumes the TrueType convention */
2381 /* for the orientation of contours */
2383 (FT_Byte
)( ( blue
->flags
& ( AF_LATIN_BLUE_TOP
|
2384 AF_LATIN_BLUE_SUB_TOP
) ) != 0 );
2386 (FT_Byte
)( ( blue
->flags
& AF_LATIN_BLUE_NEUTRAL
) != 0);
2388 FT_BOOL( edge
->dir
== axis
->major_dir
);
2390 /* neutral blue zones are handled for both directions */
2391 if ( is_top_blue
^ is_major_dir
|| is_neutral_blue
)
2396 /* first of all, compare it to the reference position */
2397 dist
= edge
->fpos
- blue
->ref
.org
;
2401 dist
= FT_MulFix( dist
, scale
);
2402 if ( dist
< best_dist
)
2405 best_blue
= &blue
->ref
;
2406 best_blue_is_neutral
= is_neutral_blue
;
2409 /* now compare it to the overshoot position and check whether */
2410 /* the edge is rounded, and whether the edge is over the */
2411 /* reference position of a top zone, or under the reference */
2412 /* position of a bottom zone (provided we don't have a */
2413 /* neutral blue zone) */
2414 if ( edge
->flags
& AF_EDGE_ROUND
&&
2418 FT_Bool is_under_ref
= FT_BOOL( edge
->fpos
< blue
->ref
.org
);
2421 if ( is_top_blue
^ is_under_ref
)
2423 dist
= edge
->fpos
- blue
->shoot
.org
;
2427 dist
= FT_MulFix( dist
, scale
);
2428 if ( dist
< best_dist
)
2431 best_blue
= &blue
->shoot
;
2432 best_blue_is_neutral
= is_neutral_blue
;
2441 edge
->blue_edge
= best_blue
;
2442 if ( best_blue_is_neutral
)
2443 edge
->flags
|= AF_EDGE_NEUTRAL
;
2449 /* Initalize hinting engine. */
2452 af_latin_hints_init( AF_GlyphHints hints
,
2453 AF_LatinMetrics metrics
)
2455 FT_Render_Mode mode
;
2456 FT_UInt32 scaler_flags
, other_flags
;
2457 FT_Face face
= metrics
->root
.scaler
.face
;
2460 af_glyph_hints_rescale( hints
, (AF_StyleMetrics
)metrics
);
2463 * correct x_scale and y_scale if needed, since they may have
2464 * been modified by `af_latin_metrics_scale_dim' above
2466 hints
->x_scale
= metrics
->axis
[AF_DIMENSION_HORZ
].scale
;
2467 hints
->x_delta
= metrics
->axis
[AF_DIMENSION_HORZ
].delta
;
2468 hints
->y_scale
= metrics
->axis
[AF_DIMENSION_VERT
].scale
;
2469 hints
->y_delta
= metrics
->axis
[AF_DIMENSION_VERT
].delta
;
2471 /* compute flags depending on render mode, etc. */
2472 mode
= metrics
->root
.scaler
.render_mode
;
2474 #if 0 /* #ifdef AF_CONFIG_OPTION_USE_WARPER */
2475 if ( mode
== FT_RENDER_MODE_LCD
|| mode
== FT_RENDER_MODE_LCD_V
)
2476 metrics
->root
.scaler
.render_mode
= mode
= FT_RENDER_MODE_NORMAL
;
2479 scaler_flags
= hints
->scaler_flags
;
2483 * We snap the width of vertical stems for the monochrome and
2484 * horizontal LCD rendering targets only.
2486 if ( mode
== FT_RENDER_MODE_MONO
|| mode
== FT_RENDER_MODE_LCD
)
2487 other_flags
|= AF_LATIN_HINTS_HORZ_SNAP
;
2490 * We snap the width of horizontal stems for the monochrome and
2491 * vertical LCD rendering targets only.
2493 if ( mode
== FT_RENDER_MODE_MONO
|| mode
== FT_RENDER_MODE_LCD_V
)
2494 other_flags
|= AF_LATIN_HINTS_VERT_SNAP
;
2497 * We adjust stems to full pixels only if we don't use the `light' mode.
2499 if ( mode
!= FT_RENDER_MODE_LIGHT
)
2500 other_flags
|= AF_LATIN_HINTS_STEM_ADJUST
;
2502 if ( mode
== FT_RENDER_MODE_MONO
)
2503 other_flags
|= AF_LATIN_HINTS_MONO
;
2506 * In `light' hinting mode we disable horizontal hinting completely.
2507 * We also do it if the face is italic.
2509 * However, if warping is enabled (which only works in `light' hinting
2510 * mode), advance widths get adjusted, too.
2512 if ( mode
== FT_RENDER_MODE_LIGHT
||
2513 ( face
->style_flags
& FT_STYLE_FLAG_ITALIC
) != 0 )
2514 scaler_flags
|= AF_SCALER_FLAG_NO_HORIZONTAL
;
2516 #ifdef AF_CONFIG_OPTION_USE_WARPER
2517 /* get (global) warper flag */
2518 if ( !metrics
->root
.globals
->module
->warping
)
2519 scaler_flags
|= AF_SCALER_FLAG_NO_WARPER
;
2522 hints
->scaler_flags
= scaler_flags
;
2523 hints
->other_flags
= other_flags
;
2529 /*************************************************************************/
2530 /*************************************************************************/
2532 /***** L A T I N G L Y P H G R I D - F I T T I N G *****/
2534 /*************************************************************************/
2535 /*************************************************************************/
2537 /* Snap a given width in scaled coordinates to one of the */
2538 /* current standard widths. */
2541 af_latin_snap_width( AF_Width widths
,
2546 FT_Pos best
= 64 + 32 + 2;
2547 FT_Pos reference
= width
;
2551 for ( n
= 0; n
< count
; n
++ )
2568 scaled
= FT_PIX_ROUND( reference
);
2570 if ( width
>= reference
)
2572 if ( width
< scaled
+ 48 )
2577 if ( width
> scaled
- 48 )
2585 /* Compute the snapped width of a given stem, ignoring very thin ones. */
2586 /* There is a lot of voodoo in this function; changing the hard-coded */
2587 /* parameters influence the whole hinting process. */
2590 af_latin_compute_stem_width( AF_GlyphHints hints
,
2595 FT_UInt stem_flags
)
2597 AF_LatinMetrics metrics
= (AF_LatinMetrics
)hints
->metrics
;
2598 AF_LatinAxis axis
= &metrics
->axis
[dim
];
2599 FT_Pos dist
= width
;
2601 FT_Int vertical
= ( dim
== AF_DIMENSION_VERT
);
2604 if ( !AF_LATIN_HINTS_DO_STEM_ADJUST( hints
) ||
2614 if ( ( vertical
&& !AF_LATIN_HINTS_DO_VERT_SNAP( hints
) ) ||
2615 ( !vertical
&& !AF_LATIN_HINTS_DO_HORZ_SNAP( hints
) ) )
2617 /* smooth hinting process: very lightly quantize the stem width */
2619 /* leave the widths of serifs alone */
2620 if ( ( stem_flags
& AF_EDGE_SERIF
) &&
2625 else if ( base_flags
& AF_EDGE_ROUND
)
2630 else if ( dist
< 56 )
2633 if ( axis
->width_count
> 0 )
2638 /* compare to standard width */
2639 delta
= dist
- axis
->widths
[0].cur
;
2646 dist
= axis
->widths
[0].cur
;
2653 if ( dist
< 3 * 64 )
2661 else if ( delta
< 32 )
2664 else if ( delta
< 54 )
2672 /* A stem's end position depends on two values: the start */
2673 /* position and the stem length. The former gets usually */
2674 /* rounded to the grid, while the latter gets rounded also if it */
2675 /* exceeds a certain length (see below in this function). This */
2676 /* `double rounding' can lead to a great difference to the */
2677 /* original, unhinted position; this normally doesn't matter for */
2678 /* large PPEM values, but for small sizes it can easily make */
2679 /* outlines collide. For this reason, we adjust the stem length */
2680 /* by a small amount depending on the PPEM value in case the */
2681 /* former and latter rounding both point into the same */
2687 if ( ( ( width
> 0 ) && ( base_delta
> 0 ) ) ||
2688 ( ( width
< 0 ) && ( base_delta
< 0 ) ) )
2690 FT_UInt ppem
= metrics
->root
.scaler
.face
->size
->metrics
.x_ppem
;
2694 bdelta
= base_delta
;
2695 else if ( ppem
< 30 )
2696 bdelta
= ( base_delta
* (FT_Pos
)( 30 - ppem
) ) / 20;
2702 dist
= ( dist
- bdelta
+ 32 ) & ~63;
2708 /* strong hinting process: snap the stem width to integer pixels */
2710 FT_Pos org_dist
= dist
;
2713 dist
= af_latin_snap_width( axis
->widths
, axis
->width_count
, dist
);
2717 /* in the case of vertical hinting, always round */
2718 /* the stem heights to integer pixels */
2721 dist
= ( dist
+ 16 ) & ~63;
2727 if ( AF_LATIN_HINTS_DO_MONO( hints
) )
2729 /* monochrome horizontal hinting: snap widths to integer pixels */
2730 /* with a different threshold */
2735 dist
= ( dist
+ 32 ) & ~63;
2739 /* for horizontal anti-aliased hinting, we adopt a more subtle */
2740 /* approach: we strengthen small stems, round stems whose size */
2741 /* is between 1 and 2 pixels to an integer, otherwise nothing */
2744 dist
= ( dist
+ 64 ) >> 1;
2746 else if ( dist
< 128 )
2748 /* We only round to an integer width if the corresponding */
2749 /* distortion is less than 1/4 pixel. Otherwise this */
2750 /* makes everything worse since the diagonals, which are */
2751 /* not hinted, appear a lot bolder or thinner than the */
2752 /* vertical stems. */
2757 dist
= ( dist
+ 22 ) & ~63;
2758 delta
= dist
- org_dist
;
2766 dist
= ( dist
+ 64 ) >> 1;
2770 /* round otherwise to prevent color fringes in LCD mode */
2771 dist
= ( dist
+ 32 ) & ~63;
2784 /* Align one stem edge relative to the previous stem edge. */
2787 af_latin_align_linked_edge( AF_GlyphHints hints
,
2792 FT_Pos dist
, base_delta
;
2793 FT_Pos fitted_width
;
2796 dist
= stem_edge
->opos
- base_edge
->opos
;
2797 base_delta
= base_edge
->pos
- base_edge
->opos
;
2799 fitted_width
= af_latin_compute_stem_width( hints
, dim
,
2805 stem_edge
->pos
= base_edge
->pos
+ fitted_width
;
2807 FT_TRACE5(( " LINK: edge %d (opos=%.2f) linked to %.2f,"
2808 " dist was %.2f, now %.2f\n",
2809 stem_edge
- hints
->axis
[dim
].edges
, stem_edge
->opos
/ 64.0,
2810 stem_edge
->pos
/ 64.0, dist
/ 64.0, fitted_width
/ 64.0 ));
2814 /* Shift the coordinates of the `serif' edge by the same amount */
2815 /* as the corresponding `base' edge has been moved already. */
2818 af_latin_align_serif_edge( AF_GlyphHints hints
,
2824 serif
->pos
= base
->pos
+ ( serif
->opos
- base
->opos
);
2828 /*************************************************************************/
2829 /*************************************************************************/
2830 /*************************************************************************/
2832 /**** E D G E H I N T I N G ****/
2834 /*************************************************************************/
2835 /*************************************************************************/
2836 /*************************************************************************/
2839 /* The main grid-fitting routine. */
2842 af_latin_hint_edges( AF_GlyphHints hints
,
2845 AF_AxisHints axis
= &hints
->axis
[dim
];
2846 AF_Edge edges
= axis
->edges
;
2847 AF_Edge edge_limit
= edges
+ axis
->num_edges
;
2850 AF_Edge anchor
= NULL
;
2851 FT_Int has_serifs
= 0;
2853 AF_StyleClass style_class
= hints
->metrics
->style_class
;
2854 AF_ScriptClass script_class
= AF_SCRIPT_CLASSES_GET
2855 [style_class
->script
];
2857 FT_Bool top_to_bottom_hinting
= 0;
2859 #ifdef FT_DEBUG_LEVEL_TRACE
2860 FT_UInt num_actions
= 0;
2864 FT_TRACE5(( "latin %s edge hinting (style `%s')\n",
2865 dim
== AF_DIMENSION_VERT
? "horizontal" : "vertical",
2866 af_style_names
[hints
->metrics
->style_class
->style
] ));
2868 if ( dim
== AF_DIMENSION_VERT
)
2869 top_to_bottom_hinting
= script_class
->top_to_bottom_hinting
;
2871 /* we begin by aligning all stems relative to the blue zone */
2872 /* if needed -- that's only for horizontal edges */
2874 if ( dim
== AF_DIMENSION_VERT
&& AF_HINTS_DO_BLUES( hints
) )
2876 for ( edge
= edges
; edge
< edge_limit
; edge
++ )
2879 AF_Edge edge1
, edge2
; /* these edges form the stem to check */
2882 if ( edge
->flags
& AF_EDGE_DONE
)
2889 * If a stem contains both a neutral and a non-neutral blue zone,
2890 * skip the neutral one. Otherwise, outlines with different
2891 * directions might be incorrectly aligned at the same vertical
2894 * If we have two neutral blue zones, skip one of them.
2897 if ( edge
->blue_edge
&& edge2
&& edge2
->blue_edge
)
2899 FT_Byte neutral
= edge
->flags
& AF_EDGE_NEUTRAL
;
2900 FT_Byte neutral2
= edge2
->flags
& AF_EDGE_NEUTRAL
;
2905 edge2
->blue_edge
= NULL
;
2906 edge2
->flags
&= ~AF_EDGE_NEUTRAL
;
2910 edge
->blue_edge
= NULL
;
2911 edge
->flags
&= ~AF_EDGE_NEUTRAL
;
2915 blue
= edge
->blue_edge
;
2919 /* flip edges if the other edge is aligned to a blue zone */
2920 else if ( edge2
&& edge2
->blue_edge
)
2922 blue
= edge2
->blue_edge
;
2930 #ifdef FT_DEBUG_LEVEL_TRACE
2932 FT_TRACE5(( " BLUE_ANCHOR: edge %d (opos=%.2f) snapped to %.2f,"
2933 " was %.2f (anchor=edge %d)\n",
2934 edge1
- edges
, edge1
->opos
/ 64.0, blue
->fit
/ 64.0,
2935 edge1
->pos
/ 64.0, edge
- edges
));
2937 FT_TRACE5(( " BLUE: edge %d (opos=%.2f) snapped to %.2f,"
2939 edge1
- edges
, edge1
->opos
/ 64.0, blue
->fit
/ 64.0,
2940 edge1
->pos
/ 64.0 ));
2945 edge1
->pos
= blue
->fit
;
2946 edge1
->flags
|= AF_EDGE_DONE
;
2948 if ( edge2
&& !edge2
->blue_edge
)
2950 af_latin_align_linked_edge( hints
, dim
, edge1
, edge2
);
2951 edge2
->flags
|= AF_EDGE_DONE
;
2953 #ifdef FT_DEBUG_LEVEL_TRACE
2963 /* now we align all other stem edges, trying to maintain the */
2964 /* relative order of stems in the glyph */
2965 for ( edge
= edges
; edge
< edge_limit
; edge
++ )
2970 if ( edge
->flags
& AF_EDGE_DONE
)
2973 /* skip all non-stem edges */
2981 /* now align the stem */
2983 /* this should not happen, but it's better to be safe */
2984 if ( edge2
->blue_edge
)
2986 FT_TRACE5(( " ASSERTION FAILED for edge %d\n", edge2
- edges
));
2988 af_latin_align_linked_edge( hints
, dim
, edge2
, edge
);
2989 edge
->flags
|= AF_EDGE_DONE
;
2991 #ifdef FT_DEBUG_LEVEL_TRACE
2999 /* if we reach this if clause, no stem has been aligned yet */
3001 FT_Pos org_len
, org_center
, cur_len
;
3002 FT_Pos cur_pos1
, error1
, error2
, u_off
, d_off
;
3005 org_len
= edge2
->opos
- edge
->opos
;
3006 cur_len
= af_latin_compute_stem_width( hints
, dim
,
3011 /* some voodoo to specially round edges for small stem widths; */
3012 /* the idea is to align the center of a stem, then shifting */
3013 /* the stem edges to suitable positions */
3014 if ( cur_len
<= 64 )
3022 /* 1px < width < 1.5px */
3029 org_center
= edge
->opos
+ ( org_len
>> 1 );
3030 cur_pos1
= FT_PIX_ROUND( org_center
);
3032 error1
= org_center
- ( cur_pos1
- u_off
);
3036 error2
= org_center
- ( cur_pos1
+ d_off
);
3040 if ( error1
< error2
)
3045 edge
->pos
= cur_pos1
- cur_len
/ 2;
3046 edge2
->pos
= edge
->pos
+ cur_len
;
3049 edge
->pos
= FT_PIX_ROUND( edge
->opos
);
3052 edge
->flags
|= AF_EDGE_DONE
;
3054 FT_TRACE5(( " ANCHOR: edge %d (opos=%.2f) and %d (opos=%.2f)"
3055 " snapped to %.2f and %.2f\n",
3056 edge
- edges
, edge
->opos
/ 64.0,
3057 edge2
- edges
, edge2
->opos
/ 64.0,
3058 edge
->pos
/ 64.0, edge2
->pos
/ 64.0 ));
3060 af_latin_align_linked_edge( hints
, dim
, edge
, edge2
);
3062 #ifdef FT_DEBUG_LEVEL_TRACE
3068 FT_Pos org_pos
, org_len
, org_center
, cur_len
;
3069 FT_Pos cur_pos1
, cur_pos2
, delta1
, delta2
;
3072 org_pos
= anchor
->pos
+ ( edge
->opos
- anchor
->opos
);
3073 org_len
= edge2
->opos
- edge
->opos
;
3074 org_center
= org_pos
+ ( org_len
>> 1 );
3076 cur_len
= af_latin_compute_stem_width( hints
, dim
,
3081 if ( edge2
->flags
& AF_EDGE_DONE
)
3083 FT_TRACE5(( " ADJUST: edge %d (pos=%.2f) moved to %.2f\n",
3084 edge
- edges
, edge
->pos
/ 64.0,
3085 ( edge2
->pos
- cur_len
) / 64.0 ));
3087 edge
->pos
= edge2
->pos
- cur_len
;
3090 else if ( cur_len
< 96 )
3092 FT_Pos u_off
, d_off
;
3095 cur_pos1
= FT_PIX_ROUND( org_center
);
3097 if ( cur_len
<= 64 )
3108 delta1
= org_center
- ( cur_pos1
- u_off
);
3112 delta2
= org_center
- ( cur_pos1
+ d_off
);
3116 if ( delta1
< delta2
)
3121 edge
->pos
= cur_pos1
- cur_len
/ 2;
3122 edge2
->pos
= cur_pos1
+ cur_len
/ 2;
3124 FT_TRACE5(( " STEM: edge %d (opos=%.2f) linked to %d (opos=%.2f)"
3125 " snapped to %.2f and %.2f\n",
3126 edge
- edges
, edge
->opos
/ 64.0,
3127 edge2
- edges
, edge2
->opos
/ 64.0,
3128 edge
->pos
/ 64.0, edge2
->pos
/ 64.0 ));
3133 org_pos
= anchor
->pos
+ ( edge
->opos
- anchor
->opos
);
3134 org_len
= edge2
->opos
- edge
->opos
;
3135 org_center
= org_pos
+ ( org_len
>> 1 );
3137 cur_len
= af_latin_compute_stem_width( hints
, dim
,
3142 cur_pos1
= FT_PIX_ROUND( org_pos
);
3143 delta1
= cur_pos1
+ ( cur_len
>> 1 ) - org_center
;
3147 cur_pos2
= FT_PIX_ROUND( org_pos
+ org_len
) - cur_len
;
3148 delta2
= cur_pos2
+ ( cur_len
>> 1 ) - org_center
;
3152 edge
->pos
= ( delta1
< delta2
) ? cur_pos1
: cur_pos2
;
3153 edge2
->pos
= edge
->pos
+ cur_len
;
3155 FT_TRACE5(( " STEM: edge %d (opos=%.2f) linked to %d (opos=%.2f)"
3156 " snapped to %.2f and %.2f\n",
3157 edge
- edges
, edge
->opos
/ 64.0,
3158 edge2
- edges
, edge2
->opos
/ 64.0,
3159 edge
->pos
/ 64.0, edge2
->pos
/ 64.0 ));
3162 #ifdef FT_DEBUG_LEVEL_TRACE
3166 edge
->flags
|= AF_EDGE_DONE
;
3167 edge2
->flags
|= AF_EDGE_DONE
;
3169 if ( edge
> edges
&&
3170 ( top_to_bottom_hinting
? ( edge
->pos
> edge
[-1].pos
)
3171 : ( edge
->pos
< edge
[-1].pos
) ) )
3173 /* don't move if stem would (almost) disappear otherwise; */
3174 /* the ad-hoc value 16 corresponds to 1/4px */
3175 if ( edge
->link
&& FT_ABS( edge
->link
->pos
- edge
[-1].pos
) > 16 )
3177 #ifdef FT_DEBUG_LEVEL_TRACE
3178 FT_TRACE5(( " BOUND: edge %d (pos=%.2f) moved to %.2f\n",
3181 edge
[-1].pos
/ 64.0 ));
3186 edge
->pos
= edge
[-1].pos
;
3192 /* make sure that lowercase m's maintain their symmetry */
3194 /* In general, lowercase m's have six vertical edges if they are sans */
3195 /* serif, or twelve if they are with serifs. This implementation is */
3196 /* based on that assumption, and seems to work very well with most */
3197 /* faces. However, if for a certain face this assumption is not */
3198 /* true, the m is just rendered like before. In addition, any stem */
3199 /* correction will only be applied to symmetrical glyphs (even if the */
3200 /* glyph is not an m), so the potential for unwanted distortion is */
3201 /* relatively low. */
3203 /* We don't handle horizontal edges since we can't easily assure that */
3204 /* the third (lowest) stem aligns with the base line; it might end up */
3205 /* one pixel higher or lower. */
3207 n_edges
= edge_limit
- edges
;
3208 if ( dim
== AF_DIMENSION_HORZ
&& ( n_edges
== 6 || n_edges
== 12 ) )
3210 AF_Edge edge1
, edge2
, edge3
;
3211 FT_Pos dist1
, dist2
, span
, delta
;
3227 dist1
= edge2
->opos
- edge1
->opos
;
3228 dist2
= edge3
->opos
- edge2
->opos
;
3230 span
= dist1
- dist2
;
3236 delta
= edge3
->pos
- ( 2 * edge2
->pos
- edge1
->pos
);
3237 edge3
->pos
-= delta
;
3239 edge3
->link
->pos
-= delta
;
3241 /* move the serifs along with the stem */
3242 if ( n_edges
== 12 )
3244 ( edges
+ 8 )->pos
-= delta
;
3245 ( edges
+ 11 )->pos
-= delta
;
3248 edge3
->flags
|= AF_EDGE_DONE
;
3250 edge3
->link
->flags
|= AF_EDGE_DONE
;
3254 if ( has_serifs
|| !anchor
)
3257 * now hint the remaining edges (serifs and single) in order
3258 * to complete our processing
3260 for ( edge
= edges
; edge
< edge_limit
; edge
++ )
3265 if ( edge
->flags
& AF_EDGE_DONE
)
3272 delta
= edge
->serif
->opos
- edge
->opos
;
3277 if ( delta
< 64 + 16 )
3279 af_latin_align_serif_edge( hints
, edge
->serif
, edge
);
3280 FT_TRACE5(( " SERIF: edge %d (opos=%.2f) serif to %d (opos=%.2f)"
3281 " aligned to %.2f\n",
3282 edge
- edges
, edge
->opos
/ 64.0,
3283 edge
->serif
- edges
, edge
->serif
->opos
/ 64.0,
3284 edge
->pos
/ 64.0 ));
3288 edge
->pos
= FT_PIX_ROUND( edge
->opos
);
3290 FT_TRACE5(( " SERIF_ANCHOR: edge %d (opos=%.2f)"
3291 " snapped to %.2f\n",
3292 edge
-edges
, edge
->opos
/ 64.0, edge
->pos
/ 64.0 ));
3296 AF_Edge before
, after
;
3299 for ( before
= edge
- 1; before
>= edges
; before
-- )
3300 if ( before
->flags
& AF_EDGE_DONE
)
3303 for ( after
= edge
+ 1; after
< edge_limit
; after
++ )
3304 if ( after
->flags
& AF_EDGE_DONE
)
3307 if ( before
>= edges
&& before
< edge
&&
3308 after
< edge_limit
&& after
> edge
)
3310 if ( after
->opos
== before
->opos
)
3311 edge
->pos
= before
->pos
;
3313 edge
->pos
= before
->pos
+
3314 FT_MulDiv( edge
->opos
- before
->opos
,
3315 after
->pos
- before
->pos
,
3316 after
->opos
- before
->opos
);
3318 FT_TRACE5(( " SERIF_LINK1: edge %d (opos=%.2f) snapped to %.2f"
3319 " from %d (opos=%.2f)\n",
3320 edge
- edges
, edge
->opos
/ 64.0,
3322 before
- edges
, before
->opos
/ 64.0 ));
3326 edge
->pos
= anchor
->pos
+
3327 ( ( edge
->opos
- anchor
->opos
+ 16 ) & ~31 );
3328 FT_TRACE5(( " SERIF_LINK2: edge %d (opos=%.2f)"
3329 " snapped to %.2f\n",
3330 edge
- edges
, edge
->opos
/ 64.0, edge
->pos
/ 64.0 ));
3334 #ifdef FT_DEBUG_LEVEL_TRACE
3337 edge
->flags
|= AF_EDGE_DONE
;
3339 if ( edge
> edges
&&
3340 ( top_to_bottom_hinting
? ( edge
->pos
> edge
[-1].pos
)
3341 : ( edge
->pos
< edge
[-1].pos
) ) )
3343 /* don't move if stem would (almost) disappear otherwise; */
3344 /* the ad-hoc value 16 corresponds to 1/4px */
3345 if ( edge
->link
&& FT_ABS( edge
->link
->pos
- edge
[-1].pos
) > 16 )
3347 #ifdef FT_DEBUG_LEVEL_TRACE
3348 FT_TRACE5(( " BOUND: edge %d (pos=%.2f) moved to %.2f\n",
3351 edge
[-1].pos
/ 64.0 ));
3355 edge
->pos
= edge
[-1].pos
;
3359 if ( edge
+ 1 < edge_limit
&&
3360 edge
[1].flags
& AF_EDGE_DONE
&&
3361 ( top_to_bottom_hinting
? ( edge
->pos
< edge
[1].pos
)
3362 : ( edge
->pos
> edge
[1].pos
) ) )
3364 /* don't move if stem would (almost) disappear otherwise; */
3365 /* the ad-hoc value 16 corresponds to 1/4px */
3366 if ( edge
->link
&& FT_ABS( edge
->link
->pos
- edge
[-1].pos
) > 16 )
3368 #ifdef FT_DEBUG_LEVEL_TRACE
3369 FT_TRACE5(( " BOUND: edge %d (pos=%.2f) moved to %.2f\n",
3372 edge
[1].pos
/ 64.0 ));
3377 edge
->pos
= edge
[1].pos
;
3383 #ifdef FT_DEBUG_LEVEL_TRACE
3385 FT_TRACE5(( " (none)\n" ));
3386 FT_TRACE5(( "\n" ));
3391 /* Apply the complete hinting algorithm to a latin glyph. */
3394 af_latin_hints_apply( FT_UInt glyph_index
,
3395 AF_GlyphHints hints
,
3396 FT_Outline
* outline
,
3397 AF_LatinMetrics metrics
)
3405 error
= af_glyph_hints_reload( hints
, outline
);
3409 /* analyze glyph outline */
3410 #ifdef AF_CONFIG_OPTION_USE_WARPER
3411 if ( ( metrics
->root
.scaler
.render_mode
== FT_RENDER_MODE_LIGHT
&&
3412 AF_HINTS_DO_WARP( hints
) ) ||
3413 AF_HINTS_DO_HORIZONTAL( hints
) )
3415 if ( AF_HINTS_DO_HORIZONTAL( hints
) )
3418 axis
= &metrics
->axis
[AF_DIMENSION_HORZ
];
3419 error
= af_latin_hints_detect_features( hints
,
3422 AF_DIMENSION_HORZ
);
3427 if ( AF_HINTS_DO_VERTICAL( hints
) )
3429 axis
= &metrics
->axis
[AF_DIMENSION_VERT
];
3430 error
= af_latin_hints_detect_features( hints
,
3433 AF_DIMENSION_VERT
);
3437 /* apply blue zones to base characters only */
3438 if ( !( metrics
->root
.globals
->glyph_styles
[glyph_index
] & AF_NONBASE
) )
3439 af_latin_hints_compute_blue_edges( hints
, metrics
);
3442 /* grid-fit the outline */
3443 for ( dim
= 0; dim
< AF_DIMENSION_MAX
; dim
++ )
3445 #ifdef AF_CONFIG_OPTION_USE_WARPER
3446 if ( dim
== AF_DIMENSION_HORZ
&&
3447 metrics
->root
.scaler
.render_mode
== FT_RENDER_MODE_LIGHT
&&
3448 AF_HINTS_DO_WARP( hints
) )
3450 AF_WarperRec warper
;
3455 af_warper_compute( &warper
, hints
, (AF_Dimension
)dim
,
3457 af_glyph_hints_scale_dim( hints
, (AF_Dimension
)dim
,
3461 #endif /* AF_CONFIG_OPTION_USE_WARPER */
3463 if ( ( dim
== AF_DIMENSION_HORZ
&& AF_HINTS_DO_HORIZONTAL( hints
) ) ||
3464 ( dim
== AF_DIMENSION_VERT
&& AF_HINTS_DO_VERTICAL( hints
) ) )
3466 af_latin_hint_edges( hints
, (AF_Dimension
)dim
);
3467 af_glyph_hints_align_edge_points( hints
, (AF_Dimension
)dim
);
3468 af_glyph_hints_align_strong_points( hints
, (AF_Dimension
)dim
);
3469 af_glyph_hints_align_weak_points( hints
, (AF_Dimension
)dim
);
3473 af_glyph_hints_save( hints
, outline
);
3480 /*************************************************************************/
3481 /*************************************************************************/
3483 /***** L A T I N S C R I P T C L A S S *****/
3485 /*************************************************************************/
3486 /*************************************************************************/
3489 AF_DEFINE_WRITING_SYSTEM_CLASS(
3490 af_latin_writing_system_class
,
3492 AF_WRITING_SYSTEM_LATIN
,
3494 sizeof ( AF_LatinMetricsRec
),
3496 (AF_WritingSystem_InitMetricsFunc
) af_latin_metrics_init
,
3497 (AF_WritingSystem_ScaleMetricsFunc
)af_latin_metrics_scale
,
3498 (AF_WritingSystem_DoneMetricsFunc
) NULL
,
3499 (AF_WritingSystem_GetStdWidthsFunc
)af_latin_get_standard_widths
,
3501 (AF_WritingSystem_InitHintsFunc
) af_latin_hints_init
,
3502 (AF_WritingSystem_ApplyHintsFunc
) af_latin_hints_apply