Skip to content

Commit a17831d

Browse files
feat: add support to fontsource variable v5
close #60, close #57
1 parent 15ea829 commit a17831d

File tree

7 files changed

+106
-22
lines changed

7 files changed

+106
-22
lines changed

README.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,7 @@ Unfonts({
406406
// families can be either strings (load default font set)
407407
// Require the `@fontsource/abeezee` package to be installed.
408408
'ABeeZee',
409+
'Inter Variable', // Require the `@fontsource-variable/inter` package to be installed.
409410
{
410411
/**
411412
* Name of the font family.
@@ -428,17 +429,17 @@ Unfonts({
428429
{
429430
/**
430431
* Name of the font family.
431-
* Require the `@fontsource/cabin` package to be installed.
432+
* Require the `@fontsource-variable/cabin` package to be installed.
432433
*/
433434
name: 'Cabin',
434435
/**
435-
* Instead of using weights/styles, we can use variables fonts.
436+
* When using variable fonts, you can choose which axes to load.
436437
*/
437-
variables: ['variable-full', 'variable-full-italic'],
438-
/**
439-
* Use another font subset.
440-
*/
441-
subset: 'latin-ext',
438+
variable: {
439+
wght: true,
440+
slnt: true,
441+
ital: true,
442+
},
442443
},
443444
],
444445
},

examples/nuxt/app.vue

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import src from 'unfonts.css?raw'
88
<h2>I'm using a local font !</h2>
99
<div class="fontsource-abeezee">I'm a ABeeZee fontsource</div>
1010
<div class="fontsource-truculenta">I'm a Truculenta fontsource</div>
11+
<div class="fontsource-variable">I'm a Noto Sans JP Variable fontsource</div>
1112
<pre>links: {{ links }}</pre>
1213
<ClientOnly>
1314
<pre>src: {{ src }}</pre>
@@ -46,4 +47,8 @@ h2 {
4647
.fontsource-truculenta {
4748
font-family: "Truculenta", sans-serif;
4849
}
50+
.fontsource-variable {
51+
font-family: "Noto Sans JP Variable", sans-serif;
52+
font-weight: 875;
53+
}
4954
</style>

examples/nuxt/nuxt.config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ export default defineNuxtConfig({
2727
name: 'Truculenta',
2828
weights: [400, 700],
2929
},
30+
{
31+
name: 'Noto Sans JP Variable',
32+
},
3033
],
3134
},
3235
},

examples/nuxt/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
"preview": "nuxt preview"
88
},
99
"devDependencies": {
10+
"@fontsource/abeezee": "^4.5.10",
11+
"@fontsource/truculenta": "^4.5.12",
12+
"@fontsource-variable/noto-sans-jp": "^5.0.16",
1013
"nuxt": "^3.4.1",
1114
"unplugin-fonts": "workspace:*"
1215
}

pnpm-lock.yaml

Lines changed: 24 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/loaders/fontsource.ts

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,67 @@ export function fontsourceImports(options?: FontsourceFonts) {
1313

1414
if (typeof family === 'string') {
1515
const name = family.toLowerCase().replace(/ /g, '-')
16-
source.push(`@fontsource/${name}/index.css`)
16+
if (name.endsWith('-variable')) {
17+
source.push(`@fontsource-variable/${name.replace('-variable', '')}/index.css`)
18+
} else {
19+
source.push(`@fontsource/${name}/index.css`)
20+
}
1721
continue
1822
}
1923

20-
const name = family.name.toLowerCase().replace(/ /g, '-')
21-
const subsetPrefix = family.subset ? `${family.subset}-` : ''
24+
let name = family.name.toLowerCase()
25+
.replace(/ /g, '-')
26+
.replace('-variable', '')
2227

23-
if ('variables' in family) {
24-
for (const variable of family.variables)
25-
source.push(`@fontsource/${name}/${subsetPrefix}${variable}.css`)
28+
if (family.name.endsWith(' Variable') && !('variable' in family)) {
29+
source.push(`@fontsource-variable/${name}/index.css`)
30+
continue
2631
}
27-
else if ('weights' in family) {
32+
33+
if ('variable' in family) {
34+
if (family.variable === true) {
35+
source.push(`@fontsource-variable/${name}/index.css`)
36+
continue
37+
}
38+
39+
const activeAxes = Object.keys(family.variable).filter((axis) => axis !== 'ital');
40+
const isItal = family.variable.ital
41+
const isStandard = activeAxes.every((axis) =>
42+
['wght', 'wdth', 'slnt', 'opsz'].includes(axis),
43+
);
44+
if (activeAxes.length === 1 && family.variable.wght) {
45+
if (isItal) {
46+
source.push(`@fontsource-variable/${name}/wght-italic.css`)
47+
continue
48+
}
49+
50+
source.push(`@fontsource-variable/${name}/index.css`)
51+
continue
52+
}
53+
54+
if (activeAxes.length === 2 && family.variable.wght) {
55+
const selected =
56+
activeAxes.find((axis) => axis !== 'wght')?.toLowerCase() ?? 'wght';
57+
if (isItal) {
58+
source.push(`@fontsource-variable/${name}/${selected}-italic.css`);
59+
continue
60+
}
61+
source.push(`@fontsource-variable/${name}/${selected}.css`)
62+
continue
63+
}
64+
65+
if (isStandard) {
66+
source.push(`import '@fontsource-variable/${name}/standard.css';`)
67+
continue
68+
}
69+
70+
// If the selected axes is not within standard, return full
71+
source.push(`import '@fontsource-variable/${name}/full.css';`)
72+
continue
73+
}
74+
75+
if ('weights' in family) {
76+
const subsetPrefix = family.subset ? `${family.subset}-` : ''
2877
for (const weight of family.weights) {
2978
if (family.styles) {
3079
for (const style of family.styles) {

src/types.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,13 @@ interface WeightsFontsourceFontFamily extends BaseFontsourceFontFamily {
118118
weights: (100 | 200 | 300 | 400 | 500 | 600 | 700 | 800 | 900)[]
119119
}
120120
interface VariableFontsourceFontFamily extends BaseFontsourceFontFamily {
121-
variables: ('variable' | 'variable-italic' | 'variable-full' | 'variable-full-italic')[]
121+
variable: true | {
122+
wght?: boolean;
123+
wdth?: boolean;
124+
slnt?: boolean;
125+
opsz?: boolean;
126+
ital?: boolean;
127+
}
122128
}
123129
export type FontsourceFontFamily = WeightsFontsourceFontFamily | VariableFontsourceFontFamily
124130
export interface FontsourceFonts {

0 commit comments

Comments
 (0)