jpfun 0.5.0 → 0.7.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 (79) hide show
  1. package/dist/converter/midi/convert.d.ts +3 -1
  2. package/dist/converter/midi/convert.js +78 -66
  3. package/dist/converter/musicxml/features.d.ts +19 -5
  4. package/dist/converter/musicxml/features.js +46 -8
  5. package/dist/converter/musicxml/index.js +142 -23
  6. package/dist/converter/musicxml/model.d.ts +15 -0
  7. package/dist/diagnostic.js +1 -1
  8. package/dist/functions/ASTtypes.d.ts +4 -0
  9. package/dist/functions/ASTtypes.js +25 -14
  10. package/dist/functions/adjust/index.js +6 -2
  11. package/dist/functions/arpeggio/index.js +21 -17
  12. package/dist/functions/arpeggio/shape.d.ts +1 -1
  13. package/dist/functions/arpeggio/shape.js +27 -21
  14. package/dist/functions/bar/index.js +37 -36
  15. package/dist/functions/beam/auto.js +18 -24
  16. package/dist/functions/beam/layout.js +15 -23
  17. package/dist/functions/box/index.js +52 -1
  18. package/dist/functions/br/index.js +3 -4
  19. package/dist/functions/default.js +10 -5
  20. package/dist/functions/div/index.js +2 -3
  21. package/dist/functions/gliss/index.d.ts +2 -0
  22. package/dist/functions/gliss/index.js +180 -0
  23. package/dist/functions/grace/index.d.ts +4 -2
  24. package/dist/functions/grace/index.js +19 -11
  25. package/dist/functions/head/index.js +50 -62
  26. package/dist/functions/meter/index.js +45 -24
  27. package/dist/functions/meter/shape.d.ts +3 -0
  28. package/dist/functions/meter/shape.js +43 -0
  29. package/dist/functions/note/accidentals.js +10 -16
  30. package/dist/functions/note/index.js +38 -48
  31. package/dist/functions/ottava/index.d.ts +2 -0
  32. package/dist/functions/ottava/index.js +217 -0
  33. package/dist/functions/page/index.js +3 -3
  34. package/dist/functions/pagebreak/index.d.ts +2 -0
  35. package/dist/functions/pagebreak/index.js +36 -0
  36. package/dist/functions/rest/index.d.ts +2 -0
  37. package/dist/functions/rest/index.js +231 -0
  38. package/dist/functions/slide/index.d.ts +2 -0
  39. package/dist/functions/slide/index.js +132 -0
  40. package/dist/functions/stack/index.js +8 -15
  41. package/dist/functions/symbol/index.d.ts +9 -0
  42. package/dist/functions/symbol/index.js +41 -12
  43. package/dist/functions/symbol/ornament.js +15 -20
  44. package/dist/functions/symbol/shape.js +4 -1
  45. package/dist/functions/symbol/symbols/fine.js +1 -1
  46. package/dist/functions/symbol/symbols/tr.js +46 -1
  47. package/dist/functions/temporal.d.ts +36 -12
  48. package/dist/functions/temporal.js +29 -9
  49. package/dist/functions/tie/index.js +46 -32
  50. package/dist/functions/tuplet/index.js +1 -1
  51. package/dist/functions/up/index.js +51 -33
  52. package/dist/functions/voice/connections.d.ts +19 -0
  53. package/dist/functions/voice/connections.js +90 -0
  54. package/dist/functions/voice/index.js +245 -99
  55. package/dist/functions/volta/index.js +41 -21
  56. package/dist/jpfun.core.min.js +48 -55
  57. package/dist/jpfun.from-midi.min.js +5 -5
  58. package/dist/jpfun.from-musicxml.min.js +8 -8
  59. package/dist/jpfun.min.js +57 -64
  60. package/dist/layout/engine.d.ts +4 -1
  61. package/dist/layout/engine.js +232 -75
  62. package/dist/layout/model.js +22 -66
  63. package/dist/layout/page.d.ts +3 -2
  64. package/dist/layout/page.js +7 -6
  65. package/dist/layout/types.d.ts +34 -45
  66. package/dist/lowering/loweringContext.d.ts +1 -1
  67. package/dist/lowering/loweringContext.js +41 -171
  68. package/dist/parser/parserContext.d.ts +2 -0
  69. package/dist/parser/parserContext.js +31 -14
  70. package/dist/parser/preprocess.js +4 -6
  71. package/dist/playback/compile.js +12 -3
  72. package/dist/playback/types.d.ts +4 -2
  73. package/dist/render/paint.js +1 -1
  74. package/dist/render/svg.js +1 -0
  75. package/dist/render/text.d.ts +1 -1
  76. package/dist/render/text.js +3 -2
  77. package/dist/render/types.d.ts +1 -0
  78. package/grammar.md +221 -257
  79. package/package.json +1 -1
@@ -3,9 +3,10 @@ import { type VisualTemporalNode } from "../functions/temporal.js";
3
3
  import type { LoweringResult } from "../lowering/types.js";
4
4
  import { type SolverOptions } from "./model.js";
5
5
  import { type DocumentLayoutPage } from "./page.js";
6
- import type { HorizontalLineView, LayoutPrepareContext, PlacedAttachment, Rect } from "./types.js";
6
+ import type { HorizontalLineView, LayoutHost, LayoutPrepareContext, PlacedAttachment, Rect } from "./types.js";
7
7
  export interface DocumentLayoutOptions extends SolverOptions {
8
8
  rowGap?: number;
9
+ horizontalOnly?: boolean;
9
10
  }
10
11
  export { PageLayoutError } from "./page.js";
11
12
  export type { DocumentLayoutPage } from "./page.js";
@@ -17,6 +18,8 @@ export interface DocumentLayoutResult {
17
18
  lineCount: number;
18
19
  pages: DocumentLayoutPage[];
19
20
  }
21
+ /** 内容边界不含纯留白,完整占用还包含装饰预留的上下空间 */
22
+ export declare function getLayoutBounds(host: LayoutHost, contentOnly?: boolean): Rect;
20
23
  /**
21
24
  * 把多个已经定位的盒子合并到 target
22
25
  */
@@ -9,6 +9,18 @@ import { TranslatingPainter } from "../render/translate.js";
9
9
  export { PageLayoutError } from "./page.js";
10
10
  /** 轨道间距相对行内最大字号的比例;正文行与子域共用 */
11
11
  const ROW_GAP_RATIO = 0.75;
12
+ // 装饰边界是本轮排列结果,不作为主体上的第二套空间协议
13
+ const decorationExtents = new WeakMap();
14
+ /** 内容边界不含纯留白,完整占用还包含装饰预留的上下空间 */
15
+ export function getLayoutBounds(host, contentOnly = false) {
16
+ const { box } = host;
17
+ const extent = decorationExtents.get(host);
18
+ if (!extent)
19
+ return box;
20
+ const top = contentOnly ? extent.contentTop : extent.top;
21
+ const bottom = contentOnly ? 0 : extent.bottom;
22
+ return { x: box.x, y: box.y + top, w: box.w, h: box.h - top + bottom };
23
+ }
12
24
  /**
13
25
  * 把多个已经定位的盒子合并到 target
14
26
  */
@@ -48,21 +60,25 @@ export function unionLayoutBoxes(target, boxes) {
48
60
  */
49
61
  export function prepareLayoutHost(node, context) {
50
62
  node.springConfig ??= {};
63
+ if (node.decorations)
64
+ decorationExtents.delete(node);
51
65
  node.decorations = [];
52
66
  node.ports = {};
53
67
  node.box.x = node.box.y = 0;
54
68
  node.prepareLayout(context); // 计算大小等 layout 需要的参数
55
69
  // 把 lowering 固化在 addon 中的语义交给已注册 handler,生成本轮函数装饰
56
- for (const [key, value] of Object.entries(node.addon ?? {})) {
57
- const handler = context.decorationHandlers.get(key);
58
- if (!handler)
59
- continue;
60
- const decoration = handler(node, value, context);
61
- if (decoration)
62
- node.decorations.push(decoration);
70
+ if (node.addon) {
71
+ for (const [key, value] of Object.entries(node.addon)) {
72
+ const handler = context.decorationHandlers.get(key);
73
+ if (!handler)
74
+ continue;
75
+ const decoration = handler(node, value, context);
76
+ if (decoration)
77
+ node.decorations.push(decoration);
78
+ }
63
79
  }
64
80
  // 处理 decoration
65
- arrangeBelowDecorations(node);
81
+ arrangeDecorations(node);
66
82
  // 让节点在最终盒尺寸确定后发布依赖 box 的端口
67
83
  node.finalizeLayout?.(context);
68
84
  }
@@ -78,10 +94,19 @@ export function layoutDocument(result, context, options = {}) {
78
94
  // 子域在主体准备时就排版完毕;它认领的附件保留块内几何,随块平移
79
95
  const order = new Map(layoutAttachments.map((attachment, index) => [attachment, index]));
80
96
  const relations = groupRelations(layoutAttachments);
97
+ let hosts = [];
98
+ if (layoutAttachments.length) {
99
+ for (const nodes of result.astToTemporal.values()) {
100
+ if (!nodes.some(node => node.placementOffset?.x || node.placementOffset?.y))
101
+ continue;
102
+ hosts = [...new Set([...result.astToTemporal.values()].flat().filter(isVisualTemporalNode))];
103
+ break;
104
+ }
105
+ }
81
106
  const subdomainItems = new Map();
82
- const subdomainBox = (attachment) => {
107
+ const subdomainGeometry = (attachment, layoutOnly = false) => {
83
108
  const item = subdomainItems.get(attachment);
84
- return item && moveSubdomainItem(item).box;
109
+ return item && moveSubdomainItem(item, layoutOnly);
85
110
  };
86
111
  context = {
87
112
  ...context,
@@ -103,13 +128,13 @@ export function layoutDocument(result, context, options = {}) {
103
128
  own.push(relation);
104
129
  }
105
130
  own.sort((left, right) => order.get(left) - order.get(right));
106
- const block = layoutSubdomainBlock(columns, line, root, own, context, subdomainBox);
131
+ const block = layoutSubdomainBlock(columns, line, root, own, context, subdomainGeometry, hosts.length ? hosts.filter(within) : hosts, owner);
107
132
  own.forEach((attachment, index) => subdomainItems.set(attachment, block.items[index]));
108
133
  return { width: line.width, height: block.height, positions: block.positions };
109
134
  },
110
135
  };
111
136
  // 1. 按 layoutLine 切行,并生成固有尺寸与装饰尺寸
112
- const lines = splitLayoutLines(result);
137
+ const { lines, pageBreaks } = splitLayoutLines(result);
113
138
  const objects = [];
114
139
  for (const line of lines) {
115
140
  for (const column of line.columns) {
@@ -132,12 +157,22 @@ export function layoutDocument(result, context, options = {}) {
132
157
  node.box.x += originX;
133
158
  }
134
159
  }
160
+ // midi 导入进行自动断行的快通道
161
+ if (options.horizontalOnly) {
162
+ return {
163
+ diagnostics: result.diagnostics,
164
+ objects,
165
+ attachments: [],
166
+ bounds: { x: 0, y: 0, w: 0, h: 0 },
167
+ lineCount: lines.length,
168
+ pages: [],
169
+ };
170
+ }
135
171
  // host 申报纵向占用
136
172
  // 主体占用只依赖固有尺寸,整个纵向布局中保持不变
137
173
  for (const node of objects) {
138
174
  const line = lines[node.layoutLine];
139
- const top = -node.box.visualAxis;
140
- includeTrackExtent(line.hostExtents, node.track, top, top + node.box.h);
175
+ includeHostExtent(line.hostExtents, node);
141
176
  }
142
177
  // 根据配置得到行距
143
178
  const rowGaps = measureRowGaps(lines.length, objects, options.rowGap);
@@ -152,7 +187,7 @@ export function layoutDocument(result, context, options = {}) {
152
187
  let pages;
153
188
  let lineTops;
154
189
  try {
155
- ({ pages, lineTops } = paginateLayoutLines(heights, page));
190
+ ({ pages, lineTops } = paginateLayoutLines(heights, page, pageBreaks));
156
191
  }
157
192
  catch (error) {
158
193
  if (!(error instanceof PageLayoutError))
@@ -192,10 +227,10 @@ export function layoutDocument(result, context, options = {}) {
192
227
  };
193
228
  // 3. 首次纵向放置后测量 attachment;只有有效轨道占用扩张时才重新求解
194
229
  let placement = placeVertically();
195
- let measured = measureAttachments(documentAttachments, placement.attachmentContext, lines, lift, subdomainBox);
230
+ let measured = measureAttachments(documentAttachments, placement.attachmentContext, lines, lift, subdomainGeometry, hosts);
196
231
  if (measured.needsRelayout) {
197
232
  placement = placeVertically();
198
- measured = measureAttachments(documentAttachments, placement.attachmentContext, lines, lift, subdomainBox);
233
+ measured = measureAttachments(documentAttachments, placement.attachmentContext, lines, lift, subdomainGeometry, hosts);
199
234
  }
200
235
  const pages = placement.pages;
201
236
  let next = 0;
@@ -219,7 +254,7 @@ export function layoutDocument(result, context, options = {}) {
219
254
  for (const pageResult of pages)
220
255
  yield pageResult.bounds;
221
256
  for (const node of objects)
222
- yield node.box;
257
+ yield getLayoutBounds(node);
223
258
  for (const attachment of attachments)
224
259
  yield attachment.box;
225
260
  }
@@ -234,24 +269,50 @@ export function layoutDocument(result, context, options = {}) {
234
269
  };
235
270
  }
236
271
  /**
237
- * 按 below.order 排列主体下方装饰,调用 place 并把其占高计入 box.h
272
+ * 上下分别由内向外排列,纯留白只进入占用,不改变主体绘制坐标
238
273
  */
239
- function arrangeBelowDecorations(node) {
240
- // 现代 ECMAScript 的稳定排序会保留相同 order 的注册顺序
241
- const below = node.decorations
242
- .map(decoration => decoration.below)
243
- .filter(item => item !== void 0)
244
- .sort((left, right) => left.order - right.order);
245
- if (below.length === 0)
274
+ function arrangeDecorations(node) {
275
+ if (node.decorations.length === 0)
246
276
  return;
247
- // 依次分配每个装饰的局部上边界,并累加最终盒高
248
- let y = node.box.h;
249
- for (const item of below) {
250
- y += item.gap ?? 0;
251
- item.place?.(y);
252
- y += Math.max(0, item.height ?? 0);
277
+ let contentTop = 0;
278
+ let contentBottom = -Infinity;
279
+ let top = 0;
280
+ let bottom = -Infinity;
281
+ for (const side of ["above", "below"]) {
282
+ const above = side === "above";
283
+ const decorations = node.decorations.length === 1 ? node.decorations : node.decorations.filter(item => item[side]);
284
+ if (decorations.length > 1)
285
+ decorations.sort((left, right) => left[side].order - right[side].order);
286
+ // 下方可覆盖起点,避免把字体盒的留白当成可见正文;两侧仍独立排列
287
+ let cursor = above ? 0 : (node.ports["decoration.below"]?.y ?? node.box.h);
288
+ let sideBottom = above ? -Infinity : node.box.h;
289
+ let sideContentBottom = sideBottom;
290
+ for (const decoration of decorations) {
291
+ const item = decoration[side];
292
+ if (!item)
293
+ continue;
294
+ const height = Math.max(0, item.height ?? 0);
295
+ // 纯留白包围完整内容,不能被正文盒内的装饰起点吞掉
296
+ if (!above && !decoration.paint)
297
+ cursor = Math.max(cursor, sideContentBottom);
298
+ const y = above ? cursor - (item.gap ?? 0) - height : cursor + (item.gap ?? 0);
299
+ item.place?.(y);
300
+ cursor = above ? y : y + height;
301
+ top = Math.min(top, y);
302
+ sideBottom = above ? Math.max(sideBottom, y + height) : cursor;
303
+ if (decoration.paint) {
304
+ contentTop = Math.min(contentTop, y);
305
+ sideContentBottom = above ? Math.max(sideContentBottom, y + height) : cursor;
306
+ }
307
+ }
308
+ bottom = Math.max(bottom, sideBottom);
309
+ contentBottom = Math.max(contentBottom, sideContentBottom);
310
+ }
311
+ node.box.h = contentBottom;
312
+ const extraBottom = Math.max(0, bottom - contentBottom);
313
+ if (top !== 0 || extraBottom !== 0) {
314
+ decorationExtents.set(node, { top, bottom: extraBottom, contentTop });
253
315
  }
254
- node.box.h = y;
255
316
  }
256
317
  /**
257
318
  * 按 lowering 已固化的 layoutLine 把可见事件拆成谱面行
@@ -265,6 +326,7 @@ function splitLayoutLines(result) {
265
326
  attachmentExtents: new Map(),
266
327
  });
267
328
  const lines = [];
329
+ const pageBreaks = [];
268
330
  let currentLine = createLine();
269
331
  for (const column of result.columns) {
270
332
  const visibleColumn = []; // 从列中提取可见元素
@@ -274,16 +336,20 @@ function splitLayoutLines(result) {
274
336
  currentLine = createLine();
275
337
  }
276
338
  for (const node of column) {
339
+ const pageBreak = node.addon?.pageBreakBefore;
340
+ if (pageBreak !== undefined) {
341
+ pageBreaks[lines.length] = (pageBreaks[lines.length] ?? true) && pageBreak;
342
+ }
277
343
  if (isVisualTemporalNode(node))
278
344
  visibleColumn.push(node);
279
345
  }
280
346
  if (visibleColumn.length)
281
347
  currentLine.columns.push(visibleColumn);
282
348
  }
283
- // 至少要有一行,哪怕它没有任何可见对象
284
- if (currentLine.columns.length > 0 || lines.length === 0)
349
+ // 至少保留一行;强制换页新开的尾页也不能因没有可见内容而丢弃
350
+ if (currentLine.columns.length > 0 || pageBreaks[lines.length] !== undefined || lines.length === 0)
285
351
  lines.push(currentLine);
286
- return lines;
352
+ return { lines, pageBreaks };
287
353
  }
288
354
  /** 把新的上下界原地并入已有纵向占用范围 */
289
355
  function includeExtent(extent, top, bottom) {
@@ -305,6 +371,7 @@ export function prepareHorizontalLine(columns, index, options = {}, compact = fa
305
371
  const hooks = [];
306
372
  const columnIndex = new Map();
307
373
  const trackRuns = new Map();
374
+ let trackIndex;
308
375
  for (let column = 0; column < columns.length; column++) {
309
376
  for (const node of columns[column]) {
310
377
  columnIndex.set(node, column);
@@ -321,6 +388,16 @@ export function prepareHorizontalLine(columns, index, options = {}, compact = fa
321
388
  columns,
322
389
  trackRuns,
323
390
  columnOf: host => columnIndex.get(host) ?? -1,
391
+ trackIndexOf(host) {
392
+ if (!trackIndex) {
393
+ trackIndex = new Map();
394
+ for (const run of trackRuns.values()) {
395
+ for (let i = 0; i < run.length; i++)
396
+ trackIndex.set(run[i], i);
397
+ }
398
+ }
399
+ return trackIndex.get(host) ?? -1;
400
+ },
324
401
  registerHorizontalLayoutHook(from, to, hook) {
325
402
  const start = columnIndex.get(from);
326
403
  const end = columnIndex.get(to);
@@ -379,13 +456,12 @@ function layoutLocalLine(columns, context) {
379
456
  * 与正文行的流程相同:沿 root 的 Track 树求轴放置,按登记顺序测量附件,
380
457
  * 附件扩张占用时重排一次。positions 是放置时写入的块内位置,不含成员 onPlaced 追加的平移
381
458
  */
382
- function layoutSubdomainBlock(columns, { width, view }, root, attachments, context, measuredElsewhere) {
459
+ function layoutSubdomainBlock(columns, { width, view }, root, attachments, context, measuredElsewhere, hosts, owner) {
383
460
  const members = columns.flat();
384
461
  const extents = { hostExtents: new Map(), attachmentExtents: new Map() };
385
462
  let gap = 0;
386
463
  for (const member of members) {
387
- const top = -member.box.visualAxis;
388
- includeTrackExtent(extents.hostExtents, member.track, top, top + member.box.h);
464
+ includeHostExtent(extents.hostExtents, member);
389
465
  gap = Math.max(gap, member.ast.size * ROW_GAP_RATIO);
390
466
  }
391
467
  const positions = members.map(member => ({ x: member.box.x, y: 0 }));
@@ -415,14 +491,21 @@ function layoutSubdomainBlock(columns, { width, view }, root, attachments, conte
415
491
  getVisualAxis: (_line, track) => solved.axes.get(lift(track)) ?? 0,
416
492
  getHostExtent: (line, track) => lines[line]?.hostExtents.get(lift(track)),
417
493
  getRangeColumns: (line, range, track) => rangeColumns(views[line], range, track, lift),
418
- }, lines, lift, measuredElsewhere);
494
+ }, lines, lift, measuredElsewhere, hosts, owner);
419
495
  let measured = measure();
420
496
  if (measured.needsRelayout) {
421
497
  solved = place();
422
498
  measured = measure();
423
499
  }
424
500
  const reference = members[0];
425
- const items = measured.items.map(item => ({ ...item, reference, x: reference.box.x, y: reference.box.y }));
501
+ const position = { x: reference.box.x, y: reference.box.y };
502
+ const offset = measured.layoutItems && placementOffset(reference, owner);
503
+ const layoutPosition = offset && (offset.x !== 0 || offset.y !== 0)
504
+ ? { x: position.x - offset.x, y: position.y - offset.y } : position;
505
+ const items = measured.items.map((item, index) => ({
506
+ ...item, reference, position,
507
+ ...(measured.layoutItems && { layout: { ...measured.layoutItems[index], position: layoutPosition } }),
508
+ }));
426
509
  return { height: solved.height, positions, items };
427
510
  }
428
511
  /**
@@ -455,17 +538,19 @@ function encloses(owner, node) {
455
538
  return false;
456
539
  }
457
540
  /** 子域是刚性块:按参照成员的位移平移块内测得的几何 */
458
- function moveSubdomainItem(item) {
459
- const dx = item.reference.box.x - item.x;
460
- const dy = item.reference.box.y - item.y;
541
+ function moveSubdomainItem(item, layoutOnly = false) {
542
+ const measured = layoutOnly ? item.layout ?? item : item;
543
+ const { position, geometry, box } = measured;
544
+ const dx = item.reference.box.x - position.x;
545
+ const dy = item.reference.box.y - position.y;
461
546
  if (dx === 0 && dy === 0)
462
- return item;
547
+ return measured;
463
548
  const move = (rect) => ({ ...rect, x: rect.x + dx, y: rect.y + dy });
464
- const { geometry } = item;
465
549
  return {
466
- box: move(item.box),
550
+ box: move(box),
467
551
  geometry: {
468
552
  regions: geometry.regions.map(move),
553
+ occupancy: geometry.occupancy?.map(move),
469
554
  paint: painter => geometry.paint(new TranslatingPainter(painter, dx, dy)),
470
555
  },
471
556
  };
@@ -487,25 +572,78 @@ function measureRowGaps(lineCount, objects, override) {
487
572
  gaps[i] ||= documentGap;
488
573
  return gaps;
489
574
  }
490
- /**
491
- * 按 lowering 注册顺序原子生成本轮几何,并同步登记轨道占用
492
- *
493
- * 正文与子域共用:lines 与 baseContext.lines 都按行号索引,lift 把域外的私有轨归入本域。
494
- * 分组的 attachment 总在组内对象之后注册,所以读取依赖不需要额外排序;
495
- * 占用逐条累加,后注册者因此能通过 getRangeExtents 避让先注册者。
496
- * 其他域测得的附件由 measuredElsewhere 给出当前位置。
497
- * 返回的 needsRelayout 表示本轮占用超出了主体与先前区域的合并范围。
498
- */
499
- function measureAttachments(attachments, baseContext, lines, lift, measuredElsewhere) {
575
+ /** 折叠成员继承外层平移,子域只累加当前复合体内部的平移 */
576
+ function placementOffset(node, owner) {
577
+ let x = 0;
578
+ let y = 0;
579
+ for (let host = node; host && host !== owner; host = host.foldedInto) {
580
+ x += host.placementOffset?.x ?? 0;
581
+ y += host.placementOffset?.y ?? 0;
582
+ }
583
+ return { x, y };
584
+ }
585
+ /** 后置平移只改变绘制几何,占用始终在未平移的布局位置测量 */
586
+ function measureAttachments(attachments, baseContext, lines, lift, measuredElsewhere, hosts, owner) {
587
+ if (attachments.length === 0)
588
+ return { items: [], needsRelayout: false };
589
+ const shifts = [];
590
+ for (const node of hosts) {
591
+ const { x, y } = placementOffset(node, owner);
592
+ if (x !== 0 || y !== 0)
593
+ shifts.push({ node, x, y, boxX: node.box.x, boxY: node.box.y });
594
+ }
595
+ const displaced = shifts.length > 0 || attachments.some(attachment => attachment.placementOffset?.x || attachment.placementOffset?.y);
596
+ let layout;
597
+ if (displaced) {
598
+ for (const { node, x, y } of shifts) {
599
+ node.box.x -= x;
600
+ node.box.y -= y;
601
+ }
602
+ try {
603
+ layout = measureAttachmentPass(attachments, { ...baseContext, layoutOnly: true }, lines, lift, measuredElsewhere, true);
604
+ }
605
+ finally {
606
+ for (const { node, boxX, boxY } of shifts) {
607
+ node.box.x = boxX;
608
+ node.box.y = boxY;
609
+ }
610
+ }
611
+ }
612
+ else
613
+ layout = measureAttachmentPass(attachments, baseContext, lines, lift, measuredElsewhere, true);
614
+ if (!displaced)
615
+ return layout;
616
+ // 试测结果若被重排丢弃,就不再为它生成绘制几何
617
+ let painted;
618
+ return {
619
+ needsRelayout: layout.needsRelayout,
620
+ layoutItems: layout.items,
621
+ get items() {
622
+ return painted ??= measureAttachmentPass(attachments, baseContext, lines, lift, measuredElsewhere, false).items;
623
+ },
624
+ };
625
+ }
626
+ /** 按声明顺序测量几何,后注册的附件可以查询此前的区域和占用 */
627
+ function measureAttachmentPass(attachments, baseContext, lines, lift, measuredElsewhere, reserve) {
500
628
  const measured = new Map();
501
- const own = new Set(attachments);
502
629
  const occupancy = [];
503
630
  let needsRelayout = false;
504
- function getRangeExtents(line, columns, track) {
631
+ // 同一测量轮坐标不变,子域几何只需平移一次
632
+ function resolveAttachment(dependency) {
633
+ let item = measured.get(dependency);
634
+ if (!item) {
635
+ item = measuredElsewhere(dependency, baseContext.layoutOnly);
636
+ if (item)
637
+ measured.set(dependency, item);
638
+ }
639
+ return item;
640
+ }
641
+ function getExtents(line, range, track) {
505
642
  const view = baseContext.lines[line];
506
643
  const extents = track === undefined ? new Map() : undefined;
507
644
  let extent;
508
- if (!view)
645
+ const horizontal = range && "left" in range ? range : undefined;
646
+ if (!view || (horizontal && horizontal.left >= horizontal.right))
509
647
  return extents;
510
648
  const include = (owner, top, bottom) => {
511
649
  if (extents)
@@ -516,22 +654,26 @@ function measureAttachments(attachments, baseContext, lines, lift, measuredElsew
516
654
  extent = { top, bottom };
517
655
  };
518
656
  // 主体和附件共用范围筛选,仅累加结果的容器随查询模式变化。
519
- const { start, end, wholeLine } = resolveRange(view, columns, track, lift);
657
+ const { start, end, wholeLine } = resolveRange(view, range && !("left" in range) ? range : undefined, track, lift);
520
658
  const target = track && lift(track);
521
- let left = wholeLine ? baseContext.originX : Infinity;
522
- let right = wholeLine ? baseContext.originX + baseContext.width : -Infinity;
659
+ let left = horizontal?.left ?? (wholeLine ? baseContext.originX : Infinity);
660
+ let right = horizontal?.right ?? (wholeLine ? baseContext.originX + baseContext.width : -Infinity);
523
661
  for (let column = start; column <= end; column++) {
524
662
  for (const host of view.columns[column]) {
525
663
  if (target && host.track !== target)
526
664
  continue;
665
+ if (horizontal && (host.box.x + host.box.w <= left || host.box.x >= right))
666
+ continue;
527
667
  if (!wholeLine) {
528
668
  if (host.box.x < left)
529
669
  left = host.box.x;
530
670
  if (host.box.x + host.box.w > right)
531
671
  right = host.box.x + host.box.w;
532
672
  }
533
- const hostTop = host.box.y - baseContext.getVisualAxis(line, host.track);
534
- include(host.track, hostTop, hostTop + host.box.h);
673
+ const decoration = decorationExtents.get(host);
674
+ const top = decoration?.top ?? 0;
675
+ const hostTop = host.box.y + top - baseContext.getVisualAxis(line, host.track);
676
+ include(host.track, hostTop, hostTop + (host.box.h - top + (decoration?.bottom ?? 0)));
535
677
  }
536
678
  }
537
679
  for (const range of occupancy[line] ?? []) {
@@ -545,26 +687,32 @@ function measureAttachments(attachments, baseContext, lines, lift, measuredElsew
545
687
  }
546
688
  const context = {
547
689
  ...baseContext,
548
- getRangeExtents,
690
+ getRangeExtents: getExtents,
691
+ getHorizontalExtents: (line, left, right) => getExtents(line, { left, right }),
692
+ getAttachmentOccupancy(dependency) {
693
+ const geometry = resolveAttachment(dependency)?.geometry;
694
+ return geometry && (geometry.occupancy ?? geometry.regions);
695
+ },
549
696
  getAttachmentBox(dependency) {
550
- const resolved = measured.get(dependency) ?? measuredElsewhere(dependency);
697
+ const resolved = resolveAttachment(dependency);
551
698
  if (!resolved)
552
699
  throw new Error("Layout attachment dependency has not been measured");
553
- return resolved;
700
+ return resolved.box;
554
701
  },
555
702
  getContentBounds(content) {
556
703
  const boxes = [];
557
704
  for (const node of content.nodes) {
558
705
  if (isVisualTemporalNode(node) && (node.box.w > 0 || node.box.h > 0))
559
- boxes.push(node.box);
706
+ boxes.push(getLayoutBounds(node, true));
560
707
  }
561
708
  for (const attachment of content.attachments) {
562
709
  if (!isLayoutAttachment(attachment))
563
710
  continue;
564
711
  // 端点越出本域的关系交给外层测量,此时还没有几何
565
- if (!own.has(attachment) && !measuredElsewhere(attachment))
712
+ const resolved = resolveAttachment(attachment);
713
+ if (!resolved && !attachments.includes(attachment))
566
714
  continue;
567
- const box = context.getAttachmentBox(attachment);
715
+ const box = resolved?.box ?? context.getAttachmentBox(attachment);
568
716
  if (box.w > 0 || box.h > 0)
569
717
  boxes.push(box);
570
718
  }
@@ -577,7 +725,7 @@ function measureAttachments(attachments, baseContext, lines, lift, measuredElsew
577
725
  const box = { x: 0, y: 0, w: 0, h: 0 };
578
726
  unionLayoutBoxes(box, geometry.regions);
579
727
  const item = { geometry, box };
580
- measured.set(attachment, box);
728
+ measured.set(attachment, item);
581
729
  // 当前项立即登记,保证后注册的 attachment 能看到并避让它
582
730
  for (const region of geometry.occupancy ?? geometry.regions) {
583
731
  if (region.line === void 0)
@@ -588,11 +736,12 @@ function measureAttachments(attachments, baseContext, lines, lift, measuredElsew
588
736
  const bottom = top + region.h;
589
737
  const hostExtent = line.hostExtents.get(track);
590
738
  const attachmentExtent = line.attachmentExtents.get(track);
591
- if (top < Math.min(hostExtent?.top ?? Infinity, attachmentExtent?.top ?? Infinity)
592
- || bottom > Math.max(hostExtent?.bottom ?? -Infinity, attachmentExtent?.bottom ?? -Infinity)) {
739
+ if (reserve && (top < Math.min(hostExtent?.top ?? Infinity, attachmentExtent?.top ?? Infinity)
740
+ || bottom > Math.max(hostExtent?.bottom ?? -Infinity, attachmentExtent?.bottom ?? -Infinity))) {
593
741
  needsRelayout = true;
594
742
  }
595
- includeTrackExtent(line.attachmentExtents, track, top, bottom);
743
+ if (reserve)
744
+ includeTrackExtent(line.attachmentExtents, track, top, bottom);
596
745
  (occupancy[region.line] ??= []).push({
597
746
  track,
598
747
  left: region.x,
@@ -605,6 +754,14 @@ function measureAttachments(attachments, baseContext, lines, lift, measuredElsew
605
754
  });
606
755
  return { items, needsRelayout };
607
756
  }
757
+ /** 固有占用只需轴局部边界,不必创建全局矩形 */
758
+ function includeHostExtent(extents, host) {
759
+ const decoration = decorationExtents.get(host);
760
+ const top = decoration?.top ?? 0;
761
+ const height = host.box.h - top + (decoration?.bottom ?? 0);
762
+ const axisTop = top - host.box.visualAxis;
763
+ includeTrackExtent(extents, host.track, axisTop, axisTop + height);
764
+ }
608
765
  /**
609
766
  * 沿 Track 树自内向外求出一条谱面行里所有音轨的纵向轴
610
767
  *