Move functions related to TTC creation into separate file.
[ttfautohint.git] / src / talatin.c
blob804205bbd132b91f8295dfb7bf0a49ba13d131a9
1 /* talatin.c */
3 /*
4 * Copyright (C) 2011 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
25 #include "talatin.h"
26 #include "tasort.h"
29 #ifdef TA_CONFIG_OPTION_USE_WARPER
30 #include "afwarp.h"
31 #endif
34 /* find segments and links, compute all stem widths, and initialize */
35 /* standard width and height for the glyph with given charcode */
37 void
38 ta_latin_metrics_init_widths(TA_LatinMetrics metrics,
39 FT_Face face,
40 FT_ULong charcode)
42 /* scan the array of segments in each direction */
43 TA_GlyphHintsRec hints[1];
46 ta_glyph_hints_init(hints);
48 metrics->axis[TA_DIMENSION_HORZ].width_count = 0;
49 metrics->axis[TA_DIMENSION_VERT].width_count = 0;
52 FT_Error error;
53 FT_UInt glyph_index;
54 int dim;
55 TA_LatinMetricsRec dummy[1];
56 TA_Scaler scaler = &dummy->root.scaler;
59 glyph_index = FT_Get_Char_Index(face, charcode);
60 if (glyph_index == 0)
61 goto Exit;
63 error = FT_Load_Glyph(face, glyph_index, FT_LOAD_NO_SCALE);
64 if (error || face->glyph->outline.n_points <= 0)
65 goto Exit;
67 memset(dummy, 0, sizeof (TA_LatinMetricsRec));
69 dummy->units_per_em = metrics->units_per_em;
71 scaler->x_scale = 0x10000L;
72 scaler->y_scale = 0x10000L;
73 scaler->x_delta = 0;
74 scaler->y_delta = 0;
76 scaler->face = face;
77 scaler->render_mode = FT_RENDER_MODE_NORMAL;
78 scaler->flags = 0;
80 ta_glyph_hints_rescale(hints, (TA_ScriptMetrics)dummy);
82 error = ta_glyph_hints_reload(hints, &face->glyph->outline);
83 if (error)
84 goto Exit;
86 for (dim = 0; dim < TA_DIMENSION_MAX; dim++)
88 TA_LatinAxis axis = &metrics->axis[dim];
89 TA_AxisHints axhints = &hints->axis[dim];
91 TA_Segment seg, limit, link;
92 FT_UInt num_widths = 0;
95 error = ta_latin_hints_compute_segments(hints, (TA_Dimension)dim);
96 if (error)
97 goto Exit;
99 ta_latin_hints_link_segments(hints, (TA_Dimension)dim);
101 seg = axhints->segments;
102 limit = seg + axhints->num_segments;
104 for (; seg < limit; seg++)
106 link = seg->link;
108 /* we only consider stem segments there! */
109 if (link
110 && link->link == seg
111 && link > seg)
113 FT_Pos dist;
116 dist = seg->pos - link->pos;
117 if (dist < 0)
118 dist = -dist;
120 if (num_widths < TA_LATIN_MAX_WIDTHS)
121 axis->widths[num_widths++].org = dist;
125 ta_sort_widths(num_widths, axis->widths);
126 axis->width_count = num_widths;
129 Exit:
130 for (dim = 0; dim < TA_DIMENSION_MAX; dim++)
132 TA_LatinAxis axis = &metrics->axis[dim];
133 FT_Pos stdw;
136 stdw = (axis->width_count > 0) ? axis->widths[0].org
137 : TA_LATIN_CONSTANT(metrics, 50);
139 /* let's try 20% of the smallest width */
140 axis->edge_distance_threshold = stdw / 5;
141 axis->standard_width = stdw;
142 axis->extra_light = 0;
146 ta_glyph_hints_done(hints);
150 #define TA_LATIN_MAX_TEST_CHARACTERS 12
153 static const char ta_latin_blue_chars[TA_LATIN_MAX_BLUES]
154 [TA_LATIN_MAX_TEST_CHARACTERS + 1] =
156 "THEZOCQS",
157 "HEZLOCUS",
158 "fijkdbh",
159 "xzroesc",
160 "xzroesc",
161 "pqgjy"
165 /* find all blue zones; flat segments give the reference points, */
166 /* round segments the overshoot positions */
168 static void
169 ta_latin_metrics_init_blues(TA_LatinMetrics metrics,
170 FT_Face face)
172 FT_Pos flats[TA_LATIN_MAX_TEST_CHARACTERS];
173 FT_Pos rounds[TA_LATIN_MAX_TEST_CHARACTERS];
174 FT_Int num_flats;
175 FT_Int num_rounds;
177 FT_Int bb;
178 TA_LatinBlue blue;
179 FT_Error error;
180 TA_LatinAxis axis = &metrics->axis[TA_DIMENSION_VERT];
181 FT_GlyphSlot glyph = face->glyph;
184 /* we compute the blues simply by loading each character from the */
185 /* `ta_latin_blue_chars[blues]' string, then finding its top-most or */
186 /* bottom-most points (depending on `TA_IS_TOP_BLUE') */
188 TA_LOG(("blue zones computation\n"));
189 TA_LOG(("------------------------------------------------\n"));
191 for (bb = 0; bb < TA_LATIN_BLUE_MAX; bb++)
193 const char* p = ta_latin_blue_chars[bb];
194 const char* limit = p + TA_LATIN_MAX_TEST_CHARACTERS;
195 FT_Pos* blue_ref;
196 FT_Pos* blue_shoot;
199 TA_LOG(("blue %3d: ", bb));
201 num_flats = 0;
202 num_rounds = 0;
204 for (; p < limit && *p; p++)
206 FT_UInt glyph_index;
207 FT_Pos best_y; /* same as points.y */
208 FT_Int best_point, best_first, best_last;
209 FT_Vector* points;
210 FT_Bool round = 0;
213 TA_LOG(("'%c'", *p));
215 /* load the character in the face -- skip unknown or empty ones */
216 glyph_index = FT_Get_Char_Index(face, (FT_UInt)*p);
217 if (glyph_index == 0)
218 continue;
220 error = FT_Load_Glyph(face, glyph_index, FT_LOAD_NO_SCALE);
221 if (error || glyph->outline.n_points <= 0)
222 continue;
224 /* now compute min or max point indices and coordinates */
225 points = glyph->outline.points;
226 best_point = -1;
227 best_y = 0; /* make compiler happy */
228 best_first = 0; /* ditto */
229 best_last = 0; /* ditto */
232 FT_Int nn;
233 FT_Int first = 0;
234 FT_Int last = -1;
237 for (nn = 0; nn < glyph->outline.n_contours; first = last + 1, nn++)
239 FT_Int old_best_point = best_point;
240 FT_Int pp;
243 last = glyph->outline.contours[nn];
245 /* avoid single-point contours since they are never rasterized; */
246 /* in some fonts, they correspond to mark attachment points */
247 /* which are way outside of the glyph's real outline */
248 if (last <= first)
249 continue;
251 if (TA_LATIN_IS_TOP_BLUE(bb))
253 for (pp = first; pp <= last; pp++)
254 if (best_point < 0
255 || points[pp].y > best_y)
257 best_point = pp;
258 best_y = points[pp].y;
261 else
263 for (pp = first; pp <= last; pp++)
264 if (best_point < 0
265 || points[pp].y < best_y)
267 best_point = pp;
268 best_y = points[pp].y;
272 if (best_point != old_best_point)
274 best_first = first;
275 best_last = last;
278 TA_LOG(("%5ld", best_y));
281 /* now check whether the point belongs to a straight or round */
282 /* segment; we first need to find in which contour the extremum */
283 /* lies, then inspect its previous and next points */
284 if (best_point >= 0)
286 FT_Int prev, next;
287 FT_Pos dist;
290 /* now look for the previous and next points that are not on the */
291 /* same Y coordinate and threshold the `closeness'... */
292 prev = best_point;
293 next = prev;
297 if (prev > best_first)
298 prev--;
299 else
300 prev = best_last;
302 dist = points[prev].y - best_y;
303 if (dist < -5 || dist > 5)
304 break;
305 } while (prev != best_point);
309 if (next < best_last)
310 next++;
311 else
312 next = best_first;
314 dist = points[next].y - best_y;
315 if (dist < -5 || dist > 5)
316 break;
317 } while (next != best_point);
319 /* now set the `round' flag depending on the segment's kind */
320 round = FT_BOOL(
321 FT_CURVE_TAG(glyph->outline.tags[prev]) != FT_CURVE_TAG_ON
322 || FT_CURVE_TAG(glyph->outline.tags[next]) != FT_CURVE_TAG_ON);
324 TA_LOG(("%c ", round ? 'r' : 'f'));
327 if (round)
328 rounds[num_rounds++] = best_y;
329 else
330 flats[num_flats++] = best_y;
333 TA_LOG(("\n"));
335 if (num_flats == 0 && num_rounds == 0)
337 /* we couldn't find a single glyph to compute this blue zone, */
338 /* we will simply ignore it then */
339 TA_LOG(("empty\n"));
340 continue;
343 /* we have computed the contents of the `rounds' and `flats' tables, */
344 /* now determine the reference and overshoot position of the blue -- */
345 /* we simply take the median value after a simple sort */
346 ta_sort_pos(num_rounds, rounds);
347 ta_sort_pos(num_flats, flats);
349 blue = &axis->blues[axis->blue_count];
350 blue_ref = &blue->ref.org;
351 blue_shoot = &blue->shoot.org;
353 axis->blue_count++;
355 if (num_flats == 0)
357 *blue_ref =
358 *blue_shoot = rounds[num_rounds / 2];
360 else if (num_rounds == 0)
362 *blue_ref =
363 *blue_shoot = flats[num_flats / 2];
365 else
367 *blue_ref = flats[num_flats / 2];
368 *blue_shoot = rounds[num_rounds / 2];
371 /* there are sometimes problems if the overshoot position of top */
372 /* zones is under its reference position, or the opposite for bottom */
373 /* zones; we must thus check everything there and correct the errors */
374 if (*blue_shoot != *blue_ref)
376 FT_Pos ref = *blue_ref;
377 FT_Pos shoot = *blue_shoot;
378 FT_Bool over_ref = FT_BOOL(shoot > ref);
381 if (TA_LATIN_IS_TOP_BLUE(bb) ^ over_ref)
382 *blue_ref =
383 *blue_shoot = (shoot + ref) / 2;
386 blue->flags = 0;
387 if (TA_LATIN_IS_TOP_BLUE(bb))
388 blue->flags |= TA_LATIN_BLUE_TOP;
390 /* the following flag is used later to adjust the y and x scales */
391 /* in order to optimize the pixel grid alignment */
392 /* of the top of small letters */
393 if (bb == TA_LATIN_BLUE_SMALL_TOP)
394 blue->flags |= TA_LATIN_BLUE_ADJUSTMENT;
396 TA_LOG(("-- ref = %ld, shoot = %ld\n", *blue_ref, *blue_shoot));
399 TA_LOG(("\n"));
401 return;
405 /* check whether all ASCII digits have the same advance width */
407 void
408 ta_latin_metrics_check_digits(TA_LatinMetrics metrics,
409 FT_Face face)
411 FT_UInt i;
412 FT_Bool started = 0, same_width = 1;
413 FT_Fixed advance, old_advance = 0;
416 /* digit `0' is 0x30 in all supported charmaps */
417 for (i = 0x30; i <= 0x39; i++)
419 FT_UInt glyph_index;
422 glyph_index = FT_Get_Char_Index(face, i);
423 if (glyph_index == 0)
424 continue;
426 if (FT_Get_Advance(face, glyph_index,
427 FT_LOAD_NO_SCALE
428 | FT_LOAD_NO_HINTING
429 | FT_LOAD_IGNORE_TRANSFORM,
430 &advance))
431 continue;
433 if (started)
435 if (advance != old_advance)
437 same_width = 0;
438 break;
441 else
443 old_advance = advance;
444 started = 1;
448 metrics->root.digits_have_same_width = same_width;
452 /* initialize global metrics */
454 FT_Error
455 ta_latin_metrics_init(TA_LatinMetrics metrics,
456 FT_Face face)
458 FT_Error error = FT_Err_Ok;
459 FT_CharMap oldmap = face->charmap;
460 FT_UInt ee;
462 static const FT_Encoding latin_encodings[] =
464 FT_ENCODING_UNICODE,
465 FT_ENCODING_APPLE_ROMAN,
466 FT_ENCODING_ADOBE_STANDARD,
467 FT_ENCODING_ADOBE_LATIN_1,
469 FT_ENCODING_NONE /* end of list */
473 metrics->units_per_em = face->units_per_EM;
475 /* do we have a latin charmap in there? */
476 for (ee = 0; latin_encodings[ee] != FT_ENCODING_NONE; ee++)
478 error = FT_Select_Charmap(face, latin_encodings[ee]);
479 if (!error)
480 break;
483 if (!error)
485 /* for now, compute the standard width and height from the `o' */
486 ta_latin_metrics_init_widths(metrics, face, 'o');
487 ta_latin_metrics_init_blues(metrics, face);
488 ta_latin_metrics_check_digits(metrics, face);
491 FT_Set_Charmap(face, oldmap);
492 return FT_Err_Ok;
496 /* adjust scaling value, then scale and shift widths */
497 /* and blue zones (if applicable) for given dimension */
499 static void
500 ta_latin_metrics_scale_dim(TA_LatinMetrics metrics,
501 TA_Scaler scaler,
502 TA_Dimension dim)
504 FT_Fixed scale;
505 FT_Pos delta;
506 TA_LatinAxis axis;
507 FT_UInt nn;
510 if (dim == TA_DIMENSION_HORZ)
512 scale = scaler->x_scale;
513 delta = scaler->x_delta;
515 else
517 scale = scaler->y_scale;
518 delta = scaler->y_delta;
521 axis = &metrics->axis[dim];
523 if (axis->org_scale == scale && axis->org_delta == delta)
524 return;
526 axis->org_scale = scale;
527 axis->org_delta = delta;
529 /* correct X and Y scale to optimize the alignment of the top of */
530 /* small letters to the pixel grid */
532 TA_LatinAxis Axis = &metrics->axis[TA_DIMENSION_VERT];
533 TA_LatinBlue blue = NULL;
536 for (nn = 0; nn < Axis->blue_count; nn++)
538 if (Axis->blues[nn].flags & TA_LATIN_BLUE_ADJUSTMENT)
540 blue = &Axis->blues[nn];
541 break;
545 if (blue)
547 FT_Pos scaled = FT_MulFix(blue->shoot.org, scaler->y_scale);
548 FT_Pos fitted = (scaled + 40) & ~63;
551 if (scaled != fitted)
553 if (dim == TA_DIMENSION_VERT)
554 scale = FT_MulDiv(scale, fitted, scaled);
559 axis->scale = scale;
560 axis->delta = delta;
562 if (dim == TA_DIMENSION_HORZ)
564 metrics->root.scaler.x_scale = scale;
565 metrics->root.scaler.x_delta = delta;
567 else
569 metrics->root.scaler.y_scale = scale;
570 metrics->root.scaler.y_delta = delta;
573 /* scale the widths */
574 for (nn = 0; nn < axis->width_count; nn++)
576 TA_Width width = axis->widths + nn;
579 width->cur = FT_MulFix(width->org, scale);
580 width->fit = width->cur;
583 /* an extra-light axis corresponds to a standard width that is */
584 /* smaller than 5/8 pixels */
585 axis->extra_light =
586 (FT_Bool)(FT_MulFix(axis->standard_width, scale) < 32 + 8);
588 if (dim == TA_DIMENSION_VERT)
590 /* scale the blue zones */
591 for (nn = 0; nn < axis->blue_count; nn++)
593 TA_LatinBlue blue = &axis->blues[nn];
594 FT_Pos dist;
597 blue->ref.cur = FT_MulFix(blue->ref.org, scale) + delta;
598 blue->ref.fit = blue->ref.cur;
599 blue->shoot.cur = FT_MulFix(blue->shoot.org, scale) + delta;
600 blue->shoot.fit = blue->shoot.cur;
601 blue->flags &= ~TA_LATIN_BLUE_ACTIVE;
603 /* a blue zone is only active if it is less than 3/4 pixels tall */
604 dist = FT_MulFix(blue->ref.org - blue->shoot.org, scale);
605 if (dist <= 48 && dist >= -48)
607 #if 0
608 FT_Pos delta1;
609 #endif
610 FT_Pos delta2;
613 /* use discrete values for blue zone widths */
615 #if 0
616 /* generic, original code */
617 delta1 = blue->shoot.org - blue->ref.org;
618 delta2 = delta1;
619 if (delta1 < 0)
620 delta2 = -delta2;
622 delta2 = FT_MulFix(delta2, scale);
624 if (delta2 < 32)
625 delta2 = 0;
626 else if (delta2 < 64)
627 delta2 = 32 + (((delta2 - 32) + 16) & ~31);
628 else
629 delta2 = TA_PIX_ROUND(delta2);
631 if (delta1 < 0)
632 delta2 = -delta2;
634 blue->ref.fit = TA_PIX_ROUND(blue->ref.cur);
635 blue->shoot.fit = blue->ref.fit + delta2;
636 #else
637 /* simplified version due to abs(dist) <= 48 */
638 delta2 = dist;
639 if (dist < 0)
640 delta2 = -delta2;
642 if (delta2 < 32)
643 delta2 = 0;
644 else if (delta < 48)
645 delta2 = 32;
646 else
647 delta2 = 64;
649 if (dist < 0)
650 delta2 = -delta2;
652 blue->ref.fit = TA_PIX_ROUND(blue->ref.cur);
653 blue->shoot.fit = blue->ref.fit - delta2;
654 #endif
656 blue->flags |= TA_LATIN_BLUE_ACTIVE;
663 /* scale global values in both directions */
665 void
666 ta_latin_metrics_scale(TA_LatinMetrics metrics,
667 TA_Scaler scaler)
669 metrics->root.scaler.render_mode = scaler->render_mode;
670 metrics->root.scaler.face = scaler->face;
672 ta_latin_metrics_scale_dim(metrics, scaler, TA_DIMENSION_HORZ);
673 ta_latin_metrics_scale_dim(metrics, scaler, TA_DIMENSION_VERT);
677 /* walk over all contours and compute its segments */
679 FT_Error
680 ta_latin_hints_compute_segments(TA_GlyphHints hints,
681 TA_Dimension dim)
683 TA_AxisHints axis = &hints->axis[dim];
684 FT_Error error = FT_Err_Ok;
686 TA_Segment segment = NULL;
687 TA_SegmentRec seg0;
689 TA_Point* contour = hints->contours;
690 TA_Point* contour_limit = contour + hints->num_contours;
691 TA_Direction major_dir, segment_dir;
694 memset(&seg0, 0, sizeof (TA_SegmentRec));
695 seg0.score = 32000;
696 seg0.flags = TA_EDGE_NORMAL;
698 major_dir = (TA_Direction)TA_ABS(axis->major_dir);
699 segment_dir = major_dir;
701 axis->num_segments = 0;
703 /* set up (u,v) in each point */
704 if (dim == TA_DIMENSION_HORZ)
706 TA_Point point = hints->points;
707 TA_Point limit = point + hints->num_points;
710 for (; point < limit; point++)
712 point->u = point->fx;
713 point->v = point->fy;
716 else
718 TA_Point point = hints->points;
719 TA_Point limit = point + hints->num_points;
722 for (; point < limit; point++)
724 point->u = point->fy;
725 point->v = point->fx;
729 /* do each contour separately */
730 for (; contour < contour_limit; contour++)
732 TA_Point point = contour[0];
733 TA_Point last = point->prev;
735 int on_edge = 0;
737 FT_Pos min_pos = 32000; /* minimum segment pos != min_coord */
738 FT_Pos max_pos = -32000; /* maximum segment pos != max_coord */
739 FT_Bool passed;
742 if (point == last) /* skip singletons -- just in case */
743 continue;
745 if (TA_ABS(last->out_dir) == major_dir
746 && TA_ABS(point->out_dir) == major_dir)
748 /* we are already on an edge, try to locate its start */
749 last = point;
751 for (;;)
753 point = point->prev;
754 if (TA_ABS(point->out_dir) != major_dir)
756 point = point->next;
757 break;
759 if (point == last)
760 break;
764 last = point;
765 passed = 0;
767 for (;;)
769 FT_Pos u, v;
772 if (on_edge)
774 u = point->u;
775 if (u < min_pos)
776 min_pos = u;
777 if (u > max_pos)
778 max_pos = u;
780 if (point->out_dir != segment_dir
781 || point == last)
783 /* we are just leaving an edge; record a new segment! */
784 segment->last = point;
785 segment->pos = (FT_Short)((min_pos + max_pos) >> 1);
787 /* a segment is round if either its first or last point */
788 /* is a control point */
789 if ((segment->first->flags | point->flags) & TA_FLAG_CONTROL)
790 segment->flags |= TA_EDGE_ROUND;
792 /* compute segment size */
793 min_pos = max_pos = point->v;
795 v = segment->first->v;
796 if (v < min_pos)
797 min_pos = v;
798 if (v > max_pos)
799 max_pos = v;
801 segment->min_coord = (FT_Short)min_pos;
802 segment->max_coord = (FT_Short)max_pos;
803 segment->height = (FT_Short)(segment->max_coord -
804 segment->min_coord);
806 on_edge = 0;
807 segment = NULL;
808 /* fall through */
812 /* now exit if we are at the start/end point */
813 if (point == last)
815 if (passed)
816 break;
817 passed = 1;
820 if (!on_edge
821 && TA_ABS(point->out_dir) == major_dir)
823 /* this is the start of a new segment! */
824 segment_dir = (TA_Direction)point->out_dir;
826 /* clear all segment fields */
827 error = ta_axis_hints_new_segment(axis, &segment);
828 if (error)
829 goto Exit;
831 segment[0] = seg0;
832 segment->dir = (FT_Char)segment_dir;
833 min_pos = max_pos = point->u;
834 segment->first = point;
835 segment->last = point;
836 on_edge = 1;
839 point = point->next;
841 } /* contours */
844 /* now slightly increase the height of segments if this makes sense -- */
845 /* this is used to better detect and ignore serifs */
847 TA_Segment segments = axis->segments;
848 TA_Segment segments_end = segments + axis->num_segments;
851 for (segment = segments; segment < segments_end; segment++)
853 TA_Point first = segment->first;
854 TA_Point last = segment->last;
856 FT_Pos first_v = first->v;
857 FT_Pos last_v = last->v;
860 if (first == last)
861 continue;
863 if (first_v < last_v)
865 TA_Point p;
868 p = first->prev;
869 if (p->v < first_v)
870 segment->height = (FT_Short)(segment->height +
871 ((first_v - p->v) >> 1));
873 p = last->next;
874 if (p->v > last_v)
875 segment->height = (FT_Short)(segment->height +
876 ((p->v - last_v) >> 1));
878 else
880 TA_Point p;
883 p = first->prev;
884 if (p->v > first_v)
885 segment->height = (FT_Short)(segment->height +
886 ((p->v - first_v) >> 1));
888 p = last->next;
889 if (p->v < last_v)
890 segment->height = (FT_Short)(segment->height +
891 ((last_v - p->v) >> 1));
896 Exit:
897 return error;
901 /* link segments to form stems and serifs */
903 void
904 ta_latin_hints_link_segments(TA_GlyphHints hints,
905 TA_Dimension dim)
907 TA_AxisHints axis = &hints->axis[dim];
909 TA_Segment segments = axis->segments;
910 TA_Segment segment_limit = segments + axis->num_segments;
912 FT_Pos len_threshold, len_score;
913 TA_Segment seg1, seg2;
916 len_threshold = TA_LATIN_CONSTANT(hints->metrics, 8);
917 if (len_threshold == 0)
918 len_threshold = 1;
920 len_score = TA_LATIN_CONSTANT(hints->metrics, 6000);
922 /* now compare each segment to the others */
923 for (seg1 = segments; seg1 < segment_limit; seg1++)
925 /* the fake segments are introduced to hint the metrics -- */
926 /* we must never link them to anything */
927 if (seg1->dir != axis->major_dir
928 || seg1->first == seg1->last)
929 continue;
931 /* search for stems having opposite directions, */
932 /* with seg1 to the `left' of seg2 */
933 for (seg2 = segments; seg2 < segment_limit; seg2++)
935 FT_Pos pos1 = seg1->pos;
936 FT_Pos pos2 = seg2->pos;
939 if (seg1->dir + seg2->dir == 0
940 && pos2 > pos1)
942 /* compute distance between the two segments */
943 FT_Pos dist = pos2 - pos1;
944 FT_Pos min = seg1->min_coord;
945 FT_Pos max = seg1->max_coord;
946 FT_Pos len, score;
949 if (min < seg2->min_coord)
950 min = seg2->min_coord;
951 if (max > seg2->max_coord)
952 max = seg2->max_coord;
954 /* compute maximum coordinate difference of the two segments */
955 len = max - min;
956 if (len >= len_threshold)
958 /* small coordinate differences cause a higher score, and */
959 /* segments with a greater distance cause a higher score also */
960 score = dist + len_score / len;
962 /* and we search for the smallest score */
963 /* of the sum of the two values */
964 if (score < seg1->score)
966 seg1->score = score;
967 seg1->link = seg2;
970 if (score < seg2->score)
972 seg2->score = score;
973 seg2->link = seg1;
980 /* now compute the `serif' segments, cf. explanations in `tahints.h' */
981 for (seg1 = segments; seg1 < segment_limit; seg1++)
983 seg2 = seg1->link;
985 if (seg2)
987 if (seg2->link != seg1)
989 seg1->link = 0;
990 seg1->serif = seg2->link;
997 /* link segments to edges, using feature analysis for selection */
999 FT_Error
1000 ta_latin_hints_compute_edges(TA_GlyphHints hints,
1001 TA_Dimension dim)
1003 TA_AxisHints axis = &hints->axis[dim];
1004 FT_Error error = FT_Err_Ok;
1005 TA_LatinAxis laxis = &((TA_LatinMetrics)hints->metrics)->axis[dim];
1007 TA_Segment segments = axis->segments;
1008 TA_Segment segment_limit = segments + axis->num_segments;
1009 TA_Segment seg;
1011 #if 0
1012 TA_Direction up_dir;
1013 #endif
1014 FT_Fixed scale;
1015 FT_Pos edge_distance_threshold;
1016 FT_Pos segment_length_threshold;
1019 axis->num_edges = 0;
1021 scale = (dim == TA_DIMENSION_HORZ) ? hints->x_scale
1022 : hints->y_scale;
1024 #if 0
1025 up_dir = (dim == TA_DIMENSION_HORZ) ? TA_DIR_UP
1026 : TA_DIR_RIGHT;
1027 #endif
1029 /* we ignore all segments that are less than 1 pixel in length */
1030 /* to avoid many problems with serif fonts */
1031 /* (the corresponding threshold is computed in font units) */
1032 if (dim == TA_DIMENSION_HORZ)
1033 segment_length_threshold = FT_DivFix(64, hints->y_scale);
1034 else
1035 segment_length_threshold = 0;
1037 /********************************************************************/
1038 /* */
1039 /* We begin by generating a sorted table of edges for the current */
1040 /* direction. To do so, we simply scan each segment and try to find */
1041 /* an edge in our table that corresponds to its position. */
1042 /* */
1043 /* If no edge is found, we create and insert a new edge in the */
1044 /* sorted table. Otherwise, we simply add the segment to the edge's */
1045 /* list which gets processed in the second step to compute the */
1046 /* edge's properties. */
1047 /* */
1048 /* Note that the table of edges is sorted along the segment/edge */
1049 /* position. */
1050 /* */
1051 /********************************************************************/
1053 /* assure that edge distance threshold is at most 0.25px */
1054 edge_distance_threshold = FT_MulFix(laxis->edge_distance_threshold,
1055 scale);
1056 if (edge_distance_threshold > 64 / 4)
1057 edge_distance_threshold = 64 / 4;
1059 edge_distance_threshold = FT_DivFix(edge_distance_threshold,
1060 scale);
1062 for (seg = segments; seg < segment_limit; seg++)
1064 TA_Edge found = NULL;
1065 FT_Int ee;
1068 if (seg->height < segment_length_threshold)
1069 continue;
1071 /* a special case for serif edges: */
1072 /* if they are smaller than 1.5 pixels we ignore them */
1073 if (seg->serif
1074 && 2 * seg->height < 3 * segment_length_threshold)
1075 continue;
1077 /* look for an edge corresponding to the segment */
1078 for (ee = 0; ee < axis->num_edges; ee++)
1080 TA_Edge edge = axis->edges + ee;
1081 FT_Pos dist;
1084 dist = seg->pos - edge->fpos;
1085 if (dist < 0)
1086 dist = -dist;
1088 if (dist < edge_distance_threshold && edge->dir == seg->dir)
1090 found = edge;
1091 break;
1095 if (!found)
1097 TA_Edge edge;
1100 /* insert a new edge in the list and sort according to the position */
1101 error = ta_axis_hints_new_edge(axis, seg->pos,
1102 (TA_Direction)seg->dir,
1103 &edge);
1104 if (error)
1105 goto Exit;
1107 /* add the segment to the new edge's list */
1108 memset(edge, 0, sizeof (TA_EdgeRec));
1109 edge->first = seg;
1110 edge->last = seg;
1111 edge->dir = seg->dir;
1112 edge->fpos = seg->pos;
1113 edge->opos = FT_MulFix(seg->pos, scale);
1114 edge->pos = edge->opos;
1115 seg->edge_next = seg;
1117 else
1119 /* if an edge was found, simply add the segment to the edge's list */
1120 seg->edge_next = found->first;
1121 found->last->edge_next = seg;
1122 found->last = seg;
1126 /*****************************************************************/
1127 /* */
1128 /* Good, we now compute each edge's properties according to */
1129 /* the segments found on its position. Basically, these are */
1130 /* */
1131 /* - the edge's main direction */
1132 /* - stem edge, serif edge or both (which defaults to stem then) */
1133 /* - rounded edge, straight or both (which defaults to straight) */
1134 /* - link for edge */
1135 /* */
1136 /*****************************************************************/
1138 /* first of all, set the `edge' field in each segment -- this is */
1139 /* required in order to compute edge links */
1141 /* note that removing this loop and setting the `edge' field of each */
1142 /* segment directly in the code above slows down execution speed for */
1143 /* some reasons on platforms like the Sun */
1145 TA_Edge edges = axis->edges;
1146 TA_Edge edge_limit = edges + axis->num_edges;
1147 TA_Edge edge;
1150 for (edge = edges; edge < edge_limit; edge++)
1152 seg = edge->first;
1153 if (seg)
1156 seg->edge = edge;
1157 seg = seg->edge_next;
1158 } while (seg != edge->first);
1161 /* now compute each edge properties */
1162 for (edge = edges; edge < edge_limit; edge++)
1164 FT_Int is_round = 0; /* does it contain round segments? */
1165 FT_Int is_straight = 0; /* does it contain straight segments? */
1166 #if 0
1167 FT_Pos ups = 0; /* number of upwards segments */
1168 FT_Pos downs = 0; /* number of downwards segments */
1169 #endif
1172 seg = edge->first;
1176 FT_Bool is_serif;
1179 /* check for roundness of segment */
1180 if (seg->flags & TA_EDGE_ROUND)
1181 is_round++;
1182 else
1183 is_straight++;
1185 #if 0
1186 /* check for segment direction */
1187 if (seg->dir == up_dir)
1188 ups += seg->max_coord - seg->min_coord;
1189 else
1190 downs += seg->max_coord - seg->min_coord;
1191 #endif
1193 /* check for links -- */
1194 /* if seg->serif is set, then seg->link must be ignored */
1195 is_serif = (FT_Bool)(seg->serif
1196 && seg->serif->edge
1197 && seg->serif->edge != edge);
1199 if ((seg->link && seg->link->edge != NULL)
1200 || is_serif)
1202 TA_Edge edge2;
1203 TA_Segment seg2;
1206 edge2 = edge->link;
1207 seg2 = seg->link;
1209 if (is_serif)
1211 seg2 = seg->serif;
1212 edge2 = edge->serif;
1215 if (edge2)
1217 FT_Pos edge_delta;
1218 FT_Pos seg_delta;
1221 edge_delta = edge->fpos - edge2->fpos;
1222 if (edge_delta < 0)
1223 edge_delta = -edge_delta;
1225 seg_delta = seg->pos - seg2->pos;
1226 if (seg_delta < 0)
1227 seg_delta = -seg_delta;
1229 if (seg_delta < edge_delta)
1230 edge2 = seg2->edge;
1232 else
1233 edge2 = seg2->edge;
1235 if (is_serif)
1237 edge->serif = edge2;
1238 edge2->flags |= TA_EDGE_SERIF;
1240 else
1241 edge->link = edge2;
1244 seg = seg->edge_next;
1245 } while (seg != edge->first);
1247 /* set the round/straight flags */
1248 edge->flags = TA_EDGE_NORMAL;
1250 if (is_round > 0
1251 && is_round >= is_straight)
1252 edge->flags |= TA_EDGE_ROUND;
1254 #if 0
1255 /* set the edge's main direction */
1256 edge->dir = TA_DIR_NONE;
1258 if (ups > downs)
1259 edge->dir = (FT_Char)up_dir;
1261 else if (ups < downs)
1262 edge->dir = (FT_Char)-up_dir;
1264 else if (ups == downs)
1265 edge->dir = 0; /* both up and down! */
1266 #endif
1268 /* get rid of serifs if link is set */
1269 /* XXX: this gets rid of many unpleasant artefacts! */
1270 /* example: the `c' in cour.pfa at size 13 */
1272 if (edge->serif && edge->link)
1273 edge->serif = 0;
1277 Exit:
1278 return error;
1282 /* detect segments and edges for given dimension */
1284 FT_Error
1285 ta_latin_hints_detect_features(TA_GlyphHints hints,
1286 TA_Dimension dim)
1288 FT_Error error;
1291 error = ta_latin_hints_compute_segments(hints, dim);
1292 if (!error)
1294 ta_latin_hints_link_segments(hints, dim);
1296 error = ta_latin_hints_compute_edges(hints, dim);
1299 return error;
1303 /* compute all edges which lie within blue zones */
1305 void
1306 ta_latin_hints_compute_blue_edges(TA_GlyphHints hints,
1307 TA_LatinMetrics metrics)
1309 TA_AxisHints axis = &hints->axis[TA_DIMENSION_VERT];
1311 TA_Edge edge = axis->edges;
1312 TA_Edge edge_limit = edge + axis->num_edges;
1314 TA_LatinAxis latin = &metrics->axis[TA_DIMENSION_VERT];
1315 FT_Fixed scale = latin->scale;
1318 /* compute which blue zones are active, */
1319 /* i.e. have their scaled size < 3/4 pixels */
1321 /* for each horizontal edge search the blue zone which is closest */
1322 for (; edge < edge_limit; edge++)
1324 FT_Int bb;
1325 TA_Width best_blue = NULL;
1326 FT_Pos best_dist; /* initial threshold */
1328 FT_UInt best_blue_idx = 0;
1329 FT_Bool best_blue_is_shoot = 0;
1332 /* compute the initial threshold as a fraction of the EM size */
1333 /* (the value 40 is heuristic) */
1334 best_dist = FT_MulFix(metrics->units_per_em / 40, scale);
1336 /* assure a minimum distance of 0.5px */
1337 if (best_dist > 64 / 2)
1338 best_dist = 64 / 2;
1340 for (bb = 0; bb < TA_LATIN_BLUE_MAX; bb++)
1342 TA_LatinBlue blue = latin->blues + bb;
1343 FT_Bool is_top_blue, is_major_dir;
1346 /* skip inactive blue zones (i.e., those that are too large) */
1347 if (!(blue->flags & TA_LATIN_BLUE_ACTIVE))
1348 continue;
1350 /* if it is a top zone, check for right edges -- */
1351 /* if it is a bottom zone, check for left edges */
1352 is_top_blue = (FT_Byte)((blue->flags & TA_LATIN_BLUE_TOP) != 0);
1353 is_major_dir = FT_BOOL(edge->dir == axis->major_dir);
1355 /* if it is a top zone, the edge must be against the major */
1356 /* direction; if it is a bottom zone, it must be in the major */
1357 /* direction */
1358 if (is_top_blue ^ is_major_dir)
1360 FT_Pos dist;
1363 /* first of all, compare it to the reference position */
1364 dist = edge->fpos - blue->ref.org;
1365 if (dist < 0)
1366 dist = -dist;
1368 dist = FT_MulFix(dist, scale);
1369 if (dist < best_dist)
1371 best_dist = dist;
1372 best_blue = &blue->ref;
1374 best_blue_idx = bb;
1375 best_blue_is_shoot = 0;
1378 /* now compare it to the overshoot position and check whether */
1379 /* the edge is rounded, and whether the edge is over the */
1380 /* reference position of a top zone, or under the reference */
1381 /* position of a bottom zone */
1382 if (edge->flags & TA_EDGE_ROUND
1383 && dist != 0)
1385 FT_Bool is_under_ref = FT_BOOL(edge->fpos < blue->ref.org);
1388 if (is_top_blue ^ is_under_ref)
1390 dist = edge->fpos - blue->shoot.org;
1391 if (dist < 0)
1392 dist = -dist;
1394 dist = FT_MulFix(dist, scale);
1395 if (dist < best_dist)
1397 best_dist = dist;
1398 best_blue = &blue->shoot;
1400 best_blue_idx = bb;
1401 best_blue_is_shoot = 1;
1408 if (best_blue)
1410 edge->blue_edge = best_blue;
1411 edge->best_blue_idx = best_blue_idx;
1412 edge->best_blue_is_shoot = best_blue_is_shoot;
1418 /* initalize hinting engine */
1420 static FT_Error
1421 ta_latin_hints_init(TA_GlyphHints hints,
1422 TA_LatinMetrics metrics)
1424 FT_Render_Mode mode;
1425 FT_UInt32 scaler_flags, other_flags;
1426 FT_Face face = metrics->root.scaler.face;
1429 ta_glyph_hints_rescale(hints, (TA_ScriptMetrics)metrics);
1431 /* correct x_scale and y_scale if needed, since they may have */
1432 /* been modified by `ta_latin_metrics_scale_dim' above */
1433 hints->x_scale = metrics->axis[TA_DIMENSION_HORZ].scale;
1434 hints->x_delta = metrics->axis[TA_DIMENSION_HORZ].delta;
1435 hints->y_scale = metrics->axis[TA_DIMENSION_VERT].scale;
1436 hints->y_delta = metrics->axis[TA_DIMENSION_VERT].delta;
1438 /* compute flags depending on render mode, etc. */
1439 mode = metrics->root.scaler.render_mode;
1441 #if 0 /* #ifdef TA_CONFIG_OPTION_USE_WARPER */
1442 if (mode == FT_RENDER_MODE_LCD
1443 || mode == FT_RENDER_MODE_LCD_V)
1444 metrics->root.scaler.render_mode =
1445 mode = FT_RENDER_MODE_NORMAL;
1446 #endif
1448 scaler_flags = hints->scaler_flags;
1449 other_flags = 0;
1451 /* we snap the width of vertical stems for the monochrome */
1452 /* and horizontal LCD rendering targets only */
1453 if (mode == FT_RENDER_MODE_MONO
1454 || mode == FT_RENDER_MODE_LCD)
1455 other_flags |= TA_LATIN_HINTS_HORZ_SNAP;
1457 /* we snap the width of horizontal stems for the monochrome */
1458 /* and vertical LCD rendering targets only */
1459 if (mode == FT_RENDER_MODE_MONO
1460 || mode == FT_RENDER_MODE_LCD_V)
1461 other_flags |= TA_LATIN_HINTS_VERT_SNAP;
1463 /* we adjust stems to full pixels only if we don't use the `light' mode */
1464 if (mode != FT_RENDER_MODE_LIGHT)
1465 other_flags |= TA_LATIN_HINTS_STEM_ADJUST;
1467 if (mode == FT_RENDER_MODE_MONO)
1468 other_flags |= TA_LATIN_HINTS_MONO;
1470 /* in `light' hinting mode we disable horizontal hinting completely; */
1471 /* we also do it if the face is italic */
1472 if (mode == FT_RENDER_MODE_LIGHT
1473 || (face->style_flags & FT_STYLE_FLAG_ITALIC) != 0)
1474 scaler_flags |= TA_SCALER_FLAG_NO_HORIZONTAL;
1476 hints->scaler_flags = scaler_flags;
1477 hints->other_flags = other_flags;
1479 return FT_Err_Ok;
1483 /* snap a given width in scaled coordinates to */
1484 /* one of the current standard widths */
1486 static FT_Pos
1487 ta_latin_snap_width(TA_Width widths,
1488 FT_Int count,
1489 FT_Pos width)
1491 int n;
1492 FT_Pos best = 64 + 32 + 2;
1493 FT_Pos reference = width;
1494 FT_Pos scaled;
1497 for (n = 0; n < count; n++)
1499 FT_Pos w;
1500 FT_Pos dist;
1503 w = widths[n].cur;
1504 dist = width - w;
1505 if (dist < 0)
1506 dist = -dist;
1507 if (dist < best)
1509 best = dist;
1510 reference = w;
1514 scaled = TA_PIX_ROUND(reference);
1516 if (width >= reference)
1518 if (width < scaled + 48)
1519 width = reference;
1521 else
1523 if (width > scaled - 48)
1524 width = reference;
1527 return width;
1531 /* compute the snapped width of a given stem, ignoring very thin ones */
1533 /* there is a lot of voodoo in this function; changing the hard-coded */
1534 /* parameters influence the whole hinting process */
1536 static FT_Pos
1537 ta_latin_compute_stem_width(TA_GlyphHints hints,
1538 TA_Dimension dim,
1539 FT_Pos width,
1540 FT_Byte base_flags,
1541 FT_Byte stem_flags)
1543 TA_LatinMetrics metrics = (TA_LatinMetrics) hints->metrics;
1544 TA_LatinAxis axis = &metrics->axis[dim];
1546 FT_Pos dist = width;
1547 FT_Int sign = 0;
1548 FT_Int vertical = (dim == TA_DIMENSION_VERT);
1551 if (!TA_LATIN_HINTS_DO_STEM_ADJUST(hints)
1552 || axis->extra_light)
1553 return width;
1555 if (dist < 0)
1557 dist = -width;
1558 sign = 1;
1561 if ((vertical && !TA_LATIN_HINTS_DO_VERT_SNAP(hints))
1562 || (!vertical && !TA_LATIN_HINTS_DO_HORZ_SNAP(hints)))
1564 /* smooth hinting process: very lightly quantize the stem width */
1566 /* leave the widths of serifs alone */
1567 if ((stem_flags & TA_EDGE_SERIF)
1568 && vertical
1569 && (dist < 3 * 64))
1570 goto Done_Width;
1571 else if (base_flags & TA_EDGE_ROUND)
1573 if (dist < 80)
1574 dist = 64;
1576 else if (dist < 56)
1577 dist = 56;
1579 if (axis->width_count > 0)
1581 FT_Pos delta;
1584 /* compare to standard width */
1585 delta = dist - axis->widths[0].cur;
1587 if (delta < 0)
1588 delta = -delta;
1590 if (delta < 40)
1592 dist = axis->widths[0].cur;
1593 if (dist < 48)
1594 dist = 48;
1596 goto Done_Width;
1599 if (dist < 3 * 64)
1601 delta = dist & 63;
1602 dist &= -64;
1604 if (delta < 10)
1605 dist += delta;
1606 else if (delta < 32)
1607 dist += 10;
1608 else if (delta < 54)
1609 dist += 54;
1610 else
1611 dist += delta;
1613 else
1614 dist = (dist + 32) & ~63;
1617 else
1619 /* strong hinting process: snap the stem width to integer pixels */
1621 FT_Pos org_dist = dist;
1624 dist = ta_latin_snap_width(axis->widths, axis->width_count, dist);
1626 if (vertical)
1628 /* in the case of vertical hinting, */
1629 /* always round the stem heights to integer pixels */
1631 if (dist >= 64)
1632 dist = (dist + 16) & ~63;
1633 else
1634 dist = 64;
1636 else
1638 if (TA_LATIN_HINTS_DO_MONO(hints))
1640 /* monochrome horizontal hinting: */
1641 /* snap widths to integer pixels with a different threshold */
1643 if (dist < 64)
1644 dist = 64;
1645 else
1646 dist = (dist + 32) & ~63;
1648 else
1650 /* for horizontal anti-aliased hinting, we adopt a more subtle */
1651 /* approach: we strengthen small stems, round stems whose size */
1652 /* is between 1 and 2 pixels to an integer, otherwise nothing */
1654 if (dist < 48)
1655 dist = (dist + 64) >> 1;
1657 else if (dist < 128)
1659 /* we only round to an integer width if the corresponding */
1660 /* distortion is less than 1/4 pixel -- otherwise, this */
1661 /* makes everything worse since the diagonals, which are */
1662 /* not hinted, appear a lot bolder or thinner than the */
1663 /* vertical stems */
1665 FT_Pos delta;
1668 dist = (dist + 22) & ~63;
1669 delta = dist - org_dist;
1670 if (delta < 0)
1671 delta = -delta;
1673 if (delta >= 16)
1675 dist = org_dist;
1676 if (dist < 48)
1677 dist = (dist + 64) >> 1;
1680 else
1681 /* round otherwise to prevent color fringes in LCD mode */
1682 dist = (dist + 32) & ~63;
1687 Done_Width:
1688 if (sign)
1689 dist = -dist;
1691 return dist;
1695 /* align one stem edge relative to the previous stem edge */
1697 static void
1698 ta_latin_align_linked_edge(TA_GlyphHints hints,
1699 TA_Dimension dim,
1700 TA_Edge base_edge,
1701 TA_Edge stem_edge)
1703 FT_Pos dist = stem_edge->opos - base_edge->opos;
1705 FT_Pos fitted_width = ta_latin_compute_stem_width(
1706 hints, dim, dist,
1707 base_edge->flags,
1708 stem_edge->flags);
1711 stem_edge->pos = base_edge->pos + fitted_width;
1713 TA_LOG((" LINK: edge %d (opos=%.2f) linked to %.2f,"
1714 " dist was %.2f, now %.2f\n",
1715 stem_edge - hints->axis[dim].edges, stem_edge->opos / 64.0,
1716 stem_edge->pos / 64.0, dist / 64.0, fitted_width / 64.0));
1718 if (hints->recorder)
1719 hints->recorder(ta_link, hints, dim,
1720 base_edge, stem_edge, NULL, NULL, NULL);
1724 /* shift the coordinates of the `serif' edge by the same amount */
1725 /* as the corresponding `base' edge has been moved already */
1727 static void
1728 ta_latin_align_serif_edge(TA_GlyphHints hints,
1729 TA_Edge base,
1730 TA_Edge serif)
1732 FT_UNUSED(hints);
1734 serif->pos = base->pos + (serif->opos - base->opos);
1738 /* the main grid-fitting routine */
1740 void
1741 ta_latin_hint_edges(TA_GlyphHints hints,
1742 TA_Dimension dim)
1744 TA_AxisHints axis = &hints->axis[dim];
1746 TA_Edge edges = axis->edges;
1747 TA_Edge edge_limit = edges + axis->num_edges;
1748 FT_PtrDist n_edges;
1749 TA_Edge edge;
1751 TA_Edge anchor = NULL;
1752 FT_Int has_serifs = 0;
1755 TA_LOG(("%s edge hinting\n", dim == TA_DIMENSION_VERT ? "horizontal"
1756 : "vertical"));
1758 /* we begin by aligning all stems relative to the blue zone if needed -- */
1759 /* that's only for horizontal edges */
1761 if (dim == TA_DIMENSION_VERT
1762 && TA_HINTS_DO_BLUES(hints))
1764 for (edge = edges; edge < edge_limit; edge++)
1766 TA_Width blue;
1767 TA_Edge edge1, edge2; /* these edges form the stem to check */
1770 if (edge->flags & TA_EDGE_DONE)
1771 continue;
1773 blue = edge->blue_edge;
1774 edge1 = NULL;
1775 edge2 = edge->link;
1777 if (blue)
1778 edge1 = edge;
1780 /* flip edges if the other stem is aligned to a blue zone */
1781 else if (edge2 && edge2->blue_edge)
1783 blue = edge2->blue_edge;
1784 edge1 = edge2;
1785 edge2 = edge;
1788 if (!edge1)
1789 continue;
1791 if (!anchor)
1792 TA_LOG((" BLUE_ANCHOR: edge %d (opos=%.2f) snapped to %.2f,"
1793 " was %.2f (anchor=edge %d)\n",
1794 edge1 - edges, edge1->opos / 64.0, blue->fit / 64.0,
1795 edge1->pos / 64.0, edge - edges));
1796 else
1797 TA_LOG((" BLUE: edge %d (opos=%.2f) snapped to %.2f, was %.2f\n",
1798 edge1 - edges, edge1->opos / 64.0, blue->fit / 64.0,
1799 edge1->pos / 64.0));
1801 edge1->pos = blue->fit;
1802 edge1->flags |= TA_EDGE_DONE;
1804 if (hints->recorder)
1806 if (!anchor)
1807 hints->recorder(ta_blue_anchor, hints, dim,
1808 edge1, edge, NULL, NULL, NULL);
1809 else
1810 hints->recorder(ta_blue, hints, dim,
1811 edge1, NULL, NULL, NULL, NULL);
1814 if (edge2 && !edge2->blue_edge)
1816 ta_latin_align_linked_edge(hints, dim, edge1, edge2);
1817 edge2->flags |= TA_EDGE_DONE;
1820 if (!anchor)
1821 anchor = edge;
1825 /* now we align all other stem edges, */
1826 /* trying to maintain the relative order of stems in the glyph */
1827 for (edge = edges; edge < edge_limit; edge++)
1829 TA_Edge edge2;
1832 if (edge->flags & TA_EDGE_DONE)
1833 continue;
1835 /* skip all non-stem edges */
1836 edge2 = edge->link;
1837 if (!edge2)
1839 has_serifs++;
1840 continue;
1843 /* now align the stem */
1845 /* this should not happen, but it's better to be safe */
1846 if (edge2->blue_edge)
1848 TA_LOG((" ASSERTION FAILED for edge %d\n", edge2-edges));
1850 ta_latin_align_linked_edge(hints, dim, edge2, edge);
1851 edge->flags |= TA_EDGE_DONE;
1852 continue;
1855 if (!anchor)
1857 /* if we reach this if clause, no stem has been aligned yet */
1859 FT_Pos org_len, org_center, cur_len;
1860 FT_Pos cur_pos1, error1, error2, u_off, d_off;
1863 org_len = edge2->opos - edge->opos;
1864 cur_len = ta_latin_compute_stem_width(hints, dim, org_len,
1865 edge->flags, edge2->flags);
1867 /* some voodoo to specially round edges for small stem widths; */
1868 /* the idea is to align the center of a stem, */
1869 /* then shifting the stem edges to suitable positions */
1870 if (cur_len <= 64)
1872 /* width <= 1px */
1873 u_off = 32;
1874 d_off = 32;
1876 else
1878 /* 1px < width < 1.5px */
1879 u_off = 38;
1880 d_off = 26;
1883 if (cur_len < 96)
1885 org_center = edge->opos + (org_len >> 1);
1886 cur_pos1 = TA_PIX_ROUND(org_center);
1888 error1 = org_center - (cur_pos1 - u_off);
1889 if (error1 < 0)
1890 error1 = -error1;
1892 error2 = org_center - (cur_pos1 + d_off);
1893 if (error2 < 0)
1894 error2 = -error2;
1896 if (error1 < error2)
1897 cur_pos1 -= u_off;
1898 else
1899 cur_pos1 += d_off;
1901 edge->pos = cur_pos1 - cur_len / 2;
1902 edge2->pos = edge->pos + cur_len;
1904 else
1905 edge->pos = TA_PIX_ROUND(edge->opos);
1907 anchor = edge;
1908 edge->flags |= TA_EDGE_DONE;
1910 TA_LOG((" ANCHOR: edge %d (opos=%.2f) and %d (opos=%.2f)"
1911 " snapped to %.2f and %.2f\n",
1912 edge - edges, edge->opos / 64.0,
1913 edge2 - edges, edge2->opos / 64.0,
1914 edge->pos / 64.0, edge2->pos / 64.0));
1916 if (hints->recorder)
1917 hints->recorder(ta_anchor, hints, dim,
1918 edge, edge2, NULL, NULL, NULL);
1920 ta_latin_align_linked_edge(hints, dim, edge, edge2);
1922 else
1924 FT_Pos org_pos, org_len, org_center, cur_len;
1925 FT_Pos cur_pos1, cur_pos2, delta1, delta2;
1928 org_pos = anchor->pos + (edge->opos - anchor->opos);
1929 org_len = edge2->opos - edge->opos;
1930 org_center = org_pos + (org_len >> 1);
1932 cur_len = ta_latin_compute_stem_width(hints, dim, org_len,
1933 edge->flags, edge2->flags);
1935 if (edge2->flags & TA_EDGE_DONE)
1937 TA_LOG((" ADJUST: edge %d (pos=%.2f) moved to %.2f\n",
1938 edge - edges, edge->pos / 64.0,
1939 (edge2->pos - cur_len) / 64.0));
1941 edge->pos = edge2->pos - cur_len;
1943 if (hints->recorder)
1945 TA_Edge bound = NULL;
1948 if (edge > edges)
1949 bound = &edge[-1];
1951 hints->recorder(ta_adjust, hints, dim,
1952 edge, edge2, NULL, bound, NULL);
1956 else if (cur_len < 96)
1958 FT_Pos u_off, d_off;
1961 cur_pos1 = TA_PIX_ROUND(org_center);
1963 if (cur_len <= 64)
1965 u_off = 32;
1966 d_off = 32;
1968 else
1970 u_off = 38;
1971 d_off = 26;
1974 delta1 = org_center - (cur_pos1 - u_off);
1975 if (delta1 < 0)
1976 delta1 = -delta1;
1978 delta2 = org_center - (cur_pos1 + d_off);
1979 if (delta2 < 0)
1980 delta2 = -delta2;
1982 if (delta1 < delta2)
1983 cur_pos1 -= u_off;
1984 else
1985 cur_pos1 += d_off;
1987 edge->pos = cur_pos1 - cur_len / 2;
1988 edge2->pos = cur_pos1 + cur_len / 2;
1990 TA_LOG((" STEM: edge %d (opos=%.2f) linked to %d (opos=%.2f)"
1991 " snapped to %.2f and %.2f\n",
1992 edge - edges, edge->opos / 64.0,
1993 edge2 - edges, edge2->opos / 64.0,
1994 edge->pos / 64.0, edge2->pos / 64.0));
1996 if (hints->recorder)
1998 TA_Edge bound = NULL;
2001 if (edge > edges)
2002 bound = &edge[-1];
2004 hints->recorder(ta_stem, hints, dim,
2005 edge, edge2, NULL, bound, NULL);
2009 else
2011 org_pos = anchor->pos + (edge->opos - anchor->opos);
2012 org_len = edge2->opos - edge->opos;
2013 org_center = org_pos + (org_len >> 1);
2015 cur_len = ta_latin_compute_stem_width(hints, dim, org_len,
2016 edge->flags, edge2->flags);
2018 cur_pos1 = TA_PIX_ROUND(org_pos);
2019 delta1 = cur_pos1 + (cur_len >> 1) - org_center;
2020 if (delta1 < 0)
2021 delta1 = -delta1;
2023 cur_pos2 = TA_PIX_ROUND(org_pos + org_len) - cur_len;
2024 delta2 = cur_pos2 + (cur_len >> 1) - org_center;
2025 if (delta2 < 0)
2026 delta2 = -delta2;
2028 edge->pos = (delta1 < delta2) ? cur_pos1 : cur_pos2;
2029 edge2->pos = edge->pos + cur_len;
2031 TA_LOG((" STEM: edge %d (opos=%.2f) linked to %d (opos=%.2f)"
2032 " snapped to %.2f and %.2f\n",
2033 edge - edges, edge->opos / 64.0,
2034 edge2 - edges, edge2->opos / 64.0,
2035 edge->pos / 64.0, edge2->pos / 64.0));
2037 if (hints->recorder)
2039 TA_Edge bound = NULL;
2042 if (edge > edges)
2043 bound = &edge[-1];
2045 hints->recorder(ta_stem, hints, dim,
2046 edge, edge2, NULL, bound, NULL);
2050 edge->flags |= TA_EDGE_DONE;
2051 edge2->flags |= TA_EDGE_DONE;
2053 if (edge > edges
2054 && edge->pos < edge[-1].pos)
2056 TA_LOG((" BOUND: edge %d (pos=%.2f) moved to %.2f\n",
2057 edge - edges, edge->pos / 64.0, edge[-1].pos / 64.0));
2059 edge->pos = edge[-1].pos;
2061 if (hints->recorder)
2062 hints->recorder(ta_bound, hints, dim,
2063 edge, &edge[-1], NULL, NULL, NULL);
2068 /* make sure that lowercase m's maintain their symmetry */
2070 /* In general, lowercase m's have six vertical edges if they are sans */
2071 /* serif, or twelve if they are with serifs. This implementation is */
2072 /* based on that assumption, and seems to work very well with most */
2073 /* faces. However, if for a certain face this assumption is not */
2074 /* true, the m is just rendered like before. In addition, any stem */
2075 /* correction will only be applied to symmetrical glyphs (even if the */
2076 /* glyph is not an m), so the potential for unwanted distortion is */
2077 /* relatively low. */
2079 /* we don't handle horizontal edges since we can't easily assure that */
2080 /* the third (lowest) stem aligns with the base line; it might end up */
2081 /* one pixel higher or lower */
2083 n_edges = edge_limit - edges;
2084 if (dim == TA_DIMENSION_HORZ
2085 && (n_edges == 6 || n_edges == 12))
2087 TA_Edge edge1, edge2, edge3;
2088 FT_Pos dist1, dist2, span, delta;
2091 if (n_edges == 6)
2093 edge1 = edges;
2094 edge2 = edges + 2;
2095 edge3 = edges + 4;
2097 else
2099 edge1 = edges + 1;
2100 edge2 = edges + 5;
2101 edge3 = edges + 9;
2104 dist1 = edge2->opos - edge1->opos;
2105 dist2 = edge3->opos - edge2->opos;
2107 span = dist1 - dist2;
2108 if (span < 0)
2109 span = -span;
2111 if (span < 8)
2113 delta = edge3->pos - (2 * edge2->pos - edge1->pos);
2114 edge3->pos -= delta;
2115 if (edge3->link)
2116 edge3->link->pos -= delta;
2118 /* move the serifs along with the stem */
2119 if (n_edges == 12)
2121 (edges + 8)->pos -= delta;
2122 (edges + 11)->pos -= delta;
2125 edge3->flags |= TA_EDGE_DONE;
2126 if (edge3->link)
2127 edge3->link->flags |= TA_EDGE_DONE;
2131 if (has_serifs || !anchor)
2133 /* now hint the remaining edges (serifs and single) */
2134 /* in order to complete our processing */
2135 for (edge = edges; edge < edge_limit; edge++)
2137 TA_Edge lower_bound = NULL;
2138 TA_Edge upper_bound = NULL;
2140 FT_Pos delta;
2143 if (edge->flags & TA_EDGE_DONE)
2144 continue;
2146 delta = 1000;
2148 if (edge->serif)
2150 delta = edge->serif->opos - edge->opos;
2151 if (delta < 0)
2152 delta = -delta;
2155 if (edge > edges)
2156 lower_bound = &edge[-1];
2158 if (edge + 1 < edge_limit
2159 && edge[1].flags & TA_EDGE_DONE)
2160 upper_bound = &edge[1];
2163 if (delta < 64 + 16)
2165 ta_latin_align_serif_edge(hints, edge->serif, edge);
2167 TA_LOG((" SERIF: edge %d (opos=%.2f) serif to %d (opos=%.2f)"
2168 " aligned to %.2f\n",
2169 edge - edges, edge->opos / 64.0,
2170 edge->serif - edges, edge->serif->opos / 64.0,
2171 edge->pos / 64.0));
2173 if (hints->recorder)
2174 hints->recorder(ta_serif, hints, dim,
2175 edge, NULL, NULL, lower_bound, upper_bound);
2177 else if (!anchor)
2179 edge->pos = TA_PIX_ROUND(edge->opos);
2180 anchor = edge;
2182 TA_LOG((" SERIF_ANCHOR: edge %d (opos=%.2f) snapped to %.2f\n",
2183 edge - edges, edge->opos / 64.0, edge->pos / 64.0));
2185 if (hints->recorder)
2186 hints->recorder(ta_serif_anchor, hints, dim,
2187 edge, NULL, NULL, lower_bound, upper_bound);
2189 else
2191 TA_Edge before, after;
2194 for (before = edge - 1; before >= edges; before--)
2195 if (before->flags & TA_EDGE_DONE)
2196 break;
2198 for (after = edge + 1; after < edge_limit; after++)
2199 if (after->flags & TA_EDGE_DONE)
2200 break;
2202 if (before >= edges && before < edge
2203 && after < edge_limit && after > edge)
2205 if (after->opos == before->opos)
2206 edge->pos = before->pos;
2207 else
2208 edge->pos = before->pos + FT_MulDiv(edge->opos - before->opos,
2209 after->pos - before->pos,
2210 after->opos - before->opos);
2212 TA_LOG((" SERIF_LINK1: edge %d (opos=%.2f) snapped to %.2f"
2213 " from %d (opos=%.2f)\n",
2214 edge - edges, edge->opos / 64.0,
2215 edge->pos / 64.0,
2216 before - edges, before->opos / 64.0));
2218 if (hints->recorder)
2219 hints->recorder(ta_serif_link1, hints, dim,
2220 edge, before, after, lower_bound, upper_bound);
2222 else
2224 edge->pos = anchor->pos + ((edge->opos - anchor->opos + 16) & ~31);
2226 TA_LOG((" SERIF_LINK2: edge %d (opos=%.2f) snapped to %.2f\n",
2227 edge - edges, edge->opos / 64.0, edge->pos / 64.0));
2229 if (hints->recorder)
2230 hints->recorder(ta_serif_link2, hints, dim,
2231 edge, NULL, NULL, lower_bound, upper_bound);
2235 edge->flags |= TA_EDGE_DONE;
2237 if (edge > edges
2238 && edge->pos < edge[-1].pos)
2240 TA_LOG((" BOUND: edge %d (pos=%.2f) moved to %.2f\n",
2241 edge - edges, edge->pos / 64.0, edge[-1].pos / 64.0));
2243 edge->pos = edge[-1].pos;
2245 if (hints->recorder)
2246 hints->recorder(ta_bound, hints, dim,
2247 edge, &edge[-1], NULL, NULL, NULL);
2250 if (edge + 1 < edge_limit
2251 && edge[1].flags & TA_EDGE_DONE
2252 && edge->pos > edge[1].pos)
2254 TA_LOG((" BOUND: edge %d (pos=%.2f) moved to %.2f\n",
2255 edge - edges, edge->pos / 64.0, edge[1].pos / 64.0));
2257 edge->pos = edge[1].pos;
2259 if (hints->recorder)
2260 hints->recorder(ta_bound, hints, dim,
2261 edge, &edge[1], NULL, NULL, NULL);
2266 TA_LOG(("\n"));
2270 /* apply the complete hinting algorithm to a latin glyph */
2272 static FT_Error
2273 ta_latin_hints_apply(TA_GlyphHints hints,
2274 FT_Outline* outline,
2275 TA_LatinMetrics metrics)
2277 FT_Error error;
2278 int dim;
2281 error = ta_glyph_hints_reload(hints, outline);
2282 if (error)
2283 goto Exit;
2285 /* analyze glyph outline */
2286 #ifdef TA_CONFIG_OPTION_USE_WARPER
2287 if (metrics->root.scaler.render_mode == FT_RENDER_MODE_LIGHT
2288 || TA_HINTS_DO_HORIZONTAL(hints))
2289 #else
2290 if (TA_HINTS_DO_HORIZONTAL(hints))
2291 #endif
2293 error = ta_latin_hints_detect_features(hints, TA_DIMENSION_HORZ);
2294 if (error)
2295 goto Exit;
2298 if (TA_HINTS_DO_VERTICAL(hints))
2300 error = ta_latin_hints_detect_features(hints, TA_DIMENSION_VERT);
2301 if (error)
2302 goto Exit;
2304 ta_latin_hints_compute_blue_edges(hints, metrics);
2307 /* grid-fit the outline */
2308 for (dim = 0; dim < TA_DIMENSION_MAX; dim++)
2310 #ifdef TA_CONFIG_OPTION_USE_WARPER
2311 if (dim == TA_DIMENSION_HORZ
2312 && metrics->root.scaler.render_mode == FT_RENDER_MODE_LIGHT)
2314 TA_WarperRec warper;
2315 FT_Fixed scale;
2316 FT_Pos delta;
2319 ta_warper_compute(&warper, hints, (TA_Dimension)dim, &scale, &delta);
2320 ta_glyph_hints_scale_dim(hints, (TA_Dimension)dim, scale, delta);
2322 continue;
2324 #endif
2326 if ((dim == TA_DIMENSION_HORZ && TA_HINTS_DO_HORIZONTAL(hints))
2327 || (dim == TA_DIMENSION_VERT && TA_HINTS_DO_VERTICAL(hints)))
2329 ta_latin_hint_edges(hints, (TA_Dimension)dim);
2330 ta_glyph_hints_align_edge_points(hints, (TA_Dimension)dim);
2331 ta_glyph_hints_align_strong_points(hints, (TA_Dimension)dim);
2332 ta_glyph_hints_align_weak_points(hints, (TA_Dimension)dim);
2335 ta_glyph_hints_save(hints, outline);
2337 Exit:
2338 return error;
2342 /* XXX: this should probably fine tuned to differentiate better between */
2343 /* scripts... */
2345 static const TA_Script_UniRangeRec ta_latin_uniranges[] =
2347 TA_UNIRANGE_REC(0x0020UL, 0x007FUL), /* Basic Latin (no control chars) */
2348 TA_UNIRANGE_REC(0x00A0UL, 0x00FFUL), /* Latin-1 Supplement (no control chars) */
2349 TA_UNIRANGE_REC(0x0100UL, 0x017FUL), /* Latin Extended-A */
2350 TA_UNIRANGE_REC(0x0180UL, 0x024FUL), /* Latin Extended-B */
2351 TA_UNIRANGE_REC(0x0250UL, 0x02AFUL), /* IPA Extensions */
2352 TA_UNIRANGE_REC(0x02B0UL, 0x02FFUL), /* Spacing Modifier Letters */
2353 TA_UNIRANGE_REC(0x0300UL, 0x036FUL), /* Combining Diacritical Marks */
2354 TA_UNIRANGE_REC(0x0370UL, 0x03FFUL), /* Greek and Coptic */
2355 TA_UNIRANGE_REC(0x0400UL, 0x04FFUL), /* Cyrillic */
2356 TA_UNIRANGE_REC(0x0500UL, 0x052FUL), /* Cyrillic Supplement */
2357 TA_UNIRANGE_REC(0x1D00UL, 0x1D7FUL), /* Phonetic Extensions */
2358 TA_UNIRANGE_REC(0x1D80UL, 0x1DBFUL), /* Phonetic Extensions Supplement */
2359 TA_UNIRANGE_REC(0x1DC0UL, 0x1DFFUL), /* Combining Diacritical Marks Supplement */
2360 TA_UNIRANGE_REC(0x1E00UL, 0x1EFFUL), /* Latin Extended Additional */
2361 TA_UNIRANGE_REC(0x1F00UL, 0x1FFFUL), /* Greek Extended */
2362 TA_UNIRANGE_REC(0x2000UL, 0x206FUL), /* General Punctuation */
2363 TA_UNIRANGE_REC(0x2070UL, 0x209FUL), /* Superscripts and Subscripts */
2364 TA_UNIRANGE_REC(0x20A0UL, 0x20CFUL), /* Currency Symbols */
2365 TA_UNIRANGE_REC(0x2150UL, 0x218FUL), /* Number Forms */
2366 TA_UNIRANGE_REC(0x2460UL, 0x24FFUL), /* Enclosed Alphanumerics */
2367 TA_UNIRANGE_REC(0x2C60UL, 0x2C7FUL), /* Latin Extended-C */
2368 TA_UNIRANGE_REC(0x2DE0UL, 0x2DFFUL), /* Cyrillic Extended-A */
2369 TA_UNIRANGE_REC(0xA640UL, 0xA69FUL), /* Cyrillic Extended-B */
2370 TA_UNIRANGE_REC(0xA720UL, 0xA7FFUL), /* Latin Extended-D */
2371 TA_UNIRANGE_REC(0xFB00UL, 0xFB06UL), /* Alphab. Present. Forms (Latin Ligs) */
2372 TA_UNIRANGE_REC(0x1D400UL, 0x1D7FFUL), /* Mathematical Alphanumeric Symbols */
2373 TA_UNIRANGE_REC(0UL, 0UL)
2377 const TA_ScriptClassRec ta_latin_script_class =
2379 TA_SCRIPT_LATIN,
2380 ta_latin_uniranges,
2382 sizeof (TA_LatinMetricsRec),
2384 (TA_Script_InitMetricsFunc)ta_latin_metrics_init,
2385 (TA_Script_ScaleMetricsFunc)ta_latin_metrics_scale,
2386 (TA_Script_DoneMetricsFunc)NULL,
2388 (TA_Script_InitHintsFunc)ta_latin_hints_init,
2389 (TA_Script_ApplyHintsFunc)ta_latin_hints_apply
2392 /* end of talatin.c */