Use some macro trickery to made code more readable.
[ttfautohint.git] / src / tahints.h
blob7f3ed33b58eafda1359a5571e36d4f1e4bc2b909
1 /* tahints.h */
3 /* originally file `afhints.h' (2011-Mar-28) from FreeType */
5 /* heavily modified 2011 by Werner Lemberg <wl@gnu.org> */
7 #ifndef __TAHINTS_H__
8 #define __TAHINTS_H__
10 #include "tatypes.h"
12 #define xxTA_SORT_SEGMENTS
15 /* the definition of outline glyph hints; these are shared */
16 /* by all script analysis routines (until now) */
18 typedef enum TA_Dimension_
20 TA_DIMENSION_HORZ = 0, /* x coordinates, i.e. vert. segments & edges */
21 TA_DIMENSION_VERT = 1, /* y coordinates, i.e. horz. segments & edges */
22 TA_DIMENSION_MAX /* do not remove */
23 } TA_Dimension;
26 /* hint directions -- the values are computed so that two vectors */
27 /* are in opposite directions iff `dir1 + dir2 == 0' */
29 typedef enum TA_Direction_
31 TA_DIR_NONE = 4,
32 TA_DIR_RIGHT = 1,
33 TA_DIR_LEFT= -1,
34 TA_DIR_UP = 2,
35 TA_DIR_DOWN = -2
36 } TA_Direction;
40 * The following explanations are mostly taken from the article
42 * Real-Time Grid Fitting of Typographic Outlines
44 * by David Turner and Werner Lemberg
46 * http://www.tug.org/TUGboat/Articles/tb24-3/lemberg.pdf
49 * Segments
51 * `ta_{cjk,latin,...}_hints_compute_segments' are the functions to
52 * find segments in an outline. A segment is a series of consecutive
53 * points that are approximately aligned along a coordinate axis. The
54 * analysis to do so is specific to a script.
56 * A segment must have at least two points, except in the case of
57 * `fake' segments that are generated to hint metrics appropriately,
58 * and which consist of a single point.
61 * Edges
63 * As soon as segments are defined, the auto-hinter groups them into
64 * edges. An edge corresponds to a single position on the main
65 * dimension that collects one or more segments (allowing for a small
66 * threshold).
68 * The auto-hinter first tries to grid fit edges, then to align
69 * segments on the edges unless it detects that they form a serif.
71 * `ta_{cjk,latin,...}_hints_compute_edges' are the functions to find
72 * edges; they are specific to a script.
75 * A H
76 * | |
77 * | |
78 * | |
79 * | |
80 * C | | F
81 * +------<-----+ +-----<------+
82 * | B G |
83 * | |
84 * | |
85 * +--------------->------------------+
86 * D E
89 * Stems
91 * Segments need to be `linked' to other ones in order to detect stems.
92 * A stem is made of two segments that face each other in opposite
93 * directions and that are sufficiently close to each other. Using
94 * vocabulary from the TrueType specification, stem segments form a
95 * `black distance'.
97 * In the above ASCII drawing, the horizontal segments are BC, DE, and
98 * FG; the vertical segments are AB, CD, EF, and GH.
100 * Each segment has at most one `best' candidate to form a black
101 * distance, or no candidate at all. Notice that two distinct segments
102 * can have the same candidate, which frequently means a serif.
104 * A stem is recognized by the following condition:
106 * best segment_1 = segment_2 && best segment_2 = segment_1
108 * The best candidate is stored in field `link' in structure
109 * `TA_Segment'.
111 * Stems are detected by `ta_{cjk,latin,...}_hint_edges'.
113 * In the above ASCII drawing, the best candidate for both AB and CD is
114 * GH, while the best candidate for GH is AB. Similarly, the best
115 * candidate for EF and GH is AB, while the best candidate for AB is
116 * GH.
119 * Serifs
121 * On the opposite, a serif has
123 * best segment_1 = segment_2 && best segment_2 != segment_1
125 * where segment_1 corresponds to the serif segment (CD and EF in the
126 * above ASCII drawing).
128 * The best candidate is stored in field `serif' in structure
129 * `TA_Segment' (and `link' is set to NULL).
131 * Serifs are detected by `ta_{cjk,latin,...}_hint_edges'.
134 * Touched points
136 * A point is called `touched' if it has been processed somehow by the
137 * auto-hinter. It basically means that it shouldn't be moved again
138 * (or moved only under certain constraints to preserve the already
139 * applied processing).
142 * Flat and round segments
144 * Segments are `round' or `flat', depending on the series of points
145 * that define them. A segment is round if the next and previous point
146 * of an extremum (which can be either a single point or sequence of
147 * points) are both conic or cubic control points. Otherwise, a
148 * segment with an extremum is flat.
151 * Strong Points
153 * Experience has shown that points which are not part of an edge need
154 * to be interpolated linearly between their two closest edges, even if
155 * these are not part of the contour of those particular points.
156 * Typical candidates for this are
158 * - angle points (i.e., points where the `in' and `out' direction
159 * differ greatly)
161 * - inflection points (i.e., where the `in' and `out' angles are the
162 * same, but the curvature changes sign)
164 * `ta_glyph_hints_align_strong_points' is the function which takes
165 * care of such situations; it is equivalent to the TrueType `IP'
166 * hinting instruction.
169 * Weak Points
171 * Other points in the outline must be interpolated using the
172 * coordinates of their previous and next unfitted contour neighbours.
173 * These are called `weak points' and are touched by the function
174 * `ta_glyph_hints_align_weak_points', equivalent to the TrueType `IUP'
175 * hinting instruction. Typical candidates are control points and
176 * points on the contour without a major direction.
178 * The major effect is to reduce possible distortion caused by
179 * alignment of edges and strong points, thus weak points are processed
180 * after strong points.
184 /* point hint flags */
185 #define TA_FLAG_NONE 0
187 /* point type flags */
188 #define TA_FLAG_CONIC (1 << 0)
189 #define TA_FLAG_CUBIC (1 << 1)
190 #define TA_FLAG_CONTROL (TA_FLAG_CONIC | TA_FLAG_CUBIC)
192 /* point extremum flags */
193 #define TA_FLAG_EXTREMA_X (1 << 2)
194 #define TA_FLAG_EXTREMA_Y (1 << 3)
196 /* point roundness flags */
197 #define TA_FLAG_ROUND_X (1 << 4)
198 #define TA_FLAG_ROUND_Y (1 << 5)
200 /* point touch flags */
201 #define TA_FLAG_TOUCH_X (1 << 6)
202 #define TA_FLAG_TOUCH_Y (1 << 7)
204 /* candidates for weak interpolation have this flag set */
205 #define TA_FLAG_WEAK_INTERPOLATION (1 << 8)
207 /* all inflection points in the outline have this flag set */
208 #define TA_FLAG_INFLECTION (1 << 9)
211 /* edge hint flags */
212 #define TA_EDGE_NORMAL 0
213 #define TA_EDGE_ROUND (1 << 0)
214 #define TA_EDGE_SERIF (1 << 1)
215 #define TA_EDGE_DONE (1 << 2)
218 typedef struct TA_PointRec_* TA_Point;
219 typedef struct TA_SegmentRec_* TA_Segment;
220 typedef struct TA_EdgeRec_* TA_Edge;
223 typedef struct TA_PointRec_
225 FT_UShort flags; /* point flags used by hinter */
226 FT_Char in_dir; /* direction of inwards vector */
227 FT_Char out_dir; /* direction of outwards vector */
229 FT_Pos ox, oy; /* original, scaled position */
230 FT_Short fx, fy; /* original, unscaled position (in font units) */
231 FT_Pos x, y; /* current position */
232 FT_Pos u, v; /* current (x,y) or (y,x) depending on context */
234 TA_Point next; /* next point in contour */
235 TA_Point prev; /* previous point in contour */
236 } TA_PointRec;
239 typedef struct TA_SegmentRec_
241 FT_Byte flags; /* edge/segment flags for this segment */
242 FT_Char dir; /* segment direction */
243 FT_Short pos; /* position of segment */
244 FT_Short min_coord; /* minimum coordinate of segment */
245 FT_Short max_coord; /* maximum coordinate of segment */
246 FT_Short height; /* the hinted segment height */
248 TA_Edge edge; /* the segment's parent edge */
249 TA_Segment edge_next; /* link to next segment in parent edge */
251 TA_Segment link; /* (stem) link segment */
252 TA_Segment serif; /* primary segment for serifs */
253 FT_Pos num_linked; /* number of linked segments */
254 FT_Pos score; /* used during stem matching */
255 FT_Pos len; /* used during stem matching */
257 TA_Point first; /* first point in edge segment */
258 TA_Point last; /* last point in edge segment */
259 TA_Point* contour; /* ptr to first point of segment's contour */
260 } TA_SegmentRec;
263 typedef struct TA_EdgeRec_
265 FT_Short fpos; /* original, unscaled position (in font units) */
266 FT_Pos opos; /* original, scaled position */
267 FT_Pos pos; /* current position */
269 FT_Byte flags; /* edge flags */
270 FT_Char dir; /* edge direction */
271 FT_Fixed scale; /* used to speed up interpolation between edges */
272 TA_Width blue_edge; /* non-NULL if this is a blue edge */
274 TA_Edge link; /* link edge */
275 TA_Edge serif; /* primary edge for serifs */
276 FT_Short num_linked; /* number of linked edges */
277 FT_Int score; /* used during stem matching */
279 TA_Segment first; /* first segment in edge */
280 TA_Segment last; /* last segment in edge */
281 } TA_EdgeRec;
284 typedef struct TA_AxisHintsRec_
286 FT_Int num_segments; /* number of used segments */
287 FT_Int max_segments; /* number of allocated segments */
288 TA_Segment segments; /* segments array */
289 #ifdef TA_SORT_SEGMENTS
290 FT_Int mid_segments;
291 #endif
293 FT_Int num_edges; /* number of used edges */
294 FT_Int max_edges; /* number of allocated edges */
295 TA_Edge edges; /* edges array */
297 TA_Direction major_dir; /* either vertical or horizontal */
298 } TA_AxisHintsRec, *TA_AxisHints;
301 typedef struct TA_GlyphHintsRec_
303 FT_Fixed x_scale;
304 FT_Pos x_delta;
306 FT_Fixed y_scale;
307 FT_Pos y_delta;
309 FT_Int max_points; /* number of allocated points */
310 FT_Int num_points; /* number of used points */
311 TA_Point points; /* points array */
313 FT_Int max_contours; /* number of allocated contours */
314 FT_Int num_contours; /* number of used contours */
315 TA_Point* contours; /* contours array */
317 TA_AxisHintsRec axis[TA_DIMENSION_MAX];
319 FT_UInt32 scaler_flags; /* copy of scaler flags */
320 FT_UInt32 other_flags; /* free for script-specific implementations */
321 TA_ScriptMetrics metrics;
323 FT_Pos xmin_delta; /* used for warping */
324 FT_Pos xmax_delta;
325 } TA_GlyphHintsRec;
328 #define TA_HINTS_TEST_SCALER(h, f) \
329 ((h)->scaler_flags & (f))
330 #define TA_HINTS_TEST_OTHER(h, f) \
331 ((h)->other_flags & (f))
334 #ifdef TA_DEBUG
336 #define TA_HINTS_DO_HORIZONTAL(h) \
337 (!_ta_debug_disable_horz_hints \
338 && !TA_HINTS_TEST_SCALER(h, TA_SCALER_FLAG_NO_HORIZONTAL))
340 #define TA_HINTS_DO_VERTICAL(h) \
341 (!_ta_debug_disable_vert_hints \
342 && !TA_HINTS_TEST_SCALER(h, TA_SCALER_FLAG_NO_VERTICAL))
344 #define TA_HINTS_DO_ADVANCE(h) \
345 !TA_HINTS_TEST_SCALER(h, TA_SCALER_FLAG_NO_ADVANCE)
347 #define TA_HINTS_DO_BLUES(h) \
348 (!_ta_debug_disable_blue_hints)
350 #else /* !TA_DEBUG */
352 #define TA_HINTS_DO_HORIZONTAL(h) \
353 !TA_HINTS_TEST_SCALER(h, TA_SCALER_FLAG_NO_HORIZONTAL)
355 #define TA_HINTS_DO_VERTICAL(h) \
356 !TA_HINTS_TEST_SCALER(h, TA_SCALER_FLAG_NO_VERTICAL)
358 #define TA_HINTS_DO_ADVANCE(h) \
359 !TA_HINTS_TEST_SCALER(h, TA_SCALER_FLAG_NO_ADVANCE)
361 #define TA_HINTS_DO_BLUES(h) \
364 #endif /* !TA_DEBUG */
367 TA_Direction
368 ta_direction_compute(FT_Pos dx,
369 FT_Pos dy);
372 FT_Error
373 ta_axis_hints_new_segment(TA_AxisHints axis,
374 TA_Segment* asegment);
376 FT_Error
377 ta_axis_hints_new_edge(TA_AxisHints axis,
378 FT_Int fpos,
379 TA_Direction dir,
380 TA_Edge* edge);
382 void
383 ta_glyph_hints_init(TA_GlyphHints hints);
385 void
386 ta_glyph_hints_rescale(TA_GlyphHints hints,
387 TA_ScriptMetrics metrics);
389 FT_Error
390 ta_glyph_hints_reload(TA_GlyphHints hints,
391 FT_Outline* outline);
393 void
394 ta_glyph_hints_save(TA_GlyphHints hints,
395 FT_Outline* outline);
397 void
398 ta_glyph_hints_align_edge_points(TA_GlyphHints hints,
399 TA_Dimension dim);
401 void
402 ta_glyph_hints_align_strong_points(TA_GlyphHints hints,
403 TA_Dimension dim);
405 void
406 ta_glyph_hints_align_weak_points(TA_GlyphHints hints,
407 TA_Dimension dim);
409 #ifdef TA_CONFIG_OPTION_USE_WARPER
410 void
411 ta_glyph_hints_scale_dim(TA_GlyphHints hints,
412 TA_Dimension dim,
413 FT_Fixed scale,
414 FT_Pos delta);
415 #endif
417 void
418 ta_glyph_hints_done(TA_GlyphHints hints);
421 #define TA_SEGMENT_LEN(seg) \
422 ((seg)->max_coord - (seg)->min_coord)
424 #define TA_SEGMENT_DIST(seg1, seg2) \
425 (((seg1)->pos > (seg2)->pos) ? (seg1)->pos - (seg2)->pos \
426 : (seg2)->pos - (seg1)->pos)
428 #endif /* __TAHINTS_H__ */
430 /* end of tahints.h */