compasso 0.2.0 → 0.3.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 (67) hide show
  1. package/README.md +70 -7
  2. package/dist/chunk-F47C6ZEB.js +1041 -0
  3. package/dist/chunk-F47C6ZEB.js.map +1 -0
  4. package/dist/chunk-JP4N42AY.js +497 -0
  5. package/dist/chunk-JP4N42AY.js.map +1 -0
  6. package/dist/{chunk-P2S7AUOL.js → chunk-LRHHUJFZ.js} +3 -3
  7. package/dist/{chunk-P2S7AUOL.js.map → chunk-LRHHUJFZ.js.map} +1 -1
  8. package/dist/{chunk-5B453C4P.js → chunk-O3BT2O42.js} +32 -3
  9. package/dist/chunk-O3BT2O42.js.map +1 -0
  10. package/dist/{chunk-EHQMKVDM.js → chunk-Q6DVTCXD.js} +9 -24
  11. package/dist/chunk-Q6DVTCXD.js.map +1 -0
  12. package/dist/{chunk-5PGOL2KR.js → chunk-RWPGGWO5.js} +9 -28
  13. package/dist/chunk-RWPGGWO5.js.map +1 -0
  14. package/dist/{chunk-TP3JOOJW.js → chunk-ZBDABVIO.js} +3 -3
  15. package/dist/{chunk-TP3JOOJW.js.map → chunk-ZBDABVIO.js.map} +1 -1
  16. package/dist/core/index.cjs +30 -0
  17. package/dist/core/index.cjs.map +1 -1
  18. package/dist/core/index.d.cts +5 -1
  19. package/dist/core/index.d.ts +5 -1
  20. package/dist/core/index.js +1 -1
  21. package/dist/ecomap/index.cjs +32 -21
  22. package/dist/ecomap/index.cjs.map +1 -1
  23. package/dist/ecomap/index.js +2 -2
  24. package/dist/fault-tree/index.js +2 -2
  25. package/dist/fishbone/index.js +2 -2
  26. package/dist/genogram/index.cjs +36 -25
  27. package/dist/genogram/index.cjs.map +1 -1
  28. package/dist/genogram/index.d.cts +4 -2
  29. package/dist/genogram/index.d.ts +4 -2
  30. package/dist/genogram/index.js +2 -2
  31. package/dist/index.cjs +1616 -53
  32. package/dist/index.cjs.map +1 -1
  33. package/dist/index.d.cts +7 -2
  34. package/dist/index.d.ts +7 -2
  35. package/dist/index.js +7 -5
  36. package/dist/kinship-DqEklrDN.d.ts +51 -0
  37. package/dist/kinship-Dy_ijjJV.d.cts +51 -0
  38. package/dist/labels-CBQ_3Ec9.d.cts +123 -0
  39. package/dist/labels-DNqRkWuI.d.ts +123 -0
  40. package/dist/labels-iZjijjtK.d.cts +64 -0
  41. package/dist/labels-iZjijjtK.d.ts +64 -0
  42. package/dist/locales/pt-br.cjs +58 -0
  43. package/dist/locales/pt-br.cjs.map +1 -1
  44. package/dist/locales/pt-br.d.cts +9 -2
  45. package/dist/locales/pt-br.d.ts +9 -2
  46. package/dist/locales/pt-br.js +55 -1
  47. package/dist/locales/pt-br.js.map +1 -1
  48. package/dist/pedigree/index.cjs +1151 -0
  49. package/dist/pedigree/index.cjs.map +1 -0
  50. package/dist/pedigree/index.d.cts +155 -0
  51. package/dist/pedigree/index.d.ts +155 -0
  52. package/dist/pedigree/index.js +4 -0
  53. package/dist/pedigree/index.js.map +1 -0
  54. package/dist/phylo/index.cjs +553 -0
  55. package/dist/phylo/index.cjs.map +1 -0
  56. package/dist/phylo/index.d.cts +158 -0
  57. package/dist/phylo/index.d.ts +158 -0
  58. package/dist/phylo/index.js +4 -0
  59. package/dist/phylo/index.js.map +1 -0
  60. package/dist/types-BnMG7TCd.d.cts +66 -0
  61. package/dist/types-BnMG7TCd.d.ts +66 -0
  62. package/package.json +34 -12
  63. package/dist/chunk-5B453C4P.js.map +0 -1
  64. package/dist/chunk-5PGOL2KR.js.map +0 -1
  65. package/dist/chunk-EHQMKVDM.js.map +0 -1
  66. package/dist/kinship-BARO5-qz.d.cts +0 -115
  67. package/dist/kinship-Bkf87Jhu.d.ts +0 -115
package/dist/index.cjs CHANGED
@@ -5,6 +5,35 @@ function xmlEscape(text) {
5
5
  return text.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&apos;");
6
6
  }
7
7
 
8
+ // src/core/roman.ts
9
+ var ROMAN_TABLE = [
10
+ [1e3, "M"],
11
+ [900, "CM"],
12
+ [500, "D"],
13
+ [400, "CD"],
14
+ [100, "C"],
15
+ [90, "XC"],
16
+ [50, "L"],
17
+ [40, "XL"],
18
+ [10, "X"],
19
+ [9, "IX"],
20
+ [5, "V"],
21
+ [4, "IV"],
22
+ [1, "I"]
23
+ ];
24
+ function romanNumeral(n) {
25
+ if (!Number.isInteger(n) || n < 1 || n > 3999) return String(n);
26
+ let remaining = n;
27
+ let out = "";
28
+ for (const [value, symbol] of ROMAN_TABLE) {
29
+ while (remaining >= value) {
30
+ out += symbol;
31
+ remaining -= value;
32
+ }
33
+ }
34
+ return out;
35
+ }
36
+
8
37
  // src/core/geometry.ts
9
38
  function pathData(points) {
10
39
  return points.map((p, i) => `${i === 0 ? "M" : "L"} ${p.x} ${p.y}`).join(" ");
@@ -236,14 +265,6 @@ function shapeForSex(sex) {
236
265
  if (sex === "female") return "circle";
237
266
  return "diamond";
238
267
  }
239
- function wrapNodeLabel(displayLabel) {
240
- const perLine = Math.min(26, Math.max(14, Math.ceil(displayLabel.length / 2) + 2));
241
- return wrapLabel(displayLabel, perLine);
242
- }
243
- function clampLabel2(label, maxChars) {
244
- if (maxChars === void 0 || label.length <= maxChars) return label;
245
- return label.slice(0, Math.max(1, maxChars - 1)) + "\u2026";
246
- }
247
268
  var STUB_OFFSETS = [9, -9, 18, -18, 26, -26];
248
269
  var ARRIVAL_HALF = NODE_SIZE / 2 - 8;
249
270
  function stubOffset(slot) {
@@ -467,7 +488,7 @@ function computeGenogramLayout(people, unions, parentLinks, relationships, opts
467
488
  }
468
489
  const measured = /* @__PURE__ */ new Map();
469
490
  for (const p of people) {
470
- const lines = wrapNodeLabel(clampLabel2(p.label, opts.maxLabelChars));
491
+ const lines = wrapLabelBalanced(clampLabel(p.label, opts.maxLabelChars));
471
492
  const contentW = Math.max(NODE_SIZE, lines.reduce((m, l) => Math.max(m, estimateTextWidth(l, LABEL_FONT)), 0));
472
493
  measured.set(p.id, { person: p, lines, contentW });
473
494
  }
@@ -1031,11 +1052,6 @@ var STRUCT_WIDTH = 1.5;
1031
1052
  var STRUCT_OPACITY = 0.75;
1032
1053
  var DOTTED_DASH = [2, 3];
1033
1054
  var DOTTED_OPACITY = 0.55;
1034
- var LEGEND_ROW_H2 = 18;
1035
- var LEGEND_PAD2 = 16;
1036
- var LEGEND_SWATCH_W2 = 22;
1037
- var LEGEND_GAP2 = 14;
1038
- var LEGEND_FONT2 = 11;
1039
1055
  function glyphSvg(shape, cx, cy, half) {
1040
1056
  const stroke = `fill="transparent" stroke="${GLYPH_STROKE}" stroke-width="2"`;
1041
1057
  if (shape === "square") {
@@ -1145,13 +1161,13 @@ function genogramLayoutSvg(layout, opts = {}) {
1145
1161
  for (const shape of ["square", "circle", "diamond"]) {
1146
1162
  if (!shapesUsed.has(shape)) continue;
1147
1163
  entries.push({
1148
- swatch: (x, y) => glyphSvg(shape, x + LEGEND_SWATCH_W2 / 2, y, 6),
1164
+ swatch: (x, y) => glyphSvg(shape, x + LEGEND_SWATCH_W / 2, y, 6),
1149
1165
  label: labels.shapes[shape]
1150
1166
  });
1151
1167
  }
1152
1168
  if (layout.nodes.some((n) => n.deceased)) {
1153
1169
  entries.push({
1154
- swatch: (x, y) => glyphSvg("square", x + LEGEND_SWATCH_W2 / 2, y, 6) + `<line x1="${x + LEGEND_SWATCH_W2 / 2 - 6}" y1="${y - 6}" x2="${x + LEGEND_SWATCH_W2 / 2 + 6}" y2="${y + 6}" stroke="${GLYPH_STROKE}" stroke-width="2"/>`,
1170
+ swatch: (x, y) => glyphSvg("square", x + LEGEND_SWATCH_W / 2, y, 6) + `<line x1="${x + LEGEND_SWATCH_W / 2 - 6}" y1="${y - 6}" x2="${x + LEGEND_SWATCH_W / 2 + 6}" y2="${y + 6}" stroke="${GLYPH_STROKE}" stroke-width="2"/>`,
1155
1171
  label: labels.deceased
1156
1172
  });
1157
1173
  }
@@ -1161,27 +1177,21 @@ function genogramLayoutSvg(layout, opts = {}) {
1161
1177
  const ink = EDGE_STROKE[style];
1162
1178
  const dashAttr = ink.dash === null ? "" : ` stroke-dasharray="${ink.dash[0]} ${ink.dash[1]}"`;
1163
1179
  entries.push({
1164
- swatch: (x, y) => `<line x1="${x}" y1="${y}" x2="${x + LEGEND_SWATCH_W2}" y2="${y}" stroke="${EDGE_INK}" stroke-width="${ink.width}" stroke-opacity="${ink.opacity}"${dashAttr}/>`,
1180
+ swatch: (x, y) => `<line x1="${x}" y1="${y}" x2="${x + LEGEND_SWATCH_W}" y2="${y}" stroke="${EDGE_INK}" stroke-width="${ink.width}" stroke-opacity="${ink.opacity}"${dashAttr}/>`,
1165
1181
  label: labels.bondStyles[style]
1166
1182
  });
1167
1183
  }
1168
1184
  if (layout.isolatedPersonIds.length > 0) {
1169
1185
  entries.push({
1170
- swatch: (x, y) => glyphSvg("square", x + LEGEND_SWATCH_W2 / 2, y, 6),
1186
+ swatch: (x, y) => glyphSvg("square", x + LEGEND_SWATCH_W / 2, y, 6),
1171
1187
  label: labels.isolated
1172
1188
  });
1173
1189
  }
1174
- if (entries.length > 0) {
1175
- const startY = layout.height;
1176
- const rows = entries.map((entry, i) => {
1177
- const rowCenterY = startY + i * LEGEND_ROW_H2 + LEGEND_ROW_H2 / 2;
1178
- const textX = LEGEND_PAD2 + LEGEND_SWATCH_W2 + LEGEND_GAP2;
1179
- return entry.swatch(LEGEND_PAD2, rowCenterY) + `<text x="${textX}" y="${rowCenterY + LEGEND_FONT2 * 0.32}" font-family="${FONT_FAMILY}" font-size="${LEGEND_FONT2}" fill="${GLYPH_STROKE}">${xmlEscape(entry.label)}</text>`;
1180
- });
1181
- parts.push(`<g data-compasso-legend="true">${rows.join("")}</g>`);
1182
- const widestLabel = entries.reduce((m, e) => Math.max(m, estimateTextWidth(e.label, LEGEND_FONT2)), 0);
1183
- width = Math.max(width, LEGEND_PAD2 + LEGEND_SWATCH_W2 + LEGEND_GAP2 + widestLabel + LEGEND_PAD2);
1184
- height = startY + entries.length * LEGEND_ROW_H2 + LEGEND_PAD2 / 2;
1190
+ const block = legendBlock(entries, layout.height);
1191
+ if (block.svg !== "") {
1192
+ parts.push(block.svg);
1193
+ width = Math.max(width, block.width);
1194
+ height = block.height;
1185
1195
  }
1186
1196
  }
1187
1197
  return `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${width} ${height}" width="${width}" height="${height}" role="img" aria-label="${xmlEscape(labels.ariaLabel)}">` + parts.join("") + `</svg>`;
@@ -1258,16 +1268,7 @@ var SINGLE_RING_MAX = 8;
1258
1268
  var NODE_STROKE = "#52525b";
1259
1269
  var LABEL_FILL = "#3f3f46";
1260
1270
  var EDGE_INK2 = "#71717a";
1261
- var LEGEND_ROW_H3 = 18;
1262
- var LEGEND_PAD3 = 16;
1263
- var LEGEND_SWATCH_W3 = 22;
1264
- var LEGEND_GAP3 = 14;
1265
- var LEGEND_FONT3 = 11;
1266
1271
  var round = (n) => Math.round(n * 100) / 100;
1267
- function wrapTieLabel(displayLabel) {
1268
- const perLine = Math.min(26, Math.max(14, Math.ceil(displayLabel.length / 2) + 2));
1269
- return wrapLabel(displayLabel, perLine);
1270
- }
1271
1272
  function arrowHead(tipX, tipY, ux, uy, opacity) {
1272
1273
  const LEN = 9;
1273
1274
  const HALF_W = 4.5;
@@ -1286,7 +1287,7 @@ function ecomapSvg(input, opts = {}) {
1286
1287
  const fontSize = opts.fontSize ?? 12;
1287
1288
  const labels = opts.labels ?? ECOMAP_LABELS_EN;
1288
1289
  const sats = [...input.ties].sort((a, b) => a.id - b.id).map((tie) => {
1289
- const lines = wrapTieLabel(clampLabel(tie.label, opts.maxLabelChars));
1290
+ const lines = wrapLabelBalanced(clampLabel(tie.label, opts.maxLabelChars));
1290
1291
  const w2 = Math.max(...lines.map((l) => estimateTextWidth(l, fontSize))) + NODE_PAD_X * 2;
1291
1292
  const h2 = lines.length * LINE_H + NODE_PAD_Y * 2;
1292
1293
  return {
@@ -1300,7 +1301,7 @@ function ecomapSvg(input, opts = {}) {
1300
1301
  style: qualityLineStyle(tie.quality, opts.qualityLexicon)
1301
1302
  };
1302
1303
  });
1303
- const centerLines = wrapTieLabel(input.centerLabel);
1304
+ const centerLines = wrapLabelBalanced(input.centerLabel);
1304
1305
  const centerR = Math.max(
1305
1306
  CENTER_MIN_R,
1306
1307
  Math.max(...centerLines.map((l) => estimateTextWidth(l, fontSize))) / 2 + 12,
@@ -1398,7 +1399,7 @@ function ecomapSvg(input, opts = {}) {
1398
1399
  const entries = [];
1399
1400
  if (sats.some((s) => s.style === "plain")) {
1400
1401
  entries.push({
1401
- swatch: (x, y) => `<line x1="${x}" y1="${y}" x2="${x + LEGEND_SWATCH_W3}" y2="${y}" stroke="${EDGE_INK2}" stroke-width="${EDGE_STROKE.plain.width}" stroke-opacity="${EDGE_STROKE.plain.opacity}"/>`,
1402
+ swatch: (x, y) => `<line x1="${x}" y1="${y}" x2="${x + LEGEND_SWATCH_W}" y2="${y}" stroke="${EDGE_INK2}" stroke-width="${EDGE_STROKE.plain.width}" stroke-opacity="${EDGE_STROKE.plain.opacity}"/>`,
1402
1403
  label: labels.neutralTie
1403
1404
  });
1404
1405
  }
@@ -1408,27 +1409,21 @@ function ecomapSvg(input, opts = {}) {
1408
1409
  const ink = EDGE_STROKE[style];
1409
1410
  const dashAttr = ink.dash === null ? "" : ` stroke-dasharray="${ink.dash[0]} ${ink.dash[1]}"`;
1410
1411
  entries.push({
1411
- swatch: (x, y) => `<line x1="${x}" y1="${y}" x2="${x + LEGEND_SWATCH_W3}" y2="${y}" stroke="${EDGE_INK2}" stroke-width="${ink.width}" stroke-opacity="${ink.opacity}"${dashAttr}/>`,
1412
+ swatch: (x, y) => `<line x1="${x}" y1="${y}" x2="${x + LEGEND_SWATCH_W}" y2="${y}" stroke="${EDGE_INK2}" stroke-width="${ink.width}" stroke-opacity="${ink.opacity}"${dashAttr}/>`,
1412
1413
  label: labels.bondStyles[style]
1413
1414
  });
1414
1415
  }
1415
1416
  if (sats.some((s) => s.tie.direction !== null)) {
1416
1417
  entries.push({
1417
- swatch: (x, y) => `<line x1="${x}" y1="${y}" x2="${x + LEGEND_SWATCH_W3 - 8}" y2="${y}" stroke="${EDGE_INK2}" stroke-width="1.5" stroke-opacity="0.75"/>` + arrowHead(x + LEGEND_SWATCH_W3, y, 1, 0, 0.75),
1418
+ swatch: (x, y) => `<line x1="${x}" y1="${y}" x2="${x + LEGEND_SWATCH_W - 8}" y2="${y}" stroke="${EDGE_INK2}" stroke-width="1.5" stroke-opacity="0.75"/>` + arrowHead(x + LEGEND_SWATCH_W, y, 1, 0, 0.75),
1418
1419
  label: labels.direction
1419
1420
  });
1420
1421
  }
1421
1422
  if (entries.length > 0) {
1422
- const startY = height;
1423
- const rows = entries.map((entry, i) => {
1424
- const rowCenterY = startY + i * LEGEND_ROW_H3 + LEGEND_ROW_H3 / 2;
1425
- const textX = LEGEND_PAD3 + LEGEND_SWATCH_W3 + LEGEND_GAP3;
1426
- return entry.swatch(LEGEND_PAD3, rowCenterY) + `<text x="${textX}" y="${rowCenterY + LEGEND_FONT3 * 0.32}" font-family="${FONT_FAMILY}" font-size="${LEGEND_FONT3}" fill="${NODE_STROKE}">${xmlEscape(entry.label)}</text>`;
1427
- });
1428
- parts.push(`<g data-compasso-legend="true">${rows.join("")}</g>`);
1429
- const widestLabel = entries.reduce((m, e) => Math.max(m, estimateTextWidth(e.label, LEGEND_FONT3)), 0);
1430
- width = Math.max(width, LEGEND_PAD3 + LEGEND_SWATCH_W3 + LEGEND_GAP3 + widestLabel + LEGEND_PAD3);
1431
- height = startY + entries.length * LEGEND_ROW_H3 + LEGEND_PAD3 / 2;
1423
+ const block = legendBlock(entries, height);
1424
+ parts.push(block.svg);
1425
+ width = Math.max(width, block.width);
1426
+ height = block.height;
1432
1427
  }
1433
1428
  }
1434
1429
  const w = Math.ceil(width);
@@ -2381,6 +2376,1534 @@ function fishboneSvg(input, opts = {}) {
2381
2376
  return `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${w} ${h}" width="${w}" height="${h}" role="img" aria-label="${xmlEscape(labels.ariaLabel)}">` + parts.join("") + `</svg>`;
2382
2377
  }
2383
2378
 
2379
+ // src/pedigree/types.ts
2380
+ var LIFE_STATUSES = ["alive", "stillbirth"];
2381
+
2382
+ // src/pedigree/labels.ts
2383
+ var PEDIGREE_TITLE_LABELS_EN = {
2384
+ affected: "affected",
2385
+ carrier: "carrier",
2386
+ deceased: "deceased",
2387
+ proband: "proband",
2388
+ consultand: "consultand",
2389
+ consanguineous: "consanguineous mating",
2390
+ mating: "mating",
2391
+ sibship: "sibship",
2392
+ twins: {
2393
+ mz: "monozygotic twins",
2394
+ dz: "dizygotic twins",
2395
+ unknown: "twins, zygosity unknown"
2396
+ },
2397
+ stillbirth: "stillbirth",
2398
+ generation: "generation"
2399
+ };
2400
+ var PEDIGREE_SVG_LABELS_EN = {
2401
+ shapes: {
2402
+ square: "Male",
2403
+ circle: "Female",
2404
+ diamond: "Sex unknown"
2405
+ },
2406
+ unaffected: "Unaffected",
2407
+ carrier: "Carrier",
2408
+ deceased: "Deceased",
2409
+ proband: "Proband",
2410
+ consultand: "Consultand",
2411
+ consanguineous: "Consanguineous mating",
2412
+ twins: {
2413
+ mz: "Monozygotic twins",
2414
+ dz: "Dizygotic twins",
2415
+ unknown: "Twins (zygosity unknown)"
2416
+ },
2417
+ stillbirth: "Stillbirth",
2418
+ isolated: "No recorded relatives",
2419
+ ariaLabel: "Pedigree chart"
2420
+ };
2421
+
2422
+ // src/pedigree/validate.ts
2423
+ var PedigreeValidationError = class extends Error {
2424
+ issues;
2425
+ constructor(issues) {
2426
+ super(`invalid pedigree: ${issues.map((i) => i.message).join("; ")}`);
2427
+ this.name = "PedigreeValidationError";
2428
+ this.issues = issues;
2429
+ }
2430
+ };
2431
+ var MAX_CONDITIONS_PER_INDIVIDUAL = 4;
2432
+ function sortIssues2(issues) {
2433
+ const unique = /* @__PURE__ */ new Map();
2434
+ for (const issue of issues) unique.set(`${issue.code} ${issue.message}`, issue);
2435
+ return [...unique.values()].sort(
2436
+ (a, b) => a.code !== b.code ? a.code < b.code ? -1 : 1 : a.message < b.message ? -1 : a.message > b.message ? 1 : 0
2437
+ );
2438
+ }
2439
+ function pedigreeIssues(input) {
2440
+ if (input.individuals.length === 0 && input.matings.length === 0 && input.sibships.length === 0) return [];
2441
+ const issues = [];
2442
+ const push = (code, message) => {
2443
+ issues.push({ code, message });
2444
+ };
2445
+ const individualById = /* @__PURE__ */ new Map();
2446
+ const dupIndividualIds = /* @__PURE__ */ new Set();
2447
+ for (const ind of input.individuals) {
2448
+ if (individualById.has(ind.id)) dupIndividualIds.add(ind.id);
2449
+ else individualById.set(ind.id, ind);
2450
+ }
2451
+ for (const id of [...dupIndividualIds].sort((a, b) => a - b)) {
2452
+ push("duplicate-id", `duplicate individual id ${id}`);
2453
+ }
2454
+ const matingById = /* @__PURE__ */ new Map();
2455
+ const dupMatingIds = /* @__PURE__ */ new Set();
2456
+ for (const m of input.matings) {
2457
+ if (matingById.has(m.id)) dupMatingIds.add(m.id);
2458
+ else matingById.set(m.id, m);
2459
+ }
2460
+ for (const id of [...dupMatingIds].sort((a, b) => a - b)) {
2461
+ push("duplicate-id", `duplicate mating id ${id}`);
2462
+ }
2463
+ const sibshipById = /* @__PURE__ */ new Map();
2464
+ const dupSibshipIds = /* @__PURE__ */ new Set();
2465
+ for (const s of input.sibships) {
2466
+ if (sibshipById.has(s.id)) dupSibshipIds.add(s.id);
2467
+ else sibshipById.set(s.id, s);
2468
+ }
2469
+ for (const id of [...dupSibshipIds].sort((a, b) => a - b)) {
2470
+ push("duplicate-id", `duplicate sibship id ${id}`);
2471
+ }
2472
+ if (issues.length > 0) {
2473
+ return sortIssues2(issues);
2474
+ }
2475
+ const individuals = [...individualById.values()].sort((a, b) => a.id - b.id);
2476
+ const matings = [...matingById.values()].sort((a, b) => a.id - b.id);
2477
+ const sibships = [...sibshipById.values()].sort((a, b) => a.id - b.id);
2478
+ const conditionIds = new Set(input.conditions.map((c) => c.id));
2479
+ for (const m of matings) {
2480
+ if (!individualById.has(m.partnerAId)) {
2481
+ push("unknown-partner", `mating ${m.id} references unknown individual ${m.partnerAId}`);
2482
+ }
2483
+ if (!individualById.has(m.partnerBId)) {
2484
+ push("unknown-partner", `mating ${m.id} references unknown individual ${m.partnerBId}`);
2485
+ }
2486
+ if (m.partnerAId === m.partnerBId) {
2487
+ push("self-mating", `mating ${m.id} mates individual ${m.partnerAId} with itself`);
2488
+ }
2489
+ }
2490
+ for (const s of sibships) {
2491
+ if (!matingById.has(s.matingId)) {
2492
+ push("unknown-sibship-mating", `sibship ${s.id} references unknown mating ${s.matingId}`);
2493
+ }
2494
+ for (const childId of s.childIds) {
2495
+ if (!individualById.has(childId)) {
2496
+ push("unknown-child", `sibship ${s.id} references unknown child ${childId}`);
2497
+ }
2498
+ }
2499
+ const childSet = new Set(s.childIds);
2500
+ const offending = /* @__PURE__ */ new Set();
2501
+ for (const tg of s.twinGroups) {
2502
+ for (const memberId of tg.childIds) {
2503
+ if (!childSet.has(memberId)) offending.add(memberId);
2504
+ }
2505
+ }
2506
+ for (const memberId of [...offending].sort((a, b) => a - b)) {
2507
+ push("twin-not-in-sibship", `twin ${memberId} is not a child of sibship ${s.id}`);
2508
+ }
2509
+ for (const tg of s.twinGroups) {
2510
+ if (tg.childIds.length < 2) {
2511
+ const [only] = tg.childIds;
2512
+ push(
2513
+ "twin-group-too-small",
2514
+ `twin group in sibship ${s.id} has only 1 member (${only ?? "none"}) \u2014 at least 2 required`
2515
+ );
2516
+ }
2517
+ }
2518
+ for (const tg of s.twinGroups) {
2519
+ if (tg.childIds.length < 2) continue;
2520
+ const memberSet = new Set(tg.childIds);
2521
+ const positions = s.childIds.map((cid, pos) => memberSet.has(cid) ? pos : -1).filter((p) => p >= 0);
2522
+ if (positions.length === 0) continue;
2523
+ const first = positions[0];
2524
+ const last = positions[positions.length - 1];
2525
+ if (last - first !== positions.length - 1) {
2526
+ const memberList = [...tg.childIds].sort((a, b) => a - b).join(", ");
2527
+ push(
2528
+ "twin-group-not-contiguous",
2529
+ `twin group [${memberList}] in sibship ${s.id} is not a contiguous run in childIds`
2530
+ );
2531
+ }
2532
+ }
2533
+ }
2534
+ const sibshipsOfChild = /* @__PURE__ */ new Map();
2535
+ for (const s of sibships) {
2536
+ for (const childId of s.childIds) {
2537
+ const arr = sibshipsOfChild.get(childId) ?? [];
2538
+ arr.push(s.id);
2539
+ sibshipsOfChild.set(childId, arr);
2540
+ }
2541
+ }
2542
+ for (const [childId, sibIds] of [...sibshipsOfChild.entries()].sort((a, b) => a[0] - b[0])) {
2543
+ if (sibIds.length > 1) {
2544
+ push(
2545
+ "child-of-two-sibships",
2546
+ `individual ${childId} is a child of sibships ${[...sibIds].sort((a, b) => a - b).join(", ")}`
2547
+ );
2548
+ }
2549
+ }
2550
+ for (const ind of individuals) {
2551
+ if (!Number.isInteger(ind.generation)) {
2552
+ push("generation-not-integer", `individual ${ind.id} has non-integer generation ${ind.generation}`);
2553
+ }
2554
+ }
2555
+ for (const ind of individuals) {
2556
+ for (const conditionId of ind.affectedBy) {
2557
+ if (!conditionIds.has(conditionId)) {
2558
+ push("unknown-condition", `individual ${ind.id} is affected by unknown condition ${conditionId}`);
2559
+ }
2560
+ }
2561
+ if (ind.affectedBy.length > MAX_CONDITIONS_PER_INDIVIDUAL) {
2562
+ push(
2563
+ "too-many-conditions",
2564
+ `individual ${ind.id} has ${ind.affectedBy.length} conditions \u2014 at most ${MAX_CONDITIONS_PER_INDIVIDUAL} can be drawn`
2565
+ );
2566
+ }
2567
+ }
2568
+ const GRAPH_BLOCKING = /* @__PURE__ */ new Set([
2569
+ "duplicate-id",
2570
+ "unknown-partner",
2571
+ "unknown-sibship-mating",
2572
+ "unknown-child"
2573
+ ]);
2574
+ if (!issues.some((i) => GRAPH_BLOCKING.has(i.code))) {
2575
+ for (const s of sibships) {
2576
+ const mating = matingById.get(s.matingId);
2577
+ if (mating === void 0) continue;
2578
+ const a = individualById.get(mating.partnerAId);
2579
+ const b = individualById.get(mating.partnerBId);
2580
+ const parentGenerations = [a, b].filter((p) => p !== void 0 && Number.isInteger(p.generation)).map((p) => p.generation);
2581
+ if (parentGenerations.length === 0) continue;
2582
+ const parentRow = Math.max(...parentGenerations);
2583
+ for (const childId of s.childIds) {
2584
+ const child = individualById.get(childId);
2585
+ if (child === void 0 || !Number.isInteger(child.generation)) continue;
2586
+ if (child.generation <= parentRow) {
2587
+ push(
2588
+ "generation-inversion",
2589
+ `child ${childId} (generation ${child.generation}) is not below its parents (generation ${parentRow}) in sibship ${s.id}`
2590
+ );
2591
+ }
2592
+ }
2593
+ }
2594
+ }
2595
+ return sortIssues2(issues);
2596
+ }
2597
+ function validatePedigree(input) {
2598
+ const issues = pedigreeIssues(input);
2599
+ if (issues.length > 0) throw new PedigreeValidationError(issues);
2600
+ }
2601
+
2602
+ // src/pedigree/layout.ts
2603
+ var PED_GLYPH = 40;
2604
+ var PED_LABEL_FONT = 12;
2605
+ var PED_LABEL_LINE_H = 14;
2606
+ var PED_ADDRESS_FONT = 10;
2607
+ var PED_LABEL_GAP = 6;
2608
+ var PADDING5 = 32;
2609
+ var GUTTER = 36;
2610
+ var H_GAP2 = 28;
2611
+ var MATING_RUN = 22;
2612
+ var CORRIDOR2 = 44;
2613
+ var TWIN_JUNCTION_DROP = 12;
2614
+ var BRIDGE_DIP_GAP = 10;
2615
+ var BRIDGE_DIP_STEP = 7;
2616
+ var BRIDGE_STUB_X_STEP = 3;
2617
+ var PED_MATING_ID_BASE = 1e6;
2618
+ var PED_DESCENT_ID_BASE = 2e6;
2619
+ var PED_SIBBAR_ID_BASE = 3e6;
2620
+ var PED_RISER_ID_BASE = 4e6;
2621
+ var PED_TWINBAR_ID_BASE = 5e6;
2622
+ var round5 = (n) => Math.round(n * 100) / 100;
2623
+ function shapeForSex2(sex) {
2624
+ if (sex === "male") return "square";
2625
+ if (sex === "female") return "circle";
2626
+ return "diamond";
2627
+ }
2628
+ var PED_CONDITION_FILLS = ["#52525b", "#a1a1aa", "#3f3f46", "#71717a"];
2629
+ function wrapPedigreeLabel(displayLabel) {
2630
+ if (displayLabel === "") return [];
2631
+ return wrapLabelBalanced(displayLabel, 2);
2632
+ }
2633
+ function individualTitle(ind, address, conditionLabelById, titleLabels) {
2634
+ if (ind.title !== void 0) return ind.title;
2635
+ const parts = [`${address} \xB7 ${ind.label}`];
2636
+ if (ind.affectedBy.length > 0) {
2637
+ const names = ind.affectedBy.map((id) => conditionLabelById.get(id) ?? String(id));
2638
+ parts.push(`${titleLabels.affected}: ${names.join(", ")}`);
2639
+ }
2640
+ if (ind.carrier && ind.affectedBy.length === 0) parts.push(titleLabels.carrier);
2641
+ if (ind.deceased) parts.push(titleLabels.deceased);
2642
+ if (ind.role === "proband") parts.push(titleLabels.proband);
2643
+ else if (ind.role === "consultand") parts.push(titleLabels.consultand);
2644
+ if (ind.lifeStatus === "stillbirth") parts.push(titleLabels.stillbirth);
2645
+ return parts.join(" \xB7 ");
2646
+ }
2647
+ function computePedigreeLayout(input, opts = {}) {
2648
+ if (input.individuals.length === 0 && input.matings.length === 0 && input.sibships.length === 0) {
2649
+ return {
2650
+ width: PADDING5 * 2,
2651
+ height: PADDING5 * 2,
2652
+ nodes: [],
2653
+ elements: [],
2654
+ generations: [],
2655
+ conditionFills: [],
2656
+ twinZygositiesUsed: [],
2657
+ unknownTwinJunctions: [],
2658
+ isolatedIndividualIds: []
2659
+ };
2660
+ }
2661
+ validatePedigree(input);
2662
+ const titleLabels = opts.titleLabels ?? PEDIGREE_TITLE_LABELS_EN;
2663
+ const individuals = [...input.individuals].sort((a, b) => a.id - b.id);
2664
+ const individualById = new Map(individuals.map((i) => [i.id, i]));
2665
+ const matings = [...input.matings].sort((a, b) => a.id - b.id);
2666
+ const sibships = [...input.sibships].sort((a, b) => a.id - b.id);
2667
+ const conditionLabelById = new Map(input.conditions.map((c) => [c.id, c.label]));
2668
+ const partnersOf = (m) => m.partnerAId <= m.partnerBId ? [m.partnerAId, m.partnerBId] : [m.partnerBId, m.partnerAId];
2669
+ const distinctGenerations = [...new Set(individuals.map((i) => i.generation))].sort((a, b) => a - b);
2670
+ const rowOfGeneration = /* @__PURE__ */ new Map();
2671
+ distinctGenerations.forEach((g, i) => rowOfGeneration.set(g, i));
2672
+ const rowCount = distinctGenerations.length;
2673
+ const labelLinesOf = /* @__PURE__ */ new Map();
2674
+ for (const ind of individuals) {
2675
+ labelLinesOf.set(ind.id, wrapPedigreeLabel(clampLabel(ind.label, opts.maxLabelChars)));
2676
+ }
2677
+ const nodeHalfWidth = (ind) => {
2678
+ const lines = labelLinesOf.get(ind.id) ?? [];
2679
+ const labelW = lines.reduce((m, l) => Math.max(m, estimateTextWidth(l, PED_LABEL_FONT)), 0);
2680
+ return Math.max(PED_GLYPH, labelW) / 2;
2681
+ };
2682
+ const childToSibship = /* @__PURE__ */ new Map();
2683
+ for (const s of sibships) for (const c of s.childIds) childToSibship.set(c, s);
2684
+ const sibshipByMating = /* @__PURE__ */ new Map();
2685
+ for (const s of sibships) {
2686
+ const arr = sibshipByMating.get(s.matingId) ?? [];
2687
+ arr.push(s);
2688
+ sibshipByMating.set(s.matingId, arr);
2689
+ }
2690
+ const matingsOfIndividual = /* @__PURE__ */ new Map();
2691
+ for (const m of matings) {
2692
+ for (const p of partnersOf(m)) {
2693
+ const arr = matingsOfIndividual.get(p) ?? [];
2694
+ arr.push(m);
2695
+ matingsOfIndividual.set(p, arr);
2696
+ }
2697
+ }
2698
+ const placedById = /* @__PURE__ */ new Map();
2699
+ let cursorX = PADDING5 + GUTTER;
2700
+ const placeLeaf = (ind, leftEdge) => {
2701
+ const half = nodeHalfWidth(ind);
2702
+ const cx = leftEdge + half;
2703
+ const placed = { ind, cx, spanL: half, spanR: half };
2704
+ placedById.set(ind.id, placed);
2705
+ return placed;
2706
+ };
2707
+ const placedMatings = /* @__PURE__ */ new Set();
2708
+ const matingCount = (id) => (matingsOfIndividual.get(id) ?? []).length;
2709
+ const hubCursor = /* @__PURE__ */ new Map();
2710
+ const hubSpouseSeq = /* @__PURE__ */ new Map();
2711
+ const packMating = (mating, leftEdge) => {
2712
+ placedMatings.add(mating.id);
2713
+ const [aId, bId] = partnersOf(mating);
2714
+ const a = individualById.get(aId);
2715
+ const b = individualById.get(bId);
2716
+ const aHalf = nodeHalfWidth(a);
2717
+ const bHalf = nodeHalfWidth(b);
2718
+ const halfSep = Math.max(aHalf, bHalf) + MATING_RUN;
2719
+ const aPlaced = placedById.get(aId);
2720
+ const bPlaced = placedById.get(bId);
2721
+ const hubId = aPlaced !== void 0 ? aId : bPlaced !== void 0 ? bId : matingCount(aId) >= 2 ? aId : matingCount(bId) >= 2 ? bId : null;
2722
+ const blockLeft = hubId !== null && hubCursor.has(hubId) ? hubCursor.get(hubId) : leftEdge;
2723
+ const sibs = (sibshipByMating.get(mating.id) ?? []).sort((s1, s2) => s1.id - s2.id);
2724
+ const childIds = sibs.flatMap((s) => s.childIds);
2725
+ let childCursor = blockLeft;
2726
+ let childLeft = Number.POSITIVE_INFINITY;
2727
+ let childrenRight = blockLeft;
2728
+ let placedAnyChild = false;
2729
+ for (const childId of childIds) {
2730
+ const child = individualById.get(childId);
2731
+ if (child === void 0 || placedById.has(childId)) continue;
2732
+ const downMating = (matingsOfIndividual.get(childId) ?? []).find((m) => {
2733
+ if (placedMatings.has(m.id)) return false;
2734
+ const [pa, pb] = partnersOf(m);
2735
+ const otherId = pa === childId ? pb : pa;
2736
+ const other = individualById.get(otherId);
2737
+ return other === void 0 || child.generation <= other.generation;
2738
+ });
2739
+ if (downMating !== void 0) {
2740
+ const right = packMating(downMating, childCursor);
2741
+ childLeft = Math.min(childLeft, placedById.get(childId).cx - nodeHalfWidth(child));
2742
+ childrenRight = right;
2743
+ childCursor = right + H_GAP2;
2744
+ } else {
2745
+ const placed = placeLeaf(child, childCursor);
2746
+ childLeft = Math.min(childLeft, placed.cx - placed.spanL);
2747
+ childrenRight = placed.cx + placed.spanR;
2748
+ childCursor = childrenRight + H_GAP2;
2749
+ }
2750
+ placedAnyChild = true;
2751
+ }
2752
+ const bandMid = placedAnyChild ? (childLeft + childrenRight) / 2 : blockLeft + halfSep;
2753
+ if (hubId === null) {
2754
+ const pairMid = placedAnyChild ? bandMid : blockLeft + Math.max(aHalf, bHalf) + halfSep;
2755
+ placedById.set(aId, { ind: a, cx: pairMid - halfSep, spanL: aHalf, spanR: aHalf });
2756
+ placedById.set(bId, { ind: b, cx: pairMid + halfSep, spanL: bHalf, spanR: bHalf });
2757
+ } else {
2758
+ const spouseId = hubId === aId ? bId : aId;
2759
+ const spouse = individualById.get(spouseId);
2760
+ const spouseHalf = nodeHalfWidth(spouse);
2761
+ const hubHalf = nodeHalfWidth(individualById.get(hubId));
2762
+ const seq = hubSpouseSeq.get(hubId) ?? 0;
2763
+ const hubExisting = placedById.get(hubId);
2764
+ const minSpouseCx = blockLeft + hubHalf + halfSep;
2765
+ let spouseCx = placedAnyChild ? Math.max(bandMid, minSpouseCx) : minSpouseCx + spouseHalf;
2766
+ if (hubExisting === void 0) {
2767
+ placedById.set(hubId, { ind: individualById.get(hubId), cx: spouseCx - halfSep, spanL: hubHalf, spanR: hubHalf });
2768
+ } else {
2769
+ spouseCx = Math.max(spouseCx, hubExisting.cx + halfSep);
2770
+ }
2771
+ placedById.set(spouseId, { ind: spouse, cx: spouseCx, spanL: spouseHalf, spanR: spouseHalf });
2772
+ hubSpouseSeq.set(hubId, seq + 1);
2773
+ }
2774
+ const blockRight = Math.max(
2775
+ childrenRight,
2776
+ placedById.get(aId).cx + nodeHalfWidth(a),
2777
+ placedById.get(bId).cx + nodeHalfWidth(b)
2778
+ );
2779
+ if (hubId !== null) hubCursor.set(hubId, blockRight + H_GAP2);
2780
+ return blockRight;
2781
+ };
2782
+ const generationOfMating = (m) => {
2783
+ const [aId, bId] = partnersOf(m);
2784
+ const a = individualById.get(aId);
2785
+ const b = individualById.get(bId);
2786
+ const gens = [a, b].filter((p) => p !== void 0).map((p) => p.generation);
2787
+ return gens.length > 0 ? Math.min(...gens) : 0;
2788
+ };
2789
+ const rootMatings = matings.filter((m) => {
2790
+ const [aId, bId] = partnersOf(m);
2791
+ return !childToSibship.has(aId) && !childToSibship.has(bId);
2792
+ }).sort((m1, m2) => generationOfMating(m1) - generationOfMating(m2) || m1.id - m2.id);
2793
+ for (const m of rootMatings) {
2794
+ if (placedMatings.has(m.id)) continue;
2795
+ const right = packMating(m, cursorX);
2796
+ cursorX = right + H_GAP2 * 2;
2797
+ }
2798
+ for (const m of matings) {
2799
+ if (placedMatings.has(m.id)) continue;
2800
+ const right = packMating(m, cursorX);
2801
+ cursorX = right + H_GAP2 * 2;
2802
+ }
2803
+ const inAnyMating = /* @__PURE__ */ new Set();
2804
+ for (const m of matings) for (const p of partnersOf(m)) inAnyMating.add(p);
2805
+ const isolatedIndividualIds = individuals.filter((i) => !inAnyMating.has(i.id) && !childToSibship.has(i.id)).map((i) => i.id);
2806
+ for (const id of isolatedIndividualIds) {
2807
+ const ind = individualById.get(id);
2808
+ if (placedById.has(id)) continue;
2809
+ placeLeaf(ind, cursorX);
2810
+ cursorX = placedById.get(id).cx + nodeHalfWidth(ind) + H_GAP2;
2811
+ }
2812
+ for (const ind of individuals) {
2813
+ if (placedById.has(ind.id)) continue;
2814
+ placeLeaf(ind, cursorX);
2815
+ cursorX = placedById.get(ind.id).cx + nodeHalfWidth(ind) + H_GAP2;
2816
+ }
2817
+ const matingBridgeSlot = /* @__PURE__ */ new Map();
2818
+ {
2819
+ const dipSlotByRow = /* @__PURE__ */ new Map();
2820
+ for (const m of [...matings].sort((m1, m2) => m1.id - m2.id)) {
2821
+ const [aId, bId] = partnersOf(m);
2822
+ const a = individualById.get(aId);
2823
+ const b = individualById.get(bId);
2824
+ if (a === void 0 || b === void 0) continue;
2825
+ if (a.generation !== b.generation) continue;
2826
+ const row = rowOfGeneration.get(a.generation);
2827
+ const loX = Math.min(placedById.get(aId).cx, placedById.get(bId).cx);
2828
+ const hiX = Math.max(placedById.get(aId).cx, placedById.get(bId).cx);
2829
+ const blocked = individuals.some(
2830
+ (i) => i.id !== aId && i.id !== bId && rowOfGeneration.get(i.generation) === row && placedById.has(i.id) && placedById.get(i.id).cx > loX + 0.01 && placedById.get(i.id).cx < hiX - 0.01
2831
+ );
2832
+ if (!blocked) {
2833
+ matingBridgeSlot.set(m.id, 0);
2834
+ continue;
2835
+ }
2836
+ const slot = (dipSlotByRow.get(row) ?? 0) + 1;
2837
+ dipSlotByRow.set(row, slot);
2838
+ matingBridgeSlot.set(m.id, slot);
2839
+ }
2840
+ }
2841
+ const rowDipBand = new Array(rowCount).fill(0);
2842
+ for (const m of matings) {
2843
+ const slot = matingBridgeSlot.get(m.id) ?? 0;
2844
+ if (slot < 1) continue;
2845
+ const [aId, bId] = partnersOf(m);
2846
+ const a = individualById.get(aId);
2847
+ const b = individualById.get(bId);
2848
+ const gens = [a, b].filter((p) => p !== void 0).map((p) => p.generation);
2849
+ if (gens.length === 0) continue;
2850
+ const row = rowOfGeneration.get(Math.min(...gens));
2851
+ rowDipBand[row] = Math.max(rowDipBand[row], BRIDGE_DIP_GAP + slot * BRIDGE_DIP_STEP + PED_LABEL_GAP);
2852
+ }
2853
+ const rowHeight = new Array(rowCount).fill(0);
2854
+ for (const ind of individuals) {
2855
+ const row = rowOfGeneration.get(ind.generation);
2856
+ const lines = labelLinesOf.get(ind.id) ?? [];
2857
+ const h = PED_GLYPH + PED_LABEL_GAP + rowDipBand[row] + (lines.length + 1) * PED_LABEL_LINE_H;
2858
+ rowHeight[row] = Math.max(rowHeight[row], h);
2859
+ }
2860
+ const rowTop = [PADDING5];
2861
+ for (let r = 0; r < rowCount - 1; r++) {
2862
+ rowTop.push(rowTop[r] + rowHeight[r] + CORRIDOR2);
2863
+ }
2864
+ const glyphCyOfRow = (row) => rowTop[row] + PED_GLYPH / 2;
2865
+ const addressByIndividual = /* @__PURE__ */ new Map();
2866
+ for (let row = 0; row < rowCount; row++) {
2867
+ const roman = romanNumeral(row + 1);
2868
+ const inRow = individuals.filter((i) => rowOfGeneration.get(i.generation) === row).sort((a, b) => placedById.get(a.id).cx - placedById.get(b.id).cx || a.id - b.id);
2869
+ inRow.forEach((ind, i) => addressByIndividual.set(ind.id, `${roman}-${i + 1}`));
2870
+ }
2871
+ const declaredConditionIds = new Set(input.conditions.map((c) => c.id));
2872
+ const usedConditionIds = [...new Set(individuals.flatMap((i) => i.affectedBy))].filter((id) => declaredConditionIds.has(id)).sort((x, y) => x - y);
2873
+ const conditionFills = usedConditionIds.map((id, i) => ({
2874
+ id,
2875
+ ink: PED_CONDITION_FILLS[i % PED_CONDITION_FILLS.length],
2876
+ label: conditionLabelById.get(id) ?? `condition ${id}`
2877
+ }));
2878
+ const nodes = [];
2879
+ for (const ind of individuals) {
2880
+ const placed = placedById.get(ind.id);
2881
+ const row = rowOfGeneration.get(ind.generation);
2882
+ const cy = glyphCyOfRow(row);
2883
+ const lines = labelLinesOf.get(ind.id) ?? [];
2884
+ const address = addressByIndividual.get(ind.id);
2885
+ nodes.push({
2886
+ individualId: ind.id,
2887
+ shape: shapeForSex2(ind.sex),
2888
+ cx: round5(placed.cx),
2889
+ cy: round5(cy),
2890
+ size: PED_GLYPH,
2891
+ deceased: ind.deceased,
2892
+ carrier: ind.carrier,
2893
+ role: ind.role,
2894
+ stillbirth: ind.lifeStatus === "stillbirth",
2895
+ affectedBy: ind.affectedBy,
2896
+ labelLines: lines,
2897
+ // Labels sit BELOW the row's serial-union dip band (rowDipBand) so a hub's bridge dips
2898
+ // never cross a label box.
2899
+ labelTop: round5(cy + PED_GLYPH / 2 + PED_LABEL_GAP + rowDipBand[row]),
2900
+ addressLabel: address,
2901
+ title: individualTitle(ind, address, conditionLabelById, titleLabels)
2902
+ });
2903
+ }
2904
+ const elements = [];
2905
+ const cxOf = (id) => placedById.get(id).cx;
2906
+ const cyOf = (id) => glyphCyOfRow(rowOfGeneration.get(individualById.get(id).generation));
2907
+ const GLYPH_HALF = PED_GLYPH / 2;
2908
+ const matingBarCenter = /* @__PURE__ */ new Map();
2909
+ for (const s of sibships) {
2910
+ const childIds = s.childIds.filter((id) => placedById.has(id));
2911
+ if (childIds.length === 0) continue;
2912
+ const xs = childIds.map(cxOf);
2913
+ const center = (Math.min(...xs) + Math.max(...xs)) / 2;
2914
+ matingBarCenter.set(s.matingId, center);
2915
+ }
2916
+ const matingMidpoint = /* @__PURE__ */ new Map();
2917
+ const bridgeDipY = (slot) => BRIDGE_DIP_GAP + slot * BRIDGE_DIP_STEP;
2918
+ for (const m of matings) {
2919
+ const [aId, bId] = partnersOf(m);
2920
+ const ax = cxOf(aId);
2921
+ const bx = cxOf(bId);
2922
+ const ay = cyOf(aId);
2923
+ const by = cyOf(bId);
2924
+ const leftId = ax <= bx ? aId : bId;
2925
+ const rightId = ax <= bx ? bId : aId;
2926
+ const lx = cxOf(leftId) + GLYPH_HALF;
2927
+ const rx = cxOf(rightId) - GLYPH_HALF;
2928
+ const title = m.consanguineous ? titleLabels.consanguineous : titleLabels.mating;
2929
+ const bridgeSlot = matingBridgeSlot.get(m.id) ?? 0;
2930
+ if (ay === by && bridgeSlot >= 1) {
2931
+ const hubCx = cxOf(leftId);
2932
+ const spouseCx = cxOf(rightId);
2933
+ const glyphBottom = ay + GLYPH_HALF;
2934
+ const dipY = glyphBottom + bridgeDipY(bridgeSlot);
2935
+ const stubOff = Math.min(bridgeSlot * BRIDGE_STUB_X_STEP, GLYPH_HALF - 4);
2936
+ const hubStubX = hubCx + stubOff;
2937
+ const spouseStubX = spouseCx - stubOff;
2938
+ elements.push({
2939
+ edgeId: PED_MATING_ID_BASE + m.id,
2940
+ kind: "mating-elbow",
2941
+ points: [
2942
+ { x: round5(hubStubX), y: round5(glyphBottom) },
2943
+ { x: round5(hubStubX), y: round5(dipY) },
2944
+ { x: round5(spouseStubX), y: round5(dipY) },
2945
+ { x: round5(spouseStubX), y: round5(glyphBottom) }
2946
+ ],
2947
+ consanguineous: m.consanguineous,
2948
+ title
2949
+ });
2950
+ matingMidpoint.set(m.id, { x: round5((hubStubX + spouseStubX) / 2), y: round5(dipY) });
2951
+ } else if (ay === by) {
2952
+ const y = ay;
2953
+ elements.push({
2954
+ edgeId: PED_MATING_ID_BASE + m.id,
2955
+ kind: "mating",
2956
+ points: [
2957
+ { x: round5(lx), y: round5(y) },
2958
+ { x: round5(rx), y: round5(y) }
2959
+ ],
2960
+ consanguineous: m.consanguineous,
2961
+ title
2962
+ });
2963
+ const midX = (lx + rx) / 2;
2964
+ const channelLeft = cxOf(leftId) + nodeHalfWidth(individualById.get(leftId));
2965
+ const channelRight = cxOf(rightId) - nodeHalfWidth(individualById.get(rightId));
2966
+ const barCenter = matingBarCenter.get(m.id);
2967
+ const originX = barCenter !== void 0 && barCenter >= channelLeft && barCenter <= channelRight ? barCenter : midX;
2968
+ matingMidpoint.set(m.id, { x: round5(originX), y: round5(y) });
2969
+ } else {
2970
+ const upId = ay <= by ? aId : bId;
2971
+ const downId = ay <= by ? bId : aId;
2972
+ const upX = cxOf(upId);
2973
+ const upY = cyOf(upId);
2974
+ const downX = cxOf(downId);
2975
+ const downY = cyOf(downId);
2976
+ if (childToSibship.has(downId)) {
2977
+ const upRow = rowOfGeneration.get(individualById.get(upId).generation);
2978
+ const laneY = rowTop[upRow] + rowHeight[upRow] + CORRIDOR2 / 4 + 4;
2979
+ const downRight = downX >= upX;
2980
+ const upExitX = upX + (downRight ? -1 : 1) * (nodeHalfWidth(individualById.get(upId)) + 6);
2981
+ const downFarX = downX + (downRight ? 1 : -1) * (GLYPH_HALF + H_GAP2 / 2);
2982
+ const downSideX = downX + (downRight ? 1 : -1) * GLYPH_HALF;
2983
+ elements.push({
2984
+ edgeId: PED_MATING_ID_BASE + m.id,
2985
+ kind: "mating-elbow",
2986
+ points: [
2987
+ { x: round5(upX + (downRight ? -GLYPH_HALF : GLYPH_HALF)), y: round5(upY) },
2988
+ { x: round5(upExitX), y: round5(upY) },
2989
+ { x: round5(upExitX), y: round5(laneY) },
2990
+ { x: round5(downFarX), y: round5(laneY) },
2991
+ { x: round5(downFarX), y: round5(downY) },
2992
+ { x: round5(downSideX), y: round5(downY) }
2993
+ ],
2994
+ consanguineous: m.consanguineous,
2995
+ title
2996
+ });
2997
+ matingMidpoint.set(m.id, { x: round5(downFarX), y: round5(downY) });
2998
+ } else {
2999
+ const elbowX = (upX + downX) / 2;
3000
+ elements.push({
3001
+ edgeId: PED_MATING_ID_BASE + m.id,
3002
+ kind: "mating-elbow",
3003
+ points: [
3004
+ { x: round5(upX + (downX >= upX ? GLYPH_HALF : -GLYPH_HALF)), y: round5(upY) },
3005
+ { x: round5(elbowX), y: round5(upY) },
3006
+ { x: round5(elbowX), y: round5(downY) },
3007
+ { x: round5(downX + (upX >= downX ? GLYPH_HALF : -GLYPH_HALF)), y: round5(downY) }
3008
+ ],
3009
+ consanguineous: m.consanguineous,
3010
+ title
3011
+ });
3012
+ matingMidpoint.set(m.id, { x: round5(elbowX), y: round5(downY) });
3013
+ }
3014
+ }
3015
+ }
3016
+ const twinZygositiesUsedSet = /* @__PURE__ */ new Set();
3017
+ const unknownTwinJunctions = [];
3018
+ const bendLaneByRow = /* @__PURE__ */ new Map();
3019
+ for (const s of sibships) {
3020
+ const childIds = s.childIds.filter((id) => individualById.has(id));
3021
+ if (childIds.length === 0) continue;
3022
+ const mid = matingMidpoint.get(s.matingId);
3023
+ const childRow = rowOfGeneration.get(individualById.get(childIds[0]).generation);
3024
+ const barY = rowTop[childRow] - CORRIDOR2 / 2;
3025
+ const lane = bendLaneByRow.get(childRow) ?? 0;
3026
+ bendLaneByRow.set(childRow, lane + 1);
3027
+ const childXs = childIds.map(cxOf);
3028
+ const barLeft = Math.min(...childXs);
3029
+ const barRight = Math.max(...childXs);
3030
+ if (mid !== void 0) {
3031
+ const barCenterX = (barLeft + barRight) / 2;
3032
+ const bendY = barY - 2 - lane % 3 * 3;
3033
+ const descentPoints = Math.abs(mid.x - barCenterX) < 0.01 ? [
3034
+ { x: round5(mid.x), y: round5(mid.y) },
3035
+ { x: round5(mid.x), y: round5(barY) }
3036
+ ] : [
3037
+ { x: round5(mid.x), y: round5(mid.y) },
3038
+ { x: round5(mid.x), y: round5(bendY) },
3039
+ { x: round5(barCenterX), y: round5(bendY) },
3040
+ { x: round5(barCenterX), y: round5(barY) }
3041
+ ];
3042
+ elements.push({
3043
+ edgeId: PED_DESCENT_ID_BASE + s.id,
3044
+ kind: "descent",
3045
+ points: descentPoints,
3046
+ consanguineous: false,
3047
+ title: titleLabels.sibship
3048
+ });
3049
+ }
3050
+ if (childIds.length > 1) {
3051
+ elements.push({
3052
+ edgeId: PED_SIBBAR_ID_BASE + s.id,
3053
+ kind: "sibship-bar",
3054
+ points: [
3055
+ { x: round5(barLeft), y: round5(barY) },
3056
+ { x: round5(barRight), y: round5(barY) }
3057
+ ],
3058
+ consanguineous: false,
3059
+ title: titleLabels.sibship
3060
+ });
3061
+ }
3062
+ const twinGroupOfChild = /* @__PURE__ */ new Map();
3063
+ s.twinGroups.forEach((tg, ordinal) => {
3064
+ const members = tg.childIds.filter((id) => childIds.includes(id));
3065
+ for (const id of members) twinGroupOfChild.set(id, { ordinal, zygosity: tg.zygosity, members });
3066
+ });
3067
+ const emittedTwinOrdinals = /* @__PURE__ */ new Set();
3068
+ for (const childId of childIds) {
3069
+ const cx = cxOf(childId);
3070
+ const childTop = cyOf(childId) - PED_GLYPH / 2;
3071
+ const tg = twinGroupOfChild.get(childId);
3072
+ if (tg === void 0) {
3073
+ elements.push({
3074
+ edgeId: PED_RISER_ID_BASE + childId,
3075
+ kind: "riser",
3076
+ points: [
3077
+ { x: round5(cx), y: round5(barY) },
3078
+ { x: round5(cx), y: round5(childTop) }
3079
+ ],
3080
+ consanguineous: false,
3081
+ title: titleLabels.sibship
3082
+ });
3083
+ continue;
3084
+ }
3085
+ const memberXs = tg.members.map(cxOf);
3086
+ const junctionX = (Math.min(...memberXs) + Math.max(...memberXs)) / 2;
3087
+ const junctionY = barY + TWIN_JUNCTION_DROP;
3088
+ twinZygositiesUsedSet.add(tg.zygosity);
3089
+ if (!emittedTwinOrdinals.has(tg.ordinal)) {
3090
+ emittedTwinOrdinals.add(tg.ordinal);
3091
+ if (tg.zygosity === "unknown") {
3092
+ unknownTwinJunctions.push({ x: round5(junctionX), y: round5(junctionY) });
3093
+ }
3094
+ elements.push({
3095
+ edgeId: PED_RISER_ID_BASE + childId,
3096
+ // anchored on the first member for a stable id
3097
+ kind: "riser",
3098
+ points: [
3099
+ { x: round5(junctionX), y: round5(barY) },
3100
+ { x: round5(junctionX), y: round5(junctionY) }
3101
+ ],
3102
+ consanguineous: false,
3103
+ title: titleLabels.twins[tg.zygosity]
3104
+ });
3105
+ if (tg.zygosity === "mz" && memberXs.length >= 2) {
3106
+ const tieY = junctionY + 6;
3107
+ elements.push({
3108
+ edgeId: PED_TWINBAR_ID_BASE + s.id * 100 + tg.ordinal,
3109
+ kind: "twin-bar",
3110
+ points: [
3111
+ { x: round5(Math.min(...memberXs)), y: round5(tieY) },
3112
+ { x: round5(Math.max(...memberXs)), y: round5(tieY) }
3113
+ ],
3114
+ consanguineous: false,
3115
+ title: titleLabels.twins.mz
3116
+ });
3117
+ }
3118
+ }
3119
+ const horizontalThenDown = Math.abs(cx - junctionX) < 0.01 ? [
3120
+ { x: round5(cx), y: round5(junctionY) },
3121
+ { x: round5(cx), y: round5(childTop) }
3122
+ ] : [
3123
+ { x: round5(junctionX), y: round5(junctionY) },
3124
+ { x: round5(cx), y: round5(junctionY) },
3125
+ { x: round5(cx), y: round5(childTop) }
3126
+ ];
3127
+ elements.push({
3128
+ edgeId: PED_RISER_ID_BASE + childId,
3129
+ kind: "riser",
3130
+ points: horizontalThenDown,
3131
+ consanguineous: false,
3132
+ title: titleLabels.twins[tg.zygosity]
3133
+ });
3134
+ }
3135
+ }
3136
+ let maxX = PADDING5 * 2;
3137
+ let maxY = PADDING5 * 2;
3138
+ for (const n of nodes) {
3139
+ const labelW = n.labelLines.reduce((m, l) => Math.max(m, estimateTextWidth(l, PED_LABEL_FONT)), 0);
3140
+ const half = Math.max(PED_GLYPH, labelW) / 2;
3141
+ maxX = Math.max(maxX, n.cx + half + PADDING5);
3142
+ const labelBottom = n.labelTop + (n.labelLines.length + 1) * PED_LABEL_LINE_H;
3143
+ maxY = Math.max(maxY, labelBottom + PADDING5);
3144
+ }
3145
+ for (const el of elements) {
3146
+ for (const p of el.points) {
3147
+ maxX = Math.max(maxX, p.x + PADDING5);
3148
+ maxY = Math.max(maxY, p.y + PADDING5);
3149
+ }
3150
+ }
3151
+ const generations = [];
3152
+ for (let row = 0; row < rowCount; row++) {
3153
+ generations.push({ roman: romanNumeral(row + 1), y: round5(glyphCyOfRow(row)) });
3154
+ }
3155
+ const ZYGOSITY_ORDER = ["mz", "dz", "unknown"];
3156
+ const twinZygositiesUsed = ZYGOSITY_ORDER.filter((z) => twinZygositiesUsedSet.has(z));
3157
+ return {
3158
+ width: Math.ceil(maxX),
3159
+ height: Math.ceil(maxY),
3160
+ nodes,
3161
+ elements,
3162
+ generations,
3163
+ conditionFills,
3164
+ twinZygositiesUsed,
3165
+ unknownTwinJunctions,
3166
+ isolatedIndividualIds
3167
+ };
3168
+ }
3169
+
3170
+ // src/pedigree/svg.ts
3171
+ var GLYPH_STROKE3 = "#52525b";
3172
+ var LABEL_FILL4 = "#3f3f46";
3173
+ var EDGE_INK5 = "#71717a";
3174
+ var GLYPH_ATTRS2 = `fill="transparent" stroke="${GLYPH_STROKE3}" stroke-width="2"`;
3175
+ var CONSANG_GAP = 3;
3176
+ var ZYGOSITIES = ["mz", "dz", "unknown"];
3177
+ var round6 = (n) => Math.round(n * 100) / 100;
3178
+ function glyphOutline(shape, cx, cy, half) {
3179
+ if (shape === "square") {
3180
+ return `<rect x="${round6(cx - half)}" y="${round6(cy - half)}" width="${half * 2}" height="${half * 2}" ${GLYPH_ATTRS2}/>`;
3181
+ }
3182
+ if (shape === "circle") {
3183
+ return `<circle cx="${cx}" cy="${cy}" r="${half}" ${GLYPH_ATTRS2}/>`;
3184
+ }
3185
+ return `<polygon points="${cx},${round6(cy - half)} ${round6(cx + half)},${cy} ${cx},${round6(cy + half)} ${round6(cx - half)},${cy}" ${GLYPH_ATTRS2}/>`;
3186
+ }
3187
+ function glyphVerticalExtentAt(shape, cy, half, dx) {
3188
+ const ax = Math.min(Math.abs(dx), half);
3189
+ if (shape === "square") return [cy - half, cy + half];
3190
+ if (shape === "circle") {
3191
+ const h2 = Math.sqrt(Math.max(0, half * half - ax * ax));
3192
+ return [cy - h2, cy + h2];
3193
+ }
3194
+ const h = half - ax;
3195
+ return [cy - h, cy + h];
3196
+ }
3197
+ function fillPartitions(n, half, inkByCondition) {
3198
+ const ids = n.affectedBy;
3199
+ if (ids.length === 0) return "";
3200
+ const cx = n.cx;
3201
+ const cy = n.cy;
3202
+ const sliceW = half * 2 / ids.length;
3203
+ return ids.map((id, i) => {
3204
+ const left = cx - half + i * sliceW;
3205
+ const SAMPLES = 8;
3206
+ const top = [];
3207
+ const bottom = [];
3208
+ for (let s = 0; s <= SAMPLES; s++) {
3209
+ const x = left + sliceW * s / SAMPLES;
3210
+ const [yt, yb] = glyphVerticalExtentAt(n.shape, cy, half, x - cx);
3211
+ top.push(`${round6(x)},${round6(yt)}`);
3212
+ bottom.push(`${round6(x)},${round6(yb)}`);
3213
+ }
3214
+ const pts = [...top, ...bottom.reverse()].join(" ");
3215
+ const ink = inkByCondition.get(id) ?? GLYPH_STROKE3;
3216
+ return `<polygon points="${pts}" fill="${ink}" stroke="none"/>`;
3217
+ }).join("");
3218
+ }
3219
+ function carrierDot(n) {
3220
+ if (!n.carrier || n.affectedBy.length > 0) return "";
3221
+ return `<circle cx="${n.cx}" cy="${n.cy}" r="4" fill="${GLYPH_STROKE3}" stroke="none"/>`;
3222
+ }
3223
+ function deceasedSlash(n, half) {
3224
+ if (!n.deceased) return "";
3225
+ const ext = half + 4;
3226
+ return `<line x1="${round6(n.cx - ext)}" y1="${round6(n.cy - ext)}" x2="${round6(n.cx + ext)}" y2="${round6(n.cy + ext)}" stroke="${GLYPH_STROKE3}" stroke-width="2"/>`;
3227
+ }
3228
+ function probandArrow(n, half) {
3229
+ if (n.role === null) return "";
3230
+ const tipX = round6(n.cx - half);
3231
+ const tipY = round6(n.cy + half);
3232
+ const tailX = round6(tipX - 16);
3233
+ const tailY = round6(tipY + 16);
3234
+ const filled = n.role === "proband";
3235
+ const fill = filled ? GLYPH_STROKE3 : "transparent";
3236
+ const shaft = `<line x1="${tailX}" y1="${tailY}" x2="${tipX}" y2="${tipY}" stroke="${GLYPH_STROKE3}" stroke-width="2"/>`;
3237
+ const head = `<polygon points="${tipX},${tipY} ${round6(tipX - 8)},${round6(tipY + 2)} ${round6(tipX - 2)},${round6(tipY + 8)}" fill="${fill}" stroke="${GLYPH_STROKE3}" stroke-width="1.5"/>`;
3238
+ return shaft + head;
3239
+ }
3240
+ function stillbirthMark(n, half) {
3241
+ if (!n.stillbirth) return "";
3242
+ const y = round6(n.cy + half + PED_ADDRESS_FONT);
3243
+ return `<text x="${round6(n.cx - half - 2)}" y="${y}" text-anchor="end" font-family="${FONT_FAMILY}" font-size="${PED_ADDRESS_FONT}" font-weight="bold" fill="${LABEL_FILL4}">SB</text>`;
3244
+ }
3245
+ function nodeSvg2(n, inkByCondition) {
3246
+ const half = n.size / 2;
3247
+ const pieces = [
3248
+ `<title>${xmlEscape(n.title)}</title>`,
3249
+ // Fill partitions sit UNDER the outline so the border stays crisp.
3250
+ fillPartitions(n, half, inkByCondition),
3251
+ glyphOutline(n.shape, n.cx, n.cy, half),
3252
+ carrierDot(n),
3253
+ deceasedSlash(n, half),
3254
+ probandArrow(n, half),
3255
+ stillbirthMark(n, half)
3256
+ ];
3257
+ const addressY = round6(n.labelTop + PED_ADDRESS_FONT);
3258
+ pieces.push(
3259
+ `<text x="${n.cx}" y="${addressY}" text-anchor="middle" font-family="${FONT_FAMILY}" font-size="${PED_ADDRESS_FONT}" fill="${LABEL_FILL4}">${xmlEscape(n.addressLabel)}</text>`
3260
+ );
3261
+ if (n.labelLines.length > 0) {
3262
+ const firstBaseline = round6(n.labelTop + PED_LABEL_LINE_H + 10);
3263
+ const tspans = n.labelLines.map((line, i) => `<tspan x="${n.cx}" y="${round6(firstBaseline + i * PED_LABEL_LINE_H)}">${xmlEscape(line)}</tspan>`).join("");
3264
+ pieces.push(
3265
+ `<text text-anchor="middle" font-family="${FONT_FAMILY}" font-size="${PED_LABEL_FONT}" fill="${LABEL_FILL4}">${tspans}</text>`
3266
+ );
3267
+ }
3268
+ return `<g data-individual-id="p${n.individualId}">${pieces.join("")}</g>`;
3269
+ }
3270
+ function pathData3(points) {
3271
+ return points.map((p, i) => `${i === 0 ? "M" : "L"} ${p.x} ${p.y}`).join(" ");
3272
+ }
3273
+ function elementSvg3(el) {
3274
+ const pts = el.points;
3275
+ const title = `<title>${xmlEscape(el.title)}</title>`;
3276
+ const stroke = `stroke="${EDGE_INK5}" stroke-width="1.5" stroke-opacity="0.75"`;
3277
+ const draw = (offsetY) => {
3278
+ const shifted = pts.map((p) => ({ x: p.x, y: round6(p.y + offsetY) }));
3279
+ if (shifted.length === 2) {
3280
+ return `<line x1="${shifted[0].x}" y1="${shifted[0].y}" x2="${shifted[1].x}" y2="${shifted[1].y}" ${stroke}/>`;
3281
+ }
3282
+ return `<path d="${pathData3(shifted)}" fill="none" ${stroke}/>`;
3283
+ };
3284
+ const body = el.consanguineous && (el.kind === "mating" || el.kind === "mating-elbow") ? draw(-CONSANG_GAP / 2) + draw(CONSANG_GAP / 2) : draw(0);
3285
+ return `<g data-edge-id="${el.edgeId}">${title}${body}</g>`;
3286
+ }
3287
+ function unknownTwinMarks(layout) {
3288
+ return layout.unknownTwinJunctions.map(
3289
+ (p) => `<text x="${round6(p.x + 6)}" y="${round6(p.y + 4)}" font-family="${FONT_FAMILY}" font-size="12" font-weight="bold" fill="${LABEL_FILL4}">?</text>`
3290
+ ).join("");
3291
+ }
3292
+ var MINI_ATTRS2 = `fill="transparent" stroke="${GLYPH_STROKE3}" stroke-width="1.5"`;
3293
+ function miniShapeSwatch(shape, x, y) {
3294
+ const cx = round6(x + LEGEND_SWATCH_W / 2);
3295
+ if (shape === "square") return `<rect x="${round6(cx - 6)}" y="${round6(y - 6)}" width="12" height="12" ${MINI_ATTRS2}/>`;
3296
+ if (shape === "circle") return `<circle cx="${cx}" cy="${y}" r="6" ${MINI_ATTRS2}/>`;
3297
+ return `<polygon points="${cx},${round6(y - 7)} ${round6(cx + 7)},${y} ${cx},${round6(y + 7)} ${round6(cx - 7)},${y}" ${MINI_ATTRS2}/>`;
3298
+ }
3299
+ function miniSwatchCircle(filled, ink, x, y) {
3300
+ const cx = round6(x + LEGEND_SWATCH_W / 2);
3301
+ return `<circle cx="${cx}" cy="${y}" r="6" fill="${filled ? ink : "transparent"}" stroke="${GLYPH_STROKE3}" stroke-width="1.5"/>`;
3302
+ }
3303
+ function miniCarrierSwatch(x, y) {
3304
+ const cx = round6(x + LEGEND_SWATCH_W / 2);
3305
+ return `<circle cx="${cx}" cy="${y}" r="6" ${MINI_ATTRS2}/><circle cx="${cx}" cy="${y}" r="2" fill="${GLYPH_STROKE3}" stroke="none"/>`;
3306
+ }
3307
+ function miniDeceasedSwatch(x, y) {
3308
+ const cx = round6(x + LEGEND_SWATCH_W / 2);
3309
+ return `<circle cx="${cx}" cy="${y}" r="6" ${MINI_ATTRS2}/><line x1="${round6(cx - 7)}" y1="${round6(y - 7)}" x2="${round6(cx + 7)}" y2="${round6(y + 7)}" stroke="${GLYPH_STROKE3}" stroke-width="1.5"/>`;
3310
+ }
3311
+ function miniArrowSwatch(filled, x, y) {
3312
+ const cx = round6(x + LEGEND_SWATCH_W / 2);
3313
+ const tipX = round6(cx - 2);
3314
+ const tipY = round6(y + 2);
3315
+ return `<line x1="${round6(tipX - 8)}" y1="${round6(tipY + 8)}" x2="${tipX}" y2="${tipY}" stroke="${GLYPH_STROKE3}" stroke-width="1.5"/><polygon points="${tipX},${tipY} ${round6(tipX - 5)},${round6(tipY + 1)} ${round6(tipX - 1)},${round6(tipY + 5)}" fill="${filled ? GLYPH_STROKE3 : "transparent"}" stroke="${GLYPH_STROKE3}" stroke-width="1"/>`;
3316
+ }
3317
+ function miniConsanguineousSwatch(x, y) {
3318
+ const x1 = round6(x + 2);
3319
+ const x2 = round6(x + LEGEND_SWATCH_W - 2);
3320
+ return `<line x1="${x1}" y1="${round6(y - 1.5)}" x2="${x2}" y2="${round6(y - 1.5)}" stroke="${EDGE_INK5}" stroke-width="1.5"/><line x1="${x1}" y1="${round6(y + 1.5)}" x2="${x2}" y2="${round6(y + 1.5)}" stroke="${EDGE_INK5}" stroke-width="1.5"/>`;
3321
+ }
3322
+ function miniTwinSwatch(zygosity, x, y) {
3323
+ const cx = round6(x + LEGEND_SWATCH_W / 2);
3324
+ const apexY = round6(y - 6);
3325
+ const baseY = round6(y + 6);
3326
+ const left = round6(cx - 6);
3327
+ const right = round6(cx + 6);
3328
+ const stub = `<line x1="${cx}" y1="${apexY}" x2="${cx}" y2="${y}" stroke="${EDGE_INK5}" stroke-width="1.5"/><line x1="${left}" y1="${y}" x2="${right}" y2="${y}" stroke="${EDGE_INK5}" stroke-width="1.5"/><line x1="${left}" y1="${y}" x2="${left}" y2="${baseY}" stroke="${EDGE_INK5}" stroke-width="1.5"/><line x1="${right}" y1="${y}" x2="${right}" y2="${baseY}" stroke="${EDGE_INK5}" stroke-width="1.5"/>`;
3329
+ if (zygosity === "mz") {
3330
+ return stub + `<line x1="${left}" y1="${round6(y + 3)}" x2="${right}" y2="${round6(y + 3)}" stroke="${EDGE_INK5}" stroke-width="1.5"/>`;
3331
+ }
3332
+ if (zygosity === "unknown") {
3333
+ return stub + `<text x="${round6(cx + 8)}" y="${round6(y + 3)}" font-family="${FONT_FAMILY}" font-size="9" fill="${LABEL_FILL4}">?</text>`;
3334
+ }
3335
+ return stub;
3336
+ }
3337
+ function pedigreeLayoutSvg(layout, opts = {}) {
3338
+ const labels = opts.labels ?? PEDIGREE_SVG_LABELS_EN;
3339
+ const inkByCondition = new Map(layout.conditionFills.map((c) => [c.id, c.ink]));
3340
+ const parts = [];
3341
+ for (const el of layout.elements) parts.push(elementSvg3(el));
3342
+ parts.push(unknownTwinMarks(layout));
3343
+ for (const n of layout.nodes) parts.push(nodeSvg2(n, inkByCondition));
3344
+ for (const g of layout.generations) {
3345
+ parts.push(
3346
+ `<text x="${round6(16)}" y="${round6(g.y + PED_LABEL_FONT * 0.32)}" text-anchor="middle" font-family="${FONT_FAMILY}" font-size="${PED_LABEL_FONT}" font-weight="bold" fill="${LABEL_FILL4}">${xmlEscape(g.roman)}</text>`
3347
+ );
3348
+ }
3349
+ let width = layout.width;
3350
+ let height = layout.height;
3351
+ if (opts.legend !== false && layout.nodes.length > 0) {
3352
+ const entries = [];
3353
+ const shapesUsed = new Set(layout.nodes.map((n) => n.shape));
3354
+ for (const shape of ["square", "circle", "diamond"]) {
3355
+ if (!shapesUsed.has(shape)) continue;
3356
+ entries.push({ swatch: (x, y) => miniShapeSwatch(shape, x, y), label: labels.shapes[shape] });
3357
+ }
3358
+ for (const c of layout.conditionFills) {
3359
+ entries.push({ swatch: (x, y) => miniSwatchCircle(true, c.ink, x, y), label: c.label });
3360
+ }
3361
+ if (layout.nodes.some((n) => n.affectedBy.length === 0)) {
3362
+ entries.push({ swatch: (x, y) => miniSwatchCircle(false, GLYPH_STROKE3, x, y), label: labels.unaffected });
3363
+ }
3364
+ if (layout.nodes.some((n) => n.carrier && n.affectedBy.length === 0)) {
3365
+ entries.push({ swatch: miniCarrierSwatch, label: labels.carrier });
3366
+ }
3367
+ if (layout.nodes.some((n) => n.deceased)) {
3368
+ entries.push({ swatch: miniDeceasedSwatch, label: labels.deceased });
3369
+ }
3370
+ if (layout.nodes.some((n) => n.role === "proband")) {
3371
+ entries.push({ swatch: (x, y) => miniArrowSwatch(true, x, y), label: labels.proband });
3372
+ }
3373
+ if (layout.nodes.some((n) => n.role === "consultand")) {
3374
+ entries.push({ swatch: (x, y) => miniArrowSwatch(false, x, y), label: labels.consultand });
3375
+ }
3376
+ if (layout.nodes.some((n) => n.stillbirth)) {
3377
+ entries.push({ swatch: () => "", label: labels.stillbirth });
3378
+ }
3379
+ if (layout.elements.some((el) => el.consanguineous)) {
3380
+ entries.push({ swatch: miniConsanguineousSwatch, label: labels.consanguineous });
3381
+ }
3382
+ const twinsUsed = new Set(layout.twinZygositiesUsed);
3383
+ for (const z of ZYGOSITIES) {
3384
+ if (!twinsUsed.has(z)) continue;
3385
+ entries.push({ swatch: (x, y) => miniTwinSwatch(z, x, y), label: labels.twins[z] });
3386
+ }
3387
+ if (layout.isolatedIndividualIds.length > 0) {
3388
+ entries.push({ swatch: () => "", label: labels.isolated });
3389
+ }
3390
+ const block = legendBlock(entries, layout.height);
3391
+ if (block.svg !== "") {
3392
+ parts.push(block.svg);
3393
+ width = Math.max(width, block.width);
3394
+ height = block.height;
3395
+ }
3396
+ }
3397
+ const w = Math.ceil(width);
3398
+ const h = Math.ceil(height);
3399
+ return `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${w} ${h}" width="${w}" height="${h}" role="img" aria-label="${xmlEscape(labels.ariaLabel)}">` + parts.join("") + `</svg>`;
3400
+ }
3401
+
3402
+ // src/pedigree/render.ts
3403
+ function pedigreeSvg(input, opts = {}) {
3404
+ const layout = computePedigreeLayout(input, {
3405
+ ...opts.maxLabelChars !== void 0 ? { maxLabelChars: opts.maxLabelChars } : {},
3406
+ ...opts.titleLabels !== void 0 ? { titleLabels: opts.titleLabels } : {}
3407
+ });
3408
+ const svg = pedigreeLayoutSvg(layout, {
3409
+ ...opts.legend === false ? { legend: false } : {},
3410
+ ...opts.svgLabels !== void 0 ? { labels: opts.svgLabels } : {}
3411
+ });
3412
+ return { svg, layout };
3413
+ }
3414
+
3415
+ // src/phylo/labels.ts
3416
+ var PHYLO_TITLE_LABELS_EN = {
3417
+ branchLength: "branch length",
3418
+ support: "support",
3419
+ clade: "clade",
3420
+ tip: "tip",
3421
+ root: "root"
3422
+ };
3423
+ var PHYLO_SVG_LABELS_EN = {
3424
+ support: "Support value (bootstrap/posterior)",
3425
+ scaleBar: "Scale: substitutions per site",
3426
+ alignedTip: "Aligned tip (true position dotted)",
3427
+ ariaLabel: {
3428
+ cladogram: "Phylogenetic tree (cladogram)",
3429
+ phylogram: "Phylogenetic tree (phylogram)"
3430
+ }
3431
+ };
3432
+
3433
+ // src/phylo/validate.ts
3434
+ var PhyloValidationError = class extends Error {
3435
+ issues;
3436
+ constructor(issues) {
3437
+ super(`invalid phylo tree: ${issues.map((i) => i.message).join("; ")}`);
3438
+ this.name = "PhyloValidationError";
3439
+ this.issues = issues;
3440
+ }
3441
+ };
3442
+ function sortIssues3(issues) {
3443
+ const unique = /* @__PURE__ */ new Map();
3444
+ for (const issue of issues) unique.set(`${issue.code} ${issue.message}`, issue);
3445
+ return [...unique.values()].sort(
3446
+ (a, b) => a.code !== b.code ? a.code < b.code ? -1 : 1 : a.message < b.message ? -1 : a.message > b.message ? 1 : 0
3447
+ );
3448
+ }
3449
+ function phyloIssues(input) {
3450
+ if (input.nodes.length === 0 && input.edges.length === 0) return [];
3451
+ const issues = [];
3452
+ const push = (code, message) => {
3453
+ issues.push({ code, message });
3454
+ };
3455
+ const nodeById = /* @__PURE__ */ new Map();
3456
+ const dupNodeIds = /* @__PURE__ */ new Set();
3457
+ for (const n of input.nodes) {
3458
+ if (nodeById.has(n.id)) dupNodeIds.add(n.id);
3459
+ else nodeById.set(n.id, n);
3460
+ }
3461
+ for (const id of [...dupNodeIds].sort((a, b) => a - b)) {
3462
+ push("duplicate-id", `duplicate node id ${id}`);
3463
+ }
3464
+ const dupEdgeIds = /* @__PURE__ */ new Set();
3465
+ const seenEdgeIds = /* @__PURE__ */ new Set();
3466
+ for (const e of input.edges) {
3467
+ if (seenEdgeIds.has(e.id)) dupEdgeIds.add(e.id);
3468
+ else seenEdgeIds.add(e.id);
3469
+ }
3470
+ for (const id of [...dupEdgeIds].sort((a, b) => a - b)) {
3471
+ push("duplicate-id", `duplicate edge id ${id}`);
3472
+ }
3473
+ if (issues.length > 0) {
3474
+ return sortIssues3(issues);
3475
+ }
3476
+ if (!nodeById.has(input.rootId)) {
3477
+ push("unknown-root", `rootId ${input.rootId} is not a declared node`);
3478
+ }
3479
+ const incomingByChild = /* @__PURE__ */ new Map();
3480
+ for (const e of [...input.edges].sort((a, b) => a.id - b.id)) {
3481
+ const knownParent = nodeById.has(e.parentId);
3482
+ const knownChild = nodeById.has(e.childId);
3483
+ if (!knownParent) push("unknown-endpoint", `edge ${e.id} parentId ${e.parentId} is not a declared node`);
3484
+ if (!knownChild) push("unknown-endpoint", `edge ${e.id} childId ${e.childId} is not a declared node`);
3485
+ if (e.length !== null && (!Number.isFinite(e.length) || e.length < 0)) {
3486
+ push("negative-length", `edge ${e.id} has non-finite or negative length ${e.length}`);
3487
+ }
3488
+ if (knownParent && knownChild) {
3489
+ const arr = incomingByChild.get(e.childId) ?? [];
3490
+ arr.push(e.parentId);
3491
+ incomingByChild.set(e.childId, arr);
3492
+ }
3493
+ }
3494
+ const childIdsWithEdges = /* @__PURE__ */ new Set();
3495
+ for (const e of input.edges) childIdsWithEdges.add(e.childId);
3496
+ for (const [childId, parents] of [...incomingByChild.entries()].sort((a, b) => a[0] - b[0])) {
3497
+ if (parents.length > 1) {
3498
+ push("multiple-parents", `node ${childId} has ${parents.length} parents`);
3499
+ }
3500
+ }
3501
+ const hasChildren = /* @__PURE__ */ new Set();
3502
+ for (const e of input.edges) {
3503
+ if (nodeById.has(e.parentId)) hasChildren.add(e.parentId);
3504
+ }
3505
+ for (const n of [...nodeById.values()].sort((a, b) => a.id - b.id)) {
3506
+ if (n.isTip === true && hasChildren.has(n.id)) {
3507
+ push("tip-with-children", `node ${n.id} is declared a tip but has children`);
3508
+ } else if (n.isTip === false && !hasChildren.has(n.id)) {
3509
+ push("tip-with-children", `node ${n.id} is declared internal but has no children`);
3510
+ }
3511
+ }
3512
+ const GRAPH_BLOCKING = /* @__PURE__ */ new Set([
3513
+ "duplicate-id",
3514
+ "unknown-endpoint",
3515
+ "unknown-root",
3516
+ "multiple-parents"
3517
+ ]);
3518
+ if (!issues.some((i) => GRAPH_BLOCKING.has(i.code))) {
3519
+ const childrenOf = /* @__PURE__ */ new Map();
3520
+ for (const e of [...input.edges].sort((a, b) => a.id - b.id)) {
3521
+ const arr = childrenOf.get(e.parentId) ?? [];
3522
+ arr.push(e.childId);
3523
+ childrenOf.set(e.parentId, arr);
3524
+ }
3525
+ const color = /* @__PURE__ */ new Map();
3526
+ const seenCycles = /* @__PURE__ */ new Set();
3527
+ const dfs = (start) => {
3528
+ const stack = [{ id: start, nextChild: 0 }];
3529
+ color.set(start, 1);
3530
+ while (stack.length > 0) {
3531
+ const frame = stack[stack.length - 1];
3532
+ const children = childrenOf.get(frame.id) ?? [];
3533
+ if (frame.nextChild >= children.length) {
3534
+ color.set(frame.id, 2);
3535
+ stack.pop();
3536
+ continue;
3537
+ }
3538
+ const child = children[frame.nextChild];
3539
+ frame.nextChild += 1;
3540
+ const c = color.get(child) ?? 0;
3541
+ if (c === 1) {
3542
+ const from = stack.findIndex((f) => f.id === child);
3543
+ const cycle = stack.slice(from).map((f) => f.id);
3544
+ const minIdx = cycle.indexOf(Math.min(...cycle));
3545
+ const rotated = [...cycle.slice(minIdx), ...cycle.slice(0, minIdx)];
3546
+ const key = rotated.join(">");
3547
+ if (!seenCycles.has(key)) {
3548
+ seenCycles.add(key);
3549
+ push("cycle", `cycle: ${[...rotated, rotated[0]].join(" \u2192 ")}`);
3550
+ }
3551
+ } else if (c === 0) {
3552
+ color.set(child, 1);
3553
+ stack.push({ id: child, nextChild: 0 });
3554
+ }
3555
+ }
3556
+ };
3557
+ for (const n of [...nodeById.values()].sort((a, b) => a.id - b.id)) {
3558
+ if ((color.get(n.id) ?? 0) === 0) dfs(n.id);
3559
+ }
3560
+ const reachable = /* @__PURE__ */ new Set();
3561
+ const queue = [input.rootId];
3562
+ while (queue.length > 0) {
3563
+ const id = queue.shift();
3564
+ if (reachable.has(id)) continue;
3565
+ reachable.add(id);
3566
+ for (const childId of childrenOf.get(id) ?? []) queue.push(childId);
3567
+ }
3568
+ for (const n of [...nodeById.values()].sort((a, b) => a.id - b.id)) {
3569
+ if (!reachable.has(n.id)) push("disconnected-node", `node ${n.id} is unreachable from the root`);
3570
+ }
3571
+ }
3572
+ return sortIssues3(issues);
3573
+ }
3574
+ function validatePhylo(input) {
3575
+ const issues = phyloIssues(input);
3576
+ if (issues.length > 0) throw new PhyloValidationError(issues);
3577
+ }
3578
+
3579
+ // src/phylo/layout.ts
3580
+ var PHYLO_LABEL_FONT = 12;
3581
+ var PHYLO_ROW_SLOT = 22;
3582
+ var PHYLO_SUPPORT_FONT = 10;
3583
+ var PHYLO_LABEL_GAP = 8;
3584
+ var PADDING6 = 32;
3585
+ var PHYLO_LEVEL_W = 72;
3586
+ var PHYLO_TIP_R = 2.5;
3587
+ var SUPPORT_GAP = 4;
3588
+ var SCALEBAR_ZONE = 34;
3589
+ var MIN_DRAW_W = PHYLO_LEVEL_W;
3590
+ var PHYLO_ZERO_NUDGE = 1;
3591
+ var PHYLO_CLADEBAR_ID_BASE = 1e6;
3592
+ var PHYLO_BRANCH_ID_BASE = 2e6;
3593
+ var PHYLO_EXTENSION_ID_BASE = 3e6;
3594
+ var PHYLO_ROOTSTUB_ID_BASE = 4e6;
3595
+ var PHYLO_SCALEBAR_ID = 5e6;
3596
+ var round7 = (n) => Math.round(n * 100) / 100;
3597
+ function niceScaleStep(maxLen) {
3598
+ if (!(maxLen > 0)) return 0;
3599
+ const target = maxLen / 4;
3600
+ const exp = Math.floor(Math.log10(target));
3601
+ const pow = Math.pow(10, exp);
3602
+ const tol = target * 1e-9;
3603
+ for (const mult of [5, 2, 1]) {
3604
+ const step = mult * pow;
3605
+ if (step <= target + tol) return step;
3606
+ }
3607
+ return 5 * Math.pow(10, exp - 1);
3608
+ }
3609
+ function trimNumber(n) {
3610
+ if (Number.isInteger(n)) return String(n);
3611
+ return String(Number(n.toPrecision(6))).replace(/0+$/, "").replace(/\.$/, "");
3612
+ }
3613
+ function nodeTitle(node, isTip, isRoot, inLength, labels) {
3614
+ if (node.title !== void 0) return node.title;
3615
+ const role = isRoot ? labels.root : isTip ? labels.tip : labels.clade;
3616
+ const head = node.label === "" ? role : node.label;
3617
+ const parts = [head];
3618
+ if (inLength !== null) parts.push(`${labels.branchLength}: ${trimNumber(inLength)}`);
3619
+ if (!isTip && node.support !== void 0 && node.support !== null) {
3620
+ parts.push(`${labels.support}: ${trimNumber(node.support)}`);
3621
+ }
3622
+ return parts.join(" \xB7 ");
3623
+ }
3624
+ function computePhyloLayout(input, opts = {}) {
3625
+ const mode = opts.mode ?? "cladogram";
3626
+ if (input.nodes.length === 0 && input.edges.length === 0) {
3627
+ return { width: PADDING6 * 2, height: PADDING6 * 2, mode, nodes: [], elements: [], scaleBar: null, showSupport: false };
3628
+ }
3629
+ validatePhylo(input);
3630
+ const titleLabels = opts.titleLabels ?? PHYLO_TITLE_LABELS_EN;
3631
+ const alignTips = opts.alignTips ?? mode === "cladogram";
3632
+ const showSupport = opts.showSupport ?? false;
3633
+ const wantScaleBar = (opts.scaleBar ?? true) && mode === "phylogram";
3634
+ const nodeById = new Map(input.nodes.map((n) => [n.id, n]));
3635
+ const childrenOf = /* @__PURE__ */ new Map();
3636
+ const inLengthByChild = /* @__PURE__ */ new Map();
3637
+ for (const e of [...input.edges].sort((a, b) => a.id - b.id)) {
3638
+ (childrenOf.get(e.parentId) ?? childrenOf.set(e.parentId, []).get(e.parentId)).push(e.childId);
3639
+ inLengthByChild.set(e.childId, e.length);
3640
+ }
3641
+ const allWork = [];
3642
+ const build = (nodeId, depth, dist) => {
3643
+ const node = nodeById.get(nodeId);
3644
+ const inLength = inLengthByChild.get(nodeId) ?? null;
3645
+ const work = { node, children: [], depth, dist, inLength, cx: 0, cy: 0 };
3646
+ allWork.push(work);
3647
+ for (const childId of childrenOf.get(nodeId) ?? []) {
3648
+ const childLen = inLengthByChild.get(childId);
3649
+ const add = childLen === null || childLen === void 0 ? 0 : Math.max(0, childLen);
3650
+ work.children.push(build(childId, depth + 1, dist + add));
3651
+ }
3652
+ return work;
3653
+ };
3654
+ const root = build(input.rootId, 0, 0);
3655
+ const isTip = (w) => w.children.length === 0;
3656
+ const top = PADDING6;
3657
+ let tipIndex = 0;
3658
+ const assignY = (w) => {
3659
+ if (isTip(w)) {
3660
+ w.cy = top + tipIndex * PHYLO_ROW_SLOT + PHYLO_ROW_SLOT / 2;
3661
+ tipIndex += 1;
3662
+ return;
3663
+ }
3664
+ for (const c of w.children) assignY(c);
3665
+ const ys = w.children.map((c) => c.cy);
3666
+ w.cy = (Math.min(...ys) + Math.max(...ys)) / 2;
3667
+ };
3668
+ assignY(root);
3669
+ const tipCount = tipIndex;
3670
+ const maxDepth = allWork.reduce((m, w) => Math.max(m, w.depth), 0);
3671
+ const maxDist = allWork.reduce((m, w) => Math.max(m, w.dist), 0);
3672
+ const supportNodes = allWork.filter(
3673
+ (w) => !isTip(w) && w.node.support !== void 0 && w.node.support !== null
3674
+ );
3675
+ const effectiveShowSupport = showSupport && supportNodes.length > 0;
3676
+ const supportMargin = effectiveShowSupport ? supportNodes.reduce(
3677
+ (m, w) => Math.max(m, estimateTextWidth(trimNumber(w.node.support), PHYLO_SUPPORT_FONT)),
3678
+ 0
3679
+ ) + SUPPORT_GAP : 0;
3680
+ const padLeft = PADDING6 + supportMargin;
3681
+ const tipLabelW = allWork.filter(isTip).reduce((m, w) => Math.max(m, estimateTextWidth(clampLabel(w.node.label, opts.maxLabelChars), PHYLO_LABEL_FONT)), 0);
3682
+ const labelReserve = (tipCount > 0 ? PHYLO_LABEL_GAP + tipLabelW : 0) + PADDING6;
3683
+ const phylogramScaled = mode === "phylogram" && maxDist > 0;
3684
+ const drawW = Math.max(MIN_DRAW_W, maxDepth * PHYLO_LEVEL_W);
3685
+ const scale = phylogramScaled ? drawW / maxDist : 0;
3686
+ const xOf = (w) => {
3687
+ if (phylogramScaled) return padLeft + w.dist * scale;
3688
+ return padLeft + w.depth * PHYLO_LEVEL_W;
3689
+ };
3690
+ for (const w of allWork) w.cx = xOf(w);
3691
+ if (phylogramScaled) {
3692
+ const nudge = (w) => {
3693
+ for (const c of w.children) {
3694
+ if (!isTip(c) && c.cx <= w.cx + 1e-6) c.cx = w.cx + PHYLO_ZERO_NUDGE;
3695
+ nudge(c);
3696
+ }
3697
+ };
3698
+ nudge(root);
3699
+ }
3700
+ const tipsAligned = alignTips;
3701
+ const maxTipX = allWork.filter(isTip).reduce((m, w) => Math.max(m, w.cx), padLeft);
3702
+ const alignX = phylogramScaled ? maxTipX : padLeft + maxDepth * PHYLO_LEVEL_W;
3703
+ const tipDrawX = (w) => tipsAligned ? alignX : w.cx;
3704
+ const rightmost = tipsAligned ? alignX : allWork.reduce((m, w) => Math.max(m, w.cx), padLeft);
3705
+ const treeBottom = top + (tipCount > 0 ? tipCount * PHYLO_ROW_SLOT : PHYLO_ROW_SLOT);
3706
+ const scaleBarPx = wantScaleBar && maxDist > 0 ? niceScaleStep(maxDist) * scale : 0;
3707
+ const hasScaleBar = wantScaleBar && maxDist > 0 && scaleBarPx > 0;
3708
+ const width = Math.ceil(rightmost + labelReserve);
3709
+ const height = Math.ceil(treeBottom + (hasScaleBar ? SCALEBAR_ZONE : 0) + PADDING6 / 2);
3710
+ const nodes = [];
3711
+ const elements = [];
3712
+ const branchTitle = (w) => {
3713
+ if (w.inLength !== null) return `${titleLabels.branchLength}: ${trimNumber(w.inLength)}`;
3714
+ return titleLabels.branchLength;
3715
+ };
3716
+ const ROOT_STUB = 14;
3717
+ const rootDrawX = isTip(root) ? tipDrawX(root) : root.cx;
3718
+ elements.push({
3719
+ edgeId: PHYLO_ROOTSTUB_ID_BASE + root.node.id,
3720
+ kind: "root-stub",
3721
+ points: [
3722
+ { x: round7(Math.max(PADDING6 / 2, rootDrawX - ROOT_STUB)), y: round7(root.cy) },
3723
+ { x: round7(rootDrawX), y: round7(root.cy) }
3724
+ ],
3725
+ dotted: false,
3726
+ length: null,
3727
+ title: titleLabels.root
3728
+ });
3729
+ const emit = (w) => {
3730
+ const tip = isTip(w);
3731
+ const isRoot = w === root;
3732
+ const drawX = tip ? tipDrawX(w) : w.cx;
3733
+ const support = w.node.support === void 0 ? null : w.node.support;
3734
+ const labelLine = tip && w.node.label !== "" ? [clampLabel(w.node.label, opts.maxLabelChars)] : [];
3735
+ nodes.push({
3736
+ nodeId: w.node.id,
3737
+ isTip: tip,
3738
+ cx: round7(drawX),
3739
+ cy: round7(w.cy),
3740
+ support,
3741
+ labelLines: labelLine,
3742
+ labelLeft: round7(drawX + PHYLO_TIP_R + PHYLO_LABEL_GAP),
3743
+ depth: w.depth,
3744
+ title: nodeTitle(w.node, tip, isRoot, w.inLength, titleLabels)
3745
+ });
3746
+ if (tip && tipsAligned && w.cx < alignX - 1e-6) {
3747
+ elements.push({
3748
+ edgeId: PHYLO_EXTENSION_ID_BASE + w.node.id,
3749
+ kind: "extension",
3750
+ points: [
3751
+ { x: round7(w.cx), y: round7(w.cy) },
3752
+ { x: round7(alignX), y: round7(w.cy) }
3753
+ ],
3754
+ dotted: true,
3755
+ length: null,
3756
+ title: titleLabels.tip
3757
+ });
3758
+ }
3759
+ if (w.children.length === 0) return;
3760
+ const childYs = w.children.map((c) => c.cy);
3761
+ const barTop = Math.min(...childYs);
3762
+ const barBottom = Math.max(...childYs);
3763
+ if (barBottom - barTop > 1e-6) {
3764
+ elements.push({
3765
+ edgeId: PHYLO_CLADEBAR_ID_BASE + w.node.id,
3766
+ kind: "clade-bar",
3767
+ points: [
3768
+ { x: round7(w.cx), y: round7(barTop) },
3769
+ { x: round7(w.cx), y: round7(barBottom) }
3770
+ ],
3771
+ dotted: false,
3772
+ length: null,
3773
+ title: isRoot ? titleLabels.root : titleLabels.clade
3774
+ });
3775
+ }
3776
+ for (const c of w.children) {
3777
+ elements.push({
3778
+ edgeId: PHYLO_BRANCH_ID_BASE + c.node.id,
3779
+ kind: "branch",
3780
+ points: [
3781
+ { x: round7(w.cx), y: round7(c.cy) },
3782
+ { x: round7(c.cx), y: round7(c.cy) }
3783
+ ],
3784
+ dotted: false,
3785
+ length: c.inLength,
3786
+ title: branchTitle(c)
3787
+ });
3788
+ }
3789
+ for (const c of w.children) emit(c);
3790
+ };
3791
+ emit(root);
3792
+ let scaleBar = null;
3793
+ if (hasScaleBar) {
3794
+ const stepLen = niceScaleStep(maxDist);
3795
+ const barX = padLeft;
3796
+ const barY = treeBottom + SCALEBAR_ZONE / 2;
3797
+ scaleBar = {
3798
+ length: stepLen,
3799
+ x: round7(barX),
3800
+ y: round7(barY),
3801
+ pxLength: round7(scaleBarPx),
3802
+ valueLabel: trimNumber(stepLen)
3803
+ };
3804
+ elements.push({
3805
+ edgeId: PHYLO_SCALEBAR_ID,
3806
+ kind: "scale-bar",
3807
+ points: [
3808
+ { x: round7(barX), y: round7(barY) },
3809
+ { x: round7(barX + scaleBarPx), y: round7(barY) }
3810
+ ],
3811
+ dotted: false,
3812
+ length: stepLen,
3813
+ title: `${titleLabels.branchLength}: ${trimNumber(stepLen)}`
3814
+ });
3815
+ }
3816
+ return { width, height, mode, nodes, elements, scaleBar, showSupport: effectiveShowSupport };
3817
+ }
3818
+
3819
+ // src/phylo/svg.ts
3820
+ var GLYPH_STROKE4 = "#52525b";
3821
+ var LABEL_FILL5 = "#3f3f46";
3822
+ var EDGE_INK6 = "#71717a";
3823
+ var round8 = (n) => Math.round(n * 100) / 100;
3824
+ function nodeSvg3(n, showSupport) {
3825
+ const pieces = [`<title>${xmlEscape(n.title)}</title>`];
3826
+ pieces.push(`<circle cx="${n.cx}" cy="${n.cy}" r="2.5" fill="${GLYPH_STROKE4}"/>`);
3827
+ if (n.labelLines.length > 0) {
3828
+ pieces.push(
3829
+ `<text x="${n.labelLeft}" y="${round8(n.cy + PHYLO_LABEL_FONT * 0.32)}" font-family="${FONT_FAMILY}" font-size="${PHYLO_LABEL_FONT}" fill="${LABEL_FILL5}">${xmlEscape(n.labelLines[0])}</text>`
3830
+ );
3831
+ }
3832
+ if (showSupport && !n.isTip && n.support !== null) {
3833
+ pieces.push(
3834
+ `<text x="${round8(n.cx - 4)}" y="${round8(n.cy - 3)}" text-anchor="end" font-family="${FONT_FAMILY}" font-size="${PHYLO_SUPPORT_FONT}" fill="${LABEL_FILL5}">${xmlEscape(String(n.support))}</text>`
3835
+ );
3836
+ }
3837
+ return `<g data-node-id="n${n.nodeId}">${pieces.join("")}</g>`;
3838
+ }
3839
+ function elementSvg4(el) {
3840
+ const a = el.points[0];
3841
+ const b = el.points[1];
3842
+ const dash = el.dotted ? ` stroke-dasharray="2,3"` : "";
3843
+ const opacity = el.kind === "extension" ? "0.5" : "0.75";
3844
+ return `<g data-edge-id="${el.edgeId}"><title>${xmlEscape(el.title)}</title><line x1="${a.x}" y1="${a.y}" x2="${b.x}" y2="${b.y}" stroke="${EDGE_INK6}" stroke-width="1.5" stroke-opacity="${opacity}"${dash}/></g>`;
3845
+ }
3846
+ function scaleBarLabelSvg(layout) {
3847
+ const bar = layout.scaleBar;
3848
+ if (bar === null) return "";
3849
+ const tick = (x) => `<line x1="${x}" y1="${round8(bar.y - 3)}" x2="${x}" y2="${round8(bar.y + 3)}" stroke="${EDGE_INK6}" stroke-width="1.5" stroke-opacity="0.75"/>`;
3850
+ return tick(bar.x) + tick(round8(bar.x + bar.pxLength)) + `<text x="${round8(bar.x + bar.pxLength / 2)}" y="${round8(bar.y - 6)}" text-anchor="middle" font-family="${FONT_FAMILY}" font-size="${PHYLO_SUPPORT_FONT}" fill="${LABEL_FILL5}">${xmlEscape(bar.valueLabel)}</text>`;
3851
+ }
3852
+ function supportSwatch(x, y) {
3853
+ const cx = round8(x + LEGEND_SWATCH_W / 2);
3854
+ return `<circle cx="${cx}" cy="${y}" r="2.5" fill="${GLYPH_STROKE4}"/><text x="${round8(cx + 5)}" y="${round8(y + PHYLO_SUPPORT_FONT * 0.32)}" font-family="${FONT_FAMILY}" font-size="${PHYLO_SUPPORT_FONT}" fill="${LABEL_FILL5}">95</text>`;
3855
+ }
3856
+ function scaleSwatch(x, y) {
3857
+ return `<line x1="${round8(x)}" y1="${y}" x2="${round8(x + LEGEND_SWATCH_W)}" y2="${y}" stroke="${EDGE_INK6}" stroke-width="1.5" stroke-opacity="0.75"/>`;
3858
+ }
3859
+ function alignedTipSwatch(x, y) {
3860
+ return `<line x1="${round8(x)}" y1="${y}" x2="${round8(x + LEGEND_SWATCH_W)}" y2="${y}" stroke="${EDGE_INK6}" stroke-width="1.5" stroke-opacity="0.5" stroke-dasharray="2,3"/>`;
3861
+ }
3862
+ function phyloLayoutSvg(layout, opts = {}) {
3863
+ const labels = opts.labels ?? PHYLO_SVG_LABELS_EN;
3864
+ const showSupport = layout.showSupport;
3865
+ const parts = [];
3866
+ for (const el of layout.elements) parts.push(elementSvg4(el));
3867
+ parts.push(scaleBarLabelSvg(layout));
3868
+ for (const n of layout.nodes) parts.push(nodeSvg3(n, showSupport));
3869
+ let width = layout.width;
3870
+ let height = layout.height;
3871
+ if (opts.legend !== false && layout.nodes.length > 0) {
3872
+ const entries = [];
3873
+ if (showSupport) entries.push({ swatch: supportSwatch, label: labels.support });
3874
+ if (layout.scaleBar !== null) entries.push({ swatch: scaleSwatch, label: labels.scaleBar });
3875
+ if (layout.elements.some((e) => e.kind === "extension")) {
3876
+ entries.push({ swatch: alignedTipSwatch, label: labels.alignedTip });
3877
+ }
3878
+ const block = legendBlock(entries, layout.height);
3879
+ if (block.svg !== "") {
3880
+ parts.push(block.svg);
3881
+ width = Math.max(width, block.width);
3882
+ height = block.height;
3883
+ }
3884
+ }
3885
+ const w = Math.ceil(width);
3886
+ const h = Math.ceil(height);
3887
+ return `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${w} ${h}" width="${w}" height="${h}" role="img" aria-label="${xmlEscape(labels.ariaLabel[layout.mode])}">` + parts.join("") + `</svg>`;
3888
+ }
3889
+
3890
+ // src/phylo/render.ts
3891
+ function phyloSvg(input, opts = {}) {
3892
+ const layout = computePhyloLayout(input, {
3893
+ ...opts.mode !== void 0 ? { mode: opts.mode } : {},
3894
+ ...opts.alignTips !== void 0 ? { alignTips: opts.alignTips } : {},
3895
+ ...opts.showSupport !== void 0 ? { showSupport: opts.showSupport } : {},
3896
+ ...opts.scaleBar !== void 0 ? { scaleBar: opts.scaleBar } : {},
3897
+ ...opts.maxLabelChars !== void 0 ? { maxLabelChars: opts.maxLabelChars } : {},
3898
+ ...opts.titleLabels !== void 0 ? { titleLabels: opts.titleLabels } : {}
3899
+ });
3900
+ const svg = phyloLayoutSvg(layout, {
3901
+ ...opts.legend === false ? { legend: false } : {},
3902
+ ...opts.svgLabels !== void 0 ? { labels: opts.svgLabels } : {}
3903
+ });
3904
+ return { svg, layout };
3905
+ }
3906
+
2384
3907
  exports.CHAR_W = CHAR_W;
2385
3908
  exports.CODE_FONT = CODE_FONT;
2386
3909
  exports.ECOMAP_LABELS_EN = ECOMAP_LABELS_EN;
@@ -2412,9 +3935,37 @@ exports.LEGEND_GAP = LEGEND_GAP;
2412
3935
  exports.LEGEND_PAD = LEGEND_PAD;
2413
3936
  exports.LEGEND_ROW_H = LEGEND_ROW_H;
2414
3937
  exports.LEGEND_SWATCH_W = LEGEND_SWATCH_W;
3938
+ exports.LIFE_STATUSES = LIFE_STATUSES;
3939
+ exports.MAX_CONDITIONS_PER_INDIVIDUAL = MAX_CONDITIONS_PER_INDIVIDUAL;
2415
3940
  exports.NODE_SIZE = NODE_SIZE;
2416
3941
  exports.PARENT_REL_ID_BASE = PARENT_REL_ID_BASE;
3942
+ exports.PEDIGREE_SVG_LABELS_EN = PEDIGREE_SVG_LABELS_EN;
3943
+ exports.PEDIGREE_TITLE_LABELS_EN = PEDIGREE_TITLE_LABELS_EN;
3944
+ exports.PED_ADDRESS_FONT = PED_ADDRESS_FONT;
3945
+ exports.PED_CONDITION_FILLS = PED_CONDITION_FILLS;
3946
+ exports.PED_DESCENT_ID_BASE = PED_DESCENT_ID_BASE;
3947
+ exports.PED_GLYPH = PED_GLYPH;
3948
+ exports.PED_LABEL_FONT = PED_LABEL_FONT;
3949
+ exports.PED_LABEL_GAP = PED_LABEL_GAP;
3950
+ exports.PED_LABEL_LINE_H = PED_LABEL_LINE_H;
3951
+ exports.PED_MATING_ID_BASE = PED_MATING_ID_BASE;
3952
+ exports.PED_RISER_ID_BASE = PED_RISER_ID_BASE;
3953
+ exports.PED_SIBBAR_ID_BASE = PED_SIBBAR_ID_BASE;
3954
+ exports.PED_TWINBAR_ID_BASE = PED_TWINBAR_ID_BASE;
3955
+ exports.PHYLO_BRANCH_ID_BASE = PHYLO_BRANCH_ID_BASE;
3956
+ exports.PHYLO_CLADEBAR_ID_BASE = PHYLO_CLADEBAR_ID_BASE;
3957
+ exports.PHYLO_EXTENSION_ID_BASE = PHYLO_EXTENSION_ID_BASE;
3958
+ exports.PHYLO_LABEL_FONT = PHYLO_LABEL_FONT;
3959
+ exports.PHYLO_LABEL_GAP = PHYLO_LABEL_GAP;
3960
+ exports.PHYLO_ROOTSTUB_ID_BASE = PHYLO_ROOTSTUB_ID_BASE;
3961
+ exports.PHYLO_ROW_SLOT = PHYLO_ROW_SLOT;
3962
+ exports.PHYLO_SCALEBAR_ID = PHYLO_SCALEBAR_ID;
3963
+ exports.PHYLO_SUPPORT_FONT = PHYLO_SUPPORT_FONT;
3964
+ exports.PHYLO_SVG_LABELS_EN = PHYLO_SVG_LABELS_EN;
3965
+ exports.PHYLO_TITLE_LABELS_EN = PHYLO_TITLE_LABELS_EN;
2417
3966
  exports.PROMOTED_REL_ID_BASE = PROMOTED_REL_ID_BASE;
3967
+ exports.PedigreeValidationError = PedigreeValidationError;
3968
+ exports.PhyloValidationError = PhyloValidationError;
2418
3969
  exports.QUALITY_LEXICON_EN = QUALITY_LEXICON_EN;
2419
3970
  exports.UNION_NOTATION = UNION_NOTATION;
2420
3971
  exports.UNION_REL_ID_BASE = UNION_REL_ID_BASE;
@@ -2423,6 +3974,8 @@ exports.clampLabel = clampLabel;
2423
3974
  exports.classifyRelationshipType = classifyRelationshipType;
2424
3975
  exports.computeFaultTreeLayout = computeFaultTreeLayout;
2425
3976
  exports.computeGenogramLayout = computeGenogramLayout;
3977
+ exports.computePedigreeLayout = computePedigreeLayout;
3978
+ exports.computePhyloLayout = computePhyloLayout;
2426
3979
  exports.ecomapSvg = ecomapSvg;
2427
3980
  exports.estimateTextWidth = estimateTextWidth;
2428
3981
  exports.faultTreeIssues = faultTreeIssues;
@@ -2433,11 +3986,21 @@ exports.genogramLayoutSvg = genogramLayoutSvg;
2433
3986
  exports.genogramSvg = genogramSvg;
2434
3987
  exports.latestUnionPerPair = latestUnionPerPair;
2435
3988
  exports.legendBlock = legendBlock;
3989
+ exports.niceScaleStep = niceScaleStep;
2436
3990
  exports.normalizeText = normalizeText;
2437
3991
  exports.pathData = pathData;
3992
+ exports.pedigreeIssues = pedigreeIssues;
3993
+ exports.pedigreeLayoutSvg = pedigreeLayoutSvg;
3994
+ exports.pedigreeSvg = pedigreeSvg;
3995
+ exports.phyloIssues = phyloIssues;
3996
+ exports.phyloLayoutSvg = phyloLayoutSvg;
3997
+ exports.phyloSvg = phyloSvg;
2438
3998
  exports.qualityLineStyle = qualityLineStyle;
2439
3999
  exports.relationshipTypeTokens = relationshipTypeTokens;
4000
+ exports.romanNumeral = romanNumeral;
2440
4001
  exports.validateFaultTree = validateFaultTree;
4002
+ exports.validatePedigree = validatePedigree;
4003
+ exports.validatePhylo = validatePhylo;
2441
4004
  exports.wrapLabel = wrapLabel;
2442
4005
  exports.wrapLabelBalanced = wrapLabelBalanced;
2443
4006
  exports.xmlEscape = xmlEscape;