[FREETYPE] Update to v2.6.3. CORE-10964
[reactos.git] / reactos / 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, scaler->y_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 scale = new_scale;
1143
1144 FT_TRACE5((
1145 "af_latin_metrics_scale_dim:"
1146 " x height alignment (style `%s'):\n"
1147 " "
1148 " vertical scaling changed from %.4f to %.4f (by %d%%)\n"
1149 "\n",
1150 af_style_names[metrics->root.style_class->style],
1151 axis->org_scale / 65536.0,
1152 scale / 65536.0,
1153 ( fitted - scaled ) * 100 / scaled ));
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 FT_Pos min_pos = 32000; /* minimum segment pos != min_coord */
1459 FT_Pos max_pos = -32000; /* maximum segment pos != max_coord */
1460 FT_Pos min_on_pos = 32000;
1461 FT_Pos max_on_pos = -32000;
1462 FT_Bool passed;
1463
1464
1465 if ( point == last ) /* skip singletons -- just in case */
1466 continue;
1467
1468 if ( FT_ABS( last->out_dir ) == major_dir &&
1469 FT_ABS( point->out_dir ) == major_dir )
1470 {
1471 /* we are already on an edge, try to locate its start */
1472 last = point;
1473
1474 for (;;)
1475 {
1476 point = point->prev;
1477 if ( FT_ABS( point->out_dir ) != major_dir )
1478 {
1479 point = point->next;
1480 break;
1481 }
1482 if ( point == last )
1483 break;
1484 }
1485 }
1486
1487 last = point;
1488 passed = 0;
1489
1490 for (;;)
1491 {
1492 FT_Pos u, v;
1493
1494
1495 if ( on_edge )
1496 {
1497 u = point->u;
1498 if ( u < min_pos )
1499 min_pos = u;
1500 if ( u > max_pos )
1501 max_pos = u;
1502
1503 /* get minimum and maximum coordinate of on points */
1504 if ( !( point->flags & AF_FLAG_CONTROL ) )
1505 {
1506 v = point->v;
1507 if ( v < min_on_pos )
1508 min_on_pos = v;
1509 if ( v > max_on_pos )
1510 max_on_pos = v;
1511 }
1512
1513 if ( point->out_dir != segment_dir || point == last )
1514 {
1515 /* we are just leaving an edge; record a new segment! */
1516 segment->last = point;
1517 segment->pos = (FT_Short)( ( min_pos + max_pos ) >> 1 );
1518
1519 /* a segment is round if either its first or last point */
1520 /* is a control point, and the length of the on points */
1521 /* inbetween doesn't exceed a heuristic limit */
1522 if ( ( segment->first->flags | point->flags ) & AF_FLAG_CONTROL &&
1523 ( max_on_pos - min_on_pos ) < flat_threshold )
1524 segment->flags |= AF_EDGE_ROUND;
1525
1526 /* compute segment size */
1527 min_pos = max_pos = point->v;
1528
1529 v = segment->first->v;
1530 if ( v < min_pos )
1531 min_pos = v;
1532 if ( v > max_pos )
1533 max_pos = v;
1534
1535 segment->min_coord = (FT_Short)min_pos;
1536 segment->max_coord = (FT_Short)max_pos;
1537 segment->height = (FT_Short)( segment->max_coord -
1538 segment->min_coord );
1539
1540 on_edge = 0;
1541 segment = NULL;
1542 /* fall through */
1543 }
1544 }
1545
1546 /* now exit if we are at the start/end point */
1547 if ( point == last )
1548 {
1549 if ( passed )
1550 break;
1551 passed = 1;
1552 }
1553
1554 if ( !on_edge && FT_ABS( point->out_dir ) == major_dir )
1555 {
1556 /* this is the start of a new segment! */
1557 segment_dir = (AF_Direction)point->out_dir;
1558
1559 error = af_axis_hints_new_segment( axis, memory, &segment );
1560 if ( error )
1561 goto Exit;
1562
1563 /* clear all segment fields */
1564 segment[0] = seg0;
1565
1566 segment->dir = (FT_Char)segment_dir;
1567 segment->first = point;
1568 segment->last = point;
1569
1570 min_pos = max_pos = point->u;
1571
1572 if ( point->flags & AF_FLAG_CONTROL )
1573 {
1574 min_on_pos = 32000;
1575 max_on_pos = -32000;
1576 }
1577 else
1578 min_on_pos = max_on_pos = point->v;
1579
1580 on_edge = 1;
1581 }
1582
1583 point = point->next;
1584 }
1585
1586 } /* contours */
1587
1588
1589 /* now slightly increase the height of segments if this makes */
1590 /* sense -- this is used to better detect and ignore serifs */
1591 {
1592 AF_Segment segments = axis->segments;
1593 AF_Segment segments_end = segments + axis->num_segments;
1594
1595
1596 for ( segment = segments; segment < segments_end; segment++ )
1597 {
1598 AF_Point first = segment->first;
1599 AF_Point last = segment->last;
1600 FT_Pos first_v = first->v;
1601 FT_Pos last_v = last->v;
1602
1603
1604 if ( first_v < last_v )
1605 {
1606 AF_Point p;
1607
1608
1609 p = first->prev;
1610 if ( p->v < first_v )
1611 segment->height = (FT_Short)( segment->height +
1612 ( ( first_v - p->v ) >> 1 ) );
1613
1614 p = last->next;
1615 if ( p->v > last_v )
1616 segment->height = (FT_Short)( segment->height +
1617 ( ( p->v - last_v ) >> 1 ) );
1618 }
1619 else
1620 {
1621 AF_Point p;
1622
1623
1624 p = first->prev;
1625 if ( p->v > first_v )
1626 segment->height = (FT_Short)( segment->height +
1627 ( ( p->v - first_v ) >> 1 ) );
1628
1629 p = last->next;
1630 if ( p->v < last_v )
1631 segment->height = (FT_Short)( segment->height +
1632 ( ( last_v - p->v ) >> 1 ) );
1633 }
1634 }
1635 }
1636
1637 Exit:
1638 return error;
1639 }
1640
1641
1642 /* Link segments to form stems and serifs. If `width_count' and */
1643 /* `widths' are non-zero, use them to fine-tune the scoring function. */
1644
1645 FT_LOCAL_DEF( void )
1646 af_latin_hints_link_segments( AF_GlyphHints hints,
1647 FT_UInt width_count,
1648 AF_WidthRec* widths,
1649 AF_Dimension dim )
1650 {
1651 AF_AxisHints axis = &hints->axis[dim];
1652 AF_Segment segments = axis->segments;
1653 AF_Segment segment_limit = segments + axis->num_segments;
1654 FT_Pos len_threshold, len_score, dist_score, max_width;
1655 AF_Segment seg1, seg2;
1656
1657
1658 if ( width_count )
1659 max_width = widths[width_count - 1].org;
1660 else
1661 max_width = 0;
1662
1663 /* a heuristic value to set up a minimum value for overlapping */
1664 len_threshold = AF_LATIN_CONSTANT( hints->metrics, 8 );
1665 if ( len_threshold == 0 )
1666 len_threshold = 1;
1667
1668 /* a heuristic value to weight lengths */
1669 len_score = AF_LATIN_CONSTANT( hints->metrics, 6000 );
1670
1671 /* a heuristic value to weight distances (no call to */
1672 /* AF_LATIN_CONSTANT needed, since we work on multiples */
1673 /* of the stem width) */
1674 dist_score = 3000;
1675
1676 /* now compare each segment to the others */
1677 for ( seg1 = segments; seg1 < segment_limit; seg1++ )
1678 {
1679 if ( seg1->dir != axis->major_dir )
1680 continue;
1681
1682 /* search for stems having opposite directions, */
1683 /* with seg1 to the `left' of seg2 */
1684 for ( seg2 = segments; seg2 < segment_limit; seg2++ )
1685 {
1686 FT_Pos pos1 = seg1->pos;
1687 FT_Pos pos2 = seg2->pos;
1688
1689
1690 if ( seg1->dir + seg2->dir == 0 && pos2 > pos1 )
1691 {
1692 /* compute distance between the two segments */
1693 FT_Pos min = seg1->min_coord;
1694 FT_Pos max = seg1->max_coord;
1695 FT_Pos len;
1696
1697
1698 if ( min < seg2->min_coord )
1699 min = seg2->min_coord;
1700
1701 if ( max > seg2->max_coord )
1702 max = seg2->max_coord;
1703
1704 /* compute maximum coordinate difference of the two segments */
1705 /* (this is, how much they overlap) */
1706 len = max - min;
1707 if ( len >= len_threshold )
1708 {
1709 /*
1710 * The score is the sum of two demerits indicating the
1711 * `badness' of a fit, measured along the segments' main axis
1712 * and orthogonal to it, respectively.
1713 *
1714 * o The less overlapping along the main axis, the worse it
1715 * is, causing a larger demerit.
1716 *
1717 * o The nearer the orthogonal distance to a stem width, the
1718 * better it is, causing a smaller demerit. For simplicity,
1719 * however, we only increase the demerit for values that
1720 * exceed the largest stem width.
1721 */
1722
1723 FT_Pos dist = pos2 - pos1;
1724
1725 FT_Pos dist_demerit, score;
1726
1727
1728 if ( max_width )
1729 {
1730 /* distance demerits are based on multiples of `max_width'; */
1731 /* we scale by 1024 for getting more precision */
1732 FT_Pos delta = ( dist << 10 ) / max_width - ( 1 << 10 );
1733
1734
1735 if ( delta > 10000 )
1736 dist_demerit = 32000;
1737 else if ( delta > 0 )
1738 dist_demerit = delta * delta / dist_score;
1739 else
1740 dist_demerit = 0;
1741 }
1742 else
1743 dist_demerit = dist; /* default if no widths available */
1744
1745 score = dist_demerit + len_score / len;
1746
1747 /* and we search for the smallest score */
1748 if ( score < seg1->score )
1749 {
1750 seg1->score = score;
1751 seg1->link = seg2;
1752 }
1753
1754 if ( score < seg2->score )
1755 {
1756 seg2->score = score;
1757 seg2->link = seg1;
1758 }
1759 }
1760 }
1761 }
1762 }
1763
1764 /* now compute the `serif' segments, cf. explanations in `afhints.h' */
1765 for ( seg1 = segments; seg1 < segment_limit; seg1++ )
1766 {
1767 seg2 = seg1->link;
1768
1769 if ( seg2 )
1770 {
1771 if ( seg2->link != seg1 )
1772 {
1773 seg1->link = 0;
1774 seg1->serif = seg2->link;
1775 }
1776 }
1777 }
1778 }
1779
1780
1781 /* Link segments to edges, using feature analysis for selection. */
1782
1783 FT_LOCAL_DEF( FT_Error )
1784 af_latin_hints_compute_edges( AF_GlyphHints hints,
1785 AF_Dimension dim )
1786 {
1787 AF_AxisHints axis = &hints->axis[dim];
1788 FT_Error error = FT_Err_Ok;
1789 FT_Memory memory = hints->memory;
1790 AF_LatinAxis laxis = &((AF_LatinMetrics)hints->metrics)->axis[dim];
1791
1792 AF_StyleClass style_class = hints->metrics->style_class;
1793 AF_ScriptClass script_class = AF_SCRIPT_CLASSES_GET
1794 [style_class->script];
1795
1796 FT_Bool top_to_bottom_hinting = 0;
1797
1798 AF_Segment segments = axis->segments;
1799 AF_Segment segment_limit = segments + axis->num_segments;
1800 AF_Segment seg;
1801
1802 #if 0
1803 AF_Direction up_dir;
1804 #endif
1805 FT_Fixed scale;
1806 FT_Pos edge_distance_threshold;
1807 FT_Pos segment_length_threshold;
1808
1809
1810 axis->num_edges = 0;
1811
1812 scale = ( dim == AF_DIMENSION_HORZ ) ? hints->x_scale
1813 : hints->y_scale;
1814
1815 #if 0
1816 up_dir = ( dim == AF_DIMENSION_HORZ ) ? AF_DIR_UP
1817 : AF_DIR_RIGHT;
1818 #endif
1819
1820 if ( dim == AF_DIMENSION_VERT )
1821 top_to_bottom_hinting = script_class->top_to_bottom_hinting;
1822
1823 /*
1824 * We ignore all segments that are less than 1 pixel in length
1825 * to avoid many problems with serif fonts. We compute the
1826 * corresponding threshold in font units.
1827 */
1828 if ( dim == AF_DIMENSION_HORZ )
1829 segment_length_threshold = FT_DivFix( 64, hints->y_scale );
1830 else
1831 segment_length_threshold = 0;
1832
1833 /*********************************************************************/
1834 /* */
1835 /* We begin by generating a sorted table of edges for the current */
1836 /* direction. To do so, we simply scan each segment and try to find */
1837 /* an edge in our table that corresponds to its position. */
1838 /* */
1839 /* If no edge is found, we create and insert a new edge in the */
1840 /* sorted table. Otherwise, we simply add the segment to the edge's */
1841 /* list which gets processed in the second step to compute the */
1842 /* edge's properties. */
1843 /* */
1844 /* Note that the table of edges is sorted along the segment/edge */
1845 /* position. */
1846 /* */
1847 /*********************************************************************/
1848
1849 /* assure that edge distance threshold is at most 0.25px */
1850 edge_distance_threshold = FT_MulFix( laxis->edge_distance_threshold,
1851 scale );
1852 if ( edge_distance_threshold > 64 / 4 )
1853 edge_distance_threshold = 64 / 4;
1854
1855 edge_distance_threshold = FT_DivFix( edge_distance_threshold,
1856 scale );
1857
1858 for ( seg = segments; seg < segment_limit; seg++ )
1859 {
1860 AF_Edge found = NULL;
1861 FT_Int ee;
1862
1863
1864 if ( seg->height < segment_length_threshold )
1865 continue;
1866
1867 /* A special case for serif edges: If they are smaller than */
1868 /* 1.5 pixels we ignore them. */
1869 if ( seg->serif &&
1870 2 * seg->height < 3 * segment_length_threshold )
1871 continue;
1872
1873 /* look for an edge corresponding to the segment */
1874 for ( ee = 0; ee < axis->num_edges; ee++ )
1875 {
1876 AF_Edge edge = axis->edges + ee;
1877 FT_Pos dist;
1878
1879
1880 dist = seg->pos - edge->fpos;
1881 if ( dist < 0 )
1882 dist = -dist;
1883
1884 if ( dist < edge_distance_threshold && edge->dir == seg->dir )
1885 {
1886 found = edge;
1887 break;
1888 }
1889 }
1890
1891 if ( !found )
1892 {
1893 AF_Edge edge;
1894
1895
1896 /* insert a new edge in the list and */
1897 /* sort according to the position */
1898 error = af_axis_hints_new_edge( axis, seg->pos,
1899 (AF_Direction)seg->dir,
1900 top_to_bottom_hinting,
1901 memory, &edge );
1902 if ( error )
1903 goto Exit;
1904
1905 /* add the segment to the new edge's list */
1906 FT_ZERO( edge );
1907
1908 edge->first = seg;
1909 edge->last = seg;
1910 edge->dir = seg->dir;
1911 edge->fpos = seg->pos;
1912 edge->opos = FT_MulFix( seg->pos, scale );
1913 edge->pos = edge->opos;
1914 seg->edge_next = seg;
1915 }
1916 else
1917 {
1918 /* if an edge was found, simply add the segment to the edge's */
1919 /* list */
1920 seg->edge_next = found->first;
1921 found->last->edge_next = seg;
1922 found->last = seg;
1923 }
1924 }
1925
1926
1927 /******************************************************************/
1928 /* */
1929 /* Good, we now compute each edge's properties according to the */
1930 /* segments found on its position. Basically, these are */
1931 /* */
1932 /* - the edge's main direction */
1933 /* - stem edge, serif edge or both (which defaults to stem then) */
1934 /* - rounded edge, straight or both (which defaults to straight) */
1935 /* - link for edge */
1936 /* */
1937 /******************************************************************/
1938
1939 /* first of all, set the `edge' field in each segment -- this is */
1940 /* required in order to compute edge links */
1941
1942 /*
1943 * Note that removing this loop and setting the `edge' field of each
1944 * segment directly in the code above slows down execution speed for
1945 * some reasons on platforms like the Sun.
1946 */
1947 {
1948 AF_Edge edges = axis->edges;
1949 AF_Edge edge_limit = edges + axis->num_edges;
1950 AF_Edge edge;
1951
1952
1953 for ( edge = edges; edge < edge_limit; edge++ )
1954 {
1955 seg = edge->first;
1956 if ( seg )
1957 do
1958 {
1959 seg->edge = edge;
1960 seg = seg->edge_next;
1961
1962 } while ( seg != edge->first );
1963 }
1964
1965 /* now compute each edge properties */
1966 for ( edge = edges; edge < edge_limit; edge++ )
1967 {
1968 FT_Int is_round = 0; /* does it contain round segments? */
1969 FT_Int is_straight = 0; /* does it contain straight segments? */
1970 #if 0
1971 FT_Pos ups = 0; /* number of upwards segments */
1972 FT_Pos downs = 0; /* number of downwards segments */
1973 #endif
1974
1975
1976 seg = edge->first;
1977
1978 do
1979 {
1980 FT_Bool is_serif;
1981
1982
1983 /* check for roundness of segment */
1984 if ( seg->flags & AF_EDGE_ROUND )
1985 is_round++;
1986 else
1987 is_straight++;
1988
1989 #if 0
1990 /* check for segment direction */
1991 if ( seg->dir == up_dir )
1992 ups += seg->max_coord - seg->min_coord;
1993 else
1994 downs += seg->max_coord - seg->min_coord;
1995 #endif
1996
1997 /* check for links -- if seg->serif is set, then seg->link must */
1998 /* be ignored */
1999 is_serif = (FT_Bool)( seg->serif &&
2000 seg->serif->edge &&
2001 seg->serif->edge != edge );
2002
2003 if ( ( seg->link && seg->link->edge != NULL ) || is_serif )
2004 {
2005 AF_Edge edge2;
2006 AF_Segment seg2;
2007
2008
2009 edge2 = edge->link;
2010 seg2 = seg->link;
2011
2012 if ( is_serif )
2013 {
2014 seg2 = seg->serif;
2015 edge2 = edge->serif;
2016 }
2017
2018 if ( edge2 )
2019 {
2020 FT_Pos edge_delta;
2021 FT_Pos seg_delta;
2022
2023
2024 edge_delta = edge->fpos - edge2->fpos;
2025 if ( edge_delta < 0 )
2026 edge_delta = -edge_delta;
2027
2028 seg_delta = seg->pos - seg2->pos;
2029 if ( seg_delta < 0 )
2030 seg_delta = -seg_delta;
2031
2032 if ( seg_delta < edge_delta )
2033 edge2 = seg2->edge;
2034 }
2035 else
2036 edge2 = seg2->edge;
2037
2038 if ( is_serif )
2039 {
2040 edge->serif = edge2;
2041 edge2->flags |= AF_EDGE_SERIF;
2042 }
2043 else
2044 edge->link = edge2;
2045 }
2046
2047 seg = seg->edge_next;
2048
2049 } while ( seg != edge->first );
2050
2051 /* set the round/straight flags */
2052 edge->flags = AF_EDGE_NORMAL;
2053
2054 if ( is_round > 0 && is_round >= is_straight )
2055 edge->flags |= AF_EDGE_ROUND;
2056
2057 #if 0
2058 /* set the edge's main direction */
2059 edge->dir = AF_DIR_NONE;
2060
2061 if ( ups > downs )
2062 edge->dir = (FT_Char)up_dir;
2063
2064 else if ( ups < downs )
2065 edge->dir = (FT_Char)-up_dir;
2066
2067 else if ( ups == downs )
2068 edge->dir = 0; /* both up and down! */
2069 #endif
2070
2071 /* get rid of serifs if link is set */
2072 /* XXX: This gets rid of many unpleasant artefacts! */
2073 /* Example: the `c' in cour.pfa at size 13 */
2074
2075 if ( edge->serif && edge->link )
2076 edge->serif = NULL;
2077 }
2078 }
2079
2080 Exit:
2081 return error;
2082 }
2083
2084
2085 /* Detect segments and edges for given dimension. */
2086
2087 FT_LOCAL_DEF( FT_Error )
2088 af_latin_hints_detect_features( AF_GlyphHints hints,
2089 FT_UInt width_count,
2090 AF_WidthRec* widths,
2091 AF_Dimension dim )
2092 {
2093 FT_Error error;
2094
2095
2096 error = af_latin_hints_compute_segments( hints, dim );
2097 if ( !error )
2098 {
2099 af_latin_hints_link_segments( hints, width_count, widths, dim );
2100
2101 error = af_latin_hints_compute_edges( hints, dim );
2102 }
2103
2104 return error;
2105 }
2106
2107
2108 /* Compute all edges which lie within blue zones. */
2109
2110 static void
2111 af_latin_hints_compute_blue_edges( AF_GlyphHints hints,
2112 AF_LatinMetrics metrics )
2113 {
2114 AF_AxisHints axis = &hints->axis[AF_DIMENSION_VERT];
2115 AF_Edge edge = axis->edges;
2116 AF_Edge edge_limit = edge + axis->num_edges;
2117 AF_LatinAxis latin = &metrics->axis[AF_DIMENSION_VERT];
2118 FT_Fixed scale = latin->scale;
2119
2120
2121 /* compute which blue zones are active, i.e. have their scaled */
2122 /* size < 3/4 pixels */
2123
2124 /* for each horizontal edge search the blue zone which is closest */
2125 for ( ; edge < edge_limit; edge++ )
2126 {
2127 FT_UInt bb;
2128 AF_Width best_blue = NULL;
2129 FT_Bool best_blue_is_neutral = 0;
2130 FT_Pos best_dist; /* initial threshold */
2131
2132
2133 /* compute the initial threshold as a fraction of the EM size */
2134 /* (the value 40 is heuristic) */
2135 best_dist = FT_MulFix( metrics->units_per_em / 40, scale );
2136
2137 /* assure a minimum distance of 0.5px */
2138 if ( best_dist > 64 / 2 )
2139 best_dist = 64 / 2;
2140
2141 for ( bb = 0; bb < latin->blue_count; bb++ )
2142 {
2143 AF_LatinBlue blue = latin->blues + bb;
2144 FT_Bool is_top_blue, is_neutral_blue, is_major_dir;
2145
2146
2147 /* skip inactive blue zones (i.e., those that are too large) */
2148 if ( !( blue->flags & AF_LATIN_BLUE_ACTIVE ) )
2149 continue;
2150
2151 /* if it is a top zone, check for right edges (against the major */
2152 /* direction); if it is a bottom zone, check for left edges (in */
2153 /* the major direction) -- this assumes the TrueType convention */
2154 /* for the orientation of contours */
2155 is_top_blue =
2156 (FT_Byte)( ( blue->flags & ( AF_LATIN_BLUE_TOP |
2157 AF_LATIN_BLUE_SUB_TOP ) ) != 0 );
2158 is_neutral_blue =
2159 (FT_Byte)( ( blue->flags & AF_LATIN_BLUE_NEUTRAL ) != 0);
2160 is_major_dir =
2161 FT_BOOL( edge->dir == axis->major_dir );
2162
2163 /* neutral blue zones are handled for both directions */
2164 if ( is_top_blue ^ is_major_dir || is_neutral_blue )
2165 {
2166 FT_Pos dist;
2167
2168
2169 /* first of all, compare it to the reference position */
2170 dist = edge->fpos - blue->ref.org;
2171 if ( dist < 0 )
2172 dist = -dist;
2173
2174 dist = FT_MulFix( dist, scale );
2175 if ( dist < best_dist )
2176 {
2177 best_dist = dist;
2178 best_blue = &blue->ref;
2179 best_blue_is_neutral = is_neutral_blue;
2180 }
2181
2182 /* now compare it to the overshoot position and check whether */
2183 /* the edge is rounded, and whether the edge is over the */
2184 /* reference position of a top zone, or under the reference */
2185 /* position of a bottom zone (provided we don't have a */
2186 /* neutral blue zone) */
2187 if ( edge->flags & AF_EDGE_ROUND &&
2188 dist != 0 &&
2189 !is_neutral_blue )
2190 {
2191 FT_Bool is_under_ref = FT_BOOL( edge->fpos < blue->ref.org );
2192
2193
2194 if ( is_top_blue ^ is_under_ref )
2195 {
2196 dist = edge->fpos - blue->shoot.org;
2197 if ( dist < 0 )
2198 dist = -dist;
2199
2200 dist = FT_MulFix( dist, scale );
2201 if ( dist < best_dist )
2202 {
2203 best_dist = dist;
2204 best_blue = &blue->shoot;
2205 best_blue_is_neutral = is_neutral_blue;
2206 }
2207 }
2208 }
2209 }
2210 }
2211
2212 if ( best_blue )
2213 {
2214 edge->blue_edge = best_blue;
2215 if ( best_blue_is_neutral )
2216 edge->flags |= AF_EDGE_NEUTRAL;
2217 }
2218 }
2219 }
2220
2221
2222 /* Initalize hinting engine. */
2223
2224 static FT_Error
2225 af_latin_hints_init( AF_GlyphHints hints,
2226 AF_LatinMetrics metrics )
2227 {
2228 FT_Render_Mode mode;
2229 FT_UInt32 scaler_flags, other_flags;
2230 FT_Face face = metrics->root.scaler.face;
2231
2232
2233 af_glyph_hints_rescale( hints, (AF_StyleMetrics)metrics );
2234
2235 /*
2236 * correct x_scale and y_scale if needed, since they may have
2237 * been modified by `af_latin_metrics_scale_dim' above
2238 */
2239 hints->x_scale = metrics->axis[AF_DIMENSION_HORZ].scale;
2240 hints->x_delta = metrics->axis[AF_DIMENSION_HORZ].delta;
2241 hints->y_scale = metrics->axis[AF_DIMENSION_VERT].scale;
2242 hints->y_delta = metrics->axis[AF_DIMENSION_VERT].delta;
2243
2244 /* compute flags depending on render mode, etc. */
2245 mode = metrics->root.scaler.render_mode;
2246
2247 #if 0 /* #ifdef AF_CONFIG_OPTION_USE_WARPER */
2248 if ( mode == FT_RENDER_MODE_LCD || mode == FT_RENDER_MODE_LCD_V )
2249 metrics->root.scaler.render_mode = mode = FT_RENDER_MODE_NORMAL;
2250 #endif
2251
2252 scaler_flags = hints->scaler_flags;
2253 other_flags = 0;
2254
2255 /*
2256 * We snap the width of vertical stems for the monochrome and
2257 * horizontal LCD rendering targets only.
2258 */
2259 if ( mode == FT_RENDER_MODE_MONO || mode == FT_RENDER_MODE_LCD )
2260 other_flags |= AF_LATIN_HINTS_HORZ_SNAP;
2261
2262 /*
2263 * We snap the width of horizontal stems for the monochrome and
2264 * vertical LCD rendering targets only.
2265 */
2266 if ( mode == FT_RENDER_MODE_MONO || mode == FT_RENDER_MODE_LCD_V )
2267 other_flags |= AF_LATIN_HINTS_VERT_SNAP;
2268
2269 /*
2270 * We adjust stems to full pixels only if we don't use the `light' mode.
2271 */
2272 if ( mode != FT_RENDER_MODE_LIGHT )
2273 other_flags |= AF_LATIN_HINTS_STEM_ADJUST;
2274
2275 if ( mode == FT_RENDER_MODE_MONO )
2276 other_flags |= AF_LATIN_HINTS_MONO;
2277
2278 /*
2279 * In `light' hinting mode we disable horizontal hinting completely.
2280 * We also do it if the face is italic.
2281 *
2282 * However, if warping is enabled (which only works in `light' hinting
2283 * mode), advance widths get adjusted, too.
2284 */
2285 if ( mode == FT_RENDER_MODE_LIGHT ||
2286 ( face->style_flags & FT_STYLE_FLAG_ITALIC ) != 0 )
2287 scaler_flags |= AF_SCALER_FLAG_NO_HORIZONTAL;
2288
2289 #ifdef AF_CONFIG_OPTION_USE_WARPER
2290 /* get (global) warper flag */
2291 if ( !metrics->root.globals->module->warping )
2292 scaler_flags |= AF_SCALER_FLAG_NO_WARPER;
2293 #endif
2294
2295 hints->scaler_flags = scaler_flags;
2296 hints->other_flags = other_flags;
2297
2298 return FT_Err_Ok;
2299 }
2300
2301
2302 /*************************************************************************/
2303 /*************************************************************************/
2304 /***** *****/
2305 /***** L A T I N G L Y P H G R I D - F I T T I N G *****/
2306 /***** *****/
2307 /*************************************************************************/
2308 /*************************************************************************/
2309
2310 /* Snap a given width in scaled coordinates to one of the */
2311 /* current standard widths. */
2312
2313 static FT_Pos
2314 af_latin_snap_width( AF_Width widths,
2315 FT_UInt count,
2316 FT_Pos width )
2317 {
2318 FT_UInt n;
2319 FT_Pos best = 64 + 32 + 2;
2320 FT_Pos reference = width;
2321 FT_Pos scaled;
2322
2323
2324 for ( n = 0; n < count; n++ )
2325 {
2326 FT_Pos w;
2327 FT_Pos dist;
2328
2329
2330 w = widths[n].cur;
2331 dist = width - w;
2332 if ( dist < 0 )
2333 dist = -dist;
2334 if ( dist < best )
2335 {
2336 best = dist;
2337 reference = w;
2338 }
2339 }
2340
2341 scaled = FT_PIX_ROUND( reference );
2342
2343 if ( width >= reference )
2344 {
2345 if ( width < scaled + 48 )
2346 width = reference;
2347 }
2348 else
2349 {
2350 if ( width > scaled - 48 )
2351 width = reference;
2352 }
2353
2354 return width;
2355 }
2356
2357
2358 /* Compute the snapped width of a given stem, ignoring very thin ones. */
2359 /* There is a lot of voodoo in this function; changing the hard-coded */
2360 /* parameters influence the whole hinting process. */
2361
2362 static FT_Pos
2363 af_latin_compute_stem_width( AF_GlyphHints hints,
2364 AF_Dimension dim,
2365 FT_Pos width,
2366 FT_UInt base_flags,
2367 FT_UInt stem_flags )
2368 {
2369 AF_LatinMetrics metrics = (AF_LatinMetrics)hints->metrics;
2370 AF_LatinAxis axis = &metrics->axis[dim];
2371 FT_Pos dist = width;
2372 FT_Int sign = 0;
2373 FT_Int vertical = ( dim == AF_DIMENSION_VERT );
2374
2375
2376 if ( !AF_LATIN_HINTS_DO_STEM_ADJUST( hints ) ||
2377 axis->extra_light )
2378 return width;
2379
2380 if ( dist < 0 )
2381 {
2382 dist = -width;
2383 sign = 1;
2384 }
2385
2386 if ( ( vertical && !AF_LATIN_HINTS_DO_VERT_SNAP( hints ) ) ||
2387 ( !vertical && !AF_LATIN_HINTS_DO_HORZ_SNAP( hints ) ) )
2388 {
2389 /* smooth hinting process: very lightly quantize the stem width */
2390
2391 /* leave the widths of serifs alone */
2392 if ( ( stem_flags & AF_EDGE_SERIF ) &&
2393 vertical &&
2394 ( dist < 3 * 64 ) )
2395 goto Done_Width;
2396
2397 else if ( base_flags & AF_EDGE_ROUND )
2398 {
2399 if ( dist < 80 )
2400 dist = 64;
2401 }
2402 else if ( dist < 56 )
2403 dist = 56;
2404
2405 if ( axis->width_count > 0 )
2406 {
2407 FT_Pos delta;
2408
2409
2410 /* compare to standard width */
2411 delta = dist - axis->widths[0].cur;
2412
2413 if ( delta < 0 )
2414 delta = -delta;
2415
2416 if ( delta < 40 )
2417 {
2418 dist = axis->widths[0].cur;
2419 if ( dist < 48 )
2420 dist = 48;
2421
2422 goto Done_Width;
2423 }
2424
2425 if ( dist < 3 * 64 )
2426 {
2427 delta = dist & 63;
2428 dist &= -64;
2429
2430 if ( delta < 10 )
2431 dist += delta;
2432
2433 else if ( delta < 32 )
2434 dist += 10;
2435
2436 else if ( delta < 54 )
2437 dist += 54;
2438
2439 else
2440 dist += delta;
2441 }
2442 else
2443 dist = ( dist + 32 ) & ~63;
2444 }
2445 }
2446 else
2447 {
2448 /* strong hinting process: snap the stem width to integer pixels */
2449
2450 FT_Pos org_dist = dist;
2451
2452
2453 dist = af_latin_snap_width( axis->widths, axis->width_count, dist );
2454
2455 if ( vertical )
2456 {
2457 /* in the case of vertical hinting, always round */
2458 /* the stem heights to integer pixels */
2459
2460 if ( dist >= 64 )
2461 dist = ( dist + 16 ) & ~63;
2462 else
2463 dist = 64;
2464 }
2465 else
2466 {
2467 if ( AF_LATIN_HINTS_DO_MONO( hints ) )
2468 {
2469 /* monochrome horizontal hinting: snap widths to integer pixels */
2470 /* with a different threshold */
2471
2472 if ( dist < 64 )
2473 dist = 64;
2474 else
2475 dist = ( dist + 32 ) & ~63;
2476 }
2477 else
2478 {
2479 /* for horizontal anti-aliased hinting, we adopt a more subtle */
2480 /* approach: we strengthen small stems, round stems whose size */
2481 /* is between 1 and 2 pixels to an integer, otherwise nothing */
2482
2483 if ( dist < 48 )
2484 dist = ( dist + 64 ) >> 1;
2485
2486 else if ( dist < 128 )
2487 {
2488 /* We only round to an integer width if the corresponding */
2489 /* distortion is less than 1/4 pixel. Otherwise this */
2490 /* makes everything worse since the diagonals, which are */
2491 /* not hinted, appear a lot bolder or thinner than the */
2492 /* vertical stems. */
2493
2494 FT_Pos delta;
2495
2496
2497 dist = ( dist + 22 ) & ~63;
2498 delta = dist - org_dist;
2499 if ( delta < 0 )
2500 delta = -delta;
2501
2502 if ( delta >= 16 )
2503 {
2504 dist = org_dist;
2505 if ( dist < 48 )
2506 dist = ( dist + 64 ) >> 1;
2507 }
2508 }
2509 else
2510 /* round otherwise to prevent color fringes in LCD mode */
2511 dist = ( dist + 32 ) & ~63;
2512 }
2513 }
2514 }
2515
2516 Done_Width:
2517 if ( sign )
2518 dist = -dist;
2519
2520 return dist;
2521 }
2522
2523
2524 /* Align one stem edge relative to the previous stem edge. */
2525
2526 static void
2527 af_latin_align_linked_edge( AF_GlyphHints hints,
2528 AF_Dimension dim,
2529 AF_Edge base_edge,
2530 AF_Edge stem_edge )
2531 {
2532 FT_Pos dist = stem_edge->opos - base_edge->opos;
2533
2534 FT_Pos fitted_width = af_latin_compute_stem_width( hints, dim, dist,
2535 base_edge->flags,
2536 stem_edge->flags );
2537
2538
2539 stem_edge->pos = base_edge->pos + fitted_width;
2540
2541 FT_TRACE5(( " LINK: edge %d (opos=%.2f) linked to %.2f,"
2542 " dist was %.2f, now %.2f\n",
2543 stem_edge - hints->axis[dim].edges, stem_edge->opos / 64.0,
2544 stem_edge->pos / 64.0, dist / 64.0, fitted_width / 64.0 ));
2545 }
2546
2547
2548 /* Shift the coordinates of the `serif' edge by the same amount */
2549 /* as the corresponding `base' edge has been moved already. */
2550
2551 static void
2552 af_latin_align_serif_edge( AF_GlyphHints hints,
2553 AF_Edge base,
2554 AF_Edge serif )
2555 {
2556 FT_UNUSED( hints );
2557
2558 serif->pos = base->pos + ( serif->opos - base->opos );
2559 }
2560
2561
2562 /*************************************************************************/
2563 /*************************************************************************/
2564 /*************************************************************************/
2565 /**** ****/
2566 /**** E D G E H I N T I N G ****/
2567 /**** ****/
2568 /*************************************************************************/
2569 /*************************************************************************/
2570 /*************************************************************************/
2571
2572
2573 /* The main grid-fitting routine. */
2574
2575 static void
2576 af_latin_hint_edges( AF_GlyphHints hints,
2577 AF_Dimension dim )
2578 {
2579 AF_AxisHints axis = &hints->axis[dim];
2580 AF_Edge edges = axis->edges;
2581 AF_Edge edge_limit = edges + axis->num_edges;
2582 FT_PtrDist n_edges;
2583 AF_Edge edge;
2584 AF_Edge anchor = NULL;
2585 FT_Int has_serifs = 0;
2586
2587 AF_StyleClass style_class = hints->metrics->style_class;
2588 AF_ScriptClass script_class = AF_SCRIPT_CLASSES_GET
2589 [style_class->script];
2590
2591 FT_Bool top_to_bottom_hinting = 0;
2592
2593 #ifdef FT_DEBUG_LEVEL_TRACE
2594 FT_UInt num_actions = 0;
2595 #endif
2596
2597
2598 FT_TRACE5(( "latin %s edge hinting (style `%s')\n",
2599 dim == AF_DIMENSION_VERT ? "horizontal" : "vertical",
2600 af_style_names[hints->metrics->style_class->style] ));
2601
2602 if ( dim == AF_DIMENSION_VERT )
2603 top_to_bottom_hinting = script_class->top_to_bottom_hinting;
2604
2605 /* we begin by aligning all stems relative to the blue zone */
2606 /* if needed -- that's only for horizontal edges */
2607
2608 if ( dim == AF_DIMENSION_VERT && AF_HINTS_DO_BLUES( hints ) )
2609 {
2610 for ( edge = edges; edge < edge_limit; edge++ )
2611 {
2612 AF_Width blue;
2613 AF_Edge edge1, edge2; /* these edges form the stem to check */
2614
2615
2616 if ( edge->flags & AF_EDGE_DONE )
2617 continue;
2618
2619 edge1 = NULL;
2620 edge2 = edge->link;
2621
2622 /*
2623 * If a stem contains both a neutral and a non-neutral blue zone,
2624 * skip the neutral one. Otherwise, outlines with different
2625 * directions might be incorrectly aligned at the same vertical
2626 * position.
2627 *
2628 * If we have two neutral blue zones, skip one of them.
2629 *
2630 */
2631 if ( edge->blue_edge && edge2 && edge2->blue_edge )
2632 {
2633 FT_Byte neutral = edge->flags & AF_EDGE_NEUTRAL;
2634 FT_Byte neutral2 = edge2->flags & AF_EDGE_NEUTRAL;
2635
2636
2637 if ( neutral2 )
2638 {
2639 edge2->blue_edge = NULL;
2640 edge2->flags &= ~AF_EDGE_NEUTRAL;
2641 }
2642 else if ( neutral )
2643 {
2644 edge->blue_edge = NULL;
2645 edge->flags &= ~AF_EDGE_NEUTRAL;
2646 }
2647 }
2648
2649 blue = edge->blue_edge;
2650 if ( blue )
2651 edge1 = edge;
2652
2653 /* flip edges if the other edge is aligned to a blue zone */
2654 else if ( edge2 && edge2->blue_edge )
2655 {
2656 blue = edge2->blue_edge;
2657 edge1 = edge2;
2658 edge2 = edge;
2659 }
2660
2661 if ( !edge1 )
2662 continue;
2663
2664 #ifdef FT_DEBUG_LEVEL_TRACE
2665 if ( !anchor )
2666 FT_TRACE5(( " BLUE_ANCHOR: edge %d (opos=%.2f) snapped to %.2f,"
2667 " was %.2f (anchor=edge %d)\n",
2668 edge1 - edges, edge1->opos / 64.0, blue->fit / 64.0,
2669 edge1->pos / 64.0, edge - edges ));
2670 else
2671 FT_TRACE5(( " BLUE: edge %d (opos=%.2f) snapped to %.2f,"
2672 " was %.2f\n",
2673 edge1 - edges, edge1->opos / 64.0, blue->fit / 64.0,
2674 edge1->pos / 64.0 ));
2675
2676 num_actions++;
2677 #endif
2678
2679 edge1->pos = blue->fit;
2680 edge1->flags |= AF_EDGE_DONE;
2681
2682 if ( edge2 && !edge2->blue_edge )
2683 {
2684 af_latin_align_linked_edge( hints, dim, edge1, edge2 );
2685 edge2->flags |= AF_EDGE_DONE;
2686
2687 #ifdef FT_DEBUG_LEVEL_TRACE
2688 num_actions++;
2689 #endif
2690 }
2691
2692 if ( !anchor )
2693 anchor = edge;
2694 }
2695 }
2696
2697 /* now we align all other stem edges, trying to maintain the */
2698 /* relative order of stems in the glyph */
2699 for ( edge = edges; edge < edge_limit; edge++ )
2700 {
2701 AF_Edge edge2;
2702
2703
2704 if ( edge->flags & AF_EDGE_DONE )
2705 continue;
2706
2707 /* skip all non-stem edges */
2708 edge2 = edge->link;
2709 if ( !edge2 )
2710 {
2711 has_serifs++;
2712 continue;
2713 }
2714
2715 /* now align the stem */
2716
2717 /* this should not happen, but it's better to be safe */
2718 if ( edge2->blue_edge )
2719 {
2720 FT_TRACE5(( " ASSERTION FAILED for edge %d\n", edge2 - edges ));
2721
2722 af_latin_align_linked_edge( hints, dim, edge2, edge );
2723 edge->flags |= AF_EDGE_DONE;
2724
2725 #ifdef FT_DEBUG_LEVEL_TRACE
2726 num_actions++;
2727 #endif
2728 continue;
2729 }
2730
2731 if ( !anchor )
2732 {
2733 /* if we reach this if clause, no stem has been aligned yet */
2734
2735 FT_Pos org_len, org_center, cur_len;
2736 FT_Pos cur_pos1, error1, error2, u_off, d_off;
2737
2738
2739 org_len = edge2->opos - edge->opos;
2740 cur_len = af_latin_compute_stem_width( hints, dim, org_len,
2741 edge->flags,
2742 edge2->flags );
2743
2744 /* some voodoo to specially round edges for small stem widths; */
2745 /* the idea is to align the center of a stem, then shifting */
2746 /* the stem edges to suitable positions */
2747 if ( cur_len <= 64 )
2748 {
2749 /* width <= 1px */
2750 u_off = 32;
2751 d_off = 32;
2752 }
2753 else
2754 {
2755 /* 1px < width < 1.5px */
2756 u_off = 38;
2757 d_off = 26;
2758 }
2759
2760 if ( cur_len < 96 )
2761 {
2762 org_center = edge->opos + ( org_len >> 1 );
2763 cur_pos1 = FT_PIX_ROUND( org_center );
2764
2765 error1 = org_center - ( cur_pos1 - u_off );
2766 if ( error1 < 0 )
2767 error1 = -error1;
2768
2769 error2 = org_center - ( cur_pos1 + d_off );
2770 if ( error2 < 0 )
2771 error2 = -error2;
2772
2773 if ( error1 < error2 )
2774 cur_pos1 -= u_off;
2775 else
2776 cur_pos1 += d_off;
2777
2778 edge->pos = cur_pos1 - cur_len / 2;
2779 edge2->pos = edge->pos + cur_len;
2780 }
2781 else
2782 edge->pos = FT_PIX_ROUND( edge->opos );
2783
2784 anchor = edge;
2785 edge->flags |= AF_EDGE_DONE;
2786
2787 FT_TRACE5(( " ANCHOR: edge %d (opos=%.2f) and %d (opos=%.2f)"
2788 " snapped to %.2f and %.2f\n",
2789 edge - edges, edge->opos / 64.0,
2790 edge2 - edges, edge2->opos / 64.0,
2791 edge->pos / 64.0, edge2->pos / 64.0 ));
2792
2793 af_latin_align_linked_edge( hints, dim, edge, edge2 );
2794
2795 #ifdef FT_DEBUG_LEVEL_TRACE
2796 num_actions += 2;
2797 #endif
2798 }
2799 else
2800 {
2801 FT_Pos org_pos, org_len, org_center, cur_len;
2802 FT_Pos cur_pos1, cur_pos2, delta1, delta2;
2803
2804
2805 org_pos = anchor->pos + ( edge->opos - anchor->opos );
2806 org_len = edge2->opos - edge->opos;
2807 org_center = org_pos + ( org_len >> 1 );
2808
2809 cur_len = af_latin_compute_stem_width( hints, dim, org_len,
2810 edge->flags,
2811 edge2->flags );
2812
2813 if ( edge2->flags & AF_EDGE_DONE )
2814 {
2815 FT_TRACE5(( " ADJUST: edge %d (pos=%.2f) moved to %.2f\n",
2816 edge - edges, edge->pos / 64.0,
2817 ( edge2->pos - cur_len ) / 64.0 ));
2818
2819 edge->pos = edge2->pos - cur_len;
2820 }
2821
2822 else if ( cur_len < 96 )
2823 {
2824 FT_Pos u_off, d_off;
2825
2826
2827 cur_pos1 = FT_PIX_ROUND( org_center );
2828
2829 if ( cur_len <= 64 )
2830 {
2831 u_off = 32;
2832 d_off = 32;
2833 }
2834 else
2835 {
2836 u_off = 38;
2837 d_off = 26;
2838 }
2839
2840 delta1 = org_center - ( cur_pos1 - u_off );
2841 if ( delta1 < 0 )
2842 delta1 = -delta1;
2843
2844 delta2 = org_center - ( cur_pos1 + d_off );
2845 if ( delta2 < 0 )
2846 delta2 = -delta2;
2847
2848 if ( delta1 < delta2 )
2849 cur_pos1 -= u_off;
2850 else
2851 cur_pos1 += d_off;
2852
2853 edge->pos = cur_pos1 - cur_len / 2;
2854 edge2->pos = cur_pos1 + cur_len / 2;
2855
2856 FT_TRACE5(( " STEM: edge %d (opos=%.2f) linked to %d (opos=%.2f)"
2857 " snapped to %.2f and %.2f\n",
2858 edge - edges, edge->opos / 64.0,
2859 edge2 - edges, edge2->opos / 64.0,
2860 edge->pos / 64.0, edge2->pos / 64.0 ));
2861 }
2862
2863 else
2864 {
2865 org_pos = anchor->pos + ( edge->opos - anchor->opos );
2866 org_len = edge2->opos - edge->opos;
2867 org_center = org_pos + ( org_len >> 1 );
2868
2869 cur_len = af_latin_compute_stem_width( hints, dim, org_len,
2870 edge->flags,
2871 edge2->flags );
2872
2873 cur_pos1 = FT_PIX_ROUND( org_pos );
2874 delta1 = cur_pos1 + ( cur_len >> 1 ) - org_center;
2875 if ( delta1 < 0 )
2876 delta1 = -delta1;
2877
2878 cur_pos2 = FT_PIX_ROUND( org_pos + org_len ) - cur_len;
2879 delta2 = cur_pos2 + ( cur_len >> 1 ) - org_center;
2880 if ( delta2 < 0 )
2881 delta2 = -delta2;
2882
2883 edge->pos = ( delta1 < delta2 ) ? cur_pos1 : cur_pos2;
2884 edge2->pos = edge->pos + cur_len;
2885
2886 FT_TRACE5(( " STEM: edge %d (opos=%.2f) linked to %d (opos=%.2f)"
2887 " snapped to %.2f and %.2f\n",
2888 edge - edges, edge->opos / 64.0,
2889 edge2 - edges, edge2->opos / 64.0,
2890 edge->pos / 64.0, edge2->pos / 64.0 ));
2891 }
2892
2893 #ifdef FT_DEBUG_LEVEL_TRACE
2894 num_actions++;
2895 #endif
2896
2897 edge->flags |= AF_EDGE_DONE;
2898 edge2->flags |= AF_EDGE_DONE;
2899
2900 if ( edge > edges &&
2901 ( top_to_bottom_hinting ? ( edge->pos > edge[-1].pos )
2902 : ( edge->pos < edge[-1].pos ) ) )
2903 {
2904 #ifdef FT_DEBUG_LEVEL_TRACE
2905 FT_TRACE5(( " BOUND: edge %d (pos=%.2f) moved to %.2f\n",
2906 edge - edges, edge->pos / 64.0, edge[-1].pos / 64.0 ));
2907
2908 num_actions++;
2909 #endif
2910
2911 edge->pos = edge[-1].pos;
2912 }
2913 }
2914 }
2915
2916 /* make sure that lowercase m's maintain their symmetry */
2917
2918 /* In general, lowercase m's have six vertical edges if they are sans */
2919 /* serif, or twelve if they are with serifs. This implementation is */
2920 /* based on that assumption, and seems to work very well with most */
2921 /* faces. However, if for a certain face this assumption is not */
2922 /* true, the m is just rendered like before. In addition, any stem */
2923 /* correction will only be applied to symmetrical glyphs (even if the */
2924 /* glyph is not an m), so the potential for unwanted distortion is */
2925 /* relatively low. */
2926
2927 /* We don't handle horizontal edges since we can't easily assure that */
2928 /* the third (lowest) stem aligns with the base line; it might end up */
2929 /* one pixel higher or lower. */
2930
2931 n_edges = edge_limit - edges;
2932 if ( dim == AF_DIMENSION_HORZ && ( n_edges == 6 || n_edges == 12 ) )
2933 {
2934 AF_Edge edge1, edge2, edge3;
2935 FT_Pos dist1, dist2, span, delta;
2936
2937
2938 if ( n_edges == 6 )
2939 {
2940 edge1 = edges;
2941 edge2 = edges + 2;
2942 edge3 = edges + 4;
2943 }
2944 else
2945 {
2946 edge1 = edges + 1;
2947 edge2 = edges + 5;
2948 edge3 = edges + 9;
2949 }
2950
2951 dist1 = edge2->opos - edge1->opos;
2952 dist2 = edge3->opos - edge2->opos;
2953
2954 span = dist1 - dist2;
2955 if ( span < 0 )
2956 span = -span;
2957
2958 if ( span < 8 )
2959 {
2960 delta = edge3->pos - ( 2 * edge2->pos - edge1->pos );
2961 edge3->pos -= delta;
2962 if ( edge3->link )
2963 edge3->link->pos -= delta;
2964
2965 /* move the serifs along with the stem */
2966 if ( n_edges == 12 )
2967 {
2968 ( edges + 8 )->pos -= delta;
2969 ( edges + 11 )->pos -= delta;
2970 }
2971
2972 edge3->flags |= AF_EDGE_DONE;
2973 if ( edge3->link )
2974 edge3->link->flags |= AF_EDGE_DONE;
2975 }
2976 }
2977
2978 if ( has_serifs || !anchor )
2979 {
2980 /*
2981 * now hint the remaining edges (serifs and single) in order
2982 * to complete our processing
2983 */
2984 for ( edge = edges; edge < edge_limit; edge++ )
2985 {
2986 FT_Pos delta;
2987
2988
2989 if ( edge->flags & AF_EDGE_DONE )
2990 continue;
2991
2992 delta = 1000;
2993
2994 if ( edge->serif )
2995 {
2996 delta = edge->serif->opos - edge->opos;
2997 if ( delta < 0 )
2998 delta = -delta;
2999 }
3000
3001 if ( delta < 64 + 16 )
3002 {
3003 af_latin_align_serif_edge( hints, edge->serif, edge );
3004 FT_TRACE5(( " SERIF: edge %d (opos=%.2f) serif to %d (opos=%.2f)"
3005 " aligned to %.2f\n",
3006 edge - edges, edge->opos / 64.0,
3007 edge->serif - edges, edge->serif->opos / 64.0,
3008 edge->pos / 64.0 ));
3009 }
3010 else if ( !anchor )
3011 {
3012 edge->pos = FT_PIX_ROUND( edge->opos );
3013 anchor = edge;
3014 FT_TRACE5(( " SERIF_ANCHOR: edge %d (opos=%.2f)"
3015 " snapped to %.2f\n",
3016 edge-edges, edge->opos / 64.0, edge->pos / 64.0 ));
3017 }
3018 else
3019 {
3020 AF_Edge before, after;
3021
3022
3023 for ( before = edge - 1; before >= edges; before-- )
3024 if ( before->flags & AF_EDGE_DONE )
3025 break;
3026
3027 for ( after = edge + 1; after < edge_limit; after++ )
3028 if ( after->flags & AF_EDGE_DONE )
3029 break;
3030
3031 if ( before >= edges && before < edge &&
3032 after < edge_limit && after > edge )
3033 {
3034 if ( after->opos == before->opos )
3035 edge->pos = before->pos;
3036 else
3037 edge->pos = before->pos +
3038 FT_MulDiv( edge->opos - before->opos,
3039 after->pos - before->pos,
3040 after->opos - before->opos );
3041
3042 FT_TRACE5(( " SERIF_LINK1: edge %d (opos=%.2f) snapped to %.2f"
3043 " from %d (opos=%.2f)\n",
3044 edge - edges, edge->opos / 64.0,
3045 edge->pos / 64.0,
3046 before - edges, before->opos / 64.0 ));
3047 }
3048 else
3049 {
3050 edge->pos = anchor->pos +
3051 ( ( edge->opos - anchor->opos + 16 ) & ~31 );
3052 FT_TRACE5(( " SERIF_LINK2: edge %d (opos=%.2f)"
3053 " snapped to %.2f\n",
3054 edge - edges, edge->opos / 64.0, edge->pos / 64.0 ));
3055 }
3056 }
3057
3058 #ifdef FT_DEBUG_LEVEL_TRACE
3059 num_actions++;
3060 #endif
3061 edge->flags |= AF_EDGE_DONE;
3062
3063 if ( edge > edges &&
3064 ( top_to_bottom_hinting ? ( edge->pos > edge[-1].pos )
3065 : ( edge->pos < edge[-1].pos ) ) )
3066 {
3067 #ifdef FT_DEBUG_LEVEL_TRACE
3068 FT_TRACE5(( " BOUND: edge %d (pos=%.2f) moved to %.2f\n",
3069 edge - edges, edge->pos / 64.0, edge[-1].pos / 64.0 ));
3070
3071 num_actions++;
3072 #endif
3073 edge->pos = edge[-1].pos;
3074 }
3075
3076 if ( edge + 1 < edge_limit &&
3077 edge[1].flags & AF_EDGE_DONE &&
3078 ( top_to_bottom_hinting ? ( edge->pos < edge[1].pos )
3079 : ( edge->pos > edge[1].pos ) ) )
3080 {
3081 #ifdef FT_DEBUG_LEVEL_TRACE
3082 FT_TRACE5(( " BOUND: edge %d (pos=%.2f) moved to %.2f\n",
3083 edge - edges, edge->pos / 64.0, edge[1].pos / 64.0 ));
3084
3085 num_actions++;
3086 #endif
3087
3088 edge->pos = edge[1].pos;
3089 }
3090 }
3091 }
3092
3093 #ifdef FT_DEBUG_LEVEL_TRACE
3094 if ( !num_actions )
3095 FT_TRACE5(( " (none)\n" ));
3096 FT_TRACE5(( "\n" ));
3097 #endif
3098 }
3099
3100
3101 /* Apply the complete hinting algorithm to a latin glyph. */
3102
3103 static FT_Error
3104 af_latin_hints_apply( FT_UInt glyph_index,
3105 AF_GlyphHints hints,
3106 FT_Outline* outline,
3107 AF_LatinMetrics metrics )
3108 {
3109 FT_Error error;
3110 int dim;
3111
3112 AF_LatinAxis axis;
3113
3114
3115 error = af_glyph_hints_reload( hints, outline );
3116 if ( error )
3117 goto Exit;
3118
3119 /* analyze glyph outline */
3120 #ifdef AF_CONFIG_OPTION_USE_WARPER
3121 if ( ( metrics->root.scaler.render_mode == FT_RENDER_MODE_LIGHT &&
3122 AF_HINTS_DO_WARP( hints ) ) ||
3123 AF_HINTS_DO_HORIZONTAL( hints ) )
3124 #else
3125 if ( AF_HINTS_DO_HORIZONTAL( hints ) )
3126 #endif
3127 {
3128 axis = &metrics->axis[AF_DIMENSION_HORZ];
3129 error = af_latin_hints_detect_features( hints,
3130 axis->width_count,
3131 axis->widths,
3132 AF_DIMENSION_HORZ );
3133 if ( error )
3134 goto Exit;
3135 }
3136
3137 if ( AF_HINTS_DO_VERTICAL( hints ) )
3138 {
3139 axis = &metrics->axis[AF_DIMENSION_VERT];
3140 error = af_latin_hints_detect_features( hints,
3141 axis->width_count,
3142 axis->widths,
3143 AF_DIMENSION_VERT );
3144 if ( error )
3145 goto Exit;
3146
3147 /* apply blue zones to base characters only */
3148 if ( !( metrics->root.globals->glyph_styles[glyph_index] & AF_NONBASE ) )
3149 af_latin_hints_compute_blue_edges( hints, metrics );
3150 }
3151
3152 /* grid-fit the outline */
3153 for ( dim = 0; dim < AF_DIMENSION_MAX; dim++ )
3154 {
3155 #ifdef AF_CONFIG_OPTION_USE_WARPER
3156 if ( dim == AF_DIMENSION_HORZ &&
3157 metrics->root.scaler.render_mode == FT_RENDER_MODE_LIGHT &&
3158 AF_HINTS_DO_WARP( hints ) )
3159 {
3160 AF_WarperRec warper;
3161 FT_Fixed scale;
3162 FT_Pos delta;
3163
3164
3165 af_warper_compute( &warper, hints, (AF_Dimension)dim,
3166 &scale, &delta );
3167 af_glyph_hints_scale_dim( hints, (AF_Dimension)dim,
3168 scale, delta );
3169 continue;
3170 }
3171 #endif /* AF_CONFIG_OPTION_USE_WARPER */
3172
3173 if ( ( dim == AF_DIMENSION_HORZ && AF_HINTS_DO_HORIZONTAL( hints ) ) ||
3174 ( dim == AF_DIMENSION_VERT && AF_HINTS_DO_VERTICAL( hints ) ) )
3175 {
3176 af_latin_hint_edges( hints, (AF_Dimension)dim );
3177 af_glyph_hints_align_edge_points( hints, (AF_Dimension)dim );
3178 af_glyph_hints_align_strong_points( hints, (AF_Dimension)dim );
3179 af_glyph_hints_align_weak_points( hints, (AF_Dimension)dim );
3180 }
3181 }
3182
3183 af_glyph_hints_save( hints, outline );
3184
3185 Exit:
3186 return error;
3187 }
3188
3189
3190 /*************************************************************************/
3191 /*************************************************************************/
3192 /***** *****/
3193 /***** L A T I N S C R I P T C L A S S *****/
3194 /***** *****/
3195 /*************************************************************************/
3196 /*************************************************************************/
3197
3198
3199 AF_DEFINE_WRITING_SYSTEM_CLASS(
3200 af_latin_writing_system_class,
3201
3202 AF_WRITING_SYSTEM_LATIN,
3203
3204 sizeof ( AF_LatinMetricsRec ),
3205
3206 (AF_WritingSystem_InitMetricsFunc) af_latin_metrics_init,
3207 (AF_WritingSystem_ScaleMetricsFunc)af_latin_metrics_scale,
3208 (AF_WritingSystem_DoneMetricsFunc) NULL,
3209 (AF_WritingSystem_GetStdWidthsFunc)af_latin_get_standard_widths,
3210
3211 (AF_WritingSystem_InitHintsFunc) af_latin_hints_init,
3212 (AF_WritingSystem_ApplyHintsFunc) af_latin_hints_apply
3213 )
3214
3215
3216 /* END */