197 lines
6.4 KiB
JavaScript
197 lines
6.4 KiB
JavaScript
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
// Copyright (C) 2026 ZiroEDA and contributors.
|
|
// Portions derived from KiCad, copyright The KiCad Developers. See NOTICE.md.
|
|
/**
|
|
* Regenerate the OpenGL GAL's bitmap font, whole, from a KiCad checkout.
|
|
*
|
|
* KiCad's `OPENGL_GAL::BitmapText` samples an MSDF atlas baked by
|
|
* msdf-atlasgen into two generated C files:
|
|
*
|
|
* common/gal/opengl/bitmap_font_img.c 1024 x 1024 RGB
|
|
* common/gal/opengl/bitmap_font_desc.c the spans and the glyph metrics
|
|
*
|
|
* This writes the same data for the port: the whole atlas as a PNG, and the
|
|
* font information, the codepoint spans and the glyph table verbatim as
|
|
* TypeScript (`gl_resources.h`'s FONT_INFO_TYPE / FONT_SPAN_TYPE /
|
|
* FONT_GLYPH_TYPE). Nothing is repacked or dropped.
|
|
*
|
|
* Usage:
|
|
* node tools/gen_bitmap_font_full.mjs [path-to-kicad-source]
|
|
*
|
|
* Writes common/gal/opengl/bitmap_font_desc.ts and
|
|
* common/gal/opengl/bitmap_font_img.png.
|
|
*/
|
|
import { readFileSync, writeFileSync } from 'node:fs';
|
|
import { dirname, join, resolve } from 'node:path';
|
|
import { fileURLToPath } from 'node:url';
|
|
import { deflateSync } from 'node:zlib';
|
|
|
|
const HERE = dirname(fileURLToPath(import.meta.url));
|
|
const KICAD = resolve(process.argv[2] ?? '/home/akshay/kicad-reference');
|
|
const DESC = join(KICAD, 'common/gal/opengl/bitmap_font_desc.c');
|
|
const IMG = join(KICAD, 'common/gal/opengl/bitmap_font_img.c');
|
|
|
|
const num = (s) => Number(s.trim().replace(/f$/, ''));
|
|
|
|
function parseDesc() {
|
|
const src = readFileSync(DESC, 'utf8');
|
|
|
|
const info = /FONT_INFO_TYPE font_information = \{([^}]*)\}/.exec(src);
|
|
const [smooth, minY, maxY] = info[1].split(',').map(num);
|
|
|
|
const spanBlock = /FONT_SPAN_TYPE \w+\[\] = \{([\s\S]*?)\n\};/.exec(src)[1];
|
|
const spans = [...spanBlock.matchAll(/\{([^}]*)\}/g)].map((m) => m[1].split(',').map(num));
|
|
|
|
const glyphBlock = /FONT_GLYPH_TYPE \w+\[\] = \{([\s\S]*?)\n\};/.exec(src)[1];
|
|
const glyphs = [...glyphBlock.matchAll(/\{([^}]*)\}/g)].map((m) =>
|
|
m[1].split(',').map((s) => s.trim().replace(/f$/, '')),
|
|
);
|
|
|
|
return { smooth, minY, maxY, spans, glyphs };
|
|
}
|
|
|
|
/** The pixel array is ~10 MB of decimal text; scan it rather than split it. */
|
|
function parseImage() {
|
|
const src = readFileSync(IMG, 'utf8');
|
|
const head = /FONT_IMAGE_TYPE \w+ = \{([^{]*)\{/.exec(src);
|
|
const [width, height, charBorder, spacing] = head[1].split(',').map(num);
|
|
|
|
const start = src.indexOf('{', head.index + head[0].length - 1) + 1;
|
|
const px = new Uint8Array(width * height * 3);
|
|
let n = 0;
|
|
let acc = 0;
|
|
let inNumber = false;
|
|
for (let i = start; i < src.length; i++) {
|
|
const c = src.charCodeAt(i);
|
|
if (c >= 48 && c <= 57) {
|
|
acc = acc * 10 + (c - 48);
|
|
inNumber = true;
|
|
} else if (inNumber) {
|
|
px[n++] = acc;
|
|
acc = 0;
|
|
inNumber = false;
|
|
if (n === px.length) break;
|
|
}
|
|
}
|
|
if (n !== px.length) throw new Error(`read ${n} of ${px.length} texels`);
|
|
return { width, height, charBorder, spacing, px };
|
|
}
|
|
|
|
/** Minimal PNG writer: 8-bit RGB, one filter-0 scanline per row. */
|
|
function png(width, height, rgb) {
|
|
const raw = Buffer.alloc(height * (width * 3 + 1));
|
|
for (let r = 0; r < height; r++) {
|
|
raw[r * (width * 3 + 1)] = 0;
|
|
Buffer.from(rgb.buffer, rgb.byteOffset + r * width * 3, width * 3).copy(
|
|
raw,
|
|
r * (width * 3 + 1) + 1,
|
|
);
|
|
}
|
|
const chunk = (type, data) => {
|
|
const len = Buffer.alloc(4);
|
|
len.writeUInt32BE(data.length);
|
|
const body = Buffer.concat([Buffer.from(type, 'ascii'), data]);
|
|
const crc = Buffer.alloc(4);
|
|
crc.writeUInt32BE(crc32(body));
|
|
return Buffer.concat([len, body, crc]);
|
|
};
|
|
const ihdr = Buffer.alloc(13);
|
|
ihdr.writeUInt32BE(width, 0);
|
|
ihdr.writeUInt32BE(height, 4);
|
|
ihdr[8] = 8; // bit depth
|
|
ihdr[9] = 2; // colour type: truecolour
|
|
return Buffer.concat([
|
|
Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]),
|
|
chunk('IHDR', ihdr),
|
|
chunk('IDAT', deflateSync(raw, { level: 9 })),
|
|
chunk('IEND', Buffer.alloc(0)),
|
|
]);
|
|
}
|
|
|
|
const CRC_TABLE = (() => {
|
|
const t = new Int32Array(256);
|
|
for (let n = 0; n < 256; n++) {
|
|
let c = n;
|
|
for (let k = 0; k < 8; k++) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
|
|
t[n] = c;
|
|
}
|
|
return t;
|
|
})();
|
|
function crc32(buf) {
|
|
let c = -1;
|
|
for (let i = 0; i < buf.length; i++) c = CRC_TABLE[(c ^ buf[i]) & 0xff] ^ (c >>> 8);
|
|
return (c ^ -1) >>> 0;
|
|
}
|
|
|
|
const desc = parseDesc();
|
|
const img = parseImage();
|
|
console.log(
|
|
`atlas ${img.width}x${img.height}, ${desc.glyphs.length} glyphs, ${desc.spans.length} spans`,
|
|
);
|
|
|
|
writeFileSync(
|
|
join(HERE, '..', 'common/gal/opengl/bitmap_font_img.png'),
|
|
png(img.width, img.height, img.px),
|
|
);
|
|
|
|
const ts = `// SPDX-License-Identifier: GPL-3.0-or-later
|
|
// Copyright (C) 2026 ZiroEDA and contributors.
|
|
// Portions derived from KiCad, copyright The KiCad Developers. See NOTICE.md.
|
|
// The font itself is Ubuntu, under the Ubuntu Font Licence 1.0.
|
|
//
|
|
// GENERATED by tools/gen_bitmap_font_full.mjs from KiCad's bitmap_font_desc.c
|
|
// and bitmap_font_img.c (msdf-atlasgen output). Do not edit; re-run the
|
|
// generator instead. \`gl_resources.h\`'s FONT_INFO_TYPE, FONT_SPAN_TYPE and
|
|
// FONT_GLYPH_TYPE, verbatim; the atlas is bitmap_font_img.png alongside.
|
|
|
|
/** \`FONT_IMAGE_TYPE font_image\`, less the pixels (bitmap_font_img.png). */
|
|
export const font_image = {
|
|
width: ${img.width},
|
|
height: ${img.height},
|
|
char_border: ${img.charBorder},
|
|
spacing: ${img.spacing},
|
|
} as const;
|
|
|
|
export interface FONT_INFO_TYPE {
|
|
smooth_pixels: number;
|
|
min_y: number;
|
|
max_y: number;
|
|
}
|
|
|
|
export interface FONT_SPAN_TYPE {
|
|
start: number;
|
|
end: number;
|
|
cumulative: number;
|
|
}
|
|
|
|
export interface FONT_GLYPH_TYPE {
|
|
atlas_x: number;
|
|
atlas_y: number;
|
|
atlas_w: number;
|
|
atlas_h: number;
|
|
minx: number;
|
|
maxx: number;
|
|
miny: number;
|
|
maxy: number;
|
|
advance: number;
|
|
}
|
|
|
|
export const font_information: FONT_INFO_TYPE = {
|
|
smooth_pixels: ${desc.smooth},
|
|
min_y: ${desc.minY},
|
|
max_y: ${desc.maxY},
|
|
};
|
|
|
|
/** [start, end, cumulative] per span. */
|
|
export const font_codepoint_spans: readonly (readonly [number, number, number])[] = [
|
|
${desc.spans.map((s) => ` [${s.join(', ')}],`).join('\n')}
|
|
];
|
|
|
|
/** [atlas_x, atlas_y, atlas_w, atlas_h, minx, maxx, miny, maxy, advance] per glyph. */
|
|
export const font_codepoint_infos: readonly (readonly number[])[] = [
|
|
${desc.glyphs.map((g) => ` [${g.join(', ')}],`).join('\n')}
|
|
];
|
|
`;
|
|
writeFileSync(join(HERE, '..', 'common/gal/opengl/bitmap_font_desc.ts'), ts);
|
|
console.log('wrote common/gal/opengl/bitmap_font_desc.ts and bitmap_font_img.png');
|