jpfun 0.5.0 → 0.6.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/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 +19 -12
- package/dist/functions/beam/auto.js +2 -11
- package/dist/functions/box/index.js +52 -1
- package/dist/functions/default.js +2 -1
- package/dist/functions/div/index.js +2 -3
- package/dist/functions/grace/index.d.ts +4 -2
- package/dist/functions/grace/index.js +16 -5
- package/dist/functions/head/index.js +11 -4
- package/dist/functions/meter/index.js +26 -6
- package/dist/functions/meter/shape.d.ts +3 -0
- package/dist/functions/meter/shape.js +43 -0
- package/dist/functions/note/index.js +25 -45
- package/dist/functions/rest/index.d.ts +2 -0
- package/dist/functions/rest/index.js +233 -0
- package/dist/functions/symbol/ornament.js +1 -1
- package/dist/functions/temporal.d.ts +21 -7
- package/dist/functions/temporal.js +14 -4
- package/dist/functions/tie/index.js +14 -10
- package/dist/functions/tuplet/index.js +1 -1
- package/dist/functions/up/index.js +47 -27
- package/dist/functions/voice/connections.d.ts +19 -0
- package/dist/functions/voice/connections.js +88 -0
- package/dist/functions/voice/index.js +244 -99
- package/dist/jpfun.core.min.js +56 -39
- package/dist/jpfun.min.js +65 -48
- package/dist/layout/engine.d.ts +3 -1
- package/dist/layout/engine.js +190 -64
- package/dist/layout/model.js +22 -66
- package/dist/layout/page.js +1 -1
- package/dist/layout/types.d.ts +30 -45
- package/dist/lowering/loweringContext.d.ts +1 -1
- package/dist/lowering/loweringContext.js +34 -4
- package/dist/parser/parserContext.js +4 -8
- package/dist/parser/preprocess.js +4 -6
- package/dist/playback/compile.js +1 -1
- package/dist/render/paint.js +1 -1
- package/grammar.md +200 -257
- package/package.json +1 -1
|
@@ -180,10 +180,6 @@ class NoteTemporalNode extends TemporalNodeBase {
|
|
|
180
180
|
accidentals = [];
|
|
181
181
|
/** 字号和颜色在解析时已经固化,样式随之不再变 */
|
|
182
182
|
numberStyle;
|
|
183
|
-
/** 上八度点属于主体,下八度点通过通用装饰流排在其他下方符号之后 */
|
|
184
|
-
upperOctaveDotY = [];
|
|
185
|
-
lowerOctaveDotY = [];
|
|
186
|
-
octaveDotRadius = 0;
|
|
187
183
|
numberY = 0;
|
|
188
184
|
constructor(ast) {
|
|
189
185
|
super();
|
|
@@ -203,69 +199,57 @@ class NoteTemporalNode extends TemporalNodeBase {
|
|
|
203
199
|
this.initLayoutBox();
|
|
204
200
|
}
|
|
205
201
|
prepareLayout(context) {
|
|
206
|
-
this.upperOctaveDotY.length = 0;
|
|
207
|
-
this.lowerOctaveDotY.length = 0;
|
|
208
202
|
const { size, color } = this.ast;
|
|
209
203
|
const mainMetrics = context.textMeasurer.measureText("0", this.numberStyle);
|
|
210
|
-
|
|
211
|
-
this.octaveDotRadius = size * 0.075;
|
|
212
|
-
const dotGap = size * 0.08;
|
|
213
|
-
const dotStep = this.octaveDotRadius * 2 + dotGap;
|
|
214
|
-
const octaveSpace = dotCount === 0
|
|
215
|
-
? 0
|
|
216
|
-
: dotCount * this.octaveDotRadius * 2 + (dotCount - 1) * dotGap + dotGap;
|
|
217
|
-
const mainY = this.octave > 0 ? octaveSpace : 0;
|
|
218
|
-
this.numberY = mainY + mainMetrics.baseline;
|
|
204
|
+
this.numberY = mainMetrics.baseline;
|
|
219
205
|
// 升降号贴近数字左上角,按同一 baseline 排在数字左侧;
|
|
220
206
|
// 缩小右侧间隔会在全局 anchor 不变时把升降号向右移动
|
|
221
207
|
const accidentalGap = size * 0.01;
|
|
222
208
|
const { placed, right: mainX } = placeAccidentals(this.acc, size, 0, this.numberY, accidentalGap);
|
|
223
209
|
this.accidentals = placed;
|
|
224
210
|
this.box.w = mainX + mainMetrics.w;
|
|
225
|
-
this.box.h =
|
|
211
|
+
this.box.h = mainMetrics.h;
|
|
226
212
|
this.box.anchor = mainX + mainMetrics.w / 2;
|
|
227
|
-
this.box.visualAxis =
|
|
213
|
+
this.box.visualAxis = mainMetrics.h / 2;
|
|
228
214
|
// 升降号和附点参与完整盒布局,但不计入数字主体范围,减时线因此只画在数字下
|
|
229
215
|
this.ports["body.left"] = { x: this.box.anchor - mainMetrics.w / 2, y: this.box.visualAxis };
|
|
230
216
|
this.ports["body.right"] = { x: this.box.anchor + mainMetrics.w / 2, y: this.box.visualAxis };
|
|
231
|
-
this.ports["shoulder"] = { x: this.box.anchor, y:
|
|
217
|
+
this.ports["shoulder"] = { x: this.box.anchor, y: 0 };
|
|
218
|
+
// 下方装饰从数字基线附近起排,不把 em 盒底部的留白算作可见正文。
|
|
219
|
+
this.ports["decoration.below"] = { x: this.box.anchor, y: this.numberY + size * 0.04 };
|
|
232
220
|
// 覆盖 dot 的默认“右边界 + 视觉轴”,让附点贴合数字字形的视觉位置
|
|
233
221
|
this.ports["dot"] = {
|
|
234
222
|
x: this.box.w,
|
|
235
|
-
y:
|
|
223
|
+
y: mainMetrics.h * 0.64,
|
|
236
224
|
};
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
for (let i = 0; i < dotCount; i++) {
|
|
240
|
-
this.upperOctaveDotY.push(mainY - dotGap - this.octaveDotRadius - i * dotStep);
|
|
241
|
-
}
|
|
242
|
-
return;
|
|
243
|
-
}
|
|
244
|
-
if (this.octave >= 0)
|
|
225
|
+
const dotCount = Math.abs(this.octave);
|
|
226
|
+
if (dotCount === 0)
|
|
245
227
|
return;
|
|
246
|
-
const
|
|
247
|
-
const
|
|
248
|
-
|
|
228
|
+
const above = this.octave > 0;
|
|
229
|
+
const dotRadius = size * 0.075;
|
|
230
|
+
const dotGap = size * 0.08;
|
|
231
|
+
const dotStep = dotRadius * 2 + dotGap;
|
|
232
|
+
const dotStyle = { fill: color };
|
|
233
|
+
let dotY = 0;
|
|
234
|
+
const height = dotCount * dotRadius * 2 + (dotCount - 1) * dotGap;
|
|
235
|
+
const dots = {
|
|
236
|
+
[above ? "above" : "below"]: {
|
|
249
237
|
// order 只表达通用的由近到远顺序
|
|
250
238
|
// 时值类装饰可以选择较小 order,下八度点选择较大 order
|
|
251
239
|
order: 100,
|
|
252
240
|
gap: dotGap,
|
|
253
|
-
height
|
|
254
|
-
place: y => {
|
|
255
|
-
// 下八度点从引擎分配的外层区域顶部向下排列
|
|
256
|
-
for (let i = 0; i < dotCount; i++) {
|
|
257
|
-
this.lowerOctaveDotY.push(y + this.octaveDotRadius + i * dotStep);
|
|
258
|
-
}
|
|
259
|
-
},
|
|
241
|
+
height,
|
|
242
|
+
place: (y) => { dotY = y; },
|
|
260
243
|
},
|
|
261
244
|
paint: painter => {
|
|
262
|
-
//
|
|
263
|
-
for (
|
|
264
|
-
|
|
245
|
+
// 每列八度点共用一个装饰,上方从靠近数字的位置向外绘制
|
|
246
|
+
for (let i = 0; i < dotCount; i++) {
|
|
247
|
+
const y = above ? dotY + height - dotRadius - i * dotStep : dotY + dotRadius + i * dotStep;
|
|
248
|
+
painter.drawCircle(this.box.x + this.box.anchor, this.box.y + y, dotRadius, dotStyle);
|
|
265
249
|
}
|
|
266
250
|
},
|
|
267
251
|
};
|
|
268
|
-
this.decorations.push(
|
|
252
|
+
this.decorations.push(dots);
|
|
269
253
|
}
|
|
270
254
|
finalizeLayout() {
|
|
271
255
|
this.ports["lyric"] = { x: this.box.anchor, y: this.box.h };
|
|
@@ -278,10 +262,6 @@ class NoteTemporalNode extends TemporalNodeBase {
|
|
|
278
262
|
if (glyph) {
|
|
279
263
|
painter.drawText(glyph, this.box.x + this.box.anchor, this.box.y + this.numberY, this.numberStyle);
|
|
280
264
|
}
|
|
281
|
-
// 上八度点属于主体绘制,不参与下方装饰流
|
|
282
|
-
for (const y of this.upperOctaveDotY) {
|
|
283
|
-
painter.drawCircle(this.box.x + this.box.anchor, this.box.y + y, this.octaveDotRadius, { fill: color });
|
|
284
|
-
}
|
|
285
265
|
}
|
|
286
266
|
/** 固化记谱位置的音高、力度和音色,并保留基于该调性的相对移调能力 */
|
|
287
267
|
onTimeState(state) {
|
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
import { ErrorDiagnostic } from "../../diagnostic.js";
|
|
2
|
+
import { Fraction } from "../../fraction.js";
|
|
3
|
+
import { ASTFunctionNode, ASTNodeBase, } from "../ASTtypes.js";
|
|
4
|
+
import { ANCHOR_KEY, isVisualTemporalNode, TemporalNodeBase } from "../temporal.js";
|
|
5
|
+
// 同刻状态先固化,休止只与休止合列,普通音符随后进入
|
|
6
|
+
const REST_KEY = Number.MAX_VALUE;
|
|
7
|
+
const restsByHost = new WeakMap();
|
|
8
|
+
class RestFunction extends ASTFunctionNode {
|
|
9
|
+
static def = {
|
|
10
|
+
name: ["rest"],
|
|
11
|
+
description: "多小节休止",
|
|
12
|
+
details: `\
|
|
13
|
+
~~~jpfun
|
|
14
|
+
@meter(4,4) | @rest(19) |
|
|
15
|
+
~~~
|
|
16
|
+
使用当前拍号产生 19 个整小节的静音(必须先声明拍号)`,
|
|
17
|
+
allowExtraArgs: false,
|
|
18
|
+
args: [
|
|
19
|
+
{
|
|
20
|
+
name: "n",
|
|
21
|
+
description: "休止的小节数",
|
|
22
|
+
type: "number",
|
|
23
|
+
default: 1
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
name: "width",
|
|
27
|
+
description: "横线的最小宽度",
|
|
28
|
+
type: "length",
|
|
29
|
+
default: { value: 3, unit: "em" }
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
name: "size",
|
|
33
|
+
description: "数字字号与笔画尺寸",
|
|
34
|
+
type: "length",
|
|
35
|
+
default: { value: 1, unit: "em" }
|
|
36
|
+
},
|
|
37
|
+
],
|
|
38
|
+
};
|
|
39
|
+
count;
|
|
40
|
+
width;
|
|
41
|
+
size;
|
|
42
|
+
font;
|
|
43
|
+
constructor(span, args, ctx, parent = null) {
|
|
44
|
+
super(span, parent);
|
|
45
|
+
const [count, width, size] = this.getArgValue(args, ctx);
|
|
46
|
+
if (!Number.isSafeInteger(count) || count <= 0 || count > 0xffffffff) {
|
|
47
|
+
throw new ErrorDiagnostic("E_REST_COUNT", "@rest 的 n 必须是正安全整数,且小节线数量可由数组表示", span);
|
|
48
|
+
}
|
|
49
|
+
this.count = count;
|
|
50
|
+
this.width = ctx.length2px(width);
|
|
51
|
+
this.size = ctx.length2px(size);
|
|
52
|
+
if (!Number.isFinite(this.width) || this.width < 0 || !Number.isFinite(this.size) || this.size <= 0) {
|
|
53
|
+
throw new ErrorDiagnostic("E_REST_SIZE", "@rest 的 width 必须有限且非负,size 必须有限且为正", span);
|
|
54
|
+
}
|
|
55
|
+
this.font = ctx.variables.numberfont;
|
|
56
|
+
}
|
|
57
|
+
labelable() { return null; }
|
|
58
|
+
loweringEnter() {
|
|
59
|
+
return [new RestTemporal(this)];
|
|
60
|
+
}
|
|
61
|
+
static loweringFinalize(result) {
|
|
62
|
+
// 没有全局休止列时,跳过索引扫描和边界补入
|
|
63
|
+
if (!result.columns.some(column => column.some(node => node.mergeKey === REST_KEY)))
|
|
64
|
+
return;
|
|
65
|
+
let order = 0;
|
|
66
|
+
const onsets = new Map();
|
|
67
|
+
// 扫描完整来源索引,纳入未直接出现在时间列中的折叠成员
|
|
68
|
+
for (const nodes of result.astToTemporal.values()) {
|
|
69
|
+
// 为新增边界预留唯一 order,并记录各宿主的休止代表和最早音符
|
|
70
|
+
for (const node of nodes) {
|
|
71
|
+
order = Math.max(order, node.order + 1);
|
|
72
|
+
if (!(node instanceof RestTemporal)
|
|
73
|
+
&& (!(node.ast instanceof ASTFunctionNode)
|
|
74
|
+
|| (node.ast.callName !== "note" && node.ast.callName !== "dash")))
|
|
75
|
+
continue;
|
|
76
|
+
let host = node;
|
|
77
|
+
// 沿折叠链找到最外层宿主,将内部成员归到同一主体
|
|
78
|
+
while (host.foldedInto)
|
|
79
|
+
host = host.foldedInto;
|
|
80
|
+
// 私有倚音属于独立轨道,不代表外层宿主的休止或起音
|
|
81
|
+
if (node.track !== host.track)
|
|
82
|
+
continue;
|
|
83
|
+
if (node instanceof RestTemporal) {
|
|
84
|
+
// 非宿主休止不接管外层时间列,同轨最早休止代表宿主
|
|
85
|
+
if (host.mergeKey !== REST_KEY)
|
|
86
|
+
continue;
|
|
87
|
+
const rest = restsByHost.get(host);
|
|
88
|
+
if (!rest || node.order < rest.order)
|
|
89
|
+
restsByHost.set(host, node);
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
const onset = onsets.get(host);
|
|
93
|
+
if (!onset || node.order < onset.order)
|
|
94
|
+
onsets.set(host, node);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
const columns = [];
|
|
98
|
+
const previous = new Map();
|
|
99
|
+
// 保留原时间列,并在休止列后追加其内部小节边界
|
|
100
|
+
for (const column of result.columns) {
|
|
101
|
+
columns.push(column);
|
|
102
|
+
let longest;
|
|
103
|
+
// 检查各轨是否用增时线延续休止,同时选出同列最长休止
|
|
104
|
+
for (const node of column) {
|
|
105
|
+
if (node.T.compare(0) > 0) {
|
|
106
|
+
const onset = onsets.get(node);
|
|
107
|
+
const before = previous.get(node.track);
|
|
108
|
+
if (before && restsByHost.has(before)
|
|
109
|
+
&& onset?.ast instanceof ASTFunctionNode && onset.ast.callName === "dash") {
|
|
110
|
+
throw new ErrorDiagnostic("E_REST_SUSTAIN", "增时线不能延续整小节休止", onset.ast.sourceSpan);
|
|
111
|
+
}
|
|
112
|
+
previous.set(node.track, node);
|
|
113
|
+
}
|
|
114
|
+
const rest = restsByHost.get(node);
|
|
115
|
+
if (rest && (!longest || rest.T.compare(longest.T) > 0))
|
|
116
|
+
longest = rest;
|
|
117
|
+
}
|
|
118
|
+
if (!longest)
|
|
119
|
+
continue;
|
|
120
|
+
const time = longest.t.clone();
|
|
121
|
+
// 按小节时长补入 n−1 个不可见边界;时间已固化,不再参与声部归并
|
|
122
|
+
for (let index = 1; index < longest.ast.count; index++) {
|
|
123
|
+
time.add(longest.measureDuration);
|
|
124
|
+
const boundary = new TemporalNodeBase();
|
|
125
|
+
boundary.ast = new ASTNodeBase(longest.ast.sourceSpan, longest.ast);
|
|
126
|
+
boundary.t.copyFrom(time);
|
|
127
|
+
boundary.track = longest.track;
|
|
128
|
+
boundary.layoutLine = longest.layoutLine;
|
|
129
|
+
boundary.order = order++;
|
|
130
|
+
boundary.mergeKey = ANCHOR_KEY;
|
|
131
|
+
result.astToTemporal.set(boundary.ast, [boundary]);
|
|
132
|
+
columns.push([boundary]);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
result.columns = columns;
|
|
136
|
+
}
|
|
137
|
+
toString() { return `@rest(${this.count})`; }
|
|
138
|
+
}
|
|
139
|
+
export const RestNode = RestFunction;
|
|
140
|
+
class RestTemporal extends TemporalNodeBase {
|
|
141
|
+
measureDuration = new Fraction();
|
|
142
|
+
metrics;
|
|
143
|
+
lineWidth = 0;
|
|
144
|
+
textStyle;
|
|
145
|
+
constructor(ast) {
|
|
146
|
+
super();
|
|
147
|
+
this.ast = ast;
|
|
148
|
+
this.mergeKey = REST_KEY;
|
|
149
|
+
restsByHost.set(this, this);
|
|
150
|
+
this.initLayoutBox();
|
|
151
|
+
this.textStyle = { fontSize: ast.size, fontFamily: ast.font, textAlign: "center" };
|
|
152
|
+
}
|
|
153
|
+
onTimeState(state) {
|
|
154
|
+
const measure = state.meter;
|
|
155
|
+
if (!(measure instanceof Fraction)) {
|
|
156
|
+
throw new ErrorDiagnostic("E_REST_METER", "整小节休止前必须有实际生效的显式拍号", this.ast.sourceSpan);
|
|
157
|
+
}
|
|
158
|
+
if (!Number.isSafeInteger(measure.numerator * this.ast.count)) {
|
|
159
|
+
throw new ErrorDiagnostic("E_REST_COUNT", "@rest 的总时值超出精确整数范围", this.ast.sourceSpan);
|
|
160
|
+
}
|
|
161
|
+
this.measureDuration.copyFrom(measure);
|
|
162
|
+
this.T.copyFrom(measure).mul(this.ast.count);
|
|
163
|
+
this.playbackState = { bpm: state.bpm };
|
|
164
|
+
return this.T;
|
|
165
|
+
}
|
|
166
|
+
emitPlayback(emitter) {
|
|
167
|
+
emitter.span({ start: emitter.start, end: emitter.end });
|
|
168
|
+
}
|
|
169
|
+
prepareLayout(context) {
|
|
170
|
+
const size = this.ast.size;
|
|
171
|
+
// 抵消布局器的时长平方根缩放,左右弹簧自然长度固定为 6px
|
|
172
|
+
const alpha = 6 / Math.sqrt(this.T.toNumber());
|
|
173
|
+
this.springConfig = { alpha_L: alpha, alpha_R: alpha };
|
|
174
|
+
this.metrics = context.textMeasurer.measureText(String(this.ast.count), this.textStyle);
|
|
175
|
+
this.lineWidth = Math.max(this.ast.width, this.metrics.w + size * 0.5);
|
|
176
|
+
this.box.w = this.lineWidth + size * 0.25;
|
|
177
|
+
this.box.h = this.metrics.h + size * 0.5;
|
|
178
|
+
this.box.anchor = size * 0.4;
|
|
179
|
+
this.box.visualAxis = this.box.h - size * 0.65 / 2;
|
|
180
|
+
}
|
|
181
|
+
prepareHorizontal(line) {
|
|
182
|
+
let host = this;
|
|
183
|
+
// 向外寻找实际占据布局列的宿主,让被包装的休止也能注册横向约束
|
|
184
|
+
while (line.columnOf(host) < 0 && host.foldedInto && isVisualTemporalNode(host.foldedInto))
|
|
185
|
+
host = host.foldedInto;
|
|
186
|
+
const column = line.columnOf(host);
|
|
187
|
+
if (column < 0)
|
|
188
|
+
return;
|
|
189
|
+
const barAt = (index) => line.columns[index]?.find(node => node.track === host.track && node.mergeKey === ANCHOR_KEY) ?? host;
|
|
190
|
+
const first = barAt(column - 1);
|
|
191
|
+
const last = barAt(column + 1);
|
|
192
|
+
if (first === last)
|
|
193
|
+
return;
|
|
194
|
+
line.registerHorizontalLayoutHook(first, last, ({ columns, rows, start, end, X, fixed }) => {
|
|
195
|
+
// 锁定休止及相邻小节线之间的列间距,撑满行宽时也不拉开
|
|
196
|
+
for (let i = start; i < end; i++) {
|
|
197
|
+
if (fixed[i])
|
|
198
|
+
continue;
|
|
199
|
+
let distance = 0;
|
|
200
|
+
// 取所有声部所需间距的最大值,避免压紧休止时造成其他声部交叠
|
|
201
|
+
for (let row = 0; row < rows; row++) {
|
|
202
|
+
const left = columns[i][row];
|
|
203
|
+
const right = columns[i + 1][row];
|
|
204
|
+
// 右侧留隙已在休止盒内,只给左侧补同样的墨迹间距
|
|
205
|
+
const rest = restsByHost.get(right.time);
|
|
206
|
+
const gap = rest ? rest.ast.size * 0.25 : 0;
|
|
207
|
+
distance = Math.max(distance, left.WR + right.WL + gap);
|
|
208
|
+
}
|
|
209
|
+
const shift = X[i] + distance - X[i + 1];
|
|
210
|
+
// 平移右侧已锁定的块及其末列,保留连续休止先前确定的间隙
|
|
211
|
+
for (let j = i + 1; j < X.length; j++) {
|
|
212
|
+
X[j] += shift;
|
|
213
|
+
if (!fixed[j])
|
|
214
|
+
break;
|
|
215
|
+
}
|
|
216
|
+
fixed[i] = 1;
|
|
217
|
+
}
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
paint(painter) {
|
|
221
|
+
const { size, count } = this.ast;
|
|
222
|
+
const left = this.box.x;
|
|
223
|
+
const right = left + this.lineWidth;
|
|
224
|
+
const axis = this.box.y + this.box.visualAxis;
|
|
225
|
+
const stroke = size * 0.12;
|
|
226
|
+
const barStroke = stroke * 2;
|
|
227
|
+
const halfCap = (size * 0.65 - stroke) / 2;
|
|
228
|
+
painter.drawLine(left + barStroke / 2, axis, right - barStroke / 2, axis, { strokeWidth: barStroke });
|
|
229
|
+
painter.drawLine(left + stroke / 2, axis - halfCap, left + stroke / 2, axis + halfCap, { strokeWidth: stroke });
|
|
230
|
+
painter.drawLine(right - stroke / 2, axis - halfCap, right - stroke / 2, axis + halfCap, { strokeWidth: stroke });
|
|
231
|
+
painter.drawText(String(count), left + this.lineWidth / 2, this.box.y + this.metrics.baseline, this.textStyle);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
@@ -29,7 +29,7 @@ export function ornament(pattern) {
|
|
|
29
29
|
// 用 Fraction 等分最终区间,保证首尾边界准确且不会积累浮点时间误差。
|
|
30
30
|
for (let index = 0; index < steps.length; index++) {
|
|
31
31
|
const offset = steps[index];
|
|
32
|
-
const start =
|
|
32
|
+
const start = step.clone().mul(index).add(note.start);
|
|
33
33
|
result.push({
|
|
34
34
|
...note,
|
|
35
35
|
start,
|
|
@@ -12,7 +12,7 @@ import type { Painter } from "../render/types.js";
|
|
|
12
12
|
* 沿记谱顺序流动的时间状态
|
|
13
13
|
*
|
|
14
14
|
* 速度、力度、调性、音色是系统级字段:有确定的类型和初值,任何时刻都能直接读,不用写兼容分支。
|
|
15
|
-
*
|
|
15
|
+
* 拍号仅在显式设置后有值;其余自定义键由具体函数约定,核心不认识。
|
|
16
16
|
*
|
|
17
17
|
* velocity 和 program 按音轨各自流动,新音轨继承分叉处父轨的值;其余键整篇共享。
|
|
18
18
|
*/
|
|
@@ -21,6 +21,8 @@ export interface TimeState {
|
|
|
21
21
|
velocity: number;
|
|
22
22
|
keySignature: string;
|
|
23
23
|
program: number;
|
|
24
|
+
/** 当前小节时长(QN);尚无显式拍号或散板时为 undefined */
|
|
25
|
+
meter?: Fraction;
|
|
24
26
|
[key: string]: any;
|
|
25
27
|
}
|
|
26
28
|
export declare const DEFAULT_BPM = 120;
|
|
@@ -69,14 +71,26 @@ export declare class TemporalNodeBase implements TimeLineEvent {
|
|
|
69
71
|
layoutLine: number;
|
|
70
72
|
/** 不可见状态事件保持 undefined */
|
|
71
73
|
box?: LayoutBox;
|
|
74
|
+
/** 显式微调时才创建后置平移,折叠成员还会继承外层对象的平移 */
|
|
75
|
+
placementOffset?: LayoutPoint;
|
|
72
76
|
/**
|
|
73
77
|
* 横向弹簧布局参数
|
|
74
78
|
* 大部分对象使用空对象即可采用布局器默认值
|
|
75
79
|
*/
|
|
76
80
|
springConfig: HorizontalSpringConfig;
|
|
77
81
|
/**
|
|
78
|
-
*
|
|
79
|
-
*
|
|
82
|
+
* 装饰与关系函数使用的命名局部坐标,相对 box 左上角,每轮布局重建。
|
|
83
|
+
*
|
|
84
|
+
* 当前内置端口:
|
|
85
|
+
* - body.left / body.right:主体核心左右边界,供 div、tuplet、volta 使用;缺省为盒左右边界。
|
|
86
|
+
* - decoration.below:下方装饰排列起点(只读 y);缺省为正文 box.h。
|
|
87
|
+
* - shoulder:倚音的纵向定位基准(只读 y);音符提供数字顶边,缺省为盒顶。
|
|
88
|
+
* - tie.top:连音线端点;缺省为 anchor 与内容上沿,up 提供最上层成员的端点。
|
|
89
|
+
* - div.{level}.left / div.{level}.right:从 0 编号的减时线端点,由 div.place 发布,供 beam 接管。
|
|
90
|
+
* - dot:附点起点;缺省为盒右边界与 visualAxis,音符覆盖为字形附近的位置。
|
|
91
|
+
* - lyric:歌词对齐点(voice 只读 x);未提供则不作为歌词目标,note 在 finalizeLayout 发布。
|
|
92
|
+
*
|
|
93
|
+
* up、grace、arpeggio 转发宿主端口并换算局部坐标;端口不表达装饰是否存在。
|
|
80
94
|
*/
|
|
81
95
|
ports: Record<string, LayoutPoint>;
|
|
82
96
|
/**
|
|
@@ -89,8 +103,8 @@ export declare class TemporalNodeBase implements TimeLineEvent {
|
|
|
89
103
|
* 1. layout 的 prepareLayoutHost 开始时创建或清空数组;
|
|
90
104
|
* 2. Temporal.prepareLayout 可先加入节点自身的装饰,例如下八度点;
|
|
91
105
|
* 3. 引擎再通过 addon 对应的 layoutDecorationHandler 加入函数装饰;
|
|
92
|
-
* 4.
|
|
93
|
-
* 5. 最终 paintLayout 在主体 paint
|
|
106
|
+
* 4. 引擎分别按 above/below.order 分配空间并调用 place
|
|
107
|
+
* 5. 最终 paintLayout 在主体 paint 后调用可选的 decoration.paint
|
|
94
108
|
*
|
|
95
109
|
* 数组必须保留到绘制结束,因为 decoration 通常以闭包保存本次测量得到的几何。它只属于当前 layout pass,不能跨 pass 复用。
|
|
96
110
|
* prepare 阶段生成、place 后冻结、paint 后失效
|
|
@@ -103,9 +117,9 @@ export declare class TemporalNodeBase implements TimeLineEvent {
|
|
|
103
117
|
protected initLayoutBox(): LayoutBox;
|
|
104
118
|
/**
|
|
105
119
|
* 时间状态 修改&冻结 入口
|
|
106
|
-
*
|
|
120
|
+
* 当前列定位后固化状态;偏移仅支持非负值,整列取最大值累加到后续列
|
|
107
121
|
*/
|
|
108
|
-
onTimeState?(state: TimeState): void;
|
|
122
|
+
onTimeState?(state: TimeState): Fraction | void;
|
|
109
123
|
/** 把已固化的音乐语义声明为播放事件;不执行实时播放 */
|
|
110
124
|
emitPlayback?(emitter: PlaybackEmitter): void;
|
|
111
125
|
/** 给自己所在的时间列贴播放标记,供控制流游标查询 */
|
|
@@ -53,8 +53,18 @@ export class TemporalNodeBase {
|
|
|
53
53
|
*/
|
|
54
54
|
springConfig;
|
|
55
55
|
/**
|
|
56
|
-
*
|
|
57
|
-
*
|
|
56
|
+
* 装饰与关系函数使用的命名局部坐标,相对 box 左上角,每轮布局重建。
|
|
57
|
+
*
|
|
58
|
+
* 当前内置端口:
|
|
59
|
+
* - body.left / body.right:主体核心左右边界,供 div、tuplet、volta 使用;缺省为盒左右边界。
|
|
60
|
+
* - decoration.below:下方装饰排列起点(只读 y);缺省为正文 box.h。
|
|
61
|
+
* - shoulder:倚音的纵向定位基准(只读 y);音符提供数字顶边,缺省为盒顶。
|
|
62
|
+
* - tie.top:连音线端点;缺省为 anchor 与内容上沿,up 提供最上层成员的端点。
|
|
63
|
+
* - div.{level}.left / div.{level}.right:从 0 编号的减时线端点,由 div.place 发布,供 beam 接管。
|
|
64
|
+
* - dot:附点起点;缺省为盒右边界与 visualAxis,音符覆盖为字形附近的位置。
|
|
65
|
+
* - lyric:歌词对齐点(voice 只读 x);未提供则不作为歌词目标,note 在 finalizeLayout 发布。
|
|
66
|
+
*
|
|
67
|
+
* up、grace、arpeggio 转发宿主端口并换算局部坐标;端口不表达装饰是否存在。
|
|
58
68
|
*/
|
|
59
69
|
ports;
|
|
60
70
|
/**
|
|
@@ -67,8 +77,8 @@ export class TemporalNodeBase {
|
|
|
67
77
|
* 1. layout 的 prepareLayoutHost 开始时创建或清空数组;
|
|
68
78
|
* 2. Temporal.prepareLayout 可先加入节点自身的装饰,例如下八度点;
|
|
69
79
|
* 3. 引擎再通过 addon 对应的 layoutDecorationHandler 加入函数装饰;
|
|
70
|
-
* 4.
|
|
71
|
-
* 5. 最终 paintLayout 在主体 paint
|
|
80
|
+
* 4. 引擎分别按 above/below.order 分配空间并调用 place
|
|
81
|
+
* 5. 最终 paintLayout 在主体 paint 后调用可选的 decoration.paint
|
|
72
82
|
*
|
|
73
83
|
* 数组必须保留到绘制结束,因为 decoration 通常以闭包保存本次测量得到的几何。它只属于当前 layout pass,不能跨 pass 复用。
|
|
74
84
|
* prepare 阶段生成、place 后冻结、paint 后失效
|
|
@@ -2,6 +2,7 @@ import { ASTFunctionNode } from "../ASTtypes.js";
|
|
|
2
2
|
import { Diagnostic, ErrorDiagnostic } from "../../diagnostic.js";
|
|
3
3
|
import { isVisualTemporalNode, } from "../temporal.js";
|
|
4
4
|
import { pathBounds } from "../../layout/path.js";
|
|
5
|
+
import { getLayoutBounds } from "../../layout/engine.js";
|
|
5
6
|
/** 三次贝塞尔控制点的抬高系数:两个控制点同高时,实际弧高恰好是抬高的 3/4 */
|
|
6
7
|
const CUBIC_LIFT = 4 / 3;
|
|
7
8
|
/** 弧带中部最大厚度与字号之比 */
|
|
@@ -15,25 +16,28 @@ class TieFunction extends ASTFunctionNode {
|
|
|
15
16
|
1@a 2 3@b @tie(a, b, height=0.5em)
|
|
16
17
|
~~~
|
|
17
18
|
- **位置参数**:端点标签,可传多个,按顺序连接;省略时查找最近的可标记对象,包括增时线
|
|
18
|
-
- **height
|
|
19
|
+
- **height**:仅命名的弧线高度参数,默认 \`0.5em\`;可用 \`@set(tie.height=0.8em)\` 设置局部默认值,显式传参优先
|
|
19
20
|
|
|
20
21
|
同行端点用一条弧线连接,跨行时自动拆成多段。同轨同音且时间连续的音段建立播放连接,各段保留自己的装饰,例如 \`1 ^ $tr 1 @tie()\` 的后一音段不继承颤音。涉及增时线的连接仅绘制,不合并播放音符。`,
|
|
21
22
|
allowExtraArgs: true,
|
|
22
23
|
extraArgType: "label",
|
|
23
|
-
args: [
|
|
24
|
+
args: [{
|
|
25
|
+
name: "height",
|
|
26
|
+
namedOnly: true,
|
|
27
|
+
description: "弧线高度,支持 px/em;em 相对调用处字号,负值按 0 处理",
|
|
28
|
+
type: "length",
|
|
29
|
+
default: { value: 0.5, unit: "em" },
|
|
30
|
+
}]
|
|
24
31
|
};
|
|
25
32
|
endPoints = [];
|
|
26
33
|
height;
|
|
27
34
|
constructor(sourceSpan, args, ctx, parent = null) {
|
|
28
35
|
super(sourceSpan, parent);
|
|
29
|
-
|
|
36
|
+
// 命名选项统一解析默认值,位置参数仍全部作为端点
|
|
37
|
+
const [height] = this.getArgValue(args, ctx);
|
|
30
38
|
for (const [key, value] of args) {
|
|
31
|
-
if (key === "height")
|
|
32
|
-
const length = ctx.parseArgWithType(value.valueSpan, "length", sourceSpan.start);
|
|
33
|
-
if (length)
|
|
34
|
-
height = Math.max(0, ctx.length2px(length));
|
|
39
|
+
if (key === "height")
|
|
35
40
|
continue;
|
|
36
|
-
}
|
|
37
41
|
const v = value instanceof ASTFunctionNode
|
|
38
42
|
? value
|
|
39
43
|
: ctx.parseArgWithType(value.valueSpan, "label", sourceSpan.start);
|
|
@@ -50,7 +54,7 @@ class TieFunction extends ASTFunctionNode {
|
|
|
50
54
|
}
|
|
51
55
|
if (this.endPoints.length < 2)
|
|
52
56
|
throw new ErrorDiagnostic("E_NOT_ENOUGH_ARGS", "@tie 连音线需要至少两个端点", sourceSpan);
|
|
53
|
-
this.height = height;
|
|
57
|
+
this.height = Math.max(0, ctx.length2px(height));
|
|
54
58
|
}
|
|
55
59
|
/**
|
|
56
60
|
* tie 不推进时间
|
|
@@ -157,7 +161,7 @@ class TieLayoutAttachment {
|
|
|
157
161
|
}
|
|
158
162
|
return {
|
|
159
163
|
x: node.box.x + node.box.anchor,
|
|
160
|
-
y: node.
|
|
164
|
+
y: getLayoutBounds(node, true).y,
|
|
161
165
|
};
|
|
162
166
|
}
|
|
163
167
|
/** 根据端点固化后的行号选择同行弧线或跨行分段 */
|
|
@@ -77,7 +77,7 @@ class TupletFunction extends ASTFunctionNode {
|
|
|
77
77
|
shortest.copyFrom(event.T);
|
|
78
78
|
writtenTotal.add(event.T);
|
|
79
79
|
}
|
|
80
|
-
const units = writtenTotal.
|
|
80
|
+
const units = writtenTotal.div(shortest);
|
|
81
81
|
const actual = units.numerator;
|
|
82
82
|
if (units.denominator !== 1 || actual < 2 || !Number.isSafeInteger(actual)) {
|
|
83
83
|
throw new ErrorDiagnostic("E_TUPLET_NON_INTEGRAL_UNITS", "@tuplet 的总时值必须是最短正时值的整数倍,且至少包含2个单位", this.sourceSpan);
|