Skip to content

Commit

Permalink
feat(glyph): add --byte-align option (#127)
Browse files Browse the repository at this point in the history
  • Loading branch information
kisvegabor authored Nov 17, 2024
1 parent 841f1ef commit 58b347e
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 9 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ Common:
grayscale icons. Since gray tones are emulated via transparency, result
will be good on contrast background only.
- `--lv-include` - only with `--format lvgl`, set alternate path for `lvgl.h`.
- `--no-compress` - disable built-in RLE compression.
- `--no-prefilter` - disable bitmap lines filter (XOR), used to improve
compression ratio.
- `--byte-align` - pad the line ends of the bitmaps to a whole byte (requires `--no-compress` and `--bpp != 3`)
- `--no-kerning` - drop kerning info to reduce size (not recommended).

Per font:

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

Additional debug options:

- `--no-compress` - disable built-in RLE compression.
- `--no-prefilter` - disable bitmap lines filter (XOR), used to improve
compression ratio.
- `--no-kerning` - drop kerning info to reduce size (not recommended).
- `--full-info` - don't shorten 'font_info.json' (include pixels data).


Expand Down
17 changes: 17 additions & 0 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,13 @@ List of characters to copy, belongs to previously declared "--font". Examples:
help: 'Drop kerning info to reduce size (not recommended).'
});

parser.add_argument('--byte-align', {
dest: 'byte_align',
action: 'store_true',
default: false,
help: 'Pad bitmap line endings to whole bytes.'
});

parser.add_argument('--lv-include', {
metavar: '<path>',
help: 'Set alternate "lvgl.h" path (for --format lvgl).'
Expand Down Expand Up @@ -302,6 +309,16 @@ List of characters to copy, belongs to previously declared "--font". Examples:
}
}

if (args.byte_align) {
if (args.no_compress === false) {
parser.error('--byte_align requires --no-compress');
}

if (args.bpp === 3) {
parser.error('--byte_align requires --bpp 1, 2, 4, or 8');
}
}

//
// Convert
//
Expand Down
16 changes: 14 additions & 2 deletions lib/font/table_glyf.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,30 @@ class Glyf {
}

storePixels(bitStream, pixels) {
if (this.getCompressionCode() === 0) this.storePixelsRaw(bitStream, pixels);
if (this.getCompressionCode() === 0 || this.getCompressionCode() === 3) this.storePixelsRaw(bitStream, pixels);
else this.storePixelsCompressed(bitStream, pixels);
}

storePixelsRaw(bitStream, pixels) {
if (pixels.length === 0) return;

const bpp = this.font.opts.bpp;
let pad = 0;
if (this.font.opts.byte_align) {
const pxPerByte = 8 / bpp;
pad = pxPerByte - (pixels[0].length % pxPerByte);
}

for (let y = 0; y < pixels.length; y++) {
const line = pixels[y];
for (let x = 0; x < line.length; x++) {
bitStream.writeBits(line[x], bpp);
}
if (pad) {
for (let x = 0; x < pad; x++) {
bitStream.writeBits(0, bpp);
}
}
}
}

Expand Down Expand Up @@ -135,9 +147,9 @@ class Glyf {
}

getCompressionCode() {
if (this.font.opts.byte_align) return 3;
if (this.font.opts.no_compress) return 0;
if (this.font.opts.bpp === 1) return 0;

if (this.font.opts.no_prefilter) return 2;
return 1;
}
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 58b347e

Please sign in to comment.