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. */
26 #define COMPARE(str) \
27 (len == (sizeof (str) - 1) \
28 && !strncmp(start, str, sizeof (str) - 1))
32 TA_sfnt_set_properties(SFNT
* sfnt
,
35 TA_FaceGlobals globals
= (TA_FaceGlobals
)sfnt
->face
->autohint
.data
;
38 globals
->increase_x_height
= font
->increase_x_height
;
43 TTF_autohint(const char* options
,
52 char* error_string
= NULL
;
53 unsigned int errlinenum
= 0;
56 FT_Bool free_errline
= 0;
57 FT_Bool free_error_string
= 0;
60 FILE* out_file
= NULL
;
61 FILE* control_file
= NULL
;
63 const char* in_buf
= NULL
;
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;
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
;
107 FT_Bool TTFA_info
= 0;
111 if (!options
|| !*options
)
113 error
= FT_Err_Invalid_Argument
;
118 va_start(ap
, options
);
131 while (*op
&& *op
!= ',')
134 /* remove leading whitespace */
135 while (isspace(*start
))
138 /* check for empty option */
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"))
158 control_buf
= va_arg(ap
, const char*);
160 else if (COMPARE("control-buffer-len"))
163 control_len
= va_arg(ap
, size_t);
165 else if (COMPARE("control-file"))
167 control_file
= va_arg(ap
, FILE*);
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"))
200 in_buf
= va_arg(ap
, const char*);
202 else if (COMPARE("in-buffer-len"))
205 in_len
= va_arg(ap
, size_t);
207 else if (COMPARE("in-file"))
209 in_file
= va_arg(ap
, FILE*);
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"))
224 out_bufp
= va_arg(ap
, char**);
226 else if (COMPARE("out-buffer-len"))
229 out_lenp
= va_arg(ap
, size_t*);
231 else if (COMPARE("out-file"))
233 out_file
= va_arg(ap
, FILE*);
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*);
253 error
= TA_Err_Unknown_Argument
;
268 || (in_buf
&& in_len
)))
270 error
= FT_Err_Invalid_Argument
;
275 || (out_bufp
&& out_lenp
)))
277 error
= FT_Err_Invalid_Argument
;
281 font
= (FONT
*)calloc(1, sizeof (FONT
));
284 error
= FT_Err_Out_Of_Memory
;
291 if (hinting_range_min
>= 0 && hinting_range_min
< 2)
293 error
= FT_Err_Invalid_Argument
;
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
;
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
;
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
;
322 if (increase_x_height
< 0)
323 increase_x_height
= TA_INCREASE_X_HEIGHT
;
325 if (fallback_script_string
)
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
))
340 if (i
== TA_STYLE_MAX
)
342 error
= FT_Err_Invalid_Argument
;
346 fallback_style
= (TA_Style
)i
;
349 if (default_script_string
)
354 for (i
= 0; i
< TA_SCRIPT_MAX
; i
++)
356 if (!strcmp(script_names
[i
], default_script_string
))
359 if (i
== TA_SCRIPT_MAX
)
361 error
= FT_Err_Invalid_Argument
;
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
,
376 /* we map numberset.h's error codes to values starting with 0x100 */
377 error
= 0x100 - (FT_Error
)x_height_snapping_exceptions
;
379 errline
= (char*)x_height_snapping_exceptions_string
;
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
;
406 font
->progress
= progress
;
407 font
->progress_data
= progress_data
;
409 font
->info_post
= info_post
;
410 font
->info_data
= info_data
;
413 font
->dehint
= dehint
;
414 font
->TTFA_info
= TTFA_info
;
416 font
->gasp_idx
= MISSING
;
418 /* start with processing the data */
422 error
= TA_font_file_read(font
, in_file
);
428 /* a valid TTF can never be that small */
431 error
= TA_Err_Invalid_Font_Type
;
434 font
->in_buf
= (FT_Byte
*)in_buf
;
435 font
->in_len
= in_len
;
440 error
= TA_control_file_read(font
, control_file
);
444 else if (control_buf
)
446 font
->control_buf
= (char*)control_buf
;
447 font
->control_len
= control_len
;
450 error
= TA_font_init(font
);
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
];
468 error
= FT_New_Memory_Face(font
->lib
, font
->in_buf
, font
->in_len
,
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
);
476 error
= TA_Err_Already_Processed
;
484 /* process control instructions */
485 error
= TA_control_parse_buffer(font
,
487 &errlinenum
, &errline
, &errpos
);
491 free_error_string
= 1;
495 /* now we are able to dump all parameters */
501 s
= TA_font_dump_parameters(font
, 1);
504 error
= FT_Err_Out_Of_Memory
;
508 fprintf(stderr
, "%s", s
);
512 error
= TA_control_build_tree(font
);
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
);
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
;
543 error
= TA_sfnt_split_glyf_table(sfnt
, font
);
549 if (font
->adjust_subglyphs
)
550 error
= TA_sfnt_create_glyf_data(sfnt
, font
);
552 error
= TA_sfnt_split_glyf_table(sfnt
, font
);
556 /* this call creates a `globals' object... */
557 error
= TA_sfnt_handle_coverage(sfnt
, font
);
561 /* ... so that we now can initialize its properties */
562 TA_sfnt_set_properties(sfnt
, font
);
568 for (i
= 0; i
< font
->num_sfnts
; i
++)
570 SFNT
* sfnt
= &font
->sfnts
[i
];
573 TA_sfnt_adjust_coverage(sfnt
, font
);
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 */
585 for (i
= 0; i
< font
->num_sfnts
; i
++)
587 SFNT
* sfnt
= &font
->sfnts
[i
];
590 TA_sfnt_copy_master_coverage(sfnt
, font
);
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
);
605 error
= TA_sfnt_build_gasp_table(sfnt
, font
);
610 error
= TA_sfnt_build_cvt_table(sfnt
, font
);
613 error
= TA_sfnt_build_fpgm_table(sfnt
, font
);
616 error
= TA_sfnt_build_prep_table(sfnt
, font
);
620 error
= TA_sfnt_build_glyf_table(sfnt
, font
);
623 error
= TA_sfnt_build_loca_table(sfnt
, font
);
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
);
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
);
650 error
= TA_sfnt_update_post_table(sfnt
, font
);
653 error
= TA_sfnt_update_GPOS_table(sfnt
, font
);
661 /* add info about ttfautohint to the version string */
662 error
= TA_sfnt_update_name_table(sfnt
, font
);
668 if (font
->num_sfnts
== 1)
669 error
= TA_font_build_TTF(font
);
671 error
= TA_font_build_TTC(font
);
677 error
= TA_font_file_write(font
, out_file
);
683 *out_bufp
= (char*)font
->out_buf
;
684 *out_lenp
= font
->out_len
;
690 TA_control_free(font
->control
);
691 TA_control_free_tree(font
);
692 TA_font_unload(font
, in_buf
, out_bufp
, control_buf
);
696 error_string
= (char*)TA_get_error_message(error
);
698 /* this must be a static value */
700 *error_stringp
= (const unsigned char*)TA_get_error_message(error
);
712 if (free_error_string
)
718 /* end of ttfautohint.c */