Skip to content

Instantly share code, notes, and snippets.

@metaquanta
Last active July 9, 2017 05:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save metaquanta/5d603e446682144d2ebc44f6aa993104 to your computer and use it in GitHub Desktop.
Save metaquanta/5d603e446682144d2ebc44f6aa993104 to your computer and use it in GitHub Desktop.
A script that turns a BDF bitmap font into a dictionary of bitmaps
#!/usr/bin/env node
const regrep = require('stream-replace');
/* Stream in, stream out.
The output will look something like this:
{ "${code_0}": ["${hex^0_0}", ..., "${hex^0_n}"],
"${code_1}": [...],
....
"${code_n}": ["${hex^n_0}", ..., "${hex^n_n}"]
}
*/
process.stdin.pipe(
regrep(/^[\s\S]*CHARS [0-9]*\n/, '{')
).pipe(
regrep(/\nSTARTCHAR [0-9]*\nENCODING ([0-9]*)\n/g, "\n \"$1\":[\n")
).pipe(
regrep(/\[\n([A-Z]* .*\n)*BITMAP/g, "[")
).pipe(
regrep(/\nENDCHAR\n/g, "\n ],")
).pipe(
regrep(/\n([A-F0-9][A-F0-9])$/mg, "\"$1\",")
).pipe(
regrep(/,\n \]/g, "]")
).pipe(
regrep(/,\nENDFONT/, "\n}")
).pipe(process.stdout);
{
"0":["00","00","00","00","00","00","00","00","00","00","00","00"],
"1":["00","00","00","00","00","00","00","00","00","00","00","00"],
"2":["00","00","00","00","00","00","00","00","00","00","00","00"],
"3":["00","00","00","00","00","00","00","00","00","00","00","00"],
"4":["00","00","00","00","00","00","00","00","00","00","00","00"],
"5":["00","00","00","00","00","00","00","00","00","00","00","00"],
"6":["00","00","00","00","00","00","00","00","00","00","00","00"],
"7":["00","00","00","00","00","00","00","00","00","00","00","00"],
"8":["00","00","00","00","00","00","00","00","00","00","00","00"],
"9":["00","00","00","00","00","00","00","00","00","00","00","00"],
"10":["00","00","00","00","00","00","00","00","00","00","00","00"],
"11":["00","00","00","00","00","00","00","00","00","00","00","00"],
"12":["00","00","00","00","00","00","00","00","00","00","00","00"],
"13":["00","00","00","00","00","00","00","00","00","00","00","00"],
"14":["00","00","00","00","00","00","00","00","00","00","00","00"],
"15":["00","00","00","00","00","00","00","00","00","00","00","00"],
"16":["00","00","00","00","00","00","00","00","00","00","00","00"],
"17":["00","00","00","00","00","00","00","00","00","00","00","00"],
"18":["00","00","00","00","00","00","00","00","00","00","00","00"],
"19":["00","00","00","00","00","00","00","00","00","00","00","00"],
"20":["00","00","00","00","00","00","00","00","00","00","00","00"],
"21":["00","00","00","00","00","00","00","00","00","00","00","00"],
"22":["00","00","00","00","00","00","00","00","00","00","00","00"],
"23":["00","00","00","00","00","00","00","00","00","00","00","00"],
"24":["00","00","00","00","00","00","00","00","00","00","00","00"],
"25":["00","00","00","00","00","00","00","00","00","00","00","00"],
"26":["00","00","00","00","00","00","00","00","00","00","00","00"],
"27":["00","00","00","00","00","00","00","00","00","00","00","00"],
"28":["00","00","00","00","00","00","00","00","00","00","00","00"],
"29":["00","00","00","00","00","00","00","00","00","00","00","00"],
"30":["00","00","00","00","00","00","00","00","00","00","00","00"],
"31":["00","00","00","00","00","00","00","00","00","00","00","00"],
"32":["00","00","00","00","00","00","00","00","00","00","00","00"],
"33":["00","00","20","20","20","20","20","20","00","20","00","00"],
"34":["00","50","50","50","00","00","00","00","00","00","00","00"],
"35":["00","00","50","F8","50","F8","50","00","00","00","00","00"],
"36":["00","20","70","A8","A0","70","28","28","A8","70","20","00"],
"37":["00","00","78","A8","A8","50","30","68","A8","90","00","00"],
"38":["00","00","60","90","A0","40","A0","A8","90","68","00","00"],
"39":["00","20","20","20","00","00","00","00","00","00","00","00"],
"40":["00","10","20","40","40","40","40","40","40","20","10","00"],
"41":["00","40","20","10","10","10","10","10","10","20","40","00"],
"42":["00","00","20","A8","70","A8","20","00","00","00","00","00"],
"43":["00","00","00","00","20","20","F8","20","20","00","00","00"],
"44":["00","00","00","00","00","00","00","00","60","60","20","40"],
"45":["00","00","00","00","00","00","70","00","00","00","00","00"],
"46":["00","00","00","00","00","00","00","00","30","30","00","00"],
"47":["00","00","08","08","10","10","20","20","40","40","80","80"],
"48":["00","00","70","88","98","A8","C8","88","88","70","00","00"],
"49":["00","00","20","E0","20","20","20","20","20","F8","00","00"],
"50":["00","00","70","88","08","10","20","40","80","F8","00","00"],
"51":["00","00","70","88","08","30","08","08","88","70","00","00"],
"52":["00","00","10","30","50","90","F8","10","10","38","00","00"],
"53":["00","00","F8","80","80","F0","08","08","88","70","00","00"],
"54":["00","00","70","80","80","F0","88","88","88","70","00","00"],
"55":["00","00","F8","08","08","10","20","20","20","20","00","00"],
"56":["00","00","70","88","88","70","88","88","88","70","00","00"],
"57":["00","00","70","88","88","88","78","08","08","70","00","00"],
"58":["00","00","00","00","30","30","00","00","30","30","00","00"],
"59":["00","00","00","00","60","60","00","00","60","60","20","40"],
"60":["00","00","08","10","20","40","20","10","08","00","00","00"],
"61":["00","00","00","00","F8","00","F8","00","00","00","00","00"],
"62":["00","00","40","20","10","08","10","20","40","00","00","00"],
"63":["00","00","70","88","08","08","10","20","00","20","00","00"],
"64":["00","00","70","88","B8","A8","B8","80","80","78","00","00"],
"65":["00","00","20","50","50","88","88","F8","88","88","00","00"],
"66":["00","00","F0","88","88","F0","88","88","88","F0","00","00"],
"67":["00","00","70","88","80","80","80","80","88","70","00","00"],
"68":["00","00","F0","88","88","88","88","88","88","F0","00","00"],
"69":["00","00","F8","80","80","F0","80","80","80","F8","00","00"],
"70":["00","00","F8","80","80","F0","80","80","80","80","00","00"],
"71":["00","00","70","88","80","80","98","88","88","70","00","00"],
"72":["00","00","88","88","88","F8","88","88","88","88","00","00"],
"73":["00","00","F8","20","20","20","20","20","20","F8","00","00"],
"74":["00","00","08","08","08","08","08","88","88","70","00","00"],
"75":["00","00","88","90","A0","C0","C0","A0","90","88","00","00"],
"76":["00","00","80","80","80","80","80","80","80","F8","00","00"],
"77":["00","00","88","D8","A8","A8","88","88","88","88","00","00"],
"78":["00","00","88","C8","A8","98","88","88","88","88","00","00"],
"79":["00","00","70","88","88","88","88","88","88","70","00","00"],
"80":["00","00","F0","88","88","F0","80","80","80","80","00","00"],
"81":["00","00","70","88","88","88","88","88","A8","70","08","00"],
"82":["00","00","F0","88","88","F0","88","88","88","88","00","00"],
"83":["00","00","70","88","80","70","08","08","88","70","00","00"],
"84":["00","00","F8","20","20","20","20","20","20","20","00","00"],
"85":["00","00","88","88","88","88","88","88","88","70","00","00"],
"86":["00","00","88","88","88","88","50","50","20","20","00","00"],
"87":["00","00","88","88","88","88","A8","A8","D8","88","00","00"],
"88":["00","00","88","88","88","50","20","50","88","88","00","00"],
"89":["00","00","88","88","88","88","50","20","20","20","00","00"],
"90":["00","00","F8","08","10","20","40","80","80","F8","00","00"],
"91":["00","30","20","20","20","20","20","20","20","20","30","00"],
"92":["00","00","40","40","20","20","10","10","08","08","04","04"],
"93":["00","60","20","20","20","20","20","20","20","20","60","00"],
"94":["00","00","20","50","88","00","00","00","00","00","00","00"],
"95":["00","00","00","00","00","00","00","00","00","00","00","FC"],
"96":["00","40","20","00","00","00","00","00","00","00","00","00"],
"97":["00","00","00","00","00","78","88","88","98","68","00","00"],
"98":["00","00","80","80","80","F0","88","88","88","F0","00","00"],
"99":["00","00","00","00","00","70","88","80","80","78","00","00"],
"100":["00","00","08","08","08","78","88","88","88","78","00","00"],
"101":["00","00","00","00","00","70","88","F8","80","78","00","00"],
"102":["00","00","18","20","70","20","20","20","20","20","00","00"],
"103":["00","00","00","00","00","78","88","88","88","78","08","70"],
"104":["00","00","80","80","80","F0","88","88","88","88","00","00"],
"105":["00","00","20","00","00","60","20","20","20","70","00","00"],
"106":["00","00","20","00","00","60","20","20","20","20","20","C0"],
"107":["00","00","80","80","80","90","A0","E0","90","88","00","00"],
"108":["00","00","60","20","20","20","20","20","20","70","00","00"],
"109":["00","00","00","00","00","F0","A8","A8","A8","A8","00","00"],
"110":["00","00","00","00","00","B0","C8","88","88","88","00","00"],
"111":["00","00","00","00","00","70","88","88","88","70","00","00"],
"112":["00","00","00","00","00","F0","88","88","88","F0","80","80"],
"113":["00","00","00","00","00","78","88","88","88","78","08","08"],
"114":["00","00","00","00","00","B0","C8","80","80","80","00","00"],
"115":["00","00","00","00","00","78","80","70","08","F0","00","00"],
"116":["00","00","20","20","20","70","20","20","20","18","00","00"],
"117":["00","00","00","00","00","88","88","88","98","68","00","00"],
"118":["00","00","00","00","00","88","88","50","50","20","00","00"],
"119":["00","00","00","00","00","A8","A8","A8","A8","50","00","00"],
"120":["00","00","00","00","00","88","50","20","50","88","00","00"],
"121":["00","00","00","00","00","88","88","88","88","78","08","70"],
"122":["00","00","00","00","00","F8","10","20","40","F8","00","00"],
"123":["00","10","20","20","20","20","40","20","20","20","20","10"],
"124":["00","20","20","20","20","20","20","20","20","20","20","00"],
"125":["00","40","20","20","20","20","10","20","20","20","20","40"],
"126":["00","00","00","00","68","B0","00","00","00","00","00","00"],
"127":["00","00","00","00","00","00","00","00","00","00","00","00"],
"128":["00","00","00","00","00","00","00","00","00","00","00","00"],
"129":["00","00","00","00","00","00","00","00","00","00","00","00"],
"130":["00","00","00","00","00","00","00","00","40","40","80","00"],
"131":["18","20","20","20","20","70","20","20","20","20","20","C0"],
"132":["00","00","00","00","00","00","00","00","50","50","A0","00"],
"133":["00","00","00","00","00","00","00","00","00","A8","00","00"],
"134":["00","00","20","70","20","20","20","20","00","00","00","00"],
"135":["00","00","20","70","20","20","70","20","00","00","00","00"],
"136":["00","20","50","00","00","00","00","00","00","00","00","00"],
"137":["00","7C","A4","A8","50","20","68","D4","54","28","00","00"],
"138":["50","20","70","88","80","70","08","08","88","70","00","00"],
"139":["00","00","00","00","00","00","40","80","80","40","00","00"],
"140":["00","00","78","A0","A0","B0","A0","A0","A0","78","00","00"],
"141":["00","00","00","00","00","00","00","00","00","00","00","00"],
"142":["00","00","00","00","00","00","00","00","00","00","00","00"],
"143":["00","00","00","00","00","00","00","00","00","00","00","00"],
"144":["00","00","00","00","00","00","00","00","00","00","00","00"],
"145":["00","00","10","20","20","00","00","00","00","00","00","00"],
"146":["00","00","10","10","20","00","00","00","00","00","00","00"],
"147":["00","00","28","50","50","00","00","00","00","00","00","00"],
"148":["00","00","28","28","50","00","00","00","00","00","00","00"],
"149":["00","00","00","70","F8","F8","F8","70","00","00","00","00"],
"150":["00","00","00","00","00","00","70","00","00","00","00","00"],
"151":["00","00","00","00","00","00","FC","00","00","00","00","00"],
"152":["00","68","B0","00","00","00","00","00","00","00","00","00"],
"153":["00","00","F4","5C","5C","00","00","00","00","00","00","00"],
"154":["00","50","20","00","00","78","80","70","08","F0","00","00"],
"155":["00","00","00","00","00","00","80","40","40","80","00","00"],
"156":["00","00","00","00","00","50","A8","B8","A0","58","00","00"],
"157":["00","00","00","00","00","00","00","00","00","00","00","00"],
"158":["00","00","00","00","00","00","00","00","00","00","00","00"],
"159":["50","00","00","88","88","88","50","20","20","20","00","00"],
"160":["00","00","00","00","00","00","00","00","00","00","00","00"],
"161":["00","00","10","00","10","10","10","10","10","10","00","00"],
"162":["00","00","00","00","20","70","A8","A0","A0","78","20","00"],
"163":["00","60","90","80","E0","80","80","80","88","F0","00","00"],
"164":["00","00","00","00","88","70","50","70","88","00","00","00"],
"165":["00","00","88","50","F8","20","F8","20","20","20","00","00"],
"166":["00","20","20","20","20","00","00","20","20","20","20","00"],
"167":["70","88","48","A0","90","88","48","28","90","88","70","00"],
"168":["00","00","50","00","00","00","00","00","00","00","00","00"],
"169":["00","00","E0","10","68","88","88","68","10","E0","00","00"],
"170":["38","48","48","38","00","00","78","00","00","00","00","00"],
"171":["00","00","00","00","00","00","48","90","90","48","00","00"],
"172":["00","00","00","00","00","F8","08","08","00","00","00","00"],
"173":["00","00","00","00","00","00","F8","00","00","00","00","00"],
"174":["00","00","E0","10","C8","A8","A8","C8","B0","00","00","00"],
"175":["00","00","70","00","00","00","00","00","00","00","00","00"],
"176":["00","30","48","48","30","00","00","00","00","00","00","00"],
"177":["00","00","00","20","20","F8","20","20","F8","00","00","00"],
"178":["60","10","20","70","00","00","00","00","00","00","00","00"],
"179":["60","10","30","10","60","00","00","00","00","00","00","00"],
"180":["00","1C","38","00","00","00","00","00","00","00","00","00"],
"181":["00","00","00","00","00","90","90","90","90","E8","80","80"],
"182":["00","78","A8","A8","A8","78","28","28","28","28","00","00"],
"183":["00","00","00","00","00","30","30","00","00","00","00","00"],
"184":["00","00","00","00","00","00","00","00","00","78","38","70"],
"185":["60","20","20","70","00","00","00","00","00","00","00","00"],
"186":["30","48","48","30","00","00","78","00","00","00","00","00"],
"187":["00","00","00","00","00","00","90","48","48","90","00","00"],
"188":["C0","40","44","E8","10","20","48","98","38","08","00","00"],
"189":["C0","40","44","E8","10","20","58","84","08","1C","00","00"],
"190":["C0","20","64","28","D0","20","48","98","38","08","00","00"],
"191":["00","00","20","00","20","40","80","80","88","70","00","00"],
"192":["40","20","20","50","50","88","88","F8","88","88","00","00"],
"193":["10","20","20","50","50","88","88","F8","88","88","00","00"],
"194":["20","50","00","20","50","50","88","F8","88","88","00","00"],
"195":["68","B0","20","50","50","88","88","F8","88","88","00","00"],
"196":["50","00","20","50","50","88","88","F8","88","88","00","00"],
"197":["20","50","20","50","50","88","88","F8","88","88","00","00"],
"198":["00","00","78","A0","A0","F0","A0","A0","A0","B8","00","00"],
"199":["00","00","70","88","80","80","80","80","88","70","20","40"],
"200":["40","20","F8","80","80","F0","80","80","80","F8","00","00"],
"201":["10","20","F8","80","80","F0","80","80","80","F8","00","00"],
"202":["20","50","F8","80","80","F0","80","80","80","F8","00","00"],
"203":["50","00","F8","80","80","F0","80","80","80","F8","00","00"],
"204":["40","20","F8","20","20","20","20","20","20","F8","00","00"],
"205":["10","20","F8","20","20","20","20","20","20","F8","00","00"],
"206":["20","50","00","F8","20","20","20","20","20","F8","00","00"],
"207":["50","00","F8","20","20","20","20","20","20","F8","00","00"],
"208":["00","00","78","44","44","E4","44","44","44","78","00","00"],
"209":["68","B0","88","C8","A8","98","88","88","88","88","00","00"],
"210":["40","20","70","88","88","88","88","88","88","70","00","00"],
"211":["10","20","70","88","88","88","88","88","88","70","00","00"],
"212":["20","50","00","70","88","88","88","88","88","70","00","00"],
"213":["68","B0","70","88","88","88","88","88","88","70","00","00"],
"214":["50","00","70","88","88","88","88","88","88","70","00","00"],
"215":["00","00","00","88","50","20","50","88","00","00","00","00"],
"216":["00","00","70","88","98","A8","C8","88","88","70","00","00"],
"217":["40","20","88","88","88","88","88","88","88","70","00","00"],
"218":["10","20","88","88","88","88","88","88","88","70","00","00"],
"219":["20","50","00","88","88","88","88","88","88","70","00","00"],
"220":["50","00","88","88","88","88","88","88","88","70","00","00"],
"221":["10","20","88","88","88","88","50","20","20","20","00","00"],
"222":["00","00","80","F0","88","88","88","F0","80","80","00","00"],
"223":["00","30","48","88","90","90","88","88","88","B0","80","00"],
"224":["00","20","10","00","00","78","88","88","98","68","00","00"],
"225":["00","10","20","00","00","78","88","88","98","68","00","00"],
"226":["00","20","50","00","00","78","88","88","98","68","00","00"],
"227":["00","68","B0","00","00","78","88","88","98","68","00","00"],
"228":["00","00","50","00","00","78","88","88","98","68","00","00"],
"229":["00","20","50","20","00","78","88","88","98","68","00","00"],
"230":["00","00","00","00","00","70","A8","B8","A0","78","00","00"],
"231":["00","00","00","00","00","70","88","80","80","78","20","40"],
"232":["00","40","20","00","00","70","88","F8","80","78","00","00"],
"233":["00","10","20","00","00","70","88","F8","80","78","00","00"],
"234":["00","20","50","00","00","70","88","F8","80","78","00","00"],
"235":["00","00","50","00","00","70","88","F8","80","78","00","00"],
"236":["00","40","20","00","00","60","20","20","20","70","00","00"],
"237":["00","10","20","00","00","60","20","20","20","70","00","00"],
"238":["00","20","50","00","00","60","20","20","20","70","00","00"],
"239":["00","00","50","00","00","60","20","20","20","70","00","00"],
"240":["00","60","60","10","08","78","88","88","88","70","00","00"],
"241":["00","68","B0","00","00","B0","C8","88","88","88","00","00"],
"242":["00","40","20","00","00","70","88","88","88","70","00","00"],
"243":["00","10","20","00","00","70","88","88","88","70","00","00"],
"244":["00","20","50","00","00","70","88","88","88","70","00","00"],
"245":["00","68","B0","00","00","70","88","88","88","70","00","00"],
"246":["00","00","50","00","00","70","88","88","88","70","00","00"],
"247":["00","00","00","00","20","00","F8","00","20","00","00","00"],
"248":["00","00","00","00","00","70","98","A8","C8","70","00","00"],
"249":["00","40","20","00","00","88","88","88","98","68","00","00"],
"250":["00","10","20","00","00","88","88","88","98","68","00","00"],
"251":["00","20","50","00","00","88","88","88","98","68","00","00"],
"252":["00","00","50","00","00","88","88","88","98","68","00","00"],
"253":["00","10","20","00","00","88","88","88","88","78","08","70"],
"254":["00","00","80","80","80","F0","88","88","88","F0","80","80"],
"255":["00","00","50","00","00","88","88","88","88","78","08","70"]
}
function buildFonts(rawJson) {
const codes = Object.keys(rawJson).map((k, i) => parseInt(k, 10));
if (!codes.every((k, i) => i == k)) throw new Error();
// fixme
return codes.slice(30, 200).map((k) => buildFont(rawJson[k]));
}
function buildFont(rawJson) {
const fontMap = rawJson.map(h => parseInt(h, 16));
return fontBitmap(
(y) => (fontMap[y]), (row, x) =>
p = ((row >> x) % 2),
8, 12);
}
function fontBitmap(getRow, getCell, w, h) {
return {
getRow,
getCell,
w,
h,
xyOnOff(x, y) {
if (x >= 0 && x < this.w && y >= 0 && y < this.h)
return this.getCell(this.getRow(y), x);
return 0;
},
edgeOnOff(x1, y1, x2, y2) {
const _xor = (l, r) => (l ^ r) % 2;
if (x2 === x1 && Math.abs(y1 - y2) === 1) {
const x = x1;
const y = Math.min(y1, y2);
return _xor(this.xyOnOff(x, y), this.xyOnOff(x - 1, y));
}
if (y2 === y1 && Math.abs(x1 - x2) === 1) {
const x = Math.min(x1, x2);
const y = y1;
return _xor(this.xyOnOff(x, y), this.xyOnOff(x, y - 1));
}
throw new Error();
}
}
}
const SCALE = 10;
const MARGIN = 5;
const WIDTH = 80;
const HEIGHT = 130;
const canvi = div = document.createElement('div');
document.body.append(div);
div.style.position = 'absolute';
div.style.left = '40px';
div.style.top = '10px';
function getCanvas() {
const canvas = document.createElement('canvas');
canvas.width = WIDTH + 2 * MARGIN;
canvas.height = HEIGHT + 2 * MARGIN;
div.append(canvas);
const ctx = canvas.getContext('2d');
ctx.fillStyle = 'red';
ctx.strokeStyle = 'blue';
ctx.fillRect(1, 1, 8, 8);
ctx.beginPath();
ctx.moveTo(2, 2);
ctx.lineTo(2, canvas.height - 2);
ctx.lineTo(canvas.height - 2, canvas.height - 2);
ctx.lineTo(canvas.height - 2, 2);
ctx.lineTo(2, 2);
ctx.stroke();
ctx.fillStyle = 'white';
ctx.strokeStyle = 'white';
return {
drawLine: (x1, y1, x2, y2) => {
const y = (x) => x * SCALE + MARGIN;
const x = (x) => y(8 - x);
ctx.beginPath();
ctx.moveTo(x(x1), y(y1));
ctx.lineTo(x(x1), y(y2));
ctx.lineTo(x(x2), y(y2));
ctx.lineTo(x(x2), y(y1));
ctx.lineTo(x(x1), y(y1));
ctx.stroke();
},
drawDot: (x, y) => {
ctx.fillStyle = 'grey';
ctx.strokeStyle = 'grey';
const sy = (x) => x * SCALE + MARGIN;
const sx = (x) => sy(8 - x);
ctx.fillRect(sx(x) - SCALE, sy(y), SCALE, SCALE);
ctx.fillStyle = 'white';
ctx.strokeStyle = 'white';
ctx.fillRect(sx(x) - 7, sy(y) + 3, 4, 4)
},
ctx: ctx
}
}
document.body.style.backgroundColor = 'black';
var jsonTag = $('pre');
jsonTag.style.color = 'grey';
fntlist = JSON.parse(jsonTag.innerHTML);
for (let fm of buildFonts(fntlist)) {
let {
drawLine,
drawDot,
ctx
} = getCanvas();
for (let y = 0; y <= 12; y++) {
for (let x = 0; x <= 8; x++) {
if (fm.xyOnOff(x, y)) drawDot(x, y);
if (fm.edgeOnOff(x, y, x + 1, y)) {
drawLine(x, y, x + 1, y)
}
if (fm.edgeOnOff(x, y, x, y + 1)) {
drawLine(x, y, x, y + 1)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment