@unocss/preset-web-fonts 0.62.4 → 0.63.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/dist/index.d.mts CHANGED
@@ -1,6 +1,6 @@
1
1
  import * as _unocss_core from '@unocss/core';
2
- import { W as WebFontMeta, a as WebFontsProviders, R as ResolvedWebFontMeta, P as Provider, b as WebFontsOptions } from './shared/preset-web-fonts.DSrOMtoz.mjs';
3
- export { c as WebFontProcessor } from './shared/preset-web-fonts.DSrOMtoz.mjs';
2
+ import { W as WebFontMeta, a as WebFontsProviders, R as ResolvedWebFontMeta, P as Provider, b as WebFontsOptions } from './shared/preset-web-fonts.TGEYFvVV.mjs';
3
+ export { A as Axes, c as WebFontProcessor } from './shared/preset-web-fonts.TGEYFvVV.mjs';
4
4
 
5
5
  declare function normalizedFontMeta(meta: WebFontMeta | string, defaultProvider: WebFontsProviders): ResolvedWebFontMeta;
6
6
 
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import * as _unocss_core from '@unocss/core';
2
- import { W as WebFontMeta, a as WebFontsProviders, R as ResolvedWebFontMeta, P as Provider, b as WebFontsOptions } from './shared/preset-web-fonts.DSrOMtoz.js';
3
- export { c as WebFontProcessor } from './shared/preset-web-fonts.DSrOMtoz.js';
2
+ import { W as WebFontMeta, a as WebFontsProviders, R as ResolvedWebFontMeta, P as Provider, b as WebFontsOptions } from './shared/preset-web-fonts.TGEYFvVV.js';
3
+ export { A as Axes, c as WebFontProcessor } from './shared/preset-web-fonts.TGEYFvVV.js';
4
4
 
5
5
  declare function normalizedFontMeta(meta: WebFontMeta | string, defaultProvider: WebFontsProviders): ResolvedWebFontMeta;
6
6
 
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { toArray, definePreset } from '@unocss/core';
1
+ import { mergeDeep, toArray, definePreset } from '@unocss/core';
2
2
 
3
3
  const LAYER_IMPORTS = "imports";
4
4
 
@@ -44,6 +44,122 @@ function createFontshareProvider(name, host) {
44
44
  };
45
45
  }
46
46
 
47
+ function createFontSourceProvider(name, host) {
48
+ const fontsMap = /* @__PURE__ */ new Map();
49
+ const variablesMap = /* @__PURE__ */ new Map();
50
+ return {
51
+ name,
52
+ async getPreflight(fonts) {
53
+ const list = await Promise.all(fonts.map(async (font) => {
54
+ const css = [];
55
+ const id = font.name.toLowerCase().replace(/\s+/g, "-");
56
+ let metadata = fontsMap.get(id);
57
+ if (!metadata) {
58
+ const url = `https://api.fontsource.org/v1/fonts/${id}`;
59
+ try {
60
+ metadata = await (await import('ofetch')).$fetch(url);
61
+ fontsMap.set(id, metadata);
62
+ } catch {
63
+ throw new Error(`Failed to fetch font: ${font.name}`);
64
+ }
65
+ }
66
+ const { subsets, weights } = metadata;
67
+ if (metadata.variable) {
68
+ let variableData = variablesMap.get(id);
69
+ const url = `https://api.fontsource.org/v1/variable/${id}`;
70
+ try {
71
+ variableData = await (await import('ofetch')).$fetch(url);
72
+ variablesMap.set(id, variableData);
73
+ } catch {
74
+ throw new Error(`Failed to fetch font variable: ${font.name}`);
75
+ }
76
+ const mergeAxes = mergeDeep(variableData.axes, font.variable ?? {});
77
+ for (const subset of subsets) {
78
+ const url2 = `${host}/fontsource/fonts/${metadata.id}:vf@latest/${subset}-${font.italic ? "wght-italic" : "wght-normal"}.woff2`;
79
+ const fontObj = {
80
+ family: `${metadata.family}`,
81
+ display: "swap",
82
+ style: font.italic ? "italic" : "normal",
83
+ weight: 400,
84
+ src: [{
85
+ url: url2,
86
+ format: "woff2-variations"
87
+ }],
88
+ variable: {
89
+ wght: mergeAxes.wght ?? void 0,
90
+ wdth: mergeAxes.wdth ?? void 0,
91
+ slnt: mergeAxes.slnt ?? void 0
92
+ },
93
+ unicodeRange: metadata.unicodeRange[subset],
94
+ comment: `${metadata.id}-${subset}-wght-normal`
95
+ };
96
+ css.push(generateFontFace(fontObj));
97
+ }
98
+ } else {
99
+ const _weights = font.weights && font.weights.length > 0 ? font.weights : weights;
100
+ for (const subset of subsets) {
101
+ for (const weight of _weights) {
102
+ const url = `${host}/fontsource/fonts/${metadata.id}@latest/${subset}-${weight}-${font.italic ? "italic" : "normal"}`;
103
+ const fontObj = {
104
+ family: metadata.family,
105
+ display: "swap",
106
+ style: font.italic ? "italic" : "normal",
107
+ weight: Number(weight),
108
+ src: [{
109
+ url: `${url}.woff2`,
110
+ format: "woff2"
111
+ }],
112
+ unicodeRange: metadata.unicodeRange[subset],
113
+ comment: `${metadata.id}-${subset}-${weight}${font.italic ? "-italic" : "-normal"}`
114
+ };
115
+ css.push(generateFontFace(fontObj));
116
+ }
117
+ }
118
+ }
119
+ return css;
120
+ }));
121
+ return list.flat().join("\n\n");
122
+ }
123
+ };
124
+ }
125
+ const FontSourceProvider = createFontSourceProvider("fontsource", "https://cdn.jsdelivr.net");
126
+ function generateFontFace(font) {
127
+ const {
128
+ family,
129
+ style,
130
+ display,
131
+ weight,
132
+ variable,
133
+ src,
134
+ unicodeRange,
135
+ comment,
136
+ spacer = "\n "
137
+ } = font;
138
+ const { wght, wdth, slnt } = variable ?? {};
139
+ let result = "@font-face {";
140
+ result += `${spacer}font-family: '${family}';`;
141
+ result += `${spacer}font-style: ${slnt ? `oblique ${Number(slnt.max) * -1}deg ${Number(slnt.min) * -1}deg` : style};`;
142
+ result += `${spacer}font-display: ${display};`;
143
+ result += `${spacer}font-weight: ${wght ? getVariableWght(wght) : weight};`;
144
+ if (wdth)
145
+ result += `${spacer}font-stretch: ${wdth.min}% ${wdth.max}%;`;
146
+ result += `${spacer}src: ${src.map(({ url, format }) => `url(${url}) format('${format}')`).join(", ")};`;
147
+ if (unicodeRange)
148
+ result += `${spacer}unicode-range: ${unicodeRange};`;
149
+ if (comment)
150
+ result = `/* ${comment} */
151
+ ${result}`;
152
+ return `${result}
153
+ }`;
154
+ }
155
+ function getVariableWght(axes) {
156
+ if (!axes)
157
+ return "400";
158
+ if (axes.min === axes.max)
159
+ return `${axes.min}`;
160
+ return `${axes.min} ${axes.max}`;
161
+ }
162
+
47
163
  function createGoogleCompatibleProvider(name, host) {
48
164
  return {
49
165
  name,
@@ -80,6 +196,7 @@ const builtinProviders = {
80
196
  google: GoogleFontsProvider,
81
197
  bunny: BunnyFontsProvider,
82
198
  fontshare: FontshareProvider,
199
+ fontsource: FontSourceProvider,
83
200
  none: NoneProvider
84
201
  };
85
202
  function resolveProvider(provider) {
@@ -165,7 +282,7 @@ function createWebFontPreset(fetcher) {
165
282
  if (url)
166
283
  preflights.push(await importUrl(url));
167
284
  }
168
- preflights.push(provider.getPreflight?.(fontsForProvider));
285
+ preflights.push(await provider.getPreflight?.(fontsForProvider));
169
286
  }
170
287
  const css = preflights.filter(Boolean).join("\n");
171
288
  return css;
package/dist/local.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { c as WebFontProcessor } from './shared/preset-web-fonts.DSrOMtoz.mjs';
1
+ import { c as WebFontProcessor } from './shared/preset-web-fonts.TGEYFvVV.mjs';
2
2
  import '@unocss/core';
3
3
 
4
4
  interface LocalFontProcessorOptions {
package/dist/local.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { c as WebFontProcessor } from './shared/preset-web-fonts.DSrOMtoz.js';
1
+ import { c as WebFontProcessor } from './shared/preset-web-fonts.TGEYFvVV.js';
2
2
  import '@unocss/core';
3
3
 
4
4
  interface LocalFontProcessorOptions {
@@ -1,10 +1,11 @@
1
1
  import { Awaitable, Arrayable } from '@unocss/core';
2
2
 
3
- type WebFontsProviders = 'google' | 'bunny' | 'fontshare' | 'none' | Provider;
3
+ type WebFontsProviders = 'google' | 'bunny' | 'fontshare' | 'fontsource' | 'none' | Provider;
4
4
  interface WebFontMeta {
5
5
  name: string;
6
6
  weights?: (string | number)[];
7
7
  italic?: boolean;
8
+ variable?: Record<string, Partial<Axes>>;
8
9
  /**
9
10
  * Override the provider
10
11
  * @default <matches root config>
@@ -75,9 +76,15 @@ interface WebFontsOptions {
75
76
  }
76
77
  interface Provider {
77
78
  name: WebFontsProviders;
78
- getPreflight?: (fonts: WebFontMeta[]) => string;
79
+ getPreflight?: (fonts: WebFontMeta[]) => Awaitable<string | undefined>;
79
80
  getImportUrl?: (fonts: WebFontMeta[]) => string | undefined;
80
81
  getFontName?: (font: WebFontMeta) => string;
81
82
  }
83
+ interface Axes {
84
+ default: string;
85
+ min: string;
86
+ max: string;
87
+ step: string;
88
+ }
82
89
 
83
- export type { Provider as P, ResolvedWebFontMeta as R, WebFontMeta as W, WebFontsProviders as a, WebFontsOptions as b, WebFontProcessor as c };
90
+ export type { Axes as A, Provider as P, ResolvedWebFontMeta as R, WebFontMeta as W, WebFontsProviders as a, WebFontsOptions as b, WebFontProcessor as c };
@@ -1,10 +1,11 @@
1
1
  import { Awaitable, Arrayable } from '@unocss/core';
2
2
 
3
- type WebFontsProviders = 'google' | 'bunny' | 'fontshare' | 'none' | Provider;
3
+ type WebFontsProviders = 'google' | 'bunny' | 'fontshare' | 'fontsource' | 'none' | Provider;
4
4
  interface WebFontMeta {
5
5
  name: string;
6
6
  weights?: (string | number)[];
7
7
  italic?: boolean;
8
+ variable?: Record<string, Partial<Axes>>;
8
9
  /**
9
10
  * Override the provider
10
11
  * @default <matches root config>
@@ -75,9 +76,15 @@ interface WebFontsOptions {
75
76
  }
76
77
  interface Provider {
77
78
  name: WebFontsProviders;
78
- getPreflight?: (fonts: WebFontMeta[]) => string;
79
+ getPreflight?: (fonts: WebFontMeta[]) => Awaitable<string | undefined>;
79
80
  getImportUrl?: (fonts: WebFontMeta[]) => string | undefined;
80
81
  getFontName?: (font: WebFontMeta) => string;
81
82
  }
83
+ interface Axes {
84
+ default: string;
85
+ min: string;
86
+ max: string;
87
+ step: string;
88
+ }
82
89
 
83
- export type { Provider as P, ResolvedWebFontMeta as R, WebFontMeta as W, WebFontsProviders as a, WebFontsOptions as b, WebFontProcessor as c };
90
+ export type { Axes as A, Provider as P, ResolvedWebFontMeta as R, WebFontMeta as W, WebFontsProviders as a, WebFontsOptions as b, WebFontProcessor as c };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@unocss/preset-web-fonts",
3
3
  "type": "module",
4
- "version": "0.62.4",
4
+ "version": "0.63.0",
5
5
  "description": "Web Fonts support for Uno CSS",
6
6
  "author": "Anthony Fu <anthonyfu117@hotmail.com>",
7
7
  "license": "MIT",
@@ -49,8 +49,8 @@
49
49
  "dist"
50
50
  ],
51
51
  "dependencies": {
52
- "ofetch": "^1.3.4",
53
- "@unocss/core": "0.62.4"
52
+ "ofetch": "^1.4.0",
53
+ "@unocss/core": "0.63.0"
54
54
  },
55
55
  "scripts": {
56
56
  "build": "unbuild",