Update sds library to commit 14b8e8a1.
[ttfautohint.git] / lib / talatin.c
blob63c7fb35ae2a6f51e7f9089b4d36161ac9d2a78f
1 /* talatin.c */
3 /*
4 * Copyright (C) 2011-2014 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 /* find segments and links, compute all stem widths, and initialize */
39 /* standard width and height for the glyph with given charcode */
41 void
42 ta_latin_metrics_init_widths(TA_LatinMetrics metrics,
43 FT_Face face,
44 FT_Bool use_cmap)
46 /* scan the array of segments in each direction */
47 TA_GlyphHintsRec hints[1];
50 TA_LOG_GLOBAL(("\n"
51 "latin standard widths computation (style `%s')\n"
52 "=====================================================\n"
53 "\n",
54 ta_style_names[metrics->root.style_class->style]));
56 ta_glyph_hints_init(hints);
58 metrics->axis[TA_DIMENSION_HORZ].width_count = 0;
59 metrics->axis[TA_DIMENSION_VERT].width_count = 0;
62 FT_Error error;
63 FT_ULong glyph_index;
64 FT_Long y_offset;
65 int dim;
66 TA_LatinMetricsRec dummy[1];
67 TA_Scaler scaler = &dummy->root.scaler;
69 TA_StyleClass style_class = metrics->root.style_class;
70 TA_ScriptClass script_class = ta_script_classes[style_class->script];
72 FT_UInt32 standard_char;
75 if (!use_cmap)
76 goto Exit;
79 * We check more than a single standard character to catch features
80 * like `c2sc' (small caps from caps) that don't contain lowercase
81 * letters by definition, or other features that mainly operate on
82 * numerals.
84 standard_char = script_class->standard_char1;
85 ta_get_char_index(&metrics->root,
86 standard_char,
87 &glyph_index,
88 &y_offset);
89 if (!glyph_index)
91 if (script_class->standard_char2)
93 standard_char = script_class->standard_char2;
94 ta_get_char_index(&metrics->root,
95 standard_char,
96 &glyph_index,
97 &y_offset);
98 if (!glyph_index)
100 if (script_class->standard_char3)
102 standard_char = script_class->standard_char3;
103 ta_get_char_index(&metrics->root,
104 standard_char,
105 &glyph_index,
106 &y_offset);
107 if (!glyph_index)
108 goto Exit;
110 else
111 goto Exit;
114 else
115 goto Exit;
118 TA_LOG_GLOBAL(("standard character: U+%04lX (glyph index %d)\n",
119 standard_char, glyph_index));
121 error = FT_Load_Glyph(face, glyph_index, FT_LOAD_NO_SCALE);
122 if (error || face->glyph->outline.n_points <= 0)
123 goto Exit;
125 memset(dummy, 0, sizeof (TA_LatinMetricsRec));
127 dummy->units_per_em = metrics->units_per_em;
129 scaler->x_scale = 0x10000L;
130 scaler->y_scale = 0x10000L;
131 scaler->x_delta = 0;
132 scaler->y_delta = 0;
134 scaler->face = face;
135 scaler->render_mode = FT_RENDER_MODE_NORMAL;
136 scaler->flags = 0;
138 ta_glyph_hints_rescale(hints, (TA_StyleMetrics)dummy);
140 error = ta_glyph_hints_reload(hints, &face->glyph->outline);
141 if (error)
142 goto Exit;
144 for (dim = 0; dim < TA_DIMENSION_MAX; dim++)
146 TA_LatinAxis axis = &metrics->axis[dim];
147 TA_AxisHints axhints = &hints->axis[dim];
149 TA_Segment seg, limit, link;
150 FT_UInt num_widths = 0;
153 error = ta_latin_hints_compute_segments(hints, (TA_Dimension)dim);
154 if (error)
155 goto Exit;
158 * We assume that the glyphs selected for the stem width
159 * computation are `featureless' enough so that the linking
160 * algorithm works fine without adjustments of its scoring
161 * function.
163 ta_latin_hints_link_segments(hints, 0, NULL, (TA_Dimension)dim);
165 seg = axhints->segments;
166 limit = seg + axhints->num_segments;
168 for (; seg < limit; seg++)
170 link = seg->link;
172 /* we only consider stem segments there! */
173 if (link
174 && link->link == seg
175 && link > seg)
177 FT_Pos dist;
180 dist = seg->pos - link->pos;
181 if (dist < 0)
182 dist = -dist;
184 if (num_widths < TA_LATIN_MAX_WIDTHS)
185 axis->widths[num_widths++].org = dist;
189 /* this also replaces multiple almost identical stem widths */
190 /* with a single one (the value 100 is heuristic) */
191 ta_sort_and_quantize_widths(&num_widths, axis->widths,
192 dummy->units_per_em / 100);
193 axis->width_count = num_widths;
196 Exit:
197 for (dim = 0; dim < TA_DIMENSION_MAX; dim++)
199 FONT* font = metrics->root.globals->font;
200 TA_LatinAxis axis = &metrics->axis[dim];
201 FT_Pos stdw;
204 if (!axis->width_count)
206 /* if we have no standard characters, */
207 /* use `fallback-stem-width', if available, */
208 /* or a default width (value 50 is heuristic) */
209 stdw = (dim == TA_DIMENSION_VERT && font->fallback_stem_width)
210 ? (FT_Pos)font->fallback_stem_width
211 : TA_LATIN_CONSTANT(metrics, 50);
213 /* set one width value if we do hinting */
214 if (style_class->style != TA_STYLE_NONE_DFLT)
216 axis->width_count++;
217 axis->widths[0].org = stdw;
221 stdw = axis->widths[0].org;
223 /* let's try 20% of the smallest width */
224 axis->edge_distance_threshold = stdw / 5;
225 axis->standard_width = stdw;
226 axis->extra_light = 0;
228 #ifdef TA_DEBUG
230 FT_UInt i;
233 TA_LOG_GLOBAL(("%s widths:\n",
234 dim == TA_DIMENSION_VERT ? "horizontal"
235 : "vertical"));
237 TA_LOG_GLOBAL((" %d (standard)", axis->standard_width));
238 for (i = 1; i < axis->width_count; i++)
239 TA_LOG_GLOBAL((" %d", axis->widths[i].org));
241 TA_LOG_GLOBAL(("\n"));
243 #endif
247 TA_LOG_GLOBAL(("\n"));
249 ta_glyph_hints_done(hints);
253 /* find all blue zones; flat segments give the reference points, */
254 /* round segments the overshoot positions */
256 static void
257 ta_latin_metrics_init_blues(TA_LatinMetrics metrics,
258 FT_Face face)
260 FT_Pos flats[TA_BLUE_STRING_MAX_LEN];
261 FT_Pos rounds[TA_BLUE_STRING_MAX_LEN];
262 FT_Int num_flats;
263 FT_Int num_rounds;
265 TA_LatinBlue blue;
266 FT_Error error;
267 TA_LatinAxis axis = &metrics->axis[TA_DIMENSION_VERT];
268 FT_Outline outline;
270 TA_StyleClass sc = metrics->root.style_class;
272 TA_Blue_Stringset bss = sc->blue_stringset;
273 const TA_Blue_StringRec* bs = &ta_blue_stringsets[bss];
276 /* we walk over the blue character strings as specified in the */
277 /* style's entry in the `ta_blue_stringset' array */
279 TA_LOG_GLOBAL(("latin blue zones computation\n"
280 "============================\n"
281 "\n"));
283 for (; bs->string != TA_BLUE_STRING_MAX; bs++)
285 const char* p = &ta_blue_strings[bs->string];
286 FT_Pos* blue_ref;
287 FT_Pos* blue_shoot;
290 #ifdef TA_DEBUG
292 FT_Bool have_flag = 0;
295 TA_LOG_GLOBAL(("blue zone %d", axis->blue_count));
297 if (bs->properties)
299 TA_LOG_GLOBAL((" ("));
301 if (TA_LATIN_IS_TOP_BLUE(bs))
303 TA_LOG_GLOBAL(("top"));
304 have_flag = 1;
307 if (TA_LATIN_IS_NEUTRAL_BLUE(bs))
309 if (have_flag)
310 TA_LOG_GLOBAL((", "));
311 TA_LOG_GLOBAL(("neutral"));
312 have_flag = 1;
315 if (TA_LATIN_IS_X_HEIGHT_BLUE(bs))
317 if (have_flag)
318 TA_LOG_GLOBAL((", "));
319 TA_LOG_GLOBAL(("small top"));
320 have_flag = 1;
323 if (TA_LATIN_IS_LONG_BLUE(bs))
325 if (have_flag)
326 TA_LOG_GLOBAL((", "));
327 TA_LOG_GLOBAL(("long"));
330 TA_LOG_GLOBAL((")"));
333 TA_LOG_GLOBAL((":\n"));
335 #endif /* TA_DEBUG */
337 num_flats = 0;
338 num_rounds = 0;
340 while (*p)
342 FT_ULong ch;
343 FT_ULong glyph_index;
344 FT_Long y_offset;
345 FT_Pos best_y; /* same as points.y */
346 FT_Int best_point, best_contour_first, best_contour_last;
347 FT_Vector* points;
348 FT_Bool round = 0;
351 GET_UTF8_CHAR(ch, p);
353 /* load the character in the face -- skip unknown or empty ones */
354 ta_get_char_index(&metrics->root, ch, &glyph_index, &y_offset);
355 if (glyph_index == 0)
357 TA_LOG_GLOBAL((" U+%04lX unavailable\n", ch));
358 continue;
361 error = FT_Load_Glyph(face, glyph_index, FT_LOAD_NO_SCALE);
362 outline = face->glyph->outline;
363 if (error || outline.n_points <= 0)
365 TA_LOG_GLOBAL((" U+%04lX contains no outlines\n", ch));
366 continue;
369 /* now compute min or max point indices and coordinates */
370 points = outline.points;
371 best_point = -1;
372 best_y = 0; /* make compiler happy */
373 best_contour_first = 0; /* ditto */
374 best_contour_last = 0; /* ditto */
377 FT_Int nn;
378 FT_Int first = 0;
379 FT_Int last = -1;
382 for (nn = 0; nn < outline.n_contours; first = last + 1, nn++)
384 FT_Int old_best_point = best_point;
385 FT_Int pp;
388 last = outline.contours[nn];
390 /* avoid single-point contours since they are never rasterized; */
391 /* in some fonts, they correspond to mark attachment points */
392 /* that are way outside of the glyph's real outline */
393 if (last <= first)
394 continue;
396 if (TA_LATIN_IS_TOP_BLUE(bs))
398 for (pp = first; pp <= last; pp++)
399 if (best_point < 0
400 || points[pp].y > best_y)
402 best_point = pp;
403 best_y = points[pp].y;
406 else
408 for (pp = first; pp <= last; pp++)
409 if (best_point < 0
410 || points[pp].y < best_y)
412 best_point = pp;
413 best_y = points[pp].y;
417 if (best_point != old_best_point)
419 best_contour_first = first;
420 best_contour_last = last;
425 /* now check whether the point belongs to a straight or round */
426 /* segment; we first need to find in which contour the extremum */
427 /* lies, then inspect its previous and next points */
428 if (best_point >= 0)
430 FT_Pos best_x = points[best_point].x;
431 FT_Int prev, next;
432 FT_Int best_segment_first, best_segment_last;
433 FT_Int best_on_point_first, best_on_point_last;
434 FT_Pos dist;
437 best_segment_first = best_point;
438 best_segment_last = best_point;
440 if (FT_CURVE_TAG(outline.tags[best_point]) == FT_CURVE_TAG_ON)
442 best_on_point_first = best_point;
443 best_on_point_last = best_point;
445 else
447 best_on_point_first = -1;
448 best_on_point_last = -1;
451 /* look for the previous and next points on the contour */
452 /* that are not on the same Y coordinate, then threshold */
453 /* the `closeness'... */
454 prev = best_point;
455 next = prev;
459 if (prev > best_contour_first)
460 prev--;
461 else
462 prev = best_contour_last;
464 dist = TA_ABS(points[prev].y - best_y);
465 /* accept a small distance or a small angle (both values are */
466 /* heuristic; value 20 corresponds to approx. 2.9 degrees) */
467 if (dist > 5)
468 if (TA_ABS(points[prev].x - best_x) <= 20 * dist)
469 break;
471 best_segment_first = prev;
473 if (FT_CURVE_TAG(outline.tags[prev]) == FT_CURVE_TAG_ON)
475 best_on_point_first = prev;
476 if (best_on_point_last < 0)
477 best_on_point_last = prev;
480 } while (prev != best_point);
484 if (next < best_contour_last)
485 next++;
486 else
487 next = best_contour_first;
489 dist = TA_ABS(points[next].y - best_y);
490 if (dist > 5)
491 if (TA_ABS(points[next].x - best_x) <= 20 * dist)
492 break;
494 best_segment_last = next;
496 if (FT_CURVE_TAG(outline.tags[next]) == FT_CURVE_TAG_ON)
498 best_on_point_last = next;
499 if (best_on_point_first < 0)
500 best_on_point_first = next;
503 } while (next != best_point);
505 if (TA_LATIN_IS_LONG_BLUE(bs))
507 /* If this flag is set, we have an additional constraint to */
508 /* get the blue zone distance: Find a segment of the topmost */
509 /* (or bottommost) contour that is longer than a heuristic */
510 /* threshold. This ensures that small bumps in the outline */
511 /* are ignored (for example, the `vertical serifs' found in */
512 /* many Hebrew glyph designs). */
514 /* If this segment is long enough, we are done. Otherwise, */
515 /* search the segment next to the extremum that is long */
516 /* enough, has the same direction, and a not too large */
517 /* vertical distance from the extremum. Note that the */
518 /* algorithm doesn't check whether the found segment is */
519 /* actually the one (vertically) nearest to the extremum. */
521 /* heuristic threshold value */
522 FT_Pos length_threshold = metrics->units_per_em / 25;
525 dist = TA_ABS(points[best_segment_last].x -
526 points[best_segment_first].x);
528 if (dist < length_threshold
529 && best_segment_last - best_segment_first + 2 <=
530 best_contour_last - best_contour_first)
532 /* heuristic threshold value */
533 FT_Pos height_threshold = metrics->units_per_em / 4;
535 FT_Int first;
536 FT_Int last;
537 FT_Bool hit;
539 /* we intentionally declare these two variables */
540 /* outside of the loop since various compilers emit */
541 /* incorrect warning messages otherwise, talking about */
542 /* `possibly uninitialized variables' */
543 FT_Int p_first = 0; /* make compiler happy */
544 FT_Int p_last = 0;
546 FT_Bool left2right;
549 /* compute direction */
550 prev = best_point;
554 if (prev > best_contour_first)
555 prev--;
556 else
557 prev = best_contour_last;
559 if (points[prev].x != best_x)
560 break;
561 } while (prev != best_point);
563 /* skip glyph for the degenerate case */
564 if (prev == best_point)
565 continue;
567 left2right = FT_BOOL(points[prev].x < points[best_point].x);
569 first = best_segment_last;
570 last = first;
571 hit = 0;
575 FT_Bool l2r;
576 FT_Pos d;
579 if (!hit)
581 /* no hit; adjust first point */
582 first = last;
584 /* also adjust first and last on point */
585 if (FT_CURVE_TAG(outline.tags[first]) == FT_CURVE_TAG_ON)
587 p_first = first;
588 p_last = first;
590 else
592 p_first = -1;
593 p_last = -1;
596 hit = 1;
599 if (last < best_contour_last)
600 last++;
601 else
602 last = best_contour_first;
604 if (TA_ABS(best_y - points[first].y) > height_threshold)
606 /* vertical distance too large */
607 hit = 0;
608 continue;
611 /* same test as above */
612 dist = TA_ABS(points[last].y - points[first].y);
613 if (dist > 5)
614 if (TA_ABS(points[last].x - points[first].x) <= 20 * dist)
616 hit = 0;
617 continue;
620 if (FT_CURVE_TAG(outline.tags[last]) == FT_CURVE_TAG_ON)
622 p_last = last;
623 if (p_first < 0)
624 p_first = last;
627 l2r = FT_BOOL(points[first].x < points[last].x);
628 d = TA_ABS(points[last].x - points[first].x);
630 if (l2r == left2right
631 && d >= length_threshold)
633 /* all constraints are met; update segment after finding */
634 /* its end */
637 if (last < best_contour_last)
638 last++;
639 else
640 last = best_contour_first;
642 d = TA_ABS(points[last].y - points[first].y);
643 if (d > 5)
644 if (TA_ABS(points[next].x - points[first].x) <=
645 20 * dist)
647 if (last > best_contour_first)
648 last--;
649 else
650 last = best_contour_last;
651 break;
654 p_last = last;
656 if (FT_CURVE_TAG(outline.tags[last]) == FT_CURVE_TAG_ON)
658 p_last = last;
659 if (p_first < 0)
660 p_first = last;
662 } while (last != best_segment_first);
664 best_y = points[first].y;
666 best_segment_first = first;
667 best_segment_last = last;
669 best_on_point_first = p_first;
670 best_on_point_last = p_last;
672 break;
674 } while (last != best_segment_first);
679 * for computing blue zones, we add the y offset as returned
680 * by the currently used OpenType feature --
681 * for example, superscript glyphs might be identical
682 * to subscript glyphs with a vertical shift
684 best_y += y_offset;
686 TA_LOG_GLOBAL((" U+%04lX: best_y = %5ld", ch, best_y));
689 * now set the `round' flag depending on the segment's kind:
691 * - if the horizontal distance between the first and last
692 * `on' point is larger than upem/8 (value 8 is heuristic)
693 * we have a flat segment
694 * - if either the first or the last point of the segment is
695 * an `off' point, the segment is round, otherwise it is
696 * flat
698 if (best_on_point_first >= 0
699 && best_on_point_last >= 0
700 && (FT_UInt)(TA_ABS(points[best_on_point_last].x
701 - points[best_on_point_first].x))
702 > metrics->units_per_em / 8)
703 round = 0;
704 else
705 round = FT_BOOL(FT_CURVE_TAG(outline.tags[best_segment_first])
706 != FT_CURVE_TAG_ON
707 || FT_CURVE_TAG(outline.tags[best_segment_last])
708 != FT_CURVE_TAG_ON);
710 if (round && TA_LATIN_IS_NEUTRAL_BLUE(bs))
712 /* only use flat segments for a neutral blue zone */
713 TA_LOG_GLOBAL((" (round, skipped)\n"));
714 continue;
717 TA_LOG_GLOBAL((" (%s)\n", round ? "round" : "flat"));
720 if (round)
721 rounds[num_rounds++] = best_y;
722 else
723 flats[num_flats++] = best_y;
726 if (num_flats == 0 && num_rounds == 0)
728 /* we couldn't find a single glyph to compute this blue zone, */
729 /* we will simply ignore it then */
730 TA_LOG_GLOBAL((" empty\n"));
731 continue;
734 /* we have computed the contents of the `rounds' and `flats' tables, */
735 /* now determine the reference and overshoot position of the blue -- */
736 /* we simply take the median value after a simple sort */
737 ta_sort_pos(num_rounds, rounds);
738 ta_sort_pos(num_flats, flats);
740 blue = &axis->blues[axis->blue_count];
741 blue_ref = &blue->ref.org;
742 blue_shoot = &blue->shoot.org;
744 axis->blue_count++;
746 if (num_flats == 0)
748 *blue_ref =
749 *blue_shoot = rounds[num_rounds / 2];
751 else if (num_rounds == 0)
753 *blue_ref =
754 *blue_shoot = flats[num_flats / 2];
756 else
758 *blue_ref = flats[num_flats / 2];
759 *blue_shoot = rounds[num_rounds / 2];
762 /* there are sometimes problems if the overshoot position of top */
763 /* zones is under its reference position, or the opposite for bottom */
764 /* zones; we must thus check everything there and correct the errors */
765 if (*blue_shoot != *blue_ref)
767 FT_Pos ref = *blue_ref;
768 FT_Pos shoot = *blue_shoot;
769 FT_Bool over_ref = FT_BOOL(shoot > ref);
772 if (TA_LATIN_IS_TOP_BLUE(bs) ^ over_ref)
774 *blue_ref =
775 *blue_shoot = (shoot + ref) / 2;
777 TA_LOG_GLOBAL((" [overshoot smaller than reference,"
778 " taking mean value]\n"));
782 blue->flags = 0;
783 if (TA_LATIN_IS_TOP_BLUE(bs))
784 blue->flags |= TA_LATIN_BLUE_TOP;
785 if (TA_LATIN_IS_NEUTRAL_BLUE(bs))
786 blue->flags |= TA_LATIN_BLUE_NEUTRAL;
788 /* the following flag is used later to adjust the y and x scales */
789 /* in order to optimize the pixel grid alignment */
790 /* of the top of small letters */
791 if (TA_LATIN_IS_X_HEIGHT_BLUE(bs))
792 blue->flags |= TA_LATIN_BLUE_ADJUSTMENT;
794 TA_LOG_GLOBAL((" -> reference = %ld\n"
795 " overshoot = %ld\n",
796 *blue_ref, *blue_shoot));
799 /* add two blue zones for usWinAscent and usWinDescent */
800 /* just in case the above algorithm has missed them -- */
801 /* Windows cuts off everything outside of those two values */
803 TT_OS2* os2;
806 os2 = (TT_OS2*)FT_Get_Sfnt_Table(face, ft_sfnt_os2);
808 if (os2)
810 blue = &axis->blues[axis->blue_count];
811 blue->flags = TA_LATIN_BLUE_TOP | TA_LATIN_BLUE_ACTIVE;
812 blue->ref.org =
813 blue->shoot.org = os2->usWinAscent;
815 TA_LOG_GLOBAL(("artificial blue zone for usWinAscent:\n"
816 " -> reference = %ld\n"
817 " overshoot = %ld\n",
818 blue->ref.org, blue->shoot.org));
820 blue = &axis->blues[axis->blue_count + 1];
821 blue->flags = TA_LATIN_BLUE_ACTIVE;
822 blue->ref.org =
823 blue->shoot.org = -os2->usWinDescent;
825 TA_LOG_GLOBAL(("artificial blue zone for usWinDescent:\n"
826 " -> reference = %ld\n"
827 " overshoot = %ld\n",
828 blue->ref.org, blue->shoot.org));
830 else
832 blue = &axis->blues[axis->blue_count];
833 blue->flags =
834 blue->ref.org =
835 blue->shoot.org = 0;
837 blue = &axis->blues[axis->blue_count + 1];
838 blue->flags =
839 blue->ref.org =
840 blue->shoot.org = 0;
844 TA_LOG_GLOBAL(("\n"));
846 return;
850 /* check whether all ASCII digits have the same advance width */
852 void
853 ta_latin_metrics_check_digits(TA_LatinMetrics metrics,
854 FT_Face face)
856 FT_UInt i;
857 FT_Bool started = 0, same_width = 1;
858 FT_Fixed advance, old_advance = 0;
861 /* digit `0' is 0x30 in all supported charmaps */
862 for (i = 0x30; i <= 0x39; i++)
864 FT_ULong glyph_index;
865 FT_Long y_offset;
868 ta_get_char_index(&metrics->root, i, &glyph_index, &y_offset);
869 if (glyph_index == 0)
870 continue;
872 if (FT_Get_Advance(face, glyph_index,
873 FT_LOAD_NO_SCALE
874 | FT_LOAD_NO_HINTING
875 | FT_LOAD_IGNORE_TRANSFORM,
876 &advance))
877 continue;
879 if (started)
881 if (advance != old_advance)
883 same_width = 0;
884 break;
887 else
889 old_advance = advance;
890 started = 1;
894 metrics->root.digits_have_same_width = same_width;
898 /* initialize global metrics */
900 FT_Error
901 ta_latin_metrics_init(TA_LatinMetrics metrics,
902 FT_Face face)
904 FT_CharMap oldmap = face->charmap;
907 metrics->units_per_em = face->units_per_EM;
909 if (!FT_Select_Charmap(face, FT_ENCODING_UNICODE))
911 ta_latin_metrics_init_widths(metrics, face, 1);
912 ta_latin_metrics_init_blues(metrics, face);
913 ta_latin_metrics_check_digits(metrics, face);
915 else
917 /* we only have a symbol font encoding */
918 ta_latin_metrics_init_widths(metrics, face, 0);
921 FT_Set_Charmap(face, oldmap);
922 return FT_Err_Ok;
926 /* adjust scaling value, then scale and shift widths */
927 /* and blue zones (if applicable) for given dimension */
929 static void
930 ta_latin_metrics_scale_dim(TA_LatinMetrics metrics,
931 TA_Scaler scaler,
932 TA_Dimension dim)
934 FT_Fixed scale;
935 FT_Pos delta;
936 TA_LatinAxis axis;
937 FT_UInt ppem;
938 FT_UInt nn;
941 ppem = metrics->root.scaler.face->size->metrics.x_ppem;
943 if (dim == TA_DIMENSION_HORZ)
945 scale = scaler->x_scale;
946 delta = scaler->x_delta;
948 else
950 scale = scaler->y_scale;
951 delta = scaler->y_delta;
954 axis = &metrics->axis[dim];
956 if (axis->org_scale == scale && axis->org_delta == delta)
957 return;
959 axis->org_scale = scale;
960 axis->org_delta = delta;
962 /* correct Y scale to optimize the alignment of the top of */
963 /* small letters to the pixel grid */
964 /* (if we do x-height snapping for this ppem value) */
965 if (!number_set_is_element(
966 metrics->root.globals->font->x_height_snapping_exceptions,
967 ppem))
969 TA_LatinAxis Axis = &metrics->axis[TA_DIMENSION_VERT];
970 TA_LatinBlue blue = NULL;
973 for (nn = 0; nn < Axis->blue_count; nn++)
975 if (Axis->blues[nn].flags & TA_LATIN_BLUE_ADJUSTMENT)
977 blue = &Axis->blues[nn];
978 break;
982 if (blue)
984 FT_Pos scaled;
985 FT_Pos threshold;
986 FT_Pos fitted;
987 FT_UInt limit;
990 scaled = FT_MulFix(blue->shoot.org, scaler->y_scale);
991 limit = metrics->root.globals->increase_x_height;
992 threshold = 40;
994 /* if the `increase-x-height' property is active, */
995 /* we round up much more often */
996 if (limit
997 && ppem <= limit
998 && ppem >= TA_PROP_INCREASE_X_HEIGHT_MIN)
999 threshold = 52;
1001 fitted = (scaled + threshold) & ~63;
1003 if (scaled != fitted)
1005 if (dim == TA_DIMENSION_VERT)
1007 scale = FT_MulDiv(scale, fitted, scaled);
1009 TA_LOG_GLOBAL((
1010 "ta_latin_metrics_scale_dim:"
1011 " x height alignment (style `%s'):\n"
1013 " vertical scaling changed from %.4f to %.4f (by %d%%)\n"
1014 "\n",
1015 ta_style_names[metrics->root.style_class->style],
1016 axis->org_scale / 65536.0,
1017 scale / 65536.0,
1018 (fitted - scaled) * 100 / scaled));
1024 axis->scale = scale;
1025 axis->delta = delta;
1027 if (dim == TA_DIMENSION_HORZ)
1029 metrics->root.scaler.x_scale = scale;
1030 metrics->root.scaler.x_delta = delta;
1032 else
1034 metrics->root.scaler.y_scale = scale;
1035 metrics->root.scaler.y_delta = delta;
1038 TA_LOG_GLOBAL(("%s widths (style `%s')\n",
1039 dim == TA_DIMENSION_HORZ ? "horizontal" : "vertical",
1040 ta_style_names[metrics->root.style_class->style]));
1042 /* scale the widths */
1043 for (nn = 0; nn < axis->width_count; nn++)
1045 TA_Width width = axis->widths + nn;
1048 width->cur = FT_MulFix(width->org, scale);
1049 width->fit = width->cur;
1051 TA_LOG_GLOBAL((" %d scaled to %.2f\n",
1052 width->org,
1053 width->cur / 64.0));
1056 TA_LOG_GLOBAL(("\n"));
1058 /* an extra-light axis corresponds to a standard width that is */
1059 /* smaller than 5/8 pixels */
1060 axis->extra_light =
1061 (FT_Bool)(FT_MulFix(axis->standard_width, scale) < 32 + 8);
1063 #ifdef TA_DEBUG
1064 if (axis->extra_light)
1065 TA_LOG_GLOBAL(("`%s' style is extra light (at current resolution)\n"
1066 "\n",
1067 ta_style_names[metrics->root.style_class->style]));
1068 #endif
1070 if (dim == TA_DIMENSION_VERT)
1072 TA_LOG_GLOBAL(("blue zones (style `%s')\n",
1073 ta_style_names[metrics->root.style_class->style]));
1075 /* scale the blue zones */
1076 for (nn = 0; nn < axis->blue_count; nn++)
1078 TA_LatinBlue blue = &axis->blues[nn];
1079 FT_Pos dist;
1082 blue->ref.cur = FT_MulFix(blue->ref.org, scale) + delta;
1083 blue->ref.fit = blue->ref.cur;
1084 blue->shoot.cur = FT_MulFix(blue->shoot.org, scale) + delta;
1085 blue->shoot.fit = blue->shoot.cur;
1086 blue->flags &= ~TA_LATIN_BLUE_ACTIVE;
1088 /* a blue zone is only active if it is less than 3/4 pixels tall */
1089 dist = FT_MulFix(blue->ref.org - blue->shoot.org, scale);
1090 if (dist <= 48 && dist >= -48)
1092 #if 0
1093 FT_Pos delta1;
1094 #endif
1095 FT_Pos delta2;
1098 /* use discrete values for blue zone widths */
1100 #if 0
1101 /* generic, original code */
1102 delta1 = blue->shoot.org - blue->ref.org;
1103 delta2 = delta1;
1104 if (delta1 < 0)
1105 delta2 = -delta2;
1107 delta2 = FT_MulFix(delta2, scale);
1109 if (delta2 < 32)
1110 delta2 = 0;
1111 else if (delta2 < 64)
1112 delta2 = 32 + (((delta2 - 32) + 16) & ~31);
1113 else
1114 delta2 = TA_PIX_ROUND(delta2);
1116 if (delta1 < 0)
1117 delta2 = -delta2;
1119 blue->ref.fit = TA_PIX_ROUND(blue->ref.cur);
1120 blue->shoot.fit = blue->ref.fit + delta2;
1121 #else
1122 /* simplified version due to abs(dist) <= 48 */
1123 delta2 = dist;
1124 if (dist < 0)
1125 delta2 = -delta2;
1127 if (delta2 < 32)
1128 delta2 = 0;
1129 else if (delta2 < 48)
1130 delta2 = 32;
1131 else
1132 delta2 = 64;
1134 if (dist < 0)
1135 delta2 = -delta2;
1137 blue->ref.fit = TA_PIX_ROUND(blue->ref.cur);
1138 blue->shoot.fit = blue->ref.fit - delta2;
1139 #endif
1141 blue->flags |= TA_LATIN_BLUE_ACTIVE;
1143 TA_LOG_GLOBAL((" reference %d: %d scaled to %.2f%s\n"
1144 " overshoot %d: %d scaled to %.2f%s\n",
1146 blue->ref.org,
1147 blue->ref.fit / 64.0,
1148 blue->flags & TA_LATIN_BLUE_ACTIVE ? ""
1149 : " (inactive)",
1151 blue->shoot.org,
1152 blue->shoot.fit / 64.0,
1153 blue->flags & TA_LATIN_BLUE_ACTIVE ? ""
1154 : " (inactive)"));
1158 /* the last two artificial blue zones are to be scaled */
1159 /* with uncorrected scaling values */
1161 TA_LatinAxis a = &metrics->axis[TA_DIMENSION_VERT];
1162 TA_LatinBlue b;
1165 b = &a->blues[a->blue_count];
1166 b->ref.cur =
1167 b->ref.fit =
1168 b->shoot.cur =
1169 b->shoot.fit = FT_MulFix(b->ref.org, a->org_scale) + delta;
1171 TA_LOG_GLOBAL((" reference %d: %d scaled to %.2f (artificial)\n"
1172 " overshoot %d: %d scaled to %.2f (artificial)\n",
1173 a->blue_count,
1174 b->ref.org,
1175 b->ref.fit / 64.0,
1176 a->blue_count,
1177 b->shoot.org,
1178 b->shoot.fit / 64.0));
1180 b = &a->blues[a->blue_count + 1];
1181 b->ref.cur =
1182 b->ref.fit =
1183 b->shoot.cur =
1184 b->shoot.fit = FT_MulFix(b->ref.org, a->org_scale) + delta;
1186 TA_LOG_GLOBAL((" reference %d: %d scaled to %.2f (artificial)\n"
1187 " overshoot %d: %d scaled to %.2f (artificial)\n",
1188 a->blue_count + 1,
1189 b->ref.org,
1190 b->ref.fit / 64.0,
1191 a->blue_count + 1,
1192 b->shoot.org,
1193 b->shoot.fit / 64.0));
1196 TA_LOG_GLOBAL(("\n"));
1201 /* scale global values in both directions */
1203 void
1204 ta_latin_metrics_scale(TA_LatinMetrics metrics,
1205 TA_Scaler scaler)
1207 metrics->root.scaler.render_mode = scaler->render_mode;
1208 metrics->root.scaler.face = scaler->face;
1209 metrics->root.scaler.flags = scaler->flags;
1211 ta_latin_metrics_scale_dim(metrics, scaler, TA_DIMENSION_HORZ);
1212 ta_latin_metrics_scale_dim(metrics, scaler, TA_DIMENSION_VERT);
1216 /* walk over all contours and compute its segments */
1218 FT_Error
1219 ta_latin_hints_compute_segments(TA_GlyphHints hints,
1220 TA_Dimension dim)
1222 TA_AxisHints axis = &hints->axis[dim];
1223 FT_Error error = FT_Err_Ok;
1225 TA_Segment segment = NULL;
1226 TA_SegmentRec seg0;
1228 TA_Point* contour = hints->contours;
1229 TA_Point* contour_limit = contour + hints->num_contours;
1230 TA_Direction major_dir, segment_dir;
1233 memset(&seg0, 0, sizeof (TA_SegmentRec));
1234 seg0.score = 32000;
1235 seg0.flags = TA_EDGE_NORMAL;
1237 major_dir = (TA_Direction)TA_ABS(axis->major_dir);
1238 segment_dir = major_dir;
1240 axis->num_segments = 0;
1242 /* set up (u,v) in each point */
1243 if (dim == TA_DIMENSION_HORZ)
1245 TA_Point point = hints->points;
1246 TA_Point limit = point + hints->num_points;
1249 for (; point < limit; point++)
1251 point->u = point->fx;
1252 point->v = point->fy;
1255 else
1257 TA_Point point = hints->points;
1258 TA_Point limit = point + hints->num_points;
1261 for (; point < limit; point++)
1263 point->u = point->fy;
1264 point->v = point->fx;
1268 /* do each contour separately */
1269 for (; contour < contour_limit; contour++)
1271 TA_Point point = contour[0];
1272 TA_Point last = point->prev;
1274 int on_edge = 0;
1276 FT_Pos min_pos = 32000; /* minimum segment pos != min_coord */
1277 FT_Pos max_pos = -32000; /* maximum segment pos != max_coord */
1278 FT_Bool passed;
1281 if (point == last) /* skip singletons -- just in case */
1282 continue;
1284 if (TA_ABS(last->out_dir) == major_dir
1285 && TA_ABS(point->out_dir) == major_dir)
1287 /* we are already on an edge, try to locate its start */
1288 last = point;
1290 for (;;)
1292 point = point->prev;
1293 if (TA_ABS(point->out_dir) != major_dir)
1295 point = point->next;
1296 break;
1298 if (point == last)
1299 break;
1303 last = point;
1304 passed = 0;
1306 for (;;)
1308 FT_Pos u, v;
1311 if (on_edge)
1313 u = point->u;
1314 if (u < min_pos)
1315 min_pos = u;
1316 if (u > max_pos)
1317 max_pos = u;
1319 if (point->out_dir != segment_dir
1320 || point == last)
1322 /* we are just leaving an edge; record a new segment! */
1323 segment->last = point;
1324 segment->pos = (FT_Short)((min_pos + max_pos) >> 1);
1326 /* a segment is round if either its first or last point */
1327 /* is a control point */
1328 if ((segment->first->flags | point->flags) & TA_FLAG_CONTROL)
1329 segment->flags |= TA_EDGE_ROUND;
1331 /* compute segment size */
1332 min_pos = max_pos = point->v;
1334 v = segment->first->v;
1335 if (v < min_pos)
1336 min_pos = v;
1337 if (v > max_pos)
1338 max_pos = v;
1340 segment->min_coord = (FT_Short)min_pos;
1341 segment->max_coord = (FT_Short)max_pos;
1342 segment->height = (FT_Short)(segment->max_coord -
1343 segment->min_coord);
1345 on_edge = 0;
1346 segment = NULL;
1347 /* fall through */
1351 /* now exit if we are at the start/end point */
1352 if (point == last)
1354 if (passed)
1355 break;
1356 passed = 1;
1359 if (!on_edge
1360 && TA_ABS(point->out_dir) == major_dir)
1362 /* this is the start of a new segment! */
1363 segment_dir = (TA_Direction)point->out_dir;
1365 /* clear all segment fields */
1366 error = ta_axis_hints_new_segment(axis, &segment);
1367 if (error)
1368 goto Exit;
1370 segment[0] = seg0;
1371 segment->dir = (FT_Char)segment_dir;
1372 min_pos = max_pos = point->u;
1373 segment->first = point;
1374 segment->last = point;
1375 on_edge = 1;
1378 point = point->next;
1380 } /* contours */
1383 /* now slightly increase the height of segments if this makes sense -- */
1384 /* this is used to better detect and ignore serifs */
1386 TA_Segment segments = axis->segments;
1387 TA_Segment segments_end = segments + axis->num_segments;
1390 for (segment = segments; segment < segments_end; segment++)
1392 TA_Point first = segment->first;
1393 TA_Point last = segment->last;
1395 FT_Pos first_v = first->v;
1396 FT_Pos last_v = last->v;
1399 if (first == last)
1400 continue;
1402 if (first_v < last_v)
1404 TA_Point p;
1407 p = first->prev;
1408 if (p->v < first_v)
1409 segment->height = (FT_Short)(segment->height +
1410 ((first_v - p->v) >> 1));
1412 p = last->next;
1413 if (p->v > last_v)
1414 segment->height = (FT_Short)(segment->height +
1415 ((p->v - last_v) >> 1));
1417 else
1419 TA_Point p;
1422 p = first->prev;
1423 if (p->v > first_v)
1424 segment->height = (FT_Short)(segment->height +
1425 ((p->v - first_v) >> 1));
1427 p = last->next;
1428 if (p->v < last_v)
1429 segment->height = (FT_Short)(segment->height +
1430 ((last_v - p->v) >> 1));
1435 Exit:
1436 return error;
1440 /* link segments to form stems and serifs; if `width_count' and */
1441 /* `widths' are non-zero, use them to fine-tune the scoring function */
1443 void
1444 ta_latin_hints_link_segments(TA_GlyphHints hints,
1445 FT_UInt width_count,
1446 TA_WidthRec* widths,
1447 TA_Dimension dim)
1449 TA_AxisHints axis = &hints->axis[dim];
1451 TA_Segment segments = axis->segments;
1452 TA_Segment segment_limit = segments + axis->num_segments;
1454 FT_Pos len_threshold, len_score, dist_score, max_width;
1455 TA_Segment seg1, seg2;
1458 if (width_count)
1459 max_width = widths[width_count - 1].org;
1460 else
1461 max_width = 0;
1463 /* a heuristic value to set up a minimum value for overlapping */
1464 len_threshold = TA_LATIN_CONSTANT(hints->metrics, 8);
1465 if (len_threshold == 0)
1466 len_threshold = 1;
1468 /* a heuristic value to weight lengths */
1469 len_score = TA_LATIN_CONSTANT(hints->metrics, 6000);
1471 /* a heuristic value to weight distances (no call to */
1472 /* TA_LATIN_CONSTANT needed, since we work on multiples */
1473 /* of the stem width) */
1474 dist_score = 3000;
1476 /* now compare each segment to the others */
1477 for (seg1 = segments; seg1 < segment_limit; seg1++)
1479 /* the fake segments are introduced to hint the metrics -- */
1480 /* we must never link them to anything */
1481 if (seg1->dir != axis->major_dir
1482 || seg1->first == seg1->last)
1483 continue;
1485 /* search for stems having opposite directions, */
1486 /* with seg1 to the `left' of seg2 */
1487 for (seg2 = segments; seg2 < segment_limit; seg2++)
1489 FT_Pos pos1 = seg1->pos;
1490 FT_Pos pos2 = seg2->pos;
1493 if (seg1->dir + seg2->dir == 0
1494 && pos2 > pos1)
1496 /* compute distance between the two segments */
1497 FT_Pos min = seg1->min_coord;
1498 FT_Pos max = seg1->max_coord;
1499 FT_Pos len;
1502 if (min < seg2->min_coord)
1503 min = seg2->min_coord;
1504 if (max > seg2->max_coord)
1505 max = seg2->max_coord;
1507 /* compute maximum coordinate difference of the two segments */
1508 /* (this is, how much they overlap) */
1509 len = max - min;
1510 if (len >= len_threshold)
1513 * The score is the sum of two demerits indicating the
1514 * `badness' of a fit, measured along the segments' main axis
1515 * and orthogonal to it, respectively.
1517 * o The less overlapping along the main axis, the worse it
1518 * is, causing a larger demerit.
1520 * o The nearer the orthogonal distance to a stem width, the
1521 * better it is, causing a smaller demerit. For simplicity,
1522 * however, we only increase the demerit for values that
1523 * exceed the largest stem width.
1526 FT_Pos dist = pos2 - pos1;
1528 FT_Pos dist_demerit, score;
1531 if (max_width)
1533 /* distance demerits are based on multiples of `max_width'; */
1534 /* we scale by 1024 for getting more precision */
1535 FT_Pos delta = (dist << 10) / max_width - (1 << 10);
1538 if (delta > 10000)
1539 dist_demerit = 32000;
1540 else if (delta > 0)
1541 dist_demerit = delta * delta / dist_score;
1542 else
1543 dist_demerit = 0;
1545 else
1546 dist_demerit = dist; /* default if no widths available */
1548 score = dist_demerit + len_score / len;
1550 /* and we search for the smallest score */
1551 if (score < seg1->score)
1553 seg1->score = score;
1554 seg1->link = seg2;
1557 if (score < seg2->score)
1559 seg2->score = score;
1560 seg2->link = seg1;
1567 /* now compute the `serif' segments, cf. explanations in `tahints.h' */
1568 for (seg1 = segments; seg1 < segment_limit; seg1++)
1570 seg2 = seg1->link;
1572 if (seg2)
1574 if (seg2->link != seg1)
1576 seg1->link = 0;
1577 seg1->serif = seg2->link;
1584 /* link segments to edges, using feature analysis for selection */
1586 FT_Error
1587 ta_latin_hints_compute_edges(TA_GlyphHints hints,
1588 TA_Dimension dim)
1590 TA_AxisHints axis = &hints->axis[dim];
1591 FT_Error error = FT_Err_Ok;
1592 TA_LatinAxis laxis = &((TA_LatinMetrics)hints->metrics)->axis[dim];
1594 TA_Segment segments = axis->segments;
1595 TA_Segment segment_limit = segments + axis->num_segments;
1596 TA_Segment seg;
1598 #if 0
1599 TA_Direction up_dir;
1600 #endif
1601 FT_Fixed scale;
1602 FT_Pos edge_distance_threshold;
1603 FT_Pos segment_length_threshold;
1606 axis->num_edges = 0;
1608 scale = (dim == TA_DIMENSION_HORZ) ? hints->x_scale
1609 : hints->y_scale;
1611 #if 0
1612 up_dir = (dim == TA_DIMENSION_HORZ) ? TA_DIR_UP
1613 : TA_DIR_RIGHT;
1614 #endif
1616 /* we ignore all segments that are less than 1 pixel in length */
1617 /* to avoid many problems with serif fonts */
1618 /* (the corresponding threshold is computed in font units) */
1619 if (dim == TA_DIMENSION_HORZ)
1620 segment_length_threshold = FT_DivFix(64, hints->y_scale);
1621 else
1622 segment_length_threshold = 0;
1624 /********************************************************************/
1625 /* */
1626 /* We begin by generating a sorted table of edges for the current */
1627 /* direction. To do so, we simply scan each segment and try to find */
1628 /* an edge in our table that corresponds to its position. */
1629 /* */
1630 /* If no edge is found, we create and insert a new edge in the */
1631 /* sorted table. Otherwise, we simply add the segment to the edge's */
1632 /* list which gets processed in the second step to compute the */
1633 /* edge's properties. */
1634 /* */
1635 /* Note that the table of edges is sorted along the segment/edge */
1636 /* position. */
1637 /* */
1638 /********************************************************************/
1640 /* assure that edge distance threshold is at most 0.25px */
1641 edge_distance_threshold = FT_MulFix(laxis->edge_distance_threshold,
1642 scale);
1643 if (edge_distance_threshold > 64 / 4)
1644 edge_distance_threshold = 64 / 4;
1646 edge_distance_threshold = FT_DivFix(edge_distance_threshold,
1647 scale);
1649 for (seg = segments; seg < segment_limit; seg++)
1651 TA_Edge found = NULL;
1652 FT_Int ee;
1655 if (seg->height < segment_length_threshold)
1656 continue;
1658 /* a special case for serif edges: */
1659 /* if they are smaller than 1.5 pixels we ignore them */
1660 if (seg->serif
1661 && 2 * seg->height < 3 * segment_length_threshold)
1662 continue;
1664 /* look for an edge corresponding to the segment */
1665 for (ee = 0; ee < axis->num_edges; ee++)
1667 TA_Edge edge = axis->edges + ee;
1668 FT_Pos dist;
1671 dist = seg->pos - edge->fpos;
1672 if (dist < 0)
1673 dist = -dist;
1675 if (dist < edge_distance_threshold && edge->dir == seg->dir)
1677 found = edge;
1678 break;
1682 if (!found)
1684 TA_Edge edge;
1687 /* insert a new edge in the list and sort according to the position */
1688 error = ta_axis_hints_new_edge(axis, seg->pos,
1689 (TA_Direction)seg->dir,
1690 &edge);
1691 if (error)
1692 goto Exit;
1694 /* add the segment to the new edge's list */
1695 memset(edge, 0, sizeof (TA_EdgeRec));
1696 edge->first = seg;
1697 edge->last = seg;
1698 edge->dir = seg->dir;
1699 edge->fpos = seg->pos;
1700 edge->opos = FT_MulFix(seg->pos, scale);
1701 edge->pos = edge->opos;
1702 seg->edge_next = seg;
1704 else
1706 /* if an edge was found, simply add the segment to the edge's list */
1707 seg->edge_next = found->first;
1708 found->last->edge_next = seg;
1709 found->last = seg;
1713 /*****************************************************************/
1714 /* */
1715 /* Good, we now compute each edge's properties according to */
1716 /* the segments found on its position. Basically, these are */
1717 /* */
1718 /* - the edge's main direction */
1719 /* - stem edge, serif edge or both (which defaults to stem then) */
1720 /* - rounded edge, straight or both (which defaults to straight) */
1721 /* - link for edge */
1722 /* */
1723 /*****************************************************************/
1725 /* first of all, set the `edge' field in each segment -- this is */
1726 /* required in order to compute edge links */
1728 /* note that removing this loop and setting the `edge' field of each */
1729 /* segment directly in the code above slows down execution speed for */
1730 /* some reasons on platforms like the Sun */
1732 TA_Edge edges = axis->edges;
1733 TA_Edge edge_limit = edges + axis->num_edges;
1734 TA_Edge edge;
1737 for (edge = edges; edge < edge_limit; edge++)
1739 seg = edge->first;
1740 if (seg)
1743 seg->edge = edge;
1744 seg = seg->edge_next;
1745 } while (seg != edge->first);
1748 /* now compute each edge properties */
1749 for (edge = edges; edge < edge_limit; edge++)
1751 FT_Int is_round = 0; /* does it contain round segments? */
1752 FT_Int is_straight = 0; /* does it contain straight segments? */
1753 #if 0
1754 FT_Pos ups = 0; /* number of upwards segments */
1755 FT_Pos downs = 0; /* number of downwards segments */
1756 #endif
1759 seg = edge->first;
1763 FT_Bool is_serif;
1766 /* check for roundness of segment */
1767 if (seg->flags & TA_EDGE_ROUND)
1768 is_round++;
1769 else
1770 is_straight++;
1772 #if 0
1773 /* check for segment direction */
1774 if (seg->dir == up_dir)
1775 ups += seg->max_coord - seg->min_coord;
1776 else
1777 downs += seg->max_coord - seg->min_coord;
1778 #endif
1780 /* check for links -- */
1781 /* if seg->serif is set, then seg->link must be ignored */
1782 is_serif = (FT_Bool)(seg->serif
1783 && seg->serif->edge
1784 && seg->serif->edge != edge);
1786 if ((seg->link && seg->link->edge != NULL)
1787 || is_serif)
1789 TA_Edge edge2;
1790 TA_Segment seg2;
1793 edge2 = edge->link;
1794 seg2 = seg->link;
1796 if (is_serif)
1798 seg2 = seg->serif;
1799 edge2 = edge->serif;
1802 if (edge2)
1804 FT_Pos edge_delta;
1805 FT_Pos seg_delta;
1808 edge_delta = edge->fpos - edge2->fpos;
1809 if (edge_delta < 0)
1810 edge_delta = -edge_delta;
1812 seg_delta = seg->pos - seg2->pos;
1813 if (seg_delta < 0)
1814 seg_delta = -seg_delta;
1816 if (seg_delta < edge_delta)
1817 edge2 = seg2->edge;
1819 else
1820 edge2 = seg2->edge;
1822 if (is_serif)
1824 edge->serif = edge2;
1825 edge2->flags |= TA_EDGE_SERIF;
1827 else
1828 edge->link = edge2;
1831 seg = seg->edge_next;
1832 } while (seg != edge->first);
1834 /* set the round/straight flags */
1835 edge->flags = TA_EDGE_NORMAL;
1837 if (is_round > 0
1838 && is_round >= is_straight)
1839 edge->flags |= TA_EDGE_ROUND;
1841 #if 0
1842 /* set the edge's main direction */
1843 edge->dir = TA_DIR_NONE;
1845 if (ups > downs)
1846 edge->dir = (FT_Char)up_dir;
1848 else if (ups < downs)
1849 edge->dir = (FT_Char)-up_dir;
1851 else if (ups == downs)
1852 edge->dir = 0; /* both up and down! */
1853 #endif
1855 /* get rid of serifs if link is set */
1856 /* XXX: this gets rid of many unpleasant artefacts! */
1857 /* example: the `c' in cour.pfa at size 13 */
1859 if (edge->serif && edge->link)
1860 edge->serif = 0;
1864 Exit:
1865 return error;
1869 /* detect segments and edges for given dimension */
1871 FT_Error
1872 ta_latin_hints_detect_features(TA_GlyphHints hints,
1873 FT_UInt width_count,
1874 TA_WidthRec* widths,
1875 TA_Dimension dim)
1877 FT_Error error;
1880 error = ta_latin_hints_compute_segments(hints, dim);
1881 if (!error)
1883 ta_latin_hints_link_segments(hints, width_count, widths, dim);
1885 error = ta_latin_hints_compute_edges(hints, dim);
1888 return error;
1892 /* compute all edges which lie within blue zones */
1894 void
1895 ta_latin_hints_compute_blue_edges(TA_GlyphHints hints,
1896 TA_LatinMetrics metrics)
1898 TA_AxisHints axis = &hints->axis[TA_DIMENSION_VERT];
1900 TA_Edge edge = axis->edges;
1901 TA_Edge edge_limit = edge + axis->num_edges;
1903 TA_LatinAxis latin = &metrics->axis[TA_DIMENSION_VERT];
1904 FT_Fixed scale = latin->scale;
1907 /* compute which blue zones are active, */
1908 /* i.e. have their scaled size < 3/4 pixels */
1910 /* for each horizontal edge search the blue zone which is closest */
1911 for (; edge < edge_limit; edge++)
1913 FT_UInt bb;
1914 TA_Width best_blue = NULL;
1915 FT_Bool best_blue_is_neutral = 0;
1916 FT_Pos best_dist; /* initial threshold */
1918 FT_UInt best_blue_idx = 0;
1919 FT_Bool best_blue_is_shoot = 0;
1922 /* compute the initial threshold as a fraction of the EM size */
1923 /* (the value 40 is heuristic) */
1924 best_dist = FT_MulFix(metrics->units_per_em / 40, scale);
1926 /* assure a minimum distance of 0.5px */
1927 if (best_dist > 64 / 2)
1928 best_dist = 64 / 2;
1930 /* this loop also handles the two extra blue zones */
1931 /* for usWinAscent and usWinDescent */
1932 /* if option `windows-compatibility' is set */
1933 for (bb = 0;
1934 bb < latin->blue_count
1935 + (metrics->root.globals->font->windows_compatibility ? 2 : 0);
1936 bb++)
1938 TA_LatinBlue blue = latin->blues + bb;
1939 FT_Bool is_top_blue, is_neutral_blue, is_major_dir;
1942 /* skip inactive blue zones (i.e., those that are too large) */
1943 if (!(blue->flags & TA_LATIN_BLUE_ACTIVE))
1944 continue;
1946 /* if it is a top zone, check for right edges (against the major */
1947 /* direction); if it is a bottom zone, check for left edges (in */
1948 /* the major direction) */
1949 is_top_blue = (FT_Byte)((blue->flags & TA_LATIN_BLUE_TOP) != 0);
1950 is_neutral_blue = (FT_Byte)((blue->flags & TA_LATIN_BLUE_NEUTRAL) != 0);
1951 is_major_dir = FT_BOOL(edge->dir == axis->major_dir);
1953 /* neutral blue zones are handled for both directions */
1954 if (is_top_blue ^ is_major_dir || is_neutral_blue)
1956 FT_Pos dist;
1959 /* first of all, compare it to the reference position */
1960 dist = edge->fpos - blue->ref.org;
1961 if (dist < 0)
1962 dist = -dist;
1964 dist = FT_MulFix(dist, scale);
1965 if (dist < best_dist)
1967 best_dist = dist;
1968 best_blue = &blue->ref;
1969 best_blue_is_neutral = is_neutral_blue;
1971 best_blue_idx = bb;
1972 best_blue_is_shoot = 0;
1975 /* now compare it to the overshoot position and check whether */
1976 /* the edge is rounded, and whether the edge is over the */
1977 /* reference position of a top zone, or under the reference */
1978 /* position of a bottom zone (provided we don't have a */
1979 /* neutral blue zone) */
1980 if (edge->flags & TA_EDGE_ROUND
1981 && dist != 0
1982 && !is_neutral_blue)
1984 FT_Bool is_under_ref = FT_BOOL(edge->fpos < blue->ref.org);
1987 if (is_top_blue ^ is_under_ref)
1989 dist = edge->fpos - blue->shoot.org;
1990 if (dist < 0)
1991 dist = -dist;
1993 dist = FT_MulFix(dist, scale);
1994 if (dist < best_dist)
1996 best_dist = dist;
1997 best_blue = &blue->shoot;
1998 best_blue_is_neutral = is_neutral_blue;
2000 best_blue_idx = bb;
2001 best_blue_is_shoot = 1;
2008 if (best_blue)
2010 edge->blue_edge = best_blue;
2011 edge->best_blue_idx = best_blue_idx;
2012 edge->best_blue_is_shoot = best_blue_is_shoot;
2013 if (best_blue_is_neutral)
2014 edge->flags |= TA_EDGE_NEUTRAL;
2020 /* initalize hinting engine */
2022 static FT_Error
2023 ta_latin_hints_init(TA_GlyphHints hints,
2024 TA_LatinMetrics metrics)
2026 FT_Render_Mode mode;
2027 FT_UInt32 scaler_flags, other_flags;
2028 FT_Face face = metrics->root.scaler.face;
2031 ta_glyph_hints_rescale(hints, (TA_StyleMetrics)metrics);
2033 /* correct x_scale and y_scale if needed, since they may have */
2034 /* been modified by `ta_latin_metrics_scale_dim' above */
2035 hints->x_scale = metrics->axis[TA_DIMENSION_HORZ].scale;
2036 hints->x_delta = metrics->axis[TA_DIMENSION_HORZ].delta;
2037 hints->y_scale = metrics->axis[TA_DIMENSION_VERT].scale;
2038 hints->y_delta = metrics->axis[TA_DIMENSION_VERT].delta;
2040 /* compute flags depending on render mode, etc. */
2041 mode = metrics->root.scaler.render_mode;
2043 #if 0 /* #ifdef TA_CONFIG_OPTION_USE_WARPER */
2044 if (mode == FT_RENDER_MODE_LCD
2045 || mode == FT_RENDER_MODE_LCD_V)
2046 metrics->root.scaler.render_mode =
2047 mode = FT_RENDER_MODE_NORMAL;
2048 #endif
2050 scaler_flags = hints->scaler_flags;
2051 other_flags = 0;
2053 /* we snap the width of vertical stems for the monochrome */
2054 /* and horizontal LCD rendering targets only */
2055 if (mode == FT_RENDER_MODE_MONO
2056 || mode == FT_RENDER_MODE_LCD)
2057 other_flags |= TA_LATIN_HINTS_HORZ_SNAP;
2059 /* we snap the width of horizontal stems for the monochrome */
2060 /* and vertical LCD rendering targets only */
2061 if (mode == FT_RENDER_MODE_MONO
2062 || mode == FT_RENDER_MODE_LCD_V)
2063 other_flags |= TA_LATIN_HINTS_VERT_SNAP;
2065 /* we adjust stems to full pixels only if we don't use the `light' mode */
2066 if (mode != FT_RENDER_MODE_LIGHT)
2067 other_flags |= TA_LATIN_HINTS_STEM_ADJUST;
2069 if (mode == FT_RENDER_MODE_MONO)
2070 other_flags |= TA_LATIN_HINTS_MONO;
2072 /* in `light' hinting mode we disable horizontal hinting completely; */
2073 /* we also do it if the face is italic */
2074 if (mode == FT_RENDER_MODE_LIGHT
2075 || (face->style_flags & FT_STYLE_FLAG_ITALIC) != 0)
2076 scaler_flags |= TA_SCALER_FLAG_NO_HORIZONTAL;
2078 hints->scaler_flags = scaler_flags;
2079 hints->other_flags = other_flags;
2081 return FT_Err_Ok;
2085 /* snap a given width in scaled coordinates to */
2086 /* one of the current standard widths */
2088 static FT_Pos
2089 ta_latin_snap_width(TA_Width widths,
2090 FT_Int count,
2091 FT_Pos width)
2093 int n;
2094 FT_Pos best = 64 + 32 + 2;
2095 FT_Pos reference = width;
2096 FT_Pos scaled;
2099 for (n = 0; n < count; n++)
2101 FT_Pos w;
2102 FT_Pos dist;
2105 w = widths[n].cur;
2106 dist = width - w;
2107 if (dist < 0)
2108 dist = -dist;
2109 if (dist < best)
2111 best = dist;
2112 reference = w;
2116 scaled = TA_PIX_ROUND(reference);
2118 if (width >= reference)
2120 if (width < scaled + 48)
2121 width = reference;
2123 else
2125 if (width > scaled - 48)
2126 width = reference;
2129 return width;
2133 /* compute the snapped width of a given stem, ignoring very thin ones */
2135 /* there is a lot of voodoo in this function; changing the hard-coded */
2136 /* parameters influences the whole hinting process */
2138 static FT_Pos
2139 ta_latin_compute_stem_width(TA_GlyphHints hints,
2140 TA_Dimension dim,
2141 FT_Pos width,
2142 FT_Byte base_flags,
2143 FT_Byte stem_flags)
2145 TA_LatinMetrics metrics = (TA_LatinMetrics) hints->metrics;
2146 TA_LatinAxis axis = &metrics->axis[dim];
2148 FT_Pos dist = width;
2149 FT_Int sign = 0;
2150 FT_Int vertical = (dim == TA_DIMENSION_VERT);
2153 if (!TA_LATIN_HINTS_DO_STEM_ADJUST(hints)
2154 || axis->extra_light)
2155 return width;
2157 if (dist < 0)
2159 dist = -width;
2160 sign = 1;
2163 if ((vertical && !TA_LATIN_HINTS_DO_VERT_SNAP(hints))
2164 || (!vertical && !TA_LATIN_HINTS_DO_HORZ_SNAP(hints)))
2166 /* smooth hinting process: very lightly quantize the stem width */
2168 /* leave the widths of serifs alone */
2169 if ((stem_flags & TA_EDGE_SERIF)
2170 && vertical
2171 && (dist < 3 * 64))
2172 goto Done_Width;
2173 else if (base_flags & TA_EDGE_ROUND)
2175 if (dist < 80)
2176 dist = 64;
2178 else if (dist < 56)
2179 dist = 56;
2181 if (axis->width_count > 0)
2183 FT_Pos delta;
2186 /* compare to standard width */
2187 delta = dist - axis->widths[0].cur;
2189 if (delta < 0)
2190 delta = -delta;
2192 if (delta < 40)
2194 dist = axis->widths[0].cur;
2195 if (dist < 48)
2196 dist = 48;
2198 goto Done_Width;
2201 if (dist < 3 * 64)
2203 delta = dist & 63;
2204 dist &= -64;
2206 if (delta < 10)
2207 dist += delta;
2208 else if (delta < 32)
2209 dist += 10;
2210 else if (delta < 54)
2211 dist += 54;
2212 else
2213 dist += delta;
2215 else
2216 dist = (dist + 32) & ~63;
2219 else
2221 /* strong hinting process: snap the stem width to integer pixels */
2223 FT_Pos org_dist = dist;
2226 dist = ta_latin_snap_width(axis->widths, axis->width_count, dist);
2228 if (vertical)
2230 /* in the case of vertical hinting, */
2231 /* always round the stem heights to integer pixels */
2233 if (dist >= 64)
2234 dist = (dist + 16) & ~63;
2235 else
2236 dist = 64;
2238 else
2240 if (TA_LATIN_HINTS_DO_MONO(hints))
2242 /* monochrome horizontal hinting: */
2243 /* snap widths to integer pixels with a different threshold */
2245 if (dist < 64)
2246 dist = 64;
2247 else
2248 dist = (dist + 32) & ~63;
2250 else
2252 /* for horizontal anti-aliased hinting, we adopt a more subtle */
2253 /* approach: we strengthen small stems, round stems whose size */
2254 /* is between 1 and 2 pixels to an integer, otherwise nothing */
2256 if (dist < 48)
2257 dist = (dist + 64) >> 1;
2259 else if (dist < 128)
2261 /* we only round to an integer width if the corresponding */
2262 /* distortion is less than 1/4 pixel -- otherwise, this */
2263 /* makes everything worse since the diagonals, which are */
2264 /* not hinted, appear a lot bolder or thinner than the */
2265 /* vertical stems */
2267 FT_Pos delta;
2270 dist = (dist + 22) & ~63;
2271 delta = dist - org_dist;
2272 if (delta < 0)
2273 delta = -delta;
2275 if (delta >= 16)
2277 dist = org_dist;
2278 if (dist < 48)
2279 dist = (dist + 64) >> 1;
2282 else
2283 /* round otherwise to prevent color fringes in LCD mode */
2284 dist = (dist + 32) & ~63;
2289 Done_Width:
2290 if (sign)
2291 dist = -dist;
2293 return dist;
2297 /* align one stem edge relative to the previous stem edge */
2299 static void
2300 ta_latin_align_linked_edge(TA_GlyphHints hints,
2301 TA_Dimension dim,
2302 TA_Edge base_edge,
2303 TA_Edge stem_edge)
2305 FT_Pos dist = stem_edge->opos - base_edge->opos;
2307 FT_Pos fitted_width = ta_latin_compute_stem_width(
2308 hints, dim, dist,
2309 base_edge->flags,
2310 stem_edge->flags);
2313 stem_edge->pos = base_edge->pos + fitted_width;
2315 TA_LOG((" LINK: edge %d (opos=%.2f) linked to %.2f,"
2316 " dist was %.2f, now %.2f\n",
2317 stem_edge - hints->axis[dim].edges, stem_edge->opos / 64.0,
2318 stem_edge->pos / 64.0, dist / 64.0, fitted_width / 64.0));
2320 if (hints->recorder)
2321 hints->recorder(ta_link, hints, dim,
2322 base_edge, stem_edge, NULL, NULL, NULL);
2326 /* shift the coordinates of the `serif' edge by the same amount */
2327 /* as the corresponding `base' edge has been moved already */
2329 static void
2330 ta_latin_align_serif_edge(TA_GlyphHints hints,
2331 TA_Edge base,
2332 TA_Edge serif)
2334 FT_UNUSED(hints);
2336 serif->pos = base->pos + (serif->opos - base->opos);
2340 /* the main grid-fitting routine */
2342 void
2343 ta_latin_hint_edges(TA_GlyphHints hints,
2344 TA_Dimension dim)
2346 TA_AxisHints axis = &hints->axis[dim];
2348 TA_Edge edges = axis->edges;
2349 TA_Edge edge_limit = edges + axis->num_edges;
2350 FT_PtrDist n_edges;
2351 TA_Edge edge;
2353 TA_Edge anchor = NULL;
2354 FT_Int has_serifs = 0;
2356 #ifdef TA_DEBUG
2357 FT_UInt num_actions = 0;
2358 #endif
2360 TA_LOG(("latin %s edge hinting (style `%s')\n",
2361 dim == TA_DIMENSION_VERT ? "horizontal" : "vertical",
2362 ta_style_names[hints->metrics->style_class->style]));
2364 /* we begin by aligning all stems relative to the blue zone if needed -- */
2365 /* that's only for horizontal edges */
2367 if (dim == TA_DIMENSION_VERT
2368 && TA_HINTS_DO_BLUES(hints))
2370 for (edge = edges; edge < edge_limit; edge++)
2372 TA_Width blue;
2373 TA_Edge edge1, edge2; /* these edges form the stem to check */
2376 if (edge->flags & TA_EDGE_DONE)
2377 continue;
2379 edge1 = NULL;
2380 edge2 = edge->link;
2383 * If a stem contains both a neutral and a non-neutral blue zone,
2384 * skip the neutral one. Otherwise, outlines with different
2385 * directions might be incorrectly aligned at the same vertical
2386 * position.
2388 * If we have two neutral blue zones, skip one of them.
2390 if (edge->blue_edge && edge2 && edge2->blue_edge)
2392 FT_Byte neutral = edge->flags & TA_EDGE_NEUTRAL;
2393 FT_Byte neutral2 = edge2->flags & TA_EDGE_NEUTRAL;
2396 if ((neutral && neutral2) || neutral2)
2398 edge2->blue_edge = NULL;
2399 edge2->flags &= ~TA_EDGE_NEUTRAL;
2401 else if (neutral)
2403 edge->blue_edge = NULL;
2404 edge->flags &= ~TA_EDGE_NEUTRAL;
2408 blue = edge->blue_edge;
2409 if (blue)
2410 edge1 = edge;
2412 /* flip edges if the other edge is aligned to a blue zone */
2413 else if (edge2 && edge2->blue_edge)
2415 blue = edge2->blue_edge;
2416 edge1 = edge2;
2417 edge2 = edge;
2420 if (!edge1)
2421 continue;
2423 #ifdef TA_DEBUG
2424 if (!anchor)
2425 TA_LOG((" BLUE_ANCHOR: edge %d (opos=%.2f) snapped to %.2f,"
2426 " was %.2f (anchor=edge %d)\n",
2427 edge1 - edges, edge1->opos / 64.0, blue->fit / 64.0,
2428 edge1->pos / 64.0, edge - edges));
2429 else
2430 TA_LOG((" BLUE: edge %d (opos=%.2f) snapped to %.2f, was %.2f\n",
2431 edge1 - edges, edge1->opos / 64.0, blue->fit / 64.0,
2432 edge1->pos / 64.0));
2434 num_actions++;
2435 #endif
2437 edge1->pos = blue->fit;
2438 edge1->flags |= TA_EDGE_DONE;
2440 if (hints->recorder)
2442 if (!anchor)
2443 hints->recorder(ta_blue_anchor, hints, dim,
2444 edge1, edge, NULL, NULL, NULL);
2445 else
2446 hints->recorder(ta_blue, hints, dim,
2447 edge1, NULL, NULL, NULL, NULL);
2450 if (edge2 && !edge2->blue_edge)
2452 ta_latin_align_linked_edge(hints, dim, edge1, edge2);
2453 edge2->flags |= TA_EDGE_DONE;
2455 #ifdef TA_DEBUG
2456 num_actions++;
2457 #endif
2460 if (!anchor)
2461 anchor = edge;
2465 /* now we align all other stem edges, */
2466 /* trying to maintain the relative order of stems in the glyph */
2467 for (edge = edges; edge < edge_limit; edge++)
2469 TA_Edge edge2;
2472 if (edge->flags & TA_EDGE_DONE)
2473 continue;
2475 /* skip all non-stem edges */
2476 edge2 = edge->link;
2477 if (!edge2)
2479 has_serifs++;
2480 continue;
2483 /* now align the stem */
2485 /* this should not happen, but it's better to be safe */
2486 if (edge2->blue_edge)
2488 TA_LOG((" ASSERTION FAILED for edge %d\n", edge2 - edges));
2490 ta_latin_align_linked_edge(hints, dim, edge2, edge);
2491 edge->flags |= TA_EDGE_DONE;
2493 #ifdef TA_DEBUG
2494 num_actions++;
2495 #endif
2496 continue;
2499 if (!anchor)
2501 /* if we reach this if clause, no stem has been aligned yet */
2503 FT_Pos org_len, org_center, cur_len;
2504 FT_Pos cur_pos1, error1, error2, u_off, d_off;
2507 org_len = edge2->opos - edge->opos;
2508 cur_len = ta_latin_compute_stem_width(hints, dim, org_len,
2509 edge->flags, edge2->flags);
2511 /* some voodoo to specially round edges for small stem widths; */
2512 /* the idea is to align the center of a stem, */
2513 /* then shifting the stem edges to suitable positions */
2514 if (cur_len <= 64)
2516 /* width <= 1px */
2517 u_off = 32;
2518 d_off = 32;
2520 else
2522 /* 1px < width < 1.5px */
2523 u_off = 38;
2524 d_off = 26;
2527 if (cur_len < 96)
2529 org_center = edge->opos + (org_len >> 1);
2530 cur_pos1 = TA_PIX_ROUND(org_center);
2532 error1 = org_center - (cur_pos1 - u_off);
2533 if (error1 < 0)
2534 error1 = -error1;
2536 error2 = org_center - (cur_pos1 + d_off);
2537 if (error2 < 0)
2538 error2 = -error2;
2540 if (error1 < error2)
2541 cur_pos1 -= u_off;
2542 else
2543 cur_pos1 += d_off;
2545 edge->pos = cur_pos1 - cur_len / 2;
2546 edge2->pos = edge->pos + cur_len;
2548 else
2549 edge->pos = TA_PIX_ROUND(edge->opos);
2551 anchor = edge;
2552 edge->flags |= TA_EDGE_DONE;
2554 TA_LOG((" ANCHOR: edge %d (opos=%.2f) and %d (opos=%.2f)"
2555 " snapped to %.2f and %.2f\n",
2556 edge - edges, edge->opos / 64.0,
2557 edge2 - edges, edge2->opos / 64.0,
2558 edge->pos / 64.0, edge2->pos / 64.0));
2560 if (hints->recorder)
2561 hints->recorder(ta_anchor, hints, dim,
2562 edge, edge2, NULL, NULL, NULL);
2564 ta_latin_align_linked_edge(hints, dim, edge, edge2);
2566 #ifdef TA_DEBUG
2567 num_actions += 2;
2568 #endif
2570 else
2572 FT_Pos org_pos, org_len, org_center, cur_len;
2573 FT_Pos cur_pos1, cur_pos2, delta1, delta2;
2576 org_pos = anchor->pos + (edge->opos - anchor->opos);
2577 org_len = edge2->opos - edge->opos;
2578 org_center = org_pos + (org_len >> 1);
2580 cur_len = ta_latin_compute_stem_width(hints, dim, org_len,
2581 edge->flags, edge2->flags);
2583 if (edge2->flags & TA_EDGE_DONE)
2585 TA_LOG((" ADJUST: edge %d (pos=%.2f) moved to %.2f\n",
2586 edge - edges, edge->pos / 64.0,
2587 (edge2->pos - cur_len) / 64.0));
2589 edge->pos = edge2->pos - cur_len;
2591 if (hints->recorder)
2593 TA_Edge bound = NULL;
2596 if (edge > edges)
2597 bound = &edge[-1];
2599 hints->recorder(ta_adjust, hints, dim,
2600 edge, edge2, NULL, bound, NULL);
2604 else if (cur_len < 96)
2606 FT_Pos u_off, d_off;
2609 cur_pos1 = TA_PIX_ROUND(org_center);
2611 if (cur_len <= 64)
2613 u_off = 32;
2614 d_off = 32;
2616 else
2618 u_off = 38;
2619 d_off = 26;
2622 delta1 = org_center - (cur_pos1 - u_off);
2623 if (delta1 < 0)
2624 delta1 = -delta1;
2626 delta2 = org_center - (cur_pos1 + d_off);
2627 if (delta2 < 0)
2628 delta2 = -delta2;
2630 if (delta1 < delta2)
2631 cur_pos1 -= u_off;
2632 else
2633 cur_pos1 += d_off;
2635 edge->pos = cur_pos1 - cur_len / 2;
2636 edge2->pos = cur_pos1 + cur_len / 2;
2638 TA_LOG((" STEM: edge %d (opos=%.2f) linked to %d (opos=%.2f)"
2639 " snapped to %.2f and %.2f\n",
2640 edge - edges, edge->opos / 64.0,
2641 edge2 - edges, edge2->opos / 64.0,
2642 edge->pos / 64.0, edge2->pos / 64.0));
2644 if (hints->recorder)
2646 TA_Edge bound = NULL;
2649 if (edge > edges)
2650 bound = &edge[-1];
2652 hints->recorder(ta_stem, hints, dim,
2653 edge, edge2, NULL, bound, NULL);
2657 else
2659 org_pos = anchor->pos + (edge->opos - anchor->opos);
2660 org_len = edge2->opos - edge->opos;
2661 org_center = org_pos + (org_len >> 1);
2663 cur_len = ta_latin_compute_stem_width(hints, dim, org_len,
2664 edge->flags, edge2->flags);
2666 cur_pos1 = TA_PIX_ROUND(org_pos);
2667 delta1 = cur_pos1 + (cur_len >> 1) - org_center;
2668 if (delta1 < 0)
2669 delta1 = -delta1;
2671 cur_pos2 = TA_PIX_ROUND(org_pos + org_len) - cur_len;
2672 delta2 = cur_pos2 + (cur_len >> 1) - org_center;
2673 if (delta2 < 0)
2674 delta2 = -delta2;
2676 edge->pos = (delta1 < delta2) ? cur_pos1 : cur_pos2;
2677 edge2->pos = edge->pos + cur_len;
2679 TA_LOG((" STEM: edge %d (opos=%.2f) linked to %d (opos=%.2f)"
2680 " snapped to %.2f and %.2f\n",
2681 edge - edges, edge->opos / 64.0,
2682 edge2 - edges, edge2->opos / 64.0,
2683 edge->pos / 64.0, edge2->pos / 64.0));
2685 if (hints->recorder)
2687 TA_Edge bound = NULL;
2690 if (edge > edges)
2691 bound = &edge[-1];
2693 hints->recorder(ta_stem, hints, dim,
2694 edge, edge2, NULL, bound, NULL);
2698 #ifdef TA_DEBUG
2699 num_actions++;
2700 #endif
2702 edge->flags |= TA_EDGE_DONE;
2703 edge2->flags |= TA_EDGE_DONE;
2705 if (edge > edges
2706 && edge->pos < edge[-1].pos)
2708 #ifdef TA_DEBUG
2709 TA_LOG((" BOUND: edge %d (pos=%.2f) moved to %.2f\n",
2710 edge - edges, edge->pos / 64.0, edge[-1].pos / 64.0));
2712 num_actions++;
2713 #endif
2715 edge->pos = edge[-1].pos;
2717 if (hints->recorder)
2718 hints->recorder(ta_bound, hints, dim,
2719 edge, &edge[-1], NULL, NULL, NULL);
2724 /* make sure that lowercase m's maintain their symmetry */
2726 /* In general, lowercase m's have six vertical edges if they are sans */
2727 /* serif, or twelve if they are with serifs. This implementation is */
2728 /* based on that assumption, and seems to work very well with most */
2729 /* faces. However, if for a certain face this assumption is not */
2730 /* true, the m is just rendered like before. In addition, any stem */
2731 /* correction will only be applied to symmetrical glyphs (even if the */
2732 /* glyph is not an m), so the potential for unwanted distortion is */
2733 /* relatively low. */
2735 /* we don't handle horizontal edges since we can't easily assure that */
2736 /* the third (lowest) stem aligns with the base line; it might end up */
2737 /* one pixel higher or lower */
2739 n_edges = edge_limit - edges;
2740 if (dim == TA_DIMENSION_HORZ
2741 && (n_edges == 6 || n_edges == 12))
2743 TA_Edge edge1, edge2, edge3;
2744 FT_Pos dist1, dist2, span, delta;
2747 if (n_edges == 6)
2749 edge1 = edges;
2750 edge2 = edges + 2;
2751 edge3 = edges + 4;
2753 else
2755 edge1 = edges + 1;
2756 edge2 = edges + 5;
2757 edge3 = edges + 9;
2760 dist1 = edge2->opos - edge1->opos;
2761 dist2 = edge3->opos - edge2->opos;
2763 span = dist1 - dist2;
2764 if (span < 0)
2765 span = -span;
2767 if (span < 8)
2769 delta = edge3->pos - (2 * edge2->pos - edge1->pos);
2770 edge3->pos -= delta;
2771 if (edge3->link)
2772 edge3->link->pos -= delta;
2774 /* move the serifs along with the stem */
2775 if (n_edges == 12)
2777 (edges + 8)->pos -= delta;
2778 (edges + 11)->pos -= delta;
2781 edge3->flags |= TA_EDGE_DONE;
2782 if (edge3->link)
2783 edge3->link->flags |= TA_EDGE_DONE;
2787 if (has_serifs || !anchor)
2789 /* now hint the remaining edges (serifs and single) */
2790 /* in order to complete our processing */
2791 for (edge = edges; edge < edge_limit; edge++)
2793 TA_Edge lower_bound = NULL;
2794 TA_Edge upper_bound = NULL;
2796 FT_Pos delta;
2799 if (edge->flags & TA_EDGE_DONE)
2800 continue;
2802 delta = 1000;
2804 if (edge->serif)
2806 delta = edge->serif->opos - edge->opos;
2807 if (delta < 0)
2808 delta = -delta;
2811 if (edge > edges)
2812 lower_bound = &edge[-1];
2814 if (edge + 1 < edge_limit
2815 && edge[1].flags & TA_EDGE_DONE)
2816 upper_bound = &edge[1];
2819 if (delta < 64 + 16)
2821 ta_latin_align_serif_edge(hints, edge->serif, edge);
2823 TA_LOG((" SERIF: edge %d (opos=%.2f) serif to %d (opos=%.2f)"
2824 " aligned to %.2f\n",
2825 edge - edges, edge->opos / 64.0,
2826 edge->serif - edges, edge->serif->opos / 64.0,
2827 edge->pos / 64.0));
2829 if (hints->recorder)
2830 hints->recorder(ta_serif, hints, dim,
2831 edge, NULL, NULL, lower_bound, upper_bound);
2833 else if (!anchor)
2835 edge->pos = TA_PIX_ROUND(edge->opos);
2836 anchor = edge;
2838 TA_LOG((" SERIF_ANCHOR: edge %d (opos=%.2f) snapped to %.2f\n",
2839 edge - edges, edge->opos / 64.0, edge->pos / 64.0));
2841 if (hints->recorder)
2842 hints->recorder(ta_serif_anchor, hints, dim,
2843 edge, NULL, NULL, lower_bound, upper_bound);
2845 else
2847 TA_Edge before, after;
2850 for (before = edge - 1; before >= edges; before--)
2851 if (before->flags & TA_EDGE_DONE)
2852 break;
2854 for (after = edge + 1; after < edge_limit; after++)
2855 if (after->flags & TA_EDGE_DONE)
2856 break;
2858 if (before >= edges && before < edge
2859 && after < edge_limit && after > edge)
2861 if (after->opos == before->opos)
2862 edge->pos = before->pos;
2863 else
2864 edge->pos = before->pos + FT_MulDiv(edge->opos - before->opos,
2865 after->pos - before->pos,
2866 after->opos - before->opos);
2868 TA_LOG((" SERIF_LINK1: edge %d (opos=%.2f) snapped to %.2f"
2869 " from %d (opos=%.2f)\n",
2870 edge - edges, edge->opos / 64.0,
2871 edge->pos / 64.0,
2872 before - edges, before->opos / 64.0));
2874 if (hints->recorder)
2875 hints->recorder(ta_serif_link1, hints, dim,
2876 edge, before, after, lower_bound, upper_bound);
2878 else
2880 edge->pos = anchor->pos + ((edge->opos - anchor->opos + 16) & ~31);
2881 TA_LOG((" SERIF_LINK2: edge %d (opos=%.2f) snapped to %.2f\n",
2882 edge - edges, edge->opos / 64.0, edge->pos / 64.0));
2884 if (hints->recorder)
2885 hints->recorder(ta_serif_link2, hints, dim,
2886 edge, NULL, NULL, lower_bound, upper_bound);
2890 #ifdef TA_DEBUG
2891 num_actions++;
2892 #endif
2893 edge->flags |= TA_EDGE_DONE;
2895 if (edge > edges
2896 && edge->pos < edge[-1].pos)
2898 #ifdef TA_DEBUG
2899 TA_LOG((" BOUND: edge %d (pos=%.2f) moved to %.2f\n",
2900 edge - edges, edge->pos / 64.0, edge[-1].pos / 64.0));
2901 num_actions++;
2902 #endif
2904 edge->pos = edge[-1].pos;
2906 if (hints->recorder)
2907 hints->recorder(ta_bound, hints, dim,
2908 edge, &edge[-1], NULL, NULL, NULL);
2911 if (edge + 1 < edge_limit
2912 && edge[1].flags & TA_EDGE_DONE
2913 && edge->pos > edge[1].pos)
2915 #ifdef TA_DEBUG
2916 TA_LOG((" BOUND: edge %d (pos=%.2f) moved to %.2f\n",
2917 edge - edges, edge->pos / 64.0, edge[1].pos / 64.0));
2919 num_actions++;
2920 #endif
2922 edge->pos = edge[1].pos;
2924 if (hints->recorder)
2925 hints->recorder(ta_bound, hints, dim,
2926 edge, &edge[1], NULL, NULL, NULL);
2931 #ifdef TA_DEBUG
2932 if (!num_actions)
2933 TA_LOG((" (none)\n"));
2934 TA_LOG(("\n"));
2935 #endif
2939 /* apply the complete hinting algorithm to a latin glyph */
2941 static FT_Error
2942 ta_latin_hints_apply(TA_GlyphHints hints,
2943 FT_Outline* outline,
2944 TA_LatinMetrics metrics)
2946 FT_Error error;
2947 int dim;
2949 TA_LatinAxis axis;
2952 error = ta_glyph_hints_reload(hints, outline);
2953 if (error)
2954 goto Exit;
2956 /* analyze glyph outline */
2957 #ifdef TA_CONFIG_OPTION_USE_WARPER
2958 if (metrics->root.scaler.render_mode == FT_RENDER_MODE_LIGHT
2959 || TA_HINTS_DO_HORIZONTAL(hints))
2960 #else
2961 if (TA_HINTS_DO_HORIZONTAL(hints))
2962 #endif
2964 axis = &metrics->axis[TA_DIMENSION_HORZ];
2965 error = ta_latin_hints_detect_features(hints,
2966 axis->width_count,
2967 axis->widths,
2968 TA_DIMENSION_HORZ);
2969 if (error)
2970 goto Exit;
2973 if (TA_HINTS_DO_VERTICAL(hints))
2975 axis = &metrics->axis[TA_DIMENSION_VERT];
2976 error = ta_latin_hints_detect_features(hints,
2977 axis->width_count,
2978 axis->widths,
2979 TA_DIMENSION_VERT);
2980 if (error)
2981 goto Exit;
2983 ta_latin_hints_compute_blue_edges(hints, metrics);
2986 /* grid-fit the outline */
2987 for (dim = 0; dim < TA_DIMENSION_MAX; dim++)
2989 #ifdef TA_CONFIG_OPTION_USE_WARPER
2990 if (dim == TA_DIMENSION_HORZ
2991 && metrics->root.scaler.render_mode == FT_RENDER_MODE_LIGHT)
2993 TA_WarperRec warper;
2994 FT_Fixed scale;
2995 FT_Pos delta;
2998 ta_warper_compute(&warper, hints, (TA_Dimension)dim, &scale, &delta);
2999 ta_glyph_hints_scale_dim(hints, (TA_Dimension)dim, scale, delta);
3001 continue;
3003 #endif
3005 if ((dim == TA_DIMENSION_HORZ && TA_HINTS_DO_HORIZONTAL(hints))
3006 || (dim == TA_DIMENSION_VERT && TA_HINTS_DO_VERTICAL(hints)))
3008 ta_latin_hint_edges(hints, (TA_Dimension)dim);
3009 ta_glyph_hints_align_edge_points(hints, (TA_Dimension)dim);
3010 ta_glyph_hints_align_strong_points(hints, (TA_Dimension)dim);
3011 ta_glyph_hints_align_weak_points(hints, (TA_Dimension)dim);
3015 ta_glyph_hints_save(hints, outline);
3017 Exit:
3018 return error;
3022 const TA_WritingSystemClassRec ta_latin_writing_system_class =
3024 TA_WRITING_SYSTEM_LATIN,
3026 sizeof (TA_LatinMetricsRec),
3028 (TA_WritingSystem_InitMetricsFunc)ta_latin_metrics_init,
3029 (TA_WritingSystem_ScaleMetricsFunc)ta_latin_metrics_scale,
3030 (TA_WritingSystem_DoneMetricsFunc)NULL,
3032 (TA_WritingSystem_InitHintsFunc)ta_latin_hints_init,
3033 (TA_WritingSystem_ApplyHintsFunc)ta_latin_hints_apply
3036 /* end of talatin.c */