jpfun 0.4.1 → 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/README.md +53 -113
- package/dist/converter/midi/convert.js +14 -7
- package/dist/converter/musicxml/features.d.ts +57 -16
- package/dist/converter/musicxml/features.js +204 -42
- package/dist/converter/musicxml/index.d.ts +7 -2
- package/dist/converter/musicxml/index.js +256 -268
- package/dist/converter/musicxml/model.d.ts +36 -7
- package/dist/converter/musicxml/resolve.d.ts +10 -0
- package/dist/converter/musicxml/resolve.js +152 -0
- package/dist/core.d.ts +1 -0
- package/dist/core.js +1 -0
- package/dist/fraction.js +7 -1
- package/dist/functions/ASTtypes.d.ts +6 -3
- package/dist/functions/ASTtypes.js +30 -23
- package/dist/functions/adjust/index.js +10 -50
- package/dist/functions/arpeggio/index.js +40 -25
- package/dist/functions/beam/auto.js +2 -11
- package/dist/functions/box/index.js +132 -82
- package/dist/functions/dash/index.js +20 -11
- package/dist/functions/default.js +2 -1
- package/dist/functions/div/index.d.ts +1 -1
- package/dist/functions/div/index.js +9 -10
- package/dist/functions/dot/index.js +4 -5
- package/dist/functions/dyn/index.js +28 -32
- package/dist/functions/grace/index.d.ts +23 -8
- package/dist/functions/grace/index.js +138 -119
- package/dist/functions/head/index.js +11 -4
- package/dist/functions/key/index.js +1 -1
- package/dist/functions/meter/index.js +40 -7
- package/dist/functions/meter/shape.d.ts +3 -0
- package/dist/functions/meter/shape.js +43 -0
- package/dist/functions/note/index.d.ts +6 -1
- package/dist/functions/note/index.js +60 -57
- package/dist/functions/note/source-transform.d.ts +21 -0
- package/dist/functions/note/source-transform.js +164 -0
- package/dist/functions/page/index.d.ts +5 -0
- package/dist/functions/page/index.js +8 -5
- package/dist/functions/rest/index.d.ts +2 -0
- package/dist/functions/rest/index.js +233 -0
- package/dist/functions/set/index.js +5 -1
- package/dist/functions/symbol/index.d.ts +2 -0
- package/dist/functions/symbol/index.js +1 -1
- package/dist/functions/symbol/ornament.d.ts +3 -3
- package/dist/functions/symbol/ornament.js +21 -35
- package/dist/functions/symbol/symbols/accent.js +5 -5
- package/dist/functions/symbol/symbols/fermata.js +5 -4
- package/dist/functions/temporal.d.ts +21 -7
- package/dist/functions/temporal.js +14 -4
- package/dist/functions/tie/index.js +36 -42
- package/dist/functions/tuplet/index.js +20 -24
- package/dist/functions/up/index.js +54 -34
- package/dist/functions/voice/connections.d.ts +19 -0
- package/dist/functions/voice/connections.js +88 -0
- package/dist/functions/voice/index.js +248 -102
- package/dist/functions/volta/index.js +7 -12
- package/dist/jpfun.core.min.js +61 -41
- package/dist/jpfun.from-midi.min.js +5 -5
- package/dist/jpfun.from-musicxml.min.js +8 -8
- package/dist/jpfun.min.js +70 -50
- package/dist/layout/engine.d.ts +13 -1
- package/dist/layout/engine.js +458 -129
- package/dist/layout/model.js +22 -66
- package/dist/layout/page.js +3 -1
- package/dist/layout/range.d.ts +26 -0
- package/dist/layout/range.js +75 -0
- package/dist/layout/types.d.ts +63 -49
- package/dist/lowering/loweringContext.d.ts +1 -1
- package/dist/lowering/loweringContext.js +34 -4
- package/dist/lowering/types.d.ts +5 -0
- package/dist/parser/parse-utils/length-utils.js +2 -0
- package/dist/parser/parse-utils/note-utils.d.ts +2 -0
- package/dist/parser/parse-utils/note-utils.js +6 -2
- package/dist/parser/parserContext.js +5 -9
- package/dist/parser/preprocess.js +4 -6
- package/dist/playback/compile.d.ts +2 -9
- package/dist/playback/compile.js +297 -186
- package/dist/playback/event.d.ts +8 -16
- package/dist/playback/event.js +13 -3
- package/dist/playback/types.d.ts +76 -18
- package/dist/playback/types.js +1 -0
- package/dist/render/index.d.ts +1 -1
- package/dist/render/index.js +1 -1
- package/dist/render/paint.js +1 -1
- package/dist/render/recording.d.ts +1 -0
- package/dist/render/recording.js +21 -0
- package/dist/render/translate.d.ts +14 -0
- package/dist/render/translate.js +48 -0
- package/grammar.md +200 -250
- package/package.json +4 -4
|
@@ -48,21 +48,19 @@ class TupletFunction extends ASTFunctionNode {
|
|
|
48
48
|
};
|
|
49
49
|
}
|
|
50
50
|
loweringEnter(ctx) {
|
|
51
|
-
const events = [];
|
|
52
51
|
const group = {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
},
|
|
52
|
+
nodes: [],
|
|
53
|
+
attachments: [],
|
|
54
|
+
onTemporal(node) { group.nodes.push(node); },
|
|
55
|
+
onAttachment(attachment) { group.attachments.push(attachment); },
|
|
58
56
|
};
|
|
59
57
|
ctx.beginLoweringGroup(this, group);
|
|
60
58
|
return [];
|
|
61
59
|
}
|
|
62
60
|
loweringExit(ctx, _track, timeOffset) {
|
|
63
|
-
const
|
|
61
|
+
const content = ctx.endLoweringGroup(this);
|
|
64
62
|
// 零时长控制事件不构成连音单位,但稍后仍要随整组移动其开始位置。
|
|
65
|
-
const positive =
|
|
63
|
+
const positive = content.nodes.filter(event => event.T.compare(0) > 0);
|
|
66
64
|
if (positive.length === 0) {
|
|
67
65
|
throw new ErrorDiagnostic("E_TUPLET_EMPTY", "@tuplet 的内容必须产生至少一个正时值事件", this.sourceSpan);
|
|
68
66
|
}
|
|
@@ -79,7 +77,7 @@ class TupletFunction extends ASTFunctionNode {
|
|
|
79
77
|
shortest.copyFrom(event.T);
|
|
80
78
|
writtenTotal.add(event.T);
|
|
81
79
|
}
|
|
82
|
-
const units = writtenTotal.
|
|
80
|
+
const units = writtenTotal.div(shortest);
|
|
83
81
|
const actual = units.numerator;
|
|
84
82
|
if (units.denominator !== 1 || actual < 2 || !Number.isSafeInteger(actual)) {
|
|
85
83
|
throw new ErrorDiagnostic("E_TUPLET_NON_INTEGRAL_UNITS", "@tuplet 的总时值必须是最短正时值的整数倍,且至少包含2个单位", this.sourceSpan);
|
|
@@ -88,15 +86,15 @@ class TupletFunction extends ASTFunctionNode {
|
|
|
88
86
|
throw new ErrorDiagnostic("E_TUPLET_INVALID_RATIO", `@tuplet 的 normal 不能等于推导出的 actual (${actual})`, this.sourceSpan);
|
|
89
87
|
}
|
|
90
88
|
// 以作用域最早时间为仿射缩放原点,不能直接缩放绝对 t,否则嵌套位置会漂移。
|
|
91
|
-
let start =
|
|
92
|
-
for (let i = 1; i <
|
|
93
|
-
if (
|
|
94
|
-
start =
|
|
89
|
+
let start = content.nodes[0].t;
|
|
90
|
+
for (let i = 1; i < content.nodes.length; i++) {
|
|
91
|
+
if (content.nodes[i].t.compare(start) < 0)
|
|
92
|
+
start = content.nodes[i].t;
|
|
95
93
|
}
|
|
96
94
|
start = start.clone();
|
|
97
95
|
// t、T 与 lowering 游标必须一起修改;后继节点才会从缩放后的组尾继续。
|
|
98
96
|
const offset = new Fraction();
|
|
99
|
-
for (const event of
|
|
97
|
+
for (const event of content.nodes) {
|
|
100
98
|
offset.copyFrom(event.t).sub(start).mul(this.normal, actual);
|
|
101
99
|
event.t.copyFrom(start).add(offset);
|
|
102
100
|
event.T.mul(this.normal, actual);
|
|
@@ -105,7 +103,7 @@ class TupletFunction extends ASTFunctionNode {
|
|
|
105
103
|
// 括线只依附可见主体;时值缩放本身仍覆盖所有收集到的事件。
|
|
106
104
|
const visible = positive.filter(isVisualTemporalNode);
|
|
107
105
|
if (visible.length >= 2) {
|
|
108
|
-
ctx.addAttachment(new TupletLayoutAttachment(visible, actual, this.sourceSpan, this.font));
|
|
106
|
+
ctx.addAttachment(new TupletLayoutAttachment(visible, actual, this.sourceSpan, this.font, content));
|
|
109
107
|
}
|
|
110
108
|
return [];
|
|
111
109
|
}
|
|
@@ -131,6 +129,7 @@ export const TupletNode = TupletFunction;
|
|
|
131
129
|
* layout 根据最终宿主坐标测量数字和括线,paint 只回放已冻结的绝对几何。
|
|
132
130
|
*/
|
|
133
131
|
class TupletLayoutAttachment {
|
|
132
|
+
content;
|
|
134
133
|
layer = "foreground";
|
|
135
134
|
/** lowering 固化的连音语义;重复 layout 时保持不变 */
|
|
136
135
|
endPoints;
|
|
@@ -139,7 +138,8 @@ class TupletLayoutAttachment {
|
|
|
139
138
|
/** 由端点最大字号冻结的绘制规格 */
|
|
140
139
|
style;
|
|
141
140
|
size;
|
|
142
|
-
constructor(endPoints, actual, sourceSpan, font) {
|
|
141
|
+
constructor(endPoints, actual, sourceSpan, font, content) {
|
|
142
|
+
this.content = content;
|
|
143
143
|
this.endPoints = endPoints;
|
|
144
144
|
this.actual = actual;
|
|
145
145
|
this.sourceSpan = sourceSpan;
|
|
@@ -174,15 +174,11 @@ class TupletLayoutAttachment {
|
|
|
174
174
|
const textGap = this.size * 0.12;
|
|
175
175
|
const hookHeight = this.size * 0.18;
|
|
176
176
|
const hostGap = this.size * 0.14;
|
|
177
|
-
//
|
|
178
|
-
const view = context.lines[first.layoutLine];
|
|
179
|
-
const from = view?.columnOf(first) ?? -1;
|
|
180
|
-
const to = view?.columnOf(last) ?? -1;
|
|
177
|
+
// 范围避让与内容包含分别由系统查询,局部成员和正文使用同一入口。
|
|
181
178
|
const axis = context.getVisualAxis(first.layoutLine, first.track);
|
|
182
|
-
const extent =
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
const hostTop = axis + (extent?.top ?? 0);
|
|
179
|
+
const extent = context.getRangeExtents(first.layoutLine, [first, last], first.track);
|
|
180
|
+
const bounds = context.getContentBounds(this.content);
|
|
181
|
+
const hostTop = Math.min(axis + (extent?.top ?? 0), bounds?.y ?? Infinity);
|
|
186
182
|
const strokeWidth = Math.max(1, this.size * 0.055);
|
|
187
183
|
const lineY = hostTop - hostGap - Math.max(hookHeight, metrics.h / 2);
|
|
188
184
|
const hookBottom = lineY + hookHeight;
|
|
@@ -2,12 +2,13 @@ import { ASTNodeBase, ASTFunctionNode, ASTTextNode, ASTLabelNode, ASTBraceNode }
|
|
|
2
2
|
import { ErrorDiagnostic } from "../../diagnostic.js";
|
|
3
3
|
import { ANCHOR_KEY, DEFAULT_KEY, isVisualTemporalNode, TemporalNodeBase, } from "../temporal.js";
|
|
4
4
|
import { Fraction } from "../../fraction.js";
|
|
5
|
-
import {
|
|
5
|
+
import { getLayoutBounds, layoutLocalSequence } from "../../layout/engine.js";
|
|
6
6
|
/**
|
|
7
7
|
* `@up` / `@down` 共用的折叠容器,不导出
|
|
8
8
|
*
|
|
9
9
|
* `contents[0]` 是宿主,其余向上;`belows` 全部向下。两个方向共用一个容器,
|
|
10
10
|
* 所以混写的 `^` / `_` 会全部绑到同一个宿主,不需要括号分组。
|
|
11
|
+
* 每个参数只能产生一个 VisualTemporalNode,外层统一承担对齐、时值和合列
|
|
11
12
|
*/
|
|
12
13
|
class FoldFunction extends ASTFunctionNode {
|
|
13
14
|
contents = [];
|
|
@@ -57,9 +58,9 @@ class FoldFunction extends ASTFunctionNode {
|
|
|
57
58
|
// 成员不是外层分组的成员,折叠体才是;否则 voice 的歌词会按下标错位
|
|
58
59
|
ctx.isolateFromLoweringGroups(() => {
|
|
59
60
|
for (const content of this.children) {
|
|
60
|
-
|
|
61
|
-
const
|
|
62
|
-
if (!member ||
|
|
61
|
+
const columns = ctx.trackedEvents(content, new Fraction(), track);
|
|
62
|
+
const member = columns.length === 1 && columns[0].length === 1 ? columns[0][0] : undefined;
|
|
63
|
+
if (!member || !isVisualTemporalNode(member)) {
|
|
63
64
|
throw new ErrorDiagnostic("E_UP_INVALID_CHILD", "@up 的每个参数必须恰好产生一个可见 Temporal,且不能包含多声部结构", content.sourceSpan);
|
|
64
65
|
}
|
|
65
66
|
members.push(member);
|
|
@@ -240,21 +241,23 @@ class FoldTemporal extends TemporalNodeBase {
|
|
|
240
241
|
this.members = members;
|
|
241
242
|
// members 按 ast.children 构建,而 children 是 [...contents, ...belows]
|
|
242
243
|
this.aboveCount = ast.contents.length;
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
244
|
+
const host = members[0];
|
|
245
|
+
this.mergeKey = host && host.mergeKey !== host.order ? host.mergeKey : DEFAULT_KEY;
|
|
246
|
+
// 普通组保留 `$p ^ 1` 的时值回退,专属组不借附属音符提前推进游标
|
|
247
|
+
const fallback = this.mergeKey === DEFAULT_KEY || this.mergeKey === ANCHOR_KEY;
|
|
248
|
+
if (!fallback && host)
|
|
249
|
+
this.T.copyFrom(host.T);
|
|
248
250
|
this.initLayoutBox();
|
|
249
251
|
// 宿主决定折叠体的时值,它的修饰语义也随之成为整体的修饰,
|
|
250
252
|
// 自动连梁等语义处理才能看到这个折叠体的节奏;
|
|
251
253
|
// 随后外层 LoweringGroup 会在同一 addon 上继续累加
|
|
252
|
-
const
|
|
253
|
-
if (
|
|
254
|
-
this.addon = { ...
|
|
254
|
+
const addon = host?.addon;
|
|
255
|
+
if (addon)
|
|
256
|
+
this.addon = { ...addon };
|
|
255
257
|
for (const member of members) {
|
|
256
|
-
|
|
257
|
-
|
|
258
|
+
if (fallback && this.T.isZero())
|
|
259
|
+
this.T.copyFrom(member.T);
|
|
260
|
+
// 锚点仍优先:`| ^ @text(A)` 得保持小节线语义
|
|
258
261
|
if (member.mergeKey === ANCHOR_KEY)
|
|
259
262
|
this.mergeKey = ANCHOR_KEY;
|
|
260
263
|
// 时间同步在 onTimeState 里做,避免成员的时间被提前固化(后续时间可能会变)
|
|
@@ -270,18 +273,33 @@ class FoldTemporal extends TemporalNodeBase {
|
|
|
270
273
|
* 成员共享全局时间状态,从写在最后的成员开始固化
|
|
271
274
|
*/
|
|
272
275
|
onTimeState(state) {
|
|
276
|
+
const duration = this.T.clone();
|
|
277
|
+
let offset = undefined;
|
|
278
|
+
let durationChanged = false;
|
|
273
279
|
// 宿主写在最前面,标记写在它后面;标记先写入状态,宿主才读得到
|
|
274
280
|
for (let i = this.members.length - 1; i >= 0; i--) {
|
|
275
281
|
const member = this.members[i];
|
|
276
282
|
// 堆叠在一起的成员共享同一个时值,由第一个成员决定;
|
|
277
283
|
// 本来就没有时长的成员(标注、小节线等)保持 0,不会被拉长
|
|
278
|
-
if (!member.T.isZero())
|
|
284
|
+
if (!member.T.isZero() && !this.T.isZero())
|
|
279
285
|
member.T.copyFrom(this.T);
|
|
280
286
|
member.t.copyFrom(this.t);
|
|
281
287
|
member.track = this.track;
|
|
282
288
|
member.layoutLine = this.layoutLine;
|
|
283
|
-
member.onTimeState?.(state);
|
|
289
|
+
offset = member.onTimeState?.(state);
|
|
290
|
+
durationChanged ||= !member.T.isZero() && !member.T.equals(duration);
|
|
284
291
|
}
|
|
292
|
+
const host = this.members[0];
|
|
293
|
+
if (host && !host.T.isZero()) {
|
|
294
|
+
this.T.copyFrom(host.T);
|
|
295
|
+
if (durationChanged) {
|
|
296
|
+
for (const member of this.members) {
|
|
297
|
+
if (!member.T.isZero())
|
|
298
|
+
member.T.copyFrom(this.T);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
return offset;
|
|
285
303
|
}
|
|
286
304
|
emitPlayback(emitter) {
|
|
287
305
|
// 倒序发布,宿主(members[0])最后:附属成员注册的局部变换因此作用到宿主
|
|
@@ -295,7 +313,7 @@ class FoldTemporal extends TemporalNodeBase {
|
|
|
295
313
|
* 宿主留在轨道基线上,其余成员按书写顺序向上或向下叠放
|
|
296
314
|
*
|
|
297
315
|
* 成员不进入全局 columns,因此它们的准备、定位和绘制都由本节点负责;
|
|
298
|
-
*
|
|
316
|
+
* 每个成员独立执行局部横排,宿主继承含框约束的占位,其余成员仍沿宿主锚点悬挂。
|
|
299
317
|
*/
|
|
300
318
|
prepareLayout(context) {
|
|
301
319
|
// lowering 期间修饰挂在折叠体上(augmenter 要看到整体节奏),渲染时交给宿主:
|
|
@@ -304,36 +322,38 @@ class FoldTemporal extends TemporalNodeBase {
|
|
|
304
322
|
this.members[0].addon = this.addon;
|
|
305
323
|
this.addon = void 0;
|
|
306
324
|
}
|
|
307
|
-
for (const member of this.members)
|
|
308
|
-
prepareLayoutHost(member, context);
|
|
309
325
|
const first = this.members[0];
|
|
310
326
|
if (!first) {
|
|
311
327
|
this.box.w = this.box.h = 0;
|
|
312
328
|
this.box.anchor = this.box.visualAxis = 0;
|
|
313
329
|
return;
|
|
314
330
|
}
|
|
331
|
+
this.box.w = layoutLocalSequence([first], context);
|
|
332
|
+
for (let index = 1; index < this.members.length; index++)
|
|
333
|
+
layoutLocalSequence([this.members[index]], context);
|
|
315
334
|
this.springConfig = { ...first.springConfig, ...this.springConfig };
|
|
316
335
|
// 横向完全等于宿主:上下的标记(变速、注释、力度)常常比音符宽得多,
|
|
317
336
|
// 让它们撑宽盒子会把右邻推开一大截;它们画在基线外侧,伸出盒外也不会碰撞
|
|
318
|
-
const anchor = first.box.anchor;
|
|
337
|
+
const anchor = first.box.x + first.box.anchor;
|
|
319
338
|
const gap = this.ast.size * 0.12;
|
|
320
339
|
this.verticalOffsets.length = this.members.length;
|
|
321
|
-
this.verticalOffsets
|
|
340
|
+
this.verticalOffsets[0] = 0;
|
|
322
341
|
// 以宿主盒顶为 0,向上得到负坐标、向下得到正坐标,最后整体下移
|
|
323
|
-
|
|
342
|
+
const firstBounds = getLayoutBounds(first);
|
|
343
|
+
let top = firstBounds.y;
|
|
324
344
|
for (let i = 1; i < this.aboveCount; i++) {
|
|
325
|
-
|
|
326
|
-
this.verticalOffsets[i] = top;
|
|
345
|
+
const bounds = getLayoutBounds(this.members[i]);
|
|
346
|
+
this.verticalOffsets[i] = top - gap - bounds.y - bounds.h;
|
|
347
|
+
top -= gap + bounds.h;
|
|
327
348
|
}
|
|
328
|
-
let bottom =
|
|
349
|
+
let bottom = firstBounds.y + firstBounds.h;
|
|
329
350
|
for (let i = this.aboveCount; i < this.members.length; i++) {
|
|
330
|
-
|
|
331
|
-
this.verticalOffsets[i] = bottom;
|
|
332
|
-
bottom +=
|
|
351
|
+
const bounds = getLayoutBounds(this.members[i]);
|
|
352
|
+
this.verticalOffsets[i] = bottom + gap - bounds.y;
|
|
353
|
+
bottom += gap + bounds.h;
|
|
333
354
|
}
|
|
334
355
|
for (let i = 0; i < this.verticalOffsets.length; i++)
|
|
335
356
|
this.verticalOffsets[i] -= top;
|
|
336
|
-
this.box.w = first.box.w;
|
|
337
357
|
this.box.h = bottom - top;
|
|
338
358
|
this.box.anchor = anchor;
|
|
339
359
|
// 宿主对齐轨道基线,两侧成员各自向外撑开行高
|
|
@@ -343,17 +363,17 @@ class FoldTemporal extends TemporalNodeBase {
|
|
|
343
363
|
const firstOffset = this.verticalOffsets[0];
|
|
344
364
|
for (const name in first.ports) {
|
|
345
365
|
const port = first.ports[name];
|
|
346
|
-
this.ports[name] = { x: port.x, y: firstOffset + port.y };
|
|
366
|
+
this.ports[name] = { ...port, x: first.box.x + port.x, y: firstOffset + port.y };
|
|
347
367
|
}
|
|
348
368
|
// 代表成员没声明核心范围时退回它的整个盒子
|
|
349
|
-
this.ports["body.left"] ??= { x:
|
|
350
|
-
this.ports["body.right"] ??= { x: first.box.w, y: this.box.visualAxis };
|
|
369
|
+
this.ports["body.left"] ??= { x: first.box.x, y: this.box.visualAxis };
|
|
370
|
+
this.ports["body.right"] ??= { x: first.box.x + first.box.w, y: this.box.visualAxis };
|
|
351
371
|
// 唯一的例外:连音线要接到最上面那个成员的顶部
|
|
352
372
|
const topIndex = this.aboveCount - 1;
|
|
353
373
|
const topOffset = this.verticalOffsets[topIndex];
|
|
354
374
|
this.ports["tie.top"] = {
|
|
355
375
|
x: anchor,
|
|
356
|
-
y: topOffset + (this.members[topIndex].ports["tie.top"]?.y ??
|
|
376
|
+
y: topOffset + (this.members[topIndex].ports["tie.top"]?.y ?? getLayoutBounds(this.members[topIndex], true).y),
|
|
357
377
|
};
|
|
358
378
|
}
|
|
359
379
|
/** 引擎每次改变本节点坐标后,按当前 anchor 与准备阶段的纵向偏移重算成员绝对坐标 */
|
|
@@ -369,7 +389,7 @@ class FoldTemporal extends TemporalNodeBase {
|
|
|
369
389
|
for (const member of this.members) {
|
|
370
390
|
member.paint(painter);
|
|
371
391
|
for (const decoration of member.decorations)
|
|
372
|
-
decoration.paint(painter);
|
|
392
|
+
decoration.paint?.(painter);
|
|
373
393
|
}
|
|
374
394
|
}
|
|
375
395
|
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { SourceSpan } from "../ASTtypes.js";
|
|
2
|
+
import type { PathCommand } from "../../render/types.js";
|
|
3
|
+
export declare const CURVED_BRACE_COMMANDS: readonly PathCommand[];
|
|
4
|
+
export declare const CURVED_BRACE_BOUNDS: import("../../core.js").Rect;
|
|
5
|
+
/** 方括线端钩,以粗竖线宽度为单位,绘制时上下镜像 */
|
|
6
|
+
export declare const BRACKET_HOOK_REACH = 2.33;
|
|
7
|
+
export declare const BRACKET_HOOK_DROP = 1.17;
|
|
8
|
+
export declare const BRACKET_HOOK_COMMANDS: readonly PathCommand[];
|
|
9
|
+
export type ConnectionKind = "brace" | "bracket";
|
|
10
|
+
export interface ConnectionSpec {
|
|
11
|
+
kind: ConnectionKind;
|
|
12
|
+
from?: number;
|
|
13
|
+
to?: number;
|
|
14
|
+
}
|
|
15
|
+
/** 开放端点在声部收集完毕后补全 */
|
|
16
|
+
export type VoiceConnection = Required<ConnectionSpec>;
|
|
17
|
+
export declare function parseConnections(value: string, span: SourceSpan): ConnectionSpec[];
|
|
18
|
+
export declare function resolveConnections(specs: readonly ConnectionSpec[], count: number, span: SourceSpan): VoiceConnection[];
|
|
19
|
+
export declare function serializeConnections(specs: readonly ConnectionSpec[]): string;
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { ErrorDiagnostic } from "../../diagnostic.js";
|
|
2
|
+
import { pathBounds } from "../../layout/path.js";
|
|
3
|
+
export const CURVED_BRACE_COMMANDS = [
|
|
4
|
+
{ op: "M", x: 2.98438, y: -165.5 },
|
|
5
|
+
{ op: "C", cx1: 8.9375, cy1: -171.781, cx2: 20.5156, cy2: -184.688, x: 20.5156, y: -208.531 },
|
|
6
|
+
{ op: "C", cx1: 20.5156, cy1: -238.641, cx2: 7.60938, cy2: -269.094, x: 7.60938, y: -287.969 },
|
|
7
|
+
{ op: "C", cx1: 7.60938, cy1: -308.156, cx2: 19.2031, cy2: -327.016, x: 19.8594, y: -327.688 },
|
|
8
|
+
{ op: "C", cx1: 19.8594, cy1: -328.016, cx2: 19.8594, cy2: -328.016, x: 19.8594, y: -328.344 },
|
|
9
|
+
{ op: "C", cx1: 19.8594, cy1: -328.672, cx2: 19.8594, cy2: -329, x: 19.5313, y: -329.344 },
|
|
10
|
+
{ op: "C", cx1: 19.2031, cy1: -329.344, cx2: 18.5313, cy2: -329.344, x: 18.2031, y: -328.672 },
|
|
11
|
+
{ op: "C", cx1: 17.875, cy1: -328.344, cx2: 0, cy2: -310.469, x: 0, y: -283.328 },
|
|
12
|
+
{ op: "C", cx1: 0, cy1: -254.203, cx2: 11.5781, cy2: -233.688, x: 11.5781, y: -195.625 },
|
|
13
|
+
{ op: "C", cx1: 11.5781, cy1: -188.328, cx2: 8.60938, cy2: -176.75, x: 0.328125, y: -166.484 },
|
|
14
|
+
{ op: "C", cx1: 0, cy1: -166.484, cx2: 0, cy2: -165.828, x: 0, y: -165.5 },
|
|
15
|
+
{ op: "C", cx1: -0.328125, cy1: -165.172, cx2: 0, cy2: -164.828, x: 0.328125, y: -164.5 },
|
|
16
|
+
{ op: "C", cx1: 8.60938, cy1: -154.25, cx2: 11.5781, cy2: -142.656, x: 11.5781, y: -135.375 },
|
|
17
|
+
{ op: "C", cx1: 11.5781, cy1: -97.3125, cx2: 0, cy2: -76.7969, x: 0, y: -47.6563 },
|
|
18
|
+
{ op: "C", cx1: 0, cy1: -20.8594, cx2: 17.875, cy2: -2.64063, x: 18.2031, y: -2.3125 },
|
|
19
|
+
{ op: "C", cx1: 18.5313, cy1: -1.65625, cx2: 19.2031, cy2: -1.32813, x: 19.5313, y: -1.65625 },
|
|
20
|
+
{ op: "C", cx1: 19.8594, cy1: -1.98438, cx2: 20.1875, cy2: -2.3125, x: 19.8594, y: -2.64063 },
|
|
21
|
+
{ op: "C", cx1: 19.8594, cy1: -2.64063, cx2: 19.8594, cy2: -2.98438, x: 19.8594, y: -3.3125 },
|
|
22
|
+
{ op: "C", cx1: 19.2031, cy1: -3.96875, cx2: 7.60938, cy2: -22.8438, x: 7.60938, y: -43.0313 },
|
|
23
|
+
{ op: "C", cx1: 7.60938, cy1: -61.8906, cx2: 20.5156, cy2: -92.3438, x: 20.5156, y: -122.469 },
|
|
24
|
+
{ op: "C", cx1: 20.5156, cy1: -146.625, cx2: 8.9375, cy2: -159.203, x: 2.98438, y: -165.5 },
|
|
25
|
+
{ op: "Z" },
|
|
26
|
+
];
|
|
27
|
+
export const CURVED_BRACE_BOUNDS = pathBounds(CURVED_BRACE_COMMANDS);
|
|
28
|
+
/** 方括线端钩,以粗竖线宽度为单位,绘制时上下镜像 */
|
|
29
|
+
export const BRACKET_HOOK_REACH = 2.33;
|
|
30
|
+
export const BRACKET_HOOK_DROP = 1.17;
|
|
31
|
+
const BRACKET_HOOK_BASE = 0.34;
|
|
32
|
+
export const BRACKET_HOOK_COMMANDS = [
|
|
33
|
+
{ op: "M", x: 0, y: 0 },
|
|
34
|
+
{ op: "L", x: 0, y: -BRACKET_HOOK_BASE },
|
|
35
|
+
{
|
|
36
|
+
op: "C",
|
|
37
|
+
cx1: BRACKET_HOOK_REACH * 0.421, cy1: -BRACKET_HOOK_BASE + BRACKET_HOOK_DROP * 0.077,
|
|
38
|
+
cx2: BRACKET_HOOK_REACH * 0.733, cy2: -BRACKET_HOOK_BASE + BRACKET_HOOK_DROP * 0.346,
|
|
39
|
+
x: BRACKET_HOOK_REACH, y: -BRACKET_HOOK_BASE + BRACKET_HOOK_DROP,
|
|
40
|
+
},
|
|
41
|
+
{ op: "L", x: BRACKET_HOOK_REACH * 0.929, y: -BRACKET_HOOK_BASE + BRACKET_HOOK_DROP },
|
|
42
|
+
{
|
|
43
|
+
op: "C",
|
|
44
|
+
cx1: BRACKET_HOOK_REACH * 0.696, cy1: -BRACKET_HOOK_BASE + BRACKET_HOOK_DROP * 0.535,
|
|
45
|
+
cx2: BRACKET_HOOK_REACH * 0.328, cy2: -BRACKET_HOOK_BASE + BRACKET_HOOK_DROP * 0.352,
|
|
46
|
+
x: 0, y: 0,
|
|
47
|
+
},
|
|
48
|
+
{ op: "Z" },
|
|
49
|
+
];
|
|
50
|
+
function invalidConnection(span) {
|
|
51
|
+
return new ErrorDiagnostic("E_VOICES_CONNECT", '@voices 的 connect 必须是 [起点-终点] 或 {起点-终点} 的组合;端点可省略,否则须为有效声部编号。空字符串表示只有连谱线。', span);
|
|
52
|
+
}
|
|
53
|
+
export function parseConnections(value, span) {
|
|
54
|
+
const source = value.trim();
|
|
55
|
+
// 粘连匹配必须消费整串,不能跳过非法字符
|
|
56
|
+
// 非空数字与其后空白一起可选,避免省略端点时重复回溯
|
|
57
|
+
const pattern = /\s*([\[{])\s*(?:(\d+)\s*)?-\s*(?:(\d+)\s*)?([\]}])\s*/y;
|
|
58
|
+
const result = [];
|
|
59
|
+
while (pattern.lastIndex < source.length) {
|
|
60
|
+
const match = pattern.exec(source);
|
|
61
|
+
if (!match || match[4] !== (match[1] === "[" ? "]" : "}"))
|
|
62
|
+
throw invalidConnection(span);
|
|
63
|
+
const from = match[2] ? Number(match[2]) : undefined;
|
|
64
|
+
const to = match[3] ? Number(match[3]) : undefined;
|
|
65
|
+
for (const endpoint of [from, to]) {
|
|
66
|
+
if (endpoint !== undefined && (!Number.isSafeInteger(endpoint) || endpoint < 1)) {
|
|
67
|
+
throw invalidConnection(span);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
if (from !== undefined && to !== undefined && from > to)
|
|
71
|
+
throw invalidConnection(span);
|
|
72
|
+
result.push({ kind: match[1] === "[" ? "bracket" : "brace", from, to });
|
|
73
|
+
}
|
|
74
|
+
return result;
|
|
75
|
+
}
|
|
76
|
+
export function resolveConnections(specs, count, span) {
|
|
77
|
+
return specs.map(({ kind, from = 1, to = count }) => {
|
|
78
|
+
if (from > to || to > count)
|
|
79
|
+
throw invalidConnection(span);
|
|
80
|
+
return { kind, from, to };
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
export function serializeConnections(specs) {
|
|
84
|
+
return specs.map(({ kind, from, to }) => {
|
|
85
|
+
const range = `${from ?? ""}-${to ?? ""}`;
|
|
86
|
+
return kind === "brace" ? `{${range}}` : `[${range}]`;
|
|
87
|
+
}).join("");
|
|
88
|
+
}
|