auto-lms / src /server /render /slide.ts
akashdahad's picture
Deploy auto-lms (Next.js + worker + embedded Postgres)
6d6eaef verified
Raw History Blame Contribute Delete
3.12 kB
import { existsSync } from 'fs';
import { createCanvas, GlobalFonts, type SKRSContext2D } from '@napi-rs/canvas';
const WIDTH = 1280;
const HEIGHT = 720;
const MARGIN = 96;
// Register a system font once so text renders deterministically (the bundled
// ffmpeg's fontconfig is unreliable; we rasterise text ourselves instead).
const FONT_CANDIDATES = [
// macOS (local dev)
'/System/Library/Fonts/Supplemental/Arial.ttf',
'/Library/Fonts/Arial.ttf',
'/System/Library/Fonts/Supplemental/Verdana.ttf',
'/System/Library/Fonts/Helvetica.ttc',
// Linux (container) — fonts-dejavu-core / fonts-liberation
'/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf',
'/usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf',
];
let fontFamily = 'sans-serif';
let fontReady = false;
function ensureFont(): void {
if (fontReady) return;
for (const candidate of FONT_CANDIDATES) {
if (existsSync(candidate) && GlobalFonts.registerFromPath(candidate, 'SlideFont')) {
fontFamily = 'SlideFont';
break;
}
}
fontReady = true;
}
export interface SlideContent {
moduleTitle?: string;
title: string;
bullets: string[];
}
// Render a 1280×720 slide to a PNG buffer.
export function renderSlidePng(content: SlideContent): Buffer {
ensureFont();
const canvas = createCanvas(WIDTH, HEIGHT);
const ctx = canvas.getContext('2d');
// Background + accent bar.
ctx.fillStyle = '#0f172a';
ctx.fillRect(0, 0, WIDTH, HEIGHT);
ctx.fillStyle = '#6366f1';
ctx.fillRect(MARGIN, 110, 72, 8);
let y = 180;
if (content.moduleTitle) {
ctx.fillStyle = '#94a3b8';
ctx.font = `500 24px ${fontFamily}`;
ctx.fillText(truncate(content.moduleTitle, 60), MARGIN, y);
y += 56;
}
// Title (wrapped).
ctx.fillStyle = '#f8fafc';
ctx.font = `700 56px ${fontFamily}`;
y = drawWrapped(ctx, content.title, MARGIN, y + 24, WIDTH - MARGIN * 2, 66);
// Bullets (wrapped, with markers).
y += 36;
ctx.font = `400 34px ${fontFamily}`;
for (const bullet of content.bullets) {
if (y > HEIGHT - 80) break; // don't overflow the slide
ctx.fillStyle = '#6366f1';
ctx.fillText('•', MARGIN, y);
ctx.fillStyle = '#cbd5e1';
y = drawWrapped(ctx, bullet, MARGIN + 40, y, WIDTH - MARGIN * 2 - 40, 46) + 16;
}
return canvas.toBuffer('image/png');
}
// Draw word-wrapped text; returns the baseline y after the last line.
function drawWrapped(
ctx: SKRSContext2D,
text: string,
x: number,
startY: number,
maxWidth: number,
lineHeight: number,
): number {
const words = text.split(/\s+/).filter(Boolean);
let line = '';
let y = startY;
for (const word of words) {
const candidate = line ? `${line} ${word}` : word;
if (ctx.measureText(candidate).width > maxWidth && line) {
ctx.fillText(line, x, y);
line = word;
y += lineHeight;
} else {
line = candidate;
}
}
if (line) ctx.fillText(line, x, y);
return y + lineHeight; // next available baseline below the block
}
function truncate(text: string, max: number): string {
return text.length > max ? `${text.slice(0, max - 1)}…` : text;
}