30 lines
951 B
TypeScript
30 lines
951 B
TypeScript
|
|
import type {
|
||
|
|
FontWeight,
|
||
|
|
UnifiedFont,
|
||
|
|
} from '../../model';
|
||
|
|
|
||
|
|
const SIZES = [100, 200, 300, 400, 500, 600, 700, 800, 900];
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Constructs a URL for a font based on the provided font and weight.
|
||
|
|
* @param font - The font object.
|
||
|
|
* @param weight - The weight of the font.
|
||
|
|
* @returns The URL for the font.
|
||
|
|
*/
|
||
|
|
export function getFontUrl(font: UnifiedFont, weight: number): string | undefined {
|
||
|
|
if (!SIZES.includes(weight)) {
|
||
|
|
throw new Error(`Invalid weight: ${weight}`);
|
||
|
|
}
|
||
|
|
|
||
|
|
const weightKey = weight.toString() as FontWeight;
|
||
|
|
|
||
|
|
// 1. Try exact match (Backend now maps "100".."900" to VF URL if variable)
|
||
|
|
if (font.styles.variants?.[weightKey]) {
|
||
|
|
return font.styles.variants[weightKey];
|
||
|
|
}
|
||
|
|
|
||
|
|
// 2. Fallbacks for Static Fonts (if exact weight missing)
|
||
|
|
// Try 'regular' or '400' as safe defaults
|
||
|
|
return font.styles.regular || font.styles.variants?.['400'] || font.styles.variants?.['regular'];
|
||
|
|
}
|