Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

fix: Preserve variable font styles (#6153)

Oleg Isonendbfb0a98493c
diff --git a/docs/university/how-tos/how-to-use-custom-fonts.md b/docs/university/how-tos/how-to-use-custom-fonts.md
index 71af9d34dc..6fd4ba8a7c 100644
--- a/docs/university/how-tos/how-to-use-custom-fonts.md
+++ b/docs/university/how-tos/how-to-use-custom-fonts.md
@@ -72,7 +72,8 @@ When uploading custom fonts, these formats are supported:
- **Use WOFF2 format** when possible for smallest file size
- **Limit font variations** — only upload weights you actually use
-- **Consider variable fonts** — one file contains multiple weights
+- **Upload upright and italic files together** when a family provides separate files. Webstudio reads each file's style metadata so the browser can select the correct face.
+- **Consider variable fonts** — one file can contain multiple weights and may also contain italic or slant variations
---
diff --git a/fixtures/react-router-cloudflare/.webstudio/data.json b/fixtures/react-router-cloudflare/.webstudio/data.json
index 0feb64370c..d5105eda9d 100644
--- a/fixtures/react-router-cloudflare/.webstudio/data.json
+++ b/fixtures/react-router-cloudflare/.webstudio/data.json
@@ -1,5 +1,5 @@
{
- "bundleVersion": "bundle-oez66b",
+ "bundleVersion": "bundle-1pgeukw",
"build": {
"id": "f565d527-32e7-4731-bc71-aca9e9574587",
"projectId": "d845c167-ea07-4875-b08d-83e97c09dcce",
diff --git a/fixtures/react-router-docker/.webstudio/data.json b/fixtures/react-router-docker/.webstudio/data.json
index 0feb64370c..d5105eda9d 100644
--- a/fixtures/react-router-docker/.webstudio/data.json
+++ b/fixtures/react-router-docker/.webstudio/data.json
@@ -1,5 +1,5 @@
{
- "bundleVersion": "bundle-oez66b",
+ "bundleVersion": "bundle-1pgeukw",
"build": {
"id": "f565d527-32e7-4731-bc71-aca9e9574587",
"projectId": "d845c167-ea07-4875-b08d-83e97c09dcce",
diff --git a/fixtures/react-router-netlify/.webstudio/data.json b/fixtures/react-router-netlify/.webstudio/data.json
index 0feb64370c..d5105eda9d 100644
--- a/fixtures/react-router-netlify/.webstudio/data.json
+++ b/fixtures/react-router-netlify/.webstudio/data.json
@@ -1,5 +1,5 @@
{
- "bundleVersion": "bundle-oez66b",
+ "bundleVersion": "bundle-1pgeukw",
"build": {
"id": "f565d527-32e7-4731-bc71-aca9e9574587",
"projectId": "d845c167-ea07-4875-b08d-83e97c09dcce",
diff --git a/fixtures/react-router-vercel/.webstudio/data.json b/fixtures/react-router-vercel/.webstudio/data.json
index 0feb64370c..d5105eda9d 100644
--- a/fixtures/react-router-vercel/.webstudio/data.json
+++ b/fixtures/react-router-vercel/.webstudio/data.json
@@ -1,5 +1,5 @@
{
- "bundleVersion": "bundle-oez66b",
+ "bundleVersion": "bundle-1pgeukw",
"build": {
"id": "f565d527-32e7-4731-bc71-aca9e9574587",
"projectId": "d845c167-ea07-4875-b08d-83e97c09dcce",
diff --git a/fixtures/ssg-cloudflare-pages/.webstudio/data.json b/fixtures/ssg-cloudflare-pages/.webstudio/data.json
index 75756496fd..6f72a667c2 100644
--- a/fixtures/ssg-cloudflare-pages/.webstudio/data.json
+++ b/fixtures/ssg-cloudflare-pages/.webstudio/data.json
@@ -1,5 +1,5 @@
{
- "bundleVersion": "bundle-oez66b",
+ "bundleVersion": "bundle-1pgeukw",
"build": {
"id": "a2e8de30-03d5-4514-a3a6-406b3266a3af",
"projectId": "d845c167-ea07-4875-b08d-83e97c09dcce",
diff --git a/fixtures/ssg-netlify-by-project-id/.webstudio/data.json b/fixtures/ssg-netlify-by-project-id/.webstudio/data.json
index 862a28d792..0fb364280a 100644
--- a/fixtures/ssg-netlify-by-project-id/.webstudio/data.json
+++ b/fixtures/ssg-netlify-by-project-id/.webstudio/data.json
@@ -1,5 +1,5 @@
{
- "bundleVersion": "bundle-oez66b",
+ "bundleVersion": "bundle-1pgeukw",
"build": {
"id": "8f0fdff2-e76b-4f86-9203-ea7df5f1143c",
"projectId": "8a7358b1-7de3-459d-b7b1-56dddfb6ce1e",
diff --git a/fixtures/webstudio-cloudflare-template/.webstudio/data.json b/fixtures/webstudio-cloudflare-template/.webstudio/data.json
index 75756496fd..6f72a667c2 100644
--- a/fixtures/webstudio-cloudflare-template/.webstudio/data.json
+++ b/fixtures/webstudio-cloudflare-template/.webstudio/data.json
@@ -1,5 +1,5 @@
{
- "bundleVersion": "bundle-oez66b",
+ "bundleVersion": "bundle-1pgeukw",
"build": {
"id": "a2e8de30-03d5-4514-a3a6-406b3266a3af",
"projectId": "d845c167-ea07-4875-b08d-83e97c09dcce",
diff --git a/fixtures/webstudio-features/.webstudio/data.json b/fixtures/webstudio-features/.webstudio/data.json
index 6435f80c24..8d973bc83c 100644
--- a/fixtures/webstudio-features/.webstudio/data.json
+++ b/fixtures/webstudio-features/.webstudio/data.json
@@ -1,5 +1,5 @@
{
- "bundleVersion": "bundle-oez66b",
+ "bundleVersion": "bundle-1pgeukw",
"build": {
"id": "1b66ee06-8ea5-4420-9a0e-e0d3a67aca32",
"projectId": "cddc1d44-af37-4cb6-a430-d300cf6f932d",
diff --git a/packages/asset-uploader/src/utils/font-data.test.ts b/packages/asset-uploader/src/utils/font-data.test.ts
index 42dba397bb..b7915f0e86 100644
--- a/packages/asset-uploader/src/utils/font-data.test.ts
+++ b/packages/asset-uploader/src/utils/font-data.test.ts
@@ -1,7 +1,10 @@
import { describe, test, expect } from "vitest";
-import { parseSubfamily, __testing__ } from "./font-data";
+import { readFileSync } from "node:fs";
+import { createRequire } from "node:module";
+import { getFontData, parseSubfamily, __testing__ } from "./font-data";
-const { getFontFamily, normalizeFamily } = __testing__;
+const { detectFontStyle, getFontFamily, normalizeFamily } = __testing__;
+const require = createRequire(import.meta.url);
describe("font-data", () => {
describe("parseSubfamily()", () => {
@@ -114,6 +117,59 @@ describe("font-data", () => {
});
});
+ test("detects style from authoritative font metadata", () => {
+ expect(
+ detectFontStyle(
+ {
+ "OS/2": { fsSelection: { italic: true, oblique: false } },
+ italicAngle: 0,
+ },
+ "normal"
+ )
+ ).toBe("italic");
+ expect(
+ detectFontStyle(
+ {
+ "OS/2": { fsSelection: { italic: false, oblique: true } },
+ italicAngle: 0,
+ },
+ "normal"
+ )
+ ).toBe("oblique");
+ expect(
+ detectFontStyle(
+ {
+ "OS/2": { fsSelection: { italic: false, oblique: false } },
+ italicAngle: -12,
+ },
+ "normal"
+ )
+ ).toBe("italic");
+ });
+
+ test("detects separate upright and italic variable font files", () => {
+ const readInter = (style: "normal" | "italic") => {
+ const name = `inter-latin-wght-${style}.woff2`;
+ return getFontData(
+ readFileSync(
+ require.resolve(`@fontsource-variable/inter/files/${name}`)
+ ),
+ name
+ );
+ };
+
+ expect(readInter("normal")).toMatchObject({
+ family: "Inter",
+ style: "normal",
+ variationAxes: { wght: { min: 100, default: 400, max: 900 } },
+ });
+ expect(readInter("italic")).toMatchObject({
+ family: "Inter",
+ style: "italic",
+ variationAxes: { wght: { min: 100, default: 400, max: 900 } },
+ });
+ });
+
test("prefers the typographic family name", () => {
expect(
getFontFamily({
diff --git a/packages/asset-uploader/src/utils/font-data.ts b/packages/asset-uploader/src/utils/font-data.ts
index 8323d567da..b467e61094 100644
--- a/packages/asset-uploader/src/utils/font-data.ts
+++ b/packages/asset-uploader/src/utils/font-data.ts
@@ -91,10 +91,38 @@ type FontDataStatic = {
type FontDataVariable = {
format: FontFormat;
family: string;
+ style: FontStyle;
variationAxes: VariationAxes;
};
type FontData = FontDataStatic | FontDataVariable;
+type FontStyleSource = {
+ "OS/2"?: {
+ fsSelection?: {
+ italic?: boolean;
+ oblique?: boolean;
+ };
+ };
+ italicAngle?: number;
+};
+
+const detectFontStyle = (
+ font: FontStyleSource,
+ fallback: FontStyle
+): FontStyle => {
+ const selection = font["OS/2"]?.fsSelection;
+ if (selection?.italic === true) {
+ return "italic";
+ }
+ if (selection?.oblique === true) {
+ return "oblique";
+ }
+ if (font.italicAngle !== undefined && font.italicAngle !== 0) {
+ return "italic";
+ }
+ return fallback;
+};
+
export const getFontData = (data: Uint8Array, fileName: string): FontData => {
const font = createFontKit(data as Buffer);
if (font.type !== "TTF" && font.type !== "WOFF" && font.type !== "WOFF2") {
@@ -107,12 +135,18 @@ export const getFontData = (data: Uint8Array, fileName: string): FontData => {
font.getName("fontSubfamily", defaultLanguage) ??
"";
const family = normalizeFamily(originalFamily, subfamily, fileName);
+ const parsedSubfamily = parseSubfamily(
+ subfamily,
+ font["OS/2"]?.usWeightClass
+ );
+ const style = detectFontStyle(font, parsedSubfamily.style);
const isVariable = Object.keys(font.variationAxes).length !== 0;
if (isVariable) {
return {
format,
family,
+ style,
variationAxes: font.variationAxes,
};
}
@@ -120,7 +154,8 @@ export const getFontData = (data: Uint8Array, fileName: string): FontData => {
return {
format,
family,
- ...parseSubfamily(subfamily, font["OS/2"]?.usWeightClass),
+ style,
+ weight: parsedSubfamily.weight,
};
};
@@ -131,4 +166,5 @@ export const __testing__: {
fileName: string
) => string;
getFontFamily: typeof getFontFamily;
-} = { normalizeFamily, getFontFamily };
+ detectFontStyle: typeof detectFontStyle;
+} = { normalizeFamily, getFontFamily, detectFontStyle };
diff --git a/packages/asset-uploader/src/utils/get-asset-data.test.ts b/packages/asset-uploader/src/utils/get-asset-data.test.ts
index 970a69368f..f930f8b1a9 100644
--- a/packages/asset-uploader/src/utils/get-asset-data.test.ts
+++ b/packages/asset-uploader/src/utils/get-asset-data.test.ts
@@ -58,7 +58,11 @@ describe("asset data overrides", () => {
{
size: 100,
format: "woff2",
- meta: { family: "Detected Family", variationAxes },
+ meta: {
+ family: "Detected Family",
+ style: "italic",
+ variationAxes,
+ },
},
{
format: "woff2",
@@ -72,7 +76,11 @@ describe("asset data overrides", () => {
).toEqual({
size: 100,
format: "woff2",
- meta: { family: "Configured Family", variationAxes },
+ meta: {
+ family: "Configured Family",
+ style: "normal",
+ variationAxes,
+ },
});
});
diff --git a/packages/cli/evaluations/high-impact/validate.ts b/packages/cli/evaluations/high-impact/validate.ts
index 189fb06a92..e447a62837 100644
--- a/packages/cli/evaluations/high-impact/validate.ts
+++ b/packages/cli/evaluations/high-impact/validate.ts
@@ -522,8 +522,8 @@ const validateFontAssets = (
fontAssets.every(
(asset) =>
asset.meta.family === fontAssetFixtureMeta.family &&
- "style" in asset.meta &&
asset.meta.style === fontAssetFixtureMeta.style &&
+ "weight" in asset.meta &&
asset.meta.weight === fontAssetFixtureMeta.weight
),
"Uploaded font metadata did not persist as Rajdhani normal 600."
diff --git a/packages/fonts/src/get-font-faces.test.ts b/packages/fonts/src/get-font-faces.test.ts
index 62fa254265..051599bf28 100644
--- a/packages/fonts/src/get-font-faces.test.ts
+++ b/packages/fonts/src/get-font-faces.test.ts
@@ -202,6 +202,7 @@ describe("getFontFaces()", () => {
format: "ttf",
meta: {
family: "Inter",
+ style: "normal",
variationAxes: {
wght: { name: "wght", min: 100, default: 400, max: 1000 },
wdth: { name: "wdth", min: 25, default: 100, max: 151 },
@@ -232,7 +233,79 @@ describe("getFontFaces()", () => {
"fontWeight": "100 1000",
"src": "url("/fonts/inter.ttf") format("truetype")",
},
+ {
+ "fontDisplay": "swap",
+ "fontFamily": "Inter",
+ "fontStretch": "25% 151%",
+ "fontStyle": "oblique",
+ "fontWeight": "100 1000",
+ "src": "url("/fonts/inter.ttf") format("truetype")",
+ },
]
`);
});
+
+ test("keeps separate upright and italic variable font files", () => {
+ const variationAxes = {
+ wght: { name: "Weight", min: 100, default: 400, max: 900 },
+ wdth: { name: "Width", min: 75, default: 100, max: 100 },
+ };
+ const assets: Array<PartialFontAsset> = [
+ {
+ format: "woff2",
+ meta: { family: "Example", style: "normal", variationAxes },
+ name: "example-normal.woff2",
+ },
+ {
+ format: "woff2",
+ meta: { family: "Example", style: "italic", variationAxes },
+ name: "example-italic.woff2",
+ },
+ ];
+
+ expect(getFontFaces(assets, { assetBaseUrl: "/fonts/" })).toEqual([
+ {
+ fontDisplay: "swap",
+ fontFamily: "Example",
+ fontStretch: "75% 100%",
+ fontStyle: "normal",
+ fontWeight: "100 900",
+ src: 'url("/fonts/example-normal.woff2") format("woff2")',
+ },
+ {
+ fontDisplay: "swap",
+ fontFamily: "Example",
+ fontStretch: "75% 100%",
+ fontStyle: "italic",
+ fontWeight: "100 900",
+ src: 'url("/fonts/example-italic.woff2") format("woff2")',
+ },
+ ]);
+ });
+
+ test("registers variable ital and slant axes for matching styles", () => {
+ const faces = getFontFaces(
+ [
+ {
+ format: "woff2",
+ meta: {
+ family: "Flexible",
+ style: "normal",
+ variationAxes: {
+ ital: { name: "Italic", min: 0, default: 0, max: 1 },
+ slnt: { name: "Slant", min: -12, default: 0, max: 0 },
+ },
+ },
+ name: "flexible.woff2",
+ },
+ ],
+ { assetBaseUrl: "/fonts/" }
+ );
+
+ expect(faces.map(({ fontStyle }) => fontStyle)).toEqual([
+ "normal",
+ "italic",
+ "oblique",
+ ]);
+ });
});
diff --git a/packages/fonts/src/get-font-faces.ts b/packages/fonts/src/get-font-faces.ts
index 1c0d099ab7..706989d8df 100644
--- a/packages/fonts/src/get-font-faces.ts
+++ b/packages/fonts/src/get-font-faces.ts
@@ -1,5 +1,5 @@
-import { FONT_FORMATS } from "./constants";
-import type { FontMeta, FontFormat, FontMetaStatic } from "./schema";
+import { FONT_FORMATS, type FontStyle } from "./constants";
+import type { FontMeta, FontFormat } from "./schema";
export type PartialFontAsset = {
format: FontFormat;
@@ -11,7 +11,7 @@ export type FontFace = {
fontFamily: string;
fontDisplay: "swap" | "auto" | "block" | "fallback" | "optional";
src: string;
- fontStyle?: FontMetaStatic["style"];
+ fontStyle?: FontStyle;
fontWeight?: number | string;
fontStretch?: string;
};
@@ -27,36 +27,61 @@ const getFontFormat = (asset: PartialFontAsset): string => {
: (FONT_FORMATS.get(asset.format) ?? asset.format);
};
-const formatFace = (
- asset: PartialFontAsset,
- format: string,
- url: string
-): FontFace => {
+type FontSource = {
+ asset: PartialFontAsset;
+ format: string;
+ style: FontStyle;
+ url: string;
+};
+
+const formatFace = ({ asset, format, style, url }: FontSource): FontFace => {
+ const face = {
+ fontFamily: asset.meta.family,
+ fontStyle: style,
+ fontDisplay: "swap" as const,
+ src: `url(${sanitizeCssUrl(url)}) format("${format}")`,
+ };
if ("variationAxes" in asset.meta) {
- const { wght, wdth } = asset.meta?.variationAxes ?? {};
+ const { wght, wdth } = asset.meta.variationAxes;
return {
- fontFamily: asset.meta.family,
- fontStyle: "normal",
- fontDisplay: "swap",
- src: `url(${sanitizeCssUrl(url)}) format("${format}")`,
+ ...face,
fontStretch: wdth ? `${wdth.min}% ${wdth.max}%` : undefined,
fontWeight: wght ? `${wght.min} ${wght.max}` : undefined,
};
}
return {
- fontFamily: asset.meta.family,
- fontStyle: asset.meta.style,
+ ...face,
fontWeight: asset.meta.weight,
- fontDisplay: "swap",
- src: `url(${sanitizeCssUrl(url)}) format("${format}")`,
};
};
-const getKey = (asset: PartialFontAsset) => {
+const getKey = (asset: PartialFontAsset, style: FontStyle) => {
if ("variationAxes" in asset.meta) {
- return asset.meta.family + Object.values(asset.meta.variationAxes).join("");
+ const { wght, wdth } = asset.meta.variationAxes;
+ return JSON.stringify([asset.meta.family, style, wght, wdth]);
}
- return asset.meta.family + asset.meta.style + asset.meta.weight;
+ return JSON.stringify([asset.meta.family, style, asset.meta.weight]);
+};
+
+const getStyles = (meta: FontMeta): FontStyle[] => {
+ const styles = new Set<FontStyle>([meta.style]);
+ if ("variationAxes" in meta) {
+ const { ital, slnt } = meta.variationAxes;
+ if (ital !== undefined && ital.min <= 0 && ital.max >= 1) {
+ styles.add("normal");
+ styles.add("italic");
+ }
+ if (
+ slnt !== undefined &&
+ slnt.min < slnt.max &&
+ slnt.min <= 0 &&
+ slnt.max >= 0
+ ) {
+ styles.add("normal");
+ styles.add("oblique");
+ }
+ }
+ return Array.from(styles);
};
export const getFontFaces = (
@@ -66,35 +91,33 @@ export const getFontFaces = (
}
): Array<FontFace> => {
const { assetBaseUrl } = options;
- const faces = new Map<
- string,
- Map<string, { asset: PartialFontAsset; format: string; url: string }>
- >();
- const seenUrls = new Set<string>();
+ const faces = new Map<string, Map<string, FontSource>>();
+ const seenSources = new Set<string>();
for (const asset of assets) {
const url = `${assetBaseUrl}${asset.name}`;
- const assetKey = getKey(asset);
- if (seenUrls.has(url)) {
- continue;
- }
- seenUrls.add(url);
- const format = getFontFormat(asset);
- const sources = faces.get(assetKey) ?? new Map();
- const existing = sources.get(format);
- if (existing === undefined || url < existing.url) {
- sources.set(format, { asset, format, url });
+ for (const style of getStyles(asset.meta)) {
+ const sourceKey = JSON.stringify([url, style]);
+ if (seenSources.has(sourceKey)) {
+ continue;
+ }
+ seenSources.add(sourceKey);
+ const assetKey = getKey(asset, style);
+ const format = getFontFormat(asset);
+ const sources = faces.get(assetKey) ?? new Map();
+ const existing = sources.get(format);
+ if (existing === undefined || url < existing.url) {
+ sources.set(format, { asset, format, style, url });
+ }
+ faces.set(assetKey, sources);
}
- faces.set(assetKey, sources);
}
return Array.from(faces.values(), (sources) => {
- const [{ asset, format, url }, ...fallbacks] = Array.from(
- sources.values()
- ).sort(
+ const [source, ...fallbacks] = Array.from(sources.values()).sort(
(left, right) =>
fontFormatOrder.indexOf(left.format) -
fontFormatOrder.indexOf(right.format)
);
- const face = formatFace(asset, format, url);
+ const face = formatFace(source);
face.src += fallbacks
.map(
({ format: fallbackFormat, url: fallbackUrl }) =>
diff --git a/packages/fonts/src/schema.test.ts b/packages/fonts/src/schema.test.ts
index 2844ae606f..fb3695c19a 100644
--- a/packages/fonts/src/schema.test.ts
+++ b/packages/fonts/src/schema.test.ts
@@ -1,5 +1,5 @@
import { describe, expect, test } from "vitest";
-import { fontMeta, mergeFontMeta } from "./schema";
+import { fontMeta, fontMetaUpdate, mergeFontMeta } from "./schema";
test("accepts custom variation axes", () => {
const meta = {
@@ -10,20 +10,47 @@ test("accepts custom variation axes", () => {
},
};
- expect(fontMeta.parse(meta)).toEqual(meta);
+ expect(fontMeta.parse(meta)).toEqual({ ...meta, style: "normal" });
});
describe("mergeFontMeta", () => {
+ test("defaults legacy variable fonts to normal style", () => {
+ expect(fontMeta.parse({ family: "Inter", variationAxes: {} })).toEqual({
+ family: "Inter",
+ style: "normal",
+ variationAxes: {},
+ });
+ });
+
+ test("round-trips and updates variable font metadata", () => {
+ const variationAxes = {
+ wght: { name: "Weight", min: 100, default: 400, max: 900 },
+ };
+ const meta = { family: "Inter", style: "italic" as const, variationAxes };
+ expect(fontMetaUpdate.parse(meta)).toEqual(meta);
+ expect(
+ mergeFontMeta(
+ { family: "Inter", style: "normal", variationAxes },
+ { style: "italic" }
+ )
+ ).toEqual(meta);
+ expect(mergeFontMeta(meta, { variationAxes })).toEqual(meta);
+ });
+
test("keeps the detected font shape", () => {
const variationAxes = {
wght: { name: "Weight", min: 100, default: 400, max: 900 },
};
expect(
mergeFontMeta(
- { family: "Detected Family", variationAxes },
+ { family: "Detected Family", style: "normal", variationAxes },
{ family: "Configured Family", style: "normal", weight: 400 }
)
- ).toEqual({ family: "Configured Family", variationAxes });
+ ).toEqual({
+ family: "Configured Family",
+ style: "normal",
+ variationAxes,
+ });
expect(
mergeFontMeta(
{ family: "Detected Family", style: "normal", weight: 400 },
@@ -41,6 +68,7 @@ describe("mergeFontMeta", () => {
mergeFontMeta(
{
family: "Detected Family",
+ style: "normal",
variationAxes: {
wght: { name: "Weight", min: 100, default: 400, max: 900 },
},
diff --git a/packages/fonts/src/schema.ts b/packages/fonts/src/schema.ts
index bd454cd155..075a7e2dcb 100644
--- a/packages/fonts/src/schema.ts
+++ b/packages/fonts/src/schema.ts
@@ -1,6 +1,8 @@
import { z } from "zod";
import { FONT_STYLES } from "./constants";
+const fontStyle = z.enum(FONT_STYLES);
+
export const fontFormat = z.union([
z.literal("ttf"),
z.literal("woff"),
@@ -29,7 +31,7 @@ export type VariationAxes = z.infer<typeof variationAxes>;
export const fontMetaStatic = z.object({
family: z.string(),
- style: z.enum(FONT_STYLES),
+ style: fontStyle,
weight: z.number(),
});
@@ -37,6 +39,7 @@ export type FontMetaStatic = z.infer<typeof fontMetaStatic>;
export const fontMetaVariable = z.object({
family: z.string(),
+ style: fontStyle.default("normal"),
variationAxes: variationAxes,
});
@@ -44,7 +47,13 @@ export const fontMeta = z.union([fontMetaStatic, fontMetaVariable]);
export const fontMetaUpdate = z.union([
fontMetaStatic.partial().strict(),
- fontMetaVariable.partial().strict(),
+ z
+ .object({
+ family: fontMetaVariable.shape.family.optional(),
+ style: fontStyle.optional(),
+ variationAxes: fontMetaVariable.shape.variationAxes.optional(),
+ })
+ .strict(),
]);
export type FontMeta = z.infer<typeof fontMeta>;
@@ -57,17 +66,17 @@ export const mergeFontMeta = (
if (result.success === false) {
return;
}
- const { family } = result.data;
+ const { family, style } = result.data;
if ("variationAxes" in current) {
const variationAxes =
"variationAxes" in result.data ? result.data.variationAxes : undefined;
return {
...current,
...(family === undefined ? {} : { family }),
+ ...(style === undefined ? {} : { style }),
...(variationAxes === undefined ? {} : { variationAxes }),
};
}
- const style = "style" in result.data ? result.data.style : undefined;
const weight = "weight" in result.data ? result.data.weight : undefined;
return {
...current,
diff --git a/packages/project-build/src/contracts/__generated__/runtime-operation-contracts.ts b/packages/project-build/src/contracts/__generated__/runtime-operation-contracts.ts
index 047776fbb4..22190e88af 100644
--- a/packages/project-build/src/contracts/__generated__/runtime-operation-contracts.ts
+++ b/packages/project-build/src/contracts/__generated__/runtime-operation-contracts.ts
@@ -3741,6 +3741,11 @@ export const runtimeOperationContractData = [
family: {
type: "string",
},
+ style: {
+ default: "normal",
+ type: "string",
+ enum: ["normal", "italic", "oblique"],
+ },
variationAxes: {
type: "object",
propertyNames: {
@@ -6858,6 +6863,11 @@ export const runtimeOperationContractData = [
family: {
type: "string",
},
+ style: {
+ default: "normal",
+ type: "string",
+ enum: ["normal", "italic", "oblique"],
+ },
variationAxes: {
type: "object",
propertyNames: {
@@ -9986,6 +9996,11 @@ export const runtimeOperationContractData = [
family: {
type: "string",
},
+ style: {
+ default: "normal",
+ type: "string",
+ enum: ["normal", "italic", "oblique"],
+ },
variationAxes: {
type: "object",
propertyNames: {
@@ -13103,6 +13118,11 @@ export const runtimeOperationContractData = [
family: {
type: "string",
},
+ style: {
+ default: "normal",
+ type: "string",
+ enum: ["normal", "italic", "oblique"],
+ },
variationAxes: {
type: "object",
propertyNames: {
@@ -18929,6 +18949,15 @@ export const runtimeOperationContractData = [
family: {
type: "string",
},
+ style: {
+ default: "normal",
+ type: "string",
+ enum: [
+ "normal",
+ "italic",
+ "oblique",
+ ],
+ },
variationAxes: {
type: "object",
propertyNames: {
@@ -22250,6 +22279,15 @@ export const runtimeOperationContractData = [
family: {
type: "string",
},
+ style: {
+ default: "normal",
+ type: "string",
+ enum: [
+ "normal",
+ "italic",
+ "oblique",
+ ],
+ },
variationAxes: {
type: "object",
propertyNames: {
@@ -36615,6 +36653,11 @@ export const runtimeOperationContractData = [
family: {
type: "string",
},
+ style: {
+ default: "normal",
+ type: "string",
+ enum: ["normal", "italic", "oblique"],
+ },
variationAxes: {
type: "object",
propertyNames: {
@@ -39811,6 +39854,11 @@ export const runtimeOperationContractData = [
family: {
type: "string",
},
+ style: {
+ default: "normal",
+ type: "string",
+ enum: ["normal", "italic", "oblique"],
+ },
variationAxes: {
type: "object",
propertyNames: {
@@ -46119,6 +46167,11 @@ export const runtimeOperationContractData = [
family: {
type: "string",
},
+ style: {
+ default: "normal",
+ type: "string",
+ enum: ["normal", "italic", "oblique"],
+ },
variationAxes: {
type: "object",
propertyNames: {
@@ -51925,6 +51978,11 @@ export const runtimeOperationContractData = [
family: {
type: "string",
},
+ style: {
+ default: "normal",
+ type: "string",
+ enum: ["normal", "italic", "oblique"],
+ },
variationAxes: {
type: "object",
propertyNames: {
@@ -70656,6 +70714,10 @@ export const runtimeOperationContractData = [
family: {
type: "string",
},
+ style: {
+ type: "string",
+ enum: ["normal", "italic", "oblique"],
+ },
variationAxes: {
type: "object",
propertyNames: {
@@ -70710,7 +70772,7 @@ export const runtimeOperationContractData = [
},
],
description:
- "Type-specific metadata: family/style/weight or variationAxes for fonts, width/height for images, and no fields for generic files.",
+ "Type-specific metadata: family and style plus weight for static fonts or variationAxes for variable fonts, width/height for images, and no fields for generic files.",
},
},
required: [],
@@ -70906,6 +70968,11 @@ export const runtimeOperationContractData = [
family: {
type: "string",
},
+ style: {
+ default: "normal",
+ type: "string",
+ enum: ["normal", "italic", "oblique"],
+ },
variationAxes: {
type: "object",
propertyNames: {
diff --git a/packages/project-build/src/runtime/assets.test.ts b/packages/project-build/src/runtime/assets.test.ts
index 60f2c1daf0..1e60545b2d 100644
--- a/packages/project-build/src/runtime/assets.test.ts
+++ b/packages/project-build/src/runtime/assets.test.ts
@@ -1154,6 +1154,23 @@ describe("updateAsset", () => {
values: { meta: { family: "Rajdhani Display", weight: 600 } },
});
+ const variationAxes = {
+ wght: { name: "Weight", min: 100, default: 400, max: 900 },
+ };
+ expect(
+ assetUpdateInput.parse({
+ assetId: "variable-font-1",
+ values: {
+ meta: { family: "Inter", style: "italic", variationAxes },
+ },
+ })
+ ).toEqual({
+ assetId: "variable-font-1",
+ values: {
+ meta: { family: "Inter", style: "italic", variationAxes },
+ },
+ });
+
expect(
assetUpdateInput.parse({
assetId: "image-1",
diff --git a/packages/project-build/src/runtime/assets.ts b/packages/project-build/src/runtime/assets.ts
index 7d4d7df7e0..4e24397d39 100644
--- a/packages/project-build/src/runtime/assets.ts
+++ b/packages/project-build/src/runtime/assets.ts
@@ -105,7 +105,7 @@ export const assetUpdateInput = z.object({
folderId: z.union([z.string().min(1), z.null()]).optional(),
meta: assetMetaUpdate
.describe(
- "Type-specific metadata: family/style/weight or variationAxes for fonts, width/height for images, and no fields for generic files."
+ "Type-specific metadata: family and style plus weight for static fonts or variationAxes for variable fonts, width/height for images, and no fields for generic files."
)
.optional(),
})
diff --git a/packages/sdk/src/assets.test.ts b/packages/sdk/src/assets.test.ts
index 9f17c01165..8b6852f641 100644
--- a/packages/sdk/src/assets.test.ts
+++ b/packages/sdk/src/assets.test.ts
@@ -1,4 +1,5 @@
import { describe, test, expect } from "vitest";
+import { fontMeta } from "@webstudio-is/fonts";
import {
ALLOWED_FILE_TYPES,
getMimeTypeByExtension,
@@ -1027,10 +1028,10 @@ describe("allowed-file-types", () => {
format: "woff2" as const,
description: null,
createdAt: "2024-01-01",
- meta: {
+ meta: fontMeta.parse({
family: "Inter",
variationAxes: {},
- },
+ }),
};
const mockGenericAsset = {
@@ -1064,7 +1065,7 @@ describe("allowed-file-types", () => {
});
});
- test("converts variable font asset without style/weight", () => {
+ test("defaults variable font style and omits weight", () => {
const result = toRuntimeAsset(
mockVariableFontAsset,
"https://example.com"
@@ -1072,6 +1073,7 @@ describe("allowed-file-types", () => {
expect(result).toEqual({
url: "/cgi/asset/variable-font.woff2?format=raw",
family: "Inter",
+ style: "normal",
});
});
diff --git a/packages/sdk/src/assets.ts b/packages/sdk/src/assets.ts
index c948c96f12..ea24cda642 100644
--- a/packages/sdk/src/assets.ts
+++ b/packages/sdk/src/assets.ts
@@ -586,10 +586,10 @@ const extractFontMetadata = (asset: Asset): RuntimeMetadata => {
}
const metadata: Omit<RuntimeAsset, "url"> = {
family: asset.meta.family,
+ style: asset.meta.style,
};
- // Static fonts have style and weight, variable fonts have variationAxes
- if ("style" in asset.meta) {
- metadata.style = asset.meta.style;
+ // Static fonts have weight, variable fonts have variationAxes
+ if ("weight" in asset.meta) {
metadata.weight = asset.meta.weight;
}
return metadata;
diff --git a/packages/sdk/src/css.test.tsx b/packages/sdk/src/css.test.tsx
index 24ff20d4e8..6e228846c5 100644
--- a/packages/sdk/src/css.test.tsx
+++ b/packages/sdk/src/css.test.tsx
@@ -2,6 +2,7 @@ import { expect, test } from "vitest";
import { $, ws, css, renderData, createProxy } from "@webstudio-is/template";
import { generateCss, type CssConfig } from "./css";
import type { Breakpoint } from "./schema/breakpoints";
+import type { Asset } from "./schema/assets";
import { rootComponent } from "./core-metas";
const toMap = <T extends { id: string }>(list: T[]) =>
@@ -19,6 +20,45 @@ const generateAllCss = (config: Omit<CssConfig, "atomic">) => {
return { cssText, atomicCssText, classes, atomicClasses };
};
+test("generates distinct upright and italic variable font faces", () => {
+ const variableFontAsset = (style: "normal" | "italic"): Asset => ({
+ id: style,
+ projectId: "project",
+ name: `family-${style}.woff2`,
+ type: "font",
+ size: 1,
+ format: "woff2",
+ createdAt: "2026-01-01T00:00:00.000Z",
+ description: null,
+ meta: {
+ family: "Family",
+ style,
+ variationAxes: {
+ wght: { name: "Weight", min: 100, default: 400, max: 900 },
+ },
+ },
+ });
+ const assets = toMap([
+ variableFontAsset("normal"),
+ variableFontAsset("italic"),
+ ]);
+ const { cssText } = generateCss({
+ ...renderData(<$.Body />),
+ assets,
+ atomic: false,
+ componentMetas: new Map(),
+ assetBaseUrl: "/assets/",
+ });
+
+ expect(cssText).toBe(`@font-face {
+ font-family: Family; font-style: normal; font-weight: 100 900; font-display: swap; src: url("/assets/family-normal.woff2") format("woff2");
+}
+@font-face {
+ font-family: Family; font-style: italic; font-weight: 100 900; font-display: swap; src: url("/assets/family-italic.woff2") format("woff2");
+}
+`);
+});
+
test("generate css for one instance with two tokens", () => {
const { cssText, atomicCssText, atomicClasses } = generateAllCss({
...renderData(<$.Box ws:id="box"></$.Box>),