1 /***************************************************************************/
2 /*                                                                         */
3 /*  afhints.h                                                              */
4 /*                                                                         */
5 /*    Auto-fitter hinting routines (specification).                        */
6 /*                                                                         */
7 /*  Copyright 2003-2015 by                                                 */
8 /*  David Turner, Robert Wilhelm, and Werner Lemberg.                      */
9 /*                                                                         */
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.                                        */
15 /*                                                                         */
16 /***************************************************************************/
17 
18 
19 #ifndef __AFHINTS_H__
20 #define __AFHINTS_H__
21 
22 #include "aftypes.h"
23 
24 #define xxAF_SORT_SEGMENTS
25 
26 FT_BEGIN_HEADER
27 
28   /*
29    *  The definition of outline glyph hints.  These are shared by all
30    *  writing system analysis routines (until now).
31    */
32 
33   typedef enum  AF_Dimension_
34   {
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 */
39 
40     AF_DIMENSION_MAX  /* do not remove */
41 
42   } AF_Dimension;
43 
44 
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_
48   {
49     AF_DIR_NONE  =  4,
50     AF_DIR_RIGHT =  1,
51     AF_DIR_LEFT  = -1,
52     AF_DIR_UP    =  2,
53     AF_DIR_DOWN  = -2
54 
55   } AF_Direction;
56 
57 
58   /*
59    *  The following explanations are mostly taken from the article
60    *
61    *    Real-Time Grid Fitting of Typographic Outlines
62    *
63    *  by David Turner and Werner Lemberg
64    *
65    *    http://www.tug.org/TUGboat/Articles/tb24-3/lemberg.pdf
66    *
67    *  with appropriate updates.
68    *
69    *
70    *  Segments
71    *
72    *    `af_{cjk,latin,...}_hints_compute_segments' are the functions to
73    *    find segments in an outline.
74    *
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.
78    *
79    *
80    *  Edges
81    *
82    *    `af_{cjk,latin,...}_hints_compute_edges' are the functions to find
83    *    edges.
84    *
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
88    *    threshold).
89    *
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
92    *    they form a serif.
93    *
94    *
95    *                      A          H
96    *                       |        |
97    *                       |        |
98    *                       |        |
99    *                       |        |
100    *         C             |        |             F
101    *          +------<-----+        +-----<------+
102    *          |             B      G             |
103    *          |                                  |
104    *          |                                  |
105    *          +--------------->------------------+
106    *         D                                    E
107    *
108    *
109    *  Stems
110    *
111    *    Stems are detected by `af_{cjk,latin,...}_hint_edges'.
112    *
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
117    *    `black distance'.
118    *
119    *    In the above ASCII drawing, the horizontal segments are BC, DE, and
120    *    FG; the vertical segments are AB, CD, EF, and GH.
121    *
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.
125    *
126    *    A stem is recognized by the following condition:
127    *
128    *      best segment_1 = segment_2 && best segment_2 = segment_1
129    *
130    *    The best candidate is stored in field `link' in structure
131    *    `AF_Segment'.
132    *
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
136    *    GH.
137    *
138    *    The detection and handling of stems is dependent on the writing
139    *    system.
140    *
141    *
142    *  Serifs
143    *
144    *    Serifs are detected by `af_{cjk,latin,...}_hint_edges'.
145    *
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
148    *
149    *      best segment_1 = segment_2 && best segment_2 != segment_1
150    *
151    *    where segment_1 corresponds to the serif segment (CD and EF in the
152    *    above ASCII drawing).
153    *
154    *    The best candidate is stored in field `serif' in structure
155    *    `AF_Segment' (and `link' is set to NULL).
156    *
157    *
158    *  Touched points
159    *
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).
164    *
165    *
166    *  Flat and round segments
167    *
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.
173    *
174    *
175    *  Strong Points
176    *
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
181    *
182    *    - angle points (i.e., points where the `in' and `out' direction
183    *      differ greatly)
184    *
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]
188    *
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.
192    *
193    *
194    *  Weak Points
195    *
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.
202    *
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.
206    */
207 
208 
209   /* point hint flags */
210 #define AF_FLAG_NONE  0
211 
212   /* point type flags */
213 #define AF_FLAG_CONIC    ( 1U << 0 )
214 #define AF_FLAG_CUBIC    ( 1U << 1 )
215 #define AF_FLAG_CONTROL  ( AF_FLAG_CONIC | AF_FLAG_CUBIC )
216 
217   /* point touch flags */
218 #define AF_FLAG_TOUCH_X  ( 1U << 2 )
219 #define AF_FLAG_TOUCH_Y  ( 1U << 3 )
220 
221   /* candidates for weak interpolation have this flag set */
222 #define AF_FLAG_WEAK_INTERPOLATION  ( 1U << 4 )
223 
224 
225   /* edge hint flags */
226 #define AF_EDGE_NORMAL  0
227 #define AF_EDGE_ROUND    ( 1U << 0 )
228 #define AF_EDGE_SERIF    ( 1U << 1 )
229 #define AF_EDGE_DONE     ( 1U << 2 )
230 #define AF_EDGE_NEUTRAL  ( 1U << 3 ) /* edge aligns to a neutral blue zone */
231 
232 
233   typedef struct AF_PointRec_*    AF_Point;
234   typedef struct AF_SegmentRec_*  AF_Segment;
235   typedef struct AF_EdgeRec_*     AF_Edge;
236 
237 
238   typedef struct  AF_PointRec_
239   {
240     FT_UShort  flags;    /* point flags used by hinter   */
241     FT_Char    in_dir;   /* direction of inwards vector  */
242     FT_Char    out_dir;  /* direction of outwards vector */
243 
244     FT_Pos     ox, oy;   /* original, scaled position                   */
245     FT_Short   fx, fy;   /* original, unscaled position (in font units) */
246     FT_Pos     x, y;     /* current position                            */
247     FT_Pos     u, v;     /* current (x,y) or (y,x) depending on context */
248 
249     AF_Point   next;     /* next point in contour     */
250     AF_Point   prev;     /* previous point in contour */
251 
252   } AF_PointRec;
253 
254 
255   typedef struct  AF_SegmentRec_
256   {
257     FT_Byte     flags;       /* edge/segment flags for this segment */
258     FT_Char     dir;         /* segment direction                   */
259     FT_Short    pos;         /* position of segment                 */
260     FT_Short    min_coord;   /* minimum coordinate of segment       */
261     FT_Short    max_coord;   /* maximum coordinate of segment       */
262     FT_Short    height;      /* the hinted segment height           */
263 
264     AF_Edge     edge;        /* the segment's parent edge           */
265     AF_Segment  edge_next;   /* link to next segment in parent edge */
266 
267     AF_Segment  link;        /* (stem) link segment        */
268     AF_Segment  serif;       /* primary segment for serifs */
269     FT_Pos      num_linked;  /* number of linked segments  */
270     FT_Pos      score;       /* used during stem matching  */
271     FT_Pos      len;         /* used during stem matching  */
272 
273     AF_Point    first;       /* first point in edge segment */
274     AF_Point    last;        /* last point in edge segment  */
275 
276   } AF_SegmentRec;
277 
278 
279   typedef struct  AF_EdgeRec_
280   {
281     FT_Short    fpos;       /* original, unscaled position (in font units) */
282     FT_Pos      opos;       /* original, scaled position                   */
283     FT_Pos      pos;        /* current position                            */
284 
285     FT_Byte     flags;      /* edge flags                                   */
286     FT_Char     dir;        /* edge direction                               */
287     FT_Fixed    scale;      /* used to speed up interpolation between edges */
288 
289     AF_Width    blue_edge;  /* non-NULL if this is a blue edge */
290     AF_Edge     link;       /* link edge                       */
291     AF_Edge     serif;      /* primary edge for serifs         */
292     FT_Short    num_linked; /* number of linked edges          */
293     FT_Int      score;      /* used during stem matching       */
294 
295     AF_Segment  first;      /* first segment in edge */
296     AF_Segment  last;       /* last segment in edge  */
297 
298   } AF_EdgeRec;
299 
300 #define AF_SEGMENTS_EMBEDDED  18   /* number of embedded segments   */
301 #define AF_EDGES_EMBEDDED     12   /* number of embedded edges      */
302 
303   typedef struct  AF_AxisHintsRec_
304   {
305     FT_Int        num_segments; /* number of used segments      */
306     FT_Int        max_segments; /* number of allocated segments */
307     AF_Segment    segments;     /* segments array               */
308 #ifdef AF_SORT_SEGMENTS
309     FT_Int        mid_segments;
310 #endif
311 
312     FT_Int        num_edges;    /* number of used edges      */
313     FT_Int        max_edges;    /* number of allocated edges */
314     AF_Edge       edges;        /* edges array               */
315 
316     AF_Direction  major_dir;    /* either vertical or horizontal */
317 
318     /* two arrays to avoid allocation penalty */
319     struct
320     {
321       AF_SegmentRec  segments[AF_SEGMENTS_EMBEDDED];
322       AF_EdgeRec     edges[AF_EDGES_EMBEDDED];
323     } embedded;
324 
325 
326   } AF_AxisHintsRec, *AF_AxisHints;
327 
328 
329 #define AF_POINTS_EMBEDDED     96   /* number of embedded points   */
330 #define AF_CONTOURS_EMBEDDED    8   /* number of embedded contours */
331 
332   typedef struct  AF_GlyphHintsRec_
333   {
334     FT_Memory        memory;
335 
336     FT_Fixed         x_scale;
337     FT_Pos           x_delta;
338 
339     FT_Fixed         y_scale;
340     FT_Pos           y_delta;
341 
342     FT_Int           max_points;    /* number of allocated points */
343     FT_Int           num_points;    /* number of used points      */
344     AF_Point         points;        /* points array               */
345 
346     FT_Int           max_contours;  /* number of allocated contours */
347     FT_Int           num_contours;  /* number of used contours      */
348     AF_Point*        contours;      /* contours array               */
349 
350     AF_AxisHintsRec  axis[AF_DIMENSION_MAX];
351 
352     FT_UInt32        scaler_flags;  /* copy of scaler flags    */
353     FT_UInt32        other_flags;   /* free for style-specific */
354                                     /* implementations         */
355     AF_StyleMetrics  metrics;
356 
357     FT_Pos           xmin_delta;    /* used for warping */
358     FT_Pos           xmax_delta;
359 
360     /* Two arrays to avoid allocation penalty.            */
361     /* The `embedded' structure must be the last element! */
362     struct
363     {
364       AF_Point       contours[AF_CONTOURS_EMBEDDED];
365       AF_PointRec    points[AF_POINTS_EMBEDDED];
366     } embedded;
367 
368   } AF_GlyphHintsRec;
369 
370 
371 #define AF_HINTS_TEST_SCALER( h, f )  ( (h)->scaler_flags & (f) )
372 #define AF_HINTS_TEST_OTHER( h, f )   ( (h)->other_flags  & (f) )
373 
374 
375 #ifdef FT_DEBUG_AUTOFIT
376 
377 #define AF_HINTS_DO_HORIZONTAL( h )                                     \
378           ( !_af_debug_disable_horz_hints                            && \
379             !AF_HINTS_TEST_SCALER( h, AF_SCALER_FLAG_NO_HORIZONTAL ) )
380 
381 #define AF_HINTS_DO_VERTICAL( h )                                     \
382           ( !_af_debug_disable_vert_hints                          && \
383             !AF_HINTS_TEST_SCALER( h, AF_SCALER_FLAG_NO_VERTICAL ) )
384 
385 #define AF_HINTS_DO_BLUES( h )  ( !_af_debug_disable_blue_hints )
386 
387 #else /* !FT_DEBUG_AUTOFIT */
388 
389 #define AF_HINTS_DO_HORIZONTAL( h )                                \
390           !AF_HINTS_TEST_SCALER( h, AF_SCALER_FLAG_NO_HORIZONTAL )
391 
392 #define AF_HINTS_DO_VERTICAL( h )                                \
393           !AF_HINTS_TEST_SCALER( h, AF_SCALER_FLAG_NO_VERTICAL )
394 
395 #define AF_HINTS_DO_BLUES( h )  1
396 
397 #endif /* !FT_DEBUG_AUTOFIT */
398 
399 
400 #define AF_HINTS_DO_ADVANCE( h )                                \
401           !AF_HINTS_TEST_SCALER( h, AF_SCALER_FLAG_NO_ADVANCE )
402 
403 #define AF_HINTS_DO_WARP( h )                                  \
404           !AF_HINTS_TEST_SCALER( h, AF_SCALER_FLAG_NO_WARPER )
405 
406 
407 
408   FT_LOCAL( AF_Direction )
409   af_direction_compute( FT_Pos  dx,
410                         FT_Pos  dy );
411 
412 
413   FT_LOCAL( FT_Error )
414   af_axis_hints_new_segment( AF_AxisHints  axis,
415                              FT_Memory     memory,
416                              AF_Segment   *asegment );
417 
418   FT_LOCAL( FT_Error)
419   af_axis_hints_new_edge( AF_AxisHints  axis,
420                           FT_Int        fpos,
421                           AF_Direction  dir,
422                           FT_Memory     memory,
423                           AF_Edge      *edge );
424 
425   FT_LOCAL( void )
426   af_glyph_hints_init( AF_GlyphHints  hints,
427                        FT_Memory      memory );
428 
429   FT_LOCAL( void )
430   af_glyph_hints_rescale( AF_GlyphHints    hints,
431                           AF_StyleMetrics  metrics );
432 
433   FT_LOCAL( FT_Error )
434   af_glyph_hints_reload( AF_GlyphHints  hints,
435                          FT_Outline*    outline );
436 
437   FT_LOCAL( void )
438   af_glyph_hints_save( AF_GlyphHints  hints,
439                        FT_Outline*    outline );
440 
441   FT_LOCAL( void )
442   af_glyph_hints_align_edge_points( AF_GlyphHints  hints,
443                                     AF_Dimension   dim );
444 
445   FT_LOCAL( void )
446   af_glyph_hints_align_strong_points( AF_GlyphHints  hints,
447                                       AF_Dimension   dim );
448 
449   FT_LOCAL( void )
450   af_glyph_hints_align_weak_points( AF_GlyphHints  hints,
451                                     AF_Dimension   dim );
452 
453 #ifdef AF_CONFIG_OPTION_USE_WARPER
454   FT_LOCAL( void )
455   af_glyph_hints_scale_dim( AF_GlyphHints  hints,
456                             AF_Dimension   dim,
457                             FT_Fixed       scale,
458                             FT_Pos         delta );
459 #endif
460 
461   FT_LOCAL( void )
462   af_glyph_hints_done( AF_GlyphHints  hints );
463 
464 /* */
465 
466 #define AF_SEGMENT_LEN( seg )          ( (seg)->max_coord - (seg)->min_coord )
467 
468 #define AF_SEGMENT_DIST( seg1, seg2 )  ( ( (seg1)->pos > (seg2)->pos )   \
469                                            ? (seg1)->pos - (seg2)->pos   \
470                                            : (seg2)->pos - (seg1)->pos )
471 
472 
473 FT_END_HEADER
474 
475 #endif /* __AFHINTS_H__ */
476 
477 
478 /* END */
479