jpfun 0.4.0 → 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 (83) 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 +4 -4
  14. package/dist/functions/ASTtypes.js +5 -9
  15. package/dist/functions/adjust/index.js +16 -53
  16. package/dist/functions/arpeggio/index.js +27 -15
  17. package/dist/functions/bar/index.js +7 -8
  18. package/dist/functions/beam/index.js +6 -1
  19. package/dist/functions/beam/layout.d.ts +1 -1
  20. package/dist/functions/beam/layout.js +5 -3
  21. package/dist/functions/box/index.js +88 -82
  22. package/dist/functions/br/index.js +9 -5
  23. package/dist/functions/dash/index.js +24 -13
  24. package/dist/functions/div/index.d.ts +5 -2
  25. package/dist/functions/div/index.js +15 -14
  26. package/dist/functions/dot/index.js +13 -11
  27. package/dist/functions/dyn/index.js +35 -39
  28. package/dist/functions/grace/index.d.ts +19 -6
  29. package/dist/functions/grace/index.js +136 -121
  30. package/dist/functions/head/index.js +19 -14
  31. package/dist/functions/key/index.js +8 -4
  32. package/dist/functions/meter/index.js +22 -2
  33. package/dist/functions/note/index.d.ts +6 -1
  34. package/dist/functions/note/index.js +48 -30
  35. package/dist/functions/note/source-transform.d.ts +21 -0
  36. package/dist/functions/note/source-transform.js +164 -0
  37. package/dist/functions/page/index.d.ts +8 -1
  38. package/dist/functions/page/index.js +23 -13
  39. package/dist/functions/program/index.js +11 -2
  40. package/dist/functions/set/index.js +14 -8
  41. package/dist/functions/stack/index.js +7 -5
  42. package/dist/functions/symbol/index.d.ts +2 -0
  43. package/dist/functions/symbol/index.js +11 -7
  44. package/dist/functions/symbol/ornament.d.ts +3 -3
  45. package/dist/functions/symbol/ornament.js +21 -35
  46. package/dist/functions/symbol/symbols/accent.js +5 -5
  47. package/dist/functions/symbol/symbols/fermata.js +5 -4
  48. package/dist/functions/tempo/index.js +7 -1
  49. package/dist/functions/text/index.js +11 -3
  50. package/dist/functions/tie/index.js +28 -32
  51. package/dist/functions/tuplet/index.js +26 -24
  52. package/dist/functions/up/index.js +24 -21
  53. package/dist/functions/voice/index.js +36 -26
  54. package/dist/functions/volta/index.js +16 -17
  55. package/dist/jpfun.core.min.js +98 -129
  56. package/dist/jpfun.from-midi.min.js +5 -5
  57. package/dist/jpfun.from-musicxml.min.js +8 -8
  58. package/dist/jpfun.min.js +107 -138
  59. package/dist/layout/engine.d.ts +11 -1
  60. package/dist/layout/engine.js +299 -96
  61. package/dist/layout/page.js +2 -0
  62. package/dist/layout/range.d.ts +26 -0
  63. package/dist/layout/range.js +75 -0
  64. package/dist/layout/types.d.ts +33 -4
  65. package/dist/lowering/types.d.ts +5 -0
  66. package/dist/parser/parse-utils/length-utils.js +2 -0
  67. package/dist/parser/parse-utils/note-utils.d.ts +2 -0
  68. package/dist/parser/parse-utils/note-utils.js +6 -2
  69. package/dist/parser/parserContext.js +1 -1
  70. package/dist/playback/compile.d.ts +2 -9
  71. package/dist/playback/compile.js +297 -186
  72. package/dist/playback/event.d.ts +8 -16
  73. package/dist/playback/event.js +13 -3
  74. package/dist/playback/types.d.ts +76 -18
  75. package/dist/playback/types.js +1 -0
  76. package/dist/render/index.d.ts +1 -1
  77. package/dist/render/index.js +1 -1
  78. package/dist/render/recording.d.ts +1 -0
  79. package/dist/render/recording.js +21 -0
  80. package/dist/render/translate.d.ts +14 -0
  81. package/dist/render/translate.js +48 -0
  82. package/grammar.md +103 -78
  83. package/package.json +4 -4
@@ -1,22 +1,25 @@
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"],
10
8
  description: "给目标结构或对象加外框",
11
- example: `@box(content, padding=0.2em, stroke=0.08em, width=-1px)`,
9
+ details: `\
10
+ ~~~jpfun
11
+ @box({1 2 3}, padding=0.2em, stroke=0.08em)
12
+ ~~~`,
12
13
  allowExtraArgs: false,
13
14
  args: [
14
15
  {
15
16
  type: "content",
17
+ description: "需要加框的内容或结构",
16
18
  default: null,
17
19
  },
18
20
  {
19
21
  name: "padding",
22
+ description: "内容到框线的内边距,负值向内收缩",
20
23
  type: "length",
21
24
  default: {
22
25
  value: 0,
@@ -25,6 +28,7 @@ class BoxFunction extends ASTFunctionNode {
25
28
  },
26
29
  {
27
30
  name: "stroke",
31
+ description: "框线宽度",
28
32
  type: "length",
29
33
  default: {
30
34
  value: 1,
@@ -33,6 +37,7 @@ class BoxFunction extends ASTFunctionNode {
33
37
  },
34
38
  {
35
39
  name: "width",
40
+ description: "约束内部排版的宽度,非正值表示随内容自动确定",
36
41
  type: "length",
37
42
  default: {
38
43
  value: -1,
@@ -57,25 +62,29 @@ class BoxFunction extends ASTFunctionNode {
57
62
  * 进入目标内容前只开始收集所有成员矩形的引用
58
63
  */
59
64
  loweringEnter(ctx) {
60
- const temporalMembers = [];
61
- const childAttachments = [];
62
- ctx.beginLoweringGroup(this, {
63
- attachment: new BoxLayoutAttachment(temporalMembers, childAttachments, this),
64
- onTemporal(node) {
65
- if (!isVisualTemporalNode(node))
66
- return;
67
- temporalMembers.push(node);
68
- },
69
- onAttachment(attachment) {
70
- if (isLayoutAttachment(attachment))
71
- childAttachments.push(attachment);
72
- },
73
- });
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);
74
73
  return [];
75
74
  }
76
- /** 目标内容完成 lowering 后结束当前 box 作用域 */
75
+ /** 退出时把注册入口接到首成员;嵌套框按退出顺序由内向外组合 */
77
76
  loweringExit(ctx) {
78
- 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
+ }
79
88
  return [];
80
89
  }
81
90
  constructor(sourceSpan, args, ctx, parent = null) {
@@ -83,84 +92,83 @@ class BoxFunction extends ASTFunctionNode {
83
92
  const [tgt, pad, stroke, width] = this.getArgValue(args, ctx);
84
93
  tgt.parent = this;
85
94
  this.target = tgt;
86
- this.padding = Math.max(0, ctx.length2px(pad));
95
+ this.padding = ctx.length2px(pad);
87
96
  this.stroke = Math.max(0, ctx.length2px(stroke));
88
97
  this.width = ctx.length2px(width);
89
98
  }
99
+ toString(source) {
100
+ return `@box(${this.target.toString(source)}, padding=${this.padding}px, stroke=${this.stroke}px, width=${this.width}px)`;
101
+ }
90
102
  }
91
103
  export const BoxNode = BoxFunction;
92
104
  class BoxLayoutAttachment {
93
- layer = "background";
94
- temporalMembers;
95
- /** 写在 box 内的关系对象;lowering 递归中持续加入,因而总在 box 自身之前完成几何 */
96
- childAttachments;
105
+ content;
97
106
  owner;
107
+ layer = "background";
98
108
  fixedStart = null;
99
109
  wallOffset = 0;
100
110
  get sourceSpan() { return this.owner.sourceSpan; }
101
- constructor(temporalMembers, childAttachments, owner) {
102
- this.temporalMembers = temporalMembers;
103
- this.childAttachments = childAttachments;
111
+ constructor(content, owner) {
112
+ this.content = content;
104
113
  this.owner = owner;
105
114
  }
106
- /** 定宽 box 在成员覆盖的全局列上运行局部墙布局 */
107
- prepareHorizontal(context) {
108
- const layoutLine = this.temporalMembers[0]?.layoutLine;
109
- if (layoutLine === void 0)
115
+ /** 只在完整包含成员的视图注册;留白和定宽共用同一个由内向外执行的 hook */
116
+ registerHorizontal(line) {
117
+ const members = this.content.nodes;
118
+ let first = members[0];
119
+ if (!first)
110
120
  return;
111
- if (this.temporalMembers.some(member => member.layoutLine !== layoutLine)) {
121
+ if (members.some(member => member.layoutLine !== first.layoutLine)) {
112
122
  throw new ErrorDiagnostic("E_BOX_CROSS_LINE", "@box 的内容不能跨越谱面行", this.owner.sourceSpan);
113
123
  }
114
- if (this.owner.width <= 0)
115
- return;
116
- const line = context[layoutLine];
117
- if (!line)
118
- return;
119
- let first = null;
120
- let last = null;
121
- let firstIndex = Infinity;
122
- let lastIndex = -Infinity;
123
- let leftInset = 0;
124
- let rightInset = 0;
125
- // 首末列可能同时站着多个声部,取其中最宽的固有半宽
126
- for (const member of this.temporalMembers) {
124
+ let last = first;
125
+ for (const member of members) {
127
126
  const index = line.columnOf(member);
128
127
  if (index < 0)
129
- continue;
130
- const left = member.box.anchor;
131
- const right = member.box.w - left;
132
- if (index < firstIndex) {
133
- firstIndex = index;
128
+ return;
129
+ if (index < line.columnOf(first))
134
130
  first = member;
135
- leftInset = left;
136
- }
137
- else if (index === firstIndex)
138
- leftInset = Math.max(leftInset, left);
139
- if (index > lastIndex) {
140
- lastIndex = index;
131
+ if (index > line.columnOf(last))
141
132
  last = member;
142
- rightInset = right;
143
- }
144
- else if (index === lastIndex)
145
- rightInset = Math.max(rightInset, right);
146
- }
147
- if (!first || !last)
148
- return;
149
- if (this.owner.width < leftInset + rightInset - 1e-6) {
150
- throw new ErrorDiagnostic("E_BOX_WIDTH_TOO_SMALL", "@box 的宽度小于首末元素的固有宽度", this.owner.sourceSpan);
151
133
  }
134
+ const { width, padding, stroke } = this.owner;
152
135
  line.registerHorizontalLayoutHook(first, last, ({ columns, rows, start, end, X, fixed, options, }) => {
153
- if (fixed[start - 1] || fixed[end]) {
154
- 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];
155
166
  }
156
- const regionX = X.subarray(start, end + 1);
157
- layoutHorizontalRegion(columns.slice(start, end + 1), rows, regionX, fixed.subarray(start, end), this.owner.width, options, 0);
158
- const actualWidth = regionX[regionX.length - 1] - regionX[0] + leftInset + rightInset;
159
- if (Math.abs(actualWidth - this.owner.width) > 1e-6) {
160
- throw new ErrorDiagnostic("E_BOX_CONSTRAINT_CONFLICT", "多个 @box 为相同内容指定了不同宽度", this.owner.sourceSpan);
161
- }
162
- this.fixedStart = first;
163
- 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;
164
172
  });
165
173
  }
166
174
  /**
@@ -169,13 +177,8 @@ class BoxLayoutAttachment {
169
177
  */
170
178
  createGeometry(context) {
171
179
  const { padding, stroke } = this.owner;
172
- const rect = { x: 0, y: 0, w: 0, h: 0 };
173
- const members = [
174
- ...this.temporalMembers.map(member => member.box),
175
- ...this.childAttachments.map(attachment => context.getAttachmentBox(attachment)),
176
- ];
177
- // 未命名歌词等 attachment 会保留全零盒,不能让它把边框拉到文档原点
178
- if (!unionLayoutBoxes(rect, members.filter(member => member.w > 0 || member.h > 0))) {
180
+ const rect = context.getContentBounds(this.content);
181
+ if (!rect) {
179
182
  return { regions: [], paint() { } };
180
183
  }
181
184
  const inset = padding + stroke / 2;
@@ -189,6 +192,9 @@ class BoxLayoutAttachment {
189
192
  w: (this.fixedStart ? this.owner.width : rect.w) + inset * 2,
190
193
  h: rect.h + inset * 2,
191
194
  };
195
+ if (region.w < stroke || region.h < stroke) {
196
+ throw new ErrorDiagnostic("E_BOX_PADDING_TOO_SMALL", "@box 的负内边距使边框宽度或高度小于零", this.owner.sourceSpan);
197
+ }
192
198
  return {
193
199
  regions: [region],
194
200
  paint(painter) {
@@ -7,15 +7,19 @@ class BrFunction extends ASTFunctionNode {
7
7
  static def = {
8
8
  name: ["br"],
9
9
  description: "换行",
10
- example: `@br(offset=1): 在后续事件前偏移指定行数
11
- @br() === @br(1)
12
- @br(2): 空出一行后继续
13
- 语法糖: [仅限顶层]至少连续两个换行
14
- `,
10
+ details: `\
11
+ ~~~jpfun
12
+ 1 2 @br() 3 4
13
+ ~~~
14
+
15
+ \`@br()\` 换到下一行,\`@br(2)\` 空出一行再继续
16
+
17
+ **简写**:仅在顶层,连续至少两个换行表示换行`,
15
18
  allowExtraArgs: false,
16
19
  args: [
17
20
  {
18
21
  name: "offset",
22
+ description: "后续内容偏移的谱面行数,2 表示空出一行再继续",
19
23
  type: "number",
20
24
  default: 1,
21
25
  },
@@ -5,9 +5,11 @@ class DashFunction extends ASTFunctionNode {
5
5
  static def = {
6
6
  name: ["dash", "-"],
7
7
  description: "增时线",
8
- example: `@dash() 创建一根增时线
9
- 语法糖: 一个 '-' 代表一个 @dash()
10
- `,
8
+ details: `\
9
+ ~~~jpfun
10
+ 1 @dash()
11
+ ~~~
12
+ 无参数。添加一根增时线,扩展前方音符或休止符的完整时值;也可简写为 \`1 -\`。每根线的基础时值为一个四分音符,可用减时线或附点调整。装饰音覆盖延长后的完整音符,例如 \`1 ^ $tr -\` 在两拍内持续颤音。`,
11
13
  allowExtraArgs: false,
12
14
  args: []
13
15
  };
@@ -33,6 +35,7 @@ class DashFunction extends ASTFunctionNode {
33
35
  loweringEnter() {
34
36
  return [new DashTemporalNode(this)];
35
37
  }
38
+ labelable() { return this; }
36
39
  toString() { return "-"; }
37
40
  }
38
41
  export const DashNode = DashFunction;
@@ -46,6 +49,7 @@ class DashTemporalNode extends TemporalNodeBase {
46
49
  this.mergeKey = DEFAULT_KEY;
47
50
  this.initLayoutBox();
48
51
  }
52
+ /** 固化记谱位置的速度,控制流直接跳到增时线时也能恢复正确状态 */
49
53
  onTimeState(state) {
50
54
  this.playbackState = { bpm: state.bpm };
51
55
  }
@@ -60,31 +64,38 @@ class DashTemporalNode extends TemporalNodeBase {
60
64
  this.lineY = size * 0.5;
61
65
  this.box.visualAxis = this.lineY;
62
66
  }
67
+ /**
68
+ * 在结构阶段延长前方同轨、时间相接的一组区间
69
+ * 有声与无声目标都保留原对象,后续声音变换才能使用延长后的完整时值
70
+ */
63
71
  emitPlayback(emitter) {
64
72
  const start = emitter.start.clone();
65
- const end = emitter.end.clone();
66
73
  const track = emitter.track;
67
74
  const sourceSpan = this.ast.sourceSpan;
75
+ // 这里只查询已发布的结构前缀;装饰音尚未展开,不需要逐个修改子音。
68
76
  emitter.defer(context => {
69
77
  let rootOrigin;
70
- for (let i = context.events.length - 1; i >= 0; i--) {
71
- const event = context.events[i];
72
- 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)
73
83
  continue;
74
84
  if (rootOrigin === undefined) {
75
- if (!event.at.equals(start))
85
+ if (!span.end.equals(start))
76
86
  continue;
77
- rootOrigin = event.origins[0];
87
+ rootOrigin = span.origins[0];
78
88
  }
79
- else if (event.origins[0] !== rootOrigin) {
89
+ else if (span.origins[0] !== rootOrigin) {
80
90
  break;
81
91
  }
82
- if (event.at.equals(start)) {
83
- event.at.copyFrom(end);
92
+ // 一组目标可以含多个成员;extend 同时承接当前访问在新增部分上的速度效果。
93
+ if (span.end.equals(start)) {
94
+ emitter.extend(span);
84
95
  }
85
96
  }
86
97
  if (rootOrigin === undefined) {
87
- context.diagnostics.push(new WarningDiagnostic("W_PLAYBACK_SUSTAIN_WITHOUT_TARGET", "增时线前没有可延续的发声音符", sourceSpan));
98
+ context.diagnostics.push(new WarningDiagnostic("W_PLAYBACK_SUSTAIN_WITHOUT_TARGET", "增时线前没有可延续的音段", sourceSpan));
88
99
  }
89
100
  });
90
101
  }
@@ -22,18 +22,21 @@ declare class DivFunction extends ASTFunctionNode {
22
22
  static def: {
23
23
  name: string[];
24
24
  description: string;
25
- example: string;
25
+ details: string;
26
26
  allowExtraArgs: boolean;
27
27
  args: ({
28
+ name?: undefined;
28
29
  type: "content";
30
+ description: string;
29
31
  default: null;
30
- name?: undefined;
31
32
  } | {
32
33
  name: string;
34
+ description: string;
33
35
  type: "number";
34
36
  default: number;
35
37
  } | {
36
38
  name: string;
39
+ description: string;
37
40
  type: "boolean";
38
41
  default: boolean;
39
42
  })[];
@@ -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) {
@@ -48,26 +48,27 @@ class DivFunction extends ASTFunctionNode {
48
48
  static def = {
49
49
  name: [DIV_FUNC_NAME, "/"],
50
50
  description: "减时线",
51
- example: `@div(C1, 2): C1下方创建2根减时线
52
- @/({C1 C2 @dash()}, 3): C1、C2和增时线下方创建3根减时线并连接
53
- 语法糖:在音符后加斜杠'/',可以多个
54
- @div(C1, 2) === C1//
55
- @div(C1 C2/ @dash(), 2) === {C1 C2/ @dash()}//
56
- 设置命名参数前缀: div
57
- `,
51
+ details: `\
52
+ ~~~jpfun
53
+ @div({1 2 -}, n=2, autobeam=true)
54
+ ~~~
55
+ **简写**:在目标后加 \`/\`,可重复或作用于内容块。\`@div(1, 2)\` 等价于 \`1//\`,\`@div({1 2/ -}, 2)\` 等价于 \`{1 2/ -}//\`。默认参数使用 \`div.n\`、\`div.autobeam\` 设置。`,
58
56
  allowExtraArgs: false,
59
57
  args: [
60
58
  {
61
59
  type: "content",
60
+ description: "要减时的内容,可包含多个音符或增时线",
62
61
  default: null,
63
62
  },
64
63
  {
65
64
  name: "n",
65
+ description: "减时层数,每级将时值减半,取非负整数",
66
66
  type: "number",
67
67
  default: 1,
68
68
  },
69
69
  {
70
70
  name: "autobeam",
71
+ description: "是否参与不同 div 之间的自动连线,同一 div 内部的连线不受影响",
71
72
  type: "boolean",
72
73
  default: true,
73
74
  },
@@ -99,10 +100,10 @@ class DivFunction extends ASTFunctionNode {
99
100
  const n = nodes[at++];
100
101
  if (n.data?.class !== DivFunction)
101
102
  return null;
102
- // 向前找到第一个有效节点
103
- const prev = ASTFunctionNode.findLastFuncContentNode(ctx.nodes, ctx.nodes.length - 1);
104
- if (!prev)
103
+ const prevIndex = ASTFunctionNode.findLastFuncContentIndex(ctx.nodes, ctx.nodes.length - 1);
104
+ if (prevIndex < 0)
105
105
  return at; // 没有了 直接当作无效文本跳过
106
+ const prev = ctx.nodes[prevIndex];
106
107
  if (prev instanceof DivFunction) {
107
108
  // 已经是div了 继续加深
108
109
  prev.n += n.data.n;
@@ -116,8 +117,7 @@ class DivFunction extends ASTFunctionNode {
116
117
  start: Math.min(spanPrev.start, n.span.start),
117
118
  end: Math.max(spanPrev.end, n.span.end),
118
119
  }, argMap, ctx, null);
119
- ctx.nodes.pop(); // 消耗掉prev
120
- ctx.pushNode(node);
120
+ ctx.nodes[prevIndex] = node;
121
121
  return at;
122
122
  }
123
123
  /**
@@ -152,6 +152,7 @@ class DivFunction extends ASTFunctionNode {
152
152
  host.ports[divLinePortName(i, "left")] = {
153
153
  x: lineLeft,
154
154
  y: lineY,
155
+ state: { claimed: false },
155
156
  };
156
157
  host.ports[divLinePortName(i, "right")] = {
157
158
  x: lineRight,
@@ -164,7 +165,7 @@ class DivFunction extends ASTFunctionNode {
164
165
  // 未被关系对象接管的级别仍由本装饰独立绘制
165
166
  for (let i = 0; i < count; i++) {
166
167
  const leftPort = host.ports[divLinePortName(i, "left")];
167
- if (leftPort.claimed)
168
+ if (leftPort.state.claimed)
168
169
  continue;
169
170
  const y = host.box.y + firstLine + i * lineGap;
170
171
  painter.drawLine(host.box.x + lineLeft, y, host.box.x + lineRight, y, { stroke: "#000", strokeWidth });
@@ -10,20 +10,23 @@ class DotFunction extends ASTFunctionNode {
10
10
  static def = {
11
11
  name: [DOT_FUNC_NAME, "."],
12
12
  description: "附点",
13
- example: `@dot(C1, 2): C1右侧创建2个点 仅接收一个可接收元素
14
- 语法糖:在目标后加'.',可以多个
15
- @dot(C1, 2) === C1..
16
- {1^2}. === {1.^2}
17
- @dot(C1 C2/ @dash(), 2) 报错 因为点只能接收一个可见时间元素
18
- `,
13
+ details: `\
14
+ ~~~jpfun
15
+ @dot(1, n=2)
16
+ ~~~
17
+ **简写**:目标后加 \`.\`,可重复。\`@dot(1, 2)\` 等价于 \`1..\`;\`{1^2}.\` 等价于 \`{1.^2}\`。
18
+
19
+ \`@dot({1 2/ -}, 2)\` 包含多个时间元素,会报错;需要分别为各元素添加附点。`,
19
20
  allowExtraArgs: false,
20
21
  args: [
21
22
  {
22
23
  type: "content",
24
+ description: "单个可见时间元素,如音符、增时线或折叠和弦",
23
25
  default: null,
24
26
  },
25
27
  {
26
28
  name: "n",
29
+ description: "附点数,第一点增加原时值的一半,后续各点增加量再减半",
27
30
  type: "number",
28
31
  default: 1,
29
32
  },
@@ -55,10 +58,10 @@ class DotFunction extends ASTFunctionNode {
55
58
  const n = nodes[at++];
56
59
  if (n.data?.class !== DotFunction)
57
60
  return null;
58
- // 向前找到第一个有效节点
59
- const prev = ASTFunctionNode.findLastFuncContentNode(ctx.nodes, ctx.nodes.length - 1);
60
- if (!prev)
61
+ const prevIndex = ASTFunctionNode.findLastFuncContentIndex(ctx.nodes, ctx.nodes.length - 1);
62
+ if (prevIndex < 0)
61
63
  return at; // 没有了 直接当作无效文本跳过
64
+ const prev = ctx.nodes[prevIndex];
62
65
  if (prev instanceof DotFunction) {
63
66
  // 已经是dot了 继续加深
64
67
  prev.n += n.data.n;
@@ -73,8 +76,7 @@ class DotFunction extends ASTFunctionNode {
73
76
  start: Math.min(spanPrev.start, n.span.start),
74
77
  end: Math.max(spanPrev.end, n.span.end),
75
78
  }, argMap, ctx, null);
76
- ctx.nodes.pop(); // 消耗掉prev
77
- ctx.pushNode(node);
79
+ ctx.nodes[prevIndex] = node;
78
80
  return at;
79
81
  }
80
82
  /**
@@ -5,17 +5,19 @@ class DynFunction extends ASTFunctionNode {
5
5
  static def = {
6
6
  name: ["dyn"],
7
7
  description: "渐强渐弱",
8
- example: `@dyn(from, to, dv) 在两个标签之间线性改变力度
9
- from、to 必须位于同一音轨且时间不同
10
- dv 为正时渐强,为负时渐弱;变化量叠加在每个音符原有力度上
11
-
8
+ details: `\
9
+ ~~~jpfun
12
10
  1 ^ $p 2@a 3 4@b
13
- @dyn(a, b, 24)`,
11
+ @dyn(a, b, 24)
12
+ ~~~
13
+ 端点须位于同一音轨、不同时间;变化量沿区间线性增加,叠加在每个音符原有力度上。本例从 \`a\` 到 \`b\` 渐强,终点增加 24 的力度。
14
+
15
+ 端点也可标在增时线上。终点没有力度时,取同轨、不晚于终点的最后一个原始力度作为保持基准;找不到时给出警告并仅绘制。增时线延续的音符保持起音时的力度。`,
14
16
  allowExtraArgs: false,
15
17
  args: [
16
- { name: "from", type: "label", default: null },
17
- { name: "to", type: "label", default: null },
18
- { name: "dv", type: "number", default: null },
18
+ { name: "from", description: "渐变起点的标签", type: "label", default: null },
19
+ { name: "to", description: "渐变终点的标签", type: "label", default: null },
20
+ { name: "dv", description: "力度变化量,正数渐强,负数渐弱", type: "number", default: null },
19
21
  ],
20
22
  };
21
23
  static loweringFinalize = (result) => {
@@ -38,10 +40,18 @@ dv 为正时渐强,为负时渐弱;变化量叠加在每个音符原有力
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 {