@shibayama/pdgkit 0.1.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.
@@ -0,0 +1,214 @@
1
+ import { Lang, DiagramKind, Diagnostic } from './core.cjs';
2
+ export { Bilingual, Box, Containment, Doc, Edge, EdgeOp, LabelPlacement, LaidOut, LaidOutEdge, LaidOutNode, Node, OP_TABLE, PATTERN_LABEL, PATTERN_SOURCE, PatternId, RenderOptions, SAMPLES, SAMPLE_ORDER, SampleId, Shape, chooseLabelPlacement, estimateTextWidth, layout, parse, refsToCsv, refsToMarkdown, render, splitBilingual, stripComment } from './core.cjs';
3
+
4
+ /**
5
+ * Minimal, zero-dependency SVG DOM shim. Implements the few element operations the
6
+ * renderer needs (createElementNS, setAttribute, appendChild, textContent) and
7
+ * serializes the tree to XML. `withShimDocument()` scopes the shim around a render
8
+ * call, so rendering also works in a real browser without altering the page's document.
9
+ */
10
+ /** A serializable SVG node. The shape `render()` actually produces and consumes. */
11
+ interface SvgNode {
12
+ readonly nodeType: 'element';
13
+ tagName: string;
14
+ namespaceURI: string;
15
+ /** Attribute names in insertion order, mapped to string values. */
16
+ attrs: Map<string, string>;
17
+ children: SvgNode[];
18
+ /** Text content, or null when the element holds child elements instead. */
19
+ text: string | null;
20
+ setAttribute(name: string, value: string): void;
21
+ getAttribute(name: string): string | null;
22
+ removeAttribute(name: string): void;
23
+ appendChild(child: SvgNode): SvgNode;
24
+ set textContent(value: string);
25
+ get textContent(): string;
26
+ }
27
+ /**
28
+ * Install the shim as `globalThis.document`, idempotently, but only when no DOM
29
+ * with `createElementNS` is already present. In a real browser this is a no-op,
30
+ * so the same code path serves both environments.
31
+ */
32
+ declare function installDomShim(): void;
33
+ /**
34
+ * Serialize a shim element tree to an XML string. Produces self-closing tags for
35
+ * empty elements and correctly escapes attribute values and text content.
36
+ */
37
+ declare function serializeSvg(node: SvgNode): string;
38
+
39
+ /**
40
+ * Compute the tight content bounding box of a rendered SVG tree analytically
41
+ * (without `getBBox`), by walking the element tree and measuring each primitive;
42
+ * text width reuses {@link estimateTextWidth}. Used to crop exports to the drawn
43
+ * extent. Also provides raster and display dimension helpers.
44
+ */
45
+
46
+ interface ViewBox {
47
+ minX: number;
48
+ minY: number;
49
+ width: number;
50
+ height: number;
51
+ }
52
+ /**
53
+ * Compute the tight content bounding box of a rendered SVG tree, padded by `bleed`
54
+ * (default 3 units).
55
+ */
56
+ declare function computeContentBox(root: SvgNode, bleed?: number): ViewBox;
57
+ /**
58
+ * Display dimensions for a viewBox so the long side is at least `targetSide` px
59
+ * (browser default: 1600). Mirrors `svgDisplayDimensionsForViewBox()`.
60
+ */
61
+ declare function svgDisplayDimensions(viewBox: Pick<ViewBox, 'width' | 'height'>, targetSide?: number): {
62
+ width: number;
63
+ height: number;
64
+ scale: number;
65
+ };
66
+
67
+ /**
68
+ * Headless SVG rendering: `.pdg` source -> standalone SVG string.
69
+ * Crops to the real drawn extent (bleed 3) and sizes the document so the long side
70
+ * is at least `targetSide` px (default 1600).
71
+ */
72
+
73
+ interface RenderToSvgOptions {
74
+ /** Display language: 'ja' (default), 'en', or 'both' (bilingual two-line labels). */
75
+ lang?: Lang;
76
+ /** Crop to the real drawn extent (default true). When false, uses the full canvas. */
77
+ crop?: boolean;
78
+ /** Padding added around the content box, in user units (default 3). */
79
+ bleed?: number;
80
+ /** Minimum long-side length in px for the width/height attributes (default 1600). */
81
+ targetSide?: number;
82
+ /** Prepend an `<?xml ... ?>` declaration (default true). */
83
+ xmlDeclaration?: boolean;
84
+ }
85
+ interface RenderToSvgResult {
86
+ /** The serialized standalone SVG document. */
87
+ svg: string;
88
+ /** The diagram kind inferred from the source. */
89
+ kind: DiagramKind;
90
+ /** The viewBox used (content box when cropped). */
91
+ viewBox: ViewBox;
92
+ /** Display width in px (the `width` attribute). */
93
+ width: number;
94
+ /** Display height in px (the `height` attribute). */
95
+ height: number;
96
+ }
97
+ /**
98
+ * Render `.pdg` source to a standalone SVG string. Synchronous and dependency-free.
99
+ */
100
+ declare function renderToSvg(source: string, opts?: RenderToSvgOptions): RenderToSvgResult;
101
+
102
+ /**
103
+ * Validation for AI authoring. Runs the core parser and adds a kind assertion and
104
+ * friendly lints, returning all diagnostics.
105
+ *
106
+ * - Kind assertion: an optional `#! kind: block|flow|state|seq` directive (written
107
+ * in a comment) is checked against the kind inferred from structure; a mismatch
108
+ * is reported as an error. Guards against producing the wrong diagram type.
109
+ * - Lints: clearer hints for chained connections (`A -> B -> C`) and operators
110
+ * without surrounding spaces (`11-12`).
111
+ *
112
+ * Run on AI-generated `.pdg` and feed the diagnostics back until there are no errors.
113
+ */
114
+
115
+ interface ValidateResult {
116
+ /** True when there are no error-severity diagnostics. */
117
+ ok: boolean;
118
+ /** The diagram kind inferred from the source structure. */
119
+ kind: DiagramKind;
120
+ /** The diagram kind the author declared via directive, or null if none. */
121
+ declaredKind: DiagramKind | null;
122
+ /** Whether the declared kind matches the inferred kind (null if not declared). */
123
+ kindMatches: boolean | null;
124
+ /** All diagnostics (parser + pdgkit lints), sorted by line then column. */
125
+ diagnostics: Diagnostic[];
126
+ counts: {
127
+ errors: number;
128
+ warnings: number;
129
+ infos: number;
130
+ };
131
+ }
132
+ /**
133
+ * Validate a `.pdg` source string. Pure and synchronous — safe to call in any
134
+ * environment, including a hot AI authoring loop.
135
+ */
136
+ declare function validate(source: string): ValidateResult;
137
+
138
+ /**
139
+ * Raster output (PNG / JPEG) via `@resvg/resvg-js`, with the bundled IPAex Gothic
140
+ * font embedded and a white background. Default scale is 8x the content box (capped
141
+ * to 24000 px/side and 1e8 px total). Native dependencies are imported lazily.
142
+ */
143
+
144
+ interface RasterOptions {
145
+ lang?: Lang;
146
+ /** Resolution multiplier applied to the cropped content box (default 8). */
147
+ scale?: number;
148
+ /** Padding around the content box, in user units (default 3). */
149
+ bleed?: number;
150
+ }
151
+ /** Render `.pdg` source to a PNG (8× by default, white background). */
152
+ declare function renderToPng(source: string, opts?: RasterOptions): Promise<Uint8Array>;
153
+ /** Render `.pdg` source to a JPEG (8× by default, white background). */
154
+ declare function renderToJpeg(source: string, opts?: RasterOptions): Promise<Uint8Array>;
155
+
156
+ /**
157
+ * PDF output (A4, IPAex font embedded). Vector by default (jsPDF + svg2pdf.js under
158
+ * jsdom, with analytic getBBox / getComputedTextLength shims), falling back to a
159
+ * high-resolution raster PDF if the vector path fails. The figure is centered with
160
+ * a 10 mm margin and the page orientation follows its aspect ratio.
161
+ */
162
+
163
+ interface PdfOptions {
164
+ lang?: Lang;
165
+ /** Padding around the content box, in user units (default 3). */
166
+ bleed?: number;
167
+ /** Prefer vector output; fall back to raster on failure (default true). */
168
+ vector?: boolean;
169
+ /** Raster resolution multiplier for the raster path (default 8). */
170
+ scale?: number;
171
+ }
172
+ /** Render `.pdg` source to a PDF (A4, IPAex font, vector preferred). */
173
+ declare function renderToPdf(source: string, opts?: PdfOptions): Promise<Uint8Array>;
174
+
175
+ /**
176
+ * PPTX output: `.pdg` → PowerPoint presentation (one 16:9 slide).
177
+ *
178
+ * - image mode (default): the figure is rasterized (resvg) and placed as a
179
+ * centered picture — highest visual fidelity.
180
+ * - editable mode (`editable: true`): every SVG primitive is converted to an
181
+ * editable PowerPoint shape / connector / text box, for later tweaking.
182
+ */
183
+
184
+ interface PptxOptions {
185
+ lang?: Lang;
186
+ /** Emit editable PowerPoint shapes instead of a single image (default false). */
187
+ editable?: boolean;
188
+ /** Raster resolution multiplier for image mode (default 8). */
189
+ scale?: number;
190
+ /** Padding around the content box, in user units (default 3). */
191
+ bleed?: number;
192
+ }
193
+ /** Render `.pdg` source to a PPTX (image by default, or editable shapes). */
194
+ declare function renderToPptx(source: string, opts?: PptxOptions): Promise<Uint8Array>;
195
+
196
+ /**
197
+ * pdgkit Node API — the browser-free rendering surface a host tool calls.
198
+ *
199
+ * Implemented: SVG, PNG, JPEG, PDF, PPTX (image + editable), validation, and the
200
+ * reference-sign table exporters. All run with no browser.
201
+ */
202
+
203
+ /** The pdgkit package version. Keep in sync with package.json. */
204
+ declare const VERSION = "0.1.0";
205
+ /** Convenience: render to SVG and return just the string. */
206
+ declare function toSvgString(source: string, lang?: Lang): string;
207
+ /**
208
+ * Read the bundled AI authoring guide (docs/ai-authoring-guide.md) as a string.
209
+ * Inject this into an LLM's system prompt so it can write valid `.pdg`, instead of
210
+ * pasting the guide by hand.
211
+ */
212
+ declare function loadAuthoringGuide(): string;
213
+
214
+ export { Diagnostic, DiagramKind, Lang, type PdfOptions, type PptxOptions, type RasterOptions, type RenderToSvgOptions, type RenderToSvgResult, type SvgNode, VERSION, type ValidateResult, type ViewBox, computeContentBox, installDomShim, loadAuthoringGuide, renderToJpeg, renderToPdf, renderToPng, renderToPptx, renderToSvg, serializeSvg, svgDisplayDimensions, toSvgString, validate };
@@ -0,0 +1,214 @@
1
+ import { Lang, DiagramKind, Diagnostic } from './core.js';
2
+ export { Bilingual, Box, Containment, Doc, Edge, EdgeOp, LabelPlacement, LaidOut, LaidOutEdge, LaidOutNode, Node, OP_TABLE, PATTERN_LABEL, PATTERN_SOURCE, PatternId, RenderOptions, SAMPLES, SAMPLE_ORDER, SampleId, Shape, chooseLabelPlacement, estimateTextWidth, layout, parse, refsToCsv, refsToMarkdown, render, splitBilingual, stripComment } from './core.js';
3
+
4
+ /**
5
+ * Minimal, zero-dependency SVG DOM shim. Implements the few element operations the
6
+ * renderer needs (createElementNS, setAttribute, appendChild, textContent) and
7
+ * serializes the tree to XML. `withShimDocument()` scopes the shim around a render
8
+ * call, so rendering also works in a real browser without altering the page's document.
9
+ */
10
+ /** A serializable SVG node. The shape `render()` actually produces and consumes. */
11
+ interface SvgNode {
12
+ readonly nodeType: 'element';
13
+ tagName: string;
14
+ namespaceURI: string;
15
+ /** Attribute names in insertion order, mapped to string values. */
16
+ attrs: Map<string, string>;
17
+ children: SvgNode[];
18
+ /** Text content, or null when the element holds child elements instead. */
19
+ text: string | null;
20
+ setAttribute(name: string, value: string): void;
21
+ getAttribute(name: string): string | null;
22
+ removeAttribute(name: string): void;
23
+ appendChild(child: SvgNode): SvgNode;
24
+ set textContent(value: string);
25
+ get textContent(): string;
26
+ }
27
+ /**
28
+ * Install the shim as `globalThis.document`, idempotently, but only when no DOM
29
+ * with `createElementNS` is already present. In a real browser this is a no-op,
30
+ * so the same code path serves both environments.
31
+ */
32
+ declare function installDomShim(): void;
33
+ /**
34
+ * Serialize a shim element tree to an XML string. Produces self-closing tags for
35
+ * empty elements and correctly escapes attribute values and text content.
36
+ */
37
+ declare function serializeSvg(node: SvgNode): string;
38
+
39
+ /**
40
+ * Compute the tight content bounding box of a rendered SVG tree analytically
41
+ * (without `getBBox`), by walking the element tree and measuring each primitive;
42
+ * text width reuses {@link estimateTextWidth}. Used to crop exports to the drawn
43
+ * extent. Also provides raster and display dimension helpers.
44
+ */
45
+
46
+ interface ViewBox {
47
+ minX: number;
48
+ minY: number;
49
+ width: number;
50
+ height: number;
51
+ }
52
+ /**
53
+ * Compute the tight content bounding box of a rendered SVG tree, padded by `bleed`
54
+ * (default 3 units).
55
+ */
56
+ declare function computeContentBox(root: SvgNode, bleed?: number): ViewBox;
57
+ /**
58
+ * Display dimensions for a viewBox so the long side is at least `targetSide` px
59
+ * (browser default: 1600). Mirrors `svgDisplayDimensionsForViewBox()`.
60
+ */
61
+ declare function svgDisplayDimensions(viewBox: Pick<ViewBox, 'width' | 'height'>, targetSide?: number): {
62
+ width: number;
63
+ height: number;
64
+ scale: number;
65
+ };
66
+
67
+ /**
68
+ * Headless SVG rendering: `.pdg` source -> standalone SVG string.
69
+ * Crops to the real drawn extent (bleed 3) and sizes the document so the long side
70
+ * is at least `targetSide` px (default 1600).
71
+ */
72
+
73
+ interface RenderToSvgOptions {
74
+ /** Display language: 'ja' (default), 'en', or 'both' (bilingual two-line labels). */
75
+ lang?: Lang;
76
+ /** Crop to the real drawn extent (default true). When false, uses the full canvas. */
77
+ crop?: boolean;
78
+ /** Padding added around the content box, in user units (default 3). */
79
+ bleed?: number;
80
+ /** Minimum long-side length in px for the width/height attributes (default 1600). */
81
+ targetSide?: number;
82
+ /** Prepend an `<?xml ... ?>` declaration (default true). */
83
+ xmlDeclaration?: boolean;
84
+ }
85
+ interface RenderToSvgResult {
86
+ /** The serialized standalone SVG document. */
87
+ svg: string;
88
+ /** The diagram kind inferred from the source. */
89
+ kind: DiagramKind;
90
+ /** The viewBox used (content box when cropped). */
91
+ viewBox: ViewBox;
92
+ /** Display width in px (the `width` attribute). */
93
+ width: number;
94
+ /** Display height in px (the `height` attribute). */
95
+ height: number;
96
+ }
97
+ /**
98
+ * Render `.pdg` source to a standalone SVG string. Synchronous and dependency-free.
99
+ */
100
+ declare function renderToSvg(source: string, opts?: RenderToSvgOptions): RenderToSvgResult;
101
+
102
+ /**
103
+ * Validation for AI authoring. Runs the core parser and adds a kind assertion and
104
+ * friendly lints, returning all diagnostics.
105
+ *
106
+ * - Kind assertion: an optional `#! kind: block|flow|state|seq` directive (written
107
+ * in a comment) is checked against the kind inferred from structure; a mismatch
108
+ * is reported as an error. Guards against producing the wrong diagram type.
109
+ * - Lints: clearer hints for chained connections (`A -> B -> C`) and operators
110
+ * without surrounding spaces (`11-12`).
111
+ *
112
+ * Run on AI-generated `.pdg` and feed the diagnostics back until there are no errors.
113
+ */
114
+
115
+ interface ValidateResult {
116
+ /** True when there are no error-severity diagnostics. */
117
+ ok: boolean;
118
+ /** The diagram kind inferred from the source structure. */
119
+ kind: DiagramKind;
120
+ /** The diagram kind the author declared via directive, or null if none. */
121
+ declaredKind: DiagramKind | null;
122
+ /** Whether the declared kind matches the inferred kind (null if not declared). */
123
+ kindMatches: boolean | null;
124
+ /** All diagnostics (parser + pdgkit lints), sorted by line then column. */
125
+ diagnostics: Diagnostic[];
126
+ counts: {
127
+ errors: number;
128
+ warnings: number;
129
+ infos: number;
130
+ };
131
+ }
132
+ /**
133
+ * Validate a `.pdg` source string. Pure and synchronous — safe to call in any
134
+ * environment, including a hot AI authoring loop.
135
+ */
136
+ declare function validate(source: string): ValidateResult;
137
+
138
+ /**
139
+ * Raster output (PNG / JPEG) via `@resvg/resvg-js`, with the bundled IPAex Gothic
140
+ * font embedded and a white background. Default scale is 8x the content box (capped
141
+ * to 24000 px/side and 1e8 px total). Native dependencies are imported lazily.
142
+ */
143
+
144
+ interface RasterOptions {
145
+ lang?: Lang;
146
+ /** Resolution multiplier applied to the cropped content box (default 8). */
147
+ scale?: number;
148
+ /** Padding around the content box, in user units (default 3). */
149
+ bleed?: number;
150
+ }
151
+ /** Render `.pdg` source to a PNG (8× by default, white background). */
152
+ declare function renderToPng(source: string, opts?: RasterOptions): Promise<Uint8Array>;
153
+ /** Render `.pdg` source to a JPEG (8× by default, white background). */
154
+ declare function renderToJpeg(source: string, opts?: RasterOptions): Promise<Uint8Array>;
155
+
156
+ /**
157
+ * PDF output (A4, IPAex font embedded). Vector by default (jsPDF + svg2pdf.js under
158
+ * jsdom, with analytic getBBox / getComputedTextLength shims), falling back to a
159
+ * high-resolution raster PDF if the vector path fails. The figure is centered with
160
+ * a 10 mm margin and the page orientation follows its aspect ratio.
161
+ */
162
+
163
+ interface PdfOptions {
164
+ lang?: Lang;
165
+ /** Padding around the content box, in user units (default 3). */
166
+ bleed?: number;
167
+ /** Prefer vector output; fall back to raster on failure (default true). */
168
+ vector?: boolean;
169
+ /** Raster resolution multiplier for the raster path (default 8). */
170
+ scale?: number;
171
+ }
172
+ /** Render `.pdg` source to a PDF (A4, IPAex font, vector preferred). */
173
+ declare function renderToPdf(source: string, opts?: PdfOptions): Promise<Uint8Array>;
174
+
175
+ /**
176
+ * PPTX output: `.pdg` → PowerPoint presentation (one 16:9 slide).
177
+ *
178
+ * - image mode (default): the figure is rasterized (resvg) and placed as a
179
+ * centered picture — highest visual fidelity.
180
+ * - editable mode (`editable: true`): every SVG primitive is converted to an
181
+ * editable PowerPoint shape / connector / text box, for later tweaking.
182
+ */
183
+
184
+ interface PptxOptions {
185
+ lang?: Lang;
186
+ /** Emit editable PowerPoint shapes instead of a single image (default false). */
187
+ editable?: boolean;
188
+ /** Raster resolution multiplier for image mode (default 8). */
189
+ scale?: number;
190
+ /** Padding around the content box, in user units (default 3). */
191
+ bleed?: number;
192
+ }
193
+ /** Render `.pdg` source to a PPTX (image by default, or editable shapes). */
194
+ declare function renderToPptx(source: string, opts?: PptxOptions): Promise<Uint8Array>;
195
+
196
+ /**
197
+ * pdgkit Node API — the browser-free rendering surface a host tool calls.
198
+ *
199
+ * Implemented: SVG, PNG, JPEG, PDF, PPTX (image + editable), validation, and the
200
+ * reference-sign table exporters. All run with no browser.
201
+ */
202
+
203
+ /** The pdgkit package version. Keep in sync with package.json. */
204
+ declare const VERSION = "0.1.0";
205
+ /** Convenience: render to SVG and return just the string. */
206
+ declare function toSvgString(source: string, lang?: Lang): string;
207
+ /**
208
+ * Read the bundled AI authoring guide (docs/ai-authoring-guide.md) as a string.
209
+ * Inject this into an LLM's system prompt so it can write valid `.pdg`, instead of
210
+ * pasting the guide by hand.
211
+ */
212
+ declare function loadAuthoringGuide(): string;
213
+
214
+ export { Diagnostic, DiagramKind, Lang, type PdfOptions, type PptxOptions, type RasterOptions, type RenderToSvgOptions, type RenderToSvgResult, type SvgNode, VERSION, type ValidateResult, type ViewBox, computeContentBox, installDomShim, loadAuthoringGuide, renderToJpeg, renderToPdf, renderToPng, renderToPptx, renderToSvg, serializeSvg, svgDisplayDimensions, toSvgString, validate };