[gui] Pass missing command line flags.
[ttfautohint.git] / lib / talatin.c
blob581f2d42cc2bbf321b5cd63b39177ef114783a6d
1 /* talatin.c */
3 /*
4 * Copyright (C) 2011-2012 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;
548 FT_Pos threshold;
549 FT_Pos fitted;
552 scaled = FT_MulFix(blue->shoot.org, scaler->y_scale);
554 threshold = 40;
555 if ((scaler->flags & TA_SCALER_FLAG_INCREASE_X_HEIGHT)
556 && metrics->root.scaler.face->size->metrics.x_ppem < 15
557 && metrics->root.scaler.face->size->metrics.x_ppem > 5)
558 threshold = 52;
560 fitted = (scaled + threshold) & ~63;
562 if (scaled != fitted)
564 if (dim == TA_DIMENSION_VERT)
565 scale = FT_MulDiv(scale, fitted, scaled);
570 axis->scale = scale;
571 axis->delta = delta;
573 if (dim == TA_DIMENSION_HORZ)
575 metrics->root.scaler.x_scale = scale;
576 metrics->root.scaler.x_delta = delta;
578 else
580 metrics->root.scaler.y_scale = scale;
581 metrics->root.scaler.y_delta = delta;
584 /* scale the widths */
585 for (nn = 0; nn < axis->width_count; nn++)
587 TA_Width width = axis->widths + nn;
590 width->cur = FT_MulFix(width->org, scale);
591 width->fit = width->cur;
594 /* an extra-light axis corresponds to a standard width that is */
595 /* smaller than 5/8 pixels */
596 axis->extra_light =
597 (FT_Bool)(FT_MulFix(axis->standard_width, scale) < 32 + 8);
599 if (dim == TA_DIMENSION_VERT)
601 /* scale the blue zones */
602 for (nn = 0; nn < axis->blue_count; nn++)
604 TA_LatinBlue blue = &axis->blues[nn];
605 FT_Pos dist;
608 blue->ref.cur = FT_MulFix(blue->ref.org, scale) + delta;
609 blue->ref.fit = blue->ref.cur;
610 blue->shoot.cur = FT_MulFix(blue->shoot.org, scale) + delta;
611 blue->shoot.fit = blue->shoot.cur;
612 blue->flags &= ~TA_LATIN_BLUE_ACTIVE;
614 /* a blue zone is only active if it is less than 3/4 pixels tall */
615 dist = FT_MulFix(blue->ref.org - blue->shoot.org, scale);
616 if (dist <= 48 && dist >= -48)
618 #if 0
619 FT_Pos delta1;
620 #endif
621 FT_Pos delta2;
624 /* use discrete values for blue zone widths */
626 #if 0
627 /* generic, original code */
628 delta1 = blue->shoot.org - blue->ref.org;
629 delta2 = delta1;
630 if (delta1 < 0)
631 delta2 = -delta2;
633 delta2 = FT_MulFix(delta2, scale);
635 if (delta2 < 32)
636 delta2 = 0;
637 else if (delta2 < 64)
638 delta2 = 32 + (((delta2 - 32) + 16) & ~31);
639 else
640 delta2 = TA_PIX_ROUND(delta2);
642 if (delta1 < 0)
643 delta2 = -delta2;
645 blue->ref.fit = TA_PIX_ROUND(blue->ref.cur);
646 blue->shoot.fit = blue->ref.fit + delta2;
647 #else
648 /* simplified version due to abs(dist) <= 48 */
649 delta2 = dist;
650 if (dist < 0)
651 delta2 = -delta2;
653 if (delta2 < 32)
654 delta2 = 0;
655 else if (delta < 48)
656 delta2 = 32;
657 else
658 delta2 = 64;
660 if (dist < 0)
661 delta2 = -delta2;
663 blue->ref.fit = TA_PIX_ROUND(blue->ref.cur);
664 blue->shoot.fit = blue->ref.fit - delta2;
665 #endif
667 blue->flags |= TA_LATIN_BLUE_ACTIVE;
674 /* scale global values in both directions */
676 void
677 ta_latin_metrics_scale(TA_LatinMetrics metrics,
678 TA_Scaler scaler)
680 metrics->root.scaler.render_mode = scaler->render_mode;
681 metrics->root.scaler.face = scaler->face;
683 ta_latin_metrics_scale_dim(metrics, scaler, TA_DIMENSION_HORZ);
684 ta_latin_metrics_scale_dim(metrics, scaler, TA_DIMENSION_VERT);
688 /* walk over all contours and compute its segments */
690 FT_Error
691 ta_latin_hints_compute_segments(TA_GlyphHints hints,
692 TA_Dimension dim)
694 TA_AxisHints axis = &hints->axis[dim];
695 FT_Error error = FT_Err_Ok;
697 TA_Segment segment = NULL;
698 TA_SegmentRec seg0;
700 TA_Point* contour = hints->contours;
701 TA_Point* contour_limit = contour + hints->num_contours;
702 TA_Direction major_dir, segment_dir;
705 memset(&seg0, 0, sizeof (TA_SegmentRec));
706 seg0.score = 32000;
707 seg0.flags = TA_EDGE_NORMAL;
709 major_dir = (TA_Direction)TA_ABS(axis->major_dir);
710 segment_dir = major_dir;
712 axis->num_segments = 0;
714 /* set up (u,v) in each point */
715 if (dim == TA_DIMENSION_HORZ)
717 TA_Point point = hints->points;
718 TA_Point limit = point + hints->num_points;
721 for (; point < limit; point++)
723 point->u = point->fx;
724 point->v = point->fy;
727 else
729 TA_Point point = hints->points;
730 TA_Point limit = point + hints->num_points;
733 for (; point < limit; point++)
735 point->u = point->fy;
736 point->v = point->fx;
740 /* do each contour separately */
741 for (; contour < contour_limit; contour++)
743 TA_Point point = contour[0];
744 TA_Point last = point->prev;
746 int on_edge = 0;
748 FT_Pos min_pos = 32000; /* minimum segment pos != min_coord */
749 FT_Pos max_pos = -32000; /* maximum segment pos != max_coord */
750 FT_Bool passed;
753 if (point == last) /* skip singletons -- just in case */
754 continue;
756 if (TA_ABS(last->out_dir) == major_dir
757 && TA_ABS(point->out_dir) == major_dir)
759 /* we are already on an edge, try to locate its start */
760 last = point;
762 for (;;)
764 point = point->prev;
765 if (TA_ABS(point->out_dir) != major_dir)
767 point = point->next;
768 break;
770 if (point == last)
771 break;
775 last = point;
776 passed = 0;
778 for (;;)
780 FT_Pos u, v;
783 if (on_edge)
785 u = point->u;
786 if (u < min_pos)
787 min_pos = u;
788 if (u > max_pos)
789 max_pos = u;
791 if (point->out_dir != segment_dir
792 || point == last)
794 /* we are just leaving an edge; record a new segment! */
795 segment->last = point;
796 segment->pos = (FT_Short)((min_pos + max_pos) >> 1);
798 /* a segment is round if either its first or last point */
799 /* is a control point */
800 if ((segment->first->flags | point->flags) & TA_FLAG_CONTROL)
801 segment->flags |= TA_EDGE_ROUND;
803 /* compute segment size */
804 min_pos = max_pos = point->v;
806 v = segment->first->v;
807 if (v < min_pos)
808 min_pos = v;
809 if (v > max_pos)
810 max_pos = v;
812 segment->min_coord = (FT_Short)min_pos;
813 segment->max_coord = (FT_Short)max_pos;
814 segment->height = (FT_Short)(segment->max_coord -
815 segment->min_coord);
817 on_edge = 0;
818 segment = NULL;
819 /* fall through */
823 /* now exit if we are at the start/end point */
824 if (point == last)
826 if (passed)
827 break;
828 passed = 1;
831 if (!on_edge
832 && TA_ABS(point->out_dir) == major_dir)
834 /* this is the start of a new segment! */
835 segment_dir = (TA_Direction)point->out_dir;
837 /* clear all segment fields */
838 error = ta_axis_hints_new_segment(axis, &segment);
839 if (error)
840 goto Exit;
842 segment[0] = seg0;
843 segment->dir = (FT_Char)segment_dir;
844 min_pos = max_pos = point->u;
845 segment->first = point;
846 segment->last = point;
847 on_edge = 1;
850 point = point->next;
852 } /* contours */
855 /* now slightly increase the height of segments if this makes sense -- */
856 /* this is used to better detect and ignore serifs */
858 TA_Segment segments = axis->segments;
859 TA_Segment segments_end = segments + axis->num_segments;
862 for (segment = segments; segment < segments_end; segment++)
864 TA_Point first = segment->first;
865 TA_Point last = segment->last;
867 FT_Pos first_v = first->v;
868 FT_Pos last_v = last->v;
871 if (first == last)
872 continue;
874 if (first_v < last_v)
876 TA_Point p;
879 p = first->prev;
880 if (p->v < first_v)
881 segment->height = (FT_Short)(segment->height +
882 ((first_v - p->v) >> 1));
884 p = last->next;
885 if (p->v > last_v)
886 segment->height = (FT_Short)(segment->height +
887 ((p->v - last_v) >> 1));
889 else
891 TA_Point p;
894 p = first->prev;
895 if (p->v > first_v)
896 segment->height = (FT_Short)(segment->height +
897 ((p->v - first_v) >> 1));
899 p = last->next;
900 if (p->v < last_v)
901 segment->height = (FT_Short)(segment->height +
902 ((last_v - p->v) >> 1));
907 Exit:
908 return error;
912 /* link segments to form stems and serifs */
914 void
915 ta_latin_hints_link_segments(TA_GlyphHints hints,
916 TA_Dimension dim)
918 TA_AxisHints axis = &hints->axis[dim];
920 TA_Segment segments = axis->segments;
921 TA_Segment segment_limit = segments + axis->num_segments;
923 FT_Pos len_threshold, len_score;
924 TA_Segment seg1, seg2;
927 len_threshold = TA_LATIN_CONSTANT(hints->metrics, 8);
928 if (len_threshold == 0)
929 len_threshold = 1;
931 len_score = TA_LATIN_CONSTANT(hints->metrics, 6000);
933 /* now compare each segment to the others */
934 for (seg1 = segments; seg1 < segment_limit; seg1++)
936 /* the fake segments are introduced to hint the metrics -- */
937 /* we must never link them to anything */
938 if (seg1->dir != axis->major_dir
939 || seg1->first == seg1->last)
940 continue;
942 /* search for stems having opposite directions, */
943 /* with seg1 to the `left' of seg2 */
944 for (seg2 = segments; seg2 < segment_limit; seg2++)
946 FT_Pos pos1 = seg1->pos;
947 FT_Pos pos2 = seg2->pos;
950 if (seg1->dir + seg2->dir == 0
951 && pos2 > pos1)
953 /* compute distance between the two segments */
954 FT_Pos dist = pos2 - pos1;
955 FT_Pos min = seg1->min_coord;
956 FT_Pos max = seg1->max_coord;
957 FT_Pos len, score;
960 if (min < seg2->min_coord)
961 min = seg2->min_coord;
962 if (max > seg2->max_coord)
963 max = seg2->max_coord;
965 /* compute maximum coordinate difference of the two segments */
966 len = max - min;
967 if (len >= len_threshold)
969 /* small coordinate differences cause a higher score, and */
970 /* segments with a greater distance cause a higher score also */
971 score = dist + len_score / len;
973 /* and we search for the smallest score */
974 /* of the sum of the two values */
975 if (score < seg1->score)
977 seg1->score = score;
978 seg1->link = seg2;
981 if (score < seg2->score)
983 seg2->score = score;
984 seg2->link = seg1;
991 /* now compute the `serif' segments, cf. explanations in `tahints.h' */
992 for (seg1 = segments; seg1 < segment_limit; seg1++)
994 seg2 = seg1->link;
996 if (seg2)
998 if (seg2->link != seg1)
1000 seg1->link = 0;
1001 seg1->serif = seg2->link;
1008 /* link segments to edges, using feature analysis for selection */
1010 FT_Error
1011 ta_latin_hints_compute_edges(TA_GlyphHints hints,
1012 TA_Dimension dim)
1014 TA_AxisHints axis = &hints->axis[dim];
1015 FT_Error error = FT_Err_Ok;
1016 TA_LatinAxis laxis = &((TA_LatinMetrics)hints->metrics)->axis[dim];
1018 TA_Segment segments = axis->segments;
1019 TA_Segment segment_limit = segments + axis->num_segments;
1020 TA_Segment seg;
1022 #if 0
1023 TA_Direction up_dir;
1024 #endif
1025 FT_Fixed scale;
1026 FT_Pos edge_distance_threshold;
1027 FT_Pos segment_length_threshold;
1030 axis->num_edges = 0;
1032 scale = (dim == TA_DIMENSION_HORZ) ? hints->x_scale
1033 : hints->y_scale;
1035 #if 0
1036 up_dir = (dim == TA_DIMENSION_HORZ) ? TA_DIR_UP
1037 : TA_DIR_RIGHT;
1038 #endif
1040 /* we ignore all segments that are less than 1 pixel in length */
1041 /* to avoid many problems with serif fonts */
1042 /* (the corresponding threshold is computed in font units) */
1043 if (dim == TA_DIMENSION_HORZ)
1044 segment_length_threshold = FT_DivFix(64, hints->y_scale);
1045 else
1046 segment_length_threshold = 0;
1048 /********************************************************************/
1049 /* */
1050 /* We begin by generating a sorted table of edges for the current */
1051 /* direction. To do so, we simply scan each segment and try to find */
1052 /* an edge in our table that corresponds to its position. */
1053 /* */
1054 /* If no edge is found, we create and insert a new edge in the */
1055 /* sorted table. Otherwise, we simply add the segment to the edge's */
1056 /* list which gets processed in the second step to compute the */
1057 /* edge's properties. */
1058 /* */
1059 /* Note that the table of edges is sorted along the segment/edge */
1060 /* position. */
1061 /* */
1062 /********************************************************************/
1064 /* assure that edge distance threshold is at most 0.25px */
1065 edge_distance_threshold = FT_MulFix(laxis->edge_distance_threshold,
1066 scale);
1067 if (edge_distance_threshold > 64 / 4)
1068 edge_distance_threshold = 64 / 4;
1070 edge_distance_threshold = FT_DivFix(edge_distance_threshold,
1071 scale);
1073 for (seg = segments; seg < segment_limit; seg++)
1075 TA_Edge found = NULL;
1076 FT_Int ee;
1079 if (seg->height < segment_length_threshold)
1080 continue;
1082 /* a special case for serif edges: */
1083 /* if they are smaller than 1.5 pixels we ignore them */
1084 if (seg->serif
1085 && 2 * seg->height < 3 * segment_length_threshold)
1086 continue;
1088 /* look for an edge corresponding to the segment */
1089 for (ee = 0; ee < axis->num_edges; ee++)
1091 TA_Edge edge = axis->edges + ee;
1092 FT_Pos dist;
1095 dist = seg->pos - edge->fpos;
1096 if (dist < 0)
1097 dist = -dist;
1099 if (dist < edge_distance_threshold && edge->dir == seg->dir)
1101 found = edge;
1102 break;
1106 if (!found)
1108 TA_Edge edge;
1111 /* insert a new edge in the list and sort according to the position */
1112 error = ta_axis_hints_new_edge(axis, seg->pos,
1113 (TA_Direction)seg->dir,
1114 &edge);
1115 if (error)
1116 goto Exit;
1118 /* add the segment to the new edge's list */
1119 memset(edge, 0, sizeof (TA_EdgeRec));
1120 edge->first = seg;
1121 edge->last = seg;
1122 edge->dir = seg->dir;
1123 edge->fpos = seg->pos;
1124 edge->opos = FT_MulFix(seg->pos, scale);
1125 edge->pos = edge->opos;
1126 seg->edge_next = seg;
1128 else
1130 /* if an edge was found, simply add the segment to the edge's list */
1131 seg->edge_next = found->first;
1132 found->last->edge_next = seg;
1133 found->last = seg;
1137 /*****************************************************************/
1138 /* */
1139 /* Good, we now compute each edge's properties according to */
1140 /* the segments found on its position. Basically, these are */
1141 /* */
1142 /* - the edge's main direction */
1143 /* - stem edge, serif edge or both (which defaults to stem then) */
1144 /* - rounded edge, straight or both (which defaults to straight) */
1145 /* - link for edge */
1146 /* */
1147 /*****************************************************************/
1149 /* first of all, set the `edge' field in each segment -- this is */
1150 /* required in order to compute edge links */
1152 /* note that removing this loop and setting the `edge' field of each */
1153 /* segment directly in the code above slows down execution speed for */
1154 /* some reasons on platforms like the Sun */
1156 TA_Edge edges = axis->edges;
1157 TA_Edge edge_limit = edges + axis->num_edges;
1158 TA_Edge edge;
1161 for (edge = edges; edge < edge_limit; edge++)
1163 seg = edge->first;
1164 if (seg)
1167 seg->edge = edge;
1168 seg = seg->edge_next;
1169 } while (seg != edge->first);
1172 /* now compute each edge properties */
1173 for (edge = edges; edge < edge_limit; edge++)
1175 FT_Int is_round = 0; /* does it contain round segments? */
1176 FT_Int is_straight = 0; /* does it contain straight segments? */
1177 #if 0
1178 FT_Pos ups = 0; /* number of upwards segments */
1179 FT_Pos downs = 0; /* number of downwards segments */
1180 #endif
1183 seg = edge->first;
1187 FT_Bool is_serif;
1190 /* check for roundness of segment */
1191 if (seg->flags & TA_EDGE_ROUND)
1192 is_round++;
1193 else
1194 is_straight++;
1196 #if 0
1197 /* check for segment direction */
1198 if (seg->dir == up_dir)
1199 ups += seg->max_coord - seg->min_coord;
1200 else
1201 downs += seg->max_coord - seg->min_coord;
1202 #endif
1204 /* check for links -- */
1205 /* if seg->serif is set, then seg->link must be ignored */
1206 is_serif = (FT_Bool)(seg->serif
1207 && seg->serif->edge
1208 && seg->serif->edge != edge);
1210 if ((seg->link && seg->link->edge != NULL)
1211 || is_serif)
1213 TA_Edge edge2;
1214 TA_Segment seg2;
1217 edge2 = edge->link;
1218 seg2 = seg->link;
1220 if (is_serif)
1222 seg2 = seg->serif;
1223 edge2 = edge->serif;
1226 if (edge2)
1228 FT_Pos edge_delta;
1229 FT_Pos seg_delta;
1232 edge_delta = edge->fpos - edge2->fpos;
1233 if (edge_delta < 0)
1234 edge_delta = -edge_delta;
1236 seg_delta = seg->pos - seg2->pos;
1237 if (seg_delta < 0)
1238 seg_delta = -seg_delta;
1240 if (seg_delta < edge_delta)
1241 edge2 = seg2->edge;
1243 else
1244 edge2 = seg2->edge;
1246 if (is_serif)
1248 edge->serif = edge2;
1249 edge2->flags |= TA_EDGE_SERIF;
1251 else
1252 edge->link = edge2;
1255 seg = seg->edge_next;
1256 } while (seg != edge->first);
1258 /* set the round/straight flags */
1259 edge->flags = TA_EDGE_NORMAL;
1261 if (is_round > 0
1262 && is_round >= is_straight)
1263 edge->flags |= TA_EDGE_ROUND;
1265 #if 0
1266 /* set the edge's main direction */
1267 edge->dir = TA_DIR_NONE;
1269 if (ups > downs)
1270 edge->dir = (FT_Char)up_dir;
1272 else if (ups < downs)
1273 edge->dir = (FT_Char)-up_dir;
1275 else if (ups == downs)
1276 edge->dir = 0; /* both up and down! */
1277 #endif
1279 /* get rid of serifs if link is set */
1280 /* XXX: this gets rid of many unpleasant artefacts! */
1281 /* example: the `c' in cour.pfa at size 13 */
1283 if (edge->serif && edge->link)
1284 edge->serif = 0;
1288 Exit:
1289 return error;
1293 /* detect segments and edges for given dimension */
1295 FT_Error
1296 ta_latin_hints_detect_features(TA_GlyphHints hints,
1297 TA_Dimension dim)
1299 FT_Error error;
1302 error = ta_latin_hints_compute_segments(hints, dim);
1303 if (!error)
1305 ta_latin_hints_link_segments(hints, dim);
1307 error = ta_latin_hints_compute_edges(hints, dim);
1310 return error;
1314 /* compute all edges which lie within blue zones */
1316 void
1317 ta_latin_hints_compute_blue_edges(TA_GlyphHints hints,
1318 TA_LatinMetrics metrics)
1320 TA_AxisHints axis = &hints->axis[TA_DIMENSION_VERT];
1322 TA_Edge edge = axis->edges;
1323 TA_Edge edge_limit = edge + axis->num_edges;
1325 TA_LatinAxis latin = &metrics->axis[TA_DIMENSION_VERT];
1326 FT_Fixed scale = latin->scale;
1329 /* compute which blue zones are active, */
1330 /* i.e. have their scaled size < 3/4 pixels */
1332 /* for each horizontal edge search the blue zone which is closest */
1333 for (; edge < edge_limit; edge++)
1335 FT_Int bb;
1336 TA_Width best_blue = NULL;
1337 FT_Pos best_dist; /* initial threshold */
1339 FT_UInt best_blue_idx = 0;
1340 FT_Bool best_blue_is_shoot = 0;
1343 /* compute the initial threshold as a fraction of the EM size */
1344 /* (the value 40 is heuristic) */
1345 best_dist = FT_MulFix(metrics->units_per_em / 40, scale);
1347 /* assure a minimum distance of 0.5px */
1348 if (best_dist > 64 / 2)
1349 best_dist = 64 / 2;
1351 for (bb = 0; bb < TA_LATIN_BLUE_MAX; bb++)
1353 TA_LatinBlue blue = latin->blues + bb;
1354 FT_Bool is_top_blue, is_major_dir;
1357 /* skip inactive blue zones (i.e., those that are too large) */
1358 if (!(blue->flags & TA_LATIN_BLUE_ACTIVE))
1359 continue;
1361 /* if it is a top zone, check for right edges -- */
1362 /* if it is a bottom zone, check for left edges */
1363 is_top_blue = (FT_Byte)((blue->flags & TA_LATIN_BLUE_TOP) != 0);
1364 is_major_dir = FT_BOOL(edge->dir == axis->major_dir);
1366 /* if it is a top zone, the edge must be against the major */
1367 /* direction; if it is a bottom zone, it must be in the major */
1368 /* direction */
1369 if (is_top_blue ^ is_major_dir)
1371 FT_Pos dist;
1374 /* first of all, compare it to the reference position */
1375 dist = edge->fpos - blue->ref.org;
1376 if (dist < 0)
1377 dist = -dist;
1379 dist = FT_MulFix(dist, scale);
1380 if (dist < best_dist)
1382 best_dist = dist;
1383 best_blue = &blue->ref;
1385 best_blue_idx = bb;
1386 best_blue_is_shoot = 0;
1389 /* now compare it to the overshoot position and check whether */
1390 /* the edge is rounded, and whether the edge is over the */
1391 /* reference position of a top zone, or under the reference */
1392 /* position of a bottom zone */
1393 if (edge->flags & TA_EDGE_ROUND
1394 && dist != 0)
1396 FT_Bool is_under_ref = FT_BOOL(edge->fpos < blue->ref.org);
1399 if (is_top_blue ^ is_under_ref)
1401 dist = edge->fpos - blue->shoot.org;
1402 if (dist < 0)
1403 dist = -dist;
1405 dist = FT_MulFix(dist, scale);
1406 if (dist < best_dist)
1408 best_dist = dist;
1409 best_blue = &blue->shoot;
1411 best_blue_idx = bb;
1412 best_blue_is_shoot = 1;
1419 if (best_blue)
1421 edge->blue_edge = best_blue;
1422 edge->best_blue_idx = best_blue_idx;
1423 edge->best_blue_is_shoot = best_blue_is_shoot;
1429 /* initalize hinting engine */
1431 static FT_Error
1432 ta_latin_hints_init(TA_GlyphHints hints,
1433 TA_LatinMetrics metrics)
1435 FT_Render_Mode mode;
1436 FT_UInt32 scaler_flags, other_flags;
1437 FT_Face face = metrics->root.scaler.face;
1440 ta_glyph_hints_rescale(hints, (TA_ScriptMetrics)metrics);
1442 /* correct x_scale and y_scale if needed, since they may have */
1443 /* been modified by `ta_latin_metrics_scale_dim' above */
1444 hints->x_scale = metrics->axis[TA_DIMENSION_HORZ].scale;
1445 hints->x_delta = metrics->axis[TA_DIMENSION_HORZ].delta;
1446 hints->y_scale = metrics->axis[TA_DIMENSION_VERT].scale;
1447 hints->y_delta = metrics->axis[TA_DIMENSION_VERT].delta;
1449 /* compute flags depending on render mode, etc. */
1450 mode = metrics->root.scaler.render_mode;
1452 #if 0 /* #ifdef TA_CONFIG_OPTION_USE_WARPER */
1453 if (mode == FT_RENDER_MODE_LCD
1454 || mode == FT_RENDER_MODE_LCD_V)
1455 metrics->root.scaler.render_mode =
1456 mode = FT_RENDER_MODE_NORMAL;
1457 #endif
1459 scaler_flags = hints->scaler_flags;
1460 other_flags = 0;
1462 /* we snap the width of vertical stems for the monochrome */
1463 /* and horizontal LCD rendering targets only */
1464 if (mode == FT_RENDER_MODE_MONO
1465 || mode == FT_RENDER_MODE_LCD)
1466 other_flags |= TA_LATIN_HINTS_HORZ_SNAP;
1468 /* we snap the width of horizontal stems for the monochrome */
1469 /* and vertical LCD rendering targets only */
1470 if (mode == FT_RENDER_MODE_MONO
1471 || mode == FT_RENDER_MODE_LCD_V)
1472 other_flags |= TA_LATIN_HINTS_VERT_SNAP;
1474 /* we adjust stems to full pixels only if we don't use the `light' mode */
1475 if (mode != FT_RENDER_MODE_LIGHT)
1476 other_flags |= TA_LATIN_HINTS_STEM_ADJUST;
1478 if (mode == FT_RENDER_MODE_MONO)
1479 other_flags |= TA_LATIN_HINTS_MONO;
1481 /* in `light' hinting mode we disable horizontal hinting completely; */
1482 /* we also do it if the face is italic */
1483 if (mode == FT_RENDER_MODE_LIGHT
1484 || (face->style_flags & FT_STYLE_FLAG_ITALIC) != 0)
1485 scaler_flags |= TA_SCALER_FLAG_NO_HORIZONTAL;
1487 hints->scaler_flags = scaler_flags;
1488 hints->other_flags = other_flags;
1490 return FT_Err_Ok;
1494 /* snap a given width in scaled coordinates to */
1495 /* one of the current standard widths */
1497 static FT_Pos
1498 ta_latin_snap_width(TA_Width widths,
1499 FT_Int count,
1500 FT_Pos width)
1502 int n;
1503 FT_Pos best = 64 + 32 + 2;
1504 FT_Pos reference = width;
1505 FT_Pos scaled;
1508 for (n = 0; n < count; n++)
1510 FT_Pos w;
1511 FT_Pos dist;
1514 w = widths[n].cur;
1515 dist = width - w;
1516 if (dist < 0)
1517 dist = -dist;
1518 if (dist < best)
1520 best = dist;
1521 reference = w;
1525 scaled = TA_PIX_ROUND(reference);
1527 if (width >= reference)
1529 if (width < scaled + 48)
1530 width = reference;
1532 else
1534 if (width > scaled - 48)
1535 width = reference;
1538 return width;
1542 /* compute the snapped width of a given stem, ignoring very thin ones */
1544 /* there is a lot of voodoo in this function; changing the hard-coded */
1545 /* parameters influence the whole hinting process */
1547 static FT_Pos
1548 ta_latin_compute_stem_width(TA_GlyphHints hints,
1549 TA_Dimension dim,
1550 FT_Pos width,
1551 FT_Byte base_flags,
1552 FT_Byte stem_flags)
1554 TA_LatinMetrics metrics = (TA_LatinMetrics) hints->metrics;
1555 TA_LatinAxis axis = &metrics->axis[dim];
1557 FT_Pos dist = width;
1558 FT_Int sign = 0;
1559 FT_Int vertical = (dim == TA_DIMENSION_VERT);
1562 if (!TA_LATIN_HINTS_DO_STEM_ADJUST(hints)
1563 || axis->extra_light)
1564 return width;
1566 if (dist < 0)
1568 dist = -width;
1569 sign = 1;
1572 if ((vertical && !TA_LATIN_HINTS_DO_VERT_SNAP(hints))
1573 || (!vertical && !TA_LATIN_HINTS_DO_HORZ_SNAP(hints)))
1575 /* smooth hinting process: very lightly quantize the stem width */
1577 /* leave the widths of serifs alone */
1578 if ((stem_flags & TA_EDGE_SERIF)
1579 && vertical
1580 && (dist < 3 * 64))
1581 goto Done_Width;
1582 else if (base_flags & TA_EDGE_ROUND)
1584 if (dist < 80)
1585 dist = 64;
1587 else if (dist < 56)
1588 dist = 56;
1590 if (axis->width_count > 0)
1592 FT_Pos delta;
1595 /* compare to standard width */
1596 delta = dist - axis->widths[0].cur;
1598 if (delta < 0)
1599 delta = -delta;
1601 if (delta < 40)
1603 dist = axis->widths[0].cur;
1604 if (dist < 48)
1605 dist = 48;
1607 goto Done_Width;
1610 if (dist < 3 * 64)
1612 delta = dist & 63;
1613 dist &= -64;
1615 if (delta < 10)
1616 dist += delta;
1617 else if (delta < 32)
1618 dist += 10;
1619 else if (delta < 54)
1620 dist += 54;
1621 else
1622 dist += delta;
1624 else
1625 dist = (dist + 32) & ~63;
1628 else
1630 /* strong hinting process: snap the stem width to integer pixels */
1632 FT_Pos org_dist = dist;
1635 dist = ta_latin_snap_width(axis->widths, axis->width_count, dist);
1637 if (vertical)
1639 /* in the case of vertical hinting, */
1640 /* always round the stem heights to integer pixels */
1642 if (dist >= 64)
1643 dist = (dist + 16) & ~63;
1644 else
1645 dist = 64;
1647 else
1649 if (TA_LATIN_HINTS_DO_MONO(hints))
1651 /* monochrome horizontal hinting: */
1652 /* snap widths to integer pixels with a different threshold */
1654 if (dist < 64)
1655 dist = 64;
1656 else
1657 dist = (dist + 32) & ~63;
1659 else
1661 /* for horizontal anti-aliased hinting, we adopt a more subtle */
1662 /* approach: we strengthen small stems, round stems whose size */
1663 /* is between 1 and 2 pixels to an integer, otherwise nothing */
1665 if (dist < 48)
1666 dist = (dist + 64) >> 1;
1668 else if (dist < 128)
1670 /* we only round to an integer width if the corresponding */
1671 /* distortion is less than 1/4 pixel -- otherwise, this */
1672 /* makes everything worse since the diagonals, which are */
1673 /* not hinted, appear a lot bolder or thinner than the */
1674 /* vertical stems */
1676 FT_Pos delta;
1679 dist = (dist + 22) & ~63;
1680 delta = dist - org_dist;
1681 if (delta < 0)
1682 delta = -delta;
1684 if (delta >= 16)
1686 dist = org_dist;
1687 if (dist < 48)
1688 dist = (dist + 64) >> 1;
1691 else
1692 /* round otherwise to prevent color fringes in LCD mode */
1693 dist = (dist + 32) & ~63;
1698 Done_Width:
1699 if (sign)
1700 dist = -dist;
1702 return dist;
1706 /* align one stem edge relative to the previous stem edge */
1708 static void
1709 ta_latin_align_linked_edge(TA_GlyphHints hints,
1710 TA_Dimension dim,
1711 TA_Edge base_edge,
1712 TA_Edge stem_edge)
1714 FT_Pos dist = stem_edge->opos - base_edge->opos;
1716 FT_Pos fitted_width = ta_latin_compute_stem_width(
1717 hints, dim, dist,
1718 base_edge->flags,
1719 stem_edge->flags);
1722 stem_edge->pos = base_edge->pos + fitted_width;
1724 TA_LOG((" LINK: edge %d (opos=%.2f) linked to %.2f,"
1725 " dist was %.2f, now %.2f\n",
1726 stem_edge - hints->axis[dim].edges, stem_edge->opos / 64.0,
1727 stem_edge->pos / 64.0, dist / 64.0, fitted_width / 64.0));
1729 if (hints->recorder)
1730 hints->recorder(ta_link, hints, dim,
1731 base_edge, stem_edge, NULL, NULL, NULL);
1735 /* shift the coordinates of the `serif' edge by the same amount */
1736 /* as the corresponding `base' edge has been moved already */
1738 static void
1739 ta_latin_align_serif_edge(TA_GlyphHints hints,
1740 TA_Edge base,
1741 TA_Edge serif)
1743 FT_UNUSED(hints);
1745 serif->pos = base->pos + (serif->opos - base->opos);
1749 /* the main grid-fitting routine */
1751 void
1752 ta_latin_hint_edges(TA_GlyphHints hints,
1753 TA_Dimension dim)
1755 TA_AxisHints axis = &hints->axis[dim];
1757 TA_Edge edges = axis->edges;
1758 TA_Edge edge_limit = edges + axis->num_edges;
1759 FT_PtrDist n_edges;
1760 TA_Edge edge;
1762 TA_Edge anchor = NULL;
1763 FT_Int has_serifs = 0;
1766 TA_LOG(("%s edge hinting\n", dim == TA_DIMENSION_VERT ? "horizontal"
1767 : "vertical"));
1769 /* we begin by aligning all stems relative to the blue zone if needed -- */
1770 /* that's only for horizontal edges */
1772 if (dim == TA_DIMENSION_VERT
1773 && TA_HINTS_DO_BLUES(hints))
1775 for (edge = edges; edge < edge_limit; edge++)
1777 TA_Width blue;
1778 TA_Edge edge1, edge2; /* these edges form the stem to check */
1781 if (edge->flags & TA_EDGE_DONE)
1782 continue;
1784 blue = edge->blue_edge;
1785 edge1 = NULL;
1786 edge2 = edge->link;
1788 if (blue)
1789 edge1 = edge;
1791 /* flip edges if the other stem is aligned to a blue zone */
1792 else if (edge2 && edge2->blue_edge)
1794 blue = edge2->blue_edge;
1795 edge1 = edge2;
1796 edge2 = edge;
1799 if (!edge1)
1800 continue;
1802 if (!anchor)
1803 TA_LOG((" BLUE_ANCHOR: edge %d (opos=%.2f) snapped to %.2f,"
1804 " was %.2f (anchor=edge %d)\n",
1805 edge1 - edges, edge1->opos / 64.0, blue->fit / 64.0,
1806 edge1->pos / 64.0, edge - edges));
1807 else
1808 TA_LOG((" BLUE: edge %d (opos=%.2f) snapped to %.2f, was %.2f\n",
1809 edge1 - edges, edge1->opos / 64.0, blue->fit / 64.0,
1810 edge1->pos / 64.0));
1812 edge1->pos = blue->fit;
1813 edge1->flags |= TA_EDGE_DONE;
1815 if (hints->recorder)
1817 if (!anchor)
1818 hints->recorder(ta_blue_anchor, hints, dim,
1819 edge1, edge, NULL, NULL, NULL);
1820 else
1821 hints->recorder(ta_blue, hints, dim,
1822 edge1, NULL, NULL, NULL, NULL);
1825 if (edge2 && !edge2->blue_edge)
1827 ta_latin_align_linked_edge(hints, dim, edge1, edge2);
1828 edge2->flags |= TA_EDGE_DONE;
1831 if (!anchor)
1832 anchor = edge;
1836 /* now we align all other stem edges, */
1837 /* trying to maintain the relative order of stems in the glyph */
1838 for (edge = edges; edge < edge_limit; edge++)
1840 TA_Edge edge2;
1843 if (edge->flags & TA_EDGE_DONE)
1844 continue;
1846 /* skip all non-stem edges */
1847 edge2 = edge->link;
1848 if (!edge2)
1850 has_serifs++;
1851 continue;
1854 /* now align the stem */
1856 /* this should not happen, but it's better to be safe */
1857 if (edge2->blue_edge)
1859 TA_LOG((" ASSERTION FAILED for edge %d\n", edge2-edges));
1861 ta_latin_align_linked_edge(hints, dim, edge2, edge);
1862 edge->flags |= TA_EDGE_DONE;
1863 continue;
1866 if (!anchor)
1868 /* if we reach this if clause, no stem has been aligned yet */
1870 FT_Pos org_len, org_center, cur_len;
1871 FT_Pos cur_pos1, error1, error2, u_off, d_off;
1874 org_len = edge2->opos - edge->opos;
1875 cur_len = ta_latin_compute_stem_width(hints, dim, org_len,
1876 edge->flags, edge2->flags);
1878 /* some voodoo to specially round edges for small stem widths; */
1879 /* the idea is to align the center of a stem, */
1880 /* then shifting the stem edges to suitable positions */
1881 if (cur_len <= 64)
1883 /* width <= 1px */
1884 u_off = 32;
1885 d_off = 32;
1887 else
1889 /* 1px < width < 1.5px */
1890 u_off = 38;
1891 d_off = 26;
1894 if (cur_len < 96)
1896 org_center = edge->opos + (org_len >> 1);
1897 cur_pos1 = TA_PIX_ROUND(org_center);
1899 error1 = org_center - (cur_pos1 - u_off);
1900 if (error1 < 0)
1901 error1 = -error1;
1903 error2 = org_center - (cur_pos1 + d_off);
1904 if (error2 < 0)
1905 error2 = -error2;
1907 if (error1 < error2)
1908 cur_pos1 -= u_off;
1909 else
1910 cur_pos1 += d_off;
1912 edge->pos = cur_pos1 - cur_len / 2;
1913 edge2->pos = edge->pos + cur_len;
1915 else
1916 edge->pos = TA_PIX_ROUND(edge->opos);
1918 anchor = edge;
1919 edge->flags |= TA_EDGE_DONE;
1921 TA_LOG((" ANCHOR: edge %d (opos=%.2f) and %d (opos=%.2f)"
1922 " snapped to %.2f and %.2f\n",
1923 edge - edges, edge->opos / 64.0,
1924 edge2 - edges, edge2->opos / 64.0,
1925 edge->pos / 64.0, edge2->pos / 64.0));
1927 if (hints->recorder)
1928 hints->recorder(ta_anchor, hints, dim,
1929 edge, edge2, NULL, NULL, NULL);
1931 ta_latin_align_linked_edge(hints, dim, edge, edge2);
1933 else
1935 FT_Pos org_pos, org_len, org_center, cur_len;
1936 FT_Pos cur_pos1, cur_pos2, delta1, delta2;
1939 org_pos = anchor->pos + (edge->opos - anchor->opos);
1940 org_len = edge2->opos - edge->opos;
1941 org_center = org_pos + (org_len >> 1);
1943 cur_len = ta_latin_compute_stem_width(hints, dim, org_len,
1944 edge->flags, edge2->flags);
1946 if (edge2->flags & TA_EDGE_DONE)
1948 TA_LOG((" ADJUST: edge %d (pos=%.2f) moved to %.2f\n",
1949 edge - edges, edge->pos / 64.0,
1950 (edge2->pos - cur_len) / 64.0));
1952 edge->pos = edge2->pos - cur_len;
1954 if (hints->recorder)
1956 TA_Edge bound = NULL;
1959 if (edge > edges)
1960 bound = &edge[-1];
1962 hints->recorder(ta_adjust, hints, dim,
1963 edge, edge2, NULL, bound, NULL);
1967 else if (cur_len < 96)
1969 FT_Pos u_off, d_off;
1972 cur_pos1 = TA_PIX_ROUND(org_center);
1974 if (cur_len <= 64)
1976 u_off = 32;
1977 d_off = 32;
1979 else
1981 u_off = 38;
1982 d_off = 26;
1985 delta1 = org_center - (cur_pos1 - u_off);
1986 if (delta1 < 0)
1987 delta1 = -delta1;
1989 delta2 = org_center - (cur_pos1 + d_off);
1990 if (delta2 < 0)
1991 delta2 = -delta2;
1993 if (delta1 < delta2)
1994 cur_pos1 -= u_off;
1995 else
1996 cur_pos1 += d_off;
1998 edge->pos = cur_pos1 - cur_len / 2;
1999 edge2->pos = cur_pos1 + cur_len / 2;
2001 TA_LOG((" STEM: edge %d (opos=%.2f) linked to %d (opos=%.2f)"
2002 " snapped to %.2f and %.2f\n",
2003 edge - edges, edge->opos / 64.0,
2004 edge2 - edges, edge2->opos / 64.0,
2005 edge->pos / 64.0, edge2->pos / 64.0));
2007 if (hints->recorder)
2009 TA_Edge bound = NULL;
2012 if (edge > edges)
2013 bound = &edge[-1];
2015 hints->recorder(ta_stem, hints, dim,
2016 edge, edge2, NULL, bound, NULL);
2020 else
2022 org_pos = anchor->pos + (edge->opos - anchor->opos);
2023 org_len = edge2->opos - edge->opos;
2024 org_center = org_pos + (org_len >> 1);
2026 cur_len = ta_latin_compute_stem_width(hints, dim, org_len,
2027 edge->flags, edge2->flags);
2029 cur_pos1 = TA_PIX_ROUND(org_pos);
2030 delta1 = cur_pos1 + (cur_len >> 1) - org_center;
2031 if (delta1 < 0)
2032 delta1 = -delta1;
2034 cur_pos2 = TA_PIX_ROUND(org_pos + org_len) - cur_len;
2035 delta2 = cur_pos2 + (cur_len >> 1) - org_center;
2036 if (delta2 < 0)
2037 delta2 = -delta2;
2039 edge->pos = (delta1 < delta2) ? cur_pos1 : cur_pos2;
2040 edge2->pos = edge->pos + cur_len;
2042 TA_LOG((" STEM: edge %d (opos=%.2f) linked to %d (opos=%.2f)"
2043 " snapped to %.2f and %.2f\n",
2044 edge - edges, edge->opos / 64.0,
2045 edge2 - edges, edge2->opos / 64.0,
2046 edge->pos / 64.0, edge2->pos / 64.0));
2048 if (hints->recorder)
2050 TA_Edge bound = NULL;
2053 if (edge > edges)
2054 bound = &edge[-1];
2056 hints->recorder(ta_stem, hints, dim,
2057 edge, edge2, NULL, bound, NULL);
2061 edge->flags |= TA_EDGE_DONE;
2062 edge2->flags |= TA_EDGE_DONE;
2064 if (edge > edges
2065 && edge->pos < edge[-1].pos)
2067 TA_LOG((" BOUND: edge %d (pos=%.2f) moved to %.2f\n",
2068 edge - edges, edge->pos / 64.0, edge[-1].pos / 64.0));
2070 edge->pos = edge[-1].pos;
2072 if (hints->recorder)
2073 hints->recorder(ta_bound, hints, dim,
2074 edge, &edge[-1], NULL, NULL, NULL);
2079 /* make sure that lowercase m's maintain their symmetry */
2081 /* In general, lowercase m's have six vertical edges if they are sans */
2082 /* serif, or twelve if they are with serifs. This implementation is */
2083 /* based on that assumption, and seems to work very well with most */
2084 /* faces. However, if for a certain face this assumption is not */
2085 /* true, the m is just rendered like before. In addition, any stem */
2086 /* correction will only be applied to symmetrical glyphs (even if the */
2087 /* glyph is not an m), so the potential for unwanted distortion is */
2088 /* relatively low. */
2090 /* we don't handle horizontal edges since we can't easily assure that */
2091 /* the third (lowest) stem aligns with the base line; it might end up */
2092 /* one pixel higher or lower */
2094 n_edges = edge_limit - edges;
2095 if (dim == TA_DIMENSION_HORZ
2096 && (n_edges == 6 || n_edges == 12))
2098 TA_Edge edge1, edge2, edge3;
2099 FT_Pos dist1, dist2, span, delta;
2102 if (n_edges == 6)
2104 edge1 = edges;
2105 edge2 = edges + 2;
2106 edge3 = edges + 4;
2108 else
2110 edge1 = edges + 1;
2111 edge2 = edges + 5;
2112 edge3 = edges + 9;
2115 dist1 = edge2->opos - edge1->opos;
2116 dist2 = edge3->opos - edge2->opos;
2118 span = dist1 - dist2;
2119 if (span < 0)
2120 span = -span;
2122 if (span < 8)
2124 delta = edge3->pos - (2 * edge2->pos - edge1->pos);
2125 edge3->pos -= delta;
2126 if (edge3->link)
2127 edge3->link->pos -= delta;
2129 /* move the serifs along with the stem */
2130 if (n_edges == 12)
2132 (edges + 8)->pos -= delta;
2133 (edges + 11)->pos -= delta;
2136 edge3->flags |= TA_EDGE_DONE;
2137 if (edge3->link)
2138 edge3->link->flags |= TA_EDGE_DONE;
2142 if (has_serifs || !anchor)
2144 /* now hint the remaining edges (serifs and single) */
2145 /* in order to complete our processing */
2146 for (edge = edges; edge < edge_limit; edge++)
2148 TA_Edge lower_bound = NULL;
2149 TA_Edge upper_bound = NULL;
2151 FT_Pos delta;
2154 if (edge->flags & TA_EDGE_DONE)
2155 continue;
2157 delta = 1000;
2159 if (edge->serif)
2161 delta = edge->serif->opos - edge->opos;
2162 if (delta < 0)
2163 delta = -delta;
2166 if (edge > edges)
2167 lower_bound = &edge[-1];
2169 if (edge + 1 < edge_limit
2170 && edge[1].flags & TA_EDGE_DONE)
2171 upper_bound = &edge[1];
2174 if (delta < 64 + 16)
2176 ta_latin_align_serif_edge(hints, edge->serif, edge);
2178 TA_LOG((" SERIF: edge %d (opos=%.2f) serif to %d (opos=%.2f)"
2179 " aligned to %.2f\n",
2180 edge - edges, edge->opos / 64.0,
2181 edge->serif - edges, edge->serif->opos / 64.0,
2182 edge->pos / 64.0));
2184 if (hints->recorder)
2185 hints->recorder(ta_serif, hints, dim,
2186 edge, NULL, NULL, lower_bound, upper_bound);
2188 else if (!anchor)
2190 edge->pos = TA_PIX_ROUND(edge->opos);
2191 anchor = edge;
2193 TA_LOG((" SERIF_ANCHOR: edge %d (opos=%.2f) snapped to %.2f\n",
2194 edge - edges, edge->opos / 64.0, edge->pos / 64.0));
2196 if (hints->recorder)
2197 hints->recorder(ta_serif_anchor, hints, dim,
2198 edge, NULL, NULL, lower_bound, upper_bound);
2200 else
2202 TA_Edge before, after;
2205 for (before = edge - 1; before >= edges; before--)
2206 if (before->flags & TA_EDGE_DONE)
2207 break;
2209 for (after = edge + 1; after < edge_limit; after++)
2210 if (after->flags & TA_EDGE_DONE)
2211 break;
2213 if (before >= edges && before < edge
2214 && after < edge_limit && after > edge)
2216 if (after->opos == before->opos)
2217 edge->pos = before->pos;
2218 else
2219 edge->pos = before->pos + FT_MulDiv(edge->opos - before->opos,
2220 after->pos - before->pos,
2221 after->opos - before->opos);
2223 TA_LOG((" SERIF_LINK1: edge %d (opos=%.2f) snapped to %.2f"
2224 " from %d (opos=%.2f)\n",
2225 edge - edges, edge->opos / 64.0,
2226 edge->pos / 64.0,
2227 before - edges, before->opos / 64.0));
2229 if (hints->recorder)
2230 hints->recorder(ta_serif_link1, hints, dim,
2231 edge, before, after, lower_bound, upper_bound);
2233 else
2235 edge->pos = anchor->pos + ((edge->opos - anchor->opos + 16) & ~31);
2237 TA_LOG((" SERIF_LINK2: edge %d (opos=%.2f) snapped to %.2f\n",
2238 edge - edges, edge->opos / 64.0, edge->pos / 64.0));
2240 if (hints->recorder)
2241 hints->recorder(ta_serif_link2, hints, dim,
2242 edge, NULL, NULL, lower_bound, upper_bound);
2246 edge->flags |= TA_EDGE_DONE;
2248 if (edge > edges
2249 && edge->pos < edge[-1].pos)
2251 TA_LOG((" BOUND: edge %d (pos=%.2f) moved to %.2f\n",
2252 edge - edges, edge->pos / 64.0, edge[-1].pos / 64.0));
2254 edge->pos = edge[-1].pos;
2256 if (hints->recorder)
2257 hints->recorder(ta_bound, hints, dim,
2258 edge, &edge[-1], NULL, NULL, NULL);
2261 if (edge + 1 < edge_limit
2262 && edge[1].flags & TA_EDGE_DONE
2263 && edge->pos > edge[1].pos)
2265 TA_LOG((" BOUND: edge %d (pos=%.2f) moved to %.2f\n",
2266 edge - edges, edge->pos / 64.0, edge[1].pos / 64.0));
2268 edge->pos = edge[1].pos;
2270 if (hints->recorder)
2271 hints->recorder(ta_bound, hints, dim,
2272 edge, &edge[1], NULL, NULL, NULL);
2277 TA_LOG(("\n"));
2281 /* apply the complete hinting algorithm to a latin glyph */
2283 static FT_Error
2284 ta_latin_hints_apply(TA_GlyphHints hints,
2285 FT_Outline* outline,
2286 TA_LatinMetrics metrics)
2288 FT_Error error;
2289 int dim;
2292 error = ta_glyph_hints_reload(hints, outline);
2293 if (error)
2294 goto Exit;
2296 /* analyze glyph outline */
2297 #ifdef TA_CONFIG_OPTION_USE_WARPER
2298 if (metrics->root.scaler.render_mode == FT_RENDER_MODE_LIGHT
2299 || TA_HINTS_DO_HORIZONTAL(hints))
2300 #else
2301 if (TA_HINTS_DO_HORIZONTAL(hints))
2302 #endif
2304 error = ta_latin_hints_detect_features(hints, TA_DIMENSION_HORZ);
2305 if (error)
2306 goto Exit;
2309 if (TA_HINTS_DO_VERTICAL(hints))
2311 error = ta_latin_hints_detect_features(hints, TA_DIMENSION_VERT);
2312 if (error)
2313 goto Exit;
2315 ta_latin_hints_compute_blue_edges(hints, metrics);
2318 /* grid-fit the outline */
2319 for (dim = 0; dim < TA_DIMENSION_MAX; dim++)
2321 #ifdef TA_CONFIG_OPTION_USE_WARPER
2322 if (dim == TA_DIMENSION_HORZ
2323 && metrics->root.scaler.render_mode == FT_RENDER_MODE_LIGHT)
2325 TA_WarperRec warper;
2326 FT_Fixed scale;
2327 FT_Pos delta;
2330 ta_warper_compute(&warper, hints, (TA_Dimension)dim, &scale, &delta);
2331 ta_glyph_hints_scale_dim(hints, (TA_Dimension)dim, scale, delta);
2333 continue;
2335 #endif
2337 if ((dim == TA_DIMENSION_HORZ && TA_HINTS_DO_HORIZONTAL(hints))
2338 || (dim == TA_DIMENSION_VERT && TA_HINTS_DO_VERTICAL(hints)))
2340 ta_latin_hint_edges(hints, (TA_Dimension)dim);
2341 ta_glyph_hints_align_edge_points(hints, (TA_Dimension)dim);
2342 ta_glyph_hints_align_strong_points(hints, (TA_Dimension)dim);
2343 ta_glyph_hints_align_weak_points(hints, (TA_Dimension)dim);
2346 ta_glyph_hints_save(hints, outline);
2348 Exit:
2349 return error;
2353 /* XXX: this should probably fine tuned to differentiate better between */
2354 /* scripts... */
2356 static const TA_Script_UniRangeRec ta_latin_uniranges[] =
2358 TA_UNIRANGE_REC(0x0020UL, 0x007FUL), /* Basic Latin (no control chars) */
2359 TA_UNIRANGE_REC(0x00A0UL, 0x00FFUL), /* Latin-1 Supplement (no control chars) */
2360 TA_UNIRANGE_REC(0x0100UL, 0x017FUL), /* Latin Extended-A */
2361 TA_UNIRANGE_REC(0x0180UL, 0x024FUL), /* Latin Extended-B */
2362 TA_UNIRANGE_REC(0x0250UL, 0x02AFUL), /* IPA Extensions */
2363 TA_UNIRANGE_REC(0x02B0UL, 0x02FFUL), /* Spacing Modifier Letters */
2364 TA_UNIRANGE_REC(0x0300UL, 0x036FUL), /* Combining Diacritical Marks */
2365 TA_UNIRANGE_REC(0x0370UL, 0x03FFUL), /* Greek and Coptic */
2366 TA_UNIRANGE_REC(0x0400UL, 0x04FFUL), /* Cyrillic */
2367 TA_UNIRANGE_REC(0x0500UL, 0x052FUL), /* Cyrillic Supplement */
2368 TA_UNIRANGE_REC(0x1D00UL, 0x1D7FUL), /* Phonetic Extensions */
2369 TA_UNIRANGE_REC(0x1D80UL, 0x1DBFUL), /* Phonetic Extensions Supplement */
2370 TA_UNIRANGE_REC(0x1DC0UL, 0x1DFFUL), /* Combining Diacritical Marks Supplement */
2371 TA_UNIRANGE_REC(0x1E00UL, 0x1EFFUL), /* Latin Extended Additional */
2372 TA_UNIRANGE_REC(0x1F00UL, 0x1FFFUL), /* Greek Extended */
2373 TA_UNIRANGE_REC(0x2000UL, 0x206FUL), /* General Punctuation */
2374 TA_UNIRANGE_REC(0x2070UL, 0x209FUL), /* Superscripts and Subscripts */
2375 TA_UNIRANGE_REC(0x20A0UL, 0x20CFUL), /* Currency Symbols */
2376 TA_UNIRANGE_REC(0x2150UL, 0x218FUL), /* Number Forms */
2377 TA_UNIRANGE_REC(0x2460UL, 0x24FFUL), /* Enclosed Alphanumerics */
2378 TA_UNIRANGE_REC(0x2C60UL, 0x2C7FUL), /* Latin Extended-C */
2379 TA_UNIRANGE_REC(0x2DE0UL, 0x2DFFUL), /* Cyrillic Extended-A */
2380 TA_UNIRANGE_REC(0xA640UL, 0xA69FUL), /* Cyrillic Extended-B */
2381 TA_UNIRANGE_REC(0xA720UL, 0xA7FFUL), /* Latin Extended-D */
2382 TA_UNIRANGE_REC(0xFB00UL, 0xFB06UL), /* Alphab. Present. Forms (Latin Ligs) */
2383 TA_UNIRANGE_REC(0x1D400UL, 0x1D7FFUL), /* Mathematical Alphanumeric Symbols */
2384 TA_UNIRANGE_REC(0UL, 0UL)
2388 const TA_ScriptClassRec ta_latin_script_class =
2390 TA_SCRIPT_LATIN,
2391 ta_latin_uniranges,
2393 sizeof (TA_LatinMetricsRec),
2395 (TA_Script_InitMetricsFunc)ta_latin_metrics_init,
2396 (TA_Script_ScaleMetricsFunc)ta_latin_metrics_scale,
2397 (TA_Script_DoneMetricsFunc)NULL,
2399 (TA_Script_InitHintsFunc)ta_latin_hints_init,
2400 (TA_Script_ApplyHintsFunc)ta_latin_hints_apply
2403 /* end of talatin.c */