4 * Copyright (C) 2011-2012 by Werner Lemberg.
6 * This file is part of the ttfautohint library, and may only be used,
7 * modified, and distributed under the terms given in `COPYING'. By
8 * continuing to use, modify, or distribute this file you indicate that you
9 * have read `COPYING' and understand and accept it fully.
11 * The file `COPYING' mentioned in the previous paragraph is distributed
12 * with the ttfautohint library.
16 /* originally file `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 script 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
64 * `ta_{cjk,latin,...}_hints_compute_segments' are the functions to
65 * find segments in an outline. A segment is a series of consecutive
66 * points that are approximately aligned along a coordinate axis. The
67 * analysis to do so is specific to a script.
69 * A segment must have at least two points, except in the case of
70 * `fake' segments that are generated to hint metrics appropriately,
71 * and which consist of a single point.
76 * As soon as segments are defined, the auto-hinter groups them into
77 * edges. An edge corresponds to a single position on the main
78 * dimension that collects one or more segments (allowing for a small
81 * The auto-hinter first tries to grid fit edges, then to align
82 * segments on the edges unless it detects that they form a serif.
84 * `ta_{cjk,latin,...}_hints_compute_edges' are the functions to find
85 * edges; they are specific to a script.
94 * +------<-----+ +-----<------+
98 * +--------------->------------------+
104 * Segments need to be `linked' to other ones in order to detect stems.
105 * A stem is made of two segments that face each other in opposite
106 * directions and that are sufficiently close to each other. Using
107 * vocabulary from the TrueType specification, stem segments form a
110 * In the above ASCII drawing, the horizontal segments are BC, DE, and
111 * FG; the vertical segments are AB, CD, EF, and GH.
113 * Each segment has at most one `best' candidate to form a black
114 * distance, or no candidate at all. Notice that two distinct segments
115 * can have the same candidate, which frequently means a serif.
117 * A stem is recognized by the following condition:
119 * best segment_1 = segment_2 && best segment_2 = segment_1
121 * The best candidate is stored in field `link' in structure
124 * Stems are detected by `ta_{cjk,latin,...}_hint_edges'.
126 * In the above ASCII drawing, the best candidate for both AB and CD is
127 * GH, while the best candidate for GH is AB. Similarly, the best
128 * candidate for EF and GH is AB, while the best candidate for AB is
134 * On the opposite, a serif has
136 * best segment_1 = segment_2 && best segment_2 != segment_1
138 * where segment_1 corresponds to the serif segment (CD and EF in the
139 * above ASCII drawing).
141 * The best candidate is stored in field `serif' in structure
142 * `TA_Segment' (and `link' is set to NULL).
144 * Serifs are detected by `ta_{cjk,latin,...}_hint_edges'.
149 * A point is called `touched' if it has been processed somehow by the
150 * auto-hinter. It basically means that it shouldn't be moved again
151 * (or moved only under certain constraints to preserve the already
152 * applied processing).
155 * Flat and round segments
157 * Segments are `round' or `flat', depending on the series of points
158 * that define them. A segment is round if the next and previous point
159 * of an extremum (which can be either a single point or sequence of
160 * points) are both conic or cubic control points. Otherwise, a
161 * segment with an extremum is flat.
166 * Experience has shown that points which are not part of an edge need
167 * to be interpolated linearly between their two closest edges, even if
168 * these are not part of the contour of those particular points.
169 * Typical candidates for this are
171 * - angle points (i.e., points where the `in' and `out' direction
174 * - inflection points (i.e., where the `in' and `out' angles are the
175 * same, but the curvature changes sign)
177 * `ta_glyph_hints_align_strong_points' is the function which takes
178 * care of such situations; it is equivalent to the TrueType `IP'
179 * hinting instruction.
184 * Other points in the outline must be interpolated using the
185 * coordinates of their previous and next unfitted contour neighbours.
186 * These are called `weak points' and are touched by the function
187 * `ta_glyph_hints_align_weak_points', equivalent to the TrueType `IUP'
188 * hinting instruction. Typical candidates are control points and
189 * points on the contour without a major direction.
191 * The major effect is to reduce possible distortion caused by
192 * alignment of edges and strong points, thus weak points are processed
193 * after strong points.
197 /* point hint flags */
198 #define TA_FLAG_NONE 0
200 /* point type flags */
201 #define TA_FLAG_CONIC (1 << 0)
202 #define TA_FLAG_CUBIC (1 << 1)
203 #define TA_FLAG_CONTROL (TA_FLAG_CONIC | TA_FLAG_CUBIC)
205 /* point extremum flags */
206 #define TA_FLAG_EXTREMA_X (1 << 2)
207 #define TA_FLAG_EXTREMA_Y (1 << 3)
209 /* point roundness flags */
210 #define TA_FLAG_ROUND_X (1 << 4)
211 #define TA_FLAG_ROUND_Y (1 << 5)
213 /* point touch flags */
214 #define TA_FLAG_TOUCH_X (1 << 6)
215 #define TA_FLAG_TOUCH_Y (1 << 7)
217 /* candidates for weak interpolation have this flag set */
218 #define TA_FLAG_WEAK_INTERPOLATION (1 << 8)
220 /* all inflection points in the outline have this flag set */
221 #define TA_FLAG_INFLECTION (1 << 9)
224 /* edge hint flags */
225 #define TA_EDGE_NORMAL 0
226 #define TA_EDGE_ROUND (1 << 0)
227 #define TA_EDGE_SERIF (1 << 1)
228 #define TA_EDGE_DONE (1 << 2)
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 TA_Point next
; /* next point in contour */
248 TA_Point prev
; /* previous point in contour */
252 typedef struct TA_SegmentRec_
254 FT_Byte flags
; /* edge/segment flags for this segment */
255 FT_Char dir
; /* segment direction */
256 FT_Short pos
; /* position of segment */
257 FT_Short min_coord
; /* minimum coordinate of segment */
258 FT_Short max_coord
; /* maximum coordinate of segment */
259 FT_Short height
; /* the hinted segment height */
261 TA_Edge edge
; /* the segment's parent edge */
262 TA_Segment edge_next
; /* link to next segment in parent edge */
264 TA_Segment link
; /* (stem) link segment */
265 TA_Segment serif
; /* primary segment for serifs */
266 FT_Pos num_linked
; /* number of linked segments */
267 FT_Pos score
; /* used during stem matching */
268 FT_Pos len
; /* used during stem matching */
270 TA_Point first
; /* first point in edge segment */
271 TA_Point last
; /* last point in edge segment */
275 typedef struct TA_EdgeRec_
277 FT_Short fpos
; /* original, unscaled position (in font units) */
278 FT_Pos opos
; /* original, scaled position */
279 FT_Pos pos
; /* current position */
281 FT_Byte flags
; /* edge flags */
282 FT_Char dir
; /* edge direction */
283 FT_Fixed scale
; /* used to speed up interpolation between edges */
285 TA_Width blue_edge
; /* non-NULL if this is a blue edge */
286 FT_UInt best_blue_idx
; /* for the hinting recorder callback */
287 FT_Bool best_blue_is_shoot
; /* for the hinting recorder callback */
289 TA_Edge link
; /* link edge */
290 TA_Edge serif
; /* primary edge for serifs */
291 FT_Short num_linked
; /* number of linked edges */
292 FT_Int score
; /* used during stem matching */
294 TA_Segment first
; /* first segment in edge */
295 TA_Segment last
; /* last segment in edge */
299 typedef struct TA_AxisHintsRec_
301 FT_Int num_segments
; /* number of used segments */
302 FT_Int max_segments
; /* number of allocated segments */
303 TA_Segment segments
; /* segments array */
304 #ifdef TA_SORT_SEGMENTS
308 FT_Int num_edges
; /* number of used edges */
309 FT_Int max_edges
; /* number of allocated edges */
310 TA_Edge edges
; /* edges array */
312 TA_Direction major_dir
; /* either vertical or horizontal */
313 } TA_AxisHintsRec
, *TA_AxisHints
;
316 typedef enum TA_Action_
333 ta_anchor_round_serif
,
338 ta_adjust_round_serif
,
340 ta_adjust_bound_serif
,
341 ta_adjust_bound_round
,
342 ta_adjust_bound_round_serif
,
356 ta_stem_bound_round_serif
,
359 ta_serif_lower_bound
,
360 ta_serif_upper_bound
,
361 ta_serif_upper_lower_bound
,
364 ta_serif_anchor_lower_bound
,
365 ta_serif_anchor_upper_bound
,
366 ta_serif_anchor_upper_lower_bound
,
369 ta_serif_link1_lower_bound
,
370 ta_serif_link1_upper_bound
,
371 ta_serif_link1_upper_lower_bound
,
374 ta_serif_link2_lower_bound
,
375 ta_serif_link2_upper_bound
,
376 ta_serif_link2_upper_lower_bound
,
383 (*TA_Hints_Recorder
)(TA_Action action
,
386 void* arg1
, /* TA_Point or TA_Edge */
390 TA_Edge upper_bound
);
392 typedef struct TA_GlyphHintsRec_
400 FT_Int max_points
; /* number of allocated points */
401 FT_Int num_points
; /* number of used points */
402 TA_Point points
; /* points array */
404 FT_Int max_contours
; /* number of allocated contours */
405 FT_Int num_contours
; /* number of used contours */
406 TA_Point
* contours
; /* contours array */
408 TA_AxisHintsRec axis
[TA_DIMENSION_MAX
];
410 FT_UInt32 scaler_flags
; /* copy of scaler flags */
411 FT_UInt32 other_flags
; /* free for script-specific implementations */
412 TA_ScriptMetrics metrics
;
414 FT_Pos xmin_delta
; /* used for warping */
417 TA_Hints_Recorder recorder
;
422 #define TA_HINTS_TEST_SCALER(h, f) \
423 ((h)->scaler_flags & (f))
424 #define TA_HINTS_TEST_OTHER(h, f) \
425 ((h)->other_flags & (f))
430 #define TA_HINTS_DO_HORIZONTAL(h) \
431 (!_ta_debug_disable_horz_hints \
432 && !TA_HINTS_TEST_SCALER(h, TA_SCALER_FLAG_NO_HORIZONTAL))
434 #define TA_HINTS_DO_VERTICAL(h) \
435 (!_ta_debug_disable_vert_hints \
436 && !TA_HINTS_TEST_SCALER(h, TA_SCALER_FLAG_NO_VERTICAL))
438 #define TA_HINTS_DO_ADVANCE(h) \
439 !TA_HINTS_TEST_SCALER(h, TA_SCALER_FLAG_NO_ADVANCE)
441 #define TA_HINTS_DO_BLUES(h) \
442 (!_ta_debug_disable_blue_hints)
444 #else /* !TA_DEBUG */
446 #define TA_HINTS_DO_HORIZONTAL(h) \
447 !TA_HINTS_TEST_SCALER(h, TA_SCALER_FLAG_NO_HORIZONTAL)
449 #define TA_HINTS_DO_VERTICAL(h) \
450 !TA_HINTS_TEST_SCALER(h, TA_SCALER_FLAG_NO_VERTICAL)
452 #define TA_HINTS_DO_ADVANCE(h) \
453 !TA_HINTS_TEST_SCALER(h, TA_SCALER_FLAG_NO_ADVANCE)
455 #define TA_HINTS_DO_BLUES(h) \
458 #endif /* !TA_DEBUG */
462 ta_direction_compute(FT_Pos dx
,
467 ta_axis_hints_new_segment(TA_AxisHints axis
,
468 TA_Segment
* asegment
);
471 ta_axis_hints_new_edge(TA_AxisHints axis
,
478 ta_glyph_hints_dump_points(TA_GlyphHints hints
);
481 ta_glyph_hints_dump_segments(TA_GlyphHints hints
);
484 ta_glyph_hints_dump_edges(TA_GlyphHints hints
);
488 ta_glyph_hints_init(TA_GlyphHints hints
);
491 ta_glyph_hints_rescale(TA_GlyphHints hints
,
492 TA_ScriptMetrics metrics
);
495 ta_glyph_hints_reload(TA_GlyphHints hints
,
496 FT_Outline
* outline
);
499 ta_glyph_hints_save(TA_GlyphHints hints
,
500 FT_Outline
* outline
);
503 ta_glyph_hints_align_edge_points(TA_GlyphHints hints
,
507 ta_glyph_hints_align_strong_points(TA_GlyphHints hints
,
511 ta_glyph_hints_align_weak_points(TA_GlyphHints hints
,
514 #ifdef TA_CONFIG_OPTION_USE_WARPER
516 ta_glyph_hints_scale_dim(TA_GlyphHints hints
,
523 ta_glyph_hints_done(TA_GlyphHints hints
);
526 #define TA_SEGMENT_LEN(seg) \
527 ((seg)->max_coord - (seg)->min_coord)
529 #define TA_SEGMENT_DIST(seg1, seg2) \
530 (((seg1)->pos > (seg2)->pos) ? (seg1)->pos - (seg2)->pos \
531 : (seg2)->pos - (seg1)->pos)
533 #endif /* __TAHINTS_H__ */
535 /* end of tahints.h */