-
Is there a good way to fetch Google fonts to use them with this library? I wrote the following function to get the URL: function getFontUrl(familyName) {
return fetch(`https://fonts.googleapis.com/css2?family=${familyName}&display=swap`)
.then((rp) => rp.text())
.then((txt) => txt.match(/ latin [^]*?url\((.*?)\)/)[1]);
} but the result is still WOFF2, which isn't supported by opentype.js as far as I understand? So I thought about sending a custom user agent with the fetch request, as Google appears to deliver the font files matching system compatibility? Anybody tried this? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
You can't specify user agent with fetch unless you're using node. I'd use the font download links. besides, google fonts alters the fonts in some ways. |
Beta Was this translation helpful? Give feedback.
-
I recently discovered @yne's comment describing a pretty neat workaround for parsing You can employ a WASM build of fontello's waWoff2 library to decompress/recompile woff2 files to ttf. I've created a little loading helper function to switch between woff2 preprocessing and natively supported woff/ttf/otf loading:
Codepen example: load google fonts in opentype.js |
Beta Was this translation helpful? Give feedback.
-
With an API key, you can get a JSON listing of all of the fonts, with TTF links for each variant.
|
Beta Was this translation helpful? Give feedback.
You can't specify user agent with fetch unless you're using node. I'd use the font download links. besides, google fonts alters the fonts in some ways.