Make `number_set_prepend' merge ranges.
[ttfautohint.git] / lib / ttfautohint.c
blob00c8a66dec0e33c8b0738b43c0fdf173da93722a
1 /* ttfautohint.c */
3 /*
4 * Copyright (C) 2011-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 /* 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 const char* error_string = NULL;
53 unsigned int errlinenum = 0;
54 char* errline = NULL;
55 char* errpos = NULL;
56 FT_Bool free_errline = 0;
58 FILE* in_file = NULL;
59 FILE* out_file = NULL;
60 FILE* deltas_file = NULL;
62 const char* in_buf = NULL;
63 size_t in_len = 0;
64 char** out_bufp = NULL;
65 size_t* out_lenp = NULL;
66 const char* deltas_buf = NULL;
67 size_t deltas_len = 0;
69 const unsigned char** error_stringp = NULL;
71 Deltas* deltas = NULL;
73 FT_Long hinting_range_min = -1;
74 FT_Long hinting_range_max = -1;
75 FT_Long hinting_limit = -1;
76 FT_Long increase_x_height = -1;
78 const char* x_height_snapping_exceptions_string = NULL;
79 number_range* x_height_snapping_exceptions = NULL;
81 FT_Long fallback_stem_width = 0;
83 FT_Bool gray_strong_stem_width = 0;
84 FT_Bool gdi_cleartype_strong_stem_width = 1;
85 FT_Bool dw_cleartype_strong_stem_width = 0;
87 TA_Progress_Func progress = NULL;
88 void* progress_data = NULL;
89 TA_Error_Func err = NULL;
90 void* err_data = NULL;
91 TA_Info_Func info = 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;
108 const char* op;
110 if (!options || !*options)
112 error = FT_Err_Invalid_Argument;
113 goto Err1;
116 /* XXX */
117 va_start(ap, options);
119 op = options;
121 for(;;)
123 const char* start;
124 size_t len;
127 start = op;
129 /* search comma */
130 while (*op && *op != ',')
131 op++;
133 /* remove leading whitespace */
134 while (isspace(*start))
135 start++;
137 /* check for empty option */
138 if (start == op)
139 goto End;
141 len = op - start;
143 /* the `COMPARE' macro uses `len' and `start' */
145 /* handle options -- don't forget to update parameter dump below! */
146 if (COMPARE("adjust-subglyphs"))
147 adjust_subglyphs = (FT_Bool)va_arg(ap, FT_Int);
148 else if (COMPARE("debug"))
149 debug = (FT_Bool)va_arg(ap, FT_Int);
150 else if (COMPARE("default-script"))
151 default_script_string = va_arg(ap, const char*);
152 else if (COMPARE("dehint"))
153 dehint = (FT_Bool)va_arg(ap, FT_Int);
154 else if (COMPARE("deltas-buffer"))
156 deltas_file = NULL;
157 deltas_buf = va_arg(ap, const char*);
159 else if (COMPARE("deltas-buffer-len"))
161 deltas_file = NULL;
162 deltas_len = va_arg(ap, size_t);
164 else if (COMPARE("deltas-file"))
166 deltas_file = va_arg(ap, FILE*);
167 deltas_buf = NULL;
168 deltas_len = 0;
170 else if (COMPARE("dw-cleartype-strong-stem-width"))
171 dw_cleartype_strong_stem_width = (FT_Bool)va_arg(ap, FT_Int);
172 else if (COMPARE("error-callback"))
173 err = va_arg(ap, TA_Error_Func);
174 else if (COMPARE("error-callback-data"))
175 err_data = va_arg(ap, void*);
176 else if (COMPARE("error-string"))
177 error_stringp = va_arg(ap, const unsigned char**);
178 else if (COMPARE("fallback-script"))
179 fallback_script_string = va_arg(ap, const char*);
180 else if (COMPARE("fallback-stem-width"))
181 fallback_stem_width = (FT_Long)va_arg(ap, FT_UInt);
182 else if (COMPARE("gdi-cleartype-strong-stem-width"))
183 gdi_cleartype_strong_stem_width = (FT_Bool)va_arg(ap, FT_Int);
184 else if (COMPARE("gray-strong-stem-width"))
185 gray_strong_stem_width = (FT_Bool)va_arg(ap, FT_Int);
186 else if (COMPARE("hinting-limit"))
187 hinting_limit = (FT_Long)va_arg(ap, FT_UInt);
188 else if (COMPARE("hinting-range-max"))
189 hinting_range_max = (FT_Long)va_arg(ap, FT_UInt);
190 else if (COMPARE("hinting-range-min"))
191 hinting_range_min = (FT_Long)va_arg(ap, FT_UInt);
192 else if (COMPARE("hint-composites"))
193 hint_composites = (FT_Bool)va_arg(ap, FT_Int);
194 else if (COMPARE("ignore-restrictions"))
195 ignore_restrictions = (FT_Bool)va_arg(ap, FT_Int);
196 else if (COMPARE("in-buffer"))
198 in_file = NULL;
199 in_buf = va_arg(ap, const char*);
201 else if (COMPARE("in-buffer-len"))
203 in_file = NULL;
204 in_len = va_arg(ap, size_t);
206 else if (COMPARE("in-file"))
208 in_file = va_arg(ap, FILE*);
209 in_buf = NULL;
210 in_len = 0;
212 else if (COMPARE("increase-x-height"))
213 increase_x_height = (FT_Long)va_arg(ap, FT_UInt);
214 else if (COMPARE("info-callback"))
215 info = va_arg(ap, TA_Info_Func);
216 else if (COMPARE("info-callback-data"))
217 info_data = va_arg(ap, void*);
218 else if (COMPARE("out-buffer"))
220 out_file = NULL;
221 out_bufp = va_arg(ap, char**);
223 else if (COMPARE("out-buffer-len"))
225 out_file = NULL;
226 out_lenp = va_arg(ap, size_t*);
228 else if (COMPARE("out-file"))
230 out_file = va_arg(ap, FILE*);
231 out_bufp = NULL;
232 out_lenp = NULL;
234 else if (COMPARE("pre-hinting"))
235 adjust_subglyphs = (FT_Bool)va_arg(ap, FT_Int);
236 else if (COMPARE("progress-callback"))
237 progress = va_arg(ap, TA_Progress_Func);
238 else if (COMPARE("progress-callback-data"))
239 progress_data = va_arg(ap, void*);
240 else if (COMPARE("symbol"))
241 symbol = (FT_Bool)va_arg(ap, FT_Int);
242 else if (COMPARE("windows-compatibility"))
243 windows_compatibility = (FT_Bool)va_arg(ap, FT_Int);
244 else if (COMPARE("x-height-snapping-exceptions"))
245 x_height_snapping_exceptions_string = va_arg(ap, const char*);
246 else
248 error = TA_Err_Unknown_Argument;
249 goto Err1;
252 End:
253 if (!*op)
254 break;
255 op++;
258 va_end(ap);
260 /* check options */
262 if (!(in_file
263 || (in_buf && in_len)))
265 error = FT_Err_Invalid_Argument;
266 goto Err1;
269 if (!(out_file
270 || (out_bufp && out_lenp)))
272 error = FT_Err_Invalid_Argument;
273 goto Err1;
276 font = (FONT*)calloc(1, sizeof (FONT));
277 if (!font)
279 error = FT_Err_Out_Of_Memory;
280 goto Err1;
283 if (dehint)
284 goto No_check;
286 if (hinting_range_min >= 0 && hinting_range_min < 2)
288 error = FT_Err_Invalid_Argument;
289 goto Err1;
291 if (hinting_range_min < 0)
292 hinting_range_min = TA_HINTING_RANGE_MIN;
294 if (hinting_range_max >= 0 && hinting_range_max < hinting_range_min)
296 error = FT_Err_Invalid_Argument;
297 goto Err1;
299 if (hinting_range_max < 0)
300 hinting_range_max = TA_HINTING_RANGE_MAX;
302 /* value 0 is valid */
303 if (hinting_limit > 0 && hinting_limit < hinting_range_max)
305 error = FT_Err_Invalid_Argument;
306 goto Err1;
308 if (hinting_limit < 0)
309 hinting_limit = TA_HINTING_LIMIT;
311 if (increase_x_height > 0
312 && increase_x_height < TA_PROP_INCREASE_X_HEIGHT_MIN)
314 error = FT_Err_Invalid_Argument;
315 goto Err1;
317 if (increase_x_height < 0)
318 increase_x_height = TA_INCREASE_X_HEIGHT;
320 if (fallback_script_string)
322 int i;
325 for (i = 0; i < TA_STYLE_MAX; i++)
327 TA_StyleClass style_class = ta_style_classes[i];
330 if (style_class->coverage == TA_COVERAGE_DEFAULT
331 && !strcmp(script_names[style_class->script],
332 fallback_script_string))
333 break;
335 if (i == TA_STYLE_MAX)
337 error = FT_Err_Invalid_Argument;
338 goto Err1;
341 fallback_style = (TA_Style)i;
344 if (default_script_string)
346 int i;
349 for (i = 0; i < TA_SCRIPT_MAX; i++)
351 if (!strcmp(script_names[i], default_script_string))
352 break;
354 if (i == TA_SCRIPT_MAX)
356 error = FT_Err_Invalid_Argument;
357 goto Err1;
360 default_script = (TA_Script)i;
363 if (x_height_snapping_exceptions_string)
365 const char* s = number_set_parse(x_height_snapping_exceptions_string,
366 &x_height_snapping_exceptions,
367 TA_PROP_INCREASE_X_HEIGHT_MIN,
368 0x7FFF);
369 if (*s)
371 /* we map numberset.h's error codes to values starting with 0x100 */
372 error = 0x100 - (FT_Error)x_height_snapping_exceptions;
373 errlinenum = 0;
374 errline = (char*)x_height_snapping_exceptions_string;
375 errpos = (char*)s;
377 goto Err1;
381 font->hinting_range_min = (FT_UInt)hinting_range_min;
382 font->hinting_range_max = (FT_UInt)hinting_range_max;
383 font->hinting_limit = (FT_UInt)hinting_limit;
384 font->increase_x_height = increase_x_height;
385 font->x_height_snapping_exceptions = x_height_snapping_exceptions;
386 font->fallback_stem_width = (FT_UInt)fallback_stem_width;
388 font->gray_strong_stem_width = gray_strong_stem_width;
389 font->gdi_cleartype_strong_stem_width = gdi_cleartype_strong_stem_width;
390 font->dw_cleartype_strong_stem_width = dw_cleartype_strong_stem_width;
392 font->windows_compatibility = windows_compatibility;
393 font->ignore_restrictions = ignore_restrictions;
394 font->adjust_subglyphs = adjust_subglyphs;
395 font->hint_composites = hint_composites;
396 font->fallback_style = fallback_style;
397 font->default_script = default_script;
398 font->symbol = symbol;
400 No_check:
401 font->progress = progress;
402 font->progress_data = progress_data;
403 font->info = info;
404 font->info_data = info_data;
406 font->debug = debug;
407 font->dehint = dehint;
409 font->gasp_idx = MISSING;
411 /* start with processing the data */
413 if (in_file)
415 error = TA_font_file_read(font, in_file);
416 if (error)
417 goto Err;
419 else
421 /* a valid TTF can never be that small */
422 if (in_len < 100)
424 error = TA_Err_Invalid_Font_Type;
425 goto Err1;
427 font->in_buf = (FT_Byte*)in_buf;
428 font->in_len = in_len;
431 if (deltas_file)
433 error = TA_deltas_file_read(font, deltas_file);
434 if (error)
435 goto Err;
437 else if (deltas_buf)
439 font->deltas_buf = (char*)deltas_buf;
440 font->deltas_len = deltas_len;
443 error = TA_font_init(font);
444 if (error)
445 goto Err;
447 if (font->debug)
449 _ta_debug = 1;
450 _ta_debug_global = 1;
453 /* we do some loops over all subfonts -- */
454 /* to process options early, just start with loading all of them */
455 for (i = 0; i < font->num_sfnts; i++)
457 SFNT* sfnt = &font->sfnts[i];
458 FT_UInt idx;
461 error = FT_New_Memory_Face(font->lib, font->in_buf, font->in_len,
462 i, &sfnt->face);
464 /* assure that the font hasn't been already processed by ttfautohint; */
465 /* another, more thorough check is done in TA_glyph_parse_simple */
466 idx = FT_Get_Name_Index(sfnt->face, (FT_String*)TTFAUTOHINT_GLYPH);
467 if (idx)
469 error = TA_Err_Already_Processed;
470 goto Err;
473 if (error)
474 goto Err;
477 /* process delta exceptions data */
478 error = TA_deltas_parse_buffer(font,
479 &deltas,
480 &errlinenum, &errline, &errpos);
481 if (error)
483 free_errline = 1;
484 goto Err;
487 /* now we are able to dump all parameters */
488 if (debug)
490 error = TA_font_dump_parameters(font, stderr, deltas, dehint, 1);
491 if (error)
492 goto Err;
495 error = TA_deltas_build_tree(font, deltas);
496 if (error)
497 goto Err;
499 /* loop again over subfonts and continue processing */
500 for (i = 0; i < font->num_sfnts; i++)
502 SFNT* sfnt = &font->sfnts[i];
505 error = TA_sfnt_split_into_SFNT_tables(sfnt, font);
506 if (error)
507 goto Err;
509 /* check permission */
510 if (sfnt->OS2_idx != MISSING)
512 SFNT_Table* OS2_table = &font->tables[sfnt->OS2_idx];
515 /* check lower byte of the `fsType' field */
516 if (OS2_table->buf[OS2_FSTYPE_OFFSET + 1] == 0x02
517 && !font->ignore_restrictions)
519 error = TA_Err_Missing_Legal_Permission;
520 goto Err;
524 if (font->dehint)
526 error = TA_sfnt_split_glyf_table(sfnt, font);
527 if (error)
528 goto Err;
530 else
532 if (font->adjust_subglyphs)
533 error = TA_sfnt_create_glyf_data(sfnt, font);
534 else
535 error = TA_sfnt_split_glyf_table(sfnt, font);
536 if (error)
537 goto Err;
539 /* this call creates a `globals' object... */
540 error = TA_sfnt_handle_coverage(sfnt, font);
541 if (error)
542 goto Err;
544 /* ... so that we now can initialize its properties */
545 TA_sfnt_set_properties(sfnt, font);
549 if (!font->dehint)
551 for (i = 0; i < font->num_sfnts; i++)
553 SFNT* sfnt = &font->sfnts[i];
556 TA_sfnt_adjust_coverage(sfnt, font);
560 #if 0
561 /* this code is here for completeness -- */
562 /* right now, `glyf' tables get hinted only once, */
563 /* and referring subfonts simply reuse it, */
564 /* but this might change in the future */
566 if (!font->dehint)
568 for (i = 0; i < font->num_sfnts; i++)
570 SFNT* sfnt = &font->sfnts[i];
573 TA_sfnt_copy_master_coverage(sfnt, font);
576 #endif
578 /* loop again over subfonts */
579 for (i = 0; i < font->num_sfnts; i++)
581 SFNT* sfnt = &font->sfnts[i];
584 error = ta_loader_init(font);
585 if (error)
586 goto Err;
588 error = TA_sfnt_build_gasp_table(sfnt, font);
589 if (error)
590 goto Err;
591 if (!font->dehint)
593 error = TA_sfnt_build_cvt_table(sfnt, font);
594 if (error)
595 goto Err;
596 error = TA_sfnt_build_fpgm_table(sfnt, font);
597 if (error)
598 goto Err;
599 error = TA_sfnt_build_prep_table(sfnt, font);
600 if (error)
601 goto Err;
603 error = TA_sfnt_build_glyf_table(sfnt, font);
604 if (error)
605 goto Err;
606 error = TA_sfnt_build_loca_table(sfnt, font);
607 if (error)
608 goto Err;
610 if (font->loader)
611 ta_loader_done(font);
614 for (i = 0; i < font->num_sfnts; i++)
616 SFNT* sfnt = &font->sfnts[i];
619 error = TA_sfnt_update_maxp_table(sfnt, font);
620 if (error)
621 goto Err;
623 if (!font->dehint)
625 /* we add one glyph for composites */
626 if (sfnt->max_components
627 && !font->adjust_subglyphs
628 && font->hint_composites)
630 error = TA_sfnt_update_hmtx_table(sfnt, font);
631 if (error)
632 goto Err;
633 error = TA_sfnt_update_post_table(sfnt, font);
634 if (error)
635 goto Err;
636 error = TA_sfnt_update_GPOS_table(sfnt, font);
637 if (error)
638 goto Err;
642 if (font->info)
644 /* add info about ttfautohint to the version string */
645 error = TA_sfnt_update_name_table(sfnt, font);
646 if (error)
647 goto Err;
651 if (font->num_sfnts == 1)
652 error = TA_font_build_TTF(font);
653 else
654 error = TA_font_build_TTC(font);
655 if (error)
656 goto Err;
658 if (out_file)
660 error = TA_font_file_write(font, out_file);
661 if (error)
662 goto Err;
664 else
666 *out_bufp = (char*)font->out_buf;
667 *out_lenp = font->out_len;
670 error = TA_Err_Ok;
672 Err:
673 TA_deltas_free(deltas);
674 TA_deltas_free_tree(font);
675 TA_font_unload(font, in_buf, out_bufp, deltas_buf);
677 Err1:
678 error_string = TA_get_error_message(error);
680 if (error_stringp)
681 *error_stringp = (const unsigned char*)error_string;
683 if (err)
684 err(error,
685 error_string,
686 errlinenum,
687 errline,
688 errpos,
689 err_data);
691 if (free_errline)
692 free(errline);
694 return error;
697 /* end of ttfautohint.c */