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,30 @@
|
|
|
1
|
+
import type { AttachmentLayoutContext, HorizontalLineView, LayoutAttachment, LayoutPrepareContext, LayoutRegion } from "../../layout/types.js";
|
|
2
|
+
import { type PreparedRichLine, type RichTextRun } from "../../render/text.js";
|
|
3
|
+
import type { Painter, TextStyle } from "../../render/types.js";
|
|
4
|
+
import type { VisualTemporalNode } from "../temporal.js";
|
|
5
|
+
import type { VoiceNameTemporal } from "./voice.js";
|
|
6
|
+
/** 歌词与歌词行名称相对声部字号的字号 */
|
|
7
|
+
export declare const LYRIC_SIZE_RATIO = 0.82;
|
|
8
|
+
/** 歌词行名称与歌词正文共用同一个基础样式 */
|
|
9
|
+
export declare function lyricNameStyle(size: number, fontFamily: string): TextStyle;
|
|
10
|
+
/**
|
|
11
|
+
* 把歌词拆成与音符一一对应的富文本槽位,同时支持跨槽或槽内的 Markdown 样式(`**`、`*`、`_`、`^`)。
|
|
12
|
+
* 例如 `hello world` 拆成 `hello`、`world`,`hel-lo` 拆成 `hel-`、`lo`;
|
|
13
|
+
* `你好吗` 默认逐字拆分,`你 @ 好` 中的 `@` 留出一个空槽,`{你好} 啊` 则把 `你好` 放在同一个槽位。
|
|
14
|
+
*/
|
|
15
|
+
export declare function parseLyric(value: string): RichTextRun[][];
|
|
16
|
+
export declare function serializeLyric(tokens: readonly (readonly RichTextRun[])[]): string;
|
|
17
|
+
export declare class VoiceLyricsAttachment implements LayoutAttachment {
|
|
18
|
+
private readonly nodes;
|
|
19
|
+
private readonly nameHost;
|
|
20
|
+
private readonly frames;
|
|
21
|
+
layer: "foreground";
|
|
22
|
+
get sourceSpan(): import("../ASTtypes.js").SourceSpan;
|
|
23
|
+
constructor(nodes: readonly VisualTemporalNode[], nameHost: VoiceNameTemporal, frames: readonly LayoutAttachment[]);
|
|
24
|
+
prepareHorizontal(lines: HorizontalLineView[], context: LayoutPrepareContext): void;
|
|
25
|
+
createGeometry(context: AttachmentLayoutContext): {
|
|
26
|
+
regions: (LayoutRegion & PreparedRichLine)[];
|
|
27
|
+
paint(painter: Painter): void;
|
|
28
|
+
};
|
|
29
|
+
private prepareGeometry;
|
|
30
|
+
}
|
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
import { getLayoutBounds } from "../../layout/engine.js";
|
|
2
|
+
import { quote } from "../../parser/parse-utils/string-utils.js";
|
|
3
|
+
import { appendRun, markdownToggles, paintRichLine, prepareRichLine, scanMarkdown, } from "../../render/text.js";
|
|
4
|
+
const WHITESPACE_RE = /\s/;
|
|
5
|
+
/** 歌词与歌词行名称相对声部字号的字号 */
|
|
6
|
+
export const LYRIC_SIZE_RATIO = 0.82;
|
|
7
|
+
// 分词跟后面的标点
|
|
8
|
+
const LYRIC_OPENING_PUNCTUATION = new Set("(([[{〈《「『【〔〖〘〚‘“");
|
|
9
|
+
// 不发声的标点;位于歌词槽首尾时只渲染,不占位,也不参与对齐中心计算
|
|
10
|
+
const LYRIC_SILENT_PUNCTUATION = new Set([
|
|
11
|
+
...LYRIC_OPENING_PUNCTUATION,
|
|
12
|
+
...",。!?;:、,.!?;:))]]}〉》」』】〕〗〙〛’”\"'…—–",
|
|
13
|
+
]);
|
|
14
|
+
/** 歌词行名称与歌词正文共用同一个基础样式 */
|
|
15
|
+
export function lyricNameStyle(size, fontFamily) {
|
|
16
|
+
return {
|
|
17
|
+
fontFamily,
|
|
18
|
+
fontSize: size * LYRIC_SIZE_RATIO,
|
|
19
|
+
fill: "#000",
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
function measureLyricToken(runs, style, measurer) {
|
|
23
|
+
const richLine = prepareRichLine(runs, style, measurer);
|
|
24
|
+
const plain = richLine.runs.map(r => r.text).join("");
|
|
25
|
+
let start = 0;
|
|
26
|
+
let end = plain.length;
|
|
27
|
+
while (start < end && LYRIC_SILENT_PUNCTUATION.has(plain[start]))
|
|
28
|
+
start++;
|
|
29
|
+
while (end > start && LYRIC_SILENT_PUNCTUATION.has(plain[end - 1]))
|
|
30
|
+
end--;
|
|
31
|
+
const rangeWidth = (from, to) => {
|
|
32
|
+
let w = 0, pos = 0;
|
|
33
|
+
for (const run of richLine.runs) {
|
|
34
|
+
const runEnd = pos + run.text.length;
|
|
35
|
+
const a = Math.max(from, pos), b = Math.min(to, runEnd);
|
|
36
|
+
if (a < b) {
|
|
37
|
+
w += a === pos && b === runEnd
|
|
38
|
+
? run.w
|
|
39
|
+
: measurer.measureText(run.text.slice(a - pos, b - pos), run.style).w;
|
|
40
|
+
}
|
|
41
|
+
pos = runEnd;
|
|
42
|
+
}
|
|
43
|
+
return w;
|
|
44
|
+
};
|
|
45
|
+
return { richLine, prefixWidth: rangeWidth(0, start), bodyWidth: rangeWidth(start, end) };
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* 把歌词拆成与音符一一对应的富文本槽位,同时支持跨槽或槽内的 Markdown 样式(`**`、`*`、`_`、`^`)。
|
|
49
|
+
* 例如 `hello world` 拆成 `hello`、`world`,`hel-lo` 拆成 `hel-`、`lo`;
|
|
50
|
+
* `你好吗` 默认逐字拆分,`你 @ 好` 中的 `@` 留出一个空槽,`{你好} 啊` 则把 `你好` 放在同一个槽位。
|
|
51
|
+
*/
|
|
52
|
+
export function parseLyric(value) {
|
|
53
|
+
let closeBraces = 0;
|
|
54
|
+
for (let i = 0; i < value.length; i++) {
|
|
55
|
+
if (value[i] === "\\")
|
|
56
|
+
i++;
|
|
57
|
+
else if (value[i] === "}")
|
|
58
|
+
closeBraces++;
|
|
59
|
+
}
|
|
60
|
+
const result = [];
|
|
61
|
+
let pending = [];
|
|
62
|
+
let hasBody = false;
|
|
63
|
+
let grouped = false;
|
|
64
|
+
const add = (text, mask) => {
|
|
65
|
+
appendRun(pending, text, mask);
|
|
66
|
+
hasBody = true;
|
|
67
|
+
};
|
|
68
|
+
const flush = (force = false) => {
|
|
69
|
+
if (!force && !hasBody)
|
|
70
|
+
return;
|
|
71
|
+
result.push(pending);
|
|
72
|
+
pending = [];
|
|
73
|
+
hasBody = false;
|
|
74
|
+
};
|
|
75
|
+
scanMarkdown(value, (ch, mask, escaped) => {
|
|
76
|
+
if (!escaped && ch === "}")
|
|
77
|
+
closeBraces--;
|
|
78
|
+
if (grouped) {
|
|
79
|
+
if (!escaped && ch === "}") {
|
|
80
|
+
flush(true);
|
|
81
|
+
grouped = false;
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
appendRun(pending, ch, mask);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
else if (!escaped && ch === "{" && closeBraces > 0) {
|
|
88
|
+
flush();
|
|
89
|
+
grouped = true;
|
|
90
|
+
}
|
|
91
|
+
else if (escaped) {
|
|
92
|
+
add(ch, mask);
|
|
93
|
+
}
|
|
94
|
+
else if (WHITESPACE_RE.test(ch)) {
|
|
95
|
+
flush();
|
|
96
|
+
}
|
|
97
|
+
else if (ch === "-") {
|
|
98
|
+
const shouldFlush = hasBody;
|
|
99
|
+
add(ch, mask);
|
|
100
|
+
if (shouldFlush)
|
|
101
|
+
flush();
|
|
102
|
+
}
|
|
103
|
+
else if (ch === "@") {
|
|
104
|
+
if (hasBody) {
|
|
105
|
+
add(ch, mask);
|
|
106
|
+
flush();
|
|
107
|
+
}
|
|
108
|
+
result.push([]);
|
|
109
|
+
}
|
|
110
|
+
else if (LYRIC_OPENING_PUNCTUATION.has(ch)) {
|
|
111
|
+
flush();
|
|
112
|
+
appendRun(pending, ch, mask);
|
|
113
|
+
}
|
|
114
|
+
else if (LYRIC_SILENT_PUNCTUATION.has(ch)) {
|
|
115
|
+
appendRun(pending.length > 0 || !result.at(-1)?.length ? pending : result.at(-1), ch, mask);
|
|
116
|
+
}
|
|
117
|
+
else if (ch.charCodeAt(0) > 0x7F) {
|
|
118
|
+
flush();
|
|
119
|
+
add(ch, mask);
|
|
120
|
+
flush();
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
add(ch, mask);
|
|
124
|
+
}
|
|
125
|
+
}, "\\*_{}@-^");
|
|
126
|
+
flush();
|
|
127
|
+
if (pending.length > 0) {
|
|
128
|
+
if (result.at(-1)?.length) {
|
|
129
|
+
for (const run of pending)
|
|
130
|
+
appendRun(result.at(-1), run.text, run.mask);
|
|
131
|
+
}
|
|
132
|
+
else {
|
|
133
|
+
result.push(pending);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
return result;
|
|
137
|
+
}
|
|
138
|
+
export function serializeLyric(tokens) {
|
|
139
|
+
return quote(tokens.map(runs => {
|
|
140
|
+
if (runs.length === 0)
|
|
141
|
+
return "@";
|
|
142
|
+
let mask = 0;
|
|
143
|
+
let inner = "";
|
|
144
|
+
for (const run of runs) {
|
|
145
|
+
inner += markdownToggles(mask ^ run.mask) + run.text.replace(/[\\{}@\-*_^]/g, "\\$&");
|
|
146
|
+
mask = run.mask;
|
|
147
|
+
}
|
|
148
|
+
return `{${inner}${markdownToggles(mask)}}`;
|
|
149
|
+
}).join(" "));
|
|
150
|
+
}
|
|
151
|
+
export class VoiceLyricsAttachment {
|
|
152
|
+
nodes;
|
|
153
|
+
nameHost;
|
|
154
|
+
frames;
|
|
155
|
+
layer = "foreground";
|
|
156
|
+
get sourceSpan() { return this.nameHost.ast.sourceSpan; }
|
|
157
|
+
constructor(nodes, nameHost, frames) {
|
|
158
|
+
this.nodes = nodes;
|
|
159
|
+
this.nameHost = nameHost;
|
|
160
|
+
this.frames = frames;
|
|
161
|
+
}
|
|
162
|
+
prepareHorizontal(lines, context) {
|
|
163
|
+
const targets = this.nodes.filter(node => node.ports?.["lyric"]);
|
|
164
|
+
if (targets.length === 0)
|
|
165
|
+
return;
|
|
166
|
+
const { lyrics, size, font } = this.nameHost.ast;
|
|
167
|
+
const style = lyricNameStyle(size, font);
|
|
168
|
+
for (let i = 0; i < targets.length; i++) {
|
|
169
|
+
const target = targets[i];
|
|
170
|
+
let halfWidth = 0;
|
|
171
|
+
for (const lyric of lyrics) {
|
|
172
|
+
const runs = lyric.tokens[i];
|
|
173
|
+
if (runs?.length) {
|
|
174
|
+
halfWidth = Math.max(halfWidth, measureLyricToken(runs, style, context.textMeasurer).bodyWidth / 2);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
if (halfWidth === 0)
|
|
178
|
+
continue;
|
|
179
|
+
// 只扩大求解占用;视觉盒和端口不动,音符内部几何因此保持原位。
|
|
180
|
+
lines[target.layoutLine]?.registerHorizontalLayoutHook(target, target, ({ columns, start }) => {
|
|
181
|
+
const element = columns[start].find(item => item.time === target);
|
|
182
|
+
if (!element)
|
|
183
|
+
return;
|
|
184
|
+
const lyricOffset = target.ports["lyric"].x - target.box.anchor;
|
|
185
|
+
element.WL = Math.max(element.WL, halfWidth - lyricOffset);
|
|
186
|
+
element.WR = Math.max(element.WR, halfWidth + lyricOffset);
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
createGeometry(context) {
|
|
191
|
+
const preparedText = this.prepareGeometry(context);
|
|
192
|
+
// 同一行同一轨的多行歌词由引擎合并成一段占用
|
|
193
|
+
return {
|
|
194
|
+
regions: preparedText,
|
|
195
|
+
paint(painter) {
|
|
196
|
+
for (const item of preparedText) {
|
|
197
|
+
paintRichLine(painter, item, item.x, item.y + item.baseline);
|
|
198
|
+
}
|
|
199
|
+
},
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
prepareGeometry(context) {
|
|
203
|
+
const { lyrics, size, font } = this.nameHost.ast;
|
|
204
|
+
const preparedText = [];
|
|
205
|
+
const targets = this.nodes.filter(node => node.ports?.["lyric"]);
|
|
206
|
+
if (targets.length === 0)
|
|
207
|
+
return preparedText;
|
|
208
|
+
// 同一行、原始轨共用下沿,跨轨框外的占用可归属另一条轨
|
|
209
|
+
const baselines = new Map();
|
|
210
|
+
for (const target of targets) {
|
|
211
|
+
let byTrack = baselines.get(target.layoutLine);
|
|
212
|
+
if (!byTrack)
|
|
213
|
+
baselines.set(target.layoutLine, byTrack = new Map());
|
|
214
|
+
const baseline = byTrack.get(target.track) ?? { bottom: -Infinity, owner: target.track };
|
|
215
|
+
const bounds = getLayoutBounds(target);
|
|
216
|
+
let bottom = bounds.y + bounds.h;
|
|
217
|
+
let owner = target.track;
|
|
218
|
+
for (const frame of this.frames) {
|
|
219
|
+
const box = context.getAttachmentBox(frame);
|
|
220
|
+
if (box.x + box.w < bounds.x || box.x > bounds.x + bounds.w || box.y + box.h <= bottom)
|
|
221
|
+
continue;
|
|
222
|
+
const regions = context.getAttachmentOccupancy(frame);
|
|
223
|
+
if (!regions)
|
|
224
|
+
throw new Error("Layout attachment dependency has not been measured");
|
|
225
|
+
const tracks = regions.filter(region => region.line === target.layoutLine);
|
|
226
|
+
if (regions.some(region => region.line !== undefined) && tracks.length === 0)
|
|
227
|
+
continue;
|
|
228
|
+
bottom = box.y + box.h;
|
|
229
|
+
for (const region of tracks) {
|
|
230
|
+
if (region.line !== undefined && context.getVisualAxis(region.line, region.track)
|
|
231
|
+
> context.getVisualAxis(target.layoutLine, owner))
|
|
232
|
+
owner = region.track;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
if (bottom >= baseline.bottom)
|
|
236
|
+
baseline.owner = owner;
|
|
237
|
+
baseline.bottom = Math.max(baseline.bottom, bottom);
|
|
238
|
+
byTrack.set(target.track, baseline);
|
|
239
|
+
}
|
|
240
|
+
const style = lyricNameStyle(size, font);
|
|
241
|
+
const rowAdvance = style.fontSize + size * 0.24;
|
|
242
|
+
const baselineOffset = context.textMeasurer.measureText("M", style).baseline + size * 0.32;
|
|
243
|
+
const baselineOf = (bottom, row) => bottom + baselineOffset + row * rowAdvance;
|
|
244
|
+
// 跨轨框外的歌词由最下轨承担占用,各原始轨的歌词行依次排列
|
|
245
|
+
if (this.frames.length)
|
|
246
|
+
for (const [line, byTrack] of baselines) {
|
|
247
|
+
if (byTrack.size < 2)
|
|
248
|
+
continue;
|
|
249
|
+
const rows = new Map();
|
|
250
|
+
for (const [, baseline] of [...byTrack].sort(([left], [right]) => context.getVisualAxis(line, left) - context.getVisualAxis(line, right))) {
|
|
251
|
+
const row = rows.get(baseline.owner) ?? 0;
|
|
252
|
+
baseline.bottom += row * lyrics.length * rowAdvance;
|
|
253
|
+
rows.set(baseline.owner, row + 1);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
// 歌词行名称与声部名共用左侧那一列,因此只出现在声部名所在的那一行
|
|
257
|
+
const { box: labelBox, layoutLine: labelLine, track: labelTrack } = this.nameHost;
|
|
258
|
+
const labelBaseline = baselines.get(labelLine)?.get(labelTrack);
|
|
259
|
+
for (let row = 0; row < lyrics.length; row++) {
|
|
260
|
+
const lyric = lyrics[row];
|
|
261
|
+
for (let i = 0; i < lyric.tokens.length && i < targets.length; i++) {
|
|
262
|
+
const runs = lyric.tokens[i];
|
|
263
|
+
const target = targets[i];
|
|
264
|
+
const baseline = baselines.get(target.layoutLine)?.get(target.track);
|
|
265
|
+
if (!runs.length || !baseline)
|
|
266
|
+
continue;
|
|
267
|
+
const { richLine, prefixWidth, bodyWidth } = measureLyricToken(runs, style, context.textMeasurer);
|
|
268
|
+
preparedText.push({
|
|
269
|
+
...richLine,
|
|
270
|
+
x: target.box.x + target.ports["lyric"].x - bodyWidth / 2 - prefixWidth,
|
|
271
|
+
y: baselineOf(baseline.bottom, row) - richLine.baseline,
|
|
272
|
+
line: target.layoutLine,
|
|
273
|
+
track: baseline.owner,
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
if (!lyric.name || !labelBox || !labelBaseline)
|
|
277
|
+
continue;
|
|
278
|
+
const richLine = prepareRichLine(lyric.name, style, context.textMeasurer);
|
|
279
|
+
preparedText.push({
|
|
280
|
+
...richLine,
|
|
281
|
+
x: labelBox.x + labelBox.anchor - richLine.w,
|
|
282
|
+
y: baselineOf(labelBaseline.bottom, row) - richLine.baseline,
|
|
283
|
+
line: labelLine,
|
|
284
|
+
track: labelBaseline.owner,
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
return preparedText;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { ASTBraceNode, ASTFunctionClass, ASTFunctionNode, ASTNodeBase, FunctionArgs, SourceSpan } from "../ASTtypes.js";
|
|
2
|
+
import { type LayoutBox, type LayoutPrepareContext } from "../../layout/types.js";
|
|
3
|
+
import type { LoweringContext } from "../../lowering/loweringContext.js";
|
|
4
|
+
import { GrammarNode, GrammarSugarNode } from "../../parser/grammarType.js";
|
|
5
|
+
import { ParserContext } from "../../parser/parserContext.js";
|
|
6
|
+
import { type RichTextRun } from "../../render/text.js";
|
|
7
|
+
import type { Painter, TextStyle } from "../../render/types.js";
|
|
8
|
+
import { TemporalNodeBase } from "../temporal.js";
|
|
9
|
+
export declare function voiceNameStyle(size: number, fontFamily: string): TextStyle;
|
|
10
|
+
export declare class VoiceFunction extends ASTFunctionNode {
|
|
11
|
+
static def: {
|
|
12
|
+
name: string[];
|
|
13
|
+
description: string;
|
|
14
|
+
details: string;
|
|
15
|
+
allowExtraArgs: boolean;
|
|
16
|
+
extraArgType: "string";
|
|
17
|
+
args: ({
|
|
18
|
+
name?: undefined;
|
|
19
|
+
type: "content";
|
|
20
|
+
description: string;
|
|
21
|
+
default: null;
|
|
22
|
+
} | {
|
|
23
|
+
name: string;
|
|
24
|
+
description: string;
|
|
25
|
+
type: "string";
|
|
26
|
+
default: string;
|
|
27
|
+
})[];
|
|
28
|
+
};
|
|
29
|
+
static deSugarAtom(source: string, start: number, end: number): {
|
|
30
|
+
next: number;
|
|
31
|
+
node: GrammarSugarNode;
|
|
32
|
+
} | null;
|
|
33
|
+
static deSugarRelation(ctx: ParserContext, nodes: (GrammarNode | number)[], at: number, members?: VoiceFunction[]): number | null;
|
|
34
|
+
content: ASTBraceNode;
|
|
35
|
+
name: string;
|
|
36
|
+
size: number;
|
|
37
|
+
readonly font: string;
|
|
38
|
+
lyrics: {
|
|
39
|
+
name: string;
|
|
40
|
+
tokens: RichTextRun[][];
|
|
41
|
+
}[];
|
|
42
|
+
get children(): ASTBraceNode[];
|
|
43
|
+
/**
|
|
44
|
+
* voice 的音符内容仍按普通 sequence 进入全局时间列
|
|
45
|
+
* 此处按需建立歌词作用域,并在内容前创建声部名对象
|
|
46
|
+
*/
|
|
47
|
+
loweringEnter(ctx: LoweringContext): VoiceNameTemporal[];
|
|
48
|
+
loweringExit(ctx: LoweringContext): never[];
|
|
49
|
+
constructor(span: SourceSpan, args: FunctionArgs, ctx: ParserContext, parent?: ASTNodeBase | null);
|
|
50
|
+
addLyric(name: string | number, lyric: string, span?: SourceSpan | null, ctx?: ParserContext | null): void;
|
|
51
|
+
toString(source: string): string;
|
|
52
|
+
}
|
|
53
|
+
export declare class VoiceNameTemporal extends TemporalNodeBase {
|
|
54
|
+
ast: VoiceFunction;
|
|
55
|
+
box: LayoutBox;
|
|
56
|
+
/** 名称右侧为大括号预留的横向空间,0 表示不需要 */
|
|
57
|
+
private readonly braceSpace;
|
|
58
|
+
private preparedName;
|
|
59
|
+
constructor(ast: VoiceFunction, braceSpace: number);
|
|
60
|
+
prepareLayout(context: LayoutPrepareContext): void;
|
|
61
|
+
paint(painter: Painter): void;
|
|
62
|
+
}
|
|
63
|
+
export declare const VoiceNode: ASTFunctionClass;
|