4 * Copyright (C) 2011-2017 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 `afhints.h' (2011-Mar-28) from FreeType */
18 /* heavily modified 2011 by Werner Lemberg <wl@gnu.org> */
29 #define xxTA_SORT_SEGMENTS
32 /* the definition of outline glyph hints; these are shared */
33 /* by all writing system analysis routines (until now) */
35 typedef enum TA_Dimension_
37 TA_DIMENSION_HORZ
= 0, /* x coordinates, i.e. vert. segments & edges */
38 TA_DIMENSION_VERT
= 1, /* y coordinates, i.e. horz. segments & edges */
39 TA_DIMENSION_MAX
/* do not remove */
43 /* hint directions -- the values are computed so that two vectors */
44 /* are in opposite directions iff `dir1 + dir2 == 0' */
46 typedef enum TA_Direction_
57 * The following explanations are mostly taken from the article
59 * Real-Time Grid Fitting of Typographic Outlines
61 * by David Turner and Werner Lemberg
63 * http://www.tug.org/TUGboat/Articles/tb24-3/lemberg.pdf
65 * with appropriate updates.
70 * `ta_{cjk,latin,...}_hints_compute_segments' are the functions to
71 * find segments in an outline.
73 * A segment is a series of at least two consecutive points that are
74 * approximately aligned along a coordinate axis. The analysis to do
75 * so is specific to a writing system.
80 * `ta_{cjk,latin,...}_hints_compute_edges' are the functions to find
83 * As soon as segments are defined, the auto-hinter groups them into
84 * edges. An edge corresponds to a single position on the main
85 * dimension that collects one or more segments (allowing for a small
88 * As an example, the `latin' writing system first tries to grid-fit
89 * edges, then to align segments on the edges unless it detects that
99 * +------<-----+ +-----<------+
103 * +--------------->------------------+
109 * Stems are detected by `ta_{cjk,latin,...}_hint_edges'.
111 * Segments need to be `linked' to other ones in order to detect stems.
112 * A stem is made of two segments that face each other in opposite
113 * directions and that are sufficiently close to each other. Using
114 * vocabulary from the TrueType specification, stem segments form a
117 * In the above ASCII drawing, the horizontal segments are BC, DE, and
118 * FG; the vertical segments are AB, CD, EF, and GH.
120 * Each segment has at most one `best' candidate to form a black
121 * distance, or no candidate at all. Notice that two distinct segments
122 * can have the same candidate, which frequently means a serif.
124 * A stem is recognized by the following condition:
126 * best segment_1 = segment_2 && best segment_2 = segment_1
128 * The best candidate is stored in field `link' in structure
131 * In the above ASCII drawing, the best candidate for both AB and CD is
132 * GH, while the best candidate for GH is AB. Similarly, the best
133 * candidate for EF and GH is AB, while the best candidate for AB is
136 * The detection and handling of stems is dependent on the writing
142 * Serifs are detected by `ta_{cjk,latin,...}_hint_edges'.
144 * In comparison to a stem, a serif (as handled by the auto-hinter
145 * module that takes care of the `latin' writing system) has
147 * best segment_1 = segment_2 && best segment_2 != segment_1
149 * where segment_1 corresponds to the serif segment (CD and EF in the
150 * above ASCII drawing).
152 * The best candidate is stored in field `serif' in structure
153 * `TA_Segment' (and `link' is set to NULL).
158 * A point is called `touched' if it has been processed somehow by the
159 * auto-hinter. It basically means that it shouldn't be moved again
160 * (or moved only under certain constraints to preserve the already
161 * applied processing).
164 * Flat and round segments
166 * Segments are `round' or `flat', depending on the series of points
167 * that define them. A segment is round if the next and previous point
168 * of an extremum (which can be either a single point or sequence of
169 * points) are both conic or cubic control points. Otherwise, a
170 * segment with an extremum is flat.
175 * Experience has shown that points not part of an edge need to be
176 * interpolated linearly between their two closest edges, even if these
177 * are not part of the contour of those particular points. Typical
178 * candidates for this are
180 * - angle points (i.e., points where the `in' and `out' direction
183 * - inflection points (i.e., where the `in' and `out' angles are the
184 * same, but the curvature changes sign) [currently, such points
185 * aren't handled specially in the auto-hinter]
187 * `ta_glyph_hints_align_strong_points' is the function that takes
188 * care of such situations; it is equivalent to the TrueType `IP'
189 * hinting instruction.
194 * Other points in the outline must be interpolated using the
195 * coordinates of their previous and next unfitted contour neighbours.
196 * These are called `weak points' and are touched by the function
197 * `ta_glyph_hints_align_weak_points', equivalent to the TrueType `IUP'
198 * hinting instruction. Typical candidates are control points and
199 * points on the contour without a major direction.
201 * The major effect is to reduce possible distortion caused by
202 * alignment of edges and strong points, thus weak points are processed
203 * after strong points.
207 /* point hint flags */
208 #define TA_FLAG_NONE 0
210 /* point type flags */
211 #define TA_FLAG_CONIC (1U << 0)
212 #define TA_FLAG_CUBIC (1U << 1)
213 #define TA_FLAG_CONTROL (TA_FLAG_CONIC | TA_FLAG_CUBIC)
215 /* point touch flags */
216 #define TA_FLAG_TOUCH_X (1U << 2)
217 #define TA_FLAG_TOUCH_Y (1U << 3)
219 /* candidates for weak interpolation have this flag set */
220 #define TA_FLAG_WEAK_INTERPOLATION (1U << 4)
223 /* edge hint flags */
224 #define TA_EDGE_NORMAL 0
225 #define TA_EDGE_ROUND (1U << 0)
226 #define TA_EDGE_SERIF (1U << 1)
227 #define TA_EDGE_DONE (1U << 2)
228 #define TA_EDGE_NEUTRAL (1U << 3)
231 typedef struct TA_PointRec_
* TA_Point
;
232 typedef struct TA_SegmentRec_
* TA_Segment
;
233 typedef struct TA_EdgeRec_
* TA_Edge
;
236 typedef struct TA_PointRec_
238 FT_UShort flags
; /* point flags used by hinter */
239 FT_Char in_dir
; /* direction of inwards vector */
240 FT_Char out_dir
; /* direction of outwards vector */
242 FT_Pos ox
, oy
; /* original, scaled position */
243 FT_Short fx
, fy
; /* original, unscaled position (in font units) */
244 FT_Pos x
, y
; /* current position */
245 FT_Pos u
, v
; /* current (x,y) or (y,x) depending on context */
247 FT_Short left_offset
; /* left offset in one-point segments */
248 FT_Short right_offset
; /* right offset in one-point segments */
250 TA_Point next
; /* next point in contour */
251 TA_Point prev
; /* previous point in contour */
255 typedef struct TA_SegmentRec_
257 FT_Byte flags
; /* edge/segment flags for this segment */
258 FT_Char dir
; /* segment direction */
259 FT_Short pos
; /* position of segment */
260 FT_Short delta
; /* deviation from segment position */
261 FT_Short min_coord
; /* minimum coordinate of segment */
262 FT_Short max_coord
; /* maximum coordinate of segment */
263 FT_Short height
; /* the hinted segment height */
265 TA_Edge edge
; /* the segment's parent edge */
266 TA_Segment edge_next
; /* link to next segment in parent edge */
268 TA_Segment link
; /* (stem) link segment */
269 TA_Segment serif
; /* primary segment for serifs */
270 FT_Pos score
; /* used during stem matching */
271 FT_Pos len
; /* used during stem matching */
273 TA_Point first
; /* first point in edge segment */
274 TA_Point last
; /* last point in edge segment */
278 typedef struct TA_EdgeRec_
280 FT_Short fpos
; /* original, unscaled position (in font units) */
281 FT_Pos opos
; /* original, scaled position */
282 FT_Pos pos
; /* current position */
284 FT_Byte flags
; /* edge flags */
285 FT_Char dir
; /* edge direction */
286 FT_Fixed scale
; /* used to speed up interpolation between edges */
288 TA_Width blue_edge
; /* non-NULL if this is a blue edge */
289 FT_UInt best_blue_idx
; /* for the hinting recorder callback */
290 FT_Bool best_blue_is_shoot
; /* for the hinting recorder callback */
292 TA_Edge link
; /* link edge */
293 TA_Edge serif
; /* primary edge for serifs */
294 FT_Int score
; /* used during stem matching */
296 TA_Segment first
; /* first segment in edge */
297 TA_Segment last
; /* last segment in edge */
301 #define TA_SEGMENTS_EMBEDDED 18 /* number of embedded segments */
302 #define TA_EDGES_EMBEDDED 12 /* number of embedded edges */
304 typedef struct TA_AxisHintsRec_
306 FT_Int num_segments
; /* number of used segments */
307 FT_Int max_segments
; /* number of allocated segments */
308 TA_Segment segments
; /* segments array */
309 #ifdef TA_SORT_SEGMENTS
313 FT_Int num_edges
; /* number of used edges */
314 FT_Int max_edges
; /* number of allocated edges */
315 TA_Edge edges
; /* edges array */
317 TA_Direction major_dir
; /* either vertical or horizontal */
319 /* two arrays to avoid allocation penalty */
322 TA_SegmentRec segments
[TA_SEGMENTS_EMBEDDED
];
323 TA_EdgeRec edges
[TA_EDGES_EMBEDDED
];
325 } TA_AxisHintsRec
, *TA_AxisHints
;
328 typedef enum TA_Action_
345 ta_anchor_round_serif
,
350 ta_adjust_round_serif
,
352 ta_adjust_bound_serif
,
353 ta_adjust_bound_round
,
354 ta_adjust_bound_round_serif
,
355 ta_adjust_down_bound
,
356 ta_adjust_down_bound_serif
,
357 ta_adjust_down_bound_round
,
358 ta_adjust_down_bound_round_serif
,
372 ta_stem_bound_round_serif
,
374 ta_stem_down_bound_serif
,
375 ta_stem_down_bound_round
,
376 ta_stem_down_bound_round_serif
,
379 ta_serif_lower_bound
,
380 ta_serif_upper_bound
,
381 ta_serif_upper_lower_bound
,
382 ta_serif_down_lower_bound
,
383 ta_serif_down_upper_bound
,
384 ta_serif_down_upper_lower_bound
,
387 ta_serif_anchor_lower_bound
,
388 ta_serif_anchor_upper_bound
,
389 ta_serif_anchor_upper_lower_bound
,
390 ta_serif_anchor_down_lower_bound
,
391 ta_serif_anchor_down_upper_bound
,
392 ta_serif_anchor_down_upper_lower_bound
,
395 ta_serif_link1_lower_bound
,
396 ta_serif_link1_upper_bound
,
397 ta_serif_link1_upper_lower_bound
,
398 ta_serif_link1_down_lower_bound
,
399 ta_serif_link1_down_upper_bound
,
400 ta_serif_link1_down_upper_lower_bound
,
403 ta_serif_link2_lower_bound
,
404 ta_serif_link2_upper_bound
,
405 ta_serif_link2_upper_lower_bound
,
406 ta_serif_link2_down_lower_bound
,
407 ta_serif_link2_down_upper_bound
,
408 ta_serif_link2_down_upper_lower_bound
,
415 (*TA_Hints_Recorder
)(TA_Action action
,
418 void* arg1
, /* TA_Point or TA_Edge */
422 TA_Edge upper_bound
);
425 #define TA_POINTS_EMBEDDED 96 /* number of embedded points */
426 #define TA_CONTOURS_EMBEDDED 8 /* number of embedded contours */
428 typedef struct TA_GlyphHintsRec_
436 FT_Int max_points
; /* number of allocated points */
437 FT_Int num_points
; /* number of used points */
438 TA_Point points
; /* points array */
440 FT_Int max_contours
; /* number of allocated contours */
441 FT_Int num_contours
; /* number of used contours */
442 TA_Point
* contours
; /* contours array */
444 TA_AxisHintsRec axis
[TA_DIMENSION_MAX
];
446 FT_UInt32 scaler_flags
; /* copy of scaler flags */
447 FT_UInt32 other_flags
; /* free for style-specific implementations */
448 TA_StyleMetrics metrics
;
450 FT_Pos xmin_delta
; /* used for warping */
453 TA_Hints_Recorder recorder
;
456 /* two arrays to avoid allocation penalty; */
457 /* the `embedded' structure must be the last element! */
460 TA_Point contours
[TA_CONTOURS_EMBEDDED
];
461 TA_PointRec points
[TA_POINTS_EMBEDDED
];
466 #define TA_HINTS_TEST_SCALER(h, f) \
467 ((h)->scaler_flags & (f))
468 #define TA_HINTS_TEST_OTHER(h, f) \
469 ((h)->other_flags & (f))
474 #define TA_HINTS_DO_HORIZONTAL(h) \
475 (!_ta_debug_disable_horz_hints \
476 && !TA_HINTS_TEST_SCALER(h, TA_SCALER_FLAG_NO_HORIZONTAL))
478 #define TA_HINTS_DO_VERTICAL(h) \
479 (!_ta_debug_disable_vert_hints \
480 && !TA_HINTS_TEST_SCALER(h, TA_SCALER_FLAG_NO_VERTICAL))
482 #define TA_HINTS_DO_BLUES(h) \
483 (!_ta_debug_disable_blue_hints)
485 #else /* !TA_DEBUG */
487 #define TA_HINTS_DO_HORIZONTAL(h) \
488 !TA_HINTS_TEST_SCALER(h, TA_SCALER_FLAG_NO_HORIZONTAL)
490 #define TA_HINTS_DO_VERTICAL(h) \
491 !TA_HINTS_TEST_SCALER(h, TA_SCALER_FLAG_NO_VERTICAL)
493 #define TA_HINTS_DO_BLUES(h) \
496 #endif /* !TA_DEBUG */
499 #define TA_HINTS_DO_ADVANCE(h) \
500 !TA_HINTS_TEST_SCALER(h, TA_SCALER_FLAG_NO_ADVANCE)
502 #define TA_HINTS_DO_WARP(h) \
503 !TA_HINTS_TEST_SCALER(h, TA_SCALER_FLAG_NO_WARPER)
507 ta_direction_compute(FT_Pos dx
,
512 ta_axis_hints_new_segment(TA_AxisHints axis
,
513 TA_Segment
* asegment
);
516 ta_axis_hints_new_edge(TA_AxisHints axis
,
519 FT_Bool top_to_bottom_hinting
,
524 ta_glyph_hints_dump_points(TA_GlyphHints hints
);
527 ta_glyph_hints_dump_segments(TA_GlyphHints hints
);
530 ta_glyph_hints_dump_edges(TA_GlyphHints hints
);
534 ta_glyph_hints_init(TA_GlyphHints hints
);
537 ta_glyph_hints_rescale(TA_GlyphHints hints
,
538 TA_StyleMetrics metrics
);
541 ta_glyph_hints_reload(TA_GlyphHints hints
,
542 FT_Outline
* outline
);
545 ta_glyph_hints_save(TA_GlyphHints hints
,
546 FT_Outline
* outline
);
549 ta_glyph_hints_align_edge_points(TA_GlyphHints hints
,
553 ta_glyph_hints_align_strong_points(TA_GlyphHints hints
,
557 ta_glyph_hints_align_weak_points(TA_GlyphHints hints
,
560 #ifdef TA_CONFIG_OPTION_USE_WARPER
562 ta_glyph_hints_scale_dim(TA_GlyphHints hints
,
569 ta_glyph_hints_done(TA_GlyphHints hints
);
572 #define TA_SEGMENT_LEN(seg) \
573 ((seg)->max_coord - (seg)->min_coord)
575 #define TA_SEGMENT_DIST(seg1, seg2) \
576 (((seg1)->pos > (seg2)->pos) ? (seg1)->pos - (seg2)->pos \
577 : (seg2)->pos - (seg1)->pos)
583 #endif /* TAHINTS_H_ */
586 /* end of tahints.h */