@liiift-studio/sanity-font-manager 2.3.18 → 2.4.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 (42) hide show
  1. package/README.md +437 -437
  2. package/dist/index.js +103 -48
  3. package/dist/index.mjs +103 -48
  4. package/package.json +85 -85
  5. package/src/components/BatchUploadFonts.jsx +640 -639
  6. package/src/components/FontScriptUploaderComponent.jsx +463 -463
  7. package/src/components/GenerateCollectionsPairsComponent.jsx +259 -259
  8. package/src/components/KeyValueInput.jsx +95 -95
  9. package/src/components/KeyValueReferenceInput.jsx +254 -254
  10. package/src/components/NestedObjectArraySelector.jsx +146 -146
  11. package/src/components/PriceInput.jsx +26 -26
  12. package/src/components/PrimaryCollectionGeneratorTypeface.jsx +116 -116
  13. package/src/components/RegenerateSubfamiliesComponent.jsx +185 -185
  14. package/src/components/SetOTF.jsx +87 -87
  15. package/src/components/SingleUploaderTool.jsx +673 -673
  16. package/src/components/StatusDisplay.jsx +26 -26
  17. package/src/components/StyleCountInput.jsx +16 -16
  18. package/src/components/UpdateScriptsComponent.jsx +76 -76
  19. package/src/components/UploadButton.jsx +43 -43
  20. package/src/components/UploadScriptsComponent.jsx +537 -537
  21. package/src/components/VariableInstanceReferencesInput.jsx +190 -190
  22. package/src/hooks/useNestedObjects.js +92 -92
  23. package/src/hooks/useSanityClient.js +9 -9
  24. package/src/index.js +70 -70
  25. package/src/schema/openTypeField.js +1945 -1945
  26. package/src/schema/styleCountField.js +12 -12
  27. package/src/schema/stylesField.js +268 -268
  28. package/src/schema/stylisticSetField.js +301 -301
  29. package/src/utils/generateCssFile.js +205 -205
  30. package/src/utils/generateFontData.js +145 -145
  31. package/src/utils/generateFontFile.js +38 -38
  32. package/src/utils/generateKeywords.js +185 -185
  33. package/src/utils/generateSubset.js +45 -45
  34. package/src/utils/getEmptyFontKit.js +99 -99
  35. package/src/utils/parseVariableFontInstances.js +211 -211
  36. package/src/utils/processFontFiles.js +487 -477
  37. package/src/utils/regenerateFontData.js +146 -146
  38. package/src/utils/sanitizeForSanityId.js +65 -65
  39. package/src/utils/updateFontPrices.js +94 -94
  40. package/src/utils/updateTypefaceDocument.js +149 -160
  41. package/src/utils/uploadFontFiles.js +115 -26
  42. package/src/utils/utils.js +24 -24
@@ -1,145 +1,145 @@
1
- // Extracts metadata, metrics, glyph count, OpenType features, and variable axes from a TTF and optionally patches the Sanity font document
2
-
3
- import { Buffer } from 'buffer';
4
- import * as fontkit from 'fontkit';
5
-
6
- /**
7
- * Extracts metadata and metrics from a fontkit font object into plain objects.
8
- * @param {Object} font - fontkit font instance
9
- * @returns {{ metaData: Object, metrics: Object }}
10
- */
11
- export function buildFontMetadata(font) {
12
- const metaData = {
13
- postscriptName: font.postscriptName,
14
- fullName: font.fullName,
15
- familyName: font.familyName,
16
- subfamilyName: font.subfamilyName,
17
- copyright: font.copyright,
18
- version: font.version ? String(font.version).replaceAll('Version ', '') : '',
19
- genDate: new Date().toISOString(),
20
- };
21
- const metrics = {
22
- unitsPerEm: font.unitsPerEm,
23
- ascender: font.ascent,
24
- descender: font.descent,
25
- lineGap: font.lineGap,
26
- underlinePosition: font.underlinePosition,
27
- underlineThickness: font.underlineThickness,
28
- italicAngle: font.italicAngle,
29
- capHeight: font.capHeight,
30
- xHeight: font.xHeight,
31
- boundingBox: font.bbox,
32
- };
33
- return { metaData, metrics };
34
- }
35
-
36
- export default async function generateFontData({ fileInput, url, fontKit, fontId, client, commit = true }) {
37
- if (fontId.startsWith('drafts.')) {
38
- fontId = fontId.replace('drafts.', '');
39
- }
40
- console.log('generate-font-data ', fontId, commit);
41
-
42
- let srcUrl;
43
- if (!url || url == null) {
44
- srcUrl = await client.fetch(`*[_id == $id]{url}`, { id: fileInput.ttf.asset._ref });
45
- console.log('src url ', srcUrl);
46
- srcUrl = srcUrl[0].url;
47
- } else {
48
- srcUrl = url;
49
- }
50
-
51
- let font = fontKit;
52
- if (!fontKit || fontKit == null) {
53
- let buffer = await fetch(srcUrl);
54
- buffer = await buffer.arrayBuffer();
55
- buffer = Buffer.from(buffer);
56
- font = fontkit.create(buffer);
57
- }
58
-
59
-
60
- let variableAxes;
61
- try {
62
- variableAxes = font.variationAxes;
63
- } catch (err) {
64
- console.error('err: ', err);
65
- }
66
-
67
- let variableInstances;
68
- try {
69
- variableInstances = font.namedVariations;
70
- } catch (e) {
71
- console.log('variable instances 2 error : ', e.message);
72
- let fvar = font?.fvar?.instance;
73
-
74
- fvar?.forEach(fv => {
75
- if (fv?.nameID === 2) fv.name = font?._tables?.name?.records?.fontSubfamily
76
- if (fv?.nameID === 17) fv.name = font?._tables?.name?.records?.preferredSubfamily
77
- })
78
-
79
- variableInstances = {};
80
- fvar.forEach(v => {
81
- let key = v.name;
82
- if (typeof key === 'object') {
83
- key = Object.values(key)[0];
84
- }
85
-
86
- let coordKeys = Object.keys(variableAxes);
87
- let coord = {};
88
-
89
- coordKeys.forEach((ck, ckIndex) => {
90
- coord[ck] = v.coord[ckIndex];
91
- });
92
- variableInstances[key] = coord;
93
- });
94
-
95
- }
96
- console.log('font : ', font);
97
- console.log('variable instances : ', variableInstances);
98
- console.log('variable axes : ', variableAxes);
99
-
100
- let opentypeFeatures = font.availableFeatures;
101
- let glyphCount = font.numGlyphs;
102
- let characterSet = font.characterSet;
103
-
104
- let metaData = {
105
- postscriptName: font.postscriptName,
106
- fullName: font.fullName,
107
- familyName: font.familyName,
108
- subfamilyName: font.subfamilyName,
109
- copyright: font.copyright,
110
- version: font.version.replaceAll("Version ", ""),
111
- genDate: new Date().toISOString(),
112
- };
113
-
114
- let metrics = {
115
- unitsPerEm: font.unitsPerEm,
116
- ascender: font.ascent,
117
- descender: font.descent,
118
- lineGap: font.lineGap,
119
- underlinePosition: font.underlinePosition,
120
- underlineThickness: font.underlineThickness,
121
- italicAngle: font.italicAngle,
122
- capHeight: font.capHeight,
123
- xHeight: font.xHeight,
124
- boundingBox: font.bbox
125
- };
126
-
127
- let variableFont = false;
128
- if (variableAxes && variableAxes != null && Object.keys(variableAxes).length > 0 && variableInstances && variableInstances != null && Object.keys(variableInstances).length > 0) {
129
- variableFont = true;
130
- }
131
- let patch = {
132
- metrics: metrics,
133
- metaData: metaData,
134
- variableFont: variableFont,
135
- variableAxes: JSON.stringify(variableAxes),
136
- variableInstances: JSON.stringify(variableInstances),
137
- glyphCount: glyphCount,
138
- opentypeFeatures: { chars: opentypeFeatures },
139
- characterSet: { chars: characterSet }
140
- }
141
-
142
- console.log('data : ', patch);
143
- if (commit) patch = await client.patch(fontId).set(patch).commit({ autoGenerateArrayKeys: true });
144
- return patch;
145
- }
1
+ // Extracts metadata, metrics, glyph count, OpenType features, and variable axes from a TTF and optionally patches the Sanity font document
2
+
3
+ import { Buffer } from 'buffer';
4
+ import * as fontkit from 'fontkit';
5
+
6
+ /**
7
+ * Extracts metadata and metrics from a fontkit font object into plain objects.
8
+ * @param {Object} font - fontkit font instance
9
+ * @returns {{ metaData: Object, metrics: Object }}
10
+ */
11
+ export function buildFontMetadata(font) {
12
+ const metaData = {
13
+ postscriptName: font.postscriptName,
14
+ fullName: font.fullName,
15
+ familyName: font.familyName,
16
+ subfamilyName: font.subfamilyName,
17
+ copyright: font.copyright,
18
+ version: font.version ? String(font.version).replaceAll('Version ', '') : '',
19
+ genDate: new Date().toISOString(),
20
+ };
21
+ const metrics = {
22
+ unitsPerEm: font.unitsPerEm,
23
+ ascender: font.ascent,
24
+ descender: font.descent,
25
+ lineGap: font.lineGap,
26
+ underlinePosition: font.underlinePosition,
27
+ underlineThickness: font.underlineThickness,
28
+ italicAngle: font.italicAngle,
29
+ capHeight: font.capHeight,
30
+ xHeight: font.xHeight,
31
+ boundingBox: font.bbox,
32
+ };
33
+ return { metaData, metrics };
34
+ }
35
+
36
+ export default async function generateFontData({ fileInput, url, fontKit, fontId, client, commit = true }) {
37
+ if (fontId.startsWith('drafts.')) {
38
+ fontId = fontId.replace('drafts.', '');
39
+ }
40
+ console.log('generate-font-data ', fontId, commit);
41
+
42
+ let srcUrl;
43
+ if (!url || url == null) {
44
+ srcUrl = await client.fetch(`*[_id == $id]{url}`, { id: fileInput.ttf.asset._ref });
45
+ console.log('src url ', srcUrl);
46
+ srcUrl = srcUrl[0].url;
47
+ } else {
48
+ srcUrl = url;
49
+ }
50
+
51
+ let font = fontKit;
52
+ if (!fontKit || fontKit == null) {
53
+ let buffer = await fetch(srcUrl);
54
+ buffer = await buffer.arrayBuffer();
55
+ buffer = Buffer.from(buffer);
56
+ font = fontkit.create(buffer);
57
+ }
58
+
59
+
60
+ let variableAxes;
61
+ try {
62
+ variableAxes = font.variationAxes;
63
+ } catch (err) {
64
+ console.error('err: ', err);
65
+ }
66
+
67
+ let variableInstances;
68
+ try {
69
+ variableInstances = font.namedVariations;
70
+ } catch (e) {
71
+ console.log('variable instances 2 error : ', e.message);
72
+ let fvar = font?.fvar?.instance;
73
+
74
+ fvar?.forEach(fv => {
75
+ if (fv?.nameID === 2) fv.name = font?._tables?.name?.records?.fontSubfamily
76
+ if (fv?.nameID === 17) fv.name = font?._tables?.name?.records?.preferredSubfamily
77
+ })
78
+
79
+ variableInstances = {};
80
+ fvar.forEach(v => {
81
+ let key = v.name;
82
+ if (typeof key === 'object') {
83
+ key = Object.values(key)[0];
84
+ }
85
+
86
+ let coordKeys = Object.keys(variableAxes);
87
+ let coord = {};
88
+
89
+ coordKeys.forEach((ck, ckIndex) => {
90
+ coord[ck] = v.coord[ckIndex];
91
+ });
92
+ variableInstances[key] = coord;
93
+ });
94
+
95
+ }
96
+ console.log('font : ', font);
97
+ console.log('variable instances : ', variableInstances);
98
+ console.log('variable axes : ', variableAxes);
99
+
100
+ let opentypeFeatures = font.availableFeatures;
101
+ let glyphCount = font.numGlyphs;
102
+ let characterSet = font.characterSet;
103
+
104
+ let metaData = {
105
+ postscriptName: font.postscriptName,
106
+ fullName: font.fullName,
107
+ familyName: font.familyName,
108
+ subfamilyName: font.subfamilyName,
109
+ copyright: font.copyright,
110
+ version: font.version.replaceAll("Version ", ""),
111
+ genDate: new Date().toISOString(),
112
+ };
113
+
114
+ let metrics = {
115
+ unitsPerEm: font.unitsPerEm,
116
+ ascender: font.ascent,
117
+ descender: font.descent,
118
+ lineGap: font.lineGap,
119
+ underlinePosition: font.underlinePosition,
120
+ underlineThickness: font.underlineThickness,
121
+ italicAngle: font.italicAngle,
122
+ capHeight: font.capHeight,
123
+ xHeight: font.xHeight,
124
+ boundingBox: font.bbox
125
+ };
126
+
127
+ let variableFont = false;
128
+ if (variableAxes && variableAxes != null && Object.keys(variableAxes).length > 0 && variableInstances && variableInstances != null && Object.keys(variableInstances).length > 0) {
129
+ variableFont = true;
130
+ }
131
+ let patch = {
132
+ metrics: metrics,
133
+ metaData: metaData,
134
+ variableFont: variableFont,
135
+ variableAxes: JSON.stringify(variableAxes),
136
+ variableInstances: JSON.stringify(variableInstances),
137
+ glyphCount: glyphCount,
138
+ opentypeFeatures: { chars: opentypeFeatures },
139
+ characterSet: { chars: characterSet }
140
+ }
141
+
142
+ console.log('data : ', patch);
143
+ if (commit) patch = await client.patch(fontId).set(patch).commit({ autoGenerateArrayKeys: true });
144
+ return patch;
145
+ }
@@ -1,38 +1,38 @@
1
- // Triggers server-side font format conversion via the consuming site's fontWorker API endpoint
2
-
3
- export default async function generateFontFile({
4
- srcUrl,
5
- language = null,
6
- filename,
7
- codes,
8
- documentId,
9
- documentTitle,
10
- documentVariableFont,
11
- documentStyle,
12
- documentWeight,
13
- fileInput
14
- }) {
15
- await fetch(`${process.env.SANITY_STUDIO_SITE_URL}/api/sanity/fontWorker`, {
16
- method: 'POST',
17
- mode: 'no-cors',
18
- headers: { 'Content-Type': 'application/json' },
19
- body: JSON.stringify({
20
- code: 'generate-fonts',
21
- language: language,
22
- srcUrl: srcUrl,
23
- filename: filename,
24
- documentId: documentId,
25
- documentTitle: documentTitle,
26
- documentVariableFont: documentVariableFont,
27
- documentStyle: documentStyle,
28
- documentWeight: documentWeight,
29
- fileInput: fileInput,
30
- codes: codes
31
- })
32
- }).catch(e => {
33
- console.error(e.message);
34
- return -1;
35
- });
36
- return 1;
37
-
38
- }
1
+ // Triggers server-side font format conversion via the consuming site's fontWorker API endpoint
2
+
3
+ export default async function generateFontFile({
4
+ srcUrl,
5
+ language = null,
6
+ filename,
7
+ codes,
8
+ documentId,
9
+ documentTitle,
10
+ documentVariableFont,
11
+ documentStyle,
12
+ documentWeight,
13
+ fileInput
14
+ }) {
15
+ await fetch(`${process.env.SANITY_STUDIO_SITE_URL}/api/sanity/fontWorker`, {
16
+ method: 'POST',
17
+ mode: 'no-cors',
18
+ headers: { 'Content-Type': 'application/json' },
19
+ body: JSON.stringify({
20
+ code: 'generate-fonts',
21
+ language: language,
22
+ srcUrl: srcUrl,
23
+ filename: filename,
24
+ documentId: documentId,
25
+ documentTitle: documentTitle,
26
+ documentVariableFont: documentVariableFont,
27
+ documentStyle: documentStyle,
28
+ documentWeight: documentWeight,
29
+ fileInput: fileInput,
30
+ codes: codes
31
+ })
32
+ }).catch(e => {
33
+ console.error(e.message);
34
+ return -1;
35
+ });
36
+ return 1;
37
+
38
+ }