Thinko.
[ttfautohint.git] / src / tahints.h
blob05187b6b091086bbd6d8b98e943e0305092fbd23
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_SegmentRec;
262 typedef struct TA_EdgeRec_
264 FT_Short fpos; /* original, unscaled position (in font units) */
265 FT_Pos opos; /* original, scaled position */
266 FT_Pos pos; /* current position */
268 FT_Byte flags; /* edge flags */
269 FT_Char dir; /* edge direction */
270 FT_Fixed scale; /* used to speed up interpolation between edges */
272 TA_Width blue_edge; /* non-NULL if this is a blue edge */
273 FT_UInt best_blue_idx; /* for the hinting recorder callback */
274 FT_Bool best_blue_is_shoot; /* for the hinting recorder callback */
276 TA_Edge link; /* link edge */
277 TA_Edge serif; /* primary edge for serifs */
278 FT_Short num_linked; /* number of linked edges */
279 FT_Int score; /* used during stem matching */
281 TA_Segment first; /* first segment in edge */
282 TA_Segment last; /* last segment in edge */
283 } TA_EdgeRec;
286 typedef struct TA_AxisHintsRec_
288 FT_Int num_segments; /* number of used segments */
289 FT_Int max_segments; /* number of allocated segments */
290 TA_Segment segments; /* segments array */
291 #ifdef TA_SORT_SEGMENTS
292 FT_Int mid_segments;
293 #endif
295 FT_Int num_edges; /* number of used edges */
296 FT_Int max_edges; /* number of allocated edges */
297 TA_Edge edges; /* edges array */
299 TA_Direction major_dir; /* either vertical or horizontal */
300 } TA_AxisHintsRec, *TA_AxisHints;
303 typedef enum TA_Action_ {
304 /* edge actions */
305 ta_blue,
306 ta_blue_anchor,
307 ta_anchor,
308 ta_adjust,
309 ta_adjust_bound,
310 ta_link,
311 ta_stem,
312 ta_stem_bound,
313 ta_serif,
314 ta_serif_lower_bound,
315 ta_serif_upper_bound,
316 ta_serif_lower_upper_bound,
317 ta_serif_anchor,
318 ta_serif_anchor_lower_bound,
319 ta_serif_anchor_upper_bound,
320 ta_serif_anchor_lower_upper_bound,
321 ta_serif_link1,
322 ta_serif_link1_lower_bound,
323 ta_serif_link1_upper_bound,
324 ta_serif_link1_lower_upper_bound,
325 ta_serif_link2,
326 ta_serif_link2_lower_bound,
327 ta_serif_link2_upper_bound,
328 ta_serif_link2_lower_upper_bound,
330 ta_bound,
332 /* point actions */
333 ta_ip_before,
334 ta_ip_after,
335 ta_ip_on,
336 ta_ip_between,
337 } TA_Action;
340 typedef void
341 (*TA_Hints_Recorder)(TA_Action action,
342 TA_GlyphHints hints,
343 TA_Dimension dim,
344 void* arg1, /* TA_Point or TA_Edge */
345 TA_Edge arg2,
346 TA_Edge arg3,
347 TA_Edge lower_bound,
348 TA_Edge upper_bound);
350 typedef struct TA_GlyphHintsRec_
352 FT_Fixed x_scale;
353 FT_Pos x_delta;
355 FT_Fixed y_scale;
356 FT_Pos y_delta;
358 FT_Int max_points; /* number of allocated points */
359 FT_Int num_points; /* number of used points */
360 TA_Point points; /* points array */
362 FT_Int max_contours; /* number of allocated contours */
363 FT_Int num_contours; /* number of used contours */
364 TA_Point* contours; /* contours array */
366 TA_AxisHintsRec axis[TA_DIMENSION_MAX];
368 FT_UInt32 scaler_flags; /* copy of scaler flags */
369 FT_UInt32 other_flags; /* free for script-specific implementations */
370 TA_ScriptMetrics metrics;
372 FT_Pos xmin_delta; /* used for warping */
373 FT_Pos xmax_delta;
375 TA_Hints_Recorder recorder;
376 void *user;
377 } TA_GlyphHintsRec;
380 #define TA_HINTS_TEST_SCALER(h, f) \
381 ((h)->scaler_flags & (f))
382 #define TA_HINTS_TEST_OTHER(h, f) \
383 ((h)->other_flags & (f))
386 #ifdef TA_DEBUG
388 #define TA_HINTS_DO_HORIZONTAL(h) \
389 (!_ta_debug_disable_horz_hints \
390 && !TA_HINTS_TEST_SCALER(h, TA_SCALER_FLAG_NO_HORIZONTAL))
392 #define TA_HINTS_DO_VERTICAL(h) \
393 (!_ta_debug_disable_vert_hints \
394 && !TA_HINTS_TEST_SCALER(h, TA_SCALER_FLAG_NO_VERTICAL))
396 #define TA_HINTS_DO_ADVANCE(h) \
397 !TA_HINTS_TEST_SCALER(h, TA_SCALER_FLAG_NO_ADVANCE)
399 #define TA_HINTS_DO_BLUES(h) \
400 (!_ta_debug_disable_blue_hints)
402 #else /* !TA_DEBUG */
404 #define TA_HINTS_DO_HORIZONTAL(h) \
405 !TA_HINTS_TEST_SCALER(h, TA_SCALER_FLAG_NO_HORIZONTAL)
407 #define TA_HINTS_DO_VERTICAL(h) \
408 !TA_HINTS_TEST_SCALER(h, TA_SCALER_FLAG_NO_VERTICAL)
410 #define TA_HINTS_DO_ADVANCE(h) \
411 !TA_HINTS_TEST_SCALER(h, TA_SCALER_FLAG_NO_ADVANCE)
413 #define TA_HINTS_DO_BLUES(h) \
416 #endif /* !TA_DEBUG */
419 TA_Direction
420 ta_direction_compute(FT_Pos dx,
421 FT_Pos dy);
424 FT_Error
425 ta_axis_hints_new_segment(TA_AxisHints axis,
426 TA_Segment* asegment);
428 FT_Error
429 ta_axis_hints_new_edge(TA_AxisHints axis,
430 FT_Int fpos,
431 TA_Direction dir,
432 TA_Edge* edge);
434 void
435 ta_glyph_hints_init(TA_GlyphHints hints);
437 void
438 ta_glyph_hints_rescale(TA_GlyphHints hints,
439 TA_ScriptMetrics metrics);
441 FT_Error
442 ta_glyph_hints_reload(TA_GlyphHints hints,
443 FT_Outline* outline);
445 void
446 ta_glyph_hints_save(TA_GlyphHints hints,
447 FT_Outline* outline);
449 void
450 ta_glyph_hints_align_edge_points(TA_GlyphHints hints,
451 TA_Dimension dim);
453 void
454 ta_glyph_hints_align_strong_points(TA_GlyphHints hints,
455 TA_Dimension dim);
457 void
458 ta_glyph_hints_align_weak_points(TA_GlyphHints hints,
459 TA_Dimension dim);
461 #ifdef TA_CONFIG_OPTION_USE_WARPER
462 void
463 ta_glyph_hints_scale_dim(TA_GlyphHints hints,
464 TA_Dimension dim,
465 FT_Fixed scale,
466 FT_Pos delta);
467 #endif
469 void
470 ta_glyph_hints_done(TA_GlyphHints hints);
473 #define TA_SEGMENT_LEN(seg) \
474 ((seg)->max_coord - (seg)->min_coord)
476 #define TA_SEGMENT_DIST(seg1, seg2) \
477 (((seg1)->pos > (seg2)->pos) ? (seg1)->pos - (seg2)->pos \
478 : (seg2)->pos - (seg1)->pos)
480 #endif /* __TAHINTS_H__ */
482 /* end of tahints.h */