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.

normalize family

Oleg Isonen031c82369528
diff --git a/packages/fonts/index.server.ts b/packages/fonts/index.server.ts
index ce974eb8c9..bb12041288 100644
--- a/packages/fonts/index.server.ts
+++ b/packages/fonts/index.server.ts
@@ -1,2 +1,2 @@
-export { getFontData } from "./src/get-font-data";
+export { getFontData } from "./src/font-data";
export * from "./src/schema";
diff --git a/packages/fonts/src/font-data.test.ts b/packages/fonts/src/font-data.test.ts
new file mode 100644
index 0000000000..fd0d357b7d
--- /dev/null
+++ b/packages/fonts/src/font-data.test.ts
@@ -0,0 +1,61 @@
+import { parseSubfamily, normalizeFamily } from "./font-data";
+
+describe("font-data", () => {
+ describe("parseSubfamily()", () => {
+ test("Black Italic", () => {
+ expect(parseSubfamily("Black Italic")).toEqual({
+ style: "italic",
+ weight: 900,
+ });
+ });
+ test("Bold", () => {
+ expect(parseSubfamily("Bold")).toEqual({
+ style: "normal",
+ weight: 700,
+ });
+ });
+ test("Demi Bold Italic", () => {
+ expect(parseSubfamily("Demi Bold Italic")).toEqual({
+ style: "italic",
+ weight: 600,
+ });
+ });
+ test("Light", () => {
+ expect(parseSubfamily("Light")).toEqual({
+ style: "normal",
+ weight: 300,
+ });
+ });
+ test("Extra Light", () => {
+ expect(parseSubfamily("Extra Light")).toEqual({
+ style: "normal",
+ weight: 200,
+ });
+ });
+ test("Extra Light Italic", () => {
+ expect(parseSubfamily("Extra Light Italic")).toEqual({
+ style: "italic",
+ weight: 200,
+ });
+ });
+ test("Heavy Italic", () => {
+ expect(parseSubfamily("Heavy Italic")).toEqual({
+ style: "italic",
+ weight: 900,
+ });
+ });
+ test("Medium Italic", () => {
+ expect(parseSubfamily("Medium Italic")).toEqual({
+ style: "italic",
+ weight: 500,
+ });
+ });
+ });
+
+ describe("normalizeFamily()", () => {
+ test("basic", () => {
+ expect(normalizeFamily("Roboto Black", "Black")).toBe("Roboto");
+ expect(normalizeFamily("Roboto Light", "Light Italic")).toBe("Roboto");
+ });
+ });
+});
diff --git a/packages/fonts/src/get-font-data.ts b/packages/fonts/src/font-data.ts
similarity index 79%
rename from packages/fonts/src/get-font-data.ts
rename to packages/fonts/src/font-data.ts
index 3f94cd4a3a..6b86d5bf62 100644
--- a/packages/fonts/src/get-font-data.ts
+++ b/packages/fonts/src/font-data.ts
@@ -52,6 +52,20 @@ export const parseSubfamily = (subfamily: string) => {
return { style, weight };
};
+export const normalizeFamily = (family: string, subfamily: string) => {
+ let simplifiedFamily = family;
+ subfamily
+ .trim()
+ .split(" ")
+ .forEach((word: string) => {
+ simplifiedFamily = simplifiedFamily.replace(
+ new RegExp(word.trim(), "i"),
+ ""
+ );
+ });
+ return simplifiedFamily.trim();
+};
+
type FontData = {
format: FontFormat;
family: string;
@@ -62,11 +76,11 @@ type FontData = {
export const getFontData = (data: Uint8Array): FontData => {
const font = fontkit.create(data as Buffer);
const format = font.type.toLowerCase() as FontData["format"];
- const family = font.getName("fontFamily");
+ const originalFamily = font.getName("fontFamily");
const subfamily =
font.getName("preferredSubfamily") ?? font.getName("fontSubfamily");
const parsedSubfamily = parseSubfamily(subfamily);
-
+ const family = normalizeFamily(originalFamily, subfamily);
return {
format,
family,
diff --git a/packages/fonts/src/get-font-data.test.ts b/packages/fonts/src/get-font-data.test.ts
deleted file mode 100644
index 84e9fe7f48..0000000000
--- a/packages/fonts/src/get-font-data.test.ts
+++ /dev/null
@@ -1,52 +0,0 @@
-import { parseSubfamily } from "./get-font-data";
-
-describe("parseSubfamily()", () => {
- test("Black Italic", () => {
- expect(parseSubfamily("Black Italic")).toEqual({
- style: "italic",
- weight: 900,
- });
- });
- test("Bold", () => {
- expect(parseSubfamily("Bold")).toEqual({
- style: "normal",
- weight: 700,
- });
- });
- test("Demi Bold Italic", () => {
- expect(parseSubfamily("Demi Bold Italic")).toEqual({
- style: "italic",
- weight: 600,
- });
- });
- test("Light", () => {
- expect(parseSubfamily("Light")).toEqual({
- style: "normal",
- weight: 300,
- });
- });
- test("Extra Light", () => {
- expect(parseSubfamily("Extra Light")).toEqual({
- style: "normal",
- weight: 200,
- });
- });
- test("Extra Light Italic", () => {
- expect(parseSubfamily("Extra Light Italic")).toEqual({
- style: "italic",
- weight: 200,
- });
- });
- test("Heavy Italic", () => {
- expect(parseSubfamily("Heavy Italic")).toEqual({
- style: "italic",
- weight: 900,
- });
- });
- test("Medium Italic", () => {
- expect(parseSubfamily("Medium Italic")).toEqual({
- style: "italic",
- weight: 500,
- });
- });
-});
diff --git a/packages/fonts/src/schema.ts b/packages/fonts/src/schema.ts
index cb065f5ed2..a5e7e6ec90 100644
--- a/packages/fonts/src/schema.ts
+++ b/packages/fonts/src/schema.ts
@@ -1,5 +1,5 @@
import { z } from "zod";
-import { styles } from "./get-font-data";
+import { styles } from "./font-data";
export const FontMeta = z.object({
family: z.string(),