Skip to content

Commit 122c319

Browse files
committed
More font loading improvements
1 parent ad9d0a0 commit 122c319

1 file changed

Lines changed: 18 additions & 4 deletions

File tree

src/text.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,21 +142,27 @@ export function getFonts(
142142
fonts,
143143
templateUrl = 'https://cdn.jsdelivr.net/npm/@fontsource/{font-family}/{fontweight}{-fontstyle}.css',
144144
) {
145-
const fontDescriptions = [];
145+
let fontDescriptions;
146146
for (let i = 0, ii = fonts.length; i < ii; ++i) {
147147
const font = fonts[i];
148148
if (font in processedFontFamilies) {
149149
continue;
150150
}
151151
processedFontFamilies[font] = true;
152-
const cssFont = mb2css(font, 1);
152+
const cssFont = mb2css(font, 16);
153153
const parts = cssFont.split(' ');
154+
if (!fontDescriptions) {
155+
fontDescriptions = [];
156+
}
154157
fontDescriptions.push([
155158
parts.slice(3).join(' ').replace(/"/g, ''),
156159
parts[1],
157160
parts[0],
158161
]);
159162
}
163+
if (!fontDescriptions) {
164+
return fonts;
165+
}
160166

161167
(async () => {
162168
await document.fonts.ready;
@@ -168,9 +174,17 @@ export function getFonts(
168174
}
169175
const weight = fontDescription[1];
170176
const style = fontDescription[2];
177+
const loaded = await document.fonts.load(
178+
`${style} ${weight} 16px "${family}"`,
179+
);
171180
if (
172-
(await document.fonts.load(`${style} ${weight} 16px "${family}"`))
173-
.length === 0
181+
!loaded.some(
182+
(f) =>
183+
f.family.replace(/^['"]|['"]$/g, '').toLowerCase() ===
184+
family.toLowerCase() &&
185+
f.weight == weight &&
186+
f.style === style,
187+
)
174188
) {
175189
const fontUrl = templateUrl
176190
.replace('{font-family}', family.replace(/ /g, '-').toLowerCase())

0 commit comments

Comments
 (0)