4 * Copyright (C) 2011-2012 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) (len == (sizeof (str) - 1) \
27 && !strncmp(start, str, sizeof (str) - 1))
31 TTF_autohint(const char* options
,
41 FILE* out_file
= NULL
;
43 const char* in_buf
= NULL
;
45 char** out_bufp
= NULL
;
46 size_t* out_lenp
= NULL
;
48 const unsigned char** error_stringp
= NULL
;
50 FT_Long hinting_range_min
= -1;
51 FT_Long hinting_range_max
= -1;
53 TA_Progress_Func progress
;
56 FT_Bool ignore_permissions
= 0;
57 FT_Bool pre_hinting
= 0;
58 FT_UInt fallback_script
= 0;
63 if (!options
|| !*options
)
65 error
= FT_Err_Invalid_Argument
;
70 va_start(ap
, options
);
83 while (*op
&& *op
!= ',')
86 /* remove leading whitespace */
87 while (isspace(*start
))
90 /* check for empty option */
96 /* the `COMPARE' macro uses `len' and `start' */
99 if (COMPARE("error-string"))
100 error_stringp
= va_arg(ap
, const unsigned char**);
101 else if (COMPARE("fallback-script"))
102 fallback_script
= va_arg(ap
, FT_UInt
);
103 else if (COMPARE("hinting-range-max"))
104 hinting_range_max
= (FT_Long
)va_arg(ap
, FT_UInt
);
105 else if (COMPARE("hinting-range-min"))
106 hinting_range_min
= (FT_Long
)va_arg(ap
, FT_UInt
);
107 else if (COMPARE("ignore-permissions"))
108 ignore_permissions
= (FT_Bool
)va_arg(ap
, FT_Int
);
109 else if (COMPARE("in-buffer"))
112 in_buf
= va_arg(ap
, const char*);
114 else if (COMPARE("in-buffer-len"))
117 in_len
= va_arg(ap
, size_t);
119 else if (COMPARE("in-file"))
121 in_file
= va_arg(ap
, FILE*);
125 else if (COMPARE("out-buffer"))
128 out_bufp
= va_arg(ap
, char**);
130 else if (COMPARE("out-buffer-len"))
133 out_lenp
= va_arg(ap
, size_t*);
135 else if (COMPARE("out-file"))
137 out_file
= va_arg(ap
, FILE*);
141 else if (COMPARE("pre-hinting"))
142 pre_hinting
= (FT_Bool
)va_arg(ap
, FT_Int
);
143 else if (COMPARE("progress-callback"))
144 progress
= va_arg(ap
, TA_Progress_Func
);
145 else if (COMPARE("progress-callback-data"))
146 progress_data
= va_arg(ap
, void*);
149 x-height-snapping-exceptions
163 || (in_buf
&& in_len
)))
165 error
= FT_Err_Invalid_Argument
;
170 || (out_bufp
&& out_lenp
)))
172 error
= FT_Err_Invalid_Argument
;
176 font
= (FONT
*)calloc(1, sizeof (FONT
));
179 error
= FT_Err_Out_Of_Memory
;
183 if (hinting_range_min
>= 0 && hinting_range_min
< 2)
185 error
= FT_Err_Invalid_Argument
;
188 if (hinting_range_min
< 0)
189 hinting_range_min
= 8;
191 if (hinting_range_max
>= 0 && hinting_range_max
< hinting_range_min
)
193 error
= FT_Err_Invalid_Argument
;
196 if (hinting_range_max
< 0)
197 hinting_range_max
= 1000;
199 font
->hinting_range_min
= (FT_UInt
)hinting_range_min
;
200 font
->hinting_range_max
= (FT_UInt
)hinting_range_max
;
202 font
->progress
= progress
;
203 font
->progress_data
= progress_data
;
205 font
->ignore_permissions
= ignore_permissions
;
206 font
->pre_hinting
= pre_hinting
;
207 /* restrict value to two bits */
208 font
->fallback_script
= fallback_script
& 3;
210 /* now start with processing the data */
214 error
= TA_font_file_read(font
, in_file
);
220 /* a valid TTF can never be that small */
223 error
= FT_Err_Invalid_Argument
;
226 font
->in_buf
= (FT_Byte
*)in_buf
;
227 font
->in_len
= in_len
;
230 error
= TA_font_init(font
);
234 /* loop over subfonts */
235 for (i
= 0; i
< font
->num_sfnts
; i
++)
237 SFNT
* sfnt
= &font
->sfnts
[i
];
240 error
= FT_New_Memory_Face(font
->lib
, font
->in_buf
, font
->in_len
,
245 error
= TA_sfnt_split_into_SFNT_tables(sfnt
, font
);
249 if (font
->pre_hinting
)
250 error
= TA_sfnt_create_glyf_data(sfnt
, font
);
252 error
= TA_sfnt_split_glyf_table(sfnt
, font
);
256 /* check permission */
257 if (sfnt
->OS2_idx
!= MISSING
)
259 SFNT_Table
* OS2_table
= &font
->tables
[sfnt
->OS2_idx
];
262 /* check lower byte of the `fsType' field */
263 if (OS2_table
->buf
[OS2_FSTYPE_OFFSET
+ 1] == 0x02
264 && !font
->ignore_permissions
)
266 error
= TA_Err_Missing_Legal_Permission
;
272 /* build `gasp' table */
273 error
= TA_sfnt_build_gasp_table(&font
->sfnts
[0], font
);
277 /* XXX handle subfonts for bytecode tables */
279 /* build `cvt ' table */
280 error
= TA_sfnt_build_cvt_table(&font
->sfnts
[0], font
);
284 /* build `fpgm' table */
285 error
= TA_sfnt_build_fpgm_table(&font
->sfnts
[0], font
);
289 /* build `prep' table */
290 error
= TA_sfnt_build_prep_table(&font
->sfnts
[0], font
);
294 /* hint the glyphs and build bytecode */
295 error
= TA_sfnt_build_glyf_hints(&font
->sfnts
[0], font
);
299 /* loop again over subfonts */
300 for (i
= 0; i
< font
->num_sfnts
; i
++)
302 SFNT
* sfnt
= &font
->sfnts
[i
];
305 error
= TA_sfnt_build_glyf_table(sfnt
, font
);
308 error
= TA_sfnt_build_loca_table(sfnt
, font
);
311 error
= TA_sfnt_update_maxp_table(sfnt
, font
);
315 /* we add one glyph for composites */
316 if (sfnt
->max_components
&& !font
->pre_hinting
)
318 error
= TA_sfnt_update_hmtx_table(sfnt
, font
);
321 error
= TA_sfnt_update_post_table(sfnt
, font
);
324 error
= TA_sfnt_update_GPOS_table(sfnt
, font
);
330 if (font
->num_sfnts
== 1)
331 error
= TA_font_build_TTF(font
);
333 error
= TA_font_build_TTC(font
);
339 error
= TA_font_file_write(font
, out_file
);
345 *out_bufp
= (char*)font
->out_buf
;
346 *out_lenp
= font
->out_len
;
352 TA_font_unload(font
, in_buf
, out_bufp
);
356 *error_stringp
= (const unsigned char*)TA_get_error_message(error
);
361 /* end of ttfautohint.c */