jpfun 0.7.0 → 0.9.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 (152) hide show
  1. package/Bravura-LICENSE.txt +93 -0
  2. package/README.md +49 -146
  3. package/dist/Bravura-LICENSE.txt +93 -0
  4. package/dist/converter/midi/convert.d.ts +0 -10
  5. package/dist/converter/midi/convert.js +19 -56
  6. package/dist/converter/musicxml/features.d.ts +16 -8
  7. package/dist/converter/musicxml/features.js +88 -19
  8. package/dist/converter/musicxml/index.js +88 -50
  9. package/dist/converter/musicxml/model.d.ts +9 -0
  10. package/dist/converter/source.d.ts +4 -7
  11. package/dist/converter/source.js +11 -9
  12. package/dist/core.d.ts +1 -0
  13. package/dist/core.js +1 -0
  14. package/dist/diagnostic.d.ts +7 -1
  15. package/dist/diagnostic.js +40 -88
  16. package/dist/fraction.js +3 -2
  17. package/dist/functions/ASTtypes.d.ts +3 -32
  18. package/dist/functions/ASTtypes.js +5 -52
  19. package/dist/functions/adjust/index.js +45 -33
  20. package/dist/functions/arpeggio/index.js +50 -55
  21. package/dist/functions/arpeggio/shape.d.ts +2 -3
  22. package/dist/functions/arpeggio/shape.js +20 -50
  23. package/dist/functions/bar/index.d.ts +8 -0
  24. package/dist/functions/bar/index.js +105 -8
  25. package/dist/functions/beam/auto.d.ts +1 -2
  26. package/dist/functions/beam/auto.js +80 -135
  27. package/dist/functions/beam/index.js +6 -12
  28. package/dist/functions/beam/layout.d.ts +1 -1
  29. package/dist/functions/beam/layout.js +1 -1
  30. package/dist/functions/box/index.js +0 -6
  31. package/dist/functions/br/index.d.ts +34 -2
  32. package/dist/functions/br/index.js +5 -4
  33. package/dist/functions/dash/index.js +9 -10
  34. package/dist/functions/default.js +2 -1
  35. package/dist/functions/div/index.d.ts +3 -4
  36. package/dist/functions/div/index.js +18 -14
  37. package/dist/functions/dot/index.js +5 -11
  38. package/dist/functions/dyn/index.js +55 -57
  39. package/dist/functions/gliss/index.js +131 -103
  40. package/dist/functions/grace/index.d.ts +3 -12
  41. package/dist/functions/grace/index.js +52 -102
  42. package/dist/functions/head/index.js +10 -7
  43. package/dist/functions/key/index.js +13 -15
  44. package/dist/functions/meter/index.d.ts +56 -1
  45. package/dist/functions/meter/index.js +15 -10
  46. package/dist/functions/note/index.js +6 -6
  47. package/dist/functions/note/noteName.d.ts +16 -0
  48. package/dist/functions/note/noteName.js +88 -0
  49. package/dist/functions/note/source-transform.js +8 -10
  50. package/dist/functions/ottava/index.js +35 -40
  51. package/dist/functions/page/index.d.ts +1 -0
  52. package/dist/functions/page/index.js +27 -21
  53. package/dist/functions/pagebreak/index.js +2 -2
  54. package/dist/functions/rest/index.js +5 -5
  55. package/dist/functions/set/index.js +4 -4
  56. package/dist/functions/slide/index.js +0 -1
  57. package/dist/functions/stack/index.js +8 -57
  58. package/dist/functions/symbol/index.d.ts +21 -2
  59. package/dist/functions/symbol/index.js +69 -24
  60. package/dist/functions/symbol/ornament.d.ts +1 -2
  61. package/dist/functions/symbol/shape.d.ts +6 -0
  62. package/dist/functions/symbol/shape.js +16 -3
  63. package/dist/functions/symbol/symbols/coda.d.ts +4 -0
  64. package/dist/functions/symbol/symbols/coda.js +56 -0
  65. package/dist/functions/symbol/symbols/dynamics.d.ts +1 -1
  66. package/dist/functions/symbol/symbols/dynamics.js +18 -8
  67. package/dist/functions/symbol/symbols/harmonic.d.ts +2 -0
  68. package/dist/functions/symbol/symbols/harmonic.js +11 -0
  69. package/dist/functions/symbol/symbols/marcato.d.ts +2 -0
  70. package/dist/functions/symbol/symbols/marcato.js +22 -0
  71. package/dist/functions/symbol/symbols/staccato.d.ts +2 -0
  72. package/dist/functions/symbol/symbols/staccato.js +24 -0
  73. package/dist/functions/symbol/symbols/tenuto.d.ts +2 -0
  74. package/dist/functions/symbol/symbols/tenuto.js +11 -0
  75. package/dist/functions/symbol/symbols/tocoda.d.ts +2 -0
  76. package/dist/functions/symbol/symbols/tocoda.js +16 -0
  77. package/dist/functions/symbol/symbols/turn.d.ts +3 -0
  78. package/dist/functions/symbol/symbols/turn.js +50 -0
  79. package/dist/functions/symbol/symbols/wheel.d.ts +2 -0
  80. package/dist/functions/symbol/symbols/wheel.js +36 -0
  81. package/dist/functions/temporal.d.ts +3 -2
  82. package/dist/functions/temporal.js +8 -0
  83. package/dist/functions/text/index.js +38 -35
  84. package/dist/functions/tie/index.js +44 -43
  85. package/dist/functions/tremolo/index.d.ts +2 -0
  86. package/dist/functions/tremolo/index.js +318 -0
  87. package/dist/functions/tuplet/index.js +4 -14
  88. package/dist/functions/up/index.js +81 -85
  89. package/dist/functions/voice/connections.d.ts +2 -1
  90. package/dist/functions/voice/connections.js +48 -10
  91. package/dist/functions/voice/index.d.ts +2 -3
  92. package/dist/functions/voice/index.js +2 -963
  93. package/dist/functions/voice/lyrics.d.ts +30 -0
  94. package/dist/functions/voice/lyrics.js +289 -0
  95. package/dist/functions/voice/voice.d.ts +63 -0
  96. package/dist/functions/voice/voice.js +373 -0
  97. package/dist/functions/voice/voices.d.ts +106 -0
  98. package/dist/functions/voice/voices.js +423 -0
  99. package/dist/functions/volta/index.js +5 -23
  100. package/dist/jpfun.core.min.js +76 -59
  101. package/dist/jpfun.from-midi.min.js +5 -5
  102. package/dist/jpfun.from-musicxml.min.js +8 -8
  103. package/dist/jpfun.min.js +81 -64
  104. package/dist/layout/default.d.ts +0 -1
  105. package/dist/layout/default.js +0 -1
  106. package/dist/layout/engine.js +30 -18
  107. package/dist/layout/model.d.ts +0 -1
  108. package/dist/layout/model.js +1 -4
  109. package/dist/layout/page.d.ts +2 -1
  110. package/dist/layout/page.js +14 -14
  111. package/dist/layout/range.d.ts +7 -6
  112. package/dist/layout/range.js +23 -14
  113. package/dist/layout/types.d.ts +9 -28
  114. package/dist/lowering/loweringContext.d.ts +18 -23
  115. package/dist/lowering/loweringContext.js +33 -46
  116. package/dist/lowering/track.d.ts +8 -1
  117. package/dist/lowering/types.d.ts +13 -11
  118. package/dist/parser/parse-utils/call-utils.d.ts +1 -2
  119. package/dist/parser/parse-utils/call-utils.js +6 -34
  120. package/dist/parser/parse-utils/note-utils.d.ts +3 -3
  121. package/dist/parser/parse-utils/note-utils.js +21 -33
  122. package/dist/parser/parse-utils/string-utils.d.ts +3 -2
  123. package/dist/parser/parse-utils/string-utils.js +20 -5
  124. package/dist/parser/parserContext.d.ts +9 -9
  125. package/dist/parser/parserContext.js +62 -11
  126. package/dist/pipeline.js +2 -3
  127. package/dist/playback/compile.js +131 -82
  128. package/dist/playback/event.d.ts +6 -6
  129. package/dist/playback/event.js +13 -49
  130. package/dist/playback/time.js +42 -31
  131. package/dist/playback/types.d.ts +3 -1
  132. package/dist/render/canvas.js +4 -22
  133. package/dist/render/recording.js +1 -4
  134. package/dist/render/svg.d.ts +0 -1
  135. package/dist/render/svg.js +33 -31
  136. package/dist/render/text.d.ts +36 -1
  137. package/dist/render/text.js +134 -0
  138. package/dist/render/translate.d.ts +0 -1
  139. package/dist/render/translate.js +6 -23
  140. package/dist/render/types.d.ts +0 -2
  141. package/grammar.md +39 -30
  142. package/package.json +3 -3
  143. package/dist/functions/note/noteNameFSM.d.ts +0 -8
  144. package/dist/functions/note/noteNameFSM.js +0 -181
  145. package/dist/iife/core.d.ts +0 -1
  146. package/dist/iife/core.js +0 -3
  147. package/dist/iife/from-midi.d.ts +0 -1
  148. package/dist/iife/from-midi.js +0 -15
  149. package/dist/iife/from-musicxml.d.ts +0 -1
  150. package/dist/iife/from-musicxml.js +0 -3
  151. package/dist/iife/full.d.ts +0 -1
  152. package/dist/iife/full.js +0 -3
@@ -4,7 +4,6 @@ import type { LayoutPrepareContext } from "./types.js";
4
4
  /**
5
5
  * 从函数类声明创建排版上下文
6
6
  *
7
- * 与 LoweringContext.registerFunctions 使用相同的注册思想
8
7
  * 新增装饰函数只修改自己的类,不修改 layout 引擎
9
8
  */
10
9
  export declare function createLayoutPrepareContext(functionClasses: Iterable<ASTFunctionClass>, options?: {
@@ -3,7 +3,6 @@ import { defaultTextMeasurer } from "../render/text.js";
3
3
  /**
4
4
  * 从函数类声明创建排版上下文
5
5
  *
6
- * 与 LoweringContext.registerFunctions 使用相同的注册思想
7
6
  * 新增装饰函数只修改自己的类,不修改 layout 引擎
8
7
  */
9
8
  export function createLayoutPrepareContext(functionClasses, options = {}) {
@@ -59,7 +59,7 @@ export function unionLayoutBoxes(target, boxes) {
59
59
  * 从而保证子对象与顶层对象经过完全一致的准备流程。
60
60
  */
61
61
  export function prepareLayoutHost(node, context) {
62
- node.springConfig ??= {};
62
+ node.springConfig = {};
63
63
  if (node.decorations)
64
64
  decorationExtents.delete(node);
65
65
  node.decorations = [];
@@ -179,15 +179,23 @@ export function layoutDocument(result, context, options = {}) {
179
179
  /** 按当前主体与 attachment 占用求轴、分页并放置所有主体 */
180
180
  const placeVertically = () => {
181
181
  const heights = [];
182
+ const headrooms = [];
182
183
  const axes = lines.map((line, i) => {
183
184
  const solved = solveVerticalAxes(line, result.rootTrack, rowGaps[i]);
184
185
  heights.push(solved.height);
186
+ let top = Infinity;
187
+ for (const column of line.columns) {
188
+ for (const node of column) {
189
+ top = Math.min(top, (solved.axes.get(node.track) ?? 0) - node.ast.size / 2);
190
+ }
191
+ }
192
+ headrooms.push(top < Infinity ? top : 0);
185
193
  return solved.axes;
186
194
  });
187
195
  let pages;
188
196
  let lineTops;
189
197
  try {
190
- ({ pages, lineTops } = paginateLayoutLines(heights, page, pageBreaks));
198
+ ({ pages, lineTops } = paginateLayoutLines(heights, page, pageBreaks, headrooms));
191
199
  }
192
200
  catch (error) {
193
201
  if (!(error instanceof PageLayoutError))
@@ -230,15 +238,14 @@ export function layoutDocument(result, context, options = {}) {
230
238
  let measured = measureAttachments(documentAttachments, placement.attachmentContext, lines, lift, subdomainGeometry, hosts);
231
239
  if (measured.needsRelayout) {
232
240
  placement = placeVertically();
233
- measured = measureAttachments(documentAttachments, placement.attachmentContext, lines, lift, subdomainGeometry, hosts);
241
+ measured = measureAttachmentPass(documentAttachments, placement.attachmentContext, lines, lift, subdomainGeometry, false);
234
242
  }
235
243
  const pages = placement.pages;
236
244
  let next = 0;
237
245
  const attachments = layoutAttachments.map(attachment => {
238
246
  const item = subdomainItems.get(attachment);
239
- const { geometry, box } = item ? moveSubdomainItem(item) : measured.items[next++];
247
+ const { geometry } = item ? moveSubdomainItem(item) : measured.items[next++];
240
248
  return {
241
- box,
242
249
  regions: geometry.regions,
243
250
  layer: attachment.layer,
244
251
  sourceSpan: attachment.sourceSpan,
@@ -256,7 +263,7 @@ export function layoutDocument(result, context, options = {}) {
256
263
  for (const node of objects)
257
264
  yield getLayoutBounds(node);
258
265
  for (const attachment of attachments)
259
- yield attachment.box;
266
+ yield* attachment.regions;
260
267
  }
261
268
  unionLayoutBoxes(bounds, layoutBoxes());
262
269
  return {
@@ -540,14 +547,13 @@ function encloses(owner, node) {
540
547
  /** 子域是刚性块:按参照成员的位移平移块内测得的几何 */
541
548
  function moveSubdomainItem(item, layoutOnly = false) {
542
549
  const measured = layoutOnly ? item.layout ?? item : item;
543
- const { position, geometry, box } = measured;
550
+ const { position, geometry } = measured;
544
551
  const dx = item.reference.box.x - position.x;
545
552
  const dy = item.reference.box.y - position.y;
546
553
  if (dx === 0 && dy === 0)
547
554
  return measured;
548
555
  const move = (rect) => ({ ...rect, x: rect.x + dx, y: rect.y + dy });
549
556
  return {
550
- box: move(box),
551
557
  geometry: {
552
558
  regions: geometry.regions.map(move),
553
559
  occupancy: geometry.occupancy?.map(move),
@@ -654,17 +660,18 @@ function measureAttachmentPass(attachments, baseContext, lines, lift, measuredEl
654
660
  extent = { top, bottom };
655
661
  };
656
662
  // 主体和附件共用范围筛选,仅累加结果的容器随查询模式变化。
657
- const { start, end, wholeLine } = resolveRange(view, range && !("left" in range) ? range : undefined, track, lift);
663
+ const elementRange = range && !("left" in range) ? range : undefined;
664
+ const { start, end } = resolveRange(view, elementRange);
658
665
  const target = track && lift(track);
659
- let left = horizontal?.left ?? (wholeLine ? baseContext.originX : Infinity);
660
- let right = horizontal?.right ?? (wholeLine ? baseContext.originX + baseContext.width : -Infinity);
666
+ let left = horizontal?.left ?? (elementRange ? Infinity : baseContext.originX);
667
+ let right = horizontal?.right ?? (elementRange ? -Infinity : baseContext.originX + baseContext.width);
661
668
  for (let column = start; column <= end; column++) {
662
669
  for (const host of view.columns[column]) {
663
670
  if (target && host.track !== target)
664
671
  continue;
665
672
  if (horizontal && (host.box.x + host.box.w <= left || host.box.x >= right))
666
673
  continue;
667
- if (!wholeLine) {
674
+ if (elementRange) {
668
675
  if (host.box.x < left)
669
676
  left = host.box.x;
670
677
  if (host.box.x + host.box.w > right)
@@ -697,6 +704,10 @@ function measureAttachmentPass(attachments, baseContext, lines, lift, measuredEl
697
704
  const resolved = resolveAttachment(dependency);
698
705
  if (!resolved)
699
706
  throw new Error("Layout attachment dependency has not been measured");
707
+ if (!resolved.box) {
708
+ resolved.box = { x: 0, y: 0, w: 0, h: 0 };
709
+ unionLayoutBoxes(resolved.box, resolved.geometry.regions);
710
+ }
700
711
  return resolved.box;
701
712
  },
702
713
  getContentBounds(content) {
@@ -712,9 +723,12 @@ function measureAttachmentPass(attachments, baseContext, lines, lift, measuredEl
712
723
  const resolved = resolveAttachment(attachment);
713
724
  if (!resolved && !attachments.includes(attachment))
714
725
  continue;
715
- const box = resolved?.box ?? context.getAttachmentBox(attachment);
716
- if (box.w > 0 || box.h > 0)
717
- boxes.push(box);
726
+ if (!resolved)
727
+ throw new Error("Layout attachment dependency has not been measured");
728
+ for (const region of resolved.geometry.regions) {
729
+ if (region.w > 0 || region.h > 0)
730
+ boxes.push(region);
731
+ }
718
732
  }
719
733
  const bounds = { x: 0, y: 0, w: 0, h: 0 };
720
734
  return unionLayoutBoxes(bounds, boxes) ? bounds : undefined;
@@ -722,9 +736,7 @@ function measureAttachmentPass(attachments, baseContext, lines, lift, measuredEl
722
736
  };
723
737
  const items = attachments.map(attachment => {
724
738
  const geometry = attachment.createGeometry(context);
725
- const box = { x: 0, y: 0, w: 0, h: 0 };
726
- unionLayoutBoxes(box, geometry.regions);
727
- const item = { geometry, box };
739
+ const item = { geometry };
728
740
  measured.set(attachment, item);
729
741
  // 当前项立即登记,保证后注册的 attachment 能看到并避让它
730
742
  for (const region of geometry.occupancy ?? geometry.regions) {
@@ -14,7 +14,6 @@ export interface LayoutElement {
14
14
  WR: number;
15
15
  margin_L: number;
16
16
  margin_R: number;
17
- fake?: boolean;
18
17
  }
19
18
  /**
20
19
  * 原地补齐全部弹簧属性
@@ -87,7 +87,7 @@ function fillPlaceholders(columns, F = DEFAULT_F) {
87
87
  const rIdx = rowId[1];
88
88
  if (!sortedCol[rIdx]) {
89
89
  // 保证 margin_L = mexLeft, margin_R = maxRight, 实际宽度为0
90
- const elPlaceholder = layoutElement({
90
+ sortedCol[rIdx] = layoutElement({
91
91
  alpha_L: maxLeft,
92
92
  alpha_R: maxRight,
93
93
  }, {
@@ -96,8 +96,6 @@ function fillPlaceholders(columns, F = DEFAULT_F) {
96
96
  // 后面不会修改,所以直接引用
97
97
  t: colTime, T: new Fraction(1), track: rowId[0]
98
98
  }, F);
99
- elPlaceholder.fake = true;
100
- sortedCol[rIdx] = elPlaceholder;
101
99
  }
102
100
  }
103
101
  }
@@ -320,7 +318,6 @@ export function layoutHorizontalRegion(columns, rows, X, fixed, limit, options,
320
318
  WR: span + right.WR,
321
319
  margin_L: left.margin_L,
322
320
  margin_R: right.margin_R,
323
- fake: left.fake && right.fake,
324
321
  };
325
322
  }));
326
323
  }
@@ -21,8 +21,9 @@ export declare function normalizePageConfig(source?: Partial<PageConfig>): PageC
21
21
  /**
22
22
  * 把已经测好自然高度的谱面行分页,并给出每行的全局顶部坐标。
23
23
  * pageBreaks 按行号索引:undefined 不强制换页,true/false 强制换页并指定上一页是否拉伸。
24
+ * headrooms 按行号索引:每行顶部高出主旋律上沿(基线 - 半字高)的外扩高度,优先借用上一行下方的行距留白。
24
25
  */
25
- export declare function paginateLayoutLines(heights: readonly number[], page: PageConfig, pageBreaks?: readonly (boolean | undefined)[]): {
26
+ export declare function paginateLayoutLines(heights: readonly number[], page: PageConfig, pageBreaks?: readonly (boolean | undefined)[], headrooms?: readonly number[]): {
26
27
  pages: DocumentLayoutPage[];
27
28
  lineTops: number[];
28
29
  };
@@ -20,6 +20,7 @@ export const DEFAULT_PAGE_CONFIG = {
20
20
  marginLeft: 40,
21
21
  marginRight: 40,
22
22
  lineGap: 22,
23
+ minLineGap: 11,
23
24
  fillRatio: 0.5,
24
25
  };
25
26
  /**
@@ -35,6 +36,7 @@ export function normalizePageConfig(source = {}) {
35
36
  marginLeft: Math.max(0, source.marginLeft ?? DEFAULT_PAGE_CONFIG.marginLeft),
36
37
  marginRight: Math.max(0, source.marginRight ?? DEFAULT_PAGE_CONFIG.marginRight),
37
38
  lineGap: Math.max(0, source.lineGap ?? DEFAULT_PAGE_CONFIG.lineGap),
39
+ minLineGap: Math.max(0, source.minLineGap ?? DEFAULT_PAGE_CONFIG.minLineGap),
38
40
  fillRatio: source.fillRatio ?? DEFAULT_PAGE_CONFIG.fillRatio,
39
41
  };
40
42
  if (!Number.isFinite(page.width) || page.width <= 0) {
@@ -49,8 +51,9 @@ export function normalizePageConfig(source = {}) {
49
51
  page.marginLeft,
50
52
  page.marginRight,
51
53
  page.lineGap,
54
+ page.minLineGap,
52
55
  ];
53
- if (values.some(value => !Number.isFinite(value) || value < 0)) {
56
+ if (values.some(value => !Number.isFinite(value))) {
54
57
  throw new Error("Page margins and line gap must be finite non-negative numbers");
55
58
  }
56
59
  const contentWidth = page.width - page.marginLeft - page.marginRight;
@@ -66,27 +69,26 @@ export function normalizePageConfig(source = {}) {
66
69
  /**
67
70
  * 把已经测好自然高度的谱面行分页,并给出每行的全局顶部坐标。
68
71
  * pageBreaks 按行号索引:undefined 不强制换页,true/false 强制换页并指定上一页是否拉伸。
72
+ * headrooms 按行号索引:每行顶部高出主旋律上沿(基线 - 半字高)的外扩高度,优先借用上一行下方的行距留白。
69
73
  */
70
- export function paginateLayoutLines(heights, page, pageBreaks = []) {
74
+ export function paginateLayoutLines(heights, page, pageBreaks = [], headrooms = []) {
71
75
  const availableHeight = page.height - page.marginTop - page.marginBottom;
76
+ const floor = Math.min(page.lineGap, page.minLineGap);
77
+ const gapBefore = (line) => Math.max(page.lineGap - Math.max(0, headrooms[line] ?? 0), floor);
72
78
  const pages = [];
73
79
  const lineTops = new Array(heights.length).fill(0);
74
80
  let pageTop = 0;
75
81
  let start = 0;
76
- // used 含最小行距,用于判断下一行能否装入;contentHeight 不含行距,用于封页后拉伸
82
+ // used 含行间实际间距,用于判断下一行能否装入并在封页后计算可拉伸余白
77
83
  let used = 0;
78
- let contentHeight = 0;
79
- /** 写出 [start, end) 这一页;完整页把剩余高度均分到每行下方,末页保留最小行距 */
84
+ /** 写出 [start, end) 这一页;完整页把剩余高度均分到每行下方,末页保留自然行距 */
80
85
  const closePage = (end, justify) => {
81
86
  const count = end - start;
82
- const remaining = justify
83
- ? Math.max(0, availableHeight - contentHeight - page.lineGap * (count - 1))
84
- : 0;
85
- const gap = page.lineGap + remaining / count;
87
+ const share = justify ? Math.max(0, availableHeight - used) / count : 0;
86
88
  let lineTop = pageTop + page.marginTop;
87
89
  for (let i = start; i < end; i++) {
88
90
  lineTops[i] = lineTop;
89
- lineTop += heights[i] + (i + 1 < end ? gap : 0);
91
+ lineTop += heights[i] + (i + 1 < end ? gapBefore(i + 1) + share : 0);
90
92
  }
91
93
  const height = Number.isFinite(page.height)
92
94
  ? page.height
@@ -105,7 +107,6 @@ export function paginateLayoutLines(heights, page, pageBreaks = []) {
105
107
  pageTop += height;
106
108
  start = end;
107
109
  used = 0;
108
- contentHeight = 0;
109
110
  };
110
111
  for (let i = 0; i < heights.length; i++) {
111
112
  const height = heights[i];
@@ -113,11 +114,10 @@ export function paginateLayoutLines(heights, page, pageBreaks = []) {
113
114
  throw new PageLayoutError(i, height, availableHeight);
114
115
  }
115
116
  // 强制边界与自然溢出统一封页;无限页高不分配余白
116
- if (i > start && (pageBreaks[i] !== undefined || Number.isFinite(page.height) && used + page.lineGap + height > availableHeight + 1e-6)) {
117
+ if (i > start && (pageBreaks[i] !== undefined || Number.isFinite(page.height) && used + gapBefore(i) + height > availableHeight + 1e-6)) {
117
118
  closePage(i, Number.isFinite(page.height) && (pageBreaks[i] ?? true));
118
119
  }
119
- used += (i > start ? page.lineGap : 0) + height;
120
- contentHeight += height;
120
+ used += (i > start ? gapBefore(i) : 0) + height;
121
121
  }
122
122
  if (heights.length > start)
123
123
  closePage(heights.length, false);
@@ -1,12 +1,10 @@
1
1
  import type { Track } from "../lowering/track.js";
2
- import type { HorizontalLineView, LayoutHost, LayoutRange } from "./types.js";
2
+ import type { AttachmentLayoutContext, HorizontalLineView, LayoutHost, LayoutRange } from "./types.js";
3
3
  export type QueryLine = Omit<HorizontalLineView, "registerHorizontalLayoutHook">;
4
4
  /** 范围在本域视图中覆盖的闭区间列,start > end 表示空 */
5
5
  interface ResolvedRange {
6
6
  start: number;
7
7
  end: number;
8
- /** 省略范围即整行,附件占用不再按所选主体的横向范围筛选 */
9
- wholeLine: boolean;
10
8
  }
11
9
  /**
12
10
  * 把轨归入以 root 为根的 Track 树
@@ -17,10 +15,13 @@ export declare function liftInto(root: Track): (track: Track) => Track;
17
15
  /**
18
16
  * 在正文行或子域的视图里解析一次范围查询
19
17
  *
20
- * 数字端点是本视图的列下标;对象端点可以逆序或跨行,按本行截取。
21
- * 指定 track 时两个端点都必须位于该轨,私有轨先经 lift 归入本域
18
+ * 对象端点可以逆序或跨行,按本行截取。
22
19
  */
23
- export declare function resolveRange(view: QueryLine, range: LayoutRange | undefined, track: Track | undefined, lift: (track: Track) => Track): ResolvedRange;
20
+ export declare function resolveRange(view: QueryLine, range?: LayoutRange): ResolvedRange;
24
21
  /** 范围内的列;指定 track 时只保留该轨主体,空列照样保留 */
25
22
  export declare function rangeColumns(view: QueryLine | undefined, range: LayoutRange | undefined, track: Track | undefined, lift: (track: Track) => Track): readonly (readonly LayoutHost[])[];
23
+ /** 跳过行首零时值系统前缀,取该行指定轨首个占时值主体的左边界 */
24
+ export declare function lineEntryX(context: Pick<AttachmentLayoutContext, "lines" | "originX">, line: number, track: Track, fallback?: number): number;
25
+ /** 取该行所有可见主体的最大右边界,空行回退到版心右沿 */
26
+ export declare function lineRightX(context: Pick<AttachmentLayoutContext, "lines" | "originX" | "width">, line: number): number;
26
27
  export {};
@@ -1,5 +1,5 @@
1
1
  import { isVisualTemporalNode, TemporalNodeBase } from "../functions/temporal.js";
2
- const EMPTY = { start: 0, end: -1, wholeLine: false };
2
+ const EMPTY = { start: 0, end: -1 };
3
3
  /**
4
4
  * 把轨归入以 root 为根的 Track 树
5
5
  *
@@ -37,35 +37,27 @@ function columnOf(view, host) {
37
37
  /**
38
38
  * 在正文行或子域的视图里解析一次范围查询
39
39
  *
40
- * 数字端点是本视图的列下标;对象端点可以逆序或跨行,按本行截取。
41
- * 指定 track 时两个端点都必须位于该轨,私有轨先经 lift 归入本域
40
+ * 对象端点可以逆序或跨行,按本行截取。
42
41
  */
43
- export function resolveRange(view, range, track, lift) {
42
+ export function resolveRange(view, range) {
44
43
  if (!range)
45
- return { start: 0, end: view.columns.length - 1, wholeLine: true };
46
- if (typeof range[0] === "number") {
47
- return { start: Math.max(0, range[0]), end: Math.min(view.columns.length - 1, range[1]), wholeLine: false };
48
- }
44
+ return { start: 0, end: view.columns.length - 1 };
49
45
  let [from, to] = range;
50
46
  if (from.layoutLine > to.layoutLine)
51
47
  [from, to] = [to, from];
52
48
  if (view.index < from.layoutLine || view.index > to.layoutLine)
53
49
  return EMPTY;
54
- const target = track && lift(track);
55
- if (target && (lift(from.track) !== target || lift(to.track) !== target)) {
56
- throw new Error("Range endpoints must belong to the selected track");
57
- }
58
50
  const start = from.layoutLine < view.index ? 0 : columnOf(view, from);
59
51
  const end = to.layoutLine > view.index ? view.columns.length - 1 : columnOf(view, to);
60
52
  if (start < 0 || end < 0)
61
53
  return EMPTY;
62
- return { start: Math.min(start, end), end: Math.max(start, end), wholeLine: false };
54
+ return { start: Math.min(start, end), end: Math.max(start, end) };
63
55
  }
64
56
  /** 范围内的列;指定 track 时只保留该轨主体,空列照样保留 */
65
57
  export function rangeColumns(view, range, track, lift) {
66
58
  if (!view)
67
59
  return [];
68
- const { start, end } = resolveRange(view, range, track, lift);
60
+ const { start, end } = resolveRange(view, range);
69
61
  const columns = view.columns.slice(start, end + 1);
70
62
  if (!track)
71
63
  return columns;
@@ -73,3 +65,20 @@ export function rangeColumns(view, range, track, lift) {
73
65
  const onTarget = (host) => host.track === target;
74
66
  return columns.every(column => column.every(onTarget)) ? columns : columns.map(column => column.filter(onTarget));
75
67
  }
68
+ /** 跳过行首零时值系统前缀,取该行指定轨首个占时值主体的左边界 */
69
+ export function lineEntryX(context, line, track, fallback = context.originX) {
70
+ const host = context.lines[line]?.trackRuns.get(track)?.find(item => !item.T.isZero());
71
+ return host?.box.x ?? fallback;
72
+ }
73
+ /** 取该行所有可见主体的最大右边界,空行回退到版心右沿 */
74
+ export function lineRightX(context, line) {
75
+ const runs = context.lines[line]?.trackRuns;
76
+ if (!runs || runs.size === 0)
77
+ return context.originX + context.width;
78
+ let right = context.originX;
79
+ for (const run of runs.values()) {
80
+ for (const host of run)
81
+ right = Math.max(right, host.box.x + host.box.w);
82
+ }
83
+ return right;
84
+ }
@@ -1,10 +1,11 @@
1
1
  import type { Painter, TextMeasurer } from "../render/types.js";
2
- import type { Track } from "../lowering/track.js";
2
+ import type { Extent, Track } from "../lowering/track.js";
3
3
  import type { Fraction } from "../fraction.js";
4
4
  import type { TemporalNodeBase, VisualTemporalNode } from "../functions/temporal.js";
5
5
  import type { SourceSpan } from "../parser/types.js";
6
6
  import type { HorizontalLayoutHook } from "./model.js";
7
- import type { LoweringAttachment, LoweringContent } from "../lowering/types.js";
7
+ import type { LoweringAttachment, LoweringContent, PageConfig } from "../lowering/types.js";
8
+ export type { Extent, PageConfig };
8
9
  /** 轴对齐矩形,是所有排版几何的公共部分 */
9
10
  export interface Rect {
10
11
  x: number;
@@ -12,14 +13,6 @@ export interface Rect {
12
13
  w: number;
13
14
  h: number;
14
15
  }
15
- /**
16
- * 相对某条基线的纵向占用
17
- * 约定 top <= 0 <= bottom
18
- */
19
- export interface Extent {
20
- top: number;
21
- bottom: number;
22
- }
23
16
  /**
24
17
  * 一个排版对象的定位矩形和两个对齐基准
25
18
  *
@@ -47,17 +40,6 @@ export interface TimeLineEvent {
47
40
  T: Fraction;
48
41
  track: Track;
49
42
  }
50
- /** 文档页面的固化尺寸,所有值均为 px */
51
- export interface PageConfig {
52
- width: number;
53
- height: number;
54
- marginTop: number;
55
- marginBottom: number;
56
- marginLeft: number;
57
- marginRight: number;
58
- lineGap: number;
59
- fillRatio: number;
60
- }
61
43
  /**
62
44
  * 排版前的共享资源
63
45
  *
@@ -85,7 +67,7 @@ export interface SubdomainLayout {
85
67
  /** 与 columns.flat() 一一对应,不含成员 onPlaced 追加的平移 */
86
68
  positions: readonly LayoutPoint[];
87
69
  }
88
- export type LayoutRange = readonly [from: number, to: number] | readonly [from: LayoutHost, to: LayoutHost];
70
+ export type LayoutRange = readonly [from: LayoutHost, to: LayoutHost];
89
71
  /** attachment 根据当前视觉轴生成几何时需要的完整页面信息 */
90
72
  export interface AttachmentLayoutContext extends LayoutPrepareContext {
91
73
  /** 占用测量使用布局位置,最终绘制才应用后置平移 */
@@ -102,18 +84,18 @@ export interface AttachmentLayoutContext extends LayoutPrepareContext {
102
84
  /**
103
85
  * 本行闭区间内的已定占用;全轨返回按 Track 分组的 Map,单轨直接返回 Extent 或 undefined
104
86
  *
105
- * 数字端点表示本域的列下标;对象端点沿 foldedInto 上溯到本域的列,跨行对象区间自动截取本行部分。
87
+ * 对象端点沿 foldedInto 上溯到本域的列,跨行对象区间自动截取本行部分。
106
88
  * 子域对外是一个主体,正文查询不进入它的内部。
107
89
  * 主体按查询列筛选;attachment 按这些列最终盒子的横向范围筛选,并遵守 track 过滤。
108
90
  * attachment 部分与 getAttachmentBox 同一条可见性规则:只看得见比自己先注册的,
109
91
  * 也就是「后声明的排在外层」。避让型 attachment 用它把自己排到已有内容之外。
110
- * 省略列号表示整行,供跨行 attachment 查询没有可见列的中间行。
92
+ * 省略范围表示整行,供跨行 attachment 查询没有可见列的中间行。
111
93
  */
112
- getRangeExtents(line: number, columns?: LayoutRange): ReadonlyMap<Track, Readonly<Extent>>;
113
- getRangeExtents(line: number, columns: LayoutRange | undefined, track: Track): Readonly<Extent> | undefined;
94
+ getRangeExtents(line: number, range?: LayoutRange): ReadonlyMap<Track, Readonly<Extent>>;
95
+ getRangeExtents(line: number, range: LayoutRange | undefined, track: Track): Readonly<Extent> | undefined;
114
96
  /** 按全局 x 区间筛选横向相交的主体及已测附件,返回各轨轴局部占用;空区间返回空 Map */
115
97
  getHorizontalExtents(line: number, left: number, right: number): ReadonlyMap<Track, Readonly<Extent>>;
116
- /** 读取本轮已完成的 attachment 边界;分组在 endLoweringGroup 才注册,因而组内对象必然排在分组之前 */
98
+ /** 读取本轮已完成的 attachment 边界;外层分组在 endLoweringGroup 后才注册自身附件,因而组内对象必然排在分组之前 */
117
99
  getAttachmentBox(attachment: LayoutAttachment): Readonly<Rect>;
118
100
  /** 读取已测附件的有效占用,保留显式空数组;尚未测量时返回 undefined */
119
101
  getAttachmentOccupancy(attachment: LayoutAttachment): readonly LayoutRegion[] | undefined;
@@ -244,7 +226,6 @@ export interface LayoutAttachment extends LoweringAttachment {
244
226
  export declare function isLayoutAttachment(attachment: LoweringAttachment): attachment is LayoutAttachment;
245
227
  /** layoutDocument 输出的最终 attachment 快照 */
246
228
  export interface PlacedAttachment {
247
- readonly box: Readonly<Rect>;
248
229
  readonly regions: readonly LayoutRegion[];
249
230
  /** 相对于 Temporal 主体的绘制层;background 比内容先绘制 */
250
231
  readonly layer: "background" | "foreground";
@@ -1,31 +1,27 @@
1
1
  import { Diagnostic } from "../diagnostic.js";
2
2
  import { Fraction } from "../fraction.js";
3
- import { ASTFunctionClass, ASTNodeBase } from "../functions/ASTtypes.js";
4
- import type { PageConfig } from "../layout/types.js";
3
+ import { ASTNodeBase } from "../functions/ASTtypes.js";
5
4
  import { TemporalNodeBase } from "../functions/temporal.js";
6
- import type { LoweringGroup, LoweringAttachment, LoweringResult } from "./types.js";
5
+ import type { LoweringFinalizer, LoweringGroup, LoweringAttachment, LoweringResult, PageConfig } from "./types.js";
7
6
  import { Track } from "./track.js";
8
7
  /**
9
8
  * 把 AST 展开并固化为时间列、attachment 和页面配置
10
9
  *
11
10
  * 标准用法:
12
11
  * 1. 创建 LoweringContext
13
- * 2. 调用 registerFunctions,传入与 parser 相同的函数类列表
14
- * 3. 调用 lowerDocument(root),得到 LoweringResult
15
- * 4. 把结果交给 layoutDocument
12
+ * 2. 调用 lowerDocument(root),得到 LoweringResult
13
+ * 3. 把结果交给 layoutDocument
16
14
  *
17
15
  * 大多数 AST 函数通过以下入口参与 lowering(按时间顺序):
18
16
  * - [hook] loweringEnter/loweringExit:进入或离开节点时生成 TemporalNode、管理作用域
19
17
  * - [API] LoweringContext.beginLoweringGroup/endLoweringGroup:通常在 enter/exit 中被调用,以栈方式观察当前子树产生的 TemporalNode 和 attachment
20
18
  * - [API] LoweringContext.addAttachment:注册不推进时间的附属对象;当前外层分组会收到该对象
19
+ * - [API] LoweringContext.deferFinalize:在 enter/exit 中按需登记本轮收尾回调,支持通过 order 指定执行优先级
21
20
  * - [config] timeFlowModel:声明子节点按 sequence 或 parallel 推进时间;parallel 还要声明音轨复用键与纵向排列策略
22
21
  * - [hook] Temporal.onTimeState:锚点归并后固化调性、速度等状态
23
- * - [hook] loweringAugment:最终列和 layoutLine 固化后扫描结果并生成要追加的 attachment
24
- * - [hook] loweringFinalize:所有派生 attachment 追加完成后,按注册顺序处理完整结果
25
22
  */
26
23
  export declare class LoweringContext {
27
- private loweringAugmenters;
28
- private loweringFinalizers;
24
+ private activeFinalizers;
29
25
  private cnt;
30
26
  private usedTracks;
31
27
  /** 不推进时间的附属对象 */
@@ -43,35 +39,33 @@ export declare class LoweringContext {
43
39
  */
44
40
  private activeLoweringGroups;
45
41
  /** 收集本轮 lowering 产生的诊断信息 */
46
- readonly diagnostics: Diagnostic[];
47
- constructor(diagnostics?: Diagnostic[]);
48
- /** 供外部调用: 初始化 hook */
49
- registerFunctions(functionClasses: ASTFunctionClass[]): void;
42
+ diagnostics: Diagnostic[];
50
43
  /** 供 page 函数调用 */
51
44
  setPageConfig(page: PageConfig): void;
52
45
  /**
53
46
  * 完整 lowering 入口
54
47
  *
55
- * 每次调用都会重置本轮产生的数据
56
- * 已注册的后处理 hook 会保留,可以复用同一个 LoweringContext
48
+ * 每次调用都会重置本轮产生的数据,可以复用同一个 LoweringContext
57
49
  */
58
- lowerDocument(node: ASTNodeBase): LoweringResult;
50
+ lowerDocument(node: ASTNodeBase, diagnostics?: Diagnostic[]): LoweringResult;
59
51
  /**
60
52
  * 固化谱面行号、时间状态和最终时长
61
53
  */
62
54
  private solidifyColumns;
63
55
  /**
64
56
  * 开始一个 lowering 分组作用域
65
- * group 可以收集并提交新增 temporal,也可以观察成员并携带一个 attachment
57
+ * group 可以观察当前子树产生的 TemporalNode 和 attachment
66
58
  */
67
59
  beginLoweringGroup(owner: ASTNodeBase, group: LoweringGroup): void;
68
60
  /**
69
- * 结束最近的分组并注册其辅助排版对象
70
- * 嵌套分组按退出顺序注册,因此天然是由内向外计算边界
61
+ * 结束最近的分组并返回它
62
+ * 调用方可在退出分组后按需注册自身 attachment,天然实现由内向外计算边界
71
63
  */
72
- endLoweringGroup(owner: ASTNodeBase): LoweringGroup;
73
- /** 先统一生成派生 attachment,再按注册顺序执行最终处理 */
64
+ endLoweringGroup<T extends LoweringGroup = LoweringGroup>(owner: ASTNodeBase): T;
65
+ /** 按显式 order 升序执行本轮登记的收尾回调 */
74
66
  private postprocessResult;
67
+ /** 登记本轮需要执行的收尾回调;order 越小越先执行,默认 0 */
68
+ deferFinalize(finalize: LoweringFinalizer, order?: number): void;
75
69
  /**
76
70
  * 注册不推进时间的排版对象
77
71
  * 被 tie、beam、box 等函数使用
@@ -95,8 +89,9 @@ export declare class LoweringContext {
95
89
  * 递归展开一棵子树,得到它的时间列与结束时间
96
90
  * @param timeOffset 当前节点的时间偏移量(单位QN) 由父节点传入,会被原地修改
97
91
  * @param track 当前所在的纵向音轨
92
+ * @param columns 当前时间列数组,会被原地修改
98
93
  */
99
- trackedEvents(node: ASTNodeBase, timeOffset: Fraction, track: Track): TimeColumn[];
94
+ trackedEvents(node: ASTNodeBase, timeOffset: Fraction, track: Track, columns?: TimeColumn[]): TimeColumn[];
100
95
  /**
101
96
  * 将 hook 返回的事件规范化、加入索引与分组,再追加到时间列
102
97
  * 原地修改 timeOffset