|
1 | 1 | import type * as unifont from 'unifont'; |
2 | 2 | import { FONT_FORMAT_MAP } from '../constants.js'; |
3 | | -import type { FontTypeExtractor, UrlProxy } from '../definitions.js'; |
| 3 | +import type { FontFileReader, FontTypeExtractor, UrlProxy } from '../definitions.js'; |
4 | 4 | import type { ResolvedLocalFontFamily } from '../types.js'; |
5 | 5 |
|
6 | 6 | interface Options { |
7 | 7 | family: ResolvedLocalFontFamily; |
8 | 8 | urlProxy: UrlProxy; |
9 | 9 | fontTypeExtractor: FontTypeExtractor; |
| 10 | + fontFileReader: FontFileReader; |
10 | 11 | } |
11 | 12 |
|
12 | | -export function resolveLocalFont({ family, urlProxy, fontTypeExtractor }: Options): { |
| 13 | +export function resolveLocalFont({ |
| 14 | + family, |
| 15 | + urlProxy, |
| 16 | + fontTypeExtractor, |
| 17 | + fontFileReader, |
| 18 | +}: Options): { |
13 | 19 | fonts: Array<unifont.FontFaceData>; |
14 | 20 | } { |
15 | 21 | return { |
16 | | - fonts: family.variants.map((variant) => ({ |
17 | | - weight: variant.weight, |
18 | | - style: variant.style, |
| 22 | + fonts: family.variants.map((variant) => { |
| 23 | + const shouldInfer = variant.weight === undefined || variant.style === undefined; |
| 24 | + |
| 25 | + // We prepare the data |
| 26 | + const data: unifont.FontFaceData = { |
| 27 | + // If it should be inferred, we don't want to set the value |
| 28 | + weight: variant.weight, |
| 29 | + style: variant.style, |
| 30 | + src: [], |
| 31 | + unicodeRange: variant.unicodeRange, |
| 32 | + display: variant.display, |
| 33 | + stretch: variant.stretch, |
| 34 | + featureSettings: variant.featureSettings, |
| 35 | + variationSettings: variant.variationSettings, |
| 36 | + }; |
19 | 37 | // We proxy each source |
20 | | - src: variant.src.map((source, index) => ({ |
21 | | - originalURL: source.url, |
22 | | - url: urlProxy.proxy({ |
23 | | - url: source.url, |
24 | | - // We only use the first source for preloading. For example if woff2 and woff |
25 | | - // are available, we only keep woff2. |
26 | | - collectPreload: index === 0, |
27 | | - data: { |
28 | | - weight: variant.weight, |
29 | | - style: variant.style, |
30 | | - }, |
31 | | - init: null, |
32 | | - }), |
33 | | - format: FONT_FORMAT_MAP[fontTypeExtractor.extract(source.url)], |
34 | | - tech: source.tech, |
35 | | - })), |
36 | | - display: variant.display, |
37 | | - unicodeRange: variant.unicodeRange, |
38 | | - stretch: variant.stretch, |
39 | | - featureSettings: variant.featureSettings, |
40 | | - variationSettings: variant.variationSettings, |
41 | | - })), |
| 38 | + data.src = variant.src.map((source, index) => { |
| 39 | + // We only try to infer for the first source. Indeed if it doesn't work, the function |
| 40 | + // call will throw an error so that will be interruped anyways |
| 41 | + if (shouldInfer && index === 0) { |
| 42 | + const result = fontFileReader.extract({ family: family.name, url: source.url }); |
| 43 | + if (variant.weight === undefined) data.weight = result.weight; |
| 44 | + if (variant.style === undefined) data.style = result.style; |
| 45 | + } |
| 46 | + |
| 47 | + return { |
| 48 | + originalURL: source.url, |
| 49 | + url: urlProxy.proxy({ |
| 50 | + url: source.url, |
| 51 | + // We only use the first source for preloading. For example if woff2 and woff |
| 52 | + // are available, we only keep woff2. |
| 53 | + collectPreload: index === 0, |
| 54 | + data: { |
| 55 | + weight: data.weight, |
| 56 | + style: data.style, |
| 57 | + }, |
| 58 | + init: null, |
| 59 | + }), |
| 60 | + format: FONT_FORMAT_MAP[fontTypeExtractor.extract(source.url)], |
| 61 | + tech: source.tech, |
| 62 | + }; |
| 63 | + }); |
| 64 | + return data; |
| 65 | + }), |
42 | 66 | }; |
43 | 67 | } |
0 commit comments