@adecore/diagram 0.62.1 → 0.63.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/dist/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export { type NodeBox, type GroupBox, type EdgeLabelBox, type EdgeRoute, type DiagramLayout, GROUP_LABEL_BAND, layoutOf, type Point, type Rect } from './layout.ts';
2
2
  export { LABEL_SIZE, SUB_SIZE } from './text.ts';
3
3
  export { readingOrder } from './reading-order.ts';
4
+ export { diagramScene } from './scene.ts';
4
5
  export { type ShapePaths, shapePaths, type TextLine, textLinesOf, edgeLabelLinesOf, edgePath, arrowHeadPath, dashOf } from './shapes.ts';
5
6
  export { type DiagramSvgOptions, DEFAULT_NODE_TONE, DEFAULT_GROUP_TONE, DEFAULT_EDGE_TONE, toSvg } from './svg.ts';
package/dist/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  export { GROUP_LABEL_BAND, layoutOf } from './layout.js';
2
2
  export { LABEL_SIZE, SUB_SIZE } from './text.js';
3
3
  export { readingOrder } from './reading-order.js';
4
+ export { diagramScene } from './scene.js';
4
5
  export { shapePaths, textLinesOf, edgeLabelLinesOf, edgePath, arrowHeadPath, dashOf } from './shapes.js';
5
6
  export { DEFAULT_NODE_TONE, DEFAULT_GROUP_TONE, DEFAULT_EDGE_TONE, toSvg } from './svg.js';
package/dist/layout.d.ts CHANGED
@@ -1,6 +1,6 @@
1
- import { type Point, type Rect } from '@adecore/drawing';
1
+ import { type Point, type Rect } from '@adecore/utils/geometry';
2
2
  import type { DiagramDocument, DiagramEdge, DiagramNode } from './protocol.ts';
3
- export type { Point, Rect } from '@adecore/drawing';
3
+ export type { Point, Rect } from '@adecore/utils/geometry';
4
4
  export interface NodeBox extends Rect {
5
5
  id: string;
6
6
  layer: number;
@@ -34,7 +34,10 @@ export declare const EDGE_LABEL_PADDING: {
34
34
  y: number;
35
35
  };
36
36
  export declare const GROUP_LABEL_BAND = 24;
37
- export interface NodeSize {
37
+ export declare const GROUP_LINE_WIDTH = 1;
38
+ export declare const GROUP_DASH: number[];
39
+ export declare function groupLabelBaselineY(box: Pick<GroupBox, 'y'>): number;
40
+ interface NodeSize {
38
41
  w: number;
39
42
  h: number;
40
43
  label: string[];
package/dist/layout.js CHANGED
@@ -1,4 +1,4 @@
1
- import { centerOf, intersects, roundPoint } from '@adecore/drawing';
1
+ import { centerOf, intersects, roundPoint } from '@adecore/utils/geometry';
2
2
  import { placeAcross } from './across.js';
3
3
  import { DUMMY_MARGIN, NODE_MARGIN, bandCode } from './graph.js';
4
4
  import { orderLayers } from './order.js';
@@ -12,13 +12,14 @@ const TEXT_MAX_WIDTH = NODE_MAX_WIDTH - NODE_PADDING_X * 2;
12
12
  /* A diamond holds its text in the middle of its box, so its lines are shorter and the box is twice their size. */
13
13
  const DIAMOND_TEXT_MAX_WIDTH = 160;
14
14
  const DIAMOND_MIN = { w: 168, h: 72 };
15
- /* The extra height of a cylinder, for the lid its text stays under. */
16
15
  export const CYLINDER_LID = 10;
17
16
  const EDGE_LABEL_MAX_WIDTH = 160;
18
17
  export const EDGE_LABEL_PADDING = { x: 4, y: 2 };
19
18
  const GROUP_PADDING = 20;
20
- /* The band on top of a group where its label sits. */
21
19
  export const GROUP_LABEL_BAND = 24;
20
+ /* A group's outline is thin and dashed, so it reads as a region and not as a node. */
21
+ export const GROUP_LINE_WIDTH = 1;
22
+ export const GROUP_DASH = [6, 4];
22
23
  const GROUP_LABEL_INSET = 12;
23
24
  /* Room between a layer's widest box and the first thing in the channel after it. */
24
25
  const EDGE_CLEAR = 20;
@@ -32,7 +33,10 @@ const SNAP = 6;
32
33
  function even(value) {
33
34
  return Math.ceil(value / 2) * 2;
34
35
  }
35
- /* The size of a node's box, from what it says wrapped to the widest a box gets, and the shape it wears. */
36
+ /* The baseline of a group's label, 0.7 of the way down the band that holds it. */
37
+ export function groupLabelBaselineY(box) {
38
+ return box.y + Math.round(GROUP_LABEL_BAND * 0.7);
39
+ }
36
40
  export function sizeOfNode(node) {
37
41
  const diamond = node.shape === 'diamond';
38
42
  const maxWidth = diamond ? DIAMOND_TEXT_MAX_WIDTH : TEXT_MAX_WIDTH;
@@ -52,7 +56,6 @@ export function sizeOfNode(node) {
52
56
  }
53
57
  return { w: even(Math.max(NODE_MIN_WIDTH, textWidth + paddingX * 2)), h: even(h), label, sub };
54
58
  }
55
- /* The box of an edge's label before it is placed: wrapped narrower than a node, on a padding of its own. */
56
59
  function sizeOfEdgeLabel(text) {
57
60
  const lines = wrapText(text, SUB_SIZE, false, EDGE_LABEL_MAX_WIDTH);
58
61
  return {
@@ -158,7 +161,6 @@ function unionOf(rects) {
158
161
  const bottom = Math.max(...rects.map((rect) => rect.y + rect.h));
159
162
  return { x: left, y: top, w: right - left, h: bottom - top };
160
163
  }
161
- /* Drops repeated corners and corners in the middle of a straight run. */
162
164
  function simplify(points) {
163
165
  const unique = points.filter((point, index) => index === 0 || point.x !== points[index - 1].x || point.y !== points[index - 1].y);
164
166
  return unique.filter((point, index) => {
@@ -250,7 +252,6 @@ function looseRoute(source, target, loop, down) {
250
252
  { x: to.x, y: end }
251
253
  ]);
252
254
  }
253
- /* Every node that is not pinned is a unit, sized in the right-running frame. */
254
255
  function unitsOf(nodes, layers, sizes, down) {
255
256
  const units = [];
256
257
  const unitOf = new Map();
@@ -307,7 +308,6 @@ function bandsOf(groups, units, unitOf, down) {
307
308
  });
308
309
  return { bands, bandOfGroup };
309
310
  }
310
- /* Splits every edge between two units into hops of one layer each, adding a point to `units` for every layer it skips. */
311
311
  function edgesOf(edges, units, unitOf, knownIds) {
312
312
  const hops = [];
313
313
  const chains = [];
@@ -360,7 +360,6 @@ function edgesOf(edges, units, unitOf, knownIds) {
360
360
  });
361
361
  return { hops, chains, loops, loose, lines };
362
362
  }
363
- /* The top-level entries of every layer, with the members of each band filled in and its height measured. */
364
363
  function itemsOf(units, bands, layerCount, down) {
365
364
  const items = Array.from({ length: layerCount }, () => []);
366
365
  units.forEach((unit, index) => {
@@ -440,7 +439,6 @@ function portsOf(units, top, hops, loops) {
440
439
  });
441
440
  return { hopStart, hopEnd, loopStart, loopEnd };
442
441
  }
443
- /* Places the layers along the flow, each followed by the channel its hops and labels are routed through. */
444
442
  function placeAlong(layering, edges, ports, labelSizes, layerCount, down) {
445
443
  const { units, bands } = layering;
446
444
  const { hops, chains, loops } = edges;
package/dist/route.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { Rect } from './layout.ts';
1
+ import { type Rect } from '@adecore/utils/geometry';
2
2
  export interface ChannelHop {
3
3
  hop: number;
4
4
  ya: number;
@@ -11,7 +11,7 @@ export interface ChannelLabel {
11
11
  along: number;
12
12
  across: number;
13
13
  }
14
- export interface ChannelInput {
14
+ interface ChannelInput {
15
15
  start: number;
16
16
  clearBefore: number;
17
17
  clearAfter: number;
@@ -22,9 +22,10 @@ export interface ChannelInput {
22
22
  h: number;
23
23
  }[];
24
24
  }
25
- export interface ChannelResult {
25
+ interface ChannelResult {
26
26
  width: number;
27
27
  trackX: Map<number, number>;
28
28
  labels: Map<number, Rect>;
29
29
  }
30
30
  export declare function routeChannel(input: ChannelInput): ChannelResult;
31
+ export {};
package/dist/route.js CHANGED
@@ -1,4 +1,4 @@
1
- import { intersects } from '@adecore/drawing';
1
+ import { intersects } from '@adecore/utils/geometry';
2
2
  const TRACK_GAP = 12;
3
3
  /* Space between the lane of labels and the first track. */
4
4
  const LANE_GAP = 12;
@@ -45,7 +45,6 @@ function conflictsOf(left, right) {
45
45
  }
46
46
  return total;
47
47
  }
48
- /* A hop drawn on a track in a channel of unit tracks, only to compare two orders of the tracks. */
49
48
  function sketchOf(hop, track) {
50
49
  return [
51
50
  { x: 0, y: hop.ya },
@@ -0,0 +1,3 @@
1
+ import { type Scene } from '@adecore/drawing';
2
+ import type { DiagramDocument } from './protocol.ts';
3
+ export declare function diagramScene(document: Pick<DiagramDocument, 'rev' | 'meta' | 'nodes' | 'groups' | 'edges'>): Scene;
package/dist/scene.js ADDED
@@ -0,0 +1,57 @@
1
+ import { sceneColor } from '@adecore/drawing';
2
+ import { GROUP_DASH, GROUP_LINE_WIDTH, groupLabelBaselineY, layoutOf } from './layout.js';
3
+ import { LINE_WIDTH, arrowHeadPath, dashLengthsOf, edgeLabelLinesOf, edgePath, shapePaths, textLinesOf } from './shapes.js';
4
+ import { DEFAULT_EDGE_TONE, DEFAULT_GROUP_TONE, DEFAULT_NODE_TONE } from './svg.js';
5
+ import { SUB_SIZE } from './text.js';
6
+ function elementAtOrigin(id, paths, text) {
7
+ return { id, x: 0, y: 0, angle: 0, centerX: 0, centerY: 0, paths, text };
8
+ }
9
+ function centeredText(line, tone) {
10
+ return { text: line.text, x: line.x, y: line.y, size: line.size, bold: line.bold, align: 'center', font: 'sans', color: tone };
11
+ }
12
+ /*
13
+ * The scene of a diagram, from the layout and paths `toSvg` draws with. Every element sits in world
14
+ * coordinates, and its id says what it is: `group:<id>`, `edge:<index>` or `node:<id>`.
15
+ */
16
+ export function diagramScene(document) {
17
+ const layout = layoutOf(document);
18
+ const nodes = new Map(document.nodes.map((node) => [node.id, node]));
19
+ const groups = new Map(document.groups.map((group) => [group.id, group]));
20
+ const elements = [];
21
+ for (const box of layout.groups) {
22
+ const group = groups.get(box.id);
23
+ const tone = sceneColor(group.tone ?? DEFAULT_GROUP_TONE);
24
+ elements.push(elementAtOrigin(`group:${box.id}`, [{ d: shapePaths('round', box).body, stroke: tone, fill: null, strokeWidth: GROUP_LINE_WIDTH, dash: [...GROUP_DASH] }], [
25
+ {
26
+ text: group.label,
27
+ x: box.labelBox.x,
28
+ y: groupLabelBaselineY(box),
29
+ size: SUB_SIZE,
30
+ bold: true,
31
+ align: 'left',
32
+ font: 'sans',
33
+ color: tone
34
+ }
35
+ ]));
36
+ }
37
+ for (const route of layout.edges) {
38
+ const edge = document.edges[route.index];
39
+ const tone = sceneColor(edge.tone ?? DEFAULT_EDGE_TONE);
40
+ const paths = [{ d: edgePath(route.points), stroke: tone, fill: null, strokeWidth: LINE_WIDTH, dash: dashLengthsOf(edge.style) }];
41
+ if (route.points.length > 0) {
42
+ paths.push({ d: arrowHeadPath(route.points), stroke: null, fill: tone, strokeWidth: 0, dash: null });
43
+ }
44
+ elements.push(elementAtOrigin(`edge:${route.index}`, paths, route.label ? edgeLabelLinesOf(route.label).map((line) => centeredText(line, tone)) : []));
45
+ }
46
+ for (const box of layout.nodes) {
47
+ const node = nodes.get(box.id);
48
+ const tone = node.tone ?? DEFAULT_NODE_TONE;
49
+ const shape = shapePaths(node.shape, box);
50
+ const paths = [{ d: shape.body, stroke: sceneColor(tone), fill: sceneColor(tone, 'paper'), strokeWidth: LINE_WIDTH, dash: null }];
51
+ if (shape.detail) {
52
+ paths.push({ d: shape.detail, stroke: sceneColor(tone), fill: null, strokeWidth: LINE_WIDTH, dash: null });
53
+ }
54
+ elements.push(elementAtOrigin(`node:${node.id}`, paths, textLinesOf(box, node.shape).map((line) => centeredText(line, sceneColor(line.muted ? 'muted' : 'ink')))));
55
+ }
56
+ return { rev: document.rev, bounds: layout.bounds, elements };
57
+ }
package/dist/shapes.d.ts CHANGED
@@ -17,4 +17,6 @@ export declare function textLinesOf(box: Pick<NodeBox, 'x' | 'y' | 'w' | 'h' | '
17
17
  export declare function edgeLabelLinesOf(label: EdgeLabelBox): TextLine[];
18
18
  export declare function edgePath(points: readonly Point[]): string;
19
19
  export declare function arrowHeadPath(points: readonly Point[]): string;
20
+ export declare const LINE_WIDTH = 2;
21
+ export declare function dashLengthsOf(style: DiagramEdgeStyle | undefined): number[] | null;
20
22
  export declare function dashOf(style: DiagramEdgeStyle | undefined): string | null;
package/dist/shapes.js CHANGED
@@ -84,12 +84,16 @@ export function arrowHeadPath(points) {
84
84
  const round = Math.round;
85
85
  return `M${tip.x} ${tip.y} L${round(baseX - uy * HEAD_WIDTH)} ${round(baseY + ux * HEAD_WIDTH)} L${round(baseX + uy * HEAD_WIDTH)} ${round(baseY - ux * HEAD_WIDTH)} Z`;
86
86
  }
87
- export function dashOf(style) {
87
+ export const LINE_WIDTH = 2;
88
+ export function dashLengthsOf(style) {
88
89
  if (style === 'dashed') {
89
- return '8 6';
90
+ return [8, 6];
90
91
  }
91
92
  if (style === 'dotted') {
92
- return '2 5';
93
+ return [2, 5];
93
94
  }
94
95
  return null;
95
96
  }
97
+ export function dashOf(style) {
98
+ return dashLengthsOf(style)?.join(' ') ?? null;
99
+ }
package/dist/svg.d.ts CHANGED
@@ -9,7 +9,6 @@ export interface DiagramSvgOptions {
9
9
  font?: string;
10
10
  layout?: DiagramLayout;
11
11
  }
12
- export declare const DEFAULT_DIAGRAM_MARGIN = 32;
13
12
  export declare const DEFAULT_NODE_TONE: DrawingColor;
14
13
  export declare const DEFAULT_GROUP_TONE: DrawingColor;
15
14
  export declare const DEFAULT_EDGE_TONE: DrawingColor;
package/dist/svg.js CHANGED
@@ -1,8 +1,8 @@
1
1
  import { DEFAULT_FONT_STACKS, DEFAULT_PALETTE, DEFAULT_PAPER, escapeXml } from '@adecore/drawing';
2
- import { GROUP_LABEL_BAND, layoutOf } from './layout.js';
3
- import { arrowHeadPath, dashOf, edgeLabelLinesOf, edgePath, shapePaths, textLinesOf } from './shapes.js';
2
+ import { GROUP_DASH, GROUP_LINE_WIDTH, groupLabelBaselineY, layoutOf } from './layout.js';
3
+ import { LINE_WIDTH, arrowHeadPath, dashOf, edgeLabelLinesOf, edgePath, shapePaths, textLinesOf } from './shapes.js';
4
4
  import { SUB_SIZE } from './text.js';
5
- export const DEFAULT_DIAGRAM_MARGIN = 32;
5
+ const DEFAULT_DIAGRAM_MARGIN = 32;
6
6
  /* What a node, a group and an edge wear when the file names no tone. */
7
7
  export const DEFAULT_NODE_TONE = 'ink';
8
8
  export const DEFAULT_GROUP_TONE = 'muted';
@@ -27,13 +27,13 @@ export function toSvg(document, options = {}) {
27
27
  for (const box of layout.groups) {
28
28
  const group = groups.get(box.id);
29
29
  const tone = palette[group.tone ?? DEFAULT_GROUP_TONE];
30
- parts.push(`<rect x="${box.x}" y="${box.y}" width="${box.w}" height="${box.h}" rx="12" fill="none" stroke="${tone}" stroke-width="1" stroke-dasharray="6 4"/>`, `<text x="${box.labelBox.x}" y="${box.y + Math.round(GROUP_LABEL_BAND * 0.7)}" font-size="${SUB_SIZE}" font-weight="600" fill="${tone}">${escapeXml(group.label)}</text>`);
30
+ parts.push(`<rect x="${box.x}" y="${box.y}" width="${box.w}" height="${box.h}" rx="12" fill="none" stroke="${tone}" stroke-width="${GROUP_LINE_WIDTH}" stroke-dasharray="${GROUP_DASH.join(' ')}"/>`, `<text x="${box.labelBox.x}" y="${groupLabelBaselineY(box)}" font-size="${SUB_SIZE}" font-weight="600" fill="${tone}">${escapeXml(group.label)}</text>`);
31
31
  }
32
32
  for (const route of layout.edges) {
33
33
  const edge = document.edges[route.index];
34
34
  const tone = palette[edge.tone ?? DEFAULT_EDGE_TONE];
35
35
  const dash = dashOf(edge.style);
36
- parts.push(`<path d="${edgePath(route.points)}" fill="none" stroke="${tone}" stroke-width="2" stroke-linejoin="round"${dash ? ` stroke-dasharray="${dash}"` : ''}/>`, `<path d="${arrowHeadPath(route.points)}" fill="${tone}"/>`);
36
+ parts.push(`<path d="${edgePath(route.points)}" fill="none" stroke="${tone}" stroke-width="${LINE_WIDTH}" stroke-linejoin="round"${dash ? ` stroke-dasharray="${dash}"` : ''}/>`, `<path d="${arrowHeadPath(route.points)}" fill="${tone}"/>`);
37
37
  if (route.label) {
38
38
  const halo = options.background ? ` stroke="${options.background}" stroke-width="4" paint-order="stroke"` : '';
39
39
  for (const line of edgeLabelLinesOf(route.label)) {
@@ -45,9 +45,9 @@ export function toSvg(document, options = {}) {
45
45
  const node = nodes.get(box.id);
46
46
  const tone = node.tone ?? DEFAULT_NODE_TONE;
47
47
  const paths = shapePaths(node.shape, box);
48
- parts.push(`<path d="${paths.body}" fill="${paper[tone]}" stroke="${palette[tone]}" stroke-width="2"/>`);
48
+ parts.push(`<path d="${paths.body}" fill="${paper[tone]}" stroke="${palette[tone]}" stroke-width="${LINE_WIDTH}"/>`);
49
49
  if (paths.detail) {
50
- parts.push(`<path d="${paths.detail}" fill="none" stroke="${palette[tone]}" stroke-width="2"/>`);
50
+ parts.push(`<path d="${paths.detail}" fill="none" stroke="${palette[tone]}" stroke-width="${LINE_WIDTH}"/>`);
51
51
  }
52
52
  for (const line of textLinesOf(box, node.shape)) {
53
53
  parts.push(`<text x="${line.x}" y="${line.y}" font-size="${line.size}"${line.bold ? ' font-weight="600"' : ''} text-anchor="middle" fill="${line.muted ? palette.muted : palette.ink}">${escapeXml(line.text)}</text>`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adecore/diagram",
3
- "version": "0.62.1",
3
+ "version": "0.63.0",
4
4
  "description": "Directed graph schemas, layered layout, SVG export and reading order.",
5
5
  "license": "FSL-1.1-MIT",
6
6
  "author": "Bas Milius",
@@ -45,7 +45,8 @@
45
45
  "typescript": "^7.0.2"
46
46
  },
47
47
  "dependencies": {
48
- "@adecore/drawing": "0.62.1",
48
+ "@adecore/drawing": "0.63.0",
49
+ "@adecore/utils": "0.63.0",
49
50
  "zod": "^4.6.7"
50
51
  }
51
52
  }
package/src/graph.ts CHANGED
@@ -21,7 +21,6 @@ export interface Unit {
21
21
  down: number[];
22
22
  }
23
23
 
24
- /* One stretch of an edge, from a unit in one layer to a unit in the next. */
25
24
  export interface Hop {
26
25
  edge: number;
27
26
  from: number;
@@ -30,7 +29,6 @@ export interface Hop {
30
29
 
31
30
  /* A group seen across the flow: one block per layer it spans, at the same place in every one of them. */
32
31
  export interface Band {
33
- /* The index of the group in the file. */
34
32
  group: number;
35
33
  first: number;
36
34
  last: number;
@@ -50,7 +48,6 @@ export interface Layering {
50
48
  units: Unit[];
51
49
  hops: Hop[];
52
50
  bands: Band[];
53
- /* The points of every long edge, first to last. */
54
51
  lines: number[][];
55
52
  /* Per layer the top-level entries in order: a unit, or `bandCode(band)` for the block of a group. */
56
53
  items: number[][];
package/src/index.ts CHANGED
@@ -11,5 +11,6 @@ export {
11
11
  } from './layout.ts';
12
12
  export { LABEL_SIZE, SUB_SIZE } from './text.ts';
13
13
  export { readingOrder } from './reading-order.ts';
14
+ export { diagramScene } from './scene.ts';
14
15
  export { type ShapePaths, shapePaths, type TextLine, textLinesOf, edgeLabelLinesOf, edgePath, arrowHeadPath, dashOf } from './shapes.ts';
15
16
  export { type DiagramSvgOptions, DEFAULT_NODE_TONE, DEFAULT_GROUP_TONE, DEFAULT_EDGE_TONE, toSvg } from './svg.ts';
package/src/layout.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { centerOf, intersects, roundPoint, type Point, type Rect } from '@adecore/drawing';
1
+ import { centerOf, intersects, roundPoint, type Point, type Rect } from '@adecore/utils/geometry';
2
2
  import { placeAcross } from './across.ts';
3
3
  import { DUMMY_MARGIN, NODE_MARGIN, bandCode, type Band, type Hop, type Layering, type Unit } from './graph.ts';
4
4
  import { orderLayers } from './order.ts';
@@ -6,7 +6,7 @@ import type { DiagramDocument, DiagramEdge, DiagramGroup, DiagramNode, DiagramSh
6
6
  import { routeChannel, type ChannelHop, type ChannelLabel } from './route.ts';
7
7
  import { LABEL_LINE, LABEL_SIZE, SUB_LINE, SUB_SIZE, estimateTextWidth, widestLine, wrapText } from './text.ts';
8
8
 
9
- export type { Point, Rect } from '@adecore/drawing';
9
+ export type { Point, Rect } from '@adecore/utils/geometry';
10
10
 
11
11
  export interface NodeBox extends Rect {
12
12
  id: string;
@@ -21,7 +21,6 @@ export interface NodeBox extends Rect {
21
21
 
22
22
  export interface GroupBox extends Rect {
23
23
  id: string;
24
- /* Where the group's label is drawn, estimated like every other text. */
25
24
  labelBox: Rect;
26
25
  }
27
26
 
@@ -36,7 +35,6 @@ export interface EdgeRoute {
36
35
  to: string;
37
36
  /* Whole-number corners, first to last, with the head at the last one. */
38
37
  points: Point[];
39
- /* Where the label goes, or null for an edge without one. */
40
38
  label: EdgeLabelBox | null;
41
39
  }
42
40
 
@@ -59,13 +57,14 @@ const TEXT_MAX_WIDTH = NODE_MAX_WIDTH - NODE_PADDING_X * 2;
59
57
  /* A diamond holds its text in the middle of its box, so its lines are shorter and the box is twice their size. */
60
58
  const DIAMOND_TEXT_MAX_WIDTH = 160;
61
59
  const DIAMOND_MIN = { w: 168, h: 72 };
62
- /* The extra height of a cylinder, for the lid its text stays under. */
63
60
  export const CYLINDER_LID = 10;
64
61
  const EDGE_LABEL_MAX_WIDTH = 160;
65
62
  export const EDGE_LABEL_PADDING = { x: 4, y: 2 };
66
63
  const GROUP_PADDING = 20;
67
- /* The band on top of a group where its label sits. */
68
64
  export const GROUP_LABEL_BAND = 24;
65
+ /* A group's outline is thin and dashed, so it reads as a region and not as a node. */
66
+ export const GROUP_LINE_WIDTH = 1;
67
+ export const GROUP_DASH = [6, 4];
69
68
  const GROUP_LABEL_INSET = 12;
70
69
  /* Room between a layer's widest box and the first thing in the channel after it. */
71
70
  const EDGE_CLEAR = 20;
@@ -81,14 +80,18 @@ function even(value: number): number {
81
80
  return Math.ceil(value / 2) * 2;
82
81
  }
83
82
 
84
- export interface NodeSize {
83
+ /* The baseline of a group's label, 0.7 of the way down the band that holds it. */
84
+ export function groupLabelBaselineY(box: Pick<GroupBox, 'y'>): number {
85
+ return box.y + Math.round(GROUP_LABEL_BAND * 0.7);
86
+ }
87
+
88
+ interface NodeSize {
85
89
  w: number;
86
90
  h: number;
87
91
  label: string[];
88
92
  sub: string[];
89
93
  }
90
94
 
91
- /* The size of a node's box, from what it says wrapped to the widest a box gets, and the shape it wears. */
92
95
  export function sizeOfNode(node: Pick<DiagramNode, 'label' | 'sub' | 'shape'>): NodeSize {
93
96
  const diamond = node.shape === 'diamond';
94
97
  const maxWidth = diamond ? DIAMOND_TEXT_MAX_WIDTH : TEXT_MAX_WIDTH;
@@ -113,7 +116,6 @@ export function sizeOfNode(node: Pick<DiagramNode, 'label' | 'sub' | 'shape'>):
113
116
  return { w: even(Math.max(NODE_MIN_WIDTH, textWidth + paddingX * 2)), h: even(h), label, sub };
114
117
  }
115
118
 
116
- /* The box of an edge's label before it is placed: wrapped narrower than a node, on a padding of its own. */
117
119
  function sizeOfEdgeLabel(text: string): { w: number; h: number; lines: string[] } {
118
120
  const lines = wrapText(text, SUB_SIZE, false, EDGE_LABEL_MAX_WIDTH);
119
121
 
@@ -248,7 +250,6 @@ function unionOf(rects: readonly Rect[]): Rect {
248
250
  return { x: left, y: top, w: right - left, h: bottom - top };
249
251
  }
250
252
 
251
- /* Drops repeated corners and corners in the middle of a straight run. */
252
253
  function simplify(points: readonly Point[]): Point[] {
253
254
  const unique = points.filter((point, index) => index === 0 || point.x !== points[index - 1]!.x || point.y !== points[index - 1]!.y);
254
255
 
@@ -399,7 +400,6 @@ interface Ports {
399
400
 
400
401
  type LabelSize = ReturnType<typeof sizeOfEdgeLabel>;
401
402
 
402
- /* Every node that is not pinned is a unit, sized in the right-running frame. */
403
403
  function unitsOf(nodes: readonly DiagramNode[], layers: ReadonlyMap<string, number>, sizes: readonly NodeSize[], down: boolean) {
404
404
  const units: Unit[] = [];
405
405
  const unitOf = new Map<string, number>();
@@ -469,7 +469,6 @@ function bandsOf(groups: readonly DiagramGroup[], units: Unit[], unitOf: Readonl
469
469
  return { bands, bandOfGroup };
470
470
  }
471
471
 
472
- /* Splits every edge between two units into hops of one layer each, adding a point to `units` for every layer it skips. */
473
472
  function edgesOf(edges: readonly DiagramEdge[], units: Unit[], unitOf: ReadonlyMap<string, number>, knownIds: ReadonlySet<string>): Edges {
474
473
  const hops: Hop[] = [];
475
474
  const chains: Chain[] = [];
@@ -539,7 +538,6 @@ function edgesOf(edges: readonly DiagramEdge[], units: Unit[], unitOf: ReadonlyM
539
538
  return { hops, chains, loops, loose, lines };
540
539
  }
541
540
 
542
- /* The top-level entries of every layer, with the members of each band filled in and its height measured. */
543
541
  function itemsOf(units: readonly Unit[], bands: readonly Band[], layerCount: number, down: boolean): number[][] {
544
542
  const items: number[][] = Array.from({ length: layerCount }, () => []);
545
543
 
@@ -656,11 +654,9 @@ interface Columns {
656
654
  unitX: number[];
657
655
  /* Per hop that bends, where its run across the flow sits in the channel after its layer. */
658
656
  trackX: Map<number, number>;
659
- /* Per edge, its label in the right-running frame. */
660
657
  frameLabels: Map<number, Rect>;
661
658
  }
662
659
 
663
- /* Places the layers along the flow, each followed by the channel its hops and labels are routed through. */
664
660
  function placeAlong(layering: Layering, edges: Edges, ports: Ports, labelSizes: readonly (LabelSize | null)[], layerCount: number, down: boolean): Columns {
665
661
  const { units, bands } = layering;
666
662
  const { hops, chains, loops } = edges;
package/src/route.ts CHANGED
@@ -1,5 +1,4 @@
1
- import { intersects } from '@adecore/drawing';
2
- import type { Point, Rect } from './layout.ts';
1
+ import { intersects, type Point, type Rect } from '@adecore/utils/geometry';
3
2
 
4
3
  const TRACK_GAP = 12;
5
4
  /* Space between the lane of labels and the first track. */
@@ -25,13 +24,12 @@ export interface ChannelLabel {
25
24
  edge: number;
26
25
  /* The hop it belongs to, whose own line never counts against it. */
27
26
  hop: number;
28
- /* The line it sits beside. */
29
27
  y: number;
30
28
  along: number;
31
29
  across: number;
32
30
  }
33
31
 
34
- export interface ChannelInput {
32
+ interface ChannelInput {
35
33
  /* Where the channel begins along the flow, right after the widest box of its layer. */
36
34
  start: number;
37
35
  /* Room kept free at the start and the end, for a group's border and an arrowhead. */
@@ -43,7 +41,7 @@ export interface ChannelInput {
43
41
  obstacles: { y: number; h: number }[];
44
42
  }
45
43
 
46
- export interface ChannelResult {
44
+ interface ChannelResult {
47
45
  width: number;
48
46
  trackX: Map<number, number>;
49
47
  labels: Map<number, Rect>;
@@ -93,7 +91,6 @@ function conflictsOf(left: readonly Point[], right: readonly Point[]): number {
93
91
  return total;
94
92
  }
95
93
 
96
- /* A hop drawn on a track in a channel of unit tracks, only to compare two orders of the tracks. */
97
94
  function sketchOf(hop: ChannelHop, track: number): Point[] {
98
95
  return [
99
96
  { x: 0, y: hop.ya },
package/src/scene.ts ADDED
@@ -0,0 +1,83 @@
1
+ import { sceneColor, type Scene, type SceneColor, type SceneElement, type ScenePath, type SceneText } from '@adecore/drawing';
2
+ import type { DrawingColor } from '@adecore/drawing/protocol';
3
+ import { GROUP_DASH, GROUP_LINE_WIDTH, groupLabelBaselineY, layoutOf } from './layout.ts';
4
+ import type { DiagramDocument } from './protocol.ts';
5
+ import { LINE_WIDTH, arrowHeadPath, dashLengthsOf, edgeLabelLinesOf, edgePath, shapePaths, textLinesOf, type TextLine } from './shapes.ts';
6
+ import { DEFAULT_EDGE_TONE, DEFAULT_GROUP_TONE, DEFAULT_NODE_TONE } from './svg.ts';
7
+ import { SUB_SIZE } from './text.ts';
8
+
9
+ function elementAtOrigin(id: string, paths: ScenePath[], text: SceneText[]): SceneElement {
10
+ return { id, x: 0, y: 0, angle: 0, centerX: 0, centerY: 0, paths, text };
11
+ }
12
+
13
+ function centeredText(line: Pick<TextLine, 'text' | 'x' | 'y' | 'size' | 'bold'>, tone: SceneColor): SceneText {
14
+ return { text: line.text, x: line.x, y: line.y, size: line.size, bold: line.bold, align: 'center', font: 'sans', color: tone };
15
+ }
16
+
17
+ /*
18
+ * The scene of a diagram, from the layout and paths `toSvg` draws with. Every element sits in world
19
+ * coordinates, and its id says what it is: `group:<id>`, `edge:<index>` or `node:<id>`.
20
+ */
21
+ export function diagramScene(document: Pick<DiagramDocument, 'rev' | 'meta' | 'nodes' | 'groups' | 'edges'>): Scene {
22
+ const layout = layoutOf(document);
23
+ const nodes = new Map(document.nodes.map((node) => [node.id, node]));
24
+ const groups = new Map(document.groups.map((group) => [group.id, group]));
25
+ const elements: SceneElement[] = [];
26
+
27
+ for (const box of layout.groups) {
28
+ const group = groups.get(box.id)!;
29
+ const tone = sceneColor(group.tone ?? DEFAULT_GROUP_TONE);
30
+
31
+ elements.push(
32
+ elementAtOrigin(
33
+ `group:${box.id}`,
34
+ [{ d: shapePaths('round', box).body, stroke: tone, fill: null, strokeWidth: GROUP_LINE_WIDTH, dash: [...GROUP_DASH] }],
35
+ [
36
+ {
37
+ text: group.label,
38
+ x: box.labelBox.x,
39
+ y: groupLabelBaselineY(box),
40
+ size: SUB_SIZE,
41
+ bold: true,
42
+ align: 'left',
43
+ font: 'sans',
44
+ color: tone
45
+ }
46
+ ]
47
+ )
48
+ );
49
+ }
50
+
51
+ for (const route of layout.edges) {
52
+ const edge = document.edges[route.index]!;
53
+ const tone = sceneColor(edge.tone ?? DEFAULT_EDGE_TONE);
54
+ const paths: ScenePath[] = [{ d: edgePath(route.points), stroke: tone, fill: null, strokeWidth: LINE_WIDTH, dash: dashLengthsOf(edge.style) }];
55
+
56
+ if (route.points.length > 0) {
57
+ paths.push({ d: arrowHeadPath(route.points), stroke: null, fill: tone, strokeWidth: 0, dash: null });
58
+ }
59
+
60
+ elements.push(elementAtOrigin(`edge:${route.index}`, paths, route.label ? edgeLabelLinesOf(route.label).map((line) => centeredText(line, tone)) : []));
61
+ }
62
+
63
+ for (const box of layout.nodes) {
64
+ const node = nodes.get(box.id)!;
65
+ const tone: DrawingColor = node.tone ?? DEFAULT_NODE_TONE;
66
+ const shape = shapePaths(node.shape, box);
67
+ const paths: ScenePath[] = [{ d: shape.body, stroke: sceneColor(tone), fill: sceneColor(tone, 'paper'), strokeWidth: LINE_WIDTH, dash: null }];
68
+
69
+ if (shape.detail) {
70
+ paths.push({ d: shape.detail, stroke: sceneColor(tone), fill: null, strokeWidth: LINE_WIDTH, dash: null });
71
+ }
72
+
73
+ elements.push(
74
+ elementAtOrigin(
75
+ `node:${node.id}`,
76
+ paths,
77
+ textLinesOf(box, node.shape).map((line) => centeredText(line, sceneColor(line.muted ? 'muted' : 'ink')))
78
+ )
79
+ );
80
+ }
81
+
82
+ return { rev: document.rev, bounds: layout.bounds, elements };
83
+ }
package/src/shapes.ts CHANGED
@@ -114,14 +114,20 @@ export function arrowHeadPath(points: readonly Point[]): string {
114
114
  return `M${tip.x} ${tip.y} L${round(baseX - uy * HEAD_WIDTH)} ${round(baseY + ux * HEAD_WIDTH)} L${round(baseX + uy * HEAD_WIDTH)} ${round(baseY - ux * HEAD_WIDTH)} Z`;
115
115
  }
116
116
 
117
- export function dashOf(style: DiagramEdgeStyle | undefined): string | null {
117
+ export const LINE_WIDTH = 2;
118
+
119
+ export function dashLengthsOf(style: DiagramEdgeStyle | undefined): number[] | null {
118
120
  if (style === 'dashed') {
119
- return '8 6';
121
+ return [8, 6];
120
122
  }
121
123
 
122
124
  if (style === 'dotted') {
123
- return '2 5';
125
+ return [2, 5];
124
126
  }
125
127
 
126
128
  return null;
127
129
  }
130
+
131
+ export function dashOf(style: DiagramEdgeStyle | undefined): string | null {
132
+ return dashLengthsOf(style)?.join(' ') ?? null;
133
+ }
package/src/svg.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  import { DEFAULT_FONT_STACKS, DEFAULT_PALETTE, DEFAULT_PAPER, escapeXml } from '@adecore/drawing';
2
2
  import type { DrawingColor } from '@adecore/drawing/protocol';
3
- import { GROUP_LABEL_BAND, layoutOf, type DiagramLayout } from './layout.ts';
3
+ import { GROUP_DASH, GROUP_LINE_WIDTH, groupLabelBaselineY, layoutOf, type DiagramLayout } from './layout.ts';
4
4
  import type { DiagramDocument } from './protocol.ts';
5
- import { arrowHeadPath, dashOf, edgeLabelLinesOf, edgePath, shapePaths, textLinesOf } from './shapes.ts';
5
+ import { LINE_WIDTH, arrowHeadPath, dashOf, edgeLabelLinesOf, edgePath, shapePaths, textLinesOf } from './shapes.ts';
6
6
  import { SUB_SIZE } from './text.ts';
7
7
 
8
8
  export interface DiagramSvgOptions {
@@ -19,7 +19,7 @@ export interface DiagramSvgOptions {
19
19
  layout?: DiagramLayout;
20
20
  }
21
21
 
22
- export const DEFAULT_DIAGRAM_MARGIN = 32;
22
+ const DEFAULT_DIAGRAM_MARGIN = 32;
23
23
 
24
24
  /* What a node, a group and an edge wear when the file names no tone. */
25
25
  export const DEFAULT_NODE_TONE: DrawingColor = 'ink';
@@ -51,8 +51,8 @@ export function toSvg(document: Pick<DiagramDocument, 'meta' | 'nodes' | 'groups
51
51
  const tone = palette[group.tone ?? DEFAULT_GROUP_TONE];
52
52
 
53
53
  parts.push(
54
- `<rect x="${box.x}" y="${box.y}" width="${box.w}" height="${box.h}" rx="12" fill="none" stroke="${tone}" stroke-width="1" stroke-dasharray="6 4"/>`,
55
- `<text x="${box.labelBox.x}" y="${box.y + Math.round(GROUP_LABEL_BAND * 0.7)}" font-size="${SUB_SIZE}" font-weight="600" fill="${tone}">${escapeXml(group.label)}</text>`
54
+ `<rect x="${box.x}" y="${box.y}" width="${box.w}" height="${box.h}" rx="12" fill="none" stroke="${tone}" stroke-width="${GROUP_LINE_WIDTH}" stroke-dasharray="${GROUP_DASH.join(' ')}"/>`,
55
+ `<text x="${box.labelBox.x}" y="${groupLabelBaselineY(box)}" font-size="${SUB_SIZE}" font-weight="600" fill="${tone}">${escapeXml(group.label)}</text>`
56
56
  );
57
57
  }
58
58
 
@@ -62,7 +62,7 @@ export function toSvg(document: Pick<DiagramDocument, 'meta' | 'nodes' | 'groups
62
62
  const dash = dashOf(edge.style);
63
63
 
64
64
  parts.push(
65
- `<path d="${edgePath(route.points)}" fill="none" stroke="${tone}" stroke-width="2" stroke-linejoin="round"${dash ? ` stroke-dasharray="${dash}"` : ''}/>`,
65
+ `<path d="${edgePath(route.points)}" fill="none" stroke="${tone}" stroke-width="${LINE_WIDTH}" stroke-linejoin="round"${dash ? ` stroke-dasharray="${dash}"` : ''}/>`,
66
66
  `<path d="${arrowHeadPath(route.points)}" fill="${tone}"/>`
67
67
  );
68
68
 
@@ -81,10 +81,10 @@ export function toSvg(document: Pick<DiagramDocument, 'meta' | 'nodes' | 'groups
81
81
  const node = nodes.get(box.id)!;
82
82
  const tone = node.tone ?? DEFAULT_NODE_TONE;
83
83
  const paths = shapePaths(node.shape, box);
84
- parts.push(`<path d="${paths.body}" fill="${paper[tone]}" stroke="${palette[tone]}" stroke-width="2"/>`);
84
+ parts.push(`<path d="${paths.body}" fill="${paper[tone]}" stroke="${palette[tone]}" stroke-width="${LINE_WIDTH}"/>`);
85
85
 
86
86
  if (paths.detail) {
87
- parts.push(`<path d="${paths.detail}" fill="none" stroke="${palette[tone]}" stroke-width="2"/>`);
87
+ parts.push(`<path d="${paths.detail}" fill="none" stroke="${palette[tone]}" stroke-width="${LINE_WIDTH}"/>`);
88
88
  }
89
89
 
90
90
  for (const line of textLinesOf(box, node.shape)) {