jpfun 0.7.0 → 0.9.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 (152) hide show
  1. package/Bravura-LICENSE.txt +93 -0
  2. package/README.md +49 -146
  3. package/dist/Bravura-LICENSE.txt +93 -0
  4. package/dist/converter/midi/convert.d.ts +0 -10
  5. package/dist/converter/midi/convert.js +19 -56
  6. package/dist/converter/musicxml/features.d.ts +16 -8
  7. package/dist/converter/musicxml/features.js +88 -19
  8. package/dist/converter/musicxml/index.js +88 -50
  9. package/dist/converter/musicxml/model.d.ts +9 -0
  10. package/dist/converter/source.d.ts +4 -7
  11. package/dist/converter/source.js +11 -9
  12. package/dist/core.d.ts +1 -0
  13. package/dist/core.js +1 -0
  14. package/dist/diagnostic.d.ts +7 -1
  15. package/dist/diagnostic.js +40 -88
  16. package/dist/fraction.js +3 -2
  17. package/dist/functions/ASTtypes.d.ts +3 -32
  18. package/dist/functions/ASTtypes.js +5 -52
  19. package/dist/functions/adjust/index.js +45 -33
  20. package/dist/functions/arpeggio/index.js +50 -55
  21. package/dist/functions/arpeggio/shape.d.ts +2 -3
  22. package/dist/functions/arpeggio/shape.js +20 -50
  23. package/dist/functions/bar/index.d.ts +8 -0
  24. package/dist/functions/bar/index.js +105 -8
  25. package/dist/functions/beam/auto.d.ts +1 -2
  26. package/dist/functions/beam/auto.js +80 -135
  27. package/dist/functions/beam/index.js +6 -12
  28. package/dist/functions/beam/layout.d.ts +1 -1
  29. package/dist/functions/beam/layout.js +1 -1
  30. package/dist/functions/box/index.js +0 -6
  31. package/dist/functions/br/index.d.ts +34 -2
  32. package/dist/functions/br/index.js +5 -4
  33. package/dist/functions/dash/index.js +9 -10
  34. package/dist/functions/default.js +2 -1
  35. package/dist/functions/div/index.d.ts +3 -4
  36. package/dist/functions/div/index.js +18 -14
  37. package/dist/functions/dot/index.js +5 -11
  38. package/dist/functions/dyn/index.js +55 -57
  39. package/dist/functions/gliss/index.js +131 -103
  40. package/dist/functions/grace/index.d.ts +3 -12
  41. package/dist/functions/grace/index.js +52 -102
  42. package/dist/functions/head/index.js +10 -7
  43. package/dist/functions/key/index.js +13 -15
  44. package/dist/functions/meter/index.d.ts +56 -1
  45. package/dist/functions/meter/index.js +15 -10
  46. package/dist/functions/note/index.js +6 -6
  47. package/dist/functions/note/noteName.d.ts +16 -0
  48. package/dist/functions/note/noteName.js +88 -0
  49. package/dist/functions/note/source-transform.js +8 -10
  50. package/dist/functions/ottava/index.js +35 -40
  51. package/dist/functions/page/index.d.ts +1 -0
  52. package/dist/functions/page/index.js +27 -21
  53. package/dist/functions/pagebreak/index.js +2 -2
  54. package/dist/functions/rest/index.js +5 -5
  55. package/dist/functions/set/index.js +4 -4
  56. package/dist/functions/slide/index.js +0 -1
  57. package/dist/functions/stack/index.js +8 -57
  58. package/dist/functions/symbol/index.d.ts +21 -2
  59. package/dist/functions/symbol/index.js +69 -24
  60. package/dist/functions/symbol/ornament.d.ts +1 -2
  61. package/dist/functions/symbol/shape.d.ts +6 -0
  62. package/dist/functions/symbol/shape.js +16 -3
  63. package/dist/functions/symbol/symbols/coda.d.ts +4 -0
  64. package/dist/functions/symbol/symbols/coda.js +56 -0
  65. package/dist/functions/symbol/symbols/dynamics.d.ts +1 -1
  66. package/dist/functions/symbol/symbols/dynamics.js +18 -8
  67. package/dist/functions/symbol/symbols/harmonic.d.ts +2 -0
  68. package/dist/functions/symbol/symbols/harmonic.js +11 -0
  69. package/dist/functions/symbol/symbols/marcato.d.ts +2 -0
  70. package/dist/functions/symbol/symbols/marcato.js +22 -0
  71. package/dist/functions/symbol/symbols/staccato.d.ts +2 -0
  72. package/dist/functions/symbol/symbols/staccato.js +24 -0
  73. package/dist/functions/symbol/symbols/tenuto.d.ts +2 -0
  74. package/dist/functions/symbol/symbols/tenuto.js +11 -0
  75. package/dist/functions/symbol/symbols/tocoda.d.ts +2 -0
  76. package/dist/functions/symbol/symbols/tocoda.js +16 -0
  77. package/dist/functions/symbol/symbols/turn.d.ts +3 -0
  78. package/dist/functions/symbol/symbols/turn.js +50 -0
  79. package/dist/functions/symbol/symbols/wheel.d.ts +2 -0
  80. package/dist/functions/symbol/symbols/wheel.js +36 -0
  81. package/dist/functions/temporal.d.ts +3 -2
  82. package/dist/functions/temporal.js +8 -0
  83. package/dist/functions/text/index.js +38 -35
  84. package/dist/functions/tie/index.js +44 -43
  85. package/dist/functions/tremolo/index.d.ts +2 -0
  86. package/dist/functions/tremolo/index.js +318 -0
  87. package/dist/functions/tuplet/index.js +4 -14
  88. package/dist/functions/up/index.js +81 -85
  89. package/dist/functions/voice/connections.d.ts +2 -1
  90. package/dist/functions/voice/connections.js +48 -10
  91. package/dist/functions/voice/index.d.ts +2 -3
  92. package/dist/functions/voice/index.js +2 -963
  93. package/dist/functions/voice/lyrics.d.ts +30 -0
  94. package/dist/functions/voice/lyrics.js +289 -0
  95. package/dist/functions/voice/voice.d.ts +63 -0
  96. package/dist/functions/voice/voice.js +373 -0
  97. package/dist/functions/voice/voices.d.ts +106 -0
  98. package/dist/functions/voice/voices.js +423 -0
  99. package/dist/functions/volta/index.js +5 -23
  100. package/dist/jpfun.core.min.js +76 -59
  101. package/dist/jpfun.from-midi.min.js +5 -5
  102. package/dist/jpfun.from-musicxml.min.js +8 -8
  103. package/dist/jpfun.min.js +81 -64
  104. package/dist/layout/default.d.ts +0 -1
  105. package/dist/layout/default.js +0 -1
  106. package/dist/layout/engine.js +30 -18
  107. package/dist/layout/model.d.ts +0 -1
  108. package/dist/layout/model.js +1 -4
  109. package/dist/layout/page.d.ts +2 -1
  110. package/dist/layout/page.js +14 -14
  111. package/dist/layout/range.d.ts +7 -6
  112. package/dist/layout/range.js +23 -14
  113. package/dist/layout/types.d.ts +9 -28
  114. package/dist/lowering/loweringContext.d.ts +18 -23
  115. package/dist/lowering/loweringContext.js +33 -46
  116. package/dist/lowering/track.d.ts +8 -1
  117. package/dist/lowering/types.d.ts +13 -11
  118. package/dist/parser/parse-utils/call-utils.d.ts +1 -2
  119. package/dist/parser/parse-utils/call-utils.js +6 -34
  120. package/dist/parser/parse-utils/note-utils.d.ts +3 -3
  121. package/dist/parser/parse-utils/note-utils.js +21 -33
  122. package/dist/parser/parse-utils/string-utils.d.ts +3 -2
  123. package/dist/parser/parse-utils/string-utils.js +20 -5
  124. package/dist/parser/parserContext.d.ts +9 -9
  125. package/dist/parser/parserContext.js +62 -11
  126. package/dist/pipeline.js +2 -3
  127. package/dist/playback/compile.js +131 -82
  128. package/dist/playback/event.d.ts +6 -6
  129. package/dist/playback/event.js +13 -49
  130. package/dist/playback/time.js +42 -31
  131. package/dist/playback/types.d.ts +3 -1
  132. package/dist/render/canvas.js +4 -22
  133. package/dist/render/recording.js +1 -4
  134. package/dist/render/svg.d.ts +0 -1
  135. package/dist/render/svg.js +33 -31
  136. package/dist/render/text.d.ts +36 -1
  137. package/dist/render/text.js +134 -0
  138. package/dist/render/translate.d.ts +0 -1
  139. package/dist/render/translate.js +6 -23
  140. package/dist/render/types.d.ts +0 -2
  141. package/grammar.md +39 -30
  142. package/package.json +3 -3
  143. package/dist/functions/note/noteNameFSM.d.ts +0 -8
  144. package/dist/functions/note/noteNameFSM.js +0 -181
  145. package/dist/iife/core.d.ts +0 -1
  146. package/dist/iife/core.js +0 -3
  147. package/dist/iife/from-midi.d.ts +0 -1
  148. package/dist/iife/from-midi.js +0 -15
  149. package/dist/iife/from-musicxml.d.ts +0 -1
  150. package/dist/iife/from-musicxml.js +0 -3
  151. package/dist/iife/full.d.ts +0 -1
  152. package/dist/iife/full.js +0 -3
@@ -1,8 +1,28 @@
1
- import { ASTNodeBase, ASTFunctionNode, ASTTextNode, ASTLabelNode, ASTBraceNode } from "../ASTtypes.js";
1
+ import { ASTFunctionNode } from "../ASTtypes.js";
2
2
  import { ErrorDiagnostic } from "../../diagnostic.js";
3
3
  import { ANCHOR_KEY, DEFAULT_KEY, transferFunctionAddons, isVisualTemporalNode, TemporalNodeBase, } from "../temporal.js";
4
4
  import { Fraction } from "../../fraction.js";
5
5
  import { getLayoutBounds, layoutLocalSequence } from "../../layout/engine.js";
6
+ const FOLD_ARGS = [{
7
+ name: "gap",
8
+ namedOnly: true,
9
+ type: "length",
10
+ default: { value: 0.12, unit: "em" },
11
+ description: "相邻堆叠成员之间的间距",
12
+ }];
13
+ function formatStack(fn, host, members, gaps, source) {
14
+ let result = host;
15
+ for (let start = 0; start < members.length;) {
16
+ const gap = gaps[start];
17
+ let end = start + 1;
18
+ while (end < members.length && gaps[end] === gap)
19
+ end++;
20
+ const items = members.slice(start, end).map(item => item.toString(source));
21
+ result = `@${fn}(${[result, ...items, `gap=${gap}px`].join(", ")})`;
22
+ start = end;
23
+ }
24
+ return result;
25
+ }
6
26
  /**
7
27
  * `@up` / `@down` 共用的折叠容器,不导出
8
28
  *
@@ -13,24 +33,27 @@ import { getLayoutBounds, layoutLocalSequence } from "../../layout/engine.js";
13
33
  class FoldFunction extends ASTFunctionNode {
14
34
  contents = [];
15
35
  belows = [];
16
- size; // 用于计算 gap
36
+ aboveGaps = [];
37
+ belowGaps = [];
38
+ size;
39
+ gap;
17
40
  /** 静态而不是实例字段:子类的字段初始化器晚于基类构造器,构造期读不到 */
18
41
  get side() { return this.constructor.side; }
19
42
  get children() {
20
43
  return this.belows.length ? [...this.contents, ...this.belows] : this.contents;
21
44
  }
45
+ timeFlowModel() { return null; }
22
46
  constructor(span, args, ctx, parent = null) {
23
47
  super(span, parent);
24
48
  this.size = ctx.variables.fontsize;
25
- for (const [, value] of args) {
26
- if (value instanceof ASTNodeBase) {
49
+ const [gap] = this.getArgValue(args, ctx);
50
+ this.gap = ctx.length2px(gap);
51
+ if (!Number.isFinite(this.gap) || this.gap < 0) {
52
+ throw new ErrorDiagnostic("E_INVALID_FOLD_GAP", "@{fn} 的 gap 必须是非负有限长度", span, { fn: this.callName });
53
+ }
54
+ for (const [key, value] of args) {
55
+ if (typeof key === "number")
27
56
  this.addContent(value, this.side, ctx);
28
- continue;
29
- }
30
- const c = ctx.parseArgWithType(value.valueSpan, "content", span.start);
31
- if (c !== null) {
32
- this.addContent(c, this.side, ctx);
33
- }
34
57
  }
35
58
  }
36
59
  /** 至少有一个成员能承载标签时,折叠体本身才能作为整体被标注 */
@@ -38,12 +61,20 @@ class FoldFunction extends ASTFunctionNode {
38
61
  return this.children.some(ASTFunctionNode.findLabelable) ? this : null;
39
62
  }
40
63
  /** 遇到另一个折叠体就展平到自己身上;返回 this 只是为了链式写法 */
41
- addContent(node, side, ctx) {
64
+ addContent(node, side, ctx, gap = this.gap) {
42
65
  if (this.contents.length === 0)
43
66
  side = "above"; // 还没有宿主,来者即宿主
44
67
  if (node instanceof FoldFunction)
45
- return this.flatten(node, side, ctx);
46
- (side === "above" ? this.contents : this.belows).push(node);
68
+ return this.flatten(node, side, ctx, gap);
69
+ if (side === "above") {
70
+ if (this.contents.length > 0)
71
+ this.aboveGaps.push(gap);
72
+ this.contents.push(node);
73
+ }
74
+ else {
75
+ this.belowGaps.push(gap);
76
+ this.belows.push(node);
77
+ }
47
78
  node.parent = this;
48
79
  this.growSpan(node.sourceSpan);
49
80
  return this;
@@ -70,11 +101,11 @@ class FoldFunction extends ASTFunctionNode {
70
101
  }
71
102
  /** 混写方向时输出等价的嵌套形式,hover 的替换按钮才能生成合法代码 */
72
103
  toString(source) {
73
- const above = this.contents.map(c => c.toString(source));
74
- if (this.belows.length === 0)
75
- return `@up(${above.join(", ")})`;
76
- const host = above.length > 1 ? `@up(${above.join(", ")})` : above[0];
77
- return `@down(${[host, ...this.belows.map(c => c.toString(source))].join(", ")})`;
104
+ if (this.contents.length <= 1 && this.belows.length === 0) {
105
+ return `@${this.callName}(${[...this.contents.map(c => c.toString(source)), `gap=${this.gap}px`].join(", ")})`;
106
+ }
107
+ const up = formatStack("up", this.contents[0].toString(source), this.contents.slice(1), this.aboveGaps, source);
108
+ return formatStack("down", up, this.belows, this.belowGaps, source);
78
109
  }
79
110
  /**
80
111
  * 把另一个折叠容器并进自己:**接收者存活**,参数被掏空
@@ -84,29 +115,29 @@ class FoldFunction extends ASTFunctionNode {
84
115
  *
85
116
  * 被掏空的一方必须撤销候选登记:空壳已不在 AST 里,留在表中会让 @tie() 取到孤儿端点。
86
117
  */
87
- flatten(drop, side, ctx) {
118
+ flatten(drop, side, ctx, gap = this.gap) {
88
119
  // 成员顺序跟随源码;比较必须在 growSpan 之前
89
120
  const front = drop.sourceSpan.start < this.sourceSpan.start ? drop : this;
90
121
  const back = front === drop ? this : drop;
91
122
  const [backHost, ...backAbove] = back.contents;
123
+ const hasHost = front.contents.length > 0;
92
124
  const demoted = backHost ? [backHost] : [];
93
- const contents = [...front.contents, ...(side === "above" ? demoted : []), ...backAbove];
94
- const belows = [...front.belows, ...(side === "below" ? demoted : []), ...back.belows];
95
- this.contents = contents;
96
- this.belows = belows;
125
+ const demotedGap = hasHost && backHost ? [gap] : [];
126
+ const above = !hasHost || side === "above";
127
+ this.contents = [...front.contents, ...(above ? demoted : []), ...backAbove];
128
+ this.belows = [...front.belows, ...(!above ? demoted : []), ...back.belows];
129
+ this.aboveGaps = [...front.aboveGaps, ...(above ? demotedGap : []), ...back.aboveGaps];
130
+ this.belowGaps = [...front.belowGaps, ...(!above ? demotedGap : []), ...back.belowGaps];
97
131
  for (const c of this.children)
98
132
  c.parent = this;
99
133
  this.growSpan(drop.sourceSpan);
100
- drop.contents = [];
101
- drop.belows = [];
102
- const stale = ctx.labelableNodes.indexOf(drop);
134
+ drop.contents.length = drop.belows.length = drop.aboveGaps.length = drop.belowGaps.length = 0;
135
+ const stale = ctx.labelableNodes.lastIndexOf(drop);
103
136
  if (stale >= 0)
104
137
  ctx.labelableNodes.splice(stale, 1);
105
138
  return this;
106
139
  }
107
140
  /**
108
- * 左操作数的取法同 stack;合并策略不同:折叠要让被引用的一方存活
109
- *
110
141
  * `this` 是发起调用的那个子类,所以方向和构造都从它取。子类必须写成
111
142
  * `UpFunction.desugar(...)` 这种带显式接收者的形式 —— 解析器是把
112
143
  * `deSugarRelation` 摘成游离函数引用来调的,`this` 到不了这里。
@@ -116,62 +147,26 @@ class FoldFunction extends ASTFunctionNode {
116
147
  if (!(n.kind === "sugar" && n.data === this))
117
148
  return null;
118
149
  const label = this.side === "above" ? "@up" : "@down";
119
- // 向前跳过文本节点,实现忽略中间内容的作用
120
- let left = ctx.nodes.length - 1;
121
- while (left >= 0 && ctx.nodes[left] instanceof ASTTextNode)
122
- left--;
123
- if (left < 0) {
124
- throw new ErrorDiagnostic("E_FOLD_NO_TARGET", `${label}语法糖错误: 左边没有找到可叠加的目标`, n.span);
125
- }
126
- let leftNode = ctx.nodes[left];
127
- /** 左操作数在 ctx.nodes 中的起点;只有这一段会被折叠体吞并,更早的节点必须保留 */
128
- let replaceFrom = left;
129
- // 对 label 的特判: 目标变为label到被标记的节点范围内的所有节点
130
- if (leftNode instanceof ASTLabelNode) {
131
- const labelNode = leftNode;
132
- let tgt = labelNode.target;
133
- // 在本层这个节点位于哪里(标签节点可能被wrap)
134
- while (tgt && !ctx.nodes.includes(tgt))
135
- tgt = tgt.parent;
136
- for (let j = left - 1; j >= 0; j--) {
137
- if (ctx.nodes[j] === tgt) {
138
- leftNode = new ASTBraceNode({
139
- start: tgt.sourceSpan.start,
140
- end: labelNode.sourceSpan.end,
141
- }, ctx.nodes.slice(j, left + 1), null);
142
- replaceFrom = j;
143
- break;
144
- }
145
- }
146
- }
147
- // 左操作数收敛成一个折叠体,后续合并都在它身上进行
150
+ const leftNode = ctx.takeLeftOperand();
151
+ if (!leftNode)
152
+ throw new ErrorDiagnostic("E_FOLD_NO_TARGET", "{fn}语法糖错误: 左边没有找到可叠加的目标", n.span, { fn: label });
153
+ const wrapper = new this({ ...n.span }, new Map(), ctx);
148
154
  const host = leftNode instanceof FoldFunction
149
155
  ? leftNode
150
- : new this(n.span, new Map(), ctx).addContent(leftNode, "above", ctx);
151
- // 找到下一个非文本节点 通过全量后续解析的方式进行 还是有些trick
152
- const storage = ctx.nodes;
153
- ctx.nodes = [];
154
- ctx.makeNodes(nodes, at);
155
- for (let i = 0; i < ctx.nodes.length; i++) {
156
- // 后向跳过文本节点 和上面保持一致
157
- const right = ctx.nodes[i];
158
- if (right instanceof ASTTextNode)
159
- continue;
156
+ : wrapper.addContent(leftNode, "above", ctx);
157
+ const next = ctx.consumeRightOperand(nodes, at, right => {
160
158
  // 右操作数可能已被 @tie() 等按引用取走(它就在刚才解析的剩余里),
161
159
  // 而新建的左包装无人引用,所以合并时让右边存活;接收者就是存活的那一方
162
160
  const merged = right instanceof FoldFunction
163
- ? right.flatten(host, this.side, ctx)
164
- : host.addContent(right, this.side, ctx);
165
- storage.length = replaceFrom;
166
- storage.push(merged);
167
- while (++i < ctx.nodes.length)
168
- storage.push(ctx.nodes[i]);
169
- ctx.nodes = storage;
161
+ ? right.flatten(host, this.side, ctx, wrapper.gap)
162
+ : host.addContent(right, this.side, ctx, wrapper.gap);
170
163
  // 语法糖不走 pushNode,这里补登记;null 同样入表,和显式调用一样成为边界
171
164
  ctx.labelableNodes.push(merged.labelable());
172
- return nodes.length;
173
- }
174
- throw new ErrorDiagnostic("E_FOLD_NO_TARGET", `${label}语法糖错误: 右边没有找到可叠加的目标`, n.span);
165
+ return merged;
166
+ });
167
+ if (next !== null)
168
+ return next;
169
+ throw new ErrorDiagnostic("E_FOLD_NO_TARGET", "{fn}语法糖错误: 右边没有找到可叠加的目标", n.span, { fn: label });
175
170
  }
176
171
  }
177
172
  class UpFunction extends FoldFunction {
@@ -181,15 +176,15 @@ class UpFunction extends FoldFunction {
181
176
  description: "把同一时间位置的可见对象向上堆叠",
182
177
  details: `\
183
178
  ~~~jpfun
184
- @up(1, 3, 5)
179
+ @up(1, 3, 5, gap=0.12em)
185
180
  ~~~
186
181
 
187
- 第一个内容参数为宿主,其余依次叠在上方;所有成员共享宿主时值。常用于和弦、速度记号和音符注释。
182
+ 第一个内容参数为宿主,其余依次叠在上方;所有成员共享宿主时值。仅命名参数 \`gap\` 指定相邻成员的间距。常用于和弦、速度记号和音符注释。
188
183
 
189
184
  **简写**:\`1 ^ 3 ^ 5\`。复杂内容可用 \`{...}\` 包裹;需要独立节奏的并行分支时,使用 \`&\` / \`@stack\`。`,
190
185
  allowExtraArgs: true,
191
186
  extraArgType: "content",
192
- args: [],
187
+ args: FOLD_ARGS,
193
188
  };
194
189
  static deSugarAtom(source, start, _end) {
195
190
  if (source[start] !== "^")
@@ -208,14 +203,14 @@ class DownFunction extends FoldFunction {
208
203
  description: "把同一时间位置的可见对象向下堆叠",
209
204
  details: `\
210
205
  ~~~jpfun
211
- @down(1, $p)
206
+ @down(1, $p, gap=0.12em)
212
207
  ~~~
213
- 第一个内容参数为宿主,其余依次叠在下方并共享其时值,常用于力度等下方标记。
208
+ 第一个内容参数为宿主,其余依次叠在下方并共享其时值,仅命名参数 \`gap\` 指定相邻成员的间距,常用于力度等下方标记。
214
209
 
215
210
  **简写**:\`1 _ $p\`。与 \`^\` 混写时共用同一宿主:\`1^2_3\` 将 \`2\` 放在 \`1\` 上方、\`3\` 放在下方。`,
216
211
  allowExtraArgs: true,
217
212
  extraArgType: "content",
218
- args: [],
213
+ args: FOLD_ARGS,
219
214
  };
220
215
  static deSugarAtom(source, start, _end) {
221
216
  if (source[start] !== "_")
@@ -329,23 +324,24 @@ class FoldTemporal extends TemporalNodeBase {
329
324
  this.box.w = layoutLocalSequence([first], context);
330
325
  for (let index = 1; index < this.members.length; index++)
331
326
  layoutLocalSequence([this.members[index]], context);
332
- this.springConfig = { ...first.springConfig, ...this.springConfig };
327
+ this.springConfig = { ...first.springConfig };
333
328
  // 横向完全等于宿主:上下的标记(变速、注释、力度)常常比音符宽得多,
334
329
  // 让它们撑宽盒子会把右邻推开一大截;它们画在基线外侧,伸出盒外也不会碰撞
335
330
  const anchor = first.box.x + first.box.anchor;
336
- const gap = this.ast.size * 0.12;
337
331
  this.verticalOffsets.length = this.members.length;
338
332
  this.verticalOffsets[0] = 0;
339
333
  // 以宿主盒顶为 0,向上得到负坐标、向下得到正坐标,最后整体下移
340
334
  const firstBounds = getLayoutBounds(first);
341
335
  let top = firstBounds.y;
342
336
  for (let i = 1; i < this.aboveCount; i++) {
337
+ const gap = this.ast.aboveGaps[i - 1];
343
338
  const bounds = getLayoutBounds(this.members[i]);
344
339
  this.verticalOffsets[i] = top - gap - bounds.y - bounds.h;
345
340
  top -= gap + bounds.h;
346
341
  }
347
342
  let bottom = firstBounds.y + firstBounds.h;
348
343
  for (let i = this.aboveCount; i < this.members.length; i++) {
344
+ const gap = this.ast.belowGaps[i - this.aboveCount];
349
345
  const bounds = getLayoutBounds(this.members[i]);
350
346
  this.verticalOffsets[i] = bottom + gap - bounds.y;
351
347
  bottom += gap + bounds.h;
@@ -11,9 +11,10 @@ export interface ConnectionSpec {
11
11
  kind: ConnectionKind;
12
12
  from?: number;
13
13
  to?: number;
14
+ name?: string;
14
15
  }
15
16
  /** 开放端点在声部收集完毕后补全 */
16
- export type VoiceConnection = Required<ConnectionSpec>;
17
+ export type VoiceConnection = ConnectionSpec & Required<Pick<ConnectionSpec, "from" | "to">>;
17
18
  export declare function parseConnections(value: string, span: SourceSpan): ConnectionSpec[];
18
19
  export declare function resolveConnections(specs: readonly ConnectionSpec[], count: number, span: SourceSpan): VoiceConnection[];
19
20
  export declare function serializeConnections(specs: readonly ConnectionSpec[]): string;
@@ -1,5 +1,6 @@
1
1
  import { ErrorDiagnostic } from "../../diagnostic.js";
2
2
  import { pathBounds } from "../../layout/path.js";
3
+ import { findClosingQuote, parseString, quote } from "../../parser/parse-utils/string-utils.js";
3
4
  export const CURVED_BRACE_COMMANDS = [
4
5
  { op: "M", x: 2.98438, y: -165.5 },
5
6
  { op: "C", cx1: 8.9375, cy1: -171.781, cx2: 20.5156, cy2: -184.688, x: 20.5156, y: -208.531 },
@@ -47,19 +48,24 @@ export const BRACKET_HOOK_COMMANDS = [
47
48
  },
48
49
  { op: "Z" },
49
50
  ];
51
+ const CONNECTION_RANGE_RE = /\s*([\[{])\s*(?:(?:(\d+)\s*)?-\s*(?:(\d+)\s*)?|(\d+)\s*)/y;
52
+ const WHITESPACE_RE = /\s/;
53
+ const FORBIDDEN_RAW_NAME_RE = /[\[\]{}:"\r\n]/;
50
54
  function invalidConnection(span) {
51
- return new ErrorDiagnostic("E_VOICES_CONNECT", '@voices 的 connect 必须是 [起点-终点]、{起点-终点}、[编号] 或 {编号} 的组合;范围端点可省略,否则须为有效声部编号。空字符串表示只有连谱线。', span);
55
+ return new ErrorDiagnostic("E_VOICES_CONNECT", '@voices 的 connect 必须是 [起点-终点]、{起点-终点}、[编号] 或 {编号}(可用 :名称 指定分组名)的组合;范围端点可省略,否则须为有效声部编号。空字符串表示只有连谱线。', span);
52
56
  }
53
57
  export function parseConnections(value, span) {
54
58
  const source = value.trim();
55
59
  // 粘连匹配必须消费整串,不能跳过非法字符
56
60
  // 非空数字与其后空白一起可选,避免省略端点时重复回溯
57
- const pattern = /\s*([\[{])\s*(?:(?:(\d+)\s*)?-\s*(?:(\d+)\s*)?|(\d+)\s*)([\]}])\s*/y;
58
61
  const result = [];
59
- while (pattern.lastIndex < source.length) {
60
- const match = pattern.exec(source);
61
- if (!match || match[5] !== (match[1] === "[" ? "]" : "}"))
62
+ for (let pos = 0; pos < source.length; pos++) {
63
+ CONNECTION_RANGE_RE.lastIndex = pos;
64
+ const match = CONNECTION_RANGE_RE.exec(source);
65
+ if (!match)
62
66
  throw invalidConnection(span);
67
+ pos = CONNECTION_RANGE_RE.lastIndex;
68
+ const close = match[1] === "[" ? "]" : "}";
63
69
  const fromText = match[4] ?? match[2];
64
70
  const toText = match[4] ?? match[3];
65
71
  const from = fromText ? Number(fromText) : undefined;
@@ -71,20 +77,52 @@ export function parseConnections(value, span) {
71
77
  }
72
78
  if (from !== undefined && to !== undefined && from > to)
73
79
  throw invalidConnection(span);
74
- result.push({ kind: match[1] === "[" ? "bracket" : "brace", from, to });
80
+ let name;
81
+ if (source[pos] === ":") {
82
+ pos++;
83
+ while (pos < source.length && WHITESPACE_RE.test(source[pos]))
84
+ pos++;
85
+ if (source[pos] === '"') {
86
+ const closeQuote = findClosingQuote(source, pos, source.length);
87
+ const parsed = closeQuote < 0 ? null : parseString(source.slice(pos, closeQuote + 1));
88
+ if (typeof parsed !== "string" || !parsed)
89
+ throw invalidConnection(span);
90
+ name = parsed;
91
+ pos = closeQuote + 1;
92
+ while (pos < source.length && WHITESPACE_RE.test(source[pos]))
93
+ pos++;
94
+ }
95
+ else {
96
+ const closeAt = source.indexOf(close, pos);
97
+ const raw = closeAt < 0 ? "" : source.slice(pos, closeAt);
98
+ name = FORBIDDEN_RAW_NAME_RE.test(raw) ? "" : raw.trim();
99
+ if (!name)
100
+ throw invalidConnection(span);
101
+ pos = closeAt;
102
+ }
103
+ }
104
+ if (source[pos] !== close)
105
+ throw invalidConnection(span);
106
+ result.push({
107
+ kind: match[1] === "[" ? "bracket" : "brace",
108
+ from,
109
+ to,
110
+ ...(name ? { name } : {}),
111
+ });
75
112
  }
76
113
  return result;
77
114
  }
78
115
  export function resolveConnections(specs, count, span) {
79
- return specs.map(({ kind, from = 1, to = count }) => {
116
+ return specs.map(({ kind, from = 1, to = count, name }) => {
80
117
  if (from > to || to > count)
81
118
  throw invalidConnection(span);
82
- return { kind, from, to };
119
+ return { kind, from, to, ...(name ? { name } : {}) };
83
120
  });
84
121
  }
85
122
  export function serializeConnections(specs) {
86
- return specs.map(({ kind, from, to }) => {
87
- const range = `${from ?? ""}-${to ?? ""}`;
123
+ return specs.map(({ kind, from, to, name }) => {
124
+ const label = !name ? "" : `:${FORBIDDEN_RAW_NAME_RE.test(name) || name.trim() !== name ? quote(name) : name}`;
125
+ const range = `${from ?? ""}-${to ?? ""}${label}`;
88
126
  return kind === "brace" ? `{${range}}` : `[${range}]`;
89
127
  }).join("");
90
128
  }
@@ -1,3 +1,2 @@
1
- import { ASTFunctionClass } from "../ASTtypes.js";
2
- export declare const VoiceNode: ASTFunctionClass;
3
- export declare const VoicesNode: ASTFunctionClass;
1
+ export { VoiceNode } from "./voice.js";
2
+ export { VoicesNode } from "./voices.js";