jpfun 0.4.1 → 0.5.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 (73) 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 +2 -3
  14. package/dist/functions/ASTtypes.js +5 -9
  15. package/dist/functions/adjust/index.js +4 -48
  16. package/dist/functions/arpeggio/index.js +21 -13
  17. package/dist/functions/box/index.js +81 -82
  18. package/dist/functions/dash/index.js +20 -11
  19. package/dist/functions/div/index.d.ts +1 -1
  20. package/dist/functions/div/index.js +7 -7
  21. package/dist/functions/dot/index.js +4 -5
  22. package/dist/functions/dyn/index.js +28 -32
  23. package/dist/functions/grace/index.d.ts +19 -6
  24. package/dist/functions/grace/index.js +123 -115
  25. package/dist/functions/key/index.js +1 -1
  26. package/dist/functions/meter/index.js +14 -1
  27. package/dist/functions/note/index.d.ts +6 -1
  28. package/dist/functions/note/index.js +37 -14
  29. package/dist/functions/note/source-transform.d.ts +21 -0
  30. package/dist/functions/note/source-transform.js +164 -0
  31. package/dist/functions/page/index.d.ts +5 -0
  32. package/dist/functions/page/index.js +8 -5
  33. package/dist/functions/set/index.js +5 -1
  34. package/dist/functions/symbol/index.d.ts +2 -0
  35. package/dist/functions/symbol/index.js +1 -1
  36. package/dist/functions/symbol/ornament.d.ts +3 -3
  37. package/dist/functions/symbol/ornament.js +21 -35
  38. package/dist/functions/symbol/symbols/accent.js +5 -5
  39. package/dist/functions/symbol/symbols/fermata.js +5 -4
  40. package/dist/functions/tie/index.js +22 -32
  41. package/dist/functions/tuplet/index.js +19 -23
  42. package/dist/functions/up/index.js +9 -9
  43. package/dist/functions/voice/index.js +4 -3
  44. package/dist/functions/volta/index.js +7 -12
  45. package/dist/jpfun.core.min.js +40 -37
  46. package/dist/jpfun.from-midi.min.js +5 -5
  47. package/dist/jpfun.from-musicxml.min.js +8 -8
  48. package/dist/jpfun.min.js +49 -46
  49. package/dist/layout/engine.d.ts +11 -1
  50. package/dist/layout/engine.js +299 -96
  51. package/dist/layout/page.js +2 -0
  52. package/dist/layout/range.d.ts +26 -0
  53. package/dist/layout/range.js +75 -0
  54. package/dist/layout/types.d.ts +33 -4
  55. package/dist/lowering/types.d.ts +5 -0
  56. package/dist/parser/parse-utils/length-utils.js +2 -0
  57. package/dist/parser/parse-utils/note-utils.d.ts +2 -0
  58. package/dist/parser/parse-utils/note-utils.js +6 -2
  59. package/dist/parser/parserContext.js +1 -1
  60. package/dist/playback/compile.d.ts +2 -9
  61. package/dist/playback/compile.js +297 -186
  62. package/dist/playback/event.d.ts +8 -16
  63. package/dist/playback/event.js +13 -3
  64. package/dist/playback/types.d.ts +76 -18
  65. package/dist/playback/types.js +1 -0
  66. package/dist/render/index.d.ts +1 -1
  67. package/dist/render/index.js +1 -1
  68. package/dist/render/recording.d.ts +1 -0
  69. package/dist/render/recording.js +21 -0
  70. package/dist/render/translate.d.ts +14 -0
  71. package/dist/render/translate.js +48 -0
  72. package/grammar.md +13 -6
  73. package/package.json +4 -4
@@ -1,9 +1,7 @@
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
- import { isLayoutAttachment } from "../../layout/types.js";
7
5
  class BoxFunction extends ASTFunctionNode {
8
6
  static def = {
9
7
  name: ["box"],
@@ -21,7 +19,7 @@ class BoxFunction extends ASTFunctionNode {
21
19
  },
22
20
  {
23
21
  name: "padding",
24
- description: "内容到框线的内边距",
22
+ description: "内容到框线的内边距,负值向内收缩",
25
23
  type: "length",
26
24
  default: {
27
25
  value: 0,
@@ -64,25 +62,29 @@ class BoxFunction extends ASTFunctionNode {
64
62
  * 进入目标内容前只开始收集所有成员矩形的引用
65
63
  */
66
64
  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
- });
65
+ const group = {
66
+ nodes: [],
67
+ attachments: [],
68
+ onTemporal(node) { if (isVisualTemporalNode(node))
69
+ group.nodes.push(node); },
70
+ onAttachment(attachment) { group.attachments.push(attachment); },
71
+ };
72
+ ctx.beginLoweringGroup(this, group);
81
73
  return [];
82
74
  }
83
- /** 目标内容完成 lowering 后结束当前 box 作用域 */
75
+ /** 退出时把注册入口接到首成员;嵌套框按退出顺序由内向外组合 */
84
76
  loweringExit(ctx) {
85
- ctx.endLoweringGroup(this);
77
+ const content = ctx.endLoweringGroup(this);
78
+ const attachment = new BoxLayoutAttachment(content, this);
79
+ ctx.addAttachment(attachment);
80
+ const first = content.nodes[0];
81
+ if (first) {
82
+ const prepare = first.prepareHorizontal;
83
+ first.prepareHorizontal = line => {
84
+ prepare?.call(first, line);
85
+ attachment.registerHorizontal(line);
86
+ };
87
+ }
86
88
  return [];
87
89
  }
88
90
  constructor(sourceSpan, args, ctx, parent = null) {
@@ -90,84 +92,83 @@ class BoxFunction extends ASTFunctionNode {
90
92
  const [tgt, pad, stroke, width] = this.getArgValue(args, ctx);
91
93
  tgt.parent = this;
92
94
  this.target = tgt;
93
- this.padding = Math.max(0, ctx.length2px(pad));
95
+ this.padding = ctx.length2px(pad);
94
96
  this.stroke = Math.max(0, ctx.length2px(stroke));
95
97
  this.width = ctx.length2px(width);
96
98
  }
99
+ toString(source) {
100
+ return `@box(${this.target.toString(source)}, padding=${this.padding}px, stroke=${this.stroke}px, width=${this.width}px)`;
101
+ }
97
102
  }
98
103
  export const BoxNode = BoxFunction;
99
104
  class BoxLayoutAttachment {
100
- layer = "background";
101
- temporalMembers;
102
- /** 写在 box 内的关系对象;lowering 递归中持续加入,因而总在 box 自身之前完成几何 */
103
- childAttachments;
105
+ content;
104
106
  owner;
107
+ layer = "background";
105
108
  fixedStart = null;
106
109
  wallOffset = 0;
107
110
  get sourceSpan() { return this.owner.sourceSpan; }
108
- constructor(temporalMembers, childAttachments, owner) {
109
- this.temporalMembers = temporalMembers;
110
- this.childAttachments = childAttachments;
111
+ constructor(content, owner) {
112
+ this.content = content;
111
113
  this.owner = owner;
112
114
  }
113
- /** 定宽 box 在成员覆盖的全局列上运行局部墙布局 */
114
- prepareHorizontal(context) {
115
- const layoutLine = this.temporalMembers[0]?.layoutLine;
116
- if (layoutLine === void 0)
115
+ /** 只在完整包含成员的视图注册;留白和定宽共用同一个由内向外执行的 hook */
116
+ registerHorizontal(line) {
117
+ const members = this.content.nodes;
118
+ let first = members[0];
119
+ if (!first)
117
120
  return;
118
- if (this.temporalMembers.some(member => member.layoutLine !== layoutLine)) {
121
+ if (members.some(member => member.layoutLine !== first.layoutLine)) {
119
122
  throw new ErrorDiagnostic("E_BOX_CROSS_LINE", "@box 的内容不能跨越谱面行", this.owner.sourceSpan);
120
123
  }
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) {
124
+ let last = first;
125
+ for (const member of members) {
134
126
  const index = line.columnOf(member);
135
127
  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;
128
+ return;
129
+ if (index < line.columnOf(first))
141
130
  first = member;
142
- leftInset = left;
143
- }
144
- else if (index === firstIndex)
145
- leftInset = Math.max(leftInset, left);
146
- if (index > lastIndex) {
147
- lastIndex = index;
131
+ if (index > line.columnOf(last))
148
132
  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
133
  }
134
+ const { width, padding, stroke } = this.owner;
159
135
  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);
136
+ const edge = (index) => columns[index].filter(element => members.some(member => member.box === element.box));
137
+ const left = edge(start);
138
+ const right = edge(end);
139
+ if (width > 0) {
140
+ const leftInset = Math.max(...left.map(element => element.WL));
141
+ const rightInset = Math.max(...right.map(element => element.WR));
142
+ if (width < leftInset + rightInset - 1e-6) {
143
+ throw new ErrorDiagnostic("E_BOX_WIDTH_TOO_SMALL", "@box 的宽度小于首末元素的固有宽度", this.owner.sourceSpan);
144
+ }
145
+ if (fixed[start - 1] || fixed[end]) {
146
+ throw new ErrorDiagnostic("E_BOX_CONSTRAINT_CROSSING", "定宽 @box 可以嵌套,但不能部分交叉", this.owner.sourceSpan);
147
+ }
148
+ const regionX = X.subarray(start, end + 1);
149
+ if (start === end) {
150
+ const extra = (width - leftInset - rightInset) / 2;
151
+ for (const element of left)
152
+ element.WL += extra;
153
+ for (const element of right)
154
+ element.WR += extra;
155
+ regionX[0] = leftInset + extra;
156
+ }
157
+ else {
158
+ layoutHorizontalRegion(columns.slice(start, end + 1), rows, regionX, fixed.subarray(start, end), width, options, 0);
159
+ const actualWidth = regionX[regionX.length - 1] - regionX[0] + leftInset + rightInset;
160
+ if (Math.abs(actualWidth - width) > 1e-6) {
161
+ throw new ErrorDiagnostic("E_BOX_CONSTRAINT_CONFLICT", "多个 @box 为相同内容指定了不同宽度", this.owner.sourceSpan);
162
+ }
163
+ }
164
+ this.fixedStart = first;
165
+ this.wallOffset = -regionX[0];
162
166
  }
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);
168
- }
169
- this.fixedStart = first;
170
- this.wallOffset = -regionX[0];
167
+ const inset = padding + stroke / 2;
168
+ for (const element of left)
169
+ element.WL += inset;
170
+ for (const element of right)
171
+ element.WR += inset;
171
172
  });
172
173
  }
173
174
  /**
@@ -176,13 +177,8 @@ class BoxLayoutAttachment {
176
177
  */
177
178
  createGeometry(context) {
178
179
  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))) {
180
+ const rect = context.getContentBounds(this.content);
181
+ if (!rect) {
186
182
  return { regions: [], paint() { } };
187
183
  }
188
184
  const inset = padding + stroke / 2;
@@ -196,6 +192,9 @@ class BoxLayoutAttachment {
196
192
  w: (this.fixedStart ? this.owner.width : rect.w) + inset * 2,
197
193
  h: rect.h + inset * 2,
198
194
  };
195
+ if (region.w < stroke || region.h < stroke) {
196
+ throw new ErrorDiagnostic("E_BOX_PADDING_TOO_SMALL", "@box 的负内边距使边框宽度或高度小于零", this.owner.sourceSpan);
197
+ }
199
198
  return {
200
199
  regions: [region],
201
200
  paint(painter) {
@@ -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
  }
@@ -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
  /**
@@ -153,6 +152,7 @@ class DivFunction extends ASTFunctionNode {
153
152
  host.ports[divLinePortName(i, "left")] = {
154
153
  x: lineLeft,
155
154
  y: lineY,
155
+ state: { claimed: false },
156
156
  };
157
157
  host.ports[divLinePortName(i, "right")] = {
158
158
  x: lineRight,
@@ -165,7 +165,7 @@ class DivFunction extends ASTFunctionNode {
165
165
  // 未被关系对象接管的级别仍由本装饰独立绘制
166
166
  for (let i = 0; i < count; i++) {
167
167
  const leftPort = host.ports[divLinePortName(i, "left")];
168
- if (leftPort.claimed)
168
+ if (leftPort.state.claimed)
169
169
  continue;
170
170
  const y = host.box.y + firstLine + i * lineGap;
171
171
  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 {
@@ -1,6 +1,7 @@
1
1
  import type { LayoutBox, LayoutPrepareContext } from "../../layout/types.js";
2
2
  import type { LoweringContext } from "../../lowering/loweringContext.js";
3
- import type { Track } from "../../lowering/track.js";
3
+ import type { LoweringAttachment } from "../../lowering/types.js";
4
+ import { Track } from "../../lowering/track.js";
4
5
  import { TemporalNodeBase, type TimeState, type VisualTemporalNode } from "../temporal.js";
5
6
  import { GrammarNode, GrammarSugarNode } from "../../parser/grammarType.js";
6
7
  import { ParserContext } from "../../parser/parserContext.js";
@@ -31,7 +32,7 @@ declare class GraceFunction extends ASTFunctionNode {
31
32
  constructor(span: SourceSpan, args: FunctionArgs, ctx: ParserContext, parent?: ASTNodeBase | null, scale?: number);
32
33
  setGrace(node: ASTNodeBase, factor?: number): void;
33
34
  private adopt;
34
- /** 倚音与宿主各自收敛成可见 Temporal,然后整体折叠进一个复合节点 */
35
+ /** 宿主收敛成一个可见 Temporal,倚音展开成局部时间列,然后整体折叠进一个复合节点 */
35
36
  loweringEnter(ctx: LoweringContext, track: Track): GraceTemporal[];
36
37
  toString(source: string): string;
37
38
  }
@@ -41,29 +42,41 @@ export declare class GraceTemporal extends TemporalNodeBase {
41
42
  ast: GraceFunction;
42
43
  box: LayoutBox;
43
44
  readonly host: VisualTemporalNode;
45
+ /** 可见倚音按局部时间成列,同列成员分属不同声部 */
46
+ readonly graceColumns: readonly (readonly VisualTemporalNode[])[];
47
+ /** graceColumns 按列展开 */
44
48
  readonly graces: readonly VisualTemporalNode[];
45
- /** 承担节奏的倚音成员,按零时长标记切段;同段视觉上首尾相接,无条件连成一束减时线 */
49
+ /** 承担节奏的倚音成员,按声部与零时长标记切段;同段视觉上首尾相接,无条件连成一束减时线 */
46
50
  readonly graceRuns: readonly (readonly VisualTemporalNode[])[];
47
51
  readonly side: GraceSide;
52
+ /** 倚音子域的根轨:由宿主轨分出,不挂进它的分组,子域之外不可见 */
53
+ private readonly root;
54
+ /** 倚音内容里声明的附件,随子域排版 */
55
+ private readonly attachments;
56
+ /** 倚音内容的全部事件,含不可见的状态事件,按局部时间列展开 */
57
+ private readonly events;
58
+ /** 与 events 一一对应的局部起点,已随时值减半 */
59
+ private readonly starts;
48
60
  /** 成员相对本盒左上角的局部偏移,onPlaced 时同步为绝对坐标 */
49
61
  private hostOffset;
50
62
  private graceOffsets;
51
63
  /** 倚音线起点,同样是局部偏移 */
52
64
  private hookOrigin;
53
- constructor(ast: GraceFunction, host: VisualTemporalNode, graces: readonly VisualTemporalNode[], side: GraceSide);
65
+ constructor(ast: GraceFunction, host: VisualTemporalNode, columns: readonly (readonly TemporalNodeBase[])[], root: Track, attachments: readonly LoweringAttachment[], side: GraceSide);
54
66
  /** 成员共享全局时间状态,按发声顺序固化 */
55
67
  onTimeState(state: TimeState): void;
56
68
  /**
57
69
  * 播放时倚音从宿主借走的时值(前倚音从开头、后倚音从末尾)
58
70
  *
59
- * 比例 = 倚音字面总时值 / 四分音符,而四分音符 T=1,所以直接累加成员的 T。
71
+ * 比例 = 倚音块字面时长 / 四分音符,而四分音符 T=1,所以直接取各声部最晚的局部终点。
60
72
  * 基准取宿主经过 div 与 dot 后的实际时值;延时线是独立事件、tie 不改 T,天然不计入。
73
+ * 局部时间按同一比例映射进借走的时值,各声部因此同时起奏。
61
74
  */
62
75
  emitPlayback(emitter: PlaybackEmitter): void;
63
76
  /**
64
77
  * 宿主留在轨道基线上,倚音整体抬到它的左上或右上角
65
78
  *
66
- * 倚音成员之间自己紧排,不参与横向弹簧;整块的宽度计入复合盒,
79
+ * 倚音内容作为子域先排成一个刚性块,块内的附件同时就位;含约束留白的宽度计入复合盒,
67
80
  * 因此左右邻居会被固有宽度推开,不会在空间紧张时被压穿。
68
81
  */
69
82
  prepareLayout(context: LayoutPrepareContext): void;