feat(getFontUrl): create a helper function to choose font url

This commit is contained in:
Ilia Mashkov
2026-02-05 11:40:23 +03:00
parent 0554fcada7
commit 8195e9baa8
3 changed files with 623 additions and 0 deletions

View File

@@ -0,0 +1,29 @@
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'];
}