jpfun 0.2.0 → 0.3.0

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