jpfun 0.9.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.
Files changed (104) hide show
  1. package/README.md +10 -15
  2. package/dist/diagnostic.d.ts +29 -2
  3. package/dist/diagnostic.js +5 -4
  4. package/dist/functions/arpeggio/index.js +2 -3
  5. package/dist/functions/bar/index.d.ts +3 -0
  6. package/dist/functions/bar/index.js +18 -3
  7. package/dist/functions/beam/layout.js +3 -2
  8. package/dist/functions/box/index.js +1 -0
  9. package/dist/functions/dash/index.js +5 -2
  10. package/dist/functions/div/index.d.ts +1 -1
  11. package/dist/functions/div/index.js +4 -2
  12. package/dist/functions/dyn/index.js +1 -0
  13. package/dist/functions/gliss/index.js +4 -2
  14. package/dist/functions/grace/index.js +3 -5
  15. package/dist/functions/head/index.js +23 -30
  16. package/dist/functions/key/index.js +4 -7
  17. package/dist/functions/meter/index.d.ts +1 -0
  18. package/dist/functions/meter/index.js +3 -0
  19. package/dist/functions/meter/shape.d.ts +1 -1
  20. package/dist/functions/note/accidentals.d.ts +3 -3
  21. package/dist/functions/note/accidentals.js +29 -16
  22. package/dist/functions/note/index.js +11 -13
  23. package/dist/functions/note/source-transform.d.ts +15 -0
  24. package/dist/functions/note/source-transform.js +74 -67
  25. package/dist/functions/ottava/index.js +1 -0
  26. package/dist/functions/page/index.js +4 -3
  27. package/dist/functions/rest/index.js +8 -6
  28. package/dist/functions/slide/index.js +1 -0
  29. package/dist/functions/temporal.d.ts +9 -3
  30. package/dist/functions/temporal.js +15 -2
  31. package/dist/functions/tie/index.js +28 -4
  32. package/dist/functions/tremolo/index.js +1 -0
  33. package/dist/functions/tuplet/index.js +1 -0
  34. package/dist/functions/up/index.js +3 -5
  35. package/dist/functions/voice/connections.d.ts +1 -1
  36. package/dist/functions/voice/lyrics.d.ts +9 -5
  37. package/dist/functions/voice/lyrics.js +148 -44
  38. package/dist/functions/voice/voice.d.ts +6 -2
  39. package/dist/functions/voice/voice.js +49 -17
  40. package/dist/functions/voice/voices.js +2 -1
  41. package/dist/functions/volta/index.js +1 -0
  42. package/dist/index.d.ts +97 -3
  43. package/dist/index.js +97 -3
  44. package/dist/jpfun.min.js +47 -60
  45. package/dist/layout/engine.d.ts +4 -3
  46. package/dist/layout/engine.js +160 -6
  47. package/dist/layout/model.d.ts +12 -3
  48. package/dist/layout/model.js +15 -7
  49. package/dist/layout/page.js +2 -2
  50. package/dist/layout/path.js +1 -1
  51. package/dist/layout/types.d.ts +10 -2
  52. package/dist/parser/grammarType.d.ts +3 -3
  53. package/dist/parser/parse-utils/brace-utils.js +7 -17
  54. package/dist/parser/parse-utils/call-utils.d.ts +3 -1
  55. package/dist/parser/parse-utils/call-utils.js +17 -32
  56. package/dist/parser/parse-utils/note-utils.d.ts +4 -0
  57. package/dist/parser/parse-utils/note-utils.js +8 -12
  58. package/dist/parser/parserContext.d.ts +8 -2
  59. package/dist/parser/parserContext.js +57 -55
  60. package/dist/parser/preprocess.d.ts +7 -0
  61. package/dist/parser/preprocess.js +21 -19
  62. package/dist/pipeline.d.ts +1 -1
  63. package/dist/playback/compile.js +1 -1
  64. package/dist/render/canvas.js +50 -35
  65. package/dist/render/index.d.ts +1 -1
  66. package/dist/render/index.js +1 -1
  67. package/dist/render/paint.d.ts +7 -0
  68. package/dist/render/paint.js +70 -20
  69. package/dist/render/recording.d.ts +8 -1
  70. package/dist/render/recording.js +12 -0
  71. package/dist/render/svg.d.ts +5 -1
  72. package/dist/render/svg.js +37 -17
  73. package/dist/render/text.d.ts +2 -2
  74. package/dist/render/text.js +7 -6
  75. package/dist/render/translate.d.ts +3 -1
  76. package/dist/render/translate.js +6 -0
  77. package/dist/render/types.d.ts +9 -0
  78. package/grammar.md +6 -5
  79. package/package.json +8 -18
  80. package/dist/converter/midi/convert.d.ts +0 -42
  81. package/dist/converter/midi/convert.js +0 -682
  82. package/dist/converter/midi/index.d.ts +0 -5
  83. package/dist/converter/midi/index.js +0 -11
  84. package/dist/converter/midi/json.d.ts +0 -38
  85. package/dist/converter/midi/json.js +0 -1
  86. package/dist/converter/midi/triplet.d.ts +0 -30
  87. package/dist/converter/midi/triplet.js +0 -75
  88. package/dist/converter/musicxml/dom.d.ts +0 -25
  89. package/dist/converter/musicxml/dom.js +0 -65
  90. package/dist/converter/musicxml/features.d.ts +0 -114
  91. package/dist/converter/musicxml/features.js +0 -501
  92. package/dist/converter/musicxml/index.d.ts +0 -14
  93. package/dist/converter/musicxml/index.js +0 -1285
  94. package/dist/converter/musicxml/model.d.ts +0 -176
  95. package/dist/converter/musicxml/model.js +0 -1
  96. package/dist/converter/musicxml/resolve.d.ts +0 -10
  97. package/dist/converter/musicxml/resolve.js +0 -152
  98. package/dist/converter/source.d.ts +0 -19
  99. package/dist/converter/source.js +0 -53
  100. package/dist/core.d.ts +0 -24
  101. package/dist/core.js +0 -24
  102. package/dist/jpfun.core.min.js +0 -132
  103. package/dist/jpfun.from-midi.min.js +0 -9
  104. package/dist/jpfun.from-musicxml.min.js +0 -12
@@ -3,7 +3,7 @@ import { quote } from "../../parser/parse-utils/string-utils.js";
3
3
  import { appendRun, markdownToggles, paintRichLine, prepareRichLine, scanMarkdown, } from "../../render/text.js";
4
4
  const WHITESPACE_RE = /\s/;
5
5
  /** 歌词与歌词行名称相对声部字号的字号 */
6
- export const LYRIC_SIZE_RATIO = 0.82;
6
+ export const LYRIC_SIZE_RATIO = 0.88;
7
7
  // 分词跟后面的标点
8
8
  const LYRIC_OPENING_PUNCTUATION = new Set("(([[{〈《「『【〔〖〘〚‘“");
9
9
  // 不发声的标点;位于歌词槽首尾时只渲染,不占位,也不参与对齐中心计算
@@ -44,12 +44,25 @@ function measureLyricToken(runs, style, measurer) {
44
44
  };
45
45
  return { richLine, prefixWidth: rangeWidth(0, start), bodyWidth: rangeWidth(start, end) };
46
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
+ }
47
59
  /**
48
60
  * 把歌词拆成与音符一一对应的富文本槽位,同时支持跨槽或槽内的 Markdown 样式(`**`、`*`、`_`、`^`)。
49
61
  * 例如 `hello world` 拆成 `hello`、`world`,`hel-lo` 拆成 `hel-`、`lo`;
50
- * `你好吗` 默认逐字拆分,`你 @ 好` 中的 `@` 留出一个空槽,`{你好} 啊` 则把 `你好` 放在同一个槽位。
62
+ * `你好吗` 默认逐字拆分,`你 @ 好` 中的 `@` 留出一个空槽,
63
+ * `{你好} 啊` 把 `你好` 放在同一个槽位,`{a@@}` 则左对齐并覆盖三个槽位(后续槽记为 `null`)。
51
64
  */
52
- export function parseLyric(value) {
65
+ export function parseLyric(value, offsets, tokenSpans) {
53
66
  let closeBraces = 0;
54
67
  for (let i = 0; i < value.length; i++) {
55
68
  if (value[i] === "\\")
@@ -59,99 +72,152 @@ export function parseLyric(value) {
59
72
  }
60
73
  const result = [];
61
74
  let pending = [];
62
- let hasBody = false;
63
- let grouped = false;
64
- const add = (text, mask) => {
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) => {
65
82
  appendRun(pending, text, mask);
83
+ mark(from, to);
66
84
  hasBody = true;
67
85
  };
68
86
  const flush = (force = false) => {
69
87
  if (!force && !hasBody)
70
88
  return;
71
- result.push(pending);
89
+ if (tokenSpans && offsets && tokStart >= 0) {
90
+ tokenSpans[result.length] = lastSpan = { start: offsets[tokStart], end: offsets[tokEnd] };
91
+ }
92
+ result.push(lastToken = pending);
72
93
  pending = [];
94
+ tokStart = -1;
73
95
  hasBody = false;
74
96
  };
75
- scanMarkdown(value, (ch, mask, escaped) => {
97
+ scanMarkdown(value, (ch, mask, escaped, from, to) => {
76
98
  if (!escaped && ch === "}")
77
99
  closeBraces--;
78
100
  if (grouped) {
79
101
  if (!escaped && ch === "}") {
80
- flush(true);
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;
81
119
  grouped = false;
82
120
  }
121
+ else if (!escaped && (ch === "@" || WHITESPACE_RE.test(ch))) {
122
+ if (ch === "@")
123
+ span++;
124
+ add(ch, mask, from, to);
125
+ }
83
126
  else {
84
- appendRun(pending, ch, mask);
127
+ add(ch, mask, from, to);
128
+ keepRuns = pending.length;
129
+ keepLen = pending[keepRuns - 1].text.length;
130
+ span = 0;
85
131
  }
86
132
  }
87
133
  else if (!escaped && ch === "{" && closeBraces > 0) {
88
134
  flush();
135
+ mark(from, to);
89
136
  grouped = true;
90
137
  }
91
138
  else if (escaped) {
92
- add(ch, mask);
139
+ add(ch, mask, from, to);
93
140
  }
94
141
  else if (WHITESPACE_RE.test(ch)) {
95
142
  flush();
96
143
  }
97
144
  else if (ch === "-") {
98
145
  const shouldFlush = hasBody;
99
- add(ch, mask);
146
+ add(ch, mask, from, to);
100
147
  if (shouldFlush)
101
148
  flush();
102
149
  }
103
150
  else if (ch === "@") {
104
151
  if (hasBody) {
105
- add(ch, mask);
152
+ add(ch, mask, from, to);
106
153
  flush();
107
154
  }
108
155
  result.push([]);
156
+ lastToken = lastSpan = undefined;
109
157
  }
110
158
  else if (LYRIC_OPENING_PUNCTUATION.has(ch)) {
111
159
  flush();
160
+ mark(from, to);
112
161
  appendRun(pending, ch, mask);
113
162
  }
114
163
  else if (LYRIC_SILENT_PUNCTUATION.has(ch)) {
115
- appendRun(pending.length > 0 || !result.at(-1)?.length ? pending : result.at(-1), ch, mask);
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
+ }
116
173
  }
117
174
  else if (ch.charCodeAt(0) > 0x7F) {
118
175
  flush();
119
- add(ch, mask);
176
+ add(ch, mask, from, to);
120
177
  flush();
121
178
  }
122
179
  else {
123
- add(ch, mask);
180
+ add(ch, mask, from, to);
124
181
  }
125
182
  }, "\\*_{}@-^");
126
183
  flush();
127
184
  if (pending.length > 0) {
128
- if (result.at(-1)?.length) {
185
+ if (lastToken?.length) {
129
186
  for (const run of pending)
130
- appendRun(result.at(-1), run.text, run.mask);
187
+ appendRun(lastToken, run.text, run.mask);
188
+ if (lastSpan && offsets)
189
+ lastSpan.end = offsets[tokEnd];
131
190
  }
132
191
  else {
192
+ if (tokenSpans && offsets && tokStart >= 0) {
193
+ tokenSpans[result.length] = { start: offsets[tokStart], end: offsets[tokEnd] };
194
+ }
133
195
  result.push(pending);
134
196
  }
135
197
  }
136
198
  return result;
137
199
  }
138
200
  export function serializeLyric(tokens) {
139
- return quote(tokens.map(runs => {
201
+ return quote(tokens.flatMap((runs, i) => {
202
+ if (runs === null)
203
+ return [];
140
204
  if (runs.length === 0)
141
205
  return "@";
142
- let mask = 0;
143
- let inner = "";
206
+ let span = 0, mask = 0, inner = "";
207
+ while (tokens[i + 1 + span] === null)
208
+ span++;
144
209
  for (const run of runs) {
145
210
  inner += markdownToggles(mask ^ run.mask) + run.text.replace(/[\\{}@\-*_^]/g, "\\$&");
146
211
  mask = run.mask;
147
212
  }
148
- return `{${inner}${markdownToggles(mask)}}`;
213
+ return `{${inner}${markdownToggles(mask)}${"@".repeat(span)}}`;
149
214
  }).join(" "));
150
215
  }
151
216
  export class VoiceLyricsAttachment {
152
217
  nodes;
153
218
  nameHost;
154
219
  frames;
220
+ kind = "lyrics";
155
221
  layer = "foreground";
156
222
  get sourceSpan() { return this.nameHost.ast.sourceSpan; }
157
223
  constructor(nodes, nameHost, frames) {
@@ -165,26 +231,59 @@ export class VoiceLyricsAttachment {
165
231
  return;
166
232
  const { lyrics, size, font } = this.nameHost.ast;
167
233
  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);
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;
175
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
+ });
176
286
  }
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
287
  }
189
288
  }
190
289
  createGeometry(context) {
@@ -238,8 +337,8 @@ export class VoiceLyricsAttachment {
238
337
  byTrack.set(target.track, baseline);
239
338
  }
240
339
  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;
340
+ const rowAdvance = style.fontSize + size * 0.25;
341
+ const baselineOffset = context.textMeasurer.measureText("M", style).baseline + size * 0.4;
243
342
  const baselineOf = (bottom, row) => bottom + baselineOffset + row * rowAdvance;
244
343
  // 跨轨框外的歌词由最下轨承担占用,各原始轨的歌词行依次排列
245
344
  if (this.frames.length)
@@ -262,15 +361,20 @@ export class VoiceLyricsAttachment {
262
361
  const runs = lyric.tokens[i];
263
362
  const target = targets[i];
264
363
  const baseline = baselines.get(target.layoutLine)?.get(target.track);
265
- if (!runs.length || !baseline)
364
+ if (!runs?.length || !baseline)
266
365
  continue;
267
366
  const { richLine, prefixWidth, bodyWidth } = measureLyricToken(runs, style, context.textMeasurer);
367
+ const leftAligned = lyric.tokens[i + 1] === null;
368
+ const ast = this.nameHost.ast;
268
369
  preparedText.push({
269
370
  ...richLine,
270
- x: target.box.x + target.ports["lyric"].x - bodyWidth / 2 - prefixWidth,
371
+ x: target.box.x + (leftAligned
372
+ ? lyricLeftPortX(target) - prefixWidth
373
+ : target.ports["lyric"].x - bodyWidth / 2 - prefixWidth),
271
374
  y: baselineOf(baseline.bottom, row) - richLine.baseline,
272
375
  line: target.layoutLine,
273
376
  track: baseline.owner,
377
+ get sourceSpan() { return ast.lyricTokenSpan(row, i); },
274
378
  });
275
379
  }
276
380
  if (!lyric.name || !labelBox || !labelBaseline)
@@ -3,9 +3,9 @@ import { type LayoutBox, type LayoutPrepareContext } from "../../layout/types.js
3
3
  import type { LoweringContext } from "../../lowering/loweringContext.js";
4
4
  import { GrammarNode, GrammarSugarNode } from "../../parser/grammarType.js";
5
5
  import { ParserContext } from "../../parser/parserContext.js";
6
- import { type RichTextRun } from "../../render/text.js";
7
6
  import type { Painter, TextStyle } from "../../render/types.js";
8
7
  import { TemporalNodeBase } from "../temporal.js";
8
+ import { type LyricSlot } from "./lyrics.js";
9
9
  export declare function voiceNameStyle(size: number, fontFamily: string): TextStyle;
10
10
  export declare class VoiceFunction extends ASTFunctionNode {
11
11
  static def: {
@@ -37,9 +37,13 @@ export declare class VoiceFunction extends ASTFunctionNode {
37
37
  readonly font: string;
38
38
  lyrics: {
39
39
  name: string;
40
- tokens: RichTextRun[][];
40
+ tokens: LyricSlot[];
41
41
  }[];
42
+ private readonly lyricSources;
43
+ private cachedLyricSpans?;
42
44
  get children(): ASTBraceNode[];
45
+ /** 仅在导航定位读取时按需计算词元源码区间,正常编译渲染零开销 */
46
+ lyricTokenSpan(row: number, index: number): SourceSpan | undefined;
43
47
  /**
44
48
  * voice 的音符内容仍按普通 sequence 进入全局时间列
45
49
  * 此处按需建立歌词作用域,并在内容前创建声部名对象
@@ -1,7 +1,7 @@
1
1
  import { ASTBraceNode, ASTFunctionNode, ASTTextNode } from "../ASTtypes.js";
2
2
  import { Diagnostic, ErrorDiagnostic, WarningDiagnostic } from "../../diagnostic.js";
3
3
  import { isLayoutAttachment } from "../../layout/types.js";
4
- import { findRightParen } from "../../parser/parse-utils/call-utils.js";
4
+ import { findRightParen, trimRange } from "../../parser/parse-utils/call-utils.js";
5
5
  import { findClosingQuote, quote } from "../../parser/parse-utils/string-utils.js";
6
6
  import { ParserContext, skipSpaces } from "../../parser/parserContext.js";
7
7
  import { paintRichLine, prepareRichLine } from "../../render/text.js";
@@ -28,7 +28,7 @@ export class VoiceFunction extends ASTFunctionNode {
28
28
  ~~~
29
29
  - **额外参数**:每项为一行歌词;命名参数同时给出歌词行名,位置参数表示无行名。省略声部名但仍需歌词时,保留空位,如 \`@voice({1 2 3}, , ha ha ha)\`。
30
30
 
31
- 歌词可用引号包裹;调用 \`@voice\` 时歌词若含逗号须加引号。英文用空格或连字符 \`-\` 分词,用 \`@\` 留空。名称与歌词均支持 \`*斜体*\`、\`**粗体**\`、\`_下划线_\`、\`^上标^\`(可跨多个音符,如 \`*春眠不觉晓*\`)。
31
+ 歌词可用引号包裹;调用 \`@voice\` 时歌词若含逗号须加引号。英文用空格或连字符 \`-\` 分词,用 \`@\` 留空;\`{你好}\` 合为一个音符槽,末尾加 \`@\`(如 \`{a@@}\`)可左对齐并覆盖多个音符槽。名称与歌词均支持 \`*斜体*\`、\`**粗体**\`、\`_下划线_\`、\`^上标^\`(可跨多个音符,如 \`*春眠不觉晓*\`)。
32
32
 
33
33
  **简写**:\`N\` 声明音符行,\`L\` 声明歌词行,括号内的名称可省略:
34
34
  ~~~jpfun
@@ -84,25 +84,24 @@ L: la la la
84
84
  return { next: pos, node };
85
85
  }
86
86
  // 字符串收集
87
- pos = skipSpaces(source, pos, end);
88
- if (pos >= end)
87
+ const from = skipSpaces(source, pos, end);
88
+ if (from >= end)
89
89
  return null; // 没有内容了 不去糖
90
90
  let lyric;
91
91
  // 双引号才是字符串,单引号不是
92
- if (source[pos] === '"') {
92
+ if (source[from] === '"') {
93
93
  // 未闭合就不去糖,词法层每次击键都跑,不能抛
94
- const close = findClosingQuote(source, pos, end);
94
+ const close = findClosingQuote(source, from, end);
95
95
  if (close < 0)
96
96
  return null;
97
- lyric = { start: pos, end: close + 1 };
98
- pos = close + 1;
97
+ lyric = { start: from, end: close + 1 };
99
98
  }
100
99
  else {
101
100
  // 没有引号的 以换行符为界切分 预处理已经跳过了转义的换行符了
102
- const from = pos;
103
- while (pos < end && source[pos] !== "\n")
104
- pos++;
105
- lyric = { start: from, end: pos };
101
+ let close = from;
102
+ while (close < end && source[close] !== "\n")
103
+ close++;
104
+ lyric = { start: from, end: close };
106
105
  }
107
106
  const node = {
108
107
  kind: "sugar",
@@ -111,9 +110,13 @@ L: la la la
111
110
  lyric,
112
111
  name,
113
112
  },
114
- span: { start, end: pos },
113
+ span: { start, end: lyric.end },
114
+ syntaxKind: [
115
+ { kind: "operator", span: { start, end: pos } },
116
+ { kind: "string", span: lyric },
117
+ ],
115
118
  };
116
- return { next: pos, node };
119
+ return { next: lyric.end, node };
117
120
  }
118
121
  static deSugarRelation(ctx, nodes, at, members) {
119
122
  const n = nodes[at++];
@@ -141,7 +144,7 @@ L: la la la
141
144
  if (voiceNode === null) {
142
145
  throw new ErrorDiagnostic("E_LYRICS_WITHOUT_VOICE_NOTES", `语法糖 'L:' 或 'L(name):' 必须跟在 @voice(或 N:)之后,但没有找到符合要求的 voice;请检查语法或直接使用 @voice 函数`, n.span);
143
146
  }
144
- voiceNode.addLyric(parseStr(n.data.name), parseStr(n.data.lyric), n.span, ctx);
147
+ voiceNode.addLyric(parseStr(n.data.name), parseStr(n.data.lyric), n.data.lyric, ctx);
145
148
  ctx.nodes.length = voiceNodeAt + 1; // 清除voiceNodeAt之后的TextNode 因为被夹在N和L之间
146
149
  // 消费后面可能的换行符 只消费一个
147
150
  if (at < nodes.length && typeof nodes[at] === "number" && ctx.source[nodes[at]] === "\n")
@@ -238,7 +241,33 @@ L: la la la
238
241
  size; // 声部的字体大小
239
242
  font;
240
243
  lyrics;
244
+ lyricSources = [];
245
+ cachedLyricSpans;
241
246
  get children() { return [this.content]; }
247
+ /** 仅在导航定位读取时按需计算词元源码区间,正常编译渲染零开销 */
248
+ lyricTokenSpan(row, index) {
249
+ this.cachedLyricSpans ??= this.lyricSources.map(({ source, lyric, span }) => {
250
+ const { start, end } = trimRange(source, span.start, span.end);
251
+ const quoted = end - start >= 2 && source[start] === '"' && source[end - 1] === '"';
252
+ const base = start + (quoted ? 1 : 0), raw = source.slice(base, end - (quoted ? 1 : 0));
253
+ const offsets = [];
254
+ let c = 0;
255
+ if (lyric !== raw || !raw.includes("\\u")) {
256
+ raw.replace(/\\(u[0-9a-fA-F]{4}|["\\])/g, (m, _, i) => {
257
+ while (c <= i)
258
+ offsets.push(base + c++);
259
+ c = i + m.length;
260
+ return "";
261
+ });
262
+ }
263
+ while (c <= raw.length)
264
+ offsets.push(base + c++);
265
+ const tokenSpans = [];
266
+ parseLyric(lyric, offsets, tokenSpans);
267
+ return tokenSpans;
268
+ });
269
+ return this.cachedLyricSpans[row]?.[index];
270
+ }
242
271
  /**
243
272
  * voice 的音符内容仍按普通 sequence 进入全局时间列
244
273
  * 此处按需建立歌词作用域,并在内容前创建声部名对象
@@ -289,7 +318,7 @@ L: la la la
289
318
  this.lyrics = [];
290
319
  for (const [key, value] of args) {
291
320
  if (typeof value === "string") {
292
- this.addLyric(key, value);
321
+ this.addLyric(key, value, ctx.callNode?.args[key]?.valueSpan ?? null, ctx);
293
322
  continue;
294
323
  }
295
324
  const arg = value;
@@ -298,7 +327,7 @@ L: la la la
298
327
  ctx.diagnostics.push(new WarningDiagnostic("W_VOICE_INVALID_LYRIC", "@voice 的歌词参数值解析失败, 参数[{key}]将被忽略", arg.valueSpan, { key }));
299
328
  }
300
329
  else
301
- this.addLyric(key, v);
330
+ this.addLyric(key, v, arg.valueSpan, ctx);
302
331
  }
303
332
  }
304
333
  addLyric(name, lyric, span = null, ctx = null) {
@@ -308,6 +337,9 @@ L: la la la
308
337
  if (tokens.length === 0 && ctx && span) {
309
338
  ctx.diagnostics.push(new WarningDiagnostic("W_EMPTY_LYRIC", "@voice的歌词{name}是空的", span, { name }));
310
339
  }
340
+ this.cachedLyricSpans = undefined;
341
+ if (ctx && span)
342
+ this.lyricSources[this.lyrics.length] = { source: ctx.source, lyric, span };
311
343
  this.lyrics.push({ name, tokens });
312
344
  if (span) {
313
345
  // 歌词是成节点之后才补上来的,祖先的 span 得一起长,否则按源码位置查节点会漏掉歌词那几行
@@ -91,7 +91,7 @@ N(女低): 1 2 3
91
91
  name: "minGap",
92
92
  namedOnly: true,
93
93
  type: "length",
94
- default: { value: 0.35, unit: "em" },
94
+ default: { value: 0.2, unit: "em" },
95
95
  description: "上方记号借用间距后保留的最小间距",
96
96
  },
97
97
  ],
@@ -287,6 +287,7 @@ class VoicesBraceAttachment {
287
287
  names;
288
288
  ast;
289
289
  connections;
290
+ kind = "voices";
290
291
  layer = "background";
291
292
  leftWidth = 0;
292
293
  measuredLabels = [];
@@ -90,6 +90,7 @@ class VoltaAttachment {
90
90
  ast;
91
91
  from;
92
92
  to;
93
+ kind = "volta";
93
94
  layer = "foreground";
94
95
  constructor(ast, from, to) {
95
96
  this.ast = ast;
package/dist/index.d.ts CHANGED
@@ -1,3 +1,97 @@
1
- export * from "./core.js";
2
- export * from "./converter/midi/index.js";
3
- export * from "./converter/musicxml/index.js";
1
+ export * from "./pipeline.js";
2
+ export * from "./functions/ASTtypes.js";
3
+ export * from "./functions/temporal.js";
4
+ export * from "./functions/default.js";
5
+ export * from "./functions/adjust/index.js";
6
+ export * from "./functions/arpeggio/index.js";
7
+ export * from "./functions/arpeggio/shape.js";
8
+ export * from "./functions/bar/index.js";
9
+ export * from "./functions/beam/index.js";
10
+ export * from "./functions/beam/auto.js";
11
+ export * from "./functions/beam/layout.js";
12
+ export * from "./functions/box/index.js";
13
+ export * from "./functions/br/index.js";
14
+ export * from "./functions/dash/index.js";
15
+ export * from "./functions/div/index.js";
16
+ export * from "./functions/dot/index.js";
17
+ export * from "./functions/dyn/index.js";
18
+ export * from "./functions/gliss/index.js";
19
+ export * from "./functions/grace/index.js";
20
+ export * from "./functions/head/index.js";
21
+ export * from "./functions/key/index.js";
22
+ export * from "./functions/meter/index.js";
23
+ export * from "./functions/meter/shape.js";
24
+ export * from "./functions/note/index.js";
25
+ export * from "./functions/note/accidentals.js";
26
+ export * from "./functions/note/noteName.js";
27
+ export * from "./functions/note/source-transform.js";
28
+ export * from "./functions/ottava/index.js";
29
+ export * from "./functions/page/index.js";
30
+ export * from "./functions/pagebreak/index.js";
31
+ export * from "./functions/program/index.js";
32
+ export * from "./functions/rest/index.js";
33
+ export * from "./functions/set/index.js";
34
+ export * from "./functions/slide/index.js";
35
+ export * from "./functions/stack/index.js";
36
+ export * from "./functions/symbol/index.js";
37
+ export * from "./functions/symbol/ornament.js";
38
+ export * from "./functions/symbol/shape.js";
39
+ export * from "./functions/symbol/symbols/accent.js";
40
+ export * from "./functions/symbol/symbols/coda.js";
41
+ export * from "./functions/symbol/symbols/dc.js";
42
+ export * from "./functions/symbol/symbols/ds.js";
43
+ export * from "./functions/symbol/symbols/dynamics.js";
44
+ export * from "./functions/symbol/symbols/fermata.js";
45
+ export * from "./functions/symbol/symbols/fine.js";
46
+ export * from "./functions/symbol/symbols/harmonic.js";
47
+ export * from "./functions/symbol/symbols/marcato.js";
48
+ export * from "./functions/symbol/symbols/mordent.js";
49
+ export * from "./functions/symbol/symbols/prall.js";
50
+ export * from "./functions/symbol/symbols/segno.js";
51
+ export * from "./functions/symbol/symbols/staccato.js";
52
+ export * from "./functions/symbol/symbols/tenuto.js";
53
+ export * from "./functions/symbol/symbols/tocoda.js";
54
+ export * from "./functions/symbol/symbols/tr.js";
55
+ export * from "./functions/symbol/symbols/turn.js";
56
+ export * from "./functions/symbol/symbols/wheel.js";
57
+ export * from "./functions/tempo/index.js";
58
+ export * from "./functions/tempo/glyph.js";
59
+ export * from "./functions/text/index.js";
60
+ export * from "./functions/tie/index.js";
61
+ export * from "./functions/tremolo/index.js";
62
+ export * from "./functions/tuplet/index.js";
63
+ export * from "./functions/up/index.js";
64
+ export * from "./functions/voice/voice.js";
65
+ export * from "./functions/voice/voices.js";
66
+ export * from "./functions/voice/connections.js";
67
+ export * from "./functions/voice/lyrics.js";
68
+ export * from "./functions/volta/index.js";
69
+ export * from "./diagnostic.js";
70
+ export * from "./fraction.js";
71
+ export * from "./parser/types.js";
72
+ export * from "./parser/grammarType.js";
73
+ export { ParserContext, DEFAULT_FONT_SIZE, skipSpaces, skipSpacesBack } from "./parser/parserContext.js";
74
+ export * from "./parser/preprocess.js";
75
+ export * from "./parser/parse-utils/brace-utils.js";
76
+ export * from "./parser/parse-utils/call-utils.js";
77
+ export * from "./parser/parse-utils/label-utils.js";
78
+ export * from "./parser/parse-utils/length-utils.js";
79
+ export * from "./parser/parse-utils/note-utils.js";
80
+ export * from "./parser/parse-utils/string-utils.js";
81
+ export { quote as quoteSourceString } from "./parser/parse-utils/string-utils.js";
82
+ export * from "./lowering/types.js";
83
+ export * from "./lowering/loweringContext.js";
84
+ export * from "./lowering/track.js";
85
+ export * from "./playback/types.js";
86
+ export * from "./playback/event.js";
87
+ export * from "./playback/time.js";
88
+ export * from "./playback/compile.js";
89
+ export * from "./layout/types.js";
90
+ export * from "./layout/model.js";
91
+ export * from "./layout/default.js";
92
+ export * from "./layout/page.js";
93
+ export * from "./layout/engine.js";
94
+ export * from "./layout/path.js";
95
+ export * from "./layout/range.js";
96
+ export * from "./render/index.js";
97
+ export * from "./render/translate.js";