jpfun 0.2.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 (112) hide show
  1. package/README.md +69 -11
  2. package/dist/diagnostic.js +5 -2
  3. package/dist/fraction.d.ts +24 -0
  4. package/dist/fraction.js +132 -0
  5. package/dist/functions/ASTtypes.d.ts +24 -7
  6. package/dist/functions/ASTtypes.js +35 -16
  7. package/dist/functions/adjust/index.d.ts +2 -0
  8. package/dist/functions/adjust/index.js +193 -0
  9. package/dist/functions/bar/index.d.ts +8 -0
  10. package/dist/functions/bar/index.js +39 -10
  11. package/dist/functions/beam/auto.js +32 -15
  12. package/dist/functions/beam/index.js +10 -6
  13. package/dist/functions/beam/layout.d.ts +3 -2
  14. package/dist/functions/beam/layout.js +42 -64
  15. package/dist/functions/box/index.js +35 -34
  16. package/dist/functions/br/index.js +9 -7
  17. package/dist/functions/dash/index.js +33 -1
  18. package/dist/functions/default.js +14 -7
  19. package/dist/functions/div/index.d.ts +16 -0
  20. package/dist/functions/div/index.js +45 -17
  21. package/dist/functions/dot/index.js +11 -3
  22. package/dist/functions/grace/index.d.ts +8 -11
  23. package/dist/functions/grace/index.js +82 -76
  24. package/dist/functions/head/index.d.ts +2 -0
  25. package/dist/functions/head/index.js +492 -0
  26. package/dist/functions/key/index.js +97 -3
  27. package/dist/functions/meter/index.d.ts +2 -0
  28. package/dist/functions/meter/index.js +154 -0
  29. package/dist/functions/note/accidentals.d.ts +24 -3
  30. package/dist/functions/note/accidentals.js +31 -1
  31. package/dist/functions/note/index.js +45 -65
  32. package/dist/functions/page/index.d.ts +9 -2
  33. package/dist/functions/page/index.js +50 -4
  34. package/dist/functions/set/index.js +37 -29
  35. package/dist/functions/stack/index.js +26 -22
  36. package/dist/functions/symbol/index.d.ts +21 -0
  37. package/dist/functions/symbol/index.js +94 -0
  38. package/dist/functions/symbol/ornament.d.ts +15 -0
  39. package/dist/functions/symbol/ornament.js +73 -0
  40. package/dist/functions/symbol/shape.d.ts +22 -0
  41. package/dist/functions/symbol/shape.js +48 -0
  42. package/dist/functions/symbol/symbols/accent.d.ts +2 -0
  43. package/dist/functions/symbol/symbols/accent.js +24 -0
  44. package/dist/functions/symbol/symbols/f.d.ts +2 -0
  45. package/dist/functions/symbol/symbols/f.js +55 -0
  46. package/dist/functions/symbol/symbols/fermata.d.ts +2 -0
  47. package/dist/functions/symbol/symbols/fermata.js +24 -0
  48. package/dist/functions/symbol/symbols/mordent.d.ts +2 -0
  49. package/dist/functions/symbol/symbols/mordent.js +43 -0
  50. package/dist/functions/symbol/symbols/p.d.ts +2 -0
  51. package/dist/functions/symbol/symbols/p.js +61 -0
  52. package/dist/functions/symbol/symbols/tr.d.ts +2 -0
  53. package/dist/functions/symbol/symbols/tr.js +75 -0
  54. package/dist/functions/tempo/glyph.d.ts +12 -0
  55. package/dist/functions/tempo/glyph.js +30 -0
  56. package/dist/functions/tempo/index.js +53 -2
  57. package/dist/functions/text/index.js +76 -24
  58. package/dist/functions/tie/index.js +102 -127
  59. package/dist/functions/tuplet/index.d.ts +2 -0
  60. package/dist/functions/tuplet/index.js +211 -0
  61. package/dist/functions/up/index.d.ts +1 -0
  62. package/dist/functions/up/index.js +245 -171
  63. package/dist/functions/voice/index.js +86 -68
  64. package/dist/functions/volta/index.d.ts +2 -0
  65. package/dist/functions/volta/index.js +227 -0
  66. package/dist/index.d.ts +4 -0
  67. package/dist/index.js +4 -0
  68. package/dist/jpfun.min.js +143 -0
  69. package/dist/layout/engine.d.ts +4 -11
  70. package/dist/layout/engine.js +65 -59
  71. package/dist/layout/model.d.ts +8 -1
  72. package/dist/layout/model.js +16 -6
  73. package/dist/layout/path.d.ts +19 -0
  74. package/dist/layout/path.js +101 -0
  75. package/dist/layout/types.d.ts +51 -18
  76. package/dist/layout/types.js +3 -1
  77. package/dist/lowering/loweringContext.d.ts +18 -20
  78. package/dist/lowering/loweringContext.js +132 -94
  79. package/dist/lowering/track.d.ts +1 -1
  80. package/dist/lowering/track.js +1 -1
  81. package/dist/lowering/types.d.ts +49 -12
  82. package/dist/lowering/types.js +9 -3
  83. package/dist/parser/parse-utils/call-utils.d.ts +0 -2
  84. package/dist/parser/parse-utils/call-utils.js +0 -6
  85. package/dist/parser/parse-utils/note-utils.d.ts +8 -0
  86. package/dist/parser/parse-utils/note-utils.js +35 -13
  87. package/dist/parser/parse-utils/string-utils.d.ts +8 -0
  88. package/dist/parser/parse-utils/string-utils.js +20 -0
  89. package/dist/parser/parserContext.d.ts +8 -5
  90. package/dist/parser/parserContext.js +38 -33
  91. package/dist/parser/preprocess.js +17 -11
  92. package/dist/playback/compile.d.ts +18 -0
  93. package/dist/playback/compile.js +401 -0
  94. package/dist/playback/event.d.ts +91 -0
  95. package/dist/playback/event.js +87 -0
  96. package/dist/playback/time.d.ts +14 -0
  97. package/dist/playback/time.js +81 -0
  98. package/dist/playback/types.d.ts +128 -0
  99. package/dist/playback/types.js +3 -0
  100. package/dist/render/canvas.d.ts +4 -4
  101. package/dist/render/canvas.js +20 -5
  102. package/dist/render/index.d.ts +1 -0
  103. package/dist/render/index.js +1 -0
  104. package/dist/render/paint.d.ts +8 -0
  105. package/dist/render/paint.js +101 -0
  106. package/dist/render/svg.d.ts +3 -3
  107. package/dist/render/svg.js +10 -8
  108. package/dist/render/text.d.ts +17 -0
  109. package/dist/render/text.js +35 -0
  110. package/dist/render/types.d.ts +1 -1
  111. package/grammar.md +93 -10
  112. package/package.json +10 -6
@@ -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,6 +2,7 @@ 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"],
@@ -44,7 +45,6 @@ class BrFunction extends ASTFunctionNode {
44
45
  loweringEnter() {
45
46
  const br = new TemporalNodeBase();
46
47
  br.ast = this;
47
- br.T = 0;
48
48
  br.mergeKey = -1;
49
49
  br.breakBefore = this.offset;
50
50
  return [br];
@@ -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,5 +1,6 @@
1
1
  import { ASTFunctionNode } from "../ASTtypes.js";
2
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", "-"],
@@ -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.T.set(1);
44
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 "./up/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,19 +11,38 @@ 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 = {
@@ -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})`;
@@ -1,9 +1,10 @@
1
1
  import { ASTFunctionNode, functionAddonKey } from "../ASTtypes.js";
2
- import { ErrorDiagnostic } from "../../diagnostic.js";
2
+ import { ErrorDiagnostic, WarningDiagnostic } from "../../diagnostic.js";
3
3
  const DOT_FUNC_NAME = "dot";
4
4
  const DOT_ADDON_KEY = functionAddonKey(DOT_FUNC_NAME);
5
5
  function durationFactor(count) {
6
- return 2 - Math.pow(2, -count);
6
+ const denominator = 2 ** count;
7
+ return [2 * denominator - 1, denominator];
7
8
  }
8
9
  class DotFunction extends ASTFunctionNode {
9
10
  static def = {
@@ -113,7 +114,9 @@ class DotFunction extends ASTFunctionNode {
113
114
  const addon = node.addon ??= {};
114
115
  const current = Number(addon[DOT_ADDON_KEY]) || 0;
115
116
  const total = current + count;
116
- node.T *= durationFactor(total) / durationFactor(current);
117
+ const [totalNumerator, totalDenominator] = durationFactor(total);
118
+ const [currentNumerator, currentDenominator] = durationFactor(current);
119
+ node.T.mul(totalNumerator * currentDenominator, totalDenominator * currentNumerator);
117
120
  addon[DOT_ADDON_KEY] = total;
118
121
  },
119
122
  });
@@ -140,6 +143,11 @@ class DotFunction extends ASTFunctionNode {
140
143
  throw new ErrorDiagnostic("E_DOT_INVALID_CONTENT", `函数 @dot 只能接收 1个元素,但找到了 ${contentJudge} 个`, sourceSpan);
141
144
  }
142
145
  this.content.parent = this;
146
+ const n = Math.max(0, Math.trunc(this.n));
147
+ if (n !== this.n) {
148
+ ctx.diagnostics.push(new WarningDiagnostic("W_DOT_INVALID_N", `@dot 的数量参数 n 必须为非负整数,已被修正为 ${n}`, this.sourceSpan));
149
+ this.n = n;
150
+ }
143
151
  }
144
152
  toString(source) {
145
153
  return `@dot(${this.content.toString(source)}, ${this.n})`;
@@ -1,15 +1,16 @@
1
1
  import type { LayoutBox, LayoutPrepareContext } from "../../layout/types.js";
2
2
  import type { LoweringContext } from "../../lowering/loweringContext.js";
3
3
  import type { Track } from "../../lowering/track.js";
4
- import { TemporalNodeBase, type VisualTemporalNode } from "../../lowering/types.js";
4
+ import { TemporalNodeBase, type TimeState, type VisualTemporalNode } from "../../lowering/types.js";
5
5
  import { GrammarNode, GrammarSugarNode } from "../../parser/grammarType.js";
6
6
  import { ParserContext } from "../../parser/parserContext.js";
7
7
  import type { Painter } from "../../render/types.js";
8
+ import type { PlaybackEmitter } from "../../playback/types.js";
8
9
  import { ASTFunctionClass, ASTFunctionNode, ASTNodeBase, FunctionArgs, FunctionDef, SourceSpan } from "../ASTtypes.js";
9
10
  type GraceSide = "pre" | "post";
10
11
  declare class GraceFunction extends ASTFunctionNode {
11
12
  static def: FunctionDef;
12
- labelable(): this;
13
+ labelable(): ASTFunctionNode | null;
13
14
  static deSugarAtom(source: string, start: number, _end: number): {
14
15
  next: number;
15
16
  node: GrammarSugarNode;
@@ -41,22 +42,24 @@ export declare class GraceTemporal extends TemporalNodeBase {
41
42
  box: LayoutBox;
42
43
  readonly host: VisualTemporalNode;
43
44
  readonly graces: readonly VisualTemporalNode[];
45
+ /** 承担节奏的倚音成员,按零时长标记切段;同段视觉上首尾相接,无条件连成一束减时线 */
46
+ readonly graceRuns: readonly (readonly VisualTemporalNode[])[];
44
47
  readonly side: GraceSide;
45
48
  /** 成员相对本盒左上角的局部偏移,onPlaced 时同步为绝对坐标 */
46
49
  private hostOffset;
47
50
  private graceOffsets;
48
51
  /** 倚音线起点,同样是局部偏移 */
49
52
  private hookOrigin;
50
- private divLines;
51
53
  constructor(ast: GraceFunction, host: VisualTemporalNode, graces: readonly VisualTemporalNode[], side: GraceSide);
52
- onTimeState(state: Record<string, any>): void;
54
+ /** 成员共享全局时间状态,按发声顺序固化 */
55
+ onTimeState(state: TimeState): void;
53
56
  /**
54
57
  * 播放时倚音从宿主借走的时值(前倚音从开头、后倚音从末尾)
55
58
  *
56
59
  * 比例 = 倚音字面总时值 / 四分音符,而四分音符 T=1,所以直接累加成员的 T。
57
60
  * 基准取宿主经过 div 与 dot 后的实际时值;延时线是独立事件、tie 不改 T,天然不计入。
58
61
  */
59
- get stealTime(): number;
62
+ emitPlayback(emitter: PlaybackEmitter): void;
60
63
  /**
61
64
  * 宿主留在轨道基线上,倚音整体抬到它的左上或右上角
62
65
  *
@@ -64,12 +67,6 @@ export declare class GraceTemporal extends TemporalNodeBase {
64
67
  * 因此左右邻居会被固有宽度推开,不会在空间紧张时被压穿。
65
68
  */
66
69
  prepareLayout(context: LayoutPrepareContext): void;
67
- /**
68
- * 把相邻倚音成员的同级减时线连成整条
69
- *
70
- * 折叠成员不进入全局时间列,自动连梁看不到它们,只能由复合体自己连。
71
- */
72
- private connectGraceDivLines;
73
70
  /** 引擎每次改变本节点坐标后,按准备阶段保存的局部偏移重算成员绝对坐标 */
74
71
  onPlaced(): void;
75
72
  paint(painter: Painter): void;