jpfun 0.8.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (140) hide show
  1. package/README.md +10 -15
  2. package/dist/diagnostic.d.ts +29 -2
  3. package/dist/diagnostic.js +5 -4
  4. package/dist/functions/ASTtypes.d.ts +1 -14
  5. package/dist/functions/ASTtypes.js +5 -36
  6. package/dist/functions/adjust/index.js +45 -33
  7. package/dist/functions/arpeggio/index.js +44 -48
  8. package/dist/functions/arpeggio/shape.d.ts +2 -3
  9. package/dist/functions/arpeggio/shape.js +20 -50
  10. package/dist/functions/bar/index.d.ts +3 -0
  11. package/dist/functions/bar/index.js +21 -11
  12. package/dist/functions/beam/auto.js +54 -123
  13. package/dist/functions/beam/index.js +4 -9
  14. package/dist/functions/beam/layout.js +3 -2
  15. package/dist/functions/box/index.js +1 -6
  16. package/dist/functions/dash/index.js +6 -4
  17. package/dist/functions/div/index.d.ts +3 -5
  18. package/dist/functions/div/index.js +14 -12
  19. package/dist/functions/dot/index.js +3 -9
  20. package/dist/functions/dyn/index.js +7 -26
  21. package/dist/functions/gliss/index.js +99 -91
  22. package/dist/functions/grace/index.d.ts +2 -5
  23. package/dist/functions/grace/index.js +47 -94
  24. package/dist/functions/head/index.js +32 -36
  25. package/dist/functions/key/index.js +9 -12
  26. package/dist/functions/meter/index.d.ts +1 -0
  27. package/dist/functions/meter/index.js +6 -3
  28. package/dist/functions/meter/shape.d.ts +1 -1
  29. package/dist/functions/note/accidentals.d.ts +3 -3
  30. package/dist/functions/note/accidentals.js +29 -16
  31. package/dist/functions/note/index.js +12 -14
  32. package/dist/functions/note/source-transform.d.ts +15 -0
  33. package/dist/functions/note/source-transform.js +75 -71
  34. package/dist/functions/ottava/index.js +12 -32
  35. package/dist/functions/page/index.js +15 -17
  36. package/dist/functions/rest/index.js +10 -10
  37. package/dist/functions/slide/index.js +1 -1
  38. package/dist/functions/stack/index.js +8 -57
  39. package/dist/functions/temporal.d.ts +11 -5
  40. package/dist/functions/temporal.js +22 -2
  41. package/dist/functions/text/index.js +24 -30
  42. package/dist/functions/tie/index.js +42 -41
  43. package/dist/functions/tremolo/index.js +1 -1
  44. package/dist/functions/tuplet/index.js +1 -6
  45. package/dist/functions/up/index.js +82 -88
  46. package/dist/functions/voice/connections.d.ts +3 -2
  47. package/dist/functions/voice/connections.js +48 -10
  48. package/dist/functions/voice/index.d.ts +2 -3
  49. package/dist/functions/voice/index.js +2 -970
  50. package/dist/functions/voice/lyrics.d.ts +34 -0
  51. package/dist/functions/voice/lyrics.js +393 -0
  52. package/dist/functions/voice/voice.d.ts +67 -0
  53. package/dist/functions/voice/voice.js +405 -0
  54. package/dist/functions/voice/voices.d.ts +106 -0
  55. package/dist/functions/voice/voices.js +424 -0
  56. package/dist/functions/volta/index.js +6 -23
  57. package/dist/index.d.ts +97 -3
  58. package/dist/index.js +97 -3
  59. package/dist/jpfun.min.js +63 -67
  60. package/dist/layout/engine.d.ts +4 -3
  61. package/dist/layout/engine.js +190 -24
  62. package/dist/layout/model.d.ts +12 -4
  63. package/dist/layout/model.js +16 -11
  64. package/dist/layout/page.d.ts +2 -1
  65. package/dist/layout/page.js +15 -15
  66. package/dist/layout/path.js +1 -1
  67. package/dist/layout/range.d.ts +7 -6
  68. package/dist/layout/range.js +23 -14
  69. package/dist/layout/types.d.ts +19 -30
  70. package/dist/lowering/loweringContext.d.ts +8 -11
  71. package/dist/lowering/loweringContext.js +7 -13
  72. package/dist/lowering/track.d.ts +8 -1
  73. package/dist/lowering/types.d.ts +12 -8
  74. package/dist/parser/grammarType.d.ts +3 -3
  75. package/dist/parser/parse-utils/brace-utils.js +7 -17
  76. package/dist/parser/parse-utils/call-utils.d.ts +4 -3
  77. package/dist/parser/parse-utils/call-utils.js +23 -66
  78. package/dist/parser/parse-utils/note-utils.d.ts +7 -3
  79. package/dist/parser/parse-utils/note-utils.js +25 -40
  80. package/dist/parser/parse-utils/string-utils.d.ts +1 -1
  81. package/dist/parser/parse-utils/string-utils.js +8 -6
  82. package/dist/parser/parserContext.d.ts +17 -11
  83. package/dist/parser/parserContext.js +111 -65
  84. package/dist/parser/preprocess.d.ts +7 -0
  85. package/dist/parser/preprocess.js +21 -19
  86. package/dist/pipeline.d.ts +1 -1
  87. package/dist/pipeline.js +2 -2
  88. package/dist/playback/compile.js +14 -15
  89. package/dist/playback/event.d.ts +6 -6
  90. package/dist/playback/event.js +13 -49
  91. package/dist/render/canvas.d.ts +0 -2
  92. package/dist/render/canvas.js +50 -56
  93. package/dist/render/index.d.ts +1 -1
  94. package/dist/render/index.js +1 -1
  95. package/dist/render/paint.d.ts +7 -0
  96. package/dist/render/paint.js +70 -20
  97. package/dist/render/recording.d.ts +8 -1
  98. package/dist/render/recording.js +12 -0
  99. package/dist/render/svg.d.ts +5 -2
  100. package/dist/render/svg.js +70 -48
  101. package/dist/render/text.d.ts +37 -2
  102. package/dist/render/text.js +136 -1
  103. package/dist/render/translate.d.ts +3 -1
  104. package/dist/render/translate.js +6 -0
  105. package/dist/render/types.d.ts +9 -2
  106. package/grammar.md +18 -14
  107. package/package.json +8 -18
  108. package/dist/converter/midi/convert.d.ts +0 -42
  109. package/dist/converter/midi/convert.js +0 -729
  110. package/dist/converter/midi/index.d.ts +0 -5
  111. package/dist/converter/midi/index.js +0 -11
  112. package/dist/converter/midi/json.d.ts +0 -38
  113. package/dist/converter/midi/json.js +0 -1
  114. package/dist/converter/midi/triplet.d.ts +0 -30
  115. package/dist/converter/midi/triplet.js +0 -75
  116. package/dist/converter/musicxml/dom.d.ts +0 -25
  117. package/dist/converter/musicxml/dom.js +0 -65
  118. package/dist/converter/musicxml/features.d.ts +0 -114
  119. package/dist/converter/musicxml/features.js +0 -501
  120. package/dist/converter/musicxml/index.d.ts +0 -14
  121. package/dist/converter/musicxml/index.js +0 -1295
  122. package/dist/converter/musicxml/model.d.ts +0 -176
  123. package/dist/converter/musicxml/model.js +0 -1
  124. package/dist/converter/musicxml/resolve.d.ts +0 -10
  125. package/dist/converter/musicxml/resolve.js +0 -152
  126. package/dist/converter/source.d.ts +0 -22
  127. package/dist/converter/source.js +0 -51
  128. package/dist/core.d.ts +0 -24
  129. package/dist/core.js +0 -24
  130. package/dist/iife/core.d.ts +0 -1
  131. package/dist/iife/core.js +0 -3
  132. package/dist/iife/from-midi.d.ts +0 -1
  133. package/dist/iife/from-midi.js +0 -15
  134. package/dist/iife/from-musicxml.d.ts +0 -1
  135. package/dist/iife/from-musicxml.js +0 -3
  136. package/dist/iife/full.d.ts +0 -1
  137. package/dist/iife/full.js +0 -3
  138. package/dist/jpfun.core.min.js +0 -123
  139. package/dist/jpfun.from-midi.min.js +0 -9
  140. package/dist/jpfun.from-musicxml.min.js +0 -12
@@ -1,8 +1,29 @@
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
+ import { paintVisual } from "../../render/paint.js";
7
+ const FOLD_ARGS = [{
8
+ name: "gap",
9
+ namedOnly: true,
10
+ type: "length",
11
+ default: { value: 0.12, unit: "em" },
12
+ description: "相邻堆叠成员之间的间距",
13
+ }];
14
+ function formatStack(fn, host, members, gaps, source) {
15
+ let result = host;
16
+ for (let start = 0; start < members.length;) {
17
+ const gap = gaps[start];
18
+ let end = start + 1;
19
+ while (end < members.length && gaps[end] === gap)
20
+ end++;
21
+ const items = members.slice(start, end).map(item => item.toString(source));
22
+ result = `@${fn}(${[result, ...items, `gap=${gap}px`].join(", ")})`;
23
+ start = end;
24
+ }
25
+ return result;
26
+ }
6
27
  /**
7
28
  * `@up` / `@down` 共用的折叠容器,不导出
8
29
  *
@@ -13,24 +34,27 @@ import { getLayoutBounds, layoutLocalSequence } from "../../layout/engine.js";
13
34
  class FoldFunction extends ASTFunctionNode {
14
35
  contents = [];
15
36
  belows = [];
16
- size; // 用于计算 gap
37
+ aboveGaps = [];
38
+ belowGaps = [];
39
+ size;
40
+ gap;
17
41
  /** 静态而不是实例字段:子类的字段初始化器晚于基类构造器,构造期读不到 */
18
42
  get side() { return this.constructor.side; }
19
43
  get children() {
20
44
  return this.belows.length ? [...this.contents, ...this.belows] : this.contents;
21
45
  }
46
+ timeFlowModel() { return null; }
22
47
  constructor(span, args, ctx, parent = null) {
23
48
  super(span, parent);
24
49
  this.size = ctx.variables.fontsize;
25
- for (const [, value] of args) {
26
- if (value instanceof ASTNodeBase) {
50
+ const [gap] = this.getArgValue(args, ctx);
51
+ this.gap = ctx.length2px(gap);
52
+ if (!Number.isFinite(this.gap) || this.gap < 0) {
53
+ throw new ErrorDiagnostic("E_INVALID_FOLD_GAP", "@{fn} 的 gap 必须是非负有限长度", span, { fn: this.callName });
54
+ }
55
+ for (const [key, value] of args) {
56
+ if (typeof key === "number")
27
57
  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
58
  }
35
59
  }
36
60
  /** 至少有一个成员能承载标签时,折叠体本身才能作为整体被标注 */
@@ -38,12 +62,20 @@ class FoldFunction extends ASTFunctionNode {
38
62
  return this.children.some(ASTFunctionNode.findLabelable) ? this : null;
39
63
  }
40
64
  /** 遇到另一个折叠体就展平到自己身上;返回 this 只是为了链式写法 */
41
- addContent(node, side, ctx) {
65
+ addContent(node, side, ctx, gap = this.gap) {
42
66
  if (this.contents.length === 0)
43
67
  side = "above"; // 还没有宿主,来者即宿主
44
68
  if (node instanceof FoldFunction)
45
- return this.flatten(node, side, ctx);
46
- (side === "above" ? this.contents : this.belows).push(node);
69
+ return this.flatten(node, side, ctx, gap);
70
+ if (side === "above") {
71
+ if (this.contents.length > 0)
72
+ this.aboveGaps.push(gap);
73
+ this.contents.push(node);
74
+ }
75
+ else {
76
+ this.belowGaps.push(gap);
77
+ this.belows.push(node);
78
+ }
47
79
  node.parent = this;
48
80
  this.growSpan(node.sourceSpan);
49
81
  return this;
@@ -70,11 +102,11 @@ class FoldFunction extends ASTFunctionNode {
70
102
  }
71
103
  /** 混写方向时输出等价的嵌套形式,hover 的替换按钮才能生成合法代码 */
72
104
  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(", ")})`;
105
+ if (this.contents.length <= 1 && this.belows.length === 0) {
106
+ return `@${this.callName}(${[...this.contents.map(c => c.toString(source)), `gap=${this.gap}px`].join(", ")})`;
107
+ }
108
+ const up = formatStack("up", this.contents[0].toString(source), this.contents.slice(1), this.aboveGaps, source);
109
+ return formatStack("down", up, this.belows, this.belowGaps, source);
78
110
  }
79
111
  /**
80
112
  * 把另一个折叠容器并进自己:**接收者存活**,参数被掏空
@@ -84,29 +116,29 @@ class FoldFunction extends ASTFunctionNode {
84
116
  *
85
117
  * 被掏空的一方必须撤销候选登记:空壳已不在 AST 里,留在表中会让 @tie() 取到孤儿端点。
86
118
  */
87
- flatten(drop, side, ctx) {
119
+ flatten(drop, side, ctx, gap = this.gap) {
88
120
  // 成员顺序跟随源码;比较必须在 growSpan 之前
89
121
  const front = drop.sourceSpan.start < this.sourceSpan.start ? drop : this;
90
122
  const back = front === drop ? this : drop;
91
123
  const [backHost, ...backAbove] = back.contents;
124
+ const hasHost = front.contents.length > 0;
92
125
  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;
126
+ const demotedGap = hasHost && backHost ? [gap] : [];
127
+ const above = !hasHost || side === "above";
128
+ this.contents = [...front.contents, ...(above ? demoted : []), ...backAbove];
129
+ this.belows = [...front.belows, ...(!above ? demoted : []), ...back.belows];
130
+ this.aboveGaps = [...front.aboveGaps, ...(above ? demotedGap : []), ...back.aboveGaps];
131
+ this.belowGaps = [...front.belowGaps, ...(!above ? demotedGap : []), ...back.belowGaps];
97
132
  for (const c of this.children)
98
133
  c.parent = this;
99
134
  this.growSpan(drop.sourceSpan);
100
- drop.contents = [];
101
- drop.belows = [];
102
- const stale = ctx.labelableNodes.indexOf(drop);
135
+ drop.contents.length = drop.belows.length = drop.aboveGaps.length = drop.belowGaps.length = 0;
136
+ const stale = ctx.labelableNodes.lastIndexOf(drop);
103
137
  if (stale >= 0)
104
138
  ctx.labelableNodes.splice(stale, 1);
105
139
  return this;
106
140
  }
107
141
  /**
108
- * 左操作数的取法同 stack;合并策略不同:折叠要让被引用的一方存活
109
- *
110
142
  * `this` 是发起调用的那个子类,所以方向和构造都从它取。子类必须写成
111
143
  * `UpFunction.desugar(...)` 这种带显式接收者的形式 —— 解析器是把
112
144
  * `deSugarRelation` 摘成游离函数引用来调的,`this` 到不了这里。
@@ -116,61 +148,25 @@ class FoldFunction extends ASTFunctionNode {
116
148
  if (!(n.kind === "sugar" && n.data === this))
117
149
  return null;
118
150
  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) {
151
+ const leftNode = ctx.takeLeftOperand();
152
+ if (!leftNode)
124
153
  throw new ErrorDiagnostic("E_FOLD_NO_TARGET", "{fn}语法糖错误: 左边没有找到可叠加的目标", n.span, { fn: label });
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
- // 左操作数收敛成一个折叠体,后续合并都在它身上进行
154
+ const wrapper = new this({ ...n.span }, new Map(), ctx);
148
155
  const host = leftNode instanceof FoldFunction
149
156
  ? 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;
157
+ : wrapper.addContent(leftNode, "above", ctx);
158
+ const next = ctx.consumeRightOperand(nodes, at, right => {
160
159
  // 右操作数可能已被 @tie() 等按引用取走(它就在刚才解析的剩余里),
161
160
  // 而新建的左包装无人引用,所以合并时让右边存活;接收者就是存活的那一方
162
161
  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;
162
+ ? right.flatten(host, this.side, ctx, wrapper.gap)
163
+ : host.addContent(right, this.side, ctx, wrapper.gap);
170
164
  // 语法糖不走 pushNode,这里补登记;null 同样入表,和显式调用一样成为边界
171
165
  ctx.labelableNodes.push(merged.labelable());
172
- return nodes.length;
173
- }
166
+ return merged;
167
+ });
168
+ if (next !== null)
169
+ return next;
174
170
  throw new ErrorDiagnostic("E_FOLD_NO_TARGET", "{fn}语法糖错误: 右边没有找到可叠加的目标", n.span, { fn: label });
175
171
  }
176
172
  }
@@ -181,15 +177,15 @@ class UpFunction extends FoldFunction {
181
177
  description: "把同一时间位置的可见对象向上堆叠",
182
178
  details: `\
183
179
  ~~~jpfun
184
- @up(1, 3, 5)
180
+ @up(1, 3, 5, gap=0.12em)
185
181
  ~~~
186
182
 
187
- 第一个内容参数为宿主,其余依次叠在上方;所有成员共享宿主时值。常用于和弦、速度记号和音符注释。
183
+ 第一个内容参数为宿主,其余依次叠在上方;所有成员共享宿主时值。仅命名参数 \`gap\` 指定相邻成员的间距。常用于和弦、速度记号和音符注释。
188
184
 
189
185
  **简写**:\`1 ^ 3 ^ 5\`。复杂内容可用 \`{...}\` 包裹;需要独立节奏的并行分支时,使用 \`&\` / \`@stack\`。`,
190
186
  allowExtraArgs: true,
191
187
  extraArgType: "content",
192
- args: [],
188
+ args: FOLD_ARGS,
193
189
  };
194
190
  static deSugarAtom(source, start, _end) {
195
191
  if (source[start] !== "^")
@@ -208,14 +204,14 @@ class DownFunction extends FoldFunction {
208
204
  description: "把同一时间位置的可见对象向下堆叠",
209
205
  details: `\
210
206
  ~~~jpfun
211
- @down(1, $p)
207
+ @down(1, $p, gap=0.12em)
212
208
  ~~~
213
- 第一个内容参数为宿主,其余依次叠在下方并共享其时值,常用于力度等下方标记。
209
+ 第一个内容参数为宿主,其余依次叠在下方并共享其时值,仅命名参数 \`gap\` 指定相邻成员的间距,常用于力度等下方标记。
214
210
 
215
211
  **简写**:\`1 _ $p\`。与 \`^\` 混写时共用同一宿主:\`1^2_3\` 将 \`2\` 放在 \`1\` 上方、\`3\` 放在下方。`,
216
212
  allowExtraArgs: true,
217
213
  extraArgType: "content",
218
- args: [],
214
+ args: FOLD_ARGS,
219
215
  };
220
216
  static deSugarAtom(source, start, _end) {
221
217
  if (source[start] !== "_")
@@ -329,23 +325,24 @@ class FoldTemporal extends TemporalNodeBase {
329
325
  this.box.w = layoutLocalSequence([first], context);
330
326
  for (let index = 1; index < this.members.length; index++)
331
327
  layoutLocalSequence([this.members[index]], context);
332
- this.springConfig = { ...first.springConfig, ...this.springConfig };
328
+ this.springConfig = { ...first.springConfig };
333
329
  // 横向完全等于宿主:上下的标记(变速、注释、力度)常常比音符宽得多,
334
330
  // 让它们撑宽盒子会把右邻推开一大截;它们画在基线外侧,伸出盒外也不会碰撞
335
331
  const anchor = first.box.x + first.box.anchor;
336
- const gap = this.ast.size * 0.12;
337
332
  this.verticalOffsets.length = this.members.length;
338
333
  this.verticalOffsets[0] = 0;
339
334
  // 以宿主盒顶为 0,向上得到负坐标、向下得到正坐标,最后整体下移
340
335
  const firstBounds = getLayoutBounds(first);
341
336
  let top = firstBounds.y;
342
337
  for (let i = 1; i < this.aboveCount; i++) {
338
+ const gap = this.ast.aboveGaps[i - 1];
343
339
  const bounds = getLayoutBounds(this.members[i]);
344
340
  this.verticalOffsets[i] = top - gap - bounds.y - bounds.h;
345
341
  top -= gap + bounds.h;
346
342
  }
347
343
  let bottom = firstBounds.y + firstBounds.h;
348
344
  for (let i = this.aboveCount; i < this.members.length; i++) {
345
+ const gap = this.ast.belowGaps[i - this.aboveCount];
349
346
  const bounds = getLayoutBounds(this.members[i]);
350
347
  this.verticalOffsets[i] = bottom + gap - bounds.y;
351
348
  bottom += gap + bounds.h;
@@ -384,10 +381,7 @@ class FoldTemporal extends TemporalNodeBase {
384
381
  }
385
382
  }
386
383
  paint(painter) {
387
- for (const member of this.members) {
388
- member.paint(painter);
389
- for (const decoration of member.decorations)
390
- decoration.paint?.(painter);
391
- }
384
+ for (const member of this.members)
385
+ paintVisual(painter, member);
392
386
  }
393
387
  }
@@ -1,7 +1,7 @@
1
1
  import type { SourceSpan } from "../ASTtypes.js";
2
2
  import type { PathCommand } from "../../render/types.js";
3
3
  export declare const CURVED_BRACE_COMMANDS: readonly PathCommand[];
4
- export declare const CURVED_BRACE_BOUNDS: import("../../core.js").Rect;
4
+ export declare const CURVED_BRACE_BOUNDS: import("../../index.js").Rect;
5
5
  /** 方括线端钩,以粗竖线宽度为单位,绘制时上下镜像 */
6
6
  export declare const BRACKET_HOOK_REACH = 2.33;
7
7
  export declare const BRACKET_HOOK_DROP = 1.17;
@@ -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";