jpfun 0.8.0 → 0.9.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 (97) hide show
  1. package/dist/converter/midi/convert.js +8 -55
  2. package/dist/converter/musicxml/features.d.ts +1 -1
  3. package/dist/converter/musicxml/features.js +1 -1
  4. package/dist/converter/musicxml/index.js +8 -18
  5. package/dist/converter/source.d.ts +4 -7
  6. package/dist/converter/source.js +11 -9
  7. package/dist/functions/ASTtypes.d.ts +1 -14
  8. package/dist/functions/ASTtypes.js +5 -36
  9. package/dist/functions/adjust/index.js +45 -33
  10. package/dist/functions/arpeggio/index.js +42 -45
  11. package/dist/functions/arpeggio/shape.d.ts +2 -3
  12. package/dist/functions/arpeggio/shape.js +20 -50
  13. package/dist/functions/bar/index.js +3 -8
  14. package/dist/functions/beam/auto.js +54 -123
  15. package/dist/functions/beam/index.js +4 -9
  16. package/dist/functions/box/index.js +0 -6
  17. package/dist/functions/dash/index.js +3 -4
  18. package/dist/functions/div/index.d.ts +2 -4
  19. package/dist/functions/div/index.js +10 -10
  20. package/dist/functions/dot/index.js +3 -9
  21. package/dist/functions/dyn/index.js +6 -26
  22. package/dist/functions/gliss/index.js +95 -89
  23. package/dist/functions/grace/index.d.ts +2 -5
  24. package/dist/functions/grace/index.js +44 -89
  25. package/dist/functions/head/index.js +9 -6
  26. package/dist/functions/key/index.js +6 -6
  27. package/dist/functions/meter/index.js +3 -3
  28. package/dist/functions/note/index.js +1 -1
  29. package/dist/functions/note/source-transform.js +2 -5
  30. package/dist/functions/ottava/index.js +11 -32
  31. package/dist/functions/page/index.js +13 -16
  32. package/dist/functions/rest/index.js +2 -4
  33. package/dist/functions/slide/index.js +0 -1
  34. package/dist/functions/stack/index.js +8 -57
  35. package/dist/functions/temporal.d.ts +2 -2
  36. package/dist/functions/temporal.js +7 -0
  37. package/dist/functions/text/index.js +24 -30
  38. package/dist/functions/tie/index.js +14 -37
  39. package/dist/functions/tremolo/index.js +0 -1
  40. package/dist/functions/tuplet/index.js +0 -6
  41. package/dist/functions/up/index.js +79 -83
  42. package/dist/functions/voice/connections.d.ts +2 -1
  43. package/dist/functions/voice/connections.js +48 -10
  44. package/dist/functions/voice/index.d.ts +2 -3
  45. package/dist/functions/voice/index.js +2 -970
  46. package/dist/functions/voice/lyrics.d.ts +30 -0
  47. package/dist/functions/voice/lyrics.js +289 -0
  48. package/dist/functions/voice/voice.d.ts +63 -0
  49. package/dist/functions/voice/voice.js +373 -0
  50. package/dist/functions/voice/voices.d.ts +106 -0
  51. package/dist/functions/voice/voices.js +423 -0
  52. package/dist/functions/volta/index.js +5 -23
  53. package/dist/jpfun.core.min.js +65 -56
  54. package/dist/jpfun.from-midi.min.js +5 -5
  55. package/dist/jpfun.from-musicxml.min.js +7 -7
  56. package/dist/jpfun.min.js +68 -59
  57. package/dist/layout/engine.js +30 -18
  58. package/dist/layout/model.d.ts +0 -1
  59. package/dist/layout/model.js +1 -4
  60. package/dist/layout/page.d.ts +2 -1
  61. package/dist/layout/page.js +14 -14
  62. package/dist/layout/range.d.ts +7 -6
  63. package/dist/layout/range.js +23 -14
  64. package/dist/layout/types.d.ts +9 -28
  65. package/dist/lowering/loweringContext.d.ts +8 -11
  66. package/dist/lowering/loweringContext.js +7 -13
  67. package/dist/lowering/track.d.ts +8 -1
  68. package/dist/lowering/types.d.ts +12 -8
  69. package/dist/parser/parse-utils/call-utils.d.ts +1 -2
  70. package/dist/parser/parse-utils/call-utils.js +6 -34
  71. package/dist/parser/parse-utils/note-utils.d.ts +3 -3
  72. package/dist/parser/parse-utils/note-utils.js +17 -28
  73. package/dist/parser/parse-utils/string-utils.d.ts +1 -1
  74. package/dist/parser/parse-utils/string-utils.js +8 -6
  75. package/dist/parser/parserContext.d.ts +9 -9
  76. package/dist/parser/parserContext.js +54 -10
  77. package/dist/pipeline.js +2 -2
  78. package/dist/playback/compile.js +13 -14
  79. package/dist/playback/event.d.ts +6 -6
  80. package/dist/playback/event.js +13 -49
  81. package/dist/render/canvas.d.ts +0 -2
  82. package/dist/render/canvas.js +0 -21
  83. package/dist/render/svg.d.ts +0 -1
  84. package/dist/render/svg.js +33 -31
  85. package/dist/render/text.d.ts +36 -1
  86. package/dist/render/text.js +134 -0
  87. package/dist/render/types.d.ts +0 -2
  88. package/grammar.md +16 -13
  89. package/package.json +1 -1
  90. package/dist/iife/core.d.ts +0 -1
  91. package/dist/iife/core.js +0 -3
  92. package/dist/iife/from-midi.d.ts +0 -1
  93. package/dist/iife/from-midi.js +0 -15
  94. package/dist/iife/from-musicxml.d.ts +0 -1
  95. package/dist/iife/from-musicxml.js +0 -3
  96. package/dist/iife/full.d.ts +0 -1
  97. package/dist/iife/full.js +0 -3
@@ -7,13 +7,22 @@ import { prepareArpeggioShape } from "./shape.js";
7
7
  /** Fold 保持模块私有;琶音只在自己的边界读取它已经公开在实例上的结构。 */
8
8
  function readFold(node) {
9
9
  const candidate = node;
10
- const ast = node.ast;
11
- return (ast.callName === "up" || ast.callName === "down")
12
- && Array.isArray(candidate.members)
10
+ const ast = candidate?.ast;
11
+ return (ast?.callName === "up" || ast?.callName === "down")
12
+ && Array.isArray(candidate?.members)
13
13
  && Array.isArray(ast.contents)
14
14
  ? candidate
15
15
  : null;
16
16
  }
17
+ /** 折叠成员共享同一对齐锚点,取最宽左半宽才能避免升降号等越过宿主左界压住琶音线 */
18
+ function foldAnchor(fold) {
19
+ let anchor = fold.box.anchor;
20
+ for (const member of fold.members) {
21
+ const nested = readFold(member);
22
+ anchor = Math.max(anchor, nested ? foldAnchor(nested) : member.box.anchor);
23
+ }
24
+ return anchor;
25
+ }
17
26
  class ArpeggioFunction extends ASTFunctionNode {
18
27
  static def = {
19
28
  name: ["arp", "arpeggio"],
@@ -43,6 +52,7 @@ class ArpeggioFunction extends ASTFunctionNode {
43
52
  direction;
44
53
  size;
45
54
  get children() { return [this.content]; }
55
+ timeFlowModel() { return null; }
46
56
  constructor(span, args, ctx, parent = null) {
47
57
  super(span, parent);
48
58
  const [content, direction] = this.getArgValue(args, ctx);
@@ -58,15 +68,9 @@ class ArpeggioFunction extends ASTFunctionNode {
58
68
  content.parent = this;
59
69
  }
60
70
  loweringEnter(ctx, track) {
61
- const host = ctx.isolateFromLoweringGroups(() => {
62
- const events = ctx.trackedEvents(this.content, new Fraction(), track).flat();
63
- if (events.length !== 1 || !events[0].box) {
64
- throw new ErrorDiagnostic("E_ARPEGGIO_INVALID_CONTENT", "@arp 的内容必须恰好产生一个同轨和弦", this.content.sourceSpan);
65
- }
66
- return events[0];
67
- });
68
- const fold = readFold(host);
69
- if (!fold || fold.members.length < 2 || fold.members.some(member => member.track !== track)) {
71
+ const events = ctx.isolateFromLoweringGroups(() => ctx.trackedEvents(this.content, new Fraction(), track).flat());
72
+ const fold = events.length === 1 ? readFold(events[0]) : null;
73
+ if (!fold || fold.members.length < 2) {
70
74
  throw new ErrorDiagnostic("E_ARPEGGIO_INVALID_CONTENT", "@arp 只接受至少包含两个成员的同轨 @up/@down 折叠体", this.content.sourceSpan);
71
75
  }
72
76
  return [new ArpeggioTemporal(this, fold)];
@@ -82,7 +86,6 @@ class ArpeggioTemporal extends TemporalNodeBase {
82
86
  /** 槽位取自成员顺序;无声成员也占槽,不能按实际发声音符重新编号 */
83
87
  slots;
84
88
  hostX = 0;
85
- markX = 0;
86
89
  markCommands = [];
87
90
  constructor(ast, host) {
88
91
  super();
@@ -91,8 +94,7 @@ class ArpeggioTemporal extends TemporalNodeBase {
91
94
  const aboveCount = host.ast.contents.length;
92
95
  const bottomToTop = [
93
96
  ...host.members.slice(aboveCount).reverse(),
94
- host.members[0],
95
- ...host.members.slice(1, aboveCount),
97
+ ...host.members.slice(0, aboveCount),
96
98
  ];
97
99
  const members = ast.direction === "down" ? bottomToTop.reverse() : bottomToTop;
98
100
  this.slots = new Map(members.map((member, index) => [member, index]));
@@ -117,24 +119,23 @@ class ArpeggioTemporal extends TemporalNodeBase {
117
119
  this.host.prepareHorizontal?.(line);
118
120
  }
119
121
  prepareLayout(context) {
120
- transferFunctionAddons(this, this.host);
121
- prepareLayoutHost(this.host, context);
122
- this.springConfig = { ...this.host.springConfig, ...this.springConfig };
122
+ const { host } = this;
123
+ transferFunctionAddons(this, host);
124
+ prepareLayoutHost(host, context);
125
+ this.springConfig = { ...host.springConfig };
123
126
  const em = this.ast.size;
124
- const gap = em * 0.16;
125
- const contentBounds = getLayoutBounds(this.host, true);
126
- const bounds = getLayoutBounds(this.host);
127
- const { commands, bounds: markBounds } = prepareArpeggioShape(contentBounds.h, em, this.ast.direction);
127
+ const contentBounds = getLayoutBounds(host, true);
128
+ const bounds = getLayoutBounds(host);
129
+ const { commands, width } = prepareArpeggioShape(contentBounds.h, em, this.ast.direction);
128
130
  this.markCommands = commands;
129
- this.markX = -markBounds.x;
130
- this.hostX = markBounds.w + gap;
131
- this.box.w = this.hostX + this.host.box.w;
131
+ this.hostX = width + em * 0.16 + foldAnchor(host) - host.box.anchor;
132
+ this.box.w = this.hostX + host.box.w;
132
133
  this.box.h = bounds.h;
133
- this.box.anchor = this.hostX + this.host.box.anchor;
134
- this.box.visualAxis = this.host.box.visualAxis - bounds.y;
135
- for (const name in this.host.ports) {
136
- const port = this.host.ports[name];
137
- this.ports[name] = { x: this.hostX + port.x, y: port.y - bounds.y };
134
+ this.box.anchor = this.hostX + host.box.anchor;
135
+ this.box.visualAxis = host.box.visualAxis - bounds.y;
136
+ for (const name in host.ports) {
137
+ const port = host.ports[name];
138
+ this.ports[name] = { ...port, x: this.hostX + port.x, y: port.y - bounds.y };
138
139
  }
139
140
  }
140
141
  onPlaced() {
@@ -149,6 +150,8 @@ class ArpeggioTemporal extends TemporalNodeBase {
149
150
  emitPlayback(emitter) {
150
151
  emitter.play(this.host);
151
152
  const total = emitter.end.clone().sub(emitter.start);
153
+ if (total.compare(0) <= 0)
154
+ return;
152
155
  const step = new Fraction(1, 8);
153
156
  // 短音段限制总延迟,让最后一个成员仍保留至少一半的宿主窗口。
154
157
  const maxStep = total.clone().div(2).div(this.slots.size - 1);
@@ -161,31 +164,25 @@ class ArpeggioTemporal extends TemporalNodeBase {
161
164
  // 宿主刚在上面发布,其全部子区间连续排在 spans 末尾;命中后处理完同组即停。
162
165
  for (let index = context.spans.length - 1; index >= 0; index--) {
163
166
  const note = context.spans[index];
164
- if (!rootOrigin) {
165
- if (!note.origins.some(origin => origin.node === owner))
166
- continue;
167
- rootOrigin = note.origins[0];
168
- }
169
- else if (note.origins[0] !== rootOrigin) {
167
+ const origin = note.origins.find(item => item.node === owner);
168
+ if (!origin || (rootOrigin && origin !== rootOrigin))
170
169
  break;
171
- }
172
- const member = note.origins.find(origin => slots.has(origin.node))?.node;
170
+ rootOrigin = origin;
171
+ const member = note.origins.find(item => slots.has(item.node))?.node;
173
172
  const slot = member ? slots.get(member) : undefined;
174
173
  if (!slot)
175
174
  continue;
176
175
  // 起止一起缩放再平移,保留宿主终点及成员内部节奏,避免只钳制尾部造成负时值。
177
- const delay = step.clone().mul(slot);
178
- const scale = total.clone().sub(delay).div(total);
179
- note.start.sub(emitter.start).mul(scale).add(emitter.start).add(delay);
180
- note.end.sub(emitter.start).mul(scale).add(emitter.start).add(delay);
176
+ const offset = step.clone().mul(slot).add(emitter.start);
177
+ const scale = emitter.end.clone().sub(offset).div(total);
178
+ note.start.sub(emitter.start).mul(scale).add(offset);
179
+ note.end.sub(emitter.start).mul(scale).add(offset);
181
180
  }
182
181
  });
183
182
  }
184
183
  paint(painter) {
185
- const originX = this.box.x;
186
- const originY = getLayoutBounds(this.host, true).y;
187
184
  painter.drawPath(this.markCommands, { fill: "#000" }, {
188
- x: originX + this.markX, y: originY, scaleX: 1, scaleY: 1,
185
+ x: this.box.x, y: getLayoutBounds(this.host, true).y, scaleX: 1, scaleY: 1,
189
186
  });
190
187
  this.host.paint(painter);
191
188
  for (const decoration of this.host.decorations)
@@ -1,7 +1,6 @@
1
- import type { Rect } from "../../layout/types.js";
2
1
  import type { PathCommand } from "../../render/types.js";
3
- /** 按 MuseScore 的原始重叠比例铺满指定高度,并一次性返回最终边界 */
2
+ /** 按 MuseScore 的原始重叠比例铺满指定高度,直接生成左上角对齐原点的局部路径 */
4
3
  export declare function prepareArpeggioShape(height: number, em: number, direction: "" | "up" | "down"): {
5
4
  commands: PathCommand[];
6
- bounds: Rect;
5
+ width: number;
7
6
  };
@@ -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
  }
@@ -190,19 +190,12 @@ class BarTemporalNode extends TemporalNodeBase {
190
190
  this.ast = ast;
191
191
  this.mergeKey = ANCHOR_KEY;
192
192
  this.initLayoutBox();
193
- // 由于有最小时长,因此设置alpha依然有效
194
- this.springConfig = {
195
- alpha_L: 128,
196
- alpha_R: 128,
197
- };
198
193
  }
199
194
  /**
200
195
  * 给 bar 两侧的相邻元素增加 mu,避免被压缩到一起
201
196
  */
202
197
  prepareHorizontal(line) {
203
- let host = this;
204
- while (host.foldedInto)
205
- host = host.foldedInto;
198
+ const host = this.rootHost;
206
199
  if (!isVisualTemporalNode(host))
207
200
  return;
208
201
  const run = line.trackRuns.get(host.track);
@@ -222,6 +215,8 @@ class BarTemporalNode extends TemporalNodeBase {
222
215
  }
223
216
  }
224
217
  prepareLayout() {
218
+ // 由于有最小时长,因此设置alpha依然有效
219
+ this.springConfig = { alpha_L: 128, alpha_R: 128 };
225
220
  const { type, size } = this.ast;
226
221
  this.lines.length = 0;
227
222
  const repeatDots = [];
@@ -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);
@@ -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
  * 进入目标内容前只开始收集所有成员矩形的引用
@@ -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,7 @@ 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;
66
64
  }
67
65
  /**
68
66
  * 在结构阶段延长前方同轨、时间相接的一组区间
@@ -100,6 +98,7 @@ class DashTemporalNode extends TemporalNodeBase {
100
98
  });
101
99
  }
102
100
  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) });
101
+ const y = this.box.y + this.box.visualAxis;
102
+ painter.drawLine(this.box.x, y, this.box.x + this.lineWidth, y, { stroke: "#000", strokeWidth: Math.max(1, this.ast.size * 0.1) });
104
103
  }
105
104
  }
@@ -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;
@@ -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
  }