@webstudio-is/fonts 0.281.0 → 0.282.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.
- package/lib/index.js +26 -9
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -246,6 +246,11 @@ var FONT_STYLES = ["normal", "italic", "oblique"];
|
|
|
246
246
|
|
|
247
247
|
// src/get-font-faces.ts
|
|
248
248
|
var sanitizeCssUrl = (str) => JSON.stringify(str);
|
|
249
|
+
var fontFormatOrder = Array.from(FONT_FORMATS.values());
|
|
250
|
+
var getFontFormat = (asset) => {
|
|
251
|
+
const extension = asset.name.slice(asset.name.lastIndexOf(".") + 1);
|
|
252
|
+
return FONT_FORMATS.has(extension) ? FONT_FORMATS.get(extension) ?? asset.format : FONT_FORMATS.get(asset.format) ?? asset.format;
|
|
253
|
+
};
|
|
249
254
|
var formatFace = (asset, format, url) => {
|
|
250
255
|
if ("variationAxes" in asset.meta) {
|
|
251
256
|
const { wght, wdth } = asset.meta?.variationAxes ?? {};
|
|
@@ -275,22 +280,34 @@ var getKey = (asset) => {
|
|
|
275
280
|
var getFontFaces = (assets, options) => {
|
|
276
281
|
const { assetBaseUrl } = options;
|
|
277
282
|
const faces = /* @__PURE__ */ new Map();
|
|
283
|
+
const seenUrls = /* @__PURE__ */ new Set();
|
|
278
284
|
for (const asset of assets) {
|
|
279
285
|
const url = `${assetBaseUrl}${asset.name}`;
|
|
280
286
|
const assetKey = getKey(asset);
|
|
281
|
-
|
|
282
|
-
const format = FONT_FORMATS.get(asset.format);
|
|
283
|
-
if (format === void 0) {
|
|
287
|
+
if (seenUrls.has(url)) {
|
|
284
288
|
continue;
|
|
285
289
|
}
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
+
seenUrls.add(url);
|
|
291
|
+
const format = getFontFormat(asset);
|
|
292
|
+
const sources = faces.get(assetKey) ?? /* @__PURE__ */ new Map();
|
|
293
|
+
const existing = sources.get(format);
|
|
294
|
+
if (existing === void 0 || url < existing.url) {
|
|
295
|
+
sources.set(format, { asset, format, url });
|
|
290
296
|
}
|
|
291
|
-
|
|
297
|
+
faces.set(assetKey, sources);
|
|
292
298
|
}
|
|
293
|
-
return Array.from(faces.values())
|
|
299
|
+
return Array.from(faces.values(), (sources) => {
|
|
300
|
+
const [{ asset, format, url }, ...fallbacks] = Array.from(
|
|
301
|
+
sources.values()
|
|
302
|
+
).sort(
|
|
303
|
+
(left, right) => fontFormatOrder.indexOf(left.format) - fontFormatOrder.indexOf(right.format)
|
|
304
|
+
);
|
|
305
|
+
const face = formatFace(asset, format, url);
|
|
306
|
+
face.src += fallbacks.map(
|
|
307
|
+
({ format: fallbackFormat, url: fallbackUrl }) => `, url(${sanitizeCssUrl(fallbackUrl)}) format("${fallbackFormat}")`
|
|
308
|
+
).join("");
|
|
309
|
+
return face;
|
|
310
|
+
});
|
|
294
311
|
};
|
|
295
312
|
|
|
296
313
|
// src/schema.ts
|