1 /* ATTENTION: This file doesn't compile. It is only here as a reference */
2 /* of an alternative latin hinting algorithm that was always */
3 /* marked as experimental. */
6 /****************************************************************************
10 * Auto-fitter hinting routines for latin writing system (body).
12 * Copyright (C) 2003-2020 by
13 * David Turner, Robert Wilhelm, and Werner Lemberg.
15 * This file is part of the FreeType project, and may only be used,
16 * modified, and distributed under the terms of the FreeType project
17 * license, LICENSE.TXT. By continuing to use, modify, or distribute
18 * this file you indicate that you have read the license and
19 * understand and accept it fully.
24 #include <freetype/ftadvanc.h>
27 #ifdef FT_OPTION_AUTOFIT2
35 #ifdef AF_CONFIG_OPTION_USE_WARPER
40 /**************************************************************************
42 * The macro FT_COMPONENT is used in trace mode. It is an implicit
43 * parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log
44 * messages during execution.
47 #define FT_COMPONENT aflatin2
50 FT_LOCAL_DEF( FT_Error )
51 af_latin2_hints_compute_segments( AF_GlyphHints hints,
55 af_latin2_hints_link_segments( AF_GlyphHints hints,
58 /*************************************************************************/
59 /*************************************************************************/
61 /***** L A T I N G L O B A L M E T R I C S *****/
63 /*************************************************************************/
64 /*************************************************************************/
67 af_latin2_metrics_init_widths( AF_LatinMetrics metrics,
70 /* scan the array of segments in each direction */
71 AF_GlyphHintsRec hints[1];
74 af_glyph_hints_init( hints, face->memory );
76 metrics->axis[AF_DIMENSION_HORZ].width_count = 0;
77 metrics->axis[AF_DIMENSION_VERT].width_count = 0;
83 AF_LatinMetricsRec dummy[1];
84 AF_Scaler scaler = &dummy->root.scaler;
87 glyph_index = FT_Get_Char_Index(
89 metrics->root.style_class->standard_char );
90 if ( glyph_index == 0 )
93 error = FT_Load_Glyph( face, glyph_index, FT_LOAD_NO_SCALE );
94 if ( error || face->glyph->outline.n_points <= 0 )
99 dummy->units_per_em = metrics->units_per_em;
100 scaler->x_scale = scaler->y_scale = 0x10000L;
101 scaler->x_delta = scaler->y_delta = 0;
103 scaler->render_mode = FT_RENDER_MODE_NORMAL;
106 af_glyph_hints_rescale( hints, (AF_StyleMetrics)dummy );
108 error = af_glyph_hints_reload( hints, &face->glyph->outline );
112 for ( dim = 0; dim < AF_DIMENSION_MAX; dim++ )
114 AF_LatinAxis axis = &metrics->axis[dim];
115 AF_AxisHints axhints = &hints->axis[dim];
116 AF_Segment seg, limit, link;
117 FT_UInt num_widths = 0;
120 error = af_latin2_hints_compute_segments( hints,
125 af_latin2_hints_link_segments( hints,
128 seg = axhints->segments;
129 limit = seg + axhints->num_segments;
131 for ( ; seg < limit; seg++ )
135 /* we only consider stem segments there! */
136 if ( link && link->link == seg && link > seg )
141 dist = seg->pos - link->pos;
145 if ( num_widths < AF_LATIN_MAX_WIDTHS )
146 axis->widths[num_widths++].org = dist;
150 af_sort_widths( num_widths, axis->widths );
151 axis->width_count = num_widths;
155 for ( dim = 0; dim < AF_DIMENSION_MAX; dim++ )
157 AF_LatinAxis axis = &metrics->axis[dim];
161 stdw = ( axis->width_count > 0 )
162 ? axis->widths[0].org
163 : AF_LATIN_CONSTANT( metrics, 50 );
165 /* let's try 20% of the smallest width */
166 axis->edge_distance_threshold = stdw / 5;
167 axis->standard_width = stdw;
168 axis->extra_light = 0;
172 af_glyph_hints_done( hints );
177 #define AF_LATIN_MAX_TEST_CHARACTERS 12
180 static const char af_latin2_blue_chars[AF_LATIN_MAX_BLUES]
181 [AF_LATIN_MAX_TEST_CHARACTERS+1] =
193 af_latin2_metrics_init_blues( AF_LatinMetrics metrics,
196 FT_Pos flats [AF_LATIN_MAX_TEST_CHARACTERS];
197 FT_Pos rounds[AF_LATIN_MAX_TEST_CHARACTERS];
203 AF_LatinAxis axis = &metrics->axis[AF_DIMENSION_VERT];
204 FT_GlyphSlot glyph = face->glyph;
207 /* we compute the blues simply by loading each character from the */
208 /* 'af_latin2_blue_chars[blues]' string, then compute its top-most or */
209 /* bottom-most points (depending on `AF_IS_TOP_BLUE') */
211 FT_TRACE5(( "blue zones computation\n"
212 "======================\n\n" ));
214 for ( bb = 0; bb < AF_LATIN_BLUE_MAX; bb++ )
216 const char* p = af_latin2_blue_chars[bb];
217 const char* limit = p + AF_LATIN_MAX_TEST_CHARACTERS;
222 FT_TRACE5(( "blue zone %d:\n", bb ));
227 for ( ; p < limit && *p; p++ )
230 FT_Int best_point, best_y, best_first, best_last;
235 /* load the character in the face -- skip unknown or empty ones */
236 glyph_index = FT_Get_Char_Index( face, (FT_UInt)*p );
237 if ( glyph_index == 0 )
240 error = FT_Load_Glyph( face, glyph_index, FT_LOAD_NO_SCALE );
241 if ( error || glyph->outline.n_points <= 0 )
244 /* now compute min or max point indices and coordinates */
245 points = glyph->outline.points;
247 best_y = 0; /* make compiler happy */
248 best_first = 0; /* ditto */
249 best_last = 0; /* ditto */
257 for ( nn = 0; nn < glyph->outline.n_contours; first = last+1, nn++ )
259 FT_Int old_best_point = best_point;
263 last = glyph->outline.contours[nn];
265 /* Avoid single-point contours since they are never rasterized. */
266 /* In some fonts, they correspond to mark attachment points */
267 /* which are way outside of the glyph's real outline. */
271 if ( AF_LATIN_IS_TOP_BLUE( bb ) )
273 for ( pp = first; pp <= last; pp++ )
274 if ( best_point < 0 || points[pp].y > best_y )
277 best_y = points[pp].y;
282 for ( pp = first; pp <= last; pp++ )
283 if ( best_point < 0 || points[pp].y < best_y )
286 best_y = points[pp].y;
290 if ( best_point != old_best_point )
296 FT_TRACE5(( " %c %d", *p, best_y ));
299 /* now check whether the point belongs to a straight or round */
300 /* segment; we first need to find in which contour the extremum */
301 /* lies, then inspect its previous and next points */
302 if ( best_point >= 0 )
304 FT_Pos best_x = points[best_point].x;
305 FT_Int start, end, prev, next;
309 /* now look for the previous and next points that are not on the */
310 /* same Y coordinate. Threshold the `closeness'... */
311 start = end = best_point;
316 if ( prev < best_first )
319 dist = FT_ABS( points[prev].y - best_y );
320 /* accept a small distance or a small angle (both values are */
321 /* heuristic; value 20 corresponds to approx. 2.9 degrees) */
323 if ( FT_ABS( points[prev].x - best_x ) <= 20 * dist )
328 } while ( start != best_point );
333 if ( next > best_last )
336 dist = FT_ABS( points[next].y - best_y );
338 if ( FT_ABS( points[next].x - best_x ) <= 20 * dist )
343 } while ( end != best_point );
345 /* now, set the `round' flag depending on the segment's kind */
347 FT_CURVE_TAG( glyph->outline.tags[start] ) != FT_CURVE_TAG_ON ||
348 FT_CURVE_TAG( glyph->outline.tags[ end ] ) != FT_CURVE_TAG_ON );
350 FT_TRACE5(( " (%s)\n", round ? "round" : "flat" ));
354 rounds[num_rounds++] = best_y;
356 flats[num_flats++] = best_y;
359 if ( num_flats == 0 && num_rounds == 0 )
362 * we couldn't find a single glyph to compute this blue zone,
363 * we will simply ignore it then
365 FT_TRACE5(( " empty\n" ));
369 /* we have computed the contents of the `rounds' and `flats' tables, */
370 /* now determine the reference and overshoot position of the blue -- */
371 /* we simply take the median value after a simple sort */
372 af_sort_pos( num_rounds, rounds );
373 af_sort_pos( num_flats, flats );
375 blue = & axis->blues[axis->blue_count];
376 blue_ref = & blue->ref.org;
377 blue_shoot = & blue->shoot.org;
381 if ( num_flats == 0 )
384 *blue_shoot = rounds[num_rounds / 2];
386 else if ( num_rounds == 0 )
389 *blue_shoot = flats[num_flats / 2];
393 *blue_ref = flats[num_flats / 2];
394 *blue_shoot = rounds[num_rounds / 2];
397 /* there are sometimes problems: if the overshoot position of top */
398 /* zones is under its reference position, or the opposite for bottom */
399 /* zones. We must thus check everything there and correct the errors */
400 if ( *blue_shoot != *blue_ref )
402 FT_Pos ref = *blue_ref;
403 FT_Pos shoot = *blue_shoot;
404 FT_Bool over_ref = FT_BOOL( shoot > ref );
407 if ( AF_LATIN_IS_TOP_BLUE( bb ) ^ over_ref )
410 *blue_shoot = ( shoot + ref ) / 2;
412 FT_TRACE5(( " [overshoot smaller than reference,"
413 " taking mean value]\n" ));
418 if ( AF_LATIN_IS_TOP_BLUE( bb ) )
419 blue->flags |= AF_LATIN_BLUE_TOP;
422 * The following flag is used later to adjust the y and x scales
423 * in order to optimize the pixel grid alignment of the top of small
426 if ( AF_LATIN_IS_X_HEIGHT_BLUE( bb ) )
427 blue->flags |= AF_LATIN_BLUE_ADJUSTMENT;
429 FT_TRACE5(( " -> reference = %ld\n"
430 " overshoot = %ld\n",
431 *blue_ref, *blue_shoot ));
439 af_latin2_metrics_check_digits( AF_LatinMetrics metrics,
443 FT_Bool started = 0, same_width = 1;
444 FT_Fixed advance, old_advance = 0;
447 /* check whether all ASCII digits have the same advance width; */
448 /* digit `0' is 0x30 in all supported charmaps */
449 for ( i = 0x30; i <= 0x39; i++ )
454 glyph_index = FT_Get_Char_Index( face, i );
455 if ( glyph_index == 0 )
458 if ( FT_Get_Advance( face, glyph_index,
461 FT_LOAD_IGNORE_TRANSFORM,
467 if ( advance != old_advance )
475 old_advance = advance;
480 metrics->root.digits_have_same_width = same_width;
484 FT_LOCAL_DEF( FT_Error )
485 af_latin2_metrics_init( AF_LatinMetrics metrics,
488 FT_Error error = FT_Err_Ok;
489 FT_CharMap oldmap = face->charmap;
492 static const FT_Encoding latin_encodings[] =
495 FT_ENCODING_APPLE_ROMAN,
496 FT_ENCODING_ADOBE_STANDARD,
497 FT_ENCODING_ADOBE_LATIN_1,
498 FT_ENCODING_NONE /* end of list */
502 metrics->units_per_em = face->units_per_EM;
504 /* do we have a latin charmap in there? */
505 for ( ee = 0; latin_encodings[ee] != FT_ENCODING_NONE; ee++ )
507 error = FT_Select_Charmap( face, latin_encodings[ee] );
514 af_latin2_metrics_init_widths( metrics, face );
515 af_latin2_metrics_init_blues( metrics, face );
516 af_latin2_metrics_check_digits( metrics, face );
519 FT_Set_Charmap( face, oldmap );
525 af_latin2_metrics_scale_dim( AF_LatinMetrics metrics,
535 if ( dim == AF_DIMENSION_HORZ )
537 scale = scaler->x_scale;
538 delta = scaler->x_delta;
542 scale = scaler->y_scale;
543 delta = scaler->y_delta;
546 axis = &metrics->axis[dim];
548 if ( axis->org_scale == scale && axis->org_delta == delta )
551 axis->org_scale = scale;
552 axis->org_delta = delta;
555 * correct Y scale to optimize the alignment of the top of small
556 * letters to the pixel grid
558 if ( dim == AF_DIMENSION_VERT )
560 AF_LatinAxis vaxis = &metrics->axis[AF_DIMENSION_VERT];
561 AF_LatinBlue blue = NULL;
564 for ( nn = 0; nn < vaxis->blue_count; nn++ )
566 if ( vaxis->blues[nn].flags & AF_LATIN_BLUE_ADJUSTMENT )
568 blue = &vaxis->blues[nn];
582 scaled = FT_MulFix( blue->shoot.org, scaler->y_scale );
583 ppem = metrics->root.scaler.face->size->metrics.x_ppem;
584 limit = metrics->root.globals->increase_x_height;
587 /* if the `increase-x-height' property is active, */
588 /* we round up much more often */
591 ppem >= AF_PROP_INCREASE_X_HEIGHT_MIN )
594 fitted = ( scaled + threshold ) & ~63;
597 if ( scaled != fitted )
599 scale = FT_MulDiv( scale, fitted, scaled );
600 FT_TRACE5(( "== scaled x-top = %.2g"
601 " fitted = %.2g, scaling = %.4g\n",
602 scaled / 64.0, fitted / 64.0,
603 ( fitted * 1.0 ) / scaled ));
612 if ( dim == AF_DIMENSION_HORZ )
614 metrics->root.scaler.x_scale = scale;
615 metrics->root.scaler.x_delta = delta;
619 metrics->root.scaler.y_scale = scale;
620 metrics->root.scaler.y_delta = delta;
623 /* scale the standard widths */
624 for ( nn = 0; nn < axis->width_count; nn++ )
626 AF_Width width = axis->widths + nn;
629 width->cur = FT_MulFix( width->org, scale );
630 width->fit = width->cur;
633 /* an extra-light axis corresponds to a standard width that is */
634 /* smaller than 5/8 pixels */
636 FT_BOOL( FT_MulFix( axis->standard_width, scale ) < 32 + 8 );
638 if ( dim == AF_DIMENSION_VERT )
640 /* scale the blue zones */
641 for ( nn = 0; nn < axis->blue_count; nn++ )
643 AF_LatinBlue blue = &axis->blues[nn];
647 blue->ref.cur = FT_MulFix( blue->ref.org, scale ) + delta;
648 blue->ref.fit = blue->ref.cur;
649 blue->shoot.cur = FT_MulFix( blue->shoot.org, scale ) + delta;
650 blue->shoot.fit = blue->shoot.cur;
651 blue->flags &= ~AF_LATIN_BLUE_ACTIVE;
653 /* a blue zone is only active if it is less than 3/4 pixels tall */
654 dist = FT_MulFix( blue->ref.org - blue->shoot.org, scale );
655 if ( dist <= 48 && dist >= -48 )
657 FT_Pos delta1, delta2;
659 delta1 = blue->shoot.org - blue->ref.org;
664 delta2 = FT_MulFix( delta2, scale );
668 else if ( delta2 < 64 )
669 delta2 = 32 + ( ( ( delta2 - 32 ) + 16 ) & ~31 );
671 delta2 = FT_PIX_ROUND( delta2 );
676 blue->ref.fit = FT_PIX_ROUND( blue->ref.cur );
677 blue->shoot.fit = blue->ref.fit + delta2;
679 FT_TRACE5(( ">> activating blue zone %d:"
680 " ref.cur=%.2g ref.fit=%.2g"
681 " shoot.cur=%.2g shoot.fit=%.2g\n",
682 nn, blue->ref.cur / 64.0, blue->ref.fit / 64.0,
683 blue->shoot.cur / 64.0, blue->shoot.fit / 64.0 ));
685 blue->flags |= AF_LATIN_BLUE_ACTIVE;
693 af_latin2_metrics_scale( AF_LatinMetrics metrics,
696 metrics->root.scaler.render_mode = scaler->render_mode;
697 metrics->root.scaler.face = scaler->face;
698 metrics->root.scaler.flags = scaler->flags;
700 af_latin2_metrics_scale_dim( metrics, scaler, AF_DIMENSION_HORZ );
701 af_latin2_metrics_scale_dim( metrics, scaler, AF_DIMENSION_VERT );
705 /* Extract standard_width from writing system/script specific */
709 af_latin2_get_standard_widths( AF_LatinMetrics metrics,
714 *stdHW = metrics->axis[AF_DIMENSION_VERT].standard_width;
717 *stdVW = metrics->axis[AF_DIMENSION_HORZ].standard_width;
721 /*************************************************************************/
722 /*************************************************************************/
724 /***** L A T I N G L Y P H A N A L Y S I S *****/
726 /*************************************************************************/
727 /*************************************************************************/
729 #define SORT_SEGMENTS
731 FT_LOCAL_DEF( FT_Error )
732 af_latin2_hints_compute_segments( AF_GlyphHints hints,
735 AF_AxisHints axis = &hints->axis[dim];
736 FT_Memory memory = hints->memory;
737 FT_Error error = FT_Err_Ok;
738 AF_Segment segment = NULL;
740 AF_Point* contour = hints->contours;
741 AF_Point* contour_limit = contour + hints->num_contours;
742 AF_Direction major_dir, segment_dir;
747 seg0.flags = AF_EDGE_NORMAL;
749 major_dir = (AF_Direction)FT_ABS( axis->major_dir );
750 segment_dir = major_dir;
752 axis->num_segments = 0;
754 /* set up (u,v) in each point */
755 if ( dim == AF_DIMENSION_HORZ )
757 AF_Point point = hints->points;
758 AF_Point limit = point + hints->num_points;
761 for ( ; point < limit; point++ )
763 point->u = point->fx;
764 point->v = point->fy;
769 AF_Point point = hints->points;
770 AF_Point limit = point + hints->num_points;
773 for ( ; point < limit; point++ )
775 point->u = point->fy;
776 point->v = point->fx;
780 /* do each contour separately */
781 for ( ; contour < contour_limit; contour++ )
783 AF_Point point = contour[0];
784 AF_Point start = point;
785 AF_Point last = point->prev;
788 if ( point == last ) /* skip singletons -- just in case */
791 /* already on an edge ?, backtrack to find its start */
792 if ( FT_ABS( point->in_dir ) == major_dir )
796 while ( point->in_dir == start->in_dir )
799 else /* otherwise, find first segment start, if any */
801 while ( FT_ABS( point->out_dir ) != major_dir )
805 if ( point == start )
815 FT_Pos min_u, min_v, max_u, max_v;
817 /* we're at the start of a new segment */
818 FT_ASSERT( FT_ABS( point->out_dir ) == major_dir &&
819 point->in_dir != point->out_dir );
822 min_u = max_u = point->u;
823 min_v = max_v = point->v;
827 while ( point->out_dir == first->out_dir )
831 if ( point->u < min_u )
834 if ( point->u > max_u )
838 if ( point->v < min_v )
841 if ( point->v > max_v )
844 /* record new segment */
845 error = af_axis_hints_new_segment( axis, memory, &segment );
850 segment->dir = first->out_dir;
851 segment->first = first;
852 segment->last = point;
853 segment->pos = (FT_Short)( ( min_u + max_u ) >> 1 );
854 segment->min_coord = (FT_Short) min_v;
855 segment->max_coord = (FT_Short) max_v;
856 segment->height = (FT_Short)( max_v - min_v );
858 /* a segment is round if it doesn't have successive */
859 /* on-curve points. */
862 AF_Point last = point;
863 FT_UInt f0 = pt->flags & AF_FLAG_CONTROL;
867 segment->flags &= ~AF_EDGE_ROUND;
869 for ( ; pt != last; f0 = f1 )
872 f1 = pt->flags & AF_FLAG_CONTROL;
878 segment->flags |= AF_EDGE_ROUND;
882 /* this can happen in the case of a degenerate contour
883 * e.g. a 2-point vertical contour
885 if ( point == start )
888 /* jump to the start of the next segment, if any */
889 while ( FT_ABS( point->out_dir ) != major_dir )
893 if ( point == start )
902 /* now slightly increase the height of segments when this makes */
903 /* sense -- this is used to better detect and ignore serifs */
905 AF_Segment segments = axis->segments;
906 AF_Segment segments_end = segments + axis->num_segments;
909 for ( segment = segments; segment < segments_end; segment++ )
911 AF_Point first = segment->first;
912 AF_Point last = segment->last;
914 FT_Pos first_v = first->v;
915 FT_Pos last_v = last->v;
918 if ( first_v < last_v )
921 if ( p->v < first_v )
922 segment->height = (FT_Short)( segment->height +
923 ( ( first_v - p->v ) >> 1 ) );
927 segment->height = (FT_Short)( segment->height +
928 ( ( p->v - last_v ) >> 1 ) );
933 if ( p->v > first_v )
934 segment->height = (FT_Short)( segment->height +
935 ( ( p->v - first_v ) >> 1 ) );
939 segment->height = (FT_Short)( segment->height +
940 ( ( last_v - p->v ) >> 1 ) );
945 #ifdef AF_SORT_SEGMENTS
946 /* place all segments with a negative direction to the start
947 * of the array, used to speed up segment linking later...
950 AF_Segment segments = axis->segments;
951 FT_UInt count = axis->num_segments;
954 for ( ii = 0; ii < count; ii++ )
956 if ( segments[ii].dir > 0 )
958 for ( jj = ii + 1; jj < count; jj++ )
960 if ( segments[jj].dir < 0 )
966 segments[ii] = segments[jj];
977 axis->mid_segments = ii;
987 af_latin2_hints_link_segments( AF_GlyphHints hints,
990 AF_AxisHints axis = &hints->axis[dim];
991 AF_Segment segments = axis->segments;
992 AF_Segment segment_limit = segments + axis->num_segments;
993 #ifdef AF_SORT_SEGMENTS
994 AF_Segment segment_mid = segments + axis->mid_segments;
996 FT_Pos len_threshold, len_score;
997 AF_Segment seg1, seg2;
1000 len_threshold = AF_LATIN_CONSTANT( hints->metrics, 8 );
1001 if ( len_threshold == 0 )
1004 len_score = AF_LATIN_CONSTANT( hints->metrics, 6000 );
1006 #ifdef AF_SORT_SEGMENTS
1007 for ( seg1 = segments; seg1 < segment_mid; seg1++ )
1009 if ( seg1->dir != axis->major_dir )
1012 for ( seg2 = segment_mid; seg2 < segment_limit; seg2++ )
1014 /* now compare each segment to the others */
1015 for ( seg1 = segments; seg1 < segment_limit; seg1++ )
1017 if ( seg1->dir != axis->major_dir )
1020 for ( seg2 = segments; seg2 < segment_limit; seg2++ )
1021 if ( seg1->dir + seg2->dir == 0 && seg2->pos > seg1->pos )
1024 FT_Pos pos1 = seg1->pos;
1025 FT_Pos pos2 = seg2->pos;
1026 FT_Pos dist = pos2 - pos1;
1033 FT_Pos min = seg1->min_coord;
1034 FT_Pos max = seg1->max_coord;
1038 if ( min < seg2->min_coord )
1039 min = seg2->min_coord;
1041 if ( max > seg2->max_coord )
1042 max = seg2->max_coord;
1045 if ( len >= len_threshold )
1047 score = dist + len_score / len;
1048 if ( score < seg1->score )
1050 seg1->score = score;
1054 if ( score < seg2->score )
1056 seg2->score = score;
1067 /* now, compute the `serif' segments */
1068 for ( seg1 = segments; seg1 < segment_limit; seg1++ )
1074 if ( seg2->link != seg1 )
1077 seg1->serif = seg2->link;
1084 FT_LOCAL_DEF( FT_Error )
1085 af_latin2_hints_compute_edges( AF_GlyphHints hints,
1088 AF_AxisHints axis = &hints->axis[dim];
1089 FT_Error error = FT_Err_Ok;
1090 FT_Memory memory = hints->memory;
1091 AF_LatinAxis laxis = &((AF_LatinMetrics)hints->metrics)->axis[dim];
1093 AF_Segment segments = axis->segments;
1094 AF_Segment segment_limit = segments + axis->num_segments;
1097 AF_Direction up_dir;
1099 FT_Pos edge_distance_threshold;
1100 FT_Pos segment_length_threshold;
1103 axis->num_edges = 0;
1105 scale = ( dim == AF_DIMENSION_HORZ ) ? hints->x_scale
1108 up_dir = ( dim == AF_DIMENSION_HORZ ) ? AF_DIR_UP
1112 * We want to ignore very small (mostly serif) segments, we do that
1113 * by ignoring those that whose length is less than a given fraction
1114 * of the standard width. If there is no standard width, we ignore
1115 * those that are less than a given size in pixels
1117 * also, unlink serif segments that are linked to segments farther
1118 * than 50% of the standard width
1120 if ( dim == AF_DIMENSION_HORZ )
1122 if ( laxis->width_count > 0 )
1123 segment_length_threshold = ( laxis->standard_width * 10 ) >> 4;
1125 segment_length_threshold = FT_DivFix( 64, hints->y_scale );
1128 segment_length_threshold = 0;
1130 /**********************************************************************
1132 * We will begin by generating a sorted table of edges for the
1133 * current direction. To do so, we simply scan each segment and try
1134 * to find an edge in our table that corresponds to its position.
1136 * If no edge is found, we create and insert a new edge in the
1137 * sorted table. Otherwise, we simply add the segment to the edge's
1138 * list which will be processed in the second step to compute the
1139 * edge's properties.
1141 * Note that the edges table is sorted along the segment/edge
1146 edge_distance_threshold = FT_MulFix( laxis->edge_distance_threshold,
1148 if ( edge_distance_threshold > 64 / 4 )
1149 edge_distance_threshold = 64 / 4;
1151 edge_distance_threshold = FT_DivFix( edge_distance_threshold,
1154 for ( seg = segments; seg < segment_limit; seg++ )
1156 AF_Edge found = NULL;
1160 if ( seg->height < segment_length_threshold )
1163 /* A special case for serif edges: If they are smaller than */
1164 /* 1.5 pixels we ignore them. */
1167 FT_Pos dist = seg->serif->pos - seg->pos;
1173 if ( dist >= laxis->standard_width >> 1 )
1175 /* unlink this serif, it is too distant from its reference stem */
1178 else if ( 2*seg->height < 3 * segment_length_threshold )
1182 /* look for an edge corresponding to the segment */
1183 for ( ee = 0; ee < axis->num_edges; ee++ )
1185 AF_Edge edge = axis->edges + ee;
1189 dist = seg->pos - edge->fpos;
1193 if ( dist < edge_distance_threshold && edge->dir == seg->dir )
1205 /* insert a new edge in the list and */
1206 /* sort according to the position */
1207 error = af_axis_hints_new_edge( axis, seg->pos, seg->dir, 0,
1212 /* add the segment to the new edge's list */
1217 edge->dir = seg->dir;
1218 edge->fpos = seg->pos;
1219 edge->opos = FT_MulFix( seg->pos, scale );
1220 edge->pos = edge->opos;
1221 seg->edge_next = seg;
1225 /* if an edge was found, simply add the segment to the edge's */
1227 seg->edge_next = found->first;
1228 found->last->edge_next = seg;
1234 /**********************************************************************
1236 * Good, we will now compute each edge's properties according to
1237 * segments found on its position. Basically, these are:
1239 * - edge's main direction
1240 * - stem edge, serif edge or both (which defaults to stem then)
1241 * - rounded edge, straight or both (which defaults to straight)
1246 /* first of all, set the `edge' field in each segment -- this is */
1247 /* required in order to compute edge links */
1250 * Note that removing this loop and setting the `edge' field of each
1251 * segment directly in the code above slows down execution speed for
1252 * some reasons on platforms like the Sun.
1255 AF_Edge edges = axis->edges;
1256 AF_Edge edge_limit = edges + axis->num_edges;
1260 for ( edge = edges; edge < edge_limit; edge++ )
1267 seg = seg->edge_next;
1269 } while ( seg != edge->first );
1272 /* now, compute each edge properties */
1273 for ( edge = edges; edge < edge_limit; edge++ )
1275 FT_Int is_round = 0; /* does it contain round segments? */
1276 FT_Int is_straight = 0; /* does it contain straight segments? */
1278 FT_Pos ups = 0; /* number of upwards segments */
1279 FT_Pos downs = 0; /* number of downwards segments */
1290 /* check for roundness of segment */
1291 if ( seg->flags & AF_EDGE_ROUND )
1297 /* check for segment direction */
1298 if ( seg->dir == up_dir )
1299 ups += seg->max_coord-seg->min_coord;
1301 downs += seg->max_coord-seg->min_coord;
1304 /* check for links -- if seg->serif is set, then seg->link must */
1306 is_serif = FT_BOOL( seg->serif &&
1308 seg->serif->edge != edge );
1310 if ( ( seg->link && seg->link->edge ) || is_serif )
1322 edge2 = edge->serif;
1331 edge_delta = edge->fpos - edge2->fpos;
1332 if ( edge_delta < 0 )
1333 edge_delta = -edge_delta;
1335 seg_delta = seg->pos - seg2->pos;
1336 if ( seg_delta < 0 )
1337 seg_delta = -seg_delta;
1339 if ( seg_delta < edge_delta )
1347 edge->serif = edge2;
1348 edge2->flags |= AF_EDGE_SERIF;
1354 seg = seg->edge_next;
1356 } while ( seg != edge->first );
1358 /* set the round/straight flags */
1359 edge->flags = AF_EDGE_NORMAL;
1361 if ( is_round > 0 && is_round >= is_straight )
1362 edge->flags |= AF_EDGE_ROUND;
1365 /* set the edge's main direction */
1366 edge->dir = AF_DIR_NONE;
1369 edge->dir = (FT_Char)up_dir;
1371 else if ( ups < downs )
1372 edge->dir = (FT_Char)-up_dir;
1374 else if ( ups == downs )
1375 edge->dir = 0; /* both up and down! */
1378 /* gets rid of serifs if link is set */
1379 /* XXX: This gets rid of many unpleasant artefacts! */
1380 /* Example: the `c' in cour.pfa at size 13 */
1382 if ( edge->serif && edge->link )
1392 FT_LOCAL_DEF( FT_Error )
1393 af_latin2_hints_detect_features( AF_GlyphHints hints,
1399 error = af_latin2_hints_compute_segments( hints, dim );
1402 af_latin2_hints_link_segments( hints, dim );
1404 error = af_latin2_hints_compute_edges( hints, dim );
1411 af_latin2_hints_compute_blue_edges( AF_GlyphHints hints,
1412 AF_LatinMetrics metrics )
1414 AF_AxisHints axis = &hints->axis[AF_DIMENSION_VERT];
1415 AF_Edge edge = axis->edges;
1416 AF_Edge edge_limit = edge + axis->num_edges;
1417 AF_LatinAxis latin = &metrics->axis[AF_DIMENSION_VERT];
1418 FT_Fixed scale = latin->scale;
1419 FT_Pos best_dist0; /* initial threshold */
1422 /* compute the initial threshold as a fraction of the EM size */
1423 best_dist0 = FT_MulFix( metrics->units_per_em / 40, scale );
1425 if ( best_dist0 > 64 / 2 )
1426 best_dist0 = 64 / 2;
1428 /* compute which blue zones are active, i.e. have their scaled */
1429 /* size < 3/4 pixels */
1431 /* for each horizontal edge search the blue zone which is closest */
1432 for ( ; edge < edge_limit; edge++ )
1435 AF_Width best_blue = NULL;
1436 FT_Pos best_dist = best_dist0;
1438 for ( bb = 0; bb < AF_LATIN_BLUE_MAX; bb++ )
1440 AF_LatinBlue blue = latin->blues + bb;
1441 FT_Bool is_top_blue, is_major_dir;
1444 /* skip inactive blue zones (i.e., those that are too small) */
1445 if ( !( blue->flags & AF_LATIN_BLUE_ACTIVE ) )
1448 /* if it is a top zone, check for right edges -- if it is a bottom */
1449 /* zone, check for left edges */
1451 /* of course, that's for TrueType */
1452 is_top_blue = (FT_Byte)( ( blue->flags & AF_LATIN_BLUE_TOP ) != 0 );
1453 is_major_dir = FT_BOOL( edge->dir == axis->major_dir );
1455 /* if it is a top zone, the edge must be against the major */
1456 /* direction; if it is a bottom zone, it must be in the major */
1458 if ( is_top_blue ^ is_major_dir )
1464 /* if it's a rounded edge, compare it to the overshoot position */
1465 /* if it's a flat edge, compare it to the reference position */
1466 if ( edge->flags & AF_EDGE_ROUND )
1467 compare = &blue->shoot;
1469 compare = &blue->ref;
1471 dist = edge->fpos - compare->org;
1475 dist = FT_MulFix( dist, scale );
1476 if ( dist < best_dist )
1479 best_blue = compare;
1483 /* now, compare it to the overshoot position if the edge is */
1484 /* rounded, and if the edge is over the reference position of a */
1485 /* top zone, or under the reference position of a bottom zone */
1486 if ( edge->flags & AF_EDGE_ROUND && dist != 0 )
1488 FT_Bool is_under_ref = FT_BOOL( edge->fpos < blue->ref.org );
1491 if ( is_top_blue ^ is_under_ref )
1493 blue = latin->blues + bb;
1494 dist = edge->fpos - blue->shoot.org;
1498 dist = FT_MulFix( dist, scale );
1499 if ( dist < best_dist )
1502 best_blue = & blue->shoot;
1511 edge->blue_edge = best_blue;
1517 af_latin2_hints_init( AF_GlyphHints hints,
1518 AF_LatinMetrics metrics )
1520 FT_Render_Mode mode;
1521 FT_UInt32 scaler_flags, other_flags;
1522 FT_Face face = metrics->root.scaler.face;
1525 af_glyph_hints_rescale( hints, (AF_StyleMetrics)metrics );
1528 * correct x_scale and y_scale if needed, since they may have
1529 * been modified `af_latin2_metrics_scale_dim' above
1531 hints->x_scale = metrics->axis[AF_DIMENSION_HORZ].scale;
1532 hints->x_delta = metrics->axis[AF_DIMENSION_HORZ].delta;
1533 hints->y_scale = metrics->axis[AF_DIMENSION_VERT].scale;
1534 hints->y_delta = metrics->axis[AF_DIMENSION_VERT].delta;
1536 /* compute flags depending on render mode, etc. */
1537 mode = metrics->root.scaler.render_mode;
1539 #if 0 /* #ifdef AF_CONFIG_OPTION_USE_WARPER */
1540 if ( mode == FT_RENDER_MODE_LCD || mode == FT_RENDER_MODE_LCD_V )
1541 metrics->root.scaler.render_mode = mode = FT_RENDER_MODE_NORMAL;
1544 scaler_flags = hints->scaler_flags;
1548 * We snap the width of vertical stems for the monochrome and
1549 * horizontal LCD rendering targets only.
1551 if ( mode == FT_RENDER_MODE_MONO || mode == FT_RENDER_MODE_LCD )
1552 other_flags |= AF_LATIN_HINTS_HORZ_SNAP;
1555 * We snap the width of horizontal stems for the monochrome and
1556 * vertical LCD rendering targets only.
1558 if ( mode == FT_RENDER_MODE_MONO || mode == FT_RENDER_MODE_LCD_V )
1559 other_flags |= AF_LATIN_HINTS_VERT_SNAP;
1562 * We adjust stems to full pixels unless in `light' or `lcd' mode.
1564 if ( mode != FT_RENDER_MODE_LIGHT && mode != FT_RENDER_MODE_LCD )
1565 other_flags |= AF_LATIN_HINTS_STEM_ADJUST;
1567 if ( mode == FT_RENDER_MODE_MONO )
1568 other_flags |= AF_LATIN_HINTS_MONO;
1571 * In `light' or `lcd' mode we disable horizontal hinting completely.
1572 * We also do it if the face is italic.
1574 if ( mode == FT_RENDER_MODE_LIGHT || mode == FT_RENDER_MODE_LCD ||
1575 ( face->style_flags & FT_STYLE_FLAG_ITALIC ) != 0 )
1576 scaler_flags |= AF_SCALER_FLAG_NO_HORIZONTAL;
1578 #ifdef AF_CONFIG_OPTION_USE_WARPER
1579 /* get (global) warper flag */
1580 if ( !metrics->root.globals->module->warping )
1581 scaler_flags |= AF_SCALER_FLAG_NO_WARPER;
1584 hints->scaler_flags = scaler_flags;
1585 hints->other_flags = other_flags;
1591 /*************************************************************************/
1592 /*************************************************************************/
1594 /***** L A T I N G L Y P H G R I D - F I T T I N G *****/
1596 /*************************************************************************/
1597 /*************************************************************************/
1599 /* snap a given width in scaled coordinates to one of the */
1600 /* current standard widths */
1603 af_latin2_snap_width( AF_Width widths,
1608 FT_Pos best = 64 + 32 + 2;
1609 FT_Pos reference = width;
1613 for ( n = 0; n < count; n++ )
1630 scaled = FT_PIX_ROUND( reference );
1632 if ( width >= reference )
1634 if ( width < scaled + 48 )
1639 if ( width > scaled - 48 )
1647 /* compute the snapped width of a given stem */
1650 af_latin2_compute_stem_width( AF_GlyphHints hints,
1654 FT_UInt stem_flags )
1656 AF_LatinMetrics metrics = (AF_LatinMetrics) hints->metrics;
1657 AF_LatinAxis axis = & metrics->axis[dim];
1658 FT_Pos dist = width;
1660 FT_Int vertical = ( dim == AF_DIMENSION_VERT );
1662 FT_UNUSED( base_flags );
1665 if ( !AF_LATIN_HINTS_DO_STEM_ADJUST( hints ) ||
1675 if ( ( vertical && !AF_LATIN_HINTS_DO_VERT_SNAP( hints ) ) ||
1676 ( !vertical && !AF_LATIN_HINTS_DO_HORZ_SNAP( hints ) ) )
1678 /* smooth hinting process: very lightly quantize the stem width */
1680 /* leave the widths of serifs alone */
1682 if ( ( stem_flags & AF_EDGE_SERIF ) && vertical && ( dist < 3 * 64 ) )
1686 else if ( ( base_flags & AF_EDGE_ROUND ) )
1691 else if ( dist < 56 )
1694 if ( axis->width_count > 0 )
1699 /* compare to standard width */
1700 if ( axis->width_count > 0 )
1702 delta = dist - axis->widths[0].cur;
1709 dist = axis->widths[0].cur;
1717 if ( dist < 3 * 64 )
1725 else if ( delta < 32 )
1728 else if ( delta < 54 )
1735 dist = ( dist + 32 ) & ~63;
1740 /* strong hinting process: snap the stem width to integer pixels */
1741 FT_Pos org_dist = dist;
1744 dist = af_latin2_snap_width( axis->widths, axis->width_count, dist );
1748 /* in the case of vertical hinting, always round */
1749 /* the stem heights to integer pixels */
1752 dist = ( dist + 16 ) & ~63;
1758 if ( AF_LATIN_HINTS_DO_MONO( hints ) )
1760 /* monochrome horizontal hinting: snap widths to integer pixels */
1761 /* with a different threshold */
1766 dist = ( dist + 32 ) & ~63;
1770 /* for horizontal anti-aliased hinting, we adopt a more subtle */
1771 /* approach: we strengthen small stems, round stems whose size */
1772 /* is between 1 and 2 pixels to an integer, otherwise nothing */
1775 dist = ( dist + 64 ) >> 1;
1777 else if ( dist < 128 )
1779 /* We only round to an integer width if the corresponding */
1780 /* distortion is less than 1/4 pixel. Otherwise this */
1781 /* makes everything worse since the diagonals, which are */
1782 /* not hinted, appear a lot bolder or thinner than the */
1783 /* vertical stems. */
1788 dist = ( dist + 22 ) & ~63;
1789 delta = dist - org_dist;
1797 dist = ( dist + 64 ) >> 1;
1801 /* round otherwise to prevent color fringes in LCD mode */
1802 dist = ( dist + 32 ) & ~63;
1815 /* align one stem edge relative to the previous stem edge */
1818 af_latin2_align_linked_edge( AF_GlyphHints hints,
1823 FT_Pos dist = stem_edge->opos - base_edge->opos;
1825 FT_Pos fitted_width = af_latin2_compute_stem_width( hints, dim, dist,
1830 stem_edge->pos = base_edge->pos + fitted_width;
1832 FT_TRACE5(( "LINK: edge %d (opos=%.2f) linked to (%.2f), "
1833 "dist was %.2f, now %.2f\n",
1834 stem_edge-hints->axis[dim].edges, stem_edge->opos / 64.0,
1835 stem_edge->pos / 64.0, dist / 64.0, fitted_width / 64.0 ));
1840 af_latin2_align_serif_edge( AF_GlyphHints hints,
1846 serif->pos = base->pos + ( serif->opos - base->opos );
1850 /*************************************************************************/
1851 /*************************************************************************/
1852 /*************************************************************************/
1854 /**** E D G E H I N T I N G ****/
1856 /*************************************************************************/
1857 /*************************************************************************/
1858 /*************************************************************************/
1862 af_latin2_hint_edges( AF_GlyphHints hints,
1865 AF_AxisHints axis = &hints->axis[dim];
1866 AF_Edge edges = axis->edges;
1867 AF_Edge edge_limit = edges + axis->num_edges;
1869 AF_Edge anchor = NULL;
1870 FT_Int has_serifs = 0;
1871 FT_Pos anchor_drift = 0;
1875 FT_TRACE5(( "==== hinting %s edges =====\n",
1876 dim == AF_DIMENSION_HORZ ? "vertical" : "horizontal" ));
1878 /* we begin by aligning all stems relative to the blue zone */
1879 /* if needed -- that's only for horizontal edges */
1881 if ( dim == AF_DIMENSION_VERT && AF_HINTS_DO_BLUES( hints ) )
1883 for ( edge = edges; edge < edge_limit; edge++ )
1886 AF_Edge edge1, edge2;
1889 if ( edge->flags & AF_EDGE_DONE )
1892 blue = edge->blue_edge;
1900 else if ( edge2 && edge2->blue_edge )
1902 blue = edge2->blue_edge;
1910 FT_TRACE5(( "BLUE: edge %d (opos=%.2f) snapped to (%.2f), "
1912 edge1-edges, edge1->opos / 64.0, blue->fit / 64.0,
1913 edge1->pos / 64.0 ));
1915 edge1->pos = blue->fit;
1916 edge1->flags |= AF_EDGE_DONE;
1918 if ( edge2 && !edge2->blue_edge )
1920 af_latin2_align_linked_edge( hints, dim, edge1, edge2 );
1921 edge2->flags |= AF_EDGE_DONE;
1928 anchor_drift = ( anchor->pos - anchor->opos );
1930 anchor_drift = ( anchor_drift +
1931 ( edge2->pos - edge2->opos ) ) >> 1;
1936 /* now we will align all stem edges, trying to maintain the */
1937 /* relative order of stems in the glyph */
1938 for ( edge = edges; edge < edge_limit; edge++ )
1943 if ( edge->flags & AF_EDGE_DONE )
1946 /* skip all non-stem edges */
1954 /* now align the stem */
1956 /* this should not happen, but it's better to be safe */
1957 if ( edge2->blue_edge )
1959 FT_TRACE5(( "ASSERTION FAILED for edge %d\n", edge2-edges ));
1961 af_latin2_align_linked_edge( hints, dim, edge2, edge );
1962 edge->flags |= AF_EDGE_DONE;
1968 FT_Pos org_len, org_center, cur_len;
1969 FT_Pos cur_pos1, error1, error2, u_off, d_off;
1972 org_len = edge2->opos - edge->opos;
1973 cur_len = af_latin2_compute_stem_width( hints, dim, org_len,
1976 if ( cur_len <= 64 )
1986 org_center = edge->opos + ( org_len >> 1 );
1988 cur_pos1 = FT_PIX_ROUND( org_center );
1990 error1 = org_center - ( cur_pos1 - u_off );
1994 error2 = org_center - ( cur_pos1 + d_off );
1998 if ( error1 < error2 )
2003 edge->pos = cur_pos1 - cur_len / 2;
2004 edge2->pos = edge->pos + cur_len;
2007 edge->pos = FT_PIX_ROUND( edge->opos );
2009 FT_TRACE5(( "ANCHOR: edge %d (opos=%.2f) and %d (opos=%.2f)"
2010 " snapped to (%.2f) (%.2f)\n",
2011 edge-edges, edge->opos / 64.0,
2012 edge2-edges, edge2->opos / 64.0,
2013 edge->pos / 64.0, edge2->pos / 64.0 ));
2016 edge->flags |= AF_EDGE_DONE;
2018 af_latin2_align_linked_edge( hints, dim, edge, edge2 );
2020 edge2->flags |= AF_EDGE_DONE;
2022 anchor_drift = ( ( anchor->pos - anchor->opos ) +
2023 ( edge2->pos - edge2->opos ) ) >> 1;
2025 FT_TRACE5(( "DRIFT: %.2f\n", anchor_drift/64.0 ));
2029 FT_Pos org_pos, org_len, org_center, cur_center, cur_len;
2030 FT_Pos org_left, org_right;
2033 org_pos = edge->opos + anchor_drift;
2034 org_len = edge2->opos - edge->opos;
2035 org_center = org_pos + ( org_len >> 1 );
2037 cur_len = af_latin2_compute_stem_width( hints, dim, org_len,
2041 org_left = org_pos + ( ( org_len - cur_len ) >> 1 );
2042 org_right = org_pos + ( ( org_len + cur_len ) >> 1 );
2044 FT_TRACE5(( "ALIGN: left=%.2f right=%.2f ",
2045 org_left / 64.0, org_right / 64.0 ));
2046 cur_center = org_center;
2048 if ( edge2->flags & AF_EDGE_DONE )
2050 FT_TRACE5(( "\n" ));
2051 edge->pos = edge2->pos - cur_len;
2055 /* we want to compare several displacement, and choose
2056 * the one that increases fitness while minimizing
2057 * distortion as well
2059 FT_Pos displacements[6], scores[6], org, fit, delta;
2062 /* note: don't even try to fit tiny stems */
2065 FT_TRACE5(( "tiny stem\n" ));
2069 /* if the span is within a single pixel, don't touch it */
2070 if ( FT_PIX_FLOOR( org_left ) == FT_PIX_CEIL( org_right ) )
2072 FT_TRACE5(( "single pixel stem\n" ));
2076 if ( cur_len <= 96 )
2078 /* we want to avoid the absolute worst case which is
2079 * when the left and right edges of the span each represent
2080 * about 50% of the gray. we'd better want to change this
2081 * to 25/75%, since this is much more pleasant to the eye with
2082 * very acceptable distortion
2084 FT_Pos frac_left = org_left & 63;
2085 FT_Pos frac_right = org_right & 63;
2087 if ( frac_left >= 22 && frac_left <= 42 &&
2088 frac_right >= 22 && frac_right <= 42 )
2091 fit = ( org <= 32 ) ? 16 : 48;
2092 delta = FT_ABS( fit - org );
2093 displacements[count] = fit - org;
2094 scores[count++] = delta;
2095 FT_TRACE5(( "dispA=%.2f (%d) ", ( fit - org ) / 64.0, delta ));
2098 fit = ( org <= 32 ) ? 16 : 48;
2099 delta = FT_ABS( fit - org );
2100 displacements[count] = fit - org;
2101 scores[count++] = delta;
2102 FT_TRACE5(( "dispB=%.2f (%d) ", ( fit - org ) / 64.0, delta ));
2106 /* snapping the left edge to the grid */
2108 fit = FT_PIX_ROUND( org );
2109 delta = FT_ABS( fit - org );
2110 displacements[count] = fit - org;
2111 scores[count++] = delta;
2112 FT_TRACE5(( "dispC=%.2f (%d) ", ( fit - org ) / 64.0, delta ));
2114 /* snapping the right edge to the grid */
2116 fit = FT_PIX_ROUND( org );
2117 delta = FT_ABS( fit - org );
2118 displacements[count] = fit - org;
2119 scores[count++] = delta;
2120 FT_TRACE5(( "dispD=%.2f (%d) ", ( fit - org ) / 64.0, delta ));
2122 /* now find the best displacement */
2124 FT_Pos best_score = scores[0];
2125 FT_Pos best_disp = displacements[0];
2128 for ( nn = 1; nn < count; nn++ )
2130 if ( scores[nn] < best_score )
2132 best_score = scores[nn];
2133 best_disp = displacements[nn];
2137 cur_center = org_center + best_disp;
2139 FT_TRACE5(( "\n" ));
2143 edge->pos = cur_center - ( cur_len >> 1 );
2144 edge2->pos = edge->pos + cur_len;
2146 FT_TRACE5(( "STEM1: %d (opos=%.2f) to %d (opos=%.2f)"
2147 " snapped to (%.2f) and (%.2f),"
2148 " org_len=%.2f cur_len=%.2f\n",
2149 edge-edges, edge->opos / 64.0,
2150 edge2-edges, edge2->opos / 64.0,
2151 edge->pos / 64.0, edge2->pos / 64.0,
2152 org_len / 64.0, cur_len / 64.0 ));
2154 edge->flags |= AF_EDGE_DONE;
2155 edge2->flags |= AF_EDGE_DONE;
2157 if ( edge > edges && edge->pos < edge[-1].pos )
2159 FT_TRACE5(( "BOUND: %d (pos=%.2f) to (%.2f)\n",
2160 edge-edges, edge->pos / 64.0, edge[-1].pos / 64.0 ));
2161 edge->pos = edge[-1].pos;
2166 /* make sure that lowercase m's maintain their symmetry */
2168 /* In general, lowercase m's have six vertical edges if they are sans */
2169 /* serif, or twelve if they are with serifs. This implementation is */
2170 /* based on that assumption, and seems to work very well with most */
2171 /* faces. However, if for a certain face this assumption is not */
2172 /* true, the m is just rendered like before. In addition, any stem */
2173 /* correction will only be applied to symmetrical glyphs (even if the */
2174 /* glyph is not an m), so the potential for unwanted distortion is */
2175 /* relatively low. */
2177 /* We don't handle horizontal edges since we can't easily assure that */
2178 /* the third (lowest) stem aligns with the base line; it might end up */
2179 /* one pixel higher or lower. */
2183 FT_Int n_edges = edge_limit - edges;
2186 if ( dim == AF_DIMENSION_HORZ && ( n_edges == 6 || n_edges == 12 ) )
2188 AF_Edge edge1, edge2, edge3;
2189 FT_Pos dist1, dist2, span, delta;
2205 dist1 = edge2->opos - edge1->opos;
2206 dist2 = edge3->opos - edge2->opos;
2208 span = dist1 - dist2;
2214 delta = edge3->pos - ( 2 * edge2->pos - edge1->pos );
2215 edge3->pos -= delta;
2217 edge3->link->pos -= delta;
2219 /* move the serifs along with the stem */
2220 if ( n_edges == 12 )
2222 ( edges + 8 )->pos -= delta;
2223 ( edges + 11 )->pos -= delta;
2226 edge3->flags |= AF_EDGE_DONE;
2228 edge3->link->flags |= AF_EDGE_DONE;
2234 if ( has_serifs || !anchor )
2237 * now hint the remaining edges (serifs and single) in order
2238 * to complete our processing
2240 for ( edge = edges; edge < edge_limit; edge++ )
2245 if ( edge->flags & AF_EDGE_DONE )
2252 delta = edge->serif->opos - edge->opos;
2257 if ( delta < 64 + 16 )
2259 af_latin2_align_serif_edge( hints, edge->serif, edge );
2260 FT_TRACE5(( "SERIF: edge %d (opos=%.2f) serif to %d (opos=%.2f)"
2261 " aligned to (%.2f)\n",
2262 edge-edges, edge->opos / 64.0,
2263 edge->serif - edges, edge->serif->opos / 64.0,
2264 edge->pos / 64.0 ));
2268 FT_TRACE5(( "SERIF_ANCHOR: edge %d (opos=%.2f)"
2269 " snapped to (%.2f)\n",
2270 edge-edges, edge->opos / 64.0, edge->pos / 64.0 ));
2271 edge->pos = FT_PIX_ROUND( edge->opos );
2276 AF_Edge before, after;
2279 for ( before = edge - 1; before >= edges; before-- )
2280 if ( before->flags & AF_EDGE_DONE )
2283 for ( after = edge + 1; after < edge_limit; after++ )
2284 if ( after->flags & AF_EDGE_DONE )
2287 if ( before >= edges && before < edge &&
2288 after < edge_limit && after > edge )
2290 if ( after->opos == before->opos )
2291 edge->pos = before->pos;
2293 edge->pos = before->pos +
2294 FT_MulDiv( edge->opos - before->opos,
2295 after->pos - before->pos,
2296 after->opos - before->opos );
2297 FT_TRACE5(( "SERIF_LINK1: edge %d (opos=%.2f) snapped to (%.2f)"
2298 " from %d (opos=%.2f)\n",
2299 edge-edges, edge->opos / 64.0, edge->pos / 64.0,
2300 before - edges, before->opos / 64.0 ));
2304 edge->pos = anchor->pos +
2305 ( ( edge->opos - anchor->opos + 16 ) & ~31 );
2307 FT_TRACE5(( "SERIF_LINK2: edge %d (opos=%.2f)"
2308 " snapped to (%.2f)\n",
2309 edge-edges, edge->opos / 64.0, edge->pos / 64.0 ));
2313 edge->flags |= AF_EDGE_DONE;
2315 if ( edge > edges && edge->pos < edge[-1].pos )
2316 edge->pos = edge[-1].pos;
2318 if ( edge + 1 < edge_limit &&
2319 edge[1].flags & AF_EDGE_DONE &&
2320 edge->pos > edge[1].pos )
2321 edge->pos = edge[1].pos;
2328 af_latin2_hints_apply( FT_UInt glyph_index,
2329 AF_GlyphHints hints,
2330 FT_Outline* outline,
2331 AF_LatinMetrics metrics )
2336 FT_UNUSED( glyph_index );
2339 error = af_glyph_hints_reload( hints, outline );
2343 /* analyze glyph outline */
2344 if ( AF_HINTS_DO_HORIZONTAL( hints ) )
2346 error = af_latin2_hints_detect_features( hints, AF_DIMENSION_HORZ );
2351 if ( AF_HINTS_DO_VERTICAL( hints ) )
2353 error = af_latin2_hints_detect_features( hints, AF_DIMENSION_VERT );
2357 af_latin2_hints_compute_blue_edges( hints, metrics );
2360 /* grid-fit the outline */
2361 for ( dim = 0; dim < AF_DIMENSION_MAX; dim++ )
2363 #ifdef AF_CONFIG_OPTION_USE_WARPER
2364 if ( dim == AF_DIMENSION_HORZ &&
2365 metrics->root.scaler.render_mode == FT_RENDER_MODE_NORMAL &&
2366 AF_HINTS_DO_WARP( hints ) )
2368 AF_WarperRec warper;
2373 af_warper_compute( &warper, hints, dim, &scale, &delta );
2374 af_glyph_hints_scale_dim( hints, dim, scale, delta );
2377 #endif /* AF_CONFIG_OPTION_USE_WARPER */
2379 if ( ( dim == AF_DIMENSION_HORZ && AF_HINTS_DO_HORIZONTAL( hints ) ) ||
2380 ( dim == AF_DIMENSION_VERT && AF_HINTS_DO_VERTICAL( hints ) ) )
2382 af_latin2_hint_edges( hints, (AF_Dimension)dim );
2383 af_glyph_hints_align_edge_points( hints, (AF_Dimension)dim );
2384 af_glyph_hints_align_strong_points( hints, (AF_Dimension)dim );
2385 af_glyph_hints_align_weak_points( hints, (AF_Dimension)dim );
2388 af_glyph_hints_save( hints, outline );
2395 /*************************************************************************/
2396 /*************************************************************************/
2398 /***** L A T I N S C R I P T C L A S S *****/
2400 /*************************************************************************/
2401 /*************************************************************************/
2404 AF_DEFINE_WRITING_SYSTEM_CLASS(
2405 af_latin2_writing_system_class,
2407 AF_WRITING_SYSTEM_LATIN2,
2409 sizeof ( AF_LatinMetricsRec ),
2411 (AF_WritingSystem_InitMetricsFunc) af_latin2_metrics_init, /* style_metrics_init */
2412 (AF_WritingSystem_ScaleMetricsFunc)af_latin2_metrics_scale, /* style_metrics_scale */
2413 (AF_WritingSystem_DoneMetricsFunc) NULL, /* style_metrics_done */
2414 (AF_WritingSystem_GetStdWidthsFunc)af_latin2_get_standard_widths, /* style_metrics_getstdw */
2416 (AF_WritingSystem_InitHintsFunc) af_latin2_hints_init, /* style_hints_init */
2417 (AF_WritingSystem_ApplyHintsFunc) af_latin2_hints_apply /* style_hints_apply */
2420 #else /* !FT_OPTION_AUTOFIT2 */
2422 /* ANSI C doesn't like empty source files */
2423 typedef int _af_latin2_dummy;
2425 #endif /* !FT_OPTION_AUTOFIT2 */