jpfun 0.2.0 → 0.4.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.
- package/README.md +141 -11
- 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/diagnostic.js +5 -2
- package/dist/fraction.d.ts +24 -0
- package/dist/fraction.js +132 -0
- package/dist/functions/ASTtypes.d.ts +51 -31
- package/dist/functions/ASTtypes.js +59 -38
- package/dist/functions/adjust/index.d.ts +2 -0
- package/dist/functions/adjust/index.js +193 -0
- package/dist/functions/arpeggio/index.d.ts +2 -0
- package/dist/functions/arpeggio/index.js +180 -0
- package/dist/functions/arpeggio/shape.d.ts +7 -0
- package/dist/functions/arpeggio/shape.js +122 -0
- package/dist/functions/bar/index.d.ts +8 -0
- package/dist/functions/bar/index.js +62 -25
- package/dist/functions/beam/auto.d.ts +1 -1
- package/dist/functions/beam/auto.js +33 -16
- package/dist/functions/beam/index.js +10 -6
- package/dist/functions/beam/layout.d.ts +4 -3
- package/dist/functions/beam/layout.js +43 -65
- package/dist/functions/box/index.js +37 -36
- package/dist/functions/br/index.js +10 -8
- package/dist/functions/dash/index.js +40 -7
- package/dist/functions/default.js +19 -8
- package/dist/functions/div/index.d.ts +16 -0
- package/dist/functions/div/index.js +45 -17
- package/dist/functions/dot/index.js +23 -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 +8 -11
- package/dist/functions/grace/index.js +84 -79
- package/dist/functions/head/index.d.ts +2 -0
- package/dist/functions/head/index.js +496 -0
- package/dist/functions/key/index.js +98 -3
- package/dist/functions/meter/index.d.ts +2 -0
- package/dist/functions/meter/index.js +155 -0
- package/dist/functions/note/accidentals.d.ts +24 -3
- package/dist/functions/note/accidentals.js +31 -1
- package/dist/functions/note/index.js +106 -79
- package/dist/functions/note/noteNameFSM.js +52 -63
- package/dist/functions/page/index.d.ts +11 -2
- package/dist/functions/page/index.js +65 -4
- package/dist/functions/program/index.d.ts +2 -0
- package/dist/functions/program/index.js +30 -0
- package/dist/functions/set/index.js +43 -32
- package/dist/functions/stack/index.js +26 -22
- package/dist/functions/symbol/index.d.ts +31 -0
- package/dist/functions/symbol/index.js +139 -0
- package/dist/functions/symbol/ornament.d.ts +15 -0
- package/dist/functions/symbol/ornament.js +73 -0
- package/dist/functions/symbol/shape.d.ts +24 -0
- package/dist/functions/symbol/shape.js +52 -0
- package/dist/functions/symbol/symbols/accent.d.ts +2 -0
- package/dist/functions/symbol/symbols/accent.js +24 -0
- package/dist/functions/symbol/symbols/dc.d.ts +4 -0
- package/dist/functions/symbol/symbols/dc.js +82 -0
- package/dist/functions/symbol/symbols/ds.d.ts +2 -0
- 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/fermata.d.ts +2 -0
- package/dist/functions/symbol/symbols/fermata.js +24 -0
- package/dist/functions/symbol/symbols/fine.d.ts +2 -0
- package/dist/functions/symbol/symbols/fine.js +12 -0
- package/dist/functions/symbol/symbols/mordent.d.ts +2 -0
- package/dist/functions/symbol/symbols/mordent.js +56 -0
- 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.d.ts +2 -0
- package/dist/functions/symbol/symbols/tr.js +71 -0
- package/dist/functions/tempo/glyph.d.ts +12 -0
- package/dist/functions/tempo/glyph.js +30 -0
- package/dist/functions/tempo/index.js +57 -4
- package/dist/functions/temporal.d.ts +138 -0
- package/dist/functions/temporal.js +101 -0
- package/dist/functions/text/index.js +86 -27
- package/dist/functions/tie/index.js +136 -152
- package/dist/functions/tuplet/index.d.ts +2 -0
- package/dist/functions/tuplet/index.js +218 -0
- package/dist/functions/up/index.d.ts +1 -0
- package/dist/functions/up/index.js +258 -182
- package/dist/functions/voice/index.js +203 -109
- package/dist/functions/volta/index.d.ts +2 -0
- package/dist/functions/volta/index.js +230 -0
- 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 -17
- package/dist/index.js +3 -17
- package/dist/jpfun.core.min.js +153 -0
- package/dist/jpfun.from-midi.min.js +9 -0
- package/dist/jpfun.from-musicxml.min.js +12 -0
- package/dist/jpfun.min.js +166 -0
- package/dist/layout/default.js +2 -3
- package/dist/layout/engine.d.ts +6 -12
- package/dist/layout/engine.js +115 -64
- package/dist/layout/model.d.ts +12 -5
- package/dist/layout/model.js +31 -18
- package/dist/layout/page.js +5 -4
- package/dist/layout/path.d.ts +19 -0
- package/dist/layout/path.js +101 -0
- package/dist/layout/types.d.ts +71 -20
- package/dist/layout/types.js +3 -1
- package/dist/lowering/loweringContext.d.ts +20 -20
- package/dist/lowering/loweringContext.js +153 -101
- package/dist/lowering/track.d.ts +5 -9
- package/dist/lowering/track.js +7 -2
- package/dist/lowering/types.d.ts +26 -112
- package/dist/lowering/types.js +1 -92
- package/dist/parser/parse-utils/call-utils.d.ts +0 -2
- package/dist/parser/parse-utils/call-utils.js +0 -6
- package/dist/parser/parse-utils/note-utils.d.ts +8 -0
- package/dist/parser/parse-utils/note-utils.js +35 -13
- package/dist/parser/parse-utils/string-utils.d.ts +10 -0
- package/dist/parser/parse-utils/string-utils.js +24 -0
- package/dist/parser/parserContext.d.ts +11 -9
- package/dist/parser/parserContext.js +65 -55
- package/dist/parser/preprocess.js +17 -11
- package/dist/pipeline.d.ts +6 -5
- package/dist/pipeline.js +4 -5
- package/dist/playback/compile.d.ts +18 -0
- package/dist/playback/compile.js +414 -0
- package/dist/playback/event.d.ts +105 -0
- package/dist/playback/event.js +105 -0
- package/dist/playback/time.d.ts +14 -0
- package/dist/playback/time.js +81 -0
- package/dist/playback/types.d.ts +129 -0
- package/dist/playback/types.js +3 -0
- package/dist/render/canvas.d.ts +4 -4
- package/dist/render/canvas.js +20 -5
- package/dist/render/index.d.ts +1 -0
- package/dist/render/index.js +1 -0
- package/dist/render/paint.d.ts +8 -0
- package/dist/render/paint.js +101 -0
- package/dist/render/svg.d.ts +3 -3
- package/dist/render/svg.js +10 -8
- package/dist/render/text.d.ts +22 -0
- package/dist/render/text.js +42 -0
- package/dist/render/types.d.ts +1 -1
- package/grammar.md +132 -13
- package/package.json +19 -6
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
import { Diagnostic, ErrorDiagnostic, WarningDiagnostic } from "../../diagnostic.js";
|
|
2
|
+
import { isVisualTemporalNode, } from "../temporal.js";
|
|
3
|
+
import { ASTFunctionNode, } from "../ASTtypes.js";
|
|
4
|
+
class DynFunction extends ASTFunctionNode {
|
|
5
|
+
static def = {
|
|
6
|
+
name: ["dyn"],
|
|
7
|
+
description: "渐强渐弱",
|
|
8
|
+
example: `@dyn(from, to, dv) 在两个标签之间线性改变力度
|
|
9
|
+
from、to 必须位于同一音轨且时间不同
|
|
10
|
+
dv 为正时渐强,为负时渐弱;变化量叠加在每个音符原有力度上
|
|
11
|
+
|
|
12
|
+
1 ^ $p 2@a 3 4@b
|
|
13
|
+
@dyn(a, b, 24)`,
|
|
14
|
+
allowExtraArgs: false,
|
|
15
|
+
args: [
|
|
16
|
+
{ name: "from", type: "label", default: null },
|
|
17
|
+
{ name: "to", type: "label", default: null },
|
|
18
|
+
{ name: "dv", type: "number", default: null },
|
|
19
|
+
],
|
|
20
|
+
};
|
|
21
|
+
static loweringFinalize = (result) => {
|
|
22
|
+
const dynamics = result.attachments.filter((attachment) => attachment instanceof DynAttachment);
|
|
23
|
+
if (dynamics.length === 0)
|
|
24
|
+
return;
|
|
25
|
+
// astToTemporal 包含不进入全局 columns 的和弦、倚音成员;host 只用于统一时间和音轨
|
|
26
|
+
const records = [];
|
|
27
|
+
for (const nodes of result.astToTemporal.values()) {
|
|
28
|
+
for (const node of nodes) {
|
|
29
|
+
const velocity = node.playbackState?.velocity;
|
|
30
|
+
if (velocity === undefined)
|
|
31
|
+
continue;
|
|
32
|
+
let host = node;
|
|
33
|
+
while (host.foldedInto)
|
|
34
|
+
host = host.foldedInto;
|
|
35
|
+
records.push({ node, host, velocity, increment: 0 });
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
records.sort((left, right) => left.host.t.compare(right.host.t) || left.node.order - right.node.order);
|
|
39
|
+
// 先累计全部 dyn 的贡献,最后统一写回,避免声明顺序改变后续 dyn 读取的原始力度
|
|
40
|
+
for (const dynamic of dynamics) {
|
|
41
|
+
const endVelocity = dynamic.end.playbackState?.velocity
|
|
42
|
+
?? records.find(record => record.host === dynamic.toHost)?.velocity;
|
|
43
|
+
if (endVelocity === undefined)
|
|
44
|
+
throw new Error("@dyn end endpoint has no velocity-bearing member");
|
|
45
|
+
// 终点后保持完整增量,直到原始力度相对终点再次发生变化
|
|
46
|
+
const stop = records.find(record => record.host.track === dynamic.track
|
|
47
|
+
&& record.host.t.compare(dynamic.toHost.t) > 0
|
|
48
|
+
&& record.velocity !== endVelocity)?.host.t;
|
|
49
|
+
const duration = dynamic.toHost.t.clone().sub(dynamic.fromHost.t);
|
|
50
|
+
for (const record of records) {
|
|
51
|
+
if (record.host.track !== dynamic.track)
|
|
52
|
+
continue;
|
|
53
|
+
const fromStart = record.host.t.compare(dynamic.fromHost.t);
|
|
54
|
+
if (fromStart < 0 || (stop && record.host.t.compare(stop) >= 0))
|
|
55
|
+
continue;
|
|
56
|
+
const progress = record.host.t.compare(dynamic.toHost.t) >= 0
|
|
57
|
+
? 1
|
|
58
|
+
: record.host.t.clone().sub(dynamic.fromHost.t).div(duration).toNumber();
|
|
59
|
+
record.increment += dynamic.dv * progress;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
for (const record of records) {
|
|
63
|
+
if (record.increment === 0)
|
|
64
|
+
continue;
|
|
65
|
+
const velocity = Math.max(1, Math.min(127, record.velocity + record.increment));
|
|
66
|
+
record.node.playbackState = { ...record.node.playbackState, velocity };
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
from;
|
|
70
|
+
to;
|
|
71
|
+
dv;
|
|
72
|
+
size;
|
|
73
|
+
loweringEnter(ctx) {
|
|
74
|
+
const from = ctx.getTemporalNodes(this.from).at(0);
|
|
75
|
+
const to = ctx.getTemporalNodes(this.to).at(-1);
|
|
76
|
+
if (!from || !to) {
|
|
77
|
+
ctx.diagnostics.push(Diagnostic.warning.UnresolvedEndpoint("dyn", this.sourceSpan));
|
|
78
|
+
return [];
|
|
79
|
+
}
|
|
80
|
+
let fromHost = from;
|
|
81
|
+
let toHost = to;
|
|
82
|
+
while (fromHost.foldedInto)
|
|
83
|
+
fromHost = fromHost.foldedInto;
|
|
84
|
+
while (toHost.foldedInto)
|
|
85
|
+
toHost = toHost.foldedInto;
|
|
86
|
+
if (!isVisualTemporalNode(fromHost) || !isVisualTemporalNode(toHost)) {
|
|
87
|
+
ctx.diagnostics.push(Diagnostic.warning.UnresolvedEndpoint("dyn", this.sourceSpan));
|
|
88
|
+
return [];
|
|
89
|
+
}
|
|
90
|
+
if (fromHost.track !== toHost.track) {
|
|
91
|
+
throw new ErrorDiagnostic("E_DYN_CROSS_TRACK", "@dyn 的两个端点必须位于同一音轨", this.sourceSpan);
|
|
92
|
+
}
|
|
93
|
+
if (fromHost.t.equals(toHost.t)) {
|
|
94
|
+
throw new ErrorDiagnostic("E_DYN_ZERO_SPAN", "@dyn 的两个端点必须位于不同时间", this.sourceSpan);
|
|
95
|
+
}
|
|
96
|
+
if (fromHost.t.compare(toHost.t) < 0) {
|
|
97
|
+
ctx.addAttachment(new DynAttachment(this, to, fromHost, toHost));
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
ctx.addAttachment(new DynAttachment(this, from, toHost, fromHost));
|
|
101
|
+
}
|
|
102
|
+
return [];
|
|
103
|
+
}
|
|
104
|
+
constructor(span, args, ctx, parent = null) {
|
|
105
|
+
super(span, parent);
|
|
106
|
+
const [from, to, inputDv] = this.getArgValue(args, ctx);
|
|
107
|
+
let dv = inputDv;
|
|
108
|
+
if (!Number.isFinite(dv) || dv === 0) {
|
|
109
|
+
throw new ErrorDiagnostic("E_DYN_INVALID_DELTA", "@dyn 的 dv 必须是非零有限数", span);
|
|
110
|
+
}
|
|
111
|
+
dv = Math.max(-127, Math.min(127, dv));
|
|
112
|
+
if (dv !== inputDv) {
|
|
113
|
+
ctx.diagnostics.push(new WarningDiagnostic("W_DYN_DELTA_CLAMPED", `@dyn 的 dv 必须在 -127 到 127 之间,已被限制为 ${dv}`, span));
|
|
114
|
+
}
|
|
115
|
+
this.from = from;
|
|
116
|
+
this.to = to;
|
|
117
|
+
this.dv = dv;
|
|
118
|
+
this.size = ctx.variables.fontsize;
|
|
119
|
+
}
|
|
120
|
+
toString(source) {
|
|
121
|
+
const name = (node) => node.label ?? node.toString(source);
|
|
122
|
+
return `@dyn(${name(this.from)}, ${name(this.to)}, ${this.dv})`;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
export const DynNode = DynFunction;
|
|
126
|
+
class DynAttachment {
|
|
127
|
+
ast;
|
|
128
|
+
end;
|
|
129
|
+
fromHost;
|
|
130
|
+
toHost;
|
|
131
|
+
layer = "foreground";
|
|
132
|
+
constructor(ast, end, fromHost, toHost) {
|
|
133
|
+
this.ast = ast;
|
|
134
|
+
this.end = end;
|
|
135
|
+
this.fromHost = fromHost;
|
|
136
|
+
this.toHost = toHost;
|
|
137
|
+
}
|
|
138
|
+
get sourceSpan() { return this.ast.sourceSpan; }
|
|
139
|
+
get dv() { return this.ast.dv; }
|
|
140
|
+
get track() { return this.fromHost.track; }
|
|
141
|
+
createGeometry(context) {
|
|
142
|
+
const locate = (host) => {
|
|
143
|
+
const view = context.lines[host.layoutLine];
|
|
144
|
+
return { line: host.layoutLine, view, column: view?.columnOf(host) ?? -1 };
|
|
145
|
+
};
|
|
146
|
+
const from = locate(this.fromHost);
|
|
147
|
+
const to = locate(this.toHost);
|
|
148
|
+
if (!from.view || !to.view || from.column < 0 || to.column < 0) {
|
|
149
|
+
return { regions: [], paint() { } };
|
|
150
|
+
}
|
|
151
|
+
const ranges = [];
|
|
152
|
+
let size = this.ast.size;
|
|
153
|
+
for (let line = from.line; line <= to.line; line++) {
|
|
154
|
+
const view = context.lines[line];
|
|
155
|
+
const start = line === from.line ? from.column : 0;
|
|
156
|
+
const end = line === to.line ? to.column : view.columns.length - 1;
|
|
157
|
+
let firstColumn = -1;
|
|
158
|
+
let lastColumn = -1;
|
|
159
|
+
for (let column = start; column <= end; column++) {
|
|
160
|
+
for (const host of view.columns[column]) {
|
|
161
|
+
if (host.track !== this.track)
|
|
162
|
+
continue;
|
|
163
|
+
firstColumn = firstColumn < 0 ? column : firstColumn;
|
|
164
|
+
lastColumn = column;
|
|
165
|
+
size = Math.max(size, host.ast.size);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
if (firstColumn < 0)
|
|
169
|
+
continue;
|
|
170
|
+
ranges.push({
|
|
171
|
+
line,
|
|
172
|
+
startX: line === from.line ? this.fromHost.box.x : this.lineEntryX(context, line),
|
|
173
|
+
endX: line === to.line
|
|
174
|
+
? this.toHost.box.x + this.toHost.box.w
|
|
175
|
+
: this.lineRightX(context, line),
|
|
176
|
+
columns: [firstColumn, lastColumn],
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
// 允许左边超出一点
|
|
180
|
+
for (const range of ranges) {
|
|
181
|
+
if (range.line > from.line)
|
|
182
|
+
range.startX -= size / 4;
|
|
183
|
+
}
|
|
184
|
+
const strokeWidth = Math.max(0.75, size * 0.035);
|
|
185
|
+
const valid = ranges.filter(range => range.startX !== range.endX);
|
|
186
|
+
const total = valid.reduce((sum, range) => sum + Math.abs(range.endX - range.startX), 0);
|
|
187
|
+
if (total === 0)
|
|
188
|
+
return { regions: [], paint() { } };
|
|
189
|
+
const aperture = size * 0.4; // 最终开口高度
|
|
190
|
+
const halfStroke = strokeWidth / 2;
|
|
191
|
+
let covered = 0;
|
|
192
|
+
const segments = valid.map(range => {
|
|
193
|
+
const width = Math.abs(range.endX - range.startX);
|
|
194
|
+
const start = covered / total;
|
|
195
|
+
const end = (covered + width) / total;
|
|
196
|
+
covered += width;
|
|
197
|
+
const extent = context.getRangeExtents(range.line, range.columns).get(this.track);
|
|
198
|
+
const axis = context.getVisualAxis(range.line, this.track);
|
|
199
|
+
const hostTop = extent ? axis + extent.top : axis;
|
|
200
|
+
return {
|
|
201
|
+
line: range.line,
|
|
202
|
+
startX: range.startX,
|
|
203
|
+
endX: range.endX,
|
|
204
|
+
lineY: hostTop - size * 0.62,
|
|
205
|
+
startOpen: aperture * (this.dv > 0 ? start : 1 - start),
|
|
206
|
+
endOpen: aperture * (this.dv > 0 ? end : 1 - end),
|
|
207
|
+
};
|
|
208
|
+
});
|
|
209
|
+
return {
|
|
210
|
+
regions: segments.map(segment => ({
|
|
211
|
+
x: Math.min(segment.startX, segment.endX) - halfStroke,
|
|
212
|
+
y: segment.lineY - Math.max(segment.startOpen, segment.endOpen) / 2 - halfStroke,
|
|
213
|
+
w: Math.abs(segment.endX - segment.startX) + strokeWidth,
|
|
214
|
+
h: Math.max(segment.startOpen, segment.endOpen) + strokeWidth,
|
|
215
|
+
line: segment.line,
|
|
216
|
+
track: this.track,
|
|
217
|
+
})),
|
|
218
|
+
paint(painter) {
|
|
219
|
+
const style = { stroke: "#000", strokeWidth };
|
|
220
|
+
for (const segment of segments) {
|
|
221
|
+
painter.drawLine(segment.startX, segment.lineY - segment.startOpen / 2, segment.endX, segment.lineY - segment.endOpen / 2, style);
|
|
222
|
+
painter.drawLine(segment.startX, segment.lineY + segment.startOpen / 2, segment.endX, segment.lineY + segment.endOpen / 2, style);
|
|
223
|
+
}
|
|
224
|
+
},
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
lineEntryX(context, line) {
|
|
228
|
+
const host = context.lines[line].trackRuns.get(this.track)?.find(item => !item.T.isZero());
|
|
229
|
+
return host?.box.x ?? context.originX;
|
|
230
|
+
}
|
|
231
|
+
lineRightX(context, line) {
|
|
232
|
+
const runs = context.lines[line].trackRuns;
|
|
233
|
+
if (runs.size === 0)
|
|
234
|
+
return context.originX + context.width;
|
|
235
|
+
let right = context.originX;
|
|
236
|
+
for (const run of runs.values()) {
|
|
237
|
+
for (const host of run)
|
|
238
|
+
right = Math.max(right, host.box.x + host.box.w);
|
|
239
|
+
}
|
|
240
|
+
return right;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
@@ -1,15 +1,16 @@
|
|
|
1
1
|
import type { LayoutBox, LayoutPrepareContext } from "../../layout/types.js";
|
|
2
2
|
import type { LoweringContext } from "../../lowering/loweringContext.js";
|
|
3
3
|
import type { Track } from "../../lowering/track.js";
|
|
4
|
-
import { TemporalNodeBase, type VisualTemporalNode } from "
|
|
4
|
+
import { TemporalNodeBase, type TimeState, type VisualTemporalNode } from "../temporal.js";
|
|
5
5
|
import { GrammarNode, GrammarSugarNode } from "../../parser/grammarType.js";
|
|
6
6
|
import { ParserContext } from "../../parser/parserContext.js";
|
|
7
7
|
import type { Painter } from "../../render/types.js";
|
|
8
|
+
import type { PlaybackEmitter } from "../../playback/types.js";
|
|
8
9
|
import { ASTFunctionClass, ASTFunctionNode, ASTNodeBase, FunctionArgs, FunctionDef, SourceSpan } from "../ASTtypes.js";
|
|
9
10
|
type GraceSide = "pre" | "post";
|
|
10
11
|
declare class GraceFunction extends ASTFunctionNode {
|
|
11
12
|
static def: FunctionDef;
|
|
12
|
-
labelable():
|
|
13
|
+
labelable(): ASTFunctionNode | null;
|
|
13
14
|
static deSugarAtom(source: string, start: number, _end: number): {
|
|
14
15
|
next: number;
|
|
15
16
|
node: GrammarSugarNode;
|
|
@@ -41,22 +42,24 @@ export declare class GraceTemporal extends TemporalNodeBase {
|
|
|
41
42
|
box: LayoutBox;
|
|
42
43
|
readonly host: VisualTemporalNode;
|
|
43
44
|
readonly graces: readonly VisualTemporalNode[];
|
|
45
|
+
/** 承担节奏的倚音成员,按零时长标记切段;同段视觉上首尾相接,无条件连成一束减时线 */
|
|
46
|
+
readonly graceRuns: readonly (readonly VisualTemporalNode[])[];
|
|
44
47
|
readonly side: GraceSide;
|
|
45
48
|
/** 成员相对本盒左上角的局部偏移,onPlaced 时同步为绝对坐标 */
|
|
46
49
|
private hostOffset;
|
|
47
50
|
private graceOffsets;
|
|
48
51
|
/** 倚音线起点,同样是局部偏移 */
|
|
49
52
|
private hookOrigin;
|
|
50
|
-
private divLines;
|
|
51
53
|
constructor(ast: GraceFunction, host: VisualTemporalNode, graces: readonly VisualTemporalNode[], side: GraceSide);
|
|
52
|
-
|
|
54
|
+
/** 成员共享全局时间状态,按发声顺序固化 */
|
|
55
|
+
onTimeState(state: TimeState): void;
|
|
53
56
|
/**
|
|
54
57
|
* 播放时倚音从宿主借走的时值(前倚音从开头、后倚音从末尾)
|
|
55
58
|
*
|
|
56
59
|
* 比例 = 倚音字面总时值 / 四分音符,而四分音符 T=1,所以直接累加成员的 T。
|
|
57
60
|
* 基准取宿主经过 div 与 dot 后的实际时值;延时线是独立事件、tie 不改 T,天然不计入。
|
|
58
61
|
*/
|
|
59
|
-
|
|
62
|
+
emitPlayback(emitter: PlaybackEmitter): void;
|
|
60
63
|
/**
|
|
61
64
|
* 宿主留在轨道基线上,倚音整体抬到它的左上或右上角
|
|
62
65
|
*
|
|
@@ -64,12 +67,6 @@ export declare class GraceTemporal extends TemporalNodeBase {
|
|
|
64
67
|
* 因此左右邻居会被固有宽度推开,不会在空间紧张时被压穿。
|
|
65
68
|
*/
|
|
66
69
|
prepareLayout(context: LayoutPrepareContext): void;
|
|
67
|
-
/**
|
|
68
|
-
* 把相邻倚音成员的同级减时线连成整条
|
|
69
|
-
*
|
|
70
|
-
* 折叠成员不进入全局时间列,自动连梁看不到它们,只能由复合体自己连。
|
|
71
|
-
*/
|
|
72
|
-
private connectGraceDivLines;
|
|
73
70
|
/** 引擎每次改变本节点坐标后,按准备阶段保存的局部偏移重算成员绝对坐标 */
|
|
74
71
|
onPlaced(): void;
|
|
75
72
|
paint(painter: Painter): void;
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { ErrorDiagnostic } from "../../diagnostic.js";
|
|
2
2
|
import { prepareLayoutHost } from "../../layout/engine.js";
|
|
3
|
-
import {
|
|
3
|
+
import { Fraction } from "../../fraction.js";
|
|
4
|
+
import { isVisualTemporalNode, TemporalNodeBase, } from "../temporal.js";
|
|
4
5
|
import { ASTBraceNode, ASTFunctionNode, ASTLabelNode, ASTTextNode, } from "../ASTtypes.js";
|
|
5
|
-
import {
|
|
6
|
+
import { createBeamLayoutAttachment } from "../beam/layout.js";
|
|
7
|
+
import { DIV_ADDON_KEY } from "../div/index.js";
|
|
6
8
|
/** 倚音相对宿主的字号比例 */
|
|
7
9
|
const GRACE_SCALE = 0.7;
|
|
8
10
|
/**
|
|
@@ -55,7 +57,7 @@ class GraceFunction extends ASTFunctionNode {
|
|
|
55
57
|
}
|
|
56
58
|
],
|
|
57
59
|
};
|
|
58
|
-
labelable() { return this; }
|
|
60
|
+
labelable() { return ASTFunctionNode.findLabelable(this.host); }
|
|
59
61
|
static deSugarAtom(source, start, _end) {
|
|
60
62
|
let char = source[start];
|
|
61
63
|
switch (char) {
|
|
@@ -97,7 +99,9 @@ class GraceFunction extends ASTFunctionNode {
|
|
|
97
99
|
let replaceFrom = left;
|
|
98
100
|
// 对 label 的特判: 目标变为label到被标记的节点范围内的所有节点
|
|
99
101
|
if (leftNode instanceof ASTLabelNode) {
|
|
100
|
-
|
|
102
|
+
let tgt = leftNode.target;
|
|
103
|
+
while (tgt && !ctx.nodes.includes(tgt))
|
|
104
|
+
tgt = tgt.parent;
|
|
101
105
|
for (let j = left - 1; j >= 0; j--) {
|
|
102
106
|
if (ctx.nodes[j] === tgt) {
|
|
103
107
|
leftNode = new ASTBraceNode({
|
|
@@ -176,13 +180,13 @@ class GraceFunction extends ASTFunctionNode {
|
|
|
176
180
|
}
|
|
177
181
|
constructor(span, args, ctx, parent = null, scale = GRACE_SCALE) {
|
|
178
182
|
super(span, parent);
|
|
179
|
-
this.size = ctx.
|
|
183
|
+
this.size = ctx.variables.fontsize;
|
|
180
184
|
[this.host, this.grace, this.side] = this.getArgValue(args, ctx);
|
|
181
185
|
if (this.side !== "pre" && this.side !== "post") {
|
|
182
186
|
throw new ErrorDiagnostic("E_GRACE_INVALID_SIDE", "@grace 的 side 参数必须是 'pre' 或 'post'", span);
|
|
183
187
|
}
|
|
188
|
+
this.adopt(this.host);
|
|
184
189
|
this.setGrace(this.grace, scale);
|
|
185
|
-
this.size = ctx.fontSize;
|
|
186
190
|
}
|
|
187
191
|
setGrace(node, factor = GRACE_SCALE) {
|
|
188
192
|
scaleFontSize(node, factor);
|
|
@@ -207,7 +211,7 @@ class GraceFunction extends ASTFunctionNode {
|
|
|
207
211
|
? [this.grace, this.host]
|
|
208
212
|
: [this.host, this.grace];
|
|
209
213
|
for (const content of order) {
|
|
210
|
-
const events = ctx.trackedEvents(content,
|
|
214
|
+
const events = ctx.trackedEvents(content, new Fraction(), track).flat();
|
|
211
215
|
const visible = events.filter(isVisualTemporalNode);
|
|
212
216
|
// 并行分支的纵向关系由引擎解 Track 树,而折叠成员进不了引擎
|
|
213
217
|
if (events.some(event => event.track !== track))
|
|
@@ -226,7 +230,15 @@ class GraceFunction extends ASTFunctionNode {
|
|
|
226
230
|
}
|
|
227
231
|
}
|
|
228
232
|
});
|
|
229
|
-
|
|
233
|
+
const composite = new GraceTemporal(this, host, graces, this.side);
|
|
234
|
+
// 倚音同段无条件全部相连,只被零时长标记切断,所以 grace 一定认识 beam,允许耦合
|
|
235
|
+
// 折叠成员不进全局时间列,autobeam 看不到它们,要手动调用且强制连接
|
|
236
|
+
for (const run of composite.graceRuns) {
|
|
237
|
+
if (run.length < 2)
|
|
238
|
+
continue;
|
|
239
|
+
ctx.addAttachment(createBeamLayoutAttachment([...run], false, this.grace.sourceSpan));
|
|
240
|
+
}
|
|
241
|
+
return [composite];
|
|
230
242
|
}
|
|
231
243
|
toString(source) {
|
|
232
244
|
const host = this.host?.toString(source) ?? "";
|
|
@@ -245,29 +257,27 @@ const HOOK_COMMANDS = [
|
|
|
245
257
|
const HOOK_WIDTH = 0.07;
|
|
246
258
|
/** 倚音块底边到宿主顶边的视觉间隙;倚音线会探进这段空间指向宿主 */
|
|
247
259
|
const GRACE_RISE = 0.18;
|
|
248
|
-
/**
|
|
249
|
-
const MAX_STEAL_RATIO =
|
|
250
|
-
/** 减时线粗细占字号的比例 */
|
|
251
|
-
const DIV_LINE_WIDTH = 0.055;
|
|
260
|
+
/** 倪音向宿主借时值的上限,防止宿主被偷光;只读 */
|
|
261
|
+
const MAX_STEAL_RATIO = new Fraction(3, 4);
|
|
252
262
|
/** 倚音复合体:宿主与倚音都折叠在这一个盒子里,对外只是一个可见事件 */
|
|
253
263
|
export class GraceTemporal extends TemporalNodeBase {
|
|
254
264
|
host;
|
|
255
265
|
graces;
|
|
266
|
+
/** 承担节奏的倚音成员,按零时长标记切段;同段视觉上首尾相接,无条件连成一束减时线 */
|
|
267
|
+
graceRuns;
|
|
256
268
|
side;
|
|
257
269
|
/** 成员相对本盒左上角的局部偏移,onPlaced 时同步为绝对坐标 */
|
|
258
270
|
hostOffset = { x: 0, y: 0 };
|
|
259
271
|
graceOffsets = [];
|
|
260
272
|
/** 倚音线起点,同样是局部偏移 */
|
|
261
273
|
hookOrigin = null;
|
|
262
|
-
divLines = [];
|
|
263
274
|
constructor(ast, host, graces, side) {
|
|
264
275
|
super();
|
|
265
276
|
this.ast = ast;
|
|
266
277
|
this.host = host;
|
|
267
278
|
this.graces = graces;
|
|
268
279
|
this.side = side;
|
|
269
|
-
this.
|
|
270
|
-
this.T = host.T;
|
|
280
|
+
this.T.copyFrom(host.T);
|
|
271
281
|
this.mergeKey = host.mergeKey;
|
|
272
282
|
this.initLayoutBox();
|
|
273
283
|
// 宿主携带整体节奏,它的修饰暂时成为复合体的修饰,自动连梁才看得到
|
|
@@ -276,20 +286,39 @@ export class GraceTemporal extends TemporalNodeBase {
|
|
|
276
286
|
host.addon = void 0;
|
|
277
287
|
// 成员不进入全局 columns,对外由复合体代表:写在成员上的标签仍可做关系端点
|
|
278
288
|
host.foldedInto = this;
|
|
289
|
+
const runs = [];
|
|
290
|
+
let run = [];
|
|
279
291
|
for (const grace of graces) {
|
|
280
292
|
grace.foldedInto = this;
|
|
293
|
+
// 调号、速度这类零时长标记不承担节奏,还会在视觉上把倚音串切断
|
|
294
|
+
if (grace.T.isZero()) {
|
|
295
|
+
if (run.length > 0)
|
|
296
|
+
runs.push(run);
|
|
297
|
+
run = [];
|
|
298
|
+
continue;
|
|
299
|
+
}
|
|
300
|
+
run.push(grace);
|
|
281
301
|
// 倚音默认就是八分音符:补一条减时线,书面时值随之减半
|
|
282
302
|
const addon = grace.addon = { ...grace.addon };
|
|
283
303
|
addon[DIV_ADDON_KEY] = (Number(addon[DIV_ADDON_KEY]) || 0) + 1;
|
|
284
|
-
grace.T
|
|
304
|
+
grace.T.divPow2();
|
|
285
305
|
}
|
|
306
|
+
if (run.length > 0)
|
|
307
|
+
runs.push(run);
|
|
308
|
+
this.graceRuns = runs;
|
|
286
309
|
}
|
|
310
|
+
/** 成员共享全局时间状态,按发声顺序固化 */
|
|
287
311
|
onTimeState(state) {
|
|
288
|
-
|
|
289
|
-
|
|
312
|
+
// 同步时间的修改。比如tuplet修改的是GraceTemporal.T,这里要把它传给成员
|
|
313
|
+
this.host.T.copyFrom(this.T);
|
|
314
|
+
const members = this.side === "pre"
|
|
315
|
+
? [...this.graces, this.host]
|
|
316
|
+
: [this.host, ...this.graces];
|
|
317
|
+
for (const member of members) {
|
|
318
|
+
member.t.copyFrom(this.t);
|
|
290
319
|
member.track = this.track;
|
|
291
320
|
member.layoutLine = this.layoutLine;
|
|
292
|
-
member.onTimeState?.(
|
|
321
|
+
member.onTimeState?.(state);
|
|
293
322
|
}
|
|
294
323
|
}
|
|
295
324
|
/**
|
|
@@ -298,9 +327,35 @@ export class GraceTemporal extends TemporalNodeBase {
|
|
|
298
327
|
* 比例 = 倚音字面总时值 / 四分音符,而四分音符 T=1,所以直接累加成员的 T。
|
|
299
328
|
* 基准取宿主经过 div 与 dot 后的实际时值;延时线是独立事件、tie 不改 T,天然不计入。
|
|
300
329
|
*/
|
|
301
|
-
|
|
302
|
-
const written =
|
|
303
|
-
|
|
330
|
+
emitPlayback(emitter) {
|
|
331
|
+
const written = new Fraction();
|
|
332
|
+
for (const grace of this.graces)
|
|
333
|
+
written.add(grace.T);
|
|
334
|
+
const ratio = written.compare(MAX_STEAL_RATIO) > 0 ? MAX_STEAL_RATIO : written;
|
|
335
|
+
const total = emitter.end.clone().sub(emitter.start);
|
|
336
|
+
const steal = total.clone().mul(ratio);
|
|
337
|
+
const hostDuration = total.sub(steal);
|
|
338
|
+
const start = emitter.start.clone();
|
|
339
|
+
const playGraces = (cursor) => {
|
|
340
|
+
for (const grace of this.graces) {
|
|
341
|
+
if (grace.T.isZero() || written.isZero()) {
|
|
342
|
+
emitter.play(grace, cursor, new Fraction());
|
|
343
|
+
continue;
|
|
344
|
+
}
|
|
345
|
+
const duration = steal.clone().mul(grace.T).div(written);
|
|
346
|
+
emitter.play(grace, cursor, duration);
|
|
347
|
+
cursor.add(duration);
|
|
348
|
+
}
|
|
349
|
+
};
|
|
350
|
+
if (this.side === "pre") {
|
|
351
|
+
const cursor = start.clone();
|
|
352
|
+
playGraces(cursor);
|
|
353
|
+
emitter.play(this.host, cursor, hostDuration);
|
|
354
|
+
}
|
|
355
|
+
else {
|
|
356
|
+
emitter.play(this.host, start, hostDuration);
|
|
357
|
+
playGraces(start.clone().add(hostDuration));
|
|
358
|
+
}
|
|
304
359
|
}
|
|
305
360
|
/**
|
|
306
361
|
* 宿主留在轨道基线上,倚音整体抬到它的左上或右上角
|
|
@@ -366,60 +421,15 @@ export class GraceTemporal extends TemporalNodeBase {
|
|
|
366
421
|
}
|
|
367
422
|
// 宿主没有肩线时它自己的盒顶就是肩线;有则上面那轮转发已经带偏移抬好了
|
|
368
423
|
this.ports[SHOULDER_PORT] ??= { x: this.box.anchor, y: this.hostOffset.y };
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
this.hookOrigin =
|
|
374
|
-
x: this.graceOffsets[near].x +
|
|
424
|
+
// 钩形曲线挂在靠近宿主的那个倚音成员下方,零时长标记不承接它
|
|
425
|
+
const near = this.side === "pre"
|
|
426
|
+
? this.graceRuns.at(-1)?.at(-1)
|
|
427
|
+
: this.graceRuns[0]?.[0];
|
|
428
|
+
this.hookOrigin = near ? {
|
|
429
|
+
x: this.graceOffsets[this.graces.indexOf(near)].x + near.box.anchor,
|
|
375
430
|
y: graceTop + lift + graceHeight,
|
|
376
431
|
} : null;
|
|
377
432
|
}
|
|
378
|
-
/**
|
|
379
|
-
* 把相邻倚音成员的同级减时线连成整条
|
|
380
|
-
*
|
|
381
|
-
* 折叠成员不进入全局时间列,自动连梁看不到它们,只能由复合体自己连。
|
|
382
|
-
*/
|
|
383
|
-
connectGraceDivLines() {
|
|
384
|
-
this.divLines.length = 0;
|
|
385
|
-
if (this.graces.length < 2)
|
|
386
|
-
return;
|
|
387
|
-
for (let level = 0;; level++) {
|
|
388
|
-
let hasPorts = false;
|
|
389
|
-
let run = [];
|
|
390
|
-
const flush = () => {
|
|
391
|
-
if (run.length >= 2) {
|
|
392
|
-
const first = run[0];
|
|
393
|
-
const last = run[run.length - 1];
|
|
394
|
-
const leftPort = this.graces[first].ports[divLinePortName(level, "left")];
|
|
395
|
-
const rightPort = this.graces[last].ports[divLinePortName(level, "right")];
|
|
396
|
-
this.divLines.push({
|
|
397
|
-
x1: this.graceOffsets[first].x + leftPort.x,
|
|
398
|
-
x2: this.graceOffsets[last].x + rightPort.x,
|
|
399
|
-
y: this.graceOffsets[first].y + leftPort.y,
|
|
400
|
-
});
|
|
401
|
-
for (const index of run)
|
|
402
|
-
claimDivLine(this.graces[index], level);
|
|
403
|
-
}
|
|
404
|
-
run = [];
|
|
405
|
-
};
|
|
406
|
-
for (let i = 0; i < this.graces.length; i++) {
|
|
407
|
-
const ports = this.graces[i].ports;
|
|
408
|
-
const left = ports[divLinePortName(level, "left")];
|
|
409
|
-
const right = ports[divLinePortName(level, "right")];
|
|
410
|
-
if (left || right)
|
|
411
|
-
hasPorts = true;
|
|
412
|
-
if (!left || !right) {
|
|
413
|
-
flush();
|
|
414
|
-
continue;
|
|
415
|
-
}
|
|
416
|
-
run.push(i);
|
|
417
|
-
}
|
|
418
|
-
flush();
|
|
419
|
-
if (!hasPorts)
|
|
420
|
-
break;
|
|
421
|
-
}
|
|
422
|
-
}
|
|
423
433
|
/** 引擎每次改变本节点坐标后,按准备阶段保存的局部偏移重算成员绝对坐标 */
|
|
424
434
|
onPlaced() {
|
|
425
435
|
this.host.box.x = this.box.x + this.hostOffset.x;
|
|
@@ -437,14 +447,9 @@ export class GraceTemporal extends TemporalNodeBase {
|
|
|
437
447
|
for (const decoration of member.decorations)
|
|
438
448
|
decoration.paint(painter);
|
|
439
449
|
}
|
|
440
|
-
const graceEm = this.ast.size * GRACE_SCALE;
|
|
441
|
-
const divWidth = Math.max(0.8, graceEm * DIV_LINE_WIDTH);
|
|
442
|
-
for (const line of this.divLines) {
|
|
443
|
-
const y = this.box.y + line.y;
|
|
444
|
-
painter.drawLine(this.box.x + line.x1, y, this.box.x + line.x2, y, { stroke: "#000", strokeWidth: divWidth });
|
|
445
|
-
}
|
|
446
450
|
if (!this.hookOrigin)
|
|
447
451
|
return;
|
|
452
|
+
const graceEm = this.ast.size * GRACE_SCALE;
|
|
448
453
|
const hookOrigin = this.hookOrigin;
|
|
449
454
|
const dir = this.side === "pre" ? 1 : -1;
|
|
450
455
|
painter.drawPath(HOOK_COMMANDS, { stroke: "#000", strokeWidth: Math.max(0.8, graceEm * HOOK_WIDTH) }, {
|