@diagrammo/dgmo 0.21.1 → 0.23.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 (87) hide show
  1. package/README.md +16 -6
  2. package/dist/advanced.cjs +2230 -503
  3. package/dist/advanced.d.cts +5731 -0
  4. package/dist/advanced.d.ts +5731 -0
  5. package/dist/advanced.js +2226 -503
  6. package/dist/auto.cjs +2272 -479
  7. package/dist/auto.d.cts +39 -0
  8. package/dist/auto.d.ts +39 -0
  9. package/dist/auto.js +124 -124
  10. package/dist/auto.mjs +2274 -480
  11. package/dist/cli.cjs +170 -170
  12. package/dist/editor.cjs +16 -16
  13. package/dist/editor.js +16 -16
  14. package/dist/highlight.cjs +18 -13
  15. package/dist/highlight.js +18 -13
  16. package/dist/index.cjs +2253 -465
  17. package/dist/index.d.cts +339 -0
  18. package/dist/index.d.ts +339 -0
  19. package/dist/index.js +2255 -466
  20. package/dist/internal.cjs +2230 -503
  21. package/dist/internal.d.cts +5731 -0
  22. package/dist/internal.d.ts +5731 -0
  23. package/dist/internal.js +2226 -503
  24. package/dist/map-data/PROVENANCE.json +1 -1
  25. package/dist/map-data/gazetteer.json +1 -1
  26. package/dist/map-data/mountain-ranges.json +1 -1
  27. package/dist/map-data/water-bodies.json +1 -0
  28. package/dist/map-data/world-coarse.json +1 -1
  29. package/dist/map-data/world-detail.json +1 -1
  30. package/docs/language-reference.md +55 -9
  31. package/gallery/fixtures/boxes-and-lines.dgmo +6 -4
  32. package/gallery/fixtures/map-categorical-world.dgmo +16 -0
  33. package/gallery/fixtures/map-categorical.dgmo +0 -1
  34. package/gallery/fixtures/map-choropleth.dgmo +0 -1
  35. package/gallery/fixtures/map-coastline.dgmo +7 -0
  36. package/gallery/fixtures/map-colorize.dgmo +11 -0
  37. package/gallery/fixtures/map-direct-color.dgmo +0 -1
  38. package/gallery/fixtures/map-reference-world.dgmo +11 -0
  39. package/gallery/fixtures/map-region-scope.dgmo +0 -3
  40. package/gallery/fixtures/map-route.dgmo +0 -1
  41. package/package.json +1 -1
  42. package/src/advanced.ts +12 -1
  43. package/src/boxes-and-lines/parser.ts +39 -0
  44. package/src/boxes-and-lines/renderer.ts +205 -20
  45. package/src/boxes-and-lines/types.ts +9 -0
  46. package/src/cli.ts +1 -1
  47. package/src/completion.ts +36 -30
  48. package/src/cycle/renderer.ts +14 -1
  49. package/src/d3.ts +20 -6
  50. package/src/editor/highlight-api.ts +4 -0
  51. package/src/editor/keywords.ts +16 -16
  52. package/src/infra/renderer.ts +35 -7
  53. package/src/map/colorize.ts +54 -0
  54. package/src/map/context-labels.ts +429 -0
  55. package/src/map/data/PROVENANCE.json +1 -1
  56. package/src/map/data/README.md +6 -0
  57. package/src/map/data/gazetteer.json +1 -1
  58. package/src/map/data/mountain-ranges.json +1 -1
  59. package/src/map/data/types.ts +34 -0
  60. package/src/map/data/water-bodies.json +1 -0
  61. package/src/map/data/world-coarse.json +1 -1
  62. package/src/map/data/world-detail.json +1 -1
  63. package/src/map/dimensions.ts +117 -0
  64. package/src/map/geo-query.ts +21 -3
  65. package/src/map/geo.ts +47 -1
  66. package/src/map/layout.ts +1408 -266
  67. package/src/map/load-data.ts +10 -2
  68. package/src/map/parser.ts +42 -116
  69. package/src/map/renderer.ts +604 -14
  70. package/src/map/resolved-types.ts +16 -2
  71. package/src/map/resolver.ts +208 -59
  72. package/src/map/types.ts +30 -32
  73. package/src/mindmap/renderer.ts +10 -1
  74. package/src/palettes/atlas.ts +77 -0
  75. package/src/palettes/blueprint.ts +73 -0
  76. package/src/palettes/color-utils.ts +58 -1
  77. package/src/palettes/index.ts +12 -3
  78. package/src/palettes/slate.ts +73 -0
  79. package/src/palettes/tidewater.ts +73 -0
  80. package/src/render.ts +8 -1
  81. package/src/tech-radar/renderer.ts +3 -0
  82. package/src/tech-radar/types.ts +3 -0
  83. package/src/utils/d3-types.ts +5 -0
  84. package/src/utils/legend-layout.ts +21 -4
  85. package/src/utils/legend-types.ts +7 -0
  86. package/src/utils/reserved-key-registry.ts +8 -3
  87. package/src/palettes/bold.ts +0 -67
@@ -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
+ }
@@ -21,14 +21,19 @@ import type { DecodedFeature } from './geo';
21
21
  import { pixelToLonLat, lonLatToPixel } from './invert';
22
22
  import type { MapData, GeoExtent } from './resolved-types';
23
23
  import type { Gazetteer } from './data/types';
24
+ import type { DgmoError } from '../diagnostics';
24
25
 
25
26
  /** Nearest gazetteer city to a point: the real haversine distance, plus the
26
- * canonical name + ISO + (US-only) subdivision for token shaping. */
27
+ * canonical name + ISO + (US-only) subdivision for token shaping. `lon`/`lat`
28
+ * are the city's own gazetteer coordinates (so callers can mark it on the map,
29
+ * distinct from the inspected point). */
27
30
  export interface NearestCity {
28
31
  readonly name: string;
29
32
  readonly iso: string;
30
33
  readonly sub?: string;
31
34
  readonly distanceKm: number;
35
+ readonly lon: number;
36
+ readonly lat: number;
32
37
  }
33
38
 
34
39
  /** A region declaration with its canonical/primary form plus bare alternates
@@ -85,6 +90,10 @@ export interface MapGeoQuery {
85
90
  locate(px: number, py: number): ResultCard | null;
86
91
  /** Culled + projected cities for the all-cities layer (population-primary). */
87
92
  cities(extent?: GeoExtent): ProjectedCity[];
93
+ /** Layout-time, dimension-dependent diagnostics. They live on the geo-query
94
+ * (bound to the rendered layout) rather than the resolver. Callers merge them
95
+ * with `resolved.diagnostics`. (No producers currently — always empty.) */
96
+ readonly diagnostics: readonly DgmoError[];
88
97
  }
89
98
 
90
99
  export interface CreateMapGeoQueryOptions {
@@ -144,6 +153,8 @@ function nearestCity(
144
153
  iso: c[2],
145
154
  ...(c[5] !== undefined && { sub: c[5] }),
146
155
  distanceKm: best.dist,
156
+ lat: c[0],
157
+ lon: c[1],
147
158
  };
148
159
  }
149
160
 
@@ -206,7 +217,14 @@ const MAX_CITY_DOTS = 250;
206
217
 
207
218
  /** Construct a geo-query handle bound to the layout for `(content, width,
208
219
  * height, data, palette, isDark)`. Deterministic: identical inputs ⇒ the same
209
- * fitted projection the rendered SVG used, so inverted clicks align. */
220
+ * fitted projection the rendered SVG used, so inverted clicks align.
221
+ *
222
+ * INVARIANT: this is the PREVIEW path — it never passes `preferContain`, so its
223
+ * layout matches the in-app preview (stretch-fill), where geo-query is used. It is
224
+ * NOT valid against a content-aware EXPORT canvas (which may set `preferContain` →
225
+ * contain-fit): the inverted positions would not match that export's pixels. If
226
+ * geo-query is ever pointed at an export canvas, thread `preferContain` through
227
+ * `CreateMapGeoQueryOptions` to keep the projection in sync. */
210
228
  export function createMapGeoQuery(opts: CreateMapGeoQueryOptions): MapGeoQuery {
211
229
  const { content, width, height, data, palette, isDark } = opts;
212
230
  const resolved = resolveMap(parseMap(content), data);
@@ -273,5 +291,5 @@ export function createMapGeoQuery(opts: CreateMapGeoQueryOptions): MapGeoQuery {
273
291
  return out;
274
292
  };
275
293
 
276
- return { invert, project, locate, cities };
294
+ return { invert, project, locate, cities, diagnostics: layout.diagnostics };
277
295
  }
package/src/map/geo.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  // Geometry helpers for the resolver: topology indexing + antimeridian-correct
2
2
  // feature bounds (via d3-geo geoBounds — NOT naive min/max, which breaks on the
3
3
  // antimeridian and on multi-part features like US Alaska/Hawaii; R5/R6).
4
- import { feature } from 'topojson-client';
4
+ import { feature, neighbors } from 'topojson-client';
5
5
  import { geoBounds, geoArea } from 'd3-geo';
6
6
  import type { BoundaryTopology } from './data/types';
7
7
  import type { GeoExtent } from './resolved-types';
@@ -47,6 +47,52 @@ export function idSet(topo: BoundaryTopology): Set<string> {
47
47
  return new Set(geomObject(topo).geometries.map((g) => g.id));
48
48
  }
49
49
 
50
+ // Memoize adjacency on the RAW asset object (never the per-render-mutated
51
+ // `worldLayer`). Keyed by topology identity — the assets are stable singletons
52
+ // from load-data.ts, so one build per topology lasts the process (G13).
53
+ const adjacencyCache = new WeakMap<BoundaryTopology, Map<string, string[]>>();
54
+
55
+ /** Per-topology arc-adjacency: ISO → neighbour ISOs, from shared TopoJSON arcs
56
+ * (`topojson-client.neighbors()` on the RAW topology geometries — arcs live on
57
+ * the topology, independent of any `feature()` decode — F2/ADR-4). Computed
58
+ * per-topology (a country never neighbours a state) and memoized.
59
+ *
60
+ * DATA HYGIENE (G1): the raw geometry array can carry (a) `type: null`
61
+ * sovereignty stubs with no arcs (e.g. "Ashmore & Cartier Is." tagged `AU`) and
62
+ * (b) genuine duplicate ISO ids. Null stubs are skipped (no arcs → no
63
+ * adjacency), and every geometry sharing one ISO is UNIONED into a single ISO
64
+ * node — matching the merged layer `decodeLayer` actually draws. Without this
65
+ * the ISO-keyed graph corrupts and the AC9 no-collision guarantee degrades. */
66
+ export function buildAdjacency(topo: BoundaryTopology): Map<string, string[]> {
67
+ const cached = adjacencyCache.get(topo);
68
+ if (cached) return cached;
69
+ const geometries = geomObject(topo).geometries as Array<{
70
+ id: string;
71
+ type?: string;
72
+ }>;
73
+ // neighbors() returns, per geometry BY ARRAY POSITION, the indices of
74
+ // arc-sharing geometries. Null-geometry stubs share no arcs → empty lists.
75
+ const nb = neighbors(geometries as never);
76
+ const sets = new Map<string, Set<string>>();
77
+ geometries.forEach((g, i) => {
78
+ if (!g.type || g.type === 'null') return; // skip arc-less sovereignty stubs
79
+ let set = sets.get(g.id);
80
+ if (!set) {
81
+ set = new Set<string>();
82
+ sets.set(g.id, set);
83
+ }
84
+ for (const j of nb[i] ?? []) {
85
+ const nid = geometries[j]?.id;
86
+ if (nid && nid !== g.id) set.add(nid); // union; never self
87
+ }
88
+ });
89
+ const out = new Map<string, string[]>();
90
+ // Deterministic neighbour order (sorted) so downstream coloring is stable.
91
+ for (const [iso, set] of sets) out.set(iso, [...set].sort());
92
+ adjacencyCache.set(topo, out);
93
+ return out;
94
+ }
95
+
50
96
  /** A decoded boundary feature: the GeoJSON geometry plus its ISO id and display
51
97
  * name (carried straight from the topology's `properties.name`). Used for
52
98
  * point-in-polygon reverse-geocoding by the geo-query. */