jpfun 0.7.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/Bravura-LICENSE.txt +93 -0
- package/README.md +49 -146
- package/dist/Bravura-LICENSE.txt +93 -0
- package/dist/converter/midi/convert.d.ts +0 -10
- package/dist/converter/midi/convert.js +19 -56
- package/dist/converter/musicxml/features.d.ts +16 -8
- package/dist/converter/musicxml/features.js +88 -19
- package/dist/converter/musicxml/index.js +88 -50
- package/dist/converter/musicxml/model.d.ts +9 -0
- package/dist/converter/source.d.ts +4 -7
- package/dist/converter/source.js +11 -9
- package/dist/core.d.ts +1 -0
- package/dist/core.js +1 -0
- package/dist/diagnostic.d.ts +7 -1
- package/dist/diagnostic.js +40 -88
- package/dist/fraction.js +3 -2
- package/dist/functions/ASTtypes.d.ts +3 -32
- package/dist/functions/ASTtypes.js +5 -52
- package/dist/functions/adjust/index.js +45 -33
- package/dist/functions/arpeggio/index.js +50 -55
- package/dist/functions/arpeggio/shape.d.ts +2 -3
- package/dist/functions/arpeggio/shape.js +20 -50
- package/dist/functions/bar/index.d.ts +8 -0
- package/dist/functions/bar/index.js +105 -8
- package/dist/functions/beam/auto.d.ts +1 -2
- package/dist/functions/beam/auto.js +80 -135
- package/dist/functions/beam/index.js +6 -12
- package/dist/functions/beam/layout.d.ts +1 -1
- package/dist/functions/beam/layout.js +1 -1
- package/dist/functions/box/index.js +0 -6
- package/dist/functions/br/index.d.ts +34 -2
- package/dist/functions/br/index.js +5 -4
- package/dist/functions/dash/index.js +9 -10
- package/dist/functions/default.js +2 -1
- package/dist/functions/div/index.d.ts +3 -4
- package/dist/functions/div/index.js +18 -14
- package/dist/functions/dot/index.js +5 -11
- package/dist/functions/dyn/index.js +55 -57
- package/dist/functions/gliss/index.js +131 -103
- package/dist/functions/grace/index.d.ts +3 -12
- package/dist/functions/grace/index.js +52 -102
- package/dist/functions/head/index.js +10 -7
- package/dist/functions/key/index.js +13 -15
- package/dist/functions/meter/index.d.ts +56 -1
- package/dist/functions/meter/index.js +15 -10
- package/dist/functions/note/index.js +6 -6
- package/dist/functions/note/noteName.d.ts +16 -0
- package/dist/functions/note/noteName.js +88 -0
- package/dist/functions/note/source-transform.js +8 -10
- package/dist/functions/ottava/index.js +35 -40
- package/dist/functions/page/index.d.ts +1 -0
- package/dist/functions/page/index.js +27 -21
- package/dist/functions/pagebreak/index.js +2 -2
- package/dist/functions/rest/index.js +5 -5
- package/dist/functions/set/index.js +4 -4
- package/dist/functions/slide/index.js +0 -1
- package/dist/functions/stack/index.js +8 -57
- package/dist/functions/symbol/index.d.ts +21 -2
- package/dist/functions/symbol/index.js +69 -24
- package/dist/functions/symbol/ornament.d.ts +1 -2
- package/dist/functions/symbol/shape.d.ts +6 -0
- package/dist/functions/symbol/shape.js +16 -3
- package/dist/functions/symbol/symbols/coda.d.ts +4 -0
- package/dist/functions/symbol/symbols/coda.js +56 -0
- package/dist/functions/symbol/symbols/dynamics.d.ts +1 -1
- package/dist/functions/symbol/symbols/dynamics.js +18 -8
- package/dist/functions/symbol/symbols/harmonic.d.ts +2 -0
- package/dist/functions/symbol/symbols/harmonic.js +11 -0
- package/dist/functions/symbol/symbols/marcato.d.ts +2 -0
- package/dist/functions/symbol/symbols/marcato.js +22 -0
- package/dist/functions/symbol/symbols/staccato.d.ts +2 -0
- package/dist/functions/symbol/symbols/staccato.js +24 -0
- package/dist/functions/symbol/symbols/tenuto.d.ts +2 -0
- package/dist/functions/symbol/symbols/tenuto.js +11 -0
- package/dist/functions/symbol/symbols/tocoda.d.ts +2 -0
- package/dist/functions/symbol/symbols/tocoda.js +16 -0
- package/dist/functions/symbol/symbols/turn.d.ts +3 -0
- package/dist/functions/symbol/symbols/turn.js +50 -0
- package/dist/functions/symbol/symbols/wheel.d.ts +2 -0
- package/dist/functions/symbol/symbols/wheel.js +36 -0
- package/dist/functions/temporal.d.ts +3 -2
- package/dist/functions/temporal.js +8 -0
- package/dist/functions/text/index.js +38 -35
- package/dist/functions/tie/index.js +44 -43
- package/dist/functions/tremolo/index.d.ts +2 -0
- package/dist/functions/tremolo/index.js +318 -0
- package/dist/functions/tuplet/index.js +4 -14
- package/dist/functions/up/index.js +81 -85
- 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 -963
- 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 +76 -59
- package/dist/jpfun.from-midi.min.js +5 -5
- package/dist/jpfun.from-musicxml.min.js +8 -8
- package/dist/jpfun.min.js +81 -64
- package/dist/layout/default.d.ts +0 -1
- package/dist/layout/default.js +0 -1
- 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 +18 -23
- package/dist/lowering/loweringContext.js +33 -46
- package/dist/lowering/track.d.ts +8 -1
- package/dist/lowering/types.d.ts +13 -11
- 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 +21 -33
- package/dist/parser/parse-utils/string-utils.d.ts +3 -2
- package/dist/parser/parse-utils/string-utils.js +20 -5
- package/dist/parser/parserContext.d.ts +9 -9
- package/dist/parser/parserContext.js +62 -11
- package/dist/pipeline.js +2 -3
- package/dist/playback/compile.js +131 -82
- package/dist/playback/event.d.ts +6 -6
- package/dist/playback/event.js +13 -49
- package/dist/playback/time.js +42 -31
- package/dist/playback/types.d.ts +3 -1
- package/dist/render/canvas.js +4 -22
- package/dist/render/recording.js +1 -4
- 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/translate.d.ts +0 -1
- package/dist/render/translate.js +6 -23
- package/dist/render/types.d.ts +0 -2
- package/grammar.md +39 -30
- package/package.json +3 -3
- package/dist/functions/note/noteNameFSM.d.ts +0 -8
- package/dist/functions/note/noteNameFSM.js +0 -181
- 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
|
@@ -0,0 +1,423 @@
|
|
|
1
|
+
import { ASTFunctionNode, ASTTextNode } from "../ASTtypes.js";
|
|
2
|
+
import { ErrorDiagnostic, WarningDiagnostic } from "../../diagnostic.js";
|
|
3
|
+
import { findClosingQuote, quote } from "../../parser/parse-utils/string-utils.js";
|
|
4
|
+
import { ParserContext } from "../../parser/parserContext.js";
|
|
5
|
+
import { paintRichLine, prepareRichLine } from "../../render/text.js";
|
|
6
|
+
import { BRACKET_HOOK_COMMANDS, BRACKET_HOOK_DROP, BRACKET_HOOK_REACH, CURVED_BRACE_BOUNDS, CURVED_BRACE_COMMANDS, parseConnections, resolveConnections, serializeConnections, } from "./connections.js";
|
|
7
|
+
import { VoiceFunction, voiceNameStyle } from "./voice.js";
|
|
8
|
+
const MIN_CONNECTOR_STEM = 2.5;
|
|
9
|
+
const CONNECTOR_LABEL_GAP_RATIO = 0.3;
|
|
10
|
+
/** 分组总名称与组内声部名之间的横向空隙,相对声部块字号 */
|
|
11
|
+
const GROUP_NAME_GAP_RATIO = 0.5;
|
|
12
|
+
function makeVoicesMeasure(size, gap, minGap) {
|
|
13
|
+
const measure = Object.assign((members) => {
|
|
14
|
+
const half = measure.size / 2;
|
|
15
|
+
const floor = Math.min(measure.gap, measure.minGap);
|
|
16
|
+
const extents = members.map(member => member ? { top: Math.min(member.top, -half), bottom: Math.max(member.bottom, half) } : { top: -half, bottom: half });
|
|
17
|
+
const offsets = [];
|
|
18
|
+
let y = 0;
|
|
19
|
+
for (let i = 0; i < extents.length; i++) {
|
|
20
|
+
if (i > 0)
|
|
21
|
+
y += extents[i - 1].bottom + Math.max(measure.gap + half, floor - extents[i].top);
|
|
22
|
+
offsets.push(y);
|
|
23
|
+
}
|
|
24
|
+
const center = (offsets[0] + offsets[offsets.length - 1]) / 2;
|
|
25
|
+
return offsets.map((offset, i) => ({ offset: offset - center, extent: extents[i] }));
|
|
26
|
+
}, { size, gap, minGap });
|
|
27
|
+
return measure;
|
|
28
|
+
}
|
|
29
|
+
function findClosingGroupName(source, start, end, open, close) {
|
|
30
|
+
let depth = 1;
|
|
31
|
+
for (let i = start; i < end; i++) {
|
|
32
|
+
const ch = source[i];
|
|
33
|
+
if (ch === '"') {
|
|
34
|
+
i = findClosingQuote(source, i, end);
|
|
35
|
+
if (i < 0)
|
|
36
|
+
return -1;
|
|
37
|
+
}
|
|
38
|
+
else if (ch === "\r" || ch === "\n")
|
|
39
|
+
return -1;
|
|
40
|
+
else if (ch === open)
|
|
41
|
+
depth++;
|
|
42
|
+
else if (ch === close && --depth === 0)
|
|
43
|
+
return i;
|
|
44
|
+
}
|
|
45
|
+
return -1;
|
|
46
|
+
}
|
|
47
|
+
export class VoicesFunction extends ASTFunctionNode {
|
|
48
|
+
static def = {
|
|
49
|
+
name: ["voices", "vs"],
|
|
50
|
+
description: "多个声部",
|
|
51
|
+
details: `\
|
|
52
|
+
~~~jpfun
|
|
53
|
+
@voices(
|
|
54
|
+
@voice({1 2 3}, 右手, 男=ha ha ha),
|
|
55
|
+
@voice({3 4 5}, 左手, "la la la"),
|
|
56
|
+
connect="{-:钢琴}"
|
|
57
|
+
)
|
|
58
|
+
~~~
|
|
59
|
+
|
|
60
|
+
接收多个 \`@voice\`,按书写顺序自上而下同时演奏;可用 \`@set(voices.connect="...", voices.gap=1em)\` 预设默认参数。
|
|
61
|
+
|
|
62
|
+
**简写**:连续的 \`N:\`(及 \`L:\`)自动合为多声部,空行分段。可用 \`V{组名}:\`(花连谱号)、\`V[组名]:\`(直连谱号)、\`V|:\`(连谱线)指定连谱号,组名可省且支持 \`*斜体*\`、\`**粗体**\` 等文字样式:
|
|
63
|
+
|
|
64
|
+
~~~jpfun
|
|
65
|
+
V{钢琴}:
|
|
66
|
+
N(右手): 1 2 3
|
|
67
|
+
N(左手): 5 6 7
|
|
68
|
+
V[合唱]:
|
|
69
|
+
N(女高): 6 6 5
|
|
70
|
+
L: 啊 啊 啊
|
|
71
|
+
N(女低): 1 2 3
|
|
72
|
+
~~~`,
|
|
73
|
+
allowExtraArgs: true,
|
|
74
|
+
extraArgType: "content",
|
|
75
|
+
args: [
|
|
76
|
+
{
|
|
77
|
+
name: "connect",
|
|
78
|
+
namedOnly: true,
|
|
79
|
+
type: "string",
|
|
80
|
+
default: "[-]",
|
|
81
|
+
description: "连谱号:`[起点-终点]` 为括线,`{起点-终点}` 为大括号,`[3]`/`{3}` 表示单声部;端点可省,后可接 `:名称` 指定分组总名称,空串仅保留公共细线。",
|
|
82
|
+
},
|
|
83
|
+
{
|
|
84
|
+
name: "gap",
|
|
85
|
+
namedOnly: true,
|
|
86
|
+
type: "length",
|
|
87
|
+
default: { value: 0.75, unit: "em" },
|
|
88
|
+
description: "相邻声部主旋律之间的标准间距",
|
|
89
|
+
},
|
|
90
|
+
{
|
|
91
|
+
name: "minGap",
|
|
92
|
+
namedOnly: true,
|
|
93
|
+
type: "length",
|
|
94
|
+
default: { value: 0.35, unit: "em" },
|
|
95
|
+
description: "上方记号借用间距后保留的最小间距",
|
|
96
|
+
},
|
|
97
|
+
],
|
|
98
|
+
};
|
|
99
|
+
static deSugarAtom(source, start, end) {
|
|
100
|
+
if (source[start] !== "V")
|
|
101
|
+
return null;
|
|
102
|
+
if (source.startsWith("V|:", start) && start + 3 <= end) {
|
|
103
|
+
return {
|
|
104
|
+
next: start + 3,
|
|
105
|
+
node: {
|
|
106
|
+
kind: "sugar",
|
|
107
|
+
data: { class: VoicesFunction, kind: "line" },
|
|
108
|
+
span: { start, end: start + 3 },
|
|
109
|
+
},
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
const open = source[start + 1];
|
|
113
|
+
if (open !== "{" && open !== "[")
|
|
114
|
+
return null;
|
|
115
|
+
const at = findClosingGroupName(source, start + 2, end, open, open === "{" ? "}" : "]");
|
|
116
|
+
if (at < 0 || at + 1 >= end || source[at + 1] !== ":")
|
|
117
|
+
return null;
|
|
118
|
+
return {
|
|
119
|
+
next: at + 2,
|
|
120
|
+
node: {
|
|
121
|
+
kind: "sugar",
|
|
122
|
+
data: {
|
|
123
|
+
class: VoicesFunction,
|
|
124
|
+
kind: open === "{" ? "brace" : "bracket",
|
|
125
|
+
name: { start: start + 2, end: at },
|
|
126
|
+
},
|
|
127
|
+
span: { start, end: at + 2 },
|
|
128
|
+
},
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
static deSugarRelation(ctx, nodes, at) {
|
|
132
|
+
const first = nodes[at];
|
|
133
|
+
if (typeof first === "number" || first.kind !== "sugar" || first.data?.class !== VoicesFunction)
|
|
134
|
+
return null;
|
|
135
|
+
let previousIndex = ctx.nodes.length - 1;
|
|
136
|
+
while (previousIndex >= 0 && ctx.nodes[previousIndex] instanceof ASTTextNode) {
|
|
137
|
+
const { sourceSpan } = ctx.nodes[previousIndex];
|
|
138
|
+
const text = ctx.source.slice(sourceSpan.start, sourceSpan.end);
|
|
139
|
+
if (text.includes("\n") || text.trim())
|
|
140
|
+
break;
|
|
141
|
+
previousIndex--;
|
|
142
|
+
}
|
|
143
|
+
const previous = ctx.nodes[previousIndex];
|
|
144
|
+
const members = previous instanceof VoiceFunction ? [previous]
|
|
145
|
+
: previous instanceof VoicesFunction && previous.createdBySugar ? [...previous.voices] : [];
|
|
146
|
+
const mergePrevious = members.length > 0;
|
|
147
|
+
const connections = [];
|
|
148
|
+
let cursor = at;
|
|
149
|
+
while (cursor < nodes.length) {
|
|
150
|
+
const marker = nodes[cursor];
|
|
151
|
+
if (typeof marker === "number" || marker.kind !== "sugar" || marker.data?.class !== VoicesFunction)
|
|
152
|
+
break;
|
|
153
|
+
cursor++;
|
|
154
|
+
const carriageReturn = nodes[cursor];
|
|
155
|
+
if (typeof carriageReturn === "number" && ctx.source[carriageReturn] === "\r")
|
|
156
|
+
cursor++;
|
|
157
|
+
const newline = nodes[cursor];
|
|
158
|
+
if (typeof newline === "number" && ctx.source[newline] === "\n")
|
|
159
|
+
cursor++;
|
|
160
|
+
// 每段单独解析 N/L,防止歌词越过 V 声明附着到上一段
|
|
161
|
+
const section = new ParserContext(ctx);
|
|
162
|
+
const from = members.length + 1;
|
|
163
|
+
while (cursor < nodes.length) {
|
|
164
|
+
const node = nodes[cursor];
|
|
165
|
+
if (typeof node === "number" || node.kind !== "sugar" || node.data?.class !== VoiceFunction)
|
|
166
|
+
break;
|
|
167
|
+
cursor = VoiceFunction.deSugarRelation(section, nodes, cursor, members);
|
|
168
|
+
}
|
|
169
|
+
if (members.length < from) {
|
|
170
|
+
throw new ErrorDiagnostic("E_VOICES_GROUP_EMPTY", "V 声部分组必须至少包含一条 N: 声部声明", marker.span);
|
|
171
|
+
}
|
|
172
|
+
if (marker.data.kind === "brace" || marker.data.kind === "bracket") {
|
|
173
|
+
const name = marker.data.name
|
|
174
|
+
? ctx.parseArgWithType(marker.data.name, "string") ?? ""
|
|
175
|
+
: "";
|
|
176
|
+
connections.push({
|
|
177
|
+
kind: marker.data.kind,
|
|
178
|
+
from,
|
|
179
|
+
to: members.length,
|
|
180
|
+
...(name ? { name } : {}),
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
const args = new Map();
|
|
185
|
+
members.forEach((member, index) => args.set(index, member));
|
|
186
|
+
args.set("connect", serializeConnections(connections));
|
|
187
|
+
const voices = new VoicesFunction({
|
|
188
|
+
start: mergePrevious ? previous.sourceSpan.start : first.span.start,
|
|
189
|
+
end: members.at(-1).sourceSpan.end,
|
|
190
|
+
}, args, ctx);
|
|
191
|
+
voices.createdBySugar = true;
|
|
192
|
+
if (mergePrevious)
|
|
193
|
+
ctx.nodes.length = previousIndex;
|
|
194
|
+
ctx.pushNode(voices);
|
|
195
|
+
return cursor;
|
|
196
|
+
}
|
|
197
|
+
voices;
|
|
198
|
+
createdBySugar = false; // 不同创建方式的不能合并
|
|
199
|
+
size; // parse 期冻结的字号,px
|
|
200
|
+
font; // parse 期冻结的字体
|
|
201
|
+
gap; // 相邻声部主旋律之间的标准间距,px
|
|
202
|
+
minGap; // 顶部外扩借用间距后保留的最小间距,px
|
|
203
|
+
connect;
|
|
204
|
+
/** 声部名右侧为大括号预留的横向空间 */
|
|
205
|
+
get braceSpace() {
|
|
206
|
+
return Math.max(this.size, (MIN_CONNECTOR_STEM * (BRACKET_HOOK_REACH + 0.5) + 0.5) / (1 - CONNECTOR_LABEL_GAP_RATIO));
|
|
207
|
+
}
|
|
208
|
+
// 收集阶段只冻结配置,避免提前校验随后会被 V 声明覆盖的默认值。
|
|
209
|
+
get connectionSpecs() {
|
|
210
|
+
return parseConnections(this.connect, this.sourceSpan);
|
|
211
|
+
}
|
|
212
|
+
get children() { return this.voices; }
|
|
213
|
+
timeFlowModel() {
|
|
214
|
+
return {
|
|
215
|
+
children: this.children,
|
|
216
|
+
mode: "parallel",
|
|
217
|
+
tracks: {
|
|
218
|
+
laneKey: `voices/${this.voices.length}`,
|
|
219
|
+
hostIndex: null, // 宿主不是成员:第一个 voice 也必须拥有独立轨道
|
|
220
|
+
measure: makeVoicesMeasure(this.size, this.gap, this.minGap),
|
|
221
|
+
},
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
/** 退出时按本块各声部的名称事件注册左侧连谱号,并合并同一组音轨的间距配置 */
|
|
225
|
+
loweringExit(ctx, track) {
|
|
226
|
+
const connections = resolveConnections(this.connectionSpecs, this.voices.length, this.sourceSpan);
|
|
227
|
+
if (this.voices.length >= 2) {
|
|
228
|
+
const names = this.voices.map(voice => ctx.getTemporalNodes(voice)[0]);
|
|
229
|
+
ctx.addAttachment(new VoicesBraceAttachment(names, this, connections));
|
|
230
|
+
}
|
|
231
|
+
const laneKey = `voices/${this.voices.length}`;
|
|
232
|
+
const measure = track.groups.find(group => group.laneKey === laneKey)?.measure;
|
|
233
|
+
if (measure) {
|
|
234
|
+
if (this.voices.length >= 2 && (Math.abs(measure.gap - this.gap) > 1e-6 || Math.abs(measure.minGap - this.minGap) > 1e-6)) {
|
|
235
|
+
ctx.diagnostics.push(new WarningDiagnostic("W_VOICES_GAP_CONFLICT", "相同声部数的 @voices 共用音轨但设置了不同的间距,将取较大值", this.sourceSpan));
|
|
236
|
+
}
|
|
237
|
+
measure.size = Math.max(measure.size, this.size);
|
|
238
|
+
measure.gap = Math.max(measure.gap, this.gap);
|
|
239
|
+
measure.minGap = Math.max(measure.minGap, this.minGap);
|
|
240
|
+
}
|
|
241
|
+
return [];
|
|
242
|
+
}
|
|
243
|
+
constructor(span, args, ctx, parent = null) {
|
|
244
|
+
super(span, parent);
|
|
245
|
+
this.voices = [];
|
|
246
|
+
this.size = ctx.variables.fontsize;
|
|
247
|
+
this.font = ctx.variables.font;
|
|
248
|
+
const [connect, gap, minGap] = this.getArgValue(args, ctx);
|
|
249
|
+
this.connect = connect;
|
|
250
|
+
this.gap = ctx.length2px(gap);
|
|
251
|
+
this.minGap = ctx.length2px(minGap);
|
|
252
|
+
if (!Number.isFinite(this.gap) || this.gap < 0 || !Number.isFinite(this.minGap) || this.minGap < 0) {
|
|
253
|
+
throw new ErrorDiagnostic("E_INVALID_VOICES_GAP", "@voices 的 gap 和 minGap 必须是非负有限长度", span);
|
|
254
|
+
}
|
|
255
|
+
for (const [key, value] of args) {
|
|
256
|
+
if (typeof key === "string")
|
|
257
|
+
continue;
|
|
258
|
+
if (!(value instanceof VoiceFunction)) {
|
|
259
|
+
throw new ErrorDiagnostic("E_VOICES_INVALID_CHILD", "@voices 的参数必须是 @voice 函数,但发现了其他类型 {actual}", value.sourceSpan, { actual: String(value?.constructor.name) });
|
|
260
|
+
}
|
|
261
|
+
this.addVoice(value);
|
|
262
|
+
}
|
|
263
|
+
if (this.voices.length === 0) {
|
|
264
|
+
throw new ErrorDiagnostic("E_VOICES_EMPTY", `@voices 必须至少包含一个 @voice 函数`, span);
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
addVoice(v) {
|
|
268
|
+
this.voices.push(v);
|
|
269
|
+
v.parent = this;
|
|
270
|
+
this.sourceSpan.start = Math.min(v.sourceSpan.start, this.sourceSpan.start);
|
|
271
|
+
this.sourceSpan.end = Math.max(v.sourceSpan.end, this.sourceSpan.end);
|
|
272
|
+
}
|
|
273
|
+
toString(source) {
|
|
274
|
+
const parameters = this.voices.map(voice => `\t${voice.toString(source)}`);
|
|
275
|
+
parameters.push(`\tconnect=${quote(serializeConnections(this.connectionSpecs))}`, `\tgap=${this.gap}px`, `\tminGap=${this.minGap}px`);
|
|
276
|
+
return `@voices(\n${parameters.join(",\n")}\n)`;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
/**
|
|
280
|
+
* 画在声部名与音符之间的多声部括线
|
|
281
|
+
*
|
|
282
|
+
* 共享一条细竖线,按连接范围添加粗括线或弯曲大括号
|
|
283
|
+
* 纵向跨度直接取首末声部名事件的视觉轴(无名声部的占位盒高度为 0,其 y 就是轨道视觉轴),
|
|
284
|
+
* 因此不需要反查任何轨道信息
|
|
285
|
+
*/
|
|
286
|
+
class VoicesBraceAttachment {
|
|
287
|
+
names;
|
|
288
|
+
ast;
|
|
289
|
+
connections;
|
|
290
|
+
layer = "background";
|
|
291
|
+
leftWidth = 0;
|
|
292
|
+
measuredLabels = [];
|
|
293
|
+
get sourceSpan() { return this.ast.sourceSpan; }
|
|
294
|
+
constructor(names, ast, connections) {
|
|
295
|
+
this.names = names;
|
|
296
|
+
this.ast = ast;
|
|
297
|
+
this.connections = connections;
|
|
298
|
+
const first = names[0];
|
|
299
|
+
const last = names.at(-1);
|
|
300
|
+
const prepareLayout = last.prepareLayout;
|
|
301
|
+
last.prepareLayout = context => {
|
|
302
|
+
prepareLayout.call(last, context);
|
|
303
|
+
this.prepareLabels(context);
|
|
304
|
+
};
|
|
305
|
+
const prepareHorizontal = first.prepareHorizontal;
|
|
306
|
+
first.prepareHorizontal = line => {
|
|
307
|
+
prepareHorizontal?.call(first, line);
|
|
308
|
+
if (this.leftWidth <= 0)
|
|
309
|
+
return;
|
|
310
|
+
// 只扩大该列在求解器中的左占用;各声部名自身的 box 与 anchor 保持原位
|
|
311
|
+
line.registerHorizontalLayoutHook(first, first, ({ columns, start }) => {
|
|
312
|
+
for (const element of columns[start]) {
|
|
313
|
+
element.WL = Math.max(element.WL, this.leftWidth);
|
|
314
|
+
}
|
|
315
|
+
});
|
|
316
|
+
};
|
|
317
|
+
}
|
|
318
|
+
prepareLabels(context) {
|
|
319
|
+
const style = voiceNameStyle(this.ast.size, this.ast.font);
|
|
320
|
+
const gap = this.ast.size * GROUP_NAME_GAP_RATIO;
|
|
321
|
+
let maxLeftWidth = 0;
|
|
322
|
+
this.measuredLabels = this.connections.map(connection => {
|
|
323
|
+
if (!connection.name)
|
|
324
|
+
return undefined;
|
|
325
|
+
const richLine = prepareRichLine(connection.name, style, context.textMeasurer);
|
|
326
|
+
let innerWidth = 0;
|
|
327
|
+
for (let i = connection.from - 1; i < connection.to; i++) {
|
|
328
|
+
innerWidth = Math.max(innerWidth, this.names[i].box.anchor);
|
|
329
|
+
}
|
|
330
|
+
const leftWidth = (innerWidth > 0 ? innerWidth + gap : 0) + richLine.w;
|
|
331
|
+
maxLeftWidth = Math.max(maxLeftWidth, leftWidth);
|
|
332
|
+
return { richLine, leftWidth };
|
|
333
|
+
});
|
|
334
|
+
this.leftWidth = maxLeftWidth;
|
|
335
|
+
}
|
|
336
|
+
createGeometry() {
|
|
337
|
+
const first = this.names[0].box;
|
|
338
|
+
const em = this.ast.size;
|
|
339
|
+
const axisY = (index) => {
|
|
340
|
+
const { y, visualAxis } = this.names[index].box;
|
|
341
|
+
return y + visualAxis;
|
|
342
|
+
};
|
|
343
|
+
const top = axisY(0) - em * 0.5;
|
|
344
|
+
const bottom = axisY(this.names.length - 1) + em * 0.5;
|
|
345
|
+
if (bottom - top < 1e-6)
|
|
346
|
+
return { regions: [], paint() { } };
|
|
347
|
+
// 各部分尺寸都以粗竖线宽度为单位,比例取自常见简谱软件的括线
|
|
348
|
+
const stem = Math.max(MIN_CONNECTOR_STEM, em * 0.19);
|
|
349
|
+
const reach = stem * BRACKET_HOOK_REACH;
|
|
350
|
+
const drop = stem * BRACKET_HOOK_DROP;
|
|
351
|
+
const anchorX = first.x + first.anchor;
|
|
352
|
+
const x = anchorX + this.ast.braceSpace * CONNECTOR_LABEL_GAP_RATIO + stem / 2;
|
|
353
|
+
const inset = Math.min(stem * 0.67, em * 0.25);
|
|
354
|
+
const line = {
|
|
355
|
+
x: x + stem * 1.33 - stem * 0.165,
|
|
356
|
+
y: top + inset,
|
|
357
|
+
w: stem * 0.33,
|
|
358
|
+
h: bottom - top - inset * 2,
|
|
359
|
+
};
|
|
360
|
+
const bars = [];
|
|
361
|
+
const paths = [];
|
|
362
|
+
const labels = [];
|
|
363
|
+
const regions = [line];
|
|
364
|
+
for (let i = 0; i < this.connections.length; i++) {
|
|
365
|
+
const connection = this.connections[i];
|
|
366
|
+
const groupTop = axisY(connection.from - 1) - em * 0.5;
|
|
367
|
+
const groupBottom = axisY(connection.to - 1) + em * 0.5;
|
|
368
|
+
if (connection.kind === "bracket") {
|
|
369
|
+
bars.push({ x: x - stem / 2, y: groupTop, w: stem, h: groupBottom - groupTop });
|
|
370
|
+
paths.push({ commands: BRACKET_HOOK_COMMANDS, transform: { x, y: groupTop, scaleX: stem, scaleY: -stem } }, { commands: BRACKET_HOOK_COMMANDS, transform: { x, y: groupBottom, scaleX: stem, scaleY: stem } });
|
|
371
|
+
regions.push({
|
|
372
|
+
x: x - stem / 2, y: groupTop - drop,
|
|
373
|
+
w: reach + stem / 2, h: groupBottom - groupTop + drop * 2,
|
|
374
|
+
});
|
|
375
|
+
}
|
|
376
|
+
else {
|
|
377
|
+
const width = em * 0.35;
|
|
378
|
+
const left = line.x - em * 0.12 - width;
|
|
379
|
+
const scaleX = width / CURVED_BRACE_BOUNDS.w;
|
|
380
|
+
const scaleY = (groupBottom - groupTop) / CURVED_BRACE_BOUNDS.h;
|
|
381
|
+
paths.push({
|
|
382
|
+
commands: CURVED_BRACE_COMMANDS,
|
|
383
|
+
transform: {
|
|
384
|
+
x: left - CURVED_BRACE_BOUNDS.x * scaleX,
|
|
385
|
+
y: groupTop - CURVED_BRACE_BOUNDS.y * scaleY,
|
|
386
|
+
scaleX, scaleY,
|
|
387
|
+
},
|
|
388
|
+
});
|
|
389
|
+
regions.push({ x: left, y: groupTop, w: width, h: groupBottom - groupTop });
|
|
390
|
+
}
|
|
391
|
+
const label = this.measuredLabels[i];
|
|
392
|
+
if (label) {
|
|
393
|
+
labels.push({
|
|
394
|
+
...label.richLine,
|
|
395
|
+
x: anchorX - label.leftWidth,
|
|
396
|
+
y: (groupTop + groupBottom - label.richLine.h) / 2,
|
|
397
|
+
});
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
bars.push(line);
|
|
401
|
+
// 在首声部名称宿主上发布连谱号细竖线顶端坐标,供行首小节号直接居中对齐
|
|
402
|
+
this.names[0].ports["bar.number"] = {
|
|
403
|
+
x: x + stem * 1.33 - first.x,
|
|
404
|
+
y: Math.min(top, ...regions.map(region => region.y)) - first.y,
|
|
405
|
+
};
|
|
406
|
+
regions.push(...labels);
|
|
407
|
+
return {
|
|
408
|
+
regions,
|
|
409
|
+
paint(painter) {
|
|
410
|
+
for (const bar of bars) {
|
|
411
|
+
painter.drawRect(bar.x, bar.y, bar.w, bar.h, { fill: "#000" });
|
|
412
|
+
}
|
|
413
|
+
for (const path of paths) {
|
|
414
|
+
painter.drawPath(path.commands, { fill: "#000" }, path.transform);
|
|
415
|
+
}
|
|
416
|
+
for (const label of labels) {
|
|
417
|
+
paintRichLine(painter, label, label.x + label.w, label.y + label.baseline);
|
|
418
|
+
}
|
|
419
|
+
},
|
|
420
|
+
};
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
export const VoicesNode = VoicesFunction;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { lineEntryX, lineRightX } from "../../layout/range.js";
|
|
1
2
|
import { ANCHOR_KEY, isVisualTemporalNode, } from "../temporal.js";
|
|
2
3
|
import { repeatPass } from "../bar/index.js";
|
|
3
4
|
import { ASTFunctionNode, } from "../ASTtypes.js";
|
|
@@ -48,12 +49,8 @@ class VoltaFunction extends ASTFunctionNode {
|
|
|
48
49
|
font;
|
|
49
50
|
/** volta 不推进时间,只把 AST 端点解析成稳定的 temporal 引用 */
|
|
50
51
|
loweringEnter(ctx) {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
while (from?.foldedInto)
|
|
54
|
-
from = from.foldedInto;
|
|
55
|
-
while (to?.foldedInto)
|
|
56
|
-
to = to.foldedInto;
|
|
52
|
+
const from = ctx.getTemporalNodes(this.from).at(0)?.rootHost;
|
|
53
|
+
const to = ctx.getTemporalNodes(this.to).at(-1)?.rootHost;
|
|
57
54
|
if (!from || !to) {
|
|
58
55
|
ctx.diagnostics.push(Diagnostic.warning.UnresolvedEndpoint("volta", this.sourceSpan));
|
|
59
56
|
return [];
|
|
@@ -179,10 +176,10 @@ class VoltaAttachment {
|
|
|
179
176
|
if (hostTop === Infinity)
|
|
180
177
|
hostTop = context.getVisualAxis(line, topTrack);
|
|
181
178
|
// 换行断点跟随系统的视觉边界;空行没有主体时仍退到内容区页边
|
|
182
|
-
const rawLeft = line !== firstLine ?
|
|
179
|
+
const rawLeft = line !== firstLine ? lineEntryX(context, line, topTrack)
|
|
183
180
|
: leftBar?.layoutLine === line ? leftBar.box.x + leftBar.box.anchor + 5 // 5是防止前后房子竖线重合
|
|
184
181
|
: head.box.x + (head.ports["body.left"]?.x ?? 0);
|
|
185
|
-
const rawRight = line !== lastLine ?
|
|
182
|
+
const rawRight = line !== lastLine ? lineRightX(context, line)
|
|
186
183
|
: rightBar?.layoutLine === line ? rightBar.box.x + rightBar.box.anchor
|
|
187
184
|
: tail.box.x + (tail.ports["body.right"]?.x ?? tail.box.w);
|
|
188
185
|
const left = Math.min(rawLeft, rawRight);
|
|
@@ -231,19 +228,4 @@ class VoltaAttachment {
|
|
|
231
228
|
},
|
|
232
229
|
};
|
|
233
230
|
}
|
|
234
|
-
lineEntryX(context, line, track) {
|
|
235
|
-
const host = context.lines[line].trackRuns.get(track)?.find(item => !item.T.isZero());
|
|
236
|
-
return host?.box.x ?? context.originX;
|
|
237
|
-
}
|
|
238
|
-
lineRightX(context, line) {
|
|
239
|
-
const runs = context.lines[line].trackRuns;
|
|
240
|
-
if (runs.size === 0)
|
|
241
|
-
return context.originX + context.width;
|
|
242
|
-
let right = context.originX;
|
|
243
|
-
for (const run of runs.values()) {
|
|
244
|
-
for (const host of run)
|
|
245
|
-
right = Math.max(right, host.box.x + host.box.w);
|
|
246
|
-
}
|
|
247
|
-
return right;
|
|
248
|
-
}
|
|
249
231
|
}
|