jpfun 0.8.0 → 0.9.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/converter/midi/convert.js +8 -55
- package/dist/converter/musicxml/features.d.ts +1 -1
- package/dist/converter/musicxml/features.js +1 -1
- package/dist/converter/musicxml/index.js +8 -18
- package/dist/converter/source.d.ts +4 -7
- package/dist/converter/source.js +11 -9
- package/dist/functions/ASTtypes.d.ts +1 -14
- package/dist/functions/ASTtypes.js +5 -36
- package/dist/functions/adjust/index.js +45 -33
- package/dist/functions/arpeggio/index.js +42 -45
- package/dist/functions/arpeggio/shape.d.ts +2 -3
- package/dist/functions/arpeggio/shape.js +20 -50
- package/dist/functions/bar/index.js +3 -8
- package/dist/functions/beam/auto.js +54 -123
- package/dist/functions/beam/index.js +4 -9
- package/dist/functions/box/index.js +0 -6
- package/dist/functions/dash/index.js +3 -4
- package/dist/functions/div/index.d.ts +2 -4
- package/dist/functions/div/index.js +10 -10
- package/dist/functions/dot/index.js +3 -9
- package/dist/functions/dyn/index.js +6 -26
- package/dist/functions/gliss/index.js +95 -89
- package/dist/functions/grace/index.d.ts +2 -5
- package/dist/functions/grace/index.js +44 -89
- package/dist/functions/head/index.js +9 -6
- package/dist/functions/key/index.js +6 -6
- package/dist/functions/meter/index.js +3 -3
- package/dist/functions/note/index.js +1 -1
- package/dist/functions/note/source-transform.js +2 -5
- package/dist/functions/ottava/index.js +11 -32
- package/dist/functions/page/index.js +13 -16
- package/dist/functions/rest/index.js +2 -4
- package/dist/functions/slide/index.js +0 -1
- package/dist/functions/stack/index.js +8 -57
- package/dist/functions/temporal.d.ts +2 -2
- package/dist/functions/temporal.js +7 -0
- package/dist/functions/text/index.js +24 -30
- package/dist/functions/tie/index.js +14 -37
- package/dist/functions/tremolo/index.js +0 -1
- package/dist/functions/tuplet/index.js +0 -6
- package/dist/functions/up/index.js +79 -83
- package/dist/functions/voice/connections.d.ts +2 -1
- package/dist/functions/voice/connections.js +48 -10
- package/dist/functions/voice/index.d.ts +2 -3
- package/dist/functions/voice/index.js +2 -970
- package/dist/functions/voice/lyrics.d.ts +30 -0
- package/dist/functions/voice/lyrics.js +289 -0
- package/dist/functions/voice/voice.d.ts +63 -0
- package/dist/functions/voice/voice.js +373 -0
- package/dist/functions/voice/voices.d.ts +106 -0
- package/dist/functions/voice/voices.js +423 -0
- package/dist/functions/volta/index.js +5 -23
- package/dist/jpfun.core.min.js +65 -56
- package/dist/jpfun.from-midi.min.js +5 -5
- package/dist/jpfun.from-musicxml.min.js +7 -7
- package/dist/jpfun.min.js +68 -59
- package/dist/layout/engine.js +30 -18
- package/dist/layout/model.d.ts +0 -1
- package/dist/layout/model.js +1 -4
- package/dist/layout/page.d.ts +2 -1
- package/dist/layout/page.js +14 -14
- package/dist/layout/range.d.ts +7 -6
- package/dist/layout/range.js +23 -14
- package/dist/layout/types.d.ts +9 -28
- package/dist/lowering/loweringContext.d.ts +8 -11
- package/dist/lowering/loweringContext.js +7 -13
- package/dist/lowering/track.d.ts +8 -1
- package/dist/lowering/types.d.ts +12 -8
- package/dist/parser/parse-utils/call-utils.d.ts +1 -2
- package/dist/parser/parse-utils/call-utils.js +6 -34
- package/dist/parser/parse-utils/note-utils.d.ts +3 -3
- package/dist/parser/parse-utils/note-utils.js +17 -28
- package/dist/parser/parse-utils/string-utils.d.ts +1 -1
- package/dist/parser/parse-utils/string-utils.js +8 -6
- package/dist/parser/parserContext.d.ts +9 -9
- package/dist/parser/parserContext.js +54 -10
- package/dist/pipeline.js +2 -2
- package/dist/playback/compile.js +13 -14
- package/dist/playback/event.d.ts +6 -6
- package/dist/playback/event.js +13 -49
- package/dist/render/canvas.d.ts +0 -2
- package/dist/render/canvas.js +0 -21
- package/dist/render/svg.d.ts +0 -1
- package/dist/render/svg.js +33 -31
- package/dist/render/text.d.ts +36 -1
- package/dist/render/text.js +134 -0
- package/dist/render/types.d.ts +0 -2
- package/grammar.md +16 -13
- package/package.json +1 -1
- package/dist/iife/core.d.ts +0 -1
- package/dist/iife/core.js +0 -3
- package/dist/iife/from-midi.d.ts +0 -1
- package/dist/iife/from-midi.js +0 -15
- package/dist/iife/from-musicxml.d.ts +0 -1
- package/dist/iife/from-musicxml.js +0 -3
- package/dist/iife/full.d.ts +0 -1
- package/dist/iife/full.js +0 -3
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ASTFunctionNode } from "../ASTtypes.js";
|
|
2
2
|
import { ErrorDiagnostic } from "../../diagnostic.js";
|
|
3
3
|
/**
|
|
4
4
|
* 临时伴奏紧贴主旋律,用比系统级行距更小的间隙
|
|
@@ -65,59 +65,16 @@ class StackFunction extends ASTFunctionNode {
|
|
|
65
65
|
const n = nodes[at++];
|
|
66
66
|
if (!(n.kind === "sugar" && n.data === StackFunction))
|
|
67
67
|
return null;
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
let left = ctx.nodes.length - 1;
|
|
71
|
-
for (; left >= 0; left--) {
|
|
72
|
-
if (ctx.nodes[left] instanceof ASTTextNode)
|
|
73
|
-
continue;
|
|
74
|
-
break;
|
|
75
|
-
}
|
|
76
|
-
let leftNode = left >= 0 ? ctx.nodes[left] : null;
|
|
77
|
-
if (leftNode === null) {
|
|
68
|
+
const leftNode = ctx.takeLeftOperand();
|
|
69
|
+
if (!leftNode) {
|
|
78
70
|
throw new ErrorDiagnostic("E_STACK_NO_TARGET", "@stack语法糖错误: 左边没有找到可叠加的目标", n.span);
|
|
79
71
|
}
|
|
80
|
-
/** 左操作数在 ctx.nodes 中的起点;只有这一段会被 stack 吞并,更早的节点必须保留 */
|
|
81
|
-
let replaceFrom = left;
|
|
82
|
-
// 对 label 的特判: 目标变为label到被标记的节点范围内的所有节点
|
|
83
|
-
if (leftNode instanceof ASTLabelNode) {
|
|
84
|
-
const labelNode = leftNode;
|
|
85
|
-
let tgt = labelNode.target;
|
|
86
|
-
// 在本层这个节点位于哪里(标签节点可能被wrap)
|
|
87
|
-
while (tgt && !ctx.nodes.includes(tgt))
|
|
88
|
-
tgt = tgt.parent;
|
|
89
|
-
for (let j = left - 1; j >= 0; j--) {
|
|
90
|
-
if (ctx.nodes[j] === tgt) {
|
|
91
|
-
leftNode = new ASTBraceNode({
|
|
92
|
-
start: tgt.sourceSpan.start,
|
|
93
|
-
end: labelNode.sourceSpan.end,
|
|
94
|
-
}, ctx.nodes.slice(j, left + 1), null);
|
|
95
|
-
replaceFrom = j;
|
|
96
|
-
break;
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
// 左操作数收敛成一个 stack,后续合并都在它身上进行
|
|
101
72
|
const host = leftNode instanceof StackFunction
|
|
102
73
|
? leftNode
|
|
103
74
|
: new StackFunction(n.span, new Map(), ctx).addContent(leftNode);
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
ctx.makeNodes(nodes, at);
|
|
108
|
-
for (let i = 0; i < ctx.nodes.length; i++) {
|
|
109
|
-
// 后向跳过文本节点 和上面保持一致
|
|
110
|
-
const right = ctx.nodes[i];
|
|
111
|
-
if (right instanceof ASTTextNode)
|
|
112
|
-
continue;
|
|
113
|
-
host.addContent(right);
|
|
114
|
-
storage.length = replaceFrom;
|
|
115
|
-
storage.push(host);
|
|
116
|
-
while (++i < ctx.nodes.length)
|
|
117
|
-
storage.push(ctx.nodes[i]);
|
|
118
|
-
ctx.nodes = storage;
|
|
119
|
-
return nodes.length;
|
|
120
|
-
}
|
|
75
|
+
const next = ctx.consumeRightOperand(nodes, at, right => host.addContent(right));
|
|
76
|
+
if (next !== null)
|
|
77
|
+
return next;
|
|
121
78
|
throw new ErrorDiagnostic("E_STACK_NO_TARGET", "@stack语法糖错误: 右边没有找到可叠加的目标", n.span);
|
|
122
79
|
}
|
|
123
80
|
contents = [];
|
|
@@ -131,15 +88,9 @@ class StackFunction extends ASTFunctionNode {
|
|
|
131
88
|
}
|
|
132
89
|
constructor(span, args, ctx, parent = null) {
|
|
133
90
|
super(span, parent);
|
|
134
|
-
for (const [, value] of args) {
|
|
135
|
-
if (
|
|
91
|
+
for (const [key, value] of args) {
|
|
92
|
+
if (typeof key === "number")
|
|
136
93
|
this.addContent(value);
|
|
137
|
-
continue;
|
|
138
|
-
}
|
|
139
|
-
const c = ctx.parseArgWithType(value.valueSpan, "content", span.start);
|
|
140
|
-
if (c !== null) {
|
|
141
|
-
this.addContent(c);
|
|
142
|
-
}
|
|
143
94
|
}
|
|
144
95
|
}
|
|
145
96
|
/** 遇到另一个 stack 就展平;接收者存活,被吞并的成员列表最后清空 */
|
|
@@ -23,8 +23,6 @@ export interface TimeState {
|
|
|
23
23
|
program: number;
|
|
24
24
|
/** 当前小节时长(QN);尚无显式拍号或散板时为 undefined */
|
|
25
25
|
meter?: Fraction;
|
|
26
|
-
/** 当前执行的全局事件;复合体内部回调共用该位置 */
|
|
27
|
-
currentEvent?: TemporalNodeBase;
|
|
28
26
|
[key: string]: any;
|
|
29
27
|
}
|
|
30
28
|
export declare const DEFAULT_BPM = 120;
|
|
@@ -75,6 +73,8 @@ export declare class TemporalNodeBase implements TimeLineEvent {
|
|
|
75
73
|
* 按全局事件工作的消费者可以沿链上溯到宿主。
|
|
76
74
|
*/
|
|
77
75
|
foldedInto?: TemporalNodeBase;
|
|
76
|
+
/** 沿 foldedInto 上溯到最外层宿主(未折叠时返回自身) */
|
|
77
|
+
get rootHost(): TemporalNodeBase;
|
|
78
78
|
/** 由 LoweringContext 在列归并后固化的谱面行号 */
|
|
79
79
|
layoutLine: number;
|
|
80
80
|
/** 不可见状态事件保持 undefined */
|
|
@@ -41,6 +41,13 @@ export class TemporalNodeBase {
|
|
|
41
41
|
* 按全局事件工作的消费者可以沿链上溯到宿主。
|
|
42
42
|
*/
|
|
43
43
|
foldedInto;
|
|
44
|
+
/** 沿 foldedInto 上溯到最外层宿主(未折叠时返回自身) */
|
|
45
|
+
get rootHost() {
|
|
46
|
+
let host = this;
|
|
47
|
+
while (host.foldedInto)
|
|
48
|
+
host = host.foldedInto;
|
|
49
|
+
return host;
|
|
50
|
+
}
|
|
44
51
|
/** 由 LoweringContext 在列归并后固化的谱面行号 */
|
|
45
52
|
layoutLine = 0;
|
|
46
53
|
/** 不可见状态事件保持 undefined */
|
|
@@ -2,6 +2,7 @@ import { ASTFunctionNode } from "../ASTtypes.js";
|
|
|
2
2
|
import { ErrorDiagnostic } from "../../diagnostic.js";
|
|
3
3
|
import { findClosingQuote, quote } from "../../parser/parse-utils/string-utils.js";
|
|
4
4
|
import { DEFAULT_KEY, TemporalNodeBase } from "../temporal.js";
|
|
5
|
+
import { paintRichLine, parseRichText, prepareRichLine } from "../../render/text.js";
|
|
5
6
|
class TextFunction extends ASTFunctionNode {
|
|
6
7
|
static def = {
|
|
7
8
|
name: ["text"],
|
|
@@ -12,7 +13,7 @@ class TextFunction extends ASTFunctionNode {
|
|
|
12
13
|
~~~
|
|
13
14
|
**简写**:直接写双引号文本,如 \`"进入主题"\`;可跨行,双引号须配对。
|
|
14
15
|
|
|
15
|
-
支持
|
|
16
|
+
支持 \`*斜体*\`、\`**粗体**\`、\`_下划线_\`、\`^上标^\`(再次出现即关闭,不关闭则一直生效;写 \`\\*\`、\`\\_\`、\`\\^\` 显示原字符)。支持 \`\\uE050\` 形式的 Unicode 转义;反斜杠写 \`\\\\\`。`,
|
|
16
17
|
allowExtraArgs: false,
|
|
17
18
|
args: [
|
|
18
19
|
{
|
|
@@ -72,9 +73,9 @@ class TextFunction extends ASTFunctionNode {
|
|
|
72
73
|
ctx.pushNode(new TextFunction(node.span, new Map([[0, ctx.parseArgWithType(node.span, "string")]]), ctx, null));
|
|
73
74
|
return at + 1;
|
|
74
75
|
}
|
|
75
|
-
|
|
76
|
+
text;
|
|
76
77
|
size;
|
|
77
|
-
|
|
78
|
+
lineHeight;
|
|
78
79
|
align;
|
|
79
80
|
font;
|
|
80
81
|
constructor(sourceSpan, args, ctx, parent = null) {
|
|
@@ -84,58 +85,51 @@ class TextFunction extends ASTFunctionNode {
|
|
|
84
85
|
if (align !== "left" && align !== "center" && align !== "right") {
|
|
85
86
|
throw new ErrorDiagnostic("E_TEXT_INVALID_ALIGN", "@text 的 align 参数必须是 'left'、'center' 或 'right'", sourceSpan);
|
|
86
87
|
}
|
|
87
|
-
this.
|
|
88
|
+
this.text = text.replace(/\r?\n/g, "\n");
|
|
88
89
|
this.size = ctx.length2px(size);
|
|
89
|
-
this.
|
|
90
|
+
this.lineHeight = lineHeight;
|
|
90
91
|
this.align = align;
|
|
91
92
|
}
|
|
92
93
|
loweringEnter() { return [new TextTemporalNode(this)]; }
|
|
93
94
|
toString() {
|
|
94
|
-
|
|
95
|
-
return `@text(${quote(this.lines.join("\n"))}, size=${this.size}px, lineheight=${lineHeight}, align=${this.align}, font=${quote(this.font)})`;
|
|
95
|
+
return `@text(${quote(this.text)}, size=${this.size}px, lineheight=${this.lineHeight}, align=${this.align}, font=${quote(this.font)})`;
|
|
96
96
|
}
|
|
97
97
|
}
|
|
98
98
|
export const TextNode = TextFunction;
|
|
99
99
|
class TextTemporalNode extends TemporalNodeBase {
|
|
100
|
-
|
|
101
|
-
textBaselineY = 0;
|
|
100
|
+
preparedLines = [];
|
|
102
101
|
constructor(ast) {
|
|
103
102
|
super();
|
|
104
103
|
this.ast = ast;
|
|
105
104
|
this.mergeKey = DEFAULT_KEY;
|
|
106
|
-
this.style = { fontSize: ast.size, fontFamily: ast.font, fill: "#000", textAlign: ast.align };
|
|
107
105
|
this.initLayoutBox();
|
|
108
106
|
}
|
|
109
107
|
prepareLayout(context) {
|
|
110
|
-
const {
|
|
111
|
-
const
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
width = Math.max(width, measure(lines[i]).w);
|
|
108
|
+
const { text, align, size, font, lineHeight } = this.ast;
|
|
109
|
+
const style = { fontSize: size, fontFamily: font, fill: "#000", textAlign: align };
|
|
110
|
+
this.preparedLines = parseRichText(text).map(runs => prepareRichLine(runs, style, context.textMeasurer));
|
|
111
|
+
const first = this.preparedLines[0];
|
|
112
|
+
const width = Math.max(...this.preparedLines.map(l => l.w));
|
|
116
113
|
this.box.w = width;
|
|
117
|
-
this.box.h = (
|
|
114
|
+
this.box.h = (this.preparedLines.length - 1) * lineHeight * size + first.h;
|
|
118
115
|
this.box.visualAxis = this.box.h / 2;
|
|
119
|
-
this.textBaselineY = first.baseline;
|
|
120
116
|
if (align === "center")
|
|
121
117
|
this.box.anchor = width / 2;
|
|
122
|
-
else if (align === "right") {
|
|
123
|
-
const lastChar = [...lines[0]].at(-1);
|
|
124
|
-
this.box.anchor = width - (lastChar ? measure(lastChar).w / 2 : 0);
|
|
125
|
-
}
|
|
126
118
|
else {
|
|
127
|
-
//
|
|
128
|
-
const
|
|
129
|
-
|
|
119
|
+
// 左/右对齐时锚点落在首行首/末字符中心,单字标记才能像音符一样对准宿主
|
|
120
|
+
const edgeIdx = align === "right" ? -1 : 0;
|
|
121
|
+
const edgeRun = first.runs.at(edgeIdx);
|
|
122
|
+
const half = edgeRun ? context.textMeasurer.measureText([...edgeRun.text].at(edgeIdx), edgeRun.style).w / 2 : 0;
|
|
123
|
+
this.box.anchor = align === "right" ? width - half : half;
|
|
130
124
|
}
|
|
131
125
|
}
|
|
132
126
|
paint(painter) {
|
|
133
|
-
const {
|
|
127
|
+
const { align, lineHeight, size } = this.ast;
|
|
134
128
|
const x = this.box.x + (align === "center" ? this.box.w / 2 : align === "right" ? this.box.w : 0);
|
|
135
|
-
let y = this.box.y + this.
|
|
136
|
-
for (const line of
|
|
137
|
-
painter
|
|
138
|
-
y +=
|
|
129
|
+
let y = this.box.y + this.preparedLines[0].baseline;
|
|
130
|
+
for (const line of this.preparedLines) {
|
|
131
|
+
paintRichLine(painter, line, x, y);
|
|
132
|
+
y += lineHeight * size;
|
|
139
133
|
}
|
|
140
134
|
}
|
|
141
135
|
}
|
|
@@ -3,6 +3,7 @@ import { Diagnostic, ErrorDiagnostic } from "../../diagnostic.js";
|
|
|
3
3
|
import { isVisualTemporalNode, } from "../temporal.js";
|
|
4
4
|
import { pathBounds } from "../../layout/path.js";
|
|
5
5
|
import { getLayoutBounds } from "../../layout/engine.js";
|
|
6
|
+
import { lineEntryX, lineRightX } from "../../layout/range.js";
|
|
6
7
|
/** 仅索引被 @tie 引用过的端点,避免为全谱无关音符分配数组与 Map 节点 */
|
|
7
8
|
const tieEndpoints = new WeakSet();
|
|
8
9
|
const notesByOriginNode = new WeakMap();
|
|
@@ -57,19 +58,13 @@ class TieFunction extends ASTFunctionNode {
|
|
|
57
58
|
};
|
|
58
59
|
endPoints = [];
|
|
59
60
|
height;
|
|
60
|
-
fontSize;
|
|
61
61
|
constructor(sourceSpan, args, ctx, parent = null) {
|
|
62
62
|
super(sourceSpan, parent);
|
|
63
63
|
// 命名选项统一解析默认值,位置参数仍全部作为端点
|
|
64
64
|
const [height] = this.getArgValue(args, ctx);
|
|
65
65
|
for (const [key, value] of args) {
|
|
66
|
-
if (key === "
|
|
67
|
-
|
|
68
|
-
const v = value instanceof ASTFunctionNode
|
|
69
|
-
? value
|
|
70
|
-
: ctx.parseArgWithType(value.valueSpan, "label", sourceSpan.start);
|
|
71
|
-
if (v !== null)
|
|
72
|
-
this.endPoints.push(v);
|
|
66
|
+
if (typeof key === "number")
|
|
67
|
+
this.endPoints.push(value);
|
|
73
68
|
}
|
|
74
69
|
// 数目不足,则找最近的;用 unshift 而不是按下标赋值,否则空洞会让 length 虚高、骗过下面的校验
|
|
75
70
|
let k = ctx.labelableNodes.length - 1;
|
|
@@ -83,7 +78,6 @@ class TieFunction extends ASTFunctionNode {
|
|
|
83
78
|
throw new ErrorDiagnostic("E_NOT_ENOUGH_ARGS", "@{fn} 连音线需要至少两个端点", sourceSpan, { fn: "tie" });
|
|
84
79
|
}
|
|
85
80
|
this.height = Math.max(0, ctx.length2px(height));
|
|
86
|
-
this.fontSize = ctx.variables.fontsize;
|
|
87
81
|
}
|
|
88
82
|
/**
|
|
89
83
|
* tie 不推进时间
|
|
@@ -101,7 +95,7 @@ class TieFunction extends ASTFunctionNode {
|
|
|
101
95
|
ctx.diagnostics.push(Diagnostic.warning.UnresolvedEndpoint("tie", this.sourceSpan));
|
|
102
96
|
return [];
|
|
103
97
|
}
|
|
104
|
-
ctx.addAttachment(new TieLayoutAttachment(endPoints, this.height, this.
|
|
98
|
+
ctx.addAttachment(new TieLayoutAttachment(endPoints, this.height, this.sourceSpan));
|
|
105
99
|
return [];
|
|
106
100
|
}
|
|
107
101
|
toString(source) {
|
|
@@ -117,18 +111,17 @@ class TieLayoutAttachment {
|
|
|
117
111
|
sourceSpan;
|
|
118
112
|
endPoints;
|
|
119
113
|
height;
|
|
120
|
-
|
|
114
|
+
size;
|
|
121
115
|
/** 弧带中部厚度;两端收尖到 0。端点字号不随布局变化,可以一次算定 */
|
|
122
116
|
thickness;
|
|
123
|
-
constructor(endPoints, height,
|
|
117
|
+
constructor(endPoints, height, sourceSpan) {
|
|
124
118
|
this.endPoints = endPoints;
|
|
125
119
|
for (const endpoint of endPoints)
|
|
126
120
|
tieEndpoints.add(endpoint);
|
|
127
121
|
this.height = height;
|
|
128
|
-
this.fontSize = fontSize;
|
|
129
122
|
this.sourceSpan = sourceSpan;
|
|
130
|
-
|
|
131
|
-
this.thickness = Math.max(1.2, size * THICKNESS_RATIO);
|
|
123
|
+
this.size = endPoints.reduce((current, endpoint) => Math.max(current, endpoint.ast.size), 0);
|
|
124
|
+
this.thickness = Math.max(1.2, this.size * THICKNESS_RATIO);
|
|
132
125
|
}
|
|
133
126
|
/**
|
|
134
127
|
* 为同轨、同音且时间相接的逻辑音段建立连接,各次反复及各段装饰保持独立
|
|
@@ -213,39 +206,23 @@ class TieLayoutAttachment {
|
|
|
213
206
|
segments.push(this.arcSegment(start, end, startNode.layoutLine, startTop <= endTop ? startNode.track : endNode.track));
|
|
214
207
|
continue;
|
|
215
208
|
}
|
|
216
|
-
const height = this.height > 0 ? this.height : this.
|
|
209
|
+
const height = this.height > 0 ? this.height : this.size * CROSS_LINE_HEIGHT_RATIO;
|
|
217
210
|
// 首行先起弧,再沿水平段延伸到当前谱面行最右侧
|
|
218
|
-
segments.push(this.openingSegment(start,
|
|
211
|
+
segments.push(this.openingSegment(start, lineRightX(context, startNode.layoutLine), height, startNode.layoutLine, startNode.track));
|
|
219
212
|
// 中间行可能没有任何可见对象,仍然需要逐行补一段水平线
|
|
220
213
|
for (let line = startNode.layoutLine + 1; line < endNode.layoutLine; line++) {
|
|
221
|
-
segments.push(this.horizontalSegment(
|
|
214
|
+
segments.push(this.horizontalSegment(lineEntryX(context, line, startNode.track), lineRightX(context, line), this.hostTop(context, line, startNode.track) - height, line, startNode.track));
|
|
222
215
|
}
|
|
223
216
|
// 跳过声部名、大括号等位于端点列之前的系统前缀,再落弧到终点
|
|
224
|
-
segments.push(this.closingSegment(
|
|
217
|
+
segments.push(this.closingSegment(lineEntryX(context, endNode.layoutLine, endNode.track), end, height, endNode.layoutLine, endNode.track));
|
|
225
218
|
}
|
|
226
219
|
return segments;
|
|
227
220
|
}
|
|
228
221
|
resolveHeight(span) {
|
|
229
222
|
if (this.height > 0)
|
|
230
223
|
return this.height;
|
|
231
|
-
const ratio = Math.min(1, span / (this.
|
|
232
|
-
return this.
|
|
233
|
-
}
|
|
234
|
-
lineEntryX(context, lineIndex, track) {
|
|
235
|
-
const line = context.lines[lineIndex];
|
|
236
|
-
const host = line.trackRuns.get(track)?.find(host => !host.T.isZero());
|
|
237
|
-
return host?.box.x ?? context.originX;
|
|
238
|
-
}
|
|
239
|
-
lineRightX(context, lineIndex) {
|
|
240
|
-
const runs = context.lines[lineIndex].trackRuns;
|
|
241
|
-
if (runs.size === 0)
|
|
242
|
-
return context.originX + context.width;
|
|
243
|
-
let right = context.originX;
|
|
244
|
-
for (const run of runs.values()) {
|
|
245
|
-
for (const host of run)
|
|
246
|
-
right = Math.max(right, host.box.x + host.box.w);
|
|
247
|
-
}
|
|
248
|
-
return right;
|
|
224
|
+
const ratio = Math.min(1, span / (this.size * AUTO_SPAN_RATIO));
|
|
225
|
+
return this.size * AUTO_HEIGHT_RATIO * ratio * (2 - ratio);
|
|
249
226
|
}
|
|
250
227
|
hostTop(context, line, track) {
|
|
251
228
|
return context.getVisualAxis(line, track) + (context.getHostExtent(line, track)?.top ?? 0);
|
|
@@ -92,7 +92,6 @@ class TremoloFunction extends ASTFunctionNode {
|
|
|
92
92
|
content;
|
|
93
93
|
n;
|
|
94
94
|
get children() { return [this.content]; }
|
|
95
|
-
timeFlowModel() { return { children: [this.content], mode: "sequence" }; }
|
|
96
95
|
constructor(span, args, ctx, parent = null) {
|
|
97
96
|
super(span, parent);
|
|
98
97
|
const [content, n] = this.getArgValue(args, ctx);
|
|
@@ -35,12 +35,6 @@ class TupletFunction extends ASTFunctionNode {
|
|
|
35
35
|
normal;
|
|
36
36
|
font;
|
|
37
37
|
get children() { return [this.content]; }
|
|
38
|
-
timeFlowModel() {
|
|
39
|
-
return {
|
|
40
|
-
mode: "sequence",
|
|
41
|
-
children: [this.content],
|
|
42
|
-
};
|
|
43
|
-
}
|
|
44
38
|
loweringEnter(ctx) {
|
|
45
39
|
const group = {
|
|
46
40
|
nodes: [],
|
|
@@ -1,8 +1,28 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ASTFunctionNode } from "../ASTtypes.js";
|
|
2
2
|
import { ErrorDiagnostic } from "../../diagnostic.js";
|
|
3
3
|
import { ANCHOR_KEY, DEFAULT_KEY, transferFunctionAddons, isVisualTemporalNode, TemporalNodeBase, } from "../temporal.js";
|
|
4
4
|
import { Fraction } from "../../fraction.js";
|
|
5
5
|
import { getLayoutBounds, layoutLocalSequence } from "../../layout/engine.js";
|
|
6
|
+
const FOLD_ARGS = [{
|
|
7
|
+
name: "gap",
|
|
8
|
+
namedOnly: true,
|
|
9
|
+
type: "length",
|
|
10
|
+
default: { value: 0.12, unit: "em" },
|
|
11
|
+
description: "相邻堆叠成员之间的间距",
|
|
12
|
+
}];
|
|
13
|
+
function formatStack(fn, host, members, gaps, source) {
|
|
14
|
+
let result = host;
|
|
15
|
+
for (let start = 0; start < members.length;) {
|
|
16
|
+
const gap = gaps[start];
|
|
17
|
+
let end = start + 1;
|
|
18
|
+
while (end < members.length && gaps[end] === gap)
|
|
19
|
+
end++;
|
|
20
|
+
const items = members.slice(start, end).map(item => item.toString(source));
|
|
21
|
+
result = `@${fn}(${[result, ...items, `gap=${gap}px`].join(", ")})`;
|
|
22
|
+
start = end;
|
|
23
|
+
}
|
|
24
|
+
return result;
|
|
25
|
+
}
|
|
6
26
|
/**
|
|
7
27
|
* `@up` / `@down` 共用的折叠容器,不导出
|
|
8
28
|
*
|
|
@@ -13,24 +33,27 @@ import { getLayoutBounds, layoutLocalSequence } from "../../layout/engine.js";
|
|
|
13
33
|
class FoldFunction extends ASTFunctionNode {
|
|
14
34
|
contents = [];
|
|
15
35
|
belows = [];
|
|
16
|
-
|
|
36
|
+
aboveGaps = [];
|
|
37
|
+
belowGaps = [];
|
|
38
|
+
size;
|
|
39
|
+
gap;
|
|
17
40
|
/** 静态而不是实例字段:子类的字段初始化器晚于基类构造器,构造期读不到 */
|
|
18
41
|
get side() { return this.constructor.side; }
|
|
19
42
|
get children() {
|
|
20
43
|
return this.belows.length ? [...this.contents, ...this.belows] : this.contents;
|
|
21
44
|
}
|
|
45
|
+
timeFlowModel() { return null; }
|
|
22
46
|
constructor(span, args, ctx, parent = null) {
|
|
23
47
|
super(span, parent);
|
|
24
48
|
this.size = ctx.variables.fontsize;
|
|
25
|
-
|
|
26
|
-
|
|
49
|
+
const [gap] = this.getArgValue(args, ctx);
|
|
50
|
+
this.gap = ctx.length2px(gap);
|
|
51
|
+
if (!Number.isFinite(this.gap) || this.gap < 0) {
|
|
52
|
+
throw new ErrorDiagnostic("E_INVALID_FOLD_GAP", "@{fn} 的 gap 必须是非负有限长度", span, { fn: this.callName });
|
|
53
|
+
}
|
|
54
|
+
for (const [key, value] of args) {
|
|
55
|
+
if (typeof key === "number")
|
|
27
56
|
this.addContent(value, this.side, ctx);
|
|
28
|
-
continue;
|
|
29
|
-
}
|
|
30
|
-
const c = ctx.parseArgWithType(value.valueSpan, "content", span.start);
|
|
31
|
-
if (c !== null) {
|
|
32
|
-
this.addContent(c, this.side, ctx);
|
|
33
|
-
}
|
|
34
57
|
}
|
|
35
58
|
}
|
|
36
59
|
/** 至少有一个成员能承载标签时,折叠体本身才能作为整体被标注 */
|
|
@@ -38,12 +61,20 @@ class FoldFunction extends ASTFunctionNode {
|
|
|
38
61
|
return this.children.some(ASTFunctionNode.findLabelable) ? this : null;
|
|
39
62
|
}
|
|
40
63
|
/** 遇到另一个折叠体就展平到自己身上;返回 this 只是为了链式写法 */
|
|
41
|
-
addContent(node, side, ctx) {
|
|
64
|
+
addContent(node, side, ctx, gap = this.gap) {
|
|
42
65
|
if (this.contents.length === 0)
|
|
43
66
|
side = "above"; // 还没有宿主,来者即宿主
|
|
44
67
|
if (node instanceof FoldFunction)
|
|
45
|
-
return this.flatten(node, side, ctx);
|
|
46
|
-
(side === "above"
|
|
68
|
+
return this.flatten(node, side, ctx, gap);
|
|
69
|
+
if (side === "above") {
|
|
70
|
+
if (this.contents.length > 0)
|
|
71
|
+
this.aboveGaps.push(gap);
|
|
72
|
+
this.contents.push(node);
|
|
73
|
+
}
|
|
74
|
+
else {
|
|
75
|
+
this.belowGaps.push(gap);
|
|
76
|
+
this.belows.push(node);
|
|
77
|
+
}
|
|
47
78
|
node.parent = this;
|
|
48
79
|
this.growSpan(node.sourceSpan);
|
|
49
80
|
return this;
|
|
@@ -70,11 +101,11 @@ class FoldFunction extends ASTFunctionNode {
|
|
|
70
101
|
}
|
|
71
102
|
/** 混写方向时输出等价的嵌套形式,hover 的替换按钮才能生成合法代码 */
|
|
72
103
|
toString(source) {
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
const
|
|
77
|
-
return
|
|
104
|
+
if (this.contents.length <= 1 && this.belows.length === 0) {
|
|
105
|
+
return `@${this.callName}(${[...this.contents.map(c => c.toString(source)), `gap=${this.gap}px`].join(", ")})`;
|
|
106
|
+
}
|
|
107
|
+
const up = formatStack("up", this.contents[0].toString(source), this.contents.slice(1), this.aboveGaps, source);
|
|
108
|
+
return formatStack("down", up, this.belows, this.belowGaps, source);
|
|
78
109
|
}
|
|
79
110
|
/**
|
|
80
111
|
* 把另一个折叠容器并进自己:**接收者存活**,参数被掏空
|
|
@@ -84,29 +115,29 @@ class FoldFunction extends ASTFunctionNode {
|
|
|
84
115
|
*
|
|
85
116
|
* 被掏空的一方必须撤销候选登记:空壳已不在 AST 里,留在表中会让 @tie() 取到孤儿端点。
|
|
86
117
|
*/
|
|
87
|
-
flatten(drop, side, ctx) {
|
|
118
|
+
flatten(drop, side, ctx, gap = this.gap) {
|
|
88
119
|
// 成员顺序跟随源码;比较必须在 growSpan 之前
|
|
89
120
|
const front = drop.sourceSpan.start < this.sourceSpan.start ? drop : this;
|
|
90
121
|
const back = front === drop ? this : drop;
|
|
91
122
|
const [backHost, ...backAbove] = back.contents;
|
|
123
|
+
const hasHost = front.contents.length > 0;
|
|
92
124
|
const demoted = backHost ? [backHost] : [];
|
|
93
|
-
const
|
|
94
|
-
const
|
|
95
|
-
this.contents = contents;
|
|
96
|
-
this.belows = belows;
|
|
125
|
+
const demotedGap = hasHost && backHost ? [gap] : [];
|
|
126
|
+
const above = !hasHost || side === "above";
|
|
127
|
+
this.contents = [...front.contents, ...(above ? demoted : []), ...backAbove];
|
|
128
|
+
this.belows = [...front.belows, ...(!above ? demoted : []), ...back.belows];
|
|
129
|
+
this.aboveGaps = [...front.aboveGaps, ...(above ? demotedGap : []), ...back.aboveGaps];
|
|
130
|
+
this.belowGaps = [...front.belowGaps, ...(!above ? demotedGap : []), ...back.belowGaps];
|
|
97
131
|
for (const c of this.children)
|
|
98
132
|
c.parent = this;
|
|
99
133
|
this.growSpan(drop.sourceSpan);
|
|
100
|
-
drop.contents =
|
|
101
|
-
|
|
102
|
-
const stale = ctx.labelableNodes.indexOf(drop);
|
|
134
|
+
drop.contents.length = drop.belows.length = drop.aboveGaps.length = drop.belowGaps.length = 0;
|
|
135
|
+
const stale = ctx.labelableNodes.lastIndexOf(drop);
|
|
103
136
|
if (stale >= 0)
|
|
104
137
|
ctx.labelableNodes.splice(stale, 1);
|
|
105
138
|
return this;
|
|
106
139
|
}
|
|
107
140
|
/**
|
|
108
|
-
* 左操作数的取法同 stack;合并策略不同:折叠要让被引用的一方存活
|
|
109
|
-
*
|
|
110
141
|
* `this` 是发起调用的那个子类,所以方向和构造都从它取。子类必须写成
|
|
111
142
|
* `UpFunction.desugar(...)` 这种带显式接收者的形式 —— 解析器是把
|
|
112
143
|
* `deSugarRelation` 摘成游离函数引用来调的,`this` 到不了这里。
|
|
@@ -116,61 +147,25 @@ class FoldFunction extends ASTFunctionNode {
|
|
|
116
147
|
if (!(n.kind === "sugar" && n.data === this))
|
|
117
148
|
return null;
|
|
118
149
|
const label = this.side === "above" ? "@up" : "@down";
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
while (left >= 0 && ctx.nodes[left] instanceof ASTTextNode)
|
|
122
|
-
left--;
|
|
123
|
-
if (left < 0) {
|
|
150
|
+
const leftNode = ctx.takeLeftOperand();
|
|
151
|
+
if (!leftNode)
|
|
124
152
|
throw new ErrorDiagnostic("E_FOLD_NO_TARGET", "{fn}语法糖错误: 左边没有找到可叠加的目标", n.span, { fn: label });
|
|
125
|
-
}
|
|
126
|
-
let leftNode = ctx.nodes[left];
|
|
127
|
-
/** 左操作数在 ctx.nodes 中的起点;只有这一段会被折叠体吞并,更早的节点必须保留 */
|
|
128
|
-
let replaceFrom = left;
|
|
129
|
-
// 对 label 的特判: 目标变为label到被标记的节点范围内的所有节点
|
|
130
|
-
if (leftNode instanceof ASTLabelNode) {
|
|
131
|
-
const labelNode = leftNode;
|
|
132
|
-
let tgt = labelNode.target;
|
|
133
|
-
// 在本层这个节点位于哪里(标签节点可能被wrap)
|
|
134
|
-
while (tgt && !ctx.nodes.includes(tgt))
|
|
135
|
-
tgt = tgt.parent;
|
|
136
|
-
for (let j = left - 1; j >= 0; j--) {
|
|
137
|
-
if (ctx.nodes[j] === tgt) {
|
|
138
|
-
leftNode = new ASTBraceNode({
|
|
139
|
-
start: tgt.sourceSpan.start,
|
|
140
|
-
end: labelNode.sourceSpan.end,
|
|
141
|
-
}, ctx.nodes.slice(j, left + 1), null);
|
|
142
|
-
replaceFrom = j;
|
|
143
|
-
break;
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
// 左操作数收敛成一个折叠体,后续合并都在它身上进行
|
|
153
|
+
const wrapper = new this({ ...n.span }, new Map(), ctx);
|
|
148
154
|
const host = leftNode instanceof FoldFunction
|
|
149
155
|
? leftNode
|
|
150
|
-
:
|
|
151
|
-
|
|
152
|
-
const storage = ctx.nodes;
|
|
153
|
-
ctx.nodes = [];
|
|
154
|
-
ctx.makeNodes(nodes, at);
|
|
155
|
-
for (let i = 0; i < ctx.nodes.length; i++) {
|
|
156
|
-
// 后向跳过文本节点 和上面保持一致
|
|
157
|
-
const right = ctx.nodes[i];
|
|
158
|
-
if (right instanceof ASTTextNode)
|
|
159
|
-
continue;
|
|
156
|
+
: wrapper.addContent(leftNode, "above", ctx);
|
|
157
|
+
const next = ctx.consumeRightOperand(nodes, at, right => {
|
|
160
158
|
// 右操作数可能已被 @tie() 等按引用取走(它就在刚才解析的剩余里),
|
|
161
159
|
// 而新建的左包装无人引用,所以合并时让右边存活;接收者就是存活的那一方
|
|
162
160
|
const merged = right instanceof FoldFunction
|
|
163
|
-
? right.flatten(host, this.side, ctx)
|
|
164
|
-
: host.addContent(right, this.side, ctx);
|
|
165
|
-
storage.length = replaceFrom;
|
|
166
|
-
storage.push(merged);
|
|
167
|
-
while (++i < ctx.nodes.length)
|
|
168
|
-
storage.push(ctx.nodes[i]);
|
|
169
|
-
ctx.nodes = storage;
|
|
161
|
+
? right.flatten(host, this.side, ctx, wrapper.gap)
|
|
162
|
+
: host.addContent(right, this.side, ctx, wrapper.gap);
|
|
170
163
|
// 语法糖不走 pushNode,这里补登记;null 同样入表,和显式调用一样成为边界
|
|
171
164
|
ctx.labelableNodes.push(merged.labelable());
|
|
172
|
-
return
|
|
173
|
-
}
|
|
165
|
+
return merged;
|
|
166
|
+
});
|
|
167
|
+
if (next !== null)
|
|
168
|
+
return next;
|
|
174
169
|
throw new ErrorDiagnostic("E_FOLD_NO_TARGET", "{fn}语法糖错误: 右边没有找到可叠加的目标", n.span, { fn: label });
|
|
175
170
|
}
|
|
176
171
|
}
|
|
@@ -181,15 +176,15 @@ class UpFunction extends FoldFunction {
|
|
|
181
176
|
description: "把同一时间位置的可见对象向上堆叠",
|
|
182
177
|
details: `\
|
|
183
178
|
~~~jpfun
|
|
184
|
-
@up(1, 3, 5)
|
|
179
|
+
@up(1, 3, 5, gap=0.12em)
|
|
185
180
|
~~~
|
|
186
181
|
|
|
187
|
-
|
|
182
|
+
第一个内容参数为宿主,其余依次叠在上方;所有成员共享宿主时值。仅命名参数 \`gap\` 指定相邻成员的间距。常用于和弦、速度记号和音符注释。
|
|
188
183
|
|
|
189
184
|
**简写**:\`1 ^ 3 ^ 5\`。复杂内容可用 \`{...}\` 包裹;需要独立节奏的并行分支时,使用 \`&\` / \`@stack\`。`,
|
|
190
185
|
allowExtraArgs: true,
|
|
191
186
|
extraArgType: "content",
|
|
192
|
-
args:
|
|
187
|
+
args: FOLD_ARGS,
|
|
193
188
|
};
|
|
194
189
|
static deSugarAtom(source, start, _end) {
|
|
195
190
|
if (source[start] !== "^")
|
|
@@ -208,14 +203,14 @@ class DownFunction extends FoldFunction {
|
|
|
208
203
|
description: "把同一时间位置的可见对象向下堆叠",
|
|
209
204
|
details: `\
|
|
210
205
|
~~~jpfun
|
|
211
|
-
@down(1, $p)
|
|
206
|
+
@down(1, $p, gap=0.12em)
|
|
212
207
|
~~~
|
|
213
|
-
|
|
208
|
+
第一个内容参数为宿主,其余依次叠在下方并共享其时值,仅命名参数 \`gap\` 指定相邻成员的间距,常用于力度等下方标记。
|
|
214
209
|
|
|
215
210
|
**简写**:\`1 _ $p\`。与 \`^\` 混写时共用同一宿主:\`1^2_3\` 将 \`2\` 放在 \`1\` 上方、\`3\` 放在下方。`,
|
|
216
211
|
allowExtraArgs: true,
|
|
217
212
|
extraArgType: "content",
|
|
218
|
-
args:
|
|
213
|
+
args: FOLD_ARGS,
|
|
219
214
|
};
|
|
220
215
|
static deSugarAtom(source, start, _end) {
|
|
221
216
|
if (source[start] !== "_")
|
|
@@ -329,23 +324,24 @@ class FoldTemporal extends TemporalNodeBase {
|
|
|
329
324
|
this.box.w = layoutLocalSequence([first], context);
|
|
330
325
|
for (let index = 1; index < this.members.length; index++)
|
|
331
326
|
layoutLocalSequence([this.members[index]], context);
|
|
332
|
-
this.springConfig = { ...first.springConfig
|
|
327
|
+
this.springConfig = { ...first.springConfig };
|
|
333
328
|
// 横向完全等于宿主:上下的标记(变速、注释、力度)常常比音符宽得多,
|
|
334
329
|
// 让它们撑宽盒子会把右邻推开一大截;它们画在基线外侧,伸出盒外也不会碰撞
|
|
335
330
|
const anchor = first.box.x + first.box.anchor;
|
|
336
|
-
const gap = this.ast.size * 0.12;
|
|
337
331
|
this.verticalOffsets.length = this.members.length;
|
|
338
332
|
this.verticalOffsets[0] = 0;
|
|
339
333
|
// 以宿主盒顶为 0,向上得到负坐标、向下得到正坐标,最后整体下移
|
|
340
334
|
const firstBounds = getLayoutBounds(first);
|
|
341
335
|
let top = firstBounds.y;
|
|
342
336
|
for (let i = 1; i < this.aboveCount; i++) {
|
|
337
|
+
const gap = this.ast.aboveGaps[i - 1];
|
|
343
338
|
const bounds = getLayoutBounds(this.members[i]);
|
|
344
339
|
this.verticalOffsets[i] = top - gap - bounds.y - bounds.h;
|
|
345
340
|
top -= gap + bounds.h;
|
|
346
341
|
}
|
|
347
342
|
let bottom = firstBounds.y + firstBounds.h;
|
|
348
343
|
for (let i = this.aboveCount; i < this.members.length; i++) {
|
|
344
|
+
const gap = this.ast.belowGaps[i - this.aboveCount];
|
|
349
345
|
const bounds = getLayoutBounds(this.members[i]);
|
|
350
346
|
this.verticalOffsets[i] = bottom + gap - bounds.y;
|
|
351
347
|
bottom += gap + bounds.h;
|