jpfun 0.1.0 → 0.3.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 +200 -200
- package/README.md +191 -133
- package/dist/diagnostic.js +5 -2
- package/dist/fraction.d.ts +24 -0
- package/dist/fraction.js +132 -0
- package/dist/functions/ASTtypes.d.ts +24 -7
- package/dist/functions/ASTtypes.js +35 -16
- package/dist/functions/adjust/index.d.ts +2 -0
- package/dist/functions/adjust/index.js +193 -0
- package/dist/functions/bar/index.d.ts +8 -0
- package/dist/functions/bar/index.js +48 -19
- package/dist/functions/beam/auto.js +35 -18
- package/dist/functions/beam/index.js +10 -6
- package/dist/functions/beam/layout.d.ts +3 -2
- package/dist/functions/beam/layout.js +42 -64
- package/dist/functions/box/index.js +35 -34
- package/dist/functions/br/index.js +14 -12
- package/dist/functions/dash/index.js +37 -5
- package/dist/functions/default.js +14 -7
- package/dist/functions/div/index.d.ts +16 -0
- package/dist/functions/div/index.js +51 -23
- package/dist/functions/dot/index.js +16 -8
- package/dist/functions/grace/index.d.ts +8 -18
- package/dist/functions/grace/index.js +98 -99
- package/dist/functions/head/index.d.ts +2 -0
- package/dist/functions/head/index.js +492 -0
- package/dist/functions/key/index.js +98 -5
- package/dist/functions/meter/index.d.ts +2 -0
- package/dist/functions/meter/index.js +154 -0
- package/dist/functions/note/accidentals.d.ts +24 -3
- package/dist/functions/note/accidentals.js +31 -1
- package/dist/functions/note/index.js +60 -80
- package/dist/functions/page/index.d.ts +9 -2
- package/dist/functions/page/index.js +50 -4
- package/dist/functions/set/index.js +42 -34
- package/dist/functions/stack/index.js +39 -34
- package/dist/functions/symbol/index.d.ts +21 -0
- package/dist/functions/symbol/index.js +94 -0
- package/dist/functions/symbol/ornament.d.ts +15 -0
- package/dist/functions/symbol/ornament.js +73 -0
- package/dist/functions/symbol/shape.d.ts +22 -0
- package/dist/functions/symbol/shape.js +48 -0
- package/dist/functions/symbol/symbols/accent.d.ts +2 -0
- package/dist/functions/symbol/symbols/accent.js +24 -0
- package/dist/functions/symbol/symbols/f.d.ts +2 -0
- package/dist/functions/symbol/symbols/f.js +55 -0
- package/dist/functions/symbol/symbols/fermata.d.ts +2 -0
- package/dist/functions/symbol/symbols/fermata.js +24 -0
- package/dist/functions/symbol/symbols/mordent.d.ts +2 -0
- package/dist/functions/symbol/symbols/mordent.js +43 -0
- package/dist/functions/symbol/symbols/p.d.ts +2 -0
- package/dist/functions/symbol/symbols/p.js +61 -0
- package/dist/functions/symbol/symbols/tr.d.ts +2 -0
- package/dist/functions/symbol/symbols/tr.js +75 -0
- package/dist/functions/tempo/glyph.d.ts +12 -0
- package/dist/functions/tempo/glyph.js +30 -0
- package/dist/functions/tempo/index.js +53 -3
- package/dist/functions/text/index.js +78 -26
- package/dist/functions/tie/index.js +103 -128
- package/dist/functions/tuplet/index.d.ts +2 -0
- package/dist/functions/tuplet/index.js +211 -0
- package/dist/functions/{over → up}/index.d.ts +1 -0
- package/dist/functions/up/index.js +370 -0
- package/dist/functions/voice/index.js +125 -108
- package/dist/functions/volta/index.d.ts +2 -0
- package/dist/functions/volta/index.js +227 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +4 -0
- package/dist/jpfun.min.js +143 -0
- package/dist/layout/engine.d.ts +4 -11
- package/dist/layout/engine.js +100 -66
- package/dist/layout/model.d.ts +8 -1
- package/dist/layout/model.js +16 -6
- package/dist/layout/path.d.ts +19 -0
- package/dist/layout/path.js +101 -0
- package/dist/layout/types.d.ts +51 -18
- package/dist/layout/types.js +3 -1
- package/dist/lowering/loweringContext.d.ts +19 -23
- package/dist/lowering/loweringContext.js +179 -187
- package/dist/lowering/track.d.ts +33 -10
- package/dist/lowering/track.js +6 -6
- package/dist/lowering/types.d.ts +57 -43
- package/dist/lowering/types.js +20 -4
- package/dist/parser/parse-utils/call-utils.d.ts +0 -2
- package/dist/parser/parse-utils/call-utils.js +0 -6
- package/dist/parser/parse-utils/note-utils.d.ts +8 -0
- package/dist/parser/parse-utils/note-utils.js +35 -13
- package/dist/parser/parse-utils/string-utils.d.ts +8 -0
- package/dist/parser/parse-utils/string-utils.js +20 -0
- package/dist/parser/parserContext.d.ts +8 -5
- package/dist/parser/parserContext.js +38 -33
- package/dist/parser/preprocess.js +17 -11
- package/dist/playback/compile.d.ts +18 -0
- package/dist/playback/compile.js +401 -0
- package/dist/playback/event.d.ts +91 -0
- package/dist/playback/event.js +87 -0
- package/dist/playback/time.d.ts +14 -0
- package/dist/playback/time.js +81 -0
- package/dist/playback/types.d.ts +128 -0
- package/dist/playback/types.js +3 -0
- package/dist/render/canvas.d.ts +4 -4
- package/dist/render/canvas.js +20 -5
- package/dist/render/index.d.ts +1 -0
- package/dist/render/index.js +1 -0
- package/dist/render/paint.d.ts +8 -0
- package/dist/render/paint.js +101 -0
- package/dist/render/svg.d.ts +3 -3
- package/dist/render/svg.js +10 -8
- package/dist/render/text.d.ts +17 -0
- package/dist/render/text.js +35 -0
- package/dist/render/types.d.ts +1 -1
- package/grammar.md +217 -122
- package/package.json +14 -9
- package/dist/functions/over/index.js +0 -307
|
@@ -1,19 +1,20 @@
|
|
|
1
1
|
import { ASTFunctionNode, functionAddonKey } from "../ASTtypes.js";
|
|
2
|
-
import { ErrorDiagnostic } from "../../diagnostic.js";
|
|
2
|
+
import { ErrorDiagnostic, WarningDiagnostic } from "../../diagnostic.js";
|
|
3
3
|
const DOT_FUNC_NAME = "dot";
|
|
4
4
|
const DOT_ADDON_KEY = functionAddonKey(DOT_FUNC_NAME);
|
|
5
5
|
function durationFactor(count) {
|
|
6
|
-
|
|
6
|
+
const denominator = 2 ** count;
|
|
7
|
+
return [2 * denominator - 1, denominator];
|
|
7
8
|
}
|
|
8
9
|
class DotFunction extends ASTFunctionNode {
|
|
9
10
|
static def = {
|
|
10
11
|
name: [DOT_FUNC_NAME, "."],
|
|
11
12
|
description: "附点",
|
|
12
|
-
example: `@dot(C1, 2): C1右侧创建2个点 仅接收一个可接收元素
|
|
13
|
-
语法糖:在音符后加斜杠'.',可以多个
|
|
14
|
-
@dot(C1, 2) === C1..
|
|
15
|
-
@dot(C1 C2/ @dash(), 2) 报错 因为点只能接收一个元素
|
|
16
|
-
设置命名参数前缀: dot
|
|
13
|
+
example: `@dot(C1, 2): C1右侧创建2个点 仅接收一个可接收元素
|
|
14
|
+
语法糖:在音符后加斜杠'.',可以多个
|
|
15
|
+
@dot(C1, 2) === C1..
|
|
16
|
+
@dot(C1 C2/ @dash(), 2) 报错 因为点只能接收一个元素
|
|
17
|
+
设置命名参数前缀: dot
|
|
17
18
|
`,
|
|
18
19
|
allowExtraArgs: false,
|
|
19
20
|
args: [
|
|
@@ -113,7 +114,9 @@ class DotFunction extends ASTFunctionNode {
|
|
|
113
114
|
const addon = node.addon ??= {};
|
|
114
115
|
const current = Number(addon[DOT_ADDON_KEY]) || 0;
|
|
115
116
|
const total = current + count;
|
|
116
|
-
|
|
117
|
+
const [totalNumerator, totalDenominator] = durationFactor(total);
|
|
118
|
+
const [currentNumerator, currentDenominator] = durationFactor(current);
|
|
119
|
+
node.T.mul(totalNumerator * currentDenominator, totalDenominator * currentNumerator);
|
|
117
120
|
addon[DOT_ADDON_KEY] = total;
|
|
118
121
|
},
|
|
119
122
|
});
|
|
@@ -140,6 +143,11 @@ class DotFunction extends ASTFunctionNode {
|
|
|
140
143
|
throw new ErrorDiagnostic("E_DOT_INVALID_CONTENT", `函数 @dot 只能接收 1个元素,但找到了 ${contentJudge} 个`, sourceSpan);
|
|
141
144
|
}
|
|
142
145
|
this.content.parent = this;
|
|
146
|
+
const n = Math.max(0, Math.trunc(this.n));
|
|
147
|
+
if (n !== this.n) {
|
|
148
|
+
ctx.diagnostics.push(new WarningDiagnostic("W_DOT_INVALID_N", `@dot 的数量参数 n 必须为非负整数,已被修正为 ${n}`, this.sourceSpan));
|
|
149
|
+
this.n = n;
|
|
150
|
+
}
|
|
143
151
|
}
|
|
144
152
|
toString(source) {
|
|
145
153
|
return `@dot(${this.content.toString(source)}, ${this.n})`;
|
|
@@ -1,27 +1,21 @@
|
|
|
1
1
|
import type { LayoutBox, LayoutPrepareContext } from "../../layout/types.js";
|
|
2
2
|
import type { LoweringContext } from "../../lowering/loweringContext.js";
|
|
3
3
|
import type { Track } from "../../lowering/track.js";
|
|
4
|
-
import { TemporalNodeBase, type
|
|
4
|
+
import { TemporalNodeBase, type TimeState, type VisualTemporalNode } from "../../lowering/types.js";
|
|
5
5
|
import { GrammarNode, GrammarSugarNode } from "../../parser/grammarType.js";
|
|
6
6
|
import { ParserContext } from "../../parser/parserContext.js";
|
|
7
7
|
import type { Painter } from "../../render/types.js";
|
|
8
|
+
import type { PlaybackEmitter } from "../../playback/types.js";
|
|
8
9
|
import { ASTFunctionClass, ASTFunctionNode, ASTNodeBase, FunctionArgs, FunctionDef, SourceSpan } from "../ASTtypes.js";
|
|
9
10
|
type GraceSide = "pre" | "post";
|
|
10
11
|
declare class GraceFunction extends ASTFunctionNode {
|
|
11
12
|
static def: FunctionDef;
|
|
12
|
-
labelable():
|
|
13
|
+
labelable(): ASTFunctionNode | null;
|
|
13
14
|
static deSugarAtom(source: string, start: number, _end: number): {
|
|
14
15
|
next: number;
|
|
15
16
|
node: GrammarSugarNode;
|
|
16
17
|
} | null;
|
|
17
18
|
static deSugarRelation(ctx: ParserContext, nodes: (GrammarNode | number)[], at: number): number | null;
|
|
18
|
-
/**
|
|
19
|
-
* 语义确定后,把提升上来的修饰交还给宿主成员
|
|
20
|
-
*
|
|
21
|
-
* lowering 期间修饰必须挂在复合节点上,自动连梁之类的 augmenter 才看得到它的节奏;
|
|
22
|
-
* 但渲染上「宿主代表整个复合体」,减时线要画在宿主音符下面而不是整个盒子下面。
|
|
23
|
-
*/
|
|
24
|
-
static loweringFinalize: (result: LoweringResult) => void;
|
|
25
19
|
host: ASTNodeBase;
|
|
26
20
|
grace: ASTNodeBase;
|
|
27
21
|
side: GraceSide;
|
|
@@ -48,22 +42,24 @@ export declare class GraceTemporal extends TemporalNodeBase {
|
|
|
48
42
|
box: LayoutBox;
|
|
49
43
|
readonly host: VisualTemporalNode;
|
|
50
44
|
readonly graces: readonly VisualTemporalNode[];
|
|
45
|
+
/** 承担节奏的倚音成员,按零时长标记切段;同段视觉上首尾相接,无条件连成一束减时线 */
|
|
46
|
+
readonly graceRuns: readonly (readonly VisualTemporalNode[])[];
|
|
51
47
|
readonly side: GraceSide;
|
|
52
48
|
/** 成员相对本盒左上角的局部偏移,onPlaced 时同步为绝对坐标 */
|
|
53
49
|
private hostOffset;
|
|
54
50
|
private graceOffsets;
|
|
55
51
|
/** 倚音线起点,同样是局部偏移 */
|
|
56
52
|
private hookOrigin;
|
|
57
|
-
private divLines;
|
|
58
53
|
constructor(ast: GraceFunction, host: VisualTemporalNode, graces: readonly VisualTemporalNode[], side: GraceSide);
|
|
59
|
-
|
|
54
|
+
/** 成员共享全局时间状态,按发声顺序固化 */
|
|
55
|
+
onTimeState(state: TimeState): void;
|
|
60
56
|
/**
|
|
61
57
|
* 播放时倚音从宿主借走的时值(前倚音从开头、后倚音从末尾)
|
|
62
58
|
*
|
|
63
59
|
* 比例 = 倚音字面总时值 / 四分音符,而四分音符 T=1,所以直接累加成员的 T。
|
|
64
60
|
* 基准取宿主经过 div 与 dot 后的实际时值;延时线是独立事件、tie 不改 T,天然不计入。
|
|
65
61
|
*/
|
|
66
|
-
|
|
62
|
+
emitPlayback(emitter: PlaybackEmitter): void;
|
|
67
63
|
/**
|
|
68
64
|
* 宿主留在轨道基线上,倚音整体抬到它的左上或右上角
|
|
69
65
|
*
|
|
@@ -71,12 +67,6 @@ export declare class GraceTemporal extends TemporalNodeBase {
|
|
|
71
67
|
* 因此左右邻居会被固有宽度推开,不会在空间紧张时被压穿。
|
|
72
68
|
*/
|
|
73
69
|
prepareLayout(context: LayoutPrepareContext): void;
|
|
74
|
-
/**
|
|
75
|
-
* 把相邻倚音成员的同级减时线连成整条
|
|
76
|
-
*
|
|
77
|
-
* 折叠成员不进入全局时间列,自动连梁看不到它们,只能由复合体自己连。
|
|
78
|
-
*/
|
|
79
|
-
private connectGraceDivLines;
|
|
80
70
|
/** 引擎每次改变本节点坐标后,按准备阶段保存的局部偏移重算成员绝对坐标 */
|
|
81
71
|
onPlaced(): void;
|
|
82
72
|
paint(painter: Painter): void;
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { ErrorDiagnostic } from "../../diagnostic.js";
|
|
2
2
|
import { prepareLayoutHost } from "../../layout/engine.js";
|
|
3
|
+
import { Fraction } from "../../fraction.js";
|
|
3
4
|
import { isVisualTemporalNode, TemporalNodeBase, } from "../../lowering/types.js";
|
|
4
5
|
import { ASTBraceNode, ASTFunctionNode, ASTLabelNode, ASTTextNode, } from "../ASTtypes.js";
|
|
5
|
-
import {
|
|
6
|
+
import { createBeamLayoutAttachment } from "../beam/layout.js";
|
|
7
|
+
import { DIV_ADDON_KEY } from "../div/index.js";
|
|
6
8
|
/** 倚音相对宿主的字号比例 */
|
|
7
9
|
const GRACE_SCALE = 0.7;
|
|
8
10
|
/**
|
|
@@ -29,12 +31,12 @@ class GraceFunction extends ASTFunctionNode {
|
|
|
29
31
|
static def = {
|
|
30
32
|
name: ["grace"],
|
|
31
33
|
description: "倚音",
|
|
32
|
-
example: `前倚音: @grace(宿主, 倚音, side=pre) 后倚音: @grace(宿主, 倚音, side=post)
|
|
33
|
-
语法糖:'>' 与 '<',箭头永远指向宿主
|
|
34
|
-
2>1 前倚音:2 是 1 的倚音,画在 1 的左上角
|
|
35
|
-
1<2 后倚音:2 是 1 的倚音,画在 1 的右上角
|
|
36
|
-
{3 2}>1 多个倚音用大括号括起来
|
|
37
|
-
1>2>3 允许嵌套:1 是 2 的倚音,2 是 3 的倚音
|
|
34
|
+
example: `前倚音: @grace(宿主, 倚音, side=pre) 后倚音: @grace(宿主, 倚音, side=post)
|
|
35
|
+
语法糖:'>' 与 '<',箭头永远指向宿主
|
|
36
|
+
2>1 前倚音:2 是 1 的倚音,画在 1 的左上角
|
|
37
|
+
1<2 后倚音:2 是 1 的倚音,画在 1 的右上角
|
|
38
|
+
{3 2}>1 多个倚音用大括号括起来
|
|
39
|
+
1>2>3 允许嵌套:1 是 2 的倚音,2 是 3 的倚音
|
|
38
40
|
倚音默认就是八分音符(自带一条减时线),再写 '/' 变成十六分音符`,
|
|
39
41
|
allowExtraArgs: true,
|
|
40
42
|
args: [
|
|
@@ -55,7 +57,7 @@ class GraceFunction extends ASTFunctionNode {
|
|
|
55
57
|
}
|
|
56
58
|
],
|
|
57
59
|
};
|
|
58
|
-
labelable() { return this; }
|
|
60
|
+
labelable() { return ASTFunctionNode.findLabelable(this.host); }
|
|
59
61
|
static deSugarAtom(source, start, _end) {
|
|
60
62
|
let char = source[start];
|
|
61
63
|
switch (char) {
|
|
@@ -97,7 +99,9 @@ class GraceFunction extends ASTFunctionNode {
|
|
|
97
99
|
let replaceFrom = left;
|
|
98
100
|
// 对 label 的特判: 目标变为label到被标记的节点范围内的所有节点
|
|
99
101
|
if (leftNode instanceof ASTLabelNode) {
|
|
100
|
-
|
|
102
|
+
let tgt = leftNode.target;
|
|
103
|
+
while (tgt && !ctx.nodes.includes(tgt))
|
|
104
|
+
tgt = tgt.parent;
|
|
101
105
|
for (let j = left - 1; j >= 0; j--) {
|
|
102
106
|
if (ctx.nodes[j] === tgt) {
|
|
103
107
|
leftNode = new ASTBraceNode({
|
|
@@ -130,22 +134,6 @@ class GraceFunction extends ASTFunctionNode {
|
|
|
130
134
|
}
|
|
131
135
|
throw new ErrorDiagnostic("E_GRACE_NO_TARGET", "@grace语法糖错误: 右边没有找到目标", n.span);
|
|
132
136
|
}
|
|
133
|
-
/**
|
|
134
|
-
* 语义确定后,把提升上来的修饰交还给宿主成员
|
|
135
|
-
*
|
|
136
|
-
* lowering 期间修饰必须挂在复合节点上,自动连梁之类的 augmenter 才看得到它的节奏;
|
|
137
|
-
* 但渲染上「宿主代表整个复合体」,减时线要画在宿主音符下面而不是整个盒子下面。
|
|
138
|
-
*/
|
|
139
|
-
static loweringFinalize = (result) => {
|
|
140
|
-
for (const column of result.columns) {
|
|
141
|
-
for (const node of column) {
|
|
142
|
-
if (!(node instanceof GraceTemporal) || !node.addon)
|
|
143
|
-
continue;
|
|
144
|
-
node.host.addon = node.addon;
|
|
145
|
-
node.addon = void 0;
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
};
|
|
149
137
|
host;
|
|
150
138
|
grace;
|
|
151
139
|
side = "pre";
|
|
@@ -197,6 +185,7 @@ class GraceFunction extends ASTFunctionNode {
|
|
|
197
185
|
if (this.side !== "pre" && this.side !== "post") {
|
|
198
186
|
throw new ErrorDiagnostic("E_GRACE_INVALID_SIDE", "@grace 的 side 参数必须是 'pre' 或 'post'", span);
|
|
199
187
|
}
|
|
188
|
+
this.adopt(this.host);
|
|
200
189
|
this.setGrace(this.grace, scale);
|
|
201
190
|
this.size = ctx.fontSize;
|
|
202
191
|
}
|
|
@@ -223,8 +212,11 @@ class GraceFunction extends ASTFunctionNode {
|
|
|
223
212
|
? [this.grace, this.host]
|
|
224
213
|
: [this.host, this.grace];
|
|
225
214
|
for (const content of order) {
|
|
226
|
-
const events = ctx.trackedEvents(content,
|
|
215
|
+
const events = ctx.trackedEvents(content, new Fraction(), track).flat();
|
|
227
216
|
const visible = events.filter(isVisualTemporalNode);
|
|
217
|
+
// 并行分支的纵向关系由引擎解 Track 树,而折叠成员进不了引擎
|
|
218
|
+
if (events.some(event => event.track !== track))
|
|
219
|
+
throw new ErrorDiagnostic("E_GRACE_PARALLEL_CONTENT", "@grace 的内容不能包含多声部结构(& / @voices);要在倚音里叠音请用 ^", content.sourceSpan);
|
|
228
220
|
if (content === this.host) {
|
|
229
221
|
if (visible.length !== 1 || visible.length !== events.length) {
|
|
230
222
|
throw new ErrorDiagnostic("E_GRACE_INVALID_HOST", "@grace 的宿主必须恰好产生一个可见 Temporal", content.sourceSpan);
|
|
@@ -239,7 +231,15 @@ class GraceFunction extends ASTFunctionNode {
|
|
|
239
231
|
}
|
|
240
232
|
}
|
|
241
233
|
});
|
|
242
|
-
|
|
234
|
+
const composite = new GraceTemporal(this, host, graces, this.side);
|
|
235
|
+
// 倚音同段无条件全部相连,只被零时长标记切断,所以 grace 一定认识 beam,允许耦合
|
|
236
|
+
// 折叠成员不进全局时间列,autobeam 看不到它们,要手动调用且强制连接
|
|
237
|
+
for (const run of composite.graceRuns) {
|
|
238
|
+
if (run.length < 2)
|
|
239
|
+
continue;
|
|
240
|
+
ctx.addAttachment(createBeamLayoutAttachment([...run], false, this.grace.sourceSpan));
|
|
241
|
+
}
|
|
242
|
+
return [composite];
|
|
243
243
|
}
|
|
244
244
|
toString(source) {
|
|
245
245
|
const host = this.host?.toString(source) ?? "";
|
|
@@ -258,30 +258,28 @@ const HOOK_COMMANDS = [
|
|
|
258
258
|
const HOOK_WIDTH = 0.07;
|
|
259
259
|
/** 倚音块底边到宿主顶边的视觉间隙;倚音线会探进这段空间指向宿主 */
|
|
260
260
|
const GRACE_RISE = 0.18;
|
|
261
|
-
/**
|
|
262
|
-
const MAX_STEAL_RATIO =
|
|
263
|
-
/** 减时线粗细占字号的比例 */
|
|
264
|
-
const DIV_LINE_WIDTH = 0.055;
|
|
261
|
+
/** 倪音向宿主借时值的上限,防止宿主被偷光;只读 */
|
|
262
|
+
const MAX_STEAL_RATIO = new Fraction(3, 4);
|
|
265
263
|
/** 倚音复合体:宿主与倚音都折叠在这一个盒子里,对外只是一个可见事件 */
|
|
266
264
|
export class GraceTemporal extends TemporalNodeBase {
|
|
267
265
|
host;
|
|
268
266
|
graces;
|
|
267
|
+
/** 承担节奏的倚音成员,按零时长标记切段;同段视觉上首尾相接,无条件连成一束减时线 */
|
|
268
|
+
graceRuns;
|
|
269
269
|
side;
|
|
270
270
|
/** 成员相对本盒左上角的局部偏移,onPlaced 时同步为绝对坐标 */
|
|
271
271
|
hostOffset = { x: 0, y: 0 };
|
|
272
272
|
graceOffsets = [];
|
|
273
273
|
/** 倚音线起点,同样是局部偏移 */
|
|
274
274
|
hookOrigin = null;
|
|
275
|
-
divLines = [];
|
|
276
275
|
constructor(ast, host, graces, side) {
|
|
277
276
|
super();
|
|
278
277
|
this.ast = ast;
|
|
279
278
|
this.host = host;
|
|
280
279
|
this.graces = graces;
|
|
281
280
|
this.side = side;
|
|
282
|
-
this.
|
|
283
|
-
this.
|
|
284
|
-
this.type = host.type;
|
|
281
|
+
this.T.copyFrom(host.T);
|
|
282
|
+
this.mergeKey = host.mergeKey;
|
|
285
283
|
this.initLayoutBox();
|
|
286
284
|
// 宿主携带整体节奏,它的修饰暂时成为复合体的修饰,自动连梁才看得到
|
|
287
285
|
if (host.addon)
|
|
@@ -289,20 +287,39 @@ export class GraceTemporal extends TemporalNodeBase {
|
|
|
289
287
|
host.addon = void 0;
|
|
290
288
|
// 成员不进入全局 columns,对外由复合体代表:写在成员上的标签仍可做关系端点
|
|
291
289
|
host.foldedInto = this;
|
|
290
|
+
const runs = [];
|
|
291
|
+
let run = [];
|
|
292
292
|
for (const grace of graces) {
|
|
293
293
|
grace.foldedInto = this;
|
|
294
|
+
// 调号、速度这类零时长标记不承担节奏,还会在视觉上把倚音串切断
|
|
295
|
+
if (grace.T.isZero()) {
|
|
296
|
+
if (run.length > 0)
|
|
297
|
+
runs.push(run);
|
|
298
|
+
run = [];
|
|
299
|
+
continue;
|
|
300
|
+
}
|
|
301
|
+
run.push(grace);
|
|
294
302
|
// 倚音默认就是八分音符:补一条减时线,书面时值随之减半
|
|
295
303
|
const addon = grace.addon = { ...grace.addon };
|
|
296
304
|
addon[DIV_ADDON_KEY] = (Number(addon[DIV_ADDON_KEY]) || 0) + 1;
|
|
297
|
-
grace.T
|
|
305
|
+
grace.T.divPow2();
|
|
298
306
|
}
|
|
307
|
+
if (run.length > 0)
|
|
308
|
+
runs.push(run);
|
|
309
|
+
this.graceRuns = runs;
|
|
299
310
|
}
|
|
311
|
+
/** 成员共享全局时间状态,按发声顺序固化 */
|
|
300
312
|
onTimeState(state) {
|
|
301
|
-
|
|
302
|
-
|
|
313
|
+
// 同步时间的修改。比如tuplet修改的是GraceTemporal.T,这里要把它传给成员
|
|
314
|
+
this.host.T.copyFrom(this.T);
|
|
315
|
+
const members = this.side === "pre"
|
|
316
|
+
? [...this.graces, this.host]
|
|
317
|
+
: [this.host, ...this.graces];
|
|
318
|
+
for (const member of members) {
|
|
319
|
+
member.t.copyFrom(this.t);
|
|
303
320
|
member.track = this.track;
|
|
304
321
|
member.layoutLine = this.layoutLine;
|
|
305
|
-
member.onTimeState?.(
|
|
322
|
+
member.onTimeState?.(state);
|
|
306
323
|
}
|
|
307
324
|
}
|
|
308
325
|
/**
|
|
@@ -311,9 +328,35 @@ export class GraceTemporal extends TemporalNodeBase {
|
|
|
311
328
|
* 比例 = 倚音字面总时值 / 四分音符,而四分音符 T=1,所以直接累加成员的 T。
|
|
312
329
|
* 基准取宿主经过 div 与 dot 后的实际时值;延时线是独立事件、tie 不改 T,天然不计入。
|
|
313
330
|
*/
|
|
314
|
-
|
|
315
|
-
const written =
|
|
316
|
-
|
|
331
|
+
emitPlayback(emitter) {
|
|
332
|
+
const written = new Fraction();
|
|
333
|
+
for (const grace of this.graces)
|
|
334
|
+
written.add(grace.T);
|
|
335
|
+
const ratio = written.compare(MAX_STEAL_RATIO) > 0 ? MAX_STEAL_RATIO : written;
|
|
336
|
+
const total = emitter.end.clone().sub(emitter.start);
|
|
337
|
+
const steal = total.clone().mul(ratio);
|
|
338
|
+
const hostDuration = total.sub(steal);
|
|
339
|
+
const start = emitter.start.clone();
|
|
340
|
+
const playGraces = (cursor) => {
|
|
341
|
+
for (const grace of this.graces) {
|
|
342
|
+
if (grace.T.isZero() || written.isZero()) {
|
|
343
|
+
emitter.play(grace, cursor, new Fraction());
|
|
344
|
+
continue;
|
|
345
|
+
}
|
|
346
|
+
const duration = steal.clone().mul(grace.T).div(written);
|
|
347
|
+
emitter.play(grace, cursor, duration);
|
|
348
|
+
cursor.add(duration);
|
|
349
|
+
}
|
|
350
|
+
};
|
|
351
|
+
if (this.side === "pre") {
|
|
352
|
+
const cursor = start.clone();
|
|
353
|
+
playGraces(cursor);
|
|
354
|
+
emitter.play(this.host, cursor, hostDuration);
|
|
355
|
+
}
|
|
356
|
+
else {
|
|
357
|
+
emitter.play(this.host, start, hostDuration);
|
|
358
|
+
playGraces(start.clone().add(hostDuration));
|
|
359
|
+
}
|
|
317
360
|
}
|
|
318
361
|
/**
|
|
319
362
|
* 宿主留在轨道基线上,倚音整体抬到它的左上或右上角
|
|
@@ -322,6 +365,12 @@ export class GraceTemporal extends TemporalNodeBase {
|
|
|
322
365
|
* 因此左右邻居会被固有宽度推开,不会在空间紧张时被压穿。
|
|
323
366
|
*/
|
|
324
367
|
prepareLayout(context) {
|
|
368
|
+
// lowering 期间修饰挂在复合体上(augmenter 要看到整体节奏),渲染时交给宿主:
|
|
369
|
+
// 减时线要落在宿主数字与下八度点之间,而不是压在整个倚音盒下面
|
|
370
|
+
if (this.addon) {
|
|
371
|
+
this.host.addon = this.addon;
|
|
372
|
+
this.addon = void 0;
|
|
373
|
+
}
|
|
325
374
|
for (const grace of this.graces)
|
|
326
375
|
prepareLayoutHost(grace, context);
|
|
327
376
|
prepareLayoutHost(this.host, context);
|
|
@@ -373,60 +422,15 @@ export class GraceTemporal extends TemporalNodeBase {
|
|
|
373
422
|
}
|
|
374
423
|
// 宿主没有肩线时它自己的盒顶就是肩线;有则上面那轮转发已经带偏移抬好了
|
|
375
424
|
this.ports[SHOULDER_PORT] ??= { x: this.box.anchor, y: this.hostOffset.y };
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
this.hookOrigin =
|
|
381
|
-
x: this.graceOffsets[near].x +
|
|
425
|
+
// 钩形曲线挂在靠近宿主的那个倚音成员下方,零时长标记不承接它
|
|
426
|
+
const near = this.side === "pre"
|
|
427
|
+
? this.graceRuns.at(-1)?.at(-1)
|
|
428
|
+
: this.graceRuns[0]?.[0];
|
|
429
|
+
this.hookOrigin = near ? {
|
|
430
|
+
x: this.graceOffsets[this.graces.indexOf(near)].x + near.box.anchor,
|
|
382
431
|
y: graceTop + lift + graceHeight,
|
|
383
432
|
} : null;
|
|
384
433
|
}
|
|
385
|
-
/**
|
|
386
|
-
* 把相邻倚音成员的同级减时线连成整条
|
|
387
|
-
*
|
|
388
|
-
* 折叠成员不进入全局时间列,自动连梁看不到它们,只能由复合体自己连。
|
|
389
|
-
*/
|
|
390
|
-
connectGraceDivLines() {
|
|
391
|
-
this.divLines.length = 0;
|
|
392
|
-
if (this.graces.length < 2)
|
|
393
|
-
return;
|
|
394
|
-
for (let level = 0;; level++) {
|
|
395
|
-
let hasPorts = false;
|
|
396
|
-
let run = [];
|
|
397
|
-
const flush = () => {
|
|
398
|
-
if (run.length >= 2) {
|
|
399
|
-
const first = run[0];
|
|
400
|
-
const last = run[run.length - 1];
|
|
401
|
-
const leftPort = this.graces[first].ports[divLinePortName(level, "left")];
|
|
402
|
-
const rightPort = this.graces[last].ports[divLinePortName(level, "right")];
|
|
403
|
-
this.divLines.push({
|
|
404
|
-
x1: this.graceOffsets[first].x + leftPort.x,
|
|
405
|
-
x2: this.graceOffsets[last].x + rightPort.x,
|
|
406
|
-
y: this.graceOffsets[first].y + leftPort.y,
|
|
407
|
-
});
|
|
408
|
-
for (const index of run)
|
|
409
|
-
claimDivLine(this.graces[index], level);
|
|
410
|
-
}
|
|
411
|
-
run = [];
|
|
412
|
-
};
|
|
413
|
-
for (let i = 0; i < this.graces.length; i++) {
|
|
414
|
-
const ports = this.graces[i].ports;
|
|
415
|
-
const left = ports[divLinePortName(level, "left")];
|
|
416
|
-
const right = ports[divLinePortName(level, "right")];
|
|
417
|
-
if (left || right)
|
|
418
|
-
hasPorts = true;
|
|
419
|
-
if (!left || !right) {
|
|
420
|
-
flush();
|
|
421
|
-
continue;
|
|
422
|
-
}
|
|
423
|
-
run.push(i);
|
|
424
|
-
}
|
|
425
|
-
flush();
|
|
426
|
-
if (!hasPorts)
|
|
427
|
-
break;
|
|
428
|
-
}
|
|
429
|
-
}
|
|
430
434
|
/** 引擎每次改变本节点坐标后,按准备阶段保存的局部偏移重算成员绝对坐标 */
|
|
431
435
|
onPlaced() {
|
|
432
436
|
this.host.box.x = this.box.x + this.hostOffset.x;
|
|
@@ -444,14 +448,9 @@ export class GraceTemporal extends TemporalNodeBase {
|
|
|
444
448
|
for (const decoration of member.decorations)
|
|
445
449
|
decoration.paint(painter);
|
|
446
450
|
}
|
|
447
|
-
const graceEm = this.ast.size * GRACE_SCALE;
|
|
448
|
-
const divWidth = Math.max(0.8, graceEm * DIV_LINE_WIDTH);
|
|
449
|
-
for (const line of this.divLines) {
|
|
450
|
-
const y = this.box.y + line.y;
|
|
451
|
-
painter.drawLine(this.box.x + line.x1, y, this.box.x + line.x2, y, { stroke: "#000", strokeWidth: divWidth });
|
|
452
|
-
}
|
|
453
451
|
if (!this.hookOrigin)
|
|
454
452
|
return;
|
|
453
|
+
const graceEm = this.ast.size * GRACE_SCALE;
|
|
455
454
|
const hookOrigin = this.hookOrigin;
|
|
456
455
|
const dir = this.side === "pre" ? 1 : -1;
|
|
457
456
|
painter.drawPath(HOOK_COMMANDS, { stroke: "#000", strokeWidth: Math.max(0.8, graceEm * HOOK_WIDTH) }, {
|