Skip to content

Commit 853ccd7

Browse files
florian-lefebvresarah11918yanthomasdev
authored
feat: getFontData() (#12345)
Co-authored-by: Sarah Rainsberger <[email protected]> Co-authored-by: Yan <[email protected]>
1 parent 554bc55 commit 853ccd7

File tree

1 file changed

+43
-0
lines changed
  • src/content/docs/en/reference/experimental-flags

1 file changed

+43
-0
lines changed

src/content/docs/en/reference/experimental-flags/fonts.mdx

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,49 @@ import { Font } from 'astro:assets';
325325
<Font cssVariable="--font-roboto" preload />
326326
```
327327

328+
## Accessing font data programmatically
329+
330+
The `getFontData()` function is intended for retrieving lower-level font family data programmatically, for example, in an [API Route](/en/guides/endpoints/#server-endpoints-api-routes) or to generate your own meta tags.
331+
332+
### `getFontData()`
333+
<p>
334+
335+
**Type:** `(cssVariable: CssVariable) => FontData[]`<br />
336+
<Since v="5.14.0" />
337+
</p>
338+
339+
Returns an array of `FontData` objects for the provided [`cssVariable`](#cssvariable-1), which contains `src`, `weight` and `style`.
340+
341+
The following example uses `getFontData()` to get the font buffer from the URL when using [satori](https://github.com/vercel/satori) to generate OpenGraph images:
342+
343+
```tsx title="src/pages/og.png.ts" "getFontData(\"--font-roboto\")" "data[0].src[0].url"
344+
import type{ APIRoute } from "astro"
345+
import { getFontData } from "astro:assets"
346+
import satori from "satori"
347+
348+
export const GET: APIRoute = (context) => {
349+
const data = getFontData("--font-roboto")
350+
351+
const svg = await satori(
352+
<div style={{ color: "black" }}>hello, world</div>,
353+
{
354+
width: 600,
355+
height: 400,
356+
fonts: [
357+
{
358+
name: "Roboto",
359+
data: await fetch(new URL(data[0].src[0].url, context.url.origin)).then(res => res.arrayBuffer()),
360+
weight: 400,
361+
style: "normal",
362+
},
363+
],
364+
},
365+
)
366+
367+
// ...
368+
}
369+
```
370+
328371
## Font configuration reference
329372

330373
All properties of your fonts must be configured in the Astro config. Some properties are common to both remote and local fonts, and other properties are available depending on your chosen font provider.

0 commit comments

Comments
 (0)