jpfun 0.5.0 → 0.7.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/dist/converter/midi/convert.d.ts +3 -1
- package/dist/converter/midi/convert.js +78 -66
- package/dist/converter/musicxml/features.d.ts +19 -5
- package/dist/converter/musicxml/features.js +46 -8
- package/dist/converter/musicxml/index.js +142 -23
- package/dist/converter/musicxml/model.d.ts +15 -0
- package/dist/diagnostic.js +1 -1
- package/dist/functions/ASTtypes.d.ts +4 -0
- package/dist/functions/ASTtypes.js +25 -14
- package/dist/functions/adjust/index.js +6 -2
- package/dist/functions/arpeggio/index.js +21 -17
- package/dist/functions/arpeggio/shape.d.ts +1 -1
- package/dist/functions/arpeggio/shape.js +27 -21
- package/dist/functions/bar/index.js +37 -36
- package/dist/functions/beam/auto.js +18 -24
- package/dist/functions/beam/layout.js +15 -23
- package/dist/functions/box/index.js +52 -1
- package/dist/functions/br/index.js +3 -4
- package/dist/functions/default.js +10 -5
- package/dist/functions/div/index.js +2 -3
- package/dist/functions/gliss/index.d.ts +2 -0
- package/dist/functions/gliss/index.js +180 -0
- package/dist/functions/grace/index.d.ts +4 -2
- package/dist/functions/grace/index.js +19 -11
- package/dist/functions/head/index.js +50 -62
- package/dist/functions/meter/index.js +45 -24
- package/dist/functions/meter/shape.d.ts +3 -0
- package/dist/functions/meter/shape.js +43 -0
- package/dist/functions/note/accidentals.js +10 -16
- package/dist/functions/note/index.js +38 -48
- package/dist/functions/ottava/index.d.ts +2 -0
- package/dist/functions/ottava/index.js +217 -0
- package/dist/functions/page/index.js +3 -3
- package/dist/functions/pagebreak/index.d.ts +2 -0
- package/dist/functions/pagebreak/index.js +36 -0
- package/dist/functions/rest/index.d.ts +2 -0
- package/dist/functions/rest/index.js +231 -0
- package/dist/functions/slide/index.d.ts +2 -0
- package/dist/functions/slide/index.js +132 -0
- package/dist/functions/stack/index.js +8 -15
- package/dist/functions/symbol/index.d.ts +9 -0
- package/dist/functions/symbol/index.js +41 -12
- package/dist/functions/symbol/ornament.js +15 -20
- package/dist/functions/symbol/shape.js +4 -1
- package/dist/functions/symbol/symbols/fine.js +1 -1
- package/dist/functions/symbol/symbols/tr.js +46 -1
- package/dist/functions/temporal.d.ts +36 -12
- package/dist/functions/temporal.js +29 -9
- package/dist/functions/tie/index.js +46 -32
- package/dist/functions/tuplet/index.js +1 -1
- package/dist/functions/up/index.js +51 -33
- package/dist/functions/voice/connections.d.ts +19 -0
- package/dist/functions/voice/connections.js +90 -0
- package/dist/functions/voice/index.js +245 -99
- package/dist/functions/volta/index.js +41 -21
- package/dist/jpfun.core.min.js +48 -55
- package/dist/jpfun.from-midi.min.js +5 -5
- package/dist/jpfun.from-musicxml.min.js +8 -8
- package/dist/jpfun.min.js +57 -64
- package/dist/layout/engine.d.ts +4 -1
- package/dist/layout/engine.js +232 -75
- package/dist/layout/model.js +22 -66
- package/dist/layout/page.d.ts +3 -2
- package/dist/layout/page.js +7 -6
- package/dist/layout/types.d.ts +34 -45
- package/dist/lowering/loweringContext.d.ts +1 -1
- package/dist/lowering/loweringContext.js +41 -171
- package/dist/parser/parserContext.d.ts +2 -0
- package/dist/parser/parserContext.js +31 -14
- package/dist/parser/preprocess.js +4 -6
- package/dist/playback/compile.js +12 -3
- package/dist/playback/types.d.ts +4 -2
- package/dist/render/paint.js +1 -1
- package/dist/render/svg.js +1 -0
- package/dist/render/text.d.ts +1 -1
- package/dist/render/text.js +3 -2
- package/dist/render/types.d.ts +1 -0
- package/grammar.md +221 -257
- package/package.json +1 -1
|
@@ -6,7 +6,7 @@ const ARROW_HEIGHT = 93.6719;
|
|
|
6
6
|
const ARROW_X = -27.4688;
|
|
7
7
|
const ARROW_Y = -7.9375;
|
|
8
8
|
const ARROW_BOTTOM = ARROW_Y + ARROW_HEIGHT;
|
|
9
|
-
/** MuseScore 4.7.4
|
|
9
|
+
/** MuseScore 4.7.4 导出的琶音波形块,已旋转到自上而下的局部坐标 */
|
|
10
10
|
const TILE = [
|
|
11
11
|
{ op: "M", x: 28.1406, y: 86.06255 },
|
|
12
12
|
{ op: "C", cx1: 28.1406, cy1: 86.06255, cx2: 30.4531, cy2: 84.07815, x: 32.7656, y: 81.42185 },
|
|
@@ -60,9 +60,9 @@ const DOWN_ARROW = [
|
|
|
60
60
|
{ op: "C", cx1: 58.9063, cy1: 93.6719, cx2: 61.8907, cy2: 92.3438, x: 63.2188, y: 90.3594 },
|
|
61
61
|
{ op: "L", x: 111.8751, y: 21.5156 },
|
|
62
62
|
];
|
|
63
|
-
function appendShape(output, shape,
|
|
64
|
-
const x = (value) => offsetX + (reverse ?
|
|
65
|
-
const y = (value) => offsetY + (reverse ?
|
|
63
|
+
function appendShape(output, shape, offsetX, offsetY, scale, reverse) {
|
|
64
|
+
const x = (value) => offsetX + (reverse ? TILE_WIDTH - value : value) * scale;
|
|
65
|
+
const y = (value) => offsetY + (reverse ? TILE_HEIGHT - value : value) * scale;
|
|
66
66
|
for (const command of shape) {
|
|
67
67
|
if (command.op === "Z")
|
|
68
68
|
output.push(command);
|
|
@@ -83,7 +83,7 @@ function appendShape(output, shape, width, height, offsetX, offsetY, scale, reve
|
|
|
83
83
|
output.push({ op: command.op, x: x(command.x), y: y(command.y) });
|
|
84
84
|
}
|
|
85
85
|
}
|
|
86
|
-
/** 按 MuseScore
|
|
86
|
+
/** 按 MuseScore 的原始重叠比例铺满指定高度,并一次性返回最终边界 */
|
|
87
87
|
export function prepareArpeggioShape(height, em, direction) {
|
|
88
88
|
const arrow = direction === "up" || direction === "down";
|
|
89
89
|
const segments = Math.max(1, Math.round(height / (em * 0.3)));
|
|
@@ -94,29 +94,35 @@ export function prepareArpeggioShape(height, em, direction) {
|
|
|
94
94
|
const scale = height / naturalHeight;
|
|
95
95
|
const commands = [];
|
|
96
96
|
for (let index = 0; index < regularCount; index++) {
|
|
97
|
-
appendShape(commands, TILE,
|
|
97
|
+
appendShape(commands, TILE, 0, index * TILE_STEP * scale, scale, direction === "");
|
|
98
98
|
}
|
|
99
99
|
if (arrow) {
|
|
100
|
-
appendShape(commands, DOWN_ARROW,
|
|
100
|
+
appendShape(commands, DOWN_ARROW, ARROW_X * scale, (regularCount * TILE_STEP + ARROW_Y) * scale, scale, false);
|
|
101
101
|
}
|
|
102
102
|
const bounds = pathBounds(commands);
|
|
103
103
|
if (direction !== "up")
|
|
104
104
|
return { commands, bounds };
|
|
105
|
-
const
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
op
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
105
|
+
const right = bounds.x + bounds.w;
|
|
106
|
+
const mirrorX = (value) => right - (value - bounds.x);
|
|
107
|
+
// 命令尚未发布,直接镜像,避免再分配整条路径及每个命令对象
|
|
108
|
+
for (const command of commands) {
|
|
109
|
+
if (command.op === "Z")
|
|
110
|
+
continue;
|
|
111
|
+
if (command.op === "C") {
|
|
112
|
+
command.cx1 = mirrorX(command.cx1);
|
|
113
|
+
command.cy1 = height - command.cy1;
|
|
114
|
+
command.cx2 = mirrorX(command.cx2);
|
|
115
|
+
command.cy2 = height - command.cy2;
|
|
116
|
+
}
|
|
117
|
+
else if (command.op === "Q") {
|
|
118
|
+
command.cx = mirrorX(command.cx);
|
|
119
|
+
command.cy = height - command.cy;
|
|
120
|
+
}
|
|
121
|
+
command.x = mirrorX(command.x);
|
|
122
|
+
command.y = height - command.y;
|
|
123
|
+
}
|
|
118
124
|
return {
|
|
119
|
-
commands
|
|
125
|
+
commands,
|
|
120
126
|
bounds: { x: bounds.x, y: height - bounds.y - bounds.h, w: bounds.w, h: bounds.h },
|
|
121
127
|
};
|
|
122
128
|
}
|
|
@@ -13,7 +13,7 @@ class BarFunction extends ASTFunctionNode {
|
|
|
13
13
|
args: [
|
|
14
14
|
{
|
|
15
15
|
name: "type",
|
|
16
|
-
description: "0:普通线 `|`;1:终止线
|
|
16
|
+
description: "0:普通线 `|`;1:双竖线 `||`;2:终止线 `|||`(左细右粗);3:反复起点 `|:`;4:反复终点 `:|`;5:左右反复 `:|:`",
|
|
17
17
|
type: "number",
|
|
18
18
|
default: 0,
|
|
19
19
|
},
|
|
@@ -31,16 +31,19 @@ class BarFunction extends ASTFunctionNode {
|
|
|
31
31
|
static deSugarAtom(source, start, _end) {
|
|
32
32
|
let pos = start;
|
|
33
33
|
let type = 0;
|
|
34
|
+
const slice3 = source.slice(pos, pos + 3);
|
|
34
35
|
const slice2 = source.slice(pos, pos + 2);
|
|
35
|
-
if (
|
|
36
|
+
if (slice3 === "|||")
|
|
37
|
+
type = 2, pos += 3;
|
|
38
|
+
else if (slice2 === "||")
|
|
36
39
|
type = 1, pos += 2;
|
|
37
40
|
else if (slice2 === "|:")
|
|
38
|
-
type =
|
|
41
|
+
type = 3, pos += 2;
|
|
39
42
|
else if (slice2 === ":|") {
|
|
40
43
|
if (source[pos + 2] === ":")
|
|
41
|
-
type =
|
|
44
|
+
type = 5, pos += 3;
|
|
42
45
|
else
|
|
43
|
-
type =
|
|
46
|
+
type = 4, pos += 2;
|
|
44
47
|
}
|
|
45
48
|
else if (source[pos] === "|")
|
|
46
49
|
type = 0, pos += 1;
|
|
@@ -68,7 +71,9 @@ class BarFunction extends ASTFunctionNode {
|
|
|
68
71
|
this.size = ctx.length2px(size);
|
|
69
72
|
}
|
|
70
73
|
loweringEnter() {
|
|
71
|
-
return [this.type >=
|
|
74
|
+
return [this.type >= 3 && this.type <= 5
|
|
75
|
+
? new RepeatBarTemporalNode(this)
|
|
76
|
+
: new BarTemporalNode(this)];
|
|
72
77
|
}
|
|
73
78
|
toString() {
|
|
74
79
|
return `@bar(${this.type}, ${this.size}px)`;
|
|
@@ -99,30 +104,20 @@ class BarTemporalNode extends TemporalNodeBase {
|
|
|
99
104
|
if (!isVisualTemporalNode(host))
|
|
100
105
|
return;
|
|
101
106
|
const run = line.trackRuns.get(host.track);
|
|
102
|
-
const index =
|
|
107
|
+
const index = line.trackIndexOf(host);
|
|
103
108
|
if (!run || index < 0)
|
|
104
109
|
return;
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
current.config.mu_L *= 4;
|
|
117
|
-
}
|
|
118
|
-
const rightNode = run[index + 1];
|
|
119
|
-
const right = rightNode ? elementOf(rightNode) : undefined;
|
|
120
|
-
// 相邻两个 bar 的中间 gap 由右侧 bar 处理,避免重复增强
|
|
121
|
-
if (right && rightNode.mergeKey !== ANCHOR_KEY) {
|
|
122
|
-
current.config.mu_R *= 4;
|
|
123
|
-
right.config.mu_L *= 4;
|
|
124
|
-
}
|
|
125
|
-
});
|
|
110
|
+
const left = run[index - 1];
|
|
111
|
+
if (left) {
|
|
112
|
+
left.springConfig.mu_R *= 4;
|
|
113
|
+
host.springConfig.mu_L *= 4;
|
|
114
|
+
}
|
|
115
|
+
const right = run[index + 1];
|
|
116
|
+
// 相邻两个 bar 的中间 gap 由右侧 bar 处理,避免重复增强
|
|
117
|
+
if (right && right.mergeKey !== ANCHOR_KEY) {
|
|
118
|
+
host.springConfig.mu_R *= 4;
|
|
119
|
+
right.springConfig.mu_L *= 4;
|
|
120
|
+
}
|
|
126
121
|
}
|
|
127
122
|
prepareLayout() {
|
|
128
123
|
const { type, size } = this.ast;
|
|
@@ -134,16 +129,22 @@ class BarTemporalNode extends TemporalNodeBase {
|
|
|
134
129
|
const gap = thin * 1.8;
|
|
135
130
|
const dotRadius = thin * 0.8;
|
|
136
131
|
const dotGap = gap + dotRadius;
|
|
137
|
-
const hasLeftDots = type ===
|
|
138
|
-
const hasRightDots = type ===
|
|
132
|
+
const hasLeftDots = type === 4 || type === 5;
|
|
133
|
+
const hasRightDots = type === 3 || type === 5;
|
|
139
134
|
let x = hasLeftDots ? dotRadius * 2 + dotGap : 0;
|
|
140
|
-
if (type === 1
|
|
135
|
+
if (type === 1) {
|
|
136
|
+
this.lines.push({ x, w: thin });
|
|
137
|
+
x += thin + gap;
|
|
138
|
+
this.lines.push({ x, w: thin });
|
|
139
|
+
x += thin;
|
|
140
|
+
}
|
|
141
|
+
else if (type === 2 || type === 4 || type === 5) {
|
|
141
142
|
this.lines.push({ x, w: thin });
|
|
142
143
|
x += thin + gap;
|
|
143
144
|
this.lines.push({ x, w: thick });
|
|
144
145
|
x += thick;
|
|
145
146
|
}
|
|
146
|
-
else if (type ===
|
|
147
|
+
else if (type === 3) {
|
|
147
148
|
this.lines.push({ x, w: thick });
|
|
148
149
|
x += thick + gap;
|
|
149
150
|
this.lines.push({ x, w: thin });
|
|
@@ -166,7 +167,7 @@ class BarTemporalNode extends TemporalNodeBase {
|
|
|
166
167
|
}
|
|
167
168
|
this.box.w = x;
|
|
168
169
|
this.box.h = h;
|
|
169
|
-
const anchorLine = type >=
|
|
170
|
+
const anchorLine = type >= 3 && type <= 5 ? this.lines[0] : undefined;
|
|
170
171
|
this.box.anchor = anchorLine ? anchorLine.x + anchorLine.w / 2 : x / 2;
|
|
171
172
|
this.box.visualAxis = h / 2;
|
|
172
173
|
for (const line of this.lines)
|
|
@@ -195,13 +196,13 @@ export function repeatPass(cursor, column) {
|
|
|
195
196
|
}
|
|
196
197
|
/** 只有反复线参与播放顺序;普通小节线太多,进控制流扫描是纯开销 */
|
|
197
198
|
class RepeatBarTemporalNode extends BarTemporalNode {
|
|
198
|
-
/** 只在 type
|
|
199
|
+
/** 只在 type 3–5 时创建,所以三种分别是:3 段首、4 段尾、5 两者都是 */
|
|
199
200
|
playbackMarks() {
|
|
200
|
-
return this.ast.type ===
|
|
201
|
+
return this.ast.type === 4 ? [] : [REPEAT_START];
|
|
201
202
|
}
|
|
202
203
|
/** 每条反复线只回跳一次,所以连写几条就是几遍 */
|
|
203
204
|
playbackFlow(columnOf) {
|
|
204
|
-
if (this.ast.type ===
|
|
205
|
+
if (this.ast.type === 3)
|
|
205
206
|
return;
|
|
206
207
|
const at = columnOf(this);
|
|
207
208
|
if (at === undefined)
|
|
@@ -4,8 +4,10 @@ import { DIV_ADDON_KEY, DivNode, } from "../div/index.js";
|
|
|
4
4
|
import { collectExplicitBeamEndpoints, createBeamLayoutAttachment, } from "./layout.js";
|
|
5
5
|
/** 只返回实际参与当前事件 div addon 的 div,先沿祖先链找,不够再按书写顺序向下补 */
|
|
6
6
|
function findActiveDivs(node) {
|
|
7
|
-
const result = [];
|
|
8
7
|
let remainingLevels = Math.max(0, Math.floor(Number(node.addon?.[DIV_ADDON_KEY]) || 0));
|
|
8
|
+
if (remainingLevels <= 0)
|
|
9
|
+
return [];
|
|
10
|
+
const result = [];
|
|
9
11
|
const take = (div) => {
|
|
10
12
|
result.push(div);
|
|
11
13
|
remainingLevels -= Math.max(0, div.n);
|
|
@@ -16,6 +18,8 @@ function findActiveDivs(node) {
|
|
|
16
18
|
take(ast);
|
|
17
19
|
ast = ast.parent;
|
|
18
20
|
}
|
|
21
|
+
if (remainingLevels <= 0)
|
|
22
|
+
return result;
|
|
19
23
|
// 复合节点(如 up)把代表成员的修饰提升成了自己的,提供层级的 div 在子树里
|
|
20
24
|
const descend = (parent) => {
|
|
21
25
|
for (const child of parent.children ?? []) {
|
|
@@ -71,24 +75,21 @@ function outermostScopes(scopes) {
|
|
|
71
75
|
return true;
|
|
72
76
|
});
|
|
73
77
|
}
|
|
74
|
-
function
|
|
78
|
+
function createAutoBeam(nodes, nodeScopes) {
|
|
75
79
|
let start = Infinity;
|
|
76
80
|
let end = -Infinity;
|
|
77
81
|
for (const node of nodes) {
|
|
78
82
|
start = Math.min(start, node.ast.sourceSpan.start);
|
|
79
83
|
end = Math.max(end, node.ast.sourceSpan.end);
|
|
80
|
-
for (const
|
|
81
|
-
start = Math.min(start,
|
|
82
|
-
end = Math.max(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);
|
|
83
87
|
}
|
|
84
88
|
}
|
|
85
|
-
return { start, end };
|
|
86
|
-
}
|
|
87
|
-
function createAutoBeam(nodes) {
|
|
88
|
-
return createBeamLayoutAttachment(nodes, false, sourceSpanForAutoBeam(nodes));
|
|
89
|
+
return createBeamLayoutAttachment(nodes, false, { start, end });
|
|
89
90
|
}
|
|
90
91
|
/** 同一个 div 内部始终按轨道连接,手工 beam 端点会截断自动片段 */
|
|
91
|
-
function createScopeBeams(scopes, explicitEndpoints) {
|
|
92
|
+
function createScopeBeams(scopes, nodeScopes, explicitEndpoints) {
|
|
92
93
|
const attachments = [];
|
|
93
94
|
for (const scope of outermostScopes(scopes)) {
|
|
94
95
|
const tracks = new Map();
|
|
@@ -103,7 +104,7 @@ function createScopeBeams(scopes, explicitEndpoints) {
|
|
|
103
104
|
let segment = [];
|
|
104
105
|
const flush = () => {
|
|
105
106
|
if (segment.length >= 2)
|
|
106
|
-
attachments.push(createAutoBeam(segment));
|
|
107
|
+
attachments.push(createAutoBeam(segment, nodeScopes));
|
|
107
108
|
segment = [];
|
|
108
109
|
};
|
|
109
110
|
for (const node of nodes) {
|
|
@@ -147,7 +148,7 @@ function createAdjacentBeams(result, nodeScopes, explicitEndpoints) {
|
|
|
147
148
|
return;
|
|
148
149
|
groups.delete(track);
|
|
149
150
|
if (group.nodes.length >= 2)
|
|
150
|
-
attachments.push(createAutoBeam(group.nodes));
|
|
151
|
+
attachments.push(createAutoBeam(group.nodes, nodeScopes));
|
|
151
152
|
};
|
|
152
153
|
const flushAll = () => {
|
|
153
154
|
for (const track of [...groups.keys()])
|
|
@@ -181,17 +182,8 @@ function createAdjacentBeams(result, nodeScopes, explicitEndpoints) {
|
|
|
181
182
|
const activeStart = measureStart.compare(lineStart) >= 0 ? measureStart : lineStart;
|
|
182
183
|
const beat = beatAt(beatTime.copyFrom(node.t).sub(activeStart));
|
|
183
184
|
const current = groups.get(node.track);
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
nodes: [node],
|
|
187
|
-
line: node.layoutLine,
|
|
188
|
-
beat,
|
|
189
|
-
endTime: node.t.clone().add(node.T),
|
|
190
|
-
});
|
|
191
|
-
continue;
|
|
192
|
-
}
|
|
193
|
-
const isContinuous = node.t.equals(current.endTime);
|
|
194
|
-
if (node.layoutLine !== current.line || beat !== current.beat || !isContinuous) {
|
|
185
|
+
const isContinuous = current && node.t.equals(current.endTime);
|
|
186
|
+
if (!current || node.layoutLine !== current.line || beat !== current.beat || !isContinuous) {
|
|
195
187
|
flush(node.track);
|
|
196
188
|
groups.set(node.track, {
|
|
197
189
|
nodes: [node],
|
|
@@ -211,9 +203,11 @@ function createAdjacentBeams(result, nodeScopes, explicitEndpoints) {
|
|
|
211
203
|
/** beam 域在 lowering 固化后统一生成作用域连接和相邻自动连接 */
|
|
212
204
|
export function createAutomaticBeamAttachments(result) {
|
|
213
205
|
const { scopes, nodeScopes } = collectDivScopes(result);
|
|
206
|
+
if (scopes.length === 0)
|
|
207
|
+
return [];
|
|
214
208
|
const explicitEndpoints = collectExplicitBeamEndpoints(result.attachments);
|
|
215
209
|
return [
|
|
216
|
-
...createScopeBeams(scopes, explicitEndpoints),
|
|
210
|
+
...createScopeBeams(scopes, nodeScopes, explicitEndpoints),
|
|
217
211
|
...createAdjacentBeams(result, nodeScopes, explicitEndpoints),
|
|
218
212
|
];
|
|
219
213
|
}
|
|
@@ -8,6 +8,7 @@ export function createBeamLayoutAttachment(endPoints, explicit, sourceSpan) {
|
|
|
8
8
|
/** 显式 beam 连接同轨同谱面行中的相邻可见主体,忽略零时长小节线 */
|
|
9
9
|
export function validateExplicitBeamAttachments(result) {
|
|
10
10
|
const tracks = new Map();
|
|
11
|
+
const positions = new Map();
|
|
11
12
|
for (const column of result.columns) {
|
|
12
13
|
for (const node of column) {
|
|
13
14
|
if (!isVisualTemporalNode(node))
|
|
@@ -19,19 +20,9 @@ export function validateExplicitBeamAttachments(result) {
|
|
|
19
20
|
lineTracks = new Map();
|
|
20
21
|
tracks.set(node.layoutLine, lineTracks);
|
|
21
22
|
}
|
|
22
|
-
const
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
else
|
|
26
|
-
lineTracks.set(node.track, [node]);
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
const positions = new Map();
|
|
30
|
-
for (const [line, lineTracks] of tracks) {
|
|
31
|
-
for (const [track, sequence] of lineTracks) {
|
|
32
|
-
for (let index = 0; index < sequence.length; index++) {
|
|
33
|
-
positions.set(sequence[index], { line, track, index });
|
|
34
|
-
}
|
|
23
|
+
const index = lineTracks.get(node.track) ?? 0;
|
|
24
|
+
positions.set(node, { line: node.layoutLine, track: node.track, index });
|
|
25
|
+
lineTracks.set(node.track, index + 1);
|
|
35
26
|
}
|
|
36
27
|
}
|
|
37
28
|
for (const attachment of result.attachments) {
|
|
@@ -84,18 +75,19 @@ class BeamLayoutAttachment {
|
|
|
84
75
|
}
|
|
85
76
|
/** 同一 beam 组内的相邻元素使用更短的自然弹簧间距 */
|
|
86
77
|
prepareHorizontal(context) {
|
|
87
|
-
|
|
88
|
-
for (const
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
left.springConfig.alpha_R
|
|
96
|
-
|
|
78
|
+
let left;
|
|
79
|
+
for (const node of this.endPoints) {
|
|
80
|
+
if (!isVisualTemporalNode(node))
|
|
81
|
+
continue;
|
|
82
|
+
if (left && left.layoutLine === node.layoutLine && left.track === node.track) {
|
|
83
|
+
const line = context[left.layoutLine];
|
|
84
|
+
const index = line ? line.trackIndexOf(left) : -1;
|
|
85
|
+
if (index >= 0 && index + 1 === line.trackIndexOf(node)) {
|
|
86
|
+
left.springConfig.alpha_R *= 0.8;
|
|
87
|
+
node.springConfig.alpha_L *= 0.8;
|
|
97
88
|
}
|
|
98
89
|
}
|
|
90
|
+
left = node;
|
|
99
91
|
}
|
|
100
92
|
}
|
|
101
93
|
createGeometry(context) {
|
|
@@ -2,6 +2,7 @@ import { ASTFunctionNode } from "../ASTtypes.js";
|
|
|
2
2
|
import { isVisualTemporalNode } from "../temporal.js";
|
|
3
3
|
import { ErrorDiagnostic } from "../../diagnostic.js";
|
|
4
4
|
import { layoutHorizontalRegion } from "../../layout/model.js";
|
|
5
|
+
import { isLayoutAttachment } from "../../layout/types.js";
|
|
5
6
|
class BoxFunction extends ASTFunctionNode {
|
|
6
7
|
static def = {
|
|
7
8
|
name: ["box"],
|
|
@@ -79,6 +80,14 @@ class BoxFunction extends ASTFunctionNode {
|
|
|
79
80
|
ctx.addAttachment(attachment);
|
|
80
81
|
const first = content.nodes[0];
|
|
81
82
|
if (first) {
|
|
83
|
+
if (attachment.reservedHost) {
|
|
84
|
+
const prepareLayout = first.prepareLayout;
|
|
85
|
+
first.prepareLayout = context => {
|
|
86
|
+
prepareLayout.call(first, context);
|
|
87
|
+
const space = { order: Infinity, height: Math.max(0, this.padding + this.stroke / 2) };
|
|
88
|
+
first.decorations.push({ above: space, below: space });
|
|
89
|
+
};
|
|
90
|
+
}
|
|
82
91
|
const prepare = first.prepareHorizontal;
|
|
83
92
|
first.prepareHorizontal = line => {
|
|
84
93
|
prepare?.call(first, line);
|
|
@@ -105,12 +114,18 @@ class BoxLayoutAttachment {
|
|
|
105
114
|
content;
|
|
106
115
|
owner;
|
|
107
116
|
layer = "background";
|
|
117
|
+
reservedHost;
|
|
108
118
|
fixedStart = null;
|
|
109
119
|
wallOffset = 0;
|
|
110
120
|
get sourceSpan() { return this.owner.sourceSpan; }
|
|
111
121
|
constructor(content, owner) {
|
|
112
122
|
this.content = content;
|
|
113
123
|
this.owner = owner;
|
|
124
|
+
const [host] = content.nodes;
|
|
125
|
+
// 后测关系可能超出主体,只有已内含的嵌套框能提前预留完整空间
|
|
126
|
+
if (content.nodes.length === 1 && content.attachments.every(attachment => attachment instanceof BoxLayoutAttachment && attachment.reservedHost === host)) {
|
|
127
|
+
this.reservedHost = host;
|
|
128
|
+
}
|
|
114
129
|
}
|
|
115
130
|
/** 只在完整包含成员的视图注册;留白和定宽共用同一个由内向外执行的 hook */
|
|
116
131
|
registerHorizontal(line) {
|
|
@@ -182,7 +197,7 @@ class BoxLayoutAttachment {
|
|
|
182
197
|
return { regions: [], paint() { } };
|
|
183
198
|
}
|
|
184
199
|
const inset = padding + stroke / 2;
|
|
185
|
-
//
|
|
200
|
+
// 单主体已提前预留空间,范围框在最终测量时申报外围占用
|
|
186
201
|
const fixedX = this.fixedStart
|
|
187
202
|
? this.fixedStart.box.x + this.fixedStart.box.anchor + this.wallOffset
|
|
188
203
|
: rect.x;
|
|
@@ -197,6 +212,7 @@ class BoxLayoutAttachment {
|
|
|
197
212
|
}
|
|
198
213
|
return {
|
|
199
214
|
regions: [region],
|
|
215
|
+
occupancy: this.reservedHost ? [] : this.occupancy(context, region),
|
|
200
216
|
paint(painter) {
|
|
201
217
|
const strokeInset = stroke / 2;
|
|
202
218
|
painter.drawRect(region.x + strokeInset, region.y + strokeInset, Math.max(0, region.w - stroke), Math.max(0, region.h - stroke), {
|
|
@@ -206,4 +222,39 @@ class BoxLayoutAttachment {
|
|
|
206
222
|
},
|
|
207
223
|
};
|
|
208
224
|
}
|
|
225
|
+
occupancy(context, region) {
|
|
226
|
+
const owners = new Set();
|
|
227
|
+
let line;
|
|
228
|
+
const include = (index, track) => {
|
|
229
|
+
if (line !== undefined && line !== index) {
|
|
230
|
+
throw new ErrorDiagnostic("E_BOX_CROSS_LINE", "@box 的内容不能跨越谱面行", this.owner.sourceSpan);
|
|
231
|
+
}
|
|
232
|
+
line = index;
|
|
233
|
+
owners.add(track);
|
|
234
|
+
};
|
|
235
|
+
for (const node of this.content.nodes)
|
|
236
|
+
include(node.layoutLine, node.track);
|
|
237
|
+
for (const attachment of this.content.attachments) {
|
|
238
|
+
if (!isLayoutAttachment(attachment))
|
|
239
|
+
continue;
|
|
240
|
+
const occupancy = context.getAttachmentOccupancy(attachment);
|
|
241
|
+
if (!occupancy)
|
|
242
|
+
continue;
|
|
243
|
+
for (const point of attachment.endPoints ?? [])
|
|
244
|
+
include(point.layoutLine, point.track);
|
|
245
|
+
for (const part of occupancy) {
|
|
246
|
+
if (part.line !== undefined)
|
|
247
|
+
include(part.line, part.track);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
if (line === undefined)
|
|
251
|
+
return [];
|
|
252
|
+
const index = line;
|
|
253
|
+
const sorted = [...owners].sort((left, right) => context.getVisualAxis(index, left) - context.getVisualAxis(index, right));
|
|
254
|
+
// 整框只申报外围上下沿,不能把内部轨距重复计入每条轨道
|
|
255
|
+
return [
|
|
256
|
+
{ ...region, h: 0, line: index, track: sorted[0] },
|
|
257
|
+
{ ...region, y: region.y + region.h, h: 0, line: index, track: sorted.at(-1) },
|
|
258
|
+
];
|
|
259
|
+
}
|
|
209
260
|
}
|
|
@@ -50,19 +50,18 @@ class BrFunction extends ASTFunctionNode {
|
|
|
50
50
|
const br = new TemporalNodeBase();
|
|
51
51
|
br.ast = this;
|
|
52
52
|
br.mergeKey = -1;
|
|
53
|
-
br.
|
|
53
|
+
br.addon = { breakBefore: this.offset };
|
|
54
54
|
return [br];
|
|
55
55
|
}
|
|
56
56
|
/**
|
|
57
|
-
*
|
|
58
|
-
* 换行会把整条时间线切开,因此跨越换行点的持续事件无法被完整绘制
|
|
57
|
+
* 换行与换页共享校验;边界会切开全谱,不能截断持续事件
|
|
59
58
|
*/
|
|
60
59
|
static loweringFinalize = (result) => {
|
|
61
60
|
const latestEnd = new Fraction();
|
|
62
61
|
const candidateEnd = new Fraction();
|
|
63
62
|
let latestNode = null;
|
|
64
63
|
for (const column of result.columns) {
|
|
65
|
-
if (latestNode && latestEnd.compare(column[0].t) > 0 && column.some(node => node.breakBefore > 0)) {
|
|
64
|
+
if (latestNode && latestEnd.compare(column[0].t) > 0 && column.some(node => (node.addon?.breakBefore ?? 0) > 0)) {
|
|
66
65
|
throw Diagnostic.error.BreakInsideEvent(latestNode.ast.sourceSpan);
|
|
67
66
|
}
|
|
68
67
|
for (const node of column) {
|
|
@@ -8,13 +8,18 @@ import { DivNode } from "./div/index.js";
|
|
|
8
8
|
import { DotNode } from "./dot/index.js";
|
|
9
9
|
import { DynNode } from "./dyn/index.js";
|
|
10
10
|
import { GraceNode } from "./grace/index.js";
|
|
11
|
+
import { GlissNode } from "./gliss/index.js";
|
|
12
|
+
import { SlideNode } from "./slide/index.js";
|
|
11
13
|
import { HeadNode } from "./head/index.js";
|
|
12
14
|
import { KeyNode } from "./key/index.js";
|
|
13
15
|
import { MeterNode } from "./meter/index.js";
|
|
14
16
|
import { BrNode } from "./br/index.js";
|
|
15
17
|
import { NoteNode } from "./note/index.js";
|
|
18
|
+
import { OttavaNode } from "./ottava/index.js";
|
|
19
|
+
import { RestNode } from "./rest/index.js";
|
|
16
20
|
import { UpNode, DownNode } from "./up/index.js";
|
|
17
21
|
import { PageNode } from "./page/index.js";
|
|
22
|
+
import { PageBreakNode } from "./pagebreak/index.js";
|
|
18
23
|
import { ProgramNode } from "./program/index.js";
|
|
19
24
|
import { StackNode } from "./stack/index.js";
|
|
20
25
|
import { SymbolNode } from "./symbol/index.js";
|
|
@@ -26,17 +31,17 @@ import { TieNode } from "./tie/index.js";
|
|
|
26
31
|
import { TupletNode } from "./tuplet/index.js";
|
|
27
32
|
import { VoiceNode, VoicesNode } from "./voice/index.js";
|
|
28
33
|
export const defaultFunctions = [
|
|
29
|
-
NoteNode, DashNode, BarNode,
|
|
34
|
+
NoteNode, DashNode, RestNode, BarNode,
|
|
30
35
|
DivNode, DotNode, TupletNode, // 装饰性
|
|
31
36
|
VoiceNode, VoicesNode, // 歌词和声部
|
|
32
|
-
BrNode, // 排版
|
|
37
|
+
BrNode, PageBreakNode, // 排版
|
|
33
38
|
PageNode, HeadNode, // 文档页面与谱头
|
|
34
39
|
StackNode, // 时间同步
|
|
35
40
|
UpNode, DownNode,
|
|
36
41
|
ArpeggioNode,
|
|
37
|
-
GraceNode, //
|
|
38
|
-
TieNode, BeamNode, DynNode,
|
|
39
|
-
BoxNode, VoltaNode,
|
|
42
|
+
GraceNode, SlideNode, // 倚音与滑入滑出
|
|
43
|
+
TieNode, BeamNode, GlissNode, DynNode,
|
|
44
|
+
BoxNode, VoltaNode, OttavaNode,
|
|
40
45
|
AdjustNode, // 手工微调
|
|
41
46
|
SetNode, KeyNode, MeterNode, TempoNode, ProgramNode, // 设置
|
|
42
47
|
TextNode, SymbolNode,
|
|
@@ -135,11 +135,10 @@ class DivFunction extends ASTFunctionNode {
|
|
|
135
135
|
let lineRight = 0;
|
|
136
136
|
let firstLine = 0;
|
|
137
137
|
return {
|
|
138
|
-
// 减时线声明为靠近主体的下方项
|
|
139
|
-
// 引擎只按通用 order 排列,不识别 @div
|
|
140
138
|
below: {
|
|
141
139
|
order: 0,
|
|
142
|
-
|
|
140
|
+
// 画线沿用正文盒基准,间距换算到装饰排列起点。
|
|
141
|
+
gap: host.box.h - (host.ports["decoration.below"]?.y ?? host.box.h) - host.ast.size * 0.08,
|
|
143
142
|
height: lineBlockHeight,
|
|
144
143
|
place(y) {
|
|
145
144
|
lineLeft = host.ports["body.left"]?.x ?? 0;
|