jpfun 0.1.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (114) hide show
  1. package/LICENSE +200 -200
  2. package/README.md +191 -133
  3. package/dist/diagnostic.js +5 -2
  4. package/dist/fraction.d.ts +24 -0
  5. package/dist/fraction.js +132 -0
  6. package/dist/functions/ASTtypes.d.ts +24 -7
  7. package/dist/functions/ASTtypes.js +35 -16
  8. package/dist/functions/adjust/index.d.ts +2 -0
  9. package/dist/functions/adjust/index.js +193 -0
  10. package/dist/functions/bar/index.d.ts +8 -0
  11. package/dist/functions/bar/index.js +48 -19
  12. package/dist/functions/beam/auto.js +35 -18
  13. package/dist/functions/beam/index.js +10 -6
  14. package/dist/functions/beam/layout.d.ts +3 -2
  15. package/dist/functions/beam/layout.js +42 -64
  16. package/dist/functions/box/index.js +35 -34
  17. package/dist/functions/br/index.js +14 -12
  18. package/dist/functions/dash/index.js +37 -5
  19. package/dist/functions/default.js +14 -7
  20. package/dist/functions/div/index.d.ts +16 -0
  21. package/dist/functions/div/index.js +51 -23
  22. package/dist/functions/dot/index.js +16 -8
  23. package/dist/functions/grace/index.d.ts +8 -18
  24. package/dist/functions/grace/index.js +98 -99
  25. package/dist/functions/head/index.d.ts +2 -0
  26. package/dist/functions/head/index.js +492 -0
  27. package/dist/functions/key/index.js +98 -5
  28. package/dist/functions/meter/index.d.ts +2 -0
  29. package/dist/functions/meter/index.js +154 -0
  30. package/dist/functions/note/accidentals.d.ts +24 -3
  31. package/dist/functions/note/accidentals.js +31 -1
  32. package/dist/functions/note/index.js +60 -80
  33. package/dist/functions/page/index.d.ts +9 -2
  34. package/dist/functions/page/index.js +50 -4
  35. package/dist/functions/set/index.js +42 -34
  36. package/dist/functions/stack/index.js +39 -34
  37. package/dist/functions/symbol/index.d.ts +21 -0
  38. package/dist/functions/symbol/index.js +94 -0
  39. package/dist/functions/symbol/ornament.d.ts +15 -0
  40. package/dist/functions/symbol/ornament.js +73 -0
  41. package/dist/functions/symbol/shape.d.ts +22 -0
  42. package/dist/functions/symbol/shape.js +48 -0
  43. package/dist/functions/symbol/symbols/accent.d.ts +2 -0
  44. package/dist/functions/symbol/symbols/accent.js +24 -0
  45. package/dist/functions/symbol/symbols/f.d.ts +2 -0
  46. package/dist/functions/symbol/symbols/f.js +55 -0
  47. package/dist/functions/symbol/symbols/fermata.d.ts +2 -0
  48. package/dist/functions/symbol/symbols/fermata.js +24 -0
  49. package/dist/functions/symbol/symbols/mordent.d.ts +2 -0
  50. package/dist/functions/symbol/symbols/mordent.js +43 -0
  51. package/dist/functions/symbol/symbols/p.d.ts +2 -0
  52. package/dist/functions/symbol/symbols/p.js +61 -0
  53. package/dist/functions/symbol/symbols/tr.d.ts +2 -0
  54. package/dist/functions/symbol/symbols/tr.js +75 -0
  55. package/dist/functions/tempo/glyph.d.ts +12 -0
  56. package/dist/functions/tempo/glyph.js +30 -0
  57. package/dist/functions/tempo/index.js +53 -3
  58. package/dist/functions/text/index.js +78 -26
  59. package/dist/functions/tie/index.js +103 -128
  60. package/dist/functions/tuplet/index.d.ts +2 -0
  61. package/dist/functions/tuplet/index.js +211 -0
  62. package/dist/functions/{over → up}/index.d.ts +1 -0
  63. package/dist/functions/up/index.js +370 -0
  64. package/dist/functions/voice/index.js +125 -108
  65. package/dist/functions/volta/index.d.ts +2 -0
  66. package/dist/functions/volta/index.js +227 -0
  67. package/dist/index.d.ts +4 -0
  68. package/dist/index.js +4 -0
  69. package/dist/jpfun.min.js +143 -0
  70. package/dist/layout/engine.d.ts +4 -11
  71. package/dist/layout/engine.js +100 -66
  72. package/dist/layout/model.d.ts +8 -1
  73. package/dist/layout/model.js +16 -6
  74. package/dist/layout/path.d.ts +19 -0
  75. package/dist/layout/path.js +101 -0
  76. package/dist/layout/types.d.ts +51 -18
  77. package/dist/layout/types.js +3 -1
  78. package/dist/lowering/loweringContext.d.ts +19 -23
  79. package/dist/lowering/loweringContext.js +179 -187
  80. package/dist/lowering/track.d.ts +33 -10
  81. package/dist/lowering/track.js +6 -6
  82. package/dist/lowering/types.d.ts +57 -43
  83. package/dist/lowering/types.js +20 -4
  84. package/dist/parser/parse-utils/call-utils.d.ts +0 -2
  85. package/dist/parser/parse-utils/call-utils.js +0 -6
  86. package/dist/parser/parse-utils/note-utils.d.ts +8 -0
  87. package/dist/parser/parse-utils/note-utils.js +35 -13
  88. package/dist/parser/parse-utils/string-utils.d.ts +8 -0
  89. package/dist/parser/parse-utils/string-utils.js +20 -0
  90. package/dist/parser/parserContext.d.ts +8 -5
  91. package/dist/parser/parserContext.js +38 -33
  92. package/dist/parser/preprocess.js +17 -11
  93. package/dist/playback/compile.d.ts +18 -0
  94. package/dist/playback/compile.js +401 -0
  95. package/dist/playback/event.d.ts +91 -0
  96. package/dist/playback/event.js +87 -0
  97. package/dist/playback/time.d.ts +14 -0
  98. package/dist/playback/time.js +81 -0
  99. package/dist/playback/types.d.ts +128 -0
  100. package/dist/playback/types.js +3 -0
  101. package/dist/render/canvas.d.ts +4 -4
  102. package/dist/render/canvas.js +20 -5
  103. package/dist/render/index.d.ts +1 -0
  104. package/dist/render/index.js +1 -0
  105. package/dist/render/paint.d.ts +8 -0
  106. package/dist/render/paint.js +101 -0
  107. package/dist/render/svg.d.ts +3 -3
  108. package/dist/render/svg.js +10 -8
  109. package/dist/render/text.d.ts +17 -0
  110. package/dist/render/text.js +35 -0
  111. package/dist/render/types.d.ts +1 -1
  112. package/grammar.md +217 -122
  113. package/package.json +14 -9
  114. package/dist/functions/over/index.js +0 -307
@@ -0,0 +1,370 @@
1
+ import { ASTNodeBase, ASTFunctionNode, ASTTextNode, ASTLabelNode, ASTBraceNode } from "../ASTtypes.js";
2
+ import { ErrorDiagnostic } from "../../diagnostic.js";
3
+ import { ANCHOR_KEY, DEFAULT_KEY, isVisualTemporalNode, TemporalNodeBase, } from "../../lowering/types.js";
4
+ import { Fraction } from "../../fraction.js";
5
+ import { prepareLayoutHost } from "../../layout/engine.js";
6
+ /**
7
+ * `@up` / `@down` 共用的折叠容器,不导出
8
+ *
9
+ * `contents[0]` 是宿主,其余向上;`belows` 全部向下。两个方向共用一个容器,
10
+ * 所以混写的 `^` / `_` 会全部绑到同一个宿主,不需要括号分组。
11
+ */
12
+ class FoldFunction extends ASTFunctionNode {
13
+ contents = [];
14
+ belows = [];
15
+ size;
16
+ /** 静态而不是实例字段:子类的字段初始化器晚于基类构造器,构造期读不到 */
17
+ get side() { return this.constructor.side; }
18
+ get children() {
19
+ return this.belows.length ? [...this.contents, ...this.belows] : this.contents;
20
+ }
21
+ constructor(span, args, ctx, parent = null) {
22
+ super(span, parent);
23
+ this.size = ctx.fontSize;
24
+ for (const [, value] of args) {
25
+ if (value instanceof ASTNodeBase) {
26
+ this.addContent(value, this.side, ctx);
27
+ continue;
28
+ }
29
+ const c = ctx.parseArgWithType(value.valueSpan, "content", span.start);
30
+ if (c !== null) {
31
+ this.addContent(c, this.side, ctx);
32
+ }
33
+ }
34
+ }
35
+ /** 至少有一个成员能承载标签时,折叠体本身才能作为整体被标注 */
36
+ labelable() {
37
+ return this.children.some(ASTFunctionNode.findLabelable) ? this : null;
38
+ }
39
+ /** 遇到另一个折叠体就展平到自己身上;返回 this 只是为了链式写法 */
40
+ addContent(node, side, ctx) {
41
+ if (this.contents.length === 0)
42
+ side = "above"; // 还没有宿主,来者即宿主
43
+ if (node instanceof FoldFunction)
44
+ return this.flatten(node, side, ctx);
45
+ (side === "above" ? this.contents : this.belows).push(node);
46
+ node.parent = this;
47
+ this.growSpan(node.sourceSpan);
48
+ return this;
49
+ }
50
+ growSpan(s) {
51
+ this.sourceSpan.start = Math.min(this.sourceSpan.start, s.start);
52
+ this.sourceSpan.end = Math.max(this.sourceSpan.end, s.end);
53
+ }
54
+ /** 参数复用普通 hook,并收敛为单个可见 Temporal 成员 */
55
+ loweringEnter(ctx, track) {
56
+ const members = [];
57
+ // 成员不是外层分组的成员,折叠体才是;否则 voice 的歌词会按下标错位
58
+ ctx.isolateFromLoweringGroups(() => {
59
+ for (const content of this.children) {
60
+ // 摊平所有时间列取全部事件,每个成员要求恰好一个
61
+ const [member, ...rest] = ctx.trackedEvents(content, new Fraction(), track).flat();
62
+ if (!member || rest.length > 0 || !isVisualTemporalNode(member)) {
63
+ throw new ErrorDiagnostic("E_UP_INVALID_CHILD", "@up 的每个参数必须恰好产生一个可见 Temporal,且不能包含多声部结构", content.sourceSpan);
64
+ }
65
+ members.push(member);
66
+ }
67
+ });
68
+ return [new FoldTemporal(this, members)];
69
+ }
70
+ /** 混写方向时输出等价的嵌套形式,hover 的替换按钮才能生成合法代码 */
71
+ toString(source) {
72
+ const above = this.contents.map(c => c.toString(source));
73
+ if (this.belows.length === 0)
74
+ return `@up(${above.join(", ")})`;
75
+ const host = above.length > 1 ? `@up(${above.join(", ")})` : above[0];
76
+ return `@down(${[host, ...this.belows.map(c => c.toString(source))].join(", ")})`;
77
+ }
78
+ /**
79
+ * 把另一个折叠容器并进自己:**接收者存活**,参数被掏空
80
+ *
81
+ * 跨度靠前的一方提供宿主,靠后的一方交出宿主、按连接算符的方向入列,其余成员各自
82
+ * 保留方向。这条规则让 `1^2_3^4_5` 像 LaTeX 上下标那样全部绑到同一个宿主。
83
+ *
84
+ * 被掏空的一方必须撤销候选登记:空壳已不在 AST 里,留在表中会让 @tie() 取到孤儿端点。
85
+ */
86
+ flatten(drop, side, ctx) {
87
+ // 成员顺序跟随源码;比较必须在 growSpan 之前
88
+ const front = drop.sourceSpan.start < this.sourceSpan.start ? drop : this;
89
+ const back = front === drop ? this : drop;
90
+ const [backHost, ...backAbove] = back.contents;
91
+ const demoted = backHost ? [backHost] : [];
92
+ const contents = [...front.contents, ...(side === "above" ? demoted : []), ...backAbove];
93
+ const belows = [...front.belows, ...(side === "below" ? demoted : []), ...back.belows];
94
+ this.contents = contents;
95
+ this.belows = belows;
96
+ for (const c of this.children)
97
+ c.parent = this;
98
+ this.growSpan(drop.sourceSpan);
99
+ drop.contents = [];
100
+ drop.belows = [];
101
+ const stale = ctx.labelableNodes.indexOf(drop);
102
+ if (stale >= 0)
103
+ ctx.labelableNodes.splice(stale, 1);
104
+ return this;
105
+ }
106
+ /**
107
+ * 左操作数的取法同 stack;合并策略不同:折叠要让被引用的一方存活
108
+ *
109
+ * `this` 是发起调用的那个子类,所以方向和构造都从它取。子类必须写成
110
+ * `UpFunction.desugar(...)` 这种带显式接收者的形式 —— 解析器是把
111
+ * `deSugarRelation` 摘成游离函数引用来调的,`this` 到不了这里。
112
+ */
113
+ static desugar(ctx, nodes, at) {
114
+ const n = nodes[at++];
115
+ if (!(n.kind === "sugar" && n.data === this))
116
+ return null;
117
+ const label = this.side === "above" ? "@up" : "@down";
118
+ // 向前跳过文本节点,实现忽略中间内容的作用
119
+ let left = ctx.nodes.length - 1;
120
+ while (left >= 0 && ctx.nodes[left] instanceof ASTTextNode)
121
+ left--;
122
+ if (left < 0) {
123
+ throw new ErrorDiagnostic("E_FOLD_NO_TARGET", `${label}语法糖错误: 左边没有找到可叠加的目标`, n.span);
124
+ }
125
+ let leftNode = ctx.nodes[left];
126
+ /** 左操作数在 ctx.nodes 中的起点;只有这一段会被折叠体吞并,更早的节点必须保留 */
127
+ let replaceFrom = left;
128
+ // 对 label 的特判: 目标变为label到被标记的节点范围内的所有节点
129
+ if (leftNode instanceof ASTLabelNode) {
130
+ const labelNode = leftNode;
131
+ let tgt = labelNode.target;
132
+ // 在本层这个节点位于哪里(标签节点可能被wrap)
133
+ while (tgt && !ctx.nodes.includes(tgt))
134
+ tgt = tgt.parent;
135
+ for (let j = left - 1; j >= 0; j--) {
136
+ if (ctx.nodes[j] === tgt) {
137
+ leftNode = new ASTBraceNode({
138
+ start: tgt.sourceSpan.start,
139
+ end: labelNode.sourceSpan.end,
140
+ }, ctx.nodes.slice(j, left + 1), null);
141
+ replaceFrom = j;
142
+ break;
143
+ }
144
+ }
145
+ }
146
+ // 左操作数收敛成一个折叠体,后续合并都在它身上进行
147
+ const host = leftNode instanceof FoldFunction
148
+ ? leftNode
149
+ : new this(n.span, new Map(), ctx).addContent(leftNode, "above", ctx);
150
+ // 找到下一个非文本节点 通过全量后续解析的方式进行 还是有些trick
151
+ const storage = ctx.nodes;
152
+ ctx.nodes = [];
153
+ ctx.makeNodes(nodes, at);
154
+ for (let i = 0; i < ctx.nodes.length; i++) {
155
+ // 后向跳过文本节点 和上面保持一致
156
+ const right = ctx.nodes[i];
157
+ if (right instanceof ASTTextNode)
158
+ continue;
159
+ // 右操作数可能已被 @tie() 等按引用取走(它就在刚才解析的剩余里),
160
+ // 而新建的左包装无人引用,所以合并时让右边存活;接收者就是存活的那一方
161
+ const merged = right instanceof FoldFunction
162
+ ? right.flatten(host, this.side, ctx)
163
+ : host.addContent(right, this.side, ctx);
164
+ storage.length = replaceFrom;
165
+ storage.push(merged);
166
+ while (++i < ctx.nodes.length)
167
+ storage.push(ctx.nodes[i]);
168
+ ctx.nodes = storage;
169
+ // 语法糖不走 pushNode,这里补登记;null 同样入表,和显式调用一样成为边界
170
+ ctx.labelableNodes.push(merged.labelable());
171
+ return nodes.length;
172
+ }
173
+ throw new ErrorDiagnostic("E_FOLD_NO_TARGET", `${label}语法糖错误: 右边没有找到可叠加的目标`, n.span);
174
+ }
175
+ }
176
+ class UpFunction extends FoldFunction {
177
+ static side = "above";
178
+ static def = {
179
+ name: ["up"],
180
+ description: "把同一时间位置的可见对象向上堆叠",
181
+ example: `@up(content1, content2, ...)
182
+ 语法糖: ^
183
+ {content1} ^ {content2} ^ ...
184
+ 第一个参数是宿主,其余依次叠在它的上方,常用于和弦、变速记号、音符注释。
185
+ 全体成员折叠成一个事件、共享宿主的时值;需要各自独立时值的并行分支请用 & / @stack。
186
+ `,
187
+ allowExtraArgs: true,
188
+ extraArgType: "content",
189
+ args: [],
190
+ };
191
+ static deSugarAtom(source, start, _end) {
192
+ if (source[start] !== "^")
193
+ return null;
194
+ const node = { kind: "sugar", data: UpFunction, span: { start, end: start + 1 } };
195
+ return { next: start + 1, node };
196
+ }
197
+ static deSugarRelation(ctx, nodes, at) {
198
+ return UpFunction.desugar(ctx, nodes, at);
199
+ }
200
+ }
201
+ class DownFunction extends FoldFunction {
202
+ static side = "below";
203
+ static def = {
204
+ name: ["down"],
205
+ description: "把同一时间位置的可见对象向下堆叠",
206
+ example: `@down(content1, content2, ...)
207
+ 语法糖: _
208
+ {content1} _ {content2} _ ...
209
+ 第一个参数是宿主,其余依次叠在它的下方,常用于力度记号等写在音符下面的标记。
210
+ 与 ^ 混写时全部绑到同一个宿主:1^2_3 表示 2 在上、3 在下。
211
+ `,
212
+ allowExtraArgs: true,
213
+ extraArgType: "content",
214
+ args: [],
215
+ };
216
+ static deSugarAtom(source, start, _end) {
217
+ if (source[start] !== "_")
218
+ return null;
219
+ const node = { kind: "sugar", data: DownFunction, span: { start, end: start + 1 } };
220
+ return { next: start + 1, node };
221
+ }
222
+ static deSugarRelation(ctx, nodes, at) {
223
+ return DownFunction.desugar(ctx, nodes, at);
224
+ }
225
+ }
226
+ export const UpNode = UpFunction;
227
+ export const DownNode = DownFunction;
228
+ class FoldTemporal extends TemporalNodeBase {
229
+ members;
230
+ /** members[0] 是宿主,`[1, aboveCount)` 向上叠,`[aboveCount, end)` 向下叠 */
231
+ aboveCount;
232
+ /** 每个成员相对本盒左上角的局部偏移,onPlaced 时同步为绝对坐标 */
233
+ offsets = [];
234
+ constructor(ast, members) {
235
+ super();
236
+ this.ast = ast;
237
+ this.members = members;
238
+ // members 按 ast.children 构建,而 children 是 [...contents, ...belows]
239
+ this.aboveCount = ast.contents.length;
240
+ // 节奏由第一个有时值的成员决定;否则 `$p ^ 1` 这类写法会把整个折叠体压成零时长
241
+ const lead = members.find(member => !member.T.isZero());
242
+ if (lead)
243
+ this.T.copyFrom(lead.T);
244
+ this.mergeKey = DEFAULT_KEY;
245
+ this.initLayoutBox();
246
+ // 宿主决定折叠体的时值,它的修饰语义也随之成为整体的修饰,
247
+ // 自动连梁等语义处理才能看到这个折叠体的节奏;
248
+ // 随后外层 LoweringGroup 会在同一 addon 上继续累加
249
+ const leadAddon = members[0]?.addon;
250
+ if (leadAddon)
251
+ this.addon = { ...leadAddon };
252
+ for (const member of members) {
253
+ // 只有锚点需要传上来:`| ^ @text(A)` 得保持小节线语义;
254
+ // 成员不进全局 columns,它们自己的合并组对外没有意义
255
+ if (member.mergeKey === ANCHOR_KEY)
256
+ this.mergeKey = ANCHOR_KEY;
257
+ // 时间同步在 onTimeState 里做,避免成员的时间被提前固化(后续时间可能会变)
258
+ // 修饰已经提升到折叠体上,成员不再单独绘制,
259
+ // 否则内部会出现多余的减时线或附点
260
+ member.addon = void 0;
261
+ // 成员不进入全局 columns,对外由折叠体代表:
262
+ // beam 使用折叠体事件,tie 仍可沿 AST 索引读取被标注成员的具体几何
263
+ member.foldedInto = this;
264
+ }
265
+ }
266
+ /**
267
+ * 成员共享全局时间状态,从写在最后的成员开始固化
268
+ */
269
+ onTimeState(state) {
270
+ // 宿主写在最前面,标记写在它后面;标记先写入状态,宿主才读得到
271
+ for (let i = this.members.length - 1; i >= 0; i--) {
272
+ const member = this.members[i];
273
+ // 堆叠在一起的成员共享同一个时值,由第一个成员决定;
274
+ // 本来就没有时长的成员(标注、小节线等)保持 0,不会被拉长
275
+ if (!member.T.isZero())
276
+ member.T.copyFrom(this.T);
277
+ member.t.copyFrom(this.t);
278
+ member.track = this.track;
279
+ member.layoutLine = this.layoutLine;
280
+ member.onTimeState?.(state);
281
+ }
282
+ }
283
+ emitPlayback(emitter) {
284
+ // 倒序发布,宿主(members[0])最后:附属成员注册的局部变换因此作用到宿主
285
+ for (let i = this.members.length - 1; i >= 0; i--)
286
+ emitter.play(this.members[i]);
287
+ }
288
+ /**
289
+ * 宿主留在轨道基线上,其余成员按书写顺序向上或向下叠放
290
+ *
291
+ * 成员不进入全局 columns,因此它们的准备、定位和绘制都由本节点负责;
292
+ * 准备直接复用引擎的 prepareLayoutHost,保证成员的装饰、端口与顶层对象完全一致。
293
+ */
294
+ prepareLayout(context) {
295
+ this.offsets.length = 0;
296
+ // lowering 期间修饰挂在折叠体上(augmenter 要看到整体节奏),渲染时交给宿主:
297
+ // 减时线要落在它的数字与下八度点之间,而不是压在整个盒子下面
298
+ if (this.addon && this.members[0]) {
299
+ this.members[0].addon = this.addon;
300
+ this.addon = void 0;
301
+ }
302
+ for (const member of this.members)
303
+ prepareLayoutHost(member, context);
304
+ const first = this.members[0];
305
+ if (!first) {
306
+ this.box.w = this.box.h = 0;
307
+ this.box.anchor = this.box.visualAxis = 0;
308
+ return;
309
+ }
310
+ // 横向完全等于宿主:上下的标记(变速、注释、力度)常常比音符宽得多,
311
+ // 让它们撑宽盒子会把右邻推开一大截;它们画在基线外侧,伸出盒外也不会碰撞
312
+ const anchor = first.box.anchor;
313
+ const gap = this.ast.size * 0.12;
314
+ for (const member of this.members)
315
+ this.offsets.push({ x: anchor - member.box.anchor, y: 0 });
316
+ // 以宿主盒顶为 0,向上得到负坐标、向下得到正坐标,最后整体下移
317
+ let top = 0;
318
+ for (let i = 1; i < this.aboveCount; i++) {
319
+ top -= gap + this.members[i].box.h;
320
+ this.offsets[i].y = top;
321
+ }
322
+ let bottom = first.box.h;
323
+ for (let i = this.aboveCount; i < this.members.length; i++) {
324
+ bottom += gap;
325
+ this.offsets[i].y = bottom;
326
+ bottom += this.members[i].box.h;
327
+ }
328
+ for (const offset of this.offsets)
329
+ offset.y -= top;
330
+ this.box.w = first.box.w;
331
+ this.box.h = bottom - top;
332
+ this.box.anchor = anchor;
333
+ // 宿主对齐轨道基线,两侧成员各自向外撑开行高
334
+ this.box.visualAxis = first.box.visualAxis - top;
335
+ // 端口:宿主代表整个折叠体,把它发布的端口原样平移(它的 offset.x 恒为 0),
336
+ // 减时线、歌词等端口于是与普通音符完全一致,关系函数不需要认识 up
337
+ const firstOffset = this.offsets[0];
338
+ for (const name in first.ports) {
339
+ const port = first.ports[name];
340
+ this.ports[name] = { x: port.x, y: firstOffset.y + port.y };
341
+ }
342
+ // 代表成员没声明核心范围时退回它的整个盒子
343
+ this.ports["body.left"] ??= { x: 0, y: this.box.visualAxis };
344
+ this.ports["body.right"] ??= { x: first.box.w, y: this.box.visualAxis };
345
+ // 唯一的例外:连音线要接到最上面那个成员的顶部
346
+ const topIndex = this.aboveCount - 1;
347
+ const topOffset = this.offsets[topIndex];
348
+ this.ports["tie.top"] = {
349
+ x: anchor,
350
+ y: topOffset.y + (this.members[topIndex].ports["tie.top"]?.y ?? 0),
351
+ };
352
+ }
353
+ /** 引擎每次改变本节点坐标后,按准备阶段保存的局部偏移重算成员绝对坐标 */
354
+ onPlaced() {
355
+ for (let i = 0; i < this.members.length; i++) {
356
+ const offset = this.offsets[i];
357
+ const member = this.members[i];
358
+ member.box.x = this.box.x + offset.x;
359
+ member.box.y = this.box.y + offset.y;
360
+ member.onPlaced?.();
361
+ }
362
+ }
363
+ paint(painter) {
364
+ for (const member of this.members) {
365
+ member.paint(painter);
366
+ for (const decoration of member.decorations)
367
+ decoration.paint(painter);
368
+ }
369
+ }
370
+ }