jpfun 0.2.0 → 0.4.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 (167) hide show
  1. package/README.md +141 -11
  2. package/dist/converter/midi/convert.d.ts +50 -0
  3. package/dist/converter/midi/convert.js +700 -0
  4. package/dist/converter/midi/index.d.ts +5 -0
  5. package/dist/converter/midi/index.js +11 -0
  6. package/dist/converter/midi/json.d.ts +38 -0
  7. package/dist/converter/midi/json.js +1 -0
  8. package/dist/converter/midi/triplet.d.ts +30 -0
  9. package/dist/converter/midi/triplet.js +75 -0
  10. package/dist/converter/musicxml/dom.d.ts +25 -0
  11. package/dist/converter/musicxml/dom.js +65 -0
  12. package/dist/converter/musicxml/features.d.ts +51 -0
  13. package/dist/converter/musicxml/features.js +232 -0
  14. package/dist/converter/musicxml/index.d.ts +9 -0
  15. package/dist/converter/musicxml/index.js +1140 -0
  16. package/dist/converter/musicxml/model.d.ts +123 -0
  17. package/dist/converter/musicxml/model.js +1 -0
  18. package/dist/converter/source.d.ts +22 -0
  19. package/dist/converter/source.js +51 -0
  20. package/dist/core.d.ts +22 -0
  21. package/dist/core.js +22 -0
  22. package/dist/diagnostic.js +5 -2
  23. package/dist/fraction.d.ts +24 -0
  24. package/dist/fraction.js +132 -0
  25. package/dist/functions/ASTtypes.d.ts +51 -31
  26. package/dist/functions/ASTtypes.js +59 -38
  27. package/dist/functions/adjust/index.d.ts +2 -0
  28. package/dist/functions/adjust/index.js +193 -0
  29. package/dist/functions/arpeggio/index.d.ts +2 -0
  30. package/dist/functions/arpeggio/index.js +180 -0
  31. package/dist/functions/arpeggio/shape.d.ts +7 -0
  32. package/dist/functions/arpeggio/shape.js +122 -0
  33. package/dist/functions/bar/index.d.ts +8 -0
  34. package/dist/functions/bar/index.js +62 -25
  35. package/dist/functions/beam/auto.d.ts +1 -1
  36. package/dist/functions/beam/auto.js +33 -16
  37. package/dist/functions/beam/index.js +10 -6
  38. package/dist/functions/beam/layout.d.ts +4 -3
  39. package/dist/functions/beam/layout.js +43 -65
  40. package/dist/functions/box/index.js +37 -36
  41. package/dist/functions/br/index.js +10 -8
  42. package/dist/functions/dash/index.js +40 -7
  43. package/dist/functions/default.js +19 -8
  44. package/dist/functions/div/index.d.ts +16 -0
  45. package/dist/functions/div/index.js +45 -17
  46. package/dist/functions/dot/index.js +23 -25
  47. package/dist/functions/dyn/index.d.ts +2 -0
  48. package/dist/functions/dyn/index.js +242 -0
  49. package/dist/functions/grace/index.d.ts +8 -11
  50. package/dist/functions/grace/index.js +84 -79
  51. package/dist/functions/head/index.d.ts +2 -0
  52. package/dist/functions/head/index.js +496 -0
  53. package/dist/functions/key/index.js +98 -3
  54. package/dist/functions/meter/index.d.ts +2 -0
  55. package/dist/functions/meter/index.js +155 -0
  56. package/dist/functions/note/accidentals.d.ts +24 -3
  57. package/dist/functions/note/accidentals.js +31 -1
  58. package/dist/functions/note/index.js +106 -79
  59. package/dist/functions/note/noteNameFSM.js +52 -63
  60. package/dist/functions/page/index.d.ts +11 -2
  61. package/dist/functions/page/index.js +65 -4
  62. package/dist/functions/program/index.d.ts +2 -0
  63. package/dist/functions/program/index.js +30 -0
  64. package/dist/functions/set/index.js +43 -32
  65. package/dist/functions/stack/index.js +26 -22
  66. package/dist/functions/symbol/index.d.ts +31 -0
  67. package/dist/functions/symbol/index.js +139 -0
  68. package/dist/functions/symbol/ornament.d.ts +15 -0
  69. package/dist/functions/symbol/ornament.js +73 -0
  70. package/dist/functions/symbol/shape.d.ts +24 -0
  71. package/dist/functions/symbol/shape.js +52 -0
  72. package/dist/functions/symbol/symbols/accent.d.ts +2 -0
  73. package/dist/functions/symbol/symbols/accent.js +24 -0
  74. package/dist/functions/symbol/symbols/dc.d.ts +4 -0
  75. package/dist/functions/symbol/symbols/dc.js +82 -0
  76. package/dist/functions/symbol/symbols/ds.d.ts +2 -0
  77. package/dist/functions/symbol/symbols/ds.js +93 -0
  78. package/dist/functions/symbol/symbols/dynamics.d.ts +3 -0
  79. package/dist/functions/symbol/symbols/dynamics.js +168 -0
  80. package/dist/functions/symbol/symbols/fermata.d.ts +2 -0
  81. package/dist/functions/symbol/symbols/fermata.js +24 -0
  82. package/dist/functions/symbol/symbols/fine.d.ts +2 -0
  83. package/dist/functions/symbol/symbols/fine.js +12 -0
  84. package/dist/functions/symbol/symbols/mordent.d.ts +2 -0
  85. package/dist/functions/symbol/symbols/mordent.js +56 -0
  86. package/dist/functions/symbol/symbols/prall.d.ts +2 -0
  87. package/dist/functions/symbol/symbols/prall.js +42 -0
  88. package/dist/functions/symbol/symbols/segno.d.ts +4 -0
  89. package/dist/functions/symbol/symbols/segno.js +82 -0
  90. package/dist/functions/symbol/symbols/tr.d.ts +2 -0
  91. package/dist/functions/symbol/symbols/tr.js +71 -0
  92. package/dist/functions/tempo/glyph.d.ts +12 -0
  93. package/dist/functions/tempo/glyph.js +30 -0
  94. package/dist/functions/tempo/index.js +57 -4
  95. package/dist/functions/temporal.d.ts +138 -0
  96. package/dist/functions/temporal.js +101 -0
  97. package/dist/functions/text/index.js +86 -27
  98. package/dist/functions/tie/index.js +136 -152
  99. package/dist/functions/tuplet/index.d.ts +2 -0
  100. package/dist/functions/tuplet/index.js +218 -0
  101. package/dist/functions/up/index.d.ts +1 -0
  102. package/dist/functions/up/index.js +258 -182
  103. package/dist/functions/voice/index.js +203 -109
  104. package/dist/functions/volta/index.d.ts +2 -0
  105. package/dist/functions/volta/index.js +230 -0
  106. package/dist/iife/core.d.ts +1 -0
  107. package/dist/iife/core.js +3 -0
  108. package/dist/iife/from-midi.d.ts +1 -0
  109. package/dist/iife/from-midi.js +15 -0
  110. package/dist/iife/from-musicxml.d.ts +1 -0
  111. package/dist/iife/from-musicxml.js +3 -0
  112. package/dist/iife/full.d.ts +1 -0
  113. package/dist/iife/full.js +3 -0
  114. package/dist/index.d.ts +3 -17
  115. package/dist/index.js +3 -17
  116. package/dist/jpfun.core.min.js +153 -0
  117. package/dist/jpfun.from-midi.min.js +9 -0
  118. package/dist/jpfun.from-musicxml.min.js +12 -0
  119. package/dist/jpfun.min.js +166 -0
  120. package/dist/layout/default.js +2 -3
  121. package/dist/layout/engine.d.ts +6 -12
  122. package/dist/layout/engine.js +115 -64
  123. package/dist/layout/model.d.ts +12 -5
  124. package/dist/layout/model.js +31 -18
  125. package/dist/layout/page.js +5 -4
  126. package/dist/layout/path.d.ts +19 -0
  127. package/dist/layout/path.js +101 -0
  128. package/dist/layout/types.d.ts +71 -20
  129. package/dist/layout/types.js +3 -1
  130. package/dist/lowering/loweringContext.d.ts +20 -20
  131. package/dist/lowering/loweringContext.js +153 -101
  132. package/dist/lowering/track.d.ts +5 -9
  133. package/dist/lowering/track.js +7 -2
  134. package/dist/lowering/types.d.ts +26 -112
  135. package/dist/lowering/types.js +1 -92
  136. package/dist/parser/parse-utils/call-utils.d.ts +0 -2
  137. package/dist/parser/parse-utils/call-utils.js +0 -6
  138. package/dist/parser/parse-utils/note-utils.d.ts +8 -0
  139. package/dist/parser/parse-utils/note-utils.js +35 -13
  140. package/dist/parser/parse-utils/string-utils.d.ts +10 -0
  141. package/dist/parser/parse-utils/string-utils.js +24 -0
  142. package/dist/parser/parserContext.d.ts +11 -9
  143. package/dist/parser/parserContext.js +65 -55
  144. package/dist/parser/preprocess.js +17 -11
  145. package/dist/pipeline.d.ts +6 -5
  146. package/dist/pipeline.js +4 -5
  147. package/dist/playback/compile.d.ts +18 -0
  148. package/dist/playback/compile.js +414 -0
  149. package/dist/playback/event.d.ts +105 -0
  150. package/dist/playback/event.js +105 -0
  151. package/dist/playback/time.d.ts +14 -0
  152. package/dist/playback/time.js +81 -0
  153. package/dist/playback/types.d.ts +129 -0
  154. package/dist/playback/types.js +3 -0
  155. package/dist/render/canvas.d.ts +4 -4
  156. package/dist/render/canvas.js +20 -5
  157. package/dist/render/index.d.ts +1 -0
  158. package/dist/render/index.js +1 -0
  159. package/dist/render/paint.d.ts +8 -0
  160. package/dist/render/paint.js +101 -0
  161. package/dist/render/svg.d.ts +3 -3
  162. package/dist/render/svg.js +10 -8
  163. package/dist/render/text.d.ts +22 -0
  164. package/dist/render/text.js +42 -0
  165. package/dist/render/types.d.ts +1 -1
  166. package/grammar.md +132 -13
  167. package/package.json +19 -6
@@ -1,9 +1,27 @@
1
1
  import { ASTNodeBase, ASTBraceNode, ASTFunctionNode, ASTTextNode } from "../ASTtypes.js";
2
2
  import { Diagnostic, ErrorDiagnostic, WarningDiagnostic } from "../../diagnostic.js";
3
- import { findRightParen, removeQuote } from "../../parser/parse-utils/call-utils.js";
3
+ import { findRightParen } from "../../parser/parse-utils/call-utils.js";
4
+ import { findClosingQuote, quote, removeQuote } from "../../parser/parse-utils/string-utils.js";
4
5
  import { ParserContext, skipSpaces } from "../../parser/parserContext.js";
5
- import { isVisualTemporalNode, TemporalNodeBase, } from "../../lowering/types.js";
6
+ import { isVisualTemporalNode, TemporalNodeBase, } from "../temporal.js";
6
7
  const WHITEPACE_RE = /\s/;
8
+ // 分词跟后面的标点
9
+ const LYRIC_OPENING_PUNCTUATION = new Set("(([[{〈《「『【〔〖〘〚‘“");
10
+ // 不发声的标点;位于歌词槽首尾时只渲染,不占位,也不参与对齐中心计算
11
+ const LYRIC_SILENT_PUNCTUATION = new Set([
12
+ ...LYRIC_OPENING_PUNCTUATION,
13
+ ...",。!?;:、,.!?;:))]]}〉》」』】〕〗〙〛’”\"'…—–",
14
+ ]);
15
+ // 分词
16
+ function splitLyricText(text) {
17
+ let start = 0;
18
+ let end = text.length;
19
+ while (start < end && LYRIC_SILENT_PUNCTUATION.has(text[start]))
20
+ start++;
21
+ while (end > start && LYRIC_SILENT_PUNCTUATION.has(text[end - 1]))
22
+ end--;
23
+ return { prefix: text.slice(0, start), body: text.slice(start, end) };
24
+ }
7
25
  /**
8
26
  * voices 块在宿主基线上局部居中:首末两条 voice 基线的中点对齐宿主轴
9
27
  *
@@ -94,44 +112,31 @@ L: ...
94
112
  }
95
113
  else {
96
114
  // 字符串收集
97
- let lystart = pos = skipSpaces(source, pos, end);
115
+ pos = skipSpaces(source, pos, end);
98
116
  if (pos >= end)
99
117
  return null; // 没有内容了 不去糖
100
- let lyend = end;
101
- const quote = source[pos];
118
+ let lyric;
102
119
  // 双引号才是字符串,单引号不是
103
- if (quote === '"') {
104
- // 有引号的 直接以引号为界切分
105
- lystart = ++pos;
106
- let escaped = false;
107
- for (; pos < end; pos++) {
108
- const ch = source[pos];
109
- if (escaped)
110
- escaped = false;
111
- else if (ch === "\\")
112
- escaped = true;
113
- else if (ch === quote)
114
- break;
115
- }
116
- if (pos >= end)
117
- throw Diagnostic.error.UnterminatedString({
118
- start: lystart - 1, end
119
- });
120
- lyend = pos++; // 跳过结尾引号
120
+ if (source[pos] === '"') {
121
+ // 未闭合就不去糖,词法层每次击键都跑,不能抛
122
+ const close = findClosingQuote(source, pos, end);
123
+ if (close < 0)
124
+ return null;
125
+ lyric = removeQuote(source.slice(pos, close + 1));
126
+ pos = close + 1;
121
127
  }
122
128
  else {
123
129
  // 没有引号的 以换行符为界切分 预处理已经跳过了转义的换行符了
124
- for (; pos < end; pos++) {
125
- if (source[pos] === '\n')
126
- break;
127
- }
128
- lyend = pos;
130
+ const from = pos;
131
+ while (pos < end && source[pos] !== '\n')
132
+ pos++;
133
+ lyric = source.slice(from, pos).trim();
129
134
  }
130
135
  const node = {
131
136
  kind: "sugar",
132
137
  data: {
133
138
  class: VoiceFunction,
134
- lyric: source.slice(lystart, lyend).trim(),
139
+ lyric,
135
140
  name,
136
141
  },
137
142
  span: { start, end: pos },
@@ -150,7 +155,7 @@ L: ...
150
155
  for (; voiceNodeAt >= 0; voiceNodeAt--) {
151
156
  const n = ctx.nodes[voiceNodeAt];
152
157
  if (n instanceof ASTTextNode) {
153
- if (!ctx.strict)
158
+ if (!ctx.variables.strict)
154
159
  continue; // 非严格模式下允许文本节点夹在N和L之间
155
160
  // ParserContext.parseGrammar 处理后不会有空白字符
156
161
  throw new ErrorDiagnostic("E_LYRICS_WITHOUT_VOICE_NOTES", `strict 模式下,语法糖 'L:' 或 'L(name)' 必须跟在 @voice 的音符之后,但在其前面发现了未知文本`, n.sourceSpan);
@@ -210,7 +215,7 @@ L: ...
210
215
  ifCombine: for (; voicesNodeAt >= 0; voicesNodeAt--) {
211
216
  const n = ctx.nodes[voicesNodeAt];
212
217
  if (n instanceof ASTTextNode) {
213
- if (ctx.strict)
218
+ if (ctx.variables.strict)
214
219
  break;
215
220
  // 如果中间有换行符,直接说明是两个独立的 voice 组件,不能合并
216
221
  for (let i = n.sourceSpan.start; i < n.sourceSpan.end; i++) {
@@ -251,6 +256,7 @@ L: ...
251
256
  content; // 声部内容
252
257
  name; // 声部名称
253
258
  size; // 声部的字体大小
259
+ font;
254
260
  lyrics;
255
261
  get children() { return [this.content]; }
256
262
  timeFlowModel() {
@@ -288,9 +294,10 @@ L: ...
288
294
  }
289
295
  constructor(span, args, ctx, parent = null) {
290
296
  super(span, parent);
291
- this.size = ctx.fontSize;
297
+ this.size = ctx.variables.fontsize;
292
298
  [this.content, this.name] = this.getArgValue(args, ctx);
293
299
  this.content.parent = this;
300
+ this.font = ctx.variables.font;
294
301
  args.delete(0);
295
302
  args.delete("name");
296
303
  args.delete(1);
@@ -300,9 +307,10 @@ L: ...
300
307
  this.addLyric(key, value);
301
308
  continue;
302
309
  }
303
- const v = ctx.parseArgWithType(value.start, value.end, "string", span.start);
310
+ const arg = value;
311
+ const v = ctx.parseArgWithType(arg.valueSpan, "string", span.start);
304
312
  if (v === null) {
305
- ctx.diagnostics.push(new WarningDiagnostic("W_VOICE_INVALID_LYRIC", `@voice 的歌词参数值解析失败, 参数[${key}]将被忽略`, value));
313
+ ctx.diagnostics.push(new WarningDiagnostic("W_VOICE_INVALID_LYRIC", `@voice 的歌词参数值解析失败, 参数[${key}]将被忽略`, arg.valueSpan));
306
314
  }
307
315
  else
308
316
  this.addLyric(key, v);
@@ -324,52 +332,101 @@ L: ...
324
332
  }
325
333
  }
326
334
  }
335
+ /**
336
+ * 把歌词拆成与音符一一对应的槽位。
337
+ * 例如 `hello world` 拆成 `hello`、`world`,`hel-lo` 拆成 `hel-`、`lo`;
338
+ * `你好吗` 默认逐字拆分,`你 @ 好` 中的 `@` 留出一个空槽,`{你好} 啊` 则把 `你好` 放在同一个槽位。
339
+ * 转义符:hel\-lo \@ {你\}好} 会得到 ["hel-lo", "@", "你}好"],而不是把 -、@、} 当控制符
340
+ */
327
341
  static parseLyric(value) {
328
342
  const result = [];
329
- let lastPos = 0;
343
+ let token = "";
344
+ let prefix = "";
345
+ const pushSlot = (text) => {
346
+ result.push(prefix + text);
347
+ prefix = "";
348
+ };
349
+ const pushToken = () => {
350
+ if (token)
351
+ pushSlot(token);
352
+ token = "";
353
+ };
330
354
  for (let i = 0; i < value.length; i++) {
331
355
  const ch = value[i];
332
- if (WHITEPACE_RE.test(ch)) {
333
- if (i > lastPos)
334
- result.push(value.slice(lastPos, i));
335
- lastPos = i + 1;
356
+ if (ch === "\\" && i + 1 < value.length && /[\\{}@-]/.test(value[i + 1])) {
357
+ token += value[++i];
336
358
  }
337
- else if (ch === "-") {
338
- // 如果前面有内容 则把-放到前一个词里
339
- if (i > lastPos) {
340
- result.push(value.slice(lastPos, i + 1));
341
- lastPos = i + 1;
359
+ else if (ch === "{") {
360
+ let close = i + 1;
361
+ let grouped = "";
362
+ for (; close < value.length && value[close] !== "}"; close++) {
363
+ if (value[close] === "\\" && close + 1 < value.length && /[\\{}@-]/.test(value[close + 1])) {
364
+ grouped += value[++close];
365
+ }
366
+ else
367
+ grouped += value[close];
368
+ }
369
+ if (close < value.length) {
370
+ pushToken();
371
+ pushSlot(grouped);
372
+ i = close;
342
373
  }
343
374
  else
344
- lastPos = i; // 把-放到下一个词里
375
+ token += ch;
376
+ }
377
+ else if (WHITEPACE_RE.test(ch)) {
378
+ pushToken();
379
+ }
380
+ else if (ch === "-") {
381
+ // 如果前面有内容 则把-放到前一个词里
382
+ token += ch;
383
+ if (token.length > 1)
384
+ pushToken();
345
385
  }
346
386
  else if (ch === "@") {
347
- if (i > lastPos)
348
- result.push(value.slice(lastPos, i + 1));
387
+ if (token) {
388
+ token += ch;
389
+ pushToken();
390
+ }
349
391
  result.push('');
350
- lastPos = i + 1;
392
+ }
393
+ else if (LYRIC_OPENING_PUNCTUATION.has(ch)) {
394
+ pushToken();
395
+ prefix += ch;
396
+ }
397
+ else if (LYRIC_SILENT_PUNCTUATION.has(ch)) {
398
+ if (token)
399
+ token += ch;
400
+ else if (prefix || !result.at(-1))
401
+ prefix += ch;
402
+ else
403
+ result[result.length - 1] += ch;
351
404
  }
352
405
  else if (ch.charCodeAt(0) > 0x7F) {
353
406
  // 遇到中文等非ASCII字符 直接切分成单个字符
354
- if (i > lastPos)
355
- result.push(value.slice(lastPos, i));
356
- result.push(ch);
357
- lastPos = i + 1;
407
+ pushToken();
408
+ pushSlot(ch);
358
409
  }
410
+ else
411
+ token += ch;
412
+ }
413
+ pushToken();
414
+ if (prefix) {
415
+ if (result.at(-1))
416
+ result[result.length - 1] += prefix;
417
+ else
418
+ result.push(prefix);
359
419
  }
360
- if (lastPos < value.length)
361
- result.push(value.slice(lastPos));
362
420
  return result;
363
421
  }
364
422
  toString(source) {
365
- const notes = this.content.toString(source);
366
- const lyricStrs = this.lyrics.map(lyric => {
367
- let lyricstr = lyric.tokens.map(token => token.length === 0 ? "@" : token).join(" ");
368
- if (lyricstr.includes(",") || lyricstr.includes("\n"))
369
- lyricstr = `"${lyricstr.replace(/"/g, '\\"')}"`;
370
- return `${lyric.name ? `${lyric.name}=${lyricstr}` : lyricstr}`;
371
- });
372
- return `@voice(\n\t${notes},${this.name},\n\t${lyricStrs.join(",\n\t")}\n)`;
423
+ const parameters = [`${this.content.toString(source)},${quote(this.name)}`];
424
+ for (const lyric of this.lyrics) {
425
+ const text = quote(lyric.tokens.map(token => token.length === 0
426
+ ? "@" : `{${token.replace(/[\\{}@-]/g, "\\$&")}}`).join(" "));
427
+ parameters.push(lyric.name ? `${lyric.name}=${text}` : text);
428
+ }
429
+ return `@voice(\n\t${parameters.join(",\n\t")}\n)`;
373
430
  }
374
431
  }
375
432
  class VoicesFunction extends ASTFunctionNode {
@@ -429,9 +486,9 @@ L: la la la
429
486
  constructor(span, args, ctx, parent = null) {
430
487
  super(span, parent);
431
488
  this.voices = [];
432
- this.size = ctx.fontSize;
433
- this.braceSpace = ctx.fontSize;
434
- this.measure = makeVoicesMeasure(ctx.fontSize);
489
+ this.size = ctx.variables.fontsize;
490
+ this.braceSpace = ctx.variables.fontsize;
491
+ this.measure = makeVoicesMeasure(ctx.variables.fontsize);
435
492
  for (const [, value] of args) {
436
493
  if (value instanceof ASTNodeBase) {
437
494
  if (value instanceof VoiceFunction)
@@ -441,12 +498,12 @@ L: la la la
441
498
  }
442
499
  continue;
443
500
  }
444
- // 是用 SourceSpan 体现的原始参数
445
- const v = ctx.parseArgWithType(value.start, value.end, "content", span.start);
501
+ const arg = value;
502
+ const v = ctx.parseArgWithType(arg.valueSpan, "content", span.start);
446
503
  if (v instanceof VoiceFunction)
447
504
  this.addVoice(v);
448
505
  else {
449
- throw new ErrorDiagnostic("E_VOICES_INVALID_CHILD", `@voices 的参数必须是 @voice 函数,但发现了其他类型 ${v?.constructor.name}`, value);
506
+ throw new ErrorDiagnostic("E_VOICES_INVALID_CHILD", `@voices 的参数必须是 @voice 函数,但发现了其他类型 ${v?.constructor.name}`, arg.valueSpan);
450
507
  }
451
508
  }
452
509
  if (this.voices.length === 0) {
@@ -462,7 +519,7 @@ L: la la la
462
519
  toString(source) {
463
520
  const voicelines = Array(this.voices.length);
464
521
  for (let i = 0; i < this.voices.length; i++) {
465
- const part = this.voices[i].toString(source).replace(/^/gm, "\t");
522
+ const part = `\t${this.voices[i].toString(source)}`;
466
523
  voicelines[i] = part;
467
524
  }
468
525
  return `@voices(\n${voicelines.join(",\n")}\n)`;
@@ -475,8 +532,9 @@ const LYRIC_SIZE_RATIO = 0.82;
475
532
  /** 无大括号时标签右侧的空隙,相对声部字号 */
476
533
  const NAME_GAP_RATIO = 0.6;
477
534
  /** 歌词行名称的宽度决定标签列宽度,因此声部名事件和歌词附件必须用同一个样式测量 */
478
- function lyricNameStyle(size) {
535
+ function lyricNameStyle(size, fontFamily) {
479
536
  return {
537
+ fontFamily,
480
538
  fontSize: size * LYRIC_SIZE_RATIO,
481
539
  fill: "#000",
482
540
  fontWeight: 600,
@@ -490,7 +548,6 @@ class VoiceNameTemporal extends TemporalNodeBase {
490
548
  super();
491
549
  this.ast = ast;
492
550
  this.braceSpace = braceSpace;
493
- this.T = 0;
494
551
  this.mergeKey = -2;
495
552
  // 既没有名称、也不需要括号空间和标签列时只保留不可见的占位事件:
496
553
  // 它不进入可见对象,也不让空声部失去默认槽位高度
@@ -506,7 +563,7 @@ class VoiceNameTemporal extends TemporalNodeBase {
506
563
  for (const lyric of this.ast.lyrics) {
507
564
  if (!lyric.name)
508
565
  continue;
509
- const lyricMetrics = context.textMeasurer.measureText(lyric.name, lyricNameStyle(this.ast.size));
566
+ const lyricMetrics = context.textMeasurer.measureText(lyric.name, lyricNameStyle(this.ast.size, this.ast.font));
510
567
  labelWidth = Math.max(labelWidth, lyricMetrics.w);
511
568
  }
512
569
  // 没有大括号时也要在标签右侧留出与内容分离的空隙
@@ -525,6 +582,7 @@ class VoiceNameTemporal extends TemporalNodeBase {
525
582
  get style() {
526
583
  return {
527
584
  fontSize: this.ast.size * 0.85,
585
+ fontFamily: this.ast.font,
528
586
  fill: "#000",
529
587
  fontWeight: 600,
530
588
  textAlign: "right",
@@ -566,34 +624,30 @@ const BRACE_HOOK_COMMANDS = [
566
624
  * 因此不需要反查任何轨道信息。
567
625
  */
568
626
  class VoicesBraceAttachment {
569
- box = { x: 0, y: 0, w: 0, h: 0 };
570
627
  layer = "background";
571
628
  names;
572
629
  ast;
573
- bars = [];
574
- hooks = [];
630
+ get sourceSpan() { return this.ast.sourceSpan; }
575
631
  constructor(names, ast) {
576
632
  this.names = names;
577
633
  this.ast = ast;
578
634
  }
579
- layout() {
580
- this.bars = [];
581
- this.hooks = [];
635
+ createGeometry() {
582
636
  if (this.names.length < 2)
583
- return [];
637
+ return { regions: [], paint() { } };
584
638
  const first = this.names[0].box;
585
639
  const last = this.names[this.names.length - 1].box;
586
640
  const em = this.ast.size;
587
641
  const top = first.y + first.visualAxis - em * 0.5;
588
642
  const bottom = last.y + last.visualAxis + em * 0.5;
589
643
  if (bottom - top < 1e-6)
590
- return [];
644
+ return { regions: [], paint() { } };
591
645
  // 各部分尺寸都以粗竖线宽度为单位,比例取自常见简谱软件的括线
592
646
  const stem = Math.max(2.5, em * 0.19);
593
647
  const reach = stem * BRACE_HOOK_REACH;
594
648
  const drop = stem * BRACE_HOOK_DROP;
595
649
  const x = first.x + first.anchor + this.ast.braceSpace * 0.3 + stem / 2;
596
- this.bars = [
650
+ const bars = [
597
651
  { x: x - stem / 2, y: top, w: stem, h: bottom - top },
598
652
  {
599
653
  x: x + stem * 1.33 - stem * 0.165,
@@ -602,57 +656,93 @@ class VoicesBraceAttachment {
602
656
  h: bottom - top - stem * 1.34,
603
657
  },
604
658
  ];
605
- this.hooks = [
659
+ const hooks = [
606
660
  { x, y: top, scale: stem, dir: -1 },
607
661
  { x, y: bottom, scale: stem, dir: 1 },
608
662
  ];
609
663
  // 括线画在声部名左侧的空白里,只参与画布边界,不抢任何轨道的纵向空间
610
- return [{
664
+ const regions = [{
611
665
  x: x - stem / 2,
612
666
  y: top - drop,
613
667
  w: reach + stem / 2,
614
668
  h: bottom - top + drop * 2,
615
669
  }];
616
- }
617
- paint(painter) {
618
- for (const bar of this.bars) {
619
- painter.drawRect(bar.x, bar.y, bar.w, bar.h, { fill: "#000" });
620
- }
621
- for (const hook of this.hooks) {
622
- painter.drawPath(BRACE_HOOK_COMMANDS, { fill: "#000" }, { x: hook.x, y: hook.y, scaleX: hook.scale, scaleY: hook.scale * hook.dir });
623
- }
670
+ return {
671
+ regions,
672
+ paint(painter) {
673
+ for (const bar of bars) {
674
+ painter.drawRect(bar.x, bar.y, bar.w, bar.h, { fill: "#000" });
675
+ }
676
+ for (const hook of hooks) {
677
+ painter.drawPath(BRACE_HOOK_COMMANDS, { fill: "#000" }, { x: hook.x, y: hook.y, scaleX: hook.scale, scaleY: hook.scale * hook.dir });
678
+ }
679
+ },
680
+ };
624
681
  }
625
682
  }
626
683
  class VoiceLyricsAttachment {
627
- box = { x: 0, y: 0, w: 0, h: 0 };
628
684
  layer = "foreground";
629
685
  /** lowering 会持续向这个数组加入 voice 内容产生的 temporal */
630
686
  temporalMembers;
631
687
  /** 同一个 voice 的声部名事件,歌词行名称向它的对齐点右对齐 */
632
688
  nameHost;
633
- preparedText = [];
689
+ get sourceSpan() { return this.nameHost.ast.sourceSpan; }
634
690
  constructor(temporalMembers, nameHost) {
635
691
  this.temporalMembers = temporalMembers;
636
692
  this.nameHost = nameHost;
637
693
  }
638
- layout(context) {
639
- this.updateGeometry(context);
640
- // 同一行同一轨的多行歌词由引擎合并成一段占用
641
- return this.preparedText;
642
- }
643
- paint(painter) {
644
- for (const item of this.preparedText) {
645
- painter.drawText(item.text, item.x, item.y + item.textBaselineY, item.style);
694
+ prepareHorizontal(lines, context) {
695
+ const targets = this.temporalMembers
696
+ .filter(isVisualTemporalNode)
697
+ .filter(node => node.ports?.["lyric"]);
698
+ const { lyrics, size } = this.nameHost.ast;
699
+ if (targets.length === 0 || lyrics.length === 0)
700
+ return;
701
+ const style = { fontSize: size * LYRIC_SIZE_RATIO, fontFamily: this.nameHost.ast.font, fill: "#000" };
702
+ for (let i = 0; i < targets.length; i++) {
703
+ const target = targets[i];
704
+ let halfWidth = 0;
705
+ for (const lyric of lyrics) {
706
+ const text = lyric.tokens[i];
707
+ if (text) {
708
+ const { body } = splitLyricText(text);
709
+ if (body)
710
+ halfWidth = Math.max(halfWidth, context.textMeasurer.measureText(body, style).w / 2);
711
+ }
712
+ }
713
+ if (halfWidth === 0)
714
+ continue;
715
+ // 只扩大求解占用;视觉盒和端口不动,音符内部几何因此保持原位。
716
+ lines[target.layoutLine]?.registerHorizontalLayoutHook(target, target, ({ columns, start }) => {
717
+ const element = columns[start].find(item => item.time === target);
718
+ if (!element)
719
+ return;
720
+ const lyricOffset = target.ports["lyric"].x - target.box.anchor;
721
+ element.WL = Math.max(element.WL, halfWidth - lyricOffset);
722
+ element.WR = Math.max(element.WR, halfWidth + lyricOffset);
723
+ });
646
724
  }
647
725
  }
648
- updateGeometry(context) {
726
+ createGeometry(context) {
727
+ const preparedText = this.prepareGeometry(context);
728
+ // 同一行同一轨的多行歌词由引擎合并成一段占用
729
+ return {
730
+ regions: preparedText,
731
+ paint(painter) {
732
+ for (const item of preparedText) {
733
+ painter.drawText(item.text, item.x, item.y + item.textBaselineY, item.style);
734
+ }
735
+ },
736
+ };
737
+ }
738
+ prepareGeometry(context) {
649
739
  const { lyrics, size } = this.nameHost.ast;
650
- this.preparedText.length = 0;
740
+ const preparedText = [];
651
741
  const targets = this.temporalMembers
652
742
  .filter(isVisualTemporalNode)
653
743
  .filter(node => node.ports?.["lyric"]);
654
744
  if (targets.length === 0 || lyrics.length === 0)
655
- return;
745
+ return preparedText;
656
746
  // 每个 system+track 的歌词共用一条基线
657
747
  // 装饰高度先汇总为下边界,不进入单个 token 的 y 计算
658
748
  const contentBottom = new Map();
@@ -666,8 +756,8 @@ class VoiceLyricsAttachment {
666
756
  const fontSize = size * LYRIC_SIZE_RATIO;
667
757
  const rowGap = size * 0.24;
668
758
  const firstRowGap = size * 0.32;
669
- const lyricStyle = { fontSize, fill: "#000" };
670
- const nameStyle = lyricNameStyle(size);
759
+ const lyricStyle = { fontSize, fontFamily: this.nameHost.ast.font, fill: "#000" };
760
+ const nameStyle = lyricNameStyle(size, this.nameHost.ast.font);
671
761
  const baselineOffset = context.textMeasurer.measureText("M", lyricStyle).baseline + firstRowGap;
672
762
  const baselineOf = (bottom, row) => bottom + baselineOffset + row * (fontSize + rowGap);
673
763
  // 歌词行名称与声部名共用左侧那一列,因此只出现在声部名所在的那一行
@@ -682,11 +772,14 @@ class VoiceLyricsAttachment {
682
772
  if (!text || bottom === undefined)
683
773
  continue;
684
774
  const metrics = context.textMeasurer.measureText(text, lyricStyle);
685
- this.preparedText.push({
775
+ const { prefix, body } = splitLyricText(text);
776
+ const bodyWidth = body ? context.textMeasurer.measureText(body, lyricStyle).w : 0;
777
+ const prefixWidth = prefix ? context.textMeasurer.measureText(prefix, lyricStyle).w : 0;
778
+ preparedText.push({
686
779
  text,
687
780
  style: lyricStyle,
688
781
  textBaselineY: metrics.baseline,
689
- x: target.box.x + target.ports["lyric"].x - metrics.w / 2,
782
+ x: target.box.x + target.ports["lyric"].x - bodyWidth / 2 - prefixWidth,
690
783
  y: baselineOf(bottom, row) - metrics.baseline,
691
784
  w: metrics.w,
692
785
  h: metrics.h,
@@ -697,7 +790,7 @@ class VoiceLyricsAttachment {
697
790
  if (!lyric.name || !labelBox || labelBottom === undefined)
698
791
  continue;
699
792
  const metrics = context.textMeasurer.measureText(lyric.name, nameStyle);
700
- this.preparedText.push({
793
+ preparedText.push({
701
794
  text: lyric.name,
702
795
  style: nameStyle,
703
796
  textBaselineY: metrics.baseline,
@@ -709,5 +802,6 @@ class VoiceLyricsAttachment {
709
802
  track: labelTrack,
710
803
  });
711
804
  }
805
+ return preparedText;
712
806
  }
713
807
  }
@@ -0,0 +1,2 @@
1
+ import { type ASTFunctionClass } from "../ASTtypes.js";
2
+ export declare const VoltaNode: ASTFunctionClass;