jpfun 0.7.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 (152) hide show
  1. package/Bravura-LICENSE.txt +93 -0
  2. package/README.md +49 -146
  3. package/dist/Bravura-LICENSE.txt +93 -0
  4. package/dist/converter/midi/convert.d.ts +0 -10
  5. package/dist/converter/midi/convert.js +19 -56
  6. package/dist/converter/musicxml/features.d.ts +16 -8
  7. package/dist/converter/musicxml/features.js +88 -19
  8. package/dist/converter/musicxml/index.js +88 -50
  9. package/dist/converter/musicxml/model.d.ts +9 -0
  10. package/dist/converter/source.d.ts +4 -7
  11. package/dist/converter/source.js +11 -9
  12. package/dist/core.d.ts +1 -0
  13. package/dist/core.js +1 -0
  14. package/dist/diagnostic.d.ts +7 -1
  15. package/dist/diagnostic.js +40 -88
  16. package/dist/fraction.js +3 -2
  17. package/dist/functions/ASTtypes.d.ts +3 -32
  18. package/dist/functions/ASTtypes.js +5 -52
  19. package/dist/functions/adjust/index.js +45 -33
  20. package/dist/functions/arpeggio/index.js +50 -55
  21. package/dist/functions/arpeggio/shape.d.ts +2 -3
  22. package/dist/functions/arpeggio/shape.js +20 -50
  23. package/dist/functions/bar/index.d.ts +8 -0
  24. package/dist/functions/bar/index.js +105 -8
  25. package/dist/functions/beam/auto.d.ts +1 -2
  26. package/dist/functions/beam/auto.js +80 -135
  27. package/dist/functions/beam/index.js +6 -12
  28. package/dist/functions/beam/layout.d.ts +1 -1
  29. package/dist/functions/beam/layout.js +1 -1
  30. package/dist/functions/box/index.js +0 -6
  31. package/dist/functions/br/index.d.ts +34 -2
  32. package/dist/functions/br/index.js +5 -4
  33. package/dist/functions/dash/index.js +9 -10
  34. package/dist/functions/default.js +2 -1
  35. package/dist/functions/div/index.d.ts +3 -4
  36. package/dist/functions/div/index.js +18 -14
  37. package/dist/functions/dot/index.js +5 -11
  38. package/dist/functions/dyn/index.js +55 -57
  39. package/dist/functions/gliss/index.js +131 -103
  40. package/dist/functions/grace/index.d.ts +3 -12
  41. package/dist/functions/grace/index.js +52 -102
  42. package/dist/functions/head/index.js +10 -7
  43. package/dist/functions/key/index.js +13 -15
  44. package/dist/functions/meter/index.d.ts +56 -1
  45. package/dist/functions/meter/index.js +15 -10
  46. package/dist/functions/note/index.js +6 -6
  47. package/dist/functions/note/noteName.d.ts +16 -0
  48. package/dist/functions/note/noteName.js +88 -0
  49. package/dist/functions/note/source-transform.js +8 -10
  50. package/dist/functions/ottava/index.js +35 -40
  51. package/dist/functions/page/index.d.ts +1 -0
  52. package/dist/functions/page/index.js +27 -21
  53. package/dist/functions/pagebreak/index.js +2 -2
  54. package/dist/functions/rest/index.js +5 -5
  55. package/dist/functions/set/index.js +4 -4
  56. package/dist/functions/slide/index.js +0 -1
  57. package/dist/functions/stack/index.js +8 -57
  58. package/dist/functions/symbol/index.d.ts +21 -2
  59. package/dist/functions/symbol/index.js +69 -24
  60. package/dist/functions/symbol/ornament.d.ts +1 -2
  61. package/dist/functions/symbol/shape.d.ts +6 -0
  62. package/dist/functions/symbol/shape.js +16 -3
  63. package/dist/functions/symbol/symbols/coda.d.ts +4 -0
  64. package/dist/functions/symbol/symbols/coda.js +56 -0
  65. package/dist/functions/symbol/symbols/dynamics.d.ts +1 -1
  66. package/dist/functions/symbol/symbols/dynamics.js +18 -8
  67. package/dist/functions/symbol/symbols/harmonic.d.ts +2 -0
  68. package/dist/functions/symbol/symbols/harmonic.js +11 -0
  69. package/dist/functions/symbol/symbols/marcato.d.ts +2 -0
  70. package/dist/functions/symbol/symbols/marcato.js +22 -0
  71. package/dist/functions/symbol/symbols/staccato.d.ts +2 -0
  72. package/dist/functions/symbol/symbols/staccato.js +24 -0
  73. package/dist/functions/symbol/symbols/tenuto.d.ts +2 -0
  74. package/dist/functions/symbol/symbols/tenuto.js +11 -0
  75. package/dist/functions/symbol/symbols/tocoda.d.ts +2 -0
  76. package/dist/functions/symbol/symbols/tocoda.js +16 -0
  77. package/dist/functions/symbol/symbols/turn.d.ts +3 -0
  78. package/dist/functions/symbol/symbols/turn.js +50 -0
  79. package/dist/functions/symbol/symbols/wheel.d.ts +2 -0
  80. package/dist/functions/symbol/symbols/wheel.js +36 -0
  81. package/dist/functions/temporal.d.ts +3 -2
  82. package/dist/functions/temporal.js +8 -0
  83. package/dist/functions/text/index.js +38 -35
  84. package/dist/functions/tie/index.js +44 -43
  85. package/dist/functions/tremolo/index.d.ts +2 -0
  86. package/dist/functions/tremolo/index.js +318 -0
  87. package/dist/functions/tuplet/index.js +4 -14
  88. package/dist/functions/up/index.js +81 -85
  89. package/dist/functions/voice/connections.d.ts +2 -1
  90. package/dist/functions/voice/connections.js +48 -10
  91. package/dist/functions/voice/index.d.ts +2 -3
  92. package/dist/functions/voice/index.js +2 -963
  93. package/dist/functions/voice/lyrics.d.ts +30 -0
  94. package/dist/functions/voice/lyrics.js +289 -0
  95. package/dist/functions/voice/voice.d.ts +63 -0
  96. package/dist/functions/voice/voice.js +373 -0
  97. package/dist/functions/voice/voices.d.ts +106 -0
  98. package/dist/functions/voice/voices.js +423 -0
  99. package/dist/functions/volta/index.js +5 -23
  100. package/dist/jpfun.core.min.js +76 -59
  101. package/dist/jpfun.from-midi.min.js +5 -5
  102. package/dist/jpfun.from-musicxml.min.js +8 -8
  103. package/dist/jpfun.min.js +81 -64
  104. package/dist/layout/default.d.ts +0 -1
  105. package/dist/layout/default.js +0 -1
  106. package/dist/layout/engine.js +30 -18
  107. package/dist/layout/model.d.ts +0 -1
  108. package/dist/layout/model.js +1 -4
  109. package/dist/layout/page.d.ts +2 -1
  110. package/dist/layout/page.js +14 -14
  111. package/dist/layout/range.d.ts +7 -6
  112. package/dist/layout/range.js +23 -14
  113. package/dist/layout/types.d.ts +9 -28
  114. package/dist/lowering/loweringContext.d.ts +18 -23
  115. package/dist/lowering/loweringContext.js +33 -46
  116. package/dist/lowering/track.d.ts +8 -1
  117. package/dist/lowering/types.d.ts +13 -11
  118. package/dist/parser/parse-utils/call-utils.d.ts +1 -2
  119. package/dist/parser/parse-utils/call-utils.js +6 -34
  120. package/dist/parser/parse-utils/note-utils.d.ts +3 -3
  121. package/dist/parser/parse-utils/note-utils.js +21 -33
  122. package/dist/parser/parse-utils/string-utils.d.ts +3 -2
  123. package/dist/parser/parse-utils/string-utils.js +20 -5
  124. package/dist/parser/parserContext.d.ts +9 -9
  125. package/dist/parser/parserContext.js +62 -11
  126. package/dist/pipeline.js +2 -3
  127. package/dist/playback/compile.js +131 -82
  128. package/dist/playback/event.d.ts +6 -6
  129. package/dist/playback/event.js +13 -49
  130. package/dist/playback/time.js +42 -31
  131. package/dist/playback/types.d.ts +3 -1
  132. package/dist/render/canvas.js +4 -22
  133. package/dist/render/recording.js +1 -4
  134. package/dist/render/svg.d.ts +0 -1
  135. package/dist/render/svg.js +33 -31
  136. package/dist/render/text.d.ts +36 -1
  137. package/dist/render/text.js +134 -0
  138. package/dist/render/translate.d.ts +0 -1
  139. package/dist/render/translate.js +6 -23
  140. package/dist/render/types.d.ts +0 -2
  141. package/grammar.md +39 -30
  142. package/package.json +3 -3
  143. package/dist/functions/note/noteNameFSM.d.ts +0 -8
  144. package/dist/functions/note/noteNameFSM.js +0 -181
  145. package/dist/iife/core.d.ts +0 -1
  146. package/dist/iife/core.js +0 -3
  147. package/dist/iife/from-midi.d.ts +0 -1
  148. package/dist/iife/from-midi.js +0 -15
  149. package/dist/iife/from-musicxml.d.ts +0 -1
  150. package/dist/iife/from-musicxml.js +0 -3
  151. package/dist/iife/full.d.ts +0 -1
  152. package/dist/iife/full.js +0 -3
@@ -1,175 +1,126 @@
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
+ import { meterChangeAt } from "../meter/index.js";
4
5
  import { collectExplicitBeamEndpoints, createBeamLayoutAttachment, } from "./layout.js";
5
- /** 只返回实际参与当前事件 div addon 的 div,先沿祖先链找,不够再按书写顺序向下补 */
6
- function findActiveDivs(node) {
7
- let remainingLevels = Math.max(0, Math.floor(Number(node.addon?.[DIV_ADDON_KEY]) || 0));
8
- if (remainingLevels <= 0)
9
- return [];
10
- const result = [];
11
- const take = (div) => {
12
- result.push(div);
13
- remainingLevels -= Math.max(0, div.n);
14
- };
15
- let ast = node.ast.parent;
16
- while (ast && remainingLevels > 0) {
17
- if (ast instanceof DivNode)
18
- take(ast);
19
- ast = ast.parent;
20
- }
21
- if (remainingLevels <= 0)
22
- return result;
23
- // 复合节点(如 up)把代表成员的修饰提升成了自己的,提供层级的 div 在子树里
24
- const descend = (parent) => {
25
- for (const child of parent.children ?? []) {
26
- if (remainingLevels <= 0)
27
- return;
28
- if (child instanceof DivNode)
29
- take(child);
30
- descend(child);
31
- }
32
- };
33
- descend(node.ast);
34
- return result;
35
- }
36
- /** 收集每个 div 作用域内的可见事件,顺序与最终时间列一致 */
6
+ /** 按最外层 div 作用域收集可见事件,顺序与最终时间列一致 */
37
7
  function collectDivScopes(result) {
38
- const scopesByOwner = new Map();
39
- const nodeScopes = new Map();
8
+ const scopes = new Map();
40
9
  for (const column of result.columns) {
41
10
  for (const node of column) {
42
11
  if (!isVisualTemporalNode(node))
43
12
  continue;
44
- const owners = findActiveDivs(node);
45
- if (owners.length === 0)
13
+ const owner = getDivOwner(node);
14
+ if (!owner)
46
15
  continue;
47
- const memberships = [];
48
- for (const owner of owners) {
49
- let scope = scopesByOwner.get(owner);
50
- if (!scope) {
51
- scope = { owner, nodes: [] };
52
- scopesByOwner.set(owner, scope);
53
- }
54
- scope.nodes.push(node);
55
- memberships.push(scope);
56
- }
57
- nodeScopes.set(node, memberships);
16
+ const nodes = scopes.get(owner);
17
+ if (nodes)
18
+ nodes.push(node);
19
+ else
20
+ scopes.set(owner, [node]);
58
21
  }
59
22
  }
60
- return {
61
- scopes: [...scopesByOwner.values()],
62
- nodeScopes,
63
- };
23
+ return scopes;
64
24
  }
65
- /** 嵌套 div 由最外层作用域一次连接,内部级别通过端口自然形成次级 beam */
66
- function outermostScopes(scopes) {
67
- const owners = new Set(scopes.map(scope => scope.owner));
68
- return scopes.filter(scope => {
69
- let parent = scope.owner.parent;
70
- while (parent) {
71
- if (parent instanceof DivNode && owners.has(parent))
72
- return false;
73
- parent = parent.parent;
74
- }
75
- return true;
76
- });
77
- }
78
- function createAutoBeam(nodes, nodeScopes) {
25
+ function createAutoBeam(nodes) {
79
26
  let start = Infinity;
80
27
  let end = -Infinity;
81
28
  for (const node of nodes) {
82
- start = Math.min(start, node.ast.sourceSpan.start);
83
- end = Math.max(end, node.ast.sourceSpan.end);
84
- for (const { owner } of nodeScopes.get(node) ?? []) {
85
- start = Math.min(start, owner.sourceSpan.start);
86
- end = Math.max(end, owner.sourceSpan.end);
87
- }
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);
88
32
  }
89
33
  return createBeamLayoutAttachment(nodes, false, { start, end });
90
34
  }
91
35
  /** 同一个 div 内部始终按轨道连接,手工 beam 端点会截断自动片段 */
92
- function createScopeBeams(scopes, nodeScopes, explicitEndpoints) {
36
+ function createScopeBeams(scopes, explicitEndpoints) {
93
37
  const attachments = [];
94
- for (const scope of outermostScopes(scopes)) {
95
- const tracks = new Map();
96
- for (const node of scope.nodes) {
97
- const trackNodes = tracks.get(node.track);
98
- if (trackNodes)
99
- trackNodes.push(node);
100
- else
101
- tracks.set(node.track, [node]);
102
- }
103
- for (const nodes of tracks.values()) {
104
- let segment = [];
105
- const flush = () => {
106
- if (segment.length >= 2)
107
- attachments.push(createAutoBeam(segment, nodeScopes));
108
- segment = [];
109
- };
110
- for (const node of nodes) {
111
- if (explicitEndpoints.has(node)) {
112
- flush();
113
- continue;
114
- }
115
- 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;
116
51
  }
117
- flush();
52
+ const segment = segments.get(node.track);
53
+ if (segment)
54
+ segment.push(node);
55
+ else
56
+ segments.set(node.track, [node]);
118
57
  }
58
+ for (const segment of segments.values())
59
+ flush(segment);
60
+ segments.clear();
119
61
  }
120
62
  return attachments;
121
63
  }
122
- function beatAt(time) {
123
- return Math.floor(time.numerator / time.denominator);
124
- }
125
64
  /**
126
65
  * 多个独立 div 的默认连接规则
127
66
  *
128
67
  * MuseScore 和 LilyPond 都以拍号的 beat structure 作为默认分组边界,并允许手工 beam 覆盖。
129
- * 当前语言没有拍号,暂以四分音符为拍单位。
130
- * 分组不跨轨道、逻辑行、拍点、小节线、时间间隙、普通长事件、开关边界或显式 beam。
68
+ * 随当前生效的 @meter 分拍(缺省或散板按四分音符,复拍子及 3/8 按三个分母单位,其余短分母按一个分母单位)。
69
+ * 分组不跨轨道、逻辑行、拍点、小节线、拍号变更、时间间隙、普通长事件、开关边界或显式 beam。
131
70
  */
132
- function createAdjacentBeams(result, nodeScopes, explicitEndpoints) {
71
+ function createAdjacentBeams(result, scopes, explicitEndpoints) {
133
72
  const attachments = [];
134
73
  const groups = new Map();
135
- const lineStarts = new Map();
136
- const measureStart = new Fraction();
74
+ const phaseStart = new Fraction();
137
75
  const beatTime = new Fraction();
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
- }
76
+ const beatDuration = new Fraction(1);
77
+ let candidateDuration = 1;
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
- for (const track of [...groups.keys()])
88
+ for (const track of groups.keys())
155
89
  flush(track);
156
90
  };
157
91
  for (const column of result.columns) {
158
- const anchor = column.find(node => node.mergeKey === ANCHOR_KEY);
159
- if (anchor) {
92
+ const line = column[0].layoutLine;
93
+ let meter;
94
+ let hasAnchor = false;
95
+ for (const node of column) {
96
+ meter = meterChangeAt(node) ?? meter;
97
+ if (node.mergeKey === ANCHOR_KEY)
98
+ hasAnchor = true;
99
+ }
100
+ if (line !== currentLine || meter || hasAnchor) {
101
+ currentLine = line;
160
102
  flushAll();
161
- measureStart.copyFrom(anchor.t);
103
+ phaseStart.copyFrom(column[0].t);
104
+ }
105
+ if (meter) {
106
+ const { numerator, denominator } = meter;
107
+ if (numerator > 0 && denominator > 4) {
108
+ beatDuration.set(denominator >= 8 && numerator % 3 === 0 ? 12 : 4, denominator);
109
+ }
110
+ else {
111
+ beatDuration.set(1);
112
+ }
113
+ candidateDuration = beatDuration.compare(1) < 0 ? beatDuration : 1;
162
114
  }
163
115
  for (const node of column) {
164
116
  if (node.mergeKey === ANCHOR_KEY)
165
117
  continue;
166
- const memberships = isVisualTemporalNode(node) ? nodeScopes.get(node) : undefined;
167
- const isIndependentDiv = memberships?.every(scope => scope.nodes.length === 1) ?? false;
168
- const autoBeamEnabled = memberships?.every(scope => scope.owner.autoBeamEnabled) ?? false;
118
+ const owner = isVisualTemporalNode(node) ? getDivOwner(node) : undefined;
169
119
  const isCandidate = isVisualTemporalNode(node)
170
- && isIndependentDiv
171
- && autoBeamEnabled
172
- && node.T.compare(1) < 0
120
+ && owner !== undefined
121
+ && owner.autoBeamEnabled
122
+ && scopes.get(owner)?.length === 1
123
+ && node.T.compare(candidateDuration) < 0
173
124
  && !explicitEndpoints.has(node);
174
125
  if (!isCandidate) {
175
126
  // 同轨可见元素必须打断相邻关系,即使它本身是零时长文本
@@ -178,16 +129,13 @@ function createAdjacentBeams(result, nodeScopes, explicitEndpoints) {
178
129
  flush(node.track);
179
130
  continue;
180
131
  }
181
- const lineStart = lineStarts.get(node.layoutLine) ?? node.t;
182
- const activeStart = measureStart.compare(lineStart) >= 0 ? measureStart : lineStart;
183
- const beat = beatAt(beatTime.copyFrom(node.t).sub(activeStart));
132
+ beatTime.copyFrom(node.t).sub(phaseStart);
133
+ const beat = Math.floor(beatTime.numerator * beatDuration.denominator / (beatTime.denominator * beatDuration.numerator));
184
134
  const current = groups.get(node.track);
185
- const isContinuous = current && node.t.equals(current.endTime);
186
- if (!current || node.layoutLine !== current.line || beat !== current.beat || !isContinuous) {
135
+ if (!current || beat !== current.beat || !node.t.equals(current.endTime)) {
187
136
  flush(node.track);
188
137
  groups.set(node.track, {
189
138
  nodes: [node],
190
- line: node.layoutLine,
191
139
  beat,
192
140
  endTime: node.t.clone().add(node.T),
193
141
  });
@@ -202,12 +150,9 @@ function createAdjacentBeams(result, nodeScopes, explicitEndpoints) {
202
150
  }
203
151
  /** beam 域在 lowering 固化后统一生成作用域连接和相邻自动连接 */
204
152
  export function createAutomaticBeamAttachments(result) {
205
- const { scopes, nodeScopes } = collectDivScopes(result);
206
- if (scopes.length === 0)
207
- return [];
153
+ const scopes = collectDivScopes(result);
154
+ if (scopes.size === 0)
155
+ return;
208
156
  const explicitEndpoints = collectExplicitBeamEndpoints(result.attachments);
209
- return [
210
- ...createScopeBeams(scopes, nodeScopes, explicitEndpoints),
211
- ...createAdjacentBeams(result, nodeScopes, explicitEndpoints),
212
- ];
157
+ result.attachments.push(...createScopeBeams(scopes.values(), explicitEndpoints), ...createAdjacentBeams(result, scopes, explicitEndpoints));
213
158
  }
@@ -1,9 +1,7 @@
1
1
  import { ASTFunctionNode } from "../ASTtypes.js";
2
2
  import { Diagnostic, ErrorDiagnostic } from "../../diagnostic.js";
3
- import { createAutomaticBeamAttachments } from "./auto.js";
4
3
  import { createBeamLayoutAttachment, validateExplicitBeamAttachments, } from "./layout.js";
5
4
  class BeamFunction extends ASTFunctionNode {
6
- static loweringAugment = createAutomaticBeamAttachments;
7
5
  static loweringFinalize = validateExplicitBeamAttachments;
8
6
  static def = {
9
7
  name: ["beam"],
@@ -21,12 +19,9 @@ class BeamFunction extends ASTFunctionNode {
21
19
  endPoints = [];
22
20
  constructor(sourceSpan, args, ctx, parent = null) {
23
21
  super(sourceSpan, parent);
24
- for (const [, value] of args) {
25
- const parsed = value instanceof ASTFunctionNode
26
- ? value
27
- : ctx.parseArgWithType(value.valueSpan, "label", sourceSpan.start);
28
- if (parsed instanceof ASTFunctionNode)
29
- this.endPoints.push(parsed);
22
+ for (const [key, value] of args) {
23
+ if (typeof key === "number")
24
+ this.endPoints.push(value);
30
25
  }
31
26
  // 未显式给全时,回退到最近可标记对象,至少取两个。
32
27
  let k = ctx.labelableNodes.length - 1;
@@ -37,7 +32,7 @@ class BeamFunction extends ASTFunctionNode {
37
32
  this.endPoints.unshift(candidate);
38
33
  }
39
34
  if (this.endPoints.length < 2) {
40
- throw new ErrorDiagnostic("E_NOT_ENOUGH_ARGS", "@beam 至少需要两个端点", sourceSpan);
35
+ throw new ErrorDiagnostic("E_NOT_ENOUGH_ARGS", "@{fn} 至少需要两个端点", sourceSpan, { fn: "beam" });
41
36
  }
42
37
  }
43
38
  /** 与 tie 一样,只注册不推进时间的关系排版对象 */
@@ -45,9 +40,7 @@ class BeamFunction extends ASTFunctionNode {
45
40
  const endPoints = [];
46
41
  for (const ast of this.endPoints) {
47
42
  // 暂时只用最后一个;理应有且仅有一个
48
- let temporal = ctx.getTemporalNodes(ast).at(-1);
49
- while (temporal?.foldedInto)
50
- temporal = temporal.foldedInto;
43
+ const temporal = ctx.getTemporalNodes(ast).at(-1)?.rootHost;
51
44
  if (!temporal)
52
45
  continue;
53
46
  endPoints.push(temporal);
@@ -57,6 +50,7 @@ class BeamFunction extends ASTFunctionNode {
57
50
  return [];
58
51
  }
59
52
  ctx.addAttachment(createBeamLayoutAttachment(endPoints, true, this.sourceSpan));
53
+ ctx.deferFinalize(BeamFunction.loweringFinalize);
60
54
  return [];
61
55
  }
62
56
  }
@@ -6,5 +6,5 @@ import type { LoweringAttachment, LoweringResult } from "../../lowering/types.js
6
6
  export declare function createBeamLayoutAttachment(endPoints: TemporalNodeBase[], explicit: boolean, sourceSpan: SourceSpan): LayoutAttachment;
7
7
  /** 显式 beam 连接同轨同谱面行中的相邻可见主体,忽略零时长小节线 */
8
8
  export declare function validateExplicitBeamAttachments(result: LoweringResult): void;
9
- /** 自动分组在 loweringAugment 当下读取已经注册的显式 beam 端点 */
9
+ /** 自动分组在收尾阶段读取已经注册的显式 beam 端点 */
10
10
  export declare function collectExplicitBeamEndpoints(attachments: readonly LoweringAttachment[]): Set<TemporalNodeBase>;
@@ -47,7 +47,7 @@ export function validateExplicitBeamAttachments(result) {
47
47
  }
48
48
  }
49
49
  }
50
- /** 自动分组在 loweringAugment 当下读取已经注册的显式 beam 端点 */
50
+ /** 自动分组在收尾阶段读取已经注册的显式 beam 端点 */
51
51
  export function collectExplicitBeamEndpoints(attachments) {
52
52
  const result = new Set();
53
53
  for (const attachment of attachments) {
@@ -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
  * 进入目标内容前只开始收集所有成员矩形的引用
@@ -1,2 +1,34 @@
1
- import { ASTFunctionClass } from "../ASTtypes.js";
2
- export declare const BrNode: ASTFunctionClass;
1
+ import { ASTNodeBase, FunctionArgs, SourceSpan, ASTFunctionNode } from "../ASTtypes.js";
2
+ import { ParserContext } from "../../parser/parserContext.js";
3
+ import { TemporalNodeBase } from "../temporal.js";
4
+ import type { LoweringContext } from "../../lowering/loweringContext.js";
5
+ import type { LoweringResult } from "../../lowering/types.js";
6
+ import { GrammarCallNodeTyped } from "../../parser/grammarType.js";
7
+ declare class BrFunction extends ASTFunctionNode {
8
+ static def: {
9
+ name: string[];
10
+ description: string;
11
+ details: string;
12
+ allowExtraArgs: boolean;
13
+ args: {
14
+ name: string;
15
+ description: string;
16
+ type: "number";
17
+ default: number;
18
+ }[];
19
+ };
20
+ static deSugarAtom(source: string, start: number, end: number, depth: number): {
21
+ next: number;
22
+ node: GrammarCallNodeTyped;
23
+ } | null;
24
+ readonly offset: number;
25
+ loweringEnter(ctx: LoweringContext): TemporalNodeBase[];
26
+ /**
27
+ * 换行与换页共享校验;边界会切开全谱,不能截断持续事件
28
+ */
29
+ static loweringFinalize(result: LoweringResult): void;
30
+ constructor(span: SourceSpan, args: FunctionArgs, ctx: ParserContext, parent?: ASTNodeBase | null);
31
+ toString(): string;
32
+ }
33
+ export declare const BrNode: typeof BrFunction;
34
+ export {};
@@ -46,7 +46,8 @@ class BrFunction extends ASTFunctionNode {
46
46
  return { next, node };
47
47
  }
48
48
  offset;
49
- loweringEnter() {
49
+ loweringEnter(ctx) {
50
+ ctx.deferFinalize(BrFunction.loweringFinalize);
50
51
  const br = new TemporalNodeBase();
51
52
  br.ast = this;
52
53
  br.mergeKey = -1;
@@ -56,7 +57,7 @@ class BrFunction extends ASTFunctionNode {
56
57
  /**
57
58
  * 换行与换页共享校验;边界会切开全谱,不能截断持续事件
58
59
  */
59
- static loweringFinalize = (result) => {
60
+ static loweringFinalize(result) {
60
61
  const latestEnd = new Fraction();
61
62
  const candidateEnd = new Fraction();
62
63
  let latestNode = null;
@@ -74,13 +75,13 @@ class BrFunction extends ASTFunctionNode {
74
75
  }
75
76
  }
76
77
  }
77
- };
78
+ }
78
79
  constructor(span, args, ctx, parent = null) {
79
80
  super(span, parent);
80
81
  const [offset] = this.getArgValue(args, ctx);
81
82
  this.offset = Number.isFinite(offset) ? Math.max(1, Math.floor(offset)) : 1;
82
83
  if (this.offset !== offset) {
83
- ctx.diagnostics.push(new WarningDiagnostic("W_BR_OFFSET", `@br 的 offset 必须是大于等于 1 的整数,已被修正为 ${this.offset}`, this.sourceSpan));
84
+ ctx.diagnostics.push(new WarningDiagnostic("W_BR_OFFSET", "@br 的 offset 必须是大于等于 1 的整数,已被修正为 {offset}", this.sourceSpan, { offset: this.offset }));
84
85
  }
85
86
  }
86
87
  toString() { return `@br(${this.offset})`; }
@@ -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
  * 在结构阶段延长前方同轨、时间相接的一组区间
@@ -75,14 +73,13 @@ class DashTemporalNode extends TemporalNodeBase {
75
73
  // 这里只查询已发布的结构前缀;装饰音尚未展开,不需要逐个修改子音。
76
74
  emitter.defer(context => {
77
75
  let rootOrigin;
78
- // 从最近发布的区间向前找,锁定与当前起点相接的那次顶层访问。
76
+ let extended = false;
77
+ // 同轨最近一次顶层访问是唯一可能与当前起点相接的目标组;
79
78
  // 来源身份限定了整组目标,防止延长更早的音符或另一遍反复中的同一节点。
80
79
  for (let index = context.spans.length - 1; index >= 0; index--) {
81
80
  const span = context.spans[index];
82
- if (span.track !== track)
83
- continue;
84
- if (rootOrigin === undefined) {
85
- if (!span.end.equals(start))
81
+ if (!rootOrigin) {
82
+ if (span.track !== track)
86
83
  continue;
87
84
  rootOrigin = span.origins[0];
88
85
  }
@@ -92,14 +89,16 @@ class DashTemporalNode extends TemporalNodeBase {
92
89
  // 一组目标可以含多个成员;extend 同时承接当前访问在新增部分上的速度效果。
93
90
  if (span.end.equals(start)) {
94
91
  emitter.extend(span);
92
+ extended = true;
95
93
  }
96
94
  }
97
- if (rootOrigin === undefined) {
95
+ if (!extended) {
98
96
  context.diagnostics.push(new WarningDiagnostic("W_PLAYBACK_SUSTAIN_WITHOUT_TARGET", "增时线前没有可延续的音段", sourceSpan));
99
97
  }
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
  }
@@ -26,13 +26,14 @@ import { SymbolNode } from "./symbol/index.js";
26
26
  import { SetNode } from "./set/index.js";
27
27
  import { TempoNode } from "./tempo/index.js";
28
28
  import { TextNode } from "./text/index.js";
29
+ import { TremoloNode } from "./tremolo/index.js";
29
30
  import { VoltaNode } from "./volta/index.js";
30
31
  import { TieNode } from "./tie/index.js";
31
32
  import { TupletNode } from "./tuplet/index.js";
32
33
  import { VoiceNode, VoicesNode } from "./voice/index.js";
33
34
  export const defaultFunctions = [
34
35
  NoteNode, DashNode, RestNode, BarNode,
35
- DivNode, DotNode, TupletNode, // 装饰性
36
+ DivNode, DotNode, TupletNode, TremoloNode, // 装饰性
36
37
  VoiceNode, VoicesNode, // 歌词和声部
37
38
  BrNode, PageBreakNode, // 排版
38
39
  PageNode, HeadNode, // 文档页面与谱头
@@ -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;
@@ -53,15 +55,12 @@ declare class DivFunction extends ASTFunctionNode {
53
55
  static layoutDecorationHandler: LayoutDecorationHandler;
54
56
  loweringEnter(ctx: LoweringContext): never[];
55
57
  loweringExit(ctx: LoweringContext): never[];
58
+ private static loweringFinalize;
56
59
  content: ASTNodeBase;
57
60
  n: number;
58
61
  /** parse 时冻结;只控制不同 div 之间的自动连接,不影响当前 div 内部连接 */
59
62
  autoBeamEnabled: boolean;
60
63
  get children(): ASTNodeBase[];
61
- timeFlowModel(): {
62
- children: ASTNodeBase[];
63
- mode: "sequence";
64
- };
65
64
  constructor(sourceSpan: SourceSpan, args: FunctionArgs, ctx: ParserContext, parent?: ASTNodeBase | null);
66
65
  toString(source: string): string;
67
66
  }
@@ -1,7 +1,12 @@
1
1
  import { ASTFunctionNode, functionAddonKey } from "../ASTtypes.js";
2
2
  import { WarningDiagnostic } from "../../diagnostic.js";
3
+ import { createAutomaticBeamAttachments } from "../beam/auto.js";
3
4
  const DIV_FUNC_NAME = "div";
4
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
+ }
5
10
  /** div 暴露自己的线段端口,不依赖任何关系函数 */
6
11
  export function divLinePortName(level, edge) {
7
12
  return `div.${level}.${edge}`;
@@ -100,24 +105,24 @@ class DivFunction extends ASTFunctionNode {
100
105
  const n = nodes[at++];
101
106
  if (n.data?.class !== DivFunction)
102
107
  return null;
103
- const prevIndex = ASTFunctionNode.findLastFuncContentIndex(ctx.nodes, ctx.nodes.length - 1);
108
+ const prevIndex = ctx.findLastOperandIndex();
104
109
  if (prevIndex < 0)
105
110
  return at; // 没有了 直接当作无效文本跳过
106
111
  const prev = ctx.nodes[prevIndex];
107
112
  if (prev instanceof DivFunction) {
108
113
  // 已经是div了 继续加深
109
114
  prev.n += n.data.n;
115
+ prev.sourceSpan.end = Math.max(prev.sourceSpan.end, n.span.end);
110
116
  return at;
111
117
  }
112
118
  const argMap = new Map();
113
119
  argMap.set(0, prev);
114
120
  argMap.set("n", n.data.n);
115
121
  const spanPrev = prev.sourceSpan;
116
- const node = new DivFunction({
122
+ ctx.nodes[prevIndex] = new DivFunction({
117
123
  start: Math.min(spanPrev.start, n.span.start),
118
124
  end: Math.max(spanPrev.end, n.span.end),
119
125
  }, argMap, ctx, null);
120
- ctx.nodes[prevIndex] = node;
121
126
  return at;
122
127
  }
123
128
  /**
@@ -174,32 +179,31 @@ class DivFunction extends ASTFunctionNode {
174
179
  };
175
180
  loweringEnter(ctx) {
176
181
  const count = this.n;
182
+ if (count === 0)
183
+ return [];
184
+ const self = this;
185
+ ctx.deferFinalize(DivFunction.loweringFinalize, -5);
177
186
  ctx.beginLoweringGroup(this, {
178
187
  onTemporal(node) {
179
- if (count === 0)
180
- return;
181
- const addon = node.addon ??= {};
188
+ const addon = (node.addon ??= {});
182
189
  addon[DIV_ADDON_KEY] = (Number(addon[DIV_ADDON_KEY]) || 0) + count;
190
+ addon[DIV_OWNER_KEY] = self;
183
191
  node.T.divPow2(count);
184
192
  },
185
193
  });
186
194
  return [];
187
195
  }
188
196
  loweringExit(ctx) {
189
- ctx.endLoweringGroup(this);
197
+ if (this.n > 0)
198
+ ctx.endLoweringGroup(this);
190
199
  return [];
191
200
  }
201
+ static loweringFinalize = createAutomaticBeamAttachments;
192
202
  content;
193
203
  n;
194
204
  /** parse 时冻结;只控制不同 div 之间的自动连接,不影响当前 div 内部连接 */
195
205
  autoBeamEnabled;
196
206
  get children() { return [this.content]; }
197
- timeFlowModel() {
198
- return {
199
- children: [this.content],
200
- mode: "sequence",
201
- };
202
- }
203
207
  constructor(sourceSpan, args, ctx, parent = null) {
204
208
  super(sourceSpan, parent);
205
209
  [this.content, this.n, this.autoBeamEnabled] = this.getArgValue(args, ctx);
@@ -207,7 +211,7 @@ class DivFunction extends ASTFunctionNode {
207
211
  this.content.parent = this;
208
212
  const n = Math.max(0, Math.trunc(this.n));
209
213
  if (n !== this.n) {
210
- ctx.diagnostics.push(new WarningDiagnostic("W_DIV_INVALID_N", `@div 的数量参数 n 必须为非负整数,已被修正为 ${n}`, this.sourceSpan));
214
+ ctx.diagnostics.push(new WarningDiagnostic("W_DIV_INVALID_N", "@div 的数量参数 n 必须为非负整数,已被修正为 {n}", this.sourceSpan, { n }));
211
215
  this.n = n;
212
216
  }
213
217
  }