New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Browser support #77
Comments
Can anyone help? |
You can bundle it for use in the browser using browserify, webpack, or other tools that can take commonjs code as input to produce browser-loadble ES5/ES6 code. |
I was just searching to do the same thing, some documentation on this subject would be nice. |
Anyone managed to produce browser-loadable code? Could someone share instructions on how to do it? SyntaxError: 'import' and 'export' may appear only with 'sourceType: module' (1:0) while parsing file: /home/user/fontkit/node_modules/fontkit/src/TTFFont.js |
@twardoch, thanks a lot for the links! |
Browsers are finally starting to support ES6 module loading natively. How about an additional rollup config that resolves all of the CommonJS modules and handles misc Babel transforms? |
I just submitted a pull request that adds a build for browsers via Browserify. This is less than ideal: Browserify adds to the size and doesn't support ES6 syntax. I think the "only" major dependency on Node is Buffer and Stream. Browsers have a slightly different implementation of buffers but a subset of the API is shared with Node. |
@indolering only in webkit/blink for the forseeable future, unfortunately. |
@Pomax The PR request I submitted transpiles to ES5 and UMD. |
Hey guy, how to use this library without using module loader like requirejs, commonjs, webpack, .... |
I have extracted code from @twardoch post. Thanks for that! const loadBlob = (blob) => {
blobToBuffer(blob, (err, buffer) => {
if (err) {
throw err;
}
let font = fontkit.create(buffer)
console.log(font)
// do something with font
})
}
fetch('link-to-your-font.woff').then(res => res.blob()).then(loadBlob, console.error) |
Indeed... It would be great if you could include standalone |
A small note on today's JS landscape: everything except legacy browsers (which is only a single browser right now; IE11) support ES6 at this point, so an ES5 release wouldn't make a whole lot of sense anymore. That's more something that people who still need to support legacy browsers can quite easily effect themselves by running their code through a transpiler after bundling, rather than having individual libraries do this beforehand. |
Sounds good. Standalone ES6 |
can't ? it is only server side only?? |
code
Error in browser
|
I'm getting the same error. Did you find the solution? |
I'm trying to bundle this using webpack. I'm not clear on the differences between webpack and browserify, but the issue with webpack seems to be the |
This uses the existing trie generation scripts to also generate a JS module that exports the contents of the trie as a Buffer. This should allow tools like webpack to successfully package fontkit and allow it to be required as a module. Prior to this, webpack could package it fine, but when the generated bundle executes and tries to import fontkit, it throws an error because of the load-time fs.readFileSync calls.
I just put in a PR (#185) to get around the readFileSync issue, which I think will allow this to work in webpack. |
I think I solved this by importing |
Can someone please show a simple example how to get a browser build out of the fontkit repo? It's 2021 (almost) and ES9 is supported in a every modern browser, so many of the concerns related to JS should not be an issue anymore. |
Just scanned through the comments and still not sure how to use this with a plain browser + vanilla JS setup. Any tutorials out there that would guide me through the process step by step? |
Looks like the docs for this project are a little better: https://github.com/opentypejs/opentype.js But also this comment links to several examples that use fontkit in the browser: #77 (comment) |
This fork might be helpful to use on a browser. |
This library is the only one (as far as I know) with the ability to read variable font tables from WOFF2 files. I tried to bundle it with browserify but I bumbed into the same issues when I tried to use the result (TypeError: fs.readFileSync is not a function). |
@BaGsn does the above fork not work for you? |
@Hopding I noticed your fork doesn't have issues enabled - that does make it a bit harder to recommend new PRs to merge in =) (and suggest updates that won't ever happen here, as this project is effectively abandoned, near as I can figure. For instance, Node has had native brotli support for quite a while now, voiding the need for brotli.js) |
bumping to 2022 |
Sorry but what is missing at this point? I did a lot of work on this recently. Here's a demo using fontkit in the browser with no build step. Is something else broken for you? https://codepen.io/devongovett/pen/JjpVMKZ |
Hey @devongovett! I don't think your link works |
If esm.sh is the quasi-official CDN for fontkit, it might be good to copy that example into the README.md as browser example so folks won't need to hunt for it. |
Something must have changed with the way esm.sh works, because it worked when I wrote that codepen and I haven't changed anything. Sigh. I updated the pen to use the bundled mode, which seems to work. I wouldn't say there's an "official" CDN for fontkit. Really, I'd recommend hosting it yourself, or bundling it into your application. That way you never get weird breakages like the above. |
Going to close this because I think it is working now. If you have further problems, please open a separate issue for that. |
Your code works, thank you @devongovett!
But I don't see a |
Follow de link. |
You state...
...but you don't offer an explanation (for the less well-versed among us) how Fontkit can be used for the browser environment. It'd be amazing if you could add a few lines about how one would do that (i.e. without node.js).
I would expect something like a *.js file that I could embed in an HTML file. But it's probably not that easy...
Many thanks! This is a super helpful library.
The text was updated successfully, but these errors were encountered: