Delete all Trailing spaces in code.
[reactos.git] / reactos / dll / 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 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 "aflatin.h"
20 #include "aferrors.h"
21
22
23 #ifdef AF_USE_WARPER
24 #include "afwarp.h"
25 #endif
26
27
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 /*************************************************************************/
35
36 FT_LOCAL_DEF( void )
37 af_latin_metrics_init_widths( AF_LatinMetrics metrics,
38 FT_Face face,
39 FT_ULong charcode )
40 {
41 /* scan the array of segments in each direction */
42 AF_GlyphHintsRec hints[1];
43
44
45 af_glyph_hints_init( hints, face->memory );
46
47 metrics->axis[AF_DIMENSION_HORZ].width_count = 0;
48 metrics->axis[AF_DIMENSION_VERT].width_count = 0;
49
50 {
51 FT_Error error;
52 FT_UInt glyph_index;
53 int dim;
54 AF_LatinMetricsRec dummy[1];
55 AF_Scaler scaler = &dummy->root.scaler;
56
57
58 glyph_index = FT_Get_Char_Index( face, charcode );
59 if ( glyph_index == 0 )
60 goto Exit;
61
62 error = FT_Load_Glyph( face, glyph_index, FT_LOAD_NO_SCALE );
63 if ( error || face->glyph->outline.n_points <= 0 )
64 goto Exit;
65
66 FT_ZERO( dummy );
67
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;
74
75 af_glyph_hints_rescale( hints, (AF_ScriptMetrics)dummy );
76
77 error = af_glyph_hints_reload( hints, &face->glyph->outline, 0 );
78 if ( error )
79 goto Exit;
80
81 for ( dim = 0; dim < AF_DIMENSION_MAX; dim++ )
82 {
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;
87
88
89 error = af_latin_hints_compute_segments( hints,
90 (AF_Dimension)dim );
91 if ( error )
92 goto Exit;
93
94 af_latin_hints_link_segments( hints,
95 (AF_Dimension)dim );
96
97 seg = axhints->segments;
98 limit = seg + axhints->num_segments;
99
100 for ( ; seg < limit; seg++ )
101 {
102 link = seg->link;
103
104 /* we only consider stem segments there! */
105 if ( link && link->link == seg && link > seg )
106 {
107 FT_Pos dist;
108
109
110 dist = seg->pos - link->pos;
111 if ( dist < 0 )
112 dist = -dist;
113
114 if ( num_widths < AF_LATIN_MAX_WIDTHS )
115 axis->widths[ num_widths++ ].org = dist;
116 }
117 }
118
119 af_sort_widths( num_widths, axis->widths );
120 axis->width_count = num_widths;
121 }
122
123 Exit:
124 for ( dim = 0; dim < AF_DIMENSION_MAX; dim++ )
125 {
126 AF_LatinAxis axis = &metrics->axis[dim];
127 FT_Pos stdw;
128
129
130 stdw = ( axis->width_count > 0 )
131 ? axis->widths[0].org
132 : AF_LATIN_CONSTANT( metrics, 50 );
133
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;
138 }
139 }
140
141 af_glyph_hints_done( hints );
142 }
143
144
145
146 #define AF_LATIN_MAX_TEST_CHARACTERS 12
147
148
149 static const char* const af_latin_blue_chars[AF_LATIN_MAX_BLUES] =
150 {
151 "THEZOCQS",
152 "HEZLOCUS",
153 "fijkdbh",
154 "xzroesc",
155 "xzroesc",
156 "pqgjy"
157 };
158
159
160 static void
161 af_latin_metrics_init_blues( AF_LatinMetrics metrics,
162 FT_Face face )
163 {
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;
173
174
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') */
178
179 AF_LOG(( "blue zones computation\n" ));
180 AF_LOG(( "------------------------------------------------\n" ));
181
182 for ( bb = 0; bb < AF_LATIN_BLUE_MAX; bb++ )
183 {
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;
188
189
190 AF_LOG(( "blue %3d: ", bb ));
191
192 num_flats = 0;
193 num_rounds = 0;
194
195 for ( ; p < limit && *p; p++ )
196 {
197 FT_UInt glyph_index;
198 FT_Int best_point, best_y, best_first, best_last;
199 FT_Vector* points;
200 FT_Bool round;
201
202
203 AF_LOG(( "'%c'", *p ));
204
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;
209
210 error = FT_Load_Glyph( face, glyph_index, FT_LOAD_NO_SCALE );
211 if ( error || glyph->outline.n_points <= 0 )
212 continue;
213
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 */
220
221 {
222 FT_Int nn;
223 FT_Int first = 0;
224 FT_Int last = -1;
225
226
227 for ( nn = 0; nn < glyph->outline.n_contours; first = last+1, nn++ )
228 {
229 FT_Int old_best_point = best_point;
230 FT_Int pp;
231
232
233 last = glyph->outline.contours[nn];
234
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;
240
241 if ( AF_LATIN_IS_TOP_BLUE( bb ) )
242 {
243 for ( pp = first; pp <= last; pp++ )
244 if ( best_point < 0 || points[pp].y > best_y )
245 {
246 best_point = pp;
247 best_y = points[pp].y;
248 }
249 }
250 else
251 {
252 for ( pp = first; pp <= last; pp++ )
253 if ( best_point < 0 || points[pp].y < best_y )
254 {
255 best_point = pp;
256 best_y = points[pp].y;
257 }
258 }
259
260 if ( best_point != old_best_point )
261 {
262 best_first = first;
263 best_last = last;
264 }
265 }
266 AF_LOG(( "%5d", best_y ));
267 }
268
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 )
273 {
274 FT_Int prev, next;
275 FT_Pos dist;
276
277
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;
282
283 do
284 {
285 if ( prev > best_first )
286 prev--;
287 else
288 prev = best_last;
289
290 dist = points[prev].y - best_y;
291 if ( dist < -5 || dist > 5 )
292 break;
293
294 } while ( prev != best_point );
295
296 do
297 {
298 if ( next < best_last )
299 next++;
300 else
301 next = best_first;
302
303 dist = points[next].y - best_y;
304 if ( dist < -5 || dist > 5 )
305 break;
306
307 } while ( next != best_point );
308
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 );
313
314 AF_LOG(( "%c ", round ? 'r' : 'f' ));
315 }
316
317 if ( round )
318 rounds[num_rounds++] = best_y;
319 else
320 flats[num_flats++] = best_y;
321 }
322
323 AF_LOG(( "\n" ));
324
325 if ( num_flats == 0 && num_rounds == 0 )
326 {
327 /*
328 * we couldn't find a single glyph to compute this blue zone,
329 * we will simply ignore it then
330 */
331 AF_LOG(( "empty!\n" ));
332 continue;
333 }
334
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 );
340
341 blue = & axis->blues[axis->blue_count];
342 blue_ref = & blue->ref.org;
343 blue_shoot = & blue->shoot.org;
344
345 axis->blue_count++;
346
347 if ( num_flats == 0 )
348 {
349 *blue_ref =
350 *blue_shoot = rounds[num_rounds / 2];
351 }
352 else if ( num_rounds == 0 )
353 {
354 *blue_ref =
355 *blue_shoot = flats[num_flats / 2];
356 }
357 else
358 {
359 *blue_ref = flats[num_flats / 2];
360 *blue_shoot = rounds[num_rounds / 2];
361 }
362
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 )
367 {
368 FT_Pos ref = *blue_ref;
369 FT_Pos shoot = *blue_shoot;
370 FT_Bool over_ref = FT_BOOL( shoot > ref );
371
372
373 if ( AF_LATIN_IS_TOP_BLUE( bb ) ^ over_ref )
374 *blue_shoot = *blue_ref = ( shoot + ref ) / 2;
375 }
376
377 blue->flags = 0;
378 if ( AF_LATIN_IS_TOP_BLUE( bb ) )
379 blue->flags |= AF_LATIN_BLUE_TOP;
380
381 /*
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.
385 */
386 if ( bb == AF_LATIN_BLUE_SMALL_TOP )
387 blue->flags |= AF_LATIN_BLUE_ADJUSTMENT;
388
389 AF_LOG(( "-- ref = %ld, shoot = %ld\n", *blue_ref, *blue_shoot ));
390 }
391
392 return;
393 }
394
395
396 FT_LOCAL_DEF( FT_Error )
397 af_latin_metrics_init( AF_LatinMetrics metrics,
398 FT_Face face )
399 {
400 FT_Error error = AF_Err_Ok;
401 FT_CharMap oldmap = face->charmap;
402 FT_UInt ee;
403
404 static const FT_Encoding latin_encodings[] =
405 {
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 */
411 };
412
413
414 metrics->units_per_em = face->units_per_EM;
415
416 /* do we have a latin charmap in there? */
417 for ( ee = 0; latin_encodings[ee] != FT_ENCODING_NONE; ee++ )
418 {
419 error = FT_Select_Charmap( face, latin_encodings[ee] );
420 if ( !error )
421 break;
422 }
423
424 if ( !error )
425 {
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 );
429 }
430
431 FT_Set_Charmap( face, oldmap );
432 return AF_Err_Ok;
433 }
434
435
436 static void
437 af_latin_metrics_scale_dim( AF_LatinMetrics metrics,
438 AF_Scaler scaler,
439 AF_Dimension dim )
440 {
441 FT_Fixed scale;
442 FT_Pos delta;
443 AF_LatinAxis axis;
444 FT_UInt nn;
445
446
447 if ( dim == AF_DIMENSION_HORZ )
448 {
449 scale = scaler->x_scale;
450 delta = scaler->x_delta;
451 }
452 else
453 {
454 scale = scaler->y_scale;
455 delta = scaler->y_delta;
456 }
457
458 axis = &metrics->axis[dim];
459
460 if ( axis->org_scale == scale && axis->org_delta == delta )
461 return;
462
463 axis->org_scale = scale;
464 axis->org_delta = delta;
465
466 /*
467 * correct X and Y scale to optimize the alignment of the top of small
468 * letters to the pixel grid
469 */
470 {
471 AF_LatinAxis Axis = &metrics->axis[AF_DIMENSION_VERT];
472 AF_LatinBlue blue = NULL;
473
474
475 for ( nn = 0; nn < Axis->blue_count; nn++ )
476 {
477 if ( Axis->blues[nn].flags & AF_LATIN_BLUE_ADJUSTMENT )
478 {
479 blue = &Axis->blues[nn];
480 break;
481 }
482 }
483
484 if ( blue )
485 {
486 FT_Pos scaled = FT_MulFix( blue->shoot.org, scaler->y_scale );
487 FT_Pos fitted = ( scaled + 40 ) & ~63;
488
489
490 if ( scaled != fitted )
491 {
492 #if 0
493 if ( dim == AF_DIMENSION_HORZ )
494 {
495 if ( fitted < scaled )
496 scale -= scale / 50; /* scale *= 0.98 */
497 }
498 else
499 #endif
500 if ( dim == AF_DIMENSION_VERT )
501 {
502 scale = FT_MulDiv( scale, fitted, scaled );
503 }
504 }
505 }
506 }
507
508 axis->scale = scale;
509 axis->delta = delta;
510
511 if ( dim == AF_DIMENSION_HORZ )
512 {
513 metrics->root.scaler.x_scale = scale;
514 metrics->root.scaler.x_delta = delta;
515 }
516 else
517 {
518 metrics->root.scaler.y_scale = scale;
519 metrics->root.scaler.y_delta = delta;
520 }
521
522 /* scale the standard widths */
523 for ( nn = 0; nn < axis->width_count; nn++ )
524 {
525 AF_Width width = axis->widths + nn;
526
527
528 width->cur = FT_MulFix( width->org, scale );
529 width->fit = width->cur;
530 }
531
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 );
536
537 if ( dim == AF_DIMENSION_VERT )
538 {
539 /* scale the blue zones */
540 for ( nn = 0; nn < axis->blue_count; nn++ )
541 {
542 AF_LatinBlue blue = &axis->blues[nn];
543 FT_Pos dist;
544
545
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;
551
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 )
555 {
556 FT_Pos delta1, delta2;
557
558
559 delta1 = blue->shoot.org - blue->ref.org;
560 delta2 = delta1;
561 if ( delta1 < 0 )
562 delta2 = -delta2;
563
564 delta2 = FT_MulFix( delta2, scale );
565
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 );
572
573 if ( delta1 < 0 )
574 delta2 = -delta2;
575
576 blue->ref.fit = FT_PIX_ROUND( blue->ref.cur );
577 blue->shoot.fit = blue->ref.fit + delta2;
578
579 blue->flags |= AF_LATIN_BLUE_ACTIVE;
580 }
581 }
582 }
583 }
584
585
586 FT_LOCAL_DEF( void )
587 af_latin_metrics_scale( AF_LatinMetrics metrics,
588 AF_Scaler scaler )
589 {
590 metrics->root.scaler.render_mode = scaler->render_mode;
591 metrics->root.scaler.face = scaler->face;
592
593 af_latin_metrics_scale_dim( metrics, scaler, AF_DIMENSION_HORZ );
594 af_latin_metrics_scale_dim( metrics, scaler, AF_DIMENSION_VERT );
595 }
596
597
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 /*************************************************************************/
605
606 FT_LOCAL_DEF( FT_Error )
607 af_latin_hints_compute_segments( AF_GlyphHints hints,
608 AF_Dimension dim )
609 {
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;
618
619
620 FT_ZERO( &seg0 );
621 seg0.score = 32000;
622 seg0.flags = AF_EDGE_NORMAL;
623
624 major_dir = (AF_Direction)FT_ABS( axis->major_dir );
625 segment_dir = major_dir;
626
627 axis->num_segments = 0;
628
629 /* set up (u,v) in each point */
630 if ( dim == AF_DIMENSION_HORZ )
631 {
632 AF_Point point = hints->points;
633 AF_Point limit = point + hints->num_points;
634
635
636 for ( ; point < limit; point++ )
637 {
638 point->u = point->fx;
639 point->v = point->fy;
640 }
641 }
642 else
643 {
644 AF_Point point = hints->points;
645 AF_Point limit = point + hints->num_points;
646
647
648 for ( ; point < limit; point++ )
649 {
650 point->u = point->fy;
651 point->v = point->fx;
652 }
653 }
654
655 /* do each contour separately */
656 for ( ; contour < contour_limit; contour++ )
657 {
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;
664
665
666 if ( point == last ) /* skip singletons -- just in case */
667 continue;
668
669 if ( FT_ABS( last->out_dir ) == major_dir &&
670 FT_ABS( point->out_dir ) == major_dir )
671 {
672 /* we are already on an edge, try to locate its start */
673 last = point;
674
675 for (;;)
676 {
677 point = point->prev;
678 if ( FT_ABS( point->out_dir ) != major_dir )
679 {
680 point = point->next;
681 break;
682 }
683 if ( point == last )
684 break;
685 }
686 }
687
688 last = point;
689 passed = 0;
690
691 for (;;)
692 {
693 FT_Pos u, v;
694
695
696 if ( on_edge )
697 {
698 u = point->u;
699 if ( u < min_pos )
700 min_pos = u;
701 if ( u > max_pos )
702 max_pos = u;
703
704 if ( point->out_dir != segment_dir || point == last )
705 {
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 );
709
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;
715
716 /* compute segment size */
717 min_pos = max_pos = point->v;
718
719 v = segment->first->v;
720 if ( v < min_pos )
721 min_pos = v;
722 if ( v > max_pos )
723 max_pos = v;
724
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 );
729
730 on_edge = 0;
731 segment = NULL;
732 /* fallthrough */
733 }
734 }
735
736 /* now exit if we are at the start/end point */
737 if ( point == last )
738 {
739 if ( passed )
740 break;
741 passed = 1;
742 }
743
744 if ( !on_edge && FT_ABS( point->out_dir ) == major_dir )
745 {
746 /* this is the start of a new segment! */
747 segment_dir = (AF_Direction)point->out_dir;
748
749 /* clear all segment fields */
750 error = af_axis_hints_new_segment( axis, memory, &segment );
751 if ( error )
752 goto Exit;
753
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;
761 }
762
763 point = point->next;
764 }
765
766 } /* contours */
767
768
769 /* now slightly increase the height of segments when this makes */
770 /* sense -- this is used to better detect and ignore serifs */
771 {
772 AF_Segment segments = axis->segments;
773 AF_Segment segments_end = segments + axis->num_segments;
774
775
776 for ( segment = segments; segment < segments_end; segment++ )
777 {
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;
782
783
784 if ( first == last )
785 continue;
786
787 if ( first_v < last_v )
788 {
789 AF_Point p;
790
791
792 p = first->prev;
793 if ( p->v < first_v )
794 segment->height = (FT_Short)( segment->height +
795 ( ( first_v - p->v ) >> 1 ) );
796
797 p = last->next;
798 if ( p->v > last_v )
799 segment->height = (FT_Short)( segment->height +
800 ( ( p->v - last_v ) >> 1 ) );
801 }
802 else
803 {
804 AF_Point p;
805
806
807 p = first->prev;
808 if ( p->v > first_v )
809 segment->height = (FT_Short)( segment->height +
810 ( ( p->v - first_v ) >> 1 ) );
811
812 p = last->next;
813 if ( p->v < last_v )
814 segment->height = (FT_Short)( segment->height +
815 ( ( last_v - p->v ) >> 1 ) );
816 }
817 }
818 }
819
820 Exit:
821 return error;
822 }
823
824
825 FT_LOCAL_DEF( void )
826 af_latin_hints_link_segments( AF_GlyphHints hints,
827 AF_Dimension dim )
828 {
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;
834
835
836 len_threshold = AF_LATIN_CONSTANT( hints->metrics, 8 );
837 if ( len_threshold == 0 )
838 len_threshold = 1;
839
840 len_score = AF_LATIN_CONSTANT( hints->metrics, 6000 );
841
842 /* now compare each segment to the others */
843 for ( seg1 = segments; seg1 < segment_limit; seg1++ )
844 {
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;
849
850 for ( seg2 = segments; seg2 < segment_limit; seg2++ )
851 if ( seg1->dir + seg2->dir == 0 && seg2->pos > seg1->pos )
852 {
853 FT_Pos pos1 = seg1->pos;
854 FT_Pos pos2 = seg2->pos;
855 FT_Pos dist = pos2 - pos1;
856
857
858 if ( dist < 0 )
859 dist = -dist;
860
861 {
862 FT_Pos min = seg1->min_coord;
863 FT_Pos max = seg1->max_coord;
864 FT_Pos len, score;
865
866
867 if ( min < seg2->min_coord )
868 min = seg2->min_coord;
869
870 if ( max > seg2->max_coord )
871 max = seg2->max_coord;
872
873 len = max - min;
874 if ( len >= len_threshold )
875 {
876 score = dist + len_score / len;
877
878 if ( score < seg1->score )
879 {
880 seg1->score = score;
881 seg1->link = seg2;
882 }
883
884 if ( score < seg2->score )
885 {
886 seg2->score = score;
887 seg2->link = seg1;
888 }
889 }
890 }
891 }
892 }
893
894 /* now, compute the `serif' segments */
895 for ( seg1 = segments; seg1 < segment_limit; seg1++ )
896 {
897 seg2 = seg1->link;
898
899 if ( seg2 )
900 {
901 if ( seg2->link != seg1 )
902 {
903 seg1->link = 0;
904 seg1->serif = seg2->link;
905 }
906 }
907 }
908 }
909
910
911 FT_LOCAL_DEF( FT_Error )
912 af_latin_hints_compute_edges( AF_GlyphHints hints,
913 AF_Dimension dim )
914 {
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];
919
920 AF_Segment segments = axis->segments;
921 AF_Segment segment_limit = segments + axis->num_segments;
922 AF_Segment seg;
923
924 AF_Direction up_dir;
925 FT_Fixed scale;
926 FT_Pos edge_distance_threshold;
927 FT_Pos segment_length_threshold;
928
929
930 axis->num_edges = 0;
931
932 scale = ( dim == AF_DIMENSION_HORZ ) ? hints->x_scale
933 : hints->y_scale;
934
935 up_dir = ( dim == AF_DIMENSION_HORZ ) ? AF_DIR_UP
936 : AF_DIR_RIGHT;
937
938 /*
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.
942 */
943 if ( dim == AF_DIMENSION_HORZ )
944 segment_length_threshold = FT_DivFix( 64, hints->y_scale );
945 else
946 segment_length_threshold = 0;
947
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 /*********************************************************************/
963
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;
968
969 edge_distance_threshold = FT_DivFix( edge_distance_threshold,
970 scale );
971
972 for ( seg = segments; seg < segment_limit; seg++ )
973 {
974 AF_Edge found = 0;
975 FT_Int ee;
976
977
978 if ( seg->height < segment_length_threshold )
979 continue;
980
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;
986
987 /* look for an edge corresponding to the segment */
988 for ( ee = 0; ee < axis->num_edges; ee++ )
989 {
990 AF_Edge edge = axis->edges + ee;
991 FT_Pos dist;
992
993
994 dist = seg->pos - edge->fpos;
995 if ( dist < 0 )
996 dist = -dist;
997
998 if ( dist < edge_distance_threshold && edge->dir == seg->dir )
999 {
1000 found = edge;
1001 break;
1002 }
1003 }
1004
1005 if ( !found )
1006 {
1007 AF_Edge edge;
1008
1009
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, seg->dir, memory, &edge );
1013 if ( error )
1014 goto Exit;
1015
1016 /* add the segment to the new edge's list */
1017 FT_ZERO( edge );
1018
1019 edge->first = seg;
1020 edge->last = seg;
1021 edge->fpos = seg->pos;
1022 edge->dir = seg->dir;
1023 edge->opos = edge->pos = FT_MulFix( seg->pos, scale );
1024 seg->edge_next = seg;
1025 }
1026 else
1027 {
1028 /* if an edge was found, simply add the segment to the edge's */
1029 /* list */
1030 seg->edge_next = found->first;
1031 found->last->edge_next = seg;
1032 found->last = seg;
1033 }
1034 }
1035
1036
1037 /*********************************************************************/
1038 /* */
1039 /* Good, we will now compute each edge's properties according to */
1040 /* segments found on its position. Basically, these are: */
1041 /* */
1042 /* - edge's main direction */
1043 /* - stem edge, serif edge or both (which defaults to stem then) */
1044 /* - rounded edge, straight or both (which defaults to straight) */
1045 /* - link for edge */
1046 /* */
1047 /*********************************************************************/
1048
1049 /* first of all, set the `edge' field in each segment -- this is */
1050 /* required in order to compute edge links */
1051
1052 /*
1053 * Note that removing this loop and setting the `edge' field of each
1054 * segment directly in the code above slows down execution speed for
1055 * some reasons on platforms like the Sun.
1056 */
1057 {
1058 AF_Edge edges = axis->edges;
1059 AF_Edge edge_limit = edges + axis->num_edges;
1060 AF_Edge edge;
1061
1062
1063 for ( edge = edges; edge < edge_limit; edge++ )
1064 {
1065 seg = edge->first;
1066 if ( seg )
1067 do
1068 {
1069 seg->edge = edge;
1070 seg = seg->edge_next;
1071
1072 } while ( seg != edge->first );
1073 }
1074
1075 /* now, compute each edge properties */
1076 for ( edge = edges; edge < edge_limit; edge++ )
1077 {
1078 FT_Int is_round = 0; /* does it contain round segments? */
1079 FT_Int is_straight = 0; /* does it contain straight segments? */
1080 FT_Pos ups = 0; /* number of upwards segments */
1081 FT_Pos downs = 0; /* number of downwards segments */
1082
1083
1084 seg = edge->first;
1085
1086 do
1087 {
1088 FT_Bool is_serif;
1089
1090
1091 /* check for roundness of segment */
1092 if ( seg->flags & AF_EDGE_ROUND )
1093 is_round++;
1094 else
1095 is_straight++;
1096
1097 /* check for segment direction */
1098 if ( seg->dir == up_dir )
1099 ups += seg->max_coord-seg->min_coord;
1100 else
1101 downs += seg->max_coord-seg->min_coord;
1102
1103 /* check for links -- if seg->serif is set, then seg->link must */
1104 /* be ignored */
1105 is_serif = (FT_Bool)( seg->serif &&
1106 seg->serif->edge &&
1107 seg->serif->edge != edge );
1108
1109 if ( ( seg->link && seg->link->edge != NULL ) || is_serif )
1110 {
1111 AF_Edge edge2;
1112 AF_Segment seg2;
1113
1114
1115 edge2 = edge->link;
1116 seg2 = seg->link;
1117
1118 if ( is_serif )
1119 {
1120 seg2 = seg->serif;
1121 edge2 = edge->serif;
1122 }
1123
1124 if ( edge2 )
1125 {
1126 FT_Pos edge_delta;
1127 FT_Pos seg_delta;
1128
1129
1130 edge_delta = edge->fpos - edge2->fpos;
1131 if ( edge_delta < 0 )
1132 edge_delta = -edge_delta;
1133
1134 seg_delta = seg->pos - seg2->pos;
1135 if ( seg_delta < 0 )
1136 seg_delta = -seg_delta;
1137
1138 if ( seg_delta < edge_delta )
1139 edge2 = seg2->edge;
1140 }
1141 else
1142 edge2 = seg2->edge;
1143
1144 if ( is_serif )
1145 {
1146 edge->serif = edge2;
1147 edge2->flags |= AF_EDGE_SERIF;
1148 }
1149 else
1150 edge->link = edge2;
1151 }
1152
1153 seg = seg->edge_next;
1154
1155 } while ( seg != edge->first );
1156
1157 /* set the round/straight flags */
1158 edge->flags = AF_EDGE_NORMAL;
1159
1160 if ( is_round > 0 && is_round >= is_straight )
1161 edge->flags |= AF_EDGE_ROUND;
1162
1163 #if 0
1164 /* set the edge's main direction */
1165 edge->dir = AF_DIR_NONE;
1166
1167 if ( ups > downs )
1168 edge->dir = (FT_Char)up_dir;
1169
1170 else if ( ups < downs )
1171 edge->dir = (FT_Char)-up_dir;
1172
1173 else if ( ups == downs )
1174 edge->dir = 0; /* both up and down! */
1175 #endif
1176
1177 /* gets rid of serifs if link is set */
1178 /* XXX: This gets rid of many unpleasant artefacts! */
1179 /* Example: the `c' in cour.pfa at size 13 */
1180
1181 if ( edge->serif && edge->link )
1182 edge->serif = 0;
1183 }
1184 }
1185
1186 Exit:
1187 return error;
1188 }
1189
1190
1191 FT_LOCAL_DEF( FT_Error )
1192 af_latin_hints_detect_features( AF_GlyphHints hints,
1193 AF_Dimension dim )
1194 {
1195 FT_Error error;
1196
1197
1198 error = af_latin_hints_compute_segments( hints, dim );
1199 if ( !error )
1200 {
1201 af_latin_hints_link_segments( hints, dim );
1202
1203 error = af_latin_hints_compute_edges( hints, dim );
1204 }
1205 return error;
1206 }
1207
1208
1209 FT_LOCAL_DEF( void )
1210 af_latin_hints_compute_blue_edges( AF_GlyphHints hints,
1211 AF_LatinMetrics metrics )
1212 {
1213 AF_AxisHints axis = &hints->axis[ AF_DIMENSION_VERT ];
1214 AF_Edge edge = axis->edges;
1215 AF_Edge edge_limit = edge + axis->num_edges;
1216 AF_LatinAxis latin = &metrics->axis[ AF_DIMENSION_VERT ];
1217 FT_Fixed scale = latin->scale;
1218
1219
1220 /* compute which blue zones are active, i.e. have their scaled */
1221 /* size < 3/4 pixels */
1222
1223 /* for each horizontal edge search the blue zone which is closest */
1224 for ( ; edge < edge_limit; edge++ )
1225 {
1226 FT_Int bb;
1227 AF_Width best_blue = NULL;
1228 FT_Pos best_dist; /* initial threshold */
1229
1230
1231 /* compute the initial threshold as a fraction of the EM size */
1232 best_dist = FT_MulFix( metrics->units_per_em / 40, scale );
1233
1234 if ( best_dist > 64 / 2 )
1235 best_dist = 64 / 2;
1236
1237 for ( bb = 0; bb < AF_LATIN_BLUE_MAX; bb++ )
1238 {
1239 AF_LatinBlue blue = latin->blues + bb;
1240 FT_Bool is_top_blue, is_major_dir;
1241
1242
1243 /* skip inactive blue zones (i.e., those that are too small) */
1244 if ( !( blue->flags & AF_LATIN_BLUE_ACTIVE ) )
1245 continue;
1246
1247 /* if it is a top zone, check for right edges -- if it is a bottom */
1248 /* zone, check for left edges */
1249 /* */
1250 /* of course, that's for TrueType */
1251 is_top_blue = (FT_Byte)( ( blue->flags & AF_LATIN_BLUE_TOP ) != 0 );
1252 is_major_dir = FT_BOOL( edge->dir == axis->major_dir );
1253
1254 /* if it is a top zone, the edge must be against the major */
1255 /* direction; if it is a bottom zone, it must be in the major */
1256 /* direction */
1257 if ( is_top_blue ^ is_major_dir )
1258 {
1259 FT_Pos dist;
1260
1261
1262 /* first of all, compare it to the reference position */
1263 dist = edge->fpos - blue->ref.org;
1264 if ( dist < 0 )
1265 dist = -dist;
1266
1267 dist = FT_MulFix( dist, scale );
1268 if ( dist < best_dist )
1269 {
1270 best_dist = dist;
1271 best_blue = & blue->ref;
1272 }
1273
1274 /* now, compare it to the overshoot position if the edge is */
1275 /* rounded, and if the edge is over the reference position of a */
1276 /* top zone, or under the reference position of a bottom zone */
1277 if ( edge->flags & AF_EDGE_ROUND && dist != 0 )
1278 {
1279 FT_Bool is_under_ref = FT_BOOL( edge->fpos < blue->ref.org );
1280
1281
1282 if ( is_top_blue ^ is_under_ref )
1283 {
1284 blue = latin->blues + bb;
1285 dist = edge->fpos - blue->shoot.org;
1286 if ( dist < 0 )
1287 dist = -dist;
1288
1289 dist = FT_MulFix( dist, scale );
1290 if ( dist < best_dist )
1291 {
1292 best_dist = dist;
1293 best_blue = & blue->shoot;
1294 }
1295 }
1296 }
1297 }
1298 }
1299
1300 if ( best_blue )
1301 edge->blue_edge = best_blue;
1302 }
1303 }
1304
1305
1306 static FT_Error
1307 af_latin_hints_init( AF_GlyphHints hints,
1308 AF_LatinMetrics metrics )
1309 {
1310 FT_Render_Mode mode;
1311 FT_UInt32 scaler_flags, other_flags;
1312 FT_Face face = metrics->root.scaler.face;
1313
1314
1315 af_glyph_hints_rescale( hints, (AF_ScriptMetrics)metrics );
1316
1317 /*
1318 * correct x_scale and y_scale if needed, since they may have
1319 * been modified `af_latin_metrics_scale_dim' above
1320 */
1321 hints->x_scale = metrics->axis[AF_DIMENSION_HORZ].scale;
1322 hints->x_delta = metrics->axis[AF_DIMENSION_HORZ].delta;
1323 hints->y_scale = metrics->axis[AF_DIMENSION_VERT].scale;
1324 hints->y_delta = metrics->axis[AF_DIMENSION_VERT].delta;
1325
1326 /* compute flags depending on render mode, etc. */
1327 mode = metrics->root.scaler.render_mode;
1328
1329 #if 0 /* #ifdef AF_USE_WARPER */
1330 if ( mode == FT_RENDER_MODE_LCD || mode == FT_RENDER_MODE_LCD_V )
1331 {
1332 metrics->root.scaler.render_mode = mode = FT_RENDER_MODE_NORMAL;
1333 }
1334 #endif
1335
1336 scaler_flags = hints->scaler_flags;
1337 other_flags = 0;
1338
1339 /*
1340 * We snap the width of vertical stems for the monochrome and
1341 * horizontal LCD rendering targets only.
1342 */
1343 if ( mode == FT_RENDER_MODE_MONO || mode == FT_RENDER_MODE_LCD )
1344 other_flags |= AF_LATIN_HINTS_HORZ_SNAP;
1345
1346 /*
1347 * We snap the width of horizontal stems for the monochrome and
1348 * vertical LCD rendering targets only.
1349 */
1350 if ( mode == FT_RENDER_MODE_MONO || mode == FT_RENDER_MODE_LCD_V )
1351 other_flags |= AF_LATIN_HINTS_VERT_SNAP;
1352
1353 /*
1354 * We adjust stems to full pixels only if we don't use the `light' mode.
1355 */
1356 if ( mode != FT_RENDER_MODE_LIGHT )
1357 other_flags |= AF_LATIN_HINTS_STEM_ADJUST;
1358
1359 if ( mode == FT_RENDER_MODE_MONO )
1360 other_flags |= AF_LATIN_HINTS_MONO;
1361
1362 /*
1363 * In `light' hinting mode we disable horizontal hinting completely.
1364 * We also do it if the face is italic.
1365 */
1366 if ( mode == FT_RENDER_MODE_LIGHT ||
1367 (face->style_flags & FT_STYLE_FLAG_ITALIC) != 0 )
1368 scaler_flags |= AF_SCALER_FLAG_NO_HORIZONTAL;
1369
1370 hints->scaler_flags = scaler_flags;
1371 hints->other_flags = other_flags;
1372
1373 return 0;
1374 }
1375
1376
1377 /*************************************************************************/
1378 /*************************************************************************/
1379 /***** *****/
1380 /***** L A T I N G L Y P H G R I D - F I T T I N G *****/
1381 /***** *****/
1382 /*************************************************************************/
1383 /*************************************************************************/
1384
1385 /* snap a given width in scaled coordinates to one of the */
1386 /* current standard widths */
1387
1388 static FT_Pos
1389 af_latin_snap_width( AF_Width widths,
1390 FT_Int count,
1391 FT_Pos width )
1392 {
1393 int n;
1394 FT_Pos best = 64 + 32 + 2;
1395 FT_Pos reference = width;
1396 FT_Pos scaled;
1397
1398
1399 for ( n = 0; n < count; n++ )
1400 {
1401 FT_Pos w;
1402 FT_Pos dist;
1403
1404
1405 w = widths[n].cur;
1406 dist = width - w;
1407 if ( dist < 0 )
1408 dist = -dist;
1409 if ( dist < best )
1410 {
1411 best = dist;
1412 reference = w;
1413 }
1414 }
1415
1416 scaled = FT_PIX_ROUND( reference );
1417
1418 if ( width >= reference )
1419 {
1420 if ( width < scaled + 48 )
1421 width = reference;
1422 }
1423 else
1424 {
1425 if ( width > scaled - 48 )
1426 width = reference;
1427 }
1428
1429 return width;
1430 }
1431
1432
1433 /* compute the snapped width of a given stem */
1434
1435 static FT_Pos
1436 af_latin_compute_stem_width( AF_GlyphHints hints,
1437 AF_Dimension dim,
1438 FT_Pos width,
1439 AF_Edge_Flags base_flags,
1440 AF_Edge_Flags stem_flags )
1441 {
1442 AF_LatinMetrics metrics = (AF_LatinMetrics) hints->metrics;
1443 AF_LatinAxis axis = & metrics->axis[dim];
1444 FT_Pos dist = width;
1445 FT_Int sign = 0;
1446 FT_Int vertical = ( dim == AF_DIMENSION_VERT );
1447
1448
1449 if ( !AF_LATIN_HINTS_DO_STEM_ADJUST( hints ) ||
1450 axis->extra_light )
1451 return width;
1452
1453 if ( dist < 0 )
1454 {
1455 dist = -width;
1456 sign = 1;
1457 }
1458
1459 if ( ( vertical && !AF_LATIN_HINTS_DO_VERT_SNAP( hints ) ) ||
1460 ( !vertical && !AF_LATIN_HINTS_DO_HORZ_SNAP( hints ) ) )
1461 {
1462 /* smooth hinting process: very lightly quantize the stem width */
1463
1464 /* leave the widths of serifs alone */
1465
1466 if ( ( stem_flags & AF_EDGE_SERIF ) && vertical && ( dist < 3 * 64 ) )
1467 goto Done_Width;
1468
1469 else if ( ( base_flags & AF_EDGE_ROUND ) )
1470 {
1471 if ( dist < 80 )
1472 dist = 64;
1473 }
1474 else if ( dist < 56 )
1475 dist = 56;
1476
1477 if ( axis->width_count > 0 )
1478 {
1479 FT_Pos delta;
1480
1481
1482 /* compare to standard width */
1483 if ( axis->width_count > 0 )
1484 {
1485 delta = dist - axis->widths[0].cur;
1486
1487 if ( delta < 0 )
1488 delta = -delta;
1489
1490 if ( delta < 40 )
1491 {
1492 dist = axis->widths[0].cur;
1493 if ( dist < 48 )
1494 dist = 48;
1495
1496 goto Done_Width;
1497 }
1498 }
1499
1500 if ( dist < 3 * 64 )
1501 {
1502 delta = dist & 63;
1503 dist &= -64;
1504
1505 if ( delta < 10 )
1506 dist += delta;
1507
1508 else if ( delta < 32 )
1509 dist += 10;
1510
1511 else if ( delta < 54 )
1512 dist += 54;
1513
1514 else
1515 dist += delta;
1516 }
1517 else
1518 dist = ( dist + 32 ) & ~63;
1519 }
1520 }
1521 else
1522 {
1523 /* strong hinting process: snap the stem width to integer pixels */
1524 FT_Pos org_dist = dist;
1525
1526
1527 dist = af_latin_snap_width( axis->widths, axis->width_count, dist );
1528
1529 if ( vertical )
1530 {
1531 /* in the case of vertical hinting, always round */
1532 /* the stem heights to integer pixels */
1533
1534 if ( dist >= 64 )
1535 dist = ( dist + 16 ) & ~63;
1536 else
1537 dist = 64;
1538 }
1539 else
1540 {
1541 if ( AF_LATIN_HINTS_DO_MONO( hints ) )
1542 {
1543 /* monochrome horizontal hinting: snap widths to integer pixels */
1544 /* with a different threshold */
1545
1546 if ( dist < 64 )
1547 dist = 64;
1548 else
1549 dist = ( dist + 32 ) & ~63;
1550 }
1551 else
1552 {
1553 /* for horizontal anti-aliased hinting, we adopt a more subtle */
1554 /* approach: we strengthen small stems, round stems whose size */
1555 /* is between 1 and 2 pixels to an integer, otherwise nothing */
1556
1557 if ( dist < 48 )
1558 dist = ( dist + 64 ) >> 1;
1559
1560 else if ( dist < 128 )
1561 {
1562 /* We only round to an integer width if the corresponding */
1563 /* distortion is less than 1/4 pixel. Otherwise this */
1564 /* makes everything worse since the diagonals, which are */
1565 /* not hinted, appear a lot bolder or thinner than the */
1566 /* vertical stems. */
1567
1568 FT_Int delta;
1569
1570
1571 dist = ( dist + 22 ) & ~63;
1572 delta = dist - org_dist;
1573 if ( delta < 0 )
1574 delta = -delta;
1575
1576 if (delta >= 16)
1577 {
1578 dist = org_dist;
1579 if ( dist < 48 )
1580 dist = ( dist + 64 ) >> 1;
1581 }
1582 }
1583 else
1584 /* round otherwise to prevent color fringes in LCD mode */
1585 dist = ( dist + 32 ) & ~63;
1586 }
1587 }
1588 }
1589
1590 Done_Width:
1591 if ( sign )
1592 dist = -dist;
1593
1594 return dist;
1595 }
1596
1597
1598 /* align one stem edge relative to the previous stem edge */
1599
1600 static void
1601 af_latin_align_linked_edge( AF_GlyphHints hints,
1602 AF_Dimension dim,
1603 AF_Edge base_edge,
1604 AF_Edge stem_edge )
1605 {
1606 FT_Pos dist = stem_edge->opos - base_edge->opos;
1607
1608 FT_Pos fitted_width = af_latin_compute_stem_width(
1609 hints, dim, dist,
1610 (AF_Edge_Flags)base_edge->flags,
1611 (AF_Edge_Flags)stem_edge->flags );
1612
1613
1614 stem_edge->pos = base_edge->pos + fitted_width;
1615
1616 AF_LOG(( "LINK: edge %d (opos=%.2f) linked to (%.2f), "
1617 "dist was %.2f, now %.2f\n",
1618 stem_edge-hints->axis[dim].edges, stem_edge->opos / 64.0,
1619 stem_edge->pos / 64.0, dist / 64.0, fitted_width / 64.0 ));
1620 }
1621
1622
1623 static void
1624 af_latin_align_serif_edge( AF_GlyphHints hints,
1625 AF_Edge base,
1626 AF_Edge serif )
1627 {
1628 FT_UNUSED( hints );
1629
1630 serif->pos = base->pos + (serif->opos - base->opos);
1631 }
1632
1633
1634 /*************************************************************************/
1635 /*************************************************************************/
1636 /*************************************************************************/
1637 /**** ****/
1638 /**** E D G E H I N T I N G ****/
1639 /**** ****/
1640 /*************************************************************************/
1641 /*************************************************************************/
1642 /*************************************************************************/
1643
1644
1645 FT_LOCAL_DEF( void )
1646 af_latin_hint_edges( AF_GlyphHints hints,
1647 AF_Dimension dim )
1648 {
1649 AF_AxisHints axis = &hints->axis[dim];
1650 AF_Edge edges = axis->edges;
1651 AF_Edge edge_limit = edges + axis->num_edges;
1652 FT_Int n_edges;
1653 AF_Edge edge;
1654 AF_Edge anchor = 0;
1655 FT_Int has_serifs = 0;
1656
1657
1658 /* we begin by aligning all stems relative to the blue zone */
1659 /* if needed -- that's only for horizontal edges */
1660
1661 if ( dim == AF_DIMENSION_VERT && AF_HINTS_DO_BLUES( hints ) )
1662 {
1663 for ( edge = edges; edge < edge_limit; edge++ )
1664 {
1665 AF_Width blue;
1666 AF_Edge edge1, edge2;
1667
1668
1669 if ( edge->flags & AF_EDGE_DONE )
1670 continue;
1671
1672 blue = edge->blue_edge;
1673 edge1 = NULL;
1674 edge2 = edge->link;
1675
1676 if ( blue )
1677 {
1678 edge1 = edge;
1679 }
1680 else if ( edge2 && edge2->blue_edge )
1681 {
1682 blue = edge2->blue_edge;
1683 edge1 = edge2;
1684 edge2 = edge;
1685 }
1686
1687 if ( !edge1 )
1688 continue;
1689
1690 AF_LOG(( "BLUE: edge %d (opos=%.2f) snapped to (%.2f), "
1691 "was (%.2f)\n",
1692 edge1-edges, edge1->opos / 64.0, blue->fit / 64.0,
1693 edge1->pos / 64.0 ));
1694
1695 edge1->pos = blue->fit;
1696 edge1->flags |= AF_EDGE_DONE;
1697
1698 if ( edge2 && !edge2->blue_edge )
1699 {
1700 af_latin_align_linked_edge( hints, dim, edge1, edge2 );
1701 edge2->flags |= AF_EDGE_DONE;
1702 }
1703
1704 if ( !anchor )
1705 anchor = edge;
1706 }
1707 }
1708
1709 /* now we will align all stem edges, trying to maintain the */
1710 /* relative order of stems in the glyph */
1711 for ( edge = edges; edge < edge_limit; edge++ )
1712 {
1713 AF_Edge edge2;
1714
1715
1716 if ( edge->flags & AF_EDGE_DONE )
1717 continue;
1718
1719 /* skip all non-stem edges */
1720 edge2 = edge->link;
1721 if ( !edge2 )
1722 {
1723 has_serifs++;
1724 continue;
1725 }
1726
1727 /* now align the stem */
1728
1729 /* this should not happen, but it's better to be safe */
1730 if ( edge2->blue_edge )
1731 {
1732 AF_LOG(( "ASSERTION FAILED for edge %d\n", edge2-edges ));
1733
1734 af_latin_align_linked_edge( hints, dim, edge2, edge );
1735 edge->flags |= AF_EDGE_DONE;
1736 continue;
1737 }
1738
1739 if ( !anchor )
1740 {
1741 FT_Pos org_len, org_center, cur_len;
1742 FT_Pos cur_pos1, error1, error2, u_off, d_off;
1743
1744
1745 org_len = edge2->opos - edge->opos;
1746 cur_len = af_latin_compute_stem_width(
1747 hints, dim, org_len,
1748 (AF_Edge_Flags)edge->flags,
1749 (AF_Edge_Flags)edge2->flags );
1750 if ( cur_len <= 64 )
1751 u_off = d_off = 32;
1752 else
1753 {
1754 u_off = 38;
1755 d_off = 26;
1756 }
1757
1758 if ( cur_len < 96 )
1759 {
1760 org_center = edge->opos + ( org_len >> 1 );
1761
1762 cur_pos1 = FT_PIX_ROUND( org_center );
1763
1764 error1 = org_center - ( cur_pos1 - u_off );
1765 if ( error1 < 0 )
1766 error1 = -error1;
1767
1768 error2 = org_center - ( cur_pos1 + d_off );
1769 if ( error2 < 0 )
1770 error2 = -error2;
1771
1772 if ( error1 < error2 )
1773 cur_pos1 -= u_off;
1774 else
1775 cur_pos1 += d_off;
1776
1777 edge->pos = cur_pos1 - cur_len / 2;
1778 edge2->pos = edge->pos + cur_len;
1779 }
1780 else
1781 edge->pos = FT_PIX_ROUND( edge->opos );
1782
1783 AF_LOG(( "ANCHOR: edge %d (opos=%.2f) and %d (opos=%.2f) "
1784 "snapped to (%.2f) (%.2f)\n",
1785 edge-edges, edge->opos / 64.0,
1786 edge2-edges, edge2->opos / 64.0,
1787 edge->pos / 64.0, edge2->pos / 64.0 ));
1788 anchor = edge;
1789
1790 edge->flags |= AF_EDGE_DONE;
1791
1792 af_latin_align_linked_edge( hints, dim, edge, edge2 );
1793 }
1794 else
1795 {
1796 FT_Pos org_pos, org_len, org_center, cur_len;
1797 FT_Pos cur_pos1, cur_pos2, delta1, delta2;
1798
1799
1800 org_pos = anchor->pos + ( edge->opos - anchor->opos );
1801 org_len = edge2->opos - edge->opos;
1802 org_center = org_pos + ( org_len >> 1 );
1803
1804 cur_len = af_latin_compute_stem_width(
1805 hints, dim, org_len,
1806 (AF_Edge_Flags)edge->flags,
1807 (AF_Edge_Flags)edge2->flags );
1808
1809 if ( edge2->flags & AF_EDGE_DONE )
1810 edge->pos = edge2->pos - cur_len;
1811
1812 else if ( cur_len < 96 )
1813 {
1814 FT_Pos u_off, d_off;
1815
1816
1817 cur_pos1 = FT_PIX_ROUND( org_center );
1818
1819 if (cur_len <= 64 )
1820 u_off = d_off = 32;
1821 else
1822 {
1823 u_off = 38;
1824 d_off = 26;
1825 }
1826
1827 delta1 = org_center - ( cur_pos1 - u_off );
1828 if ( delta1 < 0 )
1829 delta1 = -delta1;
1830
1831 delta2 = org_center - ( cur_pos1 + d_off );
1832 if ( delta2 < 0 )
1833 delta2 = -delta2;
1834
1835 if ( delta1 < delta2 )
1836 cur_pos1 -= u_off;
1837 else
1838 cur_pos1 += d_off;
1839
1840 edge->pos = cur_pos1 - cur_len / 2;
1841 edge2->pos = cur_pos1 + cur_len / 2;
1842
1843 AF_LOG(( "STEM: %d (opos=%.2f) to %d (opos=%.2f) "
1844 "snapped to (%.2f) and (%.2f)\n",
1845 edge-edges, edge->opos / 64.0,
1846 edge2-edges, edge2->opos / 64.0,
1847 edge->pos / 64.0, edge2->pos / 64.0 ));
1848 }
1849 else
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 cur_pos1 = FT_PIX_ROUND( org_pos );
1861 delta1 = cur_pos1 + ( cur_len >> 1 ) - org_center;
1862 if ( delta1 < 0 )
1863 delta1 = -delta1;
1864
1865 cur_pos2 = FT_PIX_ROUND( org_pos + org_len ) - cur_len;
1866 delta2 = cur_pos2 + ( cur_len >> 1 ) - org_center;
1867 if ( delta2 < 0 )
1868 delta2 = -delta2;
1869
1870 edge->pos = ( delta1 < delta2 ) ? cur_pos1 : cur_pos2;
1871 edge2->pos = edge->pos + cur_len;
1872
1873 AF_LOG(( "STEM: %d (opos=%.2f) to %d (opos=%.2f) "
1874 "snapped to (%.2f) and (%.2f)\n",
1875 edge-edges, edge->opos / 64.0,
1876 edge2-edges, edge2->opos / 64.0,
1877 edge->pos / 64.0, edge2->pos / 64.0 ));
1878 }
1879
1880 edge->flags |= AF_EDGE_DONE;
1881 edge2->flags |= AF_EDGE_DONE;
1882
1883 if ( edge > edges && edge->pos < edge[-1].pos )
1884 {
1885 AF_LOG(( "BOUND: %d (pos=%.2f) to (%.2f)\n",
1886 edge-edges, edge->pos / 64.0, edge[-1].pos / 64.0 ));
1887 edge->pos = edge[-1].pos;
1888 }
1889 }
1890 }
1891
1892 /* make sure that lowercase m's maintain their symmetry */
1893
1894 /* In general, lowercase m's have six vertical edges if they are sans */
1895 /* serif, or twelve if they are with serifs. This implementation is */
1896 /* based on that assumption, and seems to work very well with most */
1897 /* faces. However, if for a certain face this assumption is not */
1898 /* true, the m is just rendered like before. In addition, any stem */
1899 /* correction will only be applied to symmetrical glyphs (even if the */
1900 /* glyph is not an m), so the potential for unwanted distortion is */
1901 /* relatively low. */
1902
1903 /* We don't handle horizontal edges since we can't easily assure that */
1904 /* the third (lowest) stem aligns with the base line; it might end up */
1905 /* one pixel higher or lower. */
1906
1907 n_edges = edge_limit - edges;
1908 if ( dim == AF_DIMENSION_HORZ && ( n_edges == 6 || n_edges == 12 ) )
1909 {
1910 AF_Edge edge1, edge2, edge3;
1911 FT_Pos dist1, dist2, span, delta;
1912
1913
1914 if ( n_edges == 6 )
1915 {
1916 edge1 = edges;
1917 edge2 = edges + 2;
1918 edge3 = edges + 4;
1919 }
1920 else
1921 {
1922 edge1 = edges + 1;
1923 edge2 = edges + 5;
1924 edge3 = edges + 9;
1925 }
1926
1927 dist1 = edge2->opos - edge1->opos;
1928 dist2 = edge3->opos - edge2->opos;
1929
1930 span = dist1 - dist2;
1931 if ( span < 0 )
1932 span = -span;
1933
1934 if ( span < 8 )
1935 {
1936 delta = edge3->pos - ( 2 * edge2->pos - edge1->pos );
1937 edge3->pos -= delta;
1938 if ( edge3->link )
1939 edge3->link->pos -= delta;
1940
1941 /* move the serifs along with the stem */
1942 if ( n_edges == 12 )
1943 {
1944 ( edges + 8 )->pos -= delta;
1945 ( edges + 11 )->pos -= delta;
1946 }
1947
1948 edge3->flags |= AF_EDGE_DONE;
1949 if ( edge3->link )
1950 edge3->link->flags |= AF_EDGE_DONE;
1951 }
1952 }
1953
1954 if ( has_serifs || !anchor )
1955 {
1956 /*
1957 * now hint the remaining edges (serifs and single) in order
1958 * to complete our processing
1959 */
1960 for ( edge = edges; edge < edge_limit; edge++ )
1961 {
1962 FT_Pos delta;
1963
1964
1965 if ( edge->flags & AF_EDGE_DONE )
1966 continue;
1967
1968 delta = 1000;
1969
1970 if ( edge->serif )
1971 {
1972 delta = edge->serif->opos - edge->opos;
1973 if ( delta < 0 )
1974 delta = -delta;
1975 }
1976
1977 if ( delta < 64 + 16 )
1978 {
1979 af_latin_align_serif_edge( hints, edge->serif, edge );
1980 AF_LOG(( "SERIF: edge %d (opos=%.2f) serif to %d (opos=%.2f) "
1981 "aligned to (%.2f)\n",
1982 edge-edges, edge->opos / 64.0,
1983 edge->serif - edges, edge->serif->opos / 64.0,
1984 edge->pos / 64.0 ));
1985 }
1986 else if ( !anchor )
1987 {
1988 AF_LOG(( "SERIF_ANCHOR: edge %d (opos=%.2f) snapped to (%.2f)\n",
1989 edge-edges, edge->opos / 64.0, edge->pos / 64.0 ));
1990 edge->pos = FT_PIX_ROUND( edge->opos );
1991 anchor = edge;
1992 }
1993 else
1994 {
1995 AF_Edge before, after;
1996
1997
1998 for ( before = edge - 1; before >= edges; before-- )
1999 if ( before->flags & AF_EDGE_DONE )
2000 break;
2001
2002 for ( after = edge + 1; after < edge_limit; after++ )
2003 if ( after->flags & AF_EDGE_DONE )
2004 break;
2005
2006 if ( before >= edges && before < edge &&
2007 after < edge_limit && after > edge )
2008 {
2009 edge->pos = before->pos +
2010 FT_MulDiv( edge->opos - before->opos,
2011 after->pos - before->pos,
2012 after->opos - before->opos );
2013 AF_LOG(( "SERIF_LINK1: edge %d (opos=%.2f) snapped to (%.2f) "
2014 "from %d (opos=%.2f)\n",
2015 edge-edges, edge->opos / 64.0,
2016 edge->pos / 64.0, before - edges,
2017 before->opos / 64.0 ));
2018 }
2019 else
2020 {
2021 edge->pos = anchor->pos +
2022 ( ( edge->opos - anchor->opos + 16 ) & ~31 );
2023 AF_LOG(( "SERIF_LINK2: edge %d (opos=%.2f) snapped to (%.2f)\n",
2024 edge-edges, edge->opos / 64.0, edge->pos / 64.0 ));
2025 }
2026 }
2027
2028 edge->flags |= AF_EDGE_DONE;
2029
2030 if ( edge > edges && edge->pos < edge[-1].pos )
2031 edge->pos = edge[-1].pos;
2032
2033 if ( edge + 1 < edge_limit &&
2034 edge[1].flags & AF_EDGE_DONE &&
2035 edge->pos > edge[1].pos )
2036 edge->pos = edge[1].pos;
2037 }
2038 }
2039 }
2040
2041
2042 static FT_Error
2043 af_latin_hints_apply( AF_GlyphHints hints,
2044 FT_Outline* outline,
2045 AF_LatinMetrics metrics )
2046 {
2047 FT_Error error;
2048 int dim;
2049
2050
2051 error = af_glyph_hints_reload( hints, outline, 1 );
2052 if ( error )
2053 goto Exit;
2054
2055 /* analyze glyph outline */
2056 #ifdef AF_USE_WARPER
2057 if ( metrics->root.scaler.render_mode == FT_RENDER_MODE_LIGHT ||
2058 AF_HINTS_DO_HORIZONTAL( hints ) )
2059 #else
2060 if ( AF_HINTS_DO_HORIZONTAL( hints ) )
2061 #endif
2062 {
2063 error = af_latin_hints_detect_features( hints, AF_DIMENSION_HORZ );
2064 if ( error )
2065 goto Exit;
2066 }
2067
2068 if ( AF_HINTS_DO_VERTICAL( hints ) )
2069 {
2070 error = af_latin_hints_detect_features( hints, AF_DIMENSION_VERT );
2071 if ( error )
2072 goto Exit;
2073
2074 af_latin_hints_compute_blue_edges( hints, metrics );
2075 }
2076
2077 /* grid-fit the outline */
2078 for ( dim = 0; dim < AF_DIMENSION_MAX; dim++ )
2079 {
2080 #ifdef AF_USE_WARPER
2081 if ( ( dim == AF_DIMENSION_HORZ &&
2082 metrics->root.scaler.render_mode == FT_RENDER_MODE_LIGHT ) )
2083 {
2084 AF_WarperRec warper;
2085 FT_Fixed scale;
2086 FT_Pos delta;
2087
2088
2089 af_warper_compute( &warper, hints, dim, &scale, &delta );
2090 af_glyph_hints_scale_dim( hints, dim, scale, delta );
2091 continue;
2092 }
2093 #endif
2094
2095 if ( ( dim == AF_DIMENSION_HORZ && AF_HINTS_DO_HORIZONTAL( hints ) ) ||
2096 ( dim == AF_DIMENSION_VERT && AF_HINTS_DO_VERTICAL( hints ) ) )
2097 {
2098 af_latin_hint_edges( hints, (AF_Dimension)dim );
2099 af_glyph_hints_align_edge_points( hints, (AF_Dimension)dim );
2100 af_glyph_hints_align_strong_points( hints, (AF_Dimension)dim );
2101 af_glyph_hints_align_weak_points( hints, (AF_Dimension)dim );
2102 }
2103 }
2104 af_glyph_hints_save( hints, outline );
2105
2106 Exit:
2107 return error;
2108 }
2109
2110
2111 /*************************************************************************/
2112 /*************************************************************************/
2113 /***** *****/
2114 /***** L A T I N S C R I P T C L A S S *****/
2115 /***** *****/
2116 /*************************************************************************/
2117 /*************************************************************************/
2118
2119
2120 /* XXX: this should probably fine tuned to differentiate better between */
2121 /* scripts... */
2122
2123 static const AF_Script_UniRangeRec af_latin_uniranges[] =
2124 {
2125 { 0x0020, 0x007F }, /* Basic Latin (no control characters) */
2126 { 0x00A0, 0x00FF }, /* Latin-1 Supplement (no control characters) */
2127 { 0x0100, 0x017F }, /* Latin Extended-A */
2128 { 0x0180, 0x024F }, /* Latin Extended-B */
2129 { 0x0250, 0x02AF }, /* IPA Extensions */
2130 { 0x02B0, 0x02FF }, /* Spacing Modifier Letters */
2131 { 0x0300, 0x036F }, /* Combining Diacritical Marks */
2132 { 0x0370, 0x03FF }, /* Greek and Coptic */
2133 { 0x0400, 0x04FF }, /* Cyrillic */
2134 { 0x0500, 0x052F }, /* Cyrillic Supplement */
2135 { 0x1D00, 0x1D7F }, /* Phonetic Extensions */
2136 { 0x1D80, 0x1DBF }, /* Phonetic Extensions Supplement */
2137 { 0x1DC0, 0x1DFF }, /* Combining Diacritical Marks Supplement */
2138 { 0x1E00, 0x1EFF }, /* Latin Extended Additional */
2139 { 0x1F00, 0x1FFF }, /* Greek Extended */
2140 { 0x2000, 0x206F }, /* General Punctuation */
2141 { 0x2070, 0x209F }, /* Superscripts and Subscripts */
2142 { 0x20A0, 0x20CF }, /* Currency Symbols */
2143 { 0x2150, 0x218F }, /* Number Forms */
2144 { 0x2460, 0x24FF }, /* Enclosed Alphanumerics */
2145 { 0 , 0 }
2146 };
2147
2148
2149 FT_CALLBACK_TABLE_DEF const AF_ScriptClassRec
2150 af_latin_script_class =
2151 {
2152 AF_SCRIPT_LATIN,
2153 af_latin_uniranges,
2154
2155 sizeof( AF_LatinMetricsRec ),
2156
2157 (AF_Script_InitMetricsFunc) af_latin_metrics_init,
2158 (AF_Script_ScaleMetricsFunc)af_latin_metrics_scale,
2159 (AF_Script_DoneMetricsFunc) NULL,
2160
2161 (AF_Script_InitHintsFunc) af_latin_hints_init,
2162 (AF_Script_ApplyHintsFunc) af_latin_hints_apply
2163 };
2164
2165
2166 /* END */