@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/decks.js
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { deckSchema, slideSchema } from "./schema.js";
|
|
4
|
+
import { renderDeck, renderHash } from "./render.js";
|
|
5
|
+
import { lintDeck, platformsOf, renderContext } from "./lint.js";
|
|
6
|
+
import { tiktokIssues } from "./readability.js";
|
|
7
|
+
import { getTheme } from "./themes.js";
|
|
8
|
+
import { deckExists, loadDeck, parseWith, readJson, rendersDir, saveDeck, uniqueDeckId, UserError, writeAtomic } from "./workspace.js";
|
|
9
|
+
import { plainText } from "./text.js";
|
|
10
|
+
export const manifestFile = (ws, id) => join(rendersDir(ws, id), "manifest.json");
|
|
11
|
+
export const readManifest = (ws, id) => readJson(manifestFile(ws, id), null);
|
|
12
|
+
export function newDeck(ws, input, opts = {}) {
|
|
13
|
+
const s = ws.settings;
|
|
14
|
+
const firstTitle = input.slides?.find((x) => x.title)?.title;
|
|
15
|
+
const id = input.id ?? uniqueDeckId(ws, input.topic ?? (firstTitle ? plainText(firstTitle) : "deck"));
|
|
16
|
+
if (deckExists(ws, id) && !opts.overwrite)
|
|
17
|
+
throw new UserError(`deck "${id}" already exists; use deck_update to change it`);
|
|
18
|
+
const deck = parseWith(deckSchema, {
|
|
19
|
+
size: s.defaults.size,
|
|
20
|
+
theme: s.defaults.theme,
|
|
21
|
+
language: s.defaults.language,
|
|
22
|
+
...input,
|
|
23
|
+
id,
|
|
24
|
+
}, `deck ${id}`);
|
|
25
|
+
if (!getTheme(deck.theme))
|
|
26
|
+
throw new UserError(`unknown theme "${deck.theme}"; themes_list shows the 10 themes`);
|
|
27
|
+
return saveDeck(ws, deck);
|
|
28
|
+
}
|
|
29
|
+
export function updateDeck(ws, id, patch) {
|
|
30
|
+
const deck = loadDeck(ws, id);
|
|
31
|
+
if (deck.status === "published")
|
|
32
|
+
throw new UserError(`deck ${id} is already published; make a new deck (deck_create) instead of changing a posted one`);
|
|
33
|
+
const { slides, slide_edits, insert, remove, move, ...rest } = patch;
|
|
34
|
+
let next = slides ? slides.map((s) => parseWith(slideSchema, s, "slide")) : [...deck.slides];
|
|
35
|
+
for (const e of slide_edits ?? []) {
|
|
36
|
+
if (!next[e.index])
|
|
37
|
+
throw new UserError(`no slide ${e.index} (deck has ${next.length}, counted from 0)`);
|
|
38
|
+
const merged = { ...next[e.index], ...e.set };
|
|
39
|
+
for (const [k, v] of Object.entries(e.set))
|
|
40
|
+
if (v === null)
|
|
41
|
+
delete merged[k];
|
|
42
|
+
next[e.index] = parseWith(slideSchema, merged, `slide ${e.index}`);
|
|
43
|
+
}
|
|
44
|
+
if (remove?.length) {
|
|
45
|
+
const drop = new Set(remove);
|
|
46
|
+
for (const r of drop)
|
|
47
|
+
if (!next[r])
|
|
48
|
+
throw new UserError(`no slide ${r} to remove`);
|
|
49
|
+
next = next.filter((_, i) => !drop.has(i));
|
|
50
|
+
}
|
|
51
|
+
for (const ins of [...(insert ?? [])].sort((a, b) => a.index - b.index)) {
|
|
52
|
+
if (ins.index < 0 || ins.index > next.length)
|
|
53
|
+
throw new UserError(`cannot insert at ${ins.index}`);
|
|
54
|
+
next.splice(ins.index, 0, parseWith(slideSchema, ins.slide, "slide"));
|
|
55
|
+
}
|
|
56
|
+
if (move) {
|
|
57
|
+
if (!next[move.from] || move.to < 0 || move.to >= next.length)
|
|
58
|
+
throw new UserError("move: index out of range");
|
|
59
|
+
const [s] = next.splice(move.from, 1);
|
|
60
|
+
next.splice(move.to, 0, s);
|
|
61
|
+
}
|
|
62
|
+
const updated = { ...deck, ...rest, slides: next, status: "draft" };
|
|
63
|
+
if (rest.theme && !getTheme(rest.theme))
|
|
64
|
+
throw new UserError(`unknown theme "${rest.theme}"`);
|
|
65
|
+
return saveDeck(ws, parseWith(deckSchema, updated, `deck ${id}`));
|
|
66
|
+
}
|
|
67
|
+
export async function renderDeckById(ws, id) {
|
|
68
|
+
const deck = loadDeck(ws, id);
|
|
69
|
+
const lint = await lintDeck(ws, deck);
|
|
70
|
+
const ctx = renderContext(ws, deck);
|
|
71
|
+
const dir = rendersDir(ws, id);
|
|
72
|
+
const result = await renderDeck(deck, ctx, dir);
|
|
73
|
+
const manifest = { deck: id, hash: result.hash, files: result.files, size: deck.size, theme: deck.theme, rendered_at: new Date().toISOString() };
|
|
74
|
+
writeAtomic(manifestFile(ws, id), JSON.stringify(manifest, null, 2));
|
|
75
|
+
if (deck.status === "draft")
|
|
76
|
+
saveDeck(ws, { ...deck, status: "rendered" });
|
|
77
|
+
// What only a real render can see (the photo behind the words, images that failed to load),
|
|
78
|
+
// and TikTok's buttons checked against the lines as they were drawn.
|
|
79
|
+
const drawn = platformsOf(deck).has("tiktok") ? result.lines.flatMap((l, i) => tiktokIssues(l, deck.size, i)) : [];
|
|
80
|
+
for (const i of [...result.issues, ...drawn]) {
|
|
81
|
+
const list = i.level === "error" ? lint.errors : lint.warnings;
|
|
82
|
+
if (![...lint.errors, ...lint.warnings].some((x) => x.code === i.code && x.slide === i.slide))
|
|
83
|
+
list.push({ level: i.level, code: i.code, message: i.message, slide: i.slide });
|
|
84
|
+
}
|
|
85
|
+
lint.ok = lint.errors.length === 0;
|
|
86
|
+
return { ...result, dir, lint };
|
|
87
|
+
}
|
|
88
|
+
/** The renders on disk, only if they match the deck as it is now. */
|
|
89
|
+
export function freshRenders(ws, deck) {
|
|
90
|
+
const m = readManifest(ws, deck.id);
|
|
91
|
+
if (!m)
|
|
92
|
+
throw new UserError(`deck ${deck.id} is not rendered yet; run deck_render`);
|
|
93
|
+
const now = renderHash(deck, renderContext(ws, deck));
|
|
94
|
+
if (m.hash !== now)
|
|
95
|
+
throw new UserError(`deck ${deck.id} changed since it was rendered; run deck_render again and look at it before publishing`);
|
|
96
|
+
for (const f of m.files)
|
|
97
|
+
if (!existsSync(join(rendersDir(ws, deck.id), f)))
|
|
98
|
+
throw new UserError(`render ${f} is missing; run deck_render again`);
|
|
99
|
+
return m;
|
|
100
|
+
}
|
package/dist/fonts.d.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import opentype from "opentype.js";
|
|
2
|
+
import type { FontFamily, FontRole } from "./themes.js";
|
|
3
|
+
export interface SatoriFont {
|
|
4
|
+
name: string;
|
|
5
|
+
data: Buffer;
|
|
6
|
+
weight: 100 | 200 | 300 | 400 | 500 | 600 | 700 | 800 | 900;
|
|
7
|
+
style: "normal" | "italic";
|
|
8
|
+
}
|
|
9
|
+
/** The file bytes behind a parsed font, for the shaper. */
|
|
10
|
+
export declare const fontBytes: (f: opentype.Font) => {
|
|
11
|
+
key: string;
|
|
12
|
+
data: Buffer;
|
|
13
|
+
} | undefined;
|
|
14
|
+
/** All faces, for satori. Satori does not fall back between faces of one name, so every subset gets its own name. The same array is kept between calls so satori can cache the parsed fonts. */
|
|
15
|
+
export declare function satoriFonts(): SatoriFont[];
|
|
16
|
+
/** The CSS font-family list: the family's subsets, Inter's for letters it lacks, then any loaded script fonts (in the deck language's order). */
|
|
17
|
+
export declare function fontStack(family: FontFamily, locale?: string): string;
|
|
18
|
+
export type Pack = "jp" | "sc" | "tc" | "kr" | "ar" | "he" | "th" | "hi";
|
|
19
|
+
export declare const SCRIPTS: Record<Pack, {
|
|
20
|
+
pkg: string;
|
|
21
|
+
version: string;
|
|
22
|
+
subset: string;
|
|
23
|
+
name: string;
|
|
24
|
+
label: string;
|
|
25
|
+
}>;
|
|
26
|
+
export declare function orderedPacks(locale?: string): Pack[];
|
|
27
|
+
/** The script fonts a text needs. */
|
|
28
|
+
export declare function packsFor(text: string, locale?: string): Pack[];
|
|
29
|
+
/**
|
|
30
|
+
* Loads (downloading once, then from the cache) the fonts a text needs.
|
|
31
|
+
* Returns the scripts that could not be loaded, e.g. offline on first use.
|
|
32
|
+
*/
|
|
33
|
+
export declare function ensureScriptFonts(text: string, locale?: string): Promise<Pack[]>;
|
|
34
|
+
/** The font that draws `ch` for a role: the role's own, Inter, then the script fonts. */
|
|
35
|
+
export declare function pickFont(role: FontRole, ch: string, locale?: string): opentype.Font | null;
|
|
36
|
+
/** Characters no font here can draw. Whitespace and emoji are not counted: emoji are drawn as images. */
|
|
37
|
+
export declare function missingChars(role: FontRole, text: string, locale?: string): string[];
|
|
38
|
+
/** `fallback`: drawn in Inter because the theme's font lacks them. `missing`: no font here can draw them. */
|
|
39
|
+
export declare function glyphReport(role: FontRole, text: string, locale?: string): {
|
|
40
|
+
fallback: string[];
|
|
41
|
+
missing: string[];
|
|
42
|
+
};
|
|
43
|
+
export declare function isEmoji(ch: string): boolean;
|
|
44
|
+
/** Width of `text` in px at `size`, the way satori lays it out (advances plus letter spacing). */
|
|
45
|
+
export declare function measure(role: FontRole, text: string, size: number, locale?: string): number;
|
package/dist/fonts.js
ADDED
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
import { readFileSync, existsSync, mkdirSync, writeFileSync, renameSync } from "node:fs";
|
|
2
|
+
import { homedir } from "node:os";
|
|
3
|
+
import { createRequire } from "node:module";
|
|
4
|
+
import { dirname, join } from "node:path";
|
|
5
|
+
import opentype from "opentype.js";
|
|
6
|
+
import { COMPLEX, initShaper, shaperReady, shapedWidth } from "./shape.js";
|
|
7
|
+
const require = createRequire(import.meta.url);
|
|
8
|
+
/** Latin, Cyrillic, Greek and Vietnamese fonts are OFL and ship inside the package. */
|
|
9
|
+
const FAMILIES = {
|
|
10
|
+
"TikTok Sans": { pkg: "tiktok-sans", prefix: "tiktok-sans", weights: [500, 600, 700, 800], subsets: ["latin", "latin-ext", "cyrillic", "cyrillic-ext", "greek", "vietnamese"], styles: ["normal"] },
|
|
11
|
+
Inter: { pkg: "inter", prefix: "inter", weights: [400, 600, 700], subsets: ["latin", "latin-ext", "cyrillic", "cyrillic-ext", "greek", "vietnamese"], styles: ["normal"] },
|
|
12
|
+
Anton: { pkg: "anton", prefix: "anton", weights: [400], subsets: ["latin", "latin-ext", "vietnamese"], styles: ["normal"] },
|
|
13
|
+
"Instrument Serif": { pkg: "instrument-serif", prefix: "instrument-serif", weights: [400], subsets: ["latin", "latin-ext"], styles: ["normal", "italic"] },
|
|
14
|
+
};
|
|
15
|
+
let faces = null;
|
|
16
|
+
const fontData = new WeakMap();
|
|
17
|
+
/** The file bytes behind a parsed font, for the shaper. */
|
|
18
|
+
export const fontBytes = (f) => fontData.get(f);
|
|
19
|
+
function filesDir(pkg) {
|
|
20
|
+
return join(dirname(require.resolve(`@fontsource/${pkg}`)), "files");
|
|
21
|
+
}
|
|
22
|
+
function loadFaces() {
|
|
23
|
+
if (faces)
|
|
24
|
+
return faces;
|
|
25
|
+
const out = [];
|
|
26
|
+
for (const [family, spec] of Object.entries(FAMILIES)) {
|
|
27
|
+
const dir = filesDir(spec.pkg);
|
|
28
|
+
for (const weight of spec.weights)
|
|
29
|
+
for (const style of spec.styles)
|
|
30
|
+
for (const subset of spec.subsets) {
|
|
31
|
+
const file = join(dir, `${spec.prefix}-${subset}-${weight}-${style}.woff`);
|
|
32
|
+
if (!existsSync(file))
|
|
33
|
+
continue;
|
|
34
|
+
const data = readFileSync(file);
|
|
35
|
+
const font = opentype.parse(data.buffer.slice(data.byteOffset, data.byteOffset + data.byteLength));
|
|
36
|
+
fontData.set(font, { key: file, data });
|
|
37
|
+
out.push({ family, weight, style, subset, data, font });
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
faces = out;
|
|
41
|
+
return out;
|
|
42
|
+
}
|
|
43
|
+
const faceName = (family, subset) => (subset === "latin" ? family : `${family} ${subset}`);
|
|
44
|
+
let fontList = null;
|
|
45
|
+
/** All faces, for satori. Satori does not fall back between faces of one name, so every subset gets its own name. The same array is kept between calls so satori can cache the parsed fonts. */
|
|
46
|
+
export function satoriFonts() {
|
|
47
|
+
fontList ??= [
|
|
48
|
+
...loadFaces().map((f) => ({ name: faceName(f.family, f.subset), data: f.data, weight: f.weight, style: f.style })),
|
|
49
|
+
...scriptFaces.map((f) => ({ name: SCRIPTS[f.pack].name, data: f.data, weight: f.weight, style: "normal" })),
|
|
50
|
+
];
|
|
51
|
+
return fontList;
|
|
52
|
+
}
|
|
53
|
+
/** The CSS font-family list: the family's subsets, Inter's for letters it lacks, then any loaded script fonts (in the deck language's order). */
|
|
54
|
+
export function fontStack(family, locale) {
|
|
55
|
+
const names = (fam) => FAMILIES[fam].subsets.map((sub) => `"${faceName(fam, sub)}"`);
|
|
56
|
+
const scripts = orderedPacks(locale)
|
|
57
|
+
.filter((p) => scriptFaces.some((f) => f.pack === p))
|
|
58
|
+
.map((p) => `"${SCRIPTS[p].name}"`);
|
|
59
|
+
return [...names(family), ...(family === "Inter" ? [] : names("Inter")), ...scripts].join(", ");
|
|
60
|
+
}
|
|
61
|
+
export const SCRIPTS = {
|
|
62
|
+
jp: { pkg: "noto-sans-jp", version: "5.3.1", subset: "japanese", name: "Noto Sans JP", label: "Japanese" },
|
|
63
|
+
sc: { pkg: "noto-sans-sc", version: "5.3.1", subset: "chinese-simplified", name: "Noto Sans SC", label: "Simplified Chinese" },
|
|
64
|
+
tc: { pkg: "noto-sans-tc", version: "5.3.1", subset: "chinese-traditional", name: "Noto Sans TC", label: "Traditional Chinese" },
|
|
65
|
+
kr: { pkg: "noto-sans-kr", version: "5.3.1", subset: "korean", name: "Noto Sans KR", label: "Korean" },
|
|
66
|
+
ar: { pkg: "noto-sans-arabic", version: "5.3.0", subset: "arabic", name: "Noto Sans Arabic", label: "Arabic" },
|
|
67
|
+
he: { pkg: "noto-sans-hebrew", version: "5.3.0", subset: "hebrew", name: "Noto Sans Hebrew", label: "Hebrew" },
|
|
68
|
+
th: { pkg: "noto-sans-thai", version: "5.3.0", subset: "thai", name: "Noto Sans Thai", label: "Thai" },
|
|
69
|
+
hi: { pkg: "noto-sans-devanagari", version: "5.3.0", subset: "devanagari", name: "Noto Sans Devanagari", label: "Devanagari (Hindi, Marathi, Nepali)" },
|
|
70
|
+
};
|
|
71
|
+
const SCRIPT_WEIGHTS = [400, 700];
|
|
72
|
+
const scriptFaces = [];
|
|
73
|
+
/** Han characters are drawn differently in Japan, China, Taiwan and Korea: the deck's language picks the font. */
|
|
74
|
+
function hanPack(locale, text) {
|
|
75
|
+
const l = (locale ?? "").toLowerCase();
|
|
76
|
+
if (l.startsWith("ja"))
|
|
77
|
+
return "jp";
|
|
78
|
+
if (/^zh-(tw|hk|mo|hant)/.test(l))
|
|
79
|
+
return "tc";
|
|
80
|
+
if (l.startsWith("zh"))
|
|
81
|
+
return "sc";
|
|
82
|
+
if (l.startsWith("ko"))
|
|
83
|
+
return "kr";
|
|
84
|
+
if (/[\u3040-\u30ff]/u.test(text))
|
|
85
|
+
return "jp";
|
|
86
|
+
return "sc";
|
|
87
|
+
}
|
|
88
|
+
export function orderedPacks(locale) {
|
|
89
|
+
const first = hanPack(locale, "");
|
|
90
|
+
return [first, ...["jp", "sc", "tc", "kr", "ar", "he", "th", "hi"].filter((p) => p !== first)];
|
|
91
|
+
}
|
|
92
|
+
/** The script fonts a text needs. */
|
|
93
|
+
export function packsFor(text, locale) {
|
|
94
|
+
const need = new Set();
|
|
95
|
+
if (/[\u3040-\u30ff]/u.test(text))
|
|
96
|
+
need.add("jp");
|
|
97
|
+
if (/\p{Script=Hangul}/u.test(text))
|
|
98
|
+
need.add("kr");
|
|
99
|
+
if (/\p{Script=Han}/u.test(text))
|
|
100
|
+
need.add(hanPack(locale, text));
|
|
101
|
+
if (/\p{Script=Arabic}/u.test(text))
|
|
102
|
+
need.add("ar");
|
|
103
|
+
if (/\p{Script=Hebrew}/u.test(text))
|
|
104
|
+
need.add("he");
|
|
105
|
+
if (/\p{Script=Thai}/u.test(text))
|
|
106
|
+
need.add("th");
|
|
107
|
+
if (/\p{Script=Devanagari}/u.test(text))
|
|
108
|
+
need.add("hi");
|
|
109
|
+
return [...need];
|
|
110
|
+
}
|
|
111
|
+
function fontCache() {
|
|
112
|
+
return join(process.env.OPEN_SLIDESHOW_CACHE ?? join(homedir(), ".cache", "open-slideshow"), "fonts");
|
|
113
|
+
}
|
|
114
|
+
function parseFont(data) {
|
|
115
|
+
return opentype.parse(data.buffer.slice(data.byteOffset, data.byteOffset + data.byteLength));
|
|
116
|
+
}
|
|
117
|
+
async function fetchFace(pack, weight) {
|
|
118
|
+
const s = SCRIPTS[pack];
|
|
119
|
+
const file = join(fontCache(), `${s.pkg}-${s.version}-${s.subset}-${weight}.woff`);
|
|
120
|
+
if (existsSync(file))
|
|
121
|
+
return readFileSync(file);
|
|
122
|
+
if (process.env.OPEN_SLIDESHOW_OFFLINE === "1")
|
|
123
|
+
throw new Error("offline");
|
|
124
|
+
const res = await fetch(`https://cdn.jsdelivr.net/fontsource/fonts/${s.pkg}@${s.version}/${s.subset}-${weight}-normal.woff`, { signal: AbortSignal.timeout(60_000) });
|
|
125
|
+
if (!res.ok)
|
|
126
|
+
throw new Error(`HTTP ${res.status}`);
|
|
127
|
+
const data = Buffer.from(await res.arrayBuffer());
|
|
128
|
+
if (data.length > 15_000_000 || data.toString("latin1", 0, 4) !== "wOFF")
|
|
129
|
+
throw new Error("not a font file");
|
|
130
|
+
parseFont(data); // throws on a broken file, before it is cached
|
|
131
|
+
mkdirSync(fontCache(), { recursive: true });
|
|
132
|
+
const tmp = `${file}.${process.pid}.tmp`;
|
|
133
|
+
writeFileSync(tmp, data);
|
|
134
|
+
renameSync(tmp, file);
|
|
135
|
+
return data;
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Loads (downloading once, then from the cache) the fonts a text needs.
|
|
139
|
+
* Returns the scripts that could not be loaded, e.g. offline on first use.
|
|
140
|
+
*/
|
|
141
|
+
export async function ensureScriptFonts(text, locale) {
|
|
142
|
+
const failed = [];
|
|
143
|
+
if (COMPLEX.test(text))
|
|
144
|
+
await initShaper();
|
|
145
|
+
for (const pack of packsFor(text, locale)) {
|
|
146
|
+
if (scriptFaces.some((f) => f.pack === pack))
|
|
147
|
+
continue;
|
|
148
|
+
try {
|
|
149
|
+
const loaded = await Promise.all(SCRIPT_WEIGHTS.map(async (weight) => ({ pack, weight, data: await fetchFace(pack, weight) })));
|
|
150
|
+
for (const l of loaded) {
|
|
151
|
+
const font = parseFont(l.data);
|
|
152
|
+
fontData.set(font, { key: `${SCRIPTS[pack].pkg}-${l.weight}`, data: l.data });
|
|
153
|
+
scriptFaces.push({ ...l, font });
|
|
154
|
+
}
|
|
155
|
+
fontList = null;
|
|
156
|
+
glyphCache.clear();
|
|
157
|
+
}
|
|
158
|
+
catch {
|
|
159
|
+
failed.push(pack);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
return failed;
|
|
163
|
+
}
|
|
164
|
+
/** The font that draws `ch` for a role: the role's own, Inter, then the script fonts. */
|
|
165
|
+
export function pickFont(role, ch, locale) {
|
|
166
|
+
return fontForChar(role, ch) ?? fontForChar(fallbackRole(role), ch) ?? scriptFontFor(ch, scriptWeight(role), locale);
|
|
167
|
+
}
|
|
168
|
+
/** Anton is a heavy face at weight 400: other scripts beside it take the bold cut. */
|
|
169
|
+
const scriptWeight = (role) => (role.family === "Anton" ? 700 : role.weight);
|
|
170
|
+
function scriptFontFor(ch, weight, locale) {
|
|
171
|
+
for (const pack of orderedPacks(locale)) {
|
|
172
|
+
const faces = scriptFaces.filter((f) => f.pack === pack);
|
|
173
|
+
if (!faces.length)
|
|
174
|
+
continue;
|
|
175
|
+
const face = faces.reduce((best, f) => (Math.abs(f.weight - weight) < Math.abs(best.weight - weight) ? f : best));
|
|
176
|
+
if (face.font.charToGlyphIndex(ch) > 0)
|
|
177
|
+
return face.font;
|
|
178
|
+
}
|
|
179
|
+
return null;
|
|
180
|
+
}
|
|
181
|
+
function facesFor(role) {
|
|
182
|
+
const all = loadFaces().filter((f) => f.family === role.family && f.style === (role.italic ? "italic" : "normal"));
|
|
183
|
+
const weights = [...new Set(all.map((f) => f.weight))];
|
|
184
|
+
const weight = weights.reduce((best, w) => (Math.abs(w - role.weight) < Math.abs(best - role.weight) ? w : best), weights[0] ?? 400);
|
|
185
|
+
return all.filter((f) => f.weight === weight);
|
|
186
|
+
}
|
|
187
|
+
const glyphCache = new Map();
|
|
188
|
+
function fontForChar(role, ch) {
|
|
189
|
+
const key = `${role.family}|${role.weight}|${role.italic ? 1 : 0}|${ch}`;
|
|
190
|
+
if (glyphCache.has(key))
|
|
191
|
+
return glyphCache.get(key);
|
|
192
|
+
const found = facesFor(role).find((f) => f.font.charToGlyphIndex(ch) > 0)?.font ?? null;
|
|
193
|
+
glyphCache.set(key, found);
|
|
194
|
+
return found;
|
|
195
|
+
}
|
|
196
|
+
const fallbackRole = (role) => ({ family: "Inter", weight: role.weight });
|
|
197
|
+
const invisible = (ch) => ch === "\u200d" || ch === "\ufe0f";
|
|
198
|
+
/** Characters no font here can draw. Whitespace and emoji are not counted: emoji are drawn as images. */
|
|
199
|
+
export function missingChars(role, text, locale) {
|
|
200
|
+
return glyphReport(role, text, locale).missing;
|
|
201
|
+
}
|
|
202
|
+
/** `fallback`: drawn in Inter because the theme's font lacks them. `missing`: no font here can draw them. */
|
|
203
|
+
export function glyphReport(role, text, locale) {
|
|
204
|
+
const fallback = new Set();
|
|
205
|
+
const missing = new Set();
|
|
206
|
+
for (const ch of text) {
|
|
207
|
+
if (/\s/u.test(ch) || isEmoji(ch) || invisible(ch))
|
|
208
|
+
continue;
|
|
209
|
+
if (fontForChar(role, ch))
|
|
210
|
+
continue;
|
|
211
|
+
if (fontForChar(fallbackRole(role), ch))
|
|
212
|
+
fallback.add(ch);
|
|
213
|
+
else if (!scriptFontFor(ch, scriptWeight(role), locale))
|
|
214
|
+
missing.add(ch);
|
|
215
|
+
}
|
|
216
|
+
return { fallback: [...fallback], missing: [...missing] };
|
|
217
|
+
}
|
|
218
|
+
export function isEmoji(ch) {
|
|
219
|
+
return /\p{Extended_Pictographic}|\p{Regional_Indicator}/u.test(ch);
|
|
220
|
+
}
|
|
221
|
+
/** Width of `text` in px at `size`, the way satori lays it out (advances plus letter spacing). */
|
|
222
|
+
export function measure(role, text, size, locale) {
|
|
223
|
+
let width = 0;
|
|
224
|
+
const tracking = (role.tracking ?? 0) * size;
|
|
225
|
+
// Joined letters are narrower than the letters alone: measure them as the shaper draws them.
|
|
226
|
+
if (COMPLEX.test(text) && shaperReady())
|
|
227
|
+
return shapedWidth(role, text, size, locale);
|
|
228
|
+
for (const ch of text) {
|
|
229
|
+
if (isEmoji(ch)) {
|
|
230
|
+
width += size * 1.15;
|
|
231
|
+
}
|
|
232
|
+
else if (ch === "" || ch === "️") {
|
|
233
|
+
continue;
|
|
234
|
+
}
|
|
235
|
+
else {
|
|
236
|
+
// A letter no font has (e.g. its font could not be downloaded) gets an average width.
|
|
237
|
+
const font = pickFont(role, ch, locale);
|
|
238
|
+
width += font ? (font.getAdvanceWidth(ch, size, { kerning: false }) || size * 0.3) : size * 0.6;
|
|
239
|
+
}
|
|
240
|
+
width += tracking;
|
|
241
|
+
}
|
|
242
|
+
return width;
|
|
243
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { type Workspace } from "./workspace.js";
|
|
2
|
+
export interface Gallery {
|
|
3
|
+
sheet: string;
|
|
4
|
+
themes: {
|
|
5
|
+
id: string;
|
|
6
|
+
name: string;
|
|
7
|
+
design: string;
|
|
8
|
+
description: string;
|
|
9
|
+
}[];
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Every theme side by side. With a deck: its own cover and second slide; without: a sample about the app.
|
|
13
|
+
* The user looks at one picture and picks a theme.
|
|
14
|
+
*/
|
|
15
|
+
export declare function themeGallery(ws: Workspace, opts?: {
|
|
16
|
+
deck?: string;
|
|
17
|
+
slides?: number;
|
|
18
|
+
}): Promise<Gallery>;
|
|
19
|
+
export interface TemplateGallery {
|
|
20
|
+
sheet: string;
|
|
21
|
+
theme: string;
|
|
22
|
+
templates: {
|
|
23
|
+
id: string;
|
|
24
|
+
name: string;
|
|
25
|
+
description: string;
|
|
26
|
+
}[];
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Every template as one row of example slides, in one theme, so the user sees the shapes and picks.
|
|
30
|
+
* Photo themes use the workspace's own photos when there are some, otherwise a made-up photo.
|
|
31
|
+
*/
|
|
32
|
+
export declare function templateGallery(ws: Workspace, opts?: {
|
|
33
|
+
theme?: string;
|
|
34
|
+
templates?: string[];
|
|
35
|
+
}): Promise<TemplateGallery>;
|
package/dist/gallery.js
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { join } from "node:path";
|
|
2
|
+
import sharp from "sharp";
|
|
3
|
+
import { contactSheet, laySlide, rasterize, tipNumbers } from "./render.js";
|
|
4
|
+
import { renderContext } from "./lint.js";
|
|
5
|
+
import { listImages } from "./images.js";
|
|
6
|
+
import { deckSchema, SIZES } from "./schema.js";
|
|
7
|
+
import { THEMES, getTheme } from "./themes.js";
|
|
8
|
+
import { TEMPLATES, assignSlots, getTemplate } from "./templates.js";
|
|
9
|
+
import { UserError } from "./workspace.js";
|
|
10
|
+
import { loadDeck, writeAtomic } from "./workspace.js";
|
|
11
|
+
/** A soft made-up photo so photo themes can be seen without any real image. */
|
|
12
|
+
async function samplePhoto() {
|
|
13
|
+
const svg = `<svg xmlns="http://www.w3.org/2000/svg" width="1200" height="1600"><defs><linearGradient id="s" x1="0" y1="0" x2="0" y2="1"><stop offset="0" stop-color="#f3c9a0"/><stop offset="0.55" stop-color="#c98a6a"/><stop offset="1" stop-color="#3d3a4a"/></linearGradient><radialGradient id="g" cx="0.5" cy="0.62" r="0.5"><stop offset="0" stop-color="#ffe6b8" stop-opacity="0.9"/><stop offset="1" stop-color="#ffe6b8" stop-opacity="0"/></radialGradient></defs><rect width="1200" height="1600" fill="url(#s)"/><circle cx="600" cy="990" r="380" fill="url(#g)"/><path d="M0 1250 Q300 1150 600 1230 T1200 1200 V1600 H0Z" fill="#2b2a36"/></svg>`;
|
|
14
|
+
return sharp(Buffer.from(svg)).jpeg({ quality: 88 }).toBuffer();
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Every theme side by side. With a deck: its own cover and second slide; without: a sample about the app.
|
|
18
|
+
* The user looks at one picture and picks a theme.
|
|
19
|
+
*/
|
|
20
|
+
export async function themeGallery(ws, opts = {}) {
|
|
21
|
+
let deck;
|
|
22
|
+
const own = listImages(ws).find((m) => m.width >= 600 && m.source !== "app-store");
|
|
23
|
+
let tmpPhoto;
|
|
24
|
+
if (opts.deck)
|
|
25
|
+
deck = loadDeck(ws, opts.deck);
|
|
26
|
+
else {
|
|
27
|
+
const name = ws.settings.brand.name ?? ws.settings.app.name ?? "your app";
|
|
28
|
+
if (!own) {
|
|
29
|
+
tmpPhoto = join(ws.state, "cache", "sample-photo.jpg");
|
|
30
|
+
writeAtomic(tmpPhoto, await samplePhoto());
|
|
31
|
+
}
|
|
32
|
+
const image = own ? own.ref : tmpPhoto;
|
|
33
|
+
const slides = [
|
|
34
|
+
{ kind: "cover", title: `3 things I wish I knew *before* ${name}`, image },
|
|
35
|
+
{ kind: "tip", title: "Start *smaller* than you think", body: "Five minutes a day beats one perfect hour a week.", image },
|
|
36
|
+
{ kind: "app", title: `This is why I use *${name}*`, body: ws.settings.brand.cta ?? "Free on the App Store." },
|
|
37
|
+
];
|
|
38
|
+
deck = deckSchema.parse({ id: "theme-preview", size: ws.settings.defaults.size, slides });
|
|
39
|
+
}
|
|
40
|
+
const count = Math.min(opts.slides ?? 2, deck.slides.length);
|
|
41
|
+
const buffers = [];
|
|
42
|
+
const labels = [];
|
|
43
|
+
const resolve = (ref) => (tmpPhoto && ref === tmpPhoto ? tmpPhoto : null);
|
|
44
|
+
for (const theme of THEMES) {
|
|
45
|
+
const ctx = { ...renderContext(ws, { ...deck, theme: theme.id }), images: new Map() };
|
|
46
|
+
const base = ctx.resolveImage;
|
|
47
|
+
ctx.resolveImage = (ref) => resolve(ref) ?? base(ref);
|
|
48
|
+
const nums = tipNumbers(deck.slides);
|
|
49
|
+
for (let i = 0; i < count; i++) {
|
|
50
|
+
const laid = await laySlide(ctx, { deck, slide: deck.slides[i], index: i, tipNumber: nums[i] });
|
|
51
|
+
buffers.push(await rasterize(laid.tree, deck.size, 80));
|
|
52
|
+
labels.push(i === 0 ? theme.id : "");
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
const sheet = join(ws.state, "cache", opts.deck ? `themes-${opts.deck}.jpg` : "themes.jpg");
|
|
56
|
+
writeAtomic(sheet, await contactSheet(buffers, deck.size, count * 2, labels));
|
|
57
|
+
return { sheet, themes: THEMES.map((t) => ({ id: t.id, name: t.name, design: t.design, description: t.description })) };
|
|
58
|
+
}
|
|
59
|
+
/** Which slot of the template each example slide fills, for the labels under the preview. */
|
|
60
|
+
function slotLabels(t, slides) {
|
|
61
|
+
const slots = assignSlots(t, slides.map((s) => s.kind));
|
|
62
|
+
return slides.map((_, i) => (slots ? t.slots[slots[i]].label : ""));
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Every template as one row of example slides, in one theme, so the user sees the shapes and picks.
|
|
66
|
+
* Photo themes use the workspace's own photos when there are some, otherwise a made-up photo.
|
|
67
|
+
*/
|
|
68
|
+
export async function templateGallery(ws, opts = {}) {
|
|
69
|
+
const themeId = opts.theme ?? ws.settings.defaults.theme;
|
|
70
|
+
if (!getTheme(themeId))
|
|
71
|
+
throw new UserError(`unknown theme "${themeId}"`);
|
|
72
|
+
const list = opts.templates?.length ? opts.templates.map((id) => getTemplate(id) ?? (() => { throw new UserError(`unknown template "${id}"`); })()) : TEMPLATES;
|
|
73
|
+
const name = ws.settings.brand.name ?? ws.settings.app.name ?? "your app";
|
|
74
|
+
const photos = listImages(ws).filter((m) => m.width >= 600 && m.source !== "app-store").map((m) => m.ref);
|
|
75
|
+
let fallback;
|
|
76
|
+
if (!photos.length) {
|
|
77
|
+
fallback = join(ws.state, "cache", "sample-photo.jpg");
|
|
78
|
+
writeAtomic(fallback, await samplePhoto());
|
|
79
|
+
}
|
|
80
|
+
const size = ws.settings.defaults.size;
|
|
81
|
+
const columns = Math.max(...list.map((t) => t.example(name).length));
|
|
82
|
+
const { width, height } = SIZES[size];
|
|
83
|
+
const blank = await sharp({ create: { width, height, channels: 3, background: "#f2f2f2" } }).jpeg().toBuffer();
|
|
84
|
+
const buffers = [];
|
|
85
|
+
const labels = [];
|
|
86
|
+
let p = 0;
|
|
87
|
+
for (const t of list) {
|
|
88
|
+
const slides = t.example(name).map((s) => ({ ...s, image: s.kind === "app" ? undefined : photos.length ? photos[p++ % photos.length] : fallback }));
|
|
89
|
+
const deck = deckSchema.parse({ id: "template-preview", size, theme: themeId, language: ws.settings.defaults.language, template: t.id, slides });
|
|
90
|
+
const ctx = { ...renderContext(ws, deck), images: new Map() };
|
|
91
|
+
const base = ctx.resolveImage;
|
|
92
|
+
ctx.resolveImage = (ref) => (fallback && ref === fallback ? fallback : base(ref));
|
|
93
|
+
const nums = tipNumbers(deck.slides);
|
|
94
|
+
const parts = slotLabels(t, deck.slides);
|
|
95
|
+
for (let i = 0; i < columns; i++) {
|
|
96
|
+
if (i >= deck.slides.length) {
|
|
97
|
+
buffers.push(blank);
|
|
98
|
+
labels.push("");
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
const laid = await laySlide(ctx, { deck, slide: deck.slides[i], index: i, tipNumber: nums[i] });
|
|
102
|
+
buffers.push(await rasterize(laid.tree, size, 80));
|
|
103
|
+
labels.push(i === 0 ? `${t.name.toUpperCase()} · ${parts[i]}` : parts[i]);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
const sheet = join(ws.state, "cache", `templates-${themeId}.jpg`);
|
|
107
|
+
writeAtomic(sheet, await contactSheet(buffers, size, columns, labels));
|
|
108
|
+
return { sheet, theme: themeId, templates: list.map((t) => ({ id: t.id, name: t.name, description: t.description })) };
|
|
109
|
+
}
|
package/dist/ideas.d.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { type Workspace } from "./workspace.js";
|
|
3
|
+
export declare const ideaSchema: z.ZodObject<{
|
|
4
|
+
id: z.ZodString;
|
|
5
|
+
text: z.ZodString;
|
|
6
|
+
/** Where it came from: review, comment, listing, playbook, channel, viral, user. */
|
|
7
|
+
source: z.ZodString;
|
|
8
|
+
/** The words behind it (a review, a comment), so the slides can stay true to them. */
|
|
9
|
+
evidence: z.ZodOptional<z.ZodString>;
|
|
10
|
+
status: z.ZodDefault<z.ZodEnum<["new", "used", "skipped"]>>;
|
|
11
|
+
deck: z.ZodOptional<z.ZodString>;
|
|
12
|
+
added_at: z.ZodString;
|
|
13
|
+
}, "strict", z.ZodTypeAny, {
|
|
14
|
+
status: "new" | "used" | "skipped";
|
|
15
|
+
text: string;
|
|
16
|
+
source: string;
|
|
17
|
+
id: string;
|
|
18
|
+
added_at: string;
|
|
19
|
+
deck?: string | undefined;
|
|
20
|
+
evidence?: string | undefined;
|
|
21
|
+
}, {
|
|
22
|
+
text: string;
|
|
23
|
+
source: string;
|
|
24
|
+
id: string;
|
|
25
|
+
added_at: string;
|
|
26
|
+
status?: "new" | "used" | "skipped" | undefined;
|
|
27
|
+
deck?: string | undefined;
|
|
28
|
+
evidence?: string | undefined;
|
|
29
|
+
}>;
|
|
30
|
+
export type Idea = z.infer<typeof ideaSchema>;
|
|
31
|
+
export declare const ideasFile: (ws: Workspace) => string;
|
|
32
|
+
export declare function readIdeas(ws: Workspace): Idea[];
|
|
33
|
+
/** Adds ideas that are not in the queue yet (same words = same idea). Returns the ones added. */
|
|
34
|
+
export declare function addIdeas(ws: Workspace, fresh: Omit<Idea, "id" | "status" | "added_at">[], now?: Date, max?: number): Idea[];
|
|
35
|
+
export declare function updateIdea(ws: Workspace, id: string, patch: {
|
|
36
|
+
status?: Idea["status"];
|
|
37
|
+
deck?: string;
|
|
38
|
+
}): Idea;
|
|
39
|
+
/**
|
|
40
|
+
* Turns what the repo and the store already know into slideshow ideas: questions people keep
|
|
41
|
+
* asking, what reviewers love or miss, the listing's own features, playbook wins and channel
|
|
42
|
+
* topics not used yet. Nothing is posted or sent anywhere.
|
|
43
|
+
*/
|
|
44
|
+
export declare function collectIdeas(ws: Workspace, opts?: {
|
|
45
|
+
offline?: boolean;
|
|
46
|
+
limit?: number;
|
|
47
|
+
}): Promise<{
|
|
48
|
+
added: Idea[];
|
|
49
|
+
found: number;
|
|
50
|
+
}>;
|