Use `Control_Type' to handle different segment directions.
[ttfautohint.git] / lib / tatypes.h
blob5e63c035f698ac004d8f6372c414e635990cc108
1 /* tatypes.h */
3 /*
4 * Copyright (C) 2011-2014 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 `aftypes.h' (2011-Mar-30) from FreeType */
18 /* heavily modified 2011 by Werner Lemberg <wl@gnu.org> */
20 #ifndef __TATYPES_H__
21 #define __TATYPES_H__
23 #include <ft2build.h>
25 #include FT_FREETYPE_H
26 #include FT_OUTLINE_H
28 #include "tablue.h"
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
35 /* enable one of the following three definitions for debugging */
36 /* #define TA_DEBUG */
37 /* #define TA_DEBUG_HORZ */
38 #define TA_DEBUG_VERT
40 #if defined TA_DEBUG_HORZ
41 # define TA_DEBUG_STARTDIM TA_DIMENSION_HORZ
42 # define TA_DEBUG_ENDDIM TA_DIMENSION_HORZ
43 # define TA_DEBUG
44 #elif defined TA_DEBUG_VERT
45 # define TA_DEBUG_STARTDIM TA_DIMENSION_VERT
46 # define TA_DEBUG_ENDDIM TA_DIMENSION_VERT
47 # define TA_DEBUG
48 #elif defined TA_DEBUG
49 # define TA_DEBUG_STARTDIM TA_DIMENSION_VERT
50 # define TA_DEBUG_ENDDIM TA_DIMENSION_HORZ
51 #endif
53 #ifdef TA_DEBUG
55 #define TA_LOG(x) \
56 do \
57 { \
58 if (_ta_debug) \
59 _ta_message x; \
60 } while (0)
62 #define TA_LOG_GLOBAL(x) \
63 do \
64 { \
65 if (_ta_debug_global) \
66 _ta_message x; \
67 } while (0)
69 void
70 _ta_message(const char* format,
71 ...);
73 extern int _ta_debug;
74 extern int _ta_debug_global;
75 extern int _ta_debug_disable_horz_hints;
76 extern int _ta_debug_disable_vert_hints;
77 extern int _ta_debug_disable_blue_hints;
78 extern void* _ta_debug_hints;
80 #else /* !TA_DEBUG */
82 #define TA_LOG(x) \
83 do { } while (0) /* nothing */
85 #define TA_LOG_GLOBAL(x) \
86 do { } while (0) /* nothing */
88 #endif /* !TA_DEBUG */
91 #define TA_ABS(a) ((a) < 0 ? -(a) : (a))
93 /* from file `ftobjs.h' from FreeType */
94 #define TA_PAD_FLOOR(x, n) ((x) & ~((n) - 1))
95 #define TA_PAD_ROUND(x, n) TA_PAD_FLOOR((x) + ((n) / 2), n)
96 #define TA_PAD_CEIL(x, n) TA_PAD_FLOOR((x) + ((n) - 1), n)
98 #define TA_PIX_FLOOR(x) ((x) & ~63)
99 #define TA_PIX_ROUND(x) TA_PIX_FLOOR((x) + 32)
100 #define TA_PIX_CEIL(x) TA_PIX_FLOOR((x) + 63)
103 typedef struct TA_WidthRec_
105 FT_Pos org; /* original position/width in font units */
106 FT_Pos cur; /* current/scaled position/width in device sub-pixels */
107 FT_Pos fit; /* current/fitted position/width in device sub-pixels */
108 } TA_WidthRec, *TA_Width;
111 /* the auto-hinter doesn't need a very high angular accuracy */
112 typedef FT_Int TA_Angle;
114 #define TA_ANGLE_PI 256
115 #define TA_ANGLE_2PI (TA_ANGLE_PI * 2)
116 #define TA_ANGLE_PI2 (TA_ANGLE_PI / 2)
117 #define TA_ANGLE_PI4 (TA_ANGLE_PI / 4)
119 #define TA_ANGLE_DIFF(result, angle1, angle2) \
120 do \
122 TA_Angle _delta = (angle2) - (angle1); \
125 _delta %= TA_ANGLE_2PI; \
126 if (_delta < 0) \
127 _delta += TA_ANGLE_2PI; \
129 if (_delta > TA_ANGLE_PI) \
130 _delta -= TA_ANGLE_2PI; \
132 result = _delta; \
133 } while (0)
136 /* opaque handle to glyph-specific hints -- */
137 /* see `tahints.h' for more details */
138 typedef struct TA_GlyphHintsRec_* TA_GlyphHints;
141 /* a scaler models the target pixel device that will receive */
142 /* the auto-hinted glyph image */
143 #define TA_SCALER_FLAG_NO_HORIZONTAL 0x01 /* disable horizontal hinting */
144 #define TA_SCALER_FLAG_NO_VERTICAL 0x02 /* disable vertical hinting */
145 #define TA_SCALER_FLAG_NO_ADVANCE 0x04 /* disable advance hinting */
147 typedef struct TA_ScalerRec_
149 FT_Face face; /* source font face */
150 FT_Fixed x_scale; /* from font units to 1/64th device pixels */
151 FT_Fixed y_scale; /* from font units to 1/64th device pixels */
152 FT_Pos x_delta; /* in 1/64th device pixels */
153 FT_Pos y_delta; /* in 1/64th device pixels */
154 FT_Render_Mode render_mode; /* monochrome, anti-aliased, LCD, etc.*/
155 FT_UInt32 flags; /* additional control flags, see above */
156 } TA_ScalerRec, *TA_Scaler;
158 #define TA_SCALER_EQUAL_SCALES(a, b) \
159 ((a)->x_scale == (b)->x_scale \
160 && (a)->y_scale == (b)->y_scale \
161 && (a)->x_delta == (b)->x_delta \
162 && (a)->y_delta == (b)->y_delta)
165 typedef struct TA_StyleMetricsRec_* TA_StyleMetrics;
167 /* this function parses an FT_Face to compute global metrics */
168 /* for a specific style */
169 typedef FT_Error
170 (*TA_WritingSystem_InitMetricsFunc)(TA_StyleMetrics metrics,
171 FT_Face face);
172 typedef void
173 (*TA_WritingSystem_ScaleMetricsFunc)(TA_StyleMetrics metrics,
174 TA_Scaler scaler);
175 typedef void
176 (*TA_WritingSystem_DoneMetricsFunc)(TA_StyleMetrics metrics);
177 typedef FT_Error
178 (*TA_WritingSystem_InitHintsFunc)(TA_GlyphHints hints,
179 TA_StyleMetrics metrics);
180 typedef void
181 (*TA_WritingSystem_ApplyHintsFunc)(TA_GlyphHints hints,
182 FT_Outline* outline,
183 TA_StyleMetrics metrics);
187 * For the auto-hinter, a writing system consists of multiple scripts that
188 * can be handled similarly *in a typographical way*; the relationship is
189 * not based on history. For example, both the Greek and the unrelated
190 * Armenian scripts share the same features like ascender, descender,
191 * x-height, etc. Essentially, a writing system is covered by a
192 * submodule of the auto-fitter; it contains
194 * - a specific global analyzer that computes global metrics specific to
195 * the script (based on script-specific characters to identify ascender
196 * height, x-height, etc.),
198 * - a specific glyph analyzer that computes segments and edges for each
199 * glyph covered by the script,
201 * - a specific grid-fitting algorithm that distorts the scaled glyph
202 * outline according to the results of the glyph analyzer.
205 #define __TAWRTSYS_H__ /* don't load header files */
206 #undef WRITING_SYSTEM
207 #define WRITING_SYSTEM(ws, WS) \
208 TA_WRITING_SYSTEM_ ## WS,
210 /* The list of known writing systems. */
211 typedef enum TA_WritingSystem_
213 #include "tawrtsys.h"
215 TA_WRITING_SYSTEM_MAX /* do not remove */
216 } TA_WritingSystem;
218 #undef __TAWRTSYS_H__
221 typedef struct TA_WritingSystemClassRec_
223 TA_WritingSystem writing_system;
225 FT_Offset style_metrics_size;
226 TA_WritingSystem_InitMetricsFunc style_metrics_init;
227 TA_WritingSystem_ScaleMetricsFunc style_metrics_scale;
228 TA_WritingSystem_DoneMetricsFunc style_metrics_done;
230 TA_WritingSystem_InitHintsFunc style_hints_init;
231 TA_WritingSystem_ApplyHintsFunc style_hints_apply;
232 } TA_WritingSystemClassRec;
234 typedef const TA_WritingSystemClassRec* TA_WritingSystemClass;
238 * Each script is associated with a set of Unicode ranges that gets used
239 * to test whether the font face supports the script.
241 * We use four-letter script tags from the OpenType specification,
242 * extended by `NONE', which indicates `no script'.
245 #undef SCRIPT
246 #define SCRIPT(s, S, d, h, sc1, sc2, sc3) \
247 TA_SCRIPT_ ## S,
249 /* The list of known scripts. */
250 typedef enum TA_Script_
252 #include <ttfautohint-scripts.h>
254 TA_SCRIPT_MAX /* do not remove */
255 } TA_Script;
258 typedef struct TA_Script_UniRangeRec_
260 FT_UInt32 first;
261 FT_UInt32 last;
262 } TA_Script_UniRangeRec;
264 #define TA_UNIRANGE_REC(a, b) \
265 { (FT_UInt32)(a), (FT_UInt32)(b) }
267 typedef const TA_Script_UniRangeRec* TA_Script_UniRange;
269 typedef struct TA_ScriptClassRec_
271 TA_Script script;
273 TA_Script_UniRange script_uni_ranges; /* last must be { 0, 0 } */
275 FT_UInt32 standard_char1; /* for default width and height */
276 FT_UInt32 standard_char2; /* ditto */
277 FT_UInt32 standard_char3; /* ditto */
278 } TA_ScriptClassRec;
280 typedef const TA_ScriptClassRec* TA_ScriptClass;
284 * Usually, a font contains more glyphs than can be addressed by its
285 * character map.
287 * In the PostScript font world, encoding vectors specific to a given
288 * task are used to select such glyphs, and these glyphs can be often
289 * recognized by having a suffix in its glyph names. For example, a
290 * superscript glyph `A' might be called `A.sup'. Unfortunately, this
291 * naming scheme is not standardized and thus unusable for us.
293 * In the OpenType world, a better solution was invented, namely
294 * `features', which cleanly separate a character's input encoding from
295 * the corresponding glyph's appearance, and which don't use glyph names
296 * at all. For our purposes, and slightly generalized, an OpenType
297 * feature is a name of a mapping that maps character codes to
298 * non-standard glyph indices (features get used for other things also).
299 * For example, the `sups' feature provides superscript glyphs, thus
300 * mapping character codes like `A' or `B' to superscript glyph
301 * representation forms. How this mapping happens is completely
302 * uninteresting to us.
304 * For the auto-hinter, a `coverage' represents all glyphs of an OpenType
305 * feature collected in a set (as listed below) that can be hinted
306 * together. To continue the above example, superscript glyphs must not
307 * be hinted together with normal glyphs because the blue zones
308 * completely differ.
310 * To compute coverages, we use the HarfBuzz library, which has many
311 * functions exactly for this purpose.
313 * TA_COVERAGE_DEFAULT is special: It should cover everything that isn't
314 * listed separately (including the glyphs addressable by the character
315 * map).
318 #undef COVERAGE
319 #define COVERAGE(name, NAME, description, \
320 tag1, tag2, tag3, tag4) \
321 TA_COVERAGE_ ## NAME,
323 typedef enum TA_Coverage_
325 #include "tacover.h"
327 TA_COVERAGE_DEFAULT
328 } TA_Coverage;
332 * The topmost structure for modelling the auto-hinter glyph input data
333 * is a `style class', grouping everything together.
336 #undef STYLE
337 #define STYLE(s, S, d, ws, sc, ss, c) \
338 TA_STYLE_ ## S,
340 /* The list of known styles. */
341 typedef enum TA_Style_
343 #include "tastyles.h"
345 TA_STYLE_MAX /* do not remove */
346 } TA_Style;
349 typedef struct TA_StyleClassRec_
351 TA_Style style;
353 TA_WritingSystem writing_system;
354 TA_Script script;
355 TA_Blue_Stringset blue_stringset;
356 TA_Coverage coverage;
357 } TA_StyleClassRec;
359 typedef const TA_StyleClassRec* TA_StyleClass;
362 typedef struct TA_FaceGlobalsRec_* TA_FaceGlobals;
364 /* This is the main structure that combines everything. Autofit modules */
365 /* specific to writing systems derive their structures from it, for */
366 /* example `TA_LatinMetrics'. */
368 typedef struct TA_StyleMetricsRec_
370 TA_StyleClass style_class;
371 TA_ScalerRec scaler;
372 FT_Bool digits_have_same_width;
374 TA_FaceGlobals globals; /* to access properties */
375 } TA_StyleMetricsRec;
377 #ifdef __cplusplus
378 } /* extern "C" */
379 #endif
381 #endif /* __TATYPES_H__ */
383 /* end of tatypes.h */