|
| 1 | +import { |
| 2 | + fontFamilyStackForEntry, |
| 3 | + fontTokenCssVariable, |
| 4 | + parseVariant, |
| 5 | + sortFontTokens, |
| 6 | + type FontEntry, |
| 7 | + type FontToken, |
| 8 | + type SiteFontsSettings, |
| 9 | +} from '@core/fonts' |
| 10 | + |
| 11 | +export function getFontWeightOptions( |
| 12 | + fontFamilyValue: unknown, |
| 13 | + fonts: SiteFontsSettings | null | undefined, |
| 14 | + fallbackOptions: readonly string[], |
| 15 | +): string[] { |
| 16 | + const entry = resolveFontEntryForWeightOptions(fontFamilyValue, fonts) |
| 17 | + const installedWeights = entry ? weightOptionsForEntry(entry) : [] |
| 18 | + return installedWeights.length > 0 ? installedWeights : [...fallbackOptions] |
| 19 | +} |
| 20 | + |
| 21 | +function resolveFontEntryForWeightOptions( |
| 22 | + fontFamilyValue: unknown, |
| 23 | + fonts: SiteFontsSettings | null | undefined, |
| 24 | +): FontEntry | undefined { |
| 25 | + const items = fontItems(fonts) |
| 26 | + if (items.length === 0) return undefined |
| 27 | + |
| 28 | + const explicitFamily = typeof fontFamilyValue === 'string' ? fontFamilyValue.trim() : '' |
| 29 | + if (explicitFamily) { |
| 30 | + const entry = resolveExplicitFontEntry(explicitFamily, fonts, items) |
| 31 | + if (entry || !isUnsetFontFamilyValue(explicitFamily)) return entry |
| 32 | + } |
| 33 | + |
| 34 | + return resolveDefaultBodyFontEntry(fonts, items) |
| 35 | +} |
| 36 | + |
| 37 | +function resolveExplicitFontEntry( |
| 38 | + fontFamilyValue: string, |
| 39 | + fonts: SiteFontsSettings | null | undefined, |
| 40 | + items: readonly FontEntry[], |
| 41 | +): FontEntry | undefined { |
| 42 | + const tokenVariable = readCssVariableReference(fontFamilyValue) |
| 43 | + if (tokenVariable) { |
| 44 | + const token = fontTokens(fonts).find( |
| 45 | + (candidate) => fontTokenCssVariable(candidate.variable) === tokenVariable, |
| 46 | + ) |
| 47 | + return token?.familyId ? items.find((entry) => entry.id === token.familyId) : undefined |
| 48 | + } |
| 49 | + |
| 50 | + const normalizedStack = normalizeCssFamilyStack(fontFamilyValue) |
| 51 | + const exactStackMatch = items.find( |
| 52 | + (entry) => normalizeCssFamilyStack(fontFamilyStackForEntry(entry)) === normalizedStack, |
| 53 | + ) |
| 54 | + if (exactStackMatch) return exactStackMatch |
| 55 | + |
| 56 | + const firstFamily = normalizeCssFamilyName(readFirstCssFamily(fontFamilyValue)) |
| 57 | + if (!firstFamily) return undefined |
| 58 | + return items.find((entry) => normalizeCssFamilyName(entry.family) === firstFamily) |
| 59 | +} |
| 60 | + |
| 61 | +function resolveDefaultBodyFontEntry( |
| 62 | + fonts: SiteFontsSettings | null | undefined, |
| 63 | + items: readonly FontEntry[], |
| 64 | +): FontEntry | undefined { |
| 65 | + const tokens = fontTokens(fonts) |
| 66 | + const bodyToken = tokens[1] ?? tokens[0] |
| 67 | + if (bodyToken?.familyId) { |
| 68 | + const entry = items.find((item) => item.id === bodyToken.familyId) |
| 69 | + if (entry) return entry |
| 70 | + } |
| 71 | + return items[1] ?? items[0] |
| 72 | +} |
| 73 | + |
| 74 | +function weightOptionsForEntry(entry: FontEntry): string[] { |
| 75 | + const weights = new Set<number>() |
| 76 | + const variants = [ |
| 77 | + ...entry.variants, |
| 78 | + ...entry.files.map((file) => file.variant), |
| 79 | + ] |
| 80 | + for (const variant of variants) { |
| 81 | + const parsed = parseVariant(variant) |
| 82 | + if (parsed) weights.add(parsed.weight) |
| 83 | + } |
| 84 | + return [...weights].sort((a, b) => a - b).map(String) |
| 85 | +} |
| 86 | + |
| 87 | +function fontItems(fonts: SiteFontsSettings | null | undefined): readonly FontEntry[] { |
| 88 | + return Array.isArray(fonts?.items) ? fonts.items : [] |
| 89 | +} |
| 90 | + |
| 91 | +function fontTokens(fonts: SiteFontsSettings | null | undefined): FontToken[] { |
| 92 | + return Array.isArray(fonts?.tokens) ? sortFontTokens(fonts.tokens) : [] |
| 93 | +} |
| 94 | + |
| 95 | +function readCssVariableReference(value: string): string | undefined { |
| 96 | + const match = /^var\(\s*(--[a-zA-Z0-9_-]+)\s*(?:,[^)]+)?\)$/.exec(value) |
| 97 | + return match?.[1] |
| 98 | +} |
| 99 | + |
| 100 | +function isUnsetFontFamilyValue(value: string): boolean { |
| 101 | + const normalized = value.toLowerCase() |
| 102 | + return normalized === 'inherit' || normalized === 'initial' || normalized === 'unset' || normalized === 'revert' |
| 103 | +} |
| 104 | + |
| 105 | +function readFirstCssFamily(value: string): string { |
| 106 | + const trimmed = value.trim() |
| 107 | + if (!trimmed) return '' |
| 108 | + const quote = trimmed[0] |
| 109 | + if (quote === '"' || quote === "'") { |
| 110 | + for (let index = 1; index < trimmed.length; index += 1) { |
| 111 | + if (trimmed[index] === quote && trimmed[index - 1] !== '\\') { |
| 112 | + return trimmed.slice(1, index) |
| 113 | + } |
| 114 | + } |
| 115 | + return trimmed.slice(1) |
| 116 | + } |
| 117 | + const comma = trimmed.indexOf(',') |
| 118 | + return (comma === -1 ? trimmed : trimmed.slice(0, comma)).trim() |
| 119 | +} |
| 120 | + |
| 121 | +function normalizeCssFamilyStack(value: string): string { |
| 122 | + return value.trim().replace(/\s+/g, ' ').toLowerCase() |
| 123 | +} |
| 124 | + |
| 125 | +function normalizeCssFamilyName(value: string): string { |
| 126 | + return value.trim().replace(/^['"]|['"]$/g, '').toLowerCase() |
| 127 | +} |
0 commit comments