/src/freetype/src/autofit/afhints.h

https://bitbucket.org/cabalistic/ogredeps/ · C++ Header · 467 lines · 193 code · 96 blank · 178 comment · 2 complexity · 8475690ba1ca779f90b2e628d131dc1c MD5 · raw file

  1. /***************************************************************************/
  2. /* */
  3. /* afhints.h */
  4. /* */
  5. /* Auto-fitter hinting routines (specification). */
  6. /* */
  7. /* Copyright 2003-2008, 2010-2011 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. #ifndef __AFHINTS_H__
  18. #define __AFHINTS_H__
  19. #include "aftypes.h"
  20. #define xxAF_SORT_SEGMENTS
  21. FT_BEGIN_HEADER
  22. /*
  23. * The definition of outline glyph hints. These are shared by all
  24. * script analysis routines (until now).
  25. */
  26. typedef enum AF_Dimension_
  27. {
  28. AF_DIMENSION_HORZ = 0, /* x coordinates, */
  29. /* i.e., vertical segments & edges */
  30. AF_DIMENSION_VERT = 1, /* y coordinates, */
  31. /* i.e., horizontal segments & edges */
  32. AF_DIMENSION_MAX /* do not remove */
  33. } AF_Dimension;
  34. /* hint directions -- the values are computed so that two vectors are */
  35. /* in opposite directions iff `dir1 + dir2 == 0' */
  36. typedef enum AF_Direction_
  37. {
  38. AF_DIR_NONE = 4,
  39. AF_DIR_RIGHT = 1,
  40. AF_DIR_LEFT = -1,
  41. AF_DIR_UP = 2,
  42. AF_DIR_DOWN = -2
  43. } AF_Direction;
  44. /*
  45. * The following explanations are mostly taken from the article
  46. *
  47. * Real-Time Grid Fitting of Typographic Outlines
  48. *
  49. * by David Turner and Werner Lemberg
  50. *
  51. * http://www.tug.org/TUGboat/Articles/tb24-3/lemberg.pdf
  52. *
  53. *
  54. * Segments
  55. *
  56. * `af_{cjk,latin,...}_hints_compute_segments' are the functions to
  57. * find segments in an outline. A segment is a series of consecutive
  58. * points that are approximately aligned along a coordinate axis. The
  59. * analysis to do so is specific to a script.
  60. *
  61. * A segment must have at least two points, except in the case of
  62. * `fake' segments that are generated to hint metrics appropriately,
  63. * and which consist of a single point.
  64. *
  65. *
  66. * Edges
  67. *
  68. * As soon as segments are defined, the auto-hinter groups them into
  69. * edges. An edge corresponds to a single position on the main
  70. * dimension that collects one or more segments (allowing for a small
  71. * threshold).
  72. *
  73. * The auto-hinter first tries to grid fit edges, then to align
  74. * segments on the edges unless it detects that they form a serif.
  75. *
  76. * `af_{cjk,latin,...}_hints_compute_edges' are the functions to find
  77. * edges; they are specific to a script.
  78. *
  79. *
  80. * A H
  81. * | |
  82. * | |
  83. * | |
  84. * | |
  85. * C | | F
  86. * +------<-----+ +-----<------+
  87. * | B G |
  88. * | |
  89. * | |
  90. * +--------------->------------------+
  91. * D E
  92. *
  93. *
  94. * Stems
  95. *
  96. * Segments need to be `linked' to other ones in order to detect stems.
  97. * A stem is made of two segments that face each other in opposite
  98. * directions and that are sufficiently close to each other. Using
  99. * vocabulary from the TrueType specification, stem segments form a
  100. * `black distance'.
  101. *
  102. * In the above ASCII drawing, the horizontal segments are BC, DE, and
  103. * FG; the vertical segments are AB, CD, EF, and GH.
  104. *
  105. * Each segment has at most one `best' candidate to form a black
  106. * distance, or no candidate at all. Notice that two distinct segments
  107. * can have the same candidate, which frequently means a serif.
  108. *
  109. * A stem is recognized by the following condition:
  110. *
  111. * best segment_1 = segment_2 && best segment_2 = segment_1
  112. *
  113. * The best candidate is stored in field `link' in structure
  114. * `AF_Segment'.
  115. *
  116. * Stems are detected by `af_{cjk,latin,...}_hint_edges'.
  117. *
  118. * In the above ASCII drawing, the best candidate for both AB and CD is
  119. * GH, while the best candidate for GH is AB. Similarly, the best
  120. * candidate for EF and GH is AB, while the best candidate for AB is
  121. * GH.
  122. *
  123. *
  124. * Serifs
  125. *
  126. * On the opposite, a serif has
  127. *
  128. * best segment_1 = segment_2 && best segment_2 != segment_1
  129. *
  130. * where segment_1 corresponds to the serif segment (CD and EF in the
  131. * above ASCII drawing).
  132. *
  133. * The best candidate is stored in field `serif' in structure
  134. * `AF_Segment' (and `link' is set to NULL).
  135. *
  136. * Serifs are detected by `af_{cjk,latin,...}_hint_edges'.
  137. *
  138. *
  139. * Touched points
  140. *
  141. * A point is called `touched' if it has been processed somehow by the
  142. * auto-hinter. It basically means that it shouldn't be moved again
  143. * (or moved only under certain constraints to preserve the already
  144. * applied processing).
  145. *
  146. *
  147. * Flat and round segments
  148. *
  149. * Segments are `round' or `flat', depending on the series of points
  150. * that define them. A segment is round if the next and previous point
  151. * of an extremum (which can be either a single point or sequence of
  152. * points) are both conic or cubic control points. Otherwise, a
  153. * segment with an extremum is flat.
  154. *
  155. *
  156. * Strong Points
  157. *
  158. * Experience has shown that points which are not part of an edge need
  159. * to be interpolated linearly between their two closest edges, even if
  160. * these are not part of the contour of those particular points.
  161. * Typical candidates for this are
  162. *
  163. * - angle points (i.e., points where the `in' and `out' direction
  164. * differ greatly)
  165. *
  166. * - inflection points (i.e., where the `in' and `out' angles are the
  167. * same, but the curvature changes sign)
  168. *
  169. * `af_glyph_hints_align_strong_points' is the function which takes
  170. * care of such situations; it is equivalent to the TrueType `IP'
  171. * hinting instruction.
  172. *
  173. *
  174. * Weak Points
  175. *
  176. * Other points in the outline must be interpolated using the
  177. * coordinates of their previous and next unfitted contour neighbours.
  178. * These are called `weak points' and are touched by the function
  179. * `af_glyph_hints_align_weak_points', equivalent to the TrueType `IUP'
  180. * hinting instruction. Typical candidates are control points and
  181. * points on the contour without a major direction.
  182. *
  183. * The major effect is to reduce possible distortion caused by
  184. * alignment of edges and strong points, thus weak points are processed
  185. * after strong points.
  186. */
  187. /* point hint flags */
  188. typedef enum AF_Flags_
  189. {
  190. AF_FLAG_NONE = 0,
  191. /* point type flags */
  192. AF_FLAG_CONIC = 1 << 0,
  193. AF_FLAG_CUBIC = 1 << 1,
  194. AF_FLAG_CONTROL = AF_FLAG_CONIC | AF_FLAG_CUBIC,
  195. /* point extremum flags */
  196. AF_FLAG_EXTREMA_X = 1 << 2,
  197. AF_FLAG_EXTREMA_Y = 1 << 3,
  198. /* point roundness flags */
  199. AF_FLAG_ROUND_X = 1 << 4,
  200. AF_FLAG_ROUND_Y = 1 << 5,
  201. /* point touch flags */
  202. AF_FLAG_TOUCH_X = 1 << 6,
  203. AF_FLAG_TOUCH_Y = 1 << 7,
  204. /* candidates for weak interpolation have this flag set */
  205. AF_FLAG_WEAK_INTERPOLATION = 1 << 8,
  206. /* all inflection points in the outline have this flag set */
  207. AF_FLAG_INFLECTION = 1 << 9
  208. } AF_Flags;
  209. /* edge hint flags */
  210. typedef enum AF_Edge_Flags_
  211. {
  212. AF_EDGE_NORMAL = 0,
  213. AF_EDGE_ROUND = 1 << 0,
  214. AF_EDGE_SERIF = 1 << 1,
  215. AF_EDGE_DONE = 1 << 2
  216. } AF_Edge_Flags;
  217. typedef struct AF_PointRec_* AF_Point;
  218. typedef struct AF_SegmentRec_* AF_Segment;
  219. typedef struct AF_EdgeRec_* AF_Edge;
  220. typedef struct AF_PointRec_
  221. {
  222. FT_UShort flags; /* point flags used by hinter */
  223. FT_Char in_dir; /* direction of inwards vector */
  224. FT_Char out_dir; /* direction of outwards vector */
  225. FT_Pos ox, oy; /* original, scaled position */
  226. FT_Short fx, fy; /* original, unscaled position (font units) */
  227. FT_Pos x, y; /* current position */
  228. FT_Pos u, v; /* current (x,y) or (y,x) depending on context */
  229. AF_Point next; /* next point in contour */
  230. AF_Point prev; /* previous point in contour */
  231. } AF_PointRec;
  232. typedef struct AF_SegmentRec_
  233. {
  234. FT_Byte flags; /* edge/segment flags for this segment */
  235. FT_Char dir; /* segment direction */
  236. FT_Short pos; /* position of segment */
  237. FT_Short min_coord; /* minimum coordinate of segment */
  238. FT_Short max_coord; /* maximum coordinate of segment */
  239. FT_Short height; /* the hinted segment height */
  240. AF_Edge edge; /* the segment's parent edge */
  241. AF_Segment edge_next; /* link to next segment in parent edge */
  242. AF_Segment link; /* (stem) link segment */
  243. AF_Segment serif; /* primary segment for serifs */
  244. FT_Pos num_linked; /* number of linked segments */
  245. FT_Pos score; /* used during stem matching */
  246. FT_Pos len; /* used during stem matching */
  247. AF_Point first; /* first point in edge segment */
  248. AF_Point last; /* last point in edge segment */
  249. } AF_SegmentRec;
  250. typedef struct AF_EdgeRec_
  251. {
  252. FT_Short fpos; /* original, unscaled position (font units) */
  253. FT_Pos opos; /* original, scaled position */
  254. FT_Pos pos; /* current position */
  255. FT_Byte flags; /* edge flags */
  256. FT_Char dir; /* edge direction */
  257. FT_Fixed scale; /* used to speed up interpolation between edges */
  258. AF_Width blue_edge; /* non-NULL if this is a blue edge */
  259. AF_Edge link; /* link edge */
  260. AF_Edge serif; /* primary edge for serifs */
  261. FT_Short num_linked; /* number of linked edges */
  262. FT_Int score; /* used during stem matching */
  263. AF_Segment first; /* first segment in edge */
  264. AF_Segment last; /* last segment in edge */
  265. } AF_EdgeRec;
  266. typedef struct AF_AxisHintsRec_
  267. {
  268. FT_Int num_segments; /* number of used segments */
  269. FT_Int max_segments; /* number of allocated segments */
  270. AF_Segment segments; /* segments array */
  271. #ifdef AF_SORT_SEGMENTS
  272. FT_Int mid_segments;
  273. #endif
  274. FT_Int num_edges; /* number of used edges */
  275. FT_Int max_edges; /* number of allocated edges */
  276. AF_Edge edges; /* edges array */
  277. AF_Direction major_dir; /* either vertical or horizontal */
  278. } AF_AxisHintsRec, *AF_AxisHints;
  279. typedef struct AF_GlyphHintsRec_
  280. {
  281. FT_Memory memory;
  282. FT_Fixed x_scale;
  283. FT_Pos x_delta;
  284. FT_Fixed y_scale;
  285. FT_Pos y_delta;
  286. FT_Int max_points; /* number of allocated points */
  287. FT_Int num_points; /* number of used points */
  288. AF_Point points; /* points array */
  289. FT_Int max_contours; /* number of allocated contours */
  290. FT_Int num_contours; /* number of used contours */
  291. AF_Point* contours; /* contours array */
  292. AF_AxisHintsRec axis[AF_DIMENSION_MAX];
  293. FT_UInt32 scaler_flags; /* copy of scaler flags */
  294. FT_UInt32 other_flags; /* free for script-specific */
  295. /* implementations */
  296. AF_ScriptMetrics metrics;
  297. FT_Pos xmin_delta; /* used for warping */
  298. FT_Pos xmax_delta;
  299. } AF_GlyphHintsRec;
  300. #define AF_HINTS_TEST_SCALER( h, f ) ( (h)->scaler_flags & (f) )
  301. #define AF_HINTS_TEST_OTHER( h, f ) ( (h)->other_flags & (f) )
  302. #ifdef FT_DEBUG_AUTOFIT
  303. #define AF_HINTS_DO_HORIZONTAL( h ) \
  304. ( !_af_debug_disable_horz_hints && \
  305. !AF_HINTS_TEST_SCALER( h, AF_SCALER_FLAG_NO_HORIZONTAL ) )
  306. #define AF_HINTS_DO_VERTICAL( h ) \
  307. ( !_af_debug_disable_vert_hints && \
  308. !AF_HINTS_TEST_SCALER( h, AF_SCALER_FLAG_NO_VERTICAL ) )
  309. #define AF_HINTS_DO_ADVANCE( h ) \
  310. !AF_HINTS_TEST_SCALER( h, AF_SCALER_FLAG_NO_ADVANCE )
  311. #define AF_HINTS_DO_BLUES( h ) ( !_af_debug_disable_blue_hints )
  312. #else /* !FT_DEBUG_AUTOFIT */
  313. #define AF_HINTS_DO_HORIZONTAL( h ) \
  314. !AF_HINTS_TEST_SCALER( h, AF_SCALER_FLAG_NO_HORIZONTAL )
  315. #define AF_HINTS_DO_VERTICAL( h ) \
  316. !AF_HINTS_TEST_SCALER( h, AF_SCALER_FLAG_NO_VERTICAL )
  317. #define AF_HINTS_DO_ADVANCE( h ) \
  318. !AF_HINTS_TEST_SCALER( h, AF_SCALER_FLAG_NO_ADVANCE )
  319. #define AF_HINTS_DO_BLUES( h ) 1
  320. #endif /* !FT_DEBUG_AUTOFIT */
  321. FT_LOCAL( AF_Direction )
  322. af_direction_compute( FT_Pos dx,
  323. FT_Pos dy );
  324. FT_LOCAL( FT_Error )
  325. af_axis_hints_new_segment( AF_AxisHints axis,
  326. FT_Memory memory,
  327. AF_Segment *asegment );
  328. FT_LOCAL( FT_Error)
  329. af_axis_hints_new_edge( AF_AxisHints axis,
  330. FT_Int fpos,
  331. AF_Direction dir,
  332. FT_Memory memory,
  333. AF_Edge *edge );
  334. FT_LOCAL( void )
  335. af_glyph_hints_init( AF_GlyphHints hints,
  336. FT_Memory memory );
  337. FT_LOCAL( void )
  338. af_glyph_hints_rescale( AF_GlyphHints hints,
  339. AF_ScriptMetrics metrics );
  340. FT_LOCAL( FT_Error )
  341. af_glyph_hints_reload( AF_GlyphHints hints,
  342. FT_Outline* outline );
  343. FT_LOCAL( void )
  344. af_glyph_hints_save( AF_GlyphHints hints,
  345. FT_Outline* outline );
  346. FT_LOCAL( void )
  347. af_glyph_hints_align_edge_points( AF_GlyphHints hints,
  348. AF_Dimension dim );
  349. FT_LOCAL( void )
  350. af_glyph_hints_align_strong_points( AF_GlyphHints hints,
  351. AF_Dimension dim );
  352. FT_LOCAL( void )
  353. af_glyph_hints_align_weak_points( AF_GlyphHints hints,
  354. AF_Dimension dim );
  355. #ifdef AF_CONFIG_OPTION_USE_WARPER
  356. FT_LOCAL( void )
  357. af_glyph_hints_scale_dim( AF_GlyphHints hints,
  358. AF_Dimension dim,
  359. FT_Fixed scale,
  360. FT_Pos delta );
  361. #endif
  362. FT_LOCAL( void )
  363. af_glyph_hints_done( AF_GlyphHints hints );
  364. /* */
  365. #define AF_SEGMENT_LEN( seg ) ( (seg)->max_coord - (seg)->min_coord )
  366. #define AF_SEGMENT_DIST( seg1, seg2 ) ( ( (seg1)->pos > (seg2)->pos ) \
  367. ? (seg1)->pos - (seg2)->pos \
  368. : (seg2)->pos - (seg1)->pos )
  369. FT_END_HEADER
  370. #endif /* __AFHINTS_H__ */
  371. /* END */