fontaine 0.6.0 → 0.8.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 +68 -9
- package/dist/capsize-font-metrics.d.mts +1 -0
- package/dist/capsize-font-metrics.mjs +1 -0
- package/dist/index.cjs +363 -247
- package/dist/index.d.cts +136 -89
- package/dist/index.d.mts +137 -89
- package/dist/index.mjs +334 -242
- package/package.json +31 -45
- package/dist/index.d.ts +0 -104
package/README.md
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
> Automatic font fallback based on font metrics
|
|
9
9
|
|
|
10
10
|
- [✨ Changelog](https://github.com/unjs/fontaine/blob/main/CHANGELOG.md)
|
|
11
|
-
- [▶️ Online playground](https://stackblitz.com/github/unjs/fontaine/tree/main/playground)
|
|
11
|
+
- [▶️ Online playground](https://stackblitz.com/github/unjs/fontaine/tree/main/packages/fontaine/playground)
|
|
12
12
|
|
|
13
13
|
## Features
|
|
14
14
|
|
|
@@ -52,12 +52,20 @@ import { FontaineTransform } from 'fontaine'
|
|
|
52
52
|
import { defineConfig } from 'astro/config'
|
|
53
53
|
|
|
54
54
|
const options = {
|
|
55
|
+
// You can specify fallbacks as an array (applies to all fonts)
|
|
55
56
|
fallbacks: ['BlinkMacSystemFont', 'Segoe UI', 'Helvetica Neue', 'Arial', 'Noto Sans'],
|
|
57
|
+
|
|
58
|
+
// Or as an object to configure specific fallbacks per font family
|
|
59
|
+
// fallbacks: {
|
|
60
|
+
// Poppins: ['Helvetica Neue'],
|
|
61
|
+
// 'JetBrains Mono': ['Courier New']
|
|
62
|
+
// },
|
|
63
|
+
|
|
56
64
|
// You may need to resolve assets like `/fonts/Roboto.woff2` to a particular directory
|
|
57
65
|
resolvePath: id => `file:///path/to/public/dir${id}`,
|
|
58
|
-
//
|
|
66
|
+
// fallbackName: (originalName) => `${name} fallback`
|
|
59
67
|
// sourcemap: false
|
|
60
|
-
// skipFontFaceGeneration: (fallbackName) => fallbackName === 'Roboto
|
|
68
|
+
// skipFontFaceGeneration: (fallbackName) => fallbackName === 'Roboto fallback'
|
|
61
69
|
}
|
|
62
70
|
|
|
63
71
|
// Vite
|
|
@@ -116,16 +124,67 @@ export default defineConfig({
|
|
|
116
124
|
> **Note**
|
|
117
125
|
> If you are using Nuxt, check out [nuxt-font-metrics](https://github.com/danielroe/nuxt-font-metrics) which uses `fontaine` under the hood.
|
|
118
126
|
|
|
119
|
-
If your custom font is used through the mechanism of CSS variables, you'll need to make a tweak to your CSS variables to give fontaine a helping hand. Docusaurus is an example of this, it uses the `--ifm-font-family-base` variable to reference a custom font. In order that fontaine can connect the variable with the font, we need to add a `{Name of Font}
|
|
127
|
+
If your custom font is used through the mechanism of CSS variables, you'll need to make a tweak to your CSS variables to give fontaine a helping hand. Docusaurus is an example of this, it uses the `--ifm-font-family-base` variable to reference a custom font. In order that fontaine can connect the variable with the font, we need to add a `{Name of Font} fallback` suffix to that variable. What does this look like? Well imagine we were using the custom font Poppins which is referenced from the `--ifm-font-family-base` variable, we'd make the following adjustment:
|
|
120
128
|
|
|
121
129
|
```diff
|
|
122
130
|
:root {
|
|
123
131
|
/* ... */
|
|
124
132
|
- --ifm-font-family-base: 'Poppins';
|
|
125
|
-
+ --ifm-font-family-base: 'Poppins', 'Poppins
|
|
133
|
+
+ --ifm-font-family-base: 'Poppins', 'Poppins fallback';
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
Behind the scenes, there is a 'Poppins fallback' `@font-face` rule that has been created by fontaine. By manually adding this fallback font family to our CSS variable, we make our site use the fallback `@font-face` rule with the correct font metrics that fontaine generates.
|
|
137
|
+
|
|
138
|
+
## Category-Aware Fallbacks
|
|
139
|
+
|
|
140
|
+
Fontaine automatically selects appropriate fallback fonts based on font categories (serif, sans-serif, monospace, etc.) when using object-based fallback configuration.
|
|
141
|
+
|
|
142
|
+
```js
|
|
143
|
+
const options = {
|
|
144
|
+
// Use an empty object to enable automatic category-based fallbacks
|
|
145
|
+
fallbacks: {},
|
|
146
|
+
|
|
147
|
+
// Or customize specific categories while keeping defaults for others
|
|
148
|
+
categoryFallbacks: {
|
|
149
|
+
'serif': ['Georgia', 'Times New Roman'],
|
|
150
|
+
'sans-serif': ['Arial', 'Helvetica'],
|
|
151
|
+
// monospace, display, and handwriting categories use defaults
|
|
152
|
+
}
|
|
153
|
+
}
|
|
126
154
|
```
|
|
127
155
|
|
|
128
|
-
|
|
156
|
+
### Default Category Fallbacks
|
|
157
|
+
|
|
158
|
+
- **sans-serif**: `BlinkMacSystemFont`, `Segoe UI`, `Helvetica Neue`, `Arial`, `Noto Sans`
|
|
159
|
+
- **serif**: `Times New Roman`, `Georgia`, `Noto Serif`
|
|
160
|
+
- **monospace**: `Courier New`, `Roboto Mono`, `Noto Sans Mono`
|
|
161
|
+
- **display** & **handwriting**: Same as sans-serif
|
|
162
|
+
|
|
163
|
+
> **Note:** These presets are available programmatically via `DEFAULT_CATEGORY_FALLBACKS` and can be used with the `resolveCategoryFallbacks` helper function for advanced use cases. Both are exported from the `fontaine` package and shared across related packages (e.g., `fontless`) to ensure consistent fallback behavior.
|
|
164
|
+
|
|
165
|
+
### Fallback Priority
|
|
166
|
+
|
|
167
|
+
1. **Array format** (`fallbacks: ['Arial']`) - Uses specified fonts for all families (legacy behavior)
|
|
168
|
+
2. **Per-family override** (`fallbacks: { Poppins: ['Arial'] }`) - Uses specified fonts for that family
|
|
169
|
+
3. **Category-based** - When a family isn't specified, uses the appropriate category preset
|
|
170
|
+
4. **Global default** - Falls back to sans-serif preset if no category is detected
|
|
171
|
+
|
|
172
|
+
Example:
|
|
173
|
+
|
|
174
|
+
```js
|
|
175
|
+
{
|
|
176
|
+
fallbacks: {
|
|
177
|
+
// Specific override for Poppins
|
|
178
|
+
'Poppins': ['Arial'],
|
|
179
|
+
// Other sans-serif fonts will use the sans-serif preset
|
|
180
|
+
// Serif fonts will use the serif preset automatically
|
|
181
|
+
},
|
|
182
|
+
categoryFallbacks: {
|
|
183
|
+
// Customize the serif preset
|
|
184
|
+
'serif': ['Georgia']
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
```
|
|
129
188
|
|
|
130
189
|
## How it works
|
|
131
190
|
|
|
@@ -141,7 +200,7 @@ Behind the scenes, there is a 'Poppins override' `@font-face` rule that has been
|
|
|
141
200
|
}
|
|
142
201
|
/* This additional font-face declaration will be added to your CSS. */
|
|
143
202
|
@font-face {
|
|
144
|
-
font-family: 'Roboto
|
|
203
|
+
font-family: 'Roboto fallback';
|
|
145
204
|
src: local('BlinkMacSystemFont'), local('Segoe UI'), local('Helvetica Neue'),
|
|
146
205
|
local('Arial'), local('Noto Sans');
|
|
147
206
|
ascent-override: 92.7734375%;
|
|
@@ -150,13 +209,13 @@ Behind the scenes, there is a 'Poppins override' `@font-face` rule that has been
|
|
|
150
209
|
}
|
|
151
210
|
```
|
|
152
211
|
|
|
153
|
-
Then, whenever you use `font-family: 'Roboto'`, `fontaine` will add the
|
|
212
|
+
Then, whenever you use `font-family: 'Roboto'`, `fontaine` will add the fallback to the font-family:
|
|
154
213
|
|
|
155
214
|
```css
|
|
156
215
|
:root {
|
|
157
216
|
font-family: 'Roboto';
|
|
158
217
|
/* This becomes */
|
|
159
|
-
font-family: 'Roboto', 'Roboto
|
|
218
|
+
font-family: 'Roboto', 'Roboto fallback';
|
|
160
219
|
}
|
|
161
220
|
```
|
|
162
221
|
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
declare export const entireMetricsCollection: typeof import('@capsizecss/metrics/entireMetricsCollection').entireMetricsCollection
|