Debian Codegraph @

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

raw
1 #include "dep/json-builder.h"
2 #include "otfcc/sfnt.h"
3 #include "otfcc/font.h"
4 #include "otfcc/sfnt-builder.h"
5 #include "aliases.h"
6 #include "platform.h"
7 //#include "stopwatch.h"
8
9 #ifndef _MSC_VER
10 #include <unistd.h>
11 #endif /* _MSC_VER */
12
13 /* to reset the getopt_long */
14 extern int optind;
15
16 int otfcc_build(int b_argc, char *b_argv[]);
17 int otfcc_dump(int b_argc, char *b_argv[]);
18
19 extern char *xstrdup(const char *);
20
21
22 #ifndef MAIN_VER
23 #define MAIN_VER 0
24 #endif
25 #ifndef SECONDARY_VER
26 #define SECONDARY_VER 0
27 #endif
28 #ifndef PATCH_VER
29 #define PATCH_VER 0
30 #endif
31
32 static void dump_printInfo() {
33 fprintf(stdout, "This is dump, version %d.%d.%d.\n", MAIN_VER, SECONDARY_VER,
34 PATCH_VER);
35 }
36
37 static void build_printInfo() {
38 fprintf(stdout, "This is build, version %d.%d.%d.\n", MAIN_VER, SECONDARY_VER,
39 PATCH_VER);
40 }
41
42 static void dump_printHelp() {
43 fprintf(stdout,
44 "\n"
45 "Usage : dump [OPTIONS] input.[otf|ttf|ttc]\n\n"
46 " -h, --help : Display this help message and exit.\n"
47 " -v, --version : Display version information and exit.\n"
48 " -o <file> : Set output file path to <file>. When absent the dump\n"
49 " will be written to STDOUT.\n"
50 " -n <n>, --ttc-index <n> : Use the <n>th subfont within the input font.\n"
51 " --pretty : Prettify the output JSON.\n"
52 " --ugly : Force uglify the output JSON.\n"
53 " --verbose : Show more information when building.\n"
54 " -q, --quiet : Be silent when building.\n\n"
55 " --ignore-glyph-order : Do not export glyph order information.\n"
56 " --glyph-name-prefix pfx : Add a prefix to the glyph names.\n"
57 " --ignore-hints : Do not export hinting information.\n"
58 " --decimal-cmap : Export 'cmap' keys as decimal number.\n"
59 " --hex-cmap : Export 'cmap' keys as hex number (U+FFFF).\n"
60 " --name-by-hash : Name glyphs using its hash value.\n"
61 " --name-by-gid : Name glyphs using its glyph id.\n"
62 " --add-bom : Add BOM mark in the output. (It is default on Windows\n"
63 " when redirecting to another program. Use --no-bom to\n"
64 " turn it off.)\n"
65 "\n");
66 }
67
68
69 static void build_printHelp() {
70 fprintf(stdout,
71 "\n"
72 "Usage : build [OPTIONS] [input.json] -o output.[ttf|otf]\n\n"
73 " input.json : Path to input file. When absent the input will be\n"
74 " read from the STDIN.\n\n"
75 " -h, --help : Display this help message and exit.\n"
76 " -v, --version : Display version information and exit.\n"
77 " -o <file> : Set output file path to <file>.\n"
78 " -s, --dummy-dsig : Include an empty DSIG table in the font. For some\n"
79 " Microsoft applications, DSIG is required to enable\n"
80 " OpenType features.\n"
81 " -O<n> : Specify the level for optimization.\n"
82 " -O0 Turn off any optimization.\n"
83 " -O1 Default optimization.\n"
84 " -O2 More aggressive optimizations for web font. In this\n"
85 " level, the following options will be set:\n"
86 " --merge-features\n"
87 " --short-post\n"
88 " --subroutinize\n"
89 " -O3 Most aggressive opptimization strategy will be\n"
90 " used. In this level, these options will be set:\n"
91 " --force-cid\n"
92 " --ignore-glyph-order\n"
93 " --verbose : Show more information when building.\n"
94 " -q, --quiet : Be silent when building.\n\n"
95 " --ignore-hints : Ignore the hinting information in the input.\n"
96 " --keep-average-char-width : Keep the OS/2.xAvgCharWidth value from the input\n"
97 " instead of stating the average width of glyphs.\n"
98 " Useful when creating a monospaced font.\n"
99 " --keep-unicode-ranges : Keep the OS/2.ulUnicodeRange[1-4] as-is.\n"
100 " --keep-modified-time : Keep the head.modified time in the json, instead of\n"
101 " using current time.\n\n"
102 " --short-post : Don't export glyph names in the result font.\n"
103 " --ignore-glyph-order, -i : Ignore the glyph order information in the input.\n"
104 " --keep-glyph-order, -k : Keep the glyph order information in the input.\n"
105 " Use to preserve glyph order under -O2 and -O3.\n"
106 " --dont-ignore-glyph-order : Same as --keep-glyph-order.\n"
107 " --merge-features : Merge duplicate OpenType feature definitions.\n"
108 " --dont-merge-features : Keep duplicate OpenType feature definitions.\n"
109 " --merge-lookups : Merge duplicate OpenType lookups.\n"
110 " --dont-merge-lookups : Keep duplicate OpenType lookups.\n"
111 " --force-cid : Convert name-keyed CFF OTF into CID-keyed.\n"
112 " --subroutinize : Subroutinize CFF table.\n"
113 " --stub-cmap4 : Create a stub `cmap` format 4 subtable if format\n"
114 " 12 subtable is present.\n"
115 "\n");
116 }
117
118
119 static void build_readEntireFile(char *inPath, char **_buffer, long *_length) {
120 char *buffer = NULL;
121 long length = 0;
122 FILE *f = u8fopen(inPath, "rb");
123 if (!f) {
124 fprintf(stderr, "Cannot read JSON file \"%s\". Exit.\n", inPath);
125 exit(EXIT_FAILURE);
126 }
127 fseek(f, 0, SEEK_END);
128 length = ftell(f);
129 fseek(f, 0, SEEK_SET);
130 buffer = malloc(length);
131 if (buffer) { fread(buffer, 1, length, f); }
132 fclose(f);
133
134 if (!buffer) {
135 fprintf(stderr, "Cannot read JSON file \"%s\". Exit.\n", inPath);
136 exit(EXIT_FAILURE);
137 }
138 *_buffer = buffer;
139 *_length = length;
140 }
141
142
143 int otfcc_build(int b_argc, char *b_argv[]) {
144 // struct timespec begin;
145 // time_now(&begin);
146
147 bool show_help = false;
148 bool show_version = false;
149 sds outputPath = NULL;
150 sds inPath = NULL;
151 int option_index = 0;
152 int c;
153
154 optind=0;
155 if ((b_argc==0)||(b_argv==NULL))
156 return 0;
157
158 otfcc_Options *options = otfcc_newOptions();
159 options->logger = otfcc_newLogger(otfcc_newStdErrTarget());
160 options->logger->indent(options->logger, "build");
161 otfcc_Options_optimizeTo(options, 1);
162
163 struct option longopts[] = {{"version", no_argument, NULL, 'v'},
164 {"help", no_argument, NULL, 'h'},
165 {"time", no_argument, NULL, 0},
166 {"ignore-glyph-order", no_argument, NULL, 0},
167 {"keep-glyph-order", no_argument, NULL, 0},
168 {"dont-ignore-glyph-order", no_argument, NULL, 0},
169 {"ignore-hints", no_argument, NULL, 0},
170 {"keep-average-char-width", no_argument, NULL, 0},
171 {"keep-unicode-ranges", no_argument, NULL, 0},
172 {"keep-modified-time", no_argument, NULL, 0},
173 {"merge-lookups", no_argument, NULL, 0},
174 {"merge-features", no_argument, NULL, 0},
175 {"dont-merge-lookups", no_argument, NULL, 0},
176 {"dont-merge-features", no_argument, NULL, 0},
177 {"short-post", no_argument, NULL, 0},
178 {"force-cid", no_argument, NULL, 0},
179 {"subroutinize", no_argument, NULL, 0},
180 {"stub-cmap4", no_argument, NULL, 0},
181 {"dummy-dsig", no_argument, NULL, 's'},
182 {"ship", no_argument, NULL, 0},
183 {"verbose", no_argument, NULL, 0},
184 {"quiet", no_argument, NULL, 0},
185 {"optimize", required_argument, NULL, 'O'},
186 {"output", required_argument, NULL, 'o'},
187 {0, 0, 0, 0}};
188
189 while ((c = getopt_long(b_argc, b_argv, "vhqskiO:o:", longopts, &option_index)) != (-1)) {
190 switch (c) {
191 case 0:
192 /* If this option set a flag, do nothing else now. */
193 if (longopts[option_index].flag != 0) {
194 break;
195 } else if (strcmp(longopts[option_index].name, "time") == 0) {
196 } else if (strcmp(longopts[option_index].name, "ignore-hints") == 0) {
197 options->ignore_hints = true;
198 } else if (strcmp(longopts[option_index].name, "keep-average-char-width") == 0) {
199 options->keep_average_char_width = true;
200 } else if (strcmp(longopts[option_index].name, "keep-unicode-ranges") == 0) {
201 options->keep_unicode_ranges = true;
202 } else if (strcmp(longopts[option_index].name, "keep-modified-time") == 0) {
203 options->keep_modified_time = true;
204 } else if (strcmp(longopts[option_index].name, "merge-features") == 0) {
205 options->merge_features = true;
206 } else if (strcmp(longopts[option_index].name, "merge-lookups") == 0) {
207 options->merge_lookups = true;
208 } else if (strcmp(longopts[option_index].name, "dont-merge-features") == 0) {
209 options->merge_features = false;
210 } else if (strcmp(longopts[option_index].name, "dont-merge-lookups") == 0) {
211 options->merge_lookups = false;
212 } else if (strcmp(longopts[option_index].name, "ignore-glyph-order") == 0) {
213 options->ignore_glyph_order = true;
214 } else if (strcmp(longopts[option_index].name, "keep-glyph-order") == 0) {
215 options->ignore_glyph_order = false;
216 } else if (strcmp(longopts[option_index].name, "dont-keep-glyph-order") == 0) {
217 options->ignore_glyph_order = false;
218 } else if (strcmp(longopts[option_index].name, "short-post") == 0) {
219 options->short_post = true;
220 } else if (strcmp(longopts[option_index].name, "force-cid") == 0) {
221 options->force_cid = true;
222 } else if (strcmp(longopts[option_index].name, "subroutinize") == 0) {
223 options->cff_doSubroutinize = true;
224 } else if (strcmp(longopts[option_index].name, "stub-cmap4") == 0) {
225 options->stub_cmap4 = true;
226 } else if (strcmp(longopts[option_index].name, "ship") == 0) {
227 options->ignore_glyph_order = true;
228 options->short_post = true;
229 options->dummy_DSIG = true;
230 } else if (strcmp(longopts[option_index].name, "verbose") == 0) {
231 options->verbose = true;
232 } else if (strcmp(longopts[option_index].name, "quiet") == 0) {
233 options->quiet = true;
234 }
235 break;
236 case 'v':
237 show_version = true;
238 break;
239 case 'h':
240 show_help = true;
241 break;
242 case 'k':
243 options->ignore_glyph_order = false;
244 break;
245 case 'i':
246 options->ignore_glyph_order = true;
247 break;
248 case 'o':
249 outputPath = sdsnew(optarg);
250 break;
251 case 's':
252 options->dummy_DSIG = true;
253 break;
254 case 'q':
255 options->quiet = true;
256 break;
257 case 'O':
258 otfcc_Options_optimizeTo(options, atoi(optarg));
259 break;
260 }
261 }
262 options->logger->setVerbosity(options->logger,
263 options->quiet ? 0 : options->verbose ? 0xFF : 1);
264 if (show_help) {
265 build_printInfo();
266 build_printHelp();
267 return 0;
268 }
269 if (show_version) {
270 build_printInfo();
271 return 0;
272 }
273
274 if (optind >= b_argc) {
275 inPath = NULL; // read from STDIN
276 } else {
277 inPath = sdsnew(b_argv[optind]); // read from file
278 }
279 if (!outputPath) {
280 logError("Unable to build OpenType font tile : output path not "
281 "specified. Exit.\n");
282 build_printHelp();
283 exit(EXIT_FAILURE);
284 }
285
286 char *buffer;
287 long length;
288 loggedStep("Load file") {
289 if (inPath) {
290 loggedStep("Load from file %s", inPath) {
291 build_readEntireFile(inPath, &buffer, &length);
292 sdsfree(inPath);
293 }
294 } else {
295 /*loggedStep("Load from stdin") {
296 build_readEntireStdin(&buffer, &length);
297 }*/
298 }
299 //logStepTime;
300 }
301
302 json_value *jsonRoot = NULL;
303 loggedStep("Parse into JSON") {
304 jsonRoot = json_parse(buffer, length);
305 free(buffer);
306 //logStepTime;
307 if (!jsonRoot) {
308 logError("Cannot parse JSON file \"%s\". Exit.\n", inPath);
309 exit(EXIT_FAILURE);
310 }
311 }
312
313 otfcc_Font *font;
314 loggedStep("Parse") {
315 otfcc_IFontBuilder *parser = otfcc_newJsonReader();
316 font = parser->read(jsonRoot, 0, options);
317 if (!font) {
318 logError("Cannot parse JSON file \"%s\" as a font. Exit.\n", inPath);
319 exit(EXIT_FAILURE);
320 }
321 parser->free(parser);
322 json_value_free(jsonRoot);
323 //logStepTime;
324 }
325 loggedStep("Consolidate") {
326 otfcc_iFont.consolidate(font, options);
327 //logStepTime;
328 }
329 loggedStep("Build") {
330 otfcc_IFontSerializer *writer = otfcc_newOTFWriter();
331 caryll_Buffer *otf = (caryll_Buffer *)writer->serialize(font, options);
332 loggedStep("Write to file") {
333 FILE *outfile = u8fopen(outputPath, "wb");
334 if (!outfile) {
335 logError("Cannot write to file \"%s\". Exit.\n", outputPath);
336 exit(EXIT_FAILURE);
337 }
338 fwrite(otf->data, sizeof(uint8_t), buflen(otf), outfile);
339 fclose(outfile);
340 }
341 //logStepTime;
342 buffree(otf), writer->free(writer), otfcc_iFont.free(font), sdsfree(outputPath);
343 }
344 otfcc_deleteOptions(options);
345
346 return 0;
347 }
348
349
350
351 int otfcc_dump(int b_argc, char *b_argv[]) {
352 bool show_help = false;
353 bool show_version = false;
354 bool show_pretty = false;
355 bool show_ugly = false;
356 bool add_bom = false;
357 bool no_bom = false;
358 uint32_t ttcindex = 0;
359 struct option longopts[] = {{"version", no_argument, NULL, 'v'},
360 {"help", no_argument, NULL, 'h'},
361 {"pretty", no_argument, NULL, 'p'},
362 {"ugly", no_argument, NULL, 0},
363 {"time", no_argument, NULL, 0},
364 {"ignore-glyph-order", no_argument, NULL, 0},
365 {"ignore-hints", no_argument, NULL, 0},
366 {"hex-cmap", no_argument, NULL, 0},
367 {"decimal-cmap", no_argument, NULL, 0},
368 {"instr-as-bytes", no_argument, NULL, 0},
369 {"name-by-hash", no_argument, NULL, 0},
370 {"name-by-gid", no_argument, NULL, 0},
371 {"glyph-name-prefix", required_argument, NULL, 0},
372 {"verbose", no_argument, NULL, 0},
373 {"quiet", no_argument, NULL, 0},
374 {"add-bom", no_argument, NULL, 0},
375 {"no-bom", no_argument, NULL, 0},
376 {"output", required_argument, NULL, 'o'},
377 {"ttc-index", required_argument, NULL, 'n'},
378 {"debug-wait-on-start", no_argument, NULL, 0},
379 {0, 0, 0, 0}};
380
381 optind=0;
382 if ((b_argc==0)||(b_argv==NULL))
383 return 0;
384 otfcc_Options *options = otfcc_newOptions();
385 options->logger = otfcc_newLogger(otfcc_newStdErrTarget());
386 options->logger->indent(options->logger, "dump");
387 options->decimal_cmap = true;
388
389 int option_index = 0;
390 int c;
391
392 sds outputPath = NULL;
393 sds inPath = NULL;
394
395 while ((c = getopt_long(b_argc, b_argv, "vhqpio:n:", longopts, &option_index)) != (-1)) {
396 switch (c) {
397 case 0:
398 /* If this option set a flag, do nothing else now. */
399 if (longopts[option_index].flag != 0) {
400 break;
401 } else if (strcmp(longopts[option_index].name, "ugly") == 0) {
402 show_ugly = true;
403 } else if (strcmp(longopts[option_index].name, "time") == 0) {
404 } else if (strcmp(longopts[option_index].name, "add-bom") == 0) {
405 add_bom = true;
406 } else if (strcmp(longopts[option_index].name, "no-bom") == 0) {
407 no_bom = true;
408 } else if (strcmp(longopts[option_index].name, "ignore-glyph-order") == 0) {
409 options->ignore_glyph_order = true;
410 } else if (strcmp(longopts[option_index].name, "verbose") == 0) {
411 options->verbose = true;
412 } else if (strcmp(longopts[option_index].name, "quiet") == 0) {
413 options->quiet = true;
414 } else if (strcmp(longopts[option_index].name, "ignore-hints") == 0) {
415 options->ignore_hints = true;
416 } else if (strcmp(longopts[option_index].name, "decimal-cmap") == 0) {
417 options->decimal_cmap = true;
418 } else if (strcmp(longopts[option_index].name, "hex-cmap") == 0) {
419 options->decimal_cmap = false;
420 } else if (strcmp(longopts[option_index].name, "name-by-hash") == 0) {
421 options->name_glyphs_by_hash = true;
422 } else if (strcmp(longopts[option_index].name, "name-by-gid") == 0) {
423 options->name_glyphs_by_gid = true;
424 } else if (strcmp(longopts[option_index].name, "instr-as-bytes") == 0) {
425 options->instr_as_bytes = true;
426 } else if (strcmp(longopts[option_index].name, "glyph-name-prefix") == 0) {
427 options->glyph_name_prefix = xstrdup(optarg);
428 } else if (strcmp(longopts[option_index].name, "debug-wait-on-start") == 0) {
429 options->debug_wait_on_start = true;
430 }
431 break;
432 case 'v':
433 show_version = true;
434 break;
435 case 'i':
436 options->ignore_glyph_order = true;
437 break;
438 case 'h':
439 show_help = true;
440 break;
441 case 'p':
442 show_pretty = true;
443 break;
444 case 'o':
445 outputPath = sdsnew(optarg);
446 break;
447 case 'q':
448 options->quiet = true;
449 break;
450 case 'n':
451 ttcindex = atoi(optarg);
452 break;
453 }
454 }
455
456 if (options->debug_wait_on_start) { getchar(); }
457
458 options->logger->setVerbosity(options->logger,
459 options->quiet ? 0 : options->verbose ? 0xFF : 1);
460
461 if (show_help) {
462 dump_printInfo();
463 dump_printHelp();
464 return 0;
465 }
466 if (show_version) {
467 dump_printInfo();
468 return 0;
469 }
470
471 if (optind >= b_argc) {
472 logError("Expected argument for input file name.\n");
473 dump_printHelp();
474 exit(EXIT_FAILURE);
475 } else {
476 inPath = sdsnew(b_argv[optind]);
477 }
478
479 // struct timespec begin;
480
481 // time_now(&begin);
482
483 otfcc_SplineFontContainer *sfnt;
484 loggedStep("Read SFNT") {
485 logProgress("From file %s", inPath);
486 FILE *file = u8fopen(inPath, "rb");
487 sfnt = otfcc_readSFNT(file);
488 if (!sfnt || sfnt->count == 0) {
489 logError("Cannot read SFNT file \"%s\". Exit.\n", inPath);
490 exit(EXIT_FAILURE);
491 }
492 if (ttcindex >= sfnt->count) {
493 logError("Subfont index %d out of range for \"%s\" (0 -- %d). Exit.\n", ttcindex,
494 inPath, (sfnt->count - 1));
495 exit(EXIT_FAILURE);
496 }
497 //logStepTime;
498 }
499
500 otfcc_Font *font;
501 loggedStep("Read Font") {
502 otfcc_IFontBuilder *reader = otfcc_newOTFReader();
503 font = reader->read(sfnt, ttcindex, options);
504 if (!font) {
505 logError("Font structure broken or corrupted \"%s\". Exit.\n", inPath);
506 exit(EXIT_FAILURE);
507 }
508 reader->free(reader);
509 if (sfnt) otfcc_deleteSFNT(sfnt);
510 //logStepTime;
511 }
512 loggedStep("Consolidate") {
513 otfcc_iFont.consolidate(font, options);
514 //logStepTime;
515 }
516 json_value *root;
517 loggedStep("Dump") {
518 otfcc_IFontSerializer *dumper = otfcc_newJsonWriter();
519 root = (json_value *)dumper->serialize(font, options);
520 if (!root) {
521 logError("Font structure broken or corrupted \"%s\". Exit.\n", inPath);
522 exit(EXIT_FAILURE);
523 }
524 //logStepTime;
525 dumper->free(dumper);
526 }
527
528 char *buf;
529 size_t buflen;
530 loggedStep("Serialize to JSON") {
531 json_serialize_opts jsonOptions;
532 jsonOptions.mode = json_serialize_mode_packed;
533 jsonOptions.opts = 0;
534 jsonOptions.indent_size = 4;
535 #ifdef WIN32
536 if (show_pretty || (!outputPath && isatty(fileno(stdout)))) {
537 jsonOptions.mode = json_serialize_mode_multiline;
538 }
539 #else
540 if (show_pretty || (!outputPath && isatty(STDOUT_FILENO))) {
541 jsonOptions.mode = json_serialize_mode_multiline;
542 }
543 #endif
544 if (show_ugly) jsonOptions.mode = json_serialize_mode_packed;
545 buflen = json_measure_ex(root, jsonOptions);
546 buf = calloc(1, buflen);
547 json_serialize_ex(buf, root, jsonOptions);
548 //logStepTime;
549 }
550
551 loggedStep("Output") {
552 if (outputPath) {
553 FILE *outputFile = u8fopen(outputPath, "wb");
554 if (!outputFile) {
555 logError("Cannot write to file \"%s\". Exit.", outputPath);
556 exit(EXIT_FAILURE);
557 }
558 if (add_bom) {
559 fputc(0xEF, outputFile);
560 fputc(0xBB, outputFile);
561 fputc(0xBF, outputFile);
562 }
563 size_t actualLen = buflen - 1;
564 while (!buf[actualLen])
565 actualLen -= 1;
566 fwrite(buf, sizeof(char), actualLen + 1, outputFile);
567 fclose(outputFile);
568 } else {
569 #ifdef WIN32
570 if (isatty(fileno(stdout))) {
571 LPWSTR pwStr;
572 DWORD dwNum = widen_utf8(buf, &pwStr);
573 DWORD actual = 0;
574 DWORD written = 0;
575 const DWORD chunk = 0x10000;
576 while (written < dwNum) {
577 DWORD len = dwNum - written;
578 if (len > chunk) len = chunk;
579 WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), pwStr + written, len, &actual,
580 NULL);
581 written += len;
582 }
583 free(pwStr);
584 } else {
585 if (!no_bom) {
586 fputc(0xEF, stdout);
587 fputc(0xBB, stdout);
588 fputc(0xBF, stdout);
589 }
590 fputs(buf, stdout);
591 }
592 #else
593 (void)(no_bom);
594 if (add_bom) {
595 fputc(0xEF, stdout);
596 fputc(0xBB, stdout);
597 fputc(0xBF, stdout);
598 }
599 fputs(buf, stdout);
600 #endif
601 }
602 //logStepTime;
603 }
604
605 loggedStep("Finalize") {
606 free(buf);
607 if (font) otfcc_iFont.free(font);
608 if (root) json_builder_free(root);
609 if (inPath) sdsfree(inPath);
610 if (outputPath) sdsfree(outputPath);
611 //logStepTime;
612 }
613 otfcc_deleteOptions(options);
614
615 return 0;
616 }
617
618
619