@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.
- package/README.md +437 -437
- package/dist/UploadModal-6LIX7XOK.js +6 -0
- package/dist/UploadModal-NME2W53V.mjs +6 -0
- package/dist/chunk-646WCBRR.mjs +7276 -0
- package/dist/chunk-FH4QKHOH.js +7276 -0
- package/dist/index.js +747 -1675
- package/dist/index.mjs +400 -1237
- package/package.json +85 -85
- package/src/components/BatchUploadFonts.jsx +653 -639
- package/src/components/BulkActions.jsx +99 -0
- package/src/components/ExistingDocumentResolver.jsx +152 -0
- package/src/components/FontReviewCard.jsx +415 -0
- package/src/components/FontScriptUploaderComponent.jsx +463 -463
- package/src/components/GenerateCollectionsPairsComponent.jsx +259 -259
- package/src/components/KeyValueInput.jsx +95 -95
- package/src/components/KeyValueReferenceInput.jsx +254 -254
- package/src/components/NestedObjectArraySelector.jsx +146 -146
- package/src/components/PriceInput.jsx +26 -26
- package/src/components/PrimaryCollectionGeneratorTypeface.jsx +116 -116
- package/src/components/RegenerateSubfamiliesComponent.jsx +185 -185
- package/src/components/SetOTF.jsx +87 -87
- package/src/components/SingleUploaderTool.jsx +672 -673
- package/src/components/StatusDisplay.jsx +26 -26
- package/src/components/StyleCountInput.jsx +16 -16
- package/src/components/UpdateScriptsComponent.jsx +76 -76
- package/src/components/UploadButton.jsx +43 -43
- package/src/components/UploadModal.jsx +268 -0
- package/src/components/UploadScriptsComponent.jsx +539 -537
- package/src/components/UploadStep1Settings.jsx +272 -0
- package/src/components/UploadStep2Review.jsx +472 -0
- package/src/components/UploadStep3Execute.jsx +234 -0
- package/src/components/UploadSummary.jsx +196 -0
- package/src/components/VariableInstanceReferencesInput.jsx +190 -190
- package/src/hooks/useNestedObjects.js +92 -92
- package/src/hooks/useSanityClient.js +9 -9
- package/src/index.js +115 -70
- package/src/schema/openTypeField.js +1945 -1945
- package/src/schema/styleCountField.js +12 -12
- package/src/schema/stylesField.js +268 -268
- package/src/schema/stylisticSetField.js +301 -301
- package/src/utils/buildUploadPlan.js +325 -0
- package/src/utils/executeUploadPlan.js +437 -0
- package/src/utils/executionReducer.js +56 -0
- package/src/utils/fontHelpers.js +267 -0
- package/src/utils/generateCssFile.js +207 -205
- package/src/utils/generateFontData.js +98 -145
- package/src/utils/generateFontFile.js +38 -38
- package/src/utils/generateKeywords.js +185 -185
- package/src/utils/generateSubset.js +45 -45
- package/src/utils/getEmptyFontKit.js +101 -99
- package/src/utils/parseFont.js +55 -0
- package/src/utils/parseVariableFontInstances.js +211 -211
- package/src/utils/planReducer.js +517 -0
- package/src/utils/planTypes.js +183 -0
- package/src/utils/processFontFiles.js +529 -477
- package/src/utils/regenerateFontData.js +146 -146
- package/src/utils/resolveExistingFont.js +87 -0
- package/src/utils/sanitizeForSanityId.js +65 -65
- package/src/utils/updateFontPrices.js +94 -94
- package/src/utils/updateTypefaceDocument.js +149 -160
- package/src/utils/uploadFontFiles.js +405 -316
- package/src/utils/utils.js +24 -24
|
@@ -1,477 +1,529 @@
|
|
|
1
|
-
// Reads font files via FileReader, parses with
|
|
2
|
-
|
|
3
|
-
import
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
*
|
|
11
|
-
* @
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
reader
|
|
17
|
-
reader.
|
|
18
|
-
reader.
|
|
19
|
-
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
*
|
|
25
|
-
* @param {
|
|
26
|
-
* @param {string
|
|
27
|
-
* @param {string[]}
|
|
28
|
-
* @param {
|
|
29
|
-
* @param {
|
|
30
|
-
* @param {boolean}
|
|
31
|
-
* @
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
let
|
|
44
|
-
let
|
|
45
|
-
let
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
const
|
|
50
|
-
const
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
font,
|
|
60
|
-
title,
|
|
61
|
-
weightKeywordList,
|
|
62
|
-
italicKeywordList,
|
|
63
|
-
preserveShortenedNames,
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
let originalFilename = null;
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
.
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
*
|
|
275
|
-
* @param {
|
|
276
|
-
* @
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
*
|
|
320
|
-
* @
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
}
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
}
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
*
|
|
368
|
-
*
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
*
|
|
454
|
-
* @param {
|
|
455
|
-
* @param {
|
|
456
|
-
* @
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
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
|
+
};
|