1 /***************************************************************************/
5 /* Auto-fitter hinting routines (specification). */
7 /* Copyright 2003-2008, 2010-2012, 2014 by */
8 /* David Turner, Robert Wilhelm, and Werner Lemberg. */
10 /* This file is part of the FreeType project, and may only be used, */
11 /* modified, and distributed under the terms of the FreeType project */
12 /* license, LICENSE.TXT. By continuing to use, modify, or distribute */
13 /* this file you indicate that you have read the license and */
14 /* understand and accept it fully. */
16 /***************************************************************************/
24 #define xxAF_SORT_SEGMENTS
29 * The definition of outline glyph hints. These are shared by all
30 * writing system analysis routines (until now).
33 typedef enum AF_Dimension_
35 AF_DIMENSION_HORZ = 0, /* x coordinates, */
36 /* i.e., vertical segments & edges */
37 AF_DIMENSION_VERT = 1, /* y coordinates, */
38 /* i.e., horizontal segments & edges */
40 AF_DIMENSION_MAX /* do not remove */
45 /* hint directions -- the values are computed so that two vectors are */
46 /* in opposite directions iff `dir1 + dir2 == 0' */
47 typedef enum AF_Direction_
59 * The following explanations are mostly taken from the article
61 * Real-Time Grid Fitting of Typographic Outlines
63 * by David Turner and Werner Lemberg
65 * http://www.tug.org/TUGboat/Articles/tb24-3/lemberg.pdf
67 * with appropriate updates.
72 * `af_{cjk,latin,...}_hints_compute_segments' are the functions to
73 * find segments in an outline.
75 * A segment is a series of at least two consecutive points that are
76 * approximately aligned along a coordinate axis. The analysis to do
77 * so is specific to a writing system.
82 * `af_{cjk,latin,...}_hints_compute_edges' are the functions to find
85 * As soon as segments are defined, the auto-hinter groups them into
86 * edges. An edge corresponds to a single position on the main
87 * dimension that collects one or more segments (allowing for a small
90 * As an example, the `latin' writing system first tries to grid-fit
91 * edges, then to align segments on the edges unless it detects that
101 * +------<-----+ +-----<------+
105 * +--------------->------------------+
111 * Stems are detected by `af_{cjk,latin,...}_hint_edges'.
113 * Segments need to be `linked' to other ones in order to detect stems.
114 * A stem is made of two segments that face each other in opposite
115 * directions and that are sufficiently close to each other. Using
116 * vocabulary from the TrueType specification, stem segments form a
119 * In the above ASCII drawing, the horizontal segments are BC, DE, and
120 * FG; the vertical segments are AB, CD, EF, and GH.
122 * Each segment has at most one `best' candidate to form a black
123 * distance, or no candidate at all. Notice that two distinct segments
124 * can have the same candidate, which frequently means a serif.
126 * A stem is recognized by the following condition:
128 * best segment_1 = segment_2 && best segment_2 = segment_1
130 * The best candidate is stored in field `link' in structure
133 * In the above ASCII drawing, the best candidate for both AB and CD is
134 * GH, while the best candidate for GH is AB. Similarly, the best
135 * candidate for EF and GH is AB, while the best candidate for AB is
138 * The detection and handling of stems is dependent on the writing
144 * Serifs are detected by `af_{cjk,latin,...}_hint_edges'.
146 * In comparison to a stem, a serif (as handled by the auto-hinter
147 * module that takes care of the `latin' writing system) has
149 * best segment_1 = segment_2 && best segment_2 != segment_1
151 * where segment_1 corresponds to the serif segment (CD and EF in the
152 * above ASCII drawing).
154 * The best candidate is stored in field `serif' in structure
155 * `AF_Segment' (and `link' is set to NULL).
160 * A point is called `touched' if it has been processed somehow by the
161 * auto-hinter. It basically means that it shouldn't be moved again
162 * (or moved only under certain constraints to preserve the already
163 * applied processing).
166 * Flat and round segments
168 * Segments are `round' or `flat', depending on the series of points
169 * that define them. A segment is round if the next and previous point
170 * of an extremum (which can be either a single point or sequence of
171 * points) are both conic or cubic control points. Otherwise, a
172 * segment with an extremum is flat.
177 * Experience has shown that points not part of an edge need to be
178 * interpolated linearly between their two closest edges, even if these
179 * are not part of the contour of those particular points. Typical
180 * candidates for this are
182 * - angle points (i.e., points where the `in' and `out' direction
185 * - inflection points (i.e., where the `in' and `out' angles are the
186 * same, but the curvature changes sign) [currently, such points
187 * aren't handled specially in the auto-hinter]
189 * `af_glyph_hints_align_strong_points' is the function that takes
190 * care of such situations; it is equivalent to the TrueType `IP'
191 * hinting instruction.
196 * Other points in the outline must be interpolated using the
197 * coordinates of their previous and next unfitted contour neighbours.
198 * These are called `weak points' and are touched by the function
199 * `af_glyph_hints_align_weak_points', equivalent to the TrueType `IUP'
200 * hinting instruction. Typical candidates are control points and
201 * points on the contour without a major direction.
203 * The major effect is to reduce possible distortion caused by
204 * alignment of edges and strong points, thus weak points are processed
205 * after strong points.
209 /* point hint flags */
210 typedef enum AF_Flags_
214 /* point type flags */
215 AF_FLAG_CONIC = 1 << 0,
216 AF_FLAG_CUBIC = 1 << 1,
217 AF_FLAG_CONTROL = AF_FLAG_CONIC | AF_FLAG_CUBIC,
219 /* point touch flags */
220 AF_FLAG_TOUCH_X = 1 << 2,
221 AF_FLAG_TOUCH_Y = 1 << 3,
223 /* candidates for weak interpolation have this flag set */
224 AF_FLAG_WEAK_INTERPOLATION = 1 << 4
229 /* edge hint flags */
230 typedef enum AF_Edge_Flags_
233 AF_EDGE_ROUND = 1 << 0,
234 AF_EDGE_SERIF = 1 << 1,
235 AF_EDGE_DONE = 1 << 2,
236 AF_EDGE_NEUTRAL = 1 << 3 /* set if edge aligns to a neutral blue zone */
241 typedef struct AF_PointRec_* AF_Point;
242 typedef struct AF_SegmentRec_* AF_Segment;
243 typedef struct AF_EdgeRec_* AF_Edge;
246 typedef struct AF_PointRec_
248 FT_UShort flags; /* point flags used by hinter */
249 FT_Char in_dir; /* direction of inwards vector */
250 FT_Char out_dir; /* direction of outwards vector */
252 FT_Pos ox, oy; /* original, scaled position */
253 FT_Short fx, fy; /* original, unscaled position (in font units) */
254 FT_Pos x, y; /* current position */
255 FT_Pos u, v; /* current (x,y) or (y,x) depending on context */
257 AF_Point next; /* next point in contour */
258 AF_Point prev; /* previous point in contour */
263 typedef struct AF_SegmentRec_
265 FT_Byte flags; /* edge/segment flags for this segment */
266 FT_Char dir; /* segment direction */
267 FT_Short pos; /* position of segment */
268 FT_Short min_coord; /* minimum coordinate of segment */
269 FT_Short max_coord; /* maximum coordinate of segment */
270 FT_Short height; /* the hinted segment height */
272 AF_Edge edge; /* the segment's parent edge */
273 AF_Segment edge_next; /* link to next segment in parent edge */
275 AF_Segment link; /* (stem) link segment */
276 AF_Segment serif; /* primary segment for serifs */
277 FT_Pos num_linked; /* number of linked segments */
278 FT_Pos score; /* used during stem matching */
279 FT_Pos len; /* used during stem matching */
281 AF_Point first; /* first point in edge segment */
282 AF_Point last; /* last point in edge segment */
287 typedef struct AF_EdgeRec_
289 FT_Short fpos; /* original, unscaled position (in font units) */
290 FT_Pos opos; /* original, scaled position */
291 FT_Pos pos; /* current position */
293 FT_Byte flags; /* edge flags */
294 FT_Char dir; /* edge direction */
295 FT_Fixed scale; /* used to speed up interpolation between edges */
297 AF_Width blue_edge; /* non-NULL if this is a blue edge */
298 AF_Edge link; /* link edge */
299 AF_Edge serif; /* primary edge for serifs */
300 FT_Short num_linked; /* number of linked edges */
301 FT_Int score; /* used during stem matching */
303 AF_Segment first; /* first segment in edge */
304 AF_Segment last; /* last segment in edge */
309 typedef struct AF_AxisHintsRec_
311 FT_Int num_segments; /* number of used segments */
312 FT_Int max_segments; /* number of allocated segments */
313 AF_Segment segments; /* segments array */
314 #ifdef AF_SORT_SEGMENTS
318 FT_Int num_edges; /* number of used edges */
319 FT_Int max_edges; /* number of allocated edges */
320 AF_Edge edges; /* edges array */
322 AF_Direction major_dir; /* either vertical or horizontal */
324 } AF_AxisHintsRec, *AF_AxisHints;
327 typedef struct AF_GlyphHintsRec_
337 FT_Int max_points; /* number of allocated points */
338 FT_Int num_points; /* number of used points */
339 AF_Point points; /* points array */
341 FT_Int max_contours; /* number of allocated contours */
342 FT_Int num_contours; /* number of used contours */
343 AF_Point* contours; /* contours array */
345 AF_AxisHintsRec axis[AF_DIMENSION_MAX];
347 FT_UInt32 scaler_flags; /* copy of scaler flags */
348 FT_UInt32 other_flags; /* free for style-specific */
349 /* implementations */
350 AF_StyleMetrics metrics;
352 FT_Pos xmin_delta; /* used for warping */
358 #define AF_HINTS_TEST_SCALER( h, f ) ( (h)->scaler_flags & (f) )
359 #define AF_HINTS_TEST_OTHER( h, f ) ( (h)->other_flags & (f) )
362 #ifdef FT_DEBUG_AUTOFIT
364 #define AF_HINTS_DO_HORIZONTAL( h ) \
365 ( !_af_debug_disable_horz_hints && \
366 !AF_HINTS_TEST_SCALER( h, AF_SCALER_FLAG_NO_HORIZONTAL ) )
368 #define AF_HINTS_DO_VERTICAL( h ) \
369 ( !_af_debug_disable_vert_hints && \
370 !AF_HINTS_TEST_SCALER( h, AF_SCALER_FLAG_NO_VERTICAL ) )
372 #define AF_HINTS_DO_ADVANCE( h ) \
373 !AF_HINTS_TEST_SCALER( h, AF_SCALER_FLAG_NO_ADVANCE )
375 #define AF_HINTS_DO_BLUES( h ) ( !_af_debug_disable_blue_hints )
377 #else /* !FT_DEBUG_AUTOFIT */
379 #define AF_HINTS_DO_HORIZONTAL( h ) \
380 !AF_HINTS_TEST_SCALER( h, AF_SCALER_FLAG_NO_HORIZONTAL )
382 #define AF_HINTS_DO_VERTICAL( h ) \
383 !AF_HINTS_TEST_SCALER( h, AF_SCALER_FLAG_NO_VERTICAL )
385 #define AF_HINTS_DO_ADVANCE( h ) \
386 !AF_HINTS_TEST_SCALER( h, AF_SCALER_FLAG_NO_ADVANCE )
388 #define AF_HINTS_DO_BLUES( h ) 1
390 #endif /* !FT_DEBUG_AUTOFIT */
393 FT_LOCAL( AF_Direction )
394 af_direction_compute( FT_Pos dx,
399 af_axis_hints_new_segment( AF_AxisHints axis,
401 AF_Segment *asegment );
404 af_axis_hints_new_edge( AF_AxisHints axis,
411 af_glyph_hints_init( AF_GlyphHints hints,
415 af_glyph_hints_rescale( AF_GlyphHints hints,
416 AF_StyleMetrics metrics );
419 af_glyph_hints_reload( AF_GlyphHints hints,
420 FT_Outline* outline );
423 af_glyph_hints_save( AF_GlyphHints hints,
424 FT_Outline* outline );
427 af_glyph_hints_align_edge_points( AF_GlyphHints hints,
431 af_glyph_hints_align_strong_points( AF_GlyphHints hints,
435 af_glyph_hints_align_weak_points( AF_GlyphHints hints,
438 #ifdef AF_CONFIG_OPTION_USE_WARPER
440 af_glyph_hints_scale_dim( AF_GlyphHints hints,
447 af_glyph_hints_done( AF_GlyphHints hints );
451 #define AF_SEGMENT_LEN( seg ) ( (seg)->max_coord - (seg)->min_coord )
453 #define AF_SEGMENT_DIST( seg1, seg2 ) ( ( (seg1)->pos > (seg2)->pos ) \
454 ? (seg1)->pos - (seg2)->pos \
455 : (seg2)->pos - (seg1)->pos )
460 #endif /* __AFHINTS_H__ */