@webstudio-is/fonts 0.1.0 → 0.1.1

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.
@@ -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,56 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.getFontData = exports.normalizeFamily = exports.parseSubfamily = exports.styles = void 0;
7
+ const fontkit_1 = __importDefault(require("fontkit"));
8
+ const font_weights_1 = require("./font-weights");
9
+ exports.styles = ["normal", "italic", "oblique"];
10
+ const parseSubfamily = (subfamily) => {
11
+ const subfamilyLow = subfamily.toLowerCase();
12
+ let style = "normal";
13
+ for (const possibleStyle of exports.styles) {
14
+ if (subfamilyLow.includes(possibleStyle)) {
15
+ style = possibleStyle;
16
+ break;
17
+ }
18
+ }
19
+ let weight = "400";
20
+ for (weight in font_weights_1.fontWeights) {
21
+ const { name } = font_weights_1.fontWeights[weight];
22
+ const { alt } = font_weights_1.fontWeights[weight];
23
+ if (subfamilyLow.includes(name) || subfamilyLow.includes(alt)) {
24
+ break;
25
+ }
26
+ }
27
+ return { style, weight: Number(weight) };
28
+ };
29
+ exports.parseSubfamily = parseSubfamily;
30
+ const splitAndTrim = (string) => string
31
+ .split(" ")
32
+ .map((part) => part.trim())
33
+ .filter(Boolean);
34
+ // 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.
35
+ // 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.
36
+ const normalizeFamily = (family, subfamily) => {
37
+ const familyParts = splitAndTrim(family);
38
+ const subfamilyParts = splitAndTrim(subfamily.toLowerCase());
39
+ const familyPartsNormalized = familyParts.filter((familyPart) => subfamilyParts.includes(familyPart.toLowerCase()) === false);
40
+ return familyPartsNormalized.join(" ");
41
+ };
42
+ exports.normalizeFamily = normalizeFamily;
43
+ const getFontData = (data) => {
44
+ const font = fontkit_1.default.create(data);
45
+ const format = font.type.toLowerCase();
46
+ const originalFamily = font.getName("fontFamily");
47
+ const subfamily = font.getName("preferredSubfamily") ?? font.getName("fontSubfamily");
48
+ const parsedSubfamily = (0, exports.parseSubfamily)(subfamily);
49
+ const family = (0, exports.normalizeFamily)(originalFamily, subfamily);
50
+ return {
51
+ format,
52
+ family,
53
+ ...parsedSubfamily,
54
+ };
55
+ };
56
+ exports.getFontData = getFontData;
@@ -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,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,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,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
+ };
@@ -5,5 +5,12 @@ export declare type PartialFontAsset = {
5
5
  meta: FontMeta;
6
6
  path: string;
7
7
  };
8
- export declare const getFontFaces: (assets: Array<PartialFontAsset>) => any[];
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>;
9
16
  //# sourceMappingURL=get-font-faces.d.ts.map
@@ -1 +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,eAAO,MAAM,YAAY,WAAY,MAAM,gBAAgB,CAAC,UAqB3D,CAAC"}
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"}
@@ -2,19 +2,28 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getFontFaces = void 0;
4
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;
5
15
  const getFontFaces = (assets) => {
6
16
  const faces = new Map();
7
17
  for (const asset of assets) {
8
- const face = faces.get(asset.meta.family);
18
+ const face = faces.get(getKey(asset));
9
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
+ }
10
24
  if (face === undefined) {
11
- faces.set(asset.meta.family, {
12
- fontFamily: asset.meta.family,
13
- fontStyle: asset.meta.style,
14
- fontWeight: asset.meta.weight,
15
- fontDisplay: "swap",
16
- src: `url('${asset.path}') format('${format}')`,
17
- });
25
+ const face = formatFace(asset, format);
26
+ faces.set(getKey(asset), face);
18
27
  continue;
19
28
  }
20
29
  // We already have that font face, so we need to add the new src
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const get_font_faces_1 = require("./get-font-faces");
4
4
  describe("getFontFaces()", () => {
5
- test("same family, but different formats", () => {
5
+ test("different formats", () => {
6
6
  const assets = [
7
7
  {
8
8
  format: "woff",
@@ -25,4 +25,50 @@ describe("getFontFaces()", () => {
25
25
  ];
26
26
  expect((0, get_font_faces_1.getFontFaces)(assets)).toMatchSnapshot();
27
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
+ });
28
74
  });
package/lib/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from "./constants";
2
2
  export * from "./get-font-faces";
3
3
  export * from "./types";
4
+ export * from "./font-weights";
4
5
  //# sourceMappingURL=index.d.ts.map
@@ -1 +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"}
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"}
package/lib/index.js CHANGED
@@ -17,3 +17,4 @@ Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./constants"), exports);
18
18
  __exportStar(require("./get-font-faces"), exports);
19
19
  __exportStar(require("./types"), exports);
20
+ __exportStar(require("./font-weights"), 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,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);
package/lib/schema.js CHANGED
@@ -2,9 +2,9 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.FontMeta = void 0;
4
4
  const zod_1 = require("zod");
5
- const get_font_data_1 = require("./get-font-data");
5
+ const font_data_1 = require("./font-data");
6
6
  exports.FontMeta = zod_1.z.object({
7
7
  family: zod_1.z.string(),
8
- style: zod_1.z.enum(get_font_data_1.styles),
8
+ style: zod_1.z.enum(font_data_1.styles),
9
9
  weight: zod_1.z.number(),
10
10
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webstudio-is/fonts",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "Fonts utils",
5
5
  "author": "Webstudio <github@webstudio.is>",
6
6
  "homepage": "https://webstudio.is",
@@ -8,12 +8,11 @@
8
8
  "typecheck": "tsc --noEmit",
9
9
  "test": "jest",
10
10
  "checks": "yarn typecheck && yarn lint && yarn test",
11
- "dev": "tsup src/index.ts --format esm,cjs --watch --out-dir=lib",
11
+ "dev": "tsup --watch",
12
12
  "build": "rm -fr lib tsconfig.tsbuildinfo && tsc",
13
13
  "lint": "eslint ./src --ext .ts,.tsx --max-warnings 0"
14
14
  },
15
15
  "dependencies": {
16
- "zod": "^3.19.1",
17
16
  "fontkit": "^2.0.2"
18
17
  },
19
18
  "devDependencies": {
@@ -21,7 +20,11 @@
21
20
  "@webstudio-is/jest-config": "*",
22
21
  "tsup": "^6.1.3",
23
22
  "typescript": "4.7.4",
24
- "@types/fontkit": "^1.8.0"
23
+ "@types/fontkit": "^1.8.0",
24
+ "zod": "^3.19.1"
25
+ },
26
+ "peerDependencies": {
27
+ "zod": "^3.19.1"
25
28
  },
26
29
  "main": "lib/index.js",
27
30
  "types": "lib/index.d.ts",
@@ -31,5 +34,14 @@
31
34
  "!*.test.*"
32
35
  ],
33
36
  "license": "MIT",
34
- "private": false
37
+ "private": false,
38
+ "sideEffects": false,
39
+ "tsup": {
40
+ "entry": [
41
+ "src/index.ts",
42
+ "src/index.server.ts"
43
+ ],
44
+ "format": "cjs",
45
+ "outDir": "lib"
46
+ }
35
47
  }
@@ -1,40 +0,0 @@
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
- declare const weights: {
11
- readonly thin: 100;
12
- readonly hairline: 100;
13
- readonly "extra light": 200;
14
- readonly "ultra light": 200;
15
- readonly light: 300;
16
- readonly normal: 400;
17
- readonly medium: 500;
18
- readonly "semi bold": 600;
19
- readonly "demi bold": 600;
20
- readonly bold: 700;
21
- readonly "extra bold": 800;
22
- readonly "ultra bold": 800;
23
- readonly black: 900;
24
- readonly heavy: 900;
25
- };
26
- declare type WeightKey = keyof typeof weights;
27
- declare type WeightValue = typeof weights[WeightKey];
28
- export declare const parseSubfamily: (subfamily: string) => {
29
- style: "normal" | "italic" | "oblique";
30
- weight: WeightValue;
31
- };
32
- declare type FontData = {
33
- format: FontFormat;
34
- family: string;
35
- style: Style;
36
- weight: WeightValue;
37
- };
38
- export declare const getFontData: (data: Uint8Array) => FontData;
39
- export {};
40
- //# sourceMappingURL=get-font-data.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"get-font-data.d.ts","sourceRoot":"","sources":["../src/get-font-data.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAI1C,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,QAAA,MAAM,OAAO;;;;;;;;;;;;;;;CAeH,CAAC;AAEX,aAAK,SAAS,GAAG,MAAM,OAAO,OAAO,CAAC;AACtC,aAAK,WAAW,GAAG,OAAO,OAAO,CAAC,SAAS,CAAC,CAAC;AAE7C,eAAO,MAAM,cAAc,cAAe,MAAM;;;CAkB/C,CAAC;AAEF,aAAK,QAAQ,GAAG;IACd,MAAM,EAAE,UAAU,CAAC;IACnB,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,EAAE,KAAK,CAAC;IACb,MAAM,EAAE,WAAW,CAAC;CACrB,CAAC;AAEF,eAAO,MAAM,WAAW,SAAU,UAAU,KAAG,QAa9C,CAAC"}
@@ -1,57 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.getFontData = exports.parseSubfamily = exports.styles = void 0;
7
- const fontkit_1 = __importDefault(require("fontkit"));
8
- exports.styles = ["normal", "italic", "oblique"];
9
- const weights = {
10
- thin: 100,
11
- hairline: 100,
12
- "extra light": 200,
13
- "ultra light": 200,
14
- light: 300,
15
- normal: 400,
16
- medium: 500,
17
- "semi bold": 600,
18
- "demi bold": 600,
19
- bold: 700,
20
- "extra bold": 800,
21
- "ultra bold": 800,
22
- black: 900,
23
- heavy: 900,
24
- };
25
- const parseSubfamily = (subfamily) => {
26
- const subfamilyLow = subfamily.toLowerCase();
27
- let style = "normal";
28
- for (const possibleStyle of exports.styles) {
29
- if (subfamilyLow.includes(possibleStyle)) {
30
- style = possibleStyle;
31
- break;
32
- }
33
- }
34
- let weight = weights.normal;
35
- let possibleWeight;
36
- for (possibleWeight in weights) {
37
- if (subfamilyLow.includes(possibleWeight)) {
38
- weight = weights[possibleWeight];
39
- break;
40
- }
41
- }
42
- return { style, weight };
43
- };
44
- exports.parseSubfamily = parseSubfamily;
45
- const getFontData = (data) => {
46
- const font = fontkit_1.default.create(data);
47
- const format = font.type.toLowerCase();
48
- const family = font.getName("fontFamily");
49
- const subfamily = font.getName("preferredSubfamily") ?? font.getName("fontSubfamily");
50
- const parsedSubfamily = (0, exports.parseSubfamily)(subfamily);
51
- return {
52
- format,
53
- family,
54
- ...parsedSubfamily,
55
- };
56
- };
57
- exports.getFontData = getFontData;
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=get-font-data.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"get-font-data.test.d.ts","sourceRoot":"","sources":["../src/get-font-data.test.ts"],"names":[],"mappings":""}
@@ -1,53 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const get_font_data_1 = require("./get-font-data");
4
- describe("parseSubfamily()", () => {
5
- test("Black Italic", () => {
6
- expect((0, get_font_data_1.parseSubfamily)("Black Italic")).toEqual({
7
- style: "italic",
8
- weight: 900,
9
- });
10
- });
11
- test("Bold", () => {
12
- expect((0, get_font_data_1.parseSubfamily)("Bold")).toEqual({
13
- style: "normal",
14
- weight: 700,
15
- });
16
- });
17
- test("Demi Bold Italic", () => {
18
- expect((0, get_font_data_1.parseSubfamily)("Demi Bold Italic")).toEqual({
19
- style: "italic",
20
- weight: 600,
21
- });
22
- });
23
- test("Light", () => {
24
- expect((0, get_font_data_1.parseSubfamily)("Light")).toEqual({
25
- style: "normal",
26
- weight: 300,
27
- });
28
- });
29
- test("Extra Light", () => {
30
- expect((0, get_font_data_1.parseSubfamily)("Extra Light")).toEqual({
31
- style: "normal",
32
- weight: 200,
33
- });
34
- });
35
- test("Extra Light Italic", () => {
36
- expect((0, get_font_data_1.parseSubfamily)("Extra Light Italic")).toEqual({
37
- style: "italic",
38
- weight: 200,
39
- });
40
- });
41
- test("Heavy Italic", () => {
42
- expect((0, get_font_data_1.parseSubfamily)("Heavy Italic")).toEqual({
43
- style: "italic",
44
- weight: 900,
45
- });
46
- });
47
- test("Medium Italic", () => {
48
- expect((0, get_font_data_1.parseSubfamily)("Medium Italic")).toEqual({
49
- style: "italic",
50
- weight: 500,
51
- });
52
- });
53
- });