jpfun 0.3.0 → 0.4.1

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 (129) 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 +30 -26
  23. package/dist/functions/ASTtypes.js +25 -23
  24. package/dist/functions/adjust/index.js +13 -6
  25. package/dist/functions/arpeggio/index.d.ts +2 -0
  26. package/dist/functions/arpeggio/index.js +184 -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 +30 -23
  30. package/dist/functions/beam/auto.d.ts +1 -1
  31. package/dist/functions/beam/auto.js +1 -1
  32. package/dist/functions/beam/index.js +6 -1
  33. package/dist/functions/beam/layout.d.ts +3 -3
  34. package/dist/functions/beam/layout.js +5 -3
  35. package/dist/functions/box/index.js +10 -3
  36. package/dist/functions/br/index.js +10 -6
  37. package/dist/functions/dash/index.js +12 -9
  38. package/dist/functions/default.js +6 -2
  39. package/dist/functions/div/index.d.ts +4 -1
  40. package/dist/functions/div/index.js +8 -7
  41. package/dist/functions/dot/index.js +18 -25
  42. package/dist/functions/dyn/index.d.ts +2 -0
  43. package/dist/functions/dyn/index.js +242 -0
  44. package/dist/functions/grace/index.d.ts +1 -1
  45. package/dist/functions/grace/index.js +17 -11
  46. package/dist/functions/head/index.js +168 -159
  47. package/dist/functions/key/index.js +12 -7
  48. package/dist/functions/meter/index.js +14 -6
  49. package/dist/functions/note/index.js +73 -31
  50. package/dist/functions/note/noteNameFSM.js +52 -63
  51. package/dist/functions/page/index.d.ts +5 -1
  52. package/dist/functions/page/index.js +38 -16
  53. package/dist/functions/program/index.d.ts +2 -0
  54. package/dist/functions/program/index.js +39 -0
  55. package/dist/functions/set/index.js +13 -8
  56. package/dist/functions/stack/index.js +7 -5
  57. package/dist/functions/symbol/index.d.ts +13 -3
  58. package/dist/functions/symbol/index.js +71 -22
  59. package/dist/functions/symbol/shape.d.ts +2 -0
  60. package/dist/functions/symbol/shape.js +14 -10
  61. package/dist/functions/symbol/symbols/dc.d.ts +4 -0
  62. package/dist/functions/symbol/symbols/dc.js +82 -0
  63. package/dist/functions/symbol/symbols/{f.d.ts → ds.d.ts} +1 -1
  64. package/dist/functions/symbol/symbols/ds.js +93 -0
  65. package/dist/functions/symbol/symbols/dynamics.d.ts +3 -0
  66. package/dist/functions/symbol/symbols/dynamics.js +168 -0
  67. package/dist/functions/symbol/symbols/{p.d.ts → fine.d.ts} +1 -1
  68. package/dist/functions/symbol/symbols/fine.js +12 -0
  69. package/dist/functions/symbol/symbols/mordent.js +44 -31
  70. package/dist/functions/symbol/symbols/prall.d.ts +2 -0
  71. package/dist/functions/symbol/symbols/prall.js +42 -0
  72. package/dist/functions/symbol/symbols/segno.d.ts +4 -0
  73. package/dist/functions/symbol/symbols/segno.js +82 -0
  74. package/dist/functions/symbol/symbols/tr.js +0 -4
  75. package/dist/functions/tempo/index.js +12 -4
  76. package/dist/functions/temporal.d.ts +138 -0
  77. package/dist/functions/temporal.js +101 -0
  78. package/dist/functions/text/index.js +25 -10
  79. package/dist/functions/tie/index.js +45 -30
  80. package/dist/functions/tuplet/index.js +21 -8
  81. package/dist/functions/up/index.js +38 -33
  82. package/dist/functions/voice/index.js +153 -68
  83. package/dist/functions/volta/index.js +62 -55
  84. package/dist/iife/core.d.ts +1 -0
  85. package/dist/iife/core.js +3 -0
  86. package/dist/iife/from-midi.d.ts +1 -0
  87. package/dist/iife/from-midi.js +15 -0
  88. package/dist/iife/from-musicxml.d.ts +1 -0
  89. package/dist/iife/from-musicxml.js +3 -0
  90. package/dist/iife/full.d.ts +1 -0
  91. package/dist/iife/full.js +3 -0
  92. package/dist/index.d.ts +3 -21
  93. package/dist/index.js +3 -21
  94. package/dist/jpfun.core.min.js +119 -0
  95. package/dist/jpfun.from-midi.min.js +9 -0
  96. package/dist/jpfun.from-musicxml.min.js +12 -0
  97. package/dist/jpfun.min.js +111 -122
  98. package/dist/layout/default.js +2 -3
  99. package/dist/layout/engine.d.ts +2 -1
  100. package/dist/layout/engine.js +71 -26
  101. package/dist/layout/model.d.ts +4 -4
  102. package/dist/layout/model.js +15 -12
  103. package/dist/layout/page.js +5 -4
  104. package/dist/layout/path.js +3 -3
  105. package/dist/layout/types.d.ts +20 -2
  106. package/dist/lowering/loweringContext.d.ts +3 -1
  107. package/dist/lowering/loweringContext.js +28 -14
  108. package/dist/lowering/track.d.ts +4 -8
  109. package/dist/lowering/track.js +6 -1
  110. package/dist/lowering/types.d.ts +5 -128
  111. package/dist/lowering/types.js +1 -98
  112. package/dist/parser/parse-utils/string-utils.d.ts +2 -0
  113. package/dist/parser/parse-utils/string-utils.js +4 -0
  114. package/dist/parser/parserContext.d.ts +7 -8
  115. package/dist/parser/parserContext.js +41 -36
  116. package/dist/pipeline.d.ts +6 -5
  117. package/dist/pipeline.js +4 -5
  118. package/dist/playback/compile.d.ts +1 -1
  119. package/dist/playback/compile.js +15 -2
  120. package/dist/playback/event.d.ts +17 -3
  121. package/dist/playback/event.js +30 -12
  122. package/dist/playback/time.js +1 -1
  123. package/dist/playback/types.d.ts +5 -4
  124. package/dist/render/text.d.ts +6 -1
  125. package/dist/render/text.js +8 -1
  126. package/grammar.md +111 -57
  127. package/package.json +11 -2
  128. package/dist/functions/symbol/symbols/f.js +0 -55
  129. package/dist/functions/symbol/symbols/p.js +0 -61
@@ -10,11 +10,10 @@ export function createLayoutPrepareContext(functionClasses, options = {}) {
10
10
  const handlers = new Map();
11
11
  const uniqueClasses = new Set(functionClasses);
12
12
  for (const functionClass of uniqueClasses) {
13
- const staticClass = functionClass;
14
- const handler = staticClass.layoutDecorationHandler;
13
+ const handler = functionClass.layoutDecorationHandler;
15
14
  if (!handler)
16
15
  continue;
17
- const names = staticClass.def?.name;
16
+ const names = functionClass.def?.name;
18
17
  if (!names)
19
18
  continue;
20
19
  const primaryName = Array.isArray(names) ? names[0] : names;
@@ -1,5 +1,6 @@
1
1
  import { Diagnostic } from "../diagnostic.js";
2
- import { type LoweringResult, type VisualTemporalNode } from "../lowering/types.js";
2
+ import { type VisualTemporalNode } from "../functions/temporal.js";
3
+ import type { LoweringResult } from "../lowering/types.js";
3
4
  import { type SolverOptions } from "./model.js";
4
5
  import { type DocumentLayoutPage } from "./page.js";
5
6
  import type { LayoutPrepareContext, PlacedAttachment, Rect } from "./types.js";
@@ -1,5 +1,5 @@
1
1
  import { Diagnostic } from "../diagnostic.js";
2
- import { isVisualTemporalNode, } from "../lowering/types.js";
2
+ import { isVisualTemporalNode, } from "../functions/temporal.js";
3
3
  import { completeSpringConfig, layoutElement, layoutHorizontal, } from "./model.js";
4
4
  import { normalizePageConfig, paginateLayoutLines, PageLayoutError, } from "./page.js";
5
5
  import { isLayoutAttachment } from "./types.js";
@@ -87,9 +87,10 @@ export function layoutDocument(result, context, options = {}) {
87
87
  object.prepareHorizontal?.(views[object.layoutLine]);
88
88
  for (const attachment of layoutAttachments)
89
89
  attachment.prepareHorizontal?.(views, context);
90
- for (const line of lines) {
90
+ for (let lineIndex = 0; lineIndex < lines.length; lineIndex++) {
91
+ const line = lines[lineIndex];
91
92
  const elements = line.columns.map(column => column.map(node => layoutElement(node.springConfig, node.box, node, options.globalC)));
92
- layoutHorizontal(elements, contentWidth, options, line.horizontalLayoutHooks);
93
+ layoutHorizontal(elements, contentWidth, options, line.horizontalLayoutHooks, 0.5);
93
94
  for (const column of line.columns) {
94
95
  for (const node of column)
95
96
  node.box.x += originX;
@@ -145,6 +146,7 @@ export function layoutDocument(result, context, options = {}) {
145
146
  width: contentWidth,
146
147
  originX,
147
148
  pages: pages.map(item => item.bounds),
149
+ lines: views,
148
150
  getVisualAxis: visualAxisOf,
149
151
  getHostExtent: (line, track) => lines[line]?.hostExtents.get(track),
150
152
  };
@@ -152,14 +154,14 @@ export function layoutDocument(result, context, options = {}) {
152
154
  };
153
155
  // 3. 首次纵向放置后测量 attachment;只有有效轨道占用扩张时才重新求解
154
156
  let placement = placeVertically();
155
- let measured = measureAttachments(layoutAttachments, placement.attachmentContext);
156
- if (registerAttachmentOccupancy(measured, lines, placement.attachmentContext.getVisualAxis)) {
157
+ let measured = measureAttachments(layoutAttachments, placement.attachmentContext, lines);
158
+ if (measured.needsRelayout) {
157
159
  placement = placeVertically();
158
- measured = measureAttachments(layoutAttachments, placement.attachmentContext);
160
+ measured = measureAttachments(layoutAttachments, placement.attachmentContext, lines);
159
161
  }
160
162
  const pages = placement.pages;
161
163
  const attachments = layoutAttachments.map((attachment, index) => {
162
- const { geometry, box } = measured[index];
164
+ const { geometry, box } = measured.items[index];
163
165
  return {
164
166
  box,
165
167
  regions: geometry.regions,
@@ -222,6 +224,7 @@ function splitLayoutLines(result) {
222
224
  horizontalLayoutHooks: [],
223
225
  hostExtents: new Map(),
224
226
  attachmentExtents: new Map(),
227
+ attachmentRanges: [],
225
228
  });
226
229
  const lines = [];
227
230
  let currentLine = createLine();
@@ -311,12 +314,18 @@ function measureRowGaps(lineCount, objects, override) {
311
314
  return gaps;
312
315
  }
313
316
  /**
314
- * 按 lowering 注册顺序原子生成本轮几何
317
+ * 按 lowering 注册顺序原子生成本轮几何,并同步登记轨道占用
315
318
  *
316
- * 分组的 attachment 总在组内对象之后注册,所以读取依赖不需要额外排序
319
+ * 分组的 attachment 总在组内对象之后注册,所以读取依赖不需要额外排序;
320
+ * 占用逐条累加,后注册者因此能通过 getRangeExtents 避让先注册者。
321
+ * 返回的 needsRelayout 表示本轮占用超出了主体与先前区域的合并范围。
317
322
  */
318
- function measureAttachments(attachments, baseContext) {
323
+ function measureAttachments(attachments, baseContext, lines) {
319
324
  const measured = new Map();
325
+ let needsRelayout = false;
326
+ // 避让基准逐轮重建,避免首轮试排结果污染最终几何;attachmentExtents 反之,见字段注释
327
+ for (const line of lines)
328
+ line.attachmentRanges.length = 0;
320
329
  const context = {
321
330
  ...baseContext,
322
331
  getAttachmentBox(dependency) {
@@ -325,37 +334,73 @@ function measureAttachments(attachments, baseContext) {
325
334
  throw new Error("Layout attachment dependency has not been measured");
326
335
  return resolved.box;
327
336
  },
337
+ getRangeExtents(line, columns) {
338
+ const target = lines[line];
339
+ const extents = new Map();
340
+ if (!target)
341
+ return extents;
342
+ // 主体按列精确选取;先完成的 attachment 再按这些列的最终横向范围相交选取
343
+ const wholeLine = columns === undefined;
344
+ if (columns && columns[0] > columns[1])
345
+ return extents;
346
+ const start = wholeLine ? 0 : Math.max(0, columns[0]);
347
+ const end = wholeLine ? target.columns.length - 1 : Math.min(columns[1], target.columns.length - 1);
348
+ if (!wholeLine && start > end)
349
+ return extents;
350
+ let left = wholeLine ? baseContext.originX : Infinity;
351
+ let right = wholeLine ? baseContext.originX + baseContext.width : -Infinity;
352
+ for (let column = start; column <= end; column++) {
353
+ for (const host of target.columns[column]) {
354
+ if (!wholeLine) {
355
+ if (host.box.x < left)
356
+ left = host.box.x;
357
+ if (host.box.x + host.box.w > right)
358
+ right = host.box.x + host.box.w;
359
+ }
360
+ const hostTop = host.box.y - baseContext.getVisualAxis(line, host.track);
361
+ const hostBottom = hostTop + host.box.h;
362
+ includeTrackExtent(extents, host.track, hostTop, hostBottom);
363
+ }
364
+ }
365
+ for (const range of target.attachmentRanges) {
366
+ if (range.right <= left || range.left >= right)
367
+ continue;
368
+ includeTrackExtent(extents, range.track, range.top, range.bottom);
369
+ }
370
+ return extents;
371
+ },
328
372
  };
329
- return attachments.map(attachment => {
373
+ const items = attachments.map(attachment => {
330
374
  const geometry = attachment.createGeometry(context);
331
375
  const box = { x: 0, y: 0, w: 0, h: 0 };
332
376
  unionLayoutBoxes(box, geometry.regions);
333
377
  const item = { geometry, box };
334
378
  measured.set(attachment, item);
335
- return item;
336
- });
337
- }
338
- /** 把首轮几何折算成轨道占用,返回它是否超出了主体与先前区域的合并占用 */
339
- function registerAttachmentOccupancy(measured, lines, visualAxisOf) {
340
- let needsRelayout = false;
341
- for (const { geometry } of measured) {
379
+ // 当前项立即登记,保证后注册的 attachment 能看到并避让它
342
380
  for (const region of geometry.occupancy ?? geometry.regions) {
343
381
  if (region.line === void 0)
344
382
  continue;
345
383
  const line = lines[region.line];
346
- const axis = visualAxisOf(region.line, region.track);
347
- const top = region.y - axis;
348
- const bottom = region.y + region.h - axis;
384
+ const top = region.y - baseContext.getVisualAxis(region.line, region.track);
385
+ const bottom = top + region.h;
349
386
  const hostExtent = line.hostExtents.get(region.track);
350
387
  const attachmentExtent = line.attachmentExtents.get(region.track);
351
- const previousTop = Math.min(hostExtent?.top ?? Infinity, attachmentExtent?.top ?? Infinity);
352
- const previousBottom = Math.max(hostExtent?.bottom ?? -Infinity, attachmentExtent?.bottom ?? -Infinity);
353
- if (top < previousTop || bottom > previousBottom)
388
+ if (top < Math.min(hostExtent?.top ?? Infinity, attachmentExtent?.top ?? Infinity)
389
+ || bottom > Math.max(hostExtent?.bottom ?? -Infinity, attachmentExtent?.bottom ?? -Infinity)) {
354
390
  needsRelayout = true;
391
+ }
355
392
  includeTrackExtent(line.attachmentExtents, region.track, top, bottom);
393
+ line.attachmentRanges.push({
394
+ track: region.track,
395
+ left: region.x,
396
+ right: region.x + region.w,
397
+ top,
398
+ bottom,
399
+ });
356
400
  }
357
- }
358
- return needsRelayout;
401
+ return item;
402
+ });
403
+ return { items, needsRelayout };
359
404
  }
360
405
  /**
361
406
  * 沿 Track 树自内向外求出一条谱面行里所有音轨的纵向轴
@@ -1,5 +1,5 @@
1
1
  /**
2
- * 基于弹簧的有时长物体排版模型 原理参考 docs/layout.md
2
+ * 基于弹簧的有时长物体排版模型 原理参考 apps/docs/src/content/docs/docs/developer/layout.md
3
3
  * 基本使用: 借助 layoutElement() 构建 LayoutElement[][],调用 layout() 函数进行排版
4
4
  */
5
5
  import type { HorizontalSpringConfig, LayoutBox, TimeLineEvent } from "./types.js";
@@ -65,9 +65,9 @@ export interface HorizontalLayoutHookEntry {
65
65
  * @param fixed 当前区间的 gap 是否已经固定,长度必须为 X.length - 1;通常是全行 fixed 的 subarray。fixed[i]==1 表示第 i、i+1 列只能整体移动。既是输入也是输出
66
66
  * @param limit 当前区间左右墙之间的可用宽度
67
67
  * @param options 已补齐默认值的求解参数
68
- * @param fill 空间有余量时,是否让首尾贴墙并把余量平均分到自由间隙
68
+ * @param fillMinRatio 自然宽度至少占可用宽度的比例时才 fill;省略表示不 fill,0 表示无条件 fill。fill 表示是否精确将左右拉到可用宽度,包括宽度不足和弹簧导致的溢出
69
69
  */
70
- export declare function layoutHorizontalRegion(columns: LayoutElement[][], rows: number, X: Float64Array, fixed: Uint8Array, limit: number, options: Required<SolverOptions>, fill?: boolean): void;
70
+ export declare function layoutHorizontalRegion(columns: LayoutElement[][], rows: number, X: Float64Array, fixed: Uint8Array, limit: number, options: Required<SolverOptions>, fillMinRatio?: number): void;
71
71
  /**
72
72
  * 纯扁平化物理排版解算入口
73
73
  *
@@ -76,5 +76,5 @@ export declare function layoutHorizontalRegion(columns: LayoutElement[][], rows:
76
76
  * @param options 排版物理配置
77
77
  * @returns 同一个解算完成的二维矩阵,所有盒子的绝对 box.x (左侧x坐标) 已就地更新
78
78
  */
79
- export declare function layoutHorizontal(columns: LayoutElement[][], limit: number, options?: SolverOptions, horizontalLayoutHooks?: readonly HorizontalLayoutHookEntry[]): LayoutElement[][];
79
+ export declare function layoutHorizontal(columns: LayoutElement[][], limit: number, options?: SolverOptions, horizontalLayoutHooks?: readonly HorizontalLayoutHookEntry[], fillMinRatio?: number): LayoutElement[][];
80
80
  export {};
@@ -313,9 +313,9 @@ function solveHorizontal(mat, rows, X, limit, options) {
313
313
  * @param fixed 当前区间的 gap 是否已经固定,长度必须为 X.length - 1;通常是全行 fixed 的 subarray。fixed[i]==1 表示第 i、i+1 列只能整体移动。既是输入也是输出
314
314
  * @param limit 当前区间左右墙之间的可用宽度
315
315
  * @param options 已补齐默认值的求解参数
316
- * @param fill 空间有余量时,是否让首尾贴墙并把余量平均分到自由间隙
316
+ * @param fillMinRatio 自然宽度至少占可用宽度的比例时才 fill;省略表示不 fill,0 表示无条件 fill。fill 表示是否精确将左右拉到可用宽度,包括宽度不足和弹簧导致的溢出
317
317
  */
318
- export function layoutHorizontalRegion(columns, rows, X, fixed, limit, options, fill = false) {
318
+ export function layoutHorizontalRegion(columns, rows, X, fixed, limit, options, fillMinRatio) {
319
319
  if (X.length !== columns.length || fixed.length !== Math.max(0, X.length - 1)) {
320
320
  throw new Error("Horizontal region state does not match its columns");
321
321
  }
@@ -374,20 +374,23 @@ export function layoutHorizontalRegion(columns, rows, X, fixed, limit, options,
374
374
  }
375
375
  const hasRoom = solveHorizontal(solvedColumns, rows, solvedX, limit, options);
376
376
  fixed.fill(1);
377
- if (fill && hasRoom) {
378
- // 让首尾贴墙,并把余量平均分到自由间隙
377
+ if (fillMinRatio !== void 0) {
379
378
  let leftWidth = 0;
380
379
  let rightWidth = 0;
381
380
  for (let row = 0; row < rows; row++) {
382
381
  leftWidth = Math.max(leftWidth, solvedColumns[0][row].WL);
383
382
  rightWidth = Math.max(rightWidth, solvedColumns[solvedColumns.length - 1][row].WR);
384
383
  }
385
- const shift = leftWidth - solvedX[0];
386
- const extra = limit - rightWidth - solvedX[solvedX.length - 1] - shift;
387
- for (let i = 0; i < solvedX.length; i++) {
388
- solvedX[i] += shift + (extra > 0 && solvedX.length > 1
389
- ? extra * i / (solvedX.length - 1)
390
- : 0);
384
+ const naturalWidth = solvedX[solvedX.length - 1] + rightWidth - solvedX[0] + leftWidth;
385
+ if (!hasRoom || naturalWidth >= limit * fillMinRatio) {
386
+ // 让首尾贴墙,并把余量平均分到自由间隙
387
+ const shift = leftWidth - solvedX[0];
388
+ const extra = limit - naturalWidth;
389
+ for (let i = 0; i < solvedX.length; i++) {
390
+ solvedX[i] += shift + (solvedX.length > 1
391
+ ? extra * i / (solvedX.length - 1)
392
+ : 0);
393
+ }
391
394
  }
392
395
  }
393
396
  if (hasFixed) { // 把虚拟列的求解结果写回原始列
@@ -403,7 +406,7 @@ export function layoutHorizontalRegion(columns, rows, X, fixed, limit, options,
403
406
  * @param options 排版物理配置
404
407
  * @returns 同一个解算完成的二维矩阵,所有盒子的绝对 box.x (左侧x坐标) 已就地更新
405
408
  */
406
- export function layoutHorizontal(columns, limit, options = {}, horizontalLayoutHooks = []) {
409
+ export function layoutHorizontal(columns, limit, options = {}, horizontalLayoutHooks = [], fillMinRatio) {
407
410
  const normalizedOptions = normalizeSolverOptions(options);
408
411
  const { mat, rows } = fillPlaceholders(columns, normalizedOptions.globalC);
409
412
  const X = new Float64Array(mat.length);
@@ -422,7 +425,7 @@ export function layoutHorizontal(columns, limit, options = {}, horizontalLayoutH
422
425
  options: normalizedOptions,
423
426
  });
424
427
  }
425
- layoutHorizontalRegion(mat, rows, X, fixed, limit, normalizedOptions);
428
+ layoutHorizontalRegion(mat, rows, X, fixed, limit, normalizedOptions, fillMinRatio);
426
429
  // 坐标写回
427
430
  for (let c = 0; c < mat.length; c++) {
428
431
  for (const el of mat[c])
@@ -73,12 +73,13 @@ export function paginateLayoutLines(heights, page) {
73
73
  // used 含最小行距,用于判断下一行能否装入;contentHeight 不含行距,用于封页后拉伸
74
74
  let used = 0;
75
75
  let contentHeight = 0;
76
- /** 写出 [start, end) 这一页;完整页拉伸行距,末页保留最小行距 */
76
+ /** 写出 [start, end) 这一页;完整页把剩余高度均分到每行下方,末页保留最小行距 */
77
77
  const closePage = (end, justify) => {
78
78
  const count = end - start;
79
- const gap = justify && count > 1
80
- ? (availableHeight - contentHeight) / (count - 1)
81
- : page.lineGap;
79
+ const remaining = justify
80
+ ? Math.max(0, availableHeight - contentHeight - page.lineGap * (count - 1))
81
+ : 0;
82
+ const gap = page.lineGap + remaining / count;
82
83
  let lineTop = pageTop + page.marginTop;
83
84
  for (let i = start; i < end; i++) {
84
85
  lineTops[i] = lineTop;
@@ -8,7 +8,7 @@
8
8
  * 分页路由只需要一个保守超集,与这里求真实极值的目标相反,不要合并。
9
9
  */
10
10
  /** 一维上的极值累加器 */
11
- class Extent {
11
+ class MinMax {
12
12
  min = Infinity;
13
13
  max = -Infinity;
14
14
  add(value) {
@@ -64,8 +64,8 @@ function quadraticExtrema(p0, p1, p2, extent) {
64
64
  * 33% 且墨迹不居中),所以两边必须共用同一份判据。
65
65
  */
66
66
  export function pathBounds(commands) {
67
- const x = new Extent();
68
- const y = new Extent();
67
+ const x = new MinMax();
68
+ const y = new MinMax();
69
69
  let cursorX = 0;
70
70
  let cursorY = 0;
71
71
  let startX = 0;
@@ -1,5 +1,5 @@
1
1
  import type { Painter, TextMeasurer } from "../render/types.js";
2
- import type { Extent, Track } from "../lowering/track.js";
2
+ import type { Track } from "../lowering/track.js";
3
3
  import type { Fraction } from "../fraction.js";
4
4
  import type { SourceSpan } from "../parser/types.js";
5
5
  import type { HorizontalLayoutHook } from "./model.js";
@@ -11,6 +11,14 @@ export interface Rect {
11
11
  w: number;
12
12
  h: number;
13
13
  }
14
+ /**
15
+ * 相对某条基线的纵向占用
16
+ * 约定 top <= 0 <= bottom
17
+ */
18
+ export interface Extent {
19
+ top: number;
20
+ bottom: number;
21
+ }
14
22
  /**
15
23
  * 一个排版对象的完整矩形和两个对齐基准
16
24
  *
@@ -63,9 +71,19 @@ export interface AttachmentLayoutContext extends LayoutPrepareContext {
63
71
  width: number;
64
72
  originX: number;
65
73
  pages: readonly Rect[];
74
+ lines: readonly Omit<HorizontalLineView, "registerHorizontalLayoutHook">[];
66
75
  getVisualAxis(line: number, track: Track): number;
67
76
  /** 只包含可见主体的轴局部占用(top 通常为负),不受 attachment 或最终分页坐标影响 */
68
77
  getHostExtent(line: number, track: Track): Readonly<Extent> | undefined;
78
+ /**
79
+ * 闭区间列 [from, to] 内按轨分组的已定占用
80
+ *
81
+ * 主体按时间列精确筛选;attachment 按这些列最终盒子的横向范围筛选。
82
+ * attachment 部分与 getAttachmentBox 同一条可见性规则:只看得见比自己先注册的,
83
+ * 也就是「后声明的排在外层」。避让型 attachment 用它把自己排到已有内容之外。
84
+ * 省略列号表示整行,供跨行 attachment 查询没有可见列的中间行。
85
+ */
86
+ getRangeExtents(line: number, columns?: readonly [from: number, to: number]): ReadonlyMap<Track, Readonly<Extent>>;
69
87
  /** 读取本轮已完成的 attachment 边界;分组在 endLoweringGroup 才注册,因而组内对象必然排在分组之前 */
70
88
  getAttachmentBox(attachment: LayoutAttachment): Readonly<Rect>;
71
89
  }
@@ -89,7 +107,7 @@ export interface LayoutHost extends TimeLineEvent {
89
107
  };
90
108
  }
91
109
  /**
92
- * 一条谱面行进入横向求解前的只读视图
110
+ * 一条谱面行的横向拓扑与求解扩展点
93
111
  *
94
112
  * 时间列拓扑不可改变,但其中的 host 仍可写;
95
113
  * 调用方可以调整 springConfig,也可以注册在 LayoutElement 归一化后执行的横向布局
@@ -2,7 +2,8 @@ import { Diagnostic } from "../diagnostic.js";
2
2
  import { Fraction } from "../fraction.js";
3
3
  import { ASTFunctionClass, ASTNodeBase } from "../functions/ASTtypes.js";
4
4
  import type { PageConfig } from "../layout/types.js";
5
- import { TemporalNodeBase, type LoweringGroup, type LoweringAttachment, type LoweringResult } from "./types.js";
5
+ import { TemporalNodeBase } from "../functions/temporal.js";
6
+ import type { LoweringGroup, LoweringAttachment, LoweringResult } from "./types.js";
6
7
  import { Track } from "./track.js";
7
8
  /**
8
9
  * 把 AST 展开并固化为时间列、attachment 和页面配置
@@ -111,6 +112,7 @@ export declare class LoweringContext {
111
112
  static anchorAlign(tracks: TimeColumn[][]): TimeColumn[];
112
113
  }
113
114
  declare class TimeColumn extends Array<TemporalNodeBase> {
115
+ static get [Symbol.species](): ArrayConstructor;
114
116
  readonly mergeKey: number;
115
117
  constructor(n: TemporalNodeBase);
116
118
  get t(): Fraction;
@@ -1,6 +1,6 @@
1
1
  import { Diagnostic } from "../diagnostic.js";
2
2
  import { Fraction } from "../fraction.js";
3
- import { ANCHOR_KEY, DEFAULT_BPM, DEFAULT_TONALITY, DEFAULT_VELOCITY, } from "./types.js";
3
+ import { ANCHOR_KEY, DEFAULT_BPM, DEFAULT_PROGRAM, DEFAULT_TONALITY, DEFAULT_VELOCITY, } from "../functions/temporal.js";
4
4
  import { Track } from "./track.js";
5
5
  /**
6
6
  * 把 AST 展开并固化为时间列、attachment 和页面配置
@@ -48,8 +48,7 @@ export class LoweringContext {
48
48
  registerFunctions(functionClasses) {
49
49
  const augmenters = this.loweringAugmenters = [];
50
50
  const finalizers = this.loweringFinalizers = [];
51
- for (const cls of functionClasses) {
52
- const functionClass = cls;
51
+ for (const functionClass of functionClasses) {
53
52
  if (functionClass.loweringAugment)
54
53
  augmenters.push(functionClass.loweringAugment);
55
54
  if (functionClass.loweringFinalize)
@@ -109,10 +108,21 @@ export class LoweringContext {
109
108
  bpm: DEFAULT_BPM,
110
109
  velocity: DEFAULT_VELOCITY,
111
110
  keySignature: DEFAULT_TONALITY,
111
+ program: DEFAULT_PROGRAM,
112
112
  };
113
+ // 力度和音色按音轨各自流动,新分叉的声部继承分叉处父轨的值
114
+ const velocities = new Map();
115
+ const programs = new Map();
116
+ const velocityOf = (track) => velocities.get(track) ?? (track.parent ? velocityOf(track.parent) : DEFAULT_VELOCITY);
117
+ const programOf = (track) => programs.get(track) ?? (track.parent ? programOf(track.parent) : DEFAULT_PROGRAM);
113
118
  for (const column of columns) {
114
- for (const node of column)
119
+ for (const node of column) {
120
+ state.velocity = velocityOf(node.track);
121
+ state.program = programOf(node.track);
115
122
  node.onTimeState?.(state);
123
+ velocities.set(node.track, state.velocity);
124
+ programs.set(node.track, state.program);
125
+ }
116
126
  }
117
127
  }
118
128
  /**
@@ -204,7 +214,7 @@ export class LoweringContext {
204
214
  const columns = [];
205
215
  this.appendEvents(node.loweringEnter(this, track), node, timeOffset, track, columns);
206
216
  // 处理子元素
207
- const model = node.timeFlowModel();
217
+ const model = node.timeFlowModel(this);
208
218
  if (model) {
209
219
  switch (model.mode) {
210
220
  case "sequence":
@@ -238,16 +248,17 @@ export class LoweringContext {
238
248
  branches.push(branch);
239
249
  }
240
250
  // 归并 局部归并以限制对齐作用域
251
+ const mergeStart = columns.length;
241
252
  for (const column of LoweringContext.anchorAlign(branches))
242
253
  columns.push(column);
243
- const lastCol = columns[columns.length - 1];
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;
254
+ // 最长的事件未必落在最后一列,所以要扫全部合并列取最晚终点
255
+ const eventEnd = new Fraction();
256
+ for (let i = mergeStart; i < columns.length; i++) {
257
+ for (const node of columns[i]) {
258
+ eventEnd.copyFrom(node.t).add(node.T);
259
+ if (eventEnd.compare(timeOffset) > 0)
260
+ timeOffset.copyFrom(eventEnd);
249
261
  }
250
- timeOffset.copyFrom(lastCol.t).add(maxDuration);
251
262
  }
252
263
  }
253
264
  break;
@@ -295,13 +306,14 @@ export class LoweringContext {
295
306
  tracks.splice(i, 1);
296
307
  }
297
308
  const l = tracks.length;
309
+ // 分支全空;调用方已经逐条报过 ZeroTimeTrack
298
310
  if (l === 0)
299
- throw new Error("No tracks to align");
311
+ return [];
300
312
  if (l === 1)
301
313
  return tracks[0];
302
314
  const result = [];
303
315
  if (l === 2) {
304
- // 归并两个轨道
316
+ // 双轨特化:与下面的堆归并语义等价,改规则必须两边同时改
305
317
  const track0 = tracks[0], track1 = tracks[1];
306
318
  const l0 = track0.length, l1 = track1.length;
307
319
  let i = 0, j = 0;
@@ -529,6 +541,8 @@ export class LoweringContext {
529
541
  }
530
542
  }
531
543
  class TimeColumn extends Array {
544
+ // map/filter/slice 默认会用本类重建,而构造器会把长度当事件用;统一退回普通数组
545
+ static get [Symbol.species]() { return Array; }
532
546
  mergeKey;
533
547
  constructor(n) {
534
548
  super(1);
@@ -1,11 +1,4 @@
1
- /**
2
- * 相对某条基线的纵向占用
3
- * 约定 top <= 0 <= bottom
4
- */
5
- export interface Extent {
6
- top: number;
7
- bottom: number;
8
- }
1
+ import type { Extent } from "../layout/types.js";
9
2
  /** measure 给出的单个成员的局部位置 */
10
3
  export interface TrackPlacement {
11
4
  /**
@@ -66,11 +59,14 @@ export interface TrackGroup {
66
59
  * 两次出现要不要共用基线,完全由申请音轨时给出的 laneKey 决定(见 group)
67
60
  */
68
61
  export declare class Track {
62
+ readonly parent: Track | null;
69
63
  /**
70
64
  * 挂在本轨上的分组;无需宿主定位的先合并,其余按声明顺序向外放置
71
65
  * 一个 group 理解为时间线上的一次分叉,但分叉模式相同会直接复用
72
66
  */
73
67
  readonly groups: TrackGroup[];
68
+ /** 本轨从哪条基线分叉而来,根轨为 null;新声部据此继承力度这类持续状态 */
69
+ constructor(parent?: Track | null);
74
70
  /**
75
71
  * 申请一组分支音轨
76
72
  *
@@ -8,11 +8,16 @@
8
8
  * 两次出现要不要共用基线,完全由申请音轨时给出的 laneKey 决定(见 group)
9
9
  */
10
10
  export class Track {
11
+ parent;
11
12
  /**
12
13
  * 挂在本轨上的分组;无需宿主定位的先合并,其余按声明顺序向外放置
13
14
  * 一个 group 理解为时间线上的一次分叉,但分叉模式相同会直接复用
14
15
  */
15
16
  groups = [];
17
+ /** 本轨从哪条基线分叉而来,根轨为 null;新声部据此继承力度这类持续状态 */
18
+ constructor(parent = null) {
19
+ this.parent = parent;
20
+ }
16
21
  /**
17
22
  * 申请一组分支音轨
18
23
  *
@@ -29,7 +34,7 @@ export class Track {
29
34
  this.groups.push(group);
30
35
  }
31
36
  while (group.members.length < count)
32
- group.members.push(new Track());
37
+ group.members.push(new Track(this));
33
38
  return group;
34
39
  }
35
40
  }