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