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 font rendering for different weights/styles

Oleg Isonen68d22988cc5f
diff --git a/packages/fonts/src/__snapshots__/get-font-faces.test.ts.snap b/packages/fonts/src/__snapshots__/get-font-faces.test.ts.snap
index b102e6f728..ac7ee32c9d 100644
--- a/packages/fonts/src/__snapshots__/get-font-faces.test.ts.snap
+++ b/packages/fonts/src/__snapshots__/get-font-faces.test.ts.snap
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
-exports[`getFontFaces() same family, but different formats 1`] = `
+exports[`getFontFaces() different formats 1`] = `
Array [
Object {
"fontDisplay": "swap",
@@ -11,3 +11,41 @@ Array [
},
]
`;
+
+exports[`getFontFaces() different style 1`] = `
+Array [
+ Object {
+ "fontDisplay": "swap",
+ "fontFamily": "Roboto",
+ "fontStyle": "normal",
+ "fontWeight": 400,
+ "src": "url('/fonts/roboto.ttf') format('truetype')",
+ },
+ Object {
+ "fontDisplay": "swap",
+ "fontFamily": "Roboto",
+ "fontStyle": "italic",
+ "fontWeight": 400,
+ "src": "url('/fonts/roboto-italic.ttf') format('truetype')",
+ },
+]
+`;
+
+exports[`getFontFaces() different weight 1`] = `
+Array [
+ Object {
+ "fontDisplay": "swap",
+ "fontFamily": "Roboto",
+ "fontStyle": "normal",
+ "fontWeight": 400,
+ "src": "url('/fonts/roboto.ttf') format('truetype')",
+ },
+ Object {
+ "fontDisplay": "swap",
+ "fontFamily": "Roboto",
+ "fontStyle": "normal",
+ "fontWeight": 500,
+ "src": "url('/fonts/roboto-bold.ttf') format('truetype')",
+ },
+]
+`;
diff --git a/packages/fonts/src/get-font-faces.test.ts b/packages/fonts/src/get-font-faces.test.ts
index 8cd5ef4ae4..7820f10d04 100644
--- a/packages/fonts/src/get-font-faces.test.ts
+++ b/packages/fonts/src/get-font-faces.test.ts
@@ -1,7 +1,7 @@
import { getFontFaces, type PartialFontAsset } from "./get-font-faces";
describe("getFontFaces()", () => {
- test("same family, but different formats", () => {
+ test("different formats", () => {
const assets: Array<PartialFontAsset> = [
{
format: "woff",
@@ -24,4 +24,52 @@ describe("getFontFaces()", () => {
];
expect(getFontFaces(assets)).toMatchSnapshot();
});
+
+ test("different style", () => {
+ const assets: Array<PartialFontAsset> = [
+ {
+ format: "ttf",
+ meta: {
+ family: "Roboto",
+ style: "normal",
+ weight: 400,
+ },
+ path: "/fonts/roboto.ttf",
+ },
+ {
+ format: "ttf",
+ meta: {
+ family: "Roboto",
+ style: "italic",
+ weight: 400,
+ },
+ path: "/fonts/roboto-italic.ttf",
+ },
+ ];
+ expect(getFontFaces(assets)).toMatchSnapshot();
+ });
+
+ test("different weight", () => {
+ const assets: Array<PartialFontAsset> = [
+ {
+ format: "ttf",
+ meta: {
+ family: "Roboto",
+ style: "normal",
+ weight: 400,
+ },
+ path: "/fonts/roboto.ttf",
+ },
+ {
+ format: "ttf",
+ meta: {
+ family: "Roboto",
+ style: "normal",
+ weight: 500,
+ },
+ path: "/fonts/roboto-bold.ttf",
+ },
+ ];
+ expect(getFontFaces(assets)).toMatchSnapshot();
+ });
});
diff --git a/packages/fonts/src/get-font-faces.ts b/packages/fonts/src/get-font-faces.ts
index 02be31f7ff..15311910fc 100644
--- a/packages/fonts/src/get-font-faces.ts
+++ b/packages/fonts/src/get-font-faces.ts
@@ -16,22 +16,34 @@ export type FontFace = {
src: string;
};
+const formatFace = (asset: PartialFontAsset, format: string) => {
+ return {
+ fontFamily: asset.meta.family,
+ fontStyle: asset.meta.style,
+ fontWeight: asset.meta.weight,
+ fontDisplay: "swap",
+ src: `url('${asset.path}') format('${format}')`,
+ };
+};
+
+const getKey = (asset: PartialFontAsset) =>
+ asset.meta.family + asset.meta.style + asset.meta.weight;
+
export const getFontFaces = (
assets: Array<PartialFontAsset>
): Array<FontFace> => {
const faces = new Map();
for (const asset of assets) {
- const face = faces.get(asset.meta.family);
+ const face = faces.get(getKey(asset));
const format = FONT_FORMATS.get(asset.format);
+ if (format === undefined) {
+ // Should never happen since we allow only uploading formats we support
+ continue;
+ }
if (face === undefined) {
- faces.set(asset.meta.family, {
- fontFamily: asset.meta.family,
- fontStyle: asset.meta.style,
- fontWeight: asset.meta.weight,
- fontDisplay: "swap",
- src: `url('${asset.path}') format('${format}')`,
- });
+ const face = formatFace(asset, format);
+ faces.set(getKey(asset), face);
continue;
}