jpfun 0.3.0 → 0.4.1

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 (129) hide show
  1. package/README.md +73 -1
  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/functions/ASTtypes.d.ts +30 -26
  23. package/dist/functions/ASTtypes.js +25 -23
  24. package/dist/functions/adjust/index.js +13 -6
  25. package/dist/functions/arpeggio/index.d.ts +2 -0
  26. package/dist/functions/arpeggio/index.js +184 -0
  27. package/dist/functions/arpeggio/shape.d.ts +7 -0
  28. package/dist/functions/arpeggio/shape.js +122 -0
  29. package/dist/functions/bar/index.js +30 -23
  30. package/dist/functions/beam/auto.d.ts +1 -1
  31. package/dist/functions/beam/auto.js +1 -1
  32. package/dist/functions/beam/index.js +6 -1
  33. package/dist/functions/beam/layout.d.ts +3 -3
  34. package/dist/functions/beam/layout.js +5 -3
  35. package/dist/functions/box/index.js +10 -3
  36. package/dist/functions/br/index.js +10 -6
  37. package/dist/functions/dash/index.js +12 -9
  38. package/dist/functions/default.js +6 -2
  39. package/dist/functions/div/index.d.ts +4 -1
  40. package/dist/functions/div/index.js +8 -7
  41. package/dist/functions/dot/index.js +18 -25
  42. package/dist/functions/dyn/index.d.ts +2 -0
  43. package/dist/functions/dyn/index.js +242 -0
  44. package/dist/functions/grace/index.d.ts +1 -1
  45. package/dist/functions/grace/index.js +17 -11
  46. package/dist/functions/head/index.js +168 -159
  47. package/dist/functions/key/index.js +12 -7
  48. package/dist/functions/meter/index.js +14 -6
  49. package/dist/functions/note/index.js +73 -31
  50. package/dist/functions/note/noteNameFSM.js +52 -63
  51. package/dist/functions/page/index.d.ts +5 -1
  52. package/dist/functions/page/index.js +38 -16
  53. package/dist/functions/program/index.d.ts +2 -0
  54. package/dist/functions/program/index.js +39 -0
  55. package/dist/functions/set/index.js +13 -8
  56. package/dist/functions/stack/index.js +7 -5
  57. package/dist/functions/symbol/index.d.ts +13 -3
  58. package/dist/functions/symbol/index.js +71 -22
  59. package/dist/functions/symbol/shape.d.ts +2 -0
  60. package/dist/functions/symbol/shape.js +14 -10
  61. package/dist/functions/symbol/symbols/dc.d.ts +4 -0
  62. package/dist/functions/symbol/symbols/dc.js +82 -0
  63. package/dist/functions/symbol/symbols/{f.d.ts → ds.d.ts} +1 -1
  64. package/dist/functions/symbol/symbols/ds.js +93 -0
  65. package/dist/functions/symbol/symbols/dynamics.d.ts +3 -0
  66. package/dist/functions/symbol/symbols/dynamics.js +168 -0
  67. package/dist/functions/symbol/symbols/{p.d.ts → fine.d.ts} +1 -1
  68. package/dist/functions/symbol/symbols/fine.js +12 -0
  69. package/dist/functions/symbol/symbols/mordent.js +44 -31
  70. package/dist/functions/symbol/symbols/prall.d.ts +2 -0
  71. package/dist/functions/symbol/symbols/prall.js +42 -0
  72. package/dist/functions/symbol/symbols/segno.d.ts +4 -0
  73. package/dist/functions/symbol/symbols/segno.js +82 -0
  74. package/dist/functions/symbol/symbols/tr.js +0 -4
  75. package/dist/functions/tempo/index.js +12 -4
  76. package/dist/functions/temporal.d.ts +138 -0
  77. package/dist/functions/temporal.js +101 -0
  78. package/dist/functions/text/index.js +25 -10
  79. package/dist/functions/tie/index.js +45 -30
  80. package/dist/functions/tuplet/index.js +21 -8
  81. package/dist/functions/up/index.js +38 -33
  82. package/dist/functions/voice/index.js +153 -68
  83. package/dist/functions/volta/index.js +62 -55
  84. package/dist/iife/core.d.ts +1 -0
  85. package/dist/iife/core.js +3 -0
  86. package/dist/iife/from-midi.d.ts +1 -0
  87. package/dist/iife/from-midi.js +15 -0
  88. package/dist/iife/from-musicxml.d.ts +1 -0
  89. package/dist/iife/from-musicxml.js +3 -0
  90. package/dist/iife/full.d.ts +1 -0
  91. package/dist/iife/full.js +3 -0
  92. package/dist/index.d.ts +3 -21
  93. package/dist/index.js +3 -21
  94. package/dist/jpfun.core.min.js +119 -0
  95. package/dist/jpfun.from-midi.min.js +9 -0
  96. package/dist/jpfun.from-musicxml.min.js +12 -0
  97. package/dist/jpfun.min.js +111 -122
  98. package/dist/layout/default.js +2 -3
  99. package/dist/layout/engine.d.ts +2 -1
  100. package/dist/layout/engine.js +71 -26
  101. package/dist/layout/model.d.ts +4 -4
  102. package/dist/layout/model.js +15 -12
  103. package/dist/layout/page.js +5 -4
  104. package/dist/layout/path.js +3 -3
  105. package/dist/layout/types.d.ts +20 -2
  106. package/dist/lowering/loweringContext.d.ts +3 -1
  107. package/dist/lowering/loweringContext.js +28 -14
  108. package/dist/lowering/track.d.ts +4 -8
  109. package/dist/lowering/track.js +6 -1
  110. package/dist/lowering/types.d.ts +5 -128
  111. package/dist/lowering/types.js +1 -98
  112. package/dist/parser/parse-utils/string-utils.d.ts +2 -0
  113. package/dist/parser/parse-utils/string-utils.js +4 -0
  114. package/dist/parser/parserContext.d.ts +7 -8
  115. package/dist/parser/parserContext.js +41 -36
  116. package/dist/pipeline.d.ts +6 -5
  117. package/dist/pipeline.js +4 -5
  118. package/dist/playback/compile.d.ts +1 -1
  119. package/dist/playback/compile.js +15 -2
  120. package/dist/playback/event.d.ts +17 -3
  121. package/dist/playback/event.js +30 -12
  122. package/dist/playback/time.js +1 -1
  123. package/dist/playback/types.d.ts +5 -4
  124. package/dist/render/text.d.ts +6 -1
  125. package/dist/render/text.js +8 -1
  126. package/grammar.md +111 -57
  127. package/package.json +11 -2
  128. package/dist/functions/symbol/symbols/f.js +0 -55
  129. package/dist/functions/symbol/symbols/p.js +0 -61
@@ -1,10 +1,27 @@
1
1
  import { ASTNodeBase, ASTBraceNode, ASTFunctionNode, ASTTextNode } from "../ASTtypes.js";
2
2
  import { Diagnostic, ErrorDiagnostic, WarningDiagnostic } from "../../diagnostic.js";
3
3
  import { findRightParen } from "../../parser/parse-utils/call-utils.js";
4
- import { findClosingQuote, removeQuote } from "../../parser/parse-utils/string-utils.js";
4
+ import { findClosingQuote, quote, removeQuote } from "../../parser/parse-utils/string-utils.js";
5
5
  import { ParserContext, skipSpaces } from "../../parser/parserContext.js";
6
- import { isVisualTemporalNode, TemporalNodeBase, } from "../../lowering/types.js";
6
+ import { isVisualTemporalNode, TemporalNodeBase, } from "../temporal.js";
7
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
+ }
8
25
  /**
9
26
  * voices 块在宿主基线上局部居中:首末两条 voice 基线的中点对齐宿主轴
10
27
  *
@@ -34,30 +51,32 @@ class VoiceFunction extends ASTFunctionNode {
34
51
  static def = {
35
52
  name: ["voice", "v"],
36
53
  description: "声部",
37
- example: `@voice({}, name, 歌词1名=歌词1, 歌词2名=歌词2, ...)
38
- 第一个参数为音符内容,第二个参数为写在最左侧的声部名称(可不填);
39
- 命名参数为歌词左侧的名称和内容,可以任意多个。也可以是位置参数,表示不需要名称。
40
- 例:
41
- @voice({C1 D1 E1}, 钢琴, 男=ha ha ha, 女=la la la)
42
- @voice({C1 D1 E1}, , ha ha ha, 女="la la la") 表示音符和第一个歌词都没有名称
43
- 歌词可以被引号包裹,如果包含逗号等歧义字符一定需要引号
44
- 语法糖:
45
- N(name): C1 D1 E1
46
- L(name): ha ha ha...
47
- L: ...
48
- 前面可以有任意空白,但最后换行表示结束。歌词此时允许有逗号而不加引号
49
- 如果一定要换行,最后加上'\\'
50
- 英文歌词用空格和连字符'-'分词,用'@'占位
51
- `,
54
+ details: `\
55
+ ~~~jpfun
56
+ @voice({1 2 3}, 钢琴, 男=ha ha ha, 女=la la la)
57
+ ~~~
58
+ - **额外参数**:每项为一行歌词;命名参数同时给出歌词行名,位置参数表示无行名。省略声部名但仍需歌词时,保留空位,如 \`@voice({1 2 3}, , ha ha ha)\`。
59
+
60
+ 歌词可用引号包裹;显式调用中含逗号等歧义字符时必须加引号。英文用空格或连字符 \`-\` 分词,用 \`@\` 占位。
61
+
62
+ **简写**:\`N\` 声明音符行,\`L\` 声明歌词行,括号内的名称可省略:
63
+ ~~~jpfun
64
+ N(钢琴): 1 2 3
65
+ L(男): ha ha ha
66
+ L: la la la
67
+ ~~~
68
+ 行首允许空白,换行结束当前声明;在行末加 \`\\\` 可续行。这种写法中的歌词允许直接使用逗号。`,
52
69
  allowExtraArgs: true,
53
70
  extraArgType: "string",
54
71
  args: [
55
72
  {
56
73
  type: "content",
74
+ description: "声部的音符内容",
57
75
  default: null,
58
76
  },
59
77
  {
60
78
  name: "name",
79
+ description: "显示在左侧的声部名称,空值隐藏名称",
61
80
  type: "string",
62
81
  default: "",
63
82
  },
@@ -138,7 +157,7 @@ L: ...
138
157
  for (; voiceNodeAt >= 0; voiceNodeAt--) {
139
158
  const n = ctx.nodes[voiceNodeAt];
140
159
  if (n instanceof ASTTextNode) {
141
- if (!ctx.strict)
160
+ if (!ctx.variables.strict)
142
161
  continue; // 非严格模式下允许文本节点夹在N和L之间
143
162
  // ParserContext.parseGrammar 处理后不会有空白字符
144
163
  throw new ErrorDiagnostic("E_LYRICS_WITHOUT_VOICE_NOTES", `strict 模式下,语法糖 'L:' 或 'L(name)' 必须跟在 @voice 的音符之后,但在其前面发现了未知文本`, n.sourceSpan);
@@ -198,7 +217,7 @@ L: ...
198
217
  ifCombine: for (; voicesNodeAt >= 0; voicesNodeAt--) {
199
218
  const n = ctx.nodes[voicesNodeAt];
200
219
  if (n instanceof ASTTextNode) {
201
- if (ctx.strict)
220
+ if (ctx.variables.strict)
202
221
  break;
203
222
  // 如果中间有换行符,直接说明是两个独立的 voice 组件,不能合并
204
223
  for (let i = n.sourceSpan.start; i < n.sourceSpan.end; i++) {
@@ -239,6 +258,7 @@ L: ...
239
258
  content; // 声部内容
240
259
  name; // 声部名称
241
260
  size; // 声部的字体大小
261
+ font;
242
262
  lyrics;
243
263
  get children() { return [this.content]; }
244
264
  timeFlowModel() {
@@ -276,9 +296,10 @@ L: ...
276
296
  }
277
297
  constructor(span, args, ctx, parent = null) {
278
298
  super(span, parent);
279
- this.size = ctx.fontSize;
299
+ this.size = ctx.variables.fontsize;
280
300
  [this.content, this.name] = this.getArgValue(args, ctx);
281
301
  this.content.parent = this;
302
+ this.font = ctx.variables.font;
282
303
  args.delete(0);
283
304
  args.delete("name");
284
305
  args.delete(1);
@@ -313,69 +334,125 @@ L: ...
313
334
  }
314
335
  }
315
336
  }
337
+ /**
338
+ * 把歌词拆成与音符一一对应的槽位。
339
+ * 例如 `hello world` 拆成 `hello`、`world`,`hel-lo` 拆成 `hel-`、`lo`;
340
+ * `你好吗` 默认逐字拆分,`你 @ 好` 中的 `@` 留出一个空槽,`{你好} 啊` 则把 `你好` 放在同一个槽位。
341
+ * 转义符:hel\-lo \@ {你\}好} 会得到 ["hel-lo", "@", "你}好"],而不是把 -、@、} 当控制符
342
+ */
316
343
  static parseLyric(value) {
317
344
  const result = [];
318
- let lastPos = 0;
345
+ let token = "";
346
+ let prefix = "";
347
+ const pushSlot = (text) => {
348
+ result.push(prefix + text);
349
+ prefix = "";
350
+ };
351
+ const pushToken = () => {
352
+ if (token)
353
+ pushSlot(token);
354
+ token = "";
355
+ };
319
356
  for (let i = 0; i < value.length; i++) {
320
357
  const ch = value[i];
321
- if (WHITEPACE_RE.test(ch)) {
322
- if (i > lastPos)
323
- result.push(value.slice(lastPos, i));
324
- lastPos = i + 1;
358
+ if (ch === "\\" && i + 1 < value.length && /[\\{}@-]/.test(value[i + 1])) {
359
+ token += value[++i];
325
360
  }
326
- else if (ch === "-") {
327
- // 如果前面有内容 则把-放到前一个词里
328
- if (i > lastPos) {
329
- result.push(value.slice(lastPos, i + 1));
330
- lastPos = i + 1;
361
+ else if (ch === "{") {
362
+ let close = i + 1;
363
+ let grouped = "";
364
+ for (; close < value.length && value[close] !== "}"; close++) {
365
+ if (value[close] === "\\" && close + 1 < value.length && /[\\{}@-]/.test(value[close + 1])) {
366
+ grouped += value[++close];
367
+ }
368
+ else
369
+ grouped += value[close];
370
+ }
371
+ if (close < value.length) {
372
+ pushToken();
373
+ pushSlot(grouped);
374
+ i = close;
331
375
  }
332
376
  else
333
- lastPos = i; // 把-放到下一个词里
377
+ token += ch;
378
+ }
379
+ else if (WHITEPACE_RE.test(ch)) {
380
+ pushToken();
381
+ }
382
+ else if (ch === "-") {
383
+ // 如果前面有内容 则把-放到前一个词里
384
+ token += ch;
385
+ if (token.length > 1)
386
+ pushToken();
334
387
  }
335
388
  else if (ch === "@") {
336
- if (i > lastPos)
337
- result.push(value.slice(lastPos, i + 1));
389
+ if (token) {
390
+ token += ch;
391
+ pushToken();
392
+ }
338
393
  result.push('');
339
- lastPos = i + 1;
394
+ }
395
+ else if (LYRIC_OPENING_PUNCTUATION.has(ch)) {
396
+ pushToken();
397
+ prefix += ch;
398
+ }
399
+ else if (LYRIC_SILENT_PUNCTUATION.has(ch)) {
400
+ if (token)
401
+ token += ch;
402
+ else if (prefix || !result.at(-1))
403
+ prefix += ch;
404
+ else
405
+ result[result.length - 1] += ch;
340
406
  }
341
407
  else if (ch.charCodeAt(0) > 0x7F) {
342
408
  // 遇到中文等非ASCII字符 直接切分成单个字符
343
- if (i > lastPos)
344
- result.push(value.slice(lastPos, i));
345
- result.push(ch);
346
- lastPos = i + 1;
409
+ pushToken();
410
+ pushSlot(ch);
347
411
  }
412
+ else
413
+ token += ch;
414
+ }
415
+ pushToken();
416
+ if (prefix) {
417
+ if (result.at(-1))
418
+ result[result.length - 1] += prefix;
419
+ else
420
+ result.push(prefix);
348
421
  }
349
- if (lastPos < value.length)
350
- result.push(value.slice(lastPos));
351
422
  return result;
352
423
  }
353
424
  toString(source) {
354
- const notes = this.content.toString(source);
355
- const lyricStrs = this.lyrics.map(lyric => {
356
- let lyricstr = lyric.tokens.map(token => token.length === 0 ? "@" : token).join(" ");
357
- if (lyricstr.includes(",") || lyricstr.includes("\n"))
358
- lyricstr = `"${lyricstr.replace(/"/g, '\\"')}"`;
359
- return `${lyric.name ? `${lyric.name}=${lyricstr}` : lyricstr}`;
360
- });
361
- return `@voice(\n\t${notes},${this.name},\n\t${lyricStrs.join(",\n\t")}\n)`;
425
+ const parameters = [`${this.content.toString(source)},${quote(this.name)}`];
426
+ for (const lyric of this.lyrics) {
427
+ const text = quote(lyric.tokens.map(token => token.length === 0
428
+ ? "@" : `{${token.replace(/[\\{}@-]/g, "\\$&")}}`).join(" "));
429
+ parameters.push(lyric.name ? `${lyric.name}=${text}` : text);
430
+ }
431
+ return `@voice(\n\t${parameters.join(",\n\t")}\n)`;
362
432
  }
363
433
  }
364
434
  class VoicesFunction extends ASTFunctionNode {
365
435
  static def = {
366
436
  name: ["voices", "vs"],
367
437
  description: "多个声部",
368
- example: `@voices(
369
- @voice({C1 D1 E1}, 钢琴, 男=ha ha ha),
370
- @voice({C2 D2 E2}, , "la la la")
438
+ details: `\
439
+ ~~~jpfun
440
+ @voices(
441
+ @voice({1 2 3}, 钢琴, 男=ha ha ha),
442
+ @voice({3 4 5}, , "la la la")
371
443
  )
372
- 语法糖:当多个 voice 用 voice 的语法糖写在一起时,会自动创建一个 voices 组件包裹它们。
373
- 例:
374
- N(钢琴): C1 D1 E1
444
+ ~~~
445
+
446
+ 接受多个声部作为位置参数,将它们按时间对齐、分行排布;声部名称和歌词由各自的 \`@voice\` 提供
447
+
448
+ **简写**:连续声明多个 \`N:\` 声部及其 \`L:\` 歌词,会自动组成 \`voices\`
449
+
450
+ ~~~jpfun
451
+ N(钢琴): 1 2 3
375
452
  L(男): ha ha ha
376
- N: C2 D2 E2
453
+ N: 3 4 5
377
454
  L: la la la
378
- `,
455
+ ~~~`,
379
456
  allowExtraArgs: true,
380
457
  extraArgType: "content",
381
458
  args: []
@@ -418,9 +495,9 @@ L: la la la
418
495
  constructor(span, args, ctx, parent = null) {
419
496
  super(span, parent);
420
497
  this.voices = [];
421
- this.size = ctx.fontSize;
422
- this.braceSpace = ctx.fontSize;
423
- this.measure = makeVoicesMeasure(ctx.fontSize);
498
+ this.size = ctx.variables.fontsize;
499
+ this.braceSpace = ctx.variables.fontsize;
500
+ this.measure = makeVoicesMeasure(ctx.variables.fontsize);
424
501
  for (const [, value] of args) {
425
502
  if (value instanceof ASTNodeBase) {
426
503
  if (value instanceof VoiceFunction)
@@ -451,7 +528,7 @@ L: la la la
451
528
  toString(source) {
452
529
  const voicelines = Array(this.voices.length);
453
530
  for (let i = 0; i < this.voices.length; i++) {
454
- const part = this.voices[i].toString(source).replace(/^/gm, "\t");
531
+ const part = `\t${this.voices[i].toString(source)}`;
455
532
  voicelines[i] = part;
456
533
  }
457
534
  return `@voices(\n${voicelines.join(",\n")}\n)`;
@@ -464,8 +541,9 @@ const LYRIC_SIZE_RATIO = 0.82;
464
541
  /** 无大括号时标签右侧的空隙,相对声部字号 */
465
542
  const NAME_GAP_RATIO = 0.6;
466
543
  /** 歌词行名称的宽度决定标签列宽度,因此声部名事件和歌词附件必须用同一个样式测量 */
467
- function lyricNameStyle(size) {
544
+ function lyricNameStyle(size, fontFamily) {
468
545
  return {
546
+ fontFamily,
469
547
  fontSize: size * LYRIC_SIZE_RATIO,
470
548
  fill: "#000",
471
549
  fontWeight: 600,
@@ -494,7 +572,7 @@ class VoiceNameTemporal extends TemporalNodeBase {
494
572
  for (const lyric of this.ast.lyrics) {
495
573
  if (!lyric.name)
496
574
  continue;
497
- const lyricMetrics = context.textMeasurer.measureText(lyric.name, lyricNameStyle(this.ast.size));
575
+ const lyricMetrics = context.textMeasurer.measureText(lyric.name, lyricNameStyle(this.ast.size, this.ast.font));
498
576
  labelWidth = Math.max(labelWidth, lyricMetrics.w);
499
577
  }
500
578
  // 没有大括号时也要在标签右侧留出与内容分离的空隙
@@ -513,6 +591,7 @@ class VoiceNameTemporal extends TemporalNodeBase {
513
591
  get style() {
514
592
  return {
515
593
  fontSize: this.ast.size * 0.85,
594
+ fontFamily: this.ast.font,
516
595
  fill: "#000",
517
596
  fontWeight: 600,
518
597
  textAlign: "right",
@@ -628,14 +707,17 @@ class VoiceLyricsAttachment {
628
707
  const { lyrics, size } = this.nameHost.ast;
629
708
  if (targets.length === 0 || lyrics.length === 0)
630
709
  return;
631
- const style = { fontSize: size * LYRIC_SIZE_RATIO, fill: "#000" };
710
+ const style = { fontSize: size * LYRIC_SIZE_RATIO, fontFamily: this.nameHost.ast.font, fill: "#000" };
632
711
  for (let i = 0; i < targets.length; i++) {
633
712
  const target = targets[i];
634
713
  let halfWidth = 0;
635
714
  for (const lyric of lyrics) {
636
715
  const text = lyric.tokens[i];
637
- if (text)
638
- halfWidth = Math.max(halfWidth, context.textMeasurer.measureText(text, style).w / 2);
716
+ if (text) {
717
+ const { body } = splitLyricText(text);
718
+ if (body)
719
+ halfWidth = Math.max(halfWidth, context.textMeasurer.measureText(body, style).w / 2);
720
+ }
639
721
  }
640
722
  if (halfWidth === 0)
641
723
  continue;
@@ -683,8 +765,8 @@ class VoiceLyricsAttachment {
683
765
  const fontSize = size * LYRIC_SIZE_RATIO;
684
766
  const rowGap = size * 0.24;
685
767
  const firstRowGap = size * 0.32;
686
- const lyricStyle = { fontSize, fill: "#000" };
687
- const nameStyle = lyricNameStyle(size);
768
+ const lyricStyle = { fontSize, fontFamily: this.nameHost.ast.font, fill: "#000" };
769
+ const nameStyle = lyricNameStyle(size, this.nameHost.ast.font);
688
770
  const baselineOffset = context.textMeasurer.measureText("M", lyricStyle).baseline + firstRowGap;
689
771
  const baselineOf = (bottom, row) => bottom + baselineOffset + row * (fontSize + rowGap);
690
772
  // 歌词行名称与声部名共用左侧那一列,因此只出现在声部名所在的那一行
@@ -699,11 +781,14 @@ class VoiceLyricsAttachment {
699
781
  if (!text || bottom === undefined)
700
782
  continue;
701
783
  const metrics = context.textMeasurer.measureText(text, lyricStyle);
784
+ const { prefix, body } = splitLyricText(text);
785
+ const bodyWidth = body ? context.textMeasurer.measureText(body, lyricStyle).w : 0;
786
+ const prefixWidth = prefix ? context.textMeasurer.measureText(prefix, lyricStyle).w : 0;
702
787
  preparedText.push({
703
788
  text,
704
789
  style: lyricStyle,
705
790
  textBaselineY: metrics.baseline,
706
- x: target.box.x + target.ports["lyric"].x - metrics.w / 2,
791
+ x: target.box.x + target.ports["lyric"].x - bodyWidth / 2 - prefixWidth,
707
792
  y: baselineOf(bottom, row) - metrics.baseline,
708
793
  w: metrics.w,
709
794
  h: metrics.h,
@@ -1,34 +1,38 @@
1
- import { ANCHOR_KEY, isVisualTemporalNode, } from "../../lowering/types.js";
1
+ import { ANCHOR_KEY, isVisualTemporalNode, } from "../temporal.js";
2
2
  import { repeatPass } from "../bar/index.js";
3
3
  import { ASTFunctionNode, } from "../ASTtypes.js";
4
4
  import { Diagnostic, ErrorDiagnostic } from "../../diagnostic.js";
5
5
  class VoltaFunction extends ASTFunctionNode {
6
6
  static def = {
7
7
  name: ["volta"],
8
- description: "反复房子",
9
- example: `@volta(from, to, pass, ...) 只在指定遍数演奏的一段区间
10
- from 和 to 是区间首末音符的标签,因此房子可以横跨谱面行
11
- pass 是遍数:整段谱面演奏到第几遍时才播这里,必填正整数
12
- 再写几个遍数就是几遍共用一个房子,顺序随意,标签按升序写成 1.2.
13
-
8
+ description: "反复的跳房子",
9
+ details: `\
10
+ ~~~jpfun
14
11
  |: 1 2 | 3@a 4@b :| 5@c 6@d | 7
15
- @volta(a, b, 1) @volta(c, d, 2)`,
12
+ @volta(a, b, 1) @volta(c, d, 2)
13
+ ~~~
14
+ - **额外位置参数**:更多遍数;例如 \`@volta(a, b, 1, 2)\` 让前两遍共用一个房子。遍数顺序不限,谱面按升序显示。
15
+
16
+ 本例第一遍演奏 \`a\` 到 \`b\`,第二遍演奏 \`c\` 到 \`d\`。`,
16
17
  allowExtraArgs: true,
17
18
  extraArgType: "number",
18
19
  args: [
19
20
  {
20
21
  name: "from",
22
+ description: "区间首音的标签,区间允许跨谱面行",
21
23
  type: "label",
22
24
  default: null,
23
25
  },
24
26
  {
25
27
  name: "to",
28
+ description: "区间末音的标签",
26
29
  type: "label",
27
30
  default: null,
28
31
  },
29
32
  {
30
33
  // 不给默认值:房子上的数字本来就是谱面的一部分,漏写会让整段被静默跳过
31
34
  name: "pass",
35
+ description: "进入此区间的演奏遍数,须为正整数",
32
36
  type: "number",
33
37
  default: null,
34
38
  },
@@ -40,6 +44,7 @@ pass 是遍数:整段谱面演奏到第几遍时才播这里,必填正整数
40
44
  passes;
41
45
  /** 区间没有可见宿主时括线的尺寸基准 */
42
46
  size;
47
+ font;
43
48
  /** volta 不推进时间,只把 AST 端点解析成稳定的 temporal 引用 */
44
49
  loweringEnter(ctx) {
45
50
  let from = ctx.getTemporalNodes(this.from).at(0);
@@ -58,6 +63,7 @@ pass 是遍数:整段谱面演奏到第几遍时才播这里,必填正整数
58
63
  constructor(span, args, ctx, parent = null) {
59
64
  super(span, parent);
60
65
  const [from, to, pass] = this.getArgValue(args, ctx);
66
+ this.font = ctx.variables.numberfont;
61
67
  // 第一个遍数走 def.args,其余的落在 extraArgType 里,按位置索引取
62
68
  const passes = new Set([pass]);
63
69
  for (let i = 3; args.has(i); i++)
@@ -65,7 +71,7 @@ pass 是遍数:整段谱面演奏到第几遍时才播这里,必填正整数
65
71
  this.from = from;
66
72
  this.to = to;
67
73
  this.passes = [...passes].sort((a, b) => a - b);
68
- this.size = ctx.fontSize;
74
+ this.size = ctx.variables.fontsize;
69
75
  if (this.passes.some(value => !Number.isSafeInteger(value) || value <= 0)) {
70
76
  throw new ErrorDiagnostic("E_VOLTA_INVALID_PASS", "@volta 的遍数必须是正整数", span);
71
77
  }
@@ -87,14 +93,6 @@ class VoltaAttachment {
87
93
  from;
88
94
  to;
89
95
  layer = "foreground";
90
- /** 首末端点列最上方的对象、区间最大字号与紧邻的小节线,横向准备时填入 */
91
- head;
92
- tail;
93
- span = 0;
94
- leftBar;
95
- rightBar;
96
- /** 逐行记下覆盖列内各轨的最高点,相对该轨视觉轴;轴要等纵向求解才知道 */
97
- topsByLine = [];
98
96
  constructor(ast, from, to) {
99
97
  this.ast = ast;
100
98
  this.from = from;
@@ -117,11 +115,12 @@ class VoltaAttachment {
117
115
  : { kind: "jump", column: end + 1 },
118
116
  };
119
117
  }
120
- prepareHorizontal(lines) {
121
- if (!isVisualTemporalNode(this.from) || !isVisualTemporalNode(this.to))
122
- return;
118
+ createGeometry(context) {
119
+ if (!isVisualTemporalNode(this.from) || !isVisualTemporalNode(this.to)) {
120
+ return { regions: [], paint() { } };
121
+ }
123
122
  const locate = (node) => {
124
- const view = lines[node.layoutLine];
123
+ const view = context.lines[node.layoutLine];
125
124
  return { line: node.layoutLine, view, column: view.columnOf(node) };
126
125
  };
127
126
  // 两个标签谁写在前面不一定,按谱面顺序摆正
@@ -130,42 +129,29 @@ class VoltaAttachment {
130
129
  [from, to] = [to, from];
131
130
  }
132
131
  if (from.column < 0 || to.column < 0)
133
- return;
134
- this.head = from.view.columns[from.column][0];
135
- this.tail = to.view.columns[to.column][0];
136
- this.leftBar = barIn(from.view.columns[from.column - 1]);
137
- this.rightBar = barIn(to.view.columns[to.column + 1]);
138
- this.span = 0;
139
- this.topsByLine.length = 0;
132
+ return { regions: [], paint() { } };
133
+ const head = from.view.columns[from.column][0];
134
+ const tail = to.view.columns[to.column][0];
135
+ const leftBar = barIn(from.view.columns[from.column - 1]);
136
+ const rightBar = barIn(to.view.columns[to.column + 1]);
137
+ let span = 0;
138
+ const coverage = [];
140
139
  // 括线盖住的是:首行从起点列向右、末行向左到终点列、中间行整行
141
140
  for (let line = from.line; line <= to.line; line++) {
142
- const view = lines[line];
141
+ const view = context.lines[line];
143
142
  const start = line === from.line ? from.column : 0;
144
143
  const end = line === to.line ? to.column : view.columns.length - 1;
145
- const tops = new Map();
146
144
  for (let column = start; column <= end; column++) {
147
- const hosts = view.columns[column];
148
- this.span = Math.max(this.span, hosts[0].ast.size);
149
- // 此刻 visualAxis 已冻结,纵向求解后 box.y 就是 该轨视觉轴 - visualAxis
150
- for (const host of hosts) {
151
- const top = -host.box.visualAxis;
152
- const known = tops.get(host.track);
153
- if (known === undefined || top < known)
154
- tops.set(host.track, top);
155
- }
145
+ span = Math.max(span, view.columns[column][0].ast.size);
156
146
  }
157
- this.topsByLine.push(tops);
147
+ coverage.push({ from: start, to: end, wholeLine: line !== from.line && line !== to.line });
158
148
  }
159
- }
160
- createGeometry(context) {
161
- const { head, tail, leftBar, rightBar } = this;
162
- if (!head || !tail)
163
- return { regions: [], paint() { } };
164
149
  const firstLine = head.layoutLine;
165
150
  const lastLine = tail.layoutLine;
166
- const size = this.span || this.ast.size;
151
+ const size = span || this.ast.size;
167
152
  const style = {
168
153
  fontSize: size * 0.6,
154
+ fontFamily: this.ast.font,
169
155
  textAlign: "left",
170
156
  fill: "#000",
171
157
  };
@@ -179,17 +165,14 @@ class VoltaAttachment {
179
165
  // 中间行可能没有本房子的可见对象,仍要补满,所以按行号连续遍历
180
166
  const draws = Array.from({ length: lastLine - firstLine + 1 }, (_, offset) => {
181
167
  const line = firstLine + offset;
182
- // 跨行断开的那一侧一直画到页边,接上下一行
183
- const left = line !== firstLine ? context.originX
184
- : leftBar?.layoutLine === line ? leftBar.box.x + leftBar.box.anchor + 5 // 5是防止前后房子竖线重合
185
- : head.box.x + (head.ports["body.left"]?.x ?? 0);
186
- const right = line !== lastLine ? context.originX + context.width
187
- : rightBar?.layoutLine === line ? rightBar.box.x + rightBar.box.anchor
188
- : tail.box.x + (tail.ports["body.right"]?.x ?? tail.box.w);
189
168
  let topTrack = head.track;
190
169
  let hostTop = Infinity;
191
- for (const [track, top] of this.topsByLine[offset]) {
192
- const y = context.getVisualAxis(line, track) + top;
170
+ const range = coverage[offset];
171
+ const extents = range.wholeLine
172
+ ? context.getRangeExtents(line)
173
+ : context.getRangeExtents(line, [range.from, range.to]);
174
+ for (const [track, extent] of extents) {
175
+ const y = context.getVisualAxis(line, track) + extent.top;
193
176
  if (y < hostTop) {
194
177
  topTrack = track;
195
178
  hostTop = y;
@@ -198,6 +181,15 @@ class VoltaAttachment {
198
181
  // 整行空白时没有主体可以参照,退到轨道轴上
199
182
  if (hostTop === Infinity)
200
183
  hostTop = context.getVisualAxis(line, topTrack);
184
+ // 换行断点跟随系统的视觉边界;空行没有主体时仍退到内容区页边
185
+ const rawLeft = line !== firstLine ? this.lineEntryX(context, line, topTrack)
186
+ : leftBar?.layoutLine === line ? leftBar.box.x + leftBar.box.anchor + 5 // 5是防止前后房子竖线重合
187
+ : head.box.x + (head.ports["body.left"]?.x ?? 0);
188
+ const rawRight = line !== lastLine ? this.lineRightX(context, line)
189
+ : rightBar?.layoutLine === line ? rightBar.box.x + rightBar.box.anchor
190
+ : tail.box.x + (tail.ports["body.right"]?.x ?? tail.box.w);
191
+ const left = Math.min(rawLeft, rawRight);
192
+ const right = Math.max(rawLeft, rawRight);
201
193
  return { left, right, lineY: hostTop - size * 0.2 - hook, line, track: topTrack };
202
194
  });
203
195
  return {
@@ -224,4 +216,19 @@ class VoltaAttachment {
224
216
  },
225
217
  };
226
218
  }
219
+ lineEntryX(context, line, track) {
220
+ const host = context.lines[line].trackRuns.get(track)?.find(item => !item.T.isZero());
221
+ return host?.box.x ?? context.originX;
222
+ }
223
+ lineRightX(context, line) {
224
+ const runs = context.lines[line].trackRuns;
225
+ if (runs.size === 0)
226
+ return context.originX + context.width;
227
+ let right = context.originX;
228
+ for (const run of runs.values()) {
229
+ for (const host of run)
230
+ right = Math.max(right, host.box.x + host.box.w);
231
+ }
232
+ return right;
233
+ }
227
234
  }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,3 @@
1
+ import * as core from "../core.js";
2
+ const target = globalThis;
3
+ Object.assign(target.jpfun ??= {}, core);
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,15 @@
1
+ import { convertMidiJsonToJpFun, } from "../converter/midi/convert.js";
2
+ const target = globalThis;
3
+ const api = target.jpfun ??= {};
4
+ Object.assign(api, {
5
+ midiJsonToJpFun(input, options = {}) {
6
+ const layoutCapability = (options.barsPerLine ?? 0) <= 0
7
+ ? {
8
+ compileScore: api.compileScore,
9
+ anchorKey: api.ANCHOR_KEY,
10
+ page: api.DEFAULT_PAGE_CONFIG,
11
+ }
12
+ : undefined;
13
+ return convertMidiJsonToJpFun(input, options, layoutCapability);
14
+ },
15
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,3 @@
1
+ import { musicXmlToJpFun } from "../converter/musicxml/index.js";
2
+ const target = globalThis;
3
+ Object.assign(target.jpfun ??= {}, { musicXmlToJpFun });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,3 @@
1
+ import * as full from "../index.js";
2
+ const target = globalThis;
3
+ Object.assign(target.jpfun ??= {}, full);
package/dist/index.d.ts CHANGED
@@ -1,21 +1,3 @@
1
- export * from "./pipeline.js";
2
- export * from "./functions/ASTtypes.js";
3
- export * from "./functions/default.js";
4
- export * from "./diagnostic.js";
5
- export * from "./fraction.js";
6
- export * from "./parser/types.js";
7
- export * from "./parser/grammarType.js";
8
- export * from "./parser/parserContext.js";
9
- export * from "./parser/preprocess.js";
10
- export * from "./lowering/types.js";
11
- export * from "./lowering/loweringContext.js";
12
- export * from "./lowering/track.js";
13
- export * from "./playback/types.js";
14
- export * from "./playback/time.js";
15
- export * from "./playback/compile.js";
16
- export * from "./layout/types.js";
17
- export * from "./layout/model.js";
18
- export * from "./layout/default.js";
19
- export * from "./layout/page.js";
20
- export * from "./layout/engine.js";
21
- export * from "./render/index.js";
1
+ export * from "./core.js";
2
+ export * from "./converter/midi/index.js";
3
+ export * from "./converter/musicxml/index.js";