archidocs 1.0.0 → 1.2.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.
@@ -1,60 +1,61 @@
1
1
  import { describeLens } from "../core/saved-views.js";
2
- import { FONT_BASE64, FONT_FAMILY } from "./font.js";
2
+ import { FONT_FAMILY, FONTS } from "./font.js";
3
3
  import { teamStyles } from "./teams.js";
4
4
  import { cut, fmt, text, textWidth } from "./text.js";
5
+ import { flatten, PALETTES } from "./tokens.js";
5
6
  const PAD = 24;
6
7
  const HEADER = 72;
7
8
  const KEY = 44;
8
- const COLOR = {
9
- background: "#ffffff",
10
- ink: "#1b2330",
11
- muted: "#5b6573",
12
- border: "#8a94a3",
13
- node: "#ffffff",
14
- system: "#eef1f5",
15
- container: "#f6f8fa",
16
- edge: "#5b6573",
17
- lens: "#c2410c",
18
- };
19
- /** How far a node or edge the Lens misses fades. */
20
- const DIM = 0.35;
9
+ /** Every frame, as the viewer draws it. */
10
+ const FRAME = 1.5;
11
+ /** A node's Team-coloured left edge. */
12
+ const EDGE = 6;
13
+ /** The line under a picture's title: the label if given, else the Workspace, short commit and date. */
14
+ export const sourceLine = (source, label) => label ?? [source.workspace, source.commit?.slice(0, 7) ?? "uncommitted", ...(source.date ? [source.date.slice(0, 10)] : [])].join(" · ");
15
+ /** The `<svg>` open tag, embedded fonts and background every picture starts with. */
16
+ export const opening = (width, height, ground, defs) => [
17
+ `<svg xmlns="http://www.w3.org/2000/svg" width="${width}" height="${height}" viewBox="0 0 ${width} ${height}" font-family="${FONT_FAMILY}, sans-serif">`,
18
+ `<style>${FONTS.map((f) => `@font-face{font-family:"${FONT_FAMILY}";font-weight:${f.weight};src:url(data:font/ttf;base64,${f.base64}) format("truetype")}`).join("")}</style>`,
19
+ `<defs>${defs}</defs>`,
20
+ `<rect width="${width}" height="${height}" fill="${ground}"/>`,
21
+ ];
21
22
  /**
22
- * Draws a laid-out View as one SVG document: the title and source line, the
23
- * nodes and edges, and a key. It is a pure function of its input, so the same
24
- * View at the same commit always gives the same bytes; it needs no DOM, and
25
- * runs in Node and the browser alike.
23
+ * Draws a laid-out View as one SVG document in the viewer's Wayfinding Plate
24
+ * look: the title and source line, the nodes and edges, and a key. It is a
25
+ * pure function of its input, so the same View at the same commit always
26
+ * gives the same bytes; it needs no DOM, and runs in Node and the browser alike.
27
+ * Nothing in it is faded: what a Lens leaves out steps back to solid faint colours.
26
28
  */
27
- export function renderSvg({ bundle, view, layout, source }) {
29
+ export function renderSvg({ bundle, view, layout, source, theme = "light", label }) {
30
+ const c = PALETTES[theme];
28
31
  const elements = new Map(bundle.elements.map((e) => [e.id, e]));
29
32
  const targets = new Map(bundle.targets.map((t) => [t.id, t]));
30
33
  const teams = teamStyles(bundle);
31
34
  const boxes = new Map(layout.nodes.map((n) => [n.id, n]));
32
35
  const containers = new Set(view.nodes.map((n) => n.parent).filter((p) => p !== null));
33
36
  const title = view.title;
34
- const sourceLine = [source.workspace, source.commit?.slice(0, 7) ?? "uncommitted", ...(source.date ? [source.date.slice(0, 10)] : [])].join(" · ");
35
- const key = drawKey(view.lens ? `Lens: ${describeLens(view.lens)}` : undefined);
36
- const width = Math.ceil(Math.max(layout.width, key.width, textWidth(title, 20), textWidth(sourceLine, 12)) + 2 * PAD);
37
+ const subtitle = sourceLine(source, label);
38
+ const key = drawKey(c, view.lens);
39
+ const width = Math.ceil(Math.max(layout.width, key.width, textWidth(title, 20, 700), textWidth(subtitle, 12)) + 2 * PAD);
37
40
  const height = Math.ceil(HEADER + layout.height + PAD + KEY);
38
41
  const depth = (n) => (n.parent === null ? 0 : 1 + depth(view.nodes.find((p) => p.id === n.parent)));
39
42
  const byDepth = view.nodes.map((n) => ({ n, d: depth(n) })).sort((a, b) => a.d - b.d).map(({ n }) => n);
43
+ const ring = view.lens !== undefined && "team" in view.lens;
44
+ let clips = 0;
40
45
  const drawn = (n) => {
41
46
  const box = boxes.get(n.id);
42
47
  const element = elements.get(n.element ?? n.id);
43
48
  const target = n.kind === "target" ? targets.get(n.id) : undefined;
44
49
  const team = teams.get(element?.owner ?? target?.owner ?? "");
45
- const drawing = containers.has(n.id) ? drawContainer(n, box, team, target?.kind) : drawNode(n, box, element, team);
46
- return n.mark && !n.mark.hit ? `<g opacity="${DIM}">${drawing}</g>` : drawing;
50
+ return containers.has(n.id) ? drawContainer(c, n, box, team, target?.kind) : drawNode(c, n, box, element, team, ring, `clip-${clips++}`);
47
51
  };
48
52
  return [
49
- `<svg xmlns="http://www.w3.org/2000/svg" width="${width}" height="${height}" viewBox="0 0 ${width} ${height}" font-family="${FONT_FAMILY}, sans-serif">`,
50
- `<style>@font-face{font-family:"${FONT_FAMILY}";src:url(data:font/ttf;base64,${FONT_BASE64}) format("truetype")}</style>`,
51
- `<defs>${marker("arrow", COLOR.edge)}${view.lens ? marker("arrow-lit", COLOR.lens) : ""}</defs>`,
52
- `<rect width="${width}" height="${height}" fill="${COLOR.background}"/>`,
53
- text(PAD, 34, title, { size: 20, fill: COLOR.ink }),
54
- text(PAD, 56, sourceLine, { size: 12, fill: COLOR.muted }),
53
+ ...opening(width, height, c.ground, `${marker("arrow", c.edge)}${view.lens ? marker("arrow-faint", c["edge-faint"]) + marker("arrow-lit", c.ink) : ""}${brokerPattern(c)}`),
54
+ text(PAD, 34, title, { size: 20, fill: c.ink, weight: 700 }),
55
+ text(PAD, 56, subtitle, { size: 12, fill: c.muted }),
55
56
  `<g transform="translate(${PAD},${HEADER})">`,
56
57
  ...byDepth.filter((n) => containers.has(n.id)).map(drawn),
57
- ...layout.edges.map((e) => drawEdge(e.points, view.edges.find((v) => v.from === e.from && v.to === e.to))),
58
+ ...layout.edges.map((e) => drawEdge(c, e.points, view.edges.find((v) => v.from === e.from && v.to === e.to))),
58
59
  ...byDepth.filter((n) => !containers.has(n.id)).map(drawn),
59
60
  `</g>`,
60
61
  `<g transform="translate(${PAD},${fmt(HEADER + layout.height + PAD)})">`,
@@ -65,88 +66,150 @@ export function renderSvg({ bundle, view, layout, source }) {
65
66
  ].join("\n");
66
67
  }
67
68
  const marker = (id, fill) => `<marker id="${id}" viewBox="0 0 10 10" refX="10" refY="5" markerWidth="8" markerHeight="8" orient="auto-start-reverse"><path d="M0,0 L10,5 L0,10 z" fill="${fill}"/></marker>`;
68
- /** A node's outline: the Lens colour, thicker, when the Lens hits it. */
69
- const outline = (n) => (n.mark?.hit ? `stroke="${COLOR.lens}" stroke-width="2.5"` : `stroke="${COLOR.border}" stroke-width="1.25"`);
70
- /** A badge on a node's top-left corner with its distance from the impact's origin. */
71
- function distanceBadge(n, box) {
72
- if (n.mark?.distance === undefined)
69
+ /** A broker's vertical stripes, in the rule colour. */
70
+ const brokerPattern = (c) => `<pattern id="broker" width="12" height="12" patternUnits="userSpaceOnUse"><rect x="8" width="4" height="12" fill="${c.rule}"/></pattern>`;
71
+ /** A rectangle with its own radius per corner (top-left, top-right, bottom-right, bottom-left), as a path. */
72
+ function shape(x, y, w, h, [tl, tr, br, bl]) {
73
+ const arc = (r, x2, y2) => (r ? `A${fmt(r)},${fmt(r)} 0 0 1 ${fmt(x2)},${fmt(y2)} ` : "");
74
+ return (`M${fmt(x + tl)},${fmt(y)} L${fmt(x + w - tr)},${fmt(y)} ${arc(tr, x + w, y + tr)}` +
75
+ `L${fmt(x + w)},${fmt(y + h - br)} ${arc(br, x + w - br, y + h)}` +
76
+ `L${fmt(x + bl)},${fmt(y + h)} ${arc(bl, x, y + h - bl)}` +
77
+ `L${fmt(x)},${fmt(y + tl)} ${arc(tl, x + tl, y)}Z`);
78
+ }
79
+ /** A reversed Team code tile with the Team colour as its leading band, its right edge at `right`. */
80
+ export function codeTile(c, team, right, top) {
81
+ const w = textWidth(team.code, 10, 700) + 16;
82
+ const x = right - w;
83
+ return [
84
+ `<rect x="${fmt(x)}" y="${fmt(top)}" width="${fmt(w)}" height="16" fill="${c["code-bg"]}"/>`,
85
+ `<rect x="${fmt(x)}" y="${fmt(top)}" width="5" height="16" fill="${team.color}"/>`,
86
+ text(x + 10, top + 12, team.code, { size: 10, fill: c["code-fg"], weight: 700 }),
87
+ ].join("");
88
+ }
89
+ function look(c, n, fill, team, ringLens) {
90
+ const plain = { frame: c.ink, fill, name: c.ink, sub: c.muted, edge: team?.color ?? c.ink };
91
+ const mark = n.mark;
92
+ if (!mark)
93
+ return plain;
94
+ if (!mark.hit)
95
+ return { frame: c.line, fill: c.ground, name: c.faint, sub: c.faint, edge: c.line };
96
+ if (ringLens)
97
+ return { ...plain, fill: c.paper, ring: team?.color ?? c.ink };
98
+ if (mark.side === "origin")
99
+ return { ...plain, fill: c.herebg, edge: c.ink, ring: c.here };
100
+ if (mark.side === "upstream")
101
+ return { ...plain, frame: c.up, fill: c.upbg, edge: c.up };
102
+ if (mark.side === "downstream")
103
+ return { ...plain, frame: c.down, fill: c.downbg, edge: c.down, double: true };
104
+ return { ...plain, frame: c.path, fill: c.pathbg, edge: c.path };
105
+ }
106
+ /** An impact badge on a node's top edge: the origin, or the side and distance from it; a bare distance on a path lens. */
107
+ function badge(c, n, box) {
108
+ const mark = n.mark;
109
+ if (mark?.distance === undefined)
73
110
  return [];
74
- const label = String(n.mark.distance);
75
- const r = 9;
111
+ const words = mark.side === "origin" ? "ORIGIN" : mark.side ? `${mark.side.toUpperCase()} ${mark.distance}` : String(mark.distance);
112
+ const w = textWidth(words, 10, 700) + 12;
113
+ const x = box.x + box.width - 36 - w;
114
+ const y = box.y - 10;
115
+ const [fill, ink, frame] = mark.side === "origin" ? [c.here, c["on-here"], c.ink] : mark.side === "upstream" ? [c.up, c.paper, undefined] : mark.side === "downstream" ? [c.down, c.paper, undefined] : [c.paper, c.ink, c.ink];
76
116
  return [
77
- `<circle cx="${fmt(box.x)}" cy="${fmt(box.y)}" r="${r}" fill="${COLOR.lens}"/>`,
78
- text(box.x - textWidth(label, 10) / 2, box.y + 3.5, label, { size: 10, fill: COLOR.background }),
117
+ `<rect x="${fmt(x)}" y="${fmt(y)}" width="${fmt(w)}" height="16" fill="${fill}"${frame ? ` stroke="${frame}" stroke-width="${FRAME}"` : ""}/>`,
118
+ text(x + 6, y + 12, words, { size: 10, fill: ink, weight: 700 }),
79
119
  ];
80
120
  }
81
- function drawNode(n, box, element, team) {
121
+ function drawNode(c, n, box, element, team, ringLens, clip) {
82
122
  const { x, y, width: w, height: h } = box;
83
- const person = n.kind === "person";
123
+ const type = element?.kind === "service" ? element.type : undefined;
124
+ const system = n.kind === "system";
125
+ const l = look(c, n, system ? c.tint : c.paper, team, ringLens);
126
+ const r = n.kind === "person" ? Math.min(36, h / 2) : 0;
127
+ const d = shape(x, y, w, h, n.kind === "person" ? [r, r, r, r] : type === "datastore" ? [0, 0, 12, 12] : [0, 0, 0, 0]);
84
128
  const dash = n.external ? ` stroke-dasharray="6 4"` : "";
85
- const fill = n.kind === "system" ? COLOR.system : COLOR.node;
86
- // A Person's pill has no corner to hold a Team code, so only the other kinds show one.
87
- const code = person ? "" : (team?.code ?? "");
88
- const codeWidth = code ? textWidth(code, 10) + 8 : 0;
89
- const left = person ? 20 : 16;
129
+ const frameWidth = system ? 2 : FRAME;
130
+ const tileWidth = team ? textWidth(team.code, 10, 700) + 16 + 8 : 0;
131
+ const left = x + EDGE + (n.kind === "person" ? 12 : 8);
132
+ const right = n.kind === "person" ? x + w - 16 : x + w - 10;
90
133
  return [
91
134
  `<g>`,
92
- `<rect x="${fmt(x)}" y="${fmt(y)}" width="${fmt(w)}" height="${fmt(h)}" rx="${person ? fmt(h / 2) : 8}" fill="${fill}" ${outline(n)}${dash}/>`,
93
- ...(team && !person ? [`<rect x="${fmt(x + 1)}" y="${fmt(y + 1)}" width="5" height="${fmt(h - 2)}" fill="${team.color}"/>`] : []),
94
- text(x + left, y + h / 2 - 4, cut(n.name, 14, w - left - 10 - codeWidth), { size: 14, fill: COLOR.ink }),
95
- text(x + left, y + h / 2 + 14, cut(subLabel(n, element), 11, w - left - 10), { size: 11, fill: COLOR.muted }),
96
- ...(code ? [text(x + w - 10, y + 18, code, { size: 10, fill: COLOR.muted, anchor: "end" })] : []),
97
- ...distanceBadge(n, box),
135
+ ...(l.ring ? [`<path d="${shape(x - 4, y - 4, w + 8, h + 8, r ? [r + 4, r + 4, r + 4, r + 4] : type === "datastore" ? [0, 0, 16, 16] : [0, 0, 0, 0])}" fill="${l.ring}"/>`] : []),
136
+ `<clipPath id="${clip}"><path d="${d}"/></clipPath>`,
137
+ `<path d="${d}" fill="${l.fill}"/>`,
138
+ ...(type === "broker" ? [`<path d="${d}" fill="url(#broker)"/>`] : []),
139
+ `<path d="${d}" fill="none" stroke="${l.frame}" stroke-width="${frameWidth}"${dash}/>`,
140
+ ...(l.double ? [`<path d="${shape(x + 3, y + 3, w - 6, h - 6, [0, 0, type === "datastore" ? 9 : 0, type === "datastore" ? 9 : 0])}" fill="none" stroke="${l.frame}" stroke-width="${FRAME}"/>`] : []),
141
+ `<rect x="${fmt(x - 1)}" y="${fmt(y - 1)}" width="${EDGE + 1}" height="${fmt(h + 2)}" fill="${l.edge}" clip-path="url(#${clip})"/>`,
142
+ text(left, y + h / 2 - 4, cut(n.name, 13, right - left - tileWidth, 700), { size: 13, fill: l.name, weight: 700 }),
143
+ text(left, y + h / 2 + 13, cut(subLabel(n, element), 11, right - left), { size: 11, fill: l.sub }),
144
+ ...(team ? [codeTile(c, team, right, y + 8)] : []),
145
+ ...badge(c, n, box),
146
+ ...(n.mark?.hit && n.collapsed ? [`<rect x="${fmt(x + w - 42)}" y="${fmt(y + h - 18)}" width="10" height="10" fill="${c.ink}" stroke="${c.paper}" stroke-width="2"/>`] : []),
98
147
  `</g>`,
99
148
  ].join("");
100
149
  }
101
- function drawContainer(n, box, team, targetKind) {
150
+ /** An open box: a quiet line frame on the open-box tint, its head row at the top; a Deployment Target is a quiet frame with no fill. */
151
+ function drawContainer(c, n, box, team, targetKind) {
102
152
  const { x, y, width: w, height: h } = box;
153
+ const target = n.kind === "target";
154
+ const dimmed = n.mark && !n.mark.hit;
155
+ const frame = n.external ? c.ink : c.line;
156
+ const fill = target || dimmed ? "none" : flatten(c.container, c.ground);
103
157
  const dash = n.external ? ` stroke-dasharray="6 4"` : "";
104
- const code = team?.code ?? "";
105
- const codeWidth = code ? textWidth(code, 10) + 8 : 0;
158
+ const tileWidth = team ? textWidth(team.code, 10, 700) + 16 + 8 : 0;
159
+ const left = x + (target ? 12 : EDGE + 10);
106
160
  return [
107
161
  `<g>`,
108
- `<rect x="${fmt(x)}" y="${fmt(y)}" width="${fmt(w)}" height="${fmt(h)}" rx="10" fill="${COLOR.container}" ${outline(n)}${dash}/>`,
109
- ...(team ? [`<rect x="${fmt(x + 1)}" y="${fmt(y + 1)}" width="5" height="${fmt(h - 2)}" fill="${team.color}"/>`] : []),
110
- text(x + 16, y + 24, cut(`${n.name} · ${targetKind ?? kindName(n)}`, 13, w - 26 - codeWidth), { size: 13, fill: COLOR.ink }),
111
- ...(code ? [text(x + w - 10, y + 22, code, { size: 10, fill: COLOR.muted, anchor: "end" })] : []),
112
- ...distanceBadge(n, box),
162
+ `<rect x="${fmt(x)}" y="${fmt(y)}" width="${fmt(w)}" height="${fmt(h)}" fill="${fill}" stroke="${frame}" stroke-width="${FRAME}"${dash}/>`,
163
+ ...(target ? [] : [`<rect x="${fmt(x - FRAME / 2)}" y="${fmt(y - FRAME / 2)}" width="${EDGE}" height="${fmt(h + FRAME)}" fill="${frame}"/>`]),
164
+ text(left, y + 22, cut(`${n.name} · ${targetKind ?? kindName(n)}`, 13, x + w - 10 - left - tileWidth, 700), { size: 13, fill: dimmed ? c.faint : c.ink, weight: 700 }),
165
+ ...(team ? [codeTile(c, team, x + w - 10, y + 9)] : []),
166
+ ...badge(c, n, box),
113
167
  `</g>`,
114
168
  ].join("");
115
169
  }
116
- function drawEdge(points, edge) {
117
- const async = edge.relationships.every((r) => r.interaction === "async");
170
+ /** A Relationship line: lit in ink by a Lens, stepped back to a solid fainter stroke when the Lens misses it. */
171
+ function drawEdge(c, points, edge) {
118
172
  const d = points.map((p, i) => `${i === 0 ? "M" : "L"}${fmt(p.x)},${fmt(p.y)}`).join(" ");
119
- const dash = async ? ` stroke-dasharray="6 4"` : "";
120
173
  if (edge.lit)
121
- return `<path d="${d}" fill="none" stroke="${COLOR.lens}" stroke-width="2"${dash} marker-end="url(#arrow-lit)"/>`;
122
- const path = `<path d="${d}" fill="none" stroke="${COLOR.edge}" stroke-width="1.25"${dash} marker-end="url(#arrow)"/>`;
123
- return edge.lit === false ? `<g opacity="${DIM}">${path}</g>` : path;
174
+ return `<path d="${d}" fill="none" stroke="${c.ink}" stroke-width="2.5" marker-end="url(#arrow-lit)"/>`;
175
+ if (edge.lit === false)
176
+ return `<path d="${d}" fill="none" stroke="${c["edge-faint"]}" stroke-width="${FRAME}" marker-end="url(#arrow-faint)"/>`;
177
+ return `<path d="${d}" fill="none" stroke="${c.edge}" stroke-width="${FRAME}" marker-end="url(#arrow)"/>`;
124
178
  }
125
- /** The key: what the line styles, the Team stripe and code, and a dashed box mean, and the Lens if there is one. */
126
- function drawKey(lens) {
179
+ /** The key, in the canvas's own vocabulary: the Team tile, External, broker, and the Lens if there is one. */
180
+ function drawKey(c, lens) {
127
181
  const parts = [];
128
182
  let x = 0;
129
183
  const label = (s) => {
130
- parts.push(text(x, 16, s, { size: 11, fill: COLOR.muted }));
184
+ parts.push(text(x, 16, s, { size: 11, fill: c.muted }));
131
185
  x += textWidth(s, 11) + 24;
132
186
  };
133
- for (const [name, dash] of [["Sync", ""], ["Async", ` stroke-dasharray="6 4"`]]) {
134
- parts.push(`<path d="M${x},12 L${x + 32},12" fill="none" stroke="${COLOR.edge}" stroke-width="1.25"${dash} marker-end="url(#arrow)"/>`);
135
- x += 40;
136
- label(name);
137
- }
138
- parts.push(`<rect x="${x}" y="2" width="36" height="20" rx="4" fill="${COLOR.node}" stroke="${COLOR.border}"/>`);
139
- parts.push(`<rect x="${x + 1}" y="3" width="5" height="18" fill="${COLOR.muted}"/>`);
140
- parts.push(text(x + 33, 15, "ABC", { size: 8, fill: COLOR.muted, anchor: "end" }));
141
- x += 44;
187
+ const swatch = (attrs) => {
188
+ parts.push(`<rect x="${fmt(x)}" y="4" width="28" height="16" ${attrs}/>`);
189
+ x += 36;
190
+ };
191
+ parts.push(codeTile(c, { code: "ABC", color: c.muted }, x + textWidth("ABC", 10, 700) + 16, 4));
192
+ x += textWidth("ABC", 10, 700) + 24;
142
193
  label("Team (stripe and code)");
143
- parts.push(`<rect x="${x}" y="2" width="36" height="20" rx="4" fill="${COLOR.node}" stroke="${COLOR.border}" stroke-dasharray="6 4"/>`);
144
- x += 44;
194
+ swatch(`fill="${c.paper}" stroke="${c.ink}" stroke-width="${FRAME}" stroke-dasharray="6 4"`);
145
195
  label("External");
196
+ parts.push(`<rect x="${fmt(x)}" y="4" width="28" height="16" fill="${c.paper}"/><rect x="${fmt(x)}" y="4" width="28" height="16" fill="url(#broker)" stroke="${c.ink}" stroke-width="${FRAME}"/>`);
197
+ x += 36;
198
+ label("Broker");
146
199
  if (lens) {
147
- parts.push(`<rect x="${x}" y="2" width="36" height="20" rx="4" fill="${COLOR.node}" stroke="${COLOR.lens}" stroke-width="2.5"/>`);
148
- x += 44;
149
- label(lens);
200
+ if ("impact" in lens) {
201
+ swatch(`fill="${c.herebg}" stroke="${c.here}" stroke-width="2.5"`);
202
+ label("Origin");
203
+ swatch(`fill="${c.up}"`);
204
+ label("Upstream");
205
+ swatch(`fill="${c.down}"`);
206
+ label("Downstream");
207
+ }
208
+ else if ("team" in lens)
209
+ swatch(`fill="${c.paper}" stroke="${c.muted}" stroke-width="3"`);
210
+ else
211
+ swatch(`fill="${c.pathbg}" stroke="${c.path}" stroke-width="${FRAME}"`);
212
+ label(`Lens: ${describeLens(lens)}`);
150
213
  }
151
214
  return { parts, width: x - 24 };
152
215
  }
@@ -1,12 +1,14 @@
1
- /** A string's width in the embedded font at `size` px. */
2
- export declare function textWidth(s: string, size: number): number;
1
+ import { type FontWeight } from "./font.ts";
2
+ /** A string's width in the embedded font at `size` px and `weight` (Regular by default). */
3
+ export declare function textWidth(s: string, size: number, weight?: FontWeight): number;
3
4
  /** `s` if it fits in `max` px, else as much of it as fits followed by an ellipsis. */
4
- export declare function cut(s: string, size: number, max: number): string;
5
+ export declare function cut(s: string, size: number, max: number, weight?: FontWeight): string;
5
6
  /** One `<text>` element. */
6
- export declare function text(x: number, y: number, s: string, { size, fill, anchor }: {
7
+ export declare function text(x: number, y: number, s: string, { size, fill, anchor, weight }: {
7
8
  size: number;
8
9
  fill: string;
9
10
  anchor?: "end" | "middle";
11
+ weight?: FontWeight;
10
12
  }): string;
11
13
  export declare const escape: (s: string) => string;
12
14
  /** A coordinate to at most two decimals, so output doesn't depend on float noise. */
@@ -1,23 +1,25 @@
1
- import { ADVANCES, UNITS_PER_EM } from "./font.js";
2
- /** A string's width in the embedded font at `size` px. */
3
- export function textWidth(s, size) {
1
+ import { FONTS, UNITS_PER_EM } from "./font.js";
2
+ const ADVANCES = new Map(FONTS.map((f) => [f.weight, f.advances]));
3
+ /** A string's width in the embedded font at `size` px and `weight` (Regular by default). */
4
+ export function textWidth(s, size, weight = 400) {
5
+ const advances = ADVANCES.get(weight);
4
6
  let units = 0;
5
7
  for (const ch of s)
6
- units += ADVANCES[ch.codePointAt(0)] ?? ADVANCES[77];
8
+ units += advances[ch.codePointAt(0)] ?? advances[77];
7
9
  return (units * size) / UNITS_PER_EM;
8
10
  }
9
11
  /** `s` if it fits in `max` px, else as much of it as fits followed by an ellipsis. */
10
- export function cut(s, size, max) {
11
- if (textWidth(s, size) <= max)
12
+ export function cut(s, size, max, weight = 400) {
13
+ if (textWidth(s, size, weight) <= max)
12
14
  return s;
13
15
  const chars = [...s];
14
- while (chars.length > 0 && textWidth(chars.join("").trimEnd() + "…", size) > max)
16
+ while (chars.length > 0 && textWidth(chars.join("").trimEnd() + "…", size, weight) > max)
15
17
  chars.pop();
16
18
  return chars.join("").trimEnd() + "…";
17
19
  }
18
20
  /** One `<text>` element. */
19
- export function text(x, y, s, { size, fill, anchor }) {
20
- return `<text x="${fmt(x)}" y="${fmt(y)}" font-size="${size}" fill="${fill}"${anchor ? ` text-anchor="${anchor}"` : ""}>${escape(s)}</text>`;
21
+ export function text(x, y, s, { size, fill, anchor, weight = 400 }) {
22
+ return `<text x="${fmt(x)}" y="${fmt(y)}" font-size="${size}"${weight === 400 ? "" : ` font-weight="${weight}"`} fill="${fill}"${anchor ? ` text-anchor="${anchor}"` : ""}>${escape(s)}</text>`;
21
23
  }
22
24
  export const escape = (s) => s.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;");
23
25
  /** A coordinate to at most two decimals, so output doesn't depend on float noise. */
@@ -0,0 +1,53 @@
1
+ /**
2
+ * The diagram palette: the Wayfinding Plate tokens (DESIGN.md) that a drawn
3
+ * View uses, in light and in "Night shift" dark. This is the one source for
4
+ * `renderSvg` and `renderSequenceSvg`; the viewer's CSS custom properties of
5
+ * the same names in `viewer/src/styles.css` are checked against it by
6
+ * `test/render/tokens.test.ts`, so the export and the viewer can't drift.
7
+ */
8
+ export declare const themes: readonly ["light", "dark"];
9
+ export type Theme = (typeof themes)[number];
10
+ /** Each token by its CSS custom property name, without the leading `--`. */
11
+ export interface Palette {
12
+ /** The canvas: the pale stone ground the map sits on. */
13
+ ground: string;
14
+ /** Nodes, headers, the key. */
15
+ paper: string;
16
+ /** Station Slate: text and every 1.5px frame. */
17
+ ink: string;
18
+ muted: string;
19
+ faint: string;
20
+ /** Quiet frames: Deployment Targets, open boxes and stepped-back nodes. */
21
+ line: string;
22
+ /** Hairlines and broker stripes. */
23
+ rule: string;
24
+ /** A collapsed System's fill, activation bars. */
25
+ tint: string;
26
+ /** Wayfinding Yellow: the lens origin ring, and nothing decorative. */
27
+ here: string;
28
+ /** Text on the yellow. */
29
+ "on-here": string;
30
+ herebg: string;
31
+ "code-bg": string;
32
+ "code-fg": string;
33
+ /** Upstream Rust and Downstream Blue: the Impact lens sides. */
34
+ up: string;
35
+ upbg: string;
36
+ down: string;
37
+ downbg: string;
38
+ /** Path Violet: a path lens. */
39
+ path: string;
40
+ pathbg: string;
41
+ edge: string;
42
+ /** A stepped-back edge: solid, fainter, never faded or dashed. */
43
+ "edge-faint": string;
44
+ /** An open box's translucent tint, as the viewer's CSS writes it. */
45
+ container: string;
46
+ }
47
+ export declare const PALETTES: Record<Theme, Palette>;
48
+ /**
49
+ * `rgba(r, g, b, a)` painted over a solid hex colour, as one solid hex. The
50
+ * export flattens the open-box tint onto the ground this way, so no element
51
+ * in it carries an opacity below 1.
52
+ */
53
+ export declare function flatten(rgba: string, under: string): string;
@@ -0,0 +1,71 @@
1
+ /**
2
+ * The diagram palette: the Wayfinding Plate tokens (DESIGN.md) that a drawn
3
+ * View uses, in light and in "Night shift" dark. This is the one source for
4
+ * `renderSvg` and `renderSequenceSvg`; the viewer's CSS custom properties of
5
+ * the same names in `viewer/src/styles.css` are checked against it by
6
+ * `test/render/tokens.test.ts`, so the export and the viewer can't drift.
7
+ */
8
+ export const themes = ["light", "dark"];
9
+ export const PALETTES = {
10
+ light: {
11
+ ground: "#f3f4f1",
12
+ paper: "#ffffff",
13
+ ink: "#1c2633",
14
+ muted: "#4f5966",
15
+ faint: "#616a76",
16
+ line: "#c4c9c1",
17
+ rule: "#e2e4df",
18
+ tint: "#e8eae5",
19
+ here: "#ffc629",
20
+ "on-here": "#1c2633",
21
+ herebg: "#fff4cc",
22
+ "code-bg": "#1c2633",
23
+ "code-fg": "#ffffff",
24
+ up: "#a4461a",
25
+ upbg: "#fbeee6",
26
+ down: "#1d5fa8",
27
+ downbg: "#e8f0fa",
28
+ path: "#5b3aa8",
29
+ pathbg: "#efeaf9",
30
+ edge: "#5b6573",
31
+ "edge-faint": "#80878f",
32
+ container: "rgba(232, 234, 229, 0.6)",
33
+ },
34
+ dark: {
35
+ ground: "#10151c",
36
+ paper: "#18202a",
37
+ ink: "#e6eaef",
38
+ muted: "#a9b2bd",
39
+ faint: "#8f99a5",
40
+ line: "#36414f",
41
+ rule: "#252e3a",
42
+ tint: "#232c38",
43
+ here: "#ffc629",
44
+ "on-here": "#10151c",
45
+ herebg: "#3a3216",
46
+ "code-bg": "#e6eaef",
47
+ "code-fg": "#10151c",
48
+ up: "#f0925f",
49
+ upbg: "#3a2419",
50
+ down: "#78aef5",
51
+ downbg: "#172a45",
52
+ path: "#b79cff",
53
+ pathbg: "#2a2142",
54
+ edge: "#8f99a5",
55
+ "edge-faint": "#646d78",
56
+ container: "rgba(35, 44, 56, 0.55)",
57
+ },
58
+ };
59
+ /**
60
+ * `rgba(r, g, b, a)` painted over a solid hex colour, as one solid hex. The
61
+ * export flattens the open-box tint onto the ground this way, so no element
62
+ * in it carries an opacity below 1.
63
+ */
64
+ export function flatten(rgba, under) {
65
+ const m = /^rgba\((\d+),\s*(\d+),\s*(\d+),\s*([\d.]+)\)$/.exec(rgba);
66
+ if (!m)
67
+ return rgba;
68
+ const alpha = Number(m[4]);
69
+ const base = [1, 3, 5].map((i) => parseInt(under.slice(i, i + 2), 16));
70
+ return `#${[m[1], m[2], m[3]].map((c, i) => Math.round(Number(c) * alpha + base[i] * (1 - alpha)).toString(16).padStart(2, "0")).join("")}`;
71
+ }