@fengwk/kchart 0.0.0-stage → 0.1.1

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 (201) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/LICENSE +21 -0
  3. package/NOTICE.md +11 -0
  4. package/README.md +68 -2
  5. package/README.zh-CN.md +69 -0
  6. package/dist/LICENSE +21 -0
  7. package/dist/NOTICE.md +11 -0
  8. package/dist/assets/ICONS-LICENSE.txt +53 -0
  9. package/dist/assets/maps/LICENSE.md +75 -0
  10. package/dist/assets/maps/METADATA.md +9 -0
  11. package/dist/assets/maps/REGIONS.md +28 -0
  12. package/dist/assets/maps/us-states.geojson +1 -0
  13. package/dist/assets/maps/world.geojson +1 -0
  14. package/dist/esm/_virtual/_kchart-families.js +48 -0
  15. package/dist/esm/_virtual/_rolldown/runtime.js +12 -0
  16. package/dist/esm/assets/icons.js +11 -0
  17. package/dist/esm/assets/maps/us-states.js +3 -0
  18. package/dist/esm/assets/maps/world.js +3 -0
  19. package/dist/esm/assets/themes/claude.js +69 -0
  20. package/dist/esm/assets/themes/cloudmusic.js +62 -0
  21. package/dist/esm/assets/themes/kanagawa.js +67 -0
  22. package/dist/esm/assets/themes/obsidian.js +67 -0
  23. package/dist/esm/assets/themes/paper.js +61 -0
  24. package/dist/esm/assets/themes/terminal.js +60 -0
  25. package/dist/esm/assets/themes/tokyo.js +62 -0
  26. package/dist/esm/chart/basic.js +1313 -0
  27. package/dist/esm/chart/catalog.js +476 -0
  28. package/dist/esm/chart/composition.js +844 -0
  29. package/dist/esm/chart/context.js +189 -0
  30. package/dist/esm/chart/distribution.js +442 -0
  31. package/dist/esm/chart/fields.js +67 -0
  32. package/dist/esm/chart/maps.js +563 -0
  33. package/dist/esm/chart/mindmap.js +629 -0
  34. package/dist/esm/chart/multivariate.js +1229 -0
  35. package/dist/esm/chart/registry.js +108 -0
  36. package/dist/esm/chart/relations.js +949 -0
  37. package/dist/esm/chart/spec.js +1415 -0
  38. package/dist/esm/chart/stat-compare.js +302 -0
  39. package/dist/esm/chart/stat-trend.js +181 -0
  40. package/dist/esm/chart/statistics.js +1007 -0
  41. package/dist/esm/chart/unit-variants.js +645 -0
  42. package/dist/esm/chart/variants.js +552 -0
  43. package/dist/esm/core/color.js +197 -0
  44. package/dist/esm/core/context.js +256 -0
  45. package/dist/esm/core/diagnostics.js +23 -0
  46. package/dist/esm/core/format.js +167 -0
  47. package/dist/esm/core/geometry.js +130 -0
  48. package/dist/esm/core/icon-data.js +5 -0
  49. package/dist/esm/core/icons.js +173 -0
  50. package/dist/esm/core/scale.js +112 -0
  51. package/dist/esm/core/scene.js +234 -0
  52. package/dist/esm/core/svg.js +89 -0
  53. package/dist/esm/core/text.js +184 -0
  54. package/dist/esm/core/theme.js +318 -0
  55. package/dist/esm/diagram/agentflow.js +596 -0
  56. package/dist/esm/diagram/architecture.js +729 -0
  57. package/dist/esm/diagram/block.js +988 -0
  58. package/dist/esm/diagram/blocks.js +687 -0
  59. package/dist/esm/diagram/c4.js +1002 -0
  60. package/dist/esm/diagram/class.js +1158 -0
  61. package/dist/esm/diagram/cynefin.js +749 -0
  62. package/dist/esm/diagram/emphasis.js +7 -0
  63. package/dist/esm/diagram/er.js +657 -0
  64. package/dist/esm/diagram/eventmodeling.js +948 -0
  65. package/dist/esm/diagram/gantt-date.js +323 -0
  66. package/dist/esm/diagram/gantt.js +661 -0
  67. package/dist/esm/diagram/gitgraph.js +472 -0
  68. package/dist/esm/diagram/graph-layout.js +4046 -0
  69. package/dist/esm/diagram/graph-parse.js +1366 -0
  70. package/dist/esm/diagram/graph-route.js +490 -0
  71. package/dist/esm/diagram/graph.js +553 -0
  72. package/dist/esm/diagram/ishikawa.js +496 -0
  73. package/dist/esm/diagram/journey.js +512 -0
  74. package/dist/esm/diagram/kanban.js +280 -0
  75. package/dist/esm/diagram/label.js +56 -0
  76. package/dist/esm/diagram/layers.js +385 -0
  77. package/dist/esm/diagram/map-label.js +44 -0
  78. package/dist/esm/diagram/mindmap.js +277 -0
  79. package/dist/esm/diagram/numeric-composition.js +78 -0
  80. package/dist/esm/diagram/numeric-model.js +86 -0
  81. package/dist/esm/diagram/numeric-parse.js +205 -0
  82. package/dist/esm/diagram/numeric-radar.js +355 -0
  83. package/dist/esm/diagram/numeric-sankey.js +201 -0
  84. package/dist/esm/diagram/numeric-xy.js +422 -0
  85. package/dist/esm/diagram/packet.js +175 -0
  86. package/dist/esm/diagram/presentation.js +252 -0
  87. package/dist/esm/diagram/quadrant.js +297 -0
  88. package/dist/esm/diagram/railroad-parse.js +465 -0
  89. package/dist/esm/diagram/railroad.js +294 -0
  90. package/dist/esm/diagram/registry.js +296 -0
  91. package/dist/esm/diagram/relations.js +966 -0
  92. package/dist/esm/diagram/requirement.js +341 -0
  93. package/dist/esm/diagram/sequence.js +1228 -0
  94. package/dist/esm/diagram/shapes.js +366 -0
  95. package/dist/esm/diagram/source.js +538 -0
  96. package/dist/esm/diagram/support.js +153 -0
  97. package/dist/esm/diagram/swimlane-mermaid.js +140 -0
  98. package/dist/esm/diagram/swimlane.js +1186 -0
  99. package/dist/esm/diagram/time.js +748 -0
  100. package/dist/esm/diagram/timeline.js +376 -0
  101. package/dist/esm/diagram/tree-family-icon.js +22 -0
  102. package/dist/esm/diagram/treeview.js +293 -0
  103. package/dist/esm/diagram/usecase.js +852 -0
  104. package/dist/esm/diagram/venn.js +1038 -0
  105. package/dist/esm/diagram/wardley.js +838 -0
  106. package/dist/esm/index.d.ts +133 -0
  107. package/dist/esm/index.js +92 -0
  108. package/dist/esm/interaction.js +561 -0
  109. package/dist/esm/mermaid.d.ts +28 -0
  110. package/dist/esm/mermaid.js +70 -0
  111. package/dist/esm/node.d.ts +68 -0
  112. package/dist/esm/node.js +373 -0
  113. package/dist/esm/package.json +1 -0
  114. package/dist/esm/page-api.js +65 -0
  115. package/dist/esm/runtime.js +352 -0
  116. package/dist/esm/src/families.js +209 -0
  117. package/dist/page-api.js +1 -0
  118. package/dist/units/chart/basic.js +1 -0
  119. package/dist/units/chart/catalog.js +1 -0
  120. package/dist/units/chart/composition.js +1 -0
  121. package/dist/units/chart/context.js +1 -0
  122. package/dist/units/chart/distribution.js +1 -0
  123. package/dist/units/chart/fields.js +1 -0
  124. package/dist/units/chart/maps.js +1 -0
  125. package/dist/units/chart/mindmap.js +1 -0
  126. package/dist/units/chart/multivariate.js +1 -0
  127. package/dist/units/chart/registry.js +1 -0
  128. package/dist/units/chart/relations.js +1 -0
  129. package/dist/units/chart/spec.js +3 -0
  130. package/dist/units/chart/stat-compare.js +1 -0
  131. package/dist/units/chart/stat-trend.js +1 -0
  132. package/dist/units/chart/statistics.js +1 -0
  133. package/dist/units/chart/unit-variants.js +1 -0
  134. package/dist/units/chart/variants.js +1 -0
  135. package/dist/units/core/color.js +1 -0
  136. package/dist/units/core/context.js +1 -0
  137. package/dist/units/core/diagnostics.js +1 -0
  138. package/dist/units/core/format.js +1 -0
  139. package/dist/units/core/geometry.js +1 -0
  140. package/dist/units/core/icon-data.js +1 -0
  141. package/dist/units/core/icons.js +1 -0
  142. package/dist/units/core/scale.js +1 -0
  143. package/dist/units/core/scene.js +1 -0
  144. package/dist/units/core/svg.js +1 -0
  145. package/dist/units/core/text.js +2 -0
  146. package/dist/units/core/theme.js +1 -0
  147. package/dist/units/diagram/agentflow.js +12 -0
  148. package/dist/units/diagram/architecture.js +4 -0
  149. package/dist/units/diagram/block.js +2 -0
  150. package/dist/units/diagram/blocks.js +2 -0
  151. package/dist/units/diagram/c4.js +6 -0
  152. package/dist/units/diagram/class.js +5 -0
  153. package/dist/units/diagram/cynefin.js +4 -0
  154. package/dist/units/diagram/emphasis.js +1 -0
  155. package/dist/units/diagram/er.js +9 -0
  156. package/dist/units/diagram/eventmodeling.js +5 -0
  157. package/dist/units/diagram/gantt-date.js +1 -0
  158. package/dist/units/diagram/gantt.js +3 -0
  159. package/dist/units/diagram/gitgraph.js +2 -0
  160. package/dist/units/diagram/graph-layout.js +4 -0
  161. package/dist/units/diagram/graph-parse.js +9 -0
  162. package/dist/units/diagram/graph-route.js +1 -0
  163. package/dist/units/diagram/graph.js +1 -0
  164. package/dist/units/diagram/ishikawa.js +2 -0
  165. package/dist/units/diagram/journey.js +5 -0
  166. package/dist/units/diagram/kanban.js +2 -0
  167. package/dist/units/diagram/label.js +4 -0
  168. package/dist/units/diagram/layers.js +1 -0
  169. package/dist/units/diagram/map-label.js +1 -0
  170. package/dist/units/diagram/mindmap.js +2 -0
  171. package/dist/units/diagram/numeric-composition.js +1 -0
  172. package/dist/units/diagram/numeric-model.js +1 -0
  173. package/dist/units/diagram/numeric-parse.js +2 -0
  174. package/dist/units/diagram/numeric-radar.js +3 -0
  175. package/dist/units/diagram/numeric-sankey.js +2 -0
  176. package/dist/units/diagram/numeric-xy.js +2 -0
  177. package/dist/units/diagram/packet.js +2 -0
  178. package/dist/units/diagram/presentation.js +2 -0
  179. package/dist/units/diagram/quadrant.js +2 -0
  180. package/dist/units/diagram/railroad-parse.js +4 -0
  181. package/dist/units/diagram/railroad.js +1 -0
  182. package/dist/units/diagram/registry.js +2 -0
  183. package/dist/units/diagram/relations.js +3 -0
  184. package/dist/units/diagram/requirement.js +3 -0
  185. package/dist/units/diagram/sequence.js +2 -0
  186. package/dist/units/diagram/shapes.js +60 -0
  187. package/dist/units/diagram/source.js +20 -0
  188. package/dist/units/diagram/support.js +3 -0
  189. package/dist/units/diagram/swimlane-mermaid.js +3 -0
  190. package/dist/units/diagram/swimlane.js +2 -0
  191. package/dist/units/diagram/time.js +3 -0
  192. package/dist/units/diagram/timeline.js +2 -0
  193. package/dist/units/diagram/tree-family-icon.js +1 -0
  194. package/dist/units/diagram/treeview.js +2 -0
  195. package/dist/units/diagram/usecase.js +3 -0
  196. package/dist/units/diagram/venn.js +2 -0
  197. package/dist/units/diagram/wardley.js +4 -0
  198. package/dist/units/interaction.js +1 -0
  199. package/dist/units/runtime.js +1 -0
  200. package/dist/units.json +712 -0
  201. package/package.json +30 -4
@@ -0,0 +1,1002 @@
1
+ import { __exportAll } from "../_virtual/_rolldown/runtime.js";
2
+ import { mix } from "../core/color.js";
3
+ import { resolveIconRef } from "../core/icons.js";
4
+ import { normalizeLabel } from "./label.js";
5
+ import { presentation, roleOfColor } from "./presentation.js";
6
+ import { FONT, error, isEn, warning } from "./support.js";
7
+ // src/diagram/c4.js
8
+ var c4_exports = /* @__PURE__ */ __exportAll({ implementations: () => implementations });
9
+ var TYPES = [
10
+ "C4Context",
11
+ "C4Container",
12
+ "C4Component",
13
+ "C4Dynamic",
14
+ "C4Deployment"
15
+ ];
16
+ var ELEMENTS = /* @__PURE__ */ new Map();
17
+ for (const [base, label, zh] of [
18
+ [
19
+ "Person",
20
+ "Person",
21
+ "人员"
22
+ ],
23
+ [
24
+ "System",
25
+ "Software System",
26
+ "软件系统"
27
+ ],
28
+ [
29
+ "Container",
30
+ "Container",
31
+ "容器"
32
+ ],
33
+ [
34
+ "Component",
35
+ "Component",
36
+ "组件"
37
+ ]
38
+ ]) for (const [suffix, shape] of base === "Person" ? [["", "person"]] : [
39
+ ["", base === "Component" ? "component" : "box"],
40
+ ["Db", "database"],
41
+ ["Queue", "queue"]
42
+ ]) for (const ext of ["", "_Ext"]) ELEMENTS.set(base + suffix + ext, {
43
+ label,
44
+ zh,
45
+ shape,
46
+ external: Boolean(ext),
47
+ tech: base === "Container" || base === "Component"
48
+ });
49
+ var BOUNDARIES = /* @__PURE__ */ new Map([
50
+ ["Enterprise_Boundary", "Enterprise"],
51
+ ["System_Boundary", "System"],
52
+ ["Container_Boundary", "Container"],
53
+ ["Boundary", "System"]
54
+ ]);
55
+ var FRAME_ZH = {
56
+ Enterprise: "企业",
57
+ System: "系统",
58
+ Container: "容器",
59
+ Node: "节点"
60
+ };
61
+ var DEPLOYMENT = /* @__PURE__ */ new Set([
62
+ "Deployment_Node",
63
+ "Node",
64
+ "Node_L",
65
+ "Node_R"
66
+ ]);
67
+ var RELS = new Map([
68
+ ["Rel", ""],
69
+ ["BiRel", ""],
70
+ ["Rel_Back", ""],
71
+ ["RelIndex", ""],
72
+ ...[
73
+ "Up",
74
+ "Down",
75
+ "Left",
76
+ "Right"
77
+ ].flatMap((dir) => [[`Rel_${dir}`, dir[0]], [`Rel_${dir[0]}`, dir[0]]])
78
+ ]);
79
+ var SHAPES = new Map(Object.entries({
80
+ person: "person",
81
+ box: "box",
82
+ rounded: "box",
83
+ cylinder: "database",
84
+ database: "database",
85
+ db: "database",
86
+ queue: "queue",
87
+ pipe: "queue",
88
+ component: "component"
89
+ }));
90
+ function argumentsOf(raw, slots) {
91
+ const args = [];
92
+ let start = 0, quoted = false, depth = 0;
93
+ for (let i = 0; i < raw.length; i++) {
94
+ const c = raw[i];
95
+ if (quoted && c === "\\") {
96
+ i++;
97
+ continue;
98
+ }
99
+ if (c === "\"") quoted = !quoted;
100
+ else if (!quoted && c === "(") depth++;
101
+ else if (!quoted && c === ")") depth--;
102
+ else if (!quoted && !depth && c === ",") {
103
+ args.push(raw.slice(start, i));
104
+ start = i + 1;
105
+ }
106
+ if (depth < 0) return null;
107
+ }
108
+ if (quoted || depth) return null;
109
+ args.push(raw.slice(start));
110
+ const fields = Object.create(null);
111
+ for (const [i, arg] of args.entries()) {
112
+ const named = /^\s*\$(\w+)\s*=\s*([\s\S]*)$/.exec(arg);
113
+ const value = (named ? named[2] : arg).trim();
114
+ const slot = named ? named[1] : slots[i];
115
+ if (!slot) {
116
+ if (value) return null;
117
+ continue;
118
+ }
119
+ fields[slot] = value.replace(/^"([\s\S]*)"$/, "$1").replace(/\\(["\\])/g, "$1");
120
+ }
121
+ return fields;
122
+ }
123
+ function* statements(source, firstLine) {
124
+ let text = "", line = firstLine, start = line, quoted = false, depth = 0;
125
+ for (let i = 0; i <= source.length; i++) {
126
+ const c = source[i] ?? "\n";
127
+ if (!quoted && source.startsWith("%%", i)) {
128
+ const end = source.indexOf("\n", i);
129
+ i = (end < 0 ? source.length : end) - 1;
130
+ continue;
131
+ }
132
+ if (quoted && c === "\\" && /["\\]/.test(source[i + 1] ?? "")) {
133
+ text += c + source[++i];
134
+ continue;
135
+ }
136
+ const prose = /^\s*(?:title\s|accTitle\s*:|accDescr\s*:)/i.test(text);
137
+ const style = /^\s*(?:classDef|style|linkStyle)\s/i.test(text);
138
+ if (c === "\"" && !prose) quoted = !quoted;
139
+ if (!quoted && !prose && c === "(") depth++;
140
+ if (!quoted && !prose && c === ")") depth--;
141
+ if (!quoted && !prose && !depth && c === "{" && !/^\s*accDescr\s*$/i.test(text)) {
142
+ yield {
143
+ text: text.trim() + "{",
144
+ line: start
145
+ };
146
+ text = "";
147
+ } else if (!quoted && !prose && !depth && c === "}") {
148
+ if (/^\s*accDescr\s*\{/i.test(text)) yield {
149
+ text: text.trim() + "}",
150
+ line: start
151
+ };
152
+ else {
153
+ if (text.trim()) yield {
154
+ text: text.trim(),
155
+ line: start
156
+ };
157
+ yield {
158
+ text: "}",
159
+ line
160
+ };
161
+ }
162
+ text = "";
163
+ } else if ((c === "\n" || c === ";" && !prose && !style) && !quoted && depth <= 0 && !/^\s*accDescr\s*\{/i.test(text)) {
164
+ if (text.trim() && !(c === "\n" && /^\s*\{/.test(source.slice(i + 1)) && /\)\s*$/.test(text))) {
165
+ yield {
166
+ text: text.trim(),
167
+ line: start
168
+ };
169
+ text = "";
170
+ depth = 0;
171
+ }
172
+ } else {
173
+ if (!text.trim() && /\S/.test(c)) start = line;
174
+ text += c;
175
+ }
176
+ if (c === "\n") line++;
177
+ }
178
+ if (text.trim()) yield {
179
+ text: text.trim(),
180
+ line: start
181
+ };
182
+ }
183
+ var ASSETS = [
184
+ "sprite",
185
+ "tags",
186
+ "link"
187
+ ];
188
+ var ROWS = {
189
+ c4ShapeInRow: 4,
190
+ c4BoundaryInRow: 2
191
+ };
192
+ var honoredConfig = Object.keys(ROWS).map((name) => `c4.${name}`);
193
+ var configKey = (map, name) => Object.entries(map ?? {}).find(([key]) => key.toLowerCase() === name.toLowerCase())?.[1];
194
+ var setCount = (config, key, raw) => {
195
+ const value = Number.parseInt(raw, 10);
196
+ if (Number.isSafeInteger(value) && value >= 1) config[key] = value;
197
+ };
198
+ var inline = /\s*(?::::(?<class>[\w-]+)|::icon\(\s*(?<icon>[\w:-]+)\s*\))$/i;
199
+ var iconRef = /::icon\(\s*([\w:-]+)\s*\)|(?<![\w:-])((?:logos|mdi|tabler|simple-icons):[\w-]+)/gi;
200
+ function parse(source, { kind, lang, firstLine = 2, config: sharedConfig }) {
201
+ const items = /* @__PURE__ */ new Map(), rels = /* @__PURE__ */ new Map(), errors = [], stack = [], notices = /* @__PURE__ */ new Map();
202
+ const config = { ...ROWS }, look = presentation({ lang }), assetWarnings = /* @__PURE__ */ new Set();
203
+ const c4Config = configKey(sharedConfig, "c4");
204
+ for (const key of Object.keys(config)) setCount(config, key, configKey(c4Config, key));
205
+ const warnAsset = (diagnostic) => {
206
+ if (!assetWarnings.has(diagnostic.message)) {
207
+ assetWarnings.add(diagnostic.message);
208
+ errors.push(diagnostic);
209
+ }
210
+ };
211
+ const iconOf = (ref, line) => {
212
+ const icon = resolveIconRef(ref);
213
+ if (!icon) warnAsset(warning("IGNORED_ASSET", lang, line, `未知图标 ${ref}`, `Unknown icon ${ref}`, "改用内置图标", "Use a supported icon"));
214
+ return icon;
215
+ };
216
+ const normalize = (raw, line) => {
217
+ let icon = null;
218
+ const clean = raw.replace(iconRef, (_, call, pack) => {
219
+ const resolved = iconOf(call || pack, line);
220
+ icon ??= resolved;
221
+ return "";
222
+ });
223
+ const result = normalizeLabel(clean, {
224
+ lang,
225
+ line
226
+ });
227
+ result.diagnostics.forEach(warnAsset);
228
+ return {
229
+ text: result.text,
230
+ icon: icon ?? result.icon
231
+ };
232
+ };
233
+ let title;
234
+ const bad = (line, zh, en) => errors.push(error(lang, line, zh, en, "检查 C4 声明及其参数", "Check the C4 declaration and its arguments"));
235
+ const note = (code, line, fields) => {
236
+ if (!fields.length) return;
237
+ if (!notices.has(code)) notices.set(code, {
238
+ line,
239
+ fields: /* @__PURE__ */ new Set()
240
+ });
241
+ for (const field of fields) notices.get(code).fields.add(field);
242
+ };
243
+ if (source.length > 2e5) {
244
+ bad(firstLine, "C4 源文本过长", "C4 source is too long");
245
+ return {
246
+ model: null,
247
+ errors
248
+ };
249
+ }
250
+ for (const statement of statements(source, firstLine)) {
251
+ let { text } = statement;
252
+ const { line } = statement;
253
+ if (look.accept(text, line)) continue;
254
+ if (/^title\s+/i.test(text)) {
255
+ title = normalize(text.replace(/^title\s+/i, ""), line).text;
256
+ continue;
257
+ }
258
+ if (/^acc(?:Title|Descr)\s*:|^accDescr\s*\{[\s\S]*\}$/i.test(text)) continue;
259
+ if (text === "}") {
260
+ if (stack.length) stack.pop();
261
+ else bad(line, "没有对应的边界起点", "Unmatched boundary close");
262
+ continue;
263
+ }
264
+ const opens = text.endsWith("{");
265
+ if (opens) text = text.slice(0, -1).trimEnd();
266
+ const suffixes = [];
267
+ for (let part; part = inline.exec(text); text = text.slice(0, part.index).trimEnd()) suffixes.unshift(part.groups);
268
+ const [, macro, raw] = /^(\w+)\s*\(([\s\S]*)\)$/.exec(text) || [];
269
+ const element = ELEMENTS.get(macro), deploy = DEPLOYMENT.has(macro), boundary = BOUNDARIES.get(macro);
270
+ const relation = RELS.has(macro);
271
+ const styleFields = macro === "UpdateElementStyle" ? [
272
+ "alias",
273
+ "bgColor",
274
+ "fontColor",
275
+ "borderColor",
276
+ "shadowing",
277
+ "shape",
278
+ "sprite",
279
+ "techn",
280
+ "legendText",
281
+ "legendSprite"
282
+ ] : macro === "UpdateRelStyle" ? [
283
+ "from",
284
+ "to",
285
+ "textColor",
286
+ "lineColor",
287
+ "offsetX",
288
+ "offsetY"
289
+ ] : null;
290
+ const slots = element ? [
291
+ "alias",
292
+ "label",
293
+ ...element.tech ? ["techn"] : [],
294
+ "descr",
295
+ ...ASSETS
296
+ ] : deploy ? [
297
+ "alias",
298
+ "label",
299
+ "type",
300
+ "descr",
301
+ ...ASSETS
302
+ ] : boundary ? [
303
+ "alias",
304
+ "label",
305
+ ...macro === "Boundary" ? ["type"] : [],
306
+ "tags",
307
+ "link"
308
+ ] : relation ? [
309
+ ...macro === "RelIndex" ? ["index"] : [],
310
+ "from",
311
+ "to",
312
+ "label",
313
+ "techn",
314
+ "descr",
315
+ ...ASSETS
316
+ ] : styleFields ?? (macro === "UpdateLayoutConfig" ? Object.keys(config) : null);
317
+ if (!slots && /^(?:Legend|Lay_[UDLR]|Add(?:Element|Rel)Tag|\w+Shape)$/.test(macro ?? "")) {
318
+ note("IGNORED_STYLE", line, [macro]);
319
+ continue;
320
+ }
321
+ const args = slots && argumentsOf(raw, slots);
322
+ if (!args || opens && !boundary && !deploy || boundary && !opens) {
323
+ bad(line, "无法识别 C4 语句", "Unrecognized C4 statement");
324
+ continue;
325
+ }
326
+ const { from, to } = args;
327
+ const [alias, ...classes] = (args.alias ?? "").split(":::");
328
+ const key = JSON.stringify([from, to]);
329
+ if (macro === "UpdateLayoutConfig") for (const name of slots) setCount(config, name, args[name]);
330
+ else if (styleFields) {
331
+ const target = macro === "UpdateElementStyle" ? items.get(alias) : rels.get(key);
332
+ const shape = SHAPES.get(args.shape?.toLowerCase());
333
+ const colors = [
334
+ "bgColor",
335
+ "fontColor",
336
+ "borderColor",
337
+ "textColor",
338
+ "lineColor"
339
+ ];
340
+ if (target) {
341
+ for (const field of colors) if (args[field]) target[field] = roleOfColor(args[field]);
342
+ if (shape && !target.frame) target.shape = shape;
343
+ if (args.techn !== void 0) target.techn = args.techn;
344
+ for (const dim of ["offsetX", "offsetY"]) {
345
+ const value = Number.parseInt(args[dim], 10);
346
+ if (Number.isFinite(value)) target[dim] = value;
347
+ }
348
+ }
349
+ note("IGNORED_STYLE", line, styleFields.filter((field) => args[field] && !(colors.includes(field) && target?.[field]) && ![
350
+ "alias",
351
+ "from",
352
+ "to",
353
+ "techn",
354
+ "offsetX",
355
+ "offsetY"
356
+ ].includes(field) && !(field === "shape" && shape && target && !target.frame)));
357
+ } else if (relation) {
358
+ if (!from || !to) {
359
+ bad(line, "关系缺少端点", "Relationship is missing an endpoint");
360
+ continue;
361
+ }
362
+ rels.set(key, {
363
+ from,
364
+ to,
365
+ label: args.label,
366
+ techn: args.techn,
367
+ descr: args.descr,
368
+ line,
369
+ icon: suffixes.find((part) => part.icon)?.icon,
370
+ index: args.index,
371
+ hint: RELS.get(macro),
372
+ start: macro === "BiRel" || macro === "Rel_Back",
373
+ end: macro !== "Rel_Back"
374
+ });
375
+ note("IGNORED_STYLE", line, ASSETS.filter((field) => args[field]));
376
+ } else {
377
+ if (!alias || items.has(alias)) {
378
+ bad(line, "元素别名缺失或重复", "Missing or duplicate element alias");
379
+ continue;
380
+ }
381
+ const taggedShape = [
382
+ args.shape,
383
+ args.sprite,
384
+ ...(args.tags ?? "").split("+")
385
+ ].map((value) => SHAPES.get(value?.toLowerCase())).find(Boolean);
386
+ const item = {
387
+ id: alias,
388
+ label: args.label || alias,
389
+ techn: args.techn,
390
+ descr: args.descr,
391
+ type: deploy ? args.type || "Node" : boundary ? args.type || boundary : element.label,
392
+ zh: element?.zh ?? (args.type ? void 0 : FRAME_ZH[deploy ? "Node" : boundary]),
393
+ external: element?.external,
394
+ shape: taggedShape || element?.shape || "box",
395
+ parent: stack.at(-1)?.id,
396
+ frame: Boolean(opens),
397
+ align: macro === "Node_R" ? "right" : "left",
398
+ line
399
+ };
400
+ look.addClasses(alias, [...classes, ...suffixes.map((part) => part.class).filter(Boolean)], line);
401
+ item.icon = suffixes.find((part) => part.icon)?.icon;
402
+ items.set(alias, item);
403
+ if (opens) stack.push(item);
404
+ note("IGNORED_STYLE", line, ASSETS.filter((field) => args[field] && !SHAPES.has(args[field].toLowerCase())).concat(args.shape && !taggedShape ? ["shape"] : []));
405
+ }
406
+ if (items.size > 300 || rels.size > 300 || stack.length > 64) {
407
+ errors.push(error(lang, line, "C4 图最多 300 个元素、300 条关系、64 层边界", "C4 supports up to 300 elements, 300 relations and 64 boundary levels", "请拆分图表", "Split the diagram"));
408
+ break;
409
+ }
410
+ }
411
+ if (stack.length) bad(stack.at(-1).line, "C4 边界未关闭", "Unclosed C4 boundary");
412
+ for (const rel of rels.values()) for (const id of [rel.from, rel.to]) if (!items.has(id)) bad(rel.line, `未声明关系端点「${id}」`, `Undeclared relationship endpoint "${id}"`);
413
+ for (const entry of [...items.values(), ...rels.values()]) {
414
+ let icon = entry.icon ? iconOf(entry.icon, entry.line) : null;
415
+ for (const field of [
416
+ "label",
417
+ "type",
418
+ "techn",
419
+ "descr"
420
+ ]) {
421
+ if (!entry[field]) continue;
422
+ const result = normalize(entry[field], entry.line);
423
+ entry[field] = result.text;
424
+ icon ??= result.icon;
425
+ }
426
+ entry.icon = icon;
427
+ }
428
+ for (const item of items.values()) {
429
+ item.status = look.statusOf(item.id);
430
+ item.link = look.linkOf(item.id);
431
+ }
432
+ [...rels.values()].forEach((rel, index) => {
433
+ rel.status = look.edgeStatusOf(index);
434
+ });
435
+ errors.push(...look.diagnostics());
436
+ if (kind === "C4Dynamic") {
437
+ let order = 0;
438
+ for (const rel of rels.values()) rel.label = `${++order}: ${rel.label ?? ""}`;
439
+ }
440
+ for (const [code, { line, fields }] of notices) {
441
+ const list = [...fields].join(", ");
442
+ errors.push(warning(code, lang, line, `以下 C4 表现属性未应用:${list}`, `C4 presentation options not applied: ${list}`, "使用主题控制外观", "Use the theme to control appearance"));
443
+ }
444
+ return {
445
+ model: {
446
+ kind,
447
+ title,
448
+ items: [...items.values()],
449
+ rels: [...rels.values()],
450
+ config
451
+ },
452
+ errors
453
+ };
454
+ }
455
+ var PAD = 16;
456
+ var GUTTER = 64;
457
+ var CARD = 216;
458
+ var LINE = 17;
459
+ var cx = (b) => b.x + b.w / 2;
460
+ var cy = (b) => b.y + b.h / 2;
461
+ var inflate = (b, d) => ({
462
+ x: b.x - d,
463
+ y: b.y - d,
464
+ w: b.w + 2 * d,
465
+ h: b.h + 2 * d
466
+ });
467
+ var overlaps = (a, b) => a.x < b.x + b.w && b.x < a.x + a.w && a.y < b.y + b.h && b.y < a.y + a.h;
468
+ var contains = (a, b) => a.x <= b.x && a.y <= b.y && a.x + a.w >= b.x + b.w && a.y + a.h >= b.y + b.h;
469
+ var point = (x, y) => ({
470
+ x,
471
+ y
472
+ });
473
+ var distance = (a, b) => Math.hypot(a.x - b.x, a.y - b.y);
474
+ var lerp = (a, b, t) => point(a.x + (b.x - a.x) * t, a.y + (b.y - a.y) * t);
475
+ var segments = (path) => path.slice(1).map((p, i) => [path[i], p]);
476
+ function relLabel(rel, ctx) {
477
+ const lines = [
478
+ rel.label,
479
+ rel.techn && `[${rel.techn}]`,
480
+ rel.descr
481
+ ].filter(Boolean).flatMap((value) => ctx.wrap(value, 156, FONT.label.size));
482
+ const iconSpace = rel.icon ? 22 : 0;
483
+ if (!lines.length && !iconSpace) return null;
484
+ return {
485
+ lines,
486
+ iconSpace,
487
+ w: Math.max(iconSpace, ...lines.map((line) => ctx.measure(line, FONT.label.size, FONT.label.weight))) + 16,
488
+ h: lines.length * LINE + 8 + iconSpace
489
+ };
490
+ }
491
+ function makeBoxes(model, ctx, lang) {
492
+ const children = /* @__PURE__ */ new Map();
493
+ for (const item of model.items) {
494
+ if (!children.has(item.parent)) children.set(item.parent, []);
495
+ children.get(item.parent).push(item);
496
+ }
497
+ const rows = (value, font, width = 176) => value ? ctx.wrap(value, width, font.size, font.weight).map((text) => ({
498
+ text,
499
+ font
500
+ })) : [];
501
+ const parentOf = new Map(model.items.map((item) => [item.id, item.parent]));
502
+ const lineage = (id) => {
503
+ const ids = /* @__PURE__ */ new Set();
504
+ for (let at = id; at !== void 0 && !ids.has(at); at = parentOf.get(at)) ids.add(at);
505
+ return ids;
506
+ };
507
+ const labelled = model.rels.flatMap((rel) => {
508
+ const label = relLabel(rel, ctx);
509
+ return label ? [{
510
+ from: lineage(rel.from),
511
+ to: lineage(rel.to),
512
+ label
513
+ }] : [];
514
+ });
515
+ const room = (as, bs, size) => {
516
+ const a = new Set(as.map((box) => box.id)), b = new Set(bs.map((box) => box.id));
517
+ const reaches = (ids, side) => [...ids].some((id) => side.has(id));
518
+ return Math.max(GUTTER, ...labelled.filter(({ from, to }) => reaches(from, a) && reaches(to, b) || reaches(from, b) && reaches(to, a)).map(({ label }) => label[size] + 20));
519
+ };
520
+ const sized = (item) => {
521
+ const type = `${item.external ? isEn(lang) ? "External " : "外部" : ""}${!isEn(lang) && item.zh ? item.zh : item.type}`;
522
+ const techn = item.techn ? `${isEn(lang) ? ": " : ":"}${item.techn}` : "";
523
+ const lines = [
524
+ ...rows(item.label, FONT.title),
525
+ ...rows(`[${type}${techn}]`, FONT.label),
526
+ ...rows(item.descr, FONT.body)
527
+ ];
528
+ const inner = item.frame ? pack(item.id) : null;
529
+ const iconSpace = item.icon || item.shape === "person" && !item.frame ? 25 : 0;
530
+ const topSpace = iconSpace + (item.shape === "database" && !item.frame ? 8 : 0);
531
+ const header = 32 + lines.length * LINE + topSpace;
532
+ return {
533
+ ...item,
534
+ x: 0,
535
+ y: 0,
536
+ lines,
537
+ topSpace,
538
+ iconSpace,
539
+ header,
540
+ w: Math.max(CARD, (inner?.w ?? 0) + 32),
541
+ h: header + (inner ? inner.h + PAD : 0),
542
+ children: inner?.boxes ?? []
543
+ };
544
+ };
545
+ function pack(parent) {
546
+ const items = children.get(parent) ?? [];
547
+ const bands = [];
548
+ for (const [frame, limit] of [[false, model.config.c4ShapeInRow], [true, model.config.c4BoundaryInRow]]) {
549
+ const list = items.filter((item) => item.frame === frame);
550
+ for (let i = 0; i < list.length; i += limit) bands.push(list.slice(i, i + limit));
551
+ }
552
+ const boxes = [];
553
+ let w = 0, y = 0, above = null;
554
+ for (const band of bands) {
555
+ const placed = band.map(sized);
556
+ if (above) y += Math.max(...above.map((box) => box.h)) + room(above, placed, "h");
557
+ let x = 0;
558
+ placed.forEach((box, i) => {
559
+ if (i) x += placed[i - 1].w + room([placed[i - 1]], [box], "w");
560
+ Object.assign(box, {
561
+ x,
562
+ y
563
+ });
564
+ w = Math.max(w, x + box.w);
565
+ });
566
+ boxes.push(...placed);
567
+ above = placed;
568
+ }
569
+ return {
570
+ w,
571
+ h: above ? y + Math.max(...above.map((box) => box.h)) : 0,
572
+ boxes
573
+ };
574
+ }
575
+ return pack(void 0);
576
+ }
577
+ function hits([a, b], box) {
578
+ let lo = 0, hi = 1;
579
+ for (const [key, size] of [["x", "w"], ["y", "h"]]) {
580
+ const delta = b[key] - a[key], min = box[key] - a[key], max = min + box[size];
581
+ if (!delta) {
582
+ if (min > 0 || max < 0) return false;
583
+ continue;
584
+ }
585
+ const p = min / delta, q = max / delta;
586
+ lo = Math.max(lo, Math.min(p, q));
587
+ hi = Math.min(hi, Math.max(p, q));
588
+ if (lo > hi) return false;
589
+ }
590
+ return true;
591
+ }
592
+ var TRACK_GAP = 8;
593
+ function sharesChannel([a, b], [c, d]) {
594
+ const len = distance(a, b);
595
+ if (!len) return false;
596
+ const x = (b.x - a.x) / len, y = (b.y - a.y) / len;
597
+ const cross = (p) => x * (p.y - a.y) - y * (p.x - a.x);
598
+ const project = (p) => x * (p.x - a.x) + y * (p.y - a.y);
599
+ const p = project(c), q = project(d), offset = cross(c), rise = cross(d) - offset;
600
+ if (Math.abs(rise) > Math.abs(q - p) * .1 || p === q) return false;
601
+ const lo = Math.max(0, Math.min(p, q)), hi = Math.min(len, Math.max(p, q));
602
+ const gap = Math.max(...[lo, hi].map((u) => Math.abs(offset + (u - p) * rise / (q - p))));
603
+ return hi - lo > 3 && gap <= 1.1 || hi - lo > 8 && gap < TRACK_GAP;
604
+ }
605
+ function route(a, b, obstacles, existing, bounds, index) {
606
+ const clear = (path) => segments(path).every((seg) => !obstacles.some((box) => hits(seg, box)) && !existing.some((other) => sharesChannel(other, seg) || sharesChannel(seg, other)));
607
+ const attach = (box, toward) => {
608
+ const center = point(cx(box), cy(box));
609
+ return lerp(center, toward, Math.min(box.w / 2 / Math.abs(toward.x - center.x), box.h / 2 / Math.abs(toward.y - center.y)));
610
+ };
611
+ if (cx(a) !== cx(b) || cy(a) !== cy(b)) {
612
+ const direct = [attach(a, point(cx(b), cy(b))), attach(b, point(cx(a), cy(a)))];
613
+ if (clear(direct)) return direct;
614
+ }
615
+ let best, cost = Infinity;
616
+ const tryPath = (path) => {
617
+ path = path.filter((p, i) => !i || distance(p, path[i - 1]) > .01);
618
+ const score = segments(path).reduce((sum, [p, q]) => sum + distance(p, q), 0) + (path.length - 2) * 14;
619
+ if (path.length >= 2 && score < cost && clear(path)) {
620
+ best = path;
621
+ cost = score;
622
+ }
623
+ };
624
+ const port = (box, side, f) => side === 0 ? point(box.x, box.y + box.h * f) : side === 1 ? point(box.x + box.w, box.y + box.h * f) : side === 2 ? point(box.x + box.w * f, box.y) : point(box.x + box.w * f, box.y + box.h);
625
+ for (const f of [
626
+ .5,
627
+ .36,
628
+ .64,
629
+ .24,
630
+ .76
631
+ ]) {
632
+ const lane = 12 + index % 5 * 4;
633
+ for (let s = 0; s < 4; s++) for (let t = 0; t < 4; t++) {
634
+ if (a === b && s === t) continue;
635
+ const p = port(a, s, f), q = port(b, t, f);
636
+ const step = (r, side) => point(r.x + (side === 0 ? -lane : side === 1 ? lane : 0), r.y + (side === 2 ? -lane : side === 3 ? lane : 0));
637
+ const u = step(p, s), v = step(q, t);
638
+ tryPath([
639
+ p,
640
+ u,
641
+ v,
642
+ q
643
+ ]);
644
+ tryPath([
645
+ p,
646
+ u,
647
+ point(u.x, v.y),
648
+ v,
649
+ q
650
+ ]);
651
+ tryPath([
652
+ p,
653
+ u,
654
+ point(v.x, u.y),
655
+ v,
656
+ q
657
+ ]);
658
+ for (const x of [
659
+ (u.x + v.x) / 2,
660
+ bounds.x,
661
+ bounds.x + bounds.w,
662
+ u.x,
663
+ v.x
664
+ ]) tryPath([
665
+ p,
666
+ u,
667
+ point(x, u.y),
668
+ point(x, v.y),
669
+ v,
670
+ q
671
+ ]);
672
+ for (const y of [
673
+ (u.y + v.y) / 2,
674
+ bounds.y,
675
+ bounds.y + bounds.h,
676
+ u.y,
677
+ v.y
678
+ ]) tryPath([
679
+ p,
680
+ u,
681
+ point(u.x, y),
682
+ point(v.x, y),
683
+ v,
684
+ q
685
+ ]);
686
+ }
687
+ if (best) break;
688
+ }
689
+ return best;
690
+ }
691
+ function iconShape(ctx, icon, x, y, ink) {
692
+ if (!icon) return [];
693
+ const [l, t, w, h] = icon.viewBox.split(/\s+/).map(Number), scale = 18 / Math.max(w, h);
694
+ const solid = icon.set === "simple";
695
+ return [{
696
+ ...ctx.el("g", {
697
+ transform: `translate(${x - (l + w / 2) * scale} ${y - (t + h / 2) * scale}) scale(${scale})`,
698
+ fill: solid ? ink : "none",
699
+ stroke: solid ? "none" : ink,
700
+ "stroke-width": 1.6,
701
+ "stroke-linecap": "round",
702
+ "stroke-linejoin": "round"
703
+ }, icon.elements.map(({ tag, attrs }) => ctx.shape(tag, attrs))),
704
+ meta: { kind: "icon" }
705
+ }];
706
+ }
707
+ function drawBox(ctx, b) {
708
+ const { x, y, w, h } = b;
709
+ const status = b.borderColor || b.status || b.bgColor || (b.external ? "ext" : null);
710
+ const ink = ctx.design[status] ?? ctx.frameTone(mix(ctx.design.line2, ctx.design.fg3, b.frame ? .48 : .27));
711
+ const tint = b.bgColor || b.status;
712
+ const paint = {
713
+ fill: b.frame ? "none" : tint ? mix(ctx.design.surface, ctx.design[tint], .08) : ctx.design.surface,
714
+ stroke: ink,
715
+ "stroke-width": 1.25,
716
+ ...b.frame || b.external ? { "stroke-dasharray": b.frame ? "5 4" : "3 3" } : {}
717
+ };
718
+ const meta = {
719
+ kind: b.frame ? "frame" : "node",
720
+ id: b.id
721
+ };
722
+ let outline, details = [];
723
+ if (!b.frame && b.shape === "database") {
724
+ outline = ctx.shape("path", {
725
+ d: `M${x} ${y + 8} C${x} ${y - 3} ${x + w} ${y - 3} ${x + w} ${y + 8} V${y + h - 8} C${x + w} ${y + h + 3} ${x} ${y + h + 3} ${x} ${y + h - 8} Z`,
726
+ ...paint
727
+ }, meta);
728
+ details = [ctx.path(`M${x} ${y + 8} C${x} ${y + 19} ${x + w} ${y + 19} ${x + w} ${y + 8}`, {
729
+ fill: "none",
730
+ stroke: ink
731
+ })];
732
+ } else if (!b.frame && b.shape === "queue") {
733
+ outline = ctx.shape("path", {
734
+ d: `M${x + 10} ${y} H${x + w - 10} C${x + w + 3.33} ${y} ${x + w + 3.33} ${y + h} ${x + w - 10} ${y + h} H${x + 10} C${x - 3.33} ${y + h} ${x - 3.33} ${y} ${x + 10} ${y} Z`,
735
+ ...paint
736
+ }, meta);
737
+ details = [ctx.shape("ellipse", {
738
+ cx: x + 10,
739
+ cy: cy(b),
740
+ rx: 10,
741
+ ry: h / 2,
742
+ fill: "none",
743
+ stroke: ink
744
+ })];
745
+ } else {
746
+ outline = ctx.shape("rect", {
747
+ x,
748
+ y,
749
+ width: w,
750
+ height: h,
751
+ rx: b.frame ? 5 : 8,
752
+ ...paint
753
+ }, meta);
754
+ if (!b.frame && b.shape === "component") details = [ctx.rect(x + 8, y + 9, 4, h - 18, {
755
+ fill: ink,
756
+ rx: 2
757
+ })];
758
+ if (!b.frame && b.shape === "person" && !b.icon) details = [ctx.shape("circle", {
759
+ cx: cx(b),
760
+ cy: y + 15,
761
+ r: 5,
762
+ fill: ink
763
+ }), ctx.path(`M${cx(b) - 9} ${y + 32} C${cx(b) - 9} ${y + 21} ${cx(b) + 9} ${y + 21} ${cx(b) + 9} ${y + 32} Z`, { fill: ink })];
764
+ }
765
+ const alignRight = b.frame && b.align === "right";
766
+ const labels = b.lines.map(({ text, font }, i) => ctx.text(b.frame ? alignRight ? x + w - PAD : x + PAD : cx(b), y + PAD + b.topSpace + (i + .5) * LINE, text, {
767
+ anchor: b.frame ? alignRight ? "end" : "start" : "middle",
768
+ "dominant-baseline": "central",
769
+ size: font.size,
770
+ weight: font.weight,
771
+ fill: font === FONT.title ? ctx.design[b.fontColor] ?? ctx.design.fg1 : ctx.design.fg3
772
+ }));
773
+ const icons = iconShape(ctx, b.icon, b.frame ? alignRight ? x + w - PAD - 9 : x + PAD + 9 : cx(b), y + PAD + b.iconSpace - 11, ink);
774
+ const shapes = [
775
+ outline,
776
+ ...details,
777
+ ...icons,
778
+ ...labels
779
+ ];
780
+ return b.link ? [ctx.link(b.link.href, shapes, b.link)] : shapes;
781
+ }
782
+ function layout(model, { width, ctx, lang }) {
783
+ const root = makeBoxes(model, ctx, lang), nodes = [], frames = [], byId = /* @__PURE__ */ new Map();
784
+ const margin = 38;
785
+ const sceneWidth = Math.max(width, root.w + 76);
786
+ let height = root.h + 76;
787
+ function place(boxes, x, y) {
788
+ for (const box of boxes) {
789
+ const b = {
790
+ ...box,
791
+ x: x + box.x,
792
+ y: y + box.y
793
+ };
794
+ (b.frame ? frames : nodes).push(b);
795
+ byId.set(b.id, b);
796
+ place(b.children, b.x + PAD, b.y + b.header);
797
+ }
798
+ }
799
+ place(root.boxes, Math.max(margin, (sceneWidth - root.w) / 2), margin);
800
+ const headers = frames.map((b) => {
801
+ const w = Math.max(b.iconSpace, ...b.lines.map(({ text, font }) => ctx.measure(text, font.size, font.weight))) + 6;
802
+ return {
803
+ x: b.align === "right" ? b.x + b.w - PAD + 3 - w : b.x + PAD - 3,
804
+ y: b.y + PAD - 3,
805
+ w,
806
+ h: b.topSpace + b.lines.length * LINE + 6
807
+ };
808
+ });
809
+ const children = frames.flatMap((b) => drawBox(ctx, b));
810
+ const occupied = [...nodes, ...headers];
811
+ const used = [], routes = [], errors = [], arrowBoxes = [];
812
+ for (const [i, rel] of model.rels.entries()) {
813
+ const a = byId.get(rel.from), b = byId.get(rel.to);
814
+ const blockers = frames.filter((frame) => frame !== a && frame !== b && !contains(frame, a) && !contains(frame, b));
815
+ const obstacles = [...nodes.filter((box) => box !== a && box !== b).map((box) => inflate(box, TRACK_GAP)), ...[...headers, ...blockers].map((box) => inflate(box, 4))];
816
+ const endpointInsets = [a, b].filter((box) => !box.frame || a === b || !contains(box, box === a ? b : a)).map((box) => inflate(box, -2));
817
+ const path = route(a, b, [...obstacles, ...endpointInsets], used, {
818
+ x: 10 + i % 7 * 3,
819
+ y: 10 + i % 7 * 3,
820
+ w: sceneWidth - 20 - i % 7 * 6,
821
+ h: height - 20 - i % 7 * 6
822
+ }, i);
823
+ if (!path) {
824
+ errors.push(error(lang, rel.line, "找不到不穿过元素的 C4 连线", "No clear route for this C4 relationship", "拆分图表或调整声明顺序", "Split the diagram or reorder declarations"));
825
+ continue;
826
+ }
827
+ const meta = {
828
+ from: rel.from,
829
+ to: rel.to
830
+ };
831
+ const ink = ctx.design[rel.status || rel.lineColor] ?? ctx.design.fg3;
832
+ children.push(ctx.shape("polyline", {
833
+ points: path.map((p) => `${p.x},${p.y}`).join(" "),
834
+ fill: "none",
835
+ stroke: ink,
836
+ "stroke-width": 1.35,
837
+ "stroke-linejoin": "round"
838
+ }, {
839
+ kind: "edge",
840
+ ...meta
841
+ }));
842
+ for (const [enabled, tip, next, end] of [[
843
+ rel.start,
844
+ path[0],
845
+ path[1],
846
+ "start"
847
+ ], [
848
+ rel.end,
849
+ path.at(-1),
850
+ path.at(-2),
851
+ "end"
852
+ ]]) {
853
+ if (!enabled) continue;
854
+ arrowBoxes.push({
855
+ x: tip.x - 8,
856
+ y: tip.y - 8,
857
+ w: 16,
858
+ h: 16
859
+ });
860
+ const base = lerp(tip, next, 8 / distance(tip, next));
861
+ const dx = (tip.x - base.x) * .45, dy = (tip.y - base.y) * .45;
862
+ children.push(ctx.shape("polygon", {
863
+ points: `${tip.x},${tip.y} ${base.x - dy},${base.y + dx} ${base.x + dy},${base.y - dx}`,
864
+ fill: ink
865
+ }, {
866
+ kind: "relation-marker",
867
+ ...meta,
868
+ end
869
+ }));
870
+ }
871
+ used.push(...segments(path));
872
+ routes.push({
873
+ path,
874
+ rel,
875
+ meta
876
+ });
877
+ }
878
+ children.push(...nodes.flatMap((b) => drawBox(ctx, b)));
879
+ const leaders = [];
880
+ for (const { path, rel, meta } of routes) {
881
+ const label = relLabel(rel, ctx);
882
+ if (!label) continue;
883
+ const { lines, iconSpace, w, h } = label;
884
+ const parts = segments(path), length = parts.reduce((sum, [a, b]) => sum + distance(a, b), 0);
885
+ const along = (fraction) => {
886
+ let rest = length * fraction;
887
+ for (const [a, b] of parts) {
888
+ const span = distance(a, b);
889
+ if (rest <= span) return lerp(a, b, rest / span);
890
+ rest -= span;
891
+ }
892
+ return path.at(-1);
893
+ };
894
+ const mid = along(.5), offset = rel.hint === "U" ? h / 2 + 6 : 0;
895
+ const preferred = point(mid.x + (rel.offsetX ?? 0), mid.y + (rel.offsetY ?? offset));
896
+ const plate = (p) => ({
897
+ x: Math.max(6, Math.min(p.x - w / 2, sceneWidth - w - 6)),
898
+ y: p.y - h / 2,
899
+ w,
900
+ h
901
+ });
902
+ const free = (box) => box.y >= 6 && box.y + h <= height - 6 && !occupied.some((b) => overlaps(inflate(box, 4), b)) && !arrowBoxes.some((b) => overlaps(box, b));
903
+ const candidates = [[preferred, mid]];
904
+ for (const t of [
905
+ .5,
906
+ .4,
907
+ .6,
908
+ .3,
909
+ .7,
910
+ .2,
911
+ .8,
912
+ .1,
913
+ .9
914
+ ]) {
915
+ const p = along(t);
916
+ candidates.push([point(p.x, 6 + h / 2), p], [point(p.x, height - 6 - h / 2), p]);
917
+ for (const dx of [
918
+ 0,
919
+ -w / 2 - 4,
920
+ w / 2 + 4
921
+ ]) for (const dy of [
922
+ 0,
923
+ -h / 2 - 4,
924
+ h / 2 + 4,
925
+ -h - 8,
926
+ h + 8
927
+ ]) candidates.push([point(p.x + dx, p.y + dy), p]);
928
+ }
929
+ let box, leader, best = Infinity;
930
+ const other = used.filter((seg) => !path.includes(seg[0]) || !path.includes(seg[1])).concat(leaders);
931
+ for (const [p, pOnEdge] of candidates) {
932
+ const candidate = plate(p);
933
+ if (!free(candidate)) continue;
934
+ const end = point(Math.max(candidate.x, Math.min(candidate.x + w, pOnEdge.x)), Math.max(candidate.y, Math.min(candidate.y + h, pOnEdge.y)));
935
+ const tie = parts.some((seg) => hits(seg, candidate)) ? null : [pOnEdge, end];
936
+ if (tie && occupied.some((b) => hits(tie, b))) continue;
937
+ const straddle = frames.some((frame) => !contains(frame, inflate(candidate, 2)) && overlaps(frame, inflate(candidate, 2)));
938
+ const score = distance(point(cx(candidate), cy(candidate)), preferred) + (tie ? distance(...tie) * 2 : 0) + (straddle ? 34 : 0) + 240 * other.filter((seg) => hits(seg, candidate)).length;
939
+ if (score < best) {
940
+ best = score;
941
+ box = candidate;
942
+ leader = tie;
943
+ }
944
+ }
945
+ if (!box) {
946
+ box = plate(point(mid.x, height + h / 2));
947
+ height += h + 14;
948
+ leader = [mid, point(cx(box), box.y)];
949
+ }
950
+ if (leader) {
951
+ leaders.push(leader);
952
+ children.push(ctx.path(`M${leader[0].x} ${leader[0].y} L${leader[1].x} ${leader[1].y}`, {
953
+ fill: "none",
954
+ stroke: ctx.design.fg3,
955
+ "stroke-dasharray": "2 3",
956
+ meta: {
957
+ kind: "leader",
958
+ ...meta
959
+ }
960
+ }));
961
+ }
962
+ occupied.push(box);
963
+ children.push({
964
+ ...ctx.el("g", {}, [
965
+ ctx.rect(box.x, box.y, box.w, box.h, {
966
+ fill: ctx.design.bg,
967
+ stroke: ctx.design.line2,
968
+ rx: 5
969
+ }),
970
+ ...iconShape(ctx, rel.icon, cx(box), box.y + 14, ctx.design[rel.status || rel.textColor] ?? ctx.design.fg3),
971
+ ...lines.map((line, i) => ctx.text(cx(box), box.y + 4 + iconSpace + (i + .5) * LINE, line, {
972
+ anchor: "middle",
973
+ "dominant-baseline": "central",
974
+ size: FONT.label.size,
975
+ fill: ctx.design[rel.textColor] ?? ctx.design.fg1
976
+ }))
977
+ ]),
978
+ meta: {
979
+ kind: "edge-label-group",
980
+ ...meta
981
+ }
982
+ });
983
+ }
984
+ if (sceneWidth > 2 * width || nodes.length > 60) errors.push(warning("LARGE_DIAGRAM", lang, 1, "C4 图较大", "Large C4 diagram", "考虑拆分图表", "Consider splitting the diagram"));
985
+ return {
986
+ scene: {
987
+ width: sceneWidth,
988
+ height,
989
+ children,
990
+ title: model.title,
991
+ heading: model.title
992
+ },
993
+ errors
994
+ };
995
+ }
996
+ var implementations = Object.fromEntries(TYPES.map((kind) => [kind, {
997
+ parse,
998
+ layout,
999
+ acceptsPresentation: true,
1000
+ honoredConfig
1001
+ }]));
1002
+ export { c4_exports, implementations };