Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 0 additions & 14 deletions lib/svg/constants.ts

This file was deleted.

15 changes: 10 additions & 5 deletions lib/svg/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@ import { TOWER_ANIMATION_CSS } from './animations';
import { computeTowers, type TowerData } from './layout';
import { sanitizeFont, sanitizeHexColor, sanitizeRadius, sanitizeGoogleFontUrl } from './sanitizer';

import { SVG_WIDTH, SVG_HEIGHT, FONT_MAP } from './constants';
import { SVG_WIDTH, SVG_HEIGHT, FONT_MAP, isFontKey } from './generatorConstants';

// helpers
function truncateUsername(name: string, max = 20): string {
return name.length > max ? name.slice(0, max) + '…' : name;
}

function getFontFromMap(font: string | null): string | null {
if (!font) return null;
const f = font.toLowerCase();
return isFontKey(f) ? FONT_MAP[f] : null;
}
function getSizeScale(size?: 'small' | 'medium' | 'large'): number {
if (size === 'small') return 400 / SVG_WIDTH;
if (size === 'large') return 800 / SVG_WIDTH;
Expand Down Expand Up @@ -256,7 +261,7 @@ export function generateSVG(
const text = `#${sanitizeHexColor(params.text, 'ffffff')}`;

const sanitizedFont = sanitizeFont(params.font);
const predefinedFont = sanitizedFont ? FONT_MAP[sanitizedFont.toLowerCase()] : null;
const predefinedFont = getFontFromMap(sanitizedFont);
const isPredefinedFont = Boolean(predefinedFont);
const selectedFont = isPredefinedFont
? predefinedFont
Expand Down Expand Up @@ -300,7 +305,7 @@ function generateAutoThemeSVG(
const safeUser = escapeXML(params.user || 'GitHub User');
const sanitizedFont = sanitizeFont(params.font);
const selectedFont = sanitizedFont
? FONT_MAP[sanitizedFont.toLowerCase()] || `"${sanitizedFont}", sans-serif`
? getFontFromMap(sanitizedFont) || `"${sanitizedFont}", sans-serif`
: null;
const statsFont = selectedFont || '"Space Grotesk", sans-serif';
const sf = getSizeScale(params.size);
Expand Down Expand Up @@ -402,7 +407,7 @@ export function generateMonthlySVG(stats: MonthlyStats, params: BadgeParams): st

const sanitizeFont = (name: string) => name.replace(/[^a-zA-Z0-9\s-]/g, '').trim();
const sanitizedFont = params.font ? sanitizeFont(params.font) : null;
const predefinedFont = sanitizedFont ? FONT_MAP[sanitizedFont.toLowerCase()] : null;
const predefinedFont = getFontFromMap(sanitizedFont);
const isPredefinedFont = Boolean(predefinedFont);
const selectedFont = isPredefinedFont
? predefinedFont
Expand Down Expand Up @@ -489,7 +494,7 @@ function generateAutoThemeMonthlySVG(stats: MonthlyStats, params: BadgeParams):
const safeUser = escapeXML(params.user || 'GitHub User');
const sanitizeFont = (name: string) => name.replace(/[^a-zA-Z0-9\s-]/g, '').trim();
const sanitizedFont = params.font ? sanitizeFont(params.font) : null;
const predefinedFont = sanitizedFont ? FONT_MAP[sanitizedFont.toLowerCase()] : null;
const predefinedFont = getFontFromMap(sanitizedFont);
const isPredefinedFont = Boolean(predefinedFont);
const selectedFont = isPredefinedFont
? predefinedFont
Expand Down
14 changes: 14 additions & 0 deletions lib/svg/generatorConstants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export const SVG_WIDTH = 600;
export const SVG_HEIGHT = 420;

export const FONT_MAP = {
jetbrains: '"JetBrains Mono", monospace',
fira: '"Fira Code", monospace',
roboto: '"Roboto", sans-serif',
} as const;

export type FontKey = keyof typeof FONT_MAP;

export function isFontKey(font: string): font is FontKey {
return font in FONT_MAP;
}
2 changes: 1 addition & 1 deletion lib/svg/layout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
LINEAR_SCALE_MULTIPLIER,
MAX_LOG_HEIGHT,
MAX_LINEAR_HEIGHT,
} from './constants';
} from './layoutConstants';

/** Shared layout data for a single isometric tower. */
export interface FaceOpacity {
Expand Down
5 changes: 5 additions & 0 deletions lib/svg/layoutConstants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const GHOST_HEIGHT_PX = 4;
export const LOG_SCALE_MULTIPLIER = 12;
export const LINEAR_SCALE_MULTIPLIER = 5;
export const MAX_LOG_HEIGHT = 80;
export const MAX_LINEAR_HEIGHT = 50;
Comment on lines +1 to +5
Loading