Bug 470455 - test_database_sync_embed_visits.js leaks, r=sdwilsh
[wine-gecko.git] / modules / freetype2 / src / autofit / aflatin.c
blobb70da068afa049471c5ae83515e65f0f06ba7360
1 /***************************************************************************/
2 /* */
3 /* aflatin.c */
4 /* */
5 /* Auto-fitter hinting routines for latin script (body). */
6 /* */
7 /* Copyright 2003, 2004, 2005, 2006, 2007 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 "aflatin.h"
20 #include "aferrors.h"
23 #ifdef AF_USE_WARPER
24 #include "afwarp.h"
25 #endif
28 /*************************************************************************/
29 /*************************************************************************/
30 /***** *****/
31 /***** L A T I N G L O B A L M E T R I C S *****/
32 /***** *****/
33 /*************************************************************************/
34 /*************************************************************************/
36 FT_LOCAL_DEF( void )
37 af_latin_metrics_init_widths( AF_LatinMetrics metrics,
38 FT_Face face,
39 FT_ULong charcode )
41 /* scan the array of segments in each direction */
42 AF_GlyphHintsRec hints[1];
45 af_glyph_hints_init( hints, face->memory );
47 metrics->axis[AF_DIMENSION_HORZ].width_count = 0;
48 metrics->axis[AF_DIMENSION_VERT].width_count = 0;
51 FT_Error error;
52 FT_UInt glyph_index;
53 int dim;
54 AF_LatinMetricsRec dummy[1];
55 AF_Scaler scaler = &dummy->root.scaler;
58 glyph_index = FT_Get_Char_Index( face, charcode );
59 if ( glyph_index == 0 )
60 goto Exit;
62 error = FT_Load_Glyph( face, glyph_index, FT_LOAD_NO_SCALE );
63 if ( error || face->glyph->outline.n_points <= 0 )
64 goto Exit;
66 FT_ZERO( dummy );
68 dummy->units_per_em = metrics->units_per_em;
69 scaler->x_scale = scaler->y_scale = 0x10000L;
70 scaler->x_delta = scaler->y_delta = 0;
71 scaler->face = face;
72 scaler->render_mode = FT_RENDER_MODE_NORMAL;
73 scaler->flags = 0;
75 af_glyph_hints_rescale( hints, (AF_ScriptMetrics)dummy );
77 error = af_glyph_hints_reload( hints, &face->glyph->outline, 0 );
78 if ( error )
79 goto Exit;
81 for ( dim = 0; dim < AF_DIMENSION_MAX; dim++ )
83 AF_LatinAxis axis = &metrics->axis[dim];
84 AF_AxisHints axhints = &hints->axis[dim];
85 AF_Segment seg, limit, link;
86 FT_UInt num_widths = 0;
89 error = af_latin_hints_compute_segments( hints,
90 (AF_Dimension)dim );
91 if ( error )
92 goto Exit;
94 af_latin_hints_link_segments( hints,
95 (AF_Dimension)dim );
97 seg = axhints->segments;
98 limit = seg + axhints->num_segments;
100 for ( ; seg < limit; seg++ )
102 link = seg->link;
104 /* we only consider stem segments there! */
105 if ( link && link->link == seg && link > seg )
107 FT_Pos dist;
110 dist = seg->pos - link->pos;
111 if ( dist < 0 )
112 dist = -dist;
114 if ( num_widths < AF_LATIN_MAX_WIDTHS )
115 axis->widths[ num_widths++ ].org = dist;
119 af_sort_widths( num_widths, axis->widths );
120 axis->width_count = num_widths;
123 Exit:
124 for ( dim = 0; dim < AF_DIMENSION_MAX; dim++ )
126 AF_LatinAxis axis = &metrics->axis[dim];
127 FT_Pos stdw;
130 stdw = ( axis->width_count > 0 )
131 ? axis->widths[0].org
132 : AF_LATIN_CONSTANT( metrics, 50 );
134 /* let's try 20% of the smallest width */
135 axis->edge_distance_threshold = stdw / 5;
136 axis->standard_width = stdw;
137 axis->extra_light = 0;
141 af_glyph_hints_done( hints );
146 #define AF_LATIN_MAX_TEST_CHARACTERS 12
149 static const char* const af_latin_blue_chars[AF_LATIN_MAX_BLUES] =
151 "THEZOCQS",
152 "HEZLOCUS",
153 "fijkdbh",
154 "xzroesc",
155 "xzroesc",
156 "pqgjy"
160 static void
161 af_latin_metrics_init_blues( AF_LatinMetrics metrics,
162 FT_Face face )
164 FT_Pos flats [AF_LATIN_MAX_TEST_CHARACTERS];
165 FT_Pos rounds[AF_LATIN_MAX_TEST_CHARACTERS];
166 FT_Int num_flats;
167 FT_Int num_rounds;
168 FT_Int bb;
169 AF_LatinBlue blue;
170 FT_Error error;
171 AF_LatinAxis axis = &metrics->axis[AF_DIMENSION_VERT];
172 FT_GlyphSlot glyph = face->glyph;
175 /* we compute the blues simply by loading each character from the */
176 /* 'af_latin_blue_chars[blues]' string, then compute its top-most or */
177 /* bottom-most points (depending on `AF_IS_TOP_BLUE') */
179 AF_LOG(( "blue zones computation\n" ));
180 AF_LOG(( "------------------------------------------------\n" ));
182 for ( bb = 0; bb < AF_LATIN_BLUE_MAX; bb++ )
184 const char* p = af_latin_blue_chars[bb];
185 const char* limit = p + AF_LATIN_MAX_TEST_CHARACTERS;
186 FT_Pos* blue_ref;
187 FT_Pos* blue_shoot;
190 AF_LOG(( "blue %3d: ", bb ));
192 num_flats = 0;
193 num_rounds = 0;
195 for ( ; p < limit && *p; p++ )
197 FT_UInt glyph_index;
198 FT_Int best_point, best_y, best_first, best_last;
199 FT_Vector* points;
200 FT_Bool round = 0;
203 AF_LOG(( "'%c'", *p ));
205 /* load the character in the face -- skip unknown or empty ones */
206 glyph_index = FT_Get_Char_Index( face, (FT_UInt)*p );
207 if ( glyph_index == 0 )
208 continue;
210 error = FT_Load_Glyph( face, glyph_index, FT_LOAD_NO_SCALE );
211 if ( error || glyph->outline.n_points <= 0 )
212 continue;
214 /* now compute min or max point indices and coordinates */
215 points = glyph->outline.points;
216 best_point = -1;
217 best_y = 0; /* make compiler happy */
218 best_first = 0; /* ditto */
219 best_last = 0; /* ditto */
222 FT_Int nn;
223 FT_Int first = 0;
224 FT_Int last = -1;
227 for ( nn = 0; nn < glyph->outline.n_contours; first = last+1, nn++ )
229 FT_Int old_best_point = best_point;
230 FT_Int pp;
233 last = glyph->outline.contours[nn];
235 /* Avoid single-point contours since they are never rasterized. */
236 /* In some fonts, they correspond to mark attachment points */
237 /* which are way outside of the glyph's real outline. */
238 if ( last <= first )
239 continue;
241 if ( AF_LATIN_IS_TOP_BLUE( bb ) )
243 for ( pp = first; pp <= last; pp++ )
244 if ( best_point < 0 || points[pp].y > best_y )
246 best_point = pp;
247 best_y = points[pp].y;
250 else
252 for ( pp = first; pp <= last; pp++ )
253 if ( best_point < 0 || points[pp].y < best_y )
255 best_point = pp;
256 best_y = points[pp].y;
260 if ( best_point != old_best_point )
262 best_first = first;
263 best_last = last;
266 AF_LOG(( "%5d", best_y ));
269 /* now check whether the point belongs to a straight or round */
270 /* segment; we first need to find in which contour the extremum */
271 /* lies, then inspect its previous and next points */
272 if ( best_point >= 0 )
274 FT_Int prev, next;
275 FT_Pos dist;
278 /* now look for the previous and next points that are not on the */
279 /* same Y coordinate. Threshold the `closeness'... */
280 prev = best_point;
281 next = prev;
285 if ( prev > best_first )
286 prev--;
287 else
288 prev = best_last;
290 dist = points[prev].y - best_y;
291 if ( dist < -5 || dist > 5 )
292 break;
294 } while ( prev != best_point );
298 if ( next < best_last )
299 next++;
300 else
301 next = best_first;
303 dist = points[next].y - best_y;
304 if ( dist < -5 || dist > 5 )
305 break;
307 } while ( next != best_point );
309 /* now, set the `round' flag depending on the segment's kind */
310 round = FT_BOOL(
311 FT_CURVE_TAG( glyph->outline.tags[prev] ) != FT_CURVE_TAG_ON ||
312 FT_CURVE_TAG( glyph->outline.tags[next] ) != FT_CURVE_TAG_ON );
314 AF_LOG(( "%c ", round ? 'r' : 'f' ));
317 if ( round )
318 rounds[num_rounds++] = best_y;
319 else
320 flats[num_flats++] = best_y;
323 AF_LOG(( "\n" ));
325 if ( num_flats == 0 && num_rounds == 0 )
328 * we couldn't find a single glyph to compute this blue zone,
329 * we will simply ignore it then
331 AF_LOG(( "empty!\n" ));
332 continue;
335 /* we have computed the contents of the `rounds' and `flats' tables, */
336 /* now determine the reference and overshoot position of the blue -- */
337 /* we simply take the median value after a simple sort */
338 af_sort_pos( num_rounds, rounds );
339 af_sort_pos( num_flats, flats );
341 blue = & axis->blues[axis->blue_count];
342 blue_ref = & blue->ref.org;
343 blue_shoot = & blue->shoot.org;
345 axis->blue_count++;
347 if ( num_flats == 0 )
349 *blue_ref =
350 *blue_shoot = rounds[num_rounds / 2];
352 else if ( num_rounds == 0 )
354 *blue_ref =
355 *blue_shoot = flats[num_flats / 2];
357 else
359 *blue_ref = flats[num_flats / 2];
360 *blue_shoot = rounds[num_rounds / 2];
363 /* there are sometimes problems: if the overshoot position of top */
364 /* zones is under its reference position, or the opposite for bottom */
365 /* zones. We must thus check everything there and correct the errors */
366 if ( *blue_shoot != *blue_ref )
368 FT_Pos ref = *blue_ref;
369 FT_Pos shoot = *blue_shoot;
370 FT_Bool over_ref = FT_BOOL( shoot > ref );
373 if ( AF_LATIN_IS_TOP_BLUE( bb ) ^ over_ref )
374 *blue_shoot = *blue_ref = ( shoot + ref ) / 2;
377 blue->flags = 0;
378 if ( AF_LATIN_IS_TOP_BLUE( bb ) )
379 blue->flags |= AF_LATIN_BLUE_TOP;
382 * The following flags is used later to adjust the y and x scales
383 * in order to optimize the pixel grid alignment of the top of small
384 * letters.
386 if ( bb == AF_LATIN_BLUE_SMALL_TOP )
387 blue->flags |= AF_LATIN_BLUE_ADJUSTMENT;
389 AF_LOG(( "-- ref = %ld, shoot = %ld\n", *blue_ref, *blue_shoot ));
392 return;
396 FT_LOCAL_DEF( FT_Error )
397 af_latin_metrics_init( AF_LatinMetrics metrics,
398 FT_Face face )
400 FT_Error error = AF_Err_Ok;
401 FT_CharMap oldmap = face->charmap;
402 FT_UInt ee;
404 static const FT_Encoding latin_encodings[] =
406 FT_ENCODING_UNICODE,
407 FT_ENCODING_APPLE_ROMAN,
408 FT_ENCODING_ADOBE_STANDARD,
409 FT_ENCODING_ADOBE_LATIN_1,
410 FT_ENCODING_NONE /* end of list */
414 metrics->units_per_em = face->units_per_EM;
416 /* do we have a latin charmap in there? */
417 for ( ee = 0; latin_encodings[ee] != FT_ENCODING_NONE; ee++ )
419 error = FT_Select_Charmap( face, latin_encodings[ee] );
420 if ( !error )
421 break;
424 if ( !error )
426 /* For now, compute the standard width and height from the `o'. */
427 af_latin_metrics_init_widths( metrics, face, 'o' );
428 af_latin_metrics_init_blues( metrics, face );
431 FT_Set_Charmap( face, oldmap );
432 return AF_Err_Ok;
436 static void
437 af_latin_metrics_scale_dim( AF_LatinMetrics metrics,
438 AF_Scaler scaler,
439 AF_Dimension dim )
441 FT_Fixed scale;
442 FT_Pos delta;
443 AF_LatinAxis axis;
444 FT_UInt nn;
447 if ( dim == AF_DIMENSION_HORZ )
449 scale = scaler->x_scale;
450 delta = scaler->x_delta;
452 else
454 scale = scaler->y_scale;
455 delta = scaler->y_delta;
458 axis = &metrics->axis[dim];
460 if ( axis->org_scale == scale && axis->org_delta == delta )
461 return;
463 axis->org_scale = scale;
464 axis->org_delta = delta;
467 * correct X and Y scale to optimize the alignment of the top of small
468 * letters to the pixel grid
471 AF_LatinAxis Axis = &metrics->axis[AF_DIMENSION_VERT];
472 AF_LatinBlue blue = NULL;
475 for ( nn = 0; nn < Axis->blue_count; nn++ )
477 if ( Axis->blues[nn].flags & AF_LATIN_BLUE_ADJUSTMENT )
479 blue = &Axis->blues[nn];
480 break;
484 if ( blue )
486 FT_Pos scaled = FT_MulFix( blue->shoot.org, scaler->y_scale );
487 FT_Pos fitted = ( scaled + 40 ) & ~63;
490 if ( scaled != fitted )
492 #if 0
493 if ( dim == AF_DIMENSION_HORZ )
495 if ( fitted < scaled )
496 scale -= scale / 50; /* scale *= 0.98 */
498 else
499 #endif
500 if ( dim == AF_DIMENSION_VERT )
502 scale = FT_MulDiv( scale, fitted, scaled );
508 axis->scale = scale;
509 axis->delta = delta;
511 if ( dim == AF_DIMENSION_HORZ )
513 metrics->root.scaler.x_scale = scale;
514 metrics->root.scaler.x_delta = delta;
516 else
518 metrics->root.scaler.y_scale = scale;
519 metrics->root.scaler.y_delta = delta;
522 /* scale the standard widths */
523 for ( nn = 0; nn < axis->width_count; nn++ )
525 AF_Width width = axis->widths + nn;
528 width->cur = FT_MulFix( width->org, scale );
529 width->fit = width->cur;
532 /* an extra-light axis corresponds to a standard width that is */
533 /* smaller than 0.75 pixels */
534 axis->extra_light =
535 (FT_Bool)( FT_MulFix( axis->standard_width, scale ) < 32 + 8 );
537 if ( dim == AF_DIMENSION_VERT )
539 /* scale the blue zones */
540 for ( nn = 0; nn < axis->blue_count; nn++ )
542 AF_LatinBlue blue = &axis->blues[nn];
543 FT_Pos dist;
546 blue->ref.cur = FT_MulFix( blue->ref.org, scale ) + delta;
547 blue->ref.fit = blue->ref.cur;
548 blue->shoot.cur = FT_MulFix( blue->shoot.org, scale ) + delta;
549 blue->shoot.fit = blue->shoot.cur;
550 blue->flags &= ~AF_LATIN_BLUE_ACTIVE;
552 /* a blue zone is only active if it is less than 3/4 pixels tall */
553 dist = FT_MulFix( blue->ref.org - blue->shoot.org, scale );
554 if ( dist <= 48 && dist >= -48 )
556 FT_Pos delta1, delta2;
559 delta1 = blue->shoot.org - blue->ref.org;
560 delta2 = delta1;
561 if ( delta1 < 0 )
562 delta2 = -delta2;
564 delta2 = FT_MulFix( delta2, scale );
566 if ( delta2 < 32 )
567 delta2 = 0;
568 else if ( delta2 < 64 )
569 delta2 = 32 + ( ( ( delta2 - 32 ) + 16 ) & ~31 );
570 else
571 delta2 = FT_PIX_ROUND( delta2 );
573 if ( delta1 < 0 )
574 delta2 = -delta2;
576 blue->ref.fit = FT_PIX_ROUND( blue->ref.cur );
577 blue->shoot.fit = blue->ref.fit + delta2;
579 blue->flags |= AF_LATIN_BLUE_ACTIVE;
586 FT_LOCAL_DEF( void )
587 af_latin_metrics_scale( AF_LatinMetrics metrics,
588 AF_Scaler scaler )
590 metrics->root.scaler.render_mode = scaler->render_mode;
591 metrics->root.scaler.face = scaler->face;
593 af_latin_metrics_scale_dim( metrics, scaler, AF_DIMENSION_HORZ );
594 af_latin_metrics_scale_dim( metrics, scaler, AF_DIMENSION_VERT );
598 /*************************************************************************/
599 /*************************************************************************/
600 /***** *****/
601 /***** L A T I N G L Y P H A N A L Y S I S *****/
602 /***** *****/
603 /*************************************************************************/
604 /*************************************************************************/
606 FT_LOCAL_DEF( FT_Error )
607 af_latin_hints_compute_segments( AF_GlyphHints hints,
608 AF_Dimension dim )
610 AF_AxisHints axis = &hints->axis[dim];
611 FT_Memory memory = hints->memory;
612 FT_Error error = AF_Err_Ok;
613 AF_Segment segment = NULL;
614 AF_SegmentRec seg0;
615 AF_Point* contour = hints->contours;
616 AF_Point* contour_limit = contour + hints->num_contours;
617 AF_Direction major_dir, segment_dir;
620 FT_ZERO( &seg0 );
621 seg0.score = 32000;
622 seg0.flags = AF_EDGE_NORMAL;
624 major_dir = (AF_Direction)FT_ABS( axis->major_dir );
625 segment_dir = major_dir;
627 axis->num_segments = 0;
629 /* set up (u,v) in each point */
630 if ( dim == AF_DIMENSION_HORZ )
632 AF_Point point = hints->points;
633 AF_Point limit = point + hints->num_points;
636 for ( ; point < limit; point++ )
638 point->u = point->fx;
639 point->v = point->fy;
642 else
644 AF_Point point = hints->points;
645 AF_Point limit = point + hints->num_points;
648 for ( ; point < limit; point++ )
650 point->u = point->fy;
651 point->v = point->fx;
655 /* do each contour separately */
656 for ( ; contour < contour_limit; contour++ )
658 AF_Point point = contour[0];
659 AF_Point last = point->prev;
660 int on_edge = 0;
661 FT_Pos min_pos = 32000; /* minimum segment pos != min_coord */
662 FT_Pos max_pos = -32000; /* maximum segment pos != max_coord */
663 FT_Bool passed;
666 if ( point == last ) /* skip singletons -- just in case */
667 continue;
669 if ( FT_ABS( last->out_dir ) == major_dir &&
670 FT_ABS( point->out_dir ) == major_dir )
672 /* we are already on an edge, try to locate its start */
673 last = point;
675 for (;;)
677 point = point->prev;
678 if ( FT_ABS( point->out_dir ) != major_dir )
680 point = point->next;
681 break;
683 if ( point == last )
684 break;
688 last = point;
689 passed = 0;
691 for (;;)
693 FT_Pos u, v;
696 if ( on_edge )
698 u = point->u;
699 if ( u < min_pos )
700 min_pos = u;
701 if ( u > max_pos )
702 max_pos = u;
704 if ( point->out_dir != segment_dir || point == last )
706 /* we are just leaving an edge; record a new segment! */
707 segment->last = point;
708 segment->pos = (FT_Short)( ( min_pos + max_pos ) >> 1 );
710 /* a segment is round if either its first or last point */
711 /* is a control point */
712 if ( ( segment->first->flags | point->flags ) &
713 AF_FLAG_CONTROL )
714 segment->flags |= AF_EDGE_ROUND;
716 /* compute segment size */
717 min_pos = max_pos = point->v;
719 v = segment->first->v;
720 if ( v < min_pos )
721 min_pos = v;
722 if ( v > max_pos )
723 max_pos = v;
725 segment->min_coord = (FT_Short)min_pos;
726 segment->max_coord = (FT_Short)max_pos;
727 segment->height = (FT_Short)( segment->max_coord -
728 segment->min_coord );
730 on_edge = 0;
731 segment = NULL;
732 /* fallthrough */
736 /* now exit if we are at the start/end point */
737 if ( point == last )
739 if ( passed )
740 break;
741 passed = 1;
744 if ( !on_edge && FT_ABS( point->out_dir ) == major_dir )
746 /* this is the start of a new segment! */
747 segment_dir = (AF_Direction)point->out_dir;
749 /* clear all segment fields */
750 error = af_axis_hints_new_segment( axis, memory, &segment );
751 if ( error )
752 goto Exit;
754 segment[0] = seg0;
755 segment->dir = (FT_Char)segment_dir;
756 min_pos = max_pos = point->u;
757 segment->first = point;
758 segment->last = point;
759 segment->contour = contour;
760 on_edge = 1;
763 point = point->next;
766 } /* contours */
769 /* now slightly increase the height of segments when this makes */
770 /* sense -- this is used to better detect and ignore serifs */
772 AF_Segment segments = axis->segments;
773 AF_Segment segments_end = segments + axis->num_segments;
776 for ( segment = segments; segment < segments_end; segment++ )
778 AF_Point first = segment->first;
779 AF_Point last = segment->last;
780 FT_Pos first_v = first->v;
781 FT_Pos last_v = last->v;
784 if ( first == last )
785 continue;
787 if ( first_v < last_v )
789 AF_Point p;
792 p = first->prev;
793 if ( p->v < first_v )
794 segment->height = (FT_Short)( segment->height +
795 ( ( first_v - p->v ) >> 1 ) );
797 p = last->next;
798 if ( p->v > last_v )
799 segment->height = (FT_Short)( segment->height +
800 ( ( p->v - last_v ) >> 1 ) );
802 else
804 AF_Point p;
807 p = first->prev;
808 if ( p->v > first_v )
809 segment->height = (FT_Short)( segment->height +
810 ( ( p->v - first_v ) >> 1 ) );
812 p = last->next;
813 if ( p->v < last_v )
814 segment->height = (FT_Short)( segment->height +
815 ( ( last_v - p->v ) >> 1 ) );
820 Exit:
821 return error;
825 FT_LOCAL_DEF( void )
826 af_latin_hints_link_segments( AF_GlyphHints hints,
827 AF_Dimension dim )
829 AF_AxisHints axis = &hints->axis[dim];
830 AF_Segment segments = axis->segments;
831 AF_Segment segment_limit = segments + axis->num_segments;
832 FT_Pos len_threshold, len_score;
833 AF_Segment seg1, seg2;
836 len_threshold = AF_LATIN_CONSTANT( hints->metrics, 8 );
837 if ( len_threshold == 0 )
838 len_threshold = 1;
840 len_score = AF_LATIN_CONSTANT( hints->metrics, 6000 );
842 /* now compare each segment to the others */
843 for ( seg1 = segments; seg1 < segment_limit; seg1++ )
845 /* the fake segments are introduced to hint the metrics -- */
846 /* we must never link them to anything */
847 if ( seg1->dir != axis->major_dir || seg1->first == seg1->last )
848 continue;
850 for ( seg2 = segments; seg2 < segment_limit; seg2++ )
851 if ( seg1->dir + seg2->dir == 0 && seg2->pos > seg1->pos )
853 FT_Pos pos1 = seg1->pos;
854 FT_Pos pos2 = seg2->pos;
855 FT_Pos dist = pos2 - pos1;
858 if ( dist < 0 )
859 dist = -dist;
862 FT_Pos min = seg1->min_coord;
863 FT_Pos max = seg1->max_coord;
864 FT_Pos len, score;
867 if ( min < seg2->min_coord )
868 min = seg2->min_coord;
870 if ( max > seg2->max_coord )
871 max = seg2->max_coord;
873 len = max - min;
874 if ( len >= len_threshold )
876 score = dist + len_score / len;
878 if ( score < seg1->score )
880 seg1->score = score;
881 seg1->link = seg2;
884 if ( score < seg2->score )
886 seg2->score = score;
887 seg2->link = seg1;
894 /* now, compute the `serif' segments */
895 for ( seg1 = segments; seg1 < segment_limit; seg1++ )
897 seg2 = seg1->link;
899 if ( seg2 )
901 if ( seg2->link != seg1 )
903 seg1->link = 0;
904 seg1->serif = seg2->link;
911 FT_LOCAL_DEF( FT_Error )
912 af_latin_hints_compute_edges( AF_GlyphHints hints,
913 AF_Dimension dim )
915 AF_AxisHints axis = &hints->axis[dim];
916 FT_Error error = AF_Err_Ok;
917 FT_Memory memory = hints->memory;
918 AF_LatinAxis laxis = &((AF_LatinMetrics)hints->metrics)->axis[dim];
920 AF_Segment segments = axis->segments;
921 AF_Segment segment_limit = segments + axis->num_segments;
922 AF_Segment seg;
924 AF_Direction up_dir;
925 FT_Fixed scale;
926 FT_Pos edge_distance_threshold;
927 FT_Pos segment_length_threshold;
930 axis->num_edges = 0;
932 scale = ( dim == AF_DIMENSION_HORZ ) ? hints->x_scale
933 : hints->y_scale;
935 up_dir = ( dim == AF_DIMENSION_HORZ ) ? AF_DIR_UP
936 : AF_DIR_RIGHT;
939 * We ignore all segments that are less than 1 pixels in length,
940 * to avoid many problems with serif fonts. We compute the
941 * corresponding threshold in font units.
943 if ( dim == AF_DIMENSION_HORZ )
944 segment_length_threshold = FT_DivFix( 64, hints->y_scale );
945 else
946 segment_length_threshold = 0;
948 /*********************************************************************/
949 /* */
950 /* We will begin by generating a sorted table of edges for the */
951 /* current direction. To do so, we simply scan each segment and try */
952 /* to find an edge in our table that corresponds to its position. */
953 /* */
954 /* If no edge is found, we create and insert a new edge in the */
955 /* sorted table. Otherwise, we simply add the segment to the edge's */
956 /* list which will be processed in the second step to compute the */
957 /* edge's properties. */
958 /* */
959 /* Note that the edges table is sorted along the segment/edge */
960 /* position. */
961 /* */
962 /*********************************************************************/
964 edge_distance_threshold = FT_MulFix( laxis->edge_distance_threshold,
965 scale );
966 if ( edge_distance_threshold > 64 / 4 )
967 edge_distance_threshold = 64 / 4;
969 edge_distance_threshold = FT_DivFix( edge_distance_threshold,
970 scale );
972 for ( seg = segments; seg < segment_limit; seg++ )
974 AF_Edge found = 0;
975 FT_Int ee;
978 if ( seg->height < segment_length_threshold )
979 continue;
981 /* A special case for serif edges: If they are smaller than */
982 /* 1.5 pixels we ignore them. */
983 if ( seg->serif &&
984 2 * seg->height < 3 * segment_length_threshold )
985 continue;
987 /* look for an edge corresponding to the segment */
988 for ( ee = 0; ee < axis->num_edges; ee++ )
990 AF_Edge edge = axis->edges + ee;
991 FT_Pos dist;
994 dist = seg->pos - edge->fpos;
995 if ( dist < 0 )
996 dist = -dist;
998 if ( dist < edge_distance_threshold && edge->dir == seg->dir )
1000 found = edge;
1001 break;
1005 if ( !found )
1007 AF_Edge edge;
1010 /* insert a new edge in the list and */
1011 /* sort according to the position */
1012 error = af_axis_hints_new_edge( axis, seg->pos,
1013 (AF_Direction)seg->dir,
1014 memory, &edge );
1015 if ( error )
1016 goto Exit;
1018 /* add the segment to the new edge's list */
1019 FT_ZERO( edge );
1021 edge->first = seg;
1022 edge->last = seg;
1023 edge->fpos = seg->pos;
1024 edge->dir = seg->dir;
1025 edge->opos = edge->pos = FT_MulFix( seg->pos, scale );
1026 seg->edge_next = seg;
1028 else
1030 /* if an edge was found, simply add the segment to the edge's */
1031 /* list */
1032 seg->edge_next = found->first;
1033 found->last->edge_next = seg;
1034 found->last = seg;
1039 /*********************************************************************/
1040 /* */
1041 /* Good, we will now compute each edge's properties according to */
1042 /* segments found on its position. Basically, these are: */
1043 /* */
1044 /* - edge's main direction */
1045 /* - stem edge, serif edge or both (which defaults to stem then) */
1046 /* - rounded edge, straight or both (which defaults to straight) */
1047 /* - link for edge */
1048 /* */
1049 /*********************************************************************/
1051 /* first of all, set the `edge' field in each segment -- this is */
1052 /* required in order to compute edge links */
1055 * Note that removing this loop and setting the `edge' field of each
1056 * segment directly in the code above slows down execution speed for
1057 * some reasons on platforms like the Sun.
1060 AF_Edge edges = axis->edges;
1061 AF_Edge edge_limit = edges + axis->num_edges;
1062 AF_Edge edge;
1065 for ( edge = edges; edge < edge_limit; edge++ )
1067 seg = edge->first;
1068 if ( seg )
1071 seg->edge = edge;
1072 seg = seg->edge_next;
1074 } while ( seg != edge->first );
1077 /* now, compute each edge properties */
1078 for ( edge = edges; edge < edge_limit; edge++ )
1080 FT_Int is_round = 0; /* does it contain round segments? */
1081 FT_Int is_straight = 0; /* does it contain straight segments? */
1082 FT_Pos ups = 0; /* number of upwards segments */
1083 FT_Pos downs = 0; /* number of downwards segments */
1086 seg = edge->first;
1090 FT_Bool is_serif;
1093 /* check for roundness of segment */
1094 if ( seg->flags & AF_EDGE_ROUND )
1095 is_round++;
1096 else
1097 is_straight++;
1099 /* check for segment direction */
1100 if ( seg->dir == up_dir )
1101 ups += seg->max_coord-seg->min_coord;
1102 else
1103 downs += seg->max_coord-seg->min_coord;
1105 /* check for links -- if seg->serif is set, then seg->link must */
1106 /* be ignored */
1107 is_serif = (FT_Bool)( seg->serif &&
1108 seg->serif->edge &&
1109 seg->serif->edge != edge );
1111 if ( ( seg->link && seg->link->edge != NULL ) || is_serif )
1113 AF_Edge edge2;
1114 AF_Segment seg2;
1117 edge2 = edge->link;
1118 seg2 = seg->link;
1120 if ( is_serif )
1122 seg2 = seg->serif;
1123 edge2 = edge->serif;
1126 if ( edge2 )
1128 FT_Pos edge_delta;
1129 FT_Pos seg_delta;
1132 edge_delta = edge->fpos - edge2->fpos;
1133 if ( edge_delta < 0 )
1134 edge_delta = -edge_delta;
1136 seg_delta = seg->pos - seg2->pos;
1137 if ( seg_delta < 0 )
1138 seg_delta = -seg_delta;
1140 if ( seg_delta < edge_delta )
1141 edge2 = seg2->edge;
1143 else
1144 edge2 = seg2->edge;
1146 if ( is_serif )
1148 edge->serif = edge2;
1149 edge2->flags |= AF_EDGE_SERIF;
1151 else
1152 edge->link = edge2;
1155 seg = seg->edge_next;
1157 } while ( seg != edge->first );
1159 /* set the round/straight flags */
1160 edge->flags = AF_EDGE_NORMAL;
1162 if ( is_round > 0 && is_round >= is_straight )
1163 edge->flags |= AF_EDGE_ROUND;
1165 #if 0
1166 /* set the edge's main direction */
1167 edge->dir = AF_DIR_NONE;
1169 if ( ups > downs )
1170 edge->dir = (FT_Char)up_dir;
1172 else if ( ups < downs )
1173 edge->dir = (FT_Char)-up_dir;
1175 else if ( ups == downs )
1176 edge->dir = 0; /* both up and down! */
1177 #endif
1179 /* gets rid of serifs if link is set */
1180 /* XXX: This gets rid of many unpleasant artefacts! */
1181 /* Example: the `c' in cour.pfa at size 13 */
1183 if ( edge->serif && edge->link )
1184 edge->serif = 0;
1188 Exit:
1189 return error;
1193 FT_LOCAL_DEF( FT_Error )
1194 af_latin_hints_detect_features( AF_GlyphHints hints,
1195 AF_Dimension dim )
1197 FT_Error error;
1200 error = af_latin_hints_compute_segments( hints, dim );
1201 if ( !error )
1203 af_latin_hints_link_segments( hints, dim );
1205 error = af_latin_hints_compute_edges( hints, dim );
1207 return error;
1211 FT_LOCAL_DEF( void )
1212 af_latin_hints_compute_blue_edges( AF_GlyphHints hints,
1213 AF_LatinMetrics metrics )
1215 AF_AxisHints axis = &hints->axis[ AF_DIMENSION_VERT ];
1216 AF_Edge edge = axis->edges;
1217 AF_Edge edge_limit = edge + axis->num_edges;
1218 AF_LatinAxis latin = &metrics->axis[ AF_DIMENSION_VERT ];
1219 FT_Fixed scale = latin->scale;
1222 /* compute which blue zones are active, i.e. have their scaled */
1223 /* size < 3/4 pixels */
1225 /* for each horizontal edge search the blue zone which is closest */
1226 for ( ; edge < edge_limit; edge++ )
1228 FT_Int bb;
1229 AF_Width best_blue = NULL;
1230 FT_Pos best_dist; /* initial threshold */
1233 /* compute the initial threshold as a fraction of the EM size */
1234 best_dist = FT_MulFix( metrics->units_per_em / 40, scale );
1236 if ( best_dist > 64 / 2 )
1237 best_dist = 64 / 2;
1239 for ( bb = 0; bb < AF_LATIN_BLUE_MAX; bb++ )
1241 AF_LatinBlue blue = latin->blues + bb;
1242 FT_Bool is_top_blue, is_major_dir;
1245 /* skip inactive blue zones (i.e., those that are too small) */
1246 if ( !( blue->flags & AF_LATIN_BLUE_ACTIVE ) )
1247 continue;
1249 /* if it is a top zone, check for right edges -- if it is a bottom */
1250 /* zone, check for left edges */
1251 /* */
1252 /* of course, that's for TrueType */
1253 is_top_blue = (FT_Byte)( ( blue->flags & AF_LATIN_BLUE_TOP ) != 0 );
1254 is_major_dir = FT_BOOL( edge->dir == axis->major_dir );
1256 /* if it is a top zone, the edge must be against the major */
1257 /* direction; if it is a bottom zone, it must be in the major */
1258 /* direction */
1259 if ( is_top_blue ^ is_major_dir )
1261 FT_Pos dist;
1264 /* first of all, compare it to the reference position */
1265 dist = edge->fpos - blue->ref.org;
1266 if ( dist < 0 )
1267 dist = -dist;
1269 dist = FT_MulFix( dist, scale );
1270 if ( dist < best_dist )
1272 best_dist = dist;
1273 best_blue = & blue->ref;
1276 /* now, compare it to the overshoot position if the edge is */
1277 /* rounded, and if the edge is over the reference position of a */
1278 /* top zone, or under the reference position of a bottom zone */
1279 if ( edge->flags & AF_EDGE_ROUND && dist != 0 )
1281 FT_Bool is_under_ref = FT_BOOL( edge->fpos < blue->ref.org );
1284 if ( is_top_blue ^ is_under_ref )
1286 blue = latin->blues + bb;
1287 dist = edge->fpos - blue->shoot.org;
1288 if ( dist < 0 )
1289 dist = -dist;
1291 dist = FT_MulFix( dist, scale );
1292 if ( dist < best_dist )
1294 best_dist = dist;
1295 best_blue = & blue->shoot;
1302 if ( best_blue )
1303 edge->blue_edge = best_blue;
1308 static FT_Error
1309 af_latin_hints_init( AF_GlyphHints hints,
1310 AF_LatinMetrics metrics )
1312 FT_Render_Mode mode;
1313 FT_UInt32 scaler_flags, other_flags;
1314 FT_Face face = metrics->root.scaler.face;
1317 af_glyph_hints_rescale( hints, (AF_ScriptMetrics)metrics );
1320 * correct x_scale and y_scale if needed, since they may have
1321 * been modified `af_latin_metrics_scale_dim' above
1323 hints->x_scale = metrics->axis[AF_DIMENSION_HORZ].scale;
1324 hints->x_delta = metrics->axis[AF_DIMENSION_HORZ].delta;
1325 hints->y_scale = metrics->axis[AF_DIMENSION_VERT].scale;
1326 hints->y_delta = metrics->axis[AF_DIMENSION_VERT].delta;
1328 /* compute flags depending on render mode, etc. */
1329 mode = metrics->root.scaler.render_mode;
1331 #if 0 /* #ifdef AF_USE_WARPER */
1332 if ( mode == FT_RENDER_MODE_LCD || mode == FT_RENDER_MODE_LCD_V )
1334 metrics->root.scaler.render_mode = mode = FT_RENDER_MODE_NORMAL;
1336 #endif
1338 scaler_flags = hints->scaler_flags;
1339 other_flags = 0;
1342 * We snap the width of vertical stems for the monochrome and
1343 * horizontal LCD rendering targets only.
1345 if ( mode == FT_RENDER_MODE_MONO || mode == FT_RENDER_MODE_LCD )
1346 other_flags |= AF_LATIN_HINTS_HORZ_SNAP;
1349 * We snap the width of horizontal stems for the monochrome and
1350 * vertical LCD rendering targets only.
1352 if ( mode == FT_RENDER_MODE_MONO || mode == FT_RENDER_MODE_LCD_V )
1353 other_flags |= AF_LATIN_HINTS_VERT_SNAP;
1356 * We adjust stems to full pixels only if we don't use the `light' mode.
1358 if ( mode != FT_RENDER_MODE_LIGHT )
1359 other_flags |= AF_LATIN_HINTS_STEM_ADJUST;
1361 if ( mode == FT_RENDER_MODE_MONO )
1362 other_flags |= AF_LATIN_HINTS_MONO;
1365 * In `light' hinting mode we disable horizontal hinting completely.
1366 * We also do it if the face is italic.
1368 if ( mode == FT_RENDER_MODE_LIGHT ||
1369 (face->style_flags & FT_STYLE_FLAG_ITALIC) != 0 )
1370 scaler_flags |= AF_SCALER_FLAG_NO_HORIZONTAL;
1372 hints->scaler_flags = scaler_flags;
1373 hints->other_flags = other_flags;
1375 return 0;
1379 /*************************************************************************/
1380 /*************************************************************************/
1381 /***** *****/
1382 /***** L A T I N G L Y P H G R I D - F I T T I N G *****/
1383 /***** *****/
1384 /*************************************************************************/
1385 /*************************************************************************/
1387 /* snap a given width in scaled coordinates to one of the */
1388 /* current standard widths */
1390 static FT_Pos
1391 af_latin_snap_width( AF_Width widths,
1392 FT_Int count,
1393 FT_Pos width )
1395 int n;
1396 FT_Pos best = 64 + 32 + 2;
1397 FT_Pos reference = width;
1398 FT_Pos scaled;
1401 for ( n = 0; n < count; n++ )
1403 FT_Pos w;
1404 FT_Pos dist;
1407 w = widths[n].cur;
1408 dist = width - w;
1409 if ( dist < 0 )
1410 dist = -dist;
1411 if ( dist < best )
1413 best = dist;
1414 reference = w;
1418 scaled = FT_PIX_ROUND( reference );
1420 if ( width >= reference )
1422 if ( width < scaled + 48 )
1423 width = reference;
1425 else
1427 if ( width > scaled - 48 )
1428 width = reference;
1431 return width;
1435 /* compute the snapped width of a given stem */
1437 static FT_Pos
1438 af_latin_compute_stem_width( AF_GlyphHints hints,
1439 AF_Dimension dim,
1440 FT_Pos width,
1441 AF_Edge_Flags base_flags,
1442 AF_Edge_Flags stem_flags )
1444 AF_LatinMetrics metrics = (AF_LatinMetrics) hints->metrics;
1445 AF_LatinAxis axis = & metrics->axis[dim];
1446 FT_Pos dist = width;
1447 FT_Int sign = 0;
1448 FT_Int vertical = ( dim == AF_DIMENSION_VERT );
1451 if ( !AF_LATIN_HINTS_DO_STEM_ADJUST( hints ) ||
1452 axis->extra_light )
1453 return width;
1455 if ( dist < 0 )
1457 dist = -width;
1458 sign = 1;
1461 if ( ( vertical && !AF_LATIN_HINTS_DO_VERT_SNAP( hints ) ) ||
1462 ( !vertical && !AF_LATIN_HINTS_DO_HORZ_SNAP( hints ) ) )
1464 /* smooth hinting process: very lightly quantize the stem width */
1466 /* leave the widths of serifs alone */
1468 if ( ( stem_flags & AF_EDGE_SERIF ) && vertical && ( dist < 3 * 64 ) )
1469 goto Done_Width;
1471 else if ( ( base_flags & AF_EDGE_ROUND ) )
1473 if ( dist < 80 )
1474 dist = 64;
1476 else if ( dist < 56 )
1477 dist = 56;
1479 if ( axis->width_count > 0 )
1481 FT_Pos delta;
1484 /* compare to standard width */
1485 if ( axis->width_count > 0 )
1487 delta = dist - axis->widths[0].cur;
1489 if ( delta < 0 )
1490 delta = -delta;
1492 if ( delta < 40 )
1494 dist = axis->widths[0].cur;
1495 if ( dist < 48 )
1496 dist = 48;
1498 goto Done_Width;
1502 if ( dist < 3 * 64 )
1504 delta = dist & 63;
1505 dist &= -64;
1507 if ( delta < 10 )
1508 dist += delta;
1510 else if ( delta < 32 )
1511 dist += 10;
1513 else if ( delta < 54 )
1514 dist += 54;
1516 else
1517 dist += delta;
1519 else
1520 dist = ( dist + 32 ) & ~63;
1523 else
1525 /* strong hinting process: snap the stem width to integer pixels */
1526 FT_Pos org_dist = dist;
1529 dist = af_latin_snap_width( axis->widths, axis->width_count, dist );
1531 if ( vertical )
1533 /* in the case of vertical hinting, always round */
1534 /* the stem heights to integer pixels */
1536 if ( dist >= 64 )
1537 dist = ( dist + 16 ) & ~63;
1538 else
1539 dist = 64;
1541 else
1543 if ( AF_LATIN_HINTS_DO_MONO( hints ) )
1545 /* monochrome horizontal hinting: snap widths to integer pixels */
1546 /* with a different threshold */
1548 if ( dist < 64 )
1549 dist = 64;
1550 else
1551 dist = ( dist + 32 ) & ~63;
1553 else
1555 /* for horizontal anti-aliased hinting, we adopt a more subtle */
1556 /* approach: we strengthen small stems, round stems whose size */
1557 /* is between 1 and 2 pixels to an integer, otherwise nothing */
1559 if ( dist < 48 )
1560 dist = ( dist + 64 ) >> 1;
1562 else if ( dist < 128 )
1564 /* We only round to an integer width if the corresponding */
1565 /* distortion is less than 1/4 pixel. Otherwise this */
1566 /* makes everything worse since the diagonals, which are */
1567 /* not hinted, appear a lot bolder or thinner than the */
1568 /* vertical stems. */
1570 FT_Int delta;
1573 dist = ( dist + 22 ) & ~63;
1574 delta = dist - org_dist;
1575 if ( delta < 0 )
1576 delta = -delta;
1578 if (delta >= 16)
1580 dist = org_dist;
1581 if ( dist < 48 )
1582 dist = ( dist + 64 ) >> 1;
1585 else
1586 /* round otherwise to prevent color fringes in LCD mode */
1587 dist = ( dist + 32 ) & ~63;
1592 Done_Width:
1593 if ( sign )
1594 dist = -dist;
1596 return dist;
1600 /* align one stem edge relative to the previous stem edge */
1602 static void
1603 af_latin_align_linked_edge( AF_GlyphHints hints,
1604 AF_Dimension dim,
1605 AF_Edge base_edge,
1606 AF_Edge stem_edge )
1608 FT_Pos dist = stem_edge->opos - base_edge->opos;
1610 FT_Pos fitted_width = af_latin_compute_stem_width(
1611 hints, dim, dist,
1612 (AF_Edge_Flags)base_edge->flags,
1613 (AF_Edge_Flags)stem_edge->flags );
1616 stem_edge->pos = base_edge->pos + fitted_width;
1618 AF_LOG(( "LINK: edge %d (opos=%.2f) linked to (%.2f), "
1619 "dist was %.2f, now %.2f\n",
1620 stem_edge-hints->axis[dim].edges, stem_edge->opos / 64.0,
1621 stem_edge->pos / 64.0, dist / 64.0, fitted_width / 64.0 ));
1625 static void
1626 af_latin_align_serif_edge( AF_GlyphHints hints,
1627 AF_Edge base,
1628 AF_Edge serif )
1630 FT_UNUSED( hints );
1632 serif->pos = base->pos + (serif->opos - base->opos);
1636 /*************************************************************************/
1637 /*************************************************************************/
1638 /*************************************************************************/
1639 /**** ****/
1640 /**** E D G E H I N T I N G ****/
1641 /**** ****/
1642 /*************************************************************************/
1643 /*************************************************************************/
1644 /*************************************************************************/
1647 FT_LOCAL_DEF( void )
1648 af_latin_hint_edges( AF_GlyphHints hints,
1649 AF_Dimension dim )
1651 AF_AxisHints axis = &hints->axis[dim];
1652 AF_Edge edges = axis->edges;
1653 AF_Edge edge_limit = edges + axis->num_edges;
1654 FT_Int n_edges;
1655 AF_Edge edge;
1656 AF_Edge anchor = 0;
1657 FT_Int has_serifs = 0;
1660 /* we begin by aligning all stems relative to the blue zone */
1661 /* if needed -- that's only for horizontal edges */
1663 if ( dim == AF_DIMENSION_VERT && AF_HINTS_DO_BLUES( hints ) )
1665 for ( edge = edges; edge < edge_limit; edge++ )
1667 AF_Width blue;
1668 AF_Edge edge1, edge2;
1671 if ( edge->flags & AF_EDGE_DONE )
1672 continue;
1674 blue = edge->blue_edge;
1675 edge1 = NULL;
1676 edge2 = edge->link;
1678 if ( blue )
1680 edge1 = edge;
1682 else if ( edge2 && edge2->blue_edge )
1684 blue = edge2->blue_edge;
1685 edge1 = edge2;
1686 edge2 = edge;
1689 if ( !edge1 )
1690 continue;
1692 AF_LOG(( "BLUE: edge %d (opos=%.2f) snapped to (%.2f), "
1693 "was (%.2f)\n",
1694 edge1-edges, edge1->opos / 64.0, blue->fit / 64.0,
1695 edge1->pos / 64.0 ));
1697 edge1->pos = blue->fit;
1698 edge1->flags |= AF_EDGE_DONE;
1700 if ( edge2 && !edge2->blue_edge )
1702 af_latin_align_linked_edge( hints, dim, edge1, edge2 );
1703 edge2->flags |= AF_EDGE_DONE;
1706 if ( !anchor )
1707 anchor = edge;
1711 /* now we will align all stem edges, trying to maintain the */
1712 /* relative order of stems in the glyph */
1713 for ( edge = edges; edge < edge_limit; edge++ )
1715 AF_Edge edge2;
1718 if ( edge->flags & AF_EDGE_DONE )
1719 continue;
1721 /* skip all non-stem edges */
1722 edge2 = edge->link;
1723 if ( !edge2 )
1725 has_serifs++;
1726 continue;
1729 /* now align the stem */
1731 /* this should not happen, but it's better to be safe */
1732 if ( edge2->blue_edge )
1734 AF_LOG(( "ASSERTION FAILED for edge %d\n", edge2-edges ));
1736 af_latin_align_linked_edge( hints, dim, edge2, edge );
1737 edge->flags |= AF_EDGE_DONE;
1738 continue;
1741 if ( !anchor )
1743 FT_Pos org_len, org_center, cur_len;
1744 FT_Pos cur_pos1, error1, error2, u_off, d_off;
1747 org_len = edge2->opos - edge->opos;
1748 cur_len = af_latin_compute_stem_width(
1749 hints, dim, org_len,
1750 (AF_Edge_Flags)edge->flags,
1751 (AF_Edge_Flags)edge2->flags );
1752 if ( cur_len <= 64 )
1753 u_off = d_off = 32;
1754 else
1756 u_off = 38;
1757 d_off = 26;
1760 if ( cur_len < 96 )
1762 org_center = edge->opos + ( org_len >> 1 );
1764 cur_pos1 = FT_PIX_ROUND( org_center );
1766 error1 = org_center - ( cur_pos1 - u_off );
1767 if ( error1 < 0 )
1768 error1 = -error1;
1770 error2 = org_center - ( cur_pos1 + d_off );
1771 if ( error2 < 0 )
1772 error2 = -error2;
1774 if ( error1 < error2 )
1775 cur_pos1 -= u_off;
1776 else
1777 cur_pos1 += d_off;
1779 edge->pos = cur_pos1 - cur_len / 2;
1780 edge2->pos = edge->pos + cur_len;
1782 else
1783 edge->pos = FT_PIX_ROUND( edge->opos );
1785 AF_LOG(( "ANCHOR: edge %d (opos=%.2f) and %d (opos=%.2f) "
1786 "snapped to (%.2f) (%.2f)\n",
1787 edge-edges, edge->opos / 64.0,
1788 edge2-edges, edge2->opos / 64.0,
1789 edge->pos / 64.0, edge2->pos / 64.0 ));
1790 anchor = edge;
1792 edge->flags |= AF_EDGE_DONE;
1794 af_latin_align_linked_edge( hints, dim, edge, edge2 );
1796 else
1798 FT_Pos org_pos, org_len, org_center, cur_len;
1799 FT_Pos cur_pos1, cur_pos2, delta1, delta2;
1802 org_pos = anchor->pos + ( edge->opos - anchor->opos );
1803 org_len = edge2->opos - edge->opos;
1804 org_center = org_pos + ( org_len >> 1 );
1806 cur_len = af_latin_compute_stem_width(
1807 hints, dim, org_len,
1808 (AF_Edge_Flags)edge->flags,
1809 (AF_Edge_Flags)edge2->flags );
1811 if ( edge2->flags & AF_EDGE_DONE )
1812 edge->pos = edge2->pos - cur_len;
1814 else if ( cur_len < 96 )
1816 FT_Pos u_off, d_off;
1819 cur_pos1 = FT_PIX_ROUND( org_center );
1821 if (cur_len <= 64 )
1822 u_off = d_off = 32;
1823 else
1825 u_off = 38;
1826 d_off = 26;
1829 delta1 = org_center - ( cur_pos1 - u_off );
1830 if ( delta1 < 0 )
1831 delta1 = -delta1;
1833 delta2 = org_center - ( cur_pos1 + d_off );
1834 if ( delta2 < 0 )
1835 delta2 = -delta2;
1837 if ( delta1 < delta2 )
1838 cur_pos1 -= u_off;
1839 else
1840 cur_pos1 += d_off;
1842 edge->pos = cur_pos1 - cur_len / 2;
1843 edge2->pos = cur_pos1 + cur_len / 2;
1845 AF_LOG(( "STEM: %d (opos=%.2f) to %d (opos=%.2f) "
1846 "snapped to (%.2f) and (%.2f)\n",
1847 edge-edges, edge->opos / 64.0,
1848 edge2-edges, edge2->opos / 64.0,
1849 edge->pos / 64.0, edge2->pos / 64.0 ));
1851 else
1853 org_pos = anchor->pos + ( edge->opos - anchor->opos );
1854 org_len = edge2->opos - edge->opos;
1855 org_center = org_pos + ( org_len >> 1 );
1857 cur_len = af_latin_compute_stem_width(
1858 hints, dim, org_len,
1859 (AF_Edge_Flags)edge->flags,
1860 (AF_Edge_Flags)edge2->flags );
1862 cur_pos1 = FT_PIX_ROUND( org_pos );
1863 delta1 = cur_pos1 + ( cur_len >> 1 ) - org_center;
1864 if ( delta1 < 0 )
1865 delta1 = -delta1;
1867 cur_pos2 = FT_PIX_ROUND( org_pos + org_len ) - cur_len;
1868 delta2 = cur_pos2 + ( cur_len >> 1 ) - org_center;
1869 if ( delta2 < 0 )
1870 delta2 = -delta2;
1872 edge->pos = ( delta1 < delta2 ) ? cur_pos1 : cur_pos2;
1873 edge2->pos = edge->pos + cur_len;
1875 AF_LOG(( "STEM: %d (opos=%.2f) to %d (opos=%.2f) "
1876 "snapped to (%.2f) and (%.2f)\n",
1877 edge-edges, edge->opos / 64.0,
1878 edge2-edges, edge2->opos / 64.0,
1879 edge->pos / 64.0, edge2->pos / 64.0 ));
1882 edge->flags |= AF_EDGE_DONE;
1883 edge2->flags |= AF_EDGE_DONE;
1885 if ( edge > edges && edge->pos < edge[-1].pos )
1887 AF_LOG(( "BOUND: %d (pos=%.2f) to (%.2f)\n",
1888 edge-edges, edge->pos / 64.0, edge[-1].pos / 64.0 ));
1889 edge->pos = edge[-1].pos;
1894 /* make sure that lowercase m's maintain their symmetry */
1896 /* In general, lowercase m's have six vertical edges if they are sans */
1897 /* serif, or twelve if they are with serifs. This implementation is */
1898 /* based on that assumption, and seems to work very well with most */
1899 /* faces. However, if for a certain face this assumption is not */
1900 /* true, the m is just rendered like before. In addition, any stem */
1901 /* correction will only be applied to symmetrical glyphs (even if the */
1902 /* glyph is not an m), so the potential for unwanted distortion is */
1903 /* relatively low. */
1905 /* We don't handle horizontal edges since we can't easily assure that */
1906 /* the third (lowest) stem aligns with the base line; it might end up */
1907 /* one pixel higher or lower. */
1909 n_edges = edge_limit - edges;
1910 if ( dim == AF_DIMENSION_HORZ && ( n_edges == 6 || n_edges == 12 ) )
1912 AF_Edge edge1, edge2, edge3;
1913 FT_Pos dist1, dist2, span, delta;
1916 if ( n_edges == 6 )
1918 edge1 = edges;
1919 edge2 = edges + 2;
1920 edge3 = edges + 4;
1922 else
1924 edge1 = edges + 1;
1925 edge2 = edges + 5;
1926 edge3 = edges + 9;
1929 dist1 = edge2->opos - edge1->opos;
1930 dist2 = edge3->opos - edge2->opos;
1932 span = dist1 - dist2;
1933 if ( span < 0 )
1934 span = -span;
1936 if ( span < 8 )
1938 delta = edge3->pos - ( 2 * edge2->pos - edge1->pos );
1939 edge3->pos -= delta;
1940 if ( edge3->link )
1941 edge3->link->pos -= delta;
1943 /* move the serifs along with the stem */
1944 if ( n_edges == 12 )
1946 ( edges + 8 )->pos -= delta;
1947 ( edges + 11 )->pos -= delta;
1950 edge3->flags |= AF_EDGE_DONE;
1951 if ( edge3->link )
1952 edge3->link->flags |= AF_EDGE_DONE;
1956 if ( has_serifs || !anchor )
1959 * now hint the remaining edges (serifs and single) in order
1960 * to complete our processing
1962 for ( edge = edges; edge < edge_limit; edge++ )
1964 FT_Pos delta;
1967 if ( edge->flags & AF_EDGE_DONE )
1968 continue;
1970 delta = 1000;
1972 if ( edge->serif )
1974 delta = edge->serif->opos - edge->opos;
1975 if ( delta < 0 )
1976 delta = -delta;
1979 if ( delta < 64 + 16 )
1981 af_latin_align_serif_edge( hints, edge->serif, edge );
1982 AF_LOG(( "SERIF: edge %d (opos=%.2f) serif to %d (opos=%.2f) "
1983 "aligned to (%.2f)\n",
1984 edge-edges, edge->opos / 64.0,
1985 edge->serif - edges, edge->serif->opos / 64.0,
1986 edge->pos / 64.0 ));
1988 else if ( !anchor )
1990 AF_LOG(( "SERIF_ANCHOR: edge %d (opos=%.2f) snapped to (%.2f)\n",
1991 edge-edges, edge->opos / 64.0, edge->pos / 64.0 ));
1992 edge->pos = FT_PIX_ROUND( edge->opos );
1993 anchor = edge;
1995 else
1997 AF_Edge before, after;
2000 for ( before = edge - 1; before >= edges; before-- )
2001 if ( before->flags & AF_EDGE_DONE )
2002 break;
2004 for ( after = edge + 1; after < edge_limit; after++ )
2005 if ( after->flags & AF_EDGE_DONE )
2006 break;
2008 if ( before >= edges && before < edge &&
2009 after < edge_limit && after > edge )
2011 edge->pos = before->pos +
2012 FT_MulDiv( edge->opos - before->opos,
2013 after->pos - before->pos,
2014 after->opos - before->opos );
2015 AF_LOG(( "SERIF_LINK1: edge %d (opos=%.2f) snapped to (%.2f) "
2016 "from %d (opos=%.2f)\n",
2017 edge-edges, edge->opos / 64.0,
2018 edge->pos / 64.0, before - edges,
2019 before->opos / 64.0 ));
2021 else
2023 edge->pos = anchor->pos +
2024 ( ( edge->opos - anchor->opos + 16 ) & ~31 );
2025 AF_LOG(( "SERIF_LINK2: edge %d (opos=%.2f) snapped to (%.2f)\n",
2026 edge-edges, edge->opos / 64.0, edge->pos / 64.0 ));
2030 edge->flags |= AF_EDGE_DONE;
2032 if ( edge > edges && edge->pos < edge[-1].pos )
2033 edge->pos = edge[-1].pos;
2035 if ( edge + 1 < edge_limit &&
2036 edge[1].flags & AF_EDGE_DONE &&
2037 edge->pos > edge[1].pos )
2038 edge->pos = edge[1].pos;
2044 static FT_Error
2045 af_latin_hints_apply( AF_GlyphHints hints,
2046 FT_Outline* outline,
2047 AF_LatinMetrics metrics )
2049 FT_Error error;
2050 int dim;
2053 error = af_glyph_hints_reload( hints, outline, 1 );
2054 if ( error )
2055 goto Exit;
2057 /* analyze glyph outline */
2058 #ifdef AF_USE_WARPER
2059 if ( metrics->root.scaler.render_mode == FT_RENDER_MODE_LIGHT ||
2060 AF_HINTS_DO_HORIZONTAL( hints ) )
2061 #else
2062 if ( AF_HINTS_DO_HORIZONTAL( hints ) )
2063 #endif
2065 error = af_latin_hints_detect_features( hints, AF_DIMENSION_HORZ );
2066 if ( error )
2067 goto Exit;
2070 if ( AF_HINTS_DO_VERTICAL( hints ) )
2072 error = af_latin_hints_detect_features( hints, AF_DIMENSION_VERT );
2073 if ( error )
2074 goto Exit;
2076 af_latin_hints_compute_blue_edges( hints, metrics );
2079 /* grid-fit the outline */
2080 for ( dim = 0; dim < AF_DIMENSION_MAX; dim++ )
2082 #ifdef AF_USE_WARPER
2083 if ( ( dim == AF_DIMENSION_HORZ &&
2084 metrics->root.scaler.render_mode == FT_RENDER_MODE_LIGHT ) )
2086 AF_WarperRec warper;
2087 FT_Fixed scale;
2088 FT_Pos delta;
2091 af_warper_compute( &warper, hints, dim, &scale, &delta );
2092 af_glyph_hints_scale_dim( hints, dim, scale, delta );
2093 continue;
2095 #endif
2097 if ( ( dim == AF_DIMENSION_HORZ && AF_HINTS_DO_HORIZONTAL( hints ) ) ||
2098 ( dim == AF_DIMENSION_VERT && AF_HINTS_DO_VERTICAL( hints ) ) )
2100 af_latin_hint_edges( hints, (AF_Dimension)dim );
2101 af_glyph_hints_align_edge_points( hints, (AF_Dimension)dim );
2102 af_glyph_hints_align_strong_points( hints, (AF_Dimension)dim );
2103 af_glyph_hints_align_weak_points( hints, (AF_Dimension)dim );
2106 af_glyph_hints_save( hints, outline );
2108 Exit:
2109 return error;
2113 /*************************************************************************/
2114 /*************************************************************************/
2115 /***** *****/
2116 /***** L A T I N S C R I P T C L A S S *****/
2117 /***** *****/
2118 /*************************************************************************/
2119 /*************************************************************************/
2122 /* XXX: this should probably fine tuned to differentiate better between */
2123 /* scripts... */
2125 static const AF_Script_UniRangeRec af_latin_uniranges[] =
2127 { 0x0020, 0x007F }, /* Basic Latin (no control characters) */
2128 { 0x00A0, 0x00FF }, /* Latin-1 Supplement (no control characters) */
2129 { 0x0100, 0x017F }, /* Latin Extended-A */
2130 { 0x0180, 0x024F }, /* Latin Extended-B */
2131 { 0x0250, 0x02AF }, /* IPA Extensions */
2132 { 0x02B0, 0x02FF }, /* Spacing Modifier Letters */
2133 { 0x0300, 0x036F }, /* Combining Diacritical Marks */
2134 { 0x0370, 0x03FF }, /* Greek and Coptic */
2135 { 0x0400, 0x04FF }, /* Cyrillic */
2136 { 0x0500, 0x052F }, /* Cyrillic Supplement */
2137 { 0x1D00, 0x1D7F }, /* Phonetic Extensions */
2138 { 0x1D80, 0x1DBF }, /* Phonetic Extensions Supplement */
2139 { 0x1DC0, 0x1DFF }, /* Combining Diacritical Marks Supplement */
2140 { 0x1E00, 0x1EFF }, /* Latin Extended Additional */
2141 { 0x1F00, 0x1FFF }, /* Greek Extended */
2142 { 0x2000, 0x206F }, /* General Punctuation */
2143 { 0x2070, 0x209F }, /* Superscripts and Subscripts */
2144 { 0x20A0, 0x20CF }, /* Currency Symbols */
2145 { 0x2150, 0x218F }, /* Number Forms */
2146 { 0x2460, 0x24FF }, /* Enclosed Alphanumerics */
2147 { 0 , 0 }
2151 FT_CALLBACK_TABLE_DEF const AF_ScriptClassRec
2152 af_latin_script_class =
2154 AF_SCRIPT_LATIN,
2155 af_latin_uniranges,
2157 sizeof( AF_LatinMetricsRec ),
2159 (AF_Script_InitMetricsFunc) af_latin_metrics_init,
2160 (AF_Script_ScaleMetricsFunc)af_latin_metrics_scale,
2161 (AF_Script_DoneMetricsFunc) NULL,
2163 (AF_Script_InitHintsFunc) af_latin_hints_init,
2164 (AF_Script_ApplyHintsFunc) af_latin_hints_apply
2168 /* END */