@hypersocial/decks-cli 0.9.23 → 0.9.24

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.
Files changed (86) hide show
  1. package/package.json +1 -1
  2. package/skills/hyperdecks/SKILL.md +113 -67
  3. package/skills/hyperdecks/chat.md +11 -8
  4. package/skills/hyperdecks/terminal.md +82 -0
  5. package/src/bars.mjs +268 -0
  6. package/src/brand.mjs +51 -8
  7. package/src/check.mjs +311 -15
  8. package/src/checkRules.mjs +616 -27
  9. package/src/commands.mjs +157 -28
  10. package/src/deckSlug.mjs +41 -0
  11. package/src/export-video.mjs +20 -3
  12. package/src/files.mjs +35 -0
  13. package/src/guide.mjs +91 -13
  14. package/src/hosted.mjs +18 -8
  15. package/src/iconAliasNames.mjs +70 -0
  16. package/src/iconChoice.mjs +97 -0
  17. package/src/iconSets.mjs +89 -0
  18. package/src/iconSlots.mjs +48 -0
  19. package/src/icons.mjs +285 -45
  20. package/src/iconsCli.mjs +76 -0
  21. package/src/layoutRoutes.mjs +92 -0
  22. package/src/layouts.mjs +4 -4
  23. package/src/mediaPreviews.mjs +3 -1
  24. package/src/narrate.mjs +31 -4
  25. package/src/opening.mjs +9 -3
  26. package/src/ops.mjs +49 -0
  27. package/src/photosGuide.mjs +47 -0
  28. package/src/picturesGuide.mjs +53 -0
  29. package/src/play.mjs +3 -2
  30. package/src/presenter.mjs +3 -1
  31. package/src/release.mjs +13 -5
  32. package/src/runtime.mjs +9 -2
  33. package/src/scaffold.mjs +73 -13
  34. package/src/source.mjs +86 -14
  35. package/src/stockMedia.mjs +299 -0
  36. package/src/themeCli.mjs +41 -0
  37. package/src/themes.mjs +345 -0
  38. package/templates/icons/material.LICENSE +202 -0
  39. package/templates/icons/material.json.gz +0 -0
  40. package/templates/icons/phosphor.LICENSE +21 -0
  41. package/templates/icons/phosphor.json.gz +0 -0
  42. package/templates/icons/tabler.LICENSE +21 -0
  43. package/templates/icons/tabler.json.gz +0 -0
  44. package/templates/layouts/_deck.html +177 -36
  45. package/templates/layouts/bento.html +75 -0
  46. package/templates/layouts/chart.html +6 -4
  47. package/templates/layouts/closer.html +10 -3
  48. package/templates/layouts/compare.html +13 -4
  49. package/templates/layouts/cover-band.html +10 -8
  50. package/templates/layouts/cover-frame.html +9 -7
  51. package/templates/layouts/cover-poster.html +8 -5
  52. package/templates/layouts/cover-split.html +7 -6
  53. package/templates/layouts/cover-type.html +1 -0
  54. package/templates/layouts/features.html +9 -7
  55. package/templates/layouts/funnel.html +60 -0
  56. package/templates/layouts/hero.html +40 -22
  57. package/templates/layouts/lanes.html +63 -0
  58. package/templates/layouts/loop.html +81 -0
  59. package/templates/layouts/number.html +2 -1
  60. package/templates/layouts/phone-stage.html +68 -0
  61. package/templates/layouts/phones.html +1 -0
  62. package/templates/layouts/picture-full.html +48 -0
  63. package/templates/layouts/portrait.html +45 -0
  64. package/templates/layouts/product.html +7 -6
  65. package/templates/layouts/quote.html +2 -1
  66. package/templates/layouts/rail.html +71 -0
  67. package/templates/layouts/scorecard.html +70 -0
  68. package/templates/layouts/screens.html +85 -0
  69. package/templates/layouts/sentence.html +39 -0
  70. package/templates/layouts/split.html +22 -7
  71. package/templates/layouts/spotlight.html +77 -0
  72. package/templates/layouts/stack.html +83 -0
  73. package/templates/layouts/staircase.html +76 -0
  74. package/templates/layouts/strike.html +46 -0
  75. package/templates/layouts/tap-through.html +73 -0
  76. package/templates/layouts/team.html +3 -2
  77. package/templates/layouts/timeline.html +16 -6
  78. package/templates/layouts/two-bars.html +53 -0
  79. package/templates/layouts/two-by-two.html +71 -0
  80. package/templates/layouts/video-beats.html +66 -0
  81. package/templates/layouts/video-full.html +62 -0
  82. package/templates/layouts/wall.html +81 -0
  83. package/templates/layouts/zoom.html +144 -0
  84. package/templates/narrated-deck.html +12 -7
  85. package/templates/script.md +2 -2
  86. /package/templates/icons/{LICENSE → lucide.LICENSE} +0 -0
package/src/bars.mjs ADDED
@@ -0,0 +1,268 @@
1
+ // Bars baked into a picture or clip (docs/roadmap/pictures-and-clips-with-bars-baked-in-trimmed-on-upload-flag.md):
2
+ // a screenshot or recording exported letterboxed, with solid bars of one colour at its edges.
3
+ // Used by `asset add` (reports them, --trim uploads the content box), `check --layout` (warns when
4
+ // a slide's picture or clip still shows them) and the hosted upload_deck_asset (reports them for a PNG,
5
+ // and trims one, without ffmpeg: Convex's Node runtime has none, so PNG is read and written here).
6
+ //
7
+ // A bar is a run of whole rows or columns, from the edge inwards, that are one colour: every pixel but
8
+ // a few (OUTLIERS, for compression noise or a stray watermark pixel) within TOLERANCE of the colour of
9
+ // the outermost line. Any colour counts, not only black (ffmpeg's cropdetect sees black only; the test
10
+ // deck's bars were #0e0b18). A mostly dark photo has no such lines, and a thin designed border is under
11
+ // REPORT, so neither is reported.
12
+ import { execFile } from "node:child_process";
13
+ import { deflateSync, inflateSync } from "node:zlib";
14
+ import { streamSize } from "./mediaPreviews.mjs";
15
+ import { LOCAL_ONLY, MAX_PIXELS } from "./mediaPreviewMake.mjs";
16
+
17
+ export const BARS = {
18
+ tolerance: 24, // per channel, 0–255
19
+ outliers: 0.02, // share of a line's pixels allowed off the colour
20
+ report: 0.05, // a side's bar is reported past this share of the side
21
+ };
22
+ // check --layout's limit is checkRules.mjs's PICTURE_BARS (that file stays free of Node imports).
23
+
24
+ // The reference colour of a side is the median of up to this many pixels spread along its outermost line.
25
+ const SAMPLE = 1024;
26
+
27
+ // The bars of one RGBA frame ({ width, height, data }), in pixels: { left, right, top, bottom }.
28
+ // Bars on opposite sides never meet: a frame that is one colour throughout has none. A transparent
29
+ // edge is not a bar: a cut-out's clear margin shows the slide behind it, not a box. Pixels are
30
+ // compared in place (no per-pixel arrays), so a long thin picture costs its pixels and no more.
31
+ export function barsOf({ width, height, data }) {
32
+ const tol = BARS.tolerance;
33
+ // Line k from the edge, pixel i along it: its byte offset in `data`.
34
+ const run = (count, length, offset) => {
35
+ const step = Math.max(1, Math.floor(length / SAMPLE)), picks = [];
36
+ for (let i = 0; i < length; i += step) picks.push(offset(0, i));
37
+ const ref = [0, 1, 2, 3].map((c) => picks.map((o) => data[o + c]).sort((a, b) => a - b)[picks.length >> 1]);
38
+ if (ref[3] < 16) return 0;
39
+ const allowed = length * BARS.outliers;
40
+ let k = 0;
41
+ for (; k < count; k++) {
42
+ let off = 0;
43
+ for (let i = 0; i < length && off <= allowed; i++) {
44
+ const o = offset(k, i);
45
+ if (Math.abs(data[o] - ref[0]) > tol || Math.abs(data[o + 1] - ref[1]) > tol || Math.abs(data[o + 2] - ref[2]) > tol || Math.abs(data[o + 3] - ref[3]) > tol) off++;
46
+ }
47
+ if (off > allowed) break;
48
+ }
49
+ return k;
50
+ };
51
+ const sides = {
52
+ left: run(width, height, (k, i) => (i * width + k) * 4),
53
+ right: run(width, height, (k, i) => (i * width + width - 1 - k) * 4),
54
+ top: run(height, width, (k, i) => (k * width + i) * 4),
55
+ bottom: run(height, width, (k, i) => ((height - 1 - k) * width + i) * 4),
56
+ };
57
+ if (sides.left + sides.right >= width) sides.left = sides.right = 0;
58
+ if (sides.top + sides.bottom >= height) sides.top = sides.bottom = 0;
59
+ return sides;
60
+ }
61
+
62
+ // The bars as { width, height, content: { x, y, width, height }, bars: { left, right, top, bottom }
63
+ // (each a share of its side, 3 decimals), reported } from bars in pixels of a width×height picture.
64
+ // Letterboxing centres the content: an axis's bars count only as a pair, both past the report line and
65
+ // the smaller at least PAIR of the larger. A dark-themed screenshot whose own ground runs to its edges
66
+ // unevenly (the gallery's tour-agent.jpg: 5% left, 14% right, 8% top, 28% bottom) is not letterboxed.
67
+ const PAIR = 0.75;
68
+ export function barsResult(width, height, raw) {
69
+ const b = { ...raw };
70
+ const pair = (a, z, side) => Math.min(b[a], b[z]) > side * BARS.report && Math.min(b[a], b[z]) >= PAIR * Math.max(b[a], b[z]);
71
+ if (!pair("left", "right", width)) b.left = b.right = 0;
72
+ if (!pair("top", "bottom", height)) b.top = b.bottom = 0;
73
+ const share = (n, of) => Math.round((n / of) * 1000) / 1000;
74
+ const bars = { left: share(b.left, width), right: share(b.right, width), top: share(b.top, height), bottom: share(b.bottom, height) };
75
+ const content = { x: b.left, y: b.top, width: width - b.left - b.right, height: height - b.top - b.bottom };
76
+ return { width, height, content, bars, reported: Object.values(bars).some((s) => s > BARS.report) };
77
+ }
78
+
79
+ // Several frames of one clip: a bar is a bar only as wide as it is in every frame.
80
+ export function barsAcross(frames) {
81
+ const each = frames.map(barsOf);
82
+ return Object.fromEntries(["left", "right", "top", "bottom"].map((s) => [s, Math.min(...each.map((b) => b[s]))]));
83
+ }
84
+
85
+ // "bars: 21% left, 21% right (content 800x800 of 1422x800)", or null when none is reported.
86
+ export function barsLine(r) {
87
+ if (!r?.reported) return null;
88
+ const sides = ["left", "right", "top", "bottom"].filter((s) => r.bars[s] > BARS.report).map((s) => `${Math.round(r.bars[s] * 100)}% ${s}`);
89
+ return `bars: ${sides.join(", ")} (content ${r.content.width}x${r.content.height} of ${r.width}x${r.height})`;
90
+ }
91
+
92
+ // ---- ffmpeg (the CLI's portable build) --------------------------------------
93
+
94
+ const TIMEOUT_MS = 60_000;
95
+ // Resolves with { stdout, stderr } on exit 0; `probe` resolves either way (ffmpeg with no output exits 1).
96
+ function ffmpegRun(ffmpeg, args, { probe = false, timeout = TIMEOUT_MS } = {}) {
97
+ return new Promise((resolve, reject) => {
98
+ execFile(ffmpeg, args, { encoding: "buffer", maxBuffer: 64 * 1024 * 1024, timeout }, (err, stdout, stderr) => {
99
+ const text = stderr.toString("utf8");
100
+ if (err && !(probe && !err.killed && typeof err.code === "number")) {
101
+ const why = err.killed ? `ffmpeg timed out after ${timeout / 1000}s` : text.trim().split("\n").slice(-2).join(" ") || err.message;
102
+ return reject(new Error(why.slice(0, 300)));
103
+ }
104
+ resolve({ stdout, stderr: text });
105
+ });
106
+ });
107
+ }
108
+ const duration = (stderr) => {
109
+ const m = /Duration:\s*(\d+):(\d+):(\d+(?:\.\d+)?)/.exec(stderr ?? "");
110
+ return m ? Number(m[1]) * 3600 + Number(m[2]) * 60 + Number(m[3]) : null;
111
+ };
112
+ // Frames are read at most this many pixels on their long side: bars are a share, not a pixel count.
113
+ const READ_SIDE = { image: 4096, video: 960 };
114
+
115
+ // The bars of a picture or clip ffmpeg can open (a path; `allow` widens the protocols). A clip is read at
116
+ // a fifth, half and four fifths of its length. Returns barsResult in the file's own pixels.
117
+ export async function measureBars(ffmpeg, input, { video = false, allow = LOCAL_ONLY } = {}) {
118
+ const head = ["-hide_banner", "-nostdin", "-protocol_whitelist", allow, "-max_pixels", String(MAX_PIXELS)];
119
+ const info = await ffmpegRun(ffmpeg, [...head, "-i", input], { probe: true });
120
+ const size = streamSize(info.stderr);
121
+ if (!size) throw new Error(`no picture in ${input}`);
122
+ const scale = Math.min(1, READ_SIDE[video ? "video" : "image"] / Math.max(size.w, size.h));
123
+ const w = Math.max(1, Math.round(size.w * scale)), h = Math.max(1, Math.round(size.h * scale));
124
+ const seconds = video ? duration(info.stderr) : null;
125
+ const times = video && seconds ? [0.2, 0.5, 0.8].map((f) => (seconds * f).toFixed(3)) : [null];
126
+ const frames = [];
127
+ for (const t of times) {
128
+ const { stdout } = await ffmpegRun(ffmpeg, [...head, ...(t ? ["-ss", t] : []), "-i", input, "-map", "0:v:0", "-frames:v", "1", "-vf", `scale=${w}:${h}:flags=area,format=rgba`, "-f", "rawvideo", "pipe:1"]);
129
+ if (stdout.length === w * h * 4) frames.push({ width: w, height: h, data: stdout });
130
+ }
131
+ if (!frames.length) throw new Error(`ffmpeg read no frame of ${input}`);
132
+ const b = barsAcross(frames);
133
+ // Back to the file's pixels; rounding inward keeps a scaled-down edge out of the content box.
134
+ const up = (n, side) => Math.min(Math.ceil(n / scale), side);
135
+ const px = { left: up(b.left, size.w), right: up(b.right, size.w), top: up(b.top, size.h), bottom: up(b.bottom, size.h) };
136
+ if (px.left + px.right >= size.w) px.left = px.right = 0;
137
+ if (px.top + px.bottom >= size.h) px.top = px.bottom = 0;
138
+ return barsResult(size.w, size.h, px);
139
+ }
140
+
141
+ // What --trim writes for each kind it takes: PNG and JPEG stay, a WebP becomes a PNG, a clip an MP4.
142
+ export const TRIMS = {
143
+ "image/png": { ext: ".png", type: "image/png" },
144
+ "image/jpeg": { ext: ".jpg", type: "image/jpeg" },
145
+ "image/webp": { ext: ".png", type: "image/png" }, // the bundled ffmpeg reads WebP but cannot write it
146
+ "video/mp4": { ext: ".mp4", type: "video/mp4", video: true },
147
+ "video/quicktime": { ext: ".mp4", type: "video/mp4", video: true },
148
+ };
149
+ // Crop `input` to `content` (barsResult's box) into `out` (whose extension TRIMS gave). A clip is
150
+ // re-encoded as the CLI's video export encodes (H.264, preset fast, CRF 20), its sound copied.
151
+ // Returns the size written: { width, height }.
152
+ export async function trimFile(ffmpeg, input, out, content, { video = false } = {}) {
153
+ // H.264 in 4:2:0 wants even sides: an odd pixel is shaved off the content box's far edge.
154
+ const even = (n) => (video ? n - (n % 2) : n);
155
+ // An odd offset moves in a pixel (into the bar side's content edge, never into the bar).
156
+ const x = video ? content.x + (content.x % 2) : content.x, y = video ? content.y + (content.y % 2) : content.y;
157
+ const size = { width: even(content.width - (x - content.x)), height: even(content.height - (y - content.y)) };
158
+ const crop = `crop=${size.width}:${size.height}:${x}:${y}`;
159
+ const head = ["-hide_banner", "-nostdin", "-y", "-protocol_whitelist", LOCAL_ONLY, "-max_pixels", String(MAX_PIXELS), "-i", input];
160
+ const args = video
161
+ ? [...head, "-map", "0:v:0", "-map", "0:a?", "-vf", crop, "-c:v", "libx264", "-preset", "fast", "-crf", "20", "-pix_fmt", "yuv420p", "-c:a", "copy", "-movflags", "+faststart", out]
162
+ : [...head, "-map", "0:v:0", "-frames:v", "1", "-vf", crop, ...(/\.jpg$/.test(out) ? ["-q:v", "2"] : []), out];
163
+ await ffmpegRun(ffmpeg, args, { timeout: video ? 10 * 60_000 : TIMEOUT_MS });
164
+ return size;
165
+ }
166
+
167
+ // ---- PNG in plain JS (the hosted upload) --------------------------------------
168
+
169
+ // The hosted upload reads a PNG of at most this many pixels (8 M at 16 bits a sample), and no side
170
+ // past PNG_MAX_SIDE: Convex's Node action has 512 MB, and the RGBA copy alone is four bytes a pixel.
171
+ export const PNG_MAX_PIXELS = 16 * 1024 * 1024;
172
+ export const PNG_MAX_SIDE = 16384;
173
+ const SIGNATURE = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]);
174
+ const CHANNELS = { 0: 1, 2: 3, 3: 1, 4: 2, 6: 4 };
175
+
176
+ // An RGBA frame { width, height, data } from PNG bytes, or null for one this reader does not take
177
+ // (interlaced, over PNG_MAX_PIXELS, malformed). Grey, RGB, palette, grey+alpha and RGBA at 1–16 bits.
178
+ export function decodePng(bytes) {
179
+ const buf = Buffer.from(bytes.buffer ?? bytes, bytes.byteOffset ?? 0, bytes.byteLength ?? bytes.length);
180
+ if (buf.length < 33 || !buf.subarray(0, 8).equals(SIGNATURE)) return null;
181
+ let off = 8, ihdr = null, palette = null, trns = null;
182
+ const idat = [];
183
+ while (off + 8 <= buf.length) {
184
+ const len = buf.readUInt32BE(off), type = buf.toString("latin1", off + 4, off + 8), body = buf.subarray(off + 8, off + 8 + len);
185
+ if (body.length < len) return null;
186
+ if (type === "IHDR" && len < 13) return null;
187
+ if (type === "IHDR") ihdr = { width: body.readUInt32BE(0), height: body.readUInt32BE(4), depth: body[8], color: body[9], interlace: body[12] };
188
+ else if (type === "PLTE") palette = body;
189
+ else if (type === "tRNS") trns = body;
190
+ else if (type === "IDAT") idat.push(body);
191
+ else if (type === "IEND") break;
192
+ off += 12 + len;
193
+ }
194
+ if (!ihdr || ihdr.interlace || !(ihdr.color in CHANNELS) || !ihdr.width || !ihdr.height) return null;
195
+ const { width, height, depth, color } = ihdr;
196
+ if (width * height > PNG_MAX_PIXELS / (depth === 16 ? 2 : 1) || width > PNG_MAX_SIDE || height > PNG_MAX_SIDE || ![1, 2, 4, 8, 16].includes(depth) || (depth < 8 && color !== 0 && color !== 3) || (color === 3 && !palette)) return null;
197
+ const bitsPerPixel = CHANNELS[color] * depth, stride = Math.ceil((width * bitsPerPixel) / 8), bpp = Math.max(1, bitsPerPixel >> 3);
198
+ let raw;
199
+ try {
200
+ raw = inflateSync(Buffer.concat(idat), { maxOutputLength: (stride + 1) * height + 1 });
201
+ } catch {
202
+ return null;
203
+ }
204
+ if (raw.length < (stride + 1) * height) return null;
205
+ idat.length = 0; // the compressed chunks are not needed past here
206
+ const data = new Uint8Array(width * height * 4);
207
+ let prev = new Uint8Array(stride), cur = new Uint8Array(stride);
208
+ for (let y = 0; y < height; y++) {
209
+ const filter = raw[y * (stride + 1)], row = raw.subarray(y * (stride + 1) + 1, (y + 1) * (stride + 1));
210
+ for (let i = 0; i < stride; i++) {
211
+ const a = i >= bpp ? cur[i - bpp] : 0, b = prev[i], c = i >= bpp ? prev[i - bpp] : 0;
212
+ let v = row[i];
213
+ if (filter === 1) v += a;
214
+ else if (filter === 2) v += b;
215
+ else if (filter === 3) v += (a + b) >> 1;
216
+ else if (filter === 4) { const p = a + b - c, pa = Math.abs(p - a), pb = Math.abs(p - b), pc = Math.abs(p - c); v += pa <= pb && pa <= pc ? a : pb <= pc ? b : c; }
217
+ else if (filter !== 0) return null;
218
+ cur[i] = v & 0xff;
219
+ }
220
+ // One sample (the high byte at 16 bits, scaled up below 8).
221
+ const sample = (x, ch) => {
222
+ if (depth === 16) return cur[(x * CHANNELS[color] + ch) * 2];
223
+ if (depth === 8) return cur[x * CHANNELS[color] + ch];
224
+ const bit = x * depth, v = (cur[bit >> 3] >> (8 - depth - (bit & 7))) & ((1 << depth) - 1);
225
+ return color === 3 ? v : Math.round((v * 255) / ((1 << depth) - 1));
226
+ };
227
+ for (let x = 0; x < width; x++) {
228
+ const o = (y * width + x) * 4;
229
+ if (color === 3) {
230
+ const p = sample(x, 0);
231
+ data[o] = palette[p * 3] ?? 0; data[o + 1] = palette[p * 3 + 1] ?? 0; data[o + 2] = palette[p * 3 + 2] ?? 0; data[o + 3] = trns && p < trns.length ? trns[p] : 255;
232
+ } else if (color === 0 || color === 4) {
233
+ const g = sample(x, 0);
234
+ data[o] = data[o + 1] = data[o + 2] = g; data[o + 3] = color === 4 ? sample(x, 1) : 255;
235
+ } else {
236
+ data[o] = sample(x, 0); data[o + 1] = sample(x, 1); data[o + 2] = sample(x, 2); data[o + 3] = color === 6 ? sample(x, 3) : 255;
237
+ }
238
+ }
239
+ [prev, cur] = [cur, prev];
240
+ }
241
+ return { width, height, data };
242
+ }
243
+
244
+ const CRC_TABLE = Array.from({ length: 256 }, (_, n) => { let c = n; for (let k = 0; k < 8; k++) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1; return c >>> 0; });
245
+ const crc32 = (b) => { let c = 0xffffffff; for (const x of b) c = CRC_TABLE[(c ^ x) & 0xff] ^ (c >>> 8); return (c ^ 0xffffffff) >>> 0; };
246
+ function chunk(type, body) {
247
+ const head = Buffer.alloc(8); head.writeUInt32BE(body.length, 0); head.write(type, 4, "latin1");
248
+ const crc = Buffer.alloc(4); crc.writeUInt32BE(crc32(Buffer.concat([head.subarray(4), body])), 0);
249
+ return Buffer.concat([head, body, crc]);
250
+ }
251
+ // An 8-bit RGBA PNG of `frame` ({ width, height, data }), cropped to `box` ({ x, y, width, height }) when given.
252
+ export function encodePng(frame, box = { x: 0, y: 0, width: frame.width, height: frame.height }) {
253
+ const stride = box.width * 4, raw = Buffer.alloc((stride + 1) * box.height);
254
+ for (let y = 0; y < box.height; y++) {
255
+ const from = ((box.y + y) * frame.width + box.x) * 4;
256
+ raw[y * (stride + 1)] = 0;
257
+ Buffer.from(frame.data.buffer, frame.data.byteOffset + from, stride).copy(raw, y * (stride + 1) + 1);
258
+ }
259
+ const ihdr = Buffer.alloc(13);
260
+ ihdr.writeUInt32BE(box.width, 0); ihdr.writeUInt32BE(box.height, 4); ihdr[8] = 8; ihdr[9] = 6;
261
+ return Buffer.concat([SIGNATURE, chunk("IHDR", ihdr), chunk("IDAT", deflateSync(raw)), chunk("IEND", Buffer.alloc(0))]);
262
+ }
263
+
264
+ // The hosted upload's read of a PNG: barsResult, or null when the reader does not take it.
265
+ export function pngBars(bytes) {
266
+ const frame = decodePng(bytes);
267
+ return frame ? { frame, ...barsResult(frame.width, frame.height, barsOf(frame)) } : null;
268
+ }
package/src/brand.mjs CHANGED
@@ -7,6 +7,8 @@
7
7
  // an absent one keeps the template's own. designMd is guidance the agent reads;
8
8
  // only the tokens here touch the deck.
9
9
 
10
+ import { CSS_VALUE, isLight, mixHex } from "./themes.mjs";
11
+
10
12
  // Weights each role loads: the templates set body text at 500, titles at 700
11
13
  // and labels in mono at 400/700. Every pairing family has these on Google Fonts;
12
14
  // a display family outside the pairings loads what displayFace says it has.
@@ -133,6 +135,11 @@ export function accentInk(accent, ink, paper) {
133
135
  return onInk < 4.5 && contrast(accent, paper) > onInk ? "paper" : "ink";
134
136
  }
135
137
 
138
+ // A colour the deck's :root already has (the theme's), for a brand that sets only some.
139
+ const rootHex = (block, name) => new RegExp(`(?:^|[\\s;{])${name}\\s*:\\s*(#[0-9a-f]{3,6})\\b`, "i").exec(block)?.[1];
140
+ // A card the ink reads on, from the paper: a shade lighter than a light paper, a step towards the ink on a dark one.
141
+ export const deriveCard = (paper, ink) => (isLight(paper) ? mixHex("#ffffff", 0.6, paper) : mixHex(ink, 0.1, paper));
142
+
136
143
  const escapeAttr = (s) => s.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;");
137
144
  const fontStack = (family, role) => `"${family}", ${FALLBACK[role]}`;
138
145
  const LOGO_CSS = `
@@ -152,14 +159,20 @@ export function applyBrand(html, brand) {
152
159
  if (!root) throw new Error("applyBrand: the template has no :root block with --ink");
153
160
  let block = root[0];
154
161
  const set = (name, value) => {
155
- const decl = new RegExp(`(${name}\\s*:\\s*)[^;}]*`);
162
+ const decl = new RegExp(`(${name}\\s*:\\s*)${CSS_VALUE}`);
156
163
  block = decl.test(block) ? block.replace(decl, (_, k) => `${k}${value}`) : block.replace(/\s*\}$/, (end) => ` ${name}: ${value};${end}`);
157
164
  };
158
165
  for (const [k, v] of Object.entries(b.colors)) set(CSS_VAR[k], v);
159
166
  // Text on an accent ground reads --accent-ink (ink by default): a dark accent
160
167
  // that ink cannot be read on takes the paper instead, so a cover on the accent stays legible.
161
- const { accent = TEMPLATE_ACCENT, ink = TEMPLATE_INK, paper = TEMPLATE_PAPER } = b.colors;
162
- if ((b.colors.accent || b.colors.ink || b.colors.paper) && accentInk(accent, ink, paper) === "paper") set("--accent-ink", "var(--paper)");
168
+ // The deck's theme (layouts/_deck.html's House, or one themes.mjs wrote) fills the colours the brand leaves alone.
169
+ const { accent = rootHex(block, "--accent") ?? TEMPLATE_ACCENT, ink = rootHex(block, "--ink") ?? TEMPLATE_INK, paper = rootHex(block, "--paper") ?? TEMPLATE_PAPER } = b.colors;
170
+ if (b.colors.accent || b.colors.ink || b.colors.paper) {
171
+ set("--accent-ink", accentInk(accent, ink, paper) === "paper" ? "var(--paper)" : "var(--ink)");
172
+ // A dark brand on a light theme (or the reverse): a card the ink cannot be read on is made from the paper.
173
+ const card = rootHex(block, "--card");
174
+ if (!b.colors.card && card && contrast(card, ink) < 4.5) set("--card", deriveCard(paper, ink));
175
+ }
163
176
  if (b.fonts.body) set("--font", fontStack(b.fonts.body, "body"));
164
177
  if (b.fonts.mono) set("--mono", fontStack(b.fonts.mono, "mono"));
165
178
  if (b.fonts.display) {
@@ -180,7 +193,8 @@ export function applyBrand(html, brand) {
180
193
  if (kept && families.length < FONT_ROLES.length) links.unshift(kept[0]);
181
194
  out = kept ? out.replace(kept[0], () => links.join("\n")) : out.replace("</head>", () => `${links.join("\n")}\n</head>`);
182
195
  }
183
- if (b.logoUrl) {
196
+ // Once: theme set puts the brand back on a deck that already has its logo.
197
+ if (b.logoUrl && !/<img class="brand-logo"/.test(out)) {
184
198
  // The logo's rule goes in the style block that holds :root (not the loading state's).
185
199
  const close = out.indexOf("</style>", out.indexOf(block));
186
200
  if (close === -1) throw new Error("applyBrand: no </style> after the template's :root");
@@ -195,8 +209,32 @@ export function applyBrand(html, brand) {
195
209
  return out;
196
210
  }
197
211
 
198
- // The brand a new deck starts from: { brand: null } when asked not to
199
- // (brand: false / --no-brand) or when nothing is saved. A failed read does not
212
+ // Whether the saved brand goes on: asked (true) or refused (false) wins; left
213
+ // unsaid (undefined), only when no theme is named, since naming a theme means
214
+ // its own colours, fonts and no logo. `asked` is the tool's `brand` boolean or
215
+ // the CLI's flags read by brandAsked.
216
+ export function brandWanted(asked, themeNamed) {
217
+ return asked ?? !themeNamed;
218
+ }
219
+
220
+ // What the CLI's flags ask for: true (--brand), false (--no-brand, or brand:
221
+ // false from the stdio tool) or undefined (nothing said).
222
+ export function brandAsked(opts = {}) {
223
+ const yes = opts.brand === true;
224
+ const no = opts["no-brand"] === true || opts.brand === false;
225
+ if (yes && no) throw new Error("--brand and --no-brand disagree: pick one");
226
+ return yes ? true : no ? false : undefined;
227
+ }
228
+
229
+ // index.html without the logo applyBrand put on (its <img> and its rule), for
230
+ // theme set moving a branded deck to a theme's own look. The layouts' own
231
+ // .brand-logo rules (where a logo would sit) stay.
232
+ export function stripBrandLogo(html) {
233
+ return html.split(LOGO_CSS + "\n").join("").replace(/\n {2}<img class="brand-logo" [^>]*>/g, "");
234
+ }
235
+
236
+ // The brand a new deck starts from: { brand: null } when not wanted
237
+ // (brandWanted: --no-brand, or a named theme without --brand) or when nothing is saved. A failed read does not
200
238
  // stop the deck (making decks must not depend on the brand, e.g. during a
201
239
  // rollout); it starts in the template's look and readError says so.
202
240
  export async function brandForNewDeck(getBrand, useBrand) {
@@ -209,8 +247,12 @@ export async function brandForNewDeck(getBrand, useBrand) {
209
247
  }
210
248
 
211
249
  // What a new deck took from the brand, for the agent to read back.
212
- export function brandSummary(b, readError) {
213
- if (readError) return { applied: [], error: readError, note: `Could not read your saved brand (${readError}), so the deck has the template's look. Try get_brand (hyperdecks brand show); once it works, apply the brand's colours, fonts and logo yourself or make the deck again.` };
250
+ // `theme` is the theme a deck was asked for by name; `wanted` false says the
251
+ // brand was left off (brandWanted), so the summary says how to get it.
252
+ export function brandSummary(b, readError, { theme, wanted = true } = {}) {
253
+ if (!wanted && theme) return { applied: [], themeOwn: true, note: `The ${theme} theme was named, so the deck has its own colours and fonts and no logo; your saved brand was not applied. To recolour it in your brand, make it again with brand: true (CLI: --brand; a local deck folder: hyperdecks theme set <deck-dir> ${theme} --brand).` };
254
+ if (!wanted) return { applied: [], note: "Brand left off as asked (brand: false / --no-brand): the template's look." };
255
+ if (readError) return { applied: [], error: readError, note: `Could not read your saved brand (${readError}), so the deck has ${theme ? `the ${theme} theme's own look` : "the template's look"}. Try get_brand (hyperdecks brand show); once it works, apply the brand's colours, fonts and logo yourself or make the deck again.` };
214
256
  if (!b) return { applied: [], note: "No brand saved: the deck has the template's look. set_brand (hyperdecks brand set) saves one for every new deck." };
215
257
  const applied = [
216
258
  Object.keys(b.colors).length && "colors",
@@ -219,5 +261,6 @@ export function brandSummary(b, readError) {
219
261
  b.presenterStyle && "presenterStyle",
220
262
  ].filter(Boolean);
221
263
  if (!applied.length) return { applied, note: "Your brand has no colours, fonts or logo yet, so the deck has the template's look. Read its designMd (get_brand / hyperdecks brand show) for voice and do's and don'ts." };
264
+ if (theme) return { applied, note: `Your saved brand recolours the ${theme} theme (asked with brand: true / --brand); leave it out for the theme's own colours. Read its designMd (get_brand / hyperdecks brand show) for voice and do's and don'ts.` };
222
265
  return { applied, note: "Started from your saved brand. Read its designMd (get_brand / hyperdecks brand show) for voice and do's and don'ts; an explicit request for this deck wins over the brand." };
223
266
  }