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.
- package/LICENSE +200 -200
- package/README.md +191 -133
- package/dist/diagnostic.js +5 -2
- package/dist/fraction.d.ts +24 -0
- package/dist/fraction.js +132 -0
- package/dist/functions/ASTtypes.d.ts +24 -7
- package/dist/functions/ASTtypes.js +35 -16
- package/dist/functions/adjust/index.d.ts +2 -0
- package/dist/functions/adjust/index.js +193 -0
- package/dist/functions/bar/index.d.ts +8 -0
- package/dist/functions/bar/index.js +48 -19
- package/dist/functions/beam/auto.js +35 -18
- package/dist/functions/beam/index.js +10 -6
- package/dist/functions/beam/layout.d.ts +3 -2
- package/dist/functions/beam/layout.js +42 -64
- package/dist/functions/box/index.js +35 -34
- package/dist/functions/br/index.js +14 -12
- package/dist/functions/dash/index.js +37 -5
- package/dist/functions/default.js +14 -7
- package/dist/functions/div/index.d.ts +16 -0
- package/dist/functions/div/index.js +51 -23
- package/dist/functions/dot/index.js +16 -8
- package/dist/functions/grace/index.d.ts +8 -18
- package/dist/functions/grace/index.js +98 -99
- package/dist/functions/head/index.d.ts +2 -0
- package/dist/functions/head/index.js +492 -0
- package/dist/functions/key/index.js +98 -5
- package/dist/functions/meter/index.d.ts +2 -0
- package/dist/functions/meter/index.js +154 -0
- package/dist/functions/note/accidentals.d.ts +24 -3
- package/dist/functions/note/accidentals.js +31 -1
- package/dist/functions/note/index.js +60 -80
- package/dist/functions/page/index.d.ts +9 -2
- package/dist/functions/page/index.js +50 -4
- package/dist/functions/set/index.js +42 -34
- package/dist/functions/stack/index.js +39 -34
- package/dist/functions/symbol/index.d.ts +21 -0
- package/dist/functions/symbol/index.js +94 -0
- package/dist/functions/symbol/ornament.d.ts +15 -0
- package/dist/functions/symbol/ornament.js +73 -0
- package/dist/functions/symbol/shape.d.ts +22 -0
- package/dist/functions/symbol/shape.js +48 -0
- package/dist/functions/symbol/symbols/accent.d.ts +2 -0
- package/dist/functions/symbol/symbols/accent.js +24 -0
- package/dist/functions/symbol/symbols/f.d.ts +2 -0
- package/dist/functions/symbol/symbols/f.js +55 -0
- package/dist/functions/symbol/symbols/fermata.d.ts +2 -0
- package/dist/functions/symbol/symbols/fermata.js +24 -0
- package/dist/functions/symbol/symbols/mordent.d.ts +2 -0
- package/dist/functions/symbol/symbols/mordent.js +43 -0
- package/dist/functions/symbol/symbols/p.d.ts +2 -0
- package/dist/functions/symbol/symbols/p.js +61 -0
- package/dist/functions/symbol/symbols/tr.d.ts +2 -0
- package/dist/functions/symbol/symbols/tr.js +75 -0
- package/dist/functions/tempo/glyph.d.ts +12 -0
- package/dist/functions/tempo/glyph.js +30 -0
- package/dist/functions/tempo/index.js +53 -3
- package/dist/functions/text/index.js +78 -26
- package/dist/functions/tie/index.js +103 -128
- package/dist/functions/tuplet/index.d.ts +2 -0
- package/dist/functions/tuplet/index.js +211 -0
- package/dist/functions/{over → up}/index.d.ts +1 -0
- package/dist/functions/up/index.js +370 -0
- package/dist/functions/voice/index.js +125 -108
- package/dist/functions/volta/index.d.ts +2 -0
- package/dist/functions/volta/index.js +227 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +4 -0
- package/dist/jpfun.min.js +143 -0
- package/dist/layout/engine.d.ts +4 -11
- package/dist/layout/engine.js +100 -66
- package/dist/layout/model.d.ts +8 -1
- package/dist/layout/model.js +16 -6
- package/dist/layout/path.d.ts +19 -0
- package/dist/layout/path.js +101 -0
- package/dist/layout/types.d.ts +51 -18
- package/dist/layout/types.js +3 -1
- package/dist/lowering/loweringContext.d.ts +19 -23
- package/dist/lowering/loweringContext.js +179 -187
- package/dist/lowering/track.d.ts +33 -10
- package/dist/lowering/track.js +6 -6
- package/dist/lowering/types.d.ts +57 -43
- package/dist/lowering/types.js +20 -4
- package/dist/parser/parse-utils/call-utils.d.ts +0 -2
- package/dist/parser/parse-utils/call-utils.js +0 -6
- package/dist/parser/parse-utils/note-utils.d.ts +8 -0
- package/dist/parser/parse-utils/note-utils.js +35 -13
- package/dist/parser/parse-utils/string-utils.d.ts +8 -0
- package/dist/parser/parse-utils/string-utils.js +20 -0
- package/dist/parser/parserContext.d.ts +8 -5
- package/dist/parser/parserContext.js +38 -33
- package/dist/parser/preprocess.js +17 -11
- package/dist/playback/compile.d.ts +18 -0
- package/dist/playback/compile.js +401 -0
- package/dist/playback/event.d.ts +91 -0
- package/dist/playback/event.js +87 -0
- package/dist/playback/time.d.ts +14 -0
- package/dist/playback/time.js +81 -0
- package/dist/playback/types.d.ts +128 -0
- package/dist/playback/types.js +3 -0
- package/dist/render/canvas.d.ts +4 -4
- package/dist/render/canvas.js +20 -5
- package/dist/render/index.d.ts +1 -0
- package/dist/render/index.js +1 -0
- package/dist/render/paint.d.ts +8 -0
- package/dist/render/paint.js +101 -0
- package/dist/render/svg.d.ts +3 -3
- package/dist/render/svg.js +10 -8
- package/dist/render/text.d.ts +17 -0
- package/dist/render/text.js +35 -0
- package/dist/render/types.d.ts +1 -1
- package/grammar.md +217 -122
- package/package.json +14 -9
- package/dist/functions/over/index.js +0 -307
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { Diagnostic } from "../diagnostic.js";
|
|
2
|
+
import { Fraction } from "../fraction.js";
|
|
3
|
+
import { ANCHOR_KEY, DEFAULT_BPM, DEFAULT_TONALITY, DEFAULT_VELOCITY, } from "./types.js";
|
|
2
4
|
import { Track } from "./track.js";
|
|
3
5
|
/**
|
|
4
6
|
* 把 AST 展开并固化为时间列、attachment 和页面配置
|
|
@@ -12,7 +14,7 @@ import { Track } from "./track.js";
|
|
|
12
14
|
* 大多数 AST 函数通过以下入口参与 lowering(按时间顺序):
|
|
13
15
|
* - [hook] loweringEnter/loweringExit:进入或离开节点时生成 TemporalNode、管理作用域
|
|
14
16
|
* - [API] LoweringContext.beginLoweringGroup/endLoweringGroup:通常在 enter/exit 中被调用,以栈方式观察当前子树产生的 TemporalNode 和 attachment
|
|
15
|
-
* - [API] LoweringContext.
|
|
17
|
+
* - [API] LoweringContext.addAttachment:注册不推进时间的附属对象;当前外层分组会收到该对象
|
|
16
18
|
* - [config] timeFlowModel:声明子节点按 sequence 或 parallel 推进时间;parallel 还要声明音轨复用键与纵向排列策略
|
|
17
19
|
* - [hook] Temporal.onTimeState:锚点归并后固化调性、速度等状态
|
|
18
20
|
* - [hook] loweringAugment:最终列和 layoutLine 固化后扫描结果并生成要追加的 attachment
|
|
@@ -22,7 +24,8 @@ export class LoweringContext {
|
|
|
22
24
|
loweringAugmenters = [];
|
|
23
25
|
loweringFinalizers = [];
|
|
24
26
|
cnt = 0; // 生成唯一id的计数器
|
|
25
|
-
|
|
27
|
+
usedTracks = new Set();
|
|
28
|
+
/** 不推进时间的附属对象 */
|
|
26
29
|
attachments = [];
|
|
27
30
|
/** 当前完整文档的页面配置 */
|
|
28
31
|
page;
|
|
@@ -65,20 +68,23 @@ export class LoweringContext {
|
|
|
65
68
|
*/
|
|
66
69
|
lowerDocument(node) {
|
|
67
70
|
this.cnt = 0;
|
|
71
|
+
this.usedTracks.clear();
|
|
68
72
|
this.attachments = [];
|
|
69
73
|
this.page = void 0;
|
|
70
74
|
this.astToTemporal = new Map();
|
|
71
75
|
this.activeLoweringGroups = [];
|
|
72
76
|
const rootTrack = new Track();
|
|
73
|
-
const
|
|
77
|
+
const duration = new Fraction();
|
|
78
|
+
const columns = this.trackedEvents(node, duration, rootTrack);
|
|
74
79
|
this.solidifyColumns(columns);
|
|
75
80
|
return this.postprocessResult({
|
|
76
81
|
diagnostics: this.diagnostics,
|
|
77
82
|
columns,
|
|
78
83
|
attachments: this.attachments,
|
|
79
84
|
astToTemporal: this.astToTemporal,
|
|
80
|
-
duration
|
|
85
|
+
duration,
|
|
81
86
|
rootTrack,
|
|
87
|
+
tracks: [...this.usedTracks],
|
|
82
88
|
page: this.page,
|
|
83
89
|
});
|
|
84
90
|
}
|
|
@@ -86,53 +92,24 @@ export class LoweringContext {
|
|
|
86
92
|
* 固化谱面行号并进行时间状态固化
|
|
87
93
|
*/
|
|
88
94
|
solidifyColumns(columns) {
|
|
89
|
-
//
|
|
95
|
+
// 同时刻跨轨的 br 已经合成一列,所以取最大;同轨连写仍是多个列,因而累加
|
|
90
96
|
let line = 0;
|
|
91
|
-
let t = 0; // 上一个br的时间点
|
|
92
|
-
let requests = new Map();
|
|
93
|
-
let pending = [];
|
|
94
|
-
const applyBreak = () => {
|
|
95
|
-
if (requests.size === 0)
|
|
96
|
-
return;
|
|
97
|
-
let offset = 0;
|
|
98
|
-
for (const value of requests.values())
|
|
99
|
-
offset = Math.max(offset, value);
|
|
100
|
-
line += offset;
|
|
101
|
-
// 换行列本身归属于换行之后的那一行
|
|
102
|
-
for (const column of pending)
|
|
103
|
-
column.layoutLine = line;
|
|
104
|
-
requests.clear();
|
|
105
|
-
pending.length = 0;
|
|
106
|
-
};
|
|
107
97
|
for (const column of columns) {
|
|
108
|
-
|
|
109
|
-
applyBreak();
|
|
110
|
-
column.layoutLine = line;
|
|
111
|
-
continue;
|
|
112
|
-
}
|
|
113
|
-
if (column.t !== t) {
|
|
114
|
-
applyBreak(); // 不在同一时刻,重启换行
|
|
115
|
-
t = column.t;
|
|
116
|
-
}
|
|
117
|
-
else {
|
|
118
|
-
for (const node of column) {
|
|
119
|
-
if (node.breakBefore <= 0)
|
|
120
|
-
continue;
|
|
121
|
-
if (requests.has(node.track)) {
|
|
122
|
-
applyBreak(); // 同一轨重复出现,先把之前的换行列固化
|
|
123
|
-
break;
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
}
|
|
98
|
+
let offset = 0;
|
|
127
99
|
for (const node of column) {
|
|
128
|
-
if (node.breakBefore >
|
|
129
|
-
|
|
100
|
+
if (node.breakBefore > offset)
|
|
101
|
+
offset = node.breakBefore;
|
|
130
102
|
}
|
|
131
|
-
|
|
103
|
+
line += offset;
|
|
104
|
+
// 换行列本身归属于换行之后的那一行
|
|
105
|
+
column.layoutLine = line;
|
|
132
106
|
}
|
|
133
|
-
applyBreak();
|
|
134
107
|
// 行号全部就位后再固化时间状态,up 等节点才能把行号转发给内部成员
|
|
135
|
-
const state = {
|
|
108
|
+
const state = {
|
|
109
|
+
bpm: DEFAULT_BPM,
|
|
110
|
+
velocity: DEFAULT_VELOCITY,
|
|
111
|
+
keySignature: DEFAULT_TONALITY,
|
|
112
|
+
};
|
|
136
113
|
for (const column of columns) {
|
|
137
114
|
for (const node of column)
|
|
138
115
|
node.onTimeState?.(state);
|
|
@@ -156,7 +133,8 @@ export class LoweringContext {
|
|
|
156
133
|
if (active.owner !== owner)
|
|
157
134
|
throw new Error("Lowering groups must end in reverse order");
|
|
158
135
|
if (active.group.attachment)
|
|
159
|
-
this.
|
|
136
|
+
this.addAttachment(active.group.attachment);
|
|
137
|
+
return active.group;
|
|
160
138
|
}
|
|
161
139
|
/** 先统一生成派生 attachment,再按注册顺序执行最终处理 */
|
|
162
140
|
postprocessResult(result) {
|
|
@@ -168,7 +146,7 @@ export class LoweringContext {
|
|
|
168
146
|
additions.push(attachment);
|
|
169
147
|
}
|
|
170
148
|
for (const attachment of additions)
|
|
171
|
-
this.
|
|
149
|
+
this.addAttachment(attachment);
|
|
172
150
|
// 进行校验或者其他动作
|
|
173
151
|
for (const finalize of this.loweringFinalizers)
|
|
174
152
|
finalize(result);
|
|
@@ -178,27 +156,16 @@ export class LoweringContext {
|
|
|
178
156
|
* 注册不推进时间的排版对象
|
|
179
157
|
* 被 tie、beam、box 等函数使用
|
|
180
158
|
*/
|
|
181
|
-
|
|
159
|
+
addAttachment(attachment) {
|
|
182
160
|
this.attachments.push(attachment);
|
|
183
|
-
|
|
184
|
-
|
|
161
|
+
// 由内向外分组,符合嵌套作用域语义
|
|
162
|
+
for (let i = this.activeLoweringGroups.length - 1; i >= 0; i--) {
|
|
163
|
+
this.activeLoweringGroups[i].group.onAttachment?.(attachment);
|
|
185
164
|
}
|
|
186
165
|
}
|
|
187
|
-
/**
|
|
188
|
-
* 查询 AST 节点在当前 lowering 中产生的所有 temporal 节点
|
|
189
|
-
*
|
|
190
|
-
* 被折叠进别人盒子的成员没有独立的全局位置,统一上溯到宿主,
|
|
191
|
-
* 所以写在成员上的标签等价于写在整个复合节点上。
|
|
192
|
-
*/
|
|
166
|
+
/** 查询 AST 自己产生的 temporal;是否沿 foldedInto 投影由调用者决定 */
|
|
193
167
|
getTemporalNodes(ast) {
|
|
194
|
-
|
|
195
|
-
if (!nodes)
|
|
196
|
-
return [];
|
|
197
|
-
return nodes.map(node => {
|
|
198
|
-
while (node.foldedInto)
|
|
199
|
-
node = node.foldedInto;
|
|
200
|
-
return node;
|
|
201
|
-
});
|
|
168
|
+
return this.astToTemporal.get(ast) ?? [];
|
|
202
169
|
}
|
|
203
170
|
/**
|
|
204
171
|
* 展开内容,但外层分组看不见它们
|
|
@@ -221,21 +188,21 @@ export class LoweringContext {
|
|
|
221
188
|
* 原地补全 hook: loweringEnter / loweringExit 返回的事件
|
|
222
189
|
*/
|
|
223
190
|
initEvent(event, owner, timeOffset, track) {
|
|
224
|
-
event.t
|
|
225
|
-
|
|
191
|
+
event.t.add(timeOffset);
|
|
192
|
+
this.usedTracks.add(track);
|
|
226
193
|
event.track = track;
|
|
227
194
|
event.ast ??= owner;
|
|
228
195
|
event.order = this.cnt++;
|
|
229
|
-
event.
|
|
196
|
+
event.mergeKey ??= event.order; // 默认为独立成列,用 order 保证唯一性
|
|
230
197
|
}
|
|
231
198
|
/**
|
|
232
199
|
* 递归展开一棵子树,得到它的时间列与结束时间
|
|
233
|
-
* @param timeOffset 当前节点的时间偏移量(单位QN)
|
|
200
|
+
* @param timeOffset 当前节点的时间偏移量(单位QN) 由父节点传入,会被原地修改
|
|
234
201
|
* @param track 当前所在的纵向音轨
|
|
235
202
|
*/
|
|
236
203
|
trackedEvents(node, timeOffset, track) {
|
|
237
204
|
const columns = [];
|
|
238
|
-
|
|
205
|
+
this.appendEvents(node.loweringEnter(this, track), node, timeOffset, track, columns);
|
|
239
206
|
// 处理子元素
|
|
240
207
|
const model = node.timeFlowModel();
|
|
241
208
|
if (model) {
|
|
@@ -243,9 +210,7 @@ export class LoweringContext {
|
|
|
243
210
|
case "sequence":
|
|
244
211
|
{
|
|
245
212
|
for (const c of model.children) {
|
|
246
|
-
const
|
|
247
|
-
timeOffset = result.timeOffset;
|
|
248
|
-
for (const column of result.columns)
|
|
213
|
+
for (const column of this.trackedEvents(c, timeOffset, track))
|
|
249
214
|
columns.push(column);
|
|
250
215
|
}
|
|
251
216
|
}
|
|
@@ -256,7 +221,7 @@ export class LoweringContext {
|
|
|
256
221
|
const spec = model.tracks;
|
|
257
222
|
const children = model.children;
|
|
258
223
|
const hostIndex = spec.hostIndex === void 0 ? 0 : spec.hostIndex;
|
|
259
|
-
const group = track.group(spec
|
|
224
|
+
const group = track.group(spec, hostIndex === null ? children.length : children.length - 1);
|
|
260
225
|
const branches = [];
|
|
261
226
|
for (let i = 0; i < children.length; i++) {
|
|
262
227
|
const c = children[i];
|
|
@@ -265,37 +230,50 @@ export class LoweringContext {
|
|
|
265
230
|
? track
|
|
266
231
|
: group.members[hostIndex === null || i < hostIndex ? i : i - 1];
|
|
267
232
|
// 每个分支从头开始
|
|
268
|
-
const
|
|
269
|
-
|
|
233
|
+
const branchTime = timeOffset.clone();
|
|
234
|
+
const branch = this.trackedEvents(c, branchTime, branchTrack);
|
|
235
|
+
if (branch.length === 0) {
|
|
270
236
|
this.diagnostics.push(Diagnostic.warning.ZeroTimeTrack(c.sourceSpan, i + 1));
|
|
271
237
|
}
|
|
272
|
-
branches.push(
|
|
238
|
+
branches.push(branch);
|
|
273
239
|
}
|
|
274
240
|
// 归并 局部归并以限制对齐作用域
|
|
275
241
|
for (const column of LoweringContext.anchorAlign(branches))
|
|
276
242
|
columns.push(column);
|
|
277
243
|
const lastCol = columns[columns.length - 1];
|
|
278
|
-
if (lastCol)
|
|
279
|
-
|
|
244
|
+
if (lastCol) {
|
|
245
|
+
let maxDuration = lastCol[0].T;
|
|
246
|
+
for (let i = 1; i < lastCol.length; i++) {
|
|
247
|
+
if (lastCol[i].T.compare(maxDuration) > 0)
|
|
248
|
+
maxDuration = lastCol[i].T;
|
|
249
|
+
}
|
|
250
|
+
timeOffset.copyFrom(lastCol.t).add(maxDuration);
|
|
251
|
+
}
|
|
280
252
|
}
|
|
281
253
|
break;
|
|
282
254
|
}
|
|
283
255
|
}
|
|
284
|
-
|
|
285
|
-
return
|
|
256
|
+
this.appendEvents(node.loweringExit(this, track, timeOffset), node, timeOffset, track, columns);
|
|
257
|
+
return columns;
|
|
286
258
|
}
|
|
287
|
-
/**
|
|
259
|
+
/**
|
|
260
|
+
* 将 hook 返回的事件规范化、加入索引与分组,再追加到时间列
|
|
261
|
+
* 原地修改 timeOffset
|
|
262
|
+
*/
|
|
288
263
|
appendEvents(events, owner, timeOffset, track, columns) {
|
|
264
|
+
// 由于大部分时候不会有事件,所以延迟创建
|
|
265
|
+
let eventEnd;
|
|
289
266
|
for (const event of events) {
|
|
290
267
|
this.initEvent(event, owner, timeOffset, track);
|
|
291
268
|
this.indexTemporal(event);
|
|
292
|
-
for (
|
|
293
|
-
group.onTemporal?.(event);
|
|
269
|
+
for (let i = this.activeLoweringGroups.length - 1; i >= 0; i--) {
|
|
270
|
+
this.activeLoweringGroups[i].group.onTemporal?.(event);
|
|
294
271
|
}
|
|
295
|
-
|
|
272
|
+
const end = (eventEnd ??= new Fraction()).copyFrom(event.t).add(event.T);
|
|
273
|
+
if (end.compare(timeOffset) > 0)
|
|
274
|
+
timeOffset.copyFrom(end);
|
|
296
275
|
columns.push(new TimeColumn(event));
|
|
297
276
|
}
|
|
298
|
-
return timeOffset;
|
|
299
277
|
}
|
|
300
278
|
/** 记录 AST->TemporalNode 的映射 */
|
|
301
279
|
indexTemporal(node) {
|
|
@@ -327,27 +305,30 @@ export class LoweringContext {
|
|
|
327
305
|
const track0 = tracks[0], track1 = tracks[1];
|
|
328
306
|
const l0 = track0.length, l1 = track1.length;
|
|
329
307
|
let i = 0, j = 0;
|
|
330
|
-
|
|
308
|
+
const idt = new Fraction(), jdt = new Fraction(); // 应用时间偏移一定要紧在 push 前
|
|
331
309
|
let a = track0[0], b = track1[0];
|
|
332
|
-
|
|
310
|
+
const t0 = a.t.clone(), t1 = b.t.clone();
|
|
333
311
|
while (true) {
|
|
334
|
-
if (a.
|
|
312
|
+
if (a.mergeKey === ANCHOR_KEY) {
|
|
335
313
|
// 等待b也遇到anchor
|
|
336
|
-
if (b.
|
|
314
|
+
if (b.mergeKey !== ANCHOR_KEY) {
|
|
337
315
|
result.push(b);
|
|
338
316
|
b = track1[++j];
|
|
339
|
-
while (j < l1 && b.
|
|
340
|
-
if (jdt)
|
|
341
|
-
b.
|
|
317
|
+
while (j < l1 && b.mergeKey !== ANCHOR_KEY) {
|
|
318
|
+
if (!jdt.isZero())
|
|
319
|
+
b.shiftTime(jdt);
|
|
342
320
|
result.push(b);
|
|
343
321
|
b = track1[++j];
|
|
344
322
|
}
|
|
345
|
-
if (b)
|
|
346
|
-
b.
|
|
323
|
+
if (b) {
|
|
324
|
+
b.shiftTime(jdt);
|
|
325
|
+
t1.copyFrom(b.t);
|
|
326
|
+
}
|
|
347
327
|
}
|
|
348
|
-
if (j < l1) { // b
|
|
349
|
-
const maxT =
|
|
350
|
-
idt
|
|
328
|
+
if (j < l1) { // b 也是 anchor
|
|
329
|
+
const maxT = t0.compare(t1) >= 0 ? t0 : t1;
|
|
330
|
+
idt.add(maxT).sub(t0);
|
|
331
|
+
jdt.add(maxT).sub(t1);
|
|
351
332
|
a.push(...b);
|
|
352
333
|
a.t = maxT;
|
|
353
334
|
b = track1[++j];
|
|
@@ -355,90 +336,109 @@ export class LoweringContext {
|
|
|
355
336
|
result.push(a);
|
|
356
337
|
a = track0[++i];
|
|
357
338
|
// 准备下一个
|
|
358
|
-
if (a)
|
|
359
|
-
a.
|
|
360
|
-
|
|
361
|
-
|
|
339
|
+
if (a) {
|
|
340
|
+
a.shiftTime(idt);
|
|
341
|
+
t0.copyFrom(a.t);
|
|
342
|
+
}
|
|
343
|
+
if (b) {
|
|
344
|
+
b.shiftTime(jdt);
|
|
345
|
+
t1.copyFrom(b.t);
|
|
346
|
+
}
|
|
362
347
|
if (!a || !b)
|
|
363
348
|
break;
|
|
364
349
|
}
|
|
365
|
-
if (b.
|
|
366
|
-
if (a.
|
|
350
|
+
if (b.mergeKey === ANCHOR_KEY) {
|
|
351
|
+
if (a.mergeKey !== ANCHOR_KEY) {
|
|
367
352
|
result.push(a);
|
|
368
353
|
a = track0[++i];
|
|
369
|
-
while (i < l0 && a.
|
|
370
|
-
if (idt)
|
|
371
|
-
a.
|
|
354
|
+
while (i < l0 && a.mergeKey !== ANCHOR_KEY) {
|
|
355
|
+
if (!idt.isZero())
|
|
356
|
+
a.shiftTime(idt);
|
|
372
357
|
result.push(a);
|
|
373
358
|
a = track0[++i];
|
|
374
359
|
}
|
|
375
|
-
if (a)
|
|
376
|
-
a.
|
|
360
|
+
if (a) {
|
|
361
|
+
a.shiftTime(idt);
|
|
362
|
+
t0.copyFrom(a.t);
|
|
363
|
+
}
|
|
377
364
|
}
|
|
378
365
|
if (i < l0) {
|
|
379
|
-
const maxT =
|
|
380
|
-
idt
|
|
366
|
+
const maxT = t0.compare(t1) >= 0 ? t0 : t1;
|
|
367
|
+
idt.add(maxT).sub(t0);
|
|
368
|
+
jdt.add(maxT).sub(t1);
|
|
381
369
|
b.push(...a);
|
|
382
370
|
b.t = maxT;
|
|
383
371
|
a = track0[++i];
|
|
384
372
|
}
|
|
385
373
|
result.push(b);
|
|
386
374
|
b = track1[++j];
|
|
387
|
-
if (a)
|
|
388
|
-
a.
|
|
389
|
-
|
|
390
|
-
|
|
375
|
+
if (a) {
|
|
376
|
+
a.shiftTime(idt);
|
|
377
|
+
t0.copyFrom(a.t);
|
|
378
|
+
}
|
|
379
|
+
if (b) {
|
|
380
|
+
b.shiftTime(jdt);
|
|
381
|
+
t1.copyFrom(b.t);
|
|
382
|
+
}
|
|
391
383
|
if (!a || !b)
|
|
392
384
|
break;
|
|
393
385
|
}
|
|
394
|
-
if (
|
|
395
|
-
if (a.
|
|
386
|
+
if (t0.equals(t1)) {
|
|
387
|
+
if (a.mergeKey === b.mergeKey) {
|
|
388
|
+
a.push(...b);
|
|
396
389
|
result.push(a);
|
|
397
390
|
a = track0[++i];
|
|
391
|
+
b = track1[++j];
|
|
398
392
|
if (a) {
|
|
399
|
-
a.
|
|
393
|
+
a.shiftTime(idt);
|
|
394
|
+
t0.copyFrom(a.t);
|
|
400
395
|
}
|
|
401
|
-
|
|
396
|
+
if (b) {
|
|
397
|
+
b.shiftTime(jdt);
|
|
398
|
+
t1.copyFrom(b.t);
|
|
399
|
+
}
|
|
400
|
+
if (!a || !b)
|
|
402
401
|
break;
|
|
403
402
|
}
|
|
404
|
-
else if (
|
|
405
|
-
result.push(
|
|
406
|
-
|
|
407
|
-
if (
|
|
408
|
-
|
|
403
|
+
else if (a.mergeKey < b.mergeKey) {
|
|
404
|
+
result.push(a);
|
|
405
|
+
a = track0[++i];
|
|
406
|
+
if (a) {
|
|
407
|
+
a.shiftTime(idt);
|
|
408
|
+
t0.copyFrom(a.t);
|
|
409
409
|
}
|
|
410
410
|
else
|
|
411
411
|
break;
|
|
412
412
|
}
|
|
413
413
|
else {
|
|
414
|
-
|
|
415
|
-
result.push(a);
|
|
416
|
-
a = track0[++i];
|
|
414
|
+
result.push(b);
|
|
417
415
|
b = track1[++j];
|
|
418
|
-
if (
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
416
|
+
if (b) {
|
|
417
|
+
b.shiftTime(jdt);
|
|
418
|
+
t1.copyFrom(b.t);
|
|
419
|
+
}
|
|
420
|
+
else
|
|
423
421
|
break;
|
|
424
422
|
}
|
|
425
423
|
continue;
|
|
426
424
|
}
|
|
427
|
-
if (t0 <
|
|
425
|
+
if (t0.compare(t1) < 0) {
|
|
428
426
|
result.push(a);
|
|
429
427
|
a = track0[++i];
|
|
430
428
|
if (a) {
|
|
431
|
-
a.
|
|
429
|
+
a.shiftTime(idt);
|
|
430
|
+
t0.copyFrom(a.t);
|
|
432
431
|
continue;
|
|
433
432
|
}
|
|
434
433
|
else
|
|
435
434
|
break;
|
|
436
435
|
}
|
|
437
|
-
if (t1 <
|
|
436
|
+
if (t1.compare(t0) < 0) {
|
|
438
437
|
result.push(b);
|
|
439
438
|
b = track1[++j];
|
|
440
439
|
if (b) {
|
|
441
|
-
b.
|
|
440
|
+
b.shiftTime(jdt);
|
|
441
|
+
t1.copyFrom(b.t);
|
|
442
442
|
continue;
|
|
443
443
|
}
|
|
444
444
|
else
|
|
@@ -449,23 +449,23 @@ export class LoweringContext {
|
|
|
449
449
|
result.push(a);
|
|
450
450
|
while (++i < l0) {
|
|
451
451
|
a = track0[i];
|
|
452
|
-
if (idt)
|
|
453
|
-
a.
|
|
452
|
+
if (!idt.isZero())
|
|
453
|
+
a.shiftTime(idt);
|
|
454
454
|
result.push(a);
|
|
455
455
|
}
|
|
456
456
|
if (b)
|
|
457
457
|
result.push(b);
|
|
458
458
|
while (++j < l1) {
|
|
459
459
|
b = track1[j];
|
|
460
|
-
if (jdt)
|
|
461
|
-
b.
|
|
460
|
+
if (!jdt.isZero())
|
|
461
|
+
b.shiftTime(jdt);
|
|
462
462
|
result.push(b);
|
|
463
463
|
}
|
|
464
464
|
return result;
|
|
465
465
|
}
|
|
466
466
|
// 归并多个轨道
|
|
467
467
|
const p = new Uint16Array(l); // 当前指向第几个
|
|
468
|
-
const dt = new
|
|
468
|
+
const dt = Array.from({ length: l }, () => new Fraction()); // 每个轨道的偏移量
|
|
469
469
|
const anchorBuffer = [];
|
|
470
470
|
const heap = new HeapSort(l); // 保证每个轨在堆里最多一个
|
|
471
471
|
for (let i = 0; i < l; i++)
|
|
@@ -473,8 +473,8 @@ export class LoweringContext {
|
|
|
473
473
|
const fetchNext = (i) => {
|
|
474
474
|
const e = tracks[i][++p[i]];
|
|
475
475
|
if (e) {
|
|
476
|
-
if (dt[i])
|
|
477
|
-
e.
|
|
476
|
+
if (!dt[i].isZero())
|
|
477
|
+
e.shiftTime(dt[i]);
|
|
478
478
|
heap.push({ e, i });
|
|
479
479
|
}
|
|
480
480
|
};
|
|
@@ -486,14 +486,16 @@ export class LoweringContext {
|
|
|
486
486
|
const first = anchorBuffer[0];
|
|
487
487
|
const container = first.e;
|
|
488
488
|
let maxT = container.t;
|
|
489
|
-
for (let j = 1; j < anchorBuffer.length; j++)
|
|
490
|
-
|
|
491
|
-
|
|
489
|
+
for (let j = 1; j < anchorBuffer.length; j++) {
|
|
490
|
+
if (anchorBuffer[j].e.t.compare(maxT) > 0)
|
|
491
|
+
maxT = anchorBuffer[j].e.t;
|
|
492
|
+
}
|
|
493
|
+
dt[first.i].add(maxT).sub(container.t);
|
|
492
494
|
fetchNext(first.i);
|
|
493
495
|
for (let j = 1; j < anchorBuffer.length; j++) {
|
|
494
496
|
const item = anchorBuffer[j];
|
|
495
497
|
container.push(...item.e);
|
|
496
|
-
dt[item.i]
|
|
498
|
+
dt[item.i].add(maxT).sub(item.e.t);
|
|
497
499
|
fetchNext(item.i);
|
|
498
500
|
}
|
|
499
501
|
anchorBuffer.length = 0;
|
|
@@ -502,58 +504,47 @@ export class LoweringContext {
|
|
|
502
504
|
result.push(container);
|
|
503
505
|
continue;
|
|
504
506
|
}
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
507
|
+
if (front.e.mergeKey === ANCHOR_KEY) {
|
|
508
|
+
// 进入缓冲区 不补充本track的下一个元素
|
|
509
|
+
anchorBuffer.push(front);
|
|
510
|
+
continue;
|
|
511
|
+
}
|
|
512
|
+
// 找同时刻的同组
|
|
513
|
+
const consumed = [front.i];
|
|
514
|
+
while (true) {
|
|
515
|
+
const f = heap.front();
|
|
516
|
+
// 堆序保证同组相邻,遇到别的组即说明本组已取完
|
|
517
|
+
if (!f || !f.e.t.equals(front.e.t) || f.e.mergeKey !== front.e.mergeKey)
|
|
513
518
|
break;
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
const consumed = [front.i];
|
|
518
|
-
while (true) { // heap 的排序保证此时同时刻没有single和anchor
|
|
519
|
-
const f = heap.front();
|
|
520
|
-
if (!f || f.e.t > minT)
|
|
521
|
-
break; // 所有轨道都处理完了
|
|
522
|
-
heap.pop();
|
|
523
|
-
front.e.push(...f.e);
|
|
524
|
-
consumed.push(f.i);
|
|
525
|
-
}
|
|
526
|
-
result.push(front.e);
|
|
527
|
-
// 最后再加新的,使得只合并每个轨道最前面的,防止把后面同时刻的都合并了
|
|
528
|
-
// 这也是引入 SINGLE 的原因
|
|
529
|
-
for (const i of consumed)
|
|
530
|
-
fetchNext(i);
|
|
519
|
+
heap.pop();
|
|
520
|
+
front.e.push(...f.e);
|
|
521
|
+
consumed.push(f.i);
|
|
531
522
|
}
|
|
523
|
+
result.push(front.e);
|
|
524
|
+
// 最后再加新的,使得只合并每个轨道最前面的,防止把后面同时刻的都合并了
|
|
525
|
+
for (const i of consumed)
|
|
526
|
+
fetchNext(i);
|
|
532
527
|
}
|
|
533
528
|
return result;
|
|
534
529
|
}
|
|
535
530
|
}
|
|
536
531
|
class TimeColumn extends Array {
|
|
537
|
-
|
|
532
|
+
mergeKey;
|
|
538
533
|
constructor(n) {
|
|
539
534
|
super(1);
|
|
540
|
-
this.
|
|
535
|
+
this.mergeKey = n.mergeKey;
|
|
541
536
|
this[0] = n;
|
|
542
537
|
}
|
|
543
538
|
get t() {
|
|
544
539
|
return this[0].t;
|
|
545
540
|
}
|
|
546
541
|
set t(value) {
|
|
547
|
-
for (const
|
|
548
|
-
|
|
542
|
+
for (const node of this)
|
|
543
|
+
node.t.copyFrom(value);
|
|
549
544
|
}
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
if (node.breakBefore > 0)
|
|
554
|
-
return true;
|
|
555
|
-
}
|
|
556
|
-
return false;
|
|
545
|
+
shiftTime(offset) {
|
|
546
|
+
for (const node of this)
|
|
547
|
+
node.t.add(offset);
|
|
557
548
|
}
|
|
558
549
|
/** 将当前列所有事件的行号固化为 lowering 后的实际行号 */
|
|
559
550
|
set layoutLine(value) {
|
|
@@ -562,11 +553,12 @@ class TimeColumn extends Array {
|
|
|
562
553
|
}
|
|
563
554
|
}
|
|
564
555
|
function heapItemCmp(a, b) {
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
556
|
+
const timeOrder = a.e.t.compare(b.e.t);
|
|
557
|
+
if (timeOrder !== 0)
|
|
558
|
+
return timeOrder;
|
|
559
|
+
// 有无穷所以不能直接减
|
|
560
|
+
if (a.e.mergeKey !== b.e.mergeKey)
|
|
561
|
+
return a.e.mergeKey < b.e.mergeKey ? -1 : 1;
|
|
570
562
|
return a.i - b.i;
|
|
571
563
|
}
|
|
572
564
|
// 最小堆
|