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))
29 #define DUMPVAL(str, arg) \
30 fprintf(stderr, "%33s = %ld\n", \
33 #define DUMPSTR(str, arg) \
34 fprintf(stderr, "%33s = %s\n", \
40 TA_sfnt_set_properties(SFNT
* sfnt
,
43 TA_FaceGlobals globals
= (TA_FaceGlobals
)sfnt
->face
->autohint
.data
;
46 globals
->increase_x_height
= font
->increase_x_height
;
51 TTF_autohint(const char* options
,
61 FILE* out_file
= NULL
;
63 const char* in_buf
= NULL
;
65 char** out_bufp
= NULL
;
66 size_t* out_lenp
= NULL
;
68 const unsigned char** error_stringp
= NULL
;
70 FT_Long hinting_range_min
= -1;
71 FT_Long hinting_range_max
= -1;
72 FT_Long hinting_limit
= -1;
73 FT_Long increase_x_height
= -1;
75 const char* x_height_snapping_exceptions_string
= NULL
;
76 number_range
* x_height_snapping_exceptions
= NULL
;
78 FT_Long fallback_stem_width
= 0;
80 FT_Bool gray_strong_stem_width
= 0;
81 FT_Bool gdi_cleartype_strong_stem_width
= 1;
82 FT_Bool dw_cleartype_strong_stem_width
= 0;
84 TA_Progress_Func progress
;
89 FT_Bool windows_compatibility
= 0;
90 FT_Bool ignore_restrictions
= 0;
91 FT_Bool pre_hinting
= 0;
92 FT_Bool hint_composites
= 0;
95 const char* fallback_script_string
= NULL
;
96 const char* default_script_string
= NULL
;
97 TA_Style fallback_style
= TA_STYLE_NONE_DFLT
;
98 TA_Script default_script
= TA_SCRIPT_LATN
;
107 #define SCRIPT(s, S, d, h, sc1, sc2, sc3) #s,
109 const char* script_names
[] =
112 #include <ttfautohint-scripts.h>
117 if (!options
|| !*options
)
119 error
= FT_Err_Invalid_Argument
;
124 va_start(ap
, options
);
137 while (*op
&& *op
!= ',')
140 /* remove leading whitespace */
141 while (isspace(*start
))
144 /* check for empty option */
150 /* the `COMPARE' macro uses `len' and `start' */
152 /* handle options -- don't forget to update parameter dump below! */
153 if (COMPARE("debug"))
154 debug
= (FT_Bool
)va_arg(ap
, FT_Int
);
155 else if (COMPARE("default-script"))
156 default_script_string
= va_arg(ap
, const char*);
157 else if (COMPARE("dehint"))
158 dehint
= (FT_Bool
)va_arg(ap
, FT_Int
);
159 else if (COMPARE("dw-cleartype-strong-stem-width"))
160 dw_cleartype_strong_stem_width
= (FT_Bool
)va_arg(ap
, FT_Int
);
161 else if (COMPARE("error-string"))
162 error_stringp
= va_arg(ap
, const unsigned char**);
163 else if (COMPARE("fallback-script"))
164 fallback_script_string
= va_arg(ap
, const char*);
165 else if (COMPARE("fallback-stem-width"))
166 fallback_stem_width
= (FT_Long
)va_arg(ap
, FT_UInt
);
167 else if (COMPARE("gdi-cleartype-strong-stem-width"))
168 gdi_cleartype_strong_stem_width
= (FT_Bool
)va_arg(ap
, FT_Int
);
169 else if (COMPARE("gray-strong-stem-width"))
170 gray_strong_stem_width
= (FT_Bool
)va_arg(ap
, FT_Int
);
171 else if (COMPARE("hinting-limit"))
172 hinting_limit
= (FT_Long
)va_arg(ap
, FT_UInt
);
173 else if (COMPARE("hinting-range-max"))
174 hinting_range_max
= (FT_Long
)va_arg(ap
, FT_UInt
);
175 else if (COMPARE("hinting-range-min"))
176 hinting_range_min
= (FT_Long
)va_arg(ap
, FT_UInt
);
177 else if (COMPARE("hint-composites"))
178 hint_composites
= (FT_Bool
)va_arg(ap
, FT_Int
);
179 else if (COMPARE("ignore-restrictions"))
180 ignore_restrictions
= (FT_Bool
)va_arg(ap
, FT_Int
);
181 else if (COMPARE("in-buffer"))
184 in_buf
= va_arg(ap
, const char*);
186 else if (COMPARE("in-buffer-len"))
189 in_len
= va_arg(ap
, size_t);
191 else if (COMPARE("in-file"))
193 in_file
= va_arg(ap
, FILE*);
197 else if (COMPARE("increase-x-height"))
198 increase_x_height
= (FT_Long
)va_arg(ap
, FT_UInt
);
199 else if (COMPARE("info-callback"))
200 info
= va_arg(ap
, TA_Info_Func
);
201 else if (COMPARE("info-callback-data"))
202 info_data
= va_arg(ap
, void*);
203 else if (COMPARE("out-buffer"))
206 out_bufp
= va_arg(ap
, char**);
208 else if (COMPARE("out-buffer-len"))
211 out_lenp
= va_arg(ap
, size_t*);
213 else if (COMPARE("out-file"))
215 out_file
= va_arg(ap
, FILE*);
219 else if (COMPARE("pre-hinting"))
220 pre_hinting
= (FT_Bool
)va_arg(ap
, FT_Int
);
221 else if (COMPARE("progress-callback"))
222 progress
= va_arg(ap
, TA_Progress_Func
);
223 else if (COMPARE("progress-callback-data"))
224 progress_data
= va_arg(ap
, void*);
225 else if (COMPARE("symbol"))
226 symbol
= (FT_Bool
)va_arg(ap
, FT_Int
);
227 else if (COMPARE("windows-compatibility"))
228 windows_compatibility
= (FT_Bool
)va_arg(ap
, FT_Int
);
229 else if (COMPARE("x-height-snapping-exceptions"))
230 x_height_snapping_exceptions_string
= va_arg(ap
, const char*);
233 error
= TA_Err_Unknown_Argument
;
248 || (in_buf
&& in_len
)))
250 error
= FT_Err_Invalid_Argument
;
255 || (out_bufp
&& out_lenp
)))
257 error
= FT_Err_Invalid_Argument
;
261 font
= (FONT
*)calloc(1, sizeof (FONT
));
264 error
= FT_Err_Out_Of_Memory
;
271 if (hinting_range_min
>= 0 && hinting_range_min
< 2)
273 error
= FT_Err_Invalid_Argument
;
276 if (hinting_range_min
< 0)
277 hinting_range_min
= TA_HINTING_RANGE_MIN
;
279 if (hinting_range_max
>= 0 && hinting_range_max
< hinting_range_min
)
281 error
= FT_Err_Invalid_Argument
;
284 if (hinting_range_max
< 0)
285 hinting_range_max
= TA_HINTING_RANGE_MAX
;
287 /* value 0 is valid */
288 if (hinting_limit
> 0 && hinting_limit
< hinting_range_max
)
290 error
= FT_Err_Invalid_Argument
;
293 if (hinting_limit
< 0)
294 hinting_limit
= TA_HINTING_LIMIT
;
296 if (increase_x_height
> 0
297 && increase_x_height
< TA_PROP_INCREASE_X_HEIGHT_MIN
)
299 error
= FT_Err_Invalid_Argument
;
302 if (increase_x_height
< 0)
303 increase_x_height
= TA_INCREASE_X_HEIGHT
;
305 if (fallback_script_string
)
310 for (i
= 0; i
< TA_STYLE_MAX
; i
++)
312 TA_StyleClass style_class
= ta_style_classes
[i
];
315 if (style_class
->coverage
== TA_COVERAGE_DEFAULT
316 && !strcmp(script_names
[style_class
->script
],
317 fallback_script_string
))
320 if (i
== TA_STYLE_MAX
)
322 error
= FT_Err_Invalid_Argument
;
326 fallback_style
= (TA_Style
)i
;
329 if (default_script_string
)
334 for (i
= 0; i
< TA_SCRIPT_MAX
; i
++)
336 if (!strcmp(script_names
[i
], default_script_string
))
339 if (i
== TA_SCRIPT_MAX
)
341 error
= FT_Err_Invalid_Argument
;
345 default_script
= (TA_Script
)i
;
348 if (x_height_snapping_exceptions_string
)
350 const char* s
= number_set_parse(x_height_snapping_exceptions_string
,
351 &x_height_snapping_exceptions
,
352 TA_PROP_INCREASE_X_HEIGHT_MIN
,
356 error
= FT_Err_Invalid_Argument
;
361 font
->hinting_range_min
= (FT_UInt
)hinting_range_min
;
362 font
->hinting_range_max
= (FT_UInt
)hinting_range_max
;
363 font
->hinting_limit
= (FT_UInt
)hinting_limit
;
364 font
->increase_x_height
= increase_x_height
;
365 font
->x_height_snapping_exceptions
= x_height_snapping_exceptions
;
366 font
->fallback_stem_width
= (FT_UInt
)fallback_stem_width
;
368 font
->gray_strong_stem_width
= gray_strong_stem_width
;
369 font
->gdi_cleartype_strong_stem_width
= gdi_cleartype_strong_stem_width
;
370 font
->dw_cleartype_strong_stem_width
= dw_cleartype_strong_stem_width
;
372 font
->windows_compatibility
= windows_compatibility
;
373 font
->ignore_restrictions
= ignore_restrictions
;
374 font
->pre_hinting
= pre_hinting
;
375 font
->hint_composites
= hint_composites
;
376 font
->fallback_style
= fallback_style
;
377 font
->default_script
= default_script
;
378 font
->symbol
= symbol
;
381 font
->progress
= progress
;
382 font
->progress_data
= progress_data
;
384 font
->info_data
= info_data
;
387 font
->dehint
= dehint
;
389 font
->gasp_idx
= MISSING
;
391 /* dump parameters */
394 fprintf(stderr
, "TTF_autohint parameters\n"
395 "=======================\n"
406 DUMPSTR("default-script",
407 script_names
[font
->default_script
]);
408 DUMPVAL("dw-cleartype-strong-stem-width",
409 font
->dw_cleartype_strong_stem_width
);
410 DUMPSTR("fallback-script",
411 script_names
[ta_style_classes
[font
->fallback_style
]->script
]);
412 DUMPVAL("fallback-stem-width",
413 font
->fallback_stem_width
);
414 DUMPVAL("gdi-cleartype-strong-stem-width",
415 font
->gdi_cleartype_strong_stem_width
);
416 DUMPVAL("gray-strong-stem-width",
417 font
->gray_strong_stem_width
);
418 DUMPVAL("hinting-limit",
419 font
->hinting_limit
);
420 DUMPVAL("hinting-range-max",
421 font
->hinting_range_max
);
422 DUMPVAL("hinting-range-min",
423 font
->hinting_range_min
);
424 DUMPVAL("hint-composites",
425 font
->hint_composites
);
426 DUMPVAL("ignore-restrictions",
427 font
->ignore_restrictions
);
428 DUMPVAL("increase-x-height",
429 font
->increase_x_height
);
430 DUMPVAL("pre-hinting",
434 DUMPVAL("windows-compatibility",
435 font
->windows_compatibility
);
437 s
= number_set_show(font
->x_height_snapping_exceptions
,
438 TA_PROP_INCREASE_X_HEIGHT_MIN
, 0x7FFF);
439 DUMPSTR("x-height-snapping-exceptions", s
);
443 fprintf(stderr
, "\n");
446 /* now start with processing the data */
450 error
= TA_font_file_read(font
, in_file
);
456 /* a valid TTF can never be that small */
459 error
= TA_Err_Invalid_Font_Type
;
462 font
->in_buf
= (FT_Byte
*)in_buf
;
463 font
->in_len
= in_len
;
466 error
= TA_font_init(font
);
473 _ta_debug_global
= 1;
476 /* we do some loops over all subfonts */
477 for (i
= 0; i
< font
->num_sfnts
; i
++)
479 SFNT
* sfnt
= &font
->sfnts
[i
];
483 error
= FT_New_Memory_Face(font
->lib
, font
->in_buf
, font
->in_len
,
486 /* assure that the font hasn't been already processed by ttfautohint; */
487 /* another, more thorough check is done in TA_glyph_parse_simple */
488 idx
= FT_Get_Name_Index(sfnt
->face
, (FT_String
*)TTFAUTOHINT_GLYPH
);
491 error
= TA_Err_Already_Processed
;
498 error
= TA_sfnt_split_into_SFNT_tables(sfnt
, font
);
502 /* check permission */
503 if (sfnt
->OS2_idx
!= MISSING
)
505 SFNT_Table
* OS2_table
= &font
->tables
[sfnt
->OS2_idx
];
508 /* check lower byte of the `fsType' field */
509 if (OS2_table
->buf
[OS2_FSTYPE_OFFSET
+ 1] == 0x02
510 && !font
->ignore_restrictions
)
512 error
= TA_Err_Missing_Legal_Permission
;
519 error
= TA_sfnt_split_glyf_table(sfnt
, font
);
525 if (font
->pre_hinting
)
526 error
= TA_sfnt_create_glyf_data(sfnt
, font
);
528 error
= TA_sfnt_split_glyf_table(sfnt
, font
);
532 /* this call creates a `globals' object... */
533 error
= TA_sfnt_handle_coverage(sfnt
, font
);
537 /* ... so that we now can initialize its properties */
538 TA_sfnt_set_properties(sfnt
, font
);
544 for (i
= 0; i
< font
->num_sfnts
; i
++)
546 SFNT
* sfnt
= &font
->sfnts
[i
];
549 if (TA_sfnt_adjust_master_coverage(sfnt
, font
))
555 /* this code is here for completeness -- */
556 /* right now, `glyf' tables get hinted only once, */
557 /* and referring subfonts simply reuse it, */
558 /* but this might change in the future */
562 for (i
= 0; i
< font
->num_sfnts
; i
++)
564 SFNT
* sfnt
= &font
->sfnts
[i
];
567 TA_sfnt_copy_master_coverage(sfnt
, font
);
572 /* loop again over subfonts */
573 for (i
= 0; i
< font
->num_sfnts
; i
++)
575 SFNT
* sfnt
= &font
->sfnts
[i
];
578 error
= ta_loader_init(font
);
582 error
= TA_sfnt_build_gasp_table(sfnt
, font
);
587 error
= TA_sfnt_build_cvt_table(sfnt
, font
);
590 error
= TA_sfnt_build_fpgm_table(sfnt
, font
);
593 error
= TA_sfnt_build_prep_table(sfnt
, font
);
597 error
= TA_sfnt_build_glyf_table(sfnt
, font
);
600 error
= TA_sfnt_build_loca_table(sfnt
, font
);
605 ta_loader_done(font
);
608 for (i
= 0; i
< font
->num_sfnts
; i
++)
610 SFNT
* sfnt
= &font
->sfnts
[i
];
613 error
= TA_sfnt_update_maxp_table(sfnt
, font
);
619 /* we add one glyph for composites */
620 if (sfnt
->max_components
621 && !font
->pre_hinting
622 && font
->hint_composites
)
624 error
= TA_sfnt_update_hmtx_table(sfnt
, font
);
627 error
= TA_sfnt_update_post_table(sfnt
, font
);
630 error
= TA_sfnt_update_GPOS_table(sfnt
, font
);
638 /* add info about ttfautohint to the version string */
639 error
= TA_sfnt_update_name_table(sfnt
, font
);
645 if (font
->num_sfnts
== 1)
646 error
= TA_font_build_TTF(font
);
648 error
= TA_font_build_TTC(font
);
654 error
= TA_font_file_write(font
, out_file
);
660 *out_bufp
= (char*)font
->out_buf
;
661 *out_lenp
= font
->out_len
;
667 TA_font_unload(font
, in_buf
, out_bufp
);
671 *error_stringp
= (const unsigned char*)TA_get_error_message(error
);
676 /* end of ttfautohint.c */