Skip to content

Commit 7b54655

Browse files
committed
add CLI font converter tool
1 parent eb1a20c commit 7b54655

File tree

2 files changed

+91
-3
lines changed

2 files changed

+91
-3
lines changed

cli/fontconverter.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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});

fontconverter.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,13 @@ function Font(info) {
9494
this.name = info.name;
9595
this.id = this.name ? this.name.replace(/[^A-Za-z0-9]/g,"") : "Unknown";
9696
this.fn = info.fn;
97-
this.height = info.height;
97+
this.height = 0|info.height;
9898
this.bpp = info.bpp||1;
9999

100100
this.range = info.range;
101101
if (!this.range)
102102
this.range = getRanges().ASCII.range;
103103

104-
105104
this.fixedWidth = !!info.fixedWidth;
106105
this.fullHeight = !!info.fullHeight; // output fonts at the full height available
107106
this.glyphPadX = 1; // padding at the end of glyphs (needed for old-style JS fonts)
@@ -324,6 +323,8 @@ function loadPBFF(fontInfo) {
324323
if (l.startsWith("version")) {
325324
} else if (l.startsWith("fallback")) {
326325
} else if (l.startsWith("line-height")) {
326+
if (!fontInfo.fmHeight) // if no height specified
327+
fontInfo.fmHeight = 0|l.split(" ")[1];
327328
} else if (l.startsWith("glyph")) {
328329
current = {};
329330
current.idx = parseInt(l.trim().split(" ")[1]);
@@ -749,6 +750,8 @@ Font.prototype.getPBF = function() {
749750
*/
750751
Font.prototype.getPBFAsC = function(options) {
751752
var pbf = this.getPBF();
753+
options = options||{};
754+
if (!options.path) options.path="";
752755
require("fs").writeFileSync(options.path+options.filename+".h", `/*
753756
* This file is part of Espruino, a JavaScript interpreter for Microcontrollers
754757
*
@@ -893,7 +896,12 @@ function getRanges() {
893896
fn : "renaissance_28.pbff",
894897
height : 28, // actual used height of font map
895898
range : [ min:32, max:255 ]
896-
yOffset : 4,
899+
}
900+
901+
or:
902+
903+
{
904+
fn : "font.bdf", // Linux bitmap font format
897905
}
898906
899907
or for a font made using https://www.pentacom.jp/pentacom/bitfontmaker2/

0 commit comments

Comments
 (0)