|
1 | | -import { AstroError, AstroErrorData } from '../../core/errors/index.js'; |
2 | | -import type { ConsumableMap, PreloadData, PreloadFilter } from './types.js'; |
| 1 | +import * as fontsMod from 'virtual:astro:assets/fonts/internal'; |
| 2 | +import { createGetFontData } from './core/create-get-font-data.js'; |
3 | 3 |
|
4 | | -export function createGetFontData({ consumableMap }: { consumableMap?: ConsumableMap }) { |
5 | | - return function getFontData(cssVariable: string) { |
6 | | - // TODO: remove once fonts are stabilized |
7 | | - if (!consumableMap) { |
8 | | - throw new AstroError(AstroErrorData.ExperimentalFontsNotEnabled); |
9 | | - } |
10 | | - const data = consumableMap.get(cssVariable); |
11 | | - if (!data) { |
12 | | - throw new AstroError({ |
13 | | - ...AstroErrorData.FontFamilyNotFound, |
14 | | - message: AstroErrorData.FontFamilyNotFound.message(cssVariable), |
15 | | - }); |
16 | | - } |
17 | | - return data; |
18 | | - }; |
19 | | -} |
20 | | - |
21 | | -export function filterPreloads( |
22 | | - data: Array<PreloadData>, |
23 | | - preload: PreloadFilter, |
24 | | -): Array<PreloadData> | null { |
25 | | - if (!preload) { |
26 | | - return null; |
27 | | - } |
28 | | - if (preload === true) { |
29 | | - // Preload everything |
30 | | - return data; |
31 | | - } |
32 | | - // Only preload urls based on weight, style and subset |
33 | | - return data.filter(({ weight, style, subset }) => |
34 | | - preload.some((p) => { |
35 | | - // Always check the weight |
36 | | - if ( |
37 | | - p.weight !== undefined && |
38 | | - weight !== undefined && |
39 | | - !checkWeight(p.weight.toString(), weight) |
40 | | - ) { |
41 | | - return false; |
42 | | - } |
43 | | - // Only check the style if specified |
44 | | - if (p.style !== undefined && p.style !== style) { |
45 | | - return false; |
46 | | - } |
47 | | - // Only check the subset if specified |
48 | | - if (p.subset !== undefined && p.subset !== subset) { |
49 | | - return false; |
50 | | - } |
51 | | - return true; |
52 | | - }), |
53 | | - ); |
54 | | -} |
55 | | - |
56 | | -function checkWeight(input: string, target: string): boolean { |
57 | | - // If the input looks like "100 900", we check it as is |
58 | | - const trimmedInput = input.trim(); |
59 | | - if (trimmedInput.includes(' ')) { |
60 | | - return trimmedInput === target; |
61 | | - } |
62 | | - // If the target looks like "100 900", we check if the input is between the values |
63 | | - if (target.includes(' ')) { |
64 | | - const [a, b] = target.split(' '); |
65 | | - const parsedInput = Number.parseInt(input); |
66 | | - return parsedInput >= Number.parseInt(a) && parsedInput <= Number.parseInt(b); |
67 | | - } |
68 | | - return input === target; |
69 | | -} |
| 4 | +export const getFontData = createGetFontData(fontsMod); |
0 commit comments