jpfun 0.8.0 → 1.0.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 (140) hide show
  1. package/README.md +10 -15
  2. package/dist/diagnostic.d.ts +29 -2
  3. package/dist/diagnostic.js +5 -4
  4. package/dist/functions/ASTtypes.d.ts +1 -14
  5. package/dist/functions/ASTtypes.js +5 -36
  6. package/dist/functions/adjust/index.js +45 -33
  7. package/dist/functions/arpeggio/index.js +44 -48
  8. package/dist/functions/arpeggio/shape.d.ts +2 -3
  9. package/dist/functions/arpeggio/shape.js +20 -50
  10. package/dist/functions/bar/index.d.ts +3 -0
  11. package/dist/functions/bar/index.js +21 -11
  12. package/dist/functions/beam/auto.js +54 -123
  13. package/dist/functions/beam/index.js +4 -9
  14. package/dist/functions/beam/layout.js +3 -2
  15. package/dist/functions/box/index.js +1 -6
  16. package/dist/functions/dash/index.js +6 -4
  17. package/dist/functions/div/index.d.ts +3 -5
  18. package/dist/functions/div/index.js +14 -12
  19. package/dist/functions/dot/index.js +3 -9
  20. package/dist/functions/dyn/index.js +7 -26
  21. package/dist/functions/gliss/index.js +99 -91
  22. package/dist/functions/grace/index.d.ts +2 -5
  23. package/dist/functions/grace/index.js +47 -94
  24. package/dist/functions/head/index.js +32 -36
  25. package/dist/functions/key/index.js +9 -12
  26. package/dist/functions/meter/index.d.ts +1 -0
  27. package/dist/functions/meter/index.js +6 -3
  28. package/dist/functions/meter/shape.d.ts +1 -1
  29. package/dist/functions/note/accidentals.d.ts +3 -3
  30. package/dist/functions/note/accidentals.js +29 -16
  31. package/dist/functions/note/index.js +12 -14
  32. package/dist/functions/note/source-transform.d.ts +15 -0
  33. package/dist/functions/note/source-transform.js +75 -71
  34. package/dist/functions/ottava/index.js +12 -32
  35. package/dist/functions/page/index.js +15 -17
  36. package/dist/functions/rest/index.js +10 -10
  37. package/dist/functions/slide/index.js +1 -1
  38. package/dist/functions/stack/index.js +8 -57
  39. package/dist/functions/temporal.d.ts +11 -5
  40. package/dist/functions/temporal.js +22 -2
  41. package/dist/functions/text/index.js +24 -30
  42. package/dist/functions/tie/index.js +42 -41
  43. package/dist/functions/tremolo/index.js +1 -1
  44. package/dist/functions/tuplet/index.js +1 -6
  45. package/dist/functions/up/index.js +82 -88
  46. package/dist/functions/voice/connections.d.ts +3 -2
  47. package/dist/functions/voice/connections.js +48 -10
  48. package/dist/functions/voice/index.d.ts +2 -3
  49. package/dist/functions/voice/index.js +2 -970
  50. package/dist/functions/voice/lyrics.d.ts +34 -0
  51. package/dist/functions/voice/lyrics.js +393 -0
  52. package/dist/functions/voice/voice.d.ts +67 -0
  53. package/dist/functions/voice/voice.js +405 -0
  54. package/dist/functions/voice/voices.d.ts +106 -0
  55. package/dist/functions/voice/voices.js +424 -0
  56. package/dist/functions/volta/index.js +6 -23
  57. package/dist/index.d.ts +97 -3
  58. package/dist/index.js +97 -3
  59. package/dist/jpfun.min.js +63 -67
  60. package/dist/layout/engine.d.ts +4 -3
  61. package/dist/layout/engine.js +190 -24
  62. package/dist/layout/model.d.ts +12 -4
  63. package/dist/layout/model.js +16 -11
  64. package/dist/layout/page.d.ts +2 -1
  65. package/dist/layout/page.js +15 -15
  66. package/dist/layout/path.js +1 -1
  67. package/dist/layout/range.d.ts +7 -6
  68. package/dist/layout/range.js +23 -14
  69. package/dist/layout/types.d.ts +19 -30
  70. package/dist/lowering/loweringContext.d.ts +8 -11
  71. package/dist/lowering/loweringContext.js +7 -13
  72. package/dist/lowering/track.d.ts +8 -1
  73. package/dist/lowering/types.d.ts +12 -8
  74. package/dist/parser/grammarType.d.ts +3 -3
  75. package/dist/parser/parse-utils/brace-utils.js +7 -17
  76. package/dist/parser/parse-utils/call-utils.d.ts +4 -3
  77. package/dist/parser/parse-utils/call-utils.js +23 -66
  78. package/dist/parser/parse-utils/note-utils.d.ts +7 -3
  79. package/dist/parser/parse-utils/note-utils.js +25 -40
  80. package/dist/parser/parse-utils/string-utils.d.ts +1 -1
  81. package/dist/parser/parse-utils/string-utils.js +8 -6
  82. package/dist/parser/parserContext.d.ts +17 -11
  83. package/dist/parser/parserContext.js +111 -65
  84. package/dist/parser/preprocess.d.ts +7 -0
  85. package/dist/parser/preprocess.js +21 -19
  86. package/dist/pipeline.d.ts +1 -1
  87. package/dist/pipeline.js +2 -2
  88. package/dist/playback/compile.js +14 -15
  89. package/dist/playback/event.d.ts +6 -6
  90. package/dist/playback/event.js +13 -49
  91. package/dist/render/canvas.d.ts +0 -2
  92. package/dist/render/canvas.js +50 -56
  93. package/dist/render/index.d.ts +1 -1
  94. package/dist/render/index.js +1 -1
  95. package/dist/render/paint.d.ts +7 -0
  96. package/dist/render/paint.js +70 -20
  97. package/dist/render/recording.d.ts +8 -1
  98. package/dist/render/recording.js +12 -0
  99. package/dist/render/svg.d.ts +5 -2
  100. package/dist/render/svg.js +70 -48
  101. package/dist/render/text.d.ts +37 -2
  102. package/dist/render/text.js +136 -1
  103. package/dist/render/translate.d.ts +3 -1
  104. package/dist/render/translate.js +6 -0
  105. package/dist/render/types.d.ts +9 -2
  106. package/grammar.md +18 -14
  107. package/package.json +8 -18
  108. package/dist/converter/midi/convert.d.ts +0 -42
  109. package/dist/converter/midi/convert.js +0 -729
  110. package/dist/converter/midi/index.d.ts +0 -5
  111. package/dist/converter/midi/index.js +0 -11
  112. package/dist/converter/midi/json.d.ts +0 -38
  113. package/dist/converter/midi/json.js +0 -1
  114. package/dist/converter/midi/triplet.d.ts +0 -30
  115. package/dist/converter/midi/triplet.js +0 -75
  116. package/dist/converter/musicxml/dom.d.ts +0 -25
  117. package/dist/converter/musicxml/dom.js +0 -65
  118. package/dist/converter/musicxml/features.d.ts +0 -114
  119. package/dist/converter/musicxml/features.js +0 -501
  120. package/dist/converter/musicxml/index.d.ts +0 -14
  121. package/dist/converter/musicxml/index.js +0 -1295
  122. package/dist/converter/musicxml/model.d.ts +0 -176
  123. package/dist/converter/musicxml/model.js +0 -1
  124. package/dist/converter/musicxml/resolve.d.ts +0 -10
  125. package/dist/converter/musicxml/resolve.js +0 -152
  126. package/dist/converter/source.d.ts +0 -22
  127. package/dist/converter/source.js +0 -51
  128. package/dist/core.d.ts +0 -24
  129. package/dist/core.js +0 -24
  130. package/dist/iife/core.d.ts +0 -1
  131. package/dist/iife/core.js +0 -3
  132. package/dist/iife/from-midi.d.ts +0 -1
  133. package/dist/iife/from-midi.js +0 -15
  134. package/dist/iife/from-musicxml.d.ts +0 -1
  135. package/dist/iife/from-musicxml.js +0 -3
  136. package/dist/iife/full.d.ts +0 -1
  137. package/dist/iife/full.js +0 -3
  138. package/dist/jpfun.core.min.js +0 -123
  139. package/dist/jpfun.from-midi.min.js +0 -9
  140. package/dist/jpfun.from-musicxml.min.js +0 -12
@@ -1,9 +1,9 @@
1
- import { pathBounds } from "../../layout/path.js";
2
1
  const TILE_WIDTH = 54.2813;
3
2
  const TILE_HEIGHT = 87.37505;
4
3
  const TILE_STEP = 72.8125;
4
+ const ARROW_WIDTH = 113.5313;
5
5
  const ARROW_HEIGHT = 93.6719;
6
- const ARROW_X = -27.4688;
6
+ const ARROW_OFFSET_X = 27.4688;
7
7
  const ARROW_Y = -7.9375;
8
8
  const ARROW_BOTTOM = ARROW_Y + ARROW_HEIGHT;
9
9
  /** MuseScore 4.7.4 导出的琶音波形块,已旋转到自上而下的局部坐标 */
@@ -60,69 +60,39 @@ const DOWN_ARROW = [
60
60
  { op: "C", cx1: 58.9063, cy1: 93.6719, cx2: 61.8907, cy2: 92.3438, x: 63.2188, y: 90.3594 },
61
61
  { op: "L", x: 111.8751, y: 21.5156 },
62
62
  ];
63
- function appendShape(output, shape, offsetX, offsetY, scale, reverse) {
64
- const x = (value) => offsetX + (reverse ? TILE_WIDTH - value : value) * scale;
65
- const y = (value) => offsetY + (reverse ? TILE_HEIGHT - value : value) * scale;
63
+ function appendShape(output, shape, x, y) {
66
64
  for (const command of shape) {
67
- if (command.op === "Z")
68
- output.push(command);
69
- else if (command.op === "C")
65
+ if (command.op === "C")
70
66
  output.push({
71
67
  op: "C",
72
68
  cx1: x(command.cx1), cy1: y(command.cy1),
73
69
  cx2: x(command.cx2), cy2: y(command.cy2),
74
70
  x: x(command.x), y: y(command.y),
75
71
  });
76
- else if (command.op === "Q")
77
- output.push({
78
- op: "Q",
79
- cx: x(command.cx), cy: y(command.cy),
80
- x: x(command.x), y: y(command.y),
81
- });
82
72
  else
83
73
  output.push({ op: command.op, x: x(command.x), y: y(command.y) });
84
74
  }
85
75
  }
86
- /** 按 MuseScore 的原始重叠比例铺满指定高度,并一次性返回最终边界 */
76
+ /** 按 MuseScore 的原始重叠比例铺满指定高度,直接生成左上角对齐原点的局部路径 */
87
77
  export function prepareArpeggioShape(height, em, direction) {
88
- const arrow = direction === "up" || direction === "down";
89
- const segments = Math.max(1, Math.round(height / (em * 0.3)));
90
- const regularCount = arrow ? Math.max(0, segments - 1) : segments;
91
- const naturalHeight = !arrow
92
- ? TILE_HEIGHT + TILE_STEP * (regularCount - 1)
93
- : ARROW_BOTTOM + TILE_STEP * regularCount;
78
+ const arrow = direction !== "";
79
+ const count = Math.max(1, Math.round(height / (em * 0.3)) - (arrow ? 1 : 0));
80
+ const naturalWidth = arrow ? ARROW_WIDTH : TILE_WIDTH;
81
+ const naturalHeight = arrow
82
+ ? ARROW_BOTTOM + TILE_STEP * count
83
+ : TILE_HEIGHT + TILE_STEP * (count - 1);
94
84
  const scale = height / naturalHeight;
85
+ const reverse = direction !== "down";
86
+ const place = (span, offset) => reverse
87
+ ? (value) => (span - offset - value) * scale
88
+ : (value) => (offset + value) * scale;
95
89
  const commands = [];
96
- for (let index = 0; index < regularCount; index++) {
97
- appendShape(commands, TILE, 0, index * TILE_STEP * scale, scale, direction === "");
90
+ const tileX = place(naturalWidth, arrow ? ARROW_OFFSET_X : 0);
91
+ for (let index = 0; index < count; index++) {
92
+ appendShape(commands, TILE, tileX, place(naturalHeight, index * TILE_STEP));
98
93
  }
99
94
  if (arrow) {
100
- appendShape(commands, DOWN_ARROW, ARROW_X * scale, (regularCount * TILE_STEP + ARROW_Y) * scale, scale, false);
101
- }
102
- const bounds = pathBounds(commands);
103
- if (direction !== "up")
104
- return { commands, bounds };
105
- const right = bounds.x + bounds.w;
106
- const mirrorX = (value) => right - (value - bounds.x);
107
- // 命令尚未发布,直接镜像,避免再分配整条路径及每个命令对象
108
- for (const command of commands) {
109
- if (command.op === "Z")
110
- continue;
111
- if (command.op === "C") {
112
- command.cx1 = mirrorX(command.cx1);
113
- command.cy1 = height - command.cy1;
114
- command.cx2 = mirrorX(command.cx2);
115
- command.cy2 = height - command.cy2;
116
- }
117
- else if (command.op === "Q") {
118
- command.cx = mirrorX(command.cx);
119
- command.cy = height - command.cy;
120
- }
121
- command.x = mirrorX(command.x);
122
- command.y = height - command.y;
95
+ appendShape(commands, DOWN_ARROW, place(naturalWidth, 0), place(naturalHeight, count * TILE_STEP + ARROW_Y));
123
96
  }
124
- return {
125
- commands,
126
- bounds: { x: bounds.x, y: height - bounds.y - bounds.h, w: bounds.w, h: bounds.h },
127
- };
97
+ return { commands, width: naturalWidth * scale };
128
98
  }
@@ -12,6 +12,9 @@ export declare const BarNode: ASTFunctionClass;
12
12
  /**
13
13
  * 某一列此刻处在所属反复段的第几遍
14
14
  *
15
+ * 每条 `:|` 只回跳一次,所以遍数是 1 加上本段已到过的 `:|` 数;
16
+ * 别处再跳回本段时它已停在最后一遍,只进最后一个房子。
17
+ * 没有 `:|` 的段只能靠别处跳回,进段几次就是第几遍。
15
18
  * 遍数是回跳的直接后果,所以和跳转规则一样归小节线所有;房子只消费这个数。
16
19
  * 没有 `|:` 时整首曲子就是一段。
17
20
  */
@@ -129,6 +129,7 @@ class BarNumberAttachment {
129
129
  items;
130
130
  pattern;
131
131
  font;
132
+ kind = "bar-number";
132
133
  layer = "foreground";
133
134
  sourceSpan = undefined;
134
135
  constructor(items, pattern, font) {
@@ -190,19 +191,12 @@ class BarTemporalNode extends TemporalNodeBase {
190
191
  this.ast = ast;
191
192
  this.mergeKey = ANCHOR_KEY;
192
193
  this.initLayoutBox();
193
- // 由于有最小时长,因此设置alpha依然有效
194
- this.springConfig = {
195
- alpha_L: 128,
196
- alpha_R: 128,
197
- };
198
194
  }
199
195
  /**
200
196
  * 给 bar 两侧的相邻元素增加 mu,避免被压缩到一起
201
197
  */
202
198
  prepareHorizontal(line) {
203
- let host = this;
204
- while (host.foldedInto)
205
- host = host.foldedInto;
199
+ const host = this.rootHost;
206
200
  if (!isVisualTemporalNode(host))
207
201
  return;
208
202
  const run = line.trackRuns.get(host.track);
@@ -222,6 +216,8 @@ class BarTemporalNode extends TemporalNodeBase {
222
216
  }
223
217
  }
224
218
  prepareLayout() {
219
+ // 由于有最小时长,因此设置alpha依然有效
220
+ this.springConfig = { alpha_L: 128, alpha_R: 128 };
225
221
  const { type, size } = this.ast;
226
222
  this.lines.length = 0;
227
223
  const repeatDots = [];
@@ -285,22 +281,36 @@ class BarTemporalNode extends TemporalNodeBase {
285
281
  }
286
282
  }
287
283
  }
288
- /** 反复段起点标记:`:|` 靠它找回跳目标,遍数也按它计数 */
284
+ /** 反复段起止标记:`:|` 靠起点找回跳目标,遍数按段内到过的终点计数 */
289
285
  const REPEAT_START = "repeat.start";
286
+ const REPEAT_END = "repeat.end";
290
287
  /**
291
288
  * 某一列此刻处在所属反复段的第几遍
292
289
  *
290
+ * 每条 `:|` 只回跳一次,所以遍数是 1 加上本段已到过的 `:|` 数;
291
+ * 别处再跳回本段时它已停在最后一遍,只进最后一个房子。
292
+ * 没有 `:|` 的段只能靠别处跳回,进段几次就是第几遍。
293
293
  * 遍数是回跳的直接后果,所以和跳转规则一样归小节线所有;房子只消费这个数。
294
294
  * 没有 `|:` 时整首曲子就是一段。
295
295
  */
296
296
  export function repeatPass(cursor, column) {
297
- return cursor.visits(cursor.seek(REPEAT_START, column + 1, -1) ?? 0);
297
+ const start = cursor.seek(REPEAT_START, column + 1, -1) ?? 0;
298
+ // `:|:` 既是下一段起点又回跳本段,所以段尾要含下一段起点
299
+ const next = cursor.seek(REPEAT_START, start, 1) ?? Infinity;
300
+ let pass = 1, ends = 0;
301
+ for (let end = cursor.seek(REPEAT_END, start, 1); end !== undefined && end <= next; end = cursor.seek(REPEAT_END, end, 1)) {
302
+ ends++;
303
+ if (cursor.visits(end) > 0)
304
+ pass++;
305
+ }
306
+ return ends ? pass : cursor.visits(start);
298
307
  }
299
308
  /** 只有反复线参与播放顺序;普通小节线太多,进控制流扫描是纯开销 */
300
309
  class RepeatBarTemporalNode extends BarTemporalNode {
301
310
  /** 只在 type 3–5 时创建,所以三种分别是:3 段首、4 段尾、5 两者都是 */
302
311
  playbackMarks() {
303
- return this.ast.type === 4 ? [] : [REPEAT_START];
312
+ const { type } = this.ast;
313
+ return type === 3 ? [REPEAT_START] : type === 4 ? [REPEAT_END] : [REPEAT_START, REPEAT_END];
304
314
  }
305
315
  /** 每条反复线只回跳一次,所以连写几条就是几遍 */
306
316
  playbackFlow(columnOf) {
@@ -1,122 +1,63 @@
1
1
  import { ANCHOR_KEY, isVisualTemporalNode, } from "../temporal.js";
2
2
  import { Fraction } from "../../fraction.js";
3
- import { DIV_ADDON_KEY, DivNode, } from "../div/index.js";
3
+ import { getDivOwner, } from "../div/index.js";
4
4
  import { meterChangeAt } from "../meter/index.js";
5
5
  import { collectExplicitBeamEndpoints, createBeamLayoutAttachment, } from "./layout.js";
6
- /** 只返回实际参与当前事件 div addon 的 div,先沿祖先链找,不够再按书写顺序向下补 */
7
- function findActiveDivs(node) {
8
- let remainingLevels = Math.max(0, Math.floor(Number(node.addon?.[DIV_ADDON_KEY]) || 0));
9
- if (remainingLevels <= 0)
10
- return [];
11
- const result = [];
12
- const take = (div) => {
13
- result.push(div);
14
- remainingLevels -= Math.max(0, div.n);
15
- };
16
- let ast = node.ast.parent;
17
- while (ast && remainingLevels > 0) {
18
- if (ast instanceof DivNode)
19
- take(ast);
20
- ast = ast.parent;
21
- }
22
- if (remainingLevels <= 0)
23
- return result;
24
- // 复合节点(如 up)把代表成员的修饰提升成了自己的,提供层级的 div 在子树里
25
- const descend = (parent) => {
26
- for (const child of parent.children ?? []) {
27
- if (remainingLevels <= 0)
28
- return;
29
- if (child instanceof DivNode)
30
- take(child);
31
- descend(child);
32
- }
33
- };
34
- descend(node.ast);
35
- return result;
36
- }
37
- /** 收集每个 div 作用域内的可见事件,顺序与最终时间列一致 */
6
+ /** 按最外层 div 作用域收集可见事件,顺序与最终时间列一致 */
38
7
  function collectDivScopes(result) {
39
- const scopesByOwner = new Map();
40
- const nodeScopes = new Map();
8
+ const scopes = new Map();
41
9
  for (const column of result.columns) {
42
10
  for (const node of column) {
43
11
  if (!isVisualTemporalNode(node))
44
12
  continue;
45
- const owners = findActiveDivs(node);
46
- if (owners.length === 0)
13
+ const owner = getDivOwner(node);
14
+ if (!owner)
47
15
  continue;
48
- const memberships = [];
49
- for (const owner of owners) {
50
- let scope = scopesByOwner.get(owner);
51
- if (!scope) {
52
- scope = { owner, nodes: [] };
53
- scopesByOwner.set(owner, scope);
54
- }
55
- scope.nodes.push(node);
56
- memberships.push(scope);
57
- }
58
- nodeScopes.set(node, memberships);
16
+ const nodes = scopes.get(owner);
17
+ if (nodes)
18
+ nodes.push(node);
19
+ else
20
+ scopes.set(owner, [node]);
59
21
  }
60
22
  }
61
- return {
62
- scopes: [...scopesByOwner.values()],
63
- nodeScopes,
64
- };
23
+ return scopes;
65
24
  }
66
- /** 嵌套 div 由最外层作用域一次连接,内部级别通过端口自然形成次级 beam */
67
- function outermostScopes(scopes) {
68
- const owners = new Set(scopes.map(scope => scope.owner));
69
- return scopes.filter(scope => {
70
- let parent = scope.owner.parent;
71
- while (parent) {
72
- if (parent instanceof DivNode && owners.has(parent))
73
- return false;
74
- parent = parent.parent;
75
- }
76
- return true;
77
- });
78
- }
79
- function createAutoBeam(nodes, nodeScopes) {
25
+ function createAutoBeam(nodes) {
80
26
  let start = Infinity;
81
27
  let end = -Infinity;
82
28
  for (const node of nodes) {
83
- start = Math.min(start, node.ast.sourceSpan.start);
84
- end = Math.max(end, node.ast.sourceSpan.end);
85
- for (const { owner } of nodeScopes.get(node) ?? []) {
86
- start = Math.min(start, owner.sourceSpan.start);
87
- end = Math.max(end, owner.sourceSpan.end);
88
- }
29
+ const owner = getDivOwner(node);
30
+ start = Math.min(start, node.ast.sourceSpan.start, owner.sourceSpan.start);
31
+ end = Math.max(end, node.ast.sourceSpan.end, owner.sourceSpan.end);
89
32
  }
90
33
  return createBeamLayoutAttachment(nodes, false, { start, end });
91
34
  }
92
35
  /** 同一个 div 内部始终按轨道连接,手工 beam 端点会截断自动片段 */
93
- function createScopeBeams(scopes, nodeScopes, explicitEndpoints) {
36
+ function createScopeBeams(scopes, explicitEndpoints) {
94
37
  const attachments = [];
95
- for (const scope of outermostScopes(scopes)) {
96
- const tracks = new Map();
97
- for (const node of scope.nodes) {
98
- const trackNodes = tracks.get(node.track);
99
- if (trackNodes)
100
- trackNodes.push(node);
101
- else
102
- tracks.set(node.track, [node]);
103
- }
104
- for (const nodes of tracks.values()) {
105
- let segment = [];
106
- const flush = () => {
107
- if (segment.length >= 2)
108
- attachments.push(createAutoBeam(segment, nodeScopes));
109
- segment = [];
110
- };
111
- for (const node of nodes) {
112
- if (explicitEndpoints.has(node)) {
113
- flush();
114
- continue;
115
- }
116
- segment.push(node);
38
+ const segments = new Map();
39
+ const flush = (segment) => {
40
+ if (segment && segment.length >= 2)
41
+ attachments.push(createAutoBeam(segment));
42
+ };
43
+ for (const scopeNodes of scopes) {
44
+ if (scopeNodes.length < 2)
45
+ continue;
46
+ for (const node of scopeNodes) {
47
+ if (explicitEndpoints.has(node)) {
48
+ flush(segments.get(node.track));
49
+ segments.delete(node.track);
50
+ continue;
117
51
  }
118
- flush();
52
+ const segment = segments.get(node.track);
53
+ if (segment)
54
+ segment.push(node);
55
+ else
56
+ segments.set(node.track, [node]);
119
57
  }
58
+ for (const segment of segments.values())
59
+ flush(segment);
60
+ segments.clear();
120
61
  }
121
62
  return attachments;
122
63
  }
@@ -127,34 +68,28 @@ function createScopeBeams(scopes, nodeScopes, explicitEndpoints) {
127
68
  * 随当前生效的 @meter 分拍(缺省或散板按四分音符,复拍子及 3/8 按三个分母单位,其余短分母按一个分母单位)。
128
69
  * 分组不跨轨道、逻辑行、拍点、小节线、拍号变更、时间间隙、普通长事件、开关边界或显式 beam。
129
70
  */
130
- function createAdjacentBeams(result, nodeScopes, explicitEndpoints) {
71
+ function createAdjacentBeams(result, scopes, explicitEndpoints) {
131
72
  const attachments = [];
132
73
  const groups = new Map();
133
- const lineStarts = new Map();
134
- const measureStart = new Fraction();
74
+ const phaseStart = new Fraction();
135
75
  const beatTime = new Fraction();
136
76
  const beatDuration = new Fraction(1);
137
77
  let candidateDuration = 1;
138
- for (const column of result.columns) {
139
- for (const node of column) {
140
- const current = lineStarts.get(node.layoutLine);
141
- if (current === undefined || node.t.compare(current) < 0)
142
- lineStarts.set(node.layoutLine, node.t);
143
- }
144
- }
78
+ let currentLine = 0;
145
79
  const flush = (track) => {
146
80
  const group = groups.get(track);
147
81
  if (!group)
148
82
  return;
149
83
  groups.delete(track);
150
84
  if (group.nodes.length >= 2)
151
- attachments.push(createAutoBeam(group.nodes, nodeScopes));
85
+ attachments.push(createAutoBeam(group.nodes));
152
86
  };
153
87
  const flushAll = () => {
154
88
  for (const track of groups.keys())
155
89
  flush(track);
156
90
  };
157
91
  for (const column of result.columns) {
92
+ const line = column[0].layoutLine;
158
93
  let meter;
159
94
  let hasAnchor = false;
160
95
  for (const node of column) {
@@ -162,9 +97,10 @@ function createAdjacentBeams(result, nodeScopes, explicitEndpoints) {
162
97
  if (node.mergeKey === ANCHOR_KEY)
163
98
  hasAnchor = true;
164
99
  }
165
- if (meter || hasAnchor) {
100
+ if (line !== currentLine || meter || hasAnchor) {
101
+ currentLine = line;
166
102
  flushAll();
167
- measureStart.copyFrom(column[0].t);
103
+ phaseStart.copyFrom(column[0].t);
168
104
  }
169
105
  if (meter) {
170
106
  const { numerator, denominator } = meter;
@@ -179,12 +115,11 @@ function createAdjacentBeams(result, nodeScopes, explicitEndpoints) {
179
115
  for (const node of column) {
180
116
  if (node.mergeKey === ANCHOR_KEY)
181
117
  continue;
182
- const memberships = isVisualTemporalNode(node) ? nodeScopes.get(node) : undefined;
183
- const isIndependentDiv = memberships?.every(scope => scope.nodes.length === 1) ?? false;
184
- const autoBeamEnabled = memberships?.every(scope => scope.owner.autoBeamEnabled) ?? false;
118
+ const owner = isVisualTemporalNode(node) ? getDivOwner(node) : undefined;
185
119
  const isCandidate = isVisualTemporalNode(node)
186
- && isIndependentDiv
187
- && autoBeamEnabled
120
+ && owner !== undefined
121
+ && owner.autoBeamEnabled
122
+ && scopes.get(owner)?.length === 1
188
123
  && node.T.compare(candidateDuration) < 0
189
124
  && !explicitEndpoints.has(node);
190
125
  if (!isCandidate) {
@@ -194,17 +129,13 @@ function createAdjacentBeams(result, nodeScopes, explicitEndpoints) {
194
129
  flush(node.track);
195
130
  continue;
196
131
  }
197
- const lineStart = lineStarts.get(node.layoutLine) ?? node.t;
198
- const activeStart = measureStart.compare(lineStart) >= 0 ? measureStart : lineStart;
199
- beatTime.copyFrom(node.t).sub(activeStart);
132
+ beatTime.copyFrom(node.t).sub(phaseStart);
200
133
  const beat = Math.floor(beatTime.numerator * beatDuration.denominator / (beatTime.denominator * beatDuration.numerator));
201
134
  const current = groups.get(node.track);
202
- const isContinuous = current && node.t.equals(current.endTime);
203
- if (!current || node.layoutLine !== current.line || beat !== current.beat || !isContinuous) {
135
+ if (!current || beat !== current.beat || !node.t.equals(current.endTime)) {
204
136
  flush(node.track);
205
137
  groups.set(node.track, {
206
138
  nodes: [node],
207
- line: node.layoutLine,
208
139
  beat,
209
140
  endTime: node.t.clone().add(node.T),
210
141
  });
@@ -219,9 +150,9 @@ function createAdjacentBeams(result, nodeScopes, explicitEndpoints) {
219
150
  }
220
151
  /** beam 域在 lowering 固化后统一生成作用域连接和相邻自动连接 */
221
152
  export function createAutomaticBeamAttachments(result) {
222
- const { scopes, nodeScopes } = collectDivScopes(result);
223
- if (scopes.length === 0)
153
+ const scopes = collectDivScopes(result);
154
+ if (scopes.size === 0)
224
155
  return;
225
156
  const explicitEndpoints = collectExplicitBeamEndpoints(result.attachments);
226
- result.attachments.push(...createScopeBeams(scopes, nodeScopes, explicitEndpoints), ...createAdjacentBeams(result, nodeScopes, explicitEndpoints));
157
+ result.attachments.push(...createScopeBeams(scopes.values(), explicitEndpoints), ...createAdjacentBeams(result, scopes, explicitEndpoints));
227
158
  }
@@ -19,12 +19,9 @@ class BeamFunction extends ASTFunctionNode {
19
19
  endPoints = [];
20
20
  constructor(sourceSpan, args, ctx, parent = null) {
21
21
  super(sourceSpan, parent);
22
- for (const [, value] of args) {
23
- const parsed = value instanceof ASTFunctionNode
24
- ? value
25
- : ctx.parseArgWithType(value.valueSpan, "label", sourceSpan.start);
26
- if (parsed instanceof ASTFunctionNode)
27
- this.endPoints.push(parsed);
22
+ for (const [key, value] of args) {
23
+ if (typeof key === "number")
24
+ this.endPoints.push(value);
28
25
  }
29
26
  // 未显式给全时,回退到最近可标记对象,至少取两个。
30
27
  let k = ctx.labelableNodes.length - 1;
@@ -43,9 +40,7 @@ class BeamFunction extends ASTFunctionNode {
43
40
  const endPoints = [];
44
41
  for (const ast of this.endPoints) {
45
42
  // 暂时只用最后一个;理应有且仅有一个
46
- let temporal = ctx.getTemporalNodes(ast).at(-1);
47
- while (temporal?.foldedInto)
48
- temporal = temporal.foldedInto;
43
+ const temporal = ctx.getTemporalNodes(ast).at(-1)?.rootHost;
49
44
  if (!temporal)
50
45
  continue;
51
46
  endPoints.push(temporal);
@@ -59,6 +59,7 @@ export function collectExplicitBeamEndpoints(attachments) {
59
59
  return result;
60
60
  }
61
61
  class BeamLayoutAttachment {
62
+ kind = "beam";
62
63
  layer = "foreground";
63
64
  endPoints;
64
65
  explicit;
@@ -105,8 +106,8 @@ class BeamLayoutAttachment {
105
106
  return {
106
107
  regions,
107
108
  paint(painter) {
108
- for (const line of lines) {
109
- painter.drawLine(line.start.x, line.start.y, line.end.x, line.end.y, { stroke: "#000", strokeWidth });
109
+ for (const region of regions) {
110
+ painter.drawRect(region.x, region.y, region.w, region.h, { fill: "#000" });
110
111
  }
111
112
  },
112
113
  };
@@ -52,12 +52,6 @@ class BoxFunction extends ASTFunctionNode {
52
52
  stroke;
53
53
  width;
54
54
  get children() { return [this.target]; }
55
- timeFlowModel() {
56
- return {
57
- children: [this.target],
58
- mode: "sequence",
59
- };
60
- }
61
55
  /**
62
56
  * box 不创建时间事件
63
57
  * 进入目标内容前只开始收集所有成员矩形的引用
@@ -113,6 +107,7 @@ export const BoxNode = BoxFunction;
113
107
  class BoxLayoutAttachment {
114
108
  content;
115
109
  owner;
110
+ kind = "box";
116
111
  layer = "background";
117
112
  reservedHost;
118
113
  fixedStart = null;
@@ -41,7 +41,6 @@ class DashFunction extends ASTFunctionNode {
41
41
  export const DashNode = DashFunction;
42
42
  class DashTemporalNode extends TemporalNodeBase {
43
43
  lineWidth = 0;
44
- lineY = 0;
45
44
  constructor(ast) {
46
45
  super();
47
46
  this.ast = ast;
@@ -61,8 +60,10 @@ class DashTemporalNode extends TemporalNodeBase {
61
60
  this.box.w = this.lineWidth;
62
61
  this.box.h = size;
63
62
  this.box.anchor = this.lineWidth / 2;
64
- this.lineY = size * 0.5;
65
- this.box.visualAxis = this.lineY;
63
+ this.box.visualAxis = size * 0.5;
64
+ }
65
+ finalizeLayout() {
66
+ this.ports["lyric"] = { x: this.box.anchor, y: this.box.h };
66
67
  }
67
68
  /**
68
69
  * 在结构阶段延长前方同轨、时间相接的一组区间
@@ -100,6 +101,7 @@ class DashTemporalNode extends TemporalNodeBase {
100
101
  });
101
102
  }
102
103
  paint(painter) {
103
- painter.drawLine(this.box.x, this.box.y + this.lineY, this.box.x + this.lineWidth, this.box.y + this.lineY, { stroke: "#000", strokeWidth: Math.max(1, this.ast.size * 0.1) });
104
+ const h = Math.max(1, this.ast.size * 0.1);
105
+ painter.drawRect(this.box.x, this.box.y + this.box.visualAxis - h / 2, this.lineWidth, h, { fill: "#000" });
104
106
  }
105
107
  }
@@ -2,7 +2,9 @@ import { ASTNodeBase, FunctionArgs, SourceSpan, ParserContext, ASTFunctionNode }
2
2
  import { GrammarNode, GrammarSugarNode } from "../../parser/grammarType.js";
3
3
  import type { LayoutDecorationHandler, LayoutHost } from "../../layout/types.js";
4
4
  import type { LoweringContext } from "../../lowering/loweringContext.js";
5
+ import type { TemporalNodeBase } from "../temporal.js";
5
6
  export declare const DIV_ADDON_KEY: "@div";
7
+ export declare function getDivOwner(node: TemporalNodeBase): DivFunction | undefined;
6
8
  /** div 暴露自己的线段端口,不依赖任何关系函数 */
7
9
  export declare function divLinePortName(level: number, edge: "left" | "right"): string;
8
10
  export declare function claimDivLine(host: LayoutHost, level: number): void;
@@ -25,10 +27,10 @@ declare class DivFunction extends ASTFunctionNode {
25
27
  details: string;
26
28
  allowExtraArgs: boolean;
27
29
  args: ({
28
- name?: undefined;
29
30
  type: "content";
30
31
  description: string;
31
32
  default: null;
33
+ name?: undefined;
32
34
  } | {
33
35
  name: string;
34
36
  description: string;
@@ -59,10 +61,6 @@ declare class DivFunction extends ASTFunctionNode {
59
61
  /** parse 时冻结;只控制不同 div 之间的自动连接,不影响当前 div 内部连接 */
60
62
  autoBeamEnabled: boolean;
61
63
  get children(): ASTNodeBase[];
62
- timeFlowModel(): {
63
- children: ASTNodeBase[];
64
- mode: "sequence";
65
- };
66
64
  constructor(sourceSpan: SourceSpan, args: FunctionArgs, ctx: ParserContext, parent?: ASTNodeBase | null);
67
65
  toString(source: string): string;
68
66
  }
@@ -3,6 +3,10 @@ import { WarningDiagnostic } from "../../diagnostic.js";
3
3
  import { createAutomaticBeamAttachments } from "../beam/auto.js";
4
4
  const DIV_FUNC_NAME = "div";
5
5
  export const DIV_ADDON_KEY = functionAddonKey(DIV_FUNC_NAME);
6
+ const DIV_OWNER_KEY = Symbol("div.owner");
7
+ export function getDivOwner(node) {
8
+ return node.addon?.[DIV_OWNER_KEY];
9
+ }
6
10
  /** div 暴露自己的线段端口,不依赖任何关系函数 */
7
11
  export function divLinePortName(level, edge) {
8
12
  return `div.${level}.${edge}`;
@@ -101,24 +105,24 @@ class DivFunction extends ASTFunctionNode {
101
105
  const n = nodes[at++];
102
106
  if (n.data?.class !== DivFunction)
103
107
  return null;
104
- const prevIndex = ASTFunctionNode.findLastFuncContentIndex(ctx.nodes, ctx.nodes.length - 1);
108
+ const prevIndex = ctx.findLastOperandIndex();
105
109
  if (prevIndex < 0)
106
110
  return at; // 没有了 直接当作无效文本跳过
107
111
  const prev = ctx.nodes[prevIndex];
108
112
  if (prev instanceof DivFunction) {
109
113
  // 已经是div了 继续加深
110
114
  prev.n += n.data.n;
115
+ prev.sourceSpan.end = Math.max(prev.sourceSpan.end, n.span.end);
111
116
  return at;
112
117
  }
113
118
  const argMap = new Map();
114
119
  argMap.set(0, prev);
115
120
  argMap.set("n", n.data.n);
116
121
  const spanPrev = prev.sourceSpan;
117
- const node = new DivFunction({
122
+ ctx.nodes[prevIndex] = new DivFunction({
118
123
  start: Math.min(spanPrev.start, n.span.start),
119
124
  end: Math.max(spanPrev.end, n.span.end),
120
125
  }, argMap, ctx, null);
121
- ctx.nodes[prevIndex] = node;
122
126
  return at;
123
127
  }
124
128
  /**
@@ -163,12 +167,14 @@ class DivFunction extends ASTFunctionNode {
163
167
  },
164
168
  paint(painter) {
165
169
  // 未被关系对象接管的级别仍由本装饰独立绘制
170
+ const x = host.box.x + lineLeft;
171
+ const w = lineRight - lineLeft;
172
+ const top = host.box.y + firstLine - strokeWidth / 2;
166
173
  for (let i = 0; i < count; i++) {
167
174
  const leftPort = host.ports[divLinePortName(i, "left")];
168
175
  if (leftPort.state.claimed)
169
176
  continue;
170
- const y = host.box.y + firstLine + i * lineGap;
171
- painter.drawLine(host.box.x + lineLeft, y, host.box.x + lineRight, y, { stroke: "#000", strokeWidth });
177
+ painter.drawRect(x, top + i * lineGap, w, strokeWidth, { fill: "#000" });
172
178
  }
173
179
  },
174
180
  };
@@ -177,11 +183,13 @@ class DivFunction extends ASTFunctionNode {
177
183
  const count = this.n;
178
184
  if (count === 0)
179
185
  return [];
186
+ const self = this;
180
187
  ctx.deferFinalize(DivFunction.loweringFinalize, -5);
181
188
  ctx.beginLoweringGroup(this, {
182
189
  onTemporal(node) {
183
- const addon = node.addon ??= {};
190
+ const addon = (node.addon ??= {});
184
191
  addon[DIV_ADDON_KEY] = (Number(addon[DIV_ADDON_KEY]) || 0) + count;
192
+ addon[DIV_OWNER_KEY] = self;
185
193
  node.T.divPow2(count);
186
194
  },
187
195
  });
@@ -198,12 +206,6 @@ class DivFunction extends ASTFunctionNode {
198
206
  /** parse 时冻结;只控制不同 div 之间的自动连接,不影响当前 div 内部连接 */
199
207
  autoBeamEnabled;
200
208
  get children() { return [this.content]; }
201
- timeFlowModel() {
202
- return {
203
- children: [this.content],
204
- mode: "sequence",
205
- };
206
- }
207
209
  constructor(sourceSpan, args, ctx, parent = null) {
208
210
  super(sourceSpan, parent);
209
211
  [this.content, this.n, this.autoBeamEnabled] = this.getArgValue(args, ctx);