|
| 1 | +#!/usr/bin/node |
| 2 | + |
| 3 | +var fontconverter = require("../fontconverter.js"); |
| 4 | +var RANGES = fontconverter.getRanges(); |
| 5 | + |
| 6 | +var fontInfo = {}; |
| 7 | +var options = {}; |
| 8 | + |
| 9 | +// Scan Args |
| 10 | +for (var i=2;i<process.argv.length;i++) { |
| 11 | + var arg = process.argv[i]; |
| 12 | + console.log(arg); |
| 13 | + if (arg=="--help") { |
| 14 | + console.log(`Espruino Font Converter CLI (BETA) |
| 15 | +------------------------------- |
| 16 | +
|
| 17 | +USAGE: |
| 18 | +
|
| 19 | +cli/fontconverter.js sourceFile |
| 20 | + --help - show this message |
| 21 | + --debug - print debug info for loaded font |
| 22 | + --height # - set the height of the font |
| 23 | + --range ${Object.keys(RANGES).join("|")} (default=ASCII) |
| 24 | + - which range of characters should be included |
| 25 | +
|
| 26 | + --ojs fn.js - save the font as JS - only works for <1000 chars |
| 27 | + --oh fn.h - save the font as a C header file (uses data for each char including blank ones) |
| 28 | + --opbf fn.pbf - save a binary PBF file |
| 29 | + // eg. require("fs").writeFileSync("font.pbf", Buffer.from(font.getPBF())) |
| 30 | + --opbfc fn - save a binary PBF file, but as a C file that can be included in Espruino |
| 31 | + (fn.c and fn.h are written) |
| 32 | +
|
| 33 | +Input font can be: |
| 34 | + ...json - font made using https://www.pentacom.jp/pentacom/bitfontmaker2/ |
| 35 | + ...pbff - pebble font format (text mode) |
| 36 | + ...png - 8x8 font map |
| 37 | + ...bdf - Linux font format |
| 38 | + `); |
| 39 | + } else if (arg=="--debug") { |
| 40 | + options.debug = true; |
| 41 | + } else if (arg=="--height") { |
| 42 | + fontInfo.height = 0|process.argv[++i]; |
| 43 | + } else if (arg=="--range") { |
| 44 | + options.rangeId = process.argv[++i]; |
| 45 | + var range = RANGES[options.rangeId]; |
| 46 | + if (!range) throw new Error(`Range ID ${options.rangeId} not found`); |
| 47 | + fontInfo.range = range.range; |
| 48 | + } else if (arg=="--ojs") { |
| 49 | + options.ojs = process.argv[++i]; |
| 50 | + } else if (arg=="--oh") { |
| 51 | + options.oh = process.argv[++i]; |
| 52 | + } else if (arg=="--opbf") { |
| 53 | + options.opbf = process.argv[++i]; |
| 54 | + } else if (arg=="--opbfc") { |
| 55 | + options.opbfc = process.argv[++i]; |
| 56 | + } else if (arg.startsWith("-")) { |
| 57 | + throw new Error(`Unknown argument '${arg}'`); |
| 58 | + } else { |
| 59 | + if (fontInfo.fn) throw new Error(`Source file already specified (${fontInfo.fn})`); |
| 60 | + fontInfo.fn = arg; |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | + |
| 65 | +// Checks |
| 66 | +if (!fontInfo.fn) throw new Error("No font source file specified"); |
| 67 | +// Do stuff |
| 68 | +var font = fontconverter.load(fontInfo); |
| 69 | +if (options.debug) { |
| 70 | + font.debugChars(); |
| 71 | + font.debugPixelsUsed(); |
| 72 | +} |
| 73 | +if (options.ojs) |
| 74 | + require("fs").writeFileSync(options.ojs, Buffer.from(font.getJS())) |
| 75 | +if (options.oh) |
| 76 | + require("fs").writeFileSync(options.oh, Buffer.from(font.getHeaderFile())) |
| 77 | +if (options.opbf) |
| 78 | + require("fs").writeFileSync(options.opbf, Buffer.from(font.getPBF())) |
| 79 | +if (options.opbfc) |
| 80 | + font.getPBFAsC({filename:options.opbfc}); |
0 commit comments