jpfun 0.1.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/LICENSE +201 -0
- package/README.md +134 -0
- package/dist/diagnostic.d.ts +27 -0
- package/dist/diagnostic.js +141 -0
- package/dist/functions/ASTtypes.d.ts +115 -0
- package/dist/functions/ASTtypes.js +187 -0
- package/dist/functions/bar/index.d.ts +2 -0
- package/dist/functions/bar/index.js +180 -0
- package/dist/functions/beam/auto.d.ts +4 -0
- package/dist/functions/beam/auto.js +202 -0
- package/dist/functions/beam/index.d.ts +2 -0
- package/dist/functions/beam/index.js +54 -0
- package/dist/functions/beam/layout.d.ts +9 -0
- package/dist/functions/beam/layout.js +233 -0
- package/dist/functions/box/index.d.ts +2 -0
- package/dist/functions/box/index.js +202 -0
- package/dist/functions/br/index.d.ts +2 -0
- package/dist/functions/br/index.js +83 -0
- package/dist/functions/dash/index.d.ts +2 -0
- package/dist/functions/dash/index.js +61 -0
- package/dist/functions/default.d.ts +2 -0
- package/dist/functions/default.js +32 -0
- package/dist/functions/div/index.d.ts +50 -0
- package/dist/functions/div/index.js +190 -0
- package/dist/functions/dot/index.d.ts +2 -0
- package/dist/functions/dot/index.js +160 -0
- package/dist/functions/grace/index.d.ts +84 -0
- package/dist/functions/grace/index.js +464 -0
- package/dist/functions/key/index.d.ts +2 -0
- package/dist/functions/key/index.js +40 -0
- package/dist/functions/note/accidentals.d.ts +10 -0
- package/dist/functions/note/accidentals.js +60 -0
- package/dist/functions/note/index.d.ts +2 -0
- package/dist/functions/note/index.js +284 -0
- package/dist/functions/note/noteNameFSM.d.ts +8 -0
- package/dist/functions/note/noteNameFSM.js +192 -0
- package/dist/functions/over/index.d.ts +2 -0
- package/dist/functions/over/index.js +307 -0
- package/dist/functions/page/index.d.ts +20 -0
- package/dist/functions/page/index.js +63 -0
- package/dist/functions/set/index.d.ts +2 -0
- package/dist/functions/set/index.js +55 -0
- package/dist/functions/stack/index.d.ts +2 -0
- package/dist/functions/stack/index.js +164 -0
- package/dist/functions/tempo/index.d.ts +2 -0
- package/dist/functions/tempo/index.js +37 -0
- package/dist/functions/text/index.d.ts +2 -0
- package/dist/functions/text/index.js +65 -0
- package/dist/functions/tie/index.d.ts +2 -0
- package/dist/functions/tie/index.js +301 -0
- package/dist/functions/voice/index.d.ts +3 -0
- package/dist/functions/voice/index.js +714 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.js +17 -0
- package/dist/layout/default.d.ts +12 -0
- package/dist/layout/default.js +27 -0
- package/dist/layout/engine.d.ts +47 -0
- package/dist/layout/engine.js +410 -0
- package/dist/layout/model.d.ts +73 -0
- package/dist/layout/model.js +422 -0
- package/dist/layout/page.d.ts +27 -0
- package/dist/layout/page.js +121 -0
- package/dist/layout/types.d.ts +197 -0
- package/dist/layout/types.js +1 -0
- package/dist/lowering/loweringContext.d.ts +126 -0
- package/dist/lowering/loweringContext.js +625 -0
- package/dist/lowering/track.d.ts +61 -0
- package/dist/lowering/track.js +35 -0
- package/dist/lowering/types.d.ts +176 -0
- package/dist/lowering/types.js +82 -0
- package/dist/parser/grammarType.d.ts +81 -0
- package/dist/parser/grammarType.js +1 -0
- package/dist/parser/parse-utils/brace-utils.d.ts +1 -0
- package/dist/parser/parse-utils/brace-utils.js +29 -0
- package/dist/parser/parse-utils/call-utils.d.ts +24 -0
- package/dist/parser/parse-utils/call-utils.js +178 -0
- package/dist/parser/parse-utils/label-utils.d.ts +4 -0
- package/dist/parser/parse-utils/label-utils.js +17 -0
- package/dist/parser/parse-utils/length-utils.d.ts +3 -0
- package/dist/parser/parse-utils/length-utils.js +20 -0
- package/dist/parser/parse-utils/note-utils.d.ts +10 -0
- package/dist/parser/parse-utils/note-utils.js +111 -0
- package/dist/parser/parserContext.d.ts +93 -0
- package/dist/parser/parserContext.js +542 -0
- package/dist/parser/preprocess.d.ts +6 -0
- package/dist/parser/preprocess.js +193 -0
- package/dist/parser/types.d.ts +31 -0
- package/dist/parser/types.js +2 -0
- package/dist/pipeline.d.ts +36 -0
- package/dist/pipeline.js +51 -0
- package/dist/render/canvas.d.ts +19 -0
- package/dist/render/canvas.js +121 -0
- package/dist/render/index.d.ts +5 -0
- package/dist/render/index.js +5 -0
- package/dist/render/recording.d.ts +46 -0
- package/dist/render/recording.js +76 -0
- package/dist/render/svg.d.ts +18 -0
- package/dist/render/svg.js +126 -0
- package/dist/render/text.d.ts +6 -0
- package/dist/render/text.js +17 -0
- package/dist/render/types.d.ts +86 -0
- package/dist/render/types.js +1 -0
- package/grammar.md +123 -0
- package/package.json +58 -0
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
import { isVisualTemporalNode, } from "../../lowering/types.js";
|
|
2
|
+
import { ErrorDiagnostic } from "../../diagnostic.js";
|
|
3
|
+
import { claimDivLine, divLinePortName, } from "../div/index.js";
|
|
4
|
+
/** 显式和自动分组共享同一个几何对象 */
|
|
5
|
+
export function createBeamLayoutAttachment(endPoints, explicit = false, sourceSpan) {
|
|
6
|
+
return new BeamLayoutAttachment(endPoints, explicit, sourceSpan);
|
|
7
|
+
}
|
|
8
|
+
/** 显式 beam 必须严格连接最终同轨同谱面行中的相邻可见主体 */
|
|
9
|
+
export function validateExplicitBeamAttachments(result) {
|
|
10
|
+
const tracks = new Map();
|
|
11
|
+
for (const column of result.columns) {
|
|
12
|
+
for (const node of column) {
|
|
13
|
+
if (!isVisualTemporalNode(node))
|
|
14
|
+
continue;
|
|
15
|
+
let lineTracks = tracks.get(node.layoutLine);
|
|
16
|
+
if (!lineTracks) {
|
|
17
|
+
lineTracks = new Map();
|
|
18
|
+
tracks.set(node.layoutLine, lineTracks);
|
|
19
|
+
}
|
|
20
|
+
const sequence = lineTracks.get(node.track);
|
|
21
|
+
if (sequence)
|
|
22
|
+
sequence.push(node);
|
|
23
|
+
else
|
|
24
|
+
lineTracks.set(node.track, [node]);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
const positions = new Map();
|
|
28
|
+
for (const [line, lineTracks] of tracks) {
|
|
29
|
+
for (const [track, sequence] of lineTracks) {
|
|
30
|
+
for (let index = 0; index < sequence.length; index++) {
|
|
31
|
+
positions.set(sequence[index], { line, track, index });
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
for (const attachment of result.attachments) {
|
|
36
|
+
if (!(attachment instanceof BeamLayoutAttachment) || !attachment.explicit)
|
|
37
|
+
continue;
|
|
38
|
+
for (let i = 1; i < attachment.endPoints.length; i++) {
|
|
39
|
+
const firstEndpoint = attachment.endPoints[i - 1];
|
|
40
|
+
const secondEndpoint = attachment.endPoints[i];
|
|
41
|
+
if (!isVisualTemporalNode(firstEndpoint) || !isVisualTemporalNode(secondEndpoint))
|
|
42
|
+
continue;
|
|
43
|
+
const firstPosition = positions.get(firstEndpoint);
|
|
44
|
+
const secondPosition = positions.get(secondEndpoint);
|
|
45
|
+
const adjacent = firstEndpoint !== secondEndpoint
|
|
46
|
+
&& firstPosition !== undefined
|
|
47
|
+
&& secondPosition !== undefined
|
|
48
|
+
&& firstPosition.line === secondPosition.line
|
|
49
|
+
&& firstPosition.track === secondPosition.track
|
|
50
|
+
&& secondPosition.index === firstPosition.index + 1;
|
|
51
|
+
if (adjacent)
|
|
52
|
+
continue;
|
|
53
|
+
throw new ErrorDiagnostic("E_NON_ADJACENT_BEAM", "@beam 的端点必须按时间顺序连接同一轨道、同一谱面行中的相邻可见元素", attachment.sourceSpan ?? attachment.endPoints[i].ast.sourceSpan);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
/** 自动分组在 loweringAugment 当下读取已经注册的显式 beam 端点 */
|
|
58
|
+
export function collectExplicitBeamEndpoints(attachments) {
|
|
59
|
+
const result = new Set();
|
|
60
|
+
for (const attachment of attachments) {
|
|
61
|
+
if (!(attachment instanceof BeamLayoutAttachment) || !attachment.explicit)
|
|
62
|
+
continue;
|
|
63
|
+
for (const endpoint of attachment.endPoints)
|
|
64
|
+
result.add(endpoint);
|
|
65
|
+
}
|
|
66
|
+
return result;
|
|
67
|
+
}
|
|
68
|
+
class BeamLayoutAttachment {
|
|
69
|
+
box = {
|
|
70
|
+
x: 0,
|
|
71
|
+
y: 0,
|
|
72
|
+
w: 0,
|
|
73
|
+
h: 0,
|
|
74
|
+
};
|
|
75
|
+
layer = "foreground";
|
|
76
|
+
endPoints;
|
|
77
|
+
explicit;
|
|
78
|
+
sourceSpan;
|
|
79
|
+
lines = [];
|
|
80
|
+
strokeWidth = 1;
|
|
81
|
+
constructor(endPoints, explicit, sourceSpan) {
|
|
82
|
+
this.endPoints = endPoints;
|
|
83
|
+
this.explicit = explicit;
|
|
84
|
+
this.sourceSpan = sourceSpan;
|
|
85
|
+
}
|
|
86
|
+
/** 同一 beam 组内的相邻元素使用更短的自然弹簧间距 */
|
|
87
|
+
prepareHorizontal(context) {
|
|
88
|
+
const endPoints = new Set(this.endPoints);
|
|
89
|
+
for (const line of context) {
|
|
90
|
+
for (const run of line.trackRuns.values()) {
|
|
91
|
+
for (let i = 1; i < run.length; i++) {
|
|
92
|
+
const left = run[i - 1];
|
|
93
|
+
const right = run[i];
|
|
94
|
+
if (!endPoints.has(left) || !endPoints.has(right))
|
|
95
|
+
continue;
|
|
96
|
+
left.springConfig.alpha_R = left.springConfig.alpha_R * 0.8;
|
|
97
|
+
right.springConfig.alpha_L = right.springConfig.alpha_L * 0.8;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
layout(context) {
|
|
103
|
+
this.updateGeometry(context);
|
|
104
|
+
const halfStroke = this.strokeWidth / 2;
|
|
105
|
+
return this.lines.map(line => ({
|
|
106
|
+
x: Math.min(line.start.x, line.end.x) - halfStroke,
|
|
107
|
+
y: Math.min(line.start.y, line.end.y) - halfStroke,
|
|
108
|
+
w: Math.abs(line.end.x - line.start.x) + this.strokeWidth,
|
|
109
|
+
h: Math.abs(line.end.y - line.start.y) + this.strokeWidth,
|
|
110
|
+
line: line.line,
|
|
111
|
+
track: line.track,
|
|
112
|
+
}));
|
|
113
|
+
}
|
|
114
|
+
paint(painter) {
|
|
115
|
+
for (const line of this.lines) {
|
|
116
|
+
painter.drawLine(line.start.x, line.start.y, line.end.x, line.end.y, { stroke: "#000", strokeWidth: this.strokeWidth });
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
absolutePort(node, name) {
|
|
120
|
+
const port = node.ports[name];
|
|
121
|
+
if (!port)
|
|
122
|
+
return null;
|
|
123
|
+
return {
|
|
124
|
+
x: node.box.x + port.x,
|
|
125
|
+
y: node.box.y + port.y,
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
updateGeometry(context) {
|
|
129
|
+
this.lines.length = 0;
|
|
130
|
+
const available = this.endPoints.filter(isVisualTemporalNode);
|
|
131
|
+
if (available.length < 2)
|
|
132
|
+
return;
|
|
133
|
+
// 选择端点的使用最大字号作为装饰尺寸
|
|
134
|
+
const fontSize = available.reduce((size, endpoint) => Math.max(size, endpoint.ast.size), 0);
|
|
135
|
+
this.strokeWidth = Math.max(1, fontSize * 0.07);
|
|
136
|
+
// 每一级只合并连续提供该级 div 端口的对象
|
|
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
|
+
}
|
|
152
|
+
spans.push({
|
|
153
|
+
left,
|
|
154
|
+
right,
|
|
155
|
+
line: node.layoutLine,
|
|
156
|
+
track: node.track,
|
|
157
|
+
node,
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
if (spans.length > 0)
|
|
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
|
+
}
|
|
168
|
+
}
|
|
169
|
+
// 显式关系允许连接没有 div 端口的任意可见对象
|
|
170
|
+
if (this.lines.length === 0 && this.explicit) {
|
|
171
|
+
const gap = fontSize * 0.12;
|
|
172
|
+
const spans = available.map(node => {
|
|
173
|
+
const point = {
|
|
174
|
+
x: node.box.x + node.box.anchor,
|
|
175
|
+
y: node.box.y + node.box.h + gap,
|
|
176
|
+
};
|
|
177
|
+
return {
|
|
178
|
+
left: point,
|
|
179
|
+
right: point,
|
|
180
|
+
line: node.layoutLine,
|
|
181
|
+
track: node.track,
|
|
182
|
+
node,
|
|
183
|
+
};
|
|
184
|
+
});
|
|
185
|
+
this.addSystemLines(spans, context, null);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
/** 同一级减时线按谱面行拆开,跨行时连接到对应页面边界 */
|
|
189
|
+
addSystemLines(spans, context, connectedLevel) {
|
|
190
|
+
const firstLine = Math.min(...spans.map(item => item.line));
|
|
191
|
+
const lastLine = Math.max(...spans.map(item => item.line));
|
|
192
|
+
const fallbackTrack = spans[0].track;
|
|
193
|
+
const firstVisualAxis = context.getVisualAxis(spans[0].line, fallbackTrack);
|
|
194
|
+
const visualAxisOffset = spans[0].left.y - firstVisualAxis;
|
|
195
|
+
for (let line = firstLine; line <= lastLine; line++) {
|
|
196
|
+
const current = spans
|
|
197
|
+
.filter(item => item.line === line)
|
|
198
|
+
.sort((left, right) => left.left.x - right.left.x);
|
|
199
|
+
if (current.length === 0) {
|
|
200
|
+
const visualAxis = context.getVisualAxis(line, fallbackTrack);
|
|
201
|
+
const y = visualAxis + visualAxisOffset;
|
|
202
|
+
this.lines.push({
|
|
203
|
+
start: { x: context.originX, y },
|
|
204
|
+
end: { x: context.originX + context.width, y },
|
|
205
|
+
line,
|
|
206
|
+
track: fallbackTrack,
|
|
207
|
+
});
|
|
208
|
+
continue;
|
|
209
|
+
}
|
|
210
|
+
const first = current[0];
|
|
211
|
+
const last = current[current.length - 1];
|
|
212
|
+
const y = first.left.y;
|
|
213
|
+
const start = { x: first.left.x, y };
|
|
214
|
+
const end = { x: last.right.x, y };
|
|
215
|
+
if (line > firstLine)
|
|
216
|
+
start.x = context.originX;
|
|
217
|
+
if (line < lastLine)
|
|
218
|
+
end.x = context.originX + context.width;
|
|
219
|
+
if (start.x === end.x && firstLine === lastLine)
|
|
220
|
+
continue;
|
|
221
|
+
if (connectedLevel !== null) {
|
|
222
|
+
for (const span of current)
|
|
223
|
+
claimDivLine(span.node, connectedLevel);
|
|
224
|
+
}
|
|
225
|
+
this.lines.push({
|
|
226
|
+
start,
|
|
227
|
+
end,
|
|
228
|
+
line,
|
|
229
|
+
track: first.track,
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
}
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
import { ASTFunctionNode } from "../ASTtypes.js";
|
|
2
|
+
import { isVisualTemporalNode } from "../../lowering/types.js";
|
|
3
|
+
import { ErrorDiagnostic } from "../../diagnostic.js";
|
|
4
|
+
import { unionLayoutBoxes } from "../../layout/engine.js";
|
|
5
|
+
import { layoutHorizontalRegion } from "../../layout/model.js";
|
|
6
|
+
class BoxFunction extends ASTFunctionNode {
|
|
7
|
+
static def = {
|
|
8
|
+
name: ["box"],
|
|
9
|
+
description: "给目标结构或对象加外框",
|
|
10
|
+
example: `@box(content, padding=0.2em, stroke=0.08em, width=-1px)`,
|
|
11
|
+
allowExtraArgs: false,
|
|
12
|
+
args: [
|
|
13
|
+
{
|
|
14
|
+
type: "content",
|
|
15
|
+
default: null,
|
|
16
|
+
},
|
|
17
|
+
{
|
|
18
|
+
name: "padding",
|
|
19
|
+
type: "length",
|
|
20
|
+
default: {
|
|
21
|
+
value: 0,
|
|
22
|
+
unit: "em",
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
name: "stroke",
|
|
27
|
+
type: "length",
|
|
28
|
+
default: {
|
|
29
|
+
value: 1,
|
|
30
|
+
unit: "px",
|
|
31
|
+
},
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
name: "width",
|
|
35
|
+
type: "length",
|
|
36
|
+
default: {
|
|
37
|
+
value: -1,
|
|
38
|
+
unit: "px",
|
|
39
|
+
},
|
|
40
|
+
},
|
|
41
|
+
],
|
|
42
|
+
};
|
|
43
|
+
target;
|
|
44
|
+
padding;
|
|
45
|
+
stroke;
|
|
46
|
+
width;
|
|
47
|
+
get children() { return [this.target]; }
|
|
48
|
+
timeFlowModel() {
|
|
49
|
+
return {
|
|
50
|
+
children: [this.target],
|
|
51
|
+
mode: "sequence",
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* box 不创建时间事件
|
|
56
|
+
* 进入目标内容前只开始收集所有成员矩形的引用
|
|
57
|
+
*/
|
|
58
|
+
loweringEnter(ctx) {
|
|
59
|
+
const members = [];
|
|
60
|
+
const temporalMembers = [];
|
|
61
|
+
ctx.beginLoweringGroup(this, {
|
|
62
|
+
attachment: new BoxLayoutAttachment(members, temporalMembers, this),
|
|
63
|
+
onTemporal(node) {
|
|
64
|
+
if (!isVisualTemporalNode(node))
|
|
65
|
+
return;
|
|
66
|
+
members.push(node.box);
|
|
67
|
+
temporalMembers.push(node);
|
|
68
|
+
},
|
|
69
|
+
onAttachment(attachment) {
|
|
70
|
+
members.push(attachment.box);
|
|
71
|
+
},
|
|
72
|
+
});
|
|
73
|
+
return [];
|
|
74
|
+
}
|
|
75
|
+
/** 目标内容完成 lowering 后结束当前 box 作用域 */
|
|
76
|
+
loweringExit(ctx) {
|
|
77
|
+
ctx.endLoweringGroup(this);
|
|
78
|
+
return [];
|
|
79
|
+
}
|
|
80
|
+
constructor(sourceSpan, args, ctx, parent = null) {
|
|
81
|
+
super(sourceSpan, parent);
|
|
82
|
+
const [tgt, pad, stroke, width] = this.getArgValue(args, ctx);
|
|
83
|
+
tgt.parent = this;
|
|
84
|
+
this.target = tgt;
|
|
85
|
+
this.padding = Math.max(0, ctx.length2px(pad));
|
|
86
|
+
this.stroke = Math.max(0, ctx.length2px(stroke));
|
|
87
|
+
this.width = ctx.length2px(width);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
export const BoxNode = BoxFunction;
|
|
91
|
+
class BoxLayoutAttachment {
|
|
92
|
+
box = {
|
|
93
|
+
x: 0,
|
|
94
|
+
y: 0,
|
|
95
|
+
w: 0,
|
|
96
|
+
h: 0,
|
|
97
|
+
};
|
|
98
|
+
layer = "background";
|
|
99
|
+
/** 同一个数组会在 lowering 递归过程中持续加入成员盒引用 */
|
|
100
|
+
members;
|
|
101
|
+
temporalMembers;
|
|
102
|
+
owner;
|
|
103
|
+
fixedStart = null;
|
|
104
|
+
wallOffset = 0;
|
|
105
|
+
constructor(members, temporalMembers, owner) {
|
|
106
|
+
this.members = members;
|
|
107
|
+
this.temporalMembers = temporalMembers;
|
|
108
|
+
this.owner = owner;
|
|
109
|
+
}
|
|
110
|
+
/** 定宽 box 在成员覆盖的全局列上运行局部墙布局 */
|
|
111
|
+
prepareHorizontal(context) {
|
|
112
|
+
const layoutLine = this.temporalMembers[0]?.layoutLine;
|
|
113
|
+
if (layoutLine === void 0)
|
|
114
|
+
return;
|
|
115
|
+
if (this.temporalMembers.some(member => member.layoutLine !== layoutLine)) {
|
|
116
|
+
throw new ErrorDiagnostic("E_BOX_CROSS_LINE", "@box 的内容不能跨越谱面行", this.owner.sourceSpan);
|
|
117
|
+
}
|
|
118
|
+
if (this.owner.width <= 0)
|
|
119
|
+
return;
|
|
120
|
+
const line = context[layoutLine];
|
|
121
|
+
if (!line)
|
|
122
|
+
return;
|
|
123
|
+
let first = null;
|
|
124
|
+
let last = null;
|
|
125
|
+
let firstIndex = Infinity;
|
|
126
|
+
let lastIndex = -Infinity;
|
|
127
|
+
let leftInset = 0;
|
|
128
|
+
let rightInset = 0;
|
|
129
|
+
// 首末列可能同时站着多个声部,取其中最宽的固有半宽
|
|
130
|
+
for (const member of this.temporalMembers) {
|
|
131
|
+
const index = line.columnOf(member);
|
|
132
|
+
if (index < 0)
|
|
133
|
+
continue;
|
|
134
|
+
const left = member.box.anchor;
|
|
135
|
+
const right = member.box.w - left;
|
|
136
|
+
if (index < firstIndex) {
|
|
137
|
+
firstIndex = index;
|
|
138
|
+
first = member;
|
|
139
|
+
leftInset = left;
|
|
140
|
+
}
|
|
141
|
+
else if (index === firstIndex)
|
|
142
|
+
leftInset = Math.max(leftInset, left);
|
|
143
|
+
if (index > lastIndex) {
|
|
144
|
+
lastIndex = index;
|
|
145
|
+
last = member;
|
|
146
|
+
rightInset = right;
|
|
147
|
+
}
|
|
148
|
+
else if (index === lastIndex)
|
|
149
|
+
rightInset = Math.max(rightInset, right);
|
|
150
|
+
}
|
|
151
|
+
if (!first || !last)
|
|
152
|
+
return;
|
|
153
|
+
if (this.owner.width < leftInset + rightInset - 1e-6) {
|
|
154
|
+
throw new ErrorDiagnostic("E_BOX_WIDTH_TOO_SMALL", "@box 的宽度小于首末元素的固有宽度", this.owner.sourceSpan);
|
|
155
|
+
}
|
|
156
|
+
line.registerHorizontalLayoutHook(first, last, ({ columns, rows, start, end, X, fixed, options, }) => {
|
|
157
|
+
if (fixed[start - 1] || fixed[end]) {
|
|
158
|
+
throw new ErrorDiagnostic("E_BOX_CONSTRAINT_CROSSING", "定宽 @box 可以嵌套,但不能部分交叉", this.owner.sourceSpan);
|
|
159
|
+
}
|
|
160
|
+
const regionX = X.subarray(start, end + 1);
|
|
161
|
+
layoutHorizontalRegion(columns.slice(start, end + 1), rows, regionX, fixed.subarray(start, end), this.owner.width, options, true);
|
|
162
|
+
const actualWidth = regionX[regionX.length - 1] - regionX[0] + leftInset + rightInset;
|
|
163
|
+
if (Math.abs(actualWidth - this.owner.width) > 1e-6) {
|
|
164
|
+
throw new ErrorDiagnostic("E_BOX_CONSTRAINT_CONFLICT", "多个 @box 为相同内容指定了不同宽度", this.owner.sourceSpan);
|
|
165
|
+
}
|
|
166
|
+
this.fixedStart = first;
|
|
167
|
+
this.wallOffset = -regionX[0];
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* box 必须等所有成员获得最终 x 和 y 后再求边界
|
|
172
|
+
* stroke 以矩形边界为中心绘制,因此外接盒额外包含半个线宽
|
|
173
|
+
*/
|
|
174
|
+
layout() {
|
|
175
|
+
const { padding, stroke } = this.owner;
|
|
176
|
+
const rect = { x: 0, y: 0, w: 0, h: 0 };
|
|
177
|
+
// 未命名歌词等 attachment 会保留全零盒,不能让它把边框拉到文档原点
|
|
178
|
+
if (!unionLayoutBoxes(rect, this.members.filter(member => member.w > 0 || member.h > 0)))
|
|
179
|
+
return [];
|
|
180
|
+
const inset = padding + stroke / 2;
|
|
181
|
+
// 边框不抢轨道纵向空间,只参与画布边界;this.box 由引擎写回
|
|
182
|
+
const fixedX = this.fixedStart
|
|
183
|
+
? this.fixedStart.box.x + this.fixedStart.box.anchor + this.wallOffset
|
|
184
|
+
: rect.x;
|
|
185
|
+
return [{
|
|
186
|
+
x: fixedX - inset,
|
|
187
|
+
y: rect.y - inset,
|
|
188
|
+
w: (this.fixedStart ? this.owner.width : rect.w) + inset * 2,
|
|
189
|
+
h: rect.h + inset * 2,
|
|
190
|
+
}];
|
|
191
|
+
}
|
|
192
|
+
paint(painter) {
|
|
193
|
+
if (this.box.w === 0 && this.box.h === 0)
|
|
194
|
+
return;
|
|
195
|
+
const stroke = this.owner.stroke;
|
|
196
|
+
const inset = stroke / 2;
|
|
197
|
+
painter.drawRect(this.box.x + inset, this.box.y + inset, Math.max(0, this.box.w - stroke), Math.max(0, this.box.h - stroke), {
|
|
198
|
+
stroke: "#000",
|
|
199
|
+
strokeWidth: stroke,
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { ASTFunctionNode } from "../ASTtypes.js";
|
|
2
|
+
import { skipSpaces, skipSpacesBack } from "../../parser/parserContext.js";
|
|
3
|
+
import { TemporalNodeBase } from "../../lowering/types.js";
|
|
4
|
+
import { Diagnostic, WarningDiagnostic } from "../../diagnostic.js";
|
|
5
|
+
class BrFunction extends ASTFunctionNode {
|
|
6
|
+
static def = {
|
|
7
|
+
name: ["br"],
|
|
8
|
+
description: "换行",
|
|
9
|
+
example: `@br(offset=1): 在后续事件前偏移指定行数
|
|
10
|
+
@br() === @br(1)
|
|
11
|
+
@br(2): 空出一行后继续
|
|
12
|
+
语法糖: [仅限顶层]至少连续两个换行
|
|
13
|
+
`,
|
|
14
|
+
allowExtraArgs: false,
|
|
15
|
+
args: [
|
|
16
|
+
{
|
|
17
|
+
name: "offset",
|
|
18
|
+
type: "number",
|
|
19
|
+
default: 1,
|
|
20
|
+
},
|
|
21
|
+
],
|
|
22
|
+
};
|
|
23
|
+
static deSugarAtom(source, start, end, depth) {
|
|
24
|
+
if (depth > 0)
|
|
25
|
+
return null; // 语法糖只在顶层生效
|
|
26
|
+
if (source[start] !== '\n')
|
|
27
|
+
return null;
|
|
28
|
+
// 前一个 \n 必须留在 nodes 里,否则顶层 voice 后的 \n\n 会被吸进 voice 的 children
|
|
29
|
+
if (source[skipSpacesBack(source, start - 1)] !== '\n')
|
|
30
|
+
return null;
|
|
31
|
+
let next = start + 1, p = next;
|
|
32
|
+
while ((p = skipSpaces(source, p, end)) < end && source[p] === '\n')
|
|
33
|
+
next = ++p;
|
|
34
|
+
const node = {
|
|
35
|
+
kind: "call",
|
|
36
|
+
typed: true,
|
|
37
|
+
name: "br",
|
|
38
|
+
args: new Map(),
|
|
39
|
+
span: { start, end: next },
|
|
40
|
+
};
|
|
41
|
+
return { next, node };
|
|
42
|
+
}
|
|
43
|
+
offset;
|
|
44
|
+
loweringEnter() {
|
|
45
|
+
const br = new TemporalNodeBase();
|
|
46
|
+
br.ast = this;
|
|
47
|
+
br.T = 0;
|
|
48
|
+
br.type = 1 /* ColType.SINGLE */;
|
|
49
|
+
br.breakBefore = this.offset;
|
|
50
|
+
return [br];
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* 检查换行是否切断了持续事件
|
|
54
|
+
* 换行会把整条时间线切开,因此跨越换行点的持续事件无法被完整绘制
|
|
55
|
+
*/
|
|
56
|
+
static loweringFinalize = (result) => {
|
|
57
|
+
let latestEnd = -Infinity;
|
|
58
|
+
let latestNode = null;
|
|
59
|
+
for (const column of result.columns) {
|
|
60
|
+
const t = column[0].t;
|
|
61
|
+
if (latestEnd > t + 1e-6 && column.some(node => node.breakBefore > 0)) {
|
|
62
|
+
throw Diagnostic.error.BreakInsideEvent(latestNode.ast.sourceSpan);
|
|
63
|
+
}
|
|
64
|
+
for (const node of column) {
|
|
65
|
+
const end = node.t + node.T;
|
|
66
|
+
if (node.T > 1e-6 && end > latestEnd) {
|
|
67
|
+
latestEnd = end;
|
|
68
|
+
latestNode = node;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
constructor(span, args, ctx, parent = null) {
|
|
74
|
+
super(span, parent);
|
|
75
|
+
const [offset] = this.getArgValue(args, ctx);
|
|
76
|
+
this.offset = Number.isFinite(offset) ? Math.max(1, Math.floor(offset)) : 1;
|
|
77
|
+
if (this.offset !== offset) {
|
|
78
|
+
ctx.diagnostics.push(new WarningDiagnostic("W_BR_OFFSET", `@br 的 offset 必须是大于等于 1 的整数,已被修正为 ${this.offset}`, this.sourceSpan));
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
toString() { return `@br(${this.offset})`; }
|
|
82
|
+
}
|
|
83
|
+
export const BrNode = BrFunction;
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { ASTFunctionNode } from "../ASTtypes.js";
|
|
2
|
+
import { TemporalNodeBase } from "../../lowering/types.js";
|
|
3
|
+
class DashFunction extends ASTFunctionNode {
|
|
4
|
+
static def = {
|
|
5
|
+
name: ["dash", "-"],
|
|
6
|
+
description: "增时线",
|
|
7
|
+
example: `@dash() 创建一根增时线
|
|
8
|
+
语法糖: 一个 '-' 代表一个 @dash()
|
|
9
|
+
`,
|
|
10
|
+
allowExtraArgs: false,
|
|
11
|
+
args: []
|
|
12
|
+
};
|
|
13
|
+
static deSugarAtom(source, start, _end) {
|
|
14
|
+
if (source[start] !== '-')
|
|
15
|
+
return null;
|
|
16
|
+
const node = {
|
|
17
|
+
kind: "call",
|
|
18
|
+
typed: true,
|
|
19
|
+
name: "-",
|
|
20
|
+
args: new Map(),
|
|
21
|
+
span: { start, end: start + 1 },
|
|
22
|
+
syntaxKind: "atom",
|
|
23
|
+
};
|
|
24
|
+
return { next: start + 1, node };
|
|
25
|
+
}
|
|
26
|
+
;
|
|
27
|
+
size;
|
|
28
|
+
constructor(span, _args, ctx, parent = null) {
|
|
29
|
+
super(span, parent);
|
|
30
|
+
this.size = ctx.fontSize;
|
|
31
|
+
}
|
|
32
|
+
loweringEnter() {
|
|
33
|
+
return [new DashTemporalNode(this)];
|
|
34
|
+
}
|
|
35
|
+
toString() { return "-"; }
|
|
36
|
+
}
|
|
37
|
+
export const DashNode = DashFunction;
|
|
38
|
+
class DashTemporalNode extends TemporalNodeBase {
|
|
39
|
+
lineY = 0;
|
|
40
|
+
constructor(ast) {
|
|
41
|
+
super();
|
|
42
|
+
this.ast = ast;
|
|
43
|
+
this.T = 1;
|
|
44
|
+
this.type = 2 /* ColType.DEFAULT */;
|
|
45
|
+
this.initLayoutBox();
|
|
46
|
+
}
|
|
47
|
+
prepareLayout() {
|
|
48
|
+
const size = this.ast.size;
|
|
49
|
+
const width = size * 0.5;
|
|
50
|
+
// dash 与数字音符共享视觉中心和完整字号高度
|
|
51
|
+
// 线本身位于数字视觉中心,不使用极小的 glyph 高度作为轨道高度
|
|
52
|
+
this.box.w = width;
|
|
53
|
+
this.box.h = size;
|
|
54
|
+
this.box.anchor = width / 2;
|
|
55
|
+
this.lineY = size * 0.5;
|
|
56
|
+
this.box.visualAxis = this.lineY;
|
|
57
|
+
}
|
|
58
|
+
paint(painter) {
|
|
59
|
+
painter.drawLine(this.box.x, this.box.y + this.lineY, this.box.x + this.box.w, this.box.y + this.lineY, { stroke: "#000", strokeWidth: Math.max(1, this.ast.size * 0.1) });
|
|
60
|
+
}
|
|
61
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { BarNode } from "./bar/index.js";
|
|
2
|
+
import { BeamNode } from "./beam/index.js";
|
|
3
|
+
import { BoxNode } from "./box/index.js";
|
|
4
|
+
import { DashNode } from "./dash/index.js";
|
|
5
|
+
import { DivNode } from "./div/index.js";
|
|
6
|
+
import { DotNode } from "./dot/index.js";
|
|
7
|
+
import { GraceNode } from "./grace/index.js";
|
|
8
|
+
import { KeyNode } from "./key/index.js";
|
|
9
|
+
import { BrNode } from "./br/index.js";
|
|
10
|
+
import { NoteNode } from "./note/index.js";
|
|
11
|
+
import { UpNode } from "./over/index.js";
|
|
12
|
+
import { PageNode } from "./page/index.js";
|
|
13
|
+
import { StackNode } from "./stack/index.js";
|
|
14
|
+
import { SetNode } from "./set/index.js";
|
|
15
|
+
import { TempoNode } from "./tempo/index.js";
|
|
16
|
+
import { TextNode } from "./text/index.js";
|
|
17
|
+
import { TieNode } from "./tie/index.js";
|
|
18
|
+
import { VoiceNode, VoicesNode } from "./voice/index.js";
|
|
19
|
+
export const defaultFunctions = [
|
|
20
|
+
NoteNode, DashNode, BarNode, // 有实体
|
|
21
|
+
DivNode, DotNode, // 装饰性
|
|
22
|
+
VoiceNode, VoicesNode, // 歌词和声部
|
|
23
|
+
BrNode, // 排版
|
|
24
|
+
PageNode, // 文档页面
|
|
25
|
+
StackNode, // 时间同步
|
|
26
|
+
UpNode,
|
|
27
|
+
GraceNode, // 倚音
|
|
28
|
+
TieNode, BeamNode,
|
|
29
|
+
BoxNode,
|
|
30
|
+
SetNode, KeyNode, TempoNode, // 设置
|
|
31
|
+
TextNode,
|
|
32
|
+
];
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { ASTNodeBase, FunctionArgs, SourceSpan, ParserContext, ASTFunctionNode } from "../ASTtypes.js";
|
|
2
|
+
import { GrammarNode, GrammarSugarNode } from "../../parser/grammarType.js";
|
|
3
|
+
import type { LayoutDecorationHandler, LayoutHost } from "../../layout/types.js";
|
|
4
|
+
import type { LoweringContext } from "../../lowering/loweringContext.js";
|
|
5
|
+
export declare const DIV_ADDON_KEY: "@div";
|
|
6
|
+
/** div 暴露自己的线段端口,不依赖任何关系函数 */
|
|
7
|
+
export declare function divLinePortName(level: number, edge: "left" | "right"): string;
|
|
8
|
+
export declare function claimDivLine(host: LayoutHost, level: number): void;
|
|
9
|
+
declare class DivFunction extends ASTFunctionNode {
|
|
10
|
+
static def: {
|
|
11
|
+
name: string[];
|
|
12
|
+
description: string;
|
|
13
|
+
example: string;
|
|
14
|
+
allowExtraArgs: boolean;
|
|
15
|
+
args: ({
|
|
16
|
+
type: "content";
|
|
17
|
+
default: null;
|
|
18
|
+
name?: undefined;
|
|
19
|
+
} | {
|
|
20
|
+
name: string;
|
|
21
|
+
type: "number";
|
|
22
|
+
default: number;
|
|
23
|
+
})[];
|
|
24
|
+
};
|
|
25
|
+
static deSugarAtom(source: string, start: number, end: number): {
|
|
26
|
+
next: number;
|
|
27
|
+
node: GrammarSugarNode;
|
|
28
|
+
} | null;
|
|
29
|
+
static deSugarRelation(ctx: ParserContext, nodes: (GrammarNode | number)[], at: number): number | null;
|
|
30
|
+
/**
|
|
31
|
+
* 减时线是目标对象下方的通用装饰
|
|
32
|
+
* 多对象自动连线属于 beam 的关系排版,不在这里猜测相邻元素
|
|
33
|
+
*/
|
|
34
|
+
static layoutDecorationHandler: LayoutDecorationHandler;
|
|
35
|
+
loweringEnter(ctx: LoweringContext): never[];
|
|
36
|
+
loweringExit(ctx: LoweringContext): never[];
|
|
37
|
+
content: ASTNodeBase;
|
|
38
|
+
n: number;
|
|
39
|
+
/** parse 时冻结;只控制不同 div 之间的自动连接,不影响当前 div 内部连接 */
|
|
40
|
+
autoBeamEnabled: boolean;
|
|
41
|
+
get children(): ASTNodeBase[];
|
|
42
|
+
timeFlowModel(): {
|
|
43
|
+
children: ASTNodeBase[];
|
|
44
|
+
mode: "sequence";
|
|
45
|
+
};
|
|
46
|
+
constructor(sourceSpan: SourceSpan, args: FunctionArgs, ctx: ParserContext, parent?: ASTNodeBase | null);
|
|
47
|
+
toString(source: string): string;
|
|
48
|
+
}
|
|
49
|
+
export declare const DivNode: typeof DivFunction;
|
|
50
|
+
export {};
|