Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/moody-windows-flash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes a case where the font face `src` format would be invalid when using the experimental fonts API
7 changes: 7 additions & 0 deletions packages/astro/src/assets/fonts/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ export const URL_PREFIX = '/_astro/fonts/';
export const CACHE_DIR = './fonts/';

export const FONT_TYPES = ['woff2', 'woff', 'otf', 'ttf', 'eot'] as const;
export const FONT_FORMAT_MAP: Record<(typeof FONT_TYPES)[number], string> = {
woff2: 'woff2',
woff: 'woff',
otf: 'opentype',
ttf: 'truetype',
eot: 'embedded-opentype',
};

// Extracted from https://raw.githubusercontent.com/seek-oss/capsize/refs/heads/master/packages/metrics/src/entireMetricsCollection.json
export const SYSTEM_METRICS = {
Expand Down
3 changes: 2 additions & 1 deletion packages/astro/src/assets/fonts/providers/local.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type * as unifont from 'unifont';
import type { ResolvedLocalFontFamily } from '../types.js';
import { extractFontType } from '../utils.js';
import { FONT_FORMAT_MAP } from '../constants.js';

// https://fonts.nuxt.com/get-started/providers#local
// https://github.com/nuxt/fonts/blob/main/src/providers/local.ts
Expand All @@ -26,7 +27,7 @@ export function resolveLocalFont({ family, proxyURL }: Options): ResolveFontResu
return {
originalURL,
url: proxyURL(originalURL),
format: extractFontType(originalURL),
format: FONT_FORMAT_MAP[extractFontType(originalURL)],
tech,
};
}),
Expand Down
10 changes: 5 additions & 5 deletions packages/astro/src/assets/fonts/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ export function renderFontSrc(sources: Exclude<unifont.FontFaceData['src'][numbe
.map((src) => {
if ('url' in src) {
let rendered = `url("${src.url}")`;
for (const key of ['format', 'tech'] as const) {
const value = src[key];
if (value) {
rendered += ` ${key}(${value})`;
}
if (src.format) {
rendered += ` format("${src.format}")`
}
if (src.tech) {
rendered += ` tech(${src.tech})`
}
return rendered;
}
Expand Down
6 changes: 3 additions & 3 deletions packages/astro/test/units/assets/fonts/providers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ describe('fonts providers', () => {
{
originalURL: '/src/fonts/foo.ttf',
url: '/_astro/fonts/foo.ttf',
format: 'ttf',
format: 'truetype',
tech: undefined,
},
],
Expand Down Expand Up @@ -153,7 +153,7 @@ describe('fonts providers', () => {
{
originalURL: '/src/fonts/bar.eot',
url: '/_astro/fonts/bar.eot',
format: 'eot',
format: 'embedded-opentype',
tech: 'color-SVG',
},
],
Expand All @@ -166,7 +166,7 @@ describe('fonts providers', () => {
{
originalURL: '/src/fonts/bar.eot',
url: '/_astro/fonts/bar.eot',
format: 'eot',
format: 'embedded-opentype',
tech: undefined,
},
],
Expand Down
12 changes: 12 additions & 0 deletions packages/astro/test/units/assets/fonts/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,18 @@ describe('fonts utils', () => {
false,
);
});
it('wraps format in quotes', () => {
assert.equal(
renderFontSrc([{ url: 'test', format: 'woff2' }]).includes('format("woff2")'),
true,
);
});
it('does not wrap tech in quotes', () => {
assert.equal(
renderFontSrc([{ url: 'test', tech: 'x' }]).includes('tech(x)'),
true,
);
});
});

it('toCSS', () => {
Expand Down