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
|
@@ -1,2 +1,10 @@
|
|
|
1
1
|
import { ASTFunctionClass } from "../ASTtypes.js";
|
|
2
|
+
import type { PlaybackCursor } from "../../playback/types.js";
|
|
2
3
|
export declare const BarNode: ASTFunctionClass;
|
|
4
|
+
/**
|
|
5
|
+
* 某一列此刻处在所属反复段的第几遍
|
|
6
|
+
*
|
|
7
|
+
* 遍数是回跳的直接后果,所以和跳转规则一样归小节线所有;房子只消费这个数。
|
|
8
|
+
* 没有 `|:` 时整首曲子就是一段。
|
|
9
|
+
*/
|
|
10
|
+
export declare function repeatPass(cursor: PlaybackCursor, column: number): number;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ASTFunctionNode } from "../ASTtypes.js";
|
|
2
|
-
import { ANCHOR_KEY, TemporalNodeBase } from "
|
|
2
|
+
import { ANCHOR_KEY, isVisualTemporalNode, TemporalNodeBase } from "../temporal.js";
|
|
3
3
|
class BarFunction extends ASTFunctionNode {
|
|
4
4
|
static def = {
|
|
5
5
|
name: ["bar", "|"],
|
|
@@ -23,7 +23,7 @@ class BarFunction extends ASTFunctionNode {
|
|
|
23
23
|
name: "length",
|
|
24
24
|
type: "length",
|
|
25
25
|
default: {
|
|
26
|
-
value: 1,
|
|
26
|
+
value: 1.25,
|
|
27
27
|
unit: "em",
|
|
28
28
|
},
|
|
29
29
|
},
|
|
@@ -61,20 +61,18 @@ class BarFunction extends ASTFunctionNode {
|
|
|
61
61
|
}
|
|
62
62
|
;
|
|
63
63
|
type;
|
|
64
|
-
barLength; // 固化值
|
|
65
64
|
size;
|
|
66
65
|
constructor(sourceSpan, args, ctx, parent = null) {
|
|
67
66
|
super(sourceSpan, parent);
|
|
68
|
-
const [type,
|
|
67
|
+
const [type, size] = this.getArgValue(args, ctx);
|
|
69
68
|
this.type = type;
|
|
70
|
-
this.
|
|
71
|
-
this.size = ctx.fontSize;
|
|
69
|
+
this.size = ctx.length2px(size);
|
|
72
70
|
}
|
|
73
71
|
loweringEnter() {
|
|
74
|
-
return [new BarTemporalNode(this)];
|
|
72
|
+
return [this.type >= 2 ? new RepeatBarTemporalNode(this) : new BarTemporalNode(this)];
|
|
75
73
|
}
|
|
76
74
|
toString() {
|
|
77
|
-
return `@bar(${this.type}, ${this.
|
|
75
|
+
return `@bar(${this.type}, ${this.size}px)`;
|
|
78
76
|
}
|
|
79
77
|
}
|
|
80
78
|
export const BarNode = BarFunction;
|
|
@@ -84,7 +82,6 @@ class BarTemporalNode extends TemporalNodeBase {
|
|
|
84
82
|
constructor(ast) {
|
|
85
83
|
super();
|
|
86
84
|
this.ast = ast;
|
|
87
|
-
this.T = 0;
|
|
88
85
|
this.mergeKey = ANCHOR_KEY;
|
|
89
86
|
this.initLayoutBox();
|
|
90
87
|
// 由于有最小时长,因此设置alpha依然有效
|
|
@@ -97,12 +94,17 @@ class BarTemporalNode extends TemporalNodeBase {
|
|
|
97
94
|
* 给 bar 两侧的相邻元素增加 mu,避免被压缩到一起
|
|
98
95
|
*/
|
|
99
96
|
prepareHorizontal(line) {
|
|
100
|
-
|
|
101
|
-
|
|
97
|
+
let host = this;
|
|
98
|
+
while (host.foldedInto)
|
|
99
|
+
host = host.foldedInto;
|
|
100
|
+
if (!isVisualTemporalNode(host))
|
|
101
|
+
return;
|
|
102
|
+
const run = line.trackRuns.get(host.track);
|
|
103
|
+
const index = run?.indexOf(host) ?? -1;
|
|
102
104
|
if (!run || index < 0)
|
|
103
105
|
return;
|
|
104
|
-
line.registerHorizontalLayoutHook(
|
|
105
|
-
const current = columns[start].find(element => element.time ===
|
|
106
|
+
line.registerHorizontalLayoutHook(host, host, ({ columns, start }) => {
|
|
107
|
+
const current = columns[start].find(element => element.time === host);
|
|
106
108
|
if (!current)
|
|
107
109
|
return;
|
|
108
110
|
const elementOf = (node) => {
|
|
@@ -117,17 +119,17 @@ class BarTemporalNode extends TemporalNodeBase {
|
|
|
117
119
|
const rightNode = run[index + 1];
|
|
118
120
|
const right = rightNode ? elementOf(rightNode) : undefined;
|
|
119
121
|
// 相邻两个 bar 的中间 gap 由右侧 bar 处理,避免重复增强
|
|
120
|
-
if (right &&
|
|
122
|
+
if (right && rightNode.mergeKey !== ANCHOR_KEY) {
|
|
121
123
|
current.config.mu_R *= 4;
|
|
122
124
|
right.config.mu_L *= 4;
|
|
123
125
|
}
|
|
124
126
|
});
|
|
125
127
|
}
|
|
126
128
|
prepareLayout() {
|
|
127
|
-
const { type,
|
|
129
|
+
const { type, size } = this.ast;
|
|
128
130
|
this.lines.length = 0;
|
|
129
|
-
|
|
130
|
-
const h =
|
|
131
|
+
const repeatDots = [];
|
|
132
|
+
const h = size;
|
|
131
133
|
const thin = Math.max(1, size * 0.065);
|
|
132
134
|
const thick = thin * 2.6;
|
|
133
135
|
const gap = thin * 1.8;
|
|
@@ -154,27 +156,62 @@ class BarTemporalNode extends TemporalNodeBase {
|
|
|
154
156
|
}
|
|
155
157
|
if (hasLeftDots) {
|
|
156
158
|
const dotX = dotRadius;
|
|
157
|
-
|
|
158
|
-
|
|
159
|
+
repeatDots.push({ x: dotX, y: h * 0.38 });
|
|
160
|
+
repeatDots.push({ x: dotX, y: h * 0.62 });
|
|
159
161
|
}
|
|
160
162
|
if (hasRightDots) {
|
|
161
163
|
const dotX = x + dotGap;
|
|
162
|
-
|
|
163
|
-
|
|
164
|
+
repeatDots.push({ x: dotX, y: h * 0.38 });
|
|
165
|
+
repeatDots.push({ x: dotX, y: h * 0.62 });
|
|
164
166
|
x = dotX + dotRadius;
|
|
165
167
|
}
|
|
166
168
|
this.box.w = x;
|
|
167
169
|
this.box.h = h;
|
|
168
|
-
|
|
170
|
+
const anchorLine = type >= 2 ? this.lines[0] : undefined;
|
|
171
|
+
this.box.anchor = anchorLine ? anchorLine.x + anchorLine.w / 2 : x / 2;
|
|
169
172
|
this.box.visualAxis = h / 2;
|
|
173
|
+
for (const line of this.lines)
|
|
174
|
+
line.x -= this.box.anchor;
|
|
175
|
+
this.repeatDots = repeatDots.map(dot => ({ x: dot.x - this.box.anchor, y: dot.y, r: dotRadius }));
|
|
170
176
|
}
|
|
171
177
|
paint(painter) {
|
|
172
178
|
for (const line of this.lines) {
|
|
173
|
-
painter.drawRect(this.box.x + line.x, this.box.y, line.w, this.box.h, { fill: "#000" });
|
|
179
|
+
painter.drawRect(this.box.x + this.box.anchor + line.x, this.box.y, line.w, this.box.h, { fill: "#000" });
|
|
174
180
|
}
|
|
175
|
-
const radius = Math.max(1, this.box.w * 0.06);
|
|
176
181
|
for (const dot of this.repeatDots) {
|
|
177
|
-
painter.drawCircle(this.box.x + dot.x, this.box.y + dot.y,
|
|
182
|
+
painter.drawCircle(this.box.x + this.box.anchor + dot.x, this.box.y + dot.y, dot.r, { fill: "#000" });
|
|
178
183
|
}
|
|
179
184
|
}
|
|
180
185
|
}
|
|
186
|
+
/** 反复段起点标记:`:|` 靠它找回跳目标,遍数也按它计数 */
|
|
187
|
+
const REPEAT_START = "repeat.start";
|
|
188
|
+
/**
|
|
189
|
+
* 某一列此刻处在所属反复段的第几遍
|
|
190
|
+
*
|
|
191
|
+
* 遍数是回跳的直接后果,所以和跳转规则一样归小节线所有;房子只消费这个数。
|
|
192
|
+
* 没有 `|:` 时整首曲子就是一段。
|
|
193
|
+
*/
|
|
194
|
+
export function repeatPass(cursor, column) {
|
|
195
|
+
return cursor.visits(cursor.seek(REPEAT_START, column + 1, -1) ?? 0);
|
|
196
|
+
}
|
|
197
|
+
/** 只有反复线参与播放顺序;普通小节线太多,进控制流扫描是纯开销 */
|
|
198
|
+
class RepeatBarTemporalNode extends BarTemporalNode {
|
|
199
|
+
/** 只在 type>=2 时创建,所以剩下的三种是:2 段首、3 段尾、4 两者都是 */
|
|
200
|
+
playbackMarks() {
|
|
201
|
+
return this.ast.type === 3 ? [] : [REPEAT_START];
|
|
202
|
+
}
|
|
203
|
+
/** 每条反复线只回跳一次,所以连写几条就是几遍 */
|
|
204
|
+
playbackFlow(columnOf) {
|
|
205
|
+
if (this.ast.type === 2)
|
|
206
|
+
return;
|
|
207
|
+
const at = columnOf(this);
|
|
208
|
+
if (at === undefined)
|
|
209
|
+
return;
|
|
210
|
+
return {
|
|
211
|
+
range: [at, at],
|
|
212
|
+
run: cursor => cursor.visits(at) > 1
|
|
213
|
+
? undefined
|
|
214
|
+
: { kind: "jump", column: cursor.seek(REPEAT_START, at, -1) ?? 0 },
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { LoweringResult } from "../../lowering/types.js";
|
|
2
2
|
import type { LayoutAttachment } from "../../layout/types.js";
|
|
3
3
|
/** beam 域在 lowering 固化后统一生成作用域连接和相邻自动连接 */
|
|
4
4
|
export declare function createAutomaticBeamAttachments(result: LoweringResult): LayoutAttachment[];
|
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import { ANCHOR_KEY, isVisualTemporalNode, } from "
|
|
1
|
+
import { ANCHOR_KEY, isVisualTemporalNode, } from "../temporal.js";
|
|
2
|
+
import { Fraction } from "../../fraction.js";
|
|
2
3
|
import { DIV_ADDON_KEY, DivNode, } from "../div/index.js";
|
|
3
4
|
import { collectExplicitBeamEndpoints, createBeamLayoutAttachment, } from "./layout.js";
|
|
4
|
-
const QUARTER_NOTE_DURATION = 1;
|
|
5
|
-
const TIME_EPSILON = 1e-6;
|
|
6
5
|
/** 只返回实际参与当前事件 div addon 的 div,先沿祖先链找,不够再按书写顺序向下补 */
|
|
7
6
|
function findActiveDivs(node) {
|
|
8
7
|
const result = [];
|
|
@@ -72,6 +71,22 @@ function outermostScopes(scopes) {
|
|
|
72
71
|
return true;
|
|
73
72
|
});
|
|
74
73
|
}
|
|
74
|
+
function sourceSpanForAutoBeam(nodes) {
|
|
75
|
+
let start = Infinity;
|
|
76
|
+
let end = -Infinity;
|
|
77
|
+
for (const node of nodes) {
|
|
78
|
+
start = Math.min(start, node.ast.sourceSpan.start);
|
|
79
|
+
end = Math.max(end, node.ast.sourceSpan.end);
|
|
80
|
+
for (const div of findActiveDivs(node)) {
|
|
81
|
+
start = Math.min(start, div.sourceSpan.start);
|
|
82
|
+
end = Math.max(end, div.sourceSpan.end);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return { start, end };
|
|
86
|
+
}
|
|
87
|
+
function createAutoBeam(nodes) {
|
|
88
|
+
return createBeamLayoutAttachment(nodes, false, sourceSpanForAutoBeam(nodes));
|
|
89
|
+
}
|
|
75
90
|
/** 同一个 div 内部始终按轨道连接,手工 beam 端点会截断自动片段 */
|
|
76
91
|
function createScopeBeams(scopes, explicitEndpoints) {
|
|
77
92
|
const attachments = [];
|
|
@@ -88,7 +103,7 @@ function createScopeBeams(scopes, explicitEndpoints) {
|
|
|
88
103
|
let segment = [];
|
|
89
104
|
const flush = () => {
|
|
90
105
|
if (segment.length >= 2)
|
|
91
|
-
attachments.push(
|
|
106
|
+
attachments.push(createAutoBeam(segment));
|
|
92
107
|
segment = [];
|
|
93
108
|
};
|
|
94
109
|
for (const node of nodes) {
|
|
@@ -104,7 +119,7 @@ function createScopeBeams(scopes, explicitEndpoints) {
|
|
|
104
119
|
return attachments;
|
|
105
120
|
}
|
|
106
121
|
function beatAt(time) {
|
|
107
|
-
return Math.floor(
|
|
122
|
+
return Math.floor(time.numerator / time.denominator);
|
|
108
123
|
}
|
|
109
124
|
/**
|
|
110
125
|
* 多个独立 div 的默认连接规则
|
|
@@ -117,11 +132,12 @@ function createAdjacentBeams(result, nodeScopes, explicitEndpoints) {
|
|
|
117
132
|
const attachments = [];
|
|
118
133
|
const groups = new Map();
|
|
119
134
|
const lineStarts = new Map();
|
|
120
|
-
|
|
135
|
+
const measureStart = new Fraction();
|
|
136
|
+
const beatTime = new Fraction();
|
|
121
137
|
for (const column of result.columns) {
|
|
122
138
|
for (const node of column) {
|
|
123
139
|
const current = lineStarts.get(node.layoutLine);
|
|
124
|
-
if (current === undefined || node.t <
|
|
140
|
+
if (current === undefined || node.t.compare(current) < 0)
|
|
125
141
|
lineStarts.set(node.layoutLine, node.t);
|
|
126
142
|
}
|
|
127
143
|
}
|
|
@@ -131,7 +147,7 @@ function createAdjacentBeams(result, nodeScopes, explicitEndpoints) {
|
|
|
131
147
|
return;
|
|
132
148
|
groups.delete(track);
|
|
133
149
|
if (group.nodes.length >= 2)
|
|
134
|
-
attachments.push(
|
|
150
|
+
attachments.push(createAutoBeam(group.nodes));
|
|
135
151
|
};
|
|
136
152
|
const flushAll = () => {
|
|
137
153
|
for (const track of [...groups.keys()])
|
|
@@ -141,7 +157,7 @@ function createAdjacentBeams(result, nodeScopes, explicitEndpoints) {
|
|
|
141
157
|
const anchor = column.find(node => node.mergeKey === ANCHOR_KEY);
|
|
142
158
|
if (anchor) {
|
|
143
159
|
flushAll();
|
|
144
|
-
measureStart
|
|
160
|
+
measureStart.copyFrom(anchor.t);
|
|
145
161
|
}
|
|
146
162
|
for (const node of column) {
|
|
147
163
|
if (node.mergeKey === ANCHOR_KEY)
|
|
@@ -152,40 +168,41 @@ function createAdjacentBeams(result, nodeScopes, explicitEndpoints) {
|
|
|
152
168
|
const isCandidate = isVisualTemporalNode(node)
|
|
153
169
|
&& isIndependentDiv
|
|
154
170
|
&& autoBeamEnabled
|
|
155
|
-
&& node.T <
|
|
171
|
+
&& node.T.compare(1) < 0
|
|
156
172
|
&& !explicitEndpoints.has(node);
|
|
157
173
|
if (!isCandidate) {
|
|
158
174
|
// 同轨可见元素必须打断相邻关系,即使它本身是零时长文本
|
|
159
175
|
// 不可见的 set/key/tempo 等零时长状态不会打断
|
|
160
|
-
if (isVisualTemporalNode(node) || node.T
|
|
176
|
+
if (isVisualTemporalNode(node) || !node.T.isZero())
|
|
161
177
|
flush(node.track);
|
|
162
178
|
continue;
|
|
163
179
|
}
|
|
164
180
|
const lineStart = lineStarts.get(node.layoutLine) ?? node.t;
|
|
165
|
-
const
|
|
181
|
+
const activeStart = measureStart.compare(lineStart) >= 0 ? measureStart : lineStart;
|
|
182
|
+
const beat = beatAt(beatTime.copyFrom(node.t).sub(activeStart));
|
|
166
183
|
const current = groups.get(node.track);
|
|
167
184
|
if (!current) {
|
|
168
185
|
groups.set(node.track, {
|
|
169
186
|
nodes: [node],
|
|
170
187
|
line: node.layoutLine,
|
|
171
188
|
beat,
|
|
172
|
-
endTime: node.t
|
|
189
|
+
endTime: node.t.clone().add(node.T),
|
|
173
190
|
});
|
|
174
191
|
continue;
|
|
175
192
|
}
|
|
176
|
-
const isContinuous =
|
|
193
|
+
const isContinuous = node.t.equals(current.endTime);
|
|
177
194
|
if (node.layoutLine !== current.line || beat !== current.beat || !isContinuous) {
|
|
178
195
|
flush(node.track);
|
|
179
196
|
groups.set(node.track, {
|
|
180
197
|
nodes: [node],
|
|
181
198
|
line: node.layoutLine,
|
|
182
199
|
beat,
|
|
183
|
-
endTime: node.t
|
|
200
|
+
endTime: node.t.clone().add(node.T),
|
|
184
201
|
});
|
|
185
202
|
continue;
|
|
186
203
|
}
|
|
187
204
|
current.nodes.push(node);
|
|
188
|
-
current.endTime
|
|
205
|
+
current.endTime.copyFrom(node.t).add(node.T);
|
|
189
206
|
}
|
|
190
207
|
}
|
|
191
208
|
flushAll();
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ASTFunctionNode } from "../ASTtypes.js";
|
|
2
|
-
import { ErrorDiagnostic } from "../../diagnostic.js";
|
|
2
|
+
import { Diagnostic, ErrorDiagnostic } from "../../diagnostic.js";
|
|
3
3
|
import { createAutomaticBeamAttachments } from "./auto.js";
|
|
4
4
|
import { createBeamLayoutAttachment, validateExplicitBeamAttachments, } from "./layout.js";
|
|
5
5
|
class BeamFunction extends ASTFunctionNode {
|
|
@@ -19,7 +19,7 @@ class BeamFunction extends ASTFunctionNode {
|
|
|
19
19
|
for (const [, value] of args) {
|
|
20
20
|
const parsed = value instanceof ASTFunctionNode
|
|
21
21
|
? value
|
|
22
|
-
: ctx.parseArgWithType(value.
|
|
22
|
+
: ctx.parseArgWithType(value.valueSpan, "label", sourceSpan.start);
|
|
23
23
|
if (parsed instanceof ASTFunctionNode)
|
|
24
24
|
this.endPoints.push(parsed);
|
|
25
25
|
}
|
|
@@ -27,7 +27,7 @@ class BeamFunction extends ASTFunctionNode {
|
|
|
27
27
|
let k = ctx.labelableNodes.length - 1;
|
|
28
28
|
while (this.endPoints.length < 2 && k >= 0) {
|
|
29
29
|
const candidate = ctx.labelableNodes[k--];
|
|
30
|
-
if (this.endPoints.includes(candidate))
|
|
30
|
+
if (!candidate || this.endPoints.includes(candidate))
|
|
31
31
|
continue;
|
|
32
32
|
this.endPoints.unshift(candidate);
|
|
33
33
|
}
|
|
@@ -40,14 +40,18 @@ class BeamFunction extends ASTFunctionNode {
|
|
|
40
40
|
const endPoints = [];
|
|
41
41
|
for (const ast of this.endPoints) {
|
|
42
42
|
// 暂时只用最后一个;理应有且仅有一个
|
|
43
|
-
|
|
43
|
+
let temporal = ctx.getTemporalNodes(ast).at(-1);
|
|
44
|
+
while (temporal?.foldedInto)
|
|
45
|
+
temporal = temporal.foldedInto;
|
|
44
46
|
if (!temporal)
|
|
45
47
|
continue;
|
|
46
48
|
endPoints.push(temporal);
|
|
47
49
|
}
|
|
48
|
-
if (endPoints.length < 2)
|
|
50
|
+
if (endPoints.length < 2) {
|
|
51
|
+
ctx.diagnostics.push(Diagnostic.warning.UnresolvedEndpoint("beam", this.sourceSpan));
|
|
49
52
|
return [];
|
|
50
|
-
|
|
53
|
+
}
|
|
54
|
+
ctx.addAttachment(createBeamLayoutAttachment(endPoints, true, this.sourceSpan));
|
|
51
55
|
return [];
|
|
52
56
|
}
|
|
53
57
|
}
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import { type
|
|
1
|
+
import { type TemporalNodeBase } from "../temporal.js";
|
|
2
2
|
import type { SourceSpan } from "../../parser/types.js";
|
|
3
3
|
import type { LayoutAttachment } from "../../layout/types.js";
|
|
4
|
+
import type { LoweringAttachment, LoweringResult } from "../../lowering/types.js";
|
|
4
5
|
/** 显式和自动分组共享同一个几何对象 */
|
|
5
|
-
export declare function createBeamLayoutAttachment(endPoints: TemporalNodeBase[], explicit
|
|
6
|
+
export declare function createBeamLayoutAttachment(endPoints: TemporalNodeBase[], explicit: boolean, sourceSpan: SourceSpan): LayoutAttachment;
|
|
6
7
|
/** 显式 beam 必须严格连接最终同轨同谱面行中的相邻可见主体 */
|
|
7
8
|
export declare function validateExplicitBeamAttachments(result: LoweringResult): void;
|
|
8
9
|
/** 自动分组在 loweringAugment 当下读取已经注册的显式 beam 端点 */
|
|
9
|
-
export declare function collectExplicitBeamEndpoints(attachments: readonly
|
|
10
|
+
export declare function collectExplicitBeamEndpoints(attachments: readonly LoweringAttachment[]): Set<TemporalNodeBase>;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { isVisualTemporalNode, } from "
|
|
1
|
+
import { isVisualTemporalNode, } from "../temporal.js";
|
|
2
2
|
import { ErrorDiagnostic } from "../../diagnostic.js";
|
|
3
|
-
import { claimDivLine, divLinePortName, } from "../div/index.js";
|
|
3
|
+
import { claimDivLine, divLinePortName, divLineRuns, divLineWidth, } from "../div/index.js";
|
|
4
4
|
/** 显式和自动分组共享同一个几何对象 */
|
|
5
|
-
export function createBeamLayoutAttachment(endPoints, explicit
|
|
5
|
+
export function createBeamLayoutAttachment(endPoints, explicit, sourceSpan) {
|
|
6
6
|
return new BeamLayoutAttachment(endPoints, explicit, sourceSpan);
|
|
7
7
|
}
|
|
8
8
|
/** 显式 beam 必须严格连接最终同轨同谱面行中的相邻可见主体 */
|
|
@@ -50,7 +50,7 @@ export function validateExplicitBeamAttachments(result) {
|
|
|
50
50
|
&& secondPosition.index === firstPosition.index + 1;
|
|
51
51
|
if (adjacent)
|
|
52
52
|
continue;
|
|
53
|
-
throw new ErrorDiagnostic("E_NON_ADJACENT_BEAM", "@beam 的端点必须按时间顺序连接同一轨道、同一谱面行中的相邻可见元素", attachment.sourceSpan
|
|
53
|
+
throw new ErrorDiagnostic("E_NON_ADJACENT_BEAM", "@beam 的端点必须按时间顺序连接同一轨道、同一谱面行中的相邻可见元素", attachment.sourceSpan);
|
|
54
54
|
}
|
|
55
55
|
}
|
|
56
56
|
}
|
|
@@ -66,22 +66,19 @@ export function collectExplicitBeamEndpoints(attachments) {
|
|
|
66
66
|
return result;
|
|
67
67
|
}
|
|
68
68
|
class BeamLayoutAttachment {
|
|
69
|
-
box = {
|
|
70
|
-
x: 0,
|
|
71
|
-
y: 0,
|
|
72
|
-
w: 0,
|
|
73
|
-
h: 0,
|
|
74
|
-
};
|
|
75
69
|
layer = "foreground";
|
|
76
70
|
endPoints;
|
|
77
71
|
explicit;
|
|
78
72
|
sourceSpan;
|
|
79
|
-
|
|
80
|
-
|
|
73
|
+
/** 端点字号不随布局变化,装饰尺寸可以一次算定 */
|
|
74
|
+
fontSize;
|
|
75
|
+
strokeWidth;
|
|
81
76
|
constructor(endPoints, explicit, sourceSpan) {
|
|
82
77
|
this.endPoints = endPoints;
|
|
83
78
|
this.explicit = explicit;
|
|
84
79
|
this.sourceSpan = sourceSpan;
|
|
80
|
+
this.fontSize = endPoints.reduce((size, endpoint) => isVisualTemporalNode(endpoint) ? Math.max(size, endpoint.ast.size) : size, 0);
|
|
81
|
+
this.strokeWidth = divLineWidth(this.fontSize);
|
|
85
82
|
}
|
|
86
83
|
/** 同一 beam 组内的相邻元素使用更短的自然弹簧间距 */
|
|
87
84
|
prepareHorizontal(context) {
|
|
@@ -99,76 +96,56 @@ class BeamLayoutAttachment {
|
|
|
99
96
|
}
|
|
100
97
|
}
|
|
101
98
|
}
|
|
102
|
-
|
|
103
|
-
this.
|
|
104
|
-
const
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
99
|
+
createGeometry(context) {
|
|
100
|
+
const lines = this.collectLines(context);
|
|
101
|
+
const strokeWidth = this.strokeWidth;
|
|
102
|
+
// 减时线永远水平,butt 端帽只在纵向占半个描边
|
|
103
|
+
const regions = lines.map(line => ({
|
|
104
|
+
x: Math.min(line.start.x, line.end.x),
|
|
105
|
+
y: line.start.y - strokeWidth / 2,
|
|
106
|
+
w: Math.abs(line.end.x - line.start.x),
|
|
107
|
+
h: strokeWidth,
|
|
110
108
|
line: line.line,
|
|
111
109
|
track: line.track,
|
|
112
110
|
}));
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
111
|
+
return {
|
|
112
|
+
regions,
|
|
113
|
+
paint(painter) {
|
|
114
|
+
for (const line of lines) {
|
|
115
|
+
painter.drawLine(line.start.x, line.start.y, line.end.x, line.end.y, { stroke: "#000", strokeWidth });
|
|
116
|
+
}
|
|
117
|
+
},
|
|
118
|
+
};
|
|
118
119
|
}
|
|
119
120
|
absolutePort(node, name) {
|
|
120
121
|
const port = node.ports[name];
|
|
121
|
-
if (!port)
|
|
122
|
-
return null;
|
|
123
122
|
return {
|
|
124
123
|
x: node.box.x + port.x,
|
|
125
124
|
y: node.box.y + port.y,
|
|
126
125
|
};
|
|
127
126
|
}
|
|
128
|
-
|
|
129
|
-
|
|
127
|
+
collectLines(context) {
|
|
128
|
+
const lines = [];
|
|
130
129
|
const available = this.endPoints.filter(isVisualTemporalNode);
|
|
131
130
|
if (available.length < 2)
|
|
132
|
-
return;
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
for (let level = 0;; level++) {
|
|
138
|
-
const spanGroups = [];
|
|
139
|
-
let spans = [];
|
|
140
|
-
let hasPorts = false;
|
|
141
|
-
for (const node of available) {
|
|
142
|
-
const left = this.absolutePort(node, divLinePortName(level, "left"));
|
|
143
|
-
const right = this.absolutePort(node, divLinePortName(level, "right"));
|
|
144
|
-
if (left || right)
|
|
145
|
-
hasPorts = true;
|
|
146
|
-
if (!left || !right) {
|
|
147
|
-
if (spans.length > 0)
|
|
148
|
-
spanGroups.push(spans);
|
|
149
|
-
spans = [];
|
|
150
|
-
continue;
|
|
151
|
-
}
|
|
131
|
+
return lines;
|
|
132
|
+
for (const { level, from, to } of divLineRuns(available)) {
|
|
133
|
+
const spans = [];
|
|
134
|
+
for (let i = from; i <= to; i++) {
|
|
135
|
+
const node = available[i];
|
|
152
136
|
spans.push({
|
|
153
|
-
left,
|
|
154
|
-
right,
|
|
137
|
+
left: this.absolutePort(node, divLinePortName(level, "left")),
|
|
138
|
+
right: this.absolutePort(node, divLinePortName(level, "right")),
|
|
155
139
|
line: node.layoutLine,
|
|
156
140
|
track: node.track,
|
|
157
141
|
node,
|
|
158
142
|
});
|
|
159
143
|
}
|
|
160
|
-
|
|
161
|
-
spanGroups.push(spans);
|
|
162
|
-
if (!hasPorts)
|
|
163
|
-
break;
|
|
164
|
-
for (const group of spanGroups) {
|
|
165
|
-
if (group.length >= 2)
|
|
166
|
-
this.addSystemLines(group, context, level);
|
|
167
|
-
}
|
|
144
|
+
this.addSystemLines(spans, context, level, lines);
|
|
168
145
|
}
|
|
169
146
|
// 显式关系允许连接没有 div 端口的任意可见对象
|
|
170
|
-
if (
|
|
171
|
-
const gap = fontSize * 0.12;
|
|
147
|
+
if (lines.length === 0 && this.explicit) {
|
|
148
|
+
const gap = this.fontSize * 0.12;
|
|
172
149
|
const spans = available.map(node => {
|
|
173
150
|
const point = {
|
|
174
151
|
x: node.box.x + node.box.anchor,
|
|
@@ -182,11 +159,12 @@ class BeamLayoutAttachment {
|
|
|
182
159
|
node,
|
|
183
160
|
};
|
|
184
161
|
});
|
|
185
|
-
this.addSystemLines(spans, context, null);
|
|
162
|
+
this.addSystemLines(spans, context, null, lines);
|
|
186
163
|
}
|
|
164
|
+
return lines;
|
|
187
165
|
}
|
|
188
166
|
/** 同一级减时线按谱面行拆开,跨行时连接到对应页面边界 */
|
|
189
|
-
addSystemLines(spans, context, connectedLevel) {
|
|
167
|
+
addSystemLines(spans, context, connectedLevel, lines) {
|
|
190
168
|
const firstLine = Math.min(...spans.map(item => item.line));
|
|
191
169
|
const lastLine = Math.max(...spans.map(item => item.line));
|
|
192
170
|
const fallbackTrack = spans[0].track;
|
|
@@ -199,7 +177,7 @@ class BeamLayoutAttachment {
|
|
|
199
177
|
if (current.length === 0) {
|
|
200
178
|
const visualAxis = context.getVisualAxis(line, fallbackTrack);
|
|
201
179
|
const y = visualAxis + visualAxisOffset;
|
|
202
|
-
|
|
180
|
+
lines.push({
|
|
203
181
|
start: { x: context.originX, y },
|
|
204
182
|
end: { x: context.originX + context.width, y },
|
|
205
183
|
line,
|
|
@@ -222,7 +200,7 @@ class BeamLayoutAttachment {
|
|
|
222
200
|
for (const span of current)
|
|
223
201
|
claimDivLine(span.node, connectedLevel);
|
|
224
202
|
}
|
|
225
|
-
|
|
203
|
+
lines.push({
|
|
226
204
|
start,
|
|
227
205
|
end,
|
|
228
206
|
line,
|