@johnoscott/isometric-engine 0.0.0-stage → 0.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/EDITOR-GUIDE.md +317 -0
- package/LICENSE +21 -0
- package/LICENSES/ajv.txt +22 -0
- package/LICENSES/lucide-static.txt +43 -0
- package/LICENSES/yaml.txt +13 -0
- package/README.md +62 -2
- package/assets/app.css +265 -0
- package/assets/default-theme.yaml +99 -0
- package/assets/icons.svg +1 -0
- package/assets/marks.css +35 -0
- package/assets/skin.css +181 -0
- package/iso-editor.d.ts +4 -0
- package/iso-editor.js +550 -0
- package/iso-render.d.ts +3 -0
- package/iso-render.js +388 -0
- package/iso-systems.d.ts +4 -0
- package/iso-systems.js +389 -0
- package/js/iso-core/compile-schema.js +22 -0
- package/js/iso-core/depth.js +55 -0
- package/js/iso-core/errors.js +65 -0
- package/js/iso-core/glyphs.js +100 -0
- package/js/iso-core/index.js +55 -0
- package/js/iso-core/kernel.js +158 -0
- package/js/iso-core/metrics.json +1 -0
- package/js/iso-core/package.json +6 -0
- package/js/iso-core/parse.js +79 -0
- package/js/iso-core/schema-errors.js +170 -0
- package/js/iso-core/shims/crypto.js +125 -0
- package/js/iso-core/shims/path.js +49 -0
- package/js/iso-core/svg-dom.js +144 -0
- package/js/iso-core/theme.js +48 -0
- package/js/iso-core/vdom.js +48 -0
- package/js/iso-editor/adapter.js +0 -0
- package/js/iso-editor/clipboard.js +152 -0
- package/js/iso-editor/commands.js +173 -0
- package/js/iso-editor/complete.js +55 -0
- package/js/iso-editor/doc.js +182 -0
- package/js/iso-editor/index.js +105 -0
- package/js/iso-editor/model.js +210 -0
- package/js/iso-editor/outline.js +111 -0
- package/js/iso-editor/package.json +6 -0
- package/js/iso-editor/path.js +19 -0
- package/js/iso-editor/providers.js +96 -0
- package/js/iso-editor/schema-nav.js +117 -0
- package/js/iso-editor/splice.js +441 -0
- package/js/iso-editor/storage-keys.js +6 -0
- package/js/iso-editor/ui/actions.js +116 -0
- package/js/iso-editor/ui/canvas-layer.js +0 -0
- package/js/iso-editor/ui/canvas.js +1205 -0
- package/js/iso-editor/ui/clip.js +133 -0
- package/js/iso-editor/ui/dialog.js +26 -0
- package/js/iso-editor/ui/flat-canvas.js +54 -0
- package/js/iso-editor/ui/icons.js +15 -0
- package/js/iso-editor/ui/keys.js +26 -0
- package/js/iso-editor/ui/later.js +28 -0
- package/js/iso-editor/ui/layer.js +11 -0
- package/js/iso-editor/ui/menu.js +105 -0
- package/js/iso-editor/ui/monaco-entry.js +2 -0
- package/js/iso-editor/ui/mount-kit.js +0 -0
- package/js/iso-editor/ui/mount.js +767 -0
- package/js/iso-editor/ui/pane.js +0 -0
- package/js/iso-editor/ui/part.js +0 -0
- package/js/iso-editor/ui/plain-icons.js +34 -0
- package/js/iso-editor/ui/props.js +314 -0
- package/js/iso-editor/ui/scope-css.js +81 -0
- package/js/iso-editor/ui/setup.js +145 -0
- package/js/iso-editor/ui/text.js +323 -0
- package/js/iso-editor/ui/tree.js +728 -0
- package/js/iso-editor/validate.js +33 -0
- package/js/iso-editor/window/protocol.js +8 -0
- package/js/iso-objects/assemble.js +43 -0
- package/js/iso-objects/assets.js +88 -0
- package/js/iso-objects/board.js +162 -0
- package/js/iso-objects/compile.js +60 -0
- package/js/iso-objects/css.js +154 -0
- package/js/iso-objects/flat.js +696 -0
- package/js/iso-objects/focus.js +16 -0
- package/js/iso-objects/geometry.js +76 -0
- package/js/iso-objects/index.js +93 -0
- package/js/iso-objects/kinds.js +12 -0
- package/js/iso-objects/labels.js +325 -0
- package/js/iso-objects/package.json +6 -0
- package/js/iso-objects/place.js +347 -0
- package/js/iso-objects/route.js +461 -0
- package/js/iso-objects/scene.js +350 -0
- package/js/iso-objects/steps.js +246 -0
- package/js/iso-objects/types.js +0 -0
- package/js/iso-systems/catalog.js +42 -0
- package/js/iso-systems/compile.js +34 -0
- package/js/iso-systems/css.js +8 -0
- package/js/iso-systems/index.js +16 -0
- package/js/iso-systems/nodes.js +124 -0
- package/js/iso-systems/package.json +6 -0
- package/js/iso-systems/schema.json +311 -0
- package/js/iso-systems/validate.js +35 -0
- package/js/iso-viewer/bundle.js +20 -0
- package/js/iso-viewer/card-motion.js +228 -0
- package/js/iso-viewer/dom.js +14 -0
- package/js/iso-viewer/dual-view.js +212 -0
- package/js/iso-viewer/flat-view.js +124 -0
- package/js/iso-viewer/history.js +990 -0
- package/js/iso-viewer/index.js +98 -0
- package/js/iso-viewer/morph.js +192 -0
- package/js/iso-viewer/overlay.js +199 -0
- package/js/iso-viewer/package.json +6 -0
- package/js/iso-viewer/page.js +138 -0
- package/js/iso-viewer/player.js +409 -0
- package/js/iso-viewer/still.js +81 -0
- package/js/iso-viewer/timeline-contract.js +14 -0
- package/js/iso-viewer/timeline-engine.js +475 -0
- package/js/iso-viewer/timeline-export.js +36 -0
- package/js/iso-viewer/timeline-scale.js +116 -0
- package/js/iso-viewer/timeline-scope.js +58 -0
- package/js/iso-viewer/timeline-search.js +216 -0
- package/js/iso-viewer/timeline-time.js +55 -0
- package/js/iso-viewer/view-css.js +68 -0
- package/js/iso-viewer/viewport.js +177 -0
- package/package.json +502 -4
- package/src/iso-core/compile-schema.ts +23 -0
- package/src/iso-core/depth.ts +56 -0
- package/src/iso-core/errors.ts +73 -0
- package/src/iso-core/glyphs.ts +108 -0
- package/src/iso-core/index.ts +27 -0
- package/src/iso-core/kernel.ts +163 -0
- package/src/iso-core/metrics.json +1 -0
- package/src/iso-core/package.json +6 -0
- package/src/iso-core/parse.ts +91 -0
- package/src/iso-core/schema-errors.ts +163 -0
- package/src/iso-core/shims/crypto.ts +37 -0
- package/src/iso-core/shims/path.ts +24 -0
- package/src/iso-core/svg-dom.ts +103 -0
- package/src/iso-core/theme.ts +64 -0
- package/src/iso-core/vdom.ts +41 -0
- package/src/iso-editor/adapter.ts +128 -0
- package/src/iso-editor/clipboard.ts +163 -0
- package/src/iso-editor/commands.ts +191 -0
- package/src/iso-editor/complete.ts +73 -0
- package/src/iso-editor/doc.ts +207 -0
- package/src/iso-editor/index.ts +67 -0
- package/src/iso-editor/model.ts +234 -0
- package/src/iso-editor/outline.ts +126 -0
- package/src/iso-editor/package.json +6 -0
- package/src/iso-editor/path.ts +18 -0
- package/src/iso-editor/providers.ts +113 -0
- package/src/iso-editor/schema-nav.ts +129 -0
- package/src/iso-editor/splice.ts +472 -0
- package/src/iso-editor/storage-keys.ts +6 -0
- package/src/iso-editor/ui/actions.ts +119 -0
- package/src/iso-editor/ui/app.css +230 -0
- package/src/iso-editor/ui/canvas-layer.ts +56 -0
- package/src/iso-editor/ui/canvas.ts +1071 -0
- package/src/iso-editor/ui/clip.ts +97 -0
- package/src/iso-editor/ui/dialog.ts +25 -0
- package/src/iso-editor/ui/flat-canvas.ts +51 -0
- package/src/iso-editor/ui/icons.ts +21 -0
- package/src/iso-editor/ui/keys.ts +30 -0
- package/src/iso-editor/ui/later.ts +22 -0
- package/src/iso-editor/ui/layer.ts +7 -0
- package/src/iso-editor/ui/marks.css +35 -0
- package/src/iso-editor/ui/menu.ts +67 -0
- package/src/iso-editor/ui/monaco-entry.ts +6 -0
- package/src/iso-editor/ui/mount-kit.ts +35 -0
- package/src/iso-editor/ui/mount.ts +684 -0
- package/src/iso-editor/ui/pane.ts +45 -0
- package/src/iso-editor/ui/part.ts +45 -0
- package/src/iso-editor/ui/plain-icons.ts +83 -0
- package/src/iso-editor/ui/props.ts +347 -0
- package/src/iso-editor/ui/scope-css.ts +65 -0
- package/src/iso-editor/ui/setup.ts +144 -0
- package/src/iso-editor/ui/skin.css +181 -0
- package/src/iso-editor/ui/text.ts +262 -0
- package/src/iso-editor/ui/tree.ts +657 -0
- package/src/iso-editor/validate.ts +62 -0
- package/src/iso-editor/window/protocol.ts +33 -0
- package/src/iso-objects/assemble.ts +57 -0
- package/src/iso-objects/assets.ts +122 -0
- package/src/iso-objects/board.ts +169 -0
- package/src/iso-objects/compile.ts +76 -0
- package/src/iso-objects/css.ts +163 -0
- package/src/iso-objects/flat.ts +653 -0
- package/src/iso-objects/focus.ts +14 -0
- package/src/iso-objects/geometry.ts +86 -0
- package/src/iso-objects/index.ts +33 -0
- package/src/iso-objects/kinds.ts +56 -0
- package/src/iso-objects/labels.ts +310 -0
- package/src/iso-objects/package.json +6 -0
- package/src/iso-objects/place.ts +318 -0
- package/src/iso-objects/route.ts +444 -0
- package/src/iso-objects/scene.ts +376 -0
- package/src/iso-objects/steps.ts +234 -0
- package/src/iso-objects/types.ts +68 -0
- package/src/iso-systems/catalog.ts +31 -0
- package/src/iso-systems/compile.ts +39 -0
- package/src/iso-systems/css.ts +9 -0
- package/src/iso-systems/index.ts +12 -0
- package/src/iso-systems/nodes.ts +126 -0
- package/src/iso-systems/package.json +6 -0
- package/src/iso-systems/schema.json +311 -0
- package/src/iso-systems/validate.ts +34 -0
- package/src/iso-viewer/bundle.ts +15 -0
- package/src/iso-viewer/card-motion.ts +237 -0
- package/src/iso-viewer/dom.ts +13 -0
- package/src/iso-viewer/dual-view.ts +212 -0
- package/src/iso-viewer/flat-view.ts +127 -0
- package/src/iso-viewer/history.ts +781 -0
- package/src/iso-viewer/index.ts +40 -0
- package/src/iso-viewer/morph.ts +191 -0
- package/src/iso-viewer/overlay.ts +212 -0
- package/src/iso-viewer/package.json +6 -0
- package/src/iso-viewer/page.ts +150 -0
- package/src/iso-viewer/player.ts +321 -0
- package/src/iso-viewer/still.ts +74 -0
- package/src/iso-viewer/timeline-contract.ts +342 -0
- package/src/iso-viewer/timeline-engine.ts +420 -0
- package/src/iso-viewer/timeline-export.ts +31 -0
- package/src/iso-viewer/timeline-scale.ts +129 -0
- package/src/iso-viewer/timeline-scope.ts +56 -0
- package/src/iso-viewer/timeline-search.ts +198 -0
- package/src/iso-viewer/timeline-time.ts +54 -0
- package/src/iso-viewer/view-css.ts +59 -0
- package/src/iso-viewer/viewport.ts +142 -0
- package/types/iso-core/compile-schema.d.ts +11 -0
- package/types/iso-core/depth.d.ts +6 -0
- package/types/iso-core/errors.d.ts +24 -0
- package/types/iso-core/glyphs.d.ts +19 -0
- package/types/iso-core/index.d.ts +17 -0
- package/types/iso-core/kernel.d.ts +130 -0
- package/types/iso-core/metrics.json +1 -0
- package/types/iso-core/parse.d.ts +18 -0
- package/types/iso-core/schema-errors.d.ts +14 -0
- package/types/iso-core/shims/crypto.d.ts +9 -0
- package/types/iso-core/svg-dom.d.ts +15 -0
- package/types/iso-core/theme.d.ts +38 -0
- package/types/iso-core/vdom.d.ts +26 -0
- package/types/iso-editor/adapter.d.ts +145 -0
- package/types/iso-editor/clipboard.d.ts +23 -0
- package/types/iso-editor/commands.d.ts +32 -0
- package/types/iso-editor/complete.d.ts +25 -0
- package/types/iso-editor/doc.d.ts +50 -0
- package/types/iso-editor/index.d.ts +57 -0
- package/types/iso-editor/model.d.ts +96 -0
- package/types/iso-editor/outline.d.ts +28 -0
- package/types/iso-editor/path.d.ts +7 -0
- package/types/iso-editor/providers.d.ts +43 -0
- package/types/iso-editor/schema-nav.d.ts +34 -0
- package/types/iso-editor/splice.d.ts +53 -0
- package/types/iso-editor/storage-keys.d.ts +5 -0
- package/types/iso-editor/ui/actions.d.ts +23 -0
- package/types/iso-editor/ui/canvas-layer.d.ts +65 -0
- package/types/iso-editor/ui/canvas.d.ts +279 -0
- package/types/iso-editor/ui/clip.d.ts +14 -0
- package/types/iso-editor/ui/dialog.d.ts +7 -0
- package/types/iso-editor/ui/flat-canvas.d.ts +2 -0
- package/types/iso-editor/ui/icons.d.ts +5 -0
- package/types/iso-editor/ui/keys.d.ts +7 -0
- package/types/iso-editor/ui/later.d.ts +12 -0
- package/types/iso-editor/ui/layer.d.ts +2 -0
- package/types/iso-editor/ui/menu.d.ts +21 -0
- package/types/iso-editor/ui/mount-kit.d.ts +31 -0
- package/types/iso-editor/ui/mount.d.ts +146 -0
- package/types/iso-editor/ui/pane.d.ts +60 -0
- package/types/iso-editor/ui/part.d.ts +40 -0
- package/types/iso-editor/ui/plain-icons.d.ts +10 -0
- package/types/iso-editor/ui/props.d.ts +57 -0
- package/types/iso-editor/ui/scope-css.d.ts +5 -0
- package/types/iso-editor/ui/setup.d.ts +40 -0
- package/types/iso-editor/ui/text.d.ts +19 -0
- package/types/iso-editor/ui/tree.d.ts +100 -0
- package/types/iso-editor/validate.d.ts +36 -0
- package/types/iso-editor/window/protocol.d.ts +60 -0
- package/types/iso-objects/assemble.d.ts +35 -0
- package/types/iso-objects/assets.d.ts +49 -0
- package/types/iso-objects/board.d.ts +36 -0
- package/types/iso-objects/compile.d.ts +35 -0
- package/types/iso-objects/css.d.ts +16 -0
- package/types/iso-objects/flat.d.ts +130 -0
- package/types/iso-objects/focus.d.ts +1 -0
- package/types/iso-objects/geometry.d.ts +59 -0
- package/types/iso-objects/index.d.ts +26 -0
- package/types/iso-objects/kinds.d.ts +53 -0
- package/types/iso-objects/labels.d.ts +92 -0
- package/types/iso-objects/place.d.ts +25 -0
- package/types/iso-objects/route.d.ts +67 -0
- package/types/iso-objects/scene.d.ts +63 -0
- package/types/iso-objects/steps.d.ts +90 -0
- package/types/iso-objects/types.d.ts +140 -0
- package/types/iso-systems/catalog.d.ts +4 -0
- package/types/iso-systems/compile.d.ts +6 -0
- package/types/iso-systems/css.d.ts +3 -0
- package/types/iso-systems/index.d.ts +5 -0
- package/types/iso-systems/nodes.d.ts +3 -0
- package/types/iso-viewer/card-motion.d.ts +56 -0
- package/types/iso-viewer/dom.d.ts +1 -0
- package/types/iso-viewer/dual-view.d.ts +44 -0
- package/types/iso-viewer/flat-view.d.ts +25 -0
- package/types/iso-viewer/history.d.ts +91 -0
- package/types/iso-viewer/index.d.ts +31 -0
- package/types/iso-viewer/morph.d.ts +28 -0
- package/types/iso-viewer/overlay.d.ts +135 -0
- package/types/iso-viewer/page.d.ts +13 -0
- package/types/iso-viewer/still.d.ts +26 -0
- package/types/iso-viewer/timeline-contract.d.ts +345 -0
- package/types/iso-viewer/timeline-engine.d.ts +20 -0
- package/types/iso-viewer/timeline-export.d.ts +5 -0
- package/types/iso-viewer/timeline-scale.d.ts +44 -0
- package/types/iso-viewer/timeline-scope.d.ts +33 -0
- package/types/iso-viewer/timeline-search.d.ts +52 -0
- package/types/iso-viewer/timeline-time.d.ts +8 -0
- package/types/iso-viewer/view-css.d.ts +7 -0
- package/types/iso-viewer/viewport.d.ts +50 -0
|
@@ -0,0 +1,653 @@
|
|
|
1
|
+
// The 2D view's layout (0.6.0): a board (cards on a grid of cells, panels over cell ranges, links between them) laid
|
|
2
|
+
// out flat, as its own layout rather than a projection of the 3D one. Pure: no DOM; text is measured by the caller
|
|
3
|
+
// (the browser measures rendered text, Node uses L0's font metrics), so a layout is the same every time it is made.
|
|
4
|
+
//
|
|
5
|
+
// The rules, as settled for the 2D view:
|
|
6
|
+
// - a card is one rounded rect (W × H); a panel is a rounded area with PAD on every side and a TITLE strip at its
|
|
7
|
+
// bottom left;
|
|
8
|
+
// - the gap between cards inside a panel is GX × GY; between two different containers there is a free channel CH;
|
|
9
|
+
// the extra at a column (row) boundary is the largest any row (column) needs there: CH, plus the PAD (and, below a
|
|
10
|
+
// panel, its TITLE) of every panel exited or entered there. With one level of panels that is exactly CH + PAD
|
|
11
|
+
// (+ PAD) - GX;
|
|
12
|
+
// - a card is placed by its innermost panel: evenly, between that panel's first and last column (row), so a panel
|
|
13
|
+
// that spans a channel boundary keeps its cards evenly apart;
|
|
14
|
+
// - links are orthogonal, card edge to card edge, never through a card or a panel's title: the cheapest of straight,
|
|
15
|
+
// L, Z (VHV, HVH) and four-bend candidates through lanes at gap midpoints (±12, ±24) and the outer margins, ports
|
|
16
|
+
// ±22/±44 off centre; cost = length + 70 a bend + 400 an overlap with a drawn line + 300 a crossing of a panel that
|
|
17
|
+
// holds neither end + 120 a panel border crossed + 200 running within 16px of a panel edge;
|
|
18
|
+
// - an end may be a panel; a link from something to a panel that holds it ends on that panel's inner border;
|
|
19
|
+
// - a link's label goes beside its longest segment (then the others), clear of cards, titles, labels, lines and
|
|
20
|
+
// panel borders; if no spot is clear, the least bad one.
|
|
21
|
+
// Ties go to the first candidate in a fixed order (link order, then the candidate order below), so a layout is
|
|
22
|
+
// reproducible to the byte.
|
|
23
|
+
|
|
24
|
+
import { textWidth, type Font } from '../iso-core/kernel.ts';
|
|
25
|
+
|
|
26
|
+
/** a card: one thing, at a cell of the grid ([column, row]) */
|
|
27
|
+
export interface BoardCard { id: string; label: string; sub?: string; role?: string; ghost?: boolean; cell: [number, number] }
|
|
28
|
+
/** a panel: a container over a range of cells, [c0, r0, c1, r1] inclusive; panels nest by containment */
|
|
29
|
+
export interface BoardPanel { id: string; label?: string; sub?: string; role?: string; cells: [number, number, number, number] }
|
|
30
|
+
/** a link between two cards or panels; aOn / bOn: draw that end to a panel (one holding it, or any) instead */
|
|
31
|
+
export interface BoardLink { id?: string; a: string; b: string; kind?: string; label?: string; aOn?: string; bOn?: string }
|
|
32
|
+
/** the scene description both views are drawn from */
|
|
33
|
+
export interface Board { cards: BoardCard[]; panels?: BoardPanel[]; links?: BoardLink[] }
|
|
34
|
+
|
|
35
|
+
export interface FlatRect { l: number; t: number; r: number; b: number }
|
|
36
|
+
export type FlatPoint = [number, number];
|
|
37
|
+
/** the fonts a 2D layout measures: a card's name and sub, a panel's title and its subtitle, a link's label */
|
|
38
|
+
export type FlatFont = 'name' | 'sub' | 'title' | 'titleSub' | 'label';
|
|
39
|
+
export type Measure = (text: string, font: FlatFont) => number;
|
|
40
|
+
|
|
41
|
+
export interface FlatOptions {
|
|
42
|
+
W: number; H: number; GX: number; GY: number; PAD: number; TITLE: number; CH: number;
|
|
43
|
+
/** card and panel corner radii */
|
|
44
|
+
cardRx: number; panelRx: number;
|
|
45
|
+
/** the cost model */
|
|
46
|
+
bend: number; overlap: number; crossPanel: number; border: number; nearEdge: number;
|
|
47
|
+
/** the margin round the drawing */
|
|
48
|
+
margin: number;
|
|
49
|
+
/** wrap a drawing wider than this (width / height) onto further shelves (wrapBoard); off by default */
|
|
50
|
+
wrap?: { aspect: number };
|
|
51
|
+
}
|
|
52
|
+
export const FLAT_DEFAULTS: Readonly<FlatOptions> = Object.freeze({
|
|
53
|
+
W: 178, H: 58, GX: 44, GY: 44, PAD: 22, TITLE: 30, CH: 96, cardRx: 9, panelRx: 14,
|
|
54
|
+
bend: 70, overlap: 400, crossPanel: 300, border: 120, nearEdge: 200, margin: 16,
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
export interface FlatCard extends BoardCard { rect: FlatRect }
|
|
58
|
+
export interface FlatPanel extends BoardPanel {
|
|
59
|
+
rect: FlatRect;
|
|
60
|
+
/** the title strip's box (what a link may not cross) */
|
|
61
|
+
title: FlatRect;
|
|
62
|
+
/** the title's baseline */
|
|
63
|
+
titleAt: FlatPoint;
|
|
64
|
+
/** the panel it sits in, if any */
|
|
65
|
+
parent?: string;
|
|
66
|
+
depth: number;
|
|
67
|
+
}
|
|
68
|
+
export interface FlatLabel { x: number; y: number; anchor: 'start' | 'middle' | 'end'; box: FlatRect; text: string }
|
|
69
|
+
export interface FlatLink extends BoardLink { id: string; points: FlatPoint[]; at?: FlatLabel; routed: boolean }
|
|
70
|
+
export interface FlatLayout {
|
|
71
|
+
cards: FlatCard[]; panels: FlatPanel[]; links: FlatLink[];
|
|
72
|
+
/** everything drawn, with the margin */
|
|
73
|
+
bounds: FlatRect;
|
|
74
|
+
options: FlatOptions;
|
|
75
|
+
/** warnings: a link with no clear route (drawn the least bad way) */
|
|
76
|
+
warnings: string[];
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** the 2D view's fonts (its stylesheet's: src/iso-viewer/flat-view.ts), as L0's measured metrics have them */
|
|
80
|
+
export const FLAT_FONTS: Record<FlatFont, Font> = {
|
|
81
|
+
name: { key: 'sans600_12', size: 13 },
|
|
82
|
+
sub: { key: 'sans400_13', size: 10 },
|
|
83
|
+
title: { key: 'sans700_13', size: 12, spacing: 0.02 },
|
|
84
|
+
titleSub: { key: 'sans400_13', size: 10.5 },
|
|
85
|
+
label: { key: 'sans600_12', size: 10.5 },
|
|
86
|
+
};
|
|
87
|
+
/** text measured with L0's font metrics: the same in Node and every browser (stills and tests); a page measures its
|
|
88
|
+
* rendered text instead (flat-view.ts: measureIn) */
|
|
89
|
+
export const metricsMeasure: Measure = (text, font) => textWidth(text, FLAT_FONTS[font]);
|
|
90
|
+
|
|
91
|
+
/** what is wrong with a board, as messages; layoutFlat refuses a board with any */
|
|
92
|
+
export function boardErrors(board: Board): string[] {
|
|
93
|
+
const out: string[] = [], seen = new Map<string, string>();
|
|
94
|
+
const name = (what: string, id: unknown) => {
|
|
95
|
+
if (typeof id !== 'string' || !id) { out.push(`${what} has no id`); return; }
|
|
96
|
+
const was = seen.get(id);
|
|
97
|
+
if (was) out.push(`duplicate id "${id}": a ${was} and a ${what} (cards and panels share one set of ids)`);
|
|
98
|
+
else seen.set(id, what);
|
|
99
|
+
};
|
|
100
|
+
for (const c of board.cards) {
|
|
101
|
+
name('card', c.id);
|
|
102
|
+
if (!Array.isArray(c.cell) || c.cell.length !== 2 || !c.cell.every(v => Number.isInteger(v) && v >= 0)) out.push(`card "${c.id}": cell must be [column, row], whole numbers from 0`);
|
|
103
|
+
}
|
|
104
|
+
for (const p of board.panels ?? []) {
|
|
105
|
+
name('panel', p.id);
|
|
106
|
+
const k = p.cells;
|
|
107
|
+
if (!Array.isArray(k) || k.length !== 4 || !k.every(v => Number.isInteger(v) && v >= 0) || k[2] < k[0] || k[3] < k[1]) out.push(`panel "${p.id}": cells must be [c0, r0, c1, r1] with c0 ≤ c1 and r0 ≤ r1`);
|
|
108
|
+
}
|
|
109
|
+
const cells = new Map<string, string>();
|
|
110
|
+
for (const c of board.cards) {
|
|
111
|
+
const key = String(c.cell);
|
|
112
|
+
if (cells.has(key)) out.push(`cards "${cells.get(key)}" and "${c.id}" are both at cell [${key}]`);
|
|
113
|
+
else cells.set(key, c.id);
|
|
114
|
+
}
|
|
115
|
+
const P = (board.panels ?? []).filter(p => Array.isArray(p.cells) && p.cells.length === 4);
|
|
116
|
+
for (let i = 0; i < P.length; i++) for (let j = i + 1; j < P.length; j++) {
|
|
117
|
+
const a = P[i].cells, b = P[j].cells;
|
|
118
|
+
const meet = a[0] <= b[2] && b[0] <= a[2] && a[1] <= b[3] && b[1] <= a[3];
|
|
119
|
+
if (meet && !within(a, b) && !within(b, a)) out.push(`panels "${P[i].id}" and "${P[j].id}" overlap without one holding the other`);
|
|
120
|
+
}
|
|
121
|
+
const ends = new Set([...board.cards.map(c => c.id), ...P.map(p => p.id)]), panelIds = new Set(P.map(p => p.id));
|
|
122
|
+
(board.links ?? []).forEach((l, i) => {
|
|
123
|
+
const who = `link ${l.id ? `"${l.id}"` : i}`;
|
|
124
|
+
for (const k of ['a', 'b'] as const) if (!ends.has(l[k])) out.push(`${who}: ${k} "${l[k]}" is no card or panel`);
|
|
125
|
+
for (const k of ['aOn', 'bOn'] as const) if (l[k] !== undefined && !panelIds.has(l[k]!)) out.push(`${who}: ${k} "${l[k]}" is no panel`);
|
|
126
|
+
if (l.id !== undefined) name('link', l.id);
|
|
127
|
+
});
|
|
128
|
+
return out;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** b holds a (cell ranges) */
|
|
132
|
+
const within = (a: number[], b: number[]) => a[0] >= b[0] && a[2] <= b[2] && a[1] >= b[1] && a[3] <= b[3];
|
|
133
|
+
|
|
134
|
+
/** where a board's cards and panels go on a grid with the given sizes and gaps (the 2D layout's, in px; the 3D
|
|
135
|
+
* drawing's, in grid units): each card's top-left corner, each panel's rect, and how the panels nest */
|
|
136
|
+
export function placeBoard(board: Board, o: Pick<FlatOptions, 'W' | 'H' | 'GX' | 'GY' | 'PAD' | 'TITLE' | 'CH'>) {
|
|
137
|
+
const { W, H, GX, GY, PAD, TITLE, CH } = o;
|
|
138
|
+
const PL = board.panels ?? [];
|
|
139
|
+
// ---- containment: each panel's parent (the smallest other panel holding it; of equal ranges, the earlier holds) ----
|
|
140
|
+
const area = (k: number[]) => (k[2] - k[0] + 1) * (k[3] - k[1] + 1);
|
|
141
|
+
const holds = (outer: number, inner: number) => {
|
|
142
|
+
const a = PL[outer].cells, b = PL[inner].cells;
|
|
143
|
+
if (!within(b, a)) return false;
|
|
144
|
+
return area(a) > area(b) || outer < inner;
|
|
145
|
+
};
|
|
146
|
+
const parentIx = PL.map((_, i) => {
|
|
147
|
+
let best = -1;
|
|
148
|
+
PL.forEach((_, j) => { if (j !== i && holds(j, i) && (best < 0 || holds(best, j))) best = j; });
|
|
149
|
+
return best;
|
|
150
|
+
});
|
|
151
|
+
const depthOf = (i: number): number => (parentIx[i] < 0 ? 0 : 1 + depthOf(parentIx[i]));
|
|
152
|
+
/** the panels holding a cell, outermost first */
|
|
153
|
+
const chainOf = (i: number, j: number): number[] => {
|
|
154
|
+
let inner = -1;
|
|
155
|
+
PL.forEach((p, n) => {
|
|
156
|
+
const k = p.cells;
|
|
157
|
+
if (i >= k[0] && i <= k[2] && j >= k[1] && j <= k[3] && (inner < 0 || holds(inner, n))) inner = n;
|
|
158
|
+
});
|
|
159
|
+
const c: number[] = [];
|
|
160
|
+
for (let n = inner; n >= 0; n = parentIx[n]) c.unshift(n);
|
|
161
|
+
return c;
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
// ---- the grid: columns and rows, with the extra at each boundary ----
|
|
165
|
+
const NI = Math.max(1, ...board.cards.map(c => c.cell[0] + 1), ...PL.map(p => p.cells[2] + 1));
|
|
166
|
+
const NJ = Math.max(1, ...board.cards.map(c => c.cell[1] + 1), ...PL.map(p => p.cells[3] + 1));
|
|
167
|
+
const chains: number[][][] = [];
|
|
168
|
+
for (let i = 0; i < NI; i++) { chains.push([]); for (let j = 0; j < NJ; j++) chains[i].push(chainOf(i, j)); }
|
|
169
|
+
const boundary = (A: number[], B: number[], below: boolean): number => {
|
|
170
|
+
const out = A.filter(n => !B.includes(n)), into = B.filter(n => !A.includes(n));
|
|
171
|
+
if (!out.length && !into.length) return 0;
|
|
172
|
+
return out.length * (PAD + (below ? TITLE : 0)) + into.length * PAD + CH;
|
|
173
|
+
};
|
|
174
|
+
const exX = new Array(NI + 1).fill(0), exY = new Array(NJ + 1).fill(0);
|
|
175
|
+
for (let j = 0; j < NJ; j++) for (let i = 1; i < NI; i++) {
|
|
176
|
+
const need = boundary(chains[i - 1][j], chains[i][j], false);
|
|
177
|
+
if (need) exX[i] = Math.max(exX[i], need - GX);
|
|
178
|
+
}
|
|
179
|
+
for (let i = 0; i < NI; i++) for (let j = 1; j < NJ; j++) {
|
|
180
|
+
const need = boundary(chains[i][j - 1], chains[i][j], true);
|
|
181
|
+
if (need) exY[j] = Math.max(exY[j], need - GY);
|
|
182
|
+
}
|
|
183
|
+
const bx = (i: number) => { let v = i * (W + GX); for (let k = 1; k <= i; k++) v += exX[k]; return v; };
|
|
184
|
+
const by = (j: number) => { let v = j * (H + GY); for (let k = 1; k <= j; k++) v += exY[k]; return v; };
|
|
185
|
+
const lerp = (f0: (n: number) => number, i0: number, i1: number, i: number) => (i1 === i0 ? f0(i0) : f0(i0) + (i - i0) * (f0(i1) - f0(i0)) / (i1 - i0));
|
|
186
|
+
// ---- a panel's rows: its own, compact. The rows a panel uses are its range's first and last and every row its
|
|
187
|
+
// cards and inner panels use; each is the gap below the one before (GY, or more where something inside the panel
|
|
188
|
+
// ends or starts there) further down, from where its parent has its first row. Rows only another panel needs (a
|
|
189
|
+
// tall neighbour's) open no gap inside it. With one level of panels and every row used, this is where the even
|
|
190
|
+
// spacing between the global rows puts them. ----
|
|
191
|
+
const rowsOf = PL.map(() => new Set<number>());
|
|
192
|
+
const addRow = (n: number, j: number) => { for (let m = n; m >= 0; m = parentIx[m]) rowsOf[m].add(j); };
|
|
193
|
+
PL.forEach((p, n) => { addRow(n, p.cells[1]); addRow(n, p.cells[3]); });
|
|
194
|
+
for (const c of board.cards) { const ch = chains[c.cell[0]][c.cell[1]]; if (ch.length) addRow(ch[ch.length - 1], c.cell[1]); }
|
|
195
|
+
const rowY = PL.map(() => new Map<number, number>());
|
|
196
|
+
const below = (chain: number[], n: number) => chain.slice(chain.indexOf(n) + 1);
|
|
197
|
+
for (const n of PL.map((_, i) => i).sort((a, b) => depthOf(a) - depthOf(b) || a - b)) {
|
|
198
|
+
const k = PL[n].cells, rows = [...rowsOf[n]].sort((a, b) => a - b);
|
|
199
|
+
let y = parentIx[n] >= 0 ? rowY[parentIx[n]].get(rows[0])! : by(rows[0]);
|
|
200
|
+
rowY[n].set(rows[0], y);
|
|
201
|
+
for (let r = 1; r < rows.length; r++) {
|
|
202
|
+
let need = 0;
|
|
203
|
+
for (let i = k[0]; i <= k[2]; i++) need = Math.max(need, boundary(below(chains[i][rows[r - 1]], n), below(chains[i][rows[r]], n), true));
|
|
204
|
+
y += H + Math.max(GY, need);
|
|
205
|
+
rowY[n].set(rows[r], y);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
// ---- a panel's columns, the same way: its own, packed (one gap, or what a boundary inside it needs), from where its
|
|
209
|
+
// parent has its first column. A top-level panel of cards only that uses every column of its range keeps decision 4's
|
|
210
|
+
// even spread between its first and last column of the drawing's (so a panel spanning a channel stays even). ----
|
|
211
|
+
const colsOf = PL.map(() => new Set<number>());
|
|
212
|
+
const addCol = (n: number, i: number) => { for (let m = n; m >= 0; m = parentIx[m]) colsOf[m].add(i); };
|
|
213
|
+
PL.forEach((p, n) => { addCol(n, p.cells[0]); addCol(n, p.cells[2]); });
|
|
214
|
+
for (const c of board.cards) { const ch = chains[c.cell[0]][c.cell[1]]; if (ch.length) addCol(ch[ch.length - 1], c.cell[0]); }
|
|
215
|
+
const colX = PL.map(() => new Map<number, number>());
|
|
216
|
+
for (const n of PL.map((_, i) => i).sort((a, b) => depthOf(a) - depthOf(b) || a - b)) {
|
|
217
|
+
const k = PL[n].cells, cols = [...colsOf[n]].sort((a, b) => a - b);
|
|
218
|
+
if (parentIx[n] < 0 && !parentIx.includes(n) && cols.length === k[2] - k[0] + 1) { for (const i of cols) colX[n].set(i, lerp(bx, k[0], k[2], i)); continue; }
|
|
219
|
+
let x = parentIx[n] >= 0 ? colX[parentIx[n]].get(cols[0])! : bx(cols[0]);
|
|
220
|
+
colX[n].set(cols[0], x);
|
|
221
|
+
for (let c = 1; c < cols.length; c++) {
|
|
222
|
+
let need = 0;
|
|
223
|
+
for (let j = k[1]; j <= k[3]; j++) need = Math.max(need, boundary(below(chains[cols[c - 1]][j], n), below(chains[cols[c]][j], n), false));
|
|
224
|
+
x += W + Math.max(GX, need);
|
|
225
|
+
colX[n].set(cols[c], x);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
const pos = (i: number, j: number): FlatPoint => {
|
|
229
|
+
const c = chains[i][j], n = c.length ? c[c.length - 1] : -1;
|
|
230
|
+
return n >= 0 ? [colX[n].get(i)!, rowY[n].get(j)!] : [bx(i), by(j)];
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
// ---- panels, innermost first (a panel holds its own cards and its children's rects, PAD round, TITLE below) ----
|
|
234
|
+
const order = PL.map((_, i) => i).sort((a, b) => depthOf(b) - depthOf(a) || a - b);
|
|
235
|
+
const prect: FlatRect[] = new Array(PL.length);
|
|
236
|
+
for (const n of order) {
|
|
237
|
+
const k = PL[n].cells;
|
|
238
|
+
// across: its range; down: its own cards (and any end row of its range nothing uses), and its inner panels
|
|
239
|
+
const xs: number[] = [], ys: number[] = [];
|
|
240
|
+
for (const c of board.cards) { const ch = chains[c.cell[0]][c.cell[1]]; if (ch[ch.length - 1] === n) { xs.push(colX[n].get(c.cell[0])!); ys.push(rowY[n].get(c.cell[1])!); } }
|
|
241
|
+
const inRange = (c: BoardCard) => c.cell[0] >= k[0] && c.cell[0] <= k[2] && c.cell[1] >= k[1] && c.cell[1] <= k[3];
|
|
242
|
+
for (const j of [k[1], k[3]]) if (!board.cards.some(c => c.cell[1] === j && inRange(c))) ys.push(rowY[n].get(j)!);
|
|
243
|
+
for (const i of [k[0], k[2]]) if (!board.cards.some(c => c.cell[0] === i && inRange(c))) xs.push(colX[n].get(i)!);
|
|
244
|
+
const box = { l: xs.length ? Math.min(...xs) : Infinity, t: ys.length ? Math.min(...ys) : Infinity, r: xs.length ? Math.max(...xs) + W : -Infinity, b: ys.length ? Math.max(...ys) + H : -Infinity };
|
|
245
|
+
PL.forEach((_, m) => {
|
|
246
|
+
if (parentIx[m] !== n) return;
|
|
247
|
+
const c = prect[m];
|
|
248
|
+
box.l = Math.min(box.l, c.l); box.t = Math.min(box.t, c.t); box.r = Math.max(box.r, c.r); box.b = Math.max(box.b, c.b);
|
|
249
|
+
});
|
|
250
|
+
prect[n] = { l: box.l - PAD, t: box.t - PAD, r: box.r + PAD, b: box.b + PAD + TITLE };
|
|
251
|
+
}
|
|
252
|
+
// ---- then each panel's contents slid left as far as they may go, innermost first: a card GX from a card beside it, a
|
|
253
|
+
// channel between a container and what stands beside it. Positions by columns are where a column could be; a panel
|
|
254
|
+
// that packed narrower than its column says would leave a band of nothing beside it. Not inside a top-level panel
|
|
255
|
+
// of cards only (decision 4 spreads those) or at the drawing's own level. ----
|
|
256
|
+
const at = board.cards.map(c => pos(c.cell[0], c.cell[1]));
|
|
257
|
+
const innerOf = board.cards.map(c => { const ch = chains[c.cell[0]][c.cell[1]]; return ch.length ? ch[ch.length - 1] : -1; });
|
|
258
|
+
const inside = (m: number, n: number) => { for (let a = m; a >= 0; a = parentIx[a]) if (a === n) return true; return false; };
|
|
259
|
+
const shift = (m: number, dx: number) => {
|
|
260
|
+
board.cards.forEach((_, i) => { if (innerOf[i] >= 0 && inside(innerOf[i], m)) at[i] = [at[i][0] + dx, at[i][1]]; });
|
|
261
|
+
PL.forEach((_, q) => { if (inside(q, m)) prect[q] = { ...prect[q], l: prect[q].l + dx, r: prect[q].r + dx }; });
|
|
262
|
+
};
|
|
263
|
+
const fit = (n: number) => {
|
|
264
|
+
const xs: number[] = [], ys: number[] = [], xr: number[] = [], yb: number[] = [];
|
|
265
|
+
board.cards.forEach((_, i) => { if (innerOf[i] === n) { xs.push(at[i][0]); xr.push(at[i][0] + W); ys.push(at[i][1]); yb.push(at[i][1] + H); } });
|
|
266
|
+
PL.forEach((_, m) => { if (parentIx[m] === n) { xs.push(prect[m].l); xr.push(prect[m].r); ys.push(prect[m].t); yb.push(prect[m].b); } });
|
|
267
|
+
// a range end nothing uses still counts (as the panel's own rect had it)
|
|
268
|
+
const keep = prect[n];
|
|
269
|
+
const l = Math.min(...xs, keep.l + PAD), r = Math.max(...xr, keep.r - PAD), t = Math.min(...ys, keep.t + PAD), b = Math.max(...yb, keep.b - PAD - TITLE);
|
|
270
|
+
prect[n] = { l: l - PAD, t: t - PAD, r: r + PAD, b: b + PAD + TITLE };
|
|
271
|
+
};
|
|
272
|
+
for (const n of order) {
|
|
273
|
+
const even = parentIx[n] < 0 && !parentIx.includes(n);
|
|
274
|
+
if (!even) {
|
|
275
|
+
type It = { l: number; r: number; t: number; b: number; card?: number; panel?: number };
|
|
276
|
+
const items: It[] = [];
|
|
277
|
+
board.cards.forEach((_, i) => { if (innerOf[i] === n) items.push({ l: at[i][0], r: at[i][0] + W, t: at[i][1], b: at[i][1] + H, card: i }); });
|
|
278
|
+
PL.forEach((_, m) => { if (parentIx[m] === n) items.push({ ...prect[m], panel: m }); });
|
|
279
|
+
items.sort((a, b) => a.l - b.l || a.t - b.t);
|
|
280
|
+
const left = Math.min(...items.map(i => i.l));
|
|
281
|
+
const placed: It[] = [];
|
|
282
|
+
for (const it of items) {
|
|
283
|
+
let x = left;
|
|
284
|
+
for (const q of placed) if (q.t < it.b && it.t < q.b && q.r <= it.l + 1e-6) x = Math.max(x, q.r + (q.card !== undefined && it.card !== undefined ? GX : CH));
|
|
285
|
+
const dx = x - it.l;
|
|
286
|
+
if (dx < -1e-6) {
|
|
287
|
+
if (it.card !== undefined) at[it.card] = [at[it.card][0] + dx, at[it.card][1]];
|
|
288
|
+
else shift(it.panel!, dx);
|
|
289
|
+
it.l += dx; it.r += dx;
|
|
290
|
+
}
|
|
291
|
+
placed.push(it);
|
|
292
|
+
}
|
|
293
|
+
// its rect: what it holds now (its range's ends no longer stretch it)
|
|
294
|
+
const xs = items.map(i => i.l), xr = items.map(i => i.r);
|
|
295
|
+
prect[n] = { ...prect[n], l: Math.min(...xs) - PAD, r: Math.max(...xr) + PAD };
|
|
296
|
+
} else fit(n);
|
|
297
|
+
}
|
|
298
|
+
const cardAt = (i: number): FlatPoint => at[i];
|
|
299
|
+
return { prect, parentIx, depthOf, pos, cardAt };
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/** the 2D layout of a board. Throws on a board with errors (boardErrors). */
|
|
303
|
+
export function layoutFlat(board: Board, measure: Measure, opts: Partial<FlatOptions> = {}): FlatLayout {
|
|
304
|
+
if (opts.wrap) { const { wrap, ...rest } = opts; return layoutFlat(wrapBoard(board, measure, wrap.aspect, rest), measure, rest); }
|
|
305
|
+
const errs = boardErrors(board);
|
|
306
|
+
if (errs.length) throw new Error(`the board has ${errs.length} error${errs.length > 1 ? 's' : ''}:\n ${errs.join('\n ')}`);
|
|
307
|
+
const o: FlatOptions = { ...FLAT_DEFAULTS, ...opts };
|
|
308
|
+
const { W, H } = o;
|
|
309
|
+
const f1 = (v: number) => Math.round(v * 10) / 10;
|
|
310
|
+
const warnings: string[] = [];
|
|
311
|
+
const PL = board.panels ?? [];
|
|
312
|
+
|
|
313
|
+
const { prect, parentIx, depthOf, cardAt } = placeBoard(board, o);
|
|
314
|
+
const panels: FlatPanel[] = PL.map((p, n) => {
|
|
315
|
+
const r = prect[n];
|
|
316
|
+
const tw = measure(p.label ?? '', 'title') + (p.sub ? measure(p.sub, 'titleSub') : 0);
|
|
317
|
+
return {
|
|
318
|
+
...p, rect: r, titleAt: [f1(r.l + 16), f1(r.b - 15)], depth: depthOf(n), parent: parentIx[n] >= 0 ? PL[parentIx[n]].id : undefined,
|
|
319
|
+
title: { l: r.l + 10, r: r.l + 22 + tw, t: r.b - 30, b: r.b - 6 },
|
|
320
|
+
};
|
|
321
|
+
});
|
|
322
|
+
const cards: FlatCard[] = board.cards.map(c => {
|
|
323
|
+
const [x, y] = cardAt(board.cards.indexOf(c));
|
|
324
|
+
return { ...c, rect: { l: x, t: y, r: x + W, b: y + H } };
|
|
325
|
+
});
|
|
326
|
+
|
|
327
|
+
// ---- routing ----
|
|
328
|
+
const R = new Map<string, FlatRect>();
|
|
329
|
+
panels.forEach(p => R.set(p.id, p.rect));
|
|
330
|
+
cards.forEach(c => R.set(c.id, c.rect));
|
|
331
|
+
const CR = cards.map(c => c.rect), PANELS = panels.map(p => p.rect), TITLES = panels.filter(p => p.label).map(p => p.title);
|
|
332
|
+
const inside = (c: FlatRect, p: FlatRect) => c.l >= p.l && c.r <= p.r && c.t >= p.t && c.b <= p.b;
|
|
333
|
+
const hits = (a: FlatPoint, b: FlatPoint, q: FlatRect, m: number) => Math.min(a[0], b[0]) < q.r + m && Math.max(a[0], b[0]) > q.l - m && Math.min(a[1], b[1]) < q.b + m && Math.max(a[1], b[1]) > q.t - m;
|
|
334
|
+
// how many panel edges a segment crosses (a route should leave a panel once, by the nearest channel)
|
|
335
|
+
const borders = (a: FlatPoint, b: FlatPoint) => PANELS.reduce((n, p) => {
|
|
336
|
+
if (a[0] === b[0]) { const lo = Math.min(a[1], b[1]), hi = Math.max(a[1], b[1]); if (a[0] > p.l && a[0] < p.r) n += [p.t, p.b].filter(y => y > lo + 1 && y < hi - 1).length; }
|
|
337
|
+
else { const lo = Math.min(a[0], b[0]), hi = Math.max(a[0], b[0]); if (a[1] > p.t && a[1] < p.b) n += [p.l, p.r].filter(x => x > lo + 1 && x < hi - 1).length; }
|
|
338
|
+
return n;
|
|
339
|
+
}, 0);
|
|
340
|
+
// a segment running along a panel edge (within 16px) reads as part of the panel
|
|
341
|
+
const nearEdge = (a: FlatPoint, b: FlatPoint) => PANELS.some(p => (a[1] === b[1]
|
|
342
|
+
? [p.t, p.b].some(y => Math.abs(a[1] - y) < 16 && Math.max(a[0], b[0]) > p.l && Math.min(a[0], b[0]) < p.r)
|
|
343
|
+
: [p.l, p.r].some(x => Math.abs(a[0] - x) < 16 && Math.max(a[1], b[1]) > p.t && Math.min(a[1], b[1]) < p.b)));
|
|
344
|
+
// a segment crossing the inside of a panel that holds neither end
|
|
345
|
+
const crossesPanel = (a: FlatPoint, b: FlatPoint, A: FlatRect, B: FlatRect) => PANELS.filter(p => p !== A && p !== B && !inside(A, p) && !inside(B, p) && hits(a, b, { l: p.l + 3, r: p.r - 3, t: p.t + 3, b: p.b - 3 }, 0)).length;
|
|
346
|
+
const used: [FlatPoint, FlatPoint][] = [];
|
|
347
|
+
const hUsed = new Map<number, [FlatPoint, FlatPoint][]>(), vUsed = new Map<number, [FlatPoint, FlatPoint][]>();
|
|
348
|
+
const use = (a: FlatPoint, b: FlatPoint) => {
|
|
349
|
+
used.push([a, b]);
|
|
350
|
+
const [m, v] = a[1] === b[1] ? [hUsed, a[1]] : a[0] === b[0] ? [vUsed, a[0]] : [undefined, 0];
|
|
351
|
+
if (!m) return;
|
|
352
|
+
const k = Math.floor(v / 6);
|
|
353
|
+
(m.get(k) ?? m.set(k, []).get(k)!).push([a, b]);
|
|
354
|
+
};
|
|
355
|
+
/** how many drawn segments a segment runs along (parallel, within 6px, sharing some length) */
|
|
356
|
+
const overlapUsed = (a: FlatPoint, b: FlatPoint) => {
|
|
357
|
+
const horiz = a[1] === b[1];
|
|
358
|
+
if (!horiz && a[0] !== b[0]) return 0;
|
|
359
|
+
const ax = horiz ? 1 : 0, ac = horiz ? 0 : 1, m = horiz ? hUsed : vUsed, k = Math.floor(a[ax] / 6);
|
|
360
|
+
const lo = Math.min(a[ac], b[ac]), hi = Math.max(a[ac], b[ac]);
|
|
361
|
+
let n = 0;
|
|
362
|
+
for (let q = k - 1; q <= k + 1; q++) for (const [c, d] of m.get(q) ?? []) {
|
|
363
|
+
if (Math.abs(a[ax] - c[ax]) < 6 && Math.min(hi, Math.max(c[ac], d[ac])) - Math.max(lo, Math.min(c[ac], d[ac])) > 0) n++;
|
|
364
|
+
}
|
|
365
|
+
return n;
|
|
366
|
+
};
|
|
367
|
+
const all = [...R.values()];
|
|
368
|
+
const L0 = Math.min(...all.map(r => r.l)), R0 = Math.max(...all.map(r => r.r)), T0 = Math.min(...all.map(r => r.t)), B0 = Math.max(...all.map(r => r.b));
|
|
369
|
+
const mids = (lo: number, hi: number) => { const v = (lo + hi) / 2; return [v, v - 12, v + 12, v - 24, v + 24]; };
|
|
370
|
+
const LY = (() => { const ys: number[] = []; CR.forEach(a => CR.forEach(b => { if (b.t > a.b && b.l < a.r + 400 && b.r > a.l - 400) ys.push(...mids(a.b, b.t)); })); ys.push(T0 - 22, T0 - 34, B0 + 22, B0 + 34); return [...new Set(ys.map(Math.round))]; })();
|
|
371
|
+
const LX = (() => { const xs: number[] = []; CR.forEach(a => CR.forEach(b => { if (b.l > a.r) xs.push(...mids(a.r, b.l)); })); xs.push(L0 - 22, L0 - 34, R0 + 22, R0 + 34); return [...new Set(xs.map(Math.round))]; })();
|
|
372
|
+
|
|
373
|
+
/** a route's cost; Infinity through a card or a title (strict), or a count of those hits (lenient). `bound`: stop
|
|
374
|
+
* early once the cost can't beat it (the result is then only known to be ≥ bound) */
|
|
375
|
+
// the cards a segment can meet: a horizontal one at y meets only cards whose band (t - 6, b + 6) holds y, a vertical
|
|
376
|
+
// one at x only those whose (l - 6, r + 6) holds x; kept per coordinate, in board order
|
|
377
|
+
const byRow = new Map<number, FlatRect[]>(), byCol = new Map<number, FlatRect[]>();
|
|
378
|
+
const rowAt = (y: number) => { let v = byRow.get(y); if (!v) byRow.set(y, v = CR.filter(q => y < q.b + 6 && y > q.t - 6)); return v; };
|
|
379
|
+
const colAt = (x: number) => { let v = byCol.get(x); if (!v) byCol.set(x, v = CR.filter(q => x < q.r + 6 && x > q.l - 6)); return v; };
|
|
380
|
+
const kc = o.crossPanel / o.overlap, kb = o.border / o.overlap, ke = o.nearEdge / o.overlap;
|
|
381
|
+
/** a route's cost: length, bends, and 400 × its overlap units (an overlap with a drawn line; crossings, borders and
|
|
382
|
+
* running near an edge as fractions of one); Infinity through a card or a title (strict), else 1e5 a hit.
|
|
383
|
+
* `bound`: once the cost can't beat it, any value ≥ it is returned */
|
|
384
|
+
function cost(pts: FlatPoint[], A: FlatRect, B: FlatRect, bound: number, strict = true): number {
|
|
385
|
+
let len = 0;
|
|
386
|
+
for (let k = 1; k < pts.length; k++) len += Math.abs(pts[k - 1][0] - pts[k][0]) + Math.abs(pts[k - 1][1] - pts[k][1]);
|
|
387
|
+
const base = len + o.bend * (pts.length - 2);
|
|
388
|
+
if (base >= bound) return base;
|
|
389
|
+
let bad = 0, ov = 0;
|
|
390
|
+
for (let k = 1; k < pts.length; k++) {
|
|
391
|
+
const a = pts[k - 1], b = pts[k];
|
|
392
|
+
if (strict) { if (blocked(a, b, A, B) || nearEdge(a, b)) return Infinity; continue; }
|
|
393
|
+
for (const q of a[1] === b[1] ? rowAt(a[1]) : colAt(a[0])) if (q !== A && q !== B && hits(a, b, q, 6)) bad++;
|
|
394
|
+
for (const q of TITLES) if (hits(a, b, q, 2)) bad++;
|
|
395
|
+
}
|
|
396
|
+
for (let k = 1; k < pts.length; k++) {
|
|
397
|
+
const a = pts[k - 1], b = pts[k];
|
|
398
|
+
ov += overlapUsed(a, b) + kc * crossesPanel(a, b, A, B) + kb * borders(a, b);
|
|
399
|
+
if (nearEdge(a, b)) ov += ke;
|
|
400
|
+
if (base + o.overlap * ov >= bound) return base + o.overlap * ov;
|
|
401
|
+
}
|
|
402
|
+
return base + o.overlap * ov + bad * 1e5;
|
|
403
|
+
}
|
|
404
|
+
const cx = (r: FlatRect) => (r.l + r.r) / 2, cy = (r: FlatRect) => (r.t + r.b) / 2;
|
|
405
|
+
const OXS = [0, -22, 22, -44, 44];
|
|
406
|
+
/** the candidate routes from A to B, in a fixed order */
|
|
407
|
+
/** the best cost so far for the link being routed: the candidates skip what can't beat it (as cost() would) */
|
|
408
|
+
const bound = { bc: Infinity };
|
|
409
|
+
// a candidate's length and bends, summed as cost() sums them, before the candidate is made
|
|
410
|
+
const seg = (ax: number, ay: number, bx: number, by: number) => Math.abs(ax - bx) + Math.abs(ay - by);
|
|
411
|
+
const base4 = (x0: number, y0: number, x1: number, y1: number, x2: number, y2: number, x3: number, y3: number) => {
|
|
412
|
+
let len = 0; len += seg(x0, y0, x1, y1); len += seg(x1, y1, x2, y2); len += seg(x2, y2, x3, y3); return len + o.bend * 2;
|
|
413
|
+
};
|
|
414
|
+
const base5 = (x0: number, y0: number, x1: number, y1: number, x2: number, y2: number, x3: number, y3: number, x4: number, y4: number) => {
|
|
415
|
+
let len = 0; len += seg(x0, y0, x1, y1); len += seg(x1, y1, x2, y2); len += seg(x2, y2, x3, y3); len += seg(x3, y3, x4, y4); return len + o.bend * 3;
|
|
416
|
+
};
|
|
417
|
+
/** a segment through a card (but A or B) or a title: a strict route may not have one */
|
|
418
|
+
const blocked = (a: FlatPoint, b: FlatPoint, A: FlatRect, B: FlatRect) => {
|
|
419
|
+
for (const q of a[1] === b[1] ? rowAt(a[1]) : colAt(a[0])) if (q !== A && q !== B && hits(a, b, q, 6)) return true;
|
|
420
|
+
for (const q of TITLES) if (hits(a, b, q, 2)) return true;
|
|
421
|
+
return nearEdge(a, b);
|
|
422
|
+
};
|
|
423
|
+
/** the candidate routes from A to B, in a fixed order. Strict: a candidate whose first or last segment is blocked
|
|
424
|
+
* (which cost() would make Infinity) is skipped before it is made; those segments are tested once a link */
|
|
425
|
+
function routes(A: FlatRect, B: FlatRect, take: (p: FlatPoint[]) => void, strict: boolean): void {
|
|
426
|
+
// straight
|
|
427
|
+
const ol = Math.max(A.l, B.l), orr = Math.min(A.r, B.r);
|
|
428
|
+
if (orr - ol > 24) for (const d of OXS) { const x = (ol + orr) / 2 + d; if (x > ol + 8 && x < orr - 8) take(B.t >= A.b ? [[x, A.b], [x, B.t]] : [[x, A.t], [x, B.b]]); }
|
|
429
|
+
const ot = Math.max(A.t, B.t), ob = Math.min(A.b, B.b);
|
|
430
|
+
if (ob - ot > 16) for (const d of [0, -10, 10]) { const y = (ot + ob) / 2 + d; take(B.l >= A.r ? [[A.r, y], [B.l, y]] : [[A.l, y], [B.r, y]]); }
|
|
431
|
+
// per port offset and lane: is the segment out of A (into B) clear? 0 unknown, 1 clear, 2 blocked
|
|
432
|
+
const NX = LX.length, NY = LY.length;
|
|
433
|
+
const vA = new Uint8Array(OXS.length * NY), vB = new Uint8Array(OXS.length * NY), hA = new Uint8Array(OXS.length * NX), hB = new Uint8Array(OXS.length * NX);
|
|
434
|
+
const clear = (t: Uint8Array, i: number, a: FlatPoint, b: FlatPoint) => {
|
|
435
|
+
if (!strict) return true;
|
|
436
|
+
if (!t[i]) t[i] = blocked(a, b, A, B) ? 2 : 1;
|
|
437
|
+
return t[i] === 1;
|
|
438
|
+
};
|
|
439
|
+
OXS.forEach((da, di) => OXS.forEach((db, dj) => {
|
|
440
|
+
const ax = cx(A) + da, bxx = cx(B) + db, ay = cy(A) + da / 3, byy = cy(B) + db / 3;
|
|
441
|
+
// vertical – horizontal – vertical, through a lane between, above or below
|
|
442
|
+
LY.forEach((y, yi) => {
|
|
443
|
+
const ya = y > A.b ? A.b : y < A.t ? A.t : null, yb = y > B.b ? B.b : y < B.t ? B.t : null;
|
|
444
|
+
if (ya !== null && yb !== null && base4(ax, ya, ax, y, bxx, y, bxx, yb) < bound.bc
|
|
445
|
+
&& clear(vA, di * NY + yi, [ax, ya], [ax, y]) && clear(vB, dj * NY + yi, [bxx, y], [bxx, yb])) take([[ax, ya], [ax, y], [bxx, y], [bxx, yb]]);
|
|
446
|
+
});
|
|
447
|
+
// horizontal – vertical – horizontal
|
|
448
|
+
LX.forEach((x, xi) => {
|
|
449
|
+
const xa = x > A.r ? A.r : x < A.l ? A.l : null, xb = x > B.r ? B.r : x < B.l ? B.l : null;
|
|
450
|
+
if (xa !== null && xb !== null && base4(xa, ay, x, ay, x, byy, xb, byy) < bound.bc
|
|
451
|
+
&& clear(hA, di * NX + xi, [xa, ay], [x, ay]) && clear(hB, dj * NX + xi, [x, byy], [xb, byy])) take([[xa, ay], [x, ay], [x, byy], [xb, byy]]);
|
|
452
|
+
});
|
|
453
|
+
// four bends: out of a side, along a lane, across another lane, into the top or bottom (and the reverse)
|
|
454
|
+
if (da === 0 && db === 0) LX.forEach((x, xi) => LY.forEach((y, yi) => {
|
|
455
|
+
const xa = x > A.r ? A.r : x < A.l ? A.l : null, yb = y > B.b ? B.b : y < B.t ? B.t : null;
|
|
456
|
+
if (xa !== null && yb !== null && base5(xa, ay, x, ay, x, y, bxx, y, bxx, yb) < bound.bc
|
|
457
|
+
&& clear(hA, di * NX + xi, [xa, ay], [x, ay]) && clear(vB, dj * NY + yi, [bxx, y], [bxx, yb])) take([[xa, ay], [x, ay], [x, y], [bxx, y], [bxx, yb]]);
|
|
458
|
+
const ya = y > A.b ? A.b : y < A.t ? A.t : null, xb = x > B.r ? B.r : x < B.l ? B.l : null;
|
|
459
|
+
if (ya !== null && xb !== null && base5(ax, ya, ax, y, x, y, x, byy, xb, byy) < bound.bc
|
|
460
|
+
&& clear(vA, di * NY + yi, [ax, ya], [ax, y]) && clear(hB, dj * NX + xi, [x, byy], [xb, byy])) take([[ax, ya], [ax, y], [x, y], [x, byy], [xb, byy]]);
|
|
461
|
+
}));
|
|
462
|
+
// L shapes
|
|
463
|
+
const xb2 = cx(A) < B.l ? B.l : cx(A) > B.r ? B.r : null, yA = byy < A.t ? A.t : byy > A.b ? A.b : null;
|
|
464
|
+
if (xb2 !== null && yA !== null) take([[ax, yA], [ax, byy], [xb2, byy]]);
|
|
465
|
+
const yb2 = cy(A) < B.t ? B.t : cy(A) > B.b ? B.b : null, xA = bxx < A.l ? A.l : bxx > A.r ? A.r : null;
|
|
466
|
+
if (yb2 !== null && xA !== null) take([[xA, ay], [bxx, ay], [bxx, yb2]]);
|
|
467
|
+
}));
|
|
468
|
+
}
|
|
469
|
+
/** from something to a panel holding it: straight out of a side to that panel's inner border */
|
|
470
|
+
function toHolder(A: FlatRect, B: FlatRect, flip: boolean, take: (p: FlatPoint[]) => void): void {
|
|
471
|
+
for (const d of OXS) {
|
|
472
|
+
const x = cx(A) + d, y = cy(A) + d / 3;
|
|
473
|
+
const c: FlatPoint[][] = [[[x, A.t], [x, B.t]], [[A.r, y], [B.r, y]], [[x, A.b], [x, B.b]], [[A.l, y], [B.l, y]]];
|
|
474
|
+
for (const p of c) take(flip ? [p[1], p[0]] : p);
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
/** the cheapest candidate, the first of equals (in the candidates' order) */
|
|
478
|
+
const best = (gen: (take: (p: FlatPoint[]) => void, strict: boolean) => void, A: FlatRect, B: FlatRect, strict: boolean) => {
|
|
479
|
+
let pick: FlatPoint[] | null = null;
|
|
480
|
+
bound.bc = Infinity;
|
|
481
|
+
gen(p => { const k = cost(p, A, B, bound.bc, strict); if (k < bound.bc) { bound.bc = k; pick = p; } }, strict);
|
|
482
|
+
return pick as FlatPoint[] | null;
|
|
483
|
+
};
|
|
484
|
+
|
|
485
|
+
// ---- labels ----
|
|
486
|
+
const labs: FlatRect[] = [];
|
|
487
|
+
const onBorder = (q: FlatRect) => PANELS.some(p => [p.l, p.r].some(x => q.l < x + 3 && q.r > x - 3 && q.b > p.t && q.t < p.b) || [p.t, p.b].some(y => q.t < y + 3 && q.b > y - 3 && q.r > p.l && q.l < p.r));
|
|
488
|
+
const onLine = (q: FlatRect) => used.filter(([a, b]) => hits(a, b, q, 1)).length;
|
|
489
|
+
const overTitle = (q: FlatRect) => TITLES.some(t => q.l < t.r && q.r > t.l && q.t < t.b && q.b > t.t);
|
|
490
|
+
const nearCards = (q: FlatRect) => CR.filter(c => q.l < c.r + 3 && q.r > c.l - 3 && q.t < c.b + 3 && q.b > c.t - 3).length;
|
|
491
|
+
const nearLabels = (q: FlatRect) => labs.filter(c => q.l < c.r + 4 && q.r > c.l - 4 && q.t < c.b + 2 && q.b > c.t - 2).length;
|
|
492
|
+
const boxBad = (q: FlatRect) => 50 * onCrossing(q) + 3 * onLine(q) + (onBorder(q) ? 1 : 0) + (overTitle(q) ? 5 : 0) + 5 * nearCards(q) + 5 * nearLabels(q);
|
|
493
|
+
const boxHits = (q: FlatRect) => onLine(q) > 0 || onBorder(q) || overTitle(q) || nearCards(q) > 0 || nearLabels(q) > 0;
|
|
494
|
+
|
|
495
|
+
const links: FlatLink[] = [];
|
|
496
|
+
(board.links ?? []).forEach((c, n) => {
|
|
497
|
+
const id = c.id ?? `link-${n}`;
|
|
498
|
+
const A = R.get(c.aOn ?? c.a)!, B = R.get(c.bOn ?? c.b)!;
|
|
499
|
+
if (A === B) { warnings.push(`link "${id}" joins "${c.aOn ?? c.a}" to itself: not drawn`); links.push({ ...c, id, points: [], routed: false }); return; }
|
|
500
|
+
const gen = inside(A, B) ? (t: (p: FlatPoint[]) => void) => toHolder(A, B, false, t) : inside(B, A) ? (t: (p: FlatPoint[]) => void) => toHolder(B, A, true, t) : (t: (p: FlatPoint[]) => void, strict: boolean) => routes(A, B, t, strict);
|
|
501
|
+
let route = best(gen, A, B, true), routed = true;
|
|
502
|
+
if (!route) { route = best(gen, A, B, false); routed = false; warnings.push(`link "${id}": no clear route; drawn the least bad way`); }
|
|
503
|
+
if (!route) { links.push({ ...c, id, points: [], routed: false }); return; }
|
|
504
|
+
for (let k = 1; k < route.length; k++) use(route[k - 1], route[k]);
|
|
505
|
+
links.push({ ...c, id, points: route, routed });
|
|
506
|
+
});
|
|
507
|
+
// ---- labels, once every line is drawn (a line drawn after a label would run through it) ----
|
|
508
|
+
// where two drawn lines cross: a label never sits on a crossing, even the least bad one
|
|
509
|
+
const crossings: FlatPoint[] = [];
|
|
510
|
+
for (let i = 0; i < used.length; i++) for (let j = i + 1; j < used.length; j++) {
|
|
511
|
+
const [a, b] = used[i], [c, d] = used[j];
|
|
512
|
+
const h1 = a[1] === b[1], h2 = c[1] === d[1];
|
|
513
|
+
if (h1 === h2) continue;
|
|
514
|
+
const [H1, V1] = h1 ? [[a, b], [c, d]] : [[c, d], [a, b]];
|
|
515
|
+
const x = V1[0][0], y = H1[0][1];
|
|
516
|
+
if (x > Math.min(H1[0][0], H1[1][0]) && x < Math.max(H1[0][0], H1[1][0]) && y > Math.min(V1[0][1], V1[1][1]) && y < Math.max(V1[0][1], V1[1][1])) crossings.push([x, y]);
|
|
517
|
+
}
|
|
518
|
+
const onCrossing = (q: FlatRect) => crossings.filter(([x, y]) => x > q.l - 2 && x < q.r + 2 && y > q.t - 2 && y < q.b + 2).length;
|
|
519
|
+
for (const L of links) {
|
|
520
|
+
const c = L, route = L.points;
|
|
521
|
+
if (!c.label || !route.length) continue;
|
|
522
|
+
// the label: beside the longest segment first, then the others; above or below a horizontal, right or left of a vertical
|
|
523
|
+
const segs: [FlatPoint, FlatPoint][] = [];
|
|
524
|
+
for (let k = 1; k < route.length; k++) segs.push([route[k - 1], route[k]]);
|
|
525
|
+
const segLen = (s: [FlatPoint, FlatPoint]) => Math.abs(s[0][0] - s[1][0]) + Math.abs(s[0][1] - s[1][1]);
|
|
526
|
+
segs.sort((p, q) => segLen(q) - segLen(p));
|
|
527
|
+
const w = measure(c.label, 'label') + 6;
|
|
528
|
+
type Spot = [number, number, FlatLabel['anchor'], FlatRect];
|
|
529
|
+
let placed: Spot | null = null, least: Spot | null = null, leastBad = Infinity;
|
|
530
|
+
for (const [a, b] of segs) {
|
|
531
|
+
const mx = (a[0] + b[0]) / 2, my = (a[1] + b[1]) / 2;
|
|
532
|
+
const tries: [number, number, FlatLabel['anchor']][] = a[1] === b[1]
|
|
533
|
+
? [0, -0.3, 0.3, -0.15, 0.15].flatMap(k => { const x = mx + k * Math.abs(a[0] - b[0]); return [[x, my - 6, 'middle'], [x, my + 15, 'middle']] as [number, number, FlatLabel['anchor']][]; })
|
|
534
|
+
: [0, -0.3, 0.3].flatMap(k => { const y = my + 4 + k * Math.abs(a[1] - b[1]); return [[mx + 7, y, 'start'], [mx - 7, y, 'end'], [mx + 24, y, 'start'], [mx - 24, y, 'end']] as [number, number, FlatLabel['anchor']][]; });
|
|
535
|
+
for (const [x, y, an] of tries) {
|
|
536
|
+
const l = an === 'middle' ? x - w / 2 : an === 'end' ? x - w : x, q = { l, r: l + w, t: y - 11, b: y + 3 };
|
|
537
|
+
if (!boxHits(q)) { placed = [x, y, an, q]; break; }
|
|
538
|
+
const bad = boxBad(q); if (bad < leastBad) { leastBad = bad; least = [x, y, an, q]; }
|
|
539
|
+
}
|
|
540
|
+
if (placed) break;
|
|
541
|
+
}
|
|
542
|
+
if (!placed && least) placed = least;
|
|
543
|
+
if (!placed) { const [a, b] = segs[0]; const x = (a[0] + b[0]) / 2, y = (a[1] + b[1]) / 2 - 6; placed = [x, y, 'middle', { l: x - w / 2, r: x + w / 2, t: y - 11, b: y + 3 }]; }
|
|
544
|
+
labs.push(placed[3]);
|
|
545
|
+
L.at = { x: f1(placed[0]), y: f1(placed[1]), anchor: placed[2], box: placed[3], text: c.label };
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
// ---- bounds: everything drawn, and the margin ----
|
|
549
|
+
const xs: number[] = [], ys: number[] = [];
|
|
550
|
+
for (const r of [...all, ...labs, ...panels.map(p => p.title)]) { xs.push(r.l, r.r); ys.push(r.t, r.b); }
|
|
551
|
+
for (const l of links) for (const p of l.points) { xs.push(p[0]); ys.push(p[1]); }
|
|
552
|
+
const m = o.margin;
|
|
553
|
+
const bounds = { l: Math.min(...xs) - m, t: Math.min(...ys) - m, r: Math.max(...xs) + m, b: Math.max(...ys) + m };
|
|
554
|
+
return { cards, panels, links, bounds, options: o, warnings };
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
/** which card or panel a point is on (topmost: a card, then the innermost panel) */
|
|
558
|
+
export function flatHit(L: FlatLayout, x: number, y: number): string | undefined {
|
|
559
|
+
const on = (r: FlatRect) => x >= r.l && x <= r.r && y >= r.t && y <= r.b;
|
|
560
|
+
const c = L.cards.find(k => on(k.rect));
|
|
561
|
+
if (c) return c.id;
|
|
562
|
+
return [...L.panels].sort((a, b) => b.depth - a.depth).find(p => on(p.rect))?.id;
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
// ---- wrapping a wide drawing ----
|
|
566
|
+
|
|
567
|
+
/** A wide drawing wrapped for a frame of an aspect (width / height): in a container (the drawing, or a panel) whose
|
|
568
|
+
* children stand side by side, the last of them go onto a shelf of their own below the others, in their order. Each
|
|
569
|
+
* step takes the wrap that lets the drawing be drawn largest in such a frame, while one does. Cards inside a panel of cards only
|
|
570
|
+
* stay as they are: what moves is a container's children as wholes (inner panels, and cards beside them). */
|
|
571
|
+
export function wrapBoard(board: Board, measure: Measure, aspect: number, opts: Partial<FlatOptions> = {}): Board {
|
|
572
|
+
// fitted into a frame of that aspect, how large it is drawn: the smaller its larger side (height as width), the larger
|
|
573
|
+
const score = (b: Board) => { const L = layoutFlat(b, measure, opts); return Math.max(L.bounds.r - L.bounds.l, (L.bounds.b - L.bounds.t) * aspect); };
|
|
574
|
+
let cur = board, best = score(board);
|
|
575
|
+
for (let step = 0; step < 12; step++) {
|
|
576
|
+
let next: Board | undefined, nextScore = best;
|
|
577
|
+
for (const cand of wraps(cur)) {
|
|
578
|
+
if (boardErrors(cand).length) continue;
|
|
579
|
+
const sc = score(cand);
|
|
580
|
+
if (sc < nextScore - 0.5) { nextScore = sc; next = cand; }
|
|
581
|
+
}
|
|
582
|
+
if (!next) break;
|
|
583
|
+
cur = next; best = nextScore;
|
|
584
|
+
}
|
|
585
|
+
return cur;
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
/** every way to wrap one shelf of one container's children: each split of each row of side-by-side children */
|
|
589
|
+
function* wraps(board: Board): Generator<Board> {
|
|
590
|
+
const PL = board.panels ?? [];
|
|
591
|
+
const within2 = (a: number[], b: number[]) => a[0] >= b[0] && a[2] <= b[2] && a[1] >= b[1] && a[3] <= b[3];
|
|
592
|
+
const same = (a: number[], b: number[]) => a.every((v, i) => v === b[i]);
|
|
593
|
+
// the innermost panel holding k. Two panels may cover the same cells (an AZ that holds only its VPC): the board lists
|
|
594
|
+
// the outer first, so of equal ones the later is inside the earlier (0.8.2: else each was the other's parent, and the
|
|
595
|
+
// walk up from one never ended)
|
|
596
|
+
const parentOf = (k: number[], self = -1) => {
|
|
597
|
+
let best = -1;
|
|
598
|
+
PL.forEach((p, n) => {
|
|
599
|
+
if (n === self || !within2(k, p.cells)) return;
|
|
600
|
+
if (self >= 0 && n > self && same(p.cells, PL[self].cells)) return; // an equal panel after self is inside it
|
|
601
|
+
if (best < 0 || (within2(p.cells, PL[best].cells) && (!same(p.cells, PL[best].cells) || n > best))) best = n;
|
|
602
|
+
});
|
|
603
|
+
return best;
|
|
604
|
+
};
|
|
605
|
+
// a container's children, as blocks of cells: its child panels, and its own cards
|
|
606
|
+
const blocks = (c: number) => {
|
|
607
|
+
const out: { range: number[]; panel?: number; card?: number }[] = [];
|
|
608
|
+
PL.forEach((p, n) => { if (parentOf(p.cells, n) === c) out.push({ range: [...p.cells], panel: n }); });
|
|
609
|
+
board.cards.forEach((k, i) => { const r = [k.cell[0], k.cell[1], k.cell[0], k.cell[1]]; if (parentOf(r) === c) out.push({ range: r, card: i }); });
|
|
610
|
+
return out;
|
|
611
|
+
};
|
|
612
|
+
for (const c of [-1, ...PL.map((_, n) => n)]) {
|
|
613
|
+
const bs = blocks(c);
|
|
614
|
+
if (c >= 0 && !bs.some(b => b.panel !== undefined)) continue; // a panel of cards only: left as it is
|
|
615
|
+
// shelves: children whose rows overlap, left to right
|
|
616
|
+
const rows = (b: { range: number[] }) => [b.range[1], b.range[3]];
|
|
617
|
+
const shelves: (typeof bs)[] = [];
|
|
618
|
+
for (const b of [...bs].sort((x, y) => x.range[1] - y.range[1] || x.range[0] - y.range[0])) {
|
|
619
|
+
const sh = shelves.find(s => s.some(o => rows(o)[0] <= rows(b)[1] && rows(b)[0] <= rows(o)[1]));
|
|
620
|
+
if (sh) sh.push(b); else shelves.push([b]);
|
|
621
|
+
}
|
|
622
|
+
const R = Math.max(...bs.map(b => b.range[3]));
|
|
623
|
+
for (const sh of shelves) {
|
|
624
|
+
sh.sort((x, y) => x.range[0] - y.range[0]);
|
|
625
|
+
for (let m = 1; m < sh.length; m++) {
|
|
626
|
+
const moved = sh.slice(m), c0 = sh[0].range[0], r0 = R + 1, top = Math.min(...moved.map(b => b.range[1]));
|
|
627
|
+
const h = Math.max(...moved.map(b => b.range[3])) - top + 1;
|
|
628
|
+
const cards = board.cards.map(k => ({ ...k, cell: [...k.cell] as [number, number] }));
|
|
629
|
+
const panels = PL.map(p => ({ ...p, cells: [...p.cells] as [number, number, number, number] }));
|
|
630
|
+
// below the container: down by the new shelf's height; the container and what holds it: taller
|
|
631
|
+
for (const k of cards) if (k.cell[1] > R) k.cell[1] += h;
|
|
632
|
+
const holds = new Set<number>();
|
|
633
|
+
for (let a = c; a >= 0 && !holds.has(a); a = parentOf(PL[a].cells, a)) holds.add(a);
|
|
634
|
+
panels.forEach((p, n) => { if (p.cells[1] > R) { p.cells[1] += h; p.cells[3] += h; } else if (holds.has(n) || (p.cells[1] <= R && p.cells[3] > R)) p.cells[3] += h; });
|
|
635
|
+
// the moved children: onto the shelf, left to right from the container's first column, in their order
|
|
636
|
+
let x = c0;
|
|
637
|
+
for (const b of moved) {
|
|
638
|
+
const dx = x - b.range[0], dy = r0 + (b.range[1] - top) - b.range[1];
|
|
639
|
+
const inside = (cell: number[]) => cell[0] >= b.range[0] && cell[0] <= b.range[2] && cell[1] >= b.range[1] && cell[1] <= b.range[3];
|
|
640
|
+
board.cards.forEach((k, i) => { if (inside([k.cell[0], k.cell[1]])) { cards[i].cell = [k.cell[0] + dx, k.cell[1] + dy]; } });
|
|
641
|
+
PL.forEach((p, n) => { if (within2(p.cells, b.range)) panels[n].cells = [p.cells[0] + dx, p.cells[1] + dy, p.cells[2] + dx, p.cells[3] + dy]; });
|
|
642
|
+
x += b.range[2] - b.range[0] + 1;
|
|
643
|
+
}
|
|
644
|
+
// each panel as wide and tall as the cards it held (a move leaves no empty end in a range)
|
|
645
|
+
PL.forEach((p, n) => {
|
|
646
|
+
const mine = board.cards.map((k, i) => (within2([k.cell[0], k.cell[1], k.cell[0], k.cell[1]], p.cells) ? cards[i].cell : undefined)).filter((v): v is [number, number] => !!v);
|
|
647
|
+
if (mine.length) panels[n].cells = [Math.min(...mine.map(v => v[0])), Math.min(...mine.map(v => v[1])), Math.max(...mine.map(v => v[0])), Math.max(...mine.map(v => v[1]))];
|
|
648
|
+
});
|
|
649
|
+
yield { cards, panels, links: board.links };
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
}
|