jpfun 0.4.0 → 0.5.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 (83) hide show
  1. package/README.md +53 -113
  2. package/dist/converter/midi/convert.js +14 -7
  3. package/dist/converter/musicxml/features.d.ts +57 -16
  4. package/dist/converter/musicxml/features.js +204 -42
  5. package/dist/converter/musicxml/index.d.ts +7 -2
  6. package/dist/converter/musicxml/index.js +256 -268
  7. package/dist/converter/musicxml/model.d.ts +36 -7
  8. package/dist/converter/musicxml/resolve.d.ts +10 -0
  9. package/dist/converter/musicxml/resolve.js +152 -0
  10. package/dist/core.d.ts +1 -0
  11. package/dist/core.js +1 -0
  12. package/dist/fraction.js +7 -1
  13. package/dist/functions/ASTtypes.d.ts +4 -4
  14. package/dist/functions/ASTtypes.js +5 -9
  15. package/dist/functions/adjust/index.js +16 -53
  16. package/dist/functions/arpeggio/index.js +27 -15
  17. package/dist/functions/bar/index.js +7 -8
  18. package/dist/functions/beam/index.js +6 -1
  19. package/dist/functions/beam/layout.d.ts +1 -1
  20. package/dist/functions/beam/layout.js +5 -3
  21. package/dist/functions/box/index.js +88 -82
  22. package/dist/functions/br/index.js +9 -5
  23. package/dist/functions/dash/index.js +24 -13
  24. package/dist/functions/div/index.d.ts +5 -2
  25. package/dist/functions/div/index.js +15 -14
  26. package/dist/functions/dot/index.js +13 -11
  27. package/dist/functions/dyn/index.js +35 -39
  28. package/dist/functions/grace/index.d.ts +19 -6
  29. package/dist/functions/grace/index.js +136 -121
  30. package/dist/functions/head/index.js +19 -14
  31. package/dist/functions/key/index.js +8 -4
  32. package/dist/functions/meter/index.js +22 -2
  33. package/dist/functions/note/index.d.ts +6 -1
  34. package/dist/functions/note/index.js +48 -30
  35. package/dist/functions/note/source-transform.d.ts +21 -0
  36. package/dist/functions/note/source-transform.js +164 -0
  37. package/dist/functions/page/index.d.ts +8 -1
  38. package/dist/functions/page/index.js +23 -13
  39. package/dist/functions/program/index.js +11 -2
  40. package/dist/functions/set/index.js +14 -8
  41. package/dist/functions/stack/index.js +7 -5
  42. package/dist/functions/symbol/index.d.ts +2 -0
  43. package/dist/functions/symbol/index.js +11 -7
  44. package/dist/functions/symbol/ornament.d.ts +3 -3
  45. package/dist/functions/symbol/ornament.js +21 -35
  46. package/dist/functions/symbol/symbols/accent.js +5 -5
  47. package/dist/functions/symbol/symbols/fermata.js +5 -4
  48. package/dist/functions/tempo/index.js +7 -1
  49. package/dist/functions/text/index.js +11 -3
  50. package/dist/functions/tie/index.js +28 -32
  51. package/dist/functions/tuplet/index.js +26 -24
  52. package/dist/functions/up/index.js +24 -21
  53. package/dist/functions/voice/index.js +36 -26
  54. package/dist/functions/volta/index.js +16 -17
  55. package/dist/jpfun.core.min.js +98 -129
  56. package/dist/jpfun.from-midi.min.js +5 -5
  57. package/dist/jpfun.from-musicxml.min.js +8 -8
  58. package/dist/jpfun.min.js +107 -138
  59. package/dist/layout/engine.d.ts +11 -1
  60. package/dist/layout/engine.js +299 -96
  61. package/dist/layout/page.js +2 -0
  62. package/dist/layout/range.d.ts +26 -0
  63. package/dist/layout/range.js +75 -0
  64. package/dist/layout/types.d.ts +33 -4
  65. package/dist/lowering/types.d.ts +5 -0
  66. package/dist/parser/parse-utils/length-utils.js +2 -0
  67. package/dist/parser/parse-utils/note-utils.d.ts +2 -0
  68. package/dist/parser/parse-utils/note-utils.js +6 -2
  69. package/dist/parser/parserContext.js +1 -1
  70. package/dist/playback/compile.d.ts +2 -9
  71. package/dist/playback/compile.js +297 -186
  72. package/dist/playback/event.d.ts +8 -16
  73. package/dist/playback/event.js +13 -3
  74. package/dist/playback/types.d.ts +76 -18
  75. package/dist/playback/types.js +1 -0
  76. package/dist/render/index.d.ts +1 -1
  77. package/dist/render/index.js +1 -1
  78. package/dist/render/recording.d.ts +1 -0
  79. package/dist/render/recording.js +21 -0
  80. package/dist/render/translate.d.ts +14 -0
  81. package/dist/render/translate.js +48 -0
  82. package/grammar.md +103 -78
  83. package/package.json +4 -4
@@ -6,67 +6,53 @@ const MAX_BPM = 480;
6
6
  /** 一个装饰音能展开的子音上限;防的是 `@tempo(0.01)` 这类手滑 */
7
7
  const MAX_SUBNOTES = 256;
8
8
  /**
9
- * 把一个音按固定音级模式展开成装饰音序列
9
+ * 按调内音级模式拆分传入音段,使用结构完成后的范围和最终播放速度
10
10
  *
11
- * 密度按发声时的速度决定而不是固定的记谱时值:固定 1/8 四分音符在慢速下
12
- * 每秒只有两三次,听起来根本不是颤音。默认速度下仍然恰好是 1/8 四分音符。
11
+ * 重复式模式按速度计算次数,固定模式在整段时值内均分。
12
+ * 派生音段继承轨道和来源,仍只声明音段,不直接发布 NoteOn/NoteOff。
13
13
  */
14
14
  export function ornament(pattern) {
15
15
  return (context, owned) => {
16
- const noteOffs = new Map();
17
- for (const event of owned) {
18
- if (event.kind === "note-off")
19
- noteOffs.set(event.noteId, event);
20
- }
21
- const removed = new Set();
22
- const added = [];
16
+ const result = [];
17
+ // 前一项声音变换可能已产生多个音段,因此逐段处理,不能假定只剩一个宿主音。
23
18
  for (const note of owned) {
24
- if (note.kind !== "note-on" || !note.transpose)
25
- continue;
26
- const off = noteOffs.get(note.noteId);
27
- if (!off)
19
+ // 缺少调内移调能力的目标原样保留,例如固定打击音。
20
+ if (!note.transpose) {
21
+ result.push(note);
28
22
  continue;
29
- const duration = off.at.clone().sub(note.at);
30
- const bpm = context.stateAt(note.at).effectiveBpm;
23
+ }
24
+ const duration = note.end.clone().sub(note.start);
25
+ // 此处读取最终速度,结构延长和其他声部的速度效果都已计入。
26
+ const bpm = context.stateAt(note.start).effectiveBpm;
31
27
  const steps = pattern.repeat ? repeatToFill(pattern.body, duration.toNumber(), bpm) : pattern.body;
32
28
  const step = duration.div(steps.length);
33
- removed.add(note);
34
- removed.add(off);
29
+ // 用 Fraction 等分最终区间,保证首尾边界准确且不会积累浮点时间误差。
35
30
  for (let index = 0; index < steps.length; index++) {
36
31
  const offset = steps[index];
37
- const noteId = context.nextNoteId();
38
- const at = note.at.clone().add(step.clone().mul(index));
39
- const order = note.order + index / steps.length;
40
- const on = {
32
+ const start = note.start.clone().add(step.clone().mul(index));
33
+ result.push({
41
34
  ...note,
42
- at,
43
- noteId,
44
- order,
35
+ start,
36
+ end: start.clone().add(step),
45
37
  // 本音保留主体的临时升降号,只有偏移音才走调内换算
46
38
  midi: offset === 0 ? note.midi : note.transpose(offset),
47
39
  // 后续 ornament 要从这个派生音继续移调,不能仍以最初的宿主为基准
48
40
  transpose: delta => note.transpose(offset + delta),
49
- origins: [...note.origins],
50
- sourceSpans: note.sourceSpans.map(span => ({ ...span })),
51
- };
52
- added.push(on, {
53
- ...off,
54
- at: at.clone().add(step),
55
- noteId,
56
- order,
57
- origins: [...off.origins],
58
41
  });
59
42
  }
60
43
  }
61
- return [...owned.filter(event => !removed.has(event)), ...added];
44
+ return result;
62
45
  };
63
46
  }
47
+ /** 按最终速度计算完整音型的重复次数,并限制总子音数 */
64
48
  function repeatToFill(body, duration, bpm) {
65
49
  // 名义密度与速度无关(秒数里的 bpm 与频率里的 bpm 恰好抵消),只在 bpm 越界时按比例修正
66
50
  const ratio = Math.min(MAX_BPM, Math.max(MIN_BPM, bpm)) / bpm;
51
+ // 预算按完整音型计算,避免在达到子音上限时留下半个音型。
67
52
  const limit = Math.floor(MAX_SUBNOTES / body.length);
68
53
  const cycles = Math.min(limit, Math.max(1, Math.round(duration * NOMINAL_RATE * ratio / body.length)));
69
54
  const result = [];
55
+ // 先得到完整音级序列,调用方再按实际子音总数统一分配宿主时值。
70
56
  for (let i = 0; i < cycles; i++)
71
57
  result.push(...body);
72
58
  return result;
@@ -14,11 +14,11 @@ export const accentSymbol = {
14
14
  style: { stroke: "#000", strokeWidth: 1 },
15
15
  },
16
16
  ],
17
- emitPlayback: emitter => emitter.affectFollowing((_context, events) => {
18
- for (const event of events) {
19
- if (event.kind === "note-on") {
20
- event.velocity = Math.min(MAX_VELOCITY, event.velocity + ACCENT_BOOST);
21
- }
17
+ /** 将重音登记为声音变换,保留音段的结构边界和系统速度 */
18
+ emitPlayback: emitter => emitter.affectFollowing((_context, notes) => {
19
+ // 前序装饰可能已拆出多个子音,逐个增加原有力度并限制到 MIDI 最大力度。
20
+ for (const note of notes) {
21
+ note.velocity = Math.min(MAX_VELOCITY, note.velocity + ACCENT_BOOST);
22
22
  }
23
23
  }),
24
24
  };
@@ -1,7 +1,10 @@
1
+ // 多声部常在同一时段各写一个延长记号;按区间并集减速一次,避免速度随记号数指数下降
2
+ const FERMATA_BPM_SCALE = {};
1
3
  export const fermataSymbol = {
2
4
  name: "fermata",
3
5
  description: "延长记号:覆盖区间速度减半,目标音实际时长翻倍",
4
6
  weight: 0.6,
7
+ paddingBottom: 3,
5
8
  shapes: [
6
9
  {
7
10
  path: [
@@ -17,8 +20,6 @@ export const fermataSymbol = {
17
20
  style: { fill: "#000" },
18
21
  },
19
22
  ],
20
- emitPlayback: emitter => {
21
- emitter.control(emitter.start, state => state.bpmScale.div(2));
22
- emitter.control(emitter.end, state => state.bpmScale.mul(2));
23
- },
23
+ /** 声明同 key 的减速区间,沿逻辑连接延续终点;范围不依赖实际子音的拆分结果 */
24
+ emitPlayback: emitter => emitter.scaleFollowingBpm(FERMATA_BPM_SCALE, 1, 2, { followConnections: true }),
24
25
  };
@@ -5,16 +5,22 @@ class TempoFunction extends ASTFunctionNode {
5
5
  static def = {
6
6
  name: ["tempo"],
7
7
  description: "设置时间线上的速度",
8
- example: `@tempo(96) 将其后的时间状态速度设置为 96 BPM`,
8
+ details: `\
9
+ ~~~jpfun
10
+ @tempo(96)
11
+ ~~~
12
+ 速度从当前时间位置起生效;用 \`1 ^ @tempo(96)\` 可将记号叠在音符上方`,
9
13
  allowExtraArgs: false,
10
14
  args: [
11
15
  {
12
16
  name: "bpm",
17
+ description: "每分钟四分音符数",
13
18
  type: "number",
14
19
  default: null,
15
20
  },
16
21
  {
17
22
  name: "size",
23
+ description: "速度记号字号",
18
24
  type: "length",
19
25
  default: {
20
26
  value: 1,
@@ -5,17 +5,22 @@ import { DEFAULT_KEY, TemporalNodeBase } from "../temporal.js";
5
5
  class TextFunction extends ASTFunctionNode {
6
6
  static def = {
7
7
  name: ["text"],
8
- description: "文本标记。括号内可直接换行,支持 left、center、right 对齐",
9
- example: `@text(进入主题)
10
- 语法糖:直接写双引号字符串,如 "进入主题";可跨行,引号未配对则不生效`,
8
+ description: "文本标记",
9
+ details: `\
10
+ ~~~jpfun
11
+ @text(进入主题, size=0.8em, align=center)
12
+ ~~~
13
+ **简写**:直接写双引号文本,如 \`"进入主题"\`;可跨行,双引号须配对`,
11
14
  allowExtraArgs: false,
12
15
  args: [
13
16
  {
14
17
  type: "string",
18
+ description: "文本内容,可直接换行;含逗号等歧义字符时需加引号",
15
19
  default: null,
16
20
  },
17
21
  {
18
22
  name: "size",
23
+ description: "文本字号",
19
24
  type: "length",
20
25
  default: {
21
26
  value: 0.8, // 常用于标记,默认小一些
@@ -24,16 +29,19 @@ class TextFunction extends ASTFunctionNode {
24
29
  },
25
30
  {
26
31
  name: "lineheight",
32
+ description: "相对自身字号的行高倍数",
27
33
  type: "number",
28
34
  default: 1.25, // 相对自身字号的倍数
29
35
  },
30
36
  {
31
37
  name: "align",
38
+ description: "文本对齐方式:`left`、`center` 或 `right`",
32
39
  type: "string",
33
40
  default: "left",
34
41
  },
35
42
  {
36
43
  name: "font",
44
+ description: "文本字体,空值沿用当前字体设置",
37
45
  type: "string",
38
46
  default: ""
39
47
  },
@@ -10,8 +10,14 @@ class TieFunction extends ASTFunctionNode {
10
10
  static def = {
11
11
  name: ["tie"],
12
12
  description: "连音线",
13
- example: `@tie(label1, label2, ..., height=0.5em)
14
- 将端点依次连接;同行用一条弧线,跨行拆成分段连接;若不传端点则找最近的`,
13
+ details: `\
14
+ ~~~jpfun
15
+ 1@a 2 3@b @tie(a, b, height=0.5em)
16
+ ~~~
17
+ - **位置参数**:端点标签,可传多个,按顺序连接;省略时查找最近的可标记对象,包括增时线
18
+ - **height**:弧线高度,默认 \`0.5em\`
19
+
20
+ 同行端点用一条弧线连接,跨行时自动拆成多段。同轨同音且时间连续的音段建立播放连接,各段保留自己的装饰,例如 \`1 ^ $tr 1 @tie()\` 的后一音段不继承颤音。涉及增时线的连接仅绘制,不合并播放音符。`,
15
21
  allowExtraArgs: true,
16
22
  extraArgType: "label",
17
23
  args: []
@@ -66,7 +72,10 @@ class TieFunction extends ASTFunctionNode {
66
72
  return [];
67
73
  }
68
74
  toString(source) {
69
- return `@tie(${this.endPoints.map(p => `${p.label ?? 'anon'}:[${p.toString(source)}]`).join(", ")})`;
75
+ const labels = this.endPoints.map(endpoint => endpoint.label);
76
+ if (labels.some(label => label === undefined))
77
+ return source.slice(this.sourceSpan.start, this.sourceSpan.end);
78
+ return `@tie(${labels.join(", ")}, height=${this.height}px)`;
70
79
  }
71
80
  }
72
81
  export const TieNode = TieFunction;
@@ -85,39 +94,26 @@ class TieLayoutAttachment {
85
94
  this.thickness = Math.max(1.2, size * THICKNESS_RATIO);
86
95
  }
87
96
  /**
88
- * 只有同轨、同音、演奏时间连续的事件对才合并,其余 tie 纯视觉
89
- *
90
- * 反复会让同一个端点产生多个事件对,所以按首端点的每一个 NoteOn 各串一条链。
97
+ * 为同轨、同音且时间相接的逻辑音段建立连接,各次反复及各段装饰保持独立
98
+ * 此时只匹配来源和逻辑音高,实际子音是否能合并由声音展开后的核心处理
91
99
  */
92
100
  applyPlayback(context) {
93
- const noteOns = () => context.events
94
- .filter((event) => event.kind === "note-on");
95
- const noteOff = (noteId) => context.events
96
- .find((event) => event.kind === "note-off" && event.noteId === noteId);
97
- for (const head of noteOns().filter(note => note.origins.some(origin => origin.node === this.endPoints[0]))) {
98
- for (let i = 1; i < this.endPoints.length; i++) {
99
- const end = noteOff(head.noteId);
100
- if (!end)
101
- break;
102
- const next = noteOns().find(note => note.noteId !== head.noteId
103
- && note.origins.some(origin => origin.node === this.endPoints[i])
104
- && note.track === head.track
105
- && note.midi === head.midi
106
- && note.percussion === head.percussion
107
- && end.at.equals(note.at));
101
+ // 整体端点可能包含和弦多个成员或多遍访问,每个首段分别寻找自己的连接链。
102
+ for (const head of context.notes.filter(note => note.origins.some(origin => origin.node === this.endPoints[0]))) {
103
+ let previous = head;
104
+ // 按显式端点顺序延续;某一端点没有相接音段就停止,不能跨过缺口连接。
105
+ for (let index = 1; index < this.endPoints.length; index++) {
106
+ // connect 同时认领候选:配对冲突时继续找其他成员,重复声明可复用原配对。
107
+ const next = context.notes.find(note => note !== previous
108
+ && note.origins.some(origin => origin.node === this.endPoints[index])
109
+ && note.track === previous.track
110
+ && note.midi === previous.midi
111
+ && note.percussion === previous.percussion
112
+ && previous.end.equals(note.start)
113
+ && context.connect(previous, note));
108
114
  if (!next)
109
115
  break;
110
- const nextOff = noteOff(next.noteId);
111
- if (!nextOff)
112
- break;
113
- head.sourceSpans.push(...next.sourceSpans);
114
- for (const origin of next.origins) {
115
- if (!head.origins.includes(origin))
116
- head.origins.push(origin);
117
- }
118
- nextOff.noteId = head.noteId;
119
- nextOff.origins = [...head.origins];
120
- context.events.splice(0, context.events.length, ...context.events.filter(event => event !== end && event !== next));
116
+ previous = next;
121
117
  }
122
118
  }
123
119
  }
@@ -11,15 +11,21 @@ class TupletFunction extends ASTFunctionNode {
11
11
  static def = {
12
12
  name: ["tuplet"],
13
13
  description: "多连音",
14
- example: `@tuplet({1/23}, 4): 内容含5个最短时值单位,压缩到4倍最短单位的总时长,所以是八分音符的五连音,总时长为二分音符`,
14
+ details: `\
15
+ ~~~jpfun
16
+ @tuplet({1/ 2 3}, normal=4)
17
+ ~~~
18
+ 本例最短单位为八分音符,原内容占 5 个单位,缩放后占 4 个单位:形成五连音,总时长为一个二分音符`,
15
19
  allowExtraArgs: false,
16
20
  args: [
17
21
  {
18
22
  type: "content",
23
+ description: "连音内容,各元素按同一比例缩放时值",
19
24
  default: null,
20
25
  },
21
26
  {
22
27
  name: "normal",
28
+ description: "目标总时长,以内容中的最短时值为单位",
23
29
  type: "number",
24
30
  default: null,
25
31
  },
@@ -42,21 +48,19 @@ class TupletFunction extends ASTFunctionNode {
42
48
  };
43
49
  }
44
50
  loweringEnter(ctx) {
45
- const events = [];
46
51
  const group = {
47
- events,
48
- onTemporal(node) {
49
- // 此时不能改 T:actual 依赖整个作用域的最短值和总和。
50
- events.push(node);
51
- },
52
+ nodes: [],
53
+ attachments: [],
54
+ onTemporal(node) { group.nodes.push(node); },
55
+ onAttachment(attachment) { group.attachments.push(attachment); },
52
56
  };
53
57
  ctx.beginLoweringGroup(this, group);
54
58
  return [];
55
59
  }
56
60
  loweringExit(ctx, _track, timeOffset) {
57
- const group = ctx.endLoweringGroup(this);
61
+ const content = ctx.endLoweringGroup(this);
58
62
  // 零时长控制事件不构成连音单位,但稍后仍要随整组移动其开始位置。
59
- const positive = group.events.filter(event => event.T.compare(0) > 0);
63
+ const positive = content.nodes.filter(event => event.T.compare(0) > 0);
60
64
  if (positive.length === 0) {
61
65
  throw new ErrorDiagnostic("E_TUPLET_EMPTY", "@tuplet 的内容必须产生至少一个正时值事件", this.sourceSpan);
62
66
  }
@@ -82,15 +86,15 @@ class TupletFunction extends ASTFunctionNode {
82
86
  throw new ErrorDiagnostic("E_TUPLET_INVALID_RATIO", `@tuplet 的 normal 不能等于推导出的 actual (${actual})`, this.sourceSpan);
83
87
  }
84
88
  // 以作用域最早时间为仿射缩放原点,不能直接缩放绝对 t,否则嵌套位置会漂移。
85
- let start = group.events[0].t;
86
- for (let i = 1; i < group.events.length; i++) {
87
- if (group.events[i].t.compare(start) < 0)
88
- start = group.events[i].t;
89
+ let start = content.nodes[0].t;
90
+ for (let i = 1; i < content.nodes.length; i++) {
91
+ if (content.nodes[i].t.compare(start) < 0)
92
+ start = content.nodes[i].t;
89
93
  }
90
94
  start = start.clone();
91
95
  // t、T 与 lowering 游标必须一起修改;后继节点才会从缩放后的组尾继续。
92
96
  const offset = new Fraction();
93
- for (const event of group.events) {
97
+ for (const event of content.nodes) {
94
98
  offset.copyFrom(event.t).sub(start).mul(this.normal, actual);
95
99
  event.t.copyFrom(start).add(offset);
96
100
  event.T.mul(this.normal, actual);
@@ -99,7 +103,7 @@ class TupletFunction extends ASTFunctionNode {
99
103
  // 括线只依附可见主体;时值缩放本身仍覆盖所有收集到的事件。
100
104
  const visible = positive.filter(isVisualTemporalNode);
101
105
  if (visible.length >= 2) {
102
- ctx.addAttachment(new TupletLayoutAttachment(visible, actual, this.sourceSpan, this.font));
106
+ ctx.addAttachment(new TupletLayoutAttachment(visible, actual, this.sourceSpan, this.font, content));
103
107
  }
104
108
  return [];
105
109
  }
@@ -125,6 +129,7 @@ export const TupletNode = TupletFunction;
125
129
  * layout 根据最终宿主坐标测量数字和括线,paint 只回放已冻结的绝对几何。
126
130
  */
127
131
  class TupletLayoutAttachment {
132
+ content;
128
133
  layer = "foreground";
129
134
  /** lowering 固化的连音语义;重复 layout 时保持不变 */
130
135
  endPoints;
@@ -133,7 +138,8 @@ class TupletLayoutAttachment {
133
138
  /** 由端点最大字号冻结的绘制规格 */
134
139
  style;
135
140
  size;
136
- constructor(endPoints, actual, sourceSpan, font) {
141
+ constructor(endPoints, actual, sourceSpan, font, content) {
142
+ this.content = content;
137
143
  this.endPoints = endPoints;
138
144
  this.actual = actual;
139
145
  this.sourceSpan = sourceSpan;
@@ -168,15 +174,11 @@ class TupletLayoutAttachment {
168
174
  const textGap = this.size * 0.12;
169
175
  const hookHeight = this.size * 0.18;
170
176
  const hostGap = this.size * 0.14;
171
- // 只测首尾时间列,并合并先注册的 attachment,使后声明的括线排在外层。
172
- const view = context.lines[first.layoutLine];
173
- const from = view?.columnOf(first) ?? -1;
174
- const to = view?.columnOf(last) ?? -1;
177
+ // 范围避让与内容包含分别由系统查询,局部成员和正文使用同一入口。
175
178
  const axis = context.getVisualAxis(first.layoutLine, first.track);
176
- const extent = from < 0 || to < 0
177
- ? undefined
178
- : context.getRangeExtents(first.layoutLine, [Math.min(from, to), Math.max(from, to)]).get(first.track);
179
- const hostTop = axis + (extent?.top ?? 0);
179
+ const extent = context.getRangeExtents(first.layoutLine, [first, last], first.track);
180
+ const bounds = context.getContentBounds(this.content);
181
+ const hostTop = Math.min(axis + (extent?.top ?? 0), bounds?.y ?? Infinity);
180
182
  const strokeWidth = Math.max(1, this.size * 0.055);
181
183
  const lineY = hostTop - hostGap - Math.max(hookHeight, metrics.h / 2);
182
184
  const hookBottom = lineY + hookHeight;
@@ -2,7 +2,7 @@ import { ASTNodeBase, ASTFunctionNode, ASTTextNode, ASTLabelNode, ASTBraceNode }
2
2
  import { ErrorDiagnostic } from "../../diagnostic.js";
3
3
  import { ANCHOR_KEY, DEFAULT_KEY, isVisualTemporalNode, TemporalNodeBase, } from "../temporal.js";
4
4
  import { Fraction } from "../../fraction.js";
5
- import { prepareLayoutHost } from "../../layout/engine.js";
5
+ import { layoutLocalSequence } from "../../layout/engine.js";
6
6
  /**
7
7
  * `@up` / `@down` 共用的折叠容器,不导出
8
8
  *
@@ -178,12 +178,14 @@ class UpFunction extends FoldFunction {
178
178
  static def = {
179
179
  name: ["up"],
180
180
  description: "把同一时间位置的可见对象向上堆叠",
181
- example: `@up(content1, content2, ...)
182
- 语法糖: ^
183
- {content1} ^ {content2} ^ ...
184
- 第一个参数是宿主,其余依次叠在它的上方,常用于和弦、变速记号、音符注释。
185
- 全体成员折叠成一个事件、共享宿主的时值;需要各自独立时值的并行分支请用 & / @stack。
186
- `,
181
+ details: `\
182
+ ~~~jpfun
183
+ @up(1, 3, 5)
184
+ ~~~
185
+
186
+ 第一个内容参数为宿主,其余依次叠在上方;所有成员共享宿主时值。常用于和弦、速度记号和音符注释。
187
+
188
+ **简写**:\`1 ^ 3 ^ 5\`。复杂内容可用 \`{...}\` 包裹;需要独立节奏的并行分支时,使用 \`&\` / \`@stack\`。`,
187
189
  allowExtraArgs: true,
188
190
  extraArgType: "content",
189
191
  args: [],
@@ -203,12 +205,13 @@ class DownFunction extends FoldFunction {
203
205
  static def = {
204
206
  name: ["down"],
205
207
  description: "把同一时间位置的可见对象向下堆叠",
206
- example: `@down(content1, content2, ...)
207
- 语法糖: _
208
- {content1} _ {content2} _ ...
209
- 第一个参数是宿主,其余依次叠在它的下方,常用于力度记号等写在音符下面的标记。
210
- 与 ^ 混写时全部绑到同一个宿主:1^2_3 表示 2 在上、3 在下。
211
- `,
208
+ details: `\
209
+ ~~~jpfun
210
+ @down(1, $p)
211
+ ~~~
212
+ 第一个内容参数为宿主,其余依次叠在下方并共享其时值,常用于力度等下方标记。
213
+
214
+ **简写**:\`1 _ $p\`。与 \`^\` 混写时共用同一宿主:\`1^2_3\` 将 \`2\` 放在 \`1\` 上方、\`3\` 放在下方。`,
212
215
  allowExtraArgs: true,
213
216
  extraArgType: "content",
214
217
  args: [],
@@ -292,7 +295,7 @@ class FoldTemporal extends TemporalNodeBase {
292
295
  * 宿主留在轨道基线上,其余成员按书写顺序向上或向下叠放
293
296
  *
294
297
  * 成员不进入全局 columns,因此它们的准备、定位和绘制都由本节点负责;
295
- * 准备直接复用引擎的 prepareLayoutHost,保证成员的装饰、端口与顶层对象完全一致。
298
+ * 每个成员独立执行局部横排,宿主继承含框约束的占位,其余成员仍沿宿主锚点悬挂。
296
299
  */
297
300
  prepareLayout(context) {
298
301
  // lowering 期间修饰挂在折叠体上(augmenter 要看到整体节奏),渲染时交给宿主:
@@ -301,18 +304,19 @@ class FoldTemporal extends TemporalNodeBase {
301
304
  this.members[0].addon = this.addon;
302
305
  this.addon = void 0;
303
306
  }
304
- for (const member of this.members)
305
- prepareLayoutHost(member, context);
306
307
  const first = this.members[0];
307
308
  if (!first) {
308
309
  this.box.w = this.box.h = 0;
309
310
  this.box.anchor = this.box.visualAxis = 0;
310
311
  return;
311
312
  }
313
+ this.box.w = layoutLocalSequence([first], context);
314
+ for (let index = 1; index < this.members.length; index++)
315
+ layoutLocalSequence([this.members[index]], context);
312
316
  this.springConfig = { ...first.springConfig, ...this.springConfig };
313
317
  // 横向完全等于宿主:上下的标记(变速、注释、力度)常常比音符宽得多,
314
318
  // 让它们撑宽盒子会把右邻推开一大截;它们画在基线外侧,伸出盒外也不会碰撞
315
- const anchor = first.box.anchor;
319
+ const anchor = first.box.x + first.box.anchor;
316
320
  const gap = this.ast.size * 0.12;
317
321
  this.verticalOffsets.length = this.members.length;
318
322
  this.verticalOffsets.fill(0);
@@ -330,7 +334,6 @@ class FoldTemporal extends TemporalNodeBase {
330
334
  }
331
335
  for (let i = 0; i < this.verticalOffsets.length; i++)
332
336
  this.verticalOffsets[i] -= top;
333
- this.box.w = first.box.w;
334
337
  this.box.h = bottom - top;
335
338
  this.box.anchor = anchor;
336
339
  // 宿主对齐轨道基线,两侧成员各自向外撑开行高
@@ -340,11 +343,11 @@ class FoldTemporal extends TemporalNodeBase {
340
343
  const firstOffset = this.verticalOffsets[0];
341
344
  for (const name in first.ports) {
342
345
  const port = first.ports[name];
343
- this.ports[name] = { x: port.x, y: firstOffset + port.y };
346
+ this.ports[name] = { ...port, x: first.box.x + port.x, y: firstOffset + port.y };
344
347
  }
345
348
  // 代表成员没声明核心范围时退回它的整个盒子
346
- this.ports["body.left"] ??= { x: 0, y: this.box.visualAxis };
347
- this.ports["body.right"] ??= { x: first.box.w, y: this.box.visualAxis };
349
+ this.ports["body.left"] ??= { x: first.box.x, y: this.box.visualAxis };
350
+ this.ports["body.right"] ??= { x: first.box.x + first.box.w, y: this.box.visualAxis };
348
351
  // 唯一的例外:连音线要接到最上面那个成员的顶部
349
352
  const topIndex = this.aboveCount - 1;
350
353
  const topOffset = this.verticalOffsets[topIndex];
@@ -51,30 +51,32 @@ class VoiceFunction extends ASTFunctionNode {
51
51
  static def = {
52
52
  name: ["voice", "v"],
53
53
  description: "声部",
54
- example: `@voice({}, name, 歌词1名=歌词1, 歌词2名=歌词2, ...)
55
- 第一个参数为音符内容,第二个参数为写在最左侧的声部名称(可不填);
56
- 命名参数为歌词左侧的名称和内容,可以任意多个。也可以是位置参数,表示不需要名称。
57
- 例:
58
- @voice({C1 D1 E1}, 钢琴, 男=ha ha ha, 女=la la la)
59
- @voice({C1 D1 E1}, , ha ha ha, 女="la la la") 表示音符和第一个歌词都没有名称
60
- 歌词可以被引号包裹,如果包含逗号等歧义字符一定需要引号
61
- 语法糖:
62
- N(name): C1 D1 E1
63
- L(name): ha ha ha...
64
- L: ...
65
- 前面可以有任意空白,但最后换行表示结束。歌词此时允许有逗号而不加引号
66
- 如果一定要换行,最后加上'\\'
67
- 英文歌词用空格和连字符'-'分词,用'@'占位
68
- `,
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
+ 行首允许空白,换行结束当前声明;在行末加 \`\\\` 可续行。这种写法中的歌词允许直接使用逗号。`,
69
69
  allowExtraArgs: true,
70
70
  extraArgType: "string",
71
71
  args: [
72
72
  {
73
73
  type: "content",
74
+ description: "声部的音符内容",
74
75
  default: null,
75
76
  },
76
77
  {
77
78
  name: "name",
79
+ description: "显示在左侧的声部名称,空值隐藏名称",
78
80
  type: "string",
79
81
  default: "",
80
82
  },
@@ -352,7 +354,8 @@ L: ...
352
354
  token = "";
353
355
  };
354
356
  for (let i = 0; i < value.length; i++) {
355
- const ch = value[i];
357
+ const ch = String.fromCodePoint(value.codePointAt(i));
358
+ i += ch.length - 1;
356
359
  if (ch === "\\" && i + 1 < value.length && /[\\{}@-]/.test(value[i + 1])) {
357
360
  token += value[++i];
358
361
  }
@@ -433,17 +436,24 @@ class VoicesFunction extends ASTFunctionNode {
433
436
  static def = {
434
437
  name: ["voices", "vs"],
435
438
  description: "多个声部",
436
- example: `@voices(
437
- @voice({C1 D1 E1}, 钢琴, 男=ha ha ha),
438
- @voice({C2 D2 E2}, , "la la la")
439
+ details: `\
440
+ ~~~jpfun
441
+ @voices(
442
+ @voice({1 2 3}, 钢琴, 男=ha ha ha),
443
+ @voice({3 4 5}, , "la la la")
439
444
  )
440
- 语法糖:当多个 voice 用 voice 的语法糖写在一起时,会自动创建一个 voices 组件包裹它们。
441
- 例:
442
- N(钢琴): C1 D1 E1
445
+ ~~~
446
+
447
+ 接受多个声部作为位置参数,将它们按时间对齐、分行排布;声部名称和歌词由各自的 \`@voice\` 提供
448
+
449
+ **简写**:连续声明多个 \`N:\` 声部及其 \`L:\` 歌词,会自动组成 \`voices\`
450
+
451
+ ~~~jpfun
452
+ N(钢琴): 1 2 3
443
453
  L(男): ha ha ha
444
- N: C2 D2 E2
454
+ N: 3 4 5
445
455
  L: la la la
446
- `,
456
+ ~~~`,
447
457
  allowExtraArgs: true,
448
458
  extraArgType: "content",
449
459
  args: []
@@ -472,8 +482,8 @@ L: la la la
472
482
  const names = [];
473
483
  ctx.beginLoweringGroup(this, {
474
484
  attachment: new VoicesBraceAttachment(names, this),
475
- onTemporal(node) {
476
- if (node instanceof VoiceNameTemporal)
485
+ onTemporal: node => {
486
+ if (node instanceof VoiceNameTemporal && node.ast.parent === this)
477
487
  names.push(node);
478
488
  },
479
489
  });