initial commit

This commit is contained in:
Carly Ho 2024-03-10 13:12:25 -05:00
commit b5b60cd567
3 changed files with 65 additions and 0 deletions

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
node_modules
*.bitsyfont
*.lock
*.png

48
bitfont.js Normal file
View File

@ -0,0 +1,48 @@
const Jimp = require("jimp");
const prompt = require("prompt");
const sizeOf = require("image-size");
const fs = require("node:fs");
const file = process.argv[2];
const charWidth = parseInt(process.argv[3]) || 8;
const charHeight = parseInt(process.argv[4]) || 8;
const dimensions = sizeOf(file);
const imgWidth = dimensions.width;
const imgHeight = dimensions.height;
Jimp.read(file, async (_err, img) => {
let charArray = [];
let row;
let buffer = ``;
prompt.start();
const { fontName } = await prompt.get({name: 'fontName', description: 'Font Name', hidden: false, type: 'string', default: 'My Bitsy Font'});
buffer += `FONT ${fontName}\n`;
buffer += `SIZE ${charWidth} ${charHeight}\n`;
console.log(buffer);
for (let y = 0; y < imgHeight; y += 1) {
row = [];
for (let x = 0; x < imgWidth; x += 1) {
row.push(img.getPixelColor(x, y) == 255 ? 1 : 0);
}
charArray.push(row);
}
let character;
for (y = 0; y < imgHeight; y += charHeight) {
for (x = 0; x < imgWidth; x += charWidth) {
character = charArray.slice(y, y + charHeight).map(line => line.slice(x, x + charWidth).join(""));
character.forEach((line) => console.log(line.replace(/1/g, "\x1b[30m1\x1b[0m")));
let { code } = await prompt.get({name: 'code', description: 'Character Code', hidden: false, type: 'string', required: true});
buffer += `CHAR ${code}\n`;
buffer += character.join("\n");
}
}
const { path } = await prompt.get({name: 'path', description: 'Path to Write', hidden: false, type: 'string', default: 'my_bitsy_font.bitsyfont'});
fs.writeFile(path, buffer, (err) => {
if (err) {
console.error(err);
} else {
console.log(`Font written to ${path}!`);
}
});
});

13
package.json Normal file
View File

@ -0,0 +1,13 @@
{
"name": "bitfont",
"version": "1.0.0",
"description": "converts a png file into a bitsy font",
"main": "bitfont.js",
"author": "Carly Ho",
"license": "MIT",
"dependencies": {
"image-size": "^1.1.1",
"jimp": "^0.22.12",
"prompt": "^1.3.0"
}
}