Fix OTS warning about `maxp.maxSizeOfInstructions`.
[ttfautohint.git] / lib / ta.h
blob7d1141ff797cdfab53a7d8fc15066b3ae67e3494
1 /* ta.h */
3 /*
4 * Copyright (C) 2011-2022 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 TA_H_
17 #define TA_H_
19 #include <config.h>
21 #include <ft2build.h>
22 #include FT_FREETYPE_H
23 #include FT_TRUETYPE_TABLES_H
24 #include FT_TRUETYPE_TAGS_H
26 #include <ttfautohint.h>
27 #include <sds.h>
28 #include <numberset.h>
30 #include "taloader.h"
31 #include "taglobal.h"
32 #include "tadummy.h"
33 #include "talatin.h"
36 #define TTFAUTOHINT_GLYPH ".ttfautohint"
37 #define TTFAUTOHINT_GLYPH_FIRST_BYTE "\x0C" /* first byte is string length */
38 #define TTFAUTOHINT_GLYPH_LEN 13
41 /* these macros convert 16bit and 32bit numbers into single bytes */
42 /* using the byte order needed within SFNT files */
44 #define HIGH(x) (FT_Byte)(((x) & 0xFF00) >> 8)
45 #define LOW(x) ((x) & 0x00FF)
47 #define BYTE1(x) (FT_Byte)(((FT_ULong)(x) & 0xFF000000UL) >> 24);
48 #define BYTE2(x) (FT_Byte)(((FT_ULong)(x) & 0x00FF0000UL) >> 16);
49 #define BYTE3(x) (FT_Byte)(((FT_ULong)(x) & 0x0000FF00UL) >> 8);
50 #define BYTE4(x) ((FT_ULong)(x) & 0x000000FFUL);
52 /* utility macros to get data from a pointer (with auto-increment) */
54 #define NEXT_USHORT(buffer) \
55 ((FT_UShort)(buffer += 2, (FT_UShort)buffer[-2] << 8 \
56 | (FT_UShort)buffer[-1]))
57 #define NEXT_ULONG(buffer) \
58 ((FT_ULong)(buffer += 4, (FT_ULong)buffer[-4] << 24 \
59 | (FT_ULong)buffer[-3] << 16 \
60 | (FT_ULong)buffer[-2] << 8 \
61 | (FT_ULong)buffer[-1]))
64 /* an SFNT tag for our information table */
65 #define TTAG_TTFA FT_MAKE_TAG('T', 'T', 'F', 'A')
67 /* the length of a dummy `DSIG' table */
68 #define DSIG_LEN 8
70 /* the length of our `gasp' table */
71 #define GASP_LEN 8
73 /* an empty slot in the table info array */
74 #define MISSING ~0U
76 /* the offset to the loca table format in the `head' table */
77 #define LOCA_FORMAT_OFFSET 51
79 /* various offsets within the `maxp' table */
80 #define MAXP_NUM_GLYPHS 4
81 #define MAXP_MAX_COMPOSITE_POINTS 10
82 #define MAXP_MAX_COMPOSITE_CONTOURS 12
83 #define MAXP_MAX_ZONES_OFFSET 14
84 #define MAXP_MAX_TWILIGHT_POINTS_OFFSET 16
85 #define MAXP_MAX_STORAGE_OFFSET 18
86 #define MAXP_MAX_FUNCTION_DEFS_OFFSET 20
87 #define MAXP_MAX_INSTRUCTION_DEFS_OFFSET 22
88 #define MAXP_MAX_STACK_ELEMENTS_OFFSET 24
89 #define MAXP_MAX_INSTRUCTIONS_OFFSET 26
90 #define MAXP_MAX_COMPONENTS_OFFSET 28
92 #define MAXP_LEN 32
94 /* the offset of the type flags field in the `OS/2' table */
95 #define OS2_FSTYPE_OFFSET 8
98 /* flags in composite glyph records */
99 #define ARGS_ARE_WORDS 0x0001
100 #define ARGS_ARE_XY_VALUES 0x0002
101 #define WE_HAVE_A_SCALE 0x0008
102 #define MORE_COMPONENTS 0x0020
103 #define WE_HAVE_AN_XY_SCALE 0x0040
104 #define WE_HAVE_A_2X2 0x0080
105 #define WE_HAVE_INSTR 0x0100
107 /* flags in simple glyph records */
108 #define ON_CURVE 0x01
109 #define X_SHORT_VECTOR 0x02
110 #define Y_SHORT_VECTOR 0x04
111 #define REPEAT 0x08
112 #define SAME_X 0x10
113 #define SAME_Y 0x20
116 /* a single glyph */
117 typedef struct GLYPH_
119 FT_ULong len1; /* number of bytes before instruction related data */
120 FT_ULong len2; /* number of bytes after instruction related data; */
121 /* if zero, this indicates a composite glyph */
122 FT_Byte* buf; /* extracted glyph data (without instruction related data) */
123 FT_ULong flags_offset; /* offset to last flag in a composite glyph */
125 FT_Byte ins_extra_len; /* number of extra instructions */
126 FT_Byte* ins_extra_buf; /* extra instructions data */
127 FT_ULong ins_len; /* number of new instructions */
128 FT_Byte* ins_buf; /* new instruction data */
130 FT_Short num_contours; /* >= 0 for simple glyphs */
131 FT_UShort num_points; /* number of points in a simple glyph */
133 FT_UShort num_components;
134 FT_UShort* components; /* the subglyph indices of a composite glyph */
136 FT_UShort num_pointsums;
137 FT_UShort* pointsums; /* the pointsums of all composite elements */
138 /* (after walking recursively over all subglyphs) */
140 FT_UShort num_composite_contours; /* after recursion */
141 } GLYPH;
143 /* a representation of the data in the `glyf' table */
144 typedef struct glyf_Data_
146 FT_UShort num_glyphs;
147 GLYPH* glyphs;
149 /* this field gives the `master' globals for a `glyf' table; */
150 /* see function `TA_sfnt_handle_coverage' */
151 TA_FaceGlobals master_globals;
152 /* for coverage bookkeeping */
153 FT_Bool adjusted;
155 /* if a `glyf' table gets used in more than one subfont, */
156 /* so do `cvt', `fpgm', and `prep' tables: */
157 /* these four tables are always handled in parallel */
158 FT_ULong cvt_idx;
159 FT_ULong fpgm_idx;
160 FT_ULong prep_idx;
162 /* styles present in a font get a running number; */
163 /* unavailable styles get value 0xFFFF */
164 FT_UInt style_ids[TA_STYLE_MAX];
165 FT_UInt num_used_styles;
167 /* we have separate CVT data for each style */
168 FT_UInt cvt_offsets[TA_STYLE_MAX];
169 FT_UInt cvt_horz_width_sizes[TA_STYLE_MAX];
170 FT_UInt cvt_vert_width_sizes[TA_STYLE_MAX];
171 FT_UInt cvt_blue_zone_sizes[TA_STYLE_MAX];
172 FT_UInt cvt_blue_adjustment_offsets[TA_STYLE_MAX];
173 } glyf_Data;
175 /* an SFNT table */
176 typedef struct SFNT_Table_
178 FT_ULong tag;
179 FT_ULong len;
180 FT_Byte* buf; /* the table data */
181 FT_ULong offset; /* from beginning of file */
182 FT_ULong checksum;
183 void* data; /* used e.g. for `glyf' table data */
184 FT_Bool processed;
185 } SFNT_Table;
187 /* we use indices into the SFNT table array to */
188 /* represent table info records of the TTF header */
189 typedef FT_ULong SFNT_Table_Info;
191 /* this structure is used to model a TTF or a subfont within a TTC */
192 typedef struct SFNT_
194 FT_Face face;
196 SFNT_Table_Info* table_infos;
197 FT_ULong num_table_infos;
199 /* various SFNT table indices */
200 FT_ULong glyf_idx;
201 FT_ULong loca_idx;
202 FT_ULong head_idx;
203 FT_ULong hmtx_idx;
204 FT_ULong maxp_idx;
205 FT_ULong name_idx;
206 FT_ULong post_idx;
207 FT_ULong OS2_idx;
208 FT_ULong GPOS_idx;
210 /* values necessary to update the `maxp' table */
211 FT_UShort max_composite_points;
212 FT_UShort max_composite_contours;
213 FT_UShort max_storage;
214 FT_UShort max_stack_elements;
215 FT_UShort max_twilight_points;
216 FT_UShort max_instructions;
217 FT_UShort max_components;
218 } SFNT;
220 typedef struct Control_ Control;
222 /* our font object; the `FONT' typedef is in `taloader.h' */
223 struct FONT_
225 FT_Library lib;
227 FT_Byte* in_buf;
228 size_t in_len;
230 FT_Byte* out_buf;
231 size_t out_len;
233 char* control_buf;
234 size_t control_len;
236 FT_Byte* reference_buf;
237 size_t reference_len;
239 FT_Face reference;
240 FT_Long reference_index;
241 const char* reference_name;
243 SFNT* sfnts;
244 FT_Long num_sfnts;
246 SFNT_Table* tables;
247 FT_ULong num_tables;
249 FT_Bool have_DSIG;
251 /* we have a single `gasp' table for all subfonts */
252 FT_ULong gasp_idx;
254 /* the control instructions */
255 Control* control;
257 /* two generic pointers into the control instructions tree */
258 void* control_data_head;
259 void* control_data_cur;
261 /* two fields for handling one-point segment directions */
262 /* of the current glyph */
263 void* control_segment_dirs_head;
264 void* control_segment_dirs_cur;
266 TA_LoaderRec loader[1]; /* the interface to the autohinter */
268 /* configuration options */
269 TA_Progress_Func progress;
270 void* progress_data;
271 TA_Info_Func info;
272 TA_Info_Post_Func info_post;
273 void* info_data;
274 TA_Alloc_Func allocate;
275 TA_Free_Func deallocate;
276 FT_UInt hinting_range_min;
277 FT_UInt hinting_range_max;
278 FT_UInt hinting_limit;
279 FT_UInt increase_x_height;
280 number_range* x_height_snapping_exceptions;
281 FT_UInt fallback_stem_width;
282 FT_Int gray_stem_width_mode;
283 FT_Int gdi_cleartype_stem_width_mode;
284 FT_Int dw_cleartype_stem_width_mode;
285 FT_Bool windows_compatibility;
286 FT_Bool adjust_subglyphs;
287 FT_Bool hint_composites;
288 FT_Bool ignore_restrictions;
289 TA_Style fallback_style;
290 FT_Bool fallback_scaling;
291 TA_Script default_script;
292 FT_Bool symbol;
293 FT_Bool dehint;
294 FT_Bool debug;
295 FT_Bool TTFA_info;
296 unsigned long long epoch;
300 #include "tatables.h"
301 #include "tabytecode.h"
302 #include "tacontrol.h"
305 /* in file `tascript.c' */
306 extern const char* script_names[];
307 extern size_t script_names_size;
310 const char*
311 TA_get_error_message(FT_Error error);
313 char*
314 TA_font_dump_parameters(FONT* font,
315 FT_Bool format);
317 void
318 TA_get_current_time(FONT* font,
319 FT_ULong* high,
320 FT_ULong* low);
322 FT_Byte*
323 TA_build_push(FT_Byte* bufp,
324 FT_UInt* args,
325 FT_UInt num_args,
326 FT_Bool need_words,
327 FT_Bool optimize);
329 FT_Error
330 TA_font_init(FONT* font);
331 void
332 TA_font_unload(FONT* font,
333 const char* in_buf,
334 char** out_bufp,
335 const char* control_buf,
336 const char* reference_buf);
338 FT_Error
339 TA_font_file_read(FILE* file,
340 FT_Byte** buffer,
341 size_t* length);
342 FT_Error
343 TA_font_file_write(FONT* font,
344 FILE* out_file);
345 FT_Error
346 TA_control_file_read(FONT* font,
347 FILE* control_file);
349 FT_Error
350 TA_sfnt_compute_composite_pointsums(SFNT* sfnt,
351 FONT* font);
353 FT_Error
354 TA_sfnt_build_glyph_instructions(SFNT* sfnt,
355 FONT* font,
356 FT_Long idx);
358 FT_Error
359 TA_sfnt_split_into_SFNT_tables(SFNT* sfnt,
360 FONT* font);
362 FT_Error
363 TA_sfnt_build_cvt_table(SFNT* sfnt,
364 FONT* font);
366 FT_Error
367 TA_table_build_TTFA(FT_Byte** TTFA,
368 FT_ULong* TTFA_len,
369 FONT* font);
371 FT_Error
372 TA_table_build_DSIG(FT_Byte** DSIG);
374 FT_Error
375 TA_sfnt_build_fpgm_table(SFNT* sfnt,
376 FONT* font);
378 FT_Error
379 TA_sfnt_build_gasp_table(SFNT* sfnt,
380 FONT* font);
382 FT_Error
383 TA_sfnt_split_glyf_table(SFNT* sfnt,
384 FONT* font);
385 FT_Error
386 TA_sfnt_build_glyf_table(SFNT* sfnt,
387 FONT* font);
388 FT_Error
389 TA_sfnt_create_glyf_data(SFNT* sfnt,
390 FONT* font);
391 FT_Error
392 TA_sfnt_handle_coverage(SFNT* sfnt,
393 FONT* font);
394 void
395 TA_sfnt_adjust_coverage(SFNT* sfnt,
396 FONT* font);
397 #if 0
398 void
399 TA_sfnt_copy_master_coverage(SFNT* sfnt,
400 FONT* font);
401 #endif
403 FT_Error
404 TA_sfnt_update_GPOS_table(SFNT* sfnt,
405 FONT* font);
407 FT_Error
408 TA_sfnt_update_hmtx_table(SFNT* sfnt,
409 FONT* font);
411 FT_Error
412 TA_sfnt_build_loca_table(SFNT* sfnt,
413 FONT* font);
415 FT_Error
416 TA_sfnt_update_maxp_table(SFNT* sfnt,
417 FONT* font);
419 FT_Error
420 TA_sfnt_update_post_table(SFNT* sfnt,
421 FONT* font);
423 FT_Error
424 TA_sfnt_update_name_table(SFNT* sfnt,
425 FONT* font);
427 FT_Error
428 TA_sfnt_build_prep_table(SFNT* sfnt,
429 FONT* font);
431 FT_Error
432 TA_sfnt_build_TTF_header(SFNT* sfnt,
433 FONT* font,
434 FT_Byte** header_buf,
435 FT_ULong* header_len,
436 FT_Int do_complete);
437 FT_Error
438 TA_font_build_TTF(FONT* font);
440 FT_Error
441 TA_font_build_TTC(FONT* font);
443 #endif /* TA_H_ */
445 /* end of ta.h */