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. */
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 void* info_data
= NULL
;
93 FT_Bool windows_compatibility
= 0;
94 FT_Bool ignore_restrictions
= 0;
95 FT_Bool adjust_subglyphs
= 0;
96 FT_Bool hint_composites
= 0;
99 const char* fallback_script_string
= NULL
;
100 const char* default_script_string
= NULL
;
101 TA_Style fallback_style
= TA_STYLE_NONE_DFLT
;
102 TA_Script default_script
= TA_SCRIPT_LATN
;
106 FT_Bool TTFA_info
= 0;
110 if (!options
|| !*options
)
112 error
= FT_Err_Invalid_Argument
;
117 va_start(ap
, options
);
130 while (*op
&& *op
!= ',')
133 /* remove leading whitespace */
134 while (isspace(*start
))
137 /* check for empty option */
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("control-buffer"))
157 control_buf
= va_arg(ap
, const char*);
159 else if (COMPARE("control-buffer-len"))
162 control_len
= va_arg(ap
, size_t);
164 else if (COMPARE("control-file"))
166 control_file
= va_arg(ap
, FILE*);
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"))
199 in_buf
= va_arg(ap
, const char*);
201 else if (COMPARE("in-buffer-len"))
204 in_len
= va_arg(ap
, size_t);
206 else if (COMPARE("in-file"))
208 in_file
= va_arg(ap
, FILE*);
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"))
221 out_bufp
= va_arg(ap
, char**);
223 else if (COMPARE("out-buffer-len"))
226 out_lenp
= va_arg(ap
, size_t*);
228 else if (COMPARE("out-file"))
230 out_file
= va_arg(ap
, FILE*);
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("TTFA-info"))
243 TTFA_info
= (FT_Bool
)va_arg(ap
, FT_Int
);
244 else if (COMPARE("windows-compatibility"))
245 windows_compatibility
= (FT_Bool
)va_arg(ap
, FT_Int
);
246 else if (COMPARE("x-height-snapping-exceptions"))
247 x_height_snapping_exceptions_string
= va_arg(ap
, const char*);
250 error
= TA_Err_Unknown_Argument
;
265 || (in_buf
&& in_len
)))
267 error
= FT_Err_Invalid_Argument
;
272 || (out_bufp
&& out_lenp
)))
274 error
= FT_Err_Invalid_Argument
;
278 font
= (FONT
*)calloc(1, sizeof (FONT
));
281 error
= FT_Err_Out_Of_Memory
;
288 if (hinting_range_min
>= 0 && hinting_range_min
< 2)
290 error
= FT_Err_Invalid_Argument
;
293 if (hinting_range_min
< 0)
294 hinting_range_min
= TA_HINTING_RANGE_MIN
;
296 if (hinting_range_max
>= 0 && hinting_range_max
< hinting_range_min
)
298 error
= FT_Err_Invalid_Argument
;
301 if (hinting_range_max
< 0)
302 hinting_range_max
= TA_HINTING_RANGE_MAX
;
304 /* value 0 is valid */
305 if (hinting_limit
> 0 && hinting_limit
< hinting_range_max
)
307 error
= FT_Err_Invalid_Argument
;
310 if (hinting_limit
< 0)
311 hinting_limit
= TA_HINTING_LIMIT
;
313 if (increase_x_height
> 0
314 && increase_x_height
< TA_PROP_INCREASE_X_HEIGHT_MIN
)
316 error
= FT_Err_Invalid_Argument
;
319 if (increase_x_height
< 0)
320 increase_x_height
= TA_INCREASE_X_HEIGHT
;
322 if (fallback_script_string
)
327 for (i
= 0; i
< TA_STYLE_MAX
; i
++)
329 TA_StyleClass style_class
= ta_style_classes
[i
];
332 if (style_class
->coverage
== TA_COVERAGE_DEFAULT
333 && !strcmp(script_names
[style_class
->script
],
334 fallback_script_string
))
337 if (i
== TA_STYLE_MAX
)
339 error
= FT_Err_Invalid_Argument
;
343 fallback_style
= (TA_Style
)i
;
346 if (default_script_string
)
351 for (i
= 0; i
< TA_SCRIPT_MAX
; i
++)
353 if (!strcmp(script_names
[i
], default_script_string
))
356 if (i
== TA_SCRIPT_MAX
)
358 error
= FT_Err_Invalid_Argument
;
362 default_script
= (TA_Script
)i
;
365 if (x_height_snapping_exceptions_string
)
367 const char* s
= number_set_parse(x_height_snapping_exceptions_string
,
368 &x_height_snapping_exceptions
,
369 TA_PROP_INCREASE_X_HEIGHT_MIN
,
373 /* we map numberset.h's error codes to values starting with 0x100 */
374 error
= 0x100 - (FT_Error
)x_height_snapping_exceptions
;
376 errline
= (char*)x_height_snapping_exceptions_string
;
383 font
->hinting_range_min
= (FT_UInt
)hinting_range_min
;
384 font
->hinting_range_max
= (FT_UInt
)hinting_range_max
;
385 font
->hinting_limit
= (FT_UInt
)hinting_limit
;
386 font
->increase_x_height
= increase_x_height
;
387 font
->x_height_snapping_exceptions
= x_height_snapping_exceptions
;
388 font
->fallback_stem_width
= (FT_UInt
)fallback_stem_width
;
390 font
->gray_strong_stem_width
= gray_strong_stem_width
;
391 font
->gdi_cleartype_strong_stem_width
= gdi_cleartype_strong_stem_width
;
392 font
->dw_cleartype_strong_stem_width
= dw_cleartype_strong_stem_width
;
394 font
->windows_compatibility
= windows_compatibility
;
395 font
->ignore_restrictions
= ignore_restrictions
;
396 font
->adjust_subglyphs
= adjust_subglyphs
;
397 font
->hint_composites
= hint_composites
;
398 font
->fallback_style
= fallback_style
;
399 font
->default_script
= default_script
;
400 font
->symbol
= symbol
;
403 font
->progress
= progress
;
404 font
->progress_data
= progress_data
;
406 font
->info_data
= info_data
;
409 font
->dehint
= dehint
;
410 font
->TTFA_info
= TTFA_info
;
412 font
->gasp_idx
= MISSING
;
414 /* start with processing the data */
418 error
= TA_font_file_read(font
, in_file
);
424 /* a valid TTF can never be that small */
427 error
= TA_Err_Invalid_Font_Type
;
430 font
->in_buf
= (FT_Byte
*)in_buf
;
431 font
->in_len
= in_len
;
436 error
= TA_control_file_read(font
, control_file
);
440 else if (control_buf
)
442 font
->control_buf
= (char*)control_buf
;
443 font
->control_len
= control_len
;
446 error
= TA_font_init(font
);
453 _ta_debug_global
= 1;
456 /* we do some loops over all subfonts -- */
457 /* to process options early, just start with loading all of them */
458 for (i
= 0; i
< font
->num_sfnts
; i
++)
460 SFNT
* sfnt
= &font
->sfnts
[i
];
464 error
= FT_New_Memory_Face(font
->lib
, font
->in_buf
, font
->in_len
,
467 /* assure that the font hasn't been already processed by ttfautohint; */
468 /* another, more thorough check is done in TA_glyph_parse_simple */
469 idx
= FT_Get_Name_Index(sfnt
->face
, (FT_String
*)TTFAUTOHINT_GLYPH
);
472 error
= TA_Err_Already_Processed
;
480 /* process control instructions */
481 error
= TA_control_parse_buffer(font
,
483 &errlinenum
, &errline
, &errpos
);
487 free_error_string
= 1;
491 /* now we are able to dump all parameters */
497 s
= TA_font_dump_parameters(font
, 1);
500 error
= FT_Err_Out_Of_Memory
;
504 fprintf(stderr
, "%s", s
);
508 error
= TA_control_build_tree(font
);
512 /* loop again over subfonts and continue processing */
513 for (i
= 0; i
< font
->num_sfnts
; i
++)
515 SFNT
* sfnt
= &font
->sfnts
[i
];
518 error
= TA_sfnt_split_into_SFNT_tables(sfnt
, font
);
522 /* check permission */
523 if (sfnt
->OS2_idx
!= MISSING
)
525 SFNT_Table
* OS2_table
= &font
->tables
[sfnt
->OS2_idx
];
528 /* check lower byte of the `fsType' field */
529 if (OS2_table
->buf
[OS2_FSTYPE_OFFSET
+ 1] == 0x02
530 && !font
->ignore_restrictions
)
532 error
= TA_Err_Missing_Legal_Permission
;
539 error
= TA_sfnt_split_glyf_table(sfnt
, font
);
545 if (font
->adjust_subglyphs
)
546 error
= TA_sfnt_create_glyf_data(sfnt
, font
);
548 error
= TA_sfnt_split_glyf_table(sfnt
, font
);
552 /* this call creates a `globals' object... */
553 error
= TA_sfnt_handle_coverage(sfnt
, font
);
557 /* ... so that we now can initialize its properties */
558 TA_sfnt_set_properties(sfnt
, font
);
564 for (i
= 0; i
< font
->num_sfnts
; i
++)
566 SFNT
* sfnt
= &font
->sfnts
[i
];
569 TA_sfnt_adjust_coverage(sfnt
, font
);
574 /* this code is here for completeness -- */
575 /* right now, `glyf' tables get hinted only once, */
576 /* and referring subfonts simply reuse it, */
577 /* but this might change in the future */
581 for (i
= 0; i
< font
->num_sfnts
; i
++)
583 SFNT
* sfnt
= &font
->sfnts
[i
];
586 TA_sfnt_copy_master_coverage(sfnt
, font
);
591 /* loop again over subfonts */
592 for (i
= 0; i
< font
->num_sfnts
; i
++)
594 SFNT
* sfnt
= &font
->sfnts
[i
];
597 error
= ta_loader_init(font
);
601 error
= TA_sfnt_build_gasp_table(sfnt
, font
);
606 error
= TA_sfnt_build_cvt_table(sfnt
, font
);
609 error
= TA_sfnt_build_fpgm_table(sfnt
, font
);
612 error
= TA_sfnt_build_prep_table(sfnt
, font
);
616 error
= TA_sfnt_build_glyf_table(sfnt
, font
);
619 error
= TA_sfnt_build_loca_table(sfnt
, font
);
624 ta_loader_done(font
);
627 for (i
= 0; i
< font
->num_sfnts
; i
++)
629 SFNT
* sfnt
= &font
->sfnts
[i
];
632 error
= TA_sfnt_update_maxp_table(sfnt
, font
);
638 /* we add one glyph for composites */
639 if (sfnt
->max_components
640 && !font
->adjust_subglyphs
641 && font
->hint_composites
)
643 error
= TA_sfnt_update_hmtx_table(sfnt
, font
);
646 error
= TA_sfnt_update_post_table(sfnt
, font
);
649 error
= TA_sfnt_update_GPOS_table(sfnt
, font
);
657 /* add info about ttfautohint to the version string */
658 error
= TA_sfnt_update_name_table(sfnt
, font
);
664 if (font
->num_sfnts
== 1)
665 error
= TA_font_build_TTF(font
);
667 error
= TA_font_build_TTC(font
);
673 error
= TA_font_file_write(font
, out_file
);
679 *out_bufp
= (char*)font
->out_buf
;
680 *out_lenp
= font
->out_len
;
686 TA_control_free(font
->control
);
687 TA_control_free_tree(font
);
688 TA_font_unload(font
, in_buf
, out_bufp
, control_buf
);
692 error_string
= (char*)TA_get_error_message(error
);
694 /* this must be a static value */
696 *error_stringp
= (const unsigned char*)TA_get_error_message(error
);
708 if (free_error_string
)
714 /* end of ttfautohint.c */