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.
TypeScript accessibilityalternativecloudflarecloudflare-workersdesignfreefrontendopen-sourceradix-uireactremixweb-developmentwebflowwebsite-builderwebsite-developmentwebsiteswebstudio
fix: Fallback to file name when family name is not defined in the meta (#3627)
Oleg Isonen42c7dd186f57
diff --git a/packages/asset-uploader/src/clients/fs/upload.ts b/packages/asset-uploader/src/clients/fs/upload.tsindex 01f756f5c3..82082c3ee4 100644--- a/packages/asset-uploader/src/clients/fs/upload.ts+++ b/packages/asset-uploader/src/clients/fs/upload.ts@@ -30,6 +30,7 @@ export const uploadToFs = async ({type: type.startsWith("image") ? "image" : "font",size: data.byteLength,data,+ name,});return assetData;diff --git a/packages/asset-uploader/src/clients/s3/upload.ts b/packages/asset-uploader/src/clients/s3/upload.tsindex 6b6315f65f..8d6f5cabe7 100644--- a/packages/asset-uploader/src/clients/s3/upload.ts+++ b/packages/asset-uploader/src/clients/s3/upload.ts@@ -69,6 +69,7 @@ export const uploadToS3 = async ({type: type.startsWith("image") ? "image" : "font",size: data.byteLength,data: new Uint8Array(data),+ name,});return assetData;diff --git a/packages/asset-uploader/src/utils/font-data.test.ts b/packages/asset-uploader/src/utils/font-data.test.tsindex ff8839c959..43892935bf 100644--- a/packages/asset-uploader/src/utils/font-data.test.ts+++ b/packages/asset-uploader/src/utils/font-data.test.ts@@ -1,5 +1,7 @@import { describe, test, expect } from "@jest/globals";-import { parseSubfamily, normalizeFamily } from "./font-data";+import { parseSubfamily, __testing__ } from "./font-data";++const { normalizeFamily } = __testing__;describe("font-data", () => {describe("parseSubfamily()", () => {@@ -55,16 +57,31 @@ describe("font-data", () => {describe("normalizeFamily()", () => {test("basic", () => {- expect(normalizeFamily("Roboto Black", "Black")).toBe("Roboto");- expect(normalizeFamily("Roboto Light", "Light Italic")).toBe("Roboto");- expect(normalizeFamily("Robolder Bold", "Bold")).toBe("Robolder");- expect(normalizeFamily(" Roboto X Bold ", "Bold")).toBe("Roboto X");- expect(normalizeFamily(" 'Roboto X' Bold ", "Bold")).toBe("'Roboto X'");- expect(normalizeFamily(` "Roboto X" Bold `, "Bold")).toBe(`"Roboto X"`);- expect(normalizeFamily(`"Roboto Bold"`, "Bold")).toBe(`"Roboto Bold"`);- expect(normalizeFamily(`"Roboto Bold" Bold`, "Bold")).toBe(+ expect(normalizeFamily("Roboto Black", "Black", "font.woff")).toBe(+ "Roboto"+ );+ expect(normalizeFamily("Roboto Light", "Light Italic", "font.woff")).toBe(+ "Roboto"+ );+ expect(normalizeFamily("Robolder Bold", "Bold", "font.woff")).toBe(+ "Robolder"+ );+ expect(normalizeFamily(" Roboto X Bold ", "Bold", "font.woff")).toBe(+ "Roboto X"+ );+ expect(normalizeFamily(" 'Roboto X' Bold ", "Bold", "font.woff")).toBe(+ "'Roboto X'"+ );+ expect(normalizeFamily(` "Roboto X" Bold `, "Bold", "font.woff")).toBe(+ `"Roboto X"`+ );+ expect(normalizeFamily(`"Roboto Bold"`, "Bold", "font.woff")).toBe(+ `"Roboto Bold"`+ );+ expect(normalizeFamily(`"Roboto Bold" Bold`, "Bold", "font.woff")).toBe(`"Roboto Bold"`);+ expect(normalizeFamily("", "", "font.woff")).toBe(`font`);});});});diff --git a/packages/asset-uploader/src/utils/font-data.ts b/packages/asset-uploader/src/utils/font-data.tsindex e2f2e8a948..ec07b942ac 100644--- a/packages/asset-uploader/src/utils/font-data.ts+++ b/packages/asset-uploader/src/utils/font-data.ts@@ -45,13 +45,22 @@ const splitAndTrim = (string: string) =>// Family name can contain additional information like "Roboto Black" or "Roboto Bold", though we need pure family name "Roboto", because the rest is already encoded in weight and style.// We need a name we can reference in CSS font-family property, while CSS matches it with the right font-face considering the weight and style.-export const normalizeFamily = (family: string, subfamily: string) => {+const normalizeFamily = (+ family: string,+ subfamily: string,+ fileName: string+) => {const familyParts = splitAndTrim(family);const subfamilyParts = splitAndTrim(subfamily.toLowerCase());const familyPartsNormalized = familyParts.filter((familyPart) => subfamilyParts.includes(familyPart.toLowerCase()) === false);- return familyPartsNormalized.join(" ");+ if (familyPartsNormalized.length !== 0) {+ return familyPartsNormalized.join(" ");+ }+ // Broken fonts may lack any family information, so last resort is to use the file name+ const extensionIndex = fileName.lastIndexOf(".");+ return extensionIndex === -1 ? fileName : fileName.slice(0, extensionIndex);};type FontDataStatic = {@@ -67,7 +76,7 @@ type FontDataVariable = {};type FontData = FontDataStatic | FontDataVariable;-export const getFontData = (data: Uint8Array): FontData => {+export const getFontData = (data: Uint8Array, fileName: string): FontData => {const font = createFontKit(data as Buffer);if (font.type !== "TTF" && font.type !== "WOFF" && font.type !== "WOFF2") {throw Error(`Unsupported font type ${font.type}`);@@ -78,7 +87,7 @@ export const getFontData = (data: Uint8Array): FontData => {font.getName("preferredSubfamily", defaultLanguage) ??font.getName("fontSubfamily", defaultLanguage) ??"";- const family = normalizeFamily(originalFamily, subfamily);+ const family = normalizeFamily(originalFamily, subfamily, fileName);const isVariable = Object.keys(font.variationAxes).length !== 0;if (isVariable) {@@ -95,3 +104,5 @@ export const getFontData = (data: Uint8Array): FontData => {...parseSubfamily(subfamily),};};++export const __testing__ = { normalizeFamily };diff --git a/packages/asset-uploader/src/utils/get-asset-data.ts b/packages/asset-uploader/src/utils/get-asset-data.tsindex 7d6aa289fc..25adbf5d7a 100644--- a/packages/asset-uploader/src/utils/get-asset-data.ts+++ b/packages/asset-uploader/src/utils/get-asset-data.ts@@ -15,6 +15,7 @@ export type AssetData = z.infer<typeof AssetData>;type BaseAssetOptions = {size: number;data: Uint8Array;+ name: string;};type AssetOptions =@@ -51,8 +52,7 @@ export const getAssetData = async (meta: { width, height },};}-- const { format, ...meta } = getFontData(options.data);+ const { format, ...meta } = getFontData(options.data, options.name);return {size: options.size,