Synchronize with FreeType.
[ttfautohint.git] / lib / talatin.c
blob223f61fb8fea68e69b6b899853d5e22572a69cca
1 /* talatin.c */
3 /*
4 * Copyright (C) 2011-2015 by Werner Lemberg.
6 * This file is part of the ttfautohint library, and may only be used,
7 * modified, and distributed under the terms given in `COPYING'. By
8 * continuing to use, modify, or distribute this file you indicate that you
9 * have read `COPYING' and understand and accept it fully.
11 * The file `COPYING' mentioned in the previous paragraph is distributed
12 * with the ttfautohint library.
16 /* originally file `aflatin.c' (2011-Mar-28) from FreeType */
18 /* heavily modified 2011 by Werner Lemberg <wl@gnu.org> */
20 #include <string.h>
22 #include <ft2build.h>
23 #include FT_ADVANCES_H
24 #include FT_TRUETYPE_TABLES_H
26 #include "taglobal.h"
27 #include "talatin.h"
28 #include "tasort.h"
31 #ifdef TA_CONFIG_OPTION_USE_WARPER
32 #include "tawarp.h"
33 #endif
35 #include <numberset.h>
38 /* needed for computation of round vs. flat segments */
39 #define FLAT_THRESHOLD(x) (x / 14)
42 /* find segments and links, compute all stem widths, and initialize */
43 /* standard width and height for the glyph with given charcode */
45 void
46 ta_latin_metrics_init_widths(TA_LatinMetrics metrics,
47 FT_Face face,
48 FT_Bool use_cmap)
50 /* scan the array of segments in each direction */
51 TA_GlyphHintsRec hints[1];
54 TA_LOG_GLOBAL(("\n"
55 "latin standard widths computation (style `%s')\n"
56 "=====================================================\n"
57 "\n",
58 ta_style_names[metrics->root.style_class->style]));
60 ta_glyph_hints_init(hints);
62 metrics->axis[TA_DIMENSION_HORZ].width_count = 0;
63 metrics->axis[TA_DIMENSION_VERT].width_count = 0;
66 FT_Error error;
67 FT_ULong glyph_index;
68 FT_Long y_offset;
69 int dim;
70 TA_LatinMetricsRec dummy[1];
71 TA_Scaler scaler = &dummy->root.scaler;
73 TA_StyleClass style_class = metrics->root.style_class;
74 TA_ScriptClass script_class = ta_script_classes[style_class->script];
76 FT_UInt32 standard_char;
79 if (!use_cmap)
80 goto Exit;
83 * We check more than a single standard character to catch features
84 * like `c2sc' (small caps from caps) that don't contain lowercase
85 * letters by definition, or other features that mainly operate on
86 * numerals.
88 standard_char = script_class->standard_char1;
89 ta_get_char_index(&metrics->root,
90 standard_char,
91 &glyph_index,
92 &y_offset);
93 if (!glyph_index)
95 if (script_class->standard_char2)
97 standard_char = script_class->standard_char2;
98 ta_get_char_index(&metrics->root,
99 standard_char,
100 &glyph_index,
101 &y_offset);
102 if (!glyph_index)
104 if (script_class->standard_char3)
106 standard_char = script_class->standard_char3;
107 ta_get_char_index(&metrics->root,
108 standard_char,
109 &glyph_index,
110 &y_offset);
111 if (!glyph_index)
112 goto Exit;
114 else
115 goto Exit;
118 else
119 goto Exit;
122 TA_LOG_GLOBAL(("standard character: U+%04lX (glyph index %d)\n",
123 standard_char, glyph_index));
125 error = FT_Load_Glyph(face, glyph_index, FT_LOAD_NO_SCALE);
126 if (error || face->glyph->outline.n_points <= 0)
127 goto Exit;
129 memset(dummy, 0, sizeof (TA_LatinMetricsRec));
131 dummy->units_per_em = metrics->units_per_em;
133 scaler->x_scale = 0x10000L;
134 scaler->y_scale = 0x10000L;
135 scaler->x_delta = 0;
136 scaler->y_delta = 0;
138 scaler->face = face;
139 scaler->render_mode = FT_RENDER_MODE_NORMAL;
140 scaler->flags = 0;
142 ta_glyph_hints_rescale(hints, (TA_StyleMetrics)dummy);
144 error = ta_glyph_hints_reload(hints, &face->glyph->outline);
145 if (error)
146 goto Exit;
148 for (dim = 0; dim < TA_DIMENSION_MAX; dim++)
150 TA_LatinAxis axis = &metrics->axis[dim];
151 TA_AxisHints axhints = &hints->axis[dim];
153 TA_Segment seg, limit, link;
154 FT_UInt num_widths = 0;
157 error = ta_latin_hints_compute_segments(hints, (TA_Dimension)dim);
158 if (error)
159 goto Exit;
162 * We assume that the glyphs selected for the stem width
163 * computation are `featureless' enough so that the linking
164 * algorithm works fine without adjustments of its scoring
165 * function.
167 ta_latin_hints_link_segments(hints, 0, NULL, (TA_Dimension)dim);
169 seg = axhints->segments;
170 limit = seg + axhints->num_segments;
172 for (; seg < limit; seg++)
174 link = seg->link;
176 /* we only consider stem segments there! */
177 if (link
178 && link->link == seg
179 && link > seg)
181 FT_Pos dist;
184 dist = seg->pos - link->pos;
185 if (dist < 0)
186 dist = -dist;
188 if (num_widths < TA_LATIN_MAX_WIDTHS)
189 axis->widths[num_widths++].org = dist;
193 /* this also replaces multiple almost identical stem widths */
194 /* with a single one (the value 100 is heuristic) */
195 ta_sort_and_quantize_widths(&num_widths, axis->widths,
196 dummy->units_per_em / 100);
197 axis->width_count = num_widths;
200 Exit:
201 for (dim = 0; dim < TA_DIMENSION_MAX; dim++)
203 FONT* font = metrics->root.globals->font;
204 TA_LatinAxis axis = &metrics->axis[dim];
205 FT_Pos stdw;
208 if (!axis->width_count)
210 /* if we have no standard characters, */
211 /* use `fallback-stem-width', if available, */
212 /* or a default width (value 50 is heuristic) */
213 stdw = (dim == TA_DIMENSION_VERT && font->fallback_stem_width)
214 ? (FT_Pos)font->fallback_stem_width
215 : TA_LATIN_CONSTANT(metrics, 50);
217 /* set one width value if we do hinting */
218 if (style_class->style != TA_STYLE_NONE_DFLT)
220 axis->width_count++;
221 axis->widths[0].org = stdw;
225 stdw = axis->widths[0].org;
227 /* let's try 20% of the smallest width */
228 axis->edge_distance_threshold = stdw / 5;
229 axis->standard_width = stdw;
230 axis->extra_light = 0;
232 #ifdef TA_DEBUG
234 FT_UInt i;
237 TA_LOG_GLOBAL(("%s widths:\n",
238 dim == TA_DIMENSION_VERT ? "horizontal"
239 : "vertical"));
241 TA_LOG_GLOBAL((" %d (standard)", axis->standard_width));
242 for (i = 1; i < axis->width_count; i++)
243 TA_LOG_GLOBAL((" %d", axis->widths[i].org));
245 TA_LOG_GLOBAL(("\n"));
247 #endif
251 TA_LOG_GLOBAL(("\n"));
253 ta_glyph_hints_done(hints);
257 /* find all blue zones; flat segments give the reference points, */
258 /* round segments the overshoot positions */
260 static void
261 ta_latin_metrics_init_blues(TA_LatinMetrics metrics,
262 FT_Face face)
264 FT_Pos flats[TA_BLUE_STRING_MAX_LEN];
265 FT_Pos rounds[TA_BLUE_STRING_MAX_LEN];
266 FT_UInt num_flats;
267 FT_UInt num_rounds;
269 TA_LatinBlue blue;
270 FT_Error error;
271 TA_LatinAxis axis = &metrics->axis[TA_DIMENSION_VERT];
272 FT_Outline outline;
274 TA_StyleClass sc = metrics->root.style_class;
276 TA_Blue_Stringset bss = sc->blue_stringset;
277 const TA_Blue_StringRec* bs = &ta_blue_stringsets[bss];
279 FT_Pos flat_threshold = FLAT_THRESHOLD(metrics->units_per_em);
282 /* we walk over the blue character strings as specified in the */
283 /* style's entry in the `ta_blue_stringset' array */
285 TA_LOG_GLOBAL(("latin blue zones computation\n"
286 "============================\n"
287 "\n"));
289 for (; bs->string != TA_BLUE_STRING_MAX; bs++)
291 const char* p = &ta_blue_strings[bs->string];
292 FT_Pos* blue_ref;
293 FT_Pos* blue_shoot;
296 #ifdef TA_DEBUG
298 FT_Bool have_flag = 0;
301 TA_LOG_GLOBAL(("blue zone %d", axis->blue_count));
303 if (bs->properties)
305 TA_LOG_GLOBAL((" ("));
307 if (TA_LATIN_IS_TOP_BLUE(bs))
309 TA_LOG_GLOBAL(("top"));
310 have_flag = 1;
313 if (TA_LATIN_IS_NEUTRAL_BLUE(bs))
315 if (have_flag)
316 TA_LOG_GLOBAL((", "));
317 TA_LOG_GLOBAL(("neutral"));
318 have_flag = 1;
321 if (TA_LATIN_IS_X_HEIGHT_BLUE(bs))
323 if (have_flag)
324 TA_LOG_GLOBAL((", "));
325 TA_LOG_GLOBAL(("small top"));
326 have_flag = 1;
329 if (TA_LATIN_IS_LONG_BLUE(bs))
331 if (have_flag)
332 TA_LOG_GLOBAL((", "));
333 TA_LOG_GLOBAL(("long"));
336 TA_LOG_GLOBAL((")"));
339 TA_LOG_GLOBAL((":\n"));
341 #endif /* TA_DEBUG */
343 num_flats = 0;
344 num_rounds = 0;
346 while (*p)
348 FT_ULong ch;
349 FT_ULong glyph_index;
350 FT_Long y_offset;
351 FT_Pos best_y; /* same as points.y */
352 FT_Int best_point, best_contour_first, best_contour_last;
353 FT_Vector* points;
354 FT_Bool round = 0;
357 GET_UTF8_CHAR(ch, p);
359 /* load the character in the face -- skip unknown or empty ones */
360 ta_get_char_index(&metrics->root, ch, &glyph_index, &y_offset);
361 if (glyph_index == 0)
363 TA_LOG_GLOBAL((" U+%04lX unavailable\n", ch));
364 continue;
367 error = FT_Load_Glyph(face, glyph_index, FT_LOAD_NO_SCALE);
368 outline = face->glyph->outline;
369 /* reject glyphs that don't produce any rendering */
370 if (error || outline.n_points <= 2)
372 TA_LOG_GLOBAL((" U+%04lX contains no (usable) outlines\n", ch));
373 continue;
376 /* now compute min or max point indices and coordinates */
377 points = outline.points;
378 best_point = -1;
379 best_y = 0; /* make compiler happy */
380 best_contour_first = 0; /* ditto */
381 best_contour_last = 0; /* ditto */
384 FT_Int nn;
385 FT_Int first = 0;
386 FT_Int last = -1;
389 for (nn = 0; nn < outline.n_contours; first = last + 1, nn++)
391 FT_Int old_best_point = best_point;
392 FT_Int pp;
395 last = outline.contours[nn];
397 /* avoid single-point contours since they are never rasterized; */
398 /* in some fonts, they correspond to mark attachment points */
399 /* that are way outside of the glyph's real outline */
400 if (last <= first)
401 continue;
403 if (TA_LATIN_IS_TOP_BLUE(bs))
405 for (pp = first; pp <= last; pp++)
406 if (best_point < 0
407 || points[pp].y > best_y)
409 best_point = pp;
410 best_y = points[pp].y;
413 else
415 for (pp = first; pp <= last; pp++)
416 if (best_point < 0
417 || points[pp].y < best_y)
419 best_point = pp;
420 best_y = points[pp].y;
424 if (best_point != old_best_point)
426 best_contour_first = first;
427 best_contour_last = last;
432 /* now check whether the point belongs to a straight or round */
433 /* segment; we first need to find in which contour the extremum */
434 /* lies, then inspect its previous and next points */
435 if (best_point >= 0)
437 FT_Pos best_x = points[best_point].x;
438 FT_Int prev, next;
439 FT_Int best_segment_first, best_segment_last;
440 FT_Int best_on_point_first, best_on_point_last;
441 FT_Pos dist;
444 best_segment_first = best_point;
445 best_segment_last = best_point;
447 if (FT_CURVE_TAG(outline.tags[best_point]) == FT_CURVE_TAG_ON)
449 best_on_point_first = best_point;
450 best_on_point_last = best_point;
452 else
454 best_on_point_first = -1;
455 best_on_point_last = -1;
458 /* look for the previous and next points on the contour */
459 /* that are not on the same Y coordinate, then threshold */
460 /* the `closeness'... */
461 prev = best_point;
462 next = prev;
466 if (prev > best_contour_first)
467 prev--;
468 else
469 prev = best_contour_last;
471 dist = TA_ABS(points[prev].y - best_y);
472 /* accept a small distance or a small angle (both values are */
473 /* heuristic; value 20 corresponds to approx. 2.9 degrees) */
474 if (dist > 5)
475 if (TA_ABS(points[prev].x - best_x) <= 20 * dist)
476 break;
478 best_segment_first = prev;
480 if (FT_CURVE_TAG(outline.tags[prev]) == FT_CURVE_TAG_ON)
482 best_on_point_first = prev;
483 if (best_on_point_last < 0)
484 best_on_point_last = prev;
487 } while (prev != best_point);
491 if (next < best_contour_last)
492 next++;
493 else
494 next = best_contour_first;
496 dist = TA_ABS(points[next].y - best_y);
497 if (dist > 5)
498 if (TA_ABS(points[next].x - best_x) <= 20 * dist)
499 break;
501 best_segment_last = next;
503 if (FT_CURVE_TAG(outline.tags[next]) == FT_CURVE_TAG_ON)
505 best_on_point_last = next;
506 if (best_on_point_first < 0)
507 best_on_point_first = next;
510 } while (next != best_point);
512 if (TA_LATIN_IS_LONG_BLUE(bs))
514 /* If this flag is set, we have an additional constraint to */
515 /* get the blue zone distance: Find a segment of the topmost */
516 /* (or bottommost) contour that is longer than a heuristic */
517 /* threshold. This ensures that small bumps in the outline */
518 /* are ignored (for example, the `vertical serifs' found in */
519 /* many Hebrew glyph designs). */
521 /* If this segment is long enough, we are done. Otherwise, */
522 /* search the segment next to the extremum that is long */
523 /* enough, has the same direction, and a not too large */
524 /* vertical distance from the extremum. Note that the */
525 /* algorithm doesn't check whether the found segment is */
526 /* actually the one (vertically) nearest to the extremum. */
528 /* heuristic threshold value */
529 FT_Pos length_threshold = metrics->units_per_em / 25;
532 dist = TA_ABS(points[best_segment_last].x -
533 points[best_segment_first].x);
535 if (dist < length_threshold
536 && best_segment_last - best_segment_first + 2 <=
537 best_contour_last - best_contour_first)
539 /* heuristic threshold value */
540 FT_Pos height_threshold = metrics->units_per_em / 4;
542 FT_Int first;
543 FT_Int last;
544 FT_Bool hit;
546 /* we intentionally declare these two variables */
547 /* outside of the loop since various compilers emit */
548 /* incorrect warning messages otherwise, talking about */
549 /* `possibly uninitialized variables' */
550 FT_Int p_first = 0; /* make compiler happy */
551 FT_Int p_last = 0;
553 FT_Bool left2right;
556 /* compute direction */
557 prev = best_point;
561 if (prev > best_contour_first)
562 prev--;
563 else
564 prev = best_contour_last;
566 if (points[prev].x != best_x)
567 break;
568 } while (prev != best_point);
570 /* skip glyph for the degenerate case */
571 if (prev == best_point)
572 continue;
574 left2right = FT_BOOL(points[prev].x < points[best_point].x);
576 first = best_segment_last;
577 last = first;
578 hit = 0;
582 FT_Bool l2r;
583 FT_Pos d;
586 if (!hit)
588 /* no hit; adjust first point */
589 first = last;
591 /* also adjust first and last on point */
592 if (FT_CURVE_TAG(outline.tags[first]) == FT_CURVE_TAG_ON)
594 p_first = first;
595 p_last = first;
597 else
599 p_first = -1;
600 p_last = -1;
603 hit = 1;
606 if (last < best_contour_last)
607 last++;
608 else
609 last = best_contour_first;
611 if (TA_ABS(best_y - points[first].y) > height_threshold)
613 /* vertical distance too large */
614 hit = 0;
615 continue;
618 /* same test as above */
619 dist = TA_ABS(points[last].y - points[first].y);
620 if (dist > 5)
621 if (TA_ABS(points[last].x - points[first].x) <= 20 * dist)
623 hit = 0;
624 continue;
627 if (FT_CURVE_TAG(outline.tags[last]) == FT_CURVE_TAG_ON)
629 p_last = last;
630 if (p_first < 0)
631 p_first = last;
634 l2r = FT_BOOL(points[first].x < points[last].x);
635 d = TA_ABS(points[last].x - points[first].x);
637 if (l2r == left2right
638 && d >= length_threshold)
640 /* all constraints are met; update segment after finding */
641 /* its end */
644 if (last < best_contour_last)
645 last++;
646 else
647 last = best_contour_first;
649 d = TA_ABS(points[last].y - points[first].y);
650 if (d > 5)
651 if (TA_ABS(points[next].x - points[first].x) <=
652 20 * dist)
654 if (last > best_contour_first)
655 last--;
656 else
657 last = best_contour_last;
658 break;
661 p_last = last;
663 if (FT_CURVE_TAG(outline.tags[last]) == FT_CURVE_TAG_ON)
665 p_last = last;
666 if (p_first < 0)
667 p_first = last;
669 } while (last != best_segment_first);
671 best_y = points[first].y;
673 best_segment_first = first;
674 best_segment_last = last;
676 best_on_point_first = p_first;
677 best_on_point_last = p_last;
679 break;
681 } while (last != best_segment_first);
686 * for computing blue zones, we add the y offset as returned
687 * by the currently used OpenType feature --
688 * for example, superscript glyphs might be identical
689 * to subscript glyphs with a vertical shift
691 best_y += y_offset;
693 TA_LOG_GLOBAL((" U+%04lX: best_y = %5ld", ch, best_y));
696 * now set the `round' flag depending on the segment's kind:
698 * - if the horizontal distance between the first and last
699 * `on' point is larger than a heuristic threshold
700 * we have a flat segment
701 * - if either the first or the last point of the segment is
702 * an `off' point, the segment is round, otherwise it is
703 * flat
705 if (best_on_point_first >= 0
706 && best_on_point_last >= 0
707 && (TA_ABS(points[best_on_point_last].x
708 - points[best_on_point_first].x))
709 > flat_threshold)
710 round = 0;
711 else
712 round = FT_BOOL(FT_CURVE_TAG(outline.tags[best_segment_first])
713 != FT_CURVE_TAG_ON
714 || FT_CURVE_TAG(outline.tags[best_segment_last])
715 != FT_CURVE_TAG_ON);
717 if (round && TA_LATIN_IS_NEUTRAL_BLUE(bs))
719 /* only use flat segments for a neutral blue zone */
720 TA_LOG_GLOBAL((" (round, skipped)\n"));
721 continue;
724 TA_LOG_GLOBAL((" (%s)\n", round ? "round" : "flat"));
727 if (round)
728 rounds[num_rounds++] = best_y;
729 else
730 flats[num_flats++] = best_y;
733 if (num_flats == 0 && num_rounds == 0)
735 /* we couldn't find a single glyph to compute this blue zone, */
736 /* we will simply ignore it then */
737 TA_LOG_GLOBAL((" empty\n"));
738 continue;
741 /* we have computed the contents of the `rounds' and `flats' tables, */
742 /* now determine the reference and overshoot position of the blue -- */
743 /* we simply take the median value after a simple sort */
744 ta_sort_pos(num_rounds, rounds);
745 ta_sort_pos(num_flats, flats);
747 blue = &axis->blues[axis->blue_count];
748 blue_ref = &blue->ref.org;
749 blue_shoot = &blue->shoot.org;
751 axis->blue_count++;
753 if (num_flats == 0)
755 *blue_ref =
756 *blue_shoot = rounds[num_rounds / 2];
758 else if (num_rounds == 0)
760 *blue_ref =
761 *blue_shoot = flats[num_flats / 2];
763 else
765 *blue_ref = flats[num_flats / 2];
766 *blue_shoot = rounds[num_rounds / 2];
769 /* there are sometimes problems if the overshoot position of top */
770 /* zones is under its reference position, or the opposite for bottom */
771 /* zones; we must thus check everything there and correct the errors */
772 if (*blue_shoot != *blue_ref)
774 FT_Pos ref = *blue_ref;
775 FT_Pos shoot = *blue_shoot;
776 FT_Bool over_ref = FT_BOOL(shoot > ref);
779 if (TA_LATIN_IS_TOP_BLUE(bs) ^ over_ref)
781 *blue_ref =
782 *blue_shoot = (shoot + ref) / 2;
784 TA_LOG_GLOBAL((" [overshoot smaller than reference,"
785 " taking mean value]\n"));
789 blue->flags = 0;
790 if (TA_LATIN_IS_TOP_BLUE(bs))
791 blue->flags |= TA_LATIN_BLUE_TOP;
792 if (TA_LATIN_IS_NEUTRAL_BLUE(bs))
793 blue->flags |= TA_LATIN_BLUE_NEUTRAL;
795 /* the following flag is used later to adjust the y and x scales */
796 /* in order to optimize the pixel grid alignment */
797 /* of the top of small letters */
798 if (TA_LATIN_IS_X_HEIGHT_BLUE(bs))
799 blue->flags |= TA_LATIN_BLUE_ADJUSTMENT;
801 TA_LOG_GLOBAL((" -> reference = %ld\n"
802 " overshoot = %ld\n",
803 *blue_ref, *blue_shoot));
806 /* add two blue zones for usWinAscent and usWinDescent */
807 /* just in case the above algorithm has missed them -- */
808 /* Windows cuts off everything outside of those two values */
810 TT_OS2* os2;
813 os2 = (TT_OS2*)FT_Get_Sfnt_Table(face, ft_sfnt_os2);
815 if (os2)
817 blue = &axis->blues[axis->blue_count];
818 blue->flags = TA_LATIN_BLUE_TOP | TA_LATIN_BLUE_ACTIVE;
819 blue->ref.org =
820 blue->shoot.org = os2->usWinAscent;
822 TA_LOG_GLOBAL(("artificial blue zone for usWinAscent:\n"
823 " -> reference = %ld\n"
824 " overshoot = %ld\n",
825 blue->ref.org, blue->shoot.org));
827 blue = &axis->blues[axis->blue_count + 1];
828 blue->flags = TA_LATIN_BLUE_ACTIVE;
829 blue->ref.org =
830 blue->shoot.org = -os2->usWinDescent;
832 TA_LOG_GLOBAL(("artificial blue zone for usWinDescent:\n"
833 " -> reference = %ld\n"
834 " overshoot = %ld\n",
835 blue->ref.org, blue->shoot.org));
837 else
839 blue = &axis->blues[axis->blue_count];
840 blue->flags =
841 blue->ref.org =
842 blue->shoot.org = 0;
844 blue = &axis->blues[axis->blue_count + 1];
845 blue->flags =
846 blue->ref.org =
847 blue->shoot.org = 0;
851 TA_LOG_GLOBAL(("\n"));
853 return;
857 /* check whether all ASCII digits have the same advance width */
859 void
860 ta_latin_metrics_check_digits(TA_LatinMetrics metrics,
861 FT_Face face)
863 FT_UInt i;
864 FT_Bool started = 0, same_width = 1;
865 FT_Fixed advance, old_advance = 0;
868 /* digit `0' is 0x30 in all supported charmaps */
869 for (i = 0x30; i <= 0x39; i++)
871 FT_ULong glyph_index;
872 FT_Long y_offset;
875 ta_get_char_index(&metrics->root, i, &glyph_index, &y_offset);
876 if (glyph_index == 0)
877 continue;
879 if (FT_Get_Advance(face, glyph_index,
880 FT_LOAD_NO_SCALE
881 | FT_LOAD_NO_HINTING
882 | FT_LOAD_IGNORE_TRANSFORM,
883 &advance))
884 continue;
886 if (started)
888 if (advance != old_advance)
890 same_width = 0;
891 break;
894 else
896 old_advance = advance;
897 started = 1;
901 metrics->root.digits_have_same_width = same_width;
905 /* initialize global metrics */
907 FT_Error
908 ta_latin_metrics_init(TA_LatinMetrics metrics,
909 FT_Face face)
911 FT_CharMap oldmap = face->charmap;
914 metrics->units_per_em = face->units_per_EM;
916 if (!FT_Select_Charmap(face, FT_ENCODING_UNICODE))
918 ta_latin_metrics_init_widths(metrics, face, 1);
919 ta_latin_metrics_init_blues(metrics, face);
920 ta_latin_metrics_check_digits(metrics, face);
922 else
924 /* we only have a symbol font encoding */
925 ta_latin_metrics_init_widths(metrics, face, 0);
928 FT_Set_Charmap(face, oldmap);
929 return FT_Err_Ok;
933 /* adjust scaling value, then scale and shift widths */
934 /* and blue zones (if applicable) for given dimension */
936 static void
937 ta_latin_metrics_scale_dim(TA_LatinMetrics metrics,
938 TA_Scaler scaler,
939 TA_Dimension dim)
941 FT_Fixed scale;
942 FT_Pos delta;
943 TA_LatinAxis axis;
944 FT_UInt ppem;
945 FT_UInt nn;
948 ppem = metrics->root.scaler.face->size->metrics.x_ppem;
950 if (dim == TA_DIMENSION_HORZ)
952 scale = scaler->x_scale;
953 delta = scaler->x_delta;
955 else
957 scale = scaler->y_scale;
958 delta = scaler->y_delta;
961 axis = &metrics->axis[dim];
963 if (axis->org_scale == scale && axis->org_delta == delta)
964 return;
966 axis->org_scale = scale;
967 axis->org_delta = delta;
969 /* correct Y scale to optimize the alignment of the top of */
970 /* small letters to the pixel grid */
971 /* (if we do x-height snapping for this ppem value) */
972 if (!number_set_is_element(
973 metrics->root.globals->font->x_height_snapping_exceptions,
974 (int)ppem))
976 TA_LatinAxis Axis = &metrics->axis[TA_DIMENSION_VERT];
977 TA_LatinBlue blue = NULL;
980 for (nn = 0; nn < Axis->blue_count; nn++)
982 if (Axis->blues[nn].flags & TA_LATIN_BLUE_ADJUSTMENT)
984 blue = &Axis->blues[nn];
985 break;
989 if (blue)
991 FT_Pos scaled;
992 FT_Pos threshold;
993 FT_Pos fitted;
994 FT_UInt limit;
997 scaled = FT_MulFix(blue->shoot.org, scaler->y_scale);
998 limit = metrics->root.globals->increase_x_height;
999 threshold = 40;
1001 /* if the `increase-x-height' property is active, */
1002 /* we round up much more often */
1003 if (limit
1004 && ppem <= limit
1005 && ppem >= TA_PROP_INCREASE_X_HEIGHT_MIN)
1006 threshold = 52;
1008 fitted = (scaled + threshold) & ~63;
1010 if (scaled != fitted)
1012 if (dim == TA_DIMENSION_VERT)
1014 scale = FT_MulDiv(scale, fitted, scaled);
1016 TA_LOG_GLOBAL((
1017 "ta_latin_metrics_scale_dim:"
1018 " x height alignment (style `%s'):\n"
1020 " vertical scaling changed from %.4f to %.4f (by %d%%)\n"
1021 "\n",
1022 ta_style_names[metrics->root.style_class->style],
1023 axis->org_scale / 65536.0,
1024 scale / 65536.0,
1025 (fitted - scaled) * 100 / scaled));
1031 axis->scale = scale;
1032 axis->delta = delta;
1034 if (dim == TA_DIMENSION_HORZ)
1036 metrics->root.scaler.x_scale = scale;
1037 metrics->root.scaler.x_delta = delta;
1039 else
1041 metrics->root.scaler.y_scale = scale;
1042 metrics->root.scaler.y_delta = delta;
1045 TA_LOG_GLOBAL(("%s widths (style `%s')\n",
1046 dim == TA_DIMENSION_HORZ ? "horizontal" : "vertical",
1047 ta_style_names[metrics->root.style_class->style]));
1049 /* scale the widths */
1050 for (nn = 0; nn < axis->width_count; nn++)
1052 TA_Width width = axis->widths + nn;
1055 width->cur = FT_MulFix(width->org, scale);
1056 width->fit = width->cur;
1058 TA_LOG_GLOBAL((" %d scaled to %.2f\n",
1059 width->org,
1060 width->cur / 64.0));
1063 TA_LOG_GLOBAL(("\n"));
1065 /* an extra-light axis corresponds to a standard width that is */
1066 /* smaller than 5/8 pixels */
1067 axis->extra_light =
1068 (FT_Bool)(FT_MulFix(axis->standard_width, scale) < 32 + 8);
1070 #ifdef TA_DEBUG
1071 if (axis->extra_light)
1072 TA_LOG_GLOBAL(("`%s' style is extra light (at current resolution)\n"
1073 "\n",
1074 ta_style_names[metrics->root.style_class->style]));
1075 #endif
1077 if (dim == TA_DIMENSION_VERT)
1079 TA_LOG_GLOBAL(("blue zones (style `%s')\n",
1080 ta_style_names[metrics->root.style_class->style]));
1082 /* scale the blue zones */
1083 for (nn = 0; nn < axis->blue_count; nn++)
1085 TA_LatinBlue blue = &axis->blues[nn];
1086 FT_Pos dist;
1089 blue->ref.cur = FT_MulFix(blue->ref.org, scale) + delta;
1090 blue->ref.fit = blue->ref.cur;
1091 blue->shoot.cur = FT_MulFix(blue->shoot.org, scale) + delta;
1092 blue->shoot.fit = blue->shoot.cur;
1093 blue->flags &= ~TA_LATIN_BLUE_ACTIVE;
1095 /* a blue zone is only active if it is less than 3/4 pixels tall */
1096 dist = FT_MulFix(blue->ref.org - blue->shoot.org, scale);
1097 if (dist <= 48 && dist >= -48)
1099 #if 0
1100 FT_Pos delta1;
1101 #endif
1102 FT_Pos delta2;
1105 /* use discrete values for blue zone widths */
1107 #if 0
1108 /* generic, original code */
1109 delta1 = blue->shoot.org - blue->ref.org;
1110 delta2 = delta1;
1111 if (delta1 < 0)
1112 delta2 = -delta2;
1114 delta2 = FT_MulFix(delta2, scale);
1116 if (delta2 < 32)
1117 delta2 = 0;
1118 else if (delta2 < 64)
1119 delta2 = 32 + (((delta2 - 32) + 16) & ~31);
1120 else
1121 delta2 = TA_PIX_ROUND(delta2);
1123 if (delta1 < 0)
1124 delta2 = -delta2;
1126 blue->ref.fit = TA_PIX_ROUND(blue->ref.cur);
1127 blue->shoot.fit = blue->ref.fit + delta2;
1128 #else
1129 /* simplified version due to abs(dist) <= 48 */
1130 delta2 = dist;
1131 if (dist < 0)
1132 delta2 = -delta2;
1134 if (delta2 < 32)
1135 delta2 = 0;
1136 else if (delta2 < 48)
1137 delta2 = 32;
1138 else
1139 delta2 = 64;
1141 if (dist < 0)
1142 delta2 = -delta2;
1144 blue->ref.fit = TA_PIX_ROUND(blue->ref.cur);
1145 blue->shoot.fit = blue->ref.fit - delta2;
1146 #endif
1148 blue->flags |= TA_LATIN_BLUE_ACTIVE;
1150 TA_LOG_GLOBAL((" reference %d: %d scaled to %.2f%s\n"
1151 " overshoot %d: %d scaled to %.2f%s\n",
1153 blue->ref.org,
1154 blue->ref.fit / 64.0,
1155 blue->flags & TA_LATIN_BLUE_ACTIVE ? ""
1156 : " (inactive)",
1158 blue->shoot.org,
1159 blue->shoot.fit / 64.0,
1160 blue->flags & TA_LATIN_BLUE_ACTIVE ? ""
1161 : " (inactive)"));
1165 /* the last two artificial blue zones are to be scaled */
1166 /* with uncorrected scaling values */
1168 TA_LatinAxis a = &metrics->axis[TA_DIMENSION_VERT];
1169 TA_LatinBlue b;
1172 b = &a->blues[a->blue_count];
1173 b->ref.cur =
1174 b->ref.fit =
1175 b->shoot.cur =
1176 b->shoot.fit = FT_MulFix(b->ref.org, a->org_scale) + delta;
1178 TA_LOG_GLOBAL((" reference %d: %d scaled to %.2f (artificial)\n"
1179 " overshoot %d: %d scaled to %.2f (artificial)\n",
1180 a->blue_count,
1181 b->ref.org,
1182 b->ref.fit / 64.0,
1183 a->blue_count,
1184 b->shoot.org,
1185 b->shoot.fit / 64.0));
1187 b = &a->blues[a->blue_count + 1];
1188 b->ref.cur =
1189 b->ref.fit =
1190 b->shoot.cur =
1191 b->shoot.fit = FT_MulFix(b->ref.org, a->org_scale) + delta;
1193 TA_LOG_GLOBAL((" reference %d: %d scaled to %.2f (artificial)\n"
1194 " overshoot %d: %d scaled to %.2f (artificial)\n",
1195 a->blue_count + 1,
1196 b->ref.org,
1197 b->ref.fit / 64.0,
1198 a->blue_count + 1,
1199 b->shoot.org,
1200 b->shoot.fit / 64.0));
1203 TA_LOG_GLOBAL(("\n"));
1208 /* scale global values in both directions */
1210 void
1211 ta_latin_metrics_scale(TA_LatinMetrics metrics,
1212 TA_Scaler scaler)
1214 metrics->root.scaler.render_mode = scaler->render_mode;
1215 metrics->root.scaler.face = scaler->face;
1216 metrics->root.scaler.flags = scaler->flags;
1218 ta_latin_metrics_scale_dim(metrics, scaler, TA_DIMENSION_HORZ);
1219 ta_latin_metrics_scale_dim(metrics, scaler, TA_DIMENSION_VERT);
1223 /* walk over all contours and compute its segments */
1225 FT_Error
1226 ta_latin_hints_compute_segments(TA_GlyphHints hints,
1227 TA_Dimension dim)
1229 TA_LatinMetrics metrics = (TA_LatinMetrics)hints->metrics;
1230 TA_AxisHints axis = &hints->axis[dim];
1231 FT_Error error = FT_Err_Ok;
1233 TA_Segment segment = NULL;
1234 TA_SegmentRec seg0;
1236 TA_Point* contour = hints->contours;
1237 TA_Point* contour_limit = contour + hints->num_contours;
1238 TA_Direction major_dir, segment_dir;
1240 FT_Pos flat_threshold = FLAT_THRESHOLD(metrics->units_per_em);
1243 memset(&seg0, 0, sizeof (TA_SegmentRec));
1244 seg0.score = 32000;
1245 seg0.flags = TA_EDGE_NORMAL;
1247 major_dir = (TA_Direction)TA_ABS(axis->major_dir);
1248 segment_dir = major_dir;
1250 axis->num_segments = 0;
1252 /* set up (u,v) in each point */
1253 if (dim == TA_DIMENSION_HORZ)
1255 TA_Point point = hints->points;
1256 TA_Point limit = point + hints->num_points;
1259 for (; point < limit; point++)
1261 point->u = point->fx;
1262 point->v = point->fy;
1265 else
1267 TA_Point point = hints->points;
1268 TA_Point limit = point + hints->num_points;
1271 for (; point < limit; point++)
1273 point->u = point->fy;
1274 point->v = point->fx;
1278 /* do each contour separately */
1279 for (; contour < contour_limit; contour++)
1281 TA_Point point = contour[0];
1282 TA_Point last = point->prev;
1284 int on_edge = 0;
1286 FT_Pos min_pos = 32000; /* minimum segment pos != min_coord */
1287 FT_Pos max_pos = -32000; /* maximum segment pos != max_coord */
1288 FT_Pos min_on_pos = 32000;
1289 FT_Pos max_on_pos = -32000;
1290 FT_Bool passed;
1293 if (point == last) /* skip singletons -- just in case */
1294 continue;
1296 if (TA_ABS(last->out_dir) == major_dir
1297 && TA_ABS(point->out_dir) == major_dir)
1299 /* we are already on an edge, try to locate its start */
1300 last = point;
1302 for (;;)
1304 point = point->prev;
1305 if (TA_ABS(point->out_dir) != major_dir)
1307 point = point->next;
1308 break;
1310 if (point == last)
1311 break;
1315 last = point;
1316 passed = 0;
1318 for (;;)
1320 FT_Pos u, v;
1323 if (on_edge)
1325 u = point->u;
1326 if (u < min_pos)
1327 min_pos = u;
1328 if (u > max_pos)
1329 max_pos = u;
1331 /* get minimum and maximum coordinate of on points */
1332 if (!(point->flags & TA_FLAG_CONTROL))
1334 v = point->v;
1335 if (v < min_on_pos)
1336 min_on_pos = v;
1337 if (v > max_on_pos)
1338 max_on_pos = v;
1341 if (point->out_dir != segment_dir
1342 || point == last)
1344 /* we are just leaving an edge; record a new segment! */
1345 segment->last = point;
1346 segment->pos = (FT_Short)((min_pos + max_pos) >> 1);
1348 /* a segment is round if either its first or last point */
1349 /* is a control point, and the length of the on points */
1350 /* inbetween doesn't exceed a heuristic limit */
1351 if ((segment->first->flags | point->flags) & TA_FLAG_CONTROL
1352 && (max_on_pos - min_on_pos) < flat_threshold)
1353 segment->flags |= TA_EDGE_ROUND;
1355 /* compute segment size */
1356 min_pos = max_pos = point->v;
1358 v = segment->first->v;
1359 if (v < min_pos)
1360 min_pos = v;
1361 if (v > max_pos)
1362 max_pos = v;
1364 segment->min_coord = (FT_Short)min_pos;
1365 segment->max_coord = (FT_Short)max_pos;
1366 segment->height = (FT_Short)(segment->max_coord -
1367 segment->min_coord);
1369 on_edge = 0;
1370 segment = NULL;
1371 /* fall through */
1375 /* now exit if we are at the start/end point */
1376 if (point == last)
1378 if (passed)
1379 break;
1380 passed = 1;
1383 if (!on_edge
1384 && TA_ABS(point->out_dir) == major_dir)
1386 /* this is the start of a new segment! */
1387 segment_dir = (TA_Direction)point->out_dir;
1389 error = ta_axis_hints_new_segment(axis, &segment);
1390 if (error)
1391 goto Exit;
1393 /* clear all segment fields */
1394 segment[0] = seg0;
1396 segment->dir = (FT_Char)segment_dir;
1397 segment->first = point;
1398 segment->last = point;
1400 min_pos = max_pos = point->u;
1402 if (point->flags & TA_FLAG_CONTROL)
1404 min_on_pos = 32000;
1405 max_on_pos = -32000;
1407 else
1408 min_on_pos = max_on_pos = point->v;
1410 on_edge = 1;
1412 if (point->out_dir != point->next->in_dir)
1414 /* we have a one-point segment */
1415 segment->pos = (FT_Short)min_pos;
1417 if (point->flags & TA_FLAG_CONTROL)
1418 segment->flags |= TA_EDGE_ROUND;
1420 /* artificially extend the horizontal size if requested */
1421 segment->min_coord = (FT_Short)point->v + point->left_offset;
1422 segment->max_coord = (FT_Short)point->v + point->right_offset;
1423 segment->height = 0;
1425 on_edge = 0;
1426 segment = NULL;
1430 point = point->next;
1432 } /* contours */
1435 /* now slightly increase the height of segments if this makes sense -- */
1436 /* this is used to better detect and ignore serifs */
1438 TA_Segment segments = axis->segments;
1439 TA_Segment segments_end = segments + axis->num_segments;
1442 for (segment = segments; segment < segments_end; segment++)
1444 TA_Point first = segment->first;
1445 TA_Point last = segment->last;
1447 FT_Pos first_v = first->v;
1448 FT_Pos last_v = last->v;
1451 if (first_v < last_v)
1453 TA_Point p;
1456 p = first->prev;
1457 if (p->v < first_v)
1458 segment->height = (FT_Short)(segment->height +
1459 ((first_v - p->v) >> 1));
1461 p = last->next;
1462 if (p->v > last_v)
1463 segment->height = (FT_Short)(segment->height +
1464 ((p->v - last_v) >> 1));
1466 else
1468 TA_Point p;
1471 p = first->prev;
1472 if (p->v > first_v)
1473 segment->height = (FT_Short)(segment->height +
1474 ((p->v - first_v) >> 1));
1476 p = last->next;
1477 if (p->v < last_v)
1478 segment->height = (FT_Short)(segment->height +
1479 ((last_v - p->v) >> 1));
1484 Exit:
1485 return error;
1489 /* link segments to form stems and serifs; if `width_count' and */
1490 /* `widths' are non-zero, use them to fine-tune the scoring function */
1492 void
1493 ta_latin_hints_link_segments(TA_GlyphHints hints,
1494 FT_UInt width_count,
1495 TA_WidthRec* widths,
1496 TA_Dimension dim)
1498 TA_AxisHints axis = &hints->axis[dim];
1500 TA_Segment segments = axis->segments;
1501 TA_Segment segment_limit = segments + axis->num_segments;
1503 FT_Pos len_threshold, len_score, dist_score, max_width;
1504 TA_Segment seg1, seg2;
1507 if (width_count)
1508 max_width = widths[width_count - 1].org;
1509 else
1510 max_width = 0;
1512 /* a heuristic value to set up a minimum value for overlapping */
1513 len_threshold = TA_LATIN_CONSTANT(hints->metrics, 8);
1514 if (len_threshold == 0)
1515 len_threshold = 1;
1517 /* a heuristic value to weight lengths */
1518 len_score = TA_LATIN_CONSTANT(hints->metrics, 6000);
1520 /* a heuristic value to weight distances (no call to */
1521 /* TA_LATIN_CONSTANT needed, since we work on multiples */
1522 /* of the stem width) */
1523 dist_score = 3000;
1525 /* now compare each segment to the others */
1526 for (seg1 = segments; seg1 < segment_limit; seg1++)
1528 if (seg1->dir != axis->major_dir)
1529 continue;
1531 /* search for stems having opposite directions, */
1532 /* with seg1 to the `left' of seg2 */
1533 for (seg2 = segments; seg2 < segment_limit; seg2++)
1535 FT_Pos pos1 = seg1->pos;
1536 FT_Pos pos2 = seg2->pos;
1539 if (seg1->dir + seg2->dir == 0
1540 && pos2 > pos1)
1542 /* compute distance between the two segments */
1543 FT_Pos min = seg1->min_coord;
1544 FT_Pos max = seg1->max_coord;
1545 FT_Pos len;
1548 if (min < seg2->min_coord)
1549 min = seg2->min_coord;
1550 if (max > seg2->max_coord)
1551 max = seg2->max_coord;
1553 /* compute maximum coordinate difference of the two segments */
1554 /* (this is, how much they overlap) */
1555 len = max - min;
1557 /* for one-point segments, `len' is zero if there is an overlap */
1558 /* (and negative otherwise); we have to correct this */
1559 if (len == 0
1560 && (seg1->min_coord == seg1->max_coord
1561 || seg2->min_coord == seg2->max_coord))
1562 len = len_threshold;
1564 if (len >= len_threshold)
1567 * The score is the sum of two demerits indicating the
1568 * `badness' of a fit, measured along the segments' main axis
1569 * and orthogonal to it, respectively.
1571 * o The less overlapping along the main axis, the worse it
1572 * is, causing a larger demerit.
1574 * o The nearer the orthogonal distance to a stem width, the
1575 * better it is, causing a smaller demerit. For simplicity,
1576 * however, we only increase the demerit for values that
1577 * exceed the largest stem width.
1580 FT_Pos dist = pos2 - pos1;
1582 FT_Pos dist_demerit, score;
1585 if (max_width)
1587 /* distance demerits are based on multiples of `max_width'; */
1588 /* we scale by 1024 for getting more precision */
1589 FT_Pos delta = (dist << 10) / max_width - (1 << 10);
1592 if (delta > 10000)
1593 dist_demerit = 32000;
1594 else if (delta > 0)
1595 dist_demerit = delta * delta / dist_score;
1596 else
1597 dist_demerit = 0;
1599 else
1600 dist_demerit = dist; /* default if no widths available */
1602 score = dist_demerit + len_score / len;
1604 /* and we search for the smallest score */
1605 if (score < seg1->score)
1607 seg1->score = score;
1608 seg1->link = seg2;
1611 if (score < seg2->score)
1613 seg2->score = score;
1614 seg2->link = seg1;
1621 /* now compute the `serif' segments, cf. explanations in `tahints.h' */
1622 for (seg1 = segments; seg1 < segment_limit; seg1++)
1624 seg2 = seg1->link;
1626 if (seg2)
1628 if (seg2->link != seg1)
1630 seg1->link = 0;
1631 seg1->serif = seg2->link;
1638 /* link segments to edges, using feature analysis for selection */
1640 FT_Error
1641 ta_latin_hints_compute_edges(TA_GlyphHints hints,
1642 TA_Dimension dim)
1644 TA_AxisHints axis = &hints->axis[dim];
1645 FT_Error error = FT_Err_Ok;
1646 TA_LatinAxis laxis = &((TA_LatinMetrics)hints->metrics)->axis[dim];
1648 TA_Segment segments = axis->segments;
1649 TA_Segment segment_limit = segments + axis->num_segments;
1650 TA_Segment seg;
1652 #if 0
1653 TA_Direction up_dir;
1654 #endif
1655 FT_Fixed scale;
1656 FT_Pos edge_distance_threshold;
1657 FT_Pos segment_length_threshold;
1660 axis->num_edges = 0;
1662 scale = (dim == TA_DIMENSION_HORZ) ? hints->x_scale
1663 : hints->y_scale;
1665 #if 0
1666 up_dir = (dim == TA_DIMENSION_HORZ) ? TA_DIR_UP
1667 : TA_DIR_RIGHT;
1668 #endif
1670 /* we ignore all segments that are less than 1 pixel in length */
1671 /* to avoid many problems with serif fonts */
1672 /* (the corresponding threshold is computed in font units) */
1673 if (dim == TA_DIMENSION_HORZ)
1674 segment_length_threshold = FT_DivFix(64, hints->y_scale);
1675 else
1676 segment_length_threshold = 0;
1678 /********************************************************************/
1679 /* */
1680 /* We begin by generating a sorted table of edges for the current */
1681 /* direction. To do so, we simply scan each segment and try to find */
1682 /* an edge in our table that corresponds to its position. */
1683 /* */
1684 /* If no edge is found, we create and insert a new edge in the */
1685 /* sorted table. Otherwise, we simply add the segment to the edge's */
1686 /* list which gets processed in the second step to compute the */
1687 /* edge's properties. */
1688 /* */
1689 /* Note that the table of edges is sorted along the segment/edge */
1690 /* position. */
1691 /* */
1692 /********************************************************************/
1694 /* assure that edge distance threshold is at most 0.25px */
1695 edge_distance_threshold = FT_MulFix(laxis->edge_distance_threshold,
1696 scale);
1697 if (edge_distance_threshold > 64 / 4)
1698 edge_distance_threshold = 64 / 4;
1700 edge_distance_threshold = FT_DivFix(edge_distance_threshold,
1701 scale);
1703 for (seg = segments; seg < segment_limit; seg++)
1705 TA_Edge found = NULL;
1706 FT_Int ee;
1709 if (seg->height < segment_length_threshold)
1710 continue;
1712 /* a special case for serif edges: */
1713 /* if they are smaller than 1.5 pixels we ignore them */
1714 if (seg->serif
1715 && 2 * seg->height < 3 * segment_length_threshold)
1716 continue;
1718 /* look for an edge corresponding to the segment */
1719 for (ee = 0; ee < axis->num_edges; ee++)
1721 TA_Edge edge = axis->edges + ee;
1722 FT_Pos dist;
1725 dist = seg->pos - edge->fpos;
1726 if (dist < 0)
1727 dist = -dist;
1729 if (dist < edge_distance_threshold && edge->dir == seg->dir)
1731 found = edge;
1732 break;
1736 if (!found)
1738 TA_Edge edge;
1741 /* insert a new edge in the list and sort according to the position */
1742 error = ta_axis_hints_new_edge(axis, seg->pos,
1743 (TA_Direction)seg->dir,
1744 &edge);
1745 if (error)
1746 goto Exit;
1748 /* add the segment to the new edge's list */
1749 memset(edge, 0, sizeof (TA_EdgeRec));
1750 edge->first = seg;
1751 edge->last = seg;
1752 edge->dir = seg->dir;
1753 edge->fpos = seg->pos;
1754 edge->opos = FT_MulFix(seg->pos, scale);
1755 edge->pos = edge->opos;
1756 seg->edge_next = seg;
1758 else
1760 /* if an edge was found, simply add the segment to the edge's list */
1761 seg->edge_next = found->first;
1762 found->last->edge_next = seg;
1763 found->last = seg;
1767 /*****************************************************************/
1768 /* */
1769 /* Good, we now compute each edge's properties according to */
1770 /* the segments found on its position. Basically, these are */
1771 /* */
1772 /* - the edge's main direction */
1773 /* - stem edge, serif edge or both (which defaults to stem then) */
1774 /* - rounded edge, straight or both (which defaults to straight) */
1775 /* - link for edge */
1776 /* */
1777 /*****************************************************************/
1779 /* first of all, set the `edge' field in each segment -- this is */
1780 /* required in order to compute edge links */
1782 /* note that removing this loop and setting the `edge' field of each */
1783 /* segment directly in the code above slows down execution speed for */
1784 /* some reasons on platforms like the Sun */
1786 TA_Edge edges = axis->edges;
1787 TA_Edge edge_limit = edges + axis->num_edges;
1788 TA_Edge edge;
1791 for (edge = edges; edge < edge_limit; edge++)
1793 seg = edge->first;
1794 if (seg)
1797 seg->edge = edge;
1798 seg = seg->edge_next;
1799 } while (seg != edge->first);
1802 /* now compute each edge properties */
1803 for (edge = edges; edge < edge_limit; edge++)
1805 FT_Int is_round = 0; /* does it contain round segments? */
1806 FT_Int is_straight = 0; /* does it contain straight segments? */
1807 #if 0
1808 FT_Pos ups = 0; /* number of upwards segments */
1809 FT_Pos downs = 0; /* number of downwards segments */
1810 #endif
1813 seg = edge->first;
1817 FT_Bool is_serif;
1820 /* check for roundness of segment */
1821 if (seg->flags & TA_EDGE_ROUND)
1822 is_round++;
1823 else
1824 is_straight++;
1826 #if 0
1827 /* check for segment direction */
1828 if (seg->dir == up_dir)
1829 ups += seg->max_coord - seg->min_coord;
1830 else
1831 downs += seg->max_coord - seg->min_coord;
1832 #endif
1834 /* check for links -- */
1835 /* if seg->serif is set, then seg->link must be ignored */
1836 is_serif = (FT_Bool)(seg->serif
1837 && seg->serif->edge
1838 && seg->serif->edge != edge);
1840 if ((seg->link && seg->link->edge != NULL)
1841 || is_serif)
1843 TA_Edge edge2;
1844 TA_Segment seg2;
1847 edge2 = edge->link;
1848 seg2 = seg->link;
1850 if (is_serif)
1852 seg2 = seg->serif;
1853 edge2 = edge->serif;
1856 if (edge2)
1858 FT_Pos edge_delta;
1859 FT_Pos seg_delta;
1862 edge_delta = edge->fpos - edge2->fpos;
1863 if (edge_delta < 0)
1864 edge_delta = -edge_delta;
1866 seg_delta = seg->pos - seg2->pos;
1867 if (seg_delta < 0)
1868 seg_delta = -seg_delta;
1870 if (seg_delta < edge_delta)
1871 edge2 = seg2->edge;
1873 else
1874 edge2 = seg2->edge;
1876 if (is_serif)
1878 edge->serif = edge2;
1879 edge2->flags |= TA_EDGE_SERIF;
1881 else
1882 edge->link = edge2;
1885 seg = seg->edge_next;
1886 } while (seg != edge->first);
1888 /* set the round/straight flags */
1889 edge->flags = TA_EDGE_NORMAL;
1891 if (is_round > 0
1892 && is_round >= is_straight)
1893 edge->flags |= TA_EDGE_ROUND;
1895 #if 0
1896 /* set the edge's main direction */
1897 edge->dir = TA_DIR_NONE;
1899 if (ups > downs)
1900 edge->dir = (FT_Char)up_dir;
1902 else if (ups < downs)
1903 edge->dir = (FT_Char)-up_dir;
1905 else if (ups == downs)
1906 edge->dir = 0; /* both up and down! */
1907 #endif
1909 /* get rid of serifs if link is set */
1910 /* XXX: this gets rid of many unpleasant artefacts! */
1911 /* example: the `c' in cour.pfa at size 13 */
1913 if (edge->serif && edge->link)
1914 edge->serif = NULL;
1918 Exit:
1919 return error;
1923 /* detect segments and edges for given dimension */
1925 FT_Error
1926 ta_latin_hints_detect_features(TA_GlyphHints hints,
1927 FT_UInt width_count,
1928 TA_WidthRec* widths,
1929 TA_Dimension dim)
1931 FT_Error error;
1934 error = ta_latin_hints_compute_segments(hints, dim);
1935 if (!error)
1937 ta_latin_hints_link_segments(hints, width_count, widths, dim);
1939 error = ta_latin_hints_compute_edges(hints, dim);
1942 return error;
1946 /* compute all edges which lie within blue zones */
1948 static void
1949 ta_latin_hints_compute_blue_edges(TA_GlyphHints hints,
1950 TA_LatinMetrics metrics)
1952 TA_AxisHints axis = &hints->axis[TA_DIMENSION_VERT];
1954 TA_Edge edge = axis->edges;
1955 TA_Edge edge_limit = edge + axis->num_edges;
1957 TA_LatinAxis latin = &metrics->axis[TA_DIMENSION_VERT];
1958 FT_Fixed scale = latin->scale;
1961 /* compute which blue zones are active, */
1962 /* i.e. have their scaled size < 3/4 pixels */
1964 /* for each horizontal edge search the blue zone which is closest */
1965 for (; edge < edge_limit; edge++)
1967 FT_UInt bb;
1968 TA_Width best_blue = NULL;
1969 FT_Bool best_blue_is_neutral = 0;
1970 FT_Pos best_dist; /* initial threshold */
1972 FT_UInt best_blue_idx = 0;
1973 FT_Bool best_blue_is_shoot = 0;
1976 /* compute the initial threshold as a fraction of the EM size */
1977 /* (the value 40 is heuristic) */
1978 best_dist = FT_MulFix(metrics->units_per_em / 40, scale);
1980 /* assure a minimum distance of 0.5px */
1981 if (best_dist > 64 / 2)
1982 best_dist = 64 / 2;
1984 /* this loop also handles the two extra blue zones */
1985 /* for usWinAscent and usWinDescent */
1986 /* if option `windows-compatibility' is set */
1987 for (bb = 0;
1988 bb < latin->blue_count
1989 + (metrics->root.globals->font->windows_compatibility ? 2 : 0);
1990 bb++)
1992 TA_LatinBlue blue = latin->blues + bb;
1993 FT_Bool is_top_blue, is_neutral_blue, is_major_dir;
1996 /* skip inactive blue zones (i.e., those that are too large) */
1997 if (!(blue->flags & TA_LATIN_BLUE_ACTIVE))
1998 continue;
2000 /* if it is a top zone, check for right edges (against the major */
2001 /* direction); if it is a bottom zone, check for left edges (in */
2002 /* the major direction) */
2003 is_top_blue = (FT_Byte)((blue->flags & TA_LATIN_BLUE_TOP) != 0);
2004 is_neutral_blue = (FT_Byte)((blue->flags & TA_LATIN_BLUE_NEUTRAL) != 0);
2005 is_major_dir = FT_BOOL(edge->dir == axis->major_dir);
2007 /* neutral blue zones are handled for both directions */
2008 if (is_top_blue ^ is_major_dir || is_neutral_blue)
2010 FT_Pos dist;
2013 /* first of all, compare it to the reference position */
2014 dist = edge->fpos - blue->ref.org;
2015 if (dist < 0)
2016 dist = -dist;
2018 dist = FT_MulFix(dist, scale);
2019 if (dist < best_dist)
2021 best_dist = dist;
2022 best_blue = &blue->ref;
2023 best_blue_is_neutral = is_neutral_blue;
2025 best_blue_idx = bb;
2026 best_blue_is_shoot = 0;
2029 /* now compare it to the overshoot position and check whether */
2030 /* the edge is rounded, and whether the edge is over the */
2031 /* reference position of a top zone, or under the reference */
2032 /* position of a bottom zone (provided we don't have a */
2033 /* neutral blue zone) */
2034 if (edge->flags & TA_EDGE_ROUND
2035 && dist != 0
2036 && !is_neutral_blue)
2038 FT_Bool is_under_ref = FT_BOOL(edge->fpos < blue->ref.org);
2041 if (is_top_blue ^ is_under_ref)
2043 dist = edge->fpos - blue->shoot.org;
2044 if (dist < 0)
2045 dist = -dist;
2047 dist = FT_MulFix(dist, scale);
2048 if (dist < best_dist)
2050 best_dist = dist;
2051 best_blue = &blue->shoot;
2052 best_blue_is_neutral = is_neutral_blue;
2054 best_blue_idx = bb;
2055 best_blue_is_shoot = 1;
2062 if (best_blue)
2064 edge->blue_edge = best_blue;
2065 edge->best_blue_idx = best_blue_idx;
2066 edge->best_blue_is_shoot = best_blue_is_shoot;
2067 if (best_blue_is_neutral)
2068 edge->flags |= TA_EDGE_NEUTRAL;
2074 /* initalize hinting engine */
2076 static FT_Error
2077 ta_latin_hints_init(TA_GlyphHints hints,
2078 TA_LatinMetrics metrics)
2080 FT_Render_Mode mode;
2081 FT_UInt32 scaler_flags, other_flags;
2082 FT_Face face = metrics->root.scaler.face;
2085 ta_glyph_hints_rescale(hints, (TA_StyleMetrics)metrics);
2087 /* correct x_scale and y_scale if needed, since they may have */
2088 /* been modified by `ta_latin_metrics_scale_dim' above */
2089 hints->x_scale = metrics->axis[TA_DIMENSION_HORZ].scale;
2090 hints->x_delta = metrics->axis[TA_DIMENSION_HORZ].delta;
2091 hints->y_scale = metrics->axis[TA_DIMENSION_VERT].scale;
2092 hints->y_delta = metrics->axis[TA_DIMENSION_VERT].delta;
2094 /* compute flags depending on render mode, etc. */
2095 mode = metrics->root.scaler.render_mode;
2097 #if 0 /* #ifdef TA_CONFIG_OPTION_USE_WARPER */
2098 if (mode == FT_RENDER_MODE_LCD
2099 || mode == FT_RENDER_MODE_LCD_V)
2100 metrics->root.scaler.render_mode =
2101 mode = FT_RENDER_MODE_NORMAL;
2102 #endif
2104 scaler_flags = hints->scaler_flags;
2105 other_flags = 0;
2107 /* we snap the width of vertical stems for the monochrome */
2108 /* and horizontal LCD rendering targets only */
2109 if (mode == FT_RENDER_MODE_MONO
2110 || mode == FT_RENDER_MODE_LCD)
2111 other_flags |= TA_LATIN_HINTS_HORZ_SNAP;
2113 /* we snap the width of horizontal stems for the monochrome */
2114 /* and vertical LCD rendering targets only */
2115 if (mode == FT_RENDER_MODE_MONO
2116 || mode == FT_RENDER_MODE_LCD_V)
2117 other_flags |= TA_LATIN_HINTS_VERT_SNAP;
2119 /* we adjust stems to full pixels only if we don't use the `light' mode */
2120 if (mode != FT_RENDER_MODE_LIGHT)
2121 other_flags |= TA_LATIN_HINTS_STEM_ADJUST;
2123 if (mode == FT_RENDER_MODE_MONO)
2124 other_flags |= TA_LATIN_HINTS_MONO;
2126 /* in `light' hinting mode we disable horizontal hinting completely; */
2127 /* we also do it if the face is italic -- */
2128 /* however, if warping is enabled (which only works in `light' hinting */
2129 /* mode), advance widths get adjusted, too */
2130 if (mode == FT_RENDER_MODE_LIGHT
2131 || (face->style_flags & FT_STYLE_FLAG_ITALIC) != 0)
2132 scaler_flags |= TA_SCALER_FLAG_NO_HORIZONTAL;
2134 #ifdef TA_CONFIG_OPTION_USE_WARPER
2135 /* get (global) warper flag */
2136 if (!metrics->root.globals->module->warping)
2137 scaler_flags |= TA_SCALER_FLAG_NO_WARPER;
2138 #endif
2140 hints->scaler_flags = scaler_flags;
2141 hints->other_flags = other_flags;
2143 return FT_Err_Ok;
2147 /* snap a given width in scaled coordinates to */
2148 /* one of the current standard widths */
2150 static FT_Pos
2151 ta_latin_snap_width(TA_Width widths,
2152 FT_UInt count,
2153 FT_Pos width)
2155 FT_UInt n;
2156 FT_Pos best = 64 + 32 + 2;
2157 FT_Pos reference = width;
2158 FT_Pos scaled;
2161 for (n = 0; n < count; n++)
2163 FT_Pos w;
2164 FT_Pos dist;
2167 w = widths[n].cur;
2168 dist = width - w;
2169 if (dist < 0)
2170 dist = -dist;
2171 if (dist < best)
2173 best = dist;
2174 reference = w;
2178 scaled = TA_PIX_ROUND(reference);
2180 if (width >= reference)
2182 if (width < scaled + 48)
2183 width = reference;
2185 else
2187 if (width > scaled - 48)
2188 width = reference;
2191 return width;
2195 /* compute the snapped width of a given stem, ignoring very thin ones */
2197 /* there is a lot of voodoo in this function; changing the hard-coded */
2198 /* parameters influences the whole hinting process */
2200 static FT_Pos
2201 ta_latin_compute_stem_width(TA_GlyphHints hints,
2202 TA_Dimension dim,
2203 FT_Pos width,
2204 FT_Byte base_flags,
2205 FT_Byte stem_flags)
2207 TA_LatinMetrics metrics = (TA_LatinMetrics) hints->metrics;
2208 TA_LatinAxis axis = &metrics->axis[dim];
2210 FT_Pos dist = width;
2211 FT_Int sign = 0;
2212 FT_Int vertical = (dim == TA_DIMENSION_VERT);
2215 if (!TA_LATIN_HINTS_DO_STEM_ADJUST(hints)
2216 || axis->extra_light)
2217 return width;
2219 if (dist < 0)
2221 dist = -width;
2222 sign = 1;
2225 if ((vertical && !TA_LATIN_HINTS_DO_VERT_SNAP(hints))
2226 || (!vertical && !TA_LATIN_HINTS_DO_HORZ_SNAP(hints)))
2228 /* smooth hinting process: very lightly quantize the stem width */
2230 /* leave the widths of serifs alone */
2231 if ((stem_flags & TA_EDGE_SERIF)
2232 && vertical
2233 && (dist < 3 * 64))
2234 goto Done_Width;
2235 else if (base_flags & TA_EDGE_ROUND)
2237 if (dist < 80)
2238 dist = 64;
2240 else if (dist < 56)
2241 dist = 56;
2243 if (axis->width_count > 0)
2245 FT_Pos delta;
2248 /* compare to standard width */
2249 delta = dist - axis->widths[0].cur;
2251 if (delta < 0)
2252 delta = -delta;
2254 if (delta < 40)
2256 dist = axis->widths[0].cur;
2257 if (dist < 48)
2258 dist = 48;
2260 goto Done_Width;
2263 if (dist < 3 * 64)
2265 delta = dist & 63;
2266 dist &= -64;
2268 if (delta < 10)
2269 dist += delta;
2270 else if (delta < 32)
2271 dist += 10;
2272 else if (delta < 54)
2273 dist += 54;
2274 else
2275 dist += delta;
2277 else
2278 dist = (dist + 32) & ~63;
2281 else
2283 /* strong hinting process: snap the stem width to integer pixels */
2285 FT_Pos org_dist = dist;
2288 dist = ta_latin_snap_width(axis->widths, axis->width_count, dist);
2290 if (vertical)
2292 /* in the case of vertical hinting, */
2293 /* always round the stem heights to integer pixels */
2295 if (dist >= 64)
2296 dist = (dist + 16) & ~63;
2297 else
2298 dist = 64;
2300 else
2302 if (TA_LATIN_HINTS_DO_MONO(hints))
2304 /* monochrome horizontal hinting: */
2305 /* snap widths to integer pixels with a different threshold */
2307 if (dist < 64)
2308 dist = 64;
2309 else
2310 dist = (dist + 32) & ~63;
2312 else
2314 /* for horizontal anti-aliased hinting, we adopt a more subtle */
2315 /* approach: we strengthen small stems, round stems whose size */
2316 /* is between 1 and 2 pixels to an integer, otherwise nothing */
2318 if (dist < 48)
2319 dist = (dist + 64) >> 1;
2321 else if (dist < 128)
2323 /* we only round to an integer width if the corresponding */
2324 /* distortion is less than 1/4 pixel -- otherwise, this */
2325 /* makes everything worse since the diagonals, which are */
2326 /* not hinted, appear a lot bolder or thinner than the */
2327 /* vertical stems */
2329 FT_Pos delta;
2332 dist = (dist + 22) & ~63;
2333 delta = dist - org_dist;
2334 if (delta < 0)
2335 delta = -delta;
2337 if (delta >= 16)
2339 dist = org_dist;
2340 if (dist < 48)
2341 dist = (dist + 64) >> 1;
2344 else
2345 /* round otherwise to prevent color fringes in LCD mode */
2346 dist = (dist + 32) & ~63;
2351 Done_Width:
2352 if (sign)
2353 dist = -dist;
2355 return dist;
2359 /* align one stem edge relative to the previous stem edge */
2361 static void
2362 ta_latin_align_linked_edge(TA_GlyphHints hints,
2363 TA_Dimension dim,
2364 TA_Edge base_edge,
2365 TA_Edge stem_edge)
2367 FT_Pos dist = stem_edge->opos - base_edge->opos;
2369 FT_Pos fitted_width = ta_latin_compute_stem_width(
2370 hints, dim, dist,
2371 base_edge->flags,
2372 stem_edge->flags);
2375 stem_edge->pos = base_edge->pos + fitted_width;
2377 TA_LOG((" LINK: edge %d (opos=%.2f) linked to %.2f,"
2378 " dist was %.2f, now %.2f\n",
2379 stem_edge - hints->axis[dim].edges, stem_edge->opos / 64.0,
2380 stem_edge->pos / 64.0, dist / 64.0, fitted_width / 64.0));
2382 if (hints->recorder)
2383 hints->recorder(ta_link, hints, dim,
2384 base_edge, stem_edge, NULL, NULL, NULL);
2388 /* shift the coordinates of the `serif' edge by the same amount */
2389 /* as the corresponding `base' edge has been moved already */
2391 static void
2392 ta_latin_align_serif_edge(TA_GlyphHints hints,
2393 TA_Edge base,
2394 TA_Edge serif)
2396 FT_UNUSED(hints);
2398 serif->pos = base->pos + (serif->opos - base->opos);
2402 /* the main grid-fitting routine */
2404 static void
2405 ta_latin_hint_edges(TA_GlyphHints hints,
2406 TA_Dimension dim)
2408 TA_AxisHints axis = &hints->axis[dim];
2410 TA_Edge edges = axis->edges;
2411 TA_Edge edge_limit = edges + axis->num_edges;
2412 FT_PtrDist n_edges;
2413 TA_Edge edge;
2415 TA_Edge anchor = NULL;
2416 FT_Int has_serifs = 0;
2418 #ifdef TA_DEBUG
2419 FT_UInt num_actions = 0;
2420 #endif
2422 TA_LOG(("latin %s edge hinting (style `%s')\n",
2423 dim == TA_DIMENSION_VERT ? "horizontal" : "vertical",
2424 ta_style_names[hints->metrics->style_class->style]));
2426 /* we begin by aligning all stems relative to the blue zone if needed -- */
2427 /* that's only for horizontal edges */
2429 if (dim == TA_DIMENSION_VERT
2430 && TA_HINTS_DO_BLUES(hints))
2432 for (edge = edges; edge < edge_limit; edge++)
2434 TA_Width blue;
2435 TA_Edge edge1, edge2; /* these edges form the stem to check */
2438 if (edge->flags & TA_EDGE_DONE)
2439 continue;
2441 edge1 = NULL;
2442 edge2 = edge->link;
2445 * If a stem contains both a neutral and a non-neutral blue zone,
2446 * skip the neutral one. Otherwise, outlines with different
2447 * directions might be incorrectly aligned at the same vertical
2448 * position.
2450 * If we have two neutral blue zones, skip one of them.
2452 if (edge->blue_edge && edge2 && edge2->blue_edge)
2454 FT_Byte neutral = edge->flags & TA_EDGE_NEUTRAL;
2455 FT_Byte neutral2 = edge2->flags & TA_EDGE_NEUTRAL;
2458 if (neutral2)
2460 edge2->blue_edge = NULL;
2461 edge2->flags &= ~TA_EDGE_NEUTRAL;
2463 else if (neutral)
2465 edge->blue_edge = NULL;
2466 edge->flags &= ~TA_EDGE_NEUTRAL;
2470 blue = edge->blue_edge;
2471 if (blue)
2472 edge1 = edge;
2474 /* flip edges if the other edge is aligned to a blue zone */
2475 else if (edge2 && edge2->blue_edge)
2477 blue = edge2->blue_edge;
2478 edge1 = edge2;
2479 edge2 = edge;
2482 if (!edge1)
2483 continue;
2485 #ifdef TA_DEBUG
2486 if (!anchor)
2487 TA_LOG((" BLUE_ANCHOR: edge %d (opos=%.2f) snapped to %.2f,"
2488 " was %.2f (anchor=edge %d)\n",
2489 edge1 - edges, edge1->opos / 64.0, blue->fit / 64.0,
2490 edge1->pos / 64.0, edge - edges));
2491 else
2492 TA_LOG((" BLUE: edge %d (opos=%.2f) snapped to %.2f, was %.2f\n",
2493 edge1 - edges, edge1->opos / 64.0, blue->fit / 64.0,
2494 edge1->pos / 64.0));
2496 num_actions++;
2497 #endif
2499 edge1->pos = blue->fit;
2500 edge1->flags |= TA_EDGE_DONE;
2502 if (hints->recorder)
2504 if (!anchor)
2505 hints->recorder(ta_blue_anchor, hints, dim,
2506 edge1, edge, NULL, NULL, NULL);
2507 else
2508 hints->recorder(ta_blue, hints, dim,
2509 edge1, NULL, NULL, NULL, NULL);
2512 if (edge2 && !edge2->blue_edge)
2514 ta_latin_align_linked_edge(hints, dim, edge1, edge2);
2515 edge2->flags |= TA_EDGE_DONE;
2517 #ifdef TA_DEBUG
2518 num_actions++;
2519 #endif
2522 if (!anchor)
2523 anchor = edge;
2527 /* now we align all other stem edges, */
2528 /* trying to maintain the relative order of stems in the glyph */
2529 for (edge = edges; edge < edge_limit; edge++)
2531 TA_Edge edge2;
2534 if (edge->flags & TA_EDGE_DONE)
2535 continue;
2537 /* skip all non-stem edges */
2538 edge2 = edge->link;
2539 if (!edge2)
2541 has_serifs++;
2542 continue;
2545 /* now align the stem */
2547 /* this should not happen, but it's better to be safe */
2548 if (edge2->blue_edge)
2550 TA_LOG((" ASSERTION FAILED for edge %d\n", edge2 - edges));
2552 ta_latin_align_linked_edge(hints, dim, edge2, edge);
2553 edge->flags |= TA_EDGE_DONE;
2555 #ifdef TA_DEBUG
2556 num_actions++;
2557 #endif
2558 continue;
2561 if (!anchor)
2563 /* if we reach this if clause, no stem has been aligned yet */
2565 FT_Pos org_len, org_center, cur_len;
2566 FT_Pos cur_pos1, error1, error2, u_off, d_off;
2569 org_len = edge2->opos - edge->opos;
2570 cur_len = ta_latin_compute_stem_width(hints, dim, org_len,
2571 edge->flags, edge2->flags);
2573 /* some voodoo to specially round edges for small stem widths; */
2574 /* the idea is to align the center of a stem, */
2575 /* then shifting the stem edges to suitable positions */
2576 if (cur_len <= 64)
2578 /* width <= 1px */
2579 u_off = 32;
2580 d_off = 32;
2582 else
2584 /* 1px < width < 1.5px */
2585 u_off = 38;
2586 d_off = 26;
2589 if (cur_len < 96)
2591 org_center = edge->opos + (org_len >> 1);
2592 cur_pos1 = TA_PIX_ROUND(org_center);
2594 error1 = org_center - (cur_pos1 - u_off);
2595 if (error1 < 0)
2596 error1 = -error1;
2598 error2 = org_center - (cur_pos1 + d_off);
2599 if (error2 < 0)
2600 error2 = -error2;
2602 if (error1 < error2)
2603 cur_pos1 -= u_off;
2604 else
2605 cur_pos1 += d_off;
2607 edge->pos = cur_pos1 - cur_len / 2;
2608 edge2->pos = edge->pos + cur_len;
2610 else
2611 edge->pos = TA_PIX_ROUND(edge->opos);
2613 anchor = edge;
2614 edge->flags |= TA_EDGE_DONE;
2616 TA_LOG((" ANCHOR: edge %d (opos=%.2f) and %d (opos=%.2f)"
2617 " snapped to %.2f and %.2f\n",
2618 edge - edges, edge->opos / 64.0,
2619 edge2 - edges, edge2->opos / 64.0,
2620 edge->pos / 64.0, edge2->pos / 64.0));
2622 if (hints->recorder)
2623 hints->recorder(ta_anchor, hints, dim,
2624 edge, edge2, NULL, NULL, NULL);
2626 ta_latin_align_linked_edge(hints, dim, edge, edge2);
2628 #ifdef TA_DEBUG
2629 num_actions += 2;
2630 #endif
2632 else
2634 FT_Pos org_pos, org_len, org_center, cur_len;
2635 FT_Pos cur_pos1, cur_pos2, delta1, delta2;
2638 org_pos = anchor->pos + (edge->opos - anchor->opos);
2639 org_len = edge2->opos - edge->opos;
2640 org_center = org_pos + (org_len >> 1);
2642 cur_len = ta_latin_compute_stem_width(hints, dim, org_len,
2643 edge->flags, edge2->flags);
2645 if (edge2->flags & TA_EDGE_DONE)
2647 TA_LOG((" ADJUST: edge %d (pos=%.2f) moved to %.2f\n",
2648 edge - edges, edge->pos / 64.0,
2649 (edge2->pos - cur_len) / 64.0));
2651 edge->pos = edge2->pos - cur_len;
2653 if (hints->recorder)
2655 TA_Edge bound = NULL;
2658 if (edge > edges)
2659 bound = &edge[-1];
2661 hints->recorder(ta_adjust, hints, dim,
2662 edge, edge2, NULL, bound, NULL);
2666 else if (cur_len < 96)
2668 FT_Pos u_off, d_off;
2671 cur_pos1 = TA_PIX_ROUND(org_center);
2673 if (cur_len <= 64)
2675 u_off = 32;
2676 d_off = 32;
2678 else
2680 u_off = 38;
2681 d_off = 26;
2684 delta1 = org_center - (cur_pos1 - u_off);
2685 if (delta1 < 0)
2686 delta1 = -delta1;
2688 delta2 = org_center - (cur_pos1 + d_off);
2689 if (delta2 < 0)
2690 delta2 = -delta2;
2692 if (delta1 < delta2)
2693 cur_pos1 -= u_off;
2694 else
2695 cur_pos1 += d_off;
2697 edge->pos = cur_pos1 - cur_len / 2;
2698 edge2->pos = cur_pos1 + cur_len / 2;
2700 TA_LOG((" STEM: edge %d (opos=%.2f) linked to %d (opos=%.2f)"
2701 " snapped to %.2f and %.2f\n",
2702 edge - edges, edge->opos / 64.0,
2703 edge2 - edges, edge2->opos / 64.0,
2704 edge->pos / 64.0, edge2->pos / 64.0));
2706 if (hints->recorder)
2708 TA_Edge bound = NULL;
2711 if (edge > edges)
2712 bound = &edge[-1];
2714 hints->recorder(ta_stem, hints, dim,
2715 edge, edge2, NULL, bound, NULL);
2719 else
2721 org_pos = anchor->pos + (edge->opos - anchor->opos);
2722 org_len = edge2->opos - edge->opos;
2723 org_center = org_pos + (org_len >> 1);
2725 cur_len = ta_latin_compute_stem_width(hints, dim, org_len,
2726 edge->flags, edge2->flags);
2728 cur_pos1 = TA_PIX_ROUND(org_pos);
2729 delta1 = cur_pos1 + (cur_len >> 1) - org_center;
2730 if (delta1 < 0)
2731 delta1 = -delta1;
2733 cur_pos2 = TA_PIX_ROUND(org_pos + org_len) - cur_len;
2734 delta2 = cur_pos2 + (cur_len >> 1) - org_center;
2735 if (delta2 < 0)
2736 delta2 = -delta2;
2738 edge->pos = (delta1 < delta2) ? cur_pos1 : cur_pos2;
2739 edge2->pos = edge->pos + cur_len;
2741 TA_LOG((" STEM: edge %d (opos=%.2f) linked to %d (opos=%.2f)"
2742 " snapped to %.2f and %.2f\n",
2743 edge - edges, edge->opos / 64.0,
2744 edge2 - edges, edge2->opos / 64.0,
2745 edge->pos / 64.0, edge2->pos / 64.0));
2747 if (hints->recorder)
2749 TA_Edge bound = NULL;
2752 if (edge > edges)
2753 bound = &edge[-1];
2755 hints->recorder(ta_stem, hints, dim,
2756 edge, edge2, NULL, bound, NULL);
2760 #ifdef TA_DEBUG
2761 num_actions++;
2762 #endif
2764 edge->flags |= TA_EDGE_DONE;
2765 edge2->flags |= TA_EDGE_DONE;
2767 if (edge > edges
2768 && edge->pos < edge[-1].pos)
2770 #ifdef TA_DEBUG
2771 TA_LOG((" BOUND: edge %d (pos=%.2f) moved to %.2f\n",
2772 edge - edges, edge->pos / 64.0, edge[-1].pos / 64.0));
2774 num_actions++;
2775 #endif
2777 edge->pos = edge[-1].pos;
2779 if (hints->recorder)
2780 hints->recorder(ta_bound, hints, dim,
2781 edge, &edge[-1], NULL, NULL, NULL);
2786 /* make sure that lowercase m's maintain their symmetry */
2788 /* In general, lowercase m's have six vertical edges if they are sans */
2789 /* serif, or twelve if they are with serifs. This implementation is */
2790 /* based on that assumption, and seems to work very well with most */
2791 /* faces. However, if for a certain face this assumption is not */
2792 /* true, the m is just rendered like before. In addition, any stem */
2793 /* correction will only be applied to symmetrical glyphs (even if the */
2794 /* glyph is not an m), so the potential for unwanted distortion is */
2795 /* relatively low. */
2797 /* we don't handle horizontal edges since we can't easily assure that */
2798 /* the third (lowest) stem aligns with the base line; it might end up */
2799 /* one pixel higher or lower */
2801 n_edges = edge_limit - edges;
2802 if (dim == TA_DIMENSION_HORZ
2803 && (n_edges == 6 || n_edges == 12))
2805 TA_Edge edge1, edge2, edge3;
2806 FT_Pos dist1, dist2, span, delta;
2809 if (n_edges == 6)
2811 edge1 = edges;
2812 edge2 = edges + 2;
2813 edge3 = edges + 4;
2815 else
2817 edge1 = edges + 1;
2818 edge2 = edges + 5;
2819 edge3 = edges + 9;
2822 dist1 = edge2->opos - edge1->opos;
2823 dist2 = edge3->opos - edge2->opos;
2825 span = dist1 - dist2;
2826 if (span < 0)
2827 span = -span;
2829 if (span < 8)
2831 delta = edge3->pos - (2 * edge2->pos - edge1->pos);
2832 edge3->pos -= delta;
2833 if (edge3->link)
2834 edge3->link->pos -= delta;
2836 /* move the serifs along with the stem */
2837 if (n_edges == 12)
2839 (edges + 8)->pos -= delta;
2840 (edges + 11)->pos -= delta;
2843 edge3->flags |= TA_EDGE_DONE;
2844 if (edge3->link)
2845 edge3->link->flags |= TA_EDGE_DONE;
2849 if (has_serifs || !anchor)
2851 /* now hint the remaining edges (serifs and single) */
2852 /* in order to complete our processing */
2853 for (edge = edges; edge < edge_limit; edge++)
2855 TA_Edge lower_bound = NULL;
2856 TA_Edge upper_bound = NULL;
2858 FT_Pos delta;
2861 if (edge->flags & TA_EDGE_DONE)
2862 continue;
2864 delta = 1000;
2866 if (edge->serif)
2868 delta = edge->serif->opos - edge->opos;
2869 if (delta < 0)
2870 delta = -delta;
2873 if (edge > edges)
2874 lower_bound = &edge[-1];
2876 if (edge + 1 < edge_limit
2877 && edge[1].flags & TA_EDGE_DONE)
2878 upper_bound = &edge[1];
2881 if (delta < 64 + 16)
2883 ta_latin_align_serif_edge(hints, edge->serif, edge);
2885 TA_LOG((" SERIF: edge %d (opos=%.2f) serif to %d (opos=%.2f)"
2886 " aligned to %.2f\n",
2887 edge - edges, edge->opos / 64.0,
2888 edge->serif - edges, edge->serif->opos / 64.0,
2889 edge->pos / 64.0));
2891 if (hints->recorder)
2892 hints->recorder(ta_serif, hints, dim,
2893 edge, NULL, NULL, lower_bound, upper_bound);
2895 else if (!anchor)
2897 edge->pos = TA_PIX_ROUND(edge->opos);
2898 anchor = edge;
2900 TA_LOG((" SERIF_ANCHOR: edge %d (opos=%.2f) snapped to %.2f\n",
2901 edge - edges, edge->opos / 64.0, edge->pos / 64.0));
2903 if (hints->recorder)
2904 hints->recorder(ta_serif_anchor, hints, dim,
2905 edge, NULL, NULL, lower_bound, upper_bound);
2907 else
2909 TA_Edge before, after;
2912 for (before = edge - 1; before >= edges; before--)
2913 if (before->flags & TA_EDGE_DONE)
2914 break;
2916 for (after = edge + 1; after < edge_limit; after++)
2917 if (after->flags & TA_EDGE_DONE)
2918 break;
2920 if (before >= edges && before < edge
2921 && after < edge_limit && after > edge)
2923 if (after->opos == before->opos)
2924 edge->pos = before->pos;
2925 else
2926 edge->pos = before->pos + FT_MulDiv(edge->opos - before->opos,
2927 after->pos - before->pos,
2928 after->opos - before->opos);
2930 TA_LOG((" SERIF_LINK1: edge %d (opos=%.2f) snapped to %.2f"
2931 " from %d (opos=%.2f)\n",
2932 edge - edges, edge->opos / 64.0,
2933 edge->pos / 64.0,
2934 before - edges, before->opos / 64.0));
2936 if (hints->recorder)
2937 hints->recorder(ta_serif_link1, hints, dim,
2938 edge, before, after, lower_bound, upper_bound);
2940 else
2942 edge->pos = anchor->pos + ((edge->opos - anchor->opos + 16) & ~31);
2943 TA_LOG((" SERIF_LINK2: edge %d (opos=%.2f) snapped to %.2f\n",
2944 edge - edges, edge->opos / 64.0, edge->pos / 64.0));
2946 if (hints->recorder)
2947 hints->recorder(ta_serif_link2, hints, dim,
2948 edge, NULL, NULL, lower_bound, upper_bound);
2952 #ifdef TA_DEBUG
2953 num_actions++;
2954 #endif
2955 edge->flags |= TA_EDGE_DONE;
2957 if (edge > edges
2958 && edge->pos < edge[-1].pos)
2960 #ifdef TA_DEBUG
2961 TA_LOG((" BOUND: edge %d (pos=%.2f) moved to %.2f\n",
2962 edge - edges, edge->pos / 64.0, edge[-1].pos / 64.0));
2963 num_actions++;
2964 #endif
2966 edge->pos = edge[-1].pos;
2968 if (hints->recorder)
2969 hints->recorder(ta_bound, hints, dim,
2970 edge, &edge[-1], NULL, NULL, NULL);
2973 if (edge + 1 < edge_limit
2974 && edge[1].flags & TA_EDGE_DONE
2975 && edge->pos > edge[1].pos)
2977 #ifdef TA_DEBUG
2978 TA_LOG((" BOUND: edge %d (pos=%.2f) moved to %.2f\n",
2979 edge - edges, edge->pos / 64.0, edge[1].pos / 64.0));
2981 num_actions++;
2982 #endif
2984 edge->pos = edge[1].pos;
2986 if (hints->recorder)
2987 hints->recorder(ta_bound, hints, dim,
2988 edge, &edge[1], NULL, NULL, NULL);
2993 #ifdef TA_DEBUG
2994 if (!num_actions)
2995 TA_LOG((" (none)\n"));
2996 TA_LOG(("\n"));
2997 #endif
3001 /* apply the complete hinting algorithm to a latin glyph */
3003 static FT_Error
3004 ta_latin_hints_apply(TA_GlyphHints hints,
3005 FT_Outline* outline,
3006 TA_LatinMetrics metrics)
3008 FT_Error error;
3009 int dim;
3011 TA_LatinAxis axis;
3014 error = ta_glyph_hints_reload(hints, outline);
3015 if (error)
3016 goto Exit;
3018 /* analyze glyph outline */
3019 #ifdef TA_CONFIG_OPTION_USE_WARPER
3020 if ((metrics->root.scaler.render_mode == FT_RENDER_MODE_LIGHT
3021 && TA_HINTS_DO_WARP(hints))
3022 || TA_HINTS_DO_HORIZONTAL(hints))
3023 #else
3024 if (TA_HINTS_DO_HORIZONTAL(hints))
3025 #endif
3027 axis = &metrics->axis[TA_DIMENSION_HORZ];
3028 error = ta_latin_hints_detect_features(hints,
3029 axis->width_count,
3030 axis->widths,
3031 TA_DIMENSION_HORZ);
3032 if (error)
3033 goto Exit;
3036 if (TA_HINTS_DO_VERTICAL(hints))
3038 axis = &metrics->axis[TA_DIMENSION_VERT];
3039 error = ta_latin_hints_detect_features(hints,
3040 axis->width_count,
3041 axis->widths,
3042 TA_DIMENSION_VERT);
3043 if (error)
3044 goto Exit;
3046 ta_latin_hints_compute_blue_edges(hints, metrics);
3049 /* grid-fit the outline */
3050 for (dim = 0; dim < TA_DIMENSION_MAX; dim++)
3052 #ifdef TA_CONFIG_OPTION_USE_WARPER
3053 if (dim == TA_DIMENSION_HORZ
3054 && metrics->root.scaler.render_mode == FT_RENDER_MODE_LIGHT
3055 && TA_HINTS_DO_WARP(hints))
3057 TA_WarperRec warper;
3058 FT_Fixed scale;
3059 FT_Pos delta;
3062 ta_warper_compute(&warper, hints, (TA_Dimension)dim, &scale, &delta);
3063 ta_glyph_hints_scale_dim(hints, (TA_Dimension)dim, scale, delta);
3065 continue;
3067 #endif /* TA_CONFIG_OPTION_USE_WARPER */
3069 if ((dim == TA_DIMENSION_HORZ && TA_HINTS_DO_HORIZONTAL(hints))
3070 || (dim == TA_DIMENSION_VERT && TA_HINTS_DO_VERTICAL(hints)))
3072 ta_latin_hint_edges(hints, (TA_Dimension)dim);
3073 ta_glyph_hints_align_edge_points(hints, (TA_Dimension)dim);
3074 ta_glyph_hints_align_strong_points(hints, (TA_Dimension)dim);
3075 ta_glyph_hints_align_weak_points(hints, (TA_Dimension)dim);
3079 ta_glyph_hints_save(hints, outline);
3081 Exit:
3082 return error;
3086 const TA_WritingSystemClassRec ta_latin_writing_system_class =
3088 TA_WRITING_SYSTEM_LATIN,
3090 sizeof (TA_LatinMetricsRec),
3092 (TA_WritingSystem_InitMetricsFunc)ta_latin_metrics_init,
3093 (TA_WritingSystem_ScaleMetricsFunc)ta_latin_metrics_scale,
3094 (TA_WritingSystem_DoneMetricsFunc)NULL,
3096 (TA_WritingSystem_InitHintsFunc)ta_latin_hints_init,
3097 (TA_WritingSystem_ApplyHintsFunc)ta_latin_hints_apply
3100 /* end of talatin.c */