[doc] Implement folded TOC in HTML.
[ttfautohint.git] / lib / ttfautohint.h
1 /* ttfautohint.h */
2
3 /*
4 * Copyright (C) 2011-2014 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 #ifndef __TTFAUTOHINT_H__
17 #define __TTFAUTOHINT_H__
18
19 #include <stdarg.h>
20
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24
25
26 /*
27 * This file gets processed with a simple sed script to extract the
28 * documentation (written in pandoc's markdown format); code between the
29 * `pandoc' markers are retained, everything else is discarded. C comments
30 * are uncommented so that column 4 becomes column 1; empty lines outside of
31 * comments are removed.
32 */
33
34
35 /* pandoc-start */
36
37 /*
38 * The ttfautohint API
39 * ===================
40 *
41 * This section documents the single function of the ttfautohint library,
42 * `TTF_autohint`, together with its callback functions, `TA_Progress_Func`
43 * and `TA_Info_Func`. All information has been directly extracted from the
44 * `ttfautohint.h` header file.
45 *
46 */
47
48
49 /*
50 * Preprocessor Macros and Typedefs
51 * --------------------------------
52 *
53 * Some default values.
54 *
55 * ```C
56 */
57
58 #define TA_HINTING_RANGE_MIN 8
59 #define TA_HINTING_RANGE_MAX 50
60 #define TA_HINTING_LIMIT 200
61 #define TA_INCREASE_X_HEIGHT 14
62
63 /*
64 *```
65 *
66 * An error type.
67 *
68 * ```C
69 */
70
71 typedef int TA_Error;
72
73 /*
74 * ```
75 *
76 */
77
78
79 /*
80 * Callback: `TA_Progress_Func`
81 * ----------------------------
82 *
83 * A callback function to get progress information. *curr_idx* gives the
84 * currently processed glyph index; if it is negative, an error has
85 * occurred. *num_glyphs* holds the total number of glyphs in the font
86 * (this value can't be larger than 65535).
87 *
88 * *curr_sfnt* gives the current subfont within a TrueType Collection (TTC),
89 * and *num_sfnts* the total number of subfonts.
90 *
91 * If the return value is non-zero, `TTF_autohint` aborts with
92 * `TA_Err_Canceled`. Use this for a 'Cancel' button or similar features in
93 * interactive use.
94 *
95 * *progress_data* is a void pointer to user-supplied data.
96 *
97 * ```C
98 */
99
100 typedef int
101 (*TA_Progress_Func)(long curr_idx,
102 long num_glyphs,
103 long curr_sfnt,
104 long num_sfnts,
105 void* progress_data);
106
107 /*
108 * ```
109 *
110 */
111
112
113 /*
114 * Callback: `TA_Error_Func`
115 * -------------------------
116 *
117 * A callback function to get error information.
118 *
119 * *error* is the value `TTF_autohint` returns. See file
120 * `ttfautohint-errors.h` for a list. Error codes not in this list are
121 * directly taken from FreeType; see the FreeType header file `fterrdef.h`
122 * for more.
123 *
124 * *error_string*, if non-NULL, is a pointer to an error message that
125 * represents *error*.
126 *
127 * The next three parameters help identify the origin of text string parsing
128 * errors. *linenum*, if non-zero, contains the line number. *line*, if
129 * non-NULL, is a pointer to the input line that can't be processed.
130 * *errpos*, if non-NULL, holds a pointer to the position in *line* where
131 * the problem occurs.
132 *
133 * *error_data* is a void pointer to user-supplied data.
134 *
135 * ```C
136 */
137
138 typedef void
139 (*TA_Error_Func)(TA_Error error,
140 const char* error_string,
141 unsigned int linenum,
142 const char* line,
143 const char* errpos,
144 void* error_data);
145
146 /*
147 * ```
148 *
149 */
150
151
152 /*
153 * Callback: `TA_Info_Func`
154 * ------------------------
155 *
156 * A callback function to manipulate strings in the `name` table.
157 * *platform_id*, *encoding_id*, *language_id*, and *name_id* are the
158 * identifiers of a `name` table entry pointed to by *str* with a length
159 * pointed to by *str_len* (in bytes; the string has no trailing NULL byte).
160 * Please refer to the [OpenType specification of the `name` table] for a
161 * detailed description of the various parameters, in particular which
162 * encoding is used for a given platform and encoding ID.
163 *
164 * [OpenType specification of the `name` table]: http://www.microsoft.com/typography/otspec/name.htm
165 *
166 * The string *str* is allocated with `malloc`; the application should
167 * reallocate the data if necessary, ensuring that the string length doesn't
168 * exceed 0xFFFF.
169 *
170 * *info_data* is a void pointer to user-supplied data.
171 *
172 * If an error occurs, return a non-zero value and don't modify *str* and
173 * *str_len* (such errors are handled as non-fatal).
174 *
175 * ```C
176 */
177
178 typedef int
179 (*TA_Info_Func)(unsigned short platform_id,
180 unsigned short encoding_id,
181 unsigned short language_id,
182 unsigned short name_id,
183 unsigned short* str_len,
184 unsigned char** str,
185 void* info_data);
186
187 /*
188 * ```
189 *
190 */
191
192 /* pandoc-end */
193
194
195 /*
196 * Error values in addition to the FT_Err_XXX constants from FreeType.
197 *
198 * All error values specific to ttfautohint start with `TA_Err_'.
199 */
200 #include <ttfautohint-errors.h>
201
202
203 /* pandoc-start */
204
205 /*
206 * Function: `TTF_autohint`
207 * ------------------------
208 *
209 *
210 * Read a TrueType font, remove existing bytecode (in the SFNT tables
211 * `prep`, `fpgm`, `cvt `, and `glyf`), and write a new TrueType font with
212 * new bytecode based on the autohinting of the FreeType library.
213 *
214 * It expects a format string *options* and a variable number of arguments,
215 * depending on the fields in *options*. The fields are comma separated;
216 * whitespace within the format string is not significant, a trailing comma
217 * is ignored. Fields are parsed from left to right; if a field occurs
218 * multiple times, the last field's argument wins. The same is true for
219 * fields that are mutually exclusive. Depending on the field, zero or one
220 * argument is expected.
221 *
222 * Note that fields marked as 'not implemented yet' are subject to change.
223 *
224 *
225 * ### I/O
226 *
227 * `in-file`
228 * : A pointer of type `FILE*` to the data stream of the input font,
229 * opened for binary reading. Mutually exclusive with `in-buffer`.
230 *
231 * `in-buffer`
232 * : A pointer of type `const char*` to a buffer that contains the input
233 * font. Needs `in-buffer-len`. Mutually exclusive with `in-file`.
234 *
235 * `in-buffer-len`
236 * : A value of type `size_t`, giving the length of the input buffer.
237 * Needs `in-buffer`.
238 *
239 * `out-file`
240 * : A pointer of type `FILE*` to the data stream of the output font,
241 * opened for binary writing. Mutually exclusive with `out-buffer`.
242 *
243 * `out-buffer`
244 * : A pointer of type `char**` to a buffer that contains the output
245 * font. Needs `out-buffer-len`. Mutually exclusive with `out-file`.
246 * Deallocate the memory with `free`.
247 *
248 * `out-buffer-len`
249 * : A pointer of type `size_t*` to a value giving the length of the
250 * output buffer. Needs `out-buffer`.
251 *
252 * `control-file`
253 * : A pointer of type `FILE*` to the data stream of control instructions.
254 * Mutually exclusive with `control-buffer`.
255 *
256 * An entry in a control instructions file or buffer has one of the
257 * following syntax forms (with brackets showing optional elements):
258 *
259 * > *\[*\ font‑idx\ *\]*\ \ glyph‑id\ \ *`l`\[`eft`\]|`r`\[`ight`\]*\ points\ \ *\[*\ *`(`*\ left‑offset\ *`,`*\ right‑offset\ *`)`*\ *\]*\
260 * > *\[*\ font‑idx\ *\]*\ \ glyph‑id\ \ *`n`\[`odir`\]*\ points\
261 * > *\[*\ font‑idx\ *\]*\ \ glyph‑id\ \ *`t`\[`ouch`\]|`p`\[`oint`\]*\ points\ \ *\[*\ *`x`\[`shift`\]*\ x‑shift\ *\]*\ \ *\[*\ *`y`\[`shift`\]*\ y‑shift\ *\]*\ \ *`@`*\ ppems
262 *
263 * *font‑idx* gives the index of the font in a TrueType Collection. If
264 * missing, it is set to zero. For normal TrueType fonts, only value
265 * zero is valid. If starting with `0x` the number is interpreted as
266 * hexadecimal. If starting with `0` it gets interpreted as an octal
267 * value, and as a decimal value otherwise.
268 *
269 * *glyph‑id* is a glyph's name as listed in the `post` SFNT table or a
270 * glyph index. A glyph name consists of characters from the set
271 * '`A-Za-z0-9._`' only and does not start with a digit or period, with
272 * the exceptions of the names '`.notdef`' and '`.null`'. A glyph index
273 * can be specified in decimal, octal, or hexadecimal format, the latter
274 * two indicated by the prefixes `0` and `0x`, respectively.
275 *
276 * The mutually exclusive parameters `left` and `right` (which can be
277 * abbreviated as '`l`' and '`r`', respectively) indicate that the
278 * following points have left or right 'out' direction, respectively,
279 * overriding ttfautohint's algorithm for setting point directions. The
280 * 'out direction' of a point is the direction of the outline *leaving*
281 * the point (or passing the control point). If the specified direction
282 * is identical to what ttfautohint computes, nothing special happens.
283 * Otherwise, a one-point segment with the specified direction gets
284 * created. By default, its length is zero. Setting *left‑offset* and
285 * *right‑offset*, you can change the segment's horizontal start and end
286 * position relative to the point position. *left‑offset* and
287 * *right‑offset* are integers measured in font units.
288 *
289 * Parameter `nodir` (or '`n`') sets the 'out' direction of the
290 * following points to 'no direction'. If the specified direction is
291 * identical to what ttfautohint computes, nothing special happens.
292 * Otherwise, ttfautohint no longer considers those points as part of
293 * horizontal segments, thus treating them as 'weak' points.
294 *
295 * Modifying or adding segments don't directly modify the outlines; it
296 * only influences the hinting process.
297 *
298 * The mutually exclusive parameters `touch` and `point` (which can be
299 * abbreviated as '`t`' and '`p`', respectively) make ttfautohint apply
300 * delta exceptions for the given points, shifting them by the given
301 * values. Delta exceptions entered with `touch` are applied before the
302 * final `IUP` instructions in a glyph's bytecode, exceptions entered
303 * with `point` after `IUP`. Additionally, the `touch` parameter makes
304 * the bytecode *touch* the affected points; such points are no longer
305 * affected by `IUP` at all. Note that in ClearType mode all deltas
306 * along the x\ axis are discarded, and deltas along the y\ axis are
307 * only executed for touched points. As a consequence, vertical delta
308 * exceptions entered with `point` should not be used in ClearType mode.
309 *
310 * Both *points* and *ppems* are number ranges, similar to the
311 * `x-height-snapping-exceptions` syntax.
312 *
313 * *x‑shift* and *y‑shift* represent real numbers that get rounded to
314 * multiples of 1/8 pixels. The entries for `xshift` ('`x`') and
315 * `yshift` ('`y`') are optional; if missing, the corresponding value is
316 * set to zero. If both values are zero, the delta exception entry is
317 * ignored as a whole.
318 *
319 * Values for *x‑shift* and *y‑shift* must be in the range [−1.0;1.0].
320 * Values for *ppems* must be in the range [6;53]. Values for *points*
321 * are limited by the number of points in the glyph.
322 *
323 * Control instruction entries can be either separated with newlines or
324 * with character '`;`'. Additionally, a line can be continued on the
325 * next line by ending it with backslash character ('`\`'). A backslash
326 * followed by a newline gets treated similar to a whitespace character.
327 *
328 * A comment starts with character '`#`'; the rest of the line is
329 * ignored. An empty line is ignored also.
330 *
331 * Note that only character '`.`' is recognized as a decimal point, and
332 * a thousands separator is not accepted.
333 *
334 * `control-buffer`
335 * : A pointer of type `const char*` to a buffer that contains control
336 * instructions. Needs `control-buffer-len`. Mutually exclusive with
337 * `control-file`.
338 *
339 * `control-buffer-len`
340 * : A value of type `size_t`, giving the length of the control
341 * instructions buffer. Needs `control-buffer`.
342 *
343 *
344 * ### Messages and Callbacks
345 *
346 * `progress-callback`
347 * : A pointer of type [`TA_Progress_Func`](#callback-ta_progress_func),
348 * specifying a callback function for progress reports. This function
349 * gets called after a single glyph has been processed. If this field
350 * is not set or set to NULL, no progress callback function is used.
351 *
352 * `progress-callback-data`
353 * : A pointer of type `void*` to user data that is passed to the
354 * progress callback function.
355 *
356 * `error-string`
357 * : A pointer of type `unsigned char**` to a string (in UTF-8 encoding)
358 * that verbally describes the error code. You must not change the
359 * returned value.
360 *
361 * `error-callback`
362 * : A pointer of type [`TA_Error_Func`](#callback-ta_error_func),
363 * specifying a callback function for error messages. This function
364 * gets called right before `TTF_autohint` exits. If this field is not
365 * set or set to NULL, no error callback function is used.
366 *
367 * Use it as a more sophisticated alternative to `error-string`.
368 *
369 * `error-callback-data`
370 * : A point of type `void*` to user data that is passed to the error
371 * callback function.
372 *
373 * `info-callback`
374 * : A pointer of type [`TA_Info_Func`](#callback-ta_info_func),
375 * specifying a callback function for manipulating the `name` table.
376 * This function gets called for each `name` table entry. If not set or
377 * set to NULL, the table data stays unmodified.
378 *
379 * `info-callback-data`
380 * : A pointer of type `void*` to user data that is passed to the info
381 * callback function.
382 *
383 * `debug`
384 * : If this integer is set to\ 1, lots of debugging information is print
385 * to stderr. The default value is\ 0.
386 *
387 *
388 * ### General Hinting Options
389 *
390 * `hinting-range-min`
391 * : An integer (which must be larger than or equal to\ 2) giving the
392 * lowest PPEM value used for autohinting. If this field is not set, it
393 * defaults to `TA_HINTING_RANGE_MIN`.
394 *
395 * `hinting-range-max`
396 * : An integer (which must be larger than or equal to the value of
397 * `hinting-range-min`) giving the highest PPEM value used for
398 * autohinting. If this field is not set, it defaults to
399 * `TA_HINTING_RANGE_MAX`.
400 *
401 * `hinting-limit`
402 * : An integer (which must be larger than or equal to the value of
403 * `hinting-range-max`) that gives the largest PPEM value at which
404 * hinting is applied. For larger values, hinting is switched off. If
405 * this field is not set, it defaults to `TA_HINTING_LIMIT`. If it is
406 * set to\ 0, no hinting limit is added to the bytecode.
407 *
408 * `hint-composites`
409 * : If this integer is set to\ 1, composite glyphs get separate hints.
410 * This implies adding a special glyph to the font called
411 * ['.ttfautohint'](#the-.ttfautohint-glyph). Setting it to\ 0 (which
412 * is the default), the hints of the composite glyphs' components are
413 * used. Adding hints for composite glyphs increases the size of the
414 * resulting bytecode a lot, but it might deliver better hinting
415 * results. However, this depends on the processed font and must be
416 * checked by inspection.
417 *
418 * `adjust-subglyphs`
419 * : An integer (1\ for 'on' and 0\ for 'off', which is the default) to
420 * specify whether native TrueType hinting of the *input font* shall be
421 * applied to all glyphs before passing them to the (internal)
422 * autohinter. The used resolution is the em-size in font units; for
423 * most fonts this is 2048ppem. Use this only if the old hints move or
424 * scale subglyphs independently of the output resolution, for example
425 * some exotic CJK fonts.
426 *
427 * `pre-hinting` is a deprecated alias name for this option.
428 *
429 *
430 * ### Hinting Algorithms
431 *
432 * `gray-strong-stem-width`
433 * : An integer (1\ for 'on' and 0\ for 'off', which is the default) that
434 * specifies whether horizontal stems should be snapped and positioned
435 * to integer pixel values for normal grayscale rendering.
436 *
437 * `gdi-cleartype-strong-stem-width`
438 * : An integer (1\ for 'on', which is the default, and 0\ for 'off') that
439 * specifies whether horizontal stems should be snapped and positioned
440 * to integer pixel values for GDI ClearType rendering, this is, the
441 * rasterizer version (as returned by the GETINFO bytecode instruction)
442 * is in the range 36\ <= version <\ 38 and ClearType is enabled.
443 *
444 * `dw-cleartype-strong-stem-width`
445 * : An integer (1\ for 'on' and 0\ for 'off', which is the default) that
446 * specifies whether horizontal stems should be snapped and positioned
447 * to integer pixel values for DW ClearType rendering, this is, the
448 * rasterizer version (as returned by the GETINFO bytecode instruction)
449 * is >=\ 38, ClearType is enabled, and subpixel positioning is enabled
450 * also.
451 *
452 * `increase-x-height`
453 * : An integer. For PPEM values in the range 6\ <= PPEM
454 * <= `increase-x-height`, round up the font's x\ height much more often
455 * than normally (to use the terminology of TrueType's 'Super Round'
456 * bytecode instruction, the threshold gets increased from 5/8px to
457 * 13/16px). If it is set to\ 0, this feature is switched off. If this
458 * field is not set, it defaults to `TA_INCREASE_X_HEIGHT`. Use this
459 * flag to improve the legibility of small font sizes if necessary.
460 *
461 * `x-height-snapping-exceptions`
462 * : A pointer of type `const char*` to a null-terminated string that
463 * gives a list of comma separated PPEM values or value ranges at which
464 * no x\ height snapping shall be applied. A value range has the form
465 * *value*~1~`-`*value*~2~, meaning *value*~1~ <= PPEM <= *value*~2~.
466 * *value*~1~ or *value*~2~ (or both) can be missing; a missing value is
467 * replaced by the beginning or end of the whole interval of valid PPEM
468 * values, respectively. Whitespace is not significant; superfluous
469 * commas are ignored, and ranges must be specified in increasing order.
470 * For example, the string `"3, 5-7, 9-"` means the values 3, 5, 6, 7,
471 * 9, 10, 11, 12, etc. Consequently, if the supplied argument is `"-"`,
472 * no x\ height snapping takes place at all. The default is the empty
473 * string (`""`), meaning no snapping exceptions.
474 *
475 * `windows-compatibility`
476 * : If this integer is set to\ 1, two artificial blue zones are used,
477 * positioned at the `usWinAscent` and `usWinDescent` values (from the
478 * font's `OS/2` table). The idea is to help ttfautohint so that the
479 * hinted glyphs stay within this horizontal stripe since Windows clips
480 * everything falling outside. The default is\ 0.
481 *
482 *
483 * ### Scripts
484 *
485 * `default-script`
486 * : A string consisting of four lowercase characters that specifies the
487 * default script for OpenType features. After applying all features
488 * that are handled specially, use this value for the remaining
489 * features. The default value is `"latn"`; if set to `"none"`, no
490 * script is used. Valid values can be found in the header file
491 * `ttfautohint-scripts.h`.
492 *
493 * `fallback-script`
494 * : A string consisting of four lowercase characters that specifies the
495 * default script for glyphs that can't be mapped to a script
496 * automatically. If set to `"none"` (which is the default), no script
497 * is used. Valid values can be found in the header file
498 * `ttfautohint-scripts.h`.
499 *
500 * `symbol`
501 * : Set this integer to\ 1 if you want to process a font that ttfautohint
502 * would refuse otherwise because it can't find a single standard
503 * character for any of the supported scripts. ttfautohint then uses a
504 * default (hinting) value for the standard stem width instead of
505 * deriving it from a script's set of standard characters (for the latin
506 * script, one of them is character 'o'). The default value of this
507 * option is\ 0.
508 *
509 * `fallback-stem-width`
510 * : Set the horizontal stem width (hinting) value for all scripts that
511 * lack proper standard characters. The value is given in font units
512 * and must be a positive integer. If not set, or the value is zero,
513 * ttfautohint uses a hard-coded default (50\ units at 2048 units per
514 * EM, and linearly scaled for other UPEM values, for example 24\ units
515 * at 1000 UPEM).
516 *
517 * For symbol fonts (i.e., option `symbol` is given),
518 * `fallback-stem-width` has an effect only if `fallback-script` is set
519 * also.
520 *
521 *
522 * ### Miscellaneous
523 *
524 * `ignore-restrictions`
525 * : If the font has set bit\ 1 in the 'fsType' field of the `OS/2` table,
526 * the ttfautohint library refuses to process the font since a
527 * permission to do that is required from the font's legal owner. In
528 * case you have such a permission you might set the integer argument to
529 * value\ 1 to make ttfautohint handle the font. The default value
530 * is\ 0.
531 *
532 * `TTFA-info`
533 * : If set to\ 1, ttfautohint creates an SFNT table called `TTFA` and
534 * fills it with information on the parameters used while calling
535 * `TTF_autohint`. The format of the output data resembles the
536 * information at the very beginning of the dump emitted by option
537 * `debug`. The default value is\ 0.
538 *
539 * Main use of this option is for font editing purposes. For example,
540 * after a font editor has added some glyphs, a front-end to
541 * `TTF_autohint` can parse `TTFA` and feed the parameters into another
542 * call of `TTF_autohint`. The new glyphs are then hinted while hints
543 * of the old glyphs stay unchanged.
544 *
545 * If this option is not set, and the font to be processed contains a
546 * `TTFA` table, it gets removed.
547 *
548 * Note that such a `TTFA` table gets ignored by all font rendering
549 * engines. In TrueType Collections, the `TTFA` table is added to the
550 * first subfont.
551 *
552 * `dehint`
553 * : If set to\ 1, remove all hints from the font. All other hinting
554 * options are ignored.
555 *
556 *
557 * ### Remarks
558 *
559 * * Obviously, it is necessary to have an input and an output data
560 * stream. All other options are optional.
561 *
562 * * `hinting-range-min` and `hinting-range-max` specify the range for
563 * which the autohinter generates optimized hinting code. If a PPEM
564 * value is smaller than the value of `hinting-range-min`, hinting still
565 * takes place but the configuration created for `hinting-range-min` is
566 * used. The analogous action is taken for `hinting-range-max`, only
567 * limited by the value given with `hinting-limit`. The font's `gasp`
568 * table is set up to always use grayscale rendering with grid-fitting
569 * for standard hinting, and symmetric grid-fitting and symmetric
570 * smoothing for horizontal subpixel hinting (ClearType).
571 *
572 * * ttfautohint can process its own output a second time only if option
573 * `hint-composites` is not set (or if the font doesn't contain
574 * composite glyphs at all). This limitation might change in the
575 * future.
576 *
577 * ```C
578 */
579
580 TA_Error
581 TTF_autohint(const char* options,
582 ...);
583
584 /*
585 * ```
586 *
587 */
588
589 /* pandoc-end */
590
591 #ifdef __cplusplus
592 } /* extern "C" */
593 #endif
594
595 #endif /* __TTFAUTOHINT_H__ */
596
597 /* end of ttfautohint.h */