1 /***************************************************************************/
5 /* Auto-fitter hinting routines for latin writing system (body). */
7 /* Copyright 2003-2014 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 /*************************************************************************/
45 /*************************************************************************/
47 /***** L A T I N G L O B A L M E T R I C S *****/
49 /*************************************************************************/
50 /*************************************************************************/
53 /* Find segments and links, compute all stem widths, and initialize */
54 /* standard width and height for the glyph with given charcode. */
57 af_latin_metrics_init_widths( AF_LatinMetrics metrics,
60 /* scan the array of segments in each direction */
61 AF_GlyphHintsRec hints[1];
65 "latin standard widths computation (style `%s')\n"
66 "=====================================================\n"
68 af_style_names[metrics->root.style_class->style] ));
70 af_glyph_hints_init( hints, face->memory );
72 metrics->axis[AF_DIMENSION_HORZ].width_count = 0;
73 metrics->axis[AF_DIMENSION_VERT].width_count = 0;
80 AF_LatinMetricsRec dummy[1];
81 AF_Scaler scaler = &dummy->root.scaler;
83 #ifdef FT_CONFIG_OPTION_PIC
84 AF_FaceGlobals globals = metrics->root.globals;
87 AF_StyleClass style_class = metrics->root.style_class;
88 AF_ScriptClass script_class = AF_SCRIPT_CLASSES_GET
89 [style_class->script];
91 FT_UInt32 standard_char;
95 * We check more than a single standard character to catch features
96 * like `c2sc' (small caps from caps) that don't contain lowercase
97 * letters by definition, or other features that mainly operate on
101 standard_char = script_class->standard_char1;
102 af_get_char_index( &metrics->root,
108 if ( script_class->standard_char2 )
110 standard_char = script_class->standard_char2;
111 af_get_char_index( &metrics->root,
117 if ( script_class->standard_char3 )
119 standard_char = script_class->standard_char3;
120 af_get_char_index( &metrics->root,
135 FT_TRACE5(( "standard character: U+%04lX (glyph index %d)\n",
136 standard_char, glyph_index ));
138 error = FT_Load_Glyph( face, glyph_index, FT_LOAD_NO_SCALE );
139 if ( error || face->glyph->outline.n_points <= 0 )
144 dummy->units_per_em = metrics->units_per_em;
146 scaler->x_scale = 0x10000L;
147 scaler->y_scale = 0x10000L;
152 scaler->render_mode = FT_RENDER_MODE_NORMAL;
155 af_glyph_hints_rescale( hints, (AF_StyleMetrics)dummy );
157 error = af_glyph_hints_reload( hints, &face->glyph->outline );
161 for ( dim = 0; dim < AF_DIMENSION_MAX; dim++ )
163 AF_LatinAxis axis = &metrics->axis[dim];
164 AF_AxisHints axhints = &hints->axis[dim];
165 AF_Segment seg, limit, link;
166 FT_UInt num_widths = 0;
169 error = af_latin_hints_compute_segments( hints,
175 * We assume that the glyphs selected for the stem width
176 * computation are `featureless' enough so that the linking
177 * algorithm works fine without adjustments of its scoring
180 af_latin_hints_link_segments( hints,
185 seg = axhints->segments;
186 limit = seg + axhints->num_segments;
188 for ( ; seg < limit; seg++ )
192 /* we only consider stem segments there! */
193 if ( link && link->link == seg && link > seg )
198 dist = seg->pos - link->pos;
202 if ( num_widths < AF_LATIN_MAX_WIDTHS )
203 axis->widths[num_widths++].org = dist;
207 /* this also replaces multiple almost identical stem widths */
208 /* with a single one (the value 100 is heuristic) */
209 af_sort_and_quantize_widths( &num_widths, axis->widths,
210 dummy->units_per_em / 100 );
211 axis->width_count = num_widths;
215 for ( dim = 0; dim < AF_DIMENSION_MAX; dim++ )
217 AF_LatinAxis axis = &metrics->axis[dim];
221 stdw = ( axis->width_count > 0 ) ? axis->widths[0].org
222 : AF_LATIN_CONSTANT( metrics, 50 );
224 /* let's try 20% of the smallest width */
225 axis->edge_distance_threshold = stdw / 5;
226 axis->standard_width = stdw;
227 axis->extra_light = 0;
229 #ifdef FT_DEBUG_LEVEL_TRACE
234 FT_TRACE5(( "%s widths:\n",
235 dim == AF_DIMENSION_VERT ? "horizontal"
238 FT_TRACE5(( " %d (standard)", axis->standard_width ));
239 for ( i = 1; i < axis->width_count; i++ )
240 FT_TRACE5(( " %d", axis->widths[i].org ));
250 af_glyph_hints_done( hints );
254 /* Find all blue zones. Flat segments give the reference points, */
255 /* round segments the overshoot positions. */
258 af_latin_metrics_init_blues( AF_LatinMetrics metrics,
261 FT_Pos flats [AF_BLUE_STRING_MAX_LEN];
262 FT_Pos rounds[AF_BLUE_STRING_MAX_LEN];
269 AF_LatinAxis axis = &metrics->axis[AF_DIMENSION_VERT];
272 AF_StyleClass sc = metrics->root.style_class;
274 AF_Blue_Stringset bss = sc->blue_stringset;
275 const AF_Blue_StringRec* bs = &af_blue_stringsets[bss];
278 /* we walk over the blue character strings as specified in the */
279 /* style's entry in the `af_blue_stringset' array */
281 FT_TRACE5(( "latin blue zones computation\n"
282 "============================\n"
285 for ( ; bs->string != AF_BLUE_STRING_MAX; bs++ )
287 const char* p = &af_blue_strings[bs->string];
292 #ifdef FT_DEBUG_LEVEL_TRACE
294 FT_Bool have_flag = 0;
297 FT_TRACE5(( "blue zone %d", axis->blue_count ));
299 if ( bs->properties )
303 if ( AF_LATIN_IS_TOP_BLUE( bs ) )
305 FT_TRACE5(( "top" ));
309 if ( AF_LATIN_IS_NEUTRAL_BLUE( bs ) )
313 FT_TRACE5(( "neutral" ));
317 if ( AF_LATIN_IS_X_HEIGHT_BLUE( bs ) )
321 FT_TRACE5(( "small top" ));
325 if ( AF_LATIN_IS_LONG_BLUE( bs ) )
329 FT_TRACE5(( "long" ));
335 FT_TRACE5(( ":\n" ));
337 #endif /* FT_DEBUG_LEVEL_TRACE */
345 FT_ULong glyph_index;
347 FT_Pos best_y; /* same as points.y */
348 FT_Int best_point, best_contour_first, best_contour_last;
353 GET_UTF8_CHAR( ch, p );
355 /* load the character in the face -- skip unknown or empty ones */
356 af_get_char_index( &metrics->root, ch, &glyph_index, &y_offset );
357 if ( glyph_index == 0 )
359 FT_TRACE5(( " U+%04lX unavailable\n", ch ));
363 error = FT_Load_Glyph( face, glyph_index, FT_LOAD_NO_SCALE );
364 outline = face->glyph->outline;
365 if ( error || outline.n_points <= 0 )
367 FT_TRACE5(( " U+%04lX contains no outlines\n", ch ));
371 /* now compute min or max point indices and coordinates */
372 points = outline.points;
374 best_y = 0; /* make compiler happy */
375 best_contour_first = 0; /* ditto */
376 best_contour_last = 0; /* ditto */
384 for ( nn = 0; nn < outline.n_contours; first = last + 1, nn++ )
386 FT_Int old_best_point = best_point;
390 last = outline.contours[nn];
392 /* Avoid single-point contours since they are never rasterized. */
393 /* In some fonts, they correspond to mark attachment points */
394 /* that are way outside of the glyph's real outline. */
398 if ( AF_LATIN_IS_TOP_BLUE( bs ) )
400 for ( pp = first; pp <= last; pp++ )
401 if ( best_point < 0 || points[pp].y > best_y )
404 best_y = points[pp].y;
409 for ( pp = first; pp <= last; pp++ )
410 if ( best_point < 0 || points[pp].y < best_y )
413 best_y = points[pp].y;
417 if ( best_point != old_best_point )
419 best_contour_first = first;
420 best_contour_last = last;
425 /* now check whether the point belongs to a straight or round */
426 /* segment; we first need to find in which contour the extremum */
427 /* lies, then inspect its previous and next points */
428 if ( best_point >= 0 )
430 FT_Pos best_x = points[best_point].x;
432 FT_Int best_segment_first, best_segment_last;
433 FT_Int best_on_point_first, best_on_point_last;
437 best_segment_first = best_point;
438 best_segment_last = best_point;
440 if ( FT_CURVE_TAG( outline.tags[best_point] ) == FT_CURVE_TAG_ON )
442 best_on_point_first = best_point;
443 best_on_point_last = best_point;
447 best_on_point_first = -1;
448 best_on_point_last = -1;
451 /* look for the previous and next points on the contour */
452 /* that are not on the same Y coordinate, then threshold */
453 /* the `closeness'... */
459 if ( prev > best_contour_first )
462 prev = best_contour_last;
464 dist = FT_ABS( points[prev].y - best_y );
465 /* accept a small distance or a small angle (both values are */
466 /* heuristic; value 20 corresponds to approx. 2.9 degrees) */
468 if ( FT_ABS( points[prev].x - best_x ) <= 20 * dist )
471 best_segment_first = prev;
473 if ( FT_CURVE_TAG( outline.tags[prev] ) == FT_CURVE_TAG_ON )
475 best_on_point_first = prev;
476 if ( best_on_point_last < 0 )
477 best_on_point_last = prev;
480 } while ( prev != best_point );
484 if ( next < best_contour_last )
487 next = best_contour_first;
489 dist = FT_ABS( points[next].y - best_y );
491 if ( FT_ABS( points[next].x - best_x ) <= 20 * dist )
494 best_segment_last = next;
496 if ( FT_CURVE_TAG( outline.tags[next] ) == FT_CURVE_TAG_ON )
498 best_on_point_last = next;
499 if ( best_on_point_first < 0 )
500 best_on_point_first = next;
503 } while ( next != best_point );
505 if ( AF_LATIN_IS_LONG_BLUE( bs ) )
507 /* If this flag is set, we have an additional constraint to */
508 /* get the blue zone distance: Find a segment of the topmost */
509 /* (or bottommost) contour that is longer than a heuristic */
510 /* threshold. This ensures that small bumps in the outline */
511 /* are ignored (for example, the `vertical serifs' found in */
512 /* many Hebrew glyph designs). */
514 /* If this segment is long enough, we are done. Otherwise, */
515 /* search the segment next to the extremum that is long */
516 /* enough, has the same direction, and a not too large */
517 /* vertical distance from the extremum. Note that the */
518 /* algorithm doesn't check whether the found segment is */
519 /* actually the one (vertically) nearest to the extremum. */
521 /* heuristic threshold value */
522 FT_Pos length_threshold = metrics->units_per_em / 25;
525 dist = FT_ABS( points[best_segment_last].x -
526 points[best_segment_first].x );
528 if ( dist < length_threshold &&
529 best_segment_last - best_segment_first + 2 <=
530 best_contour_last - best_contour_first )
532 /* heuristic threshold value */
533 FT_Pos height_threshold = metrics->units_per_em / 4;
539 /* we intentionally declare these two variables */
540 /* outside of the loop since various compilers emit */
541 /* incorrect warning messages otherwise, talking about */
542 /* `possibly uninitialized variables' */
543 FT_Int p_first = 0; /* make compiler happy */
549 /* compute direction */
554 if ( prev > best_contour_first )
557 prev = best_contour_last;
559 if ( points[prev].x != best_x )
562 } while ( prev != best_point );
564 /* skip glyph for the degenerate case */
565 if ( prev == best_point )
568 left2right = FT_BOOL( points[prev].x < points[best_point].x );
570 first = best_segment_last;
582 /* no hit; adjust first point */
585 /* also adjust first and last on point */
586 if ( FT_CURVE_TAG( outline.tags[first] ) ==
601 if ( last < best_contour_last )
604 last = best_contour_first;
606 if ( FT_ABS( best_y - points[first].y ) > height_threshold )
608 /* vertical distance too large */
613 /* same test as above */
614 dist = FT_ABS( points[last].y - points[first].y );
616 if ( FT_ABS( points[last].x - points[first].x ) <=
623 if ( FT_CURVE_TAG( outline.tags[last] ) == FT_CURVE_TAG_ON )
630 l2r = FT_BOOL( points[first].x < points[last].x );
631 d = FT_ABS( points[last].x - points[first].x );
633 if ( l2r == left2right &&
634 d >= length_threshold )
636 /* all constraints are met; update segment after finding */
640 if ( last < best_contour_last )
643 last = best_contour_first;
645 d = FT_ABS( points[last].y - points[first].y );
647 if ( FT_ABS( points[next].x - points[first].x ) <=
650 if ( last > best_contour_first )
653 last = best_contour_last;
659 if ( FT_CURVE_TAG( outline.tags[last] ) ==
667 } while ( last != best_segment_first );
669 best_y = points[first].y;
671 best_segment_first = first;
672 best_segment_last = last;
674 best_on_point_first = p_first;
675 best_on_point_last = p_last;
680 } while ( last != best_segment_first );
684 /* for computing blue zones, we add the y offset as returned */
685 /* by the currently used OpenType feature -- for example, */
686 /* superscript glyphs might be identical to subscript glyphs */
687 /* with a vertical shift */
690 FT_TRACE5(( " U+%04lX: best_y = %5ld", ch, best_y ));
692 /* now set the `round' flag depending on the segment's kind: */
694 /* - if the horizontal distance between the first and last */
695 /* `on' point is larger than upem/8 (value 8 is heuristic) */
696 /* we have a flat segment */
697 /* - if either the first or the last point of the segment is */
698 /* an `off' point, the segment is round, otherwise it is */
700 if ( best_on_point_first >= 0 &&
701 best_on_point_last >= 0 &&
702 (FT_UInt)( FT_ABS( points[best_on_point_last].x -
703 points[best_on_point_first].x ) ) >
704 metrics->units_per_em / 8 )
708 FT_CURVE_TAG( outline.tags[best_segment_first] ) !=
710 FT_CURVE_TAG( outline.tags[best_segment_last] ) !=
713 if ( round && AF_LATIN_IS_NEUTRAL_BLUE( bs ) )
715 /* only use flat segments for a neutral blue zone */
716 FT_TRACE5(( " (round, skipped)\n" ));
720 FT_TRACE5(( " (%s)\n", round ? "round" : "flat" ));
724 rounds[num_rounds++] = best_y;
726 flats[num_flats++] = best_y;
729 if ( num_flats == 0 && num_rounds == 0 )
732 * we couldn't find a single glyph to compute this blue zone,
733 * we will simply ignore it then
735 FT_TRACE5(( " empty\n" ));
739 /* we have computed the contents of the `rounds' and `flats' tables, */
740 /* now determine the reference and overshoot position of the blue -- */
741 /* we simply take the median value after a simple sort */
742 af_sort_pos( num_rounds, rounds );
743 af_sort_pos( num_flats, flats );
745 blue = &axis->blues[axis->blue_count];
746 blue_ref = &blue->ref.org;
747 blue_shoot = &blue->shoot.org;
751 if ( num_flats == 0 )
754 *blue_shoot = rounds[num_rounds / 2];
756 else if ( num_rounds == 0 )
759 *blue_shoot = flats[num_flats / 2];
763 *blue_ref = flats [num_flats / 2];
764 *blue_shoot = rounds[num_rounds / 2];
767 /* there are sometimes problems: if the overshoot position of top */
768 /* zones is under its reference position, or the opposite for bottom */
769 /* zones. We must thus check everything there and correct the errors */
770 if ( *blue_shoot != *blue_ref )
772 FT_Pos ref = *blue_ref;
773 FT_Pos shoot = *blue_shoot;
774 FT_Bool over_ref = FT_BOOL( shoot > ref );
777 if ( AF_LATIN_IS_TOP_BLUE( bs ) ^ over_ref )
780 *blue_shoot = ( shoot + ref ) / 2;
782 FT_TRACE5(( " [overshoot smaller than reference,"
783 " taking mean value]\n" ));
788 if ( AF_LATIN_IS_TOP_BLUE( bs ) )
789 blue->flags |= AF_LATIN_BLUE_TOP;
790 if ( AF_LATIN_IS_NEUTRAL_BLUE( bs ) )
791 blue->flags |= AF_LATIN_BLUE_NEUTRAL;
794 * The following flag is used later to adjust the y and x scales
795 * in order to optimize the pixel grid alignment of the top of small
798 if ( AF_LATIN_IS_X_HEIGHT_BLUE( bs ) )
799 blue->flags |= AF_LATIN_BLUE_ADJUSTMENT;
801 FT_TRACE5(( " -> reference = %ld\n"
802 " overshoot = %ld\n",
803 *blue_ref, *blue_shoot ));
812 /* Check whether all ASCII digits have the same advance width. */
815 af_latin_metrics_check_digits( AF_LatinMetrics metrics,
819 FT_Bool started = 0, same_width = 1;
820 FT_Fixed advance, old_advance = 0;
823 /* digit `0' is 0x30 in all supported charmaps */
824 for ( i = 0x30; i <= 0x39; i++ )
826 FT_ULong glyph_index;
830 af_get_char_index( &metrics->root, i, &glyph_index, &y_offset );
831 if ( glyph_index == 0 )
834 if ( FT_Get_Advance( face, glyph_index,
837 FT_LOAD_IGNORE_TRANSFORM,
843 if ( advance != old_advance )
851 old_advance = advance;
856 metrics->root.digits_have_same_width = same_width;
860 /* Initialize global metrics. */
862 FT_LOCAL_DEF( FT_Error )
863 af_latin_metrics_init( AF_LatinMetrics metrics,
866 FT_CharMap oldmap = face->charmap;
869 metrics->units_per_em = face->units_per_EM;
871 if ( !FT_Select_Charmap( face, FT_ENCODING_UNICODE ) )
873 af_latin_metrics_init_widths( metrics, face );
874 af_latin_metrics_init_blues( metrics, face );
875 af_latin_metrics_check_digits( metrics, face );
878 FT_Set_Charmap( face, oldmap );
883 /* Adjust scaling value, then scale and shift widths */
884 /* and blue zones (if applicable) for given dimension. */
887 af_latin_metrics_scale_dim( AF_LatinMetrics metrics,
897 if ( dim == AF_DIMENSION_HORZ )
899 scale = scaler->x_scale;
900 delta = scaler->x_delta;
904 scale = scaler->y_scale;
905 delta = scaler->y_delta;
908 axis = &metrics->axis[dim];
910 if ( axis->org_scale == scale && axis->org_delta == delta )
913 axis->org_scale = scale;
914 axis->org_delta = delta;
917 * correct X and Y scale to optimize the alignment of the top of small
918 * letters to the pixel grid
921 AF_LatinAxis Axis = &metrics->axis[AF_DIMENSION_VERT];
922 AF_LatinBlue blue = NULL;
925 for ( nn = 0; nn < Axis->blue_count; nn++ )
927 if ( Axis->blues[nn].flags & AF_LATIN_BLUE_ADJUSTMENT )
929 blue = &Axis->blues[nn];
943 scaled = FT_MulFix( blue->shoot.org, scaler->y_scale );
944 ppem = metrics->root.scaler.face->size->metrics.x_ppem;
945 limit = metrics->root.globals->increase_x_height;
948 /* if the `increase-x-height' property is active, */
949 /* we round up much more often */
952 ppem >= AF_PROP_INCREASE_X_HEIGHT_MIN )
955 fitted = ( scaled + threshold ) & ~63;
957 if ( scaled != fitted )
960 if ( dim == AF_DIMENSION_HORZ )
962 if ( fitted < scaled )
963 scale -= scale / 50; /* scale *= 0.98 */
967 if ( dim == AF_DIMENSION_VERT )
969 scale = FT_MulDiv( scale, fitted, scaled );
972 "af_latin_metrics_scale_dim:"
973 " x height alignment (style `%s'):\n"
975 " vertical scaling changed from %.4f to %.4f (by %d%%)\n"
977 af_style_names[metrics->root.style_class->style],
978 axis->org_scale / 65536.0,
980 ( fitted - scaled ) * 100 / scaled ));
989 if ( dim == AF_DIMENSION_HORZ )
991 metrics->root.scaler.x_scale = scale;
992 metrics->root.scaler.x_delta = delta;
996 metrics->root.scaler.y_scale = scale;
997 metrics->root.scaler.y_delta = delta;
1000 FT_TRACE5(( "%s widths (style `%s')\n",
1001 dim == AF_DIMENSION_HORZ ? "horizontal" : "vertical",
1002 af_style_names[metrics->root.style_class->style] ));
1004 /* scale the widths */
1005 for ( nn = 0; nn < axis->width_count; nn++ )
1007 AF_Width width = axis->widths + nn;
1010 width->cur = FT_MulFix( width->org, scale );
1011 width->fit = width->cur;
1013 FT_TRACE5(( " %d scaled to %.2f\n",
1015 width->cur / 64.0 ));
1018 FT_TRACE5(( "\n" ));
1020 /* an extra-light axis corresponds to a standard width that is */
1021 /* smaller than 5/8 pixels */
1023 (FT_Bool)( FT_MulFix( axis->standard_width, scale ) < 32 + 8 );
1025 #ifdef FT_DEBUG_LEVEL_TRACE
1026 if ( axis->extra_light )
1027 FT_TRACE5(( "`%s' style is extra light (at current resolution)\n"
1029 af_style_names[metrics->root.style_class->style] ));
1032 if ( dim == AF_DIMENSION_VERT )
1034 FT_TRACE5(( "blue zones (style `%s')\n",
1035 af_style_names[metrics->root.style_class->style] ));
1037 /* scale the blue zones */
1038 for ( nn = 0; nn < axis->blue_count; nn++ )
1040 AF_LatinBlue blue = &axis->blues[nn];
1044 blue->ref.cur = FT_MulFix( blue->ref.org, scale ) + delta;
1045 blue->ref.fit = blue->ref.cur;
1046 blue->shoot.cur = FT_MulFix( blue->shoot.org, scale ) + delta;
1047 blue->shoot.fit = blue->shoot.cur;
1048 blue->flags &= ~AF_LATIN_BLUE_ACTIVE;
1050 /* a blue zone is only active if it is less than 3/4 pixels tall */
1051 dist = FT_MulFix( blue->ref.org - blue->shoot.org, scale );
1052 if ( dist <= 48 && dist >= -48 )
1060 /* use discrete values for blue zone widths */
1064 /* generic, original code */
1065 delta1 = blue->shoot.org - blue->ref.org;
1070 delta2 = FT_MulFix( delta2, scale );
1074 else if ( delta2 < 64 )
1075 delta2 = 32 + ( ( ( delta2 - 32 ) + 16 ) & ~31 );
1077 delta2 = FT_PIX_ROUND( delta2 );
1082 blue->ref.fit = FT_PIX_ROUND( blue->ref.cur );
1083 blue->shoot.fit = blue->ref.fit + delta2;
1087 /* simplified version due to abs(dist) <= 48 */
1094 else if ( delta2 < 48 )
1102 blue->ref.fit = FT_PIX_ROUND( blue->ref.cur );
1103 blue->shoot.fit = blue->ref.fit - delta2;
1107 blue->flags |= AF_LATIN_BLUE_ACTIVE;
1109 FT_TRACE5(( " reference %d: %d scaled to %.2f%s\n"
1110 " overshoot %d: %d scaled to %.2f%s\n",
1113 blue->ref.fit / 64.0,
1114 blue->flags & AF_LATIN_BLUE_ACTIVE ? ""
1118 blue->shoot.fit / 64.0,
1119 blue->flags & AF_LATIN_BLUE_ACTIVE ? ""
1127 /* Scale global values in both directions. */
1129 FT_LOCAL_DEF( void )
1130 af_latin_metrics_scale( AF_LatinMetrics metrics,
1133 metrics->root.scaler.render_mode = scaler->render_mode;
1134 metrics->root.scaler.face = scaler->face;
1135 metrics->root.scaler.flags = scaler->flags;
1137 af_latin_metrics_scale_dim( metrics, scaler, AF_DIMENSION_HORZ );
1138 af_latin_metrics_scale_dim( metrics, scaler, AF_DIMENSION_VERT );
1142 /*************************************************************************/
1143 /*************************************************************************/
1145 /***** L A T I N G L Y P H A N A L Y S I S *****/
1147 /*************************************************************************/
1148 /*************************************************************************/
1151 /* Walk over all contours and compute its segments. */
1153 FT_LOCAL_DEF( FT_Error )
1154 af_latin_hints_compute_segments( AF_GlyphHints hints,
1157 AF_AxisHints axis = &hints->axis[dim];
1158 FT_Memory memory = hints->memory;
1159 FT_Error error = FT_Err_Ok;
1160 AF_Segment segment = NULL;
1162 AF_Point* contour = hints->contours;
1163 AF_Point* contour_limit = contour + hints->num_contours;
1164 AF_Direction major_dir, segment_dir;
1169 seg0.flags = AF_EDGE_NORMAL;
1171 major_dir = (AF_Direction)FT_ABS( axis->major_dir );
1172 segment_dir = major_dir;
1174 axis->num_segments = 0;
1176 /* set up (u,v) in each point */
1177 if ( dim == AF_DIMENSION_HORZ )
1179 AF_Point point = hints->points;
1180 AF_Point limit = point + hints->num_points;
1183 for ( ; point < limit; point++ )
1185 point->u = point->fx;
1186 point->v = point->fy;
1191 AF_Point point = hints->points;
1192 AF_Point limit = point + hints->num_points;
1195 for ( ; point < limit; point++ )
1197 point->u = point->fy;
1198 point->v = point->fx;
1202 /* do each contour separately */
1203 for ( ; contour < contour_limit; contour++ )
1205 AF_Point point = contour[0];
1206 AF_Point last = point->prev;
1208 FT_Pos min_pos = 32000; /* minimum segment pos != min_coord */
1209 FT_Pos max_pos = -32000; /* maximum segment pos != max_coord */
1213 if ( point == last ) /* skip singletons -- just in case */
1216 if ( FT_ABS( last->out_dir ) == major_dir &&
1217 FT_ABS( point->out_dir ) == major_dir )
1219 /* we are already on an edge, try to locate its start */
1224 point = point->prev;
1225 if ( FT_ABS( point->out_dir ) != major_dir )
1227 point = point->next;
1230 if ( point == last )
1251 if ( point->out_dir != segment_dir || point == last )
1253 /* we are just leaving an edge; record a new segment! */
1254 segment->last = point;
1255 segment->pos = (FT_Short)( ( min_pos + max_pos ) >> 1 );
1257 /* a segment is round if either its first or last point */
1258 /* is a control point */
1259 if ( ( segment->first->flags | point->flags ) &
1261 segment->flags |= AF_EDGE_ROUND;
1263 /* compute segment size */
1264 min_pos = max_pos = point->v;
1266 v = segment->first->v;
1272 segment->min_coord = (FT_Short)min_pos;
1273 segment->max_coord = (FT_Short)max_pos;
1274 segment->height = (FT_Short)( segment->max_coord -
1275 segment->min_coord );
1283 /* now exit if we are at the start/end point */
1284 if ( point == last )
1291 if ( !on_edge && FT_ABS( point->out_dir ) == major_dir )
1293 /* this is the start of a new segment! */
1294 segment_dir = (AF_Direction)point->out_dir;
1296 error = af_axis_hints_new_segment( axis, memory, &segment );
1300 /* clear all segment fields */
1303 segment->dir = (FT_Char)segment_dir;
1304 min_pos = max_pos = point->u;
1305 segment->first = point;
1306 segment->last = point;
1311 point = point->next;
1317 /* now slightly increase the height of segments if this makes */
1318 /* sense -- this is used to better detect and ignore serifs */
1320 AF_Segment segments = axis->segments;
1321 AF_Segment segments_end = segments + axis->num_segments;
1324 for ( segment = segments; segment < segments_end; segment++ )
1326 AF_Point first = segment->first;
1327 AF_Point last = segment->last;
1328 FT_Pos first_v = first->v;
1329 FT_Pos last_v = last->v;
1332 if ( first_v < last_v )
1338 if ( p->v < first_v )
1339 segment->height = (FT_Short)( segment->height +
1340 ( ( first_v - p->v ) >> 1 ) );
1343 if ( p->v > last_v )
1344 segment->height = (FT_Short)( segment->height +
1345 ( ( p->v - last_v ) >> 1 ) );
1353 if ( p->v > first_v )
1354 segment->height = (FT_Short)( segment->height +
1355 ( ( p->v - first_v ) >> 1 ) );
1358 if ( p->v < last_v )
1359 segment->height = (FT_Short)( segment->height +
1360 ( ( last_v - p->v ) >> 1 ) );
1370 /* Link segments to form stems and serifs. If `width_count' and */
1371 /* `widths' are non-zero, use them to fine-tune the scoring function. */
1373 FT_LOCAL_DEF( void )
1374 af_latin_hints_link_segments( AF_GlyphHints hints,
1375 FT_UInt width_count,
1376 AF_WidthRec* widths,
1379 AF_AxisHints axis = &hints->axis[dim];
1380 AF_Segment segments = axis->segments;
1381 AF_Segment segment_limit = segments + axis->num_segments;
1382 FT_Pos len_threshold, len_score, dist_score, max_width;
1383 AF_Segment seg1, seg2;
1387 max_width = widths[width_count - 1].org;
1391 /* a heuristic value to set up a minimum value for overlapping */
1392 len_threshold = AF_LATIN_CONSTANT( hints->metrics, 8 );
1393 if ( len_threshold == 0 )
1396 /* a heuristic value to weight lengths */
1397 len_score = AF_LATIN_CONSTANT( hints->metrics, 6000 );
1399 /* a heuristic value to weight distances (no call to */
1400 /* AF_LATIN_CONSTANT needed, since we work on multiples */
1401 /* of the stem width) */
1404 /* now compare each segment to the others */
1405 for ( seg1 = segments; seg1 < segment_limit; seg1++ )
1407 if ( seg1->dir != axis->major_dir )
1410 /* search for stems having opposite directions, */
1411 /* with seg1 to the `left' of seg2 */
1412 for ( seg2 = segments; seg2 < segment_limit; seg2++ )
1414 FT_Pos pos1 = seg1->pos;
1415 FT_Pos pos2 = seg2->pos;
1418 if ( seg1->dir + seg2->dir == 0 && pos2 > pos1 )
1420 /* compute distance between the two segments */
1421 FT_Pos min = seg1->min_coord;
1422 FT_Pos max = seg1->max_coord;
1426 if ( min < seg2->min_coord )
1427 min = seg2->min_coord;
1429 if ( max > seg2->max_coord )
1430 max = seg2->max_coord;
1432 /* compute maximum coordinate difference of the two segments */
1433 /* (this is, how much they overlap) */
1435 if ( len >= len_threshold )
1438 * The score is the sum of two demerits indicating the
1439 * `badness' of a fit, measured along the segments' main axis
1440 * and orthogonal to it, respectively.
1442 * o The less overlapping along the main axis, the worse it
1443 * is, causing a larger demerit.
1445 * o The nearer the orthogonal distance to a stem width, the
1446 * better it is, causing a smaller demerit. For simplicity,
1447 * however, we only increase the demerit for values that
1448 * exceed the largest stem width.
1451 FT_Pos dist = pos2 - pos1;
1453 FT_Pos dist_demerit, score;
1458 /* distance demerits are based on multiples of `max_width'; */
1459 /* we scale by 1024 for getting more precision */
1460 FT_Pos delta = ( dist << 10 ) / max_width - ( 1 << 10 );
1463 if ( delta > 10000 )
1464 dist_demerit = 32000;
1465 else if ( delta > 0 )
1466 dist_demerit = delta * delta / dist_score;
1471 dist_demerit = dist; /* default if no widths available */
1473 score = dist_demerit + len_score / len;
1475 /* and we search for the smallest score */
1476 if ( score < seg1->score )
1478 seg1->score = score;
1482 if ( score < seg2->score )
1484 seg2->score = score;
1492 /* now compute the `serif' segments, cf. explanations in `afhints.h' */
1493 for ( seg1 = segments; seg1 < segment_limit; seg1++ )
1499 if ( seg2->link != seg1 )
1502 seg1->serif = seg2->link;
1509 /* Link segments to edges, using feature analysis for selection. */
1511 FT_LOCAL_DEF( FT_Error )
1512 af_latin_hints_compute_edges( AF_GlyphHints hints,
1515 AF_AxisHints axis = &hints->axis[dim];
1516 FT_Error error = FT_Err_Ok;
1517 FT_Memory memory = hints->memory;
1518 AF_LatinAxis laxis = &((AF_LatinMetrics)hints->metrics)->axis[dim];
1520 AF_Segment segments = axis->segments;
1521 AF_Segment segment_limit = segments + axis->num_segments;
1525 AF_Direction up_dir;
1528 FT_Pos edge_distance_threshold;
1529 FT_Pos segment_length_threshold;
1532 axis->num_edges = 0;
1534 scale = ( dim == AF_DIMENSION_HORZ ) ? hints->x_scale
1538 up_dir = ( dim == AF_DIMENSION_HORZ ) ? AF_DIR_UP
1543 * We ignore all segments that are less than 1 pixel in length
1544 * to avoid many problems with serif fonts. We compute the
1545 * corresponding threshold in font units.
1547 if ( dim == AF_DIMENSION_HORZ )
1548 segment_length_threshold = FT_DivFix( 64, hints->y_scale );
1550 segment_length_threshold = 0;
1552 /*********************************************************************/
1554 /* We begin by generating a sorted table of edges for the current */
1555 /* direction. To do so, we simply scan each segment and try to find */
1556 /* an edge in our table that corresponds to its position. */
1558 /* If no edge is found, we create and insert a new edge in the */
1559 /* sorted table. Otherwise, we simply add the segment to the edge's */
1560 /* list which gets processed in the second step to compute the */
1561 /* edge's properties. */
1563 /* Note that the table of edges is sorted along the segment/edge */
1566 /*********************************************************************/
1568 /* assure that edge distance threshold is at most 0.25px */
1569 edge_distance_threshold = FT_MulFix( laxis->edge_distance_threshold,
1571 if ( edge_distance_threshold > 64 / 4 )
1572 edge_distance_threshold = 64 / 4;
1574 edge_distance_threshold = FT_DivFix( edge_distance_threshold,
1577 for ( seg = segments; seg < segment_limit; seg++ )
1579 AF_Edge found = NULL;
1583 if ( seg->height < segment_length_threshold )
1586 /* A special case for serif edges: If they are smaller than */
1587 /* 1.5 pixels we ignore them. */
1589 2 * seg->height < 3 * segment_length_threshold )
1592 /* look for an edge corresponding to the segment */
1593 for ( ee = 0; ee < axis->num_edges; ee++ )
1595 AF_Edge edge = axis->edges + ee;
1599 dist = seg->pos - edge->fpos;
1603 if ( dist < edge_distance_threshold && edge->dir == seg->dir )
1615 /* insert a new edge in the list and */
1616 /* sort according to the position */
1617 error = af_axis_hints_new_edge( axis, seg->pos,
1618 (AF_Direction)seg->dir,
1623 /* add the segment to the new edge's list */
1628 edge->dir = seg->dir;
1629 edge->fpos = seg->pos;
1630 edge->opos = FT_MulFix( seg->pos, scale );
1631 edge->pos = edge->opos;
1632 seg->edge_next = seg;
1636 /* if an edge was found, simply add the segment to the edge's */
1638 seg->edge_next = found->first;
1639 found->last->edge_next = seg;
1645 /******************************************************************/
1647 /* Good, we now compute each edge's properties according to the */
1648 /* segments found on its position. Basically, these are */
1650 /* - the edge's main direction */
1651 /* - stem edge, serif edge or both (which defaults to stem then) */
1652 /* - rounded edge, straight or both (which defaults to straight) */
1653 /* - link for edge */
1655 /******************************************************************/
1657 /* first of all, set the `edge' field in each segment -- this is */
1658 /* required in order to compute edge links */
1661 * Note that removing this loop and setting the `edge' field of each
1662 * segment directly in the code above slows down execution speed for
1663 * some reasons on platforms like the Sun.
1666 AF_Edge edges = axis->edges;
1667 AF_Edge edge_limit = edges + axis->num_edges;
1671 for ( edge = edges; edge < edge_limit; edge++ )
1678 seg = seg->edge_next;
1680 } while ( seg != edge->first );
1683 /* now compute each edge properties */
1684 for ( edge = edges; edge < edge_limit; edge++ )
1686 FT_Int is_round = 0; /* does it contain round segments? */
1687 FT_Int is_straight = 0; /* does it contain straight segments? */
1689 FT_Pos ups = 0; /* number of upwards segments */
1690 FT_Pos downs = 0; /* number of downwards segments */
1701 /* check for roundness of segment */
1702 if ( seg->flags & AF_EDGE_ROUND )
1708 /* check for segment direction */
1709 if ( seg->dir == up_dir )
1710 ups += seg->max_coord - seg->min_coord;
1712 downs += seg->max_coord - seg->min_coord;
1715 /* check for links -- if seg->serif is set, then seg->link must */
1717 is_serif = (FT_Bool)( seg->serif &&
1719 seg->serif->edge != edge );
1721 if ( ( seg->link && seg->link->edge != NULL ) || is_serif )
1733 edge2 = edge->serif;
1742 edge_delta = edge->fpos - edge2->fpos;
1743 if ( edge_delta < 0 )
1744 edge_delta = -edge_delta;
1746 seg_delta = seg->pos - seg2->pos;
1747 if ( seg_delta < 0 )
1748 seg_delta = -seg_delta;
1750 if ( seg_delta < edge_delta )
1758 edge->serif = edge2;
1759 edge2->flags |= AF_EDGE_SERIF;
1765 seg = seg->edge_next;
1767 } while ( seg != edge->first );
1769 /* set the round/straight flags */
1770 edge->flags = AF_EDGE_NORMAL;
1772 if ( is_round > 0 && is_round >= is_straight )
1773 edge->flags |= AF_EDGE_ROUND;
1776 /* set the edge's main direction */
1777 edge->dir = AF_DIR_NONE;
1780 edge->dir = (FT_Char)up_dir;
1782 else if ( ups < downs )
1783 edge->dir = (FT_Char)-up_dir;
1785 else if ( ups == downs )
1786 edge->dir = 0; /* both up and down! */
1789 /* get rid of serifs if link is set */
1790 /* XXX: This gets rid of many unpleasant artefacts! */
1791 /* Example: the `c' in cour.pfa at size 13 */
1793 if ( edge->serif && edge->link )
1803 /* Detect segments and edges for given dimension. */
1805 FT_LOCAL_DEF( FT_Error )
1806 af_latin_hints_detect_features( AF_GlyphHints hints,
1807 FT_UInt width_count,
1808 AF_WidthRec* widths,
1814 error = af_latin_hints_compute_segments( hints, dim );
1817 af_latin_hints_link_segments( hints, width_count, widths, dim );
1819 error = af_latin_hints_compute_edges( hints, dim );
1826 /* Compute all edges which lie within blue zones. */
1828 FT_LOCAL_DEF( void )
1829 af_latin_hints_compute_blue_edges( AF_GlyphHints hints,
1830 AF_LatinMetrics metrics )
1832 AF_AxisHints axis = &hints->axis[AF_DIMENSION_VERT];
1833 AF_Edge edge = axis->edges;
1834 AF_Edge edge_limit = edge + axis->num_edges;
1835 AF_LatinAxis latin = &metrics->axis[AF_DIMENSION_VERT];
1836 FT_Fixed scale = latin->scale;
1839 /* compute which blue zones are active, i.e. have their scaled */
1840 /* size < 3/4 pixels */
1842 /* for each horizontal edge search the blue zone which is closest */
1843 for ( ; edge < edge_limit; edge++ )
1846 AF_Width best_blue = NULL;
1847 FT_Bool best_blue_is_neutral = 0;
1848 FT_Pos best_dist; /* initial threshold */
1851 /* compute the initial threshold as a fraction of the EM size */
1852 /* (the value 40 is heuristic) */
1853 best_dist = FT_MulFix( metrics->units_per_em / 40, scale );
1855 /* assure a minimum distance of 0.5px */
1856 if ( best_dist > 64 / 2 )
1859 for ( bb = 0; bb < latin->blue_count; bb++ )
1861 AF_LatinBlue blue = latin->blues + bb;
1862 FT_Bool is_top_blue, is_neutral_blue, is_major_dir;
1865 /* skip inactive blue zones (i.e., those that are too large) */
1866 if ( !( blue->flags & AF_LATIN_BLUE_ACTIVE ) )
1869 /* if it is a top zone, check for right edges (against the major */
1870 /* direction); if it is a bottom zone, check for left edges (in */
1871 /* the major direction) -- this assumes the TrueType convention */
1872 /* for the orientation of contours */
1874 (FT_Byte)( ( blue->flags & AF_LATIN_BLUE_TOP ) != 0 );
1876 (FT_Byte)( ( blue->flags & AF_LATIN_BLUE_NEUTRAL ) != 0);
1878 FT_BOOL( edge->dir == axis->major_dir );
1880 /* neutral blue zones are handled for both directions */
1881 if ( is_top_blue ^ is_major_dir || is_neutral_blue )
1886 /* first of all, compare it to the reference position */
1887 dist = edge->fpos - blue->ref.org;
1891 dist = FT_MulFix( dist, scale );
1892 if ( dist < best_dist )
1895 best_blue = &blue->ref;
1896 best_blue_is_neutral = is_neutral_blue;
1899 /* now compare it to the overshoot position and check whether */
1900 /* the edge is rounded, and whether the edge is over the */
1901 /* reference position of a top zone, or under the reference */
1902 /* position of a bottom zone (provided we don't have a */
1903 /* neutral blue zone) */
1904 if ( edge->flags & AF_EDGE_ROUND &&
1908 FT_Bool is_under_ref = FT_BOOL( edge->fpos < blue->ref.org );
1911 if ( is_top_blue ^ is_under_ref )
1913 dist = edge->fpos - blue->shoot.org;
1917 dist = FT_MulFix( dist, scale );
1918 if ( dist < best_dist )
1921 best_blue = &blue->shoot;
1922 best_blue_is_neutral = is_neutral_blue;
1931 edge->blue_edge = best_blue;
1932 if ( best_blue_is_neutral )
1933 edge->flags |= AF_EDGE_NEUTRAL;
1939 /* Initalize hinting engine. */
1942 af_latin_hints_init( AF_GlyphHints hints,
1943 AF_LatinMetrics metrics )
1945 FT_Render_Mode mode;
1946 FT_UInt32 scaler_flags, other_flags;
1947 FT_Face face = metrics->root.scaler.face;
1950 af_glyph_hints_rescale( hints, (AF_StyleMetrics)metrics );
1953 * correct x_scale and y_scale if needed, since they may have
1954 * been modified by `af_latin_metrics_scale_dim' above
1956 hints->x_scale = metrics->axis[AF_DIMENSION_HORZ].scale;
1957 hints->x_delta = metrics->axis[AF_DIMENSION_HORZ].delta;
1958 hints->y_scale = metrics->axis[AF_DIMENSION_VERT].scale;
1959 hints->y_delta = metrics->axis[AF_DIMENSION_VERT].delta;
1961 /* compute flags depending on render mode, etc. */
1962 mode = metrics->root.scaler.render_mode;
1964 #if 0 /* #ifdef AF_CONFIG_OPTION_USE_WARPER */
1965 if ( mode == FT_RENDER_MODE_LCD || mode == FT_RENDER_MODE_LCD_V )
1966 metrics->root.scaler.render_mode = mode = FT_RENDER_MODE_NORMAL;
1969 scaler_flags = hints->scaler_flags;
1973 * We snap the width of vertical stems for the monochrome and
1974 * horizontal LCD rendering targets only.
1976 if ( mode == FT_RENDER_MODE_MONO || mode == FT_RENDER_MODE_LCD )
1977 other_flags |= AF_LATIN_HINTS_HORZ_SNAP;
1980 * We snap the width of horizontal stems for the monochrome and
1981 * vertical LCD rendering targets only.
1983 if ( mode == FT_RENDER_MODE_MONO || mode == FT_RENDER_MODE_LCD_V )
1984 other_flags |= AF_LATIN_HINTS_VERT_SNAP;
1987 * We adjust stems to full pixels only if we don't use the `light' mode.
1989 if ( mode != FT_RENDER_MODE_LIGHT )
1990 other_flags |= AF_LATIN_HINTS_STEM_ADJUST;
1992 if ( mode == FT_RENDER_MODE_MONO )
1993 other_flags |= AF_LATIN_HINTS_MONO;
1996 * In `light' hinting mode we disable horizontal hinting completely.
1997 * We also do it if the face is italic.
1999 if ( mode == FT_RENDER_MODE_LIGHT ||
2000 ( face->style_flags & FT_STYLE_FLAG_ITALIC ) != 0 )
2001 scaler_flags |= AF_SCALER_FLAG_NO_HORIZONTAL;
2003 hints->scaler_flags = scaler_flags;
2004 hints->other_flags = other_flags;
2010 /*************************************************************************/
2011 /*************************************************************************/
2013 /***** L A T I N G L Y P H G R I D - F I T T I N G *****/
2015 /*************************************************************************/
2016 /*************************************************************************/
2018 /* Snap a given width in scaled coordinates to one of the */
2019 /* current standard widths. */
2022 af_latin_snap_width( AF_Width widths,
2027 FT_Pos best = 64 + 32 + 2;
2028 FT_Pos reference = width;
2032 for ( n = 0; n < count; n++ )
2049 scaled = FT_PIX_ROUND( reference );
2051 if ( width >= reference )
2053 if ( width < scaled + 48 )
2058 if ( width > scaled - 48 )
2066 /* Compute the snapped width of a given stem, ignoring very thin ones. */
2067 /* There is a lot of voodoo in this function; changing the hard-coded */
2068 /* parameters influence the whole hinting process. */
2071 af_latin_compute_stem_width( AF_GlyphHints hints,
2074 AF_Edge_Flags base_flags,
2075 AF_Edge_Flags stem_flags )
2077 AF_LatinMetrics metrics = (AF_LatinMetrics)hints->metrics;
2078 AF_LatinAxis axis = &metrics->axis[dim];
2079 FT_Pos dist = width;
2081 FT_Int vertical = ( dim == AF_DIMENSION_VERT );
2084 if ( !AF_LATIN_HINTS_DO_STEM_ADJUST( hints ) ||
2094 if ( ( vertical && !AF_LATIN_HINTS_DO_VERT_SNAP( hints ) ) ||
2095 ( !vertical && !AF_LATIN_HINTS_DO_HORZ_SNAP( hints ) ) )
2097 /* smooth hinting process: very lightly quantize the stem width */
2099 /* leave the widths of serifs alone */
2100 if ( ( stem_flags & AF_EDGE_SERIF ) &&
2105 else if ( base_flags & AF_EDGE_ROUND )
2110 else if ( dist < 56 )
2113 if ( axis->width_count > 0 )
2118 /* compare to standard width */
2119 delta = dist - axis->widths[0].cur;
2126 dist = axis->widths[0].cur;
2133 if ( dist < 3 * 64 )
2141 else if ( delta < 32 )
2144 else if ( delta < 54 )
2151 dist = ( dist + 32 ) & ~63;
2156 /* strong hinting process: snap the stem width to integer pixels */
2158 FT_Pos org_dist = dist;
2161 dist = af_latin_snap_width( axis->widths, axis->width_count, dist );
2165 /* in the case of vertical hinting, always round */
2166 /* the stem heights to integer pixels */
2169 dist = ( dist + 16 ) & ~63;
2175 if ( AF_LATIN_HINTS_DO_MONO( hints ) )
2177 /* monochrome horizontal hinting: snap widths to integer pixels */
2178 /* with a different threshold */
2183 dist = ( dist + 32 ) & ~63;
2187 /* for horizontal anti-aliased hinting, we adopt a more subtle */
2188 /* approach: we strengthen small stems, round stems whose size */
2189 /* is between 1 and 2 pixels to an integer, otherwise nothing */
2192 dist = ( dist + 64 ) >> 1;
2194 else if ( dist < 128 )
2196 /* We only round to an integer width if the corresponding */
2197 /* distortion is less than 1/4 pixel. Otherwise this */
2198 /* makes everything worse since the diagonals, which are */
2199 /* not hinted, appear a lot bolder or thinner than the */
2200 /* vertical stems. */
2205 dist = ( dist + 22 ) & ~63;
2206 delta = dist - org_dist;
2214 dist = ( dist + 64 ) >> 1;
2218 /* round otherwise to prevent color fringes in LCD mode */
2219 dist = ( dist + 32 ) & ~63;
2232 /* Align one stem edge relative to the previous stem edge. */
2235 af_latin_align_linked_edge( AF_GlyphHints hints,
2240 FT_Pos dist = stem_edge->opos - base_edge->opos;
2242 FT_Pos fitted_width = af_latin_compute_stem_width(
2244 (AF_Edge_Flags)base_edge->flags,
2245 (AF_Edge_Flags)stem_edge->flags );
2248 stem_edge->pos = base_edge->pos + fitted_width;
2250 FT_TRACE5(( " LINK: edge %d (opos=%.2f) linked to %.2f,"
2251 " dist was %.2f, now %.2f\n",
2252 stem_edge - hints->axis[dim].edges, stem_edge->opos / 64.0,
2253 stem_edge->pos / 64.0, dist / 64.0, fitted_width / 64.0 ));
2257 /* Shift the coordinates of the `serif' edge by the same amount */
2258 /* as the corresponding `base' edge has been moved already. */
2261 af_latin_align_serif_edge( AF_GlyphHints hints,
2267 serif->pos = base->pos + ( serif->opos - base->opos );
2271 /*************************************************************************/
2272 /*************************************************************************/
2273 /*************************************************************************/
2275 /**** E D G E H I N T I N G ****/
2277 /*************************************************************************/
2278 /*************************************************************************/
2279 /*************************************************************************/
2282 /* The main grid-fitting routine. */
2284 FT_LOCAL_DEF( void )
2285 af_latin_hint_edges( AF_GlyphHints hints,
2288 AF_AxisHints axis = &hints->axis[dim];
2289 AF_Edge edges = axis->edges;
2290 AF_Edge edge_limit = edges + axis->num_edges;
2293 AF_Edge anchor = NULL;
2294 FT_Int has_serifs = 0;
2296 #ifdef FT_DEBUG_LEVEL_TRACE
2297 FT_UInt num_actions = 0;
2301 FT_TRACE5(( "latin %s edge hinting (style `%s')\n",
2302 dim == AF_DIMENSION_VERT ? "horizontal" : "vertical",
2303 af_style_names[hints->metrics->style_class->style] ));
2305 /* we begin by aligning all stems relative to the blue zone */
2306 /* if needed -- that's only for horizontal edges */
2308 if ( dim == AF_DIMENSION_VERT && AF_HINTS_DO_BLUES( hints ) )
2310 for ( edge = edges; edge < edge_limit; edge++ )
2313 AF_Edge edge1, edge2; /* these edges form the stem to check */
2316 if ( edge->flags & AF_EDGE_DONE )
2323 * If a stem contains both a neutral and a non-neutral blue zone,
2324 * skip the neutral one. Otherwise, outlines with different
2325 * directions might be incorrectly aligned at the same vertical
2328 * If we have two neutral blue zones, skip one of them.
2331 if ( edge->blue_edge && edge2 && edge2->blue_edge )
2333 FT_Byte neutral = edge->flags & AF_EDGE_NEUTRAL;
2334 FT_Byte neutral2 = edge2->flags & AF_EDGE_NEUTRAL;
2337 if ( ( neutral && neutral2 ) || neutral2 )
2339 edge2->blue_edge = NULL;
2340 edge2->flags &= ~AF_EDGE_NEUTRAL;
2344 edge->blue_edge = NULL;
2345 edge->flags &= ~AF_EDGE_NEUTRAL;
2349 blue = edge->blue_edge;
2353 /* flip edges if the other edge is aligned to a blue zone */
2354 else if ( edge2 && edge2->blue_edge )
2356 blue = edge2->blue_edge;
2364 #ifdef FT_DEBUG_LEVEL_TRACE
2366 FT_TRACE5(( " BLUE_ANCHOR: edge %d (opos=%.2f) snapped to %.2f,"
2367 " was %.2f (anchor=edge %d)\n",
2368 edge1 - edges, edge1->opos / 64.0, blue->fit / 64.0,
2369 edge1->pos / 64.0, edge - edges ));
2371 FT_TRACE5(( " BLUE: edge %d (opos=%.2f) snapped to %.2f,"
2373 edge1 - edges, edge1->opos / 64.0, blue->fit / 64.0,
2374 edge1->pos / 64.0 ));
2379 edge1->pos = blue->fit;
2380 edge1->flags |= AF_EDGE_DONE;
2382 if ( edge2 && !edge2->blue_edge )
2384 af_latin_align_linked_edge( hints, dim, edge1, edge2 );
2385 edge2->flags |= AF_EDGE_DONE;
2387 #ifdef FT_DEBUG_LEVEL_TRACE
2397 /* now we align all other stem edges, trying to maintain the */
2398 /* relative order of stems in the glyph */
2399 for ( edge = edges; edge < edge_limit; edge++ )
2404 if ( edge->flags & AF_EDGE_DONE )
2407 /* skip all non-stem edges */
2415 /* now align the stem */
2417 /* this should not happen, but it's better to be safe */
2418 if ( edge2->blue_edge )
2420 FT_TRACE5(( " ASSERTION FAILED for edge %d\n", edge2 - edges ));
2422 af_latin_align_linked_edge( hints, dim, edge2, edge );
2423 edge->flags |= AF_EDGE_DONE;
2425 #ifdef FT_DEBUG_LEVEL_TRACE
2433 /* if we reach this if clause, no stem has been aligned yet */
2435 FT_Pos org_len, org_center, cur_len;
2436 FT_Pos cur_pos1, error1, error2, u_off, d_off;
2439 org_len = edge2->opos - edge->opos;
2440 cur_len = af_latin_compute_stem_width(
2441 hints, dim, org_len,
2442 (AF_Edge_Flags)edge->flags,
2443 (AF_Edge_Flags)edge2->flags );
2445 /* some voodoo to specially round edges for small stem widths; */
2446 /* the idea is to align the center of a stem, then shifting */
2447 /* the stem edges to suitable positions */
2448 if ( cur_len <= 64 )
2456 /* 1px < width < 1.5px */
2463 org_center = edge->opos + ( org_len >> 1 );
2464 cur_pos1 = FT_PIX_ROUND( org_center );
2466 error1 = org_center - ( cur_pos1 - u_off );
2470 error2 = org_center - ( cur_pos1 + d_off );
2474 if ( error1 < error2 )
2479 edge->pos = cur_pos1 - cur_len / 2;
2480 edge2->pos = edge->pos + cur_len;
2483 edge->pos = FT_PIX_ROUND( edge->opos );
2486 edge->flags |= AF_EDGE_DONE;
2488 FT_TRACE5(( " ANCHOR: edge %d (opos=%.2f) and %d (opos=%.2f)"
2489 " snapped to %.2f and %.2f\n",
2490 edge - edges, edge->opos / 64.0,
2491 edge2 - edges, edge2->opos / 64.0,
2492 edge->pos / 64.0, edge2->pos / 64.0 ));
2494 af_latin_align_linked_edge( hints, dim, edge, edge2 );
2496 #ifdef FT_DEBUG_LEVEL_TRACE
2502 FT_Pos org_pos, org_len, org_center, cur_len;
2503 FT_Pos cur_pos1, cur_pos2, delta1, delta2;
2506 org_pos = anchor->pos + ( edge->opos - anchor->opos );
2507 org_len = edge2->opos - edge->opos;
2508 org_center = org_pos + ( org_len >> 1 );
2510 cur_len = af_latin_compute_stem_width(
2511 hints, dim, org_len,
2512 (AF_Edge_Flags)edge->flags,
2513 (AF_Edge_Flags)edge2->flags );
2515 if ( edge2->flags & AF_EDGE_DONE )
2517 FT_TRACE5(( " ADJUST: edge %d (pos=%.2f) moved to %.2f\n",
2518 edge - edges, edge->pos / 64.0,
2519 ( edge2->pos - cur_len ) / 64.0 ));
2521 edge->pos = edge2->pos - cur_len;
2524 else if ( cur_len < 96 )
2526 FT_Pos u_off, d_off;
2529 cur_pos1 = FT_PIX_ROUND( org_center );
2531 if ( cur_len <= 64 )
2542 delta1 = org_center - ( cur_pos1 - u_off );
2546 delta2 = org_center - ( cur_pos1 + d_off );
2550 if ( delta1 < delta2 )
2555 edge->pos = cur_pos1 - cur_len / 2;
2556 edge2->pos = cur_pos1 + cur_len / 2;
2558 FT_TRACE5(( " STEM: edge %d (opos=%.2f) linked to %d (opos=%.2f)"
2559 " snapped to %.2f and %.2f\n",
2560 edge - edges, edge->opos / 64.0,
2561 edge2 - edges, edge2->opos / 64.0,
2562 edge->pos / 64.0, edge2->pos / 64.0 ));
2567 org_pos = anchor->pos + ( edge->opos - anchor->opos );
2568 org_len = edge2->opos - edge->opos;
2569 org_center = org_pos + ( org_len >> 1 );
2571 cur_len = af_latin_compute_stem_width(
2572 hints, dim, org_len,
2573 (AF_Edge_Flags)edge->flags,
2574 (AF_Edge_Flags)edge2->flags );
2576 cur_pos1 = FT_PIX_ROUND( org_pos );
2577 delta1 = cur_pos1 + ( cur_len >> 1 ) - org_center;
2581 cur_pos2 = FT_PIX_ROUND( org_pos + org_len ) - cur_len;
2582 delta2 = cur_pos2 + ( cur_len >> 1 ) - org_center;
2586 edge->pos = ( delta1 < delta2 ) ? cur_pos1 : cur_pos2;
2587 edge2->pos = edge->pos + cur_len;
2589 FT_TRACE5(( " STEM: edge %d (opos=%.2f) linked to %d (opos=%.2f)"
2590 " snapped to %.2f and %.2f\n",
2591 edge - edges, edge->opos / 64.0,
2592 edge2 - edges, edge2->opos / 64.0,
2593 edge->pos / 64.0, edge2->pos / 64.0 ));
2596 #ifdef FT_DEBUG_LEVEL_TRACE
2600 edge->flags |= AF_EDGE_DONE;
2601 edge2->flags |= AF_EDGE_DONE;
2603 if ( edge > edges && edge->pos < edge[-1].pos )
2605 #ifdef FT_DEBUG_LEVEL_TRACE
2606 FT_TRACE5(( " BOUND: edge %d (pos=%.2f) moved to %.2f\n",
2607 edge - edges, edge->pos / 64.0, edge[-1].pos / 64.0 ));
2612 edge->pos = edge[-1].pos;
2617 /* make sure that lowercase m's maintain their symmetry */
2619 /* In general, lowercase m's have six vertical edges if they are sans */
2620 /* serif, or twelve if they are with serifs. This implementation is */
2621 /* based on that assumption, and seems to work very well with most */
2622 /* faces. However, if for a certain face this assumption is not */
2623 /* true, the m is just rendered like before. In addition, any stem */
2624 /* correction will only be applied to symmetrical glyphs (even if the */
2625 /* glyph is not an m), so the potential for unwanted distortion is */
2626 /* relatively low. */
2628 /* We don't handle horizontal edges since we can't easily assure that */
2629 /* the third (lowest) stem aligns with the base line; it might end up */
2630 /* one pixel higher or lower. */
2632 n_edges = edge_limit - edges;
2633 if ( dim == AF_DIMENSION_HORZ && ( n_edges == 6 || n_edges == 12 ) )
2635 AF_Edge edge1, edge2, edge3;
2636 FT_Pos dist1, dist2, span, delta;
2652 dist1 = edge2->opos - edge1->opos;
2653 dist2 = edge3->opos - edge2->opos;
2655 span = dist1 - dist2;
2661 delta = edge3->pos - ( 2 * edge2->pos - edge1->pos );
2662 edge3->pos -= delta;
2664 edge3->link->pos -= delta;
2666 /* move the serifs along with the stem */
2667 if ( n_edges == 12 )
2669 ( edges + 8 )->pos -= delta;
2670 ( edges + 11 )->pos -= delta;
2673 edge3->flags |= AF_EDGE_DONE;
2675 edge3->link->flags |= AF_EDGE_DONE;
2679 if ( has_serifs || !anchor )
2682 * now hint the remaining edges (serifs and single) in order
2683 * to complete our processing
2685 for ( edge = edges; edge < edge_limit; edge++ )
2690 if ( edge->flags & AF_EDGE_DONE )
2697 delta = edge->serif->opos - edge->opos;
2702 if ( delta < 64 + 16 )
2704 af_latin_align_serif_edge( hints, edge->serif, edge );
2705 FT_TRACE5(( " SERIF: edge %d (opos=%.2f) serif to %d (opos=%.2f)"
2706 " aligned to %.2f\n",
2707 edge - edges, edge->opos / 64.0,
2708 edge->serif - edges, edge->serif->opos / 64.0,
2709 edge->pos / 64.0 ));
2713 edge->pos = FT_PIX_ROUND( edge->opos );
2715 FT_TRACE5(( " SERIF_ANCHOR: edge %d (opos=%.2f)"
2716 " snapped to %.2f\n",
2717 edge-edges, edge->opos / 64.0, edge->pos / 64.0 ));
2721 AF_Edge before, after;
2724 for ( before = edge - 1; before >= edges; before-- )
2725 if ( before->flags & AF_EDGE_DONE )
2728 for ( after = edge + 1; after < edge_limit; after++ )
2729 if ( after->flags & AF_EDGE_DONE )
2732 if ( before >= edges && before < edge &&
2733 after < edge_limit && after > edge )
2735 if ( after->opos == before->opos )
2736 edge->pos = before->pos;
2738 edge->pos = before->pos +
2739 FT_MulDiv( edge->opos - before->opos,
2740 after->pos - before->pos,
2741 after->opos - before->opos );
2743 FT_TRACE5(( " SERIF_LINK1: edge %d (opos=%.2f) snapped to %.2f"
2744 " from %d (opos=%.2f)\n",
2745 edge - edges, edge->opos / 64.0,
2747 before - edges, before->opos / 64.0 ));
2751 edge->pos = anchor->pos +
2752 ( ( edge->opos - anchor->opos + 16 ) & ~31 );
2753 FT_TRACE5(( " SERIF_LINK2: edge %d (opos=%.2f)"
2754 " snapped to %.2f\n",
2755 edge - edges, edge->opos / 64.0, edge->pos / 64.0 ));
2759 #ifdef FT_DEBUG_LEVEL_TRACE
2762 edge->flags |= AF_EDGE_DONE;
2764 if ( edge > edges && edge->pos < edge[-1].pos )
2766 #ifdef FT_DEBUG_LEVEL_TRACE
2767 FT_TRACE5(( " BOUND: edge %d (pos=%.2f) moved to %.2f\n",
2768 edge - edges, edge->pos / 64.0, edge[-1].pos / 64.0 ));
2772 edge->pos = edge[-1].pos;
2775 if ( edge + 1 < edge_limit &&
2776 edge[1].flags & AF_EDGE_DONE &&
2777 edge->pos > edge[1].pos )
2779 #ifdef FT_DEBUG_LEVEL_TRACE
2780 FT_TRACE5(( " BOUND: edge %d (pos=%.2f) moved to %.2f\n",
2781 edge - edges, edge->pos / 64.0, edge[1].pos / 64.0 ));
2786 edge->pos = edge[1].pos;
2791 #ifdef FT_DEBUG_LEVEL_TRACE
2793 FT_TRACE5(( " (none)\n" ));
2794 FT_TRACE5(( "\n" ));
2799 /* Apply the complete hinting algorithm to a latin glyph. */
2802 af_latin_hints_apply( AF_GlyphHints hints,
2803 FT_Outline* outline,
2804 AF_LatinMetrics metrics )
2812 error = af_glyph_hints_reload( hints, outline );
2816 /* analyze glyph outline */
2817 #ifdef AF_CONFIG_OPTION_USE_WARPER
2818 if ( metrics->root.scaler.render_mode == FT_RENDER_MODE_LIGHT ||
2819 AF_HINTS_DO_HORIZONTAL( hints ) )
2821 if ( AF_HINTS_DO_HORIZONTAL( hints ) )
2824 axis = &metrics->axis[AF_DIMENSION_HORZ];
2825 error = af_latin_hints_detect_features( hints,
2828 AF_DIMENSION_HORZ );
2833 if ( AF_HINTS_DO_VERTICAL( hints ) )
2835 axis = &metrics->axis[AF_DIMENSION_VERT];
2836 error = af_latin_hints_detect_features( hints,
2839 AF_DIMENSION_VERT );
2843 af_latin_hints_compute_blue_edges( hints, metrics );
2846 /* grid-fit the outline */
2847 for ( dim = 0; dim < AF_DIMENSION_MAX; dim++ )
2849 #ifdef AF_CONFIG_OPTION_USE_WARPER
2850 if ( dim == AF_DIMENSION_HORZ &&
2851 metrics->root.scaler.render_mode == FT_RENDER_MODE_LIGHT )
2853 AF_WarperRec warper;
2858 af_warper_compute( &warper, hints, (AF_Dimension)dim,
2860 af_glyph_hints_scale_dim( hints, (AF_Dimension)dim,
2866 if ( ( dim == AF_DIMENSION_HORZ && AF_HINTS_DO_HORIZONTAL( hints ) ) ||
2867 ( dim == AF_DIMENSION_VERT && AF_HINTS_DO_VERTICAL( hints ) ) )
2869 af_latin_hint_edges( hints, (AF_Dimension)dim );
2870 af_glyph_hints_align_edge_points( hints, (AF_Dimension)dim );
2871 af_glyph_hints_align_strong_points( hints, (AF_Dimension)dim );
2872 af_glyph_hints_align_weak_points( hints, (AF_Dimension)dim );
2876 af_glyph_hints_save( hints, outline );
2883 /*************************************************************************/
2884 /*************************************************************************/
2886 /***** L A T I N S C R I P T C L A S S *****/
2888 /*************************************************************************/
2889 /*************************************************************************/
2892 AF_DEFINE_WRITING_SYSTEM_CLASS(
2893 af_latin_writing_system_class,
2895 AF_WRITING_SYSTEM_LATIN,
2897 sizeof ( AF_LatinMetricsRec ),
2899 (AF_WritingSystem_InitMetricsFunc) af_latin_metrics_init,
2900 (AF_WritingSystem_ScaleMetricsFunc)af_latin_metrics_scale,
2901 (AF_WritingSystem_DoneMetricsFunc) NULL,
2903 (AF_WritingSystem_InitHintsFunc) af_latin_hints_init,
2904 (AF_WritingSystem_ApplyHintsFunc) af_latin_hints_apply