font2bitmap 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +21 -0
- package/README.md +58 -0
- package/dist/cli.js +34 -0
- package/package.json +63 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025, Pavel Koltyshev.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# Font2Bitmap
|
|
2
|
+
|
|
3
|
+
Converts fonts to bitmaps for use in OLED displays.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
1. Install [Node.js 24](https://nodejs.org/en/download)
|
|
8
|
+
2. Install [dependencies](https://github.com/Automattic/node-canvas?tab=readme-ov-file#compiling) for `node-canvas`
|
|
9
|
+
3. Install font2bitmap `npm install font2bitmap -g`
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
# Create ASCII font
|
|
15
|
+
font2bitmap \
|
|
16
|
+
--fontPath ./fonts/GoogleSansCode-Medium.ttf \
|
|
17
|
+
--fontName google_sans_code \
|
|
18
|
+
--height 10 \
|
|
19
|
+
--output ./google_sans_code_ascii.h \
|
|
20
|
+
--subsets ascii \
|
|
21
|
+
--symbols '°$%'
|
|
22
|
+
|
|
23
|
+
# Create ASCII+Cyrillic font
|
|
24
|
+
font2bitmap \
|
|
25
|
+
--fontPath ./fonts/GoogleSansCode-Medium.ttf \
|
|
26
|
+
--fontName google_sans_code \
|
|
27
|
+
--height 10 \
|
|
28
|
+
--output ./google_sans_code_cyrillic.h \
|
|
29
|
+
--subsets ascii,cyrillic
|
|
30
|
+
|
|
31
|
+
# Create only digits font
|
|
32
|
+
font2bitmap \
|
|
33
|
+
--fontPath ./fonts/GoogleSansCode-Medium.ttf \
|
|
34
|
+
--fontName google_sans_code \
|
|
35
|
+
--height 10 \
|
|
36
|
+
--output ./google_sans_code_digits.h \
|
|
37
|
+
--subsets digits
|
|
38
|
+
|
|
39
|
+
# Create only symbols font
|
|
40
|
+
font2bitmap \
|
|
41
|
+
--fontPath ./fonts/GoogleSansCode-Medium.ttf \
|
|
42
|
+
--fontName google_sans_code \
|
|
43
|
+
--height 10 \
|
|
44
|
+
--output ./google_sans_code_symbols.h \
|
|
45
|
+
--symbols '°$%'
|
|
46
|
+
|
|
47
|
+
# Help
|
|
48
|
+
font2bitmap --help
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## Limitations
|
|
52
|
+
|
|
53
|
+
- The font bitmap is saved with LSB-first bit order.
|
|
54
|
+
- Variable fonts are not supported.
|
|
55
|
+
|
|
56
|
+
## License
|
|
57
|
+
|
|
58
|
+
[MIT](./LICENSE.md)
|
package/dist/cli.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import U from"node:fs";import{Command as P}from"commander";var $={name:"font2bitmap",version:"0.0.1",description:"Utility to convert font files to bitmap format",main:"dist/cli.js",type:"module",keywords:["bitmap","OLED","font","ssd1306"],author:"Pavel Koltyshev <pkoltyshev@gmail.com>",license:"MIT",repository:"pkolt/font2bitmap",bugs:{url:"https://github.com/pkolt/font2bitmap/issues",email:"pkoltyshev@gmail.com"},files:["README.md","LICENSE.md","dist/cli.js"],bin:{font2bitmap:"dist/cli.js"},engines:{node:">= 24"},scripts:{dev:"node ./src/cli.ts",build:"esbuild ./src/cli.ts --bundle --minify --platform=node --format=esm --packages=external --outfile=./dist/cli.js",types:"tsc --noEmit",format:"prettier --check src","format-fix":"prettier --write src",lint:"eslint","lint-fix":"eslint --fix",knip:"knip",test:"node --test",coverage:"node --test --experimental-test-coverage",check:"npm run types && npm run format && npm run test && npm run lint && npm run knip",prepare:'if [ "$NODE_ENV" != "production" ]; then git config core.hooksPath .git-hooks; fi',"pre-commit":"npm run check",release:"release-it"},devDependencies:{"@eslint/js":"^9.39.2","@types/node":"^25.0.3",esbuild:"^0.27.2",eslint:"^9.39.2",jiti:"^2.6.1",knip:"^5.78.0",prettier:"^3.7.4","release-it":"^19.2.2",typescript:"^5.9.3","typescript-eslint":"^8.50.1"},dependencies:{canvas:"^3.2.0",commander:"^14.0.2"}};import{createCanvas as j,registerFont as D}from"canvas";var _="Font Family",F="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",T="0123456789",I="\u0410\u0411\u0412\u0413\u0414\u0415\u0416\u0417\u0418\u0419\u041A\u041B\u041C\u041D\u041E\u041F\u0420\u0421\u0422\u0423\u0424\u0425\u0426\u0427\u0428\u0429\u042A\u042B\u042C\u042D\u042E\u042F\u0430\u0431\u0432\u0433\u0434\u0435\u0436\u0437\u0438\u0439\u043A\u043B\u043C\u043D\u043E\u043F\u0440\u0441\u0442\u0443\u0444\u0445\u0446\u0447\u0448\u0449\u044A\u044B\u044C\u044D\u044E\u044F",O=`.,!?"'-=/\\[]{}();:&*+@~`;function L(t,e,n){e.fillStyle="#fff",e.fillRect(0,0,e.canvas.width,e.canvas.height),e.fillStyle="#000",e.fillText(t,0,n/2);let c=e.getImageData(0,0,e.canvas.width,n),{data:s}=c,o=e.canvas.width,r=-1;for(let f=0;f<n;f++)for(let a=0;a<e.canvas.width;a++){let d=s[(f*e.canvas.width+a)*4];d!==void 0&&d<255&&(a<o&&(o=a),a>r&&(r=a))}if(o>r)return{char:t,width:0,symbol:{char:t,width:0,bitmap:new Uint8Array(0)}};let i=r-o+1,l=Math.ceil(i/8),h=new Uint8Array(l*n);if(i>0)for(let f=0;f<n;f++)for(let a=0;a<i;a++){let d=s[(f*e.canvas.width+(o+a))*4];if(d!==void 0&&d<128){let y=f*l+Math.floor(a/8),v=a%8,S=h[y];S!==void 0&&(h[y]=S|1<<v)}}return{char:t,width:i,symbol:{char:t,width:i,bitmap:h}}}function H(t){if(t.length===0)return[];let e=[...t].sort((o,r)=>o.char.charCodeAt(0)-r.char.charCodeAt(0)),n=e[0];if(!n)return[];let c=[],s={start:n.char.charCodeAt(0),end:n.char.charCodeAt(0),symbols:[n.symbol]};for(let o=1;o<e.length;o++){let r=e[o];if(!r)continue;let i=r.char.charCodeAt(0);i===s.end+1?(s.end=i,s.symbols.push(r.symbol)):(c.push(s),s={start:i,end:i,symbols:[r.symbol]})}return c.push(s),c}function N(t){D(t.fontPath,{family:_});let{height:e}=t,n=j(e*2,e).getContext("2d");n.font=`${e}px "${_}"`,n.textBaseline="middle",n.textAlign="left",n.textDrawingMode="glyph";let c=M(t.subsets,t.symbols),s={name:t.fontName,width:0,height:0,spacing:t.spacing,subsets:[]};if(c.length===0)return s;let o=c.map(l=>L(l,n,e)),r=o.map(({char:l,symbol:h})=>({char:l,symbol:h})),i=Math.max(0,...o.map(l=>l.width));return s.width=i,s.height=e,s.subsets=H(r),s}function M(t,e){let n={ascii:Array.from({length:95},(s,o)=>String.fromCharCode(32+o)).join(""),latin:F,digits:T,cyrillic:I,punctuation:O},c=new Set(e);return t.forEach(s=>{n[s].split("").forEach(o=>c.add(o))}),Array.from(c)}function R(t){let e=t.name,n=`FONT_${e.toUpperCase()}_H`,c=t.subsets.map(o=>{let r=0,i=[],l=[],h=[],f=(m,w,C=16,A=16)=>{if(m.length===0)return"";let g=Array.from(m).map(b=>`0x${b.toString(16).padStart(2,"0")}`),u=[];for(let b=0;b<g.length;b+=C)u.push(" ".repeat(A)+g.slice(b,b+C).join(", "));let E=u.pop();return E&&u.push(`${E},`),`${u.join(`,
|
|
3
|
+
`)} // '${w}'`},a=o.symbols.some(m=>m.width!==t.width);for(let m of o.symbols)i.push(r),r+=m.bitmap.length,l.push(m.width),h.push(f(m.bitmap,m.char,16,16));let d=(m,w=16,C=16)=>{if(m.length===0)return"{}";let A=m.map(u=>`0x${u.toString(16).padStart(2,"0")}`),g=[];for(let u=0;u<A.length;u+=w)g.push(" ".repeat(C)+A.slice(u,u+w).join(", "));return`{
|
|
4
|
+
${g.join(`,
|
|
5
|
+
`)}
|
|
6
|
+
}`},y=`{
|
|
7
|
+
${h.filter(m=>m!=="").join(`,
|
|
8
|
+
`)}
|
|
9
|
+
}`,v=`{ ${i.join(", ")} }`,S=a?`(uint8_t[]) ${d(l,20,16)}`:"NULL";return` {
|
|
10
|
+
.start = ${o.start},
|
|
11
|
+
.end = ${o.end},
|
|
12
|
+
.symbols_count = ${o.symbols.length},
|
|
13
|
+
.symbols = (uint8_t[])${y},
|
|
14
|
+
.offsets = (uint32_t[]) ${v},
|
|
15
|
+
.widths = ${S}
|
|
16
|
+
}`}).join(`,
|
|
17
|
+
`);return`#ifndef ${n}
|
|
18
|
+
#define ${n}
|
|
19
|
+
|
|
20
|
+
#include <stdint.h>
|
|
21
|
+
#include "font.h"
|
|
22
|
+
|
|
23
|
+
const font_t ${e} = {
|
|
24
|
+
.width = ${t.width},
|
|
25
|
+
.height = ${t.height},
|
|
26
|
+
.spacing = ${t.spacing},
|
|
27
|
+
.subsets_count = ${t.subsets.length},
|
|
28
|
+
.subsets = (const font_subset_t[]) {
|
|
29
|
+
${c}
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
#endif // ${n}
|
|
34
|
+
`}var x=new P;x.name("font2bitmap").version($.version).description($.description);x.requiredOption("--fontPath <path>","Path to the font file (e.g., RobotoMono-Medium.ttf)").requiredOption("--fontName <name>","Name of the font (e.g., RobotoMono)").requiredOption("--height <height>","Height of the font in pixels",t=>parseInt(t,10)).requiredOption("--output <path>","Output file path").option("--format <format>","Output format","pico").option("--subsets <subset1,subset2>","Comma-separated list of character subsets",t=>t.split(","),["ascii"]).option("--spacing <spacing>","Letter spacing",t=>parseInt(t,10),1).option("--symbols <value>","A string of additional characters to include","");x.parse(process.argv);var p=x.opts();try{if(!U.existsSync(p.fontPath))throw new Error(`Error: Font file not found at ${p.fontPath}`);let t={fontPath:p.fontPath,fontName:p.fontName,height:p.height,subsets:p.subsets,symbols:p.symbols?Array.from(p.symbols):[],spacing:p.spacing},e=N(t);if(p.format==="pico"){let n=R(e);U.writeFileSync(p.output,n),console.log(`Output successfully written to ${p.output}`)}else throw new Error(`Error: Unsupported format "${p.format}"`)}catch(t){t instanceof Error?console.error(t.message):console.error(t),process.exit(1)}
|
package/package.json
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "font2bitmap",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Utility to convert font files to bitmap format",
|
|
5
|
+
"main": "dist/cli.js",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"keywords": [
|
|
8
|
+
"bitmap",
|
|
9
|
+
"OLED",
|
|
10
|
+
"font",
|
|
11
|
+
"ssd1306"
|
|
12
|
+
],
|
|
13
|
+
"author": "Pavel Koltyshev <pkoltyshev@gmail.com>",
|
|
14
|
+
"license": "MIT",
|
|
15
|
+
"repository": "pkolt/font2bitmap",
|
|
16
|
+
"bugs": {
|
|
17
|
+
"url": "https://github.com/pkolt/font2bitmap/issues",
|
|
18
|
+
"email": "pkoltyshev@gmail.com"
|
|
19
|
+
},
|
|
20
|
+
"files": [
|
|
21
|
+
"README.md",
|
|
22
|
+
"LICENSE.md",
|
|
23
|
+
"dist/cli.js"
|
|
24
|
+
],
|
|
25
|
+
"bin": {
|
|
26
|
+
"font2bitmap": "dist/cli.js"
|
|
27
|
+
},
|
|
28
|
+
"engines": {
|
|
29
|
+
"node": ">= 24"
|
|
30
|
+
},
|
|
31
|
+
"scripts": {
|
|
32
|
+
"dev": "node ./src/cli.ts",
|
|
33
|
+
"build": "esbuild ./src/cli.ts --bundle --minify --platform=node --format=esm --packages=external --outfile=./dist/cli.js",
|
|
34
|
+
"types": "tsc --noEmit",
|
|
35
|
+
"format": "prettier --check src",
|
|
36
|
+
"format-fix": "prettier --write src",
|
|
37
|
+
"lint": "eslint",
|
|
38
|
+
"lint-fix": "eslint --fix",
|
|
39
|
+
"knip": "knip",
|
|
40
|
+
"test": "node --test",
|
|
41
|
+
"coverage": "node --test --experimental-test-coverage",
|
|
42
|
+
"check": "npm run types && npm run format && npm run test && npm run lint && npm run knip",
|
|
43
|
+
"prepare": "if [ \"$NODE_ENV\" != \"production\" ]; then git config core.hooksPath .git-hooks; fi",
|
|
44
|
+
"pre-commit": "npm run check",
|
|
45
|
+
"release": "release-it"
|
|
46
|
+
},
|
|
47
|
+
"devDependencies": {
|
|
48
|
+
"@eslint/js": "^9.39.2",
|
|
49
|
+
"@types/node": "^25.0.3",
|
|
50
|
+
"esbuild": "^0.27.2",
|
|
51
|
+
"eslint": "^9.39.2",
|
|
52
|
+
"jiti": "^2.6.1",
|
|
53
|
+
"knip": "^5.78.0",
|
|
54
|
+
"prettier": "^3.7.4",
|
|
55
|
+
"release-it": "^19.2.2",
|
|
56
|
+
"typescript": "^5.9.3",
|
|
57
|
+
"typescript-eslint": "^8.50.1"
|
|
58
|
+
},
|
|
59
|
+
"dependencies": {
|
|
60
|
+
"canvas": "^3.2.0",
|
|
61
|
+
"commander": "^14.0.2"
|
|
62
|
+
}
|
|
63
|
+
}
|