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
,
60 const char* error_string
= NULL
;
61 unsigned int linenum
= 0;
62 const char* line
= NULL
;
63 const char* errpos
= NULL
;
66 FILE* out_file
= NULL
;
68 const char* in_buf
= NULL
;
70 char** out_bufp
= NULL
;
71 size_t* out_lenp
= NULL
;
73 const unsigned char** error_stringp
= NULL
;
75 FT_Long hinting_range_min
= -1;
76 FT_Long hinting_range_max
= -1;
77 FT_Long hinting_limit
= -1;
78 FT_Long increase_x_height
= -1;
80 const char* x_height_snapping_exceptions_string
= NULL
;
81 number_range
* x_height_snapping_exceptions
= NULL
;
83 FT_Long fallback_stem_width
= 0;
85 FT_Bool gray_strong_stem_width
= 0;
86 FT_Bool gdi_cleartype_strong_stem_width
= 1;
87 FT_Bool dw_cleartype_strong_stem_width
= 0;
89 TA_Progress_Func progress
= NULL
;
90 void* progress_data
= NULL
;
91 TA_Error_Func err
= NULL
;
92 void* err_data
= NULL
;
93 TA_Info_Func info
= NULL
;
94 void* info_data
= NULL
;
96 FT_Bool windows_compatibility
= 0;
97 FT_Bool ignore_restrictions
= 0;
98 FT_Bool adjust_subglyphs
= 0;
99 FT_Bool hint_composites
= 0;
102 const char* fallback_script_string
= NULL
;
103 const char* default_script_string
= NULL
;
104 TA_Style fallback_style
= TA_STYLE_NONE_DFLT
;
105 TA_Script default_script
= TA_SCRIPT_LATN
;
113 #define SCRIPT(s, S, d, h, sc1, sc2, sc3) #s,
115 const char* script_names
[] =
118 #include <ttfautohint-scripts.h>
123 if (!options
|| !*options
)
125 error
= FT_Err_Invalid_Argument
;
130 va_start(ap
, options
);
143 while (*op
&& *op
!= ',')
146 /* remove leading whitespace */
147 while (isspace(*start
))
150 /* check for empty option */
156 /* the `COMPARE' macro uses `len' and `start' */
158 /* handle options -- don't forget to update parameter dump below! */
159 if (COMPARE("adjust-subglyphs"))
160 adjust_subglyphs
= (FT_Bool
)va_arg(ap
, FT_Int
);
161 else if (COMPARE("debug"))
162 debug
= (FT_Bool
)va_arg(ap
, FT_Int
);
163 else if (COMPARE("default-script"))
164 default_script_string
= va_arg(ap
, const char*);
165 else if (COMPARE("dehint"))
166 dehint
= (FT_Bool
)va_arg(ap
, FT_Int
);
167 else if (COMPARE("dw-cleartype-strong-stem-width"))
168 dw_cleartype_strong_stem_width
= (FT_Bool
)va_arg(ap
, FT_Int
);
169 else if (COMPARE("error-callback"))
170 err
= va_arg(ap
, TA_Error_Func
);
171 else if (COMPARE("error-callback-data"))
172 err_data
= va_arg(ap
, void*);
173 else if (COMPARE("error-string"))
174 error_stringp
= va_arg(ap
, const unsigned char**);
175 else if (COMPARE("fallback-script"))
176 fallback_script_string
= va_arg(ap
, const char*);
177 else if (COMPARE("fallback-stem-width"))
178 fallback_stem_width
= (FT_Long
)va_arg(ap
, FT_UInt
);
179 else if (COMPARE("gdi-cleartype-strong-stem-width"))
180 gdi_cleartype_strong_stem_width
= (FT_Bool
)va_arg(ap
, FT_Int
);
181 else if (COMPARE("gray-strong-stem-width"))
182 gray_strong_stem_width
= (FT_Bool
)va_arg(ap
, FT_Int
);
183 else if (COMPARE("hinting-limit"))
184 hinting_limit
= (FT_Long
)va_arg(ap
, FT_UInt
);
185 else if (COMPARE("hinting-range-max"))
186 hinting_range_max
= (FT_Long
)va_arg(ap
, FT_UInt
);
187 else if (COMPARE("hinting-range-min"))
188 hinting_range_min
= (FT_Long
)va_arg(ap
, FT_UInt
);
189 else if (COMPARE("hint-composites"))
190 hint_composites
= (FT_Bool
)va_arg(ap
, FT_Int
);
191 else if (COMPARE("ignore-restrictions"))
192 ignore_restrictions
= (FT_Bool
)va_arg(ap
, FT_Int
);
193 else if (COMPARE("in-buffer"))
196 in_buf
= va_arg(ap
, const char*);
198 else if (COMPARE("in-buffer-len"))
201 in_len
= va_arg(ap
, size_t);
203 else if (COMPARE("in-file"))
205 in_file
= va_arg(ap
, FILE*);
209 else if (COMPARE("increase-x-height"))
210 increase_x_height
= (FT_Long
)va_arg(ap
, FT_UInt
);
211 else if (COMPARE("info-callback"))
212 info
= va_arg(ap
, TA_Info_Func
);
213 else if (COMPARE("info-callback-data"))
214 info_data
= va_arg(ap
, void*);
215 else if (COMPARE("out-buffer"))
218 out_bufp
= va_arg(ap
, char**);
220 else if (COMPARE("out-buffer-len"))
223 out_lenp
= va_arg(ap
, size_t*);
225 else if (COMPARE("out-file"))
227 out_file
= va_arg(ap
, FILE*);
231 else if (COMPARE("pre-hinting"))
232 adjust_subglyphs
= (FT_Bool
)va_arg(ap
, FT_Int
);
233 else if (COMPARE("progress-callback"))
234 progress
= va_arg(ap
, TA_Progress_Func
);
235 else if (COMPARE("progress-callback-data"))
236 progress_data
= va_arg(ap
, void*);
237 else if (COMPARE("symbol"))
238 symbol
= (FT_Bool
)va_arg(ap
, FT_Int
);
239 else if (COMPARE("windows-compatibility"))
240 windows_compatibility
= (FT_Bool
)va_arg(ap
, FT_Int
);
241 else if (COMPARE("x-height-snapping-exceptions"))
242 x_height_snapping_exceptions_string
= va_arg(ap
, const char*);
245 error
= TA_Err_Unknown_Argument
;
260 || (in_buf
&& in_len
)))
262 error
= FT_Err_Invalid_Argument
;
267 || (out_bufp
&& out_lenp
)))
269 error
= FT_Err_Invalid_Argument
;
273 font
= (FONT
*)calloc(1, sizeof (FONT
));
276 error
= FT_Err_Out_Of_Memory
;
283 if (hinting_range_min
>= 0 && hinting_range_min
< 2)
285 error
= FT_Err_Invalid_Argument
;
288 if (hinting_range_min
< 0)
289 hinting_range_min
= TA_HINTING_RANGE_MIN
;
291 if (hinting_range_max
>= 0 && hinting_range_max
< hinting_range_min
)
293 error
= FT_Err_Invalid_Argument
;
296 if (hinting_range_max
< 0)
297 hinting_range_max
= TA_HINTING_RANGE_MAX
;
299 /* value 0 is valid */
300 if (hinting_limit
> 0 && hinting_limit
< hinting_range_max
)
302 error
= FT_Err_Invalid_Argument
;
305 if (hinting_limit
< 0)
306 hinting_limit
= TA_HINTING_LIMIT
;
308 if (increase_x_height
> 0
309 && increase_x_height
< TA_PROP_INCREASE_X_HEIGHT_MIN
)
311 error
= FT_Err_Invalid_Argument
;
314 if (increase_x_height
< 0)
315 increase_x_height
= TA_INCREASE_X_HEIGHT
;
317 if (fallback_script_string
)
322 for (i
= 0; i
< TA_STYLE_MAX
; i
++)
324 TA_StyleClass style_class
= ta_style_classes
[i
];
327 if (style_class
->coverage
== TA_COVERAGE_DEFAULT
328 && !strcmp(script_names
[style_class
->script
],
329 fallback_script_string
))
332 if (i
== TA_STYLE_MAX
)
334 error
= FT_Err_Invalid_Argument
;
338 fallback_style
= (TA_Style
)i
;
341 if (default_script_string
)
346 for (i
= 0; i
< TA_SCRIPT_MAX
; i
++)
348 if (!strcmp(script_names
[i
], default_script_string
))
351 if (i
== TA_SCRIPT_MAX
)
353 error
= FT_Err_Invalid_Argument
;
357 default_script
= (TA_Script
)i
;
360 if (x_height_snapping_exceptions_string
)
362 const char* s
= number_set_parse(x_height_snapping_exceptions_string
,
363 &x_height_snapping_exceptions
,
364 TA_PROP_INCREASE_X_HEIGHT_MIN
,
368 /* we map numberset.h's error codes to values starting with 0x100 */
369 error
= 0x100 - (FT_Error
)x_height_snapping_exceptions
;
370 line
= x_height_snapping_exceptions_string
;
378 font
->hinting_range_min
= (FT_UInt
)hinting_range_min
;
379 font
->hinting_range_max
= (FT_UInt
)hinting_range_max
;
380 font
->hinting_limit
= (FT_UInt
)hinting_limit
;
381 font
->increase_x_height
= increase_x_height
;
382 font
->x_height_snapping_exceptions
= x_height_snapping_exceptions
;
383 font
->fallback_stem_width
= (FT_UInt
)fallback_stem_width
;
385 font
->gray_strong_stem_width
= gray_strong_stem_width
;
386 font
->gdi_cleartype_strong_stem_width
= gdi_cleartype_strong_stem_width
;
387 font
->dw_cleartype_strong_stem_width
= dw_cleartype_strong_stem_width
;
389 font
->windows_compatibility
= windows_compatibility
;
390 font
->ignore_restrictions
= ignore_restrictions
;
391 font
->adjust_subglyphs
= adjust_subglyphs
;
392 font
->hint_composites
= hint_composites
;
393 font
->fallback_style
= fallback_style
;
394 font
->default_script
= default_script
;
395 font
->symbol
= symbol
;
398 font
->progress
= progress
;
399 font
->progress_data
= progress_data
;
401 font
->info_data
= info_data
;
404 font
->dehint
= dehint
;
406 font
->gasp_idx
= MISSING
;
408 /* dump parameters */
411 fprintf(stderr
, "TTF_autohint parameters\n"
412 "=======================\n"
423 DUMPSTR("default-script",
424 script_names
[font
->default_script
]);
425 DUMPVAL("dw-cleartype-strong-stem-width",
426 font
->dw_cleartype_strong_stem_width
);
427 DUMPSTR("fallback-script",
428 script_names
[ta_style_classes
[font
->fallback_style
]->script
]);
429 DUMPVAL("fallback-stem-width",
430 font
->fallback_stem_width
);
431 DUMPVAL("gdi-cleartype-strong-stem-width",
432 font
->gdi_cleartype_strong_stem_width
);
433 DUMPVAL("gray-strong-stem-width",
434 font
->gray_strong_stem_width
);
435 DUMPVAL("hinting-limit",
436 font
->hinting_limit
);
437 DUMPVAL("hinting-range-max",
438 font
->hinting_range_max
);
439 DUMPVAL("hinting-range-min",
440 font
->hinting_range_min
);
441 DUMPVAL("hint-composites",
442 font
->hint_composites
);
443 DUMPVAL("ignore-restrictions",
444 font
->ignore_restrictions
);
445 DUMPVAL("increase-x-height",
446 font
->increase_x_height
);
447 DUMPVAL("adjust-subglyphs",
448 font
->adjust_subglyphs
);
451 DUMPVAL("windows-compatibility",
452 font
->windows_compatibility
);
454 s
= number_set_show(font
->x_height_snapping_exceptions
,
455 TA_PROP_INCREASE_X_HEIGHT_MIN
, 0x7FFF);
456 DUMPSTR("x-height-snapping-exceptions", s
);
460 fprintf(stderr
, "\n");
463 /* now start with processing the data */
467 error
= TA_font_file_read(font
, in_file
);
473 /* a valid TTF can never be that small */
476 error
= TA_Err_Invalid_Font_Type
;
479 font
->in_buf
= (FT_Byte
*)in_buf
;
480 font
->in_len
= in_len
;
483 error
= TA_font_init(font
);
490 _ta_debug_global
= 1;
493 /* we do some loops over all subfonts */
494 for (i
= 0; i
< font
->num_sfnts
; i
++)
496 SFNT
* sfnt
= &font
->sfnts
[i
];
500 error
= FT_New_Memory_Face(font
->lib
, font
->in_buf
, font
->in_len
,
503 /* assure that the font hasn't been already processed by ttfautohint; */
504 /* another, more thorough check is done in TA_glyph_parse_simple */
505 idx
= FT_Get_Name_Index(sfnt
->face
, (FT_String
*)TTFAUTOHINT_GLYPH
);
508 error
= TA_Err_Already_Processed
;
515 error
= TA_sfnt_split_into_SFNT_tables(sfnt
, font
);
519 /* check permission */
520 if (sfnt
->OS2_idx
!= MISSING
)
522 SFNT_Table
* OS2_table
= &font
->tables
[sfnt
->OS2_idx
];
525 /* check lower byte of the `fsType' field */
526 if (OS2_table
->buf
[OS2_FSTYPE_OFFSET
+ 1] == 0x02
527 && !font
->ignore_restrictions
)
529 error
= TA_Err_Missing_Legal_Permission
;
536 error
= TA_sfnt_split_glyf_table(sfnt
, font
);
542 if (font
->adjust_subglyphs
)
543 error
= TA_sfnt_create_glyf_data(sfnt
, font
);
545 error
= TA_sfnt_split_glyf_table(sfnt
, font
);
549 /* this call creates a `globals' object... */
550 error
= TA_sfnt_handle_coverage(sfnt
, font
);
554 /* ... so that we now can initialize its properties */
555 TA_sfnt_set_properties(sfnt
, font
);
561 for (i
= 0; i
< font
->num_sfnts
; i
++)
563 SFNT
* sfnt
= &font
->sfnts
[i
];
566 TA_sfnt_adjust_coverage(sfnt
, font
);
571 /* this code is here for completeness -- */
572 /* right now, `glyf' tables get hinted only once, */
573 /* and referring subfonts simply reuse it, */
574 /* but this might change in the future */
578 for (i
= 0; i
< font
->num_sfnts
; i
++)
580 SFNT
* sfnt
= &font
->sfnts
[i
];
583 TA_sfnt_copy_master_coverage(sfnt
, font
);
588 /* loop again over subfonts */
589 for (i
= 0; i
< font
->num_sfnts
; i
++)
591 SFNT
* sfnt
= &font
->sfnts
[i
];
594 error
= ta_loader_init(font
);
598 error
= TA_sfnt_build_gasp_table(sfnt
, font
);
603 error
= TA_sfnt_build_cvt_table(sfnt
, font
);
606 error
= TA_sfnt_build_fpgm_table(sfnt
, font
);
609 error
= TA_sfnt_build_prep_table(sfnt
, font
);
613 error
= TA_sfnt_build_glyf_table(sfnt
, font
);
616 error
= TA_sfnt_build_loca_table(sfnt
, font
);
621 ta_loader_done(font
);
624 for (i
= 0; i
< font
->num_sfnts
; i
++)
626 SFNT
* sfnt
= &font
->sfnts
[i
];
629 error
= TA_sfnt_update_maxp_table(sfnt
, font
);
635 /* we add one glyph for composites */
636 if (sfnt
->max_components
637 && !font
->adjust_subglyphs
638 && font
->hint_composites
)
640 error
= TA_sfnt_update_hmtx_table(sfnt
, font
);
643 error
= TA_sfnt_update_post_table(sfnt
, font
);
646 error
= TA_sfnt_update_GPOS_table(sfnt
, font
);
654 /* add info about ttfautohint to the version string */
655 error
= TA_sfnt_update_name_table(sfnt
, font
);
661 if (font
->num_sfnts
== 1)
662 error
= TA_font_build_TTF(font
);
664 error
= TA_font_build_TTC(font
);
670 error
= TA_font_file_write(font
, out_file
);
676 *out_bufp
= (char*)font
->out_buf
;
677 *out_lenp
= font
->out_len
;
683 TA_font_unload(font
, in_buf
, out_bufp
);
686 error_string
= TA_get_error_message(error
);
689 *error_stringp
= (const unsigned char*)error_string
;
702 /* end of ttfautohint.c */