Synchronize with FreeType [4/4].
[ttfautohint.git] / lib / ttfautohint.c
blobe366f8260449a5073c996315bcb90a61a573a070
1 /* ttfautohint.c */
3 /*
4 * Copyright (C) 2011-2015 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 /* This file needs FreeType 2.4.5 or newer. */
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <ctype.h>
23 #include "ta.h"
26 #define COMPARE(str) \
27 (len == (sizeof (str) - 1) \
28 && !strncmp(start, str, sizeof (str) - 1))
31 void
32 TA_sfnt_set_properties(SFNT* sfnt,
33 FONT* font)
35 TA_FaceGlobals globals = (TA_FaceGlobals)sfnt->face->autohint.data;
38 globals->increase_x_height = font->increase_x_height;
42 TA_Error
43 TTF_autohint(const char* options,
44 ...)
46 va_list ap;
48 FONT* font;
49 FT_Long i;
51 TA_Error error;
52 char* error_string = NULL;
53 unsigned int errlinenum = 0;
54 char* errline = NULL;
55 char* errpos = NULL;
56 FT_Bool free_errline = 0;
57 FT_Bool free_error_string = 0;
59 FILE* in_file = NULL;
60 FILE* out_file = NULL;
61 FILE* control_file = NULL;
63 const char* in_buf = NULL;
64 size_t in_len = 0;
65 char** out_bufp = NULL;
66 size_t* out_lenp = NULL;
67 const char* control_buf = NULL;
68 size_t control_len = 0;
70 const unsigned char** error_stringp = NULL;
72 FT_Long hinting_range_min = -1;
73 FT_Long hinting_range_max = -1;
74 FT_Long hinting_limit = -1;
75 FT_Long increase_x_height = -1;
77 const char* x_height_snapping_exceptions_string = NULL;
78 number_range* x_height_snapping_exceptions = NULL;
80 FT_Long fallback_stem_width = 0;
82 FT_Bool gray_strong_stem_width = 0;
83 FT_Bool gdi_cleartype_strong_stem_width = 1;
84 FT_Bool dw_cleartype_strong_stem_width = 0;
86 TA_Progress_Func progress = NULL;
87 void* progress_data = NULL;
88 TA_Error_Func err = NULL;
89 void* err_data = NULL;
90 TA_Info_Func info = NULL;
91 TA_Info_Post_Func info_post = NULL;
92 void* info_data = NULL;
94 FT_Bool windows_compatibility = 0;
95 FT_Bool ignore_restrictions = 0;
96 FT_Bool adjust_subglyphs = 0;
97 FT_Bool hint_composites = 0;
98 FT_Bool symbol = 0;
100 const char* fallback_script_string = NULL;
101 const char* default_script_string = NULL;
102 TA_Style fallback_style = TA_STYLE_NONE_DFLT;
103 TA_Script default_script = TA_SCRIPT_LATN;
105 FT_Bool dehint = 0;
106 FT_Bool debug = 0;
107 FT_Bool TTFA_info = 0;
109 const char* op;
111 if (!options || !*options)
113 error = FT_Err_Invalid_Argument;
114 goto Err1;
117 /* XXX */
118 va_start(ap, options);
120 op = options;
122 for(;;)
124 const char* start;
125 size_t len;
128 start = op;
130 /* search comma */
131 while (*op && *op != ',')
132 op++;
134 /* remove leading whitespace */
135 while (isspace(*start))
136 start++;
138 /* check for empty option */
139 if (start == op)
140 goto End;
142 len = op - start;
144 /* the `COMPARE' macro uses `len' and `start' */
146 /* handle options -- don't forget to update parameter dump below! */
147 if (COMPARE("adjust-subglyphs"))
148 adjust_subglyphs = (FT_Bool)va_arg(ap, FT_Int);
149 else if (COMPARE("debug"))
150 debug = (FT_Bool)va_arg(ap, FT_Int);
151 else if (COMPARE("default-script"))
152 default_script_string = va_arg(ap, const char*);
153 else if (COMPARE("dehint"))
154 dehint = (FT_Bool)va_arg(ap, FT_Int);
155 else if (COMPARE("control-buffer"))
157 control_file = NULL;
158 control_buf = va_arg(ap, const char*);
160 else if (COMPARE("control-buffer-len"))
162 control_file = NULL;
163 control_len = va_arg(ap, size_t);
165 else if (COMPARE("control-file"))
167 control_file = va_arg(ap, FILE*);
168 control_buf = NULL;
169 control_len = 0;
171 else if (COMPARE("dw-cleartype-strong-stem-width"))
172 dw_cleartype_strong_stem_width = (FT_Bool)va_arg(ap, FT_Int);
173 else if (COMPARE("error-callback"))
174 err = va_arg(ap, TA_Error_Func);
175 else if (COMPARE("error-callback-data"))
176 err_data = va_arg(ap, void*);
177 else if (COMPARE("error-string"))
178 error_stringp = va_arg(ap, const unsigned char**);
179 else if (COMPARE("fallback-script"))
180 fallback_script_string = va_arg(ap, const char*);
181 else if (COMPARE("fallback-stem-width"))
182 fallback_stem_width = (FT_Long)va_arg(ap, FT_UInt);
183 else if (COMPARE("gdi-cleartype-strong-stem-width"))
184 gdi_cleartype_strong_stem_width = (FT_Bool)va_arg(ap, FT_Int);
185 else if (COMPARE("gray-strong-stem-width"))
186 gray_strong_stem_width = (FT_Bool)va_arg(ap, FT_Int);
187 else if (COMPARE("hinting-limit"))
188 hinting_limit = (FT_Long)va_arg(ap, FT_UInt);
189 else if (COMPARE("hinting-range-max"))
190 hinting_range_max = (FT_Long)va_arg(ap, FT_UInt);
191 else if (COMPARE("hinting-range-min"))
192 hinting_range_min = (FT_Long)va_arg(ap, FT_UInt);
193 else if (COMPARE("hint-composites"))
194 hint_composites = (FT_Bool)va_arg(ap, FT_Int);
195 else if (COMPARE("ignore-restrictions"))
196 ignore_restrictions = (FT_Bool)va_arg(ap, FT_Int);
197 else if (COMPARE("in-buffer"))
199 in_file = NULL;
200 in_buf = va_arg(ap, const char*);
202 else if (COMPARE("in-buffer-len"))
204 in_file = NULL;
205 in_len = va_arg(ap, size_t);
207 else if (COMPARE("in-file"))
209 in_file = va_arg(ap, FILE*);
210 in_buf = NULL;
211 in_len = 0;
213 else if (COMPARE("increase-x-height"))
214 increase_x_height = (FT_Long)va_arg(ap, FT_UInt);
215 else if (COMPARE("info-callback"))
216 info = va_arg(ap, TA_Info_Func);
217 else if (COMPARE("info-callback-data"))
218 info_data = va_arg(ap, void*);
219 else if (COMPARE("info-post-callback"))
220 info_post = va_arg(ap, TA_Info_Post_Func);
221 else if (COMPARE("out-buffer"))
223 out_file = NULL;
224 out_bufp = va_arg(ap, char**);
226 else if (COMPARE("out-buffer-len"))
228 out_file = NULL;
229 out_lenp = va_arg(ap, size_t*);
231 else if (COMPARE("out-file"))
233 out_file = va_arg(ap, FILE*);
234 out_bufp = NULL;
235 out_lenp = NULL;
237 else if (COMPARE("pre-hinting"))
238 adjust_subglyphs = (FT_Bool)va_arg(ap, FT_Int);
239 else if (COMPARE("progress-callback"))
240 progress = va_arg(ap, TA_Progress_Func);
241 else if (COMPARE("progress-callback-data"))
242 progress_data = va_arg(ap, void*);
243 else if (COMPARE("symbol"))
244 symbol = (FT_Bool)va_arg(ap, FT_Int);
245 else if (COMPARE("TTFA-info"))
246 TTFA_info = (FT_Bool)va_arg(ap, FT_Int);
247 else if (COMPARE("windows-compatibility"))
248 windows_compatibility = (FT_Bool)va_arg(ap, FT_Int);
249 else if (COMPARE("x-height-snapping-exceptions"))
250 x_height_snapping_exceptions_string = va_arg(ap, const char*);
251 else
253 error = TA_Err_Unknown_Argument;
254 goto Err1;
257 End:
258 if (!*op)
259 break;
260 op++;
263 va_end(ap);
265 /* check options */
267 if (!(in_file
268 || (in_buf && in_len)))
270 error = FT_Err_Invalid_Argument;
271 goto Err1;
274 if (!(out_file
275 || (out_bufp && out_lenp)))
277 error = FT_Err_Invalid_Argument;
278 goto Err1;
281 font = (FONT*)calloc(1, sizeof (FONT));
282 if (!font)
284 error = FT_Err_Out_Of_Memory;
285 goto Err1;
288 if (dehint)
289 goto No_check;
291 if (hinting_range_min >= 0 && hinting_range_min < 2)
293 error = FT_Err_Invalid_Argument;
294 goto Err1;
296 if (hinting_range_min < 0)
297 hinting_range_min = TA_HINTING_RANGE_MIN;
299 if (hinting_range_max >= 0 && hinting_range_max < hinting_range_min)
301 error = FT_Err_Invalid_Argument;
302 goto Err1;
304 if (hinting_range_max < 0)
305 hinting_range_max = TA_HINTING_RANGE_MAX;
307 /* value 0 is valid */
308 if (hinting_limit > 0 && hinting_limit < hinting_range_max)
310 error = FT_Err_Invalid_Argument;
311 goto Err1;
313 if (hinting_limit < 0)
314 hinting_limit = TA_HINTING_LIMIT;
316 if (increase_x_height > 0
317 && increase_x_height < TA_PROP_INCREASE_X_HEIGHT_MIN)
319 error = FT_Err_Invalid_Argument;
320 goto Err1;
322 if (increase_x_height < 0)
323 increase_x_height = TA_INCREASE_X_HEIGHT;
325 if (fallback_script_string)
327 int i;
330 for (i = 0; i < TA_STYLE_MAX; i++)
332 TA_StyleClass style_class = ta_style_classes[i];
335 if (style_class->coverage == TA_COVERAGE_DEFAULT
336 && !strcmp(script_names[style_class->script],
337 fallback_script_string))
338 break;
340 if (i == TA_STYLE_MAX)
342 error = FT_Err_Invalid_Argument;
343 goto Err1;
346 fallback_style = (TA_Style)i;
349 if (default_script_string)
351 int i;
354 for (i = 0; i < TA_SCRIPT_MAX; i++)
356 if (!strcmp(script_names[i], default_script_string))
357 break;
359 if (i == TA_SCRIPT_MAX)
361 error = FT_Err_Invalid_Argument;
362 goto Err1;
365 default_script = (TA_Script)i;
368 if (x_height_snapping_exceptions_string)
370 const char* s = number_set_parse(x_height_snapping_exceptions_string,
371 &x_height_snapping_exceptions,
372 TA_PROP_INCREASE_X_HEIGHT_MIN,
373 0x7FFF);
374 if (*s)
376 /* we map numberset.h's error codes to values starting with 0x100 */
377 error = 0x100 - (FT_Error)x_height_snapping_exceptions;
378 errlinenum = 0;
379 errline = (char*)x_height_snapping_exceptions_string;
380 errpos = (char*)s;
382 goto Err1;
386 font->hinting_range_min = (FT_UInt)hinting_range_min;
387 font->hinting_range_max = (FT_UInt)hinting_range_max;
388 font->hinting_limit = (FT_UInt)hinting_limit;
389 font->increase_x_height = increase_x_height;
390 font->x_height_snapping_exceptions = x_height_snapping_exceptions;
391 font->fallback_stem_width = (FT_UInt)fallback_stem_width;
393 font->gray_strong_stem_width = gray_strong_stem_width;
394 font->gdi_cleartype_strong_stem_width = gdi_cleartype_strong_stem_width;
395 font->dw_cleartype_strong_stem_width = dw_cleartype_strong_stem_width;
397 font->windows_compatibility = windows_compatibility;
398 font->ignore_restrictions = ignore_restrictions;
399 font->adjust_subglyphs = adjust_subglyphs;
400 font->hint_composites = hint_composites;
401 font->fallback_style = fallback_style;
402 font->default_script = default_script;
403 font->symbol = symbol;
405 No_check:
406 font->progress = progress;
407 font->progress_data = progress_data;
408 font->info = info;
409 font->info_post = info_post;
410 font->info_data = info_data;
412 font->debug = debug;
413 font->dehint = dehint;
414 font->TTFA_info = TTFA_info;
416 font->gasp_idx = MISSING;
418 /* start with processing the data */
420 if (in_file)
422 error = TA_font_file_read(font, in_file);
423 if (error)
424 goto Err;
426 else
428 /* a valid TTF can never be that small */
429 if (in_len < 100)
431 error = TA_Err_Invalid_Font_Type;
432 goto Err1;
434 font->in_buf = (FT_Byte*)in_buf;
435 font->in_len = in_len;
438 if (control_file)
440 error = TA_control_file_read(font, control_file);
441 if (error)
442 goto Err;
444 else if (control_buf)
446 font->control_buf = (char*)control_buf;
447 font->control_len = control_len;
450 error = TA_font_init(font);
451 if (error)
452 goto Err;
454 if (font->debug)
456 _ta_debug = 1;
457 _ta_debug_global = 1;
460 /* we do some loops over all subfonts -- */
461 /* to process options early, just start with loading all of them */
462 for (i = 0; i < font->num_sfnts; i++)
464 SFNT* sfnt = &font->sfnts[i];
465 FT_UInt idx;
468 error = FT_New_Memory_Face(font->lib, font->in_buf, font->in_len,
469 i, &sfnt->face);
471 /* assure that the font hasn't been already processed by ttfautohint; */
472 /* another, more thorough check is done in TA_glyph_parse_simple */
473 idx = FT_Get_Name_Index(sfnt->face, (FT_String*)TTFAUTOHINT_GLYPH);
474 if (idx)
476 error = TA_Err_Already_Processed;
477 goto Err;
480 if (error)
481 goto Err;
484 /* process control instructions */
485 error = TA_control_parse_buffer(font,
486 &error_string,
487 &errlinenum, &errline, &errpos);
488 if (error)
490 free_errline = 1;
491 free_error_string = 1;
492 goto Err;
495 /* now we are able to dump all parameters */
496 if (debug)
498 char* s;
501 s = TA_font_dump_parameters(font, 1);
502 if (!s)
504 error = FT_Err_Out_Of_Memory;
505 goto Err;
508 fprintf(stderr, "%s", s);
509 free(s);
512 error = TA_control_build_tree(font);
513 if (error)
514 goto Err;
516 /* loop again over subfonts and continue processing */
517 for (i = 0; i < font->num_sfnts; i++)
519 SFNT* sfnt = &font->sfnts[i];
522 error = TA_sfnt_split_into_SFNT_tables(sfnt, font);
523 if (error)
524 goto Err;
526 /* check permission */
527 if (sfnt->OS2_idx != MISSING)
529 SFNT_Table* OS2_table = &font->tables[sfnt->OS2_idx];
532 /* check lower byte of the `fsType' field */
533 if (OS2_table->buf[OS2_FSTYPE_OFFSET + 1] == 0x02
534 && !font->ignore_restrictions)
536 error = TA_Err_Missing_Legal_Permission;
537 goto Err;
541 if (font->dehint)
543 error = TA_sfnt_split_glyf_table(sfnt, font);
544 if (error)
545 goto Err;
547 else
549 if (font->adjust_subglyphs)
550 error = TA_sfnt_create_glyf_data(sfnt, font);
551 else
552 error = TA_sfnt_split_glyf_table(sfnt, font);
553 if (error)
554 goto Err;
556 /* this call creates a `globals' object... */
557 error = TA_sfnt_handle_coverage(sfnt, font);
558 if (error)
559 goto Err;
561 /* ... so that we now can initialize its properties */
562 TA_sfnt_set_properties(sfnt, font);
566 if (!font->dehint)
568 for (i = 0; i < font->num_sfnts; i++)
570 SFNT* sfnt = &font->sfnts[i];
573 TA_sfnt_adjust_coverage(sfnt, font);
577 #if 0
578 /* this code is here for completeness -- */
579 /* right now, `glyf' tables get hinted only once, */
580 /* and referring subfonts simply reuse it, */
581 /* but this might change in the future */
583 if (!font->dehint)
585 for (i = 0; i < font->num_sfnts; i++)
587 SFNT* sfnt = &font->sfnts[i];
590 TA_sfnt_copy_master_coverage(sfnt, font);
593 #endif
595 /* loop again over subfonts */
596 for (i = 0; i < font->num_sfnts; i++)
598 SFNT* sfnt = &font->sfnts[i];
601 error = ta_loader_init(font);
602 if (error)
603 goto Err;
605 error = TA_sfnt_build_gasp_table(sfnt, font);
606 if (error)
607 goto Err;
608 if (!font->dehint)
610 error = TA_sfnt_build_cvt_table(sfnt, font);
611 if (error)
612 goto Err;
613 error = TA_sfnt_build_fpgm_table(sfnt, font);
614 if (error)
615 goto Err;
616 error = TA_sfnt_build_prep_table(sfnt, font);
617 if (error)
618 goto Err;
620 error = TA_sfnt_build_glyf_table(sfnt, font);
621 if (error)
622 goto Err;
623 error = TA_sfnt_build_loca_table(sfnt, font);
624 if (error)
625 goto Err;
627 if (font->loader)
628 ta_loader_done(font);
631 for (i = 0; i < font->num_sfnts; i++)
633 SFNT* sfnt = &font->sfnts[i];
636 error = TA_sfnt_update_maxp_table(sfnt, font);
637 if (error)
638 goto Err;
640 if (!font->dehint)
642 /* we add one glyph for composites */
643 if (sfnt->max_components
644 && !font->adjust_subglyphs
645 && font->hint_composites)
647 error = TA_sfnt_update_hmtx_table(sfnt, font);
648 if (error)
649 goto Err;
650 error = TA_sfnt_update_post_table(sfnt, font);
651 if (error)
652 goto Err;
653 error = TA_sfnt_update_GPOS_table(sfnt, font);
654 if (error)
655 goto Err;
659 if (font->info)
661 /* add info about ttfautohint to the version string */
662 error = TA_sfnt_update_name_table(sfnt, font);
663 if (error)
664 goto Err;
668 if (font->num_sfnts == 1)
669 error = TA_font_build_TTF(font);
670 else
671 error = TA_font_build_TTC(font);
672 if (error)
673 goto Err;
675 if (out_file)
677 error = TA_font_file_write(font, out_file);
678 if (error)
679 goto Err;
681 else
683 *out_bufp = (char*)font->out_buf;
684 *out_lenp = font->out_len;
687 error = TA_Err_Ok;
689 Err:
690 TA_control_free(font->control);
691 TA_control_free_tree(font);
692 TA_font_unload(font, in_buf, out_bufp, control_buf);
694 Err1:
695 if (!error_string)
696 error_string = (char*)TA_get_error_message(error);
698 /* this must be a static value */
699 if (error_stringp)
700 *error_stringp = (const unsigned char*)TA_get_error_message(error);
702 if (err)
703 err(error,
704 error_string,
705 errlinenum,
706 errline,
707 errpos,
708 err_data);
710 if (free_errline)
711 free(errline);
712 if (free_error_string)
713 free(error_string);
715 return error;
718 /* end of ttfautohint.c */