@hattoriai/poney-render 0.0.0-stage → 0.1.1
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 +13 -2
- package/dist/index.d.ts +37 -0
- package/dist/index.js +157 -0
- package/dist/index.js.map +1 -0
- package/package.json +51 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Rui Freitas
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,14 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @hattoriai/poney-render
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Renders any view or walkthrough scene of a Poney document to SVG or PNG in Node, with the real fonts: self-contained SVG (no CSS variables), or PNG at twice the size.
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
import { renderScene, renderScenePng } from '@hattoriai/poney-render'
|
|
7
|
+
|
|
8
|
+
const { svg } = await renderScene(doc, { view: 'architecture' })
|
|
9
|
+
const { png } = await renderScenePng(doc, { scene: 'checkout', theme: 'dark' })
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
From a terminal, the `@hattoriai/poney` CLI: `poney render doc.poney --view architecture --format svg`.
|
|
13
|
+
|
|
14
|
+
Part of [Poney](https://github.com/hattoriai/poney): the format, the renderers, and tools to explain software. MIT licensed.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { PoneyDocument } from '@hattoriai/poney-core';
|
|
2
|
+
import { Mode, ThemeName } from '@hattoriai/poney-react';
|
|
3
|
+
|
|
4
|
+
/** Measures text with the real font files. Call before laying out in Node. */
|
|
5
|
+
declare function useNodeTextMeasurer(): void;
|
|
6
|
+
interface RenderSceneOptions {
|
|
7
|
+
/** Walkthrough id. Default: the first. */
|
|
8
|
+
walkthrough?: string;
|
|
9
|
+
/** Scene id, or "overview". Default: the overview, else the first scene with a view. */
|
|
10
|
+
scene?: string;
|
|
11
|
+
/** Render a view directly, outside any walkthrough. */
|
|
12
|
+
view?: string;
|
|
13
|
+
mode?: Mode;
|
|
14
|
+
theme?: ThemeName;
|
|
15
|
+
/** Width of the output in pixels. Height follows the scene. */
|
|
16
|
+
width?: number;
|
|
17
|
+
/** Embed the fonts in the SVG, so it looks the same everywhere. */
|
|
18
|
+
embedFonts?: boolean;
|
|
19
|
+
/** Write the numbered callout notes under the picture. Default: true. */
|
|
20
|
+
legend?: boolean;
|
|
21
|
+
}
|
|
22
|
+
interface RenderedScene {
|
|
23
|
+
svg: string;
|
|
24
|
+
width: number;
|
|
25
|
+
height: number;
|
|
26
|
+
title: string;
|
|
27
|
+
}
|
|
28
|
+
declare function renderScene(doc: PoneyDocument, options?: RenderSceneOptions): Promise<RenderedScene>;
|
|
29
|
+
/** Renders a scene to PNG at twice its size, with the bundled fonts. */
|
|
30
|
+
declare function renderScenePng(doc: PoneyDocument, options?: RenderSceneOptions): Promise<{
|
|
31
|
+
png: Buffer;
|
|
32
|
+
width: number;
|
|
33
|
+
height: number;
|
|
34
|
+
title: string;
|
|
35
|
+
}>;
|
|
36
|
+
|
|
37
|
+
export { type RenderSceneOptions, type RenderedScene, renderScene, renderScenePng, useNodeTextMeasurer };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import { readFileSync } from 'fs';
|
|
2
|
+
import { createRequire } from 'module';
|
|
3
|
+
import { renderToStaticMarkup } from 'react-dom/server';
|
|
4
|
+
import * as fontkit from 'fontkit';
|
|
5
|
+
import { indexOf, marksOf, plainText, parseRef } from '@hattoriai/poney-core';
|
|
6
|
+
import { setTextMeasurer, layoutView, wrap } from '@hattoriai/poney-layout';
|
|
7
|
+
import { findScene, diagramFor, sceneLayoutOptions, sceneFrame, fit, storyState, RenderProvider, StoryProvider, SceneStory, THEMES, stepLetter, FONTS } from '@hattoriai/poney-react';
|
|
8
|
+
import { jsx } from 'react/jsx-runtime';
|
|
9
|
+
|
|
10
|
+
// src/index.tsx
|
|
11
|
+
var STORY_TYPES = ["map", "sequence", "state"];
|
|
12
|
+
var require2 = createRequire(import.meta.url);
|
|
13
|
+
var SANS_FILES = {
|
|
14
|
+
400: "@fontsource/hanken-grotesk/files/hanken-grotesk-latin-400-normal.woff2",
|
|
15
|
+
500: "@fontsource/hanken-grotesk/files/hanken-grotesk-latin-500-normal.woff2",
|
|
16
|
+
600: "@fontsource/hanken-grotesk/files/hanken-grotesk-latin-600-normal.woff2",
|
|
17
|
+
700: "@fontsource/hanken-grotesk/files/hanken-grotesk-latin-700-normal.woff2"
|
|
18
|
+
};
|
|
19
|
+
var MONO_FILE = "@fontsource/jetbrains-mono/files/jetbrains-mono-latin-400-normal.woff2";
|
|
20
|
+
var fonts;
|
|
21
|
+
function loadFonts() {
|
|
22
|
+
if (fonts) return fonts;
|
|
23
|
+
const files = {};
|
|
24
|
+
const sans = {};
|
|
25
|
+
for (const [w, f] of Object.entries(SANS_FILES)) {
|
|
26
|
+
files[`sans-${w}`] = readFileSync(require2.resolve(f));
|
|
27
|
+
sans[Number(w)] = fontkit.create(files[`sans-${w}`]);
|
|
28
|
+
}
|
|
29
|
+
files.mono = readFileSync(require2.resolve(MONO_FILE));
|
|
30
|
+
fonts = { sans, mono: fontkit.create(files.mono), files };
|
|
31
|
+
return fonts;
|
|
32
|
+
}
|
|
33
|
+
var nearest = (weight) => [400, 500, 600, 700].reduce((a, b) => Math.abs(b - weight) < Math.abs(a - weight) ? b : a);
|
|
34
|
+
function useNodeTextMeasurer() {
|
|
35
|
+
const f = loadFonts();
|
|
36
|
+
setTextMeasurer((text, size, weight, family) => {
|
|
37
|
+
const font = family === "mono" ? f.mono : f.sans[nearest(weight)];
|
|
38
|
+
return font.layout(text).advanceWidth / font.unitsPerEm * size;
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
var PAD = 40;
|
|
42
|
+
function describeRef(idx, key) {
|
|
43
|
+
const r = parseRef(key);
|
|
44
|
+
if (!r || !idx.exists(r)) return void 0;
|
|
45
|
+
return idx.describe(r).title;
|
|
46
|
+
}
|
|
47
|
+
async function renderScene(doc, options = {}) {
|
|
48
|
+
useNodeTextMeasurer();
|
|
49
|
+
const idx = indexOf(doc);
|
|
50
|
+
let view;
|
|
51
|
+
let focus = [];
|
|
52
|
+
let callouts = [];
|
|
53
|
+
let notes = [];
|
|
54
|
+
let title = "";
|
|
55
|
+
let sceneData;
|
|
56
|
+
let walkthrough = options.walkthrough ? doc.walkthroughs.find((w) => w.id === options.walkthrough) : doc.walkthroughs[0];
|
|
57
|
+
if (options.view) {
|
|
58
|
+
view = idx.views.get(options.view);
|
|
59
|
+
if (!view) throw new Error(`No view "${options.view}". Views: ${doc.views.map((v) => v.id).join(", ")}`);
|
|
60
|
+
title = view.title;
|
|
61
|
+
walkthrough = options.walkthrough ? walkthrough : void 0;
|
|
62
|
+
} else {
|
|
63
|
+
const found = findScene(doc, options.walkthrough, options.scene);
|
|
64
|
+
if (!found) {
|
|
65
|
+
const w = walkthrough;
|
|
66
|
+
const ids = w ? ["overview", ...w.scenes.filter((s) => s.view).map((s) => s.id)].join(", ") : "none";
|
|
67
|
+
throw new Error(`No scene "${options.scene ?? "(default)"}" with a view. Scenes: ${ids}`);
|
|
68
|
+
}
|
|
69
|
+
walkthrough = found.walkthrough;
|
|
70
|
+
view = idx.views.get(found.viewId);
|
|
71
|
+
focus = found.focus;
|
|
72
|
+
callouts = found.callouts;
|
|
73
|
+
notes = found.notes;
|
|
74
|
+
title = found.title;
|
|
75
|
+
sceneData = found.scene;
|
|
76
|
+
}
|
|
77
|
+
const story = STORY_TYPES.includes(view.type);
|
|
78
|
+
const module = story ? void 0 : diagramFor(view);
|
|
79
|
+
if (!story && !module) throw new Error(`No renderer for "${view.id}", a ${view.type === "extension" ? view.extension : view.type} view. Register one with registerDiagram.`);
|
|
80
|
+
const scene = await layoutView(doc, view, story ? sceneLayoutOptions(view, walkthrough, focus) : {});
|
|
81
|
+
const frame = story ? sceneFrame(scene, view, focus, idx, walkthrough) : { x: 0, y: 0, w: scene.width, h: scene.height };
|
|
82
|
+
const natural = { w: frame.w + PAD * 2, h: frame.h + PAD * 2 };
|
|
83
|
+
const width = Math.round(options.width ?? Math.min(1600, Math.max(720, natural.w)));
|
|
84
|
+
const height = Math.round(width * natural.h / natural.w);
|
|
85
|
+
const vb = fit(frame, width, height, PAD, 10);
|
|
86
|
+
const theme = options.theme ?? "light";
|
|
87
|
+
const storyValue = storyState({
|
|
88
|
+
marks: walkthrough ? marksOf(walkthrough) : /* @__PURE__ */ new Map(),
|
|
89
|
+
mode: options.mode ?? "change",
|
|
90
|
+
view,
|
|
91
|
+
focus,
|
|
92
|
+
callouts,
|
|
93
|
+
idx,
|
|
94
|
+
walkthrough,
|
|
95
|
+
effects: sceneData?.effects,
|
|
96
|
+
badges: sceneData?.badges,
|
|
97
|
+
trace: sceneData?.trace,
|
|
98
|
+
animate: false,
|
|
99
|
+
numberSteps: true
|
|
100
|
+
});
|
|
101
|
+
const body = renderToStaticMarkup(
|
|
102
|
+
/* @__PURE__ */ jsx(RenderProvider, { value: { idx, still: true }, children: module ? /* @__PURE__ */ jsx(module.Renderer, { scene, view }) : /* @__PURE__ */ jsx(StoryProvider, { value: storyValue, children: /* @__PURE__ */ jsx(SceneStory, { view, scene, extra: walkthrough?.removed.entities }) }) })
|
|
103
|
+
);
|
|
104
|
+
const colors = THEMES[theme];
|
|
105
|
+
const steps = (sceneData?.trace ?? []).map((t, i) => t.caption ? plainText(t.caption) : describeRef(idx, t.ref) ?? `Step ${i + 1}`);
|
|
106
|
+
const entries = [...notes.map((text, i) => ({ text: plainText(text), n: i + 1, step: false })), ...steps.map((text, i) => ({ text, n: i + 1, step: true }))];
|
|
107
|
+
const showLegend = options.legend !== false && entries.length > 0;
|
|
108
|
+
const scale = vb.w / width;
|
|
109
|
+
const textW = width - 2 * 40 - 34;
|
|
110
|
+
const legendLines = showLegend ? entries.map((e) => wrap(e.text, textW, 14, 400, 4)) : [];
|
|
111
|
+
const legendH = showLegend ? (legendLines.reduce((s, l) => s + l.length, 0) * 20 + entries.length * 8 + 40) * scale : 0;
|
|
112
|
+
let ly = vb.y + vb.h + 16 * scale;
|
|
113
|
+
const legend = legendLines.map((lines, i) => {
|
|
114
|
+
const y = ly;
|
|
115
|
+
ly += (lines.length * 20 + 8) * scale;
|
|
116
|
+
const x = vb.x + 40 * scale;
|
|
117
|
+
const e = entries[i];
|
|
118
|
+
const mark = e.step ? `<rect x="1" y="1" width="20" height="20" rx="6" fill="${colors.accent}"/>` : `<circle cx="11" cy="11" r="11" fill="${colors.ink}"/>`;
|
|
119
|
+
return `<g transform="translate(${x} ${y}) scale(${scale})">${mark}<text x="11" y="15" text-anchor="middle" font-size="12" font-weight="700" fill="${colors["paper-raised"]}">${e.step ? stepLetter(e.n - 1) : e.n}</text>` + lines.map((l, k) => `<text x="34" y="${15 + k * 20}" font-size="14" fill="${colors.ink}">${escapeXml(l)}</text>`).join("") + `</g>`;
|
|
120
|
+
}).join("");
|
|
121
|
+
const fontFace = options.embedFonts ? `<style>${[400, 500, 600, 700].map((w) => `@font-face{font-family:"Hanken Grotesk";font-weight:${w};src:url(data:font/woff2;base64,${loadFonts().files[`sans-${w}`].toString("base64")}) format("woff2")}`).join("")}</style>` : "";
|
|
122
|
+
const svg = `<svg xmlns="http://www.w3.org/2000/svg" width="${width}" height="${Math.round(height + legendH / scale)}" viewBox="${vb.x} ${vb.y} ${vb.w} ${vb.h + legendH}" font-family="Hanken Grotesk, Helvetica, Arial, sans-serif" role="img"><title>${escapeXml(title)}</title>${fontFace}<rect x="${vb.x}" y="${vb.y}" width="${vb.w}" height="${vb.h + legendH}" fill="${colors["paper-raised"]}"/>` + flatten(body, colors) + (showLegend ? `<line x1="${vb.x + 40 * scale}" x2="${vb.x + vb.w - 40 * scale}" y1="${vb.y + vb.h}" y2="${vb.y + vb.h}" stroke="${colors["story-rule"]}" stroke-width="${scale}"/>` + legend : "") + `</svg>`;
|
|
123
|
+
return { svg, width, height: Math.round(height + legendH / scale), title };
|
|
124
|
+
}
|
|
125
|
+
async function renderScenePng(doc, options = {}) {
|
|
126
|
+
const { svg, width, height, title } = await renderScene(doc, { ...options, embedFonts: false });
|
|
127
|
+
const { Resvg } = await import('@resvg/resvg-js');
|
|
128
|
+
const wawoff2 = (await import('wawoff2')).default;
|
|
129
|
+
const f = loadFonts();
|
|
130
|
+
const buffers = await Promise.all([400, 500, 600, 700].map(async (w) => Buffer.from(await wawoff2.decompress(f.files[`sans-${w}`]))));
|
|
131
|
+
const png = new Resvg(svg, {
|
|
132
|
+
fitTo: { mode: "width", value: width * 2 },
|
|
133
|
+
// fontBuffers is supported at run time; the published types predate it.
|
|
134
|
+
font: { fontBuffers: buffers, loadSystemFonts: false, defaultFontFamily: "Hanken Grotesk" }
|
|
135
|
+
}).render().asPng();
|
|
136
|
+
return { png: Buffer.from(png), width: width * 2, height: height * 2, title };
|
|
137
|
+
}
|
|
138
|
+
function flatten(markup, colors) {
|
|
139
|
+
return markup.replace(/<rect[^>]*class="poney-focus"[^>]*?(?:\/>|><\/rect>)/g, "").replace(/var\(--poney-font-(sans|mono)\)/g, (_, f) => FONTS[f].replace(/'/g, "")).replace(/var\(--poney-([a-z0-9-]+)\)/g, (_, name) => colors[name] ?? "#888888").replace(/color-mix\(in srgb,\s*(#[0-9a-fA-F]{6})\s+([\d.]+)%,\s*(#[0-9a-fA-F]{6}|transparent)\s*\)/g, (_, a, pct, b) => mix(a, Number(pct) / 100, b)).replace(/<(\w+)([^>]*?) class="([^"]*)"/g, (all, tag, attrs, cls) => {
|
|
140
|
+
const opacity = cls.includes("poney-story-hidden") ? "0" : cls.includes("poney-story-dim") ? "0.16" : cls.includes("poney-dim") ? "0.3" : void 0;
|
|
141
|
+
const dashed = cls.includes("poney-flowing") ? ' stroke-dasharray="6 4"' : "";
|
|
142
|
+
return opacity || dashed ? `<${tag}${attrs}${opacity ? ` opacity="${opacity}"` : ""}${dashed}` : all;
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
function mix(a, share, b) {
|
|
146
|
+
const rgb = (h) => [1, 3, 5].map((i) => parseInt(h.slice(i, i + 2), 16));
|
|
147
|
+
const [r, g, bl] = rgb(a);
|
|
148
|
+
if (b === "transparent") return `rgba(${r},${g},${bl},${share})`;
|
|
149
|
+
const [r2, g2, b2] = rgb(b);
|
|
150
|
+
const c = (x, y) => Math.round(x * share + y * (1 - share)).toString(16).padStart(2, "0");
|
|
151
|
+
return `#${c(r, r2)}${c(g, g2)}${c(bl, b2)}`;
|
|
152
|
+
}
|
|
153
|
+
var escapeXml = (s) => s.replace(/[<>&"]/g, (c) => ({ "<": "<", ">": ">", "&": "&", '"': """ })[c]);
|
|
154
|
+
|
|
155
|
+
export { renderScene, renderScenePng, useNodeTextMeasurer };
|
|
156
|
+
//# sourceMappingURL=index.js.map
|
|
157
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.tsx"],"names":["require"],"mappings":";;;;;;;;;;AAkCA,IAAM,WAAA,GAAwB,CAAC,KAAA,EAAO,UAAA,EAAY,OAAO,CAAA;AAEzD,IAAMA,QAAAA,GAAU,aAAA,CAAc,MAAA,CAAA,IAAA,CAAY,GAAG,CAAA;AAI7C,IAAM,UAAA,GAAqC;AAAA,EACzC,GAAA,EAAK,wEAAA;AAAA,EACL,GAAA,EAAK,wEAAA;AAAA,EACL,GAAA,EAAK,wEAAA;AAAA,EACL,GAAA,EAAK;AACP,CAAA;AACA,IAAM,SAAA,GAAY,wEAAA;AAGlB,IAAI,KAAA;AAEJ,SAAS,SAAA,GAAY;AACnB,EAAA,IAAI,OAAO,OAAO,KAAA;AAClB,EAAA,MAAM,QAAgC,EAAC;AACvC,EAAA,MAAM,OAA6B,EAAC;AACpC,EAAA,KAAA,MAAW,CAAC,CAAA,EAAG,CAAC,KAAK,MAAA,CAAO,OAAA,CAAQ,UAAU,CAAA,EAAG;AAC/C,IAAA,KAAA,CAAM,CAAA,KAAA,EAAQ,CAAC,CAAA,CAAE,CAAA,GAAI,aAAaA,QAAAA,CAAQ,OAAA,CAAQ,CAAC,CAAC,CAAA;AACpD,IAAA,IAAA,CAAK,MAAA,CAAO,CAAC,CAAC,CAAA,GAAY,eAAO,KAAA,CAAM,CAAA,KAAA,EAAQ,CAAC,CAAA,CAAE,CAAC,CAAA;AAAA,EACrD;AACA,EAAA,KAAA,CAAM,IAAA,GAAO,YAAA,CAAaA,QAAAA,CAAQ,OAAA,CAAQ,SAAS,CAAC,CAAA;AACpD,EAAA,KAAA,GAAQ,EAAE,IAAA,EAAM,IAAA,EAAc,eAAO,KAAA,CAAM,IAAI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n * @hattoriai/poney-render — any view or walkthrough scene of a Poney\n * document to SVG or PNG, in Node, drawn by @hattoriai/poney-react.\n *\n * Text is measured with the same font files the PNG is drawn with, so labels\n * fit their boxes. The SVG is self-contained: colours are resolved (no CSS\n * variables), opacity is inlined, and fonts are embedded on request.\n */\nimport { readFileSync } from 'node:fs'\nimport { createRequire } from 'node:module'\nimport { renderToStaticMarkup } from 'react-dom/server'\nimport * as fontkit from 'fontkit'\nimport { indexOf, marksOf, parseRef, plainText, type PoneyDocument, type Scene, type View } from '@hattoriai/poney-core'\nimport { layoutView, setTextMeasurer, wrap, type FontFamily } from '@hattoriai/poney-layout'\nimport {\n diagramFor,\n findScene,\n fit,\n FONTS,\n RenderProvider,\n SceneStory,\n sceneFrame,\n sceneLayoutOptions,\n stepLetter,\n StoryProvider,\n storyState,\n THEMES,\n type Mode,\n type ThemeName,\n} from '@hattoriai/poney-react'\n\n/** View types drawn as walkthrough stories; the others draw with their diagram renderer. */\nconst STORY_TYPES: string[] = ['map', 'sequence', 'state']\n\nconst require = createRequire(import.meta.url)\n\n/* ─── Fonts ───────────────────────────────────────────────────────────────── */\n\nconst SANS_FILES: Record<number, string> = {\n 400: '@fontsource/hanken-grotesk/files/hanken-grotesk-latin-400-normal.woff2',\n 500: '@fontsource/hanken-grotesk/files/hanken-grotesk-latin-500-normal.woff2',\n 600: '@fontsource/hanken-grotesk/files/hanken-grotesk-latin-600-normal.woff2',\n 700: '@fontsource/hanken-grotesk/files/hanken-grotesk-latin-700-normal.woff2',\n}\nconst MONO_FILE = '@fontsource/jetbrains-mono/files/jetbrains-mono-latin-400-normal.woff2'\n\ntype Font = { layout(text: string): { advanceWidth: number }; unitsPerEm: number }\nlet fonts: { sans: Record<number, Font>; mono: Font; files: Record<string, Buffer> } | undefined\n\nfunction loadFonts() {\n if (fonts) return fonts\n const files: Record<string, Buffer> = {}\n const sans: Record<number, Font> = {}\n for (const [w, f] of Object.entries(SANS_FILES)) {\n files[`sans-${w}`] = readFileSync(require.resolve(f))\n sans[Number(w)] = fontkit.create(files[`sans-${w}`]) as unknown as Font\n }\n files.mono = readFileSync(require.resolve(MONO_FILE))\n fonts = { sans, mono: fontkit.create(files.mono) as unknown as Font, files }\n return fonts\n}\n\nconst nearest = (weight: number) => [400, 500, 600, 700].reduce((a, b) => (Math.abs(b - weight) < Math.abs(a - weight) ? b : a))\n\n/** Measures text with the real font files. Call before laying out in Node. */\nexport function useNodeTextMeasurer() {\n const f = loadFonts()\n setTextMeasurer((text: string, size: number, weight: number, family: FontFamily) => {\n const font = family === 'mono' ? f.mono : f.sans[nearest(weight)]\n return (font.layout(text).advanceWidth / font.unitsPerEm) * size\n })\n}\n\n/* ─── Rendering ───────────────────────────────────────────────────────────── */\n\nexport interface RenderSceneOptions {\n /** Walkthrough id. Default: the first. */\n walkthrough?: string\n /** Scene id, or \"overview\". Default: the overview, else the first scene with a view. */\n scene?: string\n /** Render a view directly, outside any walkthrough. */\n view?: string\n mode?: Mode\n theme?: ThemeName\n /** Width of the output in pixels. Height follows the scene. */\n width?: number\n /** Embed the fonts in the SVG, so it looks the same everywhere. */\n embedFonts?: boolean\n /** Write the numbered callout notes under the picture. Default: true. */\n legend?: boolean\n}\n\nexport interface RenderedScene {\n svg: string\n width: number\n height: number\n title: string\n}\n\nconst PAD = 40\n\n/** A plain name for a ref, for a step without a caption. */\nfunction describeRef(idx: ReturnType<typeof indexOf>, key: string): string | undefined {\n const r = parseRef(key)\n if (!r || !idx.exists(r)) return undefined\n return idx.describe(r).title\n}\n\nexport async function renderScene(doc: PoneyDocument, options: RenderSceneOptions = {}): Promise<RenderedScene> {\n useNodeTextMeasurer()\n const idx = indexOf(doc)\n let view: View | undefined\n let focus: string[] = []\n let callouts: string[] = []\n let notes: string[] = []\n let title = ''\n let sceneData: Scene | undefined\n let walkthrough = options.walkthrough ? doc.walkthroughs.find((w) => w.id === options.walkthrough) : doc.walkthroughs[0]\n if (options.view) {\n view = idx.views.get(options.view)\n if (!view) throw new Error(`No view \"${options.view}\". Views: ${doc.views.map((v) => v.id).join(', ')}`)\n title = view.title\n walkthrough = options.walkthrough ? walkthrough : undefined\n } else {\n const found = findScene(doc, options.walkthrough, options.scene)\n if (!found) {\n const w = walkthrough\n const ids = w ? ['overview', ...w.scenes.filter((s) => s.view).map((s) => s.id)].join(', ') : 'none'\n throw new Error(`No scene \"${options.scene ?? '(default)'}\" with a view. Scenes: ${ids}`)\n }\n walkthrough = found.walkthrough\n view = idx.views.get(found.viewId)!\n focus = found.focus\n callouts = found.callouts\n notes = found.notes\n title = found.title\n sceneData = found.scene\n }\n // Map, sequence and state views draw as stories (marks, focus, callouts); every other view type draws with its diagram renderer, still.\n const story = STORY_TYPES.includes(view.type)\n const module = story ? undefined : diagramFor(view)\n if (!story && !module) throw new Error(`No renderer for \"${view.id}\", a ${view.type === 'extension' ? view.extension : view.type} view. Register one with registerDiagram.`)\n const scene = await layoutView(doc, view, story ? sceneLayoutOptions(view, walkthrough, focus) : {})\n const frame = story ? sceneFrame(scene, view, focus, idx, walkthrough) : { x: 0, y: 0, w: scene.width, h: scene.height }\n const natural = { w: frame.w + PAD * 2, h: frame.h + PAD * 2 }\n const width = Math.round(options.width ?? Math.min(1600, Math.max(720, natural.w)))\n const height = Math.round((width * natural.h) / natural.w)\n const vb = fit(frame, width, height, PAD, 10)\n const theme = options.theme ?? 'light'\n const storyValue = storyState({\n marks: walkthrough ? marksOf(walkthrough) : new Map(),\n mode: options.mode ?? 'change',\n view,\n focus,\n callouts,\n idx,\n walkthrough,\n effects: sceneData?.effects,\n badges: sceneData?.badges,\n trace: sceneData?.trace,\n animate: false,\n numberSteps: true,\n })\n\n const body = renderToStaticMarkup(\n <RenderProvider value={{ idx, still: true }}>\n {module ? (\n <module.Renderer scene={scene} view={view} />\n ) : (\n <StoryProvider value={storyValue}>\n <SceneStory view={view} scene={scene} extra={walkthrough?.removed.entities} />\n </StoryProvider>\n )}\n </RenderProvider>,\n )\n\n const colors = THEMES[theme]\n // The key: each callout's number and note, under the picture.\n // The key under the picture: numbered notes (round marks), then the steps of the path (square marks).\n const steps = (sceneData?.trace ?? []).map((t, i) => (t.caption ? plainText(t.caption) : (describeRef(idx, t.ref) ?? `Step ${i + 1}`)))\n // A picture draws plain text: inline Markdown in the notes shows without its markers.\n const entries = [...notes.map((text, i) => ({ text: plainText(text), n: i + 1, step: false })), ...steps.map((text, i) => ({ text, n: i + 1, step: true }))]\n const showLegend = options.legend !== false && entries.length > 0\n const scale = vb.w / width\n const textW = width - 2 * 40 - 34\n const legendLines = showLegend ? entries.map((e) => wrap(e.text, textW, 14, 400, 4)) : []\n const legendH = showLegend ? (legendLines.reduce((s, l) => s + l.length, 0) * 20 + entries.length * 8 + 40) * scale : 0\n let ly = vb.y + vb.h + 16 * scale\n const legend = legendLines\n .map((lines, i) => {\n const y = ly\n ly += (lines.length * 20 + 8) * scale\n const x = vb.x + 40 * scale\n const e = entries[i]\n const mark = e.step ? `<rect x=\"1\" y=\"1\" width=\"20\" height=\"20\" rx=\"6\" fill=\"${colors.accent}\"/>` : `<circle cx=\"11\" cy=\"11\" r=\"11\" fill=\"${colors.ink}\"/>`\n return (\n `<g transform=\"translate(${x} ${y}) scale(${scale})\">${mark}` +\n `<text x=\"11\" y=\"15\" text-anchor=\"middle\" font-size=\"12\" font-weight=\"700\" fill=\"${colors['paper-raised']}\">${e.step ? stepLetter(e.n - 1) : e.n}</text>` +\n lines.map((l, k) => `<text x=\"34\" y=\"${15 + k * 20}\" font-size=\"14\" fill=\"${colors.ink}\">${escapeXml(l)}</text>`).join('') +\n `</g>`\n )\n })\n .join('')\n const fontFace = options.embedFonts\n ? `<style>${[400, 500, 600, 700]\n .map((w) => `@font-face{font-family:\"Hanken Grotesk\";font-weight:${w};src:url(data:font/woff2;base64,${loadFonts().files[`sans-${w}`].toString('base64')}) format(\"woff2\")}`)\n .join('')}</style>`\n : ''\n const svg =\n `<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"${width}\" height=\"${Math.round(height + legendH / scale)}\" viewBox=\"${vb.x} ${vb.y} ${vb.w} ${vb.h + legendH}\" font-family=\"Hanken Grotesk, Helvetica, Arial, sans-serif\" role=\"img\">` +\n `<title>${escapeXml(title)}</title>${fontFace}` +\n `<rect x=\"${vb.x}\" y=\"${vb.y}\" width=\"${vb.w}\" height=\"${vb.h + legendH}\" fill=\"${colors['paper-raised']}\"/>` +\n flatten(body, colors) +\n (showLegend ? `<line x1=\"${vb.x + 40 * scale}\" x2=\"${vb.x + vb.w - 40 * scale}\" y1=\"${vb.y + vb.h}\" y2=\"${vb.y + vb.h}\" stroke=\"${colors['story-rule']}\" stroke-width=\"${scale}\"/>` + legend : '') +\n `</svg>`\n return { svg, width, height: Math.round(height + legendH / scale), title }\n}\n\n/** Renders a scene to PNG at twice its size, with the bundled fonts. */\nexport async function renderScenePng(doc: PoneyDocument, options: RenderSceneOptions = {}): Promise<{ png: Buffer; width: number; height: number; title: string }> {\n const { svg, width, height, title } = await renderScene(doc, { ...options, embedFonts: false })\n const { Resvg } = await import('@resvg/resvg-js')\n const wawoff2 = (await import('wawoff2')).default\n const f = loadFonts()\n const buffers = await Promise.all([400, 500, 600, 700].map(async (w) => Buffer.from(await wawoff2.decompress(f.files[`sans-${w}`]))))\n const png = new Resvg(svg, {\n fitTo: { mode: 'width', value: width * 2 },\n // fontBuffers is supported at run time; the published types predate it.\n font: { fontBuffers: buffers, loadSystemFonts: false, defaultFontFamily: 'Hanken Grotesk' } as Record<string, unknown>,\n })\n .render()\n .asPng()\n return { png: Buffer.from(png), width: width * 2, height: height * 2, title }\n}\n\n/**\n * Makes the markup self-contained: CSS variables become colours, opacity\n * classes become attributes, and elements that only show on keyboard focus\n * are dropped.\n */\nfunction flatten(markup: string, colors: Record<string, string>): string {\n return markup\n .replace(/<rect[^>]*class=\"poney-focus\"[^>]*?(?:\\/>|><\\/rect>)/g, '')\n .replace(/var\\(--poney-font-(sans|mono)\\)/g, (_, f: 'sans' | 'mono') => FONTS[f].replace(/'/g, ''))\n .replace(/var\\(--poney-([a-z0-9-]+)\\)/g, (_, name: string) => colors[name] ?? '#888888')\n // Tints are CSS color-mix(); a picture gets the mixed color itself.\n .replace(/color-mix\\(in srgb,\\s*(#[0-9a-fA-F]{6})\\s+([\\d.]+)%,\\s*(#[0-9a-fA-F]{6}|transparent)\\s*\\)/g, (_, a: string, pct: string, b: string) => mix(a, Number(pct) / 100, b))\n .replace(/<(\\w+)([^>]*?) class=\"([^\"]*)\"/g, (all, tag: string, attrs: string, cls: string) => {\n const opacity = cls.includes('poney-story-hidden') ? '0' : cls.includes('poney-story-dim') ? '0.16' : cls.includes('poney-dim') ? '0.3' : undefined\n // A line that carries data flows in the Studio; a still picture draws it dashed.\n const dashed = cls.includes('poney-flowing') ? ' stroke-dasharray=\"6 4\"' : ''\n return opacity || dashed ? `<${tag}${attrs}${opacity ? ` opacity=\"${opacity}\"` : ''}${dashed}` : all\n })\n}\n\n/** `a` at `share` over `b` (or, over transparent, `a` with that alpha). */\nfunction mix(a: string, share: number, b: string): string {\n const rgb = (h: string) => [1, 3, 5].map((i) => parseInt(h.slice(i, i + 2), 16))\n const [r, g, bl] = rgb(a)\n if (b === 'transparent') return `rgba(${r},${g},${bl},${share})`\n const [r2, g2, b2] = rgb(b)\n const c = (x: number, y: number) => Math.round(x * share + y * (1 - share)).toString(16).padStart(2, '0')\n return `#${c(r, r2)}${c(g, g2)}${c(bl, b2)}`\n}\n\nconst escapeXml = (s: string) => s.replace(/[<>&\"]/g, (c) => ({ '<': '<', '>': '>', '&': '&', '\"': '"' })[c]!)\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,53 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hattoriai/poney-render",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"description": "
|
|
6
|
-
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "Poney documents to SVG and PNG in Node: any view or walkthrough scene, with the real fonts.",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/hattoriai/poney.git",
|
|
10
|
+
"directory": "packages/render"
|
|
11
|
+
},
|
|
12
|
+
"homepage": "https://github.com/hattoriai/poney#readme",
|
|
13
|
+
"bugs": "https://github.com/hattoriai/poney/issues",
|
|
14
|
+
"engines": {
|
|
15
|
+
"node": ">=20"
|
|
16
|
+
},
|
|
17
|
+
"exports": {
|
|
18
|
+
".": {
|
|
19
|
+
"poney-source": "./src/index.tsx",
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"default": "./dist/index.js"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"files": [
|
|
25
|
+
"dist"
|
|
26
|
+
],
|
|
27
|
+
"scripts": {
|
|
28
|
+
"build": "tsup"
|
|
29
|
+
},
|
|
30
|
+
"publishConfig": {
|
|
31
|
+
"access": "public",
|
|
32
|
+
"provenance": true
|
|
33
|
+
},
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"@fontsource/hanken-grotesk": "^5.3.0",
|
|
36
|
+
"@fontsource/jetbrains-mono": "^5.3.0",
|
|
37
|
+
"@hattoriai/poney-core": "0.1.1",
|
|
38
|
+
"@hattoriai/poney-layout": "0.1.1",
|
|
39
|
+
"@hattoriai/poney-react": "0.1.1",
|
|
40
|
+
"@resvg/resvg-js": "^2.6.2",
|
|
41
|
+
"fontkit": "^2.0.4",
|
|
42
|
+
"wawoff2": "^2.0.1"
|
|
43
|
+
},
|
|
44
|
+
"peerDependencies": {
|
|
45
|
+
"react": "^18.3.1 || ^19.0.0",
|
|
46
|
+
"react-dom": "^18.3.1 || ^19.0.0"
|
|
47
|
+
},
|
|
48
|
+
"devDependencies": {
|
|
49
|
+
"react": "^18.3.1",
|
|
50
|
+
"react-dom": "^18.3.1",
|
|
51
|
+
"tsup": "8.5.1"
|
|
52
|
+
}
|
|
53
|
+
}
|