Mention GPL-only files.
[ttfautohint.git] / src / tahints.h
blob2e12801f7e2586d97bdab479dfee01221cef5f2a
1 /* tahints.h */
3 /*
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> */
20 #ifndef __TAHINTS_H__
21 #define __TAHINTS_H__
23 #include "tatypes.h"
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 */
36 } TA_Dimension;
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_
44 TA_DIR_NONE = 4,
45 TA_DIR_RIGHT = 1,
46 TA_DIR_LEFT= -1,
47 TA_DIR_UP = 2,
48 TA_DIR_DOWN = -2
49 } 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
62 * Segments
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.
74 * Edges
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
79 * threshold).
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.
88 * A H
89 * | |
90 * | |
91 * | |
92 * | |
93 * C | | F
94 * +------<-----+ +-----<------+
95 * | B G |
96 * | |
97 * | |
98 * +--------------->------------------+
99 * D E
102 * Stems
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
108 * `black distance'.
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
122 * `TA_Segment'.
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
129 * GH.
132 * Serifs
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'.
147 * Touched points
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.
164 * Strong Points
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
172 * differ greatly)
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.
182 * Weak Points
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 */
249 } TA_PointRec;
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 */
272 } TA_SegmentRec;
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 */
296 } TA_EdgeRec;
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
305 FT_Int mid_segments;
306 #endif
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_ {
317 /* point actions */
318 ta_ip_before,
319 ta_ip_after,
320 ta_ip_on,
321 ta_ip_between,
323 /* edge actions */
324 ta_blue,
325 ta_blue_anchor,
326 ta_anchor,
327 ta_adjust,
328 ta_adjust_bound,
329 ta_link,
330 ta_stem,
331 ta_stem_bound,
332 ta_serif,
333 ta_serif_lower_bound,
334 ta_serif_upper_bound,
335 ta_serif_lower_upper_bound,
336 ta_serif_anchor,
337 ta_serif_anchor_lower_bound,
338 ta_serif_anchor_upper_bound,
339 ta_serif_anchor_lower_upper_bound,
340 ta_serif_link1,
341 ta_serif_link1_lower_bound,
342 ta_serif_link1_upper_bound,
343 ta_serif_link1_lower_upper_bound,
344 ta_serif_link2,
345 ta_serif_link2_lower_bound,
346 ta_serif_link2_upper_bound,
347 ta_serif_link2_lower_upper_bound,
349 ta_bound
350 } TA_Action;
353 typedef void
354 (*TA_Hints_Recorder)(TA_Action action,
355 TA_GlyphHints hints,
356 TA_Dimension dim,
357 void* arg1, /* TA_Point or TA_Edge */
358 TA_Edge arg2,
359 TA_Edge arg3,
360 TA_Edge lower_bound,
361 TA_Edge upper_bound);
363 typedef struct TA_GlyphHintsRec_
365 FT_Fixed x_scale;
366 FT_Pos x_delta;
368 FT_Fixed y_scale;
369 FT_Pos y_delta;
371 FT_Int max_points; /* number of allocated points */
372 FT_Int num_points; /* number of used points */
373 TA_Point points; /* points array */
375 FT_Int max_contours; /* number of allocated contours */
376 FT_Int num_contours; /* number of used contours */
377 TA_Point* contours; /* contours array */
379 TA_AxisHintsRec axis[TA_DIMENSION_MAX];
381 FT_UInt32 scaler_flags; /* copy of scaler flags */
382 FT_UInt32 other_flags; /* free for script-specific implementations */
383 TA_ScriptMetrics metrics;
385 FT_Pos xmin_delta; /* used for warping */
386 FT_Pos xmax_delta;
388 TA_Hints_Recorder recorder;
389 void *user;
390 } TA_GlyphHintsRec;
393 #define TA_HINTS_TEST_SCALER(h, f) \
394 ((h)->scaler_flags & (f))
395 #define TA_HINTS_TEST_OTHER(h, f) \
396 ((h)->other_flags & (f))
399 #ifdef TA_DEBUG
401 #define TA_HINTS_DO_HORIZONTAL(h) \
402 (!_ta_debug_disable_horz_hints \
403 && !TA_HINTS_TEST_SCALER(h, TA_SCALER_FLAG_NO_HORIZONTAL))
405 #define TA_HINTS_DO_VERTICAL(h) \
406 (!_ta_debug_disable_vert_hints \
407 && !TA_HINTS_TEST_SCALER(h, TA_SCALER_FLAG_NO_VERTICAL))
409 #define TA_HINTS_DO_ADVANCE(h) \
410 !TA_HINTS_TEST_SCALER(h, TA_SCALER_FLAG_NO_ADVANCE)
412 #define TA_HINTS_DO_BLUES(h) \
413 (!_ta_debug_disable_blue_hints)
415 #else /* !TA_DEBUG */
417 #define TA_HINTS_DO_HORIZONTAL(h) \
418 !TA_HINTS_TEST_SCALER(h, TA_SCALER_FLAG_NO_HORIZONTAL)
420 #define TA_HINTS_DO_VERTICAL(h) \
421 !TA_HINTS_TEST_SCALER(h, TA_SCALER_FLAG_NO_VERTICAL)
423 #define TA_HINTS_DO_ADVANCE(h) \
424 !TA_HINTS_TEST_SCALER(h, TA_SCALER_FLAG_NO_ADVANCE)
426 #define TA_HINTS_DO_BLUES(h) \
429 #endif /* !TA_DEBUG */
432 TA_Direction
433 ta_direction_compute(FT_Pos dx,
434 FT_Pos dy);
437 FT_Error
438 ta_axis_hints_new_segment(TA_AxisHints axis,
439 TA_Segment* asegment);
441 FT_Error
442 ta_axis_hints_new_edge(TA_AxisHints axis,
443 FT_Int fpos,
444 TA_Direction dir,
445 TA_Edge* edge);
447 void
448 ta_glyph_hints_init(TA_GlyphHints hints);
450 void
451 ta_glyph_hints_rescale(TA_GlyphHints hints,
452 TA_ScriptMetrics metrics);
454 FT_Error
455 ta_glyph_hints_reload(TA_GlyphHints hints,
456 FT_Outline* outline);
458 void
459 ta_glyph_hints_save(TA_GlyphHints hints,
460 FT_Outline* outline);
462 void
463 ta_glyph_hints_align_edge_points(TA_GlyphHints hints,
464 TA_Dimension dim);
466 void
467 ta_glyph_hints_align_strong_points(TA_GlyphHints hints,
468 TA_Dimension dim);
470 void
471 ta_glyph_hints_align_weak_points(TA_GlyphHints hints,
472 TA_Dimension dim);
474 #ifdef TA_CONFIG_OPTION_USE_WARPER
475 void
476 ta_glyph_hints_scale_dim(TA_GlyphHints hints,
477 TA_Dimension dim,
478 FT_Fixed scale,
479 FT_Pos delta);
480 #endif
482 void
483 ta_glyph_hints_done(TA_GlyphHints hints);
486 #define TA_SEGMENT_LEN(seg) \
487 ((seg)->max_coord - (seg)->min_coord)
489 #define TA_SEGMENT_DIST(seg1, seg2) \
490 (((seg1)->pos > (seg2)->pos) ? (seg1)->pos - (seg2)->pos \
491 : (seg2)->pos - (seg1)->pos)
493 #endif /* __TAHINTS_H__ */
495 /* end of tahints.h */