| 1 |
#include "otfcc/sfnt.h" |
| 2 |
#include "otfcc/font.h" |
| 3 |
#include "otfcc/sfnt-builder.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 otfccbuild, version %d.%d.%d.\n", MAIN_VER, SECONDARY_VER, |
| 21 |
PATCH_VER); |
| 22 |
} |
| 23 |
static void printHelp() { |
| 24 |
fprintf(stdout, |
| 25 |
"\n" |
| 26 |
"Usage : otfccbuild [OPTIONS] [input.json] -o output.[ttf|otf]\n\n" |
| 27 |
" input.json : Path to input file. When absent the input will be\n" |
| 28 |
" read from the STDIN.\n\n" |
| 29 |
" -h, --help : Display this help message and exit.\n" |
| 30 |
" -v, --version : Display version information and exit.\n" |
| 31 |
" -o <file> : Set output file path to <file>.\n" |
| 32 |
" -s, --dummy-dsig : Include an empty DSIG table in the font. For some\n" |
| 33 |
" Microsoft applications, DSIG is required to enable\n" |
| 34 |
" OpenType features.\n" |
| 35 |
" -O<n> : Specify the level for optimization.\n" |
| 36 |
" -O0 Turn off any optimization.\n" |
| 37 |
" -O1 Default optimization.\n" |
| 38 |
" -O2 More aggressive optimizations for web font. In this\n" |
| 39 |
" level, the following options will be set:\n" |
| 40 |
" --merge-features\n" |
| 41 |
" --short-post\n" |
| 42 |
" --subroutinize\n" |
| 43 |
" -O3 Most aggressive opptimization strategy will be\n" |
| 44 |
" used. In this level, these options will be set:\n" |
| 45 |
" --force-cid\n" |
| 46 |
" --ignore-glyph-order\n" |
| 47 |
" --verbose : Show more information when building.\n" |
| 48 |
" -q, --quiet : Be silent when building.\n\n" |
| 49 |
" --ignore-hints : Ignore the hinting information in the input.\n" |
| 50 |
" --keep-average-char-width : Keep the OS/2.xAvgCharWidth value from the input\n" |
| 51 |
" instead of stating the average width of glyphs.\n" |
| 52 |
" Useful when creating a monospaced font.\n" |
| 53 |
" --keep-unicode-ranges : Keep the OS/2.ulUnicodeRange[1-4] as-is.\n" |
| 54 |
" --keep-modified-time : Keep the head.modified time in the json, instead of\n" |
| 55 |
" using current time.\n\n" |
| 56 |
" --short-post : Don't export glyph names in the result font.\n" |
| 57 |
" --ignore-glyph-order, -i : Ignore the glyph order information in the input.\n" |
| 58 |
" --keep-glyph-order, -k : Keep the glyph order information in the input.\n" |
| 59 |
" Use to preserve glyph order under -O2 and -O3.\n" |
| 60 |
" --dont-ignore-glyph-order : Same as --keep-glyph-order.\n" |
| 61 |
" --merge-features : Merge duplicate OpenType feature definitions.\n" |
| 62 |
" --dont-merge-features : Keep duplicate OpenType feature definitions.\n" |
| 63 |
" --merge-lookups : Merge duplicate OpenType lookups.\n" |
| 64 |
" --dont-merge-lookups : Keep duplicate OpenType lookups.\n" |
| 65 |
" --force-cid : Convert name-keyed CFF OTF into CID-keyed.\n" |
| 66 |
" --subroutinize : Subroutinize CFF table.\n" |
| 67 |
" --stub-cmap4 : Create a stub `cmap` format 4 subtable if format\n" |
| 68 |
" 12 subtable is present.\n" |
| 69 |
"\n"); |
| 70 |
} |
| 71 |
static void readEntireFile(char *inPath, char **_buffer, long *_length) { |
| 72 |
char *buffer = NULL; |
| 73 |
long length = 0; |
| 74 |
FILE *f = u8fopen(inPath, "rb"); |
| 75 |
if (!f) { |
| 76 |
fprintf(stderr, "Cannot read JSON file \"%s\". Exit.\n", inPath); |
| 77 |
exit(EXIT_FAILURE); |
| 78 |
} |
| 79 |
fseek(f, 0, SEEK_END); |
| 80 |
length = ftell(f); |
| 81 |
fseek(f, 0, SEEK_SET); |
| 82 |
buffer = malloc(length); |
| 83 |
if (buffer) { fread(buffer, 1, length, f); } |
| 84 |
fclose(f); |
| 85 |
|
| 86 |
if (!buffer) { |
| 87 |
fprintf(stderr, "Cannot read JSON file \"%s\". Exit.\n", inPath); |
| 88 |
exit(EXIT_FAILURE); |
| 89 |
} |
| 90 |
*_buffer = buffer; |
| 91 |
*_length = length; |
| 92 |
} |
| 93 |
|
| 94 |
static void readEntireStdin(char **_buffer, long *_length) { |
| 95 |
#ifdef _WIN32 |
| 96 |
freopen(NULL, "rb", stdin); |
| 97 |
#endif |
| 98 |
static const long BUF_SIZE = 0x400000; |
| 99 |
static const long BUF_MIN = 0x1000; |
| 100 |
char *buffer = malloc(BUF_SIZE); |
| 101 |
long length = 0; |
| 102 |
long remain = BUF_SIZE; |
| 103 |
while (!feof(stdin)) { |
| 104 |
if (remain <= BUF_MIN) { |
| 105 |
remain += (length >> 1) & 0xFFFFFF; |
| 106 |
buffer = realloc(buffer, length + remain); |
| 107 |
} |
| 108 |
|
| 109 |
fgets(buffer + length, remain, stdin); |
| 110 |
long n = (long)strlen(buffer + length); |
| 111 |
length += n; |
| 112 |
remain -= n; |
| 113 |
} |
| 114 |
*_buffer = buffer; |
| 115 |
*_length = length; |
| 116 |
} |
| 117 |
|
| 118 |
#ifdef _WIN32 |
| 119 |
int main() { |
| 120 |
int argc; |
| 121 |
char **argv; |
| 122 |
get_argv_utf8(&argc, &argv); |
| 123 |
#else |
| 124 |
int main(int argc, char *argv[]) { |
| 125 |
#endif |
| 126 |
|
| 127 |
struct timespec begin; |
| 128 |
time_now(&begin); |
| 129 |
|
| 130 |
bool show_help = false; |
| 131 |
bool show_version = false; |
| 132 |
sds outputPath = NULL; |
| 133 |
sds inPath = NULL; |
| 134 |
int option_index = 0; |
| 135 |
int c; |
| 136 |
|
| 137 |
otfcc_Options *options = otfcc_newOptions(); |
| 138 |
options->logger = otfcc_newLogger(otfcc_newStdErrTarget()); |
| 139 |
options->logger->indent(options->logger, "otfccbuild"); |
| 140 |
otfcc_Options_optimizeTo(options, 1); |
| 141 |
|
| 142 |
struct option longopts[] = {{"version", no_argument, NULL, 'v'}, |
| 143 |
{"help", no_argument, NULL, 'h'}, |
| 144 |
{"time", no_argument, NULL, 0}, |
| 145 |
{"ignore-glyph-order", no_argument, NULL, 0}, |
| 146 |
{"keep-glyph-order", no_argument, NULL, 0}, |
| 147 |
{"dont-ignore-glyph-order", no_argument, NULL, 0}, |
| 148 |
{"ignore-hints", no_argument, NULL, 0}, |
| 149 |
{"keep-average-char-width", no_argument, NULL, 0}, |
| 150 |
{"keep-unicode-ranges", no_argument, NULL, 0}, |
| 151 |
{"keep-modified-time", no_argument, NULL, 0}, |
| 152 |
{"merge-lookups", no_argument, NULL, 0}, |
| 153 |
{"merge-features", no_argument, NULL, 0}, |
| 154 |
{"dont-merge-lookups", no_argument, NULL, 0}, |
| 155 |
{"dont-merge-features", no_argument, NULL, 0}, |
| 156 |
{"short-post", no_argument, NULL, 0}, |
| 157 |
{"force-cid", no_argument, NULL, 0}, |
| 158 |
{"subroutinize", no_argument, NULL, 0}, |
| 159 |
{"stub-cmap4", no_argument, NULL, 0}, |
| 160 |
{"dummy-dsig", no_argument, NULL, 's'}, |
| 161 |
{"ship", no_argument, NULL, 0}, |
| 162 |
{"verbose", no_argument, NULL, 0}, |
| 163 |
{"quiet", no_argument, NULL, 0}, |
| 164 |
{"optimize", required_argument, NULL, 'O'}, |
| 165 |
{"output", required_argument, NULL, 'o'}, |
| 166 |
{0, 0, 0, 0}}; |
| 167 |
|
| 168 |
while ((c = getopt_long(argc, argv, "vhqskiO:o:", longopts, &option_index)) != (-1)) { |
| 169 |
switch (c) { |
| 170 |
case 0: |
| 171 |
/* If this option set a flag, do nothing else now. */ |
| 172 |
if (longopts[option_index].flag != 0) { |
| 173 |
break; |
| 174 |
} else if (strcmp(longopts[option_index].name, "time") == 0) { |
| 175 |
} else if (strcmp(longopts[option_index].name, "ignore-hints") == 0) { |
| 176 |
options->ignore_hints = true; |
| 177 |
} else if (strcmp(longopts[option_index].name, "keep-average-char-width") == 0) { |
| 178 |
options->keep_average_char_width = true; |
| 179 |
} else if (strcmp(longopts[option_index].name, "keep-unicode-ranges") == 0) { |
| 180 |
options->keep_unicode_ranges = true; |
| 181 |
} else if (strcmp(longopts[option_index].name, "keep-modified-time") == 0) { |
| 182 |
options->keep_modified_time = true; |
| 183 |
} else if (strcmp(longopts[option_index].name, "merge-features") == 0) { |
| 184 |
options->merge_features = true; |
| 185 |
} else if (strcmp(longopts[option_index].name, "merge-lookups") == 0) { |
| 186 |
options->merge_lookups = true; |
| 187 |
} else if (strcmp(longopts[option_index].name, "dont-merge-features") == 0) { |
| 188 |
options->merge_features = false; |
| 189 |
} else if (strcmp(longopts[option_index].name, "dont-merge-lookups") == 0) { |
| 190 |
options->merge_lookups = false; |
| 191 |
} else if (strcmp(longopts[option_index].name, "ignore-glyph-order") == 0) { |
| 192 |
options->ignore_glyph_order = true; |
| 193 |
} else if (strcmp(longopts[option_index].name, "keep-glyph-order") == 0) { |
| 194 |
options->ignore_glyph_order = false; |
| 195 |
} else if (strcmp(longopts[option_index].name, "dont-keep-glyph-order") == 0) { |
| 196 |
options->ignore_glyph_order = false; |
| 197 |
} else if (strcmp(longopts[option_index].name, "short-post") == 0) { |
| 198 |
options->short_post = true; |
| 199 |
} else if (strcmp(longopts[option_index].name, "force-cid") == 0) { |
| 200 |
options->force_cid = true; |
| 201 |
} else if (strcmp(longopts[option_index].name, "subroutinize") == 0) { |
| 202 |
options->cff_doSubroutinize = true; |
| 203 |
} else if (strcmp(longopts[option_index].name, "stub-cmap4") == 0) { |
| 204 |
options->stub_cmap4 = true; |
| 205 |
} else if (strcmp(longopts[option_index].name, "ship") == 0) { |
| 206 |
options->ignore_glyph_order = true; |
| 207 |
options->short_post = true; |
| 208 |
options->dummy_DSIG = true; |
| 209 |
} else if (strcmp(longopts[option_index].name, "verbose") == 0) { |
| 210 |
options->verbose = true; |
| 211 |
} else if (strcmp(longopts[option_index].name, "quiet") == 0) { |
| 212 |
options->quiet = true; |
| 213 |
} |
| 214 |
break; |
| 215 |
case 'v': |
| 216 |
show_version = true; |
| 217 |
break; |
| 218 |
case 'h': |
| 219 |
show_help = true; |
| 220 |
break; |
| 221 |
case 'k': |
| 222 |
options->ignore_glyph_order = false; |
| 223 |
break; |
| 224 |
case 'i': |
| 225 |
options->ignore_glyph_order = true; |
| 226 |
break; |
| 227 |
case 'o': |
| 228 |
outputPath = sdsnew(optarg); |
| 229 |
break; |
| 230 |
case 's': |
| 231 |
options->dummy_DSIG = true; |
| 232 |
break; |
| 233 |
case 'q': |
| 234 |
options->quiet = true; |
| 235 |
break; |
| 236 |
case 'O': |
| 237 |
otfcc_Options_optimizeTo(options, atoi(optarg)); |
| 238 |
break; |
| 239 |
} |
| 240 |
} |
| 241 |
options->logger->setVerbosity(options->logger, |
| 242 |
options->quiet ? 0 : options->verbose ? 0xFF : 1); |
| 243 |
if (show_help) { |
| 244 |
printInfo(); |
| 245 |
printHelp(); |
| 246 |
return 0; |
| 247 |
} |
| 248 |
if (show_version) { |
| 249 |
printInfo(); |
| 250 |
return 0; |
| 251 |
} |
| 252 |
|
| 253 |
if (optind >= argc) { |
| 254 |
inPath = NULL; // read from STDIN |
| 255 |
} else { |
| 256 |
inPath = sdsnew(argv[optind]); // read from file |
| 257 |
} |
| 258 |
if (!outputPath) { |
| 259 |
logError("Unable to build OpenType font tile : output path not " |
| 260 |
"specified. Exit.\n"); |
| 261 |
printHelp(); |
| 262 |
exit(EXIT_FAILURE); |
| 263 |
} |
| 264 |
|
| 265 |
char *buffer; |
| 266 |
long length; |
| 267 |
loggedStep("Load file") { |
| 268 |
if (inPath) { |
| 269 |
loggedStep("Load from file %s", inPath) { |
| 270 |
readEntireFile(inPath, &buffer, &length); |
| 271 |
sdsfree(inPath); |
| 272 |
} |
| 273 |
} else { |
| 274 |
loggedStep("Load from stdin") { |
| 275 |
readEntireStdin(&buffer, &length); |
| 276 |
} |
| 277 |
} |
| 278 |
logStepTime; |
| 279 |
} |
| 280 |
|
| 281 |
json_value *jsonRoot = NULL; |
| 282 |
loggedStep("Parse into JSON") { |
| 283 |
jsonRoot = json_parse(buffer, length); |
| 284 |
free(buffer); |
| 285 |
logStepTime; |
| 286 |
if (!jsonRoot) { |
| 287 |
logError("Cannot parse JSON file \"%s\". Exit.\n", inPath); |
| 288 |
exit(EXIT_FAILURE); |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
otfcc_Font *font; |
| 293 |
loggedStep("Parse") { |
| 294 |
otfcc_IFontBuilder *parser = otfcc_newJsonReader(); |
| 295 |
font = parser->read(jsonRoot, 0, options); |
| 296 |
if (!font) { |
| 297 |
logError("Cannot parse JSON file \"%s\" as a font. Exit.\n", inPath); |
| 298 |
exit(EXIT_FAILURE); |
| 299 |
} |
| 300 |
parser->free(parser); |
| 301 |
json_value_free(jsonRoot); |
| 302 |
logStepTime; |
| 303 |
} |
| 304 |
loggedStep("Consolidate") { |
| 305 |
otfcc_iFont.consolidate(font, options); |
| 306 |
logStepTime; |
| 307 |
} |
| 308 |
loggedStep("Build") { |
| 309 |
otfcc_IFontSerializer *writer = otfcc_newOTFWriter(); |
| 310 |
caryll_Buffer *otf = (caryll_Buffer *)writer->serialize(font, options); |
| 311 |
loggedStep("Write to file") { |
| 312 |
FILE *outfile = u8fopen(outputPath, "wb"); |
| 313 |
if (!outfile) { |
| 314 |
logError("Cannot write to file \"%s\". Exit.\n", outputPath); |
| 315 |
exit(EXIT_FAILURE); |
| 316 |
} |
| 317 |
fwrite(otf->data, sizeof(uint8_t), buflen(otf), outfile); |
| 318 |
fclose(outfile); |
| 319 |
} |
| 320 |
logStepTime; |
| 321 |
buffree(otf), writer->free(writer), otfcc_iFont.free(font), sdsfree(outputPath); |
| 322 |
} |
| 323 |
otfcc_deleteOptions(options); |
| 324 |
|
| 325 |
return 0; |
| 326 |
} |