-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
feat(fonts): getFontData #13811
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
feat(fonts): getFontData #13811
Conversation
🦋 Changeset detectedLatest commit: 920008d The changes in this PR will be included in the next version bump. Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
CodSpeed Performance ReportMerging #13811 will not alter performanceComparing Summary
|
astro
@astrojs/cloudflare
@astrojs/netlify
@astrojs/node
@astrojs/vercel
commit: |
|
@delucis shared it would be nice to have a way to retrieve buffers directly. It doesn't need to block this proposal but I'm taking note |
|
Hey @florian-lefebvre, this works but isn't the API exposed to the user too verbose for getting the font url ? const fontSrcUrl = getFontData("--ff-pixelated")[0].src[0].url; |
|
We are talking about it and will probably expose a more generic API. I know it's verbose but there are good reasons:
|
|
Yeah I understand that aspect. Also user could wrap the implementation for their specific use case... like function getAstroFontPath(cssVariable: CssVariable): string {
const data = getFontData(cssVariable);
if (data.length === 0) {
throw new Error(`No data for ${cssVariable}`);
}
const src = data[0].src[0];
if (!src) {
throw new Error(`No src for ${cssVariable}`);
}
return src.url;
} |
|
Yeah, @florian-lefebvre and I were chatting about it and whether it wouldn’t be more helpful for your use case to just have access directly to the Astro-generated CSS @tanishqmanuja? I guess in the end otherwise you’re trying to regenerate that yourself with your own logic? |
|
@delucis that would be the perfect solution, your guess is correct... EDIT: |
|
Yeah, my two cents is the above feels very wordy (especially if you're using variable fonts in which case it's currently difficult to figure out which array member you'd use). Right now my use of this is also in Satori, with the following function to get buffers from fonts stored in my repo (which are duplicates of what I have through the font API) const fetchFonts = async () => {
// Regular Font
const fontFileRegular = await fs.readFile("fonts/SpaceGrotesk-Regular.ttf");
const fontRegular: ArrayBuffer = fontFileRegular.buffer as ArrayBuffer;
// Bold Font
const fontFileBold = await fs.readFile("fonts/SpaceGrotesk-Bold.ttf");
const fontBold: ArrayBuffer = fontFileBold.buffer as ArrayBuffer;
return { fontRegular, fontBold };
};In a perfect blue sky world, the call would look something like const fontRegular: ArrayBuffer = getFontBuffer("--font-space-grotesk", "regular");Thanks for all your work with the Font API though, it's been great overall! |
|
Could the public type have a better name? |
|
Which one, |
|
Much better! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am slightly in two minds about the function name, because people might expect it to get the raw data, but I can't think of a better one.
|
Yeah for the record we're not passing the full data (typed as
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yay! Left some changeset thoughts, Florian!
Co-authored-by: Sarah Rainsberger <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good! Just two quick code formatting fixes in the error message from me!
Co-authored-by: Sarah Rainsberger <[email protected]>
Preview release
Install:
Use:
Changes
getFontData(cssVariable)function exported fromastro:assetsto allow retrieving data relative for a given font file. We only expose a few properties and will add more as they are requestedgetImageto implement it. It's a bit tricky because it's a virtual module which depends on anotherFontFamilytype toCssVariableTesting
Updated unit and integration tests
Docs
75b5810(#1039)