jpfun 0.1.0 → 0.2.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.
@@ -29,7 +29,6 @@ class TempoTemporalNode extends TemporalNodeBase {
29
29
  constructor(ast) {
30
30
  super();
31
31
  this.ast = ast;
32
- this.type = 1 /* ColType.SINGLE */;
33
32
  }
34
33
  onTimeState(state) {
35
34
  state.bpm = this.ast.bpm;
@@ -1,5 +1,5 @@
1
1
  import { ASTFunctionNode } from "../ASTtypes.js";
2
- import { TemporalNodeBase } from "../../lowering/types.js";
2
+ import { DEFAULT_KEY, TemporalNodeBase } from "../../lowering/types.js";
3
3
  class TextFunction extends ASTFunctionNode {
4
4
  static def = {
5
5
  name: ["text"],
@@ -39,7 +39,7 @@ class TextTemporalNode extends TemporalNodeBase {
39
39
  super();
40
40
  this.ast = ast;
41
41
  this.T = 0;
42
- this.type = 2 /* ColType.DEFAULT */;
42
+ this.mergeKey = DEFAULT_KEY;
43
43
  this.initLayoutBox();
44
44
  }
45
45
  prepareLayout(context) {
@@ -9,7 +9,7 @@ class TieFunction extends ASTFunctionNode {
9
9
  static def = {
10
10
  name: ["tie"],
11
11
  description: "连音线",
12
- example: `@tie(label1, label2, ..., height=0.5em)
12
+ example: `@tie(label1, label2, ..., height=0.5em)
13
13
  将端点依次连接;同行用一条弧线,跨行拆成分段连接;若不传端点则找最近的`,
14
14
  allowExtraArgs: true,
15
15
  extraArgType: "label",
@@ -1,6 +1,6 @@
1
1
  import { ASTNodeBase, ASTFunctionNode, ASTTextNode, ASTLabelNode, ASTBraceNode } from "../ASTtypes.js";
2
2
  import { ErrorDiagnostic } from "../../diagnostic.js";
3
- import { isVisualTemporalNode, TemporalNodeBase, } from "../../lowering/types.js";
3
+ import { ANCHOR_KEY, DEFAULT_KEY, isVisualTemporalNode, TemporalNodeBase, } from "../../lowering/types.js";
4
4
  import { prepareLayoutHost } from "../../layout/engine.js";
5
5
  /**
6
6
  * 函数 up 的设计经历:
@@ -10,12 +10,12 @@ import { prepareLayoutHost } from "../../layout/engine.js";
10
10
  * 而且有三个问题:
11
11
  * 1. 排版上不好对齐。只有一个元素可以直接用anchor进行对齐,而多个,甚至是嵌套,对齐锚点应该选择哪个呢?
12
12
  * 2. 时间上不好对齐。如果某个元素里面包含了多个事件,在 onTimeState 时应该如何处理?
13
- * 3. ColType 类型不好确定。如果每个元素都只有一个,那么 ColType 可以直接取最小值,但是如果某个元素里面有多个事件,那么 ColType 应该取哪个呢?
13
+ * 3. 合并组不好确定。如果某个元素里面有多个事件,该取哪个的 mergeKey 呢?
14
14
  *
15
- * ColType 属于一个时间位置,不属于任意大的 AST 子树。所以每个元素必须有以下接口:
15
+ * mergeKey 属于一个时间位置,不属于任意大的 AST 子树。所以每个元素必须有以下接口:
16
16
  * - box
17
17
  * - t/T
18
- * - colType
18
+ * - mergeKey
19
19
  * - onTimeState
20
20
  *
21
21
  * 此时 up 的每个元素就只允许为一个 VisualTemporalNode,不再需要处理子内容了
@@ -24,34 +24,15 @@ class UpFunction extends ASTFunctionNode {
24
24
  static def = {
25
25
  name: ["up"],
26
26
  description: "把同一时间位置的可见对象向上堆叠",
27
- example: `@up(content1, content2, ...)
28
- 语法糖: ^
29
- {content1} ^ {content2} ^ ...
30
- 表示content1和content2在时间上完全重叠,通常用于和声等需要对齐的场景。可以有任意多个参数,至少需要两个参数。
27
+ example: `@up(content1, content2, ...)
28
+ 语法糖: ^
29
+ {content1} ^ {content2} ^ ...
30
+ 表示content1和content2在时间上完全重叠,通常用于和声等需要对齐的场景。可以有任意多个参数,至少需要两个参数。
31
31
  `,
32
32
  allowExtraArgs: true,
33
33
  extraArgType: "content",
34
34
  args: [],
35
35
  };
36
- /**
37
- * 语义确定后,把和弦的修饰交还给最下面的成员
38
- *
39
- * lowering 期间修饰必须挂在和弦上,自动连梁之类的 augmenter 才看得到它的节奏;
40
- * 但渲染上「最下面的成员代表整个和弦」,由它按普通音符的规则承载修饰,
41
- * 减时线才会落在数字与下八度点之间,而不是压在整个和弦盒的下面。
42
- * 交还后和弦自身不再有 addon,排版阶段不必再动它。
43
- */
44
- static loweringFinalize = (result) => {
45
- for (const column of result.columns) {
46
- for (const node of column) {
47
- // addon 非空就意味着当初从第一个成员提升过,成员必定存在
48
- if (!(node instanceof UpTemporal) || !node.addon)
49
- continue;
50
- node.members[0].addon = node.addon;
51
- node.addon = void 0;
52
- }
53
- }
54
- };
55
36
  /** 和弦自己会产生 UpTemporal,标签直接指向整个和弦(内部没有 note 时也能标注) */
56
37
  labelable() { return this; }
57
38
  static deSugarAtom(source, start, _end) {
@@ -190,7 +171,7 @@ class UpTemporal extends TemporalNodeBase {
190
171
  this.members = members;
191
172
  this.T = members[0]?.T ?? 0;
192
173
  this.t = 0;
193
- this.type = 2 /* ColType.DEFAULT */;
174
+ this.mergeKey = DEFAULT_KEY;
194
175
  this.initLayoutBox();
195
176
  // 第一个成员决定和弦的时值,它的修饰语义也随之成为整个和弦的修饰,
196
177
  // 自动连梁等语义处理才能看到这个和弦的节奏;
@@ -199,8 +180,10 @@ class UpTemporal extends TemporalNodeBase {
199
180
  if (leadAddon)
200
181
  this.addon = { ...leadAddon };
201
182
  for (const member of members) {
202
- if (member.type < this.type)
203
- this.type = member.type;
183
+ // 只有锚点需要传上来:`| ^ @text(A)` 得保持小节线语义;
184
+ // 成员不进全局 columns,它们自己的合并组对外没有意义
185
+ if (member.mergeKey === ANCHOR_KEY)
186
+ this.mergeKey = ANCHOR_KEY;
204
187
  // 堆叠在一起的成员共享同一个时值,由第一个成员决定;
205
188
  // 本来就没有时长的成员(标注、小节线等)保持 0,不会被拉长
206
189
  if (member.T !== 0)
@@ -230,6 +213,12 @@ class UpTemporal extends TemporalNodeBase {
230
213
  */
231
214
  prepareLayout(context) {
232
215
  this.offsets.length = 0;
216
+ // lowering 期间修饰挂在和弦上(augmenter 要看到整体节奏),渲染时交给最下面的成员:
217
+ // 减时线要落在它的数字与下八度点之间,而不是压在整个和弦盒下面
218
+ if (this.addon && this.members[0]) {
219
+ this.members[0].addon = this.addon;
220
+ this.addon = void 0;
221
+ }
233
222
  for (const member of this.members)
234
223
  prepareLayoutHost(member, context);
235
224
  const first = this.members[0];
@@ -14,9 +14,9 @@ const WHITEPACE_RE = /\s/;
14
14
  * 本行没有实质高度的成员(空声部,或内容都在上一行)仍然占一个默认高度的槽位,
15
15
  * 声部数量因而保持稳定,居中结果也不会因为某一声部没写东西而跳变。
16
16
  */
17
- function makeVoicesArrange(emptySlotHeight) {
17
+ function makeVoicesMeasure(emptySlotHeight) {
18
18
  const half = emptySlotHeight / 2;
19
- return (_host, members, gap) => {
19
+ return (members, gap) => {
20
20
  const extents = members.map(member => member && member.bottom - member.top > 1e-6 ? member : { top: -half, bottom: half });
21
21
  const offsets = [];
22
22
  let y = 0;
@@ -33,20 +33,20 @@ class VoiceFunction extends ASTFunctionNode {
33
33
  static def = {
34
34
  name: ["voice", "v"],
35
35
  description: "声部",
36
- example: `@voice({}, name, 歌词1名=歌词1, 歌词2名=歌词2, ...)
37
- 第一个参数为音符内容,第二个参数为写在最左侧的声部名称(可不填);
38
- 命名参数为歌词左侧的名称和内容,可以任意多个。也可以是位置参数,表示不需要名称。
39
- 例:
40
- @voice({C1 D1 E1}, 钢琴, 男=ha ha ha, 女=la la la)
41
- @voice({C1 D1 E1}, , ha ha ha, 女="la la la") 表示音符和第一个歌词都没有名称
42
- 歌词可以被引号包裹,如果包含逗号等歧义字符一定需要引号
43
- 语法糖:
44
- N(name): C1 D1 E1
45
- L(name): ha ha ha...
46
- L: ...
47
- 前面可以有任意空白,但最后换行表示结束。歌词此时允许有逗号而不加引号
48
- 如果一定要换行,最后加上'\\'
49
- 英文歌词用空格和连字符'-'分词,用'@'占位
36
+ example: `@voice({}, name, 歌词1名=歌词1, 歌词2名=歌词2, ...)
37
+ 第一个参数为音符内容,第二个参数为写在最左侧的声部名称(可不填);
38
+ 命名参数为歌词左侧的名称和内容,可以任意多个。也可以是位置参数,表示不需要名称。
39
+ 例:
40
+ @voice({C1 D1 E1}, 钢琴, 男=ha ha ha, 女=la la la)
41
+ @voice({C1 D1 E1}, , ha ha ha, 女="la la la") 表示音符和第一个歌词都没有名称
42
+ 歌词可以被引号包裹,如果包含逗号等歧义字符一定需要引号
43
+ 语法糖:
44
+ N(name): C1 D1 E1
45
+ L(name): ha ha ha...
46
+ L: ...
47
+ 前面可以有任意空白,但最后换行表示结束。歌词此时允许有逗号而不加引号
48
+ 如果一定要换行,最后加上'\\'
49
+ 英文歌词用空格和连字符'-'分词,用'@'占位
50
50
  `,
51
51
  allowExtraArgs: true,
52
52
  extraArgType: "string",
@@ -194,16 +194,15 @@ L: ...
194
194
  if (newCtx.nodes.length === 0) {
195
195
  throw Diagnostic.error.EmptyContent("voice", "content", n.span);
196
196
  }
197
+ const span = { start: n.span.start, end: newCtx.nodes.at(-1).sourceSpan.end };
197
198
  const argMap = new Map();
198
199
  if (newCtx.nodes.length === 1 && newCtx.nodes[0] instanceof ASTBraceNode)
199
200
  argMap.set(0, newCtx.nodes[0]);
201
+ // 复制一份:voice 的 span 之后会被歌词撑大,内容 brace 不该跟着长
200
202
  else
201
- argMap.set(0, new ASTBraceNode(n.span, newCtx.nodes));
203
+ argMap.set(0, new ASTBraceNode({ ...span }, newCtx.nodes));
202
204
  argMap.set("name", n.data.name);
203
- const newVoice = new VoiceFunction({
204
- start: n.span.start,
205
- end: breakAt < nodes.length ? nodes[breakAt] : ctx.source.length
206
- }, argMap, ctx, null);
205
+ const newVoice = new VoiceFunction(span, argMap, ctx, null);
207
206
  // 如果前面紧挨着 VoicesFunction | VoiceFunction 则直接加入
208
207
  let voicesNode = null;
209
208
  let voicesNodeAt = ctx.nodes.length - 1;
@@ -318,8 +317,11 @@ L: ...
318
317
  }
319
318
  this.lyrics.push({ name, tokens });
320
319
  if (span) {
321
- this.sourceSpan.start = Math.min(span.start, this.sourceSpan.start);
322
- this.sourceSpan.end = Math.max(span.end, this.sourceSpan.end);
320
+ // 歌词是成节点之后才补上来的,祖先的 span 得一起长,否则按源码位置查节点会漏掉歌词那几行
321
+ for (let node = this; node; node = node.parent) {
322
+ node.sourceSpan.start = Math.min(span.start, node.sourceSpan.start);
323
+ node.sourceSpan.end = Math.max(span.end, node.sourceSpan.end);
324
+ }
323
325
  }
324
326
  }
325
327
  static parseLyric(value) {
@@ -374,16 +376,16 @@ class VoicesFunction extends ASTFunctionNode {
374
376
  static def = {
375
377
  name: ["voices", "vs"],
376
378
  description: "多个声部",
377
- example: `@voices(
378
- @voice({C1 D1 E1}, 钢琴, 男=ha ha ha),
379
- @voice({C2 D2 E2}, , "la la la")
380
- )
381
- 语法糖:当多个 voice 用 voice 的语法糖写在一起时,会自动创建一个 voices 组件包裹它们。
382
- 例:
383
- N(钢琴): C1 D1 E1
384
- L(男): ha ha ha
385
- N: C2 D2 E2
386
- L: la la la
379
+ example: `@voices(
380
+ @voice({C1 D1 E1}, 钢琴, 男=ha ha ha),
381
+ @voice({C2 D2 E2}, , "la la la")
382
+ )
383
+ 语法糖:当多个 voice 用 voice 的语法糖写在一起时,会自动创建一个 voices 组件包裹它们。
384
+ 例:
385
+ N(钢琴): C1 D1 E1
386
+ L(男): ha ha ha
387
+ N: C2 D2 E2
388
+ L: la la la
387
389
  `,
388
390
  allowExtraArgs: true,
389
391
  extraArgType: "content",
@@ -395,7 +397,7 @@ L: la la la
395
397
  /** 声部名右侧为大括号预留的横向空间 */
396
398
  braceSpace;
397
399
  /** 闭包捕获 parse 期冻结的字号,用来决定空声部槽位的默认高度(1em) */
398
- arrange;
400
+ measure;
399
401
  get children() { return this.voices; }
400
402
  timeFlowModel() {
401
403
  return {
@@ -404,7 +406,7 @@ L: la la la
404
406
  tracks: {
405
407
  laneKey: `voices/${this.voices.length}`,
406
408
  hostIndex: null, // 宿主不是成员:第一个 voice 也必须拥有独立轨道
407
- arrange: this.arrange,
409
+ measure: this.measure,
408
410
  },
409
411
  };
410
412
  }
@@ -429,7 +431,7 @@ L: la la la
429
431
  this.voices = [];
430
432
  this.size = ctx.fontSize;
431
433
  this.braceSpace = ctx.fontSize;
432
- this.arrange = makeVoicesArrange(ctx.fontSize);
434
+ this.measure = makeVoicesMeasure(ctx.fontSize);
433
435
  for (const [, value] of args) {
434
436
  if (value instanceof ASTNodeBase) {
435
437
  if (value instanceof VoiceFunction)
@@ -489,10 +491,7 @@ class VoiceNameTemporal extends TemporalNodeBase {
489
491
  this.ast = ast;
490
492
  this.braceSpace = braceSpace;
491
493
  this.T = 0;
492
- // 同一个 voices 块里每个成员都会产生本事件,列结构对称,
493
- // 因此可以安全地归并成一列并右对齐;
494
- // 独立的 @voice 与相邻分支不对称,必须单独成列,否则会把同时刻的音符挤到下一列
495
- this.type = ast.parent instanceof VoicesFunction ? 2 /* ColType.DEFAULT */ : 1 /* ColType.SINGLE */;
494
+ this.mergeKey = -2;
496
495
  // 既没有名称、也不需要括号空间和标签列时只保留不可见的占位事件:
497
496
  // 它不进入可见对象,也不让空声部失去默认槽位高度
498
497
  if (ast.name || braceSpace > 0 || ast.lyrics.some(lyric => lyric.name))
@@ -354,8 +354,8 @@ export function paintLayout(result, painter) {
354
354
  /**
355
355
  * 沿 Track 树自内向外求出一条谱面行里所有音轨的纵向轴
356
356
  *
357
- * 引擎只做三件通用的事:递归求出每个成员的子树高度、调用该分组自己声明的 arrange、
358
- * 把返回的偏移并回宿主的占用范围。上方叠放还是局部居中完全由 arrange 决定,
357
+ * 引擎只做三件通用的事:递归求出每个成员的子树高度、调用该分组自己声明的 measure、
358
+ * 再用完整宿主占用调可选的 place 定位整组。上方叠放还是局部居中完全由函数决定,
359
359
  * 因此这里不需要认识 stack、voices 或任何将来新增的排版函数。
360
360
  */
361
361
  function solveVerticalAxes(line, rootTrack, gap) {
@@ -373,20 +373,48 @@ function solveVerticalAxes(line, rootTrack, gap) {
373
373
  else
374
374
  extent = { top: attachmentExtent.top, bottom: attachmentExtent.bottom };
375
375
  }
376
+ const measurements = [];
376
377
  for (const group of track.groups) {
377
378
  const memberExtents = group.members.map(solveTrack);
378
379
  // 整组在本行没有任何内容时不占位,避免共用音轨的分组在空行上浪费高度
379
380
  if (memberExtents.every(member => member === null))
380
381
  continue;
381
- const placements = group.arrange(extent ?? { top: 0, bottom: 0 }, memberExtents, gap);
382
+ const placements = group.measure(memberExtents, gap);
383
+ let groupExtent = null;
384
+ for (const placement of placements) {
385
+ if (!placement)
386
+ continue;
387
+ const top = placement.offset + placement.extent.top;
388
+ const bottom = placement.offset + placement.extent.bottom;
389
+ if (groupExtent)
390
+ includeExtent(groupExtent, top, bottom);
391
+ else
392
+ groupExtent = { top, bottom };
393
+ }
394
+ if (groupExtent)
395
+ measurements.push({ group, placements, extent: groupExtent });
396
+ }
397
+ const applyPlacement = (measurement, groupOffset) => {
382
398
  extent ??= { top: 0, bottom: 0 };
383
- for (let i = 0; i < group.members.length; i++) {
384
- const placement = placements[i];
399
+ for (let i = 0; i < measurement.group.members.length; i++) {
400
+ const placement = measurement.placements[i];
385
401
  if (!placement)
386
402
  continue;
387
- offsets.set(group.members[i], placement.offset);
388
- includeExtent(extent, placement.offset + placement.extent.top, placement.offset + placement.extent.bottom);
403
+ offsets.set(measurement.group.members[i], groupOffset + placement.offset);
389
404
  }
405
+ includeExtent(extent, groupOffset + measurement.extent.top, groupOffset + measurement.extent.bottom);
406
+ };
407
+ // 先完成不依赖宿主的局部布局,依赖宿主的分组再贴到完整占用之外
408
+ for (const measurement of measurements) {
409
+ if (!measurement.group.place)
410
+ applyPlacement(measurement, 0);
411
+ }
412
+ for (const measurement of measurements) {
413
+ const place = measurement.group.place;
414
+ if (!place)
415
+ continue;
416
+ const host = extent ?? { top: 0, bottom: 0 };
417
+ applyPlacement(measurement, place(host, measurement.extent, gap));
390
418
  }
391
419
  return extent;
392
420
  };
@@ -114,12 +114,10 @@ export declare class LoweringContext {
114
114
  static anchorAlign(tracks: TimeColumn[][]): TimeColumn[];
115
115
  }
116
116
  declare class TimeColumn extends Array<TemporalNodeBase> {
117
- type: TemporalNodeBase["type"];
117
+ readonly mergeKey: number;
118
118
  constructor(n: TemporalNodeBase);
119
119
  get t(): number;
120
120
  set t(value: number);
121
- /** 当前列是否携带换行请求 */
122
- get breakRequested(): boolean;
123
121
  /** 将当前列所有事件的行号固化为 lowering 后的实际行号 */
124
122
  set layoutLine(value: number);
125
123
  }
@@ -1,4 +1,5 @@
1
1
  import { Diagnostic } from "../diagnostic.js";
2
+ import { ANCHOR_KEY, } from "./types.js";
2
3
  import { Track } from "./track.js";
3
4
  /**
4
5
  * 把 AST 展开并固化为时间列、attachment 和页面配置
@@ -86,51 +87,18 @@ export class LoweringContext {
86
87
  * 固化谱面行号并进行时间状态固化
87
88
  */
88
89
  solidifyColumns(columns) {
89
- // 同一轨的请求累加(`br br` 两行),不同轨的请求取最大(两轨同时 br 只换一行)
90
+ // 同时刻跨轨的 br 已经合成一列,所以取最大;同轨连写仍是多个列,因而累加
90
91
  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
92
  for (const column of columns) {
108
- if (!column.breakRequested) {
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
- }
93
+ let offset = 0;
127
94
  for (const node of column) {
128
- if (node.breakBefore > 0)
129
- requests.set(node.track, node.breakBefore);
95
+ if (node.breakBefore > offset)
96
+ offset = node.breakBefore;
130
97
  }
131
- pending.push(column);
98
+ line += offset;
99
+ // 换行列本身归属于换行之后的那一行
100
+ column.layoutLine = line;
132
101
  }
133
- applyBreak();
134
102
  // 行号全部就位后再固化时间状态,up 等节点才能把行号转发给内部成员
135
103
  const state = {};
136
104
  for (const column of columns) {
@@ -226,7 +194,7 @@ export class LoweringContext {
226
194
  event.track = track;
227
195
  event.ast ??= owner;
228
196
  event.order = this.cnt++;
229
- event.type ??= event.T === 0 ? 1 /* ColType.SINGLE */ : 2 /* ColType.DEFAULT */;
197
+ event.mergeKey ??= event.order; // 默认为独立成列,用 order 保证唯一性
230
198
  }
231
199
  /**
232
200
  * 递归展开一棵子树,得到它的时间列与结束时间
@@ -256,7 +224,7 @@ export class LoweringContext {
256
224
  const spec = model.tracks;
257
225
  const children = model.children;
258
226
  const hostIndex = spec.hostIndex === void 0 ? 0 : spec.hostIndex;
259
- const group = track.group(spec.laneKey, hostIndex === null ? children.length : children.length - 1, spec.arrange);
227
+ const group = track.group(spec, hostIndex === null ? children.length : children.length - 1);
260
228
  const branches = [];
261
229
  for (let i = 0; i < children.length; i++) {
262
230
  const c = children[i];
@@ -331,12 +299,12 @@ export class LoweringContext {
331
299
  let a = track0[0], b = track1[0];
332
300
  let t0 = a.t, t1 = b.t;
333
301
  while (true) {
334
- if (a.type === 0 /* ColType.ANCHOR */) {
302
+ if (a.mergeKey === ANCHOR_KEY) {
335
303
  // 等待b也遇到anchor
336
- if (b.type !== 0 /* ColType.ANCHOR */) {
304
+ if (b.mergeKey !== ANCHOR_KEY) {
337
305
  result.push(b);
338
306
  b = track1[++j];
339
- while (j < l1 && b.type !== 0 /* ColType.ANCHOR */) {
307
+ while (j < l1 && b.mergeKey !== ANCHOR_KEY) {
340
308
  if (jdt)
341
309
  b.t += jdt; // setter 比较重,能避免尽量避免
342
310
  result.push(b);
@@ -345,7 +313,7 @@ export class LoweringContext {
345
313
  if (b)
346
314
  b.t = t1 = b.t + jdt;
347
315
  }
348
- if (j < l1) { // b.type === "anchor"
316
+ if (j < l1) { // b 也是 anchor
349
317
  const maxT = Math.max(t0, t1);
350
318
  idt += maxT - t0, jdt += maxT - t1;
351
319
  a.push(...b);
@@ -362,11 +330,11 @@ export class LoweringContext {
362
330
  if (!a || !b)
363
331
  break;
364
332
  }
365
- if (b.type === 0 /* ColType.ANCHOR */) {
366
- if (a.type !== 0 /* ColType.ANCHOR */) {
333
+ if (b.mergeKey === ANCHOR_KEY) {
334
+ if (a.mergeKey !== ANCHOR_KEY) {
367
335
  result.push(a);
368
336
  a = track0[++i];
369
- while (i < l0 && a.type !== 0 /* ColType.ANCHOR */) {
337
+ while (i < l0 && a.mergeKey !== ANCHOR_KEY) {
370
338
  if (idt)
371
339
  a.t += idt;
372
340
  result.push(a);
@@ -392,7 +360,19 @@ export class LoweringContext {
392
360
  break;
393
361
  }
394
362
  if (Math.abs(t0 - t1) < 1e-6) {
395
- if (a.type === 1 /* ColType.SINGLE */) {
363
+ if (a.mergeKey === b.mergeKey) {
364
+ a.push(...b);
365
+ result.push(a);
366
+ a = track0[++i];
367
+ b = track1[++j];
368
+ if (a)
369
+ a.t = t0 = a.t + idt;
370
+ if (b)
371
+ b.t = t1 = b.t + jdt;
372
+ if (!a || !b)
373
+ break;
374
+ }
375
+ else if (a.mergeKey < b.mergeKey) {
396
376
  result.push(a);
397
377
  a = track0[++i];
398
378
  if (a) {
@@ -401,7 +381,7 @@ export class LoweringContext {
401
381
  else
402
382
  break;
403
383
  }
404
- else if (b.type === 1 /* ColType.SINGLE */) {
384
+ else {
405
385
  result.push(b);
406
386
  b = track1[++j];
407
387
  if (b) {
@@ -410,18 +390,6 @@ export class LoweringContext {
410
390
  else
411
391
  break;
412
392
  }
413
- else {
414
- a.push(...b);
415
- result.push(a);
416
- a = track0[++i];
417
- b = track1[++j];
418
- if (a)
419
- a.t = t0 = a.t + idt;
420
- if (b)
421
- b.t = t1 = b.t + jdt;
422
- if (!a || !b)
423
- break;
424
- }
425
393
  continue;
426
394
  }
427
395
  if (t0 < t1) {
@@ -502,42 +470,36 @@ export class LoweringContext {
502
470
  result.push(container);
503
471
  continue;
504
472
  }
505
- switch (front.e.type) {
506
- case 0 /* ColType.ANCHOR */:
507
- // 进入缓冲区 不补充本track的下一个元素
508
- anchorBuffer.push(front);
509
- break;
510
- case 1 /* ColType.SINGLE */:
511
- result.push(front.e);
512
- fetchNext(front.i);
473
+ if (front.e.mergeKey === ANCHOR_KEY) {
474
+ // 进入缓冲区 不补充本track的下一个元素
475
+ anchorBuffer.push(front);
476
+ continue;
477
+ }
478
+ // 找同时刻的同组
479
+ const minT = front.e.t + 1e-6; // 避免浮点数误差
480
+ const consumed = [front.i];
481
+ while (true) {
482
+ const f = heap.front();
483
+ // 堆序保证同组相邻,遇到别的组即说明本组已取完
484
+ if (!f || f.e.t > minT || f.e.mergeKey !== front.e.mergeKey)
513
485
  break;
514
- default:
515
- // 找同时的
516
- const minT = front.e.t + 1e-6; // 避免浮点数误差
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);
486
+ heap.pop();
487
+ front.e.push(...f.e);
488
+ consumed.push(f.i);
531
489
  }
490
+ result.push(front.e);
491
+ // 最后再加新的,使得只合并每个轨道最前面的,防止把后面同时刻的都合并了
492
+ for (const i of consumed)
493
+ fetchNext(i);
532
494
  }
533
495
  return result;
534
496
  }
535
497
  }
536
498
  class TimeColumn extends Array {
537
- type;
499
+ mergeKey;
538
500
  constructor(n) {
539
501
  super(1);
540
- this.type = n.type;
502
+ this.mergeKey = n.mergeKey;
541
503
  this[0] = n;
542
504
  }
543
505
  get t() {
@@ -547,14 +509,6 @@ class TimeColumn extends Array {
547
509
  for (const n of this)
548
510
  n.t = value;
549
511
  }
550
- /** 当前列是否携带换行请求 */
551
- get breakRequested() {
552
- for (const node of this) {
553
- if (node.breakBefore > 0)
554
- return true;
555
- }
556
- return false;
557
- }
558
512
  /** 将当前列所有事件的行号固化为 lowering 后的实际行号 */
559
513
  set layoutLine(value) {
560
514
  for (const node of this)
@@ -564,9 +518,9 @@ class TimeColumn extends Array {
564
518
  function heapItemCmp(a, b) {
565
519
  if (a.e.t !== b.e.t)
566
520
  return a.e.t - b.e.t;
567
- const orderDiff = a.e.type - b.e.type;
568
- if (orderDiff !== 0)
569
- return orderDiff;
521
+ // 有无穷所以不能直接减
522
+ if (a.e.mergeKey !== b.e.mergeKey)
523
+ return a.e.mergeKey < b.e.mergeKey ? -1 : 1;
570
524
  return a.i - b.i;
571
525
  }
572
526
  // 最小堆