Use `Control_Type' to handle different segment directions.
[ttfautohint.git] / lib / tacontrol.h
blob06dc92e7657b44ba23d606a9c73fbb99f7049743
1 /* tacontrol.h */
3 /*
4 * Copyright (C) 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 #ifndef __TACONTROL_H__
17 #define __TACONTROL_H__
19 #ifdef __cplusplus
20 extern "C" {
21 #endif
23 #include <setjmp.h> /* for flex error handling */
26 /* see the section `Managing exceptions' in chapter 6 */
27 /* (`The TrueType Instruction Set') of the OpenType reference */
28 /* how `delta_shift' works */
30 #define CONTROL_DELTA_SHIFT 3 /* 1/8px */
31 #define CONTROL_DELTA_FACTOR (1 << CONTROL_DELTA_SHIFT)
33 #define CONTROL_DELTA_SHIFT_MAX ((1.0 / CONTROL_DELTA_FACTOR) * 8)
34 #define CONTROL_DELTA_SHIFT_MIN -CONTROL_DELTA_SHIFT_MAX
38 * For the generated TrueType bytecode, we use
40 * delta_base = 6 ,
42 * which gives us the following ppem ranges for the three delta
43 * instructions:
45 * DELTAP1 6-21ppem
46 * DELTAP2 22-37ppem
47 * DELTAP3 38-53ppem .
50 #define CONTROL_DELTA_PPEM_MIN 6
51 #define CONTROL_DELTA_PPEM_MAX 53
55 * The control type.
58 typedef enum Control_Type_
60 Control_Delta_before_IUP,
61 Control_Delta_after_IUP,
62 Control_Segment_Left,
63 Control_Segment_Right,
64 Control_Segment_None
65 } Control_Type;
69 * A structure to hold control instructions for a glyph. A linked list of it
70 * gets allocated by a successful call to `TA_control_parse_buffer'. Use
71 * `TA_control_free' to deallocate the list.
73 * `x_shift' and `y_shift' are always in the range [-8;8].
75 * The `Control' typedef is in `ta.h'.
78 struct Control_
80 Control_Type type;
82 long font_idx;
83 long glyph_idx;
84 number_range* points;
85 char x_shift;
86 char y_shift;
87 number_range* ppems;
89 struct Control_* next;
94 * A structure to hold a single control instruction.
97 typedef struct Ctrl_
99 Control_Type type;
101 long font_idx;
102 long glyph_idx;
103 int ppem;
104 int point_idx;
106 char x_shift;
107 char y_shift;
108 } Ctrl;
112 * This structure is used for communication with `TA_control_parse'.
115 typedef struct Control_Context_
117 /* The error code returned by the parser or lexer. */
118 TA_Error error;
120 /* If no error, this holds the parsing result. */
121 Control* result;
124 * The parser's or lexer's error message in case of error; might be the
125 * empty string.
127 char errmsg[256];
130 * In case of error, `errline_num' gives the line number of the offending
131 * line in `font->control_buf', starting with value 1; `errline_pos_left'
132 * and `errline_pos_right' hold the left and right position of the
133 * offending token in this line, also starting with value 1. For
134 * allocation errors or internal parser or lexer errors those values are
135 * meaningless, though.
137 int errline_num;
138 int errline_pos_left;
139 int errline_pos_right;
142 * The current font index, useful for `TA_Err_Control_Invalid_Font_Index'.
144 long font_idx;
147 * The current glyph index, useful for
148 * `TA_Err_Control_Invalid_Glyph_Index'.
150 long glyph_idx;
153 * If the returned error is `TA_Err_Control_Invalid_Range', these two
154 * values set up the allowed range.
156 long number_set_min;
157 long number_set_max;
159 /* private flex data */
160 void* scanner;
161 int eof;
162 jmp_buf jump_buffer;
164 /* private bison data */
165 FONT* font;
166 } Control_Context;
170 * Create and initialize a `Control' object. In case of an allocation error,
171 * the return value is NULL. `point_set' and `ppem_set' are expected to be
172 * in reverse list order; `TA_control_new' then reverts them to normal order.
175 Control*
176 TA_control_new(Control_Type type,
177 long font_idx,
178 long glyph_idx,
179 number_range* point_set,
180 double x_shift,
181 double y_shift,
182 number_range* ppem_set);
186 * Prepend `element' to `list' of `Control' objects. If `element' is NULL,
187 * return `list.
190 Control*
191 TA_control_prepend(Control* list,
192 Control* element);
196 * Reverse a list of `Control' objects.
199 Control*
200 TA_control_reverse(Control* list);
204 * Initialize the scanner data within a `Control_Context' object.
205 * `font->control_buf' is the control instructions buffer to be parsed,
206 * `font->control_len' its length.
208 * This function is defined in `tacontrol.flex'.
211 void
212 TA_control_scanner_init(Control_Context* context,
213 FONT* font);
217 * Free the scanner data within a `Control_Context' object.
219 * This function is defined in `tacontrol.flex'.
222 void
223 TA_control_scanner_done(Control_Context* context);
227 * Parse buffer with ttfautohint control instructions, stored in
228 * `font->control_buf' with length `font->control_len'.
230 * The format of entries in such a control instructions buffer is given in
231 * `ttfautohint.h' (option `control-file'); the following describes more
232 * technical details, using the constants defined above.
234 * x shift and y shift values represent floating point numbers that get
235 * rounded to multiples of 1/(2^CONTROL_DELTA_SHIFT) pixels.
237 * Values for x and y shifts must be in the range
238 * [CONTROL_DELTA_SHIFT_MIN;CONTROL_DELTA_SHIFT_MAX]. Values for ppems must
239 * be in the range [CONTROL_DELTA_PPEM_MIN;CONTROL_DELTA_PPEM_MAX].
241 * The returned error codes are 0 (TA_Err_Ok) or in the range 0x200-0x2FF;
242 * see `ttfautohint-errors.h' for all possible values.
244 * `TA_control_parse_buffer' stores the parsed result in `font->control', to
245 * be freed with `TA_control_free' after use. If there is no control
246 * instructions data (for example, an empty string or whitespace only)
247 * nothing gets allocated, and `font->control' is set to NULL.
249 * In case of error, `error_string_p' holds an error message, `errlinenum_p'
250 * gives the line number in the control instructions buffer where the error
251 * occurred, `errline_p' the corresponding line, and `errpos_p' the position
252 * in this line. After use, `error_string_p' and `errline_p' must be
253 * deallocated by the user. Note that `errline_p' and `errpos_p' can be
254 * NULL even in case of an error. If there is no error, those four values
255 * are undefined.
258 TA_Error
259 TA_control_parse_buffer(FONT* font,
260 char** error_string_p,
261 unsigned int* errlinenum_p,
262 char** errline_p,
263 char** errpos_p);
267 * Free the allocated data in `control'.
270 void
271 TA_control_free(Control* control);
275 * Return a string representation of `font->control'. After use, the string
276 * should be deallocated with a call to `free'. In case of an allocation
277 * error, the return value is NULL.
280 char*
281 TA_control_show(FONT* font);
285 * Build a tree providing sequential access to the control instructions data
286 * in `font->control'. This also sets `font->control_data_cur' to the first
287 * element (or NULL if there isn't one).
290 TA_Error
291 TA_control_build_tree(FONT* font);
295 * Free the control instructions data tree.
298 void
299 TA_control_free_tree(FONT* font);
303 * Get next control instruction and store it in `font->control_data_cur'.
306 void
307 TA_control_get_next(FONT* font);
311 * Access control instruction. Return NULL if there is no more data.
314 const Ctrl*
315 TA_control_get_ctrl(FONT* font);
319 * Collect forced point directions and store them in
320 * `font->control_point_dirs'.
323 TA_Error
324 TA_control_point_dir_collect(FONT* font,
325 long font_idx,
326 long glyph_idx);
329 * Access next forced point direction. Returns 1 on success or 0 if no more
330 * data. In the latter case, it resets the internal iterator so that
331 * calling this function another time starts at the beginning again.
335 TA_control_point_dir_get_next(FONT* font,
336 int* point_idx,
337 TA_Direction* dir);
340 #ifdef __cplusplus
341 } /* extern "C" */
342 #endif
344 #endif /* __TACONTROL_H__ */
346 /* end of control.h */