@@ -15,7 +15,10 @@ import {
1515 getGradientCoordinates ,
1616} from './sanitizer' ;
1717
18- import { SVG_WIDTH , SVG_HEIGHT , FONT_MAP } from './generatorConstants' ;
18+ import { SVG_WIDTH , SVG_HEIGHT } from './generatorConstants' ;
19+ import { FONT_MAP , resolveFont } from './fonts' ;
20+
21+ export { FONT_MAP , resolveFont } from './fonts' ;
1922
2023// helpers
2124export function getSizeScale ( size ?: 'small' | 'medium' | 'large' ) {
@@ -632,15 +635,10 @@ export function generateSVG(
632635 const borderAttr = params . border ? `stroke="#${ params . border } " stroke-width="2"` : '' ;
633636
634637 const sanitizedFont = sanitizeFont ( params . font ) ;
635- const predefinedFont = sanitizedFont
636- ? ( FONT_MAP [ sanitizedFont . toLowerCase ( ) as keyof typeof FONT_MAP ] ?? null )
637- : null ;
638- const isPredefinedFont = Boolean ( predefinedFont ) ;
639- const selectedFont = isPredefinedFont
640- ? predefinedFont
641- : sanitizedFont
642- ? `"${ sanitizedFont } ", sans-serif`
643- : null ;
638+ const selectedFont = resolveFont ( sanitizedFont ) ;
639+ const isPredefinedFont = sanitizedFont
640+ ? Boolean ( FONT_MAP [ sanitizedFont . toLowerCase ( ) as keyof typeof FONT_MAP ] )
641+ : false ;
644642 const statsFont = selectedFont || '"Space Grotesk", sans-serif' ;
645643 const googleFontUrlPart =
646644 sanitizedFont && ! isPredefinedFont ? sanitizeGoogleFontUrl ( sanitizedFont ) : null ;
@@ -704,10 +702,7 @@ function generateAutoThemeSVG(
704702 const darkLabelOpacity = getLuminance ( dark . bg ) > 0.5 ? '0.8' : '0.7' ;
705703 const safeUser = escapeXML ( params . user || 'GitHub User' ) ;
706704 const sanitizedFont = sanitizeFont ( params . font ) ;
707- const selectedFont = sanitizedFont
708- ? ( FONT_MAP [ sanitizedFont . toLowerCase ( ) as keyof typeof FONT_MAP ] ?? null ) ||
709- `"${ sanitizedFont } ", sans-serif`
710- : null ;
705+ const selectedFont = resolveFont ( sanitizedFont ) ;
711706 const statsFont = selectedFont || '"Space Grotesk", sans-serif' ;
712707 const googleFontUrlPart = sanitizedFont ? sanitizeGoogleFontUrl ( sanitizedFont ) : null ;
713708 const googleFontsImport = googleFontUrlPart
@@ -807,16 +802,10 @@ export function generateMonthlySVG(stats: MonthlyStats, params: BadgeParams): st
807802 const text = `#${ sanitizeHexColor ( params . text , 'ffffff' ) } ` ;
808803
809804 const sanitizedFont = sanitizeFont ( params . font ) ;
810- const predefinedFont = sanitizedFont
811- ? ( FONT_MAP [ sanitizedFont . toLowerCase ( ) as keyof typeof FONT_MAP ] ?? null )
812- : null ;
813- const isPredefinedFont = Boolean ( predefinedFont ) ;
814- const selectedFont = isPredefinedFont
815- ? predefinedFont
816- : sanitizedFont
817- ? `"${ sanitizedFont } ", sans-serif`
818- : null ;
819-
805+ const selectedFont = resolveFont ( sanitizedFont ) ;
806+ const isPredefinedFont = sanitizedFont
807+ ? Boolean ( FONT_MAP [ sanitizedFont . toLowerCase ( ) as keyof typeof FONT_MAP ] )
808+ : false ;
820809 const statsFont = selectedFont || '"Space Grotesk", sans-serif' ;
821810 const radius = sanitizeRadius ( params . radius , 8 ) ;
822811 const labels = getLabels ( params . lang ) ;
@@ -936,16 +925,10 @@ export function generateWrappedSVG(
936925 const text = `#${ sanitizeHexColor ( params . text , 'ffffff' ) } ` ;
937926
938927 const sanitizedFont = sanitizeFont ( params . font ) ;
939- const predefinedFont = sanitizedFont
940- ? ( FONT_MAP [ sanitizedFont . toLowerCase ( ) as keyof typeof FONT_MAP ] ?? null )
941- : null ;
942- const isPredefinedFont = Boolean ( predefinedFont ) ;
943- const selectedFont = isPredefinedFont
944- ? predefinedFont
945- : sanitizedFont
946- ? `"${ sanitizedFont } ", sans-serif`
947- : null ;
948-
928+ const selectedFont = resolveFont ( sanitizedFont ) ;
929+ const isPredefinedFont = sanitizedFont
930+ ? Boolean ( FONT_MAP [ sanitizedFont . toLowerCase ( ) as keyof typeof FONT_MAP ] )
931+ : false ;
949932 const statsFont = selectedFont || '"Space Grotesk", sans-serif' ;
950933 const radius = sanitizeRadius ( params . radius , 8 ) ;
951934
@@ -1195,10 +1178,7 @@ function generateAutoThemeMonthlySVG(stats: MonthlyStats, params: BadgeParams):
11951178 const dark = AUTO_THEME_DARK ;
11961179 const safeUser = escapeXML ( params . user || 'GitHub User' ) ;
11971180 const sanitizedFont = sanitizeFont ( params . font ) ;
1198- const selectedFont = sanitizedFont
1199- ? ( FONT_MAP [ sanitizedFont . toLowerCase ( ) as keyof typeof FONT_MAP ] ?? null ) ||
1200- `"${ sanitizedFont } ", sans-serif`
1201- : null ;
1181+ const selectedFont = resolveFont ( sanitizedFont ) ;
12021182
12031183 const statsFont = selectedFont || '"Space Grotesk", sans-serif' ;
12041184 const googleFontUrlPart = sanitizedFont ? sanitizeGoogleFontUrl ( sanitizedFont ) : null ;
@@ -1482,12 +1462,10 @@ export function generateHeatmapSVG(
14821462 const predefinedFont = sanitizedFont
14831463 ? ( FONT_MAP [ sanitizedFont . toLowerCase ( ) as keyof typeof FONT_MAP ] ?? null )
14841464 : null ;
1485- const isPredefinedFont = Boolean ( predefinedFont ) ;
1486- const selectedFont = isPredefinedFont
1487- ? predefinedFont
1488- : sanitizedFont
1489- ? `"${ sanitizedFont } ", sans-serif`
1490- : null ;
1465+ const selectedFont = resolveFont ( sanitizedFont ) ;
1466+ const isPredefinedFont = sanitizedFont
1467+ ? Boolean ( FONT_MAP [ sanitizedFont . toLowerCase ( ) as keyof typeof FONT_MAP ] )
1468+ : false ;
14911469 const statsFont = selectedFont || '"Space Grotesk", sans-serif' ;
14921470 const googleFontUrlPart =
14931471 sanitizedFont && ! isPredefinedFont ? sanitizeGoogleFontUrl ( sanitizedFont ) : null ;
@@ -1619,10 +1597,7 @@ function generateAutoThemeHeatmapSVG(
16191597 const safeUser = escapeXML ( params . user || 'GitHub User' ) ;
16201598
16211599 const sanitizedFont = sanitizeFont ( params . font ) ;
1622- const selectedFont = sanitizedFont
1623- ? ( FONT_MAP [ sanitizedFont . toLowerCase ( ) as keyof typeof FONT_MAP ] ?? null ) ||
1624- `"${ sanitizedFont } ", sans-serif`
1625- : null ;
1600+ const selectedFont = resolveFont ( sanitizedFont ) ;
16261601 const statsFont = selectedFont || '"Space Grotesk", sans-serif' ;
16271602 const googleFontUrlPart = sanitizedFont ? sanitizeGoogleFontUrl ( sanitizedFont ) : null ;
16281603 const googleFontsImport = googleFontUrlPart
@@ -1941,15 +1916,10 @@ export function generateVersusSVG(
19411916 const text = `#${ sanitizeHexColor ( params . text , 'ffffff' ) } ` ;
19421917
19431918 const sanitizedFont = sanitizeFont ( params . font ) ;
1944- const predefinedFont = sanitizedFont
1945- ? ( FONT_MAP [ sanitizedFont . toLowerCase ( ) as keyof typeof FONT_MAP ] ?? null )
1946- : null ;
1947- const isPredefinedFont = Boolean ( predefinedFont ) ;
1948- const selectedFont = isPredefinedFont
1949- ? predefinedFont
1950- : sanitizedFont
1951- ? `"${ sanitizedFont } ", sans-serif`
1952- : null ;
1919+ const selectedFont = resolveFont ( sanitizedFont ) ;
1920+ const isPredefinedFont = sanitizedFont
1921+ ? Boolean ( FONT_MAP [ sanitizedFont . toLowerCase ( ) as keyof typeof FONT_MAP ] )
1922+ : false ;
19531923 const statsFont = selectedFont || '"Space Grotesk", sans-serif' ;
19541924 const googleFontUrlPart =
19551925 sanitizedFont && ! isPredefinedFont ? sanitizeGoogleFontUrl ( sanitizedFont ) : null ;
@@ -2027,10 +1997,7 @@ function generateAutoThemeVersusSVG(
20271997 const safeUser1 = escapeXML ( params . user || 'User 1' ) ;
20281998 const safeUser2 = escapeXML ( params . versus || 'User 2' ) ;
20291999 const sanitizedFont = sanitizeFont ( params . font ) ;
2030- const selectedFont = sanitizedFont
2031- ? ( FONT_MAP [ sanitizedFont . toLowerCase ( ) as keyof typeof FONT_MAP ] ?? null ) ||
2032- `"${ sanitizedFont } ", sans-serif`
2033- : null ;
2000+ const selectedFont = resolveFont ( sanitizedFont ) ;
20342001 const statsFont = selectedFont || '"Space Grotesk", sans-serif' ;
20352002 const sf = getSizeScale ( params . size ) ;
20362003 const radius = sanitizeRadius ( params . radius , 8 ) * sf ;
0 commit comments