Replies: 2 comments 2 replies
-
You could store the buffer and use |
Beta Was this translation helpful? Give feedback.
0 replies
-
Thanks @Connum 🙌, I got it working with a node script to create import fs from 'fs';
const inputPath = process.argv[2];
const outputPath = process.argv[3];
if (!inputPath || !outputPath) {
console.log('Usage: node ttf-to-js.js path/to/input/font-name.ttf path/to/output/font-name.js');
process.exit(1);
}
const buffer = fs.promises.readFile(inputPath);
const bf = await buffer;
fs.writeFileSync(`${outputPath}`, `const font = \`${bf.toString('base64')}\`; export default font;`); Then in my browser scripts I can import like: import fontString from "../../../fonts/FontdinerSwanky.js";
function base64ToArrayBuffer(base64: string) {
const binary_string = atob(base64);
const len = binary_string.length;
const bytes = new Uint8Array( len );
for (let i = 0; i < len; i++) {
bytes[i] = binary_string.charCodeAt(i);
}
return bytes.buffer;
}
function getTextPath() {
const buffer = base64ToArrayBuffer(fontString);
const font = opentype.parse(buffer);
const path = font.getPath(text, 0, 0, size);
return path;
} Am I over-complicating it with the |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
For example, instead of loading the
.ttf
directly, can I "precompile" them, save theFont
object as JSON to load directly? I know there are non-serializable functions onFont
such asFont.getPath()
so I know just serializing it directly isn't going to work but is there any other way?Beta Was this translation helpful? Give feedback.
All reactions