@webstudio-is/fonts 0.55.0 → 0.57.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -22,14 +22,14 @@ __export(get_font_faces_exports, {
22
22
  });
23
23
  module.exports = __toCommonJS(get_font_faces_exports);
24
24
  var import_constants = require("./constants");
25
- const formatFace = (asset, format) => {
25
+ const formatFace = (asset, format, url) => {
26
26
  if ("variationAxes" in asset.meta) {
27
27
  const { wght, wdth } = asset.meta?.variationAxes ?? {};
28
28
  return {
29
29
  fontFamily: asset.meta.family,
30
30
  fontStyle: "normal",
31
31
  fontDisplay: "swap",
32
- src: `url('${asset.path}') format('${format}')`,
32
+ src: `url('${url}') format('${format}')`,
33
33
  fontStretch: wdth ? `${wdth.min}% ${wdth.max}%` : void 0,
34
34
  fontWeight: wght ? `${wght.min} ${wght.max}` : void 0
35
35
  };
@@ -39,7 +39,7 @@ const formatFace = (asset, format) => {
39
39
  fontStyle: asset.meta.style,
40
40
  fontWeight: asset.meta.weight,
41
41
  fontDisplay: "swap",
42
- src: `url('${asset.path}') format('${format}')`
42
+ src: `url('${url}') format('${format}')`
43
43
  };
44
44
  };
45
45
  const getKey = (asset) => {
@@ -48,9 +48,11 @@ const getKey = (asset) => {
48
48
  }
49
49
  return asset.meta.family + asset.meta.style + asset.meta.weight;
50
50
  };
51
- const getFontFaces = (assets) => {
51
+ const getFontFaces = (assets, options) => {
52
+ const { assetBaseUrl } = options;
52
53
  const faces = /* @__PURE__ */ new Map();
53
54
  for (const asset of assets) {
55
+ const url = `${assetBaseUrl}${asset.name}`;
54
56
  const assetKey = getKey(asset);
55
57
  const face = faces.get(assetKey);
56
58
  const format = import_constants.FONT_FORMATS.get(asset.format);
@@ -58,11 +60,11 @@ const getFontFaces = (assets) => {
58
60
  continue;
59
61
  }
60
62
  if (face === void 0) {
61
- const face2 = formatFace(asset, format);
63
+ const face2 = formatFace(asset, format, url);
62
64
  faces.set(assetKey, face2);
63
65
  continue;
64
66
  }
65
- face.src += `, url('${asset.path}') format('${format}')`;
67
+ face.src += `, url('${url}') format('${format}')`;
66
68
  }
67
69
  return Array.from(faces.values());
68
70
  };
@@ -1,12 +1,12 @@
1
1
  import { FONT_FORMATS } from "./constants";
2
- const formatFace = (asset, format) => {
2
+ const formatFace = (asset, format, url) => {
3
3
  if ("variationAxes" in asset.meta) {
4
4
  const { wght, wdth } = asset.meta?.variationAxes ?? {};
5
5
  return {
6
6
  fontFamily: asset.meta.family,
7
7
  fontStyle: "normal",
8
8
  fontDisplay: "swap",
9
- src: `url('${asset.path}') format('${format}')`,
9
+ src: `url('${url}') format('${format}')`,
10
10
  fontStretch: wdth ? `${wdth.min}% ${wdth.max}%` : void 0,
11
11
  fontWeight: wght ? `${wght.min} ${wght.max}` : void 0
12
12
  };
@@ -16,7 +16,7 @@ const formatFace = (asset, format) => {
16
16
  fontStyle: asset.meta.style,
17
17
  fontWeight: asset.meta.weight,
18
18
  fontDisplay: "swap",
19
- src: `url('${asset.path}') format('${format}')`
19
+ src: `url('${url}') format('${format}')`
20
20
  };
21
21
  };
22
22
  const getKey = (asset) => {
@@ -25,9 +25,11 @@ const getKey = (asset) => {
25
25
  }
26
26
  return asset.meta.family + asset.meta.style + asset.meta.weight;
27
27
  };
28
- const getFontFaces = (assets) => {
28
+ const getFontFaces = (assets, options) => {
29
+ const { assetBaseUrl } = options;
29
30
  const faces = /* @__PURE__ */ new Map();
30
31
  for (const asset of assets) {
32
+ const url = `${assetBaseUrl}${asset.name}`;
31
33
  const assetKey = getKey(asset);
32
34
  const face = faces.get(assetKey);
33
35
  const format = FONT_FORMATS.get(asset.format);
@@ -35,11 +37,11 @@ const getFontFaces = (assets) => {
35
37
  continue;
36
38
  }
37
39
  if (face === void 0) {
38
- const face2 = formatFace(asset, format);
40
+ const face2 = formatFace(asset, format, url);
39
41
  faces.set(assetKey, face2);
40
42
  continue;
41
43
  }
42
- face.src += `, url('${asset.path}') format('${format}')`;
44
+ face.src += `, url('${url}') format('${format}')`;
43
45
  }
44
46
  return Array.from(faces.values());
45
47
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webstudio-is/fonts",
3
- "version": "0.55.0",
3
+ "version": "0.57.0",
4
4
  "description": "Fonts utils",
5
5
  "author": "Webstudio <github@webstudio.is>",
6
6
  "homepage": "https://webstudio.is",
@@ -14,10 +14,10 @@
14
14
  "jest": "^29.3.1",
15
15
  "typescript": "5.0.3",
16
16
  "zod": "^3.19.1",
17
- "@webstudio-is/design-system": "^0.55.0",
18
- "@webstudio-is/jest-config": "^1.0.2",
17
+ "@webstudio-is/design-system": "^0.57.0",
18
+ "@webstudio-is/jest-config": "^1.0.4",
19
19
  "@webstudio-is/scripts": "^0.0.0",
20
- "@webstudio-is/tsconfig": "^1.0.3"
20
+ "@webstudio-is/tsconfig": "^1.0.4"
21
21
  },
22
22
  "peerDependencies": {
23
23
  "zod": "^3.19.1"
@@ -44,12 +44,12 @@
44
44
  "private": false,
45
45
  "sideEffects": false,
46
46
  "scripts": {
47
- "typecheck": "tsc --noEmit",
47
+ "typecheck": "tsc --noEmit --emitDeclarationOnly false",
48
48
  "test": "NODE_OPTIONS=--experimental-vm-modules jest",
49
49
  "checks": "pnpm typecheck && pnpm lint && pnpm test",
50
50
  "dev": "build-package --watch",
51
51
  "build": "build-package",
52
- "dts": "tsc --emitDeclarationOnly --declaration --declarationDir lib/types",
52
+ "dts": "tsc",
53
53
  "lint": "eslint ./src --ext .ts,.tsx --max-warnings 0"
54
54
  }
55
55
  }
@@ -11,7 +11,7 @@ describe("getFontFaces()", () => {
11
11
  style: "normal",
12
12
  weight: 400,
13
13
  },
14
- path: "/fonts/roboto.woff",
14
+ name: "roboto.woff",
15
15
  },
16
16
  {
17
17
  format: "ttf",
@@ -20,10 +20,10 @@ describe("getFontFaces()", () => {
20
20
  style: "normal",
21
21
  weight: 400,
22
22
  },
23
- path: "/fonts/roboto.ttf",
23
+ name: "roboto.ttf",
24
24
  },
25
25
  ];
26
- expect(getFontFaces(assets)).toMatchSnapshot();
26
+ expect(getFontFaces(assets, { assetBaseUrl: "/fonts/" })).toMatchSnapshot();
27
27
  });
28
28
 
29
29
  test("different style", () => {
@@ -35,7 +35,7 @@ describe("getFontFaces()", () => {
35
35
  style: "normal",
36
36
  weight: 400,
37
37
  },
38
- path: "/fonts/roboto.ttf",
38
+ name: "roboto.ttf",
39
39
  },
40
40
  {
41
41
  format: "ttf",
@@ -44,10 +44,10 @@ describe("getFontFaces()", () => {
44
44
  style: "italic",
45
45
  weight: 400,
46
46
  },
47
- path: "/fonts/roboto-italic.ttf",
47
+ name: "roboto-italic.ttf",
48
48
  },
49
49
  ];
50
- expect(getFontFaces(assets)).toMatchSnapshot();
50
+ expect(getFontFaces(assets, { assetBaseUrl: "/fonts/" })).toMatchSnapshot();
51
51
  });
52
52
 
53
53
  test("different weight", () => {
@@ -59,7 +59,7 @@ describe("getFontFaces()", () => {
59
59
  style: "normal",
60
60
  weight: 400,
61
61
  },
62
- path: "/fonts/roboto.ttf",
62
+ name: "roboto.ttf",
63
63
  },
64
64
  {
65
65
  format: "ttf",
@@ -68,10 +68,10 @@ describe("getFontFaces()", () => {
68
68
  style: "normal",
69
69
  weight: 500,
70
70
  },
71
- path: "/fonts/roboto-bold.ttf",
71
+ name: "roboto-bold.ttf",
72
72
  },
73
73
  ];
74
- expect(getFontFaces(assets)).toMatchSnapshot();
74
+ expect(getFontFaces(assets, { assetBaseUrl: "/fonts/" })).toMatchSnapshot();
75
75
  });
76
76
 
77
77
  test("variable font", () => {
@@ -96,9 +96,9 @@ describe("getFontFaces()", () => {
96
96
  YTFI: { name: "YTFI", min: 560, default: 738, max: 788 },
97
97
  },
98
98
  },
99
- path: "/fonts/inter.ttf",
99
+ name: "inter.ttf",
100
100
  },
101
101
  ];
102
- expect(getFontFaces(assets)).toMatchSnapshot();
102
+ expect(getFontFaces(assets, { assetBaseUrl: "/fonts/" })).toMatchSnapshot();
103
103
  });
104
104
  });
@@ -4,7 +4,7 @@ import type { FontMeta, FontFormat, FontMetaStatic } from "./schema";
4
4
  export type PartialFontAsset = {
5
5
  format: FontFormat;
6
6
  meta: FontMeta;
7
- path: string;
7
+ name: string;
8
8
  };
9
9
 
10
10
  export type FontFace = {
@@ -16,14 +16,18 @@ export type FontFace = {
16
16
  fontStretch?: string;
17
17
  };
18
18
 
19
- const formatFace = (asset: PartialFontAsset, format: string): FontFace => {
19
+ const formatFace = (
20
+ asset: PartialFontAsset,
21
+ format: string,
22
+ url: string
23
+ ): FontFace => {
20
24
  if ("variationAxes" in asset.meta) {
21
25
  const { wght, wdth } = asset.meta?.variationAxes ?? {};
22
26
  return {
23
27
  fontFamily: asset.meta.family,
24
28
  fontStyle: "normal",
25
29
  fontDisplay: "swap",
26
- src: `url('${asset.path}') format('${format}')`,
30
+ src: `url('${url}') format('${format}')`,
27
31
  fontStretch: wdth ? `${wdth.min}% ${wdth.max}%` : undefined,
28
32
  fontWeight: wght ? `${wght.min} ${wght.max}` : undefined,
29
33
  };
@@ -33,7 +37,7 @@ const formatFace = (asset: PartialFontAsset, format: string): FontFace => {
33
37
  fontStyle: asset.meta.style,
34
38
  fontWeight: asset.meta.weight,
35
39
  fontDisplay: "swap",
36
- src: `url('${asset.path}') format('${format}')`,
40
+ src: `url('${url}') format('${format}')`,
37
41
  };
38
42
  };
39
43
 
@@ -45,10 +49,15 @@ const getKey = (asset: PartialFontAsset) => {
45
49
  };
46
50
 
47
51
  export const getFontFaces = (
48
- assets: Array<PartialFontAsset>
52
+ assets: Array<PartialFontAsset>,
53
+ options: {
54
+ assetBaseUrl: string;
55
+ }
49
56
  ): Array<FontFace> => {
57
+ const { assetBaseUrl } = options;
50
58
  const faces = new Map();
51
59
  for (const asset of assets) {
60
+ const url = `${assetBaseUrl}${asset.name}`;
52
61
  const assetKey = getKey(asset);
53
62
  const face = faces.get(assetKey);
54
63
  const format = FONT_FORMATS.get(asset.format);
@@ -58,13 +67,13 @@ export const getFontFaces = (
58
67
  }
59
68
 
60
69
  if (face === undefined) {
61
- const face = formatFace(asset, format);
70
+ const face = formatFace(asset, format, url);
62
71
  faces.set(assetKey, face);
63
72
  continue;
64
73
  }
65
74
 
66
75
  // We already have that font face, so we need to add the new src
67
- face.src += `, url('${asset.path}') format('${format}')`;
76
+ face.src += `, url('${url}') format('${format}')`;
68
77
  }
69
78
  return Array.from(faces.values());
70
79
  };
@@ -1,5 +0,0 @@
1
- import type { FontFormat } from "./schema";
2
- export declare const SYSTEM_FONTS: Map<string, string[]>;
3
- export declare const DEFAULT_FONT_FALLBACK = "sans-serif";
4
- export declare const FONT_FORMATS: Map<FontFormat, string>;
5
- export declare const FONT_MIME_TYPES: string;
@@ -1,29 +0,0 @@
1
- import type { FontFormat, VariationAxes } from "./schema";
2
- declare module "fontkit" {
3
- interface Font {
4
- type: string;
5
- getName: (name: string) => string;
6
- variationAxes: VariationAxes;
7
- }
8
- }
9
- export declare const styles: readonly ["normal", "italic", "oblique"];
10
- type Style = (typeof styles)[number];
11
- export declare const parseSubfamily: (subfamily: string) => {
12
- style: "normal" | "italic" | "oblique";
13
- weight: number;
14
- };
15
- export declare const normalizeFamily: (family: string, subfamily: string) => string;
16
- type FontDataStatic = {
17
- format: FontFormat;
18
- family: string;
19
- style: Style;
20
- weight: number;
21
- };
22
- type FontDataVariable = {
23
- format: FontFormat;
24
- family: string;
25
- variationAxes: VariationAxes;
26
- };
27
- type FontData = FontDataStatic | FontDataVariable;
28
- export declare const getFontData: (data: Uint8Array) => FontData;
29
- export {};
@@ -1 +0,0 @@
1
- export {};
@@ -1,49 +0,0 @@
1
- export declare const fontWeights: {
2
- readonly "100": {
3
- readonly label: "Thin";
4
- readonly name: "thin";
5
- readonly alt: "hairline";
6
- };
7
- readonly "200": {
8
- readonly label: "Extra Light";
9
- readonly name: "extra light";
10
- readonly alt: "ultra light";
11
- };
12
- readonly "300": {
13
- readonly label: "Light";
14
- readonly name: "light";
15
- readonly alt: "light";
16
- };
17
- readonly "400": {
18
- readonly label: "Normal";
19
- readonly name: "normal";
20
- readonly alt: "normal";
21
- };
22
- readonly "500": {
23
- readonly label: "Medium";
24
- readonly name: "medium";
25
- readonly alt: "medium";
26
- };
27
- readonly "600": {
28
- readonly label: "Semi Bold";
29
- readonly name: "semi bold";
30
- readonly alt: "demi bold";
31
- };
32
- readonly "700": {
33
- readonly label: "Bold";
34
- readonly name: "bold";
35
- readonly alt: "bold";
36
- };
37
- readonly "800": {
38
- readonly label: "Extra Bold";
39
- readonly name: "extra bold";
40
- readonly alt: "ultra bold";
41
- };
42
- readonly "900": {
43
- readonly label: "Black";
44
- readonly name: "black";
45
- readonly alt: "heavy";
46
- };
47
- };
48
- export type FontWeight = keyof typeof fontWeights;
49
- export type FontWeightKeyword = (typeof fontWeights)[FontWeight]["name"] | (typeof fontWeights)[FontWeight]["alt"];
@@ -1,15 +0,0 @@
1
- import type { FontMeta, FontFormat, FontMetaStatic } from "./schema";
2
- export type PartialFontAsset = {
3
- format: FontFormat;
4
- meta: FontMeta;
5
- path: string;
6
- };
7
- export type FontFace = {
8
- fontFamily: string;
9
- fontDisplay: "swap" | "auto" | "block" | "fallback" | "optional";
10
- src: string;
11
- fontStyle?: FontMetaStatic["style"];
12
- fontWeight?: number | string;
13
- fontStretch?: string;
14
- };
15
- export declare const getFontFaces: (assets: Array<PartialFontAsset>) => Array<FontFace>;
@@ -1 +0,0 @@
1
- export {};
@@ -1,4 +0,0 @@
1
- export * from "./constants";
2
- export * from "./get-font-faces";
3
- export * from "./schema";
4
- export * from "./font-weights";
@@ -1 +0,0 @@
1
- export { getFontData } from "./font-data";
@@ -1,83 +0,0 @@
1
- import { z } from "zod";
2
- export declare const FontFormat: z.ZodUnion<[z.ZodLiteral<"ttf">, z.ZodLiteral<"woff">, z.ZodLiteral<"woff2">, z.ZodLiteral<"otf">]>;
3
- export type FontFormat = z.infer<typeof FontFormat>;
4
- declare const VariationAxes: z.ZodRecord<z.ZodEnum<["wght", "wdth", "slnt", "opsz", "ital", "GRAD", "XTRA", "XOPQ", "YOPQ", "YTLC", "YTUC", "YTAS", "YTDE", "YTFI"]>, z.ZodObject<{
5
- name: z.ZodString;
6
- min: z.ZodNumber;
7
- default: z.ZodNumber;
8
- max: z.ZodNumber;
9
- }, "strip", z.ZodTypeAny, {
10
- name: string;
11
- min: number;
12
- default: number;
13
- max: number;
14
- }, {
15
- name: string;
16
- min: number;
17
- default: number;
18
- max: number;
19
- }>>;
20
- export type VariationAxes = z.infer<typeof VariationAxes>;
21
- export declare const FontMetaStatic: z.ZodObject<{
22
- family: z.ZodString;
23
- style: z.ZodEnum<["normal", "italic", "oblique"]>;
24
- weight: z.ZodNumber;
25
- }, "strip", z.ZodTypeAny, {
26
- style: "normal" | "italic" | "oblique";
27
- weight: number;
28
- family: string;
29
- }, {
30
- style: "normal" | "italic" | "oblique";
31
- weight: number;
32
- family: string;
33
- }>;
34
- export type FontMetaStatic = z.infer<typeof FontMetaStatic>;
35
- export declare const FontMeta: z.ZodUnion<[z.ZodObject<{
36
- family: z.ZodString;
37
- style: z.ZodEnum<["normal", "italic", "oblique"]>;
38
- weight: z.ZodNumber;
39
- }, "strip", z.ZodTypeAny, {
40
- style: "normal" | "italic" | "oblique";
41
- weight: number;
42
- family: string;
43
- }, {
44
- style: "normal" | "italic" | "oblique";
45
- weight: number;
46
- family: string;
47
- }>, z.ZodObject<{
48
- family: z.ZodString;
49
- variationAxes: z.ZodRecord<z.ZodEnum<["wght", "wdth", "slnt", "opsz", "ital", "GRAD", "XTRA", "XOPQ", "YOPQ", "YTLC", "YTUC", "YTAS", "YTDE", "YTFI"]>, z.ZodObject<{
50
- name: z.ZodString;
51
- min: z.ZodNumber;
52
- default: z.ZodNumber;
53
- max: z.ZodNumber;
54
- }, "strip", z.ZodTypeAny, {
55
- name: string;
56
- min: number;
57
- default: number;
58
- max: number;
59
- }, {
60
- name: string;
61
- min: number;
62
- default: number;
63
- max: number;
64
- }>>;
65
- }, "strip", z.ZodTypeAny, {
66
- variationAxes: Partial<Record<"wght" | "wdth" | "slnt" | "opsz" | "ital" | "GRAD" | "XTRA" | "XOPQ" | "YOPQ" | "YTLC" | "YTUC" | "YTAS" | "YTDE" | "YTFI", {
67
- name: string;
68
- min: number;
69
- default: number;
70
- max: number;
71
- }>>;
72
- family: string;
73
- }, {
74
- variationAxes: Partial<Record<"wght" | "wdth" | "slnt" | "opsz" | "ital" | "GRAD" | "XTRA" | "XOPQ" | "YOPQ" | "YTLC" | "YTUC" | "YTAS" | "YTDE" | "YTFI", {
75
- name: string;
76
- min: number;
77
- default: number;
78
- max: number;
79
- }>>;
80
- family: string;
81
- }>]>;
82
- export type FontMeta = z.infer<typeof FontMeta>;
83
- export {};