Skip to content

Commit

Permalink
webfont can do conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
delphiactual committed Jun 12, 2024
1 parent 06dcc1a commit ce9cec1
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 58 deletions.
Binary file modified output/DIMSymbols.woff2
Binary file not shown.
4 changes: 0 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@
"@types/node": "^20.14.2",
"@types/opentype.js": "^1.3.8",
"@types/stringify-object": "^4.0.5",
"@types/svg2ttf": "^5.0.3",
"@types/ttf2woff": "^2.0.4",
"@types/ttf2woff2": "^2.0.2",
"@typescript-eslint/eslint-plugin": "^7.13.0",
"@typescript-eslint/parser": "^7.13.0",
Expand All @@ -58,8 +56,6 @@
"pretty-quick": "^4.0.0",
"resolve-tspaths": "^0.8.19",
"stringify-object": "^5.0.0",
"svg2ttf": "^6.0.3",
"ttf2woff": "^3.0.0",
"ttf2woff2": "^5.0.0",
"typescript": "^5.4.5",
"webfont": "^11.2.26"
Expand Down
30 changes: 0 additions & 30 deletions pnpm-lock.yaml

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

35 changes: 11 additions & 24 deletions src/generate-custom-glyph-enums.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
import { loadSync } from 'opentype.js';
import { parse } from 'opentype.js';
import { webfont } from 'webfont';
import { writeFile } from './helpers.js';
import { writeFile, toArrayBuffer } from './helpers.js';
import { infoLog } from './log.js';
import svg2ttf from 'svg2ttf';
import ttf2woff2 from 'ttf2woff2';
import ttf2woff from 'ttf2woff';
import fse from 'fs-extra';

const { writeFileSync, unlinkSync } = fse;
const { writeFileSync } = fse;
const acc: Record<string, number> = {};
const TAG = 'CUSTOM-GLYPHS';
const options = {
ts: 0,
description: 'generated by d2ai for Destiny Item Manager',
url: 'http://www.destinyitemmanager.com',
};

const svgFont = await webfont({
files: './DIM-custom-font/SVGs/',
Expand All @@ -25,19 +17,17 @@ const svgFont = await webfont({
centerHorizontally: true,
fontHeight: '960',
descent: '150',
formats: ['woff', 'woff2'],
});

const woffFile = './DIM-custom-font/DIM-Symbols.woff';
const woff2File = './output/DIMSymbols.woff2';
// loadSync requires .otf or .woff filetype for enumeration
const ttf = Buffer.from(svg2ttf(String(svgFont.svg), options).buffer);
writeFileSync(woffFile, ttf2woff(ttf));
infoLog(TAG, `TEMP: ${woffFile} saved.`);
// Generate font format to be used by DIM
writeFileSync(woff2File, ttf2woff2(ttf));
infoLog(TAG, `${woff2File} saved.`);
const font = parse(toArrayBuffer(svgFont.woff!));

const font = loadSync(woffFile);
// Generate font format to be used by DIM
if (svgFont.woff2) {
const woff2File = './output/DIMSymbols.woff2';
writeFileSync(woff2File, svgFont.woff2);
infoLog(TAG, `${woff2File} saved.`);
}

for (let i = 0; i < font.glyphs.length; i++) {
const glyph = font.glyphs.get(i);
Expand All @@ -54,6 +44,3 @@ const outputEnum = `export const enum DimCustomSymbols {${Object.entries(acc)

writeFile('./output/dim-custom-symbols.ts', outputEnum);
writeFile('./data/dim-custom-symbols.ts', outputEnum);
// no need to keep this temp file
unlinkSync(woffFile);
infoLog(TAG, `TEMP: ${woffFile} removed.`);
9 changes: 9 additions & 0 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,3 +196,12 @@ function removeArticles(str: string) {
}
return str;
}

export function toArrayBuffer(buffer: Buffer) {
const arrayBuffer = new ArrayBuffer(buffer.length);
const view = new Uint8Array(arrayBuffer);
for (let i = 0; i < buffer.length; ++i) {
view[i] = buffer[i];
}
return arrayBuffer;
}

0 comments on commit ce9cec1

Please sign in to comment.