4 * Copyright (C) 2011-2014 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> */
25 #define xxTA_SORT_SEGMENTS
28 /* the definition of outline glyph hints; these are shared */
29 /* by all writing system analysis routines (until now) */
31 typedef enum TA_Dimension_
33 TA_DIMENSION_HORZ
= 0, /* x coordinates, i.e. vert. segments & edges */
34 TA_DIMENSION_VERT
= 1, /* y coordinates, i.e. horz. segments & edges */
35 TA_DIMENSION_MAX
/* do not remove */
39 /* hint directions -- the values are computed so that two vectors */
40 /* are in opposite directions iff `dir1 + dir2 == 0' */
42 typedef enum TA_Direction_
53 * The following explanations are mostly taken from the article
55 * Real-Time Grid Fitting of Typographic Outlines
57 * by David Turner and Werner Lemberg
59 * http://www.tug.org/TUGboat/Articles/tb24-3/lemberg.pdf
61 * with appropriate updates.
66 * `ta_{cjk,latin,...}_hints_compute_segments' are the functions to
67 * find segments in an outline.
69 * A segment is a series of consecutive points that are approximately
70 * aligned along a coordinate axis. The analysis to do so is specific
71 * to a writing system.
73 * A segment must have at least two points, except in the case of
74 * `fake' segments that are generated to hint metrics appropriately,
75 * and which consist of a single point.
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 which 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 which are not part of an edge need
176 * to be interpolated linearly between their two closest edges, even if
177 * these are not part of the contour of those particular points.
178 * Typical 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 in the auto-hinter]
187 * `ta_glyph_hints_align_strong_points' is the function which 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 (1 << 0)
212 #define TA_FLAG_CUBIC (1 << 1)
213 #define TA_FLAG_CONTROL (TA_FLAG_CONIC | TA_FLAG_CUBIC)
215 /* point extremum flags */
216 #define TA_FLAG_EXTREMA_X (1 << 2)
217 #define TA_FLAG_EXTREMA_Y (1 << 3)
219 /* point roundness flags */
220 #define TA_FLAG_ROUND_X (1 << 4)
221 #define TA_FLAG_ROUND_Y (1 << 5)
223 /* point touch flags */
224 #define TA_FLAG_TOUCH_X (1 << 6)
225 #define TA_FLAG_TOUCH_Y (1 << 7)
227 /* candidates for weak interpolation have this flag set */
228 #define TA_FLAG_WEAK_INTERPOLATION (1 << 8)
230 /* all inflection points in the outline have this flag set */
231 #define TA_FLAG_INFLECTION (1 << 9)
233 /* the current point is very near to another one */
234 #define TA_FLAG_NEAR (1 << 10)
237 /* edge hint flags */
238 #define TA_EDGE_NORMAL 0
239 #define TA_EDGE_ROUND (1 << 0)
240 #define TA_EDGE_SERIF (1 << 1)
241 #define TA_EDGE_DONE (1 << 2)
244 typedef struct TA_PointRec_
* TA_Point
;
245 typedef struct TA_SegmentRec_
* TA_Segment
;
246 typedef struct TA_EdgeRec_
* TA_Edge
;
249 typedef struct TA_PointRec_
251 FT_UShort flags
; /* point flags used by hinter */
252 FT_Char in_dir
; /* direction of inwards vector */
253 FT_Char out_dir
; /* direction of outwards vector */
255 FT_Pos ox
, oy
; /* original, scaled position */
256 FT_Short fx
, fy
; /* original, unscaled position (in font units) */
257 FT_Pos x
, y
; /* current position */
258 FT_Pos u
, v
; /* current (x,y) or (y,x) depending on context */
260 TA_Point next
; /* next point in contour */
261 TA_Point prev
; /* previous point in contour */
265 typedef struct TA_SegmentRec_
267 FT_Byte flags
; /* edge/segment flags for this segment */
268 FT_Char dir
; /* segment direction */
269 FT_Short pos
; /* position of segment */
270 FT_Short min_coord
; /* minimum coordinate of segment */
271 FT_Short max_coord
; /* maximum coordinate of segment */
272 FT_Short height
; /* the hinted segment height */
274 TA_Edge edge
; /* the segment's parent edge */
275 TA_Segment edge_next
; /* link to next segment in parent edge */
277 TA_Segment link
; /* (stem) link segment */
278 TA_Segment serif
; /* primary segment for serifs */
279 FT_Pos num_linked
; /* number of linked segments */
280 FT_Pos score
; /* used during stem matching */
281 FT_Pos len
; /* used during stem matching */
283 TA_Point first
; /* first point in edge segment */
284 TA_Point last
; /* last point in edge segment */
288 typedef struct TA_EdgeRec_
290 FT_Short fpos
; /* original, unscaled position (in font units) */
291 FT_Pos opos
; /* original, scaled position */
292 FT_Pos pos
; /* current position */
294 FT_Byte flags
; /* edge flags */
295 FT_Char dir
; /* edge direction */
296 FT_Fixed scale
; /* used to speed up interpolation between edges */
298 TA_Width blue_edge
; /* non-NULL if this is a blue edge */
299 FT_UInt best_blue_idx
; /* for the hinting recorder callback */
300 FT_Bool best_blue_is_shoot
; /* for the hinting recorder callback */
302 TA_Edge link
; /* link edge */
303 TA_Edge serif
; /* primary edge for serifs */
304 FT_Short num_linked
; /* number of linked edges */
305 FT_Int score
; /* used during stem matching */
307 TA_Segment first
; /* first segment in edge */
308 TA_Segment last
; /* last segment in edge */
312 typedef struct TA_AxisHintsRec_
314 FT_Int num_segments
; /* number of used segments */
315 FT_Int max_segments
; /* number of allocated segments */
316 TA_Segment segments
; /* segments array */
317 #ifdef TA_SORT_SEGMENTS
321 FT_Int num_edges
; /* number of used edges */
322 FT_Int max_edges
; /* number of allocated edges */
323 TA_Edge edges
; /* edges array */
325 TA_Direction major_dir
; /* either vertical or horizontal */
326 } TA_AxisHintsRec
, *TA_AxisHints
;
329 typedef enum TA_Action_
346 ta_anchor_round_serif
,
351 ta_adjust_round_serif
,
353 ta_adjust_bound_serif
,
354 ta_adjust_bound_round
,
355 ta_adjust_bound_round_serif
,
369 ta_stem_bound_round_serif
,
372 ta_serif_lower_bound
,
373 ta_serif_upper_bound
,
374 ta_serif_upper_lower_bound
,
377 ta_serif_anchor_lower_bound
,
378 ta_serif_anchor_upper_bound
,
379 ta_serif_anchor_upper_lower_bound
,
382 ta_serif_link1_lower_bound
,
383 ta_serif_link1_upper_bound
,
384 ta_serif_link1_upper_lower_bound
,
387 ta_serif_link2_lower_bound
,
388 ta_serif_link2_upper_bound
,
389 ta_serif_link2_upper_lower_bound
,
396 (*TA_Hints_Recorder
)(TA_Action action
,
399 void* arg1
, /* TA_Point or TA_Edge */
403 TA_Edge upper_bound
);
405 typedef struct TA_GlyphHintsRec_
413 FT_Int max_points
; /* number of allocated points */
414 FT_Int num_points
; /* number of used points */
415 TA_Point points
; /* points array */
417 FT_Int max_contours
; /* number of allocated contours */
418 FT_Int num_contours
; /* number of used contours */
419 TA_Point
* contours
; /* contours array */
421 TA_AxisHintsRec axis
[TA_DIMENSION_MAX
];
423 FT_UInt32 scaler_flags
; /* copy of scaler flags */
424 FT_UInt32 other_flags
; /* free for style-specific implementations */
425 TA_StyleMetrics metrics
;
427 FT_Pos xmin_delta
; /* used for warping */
430 TA_Hints_Recorder recorder
;
435 #define TA_HINTS_TEST_SCALER(h, f) \
436 ((h)->scaler_flags & (f))
437 #define TA_HINTS_TEST_OTHER(h, f) \
438 ((h)->other_flags & (f))
443 #define TA_HINTS_DO_HORIZONTAL(h) \
444 (!_ta_debug_disable_horz_hints \
445 && !TA_HINTS_TEST_SCALER(h, TA_SCALER_FLAG_NO_HORIZONTAL))
447 #define TA_HINTS_DO_VERTICAL(h) \
448 (!_ta_debug_disable_vert_hints \
449 && !TA_HINTS_TEST_SCALER(h, TA_SCALER_FLAG_NO_VERTICAL))
451 #define TA_HINTS_DO_ADVANCE(h) \
452 !TA_HINTS_TEST_SCALER(h, TA_SCALER_FLAG_NO_ADVANCE)
454 #define TA_HINTS_DO_BLUES(h) \
455 (!_ta_debug_disable_blue_hints)
457 #else /* !TA_DEBUG */
459 #define TA_HINTS_DO_HORIZONTAL(h) \
460 !TA_HINTS_TEST_SCALER(h, TA_SCALER_FLAG_NO_HORIZONTAL)
462 #define TA_HINTS_DO_VERTICAL(h) \
463 !TA_HINTS_TEST_SCALER(h, TA_SCALER_FLAG_NO_VERTICAL)
465 #define TA_HINTS_DO_ADVANCE(h) \
466 !TA_HINTS_TEST_SCALER(h, TA_SCALER_FLAG_NO_ADVANCE)
468 #define TA_HINTS_DO_BLUES(h) \
471 #endif /* !TA_DEBUG */
475 ta_direction_compute(FT_Pos dx
,
480 ta_axis_hints_new_segment(TA_AxisHints axis
,
481 TA_Segment
* asegment
);
484 ta_axis_hints_new_edge(TA_AxisHints axis
,
491 ta_glyph_hints_dump_points(TA_GlyphHints hints
);
494 ta_glyph_hints_dump_segments(TA_GlyphHints hints
);
497 ta_glyph_hints_dump_edges(TA_GlyphHints hints
);
501 ta_glyph_hints_init(TA_GlyphHints hints
);
504 ta_glyph_hints_rescale(TA_GlyphHints hints
,
505 TA_StyleMetrics metrics
);
508 ta_glyph_hints_reload(TA_GlyphHints hints
,
509 FT_Outline
* outline
);
512 ta_glyph_hints_save(TA_GlyphHints hints
,
513 FT_Outline
* outline
);
516 ta_glyph_hints_align_edge_points(TA_GlyphHints hints
,
520 ta_glyph_hints_align_strong_points(TA_GlyphHints hints
,
524 ta_glyph_hints_align_weak_points(TA_GlyphHints hints
,
527 #ifdef TA_CONFIG_OPTION_USE_WARPER
529 ta_glyph_hints_scale_dim(TA_GlyphHints hints
,
536 ta_glyph_hints_done(TA_GlyphHints hints
);
539 #define TA_SEGMENT_LEN(seg) \
540 ((seg)->max_coord - (seg)->min_coord)
542 #define TA_SEGMENT_DIST(seg1, seg2) \
543 (((seg1)->pos > (seg2)->pos) ? (seg1)->pos - (seg2)->pos \
544 : (seg2)->pos - (seg1)->pos)
546 #endif /* __TAHINTS_H__ */
548 /* end of tahints.h */