mgh: fix for default HDD DMA mode, that wasn't correctly set
[open-ps2-loader.git] / thirdparty / freetype-2.3.12 / src / autofit / aflatin.c
blobe6882d5e7b558cba323b1a64a3fb595a0bee515d
1 /***************************************************************************/
2 /* */
3 /* aflatin.c */
4 /* */
5 /* Auto-fitter hinting routines for latin script (body). */
6 /* */
7 /* Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009 by */
8 /* David Turner, Robert Wilhelm, and Werner Lemberg. */
9 /* */
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. */
15 /* */
16 /***************************************************************************/
19 #include <ft2build.h>
20 #include FT_ADVANCES_H
22 #include "aflatin.h"
23 #include "aferrors.h"
26 #ifdef AF_USE_WARPER
27 #include "afwarp.h"
28 #endif
31 /*************************************************************************/
32 /*************************************************************************/
33 /***** *****/
34 /***** L A T I N G L O B A L M E T R I C S *****/
35 /***** *****/
36 /*************************************************************************/
37 /*************************************************************************/
39 FT_LOCAL_DEF( void )
40 af_latin_metrics_init_widths( AF_LatinMetrics metrics,
41 FT_Face face,
42 FT_ULong charcode )
44 /* scan the array of segments in each direction */
45 AF_GlyphHintsRec hints[1];
48 af_glyph_hints_init( hints, face->memory );
50 metrics->axis[AF_DIMENSION_HORZ].width_count = 0;
51 metrics->axis[AF_DIMENSION_VERT].width_count = 0;
54 FT_Error error;
55 FT_UInt glyph_index;
56 int dim;
57 AF_LatinMetricsRec dummy[1];
58 AF_Scaler scaler = &dummy->root.scaler;
61 glyph_index = FT_Get_Char_Index( face, charcode );
62 if ( glyph_index == 0 )
63 goto Exit;
65 error = FT_Load_Glyph( face, glyph_index, FT_LOAD_NO_SCALE );
66 if ( error || face->glyph->outline.n_points <= 0 )
67 goto Exit;
69 FT_ZERO( dummy );
71 dummy->units_per_em = metrics->units_per_em;
72 scaler->x_scale = scaler->y_scale = 0x10000L;
73 scaler->x_delta = scaler->y_delta = 0;
74 scaler->face = face;
75 scaler->render_mode = FT_RENDER_MODE_NORMAL;
76 scaler->flags = 0;
78 af_glyph_hints_rescale( hints, (AF_ScriptMetrics)dummy );
80 error = af_glyph_hints_reload( hints, &face->glyph->outline, 0 );
81 if ( error )
82 goto Exit;
84 for ( dim = 0; dim < AF_DIMENSION_MAX; dim++ )
86 AF_LatinAxis axis = &metrics->axis[dim];
87 AF_AxisHints axhints = &hints->axis[dim];
88 AF_Segment seg, limit, link;
89 FT_UInt num_widths = 0;
92 error = af_latin_hints_compute_segments( hints,
93 (AF_Dimension)dim );
94 if ( error )
95 goto Exit;
97 af_latin_hints_link_segments( hints,
98 (AF_Dimension)dim );
100 seg = axhints->segments;
101 limit = seg + axhints->num_segments;
103 for ( ; seg < limit; seg++ )
105 link = seg->link;
107 /* we only consider stem segments there! */
108 if ( link && link->link == seg && link > seg )
110 FT_Pos dist;
113 dist = seg->pos - link->pos;
114 if ( dist < 0 )
115 dist = -dist;
117 if ( num_widths < AF_LATIN_MAX_WIDTHS )
118 axis->widths[ num_widths++ ].org = dist;
122 af_sort_widths( num_widths, axis->widths );
123 axis->width_count = num_widths;
126 Exit:
127 for ( dim = 0; dim < AF_DIMENSION_MAX; dim++ )
129 AF_LatinAxis axis = &metrics->axis[dim];
130 FT_Pos stdw;
133 stdw = ( axis->width_count > 0 )
134 ? axis->widths[0].org
135 : AF_LATIN_CONSTANT( metrics, 50 );
137 /* let's try 20% of the smallest width */
138 axis->edge_distance_threshold = stdw / 5;
139 axis->standard_width = stdw;
140 axis->extra_light = 0;
144 af_glyph_hints_done( hints );
149 #define AF_LATIN_MAX_TEST_CHARACTERS 12
152 static const char af_latin_blue_chars[AF_LATIN_MAX_BLUES]
153 [AF_LATIN_MAX_TEST_CHARACTERS + 1] =
155 "THEZOCQS",
156 "HEZLOCUS",
157 "fijkdbh",
158 "xzroesc",
159 "xzroesc",
160 "pqgjy"
164 static void
165 af_latin_metrics_init_blues( AF_LatinMetrics metrics,
166 FT_Face face )
168 FT_Pos flats [AF_LATIN_MAX_TEST_CHARACTERS];
169 FT_Pos rounds[AF_LATIN_MAX_TEST_CHARACTERS];
170 FT_Int num_flats;
171 FT_Int num_rounds;
172 FT_Int bb;
173 AF_LatinBlue blue;
174 FT_Error error;
175 AF_LatinAxis axis = &metrics->axis[AF_DIMENSION_VERT];
176 FT_GlyphSlot glyph = face->glyph;
179 /* we compute the blues simply by loading each character from the */
180 /* 'af_latin_blue_chars[blues]' string, then compute its top-most or */
181 /* bottom-most points (depending on `AF_IS_TOP_BLUE') */
183 AF_LOG(( "blue zones computation\n" ));
184 AF_LOG(( "------------------------------------------------\n" ));
186 for ( bb = 0; bb < AF_LATIN_BLUE_MAX; bb++ )
188 const char* p = af_latin_blue_chars[bb];
189 const char* limit = p + AF_LATIN_MAX_TEST_CHARACTERS;
190 FT_Pos* blue_ref;
191 FT_Pos* blue_shoot;
194 AF_LOG(( "blue %3d: ", bb ));
196 num_flats = 0;
197 num_rounds = 0;
199 for ( ; p < limit && *p; p++ )
201 FT_UInt glyph_index;
202 FT_Pos best_y; /* same as points.y */
203 FT_Int best_point, best_first, best_last;
204 FT_Vector* points;
205 FT_Bool round = 0;
208 AF_LOG(( "'%c'", *p ));
210 /* load the character in the face -- skip unknown or empty ones */
211 glyph_index = FT_Get_Char_Index( face, (FT_UInt)*p );
212 if ( glyph_index == 0 )
213 continue;
215 error = FT_Load_Glyph( face, glyph_index, FT_LOAD_NO_SCALE );
216 if ( error || glyph->outline.n_points <= 0 )
217 continue;
219 /* now compute min or max point indices and coordinates */
220 points = glyph->outline.points;
221 best_point = -1;
222 best_y = 0; /* make compiler happy */
223 best_first = 0; /* ditto */
224 best_last = 0; /* ditto */
227 FT_Int nn;
228 FT_Int first = 0;
229 FT_Int last = -1;
232 for ( nn = 0; nn < glyph->outline.n_contours; first = last+1, nn++ )
234 FT_Int old_best_point = best_point;
235 FT_Int pp;
238 last = glyph->outline.contours[nn];
240 /* Avoid single-point contours since they are never rasterized. */
241 /* In some fonts, they correspond to mark attachment points */
242 /* which are way outside of the glyph's real outline. */
243 if ( last <= first )
244 continue;
246 if ( AF_LATIN_IS_TOP_BLUE( bb ) )
248 for ( pp = first; pp <= last; pp++ )
249 if ( best_point < 0 || points[pp].y > best_y )
251 best_point = pp;
252 best_y = points[pp].y;
255 else
257 for ( pp = first; pp <= last; pp++ )
258 if ( best_point < 0 || points[pp].y < best_y )
260 best_point = pp;
261 best_y = points[pp].y;
265 if ( best_point != old_best_point )
267 best_first = first;
268 best_last = last;
271 AF_LOG(( "%5d", best_y ));
274 /* now check whether the point belongs to a straight or round */
275 /* segment; we first need to find in which contour the extremum */
276 /* lies, then inspect its previous and next points */
277 if ( best_point >= 0 )
279 FT_Int prev, next;
280 FT_Pos dist;
283 /* now look for the previous and next points that are not on the */
284 /* same Y coordinate. Threshold the `closeness'... */
285 prev = best_point;
286 next = prev;
290 if ( prev > best_first )
291 prev--;
292 else
293 prev = best_last;
295 dist = points[prev].y - best_y;
296 if ( dist < -5 || dist > 5 )
297 break;
299 } while ( prev != best_point );
303 if ( next < best_last )
304 next++;
305 else
306 next = best_first;
308 dist = points[next].y - best_y;
309 if ( dist < -5 || dist > 5 )
310 break;
312 } while ( next != best_point );
314 /* now, set the `round' flag depending on the segment's kind */
315 round = FT_BOOL(
316 FT_CURVE_TAG( glyph->outline.tags[prev] ) != FT_CURVE_TAG_ON ||
317 FT_CURVE_TAG( glyph->outline.tags[next] ) != FT_CURVE_TAG_ON );
319 AF_LOG(( "%c ", round ? 'r' : 'f' ));
322 if ( round )
323 rounds[num_rounds++] = best_y;
324 else
325 flats[num_flats++] = best_y;
328 AF_LOG(( "\n" ));
330 if ( num_flats == 0 && num_rounds == 0 )
333 * we couldn't find a single glyph to compute this blue zone,
334 * we will simply ignore it then
336 AF_LOG(( "empty\n" ));
337 continue;
340 /* we have computed the contents of the `rounds' and `flats' tables, */
341 /* now determine the reference and overshoot position of the blue -- */
342 /* we simply take the median value after a simple sort */
343 af_sort_pos( num_rounds, rounds );
344 af_sort_pos( num_flats, flats );
346 blue = & axis->blues[axis->blue_count];
347 blue_ref = & blue->ref.org;
348 blue_shoot = & blue->shoot.org;
350 axis->blue_count++;
352 if ( num_flats == 0 )
354 *blue_ref =
355 *blue_shoot = rounds[num_rounds / 2];
357 else if ( num_rounds == 0 )
359 *blue_ref =
360 *blue_shoot = flats[num_flats / 2];
362 else
364 *blue_ref = flats[num_flats / 2];
365 *blue_shoot = rounds[num_rounds / 2];
368 /* there are sometimes problems: if the overshoot position of top */
369 /* zones is under its reference position, or the opposite for bottom */
370 /* zones. We must thus check everything there and correct the errors */
371 if ( *blue_shoot != *blue_ref )
373 FT_Pos ref = *blue_ref;
374 FT_Pos shoot = *blue_shoot;
375 FT_Bool over_ref = FT_BOOL( shoot > ref );
378 if ( AF_LATIN_IS_TOP_BLUE( bb ) ^ over_ref )
379 *blue_shoot = *blue_ref = ( shoot + ref ) / 2;
382 blue->flags = 0;
383 if ( AF_LATIN_IS_TOP_BLUE( bb ) )
384 blue->flags |= AF_LATIN_BLUE_TOP;
387 * The following flag is used later to adjust the y and x scales
388 * in order to optimize the pixel grid alignment of the top of small
389 * letters.
391 if ( bb == AF_LATIN_BLUE_SMALL_TOP )
392 blue->flags |= AF_LATIN_BLUE_ADJUSTMENT;
394 AF_LOG(( "-- ref = %ld, shoot = %ld\n", *blue_ref, *blue_shoot ));
397 return;
401 FT_LOCAL_DEF( void )
402 af_latin_metrics_check_digits( AF_LatinMetrics metrics,
403 FT_Face face )
405 FT_UInt i;
406 FT_Bool started = 0, same_width = 1;
407 FT_Fixed advance, old_advance = 0;
410 /* check whether all ASCII digits have the same advance width; */
411 /* digit `0' is 0x30 in all supported charmaps */
412 for ( i = 0x30; i <= 0x39; i++ )
414 FT_UInt glyph_index;
417 glyph_index = FT_Get_Char_Index( face, i );
418 if ( glyph_index == 0 )
419 continue;
421 if ( FT_Get_Advance( face, glyph_index,
422 FT_LOAD_NO_SCALE |
423 FT_LOAD_NO_HINTING |
424 FT_LOAD_IGNORE_TRANSFORM,
425 &advance ) )
426 continue;
428 if ( started )
430 if ( advance != old_advance )
432 same_width = 0;
433 break;
436 else
438 old_advance = advance;
439 started = 1;
443 metrics->root.digits_have_same_width = same_width;
447 FT_LOCAL_DEF( FT_Error )
448 af_latin_metrics_init( AF_LatinMetrics metrics,
449 FT_Face face )
451 FT_Error error = AF_Err_Ok;
452 FT_CharMap oldmap = face->charmap;
453 FT_UInt ee;
455 static const FT_Encoding latin_encodings[] =
457 FT_ENCODING_UNICODE,
458 FT_ENCODING_APPLE_ROMAN,
459 FT_ENCODING_ADOBE_STANDARD,
460 FT_ENCODING_ADOBE_LATIN_1,
461 FT_ENCODING_NONE /* end of list */
465 metrics->units_per_em = face->units_per_EM;
467 /* do we have a latin charmap in there? */
468 for ( ee = 0; latin_encodings[ee] != FT_ENCODING_NONE; ee++ )
470 error = FT_Select_Charmap( face, latin_encodings[ee] );
471 if ( !error )
472 break;
475 if ( !error )
477 /* For now, compute the standard width and height from the `o'. */
478 af_latin_metrics_init_widths( metrics, face, 'o' );
479 af_latin_metrics_init_blues( metrics, face );
480 af_latin_metrics_check_digits( metrics, face );
483 FT_Set_Charmap( face, oldmap );
484 return AF_Err_Ok;
488 static void
489 af_latin_metrics_scale_dim( AF_LatinMetrics metrics,
490 AF_Scaler scaler,
491 AF_Dimension dim )
493 FT_Fixed scale;
494 FT_Pos delta;
495 AF_LatinAxis axis;
496 FT_UInt nn;
499 if ( dim == AF_DIMENSION_HORZ )
501 scale = scaler->x_scale;
502 delta = scaler->x_delta;
504 else
506 scale = scaler->y_scale;
507 delta = scaler->y_delta;
510 axis = &metrics->axis[dim];
512 if ( axis->org_scale == scale && axis->org_delta == delta )
513 return;
515 axis->org_scale = scale;
516 axis->org_delta = delta;
519 * correct X and Y scale to optimize the alignment of the top of small
520 * letters to the pixel grid
523 AF_LatinAxis Axis = &metrics->axis[AF_DIMENSION_VERT];
524 AF_LatinBlue blue = NULL;
527 for ( nn = 0; nn < Axis->blue_count; nn++ )
529 if ( Axis->blues[nn].flags & AF_LATIN_BLUE_ADJUSTMENT )
531 blue = &Axis->blues[nn];
532 break;
536 if ( blue )
538 FT_Pos scaled = FT_MulFix( blue->shoot.org, scaler->y_scale );
539 FT_Pos fitted = ( scaled + 40 ) & ~63;
542 if ( scaled != fitted )
544 #if 0
545 if ( dim == AF_DIMENSION_HORZ )
547 if ( fitted < scaled )
548 scale -= scale / 50; /* scale *= 0.98 */
550 else
551 #endif
552 if ( dim == AF_DIMENSION_VERT )
554 scale = FT_MulDiv( scale, fitted, scaled );
560 axis->scale = scale;
561 axis->delta = delta;
563 if ( dim == AF_DIMENSION_HORZ )
565 metrics->root.scaler.x_scale = scale;
566 metrics->root.scaler.x_delta = delta;
568 else
570 metrics->root.scaler.y_scale = scale;
571 metrics->root.scaler.y_delta = delta;
574 /* scale the standard widths */
575 for ( nn = 0; nn < axis->width_count; nn++ )
577 AF_Width width = axis->widths + nn;
580 width->cur = FT_MulFix( width->org, scale );
581 width->fit = width->cur;
584 /* an extra-light axis corresponds to a standard width that is */
585 /* smaller than 0.75 pixels */
586 axis->extra_light =
587 (FT_Bool)( FT_MulFix( axis->standard_width, scale ) < 32 + 8 );
589 if ( dim == AF_DIMENSION_VERT )
591 /* scale the blue zones */
592 for ( nn = 0; nn < axis->blue_count; nn++ )
594 AF_LatinBlue blue = &axis->blues[nn];
595 FT_Pos dist;
598 blue->ref.cur = FT_MulFix( blue->ref.org, scale ) + delta;
599 blue->ref.fit = blue->ref.cur;
600 blue->shoot.cur = FT_MulFix( blue->shoot.org, scale ) + delta;
601 blue->shoot.fit = blue->shoot.cur;
602 blue->flags &= ~AF_LATIN_BLUE_ACTIVE;
604 /* a blue zone is only active if it is less than 3/4 pixels tall */
605 dist = FT_MulFix( blue->ref.org - blue->shoot.org, scale );
606 if ( dist <= 48 && dist >= -48 )
608 FT_Pos delta1, delta2;
611 delta1 = blue->shoot.org - blue->ref.org;
612 delta2 = delta1;
613 if ( delta1 < 0 )
614 delta2 = -delta2;
616 delta2 = FT_MulFix( delta2, scale );
618 if ( delta2 < 32 )
619 delta2 = 0;
620 else if ( delta2 < 64 )
621 delta2 = 32 + ( ( ( delta2 - 32 ) + 16 ) & ~31 );
622 else
623 delta2 = FT_PIX_ROUND( delta2 );
625 if ( delta1 < 0 )
626 delta2 = -delta2;
628 blue->ref.fit = FT_PIX_ROUND( blue->ref.cur );
629 blue->shoot.fit = blue->ref.fit + delta2;
631 blue->flags |= AF_LATIN_BLUE_ACTIVE;
638 FT_LOCAL_DEF( void )
639 af_latin_metrics_scale( AF_LatinMetrics metrics,
640 AF_Scaler scaler )
642 metrics->root.scaler.render_mode = scaler->render_mode;
643 metrics->root.scaler.face = scaler->face;
645 af_latin_metrics_scale_dim( metrics, scaler, AF_DIMENSION_HORZ );
646 af_latin_metrics_scale_dim( metrics, scaler, AF_DIMENSION_VERT );
650 /*************************************************************************/
651 /*************************************************************************/
652 /***** *****/
653 /***** L A T I N G L Y P H A N A L Y S I S *****/
654 /***** *****/
655 /*************************************************************************/
656 /*************************************************************************/
658 FT_LOCAL_DEF( FT_Error )
659 af_latin_hints_compute_segments( AF_GlyphHints hints,
660 AF_Dimension dim )
662 AF_AxisHints axis = &hints->axis[dim];
663 FT_Memory memory = hints->memory;
664 FT_Error error = AF_Err_Ok;
665 AF_Segment segment = NULL;
666 AF_SegmentRec seg0;
667 AF_Point* contour = hints->contours;
668 AF_Point* contour_limit = contour + hints->num_contours;
669 AF_Direction major_dir, segment_dir;
672 FT_ZERO( &seg0 );
673 seg0.score = 32000;
674 seg0.flags = AF_EDGE_NORMAL;
676 major_dir = (AF_Direction)FT_ABS( axis->major_dir );
677 segment_dir = major_dir;
679 axis->num_segments = 0;
681 /* set up (u,v) in each point */
682 if ( dim == AF_DIMENSION_HORZ )
684 AF_Point point = hints->points;
685 AF_Point limit = point + hints->num_points;
688 for ( ; point < limit; point++ )
690 point->u = point->fx;
691 point->v = point->fy;
694 else
696 AF_Point point = hints->points;
697 AF_Point limit = point + hints->num_points;
700 for ( ; point < limit; point++ )
702 point->u = point->fy;
703 point->v = point->fx;
707 /* do each contour separately */
708 for ( ; contour < contour_limit; contour++ )
710 AF_Point point = contour[0];
711 AF_Point last = point->prev;
712 int on_edge = 0;
713 FT_Pos min_pos = 32000; /* minimum segment pos != min_coord */
714 FT_Pos max_pos = -32000; /* maximum segment pos != max_coord */
715 FT_Bool passed;
718 if ( point == last ) /* skip singletons -- just in case */
719 continue;
721 if ( FT_ABS( last->out_dir ) == major_dir &&
722 FT_ABS( point->out_dir ) == major_dir )
724 /* we are already on an edge, try to locate its start */
725 last = point;
727 for (;;)
729 point = point->prev;
730 if ( FT_ABS( point->out_dir ) != major_dir )
732 point = point->next;
733 break;
735 if ( point == last )
736 break;
740 last = point;
741 passed = 0;
743 for (;;)
745 FT_Pos u, v;
748 if ( on_edge )
750 u = point->u;
751 if ( u < min_pos )
752 min_pos = u;
753 if ( u > max_pos )
754 max_pos = u;
756 if ( point->out_dir != segment_dir || point == last )
758 /* we are just leaving an edge; record a new segment! */
759 segment->last = point;
760 segment->pos = (FT_Short)( ( min_pos + max_pos ) >> 1 );
762 /* a segment is round if either its first or last point */
763 /* is a control point */
764 if ( ( segment->first->flags | point->flags ) &
765 AF_FLAG_CONTROL )
766 segment->flags |= AF_EDGE_ROUND;
768 /* compute segment size */
769 min_pos = max_pos = point->v;
771 v = segment->first->v;
772 if ( v < min_pos )
773 min_pos = v;
774 if ( v > max_pos )
775 max_pos = v;
777 segment->min_coord = (FT_Short)min_pos;
778 segment->max_coord = (FT_Short)max_pos;
779 segment->height = (FT_Short)( segment->max_coord -
780 segment->min_coord );
782 on_edge = 0;
783 segment = NULL;
784 /* fallthrough */
788 /* now exit if we are at the start/end point */
789 if ( point == last )
791 if ( passed )
792 break;
793 passed = 1;
796 if ( !on_edge && FT_ABS( point->out_dir ) == major_dir )
798 /* this is the start of a new segment! */
799 segment_dir = (AF_Direction)point->out_dir;
801 /* clear all segment fields */
802 error = af_axis_hints_new_segment( axis, memory, &segment );
803 if ( error )
804 goto Exit;
806 segment[0] = seg0;
807 segment->dir = (FT_Char)segment_dir;
808 min_pos = max_pos = point->u;
809 segment->first = point;
810 segment->last = point;
811 segment->contour = contour;
812 on_edge = 1;
815 point = point->next;
818 } /* contours */
821 /* now slightly increase the height of segments when this makes */
822 /* sense -- this is used to better detect and ignore serifs */
824 AF_Segment segments = axis->segments;
825 AF_Segment segments_end = segments + axis->num_segments;
828 for ( segment = segments; segment < segments_end; segment++ )
830 AF_Point first = segment->first;
831 AF_Point last = segment->last;
832 FT_Pos first_v = first->v;
833 FT_Pos last_v = last->v;
836 if ( first == last )
837 continue;
839 if ( first_v < last_v )
841 AF_Point p;
844 p = first->prev;
845 if ( p->v < first_v )
846 segment->height = (FT_Short)( segment->height +
847 ( ( first_v - p->v ) >> 1 ) );
849 p = last->next;
850 if ( p->v > last_v )
851 segment->height = (FT_Short)( segment->height +
852 ( ( p->v - last_v ) >> 1 ) );
854 else
856 AF_Point p;
859 p = first->prev;
860 if ( p->v > first_v )
861 segment->height = (FT_Short)( segment->height +
862 ( ( p->v - first_v ) >> 1 ) );
864 p = last->next;
865 if ( p->v < last_v )
866 segment->height = (FT_Short)( segment->height +
867 ( ( last_v - p->v ) >> 1 ) );
872 Exit:
873 return error;
877 FT_LOCAL_DEF( void )
878 af_latin_hints_link_segments( AF_GlyphHints hints,
879 AF_Dimension dim )
881 AF_AxisHints axis = &hints->axis[dim];
882 AF_Segment segments = axis->segments;
883 AF_Segment segment_limit = segments + axis->num_segments;
884 FT_Pos len_threshold, len_score;
885 AF_Segment seg1, seg2;
888 len_threshold = AF_LATIN_CONSTANT( hints->metrics, 8 );
889 if ( len_threshold == 0 )
890 len_threshold = 1;
892 len_score = AF_LATIN_CONSTANT( hints->metrics, 6000 );
894 /* now compare each segment to the others */
895 for ( seg1 = segments; seg1 < segment_limit; seg1++ )
897 /* the fake segments are introduced to hint the metrics -- */
898 /* we must never link them to anything */
899 if ( seg1->dir != axis->major_dir || seg1->first == seg1->last )
900 continue;
902 for ( seg2 = segments; seg2 < segment_limit; seg2++ )
903 if ( seg1->dir + seg2->dir == 0 && seg2->pos > seg1->pos )
905 FT_Pos pos1 = seg1->pos;
906 FT_Pos pos2 = seg2->pos;
907 FT_Pos dist = pos2 - pos1;
910 if ( dist < 0 )
911 dist = -dist;
914 FT_Pos min = seg1->min_coord;
915 FT_Pos max = seg1->max_coord;
916 FT_Pos len, score;
919 if ( min < seg2->min_coord )
920 min = seg2->min_coord;
922 if ( max > seg2->max_coord )
923 max = seg2->max_coord;
925 len = max - min;
926 if ( len >= len_threshold )
928 score = dist + len_score / len;
930 if ( score < seg1->score )
932 seg1->score = score;
933 seg1->link = seg2;
936 if ( score < seg2->score )
938 seg2->score = score;
939 seg2->link = seg1;
946 /* now, compute the `serif' segments */
947 for ( seg1 = segments; seg1 < segment_limit; seg1++ )
949 seg2 = seg1->link;
951 if ( seg2 )
953 if ( seg2->link != seg1 )
955 seg1->link = 0;
956 seg1->serif = seg2->link;
963 FT_LOCAL_DEF( FT_Error )
964 af_latin_hints_compute_edges( AF_GlyphHints hints,
965 AF_Dimension dim )
967 AF_AxisHints axis = &hints->axis[dim];
968 FT_Error error = AF_Err_Ok;
969 FT_Memory memory = hints->memory;
970 AF_LatinAxis laxis = &((AF_LatinMetrics)hints->metrics)->axis[dim];
972 AF_Segment segments = axis->segments;
973 AF_Segment segment_limit = segments + axis->num_segments;
974 AF_Segment seg;
976 AF_Direction up_dir;
977 FT_Fixed scale;
978 FT_Pos edge_distance_threshold;
979 FT_Pos segment_length_threshold;
982 axis->num_edges = 0;
984 scale = ( dim == AF_DIMENSION_HORZ ) ? hints->x_scale
985 : hints->y_scale;
987 up_dir = ( dim == AF_DIMENSION_HORZ ) ? AF_DIR_UP
988 : AF_DIR_RIGHT;
991 * We ignore all segments that are less than 1 pixels in length,
992 * to avoid many problems with serif fonts. We compute the
993 * corresponding threshold in font units.
995 if ( dim == AF_DIMENSION_HORZ )
996 segment_length_threshold = FT_DivFix( 64, hints->y_scale );
997 else
998 segment_length_threshold = 0;
1000 /*********************************************************************/
1001 /* */
1002 /* We will begin by generating a sorted table of edges for the */
1003 /* current direction. To do so, we simply scan each segment and try */
1004 /* to find an edge in our table that corresponds to its position. */
1005 /* */
1006 /* If no edge is found, we create and insert a new edge in the */
1007 /* sorted table. Otherwise, we simply add the segment to the edge's */
1008 /* list which will be processed in the second step to compute the */
1009 /* edge's properties. */
1010 /* */
1011 /* Note that the edges table is sorted along the segment/edge */
1012 /* position. */
1013 /* */
1014 /*********************************************************************/
1016 edge_distance_threshold = FT_MulFix( laxis->edge_distance_threshold,
1017 scale );
1018 if ( edge_distance_threshold > 64 / 4 )
1019 edge_distance_threshold = 64 / 4;
1021 edge_distance_threshold = FT_DivFix( edge_distance_threshold,
1022 scale );
1024 for ( seg = segments; seg < segment_limit; seg++ )
1026 AF_Edge found = 0;
1027 FT_Int ee;
1030 if ( seg->height < segment_length_threshold )
1031 continue;
1033 /* A special case for serif edges: If they are smaller than */
1034 /* 1.5 pixels we ignore them. */
1035 if ( seg->serif &&
1036 2 * seg->height < 3 * segment_length_threshold )
1037 continue;
1039 /* look for an edge corresponding to the segment */
1040 for ( ee = 0; ee < axis->num_edges; ee++ )
1042 AF_Edge edge = axis->edges + ee;
1043 FT_Pos dist;
1046 dist = seg->pos - edge->fpos;
1047 if ( dist < 0 )
1048 dist = -dist;
1050 if ( dist < edge_distance_threshold && edge->dir == seg->dir )
1052 found = edge;
1053 break;
1057 if ( !found )
1059 AF_Edge edge;
1062 /* insert a new edge in the list and */
1063 /* sort according to the position */
1064 error = af_axis_hints_new_edge( axis, seg->pos,
1065 (AF_Direction)seg->dir,
1066 memory, &edge );
1067 if ( error )
1068 goto Exit;
1070 /* add the segment to the new edge's list */
1071 FT_ZERO( edge );
1073 edge->first = seg;
1074 edge->last = seg;
1075 edge->fpos = seg->pos;
1076 edge->dir = seg->dir;
1077 edge->opos = edge->pos = FT_MulFix( seg->pos, scale );
1078 seg->edge_next = seg;
1080 else
1082 /* if an edge was found, simply add the segment to the edge's */
1083 /* list */
1084 seg->edge_next = found->first;
1085 found->last->edge_next = seg;
1086 found->last = seg;
1091 /*********************************************************************/
1092 /* */
1093 /* Good, we will now compute each edge's properties according to */
1094 /* segments found on its position. Basically, these are: */
1095 /* */
1096 /* - edge's main direction */
1097 /* - stem edge, serif edge or both (which defaults to stem then) */
1098 /* - rounded edge, straight or both (which defaults to straight) */
1099 /* - link for edge */
1100 /* */
1101 /*********************************************************************/
1103 /* first of all, set the `edge' field in each segment -- this is */
1104 /* required in order to compute edge links */
1107 * Note that removing this loop and setting the `edge' field of each
1108 * segment directly in the code above slows down execution speed for
1109 * some reasons on platforms like the Sun.
1112 AF_Edge edges = axis->edges;
1113 AF_Edge edge_limit = edges + axis->num_edges;
1114 AF_Edge edge;
1117 for ( edge = edges; edge < edge_limit; edge++ )
1119 seg = edge->first;
1120 if ( seg )
1123 seg->edge = edge;
1124 seg = seg->edge_next;
1126 } while ( seg != edge->first );
1129 /* now, compute each edge properties */
1130 for ( edge = edges; edge < edge_limit; edge++ )
1132 FT_Int is_round = 0; /* does it contain round segments? */
1133 FT_Int is_straight = 0; /* does it contain straight segments? */
1134 FT_Pos ups = 0; /* number of upwards segments */
1135 FT_Pos downs = 0; /* number of downwards segments */
1138 seg = edge->first;
1142 FT_Bool is_serif;
1145 /* check for roundness of segment */
1146 if ( seg->flags & AF_EDGE_ROUND )
1147 is_round++;
1148 else
1149 is_straight++;
1151 /* check for segment direction */
1152 if ( seg->dir == up_dir )
1153 ups += seg->max_coord-seg->min_coord;
1154 else
1155 downs += seg->max_coord-seg->min_coord;
1157 /* check for links -- if seg->serif is set, then seg->link must */
1158 /* be ignored */
1159 is_serif = (FT_Bool)( seg->serif &&
1160 seg->serif->edge &&
1161 seg->serif->edge != edge );
1163 if ( ( seg->link && seg->link->edge != NULL ) || is_serif )
1165 AF_Edge edge2;
1166 AF_Segment seg2;
1169 edge2 = edge->link;
1170 seg2 = seg->link;
1172 if ( is_serif )
1174 seg2 = seg->serif;
1175 edge2 = edge->serif;
1178 if ( edge2 )
1180 FT_Pos edge_delta;
1181 FT_Pos seg_delta;
1184 edge_delta = edge->fpos - edge2->fpos;
1185 if ( edge_delta < 0 )
1186 edge_delta = -edge_delta;
1188 seg_delta = seg->pos - seg2->pos;
1189 if ( seg_delta < 0 )
1190 seg_delta = -seg_delta;
1192 if ( seg_delta < edge_delta )
1193 edge2 = seg2->edge;
1195 else
1196 edge2 = seg2->edge;
1198 if ( is_serif )
1200 edge->serif = edge2;
1201 edge2->flags |= AF_EDGE_SERIF;
1203 else
1204 edge->link = edge2;
1207 seg = seg->edge_next;
1209 } while ( seg != edge->first );
1211 /* set the round/straight flags */
1212 edge->flags = AF_EDGE_NORMAL;
1214 if ( is_round > 0 && is_round >= is_straight )
1215 edge->flags |= AF_EDGE_ROUND;
1217 #if 0
1218 /* set the edge's main direction */
1219 edge->dir = AF_DIR_NONE;
1221 if ( ups > downs )
1222 edge->dir = (FT_Char)up_dir;
1224 else if ( ups < downs )
1225 edge->dir = (FT_Char)-up_dir;
1227 else if ( ups == downs )
1228 edge->dir = 0; /* both up and down! */
1229 #endif
1231 /* gets rid of serifs if link is set */
1232 /* XXX: This gets rid of many unpleasant artefacts! */
1233 /* Example: the `c' in cour.pfa at size 13 */
1235 if ( edge->serif && edge->link )
1236 edge->serif = 0;
1240 Exit:
1241 return error;
1245 FT_LOCAL_DEF( FT_Error )
1246 af_latin_hints_detect_features( AF_GlyphHints hints,
1247 AF_Dimension dim )
1249 FT_Error error;
1252 error = af_latin_hints_compute_segments( hints, dim );
1253 if ( !error )
1255 af_latin_hints_link_segments( hints, dim );
1257 error = af_latin_hints_compute_edges( hints, dim );
1259 return error;
1263 FT_LOCAL_DEF( void )
1264 af_latin_hints_compute_blue_edges( AF_GlyphHints hints,
1265 AF_LatinMetrics metrics )
1267 AF_AxisHints axis = &hints->axis[ AF_DIMENSION_VERT ];
1268 AF_Edge edge = axis->edges;
1269 AF_Edge edge_limit = edge + axis->num_edges;
1270 AF_LatinAxis latin = &metrics->axis[ AF_DIMENSION_VERT ];
1271 FT_Fixed scale = latin->scale;
1274 /* compute which blue zones are active, i.e. have their scaled */
1275 /* size < 3/4 pixels */
1277 /* for each horizontal edge search the blue zone which is closest */
1278 for ( ; edge < edge_limit; edge++ )
1280 FT_Int bb;
1281 AF_Width best_blue = NULL;
1282 FT_Pos best_dist; /* initial threshold */
1285 /* compute the initial threshold as a fraction of the EM size */
1286 best_dist = FT_MulFix( metrics->units_per_em / 40, scale );
1288 if ( best_dist > 64 / 2 )
1289 best_dist = 64 / 2;
1291 for ( bb = 0; bb < AF_LATIN_BLUE_MAX; bb++ )
1293 AF_LatinBlue blue = latin->blues + bb;
1294 FT_Bool is_top_blue, is_major_dir;
1297 /* skip inactive blue zones (i.e., those that are too small) */
1298 if ( !( blue->flags & AF_LATIN_BLUE_ACTIVE ) )
1299 continue;
1301 /* if it is a top zone, check for right edges -- if it is a bottom */
1302 /* zone, check for left edges */
1303 /* */
1304 /* of course, that's for TrueType */
1305 is_top_blue = (FT_Byte)( ( blue->flags & AF_LATIN_BLUE_TOP ) != 0 );
1306 is_major_dir = FT_BOOL( edge->dir == axis->major_dir );
1308 /* if it is a top zone, the edge must be against the major */
1309 /* direction; if it is a bottom zone, it must be in the major */
1310 /* direction */
1311 if ( is_top_blue ^ is_major_dir )
1313 FT_Pos dist;
1316 /* first of all, compare it to the reference position */
1317 dist = edge->fpos - blue->ref.org;
1318 if ( dist < 0 )
1319 dist = -dist;
1321 dist = FT_MulFix( dist, scale );
1322 if ( dist < best_dist )
1324 best_dist = dist;
1325 best_blue = & blue->ref;
1328 /* now, compare it to the overshoot position if the edge is */
1329 /* rounded, and if the edge is over the reference position of a */
1330 /* top zone, or under the reference position of a bottom zone */
1331 if ( edge->flags & AF_EDGE_ROUND && dist != 0 )
1333 FT_Bool is_under_ref = FT_BOOL( edge->fpos < blue->ref.org );
1336 if ( is_top_blue ^ is_under_ref )
1338 blue = latin->blues + bb;
1339 dist = edge->fpos - blue->shoot.org;
1340 if ( dist < 0 )
1341 dist = -dist;
1343 dist = FT_MulFix( dist, scale );
1344 if ( dist < best_dist )
1346 best_dist = dist;
1347 best_blue = & blue->shoot;
1354 if ( best_blue )
1355 edge->blue_edge = best_blue;
1360 static FT_Error
1361 af_latin_hints_init( AF_GlyphHints hints,
1362 AF_LatinMetrics metrics )
1364 FT_Render_Mode mode;
1365 FT_UInt32 scaler_flags, other_flags;
1366 FT_Face face = metrics->root.scaler.face;
1369 af_glyph_hints_rescale( hints, (AF_ScriptMetrics)metrics );
1372 * correct x_scale and y_scale if needed, since they may have
1373 * been modified `af_latin_metrics_scale_dim' above
1375 hints->x_scale = metrics->axis[AF_DIMENSION_HORZ].scale;
1376 hints->x_delta = metrics->axis[AF_DIMENSION_HORZ].delta;
1377 hints->y_scale = metrics->axis[AF_DIMENSION_VERT].scale;
1378 hints->y_delta = metrics->axis[AF_DIMENSION_VERT].delta;
1380 /* compute flags depending on render mode, etc. */
1381 mode = metrics->root.scaler.render_mode;
1383 #if 0 /* #ifdef AF_USE_WARPER */
1384 if ( mode == FT_RENDER_MODE_LCD || mode == FT_RENDER_MODE_LCD_V )
1386 metrics->root.scaler.render_mode = mode = FT_RENDER_MODE_NORMAL;
1388 #endif
1390 scaler_flags = hints->scaler_flags;
1391 other_flags = 0;
1394 * We snap the width of vertical stems for the monochrome and
1395 * horizontal LCD rendering targets only.
1397 if ( mode == FT_RENDER_MODE_MONO || mode == FT_RENDER_MODE_LCD )
1398 other_flags |= AF_LATIN_HINTS_HORZ_SNAP;
1401 * We snap the width of horizontal stems for the monochrome and
1402 * vertical LCD rendering targets only.
1404 if ( mode == FT_RENDER_MODE_MONO || mode == FT_RENDER_MODE_LCD_V )
1405 other_flags |= AF_LATIN_HINTS_VERT_SNAP;
1408 * We adjust stems to full pixels only if we don't use the `light' mode.
1410 if ( mode != FT_RENDER_MODE_LIGHT )
1411 other_flags |= AF_LATIN_HINTS_STEM_ADJUST;
1413 if ( mode == FT_RENDER_MODE_MONO )
1414 other_flags |= AF_LATIN_HINTS_MONO;
1417 * In `light' hinting mode we disable horizontal hinting completely.
1418 * We also do it if the face is italic.
1420 if ( mode == FT_RENDER_MODE_LIGHT ||
1421 (face->style_flags & FT_STYLE_FLAG_ITALIC) != 0 )
1422 scaler_flags |= AF_SCALER_FLAG_NO_HORIZONTAL;
1424 hints->scaler_flags = scaler_flags;
1425 hints->other_flags = other_flags;
1427 return 0;
1431 /*************************************************************************/
1432 /*************************************************************************/
1433 /***** *****/
1434 /***** L A T I N G L Y P H G R I D - F I T T I N G *****/
1435 /***** *****/
1436 /*************************************************************************/
1437 /*************************************************************************/
1439 /* snap a given width in scaled coordinates to one of the */
1440 /* current standard widths */
1442 static FT_Pos
1443 af_latin_snap_width( AF_Width widths,
1444 FT_Int count,
1445 FT_Pos width )
1447 int n;
1448 FT_Pos best = 64 + 32 + 2;
1449 FT_Pos reference = width;
1450 FT_Pos scaled;
1453 for ( n = 0; n < count; n++ )
1455 FT_Pos w;
1456 FT_Pos dist;
1459 w = widths[n].cur;
1460 dist = width - w;
1461 if ( dist < 0 )
1462 dist = -dist;
1463 if ( dist < best )
1465 best = dist;
1466 reference = w;
1470 scaled = FT_PIX_ROUND( reference );
1472 if ( width >= reference )
1474 if ( width < scaled + 48 )
1475 width = reference;
1477 else
1479 if ( width > scaled - 48 )
1480 width = reference;
1483 return width;
1487 /* compute the snapped width of a given stem */
1489 static FT_Pos
1490 af_latin_compute_stem_width( AF_GlyphHints hints,
1491 AF_Dimension dim,
1492 FT_Pos width,
1493 AF_Edge_Flags base_flags,
1494 AF_Edge_Flags stem_flags )
1496 AF_LatinMetrics metrics = (AF_LatinMetrics) hints->metrics;
1497 AF_LatinAxis axis = & metrics->axis[dim];
1498 FT_Pos dist = width;
1499 FT_Int sign = 0;
1500 FT_Int vertical = ( dim == AF_DIMENSION_VERT );
1503 if ( !AF_LATIN_HINTS_DO_STEM_ADJUST( hints ) ||
1504 axis->extra_light )
1505 return width;
1507 if ( dist < 0 )
1509 dist = -width;
1510 sign = 1;
1513 if ( ( vertical && !AF_LATIN_HINTS_DO_VERT_SNAP( hints ) ) ||
1514 ( !vertical && !AF_LATIN_HINTS_DO_HORZ_SNAP( hints ) ) )
1516 /* smooth hinting process: very lightly quantize the stem width */
1518 /* leave the widths of serifs alone */
1520 if ( ( stem_flags & AF_EDGE_SERIF ) && vertical && ( dist < 3 * 64 ) )
1521 goto Done_Width;
1523 else if ( ( base_flags & AF_EDGE_ROUND ) )
1525 if ( dist < 80 )
1526 dist = 64;
1528 else if ( dist < 56 )
1529 dist = 56;
1531 if ( axis->width_count > 0 )
1533 FT_Pos delta;
1536 /* compare to standard width */
1537 if ( axis->width_count > 0 )
1539 delta = dist - axis->widths[0].cur;
1541 if ( delta < 0 )
1542 delta = -delta;
1544 if ( delta < 40 )
1546 dist = axis->widths[0].cur;
1547 if ( dist < 48 )
1548 dist = 48;
1550 goto Done_Width;
1554 if ( dist < 3 * 64 )
1556 delta = dist & 63;
1557 dist &= -64;
1559 if ( delta < 10 )
1560 dist += delta;
1562 else if ( delta < 32 )
1563 dist += 10;
1565 else if ( delta < 54 )
1566 dist += 54;
1568 else
1569 dist += delta;
1571 else
1572 dist = ( dist + 32 ) & ~63;
1575 else
1577 /* strong hinting process: snap the stem width to integer pixels */
1578 FT_Pos org_dist = dist;
1581 dist = af_latin_snap_width( axis->widths, axis->width_count, dist );
1583 if ( vertical )
1585 /* in the case of vertical hinting, always round */
1586 /* the stem heights to integer pixels */
1588 if ( dist >= 64 )
1589 dist = ( dist + 16 ) & ~63;
1590 else
1591 dist = 64;
1593 else
1595 if ( AF_LATIN_HINTS_DO_MONO( hints ) )
1597 /* monochrome horizontal hinting: snap widths to integer pixels */
1598 /* with a different threshold */
1600 if ( dist < 64 )
1601 dist = 64;
1602 else
1603 dist = ( dist + 32 ) & ~63;
1605 else
1607 /* for horizontal anti-aliased hinting, we adopt a more subtle */
1608 /* approach: we strengthen small stems, round stems whose size */
1609 /* is between 1 and 2 pixels to an integer, otherwise nothing */
1611 if ( dist < 48 )
1612 dist = ( dist + 64 ) >> 1;
1614 else if ( dist < 128 )
1616 /* We only round to an integer width if the corresponding */
1617 /* distortion is less than 1/4 pixel. Otherwise this */
1618 /* makes everything worse since the diagonals, which are */
1619 /* not hinted, appear a lot bolder or thinner than the */
1620 /* vertical stems. */
1622 FT_Pos delta;
1625 dist = ( dist + 22 ) & ~63;
1626 delta = dist - org_dist;
1627 if ( delta < 0 )
1628 delta = -delta;
1630 if (delta >= 16)
1632 dist = org_dist;
1633 if ( dist < 48 )
1634 dist = ( dist + 64 ) >> 1;
1637 else
1638 /* round otherwise to prevent color fringes in LCD mode */
1639 dist = ( dist + 32 ) & ~63;
1644 Done_Width:
1645 if ( sign )
1646 dist = -dist;
1648 return dist;
1652 /* align one stem edge relative to the previous stem edge */
1654 static void
1655 af_latin_align_linked_edge( AF_GlyphHints hints,
1656 AF_Dimension dim,
1657 AF_Edge base_edge,
1658 AF_Edge stem_edge )
1660 FT_Pos dist = stem_edge->opos - base_edge->opos;
1662 FT_Pos fitted_width = af_latin_compute_stem_width(
1663 hints, dim, dist,
1664 (AF_Edge_Flags)base_edge->flags,
1665 (AF_Edge_Flags)stem_edge->flags );
1668 stem_edge->pos = base_edge->pos + fitted_width;
1670 AF_LOG(( "LINK: edge %d (opos=%.2f) linked to (%.2f), "
1671 "dist was %.2f, now %.2f\n",
1672 stem_edge-hints->axis[dim].edges, stem_edge->opos / 64.0,
1673 stem_edge->pos / 64.0, dist / 64.0, fitted_width / 64.0 ));
1677 static void
1678 af_latin_align_serif_edge( AF_GlyphHints hints,
1679 AF_Edge base,
1680 AF_Edge serif )
1682 FT_UNUSED( hints );
1684 serif->pos = base->pos + (serif->opos - base->opos);
1688 /*************************************************************************/
1689 /*************************************************************************/
1690 /*************************************************************************/
1691 /**** ****/
1692 /**** E D G E H I N T I N G ****/
1693 /**** ****/
1694 /*************************************************************************/
1695 /*************************************************************************/
1696 /*************************************************************************/
1699 FT_LOCAL_DEF( void )
1700 af_latin_hint_edges( AF_GlyphHints hints,
1701 AF_Dimension dim )
1703 AF_AxisHints axis = &hints->axis[dim];
1704 AF_Edge edges = axis->edges;
1705 AF_Edge edge_limit = edges + axis->num_edges;
1706 FT_PtrDist n_edges;
1707 AF_Edge edge;
1708 AF_Edge anchor = 0;
1709 FT_Int has_serifs = 0;
1712 /* we begin by aligning all stems relative to the blue zone */
1713 /* if needed -- that's only for horizontal edges */
1715 if ( dim == AF_DIMENSION_VERT && AF_HINTS_DO_BLUES( hints ) )
1717 for ( edge = edges; edge < edge_limit; edge++ )
1719 AF_Width blue;
1720 AF_Edge edge1, edge2;
1723 if ( edge->flags & AF_EDGE_DONE )
1724 continue;
1726 blue = edge->blue_edge;
1727 edge1 = NULL;
1728 edge2 = edge->link;
1730 if ( blue )
1732 edge1 = edge;
1734 else if ( edge2 && edge2->blue_edge )
1736 blue = edge2->blue_edge;
1737 edge1 = edge2;
1738 edge2 = edge;
1741 if ( !edge1 )
1742 continue;
1744 AF_LOG(( "BLUE: edge %d (opos=%.2f) snapped to (%.2f), "
1745 "was (%.2f)\n",
1746 edge1-edges, edge1->opos / 64.0, blue->fit / 64.0,
1747 edge1->pos / 64.0 ));
1749 edge1->pos = blue->fit;
1750 edge1->flags |= AF_EDGE_DONE;
1752 if ( edge2 && !edge2->blue_edge )
1754 af_latin_align_linked_edge( hints, dim, edge1, edge2 );
1755 edge2->flags |= AF_EDGE_DONE;
1758 if ( !anchor )
1759 anchor = edge;
1763 /* now we will align all stem edges, trying to maintain the */
1764 /* relative order of stems in the glyph */
1765 for ( edge = edges; edge < edge_limit; edge++ )
1767 AF_Edge edge2;
1770 if ( edge->flags & AF_EDGE_DONE )
1771 continue;
1773 /* skip all non-stem edges */
1774 edge2 = edge->link;
1775 if ( !edge2 )
1777 has_serifs++;
1778 continue;
1781 /* now align the stem */
1783 /* this should not happen, but it's better to be safe */
1784 if ( edge2->blue_edge )
1786 AF_LOG(( "ASSERTION FAILED for edge %d\n", edge2-edges ));
1788 af_latin_align_linked_edge( hints, dim, edge2, edge );
1789 edge->flags |= AF_EDGE_DONE;
1790 continue;
1793 if ( !anchor )
1795 FT_Pos org_len, org_center, cur_len;
1796 FT_Pos cur_pos1, error1, error2, u_off, d_off;
1799 org_len = edge2->opos - edge->opos;
1800 cur_len = af_latin_compute_stem_width(
1801 hints, dim, org_len,
1802 (AF_Edge_Flags)edge->flags,
1803 (AF_Edge_Flags)edge2->flags );
1804 if ( cur_len <= 64 )
1805 u_off = d_off = 32;
1806 else
1808 u_off = 38;
1809 d_off = 26;
1812 if ( cur_len < 96 )
1814 org_center = edge->opos + ( org_len >> 1 );
1816 cur_pos1 = FT_PIX_ROUND( org_center );
1818 error1 = org_center - ( cur_pos1 - u_off );
1819 if ( error1 < 0 )
1820 error1 = -error1;
1822 error2 = org_center - ( cur_pos1 + d_off );
1823 if ( error2 < 0 )
1824 error2 = -error2;
1826 if ( error1 < error2 )
1827 cur_pos1 -= u_off;
1828 else
1829 cur_pos1 += d_off;
1831 edge->pos = cur_pos1 - cur_len / 2;
1832 edge2->pos = edge->pos + cur_len;
1834 else
1835 edge->pos = FT_PIX_ROUND( edge->opos );
1837 AF_LOG(( "ANCHOR: edge %d (opos=%.2f) and %d (opos=%.2f) "
1838 "snapped to (%.2f) (%.2f)\n",
1839 edge-edges, edge->opos / 64.0,
1840 edge2-edges, edge2->opos / 64.0,
1841 edge->pos / 64.0, edge2->pos / 64.0 ));
1842 anchor = edge;
1844 edge->flags |= AF_EDGE_DONE;
1846 af_latin_align_linked_edge( hints, dim, edge, edge2 );
1848 else
1850 FT_Pos org_pos, org_len, org_center, cur_len;
1851 FT_Pos cur_pos1, cur_pos2, delta1, delta2;
1854 org_pos = anchor->pos + ( edge->opos - anchor->opos );
1855 org_len = edge2->opos - edge->opos;
1856 org_center = org_pos + ( org_len >> 1 );
1858 cur_len = af_latin_compute_stem_width(
1859 hints, dim, org_len,
1860 (AF_Edge_Flags)edge->flags,
1861 (AF_Edge_Flags)edge2->flags );
1863 if ( edge2->flags & AF_EDGE_DONE )
1864 edge->pos = edge2->pos - cur_len;
1866 else if ( cur_len < 96 )
1868 FT_Pos u_off, d_off;
1871 cur_pos1 = FT_PIX_ROUND( org_center );
1873 if (cur_len <= 64 )
1874 u_off = d_off = 32;
1875 else
1877 u_off = 38;
1878 d_off = 26;
1881 delta1 = org_center - ( cur_pos1 - u_off );
1882 if ( delta1 < 0 )
1883 delta1 = -delta1;
1885 delta2 = org_center - ( cur_pos1 + d_off );
1886 if ( delta2 < 0 )
1887 delta2 = -delta2;
1889 if ( delta1 < delta2 )
1890 cur_pos1 -= u_off;
1891 else
1892 cur_pos1 += d_off;
1894 edge->pos = cur_pos1 - cur_len / 2;
1895 edge2->pos = cur_pos1 + cur_len / 2;
1897 AF_LOG(( "STEM: %d (opos=%.2f) to %d (opos=%.2f) "
1898 "snapped to (%.2f) and (%.2f)\n",
1899 edge-edges, edge->opos / 64.0,
1900 edge2-edges, edge2->opos / 64.0,
1901 edge->pos / 64.0, edge2->pos / 64.0 ));
1903 else
1905 org_pos = anchor->pos + ( edge->opos - anchor->opos );
1906 org_len = edge2->opos - edge->opos;
1907 org_center = org_pos + ( org_len >> 1 );
1909 cur_len = af_latin_compute_stem_width(
1910 hints, dim, org_len,
1911 (AF_Edge_Flags)edge->flags,
1912 (AF_Edge_Flags)edge2->flags );
1914 cur_pos1 = FT_PIX_ROUND( org_pos );
1915 delta1 = cur_pos1 + ( cur_len >> 1 ) - org_center;
1916 if ( delta1 < 0 )
1917 delta1 = -delta1;
1919 cur_pos2 = FT_PIX_ROUND( org_pos + org_len ) - cur_len;
1920 delta2 = cur_pos2 + ( cur_len >> 1 ) - org_center;
1921 if ( delta2 < 0 )
1922 delta2 = -delta2;
1924 edge->pos = ( delta1 < delta2 ) ? cur_pos1 : cur_pos2;
1925 edge2->pos = edge->pos + cur_len;
1927 AF_LOG(( "STEM: %d (opos=%.2f) to %d (opos=%.2f) "
1928 "snapped to (%.2f) and (%.2f)\n",
1929 edge-edges, edge->opos / 64.0,
1930 edge2-edges, edge2->opos / 64.0,
1931 edge->pos / 64.0, edge2->pos / 64.0 ));
1934 edge->flags |= AF_EDGE_DONE;
1935 edge2->flags |= AF_EDGE_DONE;
1937 if ( edge > edges && edge->pos < edge[-1].pos )
1939 AF_LOG(( "BOUND: %d (pos=%.2f) to (%.2f)\n",
1940 edge-edges, edge->pos / 64.0, edge[-1].pos / 64.0 ));
1941 edge->pos = edge[-1].pos;
1946 /* make sure that lowercase m's maintain their symmetry */
1948 /* In general, lowercase m's have six vertical edges if they are sans */
1949 /* serif, or twelve if they are with serifs. This implementation is */
1950 /* based on that assumption, and seems to work very well with most */
1951 /* faces. However, if for a certain face this assumption is not */
1952 /* true, the m is just rendered like before. In addition, any stem */
1953 /* correction will only be applied to symmetrical glyphs (even if the */
1954 /* glyph is not an m), so the potential for unwanted distortion is */
1955 /* relatively low. */
1957 /* We don't handle horizontal edges since we can't easily assure that */
1958 /* the third (lowest) stem aligns with the base line; it might end up */
1959 /* one pixel higher or lower. */
1961 n_edges = edge_limit - edges;
1962 if ( dim == AF_DIMENSION_HORZ && ( n_edges == 6 || n_edges == 12 ) )
1964 AF_Edge edge1, edge2, edge3;
1965 FT_Pos dist1, dist2, span, delta;
1968 if ( n_edges == 6 )
1970 edge1 = edges;
1971 edge2 = edges + 2;
1972 edge3 = edges + 4;
1974 else
1976 edge1 = edges + 1;
1977 edge2 = edges + 5;
1978 edge3 = edges + 9;
1981 dist1 = edge2->opos - edge1->opos;
1982 dist2 = edge3->opos - edge2->opos;
1984 span = dist1 - dist2;
1985 if ( span < 0 )
1986 span = -span;
1988 if ( span < 8 )
1990 delta = edge3->pos - ( 2 * edge2->pos - edge1->pos );
1991 edge3->pos -= delta;
1992 if ( edge3->link )
1993 edge3->link->pos -= delta;
1995 /* move the serifs along with the stem */
1996 if ( n_edges == 12 )
1998 ( edges + 8 )->pos -= delta;
1999 ( edges + 11 )->pos -= delta;
2002 edge3->flags |= AF_EDGE_DONE;
2003 if ( edge3->link )
2004 edge3->link->flags |= AF_EDGE_DONE;
2008 if ( has_serifs || !anchor )
2011 * now hint the remaining edges (serifs and single) in order
2012 * to complete our processing
2014 for ( edge = edges; edge < edge_limit; edge++ )
2016 FT_Pos delta;
2019 if ( edge->flags & AF_EDGE_DONE )
2020 continue;
2022 delta = 1000;
2024 if ( edge->serif )
2026 delta = edge->serif->opos - edge->opos;
2027 if ( delta < 0 )
2028 delta = -delta;
2031 if ( delta < 64 + 16 )
2033 af_latin_align_serif_edge( hints, edge->serif, edge );
2034 AF_LOG(( "SERIF: edge %d (opos=%.2f) serif to %d (opos=%.2f) "
2035 "aligned to (%.2f)\n",
2036 edge-edges, edge->opos / 64.0,
2037 edge->serif - edges, edge->serif->opos / 64.0,
2038 edge->pos / 64.0 ));
2040 else if ( !anchor )
2042 AF_LOG(( "SERIF_ANCHOR: edge %d (opos=%.2f) snapped to (%.2f)\n",
2043 edge-edges, edge->opos / 64.0, edge->pos / 64.0 ));
2044 edge->pos = FT_PIX_ROUND( edge->opos );
2045 anchor = edge;
2047 else
2049 AF_Edge before, after;
2052 for ( before = edge - 1; before >= edges; before-- )
2053 if ( before->flags & AF_EDGE_DONE )
2054 break;
2056 for ( after = edge + 1; after < edge_limit; after++ )
2057 if ( after->flags & AF_EDGE_DONE )
2058 break;
2060 if ( before >= edges && before < edge &&
2061 after < edge_limit && after > edge )
2063 if ( after->opos == before->opos )
2064 edge->pos = before->pos;
2065 else
2066 edge->pos = before->pos +
2067 FT_MulDiv( edge->opos - before->opos,
2068 after->pos - before->pos,
2069 after->opos - before->opos );
2070 AF_LOG(( "SERIF_LINK1: edge %d (opos=%.2f) snapped to (%.2f) "
2071 "from %d (opos=%.2f)\n",
2072 edge-edges, edge->opos / 64.0,
2073 edge->pos / 64.0, before - edges,
2074 before->opos / 64.0 ));
2076 else
2078 edge->pos = anchor->pos +
2079 ( ( edge->opos - anchor->opos + 16 ) & ~31 );
2080 AF_LOG(( "SERIF_LINK2: edge %d (opos=%.2f) snapped to (%.2f)\n",
2081 edge-edges, edge->opos / 64.0, edge->pos / 64.0 ));
2085 edge->flags |= AF_EDGE_DONE;
2087 if ( edge > edges && edge->pos < edge[-1].pos )
2088 edge->pos = edge[-1].pos;
2090 if ( edge + 1 < edge_limit &&
2091 edge[1].flags & AF_EDGE_DONE &&
2092 edge->pos > edge[1].pos )
2093 edge->pos = edge[1].pos;
2099 static FT_Error
2100 af_latin_hints_apply( AF_GlyphHints hints,
2101 FT_Outline* outline,
2102 AF_LatinMetrics metrics )
2104 FT_Error error;
2105 int dim;
2108 error = af_glyph_hints_reload( hints, outline, 1 );
2109 if ( error )
2110 goto Exit;
2112 /* analyze glyph outline */
2113 #ifdef AF_USE_WARPER
2114 if ( metrics->root.scaler.render_mode == FT_RENDER_MODE_LIGHT ||
2115 AF_HINTS_DO_HORIZONTAL( hints ) )
2116 #else
2117 if ( AF_HINTS_DO_HORIZONTAL( hints ) )
2118 #endif
2120 error = af_latin_hints_detect_features( hints, AF_DIMENSION_HORZ );
2121 if ( error )
2122 goto Exit;
2125 if ( AF_HINTS_DO_VERTICAL( hints ) )
2127 error = af_latin_hints_detect_features( hints, AF_DIMENSION_VERT );
2128 if ( error )
2129 goto Exit;
2131 af_latin_hints_compute_blue_edges( hints, metrics );
2134 /* grid-fit the outline */
2135 for ( dim = 0; dim < AF_DIMENSION_MAX; dim++ )
2137 #ifdef AF_USE_WARPER
2138 if ( ( dim == AF_DIMENSION_HORZ &&
2139 metrics->root.scaler.render_mode == FT_RENDER_MODE_LIGHT ) )
2141 AF_WarperRec warper;
2142 FT_Fixed scale;
2143 FT_Pos delta;
2146 af_warper_compute( &warper, hints, dim, &scale, &delta );
2147 af_glyph_hints_scale_dim( hints, dim, scale, delta );
2148 continue;
2150 #endif
2152 if ( ( dim == AF_DIMENSION_HORZ && AF_HINTS_DO_HORIZONTAL( hints ) ) ||
2153 ( dim == AF_DIMENSION_VERT && AF_HINTS_DO_VERTICAL( hints ) ) )
2155 af_latin_hint_edges( hints, (AF_Dimension)dim );
2156 af_glyph_hints_align_edge_points( hints, (AF_Dimension)dim );
2157 af_glyph_hints_align_strong_points( hints, (AF_Dimension)dim );
2158 af_glyph_hints_align_weak_points( hints, (AF_Dimension)dim );
2161 af_glyph_hints_save( hints, outline );
2163 Exit:
2164 return error;
2168 /*************************************************************************/
2169 /*************************************************************************/
2170 /***** *****/
2171 /***** L A T I N S C R I P T C L A S S *****/
2172 /***** *****/
2173 /*************************************************************************/
2174 /*************************************************************************/
2177 /* XXX: this should probably fine tuned to differentiate better between */
2178 /* scripts... */
2180 static const AF_Script_UniRangeRec af_latin_uniranges[] =
2182 AF_UNIRANGE_REC( 0x0020UL, 0x007FUL ), /* Basic Latin (no control chars) */
2183 AF_UNIRANGE_REC( 0x00A0UL, 0x00FFUL ), /* Latin-1 Supplement (no control chars) */
2184 AF_UNIRANGE_REC( 0x0100UL, 0x017FUL ), /* Latin Extended-A */
2185 AF_UNIRANGE_REC( 0x0180UL, 0x024FUL ), /* Latin Extended-B */
2186 AF_UNIRANGE_REC( 0x0250UL, 0x02AFUL ), /* IPA Extensions */
2187 AF_UNIRANGE_REC( 0x02B0UL, 0x02FFUL ), /* Spacing Modifier Letters */
2188 AF_UNIRANGE_REC( 0x0300UL, 0x036FUL ), /* Combining Diacritical Marks */
2189 AF_UNIRANGE_REC( 0x0370UL, 0x03FFUL ), /* Greek and Coptic */
2190 AF_UNIRANGE_REC( 0x0400UL, 0x04FFUL ), /* Cyrillic */
2191 AF_UNIRANGE_REC( 0x0500UL, 0x052FUL ), /* Cyrillic Supplement */
2192 AF_UNIRANGE_REC( 0x1D00UL, 0x1D7FUL ), /* Phonetic Extensions */
2193 AF_UNIRANGE_REC( 0x1D80UL, 0x1DBFUL ), /* Phonetic Extensions Supplement */
2194 AF_UNIRANGE_REC( 0x1DC0UL, 0x1DFFUL ), /* Combining Diacritical Marks Supplement */
2195 AF_UNIRANGE_REC( 0x1E00UL, 0x1EFFUL ), /* Latin Extended Additional */
2196 AF_UNIRANGE_REC( 0x1F00UL, 0x1FFFUL ), /* Greek Extended */
2197 AF_UNIRANGE_REC( 0x2000UL, 0x206FUL ), /* General Punctuation */
2198 AF_UNIRANGE_REC( 0x2070UL, 0x209FUL ), /* Superscripts and Subscripts */
2199 AF_UNIRANGE_REC( 0x20A0UL, 0x20CFUL ), /* Currency Symbols */
2200 AF_UNIRANGE_REC( 0x2150UL, 0x218FUL ), /* Number Forms */
2201 AF_UNIRANGE_REC( 0x2460UL, 0x24FFUL ), /* Enclosed Alphanumerics */
2202 AF_UNIRANGE_REC( 0x2C60UL, 0x2C7FUL ), /* Latin Extended-C */
2203 AF_UNIRANGE_REC( 0x2DE0UL, 0x2DFFUL ), /* Cyrillic Extended-A */
2204 AF_UNIRANGE_REC( 0xA640UL, 0xA69FUL ), /* Cyrillic Extended-B */
2205 AF_UNIRANGE_REC( 0xA720UL, 0xA7FFUL ), /* Latin Extended-D */
2206 AF_UNIRANGE_REC( 0xFB00UL, 0xFB06UL ), /* Alphab. Present. Forms (Latin Ligs) */
2207 AF_UNIRANGE_REC( 0x1D400UL, 0x1D7FFUL ), /* Mathematical Alphanumeric Symbols */
2208 AF_UNIRANGE_REC( 0UL, 0UL )
2212 AF_DEFINE_SCRIPT_CLASS(af_latin_script_class,
2213 AF_SCRIPT_LATIN,
2214 af_latin_uniranges,
2216 sizeof( AF_LatinMetricsRec ),
2218 (AF_Script_InitMetricsFunc) af_latin_metrics_init,
2219 (AF_Script_ScaleMetricsFunc)af_latin_metrics_scale,
2220 (AF_Script_DoneMetricsFunc) NULL,
2222 (AF_Script_InitHintsFunc) af_latin_hints_init,
2223 (AF_Script_ApplyHintsFunc) af_latin_hints_apply
2227 /* END */