reladraw 0.20.0 → 0.21.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/README.md +2 -2
- package/SYNTAX.md +31 -4
- package/dist/ast.d.ts +9 -2
- package/dist/ast.js +9 -2
- package/dist/cli.js +35 -12
- package/dist/constants.d.ts +4 -0
- package/dist/constants.js +8 -0
- package/dist/constrain.d.ts +56 -0
- package/dist/constrain.js +95 -0
- package/dist/model.d.ts +13 -0
- package/dist/model.js +7 -0
- package/dist/page.d.ts +14 -0
- package/dist/page.js +488 -0
- package/dist/parser.js +21 -3
- package/dist/place.d.ts +19 -0
- package/dist/place.js +67 -8
- package/dist/render.d.ts +17 -0
- package/dist/render.js +101 -29
- package/dist/resolve.d.ts +14 -0
- package/dist/resolve.js +33 -7
- package/dist/themes.d.ts +1 -0
- package/dist/themes.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -45,7 +45,7 @@ npm install -g reladraw
|
|
|
45
45
|
reladraw render diagram.reladraw -o diagram.svg
|
|
46
46
|
```
|
|
47
47
|
|
|
48
|
-
`reladraw check diagram.reladraw` prints the warnings without writing a picture, and `reladraw format diagram.reladraw` lines its statements up in columns.
|
|
48
|
+
`reladraw check diagram.reladraw` prints the warnings without writing a picture, and `reladraw format diagram.reladraw` lines its statements up in columns. `reladraw render diagram.reladraw --page` writes a page for looking at it in a browser, with pan, zoom, full screen and the warnings beside it.
|
|
49
49
|
|
|
50
50
|
Or from a clone, which also gets you the examples:
|
|
51
51
|
|
|
@@ -88,7 +88,7 @@ That gets you a copy of the skill at the time you run it, so you'll need to re-r
|
|
|
88
88
|
|
|
89
89
|
## Status
|
|
90
90
|
|
|
91
|
-
Version 0.
|
|
91
|
+
Version 0.21.0. Early stage, but works. The parser, layout engine, and SVG renderer are written in TypeScript, with zero runtime dependencies. There is a command-line tool that turns a .reladraw text file into an SVG, and an element that does the same inside a web page.
|
|
92
92
|
|
|
93
93
|
## License
|
|
94
94
|
|
package/SYNTAX.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# Syntax reference — 0.
|
|
1
|
+
# Syntax reference — 0.21.0
|
|
2
2
|
|
|
3
3
|
What the language accepts. The changelog at the end lists what changed in each published version; 0.17.0's changes are large, and its entry has a table of every old spelling and what to write instead.
|
|
4
4
|
|
|
@@ -241,7 +241,7 @@ above-left of X above-right of X below-left of X below-right of X
|
|
|
241
241
|
|
|
242
242
|
**A gap is exactly what was said.** `right of a` puts the box a `normal` gap from `a`'s right side. The gap goes in the placement's bracket, `right of a (gap: tight)`, or as a distance in front, `2 right of a`; writing both is refused as said twice. A diagonal distance is per axis: `2 below-right of a` is 2 across and 2 down.
|
|
243
243
|
|
|
244
|
-
**A lone direction also centers on its target.** `right of a` alone puts the box level with `a`'s center, because walking right from something keeps you on its center line. That half gives way as soon as another placement says how high.
|
|
244
|
+
**A lone direction also centers on its target.** `right of a` alone puts the box level with `a`'s center, because walking right from something keeps you on its center line. That half gives way as soon as another placement says how high, whichever box it is written on: with `node b right of a` and `node a even with b.1`, B goes where B.1 is level with A, rather than centered on it.
|
|
245
245
|
|
|
246
246
|
```
|
|
247
247
|
node parser
|
|
@@ -316,7 +316,7 @@ A target need not be written first, but there must be an order in which each box
|
|
|
316
316
|
|
|
317
317
|
### One anchor
|
|
318
318
|
|
|
319
|
-
Exactly one box in the file places itself, and every other is placed relative to it, directly or through others. Two unplaced boxes are refused by name. Chaining, `b right of a` then `c right of b`, is the plain way to write a few in a row; `layout:` ([Containers](#containers)) is for "these, in a line, in written order". Place against neighbors rather than hanging many boxes off one small one: a gap from a small box says nothing about the large ones also hanging off it.
|
|
319
|
+
Exactly one box in the file places itself, and every other is placed relative to it, directly or through others. Two unplaced boxes are refused by name. The anchor may be split between two boxes, one saying nothing across and the other nothing up and down: with `node a "A" even with b.1` and `node b "B" right of a`, A says nothing across, and B, once its centering on A gives way, nothing up and down. Chaining, `b right of a` then `c right of b`, is the plain way to write a few in a row; `layout:` ([Containers](#containers)) is for "these, in a line, in written order". Place against neighbors rather than hanging many boxes off one small one: a gap from a small box says nothing about the large ones also hanging off it.
|
|
320
320
|
|
|
321
321
|
### A box goes where it is placed
|
|
322
322
|
|
|
@@ -1033,6 +1033,14 @@ A key means one thing everywhere, so it can be wrong: `border:` on a node with n
|
|
|
1033
1033
|
|
|
1034
1034
|
**Changing a color never moves anything.** Size is different: a bigger text or a thicker line takes more room.
|
|
1035
1035
|
|
|
1036
|
+
### A border's bracket
|
|
1037
|
+
|
|
1038
|
+
```
|
|
1039
|
+
node host "Host" border: (color: #d2904e, thickness: thick)
|
|
1040
|
+
```
|
|
1041
|
+
|
|
1042
|
+
`border: red` is short for `border: (color: red)`. `thickness:` takes `thin`, `normal` (the default), `thick`, a multiple of normal such as `3`, or pixels such as `4px`; `border: thick` is refused with the bracket it meant. A thick border is drawn inside the box's outline and the box grows by the extra, so its padding is still measured from the inside of the stroke, a gap to its neighbor from the outside, and a line still ends on its outer edge. A stated `width:` or `height:` is the outside of the box, stroke included. A region's border is drawn inside the edge its points state, and the region doesn't grow.
|
|
1043
|
+
|
|
1036
1044
|
### Styles
|
|
1037
1045
|
|
|
1038
1046
|
```
|
|
@@ -1114,9 +1122,12 @@ A default takes the words a style does and may wear a style of its own (`default
|
|
|
1114
1122
|
|
|
1115
1123
|
```
|
|
1116
1124
|
diagram theme: nord background: #1e2229 text: (color: #e0e0e0)
|
|
1125
|
+
diagram title: "How a login reaches the database"
|
|
1117
1126
|
```
|
|
1118
1127
|
|
|
1119
|
-
`diagram` holds what covers the whole drawing: `theme`, `background` (the page behind everything), `text: (color: …)` for every text, `guides: show`,
|
|
1128
|
+
`diagram` holds what covers the whole drawing: `theme`, `background` (the page behind everything), `text: (color: …)` for every text, `guides: show`, `checks: (…)` ([Findings](#errors-and-findings)), and `title`. It has no name, since a file holds one diagram, and a second one is an error. `diagram text:` takes a color only; a text size belongs on a default.
|
|
1129
|
+
|
|
1130
|
+
`title:` names the drawing, in quotes. It isn't drawn: the SVG carries it as its own title, which a browser shows on the tab and a screen reader announces, and a page made with `--page` is headed with it.
|
|
1120
1131
|
|
|
1121
1132
|
### Destinations
|
|
1122
1133
|
|
|
@@ -1168,6 +1179,7 @@ reladraw check diagram.reladraw
|
|
|
1168
1179
|
| option | |
|
|
1169
1180
|
|---|---|
|
|
1170
1181
|
| `-o`, `--out` | where `render` writes; defaults to the input with `.svg`; `-` for standard output |
|
|
1182
|
+
| `--page` | `render` writes a page for looking at the diagram instead, defaulting to the input with `.html` |
|
|
1171
1183
|
| `--theme <name>` | render in another theme, whatever the file says |
|
|
1172
1184
|
| `--guides` | draw the guides |
|
|
1173
1185
|
| `--checks "<kind>: on, …"` | turn kinds of finding on or off, over the file |
|
|
@@ -1176,6 +1188,14 @@ reladraw check diagram.reladraw
|
|
|
1176
1188
|
|
|
1177
1189
|
The output is a standalone SVG: every picture is drawn into it as path data, never from a font or a linked file. A web page can draw a diagram in place with `<reladraw-diagram>`; see "In a web page" in the README.
|
|
1178
1190
|
|
|
1191
|
+
### A page for looking at a diagram
|
|
1192
|
+
|
|
1193
|
+
```
|
|
1194
|
+
reladraw render arch.reladraw --page
|
|
1195
|
+
```
|
|
1196
|
+
|
|
1197
|
+
`--page` writes `arch.html`: one self-contained file, for opening in a browser, attaching or committing, that needs nothing running and fetches nothing. The page is in the diagram's own colors, so a dark diagram doesn't sit on a white page, and it is headed with `diagram title:`, or the file's name when there is none. Drag to pan; scroll, pinch or double-click to zoom; Fit and Actual size reset the view, and there is a full-screen button. Save SVG and Save PNG save the drawing as `render` would. The findings are listed beside it in a panel that folds away, and clicking one lights up what its line drew. A box's or a line's name shows under the pointer. Keys: `+` and `-` zoom, `0` fits, `1` is actual size, `f` is full screen.
|
|
1198
|
+
|
|
1179
1199
|
### Formatting
|
|
1180
1200
|
|
|
1181
1201
|
```
|
|
@@ -1295,6 +1315,13 @@ Designed and absent from the code.
|
|
|
1295
1315
|
|
|
1296
1316
|
Pre-1.0, so the minor number is where a breaking change goes. Every removal is refused by name with the replacement quoted, rather than dropped in silence: an older file stops with an error saying what to write instead.
|
|
1297
1317
|
|
|
1318
|
+
**0.21.0**
|
|
1319
|
+
|
|
1320
|
+
- **A diagram can have a title**: `diagram title: "How a login reaches the database"`. The SVG carries it as its own title, which a browser shows on the tab. See [The diagram](#the-diagram).
|
|
1321
|
+
- **`reladraw render --page`** writes a page for looking at a diagram: one self-contained HTML file in the diagram's colors, headed with its title, that pans, zooms, goes full screen, saves the SVG or a PNG, lists the findings and lights up what each one is about. See [A page for looking at a diagram](#a-page-for-looking-at-a-diagram).
|
|
1322
|
+
- **A border can be thicker or thinner**: `border: (color: #d2904e, thickness: thick)`, mirroring the line's bracket. A thick border is drawn inside the box, and the box grows to keep its padding, so it may move what is placed beside it. See [A border's bracket](#a-borders-bracket).
|
|
1323
|
+
- **A box can be lined up with something inside a box placed against it.** `node a "Node A" even with b.1` with `node b "Node B" right of a` used to be refused, "nothing anchors the diagram", because `right of a` also centered B on A. That centering now gives way, as it already did to a placement written on B itself, and B goes where B.1 is level with A.
|
|
1324
|
+
|
|
1298
1325
|
**0.20.0**
|
|
1299
1326
|
|
|
1300
1327
|
- **`stays above`, `stays below`, `stays left of` and `stays right of`** keep a child placed in its box's corner clear of the other children: `node laptop.mark icon: laptop inside laptop top-right stays above laptop.photos`. The laptop stays in the corner, and the box makes room. See [Against its own parent](#against-its-own-parent).
|
package/dist/ast.d.ts
CHANGED
|
@@ -354,6 +354,12 @@ export declare const TEXT_KEYS: readonly ["color", "size", "wrap", "align", "at"
|
|
|
354
354
|
* of `line: (color: red)`, and both are stored under the bare `line` key.
|
|
355
355
|
*/
|
|
356
356
|
export declare const LINE_KEYS: readonly ["color", "path", "corners", "crossing", "pattern", "thickness", "curve", "turn", "step-out"];
|
|
357
|
+
/**
|
|
358
|
+
* What a box's border takes in its bracket: `border: (color: red, thickness:
|
|
359
|
+
* thick)`, mirroring the line's. `border: red` is the short form of the color,
|
|
360
|
+
* and both are stored under the bare `border` key.
|
|
361
|
+
*/
|
|
362
|
+
export declare const BORDER_KEYS: readonly ["color", "thickness"];
|
|
357
363
|
/**
|
|
358
364
|
* The words each of the line's properties takes, the default first. Thickness
|
|
359
365
|
* takes a multiple of normal (`2`) or pixels (`3px`) as well. `turn:` takes a fraction and
|
|
@@ -655,9 +661,10 @@ export interface DiagramStmt {
|
|
|
655
661
|
* `text: (color: …)`, and sets the text color of everything at once — the
|
|
656
662
|
* theme has one text color, shared by nodes and edges, and this is that.
|
|
657
663
|
* `checks` takes a bracket too, `checks: (edge-crossings: on)`, turning kinds
|
|
658
|
-
* of finding on or off by name.
|
|
664
|
+
* of finding on or off by name. `title` is quoted, and names the drawing: the
|
|
665
|
+
* SVG carries it as its own title, and a page made with `--page` heads with it.
|
|
659
666
|
*/
|
|
660
|
-
export declare const DIAGRAM_KEYS: readonly ["theme", "background", "text", "checks", "guides"];
|
|
667
|
+
export declare const DIAGRAM_KEYS: readonly ["theme", "background", "text", "checks", "guides", "title"];
|
|
661
668
|
/**
|
|
662
669
|
* `default leaf fill: #2e5d3a` — a style that applies to every thing of one
|
|
663
670
|
* kind without being named. The more specific wins: `node` covers every node,
|
package/dist/ast.js
CHANGED
|
@@ -298,6 +298,12 @@ export const TEXT_KEYS = ['color', 'size', 'wrap', 'align', 'at'];
|
|
|
298
298
|
* of `line: (color: red)`, and both are stored under the bare `line` key.
|
|
299
299
|
*/
|
|
300
300
|
export const LINE_KEYS = ['color', 'path', 'corners', 'crossing', 'pattern', 'thickness', 'curve', 'turn', 'step-out'];
|
|
301
|
+
/**
|
|
302
|
+
* What a box's border takes in its bracket: `border: (color: red, thickness:
|
|
303
|
+
* thick)`, mirroring the line's. `border: red` is the short form of the color,
|
|
304
|
+
* and both are stored under the bare `border` key.
|
|
305
|
+
*/
|
|
306
|
+
export const BORDER_KEYS = ['color', 'thickness'];
|
|
301
307
|
/**
|
|
302
308
|
* The words each of the line's properties takes, the default first. Thickness
|
|
303
309
|
* takes a multiple of normal (`2`) or pixels (`3px`) as well. `turn:` takes a fraction and
|
|
@@ -345,9 +351,10 @@ export const REGION_KEYS = ['style', 'fill', 'opacity', 'border', 'padding', 'pa
|
|
|
345
351
|
* `text: (color: …)`, and sets the text color of everything at once — the
|
|
346
352
|
* theme has one text color, shared by nodes and edges, and this is that.
|
|
347
353
|
* `checks` takes a bracket too, `checks: (edge-crossings: on)`, turning kinds
|
|
348
|
-
* of finding on or off by name.
|
|
354
|
+
* of finding on or off by name. `title` is quoted, and names the drawing: the
|
|
355
|
+
* SVG carries it as its own title, and a page made with `--page` heads with it.
|
|
349
356
|
*/
|
|
350
|
-
export const DIAGRAM_KEYS = ['theme', 'background', 'text', 'checks', 'guides'];
|
|
357
|
+
export const DIAGRAM_KEYS = ['theme', 'background', 'text', 'checks', 'guides', 'title'];
|
|
351
358
|
export const DEFAULT_TARGETS = [
|
|
352
359
|
'node',
|
|
353
360
|
'leaf',
|
package/dist/cli.js
CHANGED
|
@@ -1,16 +1,18 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { existsSync, readFileSync } from 'node:fs';
|
|
3
3
|
import { mkdir, readFile, writeFile } from 'node:fs/promises';
|
|
4
|
-
import { dirname, resolve as resolvePath } from 'node:path';
|
|
4
|
+
import { basename, dirname, resolve as resolvePath } from 'node:path';
|
|
5
5
|
import { nearMatch, readChecks } from './checks.js';
|
|
6
6
|
import { SourceError } from './errors.js';
|
|
7
7
|
import { format } from './format.js';
|
|
8
8
|
import { DEFAULT_FORMAT, readFormatSettings, SETTINGS_FILE, settingsFileText } from './format-settings.js';
|
|
9
9
|
import { draw } from './index.js';
|
|
10
|
+
import { drawPage } from './page.js';
|
|
10
11
|
import { THEME_NAMES, THEMES } from './themes.js';
|
|
11
12
|
const USAGE = `reladraw — draw a diagram from stated placement
|
|
12
13
|
|
|
13
14
|
reladraw render <input.reladraw> [-o <output.svg>] [options]
|
|
15
|
+
reladraw render <input.reladraw> --page [-o <output.html>] [options]
|
|
14
16
|
reladraw check <input.reladraw> [options]
|
|
15
17
|
reladraw format <input.reladraw>... [--check]
|
|
16
18
|
reladraw format - [--check]
|
|
@@ -33,6 +35,11 @@ const USAGE = `reladraw — draw a diagram from stated placement
|
|
|
33
35
|
|
|
34
36
|
-o, --out where render writes the SVG. Defaults to the input path with
|
|
35
37
|
its extension replaced by .svg. Use - for standard output.
|
|
38
|
+
--page render writes a page for looking at the diagram instead: one
|
|
39
|
+
HTML file in the diagram's colors, headed with its title, that
|
|
40
|
+
pans, zooms, goes full screen, saves the SVG or a PNG, lists the
|
|
41
|
+
findings and names what is under the pointer. Defaults to the
|
|
42
|
+
input path with .html.
|
|
36
43
|
--theme render in this theme, whatever the file's \`diagram theme:\`
|
|
37
44
|
says. One of ${THEME_NAMES.join(', ')}.
|
|
38
45
|
--guides draw the guides: everything written with \`guide\` in front,
|
|
@@ -79,6 +86,7 @@ async function main(argv) {
|
|
|
79
86
|
let checks;
|
|
80
87
|
let noChecks = false;
|
|
81
88
|
let guides = false;
|
|
89
|
+
let page = false;
|
|
82
90
|
for (let i = 1; i < argv.length; i += 1) {
|
|
83
91
|
const arg = argv[i];
|
|
84
92
|
if ((arg === '-o' || arg === '--out') && command === 'render') {
|
|
@@ -119,6 +127,13 @@ async function main(argv) {
|
|
|
119
127
|
else if (arg === '--guides') {
|
|
120
128
|
guides = true;
|
|
121
129
|
}
|
|
130
|
+
else if (arg === '--page' && command === 'render') {
|
|
131
|
+
page = true;
|
|
132
|
+
}
|
|
133
|
+
else if (arg === '--page') {
|
|
134
|
+
process.stderr.write('reladraw: check writes nothing, so it takes no --page; reladraw render --page writes one\n');
|
|
135
|
+
return 1;
|
|
136
|
+
}
|
|
122
137
|
else if (arg.startsWith('-') && arg !== '-') {
|
|
123
138
|
process.stderr.write(`reladraw: unknown option ${arg}\n`);
|
|
124
139
|
return 1;
|
|
@@ -157,16 +172,24 @@ async function main(argv) {
|
|
|
157
172
|
overChecks = (fromFile) => readChecks(written, fromFile);
|
|
158
173
|
}
|
|
159
174
|
const source = await readFile(input, 'utf8');
|
|
175
|
+
const options = {
|
|
176
|
+
...(theme === undefined ? {} : { theme: THEMES[theme] }),
|
|
177
|
+
...(guides ? { guides: true } : {}),
|
|
178
|
+
// An icon's file is found from the diagram, not from wherever the tool
|
|
179
|
+
// happens to be run.
|
|
180
|
+
readIconFile: (path) => readFileSync(resolvePath(dirname(input), path), 'utf8'),
|
|
181
|
+
...(noChecks ? { checks: false } : overChecks ? { checks: overChecks } : {}),
|
|
182
|
+
};
|
|
160
183
|
let drawn;
|
|
161
184
|
try {
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
}
|
|
185
|
+
if (page) {
|
|
186
|
+
const made = drawPage(source, basename(input).replace(/\.[^.]+$/, ''), options);
|
|
187
|
+
drawn = { text: made.html, findings: made.findings };
|
|
188
|
+
}
|
|
189
|
+
else {
|
|
190
|
+
const made = draw(source, options);
|
|
191
|
+
drawn = { text: made.svg, findings: made.findings };
|
|
192
|
+
}
|
|
170
193
|
}
|
|
171
194
|
catch (error) {
|
|
172
195
|
if (error instanceof SourceError) {
|
|
@@ -180,12 +203,12 @@ async function main(argv) {
|
|
|
180
203
|
if (command === 'check')
|
|
181
204
|
return 0;
|
|
182
205
|
if (out === '-') {
|
|
183
|
-
process.stdout.write(drawn.
|
|
206
|
+
process.stdout.write(drawn.text);
|
|
184
207
|
return 0;
|
|
185
208
|
}
|
|
186
|
-
const target = resolvePath(out ?? input.replace(/\.[^.]+$/, '') + '.svg');
|
|
209
|
+
const target = resolvePath(out ?? input.replace(/\.[^.]+$/, '') + (page ? '.html' : '.svg'));
|
|
187
210
|
await mkdir(dirname(target), { recursive: true });
|
|
188
|
-
await writeFile(target, drawn.
|
|
211
|
+
await writeFile(target, drawn.text, 'utf8');
|
|
189
212
|
process.stderr.write(`${target}\n`);
|
|
190
213
|
return 0;
|
|
191
214
|
}
|
package/dist/constants.d.ts
CHANGED
|
@@ -55,6 +55,10 @@ export declare const ARROW_MARKER_WIDTH = 7;
|
|
|
55
55
|
export declare const ARROW_LENGTH: number;
|
|
56
56
|
/** What `thickness: thin | normal | thick` draw, in pixels. `normal` is the line as it always was. */
|
|
57
57
|
export declare const THICKNESS: Record<string, number>;
|
|
58
|
+
/** A box's border as it always was, in pixels: `border: (thickness: normal)`. */
|
|
59
|
+
export declare const BORDER_WIDTH = 1;
|
|
60
|
+
/** What a border's `thickness: thin | normal | thick` draw, in pixels. */
|
|
61
|
+
export declare const BORDER_THICKNESS: Record<string, number>;
|
|
58
62
|
/**
|
|
59
63
|
* How much of a line of this thickness its arrowhead covers. The marker scales
|
|
60
64
|
* with the stroke, so a thick line gets a bigger head, and the room an edge's
|
package/dist/constants.js
CHANGED
|
@@ -65,6 +65,14 @@ export const THICKNESS = {
|
|
|
65
65
|
normal: LINE_WIDTH,
|
|
66
66
|
thick: LINE_WIDTH * 2,
|
|
67
67
|
};
|
|
68
|
+
/** A box's border as it always was, in pixels: `border: (thickness: normal)`. */
|
|
69
|
+
export const BORDER_WIDTH = 1;
|
|
70
|
+
/** What a border's `thickness: thin | normal | thick` draw, in pixels. */
|
|
71
|
+
export const BORDER_THICKNESS = {
|
|
72
|
+
thin: BORDER_WIDTH / 2,
|
|
73
|
+
normal: BORDER_WIDTH,
|
|
74
|
+
thick: BORDER_WIDTH * 2,
|
|
75
|
+
};
|
|
68
76
|
/**
|
|
69
77
|
* How much of a line of this thickness its arrowhead covers. The marker scales
|
|
70
78
|
* with the stroke, so a thick line gets a bigger head, and the room an edge's
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import type { Placement } from './ast.js';
|
|
2
|
+
/**
|
|
3
|
+
* The tightest arrangement satisfying a set of relative distances.
|
|
4
|
+
*
|
|
5
|
+
* Every placement in the language turns into the same shape of fact: one thing sits
|
|
6
|
+
* at least so far along an axis from another. Written as `pos[to] >= pos[from]
|
|
7
|
+
* + weight`, a whole diagram is a system of those, and the arrangement the
|
|
8
|
+
* author meant is the one where nothing is further apart than it has to be.
|
|
9
|
+
*
|
|
10
|
+
* That arrangement is unique and is found by longest paths, so no search
|
|
11
|
+
* happens and no alternative is ever weighed. Distances come out of the
|
|
12
|
+
* arithmetic; which side of what a thing sits on came from the author.
|
|
13
|
+
*/
|
|
14
|
+
export interface Constraint {
|
|
15
|
+
from: number;
|
|
16
|
+
to: number;
|
|
17
|
+
weight: number;
|
|
18
|
+
/** The placement this came from, so a failure can be reported in the author's words. */
|
|
19
|
+
placement?: Placement;
|
|
20
|
+
}
|
|
21
|
+
/** An exact distance, which is two inequalities pointing opposite ways. */
|
|
22
|
+
export declare function fix(from: number, to: number, distance: number, placement?: Placement): Constraint[];
|
|
23
|
+
export interface Contradiction {
|
|
24
|
+
/** The placements that cannot all hold. Empty if the loop is entirely implicit. */
|
|
25
|
+
placements: Placement[];
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Solve, or report the placements that fight.
|
|
29
|
+
*
|
|
30
|
+
* Longest path from a virtual source joined to everything at zero, which is
|
|
31
|
+
* Bellman-Ford with the comparison flipped. A distance that keeps growing after
|
|
32
|
+
* one pass per node means the constraints run in a circle that demands ever
|
|
33
|
+
* more room, and that circle is exactly the set of placements to quote back.
|
|
34
|
+
*/
|
|
35
|
+
export declare function tightest(count: number, constraints: Constraint[]): {
|
|
36
|
+
positions: number[];
|
|
37
|
+
} | {
|
|
38
|
+
contradiction: Contradiction;
|
|
39
|
+
};
|
|
40
|
+
/**
|
|
41
|
+
* Which members can be driven apart from which, along one axis.
|
|
42
|
+
*
|
|
43
|
+
* A constraint says `pos[to] >= pos[from] + weight`, so it bounds `to` from
|
|
44
|
+
* below and leaves it free to move further away. Follow those edges and the
|
|
45
|
+
* question "may this one end up beyond that one?" becomes plain reachability:
|
|
46
|
+
* if `j` is reachable from `i` and `i` is not reachable from `j`, then the file
|
|
47
|
+
* lets the distance from `i` to `j` grow without limit and never lets it be
|
|
48
|
+
* closed from the other side. Pushing `j` past `i` is then the one separation
|
|
49
|
+
* the file allows, and no choice was made.
|
|
50
|
+
*
|
|
51
|
+
* Reachable both ways means the two are pinned at a fixed distance, so they
|
|
52
|
+
* cannot be separated along this axis at all. Reachable neither way means the
|
|
53
|
+
* file said nothing that orders them, which is the case the caller must refuse
|
|
54
|
+
* rather than guess at.
|
|
55
|
+
*/
|
|
56
|
+
export declare function reachability(count: number, constraints: Constraint[]): boolean[][];
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/** An exact distance, which is two inequalities pointing opposite ways. */
|
|
2
|
+
export function fix(from, to, distance, placement) {
|
|
3
|
+
return [
|
|
4
|
+
{ from, to, weight: distance, ...(placement ? { placement } : {}) },
|
|
5
|
+
{ from: to, to: from, weight: -distance, ...(placement ? { placement } : {}) },
|
|
6
|
+
];
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Solve, or report the placements that fight.
|
|
10
|
+
*
|
|
11
|
+
* Longest path from a virtual source joined to everything at zero, which is
|
|
12
|
+
* Bellman-Ford with the comparison flipped. A distance that keeps growing after
|
|
13
|
+
* one pass per node means the constraints run in a circle that demands ever
|
|
14
|
+
* more room, and that circle is exactly the set of placements to quote back.
|
|
15
|
+
*/
|
|
16
|
+
export function tightest(count, constraints) {
|
|
17
|
+
const positions = new Array(count).fill(0);
|
|
18
|
+
const cameFrom = new Array(count).fill(undefined);
|
|
19
|
+
for (let pass = 0; pass < count; pass += 1) {
|
|
20
|
+
let moved = false;
|
|
21
|
+
for (const constraint of constraints) {
|
|
22
|
+
const candidate = positions[constraint.from] + constraint.weight;
|
|
23
|
+
if (candidate > positions[constraint.to] + 1e-9) {
|
|
24
|
+
positions[constraint.to] = candidate;
|
|
25
|
+
cameFrom[constraint.to] = constraint;
|
|
26
|
+
moved = true;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
if (!moved)
|
|
30
|
+
return { positions };
|
|
31
|
+
}
|
|
32
|
+
// Still moving after a full pass per node, so some loop demands more room
|
|
33
|
+
// every time round it. Walk backwards to find it.
|
|
34
|
+
for (const constraint of constraints) {
|
|
35
|
+
if (positions[constraint.from] + constraint.weight > positions[constraint.to] + 1e-9) {
|
|
36
|
+
return { contradiction: { placements: loopFrom(constraint.to, cameFrom, count) } };
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return { contradiction: { placements: [] } };
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Which members can be driven apart from which, along one axis.
|
|
43
|
+
*
|
|
44
|
+
* A constraint says `pos[to] >= pos[from] + weight`, so it bounds `to` from
|
|
45
|
+
* below and leaves it free to move further away. Follow those edges and the
|
|
46
|
+
* question "may this one end up beyond that one?" becomes plain reachability:
|
|
47
|
+
* if `j` is reachable from `i` and `i` is not reachable from `j`, then the file
|
|
48
|
+
* lets the distance from `i` to `j` grow without limit and never lets it be
|
|
49
|
+
* closed from the other side. Pushing `j` past `i` is then the one separation
|
|
50
|
+
* the file allows, and no choice was made.
|
|
51
|
+
*
|
|
52
|
+
* Reachable both ways means the two are pinned at a fixed distance, so they
|
|
53
|
+
* cannot be separated along this axis at all. Reachable neither way means the
|
|
54
|
+
* file said nothing that orders them, which is the case the caller must refuse
|
|
55
|
+
* rather than guess at.
|
|
56
|
+
*/
|
|
57
|
+
export function reachability(count, constraints) {
|
|
58
|
+
const reach = Array.from({ length: count }, () => new Array(count).fill(false));
|
|
59
|
+
for (const constraint of constraints)
|
|
60
|
+
reach[constraint.from][constraint.to] = true;
|
|
61
|
+
for (let via = 0; via < count; via += 1) {
|
|
62
|
+
for (let from = 0; from < count; from += 1) {
|
|
63
|
+
if (!reach[from][via])
|
|
64
|
+
continue;
|
|
65
|
+
for (let to = 0; to < count; to += 1) {
|
|
66
|
+
if (reach[via][to])
|
|
67
|
+
reach[from][to] = true;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return reach;
|
|
72
|
+
}
|
|
73
|
+
/** The placements on the loop reached by following each position back to what set it. */
|
|
74
|
+
function loopFrom(start, cameFrom, count) {
|
|
75
|
+
let at = start;
|
|
76
|
+
for (let step = 0; step < count; step += 1) {
|
|
77
|
+
const previous = cameFrom[at];
|
|
78
|
+
if (previous === undefined)
|
|
79
|
+
break;
|
|
80
|
+
at = previous.from;
|
|
81
|
+
}
|
|
82
|
+
const placements = [];
|
|
83
|
+
const seen = new Set();
|
|
84
|
+
let cursor = at;
|
|
85
|
+
while (!seen.has(cursor)) {
|
|
86
|
+
seen.add(cursor);
|
|
87
|
+
const previous = cameFrom[cursor];
|
|
88
|
+
if (previous === undefined)
|
|
89
|
+
break;
|
|
90
|
+
if (previous.placement)
|
|
91
|
+
placements.push(previous.placement);
|
|
92
|
+
cursor = previous.from;
|
|
93
|
+
}
|
|
94
|
+
return placements.reverse();
|
|
95
|
+
}
|
package/dist/model.d.ts
CHANGED
|
@@ -30,6 +30,12 @@ export interface LayoutNode {
|
|
|
30
30
|
* a document's are read; drawing alone depends on it, never placement.
|
|
31
31
|
*/
|
|
32
32
|
corners: NodeCorners;
|
|
33
|
+
/**
|
|
34
|
+
* How wide the border is drawn, in pixels, from `border: (thickness: …)`;
|
|
35
|
+
* left out when it is the normal width. The stroke lies inside the outline,
|
|
36
|
+
* and the padding grows by the extra, so a thick border takes room.
|
|
37
|
+
*/
|
|
38
|
+
borderWidth?: number;
|
|
33
39
|
/** The text as written, before line splitting. */
|
|
34
40
|
text: string;
|
|
35
41
|
/** The text split into the lines that will be drawn, each as its runs. */
|
|
@@ -138,6 +144,8 @@ export interface LayoutMark {
|
|
|
138
144
|
names: string[];
|
|
139
145
|
appearance: Attrs;
|
|
140
146
|
guide: boolean;
|
|
147
|
+
/** A region's border width in pixels, left out when normal; drawn inside its stated edge. */
|
|
148
|
+
borderWidth?: number;
|
|
141
149
|
/** Its text, if it has one, and where that sits on the page (design.org 7.4). */
|
|
142
150
|
text?: MarkTextBox;
|
|
143
151
|
line: number;
|
|
@@ -325,6 +333,11 @@ export interface Layout {
|
|
|
325
333
|
/** How a shown guide of each kind looks: `default guide node` and the rest. */
|
|
326
334
|
guideLooks: Record<'node' | 'edge' | 'point' | 'region', Attrs>;
|
|
327
335
|
}
|
|
336
|
+
/**
|
|
337
|
+
* Whether a deck, which draws no outline of its own, was given one: a fill, or
|
|
338
|
+
* something about its border.
|
|
339
|
+
*/
|
|
340
|
+
export declare function outlineStated(appearance: Attrs): boolean;
|
|
328
341
|
/**
|
|
329
342
|
* Whether a node is joined to its container's text, directly or through
|
|
330
343
|
* siblings joined to it: one unit with the text, placed by the text's position
|
package/dist/model.js
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Whether a deck, which draws no outline of its own, was given one: a fill, or
|
|
3
|
+
* something about its border.
|
|
4
|
+
*/
|
|
5
|
+
export function outlineStated(appearance) {
|
|
6
|
+
return appearance['fill'] !== undefined || appearance['border'] !== undefined || appearance['border.thickness'] !== undefined;
|
|
7
|
+
}
|
|
1
8
|
/**
|
|
2
9
|
* Whether a node is joined to its container's text, directly or through
|
|
3
10
|
* siblings joined to it: one unit with the text, placed by the text's position
|
package/dist/page.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { type Finding } from './checks.js';
|
|
2
|
+
import type { DrawOptions } from './index.js';
|
|
3
|
+
/**
|
|
4
|
+
* `reladraw render --page`: one diagram in one self-contained HTML page, made
|
|
5
|
+
* for looking at rather than embedding. The page is the diagram's own colors,
|
|
6
|
+
* headed with its title, and it pans, zooms, goes full screen, saves the SVG
|
|
7
|
+
* or a PNG, lists the findings, and names a box under the pointer. It needs
|
|
8
|
+
* nothing running and fetches nothing, so it can be attached, mailed or
|
|
9
|
+
* committed. `name` is the file's name, the title when the file states none.
|
|
10
|
+
*/
|
|
11
|
+
export declare function drawPage(source: string, name: string, options?: DrawOptions): {
|
|
12
|
+
html: string;
|
|
13
|
+
findings: Finding[];
|
|
14
|
+
};
|