Synchronize with FreeType.
[ttfautohint.git] / lib / talatin.c
blob55236e73c177c623b7a92dc4e0589b4cf55714c8
1 /* talatin.c */
3 /*
4 * Copyright (C) 2011-2018 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 int dim, dim_max;
69 TA_LatinMetricsRec dummy[1];
70 TA_Scaler scaler = &dummy->root.scaler;
72 TA_StyleClass style_class = metrics->root.style_class;
73 TA_ScriptClass script_class = ta_script_classes[style_class->script];
75 FONT* font = metrics->root.globals->font;
77 void* shaper_buf;
78 const char* p;
80 #ifdef TA_DEBUG
81 FT_ULong ch = 0;
82 #endif
85 /* if the user provides horizontal stem widths, */
86 /* apply the algorithm only along the horizontal axis */
87 TA_control_set_stem_widths(metrics, font);
88 if (metrics->axis[TA_DIMENSION_VERT].width_count)
90 TA_LatinAxis axis = &metrics->axis[TA_DIMENSION_VERT];
93 dim_max = TA_DIMENSION_VERT;
95 axis->standard_width = axis->widths[0].org;
96 axis->edge_distance_threshold = axis->standard_width / 5;
97 axis->extra_light = 0;
99 #ifdef TA_DEBUG
101 FT_UInt i;
104 TA_LOG_GLOBAL(("horizontal widths (user provided):\n" ));
106 TA_LOG_GLOBAL((" %d (standard)", axis->standard_width));
107 for (i = 1; i < axis->width_count; i++)
108 TA_LOG_GLOBAL((" %d", axis->widths[i].org));
110 TA_LOG_GLOBAL(("\n"));
112 #endif
115 else
116 dim_max = TA_DIMENSION_MAX;
118 if (!use_cmap)
119 goto Exit;
121 p = script_class->standard_charstring;
122 shaper_buf = ta_shaper_buf_create(face);
125 * We check a list of standard characters to catch features like
126 * `c2sc' (small caps from caps) that don't contain lowercase letters
127 * by definition, or other features that mainly operate on numerals.
128 * The first match wins.
131 glyph_index = 0;
132 while (*p)
134 unsigned int num_idx;
136 #ifdef TA_DEBUG
137 const char* p_old;
138 #endif
141 while (*p == ' ')
142 p++;
144 #ifdef TA_DEBUG
145 p_old = p;
146 GET_UTF8_CHAR(ch, p_old);
147 #endif
149 /* reject input that maps to more than a single glyph */
150 p = ta_shaper_get_cluster(p, &metrics->root, shaper_buf, &num_idx);
151 if (num_idx > 1)
152 continue;
154 /* otherwise exit loop if we have a result */
155 glyph_index = ta_shaper_get_elem(&metrics->root,
156 shaper_buf,
158 NULL,
159 NULL);
160 if (glyph_index)
161 break;
164 ta_shaper_buf_destroy(face, shaper_buf);
166 if (!glyph_index)
168 TA_LOG_GLOBAL(("no standard character\n"));
169 goto Exit;
172 TA_LOG_GLOBAL(("standard character: U+%04lX (glyph index %d)\n",
173 ch, glyph_index));
175 error = FT_Load_Glyph(face, glyph_index, FT_LOAD_NO_SCALE);
176 if (error || face->glyph->outline.n_points <= 0)
177 goto Exit;
179 memset(dummy, 0, sizeof (TA_LatinMetricsRec));
181 dummy->units_per_em = metrics->units_per_em;
183 scaler->x_scale = 0x10000L;
184 scaler->y_scale = 0x10000L;
185 scaler->x_delta = 0;
186 scaler->y_delta = 0;
188 scaler->face = face;
189 scaler->render_mode = FT_RENDER_MODE_NORMAL;
190 scaler->flags = 0;
192 ta_glyph_hints_rescale(hints, (TA_StyleMetrics)dummy);
194 error = ta_glyph_hints_reload(hints, &face->glyph->outline);
195 if (error)
196 goto Exit;
198 for (dim = 0; dim < dim_max; dim++)
200 TA_LatinAxis axis = &metrics->axis[dim];
201 TA_AxisHints axhints = &hints->axis[dim];
203 TA_Segment seg, limit, link;
204 FT_UInt num_widths = 0;
207 error = ta_latin_hints_compute_segments(hints, (TA_Dimension)dim);
208 if (error)
209 goto Exit;
212 * We assume that the glyphs selected for the stem width
213 * computation are `featureless' enough so that the linking
214 * algorithm works fine without adjustments of its scoring
215 * function.
217 ta_latin_hints_link_segments(hints, 0, NULL, (TA_Dimension)dim);
219 seg = axhints->segments;
220 limit = seg + axhints->num_segments;
222 for (; seg < limit; seg++)
224 link = seg->link;
226 /* we only consider stem segments there! */
227 if (link
228 && link->link == seg
229 && link > seg)
231 FT_Pos dist;
234 dist = seg->pos - link->pos;
235 if (dist < 0)
236 dist = -dist;
238 if (num_widths < TA_LATIN_MAX_WIDTHS)
239 axis->widths[num_widths++].org = dist;
243 /* this also replaces multiple almost identical stem widths */
244 /* with a single one (the value 100 is heuristic) */
245 ta_sort_and_quantize_widths(&num_widths, axis->widths,
246 dummy->units_per_em / 100);
247 axis->width_count = num_widths;
250 Exit:
251 for (dim = 0; dim < dim_max; dim++)
253 TA_LatinAxis axis = &metrics->axis[dim];
254 FT_Pos stdw;
257 if (!axis->width_count)
259 /* if we have no standard characters, */
260 /* use `fallback-stem-width', if available, */
261 /* or a default width (value 50 is heuristic) */
262 if (dim == TA_DIMENSION_VERT && font->fallback_stem_width)
264 stdw = (FT_Pos)font->fallback_stem_width;
265 TA_LOG_GLOBAL(("using horizontal fallback stem width\n"));
267 else
269 stdw = TA_LATIN_CONSTANT(metrics, 50);
270 TA_LOG_GLOBAL(("using a default %s stem width\n",
271 dim == TA_DIMENSION_VERT ? "horizontal"
272 : "vertical"));
275 axis->width_count++;
276 axis->widths[0].org = stdw;
279 stdw = axis->widths[0].org;
281 /* let's try 20% of the smallest width */
282 axis->edge_distance_threshold = stdw / 5;
283 axis->standard_width = stdw;
284 axis->extra_light = 0;
286 #ifdef TA_DEBUG
288 FT_UInt i;
291 TA_LOG_GLOBAL(("%s widths:\n",
292 dim == TA_DIMENSION_VERT ? "horizontal"
293 : "vertical"));
295 TA_LOG_GLOBAL((" %d (standard)", axis->standard_width));
296 for (i = 1; i < axis->width_count; i++)
297 TA_LOG_GLOBAL((" %d", axis->widths[i].org));
299 TA_LOG_GLOBAL(("\n"));
301 #endif
305 TA_LOG_GLOBAL(("\n"));
307 ta_glyph_hints_done(hints);
311 static void
312 ta_latin_sort_blue(FT_UInt count,
313 TA_LatinBlue* table)
315 FT_UInt i;
316 FT_UInt j;
317 TA_LatinBlue swap;
320 /* we sort from bottom to top */
321 for (i = 1; i < count; i++)
323 for (j = i; j > 0; j--)
325 FT_Pos a, b;
328 if (table[j - 1]->flags & (TA_LATIN_BLUE_TOP
329 | TA_LATIN_BLUE_SUB_TOP))
330 a = table[j - 1]->ref.org;
331 else
332 a = table[j - 1]->shoot.org;
334 if (table[j]->flags & (TA_LATIN_BLUE_TOP
335 | TA_LATIN_BLUE_SUB_TOP))
336 b = table[j]->ref.org;
337 else
338 b = table[j]->shoot.org;
340 if (b >= a)
341 break;
343 swap = table[j];
344 table[j] = table[j - 1];
345 table[j - 1] = swap;
351 /* find all blue zones; flat segments give the reference points, */
352 /* round segments the overshoot positions */
354 static void
355 ta_latin_metrics_init_blues(TA_LatinMetrics metrics,
356 FT_Face face)
358 FT_Pos flats[TA_BLUE_STRING_MAX_LEN];
359 FT_Pos rounds[TA_BLUE_STRING_MAX_LEN];
360 FT_UInt num_flats;
361 FT_UInt num_rounds;
363 TA_LatinBlue blue;
364 FT_Error error;
365 TA_LatinAxis axis = &metrics->axis[TA_DIMENSION_VERT];
366 FT_Outline outline;
368 TA_StyleClass sc = metrics->root.style_class;
370 TA_Blue_Stringset bss = sc->blue_stringset;
371 const TA_Blue_StringRec* bs = &ta_blue_stringsets[bss];
373 FT_Pos flat_threshold = FLAT_THRESHOLD(metrics->units_per_em);
375 void* shaper_buf;
378 /* we walk over the blue character strings as specified in the */
379 /* style's entry in the `ta_blue_stringset' array */
381 TA_LOG_GLOBAL(("latin blue zones computation\n"
382 "============================\n"
383 "\n"));
385 shaper_buf = ta_shaper_buf_create(face);
387 for (; bs->string != TA_BLUE_STRING_MAX; bs++)
389 const char* p = &ta_blue_strings[bs->string];
390 FT_Pos* blue_ref;
391 FT_Pos* blue_shoot;
392 FT_Pos ascender;
393 FT_Pos descender;
396 #ifdef TA_DEBUG
398 FT_Bool have_flag = 0;
401 TA_LOG_GLOBAL(("blue zone %d", axis->blue_count));
403 if (bs->properties)
405 TA_LOG_GLOBAL((" ("));
407 if (TA_LATIN_IS_TOP_BLUE(bs))
409 TA_LOG_GLOBAL(("top"));
410 have_flag = 1;
412 else if (TA_LATIN_IS_SUB_TOP_BLUE(bs))
414 TA_LOG_GLOBAL(("sub top"));
415 have_flag = 1;
418 if (TA_LATIN_IS_NEUTRAL_BLUE(bs))
420 if (have_flag)
421 TA_LOG_GLOBAL((", "));
422 TA_LOG_GLOBAL(("neutral"));
423 have_flag = 1;
426 if (TA_LATIN_IS_X_HEIGHT_BLUE(bs))
428 if (have_flag)
429 TA_LOG_GLOBAL((", "));
430 TA_LOG_GLOBAL(("small top"));
431 have_flag = 1;
434 if (TA_LATIN_IS_LONG_BLUE(bs))
436 if (have_flag)
437 TA_LOG_GLOBAL((", "));
438 TA_LOG_GLOBAL(("long"));
441 TA_LOG_GLOBAL((")"));
444 TA_LOG_GLOBAL((":\n"));
446 #endif /* TA_DEBUG */
448 num_flats = 0;
449 num_rounds = 0;
450 ascender = 0;
451 descender = 0;
453 while (*p)
455 FT_ULong glyph_index;
456 FT_Long y_offset;
457 FT_Int best_point, best_contour_first, best_contour_last;
458 FT_Vector* points;
460 FT_Pos best_y_extremum; /* same as points.y */
461 FT_Bool best_round = 0;
463 unsigned int i, num_idx;
465 #ifdef TA_DEBUG
466 const char* p_old;
467 FT_ULong ch;
468 #endif
471 while (*p == ' ')
472 p++;
474 #ifdef TA_DEBUG
475 p_old = p;
476 GET_UTF8_CHAR(ch, p_old);
477 #endif
479 p = ta_shaper_get_cluster(p, &metrics->root, shaper_buf, &num_idx);
481 if (!num_idx)
483 TA_LOG_GLOBAL((" U+%04lX unavailable\n", ch));
484 continue;
487 if (TA_LATIN_IS_TOP_BLUE(bs))
488 best_y_extremum = FT_INT_MIN;
489 else
490 best_y_extremum = FT_INT_MAX;
492 /* iterate over all glyph elements of the character cluster */
493 /* and get the data of the `biggest' one */
494 for (i = 0; i < num_idx; i++)
496 FT_Pos best_y;
497 FT_Bool round = 0;
500 /* load the character in the face -- skip unknown or empty ones */
501 glyph_index = ta_shaper_get_elem(&metrics->root,
502 shaper_buf,
504 NULL,
505 &y_offset);
506 if (glyph_index == 0)
508 TA_LOG_GLOBAL((" U+%04lX unavailable\n", ch));
509 continue;
512 error = FT_Load_Glyph(face, glyph_index, FT_LOAD_NO_SCALE);
513 outline = face->glyph->outline;
514 /* reject glyphs that don't produce any rendering */
515 if (error || outline.n_points <= 2)
517 #ifdef TA_DEBUG
518 if (num_idx == 1)
519 TA_LOG_GLOBAL((" U+%04lX contains no (usable) outlines\n", ch));
520 else
521 TA_LOG_GLOBAL((" component %d of cluster starting with U+%04lX"
522 " contains no (usable) outlines\n", i, ch));
523 #endif
524 continue;
527 /* now compute min or max point indices and coordinates */
528 points = outline.points;
529 best_point = -1;
530 best_y = 0; /* make compiler happy */
531 best_contour_first = 0; /* ditto */
532 best_contour_last = 0; /* ditto */
535 FT_Int nn;
536 FT_Int first = 0;
537 FT_Int last = -1;
540 for (nn = 0; nn < outline.n_contours; first = last + 1, nn++)
542 FT_Int old_best_point = best_point;
543 FT_Int pp;
546 last = outline.contours[nn];
548 /* avoid single-point contours since they are never */
549 /* rasterized; in some fonts, they correspond to mark */
550 /* attachment points that are way outside of the glyph's */
551 /* real outline */
552 if (last <= first)
553 continue;
555 if (TA_LATIN_IS_TOP_BLUE(bs)
556 || TA_LATIN_IS_SUB_TOP_BLUE(bs))
558 for (pp = first; pp <= last; pp++)
560 if (best_point < 0
561 || points[pp].y > best_y)
563 best_point = pp;
564 best_y = points[pp].y;
565 ascender = TA_MAX(ascender, best_y + y_offset);
567 else
568 descender = TA_MIN(descender, points[pp].y + y_offset);
571 else
573 for (pp = first; pp <= last; pp++)
575 if (best_point < 0
576 || points[pp].y < best_y)
578 best_point = pp;
579 best_y = points[pp].y;
580 descender = TA_MIN(descender, best_y + y_offset);
582 else
583 ascender = TA_MAX(ascender, points[pp].y + y_offset);
587 if (best_point != old_best_point)
589 best_contour_first = first;
590 best_contour_last = last;
595 /* now check whether the point belongs to a straight or round */
596 /* segment; we first need to find in which contour the extremum */
597 /* lies, then inspect its previous and next points */
598 if (best_point >= 0)
600 FT_Pos best_x = points[best_point].x;
601 FT_Int prev, next;
602 FT_Int best_segment_first, best_segment_last;
603 FT_Int best_on_point_first, best_on_point_last;
604 FT_Pos dist;
607 best_segment_first = best_point;
608 best_segment_last = best_point;
610 if (FT_CURVE_TAG(outline.tags[best_point]) == FT_CURVE_TAG_ON)
612 best_on_point_first = best_point;
613 best_on_point_last = best_point;
615 else
617 best_on_point_first = -1;
618 best_on_point_last = -1;
621 /* look for the previous and next points on the contour */
622 /* that are not on the same Y coordinate, then threshold */
623 /* the `closeness'... */
624 prev = best_point;
625 next = prev;
629 if (prev > best_contour_first)
630 prev--;
631 else
632 prev = best_contour_last;
634 dist = TA_ABS(points[prev].y - best_y);
635 /* accept a small distance or a small angle (both values are */
636 /* heuristic; value 20 corresponds to approx. 2.9 degrees) */
637 if (dist > 5)
638 if (TA_ABS(points[prev].x - best_x) <= 20 * dist)
639 break;
641 best_segment_first = prev;
643 if (FT_CURVE_TAG(outline.tags[prev]) == FT_CURVE_TAG_ON)
645 best_on_point_first = prev;
646 if (best_on_point_last < 0)
647 best_on_point_last = prev;
650 } while (prev != best_point);
654 if (next < best_contour_last)
655 next++;
656 else
657 next = best_contour_first;
659 dist = TA_ABS(points[next].y - best_y);
660 if (dist > 5)
661 if (TA_ABS(points[next].x - best_x) <= 20 * dist)
662 break;
664 best_segment_last = next;
666 if (FT_CURVE_TAG(outline.tags[next]) == FT_CURVE_TAG_ON)
668 best_on_point_last = next;
669 if (best_on_point_first < 0)
670 best_on_point_first = next;
673 } while (next != best_point);
675 if (TA_LATIN_IS_LONG_BLUE(bs))
677 /* If this flag is set, we have an additional constraint to */
678 /* get the blue zone distance: Find a segment of the topmost */
679 /* (or bottommost) contour that is longer than a heuristic */
680 /* threshold. This ensures that small bumps in the outline */
681 /* are ignored (for example, the `vertical serifs' found in */
682 /* many Hebrew glyph designs). */
684 /* If this segment is long enough, we are done. Otherwise, */
685 /* search the segment next to the extremum that is long */
686 /* enough, has the same direction, and a not too large */
687 /* vertical distance from the extremum. Note that the */
688 /* algorithm doesn't check whether the found segment is */
689 /* actually the one (vertically) nearest to the extremum. */
691 /* heuristic threshold value */
692 FT_Pos length_threshold = metrics->units_per_em / 25;
695 dist = TA_ABS(points[best_segment_last].x -
696 points[best_segment_first].x);
698 if (dist < length_threshold
699 && best_segment_last - best_segment_first + 2 <=
700 best_contour_last - best_contour_first)
702 /* heuristic threshold value */
703 FT_Pos height_threshold = metrics->units_per_em / 4;
705 FT_Int first;
706 FT_Int last;
707 FT_Bool hit;
709 /* we intentionally declare these two variables */
710 /* outside of the loop since various compilers emit */
711 /* incorrect warning messages otherwise, talking about */
712 /* `possibly uninitialized variables' */
713 FT_Int p_first = 0; /* make compiler happy */
714 FT_Int p_last = 0;
716 FT_Bool left2right;
719 /* compute direction */
720 prev = best_point;
724 if (prev > best_contour_first)
725 prev--;
726 else
727 prev = best_contour_last;
729 if (points[prev].x != best_x)
730 break;
731 } while (prev != best_point);
733 /* skip glyph for the degenerate case */
734 if (prev == best_point)
735 continue;
737 left2right = FT_BOOL(points[prev].x < points[best_point].x);
739 first = best_segment_last;
740 last = first;
741 hit = 0;
745 FT_Bool l2r;
746 FT_Pos d;
749 if (!hit)
751 /* no hit; adjust first point */
752 first = last;
754 /* also adjust first and last on point */
755 if (FT_CURVE_TAG(outline.tags[first]) == FT_CURVE_TAG_ON)
757 p_first = first;
758 p_last = first;
760 else
762 p_first = -1;
763 p_last = -1;
766 hit = 1;
769 if (last < best_contour_last)
770 last++;
771 else
772 last = best_contour_first;
774 if (TA_ABS(best_y - points[first].y) > height_threshold)
776 /* vertical distance too large */
777 hit = 0;
778 continue;
781 /* same test as above */
782 dist = TA_ABS(points[last].y - points[first].y);
783 if (dist > 5)
784 if (TA_ABS(points[last].x - points[first].x) <= 20 * dist)
786 hit = 0;
787 continue;
790 if (FT_CURVE_TAG(outline.tags[last]) == FT_CURVE_TAG_ON)
792 p_last = last;
793 if (p_first < 0)
794 p_first = last;
797 l2r = FT_BOOL(points[first].x < points[last].x);
798 d = TA_ABS(points[last].x - points[first].x);
800 if (l2r == left2right
801 && d >= length_threshold)
803 /* all constraints are met; update segment after */
804 /* finding its end */
807 if (last < best_contour_last)
808 last++;
809 else
810 last = best_contour_first;
812 d = TA_ABS(points[last].y - points[first].y);
813 if (d > 5)
814 if (TA_ABS(points[next].x - points[first].x) <=
815 20 * dist)
817 if (last > best_contour_first)
818 last--;
819 else
820 last = best_contour_last;
821 break;
824 p_last = last;
826 if (FT_CURVE_TAG(outline.tags[last]) == FT_CURVE_TAG_ON)
828 p_last = last;
829 if (p_first < 0)
830 p_first = last;
832 } while (last != best_segment_first);
834 best_y = points[first].y;
836 best_segment_first = first;
837 best_segment_last = last;
839 best_on_point_first = p_first;
840 best_on_point_last = p_last;
842 break;
844 } while (last != best_segment_first);
849 * for computing blue zones, we add the y offset as returned
850 * by the currently used OpenType feature --
851 * for example, superscript glyphs might be identical
852 * to subscript glyphs with a vertical shift
854 best_y += y_offset;
856 #ifdef TA_DEBUG
857 if (num_idx == 1)
858 TA_LOG_GLOBAL((" U+%04lX: best_y = %5ld", ch, best_y));
859 else
860 TA_LOG_GLOBAL((" component %d of cluster starting with U+%04lX:"
861 " best_y = %5ld", i, ch, best_y));
862 #endif
865 * now set the `round' flag depending on the segment's kind:
867 * - if the horizontal distance between the first and last
868 * `on' point is larger than a heuristic threshold
869 * we have a flat segment
870 * - if either the first or the last point of the segment is
871 * an `off' point, the segment is round, otherwise it is
872 * flat
874 if (best_on_point_first >= 0
875 && best_on_point_last >= 0
876 && (TA_ABS(points[best_on_point_last].x
877 - points[best_on_point_first].x))
878 > flat_threshold)
879 round = 0;
880 else
881 round = FT_BOOL(FT_CURVE_TAG(outline.tags[best_segment_first])
882 != FT_CURVE_TAG_ON
883 || FT_CURVE_TAG(outline.tags[best_segment_last])
884 != FT_CURVE_TAG_ON);
886 if (round && TA_LATIN_IS_NEUTRAL_BLUE(bs))
888 /* only use flat segments for a neutral blue zone */
889 TA_LOG_GLOBAL((" (round, skipped)\n"));
890 continue;
893 TA_LOG_GLOBAL((" (%s)\n", round ? "round" : "flat"));
896 if (TA_LATIN_IS_TOP_BLUE(bs))
898 if (best_y > best_y_extremum)
900 best_y_extremum = best_y;
901 best_round = round;
904 else
906 if (best_y < best_y_extremum)
908 best_y_extremum = best_y;
909 best_round = round;
913 } /* end for loop */
915 if (!(best_y_extremum == FT_INT_MIN
916 || best_y_extremum == FT_INT_MAX))
918 if (best_round)
919 rounds[num_rounds++] = best_y_extremum;
920 else
921 flats[num_flats++] = best_y_extremum;
924 } /* end while loop */
926 if (num_flats == 0 && num_rounds == 0)
928 /* we couldn't find a single glyph to compute this blue zone, */
929 /* we will simply ignore it then */
930 TA_LOG_GLOBAL((" empty\n"));
931 continue;
934 /* we have computed the contents of the `rounds' and `flats' tables, */
935 /* now determine the reference and overshoot position of the blue -- */
936 /* we simply take the median value after a simple sort */
937 ta_sort_pos(num_rounds, rounds);
938 ta_sort_pos(num_flats, flats);
940 blue = &axis->blues[axis->blue_count];
941 blue_ref = &blue->ref.org;
942 blue_shoot = &blue->shoot.org;
944 axis->blue_count++;
946 if (num_flats == 0)
948 *blue_ref =
949 *blue_shoot = rounds[num_rounds / 2];
951 else if (num_rounds == 0)
953 *blue_ref =
954 *blue_shoot = flats[num_flats / 2];
956 else
958 *blue_ref = flats[num_flats / 2];
959 *blue_shoot = rounds[num_rounds / 2];
962 /* there are sometimes problems if the overshoot position of top */
963 /* zones is under its reference position, or the opposite for bottom */
964 /* zones; we must thus check everything there and correct the errors */
965 if (*blue_shoot != *blue_ref)
967 FT_Pos ref = *blue_ref;
968 FT_Pos shoot = *blue_shoot;
969 FT_Bool over_ref = FT_BOOL(shoot > ref);
972 if ((TA_LATIN_IS_TOP_BLUE(bs)
973 || TA_LATIN_IS_SUB_TOP_BLUE(bs)) ^ over_ref)
975 *blue_ref =
976 *blue_shoot = (shoot + ref) / 2;
978 TA_LOG_GLOBAL((" [overshoot smaller than reference,"
979 " taking mean value]\n"));
983 blue->ascender = ascender;
984 blue->descender = descender;
986 blue->flags = 0;
987 if (TA_LATIN_IS_TOP_BLUE(bs))
988 blue->flags |= TA_LATIN_BLUE_TOP;
989 if (TA_LATIN_IS_SUB_TOP_BLUE(bs))
990 blue->flags |= TA_LATIN_BLUE_SUB_TOP;
991 if (TA_LATIN_IS_NEUTRAL_BLUE(bs))
992 blue->flags |= TA_LATIN_BLUE_NEUTRAL;
994 /* the following flag is used later to adjust the y and x scales */
995 /* in order to optimize the pixel grid alignment */
996 /* of the top of small letters */
997 if (TA_LATIN_IS_X_HEIGHT_BLUE(bs))
998 blue->flags |= TA_LATIN_BLUE_ADJUSTMENT;
1000 TA_LOG_GLOBAL((" -> reference = %ld\n"
1001 " overshoot = %ld\n",
1002 *blue_ref, *blue_shoot));
1004 } /* end for loop */
1006 ta_shaper_buf_destroy(face, shaper_buf);
1008 /* if windows compatibility mode is activated, */
1009 /* add two artificial blue zones for usWinAscent and usWinDescent */
1010 /* just in case the above algorithm has missed them -- */
1011 /* Windows cuts off everything outside of those two values */
1012 if (metrics->root.globals->font->windows_compatibility)
1014 TT_OS2* os2;
1017 os2 = (TT_OS2*)FT_Get_Sfnt_Table(face, ft_sfnt_os2);
1019 if (os2)
1021 blue = &axis->blues[axis->blue_count];
1022 blue->flags = TA_LATIN_BLUE_TOP | TA_LATIN_BLUE_ACTIVE;
1023 blue->ref.org =
1024 blue->shoot.org = os2->usWinAscent;
1026 TA_LOG_GLOBAL(("artificial blue zone for usWinAscent:\n"
1027 " -> reference = %ld\n"
1028 " overshoot = %ld\n",
1029 blue->ref.org, blue->shoot.org));
1031 blue = &axis->blues[axis->blue_count + 1];
1032 blue->flags = TA_LATIN_BLUE_ACTIVE;
1033 blue->ref.org =
1034 blue->shoot.org = -os2->usWinDescent;
1036 TA_LOG_GLOBAL(("artificial blue zone for usWinDescent:\n"
1037 " -> reference = %ld\n"
1038 " overshoot = %ld\n",
1039 blue->ref.org, blue->shoot.org));
1041 else
1043 blue = &axis->blues[axis->blue_count];
1044 blue->flags =
1045 blue->ref.org =
1046 blue->shoot.org = 0;
1048 blue = &axis->blues[axis->blue_count + 1];
1049 blue->flags =
1050 blue->ref.org =
1051 blue->shoot.org = 0;
1055 /* we finally check whether blue zones are ordered; */
1056 /* `ref' and `shoot' values of two blue zones must not overlap */
1057 if (axis->blue_count)
1059 FT_UInt i;
1060 TA_LatinBlue blue_sorted[TA_BLUE_STRINGSET_MAX_LEN + 2];
1063 for (i = 0; i < axis->blue_count; i++)
1064 blue_sorted[i] = &axis->blues[i];
1066 /* sort bottoms of blue zones... */
1067 ta_latin_sort_blue(axis->blue_count, blue_sorted);
1069 /* ...and adjust top values if necessary */
1070 for (i = 0; i < axis->blue_count - 1; i++)
1072 FT_Pos* a;
1073 FT_Pos* b;
1075 #ifdef TA_DEBUG
1076 FT_Bool a_is_top = 0;
1077 #endif
1080 if (blue_sorted[i]->flags & (TA_LATIN_BLUE_TOP
1081 | TA_LATIN_BLUE_SUB_TOP))
1083 a = &blue_sorted[i]->shoot.org;
1084 #ifdef TA_DEBUG
1085 a_is_top = 1;
1086 #endif
1088 else
1089 a = &blue_sorted[i]->ref.org;
1091 if (blue_sorted[i + 1]->flags & (TA_LATIN_BLUE_TOP
1092 | TA_LATIN_BLUE_SUB_TOP))
1093 b = &blue_sorted[i + 1]->shoot.org;
1094 else
1095 b = &blue_sorted[i + 1]->ref.org;
1097 if (*a > *b)
1099 *a = *b;
1100 TA_LOG_GLOBAL(("blue zone overlap:"
1101 " adjusting %s %d to %ld\n",
1102 a_is_top ? "overshoot" : "reference",
1103 blue_sorted[i] - axis->blues,
1104 *a));
1109 TA_LOG_GLOBAL(("\n"));
1111 return;
1115 /* check whether all ASCII digits have the same advance width */
1117 void
1118 ta_latin_metrics_check_digits(TA_LatinMetrics metrics,
1119 FT_Face face)
1121 FT_Bool started = 0, same_width = 1;
1122 FT_Fixed advance = 0, old_advance = 0;
1124 void* shaper_buf;
1126 /* in all supported charmaps, digits have character codes 0x30-0x39 */
1127 const char digits[] = "0 1 2 3 4 5 6 7 8 9";
1128 const char* p;
1131 p = digits;
1132 shaper_buf = ta_shaper_buf_create(face);
1134 while (*p)
1136 FT_ULong glyph_index;
1137 unsigned int num_idx;
1140 /* reject input that maps to more than a single glyph */
1141 p = ta_shaper_get_cluster(p, &metrics->root, shaper_buf, &num_idx);
1142 if (num_idx > 1)
1143 continue;
1145 glyph_index = ta_shaper_get_elem(&metrics->root,
1146 shaper_buf,
1148 &advance,
1149 NULL);
1150 if (!glyph_index)
1151 continue;
1153 if (started)
1155 if (advance != old_advance)
1157 same_width = 0;
1158 break;
1161 else
1163 old_advance = advance;
1164 started = 1;
1168 ta_shaper_buf_destroy(face, shaper_buf);
1170 metrics->root.digits_have_same_width = same_width;
1174 /* initialize global metrics */
1176 FT_Error
1177 ta_latin_metrics_init(TA_LatinMetrics metrics,
1178 FT_Face face,
1179 FT_Face reference)
1181 FT_CharMap oldmap = face->charmap;
1184 metrics->units_per_em = face->units_per_EM;
1186 if (!FT_Select_Charmap(face, FT_ENCODING_UNICODE))
1188 ta_latin_metrics_init_widths(metrics, face, 1);
1189 ta_latin_metrics_init_blues(metrics, reference ? reference : face);
1190 ta_latin_metrics_check_digits(metrics, face);
1192 else
1194 /* we only have a symbol font encoding */
1195 ta_latin_metrics_init_widths(metrics, face, 0);
1198 FT_Set_Charmap(face, oldmap);
1199 return FT_Err_Ok;
1203 /* adjust scaling value, then scale and shift widths */
1204 /* and blue zones (if applicable) for given dimension */
1206 static void
1207 ta_latin_metrics_scale_dim(TA_LatinMetrics metrics,
1208 TA_Scaler scaler,
1209 TA_Dimension dim)
1211 FT_Fixed scale;
1212 FT_Pos delta;
1213 TA_LatinAxis axis;
1214 FT_UInt ppem;
1215 FT_UInt nn;
1218 ppem = metrics->root.scaler.face->size->metrics.x_ppem;
1220 if (dim == TA_DIMENSION_HORZ)
1222 scale = scaler->x_scale;
1223 delta = scaler->x_delta;
1225 else
1227 scale = scaler->y_scale;
1228 delta = scaler->y_delta;
1231 axis = &metrics->axis[dim];
1233 if (axis->org_scale == scale && axis->org_delta == delta)
1234 return;
1236 axis->org_scale = scale;
1237 axis->org_delta = delta;
1239 /* correct Y scale to optimize the alignment of the top of */
1240 /* small letters to the pixel grid */
1241 /* (if we do x-height snapping for this ppem value) */
1242 if (!number_set_is_element(
1243 metrics->root.globals->font->x_height_snapping_exceptions,
1244 (int)ppem))
1246 TA_LatinAxis Axis = &metrics->axis[TA_DIMENSION_VERT];
1247 TA_LatinBlue blue = NULL;
1250 for (nn = 0; nn < Axis->blue_count; nn++)
1252 if (Axis->blues[nn].flags & TA_LATIN_BLUE_ADJUSTMENT)
1254 blue = &Axis->blues[nn];
1255 break;
1259 if (blue)
1261 FT_Pos scaled;
1262 FT_Pos threshold;
1263 FT_Pos fitted;
1264 FT_UInt limit;
1267 scaled = FT_MulFix(blue->shoot.org, scale);
1268 limit = metrics->root.globals->increase_x_height;
1269 threshold = 40;
1271 /* if the `increase-x-height' property is active, */
1272 /* we round up much more often */
1273 if (limit
1274 && ppem <= limit
1275 && ppem >= TA_PROP_INCREASE_X_HEIGHT_MIN)
1276 threshold = 52;
1278 fitted = (scaled + threshold) & ~63;
1280 if (scaled != fitted)
1282 if (dim == TA_DIMENSION_VERT)
1284 FT_Pos max_height;
1285 FT_Pos dist;
1286 FT_Fixed new_scale;
1289 new_scale = FT_MulDiv(scale, fitted, scaled);
1291 /* the scaling should not change the result by more than two pixels */
1292 max_height = metrics->units_per_em;
1294 for (nn = 0; nn < Axis->blue_count; nn++)
1296 max_height = TA_MAX(max_height, Axis->blues[nn].ascender);
1297 max_height = TA_MAX(max_height, -Axis->blues[nn].descender);
1300 dist = TA_ABS(FT_MulFix(max_height, new_scale - scale));
1301 dist &= ~127;
1303 if (dist == 0)
1305 TA_LOG_GLOBAL((
1306 "ta_latin_metrics_scale_dim:"
1307 " x height alignment (style `%s'):\n"
1309 " vertical scaling changed from %.5f to %.5f (by %d%%)\n"
1310 "\n",
1311 ta_style_names[metrics->root.style_class->style],
1312 scale / 65536.0,
1313 new_scale / 65536.0,
1314 (fitted - scaled) * 100 / scaled));
1316 scale = new_scale;
1318 #ifdef TA_DEBUG
1319 else
1321 TA_LOG_GLOBAL((
1322 "ta_latin_metrics_scale_dim:"
1323 " x height alignment (style `%s'):\n"
1325 " excessive vertical scaling abandoned\n"
1326 "\n",
1327 ta_style_names[metrics->root.style_class->style]));
1329 #endif
1335 axis->scale = scale;
1336 axis->delta = delta;
1338 if (dim == TA_DIMENSION_HORZ)
1340 metrics->root.scaler.x_scale = scale;
1341 metrics->root.scaler.x_delta = delta;
1343 else
1345 metrics->root.scaler.y_scale = scale;
1346 metrics->root.scaler.y_delta = delta;
1349 TA_LOG_GLOBAL(("%s widths (style `%s')\n",
1350 dim == TA_DIMENSION_HORZ ? "horizontal" : "vertical",
1351 ta_style_names[metrics->root.style_class->style]));
1353 /* scale the widths */
1354 for (nn = 0; nn < axis->width_count; nn++)
1356 TA_Width width = axis->widths + nn;
1359 width->cur = FT_MulFix(width->org, scale);
1360 width->fit = width->cur;
1362 TA_LOG_GLOBAL((" %d scaled to %.2f\n",
1363 width->org,
1364 width->cur / 64.0));
1367 TA_LOG_GLOBAL(("\n"));
1369 /* an extra-light axis corresponds to a standard width that is */
1370 /* smaller than 5/8 pixels */
1371 axis->extra_light =
1372 (FT_Bool)(FT_MulFix(axis->standard_width, scale) < 32 + 8);
1374 #ifdef TA_DEBUG
1375 if (axis->extra_light)
1376 TA_LOG_GLOBAL(("`%s' style is extra light (at current resolution)\n"
1377 "\n",
1378 ta_style_names[metrics->root.style_class->style]));
1379 #endif
1381 if (dim == TA_DIMENSION_VERT)
1383 #ifdef TA_DEBUG
1384 if (axis->blue_count)
1385 TA_LOG_GLOBAL(("blue zones (style `%s')\n",
1386 ta_style_names[metrics->root.style_class->style]));
1387 #endif
1389 /* scale the blue zones */
1390 for (nn = 0; nn < axis->blue_count; nn++)
1392 TA_LatinBlue blue = &axis->blues[nn];
1393 FT_Pos dist;
1396 blue->ref.cur = FT_MulFix(blue->ref.org, scale) + delta;
1397 blue->ref.fit = blue->ref.cur;
1398 blue->shoot.cur = FT_MulFix(blue->shoot.org, scale) + delta;
1399 blue->shoot.fit = blue->shoot.cur;
1400 blue->flags &= ~TA_LATIN_BLUE_ACTIVE;
1402 /* a blue zone is only active if it is less than 3/4 pixels tall */
1403 dist = FT_MulFix(blue->ref.org - blue->shoot.org, scale);
1404 if (dist <= 48 && dist >= -48)
1406 #if 0
1407 FT_Pos delta1;
1408 #endif
1409 FT_Pos delta2;
1412 /* use discrete values for blue zone widths */
1414 #if 0
1415 /* generic, original code */
1416 delta1 = blue->shoot.org - blue->ref.org;
1417 delta2 = delta1;
1418 if (delta1 < 0)
1419 delta2 = -delta2;
1421 delta2 = FT_MulFix(delta2, scale);
1423 if (delta2 < 32)
1424 delta2 = 0;
1425 else if (delta2 < 64)
1426 delta2 = 32 + (((delta2 - 32) + 16) & ~31);
1427 else
1428 delta2 = TA_PIX_ROUND(delta2);
1430 if (delta1 < 0)
1431 delta2 = -delta2;
1433 blue->ref.fit = TA_PIX_ROUND(blue->ref.cur);
1434 blue->shoot.fit = blue->ref.fit + delta2;
1435 #else
1436 /* simplified version due to abs(dist) <= 48 */
1437 delta2 = dist;
1438 if (dist < 0)
1439 delta2 = -delta2;
1441 if (delta2 < 32)
1442 delta2 = 0;
1443 else if (delta2 < 48)
1444 delta2 = 32;
1445 else
1446 delta2 = 64;
1448 if (dist < 0)
1449 delta2 = -delta2;
1451 blue->ref.fit = TA_PIX_ROUND(blue->ref.cur);
1452 blue->shoot.fit = blue->ref.fit - delta2;
1453 #endif
1455 blue->flags |= TA_LATIN_BLUE_ACTIVE;
1459 /* use sub-top blue zone only if it doesn't overlap with */
1460 /* another (non-sup-top) blue zone; otherwise, the */
1461 /* effect would be similar to a neutral blue zone, which */
1462 /* is not desired here */
1463 for (nn = 0; nn < axis->blue_count; nn++)
1465 TA_LatinBlue blue = &axis->blues[nn];
1466 FT_UInt i;
1469 if (!(blue->flags & TA_LATIN_BLUE_SUB_TOP))
1470 continue;
1471 if (!(blue->flags & TA_LATIN_BLUE_ACTIVE))
1472 continue;
1474 for (i = 0; i < axis->blue_count; i++)
1476 TA_LatinBlue b = &axis->blues[i];
1479 if (b->flags & TA_LATIN_BLUE_SUB_TOP)
1480 continue;
1481 if (!(b->flags & TA_LATIN_BLUE_ACTIVE))
1482 continue;
1484 if (b->ref.fit <= blue->shoot.fit
1485 && b->shoot.fit >= blue->ref.fit)
1487 blue->flags &= ~TA_LATIN_BLUE_ACTIVE;
1488 break;
1493 #ifdef TA_DEBUG
1494 for (nn = 0; nn < axis->blue_count; nn++)
1496 TA_LatinBlue blue = &axis->blues[nn];
1499 TA_LOG_GLOBAL((" reference %d: %d scaled to %.2f%s\n"
1500 " overshoot %d: %d scaled to %.2f%s\n",
1502 blue->ref.org,
1503 blue->ref.fit / 64.0,
1504 blue->flags & TA_LATIN_BLUE_ACTIVE ? ""
1505 : " (inactive)",
1507 blue->shoot.org,
1508 blue->shoot.fit / 64.0,
1509 blue->flags & TA_LATIN_BLUE_ACTIVE ? ""
1510 : " (inactive)"));
1512 #endif
1514 /* the last two artificial blue zones are to be scaled */
1515 /* with uncorrected scaling values */
1516 if (metrics->root.globals->font->windows_compatibility)
1518 TA_LatinAxis a = &metrics->axis[TA_DIMENSION_VERT];
1519 TA_LatinBlue b;
1522 b = &a->blues[a->blue_count];
1523 b->ref.cur =
1524 b->ref.fit =
1525 b->shoot.cur =
1526 b->shoot.fit = FT_MulFix(b->ref.org, a->org_scale) + delta;
1528 TA_LOG_GLOBAL((" reference %d: %d scaled to %.2f (artificial)\n"
1529 " overshoot %d: %d scaled to %.2f (artificial)\n",
1530 a->blue_count,
1531 b->ref.org,
1532 b->ref.fit / 64.0,
1533 a->blue_count,
1534 b->shoot.org,
1535 b->shoot.fit / 64.0));
1537 b = &a->blues[a->blue_count + 1];
1538 b->ref.cur =
1539 b->ref.fit =
1540 b->shoot.cur =
1541 b->shoot.fit = FT_MulFix(b->ref.org, a->org_scale) + delta;
1543 TA_LOG_GLOBAL((" reference %d: %d scaled to %.2f (artificial)\n"
1544 " overshoot %d: %d scaled to %.2f (artificial)\n",
1545 a->blue_count + 1,
1546 b->ref.org,
1547 b->ref.fit / 64.0,
1548 a->blue_count + 1,
1549 b->shoot.org,
1550 b->shoot.fit / 64.0));
1553 TA_LOG_GLOBAL(("\n"));
1558 /* scale global values in both directions */
1560 void
1561 ta_latin_metrics_scale(TA_LatinMetrics metrics,
1562 TA_Scaler scaler)
1564 metrics->root.scaler.render_mode = scaler->render_mode;
1565 metrics->root.scaler.face = scaler->face;
1566 metrics->root.scaler.flags = scaler->flags;
1568 ta_latin_metrics_scale_dim(metrics, scaler, TA_DIMENSION_HORZ);
1569 ta_latin_metrics_scale_dim(metrics, scaler, TA_DIMENSION_VERT);
1573 /* walk over all contours and compute its segments */
1575 FT_Error
1576 ta_latin_hints_compute_segments(TA_GlyphHints hints,
1577 TA_Dimension dim)
1579 TA_LatinMetrics metrics = (TA_LatinMetrics)hints->metrics;
1580 TA_AxisHints axis = &hints->axis[dim];
1581 FT_Error error = FT_Err_Ok;
1583 TA_Segment segment = NULL;
1584 TA_SegmentRec seg0;
1586 TA_Point* contour = hints->contours;
1587 TA_Point* contour_limit = contour + hints->num_contours;
1588 TA_Direction major_dir, segment_dir;
1590 FT_Pos flat_threshold = FLAT_THRESHOLD(metrics->units_per_em);
1593 memset(&seg0, 0, sizeof (TA_SegmentRec));
1594 seg0.score = 32000;
1595 seg0.flags = TA_EDGE_NORMAL;
1597 major_dir = (TA_Direction)TA_ABS(axis->major_dir);
1598 segment_dir = major_dir;
1600 axis->num_segments = 0;
1602 /* set up (u,v) in each point */
1603 if (dim == TA_DIMENSION_HORZ)
1605 TA_Point point = hints->points;
1606 TA_Point limit = point + hints->num_points;
1609 for (; point < limit; point++)
1611 point->u = point->fx;
1612 point->v = point->fy;
1615 else
1617 TA_Point point = hints->points;
1618 TA_Point limit = point + hints->num_points;
1621 for (; point < limit; point++)
1623 point->u = point->fy;
1624 point->v = point->fx;
1628 /* do each contour separately */
1629 for (; contour < contour_limit; contour++)
1631 TA_Point point = contour[0];
1632 TA_Point last = point->prev;
1634 int on_edge = 0;
1636 /* we call values measured along a segment (point->v) */
1637 /* `coordinates', and values orthogonal to it (point->u) */
1638 /* `positions' */
1639 FT_Pos min_pos = 32000;
1640 FT_Pos max_pos = -32000;
1641 FT_Pos min_coord = 32000;
1642 FT_Pos max_coord = -32000;
1643 FT_UShort min_flags = TA_FLAG_NONE;
1644 FT_UShort max_flags = TA_FLAG_NONE;
1645 FT_Pos min_on_coord = 32000;
1646 FT_Pos max_on_coord = -32000;
1648 FT_Bool passed;
1650 TA_Segment prev_segment = NULL;
1652 FT_Pos prev_min_pos = min_pos;
1653 FT_Pos prev_max_pos = max_pos;
1654 FT_Pos prev_min_coord = min_coord;
1655 FT_Pos prev_max_coord = max_coord;
1656 FT_UShort prev_min_flags = min_flags;
1657 FT_UShort prev_max_flags = max_flags;
1658 FT_Pos prev_min_on_coord = min_on_coord;
1659 FT_Pos prev_max_on_coord = max_on_coord;
1662 if (TA_ABS(last->out_dir) == major_dir
1663 && TA_ABS(point->out_dir) == major_dir)
1665 /* we are already on an edge, try to locate its start */
1666 last = point;
1668 for (;;)
1670 point = point->prev;
1671 if (TA_ABS(point->out_dir) != major_dir)
1673 point = point->next;
1674 break;
1676 if (point == last)
1677 break;
1681 last = point;
1682 passed = 0;
1684 for (;;)
1686 FT_Pos u, v;
1689 if (on_edge)
1691 /* get minimum and maximum position */
1692 u = point->u;
1693 if (u < min_pos)
1694 min_pos = u;
1695 if (u > max_pos)
1696 max_pos = u;
1698 /* get minimum and maximum coordinate together with flags */
1699 v = point->v;
1700 if (v < min_coord)
1702 min_coord = v;
1703 min_flags = point->flags;
1705 if (v > max_coord)
1707 max_coord = v;
1708 max_flags = point->flags;
1711 /* get minimum and maximum coordinate of `on' points */
1712 if (!(point->flags & TA_FLAG_CONTROL))
1714 v = point->v;
1715 if (v < min_on_coord)
1716 min_on_coord = v;
1717 if (v > max_on_coord)
1718 max_on_coord = v;
1721 if (point->out_dir != segment_dir
1722 || point == last)
1724 /* check whether the new segment's start point is identical to */
1725 /* the previous segment's end point; for example, this might */
1726 /* happen for spikes */
1728 if (!prev_segment
1729 || segment->first != prev_segment->last)
1731 /* points are different: we are just leaving an edge, thus */
1732 /* record a new segment */
1734 segment->last = point;
1735 segment->pos = (FT_Short)((min_pos + max_pos) >> 1);
1736 segment->delta = (FT_Short)((max_pos - min_pos) >> 1);
1738 /* a segment is round if either its first or last point */
1739 /* is a control point, and the length of the on points */
1740 /* inbetween doesn't exceed a heuristic limit */
1741 if ((min_flags | max_flags) & TA_FLAG_CONTROL
1742 && (max_on_coord - min_on_coord) < flat_threshold)
1743 segment->flags |= TA_EDGE_ROUND;
1745 segment->min_coord = (FT_Short)min_coord;
1746 segment->max_coord = (FT_Short)max_coord;
1747 segment->height = segment->max_coord - segment->min_coord;
1749 prev_segment = segment;
1750 prev_min_pos = min_pos;
1751 prev_max_pos = max_pos;
1752 prev_min_coord = min_coord;
1753 prev_max_coord = max_coord;
1754 prev_min_flags = min_flags;
1755 prev_max_flags = max_flags;
1756 prev_min_on_coord = min_on_coord;
1757 prev_max_on_coord = max_on_coord;
1759 else
1761 /* points are the same: we don't create a new segment but */
1762 /* merge the current segment with the previous one */
1764 if (prev_segment->last->in_dir == point->in_dir)
1766 /* we have identical directions (this can happen for */
1767 /* degenerate outlines that move zig-zag along the main */
1768 /* axis without changing the coordinate value of the other */
1769 /* axis, and where the segments have just been merged): */
1770 /* unify segments */
1772 /* update constraints */
1774 if (prev_min_pos < min_pos)
1775 min_pos = prev_min_pos;
1776 if (prev_max_pos > max_pos)
1777 max_pos = prev_max_pos;
1779 if (prev_min_coord < min_coord)
1781 min_coord = prev_min_coord;
1782 min_flags = prev_min_flags;
1784 if (prev_max_coord > max_coord)
1786 max_coord = prev_max_coord;
1787 max_flags = prev_max_flags;
1790 if (prev_min_on_coord < min_on_coord)
1791 min_on_coord = prev_min_on_coord;
1792 if (prev_max_on_coord > max_on_coord)
1793 max_on_coord = prev_max_on_coord;
1795 prev_segment->last = point;
1796 prev_segment->pos = (FT_Short)((min_pos + max_pos) >> 1);
1798 if ((min_flags | max_flags) & TA_FLAG_CONTROL
1799 && (max_on_coord - min_on_coord) < flat_threshold)
1800 prev_segment->flags |= TA_EDGE_ROUND;
1801 else
1802 prev_segment->flags &= ~TA_EDGE_ROUND;
1804 prev_segment->min_coord = (FT_Short)min_coord;
1805 prev_segment->max_coord = (FT_Short)max_coord;
1806 prev_segment->height = prev_segment->max_coord
1807 - prev_segment->min_coord;
1809 else
1811 /* we have different directions; use the properties of the */
1812 /* longer segment and discard the other one */
1814 if (TA_ABS(prev_max_coord - prev_min_coord)
1815 > TA_ABS(max_coord - min_coord))
1817 /* discard current segment */
1819 if (min_pos < prev_min_pos)
1820 prev_min_pos = min_pos;
1821 if (max_pos > prev_max_pos)
1822 prev_max_pos = max_pos;
1824 prev_segment->last = point;
1825 prev_segment->pos = (FT_Short)((prev_min_pos
1826 + prev_max_pos) >> 1);
1828 else
1830 /* discard previous segment */
1832 if (prev_min_pos < min_pos)
1833 min_pos = prev_min_pos;
1834 if (prev_max_pos > max_pos)
1835 max_pos = prev_max_pos;
1837 segment->last = point;
1838 segment->pos = (FT_Short)((min_pos + max_pos) >> 1);
1840 if ((min_flags | max_flags) & TA_FLAG_CONTROL
1841 && (max_on_coord - min_on_coord) < flat_threshold)
1842 segment->flags |= TA_EDGE_ROUND;
1844 segment->min_coord = (FT_Short)min_coord;
1845 segment->max_coord = (FT_Short)max_coord;
1846 segment->height = segment->max_coord - segment->min_coord;
1848 *prev_segment = *segment;
1850 prev_min_pos = min_pos;
1851 prev_max_pos = max_pos;
1852 prev_min_coord = min_coord;
1853 prev_max_coord = max_coord;
1854 prev_min_flags = min_flags;
1855 prev_max_flags = max_flags;
1856 prev_min_on_coord = min_on_coord;
1857 prev_max_on_coord = max_on_coord;
1861 axis->num_segments--;
1864 on_edge = 0;
1865 segment = NULL;
1867 /* fall through */
1871 /* now exit if we are at the start/end point */
1872 if (point == last)
1874 if (passed)
1875 break;
1876 passed = 1;
1879 /* if we are not on an edge, check whether the major direction */
1880 /* coincides with the current point's `out' direction, or */
1881 /* whether we have a single-point contour */
1882 if (!on_edge
1883 && (TA_ABS(point->out_dir) == major_dir
1884 || point == point->prev))
1886 /* this is the start of a new segment! */
1887 segment_dir = (TA_Direction)point->out_dir;
1889 error = ta_axis_hints_new_segment(axis, &segment);
1890 if (error)
1891 goto Exit;
1893 /* clear all segment fields */
1894 segment[0] = seg0;
1896 segment->dir = (FT_Char)segment_dir;
1897 segment->first = point;
1898 segment->last = point;
1900 /* `ta_axis_hints_new_segment' reallocates memory, */
1901 /* thus we have to refresh the `prev_segment' pointer */
1902 if (prev_segment)
1903 prev_segment = segment - 1;
1905 min_pos = max_pos = point->u;
1906 min_coord = max_coord = point->v;
1907 min_flags = max_flags = point->flags;
1909 if (point->flags & TA_FLAG_CONTROL)
1911 min_on_coord = 32000;
1912 max_on_coord = -32000;
1914 else
1915 min_on_coord = max_on_coord = point->v;
1917 on_edge = 1;
1919 if (point->out_dir != point->next->in_dir
1920 || point == point->prev)
1923 * We have a one-point segment. This is either
1925 * . a one-point contour (with `in' and `out' direction
1926 * set to TA_DIR_NONE by default), or
1928 * . an artificial one-point segment (with a forced
1929 * `out' direction).
1931 segment->pos = (FT_Short)min_pos;
1933 if (point->flags & TA_FLAG_CONTROL)
1934 segment->flags |= TA_EDGE_ROUND;
1936 /* artificially extend the horizontal size if requested */
1937 segment->min_coord = (FT_Short)point->v + point->left_offset;
1938 segment->max_coord = (FT_Short)point->v + point->right_offset;
1939 segment->height = 0;
1941 on_edge = 0;
1942 segment = NULL;
1946 point = point->next;
1948 } /* contours */
1951 /* now slightly increase the height of segments if this makes sense -- */
1952 /* this is used to better detect and ignore serifs */
1954 TA_Segment segments = axis->segments;
1955 TA_Segment segments_end = segments + axis->num_segments;
1958 for (segment = segments; segment < segments_end; segment++)
1960 TA_Point first = segment->first;
1961 TA_Point last = segment->last;
1963 FT_Pos first_v = first->v;
1964 FT_Pos last_v = last->v;
1967 if (first_v < last_v)
1969 TA_Point p;
1972 p = first->prev;
1973 if (p->v < first_v)
1974 segment->height = (FT_Short)(segment->height +
1975 ((first_v - p->v) >> 1));
1977 p = last->next;
1978 if (p->v > last_v)
1979 segment->height = (FT_Short)(segment->height +
1980 ((p->v - last_v) >> 1));
1982 else
1984 TA_Point p;
1987 p = first->prev;
1988 if (p->v > first_v)
1989 segment->height = (FT_Short)(segment->height +
1990 ((p->v - first_v) >> 1));
1992 p = last->next;
1993 if (p->v < last_v)
1994 segment->height = (FT_Short)(segment->height +
1995 ((last_v - p->v) >> 1));
2000 Exit:
2001 return error;
2005 /* link segments to form stems and serifs; if `width_count' and */
2006 /* `widths' are non-zero, use them to fine-tune the scoring function */
2008 void
2009 ta_latin_hints_link_segments(TA_GlyphHints hints,
2010 FT_UInt width_count,
2011 TA_WidthRec* widths,
2012 TA_Dimension dim)
2014 TA_AxisHints axis = &hints->axis[dim];
2016 TA_Segment segments = axis->segments;
2017 TA_Segment segment_limit = segments + axis->num_segments;
2019 FT_Pos len_threshold, len_score, dist_score, max_width;
2020 TA_Segment seg1, seg2;
2023 if (width_count)
2024 max_width = widths[width_count - 1].org;
2025 else
2026 max_width = 0;
2028 /* a heuristic value to set up a minimum value for overlapping */
2029 len_threshold = TA_LATIN_CONSTANT(hints->metrics, 8);
2030 if (len_threshold == 0)
2031 len_threshold = 1;
2033 /* a heuristic value to weight lengths */
2034 len_score = TA_LATIN_CONSTANT(hints->metrics, 6000);
2036 /* a heuristic value to weight distances (no call to */
2037 /* TA_LATIN_CONSTANT needed, since we work on multiples */
2038 /* of the stem width) */
2039 dist_score = 3000;
2041 /* now compare each segment to the others */
2042 for (seg1 = segments; seg1 < segment_limit; seg1++)
2044 if (seg1->dir != axis->major_dir)
2045 continue;
2047 /* search for stems having opposite directions, */
2048 /* with seg1 to the `left' of seg2 */
2049 for (seg2 = segments; seg2 < segment_limit; seg2++)
2051 FT_Pos pos1 = seg1->pos;
2052 FT_Pos pos2 = seg2->pos;
2055 if (seg1->dir + seg2->dir == 0
2056 && pos2 > pos1)
2058 /* compute distance between the two segments */
2059 FT_Pos min = seg1->min_coord;
2060 FT_Pos max = seg1->max_coord;
2061 FT_Pos len;
2064 if (min < seg2->min_coord)
2065 min = seg2->min_coord;
2066 if (max > seg2->max_coord)
2067 max = seg2->max_coord;
2069 /* compute maximum coordinate difference of the two segments */
2070 /* (this is, how much they overlap) */
2071 len = max - min;
2073 /* for one-point segments, `len' is zero if there is an overlap */
2074 /* (and negative otherwise); we have to correct this */
2075 if (len == 0
2076 && (seg1->min_coord == seg1->max_coord
2077 || seg2->min_coord == seg2->max_coord))
2078 len = len_threshold;
2080 if (len >= len_threshold)
2083 * The score is the sum of two demerits indicating the
2084 * `badness' of a fit, measured along the segments' main axis
2085 * and orthogonal to it, respectively.
2087 * o The less overlapping along the main axis, the worse it
2088 * is, causing a larger demerit.
2090 * o The nearer the orthogonal distance to a stem width, the
2091 * better it is, causing a smaller demerit. For simplicity,
2092 * however, we only increase the demerit for values that
2093 * exceed the largest stem width.
2096 FT_Pos dist = pos2 - pos1;
2098 FT_Pos dist_demerit, score;
2101 if (max_width)
2103 /* distance demerits are based on multiples of `max_width'; */
2104 /* we scale by 1024 for getting more precision */
2105 FT_Pos delta = (dist << 10) / max_width - (1 << 10);
2108 if (delta > 10000)
2109 dist_demerit = 32000;
2110 else if (delta > 0)
2111 dist_demerit = delta * delta / dist_score;
2112 else
2113 dist_demerit = 0;
2115 else
2116 dist_demerit = dist; /* default if no widths available */
2118 score = dist_demerit + len_score / len;
2120 /* and we search for the smallest score */
2121 if (score < seg1->score)
2123 seg1->score = score;
2124 seg1->link = seg2;
2127 if (score < seg2->score)
2129 seg2->score = score;
2130 seg2->link = seg1;
2137 /* now compute the `serif' segments, cf. explanations in `tahints.h' */
2138 for (seg1 = segments; seg1 < segment_limit; seg1++)
2140 seg2 = seg1->link;
2142 if (seg2)
2144 if (seg2->link != seg1)
2146 seg1->link = 0;
2147 seg1->serif = seg2->link;
2154 /* link segments to edges, using feature analysis for selection */
2156 FT_Error
2157 ta_latin_hints_compute_edges(TA_GlyphHints hints,
2158 TA_Dimension dim)
2160 TA_AxisHints axis = &hints->axis[dim];
2161 FT_Error error = FT_Err_Ok;
2162 TA_LatinAxis laxis = &((TA_LatinMetrics)hints->metrics)->axis[dim];
2164 TA_StyleClass style_class = hints->metrics->style_class;
2165 TA_ScriptClass script_class = ta_script_classes[style_class->script];
2167 FT_Bool top_to_bottom_hinting = 0;
2169 TA_Segment segments = axis->segments;
2170 TA_Segment segment_limit = segments + axis->num_segments;
2171 TA_Segment seg;
2173 #if 0
2174 TA_Direction up_dir;
2175 #endif
2176 FT_Fixed scale;
2177 FT_Pos edge_distance_threshold;
2178 FT_Pos segment_length_threshold;
2179 FT_Pos segment_width_threshold;
2182 axis->num_edges = 0;
2184 scale = (dim == TA_DIMENSION_HORZ) ? hints->x_scale
2185 : hints->y_scale;
2187 #if 0
2188 up_dir = (dim == TA_DIMENSION_HORZ) ? TA_DIR_UP
2189 : TA_DIR_RIGHT;
2190 #endif
2192 if (dim == TA_DIMENSION_VERT)
2193 top_to_bottom_hinting = script_class->top_to_bottom_hinting;
2195 /* we ignore all segments that are less than 1 pixel in length */
2196 /* to avoid many problems with serif fonts */
2197 /* (the corresponding threshold is computed in font units) */
2198 if (dim == TA_DIMENSION_HORZ)
2199 segment_length_threshold = FT_DivFix(64, hints->y_scale);
2200 else
2201 segment_length_threshold = 0;
2204 * Similarly, we ignore segments that have a width delta
2205 * larger than 0.5px (i.e., a width larger than 1px).
2207 segment_width_threshold = FT_DivFix(32, scale);
2209 /********************************************************************/
2210 /* */
2211 /* We begin by generating a sorted table of edges for the current */
2212 /* direction. To do so, we simply scan each segment and try to find */
2213 /* an edge in our table that corresponds to its position. */
2214 /* */
2215 /* If no edge is found, we create and insert a new edge in the */
2216 /* sorted table. Otherwise, we simply add the segment to the edge's */
2217 /* list which gets processed in the second step to compute the */
2218 /* edge's properties. */
2219 /* */
2220 /* Note that the table of edges is sorted along the segment/edge */
2221 /* position. */
2222 /* */
2223 /********************************************************************/
2225 /* assure that edge distance threshold is at most 0.25px */
2226 edge_distance_threshold = FT_MulFix(laxis->edge_distance_threshold,
2227 scale);
2228 if (edge_distance_threshold > 64 / 4)
2229 edge_distance_threshold = 64 / 4;
2231 edge_distance_threshold = FT_DivFix(edge_distance_threshold,
2232 scale);
2234 for (seg = segments; seg < segment_limit; seg++)
2236 TA_Edge found = NULL;
2237 FT_Int ee;
2240 /* ignore too short segments, too wide ones, and, in this loop, */
2241 /* one-point segments without a direction */
2242 if (seg->height < segment_length_threshold
2243 || seg->delta > segment_width_threshold
2244 || seg->dir == TA_DIR_NONE)
2245 continue;
2247 /* a special case for serif edges: */
2248 /* if they are smaller than 1.5 pixels we ignore them */
2249 if (seg->serif
2250 && 2 * seg->height < 3 * segment_length_threshold)
2251 continue;
2253 /* look for an edge corresponding to the segment */
2254 for (ee = 0; ee < axis->num_edges; ee++)
2256 TA_Edge edge = axis->edges + ee;
2257 FT_Pos dist;
2260 dist = seg->pos - edge->fpos;
2261 if (dist < 0)
2262 dist = -dist;
2264 if (dist < edge_distance_threshold && edge->dir == seg->dir)
2266 found = edge;
2267 break;
2271 if (!found)
2273 TA_Edge edge;
2276 /* insert a new edge in the list and sort according to the position */
2277 error = ta_axis_hints_new_edge(axis, seg->pos,
2278 (TA_Direction)seg->dir,
2279 top_to_bottom_hinting,
2280 &edge);
2281 if (error)
2282 goto Exit;
2284 /* add the segment to the new edge's list */
2285 memset(edge, 0, sizeof (TA_EdgeRec));
2286 edge->first = seg;
2287 edge->last = seg;
2288 edge->dir = seg->dir;
2289 edge->fpos = seg->pos;
2290 edge->opos = FT_MulFix(seg->pos, scale);
2291 edge->pos = edge->opos;
2292 seg->edge_next = seg;
2294 else
2296 /* if an edge was found, simply add the segment to the edge's list */
2297 seg->edge_next = found->first;
2298 found->last->edge_next = seg;
2299 found->last = seg;
2303 /* we loop again over all segments to catch one-point segments */
2304 /* without a direction: if possible, link them to existing edges */
2305 for (seg = segments; seg < segment_limit; seg++)
2307 TA_Edge found = NULL;
2308 FT_Int ee;
2311 if (seg->dir != TA_DIR_NONE)
2312 continue;
2314 /* look for an edge corresponding to the segment */
2315 for (ee = 0; ee < axis->num_edges; ee++)
2317 TA_Edge edge = axis->edges + ee;
2318 FT_Pos dist;
2321 dist = seg->pos - edge->fpos;
2322 if (dist < 0)
2323 dist = -dist;
2325 if (dist < edge_distance_threshold)
2327 found = edge;
2328 break;
2332 /* one-point segments without a match are ignored */
2333 if (found)
2335 seg->edge_next = found->first;
2336 found->last->edge_next = seg;
2337 found->last = seg;
2341 /*****************************************************************/
2342 /* */
2343 /* Good, we now compute each edge's properties according to */
2344 /* the segments found on its position. Basically, these are */
2345 /* */
2346 /* - the edge's main direction */
2347 /* - stem edge, serif edge or both (which defaults to stem then) */
2348 /* - rounded edge, straight or both (which defaults to straight) */
2349 /* - link for edge */
2350 /* */
2351 /*****************************************************************/
2353 /* first of all, set the `edge' field in each segment -- this is */
2354 /* required in order to compute edge links */
2356 /* note that removing this loop and setting the `edge' field of each */
2357 /* segment directly in the code above slows down execution speed for */
2358 /* some reasons on platforms like the Sun */
2360 TA_Edge edges = axis->edges;
2361 TA_Edge edge_limit = edges + axis->num_edges;
2362 TA_Edge edge;
2365 for (edge = edges; edge < edge_limit; edge++)
2367 seg = edge->first;
2368 if (seg)
2371 seg->edge = edge;
2372 seg = seg->edge_next;
2373 } while (seg != edge->first);
2376 /* now compute each edge properties */
2377 for (edge = edges; edge < edge_limit; edge++)
2379 FT_Int is_round = 0; /* does it contain round segments? */
2380 FT_Int is_straight = 0; /* does it contain straight segments? */
2381 #if 0
2382 FT_Pos ups = 0; /* number of upwards segments */
2383 FT_Pos downs = 0; /* number of downwards segments */
2384 #endif
2387 seg = edge->first;
2391 FT_Bool is_serif;
2394 /* check for roundness of segment */
2395 if (seg->flags & TA_EDGE_ROUND)
2396 is_round++;
2397 else
2398 is_straight++;
2400 #if 0
2401 /* check for segment direction */
2402 if (seg->dir == up_dir)
2403 ups += seg->max_coord - seg->min_coord;
2404 else
2405 downs += seg->max_coord - seg->min_coord;
2406 #endif
2408 /* check for links -- */
2409 /* if seg->serif is set, then seg->link must be ignored */
2410 is_serif = (FT_Bool)(seg->serif
2411 && seg->serif->edge
2412 && seg->serif->edge != edge);
2414 if ((seg->link && seg->link->edge)
2415 || is_serif)
2417 TA_Edge edge2;
2418 TA_Segment seg2;
2421 edge2 = edge->link;
2422 seg2 = seg->link;
2424 if (is_serif)
2426 seg2 = seg->serif;
2427 edge2 = edge->serif;
2430 if (edge2)
2432 FT_Pos edge_delta;
2433 FT_Pos seg_delta;
2436 edge_delta = edge->fpos - edge2->fpos;
2437 if (edge_delta < 0)
2438 edge_delta = -edge_delta;
2440 seg_delta = seg->pos - seg2->pos;
2441 if (seg_delta < 0)
2442 seg_delta = -seg_delta;
2444 if (seg_delta < edge_delta)
2445 edge2 = seg2->edge;
2447 else
2448 edge2 = seg2->edge;
2450 if (is_serif)
2452 edge->serif = edge2;
2453 edge2->flags |= TA_EDGE_SERIF;
2455 else
2456 edge->link = edge2;
2459 seg = seg->edge_next;
2460 } while (seg != edge->first);
2462 /* set the round/straight flags */
2463 edge->flags = TA_EDGE_NORMAL;
2465 if (is_round > 0
2466 && is_round >= is_straight)
2467 edge->flags |= TA_EDGE_ROUND;
2469 #if 0
2470 /* set the edge's main direction */
2471 edge->dir = TA_DIR_NONE;
2473 if (ups > downs)
2474 edge->dir = (FT_Char)up_dir;
2476 else if (ups < downs)
2477 edge->dir = (FT_Char)-up_dir;
2479 else if (ups == downs)
2480 edge->dir = 0; /* both up and down! */
2481 #endif
2483 /* get rid of serifs if link is set */
2484 /* XXX: this gets rid of many unpleasant artefacts! */
2485 /* example: the `c' in cour.pfa at size 13 */
2487 if (edge->serif && edge->link)
2488 edge->serif = NULL;
2492 Exit:
2493 return error;
2497 /* detect segments and edges for given dimension */
2499 FT_Error
2500 ta_latin_hints_detect_features(TA_GlyphHints hints,
2501 FT_UInt width_count,
2502 TA_WidthRec* widths,
2503 TA_Dimension dim)
2505 FT_Error error;
2508 error = ta_latin_hints_compute_segments(hints, dim);
2509 if (!error)
2511 ta_latin_hints_link_segments(hints, width_count, widths, dim);
2513 error = ta_latin_hints_compute_edges(hints, dim);
2516 return error;
2520 /* compute all edges which lie within blue zones */
2522 static void
2523 ta_latin_hints_compute_blue_edges(TA_GlyphHints hints,
2524 TA_LatinMetrics metrics)
2526 TA_AxisHints axis = &hints->axis[TA_DIMENSION_VERT];
2528 TA_Edge edge = axis->edges;
2529 TA_Edge edge_limit = edge + axis->num_edges;
2531 TA_LatinAxis latin = &metrics->axis[TA_DIMENSION_VERT];
2532 FT_Fixed scale = latin->scale;
2535 /* compute which blue zones are active, */
2536 /* i.e. have their scaled size < 3/4 pixels */
2538 /* for each horizontal edge search the blue zone which is closest */
2539 for (; edge < edge_limit; edge++)
2541 FT_UInt bb;
2542 TA_Width best_blue = NULL;
2543 FT_Bool best_blue_is_neutral = 0;
2544 FT_Pos best_dist; /* initial threshold */
2546 FT_UInt best_blue_idx = 0;
2547 FT_Bool best_blue_is_shoot = 0;
2550 /* compute the initial threshold as a fraction of the EM size */
2551 /* (the value 40 is heuristic) */
2552 best_dist = FT_MulFix(metrics->units_per_em / 40, scale);
2554 /* assure a minimum distance of 0.5px */
2555 if (best_dist > 64 / 2)
2556 best_dist = 64 / 2;
2558 /* this loop also handles the two extra blue zones */
2559 /* for usWinAscent and usWinDescent */
2560 /* if option `windows-compatibility' is set */
2561 for (bb = 0;
2562 bb < latin->blue_count
2563 + (metrics->root.globals->font->windows_compatibility ? 2 : 0);
2564 bb++)
2566 TA_LatinBlue blue = latin->blues + bb;
2567 FT_Bool is_top_blue, is_neutral_blue, is_major_dir;
2570 /* skip inactive blue zones (i.e., those that are too large) */
2571 if (!(blue->flags & TA_LATIN_BLUE_ACTIVE))
2572 continue;
2574 /* if it is a top zone, check for right edges (against the major */
2575 /* direction); if it is a bottom zone, check for left edges (in */
2576 /* the major direction) */
2577 is_top_blue = (FT_Byte)((blue->flags & (TA_LATIN_BLUE_TOP
2578 | TA_LATIN_BLUE_SUB_TOP)) != 0);
2579 is_neutral_blue = (FT_Byte)((blue->flags & TA_LATIN_BLUE_NEUTRAL) != 0);
2580 is_major_dir = FT_BOOL(edge->dir == axis->major_dir);
2582 /* neutral blue zones are handled for both directions */
2583 if (is_top_blue ^ is_major_dir || is_neutral_blue)
2585 FT_Pos dist;
2588 /* first of all, compare it to the reference position */
2589 dist = edge->fpos - blue->ref.org;
2590 if (dist < 0)
2591 dist = -dist;
2593 dist = FT_MulFix(dist, scale);
2594 if (dist < best_dist)
2596 best_dist = dist;
2597 best_blue = &blue->ref;
2598 best_blue_is_neutral = is_neutral_blue;
2600 best_blue_idx = bb;
2601 best_blue_is_shoot = 0;
2604 /* now compare it to the overshoot position and check whether */
2605 /* the edge is rounded, and whether the edge is over the */
2606 /* reference position of a top zone, or under the reference */
2607 /* position of a bottom zone (provided we don't have a */
2608 /* neutral blue zone) */
2609 if (edge->flags & TA_EDGE_ROUND
2610 && dist != 0
2611 && !is_neutral_blue)
2613 FT_Bool is_under_ref = FT_BOOL(edge->fpos < blue->ref.org);
2616 if (is_top_blue ^ is_under_ref)
2618 dist = edge->fpos - blue->shoot.org;
2619 if (dist < 0)
2620 dist = -dist;
2622 dist = FT_MulFix(dist, scale);
2623 if (dist < best_dist)
2625 best_dist = dist;
2626 best_blue = &blue->shoot;
2627 best_blue_is_neutral = is_neutral_blue;
2629 best_blue_idx = bb;
2630 best_blue_is_shoot = 1;
2637 if (best_blue)
2639 edge->blue_edge = best_blue;
2640 edge->best_blue_idx = best_blue_idx;
2641 edge->best_blue_is_shoot = best_blue_is_shoot;
2642 if (best_blue_is_neutral)
2643 edge->flags |= TA_EDGE_NEUTRAL;
2649 /* initalize hinting engine */
2651 static FT_Error
2652 ta_latin_hints_init(TA_GlyphHints hints,
2653 TA_LatinMetrics metrics)
2655 FT_Render_Mode mode;
2656 FT_UInt32 scaler_flags, other_flags;
2657 FT_Face face = metrics->root.scaler.face;
2660 ta_glyph_hints_rescale(hints, (TA_StyleMetrics)metrics);
2662 /* correct x_scale and y_scale if needed, since they may have */
2663 /* been modified by `ta_latin_metrics_scale_dim' above */
2664 hints->x_scale = metrics->axis[TA_DIMENSION_HORZ].scale;
2665 hints->x_delta = metrics->axis[TA_DIMENSION_HORZ].delta;
2666 hints->y_scale = metrics->axis[TA_DIMENSION_VERT].scale;
2667 hints->y_delta = metrics->axis[TA_DIMENSION_VERT].delta;
2669 /* compute flags depending on render mode, etc. */
2670 mode = metrics->root.scaler.render_mode;
2672 #if 0 /* #ifdef TA_CONFIG_OPTION_USE_WARPER */
2673 if (mode == FT_RENDER_MODE_LCD
2674 || mode == FT_RENDER_MODE_LCD_V)
2675 metrics->root.scaler.render_mode =
2676 mode = FT_RENDER_MODE_NORMAL;
2677 #endif
2679 scaler_flags = hints->scaler_flags;
2680 other_flags = 0;
2682 /* we snap the width of vertical stems for the monochrome */
2683 /* and horizontal LCD rendering targets only */
2684 if (mode == FT_RENDER_MODE_MONO
2685 || mode == FT_RENDER_MODE_LCD)
2686 other_flags |= TA_LATIN_HINTS_HORZ_SNAP;
2688 /* we snap the width of horizontal stems for the monochrome */
2689 /* and vertical LCD rendering targets only */
2690 if (mode == FT_RENDER_MODE_MONO
2691 || mode == FT_RENDER_MODE_LCD_V)
2692 other_flags |= TA_LATIN_HINTS_VERT_SNAP;
2694 /* we adjust stems to full pixels unless in `light' or `lcd' mode */
2695 if (mode != FT_RENDER_MODE_LIGHT && mode != FT_RENDER_MODE_LCD)
2696 other_flags |= TA_LATIN_HINTS_STEM_ADJUST;
2698 if (mode == FT_RENDER_MODE_MONO)
2699 other_flags |= TA_LATIN_HINTS_MONO;
2701 /* in `light' or `lcd' mode we disable horizontal hinting completely; */
2702 /* we also do it if the face is italic -- */
2703 /* however, if warping is enabled (which only works in `light' hinting */
2704 /* mode), advance widths get adjusted, too */
2705 if (mode == FT_RENDER_MODE_LIGHT || mode == FT_RENDER_MODE_LCD
2706 || (face->style_flags & FT_STYLE_FLAG_ITALIC) != 0)
2707 scaler_flags |= TA_SCALER_FLAG_NO_HORIZONTAL;
2709 #ifdef TA_CONFIG_OPTION_USE_WARPER
2710 /* get (global) warper flag */
2711 if (!metrics->root.globals->module->warping)
2712 scaler_flags |= TA_SCALER_FLAG_NO_WARPER;
2713 #endif
2715 hints->scaler_flags = scaler_flags;
2716 hints->other_flags = other_flags;
2718 return FT_Err_Ok;
2722 /* snap a given width in scaled coordinates to */
2723 /* one of the current standard widths */
2725 static FT_Pos
2726 ta_latin_snap_width(TA_Width widths,
2727 FT_UInt count,
2728 FT_Pos width)
2730 FT_UInt n;
2731 FT_Pos best = 64 + 32 + 2;
2732 FT_Pos reference = width;
2733 FT_Pos scaled;
2736 for (n = 0; n < count; n++)
2738 FT_Pos w;
2739 FT_Pos dist;
2742 w = widths[n].cur;
2743 dist = width - w;
2744 if (dist < 0)
2745 dist = -dist;
2746 if (dist < best)
2748 best = dist;
2749 reference = w;
2753 scaled = TA_PIX_ROUND(reference);
2755 if (width >= reference)
2757 if (width < scaled + 48)
2758 width = reference;
2760 else
2762 if (width > scaled - 48)
2763 width = reference;
2766 return width;
2770 /* compute the snapped width of a given stem, ignoring very thin ones */
2772 /* there is a lot of voodoo in this function; changing the hard-coded */
2773 /* parameters influences the whole hinting process */
2775 static FT_Pos
2776 ta_latin_compute_stem_width(TA_GlyphHints hints,
2777 TA_Dimension dim,
2778 FT_Pos width,
2779 FT_Pos base_delta,
2780 FT_Byte base_flags,
2781 FT_Byte stem_flags)
2783 TA_LatinMetrics metrics = (TA_LatinMetrics) hints->metrics;
2784 TA_LatinAxis axis = &metrics->axis[dim];
2786 FT_Pos dist = width;
2787 FT_Int sign = 0;
2788 FT_Int vertical = (dim == TA_DIMENSION_VERT);
2791 if (!TA_LATIN_HINTS_DO_STEM_ADJUST(hints)
2792 || axis->extra_light)
2793 return width;
2795 if (dist < 0)
2797 dist = -width;
2798 sign = 1;
2801 if ((vertical && !TA_LATIN_HINTS_DO_VERT_SNAP(hints))
2802 || (!vertical && !TA_LATIN_HINTS_DO_HORZ_SNAP(hints)))
2804 /* smooth hinting process: very lightly quantize the stem width */
2806 /* leave the widths of serifs alone */
2807 if ((stem_flags & TA_EDGE_SERIF)
2808 && vertical
2809 && (dist < 3 * 64))
2810 goto Done_Width;
2811 else if (base_flags & TA_EDGE_ROUND)
2813 if (dist < 80)
2814 dist = 64;
2816 else if (dist < 56)
2817 dist = 56;
2819 if (axis->width_count > 0)
2821 FT_Pos delta;
2824 /* compare to standard width */
2825 delta = dist - axis->widths[0].cur;
2827 if (delta < 0)
2828 delta = -delta;
2830 if (delta < 40)
2832 dist = axis->widths[0].cur;
2833 if (dist < 48)
2834 dist = 48;
2836 goto Done_Width;
2839 if (dist < 3 * 64)
2841 delta = dist & 63;
2842 dist &= -64;
2844 if (delta < 10)
2845 dist += delta;
2846 else if (delta < 32)
2847 dist += 10;
2848 else if (delta < 54)
2849 dist += 54;
2850 else
2851 dist += delta;
2853 else
2855 /* A stem's end position depends on two values: the start */
2856 /* position and the stem length. The former gets usually */
2857 /* rounded to the grid, while the latter gets rounded also if it */
2858 /* exceeds a certain length (see below in this function). This */
2859 /* `double rounding' can lead to a great difference to the */
2860 /* original, unhinted position; this normally doesn't matter for */
2861 /* large PPEM values, but for small sizes it can easily make */
2862 /* outlines collide. For this reason, we adjust the stem length */
2863 /* by a small amount depending on the PPEM value in case the */
2864 /* former and latter rounding both point into the same */
2865 /* direction. */
2867 FT_Pos bdelta = 0;
2870 if (((width > 0) && (base_delta > 0))
2871 || ((width < 0) && (base_delta < 0)))
2873 FT_UInt ppem = metrics->root.scaler.face->size->metrics.x_ppem;
2876 if (ppem < 10)
2877 bdelta = base_delta;
2878 else if (ppem < 30)
2879 bdelta = (base_delta * (FT_Pos)(30 - ppem)) / 20;
2881 if (bdelta < 0)
2882 bdelta = -bdelta;
2885 dist = (dist - bdelta + 32) & ~63;
2889 else
2891 /* strong hinting process: snap the stem width to integer pixels */
2893 FT_Pos org_dist = dist;
2896 dist = ta_latin_snap_width(axis->widths, axis->width_count, dist);
2898 if (vertical)
2900 /* in the case of vertical hinting, */
2901 /* always round the stem heights to integer pixels */
2903 if (dist >= 64)
2904 dist = (dist + 16) & ~63;
2905 else
2906 dist = 64;
2908 else
2910 if (TA_LATIN_HINTS_DO_MONO(hints))
2912 /* monochrome horizontal hinting: */
2913 /* snap widths to integer pixels with a different threshold */
2915 if (dist < 64)
2916 dist = 64;
2917 else
2918 dist = (dist + 32) & ~63;
2920 else
2922 /* for horizontal anti-aliased hinting, we adopt a more subtle */
2923 /* approach: we strengthen small stems, round stems whose size */
2924 /* is between 1 and 2 pixels to an integer, otherwise nothing */
2926 if (dist < 48)
2927 dist = (dist + 64) >> 1;
2929 else if (dist < 128)
2931 /* we only round to an integer width if the corresponding */
2932 /* distortion is less than 1/4 pixel -- otherwise, this */
2933 /* makes everything worse since the diagonals, which are */
2934 /* not hinted, appear a lot bolder or thinner than the */
2935 /* vertical stems */
2937 FT_Pos delta;
2940 dist = (dist + 22) & ~63;
2941 delta = dist - org_dist;
2942 if (delta < 0)
2943 delta = -delta;
2945 if (delta >= 16)
2947 dist = org_dist;
2948 if (dist < 48)
2949 dist = (dist + 64) >> 1;
2952 else
2953 /* round otherwise to prevent color fringes in LCD mode */
2954 dist = (dist + 32) & ~63;
2959 Done_Width:
2960 if (sign)
2961 dist = -dist;
2963 return dist;
2967 /* align one stem edge relative to the previous stem edge */
2969 static void
2970 ta_latin_align_linked_edge(TA_GlyphHints hints,
2971 TA_Dimension dim,
2972 TA_Edge base_edge,
2973 TA_Edge stem_edge)
2975 FT_Pos dist, base_delta;
2976 FT_Pos fitted_width;
2979 dist = stem_edge->opos - base_edge->opos;
2980 base_delta = base_edge->pos - base_edge->opos;
2983 fitted_width = ta_latin_compute_stem_width(hints, dim,
2984 dist, base_delta,
2985 base_edge->flags,
2986 stem_edge->flags);
2988 stem_edge->pos = base_edge->pos + fitted_width;
2990 TA_LOG((" LINK: edge %d (opos=%.2f) linked to %.2f,"
2991 " dist was %.2f, now %.2f\n",
2992 stem_edge - hints->axis[dim].edges, stem_edge->opos / 64.0,
2993 stem_edge->pos / 64.0, dist / 64.0, fitted_width / 64.0));
2995 if (hints->recorder)
2996 hints->recorder(ta_link, hints, dim,
2997 base_edge, stem_edge, NULL, NULL, NULL);
3001 /* shift the coordinates of the `serif' edge by the same amount */
3002 /* as the corresponding `base' edge has been moved already */
3004 static void
3005 ta_latin_align_serif_edge(TA_GlyphHints hints,
3006 TA_Edge base,
3007 TA_Edge serif)
3009 FT_UNUSED(hints);
3011 serif->pos = base->pos + (serif->opos - base->opos);
3015 /* the main grid-fitting routine */
3017 static void
3018 ta_latin_hint_edges(TA_GlyphHints hints,
3019 TA_Dimension dim)
3021 TA_AxisHints axis = &hints->axis[dim];
3023 TA_Edge edges = axis->edges;
3024 TA_Edge edge_limit = edges + axis->num_edges;
3025 FT_PtrDist n_edges;
3026 TA_Edge edge;
3028 TA_Edge anchor = NULL;
3029 FT_Int has_serifs = 0;
3031 TA_StyleClass style_class = hints->metrics->style_class;
3032 TA_ScriptClass script_class = ta_script_classes[style_class->script];
3034 FT_Bool top_to_bottom_hinting = 0;
3036 #ifdef TA_DEBUG
3037 FT_UInt num_actions = 0;
3038 #endif
3040 TA_LOG(("latin %s edge hinting (style `%s')\n",
3041 dim == TA_DIMENSION_VERT ? "horizontal" : "vertical",
3042 ta_style_names[hints->metrics->style_class->style]));
3044 if (dim == TA_DIMENSION_VERT)
3045 top_to_bottom_hinting = script_class->top_to_bottom_hinting;
3047 /* we begin by aligning all stems relative to the blue zone if needed -- */
3048 /* that's only for horizontal edges */
3050 if (dim == TA_DIMENSION_VERT
3051 && TA_HINTS_DO_BLUES(hints))
3053 for (edge = edges; edge < edge_limit; edge++)
3055 TA_Width blue;
3056 TA_Edge edge1, edge2; /* these edges form the stem to check */
3059 if (edge->flags & TA_EDGE_DONE)
3060 continue;
3062 edge1 = NULL;
3063 edge2 = edge->link;
3066 * If a stem contains both a neutral and a non-neutral blue zone,
3067 * skip the neutral one. Otherwise, outlines with different
3068 * directions might be incorrectly aligned at the same vertical
3069 * position.
3071 * If we have two neutral blue zones, skip one of them.
3073 if (edge->blue_edge && edge2 && edge2->blue_edge)
3075 FT_Byte neutral = edge->flags & TA_EDGE_NEUTRAL;
3076 FT_Byte neutral2 = edge2->flags & TA_EDGE_NEUTRAL;
3079 if (neutral2)
3081 edge2->blue_edge = NULL;
3082 edge2->flags &= ~TA_EDGE_NEUTRAL;
3084 else if (neutral)
3086 edge->blue_edge = NULL;
3087 edge->flags &= ~TA_EDGE_NEUTRAL;
3091 blue = edge->blue_edge;
3092 if (blue)
3093 edge1 = edge;
3095 /* flip edges if the other edge is aligned to a blue zone */
3096 else if (edge2 && edge2->blue_edge)
3098 blue = edge2->blue_edge;
3099 edge1 = edge2;
3100 edge2 = edge;
3103 if (!edge1)
3104 continue;
3106 #ifdef TA_DEBUG
3107 if (!anchor)
3108 TA_LOG((" BLUE_ANCHOR: edge %d (opos=%.2f) snapped to %.2f,"
3109 " was %.2f (anchor=edge %d)\n",
3110 edge1 - edges, edge1->opos / 64.0, blue->fit / 64.0,
3111 edge1->pos / 64.0, edge - edges));
3112 else
3113 TA_LOG((" BLUE: edge %d (opos=%.2f) snapped to %.2f, was %.2f\n",
3114 edge1 - edges, edge1->opos / 64.0, blue->fit / 64.0,
3115 edge1->pos / 64.0));
3117 num_actions++;
3118 #endif
3120 edge1->pos = blue->fit;
3121 edge1->flags |= TA_EDGE_DONE;
3123 if (hints->recorder)
3125 if (!anchor)
3126 hints->recorder(ta_blue_anchor, hints, dim,
3127 edge1, edge, NULL, NULL, NULL);
3128 else
3129 hints->recorder(ta_blue, hints, dim,
3130 edge1, NULL, NULL, NULL, NULL);
3133 if (edge2 && !edge2->blue_edge)
3135 ta_latin_align_linked_edge(hints, dim, edge1, edge2);
3136 edge2->flags |= TA_EDGE_DONE;
3138 #ifdef TA_DEBUG
3139 num_actions++;
3140 #endif
3143 if (!anchor)
3144 anchor = edge;
3148 /* now we align all other stem edges, */
3149 /* trying to maintain the relative order of stems in the glyph */
3150 for (edge = edges; edge < edge_limit; edge++)
3152 TA_Edge edge2;
3155 if (edge->flags & TA_EDGE_DONE)
3156 continue;
3158 /* skip all non-stem edges */
3159 edge2 = edge->link;
3160 if (!edge2)
3162 has_serifs++;
3163 continue;
3166 /* now align the stem */
3168 /* this should not happen, but it's better to be safe */
3169 if (edge2->blue_edge)
3171 TA_LOG((" ASSERTION FAILED for edge %d\n", edge2 - edges));
3173 ta_latin_align_linked_edge(hints, dim, edge2, edge);
3174 edge->flags |= TA_EDGE_DONE;
3176 #ifdef TA_DEBUG
3177 num_actions++;
3178 #endif
3179 continue;
3182 if (!anchor)
3184 /* if we reach this if clause, no stem has been aligned yet */
3186 FT_Pos org_len, org_center, cur_len;
3187 FT_Pos cur_pos1, error1, error2, u_off, d_off;
3190 org_len = edge2->opos - edge->opos;
3191 cur_len = ta_latin_compute_stem_width(hints, dim,
3192 org_len, 0,
3193 edge->flags, edge2->flags);
3195 /* some voodoo to specially round edges for small stem widths; */
3196 /* the idea is to align the center of a stem, */
3197 /* then shifting the stem edges to suitable positions */
3198 if (cur_len <= 64)
3200 /* width <= 1px */
3201 u_off = 32;
3202 d_off = 32;
3204 else
3206 /* 1px < width < 1.5px */
3207 u_off = 38;
3208 d_off = 26;
3211 if (cur_len < 96)
3213 org_center = edge->opos + (org_len >> 1);
3214 cur_pos1 = TA_PIX_ROUND(org_center);
3216 error1 = org_center - (cur_pos1 - u_off);
3217 if (error1 < 0)
3218 error1 = -error1;
3220 error2 = org_center - (cur_pos1 + d_off);
3221 if (error2 < 0)
3222 error2 = -error2;
3224 if (error1 < error2)
3225 cur_pos1 -= u_off;
3226 else
3227 cur_pos1 += d_off;
3229 edge->pos = cur_pos1 - cur_len / 2;
3230 edge2->pos = edge->pos + cur_len;
3232 else
3233 edge->pos = TA_PIX_ROUND(edge->opos);
3235 anchor = edge;
3236 edge->flags |= TA_EDGE_DONE;
3238 TA_LOG((" ANCHOR: edge %d (opos=%.2f) and %d (opos=%.2f)"
3239 " snapped to %.2f and %.2f\n",
3240 edge - edges, edge->opos / 64.0,
3241 edge2 - edges, edge2->opos / 64.0,
3242 edge->pos / 64.0, edge2->pos / 64.0));
3244 if (hints->recorder)
3245 hints->recorder(ta_anchor, hints, dim,
3246 edge, edge2, NULL, NULL, NULL);
3248 ta_latin_align_linked_edge(hints, dim, edge, edge2);
3250 #ifdef TA_DEBUG
3251 num_actions += 2;
3252 #endif
3254 else
3256 FT_Pos org_pos, org_len, org_center, cur_len;
3257 FT_Pos cur_pos1, cur_pos2, delta1, delta2;
3260 org_pos = anchor->pos + (edge->opos - anchor->opos);
3261 org_len = edge2->opos - edge->opos;
3262 org_center = org_pos + (org_len >> 1);
3264 cur_len = ta_latin_compute_stem_width(hints, dim,
3265 org_len, 0,
3266 edge->flags, edge2->flags);
3268 if (edge2->flags & TA_EDGE_DONE)
3270 TA_LOG((" ADJUST: edge %d (pos=%.2f) moved to %.2f\n",
3271 edge - edges, edge->pos / 64.0,
3272 (edge2->pos - cur_len) / 64.0));
3274 edge->pos = edge2->pos - cur_len;
3276 if (hints->recorder)
3278 TA_Edge bound = NULL;
3281 if (edge > edges)
3282 bound = &edge[-1];
3284 hints->recorder(ta_adjust, hints, dim,
3285 edge, edge2, NULL, bound, NULL);
3289 else if (cur_len < 96)
3291 FT_Pos u_off, d_off;
3294 cur_pos1 = TA_PIX_ROUND(org_center);
3296 if (cur_len <= 64)
3298 u_off = 32;
3299 d_off = 32;
3301 else
3303 u_off = 38;
3304 d_off = 26;
3307 delta1 = org_center - (cur_pos1 - u_off);
3308 if (delta1 < 0)
3309 delta1 = -delta1;
3311 delta2 = org_center - (cur_pos1 + d_off);
3312 if (delta2 < 0)
3313 delta2 = -delta2;
3315 if (delta1 < delta2)
3316 cur_pos1 -= u_off;
3317 else
3318 cur_pos1 += d_off;
3320 edge->pos = cur_pos1 - cur_len / 2;
3321 edge2->pos = cur_pos1 + cur_len / 2;
3323 TA_LOG((" STEM: edge %d (opos=%.2f) linked to %d (opos=%.2f)"
3324 " snapped to %.2f and %.2f\n",
3325 edge - edges, edge->opos / 64.0,
3326 edge2 - edges, edge2->opos / 64.0,
3327 edge->pos / 64.0, edge2->pos / 64.0));
3329 if (hints->recorder)
3331 TA_Edge bound = NULL;
3334 if (edge > edges)
3335 bound = &edge[-1];
3337 hints->recorder(ta_stem, hints, dim,
3338 edge, edge2, NULL, bound, NULL);
3342 else
3344 org_pos = anchor->pos + (edge->opos - anchor->opos);
3345 org_len = edge2->opos - edge->opos;
3346 org_center = org_pos + (org_len >> 1);
3348 cur_len = ta_latin_compute_stem_width(hints, dim,
3349 org_len, 0,
3350 edge->flags, edge2->flags);
3352 cur_pos1 = TA_PIX_ROUND(org_pos);
3353 delta1 = cur_pos1 + (cur_len >> 1) - org_center;
3354 if (delta1 < 0)
3355 delta1 = -delta1;
3357 cur_pos2 = TA_PIX_ROUND(org_pos + org_len) - cur_len;
3358 delta2 = cur_pos2 + (cur_len >> 1) - org_center;
3359 if (delta2 < 0)
3360 delta2 = -delta2;
3362 edge->pos = (delta1 < delta2) ? cur_pos1 : cur_pos2;
3363 edge2->pos = edge->pos + cur_len;
3365 TA_LOG((" STEM: edge %d (opos=%.2f) linked to %d (opos=%.2f)"
3366 " snapped to %.2f and %.2f\n",
3367 edge - edges, edge->opos / 64.0,
3368 edge2 - edges, edge2->opos / 64.0,
3369 edge->pos / 64.0, edge2->pos / 64.0));
3371 if (hints->recorder)
3373 TA_Edge bound = NULL;
3376 if (edge > edges)
3377 bound = &edge[-1];
3379 hints->recorder(ta_stem, hints, dim,
3380 edge, edge2, NULL, bound, NULL);
3384 #ifdef TA_DEBUG
3385 num_actions++;
3386 #endif
3388 edge->flags |= TA_EDGE_DONE;
3389 edge2->flags |= TA_EDGE_DONE;
3391 if (edge > edges
3392 && (top_to_bottom_hinting ? (edge->pos > edge[-1].pos)
3393 : (edge->pos < edge[-1].pos)))
3395 /* don't move if stem would (almost) disappear otherwise; */
3396 /* the ad-hoc value 16 corresponds to 1/4px */
3397 if (edge->link
3398 && TA_ABS(edge->link->pos - edge[-1].pos) > 16)
3400 #ifdef TA_DEBUG
3401 TA_LOG((" BOUND: edge %d (pos=%.2f) moved to %.2f\n",
3402 edge - edges, edge->pos / 64.0, edge[-1].pos / 64.0));
3404 num_actions++;
3405 #endif
3407 edge->pos = edge[-1].pos;
3409 if (hints->recorder)
3410 hints->recorder(ta_bound, hints, dim,
3411 edge, &edge[-1], NULL, NULL, NULL);
3417 /* make sure that lowercase m's maintain their symmetry */
3419 /* In general, lowercase m's have six vertical edges if they are sans */
3420 /* serif, or twelve if they are with serifs. This implementation is */
3421 /* based on that assumption, and seems to work very well with most */
3422 /* faces. However, if for a certain face this assumption is not */
3423 /* true, the m is just rendered like before. In addition, any stem */
3424 /* correction will only be applied to symmetrical glyphs (even if the */
3425 /* glyph is not an m), so the potential for unwanted distortion is */
3426 /* relatively low. */
3428 /* we don't handle horizontal edges since we can't easily assure that */
3429 /* the third (lowest) stem aligns with the base line; it might end up */
3430 /* one pixel higher or lower */
3432 n_edges = edge_limit - edges;
3433 if (dim == TA_DIMENSION_HORZ
3434 && (n_edges == 6 || n_edges == 12))
3436 TA_Edge edge1, edge2, edge3;
3437 FT_Pos dist1, dist2, span, delta;
3440 if (n_edges == 6)
3442 edge1 = edges;
3443 edge2 = edges + 2;
3444 edge3 = edges + 4;
3446 else
3448 edge1 = edges + 1;
3449 edge2 = edges + 5;
3450 edge3 = edges + 9;
3453 dist1 = edge2->opos - edge1->opos;
3454 dist2 = edge3->opos - edge2->opos;
3456 span = dist1 - dist2;
3457 if (span < 0)
3458 span = -span;
3460 if (span < 8)
3462 delta = edge3->pos - (2 * edge2->pos - edge1->pos);
3463 edge3->pos -= delta;
3464 if (edge3->link)
3465 edge3->link->pos -= delta;
3467 /* move the serifs along with the stem */
3468 if (n_edges == 12)
3470 (edges + 8)->pos -= delta;
3471 (edges + 11)->pos -= delta;
3474 edge3->flags |= TA_EDGE_DONE;
3475 if (edge3->link)
3476 edge3->link->flags |= TA_EDGE_DONE;
3480 if (has_serifs || !anchor)
3482 /* now hint the remaining edges (serifs and single) */
3483 /* in order to complete our processing */
3484 for (edge = edges; edge < edge_limit; edge++)
3486 TA_Edge lower_bound = NULL;
3487 TA_Edge upper_bound = NULL;
3489 FT_Pos delta;
3492 if (edge->flags & TA_EDGE_DONE)
3493 continue;
3495 delta = 1000;
3497 if (edge->serif)
3499 delta = edge->serif->opos - edge->opos;
3500 if (delta < 0)
3501 delta = -delta;
3504 if (edge > edges)
3505 lower_bound = &edge[-1];
3507 if (edge + 1 < edge_limit
3508 && edge[1].flags & TA_EDGE_DONE)
3509 upper_bound = &edge[1];
3512 if (delta < 64 + 16)
3514 ta_latin_align_serif_edge(hints, edge->serif, edge);
3516 TA_LOG((" SERIF: edge %d (opos=%.2f) serif to %d (opos=%.2f)"
3517 " aligned to %.2f\n",
3518 edge - edges, edge->opos / 64.0,
3519 edge->serif - edges, edge->serif->opos / 64.0,
3520 edge->pos / 64.0));
3522 if (hints->recorder)
3523 hints->recorder(ta_serif, hints, dim,
3524 edge, NULL, NULL, lower_bound, upper_bound);
3526 else if (!anchor)
3528 edge->pos = TA_PIX_ROUND(edge->opos);
3529 anchor = edge;
3531 TA_LOG((" SERIF_ANCHOR: edge %d (opos=%.2f) snapped to %.2f\n",
3532 edge - edges, edge->opos / 64.0, edge->pos / 64.0));
3534 if (hints->recorder)
3535 hints->recorder(ta_serif_anchor, hints, dim,
3536 edge, NULL, NULL, lower_bound, upper_bound);
3538 else
3540 TA_Edge before, after;
3543 for (before = edge - 1; before >= edges; before--)
3544 if (before->flags & TA_EDGE_DONE)
3545 break;
3547 for (after = edge + 1; after < edge_limit; after++)
3548 if (after->flags & TA_EDGE_DONE)
3549 break;
3551 if (before >= edges && before < edge
3552 && after < edge_limit && after > edge)
3554 if (after->opos == before->opos)
3555 edge->pos = before->pos;
3556 else
3557 edge->pos = before->pos + FT_MulDiv(edge->opos - before->opos,
3558 after->pos - before->pos,
3559 after->opos - before->opos);
3561 TA_LOG((" SERIF_LINK1: edge %d (opos=%.2f) snapped to %.2f"
3562 " from %d (opos=%.2f)\n",
3563 edge - edges, edge->opos / 64.0,
3564 edge->pos / 64.0,
3565 before - edges, before->opos / 64.0));
3567 if (hints->recorder)
3568 hints->recorder(ta_serif_link1, hints, dim,
3569 edge, before, after, lower_bound, upper_bound);
3571 else
3573 edge->pos = anchor->pos + ((edge->opos - anchor->opos + 16) & ~31);
3574 TA_LOG((" SERIF_LINK2: edge %d (opos=%.2f) snapped to %.2f\n",
3575 edge - edges, edge->opos / 64.0, edge->pos / 64.0));
3577 if (hints->recorder)
3578 hints->recorder(ta_serif_link2, hints, dim,
3579 edge, NULL, NULL, lower_bound, upper_bound);
3583 #ifdef TA_DEBUG
3584 num_actions++;
3585 #endif
3586 edge->flags |= TA_EDGE_DONE;
3588 if (edge > edges
3589 && (top_to_bottom_hinting ? (edge->pos > edge[-1].pos)
3590 : (edge->pos < edge[-1].pos)))
3592 /* don't move if stem would (almost) disappear otherwise; */
3593 /* the ad-hoc value 16 corresponds to 1/4px */
3594 if (edge->link
3595 && TA_ABS(edge->link->pos - edge[-1].pos) > 16)
3597 #ifdef TA_DEBUG
3598 TA_LOG((" BOUND: edge %d (pos=%.2f) moved to %.2f\n",
3599 edge - edges, edge->pos / 64.0, edge[-1].pos / 64.0));
3600 num_actions++;
3601 #endif
3603 edge->pos = edge[-1].pos;
3605 if (hints->recorder)
3606 hints->recorder(ta_bound, hints, dim,
3607 edge, &edge[-1], NULL, NULL, NULL);
3611 if (edge + 1 < edge_limit
3612 && edge[1].flags & TA_EDGE_DONE
3613 && (top_to_bottom_hinting ? (edge->pos < edge[1].pos)
3614 : (edge->pos > edge[1].pos)))
3617 /* don't move if stem would (almost) disappear otherwise; */
3618 /* the ad-hoc value 16 corresponds to 1/4px */
3619 if (edge->link
3620 && TA_ABS(edge->link->pos - edge[-1].pos) > 16)
3622 #ifdef TA_DEBUG
3623 TA_LOG((" BOUND: edge %d (pos=%.2f) moved to %.2f\n",
3624 edge - edges, edge->pos / 64.0, edge[1].pos / 64.0));
3626 num_actions++;
3627 #endif
3629 edge->pos = edge[1].pos;
3631 if (hints->recorder)
3632 hints->recorder(ta_bound, hints, dim,
3633 edge, &edge[1], NULL, NULL, NULL);
3639 #ifdef TA_DEBUG
3640 if (!num_actions)
3641 TA_LOG((" (none)\n"));
3642 TA_LOG(("\n"));
3643 #endif
3647 /* apply the complete hinting algorithm to a latin glyph */
3649 static FT_Error
3650 ta_latin_hints_apply(FT_UInt glyph_index,
3651 TA_GlyphHints hints,
3652 FT_Outline* outline,
3653 TA_LatinMetrics metrics)
3655 FT_Error error;
3656 int dim;
3658 TA_LatinAxis axis;
3661 error = ta_glyph_hints_reload(hints, outline);
3662 if (error)
3663 goto Exit;
3665 /* analyze glyph outline */
3666 if (TA_HINTS_DO_HORIZONTAL(hints))
3668 axis = &metrics->axis[TA_DIMENSION_HORZ];
3669 error = ta_latin_hints_detect_features(hints,
3670 axis->width_count,
3671 axis->widths,
3672 TA_DIMENSION_HORZ);
3673 if (error)
3674 goto Exit;
3677 if (TA_HINTS_DO_VERTICAL(hints))
3679 axis = &metrics->axis[TA_DIMENSION_VERT];
3680 error = ta_latin_hints_detect_features(hints,
3681 axis->width_count,
3682 axis->widths,
3683 TA_DIMENSION_VERT);
3684 if (error)
3685 goto Exit;
3687 /* apply blue zones to base characters only */
3688 if (!(metrics->root.globals->glyph_styles[glyph_index] & TA_NONBASE))
3689 ta_latin_hints_compute_blue_edges(hints, metrics);
3692 /* grid-fit the outline */
3693 for (dim = 0; dim < TA_DIMENSION_MAX; dim++)
3695 #ifdef TA_CONFIG_OPTION_USE_WARPER
3696 if (dim == TA_DIMENSION_HORZ
3697 && metrics->root.scaler.render_mode == FT_RENDER_MODE_NORMAL
3698 && TA_HINTS_DO_WARP(hints))
3700 TA_WarperRec warper;
3701 FT_Fixed scale;
3702 FT_Pos delta;
3705 ta_warper_compute(&warper, hints, (TA_Dimension)dim, &scale, &delta);
3706 ta_glyph_hints_scale_dim(hints, (TA_Dimension)dim, scale, delta);
3708 continue;
3710 #endif /* TA_CONFIG_OPTION_USE_WARPER */
3712 if ((dim == TA_DIMENSION_HORZ && TA_HINTS_DO_HORIZONTAL(hints))
3713 || (dim == TA_DIMENSION_VERT && TA_HINTS_DO_VERTICAL(hints)))
3715 ta_latin_hint_edges(hints, (TA_Dimension)dim);
3716 ta_glyph_hints_align_edge_points(hints, (TA_Dimension)dim);
3717 ta_glyph_hints_align_strong_points(hints, (TA_Dimension)dim);
3718 ta_glyph_hints_align_weak_points(hints, (TA_Dimension)dim);
3722 ta_glyph_hints_save(hints, outline);
3724 Exit:
3725 return error;
3729 const TA_WritingSystemClassRec ta_latin_writing_system_class =
3731 TA_WRITING_SYSTEM_LATIN,
3733 sizeof (TA_LatinMetricsRec),
3735 (TA_WritingSystem_InitMetricsFunc)ta_latin_metrics_init, /* style_metrics_init */
3736 (TA_WritingSystem_ScaleMetricsFunc)ta_latin_metrics_scale, /* style_metrics_scale */
3737 (TA_WritingSystem_DoneMetricsFunc)NULL, /* style_metrics_done */
3739 (TA_WritingSystem_InitHintsFunc)ta_latin_hints_init, /* style_hints_init */
3740 (TA_WritingSystem_ApplyHintsFunc)ta_latin_hints_apply /* style_hints_apply */
3743 /* end of talatin.c */