[CMAKE]
[reactos.git] / lib / 3rdparty / freetype / src / autofit / aflatin.c
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, 2010 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 /***************************************************************************/
17
18
19 #include <ft2build.h>
20 #include FT_ADVANCES_H
21
22 #include "aflatin.h"
23 #include "aferrors.h"
24
25
26 #ifdef AF_USE_WARPER
27 #include "afwarp.h"
28 #endif
29
30
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 /*************************************************************************/
38
39 FT_LOCAL_DEF( void )
40 af_latin_metrics_init_widths( AF_LatinMetrics metrics,
41 FT_Face face,
42 FT_ULong charcode )
43 {
44 /* scan the array of segments in each direction */
45 AF_GlyphHintsRec hints[1];
46
47
48 af_glyph_hints_init( hints, face->memory );
49
50 metrics->axis[AF_DIMENSION_HORZ].width_count = 0;
51 metrics->axis[AF_DIMENSION_VERT].width_count = 0;
52
53 {
54 FT_Error error;
55 FT_UInt glyph_index;
56 int dim;
57 AF_LatinMetricsRec dummy[1];
58 AF_Scaler scaler = &dummy->root.scaler;
59
60
61 glyph_index = FT_Get_Char_Index( face, charcode );
62 if ( glyph_index == 0 )
63 goto Exit;
64
65 error = FT_Load_Glyph( face, glyph_index, FT_LOAD_NO_SCALE );
66 if ( error || face->glyph->outline.n_points <= 0 )
67 goto Exit;
68
69 FT_ZERO( dummy );
70
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;
77
78 af_glyph_hints_rescale( hints, (AF_ScriptMetrics)dummy );
79
80 error = af_glyph_hints_reload( hints, &face->glyph->outline );
81 if ( error )
82 goto Exit;
83
84 for ( dim = 0; dim < AF_DIMENSION_MAX; dim++ )
85 {
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;
90
91
92 error = af_latin_hints_compute_segments( hints,
93 (AF_Dimension)dim );
94 if ( error )
95 goto Exit;
96
97 af_latin_hints_link_segments( hints,
98 (AF_Dimension)dim );
99
100 seg = axhints->segments;
101 limit = seg + axhints->num_segments;
102
103 for ( ; seg < limit; seg++ )
104 {
105 link = seg->link;
106
107 /* we only consider stem segments there! */
108 if ( link && link->link == seg && link > seg )
109 {
110 FT_Pos dist;
111
112
113 dist = seg->pos - link->pos;
114 if ( dist < 0 )
115 dist = -dist;
116
117 if ( num_widths < AF_LATIN_MAX_WIDTHS )
118 axis->widths[ num_widths++ ].org = dist;
119 }
120 }
121
122 af_sort_widths( num_widths, axis->widths );
123 axis->width_count = num_widths;
124 }
125
126 Exit:
127 for ( dim = 0; dim < AF_DIMENSION_MAX; dim++ )
128 {
129 AF_LatinAxis axis = &metrics->axis[dim];
130 FT_Pos stdw;
131
132
133 stdw = ( axis->width_count > 0 )
134 ? axis->widths[0].org
135 : AF_LATIN_CONSTANT( metrics, 50 );
136
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;
141 }
142 }
143
144 af_glyph_hints_done( hints );
145 }
146
147
148
149 #define AF_LATIN_MAX_TEST_CHARACTERS 12
150
151
152 static const char af_latin_blue_chars[AF_LATIN_MAX_BLUES]
153 [AF_LATIN_MAX_TEST_CHARACTERS + 1] =
154 {
155 "THEZOCQS",
156 "HEZLOCUS",
157 "fijkdbh",
158 "xzroesc",
159 "xzroesc",
160 "pqgjy"
161 };
162
163
164 static void
165 af_latin_metrics_init_blues( AF_LatinMetrics metrics,
166 FT_Face face )
167 {
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;
177
178
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') */
182
183 AF_LOG(( "blue zones computation\n" ));
184 AF_LOG(( "------------------------------------------------\n" ));
185
186 for ( bb = 0; bb < AF_LATIN_BLUE_MAX; bb++ )
187 {
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;
192
193
194 AF_LOG(( "blue %3d: ", bb ));
195
196 num_flats = 0;
197 num_rounds = 0;
198
199 for ( ; p < limit && *p; p++ )
200 {
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;
206
207
208 AF_LOG(( "'%c'", *p ));
209
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;
214
215 error = FT_Load_Glyph( face, glyph_index, FT_LOAD_NO_SCALE );
216 if ( error || glyph->outline.n_points <= 0 )
217 continue;
218
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 */
225
226 {
227 FT_Int nn;
228 FT_Int first = 0;
229 FT_Int last = -1;
230
231
232 for ( nn = 0; nn < glyph->outline.n_contours; first = last+1, nn++ )
233 {
234 FT_Int old_best_point = best_point;
235 FT_Int pp;
236
237
238 last = glyph->outline.contours[nn];
239
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;
245
246 if ( AF_LATIN_IS_TOP_BLUE( bb ) )
247 {
248 for ( pp = first; pp <= last; pp++ )
249 if ( best_point < 0 || points[pp].y > best_y )
250 {
251 best_point = pp;
252 best_y = points[pp].y;
253 }
254 }
255 else
256 {
257 for ( pp = first; pp <= last; pp++ )
258 if ( best_point < 0 || points[pp].y < best_y )
259 {
260 best_point = pp;
261 best_y = points[pp].y;
262 }
263 }
264
265 if ( best_point != old_best_point )
266 {
267 best_first = first;
268 best_last = last;
269 }
270 }
271 AF_LOG(( "%5d", best_y ));
272 }
273
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 )
278 {
279 FT_Int prev, next;
280 FT_Pos dist;
281
282
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;
287
288 do
289 {
290 if ( prev > best_first )
291 prev--;
292 else
293 prev = best_last;
294
295 dist = points[prev].y - best_y;
296 if ( dist < -5 || dist > 5 )
297 break;
298
299 } while ( prev != best_point );
300
301 do
302 {
303 if ( next < best_last )
304 next++;
305 else
306 next = best_first;
307
308 dist = points[next].y - best_y;
309 if ( dist < -5 || dist > 5 )
310 break;
311
312 } while ( next != best_point );
313
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 );
318
319 AF_LOG(( "%c ", round ? 'r' : 'f' ));
320 }
321
322 if ( round )
323 rounds[num_rounds++] = best_y;
324 else
325 flats[num_flats++] = best_y;
326 }
327
328 AF_LOG(( "\n" ));
329
330 if ( num_flats == 0 && num_rounds == 0 )
331 {
332 /*
333 * we couldn't find a single glyph to compute this blue zone,
334 * we will simply ignore it then
335 */
336 AF_LOG(( "empty\n" ));
337 continue;
338 }
339
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 );
345
346 blue = & axis->blues[axis->blue_count];
347 blue_ref = & blue->ref.org;
348 blue_shoot = & blue->shoot.org;
349
350 axis->blue_count++;
351
352 if ( num_flats == 0 )
353 {
354 *blue_ref =
355 *blue_shoot = rounds[num_rounds / 2];
356 }
357 else if ( num_rounds == 0 )
358 {
359 *blue_ref =
360 *blue_shoot = flats[num_flats / 2];
361 }
362 else
363 {
364 *blue_ref = flats[num_flats / 2];
365 *blue_shoot = rounds[num_rounds / 2];
366 }
367
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 )
372 {
373 FT_Pos ref = *blue_ref;
374 FT_Pos shoot = *blue_shoot;
375 FT_Bool over_ref = FT_BOOL( shoot > ref );
376
377
378 if ( AF_LATIN_IS_TOP_BLUE( bb ) ^ over_ref )
379 *blue_shoot = *blue_ref = ( shoot + ref ) / 2;
380 }
381
382 blue->flags = 0;
383 if ( AF_LATIN_IS_TOP_BLUE( bb ) )
384 blue->flags |= AF_LATIN_BLUE_TOP;
385
386 /*
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.
390 */
391 if ( bb == AF_LATIN_BLUE_SMALL_TOP )
392 blue->flags |= AF_LATIN_BLUE_ADJUSTMENT;
393
394 AF_LOG(( "-- ref = %ld, shoot = %ld\n", *blue_ref, *blue_shoot ));
395 }
396
397 return;
398 }
399
400
401 FT_LOCAL_DEF( void )
402 af_latin_metrics_check_digits( AF_LatinMetrics metrics,
403 FT_Face face )
404 {
405 FT_UInt i;
406 FT_Bool started = 0, same_width = 1;
407 FT_Fixed advance, old_advance = 0;
408
409
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++ )
413 {
414 FT_UInt glyph_index;
415
416
417 glyph_index = FT_Get_Char_Index( face, i );
418 if ( glyph_index == 0 )
419 continue;
420
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;
427
428 if ( started )
429 {
430 if ( advance != old_advance )
431 {
432 same_width = 0;
433 break;
434 }
435 }
436 else
437 {
438 old_advance = advance;
439 started = 1;
440 }
441 }
442
443 metrics->root.digits_have_same_width = same_width;
444 }
445
446
447 FT_LOCAL_DEF( FT_Error )
448 af_latin_metrics_init( AF_LatinMetrics metrics,
449 FT_Face face )
450 {
451 FT_Error error = AF_Err_Ok;
452 FT_CharMap oldmap = face->charmap;
453 FT_UInt ee;
454
455 static const FT_Encoding latin_encodings[] =
456 {
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 */
462 };
463
464
465 metrics->units_per_em = face->units_per_EM;
466
467 /* do we have a latin charmap in there? */
468 for ( ee = 0; latin_encodings[ee] != FT_ENCODING_NONE; ee++ )
469 {
470 error = FT_Select_Charmap( face, latin_encodings[ee] );
471 if ( !error )
472 break;
473 }
474
475 if ( !error )
476 {
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 );
481 }
482
483 FT_Set_Charmap( face, oldmap );
484 return AF_Err_Ok;
485 }
486
487
488 static void
489 af_latin_metrics_scale_dim( AF_LatinMetrics metrics,
490 AF_Scaler scaler,
491 AF_Dimension dim )
492 {
493 FT_Fixed scale;
494 FT_Pos delta;
495 AF_LatinAxis axis;
496 FT_UInt nn;
497
498
499 if ( dim == AF_DIMENSION_HORZ )
500 {
501 scale = scaler->x_scale;
502 delta = scaler->x_delta;
503 }
504 else
505 {
506 scale = scaler->y_scale;
507 delta = scaler->y_delta;
508 }
509
510 axis = &metrics->axis[dim];
511
512 if ( axis->org_scale == scale && axis->org_delta == delta )
513 return;
514
515 axis->org_scale = scale;
516 axis->org_delta = delta;
517
518 /*
519 * correct X and Y scale to optimize the alignment of the top of small
520 * letters to the pixel grid
521 */
522 {
523 AF_LatinAxis Axis = &metrics->axis[AF_DIMENSION_VERT];
524 AF_LatinBlue blue = NULL;
525
526
527 for ( nn = 0; nn < Axis->blue_count; nn++ )
528 {
529 if ( Axis->blues[nn].flags & AF_LATIN_BLUE_ADJUSTMENT )
530 {
531 blue = &Axis->blues[nn];
532 break;
533 }
534 }
535
536 if ( blue )
537 {
538 FT_Pos scaled = FT_MulFix( blue->shoot.org, scaler->y_scale );
539 FT_Pos fitted = ( scaled + 40 ) & ~63;
540
541
542 if ( scaled != fitted )
543 {
544 #if 0
545 if ( dim == AF_DIMENSION_HORZ )
546 {
547 if ( fitted < scaled )
548 scale -= scale / 50; /* scale *= 0.98 */
549 }
550 else
551 #endif
552 if ( dim == AF_DIMENSION_VERT )
553 {
554 scale = FT_MulDiv( scale, fitted, scaled );
555 }
556 }
557 }
558 }
559
560 axis->scale = scale;
561 axis->delta = delta;
562
563 if ( dim == AF_DIMENSION_HORZ )
564 {
565 metrics->root.scaler.x_scale = scale;
566 metrics->root.scaler.x_delta = delta;
567 }
568 else
569 {
570 metrics->root.scaler.y_scale = scale;
571 metrics->root.scaler.y_delta = delta;
572 }
573
574 /* scale the standard widths */
575 for ( nn = 0; nn < axis->width_count; nn++ )
576 {
577 AF_Width width = axis->widths + nn;
578
579
580 width->cur = FT_MulFix( width->org, scale );
581 width->fit = width->cur;
582 }
583
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 );
588
589 if ( dim == AF_DIMENSION_VERT )
590 {
591 /* scale the blue zones */
592 for ( nn = 0; nn < axis->blue_count; nn++ )
593 {
594 AF_LatinBlue blue = &axis->blues[nn];
595 FT_Pos dist;
596
597
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;
603
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 )
607 {
608 FT_Pos delta1, delta2;
609
610
611 delta1 = blue->shoot.org - blue->ref.org;
612 delta2 = delta1;
613 if ( delta1 < 0 )
614 delta2 = -delta2;
615
616 delta2 = FT_MulFix( delta2, scale );
617
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 );
624
625 if ( delta1 < 0 )
626 delta2 = -delta2;
627
628 blue->ref.fit = FT_PIX_ROUND( blue->ref.cur );
629 blue->shoot.fit = blue->ref.fit + delta2;
630
631 blue->flags |= AF_LATIN_BLUE_ACTIVE;
632 }
633 }
634 }
635 }
636
637
638 FT_LOCAL_DEF( void )
639 af_latin_metrics_scale( AF_LatinMetrics metrics,
640 AF_Scaler scaler )
641 {
642 metrics->root.scaler.render_mode = scaler->render_mode;
643 metrics->root.scaler.face = scaler->face;
644
645 af_latin_metrics_scale_dim( metrics, scaler, AF_DIMENSION_HORZ );
646 af_latin_metrics_scale_dim( metrics, scaler, AF_DIMENSION_VERT );
647 }
648
649
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 /*************************************************************************/
657
658 FT_LOCAL_DEF( FT_Error )
659 af_latin_hints_compute_segments( AF_GlyphHints hints,
660 AF_Dimension dim )
661 {
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;
670
671
672 FT_ZERO( &seg0 );
673 seg0.score = 32000;
674 seg0.flags = AF_EDGE_NORMAL;
675
676 major_dir = (AF_Direction)FT_ABS( axis->major_dir );
677 segment_dir = major_dir;
678
679 axis->num_segments = 0;
680
681 /* set up (u,v) in each point */
682 if ( dim == AF_DIMENSION_HORZ )
683 {
684 AF_Point point = hints->points;
685 AF_Point limit = point + hints->num_points;
686
687
688 for ( ; point < limit; point++ )
689 {
690 point->u = point->fx;
691 point->v = point->fy;
692 }
693 }
694 else
695 {
696 AF_Point point = hints->points;
697 AF_Point limit = point + hints->num_points;
698
699
700 for ( ; point < limit; point++ )
701 {
702 point->u = point->fy;
703 point->v = point->fx;
704 }
705 }
706
707 /* do each contour separately */
708 for ( ; contour < contour_limit; contour++ )
709 {
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;
716
717
718 if ( point == last ) /* skip singletons -- just in case */
719 continue;
720
721 if ( FT_ABS( last->out_dir ) == major_dir &&
722 FT_ABS( point->out_dir ) == major_dir )
723 {
724 /* we are already on an edge, try to locate its start */
725 last = point;
726
727 for (;;)
728 {
729 point = point->prev;
730 if ( FT_ABS( point->out_dir ) != major_dir )
731 {
732 point = point->next;
733 break;
734 }
735 if ( point == last )
736 break;
737 }
738 }
739
740 last = point;
741 passed = 0;
742
743 for (;;)
744 {
745 FT_Pos u, v;
746
747
748 if ( on_edge )
749 {
750 u = point->u;
751 if ( u < min_pos )
752 min_pos = u;
753 if ( u > max_pos )
754 max_pos = u;
755
756 if ( point->out_dir != segment_dir || point == last )
757 {
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 );
761
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;
767
768 /* compute segment size */
769 min_pos = max_pos = point->v;
770
771 v = segment->first->v;
772 if ( v < min_pos )
773 min_pos = v;
774 if ( v > max_pos )
775 max_pos = v;
776
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 );
781
782 on_edge = 0;
783 segment = NULL;
784 /* fallthrough */
785 }
786 }
787
788 /* now exit if we are at the start/end point */
789 if ( point == last )
790 {
791 if ( passed )
792 break;
793 passed = 1;
794 }
795
796 if ( !on_edge && FT_ABS( point->out_dir ) == major_dir )
797 {
798 /* this is the start of a new segment! */
799 segment_dir = (AF_Direction)point->out_dir;
800
801 /* clear all segment fields */
802 error = af_axis_hints_new_segment( axis, memory, &segment );
803 if ( error )
804 goto Exit;
805
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;
813 }
814
815 point = point->next;
816 }
817
818 } /* contours */
819
820
821 /* now slightly increase the height of segments when this makes */
822 /* sense -- this is used to better detect and ignore serifs */
823 {
824 AF_Segment segments = axis->segments;
825 AF_Segment segments_end = segments + axis->num_segments;
826
827
828 for ( segment = segments; segment < segments_end; segment++ )
829 {
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;
834
835
836 if ( first == last )
837 continue;
838
839 if ( first_v < last_v )
840 {
841 AF_Point p;
842
843
844 p = first->prev;
845 if ( p->v < first_v )
846 segment->height = (FT_Short)( segment->height +
847 ( ( first_v - p->v ) >> 1 ) );
848
849 p = last->next;
850 if ( p->v > last_v )
851 segment->height = (FT_Short)( segment->height +
852 ( ( p->v - last_v ) >> 1 ) );
853 }
854 else
855 {
856 AF_Point p;
857
858
859 p = first->prev;
860 if ( p->v > first_v )
861 segment->height = (FT_Short)( segment->height +
862 ( ( p->v - first_v ) >> 1 ) );
863
864 p = last->next;
865 if ( p->v < last_v )
866 segment->height = (FT_Short)( segment->height +
867 ( ( last_v - p->v ) >> 1 ) );
868 }
869 }
870 }
871
872 Exit:
873 return error;
874 }
875
876
877 FT_LOCAL_DEF( void )
878 af_latin_hints_link_segments( AF_GlyphHints hints,
879 AF_Dimension dim )
880 {
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;
886
887
888 len_threshold = AF_LATIN_CONSTANT( hints->metrics, 8 );
889 if ( len_threshold == 0 )
890 len_threshold = 1;
891
892 len_score = AF_LATIN_CONSTANT( hints->metrics, 6000 );
893
894 /* now compare each segment to the others */
895 for ( seg1 = segments; seg1 < segment_limit; seg1++ )
896 {
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;
901
902 for ( seg2 = segments; seg2 < segment_limit; seg2++ )
903 if ( seg1->dir + seg2->dir == 0 && seg2->pos > seg1->pos )
904 {
905 FT_Pos pos1 = seg1->pos;
906 FT_Pos pos2 = seg2->pos;
907 FT_Pos dist = pos2 - pos1;
908
909
910 if ( dist < 0 )
911 dist = -dist;
912
913 {
914 FT_Pos min = seg1->min_coord;
915 FT_Pos max = seg1->max_coord;
916 FT_Pos len, score;
917
918
919 if ( min < seg2->min_coord )
920 min = seg2->min_coord;
921
922 if ( max > seg2->max_coord )
923 max = seg2->max_coord;
924
925 len = max - min;
926 if ( len >= len_threshold )
927 {
928 score = dist + len_score / len;
929
930 if ( score < seg1->score )
931 {
932 seg1->score = score;
933 seg1->link = seg2;
934 }
935
936 if ( score < seg2->score )
937 {
938 seg2->score = score;
939 seg2->link = seg1;
940 }
941 }
942 }
943 }
944 }
945
946 /* now, compute the `serif' segments */
947 for ( seg1 = segments; seg1 < segment_limit; seg1++ )
948 {
949 seg2 = seg1->link;
950
951 if ( seg2 )
952 {
953 if ( seg2->link != seg1 )
954 {
955 seg1->link = 0;
956 seg1->serif = seg2->link;
957 }
958 }
959 }
960 }
961
962
963 FT_LOCAL_DEF( FT_Error )
964 af_latin_hints_compute_edges( AF_GlyphHints hints,
965 AF_Dimension dim )
966 {
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];
971
972 AF_Segment segments = axis->segments;
973 AF_Segment segment_limit = segments + axis->num_segments;
974 AF_Segment seg;
975
976 AF_Direction up_dir;
977 FT_Fixed scale;
978 FT_Pos edge_distance_threshold;
979 FT_Pos segment_length_threshold;
980
981
982 axis->num_edges = 0;
983
984 scale = ( dim == AF_DIMENSION_HORZ ) ? hints->x_scale
985 : hints->y_scale;
986
987 up_dir = ( dim == AF_DIMENSION_HORZ ) ? AF_DIR_UP
988 : AF_DIR_RIGHT;
989
990 /*
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.
994 */
995 if ( dim == AF_DIMENSION_HORZ )
996 segment_length_threshold = FT_DivFix( 64, hints->y_scale );
997 else
998 segment_length_threshold = 0;
999
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 /*********************************************************************/
1015
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;
1020
1021 edge_distance_threshold = FT_DivFix( edge_distance_threshold,
1022 scale );
1023
1024 for ( seg = segments; seg < segment_limit; seg++ )
1025 {
1026 AF_Edge found = 0;
1027 FT_Int ee;
1028
1029
1030 if ( seg->height < segment_length_threshold )
1031 continue;
1032
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;
1038
1039 /* look for an edge corresponding to the segment */
1040 for ( ee = 0; ee < axis->num_edges; ee++ )
1041 {
1042 AF_Edge edge = axis->edges + ee;
1043 FT_Pos dist;
1044
1045
1046 dist = seg->pos - edge->fpos;
1047 if ( dist < 0 )
1048 dist = -dist;
1049
1050 if ( dist < edge_distance_threshold && edge->dir == seg->dir )
1051 {
1052 found = edge;
1053 break;
1054 }
1055 }
1056
1057 if ( !found )
1058 {
1059 AF_Edge edge;
1060
1061
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;
1069
1070 /* add the segment to the new edge's list */
1071 FT_ZERO( edge );
1072
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;
1079 }
1080 else
1081 {
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;
1087 }
1088 }
1089
1090
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 /*********************************************************************/
1102
1103 /* first of all, set the `edge' field in each segment -- this is */
1104 /* required in order to compute edge links */
1105
1106 /*
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.
1110 */
1111 {
1112 AF_Edge edges = axis->edges;
1113 AF_Edge edge_limit = edges + axis->num_edges;
1114 AF_Edge edge;
1115
1116
1117 for ( edge = edges; edge < edge_limit; edge++ )
1118 {
1119 seg = edge->first;
1120 if ( seg )
1121 do
1122 {
1123 seg->edge = edge;
1124 seg = seg->edge_next;
1125
1126 } while ( seg != edge->first );
1127 }
1128
1129 /* now, compute each edge properties */
1130 for ( edge = edges; edge < edge_limit; edge++ )
1131 {
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 */
1136
1137
1138 seg = edge->first;
1139
1140 do
1141 {
1142 FT_Bool is_serif;
1143
1144
1145 /* check for roundness of segment */
1146 if ( seg->flags & AF_EDGE_ROUND )
1147 is_round++;
1148 else
1149 is_straight++;
1150
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;
1156
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 );
1162
1163 if ( ( seg->link && seg->link->edge != NULL ) || is_serif )
1164 {
1165 AF_Edge edge2;
1166 AF_Segment seg2;
1167
1168
1169 edge2 = edge->link;
1170 seg2 = seg->link;
1171
1172 if ( is_serif )
1173 {
1174 seg2 = seg->serif;
1175 edge2 = edge->serif;
1176 }
1177
1178 if ( edge2 )
1179 {
1180 FT_Pos edge_delta;
1181 FT_Pos seg_delta;
1182
1183
1184 edge_delta = edge->fpos - edge2->fpos;
1185 if ( edge_delta < 0 )
1186 edge_delta = -edge_delta;
1187
1188 seg_delta = seg->pos - seg2->pos;
1189 if ( seg_delta < 0 )
1190 seg_delta = -seg_delta;
1191
1192 if ( seg_delta < edge_delta )
1193 edge2 = seg2->edge;
1194 }
1195 else
1196 edge2 = seg2->edge;
1197
1198 if ( is_serif )
1199 {
1200 edge->serif = edge2;
1201 edge2->flags |= AF_EDGE_SERIF;
1202 }
1203 else
1204 edge->link = edge2;
1205 }
1206
1207 seg = seg->edge_next;
1208
1209 } while ( seg != edge->first );
1210
1211 /* set the round/straight flags */
1212 edge->flags = AF_EDGE_NORMAL;
1213
1214 if ( is_round > 0 && is_round >= is_straight )
1215 edge->flags |= AF_EDGE_ROUND;
1216
1217 #if 0
1218 /* set the edge's main direction */
1219 edge->dir = AF_DIR_NONE;
1220
1221 if ( ups > downs )
1222 edge->dir = (FT_Char)up_dir;
1223
1224 else if ( ups < downs )
1225 edge->dir = (FT_Char)-up_dir;
1226
1227 else if ( ups == downs )
1228 edge->dir = 0; /* both up and down! */
1229 #endif
1230
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 */
1234
1235 if ( edge->serif && edge->link )
1236 edge->serif = 0;
1237 }
1238 }
1239
1240 Exit:
1241 return error;
1242 }
1243
1244
1245 FT_LOCAL_DEF( FT_Error )
1246 af_latin_hints_detect_features( AF_GlyphHints hints,
1247 AF_Dimension dim )
1248 {
1249 FT_Error error;
1250
1251
1252 error = af_latin_hints_compute_segments( hints, dim );
1253 if ( !error )
1254 {
1255 af_latin_hints_link_segments( hints, dim );
1256
1257 error = af_latin_hints_compute_edges( hints, dim );
1258 }
1259 return error;
1260 }
1261
1262
1263 FT_LOCAL_DEF( void )
1264 af_latin_hints_compute_blue_edges( AF_GlyphHints hints,
1265 AF_LatinMetrics metrics )
1266 {
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;
1272
1273
1274 /* compute which blue zones are active, i.e. have their scaled */
1275 /* size < 3/4 pixels */
1276
1277 /* for each horizontal edge search the blue zone which is closest */
1278 for ( ; edge < edge_limit; edge++ )
1279 {
1280 FT_Int bb;
1281 AF_Width best_blue = NULL;
1282 FT_Pos best_dist; /* initial threshold */
1283
1284
1285 /* compute the initial threshold as a fraction of the EM size */
1286 best_dist = FT_MulFix( metrics->units_per_em / 40, scale );
1287
1288 if ( best_dist > 64 / 2 )
1289 best_dist = 64 / 2;
1290
1291 for ( bb = 0; bb < AF_LATIN_BLUE_MAX; bb++ )
1292 {
1293 AF_LatinBlue blue = latin->blues + bb;
1294 FT_Bool is_top_blue, is_major_dir;
1295
1296
1297 /* skip inactive blue zones (i.e., those that are too small) */
1298 if ( !( blue->flags & AF_LATIN_BLUE_ACTIVE ) )
1299 continue;
1300
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 );
1307
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 )
1312 {
1313 FT_Pos dist;
1314
1315
1316 /* first of all, compare it to the reference position */
1317 dist = edge->fpos - blue->ref.org;
1318 if ( dist < 0 )
1319 dist = -dist;
1320
1321 dist = FT_MulFix( dist, scale );
1322 if ( dist < best_dist )
1323 {
1324 best_dist = dist;
1325 best_blue = & blue->ref;
1326 }
1327
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 )
1332 {
1333 FT_Bool is_under_ref = FT_BOOL( edge->fpos < blue->ref.org );
1334
1335
1336 if ( is_top_blue ^ is_under_ref )
1337 {
1338 blue = latin->blues + bb;
1339 dist = edge->fpos - blue->shoot.org;
1340 if ( dist < 0 )
1341 dist = -dist;
1342
1343 dist = FT_MulFix( dist, scale );
1344 if ( dist < best_dist )
1345 {
1346 best_dist = dist;
1347 best_blue = & blue->shoot;
1348 }
1349 }
1350 }
1351 }
1352 }
1353
1354 if ( best_blue )
1355 edge->blue_edge = best_blue;
1356 }
1357 }
1358
1359
1360 static FT_Error
1361 af_latin_hints_init( AF_GlyphHints hints,
1362 AF_LatinMetrics metrics )
1363 {
1364 FT_Render_Mode mode;
1365 FT_UInt32 scaler_flags, other_flags;
1366 FT_Face face = metrics->root.scaler.face;
1367
1368
1369 af_glyph_hints_rescale( hints, (AF_ScriptMetrics)metrics );
1370
1371 /*
1372 * correct x_scale and y_scale if needed, since they may have
1373 * been modified `af_latin_metrics_scale_dim' above
1374 */
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;
1379
1380 /* compute flags depending on render mode, etc. */
1381 mode = metrics->root.scaler.render_mode;
1382
1383 #if 0 /* #ifdef AF_USE_WARPER */
1384 if ( mode == FT_RENDER_MODE_LCD || mode == FT_RENDER_MODE_LCD_V )
1385 {
1386 metrics->root.scaler.render_mode = mode = FT_RENDER_MODE_NORMAL;
1387 }
1388 #endif
1389
1390 scaler_flags = hints->scaler_flags;
1391 other_flags = 0;
1392
1393 /*
1394 * We snap the width of vertical stems for the monochrome and
1395 * horizontal LCD rendering targets only.
1396 */
1397 if ( mode == FT_RENDER_MODE_MONO || mode == FT_RENDER_MODE_LCD )
1398 other_flags |= AF_LATIN_HINTS_HORZ_SNAP;
1399
1400 /*
1401 * We snap the width of horizontal stems for the monochrome and
1402 * vertical LCD rendering targets only.
1403 */
1404 if ( mode == FT_RENDER_MODE_MONO || mode == FT_RENDER_MODE_LCD_V )
1405 other_flags |= AF_LATIN_HINTS_VERT_SNAP;
1406
1407 /*
1408 * We adjust stems to full pixels only if we don't use the `light' mode.
1409 */
1410 if ( mode != FT_RENDER_MODE_LIGHT )
1411 other_flags |= AF_LATIN_HINTS_STEM_ADJUST;
1412
1413 if ( mode == FT_RENDER_MODE_MONO )
1414 other_flags |= AF_LATIN_HINTS_MONO;
1415
1416 /*
1417 * In `light' hinting mode we disable horizontal hinting completely.
1418 * We also do it if the face is italic.
1419 */
1420 if ( mode == FT_RENDER_MODE_LIGHT ||
1421 (face->style_flags & FT_STYLE_FLAG_ITALIC) != 0 )
1422 scaler_flags |= AF_SCALER_FLAG_NO_HORIZONTAL;
1423
1424 hints->scaler_flags = scaler_flags;
1425 hints->other_flags = other_flags;
1426
1427 return 0;
1428 }
1429
1430
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 /*************************************************************************/
1438
1439 /* snap a given width in scaled coordinates to one of the */
1440 /* current standard widths */
1441
1442 static FT_Pos
1443 af_latin_snap_width( AF_Width widths,
1444 FT_Int count,
1445 FT_Pos width )
1446 {
1447 int n;
1448 FT_Pos best = 64 + 32 + 2;
1449 FT_Pos reference = width;
1450 FT_Pos scaled;
1451
1452
1453 for ( n = 0; n < count; n++ )
1454 {
1455 FT_Pos w;
1456 FT_Pos dist;
1457
1458
1459 w = widths[n].cur;
1460 dist = width - w;
1461 if ( dist < 0 )
1462 dist = -dist;
1463 if ( dist < best )
1464 {
1465 best = dist;
1466 reference = w;
1467 }
1468 }
1469
1470 scaled = FT_PIX_ROUND( reference );
1471
1472 if ( width >= reference )
1473 {
1474 if ( width < scaled + 48 )
1475 width = reference;
1476 }
1477 else
1478 {
1479 if ( width > scaled - 48 )
1480 width = reference;
1481 }
1482
1483 return width;
1484 }
1485
1486
1487 /* compute the snapped width of a given stem */
1488
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 )
1495 {
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 );
1501
1502
1503 if ( !AF_LATIN_HINTS_DO_STEM_ADJUST( hints ) ||
1504 axis->extra_light )
1505 return width;
1506
1507 if ( dist < 0 )
1508 {
1509 dist = -width;
1510 sign = 1;
1511 }
1512
1513 if ( ( vertical && !AF_LATIN_HINTS_DO_VERT_SNAP( hints ) ) ||
1514 ( !vertical && !AF_LATIN_HINTS_DO_HORZ_SNAP( hints ) ) )
1515 {
1516 /* smooth hinting process: very lightly quantize the stem width */
1517
1518 /* leave the widths of serifs alone */
1519
1520 if ( ( stem_flags & AF_EDGE_SERIF ) && vertical && ( dist < 3 * 64 ) )
1521 goto Done_Width;
1522
1523 else if ( ( base_flags & AF_EDGE_ROUND ) )
1524 {
1525 if ( dist < 80 )
1526 dist = 64;
1527 }
1528 else if ( dist < 56 )
1529 dist = 56;
1530
1531 if ( axis->width_count > 0 )
1532 {
1533 FT_Pos delta;
1534
1535
1536 /* compare to standard width */
1537 delta = dist - axis->widths[0].cur;
1538
1539 if ( delta < 0 )
1540 delta = -delta;
1541
1542 if ( delta < 40 )
1543 {
1544 dist = axis->widths[0].cur;
1545 if ( dist < 48 )
1546 dist = 48;
1547
1548 goto Done_Width;
1549 }
1550
1551 if ( dist < 3 * 64 )
1552 {
1553 delta = dist & 63;
1554 dist &= -64;
1555
1556 if ( delta < 10 )
1557 dist += delta;
1558
1559 else if ( delta < 32 )
1560 dist += 10;
1561
1562 else if ( delta < 54 )
1563 dist += 54;
1564
1565 else
1566 dist += delta;
1567 }
1568 else
1569 dist = ( dist + 32 ) & ~63;
1570 }
1571 }
1572 else
1573 {
1574 /* strong hinting process: snap the stem width to integer pixels */
1575 FT_Pos org_dist = dist;
1576
1577
1578 dist = af_latin_snap_width( axis->widths, axis->width_count, dist );
1579
1580 if ( vertical )
1581 {
1582 /* in the case of vertical hinting, always round */
1583 /* the stem heights to integer pixels */
1584
1585 if ( dist >= 64 )
1586 dist = ( dist + 16 ) & ~63;
1587 else
1588 dist = 64;
1589 }
1590 else
1591 {
1592 if ( AF_LATIN_HINTS_DO_MONO( hints ) )
1593 {
1594 /* monochrome horizontal hinting: snap widths to integer pixels */
1595 /* with a different threshold */
1596
1597 if ( dist < 64 )
1598 dist = 64;
1599 else
1600 dist = ( dist + 32 ) & ~63;
1601 }
1602 else
1603 {
1604 /* for horizontal anti-aliased hinting, we adopt a more subtle */
1605 /* approach: we strengthen small stems, round stems whose size */
1606 /* is between 1 and 2 pixels to an integer, otherwise nothing */
1607
1608 if ( dist < 48 )
1609 dist = ( dist + 64 ) >> 1;
1610
1611 else if ( dist < 128 )
1612 {
1613 /* We only round to an integer width if the corresponding */
1614 /* distortion is less than 1/4 pixel. Otherwise this */
1615 /* makes everything worse since the diagonals, which are */
1616 /* not hinted, appear a lot bolder or thinner than the */
1617 /* vertical stems. */
1618
1619 FT_Pos delta;
1620
1621
1622 dist = ( dist + 22 ) & ~63;
1623 delta = dist - org_dist;
1624 if ( delta < 0 )
1625 delta = -delta;
1626
1627 if (delta >= 16)
1628 {
1629 dist = org_dist;
1630 if ( dist < 48 )
1631 dist = ( dist + 64 ) >> 1;
1632 }
1633 }
1634 else
1635 /* round otherwise to prevent color fringes in LCD mode */
1636 dist = ( dist + 32 ) & ~63;
1637 }
1638 }
1639 }
1640
1641 Done_Width:
1642 if ( sign )
1643 dist = -dist;
1644
1645 return dist;
1646 }
1647
1648
1649 /* align one stem edge relative to the previous stem edge */
1650
1651 static void
1652 af_latin_align_linked_edge( AF_GlyphHints hints,
1653 AF_Dimension dim,
1654 AF_Edge base_edge,
1655 AF_Edge stem_edge )
1656 {
1657 FT_Pos dist = stem_edge->opos - base_edge->opos;
1658
1659 FT_Pos fitted_width = af_latin_compute_stem_width(
1660 hints, dim, dist,
1661 (AF_Edge_Flags)base_edge->flags,
1662 (AF_Edge_Flags)stem_edge->flags );
1663
1664
1665 stem_edge->pos = base_edge->pos + fitted_width;
1666
1667 AF_LOG(( "LINK: edge %d (opos=%.2f) linked to (%.2f), "
1668 "dist was %.2f, now %.2f\n",
1669 stem_edge-hints->axis[dim].edges, stem_edge->opos / 64.0,
1670 stem_edge->pos / 64.0, dist / 64.0, fitted_width / 64.0 ));
1671 }
1672
1673
1674 static void
1675 af_latin_align_serif_edge( AF_GlyphHints hints,
1676 AF_Edge base,
1677 AF_Edge serif )
1678 {
1679 FT_UNUSED( hints );
1680
1681 serif->pos = base->pos + (serif->opos - base->opos);
1682 }
1683
1684
1685 /*************************************************************************/
1686 /*************************************************************************/
1687 /*************************************************************************/
1688 /**** ****/
1689 /**** E D G E H I N T I N G ****/
1690 /**** ****/
1691 /*************************************************************************/
1692 /*************************************************************************/
1693 /*************************************************************************/
1694
1695
1696 FT_LOCAL_DEF( void )
1697 af_latin_hint_edges( AF_GlyphHints hints,
1698 AF_Dimension dim )
1699 {
1700 AF_AxisHints axis = &hints->axis[dim];
1701 AF_Edge edges = axis->edges;
1702 AF_Edge edge_limit = edges + axis->num_edges;
1703 FT_PtrDist n_edges;
1704 AF_Edge edge;
1705 AF_Edge anchor = 0;
1706 FT_Int has_serifs = 0;
1707
1708
1709 /* we begin by aligning all stems relative to the blue zone */
1710 /* if needed -- that's only for horizontal edges */
1711
1712 if ( dim == AF_DIMENSION_VERT && AF_HINTS_DO_BLUES( hints ) )
1713 {
1714 for ( edge = edges; edge < edge_limit; edge++ )
1715 {
1716 AF_Width blue;
1717 AF_Edge edge1, edge2;
1718
1719
1720 if ( edge->flags & AF_EDGE_DONE )
1721 continue;
1722
1723 blue = edge->blue_edge;
1724 edge1 = NULL;
1725 edge2 = edge->link;
1726
1727 if ( blue )
1728 {
1729 edge1 = edge;
1730 }
1731 else if ( edge2 && edge2->blue_edge )
1732 {
1733 blue = edge2->blue_edge;
1734 edge1 = edge2;
1735 edge2 = edge;
1736 }
1737
1738 if ( !edge1 )
1739 continue;
1740
1741 AF_LOG(( "BLUE: edge %d (opos=%.2f) snapped to (%.2f), "
1742 "was (%.2f)\n",
1743 edge1-edges, edge1->opos / 64.0, blue->fit / 64.0,
1744 edge1->pos / 64.0 ));
1745
1746 edge1->pos = blue->fit;
1747 edge1->flags |= AF_EDGE_DONE;
1748
1749 if ( edge2 && !edge2->blue_edge )
1750 {
1751 af_latin_align_linked_edge( hints, dim, edge1, edge2 );
1752 edge2->flags |= AF_EDGE_DONE;
1753 }
1754
1755 if ( !anchor )
1756 anchor = edge;
1757 }
1758 }
1759
1760 /* now we will align all stem edges, trying to maintain the */
1761 /* relative order of stems in the glyph */
1762 for ( edge = edges; edge < edge_limit; edge++ )
1763 {
1764 AF_Edge edge2;
1765
1766
1767 if ( edge->flags & AF_EDGE_DONE )
1768 continue;
1769
1770 /* skip all non-stem edges */
1771 edge2 = edge->link;
1772 if ( !edge2 )
1773 {
1774 has_serifs++;
1775 continue;
1776 }
1777
1778 /* now align the stem */
1779
1780 /* this should not happen, but it's better to be safe */
1781 if ( edge2->blue_edge )
1782 {
1783 AF_LOG(( "ASSERTION FAILED for edge %d\n", edge2-edges ));
1784
1785 af_latin_align_linked_edge( hints, dim, edge2, edge );
1786 edge->flags |= AF_EDGE_DONE;
1787 continue;
1788 }
1789
1790 if ( !anchor )
1791 {
1792 FT_Pos org_len, org_center, cur_len;
1793 FT_Pos cur_pos1, error1, error2, u_off, d_off;
1794
1795
1796 org_len = edge2->opos - edge->opos;
1797 cur_len = af_latin_compute_stem_width(
1798 hints, dim, org_len,
1799 (AF_Edge_Flags)edge->flags,
1800 (AF_Edge_Flags)edge2->flags );
1801 if ( cur_len <= 64 )
1802 u_off = d_off = 32;
1803 else
1804 {
1805 u_off = 38;
1806 d_off = 26;
1807 }
1808
1809 if ( cur_len < 96 )
1810 {
1811 org_center = edge->opos + ( org_len >> 1 );
1812
1813 cur_pos1 = FT_PIX_ROUND( org_center );
1814
1815 error1 = org_center - ( cur_pos1 - u_off );
1816 if ( error1 < 0 )
1817 error1 = -error1;
1818
1819 error2 = org_center - ( cur_pos1 + d_off );
1820 if ( error2 < 0 )
1821 error2 = -error2;
1822
1823 if ( error1 < error2 )
1824 cur_pos1 -= u_off;
1825 else
1826 cur_pos1 += d_off;
1827
1828 edge->pos = cur_pos1 - cur_len / 2;
1829 edge2->pos = edge->pos + cur_len;
1830 }
1831 else
1832 edge->pos = FT_PIX_ROUND( edge->opos );
1833
1834 AF_LOG(( "ANCHOR: edge %d (opos=%.2f) and %d (opos=%.2f) "
1835 "snapped to (%.2f) (%.2f)\n",
1836 edge-edges, edge->opos / 64.0,
1837 edge2-edges, edge2->opos / 64.0,
1838 edge->pos / 64.0, edge2->pos / 64.0 ));
1839 anchor = edge;
1840
1841 edge->flags |= AF_EDGE_DONE;
1842
1843 af_latin_align_linked_edge( hints, dim, edge, edge2 );
1844 }
1845 else
1846 {
1847 FT_Pos org_pos, org_len, org_center, cur_len;
1848 FT_Pos cur_pos1, cur_pos2, delta1, delta2;
1849
1850
1851 org_pos = anchor->pos + ( edge->opos - anchor->opos );
1852 org_len = edge2->opos - edge->opos;
1853 org_center = org_pos + ( org_len >> 1 );
1854
1855 cur_len = af_latin_compute_stem_width(
1856 hints, dim, org_len,
1857 (AF_Edge_Flags)edge->flags,
1858 (AF_Edge_Flags)edge2->flags );
1859
1860 if ( edge2->flags & AF_EDGE_DONE )
1861 edge->pos = edge2->pos - cur_len;
1862
1863 else if ( cur_len < 96 )
1864 {
1865 FT_Pos u_off, d_off;
1866
1867
1868 cur_pos1 = FT_PIX_ROUND( org_center );
1869
1870 if (cur_len <= 64 )
1871 u_off = d_off = 32;
1872 else
1873 {
1874 u_off = 38;
1875 d_off = 26;
1876 }
1877
1878 delta1 = org_center - ( cur_pos1 - u_off );
1879 if ( delta1 < 0 )
1880 delta1 = -delta1;
1881
1882 delta2 = org_center - ( cur_pos1 + d_off );
1883 if ( delta2 < 0 )
1884 delta2 = -delta2;
1885
1886 if ( delta1 < delta2 )
1887 cur_pos1 -= u_off;
1888 else
1889 cur_pos1 += d_off;
1890
1891 edge->pos = cur_pos1 - cur_len / 2;
1892 edge2->pos = cur_pos1 + cur_len / 2;
1893
1894 AF_LOG(( "STEM: %d (opos=%.2f) to %d (opos=%.2f) "
1895 "snapped to (%.2f) and (%.2f)\n",
1896 edge-edges, edge->opos / 64.0,
1897 edge2-edges, edge2->opos / 64.0,
1898 edge->pos / 64.0, edge2->pos / 64.0 ));
1899 }
1900 else
1901 {
1902 org_pos = anchor->pos + ( edge->opos - anchor->opos );
1903 org_len = edge2->opos - edge->opos;
1904 org_center = org_pos + ( org_len >> 1 );
1905
1906 cur_len = af_latin_compute_stem_width(
1907 hints, dim, org_len,
1908 (AF_Edge_Flags)edge->flags,
1909 (AF_Edge_Flags)edge2->flags );
1910
1911 cur_pos1 = FT_PIX_ROUND( org_pos );
1912 delta1 = cur_pos1 + ( cur_len >> 1 ) - org_center;
1913 if ( delta1 < 0 )
1914 delta1 = -delta1;
1915
1916 cur_pos2 = FT_PIX_ROUND( org_pos + org_len ) - cur_len;
1917 delta2 = cur_pos2 + ( cur_len >> 1 ) - org_center;
1918 if ( delta2 < 0 )
1919 delta2 = -delta2;
1920
1921 edge->pos = ( delta1 < delta2 ) ? cur_pos1 : cur_pos2;
1922 edge2->pos = edge->pos + cur_len;
1923
1924 AF_LOG(( "STEM: %d (opos=%.2f) to %d (opos=%.2f) "
1925 "snapped to (%.2f) and (%.2f)\n",
1926 edge-edges, edge->opos / 64.0,
1927 edge2-edges, edge2->opos / 64.0,
1928 edge->pos / 64.0, edge2->pos / 64.0 ));
1929 }
1930
1931 edge->flags |= AF_EDGE_DONE;
1932 edge2->flags |= AF_EDGE_DONE;
1933
1934 if ( edge > edges && edge->pos < edge[-1].pos )
1935 {
1936 AF_LOG(( "BOUND: %d (pos=%.2f) to (%.2f)\n",
1937 edge-edges, edge->pos / 64.0, edge[-1].pos / 64.0 ));
1938 edge->pos = edge[-1].pos;
1939 }
1940 }
1941 }
1942
1943 /* make sure that lowercase m's maintain their symmetry */
1944
1945 /* In general, lowercase m's have six vertical edges if they are sans */
1946 /* serif, or twelve if they are with serifs. This implementation is */
1947 /* based on that assumption, and seems to work very well with most */
1948 /* faces. However, if for a certain face this assumption is not */
1949 /* true, the m is just rendered like before. In addition, any stem */
1950 /* correction will only be applied to symmetrical glyphs (even if the */
1951 /* glyph is not an m), so the potential for unwanted distortion is */
1952 /* relatively low. */
1953
1954 /* We don't handle horizontal edges since we can't easily assure that */
1955 /* the third (lowest) stem aligns with the base line; it might end up */
1956 /* one pixel higher or lower. */
1957
1958 n_edges = edge_limit - edges;
1959 if ( dim == AF_DIMENSION_HORZ && ( n_edges == 6 || n_edges == 12 ) )
1960 {
1961 AF_Edge edge1, edge2, edge3;
1962 FT_Pos dist1, dist2, span, delta;
1963
1964
1965 if ( n_edges == 6 )
1966 {
1967 edge1 = edges;
1968 edge2 = edges + 2;
1969 edge3 = edges + 4;
1970 }
1971 else
1972 {
1973 edge1 = edges + 1;
1974 edge2 = edges + 5;
1975 edge3 = edges + 9;
1976 }
1977
1978 dist1 = edge2->opos - edge1->opos;
1979 dist2 = edge3->opos - edge2->opos;
1980
1981 span = dist1 - dist2;
1982 if ( span < 0 )
1983 span = -span;
1984
1985 if ( span < 8 )
1986 {
1987 delta = edge3->pos - ( 2 * edge2->pos - edge1->pos );
1988 edge3->pos -= delta;
1989 if ( edge3->link )
1990 edge3->link->pos -= delta;
1991
1992 /* move the serifs along with the stem */
1993 if ( n_edges == 12 )
1994 {
1995 ( edges + 8 )->pos -= delta;
1996 ( edges + 11 )->pos -= delta;
1997 }
1998
1999 edge3->flags |= AF_EDGE_DONE;
2000 if ( edge3->link )
2001 edge3->link->flags |= AF_EDGE_DONE;
2002 }
2003 }
2004
2005 if ( has_serifs || !anchor )
2006 {
2007 /*
2008 * now hint the remaining edges (serifs and single) in order
2009 * to complete our processing
2010 */
2011 for ( edge = edges; edge < edge_limit; edge++ )
2012 {
2013 FT_Pos delta;
2014
2015
2016 if ( edge->flags & AF_EDGE_DONE )
2017 continue;
2018
2019 delta = 1000;
2020
2021 if ( edge->serif )
2022 {
2023 delta = edge->serif->opos - edge->opos;
2024 if ( delta < 0 )
2025 delta = -delta;
2026 }
2027
2028 if ( delta < 64 + 16 )
2029 {
2030 af_latin_align_serif_edge( hints, edge->serif, edge );
2031 AF_LOG(( "SERIF: edge %d (opos=%.2f) serif to %d (opos=%.2f) "
2032 "aligned to (%.2f)\n",
2033 edge-edges, edge->opos / 64.0,
2034 edge->serif - edges, edge->serif->opos / 64.0,
2035 edge->pos / 64.0 ));
2036 }
2037 else if ( !anchor )
2038 {
2039 AF_LOG(( "SERIF_ANCHOR: edge %d (opos=%.2f) snapped to (%.2f)\n",
2040 edge-edges, edge->opos / 64.0, edge->pos / 64.0 ));
2041 edge->pos = FT_PIX_ROUND( edge->opos );
2042 anchor = edge;
2043 }
2044 else
2045 {
2046 AF_Edge before, after;
2047
2048
2049 for ( before = edge - 1; before >= edges; before-- )
2050 if ( before->flags & AF_EDGE_DONE )
2051 break;
2052
2053 for ( after = edge + 1; after < edge_limit; after++ )
2054 if ( after->flags & AF_EDGE_DONE )
2055 break;
2056
2057 if ( before >= edges && before < edge &&
2058 after < edge_limit && after > edge )
2059 {
2060 if ( after->opos == before->opos )
2061 edge->pos = before->pos;
2062 else
2063 edge->pos = before->pos +
2064 FT_MulDiv( edge->opos - before->opos,
2065 after->pos - before->pos,
2066 after->opos - before->opos );
2067 AF_LOG(( "SERIF_LINK1: edge %d (opos=%.2f) snapped to (%.2f) "
2068 "from %d (opos=%.2f)\n",
2069 edge-edges, edge->opos / 64.0,
2070 edge->pos / 64.0, before - edges,
2071 before->opos / 64.0 ));
2072 }
2073 else
2074 {
2075 edge->pos = anchor->pos +
2076 ( ( edge->opos - anchor->opos + 16 ) & ~31 );
2077 AF_LOG(( "SERIF_LINK2: edge %d (opos=%.2f) snapped to (%.2f)\n",
2078 edge-edges, edge->opos / 64.0, edge->pos / 64.0 ));
2079 }
2080 }
2081
2082 edge->flags |= AF_EDGE_DONE;
2083
2084 if ( edge > edges && edge->pos < edge[-1].pos )
2085 edge->pos = edge[-1].pos;
2086
2087 if ( edge + 1 < edge_limit &&
2088 edge[1].flags & AF_EDGE_DONE &&
2089 edge->pos > edge[1].pos )
2090 edge->pos = edge[1].pos;
2091 }
2092 }
2093 }
2094
2095
2096 static FT_Error
2097 af_latin_hints_apply( AF_GlyphHints hints,
2098 FT_Outline* outline,
2099 AF_LatinMetrics metrics )
2100 {
2101 FT_Error error;
2102 int dim;
2103
2104
2105 error = af_glyph_hints_reload( hints, outline );
2106 if ( error )
2107 goto Exit;
2108
2109 /* analyze glyph outline */
2110 #ifdef AF_USE_WARPER
2111 if ( metrics->root.scaler.render_mode == FT_RENDER_MODE_LIGHT ||
2112 AF_HINTS_DO_HORIZONTAL( hints ) )
2113 #else
2114 if ( AF_HINTS_DO_HORIZONTAL( hints ) )
2115 #endif
2116 {
2117 error = af_latin_hints_detect_features( hints, AF_DIMENSION_HORZ );
2118 if ( error )
2119 goto Exit;
2120 }
2121
2122 if ( AF_HINTS_DO_VERTICAL( hints ) )
2123 {
2124 error = af_latin_hints_detect_features( hints, AF_DIMENSION_VERT );
2125 if ( error )
2126 goto Exit;
2127
2128 af_latin_hints_compute_blue_edges( hints, metrics );
2129 }
2130
2131 /* grid-fit the outline */
2132 for ( dim = 0; dim < AF_DIMENSION_MAX; dim++ )
2133 {
2134 #ifdef AF_USE_WARPER
2135 if ( ( dim == AF_DIMENSION_HORZ &&
2136 metrics->root.scaler.render_mode == FT_RENDER_MODE_LIGHT ) )
2137 {
2138 AF_WarperRec warper;
2139 FT_Fixed scale;
2140 FT_Pos delta;
2141
2142
2143 af_warper_compute( &warper, hints, dim, &scale, &delta );
2144 af_glyph_hints_scale_dim( hints, dim, scale, delta );
2145 continue;
2146 }
2147 #endif
2148
2149 if ( ( dim == AF_DIMENSION_HORZ && AF_HINTS_DO_HORIZONTAL( hints ) ) ||
2150 ( dim == AF_DIMENSION_VERT && AF_HINTS_DO_VERTICAL( hints ) ) )
2151 {
2152 af_latin_hint_edges( hints, (AF_Dimension)dim );
2153 af_glyph_hints_align_edge_points( hints, (AF_Dimension)dim );
2154 af_glyph_hints_align_strong_points( hints, (AF_Dimension)dim );
2155 af_glyph_hints_align_weak_points( hints, (AF_Dimension)dim );
2156 }
2157 }
2158 af_glyph_hints_save( hints, outline );
2159
2160 Exit:
2161 return error;
2162 }
2163
2164
2165 /*************************************************************************/
2166 /*************************************************************************/
2167 /***** *****/
2168 /***** L A T I N S C R I P T C L A S S *****/
2169 /***** *****/
2170 /*************************************************************************/
2171 /*************************************************************************/
2172
2173
2174 /* XXX: this should probably fine tuned to differentiate better between */
2175 /* scripts... */
2176
2177 static const AF_Script_UniRangeRec af_latin_uniranges[] =
2178 {
2179 AF_UNIRANGE_REC( 0x0020UL, 0x007FUL ), /* Basic Latin (no control chars) */
2180 AF_UNIRANGE_REC( 0x00A0UL, 0x00FFUL ), /* Latin-1 Supplement (no control chars) */
2181 AF_UNIRANGE_REC( 0x0100UL, 0x017FUL ), /* Latin Extended-A */
2182 AF_UNIRANGE_REC( 0x0180UL, 0x024FUL ), /* Latin Extended-B */
2183 AF_UNIRANGE_REC( 0x0250UL, 0x02AFUL ), /* IPA Extensions */
2184 AF_UNIRANGE_REC( 0x02B0UL, 0x02FFUL ), /* Spacing Modifier Letters */
2185 AF_UNIRANGE_REC( 0x0300UL, 0x036FUL ), /* Combining Diacritical Marks */
2186 AF_UNIRANGE_REC( 0x0370UL, 0x03FFUL ), /* Greek and Coptic */
2187 AF_UNIRANGE_REC( 0x0400UL, 0x04FFUL ), /* Cyrillic */
2188 AF_UNIRANGE_REC( 0x0500UL, 0x052FUL ), /* Cyrillic Supplement */
2189 AF_UNIRANGE_REC( 0x1D00UL, 0x1D7FUL ), /* Phonetic Extensions */
2190 AF_UNIRANGE_REC( 0x1D80UL, 0x1DBFUL ), /* Phonetic Extensions Supplement */
2191 AF_UNIRANGE_REC( 0x1DC0UL, 0x1DFFUL ), /* Combining Diacritical Marks Supplement */
2192 AF_UNIRANGE_REC( 0x1E00UL, 0x1EFFUL ), /* Latin Extended Additional */
2193 AF_UNIRANGE_REC( 0x1F00UL, 0x1FFFUL ), /* Greek Extended */
2194 AF_UNIRANGE_REC( 0x2000UL, 0x206FUL ), /* General Punctuation */
2195 AF_UNIRANGE_REC( 0x2070UL, 0x209FUL ), /* Superscripts and Subscripts */
2196 AF_UNIRANGE_REC( 0x20A0UL, 0x20CFUL ), /* Currency Symbols */
2197 AF_UNIRANGE_REC( 0x2150UL, 0x218FUL ), /* Number Forms */
2198 AF_UNIRANGE_REC( 0x2460UL, 0x24FFUL ), /* Enclosed Alphanumerics */
2199 AF_UNIRANGE_REC( 0x2C60UL, 0x2C7FUL ), /* Latin Extended-C */
2200 AF_UNIRANGE_REC( 0x2DE0UL, 0x2DFFUL ), /* Cyrillic Extended-A */
2201 AF_UNIRANGE_REC( 0xA640UL, 0xA69FUL ), /* Cyrillic Extended-B */
2202 AF_UNIRANGE_REC( 0xA720UL, 0xA7FFUL ), /* Latin Extended-D */
2203 AF_UNIRANGE_REC( 0xFB00UL, 0xFB06UL ), /* Alphab. Present. Forms (Latin Ligs) */
2204 AF_UNIRANGE_REC( 0x1D400UL, 0x1D7FFUL ), /* Mathematical Alphanumeric Symbols */
2205 AF_UNIRANGE_REC( 0UL, 0UL )
2206 };
2207
2208
2209 AF_DEFINE_SCRIPT_CLASS(af_latin_script_class,
2210 AF_SCRIPT_LATIN,
2211 af_latin_uniranges,
2212
2213 sizeof( AF_LatinMetricsRec ),
2214
2215 (AF_Script_InitMetricsFunc) af_latin_metrics_init,
2216 (AF_Script_ScaleMetricsFunc)af_latin_metrics_scale,
2217 (AF_Script_DoneMetricsFunc) NULL,
2218
2219 (AF_Script_InitHintsFunc) af_latin_hints_init,
2220 (AF_Script_ApplyHintsFunc) af_latin_hints_apply
2221 )
2222
2223
2224 /* END */