jpfun 0.4.1 → 0.6.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 (89) hide show
  1. package/README.md +53 -113
  2. package/dist/converter/midi/convert.js +14 -7
  3. package/dist/converter/musicxml/features.d.ts +57 -16
  4. package/dist/converter/musicxml/features.js +204 -42
  5. package/dist/converter/musicxml/index.d.ts +7 -2
  6. package/dist/converter/musicxml/index.js +256 -268
  7. package/dist/converter/musicxml/model.d.ts +36 -7
  8. package/dist/converter/musicxml/resolve.d.ts +10 -0
  9. package/dist/converter/musicxml/resolve.js +152 -0
  10. package/dist/core.d.ts +1 -0
  11. package/dist/core.js +1 -0
  12. package/dist/fraction.js +7 -1
  13. package/dist/functions/ASTtypes.d.ts +6 -3
  14. package/dist/functions/ASTtypes.js +30 -23
  15. package/dist/functions/adjust/index.js +10 -50
  16. package/dist/functions/arpeggio/index.js +40 -25
  17. package/dist/functions/beam/auto.js +2 -11
  18. package/dist/functions/box/index.js +132 -82
  19. package/dist/functions/dash/index.js +20 -11
  20. package/dist/functions/default.js +2 -1
  21. package/dist/functions/div/index.d.ts +1 -1
  22. package/dist/functions/div/index.js +9 -10
  23. package/dist/functions/dot/index.js +4 -5
  24. package/dist/functions/dyn/index.js +28 -32
  25. package/dist/functions/grace/index.d.ts +23 -8
  26. package/dist/functions/grace/index.js +138 -119
  27. package/dist/functions/head/index.js +11 -4
  28. package/dist/functions/key/index.js +1 -1
  29. package/dist/functions/meter/index.js +40 -7
  30. package/dist/functions/meter/shape.d.ts +3 -0
  31. package/dist/functions/meter/shape.js +43 -0
  32. package/dist/functions/note/index.d.ts +6 -1
  33. package/dist/functions/note/index.js +60 -57
  34. package/dist/functions/note/source-transform.d.ts +21 -0
  35. package/dist/functions/note/source-transform.js +164 -0
  36. package/dist/functions/page/index.d.ts +5 -0
  37. package/dist/functions/page/index.js +8 -5
  38. package/dist/functions/rest/index.d.ts +2 -0
  39. package/dist/functions/rest/index.js +233 -0
  40. package/dist/functions/set/index.js +5 -1
  41. package/dist/functions/symbol/index.d.ts +2 -0
  42. package/dist/functions/symbol/index.js +1 -1
  43. package/dist/functions/symbol/ornament.d.ts +3 -3
  44. package/dist/functions/symbol/ornament.js +21 -35
  45. package/dist/functions/symbol/symbols/accent.js +5 -5
  46. package/dist/functions/symbol/symbols/fermata.js +5 -4
  47. package/dist/functions/temporal.d.ts +21 -7
  48. package/dist/functions/temporal.js +14 -4
  49. package/dist/functions/tie/index.js +36 -42
  50. package/dist/functions/tuplet/index.js +20 -24
  51. package/dist/functions/up/index.js +54 -34
  52. package/dist/functions/voice/connections.d.ts +19 -0
  53. package/dist/functions/voice/connections.js +88 -0
  54. package/dist/functions/voice/index.js +248 -102
  55. package/dist/functions/volta/index.js +7 -12
  56. package/dist/jpfun.core.min.js +61 -41
  57. package/dist/jpfun.from-midi.min.js +5 -5
  58. package/dist/jpfun.from-musicxml.min.js +8 -8
  59. package/dist/jpfun.min.js +70 -50
  60. package/dist/layout/engine.d.ts +13 -1
  61. package/dist/layout/engine.js +458 -129
  62. package/dist/layout/model.js +22 -66
  63. package/dist/layout/page.js +3 -1
  64. package/dist/layout/range.d.ts +26 -0
  65. package/dist/layout/range.js +75 -0
  66. package/dist/layout/types.d.ts +63 -49
  67. package/dist/lowering/loweringContext.d.ts +1 -1
  68. package/dist/lowering/loweringContext.js +34 -4
  69. package/dist/lowering/types.d.ts +5 -0
  70. package/dist/parser/parse-utils/length-utils.js +2 -0
  71. package/dist/parser/parse-utils/note-utils.d.ts +2 -0
  72. package/dist/parser/parse-utils/note-utils.js +6 -2
  73. package/dist/parser/parserContext.js +5 -9
  74. package/dist/parser/preprocess.js +4 -6
  75. package/dist/playback/compile.d.ts +2 -9
  76. package/dist/playback/compile.js +297 -186
  77. package/dist/playback/event.d.ts +8 -16
  78. package/dist/playback/event.js +13 -3
  79. package/dist/playback/types.d.ts +76 -18
  80. package/dist/playback/types.js +1 -0
  81. package/dist/render/index.d.ts +1 -1
  82. package/dist/render/index.js +1 -1
  83. package/dist/render/paint.js +1 -1
  84. package/dist/render/recording.d.ts +1 -0
  85. package/dist/render/recording.js +21 -0
  86. package/dist/render/translate.d.ts +14 -0
  87. package/dist/render/translate.js +48 -0
  88. package/grammar.md +200 -250
  89. package/package.json +4 -4
@@ -181,17 +181,8 @@ function createAdjacentBeams(result, nodeScopes, explicitEndpoints) {
181
181
  const activeStart = measureStart.compare(lineStart) >= 0 ? measureStart : lineStart;
182
182
  const beat = beatAt(beatTime.copyFrom(node.t).sub(activeStart));
183
183
  const current = groups.get(node.track);
184
- if (!current) {
185
- groups.set(node.track, {
186
- nodes: [node],
187
- line: node.layoutLine,
188
- beat,
189
- endTime: node.t.clone().add(node.T),
190
- });
191
- continue;
192
- }
193
- const isContinuous = node.t.equals(current.endTime);
194
- if (node.layoutLine !== current.line || beat !== current.beat || !isContinuous) {
184
+ const isContinuous = current && node.t.equals(current.endTime);
185
+ if (!current || node.layoutLine !== current.line || beat !== current.beat || !isContinuous) {
195
186
  flush(node.track);
196
187
  groups.set(node.track, {
197
188
  nodes: [node],
@@ -1,7 +1,6 @@
1
1
  import { ASTFunctionNode } from "../ASTtypes.js";
2
2
  import { isVisualTemporalNode } from "../temporal.js";
3
3
  import { ErrorDiagnostic } from "../../diagnostic.js";
4
- import { unionLayoutBoxes } from "../../layout/engine.js";
5
4
  import { layoutHorizontalRegion } from "../../layout/model.js";
6
5
  import { isLayoutAttachment } from "../../layout/types.js";
7
6
  class BoxFunction extends ASTFunctionNode {
@@ -21,7 +20,7 @@ class BoxFunction extends ASTFunctionNode {
21
20
  },
22
21
  {
23
22
  name: "padding",
24
- description: "内容到框线的内边距",
23
+ description: "内容到框线的内边距,负值向内收缩",
25
24
  type: "length",
26
25
  default: {
27
26
  value: 0,
@@ -64,25 +63,37 @@ class BoxFunction extends ASTFunctionNode {
64
63
  * 进入目标内容前只开始收集所有成员矩形的引用
65
64
  */
66
65
  loweringEnter(ctx) {
67
- const temporalMembers = [];
68
- const childAttachments = [];
69
- ctx.beginLoweringGroup(this, {
70
- attachment: new BoxLayoutAttachment(temporalMembers, childAttachments, this),
71
- onTemporal(node) {
72
- if (!isVisualTemporalNode(node))
73
- return;
74
- temporalMembers.push(node);
75
- },
76
- onAttachment(attachment) {
77
- if (isLayoutAttachment(attachment))
78
- childAttachments.push(attachment);
79
- },
80
- });
66
+ const group = {
67
+ nodes: [],
68
+ attachments: [],
69
+ onTemporal(node) { if (isVisualTemporalNode(node))
70
+ group.nodes.push(node); },
71
+ onAttachment(attachment) { group.attachments.push(attachment); },
72
+ };
73
+ ctx.beginLoweringGroup(this, group);
81
74
  return [];
82
75
  }
83
- /** 目标内容完成 lowering 后结束当前 box 作用域 */
76
+ /** 退出时把注册入口接到首成员;嵌套框按退出顺序由内向外组合 */
84
77
  loweringExit(ctx) {
85
- ctx.endLoweringGroup(this);
78
+ const content = ctx.endLoweringGroup(this);
79
+ const attachment = new BoxLayoutAttachment(content, this);
80
+ ctx.addAttachment(attachment);
81
+ const first = content.nodes[0];
82
+ if (first) {
83
+ if (attachment.reservedHost) {
84
+ const prepareLayout = first.prepareLayout;
85
+ first.prepareLayout = context => {
86
+ prepareLayout.call(first, context);
87
+ const space = { order: Infinity, height: Math.max(0, this.padding + this.stroke / 2) };
88
+ first.decorations.push({ above: space, below: space });
89
+ };
90
+ }
91
+ const prepare = first.prepareHorizontal;
92
+ first.prepareHorizontal = line => {
93
+ prepare?.call(first, line);
94
+ attachment.registerHorizontal(line);
95
+ };
96
+ }
86
97
  return [];
87
98
  }
88
99
  constructor(sourceSpan, args, ctx, parent = null) {
@@ -90,84 +101,89 @@ class BoxFunction extends ASTFunctionNode {
90
101
  const [tgt, pad, stroke, width] = this.getArgValue(args, ctx);
91
102
  tgt.parent = this;
92
103
  this.target = tgt;
93
- this.padding = Math.max(0, ctx.length2px(pad));
104
+ this.padding = ctx.length2px(pad);
94
105
  this.stroke = Math.max(0, ctx.length2px(stroke));
95
106
  this.width = ctx.length2px(width);
96
107
  }
108
+ toString(source) {
109
+ return `@box(${this.target.toString(source)}, padding=${this.padding}px, stroke=${this.stroke}px, width=${this.width}px)`;
110
+ }
97
111
  }
98
112
  export const BoxNode = BoxFunction;
99
113
  class BoxLayoutAttachment {
100
- layer = "background";
101
- temporalMembers;
102
- /** 写在 box 内的关系对象;lowering 递归中持续加入,因而总在 box 自身之前完成几何 */
103
- childAttachments;
114
+ content;
104
115
  owner;
116
+ layer = "background";
117
+ reservedHost;
105
118
  fixedStart = null;
106
119
  wallOffset = 0;
107
120
  get sourceSpan() { return this.owner.sourceSpan; }
108
- constructor(temporalMembers, childAttachments, owner) {
109
- this.temporalMembers = temporalMembers;
110
- this.childAttachments = childAttachments;
121
+ constructor(content, owner) {
122
+ this.content = content;
111
123
  this.owner = owner;
124
+ const [host] = content.nodes;
125
+ // 后测关系可能超出主体,只有已内含的嵌套框能提前预留完整空间
126
+ if (content.nodes.length === 1 && content.attachments.every(attachment => attachment instanceof BoxLayoutAttachment && attachment.reservedHost === host)) {
127
+ this.reservedHost = host;
128
+ }
112
129
  }
113
- /** 定宽 box 在成员覆盖的全局列上运行局部墙布局 */
114
- prepareHorizontal(context) {
115
- const layoutLine = this.temporalMembers[0]?.layoutLine;
116
- if (layoutLine === void 0)
130
+ /** 只在完整包含成员的视图注册;留白和定宽共用同一个由内向外执行的 hook */
131
+ registerHorizontal(line) {
132
+ const members = this.content.nodes;
133
+ let first = members[0];
134
+ if (!first)
117
135
  return;
118
- if (this.temporalMembers.some(member => member.layoutLine !== layoutLine)) {
136
+ if (members.some(member => member.layoutLine !== first.layoutLine)) {
119
137
  throw new ErrorDiagnostic("E_BOX_CROSS_LINE", "@box 的内容不能跨越谱面行", this.owner.sourceSpan);
120
138
  }
121
- if (this.owner.width <= 0)
122
- return;
123
- const line = context[layoutLine];
124
- if (!line)
125
- return;
126
- let first = null;
127
- let last = null;
128
- let firstIndex = Infinity;
129
- let lastIndex = -Infinity;
130
- let leftInset = 0;
131
- let rightInset = 0;
132
- // 首末列可能同时站着多个声部,取其中最宽的固有半宽
133
- for (const member of this.temporalMembers) {
139
+ let last = first;
140
+ for (const member of members) {
134
141
  const index = line.columnOf(member);
135
142
  if (index < 0)
136
- continue;
137
- const left = member.box.anchor;
138
- const right = member.box.w - left;
139
- if (index < firstIndex) {
140
- firstIndex = index;
143
+ return;
144
+ if (index < line.columnOf(first))
141
145
  first = member;
142
- leftInset = left;
143
- }
144
- else if (index === firstIndex)
145
- leftInset = Math.max(leftInset, left);
146
- if (index > lastIndex) {
147
- lastIndex = index;
146
+ if (index > line.columnOf(last))
148
147
  last = member;
149
- rightInset = right;
150
- }
151
- else if (index === lastIndex)
152
- rightInset = Math.max(rightInset, right);
153
- }
154
- if (!first || !last)
155
- return;
156
- if (this.owner.width < leftInset + rightInset - 1e-6) {
157
- throw new ErrorDiagnostic("E_BOX_WIDTH_TOO_SMALL", "@box 的宽度小于首末元素的固有宽度", this.owner.sourceSpan);
158
148
  }
149
+ const { width, padding, stroke } = this.owner;
159
150
  line.registerHorizontalLayoutHook(first, last, ({ columns, rows, start, end, X, fixed, options, }) => {
160
- if (fixed[start - 1] || fixed[end]) {
161
- throw new ErrorDiagnostic("E_BOX_CONSTRAINT_CROSSING", "定宽 @box 可以嵌套,但不能部分交叉", this.owner.sourceSpan);
162
- }
163
- const regionX = X.subarray(start, end + 1);
164
- layoutHorizontalRegion(columns.slice(start, end + 1), rows, regionX, fixed.subarray(start, end), this.owner.width, options, 0);
165
- const actualWidth = regionX[regionX.length - 1] - regionX[0] + leftInset + rightInset;
166
- if (Math.abs(actualWidth - this.owner.width) > 1e-6) {
167
- throw new ErrorDiagnostic("E_BOX_CONSTRAINT_CONFLICT", "多个 @box 为相同内容指定了不同宽度", this.owner.sourceSpan);
151
+ const edge = (index) => columns[index].filter(element => members.some(member => member.box === element.box));
152
+ const left = edge(start);
153
+ const right = edge(end);
154
+ if (width > 0) {
155
+ const leftInset = Math.max(...left.map(element => element.WL));
156
+ const rightInset = Math.max(...right.map(element => element.WR));
157
+ if (width < leftInset + rightInset - 1e-6) {
158
+ throw new ErrorDiagnostic("E_BOX_WIDTH_TOO_SMALL", "@box 的宽度小于首末元素的固有宽度", this.owner.sourceSpan);
159
+ }
160
+ if (fixed[start - 1] || fixed[end]) {
161
+ throw new ErrorDiagnostic("E_BOX_CONSTRAINT_CROSSING", "定宽 @box 可以嵌套,但不能部分交叉", this.owner.sourceSpan);
162
+ }
163
+ const regionX = X.subarray(start, end + 1);
164
+ if (start === end) {
165
+ const extra = (width - leftInset - rightInset) / 2;
166
+ for (const element of left)
167
+ element.WL += extra;
168
+ for (const element of right)
169
+ element.WR += extra;
170
+ regionX[0] = leftInset + extra;
171
+ }
172
+ else {
173
+ layoutHorizontalRegion(columns.slice(start, end + 1), rows, regionX, fixed.subarray(start, end), width, options, 0);
174
+ const actualWidth = regionX[regionX.length - 1] - regionX[0] + leftInset + rightInset;
175
+ if (Math.abs(actualWidth - width) > 1e-6) {
176
+ throw new ErrorDiagnostic("E_BOX_CONSTRAINT_CONFLICT", "多个 @box 为相同内容指定了不同宽度", this.owner.sourceSpan);
177
+ }
178
+ }
179
+ this.fixedStart = first;
180
+ this.wallOffset = -regionX[0];
168
181
  }
169
- this.fixedStart = first;
170
- this.wallOffset = -regionX[0];
182
+ const inset = padding + stroke / 2;
183
+ for (const element of left)
184
+ element.WL += inset;
185
+ for (const element of right)
186
+ element.WR += inset;
171
187
  });
172
188
  }
173
189
  /**
@@ -176,17 +192,12 @@ class BoxLayoutAttachment {
176
192
  */
177
193
  createGeometry(context) {
178
194
  const { padding, stroke } = this.owner;
179
- const rect = { x: 0, y: 0, w: 0, h: 0 };
180
- const members = [
181
- ...this.temporalMembers.map(member => member.box),
182
- ...this.childAttachments.map(attachment => context.getAttachmentBox(attachment)),
183
- ];
184
- // 未命名歌词等 attachment 会保留全零盒,不能让它把边框拉到文档原点
185
- if (!unionLayoutBoxes(rect, members.filter(member => member.w > 0 || member.h > 0))) {
195
+ const rect = context.getContentBounds(this.content);
196
+ if (!rect) {
186
197
  return { regions: [], paint() { } };
187
198
  }
188
199
  const inset = padding + stroke / 2;
189
- // 边框不抢轨道纵向空间,只参与画布边界
200
+ // 单主体已提前预留空间,范围框在最终测量时申报外围占用
190
201
  const fixedX = this.fixedStart
191
202
  ? this.fixedStart.box.x + this.fixedStart.box.anchor + this.wallOffset
192
203
  : rect.x;
@@ -196,8 +207,12 @@ class BoxLayoutAttachment {
196
207
  w: (this.fixedStart ? this.owner.width : rect.w) + inset * 2,
197
208
  h: rect.h + inset * 2,
198
209
  };
210
+ if (region.w < stroke || region.h < stroke) {
211
+ throw new ErrorDiagnostic("E_BOX_PADDING_TOO_SMALL", "@box 的负内边距使边框宽度或高度小于零", this.owner.sourceSpan);
212
+ }
199
213
  return {
200
214
  regions: [region],
215
+ occupancy: this.reservedHost ? [] : this.occupancy(context, region),
201
216
  paint(painter) {
202
217
  const strokeInset = stroke / 2;
203
218
  painter.drawRect(region.x + strokeInset, region.y + strokeInset, Math.max(0, region.w - stroke), Math.max(0, region.h - stroke), {
@@ -207,4 +222,39 @@ class BoxLayoutAttachment {
207
222
  },
208
223
  };
209
224
  }
225
+ occupancy(context, region) {
226
+ const owners = new Set();
227
+ let line;
228
+ const include = (index, track) => {
229
+ if (line !== undefined && line !== index) {
230
+ throw new ErrorDiagnostic("E_BOX_CROSS_LINE", "@box 的内容不能跨越谱面行", this.owner.sourceSpan);
231
+ }
232
+ line = index;
233
+ owners.add(track);
234
+ };
235
+ for (const node of this.content.nodes)
236
+ include(node.layoutLine, node.track);
237
+ for (const attachment of this.content.attachments) {
238
+ if (!isLayoutAttachment(attachment))
239
+ continue;
240
+ const occupancy = context.getAttachmentOccupancy(attachment);
241
+ if (!occupancy)
242
+ continue;
243
+ for (const point of attachment.endPoints ?? [])
244
+ include(point.layoutLine, point.track);
245
+ for (const part of occupancy) {
246
+ if (part.line !== undefined)
247
+ include(part.line, part.track);
248
+ }
249
+ }
250
+ if (line === undefined)
251
+ return [];
252
+ const index = line;
253
+ const sorted = [...owners].sort((left, right) => context.getVisualAxis(index, left) - context.getVisualAxis(index, right));
254
+ // 整框只申报外围上下沿,不能把内部轨距重复计入每条轨道
255
+ return [
256
+ { ...region, h: 0, line: index, track: sorted[0] },
257
+ { ...region, y: region.y + region.h, h: 0, line: index, track: sorted.at(-1) },
258
+ ];
259
+ }
210
260
  }
@@ -9,7 +9,7 @@ class DashFunction extends ASTFunctionNode {
9
9
  ~~~jpfun
10
10
  1 @dash()
11
11
  ~~~
12
- 无参数。添加一根增时线,延续前方音符的发声;也可简写为 \`1 -\`。每根线的基础时值为一个四分音符,可用减时线或附点调整。`,
12
+ 无参数。添加一根增时线,扩展前方音符或休止符的完整时值;也可简写为 \`1 -\`。每根线的基础时值为一个四分音符,可用减时线或附点调整。装饰音覆盖延长后的完整音符,例如 \`1 ^ $tr -\` 在两拍内持续颤音。`,
13
13
  allowExtraArgs: false,
14
14
  args: []
15
15
  };
@@ -35,6 +35,7 @@ class DashFunction extends ASTFunctionNode {
35
35
  loweringEnter() {
36
36
  return [new DashTemporalNode(this)];
37
37
  }
38
+ labelable() { return this; }
38
39
  toString() { return "-"; }
39
40
  }
40
41
  export const DashNode = DashFunction;
@@ -48,6 +49,7 @@ class DashTemporalNode extends TemporalNodeBase {
48
49
  this.mergeKey = DEFAULT_KEY;
49
50
  this.initLayoutBox();
50
51
  }
52
+ /** 固化记谱位置的速度,控制流直接跳到增时线时也能恢复正确状态 */
51
53
  onTimeState(state) {
52
54
  this.playbackState = { bpm: state.bpm };
53
55
  }
@@ -62,31 +64,38 @@ class DashTemporalNode extends TemporalNodeBase {
62
64
  this.lineY = size * 0.5;
63
65
  this.box.visualAxis = this.lineY;
64
66
  }
67
+ /**
68
+ * 在结构阶段延长前方同轨、时间相接的一组区间
69
+ * 有声与无声目标都保留原对象,后续声音变换才能使用延长后的完整时值
70
+ */
65
71
  emitPlayback(emitter) {
66
72
  const start = emitter.start.clone();
67
- const end = emitter.end.clone();
68
73
  const track = emitter.track;
69
74
  const sourceSpan = this.ast.sourceSpan;
75
+ // 这里只查询已发布的结构前缀;装饰音尚未展开,不需要逐个修改子音。
70
76
  emitter.defer(context => {
71
77
  let rootOrigin;
72
- for (let i = context.events.length - 1; i >= 0; i--) {
73
- const event = context.events[i];
74
- if (event.kind !== "note-off" || event.origins.at(-1).node.track !== track)
78
+ // 从最近发布的区间向前找,锁定与当前起点相接的那次顶层访问。
79
+ // 来源身份限定了整组目标,防止延长更早的音符或另一遍反复中的同一节点。
80
+ for (let index = context.spans.length - 1; index >= 0; index--) {
81
+ const span = context.spans[index];
82
+ if (span.track !== track)
75
83
  continue;
76
84
  if (rootOrigin === undefined) {
77
- if (!event.at.equals(start))
85
+ if (!span.end.equals(start))
78
86
  continue;
79
- rootOrigin = event.origins[0];
87
+ rootOrigin = span.origins[0];
80
88
  }
81
- else if (event.origins[0] !== rootOrigin) {
89
+ else if (span.origins[0] !== rootOrigin) {
82
90
  break;
83
91
  }
84
- if (event.at.equals(start)) {
85
- event.at.copyFrom(end);
92
+ // 一组目标可以含多个成员;extend 同时承接当前访问在新增部分上的速度效果。
93
+ if (span.end.equals(start)) {
94
+ emitter.extend(span);
86
95
  }
87
96
  }
88
97
  if (rootOrigin === undefined) {
89
- context.diagnostics.push(new WarningDiagnostic("W_PLAYBACK_SUSTAIN_WITHOUT_TARGET", "增时线前没有可延续的发声音符", sourceSpan));
98
+ context.diagnostics.push(new WarningDiagnostic("W_PLAYBACK_SUSTAIN_WITHOUT_TARGET", "增时线前没有可延续的音段", sourceSpan));
90
99
  }
91
100
  });
92
101
  }
@@ -13,6 +13,7 @@ import { KeyNode } from "./key/index.js";
13
13
  import { MeterNode } from "./meter/index.js";
14
14
  import { BrNode } from "./br/index.js";
15
15
  import { NoteNode } from "./note/index.js";
16
+ import { RestNode } from "./rest/index.js";
16
17
  import { UpNode, DownNode } from "./up/index.js";
17
18
  import { PageNode } from "./page/index.js";
18
19
  import { ProgramNode } from "./program/index.js";
@@ -26,7 +27,7 @@ import { TieNode } from "./tie/index.js";
26
27
  import { TupletNode } from "./tuplet/index.js";
27
28
  import { VoiceNode, VoicesNode } from "./voice/index.js";
28
29
  export const defaultFunctions = [
29
- NoteNode, DashNode, BarNode, // 有实体
30
+ NoteNode, DashNode, RestNode, BarNode,
30
31
  DivNode, DotNode, TupletNode, // 装饰性
31
32
  VoiceNode, VoicesNode, // 歌词和声部
32
33
  BrNode, // 排版
@@ -25,10 +25,10 @@ declare class DivFunction extends ASTFunctionNode {
25
25
  details: string;
26
26
  allowExtraArgs: boolean;
27
27
  args: ({
28
+ name?: undefined;
28
29
  type: "content";
29
30
  description: string;
30
31
  default: null;
31
- name?: undefined;
32
32
  } | {
33
33
  name: string;
34
34
  description: string;
@@ -9,7 +9,7 @@ export function divLinePortName(level, edge) {
9
9
  export function claimDivLine(host, level) {
10
10
  const port = host.ports[divLinePortName(level, "left")];
11
11
  if (port)
12
- port.claimed = true;
12
+ port.state.claimed = true;
13
13
  }
14
14
  /** 接管绘制的函数必须用同一条公式,否则同一条减时线会因为谁画而粗细不一 */
15
15
  export function divLineWidth(size) {
@@ -100,10 +100,10 @@ class DivFunction extends ASTFunctionNode {
100
100
  const n = nodes[at++];
101
101
  if (n.data?.class !== DivFunction)
102
102
  return null;
103
- // 向前找到第一个有效节点
104
- const prev = ASTFunctionNode.findLastFuncContentNode(ctx.nodes, ctx.nodes.length - 1);
105
- if (!prev)
103
+ const prevIndex = ASTFunctionNode.findLastFuncContentIndex(ctx.nodes, ctx.nodes.length - 1);
104
+ if (prevIndex < 0)
106
105
  return at; // 没有了 直接当作无效文本跳过
106
+ const prev = ctx.nodes[prevIndex];
107
107
  if (prev instanceof DivFunction) {
108
108
  // 已经是div了 继续加深
109
109
  prev.n += n.data.n;
@@ -117,8 +117,7 @@ class DivFunction extends ASTFunctionNode {
117
117
  start: Math.min(spanPrev.start, n.span.start),
118
118
  end: Math.max(spanPrev.end, n.span.end),
119
119
  }, argMap, ctx, null);
120
- ctx.nodes.pop(); // 消耗掉prev
121
- ctx.pushNode(node);
120
+ ctx.nodes[prevIndex] = node;
122
121
  return at;
123
122
  }
124
123
  /**
@@ -136,11 +135,10 @@ class DivFunction extends ASTFunctionNode {
136
135
  let lineRight = 0;
137
136
  let firstLine = 0;
138
137
  return {
139
- // 减时线声明为靠近主体的下方项
140
- // 引擎只按通用 order 排列,不识别 @div
141
138
  below: {
142
139
  order: 0,
143
- gap: -host.ast.size * 0.08,
140
+ // 画线沿用正文盒基准,间距换算到装饰排列起点。
141
+ gap: host.box.h - (host.ports["decoration.below"]?.y ?? host.box.h) - host.ast.size * 0.08,
144
142
  height: lineBlockHeight,
145
143
  place(y) {
146
144
  lineLeft = host.ports["body.left"]?.x ?? 0;
@@ -153,6 +151,7 @@ class DivFunction extends ASTFunctionNode {
153
151
  host.ports[divLinePortName(i, "left")] = {
154
152
  x: lineLeft,
155
153
  y: lineY,
154
+ state: { claimed: false },
156
155
  };
157
156
  host.ports[divLinePortName(i, "right")] = {
158
157
  x: lineRight,
@@ -165,7 +164,7 @@ class DivFunction extends ASTFunctionNode {
165
164
  // 未被关系对象接管的级别仍由本装饰独立绘制
166
165
  for (let i = 0; i < count; i++) {
167
166
  const leftPort = host.ports[divLinePortName(i, "left")];
168
- if (leftPort.claimed)
167
+ if (leftPort.state.claimed)
169
168
  continue;
170
169
  const y = host.box.y + firstLine + i * lineGap;
171
170
  painter.drawLine(host.box.x + lineLeft, y, host.box.x + lineRight, y, { stroke: "#000", strokeWidth });
@@ -58,10 +58,10 @@ class DotFunction extends ASTFunctionNode {
58
58
  const n = nodes[at++];
59
59
  if (n.data?.class !== DotFunction)
60
60
  return null;
61
- // 向前找到第一个有效节点
62
- const prev = ASTFunctionNode.findLastFuncContentNode(ctx.nodes, ctx.nodes.length - 1);
63
- if (!prev)
61
+ const prevIndex = ASTFunctionNode.findLastFuncContentIndex(ctx.nodes, ctx.nodes.length - 1);
62
+ if (prevIndex < 0)
64
63
  return at; // 没有了 直接当作无效文本跳过
64
+ const prev = ctx.nodes[prevIndex];
65
65
  if (prev instanceof DotFunction) {
66
66
  // 已经是dot了 继续加深
67
67
  prev.n += n.data.n;
@@ -76,8 +76,7 @@ class DotFunction extends ASTFunctionNode {
76
76
  start: Math.min(spanPrev.start, n.span.start),
77
77
  end: Math.max(spanPrev.end, n.span.end),
78
78
  }, argMap, ctx, null);
79
- ctx.nodes.pop(); // 消耗掉prev
80
- ctx.pushNode(node);
79
+ ctx.nodes[prevIndex] = node;
81
80
  return at;
82
81
  }
83
82
  /**
@@ -10,7 +10,9 @@ class DynFunction extends ASTFunctionNode {
10
10
  1 ^ $p 2@a 3 4@b
11
11
  @dyn(a, b, 24)
12
12
  ~~~
13
- 端点须位于同一音轨、不同时间;变化量沿区间线性增加,叠加在每个音符原有力度上。本例从 \`a\` 到 \`b\` 渐强,终点增加 24 的力度。`,
13
+ 端点须位于同一音轨、不同时间;变化量沿区间线性增加,叠加在每个音符原有力度上。本例从 \`a\` 到 \`b\` 渐强,终点增加 24 的力度。
14
+
15
+ 端点也可标在增时线上。终点没有力度时,取同轨、不晚于终点的最后一个原始力度作为保持基准;找不到时给出警告并仅绘制。增时线延续的音符保持起音时的力度。`,
14
16
  allowExtraArgs: false,
15
17
  args: [
16
18
  { name: "from", description: "渐变起点的标签", type: "label", default: null },
@@ -38,10 +40,18 @@ class DynFunction extends ASTFunctionNode {
38
40
  records.sort((left, right) => left.host.t.compare(right.host.t) || left.node.order - right.node.order);
39
41
  // 先累计全部 dyn 的贡献,最后统一写回,避免声明顺序改变后续 dyn 读取的原始力度
40
42
  for (const dynamic of dynamics) {
41
- const endVelocity = dynamic.end.playbackState?.velocity
43
+ let endVelocity = dynamic.end.playbackState?.velocity
42
44
  ?? records.find(record => record.host === dynamic.toHost)?.velocity;
43
- if (endVelocity === undefined)
44
- throw new Error("@dyn end endpoint has no velocity-bearing member");
45
+ for (let index = records.length - 1; endVelocity === undefined && index >= 0; index--) {
46
+ const record = records[index];
47
+ if (record.host.track === dynamic.track && record.host.t.compare(dynamic.toHost.t) <= 0) {
48
+ endVelocity = record.velocity;
49
+ }
50
+ }
51
+ if (endVelocity === undefined) {
52
+ result.diagnostics.push(new WarningDiagnostic("W_DYN_NO_VELOCITY", "@dyn 终点及此前同轨没有可用的力度基准,仅绘制渐变线", dynamic.sourceSpan));
53
+ continue;
54
+ }
45
55
  // 终点后保持完整增量,直到原始力度相对终点再次发生变化
46
56
  const stop = records.find(record => record.host.track === dynamic.track
47
57
  && record.host.t.compare(dynamic.toHost.t) > 0
@@ -136,49 +146,35 @@ class DynAttachment {
136
146
  this.toHost = toHost;
137
147
  }
138
148
  get sourceSpan() { return this.ast.sourceSpan; }
149
+ get endPoints() { return [this.fromHost, this.toHost]; }
139
150
  get dv() { return this.ast.dv; }
140
151
  get track() { return this.fromHost.track; }
141
152
  createGeometry(context) {
142
- const locate = (host) => {
143
- const view = context.lines[host.layoutLine];
144
- return { line: host.layoutLine, view, column: view?.columnOf(host) ?? -1 };
145
- };
146
- const from = locate(this.fromHost);
147
- const to = locate(this.toHost);
148
- if (!from.view || !to.view || from.column < 0 || to.column < 0) {
149
- return { regions: [], paint() { } };
150
- }
153
+ const fromLine = this.fromHost.layoutLine;
154
+ const toLine = this.toHost.layoutLine;
155
+ const endpoints = [this.fromHost, this.toHost];
151
156
  const ranges = [];
152
157
  let size = this.ast.size;
153
- for (let line = from.line; line <= to.line; line++) {
154
- const view = context.lines[line];
155
- const start = line === from.line ? from.column : 0;
156
- const end = line === to.line ? to.column : view.columns.length - 1;
157
- let firstColumn = -1;
158
- let lastColumn = -1;
159
- for (let column = start; column <= end; column++) {
160
- for (const host of view.columns[column]) {
161
- if (host.track !== this.track)
162
- continue;
163
- firstColumn = firstColumn < 0 ? column : firstColumn;
164
- lastColumn = column;
158
+ for (let line = fromLine; line <= toLine; line++) {
159
+ const columns = context.getRangeColumns(line, endpoints, this.track);
160
+ if (!columns.some(column => column.length > 0))
161
+ continue;
162
+ for (const column of columns) {
163
+ for (const host of column) {
165
164
  size = Math.max(size, host.ast.size);
166
165
  }
167
166
  }
168
- if (firstColumn < 0)
169
- continue;
170
167
  ranges.push({
171
168
  line,
172
- startX: line === from.line ? this.fromHost.box.x : this.lineEntryX(context, line),
173
- endX: line === to.line
169
+ startX: line === fromLine ? this.fromHost.box.x : this.lineEntryX(context, line),
170
+ endX: line === toLine
174
171
  ? this.toHost.box.x + this.toHost.box.w
175
172
  : this.lineRightX(context, line),
176
- columns: [firstColumn, lastColumn],
177
173
  });
178
174
  }
179
175
  // 允许左边超出一点
180
176
  for (const range of ranges) {
181
- if (range.line > from.line)
177
+ if (range.line > fromLine)
182
178
  range.startX -= size / 4;
183
179
  }
184
180
  const strokeWidth = Math.max(0.75, size * 0.035);
@@ -194,7 +190,7 @@ class DynAttachment {
194
190
  const start = covered / total;
195
191
  const end = (covered + width) / total;
196
192
  covered += width;
197
- const extent = context.getRangeExtents(range.line, range.columns).get(this.track);
193
+ const extent = context.getRangeExtents(range.line, endpoints, this.track);
198
194
  const axis = context.getVisualAxis(range.line, this.track);
199
195
  const hostTop = extent ? axis + extent.top : axis;
200
196
  return {