File size: 3,124 Bytes
6d6eaef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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;
}