jpfun 0.8.0 → 1.0.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 +10 -15
- package/dist/diagnostic.d.ts +29 -2
- package/dist/diagnostic.js +5 -4
- 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 +44 -48
- package/dist/functions/arpeggio/shape.d.ts +2 -3
- package/dist/functions/arpeggio/shape.js +20 -50
- package/dist/functions/bar/index.d.ts +3 -0
- package/dist/functions/bar/index.js +21 -11
- package/dist/functions/beam/auto.js +54 -123
- package/dist/functions/beam/index.js +4 -9
- package/dist/functions/beam/layout.js +3 -2
- package/dist/functions/box/index.js +1 -6
- package/dist/functions/dash/index.js +6 -4
- package/dist/functions/div/index.d.ts +3 -5
- package/dist/functions/div/index.js +14 -12
- package/dist/functions/dot/index.js +3 -9
- package/dist/functions/dyn/index.js +7 -26
- package/dist/functions/gliss/index.js +99 -91
- package/dist/functions/grace/index.d.ts +2 -5
- package/dist/functions/grace/index.js +47 -94
- package/dist/functions/head/index.js +32 -36
- package/dist/functions/key/index.js +9 -12
- package/dist/functions/meter/index.d.ts +1 -0
- package/dist/functions/meter/index.js +6 -3
- package/dist/functions/meter/shape.d.ts +1 -1
- package/dist/functions/note/accidentals.d.ts +3 -3
- package/dist/functions/note/accidentals.js +29 -16
- package/dist/functions/note/index.js +12 -14
- package/dist/functions/note/source-transform.d.ts +15 -0
- package/dist/functions/note/source-transform.js +75 -71
- package/dist/functions/ottava/index.js +12 -32
- package/dist/functions/page/index.js +15 -17
- package/dist/functions/rest/index.js +10 -10
- package/dist/functions/slide/index.js +1 -1
- package/dist/functions/stack/index.js +8 -57
- package/dist/functions/temporal.d.ts +11 -5
- package/dist/functions/temporal.js +22 -2
- package/dist/functions/text/index.js +24 -30
- package/dist/functions/tie/index.js +42 -41
- package/dist/functions/tremolo/index.js +1 -1
- package/dist/functions/tuplet/index.js +1 -6
- package/dist/functions/up/index.js +82 -88
- package/dist/functions/voice/connections.d.ts +3 -2
- 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 +34 -0
- package/dist/functions/voice/lyrics.js +393 -0
- package/dist/functions/voice/voice.d.ts +67 -0
- package/dist/functions/voice/voice.js +405 -0
- package/dist/functions/voice/voices.d.ts +106 -0
- package/dist/functions/voice/voices.js +424 -0
- package/dist/functions/volta/index.js +6 -23
- package/dist/index.d.ts +97 -3
- package/dist/index.js +97 -3
- package/dist/jpfun.min.js +63 -67
- package/dist/layout/engine.d.ts +4 -3
- package/dist/layout/engine.js +190 -24
- package/dist/layout/model.d.ts +12 -4
- package/dist/layout/model.js +16 -11
- package/dist/layout/page.d.ts +2 -1
- package/dist/layout/page.js +15 -15
- package/dist/layout/path.js +1 -1
- package/dist/layout/range.d.ts +7 -6
- package/dist/layout/range.js +23 -14
- package/dist/layout/types.d.ts +19 -30
- 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/grammarType.d.ts +3 -3
- package/dist/parser/parse-utils/brace-utils.js +7 -17
- package/dist/parser/parse-utils/call-utils.d.ts +4 -3
- package/dist/parser/parse-utils/call-utils.js +23 -66
- package/dist/parser/parse-utils/note-utils.d.ts +7 -3
- package/dist/parser/parse-utils/note-utils.js +25 -40
- 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 +17 -11
- package/dist/parser/parserContext.js +111 -65
- package/dist/parser/preprocess.d.ts +7 -0
- package/dist/parser/preprocess.js +21 -19
- package/dist/pipeline.d.ts +1 -1
- package/dist/pipeline.js +2 -2
- package/dist/playback/compile.js +14 -15
- 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 +50 -56
- package/dist/render/index.d.ts +1 -1
- package/dist/render/index.js +1 -1
- package/dist/render/paint.d.ts +7 -0
- package/dist/render/paint.js +70 -20
- package/dist/render/recording.d.ts +8 -1
- package/dist/render/recording.js +12 -0
- package/dist/render/svg.d.ts +5 -2
- package/dist/render/svg.js +70 -48
- package/dist/render/text.d.ts +37 -2
- package/dist/render/text.js +136 -1
- package/dist/render/translate.d.ts +3 -1
- package/dist/render/translate.js +6 -0
- package/dist/render/types.d.ts +9 -2
- package/grammar.md +18 -14
- package/package.json +8 -18
- package/dist/converter/midi/convert.d.ts +0 -42
- package/dist/converter/midi/convert.js +0 -729
- package/dist/converter/midi/index.d.ts +0 -5
- package/dist/converter/midi/index.js +0 -11
- package/dist/converter/midi/json.d.ts +0 -38
- package/dist/converter/midi/json.js +0 -1
- package/dist/converter/midi/triplet.d.ts +0 -30
- package/dist/converter/midi/triplet.js +0 -75
- package/dist/converter/musicxml/dom.d.ts +0 -25
- package/dist/converter/musicxml/dom.js +0 -65
- package/dist/converter/musicxml/features.d.ts +0 -114
- package/dist/converter/musicxml/features.js +0 -501
- package/dist/converter/musicxml/index.d.ts +0 -14
- package/dist/converter/musicxml/index.js +0 -1295
- package/dist/converter/musicxml/model.d.ts +0 -176
- package/dist/converter/musicxml/model.js +0 -1
- package/dist/converter/musicxml/resolve.d.ts +0 -10
- package/dist/converter/musicxml/resolve.js +0 -152
- package/dist/converter/source.d.ts +0 -22
- package/dist/converter/source.js +0 -51
- package/dist/core.d.ts +0 -24
- package/dist/core.js +0 -24
- 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
- package/dist/jpfun.core.min.js +0 -123
- package/dist/jpfun.from-midi.min.js +0 -9
- package/dist/jpfun.from-musicxml.min.js +0 -12
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { AttachmentLayoutContext, HorizontalLineView, LayoutAttachment, LayoutPrepareContext, LayoutRegion } from "../../layout/types.js";
|
|
2
|
+
import type { SourceSpan } from "../../parser/types.js";
|
|
3
|
+
import { type PreparedRichLine, type RichTextRun } from "../../render/text.js";
|
|
4
|
+
import type { Painter, TextStyle } from "../../render/types.js";
|
|
5
|
+
import type { VisualTemporalNode } from "../temporal.js";
|
|
6
|
+
import type { VoiceNameTemporal } from "./voice.js";
|
|
7
|
+
/** 歌词与歌词行名称相对声部字号的字号 */
|
|
8
|
+
export declare const LYRIC_SIZE_RATIO = 0.88;
|
|
9
|
+
/** 歌词行名称与歌词正文共用同一个基础样式 */
|
|
10
|
+
export declare function lyricNameStyle(size: number, fontFamily: string): TextStyle;
|
|
11
|
+
export type LyricSlot = RichTextRun[] | null;
|
|
12
|
+
/**
|
|
13
|
+
* 把歌词拆成与音符一一对应的富文本槽位,同时支持跨槽或槽内的 Markdown 样式(`**`、`*`、`_`、`^`)。
|
|
14
|
+
* 例如 `hello world` 拆成 `hello`、`world`,`hel-lo` 拆成 `hel-`、`lo`;
|
|
15
|
+
* `你好吗` 默认逐字拆分,`你 @ 好` 中的 `@` 留出一个空槽,
|
|
16
|
+
* `{你好} 啊` 把 `你好` 放在同一个槽位,`{a@@}` 则左对齐并覆盖三个槽位(后续槽记为 `null`)。
|
|
17
|
+
*/
|
|
18
|
+
export declare function parseLyric(value: string, offsets?: readonly number[], tokenSpans?: (SourceSpan | undefined)[]): LyricSlot[];
|
|
19
|
+
export declare function serializeLyric(tokens: readonly (readonly RichTextRun[] | null)[]): string;
|
|
20
|
+
export declare class VoiceLyricsAttachment implements LayoutAttachment {
|
|
21
|
+
private readonly nodes;
|
|
22
|
+
private readonly nameHost;
|
|
23
|
+
private readonly frames;
|
|
24
|
+
readonly kind = "lyrics";
|
|
25
|
+
layer: "foreground";
|
|
26
|
+
get sourceSpan(): SourceSpan;
|
|
27
|
+
constructor(nodes: readonly VisualTemporalNode[], nameHost: VoiceNameTemporal, frames: readonly LayoutAttachment[]);
|
|
28
|
+
prepareHorizontal(lines: HorizontalLineView[], context: LayoutPrepareContext): void;
|
|
29
|
+
createGeometry(context: AttachmentLayoutContext): {
|
|
30
|
+
regions: (LayoutRegion & PreparedRichLine)[];
|
|
31
|
+
paint(painter: Painter): void;
|
|
32
|
+
};
|
|
33
|
+
private prepareGeometry;
|
|
34
|
+
}
|
|
@@ -0,0 +1,393 @@
|
|
|
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.88;
|
|
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
|
+
function lyricLeftPortX(target) {
|
|
48
|
+
return target.ports["body.left"]?.x ?? 0;
|
|
49
|
+
}
|
|
50
|
+
/** 将刚性半宽提升至 min,并从同侧弹簧 margin 中等量扣减:未超长时保持 WL/WR+margin 总宽不变,压缩时以刚性下界防重叠 */
|
|
51
|
+
function reserveSide(el, side, min) {
|
|
52
|
+
const w = side === "L" ? "WL" : "WR";
|
|
53
|
+
const m = side === "L" ? "margin_L" : "margin_R";
|
|
54
|
+
if (min > el[w]) {
|
|
55
|
+
el[m] = Math.max(0, el[w] + el[m] - min);
|
|
56
|
+
el[w] = min;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* 把歌词拆成与音符一一对应的富文本槽位,同时支持跨槽或槽内的 Markdown 样式(`**`、`*`、`_`、`^`)。
|
|
61
|
+
* 例如 `hello world` 拆成 `hello`、`world`,`hel-lo` 拆成 `hel-`、`lo`;
|
|
62
|
+
* `你好吗` 默认逐字拆分,`你 @ 好` 中的 `@` 留出一个空槽,
|
|
63
|
+
* `{你好} 啊` 把 `你好` 放在同一个槽位,`{a@@}` 则左对齐并覆盖三个槽位(后续槽记为 `null`)。
|
|
64
|
+
*/
|
|
65
|
+
export function parseLyric(value, offsets, tokenSpans) {
|
|
66
|
+
let closeBraces = 0;
|
|
67
|
+
for (let i = 0; i < value.length; i++) {
|
|
68
|
+
if (value[i] === "\\")
|
|
69
|
+
i++;
|
|
70
|
+
else if (value[i] === "}")
|
|
71
|
+
closeBraces++;
|
|
72
|
+
}
|
|
73
|
+
const result = [];
|
|
74
|
+
let pending = [];
|
|
75
|
+
let lastToken;
|
|
76
|
+
let lastSpan;
|
|
77
|
+
let tokStart = -1, tokEnd = 0, keepRuns = 0, keepLen = 0, span = 0;
|
|
78
|
+
let hasBody = false, grouped = false;
|
|
79
|
+
const mark = (from, to) => { if (tokStart < 0)
|
|
80
|
+
tokStart = from; tokEnd = to; };
|
|
81
|
+
const add = (text, mask, from, to) => {
|
|
82
|
+
appendRun(pending, text, mask);
|
|
83
|
+
mark(from, to);
|
|
84
|
+
hasBody = true;
|
|
85
|
+
};
|
|
86
|
+
const flush = (force = false) => {
|
|
87
|
+
if (!force && !hasBody)
|
|
88
|
+
return;
|
|
89
|
+
if (tokenSpans && offsets && tokStart >= 0) {
|
|
90
|
+
tokenSpans[result.length] = lastSpan = { start: offsets[tokStart], end: offsets[tokEnd] };
|
|
91
|
+
}
|
|
92
|
+
result.push(lastToken = pending);
|
|
93
|
+
pending = [];
|
|
94
|
+
tokStart = -1;
|
|
95
|
+
hasBody = false;
|
|
96
|
+
};
|
|
97
|
+
scanMarkdown(value, (ch, mask, escaped, from, to) => {
|
|
98
|
+
if (!escaped && ch === "}")
|
|
99
|
+
closeBraces--;
|
|
100
|
+
if (grouped) {
|
|
101
|
+
if (!escaped && ch === "}") {
|
|
102
|
+
mark(from, to);
|
|
103
|
+
if (span) {
|
|
104
|
+
pending.length = keepRuns;
|
|
105
|
+
if (keepRuns)
|
|
106
|
+
pending[keepRuns - 1].text = pending[keepRuns - 1].text.slice(0, keepLen);
|
|
107
|
+
}
|
|
108
|
+
if (!keepRuns && span) {
|
|
109
|
+
while (span--)
|
|
110
|
+
result.push([]);
|
|
111
|
+
tokStart = -1;
|
|
112
|
+
}
|
|
113
|
+
else {
|
|
114
|
+
flush(true);
|
|
115
|
+
while (span--)
|
|
116
|
+
result.push(null);
|
|
117
|
+
}
|
|
118
|
+
keepRuns = keepLen = span = 0;
|
|
119
|
+
grouped = false;
|
|
120
|
+
}
|
|
121
|
+
else if (!escaped && (ch === "@" || WHITESPACE_RE.test(ch))) {
|
|
122
|
+
if (ch === "@")
|
|
123
|
+
span++;
|
|
124
|
+
add(ch, mask, from, to);
|
|
125
|
+
}
|
|
126
|
+
else {
|
|
127
|
+
add(ch, mask, from, to);
|
|
128
|
+
keepRuns = pending.length;
|
|
129
|
+
keepLen = pending[keepRuns - 1].text.length;
|
|
130
|
+
span = 0;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
else if (!escaped && ch === "{" && closeBraces > 0) {
|
|
134
|
+
flush();
|
|
135
|
+
mark(from, to);
|
|
136
|
+
grouped = true;
|
|
137
|
+
}
|
|
138
|
+
else if (escaped) {
|
|
139
|
+
add(ch, mask, from, to);
|
|
140
|
+
}
|
|
141
|
+
else if (WHITESPACE_RE.test(ch)) {
|
|
142
|
+
flush();
|
|
143
|
+
}
|
|
144
|
+
else if (ch === "-") {
|
|
145
|
+
const shouldFlush = hasBody;
|
|
146
|
+
add(ch, mask, from, to);
|
|
147
|
+
if (shouldFlush)
|
|
148
|
+
flush();
|
|
149
|
+
}
|
|
150
|
+
else if (ch === "@") {
|
|
151
|
+
if (hasBody) {
|
|
152
|
+
add(ch, mask, from, to);
|
|
153
|
+
flush();
|
|
154
|
+
}
|
|
155
|
+
result.push([]);
|
|
156
|
+
lastToken = lastSpan = undefined;
|
|
157
|
+
}
|
|
158
|
+
else if (LYRIC_OPENING_PUNCTUATION.has(ch)) {
|
|
159
|
+
flush();
|
|
160
|
+
mark(from, to);
|
|
161
|
+
appendRun(pending, ch, mask);
|
|
162
|
+
}
|
|
163
|
+
else if (LYRIC_SILENT_PUNCTUATION.has(ch)) {
|
|
164
|
+
if (pending.length > 0 || !lastToken?.length) {
|
|
165
|
+
mark(from, to);
|
|
166
|
+
appendRun(pending, ch, mask);
|
|
167
|
+
}
|
|
168
|
+
else {
|
|
169
|
+
appendRun(lastToken, ch, mask);
|
|
170
|
+
if (lastSpan && offsets)
|
|
171
|
+
lastSpan.end = offsets[to];
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
else if (ch.charCodeAt(0) > 0x7F) {
|
|
175
|
+
flush();
|
|
176
|
+
add(ch, mask, from, to);
|
|
177
|
+
flush();
|
|
178
|
+
}
|
|
179
|
+
else {
|
|
180
|
+
add(ch, mask, from, to);
|
|
181
|
+
}
|
|
182
|
+
}, "\\*_{}@-^");
|
|
183
|
+
flush();
|
|
184
|
+
if (pending.length > 0) {
|
|
185
|
+
if (lastToken?.length) {
|
|
186
|
+
for (const run of pending)
|
|
187
|
+
appendRun(lastToken, run.text, run.mask);
|
|
188
|
+
if (lastSpan && offsets)
|
|
189
|
+
lastSpan.end = offsets[tokEnd];
|
|
190
|
+
}
|
|
191
|
+
else {
|
|
192
|
+
if (tokenSpans && offsets && tokStart >= 0) {
|
|
193
|
+
tokenSpans[result.length] = { start: offsets[tokStart], end: offsets[tokEnd] };
|
|
194
|
+
}
|
|
195
|
+
result.push(pending);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
return result;
|
|
199
|
+
}
|
|
200
|
+
export function serializeLyric(tokens) {
|
|
201
|
+
return quote(tokens.flatMap((runs, i) => {
|
|
202
|
+
if (runs === null)
|
|
203
|
+
return [];
|
|
204
|
+
if (runs.length === 0)
|
|
205
|
+
return "@";
|
|
206
|
+
let span = 0, mask = 0, inner = "";
|
|
207
|
+
while (tokens[i + 1 + span] === null)
|
|
208
|
+
span++;
|
|
209
|
+
for (const run of runs) {
|
|
210
|
+
inner += markdownToggles(mask ^ run.mask) + run.text.replace(/[\\{}@\-*_^]/g, "\\$&");
|
|
211
|
+
mask = run.mask;
|
|
212
|
+
}
|
|
213
|
+
return `{${inner}${markdownToggles(mask)}${"@".repeat(span)}}`;
|
|
214
|
+
}).join(" "));
|
|
215
|
+
}
|
|
216
|
+
export class VoiceLyricsAttachment {
|
|
217
|
+
nodes;
|
|
218
|
+
nameHost;
|
|
219
|
+
frames;
|
|
220
|
+
kind = "lyrics";
|
|
221
|
+
layer = "foreground";
|
|
222
|
+
get sourceSpan() { return this.nameHost.ast.sourceSpan; }
|
|
223
|
+
constructor(nodes, nameHost, frames) {
|
|
224
|
+
this.nodes = nodes;
|
|
225
|
+
this.nameHost = nameHost;
|
|
226
|
+
this.frames = frames;
|
|
227
|
+
}
|
|
228
|
+
prepareHorizontal(lines, context) {
|
|
229
|
+
const targets = this.nodes.filter(node => node.ports?.["lyric"]);
|
|
230
|
+
if (targets.length === 0)
|
|
231
|
+
return;
|
|
232
|
+
const { lyrics, size, font } = this.nameHost.ast;
|
|
233
|
+
const style = lyricNameStyle(size, font);
|
|
234
|
+
for (const { tokens } of lyrics) {
|
|
235
|
+
for (let i = 0; i < tokens.length && i < targets.length; i++) {
|
|
236
|
+
const runs = tokens[i];
|
|
237
|
+
if (!runs?.length)
|
|
238
|
+
continue;
|
|
239
|
+
const { bodyWidth } = measureLyricToken(runs, style, context.textMeasurer);
|
|
240
|
+
if (bodyWidth === 0)
|
|
241
|
+
continue;
|
|
242
|
+
const first = targets[i];
|
|
243
|
+
if (tokens[i + 1] !== null) {
|
|
244
|
+
// 只扩大求解占用;视觉盒和端口不动,音符内部几何因此保持原位。
|
|
245
|
+
lines[first.layoutLine]?.registerHorizontalLayoutHook(first, first, ({ columns, start }) => {
|
|
246
|
+
const element = columns[start].find(item => item.time === first);
|
|
247
|
+
if (!element)
|
|
248
|
+
return;
|
|
249
|
+
const lyricOffset = first.ports["lyric"].x - first.box.anchor;
|
|
250
|
+
element.WL = Math.max(element.WL, bodyWidth / 2 - lyricOffset);
|
|
251
|
+
element.WR = Math.max(element.WR, bodyWidth / 2 + lyricOffset);
|
|
252
|
+
});
|
|
253
|
+
continue;
|
|
254
|
+
}
|
|
255
|
+
let end = i;
|
|
256
|
+
while (tokens[i + 1] === null) {
|
|
257
|
+
i++;
|
|
258
|
+
if (targets[i]?.layoutLine === first.layoutLine && targets[i].track === first.track)
|
|
259
|
+
end = i;
|
|
260
|
+
}
|
|
261
|
+
const leftExtent = first.box.anchor - lyricLeftPortX(first);
|
|
262
|
+
const rightSpan = Math.max(0, bodyWidth - leftExtent);
|
|
263
|
+
// 跨槽词从首音符 body.left 向右延伸 rightSpan:
|
|
264
|
+
// 1. 词未超过自然宽度(total - lastEl.margin_R)时,按各段自然宽度比例从 margin 置换刚性下界,自然间距不变;
|
|
265
|
+
// 2. 词超过自然宽度时,scale > 1 按原间距比例等比撑开各列,并借 keepMargin 保留末音右侧的弹性间隙。
|
|
266
|
+
lines[first.layoutLine]?.registerHorizontalLayoutHook(first, targets[end], ({ columns, start, end }) => {
|
|
267
|
+
const row = columns[start].findIndex(item => item.time === first);
|
|
268
|
+
if (row < 0)
|
|
269
|
+
return;
|
|
270
|
+
const lastEl = columns[end][row];
|
|
271
|
+
reserveSide(columns[start][row], "L", leftExtent);
|
|
272
|
+
let total = lastEl.WR + lastEl.margin_R;
|
|
273
|
+
for (let c = start; c < end; c++) {
|
|
274
|
+
const a = columns[c][row], b = columns[c + 1][row];
|
|
275
|
+
total += a.WR + a.margin_R + b.WL + b.margin_L;
|
|
276
|
+
}
|
|
277
|
+
const keepMargin = Math.max(0, Math.min(lastEl.margin_R, rightSpan - (total - lastEl.margin_R)));
|
|
278
|
+
const scale = (rightSpan + keepMargin) / total;
|
|
279
|
+
for (let c = start; c < end; c++) {
|
|
280
|
+
const a = columns[c][row], b = columns[c + 1][row];
|
|
281
|
+
reserveSide(a, "R", (a.WR + a.margin_R) * scale);
|
|
282
|
+
reserveSide(b, "L", (b.WL + b.margin_L) * scale);
|
|
283
|
+
}
|
|
284
|
+
reserveSide(lastEl, "R", (lastEl.WR + lastEl.margin_R) * scale - keepMargin);
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
createGeometry(context) {
|
|
290
|
+
const preparedText = this.prepareGeometry(context);
|
|
291
|
+
// 同一行同一轨的多行歌词由引擎合并成一段占用
|
|
292
|
+
return {
|
|
293
|
+
regions: preparedText,
|
|
294
|
+
paint(painter) {
|
|
295
|
+
for (const item of preparedText) {
|
|
296
|
+
paintRichLine(painter, item, item.x, item.y + item.baseline);
|
|
297
|
+
}
|
|
298
|
+
},
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
prepareGeometry(context) {
|
|
302
|
+
const { lyrics, size, font } = this.nameHost.ast;
|
|
303
|
+
const preparedText = [];
|
|
304
|
+
const targets = this.nodes.filter(node => node.ports?.["lyric"]);
|
|
305
|
+
if (targets.length === 0)
|
|
306
|
+
return preparedText;
|
|
307
|
+
// 同一行、原始轨共用下沿,跨轨框外的占用可归属另一条轨
|
|
308
|
+
const baselines = new Map();
|
|
309
|
+
for (const target of targets) {
|
|
310
|
+
let byTrack = baselines.get(target.layoutLine);
|
|
311
|
+
if (!byTrack)
|
|
312
|
+
baselines.set(target.layoutLine, byTrack = new Map());
|
|
313
|
+
const baseline = byTrack.get(target.track) ?? { bottom: -Infinity, owner: target.track };
|
|
314
|
+
const bounds = getLayoutBounds(target);
|
|
315
|
+
let bottom = bounds.y + bounds.h;
|
|
316
|
+
let owner = target.track;
|
|
317
|
+
for (const frame of this.frames) {
|
|
318
|
+
const box = context.getAttachmentBox(frame);
|
|
319
|
+
if (box.x + box.w < bounds.x || box.x > bounds.x + bounds.w || box.y + box.h <= bottom)
|
|
320
|
+
continue;
|
|
321
|
+
const regions = context.getAttachmentOccupancy(frame);
|
|
322
|
+
if (!regions)
|
|
323
|
+
throw new Error("Layout attachment dependency has not been measured");
|
|
324
|
+
const tracks = regions.filter(region => region.line === target.layoutLine);
|
|
325
|
+
if (regions.some(region => region.line !== undefined) && tracks.length === 0)
|
|
326
|
+
continue;
|
|
327
|
+
bottom = box.y + box.h;
|
|
328
|
+
for (const region of tracks) {
|
|
329
|
+
if (region.line !== undefined && context.getVisualAxis(region.line, region.track)
|
|
330
|
+
> context.getVisualAxis(target.layoutLine, owner))
|
|
331
|
+
owner = region.track;
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
if (bottom >= baseline.bottom)
|
|
335
|
+
baseline.owner = owner;
|
|
336
|
+
baseline.bottom = Math.max(baseline.bottom, bottom);
|
|
337
|
+
byTrack.set(target.track, baseline);
|
|
338
|
+
}
|
|
339
|
+
const style = lyricNameStyle(size, font);
|
|
340
|
+
const rowAdvance = style.fontSize + size * 0.25;
|
|
341
|
+
const baselineOffset = context.textMeasurer.measureText("M", style).baseline + size * 0.4;
|
|
342
|
+
const baselineOf = (bottom, row) => bottom + baselineOffset + row * rowAdvance;
|
|
343
|
+
// 跨轨框外的歌词由最下轨承担占用,各原始轨的歌词行依次排列
|
|
344
|
+
if (this.frames.length)
|
|
345
|
+
for (const [line, byTrack] of baselines) {
|
|
346
|
+
if (byTrack.size < 2)
|
|
347
|
+
continue;
|
|
348
|
+
const rows = new Map();
|
|
349
|
+
for (const [, baseline] of [...byTrack].sort(([left], [right]) => context.getVisualAxis(line, left) - context.getVisualAxis(line, right))) {
|
|
350
|
+
const row = rows.get(baseline.owner) ?? 0;
|
|
351
|
+
baseline.bottom += row * lyrics.length * rowAdvance;
|
|
352
|
+
rows.set(baseline.owner, row + 1);
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
// 歌词行名称与声部名共用左侧那一列,因此只出现在声部名所在的那一行
|
|
356
|
+
const { box: labelBox, layoutLine: labelLine, track: labelTrack } = this.nameHost;
|
|
357
|
+
const labelBaseline = baselines.get(labelLine)?.get(labelTrack);
|
|
358
|
+
for (let row = 0; row < lyrics.length; row++) {
|
|
359
|
+
const lyric = lyrics[row];
|
|
360
|
+
for (let i = 0; i < lyric.tokens.length && i < targets.length; i++) {
|
|
361
|
+
const runs = lyric.tokens[i];
|
|
362
|
+
const target = targets[i];
|
|
363
|
+
const baseline = baselines.get(target.layoutLine)?.get(target.track);
|
|
364
|
+
if (!runs?.length || !baseline)
|
|
365
|
+
continue;
|
|
366
|
+
const { richLine, prefixWidth, bodyWidth } = measureLyricToken(runs, style, context.textMeasurer);
|
|
367
|
+
const leftAligned = lyric.tokens[i + 1] === null;
|
|
368
|
+
const ast = this.nameHost.ast;
|
|
369
|
+
preparedText.push({
|
|
370
|
+
...richLine,
|
|
371
|
+
x: target.box.x + (leftAligned
|
|
372
|
+
? lyricLeftPortX(target) - prefixWidth
|
|
373
|
+
: target.ports["lyric"].x - bodyWidth / 2 - prefixWidth),
|
|
374
|
+
y: baselineOf(baseline.bottom, row) - richLine.baseline,
|
|
375
|
+
line: target.layoutLine,
|
|
376
|
+
track: baseline.owner,
|
|
377
|
+
get sourceSpan() { return ast.lyricTokenSpan(row, i); },
|
|
378
|
+
});
|
|
379
|
+
}
|
|
380
|
+
if (!lyric.name || !labelBox || !labelBaseline)
|
|
381
|
+
continue;
|
|
382
|
+
const richLine = prepareRichLine(lyric.name, style, context.textMeasurer);
|
|
383
|
+
preparedText.push({
|
|
384
|
+
...richLine,
|
|
385
|
+
x: labelBox.x + labelBox.anchor - richLine.w,
|
|
386
|
+
y: baselineOf(labelBaseline.bottom, row) - richLine.baseline,
|
|
387
|
+
line: labelLine,
|
|
388
|
+
track: labelBaseline.owner,
|
|
389
|
+
});
|
|
390
|
+
}
|
|
391
|
+
return preparedText;
|
|
392
|
+
}
|
|
393
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
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 { Painter, TextStyle } from "../../render/types.js";
|
|
7
|
+
import { TemporalNodeBase } from "../temporal.js";
|
|
8
|
+
import { type LyricSlot } from "./lyrics.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: LyricSlot[];
|
|
41
|
+
}[];
|
|
42
|
+
private readonly lyricSources;
|
|
43
|
+
private cachedLyricSpans?;
|
|
44
|
+
get children(): ASTBraceNode[];
|
|
45
|
+
/** 仅在导航定位读取时按需计算词元源码区间,正常编译渲染零开销 */
|
|
46
|
+
lyricTokenSpan(row: number, index: number): SourceSpan | undefined;
|
|
47
|
+
/**
|
|
48
|
+
* voice 的音符内容仍按普通 sequence 进入全局时间列
|
|
49
|
+
* 此处按需建立歌词作用域,并在内容前创建声部名对象
|
|
50
|
+
*/
|
|
51
|
+
loweringEnter(ctx: LoweringContext): VoiceNameTemporal[];
|
|
52
|
+
loweringExit(ctx: LoweringContext): never[];
|
|
53
|
+
constructor(span: SourceSpan, args: FunctionArgs, ctx: ParserContext, parent?: ASTNodeBase | null);
|
|
54
|
+
addLyric(name: string | number, lyric: string, span?: SourceSpan | null, ctx?: ParserContext | null): void;
|
|
55
|
+
toString(source: string): string;
|
|
56
|
+
}
|
|
57
|
+
export declare class VoiceNameTemporal extends TemporalNodeBase {
|
|
58
|
+
ast: VoiceFunction;
|
|
59
|
+
box: LayoutBox;
|
|
60
|
+
/** 名称右侧为大括号预留的横向空间,0 表示不需要 */
|
|
61
|
+
private readonly braceSpace;
|
|
62
|
+
private preparedName;
|
|
63
|
+
constructor(ast: VoiceFunction, braceSpace: number);
|
|
64
|
+
prepareLayout(context: LayoutPrepareContext): void;
|
|
65
|
+
paint(painter: Painter): void;
|
|
66
|
+
}
|
|
67
|
+
export declare const VoiceNode: ASTFunctionClass;
|