@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.
Files changed (310) hide show
  1. package/EDITOR-GUIDE.md +317 -0
  2. package/LICENSE +21 -0
  3. package/LICENSES/ajv.txt +22 -0
  4. package/LICENSES/lucide-static.txt +43 -0
  5. package/LICENSES/yaml.txt +13 -0
  6. package/README.md +62 -2
  7. package/assets/app.css +265 -0
  8. package/assets/default-theme.yaml +99 -0
  9. package/assets/icons.svg +1 -0
  10. package/assets/marks.css +35 -0
  11. package/assets/skin.css +181 -0
  12. package/iso-editor.d.ts +4 -0
  13. package/iso-editor.js +550 -0
  14. package/iso-render.d.ts +3 -0
  15. package/iso-render.js +388 -0
  16. package/iso-systems.d.ts +4 -0
  17. package/iso-systems.js +389 -0
  18. package/js/iso-core/compile-schema.js +22 -0
  19. package/js/iso-core/depth.js +55 -0
  20. package/js/iso-core/errors.js +65 -0
  21. package/js/iso-core/glyphs.js +100 -0
  22. package/js/iso-core/index.js +55 -0
  23. package/js/iso-core/kernel.js +158 -0
  24. package/js/iso-core/metrics.json +1 -0
  25. package/js/iso-core/package.json +6 -0
  26. package/js/iso-core/parse.js +79 -0
  27. package/js/iso-core/schema-errors.js +170 -0
  28. package/js/iso-core/shims/crypto.js +125 -0
  29. package/js/iso-core/shims/path.js +49 -0
  30. package/js/iso-core/svg-dom.js +144 -0
  31. package/js/iso-core/theme.js +48 -0
  32. package/js/iso-core/vdom.js +48 -0
  33. package/js/iso-editor/adapter.js +0 -0
  34. package/js/iso-editor/clipboard.js +152 -0
  35. package/js/iso-editor/commands.js +173 -0
  36. package/js/iso-editor/complete.js +55 -0
  37. package/js/iso-editor/doc.js +182 -0
  38. package/js/iso-editor/index.js +105 -0
  39. package/js/iso-editor/model.js +210 -0
  40. package/js/iso-editor/outline.js +111 -0
  41. package/js/iso-editor/package.json +6 -0
  42. package/js/iso-editor/path.js +19 -0
  43. package/js/iso-editor/providers.js +96 -0
  44. package/js/iso-editor/schema-nav.js +117 -0
  45. package/js/iso-editor/splice.js +441 -0
  46. package/js/iso-editor/storage-keys.js +6 -0
  47. package/js/iso-editor/ui/actions.js +116 -0
  48. package/js/iso-editor/ui/canvas-layer.js +0 -0
  49. package/js/iso-editor/ui/canvas.js +1205 -0
  50. package/js/iso-editor/ui/clip.js +133 -0
  51. package/js/iso-editor/ui/dialog.js +26 -0
  52. package/js/iso-editor/ui/flat-canvas.js +54 -0
  53. package/js/iso-editor/ui/icons.js +15 -0
  54. package/js/iso-editor/ui/keys.js +26 -0
  55. package/js/iso-editor/ui/later.js +28 -0
  56. package/js/iso-editor/ui/layer.js +11 -0
  57. package/js/iso-editor/ui/menu.js +105 -0
  58. package/js/iso-editor/ui/monaco-entry.js +2 -0
  59. package/js/iso-editor/ui/mount-kit.js +0 -0
  60. package/js/iso-editor/ui/mount.js +767 -0
  61. package/js/iso-editor/ui/pane.js +0 -0
  62. package/js/iso-editor/ui/part.js +0 -0
  63. package/js/iso-editor/ui/plain-icons.js +34 -0
  64. package/js/iso-editor/ui/props.js +314 -0
  65. package/js/iso-editor/ui/scope-css.js +81 -0
  66. package/js/iso-editor/ui/setup.js +145 -0
  67. package/js/iso-editor/ui/text.js +323 -0
  68. package/js/iso-editor/ui/tree.js +728 -0
  69. package/js/iso-editor/validate.js +33 -0
  70. package/js/iso-editor/window/protocol.js +8 -0
  71. package/js/iso-objects/assemble.js +43 -0
  72. package/js/iso-objects/assets.js +88 -0
  73. package/js/iso-objects/board.js +162 -0
  74. package/js/iso-objects/compile.js +60 -0
  75. package/js/iso-objects/css.js +154 -0
  76. package/js/iso-objects/flat.js +696 -0
  77. package/js/iso-objects/focus.js +16 -0
  78. package/js/iso-objects/geometry.js +76 -0
  79. package/js/iso-objects/index.js +93 -0
  80. package/js/iso-objects/kinds.js +12 -0
  81. package/js/iso-objects/labels.js +325 -0
  82. package/js/iso-objects/package.json +6 -0
  83. package/js/iso-objects/place.js +347 -0
  84. package/js/iso-objects/route.js +461 -0
  85. package/js/iso-objects/scene.js +350 -0
  86. package/js/iso-objects/steps.js +246 -0
  87. package/js/iso-objects/types.js +0 -0
  88. package/js/iso-systems/catalog.js +42 -0
  89. package/js/iso-systems/compile.js +34 -0
  90. package/js/iso-systems/css.js +8 -0
  91. package/js/iso-systems/index.js +16 -0
  92. package/js/iso-systems/nodes.js +124 -0
  93. package/js/iso-systems/package.json +6 -0
  94. package/js/iso-systems/schema.json +311 -0
  95. package/js/iso-systems/validate.js +35 -0
  96. package/js/iso-viewer/bundle.js +20 -0
  97. package/js/iso-viewer/card-motion.js +228 -0
  98. package/js/iso-viewer/dom.js +14 -0
  99. package/js/iso-viewer/dual-view.js +212 -0
  100. package/js/iso-viewer/flat-view.js +124 -0
  101. package/js/iso-viewer/history.js +990 -0
  102. package/js/iso-viewer/index.js +98 -0
  103. package/js/iso-viewer/morph.js +192 -0
  104. package/js/iso-viewer/overlay.js +199 -0
  105. package/js/iso-viewer/package.json +6 -0
  106. package/js/iso-viewer/page.js +138 -0
  107. package/js/iso-viewer/player.js +409 -0
  108. package/js/iso-viewer/still.js +81 -0
  109. package/js/iso-viewer/timeline-contract.js +14 -0
  110. package/js/iso-viewer/timeline-engine.js +475 -0
  111. package/js/iso-viewer/timeline-export.js +36 -0
  112. package/js/iso-viewer/timeline-scale.js +116 -0
  113. package/js/iso-viewer/timeline-scope.js +58 -0
  114. package/js/iso-viewer/timeline-search.js +216 -0
  115. package/js/iso-viewer/timeline-time.js +55 -0
  116. package/js/iso-viewer/view-css.js +68 -0
  117. package/js/iso-viewer/viewport.js +177 -0
  118. package/package.json +502 -4
  119. package/src/iso-core/compile-schema.ts +23 -0
  120. package/src/iso-core/depth.ts +56 -0
  121. package/src/iso-core/errors.ts +73 -0
  122. package/src/iso-core/glyphs.ts +108 -0
  123. package/src/iso-core/index.ts +27 -0
  124. package/src/iso-core/kernel.ts +163 -0
  125. package/src/iso-core/metrics.json +1 -0
  126. package/src/iso-core/package.json +6 -0
  127. package/src/iso-core/parse.ts +91 -0
  128. package/src/iso-core/schema-errors.ts +163 -0
  129. package/src/iso-core/shims/crypto.ts +37 -0
  130. package/src/iso-core/shims/path.ts +24 -0
  131. package/src/iso-core/svg-dom.ts +103 -0
  132. package/src/iso-core/theme.ts +64 -0
  133. package/src/iso-core/vdom.ts +41 -0
  134. package/src/iso-editor/adapter.ts +128 -0
  135. package/src/iso-editor/clipboard.ts +163 -0
  136. package/src/iso-editor/commands.ts +191 -0
  137. package/src/iso-editor/complete.ts +73 -0
  138. package/src/iso-editor/doc.ts +207 -0
  139. package/src/iso-editor/index.ts +67 -0
  140. package/src/iso-editor/model.ts +234 -0
  141. package/src/iso-editor/outline.ts +126 -0
  142. package/src/iso-editor/package.json +6 -0
  143. package/src/iso-editor/path.ts +18 -0
  144. package/src/iso-editor/providers.ts +113 -0
  145. package/src/iso-editor/schema-nav.ts +129 -0
  146. package/src/iso-editor/splice.ts +472 -0
  147. package/src/iso-editor/storage-keys.ts +6 -0
  148. package/src/iso-editor/ui/actions.ts +119 -0
  149. package/src/iso-editor/ui/app.css +230 -0
  150. package/src/iso-editor/ui/canvas-layer.ts +56 -0
  151. package/src/iso-editor/ui/canvas.ts +1071 -0
  152. package/src/iso-editor/ui/clip.ts +97 -0
  153. package/src/iso-editor/ui/dialog.ts +25 -0
  154. package/src/iso-editor/ui/flat-canvas.ts +51 -0
  155. package/src/iso-editor/ui/icons.ts +21 -0
  156. package/src/iso-editor/ui/keys.ts +30 -0
  157. package/src/iso-editor/ui/later.ts +22 -0
  158. package/src/iso-editor/ui/layer.ts +7 -0
  159. package/src/iso-editor/ui/marks.css +35 -0
  160. package/src/iso-editor/ui/menu.ts +67 -0
  161. package/src/iso-editor/ui/monaco-entry.ts +6 -0
  162. package/src/iso-editor/ui/mount-kit.ts +35 -0
  163. package/src/iso-editor/ui/mount.ts +684 -0
  164. package/src/iso-editor/ui/pane.ts +45 -0
  165. package/src/iso-editor/ui/part.ts +45 -0
  166. package/src/iso-editor/ui/plain-icons.ts +83 -0
  167. package/src/iso-editor/ui/props.ts +347 -0
  168. package/src/iso-editor/ui/scope-css.ts +65 -0
  169. package/src/iso-editor/ui/setup.ts +144 -0
  170. package/src/iso-editor/ui/skin.css +181 -0
  171. package/src/iso-editor/ui/text.ts +262 -0
  172. package/src/iso-editor/ui/tree.ts +657 -0
  173. package/src/iso-editor/validate.ts +62 -0
  174. package/src/iso-editor/window/protocol.ts +33 -0
  175. package/src/iso-objects/assemble.ts +57 -0
  176. package/src/iso-objects/assets.ts +122 -0
  177. package/src/iso-objects/board.ts +169 -0
  178. package/src/iso-objects/compile.ts +76 -0
  179. package/src/iso-objects/css.ts +163 -0
  180. package/src/iso-objects/flat.ts +653 -0
  181. package/src/iso-objects/focus.ts +14 -0
  182. package/src/iso-objects/geometry.ts +86 -0
  183. package/src/iso-objects/index.ts +33 -0
  184. package/src/iso-objects/kinds.ts +56 -0
  185. package/src/iso-objects/labels.ts +310 -0
  186. package/src/iso-objects/package.json +6 -0
  187. package/src/iso-objects/place.ts +318 -0
  188. package/src/iso-objects/route.ts +444 -0
  189. package/src/iso-objects/scene.ts +376 -0
  190. package/src/iso-objects/steps.ts +234 -0
  191. package/src/iso-objects/types.ts +68 -0
  192. package/src/iso-systems/catalog.ts +31 -0
  193. package/src/iso-systems/compile.ts +39 -0
  194. package/src/iso-systems/css.ts +9 -0
  195. package/src/iso-systems/index.ts +12 -0
  196. package/src/iso-systems/nodes.ts +126 -0
  197. package/src/iso-systems/package.json +6 -0
  198. package/src/iso-systems/schema.json +311 -0
  199. package/src/iso-systems/validate.ts +34 -0
  200. package/src/iso-viewer/bundle.ts +15 -0
  201. package/src/iso-viewer/card-motion.ts +237 -0
  202. package/src/iso-viewer/dom.ts +13 -0
  203. package/src/iso-viewer/dual-view.ts +212 -0
  204. package/src/iso-viewer/flat-view.ts +127 -0
  205. package/src/iso-viewer/history.ts +781 -0
  206. package/src/iso-viewer/index.ts +40 -0
  207. package/src/iso-viewer/morph.ts +191 -0
  208. package/src/iso-viewer/overlay.ts +212 -0
  209. package/src/iso-viewer/package.json +6 -0
  210. package/src/iso-viewer/page.ts +150 -0
  211. package/src/iso-viewer/player.ts +321 -0
  212. package/src/iso-viewer/still.ts +74 -0
  213. package/src/iso-viewer/timeline-contract.ts +342 -0
  214. package/src/iso-viewer/timeline-engine.ts +420 -0
  215. package/src/iso-viewer/timeline-export.ts +31 -0
  216. package/src/iso-viewer/timeline-scale.ts +129 -0
  217. package/src/iso-viewer/timeline-scope.ts +56 -0
  218. package/src/iso-viewer/timeline-search.ts +198 -0
  219. package/src/iso-viewer/timeline-time.ts +54 -0
  220. package/src/iso-viewer/view-css.ts +59 -0
  221. package/src/iso-viewer/viewport.ts +142 -0
  222. package/types/iso-core/compile-schema.d.ts +11 -0
  223. package/types/iso-core/depth.d.ts +6 -0
  224. package/types/iso-core/errors.d.ts +24 -0
  225. package/types/iso-core/glyphs.d.ts +19 -0
  226. package/types/iso-core/index.d.ts +17 -0
  227. package/types/iso-core/kernel.d.ts +130 -0
  228. package/types/iso-core/metrics.json +1 -0
  229. package/types/iso-core/parse.d.ts +18 -0
  230. package/types/iso-core/schema-errors.d.ts +14 -0
  231. package/types/iso-core/shims/crypto.d.ts +9 -0
  232. package/types/iso-core/svg-dom.d.ts +15 -0
  233. package/types/iso-core/theme.d.ts +38 -0
  234. package/types/iso-core/vdom.d.ts +26 -0
  235. package/types/iso-editor/adapter.d.ts +145 -0
  236. package/types/iso-editor/clipboard.d.ts +23 -0
  237. package/types/iso-editor/commands.d.ts +32 -0
  238. package/types/iso-editor/complete.d.ts +25 -0
  239. package/types/iso-editor/doc.d.ts +50 -0
  240. package/types/iso-editor/index.d.ts +57 -0
  241. package/types/iso-editor/model.d.ts +96 -0
  242. package/types/iso-editor/outline.d.ts +28 -0
  243. package/types/iso-editor/path.d.ts +7 -0
  244. package/types/iso-editor/providers.d.ts +43 -0
  245. package/types/iso-editor/schema-nav.d.ts +34 -0
  246. package/types/iso-editor/splice.d.ts +53 -0
  247. package/types/iso-editor/storage-keys.d.ts +5 -0
  248. package/types/iso-editor/ui/actions.d.ts +23 -0
  249. package/types/iso-editor/ui/canvas-layer.d.ts +65 -0
  250. package/types/iso-editor/ui/canvas.d.ts +279 -0
  251. package/types/iso-editor/ui/clip.d.ts +14 -0
  252. package/types/iso-editor/ui/dialog.d.ts +7 -0
  253. package/types/iso-editor/ui/flat-canvas.d.ts +2 -0
  254. package/types/iso-editor/ui/icons.d.ts +5 -0
  255. package/types/iso-editor/ui/keys.d.ts +7 -0
  256. package/types/iso-editor/ui/later.d.ts +12 -0
  257. package/types/iso-editor/ui/layer.d.ts +2 -0
  258. package/types/iso-editor/ui/menu.d.ts +21 -0
  259. package/types/iso-editor/ui/mount-kit.d.ts +31 -0
  260. package/types/iso-editor/ui/mount.d.ts +146 -0
  261. package/types/iso-editor/ui/pane.d.ts +60 -0
  262. package/types/iso-editor/ui/part.d.ts +40 -0
  263. package/types/iso-editor/ui/plain-icons.d.ts +10 -0
  264. package/types/iso-editor/ui/props.d.ts +57 -0
  265. package/types/iso-editor/ui/scope-css.d.ts +5 -0
  266. package/types/iso-editor/ui/setup.d.ts +40 -0
  267. package/types/iso-editor/ui/text.d.ts +19 -0
  268. package/types/iso-editor/ui/tree.d.ts +100 -0
  269. package/types/iso-editor/validate.d.ts +36 -0
  270. package/types/iso-editor/window/protocol.d.ts +60 -0
  271. package/types/iso-objects/assemble.d.ts +35 -0
  272. package/types/iso-objects/assets.d.ts +49 -0
  273. package/types/iso-objects/board.d.ts +36 -0
  274. package/types/iso-objects/compile.d.ts +35 -0
  275. package/types/iso-objects/css.d.ts +16 -0
  276. package/types/iso-objects/flat.d.ts +130 -0
  277. package/types/iso-objects/focus.d.ts +1 -0
  278. package/types/iso-objects/geometry.d.ts +59 -0
  279. package/types/iso-objects/index.d.ts +26 -0
  280. package/types/iso-objects/kinds.d.ts +53 -0
  281. package/types/iso-objects/labels.d.ts +92 -0
  282. package/types/iso-objects/place.d.ts +25 -0
  283. package/types/iso-objects/route.d.ts +67 -0
  284. package/types/iso-objects/scene.d.ts +63 -0
  285. package/types/iso-objects/steps.d.ts +90 -0
  286. package/types/iso-objects/types.d.ts +140 -0
  287. package/types/iso-systems/catalog.d.ts +4 -0
  288. package/types/iso-systems/compile.d.ts +6 -0
  289. package/types/iso-systems/css.d.ts +3 -0
  290. package/types/iso-systems/index.d.ts +5 -0
  291. package/types/iso-systems/nodes.d.ts +3 -0
  292. package/types/iso-viewer/card-motion.d.ts +56 -0
  293. package/types/iso-viewer/dom.d.ts +1 -0
  294. package/types/iso-viewer/dual-view.d.ts +44 -0
  295. package/types/iso-viewer/flat-view.d.ts +25 -0
  296. package/types/iso-viewer/history.d.ts +91 -0
  297. package/types/iso-viewer/index.d.ts +31 -0
  298. package/types/iso-viewer/morph.d.ts +28 -0
  299. package/types/iso-viewer/overlay.d.ts +135 -0
  300. package/types/iso-viewer/page.d.ts +13 -0
  301. package/types/iso-viewer/still.d.ts +26 -0
  302. package/types/iso-viewer/timeline-contract.d.ts +345 -0
  303. package/types/iso-viewer/timeline-engine.d.ts +20 -0
  304. package/types/iso-viewer/timeline-export.d.ts +5 -0
  305. package/types/iso-viewer/timeline-scale.d.ts +44 -0
  306. package/types/iso-viewer/timeline-scope.d.ts +33 -0
  307. package/types/iso-viewer/timeline-search.d.ts +52 -0
  308. package/types/iso-viewer/timeline-time.d.ts +8 -0
  309. package/types/iso-viewer/view-css.d.ts +7 -0
  310. package/types/iso-viewer/viewport.d.ts +50 -0
@@ -0,0 +1,14 @@
1
+ // The view a step's focus asks for: its screen box grown to the drawing's proportions, kept inside the drawing.
2
+ // Shared by the player (which eases to it) and the proof sheet (which draws it).
3
+
4
+ export function focusView(full: number[], box: [number, number, number, number] | undefined): number[] {
5
+ if (!box) return full;
6
+ const [x0, y0, x1, y1] = box, ar = full[2] / full[3];
7
+ let w = x1 - x0, h = y1 - y0;
8
+ if (w / h < ar) w = h * ar; else h = w / ar;
9
+ w = Math.min(w, full[2]); h = Math.min(h, full[3]);
10
+ const cx = (x0 + x1) / 2, cy = (y0 + y1) / 2;
11
+ const x = Math.max(full[0], Math.min(full[0] + full[2] - w, cx - w / 2));
12
+ const y = Math.max(full[1], Math.min(full[1] + full[3] - h, cy - h / 2));
13
+ return [x, y, w, h];
14
+ }
@@ -0,0 +1,86 @@
1
+ // The geometry an interactive view and an editor need over a laid-out scene (moved here from the editor canvas's
2
+ // drawing, R1 S3): a screen move as a floor move, where an item sits in its parent's frame, a box's outline and floor
3
+ // on screen, and where a drag lands, inside its parent and clear of its siblings. Pure.
4
+
5
+ import { C, S, iso } from '../iso-core/kernel.ts';
6
+ import type { CompiledScene } from './compile.ts';
7
+ import { type KindCatalog, regionKind } from './kinds.ts';
8
+
9
+ /** A move on screen (SVG units) as a move on the floor (grid units): the inverse of the iso projection at one height. */
10
+ export function screenToGrid(dx: number, dy: number): [number, number] {
11
+ const a = dx / (C * S), b = (2 * dy) / S; // a = x − y, b = x + y
12
+ return [(a + b) / 2, (b - a) / 2];
13
+ }
14
+
15
+ /** Where an element sits in its parent's frame (what at: means), in grid units. */
16
+ export function localAt(sc: CompiledScene, id: string): [number, number] | undefined {
17
+ const p = sc.layout.byId.get(id);
18
+ if (!p) return undefined;
19
+ const o = p.parent ? [p.parent.box.x0, p.parent.box.y0] : [0, 0];
20
+ return [p.box.x0 - o[0], p.box.y0 - o[1]];
21
+ }
22
+
23
+ type Box3 = { x0: number; y0: number; z0: number; x1: number; y1: number; z1: number };
24
+ /** The outline a box makes on screen: the convex hull of its eight projected corners (a hexagon, seen from above). */
25
+ export function boxSilhouette(b: Box3): [number, number][] {
26
+ const pts: [number, number][] = [];
27
+ for (const x of [b.x0, b.x1]) for (const y of [b.y0, b.y1]) for (const z of [b.z0, b.z1]) pts.push(iso(x, y, z));
28
+ return hull(pts);
29
+ }
30
+ /** A region's floor: its footprint at the height it stands on, projected (a parallelogram). */
31
+ export function floorOf(b: Box3): [number, number][] {
32
+ return [[b.x0, b.y0], [b.x1, b.y0], [b.x1, b.y1], [b.x0, b.y1]].map(([x, y]) => iso(x, y, b.z0));
33
+ }
34
+ /** convex hull (Andrew's monotone chain), counter-clockwise, no repeated point */
35
+ function hull(p: [number, number][]): [number, number][] {
36
+ const pts = [...new Map(p.map(q => [`${q[0].toFixed(3)},${q[1].toFixed(3)}`, q])).values()].sort((a, b) => a[0] - b[0] || a[1] - b[1]);
37
+ if (pts.length < 3) return pts;
38
+ const cross = (o: number[], a: number[], b: number[]) => (a[0] - o[0]) * (b[1] - o[1]) - (a[1] - o[1]) * (b[0] - o[0]);
39
+ const lo: [number, number][] = [], hi: [number, number][] = [];
40
+ for (const q of pts) { while (lo.length >= 2 && cross(lo[lo.length - 2], lo[lo.length - 1], q) <= 1e-9) lo.pop(); lo.push(q); }
41
+ for (const q of [...pts].reverse()) { while (hi.length >= 2 && cross(hi[hi.length - 2], hi[hi.length - 1], q) <= 1e-9) hi.pop(); hi.push(q); }
42
+ return [...lo.slice(0, -1), ...hi.slice(0, -1)];
43
+ }
44
+
45
+ // ---------- dragging: where a move lands, inside its parent, clear of its siblings ----------
46
+
47
+ /** a region's inner margin: its own, else its kind's (as place.ts pads it); 0 for a kind the catalogue doesn't have */
48
+ export const paddingOf = (cat: KindCatalog, region: { kind?: string; padding?: number } | undefined) => region?.padding ?? regionKind(cat, region?.kind)?.pad ?? 0;
49
+
50
+ export interface Move {
51
+ /** the new at: in the parent's frame */
52
+ at: [number, number];
53
+ /** the grid move actually made (after snapping and clamping) */
54
+ delta: [number, number];
55
+ /** which edges of the parent stopped it */
56
+ clamped: { left: boolean; right: boolean; top: boolean; bottom: boolean };
57
+ }
58
+
59
+ /**
60
+ * A drag of (dx, dy) grid units, from `start` (at: in the parent's frame), to a position snapped to `step` (1: whole
61
+ * cells; 0.5: half cells), then clamped so the item's footprint `size` stays inside its parent's inner area (`parent`: its size, `pad`:
62
+ * its margin). With no parent (the scene), nothing clamps.
63
+ */
64
+ export function snapMove(start: [number, number], d: [number, number], step: number, size: [number, number], parent?: { w: number; d: number; pad: number }): Move {
65
+ const snap = (v: number) => Math.round(v / step) * step;
66
+ // the position lands on the grid (the lines drawn while dragging), not just moves by whole steps
67
+ let x = snap(start[0] + d[0]), y = snap(start[1] + d[1]);
68
+ const clamped = { left: false, right: false, top: false, bottom: false };
69
+ if (parent) {
70
+ // the bounds are on the grid too (the layout snaps positions to it), inside the parent's margin
71
+ const lo = Math.ceil(parent.pad / step - 1e-9) * step;
72
+ const hiX = Math.floor((parent.w - parent.pad - size[0]) / step + 1e-9) * step, hiY = Math.floor((parent.d - parent.pad - size[1]) / step + 1e-9) * step;
73
+ if (x < lo) { x = lo; clamped.left = true; }
74
+ if (hiX >= lo && x > hiX) { x = hiX; clamped.right = true; }
75
+ if (y < lo) { y = lo; clamped.top = true; }
76
+ if (hiY >= lo && y > hiY) { y = hiY; clamped.bottom = true; }
77
+ }
78
+ const r = (v: number) => Math.round(v * 1000) / 1000;
79
+ return { at: [r(x), r(y)], delta: [r(x - start[0]), r(y - start[1])], clamped };
80
+ }
81
+
82
+ /** Does a footprint overlap any of these (sharing an edge isn't overlapping)? */
83
+ export function overlaps(a: { x0: number; y0: number; x1: number; y1: number }, others: { x0: number; y0: number; x1: number; y1: number }[]): boolean {
84
+ const e = 1e-6;
85
+ return others.some(b => a.x0 < b.x1 - e && a.x1 > b.x0 + e && a.y0 < b.y1 - e && a.y1 > b.y0 + e);
86
+ }
@@ -0,0 +1,33 @@
1
+ // iso-objects (L1): object kinds as data, containment, placement, routing, labels, assembly, steps and focus, and a
2
+ // scene drawn as SVG (docs/editor/LAYERS.md §2). It knows a kind by its shape and numbers in a catalogue (kinds.ts),
3
+ // never by name; the drawing format's catalogue is the systems pack's (src/iso-systems/).
4
+ //
5
+ // The layer's public API. Importers name the module a name comes from (with code splitting, a barrel puts every file
6
+ // it reaches into each bundle that imports it); the Node tests may import this index.
7
+
8
+ export type { Side, StepRef, RawPlacement, RawDeco, RawNode, RawGate, RawRegion, RawNote, RawScene, GridDef, RawDiagram, RegionKindName, Placed, RawRole, RawKind, Theme } from './types.ts';
9
+ export { regionKind, nodeKind, nodeKindOr, wallHeight, kindsCss } from './kinds.ts';
10
+ export type { RegionShape, RegionKind, KindCatalog } from './kinds.ts';
11
+ export { drawDecos, boxAsset, plate, cloudPath, drawCloud, defaultLabel, registerMarks, markCss, iconPainter, facePainter, placeholderIcon } from './assets.ts';
12
+ export type { LabelSpot, NodeAsset, IconPainter } from './assets.ts';
13
+ export { WALL_T, place, gateBox, overlap2 } from './place.ts';
14
+ export type { Located, Layout } from './place.ts';
15
+ export { parseEnd, floorAt, ROUTE_BUDGET, routeAll, openList, simplify } from './route.ts';
16
+ export type { Side as RouteSide, RawRoute, Routed, RouteResult } from './route.ts';
17
+ export { silhouette, polyBounds, polyRectArea, rectArea, lineRectArea, Obstacles, labelRect, placeNodeLabel, calloutRect, placeCallout, inConvex, convexOverlap } from './labels.ts';
18
+ export type { Rect, Poly, LabelText, PlacedLabel, CalloutSpot } from './labels.ts';
19
+ export { offscreen, assembly } from './assemble.ts';
20
+ export type { Edge, Piece, Assembly } from './assemble.ts';
21
+ export { durationFor, resolveSteps, midpoint } from './steps.ts';
22
+ export type { RawStep, Token, StepData, Say, Mark, Badge, Steps } from './steps.ts';
23
+ export { focusView } from './focus.ts';
24
+ export { regionRole, renderScene, wallsOf, LABEL_BUDGET } from './scene.ts';
25
+ export type { RenderedScene, RenderWarning, SceneInput } from './scene.ts';
26
+ export { PAGE_CSS, drawingCss, baseCss, MARKS_CSS, PLAYER_UI_CSS, GRID_CSS, PLAYER_CSS } from './css.ts';
27
+ export { noSource, compileRaw } from './compile.ts';
28
+ export { screenToGrid, localAt, boxSilhouette, floorOf, paddingOf, snapMove, overlaps } from './geometry.ts';
29
+ export type { Move } from './geometry.ts';
30
+ export type { CompiledScene, Compiled } from './compile.ts';
31
+ export { FLAT_DEFAULTS, FLAT_FONTS, metricsMeasure, boardErrors, placeBoard, layoutFlat, flatHit } from './flat.ts';
32
+ export type { Board, BoardCard, BoardPanel, BoardLink, FlatRect, FlatPoint, FlatFont, Measure, FlatOptions, FlatCard, FlatPanel, FlatLabel, FlatLink, FlatLayout } from './flat.ts';
33
+ export { boardOf, BOARD_3D, BOARD_CSS, cardAsset, boardCatalog, boardDiagram } from './board.ts';
@@ -0,0 +1,56 @@
1
+ // Object kinds as data (docs/editor/LAYERS.md §2): what a region kind is, and the catalogue a drawing is laid out and
2
+ // drawn with. Layout and drawing ask a kind's shape and numbers here, never its name, so a kind pack can name its
3
+ // kinds as it likes (src/iso-systems/catalog.ts is the drawing format's).
4
+
5
+ import type { NodeAsset } from './assets.ts';
6
+
7
+ /** How a region is drawn, and how it takes part in layout and routing. */
8
+ export type RegionShape =
9
+ /** a thick base slab: a skirt below its floor (none on another slab), its name large on its front face */
10
+ | 'slab'
11
+ /** a plain block children stand on */
12
+ | 'block'
13
+ /** a cloud lying on the floor, its name in the middle */
14
+ | 'cloud'
15
+ /** a flat tint on the floor: overlays other things on purpose */
16
+ | 'zone'
17
+ /** a cage of thin walls with a gate in one: routes go round its walls (or through its gate) */
18
+ | 'walled'
19
+ /** a translucent tube over other things: overlays them on purpose */
20
+ | 'tunnel';
21
+
22
+ export interface RegionKind {
23
+ shape: RegionShape;
24
+ /** inner margin, grid units */
25
+ pad: number;
26
+ /** [z0 offset below the floor it stands on, default height above it] */
27
+ z: [number, number];
28
+ /** children stand on its top, and routes find a floor on it */
29
+ floor: boolean;
30
+ /** the colour role it is drawn in, unless the region says its own */
31
+ role: string;
32
+ /** where its name goes, unless the region says */
33
+ labelStyle: 'face' | 'face-right' | 'floor' | 'none';
34
+ /** its row in a page's legend: a swatch (a box, or a line) and its words; auto: shown whenever the kind is drawn */
35
+ legend?: { label: string; swatch: string; box: boolean; auto: boolean };
36
+ /** the kind's own drawing CSS, in the drawing's stylesheet */
37
+ css?: string;
38
+ }
39
+
40
+ export interface KindCatalog {
41
+ regions: Record<string, RegionKind>;
42
+ nodes: Record<string, NodeAsset>;
43
+ /** the node kind a node without one (or with an unknown one) is drawn as */
44
+ defaultNode: string;
45
+ }
46
+
47
+ /** a region kind's data; undefined for a name the catalogue doesn't have */
48
+ export const regionKind = (cat: KindCatalog, name: string | undefined): RegionKind | undefined => (name !== undefined && Object.hasOwn(cat.regions, name) ? cat.regions[name] : undefined);
49
+ /** a node kind by name (none: the default); undefined for a name the catalogue doesn't have */
50
+ export const nodeKind = (cat: KindCatalog, name: string | undefined): NodeAsset | undefined => cat.nodes[name ?? cat.defaultNode];
51
+ /** a node kind by name, or the default one */
52
+ export const nodeKindOr = (cat: KindCatalog, name: string | undefined): NodeAsset => nodeKind(cat, name) ?? cat.nodes[cat.defaultNode];
53
+ /** a walled kind's default height: a gate's height follows it */
54
+ export const wallHeight = (cat: KindCatalog): number => Object.values(cat.regions).find(r => r.shape === 'walled')?.z[1] ?? 0.8;
55
+ /** the kinds' CSS, in the catalogue's order */
56
+ export const kindsCss = (cat: KindCatalog): string => Object.values(cat.regions).map(r => r.css ?? '').join('');
@@ -0,0 +1,310 @@
1
+ // Label placement in screen pixels. A label tries a few spots around its node (above first, as the references do)
2
+ // and takes the first one that clears every node's silhouette, the labels already placed and the routes' lines;
3
+ // failing that, the spot that overlaps least.
4
+
5
+ import { type Box, type P2, iso, textWidth, FONTS } from '../iso-core/kernel.ts';
6
+
7
+ export interface Rect { x: number; y: number; w: number; h: number }
8
+ export type Poly = P2[];
9
+
10
+ /** The outline of a box as seen: six corners, clockwise from the back-top corner. */
11
+ export function silhouette(b: Box): Poly {
12
+ return [iso(b.x0, b.y0, b.z1), iso(b.x1, b.y0, b.z1), iso(b.x1, b.y0, b.z0), iso(b.x1, b.y1, b.z0), iso(b.x0, b.y1, b.z0), iso(b.x0, b.y1, b.z1)];
13
+ }
14
+ export function polyBounds(p: Poly): Rect {
15
+ const xs = p.map(q => q[0]), ys = p.map(q => q[1]);
16
+ const x = Math.min(...xs), y = Math.min(...ys);
17
+ return { x, y, w: Math.max(...xs) - x, h: Math.max(...ys) - y };
18
+ }
19
+
20
+ /** Area of a convex polygon clipped to a rectangle (Sutherland–Hodgman). */
21
+ export function polyRectArea(poly: Poly, r: Rect): number {
22
+ let pts = poly;
23
+ const clip = (inside: (p: P2) => boolean, cross: (a: P2, b: P2) => P2) => {
24
+ const out: P2[] = [];
25
+ for (let i = 0; i < pts.length; i++) {
26
+ const a = pts[i], b = pts[(i + 1) % pts.length];
27
+ const ia = inside(a), ib = inside(b);
28
+ if (ia) out.push(a);
29
+ if (ia !== ib) out.push(cross(a, b));
30
+ }
31
+ pts = out;
32
+ };
33
+ const atX = (x: number) => (a: P2, b: P2): P2 => [x, a[1] + (b[1] - a[1]) * (x - a[0]) / (b[0] - a[0])];
34
+ const atY = (y: number) => (a: P2, b: P2): P2 => [a[0] + (b[0] - a[0]) * (y - a[1]) / (b[1] - a[1]), y];
35
+ clip(p => p[0] >= r.x, atX(r.x)); if (!pts.length) return 0;
36
+ clip(p => p[0] <= r.x + r.w, atX(r.x + r.w)); if (!pts.length) return 0;
37
+ clip(p => p[1] >= r.y, atY(r.y)); if (!pts.length) return 0;
38
+ clip(p => p[1] <= r.y + r.h, atY(r.y + r.h)); if (!pts.length) return 0;
39
+ let a = 0;
40
+ for (let i = 0; i < pts.length; i++) { const p = pts[i], q = pts[(i + 1) % pts.length]; a += p[0] * q[1] - q[0] * p[1]; }
41
+ return Math.abs(a) / 2;
42
+ }
43
+ export function rectArea(a: Rect, b: Rect): number {
44
+ const w = Math.min(a.x + a.w, b.x + b.w) - Math.max(a.x, b.x), h = Math.min(a.y + a.h, b.y + b.h) - Math.max(a.y, b.y);
45
+ return w > 0 && h > 0 ? w * h : 0;
46
+ }
47
+ /** How much of a polyline (as 4px-wide strokes) falls inside a rectangle, in px². */
48
+ export function lineRectArea(pts: P2[], r: Rect): number {
49
+ let a = 0;
50
+ for (let i = 0; i + 1 < pts.length; i++) {
51
+ const p = pts[i], q = pts[i + 1], len = Math.hypot(q[0] - p[0], q[1] - p[1]), n = Math.max(1, Math.ceil(len / 4));
52
+ for (let j = 0; j < n; j++) {
53
+ const x = p[0] + (q[0] - p[0]) * (j + 0.5) / n, y = p[1] + (q[1] - p[1]) * (j + 0.5) / n;
54
+ if (x >= r.x && x <= r.x + r.w && y >= r.y && y <= r.y + r.h) a += (len / n) * 4;
55
+ }
56
+ }
57
+ return a;
58
+ }
59
+
60
+ /**
61
+ * Where shapes are, in buckets of a grid on the screen, so a query visits only the shapes near it. Lists are only ever
62
+ * appended to, so each list's index catches up when it is next asked. A query returns indices in list order, so sums
63
+ * run in the order a full scan would; the shapes it leaves out are those whose bounds miss the query's, which add
64
+ * exactly nothing. The answers are the same as scanning every shape; on a large drawing, far fewer shapes are visited.
65
+ */
66
+ const CELL = 64;
67
+ class Buckets {
68
+ private cells = new Map<number, number[]>();
69
+ n = 0;
70
+ add(id: number, x0: number, y0: number, x1: number, y1: number) {
71
+ for (let cx = Math.floor(x0 / CELL); cx <= Math.floor(x1 / CELL); cx++) for (let cy = Math.floor(y0 / CELL); cy <= Math.floor(y1 / CELL); cy++) {
72
+ const k = cx * 1048576 + cy;
73
+ let list = this.cells.get(k);
74
+ if (!list) this.cells.set(k, list = []);
75
+ if (list[list.length - 1] !== id) list.push(id);
76
+ }
77
+ }
78
+ /** the ids whose buckets meet the box, ascending, each once */
79
+ query(x0: number, y0: number, x1: number, y1: number): number[] {
80
+ const out = new Set<number>();
81
+ for (let cx = Math.floor(x0 / CELL); cx <= Math.floor(x1 / CELL); cx++) for (let cy = Math.floor(y0 / CELL); cy <= Math.floor(y1 / CELL); cy++) {
82
+ const list = this.cells.get(cx * 1048576 + cy);
83
+ if (list) for (const id of list) out.add(id);
84
+ }
85
+ return [...out].sort((p, q) => p - q);
86
+ }
87
+ }
88
+ type Box2 = [number, number, number, number];
89
+ const boundsOf = (p: Poly): Box2 => { let x0 = Infinity, y0 = Infinity, x1 = -Infinity, y1 = -Infinity; for (const [x, y] of p) { if (x < x0) x0 = x; if (x > x1) x1 = x; if (y < y0) y0 = y; if (y > y1) y1 = y; } return [x0, y0, x1, y1]; };
90
+ const indexes = new WeakMap<object, Buckets>();
91
+ /** the index of a list of shapes (one bounding box each), caught up with what was appended since */
92
+ function indexOf<T>(list: T[], box: (t: T) => Box2): Buckets {
93
+ let ix = indexes.get(list);
94
+ if (!ix || ix.n > list.length) indexes.set(list, ix = new Buckets());
95
+ for (; ix.n < list.length; ix.n++) { const [x0, y0, x1, y1] = box(list[ix.n]); ix.add(ix.n, x0, y0, x1, y1); }
96
+ return ix;
97
+ }
98
+ /** route lines by segment: id = line × 65536 + segment */
99
+ function segmentsOf(lines: { pts: P2[] }[]): Buckets {
100
+ let ix = indexes.get(lines);
101
+ if (!ix || ix.n > lines.length) indexes.set(lines, ix = new Buckets());
102
+ for (; ix.n < lines.length; ix.n++) {
103
+ const pts = lines[ix.n].pts;
104
+ for (let i = 0; i + 1 < pts.length; i++) {
105
+ // the cells the segment passes through, not its bounding box (on screen most segments are diagonal, and a long
106
+ // one's box covers hundreds of cells it never touches): short pieces, each indexed by its own box, 1 px wider
107
+ const a = pts[i], b = pts[i + 1], id = ix.n * 65536 + i;
108
+ const k = Math.max(1, Math.ceil(Math.hypot(b[0] - a[0], b[1] - a[1]) / (CELL / 2)));
109
+ for (let m = 0; m < k; m++) {
110
+ const u0 = m / k, u1 = (m + 1) / k;
111
+ const x0 = a[0] + (b[0] - a[0]) * u0, y0 = a[1] + (b[1] - a[1]) * u0, x1 = a[0] + (b[0] - a[0]) * u1, y1 = a[1] + (b[1] - a[1]) * u1;
112
+ ix.add(id, Math.min(x0, x1) - 1, Math.min(y0, y1) - 1, Math.max(x0, x1) + 1, Math.max(y0, y1) + 1);
113
+ }
114
+ }
115
+ }
116
+ return ix;
117
+ }
118
+ const near = (ix: Buckets, x0: number, y0: number, x1: number, y1: number) => ix.query(x0, y0, x1, y1);
119
+ const polyBox = (p: Poly) => boundsOf(p);
120
+ const rectBox = (r: Rect): Box2 => [r.x, r.y, r.x + r.w, r.y + r.h];
121
+
122
+ /** The samples j (0 … n−1, at (j + ½)/n along a→b) that may lie in a box: a range, one wider each side. */
123
+ function sampleRange(a: P2, b: P2, n: number, [x0, y0, x1, y1]: Box2): [number, number] {
124
+ let lo = 0, hi = n - 1;
125
+ for (const k of [0, 1] as const) {
126
+ const d = b[k] - a[k], min = k ? y0 : x0, max = k ? y1 : x1;
127
+ if (Math.abs(d) < 1e-12) { if (a[k] < min || a[k] > max) return [1, 0]; continue; }
128
+ let t0 = ((min - a[k]) / d) * n - 0.5, t1 = ((max - a[k]) / d) * n - 0.5;
129
+ if (t0 > t1) [t0, t1] = [t1, t0];
130
+ lo = Math.max(lo, Math.floor(t0) - 1); hi = Math.min(hi, Math.ceil(t1) + 1);
131
+ }
132
+ return [lo, hi];
133
+ }
134
+
135
+ export class Obstacles {
136
+ polys: { poly: Poly; owner: string }[] = [];
137
+ rects: { r: Rect; owner: string }[] = [];
138
+ lines: { pts: P2[]; owner: string }[] = [];
139
+ /** everything drawn over the floor (nodes, walls, gates): floor text under them is hidden */
140
+ solids: Poly[] = [];
141
+ /** text lying in a plane (floor and face labels), as parallelograms */
142
+ slants: Poly[] = [];
143
+ /** label spots the scene may still weigh (see LABEL_BUDGET in src/iso-objects/scene.ts); shared with forks */
144
+ budget = { spots: Infinity, over: false, told: false };
145
+ /** px² of a rectangle hidden under solids */
146
+ hidden(r: Rect): number {
147
+ let c = 0;
148
+ for (const i of near(indexOf(this.solids, polyBox), ...rectBox(r))) c += polyRectArea(this.solids[i], r);
149
+ return c;
150
+ }
151
+ /** px² of a convex polygon (slanted floor text) hidden under solids */
152
+ hiddenPoly(q: Poly): number {
153
+ let c = 0;
154
+ for (const i of near(indexOf(this.solids, polyBox), ...boundsOf(q))) c += convexOverlap(q, this.solids[i]);
155
+ return c;
156
+ }
157
+ /** like cost(), for a convex polygon */
158
+ costPoly(q: Poly, limit = Infinity): number {
159
+ const qb = boundsOf(q);
160
+ let c = 0;
161
+ for (const i of near(indexOf(this.polys, p => polyBox(p.poly)), ...qb)) c += convexOverlap(q, this.polys[i].poly);
162
+ for (const i of near(indexOf(this.rects, x => rectBox(x.r)), ...qb)) c += polyRectArea(q, this.rects[i].r) * 2;
163
+ for (const i of near(indexOf(this.slants, polyBox), ...qb)) c += convexOverlap(q, this.slants[i]) * 2;
164
+ // (a caller comparing against a best so far passes it as `limit`: past it, the exact sum no longer matters)
165
+ if (c >= limit) return c;
166
+ for (const id of near(segmentsOf(this.lines), ...qb)) {
167
+ if (c >= limit) return c;
168
+ const pts = this.lines[Math.floor(id / 65536)].pts, i = id % 65536;
169
+ const a = pts[i], b = pts[i + 1];
170
+ const len = Math.hypot(b[0] - a[0], b[1] - a[1]), n = Math.max(1, Math.ceil(len / 4));
171
+ // only the samples that can fall in q's bounds (a sample outside them is outside q), one more each side to be
172
+ // safe with rounding; in order, so the sum is the same
173
+ const [j0, j1] = sampleRange(a, b, n, qb);
174
+ for (let j = j0; j <= j1; j++) if (inConvex(q, [a[0] + (b[0] - a[0]) * (j + 0.5) / n, a[1] + (b[1] - a[1]) * (j + 0.5) / n])) c += (len / n) * 4 * 0.6;
175
+ }
176
+ return c;
177
+ }
178
+ /** px² a rectangle shares with nodes, labels and (weighted by lineW) route lines */
179
+ cost(r: Rect, owner?: string, lineW = 0.6): number {
180
+ const rb = rectBox(r);
181
+ let c = 0;
182
+ for (const i of near(indexOf(this.polys, p => polyBox(p.poly)), ...rb)) { const p = this.polys[i]; c += polyRectArea(p.poly, r) * (p.owner === owner ? 1.5 : 1); }
183
+ for (const i of near(indexOf(this.rects, x => rectBox(x.r)), ...rb)) { const q = this.rects[i]; if (q.owner !== owner || !owner) c += rectArea(q.r, r) * 2; }
184
+ for (const i of near(indexOf(this.slants, polyBox), ...rb)) c += polyRectArea(this.slants[i], r) * 2;
185
+ if (lineW) {
186
+ // each line's share, summed over its segments in order (as lineRectArea does), then weighted
187
+ let line = -1, a = 0;
188
+ for (const id of near(segmentsOf(this.lines), ...rb)) {
189
+ const li = Math.floor(id / 65536);
190
+ if (li !== line) { if (line >= 0) c += a * lineW; line = li; a = 0; }
191
+ // lineRectArea's samples on this segment, only those that can fall in r (the same sum)
192
+ const pts = this.lines[li].pts, i = id % 65536, p = pts[i], q = pts[i + 1];
193
+ const len = Math.hypot(q[0] - p[0], q[1] - p[1]), n = Math.max(1, Math.ceil(len / 4));
194
+ const [j0, j1] = sampleRange(p, q, n, rb);
195
+ for (let j = j0; j <= j1; j++) {
196
+ const x = p[0] + (q[0] - p[0]) * (j + 0.5) / n, y = p[1] + (q[1] - p[1]) * (j + 0.5) / n;
197
+ if (x >= r.x && x <= r.x + r.w && y >= r.y && y <= r.y + r.h) a += (len / n) * 4;
198
+ }
199
+ }
200
+ if (line >= 0) c += a * lineW;
201
+ }
202
+ return c;
203
+ }
204
+ /** a copy whose added rectangles do not reach this one (callouts of one step) */
205
+ fork(): Obstacles {
206
+ const o = new Obstacles();
207
+ o.budget = this.budget; o.polys = this.polys; o.lines = this.lines; o.solids = this.solids; o.rects = [...this.rects]; o.slants = this.slants;
208
+ return o;
209
+ }
210
+ }
211
+
212
+ export interface LabelText { name?: string; sub?: string }
213
+ export interface PlacedLabel { x: number; y: number; anchor: 'start' | 'middle' | 'end'; rect: Rect }
214
+
215
+ const LINE = 14;
216
+ /** The rectangle a label block covers when its first baseline is at (x, y). */
217
+ export function labelRect(t: LabelText, x: number, y: number, anchor: 'start' | 'middle' | 'end'): Rect {
218
+ const w = Math.max(t.name ? textWidth(t.name, FONTS.lbl) : 0, t.sub ? textWidth(t.sub, FONTS.sub) : 0) + 4;
219
+ const lines = (t.name ? 1 : 0) + (t.sub ? 1 : 0);
220
+ const top = y - (t.name ? 11 : 9) - 1;
221
+ const h = (lines - 1) * LINE + (t.name ? 11 : 9) + 4;
222
+ const left = anchor === 'start' ? x - 2 : anchor === 'end' ? x - w + 2 : x - w / 2;
223
+ return { x: left, y: top, w, h };
224
+ }
225
+
226
+ /**
227
+ * Place a node's label. Spots, each with a penalty that says how natural it is: above the node clear of its top (the
228
+ * references' place), a little higher, beside it, then below. Each spot costs its penalty plus what it covers;
229
+ * route lines count little, since labels have a halo. The cheapest wins.
230
+ */
231
+ export function placeNodeLabel(t: LabelText, box: Box, own: Poly, obs: Obstacles, owner: string, preferred?: { x: number; y: number; anchor: 'start' | 'middle' | 'end' }): PlacedLabel {
232
+ const sb = polyBounds(own);
233
+ const cx = iso((box.x0 + box.x1) / 2, (box.y0 + box.y1) / 2, box.z1)[0];
234
+ const lines = (t.name ? 1 : 0) + (t.sub ? 1 : 0);
235
+ const blockBelowBaseline = (lines - 1) * LINE + 4;
236
+ const aboveY = sb.y - 6 - blockBelowBaseline; // the last line clears the silhouette's top
237
+ const midY = sb.y + Math.min(sb.h, 60) * 0.45;
238
+ const cands: { x: number; y: number; anchor: 'start' | 'middle' | 'end'; pen: number }[] = [];
239
+ if (preferred) cands.push({ ...preferred, pen: 0 });
240
+ cands.push({ x: cx, y: aboveY, anchor: 'middle', pen: preferred ? 20 : 0 });
241
+ for (const up of [12, 24, 40]) cands.push({ x: cx, y: aboveY - up, anchor: 'middle', pen: 20 + up });
242
+ for (const dx of [-30, 30, -60, 60]) cands.push({ x: cx + dx, y: aboveY - 4, anchor: 'middle', pen: 30 + Math.abs(dx) });
243
+ cands.push({ x: sb.x + sb.w + 8, y: midY, anchor: 'start', pen: 120 });
244
+ cands.push({ x: sb.x - 8, y: midY, anchor: 'end', pen: 120 });
245
+ cands.push({ x: cx, y: sb.y + sb.h + 14, anchor: 'middle', pen: 300 });
246
+ let best: PlacedLabel | undefined, bestCost = Infinity;
247
+ for (const c of cands) {
248
+ const rect = labelRect(t, c.x, c.y, c.anchor);
249
+ const cost = obs.cost(rect, owner, 0.15) * 2 + c.pen;
250
+ if (cost < bestCost) { bestCost = cost; best = { x: c.x, y: c.y, anchor: c.anchor, rect }; }
251
+ }
252
+ return best!;
253
+ }
254
+
255
+ export interface CalloutSpot { x: number; y: number; anchor: 'start' | 'middle' | 'end'; rect: Rect }
256
+ /** The rectangle of a callout pill whose text centre line is at y. */
257
+ export function calloutRect(w: number, x: number, y: number, anchor: 'start' | 'middle' | 'end'): Rect {
258
+ const left = anchor === 'start' ? x : anchor === 'end' ? x - w : x - w / 2;
259
+ return { x: left - 2, y: y - 14, w: w + 4, h: 28 };
260
+ }
261
+ /**
262
+ * Place a step's callout pill: where the author put it if that is clear, else around its anchor (above first),
263
+ * whichever covers least of the nodes, the labels and the step's other callouts. Callouts move; labels stay.
264
+ */
265
+ export function placeCallout(w: number, ax: number, ay: number, preferred: { x: number; y: number; anchor: 'start' | 'middle' | 'end' }, obs: Obstacles): CalloutSpot {
266
+ const cands: { x: number; y: number; anchor: 'start' | 'middle' | 'end'; pen: number }[] = [{ ...preferred, pen: 0 }];
267
+ for (const dy of [-46, -66, -86, -106, -126, -146]) for (const dx of [0, -70, 70, -140, 140]) {
268
+ cands.push({ x: ax + dx, y: ay + dy, anchor: 'middle', pen: 10 + (-dy - 46) * 0.5 + Math.abs(dx) * 0.4 });
269
+ }
270
+ for (const dy of [34, 60, 86]) cands.push({ x: ax, y: ay + dy, anchor: 'middle', pen: 60 + dy * 0.3 });
271
+ const pdx = preferred.x - ax, pdy = preferred.y - ay;
272
+ for (const k of [-1, 1]) for (const d of [16, 32, 48]) cands.push({ ...preferred, y: ay + pdy + k * d, x: ax + pdx, pen: 5 + d });
273
+ for (const k of [-1, 1]) for (const d of [30, 60, 90]) for (const e of [0, 16, -16]) cands.push({ ...preferred, x: ax + pdx + k * d, y: ay + pdy + e, pen: 8 + d * 0.6 + Math.abs(e) });
274
+ let best: CalloutSpot | undefined, bestCost = Infinity;
275
+ for (const c of cands) {
276
+ const rect = calloutRect(w, c.x, c.y, c.anchor);
277
+ const cost = obs.cost(rect, undefined, 0.05) * 3 + c.pen;
278
+ if (cost < bestCost) { bestCost = cost; best = { x: c.x, y: c.y, anchor: c.anchor, rect }; }
279
+ }
280
+ return best!;
281
+ }
282
+
283
+ const area = (p: Poly) => { let a = 0; for (let i = 0; i < p.length; i++) { const u = p[i], v = p[(i + 1) % p.length]; a += u[0] * v[1] - v[0] * u[1]; } return a / 2; };
284
+ const ccw = (p: Poly) => (area(p) < 0 ? [...p].reverse() : p);
285
+
286
+ export function inConvex(poly: Poly, q: P2): boolean {
287
+ const p = ccw(poly);
288
+ for (let i = 0; i < p.length; i++) {
289
+ const a = p[i], b = p[(i + 1) % p.length];
290
+ if ((b[0] - a[0]) * (q[1] - a[1]) - (b[1] - a[1]) * (q[0] - a[0]) < 0) return false;
291
+ }
292
+ return true;
293
+ }
294
+
295
+ /** Area of the intersection of two convex polygons (Sutherland–Hodgman, clipping a by each edge of b). */
296
+ export function convexOverlap(a: Poly, b: Poly): number {
297
+ let out: Poly = ccw(a);
298
+ const c = ccw(b);
299
+ for (let i = 0; i < c.length && out.length; i++) {
300
+ const e0 = c[i], e1 = c[(i + 1) % c.length];
301
+ const side = (p: P2) => (e1[0] - e0[0]) * (p[1] - e0[1]) - (e1[1] - e0[1]) * (p[0] - e0[0]);
302
+ const inp = out; out = [];
303
+ for (let j = 0; j < inp.length; j++) {
304
+ const p = inp[j], q = inp[(j + 1) % inp.length], sp = side(p), sq = side(q);
305
+ if (sp >= 0) out.push(p);
306
+ if ((sp >= 0) !== (sq >= 0)) { const t = sp / (sp - sq); out.push([p[0] + (q[0] - p[0]) * t, p[1] + (q[1] - p[1]) * t]); }
307
+ }
308
+ }
309
+ return out.length >= 3 ? Math.abs(area(out)) : 0;
310
+ }
@@ -0,0 +1,6 @@
1
+ {
2
+ "//": "iso-objects (L1) has no side effects: bundles drop the modules they do not use. LAYERS.md S1, S2.",
3
+ "type": "module",
4
+ "private": true,
5
+ "sideEffects": false
6
+ }