jpfun 0.8.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 (97) hide show
  1. package/dist/converter/midi/convert.js +8 -55
  2. package/dist/converter/musicxml/features.d.ts +1 -1
  3. package/dist/converter/musicxml/features.js +1 -1
  4. package/dist/converter/musicxml/index.js +8 -18
  5. package/dist/converter/source.d.ts +4 -7
  6. package/dist/converter/source.js +11 -9
  7. package/dist/functions/ASTtypes.d.ts +1 -14
  8. package/dist/functions/ASTtypes.js +5 -36
  9. package/dist/functions/adjust/index.js +45 -33
  10. package/dist/functions/arpeggio/index.js +42 -45
  11. package/dist/functions/arpeggio/shape.d.ts +2 -3
  12. package/dist/functions/arpeggio/shape.js +20 -50
  13. package/dist/functions/bar/index.js +3 -8
  14. package/dist/functions/beam/auto.js +54 -123
  15. package/dist/functions/beam/index.js +4 -9
  16. package/dist/functions/box/index.js +0 -6
  17. package/dist/functions/dash/index.js +3 -4
  18. package/dist/functions/div/index.d.ts +2 -4
  19. package/dist/functions/div/index.js +10 -10
  20. package/dist/functions/dot/index.js +3 -9
  21. package/dist/functions/dyn/index.js +6 -26
  22. package/dist/functions/gliss/index.js +95 -89
  23. package/dist/functions/grace/index.d.ts +2 -5
  24. package/dist/functions/grace/index.js +44 -89
  25. package/dist/functions/head/index.js +9 -6
  26. package/dist/functions/key/index.js +6 -6
  27. package/dist/functions/meter/index.js +3 -3
  28. package/dist/functions/note/index.js +1 -1
  29. package/dist/functions/note/source-transform.js +2 -5
  30. package/dist/functions/ottava/index.js +11 -32
  31. package/dist/functions/page/index.js +13 -16
  32. package/dist/functions/rest/index.js +2 -4
  33. package/dist/functions/slide/index.js +0 -1
  34. package/dist/functions/stack/index.js +8 -57
  35. package/dist/functions/temporal.d.ts +2 -2
  36. package/dist/functions/temporal.js +7 -0
  37. package/dist/functions/text/index.js +24 -30
  38. package/dist/functions/tie/index.js +14 -37
  39. package/dist/functions/tremolo/index.js +0 -1
  40. package/dist/functions/tuplet/index.js +0 -6
  41. package/dist/functions/up/index.js +79 -83
  42. package/dist/functions/voice/connections.d.ts +2 -1
  43. package/dist/functions/voice/connections.js +48 -10
  44. package/dist/functions/voice/index.d.ts +2 -3
  45. package/dist/functions/voice/index.js +2 -970
  46. package/dist/functions/voice/lyrics.d.ts +30 -0
  47. package/dist/functions/voice/lyrics.js +289 -0
  48. package/dist/functions/voice/voice.d.ts +63 -0
  49. package/dist/functions/voice/voice.js +373 -0
  50. package/dist/functions/voice/voices.d.ts +106 -0
  51. package/dist/functions/voice/voices.js +423 -0
  52. package/dist/functions/volta/index.js +5 -23
  53. package/dist/jpfun.core.min.js +65 -56
  54. package/dist/jpfun.from-midi.min.js +5 -5
  55. package/dist/jpfun.from-musicxml.min.js +7 -7
  56. package/dist/jpfun.min.js +68 -59
  57. package/dist/layout/engine.js +30 -18
  58. package/dist/layout/model.d.ts +0 -1
  59. package/dist/layout/model.js +1 -4
  60. package/dist/layout/page.d.ts +2 -1
  61. package/dist/layout/page.js +14 -14
  62. package/dist/layout/range.d.ts +7 -6
  63. package/dist/layout/range.js +23 -14
  64. package/dist/layout/types.d.ts +9 -28
  65. package/dist/lowering/loweringContext.d.ts +8 -11
  66. package/dist/lowering/loweringContext.js +7 -13
  67. package/dist/lowering/track.d.ts +8 -1
  68. package/dist/lowering/types.d.ts +12 -8
  69. package/dist/parser/parse-utils/call-utils.d.ts +1 -2
  70. package/dist/parser/parse-utils/call-utils.js +6 -34
  71. package/dist/parser/parse-utils/note-utils.d.ts +3 -3
  72. package/dist/parser/parse-utils/note-utils.js +17 -28
  73. package/dist/parser/parse-utils/string-utils.d.ts +1 -1
  74. package/dist/parser/parse-utils/string-utils.js +8 -6
  75. package/dist/parser/parserContext.d.ts +9 -9
  76. package/dist/parser/parserContext.js +54 -10
  77. package/dist/pipeline.js +2 -2
  78. package/dist/playback/compile.js +13 -14
  79. package/dist/playback/event.d.ts +6 -6
  80. package/dist/playback/event.js +13 -49
  81. package/dist/render/canvas.d.ts +0 -2
  82. package/dist/render/canvas.js +0 -21
  83. package/dist/render/svg.d.ts +0 -1
  84. package/dist/render/svg.js +33 -31
  85. package/dist/render/text.d.ts +36 -1
  86. package/dist/render/text.js +134 -0
  87. package/dist/render/types.d.ts +0 -2
  88. package/grammar.md +16 -13
  89. package/package.json +1 -1
  90. package/dist/iife/core.d.ts +0 -1
  91. package/dist/iife/core.js +0 -3
  92. package/dist/iife/from-midi.d.ts +0 -1
  93. package/dist/iife/from-midi.js +0 -15
  94. package/dist/iife/from-musicxml.d.ts +0 -1
  95. package/dist/iife/from-musicxml.js +0 -3
  96. package/dist/iife/full.d.ts +0 -1
  97. package/dist/iife/full.js +0 -3
@@ -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,9 +1,8 @@
1
1
  import { Diagnostic } from "../diagnostic.js";
2
2
  import { Fraction } from "../fraction.js";
3
3
  import { ASTNodeBase } from "../functions/ASTtypes.js";
4
- import type { PageConfig } from "../layout/types.js";
5
4
  import { TemporalNodeBase } from "../functions/temporal.js";
6
- import type { LoweringFinalizer, 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 和页面配置
@@ -40,31 +39,29 @@ export declare class LoweringContext {
40
39
  */
41
40
  private activeLoweringGroups;
42
41
  /** 收集本轮 lowering 产生的诊断信息 */
43
- readonly diagnostics: Diagnostic[];
44
- constructor(diagnostics?: Diagnostic[]);
42
+ diagnostics: Diagnostic[];
45
43
  /** 供 page 函数调用 */
46
44
  setPageConfig(page: PageConfig): void;
47
45
  /**
48
46
  * 完整 lowering 入口
49
47
  *
50
- * 每次调用都会重置本轮产生的数据
51
- * 已注册的后处理 hook 会保留,可以复用同一个 LoweringContext
48
+ * 每次调用都会重置本轮产生的数据,可以复用同一个 LoweringContext
52
49
  */
53
- lowerDocument(node: ASTNodeBase): LoweringResult;
50
+ lowerDocument(node: ASTNodeBase, diagnostics?: Diagnostic[]): LoweringResult;
54
51
  /**
55
52
  * 固化谱面行号、时间状态和最终时长
56
53
  */
57
54
  private solidifyColumns;
58
55
  /**
59
56
  * 开始一个 lowering 分组作用域
60
- * group 可以收集并提交新增 temporal,也可以观察成员并携带一个 attachment
57
+ * group 可以观察当前子树产生的 TemporalNode 和 attachment
61
58
  */
62
59
  beginLoweringGroup(owner: ASTNodeBase, group: LoweringGroup): void;
63
60
  /**
64
- * 结束最近的分组并注册其辅助排版对象
65
- * 嵌套分组按退出顺序注册,因此天然是由内向外计算边界
61
+ * 结束最近的分组并返回它
62
+ * 调用方可在退出分组后按需注册自身 attachment,天然实现由内向外计算边界
66
63
  */
67
- endLoweringGroup(owner: ASTNodeBase): LoweringGroup;
64
+ endLoweringGroup<T extends LoweringGroup = LoweringGroup>(owner: ASTNodeBase): T;
68
65
  /** 按显式 order 升序执行本轮登记的收尾回调 */
69
66
  private postprocessResult;
70
67
  /** 登记本轮需要执行的收尾回调;order 越小越先执行,默认 0 */
@@ -37,10 +37,7 @@ export class LoweringContext {
37
37
  */
38
38
  activeLoweringGroups = [];
39
39
  /** 收集本轮 lowering 产生的诊断信息 */
40
- diagnostics;
41
- constructor(diagnostics = []) {
42
- this.diagnostics = diagnostics;
43
- }
40
+ diagnostics = [];
44
41
  /** 供 page 函数调用 */
45
42
  setPageConfig(page) {
46
43
  this.page = page;
@@ -48,10 +45,9 @@ export class LoweringContext {
48
45
  /**
49
46
  * 完整 lowering 入口
50
47
  *
51
- * 每次调用都会重置本轮产生的数据
52
- * 已注册的后处理 hook 会保留,可以复用同一个 LoweringContext
48
+ * 每次调用都会重置本轮产生的数据,可以复用同一个 LoweringContext
53
49
  */
54
- lowerDocument(node) {
50
+ lowerDocument(node, diagnostics = []) {
55
51
  this.cnt = 0;
56
52
  this.usedTracks.clear();
57
53
  this.activeFinalizers.clear();
@@ -59,6 +55,7 @@ export class LoweringContext {
59
55
  this.page = void 0;
60
56
  this.astToTemporal = new Map();
61
57
  this.activeLoweringGroups = [];
58
+ this.diagnostics = diagnostics;
62
59
  const rootTrack = new Track();
63
60
  const duration = new Fraction();
64
61
  const columns = this.trackedEvents(node, duration, rootTrack);
@@ -109,7 +106,6 @@ export class LoweringContext {
109
106
  column.shiftTime(timeOffset);
110
107
  let insertion;
111
108
  for (const node of column) {
112
- state.currentEvent = node;
113
109
  state.velocity = velocityOf(node.track);
114
110
  state.program = programOf(node.track);
115
111
  const offset = node.onTimeState?.(state);
@@ -146,14 +142,14 @@ export class LoweringContext {
146
142
  }
147
143
  /**
148
144
  * 开始一个 lowering 分组作用域
149
- * group 可以收集并提交新增 temporal,也可以观察成员并携带一个 attachment
145
+ * group 可以观察当前子树产生的 TemporalNode 和 attachment
150
146
  */
151
147
  beginLoweringGroup(owner, group) {
152
148
  this.activeLoweringGroups.push({ owner, group });
153
149
  }
154
150
  /**
155
- * 结束最近的分组并注册其辅助排版对象
156
- * 嵌套分组按退出顺序注册,因此天然是由内向外计算边界
151
+ * 结束最近的分组并返回它
152
+ * 调用方可在退出分组后按需注册自身 attachment,天然实现由内向外计算边界
157
153
  */
158
154
  endLoweringGroup(owner) {
159
155
  const active = this.activeLoweringGroups.pop();
@@ -161,8 +157,6 @@ export class LoweringContext {
161
157
  throw new Error("No active lowering group to end");
162
158
  if (active.owner !== owner)
163
159
  throw new Error("Lowering groups must end in reverse order");
164
- if (active.group.attachment)
165
- this.addAttachment(active.group.attachment);
166
160
  return active.group;
167
161
  }
168
162
  /** 按显式 order 升序执行本轮登记的收尾回调 */
@@ -1,4 +1,11 @@
1
- import type { Extent } from "../layout/types.js";
1
+ /**
2
+ * 相对某条基线的纵向占用
3
+ * 约定 top <= 0 <= bottom
4
+ */
5
+ export interface Extent {
6
+ top: number;
7
+ bottom: number;
8
+ }
2
9
  /** measure 给出的单个成员的局部位置 */
3
10
  export interface TrackPlacement {
4
11
  /**
@@ -3,8 +3,19 @@ import type { TemporalNodeBase } from "../functions/temporal.js";
3
3
  import type { Diagnostic } from "../diagnostic.js";
4
4
  import type { SourceSpan } from "../parser/types.js";
5
5
  import type { Fraction } from "../fraction.js";
6
- import type { PageConfig } from "../layout/types.js";
7
6
  import type { Track, TrackArrangement } from "./track.js";
7
+ /** 文档页面的固化尺寸,所有值均为 px */
8
+ export interface PageConfig {
9
+ width: number;
10
+ height: number;
11
+ marginTop: number;
12
+ marginBottom: number;
13
+ marginLeft: number;
14
+ marginRight: number;
15
+ lineGap: number;
16
+ minLineGap: number;
17
+ fillRatio: number;
18
+ }
8
19
  /** lowering 产生的不推进时间的附属对象;具体能力由 layout/playback 子协议声明 */
9
20
  export interface LoweringAttachment {
10
21
  readonly sourceSpan?: SourceSpan;
@@ -40,13 +51,6 @@ export interface LoweringResult {
40
51
  * 回调按当前作用域栈由内向外执行;onTemporal 在事件加入时间列、推进时间游标前调用。
41
52
  */
42
53
  export interface LoweringGroup {
43
- /**
44
- * 当前分组退出后会自动注册的 attachment
45
- *
46
- * attachment 在 layout 的时候会按照顺序求解,对于 box 这样的必须让子元素先被测量,才能确定尺寸,所以需要最后加入
47
- * 之所以有这个字段(而不是在 endLoweringGroup 之后手动 push)是为了收集到的信息可以直接传递,参考 box
48
- */
49
- attachment?: LoweringAttachment;
50
54
  /** 观察组内生成的事件 */
51
55
  onTemporal?(node: TemporalNodeBase): void;
52
56
  /** 观察组内注册的附属对象 */
@@ -13,10 +13,9 @@ import type { CallInfo } from "../grammarType.js";
13
13
  * - 正确处理字符串字面量(支持转义)
14
14
  * - 支持嵌套小括号 `(...)`,但被大括号 `{...}` 包裹的小括号不会影响外层匹配
15
15
  * - 忽略大括号内的小括号深度变化
16
- * - 在顶层小括号内以逗号分割参数,记录每个参数的原始文本及其在 `source` 中的起始/结束位置
16
+ * - 在顶层小括号内以逗号分割参数,同时记录每个参数的首个顶层 `=` 及其在 `source` 中的起始/结束位置
17
17
  * - 若找到匹配右括号,记录 closeParenSpan;否则在 end 处结束,让上层决定诊断/恢复策略
18
18
  */
19
19
  export declare function readCall(source: string, atPos: number, end: number): CallInfo | null;
20
20
  export declare function trimRange(source: string, start: number, end: number): SourceSpan;
21
- export declare function findTopLevelEquals(text: string, start?: number, end?: number): number;
22
21
  export declare function findRightParen(source: string, start: number, end: number): number;