Move functions related to TTC creation into separate file.
[ttfautohint.git] / src / hint.c
blobaa2c1c62f0d6c685c8f092513e517d6effdfc8be
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 latin_fallback = 0;
164 while (1)
166 static struct option long_options[] = {
167 {"help", no_argument, 0, 'h'},
168 {"hinting-range-max", required_argument, 0, 'r'},
169 {"hinting-range-min", required_argument, 0, 'l'},
170 {"ignore-permissions", no_argument, 0, 'i'},
171 {"latin-fallback", no_argument, 0, 'f'},
172 {"pre-hinting", no_argument, 0, 'p'},
173 {"verbose", no_argument, 0, 'v'},
174 {"version", no_argument, 0, 'V'},
175 {"x-height-snapping-exceptions", required_argument, 0, 'x'},
176 {0, 0, 0, 0}
179 int option_index = 0;
182 c = getopt_long(argc, argv, "fhil:r:pVvx:", long_options, &option_index);
183 if (c == -1)
184 break;
186 switch (c)
188 case 'f':
189 latin_fallback = 1;
190 break;
192 case 'h':
193 show_help(argv[0], 0);
194 break;
196 case 'i':
197 ignore_permissions = 1;
198 break;
200 case 'l':
201 hinting_range_min = atoi(optarg);
202 have_hinting_range_min = 1;
203 break;
205 case 'r':
206 hinting_range_max = atoi(optarg);
207 have_hinting_range_max = 1;
208 break;
210 case 'p':
211 fprintf(stderr, "Option `-p' not implemented yet\n");
212 break;
214 case 'v':
215 progress_func = progress;
216 break;
218 case 'V':
219 show_version();
220 break;
222 case 'x':
223 fprintf(stderr, "Option `-x' not implemented yet\n");
224 break;
226 default:
227 exit(EXIT_FAILURE);
231 if (!have_hinting_range_min)
232 hinting_range_min = 8;
233 if (!have_hinting_range_max)
234 hinting_range_max = 1000;
236 if (hinting_range_min < 2)
238 fprintf(stderr, "The hinting range minimum must be at least 2\n");
239 exit(EXIT_FAILURE);
241 if (hinting_range_max < hinting_range_min)
243 fprintf(stderr, "The hinting range maximum must not be smaller"
244 " than the minimum (%d)\n",
245 hinting_range_min);
246 exit(EXIT_FAILURE);
249 if (argc - optind != 2)
250 show_help(argv[0], 1);
252 in = fopen(argv[optind], "rb");
253 if (!in)
255 fprintf(stderr, "The following error occurred while opening font `%s':\n"
256 "\n"
257 " %s\n",
258 argv[optind], strerror(errno));
259 exit(EXIT_FAILURE);
262 out = fopen(argv[optind + 1], "wb");
263 if (!out)
265 fprintf(stderr, "The following error occurred while opening font `%s':\n"
266 "\n"
267 " %s\n",
268 argv[optind + 1], strerror(errno));
269 exit(EXIT_FAILURE);
272 error = TTF_autohint("in-file, out-file,"
273 "hinting-range-min, hinting-range-max,"
274 "error-string,"
275 "progress-callback, progress-callback-data,"
276 "ignore-permissions, fallback-script",
277 in, out,
278 hinting_range_min, hinting_range_max,
279 &error_string,
280 progress_func, &progress_data,
281 ignore_permissions, latin_fallback);
283 if (error)
285 if (error == TA_Err_Invalid_FreeType_Version)
286 fprintf(stderr,
287 "FreeType version 2.4.5 or higher is needed.\n"
288 "Perhaps using a wrong FreeType DLL?\n");
289 else if (error == TA_Err_Missing_Legal_Permission)
290 fprintf(stderr,
291 "Bit 1 in the `fsType' field of the `OS/2' table is set:\n"
292 "This font must not be modified"
293 " without permission of the legal owner.\n"
294 "Use command line option `-i' to continue"
295 " if you have such a permission.\n");
296 else if (error == TA_Err_Missing_Unicode_CMap)
297 fprintf(stderr,
298 "No Unicode character map.\n");
299 else if (error == TA_Err_Missing_Glyph)
300 fprintf(stderr,
301 "No glyph for the key character"
302 " to derive standard width and height.\n"
303 "For the latin script, this key character is `o' (U+006F).\n");
304 else
305 fprintf(stderr,
306 "Error code `0x%02x' while autohinting font:\n"
307 " %s\n", error, error_string);
308 exit(EXIT_FAILURE);
311 exit(EXIT_SUCCESS);
314 /* end of hint.c */