Skip to content

Commit ca50cc8

Browse files
committed
feat(glyph): add --byte-align option
1 parent 841f1ef commit ca50cc8

File tree

4 files changed

+39
-9
lines changed

4 files changed

+39
-9
lines changed

README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ Common:
6060
grayscale icons. Since gray tones are emulated via transparency, result
6161
will be good on contrast background only.
6262
- `--lv-include` - only with `--format lvgl`, set alternate path for `lvgl.h`.
63+
- `--no-compress` - disable built-in RLE compression.
64+
- `--no-prefilter` - disable bitmap lines filter (XOR), used to improve
65+
compression ratio.
66+
- `--byte-align` - pad the line ends of the bitmaps to a whole byte (requires `--no-compress` and `--bpp != 3`)
67+
- `--no-kerning` - drop kerning info to reduce size (not recommended).
6368

6469
Per font:
6570

@@ -79,11 +84,6 @@ Per font:
7984
- `--autohint-strong` - use more strong autohinting (will break kerning).
8085

8186
Additional debug options:
82-
83-
- `--no-compress` - disable built-in RLE compression.
84-
- `--no-prefilter` - disable bitmap lines filter (XOR), used to improve
85-
compression ratio.
86-
- `--no-kerning` - drop kerning info to reduce size (not recommended).
8787
- `--full-info` - don't shorten 'font_info.json' (include pixels data).
8888

8989

lib/cli.js

+17
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,13 @@ List of characters to copy, belongs to previously declared "--font". Examples:
263263
help: 'Drop kerning info to reduce size (not recommended).'
264264
});
265265

266+
parser.add_argument('--byte-align', {
267+
dest: 'byte_align',
268+
action: 'store_true',
269+
default: false,
270+
help: 'Pad bitmap line endings to whole bytes.'
271+
});
272+
266273
parser.add_argument('--lv-include', {
267274
metavar: '<path>',
268275
help: 'Set alternate "lvgl.h" path (for --format lvgl).'
@@ -302,6 +309,16 @@ List of characters to copy, belongs to previously declared "--font". Examples:
302309
}
303310
}
304311

312+
if (args.byte_align) {
313+
if (args.no_compress === false) {
314+
parser.error('--byte_align requires --no-compress');
315+
}
316+
317+
if (args.bpp === 3) {
318+
parser.error('--byte_align requires --bpp 1, 2, 4, or 8');
319+
}
320+
}
321+
305322
//
306323
// Convert
307324
//

lib/font/table_glyf.js

+15-2
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,30 @@ class Glyf {
6060
}
6161

6262
storePixels(bitStream, pixels) {
63-
if (this.getCompressionCode() === 0) this.storePixelsRaw(bitStream, pixels);
63+
if (this.getCompressionCode() === 0 || this.getCompressionCode() === 3) this.storePixelsRaw(bitStream, pixels);
6464
else this.storePixelsCompressed(bitStream, pixels);
6565
}
6666

6767
storePixelsRaw(bitStream, pixels) {
68+
if (pixels.length == 0) return;
69+
6870
const bpp = this.font.opts.bpp;
71+
let pad = 0;
72+
if (this.font.opts.byte_align) {
73+
const pxPerByte = 8 / bpp;
74+
pad = pxPerByte - (pixels[0].length % pxPerByte);
75+
}
6976

7077
for (let y = 0; y < pixels.length; y++) {
7178
const line = pixels[y];
7279
for (let x = 0; x < line.length; x++) {
7380
bitStream.writeBits(line[x], bpp);
7481
}
82+
if (pad) {
83+
for (let x = 0; x < pad; x++) {
84+
bitStream.writeBits(0, bpp);
85+
}
86+
}
7587
}
7688
}
7789

@@ -135,9 +147,10 @@ class Glyf {
135147
}
136148

137149
getCompressionCode() {
150+
if (this.font.opts.byte_align) return 3;
138151
if (this.font.opts.no_compress) return 0;
139152
if (this.font.opts.bpp === 1) return 0;
140-
153+
141154
if (this.font.opts.no_prefilter) return 2;
142155
return 1;
143156
}

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)