@appeeky/open-slideshow 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +297 -2
- package/dist/bin.d.ts +2 -0
- package/dist/bin.js +9 -0
- package/dist/brand.d.ts +57 -0
- package/dist/brand.js +263 -0
- package/dist/brandkit.d.ts +50 -0
- package/dist/brandkit.js +212 -0
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +355 -0
- package/dist/decks.d.ts +44 -0
- package/dist/decks.js +100 -0
- package/dist/fonts.d.ts +45 -0
- package/dist/fonts.js +243 -0
- package/dist/gallery.d.ts +35 -0
- package/dist/gallery.js +109 -0
- package/dist/ideas.d.ts +50 -0
- package/dist/ideas.js +146 -0
- package/dist/images.d.ts +62 -0
- package/dist/images.js +241 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.js +22 -0
- package/dist/lint.d.ts +36 -0
- package/dist/lint.js +197 -0
- package/dist/mcp.d.ts +3 -0
- package/dist/mcp.js +303 -0
- package/dist/preview.d.ts +18 -0
- package/dist/preview.js +96 -0
- package/dist/publish.d.ts +105 -0
- package/dist/publish.js +406 -0
- package/dist/readability.d.ts +19 -0
- package/dist/readability.js +42 -0
- package/dist/render.d.ts +92 -0
- package/dist/render.js +647 -0
- package/dist/results.d.ts +89 -0
- package/dist/results.js +252 -0
- package/dist/schedule.d.ts +19 -0
- package/dist/schedule.js +114 -0
- package/dist/schema.d.ts +1159 -0
- package/dist/schema.js +211 -0
- package/dist/screenshots.d.ts +18 -0
- package/dist/screenshots.js +69 -0
- package/dist/shape.d.ts +31 -0
- package/dist/shape.js +169 -0
- package/dist/skill.d.ts +7 -0
- package/dist/skill.js +17 -0
- package/dist/templates.d.ts +47 -0
- package/dist/templates.js +330 -0
- package/dist/text.d.ts +40 -0
- package/dist/text.js +231 -0
- package/dist/themes.d.ts +43 -0
- package/dist/themes.js +202 -0
- package/dist/variants.d.ts +48 -0
- package/dist/variants.js +175 -0
- package/dist/version.d.ts +1 -0
- package/dist/version.js +1 -0
- package/dist/video.d.ts +22 -0
- package/dist/video.js +59 -0
- package/dist/viral.d.ts +12 -0
- package/dist/viral.js +35 -0
- package/dist/workspace.d.ts +85 -0
- package/dist/workspace.js +322 -0
- package/package.json +65 -4
- package/skills/open-slideshow/SKILL.md +65 -0
- package/skills/open-slideshow/references/brand.md +64 -0
- package/skills/open-slideshow/references/channels.md +63 -0
- package/skills/open-slideshow/references/ideas.md +23 -0
- package/skills/open-slideshow/references/images.md +61 -0
- package/skills/open-slideshow/references/languages.md +51 -0
- package/skills/open-slideshow/references/publishing.md +70 -0
- package/skills/open-slideshow/references/results.md +47 -0
- package/skills/open-slideshow/references/review.md +81 -0
- package/skills/open-slideshow/references/screenshots.md +26 -0
- package/skills/open-slideshow/references/series.md +20 -0
- package/skills/open-slideshow/references/templates.md +96 -0
- package/skills/open-slideshow/references/troubleshooting.md +92 -0
- package/skills/open-slideshow/references/video.md +18 -0
- package/skills/open-slideshow/references/writing.md +76 -0
package/dist/render.js
ADDED
|
@@ -0,0 +1,647 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { mkdirSync, readFileSync, writeFileSync, existsSync, readdirSync, unlinkSync } from "node:fs";
|
|
3
|
+
import { homedir } from "node:os";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import satori from "satori";
|
|
6
|
+
import { Resvg } from "@resvg/resvg-js";
|
|
7
|
+
import sharp from "sharp";
|
|
8
|
+
import { satoriFonts, missingChars, glyphReport, fontStack, ensureScriptFonts, SCRIPTS, pickFont, measure } from "./fonts.js";
|
|
9
|
+
import { COMPLEX, shaperReady, shapeLine } from "./shape.js";
|
|
10
|
+
import { fit, plainText, upperCase, isRtl } from "./text.js";
|
|
11
|
+
import { SIZES } from "./schema.js";
|
|
12
|
+
import { getTheme, withColors, readableOn, mix, luminance } from "./themes.js";
|
|
13
|
+
export const RENDERER_VERSION = "1";
|
|
14
|
+
const el = (type, style, children, extra = {}) => ({
|
|
15
|
+
type,
|
|
16
|
+
props: { style, ...(children === undefined ? {} : { children }), ...extra },
|
|
17
|
+
});
|
|
18
|
+
const div = (style, children) => el("div", { display: "flex", ...style }, children);
|
|
19
|
+
export function frameFor(size) {
|
|
20
|
+
const { width: W, height: H } = SIZES[size];
|
|
21
|
+
// Where TikTok and Instagram draw their own buttons and captions.
|
|
22
|
+
const top = size === "9:16" ? Math.round(H * 0.11) : Math.round(H * 0.07);
|
|
23
|
+
const bottom = size === "9:16" ? Math.round(H * 0.25) : size === "4:5" ? Math.round(H * 0.12) : Math.round(H * 0.09);
|
|
24
|
+
return { W, H, pad: 84, top, bottom };
|
|
25
|
+
}
|
|
26
|
+
function textStyleFor(style, size) {
|
|
27
|
+
if (style === "outline") {
|
|
28
|
+
// A stroke outside the letters, as a ring of hard shadows: a real stroke eats into thin letters.
|
|
29
|
+
const r = Math.max(2, Math.round(size * 0.05));
|
|
30
|
+
const rings = [];
|
|
31
|
+
for (let i = 0; i < 16; i++) {
|
|
32
|
+
const a = (i / 16) * Math.PI * 2;
|
|
33
|
+
rings.push(`${(Math.cos(a) * r).toFixed(1)}px ${(Math.sin(a) * r).toFixed(1)}px 0 #000000`);
|
|
34
|
+
}
|
|
35
|
+
return { textShadow: rings.join(", ") };
|
|
36
|
+
}
|
|
37
|
+
if (style === "shadow")
|
|
38
|
+
return { textShadow: `0 ${Math.round(size * 0.04)}px ${Math.round(size * 0.2)}px rgba(0,0,0,0.6)` };
|
|
39
|
+
return {};
|
|
40
|
+
}
|
|
41
|
+
/** A shaped line (Arabic, Hebrew, Thai, Devanagari) as an SVG image, with the same text style and accent look as other lines. */
|
|
42
|
+
function shapedLineEl(line, b, theme, style, align) {
|
|
43
|
+
const size = b.fitted.size;
|
|
44
|
+
const lineH = Math.round(size * b.line_height);
|
|
45
|
+
const shaped = shapeLine(b.role, line, size, b.locale);
|
|
46
|
+
const primary = pickFont(b.role, "a", b.locale);
|
|
47
|
+
const em = primary?.unitsPerEm ?? 1000;
|
|
48
|
+
const asc = ((primary?.ascender ?? 900) / em) * size;
|
|
49
|
+
const desc = ((primary?.descender ?? -250) / em) * size;
|
|
50
|
+
const baseline = (lineH - (asc - desc)) / 2 + asc;
|
|
51
|
+
const ring = style === "outline" ? Math.max(2, Math.round(size * 0.05)) : 0;
|
|
52
|
+
const boxPad = theme.accent_style === "box" ? Math.round(size * 0.12) : 0;
|
|
53
|
+
const pad = Math.max(ring, boxPad) + (style === "shadow" ? Math.round(size * 0.3) : 0) + 2;
|
|
54
|
+
// Arabic dots and marks reach well above and below the Latin line box: leave room so they are not cut.
|
|
55
|
+
const padY = Math.max(pad, Math.round(size * 0.5));
|
|
56
|
+
const w = Math.ceil(shaped.width) + pad * 2;
|
|
57
|
+
const h = lineH + padY * 2;
|
|
58
|
+
const accent = theme.colors.accent;
|
|
59
|
+
// Glyphs are drawn in font units and scaled down: a stroke width has to be given in font units too.
|
|
60
|
+
const glyph = (g, stroke = 0) => `<path transform="translate(${(g.x + pad).toFixed(2)} ${(baseline + padY + g.dy).toFixed(2)}) scale(${g.scale.toFixed(5)} ${(-g.scale).toFixed(5)})"${stroke ? ` stroke-width="${(stroke / g.scale).toFixed(1)}"` : ""} d="${g.d}"/>`;
|
|
61
|
+
const fillFor = (isAccent) => isAccent ? (theme.accent_style === "color" ? accent : theme.accent_style === "box" ? readableOn(accent) : b.color) : b.color;
|
|
62
|
+
const parts = [];
|
|
63
|
+
if (style === "shadow")
|
|
64
|
+
parts.push(`<defs><filter id="s" x="-20%" y="-50%" width="140%" height="200%"><feDropShadow dx="0" dy="${Math.round(size * 0.04)}" stdDeviation="${Math.round(size * 0.1)}" flood-color="#000" flood-opacity="0.6"/></filter></defs>`);
|
|
65
|
+
const boxes = theme.accent_style === "box"
|
|
66
|
+
? shaped.accentSpans.map((sp) => `<rect x="${sp.x0 + pad - boxPad}" y="${padY}" width="${sp.x1 - sp.x0 + boxPad * 2}" height="${lineH}" rx="${Math.round(size * 0.12)}" fill="${accent}"/>`)
|
|
67
|
+
: theme.accent_style === "underline"
|
|
68
|
+
? shaped.accentSpans.map((sp) => `<rect x="${sp.x0 + pad}" y="${baseline + padY + size * 0.1}" width="${sp.x1 - sp.x0}" height="${Math.max(3, Math.round(size * 0.08))}" fill="${accent}"/>`)
|
|
69
|
+
: [];
|
|
70
|
+
if (ring) {
|
|
71
|
+
const outlined = shaped.glyphs.filter((g) => !(g.accent && theme.accent_style === "box"));
|
|
72
|
+
parts.push(`<g fill="#000" stroke="#000" stroke-linejoin="round">${outlined.map((g) => glyph(g, ring * 2)).join("")}</g>`);
|
|
73
|
+
}
|
|
74
|
+
parts.push(...boxes);
|
|
75
|
+
for (const isAccent of [false, true]) {
|
|
76
|
+
const gs = shaped.glyphs.filter((g) => g.accent === isAccent);
|
|
77
|
+
if (gs.length)
|
|
78
|
+
parts.push(`<g fill="${fillFor(isAccent)}">${gs.map((g) => glyph(g)).join("")}</g>`);
|
|
79
|
+
}
|
|
80
|
+
const body = style === "shadow" ? `${parts[0]}<g filter="url(#s)">${parts.slice(1).join("")}</g>` : parts.join("");
|
|
81
|
+
const svg = `<svg xmlns="http://www.w3.org/2000/svg" width="${w}" height="${h}" viewBox="0 0 ${w} ${h}">${body}</svg>`;
|
|
82
|
+
const image = el("img", { width: w, height: h, margin: `${-padY}px ${-pad}px` }, undefined, { src: `data:image/svg+xml;base64,${Buffer.from(svg).toString("base64")}`, width: w, height: h });
|
|
83
|
+
const justify = align === "center" ? "center" : shaped.rtl ? "flex-end" : "flex-start";
|
|
84
|
+
const row = { justifyContent: justify, alignSelf: "stretch" };
|
|
85
|
+
if (style === "box")
|
|
86
|
+
return div(row, [div({ backgroundColor: theme.colors.card, padding: `0 ${Math.round(size * 0.3)}px`, borderRadius: Math.round(size * 0.2) }, [image])]);
|
|
87
|
+
return div({ ...row, height: lineH, alignItems: "center" }, [image]);
|
|
88
|
+
}
|
|
89
|
+
function lineEl(line, b, theme, style, align) {
|
|
90
|
+
if (shaperReady() && COMPLEX.test(line.map((r) => r.text).join("")))
|
|
91
|
+
return shapedLineEl(line, b, theme, style, align);
|
|
92
|
+
const size = b.fitted.size;
|
|
93
|
+
const accent = theme.colors.accent;
|
|
94
|
+
const rtl = isRtl(line.map((r) => r.text).join(""));
|
|
95
|
+
const runs = line.map((r) => {
|
|
96
|
+
const s = { whiteSpace: "pre", color: b.color };
|
|
97
|
+
if (r.accent) {
|
|
98
|
+
if (theme.accent_style === "color")
|
|
99
|
+
s.color = accent;
|
|
100
|
+
else if (theme.accent_style === "underline") {
|
|
101
|
+
s.borderBottom = `${Math.max(3, Math.round(size * 0.08))}px solid ${accent}`;
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
s.backgroundColor = accent;
|
|
105
|
+
s.color = readableOn(accent);
|
|
106
|
+
s.padding = `0 ${Math.round(size * 0.12)}px`;
|
|
107
|
+
s.borderRadius = Math.round(size * 0.12);
|
|
108
|
+
if (style === "outline")
|
|
109
|
+
s.textShadow = "none";
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return div(s, r.text);
|
|
113
|
+
});
|
|
114
|
+
const base = {
|
|
115
|
+
fontFamily: fontStack(b.role.family, b.locale),
|
|
116
|
+
fontWeight: b.role.weight,
|
|
117
|
+
fontStyle: b.role.italic ? "italic" : "normal",
|
|
118
|
+
fontSize: size,
|
|
119
|
+
lineHeight: b.line_height,
|
|
120
|
+
letterSpacing: b.role.tracking ? `${b.role.tracking}em` : undefined,
|
|
121
|
+
justifyContent: align === "center" ? "center" : rtl ? "flex-end" : "flex-start",
|
|
122
|
+
// Accent runs are separate boxes: a right-to-left line lays them out from the right.
|
|
123
|
+
flexDirection: rtl ? "row-reverse" : "row",
|
|
124
|
+
...textStyleFor(style, size),
|
|
125
|
+
};
|
|
126
|
+
if (style === "box") {
|
|
127
|
+
return div({ justifyContent: align === "center" ? "center" : rtl ? "flex-end" : "flex-start" }, [
|
|
128
|
+
div({ ...base, backgroundColor: theme.colors.card, padding: `0 ${Math.round(size * 0.3)}px`, borderRadius: Math.round(size * 0.2) }, runs),
|
|
129
|
+
]);
|
|
130
|
+
}
|
|
131
|
+
return div(base, runs);
|
|
132
|
+
}
|
|
133
|
+
function blockEl(b, theme, style, align) {
|
|
134
|
+
const lines = b.fitted.lines.map((l) => lineEl(l, b, theme, style, align));
|
|
135
|
+
if (b.marker !== undefined) {
|
|
136
|
+
const size = b.fitted.size;
|
|
137
|
+
const rtl = isRtl(b.fitted.lines.map((l) => l.map((r) => r.text).join("")).join(" "));
|
|
138
|
+
const markerStyle = {
|
|
139
|
+
fontFamily: fontStack(b.role.family, b.locale),
|
|
140
|
+
fontWeight: 800,
|
|
141
|
+
fontSize: size,
|
|
142
|
+
lineHeight: b.line_height,
|
|
143
|
+
color: theme.colors.accent,
|
|
144
|
+
width: Math.round(size * 1.3),
|
|
145
|
+
flexShrink: 0,
|
|
146
|
+
...textStyleFor(style === "box" ? "plain" : style, size),
|
|
147
|
+
};
|
|
148
|
+
const marker = b.marker === "•"
|
|
149
|
+
? div({ width: Math.round(size * 1.3), height: Math.round(size * b.line_height), alignItems: "center", justifyContent: rtl ? "flex-end" : "flex-start", flexShrink: 0 }, [
|
|
150
|
+
div({ width: Math.round(size * 0.32), height: Math.round(size * 0.32), borderRadius: 999, backgroundColor: theme.colors.accent }),
|
|
151
|
+
])
|
|
152
|
+
: div(markerStyle, b.marker);
|
|
153
|
+
return div({ marginTop: b.gap_before, flexDirection: rtl ? "row-reverse" : "row", alignItems: "flex-start" }, [marker, div({ flexDirection: "column", flexGrow: 1 }, lines)]);
|
|
154
|
+
}
|
|
155
|
+
return div({ marginTop: b.gap_before, flexDirection: "column", alignItems: align === "center" ? "center" : "flex-start" }, lines);
|
|
156
|
+
}
|
|
157
|
+
/** Body text under this size (on a 1080-wide slide) is hard to read on a phone. */
|
|
158
|
+
export const SMALL_TEXT = 38;
|
|
159
|
+
const blocksHeight = (blocks) => blocks.reduce((h, b) => h + b.gap_before + b.fitted.height, 0);
|
|
160
|
+
/** Fits a slide's words into the box: the title gets what is left after the smaller text. */
|
|
161
|
+
function planText(slide, index, tipNumber, theme, box, color, muted, scale) {
|
|
162
|
+
const issues = [];
|
|
163
|
+
const blocks = [];
|
|
164
|
+
const display = theme.display;
|
|
165
|
+
const body = theme.body;
|
|
166
|
+
const serifish = display.family === "Instrument Serif" || display.family === "Anton";
|
|
167
|
+
const titleLh = display.family === "Anton" ? 1.05 : serifish ? 1.0 : 1.12;
|
|
168
|
+
const bodyLh = 1.3;
|
|
169
|
+
const s = (n) => Math.round(n * scale);
|
|
170
|
+
let title = slide.title;
|
|
171
|
+
if (slide.kind === "tip" && title && tipNumber !== undefined)
|
|
172
|
+
title = `${tipNumber}. ${title}`;
|
|
173
|
+
if (slide.kind === "quote" && title)
|
|
174
|
+
title = `“${title}”`;
|
|
175
|
+
const fitBody = (text, max, min, height, kind, gap, role = body, c = color, marker, width = box.width) => ({ role, fitted: fit(role, text, { max, min, width, height, line_height: bodyLh, locale: box.locale }), color: c, line_height: bodyLh, gap_before: gap, kind, marker, locale: box.locale });
|
|
176
|
+
// Smaller text first, at a size that reads; the title then takes the rest of the height.
|
|
177
|
+
const lower = [];
|
|
178
|
+
if (slide.kind === "list" && slide.items?.length) {
|
|
179
|
+
const n = slide.items.length;
|
|
180
|
+
const perItem = (box.height * (title ? 0.62 : 0.95)) / n - s(20);
|
|
181
|
+
slide.items.forEach((item, i) => {
|
|
182
|
+
const marker = slide.list_style === "number" ? `${i + 1}.` : slide.list_style === "check" ? "✓" : "•";
|
|
183
|
+
const markerWidth = Math.round(s(58) * 1.3);
|
|
184
|
+
const role = { ...body, weight: Math.max(body.weight, 600) };
|
|
185
|
+
lower.push(fitBody(item, s(58), s(32), perItem, "item", i === 0 ? s(title ? 40 : 0) : s(18), role, color, missingChars(body, marker).length ? "•" : marker, box.width - markerWidth));
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
if (slide.body) {
|
|
189
|
+
const max = slide.kind === "text" ? s(72) : s(54);
|
|
190
|
+
const min = slide.kind === "text" ? s(36) : s(30);
|
|
191
|
+
lower.push(fitBody(slide.body, max, min, box.height * (title ? 0.42 : 0.95), "body", title || lower.length ? s(28) : 0));
|
|
192
|
+
}
|
|
193
|
+
if (slide.kind === "quote" && slide.author)
|
|
194
|
+
lower.push(fitBody(`— ${slide.author}`, s(42), s(28), s(140), "meta", s(28), body, muted));
|
|
195
|
+
if (title) {
|
|
196
|
+
const isCover = slide.kind === "cover";
|
|
197
|
+
const max = isCover ? s(display.family === "Anton" ? 150 : display.family === "Instrument Serif" ? 136 : 118) : s(display.family === "Anton" ? 130 : display.family === "Instrument Serif" ? 120 : 100);
|
|
198
|
+
const min = isCover ? s(56) : s(46);
|
|
199
|
+
const room = Math.max(box.height - blocksHeight(lower), box.height * 0.3);
|
|
200
|
+
blocks.push({ role: display, fitted: fit(display, title, { max, min, width: box.width, height: room, line_height: titleLh, locale: box.locale }), color, line_height: titleLh, gap_before: 0, kind: "title", locale: box.locale });
|
|
201
|
+
}
|
|
202
|
+
blocks.push(...lower);
|
|
203
|
+
for (const b of blocks) {
|
|
204
|
+
if (!b.fitted.overflow && (b.kind === "body" || b.kind === "item") && b.fitted.size < SMALL_TEXT)
|
|
205
|
+
issues.push({ slide: index, level: "warning", code: "small-text", message: `slide ${index + 1}: the ${b.kind === "item" ? "list" : "body"} is drawn at ${b.fitted.size}px, small on a phone; cut words so it can be bigger` });
|
|
206
|
+
if (b.fitted.overflow)
|
|
207
|
+
issues.push({ slide: index, level: "error", code: "text-overflow", message: `slide ${index + 1}: the ${b.kind} does not fit even at the smallest size; shorten it` });
|
|
208
|
+
else if (b.fitted.broke_word)
|
|
209
|
+
issues.push({ slide: index, level: "warning", code: "word-cut", message: `slide ${index + 1}: a word in the ${b.kind} is too long for one line and is cut` });
|
|
210
|
+
}
|
|
211
|
+
if (blocksHeight(blocks) > box.height + 2)
|
|
212
|
+
issues.push({ slide: index, level: "error", code: "text-overflow", message: `slide ${index + 1}: the text is taller than the slide's safe area; shorten it or split the slide` });
|
|
213
|
+
return { blocks, issues };
|
|
214
|
+
}
|
|
215
|
+
// ---------- images ----------
|
|
216
|
+
async function cropTo(file, w, h, focus) {
|
|
217
|
+
return `data:image/jpeg;base64,${(await cropBuffer(file, w, h, focus)).toString("base64")}`;
|
|
218
|
+
}
|
|
219
|
+
async function cropBuffer(file, w, h, focus) {
|
|
220
|
+
const img = sharp(file, { limitInputPixels: 80_000_000 }).rotate();
|
|
221
|
+
const meta = await sharp(await img.toBuffer()).metadata();
|
|
222
|
+
const iw = meta.width ?? w;
|
|
223
|
+
const ih = meta.height ?? h;
|
|
224
|
+
const scale = Math.max(w / iw, h / ih);
|
|
225
|
+
const sw = Math.ceil(iw * scale);
|
|
226
|
+
const sh = Math.ceil(ih * scale);
|
|
227
|
+
const fx = (focus?.x ?? 50) / 100;
|
|
228
|
+
const fy = (focus?.y ?? 50) / 100;
|
|
229
|
+
const left = Math.min(Math.max(0, Math.round(sw * fx - w / 2)), sw - w);
|
|
230
|
+
const top = Math.min(Math.max(0, Math.round(sh * fy - h / 2)), sh - h);
|
|
231
|
+
return sharp(file, { limitInputPixels: 80_000_000 }).rotate().resize(sw, sh).extract({ left, top, width: w, height: h }).jpeg({ quality: 90 }).toBuffer();
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* How well white words read over the part of the photo behind them: the photo's brightness there
|
|
235
|
+
* (after the theme's darkening) and how busy it is. Outline and box styles carry their own contrast.
|
|
236
|
+
*/
|
|
237
|
+
async function photoReadability(photo, rects, W, H, scrim, style, index) {
|
|
238
|
+
if (!rects.length || style === "outline" || style === "box")
|
|
239
|
+
return [];
|
|
240
|
+
const x0 = Math.max(0, Math.min(...rects.map((r) => r.x0)));
|
|
241
|
+
const y0 = Math.max(0, Math.min(...rects.map((r) => r.y0)));
|
|
242
|
+
const x1 = Math.min(W, Math.max(...rects.map((r) => r.x1)));
|
|
243
|
+
const y1 = Math.min(H, Math.max(...rects.map((r) => r.y1)));
|
|
244
|
+
if (x1 - x0 < 8 || y1 - y0 < 8)
|
|
245
|
+
return [];
|
|
246
|
+
const stats = await sharp(photo).extract({ left: x0, top: y0, width: x1 - x0, height: y1 - y0 }).greyscale().stats();
|
|
247
|
+
const ch = stats.channels[0];
|
|
248
|
+
const keep = 1 - scrim;
|
|
249
|
+
const grey = Math.round(ch.mean * keep);
|
|
250
|
+
const hex = `#${grey.toString(16).padStart(2, "0").repeat(3)}`;
|
|
251
|
+
const ratio = 1.05 / (luminance(hex) + 0.05);
|
|
252
|
+
const issues = [];
|
|
253
|
+
if (ratio < 2.6)
|
|
254
|
+
issues.push({ slide: index, level: "warning", code: "photo-contrast", message: `slide ${index + 1}: the photo behind the words is bright (contrast ${ratio.toFixed(1)}:1); use text_style outline or box, move the words (text_position) or the photo (focus), or pick a darker photo` });
|
|
255
|
+
else if (ch.stdev * keep > 72)
|
|
256
|
+
issues.push({ slide: index, level: "warning", code: "busy-photo", message: `slide ${index + 1}: the photo behind the words is busy; the words may be hard to read. Try text_style outline or box, or move them to a calmer part (text_position)` });
|
|
257
|
+
return issues;
|
|
258
|
+
}
|
|
259
|
+
function readImage(ctx, path) {
|
|
260
|
+
const cached = ctx.images?.get(path);
|
|
261
|
+
if (cached)
|
|
262
|
+
return cached;
|
|
263
|
+
const buf = readFileSync(path);
|
|
264
|
+
ctx.images?.set(path, buf);
|
|
265
|
+
return buf;
|
|
266
|
+
}
|
|
267
|
+
async function imageEl(ctx, slide, w, h, style = {}) {
|
|
268
|
+
if (!slide.image)
|
|
269
|
+
return null;
|
|
270
|
+
const path = ctx.resolveImage(slide.image);
|
|
271
|
+
if (!path)
|
|
272
|
+
return null;
|
|
273
|
+
const src = await cropTo(readImage(ctx, path), w, h, slide.focus);
|
|
274
|
+
return el("img", { width: w, height: h, ...style }, undefined, { src, width: w, height: h });
|
|
275
|
+
}
|
|
276
|
+
async function iconEl(ctx, size, theme) {
|
|
277
|
+
if (ctx.brand.icon && existsSync(ctx.brand.icon)) {
|
|
278
|
+
const src = await cropTo(readImage(ctx, ctx.brand.icon), size * 2, size * 2);
|
|
279
|
+
return el("img", { width: size, height: size, borderRadius: Math.round(size * 0.225) }, undefined, { src, width: size, height: size });
|
|
280
|
+
}
|
|
281
|
+
const initial = (ctx.brand.name ?? "?").trim().charAt(0).toUpperCase() || "?";
|
|
282
|
+
return div({ width: size, height: size, borderRadius: Math.round(size * 0.225), backgroundColor: theme.colors.accent, alignItems: "center", justifyContent: "center", color: readableOn(theme.colors.accent), fontFamily: fontStack("Inter"), fontWeight: 700, fontSize: Math.round(size * 0.5) }, initial);
|
|
283
|
+
}
|
|
284
|
+
function handleText(ctx) {
|
|
285
|
+
const h = ctx.brand.handle?.trim();
|
|
286
|
+
if (!h)
|
|
287
|
+
return undefined;
|
|
288
|
+
return h.startsWith("@") ? h : `@${h}`;
|
|
289
|
+
}
|
|
290
|
+
function lineWidth(b, line) {
|
|
291
|
+
const text = line.map((r) => r.text).join("");
|
|
292
|
+
return measure(b.role, b.role.uppercase ? upperCase(text, b.locale) : text, b.fitted.size, b.locale);
|
|
293
|
+
}
|
|
294
|
+
/** The rectangles of all lines of the blocks, laid out from (left, top) in a column `width` wide. */
|
|
295
|
+
function lineRects(blocks, left, top, width, align) {
|
|
296
|
+
const out = [];
|
|
297
|
+
let y = top;
|
|
298
|
+
for (const b of blocks) {
|
|
299
|
+
y += b.gap_before;
|
|
300
|
+
const lh = b.fitted.size * b.line_height;
|
|
301
|
+
const marker = b.marker !== undefined ? Math.round(b.fitted.size * 1.3) : 0;
|
|
302
|
+
for (const line of b.fitted.lines) {
|
|
303
|
+
const w = Math.min(width, lineWidth(b, line) + marker);
|
|
304
|
+
const rtl = isRtl(line.map((r) => r.text).join(""));
|
|
305
|
+
const x0 = align === "center" ? left + (width - w) / 2 : rtl ? left + width - w : left;
|
|
306
|
+
out.push({ x0: Math.round(x0), y0: Math.round(y), x1: Math.round(x0 + w), y1: Math.round(y + lh) });
|
|
307
|
+
y += lh;
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
return out;
|
|
311
|
+
}
|
|
312
|
+
async function photoLayout(ctx, a, f) {
|
|
313
|
+
const t = ctx.theme;
|
|
314
|
+
const style = a.slide.text_style ?? a.deck.text_style ?? t.text_style;
|
|
315
|
+
const align = t.align;
|
|
316
|
+
const width = f.W - f.pad * 2;
|
|
317
|
+
const safe = f.H - f.top - f.bottom;
|
|
318
|
+
const plan = planText(a.slide, a.index, a.tipNumber, t, { width, height: safe - (a.slide.kind === "app" ? BADGE_H : 0), locale: ctx.locale }, style === "box" ? t.colors.fg : "#ffffff", style === "box" ? t.colors.muted : "#f0f0f0", 1);
|
|
319
|
+
const issues = [...plan.issues];
|
|
320
|
+
const children = [];
|
|
321
|
+
const path = !a.dry && a.slide.image ? ctx.resolveImage(a.slide.image) : null;
|
|
322
|
+
const photo = path ? await cropBuffer(readImage(ctx, path), f.W, f.H, a.slide.focus) : null;
|
|
323
|
+
const img = photo ? el("img", { width: f.W, height: f.H, position: "absolute", left: 0, top: 0 }, undefined, { src: `data:image/jpeg;base64,${photo.toString("base64")}`, width: f.W, height: f.H }) : null;
|
|
324
|
+
if (img)
|
|
325
|
+
children.push(img);
|
|
326
|
+
else {
|
|
327
|
+
if (a.slide.image && !a.dry)
|
|
328
|
+
issues.push({ slide: a.index, level: "error", code: "image-missing", message: `slide ${a.index + 1}: image ${a.slide.image} not found` });
|
|
329
|
+
children.push(div({ position: "absolute", left: 0, top: 0, width: f.W, height: f.H, backgroundImage: `linear-gradient(160deg, ${mix(t.colors.bg, t.colors.accent, 0.35)}, ${t.colors.bg})` }));
|
|
330
|
+
}
|
|
331
|
+
if (t.scrim > 0) {
|
|
332
|
+
const a1 = t.scrim;
|
|
333
|
+
const grad = t.align === "left" ? `linear-gradient(180deg, rgba(0,0,0,${(a1 * 0.25).toFixed(2)}) 0%, rgba(0,0,0,${(a1 * 0.6).toFixed(2)}) 45%, rgba(0,0,0,${a1.toFixed(2)}) 100%)` : `linear-gradient(180deg, rgba(0,0,0,${a1.toFixed(2)}), rgba(0,0,0,${a1.toFixed(2)}))`;
|
|
334
|
+
children.push(div({ position: "absolute", left: 0, top: 0, width: f.W, height: f.H, backgroundImage: grad }));
|
|
335
|
+
}
|
|
336
|
+
const h = blocksHeight(plan.blocks) + (a.slide.kind === "app" ? BADGE_H : 0);
|
|
337
|
+
const pos = a.slide.text_position ?? (a.slide.kind === "cover" ? t.cover_position : t.tip_position);
|
|
338
|
+
const top = Math.min(Math.max(f.top, Math.round((f.H * pos) / 100 - h / 2)), f.H - f.bottom - h);
|
|
339
|
+
children.push(div({ position: "absolute", left: f.pad, top: Math.max(f.top, top), width, flexDirection: "column", alignItems: align === "center" ? "center" : "flex-start" }, [...plan.blocks.map((b) => blockEl(b, t, style, align)), ...(a.slide.kind === "app" ? [await appBadge(ctx, f, t, "dark")] : [])]));
|
|
340
|
+
if (a.deck.show_handle && handleText(ctx))
|
|
341
|
+
children.push(div({ position: "absolute", left: f.pad, top: Math.round(f.top * 0.45), fontFamily: fontStack("Inter"), fontWeight: 600, fontSize: 30, color: "#ffffff", opacity: 0.85, textShadow: "0 2px 8px rgba(0,0,0,0.5)" }, handleText(ctx)));
|
|
342
|
+
const lines = lineRects(plan.blocks, f.pad, Math.max(f.top, top), width, align);
|
|
343
|
+
if (photo)
|
|
344
|
+
issues.push(...(await photoReadability(photo, lines, f.W, f.H, t.align === "left" ? t.scrim * 0.6 : t.scrim, style, a.index)));
|
|
345
|
+
return { tree: div({ width: f.W, height: f.H, position: "relative", backgroundColor: t.colors.bg }, children), issues, lines };
|
|
346
|
+
}
|
|
347
|
+
const BADGE_H = 196;
|
|
348
|
+
async function appBadge(ctx, f, t, tone) {
|
|
349
|
+
const icon = await iconEl(ctx, 96, t);
|
|
350
|
+
const name = ctx.brand.name ?? "";
|
|
351
|
+
const bg = tone === "dark" ? "rgba(255,255,255,0.94)" : t.colors.card;
|
|
352
|
+
const fg = tone === "dark" ? "#111111" : t.colors.fg;
|
|
353
|
+
return div({ marginTop: 56, alignItems: "center", justifyContent: t.align === "center" ? "center" : "flex-start" }, [
|
|
354
|
+
div({ alignItems: "center", backgroundColor: bg, borderRadius: 36, padding: "20px 34px 20px 20px" }, [icon, div({ marginLeft: 24, fontFamily: fontStack("Inter"), fontWeight: 700, fontSize: 40, color: fg }, name || "Get the app")]),
|
|
355
|
+
]);
|
|
356
|
+
}
|
|
357
|
+
async function magazineLayout(ctx, a, f) {
|
|
358
|
+
const t = ctx.theme;
|
|
359
|
+
const width = f.W - f.pad * 2;
|
|
360
|
+
const issues = [];
|
|
361
|
+
const children = [];
|
|
362
|
+
const hasImage = !!a.slide.image;
|
|
363
|
+
const header = a.deck.show_handle ? handleText(ctx) ?? ctx.brand.name : undefined;
|
|
364
|
+
if (header)
|
|
365
|
+
children.push(div({ position: "absolute", left: f.pad, top: Math.round(f.top * 0.55), fontFamily: fontStack("Inter"), fontWeight: 600, fontSize: 30, color: t.colors.muted }, header));
|
|
366
|
+
const missing = () => {
|
|
367
|
+
if (a.slide.image && !a.dry)
|
|
368
|
+
issues.push({ slide: a.index, level: "error", code: "image-missing", message: `slide ${a.index + 1}: image ${a.slide.image} not found` });
|
|
369
|
+
};
|
|
370
|
+
if (a.slide.kind === "cover" && hasImage && a.deck.full_cover) {
|
|
371
|
+
// A full photo that fades into the page colour, with the title on the fade.
|
|
372
|
+
const img = a.dry ? null : await imageEl(ctx, a.slide, f.W, f.H, { position: "absolute", left: 0, top: 0 });
|
|
373
|
+
if (img)
|
|
374
|
+
children.unshift(img);
|
|
375
|
+
else
|
|
376
|
+
missing();
|
|
377
|
+
const textRoom = Math.round((f.H - f.top - f.bottom) * 0.5);
|
|
378
|
+
const plan = planText(a.slide, a.index, a.tipNumber, t, { width, height: textRoom, locale: ctx.locale }, t.colors.fg, t.colors.muted, 1);
|
|
379
|
+
issues.push(...plan.issues);
|
|
380
|
+
const h = blocksHeight(plan.blocks);
|
|
381
|
+
const top = f.H - f.bottom - h;
|
|
382
|
+
const fadeStart = Math.max(0, top - 260);
|
|
383
|
+
children.push(div({ position: "absolute", left: 0, top: fadeStart, width: f.W, height: f.H - fadeStart, backgroundImage: `linear-gradient(180deg, ${t.colors.bg}00 0%, ${t.colors.bg}e6 38%, ${t.colors.bg} 60%)` }));
|
|
384
|
+
children.push(div({ position: "absolute", left: f.pad, top, width, flexDirection: "column" }, plan.blocks.map((b) => blockEl(b, t, "plain", "left"))));
|
|
385
|
+
return { tree: div({ width: f.W, height: f.H, position: "relative", backgroundColor: t.colors.bg }, children), issues, lines: lineRects(plan.blocks, f.pad, top, width, "left") };
|
|
386
|
+
}
|
|
387
|
+
const safeTop = f.top + (header ? 40 : 0);
|
|
388
|
+
const safe = f.H - safeTop - f.bottom;
|
|
389
|
+
const imageHeight = hasImage ? Math.round(safe * (a.slide.kind === "cover" ? 0.56 : 0.5)) : 0;
|
|
390
|
+
const badge = a.slide.kind === "app" ? BADGE_H : 0;
|
|
391
|
+
const textRoom = (hasImage ? safe - imageHeight - 48 : safe) - badge;
|
|
392
|
+
const plan = planText(a.slide, a.index, a.tipNumber, t, { width, height: textRoom, locale: ctx.locale }, t.colors.fg, t.colors.muted, 1);
|
|
393
|
+
issues.push(...plan.issues);
|
|
394
|
+
const h = blocksHeight(plan.blocks) + badge;
|
|
395
|
+
const textTop = hasImage ? safeTop : Math.round(safeTop + Math.max(0, (safe - h) * (a.slide.kind === "cover" ? 0.42 : 0.3)));
|
|
396
|
+
children.push(div({ position: "absolute", left: f.pad, top: textTop, width, flexDirection: "column" }, [...plan.blocks.map((b) => blockEl(b, t, "plain", "left")), ...(badge ? [await appBadge(ctx, f, t, "page")] : [])]));
|
|
397
|
+
if (hasImage) {
|
|
398
|
+
const imgTop = f.H - f.bottom - imageHeight;
|
|
399
|
+
const img = a.dry ? null : await imageEl(ctx, a.slide, width, imageHeight, { borderRadius: 32 });
|
|
400
|
+
if (img)
|
|
401
|
+
children.push(div({ position: "absolute", left: f.pad, top: imgTop, width, height: imageHeight }, [img]));
|
|
402
|
+
else {
|
|
403
|
+
missing();
|
|
404
|
+
children.push(div({ position: "absolute", left: f.pad, top: imgTop, width, height: imageHeight, borderRadius: 32, backgroundColor: t.colors.card }));
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
return { tree: div({ width: f.W, height: f.H, position: "relative", backgroundColor: t.colors.bg }, children), issues, lines: lineRects(plan.blocks, f.pad, textTop, width, "left") };
|
|
408
|
+
}
|
|
409
|
+
async function plainLayout(ctx, a, f) {
|
|
410
|
+
const t = ctx.theme;
|
|
411
|
+
const width = f.W - f.pad * 2;
|
|
412
|
+
const issues = [];
|
|
413
|
+
const headerH = 120;
|
|
414
|
+
const safe = f.H - f.top - f.bottom - headerH - 40;
|
|
415
|
+
const hasImage = !!a.slide.image;
|
|
416
|
+
const imageHeight = hasImage ? Math.round(safe * 0.45) : 0;
|
|
417
|
+
const plan = planText(a.slide, a.index, a.tipNumber, t, { width, height: safe - (hasImage ? imageHeight + 40 : 0), locale: ctx.locale }, t.colors.fg, t.colors.muted, 1.08);
|
|
418
|
+
issues.push(...plan.issues);
|
|
419
|
+
const header = div({ alignItems: "center", height: headerH }, [
|
|
420
|
+
await iconEl(ctx, 96, t),
|
|
421
|
+
div({ flexDirection: "column", marginLeft: 26 }, [
|
|
422
|
+
div({ fontFamily: fontStack("Inter"), fontWeight: 700, fontSize: 38, color: t.colors.fg }, ctx.brand.name ?? "Your app"),
|
|
423
|
+
...(handleText(ctx) ? [div({ fontFamily: fontStack("Inter"), fontWeight: 400, fontSize: 32, color: t.colors.muted }, handleText(ctx))] : []),
|
|
424
|
+
]),
|
|
425
|
+
]);
|
|
426
|
+
const content = [header, div({ flexDirection: "column", marginTop: 40 }, plan.blocks.map((b) => blockEl(b, t, "plain", "left")))];
|
|
427
|
+
if (hasImage) {
|
|
428
|
+
const img = a.dry ? null : await imageEl(ctx, a.slide, width, imageHeight, { borderRadius: 28 });
|
|
429
|
+
if (img)
|
|
430
|
+
content.push(div({ marginTop: 40, width, height: imageHeight }, [img]));
|
|
431
|
+
else if (!a.dry)
|
|
432
|
+
issues.push({ slide: a.index, level: "error", code: "image-missing", message: `slide ${a.index + 1}: image ${a.slide.image} not found` });
|
|
433
|
+
}
|
|
434
|
+
const total = headerH + 40 + blocksHeight(plan.blocks) + (hasImage ? imageHeight + 40 : 0);
|
|
435
|
+
const top = Math.max(f.top, Math.round(f.top + (f.H - f.top - f.bottom - total) * 0.45));
|
|
436
|
+
return {
|
|
437
|
+
tree: div({ width: f.W, height: f.H, position: "relative", backgroundColor: t.colors.bg }, [div({ position: "absolute", left: f.pad, top, width, flexDirection: "column" }, content)]),
|
|
438
|
+
issues,
|
|
439
|
+
lines: lineRects(plan.blocks, f.pad, top + headerH + 40, width, "left"),
|
|
440
|
+
};
|
|
441
|
+
}
|
|
442
|
+
/** Screen width / height of a phone screenshot when the real one cannot be read (dry runs). */
|
|
443
|
+
const PHONE_ASPECT = 1179 / 2556;
|
|
444
|
+
/** A screenshot of the app inside a phone, with the title (and body) above it. */
|
|
445
|
+
async function screenshotLayout(ctx, a, f) {
|
|
446
|
+
const t = ctx.theme;
|
|
447
|
+
const width = f.W - f.pad * 2;
|
|
448
|
+
const safe = f.H - f.top - f.bottom;
|
|
449
|
+
const issues = [];
|
|
450
|
+
const onPhoto = t.design === "photo";
|
|
451
|
+
const fg = onPhoto ? "#ffffff" : t.colors.fg;
|
|
452
|
+
const muted = onPhoto ? "#eeeeee" : t.colors.muted;
|
|
453
|
+
const align = t.design === "photo" ? t.align : "left";
|
|
454
|
+
const hasText = !!(a.slide.title || a.slide.body);
|
|
455
|
+
const plan = hasText ? planText(a.slide, a.index, undefined, t, { width, height: Math.round(safe * 0.3), locale: ctx.locale }, fg, muted, 0.9) : { blocks: [], issues: [] };
|
|
456
|
+
issues.push(...plan.issues);
|
|
457
|
+
const textH = blocksHeight(plan.blocks);
|
|
458
|
+
const gap = hasText ? 44 : 0;
|
|
459
|
+
const path = !a.dry && a.slide.image ? ctx.resolveImage(a.slide.image) : null;
|
|
460
|
+
if (a.slide.image && !a.dry && !path)
|
|
461
|
+
issues.push({ slide: a.index, level: "error", code: "image-missing", message: `slide ${a.index + 1}: image ${a.slide.image} not found` });
|
|
462
|
+
let aspect = PHONE_ASPECT;
|
|
463
|
+
if (path) {
|
|
464
|
+
const m = await sharp(readImage(ctx, path)).metadata();
|
|
465
|
+
if (m.width && m.height)
|
|
466
|
+
aspect = Math.min(Math.max(m.width / m.height, 0.4), 0.8);
|
|
467
|
+
}
|
|
468
|
+
let outerH = Math.floor(safe - textH - gap);
|
|
469
|
+
const bezel = Math.max(10, Math.round(outerH * 0.018));
|
|
470
|
+
let innerH = outerH - bezel * 2;
|
|
471
|
+
let innerW = Math.round(innerH * aspect);
|
|
472
|
+
if (innerW + bezel * 2 > width) {
|
|
473
|
+
innerW = width - bezel * 2;
|
|
474
|
+
innerH = Math.round(innerW / aspect);
|
|
475
|
+
outerH = innerH + bezel * 2;
|
|
476
|
+
}
|
|
477
|
+
const outerW = innerW + bezel * 2;
|
|
478
|
+
if (innerH < 400)
|
|
479
|
+
issues.push({ slide: a.index, level: "warning", code: "small-screenshot", message: `slide ${a.index + 1}: the screenshot is small; shorten the text above it` });
|
|
480
|
+
const radius = Math.round(outerW * 0.12);
|
|
481
|
+
const screen = path
|
|
482
|
+
? el("img", { position: "absolute", left: bezel, top: bezel, width: innerW, height: innerH, borderRadius: Math.max(8, radius - bezel) }, undefined, { src: await cropTo(readImage(ctx, path), innerW, innerH), width: innerW, height: innerH })
|
|
483
|
+
: div({ position: "absolute", left: bezel, top: bezel, width: innerW, height: innerH, borderRadius: Math.max(8, radius - bezel), backgroundColor: mix(t.colors.bg, "#888888", 0.4) });
|
|
484
|
+
const phone = div({ position: "relative", width: outerW, height: outerH, flexShrink: 0, borderRadius: radius, backgroundColor: "#0b0b0c" }, [screen]);
|
|
485
|
+
const top = f.top + Math.max(0, Math.round((safe - textH - gap - outerH) / 2));
|
|
486
|
+
const bg = onPhoto ? div({ position: "absolute", left: 0, top: 0, width: f.W, height: f.H, backgroundImage: `linear-gradient(160deg, ${mix(t.colors.bg, t.colors.accent, 0.35)}, ${t.colors.bg})` }) : null;
|
|
487
|
+
const column = div({ position: "absolute", left: f.pad, top, width, flexDirection: "column", alignItems: align === "center" ? "center" : "flex-start" }, [
|
|
488
|
+
...plan.blocks.map((b) => blockEl(b, t, onPhoto ? (t.text_style === "box" ? "plain" : t.text_style) : "plain", align)),
|
|
489
|
+
div({ marginTop: gap, width, justifyContent: "center" }, [phone]),
|
|
490
|
+
]);
|
|
491
|
+
return { tree: div({ width: f.W, height: f.H, position: "relative", backgroundColor: t.colors.bg }, [...(bg ? [bg] : []), column]), issues, lines: lineRects(plan.blocks, f.pad, top, width, align) };
|
|
492
|
+
}
|
|
493
|
+
/** "Part 2", "Part 2/5" or the series' own label, filled in. */
|
|
494
|
+
export function seriesLabel(s) {
|
|
495
|
+
const fill = (x) => x.replace(/\{part\}/g, String(s.part)).replace(/\{of\}/g, s.of ? String(s.of) : "");
|
|
496
|
+
return s.label ? fill(s.label) : `Part ${s.part}${s.of ? `/${s.of}` : ""}`;
|
|
497
|
+
}
|
|
498
|
+
function seriesPill(ctx, f, s) {
|
|
499
|
+
const t = ctx.theme;
|
|
500
|
+
return div({ position: "absolute", right: f.pad, top: f.top, backgroundColor: t.colors.accent, color: readableOn(t.colors.accent), borderRadius: 999, padding: "10px 26px", fontFamily: fontStack("Inter"), fontWeight: 700, fontSize: 34 }, seriesLabel(s));
|
|
501
|
+
}
|
|
502
|
+
export function tipNumbers(slides) {
|
|
503
|
+
let n = 0;
|
|
504
|
+
return slides.map((s) => (s.kind === "tip" ? (n = s.number ?? n + 1) : undefined));
|
|
505
|
+
}
|
|
506
|
+
export async function laySlide(ctx, a) {
|
|
507
|
+
const f = frameFor(ctx.size);
|
|
508
|
+
const all = [a.slide.title, a.slide.body, ...(a.slide.items ?? []), a.slide.author, ctx.brand.name, ctx.brand.handle].filter(Boolean).join(" ");
|
|
509
|
+
const failed = await ensureScriptFonts(all, ctx.locale);
|
|
510
|
+
const laid = a.slide.kind === "screenshot"
|
|
511
|
+
? await screenshotLayout(ctx, a, f)
|
|
512
|
+
: ctx.theme.design === "photo"
|
|
513
|
+
? await photoLayout(ctx, a, f)
|
|
514
|
+
: ctx.theme.design === "magazine"
|
|
515
|
+
? await magazineLayout(ctx, a, f)
|
|
516
|
+
: await plainLayout(ctx, a, f);
|
|
517
|
+
if (a.deck.series && a.slide.kind === "cover")
|
|
518
|
+
laid.tree.props.children.push(seriesPill(ctx, f, a.deck.series));
|
|
519
|
+
const text = [a.slide.title, a.slide.body, ...(a.slide.items ?? []), a.slide.author].filter(Boolean).map((s) => plainText(s)).join(" ");
|
|
520
|
+
const roles = [ctx.theme.display, ctx.theme.body];
|
|
521
|
+
const reports = roles.map((r) => glyphReport(r, ctx.theme.display.uppercase && r === ctx.theme.display ? upperCase(text, ctx.locale) : text, ctx.locale));
|
|
522
|
+
const missing = [...new Set(reports.flatMap((r) => r.missing))];
|
|
523
|
+
const fallback = [...new Set(reports.flatMap((r) => r.fallback))];
|
|
524
|
+
if (COMPLEX.test(text) && [...text].some((ch) => /\p{Extended_Pictographic}/u.test(ch)))
|
|
525
|
+
laid.issues.push({ slide: a.index, level: "warning", code: "emoji-shaped", message: `slide ${a.index + 1}: emoji next to Arabic, Hebrew, Thai or Hindi text are not drawn yet; move them to a line of their own or drop them` });
|
|
526
|
+
if (failed.length)
|
|
527
|
+
laid.issues.push({ slide: a.index, level: "error", code: "font-download", message: `slide ${a.index + 1}: the ${failed.map((p) => SCRIPTS[p].label).join(" and ")} font is downloaded once on first use, and that failed (offline?); connect and render again` });
|
|
528
|
+
else if (missing.length)
|
|
529
|
+
laid.issues.push({ slide: a.index, level: "error", code: "missing-glyphs", message: `slide ${a.index + 1}: no font here can draw ${missing.slice(0, 12).join(" ")}; change the text` });
|
|
530
|
+
else if (fallback.length)
|
|
531
|
+
laid.issues.push({ slide: a.index, level: "warning", code: "font-fallback", message: `slide ${a.index + 1}: the theme's font has no ${fallback.slice(0, 12).join(" ")}; those letters are drawn in Inter, a theme with TikTok Sans or Inter looks more even` });
|
|
532
|
+
return laid;
|
|
533
|
+
}
|
|
534
|
+
// ---------- emoji ----------
|
|
535
|
+
function cacheDir() {
|
|
536
|
+
const dir = process.env.OPEN_SLIDESHOW_CACHE ?? join(homedir(), ".cache", "open-slideshow");
|
|
537
|
+
mkdirSync(join(dir, "emoji"), { recursive: true });
|
|
538
|
+
return dir;
|
|
539
|
+
}
|
|
540
|
+
function emojiCode(segment) {
|
|
541
|
+
const cps = [...segment].map((c) => c.codePointAt(0).toString(16));
|
|
542
|
+
// Twemoji names drop FE0F unless the sequence has a ZWJ.
|
|
543
|
+
return (segment.includes("") ? cps : cps.filter((c) => c !== "fe0f")).join("-");
|
|
544
|
+
}
|
|
545
|
+
async function loadEmoji(segment) {
|
|
546
|
+
const code = emojiCode(segment);
|
|
547
|
+
const file = join(cacheDir(), "emoji", `${code}.svg`);
|
|
548
|
+
if (existsSync(file))
|
|
549
|
+
return `data:image/svg+xml;base64,${readFileSync(file).toString("base64")}`;
|
|
550
|
+
if (process.env.OPEN_SLIDESHOW_OFFLINE === "1")
|
|
551
|
+
return "";
|
|
552
|
+
try {
|
|
553
|
+
const res = await fetch(`https://cdn.jsdelivr.net/gh/jdecked/twemoji@15.1.0/assets/svg/${code}.svg`, { signal: AbortSignal.timeout(8000) });
|
|
554
|
+
if (!res.ok)
|
|
555
|
+
return "";
|
|
556
|
+
const svg = Buffer.from(await res.arrayBuffer());
|
|
557
|
+
if (svg.length > 200_000 || !svg.toString("utf8", 0, 200).includes("<svg"))
|
|
558
|
+
return "";
|
|
559
|
+
writeFileSync(file, svg);
|
|
560
|
+
return `data:image/svg+xml;base64,${svg.toString("base64")}`;
|
|
561
|
+
}
|
|
562
|
+
catch {
|
|
563
|
+
return "";
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
// ---------- rasterize ----------
|
|
567
|
+
export async function rasterize(tree, size, quality = 90) {
|
|
568
|
+
const { width, height } = SIZES[size];
|
|
569
|
+
const svg = await satori(tree, {
|
|
570
|
+
width,
|
|
571
|
+
height,
|
|
572
|
+
fonts: satoriFonts(),
|
|
573
|
+
loadAdditionalAsset: async (code, segment) => (code === "emoji" ? loadEmoji(segment) : []),
|
|
574
|
+
});
|
|
575
|
+
const png = new Resvg(svg, { fitTo: { mode: "width", value: width }, font: { loadSystemFonts: false } }).render().asPng();
|
|
576
|
+
return sharp(png).jpeg({ quality, mozjpeg: true }).toBuffer();
|
|
577
|
+
}
|
|
578
|
+
export function themeFor(deck) {
|
|
579
|
+
const base = getTheme(deck.theme);
|
|
580
|
+
if (!base)
|
|
581
|
+
throw new Error(`unknown theme "${deck.theme}"; run themes_list to see them`);
|
|
582
|
+
return withColors(base, deck.colors);
|
|
583
|
+
}
|
|
584
|
+
export function renderHash(deck, ctx) {
|
|
585
|
+
const h = createHash("sha256");
|
|
586
|
+
h.update(RENDERER_VERSION);
|
|
587
|
+
h.update(JSON.stringify({ size: deck.size, theme: ctx.theme, full_cover: deck.full_cover, text_style: deck.text_style, show_handle: deck.show_handle, slides: deck.slides, brand: { n: ctx.brand.name, h: ctx.brand.handle }, series: deck.series }));
|
|
588
|
+
const images = [...deck.slides.map((s) => s.image), ctx.brand.icon].filter(Boolean);
|
|
589
|
+
for (const ref of images) {
|
|
590
|
+
const p = ref === ctx.brand.icon ? ref : ctx.resolveImage(ref);
|
|
591
|
+
if (p && existsSync(p))
|
|
592
|
+
h.update(createHash("sha256").update(readFileSync(p)).digest("hex"));
|
|
593
|
+
else
|
|
594
|
+
h.update(`missing:${ref}`);
|
|
595
|
+
}
|
|
596
|
+
return h.digest("hex").slice(0, 16);
|
|
597
|
+
}
|
|
598
|
+
export async function renderDeck(deck, ctx, outDir) {
|
|
599
|
+
mkdirSync(outDir, { recursive: true });
|
|
600
|
+
ctx.images ??= new Map();
|
|
601
|
+
const numbers = tipNumbers(deck.slides);
|
|
602
|
+
const files = [];
|
|
603
|
+
const issues = [];
|
|
604
|
+
const buffers = [];
|
|
605
|
+
const lines = [];
|
|
606
|
+
for (let i = 0; i < deck.slides.length; i++) {
|
|
607
|
+
const laid = await laySlide(ctx, { deck, slide: deck.slides[i], index: i, tipNumber: numbers[i] });
|
|
608
|
+
issues.push(...laid.issues);
|
|
609
|
+
lines.push(laid.lines);
|
|
610
|
+
const jpg = await rasterize(laid.tree, deck.size);
|
|
611
|
+
const name = `${String(i + 1).padStart(2, "0")}.jpg`;
|
|
612
|
+
writeFileSync(join(outDir, name), jpg);
|
|
613
|
+
files.push(name);
|
|
614
|
+
buffers.push(jpg);
|
|
615
|
+
}
|
|
616
|
+
// Renders of slides that no longer exist would be posted by mistake.
|
|
617
|
+
for (const f of readdirSync(outDir))
|
|
618
|
+
if (/^\d{2}\.jpg$/.test(f) && !files.includes(f))
|
|
619
|
+
unlinkSync(join(outDir, f));
|
|
620
|
+
writeFileSync(join(outDir, "sheet.jpg"), await contactSheet(buffers, deck.size));
|
|
621
|
+
const { safeZoneSlide } = await import("./readability.js");
|
|
622
|
+
writeFileSync(join(outDir, "sheet-safe.jpg"), await contactSheet(await Promise.all(buffers.map((b) => safeZoneSlide(b, deck.size))), deck.size));
|
|
623
|
+
return { files, sheet: "sheet.jpg", safe_sheet: "sheet-safe.jpg", hash: renderHash(deck, ctx), issues, lines };
|
|
624
|
+
}
|
|
625
|
+
/** All slides side by side in one image, small, for a quick look. */
|
|
626
|
+
export async function contactSheet(slides, size, columns = Math.min(slides.length, 5), labels) {
|
|
627
|
+
const { width, height } = SIZES[size];
|
|
628
|
+
const tw = 300;
|
|
629
|
+
const th = Math.round((height / width) * tw);
|
|
630
|
+
const gap = 16;
|
|
631
|
+
const labelH = labels ? 44 : 0;
|
|
632
|
+
const rows = Math.ceil(slides.length / columns);
|
|
633
|
+
const W = columns * tw + (columns + 1) * gap;
|
|
634
|
+
const H = rows * (th + labelH) + (rows + 1) * gap;
|
|
635
|
+
const composites = await Promise.all(slides.map(async (buf, i) => ({ input: await sharp(buf).resize(tw, th).toBuffer(), left: gap + (i % columns) * (tw + gap), top: gap + Math.floor(i / columns) * (th + labelH + gap) + labelH })));
|
|
636
|
+
if (labels) {
|
|
637
|
+
for (let i = 0; i < labels.length && i < slides.length; i++) {
|
|
638
|
+
const label = await rasterLabel(labels[i], tw, labelH);
|
|
639
|
+
composites.push({ input: label, left: gap + (i % columns) * (tw + gap), top: gap + Math.floor(i / columns) * (th + labelH + gap) });
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
return sharp({ create: { width: W, height: H, channels: 3, background: "#f2f2f2" } }).composite(composites).jpeg({ quality: 85 }).toBuffer();
|
|
643
|
+
}
|
|
644
|
+
async function rasterLabel(text, w, h) {
|
|
645
|
+
const svg = await satori(div({ width: w, height: h, alignItems: "center", fontFamily: fontStack("Inter"), fontWeight: 600, fontSize: 22, color: "#222222" }, text), { width: w, height: h, fonts: satoriFonts() });
|
|
646
|
+
return new Resvg(svg, { font: { loadSystemFonts: false } }).render().asPng();
|
|
647
|
+
}
|