Add code to adjust `hmtx' table.
[ttfautohint.git] / src / hint.c
blob168daae9f59f907881d31c81548a8fbd6ebad948
1 /* hint.c */
3 /*
4 * Copyright (C) 2011 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 test program is a wrapper for `TTF_autohint'. */
18 #include <config.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <errno.h>
23 #include <string.h>
24 #include <getopt.h>
26 #include <ttfautohint.h>
29 typedef struct Progress_Data_ {
30 long last_sfnt;
31 int begin;
32 int last_percent;
33 } Progress_Data;
36 void
37 progress(long curr_idx,
38 long num_glyphs,
39 long curr_sfnt,
40 long num_sfnts,
41 void *user)
43 Progress_Data* data = (Progress_Data*)user;
44 int curr_percent;
45 int curr_diff;
48 if (num_sfnts > 1 && curr_sfnt != data->last_sfnt)
50 fprintf(stderr, "subfont %ld of %ld\n", curr_sfnt + 1, num_sfnts);
51 data->last_sfnt = curr_sfnt;
52 data->begin = 1;
55 if (data->begin)
57 fprintf(stderr, " %ld glyphs\n"
58 " ", num_glyphs);
59 data->begin = 0;
62 /* print progress approx. all 10% */
63 curr_percent = curr_idx * 100 / num_glyphs;
64 curr_diff = curr_percent - data->last_percent;
65 if (curr_diff >= 10)
67 fprintf(stderr, " %d%%", curr_percent);
68 data->last_percent = curr_percent - curr_percent % 10;
71 if (curr_idx + 1 == num_glyphs)
72 fprintf(stderr, "\n");
76 static void
77 show_help(char* program_name,
78 int is_error)
80 FILE* handle = is_error ? stderr : stdout;
83 fprintf(handle,
84 "Usage: %s [OPTION] IN-FILE OUT-FILE\n"
85 "Replace hints in TrueType font IN-FILE and write output to OUT-FILE.\n"
86 "The new hints are based on FreeType's autohinter.\n"
87 "\n"
88 "This program is a simple front-end to the `ttfautohint' library.\n"
89 "\n",
90 program_name);
91 fprintf(handle,
92 "Options:\n"
93 " -f, --latin-fallback set fallback script to latin\n"
94 " -h, --help display this help and exit\n"
95 " -i, --ignore-permissions override font license restrictions\n"
96 " -l, --hinting-range-min=N the minimum ppem value for generating hints\n"
97 " -p, --pre-hinting apply original hints before generating hints\n");
98 fprintf(handle,
99 " -r, --hinting-range-max=N the maximum ppem value for generating hints\n"
100 " -v, --verbose show progress information\n"
101 " -V, --version print version information and exit\n"
102 " -x, --x-height-snapping-exceptions=STRING\n"
103 " specify a comma-separated list of x-height\n"
104 " snapping exceptions ranges and single values\n"
105 "\n");
106 fprintf(handle,
107 "The program accepts both TTF and TTC files as input.\n"
108 "The `gasp' table of OUT-FILE enables grayscale hinting for all sizes.\n"
109 "Use option -i only if you have a legal permission to modify the font.\n"
110 "If option -f is not set, glyphs not in the latin range stay unhinted.\n"
111 "The used ppem value for option -p is FUnits per em, normally 2048.\n"
112 "\n"
113 "Report bugs to: freetype-devel@nongnu.org\n"
114 "FreeType home page: <http://www.freetype.org>\n");
116 if (is_error)
117 exit(EXIT_FAILURE);
118 else
119 exit(EXIT_SUCCESS);
123 static void
124 show_version(void)
126 fprintf(stdout,
128 "ttfautohint version " VERSION "\n"
129 "Copyright (C) 2011 Werner Lemberg <wl@gnu.org>.\n"
130 "License: FreeType License (FTL) or GNU GPLv2.\n"
131 "This is free software: you are free to change and redistribute it.\n"
132 "There is NO WARRANTY, to the extent permitted by law.\n"
136 exit(EXIT_SUCCESS);
141 main(int argc,
142 char** argv)
144 int c;
146 FILE *in;
147 FILE *out;
149 TA_Error error;
150 const unsigned char* error_string;
152 Progress_Data progress_data = {-1, 1, 0};
153 TA_Progress_Func progress_func = NULL;
155 int hinting_range_min = 0;
156 int hinting_range_max = 0;
157 int have_hinting_range_min = 0;
158 int have_hinting_range_max = 0;
160 int ignore_permissions = 0;
161 int pre_hinting = 0;
162 int latin_fallback = 0;
165 while (1)
167 static struct option long_options[] = {
168 {"help", no_argument, 0, 'h'},
169 {"hinting-range-max", required_argument, 0, 'r'},
170 {"hinting-range-min", required_argument, 0, 'l'},
171 {"ignore-permissions", no_argument, 0, 'i'},
172 {"latin-fallback", no_argument, 0, 'f'},
173 {"pre-hinting", no_argument, 0, 'p'},
174 {"verbose", no_argument, 0, 'v'},
175 {"version", no_argument, 0, 'V'},
176 {"x-height-snapping-exceptions", required_argument, 0, 'x'},
177 {0, 0, 0, 0}
180 int option_index = 0;
183 c = getopt_long(argc, argv, "fhil:r:pVvx:", long_options, &option_index);
184 if (c == -1)
185 break;
187 switch (c)
189 case 'f':
190 latin_fallback = 1;
191 break;
193 case 'h':
194 show_help(argv[0], 0);
195 break;
197 case 'i':
198 ignore_permissions = 1;
199 break;
201 case 'l':
202 hinting_range_min = atoi(optarg);
203 have_hinting_range_min = 1;
204 break;
206 case 'r':
207 hinting_range_max = atoi(optarg);
208 have_hinting_range_max = 1;
209 break;
211 case 'p':
212 pre_hinting = 1;
213 break;
215 case 'v':
216 progress_func = progress;
217 break;
219 case 'V':
220 show_version();
221 break;
223 case 'x':
224 fprintf(stderr, "Option `-x' not implemented yet\n");
225 break;
227 default:
228 exit(EXIT_FAILURE);
232 if (!have_hinting_range_min)
233 hinting_range_min = 8;
234 if (!have_hinting_range_max)
235 hinting_range_max = 1000;
237 if (hinting_range_min < 2)
239 fprintf(stderr, "The hinting range minimum must be at least 2\n");
240 exit(EXIT_FAILURE);
242 if (hinting_range_max < hinting_range_min)
244 fprintf(stderr, "The hinting range maximum must not be smaller"
245 " than the minimum (%d)\n",
246 hinting_range_min);
247 exit(EXIT_FAILURE);
250 if (argc - optind != 2)
251 show_help(argv[0], 1);
253 in = fopen(argv[optind], "rb");
254 if (!in)
256 fprintf(stderr, "The following error occurred while opening font `%s':\n"
257 "\n"
258 " %s\n",
259 argv[optind], strerror(errno));
260 exit(EXIT_FAILURE);
263 out = fopen(argv[optind + 1], "wb");
264 if (!out)
266 fprintf(stderr, "The following error occurred while opening font `%s':\n"
267 "\n"
268 " %s\n",
269 argv[optind + 1], strerror(errno));
270 exit(EXIT_FAILURE);
273 error = TTF_autohint("in-file, out-file,"
274 "hinting-range-min, hinting-range-max,"
275 "error-string,"
276 "progress-callback, progress-callback-data,"
277 "ignore-permissions, pre-hinting, fallback-script",
278 in, out,
279 hinting_range_min, hinting_range_max,
280 &error_string,
281 progress_func, &progress_data,
282 ignore_permissions, pre_hinting, latin_fallback);
284 if (error)
286 if (error == TA_Err_Invalid_FreeType_Version)
287 fprintf(stderr,
288 "FreeType version 2.4.5 or higher is needed.\n"
289 "Perhaps using a wrong FreeType DLL?\n");
290 else if (error == TA_Err_Missing_Legal_Permission)
291 fprintf(stderr,
292 "Bit 1 in the `fsType' field of the `OS/2' table is set:\n"
293 "This font must not be modified"
294 " without permission of the legal owner.\n"
295 "Use command line option `-i' to continue"
296 " if you have such a permission.\n");
297 else if (error == TA_Err_Missing_Unicode_CMap)
298 fprintf(stderr,
299 "No Unicode character map.\n");
300 else if (error == TA_Err_Missing_Glyph)
301 fprintf(stderr,
302 "No glyph for the key character"
303 " to derive standard width and height.\n"
304 "For the latin script, this key character is `o' (U+006F).\n");
305 else
306 fprintf(stderr,
307 "Error code `0x%02x' while autohinting font:\n"
308 " %s\n", error, error_string);
309 exit(EXIT_FAILURE);
312 fclose(in);
313 fclose(out);
315 exit(EXIT_SUCCESS);
318 /* end of hint.c */