jpfun 0.1.0 → 0.3.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 (114) hide show
  1. package/LICENSE +200 -200
  2. package/README.md +191 -133
  3. package/dist/diagnostic.js +5 -2
  4. package/dist/fraction.d.ts +24 -0
  5. package/dist/fraction.js +132 -0
  6. package/dist/functions/ASTtypes.d.ts +24 -7
  7. package/dist/functions/ASTtypes.js +35 -16
  8. package/dist/functions/adjust/index.d.ts +2 -0
  9. package/dist/functions/adjust/index.js +193 -0
  10. package/dist/functions/bar/index.d.ts +8 -0
  11. package/dist/functions/bar/index.js +48 -19
  12. package/dist/functions/beam/auto.js +35 -18
  13. package/dist/functions/beam/index.js +10 -6
  14. package/dist/functions/beam/layout.d.ts +3 -2
  15. package/dist/functions/beam/layout.js +42 -64
  16. package/dist/functions/box/index.js +35 -34
  17. package/dist/functions/br/index.js +14 -12
  18. package/dist/functions/dash/index.js +37 -5
  19. package/dist/functions/default.js +14 -7
  20. package/dist/functions/div/index.d.ts +16 -0
  21. package/dist/functions/div/index.js +51 -23
  22. package/dist/functions/dot/index.js +16 -8
  23. package/dist/functions/grace/index.d.ts +8 -18
  24. package/dist/functions/grace/index.js +98 -99
  25. package/dist/functions/head/index.d.ts +2 -0
  26. package/dist/functions/head/index.js +492 -0
  27. package/dist/functions/key/index.js +98 -5
  28. package/dist/functions/meter/index.d.ts +2 -0
  29. package/dist/functions/meter/index.js +154 -0
  30. package/dist/functions/note/accidentals.d.ts +24 -3
  31. package/dist/functions/note/accidentals.js +31 -1
  32. package/dist/functions/note/index.js +60 -80
  33. package/dist/functions/page/index.d.ts +9 -2
  34. package/dist/functions/page/index.js +50 -4
  35. package/dist/functions/set/index.js +42 -34
  36. package/dist/functions/stack/index.js +39 -34
  37. package/dist/functions/symbol/index.d.ts +21 -0
  38. package/dist/functions/symbol/index.js +94 -0
  39. package/dist/functions/symbol/ornament.d.ts +15 -0
  40. package/dist/functions/symbol/ornament.js +73 -0
  41. package/dist/functions/symbol/shape.d.ts +22 -0
  42. package/dist/functions/symbol/shape.js +48 -0
  43. package/dist/functions/symbol/symbols/accent.d.ts +2 -0
  44. package/dist/functions/symbol/symbols/accent.js +24 -0
  45. package/dist/functions/symbol/symbols/f.d.ts +2 -0
  46. package/dist/functions/symbol/symbols/f.js +55 -0
  47. package/dist/functions/symbol/symbols/fermata.d.ts +2 -0
  48. package/dist/functions/symbol/symbols/fermata.js +24 -0
  49. package/dist/functions/symbol/symbols/mordent.d.ts +2 -0
  50. package/dist/functions/symbol/symbols/mordent.js +43 -0
  51. package/dist/functions/symbol/symbols/p.d.ts +2 -0
  52. package/dist/functions/symbol/symbols/p.js +61 -0
  53. package/dist/functions/symbol/symbols/tr.d.ts +2 -0
  54. package/dist/functions/symbol/symbols/tr.js +75 -0
  55. package/dist/functions/tempo/glyph.d.ts +12 -0
  56. package/dist/functions/tempo/glyph.js +30 -0
  57. package/dist/functions/tempo/index.js +53 -3
  58. package/dist/functions/text/index.js +78 -26
  59. package/dist/functions/tie/index.js +103 -128
  60. package/dist/functions/tuplet/index.d.ts +2 -0
  61. package/dist/functions/tuplet/index.js +211 -0
  62. package/dist/functions/{over → up}/index.d.ts +1 -0
  63. package/dist/functions/up/index.js +370 -0
  64. package/dist/functions/voice/index.js +125 -108
  65. package/dist/functions/volta/index.d.ts +2 -0
  66. package/dist/functions/volta/index.js +227 -0
  67. package/dist/index.d.ts +4 -0
  68. package/dist/index.js +4 -0
  69. package/dist/jpfun.min.js +143 -0
  70. package/dist/layout/engine.d.ts +4 -11
  71. package/dist/layout/engine.js +100 -66
  72. package/dist/layout/model.d.ts +8 -1
  73. package/dist/layout/model.js +16 -6
  74. package/dist/layout/path.d.ts +19 -0
  75. package/dist/layout/path.js +101 -0
  76. package/dist/layout/types.d.ts +51 -18
  77. package/dist/layout/types.js +3 -1
  78. package/dist/lowering/loweringContext.d.ts +19 -23
  79. package/dist/lowering/loweringContext.js +179 -187
  80. package/dist/lowering/track.d.ts +33 -10
  81. package/dist/lowering/track.js +6 -6
  82. package/dist/lowering/types.d.ts +57 -43
  83. package/dist/lowering/types.js +20 -4
  84. package/dist/parser/parse-utils/call-utils.d.ts +0 -2
  85. package/dist/parser/parse-utils/call-utils.js +0 -6
  86. package/dist/parser/parse-utils/note-utils.d.ts +8 -0
  87. package/dist/parser/parse-utils/note-utils.js +35 -13
  88. package/dist/parser/parse-utils/string-utils.d.ts +8 -0
  89. package/dist/parser/parse-utils/string-utils.js +20 -0
  90. package/dist/parser/parserContext.d.ts +8 -5
  91. package/dist/parser/parserContext.js +38 -33
  92. package/dist/parser/preprocess.js +17 -11
  93. package/dist/playback/compile.d.ts +18 -0
  94. package/dist/playback/compile.js +401 -0
  95. package/dist/playback/event.d.ts +91 -0
  96. package/dist/playback/event.js +87 -0
  97. package/dist/playback/time.d.ts +14 -0
  98. package/dist/playback/time.js +81 -0
  99. package/dist/playback/types.d.ts +128 -0
  100. package/dist/playback/types.js +3 -0
  101. package/dist/render/canvas.d.ts +4 -4
  102. package/dist/render/canvas.js +20 -5
  103. package/dist/render/index.d.ts +1 -0
  104. package/dist/render/index.js +1 -0
  105. package/dist/render/paint.d.ts +8 -0
  106. package/dist/render/paint.js +101 -0
  107. package/dist/render/svg.d.ts +3 -3
  108. package/dist/render/svg.js +10 -8
  109. package/dist/render/text.d.ts +17 -0
  110. package/dist/render/text.js +35 -0
  111. package/dist/render/types.d.ts +1 -1
  112. package/grammar.md +217 -122
  113. package/package.json +14 -9
  114. package/dist/functions/over/index.js +0 -307
@@ -1,8 +1,8 @@
1
1
  import { isVisualTemporalNode, } from "../../lowering/types.js";
2
2
  import { ErrorDiagnostic } from "../../diagnostic.js";
3
- import { claimDivLine, divLinePortName, } from "../div/index.js";
3
+ import { claimDivLine, divLinePortName, divLineRuns, divLineWidth, } from "../div/index.js";
4
4
  /** 显式和自动分组共享同一个几何对象 */
5
- export function createBeamLayoutAttachment(endPoints, explicit = false, sourceSpan) {
5
+ export function createBeamLayoutAttachment(endPoints, explicit, sourceSpan) {
6
6
  return new BeamLayoutAttachment(endPoints, explicit, sourceSpan);
7
7
  }
8
8
  /** 显式 beam 必须严格连接最终同轨同谱面行中的相邻可见主体 */
@@ -50,7 +50,7 @@ export function validateExplicitBeamAttachments(result) {
50
50
  && secondPosition.index === firstPosition.index + 1;
51
51
  if (adjacent)
52
52
  continue;
53
- throw new ErrorDiagnostic("E_NON_ADJACENT_BEAM", "@beam 的端点必须按时间顺序连接同一轨道、同一谱面行中的相邻可见元素", attachment.sourceSpan ?? attachment.endPoints[i].ast.sourceSpan);
53
+ throw new ErrorDiagnostic("E_NON_ADJACENT_BEAM", "@beam 的端点必须按时间顺序连接同一轨道、同一谱面行中的相邻可见元素", attachment.sourceSpan);
54
54
  }
55
55
  }
56
56
  }
@@ -66,22 +66,19 @@ export function collectExplicitBeamEndpoints(attachments) {
66
66
  return result;
67
67
  }
68
68
  class BeamLayoutAttachment {
69
- box = {
70
- x: 0,
71
- y: 0,
72
- w: 0,
73
- h: 0,
74
- };
75
69
  layer = "foreground";
76
70
  endPoints;
77
71
  explicit;
78
72
  sourceSpan;
79
- lines = [];
80
- strokeWidth = 1;
73
+ /** 端点字号不随布局变化,装饰尺寸可以一次算定 */
74
+ fontSize;
75
+ strokeWidth;
81
76
  constructor(endPoints, explicit, sourceSpan) {
82
77
  this.endPoints = endPoints;
83
78
  this.explicit = explicit;
84
79
  this.sourceSpan = sourceSpan;
80
+ this.fontSize = endPoints.reduce((size, endpoint) => isVisualTemporalNode(endpoint) ? Math.max(size, endpoint.ast.size) : size, 0);
81
+ this.strokeWidth = divLineWidth(this.fontSize);
85
82
  }
86
83
  /** 同一 beam 组内的相邻元素使用更短的自然弹簧间距 */
87
84
  prepareHorizontal(context) {
@@ -99,76 +96,56 @@ class BeamLayoutAttachment {
99
96
  }
100
97
  }
101
98
  }
102
- layout(context) {
103
- this.updateGeometry(context);
104
- const halfStroke = this.strokeWidth / 2;
105
- return this.lines.map(line => ({
106
- x: Math.min(line.start.x, line.end.x) - halfStroke,
107
- y: Math.min(line.start.y, line.end.y) - halfStroke,
108
- w: Math.abs(line.end.x - line.start.x) + this.strokeWidth,
109
- h: Math.abs(line.end.y - line.start.y) + this.strokeWidth,
99
+ createGeometry(context) {
100
+ const lines = this.collectLines(context);
101
+ const strokeWidth = this.strokeWidth;
102
+ // 减时线永远水平,butt 端帽只在纵向占半个描边
103
+ const regions = lines.map(line => ({
104
+ x: Math.min(line.start.x, line.end.x),
105
+ y: line.start.y - strokeWidth / 2,
106
+ w: Math.abs(line.end.x - line.start.x),
107
+ h: strokeWidth,
110
108
  line: line.line,
111
109
  track: line.track,
112
110
  }));
113
- }
114
- paint(painter) {
115
- for (const line of this.lines) {
116
- painter.drawLine(line.start.x, line.start.y, line.end.x, line.end.y, { stroke: "#000", strokeWidth: this.strokeWidth });
117
- }
111
+ return {
112
+ regions,
113
+ paint(painter) {
114
+ for (const line of lines) {
115
+ painter.drawLine(line.start.x, line.start.y, line.end.x, line.end.y, { stroke: "#000", strokeWidth });
116
+ }
117
+ },
118
+ };
118
119
  }
119
120
  absolutePort(node, name) {
120
121
  const port = node.ports[name];
121
- if (!port)
122
- return null;
123
122
  return {
124
123
  x: node.box.x + port.x,
125
124
  y: node.box.y + port.y,
126
125
  };
127
126
  }
128
- updateGeometry(context) {
129
- this.lines.length = 0;
127
+ collectLines(context) {
128
+ const lines = [];
130
129
  const available = this.endPoints.filter(isVisualTemporalNode);
131
130
  if (available.length < 2)
132
- return;
133
- // 选择端点的使用最大字号作为装饰尺寸
134
- const fontSize = available.reduce((size, endpoint) => Math.max(size, endpoint.ast.size), 0);
135
- this.strokeWidth = Math.max(1, fontSize * 0.07);
136
- // 每一级只合并连续提供该级 div 端口的对象
137
- for (let level = 0;; level++) {
138
- const spanGroups = [];
139
- let spans = [];
140
- let hasPorts = false;
141
- for (const node of available) {
142
- const left = this.absolutePort(node, divLinePortName(level, "left"));
143
- const right = this.absolutePort(node, divLinePortName(level, "right"));
144
- if (left || right)
145
- hasPorts = true;
146
- if (!left || !right) {
147
- if (spans.length > 0)
148
- spanGroups.push(spans);
149
- spans = [];
150
- continue;
151
- }
131
+ return lines;
132
+ for (const { level, from, to } of divLineRuns(available)) {
133
+ const spans = [];
134
+ for (let i = from; i <= to; i++) {
135
+ const node = available[i];
152
136
  spans.push({
153
- left,
154
- right,
137
+ left: this.absolutePort(node, divLinePortName(level, "left")),
138
+ right: this.absolutePort(node, divLinePortName(level, "right")),
155
139
  line: node.layoutLine,
156
140
  track: node.track,
157
141
  node,
158
142
  });
159
143
  }
160
- if (spans.length > 0)
161
- spanGroups.push(spans);
162
- if (!hasPorts)
163
- break;
164
- for (const group of spanGroups) {
165
- if (group.length >= 2)
166
- this.addSystemLines(group, context, level);
167
- }
144
+ this.addSystemLines(spans, context, level, lines);
168
145
  }
169
146
  // 显式关系允许连接没有 div 端口的任意可见对象
170
- if (this.lines.length === 0 && this.explicit) {
171
- const gap = fontSize * 0.12;
147
+ if (lines.length === 0 && this.explicit) {
148
+ const gap = this.fontSize * 0.12;
172
149
  const spans = available.map(node => {
173
150
  const point = {
174
151
  x: node.box.x + node.box.anchor,
@@ -182,11 +159,12 @@ class BeamLayoutAttachment {
182
159
  node,
183
160
  };
184
161
  });
185
- this.addSystemLines(spans, context, null);
162
+ this.addSystemLines(spans, context, null, lines);
186
163
  }
164
+ return lines;
187
165
  }
188
166
  /** 同一级减时线按谱面行拆开,跨行时连接到对应页面边界 */
189
- addSystemLines(spans, context, connectedLevel) {
167
+ addSystemLines(spans, context, connectedLevel, lines) {
190
168
  const firstLine = Math.min(...spans.map(item => item.line));
191
169
  const lastLine = Math.max(...spans.map(item => item.line));
192
170
  const fallbackTrack = spans[0].track;
@@ -199,7 +177,7 @@ class BeamLayoutAttachment {
199
177
  if (current.length === 0) {
200
178
  const visualAxis = context.getVisualAxis(line, fallbackTrack);
201
179
  const y = visualAxis + visualAxisOffset;
202
- this.lines.push({
180
+ lines.push({
203
181
  start: { x: context.originX, y },
204
182
  end: { x: context.originX + context.width, y },
205
183
  line,
@@ -222,7 +200,7 @@ class BeamLayoutAttachment {
222
200
  for (const span of current)
223
201
  claimDivLine(span.node, connectedLevel);
224
202
  }
225
- this.lines.push({
203
+ lines.push({
226
204
  start,
227
205
  end,
228
206
  line,
@@ -3,6 +3,7 @@ import { isVisualTemporalNode } from "../../lowering/types.js";
3
3
  import { ErrorDiagnostic } from "../../diagnostic.js";
4
4
  import { unionLayoutBoxes } from "../../layout/engine.js";
5
5
  import { layoutHorizontalRegion } from "../../layout/model.js";
6
+ import { isLayoutAttachment } from "../../layout/types.js";
6
7
  class BoxFunction extends ASTFunctionNode {
7
8
  static def = {
8
9
  name: ["box"],
@@ -56,18 +57,18 @@ class BoxFunction extends ASTFunctionNode {
56
57
  * 进入目标内容前只开始收集所有成员矩形的引用
57
58
  */
58
59
  loweringEnter(ctx) {
59
- const members = [];
60
60
  const temporalMembers = [];
61
+ const childAttachments = [];
61
62
  ctx.beginLoweringGroup(this, {
62
- attachment: new BoxLayoutAttachment(members, temporalMembers, this),
63
+ attachment: new BoxLayoutAttachment(temporalMembers, childAttachments, this),
63
64
  onTemporal(node) {
64
65
  if (!isVisualTemporalNode(node))
65
66
  return;
66
- members.push(node.box);
67
67
  temporalMembers.push(node);
68
68
  },
69
69
  onAttachment(attachment) {
70
- members.push(attachment.box);
70
+ if (isLayoutAttachment(attachment))
71
+ childAttachments.push(attachment);
71
72
  },
72
73
  });
73
74
  return [];
@@ -89,22 +90,17 @@ class BoxFunction extends ASTFunctionNode {
89
90
  }
90
91
  export const BoxNode = BoxFunction;
91
92
  class BoxLayoutAttachment {
92
- box = {
93
- x: 0,
94
- y: 0,
95
- w: 0,
96
- h: 0,
97
- };
98
93
  layer = "background";
99
- /** 同一个数组会在 lowering 递归过程中持续加入成员盒引用 */
100
- members;
101
94
  temporalMembers;
95
+ /** 写在 box 内的关系对象;lowering 递归中持续加入,因而总在 box 自身之前完成几何 */
96
+ childAttachments;
102
97
  owner;
103
98
  fixedStart = null;
104
99
  wallOffset = 0;
105
- constructor(members, temporalMembers, owner) {
106
- this.members = members;
100
+ get sourceSpan() { return this.owner.sourceSpan; }
101
+ constructor(temporalMembers, childAttachments, owner) {
107
102
  this.temporalMembers = temporalMembers;
103
+ this.childAttachments = childAttachments;
108
104
  this.owner = owner;
109
105
  }
110
106
  /** 定宽 box 在成员覆盖的全局列上运行局部墙布局 */
@@ -171,32 +167,37 @@ class BoxLayoutAttachment {
171
167
  * box 必须等所有成员获得最终 x 和 y 后再求边界
172
168
  * stroke 以矩形边界为中心绘制,因此外接盒额外包含半个线宽
173
169
  */
174
- layout() {
170
+ createGeometry(context) {
175
171
  const { padding, stroke } = this.owner;
176
172
  const rect = { x: 0, y: 0, w: 0, h: 0 };
173
+ const members = [
174
+ ...this.temporalMembers.map(member => member.box),
175
+ ...this.childAttachments.map(attachment => context.getAttachmentBox(attachment)),
176
+ ];
177
177
  // 未命名歌词等 attachment 会保留全零盒,不能让它把边框拉到文档原点
178
- if (!unionLayoutBoxes(rect, this.members.filter(member => member.w > 0 || member.h > 0)))
179
- return [];
178
+ if (!unionLayoutBoxes(rect, members.filter(member => member.w > 0 || member.h > 0))) {
179
+ return { regions: [], paint() { } };
180
+ }
180
181
  const inset = padding + stroke / 2;
181
- // 边框不抢轨道纵向空间,只参与画布边界;this.box 由引擎写回
182
+ // 边框不抢轨道纵向空间,只参与画布边界
182
183
  const fixedX = this.fixedStart
183
184
  ? this.fixedStart.box.x + this.fixedStart.box.anchor + this.wallOffset
184
185
  : rect.x;
185
- return [{
186
- x: fixedX - inset,
187
- y: rect.y - inset,
188
- w: (this.fixedStart ? this.owner.width : rect.w) + inset * 2,
189
- h: rect.h + inset * 2,
190
- }];
191
- }
192
- paint(painter) {
193
- if (this.box.w === 0 && this.box.h === 0)
194
- return;
195
- const stroke = this.owner.stroke;
196
- const inset = stroke / 2;
197
- painter.drawRect(this.box.x + inset, this.box.y + inset, Math.max(0, this.box.w - stroke), Math.max(0, this.box.h - stroke), {
198
- stroke: "#000",
199
- strokeWidth: stroke,
200
- });
186
+ const region = {
187
+ x: fixedX - inset,
188
+ y: rect.y - inset,
189
+ w: (this.fixedStart ? this.owner.width : rect.w) + inset * 2,
190
+ h: rect.h + inset * 2,
191
+ };
192
+ return {
193
+ regions: [region],
194
+ paint(painter) {
195
+ const strokeInset = stroke / 2;
196
+ painter.drawRect(region.x + strokeInset, region.y + strokeInset, Math.max(0, region.w - stroke), Math.max(0, region.h - stroke), {
197
+ stroke: "#000",
198
+ strokeWidth: stroke,
199
+ });
200
+ },
201
+ };
201
202
  }
202
203
  }
@@ -2,14 +2,15 @@ import { ASTFunctionNode } from "../ASTtypes.js";
2
2
  import { skipSpaces, skipSpacesBack } from "../../parser/parserContext.js";
3
3
  import { TemporalNodeBase } from "../../lowering/types.js";
4
4
  import { Diagnostic, WarningDiagnostic } from "../../diagnostic.js";
5
+ import { Fraction } from "../../fraction.js";
5
6
  class BrFunction extends ASTFunctionNode {
6
7
  static def = {
7
8
  name: ["br"],
8
9
  description: "换行",
9
- example: `@br(offset=1): 在后续事件前偏移指定行数
10
- @br() === @br(1)
11
- @br(2): 空出一行后继续
12
- 语法糖: [仅限顶层]至少连续两个换行
10
+ example: `@br(offset=1): 在后续事件前偏移指定行数
11
+ @br() === @br(1)
12
+ @br(2): 空出一行后继续
13
+ 语法糖: [仅限顶层]至少连续两个换行
13
14
  `,
14
15
  allowExtraArgs: false,
15
16
  args: [
@@ -44,8 +45,7 @@ class BrFunction extends ASTFunctionNode {
44
45
  loweringEnter() {
45
46
  const br = new TemporalNodeBase();
46
47
  br.ast = this;
47
- br.T = 0;
48
- br.type = 1 /* ColType.SINGLE */;
48
+ br.mergeKey = -1;
49
49
  br.breakBefore = this.offset;
50
50
  return [br];
51
51
  }
@@ -54,17 +54,19 @@ class BrFunction extends ASTFunctionNode {
54
54
  * 换行会把整条时间线切开,因此跨越换行点的持续事件无法被完整绘制
55
55
  */
56
56
  static loweringFinalize = (result) => {
57
- let latestEnd = -Infinity;
57
+ const latestEnd = new Fraction();
58
+ const candidateEnd = new Fraction();
58
59
  let latestNode = null;
59
60
  for (const column of result.columns) {
60
- const t = column[0].t;
61
- if (latestEnd > t + 1e-6 && column.some(node => node.breakBefore > 0)) {
61
+ if (latestNode && latestEnd.compare(column[0].t) > 0 && column.some(node => node.breakBefore > 0)) {
62
62
  throw Diagnostic.error.BreakInsideEvent(latestNode.ast.sourceSpan);
63
63
  }
64
64
  for (const node of column) {
65
- const end = node.t + node.T;
66
- if (node.T > 1e-6 && end > latestEnd) {
67
- latestEnd = end;
65
+ if (node.T.isZero())
66
+ continue;
67
+ candidateEnd.copyFrom(node.t).add(node.T);
68
+ if (!latestNode || candidateEnd.compare(latestEnd) > 0) {
69
+ latestEnd.copyFrom(candidateEnd);
68
70
  latestNode = node;
69
71
  }
70
72
  }
@@ -1,11 +1,12 @@
1
1
  import { ASTFunctionNode } from "../ASTtypes.js";
2
- import { TemporalNodeBase } from "../../lowering/types.js";
2
+ import { DEFAULT_KEY, TemporalNodeBase } from "../../lowering/types.js";
3
+ import { WarningDiagnostic } from "../../diagnostic.js";
3
4
  class DashFunction extends ASTFunctionNode {
4
5
  static def = {
5
6
  name: ["dash", "-"],
6
7
  description: "增时线",
7
- example: `@dash() 创建一根增时线
8
- 语法糖: 一个 '-' 代表一个 @dash()
8
+ example: `@dash() 创建一根增时线
9
+ 语法糖: 一个 '-' 代表一个 @dash()
9
10
  `,
10
11
  allowExtraArgs: false,
11
12
  args: []
@@ -40,10 +41,13 @@ class DashTemporalNode extends TemporalNodeBase {
40
41
  constructor(ast) {
41
42
  super();
42
43
  this.ast = ast;
43
- this.T = 1;
44
- this.type = 2 /* ColType.DEFAULT */;
44
+ this.T.set(1);
45
+ this.mergeKey = DEFAULT_KEY;
45
46
  this.initLayoutBox();
46
47
  }
48
+ onTimeState(state) {
49
+ this.playbackState = { bpm: state.bpm };
50
+ }
47
51
  prepareLayout() {
48
52
  const size = this.ast.size;
49
53
  const width = size * 0.5;
@@ -55,6 +59,34 @@ class DashTemporalNode extends TemporalNodeBase {
55
59
  this.lineY = size * 0.5;
56
60
  this.box.visualAxis = this.lineY;
57
61
  }
62
+ emitPlayback(emitter) {
63
+ const start = emitter.start.clone();
64
+ const end = emitter.end.clone();
65
+ const track = emitter.track;
66
+ const sourceSpan = this.ast.sourceSpan;
67
+ emitter.defer(context => {
68
+ let rootOrigin;
69
+ for (let i = context.events.length - 1; i >= 0; i--) {
70
+ const event = context.events[i];
71
+ if (event.kind !== "note-off" || event.origins.at(-1).node.track !== track)
72
+ continue;
73
+ if (rootOrigin === undefined) {
74
+ if (!event.at.equals(start))
75
+ continue;
76
+ rootOrigin = event.origins[0];
77
+ }
78
+ else if (event.origins[0] !== rootOrigin) {
79
+ break;
80
+ }
81
+ if (event.at.equals(start)) {
82
+ event.at.copyFrom(end);
83
+ }
84
+ }
85
+ if (rootOrigin === undefined) {
86
+ context.diagnostics.push(new WarningDiagnostic("W_PLAYBACK_SUSTAIN_WITHOUT_TARGET", "增时线前没有可延续的发声音符", sourceSpan));
87
+ }
88
+ });
89
+ }
58
90
  paint(painter) {
59
91
  painter.drawLine(this.box.x, this.box.y + this.lineY, this.box.x + this.box.w, this.box.y + this.lineY, { stroke: "#000", strokeWidth: Math.max(1, this.ast.size * 0.1) });
60
92
  }
@@ -1,3 +1,4 @@
1
+ import { AdjustNode } from "./adjust/index.js";
1
2
  import { BarNode } from "./bar/index.js";
2
3
  import { BeamNode } from "./beam/index.js";
3
4
  import { BoxNode } from "./box/index.js";
@@ -5,28 +6,34 @@ import { DashNode } from "./dash/index.js";
5
6
  import { DivNode } from "./div/index.js";
6
7
  import { DotNode } from "./dot/index.js";
7
8
  import { GraceNode } from "./grace/index.js";
9
+ import { HeadNode } from "./head/index.js";
8
10
  import { KeyNode } from "./key/index.js";
11
+ import { MeterNode } from "./meter/index.js";
9
12
  import { BrNode } from "./br/index.js";
10
13
  import { NoteNode } from "./note/index.js";
11
- import { UpNode } from "./over/index.js";
14
+ import { UpNode, DownNode } from "./up/index.js";
12
15
  import { PageNode } from "./page/index.js";
13
16
  import { StackNode } from "./stack/index.js";
17
+ import { SymbolNode } from "./symbol/index.js";
14
18
  import { SetNode } from "./set/index.js";
15
19
  import { TempoNode } from "./tempo/index.js";
16
20
  import { TextNode } from "./text/index.js";
21
+ import { VoltaNode } from "./volta/index.js";
17
22
  import { TieNode } from "./tie/index.js";
23
+ import { TupletNode } from "./tuplet/index.js";
18
24
  import { VoiceNode, VoicesNode } from "./voice/index.js";
19
25
  export const defaultFunctions = [
20
26
  NoteNode, DashNode, BarNode, // 有实体
21
- DivNode, DotNode, // 装饰性
27
+ DivNode, DotNode, TupletNode, // 装饰性
22
28
  VoiceNode, VoicesNode, // 歌词和声部
23
29
  BrNode, // 排版
24
- PageNode, // 文档页面
30
+ PageNode, HeadNode, // 文档页面与谱头
25
31
  StackNode, // 时间同步
26
- UpNode,
32
+ UpNode, DownNode,
27
33
  GraceNode, // 倚音
28
34
  TieNode, BeamNode,
29
- BoxNode,
30
- SetNode, KeyNode, TempoNode, // 设置
31
- TextNode,
35
+ BoxNode, VoltaNode,
36
+ AdjustNode, // 手工微调
37
+ SetNode, KeyNode, MeterNode, TempoNode, // 设置
38
+ TextNode, SymbolNode,
32
39
  ];
@@ -6,6 +6,18 @@ export declare const DIV_ADDON_KEY: "@div";
6
6
  /** div 暴露自己的线段端口,不依赖任何关系函数 */
7
7
  export declare function divLinePortName(level: number, edge: "left" | "right"): string;
8
8
  export declare function claimDivLine(host: LayoutHost, level: number): void;
9
+ /** 接管绘制的函数必须用同一条公式,否则同一条减时线会因为谁画而粗细不一 */
10
+ export declare function divLineWidth(size: number): number;
11
+ /**
12
+ * 逐级产出连续提供该级端口的下标区间(含两端,长度至少为 2)
13
+ *
14
+ * 端口按级递增排布是 div 的内部约定,接管绘制的函数只按这里给出的区间连线
15
+ */
16
+ export declare function divLineRuns(hosts: readonly LayoutHost[]): Generator<{
17
+ level: number;
18
+ from: number;
19
+ to: number;
20
+ }, void, unknown>;
9
21
  declare class DivFunction extends ASTFunctionNode {
10
22
  static def: {
11
23
  name: string[];
@@ -20,6 +32,10 @@ declare class DivFunction extends ASTFunctionNode {
20
32
  name: string;
21
33
  type: "number";
22
34
  default: number;
35
+ } | {
36
+ name: string;
37
+ type: "boolean";
38
+ default: boolean;
23
39
  })[];
24
40
  };
25
41
  static deSugarAtom(source: string, start: number, end: number): {
@@ -1,4 +1,5 @@
1
1
  import { ASTFunctionNode, functionAddonKey } from "../ASTtypes.js";
2
+ import { WarningDiagnostic } from "../../diagnostic.js";
2
3
  const DIV_FUNC_NAME = "div";
3
4
  export const DIV_ADDON_KEY = functionAddonKey(DIV_FUNC_NAME);
4
5
  /** div 暴露自己的线段端口,不依赖任何关系函数 */
@@ -10,30 +11,49 @@ export function claimDivLine(host, level) {
10
11
  if (port)
11
12
  port.claimed = true;
12
13
  }
13
- function parseAutoBeamFlag(raw) {
14
- if (typeof raw === "boolean")
15
- return raw;
16
- if (typeof raw === "number")
17
- return raw !== 0;
18
- if (typeof raw === "string") {
19
- const normalized = raw.trim().toLowerCase();
20
- if (normalized === "true" || normalized === "1" || normalized === "on")
21
- return true;
22
- if (normalized === "false" || normalized === "0" || normalized === "off")
23
- return false;
14
+ /** 接管绘制的函数必须用同一条公式,否则同一条减时线会因为谁画而粗细不一 */
15
+ export function divLineWidth(size) {
16
+ return Math.max(1, size * 0.055);
17
+ }
18
+ /**
19
+ * 逐级产出连续提供该级端口的下标区间(含两端,长度至少为 2)
20
+ *
21
+ * 端口按级递增排布是 div 的内部约定,接管绘制的函数只按这里给出的区间连线
22
+ */
23
+ export function* divLineRuns(hosts) {
24
+ for (let level = 0;; level++) {
25
+ let hasPorts = false;
26
+ let from = -1;
27
+ // 多扫一位,让最后一段也走同一条收尾分支
28
+ for (let i = 0; i <= hosts.length; i++) {
29
+ const ports = hosts[i]?.ports;
30
+ const left = ports?.[divLinePortName(level, "left")];
31
+ const right = ports?.[divLinePortName(level, "right")];
32
+ if (left || right)
33
+ hasPorts = true;
34
+ if (left && right) {
35
+ if (from < 0)
36
+ from = i;
37
+ continue;
38
+ }
39
+ if (from >= 0 && i - from >= 2)
40
+ yield { level, from, to: i - 1 };
41
+ from = -1;
42
+ }
43
+ if (!hasPorts)
44
+ return;
24
45
  }
25
- return true;
26
46
  }
27
47
  class DivFunction extends ASTFunctionNode {
28
48
  static def = {
29
49
  name: [DIV_FUNC_NAME, "/"],
30
50
  description: "减时线",
31
- example: `@div(C1, 2): C1下方创建2根减时线
32
- @/({C1 C2 @dash()}, 3): C1、C2和增时线下方创建3根减时线并连接
33
- 语法糖:在音符后加斜杠'/',可以多个
34
- @div(C1, 2) === C1//
35
- @div(C1 C2/ @dash(), 2) === {C1 C2/ @dash()}//
36
- 设置命名参数前缀: div
51
+ example: `@div(C1, 2): C1下方创建2根减时线
52
+ @/({C1 C2 @dash()}, 3): C1、C2和增时线下方创建3根减时线并连接
53
+ 语法糖:在音符后加斜杠'/',可以多个
54
+ @div(C1, 2) === C1//
55
+ @div(C1 C2/ @dash(), 2) === {C1 C2/ @dash()}//
56
+ 设置命名参数前缀: div
37
57
  `,
38
58
  allowExtraArgs: false,
39
59
  args: [
@@ -46,6 +66,11 @@ class DivFunction extends ASTFunctionNode {
46
66
  type: "number",
47
67
  default: 1,
48
68
  },
69
+ {
70
+ name: "autobeam",
71
+ type: "boolean",
72
+ default: true,
73
+ },
49
74
  ]
50
75
  };
51
76
  static deSugarAtom(source, start, end) {
@@ -104,7 +129,7 @@ class DivFunction extends ASTFunctionNode {
104
129
  if (count === 0)
105
130
  return null;
106
131
  const lineGap = host.ast.size * 0.14;
107
- const strokeWidth = Math.max(1, host.ast.size * 0.055);
132
+ const strokeWidth = divLineWidth(host.ast.size);
108
133
  const lineBlockHeight = (count - 1) * lineGap + strokeWidth;
109
134
  let lineLeft = 0;
110
135
  let lineRight = 0;
@@ -155,7 +180,7 @@ class DivFunction extends ASTFunctionNode {
155
180
  return;
156
181
  const addon = node.addon ??= {};
157
182
  addon[DIV_ADDON_KEY] = (Number(addon[DIV_ADDON_KEY]) || 0) + count;
158
- node.T /= 2 ** count;
183
+ node.T.divPow2(count);
159
184
  },
160
185
  });
161
186
  return [];
@@ -177,11 +202,14 @@ class DivFunction extends ASTFunctionNode {
177
202
  }
178
203
  constructor(sourceSpan, args, ctx, parent = null) {
179
204
  super(sourceSpan, parent);
180
- [this.content, this.n] = this.getArgValue(args, ctx);
181
- // beam loweringAugment 只读取这个快照,不读取之后可能已经变化的 ParserContext
182
- this.autoBeamEnabled = parseAutoBeamFlag(ctx.variables["autobeam"]);
205
+ [this.content, this.n, this.autoBeamEnabled] = this.getArgValue(args, ctx);
183
206
  // div 允许修饰任意 都会加下划线
184
207
  this.content.parent = this;
208
+ const n = Math.max(0, Math.trunc(this.n));
209
+ if (n !== this.n) {
210
+ ctx.diagnostics.push(new WarningDiagnostic("W_DIV_INVALID_N", `@div 的数量参数 n 必须为非负整数,已被修正为 ${n}`, this.sourceSpan));
211
+ this.n = n;
212
+ }
185
213
  }
186
214
  toString(source) {
187
215
  return `@div(${this.content.toString(source)}, ${this.n})`;