jpfun 1.0.0 → 1.2.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 +1 -1
- package/dist/diagnostic.d.ts +1 -0
- package/dist/diagnostic.js +1 -0
- package/dist/functions/ASTtypes.d.ts +2 -3
- package/dist/functions/ASTtypes.js +2 -3
- package/dist/functions/adjust/index.js +15 -67
- package/dist/functions/bar/index.js +11 -7
- package/dist/functions/beam/auto.js +15 -11
- package/dist/functions/beam/layout.js +22 -48
- package/dist/functions/br/index.d.ts +9 -3
- package/dist/functions/br/index.js +20 -16
- package/dist/functions/dash/index.js +13 -11
- package/dist/functions/default.js +2 -1
- package/dist/functions/div/index.d.ts +2 -2
- package/dist/functions/div/index.js +5 -9
- package/dist/functions/dot/index.js +5 -9
- package/dist/functions/dyn/index.js +125 -53
- package/dist/functions/gliss/index.js +81 -7
- package/dist/functions/grace/index.d.ts +3 -10
- package/dist/functions/grace/index.js +25 -57
- package/dist/functions/head/index.js +6 -7
- package/dist/functions/key/index.js +1 -0
- package/dist/functions/meter/index.js +7 -5
- package/dist/functions/note/index.d.ts +5 -2
- package/dist/functions/note/index.js +162 -19
- package/dist/functions/note/source-transform.d.ts +8 -2
- package/dist/functions/note/source-transform.js +25 -11
- package/dist/functions/ottava/index.js +8 -7
- package/dist/functions/page/index.d.ts +1 -36
- package/dist/functions/page/index.js +11 -15
- package/dist/functions/pagebreak/index.js +2 -1
- package/dist/functions/rest/index.js +5 -5
- package/dist/functions/set/index.js +2 -2
- package/dist/functions/stack/index.js +7 -7
- package/dist/functions/symbol/symbols/dynamics.d.ts +3 -0
- package/dist/functions/symbol/symbols/dynamics.js +10 -1
- package/dist/functions/tempo/index.js +8 -3
- package/dist/functions/temporal.d.ts +21 -6
- package/dist/functions/temporal.js +1 -1
- package/dist/functions/text/index.js +3 -5
- package/dist/functions/up/index.js +37 -34
- package/dist/functions/voice/connections.js +2 -2
- package/dist/functions/voice/lyrics.d.ts +2 -0
- package/dist/functions/voice/lyrics.js +4 -4
- package/dist/functions/voice/voice.d.ts +2 -1
- package/dist/functions/voice/voice.js +9 -15
- package/dist/functions/voice/voices.d.ts +6 -4
- package/dist/functions/voice/voices.js +10 -10
- package/dist/index.d.ts +6 -34
- package/dist/index.js +6 -34
- package/dist/jpfun.min.js +49 -46
- package/dist/layout/engine.js +237 -232
- package/dist/layout/model.d.ts +1 -1
- package/dist/layout/page.d.ts +2 -2
- package/dist/layout/page.js +8 -22
- package/dist/layout/types.d.ts +4 -5
- package/dist/lowering/accidentalMemory.d.ts +33 -0
- package/dist/lowering/accidentalMemory.js +84 -0
- package/dist/lowering/loweringContext.d.ts +12 -7
- package/dist/lowering/loweringContext.js +54 -51
- package/dist/lowering/types.d.ts +2 -3
- package/dist/parser/grammarType.d.ts +10 -4
- package/dist/parser/parse-utils/brace-utils.js +4 -3
- package/dist/parser/parse-utils/call-utils.js +7 -7
- package/dist/parser/parse-utils/string-utils.d.ts +1 -1
- package/dist/parser/parse-utils/string-utils.js +1 -1
- package/dist/parser/parserContext.d.ts +30 -34
- package/dist/parser/parserContext.js +77 -181
- package/dist/parser/preprocess.js +10 -23
- package/dist/parser/syntax.d.ts +9 -0
- package/dist/parser/syntax.js +109 -0
- package/dist/parser/types.d.ts +2 -1
- package/dist/pipeline.js +3 -2
- package/dist/playback/compile.js +26 -18
- package/dist/playback/event.d.ts +1 -2
- package/dist/playback/event.js +2 -3
- package/dist/playback/types.d.ts +0 -1
- package/dist/render/index.d.ts +1 -0
- package/dist/render/index.js +1 -0
- package/grammar.md +8 -6
- package/package.json +2 -2
- package/dist/functions/voice/index.d.ts +0 -2
- package/dist/functions/voice/index.js +0 -2
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { ASTFunctionNode, primaryName, resolveArgType } from "../ASTtypes.js";
|
|
2
2
|
import { WarningDiagnostic } from "../../diagnostic.js";
|
|
3
|
-
import {
|
|
3
|
+
import { quoteSourceString } from "../../parser/parse-utils/string-utils.js";
|
|
4
4
|
import { ParserContext } from "../../parser/parserContext.js";
|
|
5
5
|
class SetFunction extends ASTFunctionNode {
|
|
6
6
|
static def = {
|
|
@@ -63,7 +63,7 @@ class SetFunction extends ASTFunctionNode {
|
|
|
63
63
|
}
|
|
64
64
|
toString() {
|
|
65
65
|
return `@set(${Array.from(this.args, ([key, value]) => {
|
|
66
|
-
const text = typeof value === "string" ?
|
|
66
|
+
const text = typeof value === "string" ? quoteSourceString(value)
|
|
67
67
|
: value && typeof value === "object" && "unit" in value ? `${value.value}${value.unit}` : value;
|
|
68
68
|
return `${key}=${text}`;
|
|
69
69
|
}).join(", ")})`;
|
|
@@ -54,8 +54,8 @@ class StackFunction extends ASTFunctionNode {
|
|
|
54
54
|
if (source[start] === '&') {
|
|
55
55
|
const node = {
|
|
56
56
|
kind: "sugar",
|
|
57
|
-
data: StackFunction,
|
|
58
57
|
span: { start, end: start + 1 },
|
|
58
|
+
relation: StackFunction.deSugarRelation,
|
|
59
59
|
};
|
|
60
60
|
return { next: start + 1, node };
|
|
61
61
|
}
|
|
@@ -63,8 +63,6 @@ class StackFunction extends ASTFunctionNode {
|
|
|
63
63
|
}
|
|
64
64
|
static deSugarRelation(ctx, nodes, at) {
|
|
65
65
|
const n = nodes[at++];
|
|
66
|
-
if (!(n.kind === "sugar" && n.data === StackFunction))
|
|
67
|
-
return null;
|
|
68
66
|
const leftNode = ctx.takeLeftOperand();
|
|
69
67
|
if (!leftNode) {
|
|
70
68
|
throw new ErrorDiagnostic("E_STACK_NO_TARGET", "@stack语法糖错误: 左边没有找到可叠加的目标", n.span);
|
|
@@ -72,10 +70,12 @@ class StackFunction extends ASTFunctionNode {
|
|
|
72
70
|
const host = leftNode instanceof StackFunction
|
|
73
71
|
? leftNode
|
|
74
72
|
: new StackFunction(n.span, new Map(), ctx).addContent(leftNode);
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
73
|
+
ctx.deferRightOperand(right => {
|
|
74
|
+
if (!right)
|
|
75
|
+
throw new ErrorDiagnostic("E_STACK_NO_TARGET", "@stack语法糖错误: 右边没有找到可叠加的目标", n.span);
|
|
76
|
+
return host.addContent(right);
|
|
77
|
+
});
|
|
78
|
+
return at;
|
|
79
79
|
}
|
|
80
80
|
contents = [];
|
|
81
81
|
get children() { return this.contents; }
|
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import type { TemporalNodeBase } from "../../temporal.js";
|
|
1
2
|
import type { SymbolDefinition } from "../index.js";
|
|
3
|
+
/** 节点是力度记号时返回它设置的力度 */
|
|
4
|
+
export declare function dynamicMarkVelocity(node: TemporalNodeBase): number | undefined;
|
|
2
5
|
/** 默认力度与 MuseScore 一致;mf 恰好等于 DEFAULT_VELOCITY */
|
|
3
6
|
export declare const dynamicSymbols: readonly SymbolDefinition[];
|
|
@@ -142,7 +142,16 @@ const INK = { fill: "#000" };
|
|
|
142
142
|
const P_WEIGHT = 0.79341;
|
|
143
143
|
const P_VELOCITIES = [49, 33, 16, 10, 5, 1];
|
|
144
144
|
const F_VELOCITIES = [96, 112, 126, 127];
|
|
145
|
+
/** 力度记号的 onTimeState → 力度;符号节点直接引用定义里的回调,因而可按引用识别 */
|
|
146
|
+
const markVelocities = new WeakMap();
|
|
147
|
+
/** 节点是力度记号时返回它设置的力度 */
|
|
148
|
+
export function dynamicMarkVelocity(node) {
|
|
149
|
+
const onTimeState = node.onTimeState;
|
|
150
|
+
return onTimeState && markVelocities.get(onTimeState);
|
|
151
|
+
}
|
|
145
152
|
function dynamic(name, velocity, description) {
|
|
153
|
+
const onTimeState = (state) => { state.velocity = velocity; };
|
|
154
|
+
markVelocities.set(onTimeState, velocity);
|
|
146
155
|
let dx = 0;
|
|
147
156
|
const shapes = Array.from(name, (letter, index) => {
|
|
148
157
|
const shape = { path: GLYPHS[letter], style: INK, dx };
|
|
@@ -154,7 +163,7 @@ function dynamic(name, velocity, description) {
|
|
|
154
163
|
description,
|
|
155
164
|
shapes,
|
|
156
165
|
weight: name.includes("f") ? 1 : P_WEIGHT,
|
|
157
|
-
onTimeState
|
|
166
|
+
onTimeState,
|
|
158
167
|
};
|
|
159
168
|
}
|
|
160
169
|
/** 默认力度与 MuseScore 一致;mf 恰好等于 DEFAULT_VELOCITY */
|
|
@@ -47,6 +47,7 @@ class TempoFunction extends ASTFunctionNode {
|
|
|
47
47
|
export const TempoNode = TempoFunction;
|
|
48
48
|
class TempoTemporalNode extends TemporalNodeBase {
|
|
49
49
|
/** 字号和 bpm 在解析时已经固化,字形、文本、样式随之不再变 */
|
|
50
|
+
bpm;
|
|
50
51
|
glyph;
|
|
51
52
|
text;
|
|
52
53
|
style;
|
|
@@ -61,13 +62,17 @@ class TempoTemporalNode extends TemporalNodeBase {
|
|
|
61
62
|
this.style = { fontSize: ast.size, fontFamily: ast.font, fill: "#000" };
|
|
62
63
|
// 非法值不入时间状态,也不告知播放系统,效果是保持上一个速度
|
|
63
64
|
if (Number.isFinite(ast.bpm) && ast.bpm > 0)
|
|
64
|
-
this.
|
|
65
|
+
this.bpm = ast.bpm;
|
|
65
66
|
this.initLayoutBox();
|
|
66
67
|
}
|
|
67
68
|
onTimeState(state) {
|
|
68
|
-
|
|
69
|
+
if (this.bpm !== undefined)
|
|
70
|
+
state.bpm = this.bpm;
|
|
71
|
+
}
|
|
72
|
+
emitPlayback(emitter) {
|
|
73
|
+
const bpm = this.bpm;
|
|
69
74
|
if (bpm !== undefined)
|
|
70
|
-
state.bpm = bpm;
|
|
75
|
+
emitter.control(emitter.start, state => { state.bpm = bpm; });
|
|
71
76
|
}
|
|
72
77
|
prepareLayout(context) {
|
|
73
78
|
const glyph = this.glyph;
|
|
@@ -6,6 +6,7 @@ import { type ASTNodeBase } from "./ASTtypes.js";
|
|
|
6
6
|
import { Fraction } from "../fraction.js";
|
|
7
7
|
import type { HorizontalSpringConfig, LayoutBox, LayoutDecoration, LayoutHost, LayoutPoint, LayoutPrepareContext, HorizontalLineView, TimeLineEvent } from "../layout/types.js";
|
|
8
8
|
import type { Track } from "../lowering/track.js";
|
|
9
|
+
import type { AccidentalMemory } from "../lowering/accidentalMemory.js";
|
|
9
10
|
import type { PlaybackEmitter, PlaybackState } from "../playback/types.js";
|
|
10
11
|
import type { Painter } from "../render/types.js";
|
|
11
12
|
/**
|
|
@@ -21,18 +22,28 @@ export interface TimeState {
|
|
|
21
22
|
velocity: number;
|
|
22
23
|
keySignature: string;
|
|
23
24
|
program: number;
|
|
25
|
+
/** 小节内变音记忆。系统字段,由 solidifyColumns 创建,任何时刻都能直接读写 */
|
|
26
|
+
accidentals: AccidentalMemory;
|
|
24
27
|
/** 当前小节时长(QN);尚无显式拍号或散板时为 undefined */
|
|
25
28
|
meter?: Fraction;
|
|
29
|
+
/** 当前可输出的拍号;尚无显式拍号或散板时为 undefined */
|
|
30
|
+
timeSignature?: {
|
|
31
|
+
readonly numerator: number;
|
|
32
|
+
readonly denominator: number;
|
|
33
|
+
};
|
|
26
34
|
[key: string]: any;
|
|
27
35
|
}
|
|
28
36
|
export declare const DEFAULT_BPM = 120;
|
|
29
37
|
export declare const DEFAULT_VELOCITY = 80;
|
|
30
38
|
export declare const DEFAULT_TONALITY = "C4";
|
|
31
39
|
export declare const DEFAULT_PROGRAM = 0;
|
|
32
|
-
/** 每个事件的可选语义声明;核心字段与函数私有的
|
|
40
|
+
/** 每个事件的可选语义声明;核心字段与函数私有的 @ 键分开 */
|
|
33
41
|
export interface TemporalAddon {
|
|
34
|
-
/**
|
|
35
|
-
breakBefore?:
|
|
42
|
+
/** 在当前时间列之前换一行,值为与上一行的间距(px);同列逐项取较大值,同轨连续多列各换一行 */
|
|
43
|
+
breakBefore?: {
|
|
44
|
+
gap: number;
|
|
45
|
+
minGap: number;
|
|
46
|
+
};
|
|
36
47
|
/** 在所属行之前强制换页;配合 breakBefore 使用,布尔值控制上一页的余白分配 */
|
|
37
48
|
pageBreakBefore?: boolean;
|
|
38
49
|
[key: `@${string}`]: unknown;
|
|
@@ -85,7 +96,10 @@ export declare class TemporalNodeBase implements TimeLineEvent {
|
|
|
85
96
|
layoutLine: number;
|
|
86
97
|
/** 不可见状态事件保持 undefined */
|
|
87
98
|
box?: LayoutBox;
|
|
88
|
-
/**
|
|
99
|
+
/**
|
|
100
|
+
* 后置平移:在 lowering 阶段声明,引擎在占用测量后叠加到 box 上并再次调用 onPlaced,不参与占用求解
|
|
101
|
+
* 折叠成员经复合体的 onPlaced 跟随外层平移,再叠加自己的平移
|
|
102
|
+
*/
|
|
89
103
|
placementOffset?: LayoutPoint;
|
|
90
104
|
/**
|
|
91
105
|
* 横向弹簧布局参数
|
|
@@ -139,7 +153,7 @@ export declare class TemporalNodeBase implements TimeLineEvent {
|
|
|
139
153
|
emitPlayback?(emitter: PlaybackEmitter): void;
|
|
140
154
|
/** 给自己所在的时间列贴播放标记,供控制流游标查询 */
|
|
141
155
|
playbackMarks?(): readonly string[];
|
|
142
|
-
/**
|
|
156
|
+
/** 向播放系统告知自己这个记谱位置上的力度、音色等状态 */
|
|
143
157
|
playbackState?: PlaybackState;
|
|
144
158
|
/**
|
|
145
159
|
* 根据已经固化的语义生成固有尺寸
|
|
@@ -155,8 +169,9 @@ export declare class TemporalNodeBase implements TimeLineEvent {
|
|
|
155
169
|
*/
|
|
156
170
|
finalizeLayout?(_ctx: LayoutPrepareContext): void;
|
|
157
171
|
/**
|
|
158
|
-
* 布局器每次改变 x 或 y
|
|
172
|
+
* 布局器每次改变 x 或 y 后调用,包括施加后置平移之后
|
|
159
173
|
* 普通叶节点不需要处理,具有内部几何的复合节点可用它同步子对象坐标
|
|
174
|
+
* 只按当前 box 同步:成员自己的后置平移由引擎随后叠加,所以只能由引擎调度
|
|
160
175
|
*/
|
|
161
176
|
onPlaced?(): void;
|
|
162
177
|
/** 只执行绘制,不允许在这里重新测量或改变 LayoutBox */
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { ASTFunctionNode } from "../ASTtypes.js";
|
|
2
2
|
import { ErrorDiagnostic } from "../../diagnostic.js";
|
|
3
|
-
import { findClosingQuote,
|
|
3
|
+
import { findClosingQuote, quoteSourceString } from "../../parser/parse-utils/string-utils.js";
|
|
4
4
|
import { DEFAULT_KEY, TemporalNodeBase } from "../temporal.js";
|
|
5
5
|
import { paintRichLine, parseRichText, prepareRichLine } from "../../render/text.js";
|
|
6
6
|
class TextFunction extends ASTFunctionNode {
|
|
@@ -58,16 +58,14 @@ class TextFunction extends ASTFunctionNode {
|
|
|
58
58
|
return null; // 未闭合就不去糖,与预处理“配对才算字符串”一致
|
|
59
59
|
const node = {
|
|
60
60
|
kind: "sugar",
|
|
61
|
-
data: TextFunction,
|
|
62
61
|
span: { start, end: close + 1 },
|
|
63
62
|
syntaxKind: "string",
|
|
63
|
+
relation: TextFunction.deSugarRelation,
|
|
64
64
|
};
|
|
65
65
|
return { next: close + 1, node };
|
|
66
66
|
}
|
|
67
67
|
static deSugarRelation(ctx, nodes, at) {
|
|
68
68
|
const node = nodes[at];
|
|
69
|
-
if (node.data !== TextFunction)
|
|
70
|
-
return null;
|
|
71
69
|
// 之所以不在 deSugarAtom 里直接解码返回 typed call,是因为字符串转义可能产生诊断(如非法 \u 转义):
|
|
72
70
|
// 第一轮 deSugarAtom 没有 ctx(抛异常则会中断词法扫描),放到第二轮才能复用 parseArgWithType 记录警告并回退原文
|
|
73
71
|
ctx.pushNode(new TextFunction(node.span, new Map([[0, ctx.parseArgWithType(node.span, "string")]]), ctx, null));
|
|
@@ -92,7 +90,7 @@ class TextFunction extends ASTFunctionNode {
|
|
|
92
90
|
}
|
|
93
91
|
loweringEnter() { return [new TextTemporalNode(this)]; }
|
|
94
92
|
toString() {
|
|
95
|
-
return `@text(${
|
|
93
|
+
return `@text(${quoteSourceString(this.text)}, size=${this.size}px, lineheight=${this.lineHeight}, align=${this.align}, font=${quoteSourceString(this.font)})`;
|
|
96
94
|
}
|
|
97
95
|
}
|
|
98
96
|
export const TextNode = TextFunction;
|
|
@@ -92,7 +92,7 @@ class FoldFunction extends ASTFunctionNode {
|
|
|
92
92
|
for (const content of this.children) {
|
|
93
93
|
const columns = ctx.trackedEvents(content, new Fraction(), track);
|
|
94
94
|
const member = columns.length === 1 && columns[0].length === 1 ? columns[0][0] : undefined;
|
|
95
|
-
if (!member || !isVisualTemporalNode(member) ||
|
|
95
|
+
if (!member || !isVisualTemporalNode(member) || member.addon?.breakBefore) {
|
|
96
96
|
throw new ErrorDiagnostic("E_UP_INVALID_CHILD", "@up 的每个参数必须恰好产生一个可见 Temporal,且不能换行或包含多声部结构", content.sourceSpan);
|
|
97
97
|
}
|
|
98
98
|
members.push(member);
|
|
@@ -116,7 +116,7 @@ class FoldFunction extends ASTFunctionNode {
|
|
|
116
116
|
*
|
|
117
117
|
* 被掏空的一方必须撤销候选登记:空壳已不在 AST 里,留在表中会让 @tie() 取到孤儿端点。
|
|
118
118
|
*/
|
|
119
|
-
flatten(drop, side, ctx, gap = this.gap) {
|
|
119
|
+
flatten(drop, side, ctx, gap = this.gap, registered = true) {
|
|
120
120
|
// 成员顺序跟随源码;比较必须在 growSpan 之前
|
|
121
121
|
const front = drop.sourceSpan.start < this.sourceSpan.start ? drop : this;
|
|
122
122
|
const back = front === drop ? this : drop;
|
|
@@ -133,41 +133,38 @@ class FoldFunction extends ASTFunctionNode {
|
|
|
133
133
|
c.parent = this;
|
|
134
134
|
this.growSpan(drop.sourceSpan);
|
|
135
135
|
drop.contents.length = drop.belows.length = drop.aboveGaps.length = drop.belowGaps.length = 0;
|
|
136
|
+
if (!registered)
|
|
137
|
+
return this; // 语法糖新建的左包装从未入表;合成在本层末尾,整表扫描会让平放和弦变成平方级
|
|
136
138
|
const stale = ctx.labelableNodes.lastIndexOf(drop);
|
|
137
139
|
if (stale >= 0)
|
|
138
140
|
ctx.labelableNodes.splice(stale, 1);
|
|
139
141
|
return this;
|
|
140
142
|
}
|
|
141
|
-
/**
|
|
142
|
-
|
|
143
|
-
* `UpFunction.desugar(...)` 这种带显式接收者的形式 —— 解析器是把
|
|
144
|
-
* `deSugarRelation` 摘成游离函数引用来调的,`this` 到不了这里。
|
|
145
|
-
*/
|
|
146
|
-
static desugar(ctx, nodes, at) {
|
|
143
|
+
/** `^` / `_` 共用的关系去糖;sugar 的 data 是发出它的子类,方向和构造都从它取 */
|
|
144
|
+
static deSugarRelation(ctx, nodes, at) {
|
|
147
145
|
const n = nodes[at++];
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
const label = this.side === "above" ? "@up" : "@down";
|
|
146
|
+
const cls = n.data;
|
|
147
|
+
const label = cls.side === "above" ? "@up" : "@down";
|
|
151
148
|
const leftNode = ctx.takeLeftOperand();
|
|
152
149
|
if (!leftNode)
|
|
153
150
|
throw new ErrorDiagnostic("E_FOLD_NO_TARGET", "{fn}语法糖错误: 左边没有找到可叠加的目标", n.span, { fn: label });
|
|
154
|
-
const wrapper = new
|
|
151
|
+
const wrapper = new cls({ ...n.span }, new Map(), ctx);
|
|
155
152
|
const host = leftNode instanceof FoldFunction
|
|
156
153
|
? leftNode
|
|
157
154
|
: wrapper.addContent(leftNode, "above", ctx);
|
|
158
|
-
|
|
159
|
-
|
|
155
|
+
ctx.deferRightOperand(right => {
|
|
156
|
+
if (!right)
|
|
157
|
+
throw new ErrorDiagnostic("E_FOLD_NO_TARGET", "{fn}语法糖错误: 右边没有找到可叠加的目标", n.span, { fn: label });
|
|
158
|
+
// 右操作数可能已被 @tie() 等按引用取走(它就在本层剩余部分里,先于合成解析),
|
|
160
159
|
// 而新建的左包装无人引用,所以合并时让右边存活;接收者就是存活的那一方
|
|
161
160
|
const merged = right instanceof FoldFunction
|
|
162
|
-
? right.flatten(host,
|
|
163
|
-
: host.addContent(right,
|
|
161
|
+
? right.flatten(host, cls.side, ctx, wrapper.gap, host !== wrapper)
|
|
162
|
+
: host.addContent(right, cls.side, ctx, wrapper.gap);
|
|
164
163
|
// 语法糖不走 pushNode,这里补登记;null 同样入表,和显式调用一样成为边界
|
|
165
164
|
ctx.labelableNodes.push(merged.labelable());
|
|
166
165
|
return merged;
|
|
167
166
|
});
|
|
168
|
-
|
|
169
|
-
return next;
|
|
170
|
-
throw new ErrorDiagnostic("E_FOLD_NO_TARGET", "{fn}语法糖错误: 右边没有找到可叠加的目标", n.span, { fn: label });
|
|
167
|
+
return at;
|
|
171
168
|
}
|
|
172
169
|
}
|
|
173
170
|
class UpFunction extends FoldFunction {
|
|
@@ -190,12 +187,9 @@ class UpFunction extends FoldFunction {
|
|
|
190
187
|
static deSugarAtom(source, start, _end) {
|
|
191
188
|
if (source[start] !== "^")
|
|
192
189
|
return null;
|
|
193
|
-
const node = { kind: "sugar", data: UpFunction, span: { start, end: start + 1 } };
|
|
190
|
+
const node = { kind: "sugar", data: UpFunction, span: { start, end: start + 1 }, relation: FoldFunction.deSugarRelation };
|
|
194
191
|
return { next: start + 1, node };
|
|
195
192
|
}
|
|
196
|
-
static deSugarRelation(ctx, nodes, at) {
|
|
197
|
-
return UpFunction.desugar(ctx, nodes, at);
|
|
198
|
-
}
|
|
199
193
|
}
|
|
200
194
|
class DownFunction extends FoldFunction {
|
|
201
195
|
static side = "below";
|
|
@@ -216,12 +210,9 @@ class DownFunction extends FoldFunction {
|
|
|
216
210
|
static deSugarAtom(source, start, _end) {
|
|
217
211
|
if (source[start] !== "_")
|
|
218
212
|
return null;
|
|
219
|
-
const node = { kind: "sugar", data: DownFunction, span: { start, end: start + 1 } };
|
|
213
|
+
const node = { kind: "sugar", data: DownFunction, span: { start, end: start + 1 }, relation: FoldFunction.deSugarRelation };
|
|
220
214
|
return { next: start + 1, node };
|
|
221
215
|
}
|
|
222
|
-
static deSugarRelation(ctx, nodes, at) {
|
|
223
|
-
return DownFunction.desugar(ctx, nodes, at);
|
|
224
|
-
}
|
|
225
216
|
}
|
|
226
217
|
export const UpNode = UpFunction;
|
|
227
218
|
export const DownNode = DownFunction;
|
|
@@ -266,15 +257,27 @@ class FoldTemporal extends TemporalNodeBase {
|
|
|
266
257
|
}
|
|
267
258
|
}
|
|
268
259
|
/**
|
|
269
|
-
*
|
|
260
|
+
* 固化与播放共用的成员顺序:零时长成员(记号、设置、文字)→ 有时长成员 → 宿主,组内倒序。
|
|
261
|
+
* 记号因此先写入状态、先注册效果,上下两侧的音都能读到;宿主始终最后
|
|
262
|
+
*/
|
|
263
|
+
memberOrder() {
|
|
264
|
+
const marks = [];
|
|
265
|
+
const notes = [];
|
|
266
|
+
for (let i = this.members.length - 1; i > 0; i--) {
|
|
267
|
+
const member = this.members[i];
|
|
268
|
+
(member.T.isZero() ? marks : notes).push(member);
|
|
269
|
+
}
|
|
270
|
+
const host = this.members[0];
|
|
271
|
+
return host ? [...marks, ...notes, host] : [...marks, ...notes];
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* 成员共享全局时间状态,按 memberOrder 固化
|
|
270
275
|
*/
|
|
271
276
|
onTimeState(state) {
|
|
272
277
|
const duration = this.T.clone();
|
|
273
278
|
let offset = undefined;
|
|
274
279
|
let durationChanged = false;
|
|
275
|
-
|
|
276
|
-
for (let i = this.members.length - 1; i >= 0; i--) {
|
|
277
|
-
const member = this.members[i];
|
|
280
|
+
for (const member of this.memberOrder()) {
|
|
278
281
|
// 堆叠在一起的成员共享同一个时值,由第一个成员决定;
|
|
279
282
|
// 本来就没有时长的成员(标注、小节线等)保持 0,不会被拉长
|
|
280
283
|
if (!member.T.isZero() && !this.T.isZero())
|
|
@@ -298,9 +301,9 @@ class FoldTemporal extends TemporalNodeBase {
|
|
|
298
301
|
return offset;
|
|
299
302
|
}
|
|
300
303
|
emitPlayback(emitter) {
|
|
301
|
-
//
|
|
302
|
-
for (
|
|
303
|
-
emitter.play(
|
|
304
|
+
// 记号先发布,注册的局部变换因此作用到所有有声成员和宿主
|
|
305
|
+
for (const member of this.memberOrder())
|
|
306
|
+
emitter.play(member);
|
|
304
307
|
}
|
|
305
308
|
prepareHorizontal(line) {
|
|
306
309
|
this.members[0]?.prepareHorizontal?.(line);
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { ErrorDiagnostic } from "../../diagnostic.js";
|
|
2
2
|
import { pathBounds } from "../../layout/path.js";
|
|
3
|
-
import { findClosingQuote, parseString,
|
|
3
|
+
import { findClosingQuote, parseString, quoteSourceString } from "../../parser/parse-utils/string-utils.js";
|
|
4
4
|
export const CURVED_BRACE_COMMANDS = [
|
|
5
5
|
{ op: "M", x: 2.98438, y: -165.5 },
|
|
6
6
|
{ op: "C", cx1: 8.9375, cy1: -171.781, cx2: 20.5156, cy2: -184.688, x: 20.5156, y: -208.531 },
|
|
@@ -121,7 +121,7 @@ export function resolveConnections(specs, count, span) {
|
|
|
121
121
|
}
|
|
122
122
|
export function serializeConnections(specs) {
|
|
123
123
|
return specs.map(({ kind, from, to, name }) => {
|
|
124
|
-
const label = !name ? "" : `:${FORBIDDEN_RAW_NAME_RE.test(name) || name.trim() !== name ?
|
|
124
|
+
const label = !name ? "" : `:${FORBIDDEN_RAW_NAME_RE.test(name) || name.trim() !== name ? quoteSourceString(name) : name}`;
|
|
125
125
|
const range = `${from ?? ""}-${to ?? ""}${label}`;
|
|
126
126
|
return kind === "brace" ? `{${range}}` : `[${range}]`;
|
|
127
127
|
}).join("");
|
|
@@ -6,6 +6,8 @@ import type { VisualTemporalNode } from "../temporal.js";
|
|
|
6
6
|
import type { VoiceNameTemporal } from "./voice.js";
|
|
7
7
|
/** 歌词与歌词行名称相对声部字号的字号 */
|
|
8
8
|
export declare const LYRIC_SIZE_RATIO = 0.88;
|
|
9
|
+
export declare const LYRIC_OPENING_PUNCTUATION: Set<string>;
|
|
10
|
+
export declare const LYRIC_SILENT_PUNCTUATION: Set<string>;
|
|
9
11
|
/** 歌词行名称与歌词正文共用同一个基础样式 */
|
|
10
12
|
export declare function lyricNameStyle(size: number, fontFamily: string): TextStyle;
|
|
11
13
|
export type LyricSlot = RichTextRun[] | null;
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { getLayoutBounds } from "../../layout/engine.js";
|
|
2
|
-
import {
|
|
2
|
+
import { quoteSourceString } from "../../parser/parse-utils/string-utils.js";
|
|
3
3
|
import { appendRun, markdownToggles, paintRichLine, prepareRichLine, scanMarkdown, } from "../../render/text.js";
|
|
4
4
|
const WHITESPACE_RE = /\s/;
|
|
5
5
|
/** 歌词与歌词行名称相对声部字号的字号 */
|
|
6
6
|
export const LYRIC_SIZE_RATIO = 0.88;
|
|
7
7
|
// 分词跟后面的标点
|
|
8
|
-
const LYRIC_OPENING_PUNCTUATION = new Set("(([
|
|
8
|
+
export const LYRIC_OPENING_PUNCTUATION = new Set("(([[{〈《「『【〔〖〘〚‘“⒈⒉⒊⒋⒌");
|
|
9
9
|
// 不发声的标点;位于歌词槽首尾时只渲染,不占位,也不参与对齐中心计算
|
|
10
|
-
const LYRIC_SILENT_PUNCTUATION = new Set([
|
|
10
|
+
export const LYRIC_SILENT_PUNCTUATION = new Set([
|
|
11
11
|
...LYRIC_OPENING_PUNCTUATION,
|
|
12
12
|
...",。!?;:、,.!?;:))]]}〉》」』】〕〗〙〛’”\"'…—–",
|
|
13
13
|
]);
|
|
@@ -198,7 +198,7 @@ export function parseLyric(value, offsets, tokenSpans) {
|
|
|
198
198
|
return result;
|
|
199
199
|
}
|
|
200
200
|
export function serializeLyric(tokens) {
|
|
201
|
-
return
|
|
201
|
+
return quoteSourceString(tokens.flatMap((runs, i) => {
|
|
202
202
|
if (runs === null)
|
|
203
203
|
return [];
|
|
204
204
|
if (runs.length === 0)
|
|
@@ -30,7 +30,8 @@ export declare class VoiceFunction extends ASTFunctionNode {
|
|
|
30
30
|
next: number;
|
|
31
31
|
node: GrammarSugarNode;
|
|
32
32
|
} | null;
|
|
33
|
-
|
|
33
|
+
/** `N:` / `L:` 的关系去糖;V 声明分组时直接调用并传入 members 收集成员 */
|
|
34
|
+
static deSugarLine(ctx: ParserContext, nodes: (GrammarNode | number)[], at: number, members?: VoiceFunction[]): number;
|
|
34
35
|
content: ASTBraceNode;
|
|
35
36
|
name: string;
|
|
36
37
|
size: number;
|
|
@@ -2,7 +2,7 @@ import { ASTBraceNode, ASTFunctionNode, ASTTextNode } from "../ASTtypes.js";
|
|
|
2
2
|
import { Diagnostic, ErrorDiagnostic, WarningDiagnostic } from "../../diagnostic.js";
|
|
3
3
|
import { isLayoutAttachment } from "../../layout/types.js";
|
|
4
4
|
import { findRightParen, trimRange } from "../../parser/parse-utils/call-utils.js";
|
|
5
|
-
import { findClosingQuote,
|
|
5
|
+
import { findClosingQuote, quoteSourceString } from "../../parser/parse-utils/string-utils.js";
|
|
6
6
|
import { ParserContext, skipSpaces } from "../../parser/parserContext.js";
|
|
7
7
|
import { paintRichLine, prepareRichLine } from "../../render/text.js";
|
|
8
8
|
import { isVisualTemporalNode, TemporalNodeBase } from "../temporal.js";
|
|
@@ -75,11 +75,9 @@ L: la la la
|
|
|
75
75
|
// 等到第二轮寻找该层级的终止符 \n 来确定内容范围
|
|
76
76
|
const node = {
|
|
77
77
|
kind: "sugar",
|
|
78
|
-
data: {
|
|
79
|
-
class: VoiceFunction,
|
|
80
|
-
name,
|
|
81
|
-
},
|
|
78
|
+
data: { name },
|
|
82
79
|
span: { start, end: pos },
|
|
80
|
+
relation: VoiceFunction.deSugarLine,
|
|
83
81
|
};
|
|
84
82
|
return { next: pos, node };
|
|
85
83
|
}
|
|
@@ -105,23 +103,19 @@ L: la la la
|
|
|
105
103
|
}
|
|
106
104
|
const node = {
|
|
107
105
|
kind: "sugar",
|
|
108
|
-
data: {
|
|
109
|
-
class: VoiceFunction,
|
|
110
|
-
lyric,
|
|
111
|
-
name,
|
|
112
|
-
},
|
|
106
|
+
data: { lyric, name },
|
|
113
107
|
span: { start, end: lyric.end },
|
|
114
108
|
syntaxKind: [
|
|
115
109
|
{ kind: "operator", span: { start, end: pos } },
|
|
116
110
|
{ kind: "string", span: lyric },
|
|
117
111
|
],
|
|
112
|
+
relation: VoiceFunction.deSugarLine,
|
|
118
113
|
};
|
|
119
114
|
return { next: lyric.end, node };
|
|
120
115
|
}
|
|
121
|
-
|
|
116
|
+
/** `N:` / `L:` 的关系去糖;V 声明分组时直接调用并传入 members 收集成员 */
|
|
117
|
+
static deSugarLine(ctx, nodes, at, members) {
|
|
122
118
|
const n = nodes[at++];
|
|
123
|
-
if (n.data?.class !== VoiceFunction)
|
|
124
|
-
return null;
|
|
125
119
|
const parseStr = (span) => ctx.parseArgWithType(span, "string") ?? "";
|
|
126
120
|
if (n.data?.lyric !== void 0) {
|
|
127
121
|
// 歌词 需要找到最近的voice节点并添加歌词
|
|
@@ -165,7 +159,7 @@ L: la la la
|
|
|
165
159
|
}
|
|
166
160
|
}
|
|
167
161
|
else if (n.kind === "sugar"
|
|
168
|
-
&& (n.
|
|
162
|
+
&& (n.relation === VoiceFunction.deSugarLine || n.relation === VoicesFunction.deSugarGroup))
|
|
169
163
|
break;
|
|
170
164
|
}
|
|
171
165
|
// 解析后面的内容 得到 VoiceFunction
|
|
@@ -350,7 +344,7 @@ L: la la la
|
|
|
350
344
|
}
|
|
351
345
|
}
|
|
352
346
|
toString(source) {
|
|
353
|
-
const parameters = [`${this.content.toString(source)},${
|
|
347
|
+
const parameters = [`${this.content.toString(source)},${quoteSourceString(this.name)}`];
|
|
354
348
|
for (const lyric of this.lyrics) {
|
|
355
349
|
const text = serializeLyric(lyric.tokens);
|
|
356
350
|
parameters.push(lyric.name ? `${lyric.name}=${text}` : text);
|
|
@@ -3,6 +3,7 @@ import type { LoweringContext } from "../../lowering/loweringContext.js";
|
|
|
3
3
|
import type { MeasureFn, Track } from "../../lowering/track.js";
|
|
4
4
|
import type { GrammarNode } from "../../parser/grammarType.js";
|
|
5
5
|
import { ParserContext } from "../../parser/parserContext.js";
|
|
6
|
+
import { type ConnectionSpec } from "./connections.js";
|
|
6
7
|
import { VoiceFunction } from "./voice.js";
|
|
7
8
|
/**
|
|
8
9
|
* voices 块在宿主基线上局部居中:首末两条 voice 基线的中点对齐宿主轴
|
|
@@ -49,20 +50,19 @@ export declare class VoicesFunction extends ASTFunctionNode {
|
|
|
49
50
|
kind: "sugar";
|
|
50
51
|
data: {
|
|
51
52
|
name?: undefined;
|
|
52
|
-
class: typeof VoicesFunction;
|
|
53
53
|
kind: "line";
|
|
54
54
|
};
|
|
55
55
|
span: {
|
|
56
56
|
start: number;
|
|
57
57
|
end: number;
|
|
58
58
|
};
|
|
59
|
+
relation: typeof VoicesFunction.deSugarGroup;
|
|
59
60
|
};
|
|
60
61
|
} | {
|
|
61
62
|
next: number;
|
|
62
63
|
node: {
|
|
63
64
|
kind: "sugar";
|
|
64
65
|
data: {
|
|
65
|
-
class: typeof VoicesFunction;
|
|
66
66
|
kind: "brace" | "bracket";
|
|
67
67
|
name: {
|
|
68
68
|
start: number;
|
|
@@ -73,9 +73,11 @@ export declare class VoicesFunction extends ASTFunctionNode {
|
|
|
73
73
|
start: number;
|
|
74
74
|
end: number;
|
|
75
75
|
};
|
|
76
|
+
relation: typeof VoicesFunction.deSugarGroup;
|
|
76
77
|
};
|
|
77
78
|
} | null;
|
|
78
|
-
|
|
79
|
+
/** `V|:`、`V{name}:`、`V[name]:` 的关系去糖;向后连续收集 V 声明及其 N/L 行 */
|
|
80
|
+
static deSugarGroup(ctx: ParserContext, nodes: (GrammarNode | number)[], at: number): number;
|
|
79
81
|
voices: VoiceFunction[];
|
|
80
82
|
createdBySugar: boolean;
|
|
81
83
|
size: number;
|
|
@@ -85,7 +87,7 @@ export declare class VoicesFunction extends ASTFunctionNode {
|
|
|
85
87
|
private readonly connect;
|
|
86
88
|
/** 声部名右侧为大括号预留的横向空间 */
|
|
87
89
|
get braceSpace(): number;
|
|
88
|
-
|
|
90
|
+
get connectionSpecs(): ConnectionSpec[];
|
|
89
91
|
get children(): VoiceFunction[];
|
|
90
92
|
timeFlowModel(): {
|
|
91
93
|
children: VoiceFunction[];
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { ASTFunctionNode, ASTTextNode } from "../ASTtypes.js";
|
|
2
2
|
import { ErrorDiagnostic, WarningDiagnostic } from "../../diagnostic.js";
|
|
3
|
-
import { findClosingQuote,
|
|
3
|
+
import { findClosingQuote, quoteSourceString } from "../../parser/parse-utils/string-utils.js";
|
|
4
4
|
import { ParserContext } from "../../parser/parserContext.js";
|
|
5
5
|
import { paintRichLine, prepareRichLine } from "../../render/text.js";
|
|
6
6
|
import { BRACKET_HOOK_COMMANDS, BRACKET_HOOK_DROP, BRACKET_HOOK_REACH, CURVED_BRACE_BOUNDS, CURVED_BRACE_COMMANDS, parseConnections, resolveConnections, serializeConnections, } from "./connections.js";
|
|
@@ -104,8 +104,9 @@ N(女低): 1 2 3
|
|
|
104
104
|
next: start + 3,
|
|
105
105
|
node: {
|
|
106
106
|
kind: "sugar",
|
|
107
|
-
data: {
|
|
107
|
+
data: { kind: "line" },
|
|
108
108
|
span: { start, end: start + 3 },
|
|
109
|
+
relation: VoicesFunction.deSugarGroup,
|
|
109
110
|
},
|
|
110
111
|
};
|
|
111
112
|
}
|
|
@@ -120,18 +121,17 @@ N(女低): 1 2 3
|
|
|
120
121
|
node: {
|
|
121
122
|
kind: "sugar",
|
|
122
123
|
data: {
|
|
123
|
-
class: VoicesFunction,
|
|
124
124
|
kind: open === "{" ? "brace" : "bracket",
|
|
125
125
|
name: { start: start + 2, end: at },
|
|
126
126
|
},
|
|
127
127
|
span: { start, end: at + 2 },
|
|
128
|
+
relation: VoicesFunction.deSugarGroup,
|
|
128
129
|
},
|
|
129
130
|
};
|
|
130
131
|
}
|
|
131
|
-
|
|
132
|
+
/** `V|:`、`V{name}:`、`V[name]:` 的关系去糖;向后连续收集 V 声明及其 N/L 行 */
|
|
133
|
+
static deSugarGroup(ctx, nodes, at) {
|
|
132
134
|
const first = nodes[at];
|
|
133
|
-
if (typeof first === "number" || first.kind !== "sugar" || first.data?.class !== VoicesFunction)
|
|
134
|
-
return null;
|
|
135
135
|
let previousIndex = ctx.nodes.length - 1;
|
|
136
136
|
while (previousIndex >= 0 && ctx.nodes[previousIndex] instanceof ASTTextNode) {
|
|
137
137
|
const { sourceSpan } = ctx.nodes[previousIndex];
|
|
@@ -148,7 +148,7 @@ N(女低): 1 2 3
|
|
|
148
148
|
let cursor = at;
|
|
149
149
|
while (cursor < nodes.length) {
|
|
150
150
|
const marker = nodes[cursor];
|
|
151
|
-
if (typeof marker === "number" || marker.kind !== "sugar" || marker.
|
|
151
|
+
if (typeof marker === "number" || marker.kind !== "sugar" || marker.relation !== VoicesFunction.deSugarGroup)
|
|
152
152
|
break;
|
|
153
153
|
cursor++;
|
|
154
154
|
const carriageReturn = nodes[cursor];
|
|
@@ -162,9 +162,9 @@ N(女低): 1 2 3
|
|
|
162
162
|
const from = members.length + 1;
|
|
163
163
|
while (cursor < nodes.length) {
|
|
164
164
|
const node = nodes[cursor];
|
|
165
|
-
if (typeof node === "number" || node.kind !== "sugar" || node.
|
|
165
|
+
if (typeof node === "number" || node.kind !== "sugar" || node.relation !== VoiceFunction.deSugarLine)
|
|
166
166
|
break;
|
|
167
|
-
cursor = VoiceFunction.
|
|
167
|
+
cursor = VoiceFunction.deSugarLine(section, nodes, cursor, members);
|
|
168
168
|
}
|
|
169
169
|
if (members.length < from) {
|
|
170
170
|
throw new ErrorDiagnostic("E_VOICES_GROUP_EMPTY", "V 声部分组必须至少包含一条 N: 声部声明", marker.span);
|
|
@@ -272,7 +272,7 @@ N(女低): 1 2 3
|
|
|
272
272
|
}
|
|
273
273
|
toString(source) {
|
|
274
274
|
const parameters = this.voices.map(voice => `\t${voice.toString(source)}`);
|
|
275
|
-
parameters.push(`\tconnect=${
|
|
275
|
+
parameters.push(`\tconnect=${quoteSourceString(serializeConnections(this.connectionSpecs))}`, `\tgap=${this.gap}px`, `\tminGap=${this.minGap}px`);
|
|
276
276
|
return `@voices(\n${parameters.join(",\n")}\n)`;
|
|
277
277
|
}
|
|
278
278
|
}
|