Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,13 +272,13 @@ List of characters to copy, belongs to previously declared "--font". Examples:

parser.add_argument('--stride', {
choices: [ 0, 1, 4, 8, 16, 32, 64 ],
type: positive_int,
default: 1,
type: 'int',
default: 0,
help: 'Align each glyph\'s stride to the specfied number of bytes.'
});

parser.add_argument('--align', {
choices: [ 1, 4, 8, 16, 32, 64 ],
choices: [ 1, 4, 8, 16, 32, 64, 128, 256, 512, 1024 ],
type: positive_int,
default: 1,
help: 'Align each glyph address to the specified number of bytes.'
Expand Down Expand Up @@ -324,12 +324,16 @@ List of characters to copy, belongs to previously declared "--font". Examples:
}

if (args.byte_align) {
parser.error('--byte-align is deprecated, use --stride 1 instead');
}

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

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

Expand Down
51 changes: 25 additions & 26 deletions lib/font/table_glyf.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,24 @@ class Glyf {
return pixels.map(line => line.map(p => (p >>> (8 - bpp))));
}

widthToStride(width) {
const stride = this.font.opts.stride;
if (stride > 0) {
const byte_count = Math.ceil((width * this.font.opts.bpp) / 8);
const final_length = Math.ceil(byte_count / stride) * stride;
return final_length;
}
return Math.ceil((width * this.font.opts.bpp) / 8);
}

// Returns "binary stream" (Buffer) of compiled glyph data
compileGlyph(glyph) {
// Allocate memory, enough for eny storage formats
let buf;
// Allocate memory, enough for every storage formats
let bufSize = 100 + (this.widthToStride(glyph.bbox.width) * glyph.bbox.height) *
this.font.opts.bpp + this.font.opts.align;

if (this.font.opts.align !== 1 || this.font.opts.stride !== 1) {
buf = Buffer.alloc(100 + glyph.bbox.width * glyph.bbox.height * 4 * 4);
} else {
buf = Buffer.alloc(100 + glyph.bbox.width * glyph.bbox.height * 4);
}
if (isNaN(bufSize) || bufSize <= 0) bufSize = 128 * 1024; // Fallback for empty glyphs
let buf = Buffer.alloc(bufSize);

const bs = new BitStream(buf);
bs.bigEndian = true;
Expand Down Expand Up @@ -89,28 +97,20 @@ class Glyf {
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);
let bit_pad_line = 0;
if (this.font.opts.stride > 0) {
const bit_count = pixels[0].length * bpp;
const aligned_bit_count = this.widthToStride(pixels[0].length) * 8;
bit_pad_line = aligned_bit_count - bit_count;
}

for (let y = 0; y < pixels.length; y++) {
const line = pixels[y];
if (this.font.opts.stride && this.font.opts.stride !== 1) {
let stride = this.font.opts.stride;
const alignedLine = Buffer.alloc(Math.ceil(line.length / stride) * stride);
alignedLine.fill(Buffer.from(line), 0, line.length);
for (let x = 0; x < alignedLine.length; x++) {
bitStream.writeBits(alignedLine[x], bpp);
}
} else {
for (let x = 0; x < line.length; x++) {
bitStream.writeBits(line[x], bpp);
}
if (pad) {
this.addPadding(bitStream, pad);
}
for (let x = 0; x < line.length; x++) {
bitStream.writeBits(line[x], bpp);
}
if (bit_pad_line) {
this.addPadding(bitStream, bit_pad_line / bpp);
}
}
}
Expand Down Expand Up @@ -175,7 +175,6 @@ 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;
Expand Down
2 changes: 1 addition & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ module.exports.long_dump = function long_dump(arr, options = {}) {
let indent = ' '.repeat(opts.indent);

return chunk(Array.from(arr), opts.col)
.map(l => l.map(v => (opts.hex ? `0x${v.toString(16)}` : v.toString())))
.map(l => l.map(v => (opts.hex ? `0x${v.toString(16).padStart(2, '0')}` : v.toString())))
.map(l => `${indent}${l.join(', ')}`)
.join(',\n');
};
Expand Down
2 changes: 1 addition & 1 deletion lib/writers/lvgl/lv_font.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class LvFont extends Font {
}

stride_guard() {
if (this.opts.stride !== 1) {
if (this.opts.stride > 0) {
return `#if !LV_VERSION_CHECK(9, 3, 0)
#error "At least LVGL v9.3 is required to use the stride attribute of the fonts"
#endif`;
Expand Down
14 changes: 8 additions & 6 deletions lib/writers/lvgl/lv_table_glyf.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@ class LvGlyf extends Glyf {
lv_bitmap(glyph) {
let buf;

if (this.font.opts.align !== 1 || this.font.opts.stride !== 1) {
buf = Buffer.alloc(100 + glyph.bbox.width * glyph.bbox.height * 4 * 4);
} else {
buf = Buffer.alloc(100 + glyph.bbox.width * glyph.bbox.height * 4);
}
const bufSize = 100 + (this.widthToStride(glyph.bbox.width) * glyph.bbox.height) *
this.font.opts.bpp + this.font.opts.align;
buf = Buffer.alloc(bufSize);


const bs = new BitStream(buf);
bs.bigEndian = true;
Expand Down Expand Up @@ -72,8 +71,11 @@ class LvGlyf extends Glyf {
const code_hex = d.glyph.code.toString(16).toUpperCase();
const code_str = JSON.stringify(String.fromCodePoint(d.glyph.code));

let cols = 8;

if (this.font.opts.stride > 0) cols = this.widthToStride(d.glyph.bbox.width);
let txt = ` /* U+${code_hex.padStart(4, '0')} ${code_str} */
${u.long_dump(d.bin, { hex: true })}`;
${u.long_dump(d.bin, { hex: true, col: cols })}`;

if (idx < this.lv_data.length - 1) {
// skip comma for zero data
Expand Down
6 changes: 4 additions & 2 deletions lib/writers/lvgl/lv_table_head.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class LvHead extends Head {
}

get_stride_align() {
if (this.font.opts.stride !== 1) {
if (this.font.opts.stride > 0) {
return ` .stride = ${this.font.opts.stride}`;
}
return '';
Expand All @@ -48,6 +48,8 @@ class LvHead extends Head {
const subpixels = (f.subpixels_mode === 0) ? 'LV_FONT_SUBPX_NONE' :
(f.subpixels_mode === 1) ? 'LV_FONT_SUBPX_HOR' : 'LV_FONT_SUBPX_VER';

const staticBitmap = f.glyf.getCompressionCode() === 0 ? '1' : '0';

return `
/*--------------------
* ALL CUSTOM DATA
Expand Down Expand Up @@ -103,7 +105,7 @@ lv_font_t ${f.font_name} = {
#endif

#if LV_VERSION_CHECK(9, 3, 0)
.static_bitmap = 1, /*Bitmaps are stored as const so they are always static */
.static_bitmap = ${staticBitmap}, /*Bitmaps are stored as const so they are always static if not compressed */
#endif

.dsc = &font_dsc, /*The custom font data. Will be accessed by \`get_glyph_bitmap/dsc\` */
Expand Down