jpfun 0.3.0 → 0.4.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 (125) hide show
  1. package/README.md +73 -1
  2. package/dist/converter/midi/convert.d.ts +50 -0
  3. package/dist/converter/midi/convert.js +700 -0
  4. package/dist/converter/midi/index.d.ts +5 -0
  5. package/dist/converter/midi/index.js +11 -0
  6. package/dist/converter/midi/json.d.ts +38 -0
  7. package/dist/converter/midi/json.js +1 -0
  8. package/dist/converter/midi/triplet.d.ts +30 -0
  9. package/dist/converter/midi/triplet.js +75 -0
  10. package/dist/converter/musicxml/dom.d.ts +25 -0
  11. package/dist/converter/musicxml/dom.js +65 -0
  12. package/dist/converter/musicxml/features.d.ts +51 -0
  13. package/dist/converter/musicxml/features.js +232 -0
  14. package/dist/converter/musicxml/index.d.ts +9 -0
  15. package/dist/converter/musicxml/index.js +1140 -0
  16. package/dist/converter/musicxml/model.d.ts +123 -0
  17. package/dist/converter/musicxml/model.js +1 -0
  18. package/dist/converter/source.d.ts +22 -0
  19. package/dist/converter/source.js +51 -0
  20. package/dist/core.d.ts +22 -0
  21. package/dist/core.js +22 -0
  22. package/dist/functions/ASTtypes.d.ts +28 -25
  23. package/dist/functions/ASTtypes.js +25 -23
  24. package/dist/functions/adjust/index.js +1 -1
  25. package/dist/functions/arpeggio/index.d.ts +2 -0
  26. package/dist/functions/arpeggio/index.js +180 -0
  27. package/dist/functions/arpeggio/shape.d.ts +7 -0
  28. package/dist/functions/arpeggio/shape.js +122 -0
  29. package/dist/functions/bar/index.js +23 -15
  30. package/dist/functions/beam/auto.d.ts +1 -1
  31. package/dist/functions/beam/auto.js +1 -1
  32. package/dist/functions/beam/layout.d.ts +2 -2
  33. package/dist/functions/beam/layout.js +1 -1
  34. package/dist/functions/box/index.js +2 -2
  35. package/dist/functions/br/index.js +1 -1
  36. package/dist/functions/dash/index.js +7 -6
  37. package/dist/functions/default.js +6 -2
  38. package/dist/functions/dot/index.js +12 -22
  39. package/dist/functions/dyn/index.d.ts +2 -0
  40. package/dist/functions/dyn/index.js +242 -0
  41. package/dist/functions/grace/index.d.ts +1 -1
  42. package/dist/functions/grace/index.js +2 -3
  43. package/dist/functions/head/index.js +149 -145
  44. package/dist/functions/key/index.js +5 -4
  45. package/dist/functions/meter/index.js +6 -5
  46. package/dist/functions/note/index.js +68 -21
  47. package/dist/functions/note/noteNameFSM.js +52 -63
  48. package/dist/functions/page/index.d.ts +2 -0
  49. package/dist/functions/page/index.js +22 -7
  50. package/dist/functions/program/index.d.ts +2 -0
  51. package/dist/functions/program/index.js +30 -0
  52. package/dist/functions/set/index.js +8 -5
  53. package/dist/functions/symbol/index.d.ts +13 -3
  54. package/dist/functions/symbol/index.js +61 -16
  55. package/dist/functions/symbol/shape.d.ts +2 -0
  56. package/dist/functions/symbol/shape.js +14 -10
  57. package/dist/functions/symbol/symbols/dc.d.ts +4 -0
  58. package/dist/functions/symbol/symbols/dc.js +82 -0
  59. package/dist/functions/symbol/symbols/{f.d.ts → ds.d.ts} +1 -1
  60. package/dist/functions/symbol/symbols/ds.js +93 -0
  61. package/dist/functions/symbol/symbols/dynamics.d.ts +3 -0
  62. package/dist/functions/symbol/symbols/dynamics.js +168 -0
  63. package/dist/functions/symbol/symbols/{p.d.ts → fine.d.ts} +1 -1
  64. package/dist/functions/symbol/symbols/fine.js +12 -0
  65. package/dist/functions/symbol/symbols/mordent.js +44 -31
  66. package/dist/functions/symbol/symbols/prall.d.ts +2 -0
  67. package/dist/functions/symbol/symbols/prall.js +42 -0
  68. package/dist/functions/symbol/symbols/segno.d.ts +4 -0
  69. package/dist/functions/symbol/symbols/segno.js +82 -0
  70. package/dist/functions/symbol/symbols/tr.js +0 -4
  71. package/dist/functions/tempo/index.js +5 -3
  72. package/dist/functions/temporal.d.ts +138 -0
  73. package/dist/functions/temporal.js +101 -0
  74. package/dist/functions/text/index.js +14 -7
  75. package/dist/functions/tie/index.js +37 -28
  76. package/dist/functions/tuplet/index.js +14 -7
  77. package/dist/functions/up/index.js +23 -21
  78. package/dist/functions/voice/index.js +121 -45
  79. package/dist/functions/volta/index.js +51 -48
  80. package/dist/iife/core.d.ts +1 -0
  81. package/dist/iife/core.js +3 -0
  82. package/dist/iife/from-midi.d.ts +1 -0
  83. package/dist/iife/from-midi.js +15 -0
  84. package/dist/iife/from-musicxml.d.ts +1 -0
  85. package/dist/iife/from-musicxml.js +3 -0
  86. package/dist/iife/full.d.ts +1 -0
  87. package/dist/iife/full.js +3 -0
  88. package/dist/index.d.ts +3 -21
  89. package/dist/index.js +3 -21
  90. package/dist/jpfun.core.min.js +153 -0
  91. package/dist/jpfun.from-midi.min.js +9 -0
  92. package/dist/jpfun.from-musicxml.min.js +12 -0
  93. package/dist/jpfun.min.js +71 -48
  94. package/dist/layout/default.js +2 -3
  95. package/dist/layout/engine.d.ts +2 -1
  96. package/dist/layout/engine.js +71 -26
  97. package/dist/layout/model.d.ts +4 -4
  98. package/dist/layout/model.js +15 -12
  99. package/dist/layout/page.js +5 -4
  100. package/dist/layout/path.js +3 -3
  101. package/dist/layout/types.d.ts +20 -2
  102. package/dist/lowering/loweringContext.d.ts +3 -1
  103. package/dist/lowering/loweringContext.js +28 -14
  104. package/dist/lowering/track.d.ts +4 -8
  105. package/dist/lowering/track.js +6 -1
  106. package/dist/lowering/types.d.ts +5 -128
  107. package/dist/lowering/types.js +1 -98
  108. package/dist/parser/parse-utils/string-utils.d.ts +2 -0
  109. package/dist/parser/parse-utils/string-utils.js +4 -0
  110. package/dist/parser/parserContext.d.ts +7 -8
  111. package/dist/parser/parserContext.js +41 -36
  112. package/dist/pipeline.d.ts +6 -5
  113. package/dist/pipeline.js +4 -5
  114. package/dist/playback/compile.d.ts +1 -1
  115. package/dist/playback/compile.js +15 -2
  116. package/dist/playback/event.d.ts +17 -3
  117. package/dist/playback/event.js +30 -12
  118. package/dist/playback/time.js +1 -1
  119. package/dist/playback/types.d.ts +5 -4
  120. package/dist/render/text.d.ts +6 -1
  121. package/dist/render/text.js +8 -1
  122. package/grammar.md +44 -8
  123. package/package.json +11 -2
  124. package/dist/functions/symbol/symbols/f.js +0 -55
  125. package/dist/functions/symbol/symbols/p.js +0 -61
@@ -0,0 +1,242 @@
1
+ import { Diagnostic, ErrorDiagnostic, WarningDiagnostic } from "../../diagnostic.js";
2
+ import { isVisualTemporalNode, } from "../temporal.js";
3
+ import { ASTFunctionNode, } from "../ASTtypes.js";
4
+ class DynFunction extends ASTFunctionNode {
5
+ static def = {
6
+ name: ["dyn"],
7
+ description: "渐强渐弱",
8
+ example: `@dyn(from, to, dv) 在两个标签之间线性改变力度
9
+ from、to 必须位于同一音轨且时间不同
10
+ dv 为正时渐强,为负时渐弱;变化量叠加在每个音符原有力度上
11
+
12
+ 1 ^ $p 2@a 3 4@b
13
+ @dyn(a, b, 24)`,
14
+ allowExtraArgs: false,
15
+ args: [
16
+ { name: "from", type: "label", default: null },
17
+ { name: "to", type: "label", default: null },
18
+ { name: "dv", type: "number", default: null },
19
+ ],
20
+ };
21
+ static loweringFinalize = (result) => {
22
+ const dynamics = result.attachments.filter((attachment) => attachment instanceof DynAttachment);
23
+ if (dynamics.length === 0)
24
+ return;
25
+ // astToTemporal 包含不进入全局 columns 的和弦、倚音成员;host 只用于统一时间和音轨
26
+ const records = [];
27
+ for (const nodes of result.astToTemporal.values()) {
28
+ for (const node of nodes) {
29
+ const velocity = node.playbackState?.velocity;
30
+ if (velocity === undefined)
31
+ continue;
32
+ let host = node;
33
+ while (host.foldedInto)
34
+ host = host.foldedInto;
35
+ records.push({ node, host, velocity, increment: 0 });
36
+ }
37
+ }
38
+ records.sort((left, right) => left.host.t.compare(right.host.t) || left.node.order - right.node.order);
39
+ // 先累计全部 dyn 的贡献,最后统一写回,避免声明顺序改变后续 dyn 读取的原始力度
40
+ for (const dynamic of dynamics) {
41
+ const endVelocity = dynamic.end.playbackState?.velocity
42
+ ?? records.find(record => record.host === dynamic.toHost)?.velocity;
43
+ if (endVelocity === undefined)
44
+ throw new Error("@dyn end endpoint has no velocity-bearing member");
45
+ // 终点后保持完整增量,直到原始力度相对终点再次发生变化
46
+ const stop = records.find(record => record.host.track === dynamic.track
47
+ && record.host.t.compare(dynamic.toHost.t) > 0
48
+ && record.velocity !== endVelocity)?.host.t;
49
+ const duration = dynamic.toHost.t.clone().sub(dynamic.fromHost.t);
50
+ for (const record of records) {
51
+ if (record.host.track !== dynamic.track)
52
+ continue;
53
+ const fromStart = record.host.t.compare(dynamic.fromHost.t);
54
+ if (fromStart < 0 || (stop && record.host.t.compare(stop) >= 0))
55
+ continue;
56
+ const progress = record.host.t.compare(dynamic.toHost.t) >= 0
57
+ ? 1
58
+ : record.host.t.clone().sub(dynamic.fromHost.t).div(duration).toNumber();
59
+ record.increment += dynamic.dv * progress;
60
+ }
61
+ }
62
+ for (const record of records) {
63
+ if (record.increment === 0)
64
+ continue;
65
+ const velocity = Math.max(1, Math.min(127, record.velocity + record.increment));
66
+ record.node.playbackState = { ...record.node.playbackState, velocity };
67
+ }
68
+ };
69
+ from;
70
+ to;
71
+ dv;
72
+ size;
73
+ loweringEnter(ctx) {
74
+ const from = ctx.getTemporalNodes(this.from).at(0);
75
+ const to = ctx.getTemporalNodes(this.to).at(-1);
76
+ if (!from || !to) {
77
+ ctx.diagnostics.push(Diagnostic.warning.UnresolvedEndpoint("dyn", this.sourceSpan));
78
+ return [];
79
+ }
80
+ let fromHost = from;
81
+ let toHost = to;
82
+ while (fromHost.foldedInto)
83
+ fromHost = fromHost.foldedInto;
84
+ while (toHost.foldedInto)
85
+ toHost = toHost.foldedInto;
86
+ if (!isVisualTemporalNode(fromHost) || !isVisualTemporalNode(toHost)) {
87
+ ctx.diagnostics.push(Diagnostic.warning.UnresolvedEndpoint("dyn", this.sourceSpan));
88
+ return [];
89
+ }
90
+ if (fromHost.track !== toHost.track) {
91
+ throw new ErrorDiagnostic("E_DYN_CROSS_TRACK", "@dyn 的两个端点必须位于同一音轨", this.sourceSpan);
92
+ }
93
+ if (fromHost.t.equals(toHost.t)) {
94
+ throw new ErrorDiagnostic("E_DYN_ZERO_SPAN", "@dyn 的两个端点必须位于不同时间", this.sourceSpan);
95
+ }
96
+ if (fromHost.t.compare(toHost.t) < 0) {
97
+ ctx.addAttachment(new DynAttachment(this, to, fromHost, toHost));
98
+ }
99
+ else {
100
+ ctx.addAttachment(new DynAttachment(this, from, toHost, fromHost));
101
+ }
102
+ return [];
103
+ }
104
+ constructor(span, args, ctx, parent = null) {
105
+ super(span, parent);
106
+ const [from, to, inputDv] = this.getArgValue(args, ctx);
107
+ let dv = inputDv;
108
+ if (!Number.isFinite(dv) || dv === 0) {
109
+ throw new ErrorDiagnostic("E_DYN_INVALID_DELTA", "@dyn 的 dv 必须是非零有限数", span);
110
+ }
111
+ dv = Math.max(-127, Math.min(127, dv));
112
+ if (dv !== inputDv) {
113
+ ctx.diagnostics.push(new WarningDiagnostic("W_DYN_DELTA_CLAMPED", `@dyn 的 dv 必须在 -127 到 127 之间,已被限制为 ${dv}`, span));
114
+ }
115
+ this.from = from;
116
+ this.to = to;
117
+ this.dv = dv;
118
+ this.size = ctx.variables.fontsize;
119
+ }
120
+ toString(source) {
121
+ const name = (node) => node.label ?? node.toString(source);
122
+ return `@dyn(${name(this.from)}, ${name(this.to)}, ${this.dv})`;
123
+ }
124
+ }
125
+ export const DynNode = DynFunction;
126
+ class DynAttachment {
127
+ ast;
128
+ end;
129
+ fromHost;
130
+ toHost;
131
+ layer = "foreground";
132
+ constructor(ast, end, fromHost, toHost) {
133
+ this.ast = ast;
134
+ this.end = end;
135
+ this.fromHost = fromHost;
136
+ this.toHost = toHost;
137
+ }
138
+ get sourceSpan() { return this.ast.sourceSpan; }
139
+ get dv() { return this.ast.dv; }
140
+ get track() { return this.fromHost.track; }
141
+ createGeometry(context) {
142
+ const locate = (host) => {
143
+ const view = context.lines[host.layoutLine];
144
+ return { line: host.layoutLine, view, column: view?.columnOf(host) ?? -1 };
145
+ };
146
+ const from = locate(this.fromHost);
147
+ const to = locate(this.toHost);
148
+ if (!from.view || !to.view || from.column < 0 || to.column < 0) {
149
+ return { regions: [], paint() { } };
150
+ }
151
+ const ranges = [];
152
+ let size = this.ast.size;
153
+ for (let line = from.line; line <= to.line; line++) {
154
+ const view = context.lines[line];
155
+ const start = line === from.line ? from.column : 0;
156
+ const end = line === to.line ? to.column : view.columns.length - 1;
157
+ let firstColumn = -1;
158
+ let lastColumn = -1;
159
+ for (let column = start; column <= end; column++) {
160
+ for (const host of view.columns[column]) {
161
+ if (host.track !== this.track)
162
+ continue;
163
+ firstColumn = firstColumn < 0 ? column : firstColumn;
164
+ lastColumn = column;
165
+ size = Math.max(size, host.ast.size);
166
+ }
167
+ }
168
+ if (firstColumn < 0)
169
+ continue;
170
+ ranges.push({
171
+ line,
172
+ startX: line === from.line ? this.fromHost.box.x : this.lineEntryX(context, line),
173
+ endX: line === to.line
174
+ ? this.toHost.box.x + this.toHost.box.w
175
+ : this.lineRightX(context, line),
176
+ columns: [firstColumn, lastColumn],
177
+ });
178
+ }
179
+ // 允许左边超出一点
180
+ for (const range of ranges) {
181
+ if (range.line > from.line)
182
+ range.startX -= size / 4;
183
+ }
184
+ const strokeWidth = Math.max(0.75, size * 0.035);
185
+ const valid = ranges.filter(range => range.startX !== range.endX);
186
+ const total = valid.reduce((sum, range) => sum + Math.abs(range.endX - range.startX), 0);
187
+ if (total === 0)
188
+ return { regions: [], paint() { } };
189
+ const aperture = size * 0.4; // 最终开口高度
190
+ const halfStroke = strokeWidth / 2;
191
+ let covered = 0;
192
+ const segments = valid.map(range => {
193
+ const width = Math.abs(range.endX - range.startX);
194
+ const start = covered / total;
195
+ const end = (covered + width) / total;
196
+ covered += width;
197
+ const extent = context.getRangeExtents(range.line, range.columns).get(this.track);
198
+ const axis = context.getVisualAxis(range.line, this.track);
199
+ const hostTop = extent ? axis + extent.top : axis;
200
+ return {
201
+ line: range.line,
202
+ startX: range.startX,
203
+ endX: range.endX,
204
+ lineY: hostTop - size * 0.62,
205
+ startOpen: aperture * (this.dv > 0 ? start : 1 - start),
206
+ endOpen: aperture * (this.dv > 0 ? end : 1 - end),
207
+ };
208
+ });
209
+ return {
210
+ regions: segments.map(segment => ({
211
+ x: Math.min(segment.startX, segment.endX) - halfStroke,
212
+ y: segment.lineY - Math.max(segment.startOpen, segment.endOpen) / 2 - halfStroke,
213
+ w: Math.abs(segment.endX - segment.startX) + strokeWidth,
214
+ h: Math.max(segment.startOpen, segment.endOpen) + strokeWidth,
215
+ line: segment.line,
216
+ track: this.track,
217
+ })),
218
+ paint(painter) {
219
+ const style = { stroke: "#000", strokeWidth };
220
+ for (const segment of segments) {
221
+ painter.drawLine(segment.startX, segment.lineY - segment.startOpen / 2, segment.endX, segment.lineY - segment.endOpen / 2, style);
222
+ painter.drawLine(segment.startX, segment.lineY + segment.startOpen / 2, segment.endX, segment.lineY + segment.endOpen / 2, style);
223
+ }
224
+ },
225
+ };
226
+ }
227
+ lineEntryX(context, line) {
228
+ const host = context.lines[line].trackRuns.get(this.track)?.find(item => !item.T.isZero());
229
+ return host?.box.x ?? context.originX;
230
+ }
231
+ lineRightX(context, line) {
232
+ const runs = context.lines[line].trackRuns;
233
+ if (runs.size === 0)
234
+ return context.originX + context.width;
235
+ let right = context.originX;
236
+ for (const run of runs.values()) {
237
+ for (const host of run)
238
+ right = Math.max(right, host.box.x + host.box.w);
239
+ }
240
+ return right;
241
+ }
242
+ }
@@ -1,7 +1,7 @@
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 TimeState, type VisualTemporalNode } from "../../lowering/types.js";
4
+ import { TemporalNodeBase, type TimeState, type VisualTemporalNode } from "../temporal.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";
@@ -1,7 +1,7 @@
1
1
  import { ErrorDiagnostic } from "../../diagnostic.js";
2
2
  import { prepareLayoutHost } from "../../layout/engine.js";
3
3
  import { Fraction } from "../../fraction.js";
4
- import { isVisualTemporalNode, TemporalNodeBase, } from "../../lowering/types.js";
4
+ import { isVisualTemporalNode, TemporalNodeBase, } from "../temporal.js";
5
5
  import { ASTBraceNode, ASTFunctionNode, ASTLabelNode, ASTTextNode, } from "../ASTtypes.js";
6
6
  import { createBeamLayoutAttachment } from "../beam/layout.js";
7
7
  import { DIV_ADDON_KEY } from "../div/index.js";
@@ -180,14 +180,13 @@ class GraceFunction extends ASTFunctionNode {
180
180
  }
181
181
  constructor(span, args, ctx, parent = null, scale = GRACE_SCALE) {
182
182
  super(span, parent);
183
- this.size = ctx.fontSize;
183
+ this.size = ctx.variables.fontsize;
184
184
  [this.host, this.grace, this.side] = this.getArgValue(args, ctx);
185
185
  if (this.side !== "pre" && this.side !== "post") {
186
186
  throw new ErrorDiagnostic("E_GRACE_INVALID_SIDE", "@grace 的 side 参数必须是 'pre' 或 'post'", span);
187
187
  }
188
188
  this.adopt(this.host);
189
189
  this.setGrace(this.grace, scale);
190
- this.size = ctx.fontSize;
191
190
  }
192
191
  setGrace(node, factor = GRACE_SCALE) {
193
192
  scaleFontSize(node, factor);
@@ -1,6 +1,6 @@
1
1
  import { ErrorDiagnostic } from "../../diagnostic.js";
2
2
  import { layoutHorizontalRegion } from "../../layout/model.js";
3
- import { isVisualTemporalNode, TemporalNodeBase, } from "../../lowering/types.js";
3
+ import { isVisualTemporalNode, TemporalNodeBase, } from "../temporal.js";
4
4
  import { ParserContext, skipSpaces, skipSpacesBack } from "../../parser/parserContext.js";
5
5
  import { ASTBraceNode, ASTFunctionNode, ASTNodeBase, } from "../ASTtypes.js";
6
6
  import { KeyNode } from "../key/index.js";
@@ -72,6 +72,11 @@ function measureRows(members, gap, capture) {
72
72
  return placements;
73
73
  }
74
74
  class HeadBoundaryTemporal extends TemporalNodeBase {
75
+ members = [];
76
+ slots = [];
77
+ titleBottom = 0;
78
+ leftExtent = null;
79
+ rightExtent = null;
75
80
  constructor(ast) {
76
81
  super();
77
82
  this.ast = ast;
@@ -80,12 +85,128 @@ class HeadBoundaryTemporal extends TemporalNodeBase {
80
85
  prepareLayout() { this.box.w = this.box.h = this.box.anchor = this.box.visualAxis = 0; }
81
86
  prepareHorizontal(line) {
82
87
  // 只由第一个边界协调一次整块 Head,避免六个边界重复改弹簧
83
- if (this.ast.name === "left" && this === this.ast.start)
84
- this.ast.owner.prepareHead(line);
88
+ if (this.slots.length > 0)
89
+ this.prepareHead(line);
85
90
  }
86
91
  onPlaced() {
87
- if (this.ast.name === "left" && this === this.ast.start)
88
- this.ast.owner.alignHead();
92
+ if (this.slots.length > 0)
93
+ this.alignHead();
94
+ }
95
+ createTracks(slot) {
96
+ const { name, owner } = slot;
97
+ return {
98
+ laneKey: `head/rows/${owner.sourceSpan.start}/${name}`,
99
+ hostIndex: null,
100
+ measure: members => {
101
+ if (name === "center") {
102
+ const placements = measureRows(members, owner.gap);
103
+ const first = placements.find(placement => placement !== null);
104
+ this.titleBottom = first ? first.offset + first.extent.bottom : 0;
105
+ return placements;
106
+ }
107
+ return measureRows(members, owner.gap, extent => {
108
+ if (name === "left")
109
+ this.leftExtent = extent;
110
+ else
111
+ this.rightExtent = extent;
112
+ });
113
+ },
114
+ place: name === "center" ? undefined : () => {
115
+ const extent = name === "left" ? this.leftExtent : this.rightExtent;
116
+ if (!extent)
117
+ return this.titleBottom;
118
+ const leftHeight = this.leftExtent ? this.leftExtent.bottom - this.leftExtent.top : 0;
119
+ const rightHeight = this.rightExtent ? this.rightExtent.bottom - this.rightExtent.top : 0;
120
+ // 两侧共享底边;以较高侧定总高度,使它的顶部恰好贴标题底边
121
+ const targetBottom = this.titleBottom + Math.max(leftHeight, rightHeight);
122
+ return targetBottom - extent.bottom;
123
+ },
124
+ };
125
+ }
126
+ prepareHead(line) {
127
+ if (this.members.some(node => isVisualTemporalNode(node) && node.layoutLine !== line.index)) {
128
+ throw new ErrorDiagnostic("E_HEAD_INTERNAL_BREAK", "@head 的内容不能跨谱面行", this.ast.owner.sourceSpan);
129
+ }
130
+ const [left, center, right] = this.slots;
131
+ // 最外侧不受墙的推力,两个点才能落在纸面边缘
132
+ left.start.springConfig.alpha_L = 0;
133
+ right.end.springConfig.alpha_R = 0;
134
+ // 串联弹簧的静长是两端之和、刚度由软端主导,所以只需 center 自己向两侧撑开
135
+ this.stretch(center.start, "L");
136
+ this.stretch(center.end, "R");
137
+ for (const slot of this.slots) {
138
+ line.registerHorizontalLayoutHook(slot.start, slot.end, context => layoutHeadSlot(context, slot === center));
139
+ }
140
+ }
141
+ alignHead() {
142
+ // 全局列已经完成求解;这里不改 TimeColumn,只覆盖 left/right 主体的最终 box.x
143
+ // center 不参与后置重排,继续使用全局列坐标
144
+ const cells = new Map();
145
+ for (const member of this.members) {
146
+ if (!isVisualTemporalNode(member))
147
+ continue;
148
+ const cell = this.sideCellOf(member.ast);
149
+ if (!cell)
150
+ continue;
151
+ const members = cells.get(cell);
152
+ if (members)
153
+ members.push(member);
154
+ else
155
+ cells.set(cell, [member]);
156
+ }
157
+ const [left, , right] = this.slots;
158
+ this.packRows(left.ast, left.start.box.x, "left", cells);
159
+ this.packRows(right.ast, right.end.box.x, "right", cells);
160
+ }
161
+ stretch(node, side) {
162
+ const alpha = side === "L" ? "alpha_L" : "alpha_R";
163
+ const beta = side === "L" ? "beta_L" : "beta_R";
164
+ // alpha 撑开槽间空间;beta 反向缩放,避免弹簧刚度随 alpha 一起失控
165
+ node.springConfig[alpha] = node.springConfig[alpha] * HEAD_STRETCH;
166
+ node.springConfig[beta] = node.springConfig[beta] * 3 / HEAD_STRETCH;
167
+ }
168
+ packRows(slot, target, side, cells) {
169
+ const gap = this.ast.owner.size * ITEM_GAP_EM;
170
+ const fromLeft = side === "left";
171
+ const direction = fromLeft ? 1 : -1;
172
+ for (const row of slot.rows) {
173
+ const rowCells = row instanceof ASTBraceNode ? row.content : [row];
174
+ let cursor = target;
175
+ for (let index = fromLeft ? 0 : rowCells.length - 1; index >= 0 && index < rowCells.length; index += direction) {
176
+ const members = cells.get(rowCells[index]);
177
+ if (!members)
178
+ continue;
179
+ let left = Infinity;
180
+ let right = -Infinity;
181
+ for (const member of members) {
182
+ left = Math.min(left, member.box.x);
183
+ right = Math.max(right, member.box.x + member.box.w);
184
+ }
185
+ const offset = cursor - (fromLeft ? left : right);
186
+ for (const member of members)
187
+ member.box.x += offset;
188
+ // 复合单元先整体平移,再让其内部成员读取彼此的最终坐标
189
+ for (const member of members)
190
+ member.onPlaced?.();
191
+ cursor += direction * (right - left + gap);
192
+ }
193
+ }
194
+ }
195
+ /** 找到成员所属侧栏行的顶层单元;复合节点内部成员会归回同一个直接子节点 */
196
+ sideCellOf(ast) {
197
+ let cell = ast;
198
+ while (cell.parent) {
199
+ const parent = cell.parent;
200
+ const slot = parent instanceof HeadSlotNode
201
+ ? parent
202
+ : parent instanceof ASTBraceNode && parent.parent instanceof HeadSlotNode
203
+ ? parent.parent
204
+ : null;
205
+ if (slot?.owner === this.ast.owner)
206
+ return slot.name === "center" ? null : cell;
207
+ cell = parent;
208
+ }
209
+ return null;
89
210
  }
90
211
  }
91
212
  /** 一个槽既是 AST 容器,也是把每个顶层行分配到独立 Track 的边界 */
@@ -93,37 +214,32 @@ class HeadSlotNode extends ASTNodeBase {
93
214
  owner;
94
215
  name;
95
216
  rows;
96
- tracks;
97
- start;
98
- end;
99
- constructor(span, owner, name, rows, tracks) {
217
+ constructor(span, owner, name, rows) {
100
218
  super(span, owner);
101
219
  this.owner = owner;
102
220
  this.name = name;
103
221
  this.rows = rows;
104
- this.tracks = tracks;
105
222
  for (const row of this.rows)
106
223
  row.parent = this;
107
224
  }
108
225
  // 零宽边界需要字号契约,但字号由 Head 统一冻结,不在三个槽里重复保存
109
226
  get size() { return this.owner.size; }
110
227
  get children() { return this.rows; }
111
- timeFlowModel() {
228
+ timeFlowModel(ctx) {
112
229
  if (this.rows.length === 0)
113
230
  return null;
231
+ const head = ctx.getTemporalNodes(this.owner.slots[0])[0];
114
232
  return {
115
233
  mode: "parallel",
116
234
  children: this.rows,
117
- tracks: this.tracks,
235
+ tracks: head.createTracks(this),
118
236
  };
119
237
  }
120
238
  loweringEnter() {
121
- this.start = new HeadBoundaryTemporal(this);
122
- return [this.start];
239
+ return [new HeadBoundaryTemporal(this)];
123
240
  }
124
241
  loweringExit() {
125
- this.end = new HeadBoundaryTemporal(this);
126
- return [this.end];
242
+ return [new HeadBoundaryTemporal(this)];
127
243
  }
128
244
  addRow(row) {
129
245
  row.parent = this;
@@ -201,7 +317,6 @@ H.left: / H.center: / H.right: 接受对应槽的任意零时长 DSL 内容
201
317
  slots;
202
318
  size;
203
319
  gap;
204
- members = [];
205
320
  createdBySugar = false;
206
321
  get children() { return this.slots; }
207
322
  timeFlowModel() {
@@ -320,39 +435,8 @@ H.left: / H.center: / H.right: 接受对应槽的任意零时长 DSL 内容
320
435
  }
321
436
  constructor(span, args, ctx, parent = null) {
322
437
  super(span, parent);
323
- this.size = ctx.fontSize;
438
+ this.size = ctx.variables.fontsize;
324
439
  this.gap = ctx.length2px(this.getArgValue(args, ctx)[3]);
325
- // laneKey 必须区分同一宿主轨上的多个 Head;源码起点是稳定且无需额外存储的身份
326
- const laneKey = `head/rows/${span.start}`;
327
- let titleBottom = 0;
328
- let leftExtent = null;
329
- let rightExtent = null;
330
- const sideTracks = (name) => ({
331
- laneKey: `${laneKey}/${name}`,
332
- hostIndex: null,
333
- measure: members => measureRows(members, this.gap, extent => {
334
- if (name === "left")
335
- leftExtent = extent;
336
- else
337
- rightExtent = extent;
338
- }),
339
- place: () => {
340
- const extent = name === "left" ? leftExtent : rightExtent;
341
- if (!extent)
342
- return titleBottom;
343
- const leftHeight = leftExtent ? leftExtent.bottom - leftExtent.top : 0;
344
- const rightHeight = rightExtent ? rightExtent.bottom - rightExtent.top : 0;
345
- // 两侧共享底边;以较高侧定总高度,使它的顶部恰好贴标题底边
346
- const targetBottom = titleBottom + Math.max(leftHeight, rightHeight);
347
- return targetBottom - extent.bottom;
348
- },
349
- });
350
- const centerMeasure = members => {
351
- const placements = measureRows(members, this.gap);
352
- const first = placements.find(placement => placement !== null);
353
- titleBottom = first ? first.offset + first.extent.bottom : 0;
354
- return placements;
355
- };
356
440
  const rows = (name, index) => {
357
441
  const value = args.get(name) ?? args.get(index);
358
442
  if (!(value instanceof ASTNodeBase))
@@ -360,24 +444,29 @@ H.left: / H.center: / H.right: 接受对应槽的任意零时长 DSL 内容
360
444
  return value instanceof ASTBraceNode ? value.content : [value];
361
445
  };
362
446
  this.slots = [
363
- new HeadSlotNode(span, this, "left", rows("left", 0), sideTracks("left")),
364
- new HeadSlotNode(span, this, "center", rows("center", 1), {
365
- laneKey: `${laneKey}/center`,
366
- hostIndex: null,
367
- measure: centerMeasure,
368
- }),
369
- new HeadSlotNode(span, this, "right", rows("right", 2), sideTracks("right")),
447
+ new HeadSlotNode(span, this, "left", rows("left", 0)),
448
+ new HeadSlotNode(span, this, "center", rows("center", 1)),
449
+ new HeadSlotNode(span, this, "right", rows("right", 2)),
370
450
  ];
371
451
  }
372
452
  loweringEnter(ctx) {
373
- this.members.length = 0;
374
453
  // 收集真实全局事件:退出时校验时值,放置后按 AST 行归属做横向对齐
375
- ctx.beginLoweringGroup(this, { onTemporal: node => this.members.push(node) });
454
+ const group = {
455
+ members: [],
456
+ onTemporal(node) { this.members.push(node); },
457
+ };
458
+ ctx.beginLoweringGroup(this, group);
376
459
  return [];
377
460
  }
378
461
  loweringExit(ctx) {
379
- ctx.endLoweringGroup(this);
380
- for (const node of this.members) {
462
+ const { members } = ctx.endLoweringGroup(this);
463
+ const head = ctx.getTemporalNodes(this.slots[0])[0];
464
+ head.members = members;
465
+ head.slots = this.slots.map(ast => {
466
+ const [start, end] = ctx.getTemporalNodes(ast);
467
+ return { ast, start, end };
468
+ });
469
+ for (const node of members) {
381
470
  if (!node.T.isZero()) {
382
471
  throw new ErrorDiagnostic("E_HEAD_NONZERO_DURATION", "@head 的内容必须全部为零时长", node.ast.sourceSpan);
383
472
  }
@@ -390,41 +479,6 @@ H.left: / H.center: / H.right: 接受对应槽的任意零时长 DSL 内容
390
479
  addSugarRow(name, row) {
391
480
  this.slots.find(slot => slot.name === name).addRow(row);
392
481
  }
393
- prepareHead(line) {
394
- if (this.members.some(node => isVisualTemporalNode(node) && node.layoutLine !== line.index)) {
395
- throw new ErrorDiagnostic("E_HEAD_INTERNAL_BREAK", "@head 的内容不能跨谱面行", this.sourceSpan);
396
- }
397
- const [left, center, right] = this.slots;
398
- // 最外侧不受墙的推力,两个点才能落在纸面边缘
399
- left.start.springConfig.alpha_L = 0;
400
- right.end.springConfig.alpha_R = 0;
401
- // 串联弹簧的静长是两端之和、刚度由软端主导,所以只需 center 自己向两侧撑开
402
- this.stretch(center.start, "L");
403
- this.stretch(center.end, "R");
404
- for (const slot of this.slots) {
405
- line.registerHorizontalLayoutHook(slot.start, slot.end, context => layoutHeadSlot(context, slot === center));
406
- }
407
- }
408
- alignHead() {
409
- // 全局列已经完成求解;这里不改 TimeColumn,只覆盖 left/right 主体的最终 box.x
410
- // center 不参与后置重排,继续使用全局列坐标
411
- const cells = new Map();
412
- for (const member of this.members) {
413
- if (!isVisualTemporalNode(member))
414
- continue;
415
- const cell = this.sideCellOf(member.ast);
416
- if (!cell)
417
- continue;
418
- const members = cells.get(cell);
419
- if (members)
420
- members.push(member);
421
- else
422
- cells.set(cell, [member]);
423
- }
424
- const [left, , right] = this.slots;
425
- this.packRows(left, left.start.box.x, "left", cells);
426
- this.packRows(right, right.end.box.x, "right", cells);
427
- }
428
482
  toString(source) {
429
483
  const slots = this.slots
430
484
  .filter(slot => slot.rows.length > 0)
@@ -438,55 +492,5 @@ H.left: / H.center: / H.right: 接受对应槽的任意零时长 DSL 内容
438
492
  slots.push(` gap=${this.gap}px`);
439
493
  return `@head(\n${slots.join(",\n")}\n)`;
440
494
  }
441
- stretch(node, side) {
442
- const alpha = side === "L" ? "alpha_L" : "alpha_R";
443
- const beta = side === "L" ? "beta_L" : "beta_R";
444
- // alpha 撑开槽间空间;beta 反向缩放,避免弹簧刚度随 alpha 一起失控
445
- node.springConfig[alpha] = node.springConfig[alpha] * HEAD_STRETCH;
446
- node.springConfig[beta] = node.springConfig[beta] * 3 / HEAD_STRETCH;
447
- }
448
- packRows(slot, target, side, cells) {
449
- const gap = this.size * ITEM_GAP_EM;
450
- const fromLeft = side === "left";
451
- const direction = fromLeft ? 1 : -1;
452
- for (const row of slot.rows) {
453
- const rowCells = row instanceof ASTBraceNode ? row.content : [row];
454
- let cursor = target;
455
- for (let index = fromLeft ? 0 : rowCells.length - 1; index >= 0 && index < rowCells.length; index += direction) {
456
- const members = cells.get(rowCells[index]);
457
- if (!members)
458
- continue;
459
- let left = Infinity;
460
- let right = -Infinity;
461
- for (const member of members) {
462
- left = Math.min(left, member.box.x);
463
- right = Math.max(right, member.box.x + member.box.w);
464
- }
465
- const offset = cursor - (fromLeft ? left : right);
466
- for (const member of members)
467
- member.box.x += offset;
468
- // 复合单元先整体平移,再让其内部成员读取彼此的最终坐标
469
- for (const member of members)
470
- member.onPlaced?.();
471
- cursor += direction * (right - left + gap);
472
- }
473
- }
474
- }
475
- /** 找到成员所属侧栏行的顶层单元;复合节点内部成员会归回同一个直接子节点 */
476
- sideCellOf(ast) {
477
- let cell = ast;
478
- while (cell.parent) {
479
- const parent = cell.parent;
480
- const slot = parent instanceof HeadSlotNode
481
- ? parent
482
- : parent instanceof ASTBraceNode && parent.parent instanceof HeadSlotNode
483
- ? parent.parent
484
- : null;
485
- if (slot?.owner === this)
486
- return slot.name === "center" ? null : cell;
487
- cell = parent;
488
- }
489
- return null;
490
- }
491
495
  }
492
496
  export const HeadNode = HeadFunction;