@meltemi/diagrams-layout 0.0.0-stage → 1.9.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/LICENSE +21 -0
- package/README.md +52 -2
- package/dist/audit.d.ts +58 -0
- package/dist/audit.d.ts.map +1 -0
- package/dist/audit.js +239 -0
- package/dist/audit.js.map +1 -0
- package/dist/checks.d.ts +38 -0
- package/dist/checks.d.ts.map +1 -0
- package/dist/checks.js +331 -0
- package/dist/checks.js.map +1 -0
- package/dist/constraints.d.ts +20 -0
- package/dist/constraints.d.ts.map +1 -0
- package/dist/constraints.js +149 -0
- package/dist/constraints.js.map +1 -0
- package/dist/engine.d.ts +96 -0
- package/dist/engine.d.ts.map +1 -0
- package/dist/engine.js +3 -0
- package/dist/engine.js.map +1 -0
- package/dist/frame.d.ts +17 -0
- package/dist/frame.d.ts.map +1 -0
- package/dist/frame.js +51 -0
- package/dist/frame.js.map +1 -0
- package/dist/free.d.ts +75 -0
- package/dist/free.d.ts.map +1 -0
- package/dist/free.js +122 -0
- package/dist/free.js.map +1 -0
- package/dist/geometry.d.ts +58 -0
- package/dist/geometry.d.ts.map +1 -0
- package/dist/geometry.js +223 -0
- package/dist/geometry.js.map +1 -0
- package/dist/icons.d.ts +16 -0
- package/dist/icons.d.ts.map +1 -0
- package/dist/icons.js +94 -0
- package/dist/icons.js.map +1 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +19 -0
- package/dist/index.js.map +1 -0
- package/dist/layout.d.ts +206 -0
- package/dist/layout.d.ts.map +1 -0
- package/dist/layout.js +3527 -0
- package/dist/layout.js.map +1 -0
- package/dist/mark.d.ts +24 -0
- package/dist/mark.d.ts.map +1 -0
- package/dist/mark.js +60 -0
- package/dist/mark.js.map +1 -0
- package/dist/markers.d.ts +62 -0
- package/dist/markers.d.ts.map +1 -0
- package/dist/markers.js +255 -0
- package/dist/markers.js.map +1 -0
- package/dist/measure.d.ts +38 -0
- package/dist/measure.d.ts.map +1 -0
- package/dist/measure.js +44 -0
- package/dist/measure.js.map +1 -0
- package/dist/messages/en.d.ts +160 -0
- package/dist/messages/en.d.ts.map +1 -0
- package/dist/messages/en.js +163 -0
- package/dist/messages/en.js.map +1 -0
- package/dist/messages/es.d.ts +4 -0
- package/dist/messages/es.d.ts.map +1 -0
- package/dist/messages/es.js +104 -0
- package/dist/messages/es.js.map +1 -0
- package/dist/messages/index.d.ts +86 -0
- package/dist/messages/index.d.ts.map +1 -0
- package/dist/messages/index.js +17 -0
- package/dist/messages/index.js.map +1 -0
- package/dist/routing.d.ts +94 -0
- package/dist/routing.d.ts.map +1 -0
- package/dist/routing.js +549 -0
- package/dist/routing.js.map +1 -0
- package/dist/scene.d.ts +220 -0
- package/dist/scene.d.ts.map +1 -0
- package/dist/scene.js +5 -0
- package/dist/scene.js.map +1 -0
- package/dist/sequence.d.ts +17 -0
- package/dist/sequence.d.ts.map +1 -0
- package/dist/sequence.js +875 -0
- package/dist/sequence.js.map +1 -0
- package/dist/shapes.d.ts +140 -0
- package/dist/shapes.d.ts.map +1 -0
- package/dist/shapes.js +1030 -0
- package/dist/shapes.js.map +1 -0
- package/dist/styles.d.ts +58 -0
- package/dist/styles.d.ts.map +1 -0
- package/dist/styles.js +106 -0
- package/dist/styles.js.map +1 -0
- package/dist/text.d.ts +8 -0
- package/dist/text.d.ts.map +1 -0
- package/dist/text.js +28 -0
- package/dist/text.js.map +1 -0
- package/dist/theme.d.ts +100 -0
- package/dist/theme.d.ts.map +1 -0
- package/dist/theme.js +246 -0
- package/dist/theme.js.map +1 -0
- package/dist/viewport.d.ts +98 -0
- package/dist/viewport.d.ts.map +1 -0
- package/dist/viewport.js +177 -0
- package/dist/viewport.js.map +1 -0
- package/package.json +49 -3
package/dist/layout.js
ADDED
|
@@ -0,0 +1,3527 @@
|
|
|
1
|
+
import { DEFAULT_LOCALE } from "@meltemi/diagrams-i18n";
|
|
2
|
+
import { defaultImageSize, } from "@meltemi/diagrams-assets";
|
|
3
|
+
import { ENTITY_TYPE_NAMES, ICON_SIZE_RANGE, MAIN_LAYER, takesRotation, } from "@meltemi/diagrams-model";
|
|
4
|
+
import { auditLabels } from "./audit.js";
|
|
5
|
+
import { drawingWords, messages } from "./messages/index.js";
|
|
6
|
+
import { checkComposition } from "./checks.js";
|
|
7
|
+
import { solveConstraints } from "./constraints.js";
|
|
8
|
+
import { ENGINE_SPACING, } from "./engine.js";
|
|
9
|
+
import { LANE_SPACING, anchorPoints, avoidingRoute, crossedBoxes, bezierThrough, detourRoute, portPoint, shapeRoute, sideNormal, sideOfPoint, snapEnd, } from "./routing.js";
|
|
10
|
+
import { boxCenter, boxFromCenter, cylinderPaths, ellipsePath, expandBox, pointAlong, unionBoxes, } from "./geometry.js";
|
|
11
|
+
import { CORNER_RADIUS, endMarker, markerItems, markerPoints, markerReach, relationEnds, roundCorners, shiftMarker, } from "./markers.js";
|
|
12
|
+
import { freeFrames, groupHeaderOf, isUntitledGroup, paintOrder, parentOf } from "./free.js";
|
|
13
|
+
import { layoutSequence } from "./sequence.js";
|
|
14
|
+
import { IconPainter } from "./icons.js";
|
|
15
|
+
import { brandFrame } from "./frame.js";
|
|
16
|
+
import { markGradient } from "./mark.js";
|
|
17
|
+
import { strokeDash, } from "./scene.js";
|
|
18
|
+
import { MIN_TEXT_MARGIN, TextMetrics, estimateMeasurer } from "./measure.js";
|
|
19
|
+
import { CORNERED_SHAPES, SHAPE_SIZES, agentHeight, framedLines, isOutlined, onOutline, onPolygon, shapeOutline, shapeTextCenter, shapeTextSpan, shapeTextSpanner, turnPoint, turnedOutline, } from "./shapes.js";
|
|
20
|
+
import { StyleResolver } from "./styles.js";
|
|
21
|
+
import { splitLines } from "./text.js";
|
|
22
|
+
import { DEFAULT_THEME_NAME, THEMES, resolveTheme, } from "./theme.js";
|
|
23
|
+
/** Widest content canvas of `preset document` before `layout.too-large`. */
|
|
24
|
+
export const DOCUMENT_MAX_WIDTH = 1200;
|
|
25
|
+
/** The 16:9 canvas of `preset slide`. */
|
|
26
|
+
export const SLIDE_SIZE = { width: 1920, height: 1080 };
|
|
27
|
+
/** Size of one pattern tile in pixels. */
|
|
28
|
+
export const PATTERN_SIZE = 8;
|
|
29
|
+
/** Default box sizes of the `technical.*` type scale (13 px labels); others scale with `labelSize`. */
|
|
30
|
+
export const DEFAULT_SIZES = {
|
|
31
|
+
node: { width: 160, height: 56 },
|
|
32
|
+
decision: { width: 160, height: 80 },
|
|
33
|
+
data: { width: 160, height: 64 },
|
|
34
|
+
group: null,
|
|
35
|
+
note: null,
|
|
36
|
+
tag: null,
|
|
37
|
+
box: { width: 160, height: 56 },
|
|
38
|
+
text: null,
|
|
39
|
+
icon: { width: 48, height: 48 },
|
|
40
|
+
image: null,
|
|
41
|
+
// Sequence heads are measured by the sequence layout.
|
|
42
|
+
participant: null,
|
|
43
|
+
actor: null,
|
|
44
|
+
boundary: null,
|
|
45
|
+
control: null,
|
|
46
|
+
entity: null,
|
|
47
|
+
database: null,
|
|
48
|
+
collections: null,
|
|
49
|
+
queue: null,
|
|
50
|
+
// C4 entities: boxes with a type line, a person with a head, a datastore cylinder.
|
|
51
|
+
person: { width: 160, height: 104 },
|
|
52
|
+
system: { width: 180, height: 80 },
|
|
53
|
+
container: { width: 170, height: 76 },
|
|
54
|
+
datastore: { width: 170, height: 84 },
|
|
55
|
+
component: { width: 160, height: 72 },
|
|
56
|
+
};
|
|
57
|
+
/** The radius of a person's head, from the label size. */
|
|
58
|
+
function personHead(label) {
|
|
59
|
+
return Math.round(label * 1.2);
|
|
60
|
+
}
|
|
61
|
+
/** The size of the technology line of a relation label. */
|
|
62
|
+
function technologySize(relationLabel) {
|
|
63
|
+
return Math.max(8, Math.round(relationLabel * 0.85));
|
|
64
|
+
}
|
|
65
|
+
/** The words of each C4 entity kind, plain and external. */
|
|
66
|
+
const C4_WORDS = {
|
|
67
|
+
person: ["person", "externalPerson"],
|
|
68
|
+
system: ["system", "externalSystem"],
|
|
69
|
+
container: ["container", "externalContainer"],
|
|
70
|
+
datastore: ["container", "externalContainer"],
|
|
71
|
+
component: ["component", "externalComponent"],
|
|
72
|
+
};
|
|
73
|
+
/**
|
|
74
|
+
* The type line of a C4 entity in the content language: `[Container: React]`, `[Contenedor: React]`.
|
|
75
|
+
* The content language is explicit (English by default), never the interface's.
|
|
76
|
+
*/
|
|
77
|
+
export function c4TypeLine(element, locale = DEFAULT_LOCALE) {
|
|
78
|
+
const name = c4TypeName(element, locale);
|
|
79
|
+
if (name === null)
|
|
80
|
+
return null;
|
|
81
|
+
return element.technology ? `[${name}: ${element.technology}]` : `[${name}]`;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* The type word of a C4 entity in the content language, as its type line draws it without the
|
|
85
|
+
* brackets and the technology: `Container`, `External Software System`, `Contenedor`. Null for
|
|
86
|
+
* the boundary group and for elements that draw no entity.
|
|
87
|
+
*/
|
|
88
|
+
export function c4TypeName(element, locale = DEFAULT_LOCALE) {
|
|
89
|
+
if (!element.entity || element.kind === "group")
|
|
90
|
+
return null;
|
|
91
|
+
const keys = C4_WORDS[element.kind];
|
|
92
|
+
return keys
|
|
93
|
+
? drawingWords(locale).c4[keys[element.external ? 1 : 0]]
|
|
94
|
+
: (ENTITY_TYPE_NAMES[element.kind] ?? element.kind);
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* A C4 view as the flow pipeline draws it: each entity's description starts with its type line,
|
|
98
|
+
* and the boundary's title names the scope's type.
|
|
99
|
+
*/
|
|
100
|
+
function c4Drawing(diagram, locale) {
|
|
101
|
+
const words = drawingWords(locale).c4;
|
|
102
|
+
const elements = diagram.elements.map((element) => {
|
|
103
|
+
if (element.kind === "group") {
|
|
104
|
+
// The boundary of a container view is a system; of a component view, a container.
|
|
105
|
+
const type = diagram.c4?.level === "component" ? words.container : words.system;
|
|
106
|
+
return { ...element, label: `${element.label} [${type}]`, description: null };
|
|
107
|
+
}
|
|
108
|
+
const type = c4TypeLine(element, locale);
|
|
109
|
+
return {
|
|
110
|
+
...element,
|
|
111
|
+
description: [type, element.description].filter((l) => l).join("\n") || null,
|
|
112
|
+
};
|
|
113
|
+
});
|
|
114
|
+
return { ...diagram, elements, elementsById: new Map(elements.map((e) => [e.id, e])) };
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Shapes whose text keeps the whole width of the box, as before shapes had text areas: their
|
|
118
|
+
* default sizes and overflow checks are the boxes'.
|
|
119
|
+
*/
|
|
120
|
+
const FULL_WIDTH_TEXT = new Set([
|
|
121
|
+
"rounded",
|
|
122
|
+
"rectangle",
|
|
123
|
+
"square",
|
|
124
|
+
"cylinder",
|
|
125
|
+
"person",
|
|
126
|
+
]);
|
|
127
|
+
/**
|
|
128
|
+
* How far a shape may reach into the glyph rows of a text before they count as clipped: the label
|
|
129
|
+
* audit's tolerance, so that a wave crest grazing the descenders is not a cut.
|
|
130
|
+
*/
|
|
131
|
+
const ROW_TOLERANCE = 0.75;
|
|
132
|
+
/** Widths that shapes grow to are whole pixels, rounded down when they are within this of one. */
|
|
133
|
+
const ROUNDING = 0.01;
|
|
134
|
+
/** The heights a shaped box tries, as factors of its own, when its text does not fit. */
|
|
135
|
+
const TALLER = [1.25, 1.5, 2, 2.5, 3, 4];
|
|
136
|
+
/** A corner radius drawn on a box: never negative, at most half its shorter side. */
|
|
137
|
+
function cornerRadius(radius, box) {
|
|
138
|
+
return Math.max(0, Math.min(radius, box.width / 2, box.height / 2));
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Derives line heights and pill sizes from the type tokens. The ratios are those of the
|
|
142
|
+
* `technical.*` themes: 11 px descriptions on 16 px lines, 18 px tags at 10.5 px, a 44 px frame
|
|
143
|
+
* at 12 px.
|
|
144
|
+
*/
|
|
145
|
+
export function typeScale(theme) {
|
|
146
|
+
const tagHeight = Math.round((theme.tagSize * 18) / 10.5);
|
|
147
|
+
return {
|
|
148
|
+
label: theme.labelSize,
|
|
149
|
+
labelWeight: theme.labelWeight,
|
|
150
|
+
description: theme.descriptionSize,
|
|
151
|
+
descriptionLine: Math.round((theme.descriptionSize * 16) / 11),
|
|
152
|
+
groupTitle: theme.groupTitleSize,
|
|
153
|
+
note: theme.noteSize,
|
|
154
|
+
noteLine: Math.round((theme.noteSize * 18) / 11),
|
|
155
|
+
tag: theme.tagSize,
|
|
156
|
+
tagHeight,
|
|
157
|
+
tagPadding: tagHeight,
|
|
158
|
+
relationLabel: theme.relationLabelSize,
|
|
159
|
+
relationLabelHeight: Math.round((theme.relationLabelSize * 18) / 11),
|
|
160
|
+
relationLabelPadding: Math.round((theme.relationLabelSize * 12) / 11),
|
|
161
|
+
frameBrand: theme.frameSize,
|
|
162
|
+
frameTitle: Math.round(((theme.frameSize * 11) / 12) * 2) / 2,
|
|
163
|
+
frameHeight: Math.round((theme.frameSize * 44) / 12),
|
|
164
|
+
frameMark: Math.round(theme.frameSize * 2),
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
/** Room kept between a group's title and the connectors that pass under it. */
|
|
168
|
+
const TITLE_GAP = 3;
|
|
169
|
+
/**
|
|
170
|
+
* The visible pieces of a polyline that passes under `holes`: the parts inside a hole are left
|
|
171
|
+
* out. A line no hole touches is one piece.
|
|
172
|
+
*/
|
|
173
|
+
export function splitAround(line, holes) {
|
|
174
|
+
if (holes.length === 0 || line.length < 2)
|
|
175
|
+
return [[...line]];
|
|
176
|
+
const pieces = [];
|
|
177
|
+
let piece = [line[0]];
|
|
178
|
+
for (let i = 1; i < line.length; i++) {
|
|
179
|
+
const a = line[i - 1];
|
|
180
|
+
const b = line[i];
|
|
181
|
+
const hidden = [];
|
|
182
|
+
for (const hole of holes) {
|
|
183
|
+
const span = clipSegment(a, b, hole);
|
|
184
|
+
if (span)
|
|
185
|
+
hidden.push(span);
|
|
186
|
+
}
|
|
187
|
+
hidden.sort((x, y) => x[0] - y[0]);
|
|
188
|
+
const at = (t) => ({ x: a.x + (b.x - a.x) * t, y: a.y + (b.y - a.y) * t });
|
|
189
|
+
let t = 0;
|
|
190
|
+
for (const [from, to] of hidden) {
|
|
191
|
+
if (to <= t)
|
|
192
|
+
continue;
|
|
193
|
+
if (from > t) {
|
|
194
|
+
if (piece.length === 0)
|
|
195
|
+
piece.push(at(t));
|
|
196
|
+
piece.push(at(from));
|
|
197
|
+
}
|
|
198
|
+
if (piece.length >= 2)
|
|
199
|
+
pieces.push(piece);
|
|
200
|
+
piece = [];
|
|
201
|
+
t = Math.max(t, to);
|
|
202
|
+
}
|
|
203
|
+
if (t < 1) {
|
|
204
|
+
if (piece.length === 0)
|
|
205
|
+
piece.push(at(t));
|
|
206
|
+
piece.push(b);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
if (piece.length >= 2)
|
|
210
|
+
pieces.push(piece);
|
|
211
|
+
return pieces.length > 0 ? pieces : [[...line]];
|
|
212
|
+
}
|
|
213
|
+
/** The part of a segment inside a box, as fractions of the segment, or undefined. */
|
|
214
|
+
function clipSegment(a, b, box) {
|
|
215
|
+
let t0 = 0;
|
|
216
|
+
let t1 = 1;
|
|
217
|
+
const dx = b.x - a.x;
|
|
218
|
+
const dy = b.y - a.y;
|
|
219
|
+
const clips = [
|
|
220
|
+
[-dx, a.x - box.x],
|
|
221
|
+
[dx, box.x + box.width - a.x],
|
|
222
|
+
[-dy, a.y - box.y],
|
|
223
|
+
[dy, box.y + box.height - a.y],
|
|
224
|
+
];
|
|
225
|
+
for (const [p, q] of clips) {
|
|
226
|
+
if (p === 0) {
|
|
227
|
+
if (q < 0)
|
|
228
|
+
return undefined;
|
|
229
|
+
continue;
|
|
230
|
+
}
|
|
231
|
+
const r = q / p;
|
|
232
|
+
if (p < 0)
|
|
233
|
+
t0 = Math.max(t0, r);
|
|
234
|
+
else
|
|
235
|
+
t1 = Math.min(t1, r);
|
|
236
|
+
if (t0 >= t1)
|
|
237
|
+
return undefined;
|
|
238
|
+
}
|
|
239
|
+
return t1 - t0 > 1e-6 ? [t0, t1] : undefined;
|
|
240
|
+
}
|
|
241
|
+
/** Where along its connector a label placed by the layout may sit, in the order tried. */
|
|
242
|
+
const LABEL_STOPS = [0.5, 0.42, 0.58, 0.34, 0.66, 0.26, 0.74, 0.18, 0.82];
|
|
243
|
+
/** The shapes and group title bands a relation's label should not cover. */
|
|
244
|
+
function labelObstacles(diagram, boxes, sizing) {
|
|
245
|
+
const out = [];
|
|
246
|
+
for (const element of diagram.elements) {
|
|
247
|
+
const box = boxes.get(element.id);
|
|
248
|
+
if (!box)
|
|
249
|
+
continue;
|
|
250
|
+
if (element.kind !== "group")
|
|
251
|
+
out.push(box);
|
|
252
|
+
// An untitled group has no band to keep clear.
|
|
253
|
+
else if (!sizing.untitled(element)) {
|
|
254
|
+
const title = sizing.textNeeds(element).width;
|
|
255
|
+
out.push({
|
|
256
|
+
x: box.x,
|
|
257
|
+
y: box.y,
|
|
258
|
+
width: Math.min(box.width, title),
|
|
259
|
+
height: sizing.headerOf(element),
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
return out;
|
|
264
|
+
}
|
|
265
|
+
/** The box around an outline's points, without margin. */
|
|
266
|
+
function outlineBounds(points) {
|
|
267
|
+
const xs = points.map((p) => p.x);
|
|
268
|
+
const ys = points.map((p) => p.y);
|
|
269
|
+
const x = Math.min(...xs);
|
|
270
|
+
const y = Math.min(...ys);
|
|
271
|
+
// Turning by a right angle leaves rounding dust on the coordinates.
|
|
272
|
+
const clean = (v) => Math.round(v * 1e6) / 1e6;
|
|
273
|
+
return {
|
|
274
|
+
x: clean(x),
|
|
275
|
+
y: clean(y),
|
|
276
|
+
width: clean(Math.max(...xs) - x),
|
|
277
|
+
height: clean(Math.max(...ys) - y),
|
|
278
|
+
};
|
|
279
|
+
}
|
|
280
|
+
/** The box around a line's points. */
|
|
281
|
+
function boundsOf(points) {
|
|
282
|
+
let x0 = Infinity;
|
|
283
|
+
let y0 = Infinity;
|
|
284
|
+
let x1 = -Infinity;
|
|
285
|
+
let y1 = -Infinity;
|
|
286
|
+
for (const p of points) {
|
|
287
|
+
x0 = Math.min(x0, p.x);
|
|
288
|
+
y0 = Math.min(y0, p.y);
|
|
289
|
+
x1 = Math.max(x1, p.x);
|
|
290
|
+
y1 = Math.max(y1, p.y);
|
|
291
|
+
}
|
|
292
|
+
// A straight horizontal or vertical line still has an area to meet.
|
|
293
|
+
return { x: x0 - 1, y: y0 - 1, width: x1 - x0 + 2, height: y1 - y0 + 2 };
|
|
294
|
+
}
|
|
295
|
+
const SIDES = ["north", "east", "south", "west"];
|
|
296
|
+
const overlaps = (a, b) => Math.min(a.x + a.width, b.x + b.width) - Math.max(a.x, b.x) > 0.5 &&
|
|
297
|
+
Math.min(a.y + a.height, b.y + b.height) - Math.max(a.y, b.y) > 0.5;
|
|
298
|
+
/**
|
|
299
|
+
* Whether a point lies on a side of a box, within `tolerance` of it (the engine's ends lie a
|
|
300
|
+
* pixel outside, on its ports).
|
|
301
|
+
*/
|
|
302
|
+
function onSide(box, point, side, tolerance = 1.5) {
|
|
303
|
+
const right = box.x + box.width;
|
|
304
|
+
const bottom = box.y + box.height;
|
|
305
|
+
if (side === "north" || side === "south") {
|
|
306
|
+
const y = side === "north" ? box.y : bottom;
|
|
307
|
+
return (Math.abs(point.y - y) <= tolerance &&
|
|
308
|
+
point.x >= box.x - tolerance &&
|
|
309
|
+
point.x <= right + tolerance);
|
|
310
|
+
}
|
|
311
|
+
const x = side === "west" ? box.x : right;
|
|
312
|
+
return (Math.abs(point.x - x) <= tolerance &&
|
|
313
|
+
point.y >= box.y - tolerance &&
|
|
314
|
+
point.y <= bottom + tolerance);
|
|
315
|
+
}
|
|
316
|
+
/** Whether `inner` lies inside `outer`, borders included, to half a pixel. */
|
|
317
|
+
const containsBox = (outer, inner) => inner.x >= outer.x - 0.5 &&
|
|
318
|
+
inner.y >= outer.y - 0.5 &&
|
|
319
|
+
inner.x + inner.width <= outer.x + outer.width + 0.5 &&
|
|
320
|
+
inner.y + inner.height <= outer.y + outer.height + 0.5;
|
|
321
|
+
/**
|
|
322
|
+
* Slides each label the layout placed along its connector, from the middle outwards, to the first
|
|
323
|
+
* stop where it covers no shape, no group title, no other label and no other connector; a label
|
|
324
|
+
* with no clear stop keeps the stop that covers the least. Labels placed with `label` stay.
|
|
325
|
+
*/
|
|
326
|
+
function placeLabels(geometry, movable, obstacles) {
|
|
327
|
+
const moving = new Set(movable.map((m) => m.index));
|
|
328
|
+
const placed = geometry.flatMap((g, i) => g.label && !moving.has(i) ? [g.label.box] : []);
|
|
329
|
+
// The extent of each connector, to look at the few that come near a label.
|
|
330
|
+
const extents = geometry.map((g) => boundsOf(g.line));
|
|
331
|
+
for (const item of movable) {
|
|
332
|
+
const current = geometry[item.index];
|
|
333
|
+
if (!current.label)
|
|
334
|
+
continue;
|
|
335
|
+
let best;
|
|
336
|
+
for (const stop of LABEL_STOPS) {
|
|
337
|
+
const box = boxFromCenter(pointAlong(item.along, stop), item.width, item.height);
|
|
338
|
+
let cost = 0;
|
|
339
|
+
for (const o of obstacles)
|
|
340
|
+
if (overlaps(o, box))
|
|
341
|
+
cost++;
|
|
342
|
+
for (const p of placed)
|
|
343
|
+
if (overlaps(p, box))
|
|
344
|
+
cost++;
|
|
345
|
+
for (let i = 0; i < geometry.length; i++) {
|
|
346
|
+
if (i === item.index || !overlaps(extents[i], box))
|
|
347
|
+
continue;
|
|
348
|
+
if (crossedBoxes(geometry[i].line, [box]).length > 0)
|
|
349
|
+
cost++;
|
|
350
|
+
}
|
|
351
|
+
if (!best || cost < best.cost)
|
|
352
|
+
best = { box, cost };
|
|
353
|
+
if (cost === 0)
|
|
354
|
+
break;
|
|
355
|
+
}
|
|
356
|
+
const box = best.box;
|
|
357
|
+
geometry[item.index] = { ...current, label: { ...current.label, box } };
|
|
358
|
+
placed.push(box);
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
/** Kinds drawn by hand on the sheet as annotations: their labels may lie on boxes. */
|
|
362
|
+
const ANNOTATION_KINDS = new Set(["tag", "note", "text", "icon", "image"]);
|
|
363
|
+
/** Problems that already name a label, so that the label audit does not report them twice. */
|
|
364
|
+
const LABEL_CODES = new Set([
|
|
365
|
+
"layout.text-overflow",
|
|
366
|
+
"layout.label-under-connector",
|
|
367
|
+
"layout.overlap",
|
|
368
|
+
"layout.connector-crossing",
|
|
369
|
+
]);
|
|
370
|
+
/**
|
|
371
|
+
* Lays out one compiled diagram into a scene, then checks that no label is cut or overlaps
|
|
372
|
+
* another label, a box or a connector (`layout.label-cut`, `layout.label-overlap`).
|
|
373
|
+
*/
|
|
374
|
+
export function layoutDiagram(compilation, diagram, options = {}) {
|
|
375
|
+
const result = layoutDrawing(compilation, diagram, options);
|
|
376
|
+
if (!result.scene)
|
|
377
|
+
return result;
|
|
378
|
+
return {
|
|
379
|
+
...result,
|
|
380
|
+
diagnostics: [
|
|
381
|
+
...result.diagnostics,
|
|
382
|
+
...labelDiagnostics(diagram, result.scene, options.measurer ?? estimateMeasurer, result.diagnostics),
|
|
383
|
+
],
|
|
384
|
+
};
|
|
385
|
+
}
|
|
386
|
+
/** The label audit of a scene as warnings on the declarations that draw the labels. */
|
|
387
|
+
function labelDiagnostics(diagram, scene, measurer, reported) {
|
|
388
|
+
const annotations = new Set(diagram.elements.filter((e) => ANNOTATION_KINDS.has(e.kind)).map((e) => e.id));
|
|
389
|
+
const layers = new Map(diagram.elements.map((e) => [e.id, e.layer ?? "main"]));
|
|
390
|
+
const rangeOf = (owner) => diagram.elementsById.get(owner)?.range ??
|
|
391
|
+
diagram.relationsByKey.get(owner)?.range ??
|
|
392
|
+
diagram.eventsByKey.get(owner)?.range ??
|
|
393
|
+
diagram.idRange;
|
|
394
|
+
// Elements and relations other checks already name (an overflowing size, a label on a
|
|
395
|
+
// connector, overlapping boxes): their labels are part of that problem.
|
|
396
|
+
const named = new Set(reported.filter((d) => LABEL_CODES.has(d.code)).flatMap((d) => d.data?.ids ?? []));
|
|
397
|
+
const out = [];
|
|
398
|
+
const seen = new Set();
|
|
399
|
+
const words = messages();
|
|
400
|
+
const english = messages("en");
|
|
401
|
+
// The frame's texts belong to no element.
|
|
402
|
+
const subject = (owner) => (owner === "frame" || owner === "scene" ? null : owner);
|
|
403
|
+
for (const problem of auditLabels(scene, measurer, { annotations, layers })) {
|
|
404
|
+
const owner = problem.text.owner;
|
|
405
|
+
const range = rangeOf(owner);
|
|
406
|
+
if (named.has(owner))
|
|
407
|
+
continue;
|
|
408
|
+
if (problem.kind === "overlap" &&
|
|
409
|
+
problem.other.kind !== "connector" &&
|
|
410
|
+
named.has(problem.other.owner)) {
|
|
411
|
+
continue;
|
|
412
|
+
}
|
|
413
|
+
const text = problem.text.text.length > 40 ? `${problem.text.text.slice(0, 39)}…` : problem.text.text;
|
|
414
|
+
let describe;
|
|
415
|
+
let hint;
|
|
416
|
+
if (problem.kind === "cut") {
|
|
417
|
+
const { by } = problem;
|
|
418
|
+
const overflow = Math.ceil(problem.overflow);
|
|
419
|
+
describe = (t) => t.diagnostics.labelCut(text, subject(owner), overflow, by);
|
|
420
|
+
hint = by === "picture" ? words.hints.labelCutByPicture : words.hints.labelCut;
|
|
421
|
+
}
|
|
422
|
+
else {
|
|
423
|
+
const other = problem.other.kind === "text"
|
|
424
|
+
? {
|
|
425
|
+
kind: "text",
|
|
426
|
+
owner: subject(problem.other.owner),
|
|
427
|
+
text: problem.other.text ?? "",
|
|
428
|
+
}
|
|
429
|
+
: { kind: problem.other.kind, owner: problem.other.owner };
|
|
430
|
+
describe = (t) => t.diagnostics.labelOverlap(text, subject(owner), other);
|
|
431
|
+
hint =
|
|
432
|
+
problem.other.kind === "connector"
|
|
433
|
+
? words.hints.labelOnConnector
|
|
434
|
+
: words.hints.labelOverlap;
|
|
435
|
+
}
|
|
436
|
+
// Problems are told apart by their English words, so every language keeps the same ones.
|
|
437
|
+
const key = `${range.start.offset}|${describe(english)}`;
|
|
438
|
+
if (seen.has(key))
|
|
439
|
+
continue;
|
|
440
|
+
seen.add(key);
|
|
441
|
+
out.push({
|
|
442
|
+
code: problem.kind === "cut" ? "layout.label-cut" : "layout.label-overlap",
|
|
443
|
+
severity: "warning",
|
|
444
|
+
message: describe(words),
|
|
445
|
+
range,
|
|
446
|
+
hints: [hint],
|
|
447
|
+
});
|
|
448
|
+
}
|
|
449
|
+
return out;
|
|
450
|
+
}
|
|
451
|
+
/**
|
|
452
|
+
* Where an element's style takes a property from, as the style resolver applies them: the last
|
|
453
|
+
* `style` statement of the element that writes it, else the last class it applies that does, else
|
|
454
|
+
* the last diagram rule of its kind, else the theme chain (its classes and rules) or the element's
|
|
455
|
+
* declared type. `key` names that source, the same for every element it styles; `range` is the
|
|
456
|
+
* property statement in the diagram's block, or the element's declaration when the theme chain or
|
|
457
|
+
* the type gives it (`inherited`).
|
|
458
|
+
*/
|
|
459
|
+
function styleSource(diagram, element, property, lines, styles) {
|
|
460
|
+
const inBlock = (node, range) => {
|
|
461
|
+
const statement = node.block?.statements.find((s) => s.kind === "KeywordStatement" && s.keyword.text === property);
|
|
462
|
+
const at = statement ? lines.range({ start: statement.start, end: statement.end }) : range;
|
|
463
|
+
return { key: `${at.start.offset}`, range: at, inherited: false };
|
|
464
|
+
};
|
|
465
|
+
const kind = element.typeKind ?? element.kind;
|
|
466
|
+
const own = diagram.styles.filter((entry) => entry.target === element.id);
|
|
467
|
+
const written = own.filter((entry) => property in entry.properties).pop();
|
|
468
|
+
if (written)
|
|
469
|
+
return inBlock(written.node, written.range);
|
|
470
|
+
const used = [...element.classes, ...own.flatMap((entry) => entry.classes)]
|
|
471
|
+
.filter((use) => property in (styles.classProperties(use.name) ?? {}))
|
|
472
|
+
.pop();
|
|
473
|
+
const declared = used && diagram.classes.find((c) => c.id === used.name);
|
|
474
|
+
if (declared)
|
|
475
|
+
return inBlock(declared.node, declared.range);
|
|
476
|
+
if (used)
|
|
477
|
+
return { key: `class ${used.name}`, range: element.range, inherited: true };
|
|
478
|
+
const rule = diagram.rules.filter((r) => r.kind === kind && property in r.properties).pop();
|
|
479
|
+
if (rule)
|
|
480
|
+
return inBlock(rule.node, rule.range);
|
|
481
|
+
return { key: `kind ${kind}`, range: element.range, inherited: true };
|
|
482
|
+
}
|
|
483
|
+
/** Kinds whose icon can be the centre piece, which `labelPosition` places the label of. */
|
|
484
|
+
const LABEL_PLACED = new Set(["node", "decision", "data", "box"]);
|
|
485
|
+
/**
|
|
486
|
+
* `layout.icon-missing` for elements drawn as their icon without one, and
|
|
487
|
+
* `layout.label-position-ignored` for a `labelPosition` that no element it styles uses (its icon
|
|
488
|
+
* is not the centre piece), once per statement that writes the property, or once for a theme rule
|
|
489
|
+
* or class, on the diagram's `theme` (`themeRange`).
|
|
490
|
+
*/
|
|
491
|
+
function iconDiagnostics(diagram, styles, sizing, lines, words, themeRange) {
|
|
492
|
+
const out = [];
|
|
493
|
+
const missing = new Set();
|
|
494
|
+
const labels = new Map();
|
|
495
|
+
for (const element of diagram.elements) {
|
|
496
|
+
const style = styles.element(element);
|
|
497
|
+
if (sizing.iconShaped(element) && style.icon === undefined) {
|
|
498
|
+
const { range } = styleSource(diagram, element, "shape", lines, styles);
|
|
499
|
+
if (!missing.has(range.start.offset)) {
|
|
500
|
+
missing.add(range.start.offset);
|
|
501
|
+
out.push({
|
|
502
|
+
code: "layout.icon-missing",
|
|
503
|
+
severity: "warning",
|
|
504
|
+
message: words.diagnostics.iconMissing(element.id),
|
|
505
|
+
range,
|
|
506
|
+
hints: [words.hints.iconMissing(element.id)],
|
|
507
|
+
data: { ids: [element.id] },
|
|
508
|
+
});
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
if (style.labelPosition !== undefined) {
|
|
512
|
+
const source = styleSource(diagram, element, "labelPosition", lines, styles);
|
|
513
|
+
const known = labels.get(source.key);
|
|
514
|
+
const used = sizing.iconCentrePiece(element);
|
|
515
|
+
const placed = LABEL_PLACED.has(element.kind);
|
|
516
|
+
if (known) {
|
|
517
|
+
known.used ||= used;
|
|
518
|
+
known.placed ||= placed;
|
|
519
|
+
}
|
|
520
|
+
else {
|
|
521
|
+
labels.set(source.key, {
|
|
522
|
+
range: source.inherited ? themeRange : source.range,
|
|
523
|
+
element: element.id,
|
|
524
|
+
used,
|
|
525
|
+
placed,
|
|
526
|
+
inherited: source.inherited,
|
|
527
|
+
});
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
for (const { range, element, used, placed, inherited } of labels.values()) {
|
|
532
|
+
if (used)
|
|
533
|
+
continue;
|
|
534
|
+
out.push({
|
|
535
|
+
code: "layout.label-position-ignored",
|
|
536
|
+
severity: "warning",
|
|
537
|
+
message: inherited
|
|
538
|
+
? words.diagnostics.labelPositionIgnoredByTheme(element)
|
|
539
|
+
: words.diagnostics.labelPositionIgnored(element),
|
|
540
|
+
range,
|
|
541
|
+
hints: [
|
|
542
|
+
!placed
|
|
543
|
+
? words.hints.labelPositionNeverUsed
|
|
544
|
+
: inherited
|
|
545
|
+
? words.hints.labelPositionIgnoredByTheme
|
|
546
|
+
: words.hints.labelPositionIgnored,
|
|
547
|
+
],
|
|
548
|
+
data: { ids: [element] },
|
|
549
|
+
});
|
|
550
|
+
}
|
|
551
|
+
return out;
|
|
552
|
+
}
|
|
553
|
+
/** Lays out one compiled diagram into a scene. */
|
|
554
|
+
function layoutDrawing(compilation, diagram, options = {}) {
|
|
555
|
+
const diagnostics = [];
|
|
556
|
+
const lines = compilation.parse.lines;
|
|
557
|
+
// Diagnostics are written in the interface language of the moment they are made.
|
|
558
|
+
const words = messages();
|
|
559
|
+
const written = options.theme ?? diagram.properties.theme;
|
|
560
|
+
const definitions = options.themes ?? compilation.document.themes;
|
|
561
|
+
const resolved = resolveTheme(written ?? options.defaultTheme ?? null, definitions);
|
|
562
|
+
// A default theme that does not resolve is reported where it is configured.
|
|
563
|
+
const { theme } = resolved.known || written !== null ? resolved : resolveTheme(null, definitions);
|
|
564
|
+
if (!resolved.known && written !== null) {
|
|
565
|
+
const fileThemes = definitions.map((t) => t.id).filter((id) => !id.includes("#"));
|
|
566
|
+
diagnostics.push({
|
|
567
|
+
code: "layout.unknown-theme",
|
|
568
|
+
severity: "warning",
|
|
569
|
+
message: words.diagnostics.unknownTheme(written, DEFAULT_THEME_NAME),
|
|
570
|
+
range: options.theme !== undefined && options.theme !== null
|
|
571
|
+
? diagram.idRange
|
|
572
|
+
: (propertyRange(diagram, "theme", lines) ?? diagram.idRange),
|
|
573
|
+
hints: [words.hints.themes(Object.keys(THEMES), fileThemes)],
|
|
574
|
+
data: { candidates: [...Object.keys(THEMES), ...fileThemes] },
|
|
575
|
+
});
|
|
576
|
+
}
|
|
577
|
+
const scale = typeScale(theme);
|
|
578
|
+
const metrics = new TextMetrics(options.measurer ?? estimateMeasurer, theme);
|
|
579
|
+
if (diagram.type === "sequence") {
|
|
580
|
+
return layoutSequence({ compilation, diagram, options, theme, scale, metrics, diagnostics });
|
|
581
|
+
}
|
|
582
|
+
// C4 views draw the model's entities upright; flow and free diagrams turn theirs (decision 0045).
|
|
583
|
+
const turnable = diagram.type !== "c4";
|
|
584
|
+
if (diagram.type === "c4")
|
|
585
|
+
diagram = c4Drawing(diagram, options.locale ?? DEFAULT_LOCALE);
|
|
586
|
+
const places = new Map();
|
|
587
|
+
const sizes = new Map();
|
|
588
|
+
const sizeEntries = new Map();
|
|
589
|
+
const routes = new Map();
|
|
590
|
+
const labels = new Map();
|
|
591
|
+
for (const entry of diagram.placement) {
|
|
592
|
+
switch (entry.statement) {
|
|
593
|
+
case "place":
|
|
594
|
+
places.set(entry.target, { x: entry.x, y: entry.y });
|
|
595
|
+
break;
|
|
596
|
+
case "size":
|
|
597
|
+
sizes.set(entry.target, { width: entry.width, height: entry.height });
|
|
598
|
+
sizeEntries.set(entry.target, entry);
|
|
599
|
+
break;
|
|
600
|
+
case "route":
|
|
601
|
+
routes.set(entry.target, entry);
|
|
602
|
+
break;
|
|
603
|
+
case "label":
|
|
604
|
+
labels.set(entry.target, entry);
|
|
605
|
+
break;
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
const mode = options.layout ?? diagram.properties.layout;
|
|
609
|
+
const free = diagram.type === "free";
|
|
610
|
+
const unplaced = diagram.elements.filter((e) => e.kind !== "group" && !places.has(e.id));
|
|
611
|
+
// Places are ignored in `auto` only when an engine arranges the diagram.
|
|
612
|
+
const arranging = options.engine !== undefined &&
|
|
613
|
+
(mode === "auto" ? true : mode === "assisted" && unplaced.length > 0);
|
|
614
|
+
// Free diagrams place children inside their group, so a group must be placed when its
|
|
615
|
+
// children are, and always when nothing is arranged.
|
|
616
|
+
if (free && !(arranging && mode === "auto")) {
|
|
617
|
+
const placedChild = new Set(diagram.elements.flatMap((e) => (places.has(e.id) ? [parentOf(e) ?? ""] : [])));
|
|
618
|
+
const missing = diagram.elements.filter((e) => e.kind === "group" &&
|
|
619
|
+
!places.has(e.id) &&
|
|
620
|
+
(!arranging || placedChild.has(e.id) || unplaced.length === 0));
|
|
621
|
+
for (const group of missing) {
|
|
622
|
+
diagnostics.push({
|
|
623
|
+
code: "layout.missing-placement",
|
|
624
|
+
severity: "error",
|
|
625
|
+
message: words.diagnostics.groupWithoutPlace(group.id),
|
|
626
|
+
range: group.range,
|
|
627
|
+
hints: [words.hints.addPlace(group.id)],
|
|
628
|
+
});
|
|
629
|
+
}
|
|
630
|
+
if (missing.length > 0 && (unplaced.length === 0 || arranging)) {
|
|
631
|
+
return { scene: null, diagnostics };
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
if (unplaced.length > 0 && !arranging) {
|
|
635
|
+
if (mode === "manual") {
|
|
636
|
+
for (const element of unplaced) {
|
|
637
|
+
diagnostics.push({
|
|
638
|
+
code: "layout.missing-placement",
|
|
639
|
+
severity: "error",
|
|
640
|
+
message: words.diagnostics.elementWithoutPlace(element.id),
|
|
641
|
+
range: element.range,
|
|
642
|
+
hints: [words.hints.addPlace(element.id)],
|
|
643
|
+
});
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
else {
|
|
647
|
+
diagnostics.push({
|
|
648
|
+
code: "layout.mode-unsupported",
|
|
649
|
+
severity: "error",
|
|
650
|
+
message: words.diagnostics.modeUnsupported(mode, unplaced.map((e) => e.id)),
|
|
651
|
+
range: propertyRange(diagram, "layout", lines) ?? diagram.idRange,
|
|
652
|
+
hints: [words.hints.engine],
|
|
653
|
+
});
|
|
654
|
+
}
|
|
655
|
+
return { scene: null, diagnostics };
|
|
656
|
+
}
|
|
657
|
+
// A placed and sized group that holds nothing is a band: it stays where it is written, out of
|
|
658
|
+
// the engine's request. In `auto` only bands at the top level, whose places are in diagram
|
|
659
|
+
// coordinates, are kept.
|
|
660
|
+
const childless = (group) => !diagram.elements.some((child) => child.groupPath.includes(group.id));
|
|
661
|
+
const autoBands = new Set(diagram.elements
|
|
662
|
+
.filter((e) => e.kind === "group" &&
|
|
663
|
+
e.groupPath.length === 0 &&
|
|
664
|
+
places.has(e.id) &&
|
|
665
|
+
sizes.has(e.id) &&
|
|
666
|
+
childless(e))
|
|
667
|
+
.map((e) => e.id));
|
|
668
|
+
if (arranging && mode === "auto") {
|
|
669
|
+
const ignored = diagram.placement
|
|
670
|
+
.filter((e) => (e.statement === "place" && !autoBands.has(e.target)) ||
|
|
671
|
+
(e.statement === "route" && e.via.length > 0))
|
|
672
|
+
.sort((a, b) => a.range.start.offset - b.range.start.offset)[0];
|
|
673
|
+
if (ignored) {
|
|
674
|
+
diagnostics.push({
|
|
675
|
+
code: "layout.placement-ignored",
|
|
676
|
+
severity: "warning",
|
|
677
|
+
message: words.diagnostics.placementIgnored,
|
|
678
|
+
range: ignored.range,
|
|
679
|
+
hints: [words.hints.assisted],
|
|
680
|
+
});
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
const styles = new StyleResolver(theme, diagram);
|
|
684
|
+
const images = new Map();
|
|
685
|
+
for (const element of diagram.elements) {
|
|
686
|
+
if (element.kind !== "image" || element.source === null)
|
|
687
|
+
continue;
|
|
688
|
+
const image = options.images?.resolve(element.source);
|
|
689
|
+
images.set(element.id, image);
|
|
690
|
+
if (!image) {
|
|
691
|
+
diagnostics.push({
|
|
692
|
+
code: "layout.image-unavailable",
|
|
693
|
+
severity: "warning",
|
|
694
|
+
message: words.diagnostics.imageUnavailable(element.source, element.id),
|
|
695
|
+
range: element.sourceRange ?? element.range,
|
|
696
|
+
hints: [options.images ? words.hints.imageFile : words.hints.imageHost],
|
|
697
|
+
});
|
|
698
|
+
}
|
|
699
|
+
}
|
|
700
|
+
const sizing = new Sizing(theme, scale, metrics, styles, options.icons, images, new Map(sizes));
|
|
701
|
+
diagnostics.push(...iconDiagnostics(diagram, styles, sizing, lines, words, propertyRange(diagram, "theme", lines) ?? diagram.idRange));
|
|
702
|
+
const boxes = new Map();
|
|
703
|
+
const elementSizes = new Map();
|
|
704
|
+
for (const element of diagram.elements) {
|
|
705
|
+
if (element.kind === "group")
|
|
706
|
+
continue;
|
|
707
|
+
const explicit = sizes.get(element.id);
|
|
708
|
+
const size = explicit ?? sizing.defaultSize(element);
|
|
709
|
+
if (explicit)
|
|
710
|
+
sizing.checkOverflow(element, explicit, sizeEntries.get(element.id), diagnostics);
|
|
711
|
+
elementSizes.set(element.id, size);
|
|
712
|
+
}
|
|
713
|
+
// Turned elements: their angle, clockwise; 0 and 360 draw upright.
|
|
714
|
+
const rotations = new Map();
|
|
715
|
+
if (turnable) {
|
|
716
|
+
for (const element of diagram.elements) {
|
|
717
|
+
if (!takesRotation(element.kind))
|
|
718
|
+
continue;
|
|
719
|
+
const written = styles.element(element).rotation;
|
|
720
|
+
const degrees = written === undefined ? 0 : ((written % 360) + 360) % 360;
|
|
721
|
+
if (degrees !== 0)
|
|
722
|
+
rotations.set(element.id, degrees);
|
|
723
|
+
}
|
|
724
|
+
}
|
|
725
|
+
/** The outline of a turned element drawn at `box`, turned about the box's centre. */
|
|
726
|
+
const turnedAt = (id, box) => {
|
|
727
|
+
const element = diagram.elementsById.get(id);
|
|
728
|
+
const shape = element.kind === "tag" ? "pill" : sizing.shapeOf(element);
|
|
729
|
+
return turnedOutline(shape, box, rotations.get(id), sizing.shapeOptions(element));
|
|
730
|
+
};
|
|
731
|
+
// Icon shapes draw their label outside their box: the engine, derived groups, the obstacles of
|
|
732
|
+
// connectors and the canvas take the box with its label, its extent; the composition checks
|
|
733
|
+
// take the box and the label apart.
|
|
734
|
+
const outside = new Map();
|
|
735
|
+
for (const element of diagram.elements) {
|
|
736
|
+
const size = elementSizes.get(element.id);
|
|
737
|
+
const label = size && sizing.outsideLabel(element, { x: 0, y: 0, ...size });
|
|
738
|
+
if (label)
|
|
739
|
+
outside.set(element.id, label.box);
|
|
740
|
+
}
|
|
741
|
+
/** The label outside an element's box drawn at `box`, if it has one. */
|
|
742
|
+
const labelAt = (id, box) => {
|
|
743
|
+
const label = outside.get(id);
|
|
744
|
+
return label && { ...label, x: box.x + label.x, y: box.y + label.y };
|
|
745
|
+
};
|
|
746
|
+
/**
|
|
747
|
+
* An element's box with the label it draws outside it; for a turned element, the bounding box
|
|
748
|
+
* of its turned outline and label, its drawn box.
|
|
749
|
+
*/
|
|
750
|
+
const extentAt = (id, box) => {
|
|
751
|
+
const label = labelAt(id, box);
|
|
752
|
+
const degrees = rotations.get(id);
|
|
753
|
+
if (degrees === undefined)
|
|
754
|
+
return label ? unionBoxes([box, label]) : box;
|
|
755
|
+
const center = boxCenter(box);
|
|
756
|
+
const corners = label
|
|
757
|
+
? [
|
|
758
|
+
{ x: label.x, y: label.y },
|
|
759
|
+
{ x: label.x + label.width, y: label.y },
|
|
760
|
+
{ x: label.x + label.width, y: label.y + label.height },
|
|
761
|
+
{ x: label.x, y: label.y + label.height },
|
|
762
|
+
].map((p) => turnPoint(p, center, degrees))
|
|
763
|
+
: [];
|
|
764
|
+
return outlineBounds([...turnedAt(id, box), ...corners]);
|
|
765
|
+
};
|
|
766
|
+
// Free diagrams: turn places relative to group interiors into diagram coordinates, and give
|
|
767
|
+
// every placed group its written or derived size.
|
|
768
|
+
if (free && !(arranging && mode === "auto")) {
|
|
769
|
+
const frames = freeFrames(diagram, places, sizes, elementSizes, theme, (group) => sizing.textNeeds(group).width, (group) => sizing.headerOf(group), (id, size) => outside.has(id) || rotations.has(id)
|
|
770
|
+
? extentAt(id, boxFromCenter({ x: 0, y: 0 }, size.width, size.height))
|
|
771
|
+
: undefined);
|
|
772
|
+
places.clear();
|
|
773
|
+
for (const [id, center] of frames.centers)
|
|
774
|
+
places.set(id, center);
|
|
775
|
+
for (const [id, size] of frames.groupSizes)
|
|
776
|
+
sizes.set(id, size);
|
|
777
|
+
}
|
|
778
|
+
// Positions: placed, or arranged by the engine; fixed ones never move afterwards.
|
|
779
|
+
const centers = new Map();
|
|
780
|
+
const fixed = new Set();
|
|
781
|
+
let engineRoutes = new Map();
|
|
782
|
+
let engineGroups = new Map();
|
|
783
|
+
// `auto`: the written size of a group with children is the least size the engine gives it.
|
|
784
|
+
const minimums = new Map();
|
|
785
|
+
if (arranging && mode === "auto") {
|
|
786
|
+
for (const element of diagram.elements) {
|
|
787
|
+
const size = sizes.get(element.id);
|
|
788
|
+
if (element.kind === "group" && size && !childless(element))
|
|
789
|
+
minimums.set(element.id, size);
|
|
790
|
+
}
|
|
791
|
+
}
|
|
792
|
+
if (arranging) {
|
|
793
|
+
// A band stays out of the request: the engine would give the empty container a slot of its
|
|
794
|
+
// own and move the boxes it arranges.
|
|
795
|
+
const bands = new Set(mode === "assisted"
|
|
796
|
+
? diagram.elements
|
|
797
|
+
.filter((e) => e.kind === "group" && places.has(e.id) && sizes.has(e.id) && childless(e))
|
|
798
|
+
.map((e) => e.id)
|
|
799
|
+
: autoBands);
|
|
800
|
+
// Icon shapes are arranged with their label: the engine places their extent, whose centre lies
|
|
801
|
+
// off their box's, and their relations end on ports along the icon's sides.
|
|
802
|
+
// A turned element is arranged as its drawn box.
|
|
803
|
+
const extents = new Map();
|
|
804
|
+
const arranged = new Map(elementSizes);
|
|
805
|
+
const shifts = new Map();
|
|
806
|
+
for (const id of new Set([...outside.keys(), ...rotations.keys()])) {
|
|
807
|
+
const size = elementSizes.get(id);
|
|
808
|
+
if (!size)
|
|
809
|
+
continue;
|
|
810
|
+
const extent = extentAt(id, { x: 0, y: 0, ...size });
|
|
811
|
+
// Ports along an icon's sides only hold while the icon is upright.
|
|
812
|
+
if (!rotations.has(id))
|
|
813
|
+
extents.set(id, { size, extent });
|
|
814
|
+
arranged.set(id, { width: extent.width, height: extent.height });
|
|
815
|
+
shifts.set(id, {
|
|
816
|
+
x: extent.x + extent.width / 2 - size.width / 2,
|
|
817
|
+
y: extent.y + extent.height / 2 - size.height / 2,
|
|
818
|
+
});
|
|
819
|
+
}
|
|
820
|
+
const shifted = (id, point, sign) => {
|
|
821
|
+
const shift = shifts.get(id);
|
|
822
|
+
return shift ? { x: point.x + sign * shift.x, y: point.y + sign * shift.y } : point;
|
|
823
|
+
};
|
|
824
|
+
let arrangement;
|
|
825
|
+
const request = (mirrored) => arrangeRequest(diagram, arranged, mode === "assisted"
|
|
826
|
+
? new Map([...places].map(([id, place]) => [id, shifted(id, place, 1)]))
|
|
827
|
+
: new Map(), routes, theme, (group) => sizing.headerOf(group), (relation) => labelSize(relation, metrics, scale), bands, minimums, extents, mirrored);
|
|
828
|
+
try {
|
|
829
|
+
arrangement = options.engine.arrange(request(new Set()));
|
|
830
|
+
// The ports of icon shapes face the flow; a relation the engine turns back would wrap
|
|
831
|
+
// around the diagram to reach them, so it is arranged again with its ports facing back,
|
|
832
|
+
// and the shorter routes are kept.
|
|
833
|
+
const backward = backwardAtIcons(diagram, arrangement, arranged, extents, bands, routes);
|
|
834
|
+
if (backward.size > 0) {
|
|
835
|
+
const again = options.engine.arrange(request(backward));
|
|
836
|
+
if (routesLength(again) < routesLength(arrangement))
|
|
837
|
+
arrangement = again;
|
|
838
|
+
}
|
|
839
|
+
}
|
|
840
|
+
catch (error) {
|
|
841
|
+
diagnostics.push({
|
|
842
|
+
code: "layout.engine-failed",
|
|
843
|
+
severity: "error",
|
|
844
|
+
message: words.diagnostics.engineFailed(options.engine.name, error.message),
|
|
845
|
+
range: propertyRange(diagram, "layout", lines) ?? diagram.idRange,
|
|
846
|
+
});
|
|
847
|
+
return { scene: null, diagnostics };
|
|
848
|
+
}
|
|
849
|
+
if (shifts.size > 0) {
|
|
850
|
+
arrangement = {
|
|
851
|
+
...arrangement,
|
|
852
|
+
centers: new Map([...arrangement.centers].map(([id, center]) => [id, shifted(id, center, -1)])),
|
|
853
|
+
};
|
|
854
|
+
}
|
|
855
|
+
if (mode === "auto") {
|
|
856
|
+
for (const [id, center] of arrangement.centers)
|
|
857
|
+
centers.set(id, center);
|
|
858
|
+
engineRoutes = arrangement.routes;
|
|
859
|
+
engineGroups = arrangement.groups ?? new Map();
|
|
860
|
+
}
|
|
861
|
+
else {
|
|
862
|
+
placeAround(diagram, arrangement, places, elementSizes, centers, fixed, (id, box) => extentAt(id, box));
|
|
863
|
+
}
|
|
864
|
+
}
|
|
865
|
+
else {
|
|
866
|
+
for (const [id, place] of places)
|
|
867
|
+
centers.set(id, place);
|
|
868
|
+
for (const element of diagram.elements)
|
|
869
|
+
fixed.add(element.id);
|
|
870
|
+
}
|
|
871
|
+
// Strong constraints: free boxes move until they hold; what cannot hold is a conflict.
|
|
872
|
+
const solverBoxes = new Map();
|
|
873
|
+
for (const [id, size] of elementSizes) {
|
|
874
|
+
const center = centers.get(id);
|
|
875
|
+
if (center)
|
|
876
|
+
solverBoxes.set(id, { center, ...size, fixed: fixed.has(id) });
|
|
877
|
+
}
|
|
878
|
+
const before = new Map([...solverBoxes].map(([id, b]) => [id, b.center]));
|
|
879
|
+
const defaultAxis = diagram.properties.direction === "TB" || diagram.properties.direction === "BT"
|
|
880
|
+
? "vertical"
|
|
881
|
+
: "horizontal";
|
|
882
|
+
for (const conflict of solveConstraints(diagram.constraints, solverBoxes, defaultAxis, ENGINE_SPACING.node)) {
|
|
883
|
+
diagnostics.push({
|
|
884
|
+
code: "layout.constraint-conflict",
|
|
885
|
+
severity: "error",
|
|
886
|
+
message: conflict.message,
|
|
887
|
+
range: conflict.entry.range,
|
|
888
|
+
hints: [words.hints.constraint],
|
|
889
|
+
});
|
|
890
|
+
}
|
|
891
|
+
const movedByConstraints = new Set();
|
|
892
|
+
for (const [id, box] of solverBoxes) {
|
|
893
|
+
if (box.center !== before.get(id))
|
|
894
|
+
movedByConstraints.add(id);
|
|
895
|
+
const size = elementSizes.get(id);
|
|
896
|
+
boxes.set(id, boxFromCenter(box.center, size.width, size.height));
|
|
897
|
+
}
|
|
898
|
+
const groups = diagram.elements
|
|
899
|
+
.filter((e) => e.kind === "group")
|
|
900
|
+
.sort((a, b) => b.groupPath.length - a.groupPath.length);
|
|
901
|
+
const explicitGroups = new Set();
|
|
902
|
+
for (const group of groups) {
|
|
903
|
+
const arranged = mode === "auto" && arranging && !autoBands.has(group.id);
|
|
904
|
+
const place = arranged ? undefined : places.get(group.id);
|
|
905
|
+
const size = arranged ? undefined : sizes.get(group.id);
|
|
906
|
+
if (place && size) {
|
|
907
|
+
const entry = sizeEntries.get(group.id);
|
|
908
|
+
if (entry)
|
|
909
|
+
sizing.checkOverflow(group, size, entry, diagnostics);
|
|
910
|
+
boxes.set(group.id, boxFromCenter(place, size.width, size.height));
|
|
911
|
+
explicitGroups.add(group.id);
|
|
912
|
+
continue;
|
|
913
|
+
}
|
|
914
|
+
if (place || size) {
|
|
915
|
+
diagnostics.push({
|
|
916
|
+
code: "layout.missing-placement",
|
|
917
|
+
severity: "error",
|
|
918
|
+
message: words.diagnostics.groupHalfPlaced(group.id, place ? "place" : "size"),
|
|
919
|
+
range: group.range,
|
|
920
|
+
});
|
|
921
|
+
continue;
|
|
922
|
+
}
|
|
923
|
+
const children = diagram.elements
|
|
924
|
+
.filter((e) => e.groupPath.includes(group.id))
|
|
925
|
+
.map((e) => {
|
|
926
|
+
const box = boxes.get(e.id);
|
|
927
|
+
return box && extentAt(e.id, box);
|
|
928
|
+
})
|
|
929
|
+
.filter((b) => b !== undefined);
|
|
930
|
+
const union = unionBoxes(children);
|
|
931
|
+
if (!union) {
|
|
932
|
+
diagnostics.push({
|
|
933
|
+
code: "layout.missing-placement",
|
|
934
|
+
severity: "error",
|
|
935
|
+
message: words.diagnostics.groupUnplaced(group.id),
|
|
936
|
+
range: group.range,
|
|
937
|
+
hints: [words.hints.addPlaceAndSize(group.id)],
|
|
938
|
+
});
|
|
939
|
+
continue;
|
|
940
|
+
}
|
|
941
|
+
const derived = expandBox(union, theme.groupPadding, sizing.headerOf(group), theme.groupPadding, theme.groupPadding);
|
|
942
|
+
// The title is left-aligned; a wider title widens the band to the right.
|
|
943
|
+
const titleWidth = sizing.textNeeds(group).width;
|
|
944
|
+
const fitted = { ...derived, width: Math.max(derived.width, titleWidth) };
|
|
945
|
+
const minimum = minimums.get(group.id);
|
|
946
|
+
if (!minimum) {
|
|
947
|
+
boxes.set(group.id, fitted);
|
|
948
|
+
continue;
|
|
949
|
+
}
|
|
950
|
+
// A sized group is drawn at the engine's box, which holds its children and is at least its
|
|
951
|
+
// written size; an engine that gives no box, or a smaller one, leaves it grown about its centre.
|
|
952
|
+
const engineBox = engineGroups.get(group.id);
|
|
953
|
+
const box = engineBox ? unionBoxes([engineBox, fitted]) : fitted;
|
|
954
|
+
const width = Math.max(box.width, minimum.width);
|
|
955
|
+
const height = Math.max(box.height, minimum.height);
|
|
956
|
+
boxes.set(group.id, width === box.width && height === box.height
|
|
957
|
+
? box
|
|
958
|
+
: boxFromCenter(boxCenter(box), width, height));
|
|
959
|
+
}
|
|
960
|
+
if (diagnostics.some((d) => d.severity === "error" && d.code !== "layout.constraint-conflict")) {
|
|
961
|
+
return { scene: null, diagnostics };
|
|
962
|
+
}
|
|
963
|
+
const origins = new Map();
|
|
964
|
+
if (free) {
|
|
965
|
+
for (const element of diagram.elements) {
|
|
966
|
+
const parent = parentOf(element);
|
|
967
|
+
const group = parent === null ? undefined : diagram.elementsById.get(parent);
|
|
968
|
+
const area = group && boxes.get(group.id);
|
|
969
|
+
if (group && area) {
|
|
970
|
+
const header = sizing.headerOf(group);
|
|
971
|
+
origins.set(element.id, { x: area.x + theme.groupPadding, y: area.y + header });
|
|
972
|
+
}
|
|
973
|
+
}
|
|
974
|
+
}
|
|
975
|
+
// The drawn shape of every element box, and the outline of those that are not their box; a
|
|
976
|
+
// turned element's outline is turned, and its drawn box is the bounding box of that outline.
|
|
977
|
+
const shapes = new Map();
|
|
978
|
+
const outlines = new Map();
|
|
979
|
+
const turnedBoxes = new Map();
|
|
980
|
+
for (const element of diagram.elements) {
|
|
981
|
+
const box = boxes.get(element.id);
|
|
982
|
+
if (element.kind === "group" || !box)
|
|
983
|
+
continue;
|
|
984
|
+
const shape = element.kind === "tag" ? "pill" : sizing.shapeOf(element);
|
|
985
|
+
shapes.set(element.id, shape);
|
|
986
|
+
if (rotations.has(element.id)) {
|
|
987
|
+
const turned = turnedAt(element.id, box);
|
|
988
|
+
outlines.set(element.id, turned);
|
|
989
|
+
turnedBoxes.set(element.id, outlineBounds(turned));
|
|
990
|
+
}
|
|
991
|
+
else if (isOutlined(shape)) {
|
|
992
|
+
outlines.set(element.id, shapeOutline(shape, box, sizing.shapeOptions(element)).points);
|
|
993
|
+
}
|
|
994
|
+
}
|
|
995
|
+
/** An end found on a side of an element's box, moved onto the outline of its shape. */
|
|
996
|
+
const outlineEnd = (id, point, known) => {
|
|
997
|
+
const turned = turnedBoxes.get(id);
|
|
998
|
+
if (turned) {
|
|
999
|
+
// On a turned element: from the side of its drawn box onto its turned outline. A named port
|
|
1000
|
+
// or a pinned point already lies on that outline and stays.
|
|
1001
|
+
const side = onSide(turned, point, known) ? known : sideOfPoint(turned, point);
|
|
1002
|
+
return onSide(turned, point, side) ? onPolygon(outlines.get(id), point, side) : point;
|
|
1003
|
+
}
|
|
1004
|
+
const shape = shapes.get(id);
|
|
1005
|
+
const box = boxes.get(id);
|
|
1006
|
+
if (!shape || !box || !isOutlined(shape))
|
|
1007
|
+
return point;
|
|
1008
|
+
const side = onSide(box, point, known) ? known : sideOfPoint(box, point);
|
|
1009
|
+
if (!onSide(box, point, side))
|
|
1010
|
+
return point;
|
|
1011
|
+
return onOutline(shape, box, point, side, sizing.shapeOptions(diagram.elementsById.get(id)));
|
|
1012
|
+
};
|
|
1013
|
+
/**
|
|
1014
|
+
* The boxes a connector should not cross: the other shapes of its layer, but not its ends, the
|
|
1015
|
+
* groups that hold them, nor a shape that contains an end box (a box it is stacked inside).
|
|
1016
|
+
*/
|
|
1017
|
+
const shapeIds = diagram.elements.filter((e) => e.kind !== "group" && e.kind !== "tag");
|
|
1018
|
+
const obstaclesFor = (relation) => {
|
|
1019
|
+
const ends = [relation.from, relation.to];
|
|
1020
|
+
const holders = new Set(ends.flatMap((id) => [id, ...(diagram.elementsById.get(id)?.groupPath ?? [])]));
|
|
1021
|
+
const endBoxes = ends.flatMap((id) => boxes.get(id) ?? []);
|
|
1022
|
+
const layer = relation.layer ?? MAIN_LAYER;
|
|
1023
|
+
return shapeIds
|
|
1024
|
+
.filter((e) => !holders.has(e.id) && boxes.has(e.id) && (e.layer ?? MAIN_LAYER) === layer)
|
|
1025
|
+
.map((e) => extentAt(e.id, boxes.get(e.id)))
|
|
1026
|
+
.filter((box) => !endBoxes.some((end) => containsBox(box, end)));
|
|
1027
|
+
};
|
|
1028
|
+
/** The label each icon shape draws outside its box, as its own connectors meet it. */
|
|
1029
|
+
const ownLabels = new Map();
|
|
1030
|
+
for (const [id, box] of boxes) {
|
|
1031
|
+
const label = labelAt(id, box);
|
|
1032
|
+
if (!label || rotations.has(id))
|
|
1033
|
+
continue;
|
|
1034
|
+
ownLabels.set(id, {
|
|
1035
|
+
box: expandBox(label, TITLE_GAP, TITLE_GAP, TITLE_GAP, TITLE_GAP),
|
|
1036
|
+
side: LABEL_SIDES[sizing.labelSide(diagram.elementsById.get(id))],
|
|
1037
|
+
});
|
|
1038
|
+
}
|
|
1039
|
+
// Relations with labels sit a label apart, so their pills do not overlap.
|
|
1040
|
+
const laneSpacing = (relation) => relation.label !== null || relation.technology
|
|
1041
|
+
? scale.relationLabelHeight +
|
|
1042
|
+
(relation.technology ? Math.round(technologySize(scale.relationLabel) * 1.25) : 0) +
|
|
1043
|
+
4
|
|
1044
|
+
: LANE_SPACING;
|
|
1045
|
+
/** Labels placed by the layout, which may slide along their connectors to stay clear. */
|
|
1046
|
+
const movable = [];
|
|
1047
|
+
// Named ports and compass sides with a position are points of the box.
|
|
1048
|
+
const end = (element, endpoint) => {
|
|
1049
|
+
const port = endpoint?.port ?? "auto";
|
|
1050
|
+
const definition = endpoint?.named
|
|
1051
|
+
? diagram.elementsById.get(element)?.ports.find((p) => p.name === port)
|
|
1052
|
+
: undefined;
|
|
1053
|
+
const point = definition
|
|
1054
|
+
? { side: definition.side, position: definition.position }
|
|
1055
|
+
: endpoint?.position !== undefined && port !== "auto"
|
|
1056
|
+
? { side: port, position: endpoint.position }
|
|
1057
|
+
: null;
|
|
1058
|
+
const box = boxes.get(element);
|
|
1059
|
+
const degrees = rotations.get(element);
|
|
1060
|
+
if (degrees === undefined)
|
|
1061
|
+
return { box, port, named: point };
|
|
1062
|
+
// A turned element: free ends route to its drawn box; a named port, a pinned point or a
|
|
1063
|
+
// compass side is found on its own frame and turned with it.
|
|
1064
|
+
const drawn = turnedBoxes.get(element);
|
|
1065
|
+
const local = point ?? (SIDES.includes(port) ? { side: port, position: 0.5 } : null);
|
|
1066
|
+
if (!local)
|
|
1067
|
+
return { box: drawn, port: "auto", named: null };
|
|
1068
|
+
const own = diagram.elementsById.get(element);
|
|
1069
|
+
const shape = shapes.get(element);
|
|
1070
|
+
const atSide = portPoint(box, local.side, local.position);
|
|
1071
|
+
const onShape = isOutlined(shape)
|
|
1072
|
+
? onOutline(shape, box, atSide, local.side, sizing.shapeOptions(own))
|
|
1073
|
+
: atSide;
|
|
1074
|
+
const at = turnPoint(onShape, boxCenter(box), degrees);
|
|
1075
|
+
const normal = turnPoint(sideNormal(local.side), { x: 0, y: 0 }, degrees);
|
|
1076
|
+
const side = [...SIDES].sort((a, b) => {
|
|
1077
|
+
const na = sideNormal(a);
|
|
1078
|
+
const nb = sideNormal(b);
|
|
1079
|
+
return nb.x * normal.x + nb.y * normal.y - (na.x * normal.x + na.y * normal.y);
|
|
1080
|
+
})[0];
|
|
1081
|
+
const along = side === "north" || side === "south"
|
|
1082
|
+
? (at.x - drawn.x) / Math.max(1, drawn.width)
|
|
1083
|
+
: (at.y - drawn.y) / Math.max(1, drawn.height);
|
|
1084
|
+
return {
|
|
1085
|
+
box: drawn,
|
|
1086
|
+
port: side,
|
|
1087
|
+
named: { side, position: Math.min(1, Math.max(0, along)), point: at },
|
|
1088
|
+
};
|
|
1089
|
+
};
|
|
1090
|
+
const ignoreVia = arranging && mode === "auto";
|
|
1091
|
+
/**
|
|
1092
|
+
* An engine's route whose ends at icon shapes, on the sides of their extent, move along their
|
|
1093
|
+
* last segments onto the icon's square; undefined when an end cannot.
|
|
1094
|
+
*/
|
|
1095
|
+
const ontoIcons = (relation, route) => {
|
|
1096
|
+
let points = [...route];
|
|
1097
|
+
for (const [id, end] of [
|
|
1098
|
+
[relation.from, "first"],
|
|
1099
|
+
[relation.to, "last"],
|
|
1100
|
+
]) {
|
|
1101
|
+
if (!outside.has(id) || rotations.has(id))
|
|
1102
|
+
continue;
|
|
1103
|
+
const box = boxes.get(id);
|
|
1104
|
+
points = snapEnd(points, box, end);
|
|
1105
|
+
const tip = points[end === "first" ? 0 : points.length - 1];
|
|
1106
|
+
if (!SIDES.some((side) => onSide(box, tip, side, 0.5)))
|
|
1107
|
+
return undefined;
|
|
1108
|
+
}
|
|
1109
|
+
return points;
|
|
1110
|
+
};
|
|
1111
|
+
// Each relation's ends, waypoints and kind, and whether it follows the engine's route.
|
|
1112
|
+
const plans = diagram.relations.map((relation) => {
|
|
1113
|
+
const route = routes.get(relation.key);
|
|
1114
|
+
const style = styles.relation(relation);
|
|
1115
|
+
const kind = style.routing ?? diagram.properties.routing;
|
|
1116
|
+
const written = engineRoutes.get(relation.key);
|
|
1117
|
+
const engineRoute = written && (outside.has(relation.from) || outside.has(relation.to))
|
|
1118
|
+
? ontoIcons(relation, written)
|
|
1119
|
+
: written;
|
|
1120
|
+
const useEngine = engineRoute !== undefined &&
|
|
1121
|
+
engineRoute.length >= 2 &&
|
|
1122
|
+
!movedByConstraints.has(relation.from) &&
|
|
1123
|
+
!movedByConstraints.has(relation.to);
|
|
1124
|
+
return {
|
|
1125
|
+
relation,
|
|
1126
|
+
route,
|
|
1127
|
+
style,
|
|
1128
|
+
kind,
|
|
1129
|
+
fromEnd: end(relation.from, route?.from),
|
|
1130
|
+
toEnd: end(relation.to, route?.to),
|
|
1131
|
+
via: ignoreVia ? [] : (route?.via ?? []),
|
|
1132
|
+
engineRoute,
|
|
1133
|
+
// The engine's routes are orthogonal; straight and curved connectors join the anchors.
|
|
1134
|
+
followEngine: useEngine && (kind === "auto" || kind === "orthogonal"),
|
|
1135
|
+
};
|
|
1136
|
+
});
|
|
1137
|
+
// Free ends that share a side take lanes along it; parallel relations between the same two
|
|
1138
|
+
// boxes keep theirs straight and are not rerouted around obstacles.
|
|
1139
|
+
const lanes = endLanes(plans, laneSpacing);
|
|
1140
|
+
const parallel = parallelLanes(diagram, routes, laneSpacing);
|
|
1141
|
+
const relationGeometry = plans.map((plan, index) => {
|
|
1142
|
+
const { relation, style, kind, fromEnd, toEnd, via, engineRoute, followEngine } = plan;
|
|
1143
|
+
let anchors = anchorPoints(fromEnd, toEnd, via, lanes.get(relation.key) ?? 0);
|
|
1144
|
+
let points = followEngine ? [...engineRoute] : anchors.points;
|
|
1145
|
+
if (followEngine) {
|
|
1146
|
+
// Groups are drawn around their children, which can leave them smaller than the engine's
|
|
1147
|
+
// container: the route's ends move along their first and last segments onto the border.
|
|
1148
|
+
if (diagram.elementsById.get(relation.from)?.kind === "group") {
|
|
1149
|
+
points = snapEnd(points, boxes.get(relation.from), "first");
|
|
1150
|
+
}
|
|
1151
|
+
if (diagram.elementsById.get(relation.to)?.kind === "group") {
|
|
1152
|
+
points = snapEnd(points, boxes.get(relation.to), "last");
|
|
1153
|
+
}
|
|
1154
|
+
}
|
|
1155
|
+
else {
|
|
1156
|
+
points = shapeRoute(kind, anchors.points, { first: anchors.points[0], last: anchors.points[anchors.points.length - 1], via }, anchors.fromSide, anchors.toSide);
|
|
1157
|
+
}
|
|
1158
|
+
// The target's head is the style's or the theme's; the source's is the style's, else the
|
|
1159
|
+
// target's for a two-way arrow, else none.
|
|
1160
|
+
const endKind = style.arrowhead ?? theme.arrowhead;
|
|
1161
|
+
const startKind = style.arrowtail ?? (relation.twoWay ? endKind : "none");
|
|
1162
|
+
const headWidth = (theme.arrowSize * 7) / 8;
|
|
1163
|
+
// The labels icon shapes draw beside their ends: a connector passes under its own end's label
|
|
1164
|
+
// only straight from the icon and on beyond it, far enough for its head; elsewhere the label
|
|
1165
|
+
// is in its way as another shape is.
|
|
1166
|
+
const ownEnds = [
|
|
1167
|
+
relation.from === relation.to ? undefined : ownLabels.get(relation.from),
|
|
1168
|
+
relation.from === relation.to ? undefined : ownLabels.get(relation.to),
|
|
1169
|
+
];
|
|
1170
|
+
const rooms = [startKind, endKind].map((head) => markerReach(head, theme.arrowSize) + 2);
|
|
1171
|
+
const orthogonal = kind === "auto" || kind === "orthogonal" || followEngine;
|
|
1172
|
+
const own = (route) => ownLabelCrossings(route, ownEnds, rooms, orthogonal);
|
|
1173
|
+
// A route without waypoints that runs through another shape, or through its own end's label,
|
|
1174
|
+
// goes around it when it can: between side middles when both ends are free, else from its own
|
|
1175
|
+
// ends.
|
|
1176
|
+
const reroutable = (kind === "auto" || kind === "orthogonal") &&
|
|
1177
|
+
via.length === 0 &&
|
|
1178
|
+
(parallel.get(relation.key) ?? 0) === 0 &&
|
|
1179
|
+
points.length >= 2;
|
|
1180
|
+
if (reroutable) {
|
|
1181
|
+
const obstacles = obstaclesFor(relation);
|
|
1182
|
+
const crossings = (route) => crossedBoxes(route, obstacles).length + own(route);
|
|
1183
|
+
let crossed = crossings(points);
|
|
1184
|
+
// The engine's points carry no sides: they are read from the borders the ends lie on.
|
|
1185
|
+
let engineSides = followEngine;
|
|
1186
|
+
if (crossed > 0 && fromEnd.port === "auto" && toEnd.port === "auto") {
|
|
1187
|
+
const around = avoidingRoute(fromEnd.box, toEnd.box, obstacles, points, own);
|
|
1188
|
+
if (around) {
|
|
1189
|
+
points = around.points;
|
|
1190
|
+
anchors = { points: around.points, fromSide: around.fromSide, toSide: around.toSide };
|
|
1191
|
+
crossed = crossings(points);
|
|
1192
|
+
engineSides = false;
|
|
1193
|
+
}
|
|
1194
|
+
}
|
|
1195
|
+
if (crossed > 0) {
|
|
1196
|
+
const first = points[0];
|
|
1197
|
+
const last = points[points.length - 1];
|
|
1198
|
+
const fromSide = engineSides ? sideOfPoint(fromEnd.box, first) : anchors.fromSide;
|
|
1199
|
+
const toSide = engineSides ? sideOfPoint(toEnd.box, last) : anchors.toSide;
|
|
1200
|
+
// An end on its label's side leaves from beyond the label, the icon and the label one box.
|
|
1201
|
+
const detourEnd = (box, point, side, label) => {
|
|
1202
|
+
const normal = sideNormal(side);
|
|
1203
|
+
const away = { x: point.x + normal.x * 1e6, y: point.y + normal.y * 1e6 };
|
|
1204
|
+
const beyond = label?.side === side ? underLabel([point, away], "first", label, 0, true) : undefined;
|
|
1205
|
+
return beyond
|
|
1206
|
+
? { box: unionBoxes([box, label.box]), point: beyond, side }
|
|
1207
|
+
: { box, point, side };
|
|
1208
|
+
};
|
|
1209
|
+
const detour = detourRoute(detourEnd(fromEnd.box, first, fromSide, ownEnds[0]), detourEnd(toEnd.box, last, toSide, ownEnds[1]), [...obstacles, ...ownEnds.flatMap((label) => (label ? [label.box] : []))]);
|
|
1210
|
+
if (detour) {
|
|
1211
|
+
// The run from the icon to beyond its label is straight: the route starts at the icon.
|
|
1212
|
+
detour[0] = first;
|
|
1213
|
+
detour[detour.length - 1] = last;
|
|
1214
|
+
if (crossings(detour) < crossed) {
|
|
1215
|
+
points = detour;
|
|
1216
|
+
anchors = { points: detour, fromSide, toSide };
|
|
1217
|
+
}
|
|
1218
|
+
}
|
|
1219
|
+
}
|
|
1220
|
+
}
|
|
1221
|
+
// Every end was found on a side of its box; on a shaped box it moves onto the outline.
|
|
1222
|
+
points = [...points];
|
|
1223
|
+
points[0] = outlineEnd(relation.from, points[0], anchors.fromSide);
|
|
1224
|
+
points[points.length - 1] = outlineEnd(relation.to, points[points.length - 1], anchors.toSide);
|
|
1225
|
+
// A run under its own end's label is drawn from beyond the label, its head there, clear of the
|
|
1226
|
+
// text; `points` keeps the end on the icon for editors.
|
|
1227
|
+
const drawn = beyondOwnLabels(points, ownEnds, rooms, orthogonal);
|
|
1228
|
+
let line;
|
|
1229
|
+
let startHead;
|
|
1230
|
+
let endHead;
|
|
1231
|
+
let curve;
|
|
1232
|
+
if (kind === "bezier") {
|
|
1233
|
+
// Each head sits on the curve's tangent, which leaves and enters along the side normals.
|
|
1234
|
+
const approach = (tip, side) => {
|
|
1235
|
+
const normal = sideNormal(side);
|
|
1236
|
+
return [{ x: tip.x + normal.x * 10, y: tip.y + normal.y * 10 }, tip];
|
|
1237
|
+
};
|
|
1238
|
+
const atEnd = endMarker(approach(drawn[drawn.length - 1], anchors.toSide), endKind, theme.arrowSize, headWidth);
|
|
1239
|
+
const atStart = endMarker(approach(drawn[0], anchors.fromSide), startKind, theme.arrowSize, headWidth);
|
|
1240
|
+
const bezier = bezierThrough(drawn, anchors.fromSide, anchors.toSide, atEnd.line[atEnd.line.length - 1], atStart.line[atStart.line.length - 1]);
|
|
1241
|
+
line = bezier.samples;
|
|
1242
|
+
startHead = atStart.parts;
|
|
1243
|
+
endHead = atEnd.parts;
|
|
1244
|
+
curve = bezier.d;
|
|
1245
|
+
}
|
|
1246
|
+
else {
|
|
1247
|
+
const ends = relationEnds(drawn, startKind, endKind, theme.arrowSize, headWidth);
|
|
1248
|
+
line =
|
|
1249
|
+
(style.corners ?? theme.corners) === "rounded"
|
|
1250
|
+
? roundCorners(ends.line, CORNER_RADIUS)
|
|
1251
|
+
: ends.line;
|
|
1252
|
+
startHead = ends.start;
|
|
1253
|
+
endHead = ends.end;
|
|
1254
|
+
}
|
|
1255
|
+
let label;
|
|
1256
|
+
const icon = sizing.relationIcon(relation);
|
|
1257
|
+
const technology = relation.technology ? `[${relation.technology}]` : null;
|
|
1258
|
+
if (relation.label !== null || icon || technology) {
|
|
1259
|
+
const entry = labels.get(relation.key);
|
|
1260
|
+
const at = entry ? entry.at / 100 : 0.5;
|
|
1261
|
+
const offset = entry ? entry.offset : { x: 0, y: 0 };
|
|
1262
|
+
const center = pointAlong(curve ? line : drawn, at);
|
|
1263
|
+
const text = relation.label ?? "";
|
|
1264
|
+
const textWidth = text === "" ? 0 : metrics.width(text, "body", 500, scale.relationLabel);
|
|
1265
|
+
const iconWidth = icon ? icon.size + (text === "" ? 0 : icon.gap) : 0;
|
|
1266
|
+
// C4 relations show their technology on a second, smaller line.
|
|
1267
|
+
const techSize = technologySize(scale.relationLabel);
|
|
1268
|
+
const techWidth = technology ? metrics.width(technology, "body", 400, techSize) : 0;
|
|
1269
|
+
const width = Math.max(textWidth + iconWidth, techWidth) + scale.relationLabelPadding;
|
|
1270
|
+
const height = scale.relationLabelHeight + (technology ? Math.round(techSize * 1.25) : 0);
|
|
1271
|
+
if (!entry) {
|
|
1272
|
+
movable.push({
|
|
1273
|
+
index,
|
|
1274
|
+
along: curve ? line : drawn,
|
|
1275
|
+
width: Math.max(width, scale.relationLabelHeight),
|
|
1276
|
+
height,
|
|
1277
|
+
});
|
|
1278
|
+
}
|
|
1279
|
+
label = {
|
|
1280
|
+
box: boxFromCenter({ x: center.x + offset.x, y: center.y + offset.y }, Math.max(width, scale.relationLabelHeight), height),
|
|
1281
|
+
text,
|
|
1282
|
+
technology,
|
|
1283
|
+
};
|
|
1284
|
+
}
|
|
1285
|
+
const connector = {
|
|
1286
|
+
points,
|
|
1287
|
+
line,
|
|
1288
|
+
fromSide: anchors.fromSide,
|
|
1289
|
+
toSide: anchors.toSide,
|
|
1290
|
+
via,
|
|
1291
|
+
};
|
|
1292
|
+
return { relation, line, start: startHead, end: endHead, label, curve, connector };
|
|
1293
|
+
});
|
|
1294
|
+
// Every element's box with the label it draws outside it, in diagram coordinates.
|
|
1295
|
+
const extents = new Map([...boxes].map(([id, box]) => [id, extentAt(id, box)]));
|
|
1296
|
+
placeLabels(relationGeometry, movable, labelObstacles(diagram, extents, sizing));
|
|
1297
|
+
const elementLabels = new Map();
|
|
1298
|
+
for (const [id, box] of boxes) {
|
|
1299
|
+
const label = labelAt(id, box);
|
|
1300
|
+
// A turned label lies in its element's drawn box, which the checks compare.
|
|
1301
|
+
if (label && !rotations.has(id))
|
|
1302
|
+
elementLabels.set(id, label);
|
|
1303
|
+
}
|
|
1304
|
+
// Turned elements are checked by their drawn box.
|
|
1305
|
+
const checked = new Map([...boxes].map(([id, box]) => [id, extentAt(id, box)]));
|
|
1306
|
+
for (const [id, box] of boxes)
|
|
1307
|
+
if (!rotations.has(id))
|
|
1308
|
+
checked.set(id, box);
|
|
1309
|
+
diagnostics.push(...checkComposition(diagram, checked, relationGeometry.map((g) => ({
|
|
1310
|
+
relation: g.relation,
|
|
1311
|
+
points: g.line,
|
|
1312
|
+
curved: g.curve !== undefined,
|
|
1313
|
+
label: g.label?.box,
|
|
1314
|
+
})), explicitGroups, theme, (group) => sizing.untitled(group), outlines, elementLabels));
|
|
1315
|
+
const everything = [...extents.values()];
|
|
1316
|
+
for (const geometry of relationGeometry) {
|
|
1317
|
+
for (const p of [
|
|
1318
|
+
...geometry.line,
|
|
1319
|
+
...markerPoints(geometry.start),
|
|
1320
|
+
...markerPoints(geometry.end),
|
|
1321
|
+
])
|
|
1322
|
+
everything.push({ x: p.x, y: p.y, width: 0, height: 0 });
|
|
1323
|
+
if (geometry.label)
|
|
1324
|
+
everything.push(geometry.label.box);
|
|
1325
|
+
}
|
|
1326
|
+
const bounds = unionBoxes(everything) ?? { x: 0, y: 0, width: 0, height: 0 };
|
|
1327
|
+
const margin = theme.margin;
|
|
1328
|
+
const contentWidth = Math.ceil(bounds.width + 2 * margin);
|
|
1329
|
+
const contentHeight = Math.ceil(bounds.height + 2 * margin);
|
|
1330
|
+
const framed = theme.frame !== "none";
|
|
1331
|
+
const frameHeight = framed ? scale.frameHeight : 0;
|
|
1332
|
+
// The content canvas: the content and its margins, plus the frame strip.
|
|
1333
|
+
const naturalWidth = framed
|
|
1334
|
+
? Math.max(contentWidth, frameMinWidth(theme, scale, diagram.properties.title, metrics))
|
|
1335
|
+
: contentWidth;
|
|
1336
|
+
const naturalHeight = contentHeight + frameHeight;
|
|
1337
|
+
const canvas = canvasSize(diagram, naturalWidth, naturalHeight, lines, diagnostics);
|
|
1338
|
+
const width = canvas.width;
|
|
1339
|
+
const height = canvas.height;
|
|
1340
|
+
const frameTop = theme.frame === "header" ? frameHeight : 0;
|
|
1341
|
+
const dx = margin - bounds.x + Math.floor((width - contentWidth) / 2);
|
|
1342
|
+
const dy = margin - bounds.y + frameTop + Math.floor((height - frameHeight - contentHeight) / 2);
|
|
1343
|
+
const shift = (box) => ({
|
|
1344
|
+
x: box.x + dx,
|
|
1345
|
+
y: box.y + dy,
|
|
1346
|
+
width: box.width,
|
|
1347
|
+
height: box.height,
|
|
1348
|
+
});
|
|
1349
|
+
const shiftPoint = (p) => ({ x: p.x + dx, y: p.y + dy });
|
|
1350
|
+
const items = [];
|
|
1351
|
+
const gradients = [];
|
|
1352
|
+
const emitter = new Emitter(theme, scale, styles, metrics, sizing);
|
|
1353
|
+
const drawRelation = (geometry) => emitter.relation(geometry.relation, geometry.line.map(shiftPoint), shiftMarker(geometry.start, shiftPoint), shiftMarker(geometry.end, shiftPoint), geometry.label
|
|
1354
|
+
? {
|
|
1355
|
+
box: shift(geometry.label.box),
|
|
1356
|
+
text: geometry.label.text,
|
|
1357
|
+
technology: geometry.label.technology,
|
|
1358
|
+
}
|
|
1359
|
+
: undefined, geometry.curve ? translatePath(geometry.curve, dx, dy) : undefined);
|
|
1360
|
+
// Groups of elements and relations carry their layer, so hosts can address it.
|
|
1361
|
+
const onLayer = (group, layer) => layer !== null && layer !== MAIN_LAYER ? { ...group, layer } : group;
|
|
1362
|
+
/** A turned element's group, turned about the centre of its box in scene coordinates. */
|
|
1363
|
+
const turn = (group, box) => {
|
|
1364
|
+
const degrees = rotations.get(group.id);
|
|
1365
|
+
if (degrees === undefined)
|
|
1366
|
+
return group;
|
|
1367
|
+
const c = boxCenter(box);
|
|
1368
|
+
return { ...group, transform: `rotate(${round(degrees)} ${round(c.x)} ${round(c.y)})` };
|
|
1369
|
+
};
|
|
1370
|
+
if (free) {
|
|
1371
|
+
const byKey = new Map(relationGeometry.map((g) => [g.relation.key, g]));
|
|
1372
|
+
for (const paintable of paintOrder(diagram)) {
|
|
1373
|
+
if (paintable.kind === "relation") {
|
|
1374
|
+
const geometry = byKey.get(paintable.relation.key);
|
|
1375
|
+
if (geometry)
|
|
1376
|
+
items.push(onLayer(drawRelation(geometry), paintable.relation.layer));
|
|
1377
|
+
continue;
|
|
1378
|
+
}
|
|
1379
|
+
const box = shift(boxes.get(paintable.element.id));
|
|
1380
|
+
items.push(onLayer(paintable.kind === "group"
|
|
1381
|
+
? emitter.group(paintable.element, box)
|
|
1382
|
+
: turn(emitter.element(paintable.element, box), box), paintable.element.layer));
|
|
1383
|
+
}
|
|
1384
|
+
}
|
|
1385
|
+
else {
|
|
1386
|
+
// Layer by layer, back to front; within a layer: groups (outer before inner), the other
|
|
1387
|
+
// elements in source order, relations, and tags on top.
|
|
1388
|
+
const layers = diagram.properties.layers;
|
|
1389
|
+
const main = layers.indexOf(MAIN_LAYER);
|
|
1390
|
+
const indexOf = (layer) => {
|
|
1391
|
+
const i = layer === null ? main : layers.indexOf(layer);
|
|
1392
|
+
return i < 0 ? main : i;
|
|
1393
|
+
};
|
|
1394
|
+
const sortedGroups = [...groups].sort((a, b) => a.groupPath.length - b.groupPath.length);
|
|
1395
|
+
for (let layer = 0; layer < layers.length; layer++) {
|
|
1396
|
+
for (const group of sortedGroups) {
|
|
1397
|
+
if (indexOf(group.layer) !== layer)
|
|
1398
|
+
continue;
|
|
1399
|
+
items.push(onLayer(emitter.group(group, shift(boxes.get(group.id))), group.layer));
|
|
1400
|
+
}
|
|
1401
|
+
for (const element of diagram.elements) {
|
|
1402
|
+
if (element.kind === "group" || element.kind === "tag" || indexOf(element.layer) !== layer)
|
|
1403
|
+
continue;
|
|
1404
|
+
const box = shift(boxes.get(element.id));
|
|
1405
|
+
items.push(onLayer(turn(emitter.element(element, box), box), element.layer));
|
|
1406
|
+
}
|
|
1407
|
+
for (const geometry of relationGeometry) {
|
|
1408
|
+
if (indexOf(geometry.relation.layer) !== layer)
|
|
1409
|
+
continue;
|
|
1410
|
+
items.push(onLayer(drawRelation(geometry), geometry.relation.layer));
|
|
1411
|
+
}
|
|
1412
|
+
for (const element of diagram.elements) {
|
|
1413
|
+
if (element.kind !== "tag" || indexOf(element.layer) !== layer)
|
|
1414
|
+
continue;
|
|
1415
|
+
const box = shift(boxes.get(element.id));
|
|
1416
|
+
items.push(onLayer(turn(emitter.element(element, box), box), element.layer));
|
|
1417
|
+
}
|
|
1418
|
+
}
|
|
1419
|
+
}
|
|
1420
|
+
if (framed) {
|
|
1421
|
+
const stripTop = theme.frame === "header" ? 0 : height - frameHeight;
|
|
1422
|
+
items.push(emitter.frame(width, stripTop, diagram.properties.title));
|
|
1423
|
+
if (theme.mark === "meltemi")
|
|
1424
|
+
gradients.push(markGradient(theme.accent, theme.accentEnd));
|
|
1425
|
+
}
|
|
1426
|
+
if (metrics.unknownFonts.size > 0 || metrics.uncovered.size > 0) {
|
|
1427
|
+
diagnostics.push({
|
|
1428
|
+
code: "layout.unmeasured-font",
|
|
1429
|
+
severity: "info",
|
|
1430
|
+
message: words.diagnostics.unmeasuredFont([...metrics.unknownFonts], [...metrics.uncovered]),
|
|
1431
|
+
range: diagram.idRange,
|
|
1432
|
+
hints: [words.hints.bundledFonts],
|
|
1433
|
+
});
|
|
1434
|
+
}
|
|
1435
|
+
const scene = {
|
|
1436
|
+
width,
|
|
1437
|
+
height,
|
|
1438
|
+
background: theme.background,
|
|
1439
|
+
title: diagram.properties.title,
|
|
1440
|
+
description: diagram.properties.description,
|
|
1441
|
+
fonts: { body: theme.font, mono: theme.mono },
|
|
1442
|
+
gradients: [...gradients, ...emitter.painter.gradients],
|
|
1443
|
+
patterns: emitter.patterns,
|
|
1444
|
+
items,
|
|
1445
|
+
};
|
|
1446
|
+
const labelBoxes = new Map();
|
|
1447
|
+
const connectors = new Map();
|
|
1448
|
+
for (const geometry of relationGeometry) {
|
|
1449
|
+
if (geometry.label)
|
|
1450
|
+
labelBoxes.set(geometry.relation.key, geometry.label.box);
|
|
1451
|
+
connectors.set(geometry.relation.key, geometry.connector);
|
|
1452
|
+
}
|
|
1453
|
+
return {
|
|
1454
|
+
scene,
|
|
1455
|
+
diagnostics,
|
|
1456
|
+
boxes,
|
|
1457
|
+
offset: { x: dx, y: dy },
|
|
1458
|
+
labels: labelBoxes,
|
|
1459
|
+
connectors,
|
|
1460
|
+
origins,
|
|
1461
|
+
theme,
|
|
1462
|
+
shapes,
|
|
1463
|
+
elementLabels,
|
|
1464
|
+
rotations,
|
|
1465
|
+
radii: new Map([...shapes]
|
|
1466
|
+
.map(([id, shape]) => {
|
|
1467
|
+
const element = diagram.elementsById.get(id);
|
|
1468
|
+
const radius = CORNERED_SHAPES.has(shape)
|
|
1469
|
+
? sizing.shapeOptions(element).radius
|
|
1470
|
+
: undefined;
|
|
1471
|
+
return [id, radius];
|
|
1472
|
+
})
|
|
1473
|
+
.filter((entry) => entry[1] !== undefined)),
|
|
1474
|
+
};
|
|
1475
|
+
}
|
|
1476
|
+
/** The side of an icon shape each label position lies on. */
|
|
1477
|
+
const LABEL_SIDES = {
|
|
1478
|
+
top: "north",
|
|
1479
|
+
bottom: "south",
|
|
1480
|
+
left: "west",
|
|
1481
|
+
right: "east",
|
|
1482
|
+
};
|
|
1483
|
+
/**
|
|
1484
|
+
* Where a connector's end at an icon shape comes out from under the shape's own label: when its
|
|
1485
|
+
* last run leaves the icon on the label's side, its line meets the label's text, and it reaches on
|
|
1486
|
+
* beyond the label by at least `room` (the end's head) before it turns, the point of that run just
|
|
1487
|
+
* beyond the label; else undefined. `straight` asks the run to follow the side's normal, as the
|
|
1488
|
+
* runs of orthogonal routes do; other routes only have to head away from the icon.
|
|
1489
|
+
*/
|
|
1490
|
+
function underLabel(points, end, label, room, straight) {
|
|
1491
|
+
if (points.length < 2)
|
|
1492
|
+
return undefined;
|
|
1493
|
+
const tip = points[end === "first" ? 0 : points.length - 1];
|
|
1494
|
+
const next = points[end === "first" ? 1 : points.length - 2];
|
|
1495
|
+
const normal = sideNormal(label.side);
|
|
1496
|
+
const across = normal.x !== 0;
|
|
1497
|
+
const { box } = label;
|
|
1498
|
+
if (straight && Math.abs(across ? next.y - tip.y : next.x - tip.x) > 0.5)
|
|
1499
|
+
return undefined;
|
|
1500
|
+
const at = across ? tip.y : tip.x;
|
|
1501
|
+
const [low, high] = across ? [box.y, box.y + box.height] : [box.x, box.x + box.width];
|
|
1502
|
+
if (at <= low || at >= high)
|
|
1503
|
+
return undefined;
|
|
1504
|
+
// Distances along the side's normal, outwards from the icon.
|
|
1505
|
+
const along = (p) => (across ? p.x * normal.x : p.y * normal.y);
|
|
1506
|
+
const far = label.side === "east"
|
|
1507
|
+
? box.x + box.width
|
|
1508
|
+
: label.side === "west"
|
|
1509
|
+
? box.x
|
|
1510
|
+
: label.side === "south"
|
|
1511
|
+
? box.y + box.height
|
|
1512
|
+
: box.y;
|
|
1513
|
+
const beyond = across ? { x: far, y: tip.y } : { x: tip.x, y: far };
|
|
1514
|
+
if (along(tip) >= along(beyond) || along(next) < along(beyond) + room)
|
|
1515
|
+
return undefined;
|
|
1516
|
+
return beyond;
|
|
1517
|
+
}
|
|
1518
|
+
/**
|
|
1519
|
+
* A connector's route as it is drawn: each end whose last run passes under its own label
|
|
1520
|
+
* (`underLabel`) starts beyond the label instead of on the icon.
|
|
1521
|
+
*/
|
|
1522
|
+
function beyondOwnLabels(points, labels, rooms, straight) {
|
|
1523
|
+
const route = [...points];
|
|
1524
|
+
for (const [index, end] of [
|
|
1525
|
+
[0, "first"],
|
|
1526
|
+
[1, "last"],
|
|
1527
|
+
]) {
|
|
1528
|
+
const label = labels[index];
|
|
1529
|
+
// The other end, already drawn from beyond its own label, is where this run goes on to.
|
|
1530
|
+
const beyond = label && underLabel(route, end, label, rooms[index], straight);
|
|
1531
|
+
if (beyond)
|
|
1532
|
+
route[end === "first" ? 0 : route.length - 1] = beyond;
|
|
1533
|
+
}
|
|
1534
|
+
return route;
|
|
1535
|
+
}
|
|
1536
|
+
/**
|
|
1537
|
+
* How many of the labels of a connector's own ends its route crosses, other than by a run that
|
|
1538
|
+
* passes under the label from the icon and beyond it (`underLabel`).
|
|
1539
|
+
*/
|
|
1540
|
+
function ownLabelCrossings(points, labels, rooms, straight) {
|
|
1541
|
+
const route = beyondOwnLabels(points, labels, rooms, straight);
|
|
1542
|
+
return labels.filter((label) => label && crossedBoxes(route, [label.box]).length > 0).length;
|
|
1543
|
+
}
|
|
1544
|
+
function round(value) {
|
|
1545
|
+
return Math.round(value * 100) / 100;
|
|
1546
|
+
}
|
|
1547
|
+
/** Moves path data made of absolute coordinate pairs (`M` and `C` commands) by (dx, dy). */
|
|
1548
|
+
/** The group of an element with the element's `url`, when it has one. */
|
|
1549
|
+
export function withUrl(group, element) {
|
|
1550
|
+
return element.url ? { ...group, href: element.url } : group;
|
|
1551
|
+
}
|
|
1552
|
+
/**
|
|
1553
|
+
* The group with the box it was drawn in, for motion effects. The corner radius is the one of the
|
|
1554
|
+
* outline drawn on that box, when there is one. `inner` is the inscribed rectangle the text of a
|
|
1555
|
+
* shape other than a box lies in, and `label` the box of a label drawn outside it, for the label
|
|
1556
|
+
* audit.
|
|
1557
|
+
*/
|
|
1558
|
+
export function withBounds(group, box, inner, label) {
|
|
1559
|
+
const outline = group.items.find((item) => item.kind === "rect" &&
|
|
1560
|
+
Math.abs(item.x - box.x) < 0.5 &&
|
|
1561
|
+
Math.abs(item.y - box.y) < 0.5 &&
|
|
1562
|
+
Math.abs(item.width - box.width) < 0.5 &&
|
|
1563
|
+
Math.abs(item.height - box.height) < 0.5);
|
|
1564
|
+
return {
|
|
1565
|
+
...group,
|
|
1566
|
+
// Shapes drawn as paths (cylinders, diamonds, persons) get a softly rounded halo.
|
|
1567
|
+
bounds: {
|
|
1568
|
+
x: box.x,
|
|
1569
|
+
y: box.y,
|
|
1570
|
+
width: box.width,
|
|
1571
|
+
height: box.height,
|
|
1572
|
+
radius: outline?.radius ?? 8,
|
|
1573
|
+
...(inner
|
|
1574
|
+
? {
|
|
1575
|
+
inner: {
|
|
1576
|
+
x: round(inner.x),
|
|
1577
|
+
y: round(inner.y),
|
|
1578
|
+
width: round(inner.width),
|
|
1579
|
+
height: round(inner.height),
|
|
1580
|
+
},
|
|
1581
|
+
}
|
|
1582
|
+
: {}),
|
|
1583
|
+
...(label
|
|
1584
|
+
? {
|
|
1585
|
+
label: {
|
|
1586
|
+
x: round(label.x),
|
|
1587
|
+
y: round(label.y),
|
|
1588
|
+
width: round(label.width),
|
|
1589
|
+
height: round(label.height),
|
|
1590
|
+
},
|
|
1591
|
+
}
|
|
1592
|
+
: {}),
|
|
1593
|
+
},
|
|
1594
|
+
};
|
|
1595
|
+
}
|
|
1596
|
+
function translatePath(d, dx, dy) {
|
|
1597
|
+
let index = 0;
|
|
1598
|
+
return d.replace(/-?\d+(?:\.\d+)?/g, (number) => {
|
|
1599
|
+
const value = Number(number) + (index++ % 2 === 0 ? dx : dy);
|
|
1600
|
+
const rounded = Math.round(value * 100) / 100;
|
|
1601
|
+
return String(Object.is(rounded, -0) ? 0 : rounded);
|
|
1602
|
+
});
|
|
1603
|
+
}
|
|
1604
|
+
/** The size of a relation's label pill without its icon, or null when it has no label. */
|
|
1605
|
+
function labelSize(relation, metrics, scale) {
|
|
1606
|
+
const technology = relation.technology ? `[${relation.technology}]` : null;
|
|
1607
|
+
if (relation.label === null && !technology)
|
|
1608
|
+
return null;
|
|
1609
|
+
const techSize = technologySize(scale.relationLabel);
|
|
1610
|
+
const text = relation.label ? metrics.width(relation.label, "body", 500, scale.relationLabel) : 0;
|
|
1611
|
+
const tech = technology ? metrics.width(technology, "body", 400, techSize) : 0;
|
|
1612
|
+
return {
|
|
1613
|
+
width: Math.ceil(Math.max(text, tech) + scale.relationLabelPadding),
|
|
1614
|
+
height: scale.relationLabelHeight + (technology ? Math.round(techSize * 1.25) : 0),
|
|
1615
|
+
};
|
|
1616
|
+
}
|
|
1617
|
+
/** The engine's port of a compass end: its side, or its side and position (`east@25`). */
|
|
1618
|
+
function enginePort(end) {
|
|
1619
|
+
return end.position === undefined
|
|
1620
|
+
? end.port
|
|
1621
|
+
: `${end.port}@${Math.round(end.position * 1000) / 10}`;
|
|
1622
|
+
}
|
|
1623
|
+
/** The sides where relations enter and leave a box, by direction, as engines arrange them. */
|
|
1624
|
+
const FLOW_SIDES = {
|
|
1625
|
+
LR: { in: "west", out: "east" },
|
|
1626
|
+
RL: { in: "east", out: "west" },
|
|
1627
|
+
TB: { in: "north", out: "south" },
|
|
1628
|
+
BT: { in: "south", out: "north" },
|
|
1629
|
+
};
|
|
1630
|
+
/**
|
|
1631
|
+
* The request an engine arranges. `iconExtents` holds, for each element drawn beyond its box (an
|
|
1632
|
+
* icon shape and its label), the box's size and the extent the engine places, relative to the
|
|
1633
|
+
* box's top-left corner: ports written on the box are moved onto the extent's sides, and the
|
|
1634
|
+
* relation ends that name no port get one each on the icon's sides, spread over the icon, so that
|
|
1635
|
+
* the routes meet the icon and not its label: on the side the flow leaves by for a source and
|
|
1636
|
+
* enters by for a target, or the other way round for the relations in `mirrored`, which run
|
|
1637
|
+
* against the flow.
|
|
1638
|
+
*/
|
|
1639
|
+
function arrangeRequest(diagram, sizes, fixedPlaces, routes, theme, headerOf, labelOf = () => null, leftOut = new Set(), minimums = new Map(), iconExtents = new Map(), mirrored = new Set()) {
|
|
1640
|
+
/** A position along a side of an element's box, as a position along its extent's side. */
|
|
1641
|
+
const along = (id, side, position) => {
|
|
1642
|
+
const known = iconExtents.get(id);
|
|
1643
|
+
if (!known)
|
|
1644
|
+
return position;
|
|
1645
|
+
const { size, extent } = known;
|
|
1646
|
+
return side === "north" || side === "south"
|
|
1647
|
+
? (size.width * position - extent.x) / extent.width
|
|
1648
|
+
: (size.height * position - extent.y) / extent.height;
|
|
1649
|
+
};
|
|
1650
|
+
const compassPorts = new Map();
|
|
1651
|
+
for (const route of routes.values()) {
|
|
1652
|
+
for (const end of [route.from, route.to]) {
|
|
1653
|
+
if (end.named || end.port === "auto")
|
|
1654
|
+
continue;
|
|
1655
|
+
const set = compassPorts.get(end.element) ?? new Set();
|
|
1656
|
+
set.add(enginePort(end));
|
|
1657
|
+
compassPorts.set(end.element, set);
|
|
1658
|
+
}
|
|
1659
|
+
}
|
|
1660
|
+
// The relation ends at an icon shape that name no port, by element and side.
|
|
1661
|
+
const flow = FLOW_SIDES[diagram.properties.direction] ?? FLOW_SIDES.LR;
|
|
1662
|
+
const iconEnds = new Map();
|
|
1663
|
+
const iconPort = (relation, end) => {
|
|
1664
|
+
const element = relation[end];
|
|
1665
|
+
if (!iconExtents.has(element))
|
|
1666
|
+
return null;
|
|
1667
|
+
const side = end === "from" || mirrored.has(relation.key) ? flow.out : flow.in;
|
|
1668
|
+
const id = `\u0002${relation.key}\u0002${end}\u0002${side}`;
|
|
1669
|
+
const key = `${element}\u0000${side}`;
|
|
1670
|
+
iconEnds.set(key, [...(iconEnds.get(key) ?? []), id]);
|
|
1671
|
+
return id;
|
|
1672
|
+
};
|
|
1673
|
+
const edges = diagram.relations.flatMap((relation) => {
|
|
1674
|
+
if (leftOut.has(relation.from) || leftOut.has(relation.to))
|
|
1675
|
+
return [];
|
|
1676
|
+
const route = routes.get(relation.key);
|
|
1677
|
+
const port = (end, which) => end && end.port !== "auto"
|
|
1678
|
+
? end.named
|
|
1679
|
+
? end.port
|
|
1680
|
+
: enginePort(end)
|
|
1681
|
+
: iconPort(relation, which);
|
|
1682
|
+
return [
|
|
1683
|
+
{
|
|
1684
|
+
id: relation.key,
|
|
1685
|
+
source: relation.from,
|
|
1686
|
+
target: relation.to,
|
|
1687
|
+
sourcePort: port(route?.from, "from"),
|
|
1688
|
+
targetPort: port(route?.to, "to"),
|
|
1689
|
+
primary: relation.line === "solid",
|
|
1690
|
+
label: labelOf(relation),
|
|
1691
|
+
},
|
|
1692
|
+
];
|
|
1693
|
+
});
|
|
1694
|
+
const nodes = diagram.elements
|
|
1695
|
+
.filter((e) => e.kind !== "group")
|
|
1696
|
+
.map((element) => {
|
|
1697
|
+
const ports = element.ports.map((p) => ({
|
|
1698
|
+
id: p.name,
|
|
1699
|
+
side: p.side,
|
|
1700
|
+
position: along(element.id, p.side, p.position),
|
|
1701
|
+
}));
|
|
1702
|
+
for (const id of compassPorts.get(element.id) ?? []) {
|
|
1703
|
+
const [side, percent] = id.split("@");
|
|
1704
|
+
ports.push({
|
|
1705
|
+
id,
|
|
1706
|
+
side,
|
|
1707
|
+
position: along(element.id, side, percent === undefined ? 0.5 : Number(percent) / 100),
|
|
1708
|
+
});
|
|
1709
|
+
}
|
|
1710
|
+
for (const side of iconExtents.has(element.id) ? [flow.in, flow.out] : []) {
|
|
1711
|
+
const ids = iconEnds.get(`${element.id}\u0000${side}`) ?? [];
|
|
1712
|
+
ids.forEach((id, i) => {
|
|
1713
|
+
ports.push({ id, side, position: along(element.id, side, (i + 1) / (ids.length + 1)) });
|
|
1714
|
+
});
|
|
1715
|
+
}
|
|
1716
|
+
const size = sizes.get(element.id);
|
|
1717
|
+
return {
|
|
1718
|
+
id: element.id,
|
|
1719
|
+
width: size.width,
|
|
1720
|
+
height: size.height,
|
|
1721
|
+
kind: element.kind,
|
|
1722
|
+
parent: parentOf(element),
|
|
1723
|
+
fixed: fixedPlaces.get(element.id) ?? null,
|
|
1724
|
+
ports,
|
|
1725
|
+
};
|
|
1726
|
+
});
|
|
1727
|
+
const groups = diagram.elements
|
|
1728
|
+
.filter((e) => e.kind === "group" && !leftOut.has(e.id))
|
|
1729
|
+
.map((group) => {
|
|
1730
|
+
const minimum = minimums.get(group.id);
|
|
1731
|
+
return {
|
|
1732
|
+
id: group.id,
|
|
1733
|
+
parent: parentOf(group),
|
|
1734
|
+
padding: {
|
|
1735
|
+
top: headerOf(group),
|
|
1736
|
+
right: theme.groupPadding,
|
|
1737
|
+
bottom: theme.groupPadding,
|
|
1738
|
+
left: theme.groupPadding,
|
|
1739
|
+
},
|
|
1740
|
+
...(minimum ? { minimum } : {}),
|
|
1741
|
+
};
|
|
1742
|
+
});
|
|
1743
|
+
return {
|
|
1744
|
+
direction: diagram.properties.direction,
|
|
1745
|
+
...(diagram.definition?.layout.algorithm === "tree" ? { algorithm: "tree" } : {}),
|
|
1746
|
+
nodes,
|
|
1747
|
+
groups,
|
|
1748
|
+
edges,
|
|
1749
|
+
spacing: ENGINE_SPACING,
|
|
1750
|
+
};
|
|
1751
|
+
}
|
|
1752
|
+
/**
|
|
1753
|
+
* The relations with an end at an icon shape that an arrangement turns back against the flow:
|
|
1754
|
+
* their target lies wholly behind their source along the direction.
|
|
1755
|
+
*/
|
|
1756
|
+
function backwardAtIcons(diagram, arrangement, sizes, iconExtents, leftOut, routes) {
|
|
1757
|
+
const direction = diagram.properties.direction;
|
|
1758
|
+
const horizontal = direction !== "TB" && direction !== "BT";
|
|
1759
|
+
const sign = direction === "RL" || direction === "BT" ? -1 : 1;
|
|
1760
|
+
// Only the ends that name no port get the ports of icon shapes.
|
|
1761
|
+
const free = (id, end) => iconExtents.has(id) && (end === undefined || end.port === "auto");
|
|
1762
|
+
const out = new Set();
|
|
1763
|
+
for (const relation of diagram.relations) {
|
|
1764
|
+
const route = routes.get(relation.key);
|
|
1765
|
+
if (!free(relation.from, route?.from) && !free(relation.to, route?.to))
|
|
1766
|
+
continue;
|
|
1767
|
+
if (leftOut.has(relation.from) || leftOut.has(relation.to))
|
|
1768
|
+
continue;
|
|
1769
|
+
const from = arrangement.centers.get(relation.from);
|
|
1770
|
+
const to = arrangement.centers.get(relation.to);
|
|
1771
|
+
const fromSize = sizes.get(relation.from);
|
|
1772
|
+
const toSize = sizes.get(relation.to);
|
|
1773
|
+
if (!from || !to || !fromSize || !toSize)
|
|
1774
|
+
continue;
|
|
1775
|
+
const along = (p) => sign * (horizontal ? p.x : p.y);
|
|
1776
|
+
const half = (size) => (horizontal ? size.width : size.height) / 2;
|
|
1777
|
+
if (along(to) + half(toSize) < along(from) - half(fromSize))
|
|
1778
|
+
out.add(relation.key);
|
|
1779
|
+
}
|
|
1780
|
+
return out;
|
|
1781
|
+
}
|
|
1782
|
+
/** The length of every route of an arrangement, together. */
|
|
1783
|
+
function routesLength(arrangement) {
|
|
1784
|
+
let total = 0;
|
|
1785
|
+
for (const route of arrangement.routes.values()) {
|
|
1786
|
+
for (let i = 1; i < route.length; i++) {
|
|
1787
|
+
total += Math.hypot(route[i].x - route[i - 1].x, route[i].y - route[i - 1].y);
|
|
1788
|
+
}
|
|
1789
|
+
}
|
|
1790
|
+
return total;
|
|
1791
|
+
}
|
|
1792
|
+
/**
|
|
1793
|
+
* `assisted` positions: placed elements keep their place. Each other element keeps its offset in
|
|
1794
|
+
* the arrangement from the placed element nearest to it there, and is then pushed away from that
|
|
1795
|
+
* element, along the layout direction, until it no longer overlaps a box already positioned. Boxes
|
|
1796
|
+
* are compared with what they draw beyond them, `extentOf` (the label of an icon shape).
|
|
1797
|
+
*/
|
|
1798
|
+
function placeAround(diagram, arrangement, places, sizes, centers, fixed, extentOf = (_id, box) => box) {
|
|
1799
|
+
const direction = diagram.properties.direction;
|
|
1800
|
+
const horizontal = direction === "LR" || direction === "RL";
|
|
1801
|
+
const anchors = [...places.keys()].filter((id) => sizes.has(id) && arrangement.centers.has(id));
|
|
1802
|
+
const positioned = [];
|
|
1803
|
+
for (const element of diagram.elements) {
|
|
1804
|
+
if (element.kind === "group" || !places.has(element.id))
|
|
1805
|
+
continue;
|
|
1806
|
+
const size = sizes.get(element.id);
|
|
1807
|
+
centers.set(element.id, places.get(element.id));
|
|
1808
|
+
fixed.add(element.id);
|
|
1809
|
+
positioned.push(extentOf(element.id, boxFromCenter(places.get(element.id), size.width, size.height)));
|
|
1810
|
+
}
|
|
1811
|
+
for (const element of diagram.elements) {
|
|
1812
|
+
if (element.kind === "group" || places.has(element.id))
|
|
1813
|
+
continue;
|
|
1814
|
+
const size = sizes.get(element.id);
|
|
1815
|
+
const arranged = arrangement.centers.get(element.id) ?? { x: 0, y: 0 };
|
|
1816
|
+
let nearest;
|
|
1817
|
+
let best = Infinity;
|
|
1818
|
+
for (const id of anchors) {
|
|
1819
|
+
const at = arrangement.centers.get(id);
|
|
1820
|
+
const distance = Math.hypot(at.x - arranged.x, at.y - arranged.y);
|
|
1821
|
+
if (distance < best) {
|
|
1822
|
+
best = distance;
|
|
1823
|
+
nearest = id;
|
|
1824
|
+
}
|
|
1825
|
+
}
|
|
1826
|
+
let center = arranged;
|
|
1827
|
+
let sign = direction === "RL" || direction === "BT" ? -1 : 1;
|
|
1828
|
+
if (nearest) {
|
|
1829
|
+
const from = arrangement.centers.get(nearest);
|
|
1830
|
+
const to = places.get(nearest);
|
|
1831
|
+
center = { x: to.x + arranged.x - from.x, y: to.y + arranged.y - from.y };
|
|
1832
|
+
const delta = horizontal ? arranged.x - from.x : arranged.y - from.y;
|
|
1833
|
+
if (delta !== 0)
|
|
1834
|
+
sign = delta > 0 ? 1 : -1;
|
|
1835
|
+
}
|
|
1836
|
+
for (let attempt = 0; attempt < 100; attempt++) {
|
|
1837
|
+
const box = extentOf(element.id, boxFromCenter(center, size.width, size.height));
|
|
1838
|
+
const blocker = positioned.find((other) => box.x < other.x + other.width &&
|
|
1839
|
+
other.x < box.x + box.width &&
|
|
1840
|
+
box.y < other.y + other.height &&
|
|
1841
|
+
other.y < box.y + box.height);
|
|
1842
|
+
if (!blocker)
|
|
1843
|
+
break;
|
|
1844
|
+
const step = horizontal
|
|
1845
|
+
? sign > 0
|
|
1846
|
+
? blocker.x + blocker.width - box.x + ENGINE_SPACING.node
|
|
1847
|
+
: box.x + box.width - blocker.x + ENGINE_SPACING.node
|
|
1848
|
+
: sign > 0
|
|
1849
|
+
? blocker.y + blocker.height - box.y + ENGINE_SPACING.node
|
|
1850
|
+
: box.y + box.height - blocker.y + ENGINE_SPACING.node;
|
|
1851
|
+
center = horizontal
|
|
1852
|
+
? { x: center.x + sign * step, y: center.y }
|
|
1853
|
+
: { x: center.x, y: center.y + sign * step };
|
|
1854
|
+
}
|
|
1855
|
+
centers.set(element.id, center);
|
|
1856
|
+
positioned.push(extentOf(element.id, boxFromCenter(center, size.width, size.height)));
|
|
1857
|
+
}
|
|
1858
|
+
}
|
|
1859
|
+
/**
|
|
1860
|
+
* Lane offsets of the free ends (no named port, no point of a side) of relations drawn by the
|
|
1861
|
+
* anchor rules, without waypoints. Ends that meet the same side of the same box less than a lane
|
|
1862
|
+
* apart are spread a lane apart about the middle of their anchors, in the order, along the side,
|
|
1863
|
+
* of each relation's other anchor (then source order), so their connectors do not cross; a lane is
|
|
1864
|
+
* the widest spacing of their relations. A straight connector whose other end has no lane moves
|
|
1865
|
+
* that end too, so it stays straight. Ends already a lane apart keep their anchors.
|
|
1866
|
+
*/
|
|
1867
|
+
function endLanes(plans, spacingOf) {
|
|
1868
|
+
const sides = new Map();
|
|
1869
|
+
const straight = new Set();
|
|
1870
|
+
const coordinate = (side, point) => side === "north" || side === "south" ? point.x : point.y;
|
|
1871
|
+
plans.forEach((plan, order) => {
|
|
1872
|
+
const { relation, fromEnd, toEnd, via, followEngine } = plan;
|
|
1873
|
+
if (relation.from === relation.to || via.length > 0 || followEngine)
|
|
1874
|
+
return;
|
|
1875
|
+
if (fromEnd.named && toEnd.named)
|
|
1876
|
+
return;
|
|
1877
|
+
const { points, fromSide, toSide } = anchorPoints(fromEnd, toEnd, via, 0);
|
|
1878
|
+
const first = points[0];
|
|
1879
|
+
const last = points[points.length - 1];
|
|
1880
|
+
const add = (element, side, end, own, other) => {
|
|
1881
|
+
const key = `${element}\u0000${side}`;
|
|
1882
|
+
const list = sides.get(key) ?? [];
|
|
1883
|
+
list.push({
|
|
1884
|
+
relation,
|
|
1885
|
+
end,
|
|
1886
|
+
at: coordinate(side, own),
|
|
1887
|
+
along: coordinate(side, other),
|
|
1888
|
+
order,
|
|
1889
|
+
});
|
|
1890
|
+
sides.set(key, list);
|
|
1891
|
+
};
|
|
1892
|
+
if (!fromEnd.named)
|
|
1893
|
+
add(relation.from, fromSide, "from", first, last);
|
|
1894
|
+
if (!toEnd.named)
|
|
1895
|
+
add(relation.to, toSide, "to", last, first);
|
|
1896
|
+
if (points.length === 2 &&
|
|
1897
|
+
!fromEnd.named &&
|
|
1898
|
+
!toEnd.named &&
|
|
1899
|
+
(Math.abs(first.x - last.x) < 0.01 || Math.abs(first.y - last.y) < 0.01)) {
|
|
1900
|
+
straight.add(relation.key);
|
|
1901
|
+
}
|
|
1902
|
+
});
|
|
1903
|
+
const lanes = new Map();
|
|
1904
|
+
const moved = new Set();
|
|
1905
|
+
for (const list of sides.values()) {
|
|
1906
|
+
if (list.length < 2)
|
|
1907
|
+
continue;
|
|
1908
|
+
const spacing = Math.max(LANE_SPACING, ...list.map((e) => spacingOf(e.relation)));
|
|
1909
|
+
// Runs of ends less than a lane apart along the side.
|
|
1910
|
+
const byPlace = [...list].sort((a, b) => a.at - b.at || a.along - b.along || a.order - b.order);
|
|
1911
|
+
const runs = [];
|
|
1912
|
+
for (const entry of byPlace) {
|
|
1913
|
+
const run = runs[runs.length - 1];
|
|
1914
|
+
if (run && entry.at - run[run.length - 1].at < spacing - 0.5)
|
|
1915
|
+
run.push(entry);
|
|
1916
|
+
else
|
|
1917
|
+
runs.push([entry]);
|
|
1918
|
+
}
|
|
1919
|
+
for (const run of runs) {
|
|
1920
|
+
if (run.length < 2)
|
|
1921
|
+
continue;
|
|
1922
|
+
const middle = run.reduce((sum, e) => sum + e.at, 0) / run.length;
|
|
1923
|
+
run.sort((a, b) => a.along - b.along || a.order - b.order);
|
|
1924
|
+
run.forEach((entry, i) => {
|
|
1925
|
+
const offset = middle + (i - (run.length - 1) / 2) * spacing - entry.at;
|
|
1926
|
+
const current = lanes.get(entry.relation.key) ?? { from: 0, to: 0 };
|
|
1927
|
+
lanes.set(entry.relation.key, { ...current, [entry.end]: offset });
|
|
1928
|
+
moved.add(`${entry.relation.key}\u0000${entry.end}`);
|
|
1929
|
+
});
|
|
1930
|
+
}
|
|
1931
|
+
}
|
|
1932
|
+
// A straight connector moved at one end only moves at the other too.
|
|
1933
|
+
for (const key of straight) {
|
|
1934
|
+
const lane = lanes.get(key);
|
|
1935
|
+
if (!lane)
|
|
1936
|
+
continue;
|
|
1937
|
+
const fromMoved = moved.has(`${key}\u0000from`);
|
|
1938
|
+
const toMoved = moved.has(`${key}\u0000to`);
|
|
1939
|
+
if (fromMoved && !toMoved)
|
|
1940
|
+
lanes.set(key, { from: lane.from, to: lane.from });
|
|
1941
|
+
else if (toMoved && !fromMoved)
|
|
1942
|
+
lanes.set(key, { from: lane.to, to: lane.to });
|
|
1943
|
+
}
|
|
1944
|
+
return lanes;
|
|
1945
|
+
}
|
|
1946
|
+
/**
|
|
1947
|
+
* Lane offsets of relations between the same two elements, in either direction, that have no
|
|
1948
|
+
* waypoints and no named ports: the i-th of n moves by (i - (n - 1) / 2) lanes.
|
|
1949
|
+
*/
|
|
1950
|
+
function parallelLanes(diagram, routes, spacingOf = () => LANE_SPACING) {
|
|
1951
|
+
const pairs = new Map();
|
|
1952
|
+
for (const relation of diagram.relations) {
|
|
1953
|
+
if (relation.from === relation.to)
|
|
1954
|
+
continue;
|
|
1955
|
+
const route = routes.get(relation.key);
|
|
1956
|
+
if (route &&
|
|
1957
|
+
(route.via.length > 0 ||
|
|
1958
|
+
route.from.named ||
|
|
1959
|
+
route.to.named ||
|
|
1960
|
+
route.from.position !== undefined ||
|
|
1961
|
+
route.to.position !== undefined))
|
|
1962
|
+
continue;
|
|
1963
|
+
const key = [relation.from, relation.to].sort().join("\u0000");
|
|
1964
|
+
const list = pairs.get(key) ?? [];
|
|
1965
|
+
list.push(relation);
|
|
1966
|
+
pairs.set(key, list);
|
|
1967
|
+
}
|
|
1968
|
+
const lanes = new Map();
|
|
1969
|
+
for (const list of pairs.values()) {
|
|
1970
|
+
if (list.length < 2)
|
|
1971
|
+
continue;
|
|
1972
|
+
// Unlabelled lanes keep the narrow spacing; labelled ones make room for their pills.
|
|
1973
|
+
const spacing = Math.max(LANE_SPACING, ...list.map(spacingOf));
|
|
1974
|
+
list.forEach((relation, i) => lanes.set(relation.key, (i - (list.length - 1) / 2) * spacing));
|
|
1975
|
+
}
|
|
1976
|
+
return lanes;
|
|
1977
|
+
}
|
|
1978
|
+
/**
|
|
1979
|
+
* The canvas for the diagram's `preset` or `canvas`, reporting `layout.too-large` when the content
|
|
1980
|
+
* canvas exceeds what the preset holds. Nothing is clipped: a fixed canvas grows to fit.
|
|
1981
|
+
*/
|
|
1982
|
+
export function canvasSize(diagram, width, height, lines, diagnostics) {
|
|
1983
|
+
const { preset, canvas } = diagram.properties;
|
|
1984
|
+
const words = messages();
|
|
1985
|
+
const tooLarge = (keyword, message) => diagnostics.push({
|
|
1986
|
+
code: "layout.too-large",
|
|
1987
|
+
severity: "warning",
|
|
1988
|
+
message,
|
|
1989
|
+
range: propertyRange(diagram, keyword, lines) ?? diagram.idRange,
|
|
1990
|
+
hints: [words.hints.tooLarge],
|
|
1991
|
+
});
|
|
1992
|
+
if (preset === "document") {
|
|
1993
|
+
if (width > DOCUMENT_MAX_WIDTH) {
|
|
1994
|
+
tooLarge("preset", words.diagnostics.tooWideForDocument(width, DOCUMENT_MAX_WIDTH));
|
|
1995
|
+
}
|
|
1996
|
+
return { width, height };
|
|
1997
|
+
}
|
|
1998
|
+
if (preset === "slide") {
|
|
1999
|
+
if (width > SLIDE_SIZE.width || height > SLIDE_SIZE.height) {
|
|
2000
|
+
tooLarge("preset", words.diagnostics.tooLargeForSlide(width, height, SLIDE_SIZE.width, SLIDE_SIZE.height));
|
|
2001
|
+
}
|
|
2002
|
+
return width * 9 >= height * 16
|
|
2003
|
+
? { width, height: Math.round((width * 9) / 16) }
|
|
2004
|
+
: { width: Math.round((height * 16) / 9), height };
|
|
2005
|
+
}
|
|
2006
|
+
if (canvas) {
|
|
2007
|
+
if (width > canvas.width || height > canvas.height) {
|
|
2008
|
+
tooLarge("canvas", words.diagnostics.tooLargeForCanvas(width, height, canvas.width, canvas.height));
|
|
2009
|
+
}
|
|
2010
|
+
return { width: Math.max(width, canvas.width), height: Math.max(height, canvas.height) };
|
|
2011
|
+
}
|
|
2012
|
+
return { width, height };
|
|
2013
|
+
}
|
|
2014
|
+
/** Width the frame needs for the mark, the brand text and the title without overlap. */
|
|
2015
|
+
export function frameMinWidth(theme, scale, title, metrics) {
|
|
2016
|
+
const brand = metrics.width(theme.brand, "body", 600, scale.frameBrand);
|
|
2017
|
+
const mark = theme.mark === "meltemi" ? scale.frameMark + 8 : 0;
|
|
2018
|
+
const titleWidth = title ? metrics.width(title, "body", 400, scale.frameTitle) + 32 : 0;
|
|
2019
|
+
return Math.ceil(2 * theme.margin + mark + brand + titleWidth);
|
|
2020
|
+
}
|
|
2021
|
+
function propertyRange(diagram, keyword, lines) {
|
|
2022
|
+
for (const stmt of diagram.block?.statements ?? []) {
|
|
2023
|
+
if (stmt.kind === "KeywordStatement" && stmt.keyword.text === keyword) {
|
|
2024
|
+
return lines.range({ start: stmt.start, end: stmt.end });
|
|
2025
|
+
}
|
|
2026
|
+
}
|
|
2027
|
+
return undefined;
|
|
2028
|
+
}
|
|
2029
|
+
const BOX_KINDS = new Set([
|
|
2030
|
+
"node",
|
|
2031
|
+
"decision",
|
|
2032
|
+
"data",
|
|
2033
|
+
"box",
|
|
2034
|
+
"person",
|
|
2035
|
+
"system",
|
|
2036
|
+
"container",
|
|
2037
|
+
"datastore",
|
|
2038
|
+
"component",
|
|
2039
|
+
]);
|
|
2040
|
+
/** The kinds whose icon takes the five positions, and whose label may sit at an edge. */
|
|
2041
|
+
const ICON_KINDS = new Set(["node", "decision", "data", "box"]);
|
|
2042
|
+
/** Shapes whose whole box holds a centred icon and its label; others use an inscribed rectangle. */
|
|
2043
|
+
const RECTANGULAR = new Set(["rounded", "rectangle", "square"]);
|
|
2044
|
+
/** How many heights the inscribed rectangle of a shape tries for a centred icon. */
|
|
2045
|
+
const INSCRIBED_STEPS = 24;
|
|
2046
|
+
/** The scales a shaped box with a centred icon tries, past 1, until its content fits. */
|
|
2047
|
+
const GROWTH_STEPS = 40;
|
|
2048
|
+
/** Box sizes from measured text, icons and the theme's type scale. */
|
|
2049
|
+
class Sizing {
|
|
2050
|
+
theme;
|
|
2051
|
+
scale;
|
|
2052
|
+
metrics;
|
|
2053
|
+
styles;
|
|
2054
|
+
icons;
|
|
2055
|
+
images;
|
|
2056
|
+
written;
|
|
2057
|
+
/** The icon of each element, placed once: its side may depend on the element's box. */
|
|
2058
|
+
placed = new Map();
|
|
2059
|
+
resolved = new Map();
|
|
2060
|
+
/** Centred icons: the default size of each element, and the layouts of the boxes asked for. */
|
|
2061
|
+
centredSizes = new Map();
|
|
2062
|
+
centredLayouts = new Map();
|
|
2063
|
+
centredLays = new Map();
|
|
2064
|
+
constructor(theme, scale, metrics, styles, icons, images,
|
|
2065
|
+
/** The written `size` of each element that has one. */
|
|
2066
|
+
written = new Map()) {
|
|
2067
|
+
this.theme = theme;
|
|
2068
|
+
this.scale = scale;
|
|
2069
|
+
this.metrics = metrics;
|
|
2070
|
+
this.styles = styles;
|
|
2071
|
+
this.icons = icons;
|
|
2072
|
+
this.images = images;
|
|
2073
|
+
this.written = written;
|
|
2074
|
+
}
|
|
2075
|
+
/**
|
|
2076
|
+
* The label font: the theme's label size and weight, or for free boxes and text objects their
|
|
2077
|
+
* `fontSize` and `fontWeight`. Text objects default to 1.6 times the label size. Lines are 1.25
|
|
2078
|
+
* times the size apart.
|
|
2079
|
+
*/
|
|
2080
|
+
labelFont(element) {
|
|
2081
|
+
const free = element.kind === "box" || element.kind === "text";
|
|
2082
|
+
const style = free ? this.styles.element(element) : {};
|
|
2083
|
+
const size = style.fontSize ??
|
|
2084
|
+
(element.kind === "text" ? Math.round(this.scale.label * 1.6) : this.scale.label);
|
|
2085
|
+
const weight = style.fontWeight ?? this.scale.labelWeight;
|
|
2086
|
+
return { size, weight, line: Math.round(size * 1.25) };
|
|
2087
|
+
}
|
|
2088
|
+
/**
|
|
2089
|
+
* The drawn shape of a box-like element: its kind's (data stores are cylinders, decisions
|
|
2090
|
+
* diamonds, C4 persons persons), else the `shape` of a flow node or a free box, else `rounded`.
|
|
2091
|
+
*/
|
|
2092
|
+
shapeOf(element) {
|
|
2093
|
+
const known = (shape) => shape && Object.hasOwn(SHAPE_SIZES, shape) ? shape : undefined;
|
|
2094
|
+
switch (element.kind) {
|
|
2095
|
+
case "data":
|
|
2096
|
+
case "datastore":
|
|
2097
|
+
return "cylinder";
|
|
2098
|
+
case "decision":
|
|
2099
|
+
return "diamond";
|
|
2100
|
+
case "person":
|
|
2101
|
+
return "person";
|
|
2102
|
+
case "node":
|
|
2103
|
+
return known(this.styles.element(element).shape) ?? "rounded";
|
|
2104
|
+
case "box":
|
|
2105
|
+
return known(this.styles.element(element).shape) ?? known(element.shape) ?? "rounded";
|
|
2106
|
+
default:
|
|
2107
|
+
return "rounded";
|
|
2108
|
+
}
|
|
2109
|
+
}
|
|
2110
|
+
/**
|
|
2111
|
+
* The measures a shape is drawn with: a person's head from the label size, and the corners of a
|
|
2112
|
+
* person's or an agent's body from the theme.
|
|
2113
|
+
*/
|
|
2114
|
+
shapeOptions(element) {
|
|
2115
|
+
const shape = this.shapeOf(element);
|
|
2116
|
+
// A written radius (the element's style, a class or a rule) rounds every cornered shape.
|
|
2117
|
+
const written = CORNERED_SHAPES.has(shape) ? this.styles.element(element).radius : undefined;
|
|
2118
|
+
if (shape === "agent")
|
|
2119
|
+
return { radius: written ?? this.theme.radius * 1.5 };
|
|
2120
|
+
if (shape === "person") {
|
|
2121
|
+
return { head: personHead(this.scale.label), radius: written ?? this.theme.radius * 3 };
|
|
2122
|
+
}
|
|
2123
|
+
return written === undefined ? {} : { radius: written };
|
|
2124
|
+
}
|
|
2125
|
+
/**
|
|
2126
|
+
* The rows the text column of a box-like element covers, from its text centre: the slots of its
|
|
2127
|
+
* lines and icon, and the same rows with room for the glyphs' descenders. `width` is the widest
|
|
2128
|
+
* line, or the icon row.
|
|
2129
|
+
*/
|
|
2130
|
+
textColumn(element) {
|
|
2131
|
+
return this.columnWith(element, this.columnIcon(element));
|
|
2132
|
+
}
|
|
2133
|
+
/** The text column of an element with an icon in it, or with none. */
|
|
2134
|
+
columnWith(element, icon) {
|
|
2135
|
+
const block = this.blockWith(element, icon);
|
|
2136
|
+
const font = this.labelFont(element);
|
|
2137
|
+
const lines = [
|
|
2138
|
+
...block.labelOffsets.map((y) => ({ y, size: font.size })),
|
|
2139
|
+
...block.descriptionOffsets.map((y) => ({ y, size: this.scale.description })),
|
|
2140
|
+
];
|
|
2141
|
+
let top = Math.min(...lines.map((l) => l.y - l.size / 2));
|
|
2142
|
+
let bottom = Math.max(...lines.map((l) => l.y + l.size / 2));
|
|
2143
|
+
let glyphs = Math.max(...lines.map((l) => l.y + l.size * 0.6));
|
|
2144
|
+
if (icon?.position === "top") {
|
|
2145
|
+
top = Math.min(top, block.iconOffset - icon.size / 2);
|
|
2146
|
+
}
|
|
2147
|
+
else if (icon?.position === "bottom") {
|
|
2148
|
+
bottom = Math.max(bottom, block.iconOffset + icon.size / 2);
|
|
2149
|
+
glyphs = Math.max(glyphs, block.iconOffset + icon.size / 2);
|
|
2150
|
+
}
|
|
2151
|
+
else if (icon) {
|
|
2152
|
+
// The icon row is centred on the text centre.
|
|
2153
|
+
top = Math.min(top, -icon.size / 2);
|
|
2154
|
+
bottom = Math.max(bottom, icon.size / 2);
|
|
2155
|
+
glyphs = Math.max(glyphs, icon.size / 2);
|
|
2156
|
+
}
|
|
2157
|
+
return {
|
|
2158
|
+
width: this.contentWidthWith(element, icon),
|
|
2159
|
+
slots: { top, bottom },
|
|
2160
|
+
glyphs: { top, bottom: glyphs },
|
|
2161
|
+
};
|
|
2162
|
+
}
|
|
2163
|
+
/** The text column of an element's label and description alone, without its icon. */
|
|
2164
|
+
plainColumn(element) {
|
|
2165
|
+
return this.columnWith(element, undefined);
|
|
2166
|
+
}
|
|
2167
|
+
/**
|
|
2168
|
+
* The text area of a box-like element drawn in `box`: the box for shapes whose text keeps the
|
|
2169
|
+
* whole width, else the shape's inscribed rectangle across the rows of the text column, centred
|
|
2170
|
+
* on the text centre, or (`centred` false) reaching as far as every row does on each side, where
|
|
2171
|
+
* aligned text may lie. The glyph rows count as clipped only where the shape reaches more than
|
|
2172
|
+
* `ROW_TOLERANCE` into them.
|
|
2173
|
+
*/
|
|
2174
|
+
textArea(element, box, rows, column = this.textColumn(element), centred = true) {
|
|
2175
|
+
const shape = this.shapeOf(element);
|
|
2176
|
+
if (FULL_WIDTH_TEXT.has(shape))
|
|
2177
|
+
return box;
|
|
2178
|
+
const options = this.shapeOptions(element);
|
|
2179
|
+
const center = shapeTextCenter(shape, box, options);
|
|
2180
|
+
const top = center.y + column[rows].top;
|
|
2181
|
+
const bottom = center.y + column[rows].bottom;
|
|
2182
|
+
const inset = rows === "glyphs" ? Math.min(ROW_TOLERANCE, (bottom - top) / 2) : 0;
|
|
2183
|
+
const span = shapeTextSpan(shape, box, top + inset, bottom - inset, center.x, options);
|
|
2184
|
+
if (centred) {
|
|
2185
|
+
const half = span ? Math.max(0, Math.min(center.x - span[0], span[1] - center.x)) : 0;
|
|
2186
|
+
return { x: center.x - half, y: top, width: 2 * half, height: bottom - top };
|
|
2187
|
+
}
|
|
2188
|
+
const [left, right] = span ?? [center.x, center.x];
|
|
2189
|
+
return { x: left, y: top, width: Math.max(0, right - left), height: bottom - top };
|
|
2190
|
+
}
|
|
2191
|
+
/**
|
|
2192
|
+
* The rectangle the label audit holds the text of a shaped element to, if any: the glyph rows of
|
|
2193
|
+
* its text column as far as the shape reaches on each side, so that aligned text inside the
|
|
2194
|
+
* outline is not cut by the narrow side of an uneven shape.
|
|
2195
|
+
*/
|
|
2196
|
+
innerArea(element, box) {
|
|
2197
|
+
// An icon shape's text lies outside its box (see `outsideLabel`).
|
|
2198
|
+
if (!BOX_KINDS.has(element.kind) || this.iconShaped(element))
|
|
2199
|
+
return undefined;
|
|
2200
|
+
const shape = this.shapeOf(element);
|
|
2201
|
+
// A centred icon and its label share the rectangle inscribed in the shape.
|
|
2202
|
+
if (this.centred(element)) {
|
|
2203
|
+
return RECTANGULAR.has(shape) ? undefined : this.centredLayout(element, box).content;
|
|
2204
|
+
}
|
|
2205
|
+
if (FULL_WIDTH_TEXT.has(shape))
|
|
2206
|
+
return undefined;
|
|
2207
|
+
return this.textArea(element, box, "glyphs", this.textColumn(element), false);
|
|
2208
|
+
}
|
|
2209
|
+
/**
|
|
2210
|
+
* The narrowest whole width at `height` at which the shape's text area holds the text column of
|
|
2211
|
+
* an element with `margin` on both sides, or Infinity when no width does (the text is too tall).
|
|
2212
|
+
* The width is rounded up, or down when it is within `ROUNDING` of a whole pixel.
|
|
2213
|
+
*/
|
|
2214
|
+
widthAt(element, height, margin, rows) {
|
|
2215
|
+
const column = this.textColumn(element);
|
|
2216
|
+
const need = column.width + 2 * margin;
|
|
2217
|
+
const fits = (width) => this.textArea(element, { x: 0, y: 0, width, height }, rows, column).width >= need - 1e-9;
|
|
2218
|
+
let low = Math.max(1, need);
|
|
2219
|
+
if (fits(low))
|
|
2220
|
+
return Math.ceil(low - ROUNDING);
|
|
2221
|
+
let high = low * 2;
|
|
2222
|
+
while (!fits(high)) {
|
|
2223
|
+
if (high > 64 * low + 4096)
|
|
2224
|
+
return Infinity;
|
|
2225
|
+
high *= 2;
|
|
2226
|
+
}
|
|
2227
|
+
for (let i = 0; i < 60 && high - low > 0.005; i++) {
|
|
2228
|
+
const middle = (low + high) / 2;
|
|
2229
|
+
if (fits(middle))
|
|
2230
|
+
high = middle;
|
|
2231
|
+
else
|
|
2232
|
+
low = middle;
|
|
2233
|
+
}
|
|
2234
|
+
return Math.ceil(high - ROUNDING);
|
|
2235
|
+
}
|
|
2236
|
+
/** Whether a group is untitled (`""`, no description, no styled icon): it draws no title band. */
|
|
2237
|
+
untitled(group) {
|
|
2238
|
+
return group.kind === "group" && isUntitledGroup(group, this.styles.element(group));
|
|
2239
|
+
}
|
|
2240
|
+
/** The room above a group's interior: its title band, or its padding when it is untitled. */
|
|
2241
|
+
headerOf(group) {
|
|
2242
|
+
return groupHeaderOf(group, this.styles.element(group), this.theme);
|
|
2243
|
+
}
|
|
2244
|
+
/** Horizontal alignment of a box or text label. */
|
|
2245
|
+
alignOf(element) {
|
|
2246
|
+
if (element.kind === "text")
|
|
2247
|
+
return this.styles.element(element).textAlign ?? "left";
|
|
2248
|
+
if (element.kind === "box")
|
|
2249
|
+
return this.styles.element(element).textAlign ?? "center";
|
|
2250
|
+
return "center";
|
|
2251
|
+
}
|
|
2252
|
+
/** The inset of a free text or icon drawn with a frame (`fill`, `stroke` or `pattern`). */
|
|
2253
|
+
frameInset(element) {
|
|
2254
|
+
const style = this.styles.element(element);
|
|
2255
|
+
return style.fill || style.stroke || style.pattern ? Math.round(this.theme.padding / 2) : 0;
|
|
2256
|
+
}
|
|
2257
|
+
/** The resolved icon of an `icon` object. */
|
|
2258
|
+
sourceIcon(element) {
|
|
2259
|
+
if (element.source === null || !this.icons)
|
|
2260
|
+
return undefined;
|
|
2261
|
+
return this.icons.resolve(element.source);
|
|
2262
|
+
}
|
|
2263
|
+
/** The resolved image of an `image` object. */
|
|
2264
|
+
imageOf(element) {
|
|
2265
|
+
return this.images.get(element.id);
|
|
2266
|
+
}
|
|
2267
|
+
place(reference, size, position) {
|
|
2268
|
+
if (reference === undefined || !this.icons)
|
|
2269
|
+
return undefined;
|
|
2270
|
+
const icon = this.icons.resolve(reference);
|
|
2271
|
+
if (!icon)
|
|
2272
|
+
return undefined;
|
|
2273
|
+
return { icon, size, gap: Math.round(size * 0.4), position };
|
|
2274
|
+
}
|
|
2275
|
+
/**
|
|
2276
|
+
* The icon of an element: on boxes, the theme's `iconSize` or the element's beside the text,
|
|
2277
|
+
* fitted to the box when the icon is the centre piece or the box itself; 1.3 times the text size
|
|
2278
|
+
* elsewhere.
|
|
2279
|
+
*/
|
|
2280
|
+
iconOf(element) {
|
|
2281
|
+
if (this.placed.has(element.id))
|
|
2282
|
+
return this.placed.get(element.id);
|
|
2283
|
+
const placed = this.placeIcon(element);
|
|
2284
|
+
this.placed.set(element.id, placed);
|
|
2285
|
+
return placed;
|
|
2286
|
+
}
|
|
2287
|
+
placeIcon(element) {
|
|
2288
|
+
const style = this.styles.element(element);
|
|
2289
|
+
const { scale, theme } = this;
|
|
2290
|
+
switch (element.kind) {
|
|
2291
|
+
case "text":
|
|
2292
|
+
case "icon":
|
|
2293
|
+
case "image":
|
|
2294
|
+
return undefined;
|
|
2295
|
+
case "group":
|
|
2296
|
+
return this.place(style.icon, Math.round(scale.groupTitle * 1.3), "left");
|
|
2297
|
+
case "tag":
|
|
2298
|
+
return this.place(style.icon, Math.round(scale.tag * 1.3), "left");
|
|
2299
|
+
case "note":
|
|
2300
|
+
return this.place(style.icon, Math.round(scale.note * 1.3), "left");
|
|
2301
|
+
}
|
|
2302
|
+
const icon = this.elementIcon(element);
|
|
2303
|
+
if (!icon)
|
|
2304
|
+
return undefined;
|
|
2305
|
+
const written = this.written.get(element.id);
|
|
2306
|
+
if (this.iconShaped(element)) {
|
|
2307
|
+
const size = this.iconShapeSide(element, written ?? this.iconShapeSize(element));
|
|
2308
|
+
return { icon, size, gap: 0, position: "center" };
|
|
2309
|
+
}
|
|
2310
|
+
let position = style.iconPosition ?? theme.iconPosition;
|
|
2311
|
+
if (position === "center") {
|
|
2312
|
+
if (ICON_KINDS.has(element.kind)) {
|
|
2313
|
+
const box = { x: 0, y: 0, ...(written ?? this.centredSize(element)) };
|
|
2314
|
+
return { icon, size: this.centredLayout(element, box).icon.width, gap: 0, position };
|
|
2315
|
+
}
|
|
2316
|
+
// Kinds that draw their own shape keep the icon above the text.
|
|
2317
|
+
position = "top";
|
|
2318
|
+
}
|
|
2319
|
+
const at = (size) => ({
|
|
2320
|
+
icon,
|
|
2321
|
+
size,
|
|
2322
|
+
// A large icon keeps the gap of a 24 px one.
|
|
2323
|
+
gap: Math.round((style.iconSize === undefined ? size : Math.min(size, 24)) * 0.4),
|
|
2324
|
+
position,
|
|
2325
|
+
});
|
|
2326
|
+
const requested = style.iconSize;
|
|
2327
|
+
if (requested === undefined)
|
|
2328
|
+
return at(theme.iconSize);
|
|
2329
|
+
if (!written)
|
|
2330
|
+
return at(requested === "auto" ? theme.iconSize : requested);
|
|
2331
|
+
// A written size holds the icon: `auto` fills the room, a number is clamped to it.
|
|
2332
|
+
return at(this.largestFitting(element, written, requested === "auto" ? ICON_SIZE_RANGE.max : requested, at));
|
|
2333
|
+
}
|
|
2334
|
+
/** The resolved `icon` of an element's style. */
|
|
2335
|
+
elementIcon(element) {
|
|
2336
|
+
if (this.resolved.has(element.id))
|
|
2337
|
+
return this.resolved.get(element.id);
|
|
2338
|
+
const reference = this.styles.element(element).icon;
|
|
2339
|
+
const icon = reference === undefined || !this.icons ? undefined : this.icons.resolve(reference);
|
|
2340
|
+
this.resolved.set(element.id, icon);
|
|
2341
|
+
return icon;
|
|
2342
|
+
}
|
|
2343
|
+
/**
|
|
2344
|
+
* The largest icon side up to `requested`, and at least 8 px, at which the text column with the
|
|
2345
|
+
* icon fits a box of a written size, half the padding around it.
|
|
2346
|
+
*/
|
|
2347
|
+
largestFitting(element, size, requested, at) {
|
|
2348
|
+
const fits = (side) => this.contentFits(element, size, at(side));
|
|
2349
|
+
const low = ICON_SIZE_RANGE.min;
|
|
2350
|
+
if (requested <= low || fits(requested))
|
|
2351
|
+
return requested;
|
|
2352
|
+
if (!fits(low))
|
|
2353
|
+
return low;
|
|
2354
|
+
let lo = low;
|
|
2355
|
+
let hi = requested;
|
|
2356
|
+
while (hi - lo > 1) {
|
|
2357
|
+
const middle = Math.floor((lo + hi) / 2);
|
|
2358
|
+
if (fits(middle))
|
|
2359
|
+
lo = middle;
|
|
2360
|
+
else
|
|
2361
|
+
hi = middle;
|
|
2362
|
+
}
|
|
2363
|
+
return lo;
|
|
2364
|
+
}
|
|
2365
|
+
/**
|
|
2366
|
+
* Whether an element's text column, with an icon in it, fits a box of a size: across and down
|
|
2367
|
+
* the shape's text area (a person's body, a cylinder below its top), half the padding around it.
|
|
2368
|
+
*/
|
|
2369
|
+
contentFits(element, size, icon) {
|
|
2370
|
+
const margin = Math.round(this.theme.padding / 2);
|
|
2371
|
+
const column = this.columnWith(element, icon);
|
|
2372
|
+
const slots = { top: column.slots.top - margin, bottom: column.slots.bottom + margin };
|
|
2373
|
+
if (slots.bottom - slots.top > size.height + 1e-9)
|
|
2374
|
+
return false;
|
|
2375
|
+
const box = { x: 0, y: 0, ...size };
|
|
2376
|
+
if (!this.rowsFit(element, box, slots))
|
|
2377
|
+
return false;
|
|
2378
|
+
const area = this.textArea(element, box, "slots", { ...column, slots });
|
|
2379
|
+
return area.width >= column.width + 2 * margin - 1e-9;
|
|
2380
|
+
}
|
|
2381
|
+
/** Whether rows about an element's text centre lie inside the text area of its shape in `box`. */
|
|
2382
|
+
rowsFit(element, box, rows) {
|
|
2383
|
+
const shape = this.shapeOf(element);
|
|
2384
|
+
const options = this.shapeOptions(element);
|
|
2385
|
+
const center = shapeTextCenter(shape, box, options);
|
|
2386
|
+
const span = shapeTextSpan(shape, box, center.y + rows.top, center.y + rows.bottom, center.x, options);
|
|
2387
|
+
return span !== undefined;
|
|
2388
|
+
}
|
|
2389
|
+
/**
|
|
2390
|
+
* The least whole height, from a box's own, at which the text column of an element with the icon
|
|
2391
|
+
* it draws there lies inside its shape's text area with half the padding above and below.
|
|
2392
|
+
*/
|
|
2393
|
+
heightHolding(element, size) {
|
|
2394
|
+
const margin = Math.round(this.theme.padding / 2);
|
|
2395
|
+
const column = this.textColumn(element);
|
|
2396
|
+
const rows = { top: column.slots.top - margin, bottom: column.slots.bottom + margin };
|
|
2397
|
+
const holds = (height) => this.rowsFit(element, { x: 0, y: 0, width: size.width, height }, rows);
|
|
2398
|
+
let low = size.height;
|
|
2399
|
+
if (holds(low))
|
|
2400
|
+
return low;
|
|
2401
|
+
let high = Math.ceil(Math.max(low * 2, rows.bottom - rows.top));
|
|
2402
|
+
while (!holds(high)) {
|
|
2403
|
+
if (high > 64 * low + 4096)
|
|
2404
|
+
return high;
|
|
2405
|
+
high *= 2;
|
|
2406
|
+
}
|
|
2407
|
+
while (high - low > 1) {
|
|
2408
|
+
const middle = Math.floor((low + high) / 2);
|
|
2409
|
+
if (holds(middle))
|
|
2410
|
+
high = middle;
|
|
2411
|
+
else
|
|
2412
|
+
low = middle;
|
|
2413
|
+
}
|
|
2414
|
+
return high;
|
|
2415
|
+
}
|
|
2416
|
+
/** The icon of an element when it sits in its text column: beside, above or below the text. */
|
|
2417
|
+
columnIcon(element) {
|
|
2418
|
+
if (!BOX_KINDS.has(element.kind))
|
|
2419
|
+
return undefined;
|
|
2420
|
+
const icon = this.iconOf(element);
|
|
2421
|
+
return icon && icon.position !== "center" ? icon : undefined;
|
|
2422
|
+
}
|
|
2423
|
+
/** Whether an element is drawn as its icon alone, its label outside (`shape icon`). */
|
|
2424
|
+
iconShaped(element) {
|
|
2425
|
+
return (element.kind === "node" || element.kind === "box") && this.shapeOf(element) === "icon";
|
|
2426
|
+
}
|
|
2427
|
+
/** Whether the style makes the icon the centre piece: `iconPosition center` or `shape icon`. */
|
|
2428
|
+
iconCentrePiece(element) {
|
|
2429
|
+
if (this.iconShaped(element))
|
|
2430
|
+
return true;
|
|
2431
|
+
const position = this.styles.element(element).iconPosition ?? this.theme.iconPosition;
|
|
2432
|
+
return ICON_KINDS.has(element.kind) && position === "center";
|
|
2433
|
+
}
|
|
2434
|
+
/** Whether an element is drawn with its icon centred and its label at an edge of its box. */
|
|
2435
|
+
centred(element) {
|
|
2436
|
+
return this.iconOf(element)?.position === "center" && !this.iconShaped(element);
|
|
2437
|
+
}
|
|
2438
|
+
/** The edge a centre piece's label sits at: `labelPosition`, else the bottom. */
|
|
2439
|
+
labelSide(element) {
|
|
2440
|
+
return this.styles.element(element).labelPosition ?? "bottom";
|
|
2441
|
+
}
|
|
2442
|
+
/** Whether an element has a label or a description to draw. */
|
|
2443
|
+
hasText(element) {
|
|
2444
|
+
return element.label.trim() !== "" || (element.description ?? "").trim() !== "";
|
|
2445
|
+
}
|
|
2446
|
+
/**
|
|
2447
|
+
* The default box of an icon shape: the square of a written `iconSize`, else 64 px at the 13 px
|
|
2448
|
+
* label size, scaled with it; a frame grows around the icon, as a free text's does.
|
|
2449
|
+
*/
|
|
2450
|
+
iconShapeSize(element) {
|
|
2451
|
+
const written = this.styles.element(element).iconSize;
|
|
2452
|
+
const side = typeof written === "number"
|
|
2453
|
+
? written
|
|
2454
|
+
: Math.round(SHAPE_SIZES.icon.width * (this.theme.labelSize / 13));
|
|
2455
|
+
const framed = side + 2 * this.frameInset(element);
|
|
2456
|
+
return { width: framed, height: framed };
|
|
2457
|
+
}
|
|
2458
|
+
/** The side of an icon shape's icon in its box: the box's shorter side, inside its frame. */
|
|
2459
|
+
iconShapeSide(element, size) {
|
|
2460
|
+
return Math.max(0, Math.min(size.width, size.height) - 2 * this.frameInset(element));
|
|
2461
|
+
}
|
|
2462
|
+
/**
|
|
2463
|
+
* The label of an icon shape, outside its box at `labelPosition`, half the padding away, aligned
|
|
2464
|
+
* by `textAlign`; undefined when it has no label and no description.
|
|
2465
|
+
*/
|
|
2466
|
+
outsideLabel(element, box) {
|
|
2467
|
+
if (!this.iconShaped(element) || !this.hasText(element))
|
|
2468
|
+
return undefined;
|
|
2469
|
+
const column = this.plainColumn(element);
|
|
2470
|
+
const width = column.width;
|
|
2471
|
+
const gap = Math.round(this.theme.padding / 2);
|
|
2472
|
+
const align = this.alignOf(element);
|
|
2473
|
+
const side = this.labelSide(element);
|
|
2474
|
+
const right = box.x + box.width;
|
|
2475
|
+
const bottom = box.y + box.height;
|
|
2476
|
+
let left;
|
|
2477
|
+
let y;
|
|
2478
|
+
switch (side) {
|
|
2479
|
+
case "bottom":
|
|
2480
|
+
case "top":
|
|
2481
|
+
left =
|
|
2482
|
+
align === "left"
|
|
2483
|
+
? box.x
|
|
2484
|
+
: align === "right"
|
|
2485
|
+
? right - width
|
|
2486
|
+
: box.x + box.width / 2 - width / 2;
|
|
2487
|
+
// Below the box from the top of its first line; above it down to its descenders.
|
|
2488
|
+
y =
|
|
2489
|
+
side === "bottom" ? bottom + gap - column.slots.top : box.y - gap - column.glyphs.bottom;
|
|
2490
|
+
break;
|
|
2491
|
+
case "left":
|
|
2492
|
+
left = box.x - gap - width;
|
|
2493
|
+
y = box.y + box.height / 2;
|
|
2494
|
+
break;
|
|
2495
|
+
case "right":
|
|
2496
|
+
left = right + gap;
|
|
2497
|
+
y = box.y + box.height / 2;
|
|
2498
|
+
break;
|
|
2499
|
+
}
|
|
2500
|
+
const anchor = align === "left" ? "start" : align === "right" ? "end" : "middle";
|
|
2501
|
+
const x = align === "left" ? left : align === "right" ? left + width : left + width / 2;
|
|
2502
|
+
return {
|
|
2503
|
+
box: {
|
|
2504
|
+
x: left,
|
|
2505
|
+
y: y + column.slots.top,
|
|
2506
|
+
width,
|
|
2507
|
+
height: column.glyphs.bottom - column.slots.top,
|
|
2508
|
+
},
|
|
2509
|
+
x,
|
|
2510
|
+
y,
|
|
2511
|
+
anchor,
|
|
2512
|
+
};
|
|
2513
|
+
}
|
|
2514
|
+
/**
|
|
2515
|
+
* How a centred icon and its label share a box: the label's band at the `labelPosition` edge of
|
|
2516
|
+
* the content, the padding from it, and the icon centred in the rest, as large as fits with the
|
|
2517
|
+
* padding around it, or a written `iconSize` when it is smaller. The content is the box of a
|
|
2518
|
+
* rectangular shape (with half the padding when the whole one leaves the icon under its least
|
|
2519
|
+
* size), else the rectangle inscribed in the shape about its text centre that holds the label and
|
|
2520
|
+
* the largest icon, with half the padding.
|
|
2521
|
+
*/
|
|
2522
|
+
centredLayout(element, box) {
|
|
2523
|
+
// Sizes, icons, checks and the drawing ask for the same boxes again: each is laid out once.
|
|
2524
|
+
const key = `${element.id}\u0000${box.x}\u0000${box.y}\u0000${box.width}\u0000${box.height}`;
|
|
2525
|
+
let layout = this.centredLayouts.get(key);
|
|
2526
|
+
if (!layout) {
|
|
2527
|
+
layout = this.layCentred(element, box);
|
|
2528
|
+
this.centredLayouts.set(key, layout);
|
|
2529
|
+
}
|
|
2530
|
+
return layout;
|
|
2531
|
+
}
|
|
2532
|
+
layCentred(element, box) {
|
|
2533
|
+
const shape = this.shapeOf(element);
|
|
2534
|
+
const rectangular = RECTANGULAR.has(shape);
|
|
2535
|
+
const tight = Math.round(this.theme.padding / 2);
|
|
2536
|
+
if (rectangular) {
|
|
2537
|
+
const layout = this.centredIn(element, this.theme.padding)(box);
|
|
2538
|
+
// A small written size keeps half the padding, as icons beside the text do, before the icon
|
|
2539
|
+
// goes under its least size.
|
|
2540
|
+
return layout.icon.width >= ICON_SIZE_RANGE.min
|
|
2541
|
+
? layout
|
|
2542
|
+
: this.centredIn(element, tight)(box);
|
|
2543
|
+
}
|
|
2544
|
+
const lay = this.centredIn(element, tight);
|
|
2545
|
+
const options = this.shapeOptions(element);
|
|
2546
|
+
const center = shapeTextCenter(shape, box, options);
|
|
2547
|
+
const spanOf = shapeTextSpanner(shape, box, options);
|
|
2548
|
+
const reach = Math.min(center.y - box.y, box.y + box.height - center.y);
|
|
2549
|
+
let best;
|
|
2550
|
+
for (let i = 1; i <= INSCRIBED_STEPS; i++) {
|
|
2551
|
+
const half = (reach * i) / INSCRIBED_STEPS;
|
|
2552
|
+
const span = spanOf(center.y - half, center.y + half, center.x);
|
|
2553
|
+
if (!span)
|
|
2554
|
+
continue;
|
|
2555
|
+
const width = 2 * Math.max(0, Math.min(center.x - span[0], span[1] - center.x));
|
|
2556
|
+
const layout = lay({ x: center.x - width / 2, y: center.y - half, width, height: 2 * half });
|
|
2557
|
+
if (!best ||
|
|
2558
|
+
(layout.fits && !best.fits) ||
|
|
2559
|
+
(layout.fits === best.fits && layout.icon.width > best.icon.width)) {
|
|
2560
|
+
best = layout;
|
|
2561
|
+
}
|
|
2562
|
+
}
|
|
2563
|
+
return best ?? lay(box);
|
|
2564
|
+
}
|
|
2565
|
+
/** Lays a centred icon and its label out in a content rectangle with a margin. */
|
|
2566
|
+
centredIn(element, margin) {
|
|
2567
|
+
const key = `${element.id}\u0000${margin}`;
|
|
2568
|
+
let lay = this.centredLays.get(key);
|
|
2569
|
+
if (!lay) {
|
|
2570
|
+
lay = this.layCentredIn(element, margin);
|
|
2571
|
+
this.centredLays.set(key, lay);
|
|
2572
|
+
}
|
|
2573
|
+
return lay;
|
|
2574
|
+
}
|
|
2575
|
+
layCentredIn(element, margin) {
|
|
2576
|
+
const column = this.plainColumn(element);
|
|
2577
|
+
const text = this.hasText(element);
|
|
2578
|
+
const bandWidth = text ? column.width : 0;
|
|
2579
|
+
const bandHeight = text ? column.slots.bottom - column.slots.top : 0;
|
|
2580
|
+
const side = this.labelSide(element);
|
|
2581
|
+
const across = side === "top" || side === "bottom";
|
|
2582
|
+
const written = this.styles.element(element).iconSize;
|
|
2583
|
+
return (content) => {
|
|
2584
|
+
const room = text ? margin + (across ? bandHeight : bandWidth) : 0;
|
|
2585
|
+
const rest = across
|
|
2586
|
+
? {
|
|
2587
|
+
x: content.x,
|
|
2588
|
+
y: side === "top" ? content.y + room : content.y,
|
|
2589
|
+
width: content.width,
|
|
2590
|
+
height: content.height - room,
|
|
2591
|
+
}
|
|
2592
|
+
: {
|
|
2593
|
+
x: side === "left" ? content.x + room : content.x,
|
|
2594
|
+
y: content.y,
|
|
2595
|
+
width: content.width - room,
|
|
2596
|
+
height: content.height,
|
|
2597
|
+
};
|
|
2598
|
+
const fit = Math.max(0, Math.min(rest.width, rest.height) - 2 * margin);
|
|
2599
|
+
const iconSide = Math.floor(typeof written === "number" ? Math.min(written, fit) : fit);
|
|
2600
|
+
const band = across
|
|
2601
|
+
? {
|
|
2602
|
+
x: content.x + margin,
|
|
2603
|
+
y: side === "top"
|
|
2604
|
+
? content.y + margin
|
|
2605
|
+
: content.y + content.height - margin - bandHeight,
|
|
2606
|
+
width: Math.max(0, content.width - 2 * margin),
|
|
2607
|
+
height: bandHeight,
|
|
2608
|
+
}
|
|
2609
|
+
: {
|
|
2610
|
+
x: side === "left" ? content.x + margin : content.x + content.width - margin - bandWidth,
|
|
2611
|
+
y: content.y + content.height / 2 - bandHeight / 2,
|
|
2612
|
+
width: bandWidth,
|
|
2613
|
+
height: bandHeight,
|
|
2614
|
+
};
|
|
2615
|
+
const fits = across
|
|
2616
|
+
? content.width - 2 * margin >= bandWidth - 1e-9 &&
|
|
2617
|
+
content.height >= 2 * margin + bandHeight - 1e-9
|
|
2618
|
+
: content.height - 2 * margin >= bandHeight - 1e-9 && rest.width > 0;
|
|
2619
|
+
return {
|
|
2620
|
+
content,
|
|
2621
|
+
margin,
|
|
2622
|
+
band,
|
|
2623
|
+
icon: boxFromCenter(boxCenter(rest), iconSide, iconSide),
|
|
2624
|
+
fits,
|
|
2625
|
+
};
|
|
2626
|
+
};
|
|
2627
|
+
}
|
|
2628
|
+
/**
|
|
2629
|
+
* The default box of an element with a centred icon: 120 × 120 at the 13 px label size, scaled
|
|
2630
|
+
* with it; wider for a long label below or above the icon, wider by the label's width less its
|
|
2631
|
+
* height for a label beside it (so the icon keeps the room a label below leaves it); grown to
|
|
2632
|
+
* hold a written `iconSize` with the padding around it. Shapes other than rectangles grow until
|
|
2633
|
+
* their inscribed rectangle holds the label and the icon a rectangle would.
|
|
2634
|
+
*/
|
|
2635
|
+
centredSize(element) {
|
|
2636
|
+
let size = this.centredSizes.get(element.id);
|
|
2637
|
+
if (!size) {
|
|
2638
|
+
size = this.sizeCentred(element);
|
|
2639
|
+
this.centredSizes.set(element.id, size);
|
|
2640
|
+
}
|
|
2641
|
+
return size;
|
|
2642
|
+
}
|
|
2643
|
+
sizeCentred(element) {
|
|
2644
|
+
const { theme } = this;
|
|
2645
|
+
const p = theme.padding;
|
|
2646
|
+
const base = Math.round(120 * (theme.labelSize / 13));
|
|
2647
|
+
const column = this.plainColumn(element);
|
|
2648
|
+
const text = this.hasText(element);
|
|
2649
|
+
const bandWidth = text ? column.width : 0;
|
|
2650
|
+
const bandHeight = text ? column.slots.bottom - column.slots.top : 0;
|
|
2651
|
+
const side = this.labelSide(element);
|
|
2652
|
+
const written = this.styles.element(element).iconSize;
|
|
2653
|
+
const icon = typeof written === "number" ? written + 2 * p : 0;
|
|
2654
|
+
let width;
|
|
2655
|
+
let height;
|
|
2656
|
+
if (side === "top" || side === "bottom") {
|
|
2657
|
+
width = Math.max(base, text ? bandWidth + 2 * p : 0, icon);
|
|
2658
|
+
height = Math.max(base, icon > 0 ? icon + (text ? p + bandHeight : 0) : 0);
|
|
2659
|
+
}
|
|
2660
|
+
else {
|
|
2661
|
+
width = Math.max(base + (text ? Math.max(0, bandWidth - bandHeight) : 0), icon > 0 ? icon + (text ? p + bandWidth : 0) : 0);
|
|
2662
|
+
height = Math.max(base, text ? bandHeight + 2 * p : 0, icon);
|
|
2663
|
+
}
|
|
2664
|
+
const size = { width: Math.ceil(width), height: Math.ceil(height) };
|
|
2665
|
+
if (RECTANGULAR.has(this.shapeOf(element)))
|
|
2666
|
+
return size;
|
|
2667
|
+
const target = this.centredIn(element, p)({ x: 0, y: 0, ...size }).icon.width;
|
|
2668
|
+
const grown = (step) => ({
|
|
2669
|
+
width: Math.ceil(size.width * (1 + step * 0.05)),
|
|
2670
|
+
height: Math.ceil(size.height * (1 + step * 0.05)),
|
|
2671
|
+
});
|
|
2672
|
+
const holds = (step) => {
|
|
2673
|
+
const layout = this.centredLayout(element, { x: 0, y: 0, ...grown(step) });
|
|
2674
|
+
return layout.fits && layout.icon.width >= target;
|
|
2675
|
+
};
|
|
2676
|
+
// A larger box holds more: the least step that holds is found by halving.
|
|
2677
|
+
if (holds(0) || !holds(GROWTH_STEPS))
|
|
2678
|
+
return grown(holds(0) ? 0 : GROWTH_STEPS);
|
|
2679
|
+
let low = 0;
|
|
2680
|
+
let high = GROWTH_STEPS;
|
|
2681
|
+
while (high - low > 1) {
|
|
2682
|
+
const middle = (low + high) >> 1;
|
|
2683
|
+
if (holds(middle))
|
|
2684
|
+
high = middle;
|
|
2685
|
+
else
|
|
2686
|
+
low = middle;
|
|
2687
|
+
}
|
|
2688
|
+
return grown(high);
|
|
2689
|
+
}
|
|
2690
|
+
/** The icon of a relation's label pill. */
|
|
2691
|
+
relationIcon(relation) {
|
|
2692
|
+
return this.place(this.styles.relation(relation).icon, Math.round(this.scale.relationLabel * 1.3), "left");
|
|
2693
|
+
}
|
|
2694
|
+
/**
|
|
2695
|
+
* Label and description positions of a box, with the icon above or below them when it sits
|
|
2696
|
+
* there. The label is broken at `\n` into lines `line` apart; a one-line label sits where it
|
|
2697
|
+
* always did.
|
|
2698
|
+
*/
|
|
2699
|
+
textBlock(element) {
|
|
2700
|
+
return this.blockWith(element, this.columnIcon(element));
|
|
2701
|
+
}
|
|
2702
|
+
/** The label and description of an element without its icon, as `textBlock` places them. */
|
|
2703
|
+
plainBlock(element) {
|
|
2704
|
+
return this.blockWith(element, undefined);
|
|
2705
|
+
}
|
|
2706
|
+
blockWith(element, icon) {
|
|
2707
|
+
const scale = this.scale;
|
|
2708
|
+
const font = this.labelFont(element);
|
|
2709
|
+
const label = font.size;
|
|
2710
|
+
const lines = splitLines(element.label).length;
|
|
2711
|
+
// How far the first line of the label lies above its last.
|
|
2712
|
+
const rows = (lines - 1) * font.line;
|
|
2713
|
+
const count = element.description ? splitLines(element.description).length : 0;
|
|
2714
|
+
const top = icon?.position === "top" || icon?.position === "bottom" ? icon.size + icon.gap : 0;
|
|
2715
|
+
let last = rows / 2;
|
|
2716
|
+
let descriptionOffsets = [];
|
|
2717
|
+
let height = label + rows;
|
|
2718
|
+
if (count > 0) {
|
|
2719
|
+
const gap = (label + scale.description) / 2 + 6;
|
|
2720
|
+
const extra = ((count - 1) * scale.descriptionLine) / 2;
|
|
2721
|
+
descriptionOffsets = Array.from({ length: count }, (_, i) => gap / 2 + 1 + i * scale.descriptionLine - extra + rows / 2);
|
|
2722
|
+
last = -(gap / 2 - 1) - extra + rows / 2;
|
|
2723
|
+
height = label / 2 + gap + (count - 1) * scale.descriptionLine + scale.description / 2 + rows;
|
|
2724
|
+
}
|
|
2725
|
+
const labelOffsets = Array.from({ length: lines }, (_, i) => last - rows + i * font.line);
|
|
2726
|
+
if (top === 0)
|
|
2727
|
+
return { labelOffsets, descriptionOffsets, height, iconOffset: 0 };
|
|
2728
|
+
// The icon sits above or below the text; the whole column is centred.
|
|
2729
|
+
const shift = icon.position === "top" ? top / 2 : -top / 2;
|
|
2730
|
+
return {
|
|
2731
|
+
labelOffsets: labelOffsets.map((offset) => offset + shift),
|
|
2732
|
+
descriptionOffsets: descriptionOffsets.map((offset) => offset + shift),
|
|
2733
|
+
height: height + top,
|
|
2734
|
+
iconOffset: icon.position === "top"
|
|
2735
|
+
? -(height + top) / 2 + icon.size / 2
|
|
2736
|
+
: (height + top) / 2 - icon.size / 2,
|
|
2737
|
+
};
|
|
2738
|
+
}
|
|
2739
|
+
/** The widest of a box's label and description lines. */
|
|
2740
|
+
textWidth(element) {
|
|
2741
|
+
const { metrics, scale } = this;
|
|
2742
|
+
const font = this.labelFont(element);
|
|
2743
|
+
let width = 0;
|
|
2744
|
+
for (const line of splitLines(element.label)) {
|
|
2745
|
+
width = Math.max(width, metrics.width(line, "body", font.weight, font.size));
|
|
2746
|
+
}
|
|
2747
|
+
for (const line of element.description ? splitLines(element.description) : []) {
|
|
2748
|
+
width = Math.max(width, metrics.width(line, "body", 400, scale.description));
|
|
2749
|
+
}
|
|
2750
|
+
return width;
|
|
2751
|
+
}
|
|
2752
|
+
/**
|
|
2753
|
+
* The widest text of an element, the comfortable box width that fits it and its icon (used for
|
|
2754
|
+
* default sizes and hints), and the minimum width below which the content touches the edges.
|
|
2755
|
+
*/
|
|
2756
|
+
textNeeds(element) {
|
|
2757
|
+
const { metrics, scale, theme } = this;
|
|
2758
|
+
const widest = (entries) => entries.reduce((a, b) => (b.width > a.width ? b : a), { text: "", width: 0 });
|
|
2759
|
+
const descriptions = (element.description ? splitLines(element.description) : []).map((line) => ({
|
|
2760
|
+
text: line,
|
|
2761
|
+
width: metrics.width(line, "body", 400, scale.description),
|
|
2762
|
+
}));
|
|
2763
|
+
const icon = this.iconOf(element);
|
|
2764
|
+
const beside = icon && icon.position === "left" ? icon.size + icon.gap : 0;
|
|
2765
|
+
switch (element.kind) {
|
|
2766
|
+
case "icon":
|
|
2767
|
+
case "image":
|
|
2768
|
+
return { text: "", width: 0, minimum: 0 };
|
|
2769
|
+
case "text": {
|
|
2770
|
+
const font = this.labelFont(element);
|
|
2771
|
+
const best = widest(splitLines(element.label).map((line) => ({
|
|
2772
|
+
text: line,
|
|
2773
|
+
width: metrics.width(line, "body", font.weight, font.size),
|
|
2774
|
+
})));
|
|
2775
|
+
const width = Math.ceil(Math.max(best.width, 1) + 2 * this.frameInset(element));
|
|
2776
|
+
return { text: best.text, width, minimum: width };
|
|
2777
|
+
}
|
|
2778
|
+
case "tag": {
|
|
2779
|
+
const width = metrics.width(element.label, "mono", 500, scale.tag) + beside;
|
|
2780
|
+
return {
|
|
2781
|
+
text: element.label,
|
|
2782
|
+
width: Math.ceil(width + scale.tagPadding),
|
|
2783
|
+
minimum: Math.ceil(width + 2 * MIN_TEXT_MARGIN),
|
|
2784
|
+
};
|
|
2785
|
+
}
|
|
2786
|
+
case "note": {
|
|
2787
|
+
const best = widest(splitLines(element.label).map((line) => ({
|
|
2788
|
+
text: line,
|
|
2789
|
+
width: metrics.width(line, "body", 400, scale.note),
|
|
2790
|
+
})));
|
|
2791
|
+
const width = Math.ceil(Math.max(best.width + beside, 1));
|
|
2792
|
+
return { text: best.text, width, minimum: width };
|
|
2793
|
+
}
|
|
2794
|
+
case "group": {
|
|
2795
|
+
// An untitled group has no text, so no minimum width.
|
|
2796
|
+
if (this.untitled(element))
|
|
2797
|
+
return { text: "", width: 0, minimum: 0 };
|
|
2798
|
+
const title = {
|
|
2799
|
+
text: element.label,
|
|
2800
|
+
width: metrics.width(element.label, "body", scale.labelWeight, scale.groupTitle) + beside,
|
|
2801
|
+
};
|
|
2802
|
+
const best = widest([title, ...descriptions]);
|
|
2803
|
+
const inset = theme.groupPadding + 4;
|
|
2804
|
+
return {
|
|
2805
|
+
text: best.text,
|
|
2806
|
+
width: Math.ceil(best.width + 2 * inset),
|
|
2807
|
+
minimum: Math.ceil(best.width + inset + MIN_TEXT_MARGIN),
|
|
2808
|
+
};
|
|
2809
|
+
}
|
|
2810
|
+
default: {
|
|
2811
|
+
const content = this.contentWidth(element);
|
|
2812
|
+
return {
|
|
2813
|
+
text: this.widestLine(element),
|
|
2814
|
+
width: Math.ceil(content + 2 * theme.padding),
|
|
2815
|
+
minimum: Math.ceil(content + 2 * MIN_TEXT_MARGIN),
|
|
2816
|
+
};
|
|
2817
|
+
}
|
|
2818
|
+
}
|
|
2819
|
+
}
|
|
2820
|
+
/** The widest of a box's label and description lines. */
|
|
2821
|
+
widestLine(element) {
|
|
2822
|
+
const { metrics, scale } = this;
|
|
2823
|
+
const font = this.labelFont(element);
|
|
2824
|
+
const [first = "", ...rest] = splitLines(element.label);
|
|
2825
|
+
let best = { text: first, width: metrics.width(first, "body", font.weight, font.size) };
|
|
2826
|
+
for (const line of rest) {
|
|
2827
|
+
const width = metrics.width(line, "body", font.weight, font.size);
|
|
2828
|
+
if (width > best.width)
|
|
2829
|
+
best = { text: line, width };
|
|
2830
|
+
}
|
|
2831
|
+
for (const line of element.description ? splitLines(element.description) : []) {
|
|
2832
|
+
const width = metrics.width(line, "body", 400, scale.description);
|
|
2833
|
+
if (width > best.width)
|
|
2834
|
+
best = { text: line, width };
|
|
2835
|
+
}
|
|
2836
|
+
return best.text;
|
|
2837
|
+
}
|
|
2838
|
+
/** The width of a box's text column: its widest line, with its icon beside, above or below. */
|
|
2839
|
+
contentWidth(element) {
|
|
2840
|
+
return this.contentWidthWith(element, this.columnIcon(element));
|
|
2841
|
+
}
|
|
2842
|
+
contentWidthWith(element, icon) {
|
|
2843
|
+
const widest = this.textWidth(element);
|
|
2844
|
+
if (!icon)
|
|
2845
|
+
return widest;
|
|
2846
|
+
if (icon.position === "top" || icon.position === "bottom")
|
|
2847
|
+
return Math.max(widest, icon.size);
|
|
2848
|
+
return widest + icon.size + icon.gap;
|
|
2849
|
+
}
|
|
2850
|
+
defaultSize(element) {
|
|
2851
|
+
const { scale, theme } = this;
|
|
2852
|
+
const needs = this.textNeeds(element);
|
|
2853
|
+
switch (element.kind) {
|
|
2854
|
+
case "tag":
|
|
2855
|
+
return { width: needs.width, height: scale.tagHeight };
|
|
2856
|
+
case "note":
|
|
2857
|
+
return {
|
|
2858
|
+
width: needs.width,
|
|
2859
|
+
height: splitLines(element.label).length * scale.noteLine + 4,
|
|
2860
|
+
};
|
|
2861
|
+
case "text":
|
|
2862
|
+
return {
|
|
2863
|
+
width: needs.width,
|
|
2864
|
+
height: splitLines(element.label).length * this.labelFont(element).line +
|
|
2865
|
+
2 * this.frameInset(element),
|
|
2866
|
+
};
|
|
2867
|
+
case "icon":
|
|
2868
|
+
return { width: 48, height: 48 };
|
|
2869
|
+
case "image":
|
|
2870
|
+
return defaultImageSize(this.imageOf(element));
|
|
2871
|
+
default: {
|
|
2872
|
+
if (this.iconShaped(element))
|
|
2873
|
+
return this.iconShapeSize(element);
|
|
2874
|
+
if (this.centred(element))
|
|
2875
|
+
return this.centredSize(element);
|
|
2876
|
+
const shape = this.shapeOf(element);
|
|
2877
|
+
const base = element.kind === "box" || element.kind === "node"
|
|
2878
|
+
? SHAPE_SIZES[shape]
|
|
2879
|
+
: (DEFAULT_SIZES[element.kind] ?? { width: 160, height: 56 });
|
|
2880
|
+
const factor = theme.labelSize / 13;
|
|
2881
|
+
const head = shape === "person" ? personHead(scale.label) * 2 : 0;
|
|
2882
|
+
// An icon beside the text is as tall as the text at least.
|
|
2883
|
+
const icon = this.columnIcon(element);
|
|
2884
|
+
const beside = icon && (icon.position === "left" || icon.position === "right");
|
|
2885
|
+
const text = Math.max(this.textBlock(element).height, beside ? icon.size : 0) +
|
|
2886
|
+
2 * Math.round(theme.padding * 0.8);
|
|
2887
|
+
// An agent's text sits in its body, below the head and the antenna.
|
|
2888
|
+
const fitted = Math.ceil(shape === "agent" ? agentHeight(text) : text + head);
|
|
2889
|
+
const width = Math.round(base.width * factor);
|
|
2890
|
+
const height = Math.max(Math.round(base.height * factor), fitted);
|
|
2891
|
+
if (FULL_WIDTH_TEXT.has(shape))
|
|
2892
|
+
return { width: Math.max(width, needs.width), height };
|
|
2893
|
+
return this.shapedSize(element, width, height);
|
|
2894
|
+
}
|
|
2895
|
+
}
|
|
2896
|
+
}
|
|
2897
|
+
/**
|
|
2898
|
+
* The default size of a shaped box: its base size, widened until the text area holds the text
|
|
2899
|
+
* column with the padding on both sides; when the base is too small, the smallest box of the
|
|
2900
|
+
* base height or a taller one, so a diamond with several lines grows on both axes.
|
|
2901
|
+
*/
|
|
2902
|
+
shapedSize(element, width, height) {
|
|
2903
|
+
const at = (h) => Math.max(width, this.widthAt(element, h, this.theme.padding, "slots"));
|
|
2904
|
+
let best = { width: at(height), height };
|
|
2905
|
+
if (best.width > width) {
|
|
2906
|
+
for (const factor of TALLER) {
|
|
2907
|
+
const h = Math.round(height * factor);
|
|
2908
|
+
const w = at(h);
|
|
2909
|
+
if (w * h < best.width * best.height)
|
|
2910
|
+
best = { width: w, height: h };
|
|
2911
|
+
}
|
|
2912
|
+
}
|
|
2913
|
+
if (!Number.isFinite(best.width)) {
|
|
2914
|
+
return { width: Math.max(width, this.textNeeds(element).width), height };
|
|
2915
|
+
}
|
|
2916
|
+
return best;
|
|
2917
|
+
}
|
|
2918
|
+
/** Whether a box-like element draws any text or icon. */
|
|
2919
|
+
hasContent(element) {
|
|
2920
|
+
return (element.label.trim() !== "" ||
|
|
2921
|
+
(element.description ?? "").trim() !== "" ||
|
|
2922
|
+
this.iconOf(element) !== undefined);
|
|
2923
|
+
}
|
|
2924
|
+
/**
|
|
2925
|
+
* The size a `layout.text-overflow` of a shaped element suggests, found with the check's own
|
|
2926
|
+
* rows and margin so that it passes there: the narrowest width of the written height, else the
|
|
2927
|
+
* smallest box at least as wide and up to four times as tall, else the default size.
|
|
2928
|
+
*/
|
|
2929
|
+
fittingSize(element, size) {
|
|
2930
|
+
const at = (height) => this.widthAt(element, height, MIN_TEXT_MARGIN, "glyphs");
|
|
2931
|
+
const width = at(size.height);
|
|
2932
|
+
if (Number.isFinite(width))
|
|
2933
|
+
return { width, height: size.height };
|
|
2934
|
+
let best;
|
|
2935
|
+
for (const factor of TALLER) {
|
|
2936
|
+
const height = Math.round(size.height * factor);
|
|
2937
|
+
const w = Math.max(size.width, at(height));
|
|
2938
|
+
if (Number.isFinite(w) && (!best || w * height < best.width * best.height)) {
|
|
2939
|
+
best = { width: w, height };
|
|
2940
|
+
}
|
|
2941
|
+
}
|
|
2942
|
+
return best ?? this.defaultSize(element);
|
|
2943
|
+
}
|
|
2944
|
+
/**
|
|
2945
|
+
* Reports `layout.text-overflow` when an explicit size is too small for its content: narrower
|
|
2946
|
+
* than a box's text needs, or for a shape, a text area (the glyph rows) that does not hold the
|
|
2947
|
+
* text column with `MIN_TEXT_MARGIN` on both sides; an icon beside, above or below the text that
|
|
2948
|
+
* reaches out of the text area even at its least size; a centred icon or an icon shape's icon
|
|
2949
|
+
* left under `ICON_SIZE_RANGE.min`. A shape that draws no text is not checked.
|
|
2950
|
+
*/
|
|
2951
|
+
checkOverflow(element, size, entry, diagnostics) {
|
|
2952
|
+
const words = messages();
|
|
2953
|
+
if (this.iconShaped(element)) {
|
|
2954
|
+
// An icon shape's text lies outside its box; its icon must keep its least size in it.
|
|
2955
|
+
const icon = this.iconOf(element);
|
|
2956
|
+
if (!icon || icon.size >= ICON_SIZE_RANGE.min)
|
|
2957
|
+
return;
|
|
2958
|
+
const holds = this.iconShapeSize(element);
|
|
2959
|
+
const fit = {
|
|
2960
|
+
width: Math.max(holds.width, size.width),
|
|
2961
|
+
height: Math.max(holds.height, size.height),
|
|
2962
|
+
};
|
|
2963
|
+
diagnostics.push({
|
|
2964
|
+
code: "layout.text-overflow",
|
|
2965
|
+
severity: "warning",
|
|
2966
|
+
message: words.diagnostics.iconTooSmall(element.id, size.width, size.height, fit.width, fit.height),
|
|
2967
|
+
range: entry.range,
|
|
2968
|
+
hints: [words.hints.iconTooSmall(element.id, fit.width, fit.height)],
|
|
2969
|
+
data: { ids: [element.id], size: { element: element.id, ...fit } },
|
|
2970
|
+
});
|
|
2971
|
+
return;
|
|
2972
|
+
}
|
|
2973
|
+
const needs = this.textNeeds(element);
|
|
2974
|
+
// The size at which the text fits: a wider box of the same height, or for a shape the size
|
|
2975
|
+
// its check passes at, taller when no width holds the text at the written height.
|
|
2976
|
+
let fit;
|
|
2977
|
+
if (this.centred(element)) {
|
|
2978
|
+
// A centred icon gives way to its label, which must fit across the content, but keeps its
|
|
2979
|
+
// least size.
|
|
2980
|
+
const { content } = this.centredLayout(element, { x: 0, y: 0, ...size });
|
|
2981
|
+
const column = this.plainColumn(element);
|
|
2982
|
+
if (content.width >= column.width + 2 * MIN_TEXT_MARGIN - ROUNDING &&
|
|
2983
|
+
content.height >= column.slots.bottom - column.slots.top + 2 * MIN_TEXT_MARGIN - ROUNDING &&
|
|
2984
|
+
this.iconOf(element).size >= ICON_SIZE_RANGE.min) {
|
|
2985
|
+
return;
|
|
2986
|
+
}
|
|
2987
|
+
const grown = this.centredSize(element);
|
|
2988
|
+
fit = {
|
|
2989
|
+
width: Math.max(grown.width, size.width),
|
|
2990
|
+
height: Math.max(grown.height, size.height),
|
|
2991
|
+
};
|
|
2992
|
+
}
|
|
2993
|
+
else if (BOX_KINDS.has(element.kind) && !FULL_WIDTH_TEXT.has(this.shapeOf(element))) {
|
|
2994
|
+
if (!this.hasContent(element))
|
|
2995
|
+
return;
|
|
2996
|
+
const room = this.textArea(element, { x: 0, y: 0, ...size }, "glyphs").width;
|
|
2997
|
+
// Suggested widths are rounded to whole pixels, which may take `ROUNDING` off the room.
|
|
2998
|
+
if (room >= this.textColumn(element).width + 2 * MIN_TEXT_MARGIN - ROUNDING)
|
|
2999
|
+
return;
|
|
3000
|
+
fit = this.fittingSize(element, size);
|
|
3001
|
+
}
|
|
3002
|
+
else {
|
|
3003
|
+
// An icon in the text column must not reach out of the shape's text area: above or below a
|
|
3004
|
+
// box, into a person's head, below a cylinder.
|
|
3005
|
+
const box = { x: 0, y: 0, ...size };
|
|
3006
|
+
const spills = BOX_KINDS.has(element.kind) &&
|
|
3007
|
+
this.columnIcon(element) !== undefined &&
|
|
3008
|
+
!this.rowsFit(element, box, this.textColumn(element).slots);
|
|
3009
|
+
if (size.width >= needs.minimum && !spills)
|
|
3010
|
+
return;
|
|
3011
|
+
fit = {
|
|
3012
|
+
width: size.width >= needs.minimum ? size.width : needs.width,
|
|
3013
|
+
height: spills ? this.heightHolding(element, size) : size.height,
|
|
3014
|
+
};
|
|
3015
|
+
}
|
|
3016
|
+
const withIcon = this.iconOf(element) !== undefined;
|
|
3017
|
+
diagnostics.push({
|
|
3018
|
+
code: "layout.text-overflow",
|
|
3019
|
+
severity: "warning",
|
|
3020
|
+
message: fit.height === size.height
|
|
3021
|
+
? words.diagnostics.textOverflow(element.id, size.width, withIcon, needs.text, fit.width)
|
|
3022
|
+
: words.diagnostics.textOverflowTall(element.id, size.width, size.height, withIcon, needs.text, fit.width, fit.height),
|
|
3023
|
+
range: entry.range,
|
|
3024
|
+
hints: [words.hints.textOverflow(element.id, fit.width, fit.height)],
|
|
3025
|
+
data: {
|
|
3026
|
+
ids: [element.id],
|
|
3027
|
+
size: { element: element.id, ...fit },
|
|
3028
|
+
},
|
|
3029
|
+
});
|
|
3030
|
+
}
|
|
3031
|
+
}
|
|
3032
|
+
class Emitter {
|
|
3033
|
+
theme;
|
|
3034
|
+
scale;
|
|
3035
|
+
styles;
|
|
3036
|
+
metrics;
|
|
3037
|
+
sizing;
|
|
3038
|
+
patterns = [];
|
|
3039
|
+
painter = new IconPainter();
|
|
3040
|
+
patternIds = new Map();
|
|
3041
|
+
/** The title areas of drawn groups, which connectors pass under. */
|
|
3042
|
+
titles = [];
|
|
3043
|
+
constructor(theme, scale, styles, metrics, sizing) {
|
|
3044
|
+
this.theme = theme;
|
|
3045
|
+
this.scale = scale;
|
|
3046
|
+
this.styles = styles;
|
|
3047
|
+
this.metrics = metrics;
|
|
3048
|
+
this.sizing = sizing;
|
|
3049
|
+
}
|
|
3050
|
+
text(x, y, text, size, weight, fill, anchor = "middle", font = "body") {
|
|
3051
|
+
const baseline = round(this.metrics.baseline(y, font, weight, size));
|
|
3052
|
+
return {
|
|
3053
|
+
kind: "text",
|
|
3054
|
+
x: round(x),
|
|
3055
|
+
y: round(y),
|
|
3056
|
+
baseline,
|
|
3057
|
+
text,
|
|
3058
|
+
size,
|
|
3059
|
+
weight,
|
|
3060
|
+
font,
|
|
3061
|
+
anchor,
|
|
3062
|
+
fill,
|
|
3063
|
+
};
|
|
3064
|
+
}
|
|
3065
|
+
/** The icon in a square of its size centred on (cx, cy). */
|
|
3066
|
+
icon(placed, cx, cy, color) {
|
|
3067
|
+
return this.painter.draw(placed.icon, cx - placed.size / 2, cy - placed.size / 2, placed.size, color);
|
|
3068
|
+
}
|
|
3069
|
+
/** The id of the scene pattern of a kind and colour, declared on first use. */
|
|
3070
|
+
pattern(kind, color) {
|
|
3071
|
+
const key = `${kind}|${color}`;
|
|
3072
|
+
let id = this.patternIds.get(key);
|
|
3073
|
+
if (!id) {
|
|
3074
|
+
id = `pattern-${this.patterns.length + 1}`;
|
|
3075
|
+
this.patternIds.set(key, id);
|
|
3076
|
+
this.patterns.push({ id, kind, color, size: PATTERN_SIZE });
|
|
3077
|
+
}
|
|
3078
|
+
return `url(#${id})`;
|
|
3079
|
+
}
|
|
3080
|
+
/** The overlay of a patterned shape: the same outline, filled with the pattern. */
|
|
3081
|
+
overlay(style, shape) {
|
|
3082
|
+
if (!style.pattern)
|
|
3083
|
+
return [];
|
|
3084
|
+
const fill = this.pattern(style.pattern, style.stroke ?? this.theme.muted);
|
|
3085
|
+
if (shape.kind === "rect")
|
|
3086
|
+
return [{ ...shape, fill, stroke: "none", strokeWidth: 0 }];
|
|
3087
|
+
if (shape.kind === "path")
|
|
3088
|
+
return [{ ...shape, fill, stroke: "none", strokeWidth: 0 }];
|
|
3089
|
+
return [];
|
|
3090
|
+
}
|
|
3091
|
+
rect(box, radius, fill, stroke, strokeWidth, dashed) {
|
|
3092
|
+
return {
|
|
3093
|
+
kind: "rect",
|
|
3094
|
+
x: round(box.x),
|
|
3095
|
+
y: round(box.y),
|
|
3096
|
+
width: round(box.width),
|
|
3097
|
+
height: round(box.height),
|
|
3098
|
+
radius,
|
|
3099
|
+
fill,
|
|
3100
|
+
stroke,
|
|
3101
|
+
strokeWidth,
|
|
3102
|
+
dashed,
|
|
3103
|
+
};
|
|
3104
|
+
}
|
|
3105
|
+
group(element, box) {
|
|
3106
|
+
return withUrl(withBounds(this.groupItems(element, box), box), element);
|
|
3107
|
+
}
|
|
3108
|
+
groupItems(element, box) {
|
|
3109
|
+
const { theme, scale } = this;
|
|
3110
|
+
const style = this.styles.element(element);
|
|
3111
|
+
const outline = this.rect(box, cornerRadius(style.radius ?? theme.groupRadius, box), style.fill ?? theme.band, style.stroke ?? theme.bandLine, style.width ?? theme.strokeWidth, strokeDash(style.line));
|
|
3112
|
+
const items = [outline, ...this.overlay(style, outline)];
|
|
3113
|
+
// An untitled group is a plain container: no title, icon or description, and no title area
|
|
3114
|
+
// for connectors to pass under.
|
|
3115
|
+
if (this.sizing.untitled(element)) {
|
|
3116
|
+
return { kind: "group", id: element.id, role: "element", items };
|
|
3117
|
+
}
|
|
3118
|
+
const inset = theme.groupPadding + 4;
|
|
3119
|
+
const titleY = box.y + theme.groupHeader * 0.45;
|
|
3120
|
+
const color = style.color ?? theme.ink;
|
|
3121
|
+
let titleX = box.x + inset;
|
|
3122
|
+
const icon = this.sizing.iconOf(element);
|
|
3123
|
+
if (icon) {
|
|
3124
|
+
items.push(this.icon(icon, titleX + icon.size / 2, titleY, style.iconColor ?? color));
|
|
3125
|
+
titleX += icon.size + icon.gap;
|
|
3126
|
+
}
|
|
3127
|
+
items.push(this.text(titleX, titleY, element.label, scale.groupTitle, scale.labelWeight, color, "start"));
|
|
3128
|
+
// The title, and the description under it, are kept clear of connectors.
|
|
3129
|
+
const titleWidth = this.metrics.width(element.label, "body", scale.labelWeight, scale.groupTitle);
|
|
3130
|
+
const descriptionWidth = element.description
|
|
3131
|
+
? this.metrics.width(element.description, "body", 400, scale.description)
|
|
3132
|
+
: 0;
|
|
3133
|
+
const right = Math.max(titleX + titleWidth, box.x + inset + descriptionWidth);
|
|
3134
|
+
this.titles.push({
|
|
3135
|
+
id: element.id,
|
|
3136
|
+
box: {
|
|
3137
|
+
x: box.x + inset - TITLE_GAP,
|
|
3138
|
+
y: titleY - scale.groupTitle * 0.75 - TITLE_GAP,
|
|
3139
|
+
width: right - (box.x + inset) + 2 * TITLE_GAP,
|
|
3140
|
+
height: (element.description
|
|
3141
|
+
? box.y + theme.groupHeader * 0.85 + scale.description * 0.6
|
|
3142
|
+
: titleY + scale.groupTitle * 0.6) -
|
|
3143
|
+
(titleY - scale.groupTitle * 0.75) +
|
|
3144
|
+
2 * TITLE_GAP,
|
|
3145
|
+
},
|
|
3146
|
+
});
|
|
3147
|
+
if (element.description) {
|
|
3148
|
+
items.push(this.text(box.x + inset, box.y + theme.groupHeader * 0.85, element.description, scale.description, 400, style.color ?? theme.muted, "start"));
|
|
3149
|
+
}
|
|
3150
|
+
return { kind: "group", id: element.id, role: "element", items };
|
|
3151
|
+
}
|
|
3152
|
+
element(element, box) {
|
|
3153
|
+
return withUrl(withBounds(this.elementItems(element, box), box, this.sizing.innerArea(element, box), this.sizing.outsideLabel(element, box)?.box), element);
|
|
3154
|
+
}
|
|
3155
|
+
elementItems(element, box) {
|
|
3156
|
+
switch (element.kind) {
|
|
3157
|
+
case "note":
|
|
3158
|
+
return this.note(element, box);
|
|
3159
|
+
case "tag":
|
|
3160
|
+
return this.tag(element, box);
|
|
3161
|
+
case "text":
|
|
3162
|
+
return this.freeText(element, box);
|
|
3163
|
+
case "icon":
|
|
3164
|
+
return this.iconObject(element, box);
|
|
3165
|
+
case "image":
|
|
3166
|
+
return this.imageObject(element, box);
|
|
3167
|
+
default:
|
|
3168
|
+
return this.shape(element, box);
|
|
3169
|
+
}
|
|
3170
|
+
}
|
|
3171
|
+
/** The frame of a free text, icon or image when the style sets `fill`, `stroke` or `pattern`. */
|
|
3172
|
+
freeFrame(element, box, fill = true) {
|
|
3173
|
+
const style = this.styles.element(element);
|
|
3174
|
+
if (!(style.fill || style.stroke || style.pattern))
|
|
3175
|
+
return [];
|
|
3176
|
+
const outline = this.rect(box, cornerRadius(style.radius ?? this.theme.radius, box), fill ? (style.fill ?? "none") : "none", style.stroke ?? "none", style.width ?? this.theme.strokeWidth, strokeDash(style.line));
|
|
3177
|
+
return [outline, ...this.overlay(style, outline)];
|
|
3178
|
+
}
|
|
3179
|
+
/**
|
|
3180
|
+
* A placeholder for an icon or image that cannot be drawn: a dashed frame with its name when the
|
|
3181
|
+
* name fits, or a cross when it does not.
|
|
3182
|
+
*/
|
|
3183
|
+
placeholder(box, name) {
|
|
3184
|
+
const { theme, scale } = this;
|
|
3185
|
+
const center = boxCenter(box);
|
|
3186
|
+
const items = [
|
|
3187
|
+
this.rect(box, theme.radius, "none", theme.muted, theme.strokeWidth, true),
|
|
3188
|
+
];
|
|
3189
|
+
if (this.metrics.width(name, "body", 400, scale.note) <= box.width - 8) {
|
|
3190
|
+
items.push(this.text(center.x, center.y, name, scale.note, 400, theme.muted));
|
|
3191
|
+
}
|
|
3192
|
+
else {
|
|
3193
|
+
const inset = Math.min(box.width, box.height) / 4;
|
|
3194
|
+
items.push({
|
|
3195
|
+
kind: "path",
|
|
3196
|
+
d: `M${round(box.x + inset)} ${round(box.y + inset)}L${round(box.x + box.width - inset)} ${round(box.y + box.height - inset)}M${round(box.x + box.width - inset)} ${round(box.y + inset)}L${round(box.x + inset)} ${round(box.y + box.height - inset)}`,
|
|
3197
|
+
fill: "none",
|
|
3198
|
+
stroke: theme.muted,
|
|
3199
|
+
strokeWidth: theme.strokeWidth,
|
|
3200
|
+
dashed: false,
|
|
3201
|
+
});
|
|
3202
|
+
}
|
|
3203
|
+
return items;
|
|
3204
|
+
}
|
|
3205
|
+
freeText(element, box) {
|
|
3206
|
+
const style = this.styles.element(element);
|
|
3207
|
+
const items = [...this.freeFrame(element, box)];
|
|
3208
|
+
const font = this.sizing.labelFont(element);
|
|
3209
|
+
const inset = this.sizing.frameInset(element);
|
|
3210
|
+
const align = this.sizing.alignOf(element);
|
|
3211
|
+
const lines = splitLines(element.label);
|
|
3212
|
+
const x = align === "left"
|
|
3213
|
+
? box.x + inset
|
|
3214
|
+
: align === "right"
|
|
3215
|
+
? box.x + box.width - inset
|
|
3216
|
+
: box.x + box.width / 2;
|
|
3217
|
+
const anchor = align === "left" ? "start" : align === "right" ? "end" : "middle";
|
|
3218
|
+
const top = box.y + box.height / 2 - (lines.length * font.line) / 2;
|
|
3219
|
+
lines.forEach((line, i) => {
|
|
3220
|
+
items.push(this.text(x, top + font.line / 2 + i * font.line, line, font.size, font.weight, style.color ?? this.theme.ink, anchor));
|
|
3221
|
+
});
|
|
3222
|
+
return { kind: "group", id: element.id, role: "element", items };
|
|
3223
|
+
}
|
|
3224
|
+
iconObject(element, box) {
|
|
3225
|
+
const style = this.styles.element(element);
|
|
3226
|
+
const items = [...this.freeFrame(element, box)];
|
|
3227
|
+
const icon = this.sizing.sourceIcon(element);
|
|
3228
|
+
if (!icon) {
|
|
3229
|
+
items.push(...this.placeholder(box, element.source ?? element.id));
|
|
3230
|
+
return { kind: "group", id: element.id, role: "element", items };
|
|
3231
|
+
}
|
|
3232
|
+
const inset = this.sizing.frameInset(element);
|
|
3233
|
+
const size = Math.max(0, Math.min(box.width, box.height) - 2 * inset);
|
|
3234
|
+
const center = boxCenter(box);
|
|
3235
|
+
const color = style.iconColor ?? style.color ?? this.theme.ink;
|
|
3236
|
+
items.push(this.icon({ icon, size, gap: 0, position: "left" }, center.x, center.y, color));
|
|
3237
|
+
return { kind: "group", id: element.id, role: "element", items };
|
|
3238
|
+
}
|
|
3239
|
+
imageObject(element, box) {
|
|
3240
|
+
const image = this.sizing.imageOf(element);
|
|
3241
|
+
const items = [];
|
|
3242
|
+
if (!image) {
|
|
3243
|
+
const name = (element.source ?? element.id).split("/").pop() ?? element.id;
|
|
3244
|
+
items.push(...this.placeholder(box, name));
|
|
3245
|
+
return { kind: "group", id: element.id, role: "element", items };
|
|
3246
|
+
}
|
|
3247
|
+
items.push({
|
|
3248
|
+
kind: "image",
|
|
3249
|
+
x: round(box.x),
|
|
3250
|
+
y: round(box.y),
|
|
3251
|
+
width: round(box.width),
|
|
3252
|
+
height: round(box.height),
|
|
3253
|
+
href: image.href,
|
|
3254
|
+
title: element.description,
|
|
3255
|
+
});
|
|
3256
|
+
items.push(...this.freeFrame(element, box, false));
|
|
3257
|
+
return { kind: "group", id: element.id, role: "element", items };
|
|
3258
|
+
}
|
|
3259
|
+
shape(element, box) {
|
|
3260
|
+
if (this.sizing.iconShaped(element))
|
|
3261
|
+
return this.iconShape(element, box);
|
|
3262
|
+
const { theme } = this;
|
|
3263
|
+
const style = this.styles.element(element);
|
|
3264
|
+
const fill = style.fill ?? theme.surface;
|
|
3265
|
+
const stroke = style.stroke ?? theme.line;
|
|
3266
|
+
const strokeWidth = style.width ?? theme.strokeWidth;
|
|
3267
|
+
const dashed = strokeDash(style.line);
|
|
3268
|
+
const items = [];
|
|
3269
|
+
const shape = this.sizing.shapeOf(element);
|
|
3270
|
+
const path = (d) => ({ kind: "path", d, fill, stroke, strokeWidth, dashed });
|
|
3271
|
+
// The outline, with the pattern overlay on it.
|
|
3272
|
+
const outline = (item) => items.push(item, ...this.overlay(style, item));
|
|
3273
|
+
switch (shape) {
|
|
3274
|
+
case "cylinder": {
|
|
3275
|
+
const { body, top } = cylinderPaths(box);
|
|
3276
|
+
outline(path(body));
|
|
3277
|
+
items.push(path(top));
|
|
3278
|
+
break;
|
|
3279
|
+
}
|
|
3280
|
+
case "diamond":
|
|
3281
|
+
// Sharp vertices, or rounded by the element's `radius`.
|
|
3282
|
+
outline(path(shapeOutline("diamond", box, this.sizing.shapeOptions(element)).d));
|
|
3283
|
+
break;
|
|
3284
|
+
case "ellipse":
|
|
3285
|
+
case "circle":
|
|
3286
|
+
outline(path(ellipsePath(box)));
|
|
3287
|
+
break;
|
|
3288
|
+
case "person": {
|
|
3289
|
+
// A head above a rounded body; the text sits in the body.
|
|
3290
|
+
const r = Math.min(this.sizing.shapeOptions(element).head, box.width / 2, box.height / 3);
|
|
3291
|
+
const body = {
|
|
3292
|
+
x: box.x,
|
|
3293
|
+
y: box.y + r * 1.6,
|
|
3294
|
+
width: box.width,
|
|
3295
|
+
height: box.height - r * 1.6,
|
|
3296
|
+
};
|
|
3297
|
+
outline(this.rect(body, Math.min(this.sizing.shapeOptions(element).radius, body.height / 2, body.width / 2), fill, stroke, strokeWidth, dashed));
|
|
3298
|
+
items.push(path(ellipsePath({ x: box.x + box.width / 2 - r, y: box.y, width: 2 * r, height: 2 * r })));
|
|
3299
|
+
break;
|
|
3300
|
+
}
|
|
3301
|
+
case "rounded":
|
|
3302
|
+
outline(this.rect(box, cornerRadius(style.radius ?? theme.radius, box), fill, stroke, strokeWidth, dashed));
|
|
3303
|
+
break;
|
|
3304
|
+
case "rectangle":
|
|
3305
|
+
case "square":
|
|
3306
|
+
outline(this.rect(box, cornerRadius(style.radius ?? 0, box), fill, stroke, strokeWidth, dashed));
|
|
3307
|
+
break;
|
|
3308
|
+
case "pill":
|
|
3309
|
+
// Half the shorter side: a pill taller than wide is the upright stadium of its outline.
|
|
3310
|
+
outline(this.rect(box, cornerRadius(Infinity, box), fill, stroke, strokeWidth, dashed));
|
|
3311
|
+
break;
|
|
3312
|
+
case "framed":
|
|
3313
|
+
outline(this.rect(box, cornerRadius(style.radius ?? 0, box), fill, stroke, strokeWidth, dashed));
|
|
3314
|
+
items.push({
|
|
3315
|
+
kind: "path",
|
|
3316
|
+
d: framedLines(box),
|
|
3317
|
+
fill: "none",
|
|
3318
|
+
stroke,
|
|
3319
|
+
strokeWidth,
|
|
3320
|
+
dashed,
|
|
3321
|
+
});
|
|
3322
|
+
break;
|
|
3323
|
+
default:
|
|
3324
|
+
outline(path(shapeOutline(shape, box, this.sizing.shapeOptions(element)).d));
|
|
3325
|
+
}
|
|
3326
|
+
const labelColor = style.color ?? theme.ink;
|
|
3327
|
+
const icon = this.sizing.iconOf(element);
|
|
3328
|
+
const align = this.sizing.alignOf(element);
|
|
3329
|
+
if (icon && this.sizing.centred(element)) {
|
|
3330
|
+
// The icon is the picture of the box; the label takes a band at an edge.
|
|
3331
|
+
const layout = this.sizing.centredLayout(element, box);
|
|
3332
|
+
const center = boxCenter(layout.icon);
|
|
3333
|
+
// An icon under its least size is a smudge: it is left out, and `layout.text-overflow` says so.
|
|
3334
|
+
if (layout.icon.width >= ICON_SIZE_RANGE.min) {
|
|
3335
|
+
items.push(this.icon({ ...icon, size: layout.icon.width }, center.x, center.y, style.iconColor ?? labelColor));
|
|
3336
|
+
}
|
|
3337
|
+
const { band } = layout;
|
|
3338
|
+
const column = this.sizing.plainColumn(element);
|
|
3339
|
+
const x = align === "left" ? band.x : align === "right" ? band.x + band.width : boxCenter(band).x;
|
|
3340
|
+
this.lines(items, element, x, band.y - column.slots.top, this.sizing.plainBlock(element), align);
|
|
3341
|
+
return { kind: "group", id: element.id, role: "element", items };
|
|
3342
|
+
}
|
|
3343
|
+
const textCenter = shapeTextCenter(shape, box, this.sizing.shapeOptions(element));
|
|
3344
|
+
const block = this.sizing.textBlock(element);
|
|
3345
|
+
// Aligned text keeps the padding inside the shape's text area.
|
|
3346
|
+
const area = this.sizing.textArea(element, box, "slots");
|
|
3347
|
+
const inner = { left: area.x + theme.padding, right: area.x + area.width - theme.padding };
|
|
3348
|
+
let textX = align === "left" ? inner.left : align === "right" ? inner.right : textCenter.x;
|
|
3349
|
+
let anchor = align === "left" ? "start" : align === "right" ? "end" : "middle";
|
|
3350
|
+
if (icon?.position === "top" || icon?.position === "bottom") {
|
|
3351
|
+
const iconX = align === "left"
|
|
3352
|
+
? inner.left + icon.size / 2
|
|
3353
|
+
: align === "right"
|
|
3354
|
+
? inner.right - icon.size / 2
|
|
3355
|
+
: textCenter.x;
|
|
3356
|
+
items.push(this.icon(icon, iconX, textCenter.y + block.iconOffset, style.iconColor ?? labelColor));
|
|
3357
|
+
}
|
|
3358
|
+
else if (icon) {
|
|
3359
|
+
// Icon, gap and left-aligned text form one row, centred in the box unless aligned; an icon
|
|
3360
|
+
// on the right follows the text.
|
|
3361
|
+
const textWidth = this.sizing.textWidth(element);
|
|
3362
|
+
const row = icon.size + icon.gap + textWidth;
|
|
3363
|
+
const left = align === "left"
|
|
3364
|
+
? inner.left
|
|
3365
|
+
: align === "right"
|
|
3366
|
+
? inner.right - row
|
|
3367
|
+
: textCenter.x - row / 2;
|
|
3368
|
+
const right = icon.position === "right";
|
|
3369
|
+
const iconX = right ? left + textWidth + icon.gap + icon.size / 2 : left + icon.size / 2;
|
|
3370
|
+
items.push(this.icon(icon, iconX, textCenter.y, style.iconColor ?? labelColor));
|
|
3371
|
+
textX = right ? left : left + icon.size + icon.gap;
|
|
3372
|
+
anchor = "start";
|
|
3373
|
+
}
|
|
3374
|
+
this.lines(items, element, textX, textCenter.y, block, anchor);
|
|
3375
|
+
return { kind: "group", id: element.id, role: "element", items };
|
|
3376
|
+
}
|
|
3377
|
+
/**
|
|
3378
|
+
* The label and description lines of a box-like element, anchored at `x` (`align` names the
|
|
3379
|
+
* anchor's side) about a text centre `y`, with the offsets of `block`.
|
|
3380
|
+
*/
|
|
3381
|
+
lines(items, element, x, y, block, align) {
|
|
3382
|
+
const { theme, scale } = this;
|
|
3383
|
+
const style = this.styles.element(element);
|
|
3384
|
+
const font = this.sizing.labelFont(element);
|
|
3385
|
+
const anchor = align === "left"
|
|
3386
|
+
? "start"
|
|
3387
|
+
: align === "right"
|
|
3388
|
+
? "end"
|
|
3389
|
+
: align === "center"
|
|
3390
|
+
? "middle"
|
|
3391
|
+
: align;
|
|
3392
|
+
splitLines(element.label).forEach((line, i) => {
|
|
3393
|
+
items.push(this.text(x, y + block.labelOffsets[i], line, font.size, font.weight, style.color ?? theme.ink, anchor));
|
|
3394
|
+
});
|
|
3395
|
+
const descriptionLines = element.description ? splitLines(element.description) : [];
|
|
3396
|
+
descriptionLines.forEach((line, i) => {
|
|
3397
|
+
items.push(this.text(x, y + block.descriptionOffsets[i], line, scale.description, 400, style.color ?? theme.muted, anchor));
|
|
3398
|
+
});
|
|
3399
|
+
}
|
|
3400
|
+
/**
|
|
3401
|
+
* An icon shape: the icon alone in its box, framed only by a `fill`, `stroke` or `pattern`, or a
|
|
3402
|
+
* dashed placeholder when it has none to draw; its label outside the box.
|
|
3403
|
+
*/
|
|
3404
|
+
iconShape(element, box) {
|
|
3405
|
+
const style = this.styles.element(element);
|
|
3406
|
+
const items = [...this.freeFrame(element, box)];
|
|
3407
|
+
const icon = this.sizing.iconOf(element);
|
|
3408
|
+
if (!icon) {
|
|
3409
|
+
items.push(...this.placeholder(box, style.icon ?? element.id));
|
|
3410
|
+
}
|
|
3411
|
+
else if (icon.size >= ICON_SIZE_RANGE.min) {
|
|
3412
|
+
// A smaller icon, which a written size leaves in its frame, is reported, not drawn.
|
|
3413
|
+
// The icon is the shape: it fills its square, without the pack's optical scale.
|
|
3414
|
+
const center = boxCenter(box);
|
|
3415
|
+
const color = style.iconColor ?? style.color ?? this.theme.ink;
|
|
3416
|
+
items.push(this.icon({ ...icon, icon: { ...icon.icon, scale: 1 } }, center.x, center.y, color));
|
|
3417
|
+
}
|
|
3418
|
+
const label = this.sizing.outsideLabel(element, box);
|
|
3419
|
+
if (label) {
|
|
3420
|
+
this.lines(items, element, label.x, label.y, this.sizing.plainBlock(element), label.anchor);
|
|
3421
|
+
}
|
|
3422
|
+
return { kind: "group", id: element.id, role: "element", items };
|
|
3423
|
+
}
|
|
3424
|
+
note(element, box) {
|
|
3425
|
+
const { theme, scale } = this;
|
|
3426
|
+
const style = this.styles.element(element);
|
|
3427
|
+
const items = [];
|
|
3428
|
+
if (style.fill || style.stroke || style.pattern) {
|
|
3429
|
+
const outline = this.rect(box, cornerRadius(style.radius ?? theme.radius, box), style.fill ?? "none", style.stroke ?? "none", style.width ?? theme.strokeWidth, strokeDash(style.line));
|
|
3430
|
+
items.push(outline, ...this.overlay(style, outline));
|
|
3431
|
+
}
|
|
3432
|
+
const color = style.color ?? theme.muted;
|
|
3433
|
+
const icon = this.sizing.iconOf(element);
|
|
3434
|
+
const indent = icon ? icon.size + icon.gap : 0;
|
|
3435
|
+
if (icon) {
|
|
3436
|
+
items.push(this.icon(icon, box.x + icon.size / 2, box.y + scale.noteLine / 2, style.iconColor ?? color));
|
|
3437
|
+
}
|
|
3438
|
+
splitLines(element.label).forEach((line, i) => {
|
|
3439
|
+
items.push(this.text(box.x + indent, box.y + scale.noteLine / 2 + i * scale.noteLine, line, scale.note, 400, color, "start"));
|
|
3440
|
+
});
|
|
3441
|
+
return { kind: "group", id: element.id, role: "element", items };
|
|
3442
|
+
}
|
|
3443
|
+
tag(element, box) {
|
|
3444
|
+
const { theme, scale } = this;
|
|
3445
|
+
const style = this.styles.element(element);
|
|
3446
|
+
const fill = style.fill ?? theme.tag;
|
|
3447
|
+
const center = boxCenter(box);
|
|
3448
|
+
// A pill, upright when the tag is taller than wide.
|
|
3449
|
+
const outline = this.rect(box, cornerRadius(Infinity, box), fill, style.stroke ?? fill, style.width ?? theme.strokeWidth, strokeDash(style.line));
|
|
3450
|
+
const color = style.color ?? theme.tagText;
|
|
3451
|
+
const items = [outline, ...this.overlay(style, outline)];
|
|
3452
|
+
const icon = this.sizing.iconOf(element);
|
|
3453
|
+
if (icon) {
|
|
3454
|
+
const textWidth = this.metrics.width(element.label, "mono", 500, scale.tag);
|
|
3455
|
+
const left = center.x - (icon.size + icon.gap + textWidth) / 2;
|
|
3456
|
+
items.push(this.icon(icon, left + icon.size / 2, center.y, style.iconColor ?? color));
|
|
3457
|
+
items.push(this.text(left + icon.size + icon.gap, center.y, element.label, scale.tag, 500, color, "start", "mono"));
|
|
3458
|
+
}
|
|
3459
|
+
else {
|
|
3460
|
+
items.push(this.text(center.x, center.y, element.label, scale.tag, 500, color, "middle", "mono"));
|
|
3461
|
+
}
|
|
3462
|
+
return { kind: "group", id: element.id, role: "element", items };
|
|
3463
|
+
}
|
|
3464
|
+
relation(relation, line, start, end, label, curve) {
|
|
3465
|
+
const { theme, scale } = this;
|
|
3466
|
+
const style = this.styles.relation(relation);
|
|
3467
|
+
// An assumption is drawn dashed in the muted colour until a style says otherwise.
|
|
3468
|
+
const assumed = relation.assumption !== null;
|
|
3469
|
+
const stroke = style.stroke ?? (assumed ? theme.muted : theme.connector);
|
|
3470
|
+
const strokeWidth = style.width ?? theme.connectorWidth;
|
|
3471
|
+
const dashed = strokeDash(style.line ?? (assumed ? "dashed" : relation.line));
|
|
3472
|
+
const points = (list) => list.map((p) => ({ x: round(p.x), y: round(p.y) }));
|
|
3473
|
+
// A straight connector passes under the titles of groups it crosses (not its own ends'). It
|
|
3474
|
+
// is drawn from beyond the labels its own ends draw outside their boxes (see `underLabel`).
|
|
3475
|
+
const holes = this.titles
|
|
3476
|
+
.filter((t) => t.id !== relation.from && t.id !== relation.to)
|
|
3477
|
+
.map((t) => t.box);
|
|
3478
|
+
const pieces = curve ? [] : splitAround(line, holes);
|
|
3479
|
+
const items = curve
|
|
3480
|
+
? [{ kind: "path", d: curve, fill: "none", stroke, strokeWidth, dashed }]
|
|
3481
|
+
: pieces.map((piece) => ({
|
|
3482
|
+
kind: "polyline",
|
|
3483
|
+
points: points(piece),
|
|
3484
|
+
stroke,
|
|
3485
|
+
strokeWidth,
|
|
3486
|
+
dashed,
|
|
3487
|
+
}));
|
|
3488
|
+
// The line first, so a moving dash finds it; the heads after it; the label last.
|
|
3489
|
+
items.push(...markerItems(start, stroke, strokeWidth), ...markerItems(end, stroke, strokeWidth));
|
|
3490
|
+
if (label) {
|
|
3491
|
+
const fill = style.fill ?? theme.surface;
|
|
3492
|
+
const techSize = technologySize(scale.relationLabel);
|
|
3493
|
+
const techLine = label.technology ? Math.round(techSize * 1.25) : 0;
|
|
3494
|
+
const box = label.box;
|
|
3495
|
+
// The first line keeps the single-line position; the technology goes below it.
|
|
3496
|
+
const center = { x: box.x + box.width / 2, y: box.y + (box.height - techLine) / 2 };
|
|
3497
|
+
const color = style.color ?? theme.ink;
|
|
3498
|
+
const pill = [
|
|
3499
|
+
this.rect(label.box, Math.min(label.box.height, scale.relationLabelHeight) / 2, fill, style.stroke ?? (style.fill ? style.fill : theme.line), theme.strokeWidth, false),
|
|
3500
|
+
];
|
|
3501
|
+
const icon = this.sizing.relationIcon(relation);
|
|
3502
|
+
if (icon && label.text === "") {
|
|
3503
|
+
pill.push(this.icon(icon, center.x, center.y, style.iconColor ?? color));
|
|
3504
|
+
}
|
|
3505
|
+
else if (icon) {
|
|
3506
|
+
const textWidth = this.metrics.width(label.text, "body", 500, scale.relationLabel);
|
|
3507
|
+
const left = center.x - (icon.size + icon.gap + textWidth) / 2;
|
|
3508
|
+
pill.push(this.icon(icon, left + icon.size / 2, center.y, style.iconColor ?? color));
|
|
3509
|
+
pill.push(this.text(left + icon.size + icon.gap, center.y, label.text, scale.relationLabel, 500, color, "start"));
|
|
3510
|
+
}
|
|
3511
|
+
else if (label.text !== "") {
|
|
3512
|
+
pill.push(this.text(center.x, center.y, label.text, scale.relationLabel, 500, color));
|
|
3513
|
+
}
|
|
3514
|
+
if (label.technology) {
|
|
3515
|
+
const y = label.text === "" && !icon ? box.y + box.height / 2 : center.y + techLine;
|
|
3516
|
+
pill.push(this.text(center.x, y, label.technology, techSize, 400, style.color ?? theme.muted));
|
|
3517
|
+
}
|
|
3518
|
+
items.push({ kind: "group", id: relation.key, role: "label", items: pill });
|
|
3519
|
+
}
|
|
3520
|
+
return { kind: "group", id: relation.key, role: "relation", items, track: points(line) };
|
|
3521
|
+
}
|
|
3522
|
+
/** The brand strip: hairline, mark, wordmark and diagram title, spanning the canvas. */
|
|
3523
|
+
frame(width, stripTop, title) {
|
|
3524
|
+
return brandFrame(this.theme, this.scale, this.metrics, width, stripTop, title);
|
|
3525
|
+
}
|
|
3526
|
+
}
|
|
3527
|
+
//# sourceMappingURL=layout.js.map
|