1 /* tatypes.h */
2 
3 /*
4  * Copyright (C) 2011-2021 by Werner Lemberg.
5  *
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.
10  *
11  * The file `COPYING' mentioned in the previous paragraph is distributed
12  * with the ttfautohint library.
13  */
14 
15 
16 /* originally file `aftypes.h' (2011-Mar-30) from FreeType */
17 
18 /* heavily modified 2011 by Werner Lemberg <wl@gnu.org> */
19 
20 #ifndef TATYPES_H_
21 #define TATYPES_H_
22 
23 #include <ft2build.h>
24 
25 #include FT_FREETYPE_H
26 #include FT_OUTLINE_H
27 
28 #include "tablue.h"
29 
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 
34 
35 /* enable one of the following three definitions for debugging */
36 /* #define TA_DEBUG */
37 /* #define TA_DEBUG_HORZ */
38 #define TA_DEBUG_VERT
39 
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
52 
53 #ifdef TA_DEBUG
54 
55 #define TA_LOG(x) \
56   do \
57   { \
58     if (_ta_debug) \
59       _ta_message x; \
60   } while (0)
61 
62 #define TA_LOG_GLOBAL(x) \
63   do \
64   { \
65     if (_ta_debug_global) \
66       _ta_message x; \
67   } while (0)
68 
69 void
70 _ta_message(const char* format,
71             ...);
72 
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;
79 
80 #else /* !TA_DEBUG */
81 
82 #define TA_LOG(x) \
83   do { } while (0) /* nothing */
84 
85 #define TA_LOG_GLOBAL(x) \
86   do { } while (0) /* nothing */
87 
88 #endif /* !TA_DEBUG */
89 
90 
91 #define TA_MIN(a, b) ((a) < (b) ? (a) : (b))
92 #define TA_MAX(a, b) ((a) > (b) ? (a) : (b))
93 
94 #define TA_ABS(a) ((a) < 0 ? -(a) : (a))
95 
96 /* from file `ftconfig.h' (2015-03-02) from FreeType */
97 /* typeof condition taken from gnulib's `intprops.h' header file */
98 #if (__GNUC__ >= 2 \
99      ||  defined(__IBM__TYPEOF__) \
100      || (__SUNPRO_C >= 0x5110 && !__STDC__))
101 #define TYPEOF(type) (__typeof__ (type))
102 #else
103 #define TYPEOF(type) /* empty */
104 #endif
105 
106 /* from file `ftobjs.h' from FreeType */
107 /* we use the TYPEOF macro to suppress signedness compilation warnings */
108 #define TA_PAD_FLOOR(x, n) ((x) & ~TYPEOF(x)((n) - 1))
109 #define TA_PAD_ROUND(x, n) TA_PAD_FLOOR((x) + ((n) / 2), n)
110 #define TA_PAD_CEIL(x, n) TA_PAD_FLOOR((x) + ((n) - 1), n)
111 
112 #define TA_PIX_FLOOR(x) ((x) & ~TYPEOF(x)63)
113 #define TA_PIX_ROUND(x) TA_PIX_FLOOR((x) + 32)
114 #define TA_PIX_CEIL(x) TA_PIX_FLOOR((x) + 63)
115 
116 
117 typedef struct TA_WidthRec_
118 {
119   FT_Pos org; /* original position/width in font units */
120   FT_Pos cur; /* current/scaled position/width in device subpixels */
121   FT_Pos fit; /* current/fitted position/width in device subpixels */
122 } TA_WidthRec, *TA_Width;
123 
124 
125 /* the auto-hinter doesn't need a very high angular accuracy */
126 typedef FT_Int TA_Angle;
127 
128 #define TA_ANGLE_PI 256
129 #define TA_ANGLE_2PI (TA_ANGLE_PI * 2)
130 #define TA_ANGLE_PI2 (TA_ANGLE_PI / 2)
131 #define TA_ANGLE_PI4 (TA_ANGLE_PI / 4)
132 
133 #define TA_ANGLE_DIFF(result, angle1, angle2) \
134           do \
135           { \
136             TA_Angle _delta = (angle2) - (angle1); \
137 \
138 \
139             while (_delta <= -TA_ANGLE_PI) \
140               _delta += TA_ANGLE_2PI; \
141 \
142             if (_delta > TA_ANGLE_PI) \
143               _delta -= TA_ANGLE_2PI; \
144 \
145             result = _delta; \
146           } while (0)
147 
148 
149 /* opaque handle to glyph-specific hints -- */
150 /* see `tahints.h' for more details */
151 typedef struct TA_GlyphHintsRec_* TA_GlyphHints;
152 
153 
154 /* a scaler models the target pixel device that will receive */
155 /* the auto-hinted glyph image */
156 #define TA_SCALER_FLAG_NO_HORIZONTAL 0x01U /* disable horizontal hinting */
157 #define TA_SCALER_FLAG_NO_VERTICAL 0x02U /* disable vertical hinting */
158 #define TA_SCALER_FLAG_NO_ADVANCE 0x04U /* disable advance hinting */
159 #define TA_SCALER_FLAG_NO_WARPER 0x08U /* disable warper */
160 
161 
162 typedef struct TA_ScalerRec_
163 {
164   FT_Face face; /* source font face */
165   FT_Fixed x_scale; /* from font units to 1/64th device pixels */
166   FT_Fixed y_scale; /* from font units to 1/64th device pixels */
167   FT_Pos x_delta; /* in 1/64th device pixels */
168   FT_Pos y_delta; /* in 1/64th device pixels */
169   FT_Render_Mode render_mode; /* monochrome, anti-aliased, LCD, etc.*/
170   FT_UInt32 flags; /* additional control flags, see above */
171 } TA_ScalerRec, *TA_Scaler;
172 
173 #define TA_SCALER_EQUAL_SCALES(a, b) \
174           ((a)->x_scale == (b)->x_scale \
175            && (a)->y_scale == (b)->y_scale \
176            && (a)->x_delta == (b)->x_delta \
177            && (a)->y_delta == (b)->y_delta)
178 
179 
180 typedef struct TA_StyleMetricsRec_* TA_StyleMetrics;
181 
182 /* this function parses an FT_Face to compute global metrics */
183 /* for a specific style, optionally using another FT_Face to */
184 /* derive blue zones                                         */
185 typedef FT_Error
186 (*TA_WritingSystem_InitMetricsFunc)(TA_StyleMetrics metrics,
187                                     FT_Face face,
188                                     FT_Face reference);
189 typedef void
190 (*TA_WritingSystem_ScaleMetricsFunc)(TA_StyleMetrics metrics,
191                                      TA_Scaler scaler);
192 typedef void
193 (*TA_WritingSystem_DoneMetricsFunc)(TA_StyleMetrics metrics);
194 typedef FT_Error
195 (*TA_WritingSystem_InitHintsFunc)(TA_GlyphHints hints,
196                                   TA_StyleMetrics metrics);
197 typedef FT_Error
198 (*TA_WritingSystem_ApplyHintsFunc)(FT_UInt glyph_index,
199                                    TA_GlyphHints hints,
200                                    FT_Outline* outline,
201                                    TA_StyleMetrics metrics);
202 
203 
204 /*
205  * For the auto-hinter, a writing system consists of multiple scripts that
206  * can be handled similarly *in a typographical way*; the relationship is
207  * not based on history.  For example, both the Greek and the unrelated
208  * Armenian scripts share the same features like ascender, descender,
209  * x-height, etc.  Essentially, a writing system is covered by a
210  * submodule of the auto-fitter; it contains
211  *
212  * - a specific global analyzer that computes global metrics specific to
213  *   the script (based on script-specific characters to identify ascender
214  *   height, x-height, etc.),
215  *
216  * - a specific glyph analyzer that computes segments and edges for each
217  *   glyph covered by the script,
218  *
219  * - a specific grid-fitting algorithm that distorts the scaled glyph
220  *   outline according to the results of the glyph analyzer.
221  */
222 
223 #define TAWRTSYS_H_ /* don't load header files */
224 #undef WRITING_SYSTEM
225 #define WRITING_SYSTEM(ws, WS) \
226           TA_WRITING_SYSTEM_ ## WS,
227 
228 /* The list of known writing systems. */
229 typedef enum TA_WritingSystem_
230 {
231 #include "tawrtsys.h"
232 
233   TA_WRITING_SYSTEM_MAX /* do not remove */
234 } TA_WritingSystem;
235 
236 #undef TAWRTSYS_H_
237 
238 
239 typedef struct TA_WritingSystemClassRec_
240 {
241   TA_WritingSystem writing_system;
242 
243   FT_Offset style_metrics_size;
244   TA_WritingSystem_InitMetricsFunc style_metrics_init;
245   TA_WritingSystem_ScaleMetricsFunc style_metrics_scale;
246   TA_WritingSystem_DoneMetricsFunc style_metrics_done;
247 
248   TA_WritingSystem_InitHintsFunc style_hints_init;
249   TA_WritingSystem_ApplyHintsFunc style_hints_apply;
250 } TA_WritingSystemClassRec;
251 
252 typedef const TA_WritingSystemClassRec* TA_WritingSystemClass;
253 
254 
255 /*
256  * Each script is associated with two sets of Unicode ranges to test
257  * whether the font face supports the script, and which base characters
258  * the script contains.
259  *
260  * We use four-letter script tags from the OpenType specification,
261  * extended by `NONE', which indicates `no script'.
262  */
263 
264 #undef SCRIPT
265 #define SCRIPT(s, S, d, h, H, ss) \
266           TA_SCRIPT_ ## S,
267 
268 /* The list of known scripts. */
269 typedef enum TA_Script_
270 {
271 #include <ttfautohint-scripts.h>
272 
273   TA_SCRIPT_MAX /* do not remove */
274 } TA_Script;
275 
276 
277 typedef struct TA_Script_UniRangeRec_
278 {
279   FT_UInt32 first;
280   FT_UInt32 last;
281 } TA_Script_UniRangeRec;
282 
283 #define TA_UNIRANGE_REC(a, b) \
284           { (FT_UInt32)(a), (FT_UInt32)(b) }
285 
286 #define TA_HINTING_BOTTOM_TO_TOP 0
287 #define TA_HINTING_TOP_TO_BOTTOM 1
288 
289 typedef const TA_Script_UniRangeRec* TA_Script_UniRange;
290 
291 typedef struct TA_ScriptClassRec_
292 {
293   TA_Script script;
294 
295   /* last element in the ranges must be { 0, 0 } */
296   TA_Script_UniRange script_uni_ranges;
297   TA_Script_UniRange script_uni_nonbase_ranges;
298 
299   FT_Bool top_to_bottom_hinting;
300 
301   const char* standard_charstring; /* for default width and height */
302 } TA_ScriptClassRec;
303 
304 typedef const TA_ScriptClassRec* TA_ScriptClass;
305 
306 
307 /*
308  * Usually, a font contains more glyphs than can be addressed by its
309  * character map.
310  *
311  * In the PostScript font world, encoding vectors specific to a given
312  * task are used to select such glyphs, and these glyphs can be often
313  * recognized by having a suffix in its glyph names.  For example, a
314  * superscript glyph `A' might be called `A.sup'.  Unfortunately, this
315  * naming scheme is not standardized and thus unusable for us.
316  *
317  * In the OpenType world, a better solution was invented, namely
318  * `features', which cleanly separate a character's input encoding from
319  * the corresponding glyph's appearance, and which don't use glyph names
320  * at all.  For our purposes, and slightly generalized, an OpenType
321  * feature is a name of a mapping that maps character codes to
322  * non-standard glyph indices (features get used for other things also).
323  * For example, the `sups' feature provides superscript glyphs, thus
324  * mapping character codes like `A' or `B' to superscript glyph
325  * representation forms.  How this mapping happens is completely
326  * uninteresting to us.
327  *
328  * For the auto-hinter, a `coverage' represents all glyphs of an OpenType
329  * feature collected in a set (as listed below) that can be hinted
330  * together.  To continue the above example, superscript glyphs must not
331  * be hinted together with normal glyphs because the blue zones
332  * completely differ.
333  *
334  * To compute coverages, we use the HarfBuzz library, which has many
335  * functions exactly for this purpose.
336  *
337  * TA_COVERAGE_DEFAULT is special: It should cover everything that isn't
338  * listed separately (including the glyphs addressable by the character
339  * map).
340  */
341 
342 #undef COVERAGE
343 #define COVERAGE(name, NAME, description, \
344                  tag, tag1, tag2, tag3, tag4) \
345           TA_COVERAGE_ ## NAME,
346 
347 typedef enum TA_Coverage_
348 {
349 #include "ttfautohint-coverages.h"
350 
351   TA_COVERAGE_DEFAULT
352 } TA_Coverage;
353 
354 
355 /*
356  * The topmost structure for modelling the auto-hinter glyph input data
357  * is a `style class', grouping everything together.
358  */
359 
360 #undef STYLE
361 #define STYLE(s, S, d, ws, sc, ss, c) \
362           TA_STYLE_ ## S,
363 
364 /* The list of known styles. */
365 typedef enum TA_Style_
366 {
367 #include "tastyles.h"
368 
369   TA_STYLE_MAX /* do not remove */
370 } TA_Style;
371 
372 
373 typedef struct TA_StyleClassRec_
374 {
375   TA_Style style;
376 
377   TA_WritingSystem writing_system;
378   TA_Script script;
379   TA_Blue_Stringset blue_stringset;
380   TA_Coverage coverage;
381 } TA_StyleClassRec;
382 
383 typedef const TA_StyleClassRec* TA_StyleClass;
384 
385 
386 typedef struct TA_FaceGlobalsRec_* TA_FaceGlobals;
387 
388 /* This is the main structure that combines everything.  Autofit modules */
389 /* specific to writing systems derive their structures from it, for      */
390 /* example `TA_LatinMetrics'.                                            */
391 
392 typedef struct TA_StyleMetricsRec_
393 {
394   TA_StyleClass style_class;
395   TA_ScalerRec scaler;
396   FT_Bool digits_have_same_width;
397 
398   TA_FaceGlobals globals; /* to access properties */
399 } TA_StyleMetricsRec;
400 
401 #ifdef __cplusplus
402 } /* extern "C" */
403 #endif
404 
405 #endif /* TATYPES_H_ */
406 
407 /* end of tatypes.h */
408