@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,1038 @@
1
+ import { __exportAll } from "../_virtual/_rolldown/runtime.js";
2
+ import { contrast, pushContrast } from "../core/color.js";
3
+ import { FONT, appearance, error, fallbackMeasure, warning, wrapText } from "./support.js";
4
+ import { normalizeLabel } from "./label.js";
5
+ import { presentation } from "./presentation.js";
6
+ import { labelIcon } from "./tree-family-icon.js";
7
+ // src/diagram/venn.js
8
+ var venn_exports = /* @__PURE__ */ __exportAll({ implementations: () => implementations });
9
+ var MARGIN = 24;
10
+ var TITLE_LINE = 20;
11
+ var TITLE_GAP = 12;
12
+ var HEAD_LINE = 18;
13
+ var ITEM_LINE = 16;
14
+ var ITEM_GAP = 4;
15
+ var SET_CLEAR = 12;
16
+ var UNION_CLEAR = 10;
17
+ var INK_GAP = 4;
18
+ var SHELF_GAP = 26;
19
+ var SHELF_PITCH = 28;
20
+ var LEADER = 8;
21
+ var GRID_PAD = 14;
22
+ var GRID_GAP = 16;
23
+ var GRID_MIN_W = 168;
24
+ var GRID_COLS = 3;
25
+ var ITEM_CAP = 12;
26
+ var SIZE_DEFAULT = 10;
27
+ var RING_STROKE = 1.5;
28
+ var RING_OPACITY = .1;
29
+ var TEXT_CONTRAST = 4.5;
30
+ var SPREADS = 17;
31
+ var GROW = 1.12;
32
+ var MAX_GROWTH = 6;
33
+ var PLOT_H = 460;
34
+ var PARTS_HINT = 60;
35
+ var RING_MAX = 3;
36
+ var PAIR_SHARE = .55;
37
+ var TRIANGLE_MIN = .55;
38
+ var TITLE_FONT = {
39
+ size: FONT.title.size,
40
+ weight: FONT.title.weight
41
+ };
42
+ var SET_FONT = {
43
+ size: FONT.title.size,
44
+ weight: FONT.title.weight
45
+ };
46
+ var ITEM_FONT = {
47
+ size: FONT.label.size,
48
+ weight: FONT.label.weight
49
+ };
50
+ var UNION_FONT = {
51
+ ...ITEM_FONT,
52
+ weight: 600
53
+ };
54
+ function stripComment(line) {
55
+ let quote = false;
56
+ for (let i = 0; i < line.length; i += 1) {
57
+ if (line[i] === "\"") {
58
+ quote = !quote;
59
+ continue;
60
+ }
61
+ if (!quote && line[i] === "%" && line[i + 1] === "%") return line.slice(0, i);
62
+ }
63
+ return line;
64
+ }
65
+ var IDENT = /^"([^"]*)"|^([A-Za-z_][\w-]*)/;
66
+ var BLANK = /^[ \t]*/;
67
+ function readIdent(text, at) {
68
+ const cursor = at + BLANK.exec(text.slice(at))[0].length;
69
+ const match = IDENT.exec(text.slice(cursor));
70
+ return match ? {
71
+ id: match[1] ?? match[2],
72
+ at: cursor + match[0].length
73
+ } : null;
74
+ }
75
+ function keywordOf(text) {
76
+ const match = /^[A-Za-z][\w-]*/.exec(text);
77
+ return match ? {
78
+ word: match[0].toLowerCase(),
79
+ at: match[0].length
80
+ } : {
81
+ word: "",
82
+ at: 0
83
+ };
84
+ }
85
+ function readIdents(text, at) {
86
+ const ids = [];
87
+ let cursor = at;
88
+ for (;;) {
89
+ const ident = readIdent(text, cursor);
90
+ if (!ident) break;
91
+ if (/^\s*:/.test(text.slice(ident.at))) break;
92
+ ids.push(ident.id);
93
+ const after = ident.at + BLANK.exec(text.slice(ident.at))[0].length;
94
+ if (text[after] !== ",") {
95
+ cursor = ident.at;
96
+ break;
97
+ }
98
+ cursor = after + 1;
99
+ }
100
+ const end = ids.length ? cursor : at;
101
+ return {
102
+ ids,
103
+ at: end + BLANK.exec(text.slice(end))[0].length
104
+ };
105
+ }
106
+ function readLabel(text, at) {
107
+ const match = /^\[([^\]]*)\]/.exec(text.slice(at));
108
+ if (!match) return {
109
+ label: null,
110
+ at
111
+ };
112
+ let value = match[1];
113
+ if (value.length >= 2 && value.startsWith("\"") && value.endsWith("\"")) value = value.slice(1, -1);
114
+ return {
115
+ label: value.trim(),
116
+ at: at + match[0].length
117
+ };
118
+ }
119
+ function readSize(text, at) {
120
+ const match = /^:\s*([+-]?(?:\d+(?:\.\d+)?|\.\d+))/.exec(text.slice(at));
121
+ return match ? {
122
+ size: Number(match[1]),
123
+ at: at + match[0].length
124
+ } : {
125
+ size: null,
126
+ at
127
+ };
128
+ }
129
+ function parse(source, info = {}) {
130
+ const lang = info.lang || "zh";
131
+ const firstLine = Number.isInteger(info.firstLine) ? info.firstLine : 2;
132
+ const headerLine = Number.isInteger(info.headerLine) ? info.headerLine : Math.max(1, firstLine - 1);
133
+ const errors = [];
134
+ const look = presentation({ lang });
135
+ const assetWarnings = /* @__PURE__ */ new Set();
136
+ const sets = [];
137
+ const unions = [];
138
+ const texts = [];
139
+ const known = /* @__PURE__ */ new Map();
140
+ let title = "";
141
+ let titleLine = headerLine;
142
+ let scope = null;
143
+ let indentMode = false;
144
+ const fail = (line, zh, en, fixZh, fixEn) => errors.push(error(lang, line, zh, en, fixZh, fixEn));
145
+ const junk = (line) => fail(line, "无法识别这一行", "Unrecognized line", "使用 set、union、text、title 或 style", "Use set, union, text, title, or style");
146
+ const orphan = (line) => fail(line, "text 前缺少 set 或 union", "A text line needs an owning set or union", "把 text 缩进到 set/union 下面,或先写集合 id", "Indent text under a set/union, or name the set first");
147
+ const params = typeof info.params === "string" ? info.params.trim() : "";
148
+ if (params) fail(headerLine, `venn-beta 头行不能带内容「${params}」`, `Unexpected text after "venn-beta": ${params}`, "把内容写成 set、union 或 text 行", "Move the text into set, union, or text lines");
149
+ for (const key of Object.keys(info.options || {})) fail(headerLine, `venn-beta 不支持选项「${key}」`, `Unsupported venn-beta option: ${key}`, "删除这个选项", "Remove this option");
150
+ const labelOf = (raw, line) => {
151
+ const normalized = normalizeLabel(raw, {
152
+ lang,
153
+ line
154
+ });
155
+ for (const diagnostic of normalized.diagnostics) {
156
+ const key = `${diagnostic.code}:${diagnostic.message}`;
157
+ if (!assetWarnings.has(key)) {
158
+ assetWarnings.add(key);
159
+ errors.push(diagnostic);
160
+ }
161
+ }
162
+ return {
163
+ text: normalized.text,
164
+ icon: normalized.icon
165
+ };
166
+ };
167
+ const addSet = (id, label, size, line) => {
168
+ if (!id) {
169
+ junk(line);
170
+ return false;
171
+ }
172
+ if (known.has(id)) {
173
+ fail(line, `集合「${id}」重复定义`, `Duplicate set identifier: ${id}`, "把同一个集合合并成一行 set", "Merge the duplicate into a single set line");
174
+ return false;
175
+ }
176
+ const normalized = label ? labelOf(label, line) : {
177
+ text: "",
178
+ icon: null
179
+ };
180
+ sets.push({
181
+ id,
182
+ label: normalized.text,
183
+ icon: normalized.icon,
184
+ size: size ?? SIZE_DEFAULT,
185
+ tone: sets.length % 6 + 1,
186
+ line
187
+ });
188
+ known.set(id, sets.at(-1));
189
+ scope = { ids: [id] };
190
+ return true;
191
+ };
192
+ const addUnion = (ids, label, size, line) => {
193
+ const unique = ids.filter((id, index) => ids.indexOf(id) === index);
194
+ if (unique.length < 2) {
195
+ fail(line, "union 至少需要两个集合", "A union needs at least two sets", "写成 union A,B 或列更多集合", "Write union A,B or list more sets");
196
+ return false;
197
+ }
198
+ const unknown = unique.filter((id) => !known.has(id));
199
+ if (unknown.length) {
200
+ fail(line, `union 引用了未定义的集合「${unknown.join("、")}」`, `Unknown set identifier: ${unknown.join(", ")}`, "先用 set 声明集合,再写 union", "Declare each set with set before the union");
201
+ return false;
202
+ }
203
+ const sorted = [...unique].sort();
204
+ const normalized = label ? labelOf(label, line) : {
205
+ text: "",
206
+ icon: null
207
+ };
208
+ unions.push({
209
+ ids: sorted,
210
+ label: normalized.text,
211
+ icon: normalized.icon,
212
+ size: size ?? SIZE_DEFAULT / unique.length ** 2,
213
+ line
214
+ });
215
+ scope = { ids: sorted };
216
+ return true;
217
+ };
218
+ const addText = (id, label, ids, line) => {
219
+ const unique = ids.filter((value, index) => ids.indexOf(value) === index);
220
+ if (!unique.length) {
221
+ orphan(line);
222
+ return;
223
+ }
224
+ const normalized = labelOf(label || id, line);
225
+ texts.push({
226
+ id,
227
+ label: normalized.text,
228
+ icon: normalized.icon,
229
+ ids: [...unique].sort(),
230
+ key: [...unique].sort().join("|"),
231
+ line
232
+ });
233
+ };
234
+ for (const [index, raw] of String(source ?? "").split("\n").entries()) {
235
+ const line = firstLine + index;
236
+ const body = stripComment(raw).trim();
237
+ if (!body) continue;
238
+ const indented = /^[ \t]/.test(raw);
239
+ const style = /^style\s+/i.exec(body);
240
+ if (style) {
241
+ const list = readIdents(body, style[0].length);
242
+ if (list.ids.length > 1) {
243
+ look.accept(`style ${JSON.stringify([...list.ids].sort().join("|"))} ${body.slice(list.at).trimStart()}`, line);
244
+ continue;
245
+ }
246
+ }
247
+ if (look.accept(body, line)) continue;
248
+ const head = keywordOf(body);
249
+ const rest = body.slice(head.at);
250
+ if (indentMode && indented && head.word === "text") {
251
+ const node = readIdent(rest, 0);
252
+ const label = readLabel(rest, node ? node.at : 0);
253
+ if (!node || rest.slice(label.at).trim()) {
254
+ junk(line);
255
+ continue;
256
+ }
257
+ if (scope) addText(node.id, label.label, scope.ids, line);
258
+ else orphan(line);
259
+ continue;
260
+ }
261
+ if (!indented) indentMode = false;
262
+ if (head.word === "title") {
263
+ const value = rest.trim();
264
+ if (!value) {
265
+ fail(line, "title 后面缺少标题文字", "title needs a heading after it", "写成 title 标题文字", "Write title followed by the heading");
266
+ continue;
267
+ }
268
+ title = labelOf(value.replace(/^"(.*)"$/, "$1").trim(), line).text;
269
+ titleLine = line;
270
+ continue;
271
+ }
272
+ if (head.word === "set") {
273
+ const first = readIdent(rest, 0);
274
+ if (!first) {
275
+ junk(line);
276
+ continue;
277
+ }
278
+ const label = readLabel(rest, first.at);
279
+ const size = readSize(rest, label.at);
280
+ if (rest.slice(size.at).trim()) {
281
+ junk(line);
282
+ continue;
283
+ }
284
+ if (addSet(first.id, label.label, size.size, line)) indentMode = true;
285
+ continue;
286
+ }
287
+ if (head.word === "union") {
288
+ const list = readIdents(rest, 0);
289
+ const label = readLabel(rest, list.at);
290
+ const size = readSize(rest, label.at);
291
+ if (!list.ids.length || rest.slice(size.at).trim()) {
292
+ junk(line);
293
+ continue;
294
+ }
295
+ if (addUnion(list.ids, label.label, size.size, line)) indentMode = true;
296
+ continue;
297
+ }
298
+ if (head.word === "text") {
299
+ const list = readIdents(rest, 0);
300
+ const owner = readIdent(rest, list.at);
301
+ const label = readLabel(rest, owner ? owner.at : list.at);
302
+ if (!list.ids.length || rest.slice(label.at).trim()) {
303
+ junk(line);
304
+ continue;
305
+ }
306
+ if (owner) addText(owner.id, label.label, list.ids, line);
307
+ else addText(list.ids[0], label.label, scope ? scope.ids : [], line);
308
+ continue;
309
+ }
310
+ junk(line);
311
+ }
312
+ errors.push(...look.diagnostics());
313
+ if (!sets.length) fail(firstLine, "文氏图没有任何 set", "A Venn diagram needs at least one set", "至少写一行 set", "Write at least one set line");
314
+ const attach = (item, id) => ({
315
+ ...item,
316
+ status: look.statusOf(id),
317
+ link: look.linkOf(id)
318
+ });
319
+ for (const set of sets) Object.assign(set, attach({}, set.id));
320
+ for (const union of unions) Object.assign(union, attach({}, union.ids.join("|")));
321
+ for (const text of texts) Object.assign(text, attach({}, text.id));
322
+ return {
323
+ model: {
324
+ kind: "venn-beta",
325
+ title,
326
+ titleLine,
327
+ sets,
328
+ unions,
329
+ texts
330
+ },
331
+ errors
332
+ };
333
+ }
334
+ function clearanceAt(x, y, inside, outside) {
335
+ let value = Infinity;
336
+ for (const ring of inside) value = Math.min(value, ring.r - Math.hypot(x - ring.cx, y - ring.cy));
337
+ for (const ring of outside) value = Math.min(value, Math.hypot(x - ring.cx, y - ring.cy) - ring.r);
338
+ return value;
339
+ }
340
+ function deepestPoint(inside, outside, rounds = 3, steps = 16) {
341
+ const cx = inside.reduce((sum, ring) => sum + ring.cx, 0) / inside.length;
342
+ const cy = inside.reduce((sum, ring) => sum + ring.cy, 0) / inside.length;
343
+ let best = {
344
+ x: cx,
345
+ y: cy,
346
+ value: clearanceAt(cx, cy, inside, outside)
347
+ };
348
+ let span = Math.max(...inside.map((ring) => ring.r));
349
+ for (let round = 0; round < rounds; round += 1) {
350
+ const from = best;
351
+ for (let ix = -steps / 2; ix <= steps / 2; ix += 1) for (let iy = -steps / 2; iy <= steps / 2; iy += 1) {
352
+ const x = from.x + ix * span / (steps / 2);
353
+ const y = from.y + iy * span / (steps / 2);
354
+ const value = clearanceAt(x, y, inside, outside);
355
+ if (value > best.value + 1e-9 || Math.abs(value - best.value) <= 1e-9 && Math.abs(y - cy) < Math.abs(best.y - cy) - 1e-9) best = {
356
+ x,
357
+ y,
358
+ value
359
+ };
360
+ }
361
+ span /= steps / 4;
362
+ }
363
+ return best;
364
+ }
365
+ function radiiOf(sets, R) {
366
+ const weights = sets.map((set) => Number.isFinite(set.size) ? Math.max(0, set.size) : SIZE_DEFAULT);
367
+ const peak = Math.max(0, ...weights);
368
+ return weights.map((weight) => R * (peak > 0 ? Math.max(.3, Math.sqrt(weight / peak)) : 1));
369
+ }
370
+ function ringsAt(sets, R, spread) {
371
+ const radii = radiiOf(sets, R);
372
+ const rings = sets.map((set, index) => ({
373
+ id: set.id,
374
+ label: set.label,
375
+ icon: set.icon,
376
+ tone: set.tone,
377
+ status: set.status,
378
+ link: set.link,
379
+ line: set.line,
380
+ r: radii[index],
381
+ cx: 0,
382
+ cy: 0
383
+ }));
384
+ if (rings.length === 1) return rings;
385
+ if (rings.length === 2) {
386
+ const small = Math.min(radii[0], radii[1]);
387
+ const near = Math.abs(radii[0] - radii[1]) + .25 * small;
388
+ const far = radii[0] + radii[1] - .1 * small;
389
+ const gap = near + Math.min(1, Math.max(0, spread)) * (far - near);
390
+ rings[0].cx = -gap / 2;
391
+ rings[1].cx = gap / 2;
392
+ return rings;
393
+ }
394
+ const apex = rings.map((ring, index) => ({
395
+ index,
396
+ r: ring.r
397
+ })).sort((left, right) => right.r - left.r || right.index - left.index)[0].index;
398
+ const rest = [
399
+ 0,
400
+ 1,
401
+ 2
402
+ ].filter((index) => index !== apex).sort((left, right) => left - right);
403
+ const share = (TRIANGLE_MIN + Math.min(1, Math.max(0, spread)) * .7) * (radii.reduce((sum, r) => sum + r, 0) / 3);
404
+ const angle = [];
405
+ angle[apex] = -Math.PI / 2;
406
+ angle[rest[0]] = 5 * Math.PI / 6;
407
+ angle[rest[1]] = Math.PI / 6;
408
+ rings.forEach((ring, index) => {
409
+ ring.cx = Math.cos(angle[index]) * share;
410
+ ring.cy = Math.sin(angle[index]) * share;
411
+ });
412
+ return rings;
413
+ }
414
+ function canonicalSpread(sets, R) {
415
+ if (sets.length <= 1) return .5;
416
+ const radii = radiiOf(sets, R);
417
+ if (sets.length === 2) {
418
+ const small = Math.min(radii[0], radii[1]);
419
+ const near = Math.abs(radii[0] - radii[1]) + .25 * small;
420
+ const far = radii[0] + radii[1] - .1 * small;
421
+ return far > near ? Math.min(1, Math.max(0, (radii[0] + radii[1] - PAIR_SHARE * small - near) / (far - near))) : .5;
422
+ }
423
+ return .44999999999999996 / .7;
424
+ }
425
+ function boundsOf(rings) {
426
+ const left = Math.min(...rings.map((ring) => ring.cx - ring.r));
427
+ const right = Math.max(...rings.map((ring) => ring.cx + ring.r));
428
+ const top = Math.min(...rings.map((ring) => ring.cy - ring.r));
429
+ const bottom = Math.max(...rings.map((ring) => ring.cy + ring.r));
430
+ return {
431
+ x: left,
432
+ y: top,
433
+ w: right - left,
434
+ h: bottom - top
435
+ };
436
+ }
437
+ function planBlock(region, measure) {
438
+ const slack = region.kind === "set" ? SET_CLEAR : UNION_CLEAR;
439
+ const labelFont = region.kind === "set" ? SET_FONT : UNION_FONT;
440
+ const floorOf = (entries, font) => Math.max(0, ...entries.flatMap(({ text }) => String(text).split(/\s+/).filter(Boolean).map((word) => measure(word, font))));
441
+ const cap = Math.max(48, floorOf(region.labels, labelFont) + 2, floorOf(region.items, ITEM_FONT) + 2, (region.clear - slack) * 2 - 20);
442
+ const rows = (entries, font, status) => entries.flatMap((entry) => wrapText(entry.text, cap, font, measure).map((text) => ({
443
+ text,
444
+ font,
445
+ icon: entry.icon,
446
+ link: entry.link,
447
+ status: entry.status || status
448
+ })));
449
+ const labels = rows(region.labels, labelFont, region.status);
450
+ const items = rows(region.items, ITEM_FONT, labels.length ? null : region.status);
451
+ const labelH = labels.length * (region.kind === "set" ? HEAD_LINE : ITEM_LINE);
452
+ const w = Math.max(0, ...[...labels, ...items].map((row) => rowWidth(row, measure)));
453
+ const h = labelH + (labels.length && items.length ? ITEM_GAP : 0) + items.length * ITEM_LINE;
454
+ return {
455
+ labels,
456
+ items,
457
+ labelH,
458
+ w,
459
+ h,
460
+ room: h ? blockClearance(region, w, h) - INK_GAP : Infinity
461
+ };
462
+ }
463
+ function rowWidth(row, measure) {
464
+ return measure(row.text, row.font) + (row.icon ? 18 : 0);
465
+ }
466
+ function blockClearance(region, w, h) {
467
+ let worst = Infinity;
468
+ for (const ring of region.inside) worst = Math.min(worst, ring.r - Math.hypot(Math.abs(region.x - ring.cx) + w / 2, Math.abs(region.y - ring.cy) + h / 2));
469
+ for (const ring of region.outside) worst = Math.min(worst, Math.hypot(Math.max(0, Math.abs(region.x - ring.cx) - w / 2), Math.max(0, Math.abs(region.y - ring.cy) - h / 2)) - ring.r);
470
+ return worst;
471
+ }
472
+ function regionList(rings, model) {
473
+ const groups = /* @__PURE__ */ new Map();
474
+ for (const text of model.texts) {
475
+ const list = groups.get(text.key) || [];
476
+ list.push(text);
477
+ groups.set(text.key, list);
478
+ }
479
+ const regions = [];
480
+ for (const ring of rings) regions.push({
481
+ key: ring.id,
482
+ kind: "set",
483
+ ids: [ring.id],
484
+ line: ring.line,
485
+ tone: ring.tone,
486
+ labels: [{
487
+ text: ring.label || ring.id,
488
+ icon: ring.icon,
489
+ line: ring.line,
490
+ link: ring.link
491
+ }],
492
+ items: (groups.get(ring.id) || []).map((text) => ({
493
+ text: text.label,
494
+ icon: text.icon,
495
+ link: text.link,
496
+ status: text.status
497
+ })),
498
+ texts: groups.get(ring.id) || []
499
+ });
500
+ const merged = /* @__PURE__ */ new Map();
501
+ for (const union of model.unions) {
502
+ const key = union.ids.join("|");
503
+ const existing = merged.get(key);
504
+ const labels = union.label || union.icon ? [{
505
+ text: union.label,
506
+ icon: union.icon,
507
+ line: union.line,
508
+ link: union.link
509
+ }] : [];
510
+ if (existing) {
511
+ existing.labels.push(...labels);
512
+ continue;
513
+ }
514
+ const region = {
515
+ key,
516
+ kind: "union",
517
+ ids: union.ids,
518
+ line: union.line,
519
+ tone: rings.find((ring) => ring.id === union.ids[0])?.tone || 1,
520
+ labels,
521
+ items: (groups.get(key) || []).map((text) => ({
522
+ text: text.label,
523
+ icon: text.icon,
524
+ link: text.link,
525
+ status: text.status
526
+ })),
527
+ status: union.status,
528
+ link: union.link,
529
+ texts: groups.get(key) || []
530
+ };
531
+ merged.set(key, region);
532
+ regions.push(region);
533
+ }
534
+ bindRegions(regions, rings);
535
+ return regions;
536
+ }
537
+ function bindRegions(regions, rings) {
538
+ for (const region of regions) {
539
+ region.inside = region.ids.map((id) => rings.find((ring) => ring.id === id)).filter(Boolean);
540
+ region.outside = rings.filter((ring) => !region.inside.includes(ring));
541
+ }
542
+ }
543
+ function roomOf(regions, measure, coarse) {
544
+ let worst = Infinity;
545
+ for (const region of regions) {
546
+ const point = deepestPoint(region.inside, region.outside, coarse ? 2 : 3, coarse ? 10 : 16);
547
+ region.x = point.x;
548
+ region.y = point.y;
549
+ region.clear = point.value;
550
+ region.block = planBlock(region, measure);
551
+ worst = Math.min(worst, region.block.room);
552
+ }
553
+ return worst;
554
+ }
555
+ function chooseSpread(ringsFor, regions, measure, preferred) {
556
+ const order = [];
557
+ for (let index = 0; index < SPREADS; index += 1) order.push(index / 16);
558
+ order.sort((left, right) => Math.abs(left - preferred) - Math.abs(right - preferred) || right - left);
559
+ let best = null;
560
+ for (const spread of order) {
561
+ const rings = ringsFor(spread);
562
+ bindRegions(regions, rings);
563
+ const coarse = roomOf(regions, measure, true);
564
+ const worst = coarse >= 0 ? roomOf(regions, measure, false) : coarse;
565
+ if (worst >= 0) return {
566
+ rings,
567
+ worst
568
+ };
569
+ if (!best || worst > best.worst + 1e-9) best = {
570
+ spread,
571
+ worst
572
+ };
573
+ }
574
+ const rings = ringsFor(best.spread);
575
+ bindRegions(regions, rings);
576
+ return {
577
+ rings,
578
+ worst: roomOf(regions, measure, false)
579
+ };
580
+ }
581
+ function layoutBlock(region) {
582
+ const block = region.block;
583
+ const top = region.y - block.h / 2;
584
+ const pitch = region.kind === "set" ? HEAD_LINE : ITEM_LINE;
585
+ region.rows = block.labels.map((row, index) => ({
586
+ ...row,
587
+ x: region.x,
588
+ y: top + (index + .5) * pitch,
589
+ anchor: "middle"
590
+ }));
591
+ const itemTop = top + block.labelH + (block.labels.length && block.items.length ? ITEM_GAP : 0);
592
+ block.items.forEach((row, index) => region.rows.push({
593
+ ...row,
594
+ x: region.x,
595
+ anchor: "middle",
596
+ y: itemTop + (index + .5) * ITEM_LINE
597
+ }));
598
+ }
599
+ function planRings(model, { measure, width, lang }) {
600
+ const available = Math.max(240, width - 48);
601
+ const preferred = canonicalSpread(model.sets, 1);
602
+ const unit = boundsOf(ringsAt(model.sets, 1, preferred));
603
+ let R = Math.max(56, Math.min(available / Math.max(1, unit.w), PLOT_H / Math.max(1, unit.h)));
604
+ const regions = regionList(ringsAt(model.sets, R, preferred), model);
605
+ let chosen = null;
606
+ for (let pass = 0; pass <= MAX_GROWTH; pass += 1) {
607
+ chosen = chooseSpread((spread) => ringsAt(model.sets, R, spread), regions, measure, preferred);
608
+ if (chosen.worst >= 0) break;
609
+ const next = R * GROW;
610
+ if (boundsOf(ringsAt(model.sets, next, preferred)).w > available * 2) break;
611
+ R = next;
612
+ }
613
+ const rings = chosen.rings;
614
+ const shelf = [];
615
+ for (const region of regions) {
616
+ if (region.block.room >= 0) continue;
617
+ shelf.push({
618
+ region,
619
+ rows: [...region.block.labels, ...region.block.items]
620
+ });
621
+ }
622
+ const claimed = new Set(regions.map((region) => region.key));
623
+ for (const text of model.texts) {
624
+ if (claimed.has(text.key)) continue;
625
+ shelf.push({
626
+ region: {
627
+ x: null,
628
+ y: null,
629
+ tone: 1,
630
+ line: text.line,
631
+ caption: text.ids.join(" ∩ ")
632
+ },
633
+ rows: [{
634
+ text: text.label,
635
+ font: ITEM_FONT,
636
+ icon: text.icon,
637
+ link: text.link,
638
+ status: text.status
639
+ }]
640
+ });
641
+ }
642
+ const notes = [];
643
+ if (shelf.length) notes.push(warning("DIAGRAM_HINT", lang, shelf[0].region.line ?? model.sets[0].line, `有 ${shelf.length} 个标签放不进所属区域,已排在图下方并用引线指向该区域`, `${shelf.length} label(s) do not fit their region and moved below the plot with a leader`, "缩短标签,或减少同一区域的 text", "Shorten the labels, or use fewer text nodes per region"));
644
+ for (const region of regions) {
645
+ if (region.texts.length <= ITEM_CAP) continue;
646
+ notes.push(warning("DIAGRAM_HINT", lang, region.texts[ITEM_CAP].line, `「${region.key}」区域有 ${region.texts.length} 个 text,超过 ${ITEM_CAP} 个后很难阅读`, `Region "${region.key}" holds ${region.texts.length} text nodes; more than ${ITEM_CAP} is hard to read`, "把说明移到图外,或拆成多张图", "Move details outside, or split the diagram"));
647
+ break;
648
+ }
649
+ const drawn = regions.filter((region) => region.block.h > 0 && region.block.room >= 0);
650
+ for (const region of drawn) layoutBlock(region);
651
+ return finish({
652
+ mode: "rings",
653
+ rings,
654
+ regions: drawn,
655
+ plot: boundsOf(rings),
656
+ rows: drawn.flatMap((region) => region.rows),
657
+ shelf,
658
+ notes
659
+ }, model, {
660
+ measure,
661
+ width
662
+ });
663
+ }
664
+ function planGrid(model, { measure, width, lang }) {
665
+ const groups = /* @__PURE__ */ new Map();
666
+ for (const text of model.texts) {
667
+ const list = groups.get(text.key) || [];
668
+ list.push(text);
669
+ groups.set(text.key, list);
670
+ }
671
+ const panels = [];
672
+ for (const set of model.sets) panels.push({
673
+ kind: "set",
674
+ key: set.id,
675
+ tone: set.tone,
676
+ status: set.status,
677
+ link: set.link,
678
+ line: set.line,
679
+ titles: [{
680
+ ...set,
681
+ label: set.label || set.id
682
+ }],
683
+ caption: "",
684
+ items: groups.get(set.id) || []
685
+ });
686
+ const merged = /* @__PURE__ */ new Map();
687
+ for (const union of model.unions) {
688
+ const key = union.ids.join("|");
689
+ const existing = merged.get(key);
690
+ if (existing) {
691
+ if (union.label) existing.titles.push(union);
692
+ continue;
693
+ }
694
+ const panel = {
695
+ kind: "union",
696
+ key,
697
+ tone: model.sets.find((set) => set.id === union.ids[0])?.tone || 1,
698
+ status: union.status,
699
+ link: union.link,
700
+ line: union.line,
701
+ titles: union.label || union.icon ? [union] : [],
702
+ caption: union.ids.join(" ∩ "),
703
+ items: groups.get(key) || []
704
+ };
705
+ merged.set(key, panel);
706
+ panels.push(panel);
707
+ }
708
+ const claimed = new Set(panels.map((panel) => panel.key));
709
+ for (const text of model.texts) {
710
+ if (claimed.has(text.key)) continue;
711
+ panels.push({
712
+ kind: "loose",
713
+ key: text.key,
714
+ tone: 1,
715
+ line: text.line,
716
+ titles: [text],
717
+ caption: text.ids.join(" ∩ "),
718
+ items: []
719
+ });
720
+ }
721
+ const wrap = (value, font) => wrapText(value, 140, font, (piece, spec) => measure(piece, spec));
722
+ for (const panel of panels) {
723
+ const rows = (entries, font) => entries.flatMap((item) => wrap(item.label, font).map((text, index) => ({
724
+ text,
725
+ font,
726
+ status: item.status,
727
+ link: item.link,
728
+ icon: index === 0 ? item.icon : null
729
+ })));
730
+ panel.header = rows(panel.titles, SET_FONT);
731
+ panel.captionLines = panel.caption ? wrap(panel.caption, ITEM_FONT) : [];
732
+ panel.itemLines = rows(panel.items, ITEM_FONT);
733
+ const widths = [
734
+ ...panel.header.map((row) => measure(row.text, row.font) + (row.icon ? 18 : 0)),
735
+ ...panel.captionLines.map((row) => measure(row, ITEM_FONT)),
736
+ ...panel.itemLines.map((row) => measure(row.text, row.font) + (row.icon ? 18 : 0))
737
+ ];
738
+ panel.w = Math.max(GRID_MIN_W, Math.ceil(Math.max(0, ...widths) + 28));
739
+ panel.h = 28 + panel.header.length * HEAD_LINE + (panel.captionLines.length ? 4 + panel.captionLines.length * ITEM_LINE : 0) + (panel.itemLines.length ? 6 + panel.itemLines.length * ITEM_LINE : 0);
740
+ }
741
+ const cellW = Math.max(...panels.map((panel) => panel.w));
742
+ const available = Math.max(240, width - 48);
743
+ let cols = Math.min(GRID_COLS, panels.length);
744
+ while (cols > 1 && cols * cellW + (cols - 1) * GRID_GAP > available * 2) cols -= 1;
745
+ const rowCount = Math.ceil(panels.length / cols);
746
+ const rowHeights = [];
747
+ panels.forEach((panel, index) => {
748
+ panel.column = index % cols;
749
+ panel.row = Math.floor(index / cols);
750
+ rowHeights[panel.row] = Math.max(rowHeights[panel.row] || 0, panel.h);
751
+ });
752
+ const tops = [];
753
+ let cursor = 0;
754
+ for (let row = 0; row < rowCount; row += 1) {
755
+ tops[row] = cursor;
756
+ cursor += rowHeights[row] + GRID_GAP;
757
+ }
758
+ panels.forEach((panel) => {
759
+ panel.w = cellW;
760
+ panel.x = panel.column * (cellW + GRID_GAP);
761
+ panel.y = tops[panel.row];
762
+ panel.rows = [];
763
+ let y = panel.y + GRID_PAD;
764
+ for (const row of panel.header) {
765
+ panel.rows.push({
766
+ ...row,
767
+ x: panel.x + GRID_PAD,
768
+ y: y + HEAD_LINE / 2,
769
+ anchor: "start"
770
+ });
771
+ y += HEAD_LINE;
772
+ }
773
+ if (panel.captionLines.length) {
774
+ y += 4;
775
+ for (const row of panel.captionLines) {
776
+ panel.rows.push({
777
+ text: row,
778
+ font: ITEM_FONT,
779
+ x: panel.x + GRID_PAD,
780
+ y: y + ITEM_LINE / 2,
781
+ anchor: "start"
782
+ });
783
+ y += ITEM_LINE;
784
+ }
785
+ }
786
+ if (panel.itemLines.length) {
787
+ y += 6;
788
+ for (const row of panel.itemLines) {
789
+ panel.rows.push({
790
+ ...row,
791
+ x: panel.x + GRID_PAD,
792
+ y: y + ITEM_LINE / 2,
793
+ anchor: "start"
794
+ });
795
+ y += ITEM_LINE;
796
+ }
797
+ }
798
+ });
799
+ const notes = [warning("DIAGRAM_HINT", lang, model.sets[0].line, `${model.sets.length} 个集合画成圆环后重叠区无法阅读,已改用分区面板`, `${model.sets.length} sets would leave unreadable overlaps; drawn as a region grid instead`, "集合减到 3 个可获得圆环布局,或接受分区面板", "Keep three sets for rings, or accept the region grid")];
800
+ return finish({
801
+ mode: "grid",
802
+ panels,
803
+ plot: {
804
+ x: 0,
805
+ y: 0,
806
+ w: cols * cellW + (cols - 1) * GRID_GAP,
807
+ h: cursor - GRID_GAP
808
+ },
809
+ rows: panels.flatMap((panel) => panel.rows),
810
+ shelf: [],
811
+ notes
812
+ }, model, {
813
+ measure,
814
+ width
815
+ });
816
+ }
817
+ function finish(plan, model, { measure, width }) {
818
+ const titleLines = model.title ? wrapText(model.title, Math.max(160, Math.min(width - 48, 480)), TITLE_FONT, (piece, spec) => measure(piece, spec)) : [];
819
+ const titleH = titleLines.length ? titleLines.length * TITLE_LINE + TITLE_GAP : 0;
820
+ const shelf = [];
821
+ let shelfRight = plan.plot.x + plan.plot.w;
822
+ let shelfBottom = plan.plot.y + plan.plot.h;
823
+ if (plan.shelf.length) {
824
+ const limit = Math.max(plan.plot.w, 240);
825
+ let cursor = plan.plot.x;
826
+ let top = plan.plot.y + plan.plot.h + SHELF_GAP;
827
+ let rowH = 0;
828
+ for (const item of plan.shelf) {
829
+ const rows = [...item.region.caption ? [{
830
+ text: item.region.caption,
831
+ font: ITEM_FONT,
832
+ kind: "caption"
833
+ }] : [], ...item.rows];
834
+ const w = Math.max(0, ...rows.map((row) => rowWidth(row, measure)));
835
+ if (cursor > plan.plot.x && cursor + w > plan.plot.x + limit) {
836
+ cursor = plan.plot.x;
837
+ top += rowH + GRID_GAP / 2;
838
+ rowH = 0;
839
+ }
840
+ const lines = rows.map((row, index) => ({
841
+ ...row,
842
+ anchor: "start",
843
+ x: cursor,
844
+ y: top + (index + .5) * ITEM_LINE
845
+ }));
846
+ shelf.push({
847
+ lines,
848
+ tone: item.region.tone,
849
+ from: item.region.x == null ? null : {
850
+ x: item.region.x,
851
+ y: item.region.y
852
+ },
853
+ to: {
854
+ x: cursor + w / 2,
855
+ y: top - LEADER
856
+ }
857
+ });
858
+ shelfBottom = Math.max(shelfBottom, top + rows.length * ITEM_LINE);
859
+ shelfRight = Math.max(shelfRight, cursor + w);
860
+ rowH = Math.max(rowH, rows.length * ITEM_LINE);
861
+ cursor += w + SHELF_PITCH;
862
+ }
863
+ }
864
+ const spanW = Math.max(plan.plot.x + plan.plot.w, shelfRight) - plan.plot.x;
865
+ const contentW = Math.max(spanW, ...titleLines.map((row) => measure(row, TITLE_FONT)));
866
+ const dx = MARGIN + (contentW - spanW) / 2 - plan.plot.x;
867
+ const dy = MARGIN + titleH - plan.plot.y;
868
+ const height = Math.ceil(shelfBottom - plan.plot.y + titleH + 48);
869
+ const finalWidth = Math.ceil(contentW + 48);
870
+ for (const ring of plan.rings || []) {
871
+ ring.cx += dx;
872
+ ring.cy += dy;
873
+ }
874
+ for (const panel of plan.panels || []) {
875
+ panel.x += dx;
876
+ panel.y += dy;
877
+ }
878
+ for (const row of plan.rows) {
879
+ row.x += dx;
880
+ row.y += dy;
881
+ }
882
+ for (const region of plan.regions || []) {
883
+ region.x += dx;
884
+ region.y += dy;
885
+ }
886
+ for (const item of shelf) {
887
+ for (const line of item.lines) {
888
+ line.x += dx;
889
+ line.y += dy;
890
+ }
891
+ if (item.from) item.from = {
892
+ x: item.from.x + dx,
893
+ y: item.from.y + dy
894
+ };
895
+ item.to = {
896
+ x: item.to.x + dx,
897
+ y: item.to.y + dy
898
+ };
899
+ }
900
+ return {
901
+ width: finalWidth,
902
+ height,
903
+ mode: plan.mode,
904
+ title: titleLines.length ? {
905
+ lines: titleLines,
906
+ x: MARGIN + contentW / 2,
907
+ top: 34
908
+ } : null,
909
+ rings: plan.rings || [],
910
+ regions: plan.regions || [],
911
+ panels: plan.panels || [],
912
+ rows: plan.rows,
913
+ shelf,
914
+ notes: plan.notes,
915
+ anchor: {
916
+ x: MARGIN + contentW / 2,
917
+ y: MARGIN + titleH + plan.plot.h / 2
918
+ }
919
+ };
920
+ }
921
+ function layout(model, options = {}) {
922
+ const measure = options.measure || fallbackMeasure;
923
+ const width = Math.max(1, Number(options.width) || 800);
924
+ const lang = options.lang || "zh";
925
+ if (!model.sets.length) return {
926
+ width,
927
+ height: 0,
928
+ errors: []
929
+ };
930
+ const plan = model.sets.length <= RING_MAX ? planRings(model, {
931
+ measure,
932
+ width,
933
+ lang
934
+ }) : planGrid(model, {
935
+ measure,
936
+ width,
937
+ lang
938
+ });
939
+ const errors = [...plan.notes];
940
+ const parts = model.sets.length + model.unions.length + model.texts.length;
941
+ if (parts > PARTS_HINT) errors.push(warning("LARGE_DIAGRAM", lang, model.sets[0].line, `文氏图有 ${parts} 个元素,建议拆分`, `The Venn diagram has ${parts} parts; consider splitting it`, "按集合拆成多张图", "Split the diagram by set"));
942
+ else if (plan.width > width * 2) errors.push(warning("LARGE_DIAGRAM", lang, model.sets[0].line, `文氏图需要 ${plan.width}px 宽,超过图框两倍`, `The Venn diagram needs ${plan.width}px, more than twice the frame`, "加宽图框,或拆分文氏图", "Widen the frame, or split the diagram"));
943
+ return {
944
+ ...plan,
945
+ errors
946
+ };
947
+ }
948
+ function scene(plan, ctx) {
949
+ const look = appearance(ctx);
950
+ const paper = ctx.color("bg");
951
+ const tone = (index) => ctx.frameTone(ctx.color(`tone${(index - 1) % 6 + 1}`));
952
+ const row = (item, background) => {
953
+ let ink = item.status ? ctx.textColor(look[item.status]) : item.kind === "caption" ? look.fg3 : item.font === SET_FONT ? look.fg1 : look.fg2;
954
+ if (background && contrast(ink, background) < TEXT_CONTRAST) {
955
+ let target = ctx.textOn(background);
956
+ if (contrast(target, background) < TEXT_CONTRAST) target = contrast("#000000", background) >= TEXT_CONTRAST ? "#000000" : "#ffffff";
957
+ ink = pushContrast(ink, target, background, TEXT_CONTRAST);
958
+ }
959
+ const children = [ctx.text(item.x + (item.icon ? item.anchor === "middle" ? 9 : 18 : 0), item.y, item.text, {
960
+ anchor: item.anchor,
961
+ size: item.font.size,
962
+ weight: item.font.weight,
963
+ "dominant-baseline": "central",
964
+ fill: ink
965
+ })];
966
+ if (item.icon) {
967
+ const x = item.anchor === "middle" ? item.x - ctx.measure(item.text, item.font.size, item.font.weight) / 2 - 3 : item.x + 6;
968
+ children.unshift(labelIcon(ctx, item.icon, x, item.y, 12, ink));
969
+ }
970
+ return item.link ? ctx.link(item.link.href, children, item.link) : ctx.el("g", {}, children);
971
+ };
972
+ const children = [];
973
+ if (plan.mode === "rings") {
974
+ const fills = plan.rings.map((ring) => ring.status ? ctx.frameTone(look[ring.status]) : tone(ring.tone));
975
+ for (const [index, ring] of plan.rings.entries()) children.push(ctx.circle(ring.cx, ring.cy, ring.r, {
976
+ fill: fills[index],
977
+ "fill-opacity": RING_OPACITY
978
+ }));
979
+ for (const [index, ring] of plan.rings.entries()) children.push(ctx.circle(ring.cx, ring.cy, ring.r, {
980
+ fill: "none",
981
+ stroke: fills[index],
982
+ "stroke-width": RING_STROKE,
983
+ meta: {
984
+ kind: "node",
985
+ id: ring.id,
986
+ label: ring.id
987
+ }
988
+ }));
989
+ for (const region of plan.regions) {
990
+ const background = plan.rings.reduce((under, ring, index) => region.ids.includes(ring.id) ? ctx.mix(under, fills[index], RING_OPACITY) : under, paper);
991
+ for (const item of region.rows) children.push(row(item, background));
992
+ }
993
+ for (const item of plan.shelf) {
994
+ if (item.from) children.push(ctx.line(item.from.x, item.from.y, item.to.x, item.to.y, {
995
+ stroke: ctx.tint(paper, tone(item.tone), .55),
996
+ "stroke-width": 1,
997
+ "stroke-dasharray": "3 3"
998
+ }));
999
+ for (const line of item.lines) children.push(row(line, paper));
1000
+ }
1001
+ } else for (const panel of plan.panels) {
1002
+ const color = panel.status ? ctx.frameTone(look[panel.status]) : tone(panel.tone);
1003
+ children.push(ctx.rect(panel.x, panel.y, panel.w, panel.h, {
1004
+ rx: look.radius,
1005
+ fill: ctx.tint(paper, color, panel.kind === "set" ? .07 : .04),
1006
+ stroke: ctx.tint(paper, color, .45),
1007
+ "stroke-width": 1,
1008
+ ...panel.kind === "set" ? {} : { "stroke-dasharray": "5 4" },
1009
+ meta: {
1010
+ kind: panel.kind === "set" ? "node" : "frame",
1011
+ id: panel.key,
1012
+ label: panel.key
1013
+ }
1014
+ }));
1015
+ for (const item of panel.rows) children.push(row(item));
1016
+ }
1017
+ if (plan.title) plan.title.lines.forEach((line, index) => children.push(ctx.text(plan.title.x, plan.title.top + index * TITLE_LINE, line, {
1018
+ anchor: "middle",
1019
+ size: TITLE_FONT.size,
1020
+ weight: TITLE_FONT.weight,
1021
+ fill: look.fg1,
1022
+ "dominant-baseline": "central"
1023
+ })));
1024
+ return {
1025
+ width: plan.width,
1026
+ height: plan.height,
1027
+ title: plan.title ? plan.title.lines.join(" ") : void 0,
1028
+ children,
1029
+ anchor: plan.anchor
1030
+ };
1031
+ }
1032
+ var implementations = { "venn-beta": {
1033
+ acceptsPresentation: true,
1034
+ parse,
1035
+ layout,
1036
+ scene
1037
+ } };
1038
+ export { implementations, venn_exports };