4 * Copyright (C) 2011-2019 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> */
23 #include FT_ADVANCES_H
24 #include FT_TRUETYPE_TABLES_H
31 #ifdef TA_CONFIG_OPTION_USE_WARPER
35 #include <numberset.h>
38 /* needed for computation of round vs. flat segments */
39 #define FLAT_THRESHOLD(x) (x / 14)
42 /* find segments and links, compute all stem widths, and initialize */
43 /* standard width and height for the glyph with given charcode */
46 ta_latin_metrics_init_widths(TA_LatinMetrics metrics
,
50 /* scan the array of segments in each direction */
51 TA_GlyphHintsRec hints
[1];
55 "latin standard widths computation (style `%s')\n"
56 "=====================================================\n"
58 ta_style_names
[metrics
->root
.style_class
->style
]));
60 ta_glyph_hints_init(hints
);
62 metrics
->axis
[TA_DIMENSION_HORZ
].width_count
= 0;
63 metrics
->axis
[TA_DIMENSION_VERT
].width_count
= 0;
69 TA_LatinMetricsRec dummy
[1];
70 TA_Scaler scaler
= &dummy
->root
.scaler
;
72 TA_StyleClass style_class
= metrics
->root
.style_class
;
73 TA_ScriptClass script_class
= ta_script_classes
[style_class
->script
];
75 FONT
* font
= metrics
->root
.globals
->font
;
85 /* if the user provides horizontal stem widths, */
86 /* apply the algorithm only along the horizontal axis */
87 TA_control_set_stem_widths(metrics
, font
);
88 if (metrics
->axis
[TA_DIMENSION_VERT
].width_count
)
90 TA_LatinAxis axis
= &metrics
->axis
[TA_DIMENSION_VERT
];
93 dim_max
= TA_DIMENSION_VERT
;
95 axis
->standard_width
= axis
->widths
[0].org
;
96 axis
->edge_distance_threshold
= axis
->standard_width
/ 5;
97 axis
->extra_light
= 0;
104 TA_LOG_GLOBAL(("horizontal widths (user provided):\n" ));
106 TA_LOG_GLOBAL((" %d (standard)", axis
->standard_width
));
107 for (i
= 1; i
< axis
->width_count
; i
++)
108 TA_LOG_GLOBAL((" %d", axis
->widths
[i
].org
));
110 TA_LOG_GLOBAL(("\n"));
116 dim_max
= TA_DIMENSION_MAX
;
121 p
= script_class
->standard_charstring
;
122 shaper_buf
= ta_shaper_buf_create(face
);
125 * We check a list of standard characters to catch features like
126 * `c2sc' (small caps from caps) that don't contain lowercase letters
127 * by definition, or other features that mainly operate on numerals.
128 * The first match wins.
134 unsigned int num_idx
;
146 GET_UTF8_CHAR(ch
, p_old
);
149 /* reject input that maps to more than a single glyph */
150 p
= ta_shaper_get_cluster(p
, &metrics
->root
, shaper_buf
, &num_idx
);
154 /* otherwise exit loop if we have a result */
155 glyph_index
= ta_shaper_get_elem(&metrics
->root
,
164 ta_shaper_buf_destroy(face
, shaper_buf
);
168 TA_LOG_GLOBAL(("no standard character\n"));
172 TA_LOG_GLOBAL(("standard character: U+%04lX (glyph index %d)\n",
175 error
= FT_Load_Glyph(face
, glyph_index
, FT_LOAD_NO_SCALE
);
176 if (error
|| face
->glyph
->outline
.n_points
<= 0)
179 memset(dummy
, 0, sizeof (TA_LatinMetricsRec
));
181 dummy
->units_per_em
= metrics
->units_per_em
;
183 scaler
->x_scale
= 0x10000L
;
184 scaler
->y_scale
= 0x10000L
;
189 scaler
->render_mode
= FT_RENDER_MODE_NORMAL
;
192 ta_glyph_hints_rescale(hints
, (TA_StyleMetrics
)dummy
);
194 error
= ta_glyph_hints_reload(hints
, &face
->glyph
->outline
);
198 for (dim
= 0; dim
< dim_max
; dim
++)
200 TA_LatinAxis axis
= &metrics
->axis
[dim
];
201 TA_AxisHints axhints
= &hints
->axis
[dim
];
203 TA_Segment seg
, limit
, link
;
204 FT_UInt num_widths
= 0;
207 error
= ta_latin_hints_compute_segments(hints
, (TA_Dimension
)dim
);
212 * We assume that the glyphs selected for the stem width
213 * computation are `featureless' enough so that the linking
214 * algorithm works fine without adjustments of its scoring
217 ta_latin_hints_link_segments(hints
, 0, NULL
, (TA_Dimension
)dim
);
219 seg
= axhints
->segments
;
220 limit
= seg
+ axhints
->num_segments
;
222 for (; seg
< limit
; seg
++)
226 /* we only consider stem segments there! */
234 dist
= seg
->pos
- link
->pos
;
238 if (num_widths
< TA_LATIN_MAX_WIDTHS
)
239 axis
->widths
[num_widths
++].org
= dist
;
243 /* this also replaces multiple almost identical stem widths */
244 /* with a single one (the value 100 is heuristic) */
245 ta_sort_and_quantize_widths(&num_widths
, axis
->widths
,
246 dummy
->units_per_em
/ 100);
247 axis
->width_count
= num_widths
;
251 for (dim
= 0; dim
< dim_max
; dim
++)
253 TA_LatinAxis axis
= &metrics
->axis
[dim
];
257 if (!axis
->width_count
)
259 /* if we have no standard characters, */
260 /* use `fallback-stem-width', if available, */
261 /* or a default width (value 50 is heuristic) */
262 if (dim
== TA_DIMENSION_VERT
&& font
->fallback_stem_width
)
264 stdw
= (FT_Pos
)font
->fallback_stem_width
;
265 TA_LOG_GLOBAL(("using horizontal fallback stem width\n"));
269 stdw
= TA_LATIN_CONSTANT(metrics
, 50);
270 TA_LOG_GLOBAL(("using a default %s stem width\n",
271 dim
== TA_DIMENSION_VERT
? "horizontal"
276 axis
->widths
[0].org
= stdw
;
279 stdw
= axis
->widths
[0].org
;
281 /* let's try 20% of the smallest width */
282 axis
->edge_distance_threshold
= stdw
/ 5;
283 axis
->standard_width
= stdw
;
284 axis
->extra_light
= 0;
291 TA_LOG_GLOBAL(("%s widths:\n",
292 dim
== TA_DIMENSION_VERT
? "horizontal"
295 TA_LOG_GLOBAL((" %d (standard)", axis
->standard_width
));
296 for (i
= 1; i
< axis
->width_count
; i
++)
297 TA_LOG_GLOBAL((" %d", axis
->widths
[i
].org
));
299 TA_LOG_GLOBAL(("\n"));
305 TA_LOG_GLOBAL(("\n"));
307 ta_glyph_hints_done(hints
);
312 ta_latin_sort_blue(FT_UInt count
,
320 /* we sort from bottom to top */
321 for (i
= 1; i
< count
; i
++)
323 for (j
= i
; j
> 0; j
--)
328 if (table
[j
- 1]->flags
& (TA_LATIN_BLUE_TOP
329 | TA_LATIN_BLUE_SUB_TOP
))
330 a
= table
[j
- 1]->ref
.org
;
332 a
= table
[j
- 1]->shoot
.org
;
334 if (table
[j
]->flags
& (TA_LATIN_BLUE_TOP
335 | TA_LATIN_BLUE_SUB_TOP
))
336 b
= table
[j
]->ref
.org
;
338 b
= table
[j
]->shoot
.org
;
344 table
[j
] = table
[j
- 1];
351 /* find all blue zones; flat segments give the reference points, */
352 /* round segments the overshoot positions */
355 ta_latin_metrics_init_blues(TA_LatinMetrics metrics
,
358 FT_Pos flats
[TA_BLUE_STRING_MAX_LEN
];
359 FT_Pos rounds
[TA_BLUE_STRING_MAX_LEN
];
365 TA_LatinAxis axis
= &metrics
->axis
[TA_DIMENSION_VERT
];
368 TA_StyleClass sc
= metrics
->root
.style_class
;
370 TA_Blue_Stringset bss
= sc
->blue_stringset
;
371 const TA_Blue_StringRec
* bs
= &ta_blue_stringsets
[bss
];
373 FT_Pos flat_threshold
= FLAT_THRESHOLD(metrics
->units_per_em
);
378 /* we walk over the blue character strings as specified in the */
379 /* style's entry in the `ta_blue_stringset' array */
381 TA_LOG_GLOBAL(("latin blue zones computation\n"
382 "============================\n"
385 shaper_buf
= ta_shaper_buf_create(face
);
387 for (; bs
->string
!= TA_BLUE_STRING_MAX
; bs
++)
389 const char* p
= &ta_blue_strings
[bs
->string
];
398 FT_Bool have_flag
= 0;
401 TA_LOG_GLOBAL(("blue zone %d", axis
->blue_count
));
405 TA_LOG_GLOBAL((" ("));
407 if (TA_LATIN_IS_TOP_BLUE(bs
))
409 TA_LOG_GLOBAL(("top"));
412 else if (TA_LATIN_IS_SUB_TOP_BLUE(bs
))
414 TA_LOG_GLOBAL(("sub top"));
418 if (TA_LATIN_IS_NEUTRAL_BLUE(bs
))
421 TA_LOG_GLOBAL((", "));
422 TA_LOG_GLOBAL(("neutral"));
426 if (TA_LATIN_IS_X_HEIGHT_BLUE(bs
))
429 TA_LOG_GLOBAL((", "));
430 TA_LOG_GLOBAL(("small top"));
434 if (TA_LATIN_IS_LONG_BLUE(bs
))
437 TA_LOG_GLOBAL((", "));
438 TA_LOG_GLOBAL(("long"));
441 TA_LOG_GLOBAL((")"));
444 TA_LOG_GLOBAL((":\n"));
446 #endif /* TA_DEBUG */
455 FT_ULong glyph_index
;
457 FT_Int best_point
, best_contour_first
, best_contour_last
;
460 FT_Pos best_y_extremum
; /* same as points.y */
461 FT_Bool best_round
= 0;
463 unsigned int i
, num_idx
;
476 GET_UTF8_CHAR(ch
, p_old
);
479 p
= ta_shaper_get_cluster(p
, &metrics
->root
, shaper_buf
, &num_idx
);
483 TA_LOG_GLOBAL((" U+%04lX unavailable\n", ch
));
487 if (TA_LATIN_IS_TOP_BLUE(bs
))
488 best_y_extremum
= FT_INT_MIN
;
490 best_y_extremum
= FT_INT_MAX
;
492 /* iterate over all glyph elements of the character cluster */
493 /* and get the data of the `biggest' one */
494 for (i
= 0; i
< num_idx
; i
++)
500 /* load the character in the face -- skip unknown or empty ones */
501 glyph_index
= ta_shaper_get_elem(&metrics
->root
,
506 if (glyph_index
== 0)
508 TA_LOG_GLOBAL((" U+%04lX unavailable\n", ch
));
512 error
= FT_Load_Glyph(face
, glyph_index
, FT_LOAD_NO_SCALE
);
513 outline
= face
->glyph
->outline
;
514 /* reject glyphs that don't produce any rendering */
515 if (error
|| outline
.n_points
<= 2)
519 TA_LOG_GLOBAL((" U+%04lX contains no (usable) outlines\n", ch
));
521 TA_LOG_GLOBAL((" component %d of cluster starting with U+%04lX"
522 " contains no (usable) outlines\n", i
, ch
));
527 /* now compute min or max point indices and coordinates */
528 points
= outline
.points
;
530 best_y
= 0; /* make compiler happy */
531 best_contour_first
= 0; /* ditto */
532 best_contour_last
= 0; /* ditto */
540 for (nn
= 0; nn
< outline
.n_contours
; first
= last
+ 1, nn
++)
542 FT_Int old_best_point
= best_point
;
546 last
= outline
.contours
[nn
];
548 /* avoid single-point contours since they are never */
549 /* rasterized; in some fonts, they correspond to mark */
550 /* attachment points that are way outside of the glyph's */
555 if (TA_LATIN_IS_TOP_BLUE(bs
)
556 || TA_LATIN_IS_SUB_TOP_BLUE(bs
))
558 for (pp
= first
; pp
<= last
; pp
++)
561 || points
[pp
].y
> best_y
)
564 best_y
= points
[pp
].y
;
565 ascender
= TA_MAX(ascender
, best_y
+ y_offset
);
568 descender
= TA_MIN(descender
, points
[pp
].y
+ y_offset
);
573 for (pp
= first
; pp
<= last
; pp
++)
576 || points
[pp
].y
< best_y
)
579 best_y
= points
[pp
].y
;
580 descender
= TA_MIN(descender
, best_y
+ y_offset
);
583 ascender
= TA_MAX(ascender
, points
[pp
].y
+ y_offset
);
587 if (best_point
!= old_best_point
)
589 best_contour_first
= first
;
590 best_contour_last
= last
;
595 /* now check whether the point belongs to a straight or round */
596 /* segment; we first need to find in which contour the extremum */
597 /* lies, then inspect its previous and next points */
600 FT_Pos best_x
= points
[best_point
].x
;
602 FT_Int best_segment_first
, best_segment_last
;
603 FT_Int best_on_point_first
, best_on_point_last
;
607 best_segment_first
= best_point
;
608 best_segment_last
= best_point
;
610 if (FT_CURVE_TAG(outline
.tags
[best_point
]) == FT_CURVE_TAG_ON
)
612 best_on_point_first
= best_point
;
613 best_on_point_last
= best_point
;
617 best_on_point_first
= -1;
618 best_on_point_last
= -1;
621 /* look for the previous and next points on the contour */
622 /* that are not on the same Y coordinate, then threshold */
623 /* the `closeness'... */
629 if (prev
> best_contour_first
)
632 prev
= best_contour_last
;
634 dist
= TA_ABS(points
[prev
].y
- best_y
);
635 /* accept a small distance or a small angle (both values are */
636 /* heuristic; value 20 corresponds to approx. 2.9 degrees) */
638 if (TA_ABS(points
[prev
].x
- best_x
) <= 20 * dist
)
641 best_segment_first
= prev
;
643 if (FT_CURVE_TAG(outline
.tags
[prev
]) == FT_CURVE_TAG_ON
)
645 best_on_point_first
= prev
;
646 if (best_on_point_last
< 0)
647 best_on_point_last
= prev
;
650 } while (prev
!= best_point
);
654 if (next
< best_contour_last
)
657 next
= best_contour_first
;
659 dist
= TA_ABS(points
[next
].y
- best_y
);
661 if (TA_ABS(points
[next
].x
- best_x
) <= 20 * dist
)
664 best_segment_last
= next
;
666 if (FT_CURVE_TAG(outline
.tags
[next
]) == FT_CURVE_TAG_ON
)
668 best_on_point_last
= next
;
669 if (best_on_point_first
< 0)
670 best_on_point_first
= next
;
673 } while (next
!= best_point
);
675 if (TA_LATIN_IS_LONG_BLUE(bs
))
677 /* If this flag is set, we have an additional constraint to */
678 /* get the blue zone distance: Find a segment of the topmost */
679 /* (or bottommost) contour that is longer than a heuristic */
680 /* threshold. This ensures that small bumps in the outline */
681 /* are ignored (for example, the `vertical serifs' found in */
682 /* many Hebrew glyph designs). */
684 /* If this segment is long enough, we are done. Otherwise, */
685 /* search the segment next to the extremum that is long */
686 /* enough, has the same direction, and a not too large */
687 /* vertical distance from the extremum. Note that the */
688 /* algorithm doesn't check whether the found segment is */
689 /* actually the one (vertically) nearest to the extremum. */
691 /* heuristic threshold value */
692 FT_Pos length_threshold
= metrics
->units_per_em
/ 25;
695 dist
= TA_ABS(points
[best_segment_last
].x
-
696 points
[best_segment_first
].x
);
698 if (dist
< length_threshold
699 && best_segment_last
- best_segment_first
+ 2 <=
700 best_contour_last
- best_contour_first
)
702 /* heuristic threshold value */
703 FT_Pos height_threshold
= metrics
->units_per_em
/ 4;
709 /* we intentionally declare these two variables */
710 /* outside of the loop since various compilers emit */
711 /* incorrect warning messages otherwise, talking about */
712 /* `possibly uninitialized variables' */
713 FT_Int p_first
= 0; /* make compiler happy */
719 /* compute direction */
724 if (prev
> best_contour_first
)
727 prev
= best_contour_last
;
729 if (points
[prev
].x
!= best_x
)
731 } while (prev
!= best_point
);
733 /* skip glyph for the degenerate case */
734 if (prev
== best_point
)
737 left2right
= FT_BOOL(points
[prev
].x
< points
[best_point
].x
);
739 first
= best_segment_last
;
751 /* no hit; adjust first point */
754 /* also adjust first and last on point */
755 if (FT_CURVE_TAG(outline
.tags
[first
]) == FT_CURVE_TAG_ON
)
769 if (last
< best_contour_last
)
772 last
= best_contour_first
;
774 if (TA_ABS(best_y
- points
[first
].y
) > height_threshold
)
776 /* vertical distance too large */
781 /* same test as above */
782 dist
= TA_ABS(points
[last
].y
- points
[first
].y
);
784 if (TA_ABS(points
[last
].x
- points
[first
].x
) <= 20 * dist
)
790 if (FT_CURVE_TAG(outline
.tags
[last
]) == FT_CURVE_TAG_ON
)
797 l2r
= FT_BOOL(points
[first
].x
< points
[last
].x
);
798 d
= TA_ABS(points
[last
].x
- points
[first
].x
);
800 if (l2r
== left2right
801 && d
>= length_threshold
)
803 /* all constraints are met; update segment after */
804 /* finding its end */
807 if (last
< best_contour_last
)
810 last
= best_contour_first
;
812 d
= TA_ABS(points
[last
].y
- points
[first
].y
);
814 if (TA_ABS(points
[next
].x
- points
[first
].x
) <=
817 if (last
> best_contour_first
)
820 last
= best_contour_last
;
826 if (FT_CURVE_TAG(outline
.tags
[last
]) == FT_CURVE_TAG_ON
)
832 } while (last
!= best_segment_first
);
834 best_y
= points
[first
].y
;
836 best_segment_first
= first
;
837 best_segment_last
= last
;
839 best_on_point_first
= p_first
;
840 best_on_point_last
= p_last
;
844 } while (last
!= best_segment_first
);
849 * for computing blue zones, we add the y offset as returned
850 * by the currently used OpenType feature --
851 * for example, superscript glyphs might be identical
852 * to subscript glyphs with a vertical shift
858 TA_LOG_GLOBAL((" U+%04lX: best_y = %5ld", ch
, best_y
));
860 TA_LOG_GLOBAL((" component %d of cluster starting with U+%04lX:"
861 " best_y = %5ld", i
, ch
, best_y
));
865 * now set the `round' flag depending on the segment's kind:
867 * - if the horizontal distance between the first and last
868 * `on' point is larger than a heuristic threshold
869 * we have a flat segment
870 * - if either the first or the last point of the segment is
871 * an `off' point, the segment is round, otherwise it is
874 if (best_on_point_first
>= 0
875 && best_on_point_last
>= 0
876 && (TA_ABS(points
[best_on_point_last
].x
877 - points
[best_on_point_first
].x
))
881 round
= FT_BOOL(FT_CURVE_TAG(outline
.tags
[best_segment_first
])
883 || FT_CURVE_TAG(outline
.tags
[best_segment_last
])
886 if (round
&& TA_LATIN_IS_NEUTRAL_BLUE(bs
))
888 /* only use flat segments for a neutral blue zone */
889 TA_LOG_GLOBAL((" (round, skipped)\n"));
893 TA_LOG_GLOBAL((" (%s)\n", round
? "round" : "flat"));
896 if (TA_LATIN_IS_TOP_BLUE(bs
))
898 if (best_y
> best_y_extremum
)
900 best_y_extremum
= best_y
;
906 if (best_y
< best_y_extremum
)
908 best_y_extremum
= best_y
;
915 if (!(best_y_extremum
== FT_INT_MIN
916 || best_y_extremum
== FT_INT_MAX
))
919 rounds
[num_rounds
++] = best_y_extremum
;
921 flats
[num_flats
++] = best_y_extremum
;
924 } /* end while loop */
926 if (num_flats
== 0 && num_rounds
== 0)
928 /* we couldn't find a single glyph to compute this blue zone, */
929 /* we will simply ignore it then */
930 TA_LOG_GLOBAL((" empty\n"));
934 /* we have computed the contents of the `rounds' and `flats' tables, */
935 /* now determine the reference and overshoot position of the blue -- */
936 /* we simply take the median value after a simple sort */
937 ta_sort_pos(num_rounds
, rounds
);
938 ta_sort_pos(num_flats
, flats
);
940 blue
= &axis
->blues
[axis
->blue_count
];
941 blue_ref
= &blue
->ref
.org
;
942 blue_shoot
= &blue
->shoot
.org
;
949 *blue_shoot
= rounds
[num_rounds
/ 2];
951 else if (num_rounds
== 0)
954 *blue_shoot
= flats
[num_flats
/ 2];
958 *blue_ref
= flats
[num_flats
/ 2];
959 *blue_shoot
= rounds
[num_rounds
/ 2];
962 /* there are sometimes problems if the overshoot position of top */
963 /* zones is under its reference position, or the opposite for bottom */
964 /* zones; we must thus check everything there and correct the errors */
965 if (*blue_shoot
!= *blue_ref
)
967 FT_Pos ref
= *blue_ref
;
968 FT_Pos shoot
= *blue_shoot
;
969 FT_Bool over_ref
= FT_BOOL(shoot
> ref
);
972 if ((TA_LATIN_IS_TOP_BLUE(bs
)
973 || TA_LATIN_IS_SUB_TOP_BLUE(bs
)) ^ over_ref
)
976 *blue_shoot
= (shoot
+ ref
) / 2;
978 TA_LOG_GLOBAL((" [overshoot smaller than reference,"
979 " taking mean value]\n"));
983 blue
->ascender
= ascender
;
984 blue
->descender
= descender
;
987 if (TA_LATIN_IS_TOP_BLUE(bs
))
988 blue
->flags
|= TA_LATIN_BLUE_TOP
;
989 if (TA_LATIN_IS_SUB_TOP_BLUE(bs
))
990 blue
->flags
|= TA_LATIN_BLUE_SUB_TOP
;
991 if (TA_LATIN_IS_NEUTRAL_BLUE(bs
))
992 blue
->flags
|= TA_LATIN_BLUE_NEUTRAL
;
994 /* the following flag is used later to adjust the y and x scales */
995 /* in order to optimize the pixel grid alignment */
996 /* of the top of small letters */
997 if (TA_LATIN_IS_X_HEIGHT_BLUE(bs
))
998 blue
->flags
|= TA_LATIN_BLUE_ADJUSTMENT
;
1000 TA_LOG_GLOBAL((" -> reference = %ld\n"
1001 " overshoot = %ld\n",
1002 *blue_ref
, *blue_shoot
));
1004 } /* end for loop */
1006 ta_shaper_buf_destroy(face
, shaper_buf
);
1008 /* if windows compatibility mode is activated, */
1009 /* add two artificial blue zones for usWinAscent and usWinDescent */
1010 /* just in case the above algorithm has missed them -- */
1011 /* Windows cuts off everything outside of those two values */
1012 if (metrics
->root
.globals
->font
->windows_compatibility
)
1017 os2
= (TT_OS2
*)FT_Get_Sfnt_Table(face
, ft_sfnt_os2
);
1021 blue
= &axis
->blues
[axis
->blue_count
];
1022 blue
->flags
= TA_LATIN_BLUE_TOP
| TA_LATIN_BLUE_ACTIVE
;
1024 blue
->shoot
.org
= os2
->usWinAscent
;
1026 TA_LOG_GLOBAL(("artificial blue zone for usWinAscent:\n"
1027 " -> reference = %ld\n"
1028 " overshoot = %ld\n",
1029 blue
->ref
.org
, blue
->shoot
.org
));
1031 blue
= &axis
->blues
[axis
->blue_count
+ 1];
1032 blue
->flags
= TA_LATIN_BLUE_ACTIVE
;
1034 blue
->shoot
.org
= -os2
->usWinDescent
;
1036 TA_LOG_GLOBAL(("artificial blue zone for usWinDescent:\n"
1037 " -> reference = %ld\n"
1038 " overshoot = %ld\n",
1039 blue
->ref
.org
, blue
->shoot
.org
));
1043 blue
= &axis
->blues
[axis
->blue_count
];
1046 blue
->shoot
.org
= 0;
1048 blue
= &axis
->blues
[axis
->blue_count
+ 1];
1051 blue
->shoot
.org
= 0;
1055 /* we finally check whether blue zones are ordered; */
1056 /* `ref' and `shoot' values of two blue zones must not overlap */
1057 if (axis
->blue_count
)
1060 TA_LatinBlue blue_sorted
[TA_BLUE_STRINGSET_MAX_LEN
+ 2];
1063 for (i
= 0; i
< axis
->blue_count
; i
++)
1064 blue_sorted
[i
] = &axis
->blues
[i
];
1066 /* sort bottoms of blue zones... */
1067 ta_latin_sort_blue(axis
->blue_count
, blue_sorted
);
1069 /* ...and adjust top values if necessary */
1070 for (i
= 0; i
< axis
->blue_count
- 1; i
++)
1076 FT_Bool a_is_top
= 0;
1080 if (blue_sorted
[i
]->flags
& (TA_LATIN_BLUE_TOP
1081 | TA_LATIN_BLUE_SUB_TOP
))
1083 a
= &blue_sorted
[i
]->shoot
.org
;
1089 a
= &blue_sorted
[i
]->ref
.org
;
1091 if (blue_sorted
[i
+ 1]->flags
& (TA_LATIN_BLUE_TOP
1092 | TA_LATIN_BLUE_SUB_TOP
))
1093 b
= &blue_sorted
[i
+ 1]->shoot
.org
;
1095 b
= &blue_sorted
[i
+ 1]->ref
.org
;
1100 TA_LOG_GLOBAL(("blue zone overlap:"
1101 " adjusting %s %d to %ld\n",
1102 a_is_top
? "overshoot" : "reference",
1103 blue_sorted
[i
] - axis
->blues
,
1109 TA_LOG_GLOBAL(("\n"));
1115 /* check whether all ASCII digits have the same advance width */
1118 ta_latin_metrics_check_digits(TA_LatinMetrics metrics
,
1121 FT_Bool started
= 0, same_width
= 1;
1122 FT_Fixed advance
= 0, old_advance
= 0;
1126 /* in all supported charmaps, digits have character codes 0x30-0x39 */
1127 const char digits
[] = "0 1 2 3 4 5 6 7 8 9";
1132 shaper_buf
= ta_shaper_buf_create(face
);
1136 FT_ULong glyph_index
;
1137 unsigned int num_idx
;
1140 /* reject input that maps to more than a single glyph */
1141 p
= ta_shaper_get_cluster(p
, &metrics
->root
, shaper_buf
, &num_idx
);
1145 glyph_index
= ta_shaper_get_elem(&metrics
->root
,
1155 if (advance
!= old_advance
)
1163 old_advance
= advance
;
1168 ta_shaper_buf_destroy(face
, shaper_buf
);
1170 metrics
->root
.digits_have_same_width
= same_width
;
1174 /* initialize global metrics */
1177 ta_latin_metrics_init(TA_LatinMetrics metrics
,
1181 FT_CharMap oldmap
= face
->charmap
;
1184 metrics
->units_per_em
= face
->units_per_EM
;
1186 if (!FT_Select_Charmap(face
, FT_ENCODING_UNICODE
))
1188 ta_latin_metrics_init_widths(metrics
, face
, 1);
1189 ta_latin_metrics_init_blues(metrics
, reference
? reference
: face
);
1190 ta_latin_metrics_check_digits(metrics
, face
);
1194 /* we only have a symbol font encoding */
1195 ta_latin_metrics_init_widths(metrics
, face
, 0);
1198 FT_Set_Charmap(face
, oldmap
);
1203 /* adjust scaling value, then scale and shift widths */
1204 /* and blue zones (if applicable) for given dimension */
1207 ta_latin_metrics_scale_dim(TA_LatinMetrics metrics
,
1218 ppem
= metrics
->root
.scaler
.face
->size
->metrics
.x_ppem
;
1220 if (dim
== TA_DIMENSION_HORZ
)
1222 scale
= scaler
->x_scale
;
1223 delta
= scaler
->x_delta
;
1227 scale
= scaler
->y_scale
;
1228 delta
= scaler
->y_delta
;
1231 axis
= &metrics
->axis
[dim
];
1233 if (axis
->org_scale
== scale
&& axis
->org_delta
== delta
)
1236 axis
->org_scale
= scale
;
1237 axis
->org_delta
= delta
;
1239 /* correct Y scale to optimize the alignment of the top of */
1240 /* small letters to the pixel grid */
1241 /* (if we do x-height snapping for this ppem value) */
1242 if (!number_set_is_element(
1243 metrics
->root
.globals
->font
->x_height_snapping_exceptions
,
1246 TA_LatinAxis Axis
= &metrics
->axis
[TA_DIMENSION_VERT
];
1247 TA_LatinBlue blue
= NULL
;
1250 for (nn
= 0; nn
< Axis
->blue_count
; nn
++)
1252 if (Axis
->blues
[nn
].flags
& TA_LATIN_BLUE_ADJUSTMENT
)
1254 blue
= &Axis
->blues
[nn
];
1267 scaled
= FT_MulFix(blue
->shoot
.org
, scale
);
1268 limit
= metrics
->root
.globals
->increase_x_height
;
1271 /* if the `increase-x-height' property is active, */
1272 /* we round up much more often */
1275 && ppem
>= TA_PROP_INCREASE_X_HEIGHT_MIN
)
1278 fitted
= (scaled
+ threshold
) & ~63;
1280 if (scaled
!= fitted
)
1282 if (dim
== TA_DIMENSION_VERT
)
1289 new_scale
= FT_MulDiv(scale
, fitted
, scaled
);
1291 /* the scaling should not change the result by more than two pixels */
1292 max_height
= metrics
->units_per_em
;
1294 for (nn
= 0; nn
< Axis
->blue_count
; nn
++)
1296 max_height
= TA_MAX(max_height
, Axis
->blues
[nn
].ascender
);
1297 max_height
= TA_MAX(max_height
, -Axis
->blues
[nn
].descender
);
1300 dist
= TA_ABS(FT_MulFix(max_height
, new_scale
- scale
));
1306 "ta_latin_metrics_scale_dim:"
1307 " x height alignment (style `%s'):\n"
1309 " vertical scaling changed from %.5f to %.5f (by %d%%)\n"
1311 ta_style_names
[metrics
->root
.style_class
->style
],
1313 new_scale
/ 65536.0,
1314 (fitted
- scaled
) * 100 / scaled
));
1322 "ta_latin_metrics_scale_dim:"
1323 " x height alignment (style `%s'):\n"
1325 " excessive vertical scaling abandoned\n"
1327 ta_style_names
[metrics
->root
.style_class
->style
]));
1335 axis
->scale
= scale
;
1336 axis
->delta
= delta
;
1338 if (dim
== TA_DIMENSION_HORZ
)
1340 metrics
->root
.scaler
.x_scale
= scale
;
1341 metrics
->root
.scaler
.x_delta
= delta
;
1345 metrics
->root
.scaler
.y_scale
= scale
;
1346 metrics
->root
.scaler
.y_delta
= delta
;
1349 TA_LOG_GLOBAL(("%s widths (style `%s')\n",
1350 dim
== TA_DIMENSION_HORZ
? "horizontal" : "vertical",
1351 ta_style_names
[metrics
->root
.style_class
->style
]));
1353 /* scale the widths */
1354 for (nn
= 0; nn
< axis
->width_count
; nn
++)
1356 TA_Width width
= axis
->widths
+ nn
;
1359 width
->cur
= FT_MulFix(width
->org
, scale
);
1360 width
->fit
= width
->cur
;
1362 TA_LOG_GLOBAL((" %d scaled to %.2f\n",
1364 width
->cur
/ 64.0));
1367 TA_LOG_GLOBAL(("\n"));
1369 /* an extra-light axis corresponds to a standard width that is */
1370 /* smaller than 5/8 pixels */
1372 (FT_Bool
)(FT_MulFix(axis
->standard_width
, scale
) < 32 + 8);
1375 if (axis
->extra_light
)
1376 TA_LOG_GLOBAL(("`%s' style is extra light (at current resolution)\n"
1378 ta_style_names
[metrics
->root
.style_class
->style
]));
1381 if (dim
== TA_DIMENSION_VERT
)
1384 if (axis
->blue_count
)
1385 TA_LOG_GLOBAL(("blue zones (style `%s')\n",
1386 ta_style_names
[metrics
->root
.style_class
->style
]));
1389 /* scale the blue zones */
1390 for (nn
= 0; nn
< axis
->blue_count
; nn
++)
1392 TA_LatinBlue blue
= &axis
->blues
[nn
];
1396 blue
->ref
.cur
= FT_MulFix(blue
->ref
.org
, scale
) + delta
;
1397 blue
->ref
.fit
= blue
->ref
.cur
;
1398 blue
->shoot
.cur
= FT_MulFix(blue
->shoot
.org
, scale
) + delta
;
1399 blue
->shoot
.fit
= blue
->shoot
.cur
;
1400 blue
->flags
&= ~TA_LATIN_BLUE_ACTIVE
;
1402 /* a blue zone is only active if it is less than 3/4 pixels tall */
1403 dist
= FT_MulFix(blue
->ref
.org
- blue
->shoot
.org
, scale
);
1404 if (dist
<= 48 && dist
>= -48)
1412 /* use discrete values for blue zone widths */
1415 /* generic, original code */
1416 delta1
= blue
->shoot
.org
- blue
->ref
.org
;
1421 delta2
= FT_MulFix(delta2
, scale
);
1425 else if (delta2
< 64)
1426 delta2
= 32 + (((delta2
- 32) + 16) & ~31);
1428 delta2
= TA_PIX_ROUND(delta2
);
1433 blue
->ref
.fit
= TA_PIX_ROUND(blue
->ref
.cur
);
1434 blue
->shoot
.fit
= blue
->ref
.fit
+ delta2
;
1436 /* simplified version due to abs(dist) <= 48 */
1443 else if (delta2
< 48)
1451 blue
->ref
.fit
= TA_PIX_ROUND(blue
->ref
.cur
);
1452 blue
->shoot
.fit
= blue
->ref
.fit
- delta2
;
1455 blue
->flags
|= TA_LATIN_BLUE_ACTIVE
;
1459 /* use sub-top blue zone only if it doesn't overlap with */
1460 /* another (non-sup-top) blue zone; otherwise, the */
1461 /* effect would be similar to a neutral blue zone, which */
1462 /* is not desired here */
1463 for (nn
= 0; nn
< axis
->blue_count
; nn
++)
1465 TA_LatinBlue blue
= &axis
->blues
[nn
];
1469 if (!(blue
->flags
& TA_LATIN_BLUE_SUB_TOP
))
1471 if (!(blue
->flags
& TA_LATIN_BLUE_ACTIVE
))
1474 for (i
= 0; i
< axis
->blue_count
; i
++)
1476 TA_LatinBlue b
= &axis
->blues
[i
];
1479 if (b
->flags
& TA_LATIN_BLUE_SUB_TOP
)
1481 if (!(b
->flags
& TA_LATIN_BLUE_ACTIVE
))
1484 if (b
->ref
.fit
<= blue
->shoot
.fit
1485 && b
->shoot
.fit
>= blue
->ref
.fit
)
1487 blue
->flags
&= ~TA_LATIN_BLUE_ACTIVE
;
1494 for (nn
= 0; nn
< axis
->blue_count
; nn
++)
1496 TA_LatinBlue blue
= &axis
->blues
[nn
];
1499 TA_LOG_GLOBAL((" reference %d: %d scaled to %.2f%s\n"
1500 " overshoot %d: %d scaled to %.2f%s\n",
1503 blue
->ref
.fit
/ 64.0,
1504 (blue
->flags
& TA_LATIN_BLUE_ACTIVE
) ? ""
1508 blue
->shoot
.fit
/ 64.0,
1509 (blue
->flags
& TA_LATIN_BLUE_ACTIVE
) ? ""
1514 /* the last two artificial blue zones are to be scaled */
1515 /* with uncorrected scaling values */
1516 if (metrics
->root
.globals
->font
->windows_compatibility
)
1518 TA_LatinAxis a
= &metrics
->axis
[TA_DIMENSION_VERT
];
1522 b
= &a
->blues
[a
->blue_count
];
1526 b
->shoot
.fit
= FT_MulFix(b
->ref
.org
, a
->org_scale
) + delta
;
1528 TA_LOG_GLOBAL((" reference %d: %d scaled to %.2f (artificial)\n"
1529 " overshoot %d: %d scaled to %.2f (artificial)\n",
1535 b
->shoot
.fit
/ 64.0));
1537 b
= &a
->blues
[a
->blue_count
+ 1];
1541 b
->shoot
.fit
= FT_MulFix(b
->ref
.org
, a
->org_scale
) + delta
;
1543 TA_LOG_GLOBAL((" reference %d: %d scaled to %.2f (artificial)\n"
1544 " overshoot %d: %d scaled to %.2f (artificial)\n",
1550 b
->shoot
.fit
/ 64.0));
1553 TA_LOG_GLOBAL(("\n"));
1558 /* scale global values in both directions */
1561 ta_latin_metrics_scale(TA_LatinMetrics metrics
,
1564 metrics
->root
.scaler
.render_mode
= scaler
->render_mode
;
1565 metrics
->root
.scaler
.face
= scaler
->face
;
1566 metrics
->root
.scaler
.flags
= scaler
->flags
;
1568 ta_latin_metrics_scale_dim(metrics
, scaler
, TA_DIMENSION_HORZ
);
1569 ta_latin_metrics_scale_dim(metrics
, scaler
, TA_DIMENSION_VERT
);
1573 /* walk over all contours and compute its segments */
1576 ta_latin_hints_compute_segments(TA_GlyphHints hints
,
1579 TA_LatinMetrics metrics
= (TA_LatinMetrics
)hints
->metrics
;
1580 TA_AxisHints axis
= &hints
->axis
[dim
];
1581 FT_Error error
= FT_Err_Ok
;
1583 TA_Segment segment
= NULL
;
1586 TA_Point
* contour
= hints
->contours
;
1587 TA_Point
* contour_limit
= contour
+ hints
->num_contours
;
1588 TA_Direction major_dir
, segment_dir
;
1590 FT_Pos flat_threshold
= FLAT_THRESHOLD(metrics
->units_per_em
);
1593 memset(&seg0
, 0, sizeof (TA_SegmentRec
));
1595 seg0
.flags
= TA_EDGE_NORMAL
;
1597 major_dir
= (TA_Direction
)TA_ABS(axis
->major_dir
);
1598 segment_dir
= major_dir
;
1600 axis
->num_segments
= 0;
1602 /* set up (u,v) in each point */
1603 if (dim
== TA_DIMENSION_HORZ
)
1605 TA_Point point
= hints
->points
;
1606 TA_Point limit
= point
+ hints
->num_points
;
1609 for (; point
< limit
; point
++)
1611 point
->u
= point
->fx
;
1612 point
->v
= point
->fy
;
1617 TA_Point point
= hints
->points
;
1618 TA_Point limit
= point
+ hints
->num_points
;
1621 for (; point
< limit
; point
++)
1623 point
->u
= point
->fy
;
1624 point
->v
= point
->fx
;
1628 /* do each contour separately */
1629 for (; contour
< contour_limit
; contour
++)
1631 TA_Point point
= contour
[0];
1632 TA_Point last
= point
->prev
;
1636 /* we call values measured along a segment (point->v) */
1637 /* `coordinates', and values orthogonal to it (point->u) */
1639 FT_Pos min_pos
= 32000;
1640 FT_Pos max_pos
= -32000;
1641 FT_Pos min_coord
= 32000;
1642 FT_Pos max_coord
= -32000;
1643 FT_UShort min_flags
= TA_FLAG_NONE
;
1644 FT_UShort max_flags
= TA_FLAG_NONE
;
1645 FT_Pos min_on_coord
= 32000;
1646 FT_Pos max_on_coord
= -32000;
1650 TA_Segment prev_segment
= NULL
;
1652 FT_Pos prev_min_pos
= min_pos
;
1653 FT_Pos prev_max_pos
= max_pos
;
1654 FT_Pos prev_min_coord
= min_coord
;
1655 FT_Pos prev_max_coord
= max_coord
;
1656 FT_UShort prev_min_flags
= min_flags
;
1657 FT_UShort prev_max_flags
= max_flags
;
1658 FT_Pos prev_min_on_coord
= min_on_coord
;
1659 FT_Pos prev_max_on_coord
= max_on_coord
;
1662 if (TA_ABS(last
->out_dir
) == major_dir
1663 && TA_ABS(point
->out_dir
) == major_dir
)
1665 /* we are already on an edge, try to locate its start */
1670 point
= point
->prev
;
1671 if (TA_ABS(point
->out_dir
) != major_dir
)
1673 point
= point
->next
;
1691 /* get minimum and maximum position */
1698 /* get minimum and maximum coordinate together with flags */
1703 min_flags
= point
->flags
;
1708 max_flags
= point
->flags
;
1711 /* get minimum and maximum coordinate of `on' points */
1712 if (!(point
->flags
& TA_FLAG_CONTROL
))
1715 if (v
< min_on_coord
)
1717 if (v
> max_on_coord
)
1721 if (point
->out_dir
!= segment_dir
1724 /* check whether the new segment's start point is identical to */
1725 /* the previous segment's end point; for example, this might */
1726 /* happen for spikes */
1729 || segment
->first
!= prev_segment
->last
)
1731 /* points are different: we are just leaving an edge, thus */
1732 /* record a new segment */
1734 segment
->last
= point
;
1735 segment
->pos
= (FT_Short
)((min_pos
+ max_pos
) >> 1);
1736 segment
->delta
= (FT_Short
)((max_pos
- min_pos
) >> 1);
1738 /* a segment is round if either its first or last point */
1739 /* is a control point, and the length of the on points */
1740 /* inbetween doesn't exceed a heuristic limit */
1741 if ((min_flags
| max_flags
) & TA_FLAG_CONTROL
1742 && (max_on_coord
- min_on_coord
) < flat_threshold
)
1743 segment
->flags
|= TA_EDGE_ROUND
;
1745 segment
->min_coord
= (FT_Short
)min_coord
;
1746 segment
->max_coord
= (FT_Short
)max_coord
;
1747 segment
->height
= segment
->max_coord
- segment
->min_coord
;
1749 prev_segment
= segment
;
1750 prev_min_pos
= min_pos
;
1751 prev_max_pos
= max_pos
;
1752 prev_min_coord
= min_coord
;
1753 prev_max_coord
= max_coord
;
1754 prev_min_flags
= min_flags
;
1755 prev_max_flags
= max_flags
;
1756 prev_min_on_coord
= min_on_coord
;
1757 prev_max_on_coord
= max_on_coord
;
1761 /* points are the same: we don't create a new segment but */
1762 /* merge the current segment with the previous one */
1764 if (prev_segment
->last
->in_dir
== point
->in_dir
)
1766 /* we have identical directions (this can happen for */
1767 /* degenerate outlines that move zig-zag along the main */
1768 /* axis without changing the coordinate value of the other */
1769 /* axis, and where the segments have just been merged): */
1770 /* unify segments */
1772 /* update constraints */
1774 if (prev_min_pos
< min_pos
)
1775 min_pos
= prev_min_pos
;
1776 if (prev_max_pos
> max_pos
)
1777 max_pos
= prev_max_pos
;
1779 if (prev_min_coord
< min_coord
)
1781 min_coord
= prev_min_coord
;
1782 min_flags
= prev_min_flags
;
1784 if (prev_max_coord
> max_coord
)
1786 max_coord
= prev_max_coord
;
1787 max_flags
= prev_max_flags
;
1790 if (prev_min_on_coord
< min_on_coord
)
1791 min_on_coord
= prev_min_on_coord
;
1792 if (prev_max_on_coord
> max_on_coord
)
1793 max_on_coord
= prev_max_on_coord
;
1795 prev_segment
->last
= point
;
1796 prev_segment
->pos
= (FT_Short
)((min_pos
+ max_pos
) >> 1);
1798 if ((min_flags
| max_flags
) & TA_FLAG_CONTROL
1799 && (max_on_coord
- min_on_coord
) < flat_threshold
)
1800 prev_segment
->flags
|= TA_EDGE_ROUND
;
1802 prev_segment
->flags
&= ~TA_EDGE_ROUND
;
1804 prev_segment
->min_coord
= (FT_Short
)min_coord
;
1805 prev_segment
->max_coord
= (FT_Short
)max_coord
;
1806 prev_segment
->height
= prev_segment
->max_coord
1807 - prev_segment
->min_coord
;
1811 /* we have different directions; use the properties of the */
1812 /* longer segment and discard the other one */
1814 if (TA_ABS(prev_max_coord
- prev_min_coord
)
1815 > TA_ABS(max_coord
- min_coord
))
1817 /* discard current segment */
1819 if (min_pos
< prev_min_pos
)
1820 prev_min_pos
= min_pos
;
1821 if (max_pos
> prev_max_pos
)
1822 prev_max_pos
= max_pos
;
1824 prev_segment
->last
= point
;
1825 prev_segment
->pos
= (FT_Short
)((prev_min_pos
1826 + prev_max_pos
) >> 1);
1830 /* discard previous segment */
1832 if (prev_min_pos
< min_pos
)
1833 min_pos
= prev_min_pos
;
1834 if (prev_max_pos
> max_pos
)
1835 max_pos
= prev_max_pos
;
1837 segment
->last
= point
;
1838 segment
->pos
= (FT_Short
)((min_pos
+ max_pos
) >> 1);
1840 if ((min_flags
| max_flags
) & TA_FLAG_CONTROL
1841 && (max_on_coord
- min_on_coord
) < flat_threshold
)
1842 segment
->flags
|= TA_EDGE_ROUND
;
1844 segment
->min_coord
= (FT_Short
)min_coord
;
1845 segment
->max_coord
= (FT_Short
)max_coord
;
1846 segment
->height
= segment
->max_coord
- segment
->min_coord
;
1848 *prev_segment
= *segment
;
1850 prev_min_pos
= min_pos
;
1851 prev_max_pos
= max_pos
;
1852 prev_min_coord
= min_coord
;
1853 prev_max_coord
= max_coord
;
1854 prev_min_flags
= min_flags
;
1855 prev_max_flags
= max_flags
;
1856 prev_min_on_coord
= min_on_coord
;
1857 prev_max_on_coord
= max_on_coord
;
1861 axis
->num_segments
--;
1871 /* now exit if we are at the start/end point */
1879 /* if we are not on an edge, check whether the major direction */
1880 /* coincides with the current point's `out' direction, or */
1881 /* whether we have a single-point contour */
1883 && (TA_ABS(point
->out_dir
) == major_dir
1884 || point
== point
->prev
))
1886 /* this is the start of a new segment! */
1887 segment_dir
= (TA_Direction
)point
->out_dir
;
1889 error
= ta_axis_hints_new_segment(axis
, &segment
);
1893 /* clear all segment fields */
1896 segment
->dir
= (FT_Char
)segment_dir
;
1897 segment
->first
= point
;
1898 segment
->last
= point
;
1900 /* `ta_axis_hints_new_segment' reallocates memory, */
1901 /* thus we have to refresh the `prev_segment' pointer */
1903 prev_segment
= segment
- 1;
1905 min_pos
= max_pos
= point
->u
;
1906 min_coord
= max_coord
= point
->v
;
1907 min_flags
= max_flags
= point
->flags
;
1909 if (point
->flags
& TA_FLAG_CONTROL
)
1911 min_on_coord
= 32000;
1912 max_on_coord
= -32000;
1915 min_on_coord
= max_on_coord
= point
->v
;
1919 if (point
->out_dir
!= point
->next
->in_dir
1920 || point
== point
->prev
)
1923 * We have a one-point segment. This is either
1925 * . a one-point contour (with `in' and `out' direction
1926 * set to TA_DIR_NONE by default), or
1928 * . an artificial one-point segment (with a forced
1931 segment
->pos
= (FT_Short
)min_pos
;
1933 if (point
->flags
& TA_FLAG_CONTROL
)
1934 segment
->flags
|= TA_EDGE_ROUND
;
1936 /* artificially extend the horizontal size if requested */
1937 segment
->min_coord
= (FT_Short
)point
->v
+ point
->left_offset
;
1938 segment
->max_coord
= (FT_Short
)point
->v
+ point
->right_offset
;
1939 segment
->height
= 0;
1946 point
= point
->next
;
1951 /* now slightly increase the height of segments if this makes sense -- */
1952 /* this is used to better detect and ignore serifs */
1954 TA_Segment segments
= axis
->segments
;
1955 TA_Segment segments_end
= segments
+ axis
->num_segments
;
1958 for (segment
= segments
; segment
< segments_end
; segment
++)
1960 TA_Point first
= segment
->first
;
1961 TA_Point last
= segment
->last
;
1963 FT_Pos first_v
= first
->v
;
1964 FT_Pos last_v
= last
->v
;
1967 if (first_v
< last_v
)
1974 segment
->height
= (FT_Short
)(segment
->height
+
1975 ((first_v
- p
->v
) >> 1));
1979 segment
->height
= (FT_Short
)(segment
->height
+
1980 ((p
->v
- last_v
) >> 1));
1989 segment
->height
= (FT_Short
)(segment
->height
+
1990 ((p
->v
- first_v
) >> 1));
1994 segment
->height
= (FT_Short
)(segment
->height
+
1995 ((last_v
- p
->v
) >> 1));
2005 /* link segments to form stems and serifs; if `width_count' and */
2006 /* `widths' are non-zero, use them to fine-tune the scoring function */
2009 ta_latin_hints_link_segments(TA_GlyphHints hints
,
2010 FT_UInt width_count
,
2011 TA_WidthRec
* widths
,
2014 TA_AxisHints axis
= &hints
->axis
[dim
];
2016 TA_Segment segments
= axis
->segments
;
2017 TA_Segment segment_limit
= segments
+ axis
->num_segments
;
2019 FT_Pos len_threshold
, len_score
, dist_score
, max_width
;
2020 TA_Segment seg1
, seg2
;
2024 max_width
= widths
[width_count
- 1].org
;
2028 /* a heuristic value to set up a minimum value for overlapping */
2029 len_threshold
= TA_LATIN_CONSTANT(hints
->metrics
, 8);
2030 if (len_threshold
== 0)
2033 /* a heuristic value to weight lengths */
2034 len_score
= TA_LATIN_CONSTANT(hints
->metrics
, 6000);
2036 /* a heuristic value to weight distances (no call to */
2037 /* TA_LATIN_CONSTANT needed, since we work on multiples */
2038 /* of the stem width) */
2041 /* now compare each segment to the others */
2042 for (seg1
= segments
; seg1
< segment_limit
; seg1
++)
2044 if (seg1
->dir
!= axis
->major_dir
)
2047 /* search for stems having opposite directions, */
2048 /* with seg1 to the `left' of seg2 */
2049 for (seg2
= segments
; seg2
< segment_limit
; seg2
++)
2051 FT_Pos pos1
= seg1
->pos
;
2052 FT_Pos pos2
= seg2
->pos
;
2055 if (seg1
->dir
+ seg2
->dir
== 0
2058 /* compute distance between the two segments */
2059 FT_Pos min
= seg1
->min_coord
;
2060 FT_Pos max
= seg1
->max_coord
;
2064 if (min
< seg2
->min_coord
)
2065 min
= seg2
->min_coord
;
2066 if (max
> seg2
->max_coord
)
2067 max
= seg2
->max_coord
;
2069 /* compute maximum coordinate difference of the two segments */
2070 /* (this is, how much they overlap) */
2073 /* for one-point segments, `len' is zero if there is an overlap */
2074 /* (and negative otherwise); we have to correct this */
2076 && (seg1
->min_coord
== seg1
->max_coord
2077 || seg2
->min_coord
== seg2
->max_coord
))
2078 len
= len_threshold
;
2080 if (len
>= len_threshold
)
2083 * The score is the sum of two demerits indicating the
2084 * `badness' of a fit, measured along the segments' main axis
2085 * and orthogonal to it, respectively.
2087 * o The less overlapping along the main axis, the worse it
2088 * is, causing a larger demerit.
2090 * o The nearer the orthogonal distance to a stem width, the
2091 * better it is, causing a smaller demerit. For simplicity,
2092 * however, we only increase the demerit for values that
2093 * exceed the largest stem width.
2096 FT_Pos dist
= pos2
- pos1
;
2098 FT_Pos dist_demerit
, score
;
2103 /* distance demerits are based on multiples of `max_width'; */
2104 /* we scale by 1024 for getting more precision */
2105 FT_Pos delta
= (dist
<< 10) / max_width
- (1 << 10);
2109 dist_demerit
= 32000;
2111 dist_demerit
= delta
* delta
/ dist_score
;
2116 dist_demerit
= dist
; /* default if no widths available */
2118 score
= dist_demerit
+ len_score
/ len
;
2120 /* and we search for the smallest score */
2121 if (score
< seg1
->score
)
2123 seg1
->score
= score
;
2127 if (score
< seg2
->score
)
2129 seg2
->score
= score
;
2137 /* now compute the `serif' segments, cf. explanations in `tahints.h' */
2138 for (seg1
= segments
; seg1
< segment_limit
; seg1
++)
2144 if (seg2
->link
!= seg1
)
2147 seg1
->serif
= seg2
->link
;
2154 /* link segments to edges, using feature analysis for selection */
2157 ta_latin_hints_compute_edges(TA_GlyphHints hints
,
2160 TA_AxisHints axis
= &hints
->axis
[dim
];
2161 FT_Error error
= FT_Err_Ok
;
2162 TA_LatinAxis laxis
= &((TA_LatinMetrics
)hints
->metrics
)->axis
[dim
];
2164 TA_StyleClass style_class
= hints
->metrics
->style_class
;
2165 TA_ScriptClass script_class
= ta_script_classes
[style_class
->script
];
2167 FT_Bool top_to_bottom_hinting
= 0;
2169 TA_Segment segments
= axis
->segments
;
2170 TA_Segment segment_limit
= segments
+ axis
->num_segments
;
2174 TA_Direction up_dir
;
2177 FT_Pos edge_distance_threshold
;
2178 FT_Pos segment_length_threshold
;
2179 FT_Pos segment_width_threshold
;
2182 axis
->num_edges
= 0;
2184 scale
= (dim
== TA_DIMENSION_HORZ
) ? hints
->x_scale
2188 up_dir
= (dim
== TA_DIMENSION_HORZ
) ? TA_DIR_UP
2192 if (dim
== TA_DIMENSION_VERT
)
2193 top_to_bottom_hinting
= script_class
->top_to_bottom_hinting
;
2195 /* we ignore all segments that are less than 1 pixel in length */
2196 /* to avoid many problems with serif fonts */
2197 /* (the corresponding threshold is computed in font units) */
2198 if (dim
== TA_DIMENSION_HORZ
)
2199 segment_length_threshold
= FT_DivFix(64, hints
->y_scale
);
2201 segment_length_threshold
= 0;
2204 * Similarly, we ignore segments that have a width delta
2205 * larger than 0.5px (i.e., a width larger than 1px).
2207 segment_width_threshold
= FT_DivFix(32, scale
);
2209 /********************************************************************/
2211 /* We begin by generating a sorted table of edges for the current */
2212 /* direction. To do so, we simply scan each segment and try to find */
2213 /* an edge in our table that corresponds to its position. */
2215 /* If no edge is found, we create and insert a new edge in the */
2216 /* sorted table. Otherwise, we simply add the segment to the edge's */
2217 /* list which gets processed in the second step to compute the */
2218 /* edge's properties. */
2220 /* Note that the table of edges is sorted along the segment/edge */
2223 /********************************************************************/
2225 /* assure that edge distance threshold is at most 0.25px */
2226 edge_distance_threshold
= FT_MulFix(laxis
->edge_distance_threshold
,
2228 if (edge_distance_threshold
> 64 / 4)
2229 edge_distance_threshold
= 64 / 4;
2231 edge_distance_threshold
= FT_DivFix(edge_distance_threshold
,
2234 for (seg
= segments
; seg
< segment_limit
; seg
++)
2236 TA_Edge found
= NULL
;
2240 /* ignore too short segments, too wide ones, and, in this loop, */
2241 /* one-point segments without a direction */
2242 if (seg
->height
< segment_length_threshold
2243 || seg
->delta
> segment_width_threshold
2244 || seg
->dir
== TA_DIR_NONE
)
2247 /* a special case for serif edges: */
2248 /* if they are smaller than 1.5 pixels we ignore them */
2250 && 2 * seg
->height
< 3 * segment_length_threshold
)
2253 /* look for an edge corresponding to the segment */
2254 for (ee
= 0; ee
< axis
->num_edges
; ee
++)
2256 TA_Edge edge
= axis
->edges
+ ee
;
2260 dist
= seg
->pos
- edge
->fpos
;
2264 if (dist
< edge_distance_threshold
&& edge
->dir
== seg
->dir
)
2276 /* insert a new edge in the list and sort according to the position */
2277 error
= ta_axis_hints_new_edge(axis
, seg
->pos
,
2278 (TA_Direction
)seg
->dir
,
2279 top_to_bottom_hinting
,
2284 /* add the segment to the new edge's list */
2285 memset(edge
, 0, sizeof (TA_EdgeRec
));
2288 edge
->dir
= seg
->dir
;
2289 edge
->fpos
= seg
->pos
;
2290 edge
->opos
= FT_MulFix(seg
->pos
, scale
);
2291 edge
->pos
= edge
->opos
;
2292 seg
->edge_next
= seg
;
2296 /* if an edge was found, simply add the segment to the edge's list */
2297 seg
->edge_next
= found
->first
;
2298 found
->last
->edge_next
= seg
;
2303 /* we loop again over all segments to catch one-point segments */
2304 /* without a direction: if possible, link them to existing edges */
2305 for (seg
= segments
; seg
< segment_limit
; seg
++)
2307 TA_Edge found
= NULL
;
2311 if (seg
->dir
!= TA_DIR_NONE
)
2314 /* look for an edge corresponding to the segment */
2315 for (ee
= 0; ee
< axis
->num_edges
; ee
++)
2317 TA_Edge edge
= axis
->edges
+ ee
;
2321 dist
= seg
->pos
- edge
->fpos
;
2325 if (dist
< edge_distance_threshold
)
2332 /* one-point segments without a match are ignored */
2335 seg
->edge_next
= found
->first
;
2336 found
->last
->edge_next
= seg
;
2341 /*****************************************************************/
2343 /* Good, we now compute each edge's properties according to */
2344 /* the segments found on its position. Basically, these are */
2346 /* - the edge's main direction */
2347 /* - stem edge, serif edge or both (which defaults to stem then) */
2348 /* - rounded edge, straight or both (which defaults to straight) */
2349 /* - link for edge */
2351 /*****************************************************************/
2353 /* first of all, set the `edge' field in each segment -- this is */
2354 /* required in order to compute edge links */
2356 /* note that removing this loop and setting the `edge' field of each */
2357 /* segment directly in the code above slows down execution speed for */
2358 /* some reasons on platforms like the Sun */
2360 TA_Edge edges
= axis
->edges
;
2361 TA_Edge edge_limit
= edges
+ axis
->num_edges
;
2365 for (edge
= edges
; edge
< edge_limit
; edge
++)
2372 seg
= seg
->edge_next
;
2373 } while (seg
!= edge
->first
);
2376 /* now compute each edge properties */
2377 for (edge
= edges
; edge
< edge_limit
; edge
++)
2379 FT_Int is_round
= 0; /* does it contain round segments? */
2380 FT_Int is_straight
= 0; /* does it contain straight segments? */
2382 FT_Pos ups
= 0; /* number of upwards segments */
2383 FT_Pos downs
= 0; /* number of downwards segments */
2394 /* check for roundness of segment */
2395 if (seg
->flags
& TA_EDGE_ROUND
)
2401 /* check for segment direction */
2402 if (seg
->dir
== up_dir
)
2403 ups
+= seg
->max_coord
- seg
->min_coord
;
2405 downs
+= seg
->max_coord
- seg
->min_coord
;
2408 /* check for links -- */
2409 /* if seg->serif is set, then seg->link must be ignored */
2410 is_serif
= (FT_Bool
)(seg
->serif
2412 && seg
->serif
->edge
!= edge
);
2414 if ((seg
->link
&& seg
->link
->edge
)
2427 edge2
= edge
->serif
;
2436 edge_delta
= edge
->fpos
- edge2
->fpos
;
2438 edge_delta
= -edge_delta
;
2440 seg_delta
= seg
->pos
- seg2
->pos
;
2442 seg_delta
= -seg_delta
;
2444 if (seg_delta
< edge_delta
)
2452 edge
->serif
= edge2
;
2453 edge2
->flags
|= TA_EDGE_SERIF
;
2459 seg
= seg
->edge_next
;
2460 } while (seg
!= edge
->first
);
2462 /* set the round/straight flags */
2463 edge
->flags
= TA_EDGE_NORMAL
;
2466 && is_round
>= is_straight
)
2467 edge
->flags
|= TA_EDGE_ROUND
;
2470 /* set the edge's main direction */
2471 edge
->dir
= TA_DIR_NONE
;
2474 edge
->dir
= (FT_Char
)up_dir
;
2476 else if (ups
< downs
)
2477 edge
->dir
= (FT_Char
)-up_dir
;
2479 else if (ups
== downs
)
2480 edge
->dir
= 0; /* both up and down! */
2483 /* get rid of serifs if link is set */
2484 /* XXX: this gets rid of many unpleasant artefacts! */
2485 /* example: the `c' in cour.pfa at size 13 */
2487 if (edge
->serif
&& edge
->link
)
2497 /* detect segments and edges for given dimension */
2500 ta_latin_hints_detect_features(TA_GlyphHints hints
,
2501 FT_UInt width_count
,
2502 TA_WidthRec
* widths
,
2508 error
= ta_latin_hints_compute_segments(hints
, dim
);
2511 ta_latin_hints_link_segments(hints
, width_count
, widths
, dim
);
2513 error
= ta_latin_hints_compute_edges(hints
, dim
);
2520 /* compute all edges which lie within blue zones */
2523 ta_latin_hints_compute_blue_edges(TA_GlyphHints hints
,
2524 TA_LatinMetrics metrics
)
2526 TA_AxisHints axis
= &hints
->axis
[TA_DIMENSION_VERT
];
2528 TA_Edge edge
= axis
->edges
;
2529 TA_Edge edge_limit
= edge
+ axis
->num_edges
;
2531 TA_LatinAxis latin
= &metrics
->axis
[TA_DIMENSION_VERT
];
2532 FT_Fixed scale
= latin
->scale
;
2535 /* compute which blue zones are active, */
2536 /* i.e. have their scaled size < 3/4 pixels */
2538 /* for each horizontal edge search the blue zone which is closest */
2539 for (; edge
< edge_limit
; edge
++)
2542 TA_Width best_blue
= NULL
;
2543 FT_Bool best_blue_is_neutral
= 0;
2544 FT_Pos best_dist
; /* initial threshold */
2546 FT_UInt best_blue_idx
= 0;
2547 FT_Bool best_blue_is_shoot
= 0;
2550 /* compute the initial threshold as a fraction of the EM size */
2551 /* (the value 40 is heuristic) */
2552 best_dist
= FT_MulFix(metrics
->units_per_em
/ 40, scale
);
2554 /* assure a minimum distance of 0.5px */
2555 if (best_dist
> 64 / 2)
2558 /* this loop also handles the two extra blue zones */
2559 /* for usWinAscent and usWinDescent */
2560 /* if option `windows-compatibility' is set */
2562 bb
< latin
->blue_count
2563 + (metrics
->root
.globals
->font
->windows_compatibility
? 2 : 0);
2566 TA_LatinBlue blue
= latin
->blues
+ bb
;
2567 FT_Bool is_top_blue
, is_neutral_blue
, is_major_dir
;
2570 /* skip inactive blue zones (i.e., those that are too large) */
2571 if (!(blue
->flags
& TA_LATIN_BLUE_ACTIVE
))
2574 /* if it is a top zone, check for right edges (against the major */
2575 /* direction); if it is a bottom zone, check for left edges (in */
2576 /* the major direction) */
2577 is_top_blue
= (FT_Byte
)((blue
->flags
& (TA_LATIN_BLUE_TOP
2578 | TA_LATIN_BLUE_SUB_TOP
)) != 0);
2579 is_neutral_blue
= (FT_Byte
)((blue
->flags
& TA_LATIN_BLUE_NEUTRAL
) != 0);
2580 is_major_dir
= FT_BOOL(edge
->dir
== axis
->major_dir
);
2582 /* neutral blue zones are handled for both directions */
2583 if (is_top_blue
^ is_major_dir
|| is_neutral_blue
)
2588 /* first of all, compare it to the reference position */
2589 dist
= edge
->fpos
- blue
->ref
.org
;
2593 dist
= FT_MulFix(dist
, scale
);
2594 if (dist
< best_dist
)
2597 best_blue
= &blue
->ref
;
2598 best_blue_is_neutral
= is_neutral_blue
;
2601 best_blue_is_shoot
= 0;
2604 /* now compare it to the overshoot position and check whether */
2605 /* the edge is rounded, and whether the edge is over the */
2606 /* reference position of a top zone, or under the reference */
2607 /* position of a bottom zone (provided we don't have a */
2608 /* neutral blue zone) */
2609 if (edge
->flags
& TA_EDGE_ROUND
2611 && !is_neutral_blue
)
2613 FT_Bool is_under_ref
= FT_BOOL(edge
->fpos
< blue
->ref
.org
);
2616 if (is_top_blue
^ is_under_ref
)
2618 dist
= edge
->fpos
- blue
->shoot
.org
;
2622 dist
= FT_MulFix(dist
, scale
);
2623 if (dist
< best_dist
)
2626 best_blue
= &blue
->shoot
;
2627 best_blue_is_neutral
= is_neutral_blue
;
2630 best_blue_is_shoot
= 1;
2639 edge
->blue_edge
= best_blue
;
2640 edge
->best_blue_idx
= best_blue_idx
;
2641 edge
->best_blue_is_shoot
= best_blue_is_shoot
;
2642 if (best_blue_is_neutral
)
2643 edge
->flags
|= TA_EDGE_NEUTRAL
;
2649 /* initalize hinting engine */
2652 ta_latin_hints_init(TA_GlyphHints hints
,
2653 TA_LatinMetrics metrics
)
2655 FT_Render_Mode mode
;
2656 FT_UInt32 scaler_flags
, other_flags
;
2657 FT_Face face
= metrics
->root
.scaler
.face
;
2660 ta_glyph_hints_rescale(hints
, (TA_StyleMetrics
)metrics
);
2662 /* correct x_scale and y_scale if needed, since they may have */
2663 /* been modified by `ta_latin_metrics_scale_dim' above */
2664 hints
->x_scale
= metrics
->axis
[TA_DIMENSION_HORZ
].scale
;
2665 hints
->x_delta
= metrics
->axis
[TA_DIMENSION_HORZ
].delta
;
2666 hints
->y_scale
= metrics
->axis
[TA_DIMENSION_VERT
].scale
;
2667 hints
->y_delta
= metrics
->axis
[TA_DIMENSION_VERT
].delta
;
2669 /* compute flags depending on render mode, etc. */
2670 mode
= metrics
->root
.scaler
.render_mode
;
2672 #if 0 /* #ifdef TA_CONFIG_OPTION_USE_WARPER */
2673 if (mode
== FT_RENDER_MODE_LCD
2674 || mode
== FT_RENDER_MODE_LCD_V
)
2675 metrics
->root
.scaler
.render_mode
=
2676 mode
= FT_RENDER_MODE_NORMAL
;
2679 scaler_flags
= hints
->scaler_flags
;
2682 /* we snap the width of vertical stems for the monochrome */
2683 /* and horizontal LCD rendering targets only */
2684 if (mode
== FT_RENDER_MODE_MONO
2685 || mode
== FT_RENDER_MODE_LCD
)
2686 other_flags
|= TA_LATIN_HINTS_HORZ_SNAP
;
2688 /* we snap the width of horizontal stems for the monochrome */
2689 /* and vertical LCD rendering targets only */
2690 if (mode
== FT_RENDER_MODE_MONO
2691 || mode
== FT_RENDER_MODE_LCD_V
)
2692 other_flags
|= TA_LATIN_HINTS_VERT_SNAP
;
2694 /* we adjust stems to full pixels unless in `light' or `lcd' mode */
2695 if (mode
!= FT_RENDER_MODE_LIGHT
&& mode
!= FT_RENDER_MODE_LCD
)
2696 other_flags
|= TA_LATIN_HINTS_STEM_ADJUST
;
2698 if (mode
== FT_RENDER_MODE_MONO
)
2699 other_flags
|= TA_LATIN_HINTS_MONO
;
2701 /* in `light' or `lcd' mode we disable horizontal hinting completely; */
2702 /* we also do it if the face is italic -- */
2703 /* however, if warping is enabled (which only works in `light' hinting */
2704 /* mode), advance widths get adjusted, too */
2705 if (mode
== FT_RENDER_MODE_LIGHT
|| mode
== FT_RENDER_MODE_LCD
2706 || (face
->style_flags
& FT_STYLE_FLAG_ITALIC
) != 0)
2707 scaler_flags
|= TA_SCALER_FLAG_NO_HORIZONTAL
;
2709 #ifdef TA_CONFIG_OPTION_USE_WARPER
2710 /* get (global) warper flag */
2711 if (!metrics
->root
.globals
->module
->warping
)
2712 scaler_flags
|= TA_SCALER_FLAG_NO_WARPER
;
2715 hints
->scaler_flags
= scaler_flags
;
2716 hints
->other_flags
= other_flags
;
2722 /* snap a given width in scaled coordinates to */
2723 /* one of the current standard widths */
2726 ta_latin_snap_width(TA_Width widths
,
2731 FT_Pos best
= 64 + 32 + 2;
2732 FT_Pos reference
= width
;
2736 for (n
= 0; n
< count
; n
++)
2753 scaled
= TA_PIX_ROUND(reference
);
2755 if (width
>= reference
)
2757 if (width
< scaled
+ 48)
2762 if (width
> scaled
- 48)
2770 /* compute the snapped width of a given stem, ignoring very thin ones */
2772 /* there is a lot of voodoo in this function; changing the hard-coded */
2773 /* parameters influences the whole hinting process */
2776 ta_latin_compute_stem_width(TA_GlyphHints hints
,
2783 TA_LatinMetrics metrics
= (TA_LatinMetrics
) hints
->metrics
;
2784 TA_LatinAxis axis
= &metrics
->axis
[dim
];
2786 FT_Pos dist
= width
;
2788 FT_Int vertical
= (dim
== TA_DIMENSION_VERT
);
2791 if (!TA_LATIN_HINTS_DO_STEM_ADJUST(hints
)
2792 || axis
->extra_light
)
2801 if ((vertical
&& !TA_LATIN_HINTS_DO_VERT_SNAP(hints
))
2802 || (!vertical
&& !TA_LATIN_HINTS_DO_HORZ_SNAP(hints
)))
2804 /* smooth hinting process: very lightly quantize the stem width */
2806 /* leave the widths of serifs alone */
2807 if ((stem_flags
& TA_EDGE_SERIF
)
2811 else if (base_flags
& TA_EDGE_ROUND
)
2819 if (axis
->width_count
> 0)
2824 /* compare to standard width */
2825 delta
= dist
- axis
->widths
[0].cur
;
2832 dist
= axis
->widths
[0].cur
;
2846 else if (delta
< 32)
2848 else if (delta
< 54)
2855 /* A stem's end position depends on two values: the start */
2856 /* position and the stem length. The former gets usually */
2857 /* rounded to the grid, while the latter gets rounded also if it */
2858 /* exceeds a certain length (see below in this function). This */
2859 /* `double rounding' can lead to a great difference to the */
2860 /* original, unhinted position; this normally doesn't matter for */
2861 /* large PPEM values, but for small sizes it can easily make */
2862 /* outlines collide. For this reason, we adjust the stem length */
2863 /* by a small amount depending on the PPEM value in case the */
2864 /* former and latter rounding both point into the same */
2870 if (((width
> 0) && (base_delta
> 0))
2871 || ((width
< 0) && (base_delta
< 0)))
2873 FT_UInt ppem
= metrics
->root
.scaler
.face
->size
->metrics
.x_ppem
;
2877 bdelta
= base_delta
;
2879 bdelta
= (base_delta
* (FT_Pos
)(30 - ppem
)) / 20;
2885 dist
= (dist
- bdelta
+ 32) & ~63;
2891 /* strong hinting process: snap the stem width to integer pixels */
2893 FT_Pos org_dist
= dist
;
2896 dist
= ta_latin_snap_width(axis
->widths
, axis
->width_count
, dist
);
2900 /* in the case of vertical hinting, */
2901 /* always round the stem heights to integer pixels */
2904 dist
= (dist
+ 16) & ~63;
2910 if (TA_LATIN_HINTS_DO_MONO(hints
))
2912 /* monochrome horizontal hinting: */
2913 /* snap widths to integer pixels with a different threshold */
2918 dist
= (dist
+ 32) & ~63;
2922 /* for horizontal anti-aliased hinting, we adopt a more subtle */
2923 /* approach: we strengthen small stems, round stems whose size */
2924 /* is between 1 and 2 pixels to an integer, otherwise nothing */
2927 dist
= (dist
+ 64) >> 1;
2929 else if (dist
< 128)
2931 /* we only round to an integer width if the corresponding */
2932 /* distortion is less than 1/4 pixel -- otherwise, this */
2933 /* makes everything worse since the diagonals, which are */
2934 /* not hinted, appear a lot bolder or thinner than the */
2935 /* vertical stems */
2940 dist
= (dist
+ 22) & ~63;
2941 delta
= dist
- org_dist
;
2949 dist
= (dist
+ 64) >> 1;
2953 /* round otherwise to prevent color fringes in LCD mode */
2954 dist
= (dist
+ 32) & ~63;
2967 /* align one stem edge relative to the previous stem edge */
2970 ta_latin_align_linked_edge(TA_GlyphHints hints
,
2975 FT_Pos dist
, base_delta
;
2976 FT_Pos fitted_width
;
2979 dist
= stem_edge
->opos
- base_edge
->opos
;
2980 base_delta
= base_edge
->pos
- base_edge
->opos
;
2983 fitted_width
= ta_latin_compute_stem_width(hints
, dim
,
2988 stem_edge
->pos
= base_edge
->pos
+ fitted_width
;
2990 TA_LOG((" LINK: edge %d (opos=%.2f) linked to %.2f,"
2991 " dist was %.2f, now %.2f\n",
2992 stem_edge
- hints
->axis
[dim
].edges
, stem_edge
->opos
/ 64.0,
2993 stem_edge
->pos
/ 64.0, dist
/ 64.0, fitted_width
/ 64.0));
2995 if (hints
->recorder
)
2996 hints
->recorder(ta_link
, hints
, dim
,
2997 base_edge
, stem_edge
, NULL
, NULL
, NULL
);
3001 /* shift the coordinates of the `serif' edge by the same amount */
3002 /* as the corresponding `base' edge has been moved already */
3005 ta_latin_align_serif_edge(TA_GlyphHints hints
,
3011 serif
->pos
= base
->pos
+ (serif
->opos
- base
->opos
);
3015 /* the main grid-fitting routine */
3018 ta_latin_hint_edges(TA_GlyphHints hints
,
3021 TA_AxisHints axis
= &hints
->axis
[dim
];
3023 TA_Edge edges
= axis
->edges
;
3024 TA_Edge edge_limit
= edges
+ axis
->num_edges
;
3028 TA_Edge anchor
= NULL
;
3029 FT_Int has_serifs
= 0;
3031 TA_StyleClass style_class
= hints
->metrics
->style_class
;
3032 TA_ScriptClass script_class
= ta_script_classes
[style_class
->script
];
3034 FT_Bool top_to_bottom_hinting
= 0;
3037 FT_UInt num_actions
= 0;
3040 TA_LOG(("latin %s edge hinting (style `%s')\n",
3041 dim
== TA_DIMENSION_VERT
? "horizontal" : "vertical",
3042 ta_style_names
[hints
->metrics
->style_class
->style
]));
3044 if (dim
== TA_DIMENSION_VERT
)
3045 top_to_bottom_hinting
= script_class
->top_to_bottom_hinting
;
3047 /* we begin by aligning all stems relative to the blue zone if needed -- */
3048 /* that's only for horizontal edges */
3050 if (dim
== TA_DIMENSION_VERT
3051 && TA_HINTS_DO_BLUES(hints
))
3053 for (edge
= edges
; edge
< edge_limit
; edge
++)
3056 TA_Edge edge1
, edge2
; /* these edges form the stem to check */
3059 if (edge
->flags
& TA_EDGE_DONE
)
3066 * If a stem contains both a neutral and a non-neutral blue zone,
3067 * skip the neutral one. Otherwise, outlines with different
3068 * directions might be incorrectly aligned at the same vertical
3071 * If we have two neutral blue zones, skip one of them.
3073 if (edge
->blue_edge
&& edge2
&& edge2
->blue_edge
)
3075 FT_Byte neutral
= edge
->flags
& TA_EDGE_NEUTRAL
;
3076 FT_Byte neutral2
= edge2
->flags
& TA_EDGE_NEUTRAL
;
3081 edge2
->blue_edge
= NULL
;
3082 edge2
->flags
&= ~TA_EDGE_NEUTRAL
;
3086 edge
->blue_edge
= NULL
;
3087 edge
->flags
&= ~TA_EDGE_NEUTRAL
;
3091 blue
= edge
->blue_edge
;
3095 /* flip edges if the other edge is aligned to a blue zone */
3096 else if (edge2
&& edge2
->blue_edge
)
3098 blue
= edge2
->blue_edge
;
3108 TA_LOG((" BLUE_ANCHOR: edge %d (opos=%.2f) snapped to %.2f,"
3109 " was %.2f (anchor=edge %d)\n",
3110 edge1
- edges
, edge1
->opos
/ 64.0, blue
->fit
/ 64.0,
3111 edge1
->pos
/ 64.0, edge
- edges
));
3113 TA_LOG((" BLUE: edge %d (opos=%.2f) snapped to %.2f, was %.2f\n",
3114 edge1
- edges
, edge1
->opos
/ 64.0, blue
->fit
/ 64.0,
3115 edge1
->pos
/ 64.0));
3120 edge1
->pos
= blue
->fit
;
3121 edge1
->flags
|= TA_EDGE_DONE
;
3123 if (hints
->recorder
)
3126 hints
->recorder(ta_blue_anchor
, hints
, dim
,
3127 edge1
, edge
, NULL
, NULL
, NULL
);
3129 hints
->recorder(ta_blue
, hints
, dim
,
3130 edge1
, NULL
, NULL
, NULL
, NULL
);
3133 if (edge2
&& !edge2
->blue_edge
)
3135 ta_latin_align_linked_edge(hints
, dim
, edge1
, edge2
);
3136 edge2
->flags
|= TA_EDGE_DONE
;
3148 /* now we align all other stem edges, */
3149 /* trying to maintain the relative order of stems in the glyph */
3150 for (edge
= edges
; edge
< edge_limit
; edge
++)
3155 if (edge
->flags
& TA_EDGE_DONE
)
3158 /* skip all non-stem edges */
3166 /* now align the stem */
3168 /* this should not happen, but it's better to be safe */
3169 if (edge2
->blue_edge
)
3171 TA_LOG((" ASSERTION FAILED for edge %d\n", edge2
- edges
));
3173 ta_latin_align_linked_edge(hints
, dim
, edge2
, edge
);
3174 edge
->flags
|= TA_EDGE_DONE
;
3184 /* if we reach this if clause, no stem has been aligned yet */
3186 FT_Pos org_len
, org_center
, cur_len
;
3187 FT_Pos cur_pos1
, error1
, error2
, u_off
, d_off
;
3190 org_len
= edge2
->opos
- edge
->opos
;
3191 cur_len
= ta_latin_compute_stem_width(hints
, dim
,
3193 edge
->flags
, edge2
->flags
);
3195 /* some voodoo to specially round edges for small stem widths; */
3196 /* the idea is to align the center of a stem, */
3197 /* then shifting the stem edges to suitable positions */
3206 /* 1px < width < 1.5px */
3213 org_center
= edge
->opos
+ (org_len
>> 1);
3214 cur_pos1
= TA_PIX_ROUND(org_center
);
3216 error1
= org_center
- (cur_pos1
- u_off
);
3220 error2
= org_center
- (cur_pos1
+ d_off
);
3224 if (error1
< error2
)
3229 edge
->pos
= cur_pos1
- cur_len
/ 2;
3230 edge2
->pos
= edge
->pos
+ cur_len
;
3233 edge
->pos
= TA_PIX_ROUND(edge
->opos
);
3236 edge
->flags
|= TA_EDGE_DONE
;
3238 TA_LOG((" ANCHOR: edge %d (opos=%.2f) and %d (opos=%.2f)"
3239 " snapped to %.2f and %.2f\n",
3240 edge
- edges
, edge
->opos
/ 64.0,
3241 edge2
- edges
, edge2
->opos
/ 64.0,
3242 edge
->pos
/ 64.0, edge2
->pos
/ 64.0));
3244 if (hints
->recorder
)
3245 hints
->recorder(ta_anchor
, hints
, dim
,
3246 edge
, edge2
, NULL
, NULL
, NULL
);
3248 ta_latin_align_linked_edge(hints
, dim
, edge
, edge2
);
3256 FT_Pos org_pos
, org_len
, org_center
, cur_len
;
3257 FT_Pos cur_pos1
, cur_pos2
, delta1
, delta2
;
3260 org_pos
= anchor
->pos
+ (edge
->opos
- anchor
->opos
);
3261 org_len
= edge2
->opos
- edge
->opos
;
3262 org_center
= org_pos
+ (org_len
>> 1);
3264 cur_len
= ta_latin_compute_stem_width(hints
, dim
,
3266 edge
->flags
, edge2
->flags
);
3268 if (edge2
->flags
& TA_EDGE_DONE
)
3270 TA_LOG((" ADJUST: edge %d (pos=%.2f) moved to %.2f\n",
3271 edge
- edges
, edge
->pos
/ 64.0,
3272 (edge2
->pos
- cur_len
) / 64.0));
3274 edge
->pos
= edge2
->pos
- cur_len
;
3276 if (hints
->recorder
)
3278 TA_Edge bound
= NULL
;
3284 hints
->recorder(ta_adjust
, hints
, dim
,
3285 edge
, edge2
, NULL
, bound
, NULL
);
3289 else if (cur_len
< 96)
3291 FT_Pos u_off
, d_off
;
3294 cur_pos1
= TA_PIX_ROUND(org_center
);
3307 delta1
= org_center
- (cur_pos1
- u_off
);
3311 delta2
= org_center
- (cur_pos1
+ d_off
);
3315 if (delta1
< delta2
)
3320 edge
->pos
= cur_pos1
- cur_len
/ 2;
3321 edge2
->pos
= cur_pos1
+ cur_len
/ 2;
3323 TA_LOG((" STEM: edge %d (opos=%.2f) linked to %d (opos=%.2f)"
3324 " snapped to %.2f and %.2f\n",
3325 edge
- edges
, edge
->opos
/ 64.0,
3326 edge2
- edges
, edge2
->opos
/ 64.0,
3327 edge
->pos
/ 64.0, edge2
->pos
/ 64.0));
3329 if (hints
->recorder
)
3331 TA_Edge bound
= NULL
;
3337 hints
->recorder(ta_stem
, hints
, dim
,
3338 edge
, edge2
, NULL
, bound
, NULL
);
3344 org_pos
= anchor
->pos
+ (edge
->opos
- anchor
->opos
);
3345 org_len
= edge2
->opos
- edge
->opos
;
3346 org_center
= org_pos
+ (org_len
>> 1);
3348 cur_len
= ta_latin_compute_stem_width(hints
, dim
,
3350 edge
->flags
, edge2
->flags
);
3352 cur_pos1
= TA_PIX_ROUND(org_pos
);
3353 delta1
= cur_pos1
+ (cur_len
>> 1) - org_center
;
3357 cur_pos2
= TA_PIX_ROUND(org_pos
+ org_len
) - cur_len
;
3358 delta2
= cur_pos2
+ (cur_len
>> 1) - org_center
;
3362 edge
->pos
= (delta1
< delta2
) ? cur_pos1
: cur_pos2
;
3363 edge2
->pos
= edge
->pos
+ cur_len
;
3365 TA_LOG((" STEM: edge %d (opos=%.2f) linked to %d (opos=%.2f)"
3366 " snapped to %.2f and %.2f\n",
3367 edge
- edges
, edge
->opos
/ 64.0,
3368 edge2
- edges
, edge2
->opos
/ 64.0,
3369 edge
->pos
/ 64.0, edge2
->pos
/ 64.0));
3371 if (hints
->recorder
)
3373 TA_Edge bound
= NULL
;
3379 hints
->recorder(ta_stem
, hints
, dim
,
3380 edge
, edge2
, NULL
, bound
, NULL
);
3388 edge
->flags
|= TA_EDGE_DONE
;
3389 edge2
->flags
|= TA_EDGE_DONE
;
3392 && (top_to_bottom_hinting
? (edge
->pos
> edge
[-1].pos
)
3393 : (edge
->pos
< edge
[-1].pos
)))
3395 /* don't move if stem would (almost) disappear otherwise; */
3396 /* the ad-hoc value 16 corresponds to 1/4px */
3398 && TA_ABS(edge
->link
->pos
- edge
[-1].pos
) > 16)
3401 TA_LOG((" BOUND: edge %d (pos=%.2f) moved to %.2f\n",
3402 edge
- edges
, edge
->pos
/ 64.0, edge
[-1].pos
/ 64.0));
3407 edge
->pos
= edge
[-1].pos
;
3409 if (hints
->recorder
)
3410 hints
->recorder(ta_bound
, hints
, dim
,
3411 edge
, &edge
[-1], NULL
, NULL
, NULL
);
3417 /* make sure that lowercase m's maintain their symmetry */
3419 /* In general, lowercase m's have six vertical edges if they are sans */
3420 /* serif, or twelve if they are with serifs. This implementation is */
3421 /* based on that assumption, and seems to work very well with most */
3422 /* faces. However, if for a certain face this assumption is not */
3423 /* true, the m is just rendered like before. In addition, any stem */
3424 /* correction will only be applied to symmetrical glyphs (even if the */
3425 /* glyph is not an m), so the potential for unwanted distortion is */
3426 /* relatively low. */
3428 /* we don't handle horizontal edges since we can't easily assure that */
3429 /* the third (lowest) stem aligns with the base line; it might end up */
3430 /* one pixel higher or lower */
3432 n_edges
= edge_limit
- edges
;
3433 if (dim
== TA_DIMENSION_HORZ
3434 && (n_edges
== 6 || n_edges
== 12))
3436 TA_Edge edge1
, edge2
, edge3
;
3437 FT_Pos dist1
, dist2
, span
, delta
;
3453 dist1
= edge2
->opos
- edge1
->opos
;
3454 dist2
= edge3
->opos
- edge2
->opos
;
3456 span
= dist1
- dist2
;
3462 delta
= edge3
->pos
- (2 * edge2
->pos
- edge1
->pos
);
3463 edge3
->pos
-= delta
;
3465 edge3
->link
->pos
-= delta
;
3467 /* move the serifs along with the stem */
3470 (edges
+ 8)->pos
-= delta
;
3471 (edges
+ 11)->pos
-= delta
;
3474 edge3
->flags
|= TA_EDGE_DONE
;
3476 edge3
->link
->flags
|= TA_EDGE_DONE
;
3480 if (has_serifs
|| !anchor
)
3482 /* now hint the remaining edges (serifs and single) */
3483 /* in order to complete our processing */
3484 for (edge
= edges
; edge
< edge_limit
; edge
++)
3486 TA_Edge lower_bound
= NULL
;
3487 TA_Edge upper_bound
= NULL
;
3492 if (edge
->flags
& TA_EDGE_DONE
)
3499 delta
= edge
->serif
->opos
- edge
->opos
;
3505 lower_bound
= &edge
[-1];
3507 if (edge
+ 1 < edge_limit
3508 && edge
[1].flags
& TA_EDGE_DONE
)
3509 upper_bound
= &edge
[1];
3512 if (delta
< 64 + 16)
3514 ta_latin_align_serif_edge(hints
, edge
->serif
, edge
);
3516 TA_LOG((" SERIF: edge %d (opos=%.2f) serif to %d (opos=%.2f)"
3517 " aligned to %.2f\n",
3518 edge
- edges
, edge
->opos
/ 64.0,
3519 edge
->serif
- edges
, edge
->serif
->opos
/ 64.0,
3522 if (hints
->recorder
)
3523 hints
->recorder(ta_serif
, hints
, dim
,
3524 edge
, NULL
, NULL
, lower_bound
, upper_bound
);
3528 edge
->pos
= TA_PIX_ROUND(edge
->opos
);
3531 TA_LOG((" SERIF_ANCHOR: edge %d (opos=%.2f) snapped to %.2f\n",
3532 edge
- edges
, edge
->opos
/ 64.0, edge
->pos
/ 64.0));
3534 if (hints
->recorder
)
3535 hints
->recorder(ta_serif_anchor
, hints
, dim
,
3536 edge
, NULL
, NULL
, lower_bound
, upper_bound
);
3540 TA_Edge before
, after
;
3543 for (before
= edge
- 1; before
>= edges
; before
--)
3544 if (before
->flags
& TA_EDGE_DONE
)
3547 for (after
= edge
+ 1; after
< edge_limit
; after
++)
3548 if (after
->flags
& TA_EDGE_DONE
)
3551 if (before
>= edges
&& before
< edge
3552 && after
< edge_limit
&& after
> edge
)
3554 if (after
->opos
== before
->opos
)
3555 edge
->pos
= before
->pos
;
3557 edge
->pos
= before
->pos
+ FT_MulDiv(edge
->opos
- before
->opos
,
3558 after
->pos
- before
->pos
,
3559 after
->opos
- before
->opos
);
3561 TA_LOG((" SERIF_LINK1: edge %d (opos=%.2f) snapped to %.2f"
3562 " from %d (opos=%.2f)\n",
3563 edge
- edges
, edge
->opos
/ 64.0,
3565 before
- edges
, before
->opos
/ 64.0));
3567 if (hints
->recorder
)
3568 hints
->recorder(ta_serif_link1
, hints
, dim
,
3569 edge
, before
, after
, lower_bound
, upper_bound
);
3573 edge
->pos
= anchor
->pos
+ ((edge
->opos
- anchor
->opos
+ 16) & ~31);
3574 TA_LOG((" SERIF_LINK2: edge %d (opos=%.2f) snapped to %.2f\n",
3575 edge
- edges
, edge
->opos
/ 64.0, edge
->pos
/ 64.0));
3577 if (hints
->recorder
)
3578 hints
->recorder(ta_serif_link2
, hints
, dim
,
3579 edge
, NULL
, NULL
, lower_bound
, upper_bound
);
3586 edge
->flags
|= TA_EDGE_DONE
;
3589 && (top_to_bottom_hinting
? (edge
->pos
> edge
[-1].pos
)
3590 : (edge
->pos
< edge
[-1].pos
)))
3592 /* don't move if stem would (almost) disappear otherwise; */
3593 /* the ad-hoc value 16 corresponds to 1/4px */
3595 && TA_ABS(edge
->link
->pos
- edge
[-1].pos
) > 16)
3598 TA_LOG((" BOUND: edge %d (pos=%.2f) moved to %.2f\n",
3599 edge
- edges
, edge
->pos
/ 64.0, edge
[-1].pos
/ 64.0));
3603 edge
->pos
= edge
[-1].pos
;
3605 if (hints
->recorder
)
3606 hints
->recorder(ta_bound
, hints
, dim
,
3607 edge
, &edge
[-1], NULL
, NULL
, NULL
);
3611 if (edge
+ 1 < edge_limit
3612 && edge
[1].flags
& TA_EDGE_DONE
3613 && (top_to_bottom_hinting
? (edge
->pos
< edge
[1].pos
)
3614 : (edge
->pos
> edge
[1].pos
)))
3617 /* don't move if stem would (almost) disappear otherwise; */
3618 /* the ad-hoc value 16 corresponds to 1/4px */
3620 && TA_ABS(edge
->link
->pos
- edge
[-1].pos
) > 16)
3623 TA_LOG((" BOUND: edge %d (pos=%.2f) moved to %.2f\n",
3624 edge
- edges
, edge
->pos
/ 64.0, edge
[1].pos
/ 64.0));
3629 edge
->pos
= edge
[1].pos
;
3631 if (hints
->recorder
)
3632 hints
->recorder(ta_bound
, hints
, dim
,
3633 edge
, &edge
[1], NULL
, NULL
, NULL
);
3641 TA_LOG((" (none)\n"));
3647 /* apply the complete hinting algorithm to a latin glyph */
3650 ta_latin_hints_apply(FT_UInt glyph_index
,
3651 TA_GlyphHints hints
,
3652 FT_Outline
* outline
,
3653 TA_LatinMetrics metrics
)
3661 error
= ta_glyph_hints_reload(hints
, outline
);
3665 /* analyze glyph outline */
3666 if (TA_HINTS_DO_HORIZONTAL(hints
))
3668 axis
= &metrics
->axis
[TA_DIMENSION_HORZ
];
3669 error
= ta_latin_hints_detect_features(hints
,
3677 if (TA_HINTS_DO_VERTICAL(hints
))
3679 axis
= &metrics
->axis
[TA_DIMENSION_VERT
];
3680 error
= ta_latin_hints_detect_features(hints
,
3687 /* apply blue zones to base characters only */
3688 if (!(metrics
->root
.globals
->glyph_styles
[glyph_index
] & TA_NONBASE
))
3689 ta_latin_hints_compute_blue_edges(hints
, metrics
);
3692 /* grid-fit the outline */
3693 for (dim
= 0; dim
< TA_DIMENSION_MAX
; dim
++)
3695 #ifdef TA_CONFIG_OPTION_USE_WARPER
3696 if (dim
== TA_DIMENSION_HORZ
3697 && metrics
->root
.scaler
.render_mode
== FT_RENDER_MODE_NORMAL
3698 && TA_HINTS_DO_WARP(hints
))
3700 TA_WarperRec warper
;
3705 ta_warper_compute(&warper
, hints
, (TA_Dimension
)dim
, &scale
, &delta
);
3706 ta_glyph_hints_scale_dim(hints
, (TA_Dimension
)dim
, scale
, delta
);
3710 #endif /* TA_CONFIG_OPTION_USE_WARPER */
3712 if ((dim
== TA_DIMENSION_HORZ
&& TA_HINTS_DO_HORIZONTAL(hints
))
3713 || (dim
== TA_DIMENSION_VERT
&& TA_HINTS_DO_VERTICAL(hints
)))
3715 ta_latin_hint_edges(hints
, (TA_Dimension
)dim
);
3716 ta_glyph_hints_align_edge_points(hints
, (TA_Dimension
)dim
);
3717 ta_glyph_hints_align_strong_points(hints
, (TA_Dimension
)dim
);
3718 ta_glyph_hints_align_weak_points(hints
, (TA_Dimension
)dim
);
3722 ta_glyph_hints_save(hints
, outline
);
3729 const TA_WritingSystemClassRec ta_latin_writing_system_class
=
3731 TA_WRITING_SYSTEM_LATIN
,
3733 sizeof (TA_LatinMetricsRec
),
3735 (TA_WritingSystem_InitMetricsFunc
)ta_latin_metrics_init
, /* style_metrics_init */
3736 (TA_WritingSystem_ScaleMetricsFunc
)ta_latin_metrics_scale
, /* style_metrics_scale */
3737 (TA_WritingSystem_DoneMetricsFunc
)NULL
, /* style_metrics_done */
3739 (TA_WritingSystem_InitHintsFunc
)ta_latin_hints_init
, /* style_hints_init */
3740 (TA_WritingSystem_ApplyHintsFunc
)ta_latin_hints_apply
/* style_hints_apply */
3743 /* end of talatin.c */