jpfun 0.3.0 → 0.4.1
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.
- package/README.md +73 -1
- package/dist/converter/midi/convert.d.ts +50 -0
- package/dist/converter/midi/convert.js +700 -0
- package/dist/converter/midi/index.d.ts +5 -0
- package/dist/converter/midi/index.js +11 -0
- package/dist/converter/midi/json.d.ts +38 -0
- package/dist/converter/midi/json.js +1 -0
- package/dist/converter/midi/triplet.d.ts +30 -0
- package/dist/converter/midi/triplet.js +75 -0
- package/dist/converter/musicxml/dom.d.ts +25 -0
- package/dist/converter/musicxml/dom.js +65 -0
- package/dist/converter/musicxml/features.d.ts +51 -0
- package/dist/converter/musicxml/features.js +232 -0
- package/dist/converter/musicxml/index.d.ts +9 -0
- package/dist/converter/musicxml/index.js +1140 -0
- package/dist/converter/musicxml/model.d.ts +123 -0
- package/dist/converter/musicxml/model.js +1 -0
- package/dist/converter/source.d.ts +22 -0
- package/dist/converter/source.js +51 -0
- package/dist/core.d.ts +22 -0
- package/dist/core.js +22 -0
- package/dist/functions/ASTtypes.d.ts +30 -26
- package/dist/functions/ASTtypes.js +25 -23
- package/dist/functions/adjust/index.js +13 -6
- package/dist/functions/arpeggio/index.d.ts +2 -0
- package/dist/functions/arpeggio/index.js +184 -0
- package/dist/functions/arpeggio/shape.d.ts +7 -0
- package/dist/functions/arpeggio/shape.js +122 -0
- package/dist/functions/bar/index.js +30 -23
- package/dist/functions/beam/auto.d.ts +1 -1
- package/dist/functions/beam/auto.js +1 -1
- package/dist/functions/beam/index.js +6 -1
- package/dist/functions/beam/layout.d.ts +3 -3
- package/dist/functions/beam/layout.js +5 -3
- package/dist/functions/box/index.js +10 -3
- package/dist/functions/br/index.js +10 -6
- package/dist/functions/dash/index.js +12 -9
- package/dist/functions/default.js +6 -2
- package/dist/functions/div/index.d.ts +4 -1
- package/dist/functions/div/index.js +8 -7
- package/dist/functions/dot/index.js +18 -25
- package/dist/functions/dyn/index.d.ts +2 -0
- package/dist/functions/dyn/index.js +242 -0
- package/dist/functions/grace/index.d.ts +1 -1
- package/dist/functions/grace/index.js +17 -11
- package/dist/functions/head/index.js +168 -159
- package/dist/functions/key/index.js +12 -7
- package/dist/functions/meter/index.js +14 -6
- package/dist/functions/note/index.js +73 -31
- package/dist/functions/note/noteNameFSM.js +52 -63
- package/dist/functions/page/index.d.ts +5 -1
- package/dist/functions/page/index.js +38 -16
- package/dist/functions/program/index.d.ts +2 -0
- package/dist/functions/program/index.js +39 -0
- package/dist/functions/set/index.js +13 -8
- package/dist/functions/stack/index.js +7 -5
- package/dist/functions/symbol/index.d.ts +13 -3
- package/dist/functions/symbol/index.js +71 -22
- package/dist/functions/symbol/shape.d.ts +2 -0
- package/dist/functions/symbol/shape.js +14 -10
- package/dist/functions/symbol/symbols/dc.d.ts +4 -0
- package/dist/functions/symbol/symbols/dc.js +82 -0
- package/dist/functions/symbol/symbols/{f.d.ts → ds.d.ts} +1 -1
- package/dist/functions/symbol/symbols/ds.js +93 -0
- package/dist/functions/symbol/symbols/dynamics.d.ts +3 -0
- package/dist/functions/symbol/symbols/dynamics.js +168 -0
- package/dist/functions/symbol/symbols/{p.d.ts → fine.d.ts} +1 -1
- package/dist/functions/symbol/symbols/fine.js +12 -0
- package/dist/functions/symbol/symbols/mordent.js +44 -31
- package/dist/functions/symbol/symbols/prall.d.ts +2 -0
- package/dist/functions/symbol/symbols/prall.js +42 -0
- package/dist/functions/symbol/symbols/segno.d.ts +4 -0
- package/dist/functions/symbol/symbols/segno.js +82 -0
- package/dist/functions/symbol/symbols/tr.js +0 -4
- package/dist/functions/tempo/index.js +12 -4
- package/dist/functions/temporal.d.ts +138 -0
- package/dist/functions/temporal.js +101 -0
- package/dist/functions/text/index.js +25 -10
- package/dist/functions/tie/index.js +45 -30
- package/dist/functions/tuplet/index.js +21 -8
- package/dist/functions/up/index.js +38 -33
- package/dist/functions/voice/index.js +153 -68
- package/dist/functions/volta/index.js +62 -55
- package/dist/iife/core.d.ts +1 -0
- package/dist/iife/core.js +3 -0
- package/dist/iife/from-midi.d.ts +1 -0
- package/dist/iife/from-midi.js +15 -0
- package/dist/iife/from-musicxml.d.ts +1 -0
- package/dist/iife/from-musicxml.js +3 -0
- package/dist/iife/full.d.ts +1 -0
- package/dist/iife/full.js +3 -0
- package/dist/index.d.ts +3 -21
- package/dist/index.js +3 -21
- package/dist/jpfun.core.min.js +119 -0
- package/dist/jpfun.from-midi.min.js +9 -0
- package/dist/jpfun.from-musicxml.min.js +12 -0
- package/dist/jpfun.min.js +111 -122
- package/dist/layout/default.js +2 -3
- package/dist/layout/engine.d.ts +2 -1
- package/dist/layout/engine.js +71 -26
- package/dist/layout/model.d.ts +4 -4
- package/dist/layout/model.js +15 -12
- package/dist/layout/page.js +5 -4
- package/dist/layout/path.js +3 -3
- package/dist/layout/types.d.ts +20 -2
- package/dist/lowering/loweringContext.d.ts +3 -1
- package/dist/lowering/loweringContext.js +28 -14
- package/dist/lowering/track.d.ts +4 -8
- package/dist/lowering/track.js +6 -1
- package/dist/lowering/types.d.ts +5 -128
- package/dist/lowering/types.js +1 -98
- package/dist/parser/parse-utils/string-utils.d.ts +2 -0
- package/dist/parser/parse-utils/string-utils.js +4 -0
- package/dist/parser/parserContext.d.ts +7 -8
- package/dist/parser/parserContext.js +41 -36
- package/dist/pipeline.d.ts +6 -5
- package/dist/pipeline.js +4 -5
- package/dist/playback/compile.d.ts +1 -1
- package/dist/playback/compile.js +15 -2
- package/dist/playback/event.d.ts +17 -3
- package/dist/playback/event.js +30 -12
- package/dist/playback/time.js +1 -1
- package/dist/playback/types.d.ts +5 -4
- package/dist/render/text.d.ts +6 -1
- package/dist/render/text.js +8 -1
- package/grammar.md +111 -57
- package/package.json +11 -2
- package/dist/functions/symbol/symbols/f.js +0 -55
- package/dist/functions/symbol/symbols/p.js +0 -61
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { ASTFunctionNode } from "../ASTtypes.js";
|
|
2
2
|
import { Diagnostic, ErrorDiagnostic } from "../../diagnostic.js";
|
|
3
|
-
import { isVisualTemporalNode, } from "
|
|
3
|
+
import { isVisualTemporalNode, } from "../temporal.js";
|
|
4
4
|
import { pathBounds } from "../../layout/path.js";
|
|
5
5
|
/** 三次贝塞尔控制点的抬高系数:两个控制点同高时,实际弧高恰好是抬高的 3/4 */
|
|
6
6
|
const CUBIC_LIFT = 4 / 3;
|
|
@@ -10,8 +10,14 @@ class TieFunction extends ASTFunctionNode {
|
|
|
10
10
|
static def = {
|
|
11
11
|
name: ["tie"],
|
|
12
12
|
description: "连音线",
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
details: `\
|
|
14
|
+
~~~jpfun
|
|
15
|
+
1@a 2 3@b @tie(a, b, height=0.5em)
|
|
16
|
+
~~~
|
|
17
|
+
- **位置参数**:端点标签,可传多个,按顺序连接;省略时查找最近的可连接对象
|
|
18
|
+
- **height**:弧线高度,默认 \`0.5em\`
|
|
19
|
+
|
|
20
|
+
同行端点用一条弧线连接,跨行时自动拆成多段`,
|
|
15
21
|
allowExtraArgs: true,
|
|
16
22
|
extraArgType: "label",
|
|
17
23
|
args: []
|
|
@@ -20,7 +26,7 @@ class TieFunction extends ASTFunctionNode {
|
|
|
20
26
|
height;
|
|
21
27
|
constructor(sourceSpan, args, ctx, parent = null) {
|
|
22
28
|
super(sourceSpan, parent);
|
|
23
|
-
let height = ctx.
|
|
29
|
+
let height = ctx.variables.fontsize * 0.5;
|
|
24
30
|
for (const [key, value] of args) {
|
|
25
31
|
if (key === "height") {
|
|
26
32
|
const length = ctx.parseArgWithType(value.valueSpan, "length", sourceSpan.start);
|
|
@@ -70,17 +76,6 @@ class TieFunction extends ASTFunctionNode {
|
|
|
70
76
|
}
|
|
71
77
|
}
|
|
72
78
|
export const TieNode = TieFunction;
|
|
73
|
-
/** 二次贝塞尔在某一维上的内部极值 */
|
|
74
|
-
function quadExtremum(p0, p1, p2) {
|
|
75
|
-
const d = p0 - 2 * p1 + p2;
|
|
76
|
-
if (Math.abs(d) < 1e-9)
|
|
77
|
-
return null;
|
|
78
|
-
const t = (p0 - p1) / d;
|
|
79
|
-
if (t <= 0 || t >= 1)
|
|
80
|
-
return null;
|
|
81
|
-
const u = 1 - t;
|
|
82
|
-
return u * u * p0 + 2 * u * t * p1 + t * t * p2;
|
|
83
|
-
}
|
|
84
79
|
class TieLayoutAttachment {
|
|
85
80
|
layer = "foreground";
|
|
86
81
|
sourceSpan;
|
|
@@ -114,6 +109,7 @@ class TieLayoutAttachment {
|
|
|
114
109
|
&& note.origins.some(origin => origin.node === this.endPoints[i])
|
|
115
110
|
&& note.track === head.track
|
|
116
111
|
&& note.midi === head.midi
|
|
112
|
+
&& note.percussion === head.percussion
|
|
117
113
|
&& end.at.equals(note.at));
|
|
118
114
|
if (!next)
|
|
119
115
|
break;
|
|
@@ -136,6 +132,7 @@ class TieLayoutAttachment {
|
|
|
136
132
|
const regions = segments.map(({ commands, ...region }) => region);
|
|
137
133
|
const occupancy = segments.map(segment => {
|
|
138
134
|
// 只申报主体上方那一段,下半截由主体自己占;本行没有主体时就只有弧带本身
|
|
135
|
+
// 用 getHostExtent 而不是 getRangeExtents:连音线只避让主体,彼此之间允许重叠
|
|
139
136
|
const hostExtent = context.getHostExtent(segment.line, segment.track);
|
|
140
137
|
const bottom = hostExtent
|
|
141
138
|
? Math.max(segment.y, context.getVisualAxis(segment.line, segment.track) + hostExtent.top)
|
|
@@ -160,8 +157,8 @@ class TieLayoutAttachment {
|
|
|
160
157
|
},
|
|
161
158
|
};
|
|
162
159
|
}
|
|
163
|
-
|
|
164
|
-
const port = node.ports[
|
|
160
|
+
tiePort(node) {
|
|
161
|
+
const port = node.ports["tie.top"];
|
|
165
162
|
if (port) {
|
|
166
163
|
return {
|
|
167
164
|
x: node.box.x + port.x,
|
|
@@ -173,7 +170,7 @@ class TieLayoutAttachment {
|
|
|
173
170
|
y: node.box.y,
|
|
174
171
|
};
|
|
175
172
|
}
|
|
176
|
-
/**
|
|
173
|
+
/** 根据端点固化后的行号选择同行弧线或跨行分段 */
|
|
177
174
|
createSegments(context) {
|
|
178
175
|
const segments = [];
|
|
179
176
|
for (let i = 1; i < this.endPoints.length; i++) {
|
|
@@ -182,8 +179,8 @@ class TieLayoutAttachment {
|
|
|
182
179
|
// 标签书写顺序不保证时间顺序,先按谱面行升序规范化
|
|
183
180
|
if (startNode.layoutLine > endNode.layoutLine)
|
|
184
181
|
[startNode, endNode] = [endNode, startNode];
|
|
185
|
-
const start = this.
|
|
186
|
-
const end = this.
|
|
182
|
+
const start = this.tiePort(startNode);
|
|
183
|
+
const end = this.tiePort(endNode);
|
|
187
184
|
if (startNode.layoutLine === endNode.layoutLine) {
|
|
188
185
|
const startTop = context.getVisualAxis(startNode.layoutLine, startNode.track)
|
|
189
186
|
+ (context.getHostExtent(startNode.layoutLine, startNode.track)?.top ?? 0);
|
|
@@ -193,21 +190,39 @@ class TieLayoutAttachment {
|
|
|
193
190
|
continue;
|
|
194
191
|
}
|
|
195
192
|
// 首行先起弧,再沿水平段延伸到当前谱面行最右侧
|
|
196
|
-
segments.push(this.openingSegment(start, context
|
|
193
|
+
segments.push(this.openingSegment(start, this.lineRightX(context, startNode.layoutLine), this.plateauY(context, startNode.layoutLine, startNode.track, start.y), startNode.layoutLine, startNode.track));
|
|
197
194
|
// 中间行可能没有任何可见对象,仍然需要逐行补一段水平线
|
|
198
195
|
for (let line = startNode.layoutLine + 1; line < endNode.layoutLine; line++) {
|
|
199
|
-
segments.push(this.horizontalSegment(context.
|
|
196
|
+
segments.push(this.horizontalSegment(this.lineEntryX(context, line, startNode.track), this.lineRightX(context, line), this.plateauY(context, line, startNode.track), line, startNode.track));
|
|
200
197
|
}
|
|
201
|
-
//
|
|
202
|
-
segments.push(this.closingSegment(context.
|
|
198
|
+
// 跳过声部名、大括号等位于端点列之前的系统前缀,再落弧到终点
|
|
199
|
+
segments.push(this.closingSegment(this.lineEntryX(context, endNode.layoutLine, endNode.track), end, this.plateauY(context, endNode.layoutLine, endNode.track, end.y), endNode.layoutLine, endNode.track));
|
|
203
200
|
}
|
|
204
201
|
return segments;
|
|
205
202
|
}
|
|
206
|
-
|
|
203
|
+
lineEntryX(context, lineIndex, track) {
|
|
204
|
+
const line = context.lines[lineIndex];
|
|
205
|
+
const host = line.trackRuns.get(track)?.find(host => !host.T.isZero());
|
|
206
|
+
return host?.box.x ?? context.originX;
|
|
207
|
+
}
|
|
208
|
+
lineRightX(context, lineIndex) {
|
|
209
|
+
const runs = context.lines[lineIndex].trackRuns;
|
|
210
|
+
if (runs.size === 0)
|
|
211
|
+
return context.originX + context.width;
|
|
212
|
+
let right = context.originX;
|
|
213
|
+
for (const run of runs.values()) {
|
|
214
|
+
for (const host of run)
|
|
215
|
+
right = Math.max(right, host.box.x + host.box.w);
|
|
216
|
+
}
|
|
217
|
+
return right;
|
|
218
|
+
}
|
|
219
|
+
/** 有端点时从端点固定抬高;中间段没有端点,才以当前 Track 的主体顶部为基准 */
|
|
207
220
|
plateauY(context, line, track, endpointY = Infinity) {
|
|
221
|
+
if (Number.isFinite(endpointY))
|
|
222
|
+
return endpointY - this.height;
|
|
208
223
|
const axis = context.getVisualAxis(line, track);
|
|
209
224
|
const hostTop = context.getHostExtent(line, track)?.top ?? 0;
|
|
210
|
-
return
|
|
225
|
+
return axis + hostTop - this.height;
|
|
211
226
|
}
|
|
212
227
|
/** 一条三次贝塞尔的外缘配上反向内缘,得到两端收尖、中部最厚的乐谱弧线 */
|
|
213
228
|
arcSegment(startInput, endInput, line, track) {
|
|
@@ -230,8 +245,8 @@ class TieLayoutAttachment {
|
|
|
230
245
|
}
|
|
231
246
|
/** 跨行首段:从端点起弧,随后水平延伸到右边界 */
|
|
232
247
|
openingSegment(start, right, plateauY, line, track) {
|
|
233
|
-
const
|
|
234
|
-
|
|
248
|
+
const run = this.height * 1.2;
|
|
249
|
+
right = Math.max(right, start.x + run);
|
|
235
250
|
const curveX = start.x + run;
|
|
236
251
|
const controlX = start.x + run * 0.45;
|
|
237
252
|
const bottom = plateauY + this.thickness;
|
|
@@ -247,8 +262,8 @@ class TieLayoutAttachment {
|
|
|
247
262
|
}
|
|
248
263
|
/** 跨行末段:从左边界水平进入,最后落弧到端点 */
|
|
249
264
|
closingSegment(left, end, plateauY, line, track) {
|
|
250
|
-
const
|
|
251
|
-
|
|
265
|
+
const run = this.height * 1.2;
|
|
266
|
+
left = Math.min(left, end.x - run);
|
|
252
267
|
const curveX = end.x - run;
|
|
253
268
|
const controlX = end.x - run * 0.45;
|
|
254
269
|
const bottom = plateauY + this.thickness;
|
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import { ErrorDiagnostic } from "../../diagnostic.js";
|
|
2
2
|
import { Fraction } from "../../fraction.js";
|
|
3
|
-
import { isVisualTemporalNode, } from "
|
|
3
|
+
import { isVisualTemporalNode, } from "../temporal.js";
|
|
4
4
|
import { ASTFunctionNode, } from "../ASTtypes.js";
|
|
5
|
-
import { JIANPU_NUMBER_FONT } from "../../render/text.js";
|
|
6
5
|
/**
|
|
7
6
|
* 多连音是范围时值变换,不产生自己的 Temporal:
|
|
8
7
|
* actual = 内容原总时值 / 最短正时值
|
|
@@ -12,15 +11,21 @@ class TupletFunction extends ASTFunctionNode {
|
|
|
12
11
|
static def = {
|
|
13
12
|
name: ["tuplet"],
|
|
14
13
|
description: "多连音",
|
|
15
|
-
|
|
14
|
+
details: `\
|
|
15
|
+
~~~jpfun
|
|
16
|
+
@tuplet({1/ 2 3}, normal=4)
|
|
17
|
+
~~~
|
|
18
|
+
本例最短单位为八分音符,原内容占 5 个单位,缩放后占 4 个单位:形成五连音,总时长为一个二分音符`,
|
|
16
19
|
allowExtraArgs: false,
|
|
17
20
|
args: [
|
|
18
21
|
{
|
|
19
22
|
type: "content",
|
|
23
|
+
description: "连音内容,各元素按同一比例缩放时值",
|
|
20
24
|
default: null,
|
|
21
25
|
},
|
|
22
26
|
{
|
|
23
27
|
name: "normal",
|
|
28
|
+
description: "目标总时长,以内容中的最短时值为单位",
|
|
24
29
|
type: "number",
|
|
25
30
|
default: null,
|
|
26
31
|
},
|
|
@@ -34,6 +39,7 @@ class TupletFunction extends ASTFunctionNode {
|
|
|
34
39
|
};
|
|
35
40
|
content;
|
|
36
41
|
normal;
|
|
42
|
+
font;
|
|
37
43
|
get children() { return [this.content]; }
|
|
38
44
|
timeFlowModel() {
|
|
39
45
|
return {
|
|
@@ -99,13 +105,14 @@ class TupletFunction extends ASTFunctionNode {
|
|
|
99
105
|
// 括线只依附可见主体;时值缩放本身仍覆盖所有收集到的事件。
|
|
100
106
|
const visible = positive.filter(isVisualTemporalNode);
|
|
101
107
|
if (visible.length >= 2) {
|
|
102
|
-
ctx.addAttachment(new TupletLayoutAttachment(visible, actual, this.sourceSpan));
|
|
108
|
+
ctx.addAttachment(new TupletLayoutAttachment(visible, actual, this.sourceSpan, this.font));
|
|
103
109
|
}
|
|
104
110
|
return [];
|
|
105
111
|
}
|
|
106
112
|
constructor(span, args, ctx, parent = null) {
|
|
107
113
|
super(span, parent);
|
|
108
114
|
const [content, normal] = this.getArgValue(args, ctx);
|
|
115
|
+
this.font = ctx.variables.numberfont;
|
|
109
116
|
// normal 是目标包含的“最短时值单位数”,不是 QN 数,也不是显示的 actual。
|
|
110
117
|
if (!Number.isSafeInteger(normal) || normal <= 0) {
|
|
111
118
|
throw new ErrorDiagnostic("E_TUPLET_INVALID_NORMAL", "@tuplet 的 normal 必须是正的安全整数", span);
|
|
@@ -132,14 +139,14 @@ class TupletLayoutAttachment {
|
|
|
132
139
|
/** 由端点最大字号冻结的绘制规格 */
|
|
133
140
|
style;
|
|
134
141
|
size;
|
|
135
|
-
constructor(endPoints, actual, sourceSpan) {
|
|
142
|
+
constructor(endPoints, actual, sourceSpan, font) {
|
|
136
143
|
this.endPoints = endPoints;
|
|
137
144
|
this.actual = actual;
|
|
138
145
|
this.sourceSpan = sourceSpan;
|
|
139
146
|
this.size = endPoints.reduce((size, endpoint) => Math.max(size, endpoint.ast.size), 0);
|
|
140
147
|
this.style = {
|
|
141
148
|
fontSize: this.size * 0.72,
|
|
142
|
-
fontFamily:
|
|
149
|
+
fontFamily: font,
|
|
143
150
|
textAlign: "center",
|
|
144
151
|
fill: "#000",
|
|
145
152
|
};
|
|
@@ -167,9 +174,15 @@ class TupletLayoutAttachment {
|
|
|
167
174
|
const textGap = this.size * 0.12;
|
|
168
175
|
const hookHeight = this.size * 0.18;
|
|
169
176
|
const hostGap = this.size * 0.14;
|
|
170
|
-
//
|
|
177
|
+
// 只测首尾时间列,并合并先注册的 attachment,使后声明的括线排在外层。
|
|
178
|
+
const view = context.lines[first.layoutLine];
|
|
179
|
+
const from = view?.columnOf(first) ?? -1;
|
|
180
|
+
const to = view?.columnOf(last) ?? -1;
|
|
171
181
|
const axis = context.getVisualAxis(first.layoutLine, first.track);
|
|
172
|
-
const
|
|
182
|
+
const extent = from < 0 || to < 0
|
|
183
|
+
? undefined
|
|
184
|
+
: context.getRangeExtents(first.layoutLine, [Math.min(from, to), Math.max(from, to)]).get(first.track);
|
|
185
|
+
const hostTop = axis + (extent?.top ?? 0);
|
|
173
186
|
const strokeWidth = Math.max(1, this.size * 0.055);
|
|
174
187
|
const lineY = hostTop - hostGap - Math.max(hookHeight, metrics.h / 2);
|
|
175
188
|
const hookBottom = lineY + hookHeight;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { ASTNodeBase, ASTFunctionNode, ASTTextNode, ASTLabelNode, ASTBraceNode } from "../ASTtypes.js";
|
|
2
2
|
import { ErrorDiagnostic } from "../../diagnostic.js";
|
|
3
|
-
import { ANCHOR_KEY, DEFAULT_KEY, isVisualTemporalNode, TemporalNodeBase, } from "
|
|
3
|
+
import { ANCHOR_KEY, DEFAULT_KEY, isVisualTemporalNode, TemporalNodeBase, } from "../temporal.js";
|
|
4
4
|
import { Fraction } from "../../fraction.js";
|
|
5
5
|
import { prepareLayoutHost } from "../../layout/engine.js";
|
|
6
6
|
/**
|
|
@@ -12,7 +12,7 @@ import { prepareLayoutHost } from "../../layout/engine.js";
|
|
|
12
12
|
class FoldFunction extends ASTFunctionNode {
|
|
13
13
|
contents = [];
|
|
14
14
|
belows = [];
|
|
15
|
-
size;
|
|
15
|
+
size; // 用于计算 gap
|
|
16
16
|
/** 静态而不是实例字段:子类的字段初始化器晚于基类构造器,构造期读不到 */
|
|
17
17
|
get side() { return this.constructor.side; }
|
|
18
18
|
get children() {
|
|
@@ -20,7 +20,7 @@ class FoldFunction extends ASTFunctionNode {
|
|
|
20
20
|
}
|
|
21
21
|
constructor(span, args, ctx, parent = null) {
|
|
22
22
|
super(span, parent);
|
|
23
|
-
this.size = ctx.
|
|
23
|
+
this.size = ctx.variables.fontsize;
|
|
24
24
|
for (const [, value] of args) {
|
|
25
25
|
if (value instanceof ASTNodeBase) {
|
|
26
26
|
this.addContent(value, this.side, ctx);
|
|
@@ -178,12 +178,14 @@ class UpFunction extends FoldFunction {
|
|
|
178
178
|
static def = {
|
|
179
179
|
name: ["up"],
|
|
180
180
|
description: "把同一时间位置的可见对象向上堆叠",
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
181
|
+
details: `\
|
|
182
|
+
~~~jpfun
|
|
183
|
+
@up(1, 3, 5)
|
|
184
|
+
~~~
|
|
185
|
+
|
|
186
|
+
第一个内容参数为宿主,其余依次叠在上方;所有成员共享宿主时值。常用于和弦、速度记号和音符注释。
|
|
187
|
+
|
|
188
|
+
**简写**:\`1 ^ 3 ^ 5\`。复杂内容可用 \`{...}\` 包裹;需要独立节奏的并行分支时,使用 \`&\` / \`@stack\`。`,
|
|
187
189
|
allowExtraArgs: true,
|
|
188
190
|
extraArgType: "content",
|
|
189
191
|
args: [],
|
|
@@ -203,12 +205,13 @@ class DownFunction extends FoldFunction {
|
|
|
203
205
|
static def = {
|
|
204
206
|
name: ["down"],
|
|
205
207
|
description: "把同一时间位置的可见对象向下堆叠",
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
208
|
+
details: `\
|
|
209
|
+
~~~jpfun
|
|
210
|
+
@down(1, $p)
|
|
211
|
+
~~~
|
|
212
|
+
第一个内容参数为宿主,其余依次叠在下方并共享其时值,常用于力度等下方标记。
|
|
213
|
+
|
|
214
|
+
**简写**:\`1 _ $p\`。与 \`^\` 混写时共用同一宿主:\`1^2_3\` 将 \`2\` 放在 \`1\` 上方、\`3\` 放在下方。`,
|
|
212
215
|
allowExtraArgs: true,
|
|
213
216
|
extraArgType: "content",
|
|
214
217
|
args: [],
|
|
@@ -229,8 +232,8 @@ class FoldTemporal extends TemporalNodeBase {
|
|
|
229
232
|
members;
|
|
230
233
|
/** members[0] 是宿主,`[1, aboveCount)` 向上叠,`[aboveCount, end)` 向下叠 */
|
|
231
234
|
aboveCount;
|
|
232
|
-
/**
|
|
233
|
-
|
|
235
|
+
/** 每个成员相对本盒顶部的纵向偏移,onPlaced 时同步为绝对坐标 */
|
|
236
|
+
verticalOffsets = [];
|
|
234
237
|
constructor(ast, members) {
|
|
235
238
|
super();
|
|
236
239
|
this.ast = ast;
|
|
@@ -285,6 +288,9 @@ class FoldTemporal extends TemporalNodeBase {
|
|
|
285
288
|
for (let i = this.members.length - 1; i >= 0; i--)
|
|
286
289
|
emitter.play(this.members[i]);
|
|
287
290
|
}
|
|
291
|
+
prepareHorizontal(line) {
|
|
292
|
+
this.members[0]?.prepareHorizontal?.(line);
|
|
293
|
+
}
|
|
288
294
|
/**
|
|
289
295
|
* 宿主留在轨道基线上,其余成员按书写顺序向上或向下叠放
|
|
290
296
|
*
|
|
@@ -292,7 +298,6 @@ class FoldTemporal extends TemporalNodeBase {
|
|
|
292
298
|
* 准备直接复用引擎的 prepareLayoutHost,保证成员的装饰、端口与顶层对象完全一致。
|
|
293
299
|
*/
|
|
294
300
|
prepareLayout(context) {
|
|
295
|
-
this.offsets.length = 0;
|
|
296
301
|
// lowering 期间修饰挂在折叠体上(augmenter 要看到整体节奏),渲染时交给宿主:
|
|
297
302
|
// 减时线要落在它的数字与下八度点之间,而不是压在整个盒子下面
|
|
298
303
|
if (this.addon && this.members[0]) {
|
|
@@ -307,56 +312,56 @@ class FoldTemporal extends TemporalNodeBase {
|
|
|
307
312
|
this.box.anchor = this.box.visualAxis = 0;
|
|
308
313
|
return;
|
|
309
314
|
}
|
|
315
|
+
this.springConfig = { ...first.springConfig, ...this.springConfig };
|
|
310
316
|
// 横向完全等于宿主:上下的标记(变速、注释、力度)常常比音符宽得多,
|
|
311
317
|
// 让它们撑宽盒子会把右邻推开一大截;它们画在基线外侧,伸出盒外也不会碰撞
|
|
312
318
|
const anchor = first.box.anchor;
|
|
313
319
|
const gap = this.ast.size * 0.12;
|
|
314
|
-
|
|
315
|
-
|
|
320
|
+
this.verticalOffsets.length = this.members.length;
|
|
321
|
+
this.verticalOffsets.fill(0);
|
|
316
322
|
// 以宿主盒顶为 0,向上得到负坐标、向下得到正坐标,最后整体下移
|
|
317
323
|
let top = 0;
|
|
318
324
|
for (let i = 1; i < this.aboveCount; i++) {
|
|
319
325
|
top -= gap + this.members[i].box.h;
|
|
320
|
-
this.
|
|
326
|
+
this.verticalOffsets[i] = top;
|
|
321
327
|
}
|
|
322
328
|
let bottom = first.box.h;
|
|
323
329
|
for (let i = this.aboveCount; i < this.members.length; i++) {
|
|
324
330
|
bottom += gap;
|
|
325
|
-
this.
|
|
331
|
+
this.verticalOffsets[i] = bottom;
|
|
326
332
|
bottom += this.members[i].box.h;
|
|
327
333
|
}
|
|
328
|
-
for (
|
|
329
|
-
|
|
334
|
+
for (let i = 0; i < this.verticalOffsets.length; i++)
|
|
335
|
+
this.verticalOffsets[i] -= top;
|
|
330
336
|
this.box.w = first.box.w;
|
|
331
337
|
this.box.h = bottom - top;
|
|
332
338
|
this.box.anchor = anchor;
|
|
333
339
|
// 宿主对齐轨道基线,两侧成员各自向外撑开行高
|
|
334
340
|
this.box.visualAxis = first.box.visualAxis - top;
|
|
335
|
-
//
|
|
341
|
+
// 端口:宿主代表整个折叠体,把它发布的端口原样上提,
|
|
336
342
|
// 减时线、歌词等端口于是与普通音符完全一致,关系函数不需要认识 up
|
|
337
|
-
const firstOffset = this.
|
|
343
|
+
const firstOffset = this.verticalOffsets[0];
|
|
338
344
|
for (const name in first.ports) {
|
|
339
345
|
const port = first.ports[name];
|
|
340
|
-
this.ports[name] = { x: port.x, y: firstOffset
|
|
346
|
+
this.ports[name] = { x: port.x, y: firstOffset + port.y };
|
|
341
347
|
}
|
|
342
348
|
// 代表成员没声明核心范围时退回它的整个盒子
|
|
343
349
|
this.ports["body.left"] ??= { x: 0, y: this.box.visualAxis };
|
|
344
350
|
this.ports["body.right"] ??= { x: first.box.w, y: this.box.visualAxis };
|
|
345
351
|
// 唯一的例外:连音线要接到最上面那个成员的顶部
|
|
346
352
|
const topIndex = this.aboveCount - 1;
|
|
347
|
-
const topOffset = this.
|
|
353
|
+
const topOffset = this.verticalOffsets[topIndex];
|
|
348
354
|
this.ports["tie.top"] = {
|
|
349
355
|
x: anchor,
|
|
350
|
-
y: topOffset
|
|
356
|
+
y: topOffset + (this.members[topIndex].ports["tie.top"]?.y ?? 0),
|
|
351
357
|
};
|
|
352
358
|
}
|
|
353
|
-
/**
|
|
359
|
+
/** 引擎每次改变本节点坐标后,按当前 anchor 与准备阶段的纵向偏移重算成员绝对坐标 */
|
|
354
360
|
onPlaced() {
|
|
355
361
|
for (let i = 0; i < this.members.length; i++) {
|
|
356
|
-
const offset = this.offsets[i];
|
|
357
362
|
const member = this.members[i];
|
|
358
|
-
member.box.x = this.box.x +
|
|
359
|
-
member.box.y = this.box.y +
|
|
363
|
+
member.box.x = this.box.x + this.box.anchor - member.box.anchor;
|
|
364
|
+
member.box.y = this.box.y + this.verticalOffsets[i];
|
|
360
365
|
member.onPlaced?.();
|
|
361
366
|
}
|
|
362
367
|
}
|