@@ -39,6 +39,8 @@ const FX = new Set([
3939 'boot',
4040 ]);
4141
42+const CAPTION_SPEAKERS = new Set(['narrator', 'dev', 'clanker']);
43+
4244 const LAYOUTS = new Set([
4345 'auto',
4446 'one-panel',
@@ -129,7 +131,10 @@ function validateSpec(spec) {
129131 spec.panels.forEach((panel, index) => {
130132 const label = `Panel ${index + 1}`;
131133 if (!panel || typeof panel !== 'object') throw new Error(`${label} must be an object.`);
132- if (!panel.caption || typeof panel.caption !== 'string') throw new Error(`${label} requires caption.`);
134+ const hasCaption = typeof panel.caption === 'string' && panel.caption.length > 0;
135+ const hasDialogue = Array.isArray(panel.dialogue) && panel.dialogue.length > 0;
136+ if (!hasCaption && !hasDialogue) throw new Error(`${label} requires caption or dialogue.`);
137+ if (panel.caption !== undefined && typeof panel.caption !== 'string') throw new Error(`${label} caption must be a string.`);
133138 if (!DEV_POSES.has(panel.devPose)) throw new Error(`${label} has unknown devPose: ${panel.devPose}`);
134139 if (!CLANKER_POSES.has(panel.clankerPose)) throw new Error(`${label} has unknown clankerPose: ${panel.clankerPose}`);
135140 if (panel.fx !== undefined) {
@@ -138,6 +143,19 @@ function validateSpec(spec) {
138143 if (!FX.has(item)) throw new Error(`${label} has unknown fx: ${item}`);
139144 }
140145 }
146+ if (panel.captionSpeaker !== undefined && !CAPTION_SPEAKERS.has(panel.captionSpeaker)) {
147+ throw new Error(`${label} captionSpeaker must be narrator, dev, or clanker.`);
148+ }
149+ if (panel.dialogue !== undefined) {
150+ if (!Array.isArray(panel.dialogue)) throw new Error(`${label} dialogue must be an array.`);
151+ if (panel.dialogue.length > 3) throw new Error(`${label} dialogue supports at most 3 lines.`);
152+ for (const [dialogueIndex, item] of panel.dialogue.entries()) {
153+ const dialogueLabel = `${label} dialogue ${dialogueIndex + 1}`;
154+ if (!item || typeof item !== 'object') throw new Error(`${dialogueLabel} must be an object.`);
155+ if (!CAPTION_SPEAKERS.has(item.speaker)) throw new Error(`${dialogueLabel} speaker must be narrator, dev, or clanker.`);
156+ if (!item.text || typeof item.text !== 'string') throw new Error(`${dialogueLabel} requires text.`);
157+ }
158+ }
141159 if (panel.tokenCounter !== undefined && typeof panel.tokenCounter !== 'string') {
142160 throw new Error(`${label} tokenCounter must be a string.`);
143161 }
@@ -258,17 +276,71 @@ function renderTitle(title, width) {
258276 <line x1="24" y1="78" x2="${width - 24}" y2="78" class="terminal-line" />`;
259277 }
260278
261-function renderCaption(panel, box) {
262- const maxChars = Math.max(12, Math.floor((box.w - 44) / 17));
263- const lines = wrapText(panel.caption, maxChars).slice(0, 3);
264- const bold = /\*\*|__/.test(panel.caption);
279+function panelCaptions(panel) {
280+ if (Array.isArray(panel.dialogue) && panel.dialogue.length) {
281+ return panel.dialogue.map((item) => ({
282+ speaker: item.speaker || 'narrator',
283+ text: item.text,
284+ }));
285+ }
286+
287+ return [{
288+ speaker: panel.captionSpeaker || 'narrator',
289+ text: panel.caption,
290+ }];
291+}
292+
293+function renderCaptionBalloon(item, box, devX, botX, groundY, y) {
294+ const speaker = item.speaker || 'narrator';
295+ const text = item.text;
296+ const bold = /\*\*|__/.test(text);
265297 const fontSize = box.w > 800 ? 36 : box.w < 400 ? 28 : 32;
298+ const lineHeight = Math.round(fontSize * 0.9);
299+ const maxW = speaker === 'narrator'
300+ ? box.w - 24
301+ : Math.min(box.w - 44, Math.max(270, box.w * 0.58));
302+ const maxChars = Math.max(10, Math.floor((maxW - 34) / (fontSize * 0.47)));
303+ const lines = wrapText(text, maxChars).slice(0, 4);
304+ const balloonH = 30 + lines.length * lineHeight;
305+ const targetX = speaker === 'dev' ? devX : speaker === 'clanker' ? botX : null;
306+ const x = speaker === 'narrator'
307+ ? box.x + 12
308+ : Math.max(box.x + 12, Math.min(targetX - maxW / 2, box.x + box.w - maxW - 12));
309+ const textX = x + 16;
310+ const textY = y + 36;
311+
312+ if (speaker === 'narrator') {
313+ return {
314+ height: balloonH,
315+ svg: `
316+ ${rect(x, y, maxW, balloonH, 'rx="6" class="caption-bg narrator"')}
317+ <text x="${textX}" y="${textY}" class="caption ${bold ? 'caption-bold' : ''}" style="font-size:${fontSize}px">
318+ ${lines.map((lineText, i) => `<tspan x="${textX}" dy="${i === 0 ? 0 : lineHeight}">${escapeHtml(lineText)}</tspan>`).join('')}
319+ </text>`,
320+ };
321+ }
266322
267- return `
268- ${rect(box.x + 12, box.y + 12, box.w - 24, 34 + lines.length * 30, 'class="caption-bg"')}
269- <text x="${box.x + 28}" y="${box.y + 48}" class="caption ${bold ? 'caption-bold' : ''}" style="font-size:${fontSize}px">
270- ${lines.map((lineText, i) => `<tspan x="${box.x + 28}" dy="${i === 0 ? 0 : 30}">${escapeHtml(lineText)}</tspan>`).join('')}
271- </text>`;
323+ const tailBaseX = Math.max(x + 36, Math.min(targetX, x + maxW - 36));
324+ const tailTipY = Math.max(y + balloonH + 26, Math.min(groundY - 118, box.y + box.h - 72));
325+
326+ return {
327+ height: balloonH,
328+ svg: `
329+ <path d="M ${tailBaseX - 20} ${y + balloonH - 2} Q ${tailBaseX} ${y + balloonH + 22} ${targetX} ${tailTipY} Q ${tailBaseX + 14} ${y + balloonH + 18} ${tailBaseX + 20} ${y + balloonH - 2} Z" class="caption-tail" />
330+ ${rect(x, y, maxW, balloonH, 'rx="14" class="caption-bg speech"')}
331+ <text x="${textX}" y="${textY}" class="caption ${bold ? 'caption-bold' : ''}" style="font-size:${fontSize}px">
332+ ${lines.map((lineText, i) => `<tspan x="${textX}" dy="${i === 0 ? 0 : lineHeight}">${escapeHtml(lineText)}</tspan>`).join('')}
333+ </text>`,
334+ };
335+}
336+
337+function renderCaptions(panel, box, devX, botX, groundY) {
338+ let y = box.y + 12;
339+ return panelCaptions(panel).map((item) => {
340+ const rendered = renderCaptionBalloon(item, box, devX, botX, groundY, y);
341+ y += rendered.height + 10;
342+ return rendered.svg;
343+ }).join('\n');
272344 }
273345
274346 function renderDev(pose, x, footY, s) {
@@ -508,7 +580,7 @@ function renderPanel(panel, box, index) {
508580 ${renderTerminalLines(panel, box)}
509581 ${renderDev(panel.devPose, devX, groundY, s)}
510582 ${renderClanker(panel.clankerPose, botX, groundY, s)}
511- ${renderCaption(panel, box)}
583+ ${renderCaptions(panel, box, devX, botX, groundY)}
512584 ${renderTokenCounter(panel, box)}
513585 <text x="${box.x + box.w - 22}" y="${box.y + box.h - 16}" text-anchor="end" class="panel-num">${index + 1}</text>
514586 </g>`;
@@ -525,6 +597,8 @@ function renderSvg(spec) {
525597 .terminal-line, .ground { stroke: ${GREEN_DIM}; stroke-width: 3; }
526598 .panel-bg { fill: ${PANEL_BG}; stroke: ${GREEN}; stroke-width: 3; }
527599 .caption-bg { fill: rgba(0, 0, 0, 0.74); stroke: ${GREEN_DIM}; stroke-width: 2; }
600+ .caption-bg.speech, .caption-tail { filter: drop-shadow(0 0 5px rgba(34, 197, 94, 0.25)); }
601+ .caption-tail { fill: rgba(0, 0, 0, 0.74); stroke: ${GREEN_DIM}; stroke-width: 2; stroke-linejoin: round; }
528602 .caption { fill: ${GREEN_BRIGHT}; font-family: 'VT323', 'Courier New', monospace; }
529603 .caption-bold { font-weight: 700; letter-spacing: 1px; }
530604 .panel-num, .boot-text { fill: ${GREEN_DIM}; font: 22px 'VT323', 'Courier New', monospace; }