Debian Codegraph @

texk/web2c/mfluadir/otfcc/src/otfccdump.c

raw
1 #include "dep/json-builder.h"
2 #include "otfcc/sfnt.h"
3 #include "otfcc/font.h"
4
5 #include "aliases.h"
6 #include "platform.h"
7 #include "stopwatch.h"
8
9 #ifndef MAIN_VER
10 #define MAIN_VER 0
11 #endif
12 #ifndef SECONDARY_VER
13 #define SECONDARY_VER 0
14 #endif
15 #ifndef PATCH_VER
16 #define PATCH_VER 0
17 #endif
18
19 static void printInfo() {
20 fprintf(stdout, "This is Polymorphic otfccdump, version %d.%d.%d.\n", MAIN_VER, SECONDARY_VER,
21 PATCH_VER);
22 }
23 static void printHelp() {
24 fprintf(stdout,
25 "\n"
26 "Usage : otfccdump [OPTIONS] input.[otf|ttf|ttc]\n\n"
27 " -h, --help : Display this help message and exit.\n"
28 " -v, --version : Display version information and exit.\n"
29 " -o <file> : Set output file path to <file>. When absent the dump\n"
30 " will be written to STDOUT.\n"
31 " -n <n>, --ttc-index <n> : Use the <n>th subfont within the input font.\n"
32 " --pretty : Prettify the output JSON.\n"
33 " --ugly : Force uglify the output JSON.\n"
34 " --verbose : Show more information when building.\n"
35 " -q, --quiet : Be silent when building.\n\n"
36 " --ignore-glyph-order : Do not export glyph order information.\n"
37 " --glyph-name-prefix pfx : Add a prefix to the glyph names.\n"
38 " --ignore-hints : Do not export hinting information.\n"
39 " --decimal-cmap : Export 'cmap' keys as decimal number.\n"
40 " --hex-cmap : Export 'cmap' keys as hex number (U+FFFF).\n"
41 " --name-by-hash : Name glyphs using its hash value.\n"
42 " --name-by-gid : Name glyphs using its glyph id.\n"
43 " --add-bom : Add BOM mark in the output. (It is default on Windows\n"
44 " when redirecting to another program. Use --no-bom to\n"
45 " turn it off.)\n"
46 "\n");
47 }
48 #ifdef _WIN32
49 int main() {
50 int argc;
51 char **argv;
52 get_argv_utf8(&argc, &argv);
53 #else
54 int main(int argc, char *argv[]) {
55 #endif
56
57 bool show_help = false;
58 bool show_version = false;
59 bool show_pretty = false;
60 bool show_ugly = false;
61 bool add_bom = false;
62 bool no_bom = false;
63 uint32_t ttcindex = 0;
64 struct option longopts[] = {{"version", no_argument, NULL, 'v'},
65 {"help", no_argument, NULL, 'h'},
66 {"pretty", no_argument, NULL, 'p'},
67 {"ugly", no_argument, NULL, 0},
68 {"time", no_argument, NULL, 0},
69 {"ignore-glyph-order", no_argument, NULL, 0},
70 {"ignore-hints", no_argument, NULL, 0},
71 {"hex-cmap", no_argument, NULL, 0},
72 {"decimal-cmap", no_argument, NULL, 0},
73 {"instr-as-bytes", no_argument, NULL, 0},
74 {"name-by-hash", no_argument, NULL, 0},
75 {"name-by-gid", no_argument, NULL, 0},
76 {"glyph-name-prefix", required_argument, NULL, 0},
77 {"verbose", no_argument, NULL, 0},
78 {"quiet", no_argument, NULL, 0},
79 {"add-bom", no_argument, NULL, 0},
80 {"no-bom", no_argument, NULL, 0},
81 {"output", required_argument, NULL, 'o'},
82 {"ttc-index", required_argument, NULL, 'n'},
83 {"debug-wait-on-start", no_argument, NULL, 0},
84 {0, 0, 0, 0}};
85
86 otfcc_Options *options = otfcc_newOptions();
87 options->logger = otfcc_newLogger(otfcc_newStdErrTarget());
88 options->logger->indent(options->logger, "otfccdump");
89 options->decimal_cmap = true;
90
91 int option_index = 0;
92 int c;
93
94 sds outputPath = NULL;
95 sds inPath = NULL;
96
97 while ((c = getopt_long(argc, argv, "vhqpio:n:", longopts, &option_index)) != (-1)) {
98 switch (c) {
99 case 0:
100 /* If this option set a flag, do nothing else now. */
101 if (longopts[option_index].flag != 0) {
102 break;
103 } else if (strcmp(longopts[option_index].name, "ugly") == 0) {
104 show_ugly = true;
105 } else if (strcmp(longopts[option_index].name, "time") == 0) {
106 } else if (strcmp(longopts[option_index].name, "add-bom") == 0) {
107 add_bom = true;
108 } else if (strcmp(longopts[option_index].name, "no-bom") == 0) {
109 no_bom = true;
110 } else if (strcmp(longopts[option_index].name, "ignore-glyph-order") == 0) {
111 options->ignore_glyph_order = true;
112 } else if (strcmp(longopts[option_index].name, "verbose") == 0) {
113 options->verbose = true;
114 } else if (strcmp(longopts[option_index].name, "quiet") == 0) {
115 options->quiet = true;
116 } else if (strcmp(longopts[option_index].name, "ignore-hints") == 0) {
117 options->ignore_hints = true;
118 } else if (strcmp(longopts[option_index].name, "decimal-cmap") == 0) {
119 options->decimal_cmap = true;
120 } else if (strcmp(longopts[option_index].name, "hex-cmap") == 0) {
121 options->decimal_cmap = false;
122 } else if (strcmp(longopts[option_index].name, "name-by-hash") == 0) {
123 options->name_glyphs_by_hash = true;
124 } else if (strcmp(longopts[option_index].name, "name-by-gid") == 0) {
125 options->name_glyphs_by_gid = true;
126 } else if (strcmp(longopts[option_index].name, "instr-as-bytes") == 0) {
127 options->instr_as_bytes = true;
128 } else if (strcmp(longopts[option_index].name, "glyph-name-prefix") == 0) {
129 options->glyph_name_prefix = strdup(optarg);
130 } else if (strcmp(longopts[option_index].name, "debug-wait-on-start") == 0) {
131 options->debug_wait_on_start = true;
132 }
133 break;
134 case 'v':
135 show_version = true;
136 break;
137 case 'i':
138 options->ignore_glyph_order = true;
139 break;
140 case 'h':
141 show_help = true;
142 break;
143 case 'p':
144 show_pretty = true;
145 break;
146 case 'o':
147 outputPath = sdsnew(optarg);
148 break;
149 case 'q':
150 options->quiet = true;
151 break;
152 case 'n':
153 ttcindex = atoi(optarg);
154 break;
155 }
156 }
157
158 if (options->debug_wait_on_start) { getchar(); }
159
160 options->logger->setVerbosity(options->logger,
161 options->quiet ? 0 : options->verbose ? 0xFF : 1);
162
163 if (show_help) {
164 printInfo();
165 printHelp();
166 return 0;
167 }
168 if (show_version) {
169 printInfo();
170 return 0;
171 }
172
173 if (optind >= argc) {
174 logError("Expected argument for input file name.\n");
175 printHelp();
176 exit(EXIT_FAILURE);
177 } else {
178 inPath = sdsnew(argv[optind]);
179 }
180
181 struct timespec begin;
182
183 time_now(&begin);
184
185 otfcc_SplineFontContainer *sfnt;
186 loggedStep("Read SFNT") {
187 logProgress("From file %s", inPath);
188 FILE *file = u8fopen(inPath, "rb");
189 sfnt = otfcc_readSFNT(file);
190 if (!sfnt || sfnt->count == 0) {
191 logError("Cannot read SFNT file \"%s\". Exit.\n", inPath);
192 exit(EXIT_FAILURE);
193 }
194 if (ttcindex >= sfnt->count) {
195 logError("Subfont index %d out of range for \"%s\" (0 -- %d). Exit.\n", ttcindex,
196 inPath, (sfnt->count - 1));
197 exit(EXIT_FAILURE);
198 }
199 logStepTime;
200 }
201
202 otfcc_Font *font;
203 loggedStep("Read Font") {
204 otfcc_IFontBuilder *reader = otfcc_newOTFReader();
205 font = reader->read(sfnt, ttcindex, options);
206 if (!font) {
207 logError("Font structure broken or corrupted \"%s\". Exit.\n", inPath);
208 exit(EXIT_FAILURE);
209 }
210 reader->free(reader);
211 if (sfnt) otfcc_deleteSFNT(sfnt);
212 logStepTime;
213 }
214 loggedStep("Consolidate") {
215 otfcc_iFont.consolidate(font, options);
216 logStepTime;
217 }
218 json_value *root;
219 loggedStep("Dump") {
220 otfcc_IFontSerializer *dumper = otfcc_newJsonWriter();
221 root = (json_value *)dumper->serialize(font, options);
222 if (!root) {
223 logError("Font structure broken or corrupted \"%s\". Exit.\n", inPath);
224 exit(EXIT_FAILURE);
225 }
226 logStepTime;
227 dumper->free(dumper);
228 }
229
230 char *buf;
231 size_t buflen;
232 loggedStep("Serialize to JSON") {
233 json_serialize_opts jsonOptions;
234 jsonOptions.mode = json_serialize_mode_packed;
235 jsonOptions.opts = 0;
236 jsonOptions.indent_size = 4;
237 if (show_pretty || (!outputPath && isatty(fileno(stdout)))) {
238 jsonOptions.mode = json_serialize_mode_multiline;
239 }
240 if (show_ugly) jsonOptions.mode = json_serialize_mode_packed;
241 buflen = json_measure_ex(root, jsonOptions);
242 buf = calloc(1, buflen);
243 json_serialize_ex(buf, root, jsonOptions);
244 logStepTime;
245 }
246
247 loggedStep("Output") {
248 if (outputPath) {
249 FILE *outputFile = u8fopen(outputPath, "wb");
250 if (!outputFile) {
251 logError("Cannot write to file \"%s\". Exit.", outputPath);
252 exit(EXIT_FAILURE);
253 }
254 if (add_bom) {
255 fputc(0xEF, outputFile);
256 fputc(0xBB, outputFile);
257 fputc(0xBF, outputFile);
258 }
259 size_t actualLen = buflen - 1;
260 while (!buf[actualLen])
261 actualLen -= 1;
262 fwrite(buf, sizeof(char), actualLen + 1, outputFile);
263 fclose(outputFile);
264 } else {
265 #ifdef WIN32
266 if (isatty(fileno(stdout))) {
267 LPWSTR pwStr;
268 DWORD dwNum = widen_utf8(buf, &pwStr);
269 DWORD actual = 0;
270 DWORD written = 0;
271 const DWORD chunk = 0x10000;
272 while (written < dwNum) {
273 DWORD len = dwNum - written;
274 if (len > chunk) len = chunk;
275 WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), pwStr + written, len, &actual,
276 NULL);
277 written += len;
278 }
279 free(pwStr);
280 } else {
281 if (!no_bom) {
282 fputc(0xEF, stdout);
283 fputc(0xBB, stdout);
284 fputc(0xBF, stdout);
285 }
286 fputs(buf, stdout);
287 }
288 #else
289 if (add_bom) {
290 fputc(0xEF, stdout);
291 fputc(0xBB, stdout);
292 fputc(0xBF, stdout);
293 }
294 fputs(buf, stdout);
295 #endif
296 }
297 logStepTime;
298 }
299
300 loggedStep("Finalize") {
301 free(buf);
302 if (font) otfcc_iFont.free(font);
303 if (root) json_builder_free(root);
304 if (inPath) sdsfree(inPath);
305 if (outputPath) sdsfree(outputPath);
306 logStepTime;
307 }
308 otfcc_deleteOptions(options);
309
310 return 0;
311 }