cornfield 0.42.0 → 0.44.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.
@@ -0,0 +1,130 @@
1
+ /**
2
+ * @module explorer/glyphs
3
+ *
4
+ * The built-in glyph of each node type (spec 0008 D8): a small monochrome picture drawn in the middle of a node once it is 14 px across on screen,
5
+ * where the white core dot is. It tells types of one silhouette apart beyond their colour. Both renderers draw the same picture, made once here as
6
+ * a small transparent image (like kind-letters.js), and by the same rule:
7
+ *
8
+ * a theme's icon for the node (`theme.icons`) > a finer kind's letters (spec 0009, kind-letters.js) > the type's glyph > the white core dot
9
+ *
10
+ * Canvas 2D draws the image with `drawImage` where an icon would go; WebGL uploads it as a texture and draws it in the icon pass, grouped by
11
+ * `glyph:<type>|<ink>`. The glyphs are a fixed set (one per type in core/schema.js, nothing a map holds), in two inks at most per type
12
+ * (white or dark, whichever reads on the node's fill; or the theme's `--corn-map-glyph`), so the textures are few and bounded.
13
+ */
14
+
15
+ import { NODE_THEMES } from '../core/schema.js';
16
+ import { parseColor } from './theme-model.js';
17
+
18
+ /** A node is at least this many px across (on screen) before its glyph is drawn: smaller, no glyph reads and the silhouette does the work */
19
+ export const GLYPH_MIN_PX = 14;
20
+ /** The glyph's box, as a fraction of the node's radius across (a square centred on the node, like an icon) */
21
+ export const GLYPH_RATIO = 1.2;
22
+ const SIDE = 96;
23
+ const GRID = 24; // the glyphs are drawn on a 24 by 24 grid
24
+ const KEY_PREFIX = 'glyph:';
25
+ /** The two inks the glyph takes when the theme does not set `--corn-map-glyph`: white on a deep fill, dark on a pale one */
26
+ export const INK_LIGHT = '#ffffff';
27
+ export const INK_DARK = '#0b1220';
28
+ /** The most glyph images made: types times inks is far under it; a theme that sets many colours at once cannot grow it */
29
+ export const MAX_GLYPH_IMAGES = 64;
30
+
31
+ /** Types that have a glyph: the built-in ones. A type a project invents has none (it keeps the white core dot) */
32
+ export const GLYPH_TYPES = Object.freeze(Object.keys(NODE_THEMES));
33
+ export const hasGlyph = (type) => typeof type === 'string' && Object.hasOwn(NODE_THEMES, type);
34
+
35
+ /** The key a glyph goes under in the renderers' icon tables */
36
+ export const glyphKey = (type, ink) => `${KEY_PREFIX}${type}|${ink}`;
37
+ export const isGlyphKey = (key) => typeof key === 'string' && key.startsWith(KEY_PREFIX);
38
+ /** @returns {{type: string, ink: string}} */
39
+ export function glyphOfKey(key) {
40
+ const rest = key.slice(KEY_PREFIX.length);
41
+ const at = rest.indexOf('|');
42
+ return { type: rest.slice(0, at), ink: rest.slice(at + 1) };
43
+ }
44
+
45
+ const luminanceOf = ([r, g, b]) => {
46
+ const lin = (c) => { const v = c / 255; return v <= 0.03928 ? v / 12.92 : ((v + 0.055) / 1.055) ** 2.4; };
47
+ return 0.2126 * lin(r) + 0.7152 * lin(g) + 0.0722 * lin(b);
48
+ };
49
+ const ratioOf = (a, b) => { const [hi, lo] = a > b ? [a, b] : [b, a]; return (hi + 0.05) / (lo + 0.05); };
50
+
51
+ /** The ink that reads on a fill: white or dark, whichever has the higher contrast (the glyph is at least 3:1 on every fill of the default palette: a test holds it) */
52
+ export function inkFor(fill) {
53
+ const lum = luminanceOf(parseColor(fill));
54
+ return ratioOf(1, lum) >= ratioOf(luminanceOf(parseColor(INK_DARK)), lum) ? INK_LIGHT : INK_DARK;
55
+ }
56
+
57
+ /** The ink a node's glyph is drawn in: the theme's (`--corn-map-glyph`, in the style's `values`), else the one that reads on its fill */
58
+ export const glyphInk = (node, style) => (style && style.values && style.values['--corn-map-glyph']) || inkFor(node.color);
59
+
60
+ /** Whether a node's glyph is drawn at this zoom: it has one, no icon or letters replace it, and it is wide enough on screen */
61
+ export const showsGlyph = (node, zoom) => !!node.glyph && !node.icon && !node.letters && node.r * 2 * zoom >= GLYPH_MIN_PX;
62
+
63
+ /** The glyph of each type: a function that adds its strokes and fills to a 24 by 24 grid (`ink` is already the fill and stroke style) */
64
+ const PATHS = {
65
+ root: (c) => { ring(c, 12, 12, 8); dot(c, 12, 12, 3.2); },
66
+ app: (c) => { c.beginPath(); c.rect(3.5, 5, 17, 14); c.stroke(); line(c, 3.5, 9.5, 20.5, 9.5); },
67
+ page: (c) => { c.beginPath(); c.rect(6, 3.5, 12, 17); c.stroke(); line(c, 9, 9, 15, 9); line(c, 9, 12.5, 15, 12.5); line(c, 9, 16, 13, 16); },
68
+ pkg: (c) => { c.beginPath(); c.rect(4, 7, 16, 13); c.stroke(); line(c, 4, 12, 20, 12); line(c, 12, 7, 12, 12); },
69
+ core: (c) => { ring(c, 12, 12, 8.2); dot(c, 12, 12, 4); },
70
+ token: (c) => { for (const [x, y] of [[5, 5], [13, 5], [5, 13], [13, 13]]) { c.fillRect(x, y, 6, 6); } },
71
+ atom: (c) => { c.beginPath(); c.ellipse(12, 12, 9.5, 4, -0.5, 0, Math.PI * 2); c.stroke(); dot(c, 12, 12, 3); },
72
+ molecule: (c) => { line(c, 7, 16, 17, 8); dot(c, 7, 16, 4); dot(c, 17, 8, 4); },
73
+ tool: (c) => { line(c, 12, 4, 12, 20); line(c, 4, 12, 20, 12); },
74
+ rom: (c) => { c.beginPath(); c.arc(12, 12, 7.5, 0.4, Math.PI * 1.6); c.stroke(); c.beginPath(); c.moveTo(14.5, 2.5); c.lineTo(20, 5.5); c.lineTo(14, 8.5); c.closePath(); c.fill(); },
75
+ service: (c) => { c.beginPath(); c.moveTo(13.5, 2.5); c.lineTo(6, 13.5); c.lineTo(11.5, 13.5); c.lineTo(10, 21.5); c.lineTo(18, 9.5); c.lineTo(12.5, 9.5); c.closePath(); c.fill(); },
76
+ store: (c) => { c.beginPath(); c.ellipse(12, 6.5, 7.5, 3, 0, 0, Math.PI * 2); c.stroke(); line(c, 4.5, 6.5, 4.5, 17.5); line(c, 19.5, 6.5, 19.5, 17.5); c.beginPath(); c.ellipse(12, 17.5, 7.5, 3, 0, 0, Math.PI); c.stroke(); },
77
+ table: (c) => { c.beginPath(); c.rect(4, 5, 16, 14); c.stroke(); line(c, 4, 10, 20, 10); line(c, 12, 10, 12, 19); },
78
+ external: (c) => { line(c, 7, 17, 17, 7); c.beginPath(); c.moveTo(9.5, 6.5); c.lineTo(17.5, 6.5); c.lineTo(17.5, 14.5); c.stroke(); },
79
+ stack: (c) => { for (const y of [4.5, 10.5, 16.5]) c.fillRect(4, y, 16, 3.5); },
80
+ machine: (c) => { c.beginPath(); c.rect(3.5, 4.5, 17, 11); c.stroke(); line(c, 12, 15.5, 12, 19.5); line(c, 7.5, 19.5, 16.5, 19.5); },
81
+ container: (c) => { c.beginPath(); c.rect(4, 4, 16, 16); c.stroke(); c.fillRect(8.5, 8.5, 7, 7); },
82
+ device: (c) => { c.beginPath(); c.rect(7.5, 3, 9, 18); c.stroke(); dot(c, 12, 17.5, 1.3); },
83
+ floor: (c) => { c.beginPath(); c.moveTo(3.5, 20.5); c.lineTo(3.5, 15.5); c.lineTo(9, 15.5); c.lineTo(9, 10.5); c.lineTo(14.5, 10.5); c.lineTo(14.5, 5.5); c.lineTo(20.5, 5.5); c.stroke(); },
84
+ room: (c) => { c.beginPath(); c.rect(6.5, 3.5, 11, 17); c.stroke(); dot(c, 14.5, 12.5, 1.4); },
85
+ persona: (c) => { dot(c, 12, 8, 3.8); c.beginPath(); c.arc(12, 21, 8, Math.PI * 1.15, Math.PI * 1.85); c.stroke(); },
86
+ };
87
+ function line(c, x1, y1, x2, y2) { c.beginPath(); c.moveTo(x1, y1); c.lineTo(x2, y2); c.stroke(); }
88
+ function ring(c, x, y, r) { c.beginPath(); c.arc(x, y, r, 0, Math.PI * 2); c.stroke(); }
89
+ function dot(c, x, y, r) { c.beginPath(); c.arc(x, y, r, 0, Math.PI * 2); c.fill(); }
90
+
91
+ /** Draws a glyph on a 2D context sized `side` px, in `ink` (the tone was chosen against the node's fill, so the glyph needs no halo; it sits over the white core dot) */
92
+ export function drawGlyph(ctx, type, side, ink) {
93
+ const draw = PATHS[type];
94
+ if (!draw) return;
95
+ const k = side / GRID;
96
+ ctx.save();
97
+ ctx.scale(k, k);
98
+ ctx.lineWidth = 2.4;
99
+ ctx.lineCap = 'round';
100
+ ctx.lineJoin = 'round';
101
+ ctx.strokeStyle = ink;
102
+ ctx.fillStyle = ink;
103
+ draw(ctx);
104
+ ctx.restore();
105
+ }
106
+
107
+ const images = new Map();
108
+ /** Lets the images go (a test starts afresh with it) */
109
+ export const forgetGlyphImages = () => images.clear();
110
+
111
+ /**
112
+ * The image of a type's glyph in an ink, made once (null where there is no canvas to draw one on, as in a test without a browser, and for a type with no glyph).
113
+ * @param {string} type
114
+ * @param {string} ink a CSS colour
115
+ * @returns {CanvasImageSource | null}
116
+ */
117
+ export function glyphImage(type, ink) {
118
+ if (!hasGlyph(type) || typeof ink !== 'string') return null;
119
+ const key = glyphKey(type, ink);
120
+ if (images.has(key)) return images.get(key);
121
+ if (images.size >= MAX_GLYPH_IMAGES) return null;
122
+ let image = null;
123
+ try {
124
+ const canvas = typeof OffscreenCanvas !== 'undefined' ? new OffscreenCanvas(SIDE, SIDE) : typeof document !== 'undefined' ? Object.assign(document.createElement('canvas'), { width: SIDE, height: SIDE }) : null;
125
+ const ctx = canvas && canvas.getContext && canvas.getContext('2d');
126
+ if (ctx && ctx.stroke) { drawGlyph(ctx, type, SIDE, ink); image = canvas; }
127
+ } catch { image = null; }
128
+ images.set(key, image);
129
+ return image;
130
+ }
@@ -778,8 +778,8 @@ export const CLOUD_LINE_ALPHA = { run: 0.9, deploy: 0.25, renew: 0.9 };
778
778
  /** A line an outage travels, as outage-model.js `paintOutage` draws a walked edge */
779
779
  const OUTAGE_LINE = { alpha: 0.9, width: 2.2 };
780
780
  const WHENS = Object.keys(WHEN_STYLES);
781
- /** The `when` a drawn call is for, read off its dash (scene.js draws it with `whenDash`) */
782
- const whenOfDrawn = (e) => WHENS.find((w) => String(WHEN_STYLES[w].dash) === String(e.dash)) || 'run';
781
+ /** The `when` a drawn call is for, read off its ticks (scene.js draws it with `whenTicks`) */
782
+ const whenOfDrawn = (e) => WHENS.find((w) => WHEN_STYLES[w].ticks === (e.ticks || 0)) || 'run';
783
783
 
784
784
  /**
785
785
  * The Cloud band's lines, after the scene is built: with nothing selected, `deploy` lines quiet and the rest at full;
@@ -62,6 +62,7 @@
62
62
  <p id="layers-preset-note" class="hx-note" data-corn="notice" hidden>The data-flow view shows its own tiers and ignores these toggles. Switch it off to use them.</p>
63
63
  <div id="layer-toggles-container" class="hx-toggles"></div>
64
64
  <div id="flow-legend" hidden></div>
65
+ <div id="edge-key" class="hx-menu-section" data-corn="legend-key"></div>
65
66
  <div id="cloud-legend" class="hx-menu-section" data-corn="menu-section" hidden></div>
66
67
 
67
68
  <!-- What the marks of agent runs mean, while there are runs on the map (runs-map.js) -->
@@ -77,22 +78,7 @@
77
78
  <div class="hx-legend-row"><span class="hx-mark-dot hx-mark-dot--added" data-corn="mark-dot" data-corn-state="added"></span><span>Added: dashed ring</span></div>
78
79
  <div class="hx-legend-row"><span class="hx-mark-dot hx-mark-dot--changed" data-corn="mark-dot" data-corn-state="changed"></span><span>Changed: solid ring</span></div>
79
80
  <div class="hx-legend-row"><span class="hx-mark-dot hx-mark-dot--removed" data-corn="mark-dot" data-corn-state="removed"></span><span>Removed: faded ring, label struck through</span></div>
80
- <div class="hx-legend-row"><span class="hx-mark-dot hx-mark-dot--edit" data-corn="mark-dot" data-corn-state="edit"></span><span>Your edit: dotted ring</span></div>
81
- </div>
82
-
83
- <div class="hx-menu-section" data-corn="menu-section">
84
- <div class="hx-menu-title" data-corn="menu-title">
85
- <span>Connections</span>
86
- <span class="hx-faint hx-mono">Click node to focus</span>
87
- </div>
88
- <div class="hx-legend-row hx-legend-row--split">
89
- <span class="hx-legend-label"><span data-corn="swatch" class="hx-swatch-line hx-swatch-solid"></span>Solid</span>
90
- <span class="hx-faint hx-mono">Parent-Child / Hub</span>
91
- </div>
92
- <div class="hx-legend-row hx-legend-row--split">
93
- <span class="hx-legend-label"><span data-corn="swatch" class="hx-swatch-line hx-swatch-dashed hx-swatch-indigo"></span>Dotted</span>
94
- <span class="hx-faint hx-mono">Usage &amp; Dependency</span>
95
- </div>
81
+ <div class="hx-legend-row"><span class="hx-mark-dot hx-mark-dot--edit" data-corn="mark-dot" data-corn-state="edit"></span><span>Your edit: dotted ring, long-dashed line</span></div>
96
82
  </div>
97
83
  </div><!-- .hx-menu-body: the rows scroll, the title with All and None stays (spec 0009 D8) -->
98
84
  </div>
@@ -200,7 +186,7 @@
200
186
  <div class="hx-stage" role="main" data-corn="stage">
201
187
  <!-- The map takes the keyboard when it has the focus (keyboard.js): its keys are below, read out with its name -->
202
188
  <canvas id="graph-canvas" tabindex="0" role="application" aria-roledescription="map" aria-label="Topology map" aria-describedby="map-keys-help" data-corn="map-canvas"></canvas>
203
- <p id="map-keys-help" class="hx-sr-only">Arrow keys pan, plus and minus zoom, 0 fits the map. N and P select the next and the previous node, Home and End the first and the last. Enter goes to the selected node's panel, Escape closes it. In Freeform, Alt and an arrow moves the selected node.</p>
189
+ <p id="map-keys-help" class="hx-sr-only">Arrow keys pan, plus and minus zoom, 0 fits the map. N and P select the next and the previous node, Home and End the first and the last. F frames the selected node and what it lights. Enter goes to the selected node's panel, Escape closes it. In Freeform, Alt and an arrow moves the selected node.</p>
204
190
  <!-- What the keyboard landed on, said by a screen reader (keyboard.js) -->
205
191
  <p id="map-announcer" class="hx-sr-only" role="status" aria-live="polite"></p>
206
192
  <!-- Every label, drawn on its own transparent canvas over the map (explorer/labels.js); the map canvas still gets the pointer -->
@@ -13,6 +13,7 @@
13
13
  * The keys (on the focused map; a playing scenario keeps Left, Right and Space for itself):
14
14
  * Arrow keys pan (Shift: further) + / - zoom in / out 0 fit the map
15
15
  * N / ] the next node, selected and centred P / [ the previous one Home / End the first / the last
16
+ * F frame the selected node and what it lights
16
17
  * Enter into the selected node's panel Escape close the panel
17
18
  * Alt+Arrow in Freeform, move the selected node (Shift: further)
18
19
  */
@@ -38,6 +39,7 @@ export const MAP_KEYS = [
38
39
  ['N or ]', 'select the next node'],
39
40
  ['P or [', 'select the previous node'],
40
41
  ['Home and End', 'select the first and the last node'],
42
+ ['F', 'frame the selected node and what it lights'],
41
43
  ['Enter', 'go to the selected node\'s panel'],
42
44
  ['Escape', 'close the panel'],
43
45
  ['Alt and an arrow', 'in Freeform, move the selected node'],
@@ -48,7 +50,7 @@ export const MAP_KEYS = [
48
50
  * @param {{shift?: boolean, alt?: boolean, ctrl?: boolean, meta?: boolean, scenario?: boolean, freeform?: boolean, selected?: boolean}} [opts]
49
51
  * `scenario`: a scenario is playing (its player owns Left, Right and Space); `freeform`: nodes can be moved; `selected`: a node is selected
50
52
  * @returns {null | {type: 'pan', dx: number, dy: number} | {type: 'zoom', factor: number} | {type: 'fit'} | {type: 'step', by: number}
51
- * | {type: 'first'} | {type: 'last'} | {type: 'open'} | {type: 'close'} | {type: 'move', dx: number, dy: number}}
53
+ * | {type: 'frame'} | {type: 'first'} | {type: 'last'} | {type: 'open'} | {type: 'close'} | {type: 'move', dx: number, dy: number}}
52
54
  */
53
55
  export function mapKeyAction(key, opts = {}) {
54
56
  const { shift = false, alt = false, ctrl = false, meta = false, scenario = false, freeform = false, selected = false } = opts;
@@ -68,6 +70,7 @@ export function mapKeyAction(key, opts = {}) {
68
70
  case '0': return { type: 'fit' };
69
71
  case 'n': case 'N': case ']': return { type: 'step', by: 1 };
70
72
  case 'p': case 'P': case '[': return { type: 'step', by: -1 };
73
+ case 'f': case 'F': return selected ? { type: 'frame' } : null;
71
74
  case 'Home': return { type: 'first' };
72
75
  case 'End': return { type: 'last' };
73
76
  case 'Enter': return selected ? { type: 'open' } : null;
@@ -25,6 +25,7 @@ import { saveFreeformPositions } from './layout.js';
25
25
  import { closeLegendMenu } from './layers.js';
26
26
  import * as K from './keyboard-model.js';
27
27
  import { searchActive, stepMatch } from './search.js';
28
+ import { frameSelection, litNodes } from './selection-view.js';
28
29
 
29
30
  const $ = (id) => document.getElementById(id);
30
31
  let wired = false;
@@ -63,11 +64,19 @@ export function centreOn(node) {
63
64
 
64
65
  function land(node, order) {
65
66
  if (!node) { announce('No nodes are shown. Turn a layer on in the Layers menu.'); return; }
66
- selectNode(node);
67
- centreOn(node);
67
+ selectNode(node, { frame: true }); // the keyboard's selection frames the node and what it lights (spec 0008 D6)
68
68
  announce(K.describeNode(node, order.length, order.indexOf(node)));
69
69
  }
70
70
 
71
+ /** "Frame them" and the `F` key: the camera goes to the selected node and what it lights, and a screen reader is told how many that is */
72
+ export function frameThem() {
73
+ if (!state.selectedNode) return;
74
+ const target = frameSelection();
75
+ if (!target) return;
76
+ const connected = Math.max(0, litNodes().length - 1);
77
+ announce(target.framed ? `Framed ${state.selectedNode.label} and ${connected} connected` : `${state.selectedNode.label} centred: ${connected} connected, too spread out to frame together`);
78
+ }
79
+
71
80
  /** Into the open panel: its title takes the focus, so a screen reader starts there and Tab goes on into it */
72
81
  function focusPanel() {
73
82
  const title = $('drawer-title');
@@ -123,6 +132,9 @@ function onMapKey(e) {
123
132
  land(action.type === 'first' ? order[0] : order[order.length - 1], order);
124
133
  break;
125
134
  }
135
+ case 'frame':
136
+ frameThem();
137
+ break;
126
138
  case 'open':
127
139
  focusPanel();
128
140
  break;
@@ -14,6 +14,7 @@
14
14
  */
15
15
 
16
16
  import { FONT_NODE } from './scene.js';
17
+ import { INK_DARK, INK_LIGHT, inkFor } from './glyphs.js';
17
18
 
18
19
  /** A node is at least this many px across (on screen) before its letters are drawn: smaller, they are a smudge */
19
20
  export const LETTER_MIN_PX = 14;
@@ -43,9 +44,10 @@ export const forgetLetterImages = () => images.clear();
43
44
  * @param {string} letters one or two characters
44
45
  * @returns {CanvasImageSource | null}
45
46
  */
46
- export function letterImage(letters) {
47
+ export function letterImage(letters, ink = null) {
47
48
  if (typeof letters !== 'string' || !letters) return null;
48
- if (images.has(letters)) return images.get(letters);
49
+ const cacheKey = `${typeof ink === 'string' ? ink : ''}|${letters}`;
50
+ if (images.has(cacheKey)) return images.get(cacheKey);
49
51
  if (images.size >= MAX_LETTER_IMAGES) return null;
50
52
  let image = null;
51
53
  try {
@@ -58,13 +60,15 @@ export function letterImage(letters) {
58
60
  ctx.textBaseline = 'middle';
59
61
  ctx.lineJoin = 'round';
60
62
  ctx.lineWidth = 10;
61
- ctx.strokeStyle = 'rgba(255, 255, 255, 0.92)';
63
+ // dark ink under a light outline by default; a theme's `--corn-map-glyph` is the ink, under the outline of the opposite tone
64
+ const fill = typeof ink === 'string' ? ink : INK_DARK;
65
+ ctx.strokeStyle = inkFor(fill) === INK_LIGHT ? 'rgba(11, 18, 32, 0.92)' : 'rgba(255, 255, 255, 0.92)';
62
66
  ctx.strokeText(letters, SIDE / 2, SIDE / 2 + 3);
63
- ctx.fillStyle = '#0b1220';
67
+ ctx.fillStyle = fill;
64
68
  ctx.fillText(letters, SIDE / 2, SIDE / 2 + 3);
65
69
  image = canvas;
66
70
  }
67
71
  } catch { image = null; }
68
- images.set(letters, image);
72
+ images.set(cacheKey, image);
69
73
  return image;
70
74
  }
@@ -18,7 +18,7 @@
18
18
  * A label is never drawn under 11 px (a hub's, 13 px, bold; a theme may raise the 11 with `--corn-map-label-min`): text that
19
19
  * would be smaller is not drawn smaller, it competes for room at 11 px. Zoomed in, it grows with the map.
20
20
  * A node left without a label is named by a chip over everything else while the pointer is on it (`hover`).
21
- * Lane titles, notes and arrows and the selection's edge labels are always included and take no part in the placing.
21
+ * Lane titles, notes and arrows are always included and take no part in the placing. The kind words on a selection's lit edges (spec 0008 D7) are placed last, after every node label: a word is drawn only where it touches no node label, no lit node's disc and no word before it.
22
22
  * The layer redraws only when the camera, the scene, the size or the hover changed, so a packet frame (the renderer
23
23
  * redrawing for animation) costs the labels nothing.
24
24
  *
@@ -26,7 +26,7 @@
26
26
  * never the explorer's state.
27
27
  *
28
28
  * @typedef {object} LabelItem One piece of text to draw
29
- * @property {'node'|'edge'|'lane'|'chip'} kind
29
+ * @property {'node'|'edge'|'lane'|'chip'} kind `edge`: a kind word on a lit edge, in screen space
30
30
  * @property {string} [id] the node, for `kind: 'node'` and `'chip'`
31
31
  * @property {string} text
32
32
  * @property {number} x world coordinates; the text is drawn on this baseline point
@@ -44,11 +44,13 @@
44
44
  * @property {boolean} [screen] a node's label at its minimum size: drawn in screen space at `px`, so the font stays one string while the zoom changes (a changing font string is the slow part of drawing text)
45
45
  */
46
46
 
47
- import { EDGE_LABEL_SIZE, FONT_EDGE, FONT_NODE, HUB_LABEL_EXTRA_PX, LABEL_MIN_FLOOR, LABEL_MIN_PX, LANDMARK_TYPES, looksFor } from './scene.js';
47
+ import { FONT_EDGE, FONT_NODE, HUB_LABEL_EXTRA_PX, LABEL_MIN_FLOOR, LABEL_MIN_PX, LANDMARK_TYPES, looksFor } from './scene.js';
48
48
  import { getMapStyle, styledScene } from './map-style.js';
49
49
 
50
50
  /** At most this many node labels in view at a time (lane text and selection edge labels are extra); the placing itself never needs it */
51
51
  export const MAX_LABELS = 400;
52
+ /** At most this many kind words on the lit edges in view (spec 0008 D7) */
53
+ export const MAX_EDGE_WORDS = 120;
52
54
  /** A label this many screen px past the viewport is still kept, so none pops in at the border */
53
55
  export const CULL_MARGIN = 32;
54
56
  /** A label whose box, this many px bigger on every side, touches a placed one is skipped: placed labels are at least this far apart */
@@ -162,7 +164,7 @@ export function placeLabels(scene, zoom, opts = {}) {
162
164
  * `max` node labels in view (default 400); `margin` px past the viewport (default 32); `measure` the width of a text at a font
163
165
  * size (default: an estimate from its length); `hover`: the node the pointer is on, named by a chip when it has no label;
164
166
  * `placed`: the placing for this scene at this zoom, when the caller has kept it (default: made here)
165
- * @returns {LabelItem[]} lane text, then edge labels, then node labels in scene order, then the chip
167
+ * @returns {LabelItem[]} lane text, then node labels in scene order, then the words on the lit edges, then the chip
166
168
  */
167
169
  export function chooseLabels(scene, camera, viewport, opts = {}) {
168
170
  const { max = MAX_LABELS, margin = CULL_MARGIN, measure = null, hover = null } = opts;
@@ -175,12 +177,6 @@ export function chooseLabels(scene, camera, viewport, opts = {}) {
175
177
  if (t) out.push({ kind: 'lane', text: t.text, x: t.x, y: t.y, size: t.size, bold: t.bold, italic: false, color: t.color, alpha: 1, outline: null, align: 'center', family: scene.labelFont || FONT_NODE });
176
178
  }
177
179
  }
178
- // ... and the labels on the selection's edges
179
- for (const edge of scene.edges) {
180
- const l = edge.label;
181
- if (l) out.push({ kind: 'edge', text: l.text, x: l.x, y: l.y, size: EDGE_LABEL_SIZE, bold: false, italic: !!l.italic, color: l.color, alpha: edge.alpha, outline: outline.edge, align: 'center', family: scene.labelFont || FONT_EDGE });
182
- }
183
-
184
180
  // Node labels: the placing for this zoom, then the boxes that reach the viewport
185
181
  const { zoom } = camera;
186
182
  const placed = opts.placed || placeLabels(scene, zoom, { measure });
@@ -196,6 +192,30 @@ export function chooseLabels(scene, camera, viewport, opts = {}) {
196
192
  out.push({ kind: 'node', id: p.id, text: p.node.label, x: p.x, y: p.y, size: p.size, bold: p.bold, italic: false, color: p.node.labelColor, alpha: p.node.labelAlpha, outline: outline.node ? { color: outline.node.color, width: outline.node.width * k } : null, align: 'center', family, strike: !!p.node.labelStrike, px: p.px, screen: p.screen });
197
193
  }
198
194
 
195
+ // The words on the lit edges ("reads", "calls": spec 0008 D7) go last, below every node label: one is placed only where it touches no node label, no lit
196
+ // node's disc and no word placed before it, so it never covers a name; at the label minimum size, in screen space
197
+ const px = labelMinOf(scene);
198
+ const taken = inView.map((p) => ({ l: camera.x + p.l, r: camera.x + p.r, t: camera.y + p.t, b: camera.y + p.b }));
199
+ for (const n of scene.nodes) {
200
+ if (n.alpha < 0.5) continue;
201
+ const cx = camera.x + n.x * zoom, cy = camera.y + n.y * zoom, rr = n.r * zoom;
202
+ if (cx + rr < 0 || cx - rr > viewport.width || cy + rr < 0 || cy - rr > viewport.height) continue;
203
+ taken.push({ l: cx - rr, r: cx + rr, t: cy - rr, b: cy + rr });
204
+ }
205
+ let words = 0;
206
+ for (const edge of scene.edges) {
207
+ const l = edge.label;
208
+ if (!l || words >= MAX_EDGE_WORDS) continue;
209
+ const sx = camera.x + l.x * zoom, sy = camera.y + l.y * zoom;
210
+ if (sx < 0 || sx > viewport.width || sy < 0 || sy > viewport.height) continue;
211
+ const w = (measure ? measure(l.text, px, false) : l.text.length * px * 0.55) / 2 + LABEL_PAD;
212
+ const box = { l: sx - w, r: sx + w, t: sy - px * 0.6 - LABEL_PAD, b: sy + px * 0.6 + LABEL_PAD };
213
+ if (taken.some((o) => box.l < o.r && box.r > o.l && box.t < o.b && box.b > o.t)) continue;
214
+ taken.push(box);
215
+ words++;
216
+ out.push({ kind: 'edge', text: l.text, x: l.x, y: l.y + (px * 0.35) / zoom, size: px / zoom, px, screen: true, bold: false, italic: !!l.italic, color: l.color, alpha: edge.alpha, outline: outline.edge, align: 'center', family: scene.labelFont || FONT_EDGE });
217
+ }
218
+
199
219
  // The chip: a node with no label, named while the pointer is on it
200
220
  if (hover) {
201
221
  const node = scene.nodes.find((n) => n.id === hover);
@@ -271,26 +291,6 @@ function drawItem(ctx, t, pen, v = WORLD) {
271
291
  }
272
292
  }
273
293
 
274
- /**
275
- * Erases every node circle from what is drawn so far. The map used to draw an edge's label before
276
- * the circles, so a node covered it; this layer is above the circles, so it cuts them out of the
277
- * lane and edge text instead (a dimmed node, which hides almost nothing, does not cut).
278
- */
279
- function coverWithNodes(ctx, nodes) {
280
- ctx.save();
281
- ctx.globalCompositeOperation = 'destination-out';
282
- ctx.globalAlpha = 1;
283
- ctx.fillStyle = '#000';
284
- ctx.beginPath();
285
- nodes.forEach((n) => {
286
- if (n.alpha < 0.5) return;
287
- ctx.moveTo(n.x + n.r, n.y);
288
- ctx.arc(n.x, n.y, n.r, 0, Math.PI * 2);
289
- });
290
- ctx.fill();
291
- ctx.restore();
292
- }
293
-
294
294
  /**
295
295
  * The placed label under a screen point, or null: the one drawn on top (the last in scene order) when two overlap their padding
296
296
  * @param {ReturnType<typeof placeLabels>} placed
@@ -375,11 +375,9 @@ export function createLabelLayer(canvas, opts = {}) {
375
375
  // The placing is for the whole map at this zoom: kept while the scene, the zoom and the style stay (a pan, a hover), made again when one changes
376
376
  if (!samePlacing) kept = placeLabels(scene, camera.zoom, { measure });
377
377
  const items = chooseLabels(scene, camera, { width, height }, { measure, hover, placed: kept });
378
- // The lane text and the selection's edge labels, then the circles cut out of them (only when there are edge labels), then the node labels
378
+ // The lane text (the words on the lit edges are placed below the node labels, and drawn with them in screen space, below)
379
379
  let pen = newPen();
380
- let edges = false;
381
- for (const t of items) { if (t.kind === 'lane' || t.kind === 'edge') { drawItem(ctx, t, pen); if (t.kind === 'edge') edges = true; } }
382
- if (edges) { coverWithNodes(ctx, scene.nodes); pen = newPen(); }
380
+ for (const t of items) if (t.kind === 'lane') drawItem(ctx, t, pen);
383
381
  for (const t of items) if (t.kind === 'node' && !t.screen) drawItem(ctx, t, pen); // a label that grows with the map: the camera's transform
384
382
  ctx.restore();
385
383
  // A label at its minimum size is drawn in screen space at a whole font size, whatever the zoom: the font string does not change as the
@@ -387,6 +385,7 @@ export function createLabelLayer(canvas, opts = {}) {
387
385
  const screen = { ox: camera.x, oy: camera.y, k: camera.zoom };
388
386
  pen = newPen();
389
387
  for (const t of items) if (t.kind === 'node' && t.screen) drawItem(ctx, t, pen, screen);
388
+ for (const t of items) if (t.kind === 'edge') drawItem(ctx, t, pen, screen); // the kind words: where no node label, disc or word is
390
389
  for (const t of items) if (t.kind === 'chip') drawItem(ctx, t, pen, screen);
391
390
  return true;
392
391
  },