@liiift-studio/sanity-font-manager 2.3.19 → 2.5.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 (62) hide show
  1. package/README.md +437 -437
  2. package/dist/UploadModal-6LIX7XOK.js +6 -0
  3. package/dist/UploadModal-NME2W53V.mjs +6 -0
  4. package/dist/chunk-646WCBRR.mjs +7276 -0
  5. package/dist/chunk-FH4QKHOH.js +7276 -0
  6. package/dist/index.js +747 -1675
  7. package/dist/index.mjs +400 -1237
  8. package/package.json +85 -85
  9. package/src/components/BatchUploadFonts.jsx +653 -639
  10. package/src/components/BulkActions.jsx +99 -0
  11. package/src/components/ExistingDocumentResolver.jsx +152 -0
  12. package/src/components/FontReviewCard.jsx +415 -0
  13. package/src/components/FontScriptUploaderComponent.jsx +463 -463
  14. package/src/components/GenerateCollectionsPairsComponent.jsx +259 -259
  15. package/src/components/KeyValueInput.jsx +95 -95
  16. package/src/components/KeyValueReferenceInput.jsx +254 -254
  17. package/src/components/NestedObjectArraySelector.jsx +146 -146
  18. package/src/components/PriceInput.jsx +26 -26
  19. package/src/components/PrimaryCollectionGeneratorTypeface.jsx +116 -116
  20. package/src/components/RegenerateSubfamiliesComponent.jsx +185 -185
  21. package/src/components/SetOTF.jsx +87 -87
  22. package/src/components/SingleUploaderTool.jsx +672 -673
  23. package/src/components/StatusDisplay.jsx +26 -26
  24. package/src/components/StyleCountInput.jsx +16 -16
  25. package/src/components/UpdateScriptsComponent.jsx +76 -76
  26. package/src/components/UploadButton.jsx +43 -43
  27. package/src/components/UploadModal.jsx +268 -0
  28. package/src/components/UploadScriptsComponent.jsx +539 -537
  29. package/src/components/UploadStep1Settings.jsx +272 -0
  30. package/src/components/UploadStep2Review.jsx +472 -0
  31. package/src/components/UploadStep3Execute.jsx +234 -0
  32. package/src/components/UploadSummary.jsx +196 -0
  33. package/src/components/VariableInstanceReferencesInput.jsx +190 -190
  34. package/src/hooks/useNestedObjects.js +92 -92
  35. package/src/hooks/useSanityClient.js +9 -9
  36. package/src/index.js +115 -70
  37. package/src/schema/openTypeField.js +1945 -1945
  38. package/src/schema/styleCountField.js +12 -12
  39. package/src/schema/stylesField.js +268 -268
  40. package/src/schema/stylisticSetField.js +301 -301
  41. package/src/utils/buildUploadPlan.js +325 -0
  42. package/src/utils/executeUploadPlan.js +437 -0
  43. package/src/utils/executionReducer.js +56 -0
  44. package/src/utils/fontHelpers.js +267 -0
  45. package/src/utils/generateCssFile.js +207 -205
  46. package/src/utils/generateFontData.js +98 -145
  47. package/src/utils/generateFontFile.js +38 -38
  48. package/src/utils/generateKeywords.js +185 -185
  49. package/src/utils/generateSubset.js +45 -45
  50. package/src/utils/getEmptyFontKit.js +101 -99
  51. package/src/utils/parseFont.js +55 -0
  52. package/src/utils/parseVariableFontInstances.js +211 -211
  53. package/src/utils/planReducer.js +517 -0
  54. package/src/utils/planTypes.js +183 -0
  55. package/src/utils/processFontFiles.js +529 -477
  56. package/src/utils/regenerateFontData.js +146 -146
  57. package/src/utils/resolveExistingFont.js +87 -0
  58. package/src/utils/sanitizeForSanityId.js +65 -65
  59. package/src/utils/updateFontPrices.js +94 -94
  60. package/src/utils/updateTypefaceDocument.js +149 -160
  61. package/src/utils/uploadFontFiles.js +405 -316
  62. package/src/utils/utils.js +24 -24
@@ -1,477 +1,529 @@
1
- // Reads font files via FileReader, parses with fontkit, and builds the fontsObjects map — exports individual weight/style extraction helpers
2
-
3
- import * as fontkit from 'fontkit';
4
- import { nanoid } from 'nanoid';
5
- import { expandAbbreviations, removeWeightNames, reverseSpellingLookup } from './generateKeywords';
6
- import { sanitizeForSanityId } from './sanitizeForSanityId';
7
-
8
- /**
9
- * Reads a font file and returns its content as a Uint8Array.
10
- * @param {File} file
11
- * @returns {Promise<Uint8Array>}
12
- */
13
- export const readFontFile = (file) => {
14
- return new Promise((resolve, reject) => {
15
- const reader = new FileReader();
16
- reader.onload = (event) => { resolve(new Uint8Array(event.target.result)); };
17
- reader.onerror = (error) => { reject(error); };
18
- reader.readAsArrayBuffer(file);
19
- });
20
- };
21
-
22
- /**
23
- * Processes an array of font files and extracts metadata for each.
24
- * @param {File[]} files
25
- * @param {string} title - Typeface title
26
- * @param {string[]} weightKeywordList
27
- * @param {string[]} italicKeywordList
28
- * @param {Function} setStatus
29
- * @param {boolean} preserveShortenedNames - Skip abbreviation expansion when true
30
- * @param {boolean} preserveFileNames - Preserve original filename capitalization when true
31
- * @returns {Promise<Object>}
32
- */
33
- export const processFontFiles = async (
34
- files,
35
- title,
36
- weightKeywordList,
37
- italicKeywordList,
38
- setStatus,
39
- preserveShortenedNames = false,
40
- preserveFileNames = false,
41
- ) => {
42
- let failedFiles = [];
43
- let subfamilies = {};
44
- let fontsObjects = {};
45
- let newPreferredStyle = { weight: -100, style: 'Italic', _ref: '' };
46
-
47
- for (let i = 0; i < files.length; i++) {
48
- const file = files[i];
49
- const fontBuffer = await readFontFile(file);
50
- const font = fontkit.create(fontBuffer);
51
-
52
- console.log('File name: ', file.name);
53
-
54
- if (file.name.endsWith('.woff2') || file.name.endsWith('.woff')) {
55
- await handleWebfontMetadata(file, font, files);
56
- }
57
-
58
- const { weightName, subfamilyName, fontTitle, style, italicKW, variableFont } = extractFontMetadata(
59
- font,
60
- title,
61
- weightKeywordList,
62
- italicKeywordList,
63
- preserveShortenedNames,
64
- );
65
-
66
- const id = sanitizeForSanityId(fontTitle);
67
-
68
- let originalFilename = null;
69
- if (preserveFileNames) {
70
- originalFilename = file.name.replace(/\.(ttf|otf|woff2?|eot|svg)$/i, '');
71
- }
72
-
73
- logFontInfo(id, fontTitle, font, file.name, subfamilyName, style, weightName, variableFont, italicKW);
74
-
75
- subfamilies[id] = subfamilyName;
76
-
77
- if (fontsObjects[id]) {
78
- fontsObjects[id].files = [...fontsObjects[id].files, file];
79
- if (preserveFileNames && originalFilename) {
80
- fontsObjects[id].originalFilename = originalFilename;
81
- }
82
- } else {
83
- fontsObjects[id] = createFontObject(
84
- id,
85
- fontTitle,
86
- title,
87
- font,
88
- variableFont,
89
- weightName,
90
- subfamilyName,
91
- file,
92
- preserveFileNames ? originalFilename : null,
93
- );
94
- }
95
- }
96
-
97
- fontsObjects = sortFontObjects(fontsObjects);
98
- const uniqueSubfamilies = [...new Set(Object.values(subfamilies))];
99
-
100
- console.log('Subfamilies:', subfamilies);
101
- console.log('Unique subfamilies:', uniqueSubfamilies, uniqueSubfamilies.length);
102
- console.log('Font objects:', fontsObjects);
103
-
104
- return { fontsObjects, subfamilies, uniqueSubfamilies, newPreferredStyle, failedFiles };
105
- };
106
-
107
- /**
108
- * Patches webfont name records from a matching TTF when woff/woff2 metadata is missing.
109
- * @param {File} file
110
- * @param {Object} font
111
- * @param {File[]} files
112
- */
113
- const handleWebfontMetadata = async (file, font, files) => {
114
- if (
115
- !font?.name?.records?.fullName ||
116
- font?.name?.records?.fullName === '' ||
117
- !/^[A-Z0-9]+$/.test(font?.name?.records?.fullName)
118
- ) {
119
- const ttfFile = files.find(f => f.name === file.name.replace('.woff2', '.ttf').replace('.woff', '.ttf'));
120
- if (ttfFile) {
121
- const ttfFileBuffer = await readFontFile(ttfFile);
122
- const ttfFileData = fontkit.create(ttfFileBuffer);
123
- if (ttfFileData) font.name.records = ttfFileData?.name?.records;
124
- }
125
- }
126
- };
127
-
128
- /**
129
- * Extracts and normalises metadata from a fontkit font object.
130
- * @param {Object} font
131
- * @param {string} title
132
- * @param {string[]} weightKeywordList
133
- * @param {string[]} italicKeywordList
134
- * @param {boolean} preserveShortenedNames
135
- * @returns {Object}
136
- */
137
- export const extractFontMetadata = (font, title, weightKeywordList, italicKeywordList, preserveShortenedNames = false) => {
138
- let weightName = extractWeightName(font, italicKeywordList);
139
- if (!preserveShortenedNames) {
140
- weightName = expandAbbreviations(weightName);
141
- }
142
-
143
- if ((weightName === '' || weightName.toLowerCase() === 'roman') && font?.name?.records?.fullName) {
144
- weightName = extractWeightFromFullName(font, title);
145
- if (!preserveShortenedNames) {
146
- weightName = expandAbbreviations(weightName);
147
- }
148
- }
149
-
150
- const variableFont = font?.variationAxes && Object.keys(font.variationAxes).length > 0;
151
-
152
- let subfamilyName = font?.name?.records?.fullName?.en?.replace(title.trim(), '').trim() ||
153
- font.subfamilyName.trim().replace(title.trim(), '').trim();
154
-
155
- if (!preserveShortenedNames) {
156
- subfamilyName = expandAbbreviations(subfamilyName);
157
- }
158
-
159
- let fontTitle = font?.fullName.trim();
160
- let style = (font?.italicAngle !== 0 || font?.fullName.toLowerCase().includes('italic')) ? 'Italic' : 'Regular';
161
-
162
- const italicKW = processItalicKeywords(font, fontTitle, italicKeywordList);
163
-
164
- subfamilyName = processSubfamilyName(subfamilyName, weightKeywordList, italicKW, preserveShortenedNames);
165
- fontTitle = formatFontTitle(fontTitle, preserveShortenedNames);
166
-
167
- subfamilyName = subfamilyName === '' ? 'Regular' : subfamilyName.replace(/\s+/g, ' ').trim();
168
-
169
- if (subfamilyName !== '') {
170
- weightName = weightName
171
- .replace(`${subfamilyName} `, '')
172
- .replace(` ${subfamilyName}`, '')
173
- .trim();
174
- }
175
-
176
- if (variableFont) {
177
- if (!fontTitle.toLowerCase().includes('vf')) {
178
- fontTitle = fontTitle + ' VF';
179
- }
180
- subfamilyName = '';
181
- }
182
-
183
- if (!(variableFont && fontTitle.toLowerCase().includes('italic'))) {
184
- fontTitle = addItalicToFontTitle(font, fontTitle, italicKW, style, preserveShortenedNames);
185
- }
186
-
187
- return { weightName, subfamilyName, fontTitle, style, italicKW, variableFont };
188
- };
189
-
190
- /**
191
- * Extracts the weight name from a font's preferred subfamily or subfamily record.
192
- * Returns "Variable" for variable fonts.
193
- * @param {Object} font
194
- * @param {string[]} italicKW
195
- * @returns {string}
196
- */
197
- export const extractWeightName = (font, italicKW) => {
198
- let weightName = font?.name?.records?.preferredSubfamily || font?.name?.records?.fontSubfamily;
199
-
200
- if (typeof weightName === 'object') {
201
- weightName = weightName?.en ||
202
- (weightName.constructor === Object ? weightName[Object.keys(weightName)[0]] : null);
203
- }
204
-
205
- if (font?.variationAxes && Object.keys(font.variationAxes).length > 0) {
206
- return 'Variable';
207
- }
208
-
209
- if (italicKW) {
210
- italicKW.forEach(keyword => {
211
- const kwRegex = new RegExp(`\\b${keyword.trim()}\\b`, 'i');
212
- if (kwRegex.test(weightName)) {
213
- weightName = weightName.replace(kwRegex, '').trim();
214
- }
215
- });
216
- }
217
-
218
- return weightName?.toString()
219
- .replace('Italic', '')
220
- .replace('It', '')
221
- .replace('Slanted', '')
222
- .replace('Slant', '')
223
- .replace('Backslant', '')
224
- .trim();
225
- };
226
-
227
- /**
228
- * Extracts a weight name from the font's full name record when subfamily is empty or "Roman".
229
- * @param {Object} font
230
- * @param {string} title
231
- * @returns {string}
232
- */
233
- export const extractWeightFromFullName = (font, title) => {
234
- let weightName = font?.name?.records?.fullName;
235
- weightName = weightName?.en
236
- ? weightName.en
237
- : (weightName?.constructor === Object ? weightName[Object.keys(weightName)[0]] : weightName);
238
- weightName = weightName?.replace(title + ' ', '').replace(title, '').trim();
239
- weightName = weightName?.replace('Italic', '').replace('It', '').replace('Slanted', '').replace('Slant', '').trim();
240
- return weightName;
241
- };
242
-
243
- /**
244
- * Strips weight and italic keywords from a subfamily name string.
245
- * @param {string} subfamilyName
246
- * @param {string[]} weightKeywordList
247
- * @param {string[]} italicKeywordList
248
- * @param {boolean} preserveShortenedNames
249
- * @returns {string}
250
- */
251
- export const processSubfamilyName = (subfamilyName, weightKeywordList, italicKeywordList, preserveShortenedNames = false) => {
252
- weightKeywordList.forEach(keyword => {
253
- const kwRegex = new RegExp(`\\b${keyword.trim()}\\b`, 'i');
254
- if (kwRegex.test(subfamilyName)) {
255
- subfamilyName = subfamilyName.replace(kwRegex, '').trim();
256
- }
257
- subfamilyName = removeWeightNames(subfamilyName) || subfamilyName;
258
- if (!preserveShortenedNames) {
259
- subfamilyName = expandAbbreviations(subfamilyName);
260
- }
261
- });
262
-
263
- italicKeywordList.forEach(keyword => {
264
- const kwRegex = new RegExp(`\\b${keyword.trim()}\\b`, 'i');
265
- if (kwRegex.test(subfamilyName)) {
266
- subfamilyName = subfamilyName.replace(kwRegex, '').trim();
267
- }
268
- });
269
-
270
- return subfamilyName;
271
- };
272
-
273
- /**
274
- * Collects italic keywords present in a font's full name.
275
- * @param {Object} font
276
- * @param {string} fontTitle
277
- * @param {string[]} italicKeywordList
278
- * @returns {string[]}
279
- */
280
- export const processItalicKeywords = (font, fontTitle, italicKeywordList) => {
281
- let italicKW = [];
282
-
283
- italicKeywordList.forEach(keyword => {
284
- const kw = keyword.trim();
285
- const kwRegex = new RegExp(`\\b${kw}\\b`, 'i');
286
- if (kwRegex.test(fontTitle)) {
287
- fontTitle = fontTitle.replace(kwRegex, '').trim();
288
- italicKW.push(kw);
289
- }
290
- if (font?.fullName && typeof font.fullName === 'string' && font.fullName.toLowerCase().includes(kw.toLowerCase())) {
291
- if (!italicKW.includes(kw)) italicKW.push(kw);
292
- }
293
- });
294
-
295
- return italicKW;
296
- };
297
-
298
- /**
299
- * Normalises and title-cases a font title, optionally expanding abbreviations.
300
- * @param {string} fontTitle
301
- * @param {boolean} preserveShortenedNames
302
- * @returns {string}
303
- */
304
- export const formatFontTitle = (fontTitle, preserveShortenedNames = false) => {
305
- const hasItalic = fontTitle.toLowerCase().includes('italic');
306
- fontTitle = fontTitle.replace(/-/g, ' ');
307
-
308
- return fontTitle.replace(/\s+/g, ' ').trim().split(' ').map(word => {
309
- if (hasItalic && word.toLowerCase() === 'italic') return 'Italic';
310
- let fullWord = word;
311
- if (!preserveShortenedNames) {
312
- fullWord = reverseSpellingLookup(word) || word;
313
- }
314
- return fullWord[0].toUpperCase() + fullWord.slice(1);
315
- }).join(' ');
316
- };
317
-
318
- /**
319
- * Appends any italic keywords to the font title that aren't already present.
320
- * @param {Object} font
321
- * @param {string} fontTitle
322
- * @param {string[]} italicKW
323
- * @param {string} style
324
- * @param {boolean} preserveShortenedNames
325
- * @returns {string}
326
- */
327
- export const addItalicToFontTitle = (font, fontTitle, italicKW, style, preserveShortenedNames = false) => {
328
- const hasItalicAngle = font?.italicAngle !== 0;
329
- const hasItalicInName = font?.fullName.toLowerCase().includes('italic');
330
-
331
- if (italicKW.length > 0 || hasItalicAngle || hasItalicInName) {
332
- italicKW = [...new Set(italicKW)];
333
-
334
- if (italicKW.length === 0 && (hasItalicAngle || hasItalicInName)) {
335
- italicKW = ['Italic'];
336
- }
337
-
338
- if (!preserveShortenedNames) {
339
- italicKW = italicKW.map(item => reverseSpellingLookup(item) || item);
340
- }
341
-
342
- italicKW = [...new Set(italicKW)];
343
-
344
- if (italicKW.length > 1 && italicKW.includes('Italic')) {
345
- italicKW = ['Italic'];
346
- }
347
-
348
- const fontTitleLower = fontTitle.toLowerCase();
349
- italicKW = italicKW.filter(keyword => {
350
- const keywordLower = keyword.toLowerCase();
351
- const kwRegex = new RegExp(`\\b${keywordLower}\\b`);
352
- const isSubstring = fontTitleLower.split(' ').some(word =>
353
- word.includes(keywordLower) || keywordLower.includes(word)
354
- );
355
- return !kwRegex.test(fontTitleLower) && !isSubstring;
356
- });
357
-
358
- if (italicKW.length > 0) {
359
- fontTitle = fontTitle.trim() + ' ' + italicKW.join(' ');
360
- }
361
- }
362
-
363
- return fontTitle;
364
- };
365
-
366
- /**
367
- * Builds a font object ready for staging and upload.
368
- * originalFilename is stored temporarily and deleted before saving to Sanity.
369
- * @param {string} id
370
- * @param {string} fontTitle
371
- * @param {string} title
372
- * @param {Object} font
373
- * @param {boolean} variableFont
374
- * @param {string} weightName
375
- * @param {string} subfamilyName
376
- * @param {File} file
377
- * @param {string|null} originalFilename
378
- * @returns {Object}
379
- */
380
- export const createFontObject = (id, fontTitle, title, font, variableFont, weightName, subfamilyName, file, originalFilename = null) => {
381
- const fontObject = {
382
- _key: nanoid(),
383
- _id: id,
384
- title: fontTitle,
385
- slug: { _type: 'slug', current: id },
386
- typefaceName: title,
387
- style: (font?.italicAngle !== 0 || font?.fullName.toLowerCase().includes('italic')) ? 'Italic' : 'Regular',
388
- variableFont: variableFont,
389
- weightName: weightName,
390
- subfamily: subfamilyName,
391
- normalWeight: true,
392
- weight: Number(determineWeight(font, weightName)),
393
- fileInput: {},
394
- files: [file],
395
- fontKit: font,
396
- };
397
-
398
- if (originalFilename) {
399
- fontObject.originalFilename = originalFilename;
400
- }
401
-
402
- return fontObject;
403
- };
404
-
405
- /**
406
- * Determines a numeric CSS weight value for a font.
407
- * @param {Object} font
408
- * @param {string} weightName
409
- * @returns {number}
410
- */
411
- export const determineWeight = (font, weightName) => {
412
- if (font['OS/2']?.usWeightClass) {
413
- return Number(font['OS/2'].usWeightClass);
414
- }
415
-
416
- const wn = weightName?.toLowerCase() || '';
417
-
418
- if (/hairline|extra thin|extrathin/.test(wn)) return 100;
419
- if (/thin|extra light|extralight/.test(wn)) return 200;
420
- if (/light|book/.test(wn)) return 300;
421
- if (/regular|normal/.test(wn)) return 400;
422
- if (/medium/.test(wn)) return 500;
423
- if (/semi bold|semibold/.test(wn)) return 600;
424
- if (/extra bold|extrabold/.test(wn)) return 800;
425
- if (/bold/.test(wn)) return 700;
426
- if (/black|ultra/.test(wn)) return 900;
427
-
428
- return 400;
429
- };
430
-
431
- /**
432
- * Sorts a map of font objects by ascending weight, with Regular before Italic at equal weights.
433
- * @param {Object} fontsObjects
434
- * @returns {Object}
435
- */
436
- export const sortFontObjects = (fontsObjects) => {
437
- return Object.fromEntries(
438
- Object.entries(fontsObjects).sort((a, b) => {
439
- const weightA = Number(a[1].weight);
440
- const weightB = Number(b[1].weight);
441
- if (weightA === weightB) {
442
- if (a[1].style === 'Regular' && b[1].style === 'Italic') return -1;
443
- if (a[1].style === 'Italic' && b[1].style === 'Regular') return 1;
444
- return 0;
445
- }
446
- return weightA - weightB;
447
- })
448
- );
449
- };
450
-
451
- /**
452
- * Logs font metadata to the console for debugging.
453
- * @param {string} id
454
- * @param {string} fontTitle
455
- * @param {Object} font
456
- * @param {string} fileName
457
- * @param {string} subfamilyName
458
- * @param {string} style
459
- * @param {string} weightName
460
- * @param {boolean} variableFont
461
- * @param {string[]} italicKW
462
- */
463
- export const logFontInfo = (id, fontTitle, font, fileName, subfamilyName, style, weightName, variableFont, italicKW) => {
464
- console.log('=== Font Info ====');
465
- console.log('Font id: ', id);
466
- console.log('Font title: ', fontTitle);
467
- console.log('Fontkit fullName: ', font.fullName);
468
- console.log('Fontkit family name: ', font.familyName);
469
- console.log('File name: ', fileName);
470
- console.log('Subfamily: ', subfamilyName);
471
- console.log('Style: ', style);
472
- console.log('Weight: ', weightName);
473
- console.log('Variable: ', variableFont);
474
- console.log('italicKW: ', italicKW);
475
- console.log('Font italic angle: ', (font?.italicAngle !== 0 || font?.fullName.toLowerCase().includes('italic')) ? 'Italic' : 'Regular');
476
- console.log('=======');
477
- };
1
+ // Reads font files via FileReader, parses with lib-font, and builds the fontsObjects map — exports individual weight/style extraction helpers
2
+
3
+ import { parseFont } from './parseFont';
4
+ import { getNameString, getVariationAxes, getItalicAngle, getWeightClass } from './fontHelpers';
5
+ import { nanoid } from 'nanoid';
6
+ import { expandAbbreviations, removeWeightNames, reverseSpellingLookup } from './generateKeywords';
7
+ import { sanitizeForSanityId } from './sanitizeForSanityId';
8
+
9
+ /**
10
+ * Reads a font file and returns its content as an ArrayBuffer.
11
+ * @param {File} file
12
+ * @returns {Promise<ArrayBuffer>}
13
+ */
14
+ export const readFontFile = (file) => {
15
+ return new Promise((resolve, reject) => {
16
+ const reader = new FileReader();
17
+ reader.onload = (event) => { resolve(event.target.result); };
18
+ reader.onerror = (error) => { reject(error); };
19
+ reader.readAsArrayBuffer(file);
20
+ });
21
+ };
22
+
23
+ /**
24
+ * Processes an array of font files and extracts metadata for each.
25
+ * @param {File[]} files
26
+ * @param {string} title - Typeface title
27
+ * @param {string[]} weightKeywordList
28
+ * @param {string[]} italicKeywordList
29
+ * @param {Function} setStatus
30
+ * @param {boolean} preserveShortenedNames - Skip abbreviation expansion when true
31
+ * @param {boolean} preserveFileNames - Preserve original filename capitalization when true
32
+ * @returns {Promise<Object>}
33
+ */
34
+ export const processFontFiles = async (
35
+ files,
36
+ title,
37
+ weightKeywordList,
38
+ italicKeywordList,
39
+ setStatus,
40
+ preserveShortenedNames = false,
41
+ preserveFileNames = false,
42
+ ) => {
43
+ let failedFiles = [];
44
+ let subfamilies = {};
45
+ let fontsObjects = {};
46
+ let newPreferredStyle = { weight: -100, style: 'Italic', _ref: '' };
47
+
48
+ for (let i = 0; i < files.length; i++) {
49
+ const file = files[i];
50
+ const fontBuffer = await readFontFile(file);
51
+ const font = await parseFont(fontBuffer, file.name);
52
+
53
+ console.log('File name:', file.name);
54
+
55
+ // For webfonts with missing metadata, try to extract from TTF companion
56
+ const ttfFallbackMeta = await getWebfontFallbackMetadata(file, font, files);
57
+
58
+ let { weightName, subfamilyName, fontTitle, style, italicKW, variableFont } = extractFontMetadata(
59
+ font,
60
+ title,
61
+ weightKeywordList,
62
+ italicKeywordList,
63
+ preserveShortenedNames,
64
+ ttfFallbackMeta,
65
+ );
66
+
67
+ let id;
68
+ let originalFilename = null;
69
+
70
+ if (preserveFileNames) {
71
+ originalFilename = file.name.replace(/\.(ttf|otf|woff2?|eot|svg)$/i, '');
72
+ // Normalize filename: hyphens to spaces, split camelCase boundaries, collapse whitespace
73
+ const normalizedName = originalFilename
74
+ .replace(/-/g, ' ')
75
+ .replace(/([a-z])([A-Z])/g, '$1 $2')
76
+ .replace(/\s+/g, ' ')
77
+ .trim();
78
+ fontTitle = normalizedName;
79
+ id = sanitizeForSanityId(normalizedName);
80
+ } else {
81
+ id = sanitizeForSanityId(fontTitle);
82
+ }
83
+
84
+ logFontInfo(id, fontTitle, font, file.name, subfamilyName, style, weightName, variableFont, italicKW);
85
+
86
+ subfamilies[id] = subfamilyName;
87
+
88
+ if (fontsObjects[id]) {
89
+ fontsObjects[id].files = [...fontsObjects[id].files, file];
90
+ if (preserveFileNames && originalFilename) {
91
+ fontsObjects[id].originalFilename = originalFilename;
92
+ }
93
+ } else {
94
+ fontsObjects[id] = createFontObject(
95
+ id,
96
+ fontTitle,
97
+ title,
98
+ font,
99
+ variableFont,
100
+ weightName,
101
+ subfamilyName,
102
+ file,
103
+ preserveFileNames ? originalFilename : null,
104
+ );
105
+ }
106
+ }
107
+
108
+ fontsObjects = sortFontObjects(fontsObjects);
109
+ const uniqueSubfamilies = [...new Set(Object.values(subfamilies))];
110
+
111
+ console.log('Subfamilies:', subfamilies);
112
+ console.log('Unique subfamilies:', uniqueSubfamilies, uniqueSubfamilies.length);
113
+ console.log('Font objects:', Object.keys(fontsObjects));
114
+
115
+ return { fontsObjects, subfamilies, uniqueSubfamilies, newPreferredStyle, failedFiles };
116
+ };
117
+
118
+ /**
119
+ * Gets fallback metadata from a matching TTF when woff/woff2 metadata is missing.
120
+ * Returns null if no fallback is needed or no TTF companion exists.
121
+ * Unlike the old fontkit approach, this does NOT mutate the font object.
122
+ * @param {File} file
123
+ * @param {object} font - lib-font parsed font
124
+ * @param {File[]} files
125
+ * @returns {Promise<{ fullName: string, familyName: string, subfamilyName: string, preferredSubfamily: string }|null>}
126
+ */
127
+ const getWebfontFallbackMetadata = async (file, font, files) => {
128
+ if (!file.name.endsWith('.woff2') && !file.name.endsWith('.woff')) return null;
129
+
130
+ const fullName = getNameString(font, 4);
131
+ // Check if name table is missing or corrupt (empty, or only uppercase hex-like garbage)
132
+ if (fullName && fullName !== '' && !/^[A-Z0-9]+$/.test(fullName)) return null;
133
+
134
+ const ttfFile = files.find(f => f.name === file.name.replace('.woff2', '.ttf').replace('.woff', '.ttf'));
135
+ if (!ttfFile) return null;
136
+
137
+ try {
138
+ const ttfBuffer = await readFontFile(ttfFile);
139
+ const ttfFont = await parseFont(ttfBuffer, ttfFile.name);
140
+ return {
141
+ fullName: getNameString(ttfFont, 4),
142
+ familyName: getNameString(ttfFont, 1),
143
+ subfamilyName: getNameString(ttfFont, 2),
144
+ preferredSubfamily: getNameString(ttfFont, 17),
145
+ preferredFamily: getNameString(ttfFont, 16),
146
+ };
147
+ } catch (err) {
148
+ console.warn('Could not parse TTF companion for webfont fallback:', err.message);
149
+ return null;
150
+ }
151
+ };
152
+
153
+ /**
154
+ * Extracts and normalises metadata from a lib-font parsed font object.
155
+ * @param {object} font - lib-font parsed font
156
+ * @param {string} title - Typeface title
157
+ * @param {string[]} weightKeywordList
158
+ * @param {string[]} italicKeywordList
159
+ * @param {boolean} preserveShortenedNames
160
+ * @param {object|null} ttfFallbackMeta - Fallback metadata from TTF companion (for webfonts with missing names)
161
+ * @returns {Object}
162
+ */
163
+ export const extractFontMetadata = (font, title, weightKeywordList, italicKeywordList, preserveShortenedNames = false, ttfFallbackMeta = null) => {
164
+ let weightName = extractWeightName(font, italicKeywordList, ttfFallbackMeta);
165
+ if (!preserveShortenedNames) {
166
+ weightName = expandAbbreviations(weightName);
167
+ }
168
+
169
+ const fullName = getNameString(font, 4) || ttfFallbackMeta?.fullName || '';
170
+
171
+ if ((weightName === '' || weightName.toLowerCase() === 'roman') && fullName) {
172
+ weightName = extractWeightFromFullName(font, title, ttfFallbackMeta);
173
+ if (!preserveShortenedNames) {
174
+ weightName = expandAbbreviations(weightName);
175
+ }
176
+ }
177
+
178
+ const axes = getVariationAxes(font);
179
+ const variableFont = axes !== null;
180
+
181
+ // Subfamily detection — extract width/optical variant from name table.
182
+ // Primary: nameId4 (fullName) minus typeface title — the most complete name record,
183
+ // always contains width + weight (e.g. "Gear XXNarrow Regular" → "XXNarrow Regular").
184
+ // Fallback: nameId1 (familyName) minus typeface title — contains width but not always weight.
185
+ // processSubfamilyName then strips weight/italic keywords, leaving just the width variant.
186
+ // This matches the production logic that has been reliable across all foundry sites.
187
+ const trimmedTitle = title.trim();
188
+
189
+ const nameId4Remainder = fullName ? fullName.replace(trimmedTitle, '').trim() : '';
190
+ const nameId1 = getNameString(font, 1) || ttfFallbackMeta?.familyName || '';
191
+ const nameId1Remainder = nameId1 ? nameId1.replace(trimmedTitle, '').trim() : '';
192
+
193
+ let subfamilyName = nameId4Remainder || nameId1Remainder;
194
+
195
+ if (!preserveShortenedNames) {
196
+ subfamilyName = expandAbbreviations(subfamilyName);
197
+ }
198
+
199
+ let fontTitle = fullName.trim() || '';
200
+ const italicAngle = getItalicAngle(font);
201
+ let style = (italicAngle !== 0 || fullName.toLowerCase().includes('italic')) ? 'Italic' : 'Regular';
202
+
203
+ const italicKW = processItalicKeywords(font, fontTitle, italicKeywordList);
204
+
205
+ subfamilyName = processSubfamilyName(subfamilyName, weightKeywordList, italicKW, preserveShortenedNames);
206
+ fontTitle = formatFontTitle(fontTitle, preserveShortenedNames);
207
+
208
+ // Style-only names are not subfamilies — strip them
209
+ subfamilyName = subfamilyName
210
+ .replace(/\b(Italic|Slant|Slanted|Oblique|Backslant|Roman|Upright)\b/gi, '')
211
+ .replace(/\s+/g, ' ')
212
+ .trim();
213
+
214
+ if (subfamilyName !== '') {
215
+ weightName = weightName
216
+ .replace(`${subfamilyName} `, '')
217
+ .replace(` ${subfamilyName}`, '')
218
+ .trim();
219
+ }
220
+
221
+ if (variableFont) {
222
+ if (!fontTitle.toLowerCase().includes('vf')) {
223
+ fontTitle = fontTitle + ' VF';
224
+ }
225
+ // Variable fonts are not placed in subfamilies — they go in the separate variableFont array
226
+ subfamilyName = '';
227
+ }
228
+
229
+ if (!(variableFont && fontTitle.toLowerCase().includes('italic'))) {
230
+ fontTitle = addItalicToFontTitle(font, fontTitle, italicKW, style, preserveShortenedNames);
231
+ }
232
+
233
+ return { weightName, subfamilyName, fontTitle, style, italicKW, variableFont };
234
+ };
235
+
236
+ /**
237
+ * Extracts the weight name from a font's preferred subfamily or subfamily record.
238
+ * Returns "Variable" for variable fonts.
239
+ * @param {object} font - lib-font parsed font
240
+ * @param {string[]} italicKW
241
+ * @param {object|null} ttfFallbackMeta
242
+ * @returns {string}
243
+ */
244
+ export const extractWeightName = (font, italicKW, ttfFallbackMeta = null) => {
245
+ let weightName = getNameString(font, 17) || getNameString(font, 2) ||
246
+ ttfFallbackMeta?.preferredSubfamily || ttfFallbackMeta?.subfamilyName || '';
247
+
248
+ const axes = getVariationAxes(font);
249
+ if (axes !== null) {
250
+ return '';
251
+ }
252
+
253
+ if (italicKW) {
254
+ italicKW.forEach(keyword => {
255
+ const kwRegex = new RegExp(`\\b${keyword.trim()}\\b`, 'i');
256
+ if (kwRegex.test(weightName)) {
257
+ weightName = weightName.replace(kwRegex, '').trim();
258
+ }
259
+ });
260
+ }
261
+
262
+ return weightName?.toString()
263
+ .replace('Italic', '')
264
+ .replace('It', '')
265
+ .replace('Slanted', '')
266
+ .replace('Slant', '')
267
+ .replace('Backslant', '')
268
+ .trim();
269
+ };
270
+
271
+ /**
272
+ * Extracts a weight name from the font's full name record when subfamily is empty or "Roman".
273
+ * @param {object} font - lib-font parsed font
274
+ * @param {string} title
275
+ * @param {object|null} ttfFallbackMeta
276
+ * @returns {string}
277
+ */
278
+ export const extractWeightFromFullName = (font, title, ttfFallbackMeta = null) => {
279
+ let weightName = getNameString(font, 4) || ttfFallbackMeta?.fullName || '';
280
+ weightName = weightName.replace(title + ' ', '').replace(title, '').trim();
281
+ weightName = weightName.replace('Italic', '').replace('It', '').replace('Slanted', '').replace('Slant', '').trim();
282
+ return weightName;
283
+ };
284
+
285
+ /**
286
+ * Strips weight and italic keywords from a subfamily name string.
287
+ * @param {string} subfamilyName
288
+ * @param {string[]} weightKeywordList
289
+ * @param {string[]} italicKeywordList
290
+ * @param {boolean} preserveShortenedNames
291
+ * @returns {string}
292
+ */
293
+ export const processSubfamilyName = (subfamilyName, weightKeywordList, italicKeywordList, preserveShortenedNames = false) => {
294
+ weightKeywordList.forEach(keyword => {
295
+ const kwRegex = new RegExp(`\\b${keyword.trim()}\\b`, 'i');
296
+ if (kwRegex.test(subfamilyName)) {
297
+ subfamilyName = subfamilyName.replace(kwRegex, '').trim();
298
+ }
299
+ subfamilyName = removeWeightNames(subfamilyName) || subfamilyName;
300
+ if (!preserveShortenedNames) {
301
+ subfamilyName = expandAbbreviations(subfamilyName);
302
+ }
303
+ });
304
+
305
+ italicKeywordList.forEach(keyword => {
306
+ const kwRegex = new RegExp(`\\b${keyword.trim()}\\b`, 'i');
307
+ if (kwRegex.test(subfamilyName)) {
308
+ subfamilyName = subfamilyName.replace(kwRegex, '').trim();
309
+ }
310
+ });
311
+
312
+ return subfamilyName;
313
+ };
314
+
315
+ /**
316
+ * Collects italic keywords present in a font's full name.
317
+ * @param {object} font - lib-font parsed font
318
+ * @param {string} fontTitle
319
+ * @param {string[]} italicKeywordList
320
+ * @returns {string[]}
321
+ */
322
+ export const processItalicKeywords = (font, fontTitle, italicKeywordList) => {
323
+ let italicKW = [];
324
+ const fullName = getNameString(font, 4);
325
+
326
+ italicKeywordList.forEach(keyword => {
327
+ const kw = keyword.trim();
328
+ const kwRegex = new RegExp(`\\b${kw}\\b`, 'i');
329
+ if (kwRegex.test(fontTitle)) {
330
+ fontTitle = fontTitle.replace(kwRegex, '').trim();
331
+ italicKW.push(kw);
332
+ }
333
+ if (fullName && fullName.toLowerCase().includes(kw.toLowerCase())) {
334
+ if (!italicKW.includes(kw)) italicKW.push(kw);
335
+ }
336
+ });
337
+
338
+ return italicKW;
339
+ };
340
+
341
+ /**
342
+ * Normalises and title-cases a font title, optionally expanding abbreviations.
343
+ * @param {string} fontTitle
344
+ * @param {boolean} preserveShortenedNames
345
+ * @returns {string}
346
+ */
347
+ export const formatFontTitle = (fontTitle, preserveShortenedNames = false) => {
348
+ const hasItalic = fontTitle.toLowerCase().includes('italic');
349
+ fontTitle = fontTitle.replace(/-/g, ' ');
350
+
351
+ return fontTitle.replace(/\s+/g, ' ').trim().split(' ').map(word => {
352
+ if (hasItalic && word.toLowerCase() === 'italic') return 'Italic';
353
+ let fullWord = word;
354
+ if (!preserveShortenedNames) {
355
+ fullWord = reverseSpellingLookup(word) || word;
356
+ }
357
+ return fullWord[0].toUpperCase() + fullWord.slice(1);
358
+ }).join(' ');
359
+ };
360
+
361
+ /**
362
+ * Appends any italic keywords to the font title that aren't already present.
363
+ * @param {object} font - lib-font parsed font
364
+ * @param {string} fontTitle
365
+ * @param {string[]} italicKW
366
+ * @param {string} style
367
+ * @param {boolean} preserveShortenedNames
368
+ * @returns {string}
369
+ */
370
+ export const addItalicToFontTitle = (font, fontTitle, italicKW, style, preserveShortenedNames = false) => {
371
+ const hasItalicAngle = getItalicAngle(font) !== 0;
372
+ const fullName = getNameString(font, 4);
373
+ const hasItalicInName = fullName.toLowerCase().includes('italic');
374
+
375
+ if (italicKW.length > 0 || hasItalicAngle || hasItalicInName) {
376
+ italicKW = [...new Set(italicKW)];
377
+
378
+ if (italicKW.length === 0 && (hasItalicAngle || hasItalicInName)) {
379
+ italicKW = ['Italic'];
380
+ }
381
+
382
+ if (!preserveShortenedNames) {
383
+ italicKW = italicKW.map(item => reverseSpellingLookup(item) || item);
384
+ }
385
+
386
+ italicKW = [...new Set(italicKW)];
387
+
388
+ if (italicKW.length > 1 && italicKW.includes('Italic')) {
389
+ italicKW = ['Italic'];
390
+ }
391
+
392
+ const fontTitleLower = fontTitle.toLowerCase();
393
+ italicKW = italicKW.filter(keyword => {
394
+ const keywordLower = keyword.toLowerCase();
395
+ const kwRegex = new RegExp(`\\b${keywordLower}\\b`);
396
+ const isSubstring = fontTitleLower.split(' ').some(word =>
397
+ word.includes(keywordLower) || keywordLower.includes(word)
398
+ );
399
+ return !kwRegex.test(fontTitleLower) && !isSubstring;
400
+ });
401
+
402
+ if (italicKW.length > 0) {
403
+ fontTitle = fontTitle.trim() + ' ' + italicKW.join(' ');
404
+ }
405
+ }
406
+
407
+ return fontTitle;
408
+ };
409
+
410
+ /**
411
+ * Builds a font object ready for staging and upload.
412
+ * originalFilename is stored temporarily and deleted before saving to Sanity.
413
+ * @param {string} id
414
+ * @param {string} fontTitle
415
+ * @param {string} title
416
+ * @param {object} font - lib-font parsed font
417
+ * @param {boolean} variableFont
418
+ * @param {string} weightName
419
+ * @param {string} subfamilyName
420
+ * @param {File} file
421
+ * @param {string|null} originalFilename
422
+ * @returns {Object}
423
+ */
424
+ export const createFontObject = (id, fontTitle, title, font, variableFont, weightName, subfamilyName, file, originalFilename = null) => {
425
+ const italicAngle = getItalicAngle(font);
426
+ const fullName = getNameString(font, 4);
427
+
428
+ const fontObject = {
429
+ _key: nanoid(),
430
+ _id: id,
431
+ title: fontTitle,
432
+ slug: { _type: 'slug', current: id },
433
+ typefaceName: title,
434
+ style: (italicAngle !== 0 || fullName.toLowerCase().includes('italic')) ? 'Italic' : 'Regular',
435
+ variableFont: variableFont,
436
+ weightName: weightName,
437
+ subfamily: subfamilyName,
438
+ normalWeight: true,
439
+ weight: Number(determineWeight(font, weightName)),
440
+ fileInput: {},
441
+ files: [file],
442
+ fontKit: font,
443
+ };
444
+
445
+ if (originalFilename) {
446
+ fontObject.originalFilename = originalFilename;
447
+ }
448
+
449
+ return fontObject;
450
+ };
451
+
452
+ /**
453
+ * Determines a numeric CSS weight value for a font.
454
+ * @param {object} font - lib-font parsed font
455
+ * @param {string} weightName
456
+ * @returns {number}
457
+ */
458
+ export const determineWeight = (font, weightName) => {
459
+ const usWeightClass = getWeightClass(font);
460
+ if (usWeightClass) {
461
+ return Number(usWeightClass);
462
+ }
463
+
464
+ const wn = weightName?.toLowerCase() || '';
465
+
466
+ if (/hairline|extra thin|extrathin/.test(wn)) return 100;
467
+ if (/thin|extra light|extralight/.test(wn)) return 200;
468
+ if (/light|book/.test(wn)) return 300;
469
+ if (/regular|normal/.test(wn)) return 400;
470
+ if (/medium/.test(wn)) return 500;
471
+ if (/semi bold|semibold/.test(wn)) return 600;
472
+ if (/extra bold|extrabold/.test(wn)) return 800;
473
+ if (/bold/.test(wn)) return 700;
474
+ if (/black|ultra/.test(wn)) return 900;
475
+
476
+ return 400;
477
+ };
478
+
479
+ /**
480
+ * Sorts a map of font objects by ascending weight, with Regular before Italic at equal weights.
481
+ * @param {Object} fontsObjects
482
+ * @returns {Object}
483
+ */
484
+ export const sortFontObjects = (fontsObjects) => {
485
+ return Object.fromEntries(
486
+ Object.entries(fontsObjects).sort((a, b) => {
487
+ const weightA = Number(a[1].weight);
488
+ const weightB = Number(b[1].weight);
489
+ if (weightA === weightB) {
490
+ if (a[1].style === 'Regular' && b[1].style === 'Italic') return -1;
491
+ if (a[1].style === 'Italic' && b[1].style === 'Regular') return 1;
492
+ return 0;
493
+ }
494
+ return weightA - weightB;
495
+ })
496
+ );
497
+ };
498
+
499
+ /**
500
+ * Logs font metadata to the console for debugging.
501
+ * @param {string} id
502
+ * @param {string} fontTitle
503
+ * @param {object} font - lib-font parsed font
504
+ * @param {string} fileName
505
+ * @param {string} subfamilyName
506
+ * @param {string} style
507
+ * @param {string} weightName
508
+ * @param {boolean} variableFont
509
+ * @param {string[]} italicKW
510
+ */
511
+ export const logFontInfo = (id, fontTitle, font, fileName, subfamilyName, style, weightName, variableFont, italicKW) => {
512
+ const fullName = getNameString(font, 4);
513
+ const familyName = getNameString(font, 1);
514
+ const italicAngle = getItalicAngle(font);
515
+
516
+ console.log('=== Font Info ====');
517
+ console.log('Font id:', id);
518
+ console.log('Font title:', fontTitle);
519
+ console.log('Full name:', fullName);
520
+ console.log('Family name:', familyName);
521
+ console.log('File name:', fileName);
522
+ console.log('Subfamily:', subfamilyName);
523
+ console.log('Style:', style);
524
+ console.log('Weight:', weightName);
525
+ console.log('Variable:', variableFont);
526
+ console.log('ItalicKW:', italicKW);
527
+ console.log('Italic detection:', (italicAngle !== 0 || fullName.toLowerCase().includes('italic')) ? 'Italic' : 'Regular');
528
+ console.log('=======');
529
+ };