@md2nativedocx/core 0.7.0 → 0.8.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 (199) hide show
  1. package/dist/diagrams/architecture-diagram/icons.d.ts +24 -0
  2. package/dist/diagrams/architecture-diagram/icons.js +148 -0
  3. package/dist/diagrams/architecture-diagram/icons.js.map +1 -0
  4. package/dist/diagrams/architecture-diagram/layout.d.ts +71 -0
  5. package/dist/diagrams/architecture-diagram/layout.js +388 -0
  6. package/dist/diagrams/architecture-diagram/layout.js.map +1 -0
  7. package/dist/diagrams/architecture-diagram/parser.d.ts +14 -19
  8. package/dist/diagrams/architecture-diagram/parser.js +316 -113
  9. package/dist/diagrams/architecture-diagram/parser.js.map +1 -1
  10. package/dist/diagrams/architecture-diagram/translator.d.ts +26 -36
  11. package/dist/diagrams/architecture-diagram/translator.js +234 -147
  12. package/dist/diagrams/architecture-diagram/translator.js.map +1 -1
  13. package/dist/diagrams/architecture-diagram/types.d.ts +26 -16
  14. package/dist/diagrams/architecture-diagram/types.js +4 -14
  15. package/dist/diagrams/architecture-diagram/types.js.map +1 -1
  16. package/dist/diagrams/c4/parser.d.ts +11 -19
  17. package/dist/diagrams/c4/parser.js +242 -90
  18. package/dist/diagrams/c4/parser.js.map +1 -1
  19. package/dist/diagrams/c4/translator.d.ts +23 -43
  20. package/dist/diagrams/c4/translator.js +333 -204
  21. package/dist/diagrams/c4/translator.js.map +1 -1
  22. package/dist/diagrams/c4/types.d.ts +78 -45
  23. package/dist/diagrams/c4/types.js +17 -38
  24. package/dist/diagrams/c4/types.js.map +1 -1
  25. package/dist/diagrams/class-diagram/parser.d.ts +10 -11
  26. package/dist/diagrams/class-diagram/parser.js +316 -74
  27. package/dist/diagrams/class-diagram/parser.js.map +1 -1
  28. package/dist/diagrams/class-diagram/translator.d.ts +5 -0
  29. package/dist/diagrams/class-diagram/translator.js +292 -24
  30. package/dist/diagrams/class-diagram/translator.js.map +1 -1
  31. package/dist/diagrams/class-diagram/types.d.ts +61 -4
  32. package/dist/diagrams/er-diagram/parser.d.ts +22 -19
  33. package/dist/diagrams/er-diagram/parser.js +412 -89
  34. package/dist/diagrams/er-diagram/parser.js.map +1 -1
  35. package/dist/diagrams/er-diagram/translator.d.ts +8 -0
  36. package/dist/diagrams/er-diagram/translator.js +170 -38
  37. package/dist/diagrams/er-diagram/translator.js.map +1 -1
  38. package/dist/diagrams/er-diagram/types.d.ts +32 -5
  39. package/dist/diagrams/er-diagram/types.js +2 -5
  40. package/dist/diagrams/er-diagram/types.js.map +1 -1
  41. package/dist/diagrams/gantt/date-utils.d.ts +55 -33
  42. package/dist/diagrams/gantt/date-utils.js +147 -58
  43. package/dist/diagrams/gantt/date-utils.js.map +1 -1
  44. package/dist/diagrams/gantt/parser.d.ts +31 -22
  45. package/dist/diagrams/gantt/parser.js +259 -141
  46. package/dist/diagrams/gantt/parser.js.map +1 -1
  47. package/dist/diagrams/gantt/time-axis.d.ts +44 -0
  48. package/dist/diagrams/gantt/time-axis.js +158 -0
  49. package/dist/diagrams/gantt/time-axis.js.map +1 -0
  50. package/dist/diagrams/gantt/translator.d.ts +8 -5
  51. package/dist/diagrams/gantt/translator.js +67 -56
  52. package/dist/diagrams/gantt/translator.js.map +1 -1
  53. package/dist/diagrams/gantt/types.d.ts +25 -4
  54. package/dist/diagrams/gantt/types.js +4 -3
  55. package/dist/diagrams/gantt/types.js.map +1 -1
  56. package/dist/diagrams/mindmap/parser.d.ts +18 -14
  57. package/dist/diagrams/mindmap/parser.js +169 -69
  58. package/dist/diagrams/mindmap/parser.js.map +1 -1
  59. package/dist/diagrams/mindmap/translator.js +88 -35
  60. package/dist/diagrams/mindmap/translator.js.map +1 -1
  61. package/dist/diagrams/mindmap/types.d.ts +18 -0
  62. package/dist/diagrams/plantuml/class-parser.d.ts +2 -1
  63. package/dist/diagrams/plantuml/class-parser.js +19 -11
  64. package/dist/diagrams/plantuml/class-parser.js.map +1 -1
  65. package/dist/diagrams/plantuml/sequence-parser.d.ts +3 -2
  66. package/dist/diagrams/plantuml/sequence-parser.js +28 -12
  67. package/dist/diagrams/plantuml/sequence-parser.js.map +1 -1
  68. package/dist/diagrams/plantuml/state-parser.d.ts +5 -3
  69. package/dist/diagrams/plantuml/state-parser.js +141 -19
  70. package/dist/diagrams/plantuml/state-parser.js.map +1 -1
  71. package/dist/diagrams/requirement-diagram/parser.d.ts +26 -19
  72. package/dist/diagrams/requirement-diagram/parser.js +249 -70
  73. package/dist/diagrams/requirement-diagram/parser.js.map +1 -1
  74. package/dist/diagrams/requirement-diagram/translator.d.ts +16 -11
  75. package/dist/diagrams/requirement-diagram/translator.js +170 -50
  76. package/dist/diagrams/requirement-diagram/translator.js.map +1 -1
  77. package/dist/diagrams/requirement-diagram/types.d.ts +30 -9
  78. package/dist/diagrams/requirement-diagram/types.js +7 -5
  79. package/dist/diagrams/requirement-diagram/types.js.map +1 -1
  80. package/dist/diagrams/sequence/parser.d.ts +8 -7
  81. package/dist/diagrams/sequence/parser.js +100 -31
  82. package/dist/diagrams/sequence/parser.js.map +1 -1
  83. package/dist/diagrams/sequence/participant-glyph.d.ts +38 -0
  84. package/dist/diagrams/sequence/participant-glyph.js +97 -0
  85. package/dist/diagrams/sequence/participant-glyph.js.map +1 -0
  86. package/dist/diagrams/sequence/participant-kind.d.ts +7 -0
  87. package/dist/diagrams/sequence/participant-kind.js +19 -0
  88. package/dist/diagrams/sequence/participant-kind.js.map +1 -0
  89. package/dist/diagrams/sequence/translator.d.ts +7 -3
  90. package/dist/diagrams/sequence/translator.js +134 -25
  91. package/dist/diagrams/sequence/translator.js.map +1 -1
  92. package/dist/diagrams/sequence/types.d.ts +24 -1
  93. package/dist/diagrams/state-diagram/parser.d.ts +24 -24
  94. package/dist/diagrams/state-diagram/parser.js +259 -73
  95. package/dist/diagrams/state-diagram/parser.js.map +1 -1
  96. package/dist/diagrams/state-diagram/translator.d.ts +13 -1
  97. package/dist/diagrams/state-diagram/translator.js +554 -72
  98. package/dist/diagrams/state-diagram/translator.js.map +1 -1
  99. package/dist/diagrams/state-diagram/types.d.ts +57 -11
  100. package/dist/diagrams/state-diagram/types.js +5 -7
  101. package/dist/diagrams/state-diagram/types.js.map +1 -1
  102. package/dist/diagrams/tree-view/implicit-root.d.ts +11 -0
  103. package/dist/diagrams/tree-view/implicit-root.js +14 -0
  104. package/dist/diagrams/tree-view/implicit-root.js.map +1 -0
  105. package/dist/diagrams/tree-view/parser.d.ts +23 -8
  106. package/dist/diagrams/tree-view/parser.js +242 -98
  107. package/dist/diagrams/tree-view/parser.js.map +1 -1
  108. package/dist/diagrams/tree-view/translator.d.ts +6 -5
  109. package/dist/diagrams/tree-view/translator.js +40 -23
  110. package/dist/diagrams/tree-view/translator.js.map +1 -1
  111. package/dist/diagrams/tree-view/types.d.ts +22 -6
  112. package/dist/diagrams/tree-view/types.js +2 -2
  113. package/dist/diagrams/treemap/parser.d.ts +2 -2
  114. package/dist/diagrams/treemap/parser.js +5 -12
  115. package/dist/diagrams/treemap/parser.js.map +1 -1
  116. package/dist/diagrams/zenuml/parser.d.ts +2 -2
  117. package/dist/diagrams/zenuml/parser.js +14 -7
  118. package/dist/diagrams/zenuml/parser.js.map +1 -1
  119. package/dist/index.d.ts +1 -1
  120. package/dist/index.js +1 -1
  121. package/dist/index.js.map +1 -1
  122. package/dist/layout/collapse-subgraphs.d.ts +18 -0
  123. package/dist/layout/collapse-subgraphs.js +80 -0
  124. package/dist/layout/collapse-subgraphs.js.map +1 -0
  125. package/dist/layout/layout.d.ts +9 -0
  126. package/dist/layout/layout.js +77 -17
  127. package/dist/layout/layout.js.map +1 -1
  128. package/dist/parser/css-named-colors.d.ts +7 -0
  129. package/dist/parser/css-named-colors.js +157 -0
  130. package/dist/parser/css-named-colors.js.map +1 -0
  131. package/dist/parser/css-style.d.ts +39 -0
  132. package/dist/parser/css-style.js +88 -0
  133. package/dist/parser/css-style.js.map +1 -0
  134. package/dist/parser/entities.d.ts +5 -0
  135. package/dist/parser/entities.js +52 -0
  136. package/dist/parser/entities.js.map +1 -0
  137. package/dist/parser/frontmatter.d.ts +4 -2
  138. package/dist/parser/frontmatter.js +7 -5
  139. package/dist/parser/frontmatter.js.map +1 -1
  140. package/dist/parser/label.d.ts +37 -0
  141. package/dist/parser/label.js +73 -0
  142. package/dist/parser/label.js.map +1 -0
  143. package/dist/parser/parser.d.ts +2 -1
  144. package/dist/parser/parser.js +295 -329
  145. package/dist/parser/parser.js.map +1 -1
  146. package/dist/render-diagram-odf.d.ts +2 -1
  147. package/dist/render-diagram-odf.js +20 -5
  148. package/dist/render-diagram-odf.js.map +1 -1
  149. package/dist/render-diagram.d.ts +11 -3
  150. package/dist/render-diagram.js +44 -21
  151. package/dist/render-diagram.js.map +1 -1
  152. package/dist/smartart/chain.js +1 -1
  153. package/dist/smartart/chain.js.map +1 -1
  154. package/dist/smartart/cycle.js +1 -1
  155. package/dist/smartart/cycle.js.map +1 -1
  156. package/dist/smartart/drawing.js +2 -10
  157. package/dist/smartart/drawing.js.map +1 -1
  158. package/dist/smartart/embed.d.ts +4 -3
  159. package/dist/smartart/embed.js +5 -4
  160. package/dist/smartart/embed.js.map +1 -1
  161. package/dist/smartart/from-graph.d.ts +13 -3
  162. package/dist/smartart/from-graph.js +39 -7
  163. package/dist/smartart/from-graph.js.map +1 -1
  164. package/dist/smartart/from-tree.d.ts +9 -5
  165. package/dist/smartart/from-tree.js +23 -11
  166. package/dist/smartart/from-tree.js.map +1 -1
  167. package/dist/smartart/text.d.ts +18 -4
  168. package/dist/smartart/text.js +32 -8
  169. package/dist/smartart/text.js.map +1 -1
  170. package/dist/smartart/tree-deep.js +1 -1
  171. package/dist/smartart/tree-deep.js.map +1 -1
  172. package/dist/smartart/tree.js +2 -2
  173. package/dist/smartart/tree.js.map +1 -1
  174. package/dist/translator/boxes.d.ts +1 -1
  175. package/dist/translator/canvas.d.ts +9 -1
  176. package/dist/translator/canvas.js +19 -16
  177. package/dist/translator/canvas.js.map +1 -1
  178. package/dist/translator/custom-geometry.d.ts +28 -0
  179. package/dist/translator/custom-geometry.js +59 -0
  180. package/dist/translator/custom-geometry.js.map +1 -0
  181. package/dist/translator/elbow-connector.d.ts +52 -0
  182. package/dist/translator/elbow-connector.js +110 -0
  183. package/dist/translator/elbow-connector.js.map +1 -0
  184. package/dist/translator/graph-shapes.d.ts +17 -4
  185. package/dist/translator/graph-shapes.js +25 -6
  186. package/dist/translator/graph-shapes.js.map +1 -1
  187. package/dist/translator/label-runs.d.ts +14 -0
  188. package/dist/translator/label-runs.js +36 -0
  189. package/dist/translator/label-runs.js.map +1 -0
  190. package/dist/translator/odf-translator.js +20 -22
  191. package/dist/translator/odf-translator.js.map +1 -1
  192. package/dist/translator/ooxml-translator.d.ts +17 -11
  193. package/dist/translator/ooxml-translator.js +73 -139
  194. package/dist/translator/ooxml-translator.js.map +1 -1
  195. package/dist/translator/text-color.d.ts +9 -0
  196. package/dist/translator/text-color.js +17 -0
  197. package/dist/translator/text-color.js.map +1 -0
  198. package/dist/types.d.ts +20 -3
  199. package/package.json +1 -1
@@ -1,241 +1,370 @@
1
1
  /**
2
- * OOXML translator for a Mermaid C4 diagram (Family B — reuses Dagre for
3
- * layout and `../../translator/graph-shapes.ts`'s primitives, same
4
- * rationale as `../requirement-diagram/translator.ts`'s module doc
5
- * comment, which this module's 2-compartment box/relationship-label code
6
- * mirrors closely).
2
+ * OOXML translator for a Mermaid C4 diagram, drawn the way Mermaid 12.3 draws it (its C4 renderer, read
3
+ * against the official examples; the code here is this project's own).
7
4
  *
8
- * Every element category (Person/System/Container/Component/Node) renders
9
- * as the same 2-compartment box shape — a bold centered name, then a
10
- * `[Category, Qualifier: technology]`-style stereotype line plus an
11
- * optional description line — differing only by fill color per category.
12
- * Two deliberate v1 simplifications, both surfaced in the stereotype text
13
- * rather than a distinct shape:
14
- * - **External elements aren't drawn differently** (no dashed border, no
15
- * gray recolor) — `C4Element.external` only changes the stereotype text
16
- * (e.g. `[System, External]`). Real C4 tooling (and Mermaid's own
17
- * renderer) typically grays out external elements; reproducing that
18
- * would mean threading a second border/fill style through `rect()`
19
- * (`../../translator/graph-shapes.ts`) for a purely cosmetic distinction
20
- * this project's other 2-compartment-box modules don't need either.
21
- * - **`Db`/`Queue` variants don't get a cylinder/queue shape** — same
22
- * "text conveys the semantics, box geometry stays uniform" choice
23
- * `requirementDiagram`'s translator already makes for its own stereotype
24
- * line, rather than inventing shape-per-field handling this diagram
25
- * family has never needed before.
5
+ * **Layout.** Mermaid does not run a graph layout for C4: it places things in declaration order on a
6
+ * grid. Inside the diagram and inside each frame, the elements come first, `shapesPerRow` to a row
7
+ * (default 4), then the nested frames below them, `boundariesPerRow` to a row (default 2); a frame is as
8
+ * large as what it holds. `UpdateLayoutConfig` changes both numbers. The spacing is tighter than Mermaid's
9
+ * (50 px margins on every side of a shape) so a diagram keeps a readable size once scaled to a page.
26
10
  *
27
- * **Boundary/Deployment_Node nesting is not rendered as nesting** — see
28
- * `parser.ts`'s doc comment; every element lays out as an ordinary sibling
29
- * box in one flat Dagre graph regardless of its `parent`.
11
+ * **Shapes and colours.** One shape per element, its text inside it (so it moves with the shape in Word):
12
+ * a person is a rounded body under a round head, a `Db` a cylinder, a `Queue` a cylinder lying on its
13
+ * side, anything else a rounded rectangle. Fill and outline per category and external flag are Mermaid's
14
+ * (`person_bg_color`...), white text, bold name, then `[Software System]`/`[Container: techn]` and the
15
+ * description in smaller type. A boundary is a dashed frame, a deployment node a solid one, label bold and
16
+ * centred at the top with its `[type]` under it. `UpdateElementStyle` replaces fill, outline and text
17
+ * colour, on an element or a frame.
30
18
  *
31
- * Relationship lines are straight (box-border to box-border via
32
- * `edgePoint()`), not Dagre-routed multi-point polylines — the same v1
33
- * simplification every other Family B translator already makes (see
34
- * `../class-diagram/translator.ts`'s doc comment). Confirmed via a real
35
- * render of `test-corpus/visual/fixtures/c4.mmd` (the C4-PlantUML/Mermaid
36
- * docs' own banking-system example): a relationship that skips a rank
37
- * (`Rel(customer, spa, ...)`, where `Rel(web_app, spa, "Delivers")`
38
- * separately forces `spa` a rank below `web_app`) draws straight through
39
- * `web_app`'s box rather than routing around it, same as a flowchart
40
- * skip-rank edge would without `ooxml-translator.ts`'s dedicated routing —
41
- * which this simpler family of translators doesn't have. Not fixed here,
42
- * consistent with the rest of the family.
19
+ * **Relationships** are straight lines between the two shapes' borders, `444444` unless `UpdateRelStyle`
20
+ * says otherwise, arrow at the target (both ends for `BiRel`), label at the middle of the line shifted by
21
+ * `$offsetX`/`$offsetY`, technology in italics under it, numbered `1:`, `2:`... in a `C4Dynamic`. Mermaid
22
+ * curves every line after the first one; here they all stay straight, and two relationships between the
23
+ * same pair are drawn side by side rather than on top of each other. A line can cross a shape it passes:
24
+ * Mermaid's grid does not route around shapes either.
43
25
  */
44
- import dagre from 'dagre';
45
- import { layoutWithinLimits, nodeKey } from '../../layout/graph-limits.js';
46
26
  import { estimateTextWidth } from '../../layout/layout.js';
47
- import { createIdAllocator, scaledExtent, scaledFontSizeHalfPt, scaledLineWidthEmu, wrapDrawingCanvas, } from '../../translator/canvas.js';
48
- import { connector, edgePoint, parallelEdgeOffset, perpendicularUnit, rect, scalePt, shiftPoint, textBoxLines } from '../../translator/graph-shapes.js';
49
- const LINE_COLOR = '2F5496';
50
- const CATEGORY_FILL = {
51
- person: 'FFE699',
52
- system: 'D9E2F3',
53
- container: 'C6E0B4',
54
- component: 'F8CBAD',
55
- node: 'E2E2E2',
27
+ import { createIdAllocator, scaledExtent, scaledFontSizeHalfPt, scaledLineWidthEmu, wrapDrawingCanvas } from '../../translator/canvas.js';
28
+ import { customGeometry, circlePath, horizontalCylinderGeometry, roundedRectPath } from '../../translator/custom-geometry.js';
29
+ import { connector, edgePoint, parallelEdgeOffset, perpendicularUnit, scalePt, shiftPoint, textBoxLines } from '../../translator/graph-shapes.js';
30
+ import { noteParagraph, wrapText } from '../../translator/boxes.js';
31
+ import { escapeXml } from '../../translator/xml-escape.js';
32
+ /** Mermaid's `<category>_bg_color` / `_border_color` defaults, internal then external. */
33
+ const PALETTE = {
34
+ person: { internal: ['08427B', '073B6F'], external: ['686868', '8A8A8A'] },
35
+ system: { internal: ['1168BD', '3C7FC0'], external: ['999999', '8A8A8A'] },
36
+ container: { internal: ['438DD5', '3C7FC0'], external: ['B3B3B3', 'A6A6A6'] },
37
+ component: { internal: ['85BBF0', '78A8D8'], external: ['CCCCCC', 'BFBFBF'] },
56
38
  };
57
- const TITLE_FONT_PX = 13;
58
- const BODY_FONT_PX = 10;
59
- const TITLE_SIZE_HALFPT = 22;
60
- const BODY_SIZE_HALFPT = 18;
61
- const LABEL_SIZE_HALFPT = 16;
62
- const TITLE_H = 32;
63
- const ROW_H = 22;
64
- const COMPARTMENT_PAD_Y = 6;
65
- const PAD_X = 10;
66
- const DIAGRAM_TITLE_H = 34;
67
- const DIAGRAM_TITLE_SIZE_HALFPT = 28;
68
- const CATEGORY_LABEL = {
39
+ /** Mermaid's stereotype names (`[Software System]`, not `[System]`). */
40
+ const STEREOTYPE = {
69
41
  person: 'Person',
70
- system: 'System',
42
+ system: 'Software System',
71
43
  container: 'Container',
72
44
  component: 'Component',
73
- node: 'Node',
74
45
  };
75
- function stereotypeLine(el) {
76
- const qualifiers = [];
77
- if (el.external)
78
- qualifiers.push('External');
79
- if (el.variant === 'db')
80
- qualifiers.push('Database');
81
- if (el.variant === 'queue')
82
- qualifiers.push('Queue');
83
- const head = qualifiers.length > 0 ? `${CATEGORY_LABEL[el.category]}, ${qualifiers.join(', ')}` : CATEGORY_LABEL[el.category];
84
- return el.techn ? `[${head}: ${el.techn}]` : `[${head}]`;
46
+ const ELEMENT_TEXT = 'FFFFFF';
47
+ const FRAME_COLOR = '444444';
48
+ const REL_COLOR = '444444';
49
+ /** Type sizes in pt: Mermaid's 14 px name, its stereotype at 0.75 em and description at 0.82 em. */
50
+ const NAME_PT = 10.5;
51
+ const STEREO_PT = 8;
52
+ const DESCR_PT = 8.5;
53
+ const FRAME_LABEL_PT = 12;
54
+ const FRAME_TYPE_PT = 10.5;
55
+ const FRAME_DESCR_PT = 9;
56
+ const REL_PT = 9;
57
+ const TITLE_PT = 14;
58
+ const pxOf = (pt) => (pt * 4) / 3;
59
+ /** Height of one line of text at `pt`, in px (Word's single spacing is about 1.2 em; a little room on top). */
60
+ const lineH = (pt) => Math.ceil(pxOf(pt) * 1.3);
61
+ const halfPt = (pt) => Math.round(pt * 2);
62
+ /** Mermaid's element width; text wraps inside it, less the padding. */
63
+ const ELEMENT_W = 216;
64
+ const ELEMENT_PAD_X = 16;
65
+ const ELEMENT_PAD_Y = 10;
66
+ const ELEMENT_MIN_H = 60;
67
+ const SECTION_GAP = 3;
68
+ /** Rounded-rectangle corner radius (Mermaid's `rx: 12px`). */
69
+ const CORNER = 12;
70
+ /** Half the height of a `Db` cylinder's top ellipse, and the width of a `Queue` cylinder's end cap. */
71
+ const DB_CAP = 10;
72
+ const QUEUE_CAP = 8;
73
+ const GAP_X = 40;
74
+ const GAP_Y = 56;
75
+ const FRAME_PAD = 20;
76
+ const FRAME_HEADER_GAP = 12;
77
+ const CANVAS_PAD = 24;
78
+ const DEFAULT_SHAPES_PER_ROW = 4;
79
+ const DEFAULT_BOUNDARIES_PER_ROW = 2;
80
+ function stereotype(el) {
81
+ return el.techn ? `[${STEREOTYPE[el.category]}: ${el.techn}]` : `[${STEREOTYPE[el.category]}]`;
85
82
  }
86
- function bodyLinesFor(el) {
87
- const lines = [stereotypeLine(el)];
88
- if (el.description)
89
- lines.push(el.description);
90
- return lines;
83
+ const asWritten = (pt) => pt;
84
+ function wrapped(text, pt, widthPx, fit, style = {}) {
85
+ return wrapText(text, pxOf(fit(pt)), widthPx).map((line) => ({ text: line, pt, ...style }));
86
+ }
87
+ const textHeight = (lines, fit) => lines.reduce((sum, l) => sum + lineH(fit(l.pt)), 0);
88
+ const textWidth = (lines, fit) => Math.max(0, ...lines.map((l) => estimateTextWidth(l.text, pxOf(fit(l.pt))) * (l.bold ? 1.08 : 1)));
89
+ /** Person geometry for a body `w` wide, in Mermaid's proportions: head radius about a quarter of the
90
+ * width (16 to 56 px), sinking into the body by a quarter of that radius. */
91
+ function personHead(w) {
92
+ const r = Math.min(Math.max(w * 0.23, 16), 56);
93
+ return { r, overlap: r * 0.27 };
91
94
  }
92
- function sizeForElement(el, bodyLines) {
93
- let width = estimateTextWidth(el.label, TITLE_FONT_PX) + PAD_X * 2 + 16;
94
- for (const line of bodyLines) {
95
- width = Math.max(width, estimateTextWidth(line, BODY_FONT_PX) + PAD_X * 2);
95
+ function measureElement(el, fit) {
96
+ // A word longer than the wrap width widens the shape rather than being cut.
97
+ const sections = [el.label, stereotype(el), el.description ?? ''];
98
+ const longestWord = Math.max(0, ...sections.flatMap((s, i) => s.split(/\s+/).map((word) => estimateTextWidth(word, pxOf(fit([NAME_PT, STEREO_PT, DESCR_PT][i]))) * (i === 0 ? 1.08 : 1))));
99
+ const w = Math.max(ELEMENT_W, Math.ceil(longestWord) + 2 * ELEMENT_PAD_X);
100
+ const wrapW = w - 2 * ELEMENT_PAD_X;
101
+ const lines = [
102
+ ...wrapped(el.label, NAME_PT, wrapW, fit, { bold: true }),
103
+ ...wrapped(stereotype(el), STEREO_PT, wrapW, fit),
104
+ ...(el.description ? wrapped(el.description, DESCR_PT, wrapW, fit) : []),
105
+ ];
106
+ const sectionsShown = el.description ? 3 : 2;
107
+ const textH = textHeight(lines, fit) + (sectionsShown - 1) * SECTION_GAP;
108
+ const bodyH = Math.max(ELEMENT_MIN_H, textH + 2 * ELEMENT_PAD_Y);
109
+ let bodyTop = 0;
110
+ let bodyBottom = 0;
111
+ if (el.category === 'person') {
112
+ const { r, overlap } = personHead(w);
113
+ bodyTop = 2 * r - overlap;
114
+ }
115
+ else if (el.variant === 'db') {
116
+ // The top ellipse is 2 x DB_CAP high; the bottom curve dips DB_CAP under the side lines.
117
+ bodyTop = 2 * DB_CAP;
118
+ bodyBottom = DB_CAP;
96
119
  }
97
- width = Math.max(110, width);
98
- const bodyH = bodyLines.length * ROW_H + COMPARTMENT_PAD_Y;
99
- return { width, height: TITLE_H + bodyH, titleH: TITLE_H, bodyH };
120
+ return { element: el, lines, bodyTop, bodyBottom, x: 0, y: 0, w, h: bodyTop + bodyH + bodyBottom };
100
121
  }
101
- function elementShapes(nextId, el, bodyLines, size, x, y, s) {
102
- const parts = [];
103
- const bx = scalePt(x, s);
104
- const by = scalePt(y, s);
105
- const w = scalePt(size.width, s);
106
- const titleH = scalePt(size.titleH, s);
107
- const bodyH = scalePt(size.bodyH, s);
108
- const totalH = titleH + bodyH;
109
- const borderW = scaledLineWidthEmu(9525, s);
110
- const fill = CATEGORY_FILL[el.category];
111
- parts.push(rect(nextId(), bx, by, w, totalH, 'FFFFFF', LINE_COLOR, el.label));
112
- parts.push(rect(nextId(), bx, by, w, titleH, fill, undefined, `${el.label} title`));
113
- parts.push(rect(nextId(), bx, by + titleH - Math.round(borderW / 2), w, borderW, LINE_COLOR, undefined, 'Divider'));
114
- parts.push(textBoxLines(nextId(), bx, by, w, titleH, [el.label], scaledFontSizeHalfPt(TITLE_SIZE_HALFPT, s), { bold: true, align: 'ctr' }));
115
- const padX = scalePt(PAD_X, s);
116
- parts.push(textBoxLines(nextId(), bx + padX, by + titleH, w - 2 * padX, bodyH, bodyLines, scaledFontSizeHalfPt(BODY_SIZE_HALFPT, s), { align: 'ctr' }));
117
- return parts;
122
+ /** Place `items` left to right, `perRow` to a row, from (`x0`, `y0`); returns the extent used. */
123
+ function placeRows(items, perRow, x0, y0) {
124
+ let y = y0;
125
+ let maxW = 0;
126
+ for (let start = 0; start < items.length; start += perRow) {
127
+ const row = items.slice(start, start + perRow);
128
+ let x = x0;
129
+ let rowH = 0;
130
+ for (const item of row) {
131
+ item.x = x;
132
+ item.y = y;
133
+ x += item.w + GAP_X;
134
+ rowH = Math.max(rowH, item.h);
135
+ }
136
+ maxW = Math.max(maxW, x - GAP_X - x0);
137
+ y += rowH + GAP_Y;
138
+ }
139
+ return { w: maxW, h: items.length > 0 ? y - GAP_Y - y0 : 0 };
118
140
  }
119
- /** Translate a parsed C4 diagram into a self-contained WordprocessingML
120
- * paragraph. An empty diagram renders a visible note, matching the other
121
- * Family B translators' zero-content convention — never a silent blank
122
- * canvas. */
123
- export function translateC4DiagramToOoxml(chart, options = {}) {
124
- if (chart.elements.length === 0) {
125
- return [
126
- '<w:p xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">',
127
- ' <w:pPr><w:spacing w:before="0" w:after="120"/></w:pPr>',
128
- ' <w:r>',
129
- ' <w:rPr><w:i/><w:color w:val="808080"/><w:sz w:val="18"/></w:rPr>',
130
- ' <w:t xml:space="preserve">This C4 diagram has no content to render.</w:t>',
131
- ' </w:r>',
132
- '</w:p>',
133
- ].join('\n');
141
+ /** Move a block and everything it holds by (`dx`, `dy`). */
142
+ function shiftBlock(block, dx, dy) {
143
+ block.x += dx;
144
+ block.y += dy;
145
+ for (const el of block.elements) {
146
+ el.x += dx;
147
+ el.y += dy;
134
148
  }
135
- const bodyLinesById = new Map();
136
- for (const el of chart.elements)
137
- bodyLinesById.set(el.id, bodyLinesFor(el));
138
- const sizes = new Map();
139
- const g = new dagre.graphlib.Graph();
140
- g.setGraph({ rankdir: 'TB', nodesep: 50, ranksep: 70, marginx: 0, marginy: 0 });
141
- g.setDefaultEdgeLabel(() => ({}));
149
+ for (const child of block.children)
150
+ shiftBlock(child, dx, dy);
151
+ }
152
+ /**
153
+ * Size `block` and place its content relative to its own top-left corner (children are laid out first,
154
+ * then moved into place, so every coordinate ends up relative to this block).
155
+ */
156
+ function layoutBlock(block, shapesPerRow, boundariesPerRow, fit) {
157
+ for (const child of block.children)
158
+ layoutBlock(child, shapesPerRow, boundariesPerRow, fit);
159
+ const framed = block.boundary !== undefined;
160
+ const pad = framed ? FRAME_PAD : 0;
161
+ const headerH = framed ? textHeight(block.header, fit) + FRAME_HEADER_GAP : 0;
162
+ const top = pad + headerH;
163
+ const elementsExtent = placeRows(block.elements, shapesPerRow, pad, top);
164
+ const childTop = top + elementsExtent.h + (elementsExtent.h > 0 && block.children.length > 0 ? GAP_Y : 0);
165
+ // Children were laid out at their own origin: record it, place their boxes, then move their content.
166
+ const origins = block.children.map((c) => ({ x: c.x, y: c.y }));
167
+ const childExtent = placeRows(block.children, boundariesPerRow, pad, childTop);
168
+ block.children.forEach((child, i) => {
169
+ const target = { x: child.x, y: child.y };
170
+ child.x = origins[i].x;
171
+ child.y = origins[i].y;
172
+ shiftBlock(child, target.x - child.x, target.y - child.y);
173
+ });
174
+ const contentW = Math.max(elementsExtent.w, childExtent.w);
175
+ const contentBottom = childExtent.h > 0 ? childTop + childExtent.h : top + elementsExtent.h;
176
+ block.w = Math.max(contentW, framed ? textWidth(block.header, fit) + 16 : 0, framed ? 120 : 0) + 2 * pad;
177
+ block.h = Math.max(contentBottom + pad, framed ? top + 24 : 0);
178
+ // Centre the content of a frame under its (wider) header, as Mermaid centres the label over it.
179
+ const slack = block.w - 2 * pad - contentW;
180
+ if (slack > 0 && framed) {
181
+ for (const el of block.elements)
182
+ el.x += slack / 2;
183
+ for (const child of block.children)
184
+ shiftBlock(child, slack / 2, 0);
185
+ }
186
+ }
187
+ function frameHeader(b) {
188
+ const lines = [];
189
+ if (b.label)
190
+ lines.push(...b.label.split('\n').map((text) => ({ text, pt: FRAME_LABEL_PT, bold: true })));
191
+ if (b.type)
192
+ lines.push(...`[${b.type}]`.split('\n').map((text) => ({ text, pt: FRAME_TYPE_PT })));
193
+ if (b.description)
194
+ lines.push(...b.description.split('\n').map((text) => ({ text, pt: FRAME_DESCR_PT })));
195
+ return lines;
196
+ }
197
+ /** Build the block tree (the diagram, its frames by `parent`, each holding its elements) and lay it out
198
+ * on the canvas, under the title. */
199
+ function layoutDiagram(chart, fit) {
200
+ const root = { header: [], elements: [], children: [], x: 0, y: 0, w: 0, h: 0 };
201
+ const byId = new Map();
202
+ for (const b of chart.boundaries ?? [])
203
+ byId.set(b.id, { boundary: b, header: frameHeader(b), elements: [], children: [], x: 0, y: 0, w: 0, h: 0 });
204
+ // A parent that names nothing known (or itself, or a cycle built by hand) falls back to the diagram.
205
+ const parentOf = (parent, self) => {
206
+ if (parent === undefined || parent === self)
207
+ return root;
208
+ const seen = new Set(self ? [self] : []);
209
+ let cursor = parent;
210
+ while (cursor !== undefined) {
211
+ if (seen.has(cursor))
212
+ return root;
213
+ seen.add(cursor);
214
+ cursor = byId.get(cursor)?.boundary?.parent;
215
+ }
216
+ return byId.get(parent) ?? root;
217
+ };
218
+ for (const block of byId.values())
219
+ parentOf(block.boundary.parent, block.boundary.id).children.push(block);
220
+ const elementsById = new Map();
142
221
  for (const el of chart.elements) {
143
- const size = sizeForElement(el, bodyLinesById.get(el.id));
144
- sizes.set(el.id, size);
145
- g.setNode(nodeKey(el.id), { width: size.width, height: size.height });
222
+ const layout = measureElement(el, fit);
223
+ elementsById.set(el.id, layout);
224
+ parentOf(el.parent).elements.push(layout);
225
+ }
226
+ layoutBlock(root, chart.layout?.shapesPerRow ?? DEFAULT_SHAPES_PER_ROW, chart.layout?.boundariesPerRow ?? DEFAULT_BOUNDARIES_PER_ROW, fit);
227
+ const titleH = chart.title ? lineH(fit(TITLE_PT)) + 12 : 0;
228
+ const canvasW = Math.max(root.w, chart.title ? estimateTextWidth(chart.title, pxOf(fit(TITLE_PT))) * 1.08 : 0) + 2 * CANVAS_PAD;
229
+ const canvasH = root.h + 2 * CANVAS_PAD + titleH;
230
+ shiftBlock(root, CANVAS_PAD + (canvasW - 2 * CANVAS_PAD - root.w) / 2, CANVAS_PAD + titleH);
231
+ return { root, byId, elementsById, canvasW, canvasH };
232
+ }
233
+ /** One `wps:wsp`: a geometry, its fill and outline, and its text inside it (insets in EMU). */
234
+ function shapeWithText(id, name, box, geometry, fill, line, paragraphs, inset, anchor) {
235
+ const fillXml = fill ? `<a:solidFill><a:srgbClr val="${fill}"/></a:solidFill>` : '<a:noFill/>';
236
+ const lnXml = `<a:ln w="${line.emu}"><a:solidFill><a:srgbClr val="${line.color}"/></a:solidFill>${line.dashed ? '<a:prstDash val="dash"/>' : ''}</a:ln>`;
237
+ const body = paragraphs
238
+ .map((p) => '<w:p><w:pPr><w:spacing w:before="0" w:after="0"/><w:jc w:val="center"/></w:pPr>' +
239
+ `<w:r><w:rPr>${p.bold ? '<w:b/>' : ''}${p.italic ? '<w:i/>' : ''}<w:color w:val="${p.color}"/><w:sz w:val="${p.sizeHalfPt}"/></w:rPr>` +
240
+ `<w:t xml:space="preserve">${escapeXml(p.text)}</w:t></w:r></w:p>`)
241
+ .join('');
242
+ return [
243
+ '<wps:wsp>',
244
+ ` <wps:cNvPr id="${id}" name="${escapeXml(name)}"/>`,
245
+ ' <wps:cNvSpPr/>',
246
+ ' <wps:spPr>',
247
+ ` <a:xfrm><a:off x="${box.x}" y="${box.y}"/><a:ext cx="${Math.max(1, box.w)}" cy="${Math.max(1, box.h)}"/></a:xfrm>`,
248
+ ` ${geometry}`,
249
+ ` ${fillXml}`,
250
+ ` ${lnXml}`,
251
+ ' </wps:spPr>',
252
+ ...(paragraphs.length > 0 ? [' <wps:txbx>', ` <w:txbxContent>${body}</w:txbxContent>`, ' </wps:txbx>'] : []),
253
+ ` <wps:bodyPr wrap="square" lIns="${inset.lr}" tIns="${inset.t}" rIns="${inset.lr}" bIns="${inset.b}" anchor="${anchor}"/>`,
254
+ '</wps:wsp>',
255
+ ].join('\n');
256
+ }
257
+ const roundRectPreset = (w, h, radius) => `<a:prstGeom prst="roundRect"><a:avLst><a:gd name="adj" fmla="val ${Math.round((Math.min(radius, Math.min(w, h) / 2) / Math.min(w, h)) * 100000)}"/></a:avLst></a:prstGeom>`;
258
+ /** The geometry of an element `w` x `h` px (path units are px, scaled by DrawingML to the shape). */
259
+ function elementGeometry(el) {
260
+ const { w, h } = el;
261
+ if (el.element.category === 'person') {
262
+ const { r, overlap } = personHead(w);
263
+ const bodyTop = 2 * r - overlap;
264
+ const bodyH = h - bodyTop;
265
+ const bodyR = Math.min(w * 0.177, bodyH * 0.45);
266
+ return customGeometry(w, h, [
267
+ ['norm', roundedRectPath(0, bodyTop, w, bodyH, bodyR)],
268
+ ['norm', circlePath(w / 2, r, r)],
269
+ ]);
146
270
  }
147
- for (const rel of chart.relationships) {
148
- if (g.hasNode(nodeKey(rel.from)) && g.hasNode(nodeKey(rel.to)))
149
- g.setEdge(nodeKey(rel.from), nodeKey(rel.to));
271
+ if (el.element.variant === 'db') {
272
+ return `<a:prstGeom prst="can"><a:avLst><a:gd name="adj" fmla="val ${Math.round(((2 * DB_CAP) / Math.min(w, h)) * 100000)}"/></a:avLst></a:prstGeom>`;
150
273
  }
151
- layoutWithinLimits(g, options);
152
- let minX = Infinity;
153
- let maxX = -Infinity;
154
- let minY = Infinity;
155
- let maxY = -Infinity;
156
- for (const el of chart.elements) {
157
- const n = g.node(nodeKey(el.id));
158
- const size = sizes.get(el.id);
159
- minX = Math.min(minX, n.x - size.width / 2);
160
- maxX = Math.max(maxX, n.x + size.width / 2);
161
- minY = Math.min(minY, n.y - size.height / 2);
162
- maxY = Math.max(maxY, n.y + size.height / 2);
274
+ if (el.element.variant === 'queue')
275
+ return horizontalCylinderGeometry(w, h, QUEUE_CAP);
276
+ return roundRectPreset(w, h, CORNER);
277
+ }
278
+ /** Translate a parsed C4 diagram into a self-contained WordprocessingML paragraph. An empty diagram
279
+ * renders a visible note, never a silent blank canvas. */
280
+ export function translateC4DiagramToOoxml(chart, options = {}) {
281
+ if (chart.elements.length === 0 && (chart.boundaries ?? []).length === 0)
282
+ return noteParagraph('This C4 diagram has no content to render.');
283
+ // Lay out, find the scale the page imposes, then lay out again measuring text at the half-point sizes
284
+ // Word will get at that scale. The scale barely moves between passes; three is more than enough.
285
+ let laid = layoutDiagram(chart, asWritten);
286
+ let s = scaledExtent(laid.canvasW, laid.canvasH, options).scale;
287
+ for (let pass = 0; pass < 3 && s < 1; pass++) {
288
+ const scale = s;
289
+ // Only a size rounded up is measured larger; one rounded down keeps its proportional size, whose
290
+ // slack absorbs the gap between this width estimate and Word's own line breaking.
291
+ laid = layoutDiagram(chart, (pt) => Math.max(pt, scaledFontSizeHalfPt(halfPt(pt), scale) / 2 / scale));
292
+ const next = scaledExtent(laid.canvasW, laid.canvasH, options).scale;
293
+ if (next === s)
294
+ break;
295
+ s = next;
163
296
  }
164
- const PAD = 24;
165
- const hasTitle = Boolean(chart.title);
166
- const topMargin = hasTitle ? DIAGRAM_TITLE_H : 0;
167
- const canvasW = maxX - minX + 2 * PAD;
168
- const canvasH = maxY - minY + 2 * PAD + topMargin;
169
- const dx = -minX + PAD;
170
- const dy = -minY + PAD + topMargin;
171
- const { scale: s } = scaledExtent(canvasW, canvasH, options);
297
+ const { root, byId, elementsById, canvasW, canvasH } = laid;
298
+ const S = (px) => scalePt(px, s);
299
+ const sz = (pt) => scaledFontSizeHalfPt(halfPt(pt), s);
172
300
  const nextId = createIdAllocator();
173
301
  const parts = [];
174
302
  if (chart.title) {
175
- parts.push(textBoxLines(nextId(), 0, 0, scalePt(canvasW, s), scalePt(DIAGRAM_TITLE_H, s), [chart.title], scaledFontSizeHalfPt(DIAGRAM_TITLE_SIZE_HALFPT, s), {
176
- bold: true,
177
- align: 'ctr',
178
- }));
303
+ parts.push(textBoxLines(nextId(), 0, S(CANVAS_PAD / 2), S(canvasW), S(lineH(TITLE_PT)), [chart.title], sz(TITLE_PT), { bold: true, align: 'ctr' }));
304
+ }
305
+ // Frames, outermost first so an inner frame is drawn over its parent.
306
+ const drawFrame = (block) => {
307
+ const b = block.boundary;
308
+ const color = b.style?.text ?? FRAME_COLOR;
309
+ parts.push(shapeWithText(nextId(), b.label || b.id, { x: S(block.x), y: S(block.y), w: S(block.w), h: S(block.h) }, roundRectPreset(block.w, block.h, 3), b.style?.fill, { color: b.style?.line ?? FRAME_COLOR, emu: scaledLineWidthEmu(9525, s), dashed: b.kind === 'boundary' }, block.header.map((l) => ({ text: l.text, sizeHalfPt: sz(l.pt), bold: l.bold, color })), { t: S(FRAME_PAD / 2), b: 0, lr: S(8) }, 't'));
310
+ for (const child of block.children)
311
+ drawFrame(child);
312
+ };
313
+ for (const child of root.children)
314
+ drawFrame(child);
315
+ for (const el of elementsById.values()) {
316
+ const e = el.element;
317
+ const [defFill, defLine] = PALETTE[e.category][e.external ? 'external' : 'internal'];
318
+ const color = e.style?.text ?? ELEMENT_TEXT;
319
+ const paragraphs = el.lines.map((l) => ({ text: l.text, sizeHalfPt: sz(l.pt), bold: l.bold, color }));
320
+ parts.push(shapeWithText(nextId(), e.label.split('\n')[0] || e.id, { x: S(el.x), y: S(el.y), w: S(el.w), h: S(el.h) }, elementGeometry(el), e.style?.fill ?? defFill, { color: e.style?.line ?? defLine, emu: scaledLineWidthEmu(19050, s) }, paragraphs,
321
+ // Word centres the text in the body: the insets keep it off a head or a cylinder's ends.
322
+ { t: S(el.bodyTop), b: S(el.bodyBottom), lr: S(ELEMENT_PAD_X / 2) }, 'ctr'));
179
323
  }
180
- // Group relationships by their unordered node pair so 2+ relationships
181
- // between the same two boxes push apart instead of drawing on top of each
182
- // other — see parallelEdgeOffset()'s doc comment for the real-render bug
183
- // this fixes (first found on requirementDiagram; the same geometry risk
184
- // applies here, e.g. a request/response pair of Rels between the same two
185
- // containers).
324
+ // Each relationship's endpoints: an element's box, or a frame's.
325
+ const boxOf = (id) => elementsById.get(id) ?? byId.get(id);
186
326
  const pairGroups = new Map();
187
327
  chart.relationships.forEach((rel, i) => {
188
328
  const key = [rel.from, rel.to].sort().join('|');
189
- const group = pairGroups.get(key) ?? [];
190
- group.push(i);
191
- pairGroups.set(key, group);
329
+ pairGroups.set(key, [...(pairGroups.get(key) ?? []), i]);
192
330
  });
193
- const offsetIndexByRel = new Map();
194
- for (const group of pairGroups.values()) {
195
- group.forEach((relIndex, i) => offsetIndexByRel.set(relIndex, { index: i, count: group.length }));
196
- }
331
+ const labels = [];
197
332
  chart.relationships.forEach((rel, relIndex) => {
198
- const nFrom = g.node(nodeKey(rel.from));
199
- const nTo = g.node(nodeKey(rel.to));
200
- if (!nFrom || !nTo)
333
+ const a = boxOf(rel.from);
334
+ const b = boxOf(rel.to);
335
+ if (!a || !b || a === b)
201
336
  return;
202
- const sizeFrom = sizes.get(rel.from);
203
- const sizeTo = sizes.get(rel.to);
204
- const rawP1 = edgePoint(nFrom.x, nFrom.y, sizeFrom.width / 2, sizeFrom.height / 2, nTo.x, nTo.y);
205
- const rawP2 = edgePoint(nTo.x, nTo.y, sizeTo.width / 2, sizeTo.height / 2, nFrom.x, nFrom.y);
206
- const { index, count } = offsetIndexByRel.get(relIndex);
207
- const sortedPair = [rel.from, rel.to].sort();
208
- const posA = g.node(nodeKey(sortedPair[0]));
209
- const posB = g.node(nodeKey(sortedPair[1]));
337
+ const ca = { x: a.x + a.w / 2, y: a.y + a.h / 2 };
338
+ const cb = { x: b.x + b.w / 2, y: b.y + b.h / 2 };
339
+ const group = pairGroups.get([rel.from, rel.to].sort().join('|'));
340
+ const [first, second] = [rel.from, rel.to].sort();
341
+ const posA = first === rel.from ? ca : cb;
342
+ const posB = second === rel.to ? cb : ca;
210
343
  const { ux, uy } = perpendicularUnit(posA, posB);
211
- const amount = parallelEdgeOffset(index, count);
212
- const p1 = shiftPoint(rawP1, ux, uy, amount);
213
- const p2 = shiftPoint(rawP2, ux, uy, amount);
214
- parts.push(connector(nextId(), scalePt(p1.x + dx, s), scalePt(p1.y + dy, s), scalePt(p2.x + dx, s), scalePt(p2.y + dy, s), LINE_COLOR, scaledLineWidthEmu(9525, s), 'solid', rel.bidirectional ? 'triangle' : 'none', 'sm', 'triangle', 'sm'));
215
- const label = rel.techn ? `${rel.label} [${rel.techn}]` : rel.label;
216
- if (label.length === 0)
344
+ const amount = parallelEdgeOffset(group.indexOf(relIndex), group.length);
345
+ const p1 = shiftPoint(edgePoint(ca.x, ca.y, a.w / 2, a.h / 2, cb.x, cb.y), ux, uy, amount);
346
+ const p2 = shiftPoint(edgePoint(cb.x, cb.y, b.w / 2, b.h / 2, ca.x, ca.y), ux, uy, amount);
347
+ const lineColor = rel.style?.line ?? REL_COLOR;
348
+ parts.push(connector(nextId(), S(p1.x), S(p1.y), S(p2.x), S(p2.y), lineColor, scaledLineWidthEmu(9525, s), 'solid', rel.bidirectional ? 'triangle' : 'none', 'sm', 'triangle', 'sm'));
349
+ const label = chart.level === 'dynamic' ? `${relIndex + 1}: ${rel.label}` : rel.label;
350
+ const textColor = rel.style?.text ?? REL_COLOR;
351
+ const midX = (p1.x + p2.x) / 2 + (rel.style?.offsetX ?? 0);
352
+ const midY = (p1.y + p2.y) / 2 + (rel.style?.offsetY ?? 0);
353
+ const rows = [
354
+ ...(label ? label.split('\n').map((text) => ({ text, italic: false })) : []),
355
+ ...(rel.techn ? `[${rel.techn}]`.split('\n').map((text) => ({ text, italic: true })) : []),
356
+ ];
357
+ if (rows.length === 0)
217
358
  return;
218
- // Same "along + perpendicular" double-offset as requirementDiagram's
219
- // translator — perpendicular separation between lines alone isn't
220
- // reliably wide enough to keep 2+ relationships' labels apart (found on
221
- // that fixture's real render; the same geometry risk applies here).
222
- const alongStepPx = 16;
223
- const canonicalDx = posB.x - posA.x;
224
- const canonicalDy = posB.y - posA.y;
225
- const canonicalLen = Math.hypot(canonicalDx, canonicalDy) || 1;
226
- const alongShift = count > 1 ? (index - (count - 1) / 2) * alongStepPx : 0;
227
- const midX = (p1.x + p2.x) / 2 + (canonicalDx / canonicalLen) * alongShift;
228
- const midY = (p1.y + p2.y) / 2 + (canonicalDy / canonicalLen) * alongShift;
229
- const labelWidth = estimateTextWidth(label, 8) + 12;
230
- parts.push(textBoxLines(nextId(), scalePt(midX + dx - labelWidth / 2, s), scalePt(midY + dy - 10, s), scalePt(labelWidth, s), scalePt(20, s), [label], scaledFontSizeHalfPt(LABEL_SIZE_HALFPT, s), { align: 'ctr' }));
359
+ const rowH = lineH(REL_PT);
360
+ const top = midY - rowH / 2;
361
+ rows.forEach((row, i) => {
362
+ const w = estimateTextWidth(row.text, pxOf(REL_PT)) + 12;
363
+ labels.push(textBoxLines(nextId(), S(midX - w / 2), S(top + i * rowH), S(w), S(rowH), [row.text], sz(REL_PT), { align: 'ctr', italic: row.italic, color: textColor }));
364
+ });
231
365
  });
232
- for (const el of chart.elements) {
233
- const n = g.node(nodeKey(el.id));
234
- const size = sizes.get(el.id);
235
- parts.push(...elementShapes(nextId, el, bodyLinesById.get(el.id), size, n.x - size.width / 2 + dx, n.y - size.height / 2 + dy, s));
236
- }
237
- const content = parts.join('\n');
366
+ parts.push(...labels);
238
367
  const docPrId = nextId();
239
- return wrapDrawingCanvas(content, canvasW, canvasH, docPrId, chart.title ?? 'C4 diagram', options);
368
+ return wrapDrawingCanvas(parts.join('\n'), canvasW, canvasH, docPrId, chart.title ?? 'C4 diagram', options);
240
369
  }
241
370
  //# sourceMappingURL=translator.js.map