@diagrammo/dgmo 0.21.1 → 0.22.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 (73) hide show
  1. package/README.md +16 -6
  2. package/dist/advanced.cjs +2003 -466
  3. package/dist/advanced.d.cts +5714 -0
  4. package/dist/advanced.d.ts +5714 -0
  5. package/dist/advanced.js +1999 -466
  6. package/dist/auto.cjs +2048 -449
  7. package/dist/auto.d.cts +39 -0
  8. package/dist/auto.d.ts +39 -0
  9. package/dist/auto.js +121 -121
  10. package/dist/auto.mjs +2050 -450
  11. package/dist/cli.cjs +170 -170
  12. package/dist/editor.cjs +13 -16
  13. package/dist/editor.js +13 -16
  14. package/dist/highlight.cjs +15 -13
  15. package/dist/highlight.js +15 -13
  16. package/dist/index.cjs +2032 -435
  17. package/dist/index.d.cts +339 -0
  18. package/dist/index.d.ts +339 -0
  19. package/dist/index.js +2034 -436
  20. package/dist/internal.cjs +2003 -466
  21. package/dist/internal.d.cts +5714 -0
  22. package/dist/internal.d.ts +5714 -0
  23. package/dist/internal.js +1999 -466
  24. package/dist/map-data/water-bodies.json +1 -0
  25. package/docs/language-reference.md +20 -9
  26. package/gallery/fixtures/map-categorical-world.dgmo +16 -0
  27. package/gallery/fixtures/map-categorical.dgmo +0 -1
  28. package/gallery/fixtures/map-choropleth.dgmo +0 -1
  29. package/gallery/fixtures/map-coastline.dgmo +7 -0
  30. package/gallery/fixtures/map-colorize.dgmo +11 -0
  31. package/gallery/fixtures/map-direct-color.dgmo +0 -1
  32. package/gallery/fixtures/map-reference-world.dgmo +11 -0
  33. package/gallery/fixtures/map-region-scope.dgmo +0 -3
  34. package/gallery/fixtures/map-route.dgmo +0 -1
  35. package/package.json +1 -1
  36. package/src/advanced.ts +12 -1
  37. package/src/boxes-and-lines/renderer.ts +39 -12
  38. package/src/cli.ts +1 -1
  39. package/src/completion.ts +32 -25
  40. package/src/cycle/renderer.ts +14 -1
  41. package/src/d3.ts +8 -2
  42. package/src/editor/highlight-api.ts +4 -0
  43. package/src/editor/keywords.ts +13 -16
  44. package/src/infra/renderer.ts +35 -7
  45. package/src/map/colorize.ts +54 -0
  46. package/src/map/context-labels.ts +429 -0
  47. package/src/map/data/types.ts +34 -0
  48. package/src/map/data/water-bodies.json +1 -0
  49. package/src/map/dimensions.ts +117 -0
  50. package/src/map/geo-query.ts +21 -3
  51. package/src/map/geo.ts +47 -1
  52. package/src/map/layout.ts +1300 -251
  53. package/src/map/load-data.ts +10 -2
  54. package/src/map/parser.ts +42 -116
  55. package/src/map/renderer.ts +512 -13
  56. package/src/map/resolved-types.ts +16 -2
  57. package/src/map/resolver.ts +208 -59
  58. package/src/map/types.ts +30 -32
  59. package/src/mindmap/renderer.ts +10 -1
  60. package/src/palettes/atlas.ts +77 -0
  61. package/src/palettes/blueprint.ts +73 -0
  62. package/src/palettes/color-utils.ts +58 -1
  63. package/src/palettes/index.ts +12 -3
  64. package/src/palettes/slate.ts +73 -0
  65. package/src/palettes/tidewater.ts +73 -0
  66. package/src/render.ts +8 -1
  67. package/src/tech-radar/renderer.ts +3 -0
  68. package/src/tech-radar/types.ts +3 -0
  69. package/src/utils/d3-types.ts +5 -0
  70. package/src/utils/legend-layout.ts +21 -4
  71. package/src/utils/legend-types.ts +7 -0
  72. package/src/utils/reserved-key-registry.ts +3 -0
  73. package/src/palettes/bold.ts +0 -67
@@ -0,0 +1,54 @@
1
+ // Political fill-assignment pass (§24B colorize). PURE + DETERMINISTIC — no
2
+ // projection, no palette, no DOM. Given the per-topology arc-adjacency graph
3
+ // (from geo.ts buildAdjacency), assign every drawn region a colour INDEX such
4
+ // that no two arc-neighbours share one. The only job of a political fill is
5
+ // boundary disambiguation; "no two neighbours share a hue" is the minimal
6
+ // property — and a planar political map famously needs only a handful of colours.
7
+ //
8
+ // FIRST-FIT greedy: each region takes the LOWEST index not used by an
9
+ // already-coloured neighbour. This is collision-free by construction (a node with
10
+ // k coloured neighbours has at most k forbidden indices, so index ≤ k is always
11
+ // free) AND clusters regions into the FEWEST colours — on the shipped graphs the
12
+ // max index used is 5 (world) / 4 (us-states), i.e. 5–6 colours total. The caller
13
+ // generates exactly that many palette tints, so the fills stay on-palette (no
14
+ // need for the old Δ+1 ≈ 17 wheel hues).
15
+
16
+ /** Result of {@link assignColors}: a colour INDEX per ISO + the number of
17
+ * distinct colours actually used (`= maxIndex + 1`). The index is a stable
18
+ * function of (ISO, global arc-adjacency) — extent-independent (AC10). */
19
+ export interface ColorAssignment {
20
+ readonly byIso: Map<string, number>;
21
+ readonly huesNeeded: number;
22
+ }
23
+
24
+ /** First-fit greedy graph-coloring over `isos` using `adjacency` (ISO → neighbour
25
+ * ISOs). Visits ISOs in stable ascending order; each takes the lowest index not
26
+ * taken by an already-coloured neighbour — collision-free, and minimises the
27
+ * total colour count.
28
+ *
29
+ * EVERY iso in `isos` is assigned an index — including zero-degree nodes
30
+ * (islands, DC, territories with no neighbour entry) — so the caller never needs
31
+ * a fallback fill (F14). */
32
+ export function assignColors(
33
+ isos: readonly string[],
34
+ adjacency: ReadonlyMap<string, readonly string[]>
35
+ ): ColorAssignment {
36
+ const sorted = [...isos].sort();
37
+ const byIso = new Map<string, number>();
38
+ let maxIndex = -1;
39
+
40
+ for (const iso of sorted) {
41
+ const taken = new Set<number>();
42
+ for (const n of adjacency.get(iso) ?? []) {
43
+ const c = byIso.get(n);
44
+ if (c !== undefined) taken.add(c);
45
+ }
46
+ // Lowest index not taken by a neighbour (always exists: ≤ neighbour count).
47
+ let h = 0;
48
+ while (taken.has(h)) h++;
49
+ byIso.set(iso, h);
50
+ if (h > maxIndex) maxIndex = h;
51
+ }
52
+
53
+ return { byIso, huesNeeded: maxIndex + 1 };
54
+ }
@@ -0,0 +1,429 @@
1
+ // Context-label placement (step 4, part of layout). Produces a sparse,
2
+ // density-thinned ORIENTATION layer — water-body names + unreferenced notable
3
+ // country names — distinct from `region-labels`. PURE + SYNC + DETERMINISTIC.
4
+ //
5
+ // Design (tech-spec §map-context-labels): a deliberately LOW per-view label
6
+ // BUDGET is the primary noise lever; a span-derived TIER BAND orders candidates
7
+ // into it; each candidate is committed only if it survives COLLISION against
8
+ // every already-placed data/region/POI/route label (the `collides` closure) AND
9
+ // the other context labels. Context labels place DEAD LAST and never displace
10
+ // data — they only fill leftover space, degrading gracefully to zero. See
11
+ // Decisions 6 (budget), 7 (dead-last), 8 (viewport/projection guards).
12
+ import { mix } from '../palettes/color-utils';
13
+ import type { PaletteColors } from '../palettes/types';
14
+ import type { LabelRect } from '../label-layout';
15
+ import { measureLegendText } from '../utils/legend-constants';
16
+ import type { ProjectionFamily } from './resolved-types';
17
+ import type { WaterBodies, WaterKind } from './data/types';
18
+ import type { PlacedLabel } from './layout';
19
+
20
+ /** A view span band → priority ordering (NOT a hard zoom cutoff, Decision 6). */
21
+ export type TierBand = 'world' | 'continental' | 'regional' | 'local';
22
+
23
+ /** An unreferenced country, pre-projected by layout (geo work stays in layout;
24
+ * area-rank + name-fit + collision live here so the module is unit-testable). */
25
+ export interface CountryCandidate {
26
+ readonly name: string;
27
+ /** Projected screen bbox `[x0, y0, x1, y1]` (from `path.bounds`). */
28
+ readonly bbox: readonly [number, number, number, number];
29
+ /** Projected screen anchor `[x, y]` (mainland anchor or `path.centroid`), or
30
+ * null when the feature doesn't project to a finite point. */
31
+ readonly anchor: readonly [number, number] | null;
32
+ }
33
+
34
+ export interface ContextLabelArgs {
35
+ readonly projection: ProjectionFamily;
36
+ readonly dLonSpan: number;
37
+ readonly dLatSpan: number;
38
+ readonly width: number;
39
+ readonly height: number;
40
+ readonly waterBodies?: WaterBodies | undefined;
41
+ readonly countries: readonly CountryCandidate[];
42
+ readonly palette: PaletteColors;
43
+ readonly project: (lon: number, lat: number) => [number, number] | null;
44
+ /** Collision test against every committed data/region/POI/route obstacle. */
45
+ readonly collides: (rect: LabelRect) => boolean;
46
+ /** True when the screen point sits over LAND (a country/state fill) rather than
47
+ * open water. WATER labels are rejected when their footprint touches land — an
48
+ * ocean name belongs over the ocean (they're optional orientation aids, so drop
49
+ * rather than misplace). Country labels are exempt (they label land). Optional
50
+ * for unit tests; absent ⇒ no land rejection. */
51
+ readonly overLand?: (x: number, y: number) => boolean;
52
+ }
53
+
54
+ const FONT = 11; // matches layout's on-map label font
55
+ const LINE_HEIGHT = FONT + 2; // px per wrapped line — MUST match the renderer's
56
+ const PADX = 4; // half-padding around a context label rect
57
+ const PADY = 3;
58
+ const WATER_LETTER_SPACING = 1.5; // px — cartographic spread for water names
59
+ const CONTEXT_PAD = 4; // extra gap enforced between two context labels
60
+ const EDGE_CLAMP_MARGIN = 8; // px inset for edge-clamped ocean labels
61
+ const EDGE_CLAMP_OVERSHOOT = 0.35; // max off-frame overshoot (× dim) to still clamp
62
+
63
+ // Water-kind priority within a tier (oceans first, then seas, then the rest) so
64
+ // a thin budget always spends on the highest-orientation-value names.
65
+ const KIND_ORDER: Record<WaterKind, number> = {
66
+ ocean: 0,
67
+ sea: 1,
68
+ gulf: 2,
69
+ bay: 3,
70
+ strait: 4,
71
+ channel: 5,
72
+ sound: 6,
73
+ };
74
+
75
+ /** Span band from the larger of the two view spans (Decision 6 — priority, not
76
+ * a hard gate). */
77
+ export function tierBand(maxSpanDeg: number): TierBand {
78
+ if (maxSpanDeg >= 90) return 'world';
79
+ if (maxSpanDeg >= 20) return 'continental';
80
+ if (maxSpanDeg >= 5) return 'regional';
81
+ return 'local';
82
+ }
83
+
84
+ /** Deliberately-LOW combined label budget = f(canvas area, band). Floors to ~1
85
+ * on a thumbnail and 0 on a tiny canvas (Decision 6, ADR-3; AC9). Caps the
86
+ * TOTAL context labels (water + country), so `relief`/data don't get extra
87
+ * headroom (Decision 13). */
88
+ export function labelBudget(
89
+ width: number,
90
+ height: number,
91
+ band: TierBand
92
+ ): number {
93
+ const bandCap: Record<TierBand, number> = {
94
+ world: 6,
95
+ continental: 5,
96
+ regional: 4,
97
+ local: 3,
98
+ };
99
+ const area = Math.floor(Math.sqrt(Math.max(0, width * height)) / 150);
100
+ return Math.max(0, Math.min(area, bandCap[band]));
101
+ }
102
+
103
+ /** Which water tiers/kinds are eligible at a band. World view is oceans + major
104
+ * seas ONLY (never bays/sounds/minor gulfs, AC3); broader views progressively
105
+ * admit smaller features by `scalerank` (AC4). */
106
+ function waterEligible(tier: number, kind: WaterKind, band: TierBand): boolean {
107
+ switch (band) {
108
+ case 'world':
109
+ return tier <= 1 && (kind === 'ocean' || kind === 'sea');
110
+ case 'continental':
111
+ return tier <= 2;
112
+ case 'regional':
113
+ return tier <= 3;
114
+ case 'local':
115
+ return tier <= 4;
116
+ }
117
+ }
118
+
119
+ function insideViewport(
120
+ p: readonly [number, number] | null,
121
+ width: number,
122
+ height: number
123
+ ): p is [number, number] {
124
+ return (
125
+ !!p &&
126
+ Number.isFinite(p[0]) &&
127
+ Number.isFinite(p[1]) &&
128
+ p[0] >= 0 &&
129
+ p[0] <= width &&
130
+ p[1] >= 0 &&
131
+ p[1] <= height
132
+ );
133
+ }
134
+
135
+ /** Rendered label width INCLUDING letter-spacing — `measureLegendText` ignores
136
+ * the per-gap `letter-spacing` the renderer applies to water names, so without
137
+ * this the fit/clamp math under-measures by ~`(len-1)*spacing` and the label
138
+ * clips at the canvas edge. */
139
+ export function labelWidth(text: string, letterSpacing: number): number {
140
+ const spacing =
141
+ letterSpacing > 0 ? Math.max(0, text.length - 1) * letterSpacing : 0;
142
+ return measureLegendText(text, FONT) + spacing + 2 * PADX;
143
+ }
144
+
145
+ /** Wrap a multi-word name into balanced lines, biased to wrap READILY — water
146
+ * names ("North Pacific Ocean") read better stacked, and a narrower footprint
147
+ * is far likelier to clear surrounding land (the hard rule below). Of every
148
+ * contiguous split into ≤`maxLines`, picks the one minimising the widest line
149
+ * (so it shrinks horizontally whenever a break helps); ties break to fewer
150
+ * lines, then top-heavy ("North Pacific" / "Ocean", not "North" / "Pacific
151
+ * Ocean"). Single-word names pass through unwrapped. */
152
+ export function wrapLabel(text: string, letterSpacing: number): string[] {
153
+ const words = text.split(/\s+/).filter(Boolean);
154
+ if (words.length <= 1) return [text];
155
+ const maxLines = words.length >= 4 ? 3 : 2;
156
+ const n = words.length;
157
+ type Split = { lines: string[]; cost: number; head: number };
158
+ let best: Split | null = null;
159
+ for (let mask = 0; mask < 1 << (n - 1); mask++) {
160
+ const lines: string[] = [];
161
+ let cur = [words[0]!];
162
+ for (let i = 1; i < n; i++) {
163
+ if (mask & (1 << (i - 1))) {
164
+ lines.push(cur.join(' '));
165
+ cur = [words[i]!];
166
+ } else cur.push(words[i]!);
167
+ }
168
+ lines.push(cur.join(' '));
169
+ if (lines.length > maxLines) continue;
170
+ const cost = Math.round(
171
+ Math.max(...lines.map((l) => labelWidth(l, letterSpacing)))
172
+ );
173
+ const head = labelWidth(lines[0]!, letterSpacing);
174
+ if (
175
+ !best ||
176
+ cost < best.cost ||
177
+ (cost === best.cost && lines.length < best.lines.length) ||
178
+ (cost === best.cost &&
179
+ lines.length === best.lines.length &&
180
+ head > best.head)
181
+ )
182
+ best = { lines, cost, head };
183
+ }
184
+ return best?.lines ?? [text];
185
+ }
186
+
187
+ function rectAround(
188
+ cx: number,
189
+ cy: number,
190
+ lines: readonly string[],
191
+ letterSpacing: number
192
+ ): LabelRect {
193
+ const w = Math.max(...lines.map((l) => labelWidth(l, letterSpacing)));
194
+ const h = (lines.length - 1) * LINE_HEIGHT + FONT + 2 * PADY;
195
+ return { x: cx - w / 2, y: cy - h / 2, w, h };
196
+ }
197
+
198
+ function rectFits(r: LabelRect, width: number, height: number): boolean {
199
+ return r.x >= 0 && r.y >= 0 && r.x + r.w <= width && r.y + r.h <= height;
200
+ }
201
+
202
+ function overlapsPadded(a: LabelRect, b: LabelRect, pad: number): boolean {
203
+ return (
204
+ a.x - pad < b.x + b.w &&
205
+ a.x + a.w + pad > b.x &&
206
+ a.y - pad < b.y + b.h &&
207
+ a.y + a.h + pad > b.y
208
+ );
209
+ }
210
+
211
+ /** Place the orientation backdrop. Returns committed labels in priority order;
212
+ * caller pushes them onto `labels` LAST so they never displace data. */
213
+ export function placeContextLabels(args: ContextLabelArgs): PlacedLabel[] {
214
+ const {
215
+ projection,
216
+ dLonSpan,
217
+ dLatSpan,
218
+ width,
219
+ height,
220
+ waterBodies,
221
+ countries,
222
+ palette,
223
+ project,
224
+ collides,
225
+ overLand,
226
+ } = args;
227
+
228
+ // albers-usa is supported: the CONUS conic projects CONUS-area water (Gulf of
229
+ // America, the Pacific/Atlantic margins) correctly, and the viewport-visibility
230
+ // check below drops the off-frame anchors (Gulf of Alaska, mid-Pacific) that the
231
+ // AK/HI inset relocation would otherwise mislabel. The caller additionally feeds
232
+ // the AK/HI inset frames into `collides` so a label never lands on an inset box.
233
+ // (Supersedes the original blanket albers-usa disable — the US map is the
234
+ // flagship orientation case.)
235
+ void projection;
236
+
237
+ const band = tierBand(Math.max(dLonSpan, dLatSpan));
238
+ const budget = labelBudget(width, height, band);
239
+ if (budget <= 0) return [];
240
+
241
+ // Subordinate cartographic colours (palette-derived, no hex; resvg-safe via
242
+ // pre-computed mix()). Water = muted blue-gray italic; country = muted gray.
243
+ const waterColor = mix(palette.colors.blue, palette.textMuted, 50);
244
+ const countryColor = palette.textMuted;
245
+ const haloColor = palette.bg;
246
+
247
+ type Candidate = {
248
+ text: string;
249
+ lines: string[];
250
+ cx: number;
251
+ cy: number;
252
+ italic: boolean;
253
+ letterSpacing: number;
254
+ color: string;
255
+ sort: number; // priority key (lower first)
256
+ };
257
+ const candidates: Candidate[] = [];
258
+
259
+ // -- Water candidates (priority core: oceans → seas → minor water) --
260
+ const center: [number, number] = [width / 2, height / 2];
261
+ for (const e of waterBodies?.entries ?? []) {
262
+ const [lat, lon, name, tier, kind, alt] = e;
263
+ if (!waterEligible(tier, kind, band)) continue;
264
+ // Wrap eagerly (Decision: water names stack readily) so the clamp/fit math
265
+ // below sees the real, narrower wrapped footprint, not the one-line width.
266
+ const wlines = wrapLabel(name, WATER_LETTER_SPACING);
267
+ // Multi-anchor (Decision 5 / ADR-4): of the anchors that project inside the
268
+ // viewport, pick the one nearest the viewport centre.
269
+ const anchorsLngLat: Array<[number, number]> = [[lon, lat]];
270
+ for (const a of alt ?? []) anchorsLngLat.push([a[1], a[0]]);
271
+ let best: [number, number] | null = null;
272
+ let bestD = Infinity;
273
+ let nearestProj: [number, number] | null = null; // best finite proj (any side)
274
+ let nearestProjD = Infinity;
275
+ for (const [aLon, aLat] of anchorsLngLat) {
276
+ const p = project(aLon, aLat);
277
+ if (!p || !Number.isFinite(p[0]) || !Number.isFinite(p[1])) continue;
278
+ const d = (p[0] - center[0]) ** 2 + (p[1] - center[1]) ** 2;
279
+ if (d < nearestProjD) {
280
+ nearestProjD = d;
281
+ nearestProj = p;
282
+ }
283
+ if (!insideViewport(p, width, height)) continue;
284
+ if (d < bestD) {
285
+ bestD = d;
286
+ best = p;
287
+ }
288
+ }
289
+ // Oceans (tier 0) are large enough that a frame-edge label still reads
290
+ // correctly, so when their anchor falls off-screen on a zoomed-in view
291
+ // (e.g. the mid-Atlantic/Pacific centroid on a US map) we CLAMP it to the
292
+ // viewport margin rather than drop it — the standard cartographic "ocean
293
+ // name hugs the edge" behaviour. Smaller basins keep the strict drop-if-
294
+ // off-screen rule (AC10) to avoid mislabelling an adjacent basin.
295
+ if (!best && tier === 0 && nearestProj) {
296
+ // Only clamp an ocean ADJACENT to the frame: if its centroid overshoots
297
+ // the viewport by more than ~half a dimension it's a distant ocean (e.g.
298
+ // the South Atlantic / Arctic relative to a US view) and edge-clamping it
299
+ // would mislabel that margin — drop instead. The surviving oceans are the
300
+ // ones the frame actually borders (Pacific to the west, Atlantic east).
301
+ const overX = Math.max(0, -nearestProj[0], nearestProj[0] - width);
302
+ const overY = Math.max(0, -nearestProj[1], nearestProj[1] - height);
303
+ if (
304
+ overX <= width * EDGE_CLAMP_OVERSHOOT &&
305
+ overY <= height * EDGE_CLAMP_OVERSHOOT
306
+ ) {
307
+ // Clamp the CENTRE inward by half the label so the whole (centre-
308
+ // anchored) rect stays on-canvas — clamping to the bare margin would
309
+ // overflow a wide name like "North Atlantic Ocean" off the edge.
310
+ // letter-spacing IS counted (labelWidth) so the clamp matches render.
311
+ const halfW =
312
+ Math.max(...wlines.map((l) => labelWidth(l, WATER_LETTER_SPACING))) /
313
+ 2;
314
+ const halfH = ((wlines.length - 1) * LINE_HEIGHT + FONT + 2 * PADY) / 2;
315
+ const m = EDGE_CLAMP_MARGIN;
316
+ best = [
317
+ Math.min(Math.max(nearestProj[0], halfW + m), width - halfW - m),
318
+ Math.min(Math.max(nearestProj[1], halfH + m), height - halfH - m),
319
+ ];
320
+ }
321
+ }
322
+ if (!best) continue;
323
+ candidates.push({
324
+ text: name,
325
+ lines: wlines,
326
+ cx: best[0],
327
+ cy: best[1],
328
+ italic: true,
329
+ letterSpacing: WATER_LETTER_SPACING,
330
+ color: waterColor,
331
+ // Water before any country (×1000), then by tier, then kind, then name.
332
+ sort: tier * 10 + KIND_ORDER[kind],
333
+ });
334
+ }
335
+
336
+ // -- Country candidates (unreferenced; biggest projected area first) --
337
+ // Rank by screen bbox area; keep only those whose name fits the footprint
338
+ // (width-fit, like region-labels) and whose anchor projects inside the view.
339
+ const ranked = countries
340
+ .map((c) => {
341
+ const [x0, y0, x1, y1] = c.bbox;
342
+ const w = x1 - x0;
343
+ const h = y1 - y0;
344
+ return { c, w, h, area: w * h };
345
+ })
346
+ .filter((r) => Number.isFinite(r.area) && r.area > 0)
347
+ .sort((a, b) => b.area - a.area);
348
+ let ci = 0;
349
+ for (const r of ranked) {
350
+ const { c, w, h } = r;
351
+ // F2: an antimeridian-crossing / global-smear country yields a near-full-
352
+ // canvas bbox while its real landmass is split — the `path.centroid` anchor
353
+ // is then unreliable (mid-map, wrong basin). Drop such over-wide candidates
354
+ // rather than spend a top-priority slot on a mispositioned name.
355
+ if (w > width * 0.66 || h > height * 0.66) continue;
356
+ if (!insideViewport(c.anchor, width, height)) continue;
357
+ // Always the full country name — never an ISO abbreviation. If the name
358
+ // doesn't fit the footprint, drop the label rather than abbreviate.
359
+ const text = c.name;
360
+ const tw = labelWidth(text, 0);
361
+ // Approximate fit (Decision 4): name fits inside the footprint bbox. NOT
362
+ // true point-in-polygon — cartographic labels routinely overrun coastlines.
363
+ if (tw > w || FONT + 2 * PADY > h) continue;
364
+ candidates.push({
365
+ text,
366
+ lines: [text],
367
+ cx: c.anchor[0],
368
+ cy: c.anchor[1],
369
+ italic: false,
370
+ letterSpacing: 0,
371
+ color: countryColor,
372
+ // Always after every water body (+1e6); larger area = earlier.
373
+ sort: 1_000_000 + ci++,
374
+ });
375
+ }
376
+
377
+ // -- Commit dead-last, highest-priority-first, into leftover space only --
378
+ candidates.sort((a, b) => a.sort - b.sort);
379
+ const placed: PlacedLabel[] = [];
380
+ const placedRects: LabelRect[] = [];
381
+ for (const cand of candidates) {
382
+ if (placed.length >= budget) break;
383
+ const rect = rectAround(cand.cx, cand.cy, cand.lines, cand.letterSpacing);
384
+ if (!rectFits(rect, width, height)) continue;
385
+ // Water labels must sit over OPEN WATER and NEVER touch land — sample a grid
386
+ // over every wrapped line (each line's own horizontal extent at five points);
387
+ // drop the whole label if ANY sample hits land (Decision: optional orientation
388
+ // aids, so exclude rather than misplace over a coastline). Country labels are
389
+ // exempt — they belong on their country.
390
+ if (cand.italic && overLand) {
391
+ const inset = 2;
392
+ const top = cand.cy - ((cand.lines.length - 1) / 2) * LINE_HEIGHT;
393
+ const touchesLand = cand.lines.some((line, li) => {
394
+ const lw = labelWidth(line, cand.letterSpacing);
395
+ const x0 = cand.cx - lw / 2 + inset;
396
+ const x1 = cand.cx + lw / 2 - inset;
397
+ const xs = [x0, (x0 + cand.cx) / 2, cand.cx, (cand.cx + x1) / 2, x1];
398
+ const base = top + li * LINE_HEIGHT;
399
+ // Sample the glyph body top→baseline (text rises above the baseline) so a
400
+ // label whose ascenders clip a coastline is rejected, not just one whose
401
+ // baseline sits on land.
402
+ return [base, base - FONT * 0.4, base - FONT * 0.8].some((y) =>
403
+ xs.some((x) => overLand(x, y))
404
+ );
405
+ });
406
+ if (touchesLand) continue;
407
+ }
408
+ if (collides(rect)) continue;
409
+ if (placedRects.some((r) => overlapsPadded(rect, r, CONTEXT_PAD))) continue;
410
+ placedRects.push(rect);
411
+ placed.push({
412
+ x: cand.cx,
413
+ y: cand.cy,
414
+ text: cand.text,
415
+ anchor: 'middle',
416
+ color: cand.color,
417
+ // No halo: the bg-coloured outline reads as a ghost box behind the text
418
+ // over the tinted water/land. Context labels are muted enough to sit
419
+ // cleanly on the basemap without one.
420
+ halo: false,
421
+ haloColor,
422
+ italic: cand.italic,
423
+ letterSpacing: cand.letterSpacing,
424
+ ...(cand.lines.length > 1 ? { lines: cand.lines } : {}),
425
+ lineNumber: 0,
426
+ });
427
+ }
428
+ return placed;
429
+ }
@@ -50,6 +50,40 @@ export interface Gazetteer {
50
50
  alt: Record<string, number>;
51
51
  }
52
52
 
53
+ /** Water-feature class (Natural Earth `featurecla`, rivers/reefs excluded). */
54
+ export type WaterKind =
55
+ | 'ocean'
56
+ | 'sea'
57
+ | 'gulf'
58
+ | 'bay'
59
+ | 'strait'
60
+ | 'channel'
61
+ | 'sound';
62
+
63
+ /**
64
+ * A water-body label entry: `[lat, lon, name, tier, kind, alt?]`.
65
+ * - `lat`/`lon` — label anchor (Natural Earth inner point), rounded to 3 decimals.
66
+ * - `name` — full display name (no abbreviation exists for water bodies).
67
+ * - `tier` — Natural Earth `scalerank` (0 = most prominent → orientation priority).
68
+ * - `kind` — feature class (drives styling/priority bucketing).
69
+ * - `alt` — optional extra anchor points `[lat, lon][]`; the layout picks the
70
+ * one nearest the viewport center (Decision 5 multi-anchor seam). Absent today.
71
+ */
72
+ export type WaterBodyEntry = [
73
+ lat: number,
74
+ lon: number,
75
+ name: string,
76
+ tier: number,
77
+ kind: WaterKind,
78
+ alt?: ReadonlyArray<readonly [number, number]>,
79
+ ];
80
+
81
+ export interface WaterBodies {
82
+ /** Deterministically ordered (tier, then name). Generated from Natural Earth
83
+ * marine polys by scripts/build-map-data.mjs into `water-bodies.json`. */
84
+ readonly entries: readonly WaterBodyEntry[];
85
+ }
86
+
53
87
  /** A fill-able region (country or US state) — the display name + its ISO id +
54
88
  * layer. Powers region-name autocomplete (completion-only; the renderer derives
55
89
  * names from the topology directly). Extracted from the topologies by
@@ -0,0 +1 @@
1
+ {"entries":[[85.078,21.558,"Arctic Ocean",0,"ocean"],[-26.746,83.424,"Indian Ocean",0,"ocean"],[39.898,-30.68,"North Atlantic Ocean",0,"ocean",[[40,-55],[30,-45],[50,-25],[15,-45]]],[24.49,-136.445,"North Pacific Ocean",0,"ocean",[[36,-126],[47,-131],[55,-143],[35,160],[15,165]]],[-34.311,-18.311,"South Atlantic Ocean",0,"ocean",[[-25,-35],[-40,-15],[-10,-20]]],[-30.137,-126.822,"South Pacific Ocean",0,"ocean",[[-20,-110],[-40,-95],[-15,170],[-35,170]]],[-65.892,-18.447,"Southern Ocean",0,"ocean"],[13.692,63.675,"Arabian Sea",1,"sea"],[72.951,-66.643,"Baffin Bay",1,"bay"],[13.118,86.757,"Bay of Bengal",1,"bay"],[71.997,-136.388,"Beaufort Sea",1,"sea"],[43.547,31.292,"Black Sea",1,"sea"],[13.754,-78.235,"Caribbean Sea",1,"sea"],[41.954,50.61,"Caspian Sea",1,"sea"],[-18.886,157.126,"Coral Sea",1,"sea"],[58.108,-149.198,"Gulf of Alaska",1,"gulf"],[25.319,-90.053,"Gulf of America",1,"gulf"],[59.246,-85.292,"Hudson Bay",1,"bay"],[55.979,-52.602,"Labrador Sea",1,"sea"],[34.341,17.988,"Mediterranean Sea",1,"sea"],[25.683,52.866,"Persian Gulf",1,"gulf"],[17.263,133.481,"Philippine Sea",1,"sea"],[19.579,38.751,"Red Sea",1,"sea"],[-77.493,-169.96,"Ross Sea",1,"sea"],[40.613,136.423,"Sea of Japan",1,"sea"],[52.851,149.205,"Sea of Okhotsk",1,"sea"],[13.781,114.703,"South China Sea",1,"sea"],[-40.358,160.682,"Tasman Sea",1,"sea"],[-75.66,-53.473,"Weddell Sea",1,"sea"],[-73.979,-106.379,"Amundsen Sea",2,"sea"],[10.878,95.492,"Andaman Sea",2,"sea"],[-9.347,135.278,"Arafura Sea",2,"sea"],[19.67,-93.559,"Bahía de Campeche",2,"bay"],[56.024,19.234,"Baltic Sea",2,"sea"],[-5.658,126.23,"Banda Sea",2,"sea"],[74.604,43.063,"Barents Sea",2,"sea"],[45.627,-4.33,"Bay of Biscay",2,"bay"],[-72.084,-82.745,"Bellingshausen Sea",2,"sea"],[56.349,-171.512,"Bering Sea",2,"sea"],[3.571,122.574,"Celebes Sea",2,"sea"],[68.633,-169.418,"Chukchi Sea",2,"sea"],[64.549,-57.848,"Davis Strait",2,"strait"],[-66.686,-68.017,"Drake Passage",2,"channel"],[28.854,125.37,"East China Sea",2,"sea"],[76.558,-8.516,"Greenland Sea",2,"sea"],[3.294,2.903,"Gulf of Guinea",2,"gulf"],[9.545,101.832,"Gulf of Thailand",2,"gulf"],[53.543,-5.35,"Irish Sea",2,"sea"],[-5.2,112.569,"Java Sea",2,"sea"],[76.92,73.103,"Kara Sea",2,"sea"],[5.871,75.234,"Laccadive Sea",2,"sea"],[75.961,120.379,"Laptev Sea",2,"sea"],[22.073,120.87,"Luzon Strait",2,"strait"],[-20.03,40.414,"Mozambique Channel",2,"channel"],[56.502,2.655,"North Sea",2,"sea"],[66.883,1.329,"Norwegian Sea",2,"sea"],[27.646,-59.716,"Sargasso Sea",2,"sea"],[-61.149,-54.893,"Scotia Sea",2,"sea"],[8.392,120.208,"Sulu Sea",2,"sea"],[-10.978,127.712,"Timor Sea",2,"sea"],[35.164,123.956,"Yellow Sea",2,"sea"],[42.81,15.327,"Adriatic Sea",3,"sea"],[49.707,-2.894,"English Channel",3,"channel"],[26.35,-110.53,"Golfo de California",3,"gulf"],[-35.41,131.682,"Great Australian Bight",3,"gulf"],[12.621,48.305,"Gulf of Aden",3,"gulf"],[61.904,19.763,"Gulf of Bothnia",3,"gulf"],[-14.387,139.215,"Gulf of Carpentaria",3,"gulf"],[16.3,-88.001,"Gulf of Honduras",3,"gulf"],[24.54,58.766,"Gulf of Oman",3,"gulf"],[63.3,-73.224,"Hudson Strait",3,"strait"],[53.746,-80.255,"James Bay",3,"bay"],[39.863,12.144,"Tyrrhenian Sea",3,"sea"],[65.418,38.854,"White Sea",3,"sea"],[38.899,24.994,"Aegean Sea",4,"sea"],[70.531,-120.687,"Amundsen Gulf",4,"gulf"],[40.132,1.619,"Balearic Sea",4,"sea"],[44.909,-66.051,"Bay of Fundy",4,"bay"],[-37.489,176.808,"Bay of Plenty",4,"bay"],[-3.776,147.903,"Bismarck Sea",4,"sea"],[38.756,120.053,"Bo Hai",4,"sea"],[57.651,-160.179,"Bristol Bay",4,"bay"],[51.086,-5.339,"Bristol Channel",4,"channel"],[-2.412,131.169,"Ceram Sea",4,"sea"],[37.829,-76.031,"Chesapeake Bay",4,"bay"],[59.657,-152.312,"Cook Inlet",4,"bay"],[42.981,3.976,"Golfe du Lion",4,"gulf"],[-46.117,-66.587,"Golfo San Jorge",4,"gulf"],[8.18,-79.211,"Golfo de Panamá",4,"gulf"],[59.969,26.987,"Gulf of Finland",4,"gulf"],[22.57,69.333,"Gulf of Kutch",4,"gulf"],[43.368,-68.504,"Gulf of Maine",4,"gulf"],[8.43,78.962,"Gulf of Mannar",4,"gulf"],[48.615,-60.525,"Gulf of Saint Lawrence",4,"gulf"],[19.791,107.611,"Gulf of Tonkin",4,"gulf"],[33.956,132.471,"Inner Sea",4,"sea"],[56.267,-7.03,"Inner Seas",4,"sea"],[38.178,18.626,"Ionian Sea",4,"sea"],[33.799,128.392,"Korea Strait",4,"strait"],[-1.86,118.021,"Makassar Strait",4,"strait"],[75.523,-61.147,"Melville Bay",4,"bay"],[0.591,126.128,"Molucca Sea",4,"sea"],[-35.469,-56.554,"Río de la Plata",4,"gulf"],[59.897,157.821,"Shelikhova Gulf",4,"gulf"],[-9.247,155.069,"Solomon Sea",4,"sea"],[35.783,-5.919,"Strait of Gibraltar",4,"strait"],[5.586,99.038,"Strait of Malacca",4,"strait"],[1.193,103.68,"Strait of Singapore",4,"strait"],[25.571,-79.276,"Straits of Florida",4,"strait"],[24.166,119.387,"Taiwan Strait",4,"strait"],[70.288,-99.272,"The North Western Passages",4,"channel"],[59.341,-67.41,"Ungava Bay",4,"bay"],[73.851,-109.102,"Viscount Melville Sound",4,"sound"]]}
@@ -0,0 +1,117 @@
1
+ // Content-aware export dimensions for maps (§ export-content-aspect).
2
+ //
3
+ // Outside the app — CLI, MCP, SSG embeds (remark/astro/docusaurus/fumadocs), and
4
+ // Obsidian — maps were rendered into a fixed 1200×800 canvas. A world map is
5
+ // ~2.3:1, so the global stretch-fill distorted it vertically to fill the too-tall
6
+ // box. These helpers derive the canvas HEIGHT from the map's intrinsic projected
7
+ // aspect so the export matches the content's natural shape.
8
+ //
9
+ // dgmo emits the intrinsic aspect; the host context decides display fit (Obsidian
10
+ // sets the embedded <svg> to width:100% + aspect-ratio from the viewBox). Aspect
11
+ // is the invariant; `baseWidth` is just a resolution knob.
12
+ import { geoPath } from 'd3-geo';
13
+ import { TITLE_FONT_SIZE, TITLE_Y } from '../utils/title-constants';
14
+ import { buildMapProjection } from './layout';
15
+ import type { ResolvedMap } from './resolved-types';
16
+ import type { MapData } from './resolved-types';
17
+
18
+ // Mirror the layout constants so the chrome reserve matches what the renderer
19
+ // actually reserves (layout.ts FIT_PAD / TITLE_GAP).
20
+ const FIT_PAD = 24;
21
+ const TITLE_GAP = 16;
22
+
23
+ // Clamp guardrails (w/h). The clamp is for PATHOLOGICAL extents, not the common
24
+ // case — world/continent/country must land at their true projected aspect.
25
+ // ASPECT_MAX = 3.0 → never wider/shorter than 3:1. The default world projection
26
+ // is EQUIRECTANGULAR (see resolver.ts ~L744); a full-world
27
+ // extent measures ~2.4:1 and a narrower-latitude world up to
28
+ // ~2.65:1 — all comfortably under 3.0, so any reasonable world
29
+ // renders at its true aspect (no letterbox). Only a genuinely
30
+ // extreme >3:1 band (e.g. a thin trans-global route) is clamped.
31
+ // ASPECT_MIN = 0.9 → never taller than ~1:1.1, so a tall country embedded at
32
+ // width:100% in a narrow note column stays sane.
33
+ const ASPECT_MAX = 3.0;
34
+ const ASPECT_MIN = 0.9;
35
+ // Minimum px of actual map area (below the chrome band) — keeps a short canvas
36
+ // (very wide extent) from being crowded out by the title/caption.
37
+ const MIN_MAP_BAND = 200;
38
+ // Defensive fallback when the content aspect is non-finite (NaN/0/Infinity). The
39
+ // resolver always pads the extent to a non-degenerate box, so in practice this is
40
+ // not reached via the public pipeline — it guards a degenerate `fitTarget` directly.
41
+ const FALLBACK_ASPECT = 1.5; // 3:2
42
+ // Square reference box for aspect measurement. Uniform `fitSize` scaling makes the
43
+ // measured aspect invariant to this value — it MUST be square (a non-square box
44
+ // would leak into the ratio).
45
+ const REF = 1000;
46
+
47
+ /** The map's intrinsic projected aspect (width / height) for a resolved map.
48
+ *
49
+ * Measured by fitting the projection + fit target (the SAME `buildMapProjection`
50
+ * output the renderer draws with) into a square reference box and reading the
51
+ * projected bounds of the fit target. `fitSize` scales uniformly, so the ratio is
52
+ * independent of the box size (see the reference-box invariance test).
53
+ *
54
+ * Returns {@link FALLBACK_ASPECT} (3:2) if the result is non-finite or ≤ 0 — the
55
+ * helper never emits a NaN/0/Infinity aspect. */
56
+ export function mapContentAspect(
57
+ resolved: ResolvedMap,
58
+ data: MapData,
59
+ /** Square reference box for the measurement. Uniform `fitSize` scaling makes the
60
+ * result invariant to this value; exposed only so tests can assert that. */
61
+ ref = REF
62
+ ): number {
63
+ const { projection, fitTarget } = buildMapProjection(resolved, data);
64
+ projection.fitSize([ref, ref], fitTarget as never);
65
+ const b = geoPath(projection).bounds(fitTarget as never);
66
+ const w = b[1][0] - b[0][0];
67
+ const h = b[1][1] - b[0][1];
68
+ const aspect = w / h;
69
+ return Number.isFinite(aspect) && aspect > 0 ? aspect : FALLBACK_ASPECT;
70
+ }
71
+
72
+ /** Content-aware export dimensions for a map: `width` fixed at `baseWidth`,
73
+ * `height` derived from the clamped intrinsic aspect, with a minimum-map-band
74
+ * floor for very wide extents. `preferContain` is true when the clamp or floor
75
+ * forced the canvas off the content aspect — the renderer then contain-fits
76
+ * (letterbox) instead of stretching, so the off-aspect canvas doesn't re-distort. */
77
+ export interface MapExportDimensions {
78
+ readonly width: number;
79
+ readonly height: number;
80
+ readonly preferContain: boolean;
81
+ }
82
+
83
+ export function mapExportDimensions(
84
+ resolved: ResolvedMap,
85
+ data: MapData,
86
+ baseWidth = 1200
87
+ ): MapExportDimensions {
88
+ const raw = mapContentAspect(resolved, data);
89
+ const clamped = Math.max(ASPECT_MIN, Math.min(ASPECT_MAX, raw));
90
+ const width = baseWidth;
91
+ let height = Math.round(width / clamped);
92
+
93
+ // Chrome reserve mirrors layout.ts `topPad` EXACTLY — the only chrome the layout
94
+ // actually subtracts from the map's fit box. The top banner reserves space ONLY
95
+ // when a title AND POIs are present (a POI-less choropleth lets the title overlay
96
+ // the land). The legend (foreground, top-center) and the caption (drawn at
97
+ // height-8, overlapping the bottom) reserve NO layout height in the renderer, so
98
+ // they are deliberately excluded — adding them would over-reserve.
99
+ let chromeReserve = 0;
100
+ if (resolved.title && resolved.pois.length > 0) {
101
+ const bannerBottom =
102
+ (resolved.subtitle ? TITLE_Y + TITLE_FONT_SIZE : TITLE_Y) +
103
+ TITLE_FONT_SIZE / 2;
104
+ chromeReserve += Math.max(FIT_PAD, bannerBottom + TITLE_GAP) - FIT_PAD;
105
+ }
106
+
107
+ let floored = false;
108
+ if (height - chromeReserve < MIN_MAP_BAND) {
109
+ height = Math.round(chromeReserve + MIN_MAP_BAND);
110
+ floored = true;
111
+ }
112
+
113
+ // The canvas was forced off the content aspect ⇒ tell the renderer to
114
+ // contain-fit (letterbox) rather than stretch-distort.
115
+ const preferContain = clamped !== raw || floored;
116
+ return { width, height, preferContain };
117
+ }