@webstudio-is/fonts 0.1.0 → 0.2.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.
Files changed (60) hide show
  1. package/lib/cjs/constants.cjs +19 -0
  2. package/lib/cjs/constants.d.ts +6 -0
  3. package/lib/cjs/constants.d.ts.map +1 -0
  4. package/lib/cjs/font-data.cjs +53 -0
  5. package/lib/cjs/font-data.d.ts +23 -0
  6. package/lib/cjs/font-data.d.ts.map +1 -0
  7. package/lib/cjs/font-data.test.cjs +67 -0
  8. package/lib/cjs/font-data.test.d.ts +2 -0
  9. package/lib/cjs/font-data.test.d.ts.map +1 -0
  10. package/lib/cjs/font-weights.cjs +50 -0
  11. package/lib/cjs/font-weights.d.ts +50 -0
  12. package/lib/cjs/font-weights.d.ts.map +1 -0
  13. package/lib/cjs/get-font-faces.cjs +34 -0
  14. package/lib/cjs/get-font-faces.d.ts +16 -0
  15. package/lib/cjs/get-font-faces.d.ts.map +1 -0
  16. package/lib/cjs/get-font-faces.test.cjs +74 -0
  17. package/lib/cjs/get-font-faces.test.d.ts +2 -0
  18. package/lib/cjs/get-font-faces.test.d.ts.map +1 -0
  19. package/lib/cjs/index.cjs +20 -0
  20. package/lib/cjs/index.d.ts +5 -0
  21. package/lib/cjs/index.d.ts.map +1 -0
  22. package/lib/cjs/index.server.cjs +20 -0
  23. package/lib/cjs/index.server.d.ts +3 -0
  24. package/lib/cjs/index.server.d.ts.map +1 -0
  25. package/lib/cjs/schema.cjs +10 -0
  26. package/lib/cjs/schema.d.ts +16 -0
  27. package/lib/cjs/schema.d.ts.map +1 -0
  28. package/lib/cjs/types.cjs +2 -0
  29. package/lib/cjs/types.d.ts +2 -0
  30. package/lib/cjs/types.d.ts.map +1 -0
  31. package/lib/constants.js +4 -7
  32. package/lib/font-data.d.ts +23 -0
  33. package/lib/font-data.d.ts.map +1 -0
  34. package/lib/font-data.js +47 -0
  35. package/lib/font-data.test.d.ts +2 -0
  36. package/lib/font-data.test.d.ts.map +1 -0
  37. package/lib/font-data.test.js +65 -0
  38. package/lib/font-weights.d.ts +50 -0
  39. package/lib/font-weights.d.ts.map +1 -0
  40. package/lib/font-weights.js +47 -0
  41. package/lib/get-font-faces.d.ts +8 -1
  42. package/lib/get-font-faces.d.ts.map +1 -1
  43. package/lib/get-font-faces.js +20 -15
  44. package/lib/get-font-faces.test.js +49 -5
  45. package/lib/index.d.ts +1 -0
  46. package/lib/index.d.ts.map +1 -1
  47. package/lib/index.js +4 -19
  48. package/lib/index.server.d.ts +3 -0
  49. package/lib/index.server.d.ts.map +1 -0
  50. package/lib/index.server.js +2 -0
  51. package/lib/schema.js +6 -9
  52. package/lib/tsconfig.tsbuildinfo +1 -0
  53. package/lib/types.js +1 -2
  54. package/package.json +31 -9
  55. package/lib/get-font-data.d.ts +0 -40
  56. package/lib/get-font-data.d.ts.map +0 -1
  57. package/lib/get-font-data.js +0 -57
  58. package/lib/get-font-data.test.d.ts +0 -2
  59. package/lib/get-font-data.test.d.ts.map +0 -1
  60. package/lib/get-font-data.test.js +0 -53
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FONT_MIME_TYPES = exports.FONT_FORMATS = exports.DEFAULT_FONT_FALLBACK = exports.SYSTEM_FONTS = void 0;
4
+ exports.SYSTEM_FONTS = new Map([
5
+ ["Arial", ["sans-serif"]],
6
+ ["Times New Roman", ["sans"]],
7
+ ["Courier New", ["monospace"]],
8
+ ["system-ui", []],
9
+ ]);
10
+ exports.DEFAULT_FONT_FALLBACK = "sans-serif";
11
+ exports.FONT_FORMATS = new Map([
12
+ ["woff", "woff"],
13
+ ["woff2", "woff2"],
14
+ ["ttf", "truetype"],
15
+ ["otf", "opentype"],
16
+ ]);
17
+ exports.FONT_MIME_TYPES = Array.from(exports.FONT_FORMATS.keys())
18
+ .map((format) => `.${format}`)
19
+ .join(", ");
@@ -0,0 +1,6 @@
1
+ import type { FontFormat } from "./types";
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;
6
+ //# sourceMappingURL=constants.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../src/constants.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAE1C,eAAO,MAAM,YAAY,uBAKvB,CAAC;AAEH,eAAO,MAAM,qBAAqB,eAAe,CAAC;AAElD,eAAO,MAAM,YAAY,EAAE,GAAG,CAAC,UAAU,EAAE,MAAM,CAK/C,CAAC;AAEH,eAAO,MAAM,eAAe,QAEf,CAAC"}
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getFontData = exports.normalizeFamily = exports.parseSubfamily = exports.styles = void 0;
4
+ const fontkit_1 = require("fontkit");
5
+ const font_weights_1 = require("./font-weights");
6
+ exports.styles = ["normal", "italic", "oblique"];
7
+ const parseSubfamily = (subfamily) => {
8
+ const subfamilyLow = subfamily.toLowerCase();
9
+ let style = "normal";
10
+ for (const possibleStyle of exports.styles) {
11
+ if (subfamilyLow.includes(possibleStyle)) {
12
+ style = possibleStyle;
13
+ break;
14
+ }
15
+ }
16
+ let weight = "400";
17
+ for (weight in font_weights_1.fontWeights) {
18
+ const { name } = font_weights_1.fontWeights[weight];
19
+ const { alt } = font_weights_1.fontWeights[weight];
20
+ if (subfamilyLow.includes(name) || subfamilyLow.includes(alt)) {
21
+ break;
22
+ }
23
+ }
24
+ return { style, weight: Number(weight) };
25
+ };
26
+ exports.parseSubfamily = parseSubfamily;
27
+ const splitAndTrim = (string) => string
28
+ .split(" ")
29
+ .map((part) => part.trim())
30
+ .filter(Boolean);
31
+ // 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.
32
+ // 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.
33
+ const normalizeFamily = (family, subfamily) => {
34
+ const familyParts = splitAndTrim(family);
35
+ const subfamilyParts = splitAndTrim(subfamily.toLowerCase());
36
+ const familyPartsNormalized = familyParts.filter((familyPart) => subfamilyParts.includes(familyPart.toLowerCase()) === false);
37
+ return familyPartsNormalized.join(" ");
38
+ };
39
+ exports.normalizeFamily = normalizeFamily;
40
+ const getFontData = (data) => {
41
+ const font = (0, fontkit_1.create)(data);
42
+ const format = font.type.toLowerCase();
43
+ const originalFamily = font.getName("fontFamily");
44
+ const subfamily = font.getName("preferredSubfamily") ?? font.getName("fontSubfamily");
45
+ const parsedSubfamily = (0, exports.parseSubfamily)(subfamily);
46
+ const family = (0, exports.normalizeFamily)(originalFamily, subfamily);
47
+ return {
48
+ format,
49
+ family,
50
+ ...parsedSubfamily,
51
+ };
52
+ };
53
+ exports.getFontData = getFontData;
@@ -0,0 +1,23 @@
1
+ import type { FontFormat } from "./types";
2
+ declare module "fontkit" {
3
+ interface Font {
4
+ type: string;
5
+ getName: (name: string) => string;
6
+ }
7
+ }
8
+ export declare const styles: readonly ["normal", "italic", "oblique"];
9
+ declare type Style = typeof styles[number];
10
+ export declare const parseSubfamily: (subfamily: string) => {
11
+ style: "normal" | "italic" | "oblique";
12
+ weight: number;
13
+ };
14
+ export declare const normalizeFamily: (family: string, subfamily: string) => string;
15
+ declare type FontData = {
16
+ format: FontFormat;
17
+ family: string;
18
+ style: Style;
19
+ weight: number;
20
+ };
21
+ export declare const getFontData: (data: Uint8Array) => FontData;
22
+ export {};
23
+ //# sourceMappingURL=font-data.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"font-data.d.ts","sourceRoot":"","sources":["../../src/font-data.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAK1C,OAAO,QAAQ,SAAS,CAAC;IACvB,UAAiB,IAAI;QACnB,IAAI,EAAE,MAAM,CAAC;QACb,OAAO,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,MAAM,CAAC;KACnC;CACF;AAED,eAAO,MAAM,MAAM,0CAA2C,CAAC;AAC/D,aAAK,KAAK,GAAG,OAAO,MAAM,CAAC,MAAM,CAAC,CAAC;AAEnC,eAAO,MAAM,cAAc,cAAe,MAAM;;;CAmB/C,CAAC;AAUF,eAAO,MAAM,eAAe,WAAY,MAAM,aAAa,MAAM,WAOhE,CAAC;AAEF,aAAK,QAAQ,GAAG;IACd,MAAM,EAAE,UAAU,CAAC;IACnB,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,EAAE,KAAK,CAAC;IACb,MAAM,EAAE,MAAM,CAAC;CAChB,CAAC;AAEF,eAAO,MAAM,WAAW,SAAU,UAAU,KAAG,QAa9C,CAAC"}
@@ -0,0 +1,67 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const font_data_1 = require("./font-data");
4
+ describe("font-data", () => {
5
+ describe("parseSubfamily()", () => {
6
+ test("Black Italic", () => {
7
+ expect((0, font_data_1.parseSubfamily)("Black Italic")).toEqual({
8
+ style: "italic",
9
+ weight: 900,
10
+ });
11
+ });
12
+ test("Bold", () => {
13
+ expect((0, font_data_1.parseSubfamily)("Bold")).toEqual({
14
+ style: "normal",
15
+ weight: 700,
16
+ });
17
+ });
18
+ test("Demi Bold Italic", () => {
19
+ expect((0, font_data_1.parseSubfamily)("Demi Bold Italic")).toEqual({
20
+ style: "italic",
21
+ weight: 600,
22
+ });
23
+ });
24
+ test("Light", () => {
25
+ expect((0, font_data_1.parseSubfamily)("Light")).toEqual({
26
+ style: "normal",
27
+ weight: 300,
28
+ });
29
+ });
30
+ test("Extra Light", () => {
31
+ expect((0, font_data_1.parseSubfamily)("Extra Light")).toEqual({
32
+ style: "normal",
33
+ weight: 200,
34
+ });
35
+ });
36
+ test("Extra Light Italic", () => {
37
+ expect((0, font_data_1.parseSubfamily)("Extra Light Italic")).toEqual({
38
+ style: "italic",
39
+ weight: 200,
40
+ });
41
+ });
42
+ test("Heavy Italic", () => {
43
+ expect((0, font_data_1.parseSubfamily)("Heavy Italic")).toEqual({
44
+ style: "italic",
45
+ weight: 900,
46
+ });
47
+ });
48
+ test("Medium Italic", () => {
49
+ expect((0, font_data_1.parseSubfamily)("Medium Italic")).toEqual({
50
+ style: "italic",
51
+ weight: 500,
52
+ });
53
+ });
54
+ });
55
+ describe("normalizeFamily()", () => {
56
+ test("basic", () => {
57
+ expect((0, font_data_1.normalizeFamily)("Roboto Black", "Black")).toBe("Roboto");
58
+ expect((0, font_data_1.normalizeFamily)("Roboto Light", "Light Italic")).toBe("Roboto");
59
+ expect((0, font_data_1.normalizeFamily)("Robolder Bold", "Bold")).toBe("Robolder");
60
+ expect((0, font_data_1.normalizeFamily)(" Roboto X Bold ", "Bold")).toBe("Roboto X");
61
+ expect((0, font_data_1.normalizeFamily)(" 'Roboto X' Bold ", "Bold")).toBe("'Roboto X'");
62
+ expect((0, font_data_1.normalizeFamily)(` "Roboto X" Bold `, "Bold")).toBe(`"Roboto X"`);
63
+ expect((0, font_data_1.normalizeFamily)(`"Roboto Bold"`, "Bold")).toBe(`"Roboto Bold"`);
64
+ expect((0, font_data_1.normalizeFamily)(`"Roboto Bold" Bold`, "Bold")).toBe(`"Roboto Bold"`);
65
+ });
66
+ });
67
+ });
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=font-data.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"font-data.test.d.ts","sourceRoot":"","sources":["../../src/font-data.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.fontWeights = void 0;
4
+ exports.fontWeights = {
5
+ "100": {
6
+ label: "Thin",
7
+ name: "thin",
8
+ alt: "hairline",
9
+ },
10
+ "200": {
11
+ label: "Extra Light",
12
+ name: "extra light",
13
+ alt: "ultra light",
14
+ },
15
+ "300": {
16
+ label: "Light",
17
+ name: "light",
18
+ alt: "light",
19
+ },
20
+ "400": {
21
+ label: "Normal",
22
+ name: "normal",
23
+ alt: "normal",
24
+ },
25
+ "500": {
26
+ label: "Medium",
27
+ name: "medium",
28
+ alt: "medium",
29
+ },
30
+ "600": {
31
+ label: "Semi Bold",
32
+ name: "semi bold",
33
+ alt: "demi bold",
34
+ },
35
+ "700": {
36
+ label: "Bold",
37
+ name: "bold",
38
+ alt: "bold",
39
+ },
40
+ "800": {
41
+ label: "Extra Bold",
42
+ name: "extra bold",
43
+ alt: "ultra bold",
44
+ },
45
+ "900": {
46
+ label: "Black",
47
+ name: "black",
48
+ alt: "heavy",
49
+ },
50
+ };
@@ -0,0 +1,50 @@
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 declare type FontWeight = keyof typeof fontWeights;
49
+ export declare type FontWeightKeyword = typeof fontWeights[FontWeight]["name"] | typeof fontWeights[FontWeight]["alt"];
50
+ //# sourceMappingURL=font-weights.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"font-weights.d.ts","sourceRoot":"","sources":["../../src/font-weights.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,WAAW;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA8Cd,CAAC;AAEX,oBAAY,UAAU,GAAG,MAAM,OAAO,WAAW,CAAC;AAClD,oBAAY,iBAAiB,GACzB,OAAO,WAAW,CAAC,UAAU,CAAC,CAAC,MAAM,CAAC,GACtC,OAAO,WAAW,CAAC,UAAU,CAAC,CAAC,KAAK,CAAC,CAAC"}
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getFontFaces = void 0;
4
+ const constants_1 = require("./constants");
5
+ const formatFace = (asset, format) => {
6
+ return {
7
+ fontFamily: asset.meta.family,
8
+ fontStyle: asset.meta.style,
9
+ fontWeight: asset.meta.weight,
10
+ fontDisplay: "swap",
11
+ src: `url('${asset.path}') format('${format}')`,
12
+ };
13
+ };
14
+ const getKey = (asset) => asset.meta.family + asset.meta.style + asset.meta.weight;
15
+ const getFontFaces = (assets) => {
16
+ const faces = new Map();
17
+ for (const asset of assets) {
18
+ const face = faces.get(getKey(asset));
19
+ const format = constants_1.FONT_FORMATS.get(asset.format);
20
+ if (format === undefined) {
21
+ // Should never happen since we allow only uploading formats we support
22
+ continue;
23
+ }
24
+ if (face === undefined) {
25
+ const face = formatFace(asset, format);
26
+ faces.set(getKey(asset), face);
27
+ continue;
28
+ }
29
+ // We already have that font face, so we need to add the new src
30
+ face.src += `, url('${asset.path}') format('${format}')`;
31
+ }
32
+ return Array.from(faces.values());
33
+ };
34
+ exports.getFontFaces = getFontFaces;
@@ -0,0 +1,16 @@
1
+ import type { FontMeta } from "./schema";
2
+ import type { FontFormat } from "./types";
3
+ export declare type PartialFontAsset = {
4
+ format: FontFormat;
5
+ meta: FontMeta;
6
+ path: string;
7
+ };
8
+ export declare type FontFace = {
9
+ fontFamily: string;
10
+ fontStyle: FontMeta["style"];
11
+ fontWeight: number;
12
+ fontDisplay: "swap" | "auto" | "block" | "fallback" | "optional";
13
+ src: string;
14
+ };
15
+ export declare const getFontFaces: (assets: Array<PartialFontAsset>) => Array<FontFace>;
16
+ //# sourceMappingURL=get-font-faces.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"get-font-faces.d.ts","sourceRoot":"","sources":["../../src/get-font-faces.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AACzC,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAE1C,oBAAY,gBAAgB,GAAG;IAC7B,MAAM,EAAE,UAAU,CAAC;IACnB,IAAI,EAAE,QAAQ,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;CACd,CAAC;AAEF,oBAAY,QAAQ,GAAG;IACrB,UAAU,EAAE,MAAM,CAAC;IACnB,SAAS,EAAE,QAAQ,CAAC,OAAO,CAAC,CAAC;IAC7B,UAAU,EAAE,MAAM,CAAC;IACnB,WAAW,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,UAAU,GAAG,UAAU,CAAC;IACjE,GAAG,EAAE,MAAM,CAAC;CACb,CAAC;AAeF,eAAO,MAAM,YAAY,WACf,MAAM,gBAAgB,CAAC,KAC9B,MAAM,QAAQ,CAoBhB,CAAC"}
@@ -0,0 +1,74 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const get_font_faces_1 = require("./get-font-faces");
4
+ describe("getFontFaces()", () => {
5
+ test("different formats", () => {
6
+ const assets = [
7
+ {
8
+ format: "woff",
9
+ meta: {
10
+ family: "Roboto",
11
+ style: "normal",
12
+ weight: 400,
13
+ },
14
+ path: "/fonts/roboto.woff",
15
+ },
16
+ {
17
+ format: "ttf",
18
+ meta: {
19
+ family: "Roboto",
20
+ style: "normal",
21
+ weight: 400,
22
+ },
23
+ path: "/fonts/roboto.ttf",
24
+ },
25
+ ];
26
+ expect((0, get_font_faces_1.getFontFaces)(assets)).toMatchSnapshot();
27
+ });
28
+ test("different style", () => {
29
+ const assets = [
30
+ {
31
+ format: "ttf",
32
+ meta: {
33
+ family: "Roboto",
34
+ style: "normal",
35
+ weight: 400,
36
+ },
37
+ path: "/fonts/roboto.ttf",
38
+ },
39
+ {
40
+ format: "ttf",
41
+ meta: {
42
+ family: "Roboto",
43
+ style: "italic",
44
+ weight: 400,
45
+ },
46
+ path: "/fonts/roboto-italic.ttf",
47
+ },
48
+ ];
49
+ expect((0, get_font_faces_1.getFontFaces)(assets)).toMatchSnapshot();
50
+ });
51
+ test("different weight", () => {
52
+ const assets = [
53
+ {
54
+ format: "ttf",
55
+ meta: {
56
+ family: "Roboto",
57
+ style: "normal",
58
+ weight: 400,
59
+ },
60
+ path: "/fonts/roboto.ttf",
61
+ },
62
+ {
63
+ format: "ttf",
64
+ meta: {
65
+ family: "Roboto",
66
+ style: "normal",
67
+ weight: 500,
68
+ },
69
+ path: "/fonts/roboto-bold.ttf",
70
+ },
71
+ ];
72
+ expect((0, get_font_faces_1.getFontFaces)(assets)).toMatchSnapshot();
73
+ });
74
+ });
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=get-font-faces.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"get-font-faces.test.d.ts","sourceRoot":"","sources":["../../src/get-font-faces.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./constants"), exports);
18
+ __exportStar(require("./get-font-faces"), exports);
19
+ __exportStar(require("./types"), exports);
20
+ __exportStar(require("./font-weights"), exports);
@@ -0,0 +1,5 @@
1
+ export * from "./constants";
2
+ export * from "./get-font-faces";
3
+ export * from "./types";
4
+ export * from "./font-weights";
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,kBAAkB,CAAC;AACjC,cAAc,SAAS,CAAC;AACxB,cAAc,gBAAgB,CAAC"}
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.getFontData = void 0;
18
+ var font_data_1 = require("./font-data");
19
+ Object.defineProperty(exports, "getFontData", { enumerable: true, get: function () { return font_data_1.getFontData; } });
20
+ __exportStar(require("./schema"), exports);
@@ -0,0 +1,3 @@
1
+ export { getFontData } from "./font-data";
2
+ export * from "./schema";
3
+ //# sourceMappingURL=index.server.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.server.d.ts","sourceRoot":"","sources":["../../src/index.server.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC1C,cAAc,UAAU,CAAC"}
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FontMeta = void 0;
4
+ const zod_1 = require("zod");
5
+ const font_data_1 = require("./font-data");
6
+ exports.FontMeta = zod_1.z.object({
7
+ family: zod_1.z.string(),
8
+ style: zod_1.z.enum(font_data_1.styles),
9
+ weight: zod_1.z.number(),
10
+ });
@@ -0,0 +1,16 @@
1
+ import { z } from "zod";
2
+ export declare const FontMeta: z.ZodObject<{
3
+ family: z.ZodString;
4
+ style: z.ZodEnum<["normal", "italic", "oblique"]>;
5
+ weight: z.ZodNumber;
6
+ }, "strip", z.ZodTypeAny, {
7
+ family: string;
8
+ style: "normal" | "italic" | "oblique";
9
+ weight: number;
10
+ }, {
11
+ family: string;
12
+ style: "normal" | "italic" | "oblique";
13
+ weight: number;
14
+ }>;
15
+ export declare type FontMeta = z.infer<typeof FontMeta>;
16
+ //# sourceMappingURL=schema.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../../src/schema.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAGxB,eAAO,MAAM,QAAQ;;;;;;;;;;;;EAInB,CAAC;AACH,oBAAY,QAAQ,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,QAAQ,CAAC,CAAC"}
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ export declare type FontFormat = "ttf" | "woff" | "woff2" | "otf";
2
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAAA,oBAAY,UAAU,GAAG,KAAK,GAAG,MAAM,GAAG,OAAO,GAAG,KAAK,CAAC"}
package/lib/constants.js CHANGED
@@ -1,19 +1,16 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.FONT_MIME_TYPES = exports.FONT_FORMATS = exports.DEFAULT_FONT_FALLBACK = exports.SYSTEM_FONTS = void 0;
4
- exports.SYSTEM_FONTS = new Map([
1
+ export const SYSTEM_FONTS = new Map([
5
2
  ["Arial", ["sans-serif"]],
6
3
  ["Times New Roman", ["sans"]],
7
4
  ["Courier New", ["monospace"]],
8
5
  ["system-ui", []],
9
6
  ]);
10
- exports.DEFAULT_FONT_FALLBACK = "sans-serif";
11
- exports.FONT_FORMATS = new Map([
7
+ export const DEFAULT_FONT_FALLBACK = "sans-serif";
8
+ export const FONT_FORMATS = new Map([
12
9
  ["woff", "woff"],
13
10
  ["woff2", "woff2"],
14
11
  ["ttf", "truetype"],
15
12
  ["otf", "opentype"],
16
13
  ]);
17
- exports.FONT_MIME_TYPES = Array.from(exports.FONT_FORMATS.keys())
14
+ export const FONT_MIME_TYPES = Array.from(FONT_FORMATS.keys())
18
15
  .map((format) => `.${format}`)
19
16
  .join(", ");
@@ -0,0 +1,23 @@
1
+ import type { FontFormat } from "./types";
2
+ declare module "fontkit" {
3
+ interface Font {
4
+ type: string;
5
+ getName: (name: string) => string;
6
+ }
7
+ }
8
+ export declare const styles: readonly ["normal", "italic", "oblique"];
9
+ declare type Style = typeof styles[number];
10
+ export declare const parseSubfamily: (subfamily: string) => {
11
+ style: "normal" | "italic" | "oblique";
12
+ weight: number;
13
+ };
14
+ export declare const normalizeFamily: (family: string, subfamily: string) => string;
15
+ declare type FontData = {
16
+ format: FontFormat;
17
+ family: string;
18
+ style: Style;
19
+ weight: number;
20
+ };
21
+ export declare const getFontData: (data: Uint8Array) => FontData;
22
+ export {};
23
+ //# sourceMappingURL=font-data.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"font-data.d.ts","sourceRoot":"","sources":["../src/font-data.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAK1C,OAAO,QAAQ,SAAS,CAAC;IACvB,UAAiB,IAAI;QACnB,IAAI,EAAE,MAAM,CAAC;QACb,OAAO,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,MAAM,CAAC;KACnC;CACF;AAED,eAAO,MAAM,MAAM,0CAA2C,CAAC;AAC/D,aAAK,KAAK,GAAG,OAAO,MAAM,CAAC,MAAM,CAAC,CAAC;AAEnC,eAAO,MAAM,cAAc,cAAe,MAAM;;;CAmB/C,CAAC;AAUF,eAAO,MAAM,eAAe,WAAY,MAAM,aAAa,MAAM,WAOhE,CAAC;AAEF,aAAK,QAAQ,GAAG;IACd,MAAM,EAAE,UAAU,CAAC;IACnB,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,EAAE,KAAK,CAAC;IACb,MAAM,EAAE,MAAM,CAAC;CAChB,CAAC;AAEF,eAAO,MAAM,WAAW,SAAU,UAAU,KAAG,QAa9C,CAAC"}
@@ -0,0 +1,47 @@
1
+ import { create as createFontKit } from "fontkit";
2
+ import { fontWeights } from "./font-weights";
3
+ export const styles = ["normal", "italic", "oblique"];
4
+ export const parseSubfamily = (subfamily) => {
5
+ const subfamilyLow = subfamily.toLowerCase();
6
+ let style = "normal";
7
+ for (const possibleStyle of styles) {
8
+ if (subfamilyLow.includes(possibleStyle)) {
9
+ style = possibleStyle;
10
+ break;
11
+ }
12
+ }
13
+ let weight = "400";
14
+ for (weight in fontWeights) {
15
+ const { name } = fontWeights[weight];
16
+ const { alt } = fontWeights[weight];
17
+ if (subfamilyLow.includes(name) || subfamilyLow.includes(alt)) {
18
+ break;
19
+ }
20
+ }
21
+ return { style, weight: Number(weight) };
22
+ };
23
+ const splitAndTrim = (string) => string
24
+ .split(" ")
25
+ .map((part) => part.trim())
26
+ .filter(Boolean);
27
+ // 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.
28
+ // 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.
29
+ export const normalizeFamily = (family, subfamily) => {
30
+ const familyParts = splitAndTrim(family);
31
+ const subfamilyParts = splitAndTrim(subfamily.toLowerCase());
32
+ const familyPartsNormalized = familyParts.filter((familyPart) => subfamilyParts.includes(familyPart.toLowerCase()) === false);
33
+ return familyPartsNormalized.join(" ");
34
+ };
35
+ export const getFontData = (data) => {
36
+ const font = createFontKit(data);
37
+ const format = font.type.toLowerCase();
38
+ const originalFamily = font.getName("fontFamily");
39
+ const subfamily = font.getName("preferredSubfamily") ?? font.getName("fontSubfamily");
40
+ const parsedSubfamily = parseSubfamily(subfamily);
41
+ const family = normalizeFamily(originalFamily, subfamily);
42
+ return {
43
+ format,
44
+ family,
45
+ ...parsedSubfamily,
46
+ };
47
+ };
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=font-data.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"font-data.test.d.ts","sourceRoot":"","sources":["../src/font-data.test.ts"],"names":[],"mappings":""}