Skip to content

Commit

Permalink
Exclude CJK characters from default font build, reducing bundle size …
Browse files Browse the repository at this point in the history
…by ~2.8MB
  • Loading branch information
theacodes committed Nov 3, 2023
1 parent c17ed66 commit 5cd3c31
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 45,527 deletions.
21 changes: 14 additions & 7 deletions scripts/build-font.js
Expand Up @@ -11,10 +11,12 @@
* this script transforms it into a .ts file so it can be easily imported
* into KiCanvas
*
* Note: Newstroke is *huge*, nearly 3MB! It's currently the single largest
* part of KiCanvas's bundle. This is largely due to the CJK character set,
* so it may make sense to split it into a separate file that's loaded
* on demand.
* Note: Newstroke is *huge*, nearly 3MB! This is massive compared to the rest
* of KiCanvas, so we have to do some optimization. The vast bulk of Newstroke
* is the extended CJK character set (Unicode 0x4E00-0x9FFF). There aren't a lot
* of open source schematics/boards that use those characters so this script
* removes them, reducing the size from ~3MB to ~200kB. In the future, it
* should be possible to separately load those symbols when needed.
*
*/

Expand All @@ -33,7 +35,10 @@ const preamble = `/* Generated from third-party ${INFILE} for KiCanvas.\nSee bel
const license_str = src.slice(0, src.lastIndexOf("*/", first_bracket) + 2);
const glyph_array_str = src.slice(first_bracket + 1, src.lastIndexOf("}") - 1);

const glyphs = eval(`[${glyph_array_str}]`);
const all_glyphs = eval(`[${glyph_array_str}]`);
// Note: This is where the CJK characters are excluded.
const glyphs = all_glyphs.slice(0, 0x4e00);
const excluded_count = all_glyphs.length - glyphs.length;
const glyph_set = new Set(glyphs);
const glyph_map = new Map();

Expand All @@ -56,7 +61,7 @@ for (const [glyph, count] of glyph_map) {

const repeated_glyphs_def_str = repeated_glyph_defs.join(", ");

const out_glyphs = glyphs.map((glyph) => {
const out_glyphs = glyphs.map((glyph, index) => {
if (repeated_glyphs.has(glyph)) {
return repeated_glyphs.get(glyph);
} else {
Expand All @@ -66,7 +71,9 @@ const out_glyphs = glyphs.map((glyph) => {

const out_glyph_array_str = out_glyphs.join(",\n");

console.log(`${glyphs.length} total glyphs, ${glyph_set.size} unique`);
console.log(
`${glyphs.length} total glyphs, ${glyph_set.size} unique, ${excluded_count} excluded`,
);

const code = `export const shared_glyphs = [${repeated_glyphs_def_str}];\n\nexport const glyph_data: (string|number|undefined)[] = [${out_glyph_array_str}\n];\n`;
const header = `${preamble}\n\n${license_str}`;
Expand Down

0 comments on commit 5cd3c31

Please sign in to comment.