09b3609fbaebd2703dcfea49fee5daa5c42c301a
[reactos.git] / reactos / sdk / lib / 3rdparty / freetype / src / autofit / aflatin.c
1 /***************************************************************************/
2 /* */
3 /* aflatin.c */
4 /* */
5 /* Auto-fitter hinting routines for latin writing system (body). */
6 /* */
7 /* Copyright 2003-2016 by */
8 /* David Turner, Robert Wilhelm, and Werner Lemberg. */
9 /* */
10 /* This file is part of the FreeType project, and may only be used, */
11 /* modified, and distributed under the terms of the FreeType project */
12 /* license, LICENSE.TXT. By continuing to use, modify, or distribute */
13 /* this file you indicate that you have read the license and */
14 /* understand and accept it fully. */
15 /* */
16 /***************************************************************************/
17
18
19 #include <ft2build.h>
20 #include FT_ADVANCES_H
21 #include FT_INTERNAL_DEBUG_H
22
23 #include "afglobal.h"
24 #include "afpic.h"
25 #include "aflatin.h"
26 #include "aferrors.h"
27
28
29 #ifdef AF_CONFIG_OPTION_USE_WARPER
30 #include "afwarp.h"
31 #endif
32
33
34 /*************************************************************************/
35 /* */
36 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
37 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
38 /* messages during execution. */
39 /* */
40 #undef FT_COMPONENT
41 #define FT_COMPONENT trace_aflatin
42
43
44 /* needed for computation of round vs. flat segments */
45 #define FLAT_THRESHOLD( x ) ( x / 14 )
46
47
48 /*************************************************************************/
49 /*************************************************************************/
50 /***** *****/
51 /***** L A T I N G L O B A L M E T R I C S *****/
52 /***** *****/
53 /*************************************************************************/
54 /*************************************************************************/
55
56
57 /* Find segments and links, compute all stem widths, and initialize */
58 /* standard width and height for the glyph with given charcode. */
59
60 FT_LOCAL_DEF( void )
61 af_latin_metrics_init_widths( AF_LatinMetrics metrics,
62 FT_Face face )
63 {
64 /* scan the array of segments in each direction */
65 #ifdef __REACTOS__
66 AF_GlyphHintsRec *hints = malloc(sizeof(AF_GlyphHintsRec));
67 #else
68 AF_GlyphHintsRec hints[1];
69 #endif
70
71
72 FT_TRACE5(( "\n"
73 "latin standard widths computation (style `%s')\n"
74 "=====================================================\n"
75 "\n",
76 af_style_names[metrics->root.style_class->style] ));
77
78 af_glyph_hints_init( hints, face->memory );
79
80 metrics->axis[AF_DIMENSION_HORZ].width_count = 0;
81 metrics->axis[AF_DIMENSION_VERT].width_count = 0;
82
83 {
84 FT_Error error;
85 FT_ULong glyph_index;
86 int dim;
87 #ifdef __REACTOS__
88 AF_LatinMetricsRec *dummy = malloc(sizeof(AF_LatinMetricsRec));
89 #else
90 AF_LatinMetricsRec dummy[1];
91 #endif
92 AF_Scaler scaler = &dummy->root.scaler;
93
94 #ifdef FT_CONFIG_OPTION_PIC
95 AF_FaceGlobals globals = metrics->root.globals;
96 #endif
97
98 AF_StyleClass style_class = metrics->root.style_class;
99 AF_ScriptClass script_class = AF_SCRIPT_CLASSES_GET
100 [style_class->script];
101
102 void* shaper_buf;
103 const char* p;
104
105 #ifdef FT_DEBUG_LEVEL_TRACE
106 FT_ULong ch = 0;
107 #endif
108
109 p = script_class->standard_charstring;
110 shaper_buf = af_shaper_buf_create( face );
111
112 /*
113 * We check a list of standard characters to catch features like
114 * `c2sc' (small caps from caps) that don't contain lowercase letters
115 * by definition, or other features that mainly operate on numerals.
116 * The first match wins.
117 */
118
119 glyph_index = 0;
120 while ( *p )
121 {
122 unsigned int num_idx;
123
124 #ifdef FT_DEBUG_LEVEL_TRACE
125 const char* p_old;
126 #endif
127
128
129 while ( *p == ' ' )
130 p++;
131
132 #ifdef FT_DEBUG_LEVEL_TRACE
133 p_old = p;
134 GET_UTF8_CHAR( ch, p_old );
135 #endif
136
137 /* reject input that maps to more than a single glyph */
138 p = af_shaper_get_cluster( p, &metrics->root, shaper_buf, &num_idx );
139 if ( num_idx > 1 )
140 continue;
141
142 /* otherwise exit loop if we have a result */
143 glyph_index = af_shaper_get_elem( &metrics->root,
144 shaper_buf,
145 0,
146 NULL,
147 NULL );
148 if ( glyph_index )
149 break;
150 }
151
152 af_shaper_buf_destroy( face, shaper_buf );
153
154 if ( !glyph_index )
155 goto Exit;
156
157 FT_TRACE5(( "standard character: U+%04lX (glyph index %d)\n",
158 ch, glyph_index ));
159
160 error = FT_Load_Glyph( face, glyph_index, FT_LOAD_NO_SCALE );
161 if ( error || face->glyph->outline.n_points <= 0 )
162 goto Exit;
163
164 FT_ZERO( dummy );
165
166 dummy->units_per_em = metrics->units_per_em;
167
168 scaler->x_scale = 0x10000L;
169 scaler->y_scale = 0x10000L;
170 scaler->x_delta = 0;
171 scaler->y_delta = 0;
172
173 scaler->face = face;
174 scaler->render_mode = FT_RENDER_MODE_NORMAL;
175 scaler->flags = 0;
176
177 af_glyph_hints_rescale( hints, (AF_StyleMetrics)dummy );
178
179 error = af_glyph_hints_reload( hints, &face->glyph->outline );
180 if ( error )
181 goto Exit;
182
183 for ( dim = 0; dim < AF_DIMENSION_MAX; dim++ )
184 {
185 AF_LatinAxis axis = &metrics->axis[dim];
186 AF_AxisHints axhints = &hints->axis[dim];
187 AF_Segment seg, limit, link;
188 FT_UInt num_widths = 0;
189
190
191 error = af_latin_hints_compute_segments( hints,
192 (AF_Dimension)dim );
193 if ( error )
194 goto Exit;
195
196 /*
197 * We assume that the glyphs selected for the stem width
198 * computation are `featureless' enough so that the linking
199 * algorithm works fine without adjustments of its scoring
200 * function.
201 */
202 af_latin_hints_link_segments( hints,
203 0,
204 NULL,
205 (AF_Dimension)dim );
206
207 seg = axhints->segments;
208 limit = seg + axhints->num_segments;
209
210 for ( ; seg < limit; seg++ )
211 {
212 link = seg->link;
213
214 /* we only consider stem segments there! */
215 if ( link && link->link == seg && link > seg )
216 {
217 FT_Pos dist;
218
219
220 dist = seg->pos - link->pos;
221 if ( dist < 0 )
222 dist = -dist;
223
224 if ( num_widths < AF_LATIN_MAX_WIDTHS )
225 axis->widths[num_widths++].org = dist;
226 }
227 }
228
229 /* this also replaces multiple almost identical stem widths */
230 /* with a single one (the value 100 is heuristic) */
231 af_sort_and_quantize_widths( &num_widths, axis->widths,
232 dummy->units_per_em / 100 );
233 axis->width_count = num_widths;
234 }
235
236 Exit:
237 for ( dim = 0; dim < AF_DIMENSION_MAX; dim++ )
238 {
239 AF_LatinAxis axis = &metrics->axis[dim];
240 FT_Pos stdw;
241
242
243 stdw = ( axis->width_count > 0 ) ? axis->widths[0].org
244 : AF_LATIN_CONSTANT( metrics, 50 );
245
246 /* let's try 20% of the smallest width */
247 axis->edge_distance_threshold = stdw / 5;
248 axis->standard_width = stdw;
249 axis->extra_light = 0;
250
251 #ifdef FT_DEBUG_LEVEL_TRACE
252 {
253 FT_UInt i;
254
255
256 FT_TRACE5(( "%s widths:\n",
257 dim == AF_DIMENSION_VERT ? "horizontal"
258 : "vertical" ));
259
260 FT_TRACE5(( " %d (standard)", axis->standard_width ));
261 for ( i = 1; i < axis->width_count; i++ )
262 FT_TRACE5(( " %d", axis->widths[i].org ));
263
264 FT_TRACE5(( "\n" ));
265 }
266 #endif
267 }
268 #ifdef __REACTOS__
269 free(dummy);
270 #endif
271 }
272
273 FT_TRACE5(( "\n" ));
274
275 af_glyph_hints_done( hints );
276
277 #ifdef __REACTOS__
278 free(hints);
279 #endif
280
281 }
282
283
284 /* Find all blue zones. Flat segments give the reference points, */
285 /* round segments the overshoot positions. */
286
287 static void
288 af_latin_metrics_init_blues( AF_LatinMetrics metrics,
289 FT_Face face )
290 {
291 FT_Pos flats [AF_BLUE_STRING_MAX_LEN];
292 FT_Pos rounds[AF_BLUE_STRING_MAX_LEN];
293
294 FT_UInt num_flats;
295 FT_UInt num_rounds;
296
297 AF_LatinBlue blue;
298 FT_Error error;
299 AF_LatinAxis axis = &metrics->axis[AF_DIMENSION_VERT];
300 FT_Outline outline;
301
302 AF_StyleClass sc = metrics->root.style_class;
303
304 AF_Blue_Stringset bss = sc->blue_stringset;
305 const AF_Blue_StringRec* bs = &af_blue_stringsets[bss];
306
307 FT_Pos flat_threshold = FLAT_THRESHOLD( metrics->units_per_em );
308
309 void* shaper_buf;
310
311
312 /* we walk over the blue character strings as specified in the */
313 /* style's entry in the `af_blue_stringset' array */
314
315 FT_TRACE5(( "latin blue zones computation\n"
316 "============================\n"
317 "\n" ));
318
319 shaper_buf = af_shaper_buf_create( face );
320
321 for ( ; bs->string != AF_BLUE_STRING_MAX; bs++ )
322 {
323 const char* p = &af_blue_strings[bs->string];
324 FT_Pos* blue_ref;
325 FT_Pos* blue_shoot;
326 FT_Pos ascender;
327 FT_Pos descender;
328
329
330 #ifdef FT_DEBUG_LEVEL_TRACE
331 {
332 FT_Bool have_flag = 0;
333
334
335 FT_TRACE5(( "blue zone %d", axis->blue_count ));
336
337 if ( bs->properties )
338 {
339 FT_TRACE5(( " (" ));
340
341 if ( AF_LATIN_IS_TOP_BLUE( bs ) )
342 {
343 FT_TRACE5(( "top" ));
344 have_flag = 1;
345 }
346 else if ( AF_LATIN_IS_SUB_TOP_BLUE( bs ) )
347 {
348 FT_TRACE5(( "sub top" ));
349 have_flag = 1;
350 }
351
352 if ( AF_LATIN_IS_NEUTRAL_BLUE( bs ) )
353 {
354 if ( have_flag )
355 FT_TRACE5(( ", " ));
356 FT_TRACE5(( "neutral" ));
357 have_flag = 1;
358 }
359
360 if ( AF_LATIN_IS_X_HEIGHT_BLUE( bs ) )
361 {
362 if ( have_flag )
363 FT_TRACE5(( ", " ));
364 FT_TRACE5(( "small top" ));
365 have_flag = 1;
366 }
367
368 if ( AF_LATIN_IS_LONG_BLUE( bs ) )
369 {
370 if ( have_flag )
371 FT_TRACE5(( ", " ));
372 FT_TRACE5(( "long" ));
373 }
374
375 FT_TRACE5(( ")" ));
376 }
377
378 FT_TRACE5(( ":\n" ));
379 }
380 #endif /* FT_DEBUG_LEVEL_TRACE */
381
382 num_flats = 0;
383 num_rounds = 0;
384 ascender = 0;
385 descender = 0;
386
387 while ( *p )
388 {
389 FT_ULong glyph_index;
390 FT_Long y_offset;
391 FT_Int best_point, best_contour_first, best_contour_last;
392 FT_Vector* points;
393
394 FT_Pos best_y_extremum; /* same as points.y */
395 FT_Bool best_round = 0;
396
397 unsigned int i, num_idx;
398
399 #ifdef FT_DEBUG_LEVEL_TRACE
400 const char* p_old;
401 FT_ULong ch;
402 #endif
403
404
405 while ( *p == ' ' )
406 p++;
407
408 #ifdef FT_DEBUG_LEVEL_TRACE
409 p_old = p;
410 GET_UTF8_CHAR( ch, p_old );
411 #endif
412
413 p = af_shaper_get_cluster( p, &metrics->root, shaper_buf, &num_idx );
414
415 if ( !num_idx )
416 {
417 FT_TRACE5(( " U+%04lX unavailable\n", ch ));
418 continue;
419 }
420
421 if ( AF_LATIN_IS_TOP_BLUE( bs ) )
422 best_y_extremum = FT_INT_MIN;
423 else
424 best_y_extremum = FT_INT_MAX;
425
426 /* iterate over all glyph elements of the character cluster */
427 /* and get the data of the `biggest' one */
428 for ( i = 0; i < num_idx; i++ )
429 {
430 FT_Pos best_y;
431 FT_Bool round = 0;
432
433
434 /* load the character in the face -- skip unknown or empty ones */
435 glyph_index = af_shaper_get_elem( &metrics->root,
436 shaper_buf,
437 i,
438 NULL,
439 &y_offset );
440 if ( glyph_index == 0 )
441 {
442 FT_TRACE5(( " U+%04lX unavailable\n", ch ));
443 continue;
444 }
445
446 error = FT_Load_Glyph( face, glyph_index, FT_LOAD_NO_SCALE );
447 outline = face->glyph->outline;
448 /* reject glyphs that don't produce any rendering */
449 if ( error || outline.n_points <= 2 )
450 {
451 #ifdef FT_DEBUG_LEVEL_TRACE
452 if ( num_idx == 1 )
453 FT_TRACE5(( " U+%04lX contains no (usable) outlines\n", ch ));
454 else
455 FT_TRACE5(( " component %d of cluster starting with U+%04lX"
456 " contains no (usable) outlines\n", i, ch ));
457 #endif
458 continue;
459 }
460
461 /* now compute min or max point indices and coordinates */
462 points = outline.points;
463 best_point = -1;
464 best_y = 0; /* make compiler happy */
465 best_contour_first = 0; /* ditto */
466 best_contour_last = 0; /* ditto */
467
468 {
469 FT_Int nn;
470 FT_Int first = 0;
471 FT_Int last = -1;
472
473
474 for ( nn = 0; nn < outline.n_contours; first = last + 1, nn++ )
475 {
476 FT_Int old_best_point = best_point;
477 FT_Int pp;
478
479
480 last = outline.contours[nn];
481
482 /* Avoid single-point contours since they are never */
483 /* rasterized. In some fonts, they correspond to mark */
484 /* attachment points that are way outside of the glyph's */
485 /* real outline. */
486 if ( last <= first )
487 continue;
488
489 if ( AF_LATIN_IS_TOP_BLUE( bs ) ||
490 AF_LATIN_IS_SUB_TOP_BLUE( bs ) )
491 {
492 for ( pp = first; pp <= last; pp++ )
493 {
494 if ( best_point < 0 || points[pp].y > best_y )
495 {
496 best_point = pp;
497 best_y = points[pp].y;
498 ascender = FT_MAX( ascender, best_y + y_offset );
499 }
500 else
501 descender = FT_MIN( descender, points[pp].y + y_offset );
502 }
503 }
504 else
505 {
506 for ( pp = first; pp <= last; pp++ )
507 {
508 if ( best_point < 0 || points[pp].y < best_y )
509 {
510 best_point = pp;
511 best_y = points[pp].y;
512 descender = FT_MIN( descender, best_y + y_offset );
513 }
514 else
515 ascender = FT_MAX( ascender, points[pp].y + y_offset );
516 }
517 }
518
519 if ( best_point != old_best_point )
520 {
521 best_contour_first = first;
522 best_contour_last = last;
523 }
524 }
525 }
526
527 /* now check whether the point belongs to a straight or round */
528 /* segment; we first need to find in which contour the extremum */
529 /* lies, then inspect its previous and next points */
530 if ( best_point >= 0 )
531 {
532 FT_Pos best_x = points[best_point].x;
533 FT_Int prev, next;
534 FT_Int best_segment_first, best_segment_last;
535 FT_Int best_on_point_first, best_on_point_last;
536 FT_Pos dist;
537
538
539 best_segment_first = best_point;
540 best_segment_last = best_point;
541
542 if ( FT_CURVE_TAG( outline.tags[best_point] ) == FT_CURVE_TAG_ON )
543 {
544 best_on_point_first = best_point;
545 best_on_point_last = best_point;
546 }
547 else
548 {
549 best_on_point_first = -1;
550 best_on_point_last = -1;
551 }
552
553 /* look for the previous and next points on the contour */
554 /* that are not on the same Y coordinate, then threshold */
555 /* the `closeness'... */
556 prev = best_point;
557 next = prev;
558
559 do
560 {
561 if ( prev > best_contour_first )
562 prev--;
563 else
564 prev = best_contour_last;
565
566 dist = FT_ABS( points[prev].y - best_y );
567 /* accept a small distance or a small angle (both values are */
568 /* heuristic; value 20 corresponds to approx. 2.9 degrees) */
569 if ( dist > 5 )
570 if ( FT_ABS( points[prev].x - best_x ) <= 20 * dist )
571 break;
572
573 best_segment_first = prev;
574
575 if ( FT_CURVE_TAG( outline.tags[prev] ) == FT_CURVE_TAG_ON )
576 {
577 best_on_point_first = prev;
578 if ( best_on_point_last < 0 )
579 best_on_point_last = prev;
580 }
581
582 } while ( prev != best_point );
583
584 do
585 {
586 if ( next < best_contour_last )
587 next++;
588 else
589 next = best_contour_first;
590
591 dist = FT_ABS( points[next].y - best_y );
592 if ( dist > 5 )
593 if ( FT_ABS( points[next].x - best_x ) <= 20 * dist )
594 break;
595
596 best_segment_last = next;
597
598 if ( FT_CURVE_TAG( outline.tags[next] ) == FT_CURVE_TAG_ON )
599 {
600 best_on_point_last = next;
601 if ( best_on_point_first < 0 )
602 best_on_point_first = next;
603 }
604
605 } while ( next != best_point );
606
607 if ( AF_LATIN_IS_LONG_BLUE( bs ) )
608 {
609 /* If this flag is set, we have an additional constraint to */
610 /* get the blue zone distance: Find a segment of the topmost */
611 /* (or bottommost) contour that is longer than a heuristic */
612 /* threshold. This ensures that small bumps in the outline */
613 /* are ignored (for example, the `vertical serifs' found in */
614 /* many Hebrew glyph designs). */
615
616 /* If this segment is long enough, we are done. Otherwise, */
617 /* search the segment next to the extremum that is long */
618 /* enough, has the same direction, and a not too large */
619 /* vertical distance from the extremum. Note that the */
620 /* algorithm doesn't check whether the found segment is */
621 /* actually the one (vertically) nearest to the extremum. */
622
623 /* heuristic threshold value */
624 FT_Pos length_threshold = metrics->units_per_em / 25;
625
626
627 dist = FT_ABS( points[best_segment_last].x -
628 points[best_segment_first].x );
629
630 if ( dist < length_threshold &&
631 best_segment_last - best_segment_first + 2 <=
632 best_contour_last - best_contour_first )
633 {
634 /* heuristic threshold value */
635 FT_Pos height_threshold = metrics->units_per_em / 4;
636
637 FT_Int first;
638 FT_Int last;
639 FT_Bool hit;
640
641 /* we intentionally declare these two variables */
642 /* outside of the loop since various compilers emit */
643 /* incorrect warning messages otherwise, talking about */
644 /* `possibly uninitialized variables' */
645 FT_Int p_first = 0; /* make compiler happy */
646 FT_Int p_last = 0;
647
648 FT_Bool left2right;
649
650
651 /* compute direction */
652 prev = best_point;
653
654 do
655 {
656 if ( prev > best_contour_first )
657 prev--;
658 else
659 prev = best_contour_last;
660
661 if ( points[prev].x != best_x )
662 break;
663
664 } while ( prev != best_point );
665
666 /* skip glyph for the degenerate case */
667 if ( prev == best_point )
668 continue;
669
670 left2right = FT_BOOL( points[prev].x < points[best_point].x );
671
672 first = best_segment_last;
673 last = first;
674 hit = 0;
675
676 do
677 {
678 FT_Bool l2r;
679 FT_Pos d;
680
681
682 if ( !hit )
683 {
684 /* no hit; adjust first point */
685 first = last;
686
687 /* also adjust first and last on point */
688 if ( FT_CURVE_TAG( outline.tags[first] ) ==
689 FT_CURVE_TAG_ON )
690 {
691 p_first = first;
692 p_last = first;
693 }
694 else
695 {
696 p_first = -1;
697 p_last = -1;
698 }
699
700 hit = 1;
701 }
702
703 if ( last < best_contour_last )
704 last++;
705 else
706 last = best_contour_first;
707
708 if ( FT_ABS( best_y - points[first].y ) > height_threshold )
709 {
710 /* vertical distance too large */
711 hit = 0;
712 continue;
713 }
714
715 /* same test as above */
716 dist = FT_ABS( points[last].y - points[first].y );
717 if ( dist > 5 )
718 if ( FT_ABS( points[last].x - points[first].x ) <=
719 20 * dist )
720 {
721 hit = 0;
722 continue;
723 }
724
725 if ( FT_CURVE_TAG( outline.tags[last] ) == FT_CURVE_TAG_ON )
726 {
727 p_last = last;
728 if ( p_first < 0 )
729 p_first = last;
730 }
731
732 l2r = FT_BOOL( points[first].x < points[last].x );
733 d = FT_ABS( points[last].x - points[first].x );
734
735 if ( l2r == left2right &&
736 d >= length_threshold )
737 {
738 /* all constraints are met; update segment after */
739 /* finding its end */
740 do
741 {
742 if ( last < best_contour_last )
743 last++;
744 else
745 last = best_contour_first;
746
747 d = FT_ABS( points[last].y - points[first].y );
748 if ( d > 5 )
749 if ( FT_ABS( points[next].x - points[first].x ) <=
750 20 * dist )
751 {
752 if ( last > best_contour_first )
753 last--;
754 else
755 last = best_contour_last;
756 break;
757 }
758
759 p_last = last;
760
761 if ( FT_CURVE_TAG( outline.tags[last] ) ==
762 FT_CURVE_TAG_ON )
763 {
764 p_last = last;
765 if ( p_first < 0 )
766 p_first = last;
767 }
768
769 } while ( last != best_segment_first );
770
771 best_y = points[first].y;
772
773 best_segment_first = first;
774 best_segment_last = last;
775
776 best_on_point_first = p_first;
777 best_on_point_last = p_last;
778
779 break;
780 }
781
782 } while ( last != best_segment_first );
783 }
784 }
785
786 /* for computing blue zones, we add the y offset as returned */
787 /* by the currently used OpenType feature -- for example, */
788 /* superscript glyphs might be identical to subscript glyphs */
789 /* with a vertical shift */
790 best_y += y_offset;
791
792 #ifdef FT_DEBUG_LEVEL_TRACE
793 if ( num_idx == 1 )
794 FT_TRACE5(( " U+%04lX: best_y = %5ld", ch, best_y ));
795 else
796 FT_TRACE5(( " component %d of cluster starting with U+%04lX:"
797 " best_y = %5ld", i, ch, best_y ));
798 #endif
799
800 /* now set the `round' flag depending on the segment's kind: */
801 /* */
802 /* - if the horizontal distance between the first and last */
803 /* `on' point is larger than a heuristic threshold */
804 /* we have a flat segment */
805 /* - if either the first or the last point of the segment is */
806 /* an `off' point, the segment is round, otherwise it is */
807 /* flat */
808 if ( best_on_point_first >= 0 &&
809 best_on_point_last >= 0 &&
810 ( FT_ABS( points[best_on_point_last].x -
811 points[best_on_point_first].x ) ) >
812 flat_threshold )
813 round = 0;
814 else
815 round = FT_BOOL(
816 FT_CURVE_TAG( outline.tags[best_segment_first] ) !=
817 FT_CURVE_TAG_ON ||
818 FT_CURVE_TAG( outline.tags[best_segment_last] ) !=
819 FT_CURVE_TAG_ON );
820
821 if ( round && AF_LATIN_IS_NEUTRAL_BLUE( bs ) )
822 {
823 /* only use flat segments for a neutral blue zone */
824 FT_TRACE5(( " (round, skipped)\n" ));
825 continue;
826 }
827
828 FT_TRACE5(( " (%s)\n", round ? "round" : "flat" ));
829 }
830
831 if ( AF_LATIN_IS_TOP_BLUE( bs ) )
832 {
833 if ( best_y > best_y_extremum )
834 {
835 best_y_extremum = best_y;
836 best_round = round;
837 }
838 }
839 else
840 {
841 if ( best_y < best_y_extremum )
842 {
843 best_y_extremum = best_y;
844 best_round = round;
845 }
846 }
847
848 } /* end for loop */
849
850 if ( !( best_y_extremum == FT_INT_MIN ||
851 best_y_extremum == FT_INT_MAX ) )
852 {
853 if ( best_round )
854 rounds[num_rounds++] = best_y_extremum;
855 else
856 flats[num_flats++] = best_y_extremum;
857 }
858
859 } /* end while loop */
860
861 if ( num_flats == 0 && num_rounds == 0 )
862 {
863 /*
864 * we couldn't find a single glyph to compute this blue zone,
865 * we will simply ignore it then
866 */
867 FT_TRACE5(( " empty\n" ));
868 continue;
869 }
870
871 /* we have computed the contents of the `rounds' and `flats' tables, */
872 /* now determine the reference and overshoot position of the blue -- */
873 /* we simply take the median value after a simple sort */
874 af_sort_pos( num_rounds, rounds );
875 af_sort_pos( num_flats, flats );
876
877 blue = &axis->blues[axis->blue_count];
878 blue_ref = &blue->ref.org;
879 blue_shoot = &blue->shoot.org;
880
881 axis->blue_count++;
882
883 if ( num_flats == 0 )
884 {
885 *blue_ref =
886 *blue_shoot = rounds[num_rounds / 2];
887 }
888 else if ( num_rounds == 0 )
889 {
890 *blue_ref =
891 *blue_shoot = flats[num_flats / 2];
892 }
893 else
894 {
895 *blue_ref = flats [num_flats / 2];
896 *blue_shoot = rounds[num_rounds / 2];
897 }
898
899 /* there are sometimes problems: if the overshoot position of top */
900 /* zones is under its reference position, or the opposite for bottom */
901 /* zones. We must thus check everything there and correct the errors */
902 if ( *blue_shoot != *blue_ref )
903 {
904 FT_Pos ref = *blue_ref;
905 FT_Pos shoot = *blue_shoot;
906 FT_Bool over_ref = FT_BOOL( shoot > ref );
907
908
909 if ( ( AF_LATIN_IS_TOP_BLUE( bs ) ||
910 AF_LATIN_IS_SUB_TOP_BLUE( bs) ) ^ over_ref )
911 {
912 *blue_ref =
913 *blue_shoot = ( shoot + ref ) / 2;
914
915 FT_TRACE5(( " [overshoot smaller than reference,"
916 " taking mean value]\n" ));
917 }
918 }
919
920 blue->ascender = ascender;
921 blue->descender = descender;
922
923 blue->flags = 0;
924 if ( AF_LATIN_IS_TOP_BLUE( bs ) )
925 blue->flags |= AF_LATIN_BLUE_TOP;
926 if ( AF_LATIN_IS_SUB_TOP_BLUE( bs ) )
927 blue->flags |= AF_LATIN_BLUE_SUB_TOP;
928 if ( AF_LATIN_IS_NEUTRAL_BLUE( bs ) )
929 blue->flags |= AF_LATIN_BLUE_NEUTRAL;
930
931 /*
932 * The following flag is used later to adjust the y and x scales
933 * in order to optimize the pixel grid alignment of the top of small
934 * letters.
935 */
936 if ( AF_LATIN_IS_X_HEIGHT_BLUE( bs ) )
937 blue->flags |= AF_LATIN_BLUE_ADJUSTMENT;
938
939 FT_TRACE5(( " -> reference = %ld\n"
940 " overshoot = %ld\n",
941 *blue_ref, *blue_shoot ));
942
943 } /* end for loop */
944
945 af_shaper_buf_destroy( face, shaper_buf );
946
947 FT_TRACE5(( "\n" ));
948
949 return;
950 }
951
952
953 /* Check whether all ASCII digits have the same advance width. */
954
955 FT_LOCAL_DEF( void )
956 af_latin_metrics_check_digits( AF_LatinMetrics metrics,
957 FT_Face face )
958 {
959 FT_Bool started = 0, same_width = 1;
960 FT_Fixed advance, old_advance = 0;
961
962 void* shaper_buf;
963
964 /* in all supported charmaps, digits have character codes 0x30-0x39 */
965 const char digits[] = "0 1 2 3 4 5 6 7 8 9";
966 const char* p;
967
968
969 p = digits;
970 shaper_buf = af_shaper_buf_create( face );
971
972 while ( *p )
973 {
974 FT_ULong glyph_index;
975 unsigned int num_idx;
976
977
978 /* reject input that maps to more than a single glyph */
979 p = af_shaper_get_cluster( p, &metrics->root, shaper_buf, &num_idx );
980 if ( num_idx > 1 )
981 continue;
982
983 glyph_index = af_shaper_get_elem( &metrics->root,
984 shaper_buf,
985 0,
986 &advance,
987 NULL );
988 if ( !glyph_index )
989 continue;
990
991 if ( started )
992 {
993 if ( advance != old_advance )
994 {
995 same_width = 0;
996 break;
997 }
998 }
999 else
1000 {
1001 old_advance = advance;
1002 started = 1;
1003 }
1004 }
1005
1006 af_shaper_buf_destroy( face, shaper_buf );
1007
1008 metrics->root.digits_have_same_width = same_width;
1009 }
1010
1011
1012 /* Initialize global metrics. */
1013
1014 FT_LOCAL_DEF( FT_Error )
1015 af_latin_metrics_init( AF_LatinMetrics metrics,
1016 FT_Face face )
1017 {
1018 FT_CharMap oldmap = face->charmap;
1019
1020
1021 metrics->units_per_em = face->units_per_EM;
1022
1023 if ( !FT_Select_Charmap( face, FT_ENCODING_UNICODE ) )
1024 {
1025 af_latin_metrics_init_widths( metrics, face );
1026 af_latin_metrics_init_blues( metrics, face );
1027 af_latin_metrics_check_digits( metrics, face );
1028 }
1029
1030 FT_Set_Charmap( face, oldmap );
1031 return FT_Err_Ok;
1032 }
1033
1034
1035 /* Adjust scaling value, then scale and shift widths */
1036 /* and blue zones (if applicable) for given dimension. */
1037
1038 static void
1039 af_latin_metrics_scale_dim( AF_LatinMetrics metrics,
1040 AF_Scaler scaler,
1041 AF_Dimension dim )
1042 {
1043 FT_Fixed scale;
1044 FT_Pos delta;
1045 AF_LatinAxis axis;
1046 FT_UInt nn;
1047
1048
1049 if ( dim == AF_DIMENSION_HORZ )
1050 {
1051 scale = scaler->x_scale;
1052 delta = scaler->x_delta;
1053 }
1054 else
1055 {
1056 scale = scaler->y_scale;
1057 delta = scaler->y_delta;
1058 }
1059
1060 axis = &metrics->axis[dim];
1061
1062 if ( axis->org_scale == scale && axis->org_delta == delta )
1063 return;
1064
1065 axis->org_scale = scale;
1066 axis->org_delta = delta;
1067
1068 /*
1069 * correct X and Y scale to optimize the alignment of the top of small
1070 * letters to the pixel grid
1071 */
1072 {
1073 AF_LatinAxis Axis = &metrics->axis[AF_DIMENSION_VERT];
1074 AF_LatinBlue blue = NULL;
1075
1076
1077 for ( nn = 0; nn < Axis->blue_count; nn++ )
1078 {
1079 if ( Axis->blues[nn].flags & AF_LATIN_BLUE_ADJUSTMENT )
1080 {
1081 blue = &Axis->blues[nn];
1082 break;
1083 }
1084 }
1085
1086 if ( blue )
1087 {
1088 FT_Pos scaled;
1089 FT_Pos threshold;
1090 FT_Pos fitted;
1091 FT_UInt limit;
1092 FT_UInt ppem;
1093
1094
1095 scaled = FT_MulFix( blue->shoot.org, scale );
1096 ppem = metrics->root.scaler.face->size->metrics.x_ppem;
1097 limit = metrics->root.globals->increase_x_height;
1098 threshold = 40;
1099
1100 /* if the `increase-x-height' property is active, */
1101 /* we round up much more often */
1102 if ( limit &&
1103 ppem <= limit &&
1104 ppem >= AF_PROP_INCREASE_X_HEIGHT_MIN )
1105 threshold = 52;
1106
1107 fitted = ( scaled + threshold ) & ~63;
1108
1109 if ( scaled != fitted )
1110 {
1111 #if 0
1112 if ( dim == AF_DIMENSION_HORZ )
1113 {
1114 if ( fitted < scaled )
1115 scale -= scale / 50; /* scale *= 0.98 */
1116 }
1117 else
1118 #endif
1119 if ( dim == AF_DIMENSION_VERT )
1120 {
1121 FT_Pos max_height;
1122 FT_Pos dist;
1123 FT_Fixed new_scale;
1124
1125
1126 new_scale = FT_MulDiv( scale, fitted, scaled );
1127
1128 /* the scaling should not change the result by more than two pixels */
1129 max_height = metrics->units_per_em;
1130
1131 for ( nn = 0; nn < Axis->blue_count; nn++ )
1132 {
1133 max_height = FT_MAX( max_height, Axis->blues[nn].ascender );
1134 max_height = FT_MAX( max_height, -Axis->blues[nn].descender );
1135 }
1136
1137 dist = FT_ABS( FT_MulFix( max_height, new_scale - scale ) );
1138 dist &= ~127;
1139
1140 if ( dist == 0 )
1141 {
1142 FT_TRACE5((
1143 "af_latin_metrics_scale_dim:"
1144 " x height alignment (style `%s'):\n"
1145 " "
1146 " vertical scaling changed from %.4f to %.4f (by %d%%)\n"
1147 "\n",
1148 af_style_names[metrics->root.style_class->style],
1149 scale / 65536.0,
1150 new_scale / 65536.0,
1151 ( fitted - scaled ) * 100 / scaled ));
1152
1153 scale = new_scale;
1154 }
1155 #ifdef FT_DEBUG_LEVEL_TRACE
1156 else
1157 {
1158 FT_TRACE5((
1159 "af_latin_metrics_scale_dim:"
1160 " x height alignment (style `%s'):\n"
1161 " "
1162 " excessive vertical scaling abandoned\n"
1163 "\n",
1164 af_style_names[metrics->root.style_class->style] ));
1165 }
1166 #endif
1167 }
1168 }
1169 }
1170 }
1171
1172 axis->scale = scale;
1173 axis->delta = delta;
1174
1175 if ( dim == AF_DIMENSION_HORZ )
1176 {
1177 metrics->root.scaler.x_scale = scale;
1178 metrics->root.scaler.x_delta = delta;
1179 }
1180 else
1181 {
1182 metrics->root.scaler.y_scale = scale;
1183 metrics->root.scaler.y_delta = delta;
1184 }
1185
1186 FT_TRACE5(( "%s widths (style `%s')\n",
1187 dim == AF_DIMENSION_HORZ ? "horizontal" : "vertical",
1188 af_style_names[metrics->root.style_class->style] ));
1189
1190 /* scale the widths */
1191 for ( nn = 0; nn < axis->width_count; nn++ )
1192 {
1193 AF_Width width = axis->widths + nn;
1194
1195
1196 width->cur = FT_MulFix( width->org, scale );
1197 width->fit = width->cur;
1198
1199 FT_TRACE5(( " %d scaled to %.2f\n",
1200 width->org,
1201 width->cur / 64.0 ));
1202 }
1203
1204 FT_TRACE5(( "\n" ));
1205
1206 /* an extra-light axis corresponds to a standard width that is */
1207 /* smaller than 5/8 pixels */
1208 axis->extra_light =
1209 (FT_Bool)( FT_MulFix( axis->standard_width, scale ) < 32 + 8 );
1210
1211 #ifdef FT_DEBUG_LEVEL_TRACE
1212 if ( axis->extra_light )
1213 FT_TRACE5(( "`%s' style is extra light (at current resolution)\n"
1214 "\n",
1215 af_style_names[metrics->root.style_class->style] ));
1216 #endif
1217
1218 if ( dim == AF_DIMENSION_VERT )
1219 {
1220 #ifdef FT_DEBUG_LEVEL_TRACE
1221 if ( axis->blue_count )
1222 FT_TRACE5(( "blue zones (style `%s')\n",
1223 af_style_names[metrics->root.style_class->style] ));
1224 #endif
1225
1226 /* scale the blue zones */
1227 for ( nn = 0; nn < axis->blue_count; nn++ )
1228 {
1229 AF_LatinBlue blue = &axis->blues[nn];
1230 FT_Pos dist;
1231
1232
1233 blue->ref.cur = FT_MulFix( blue->ref.org, scale ) + delta;
1234 blue->ref.fit = blue->ref.cur;
1235 blue->shoot.cur = FT_MulFix( blue->shoot.org, scale ) + delta;
1236 blue->shoot.fit = blue->shoot.cur;
1237 blue->flags &= ~AF_LATIN_BLUE_ACTIVE;
1238
1239 /* a blue zone is only active if it is less than 3/4 pixels tall */
1240 dist = FT_MulFix( blue->ref.org - blue->shoot.org, scale );
1241 if ( dist <= 48 && dist >= -48 )
1242 {
1243 #if 0
1244 FT_Pos delta1;
1245 #endif
1246 FT_Pos delta2;
1247
1248
1249 /* use discrete values for blue zone widths */
1250
1251 #if 0
1252
1253 /* generic, original code */
1254 delta1 = blue->shoot.org - blue->ref.org;
1255 delta2 = delta1;
1256 if ( delta1 < 0 )
1257 delta2 = -delta2;
1258
1259 delta2 = FT_MulFix( delta2, scale );
1260
1261 if ( delta2 < 32 )
1262 delta2 = 0;
1263 else if ( delta2 < 64 )
1264 delta2 = 32 + ( ( ( delta2 - 32 ) + 16 ) & ~31 );
1265 else
1266 delta2 = FT_PIX_ROUND( delta2 );
1267
1268 if ( delta1 < 0 )
1269 delta2 = -delta2;
1270
1271 blue->ref.fit = FT_PIX_ROUND( blue->ref.cur );
1272 blue->shoot.fit = blue->ref.fit + delta2;
1273
1274 #else
1275
1276 /* simplified version due to abs(dist) <= 48 */
1277 delta2 = dist;
1278 if ( dist < 0 )
1279 delta2 = -delta2;
1280
1281 if ( delta2 < 32 )
1282 delta2 = 0;
1283 else if ( delta2 < 48 )
1284 delta2 = 32;
1285 else
1286 delta2 = 64;
1287
1288 if ( dist < 0 )
1289 delta2 = -delta2;
1290
1291 blue->ref.fit = FT_PIX_ROUND( blue->ref.cur );
1292 blue->shoot.fit = blue->ref.fit - delta2;
1293
1294 #endif
1295
1296 blue->flags |= AF_LATIN_BLUE_ACTIVE;
1297 }
1298 }
1299
1300 /* use sub-top blue zone only if it doesn't overlap with */
1301 /* another (non-sup-top) blue zone; otherwise, the */
1302 /* effect would be similar to a neutral blue zone, which */
1303 /* is not desired here */
1304 for ( nn = 0; nn < axis->blue_count; nn++ )
1305 {
1306 AF_LatinBlue blue = &axis->blues[nn];
1307 FT_UInt i;
1308
1309
1310 if ( !( blue->flags & AF_LATIN_BLUE_SUB_TOP ) )
1311 continue;
1312 if ( !( blue->flags & AF_LATIN_BLUE_ACTIVE ) )
1313 continue;
1314
1315 for ( i = 0; i < axis->blue_count; i++ )
1316 {
1317 AF_LatinBlue b = &axis->blues[i];
1318
1319
1320 if ( b->flags & AF_LATIN_BLUE_SUB_TOP )
1321 continue;
1322 if ( !( b->flags & AF_LATIN_BLUE_ACTIVE ) )
1323 continue;
1324
1325 if ( b->ref.fit <= blue->shoot.fit &&
1326 b->shoot.fit >= blue->ref.fit )
1327 {
1328 blue->flags &= ~AF_LATIN_BLUE_ACTIVE;
1329 break;
1330 }
1331 }
1332 }
1333
1334 #ifdef FT_DEBUG_LEVEL_TRACE
1335 for ( nn = 0; nn < axis->blue_count; nn++ )
1336 {
1337 AF_LatinBlue blue = &axis->blues[nn];
1338
1339
1340 FT_TRACE5(( " reference %d: %d scaled to %.2f%s\n"
1341 " overshoot %d: %d scaled to %.2f%s\n",
1342 nn,
1343 blue->ref.org,
1344 blue->ref.fit / 64.0,
1345 blue->flags & AF_LATIN_BLUE_ACTIVE ? ""
1346 : " (inactive)",
1347 nn,
1348 blue->shoot.org,
1349 blue->shoot.fit / 64.0,
1350 blue->flags & AF_LATIN_BLUE_ACTIVE ? ""
1351 : " (inactive)" ));
1352 }
1353 #endif
1354 }
1355 }
1356
1357
1358 /* Scale global values in both directions. */
1359
1360 FT_LOCAL_DEF( void )
1361 af_latin_metrics_scale( AF_LatinMetrics metrics,
1362 AF_Scaler scaler )
1363 {
1364 metrics->root.scaler.render_mode = scaler->render_mode;
1365 metrics->root.scaler.face = scaler->face;
1366 metrics->root.scaler.flags = scaler->flags;
1367
1368 af_latin_metrics_scale_dim( metrics, scaler, AF_DIMENSION_HORZ );
1369 af_latin_metrics_scale_dim( metrics, scaler, AF_DIMENSION_VERT );
1370 }
1371
1372
1373 /* Extract standard_width from writing system/script specific */
1374 /* metrics class. */
1375
1376 FT_LOCAL_DEF( void )
1377 af_latin_get_standard_widths( AF_LatinMetrics metrics,
1378 FT_Pos* stdHW,
1379 FT_Pos* stdVW )
1380 {
1381 if ( stdHW )
1382 *stdHW = metrics->axis[AF_DIMENSION_VERT].standard_width;
1383
1384 if ( stdVW )
1385 *stdVW = metrics->axis[AF_DIMENSION_HORZ].standard_width;
1386 }
1387
1388
1389 /*************************************************************************/
1390 /*************************************************************************/
1391 /***** *****/
1392 /***** L A T I N G L Y P H A N A L Y S I S *****/
1393 /***** *****/
1394 /*************************************************************************/
1395 /*************************************************************************/
1396
1397
1398 /* Walk over all contours and compute its segments. */
1399
1400 FT_LOCAL_DEF( FT_Error )
1401 af_latin_hints_compute_segments( AF_GlyphHints hints,
1402 AF_Dimension dim )
1403 {
1404 AF_LatinMetrics metrics = (AF_LatinMetrics)hints->metrics;
1405 AF_AxisHints axis = &hints->axis[dim];
1406 FT_Memory memory = hints->memory;
1407 FT_Error error = FT_Err_Ok;
1408 AF_Segment segment = NULL;
1409 AF_SegmentRec seg0;
1410 AF_Point* contour = hints->contours;
1411 AF_Point* contour_limit = contour + hints->num_contours;
1412 AF_Direction major_dir, segment_dir;
1413
1414 FT_Pos flat_threshold = FLAT_THRESHOLD( metrics->units_per_em );
1415
1416
1417 FT_ZERO( &seg0 );
1418 seg0.score = 32000;
1419 seg0.flags = AF_EDGE_NORMAL;
1420
1421 major_dir = (AF_Direction)FT_ABS( axis->major_dir );
1422 segment_dir = major_dir;
1423
1424 axis->num_segments = 0;
1425
1426 /* set up (u,v) in each point */
1427 if ( dim == AF_DIMENSION_HORZ )
1428 {
1429 AF_Point point = hints->points;
1430 AF_Point limit = point + hints->num_points;
1431
1432
1433 for ( ; point < limit; point++ )
1434 {
1435 point->u = point->fx;
1436 point->v = point->fy;
1437 }
1438 }
1439 else
1440 {
1441 AF_Point point = hints->points;
1442 AF_Point limit = point + hints->num_points;
1443
1444
1445 for ( ; point < limit; point++ )
1446 {
1447 point->u = point->fy;
1448 point->v = point->fx;
1449 }
1450 }
1451
1452 /* do each contour separately */
1453 for ( ; contour < contour_limit; contour++ )
1454 {
1455 AF_Point point = contour[0];
1456 AF_Point last = point->prev;
1457 int on_edge = 0;
1458
1459 /* we call values measured along a segment (point->v) */
1460 /* `coordinates', and values orthogonal to it (point->u) */
1461 /* `positions' */
1462 FT_Pos min_pos = 32000;
1463 FT_Pos max_pos = -32000;
1464 FT_Pos min_coord = 32000;
1465 FT_Pos max_coord = -32000;
1466 FT_UShort min_flags = AF_FLAG_NONE;
1467 FT_UShort max_flags = AF_FLAG_NONE;
1468 FT_Pos min_on_coord = 32000;
1469 FT_Pos max_on_coord = -32000;
1470
1471 FT_Bool passed;
1472
1473 AF_Segment prev_segment = NULL;
1474
1475 FT_Pos prev_min_pos = min_pos;
1476 FT_Pos prev_max_pos = max_pos;
1477 FT_Pos prev_min_coord = min_coord;
1478 FT_Pos prev_max_coord = max_coord;
1479 FT_UShort prev_min_flags = min_flags;
1480 FT_UShort prev_max_flags = max_flags;
1481 FT_Pos prev_min_on_coord = min_on_coord;
1482 FT_Pos prev_max_on_coord = max_on_coord;
1483
1484
1485 if ( FT_ABS( last->out_dir ) == major_dir &&
1486 FT_ABS( point->out_dir ) == major_dir )
1487 {
1488 /* we are already on an edge, try to locate its start */
1489 last = point;
1490
1491 for (;;)
1492 {
1493 point = point->prev;
1494 if ( FT_ABS( point->out_dir ) != major_dir )
1495 {
1496 point = point->next;
1497 break;
1498 }
1499 if ( point == last )
1500 break;
1501 }
1502 }
1503
1504 last = point;
1505 passed = 0;
1506
1507 for (;;)
1508 {
1509 FT_Pos u, v;
1510
1511
1512 if ( on_edge )
1513 {
1514 /* get minimum and maximum position */
1515 u = point->u;
1516 if ( u < min_pos )
1517 min_pos = u;
1518 if ( u > max_pos )
1519 max_pos = u;
1520
1521 /* get minimum and maximum coordinate together with flags */
1522 v = point->v;
1523 if ( v < min_coord )
1524 {
1525 min_coord = v;
1526 min_flags = point->flags;
1527 }
1528 if ( v > max_coord )
1529 {
1530 max_coord = v;
1531 max_flags = point->flags;
1532 }
1533
1534 /* get minimum and maximum coordinate of `on' points */
1535 if ( !( point->flags & AF_FLAG_CONTROL ) )
1536 {
1537 v = point->v;
1538 if ( v < min_on_coord )
1539 min_on_coord = v;
1540 if ( v > max_on_coord )
1541 max_on_coord = v;
1542 }
1543
1544 if ( point->out_dir != segment_dir || point == last )
1545 {
1546 /* check whether the new segment's start point is identical to */
1547 /* the previous segment's end point; for example, this might */
1548 /* happen for spikes */
1549
1550 if ( !prev_segment || segment->first != prev_segment->last )
1551 {
1552 /* points are different: we are just leaving an edge, thus */
1553 /* record a new segment */
1554
1555 segment->last = point;
1556 segment->pos = (FT_Short)( ( min_pos + max_pos ) >> 1 );
1557 segment->delta = (FT_Short)( ( max_pos - min_pos ) >> 1 );
1558
1559 /* a segment is round if either its first or last point */
1560 /* is a control point, and the length of the on points */
1561 /* inbetween doesn't exceed a heuristic limit */
1562 if ( ( min_flags | max_flags ) & AF_FLAG_CONTROL &&
1563 ( max_on_coord - min_on_coord ) < flat_threshold )
1564 segment->flags |= AF_EDGE_ROUND;
1565
1566 segment->min_coord = (FT_Short)min_coord;
1567 segment->max_coord = (FT_Short)max_coord;
1568 segment->height = segment->max_coord - segment->min_coord;
1569
1570 prev_segment = segment;
1571 prev_min_pos = min_pos;
1572 prev_max_pos = max_pos;
1573 prev_min_coord = min_coord;
1574 prev_max_coord = max_coord;
1575 prev_min_flags = min_flags;
1576 prev_max_flags = max_flags;
1577 prev_min_on_coord = min_on_coord;
1578 prev_max_on_coord = max_on_coord;
1579 }
1580 else
1581 {
1582 /* points are the same: we don't create a new segment but */
1583 /* merge the current segment with the previous one */
1584
1585 if ( prev_segment->last->in_dir == point->in_dir )
1586 {
1587 /* we have identical directions (this can happen for */
1588 /* degenerate outlines that move zig-zag along the main */
1589 /* axis without changing the coordinate value of the other */
1590 /* axis, and where the segments have just been merged): */
1591 /* unify segments */
1592
1593 /* update constraints */
1594
1595 if ( prev_min_pos < min_pos )
1596 min_pos = prev_min_pos;
1597 if ( prev_max_pos > max_pos )
1598 max_pos = prev_max_pos;
1599
1600 if ( prev_min_coord < min_coord )
1601 {
1602 min_coord = prev_min_coord;
1603 min_flags = prev_min_flags;
1604 }
1605 if ( prev_max_coord > max_coord )
1606 {
1607 max_coord = prev_max_coord;
1608 max_flags = prev_max_flags;
1609 }
1610
1611 if ( prev_min_on_coord < min_on_coord )
1612 min_on_coord = prev_min_on_coord;
1613 if ( prev_max_on_coord > max_on_coord )
1614 max_on_coord = prev_max_on_coord;
1615
1616 prev_segment->last = point;
1617 prev_segment->pos = (FT_Short)( ( min_pos +
1618 max_pos ) >> 1 );
1619
1620 if ( ( min_flags | max_flags ) & AF_FLAG_CONTROL &&
1621 ( max_on_coord - min_on_coord ) < flat_threshold )
1622 prev_segment->flags |= AF_EDGE_ROUND;
1623 else
1624 prev_segment->flags &= ~AF_EDGE_ROUND;
1625
1626 prev_segment->min_coord = (FT_Short)min_coord;
1627 prev_segment->max_coord = (FT_Short)max_coord;
1628 prev_segment->height = prev_segment->max_coord -
1629 prev_segment->min_coord;
1630 }
1631 else
1632 {
1633 /* we have different directions; use the properties of the */
1634 /* longer segment and discard the other one */
1635
1636 if ( FT_ABS( prev_max_coord - prev_min_coord ) >
1637 FT_ABS( max_coord - min_coord ) )
1638 {
1639 /* discard current segment */
1640
1641 if ( min_pos < prev_min_pos )
1642 prev_min_pos = min_pos;
1643 if ( max_pos > prev_max_pos )
1644 prev_max_pos = max_pos;
1645
1646 prev_segment->last = point;
1647 prev_segment->pos = (FT_Short)( ( prev_min_pos +
1648 prev_max_pos ) >> 1 );
1649 }
1650 else
1651 {
1652 /* discard previous segment */
1653
1654 if ( prev_min_pos < min_pos )
1655 min_pos = prev_min_pos;
1656 if ( prev_max_pos > max_pos )
1657 max_pos = prev_max_pos;
1658
1659 segment->last = point;
1660 segment->pos = (FT_Short)( ( min_pos + max_pos ) >> 1 );
1661
1662 if ( ( min_flags | max_flags ) & AF_FLAG_CONTROL &&
1663 ( max_on_coord - min_on_coord ) < flat_threshold )
1664 segment->flags |= AF_EDGE_ROUND;
1665
1666 segment->min_coord = (FT_Short)min_coord;
1667 segment->max_coord = (FT_Short)max_coord;
1668 segment->height = segment->max_coord -
1669 segment->min_coord;
1670
1671 *prev_segment = *segment;
1672
1673 prev_min_pos = min_pos;
1674 prev_max_pos = max_pos;
1675 prev_min_coord = min_coord;
1676 prev_max_coord = max_coord;
1677 prev_min_flags = min_flags;
1678 prev_max_flags = max_flags;
1679 prev_min_on_coord = min_on_coord;
1680 prev_max_on_coord = max_on_coord;
1681 }
1682 }
1683
1684 axis->num_segments--;
1685 }
1686
1687 on_edge = 0;
1688 segment = NULL;
1689
1690 /* fall through */
1691 }
1692 }
1693
1694 /* now exit if we are at the start/end point */
1695 if ( point == last )
1696 {
1697 if ( passed )
1698 break;
1699 passed = 1;
1700 }
1701
1702 /* if we are not on an edge, check whether the major direction */
1703 /* coincides with the current point's `out' direction, or */
1704 /* whether we have a single-point contour */
1705 if ( !on_edge &&
1706 ( FT_ABS( point->out_dir ) == major_dir ||
1707 point == point->prev ) )
1708 {
1709 /* this is the start of a new segment! */
1710 segment_dir = (AF_Direction)point->out_dir;
1711
1712 error = af_axis_hints_new_segment( axis, memory, &segment );
1713 if ( error )
1714 goto Exit;
1715
1716 /* clear all segment fields */
1717 segment[0] = seg0;
1718
1719 segment->dir = (FT_Char)segment_dir;
1720 segment->first = point;
1721 segment->last = point;
1722
1723 /* `af_axis_hints_new_segment' reallocates memory, */
1724 /* thus we have to refresh the `prev_segment' pointer */
1725 if ( prev_segment )
1726 prev_segment = segment - 1;
1727
1728 min_pos = max_pos = point->u;
1729 min_coord = max_coord = point->v;
1730 min_flags = max_flags = point->flags;
1731
1732 if ( point->flags & AF_FLAG_CONTROL )
1733 {
1734 min_on_coord = 32000;
1735 max_on_coord = -32000;
1736 }
1737 else
1738 min_on_coord = max_on_coord = point->v;
1739
1740 on_edge = 1;
1741
1742 if ( point == point->prev )
1743 {
1744 /* we have a one-point segment: this is a one-point */
1745 /* contour with `in' and `out' direction set to */
1746 /* AF_DIR_NONE */
1747 segment->pos = (FT_Short)min_pos;
1748
1749 if (point->flags & AF_FLAG_CONTROL)
1750 segment->flags |= AF_EDGE_ROUND;
1751
1752 segment->min_coord = (FT_Short)point->v;
1753 segment->max_coord = (FT_Short)point->v;
1754 segment->height = 0;
1755
1756 on_edge = 0;
1757 segment = NULL;
1758 }
1759 }
1760
1761 point = point->next;
1762 }
1763
1764 } /* contours */
1765
1766
1767 /* now slightly increase the height of segments if this makes */
1768 /* sense -- this is used to better detect and ignore serifs */
1769 {
1770 AF_Segment segments = axis->segments;
1771 AF_Segment segments_end = segments + axis->num_segments;
1772
1773
1774 for ( segment = segments; segment < segments_end; segment++ )
1775 {
1776 AF_Point first = segment->first;
1777 AF_Point last = segment->last;
1778 FT_Pos first_v = first->v;
1779 FT_Pos last_v = last->v;
1780
1781
1782 if ( first_v < last_v )
1783 {
1784 AF_Point p;
1785
1786
1787 p = first->prev;
1788 if ( p->v < first_v )
1789 segment->height = (FT_Short)( segment->height +
1790 ( ( first_v - p->v ) >> 1 ) );
1791
1792 p = last->next;
1793 if ( p->v > last_v )
1794 segment->height = (FT_Short)( segment->height +
1795 ( ( p->v - last_v ) >> 1 ) );
1796 }
1797 else
1798 {
1799 AF_Point p;
1800
1801
1802 p = first->prev;
1803 if ( p->v > first_v )
1804 segment->height = (FT_Short)( segment->height +
1805 ( ( p->v - first_v ) >> 1 ) );
1806
1807 p = last->next;
1808 if ( p->v < last_v )
1809 segment->height = (FT_Short)( segment->height +
1810 ( ( last_v - p->v ) >> 1 ) );
1811 }
1812 }
1813 }
1814
1815 Exit:
1816 return error;
1817 }
1818
1819
1820 /* Link segments to form stems and serifs. If `width_count' and */
1821 /* `widths' are non-zero, use them to fine-tune the scoring function. */
1822
1823 FT_LOCAL_DEF( void )
1824 af_latin_hints_link_segments( AF_GlyphHints hints,
1825 FT_UInt width_count,
1826 AF_WidthRec* widths,
1827 AF_Dimension dim )
1828 {
1829 AF_AxisHints axis = &hints->axis[dim];
1830 AF_Segment segments = axis->segments;
1831 AF_Segment segment_limit = segments + axis->num_segments;
1832 FT_Pos len_threshold, len_score, dist_score, max_width;
1833 AF_Segment seg1, seg2;
1834
1835
1836 if ( width_count )
1837 max_width = widths[width_count - 1].org;
1838 else
1839 max_width = 0;
1840
1841 /* a heuristic value to set up a minimum value for overlapping */
1842 len_threshold = AF_LATIN_CONSTANT( hints->metrics, 8 );
1843 if ( len_threshold == 0 )
1844 len_threshold = 1;
1845
1846 /* a heuristic value to weight lengths */
1847 len_score = AF_LATIN_CONSTANT( hints->metrics, 6000 );
1848
1849 /* a heuristic value to weight distances (no call to */
1850 /* AF_LATIN_CONSTANT needed, since we work on multiples */
1851 /* of the stem width) */
1852 dist_score = 3000;
1853
1854 /* now compare each segment to the others */
1855 for ( seg1 = segments; seg1 < segment_limit; seg1++ )
1856 {
1857 if ( seg1->dir != axis->major_dir )
1858 continue;
1859
1860 /* search for stems having opposite directions, */
1861 /* with seg1 to the `left' of seg2 */
1862 for ( seg2 = segments; seg2 < segment_limit; seg2++ )
1863 {
1864 FT_Pos pos1 = seg1->pos;
1865 FT_Pos pos2 = seg2->pos;
1866
1867
1868 if ( seg1->dir + seg2->dir == 0 && pos2 > pos1 )
1869 {
1870 /* compute distance between the two segments */
1871 FT_Pos min = seg1->min_coord;
1872 FT_Pos max = seg1->max_coord;
1873 FT_Pos len;
1874
1875
1876 if ( min < seg2->min_coord )
1877 min = seg2->min_coord;
1878
1879 if ( max > seg2->max_coord )
1880 max = seg2->max_coord;
1881
1882 /* compute maximum coordinate difference of the two segments */
1883 /* (this is, how much they overlap) */
1884 len = max - min;
1885 if ( len >= len_threshold )
1886 {
1887 /*
1888 * The score is the sum of two demerits indicating the
1889 * `badness' of a fit, measured along the segments' main axis
1890 * and orthogonal to it, respectively.
1891 *
1892 * o The less overlapping along the main axis, the worse it
1893 * is, causing a larger demerit.
1894 *
1895 * o The nearer the orthogonal distance to a stem width, the
1896 * better it is, causing a smaller demerit. For simplicity,
1897 * however, we only increase the demerit for values that
1898 * exceed the largest stem width.
1899 */
1900
1901 FT_Pos dist = pos2 - pos1;
1902
1903 FT_Pos dist_demerit, score;
1904
1905
1906 if ( max_width )
1907 {
1908 /* distance demerits are based on multiples of `max_width'; */
1909 /* we scale by 1024 for getting more precision */
1910 FT_Pos delta = ( dist << 10 ) / max_width - ( 1 << 10 );
1911
1912
1913 if ( delta > 10000 )
1914 dist_demerit = 32000;
1915 else if ( delta > 0 )
1916 dist_demerit = delta * delta / dist_score;
1917 else
1918 dist_demerit = 0;
1919 }
1920 else
1921 dist_demerit = dist; /* default if no widths available */
1922
1923 score = dist_demerit + len_score / len;
1924
1925 /* and we search for the smallest score */
1926 if ( score < seg1->score )
1927 {
1928 seg1->score = score;
1929 seg1->link = seg2;
1930 }
1931
1932 if ( score < seg2->score )
1933 {
1934 seg2->score = score;
1935 seg2->link = seg1;
1936 }
1937 }
1938 }
1939 }
1940 }
1941
1942 /* now compute the `serif' segments, cf. explanations in `afhints.h' */
1943 for ( seg1 = segments; seg1 < segment_limit; seg1++ )
1944 {
1945 seg2 = seg1->link;
1946
1947 if ( seg2 )
1948 {
1949 if ( seg2->link != seg1 )
1950 {
1951 seg1->link = 0;
1952 seg1->serif = seg2->link;
1953 }
1954 }
1955 }
1956 }
1957
1958
1959 /* Link segments to edges, using feature analysis for selection. */
1960
1961 FT_LOCAL_DEF( FT_Error )
1962 af_latin_hints_compute_edges( AF_GlyphHints hints,
1963 AF_Dimension dim )
1964 {
1965 AF_AxisHints axis = &hints->axis[dim];
1966 FT_Error error = FT_Err_Ok;
1967 FT_Memory memory = hints->memory;
1968 AF_LatinAxis laxis = &((AF_LatinMetrics)hints->metrics)->axis[dim];
1969
1970 AF_StyleClass style_class = hints->metrics->style_class;
1971 AF_ScriptClass script_class = AF_SCRIPT_CLASSES_GET
1972 [style_class->script];
1973
1974 FT_Bool top_to_bottom_hinting = 0;
1975
1976 AF_Segment segments = axis->segments;
1977 AF_Segment segment_limit = segments + axis->num_segments;
1978 AF_Segment seg;
1979
1980 #if 0
1981 AF_Direction up_dir;
1982 #endif
1983 FT_Fixed scale;
1984 FT_Pos edge_distance_threshold;
1985 FT_Pos segment_length_threshold;
1986 FT_Pos segment_width_threshold;
1987
1988
1989 axis->num_edges = 0;
1990
1991 scale = ( dim == AF_DIMENSION_HORZ ) ? hints->x_scale
1992 : hints->y_scale;
1993
1994 #if 0
1995 up_dir = ( dim == AF_DIMENSION_HORZ ) ? AF_DIR_UP
1996 : AF_DIR_RIGHT;
1997 #endif
1998
1999 if ( dim == AF_DIMENSION_VERT )
2000 top_to_bottom_hinting = script_class->top_to_bottom_hinting;
2001
2002 /*
2003 * We ignore all segments that are less than 1 pixel in length
2004 * to avoid many problems with serif fonts. We compute the
2005 * corresponding threshold in font units.
2006 */
2007 if ( dim == AF_DIMENSION_HORZ )
2008 segment_length_threshold = FT_DivFix( 64, hints->y_scale );
2009 else
2010 segment_length_threshold = 0;
2011
2012 /*
2013 * Similarly, we ignore segments that have a width delta
2014 * larger than 0.5px (i.e., a width larger than 1px).
2015 */
2016 segment_width_threshold = FT_DivFix( 32, scale );
2017
2018 /*********************************************************************/
2019 /* */
2020 /* We begin by generating a sorted table of edges for the current */
2021 /* direction. To do so, we simply scan each segment and try to find */
2022 /* an edge in our table that corresponds to its position. */
2023 /* */
2024 /* If no edge is found, we create and insert a new edge in the */
2025 /* sorted table. Otherwise, we simply add the segment to the edge's */
2026 /* list which gets processed in the second step to compute the */
2027 /* edge's properties. */
2028 /* */
2029 /* Note that the table of edges is sorted along the segment/edge */
2030 /* position. */
2031 /* */
2032 /*********************************************************************/
2033
2034 /* assure that edge distance threshold is at most 0.25px */
2035 edge_distance_threshold = FT_MulFix( laxis->edge_distance_threshold,
2036 scale );
2037 if ( edge_distance_threshold > 64 / 4 )
2038 edge_distance_threshold = 64 / 4;
2039
2040 edge_distance_threshold = FT_DivFix( edge_distance_threshold,
2041 scale );
2042
2043 for ( seg = segments; seg < segment_limit; seg++ )
2044 {
2045 AF_Edge found = NULL;
2046 FT_Int ee;
2047
2048
2049 /* ignore too short segments, too wide ones, and, in this loop, */
2050 /* one-point segments without a direction */
2051 if ( seg->height < segment_length_threshold ||
2052 seg->delta > segment_width_threshold ||
2053 seg->dir == AF_DIR_NONE )
2054 continue;
2055
2056 /* A special case for serif edges: If they are smaller than */
2057 /* 1.5 pixels we ignore them. */
2058 if ( seg->serif &&
2059 2 * seg->height < 3 * segment_length_threshold )
2060 continue;
2061
2062 /* look for an edge corresponding to the segment */
2063 for ( ee = 0; ee < axis->num_edges; ee++ )
2064 {
2065 AF_Edge edge = axis->edges + ee;
2066 FT_Pos dist;
2067
2068
2069 dist = seg->pos - edge->fpos;
2070 if ( dist < 0 )
2071 dist = -dist;
2072
2073 if ( dist < edge_distance_threshold && edge->dir == seg->dir )
2074 {
2075 found = edge;
2076 break;
2077 }
2078 }
2079
2080 if ( !found )
2081 {
2082 AF_Edge edge;
2083
2084
2085 /* insert a new edge in the list and */
2086 /* sort according to the position */
2087 error = af_axis_hints_new_edge( axis, seg->pos,
2088 (AF_Direction)seg->dir,
2089 top_to_bottom_hinting,
2090 memory, &edge );
2091 if ( error )
2092 goto Exit;
2093
2094 /* add the segment to the new edge's list */
2095 FT_ZERO( edge );
2096
2097 edge->first = seg;
2098 edge->last = seg;
2099 edge->dir = seg->dir;
2100 edge->fpos = seg->pos;
2101 edge->opos = FT_MulFix( seg->pos, scale );
2102 edge->pos = edge->opos;
2103 seg->edge_next = seg;
2104 }
2105 else
2106 {
2107 /* if an edge was found, simply add the segment to the edge's */
2108 /* list */
2109 seg->edge_next = found->first;
2110 found->last->edge_next = seg;
2111 found->last = seg;
2112 }
2113 }
2114
2115 /* we loop again over all segments to catch one-point segments */
2116 /* without a direction: if possible, link them to existing edges */
2117 for ( seg = segments; seg < segment_limit; seg++ )
2118 {
2119 AF_Edge found = NULL;
2120 FT_Int ee;
2121
2122
2123 if ( seg->dir != AF_DIR_NONE )
2124 continue;
2125
2126 /* look for an edge corresponding to the segment */
2127 for ( ee = 0; ee < axis->num_edges; ee++ )
2128 {
2129 AF_Edge edge = axis->edges + ee;
2130 FT_Pos dist;
2131
2132
2133 dist = seg->pos - edge->fpos;
2134 if ( dist < 0 )
2135 dist = -dist;
2136
2137 if ( dist < edge_distance_threshold )
2138 {
2139 found = edge;
2140 break;
2141 }
2142 }
2143
2144 /* one-point segments without a match are ignored */
2145 if ( found )
2146 {
2147 seg->edge_next = found->first;
2148 found->last->edge_next = seg;
2149 found->last = seg;
2150 }
2151 }
2152
2153
2154 /******************************************************************/
2155 /* */
2156 /* Good, we now compute each edge's properties according to the */
2157 /* segments found on its position. Basically, these are */
2158 /* */
2159 /* - the edge's main direction */
2160 /* - stem edge, serif edge or both (which defaults to stem then) */
2161 /* - rounded edge, straight or both (which defaults to straight) */
2162 /* - link for edge */
2163 /* */
2164 /******************************************************************/
2165
2166 /* first of all, set the `edge' field in each segment -- this is */
2167 /* required in order to compute edge links */
2168
2169 /*
2170 * Note that removing this loop and setting the `edge' field of each
2171 * segment directly in the code above slows down execution speed for
2172 * some reasons on platforms like the Sun.
2173 */
2174 {
2175 AF_Edge edges = axis->edges;
2176 AF_Edge edge_limit = edges + axis->num_edges;
2177 AF_Edge edge;
2178
2179
2180 for ( edge = edges; edge < edge_limit; edge++ )
2181 {
2182 seg = edge->first;
2183 if ( seg )
2184 do
2185 {
2186 seg->edge = edge;
2187 seg = seg->edge_next;
2188
2189 } while ( seg != edge->first );
2190 }
2191
2192 /* now compute each edge properties */
2193 for ( edge = edges; edge < edge_limit; edge++ )
2194 {
2195 FT_Int is_round = 0; /* does it contain round segments? */
2196 FT_Int is_straight = 0; /* does it contain straight segments? */
2197 #if 0
2198 FT_Pos ups = 0; /* number of upwards segments */
2199 FT_Pos downs = 0; /* number of downwards segments */
2200 #endif
2201
2202
2203 seg = edge->first;
2204
2205 do
2206 {
2207 FT_Bool is_serif;
2208
2209
2210 /* check for roundness of segment */
2211 if ( seg->flags & AF_EDGE_ROUND )
2212 is_round++;
2213 else
2214 is_straight++;
2215
2216 #if 0
2217 /* check for segment direction */
2218 if ( seg->dir == up_dir )
2219 ups += seg->max_coord - seg->min_coord;
2220 else
2221 downs += seg->max_coord - seg->min_coord;
2222 #endif
2223
2224 /* check for links -- if seg->serif is set, then seg->link must */
2225 /* be ignored */
2226 is_serif = (FT_Bool)( seg->serif &&
2227 seg->serif->edge &&
2228 seg->serif->edge != edge );
2229
2230 if ( ( seg->link && seg->link->edge != NULL ) || is_serif )
2231 {
2232 AF_Edge edge2;
2233 AF_Segment seg2;
2234
2235
2236 edge2 = edge->link;
2237 seg2 = seg->link;
2238
2239 if ( is_serif )
2240 {
2241 seg2 = seg->serif;
2242 edge2 = edge->serif;
2243 }
2244
2245 if ( edge2 )
2246 {
2247 FT_Pos edge_delta;
2248 FT_Pos seg_delta;
2249
2250
2251 edge_delta = edge->fpos - edge2->fpos;
2252 if ( edge_delta < 0 )
2253 edge_delta = -edge_delta;
2254
2255 seg_delta = seg->pos - seg2->pos;
2256 if ( seg_delta < 0 )
2257 seg_delta = -seg_delta;
2258
2259 if ( seg_delta < edge_delta )
2260 edge2 = seg2->edge;
2261 }
2262 else
2263 edge2 = seg2->edge;
2264
2265 if ( is_serif )
2266 {
2267 edge->serif = edge2;
2268 edge2->flags |= AF_EDGE_SERIF;
2269 }
2270 else
2271 edge->link = edge2;
2272 }
2273
2274 seg = seg->edge_next;
2275
2276 } while ( seg != edge->first );
2277
2278 /* set the round/straight flags */
2279 edge->flags = AF_EDGE_NORMAL;
2280
2281 if ( is_round > 0 && is_round >= is_straight )
2282 edge->flags |= AF_EDGE_ROUND;
2283
2284 #if 0
2285 /* set the edge's main direction */
2286 edge->dir = AF_DIR_NONE;
2287
2288 if ( ups > downs )
2289 edge->dir = (FT_Char)up_dir;
2290
2291 else if ( ups < downs )
2292 edge->dir = (FT_Char)-up_dir;
2293
2294 else if ( ups == downs )
2295 edge->dir = 0; /* both up and down! */
2296 #endif
2297
2298 /* get rid of serifs if link is set */
2299 /* XXX: This gets rid of many unpleasant artefacts! */
2300 /* Example: the `c' in cour.pfa at size 13 */
2301
2302 if ( edge->serif && edge->link )
2303 edge->serif = NULL;
2304 }
2305 }
2306
2307 Exit:
2308 return error;
2309 }
2310
2311
2312 /* Detect segments and edges for given dimension. */
2313
2314 FT_LOCAL_DEF( FT_Error )
2315 af_latin_hints_detect_features( AF_GlyphHints hints,
2316 FT_UInt width_count,
2317 AF_WidthRec* widths,
2318 AF_Dimension dim )
2319 {
2320 FT_Error error;
2321
2322
2323 error = af_latin_hints_compute_segments( hints, dim );
2324 if ( !error )
2325 {
2326 af_latin_hints_link_segments( hints, width_count, widths, dim );
2327
2328 error = af_latin_hints_compute_edges( hints, dim );
2329 }
2330
2331 return error;
2332 }
2333
2334
2335 /* Compute all edges which lie within blue zones. */
2336
2337 static void
2338 af_latin_hints_compute_blue_edges( AF_GlyphHints hints,
2339 AF_LatinMetrics metrics )
2340 {
2341 AF_AxisHints axis = &hints->axis[AF_DIMENSION_VERT];
2342 AF_Edge edge = axis->edges;
2343 AF_Edge edge_limit = edge + axis->num_edges;
2344 AF_LatinAxis latin = &metrics->axis[AF_DIMENSION_VERT];
2345 FT_Fixed scale = latin->scale;
2346
2347
2348 /* compute which blue zones are active, i.e. have their scaled */
2349 /* size < 3/4 pixels */
2350
2351 /* for each horizontal edge search the blue zone which is closest */
2352 for ( ; edge < edge_limit; edge++ )
2353 {
2354 FT_UInt bb;
2355 AF_Width best_blue = NULL;
2356 FT_Bool best_blue_is_neutral = 0;
2357 FT_Pos best_dist; /* initial threshold */
2358
2359
2360 /* compute the initial threshold as a fraction of the EM size */
2361 /* (the value 40 is heuristic) */
2362 best_dist = FT_MulFix( metrics->units_per_em / 40, scale );
2363
2364 /* assure a minimum distance of 0.5px */
2365 if ( best_dist > 64 / 2 )
2366 best_dist = 64 / 2;
2367
2368 for ( bb = 0; bb < latin->blue_count; bb++ )
2369 {
2370 AF_LatinBlue blue = latin->blues + bb;
2371 FT_Bool is_top_blue, is_neutral_blue, is_major_dir;
2372
2373
2374 /* skip inactive blue zones (i.e., those that are too large) */
2375 if ( !( blue->flags & AF_LATIN_BLUE_ACTIVE ) )
2376 continue;
2377
2378 /* if it is a top zone, check for right edges (against the major */
2379 /* direction); if it is a bottom zone, check for left edges (in */
2380 /* the major direction) -- this assumes the TrueType convention */
2381 /* for the orientation of contours */
2382 is_top_blue =
2383 (FT_Byte)( ( blue->flags & ( AF_LATIN_BLUE_TOP |
2384 AF_LATIN_BLUE_SUB_TOP ) ) != 0 );
2385 is_neutral_blue =
2386 (FT_Byte)( ( blue->flags & AF_LATIN_BLUE_NEUTRAL ) != 0);
2387 is_major_dir =
2388 FT_BOOL( edge->dir == axis->major_dir );
2389
2390 /* neutral blue zones are handled for both directions */
2391 if ( is_top_blue ^ is_major_dir || is_neutral_blue )
2392 {
2393 FT_Pos dist;
2394
2395
2396 /* first of all, compare it to the reference position */
2397 dist = edge->fpos - blue->ref.org;
2398 if ( dist < 0 )
2399 dist = -dist;
2400
2401 dist = FT_MulFix( dist, scale );
2402 if ( dist < best_dist )
2403 {
2404 best_dist = dist;
2405 best_blue = &blue->ref;
2406 best_blue_is_neutral = is_neutral_blue;
2407 }
2408
2409 /* now compare it to the overshoot position and check whether */
2410 /* the edge is rounded, and whether the edge is over the */
2411 /* reference position of a top zone, or under the reference */
2412 /* position of a bottom zone (provided we don't have a */
2413 /* neutral blue zone) */
2414 if ( edge->flags & AF_EDGE_ROUND &&
2415 dist != 0 &&
2416 !is_neutral_blue )
2417 {
2418 FT_Bool is_under_ref = FT_BOOL( edge->fpos < blue->ref.org );
2419
2420
2421 if ( is_top_blue ^ is_under_ref )
2422 {
2423 dist = edge->fpos - blue->shoot.org;
2424 if ( dist < 0 )
2425 dist = -dist;
2426
2427 dist = FT_MulFix( dist, scale );
2428 if ( dist < best_dist )
2429 {
2430 best_dist = dist;
2431 best_blue = &blue->shoot;
2432 best_blue_is_neutral = is_neutral_blue;
2433 }
2434 }
2435 }
2436 }
2437 }
2438
2439 if ( best_blue )
2440 {
2441 edge->blue_edge = best_blue;
2442 if ( best_blue_is_neutral )
2443 edge->flags |= AF_EDGE_NEUTRAL;
2444 }
2445 }
2446 }
2447
2448
2449 /* Initalize hinting engine. */
2450
2451 static FT_Error
2452 af_latin_hints_init( AF_GlyphHints hints,
2453 AF_LatinMetrics metrics )
2454 {
2455 FT_Render_Mode mode;
2456 FT_UInt32 scaler_flags, other_flags;
2457 FT_Face face = metrics->root.scaler.face;
2458
2459
2460 af_glyph_hints_rescale( hints, (AF_StyleMetrics)metrics );
2461
2462 /*
2463 * correct x_scale and y_scale if needed, since they may have
2464 * been modified by `af_latin_metrics_scale_dim' above
2465 */
2466 hints->x_scale = metrics->axis[AF_DIMENSION_HORZ].scale;
2467 hints->x_delta = metrics->axis[AF_DIMENSION_HORZ].delta;
2468 hints->y_scale = metrics->axis[AF_DIMENSION_VERT].scale;
2469 hints->y_delta = metrics->axis[AF_DIMENSION_VERT].delta;
2470
2471 /* compute flags depending on render mode, etc. */
2472 mode = metrics->root.scaler.render_mode;
2473
2474 #if 0 /* #ifdef AF_CONFIG_OPTION_USE_WARPER */
2475 if ( mode == FT_RENDER_MODE_LCD || mode == FT_RENDER_MODE_LCD_V )
2476 metrics->root.scaler.render_mode = mode = FT_RENDER_MODE_NORMAL;
2477 #endif
2478
2479 scaler_flags = hints->scaler_flags;
2480 other_flags = 0;
2481
2482 /*
2483 * We snap the width of vertical stems for the monochrome and
2484 * horizontal LCD rendering targets only.
2485 */
2486 if ( mode == FT_RENDER_MODE_MONO || mode == FT_RENDER_MODE_LCD )
2487 other_flags |= AF_LATIN_HINTS_HORZ_SNAP;
2488
2489 /*
2490 * We snap the width of horizontal stems for the monochrome and
2491 * vertical LCD rendering targets only.
2492 */
2493 if ( mode == FT_RENDER_MODE_MONO || mode == FT_RENDER_MODE_LCD_V )
2494 other_flags |= AF_LATIN_HINTS_VERT_SNAP;
2495
2496 /*
2497 * We adjust stems to full pixels only if we don't use the `light' mode.
2498 */
2499 if ( mode != FT_RENDER_MODE_LIGHT )
2500 other_flags |= AF_LATIN_HINTS_STEM_ADJUST;
2501
2502 if ( mode == FT_RENDER_MODE_MONO )
2503 other_flags |= AF_LATIN_HINTS_MONO;
2504
2505 /*
2506 * In `light' hinting mode we disable horizontal hinting completely.
2507 * We also do it if the face is italic.
2508 *
2509 * However, if warping is enabled (which only works in `light' hinting
2510 * mode), advance widths get adjusted, too.
2511 */
2512 if ( mode == FT_RENDER_MODE_LIGHT ||
2513 ( face->style_flags & FT_STYLE_FLAG_ITALIC ) != 0 )
2514 scaler_flags |= AF_SCALER_FLAG_NO_HORIZONTAL;
2515
2516 #ifdef AF_CONFIG_OPTION_USE_WARPER
2517 /* get (global) warper flag */
2518 if ( !metrics->root.globals->module->warping )
2519 scaler_flags |= AF_SCALER_FLAG_NO_WARPER;
2520 #endif
2521
2522 hints->scaler_flags = scaler_flags;
2523 hints->other_flags = other_flags;
2524
2525 return FT_Err_Ok;
2526 }
2527
2528
2529 /*************************************************************************/
2530 /*************************************************************************/
2531 /***** *****/
2532 /***** L A T I N G L Y P H G R I D - F I T T I N G *****/
2533 /***** *****/
2534 /*************************************************************************/
2535 /*************************************************************************/
2536
2537 /* Snap a given width in scaled coordinates to one of the */
2538 /* current standard widths. */
2539
2540 static FT_Pos
2541 af_latin_snap_width( AF_Width widths,
2542 FT_UInt count,
2543 FT_Pos width )
2544 {
2545 FT_UInt n;
2546 FT_Pos best = 64 + 32 + 2;
2547 FT_Pos reference = width;
2548 FT_Pos scaled;
2549
2550
2551 for ( n = 0; n < count; n++ )
2552 {
2553 FT_Pos w;
2554 FT_Pos dist;
2555
2556
2557 w = widths[n].cur;
2558 dist = width - w;
2559 if ( dist < 0 )
2560 dist = -dist;
2561 if ( dist < best )
2562 {
2563 best = dist;
2564 reference = w;
2565 }
2566 }
2567
2568 scaled = FT_PIX_ROUND( reference );
2569
2570 if ( width >= reference )
2571 {
2572 if ( width < scaled + 48 )
2573 width = reference;
2574 }
2575 else
2576 {
2577 if ( width > scaled - 48 )
2578 width = reference;
2579 }
2580
2581 return width;
2582 }
2583
2584
2585 /* Compute the snapped width of a given stem, ignoring very thin ones. */
2586 /* There is a lot of voodoo in this function; changing the hard-coded */
2587 /* parameters influence the whole hinting process. */
2588
2589 static FT_Pos
2590 af_latin_compute_stem_width( AF_GlyphHints hints,
2591 AF_Dimension dim,
2592 FT_Pos width,
2593 FT_Pos base_delta,
2594 FT_UInt base_flags,
2595 FT_UInt stem_flags )
2596 {
2597 AF_LatinMetrics metrics = (AF_LatinMetrics)hints->metrics;
2598 AF_LatinAxis axis = &metrics->axis[dim];
2599 FT_Pos dist = width;
2600 FT_Int sign = 0;
2601 FT_Int vertical = ( dim == AF_DIMENSION_VERT );
2602
2603
2604 if ( !AF_LATIN_HINTS_DO_STEM_ADJUST( hints ) ||
2605 axis->extra_light )
2606 return width;
2607
2608 if ( dist < 0 )
2609 {
2610 dist = -width;
2611 sign = 1;
2612 }
2613
2614 if ( ( vertical && !AF_LATIN_HINTS_DO_VERT_SNAP( hints ) ) ||
2615 ( !vertical && !AF_LATIN_HINTS_DO_HORZ_SNAP( hints ) ) )
2616 {
2617 /* smooth hinting process: very lightly quantize the stem width */
2618
2619 /* leave the widths of serifs alone */
2620 if ( ( stem_flags & AF_EDGE_SERIF ) &&
2621 vertical &&
2622 ( dist < 3 * 64 ) )
2623 goto Done_Width;
2624
2625 else if ( base_flags & AF_EDGE_ROUND )
2626 {
2627 if ( dist < 80 )
2628 dist = 64;
2629 }
2630 else if ( dist < 56 )
2631 dist = 56;
2632
2633 if ( axis->width_count > 0 )
2634 {
2635 FT_Pos delta;
2636
2637
2638 /* compare to standard width */
2639 delta = dist - axis->widths[0].cur;
2640
2641 if ( delta < 0 )
2642 delta = -delta;
2643
2644 if ( delta < 40 )
2645 {
2646 dist = axis->widths[0].cur;
2647 if ( dist < 48 )
2648 dist = 48;
2649
2650 goto Done_Width;
2651 }
2652
2653 if ( dist < 3 * 64 )
2654 {
2655 delta = dist & 63;
2656 dist &= -64;
2657
2658 if ( delta < 10 )
2659 dist += delta;
2660
2661 else if ( delta < 32 )
2662 dist += 10;
2663
2664 else if ( delta < 54 )
2665 dist += 54;
2666
2667 else
2668 dist += delta;
2669 }
2670 else
2671 {
2672 /* A stem's end position depends on two values: the start */
2673 /* position and the stem length. The former gets usually */
2674 /* rounded to the grid, while the latter gets rounded also if it */
2675 /* exceeds a certain length (see below in this function). This */
2676 /* `double rounding' can lead to a great difference to the */
2677 /* original, unhinted position; this normally doesn't matter for */
2678 /* large PPEM values, but for small sizes it can easily make */
2679 /* outlines collide. For this reason, we adjust the stem length */
2680 /* by a small amount depending on the PPEM value in case the */
2681 /* former and latter rounding both point into the same */
2682 /* direction. */
2683
2684 FT_Pos bdelta = 0;
2685
2686
2687 if ( ( ( width > 0 ) && ( base_delta > 0 ) ) ||
2688 ( ( width < 0 ) && ( base_delta < 0 ) ) )
2689 {
2690 FT_UInt ppem = metrics->root.scaler.face->size->metrics.x_ppem;
2691
2692
2693 if ( ppem < 10 )
2694 bdelta = base_delta;
2695 else if ( ppem < 30 )
2696 bdelta = ( base_delta * (FT_Pos)( 30 - ppem ) ) / 20;
2697
2698 if ( bdelta < 0 )
2699 bdelta = -bdelta;
2700 }
2701
2702 dist = ( dist - bdelta + 32 ) & ~63;
2703 }
2704 }
2705 }
2706 else
2707 {
2708 /* strong hinting process: snap the stem width to integer pixels */
2709
2710 FT_Pos org_dist = dist;
2711
2712
2713 dist = af_latin_snap_width( axis->widths, axis->width_count, dist );
2714
2715 if ( vertical )
2716 {
2717 /* in the case of vertical hinting, always round */
2718 /* the stem heights to integer pixels */
2719
2720 if ( dist >= 64 )
2721 dist = ( dist + 16 ) & ~63;
2722 else
2723 dist = 64;
2724 }
2725 else
2726 {
2727 if ( AF_LATIN_HINTS_DO_MONO( hints ) )
2728 {
2729 /* monochrome horizontal hinting: snap widths to integer pixels */
2730 /* with a different threshold */
2731
2732 if ( dist < 64 )
2733 dist = 64;
2734 else
2735 dist = ( dist + 32 ) & ~63;
2736 }
2737 else
2738 {
2739 /* for horizontal anti-aliased hinting, we adopt a more subtle */
2740 /* approach: we strengthen small stems, round stems whose size */
2741 /* is between 1 and 2 pixels to an integer, otherwise nothing */
2742
2743 if ( dist < 48 )
2744 dist = ( dist + 64 ) >> 1;
2745
2746 else if ( dist < 128 )
2747 {
2748 /* We only round to an integer width if the corresponding */
2749 /* distortion is less than 1/4 pixel. Otherwise this */
2750 /* makes everything worse since the diagonals, which are */
2751 /* not hinted, appear a lot bolder or thinner than the */
2752 /* vertical stems. */
2753
2754 FT_Pos delta;
2755
2756
2757 dist = ( dist + 22 ) & ~63;
2758 delta = dist - org_dist;
2759 if ( delta < 0 )
2760 delta = -delta;
2761
2762 if ( delta >= 16 )
2763 {
2764 dist = org_dist;
2765 if ( dist < 48 )
2766 dist = ( dist + 64 ) >> 1;
2767 }
2768 }
2769 else
2770 /* round otherwise to prevent color fringes in LCD mode */
2771 dist = ( dist + 32 ) & ~63;
2772 }
2773 }
2774 }
2775
2776 Done_Width:
2777 if ( sign )
2778 dist = -dist;
2779
2780 return dist;
2781 }
2782
2783
2784 /* Align one stem edge relative to the previous stem edge. */
2785
2786 static void
2787 af_latin_align_linked_edge( AF_GlyphHints hints,
2788 AF_Dimension dim,
2789 AF_Edge base_edge,
2790 AF_Edge stem_edge )
2791 {
2792 FT_Pos dist, base_delta;
2793 FT_Pos fitted_width;
2794
2795
2796 dist = stem_edge->opos - base_edge->opos;
2797 base_delta = base_edge->pos - base_edge->opos;
2798
2799 fitted_width = af_latin_compute_stem_width( hints, dim,
2800 dist, base_delta,
2801 base_edge->flags,
2802 stem_edge->flags );
2803
2804
2805 stem_edge->pos = base_edge->pos + fitted_width;
2806
2807 FT_TRACE5(( " LINK: edge %d (opos=%.2f) linked to %.2f,"
2808 " dist was %.2f, now %.2f\n",
2809 stem_edge - hints->axis[dim].edges, stem_edge->opos / 64.0,
2810 stem_edge->pos / 64.0, dist / 64.0, fitted_width / 64.0 ));
2811 }
2812
2813
2814 /* Shift the coordinates of the `serif' edge by the same amount */
2815 /* as the corresponding `base' edge has been moved already. */
2816
2817 static void
2818 af_latin_align_serif_edge( AF_GlyphHints hints,
2819 AF_Edge base,
2820 AF_Edge serif )
2821 {
2822 FT_UNUSED( hints );
2823
2824 serif->pos = base->pos + ( serif->opos - base->opos );
2825 }
2826
2827
2828 /*************************************************************************/
2829 /*************************************************************************/
2830 /*************************************************************************/
2831 /**** ****/
2832 /**** E D G E H I N T I N G ****/
2833 /**** ****/
2834 /*************************************************************************/
2835 /*************************************************************************/
2836 /*************************************************************************/
2837
2838
2839 /* The main grid-fitting routine. */
2840
2841 static void
2842 af_latin_hint_edges( AF_GlyphHints hints,
2843 AF_Dimension dim )
2844 {
2845 AF_AxisHints axis = &hints->axis[dim];
2846 AF_Edge edges = axis->edges;
2847 AF_Edge edge_limit = edges + axis->num_edges;
2848 FT_PtrDist n_edges;
2849 AF_Edge edge;
2850 AF_Edge anchor = NULL;
2851 FT_Int has_serifs = 0;
2852
2853 AF_StyleClass style_class = hints->metrics->style_class;
2854 AF_ScriptClass script_class = AF_SCRIPT_CLASSES_GET
2855 [style_class->script];
2856
2857 FT_Bool top_to_bottom_hinting = 0;
2858
2859 #ifdef FT_DEBUG_LEVEL_TRACE
2860 FT_UInt num_actions = 0;
2861 #endif
2862
2863
2864 FT_TRACE5(( "latin %s edge hinting (style `%s')\n",
2865 dim == AF_DIMENSION_VERT ? "horizontal" : "vertical",
2866 af_style_names[hints->metrics->style_class->style] ));
2867
2868 if ( dim == AF_DIMENSION_VERT )
2869 top_to_bottom_hinting = script_class->top_to_bottom_hinting;
2870
2871 /* we begin by aligning all stems relative to the blue zone */
2872 /* if needed -- that's only for horizontal edges */
2873
2874 if ( dim == AF_DIMENSION_VERT && AF_HINTS_DO_BLUES( hints ) )
2875 {
2876 for ( edge = edges; edge < edge_limit; edge++ )
2877 {
2878 AF_Width blue;
2879 AF_Edge edge1, edge2; /* these edges form the stem to check */
2880
2881
2882 if ( edge->flags & AF_EDGE_DONE )
2883 continue;
2884
2885 edge1 = NULL;
2886 edge2 = edge->link;
2887
2888 /*
2889 * If a stem contains both a neutral and a non-neutral blue zone,
2890 * skip the neutral one. Otherwise, outlines with different
2891 * directions might be incorrectly aligned at the same vertical
2892 * position.
2893 *
2894 * If we have two neutral blue zones, skip one of them.
2895 *
2896 */
2897 if ( edge->blue_edge && edge2 && edge2->blue_edge )
2898 {
2899 FT_Byte neutral = edge->flags & AF_EDGE_NEUTRAL;
2900 FT_Byte neutral2 = edge2->flags & AF_EDGE_NEUTRAL;
2901
2902
2903 if ( neutral2 )
2904 {
2905 edge2->blue_edge = NULL;
2906 edge2->flags &= ~AF_EDGE_NEUTRAL;
2907 }
2908 else if ( neutral )
2909 {
2910 edge->blue_edge = NULL;
2911 edge->flags &= ~AF_EDGE_NEUTRAL;
2912 }
2913 }
2914
2915 blue = edge->blue_edge;
2916 if ( blue )
2917 edge1 = edge;
2918
2919 /* flip edges if the other edge is aligned to a blue zone */
2920 else if ( edge2 && edge2->blue_edge )
2921 {
2922 blue = edge2->blue_edge;
2923 edge1 = edge2;
2924 edge2 = edge;
2925 }
2926
2927 if ( !edge1 )
2928 continue;
2929
2930 #ifdef FT_DEBUG_LEVEL_TRACE
2931 if ( !anchor )
2932 FT_TRACE5(( " BLUE_ANCHOR: edge %d (opos=%.2f) snapped to %.2f,"
2933 " was %.2f (anchor=edge %d)\n",
2934 edge1 - edges, edge1->opos / 64.0, blue->fit / 64.0,
2935 edge1->pos / 64.0, edge - edges ));
2936 else
2937 FT_TRACE5(( " BLUE: edge %d (opos=%.2f) snapped to %.2f,"
2938 " was %.2f\n",
2939 edge1 - edges, edge1->opos / 64.0, blue->fit / 64.0,
2940 edge1->pos / 64.0 ));
2941
2942 num_actions++;
2943 #endif
2944
2945 edge1->pos = blue->fit;
2946 edge1->flags |= AF_EDGE_DONE;
2947
2948 if ( edge2 && !edge2->blue_edge )
2949 {
2950 af_latin_align_linked_edge( hints, dim, edge1, edge2 );
2951 edge2->flags |= AF_EDGE_DONE;
2952
2953 #ifdef FT_DEBUG_LEVEL_TRACE
2954 num_actions++;
2955 #endif
2956 }
2957
2958 if ( !anchor )
2959 anchor = edge;
2960 }
2961 }
2962
2963 /* now we align all other stem edges, trying to maintain the */
2964 /* relative order of stems in the glyph */
2965 for ( edge = edges; edge < edge_limit; edge++ )
2966 {
2967 AF_Edge edge2;
2968
2969
2970 if ( edge->flags & AF_EDGE_DONE )
2971 continue;
2972
2973 /* skip all non-stem edges */
2974 edge2 = edge->link;
2975 if ( !edge2 )
2976 {
2977 has_serifs++;
2978 continue;
2979 }
2980
2981 /* now align the stem */
2982
2983 /* this should not happen, but it's better to be safe */
2984 if ( edge2->blue_edge )
2985 {
2986 FT_TRACE5(( " ASSERTION FAILED for edge %d\n", edge2 - edges ));
2987
2988 af_latin_align_linked_edge( hints, dim, edge2, edge );
2989 edge->flags |= AF_EDGE_DONE;
2990
2991 #ifdef FT_DEBUG_LEVEL_TRACE
2992 num_actions++;
2993 #endif
2994 continue;
2995 }
2996
2997 if ( !anchor )
2998 {
2999 /* if we reach this if clause, no stem has been aligned yet */
3000
3001 FT_Pos org_len, org_center, cur_len;
3002 FT_Pos cur_pos1, error1, error2, u_off, d_off;
3003
3004
3005 org_len = edge2->opos - edge->opos;
3006 cur_len = af_latin_compute_stem_width( hints, dim,
3007 org_len, 0,
3008 edge->flags,
3009 edge2->flags );
3010
3011 /* some voodoo to specially round edges for small stem widths; */
3012 /* the idea is to align the center of a stem, then shifting */
3013 /* the stem edges to suitable positions */
3014 if ( cur_len <= 64 )
3015 {
3016 /* width <= 1px */
3017 u_off = 32;
3018 d_off = 32;
3019 }
3020 else
3021 {
3022 /* 1px < width < 1.5px */
3023 u_off = 38;
3024 d_off = 26;
3025 }
3026
3027 if ( cur_len < 96 )
3028 {
3029 org_center = edge->opos + ( org_len >> 1 );
3030 cur_pos1 = FT_PIX_ROUND( org_center );
3031
3032 error1 = org_center - ( cur_pos1 - u_off );
3033 if ( error1 < 0 )
3034 error1 = -error1;
3035
3036 error2 = org_center - ( cur_pos1 + d_off );
3037 if ( error2 < 0 )
3038 error2 = -error2;
3039
3040 if ( error1 < error2 )
3041 cur_pos1 -= u_off;
3042 else
3043 cur_pos1 += d_off;
3044
3045 edge->pos = cur_pos1 - cur_len / 2;
3046 edge2->pos = edge->pos + cur_len;
3047 }
3048 else
3049 edge->pos = FT_PIX_ROUND( edge->opos );
3050
3051 anchor = edge;
3052 edge->flags |= AF_EDGE_DONE;
3053
3054 FT_TRACE5(( " ANCHOR: edge %d (opos=%.2f) and %d (opos=%.2f)"
3055 " snapped to %.2f and %.2f\n",
3056 edge - edges, edge->opos / 64.0,
3057 edge2 - edges, edge2->opos / 64.0,
3058 edge->pos / 64.0, edge2->pos / 64.0 ));
3059
3060 af_latin_align_linked_edge( hints, dim, edge, edge2 );
3061
3062 #ifdef FT_DEBUG_LEVEL_TRACE
3063 num_actions += 2;
3064 #endif
3065 }
3066 else
3067 {
3068 FT_Pos org_pos, org_len, org_center, cur_len;
3069 FT_Pos cur_pos1, cur_pos2, delta1, delta2;
3070
3071
3072 org_pos = anchor->pos + ( edge->opos - anchor->opos );
3073 org_len = edge2->opos - edge->opos;
3074 org_center = org_pos + ( org_len >> 1 );
3075
3076 cur_len = af_latin_compute_stem_width( hints, dim,
3077 org_len, 0,
3078 edge->flags,
3079 edge2->flags );
3080
3081 if ( edge2->flags & AF_EDGE_DONE )
3082 {
3083 FT_TRACE5(( " ADJUST: edge %d (pos=%.2f) moved to %.2f\n",
3084 edge - edges, edge->pos / 64.0,
3085 ( edge2->pos - cur_len ) / 64.0 ));
3086
3087 edge->pos = edge2->pos - cur_len;
3088 }
3089
3090 else if ( cur_len < 96 )
3091 {
3092 FT_Pos u_off, d_off;
3093
3094
3095 cur_pos1 = FT_PIX_ROUND( org_center );
3096
3097 if ( cur_len <= 64 )
3098 {
3099 u_off = 32;
3100 d_off = 32;
3101 }
3102 else
3103 {
3104 u_off = 38;
3105 d_off = 26;
3106 }
3107
3108 delta1 = org_center - ( cur_pos1 - u_off );
3109 if ( delta1 < 0 )
3110 delta1 = -delta1;
3111
3112 delta2 = org_center - ( cur_pos1 + d_off );
3113 if ( delta2 < 0 )
3114 delta2 = -delta2;
3115
3116 if ( delta1 < delta2 )
3117 cur_pos1 -= u_off;
3118 else
3119 cur_pos1 += d_off;
3120
3121 edge->pos = cur_pos1 - cur_len / 2;
3122 edge2->pos = cur_pos1 + cur_len / 2;
3123
3124 FT_TRACE5(( " STEM: edge %d (opos=%.2f) linked to %d (opos=%.2f)"
3125 " snapped to %.2f and %.2f\n",
3126 edge - edges, edge->opos / 64.0,
3127 edge2 - edges, edge2->opos / 64.0,
3128 edge->pos / 64.0, edge2->pos / 64.0 ));
3129 }
3130
3131 else
3132 {
3133 org_pos = anchor->pos + ( edge->opos - anchor->opos );
3134 org_len = edge2->opos - edge->opos;
3135 org_center = org_pos + ( org_len >> 1 );
3136
3137 cur_len = af_latin_compute_stem_width( hints, dim,
3138 org_len, 0,
3139 edge->flags,
3140 edge2->flags );
3141
3142 cur_pos1 = FT_PIX_ROUND( org_pos );
3143 delta1 = cur_pos1 + ( cur_len >> 1 ) - org_center;
3144 if ( delta1 < 0 )
3145 delta1 = -delta1;
3146
3147 cur_pos2 = FT_PIX_ROUND( org_pos + org_len ) - cur_len;
3148 delta2 = cur_pos2 + ( cur_len >> 1 ) - org_center;
3149 if ( delta2 < 0 )
3150 delta2 = -delta2;
3151
3152 edge->pos = ( delta1 < delta2 ) ? cur_pos1 : cur_pos2;
3153 edge2->pos = edge->pos + cur_len;
3154
3155 FT_TRACE5(( " STEM: edge %d (opos=%.2f) linked to %d (opos=%.2f)"
3156 " snapped to %.2f and %.2f\n",
3157 edge - edges, edge->opos / 64.0,
3158 edge2 - edges, edge2->opos / 64.0,
3159 edge->pos / 64.0, edge2->pos / 64.0 ));
3160 }
3161
3162 #ifdef FT_DEBUG_LEVEL_TRACE
3163 num_actions++;
3164 #endif
3165
3166 edge->flags |= AF_EDGE_DONE;
3167 edge2->flags |= AF_EDGE_DONE;
3168
3169 if ( edge > edges &&
3170 ( top_to_bottom_hinting ? ( edge->pos > edge[-1].pos )
3171 : ( edge->pos < edge[-1].pos ) ) )
3172 {
3173 /* don't move if stem would (almost) disappear otherwise; */
3174 /* the ad-hoc value 16 corresponds to 1/4px */
3175 if ( edge->link && FT_ABS( edge->link->pos - edge[-1].pos ) > 16 )
3176 {
3177 #ifdef FT_DEBUG_LEVEL_TRACE
3178 FT_TRACE5(( " BOUND: edge %d (pos=%.2f) moved to %.2f\n",
3179 edge - edges,
3180 edge->pos / 64.0,
3181 edge[-1].pos / 64.0 ));
3182
3183 num_actions++;
3184 #endif
3185
3186 edge->pos = edge[-1].pos;
3187 }
3188 }
3189 }
3190 }
3191
3192 /* make sure that lowercase m's maintain their symmetry */
3193
3194 /* In general, lowercase m's have six vertical edges if they are sans */
3195 /* serif, or twelve if they are with serifs. This implementation is */
3196 /* based on that assumption, and seems to work very well with most */
3197 /* faces. However, if for a certain face this assumption is not */
3198 /* true, the m is just rendered like before. In addition, any stem */
3199 /* correction will only be applied to symmetrical glyphs (even if the */
3200 /* glyph is not an m), so the potential for unwanted distortion is */
3201 /* relatively low. */
3202
3203 /* We don't handle horizontal edges since we can't easily assure that */
3204 /* the third (lowest) stem aligns with the base line; it might end up */
3205 /* one pixel higher or lower. */
3206
3207 n_edges = edge_limit - edges;
3208 if ( dim == AF_DIMENSION_HORZ && ( n_edges == 6 || n_edges == 12 ) )
3209 {
3210 AF_Edge edge1, edge2, edge3;
3211 FT_Pos dist1, dist2, span, delta;
3212
3213
3214 if ( n_edges == 6 )
3215 {
3216 edge1 = edges;
3217 edge2 = edges + 2;
3218 edge3 = edges + 4;
3219 }
3220 else
3221 {
3222 edge1 = edges + 1;
3223 edge2 = edges + 5;
3224 edge3 = edges + 9;
3225 }
3226
3227 dist1 = edge2->opos - edge1->opos;
3228 dist2 = edge3->opos - edge2->opos;
3229
3230 span = dist1 - dist2;
3231 if ( span < 0 )
3232 span = -span;
3233
3234 if ( span < 8 )
3235 {
3236 delta = edge3->pos - ( 2 * edge2->pos - edge1->pos );
3237 edge3->pos -= delta;
3238 if ( edge3->link )
3239 edge3->link->pos -= delta;
3240
3241 /* move the serifs along with the stem */
3242 if ( n_edges == 12 )
3243 {
3244 ( edges + 8 )->pos -= delta;
3245 ( edges + 11 )->pos -= delta;
3246 }
3247
3248 edge3->flags |= AF_EDGE_DONE;
3249 if ( edge3->link )
3250 edge3->link->flags |= AF_EDGE_DONE;
3251 }
3252 }
3253
3254 if ( has_serifs || !anchor )
3255 {
3256 /*
3257 * now hint the remaining edges (serifs and single) in order
3258 * to complete our processing
3259 */
3260 for ( edge = edges; edge < edge_limit; edge++ )
3261 {
3262 FT_Pos delta;
3263
3264
3265 if ( edge->flags & AF_EDGE_DONE )
3266 continue;
3267
3268 delta = 1000;
3269
3270 if ( edge->serif )
3271 {
3272 delta = edge->serif->opos - edge->opos;
3273 if ( delta < 0 )
3274 delta = -delta;
3275 }
3276
3277 if ( delta < 64 + 16 )
3278 {
3279 af_latin_align_serif_edge( hints, edge->serif, edge );
3280 FT_TRACE5(( " SERIF: edge %d (opos=%.2f) serif to %d (opos=%.2f)"
3281 " aligned to %.2f\n",
3282 edge - edges, edge->opos / 64.0,
3283 edge->serif - edges, edge->serif->opos / 64.0,
3284 edge->pos / 64.0 ));
3285 }
3286 else if ( !anchor )
3287 {
3288 edge->pos = FT_PIX_ROUND( edge->opos );
3289 anchor = edge;
3290 FT_TRACE5(( " SERIF_ANCHOR: edge %d (opos=%.2f)"
3291 " snapped to %.2f\n",
3292 edge-edges, edge->opos / 64.0, edge->pos / 64.0 ));
3293 }
3294 else
3295 {
3296 AF_Edge before, after;
3297
3298
3299 for ( before = edge - 1; before >= edges; before-- )
3300 if ( before->flags & AF_EDGE_DONE )
3301 break;
3302
3303 for ( after = edge + 1; after < edge_limit; after++ )
3304 if ( after->flags & AF_EDGE_DONE )
3305 break;
3306
3307 if ( before >= edges && before < edge &&
3308 after < edge_limit && after > edge )
3309 {
3310 if ( after->opos == before->opos )
3311 edge->pos = before->pos;
3312 else
3313 edge->pos = before->pos +
3314 FT_MulDiv( edge->opos - before->opos,
3315 after->pos - before->pos,
3316 after->opos - before->opos );
3317
3318 FT_TRACE5(( " SERIF_LINK1: edge %d (opos=%.2f) snapped to %.2f"
3319 " from %d (opos=%.2f)\n",
3320 edge - edges, edge->opos / 64.0,
3321 edge->pos / 64.0,
3322 before - edges, before->opos / 64.0 ));
3323 }
3324 else
3325 {
3326 edge->pos = anchor->pos +
3327 ( ( edge->opos - anchor->opos + 16 ) & ~31 );
3328 FT_TRACE5(( " SERIF_LINK2: edge %d (opos=%.2f)"
3329 " snapped to %.2f\n",
3330 edge - edges, edge->opos / 64.0, edge->pos / 64.0 ));
3331 }
3332 }
3333
3334 #ifdef FT_DEBUG_LEVEL_TRACE
3335 num_actions++;
3336 #endif
3337 edge->flags |= AF_EDGE_DONE;
3338
3339 if ( edge > edges &&
3340 ( top_to_bottom_hinting ? ( edge->pos > edge[-1].pos )
3341 : ( edge->pos < edge[-1].pos ) ) )
3342 {
3343 /* don't move if stem would (almost) disappear otherwise; */
3344 /* the ad-hoc value 16 corresponds to 1/4px */
3345 if ( edge->link && FT_ABS( edge->link->pos - edge[-1].pos ) > 16 )
3346 {
3347 #ifdef FT_DEBUG_LEVEL_TRACE
3348 FT_TRACE5(( " BOUND: edge %d (pos=%.2f) moved to %.2f\n",
3349 edge - edges,
3350 edge->pos / 64.0,
3351 edge[-1].pos / 64.0 ));
3352
3353 num_actions++;
3354 #endif
3355 edge->pos = edge[-1].pos;
3356 }
3357 }
3358
3359 if ( edge + 1 < edge_limit &&
3360 edge[1].flags & AF_EDGE_DONE &&
3361 ( top_to_bottom_hinting ? ( edge->pos < edge[1].pos )
3362 : ( edge->pos > edge[1].pos ) ) )
3363 {
3364 /* don't move if stem would (almost) disappear otherwise; */
3365 /* the ad-hoc value 16 corresponds to 1/4px */
3366 if ( edge->link && FT_ABS( edge->link->pos - edge[-1].pos ) > 16 )
3367 {
3368 #ifdef FT_DEBUG_LEVEL_TRACE
3369 FT_TRACE5(( " BOUND: edge %d (pos=%.2f) moved to %.2f\n",
3370 edge - edges,
3371 edge->pos / 64.0,
3372 edge[1].pos / 64.0 ));
3373
3374 num_actions++;
3375 #endif
3376
3377 edge->pos = edge[1].pos;
3378 }
3379 }
3380 }
3381 }
3382
3383 #ifdef FT_DEBUG_LEVEL_TRACE
3384 if ( !num_actions )
3385 FT_TRACE5(( " (none)\n" ));
3386 FT_TRACE5(( "\n" ));
3387 #endif
3388 }
3389
3390
3391 /* Apply the complete hinting algorithm to a latin glyph. */
3392
3393 static FT_Error
3394 af_latin_hints_apply( FT_UInt glyph_index,
3395 AF_GlyphHints hints,
3396 FT_Outline* outline,
3397 AF_LatinMetrics metrics )
3398 {
3399 FT_Error error;
3400 int dim;
3401
3402 AF_LatinAxis axis;
3403
3404
3405 error = af_glyph_hints_reload( hints, outline );
3406 if ( error )
3407 goto Exit;
3408
3409 /* analyze glyph outline */
3410 #ifdef AF_CONFIG_OPTION_USE_WARPER
3411 if ( ( metrics->root.scaler.render_mode == FT_RENDER_MODE_LIGHT &&
3412 AF_HINTS_DO_WARP( hints ) ) ||
3413 AF_HINTS_DO_HORIZONTAL( hints ) )
3414 #else
3415 if ( AF_HINTS_DO_HORIZONTAL( hints ) )
3416 #endif
3417 {
3418 axis = &metrics->axis[AF_DIMENSION_HORZ];
3419 error = af_latin_hints_detect_features( hints,
3420 axis->width_count,
3421 axis->widths,
3422 AF_DIMENSION_HORZ );
3423 if ( error )
3424 goto Exit;
3425 }
3426
3427 if ( AF_HINTS_DO_VERTICAL( hints ) )
3428 {
3429 axis = &metrics->axis[AF_DIMENSION_VERT];
3430 error = af_latin_hints_detect_features( hints,
3431 axis->width_count,
3432 axis->widths,
3433 AF_DIMENSION_VERT );
3434 if ( error )
3435 goto Exit;
3436
3437 /* apply blue zones to base characters only */
3438 if ( !( metrics->root.globals->glyph_styles[glyph_index] & AF_NONBASE ) )
3439 af_latin_hints_compute_blue_edges( hints, metrics );
3440 }
3441
3442 /* grid-fit the outline */
3443 for ( dim = 0; dim < AF_DIMENSION_MAX; dim++ )
3444 {
3445 #ifdef AF_CONFIG_OPTION_USE_WARPER
3446 if ( dim == AF_DIMENSION_HORZ &&
3447 metrics->root.scaler.render_mode == FT_RENDER_MODE_LIGHT &&
3448 AF_HINTS_DO_WARP( hints ) )
3449 {
3450 AF_WarperRec warper;
3451 FT_Fixed scale;
3452 FT_Pos delta;
3453
3454
3455 af_warper_compute( &warper, hints, (AF_Dimension)dim,
3456 &scale, &delta );
3457 af_glyph_hints_scale_dim( hints, (AF_Dimension)dim,
3458 scale, delta );
3459 continue;
3460 }
3461 #endif /* AF_CONFIG_OPTION_USE_WARPER */
3462
3463 if ( ( dim == AF_DIMENSION_HORZ && AF_HINTS_DO_HORIZONTAL( hints ) ) ||
3464 ( dim == AF_DIMENSION_VERT && AF_HINTS_DO_VERTICAL( hints ) ) )
3465 {
3466 af_latin_hint_edges( hints, (AF_Dimension)dim );
3467 af_glyph_hints_align_edge_points( hints, (AF_Dimension)dim );
3468 af_glyph_hints_align_strong_points( hints, (AF_Dimension)dim );
3469 af_glyph_hints_align_weak_points( hints, (AF_Dimension)dim );
3470 }
3471 }
3472
3473 af_glyph_hints_save( hints, outline );
3474
3475 Exit:
3476 return error;
3477 }
3478
3479
3480 /*************************************************************************/
3481 /*************************************************************************/
3482 /***** *****/
3483 /***** L A T I N S C R I P T C L A S S *****/
3484 /***** *****/
3485 /*************************************************************************/
3486 /*************************************************************************/
3487
3488
3489 AF_DEFINE_WRITING_SYSTEM_CLASS(
3490 af_latin_writing_system_class,
3491
3492 AF_WRITING_SYSTEM_LATIN,
3493
3494 sizeof ( AF_LatinMetricsRec ),
3495
3496 (AF_WritingSystem_InitMetricsFunc) af_latin_metrics_init,
3497 (AF_WritingSystem_ScaleMetricsFunc)af_latin_metrics_scale,
3498 (AF_WritingSystem_DoneMetricsFunc) NULL,
3499 (AF_WritingSystem_GetStdWidthsFunc)af_latin_get_standard_widths,
3500
3501 (AF_WritingSystem_InitHintsFunc) af_latin_hints_init,
3502 (AF_WritingSystem_ApplyHintsFunc) af_latin_hints_apply
3503 )
3504
3505
3506 /* END */