@webstudio-is/fonts 0.280.1 → 0.282.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/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
- const face = faces.get(assetKey);
282
- const format = FONT_FORMATS.get(asset.format);
283
- if (format === void 0) {
287
+ if (seenUrls.has(url)) {
284
288
  continue;
285
289
  }
286
- if (face === void 0) {
287
- const face2 = formatFace(asset, format, url);
288
- faces.set(assetKey, face2);
289
- continue;
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
- face.src += `, url(${sanitizeCssUrl(url)}) format("${format}")`;
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
@@ -364,7 +381,7 @@ var fontWeights = {
364
381
  },
365
382
  "700": {
366
383
  label: "Bold",
367
- names: ["bold", "bold"]
384
+ names: ["bold"]
368
385
  },
369
386
  "800": {
370
387
  label: "Extra Bold",
@@ -25,7 +25,7 @@ export declare const fontWeights: {
25
25
  };
26
26
  readonly "700": {
27
27
  readonly label: "Bold";
28
- readonly names: readonly ["bold", "bold"];
28
+ readonly names: readonly ["bold"];
29
29
  };
30
30
  readonly "800": {
31
31
  readonly label: "Extra Bold";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webstudio-is/fonts",
3
- "version": "0.280.1",
3
+ "version": "0.282.0",
4
4
  "description": "Fonts utils",
5
5
  "author": "Webstudio <github@webstudio.is>",
6
6
  "homepage": "https://webstudio.is",