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
@@ -7,13 +7,22 @@ import { prepareArpeggioShape } from "./shape.js";
7
7
  /** Fold 保持模块私有;琶音只在自己的边界读取它已经公开在实例上的结构。 */
8
8
  function readFold(node) {
9
9
  const candidate = node;
10
- const ast = node.ast;
11
- return (ast.callName === "up" || ast.callName === "down")
12
- && Array.isArray(candidate.members)
10
+ const ast = candidate?.ast;
11
+ return (ast?.callName === "up" || ast?.callName === "down")
12
+ && Array.isArray(candidate?.members)
13
13
  && Array.isArray(ast.contents)
14
14
  ? candidate
15
15
  : null;
16
16
  }
17
+ /** 折叠成员共享同一对齐锚点,取最宽左半宽才能避免升降号等越过宿主左界压住琶音线 */
18
+ function foldAnchor(fold) {
19
+ let anchor = fold.box.anchor;
20
+ for (const member of fold.members) {
21
+ const nested = readFold(member);
22
+ anchor = Math.max(anchor, nested ? foldAnchor(nested) : member.box.anchor);
23
+ }
24
+ return anchor;
25
+ }
17
26
  class ArpeggioFunction extends ASTFunctionNode {
18
27
  static def = {
19
28
  name: ["arp", "arpeggio"],
@@ -43,6 +52,7 @@ class ArpeggioFunction extends ASTFunctionNode {
43
52
  direction;
44
53
  size;
45
54
  get children() { return [this.content]; }
55
+ timeFlowModel() { return null; }
46
56
  constructor(span, args, ctx, parent = null) {
47
57
  super(span, parent);
48
58
  const [content, direction] = this.getArgValue(args, ctx);
@@ -58,15 +68,9 @@ class ArpeggioFunction extends ASTFunctionNode {
58
68
  content.parent = this;
59
69
  }
60
70
  loweringEnter(ctx, track) {
61
- const host = ctx.isolateFromLoweringGroups(() => {
62
- const events = ctx.trackedEvents(this.content, new Fraction(), track).flat();
63
- if (events.length !== 1 || !events[0].box) {
64
- throw new ErrorDiagnostic("E_ARPEGGIO_INVALID_CONTENT", "@arp 的内容必须恰好产生一个同轨和弦", this.content.sourceSpan);
65
- }
66
- return events[0];
67
- });
68
- const fold = readFold(host);
69
- if (!fold || fold.members.length < 2 || fold.members.some(member => member.track !== track)) {
71
+ const events = ctx.isolateFromLoweringGroups(() => ctx.trackedEvents(this.content, new Fraction(), track).flat());
72
+ const fold = events.length === 1 ? readFold(events[0]) : null;
73
+ if (!fold || fold.members.length < 2) {
70
74
  throw new ErrorDiagnostic("E_ARPEGGIO_INVALID_CONTENT", "@arp 只接受至少包含两个成员的同轨 @up/@down 折叠体", this.content.sourceSpan);
71
75
  }
72
76
  return [new ArpeggioTemporal(this, fold)];
@@ -79,9 +83,9 @@ class ArpeggioFunction extends ASTFunctionNode {
79
83
  export const ArpeggioNode = ArpeggioFunction;
80
84
  class ArpeggioTemporal extends TemporalNodeBase {
81
85
  host;
82
- playbackMembers;
86
+ /** 槽位取自成员顺序;无声成员也占槽,不能按实际发声音符重新编号 */
87
+ slots;
83
88
  hostX = 0;
84
- markX = 0;
85
89
  markCommands = [];
86
90
  constructor(ast, host) {
87
91
  super();
@@ -90,10 +94,10 @@ class ArpeggioTemporal extends TemporalNodeBase {
90
94
  const aboveCount = host.ast.contents.length;
91
95
  const bottomToTop = [
92
96
  ...host.members.slice(aboveCount).reverse(),
93
- host.members[0],
94
- ...host.members.slice(1, aboveCount),
97
+ ...host.members.slice(0, aboveCount),
95
98
  ];
96
- this.playbackMembers = ast.direction === "down" ? bottomToTop.reverse() : bottomToTop;
99
+ const members = ast.direction === "down" ? bottomToTop.reverse() : bottomToTop;
100
+ this.slots = new Map(members.map((member, index) => [member, index]));
97
101
  this.T.copyFrom(host.T);
98
102
  this.mergeKey = host.mergeKey;
99
103
  this.initLayoutBox();
@@ -115,24 +119,23 @@ class ArpeggioTemporal extends TemporalNodeBase {
115
119
  this.host.prepareHorizontal?.(line);
116
120
  }
117
121
  prepareLayout(context) {
118
- transferFunctionAddons(this, this.host);
119
- prepareLayoutHost(this.host, context);
120
- this.springConfig = { ...this.host.springConfig, ...this.springConfig };
122
+ const { host } = this;
123
+ transferFunctionAddons(this, host);
124
+ prepareLayoutHost(host, context);
125
+ this.springConfig = { ...host.springConfig };
121
126
  const em = this.ast.size;
122
- const gap = em * 0.16;
123
- const contentBounds = getLayoutBounds(this.host, true);
124
- const bounds = getLayoutBounds(this.host);
125
- const { commands, bounds: markBounds } = prepareArpeggioShape(contentBounds.h, em, this.ast.direction);
127
+ const contentBounds = getLayoutBounds(host, true);
128
+ const bounds = getLayoutBounds(host);
129
+ const { commands, width } = prepareArpeggioShape(contentBounds.h, em, this.ast.direction);
126
130
  this.markCommands = commands;
127
- this.markX = -markBounds.x;
128
- this.hostX = markBounds.w + gap;
129
- this.box.w = this.hostX + this.host.box.w;
131
+ this.hostX = width + em * 0.16 + foldAnchor(host) - host.box.anchor;
132
+ this.box.w = this.hostX + host.box.w;
130
133
  this.box.h = bounds.h;
131
- this.box.anchor = this.hostX + this.host.box.anchor;
132
- this.box.visualAxis = this.host.box.visualAxis - bounds.y;
133
- for (const name in this.host.ports) {
134
- const port = this.host.ports[name];
135
- this.ports[name] = { x: this.hostX + port.x, y: port.y - bounds.y };
134
+ this.box.anchor = this.hostX + host.box.anchor;
135
+ this.box.visualAxis = host.box.visualAxis - bounds.y;
136
+ for (const name in host.ports) {
137
+ const port = host.ports[name];
138
+ this.ports[name] = { ...port, x: this.hostX + port.x, y: port.y - bounds.y };
136
139
  }
137
140
  }
138
141
  onPlaced() {
@@ -147,47 +150,39 @@ class ArpeggioTemporal extends TemporalNodeBase {
147
150
  emitPlayback(emitter) {
148
151
  emitter.play(this.host);
149
152
  const total = emitter.end.clone().sub(emitter.start);
153
+ if (total.compare(0) <= 0)
154
+ return;
150
155
  const step = new Fraction(1, 8);
151
156
  // 短音段限制总延迟,让最后一个成员仍保留至少一半的宿主窗口。
152
- const maxStep = total.clone().div(2).div(this.playbackMembers.length - 1);
157
+ const maxStep = total.clone().div(2).div(this.slots.size - 1);
153
158
  if (step.compare(maxStep) > 0)
154
159
  step.copyFrom(maxStep);
155
- // 槽位取自成员顺序;无声成员也占槽,不能按实际发声音符重新编号。
156
- const slots = new Map(this.playbackMembers.map((member, index) => [member, index]));
160
+ const { slots } = this;
157
161
  const owner = this;
158
162
  emitter.defer(context => {
159
163
  let rootOrigin;
160
- // 反向锁定最近一次宿主访问的来源身份,避免修改前一遍反复留下的区间。
164
+ // 宿主刚在上面发布,其全部子区间连续排在 spans 末尾;命中后处理完同组即停。
161
165
  for (let index = context.spans.length - 1; index >= 0; index--) {
162
166
  const note = context.spans[index];
163
- if (!note.origins.some(origin => origin.node === owner))
164
- continue;
165
- rootOrigin = note.origins[0];
166
- break;
167
- }
168
- if (!rootOrigin)
169
- return;
170
- // 先限制到同一次顶层访问,再由成员来源筛出本宿主的全部有声、无声子区间。
171
- for (const note of context.spans) {
172
- if (note.origins[0] !== rootOrigin)
173
- continue;
174
- const member = note.origins.find(origin => slots.has(origin.node))?.node;
167
+ const origin = note.origins.find(item => item.node === owner);
168
+ if (!origin || (rootOrigin && origin !== rootOrigin))
169
+ break;
170
+ rootOrigin = origin;
171
+ const member = note.origins.find(item => slots.has(item.node))?.node;
175
172
  const slot = member ? slots.get(member) : undefined;
176
173
  if (!slot)
177
174
  continue;
178
175
  // 起止一起缩放再平移,保留宿主终点及成员内部节奏,避免只钳制尾部造成负时值。
179
- const delay = step.clone().mul(slot);
180
- const scale = total.clone().sub(delay).div(total);
181
- note.start.sub(emitter.start).mul(scale).add(emitter.start).add(delay);
182
- note.end.sub(emitter.start).mul(scale).add(emitter.start).add(delay);
176
+ const offset = step.clone().mul(slot).add(emitter.start);
177
+ const scale = emitter.end.clone().sub(offset).div(total);
178
+ note.start.sub(emitter.start).mul(scale).add(offset);
179
+ note.end.sub(emitter.start).mul(scale).add(offset);
183
180
  }
184
181
  });
185
182
  }
186
183
  paint(painter) {
187
- const originX = this.box.x;
188
- const originY = getLayoutBounds(this.host, true).y;
189
184
  painter.drawPath(this.markCommands, { fill: "#000" }, {
190
- x: originX + this.markX, y: originY, scaleX: 1, scaleY: 1,
185
+ x: this.box.x, y: getLayoutBounds(this.host, true).y, scaleX: 1, scaleY: 1,
191
186
  });
192
187
  this.host.paint(painter);
193
188
  for (const decoration of this.host.decorations)
@@ -1,7 +1,6 @@
1
- import type { Rect } from "../../layout/types.js";
2
1
  import type { PathCommand } from "../../render/types.js";
3
- /** 按 MuseScore 的原始重叠比例铺满指定高度,并一次性返回最终边界 */
2
+ /** 按 MuseScore 的原始重叠比例铺满指定高度,直接生成左上角对齐原点的局部路径 */
4
3
  export declare function prepareArpeggioShape(height: number, em: number, direction: "" | "up" | "down"): {
5
4
  commands: PathCommand[];
6
- bounds: Rect;
5
+ width: number;
7
6
  };
@@ -1,9 +1,9 @@
1
- import { pathBounds } from "../../layout/path.js";
2
1
  const TILE_WIDTH = 54.2813;
3
2
  const TILE_HEIGHT = 87.37505;
4
3
  const TILE_STEP = 72.8125;
4
+ const ARROW_WIDTH = 113.5313;
5
5
  const ARROW_HEIGHT = 93.6719;
6
- const ARROW_X = -27.4688;
6
+ const ARROW_OFFSET_X = 27.4688;
7
7
  const ARROW_Y = -7.9375;
8
8
  const ARROW_BOTTOM = ARROW_Y + ARROW_HEIGHT;
9
9
  /** MuseScore 4.7.4 导出的琶音波形块,已旋转到自上而下的局部坐标 */
@@ -60,69 +60,39 @@ const DOWN_ARROW = [
60
60
  { op: "C", cx1: 58.9063, cy1: 93.6719, cx2: 61.8907, cy2: 92.3438, x: 63.2188, y: 90.3594 },
61
61
  { op: "L", x: 111.8751, y: 21.5156 },
62
62
  ];
63
- function appendShape(output, shape, offsetX, offsetY, scale, reverse) {
64
- const x = (value) => offsetX + (reverse ? TILE_WIDTH - value : value) * scale;
65
- const y = (value) => offsetY + (reverse ? TILE_HEIGHT - value : value) * scale;
63
+ function appendShape(output, shape, x, y) {
66
64
  for (const command of shape) {
67
- if (command.op === "Z")
68
- output.push(command);
69
- else if (command.op === "C")
65
+ if (command.op === "C")
70
66
  output.push({
71
67
  op: "C",
72
68
  cx1: x(command.cx1), cy1: y(command.cy1),
73
69
  cx2: x(command.cx2), cy2: y(command.cy2),
74
70
  x: x(command.x), y: y(command.y),
75
71
  });
76
- else if (command.op === "Q")
77
- output.push({
78
- op: "Q",
79
- cx: x(command.cx), cy: y(command.cy),
80
- x: x(command.x), y: y(command.y),
81
- });
82
72
  else
83
73
  output.push({ op: command.op, x: x(command.x), y: y(command.y) });
84
74
  }
85
75
  }
86
- /** 按 MuseScore 的原始重叠比例铺满指定高度,并一次性返回最终边界 */
76
+ /** 按 MuseScore 的原始重叠比例铺满指定高度,直接生成左上角对齐原点的局部路径 */
87
77
  export function prepareArpeggioShape(height, em, direction) {
88
- const arrow = direction === "up" || direction === "down";
89
- const segments = Math.max(1, Math.round(height / (em * 0.3)));
90
- const regularCount = arrow ? Math.max(0, segments - 1) : segments;
91
- const naturalHeight = !arrow
92
- ? TILE_HEIGHT + TILE_STEP * (regularCount - 1)
93
- : ARROW_BOTTOM + TILE_STEP * regularCount;
78
+ const arrow = direction !== "";
79
+ const count = Math.max(1, Math.round(height / (em * 0.3)) - (arrow ? 1 : 0));
80
+ const naturalWidth = arrow ? ARROW_WIDTH : TILE_WIDTH;
81
+ const naturalHeight = arrow
82
+ ? ARROW_BOTTOM + TILE_STEP * count
83
+ : TILE_HEIGHT + TILE_STEP * (count - 1);
94
84
  const scale = height / naturalHeight;
85
+ const reverse = direction !== "down";
86
+ const place = (span, offset) => reverse
87
+ ? (value) => (span - offset - value) * scale
88
+ : (value) => (offset + value) * scale;
95
89
  const commands = [];
96
- for (let index = 0; index < regularCount; index++) {
97
- appendShape(commands, TILE, 0, index * TILE_STEP * scale, scale, direction === "");
90
+ const tileX = place(naturalWidth, arrow ? ARROW_OFFSET_X : 0);
91
+ for (let index = 0; index < count; index++) {
92
+ appendShape(commands, TILE, tileX, place(naturalHeight, index * TILE_STEP));
98
93
  }
99
94
  if (arrow) {
100
- appendShape(commands, DOWN_ARROW, ARROW_X * scale, (regularCount * TILE_STEP + ARROW_Y) * scale, scale, false);
101
- }
102
- const bounds = pathBounds(commands);
103
- if (direction !== "up")
104
- return { commands, bounds };
105
- const right = bounds.x + bounds.w;
106
- const mirrorX = (value) => right - (value - bounds.x);
107
- // 命令尚未发布,直接镜像,避免再分配整条路径及每个命令对象
108
- for (const command of commands) {
109
- if (command.op === "Z")
110
- continue;
111
- if (command.op === "C") {
112
- command.cx1 = mirrorX(command.cx1);
113
- command.cy1 = height - command.cy1;
114
- command.cx2 = mirrorX(command.cx2);
115
- command.cy2 = height - command.cy2;
116
- }
117
- else if (command.op === "Q") {
118
- command.cx = mirrorX(command.cx);
119
- command.cy = height - command.cy;
120
- }
121
- command.x = mirrorX(command.x);
122
- command.y = height - command.y;
95
+ appendShape(commands, DOWN_ARROW, place(naturalWidth, 0), place(naturalHeight, count * TILE_STEP + ARROW_Y));
123
96
  }
124
- return {
125
- commands,
126
- bounds: { x: bounds.x, y: height - bounds.y - bounds.h, w: bounds.w, h: bounds.h },
127
- };
97
+ return { commands, width: naturalWidth * scale };
128
98
  }
@@ -1,5 +1,13 @@
1
1
  import { ASTFunctionClass } from "../ASTtypes.js";
2
+ import type { LoweringContext } from "../../lowering/loweringContext.js";
2
3
  import type { PlaybackCursor } from "../../playback/types.js";
4
+ /**
5
+ * 按需注册行首小节号:统计从第二行起每条谱面行首小节的序号,并生成行首小节号附件
6
+ *
7
+ * 小节边界仅在正时值事件之后遇到 ANCHOR_KEY 时推进(兼容 @rest 补入的虚拟边界,且曲首 |: 不多计);
8
+ * 若上一行末尾未写小节线,当前小节跨行延续,新行行首因 measureRecorded 为 true 不重复标号。
9
+ */
10
+ export declare function registerBarNumbering(ctx: LoweringContext, pattern: string, font: string): void;
3
11
  export declare const BarNode: ASTFunctionClass;
4
12
  /**
5
13
  * 某一列此刻处在所属反复段的第几遍
@@ -1,5 +1,45 @@
1
1
  import { ASTFunctionNode } from "../ASTtypes.js";
2
+ import { Fraction } from "../../fraction.js";
2
3
  import { ANCHOR_KEY, isVisualTemporalNode, TemporalNodeBase } from "../temporal.js";
4
+ /**
5
+ * 按需注册行首小节号:统计从第二行起每条谱面行首小节的序号,并生成行首小节号附件
6
+ *
7
+ * 小节边界仅在正时值事件之后遇到 ANCHOR_KEY 时推进(兼容 @rest 补入的虚拟边界,且曲首 |: 不多计);
8
+ * 若上一行末尾未写小节线,当前小节跨行延续,新行行首因 measureRecorded 为 true 不重复标号。
9
+ */
10
+ export function registerBarNumbering(ctx, pattern, font) {
11
+ ctx.deferFinalize(result => {
12
+ const items = [];
13
+ const measureStart = new Fraction();
14
+ let measureNumber = 1;
15
+ let measureRecorded = false;
16
+ let lastLine = -1;
17
+ for (const column of result.columns) {
18
+ const time = column[0].t;
19
+ if (column.some(node => node.mergeKey === ANCHOR_KEY)) {
20
+ // 只有当前小节已走过正时值才闭合进入下一小节
21
+ if (time.compare(measureStart) > 0) {
22
+ measureNumber++;
23
+ measureStart.copyFrom(time);
24
+ measureRecorded = false;
25
+ }
26
+ }
27
+ else if (column.some(node => !node.T.isZero() && isVisualTemporalNode(node))) {
28
+ const line = column[0].layoutLine;
29
+ if (line !== lastLine) {
30
+ // 首行(lastLine < 0)与单行谱不标号;跨行未闭合的小节不重复标号
31
+ if (lastLine >= 0 && !measureRecorded)
32
+ items.push({ line, number: measureNumber });
33
+ lastLine = line;
34
+ }
35
+ measureRecorded = true;
36
+ }
37
+ }
38
+ if (items.length > 0) {
39
+ result.attachments.push(new BarNumberAttachment(items, pattern, font));
40
+ }
41
+ });
42
+ }
3
43
  class BarFunction extends ASTFunctionNode {
4
44
  static def = {
5
45
  name: ["bar", "|"],
@@ -79,6 +119,68 @@ class BarFunction extends ASTFunctionNode {
79
119
  return `@bar(${this.type}, ${this.size}px)`;
80
120
  }
81
121
  }
122
+ /**
123
+ * 每行行首的小节号附件
124
+ *
125
+ * 多声部行首锚定在连谱号细竖线正上方居中(通过首声部名称宿主发布的 bar.number 端口);
126
+ * 单声部行首无连谱号时统一右对齐到版心左边界 originX 外侧,保证各行纵向对齐且不压住行首升降号或记号。
127
+ */
128
+ class BarNumberAttachment {
129
+ items;
130
+ pattern;
131
+ font;
132
+ layer = "foreground";
133
+ sourceSpan = undefined;
134
+ constructor(items, pattern, font) {
135
+ this.items = items;
136
+ this.pattern = pattern;
137
+ this.font = font;
138
+ }
139
+ createGeometry(context) {
140
+ const draws = this.items.map(({ line, number }) => {
141
+ const firstCol = context.lines[line].columns[0];
142
+ // 选取行首列中视觉轴最靠上的宿主,确保小节号只挂在最顶层声部上方
143
+ const topHost = firstCol.reduce((top, host) => context.getVisualAxis(line, host.track) < context.getVisualAxis(line, top.track) ? host : top);
144
+ const fontSize = topHost.ast.size;
145
+ const port = topHost.ports["bar.number"];
146
+ const style = {
147
+ fontSize: fontSize * 0.5,
148
+ fontFamily: this.font,
149
+ textAlign: port ? "center" : "right",
150
+ fill: "#000",
151
+ };
152
+ // 仅将模板中的首个 1 替换为当前小节序号
153
+ const text = this.pattern.replace("1", String(number));
154
+ const metrics = context.textMeasurer.measureText(text, style);
155
+ const x = port ? topHost.box.x + port.x : context.originX - fontSize * 0.15;
156
+ const topY = port
157
+ ? topHost.box.y + port.y
158
+ : Math.min(topHost.box.y, context.getVisualAxis(line, topHost.track) - fontSize * 0.5);
159
+ const baselineY = topY - fontSize * 0.04;
160
+ return {
161
+ text,
162
+ x,
163
+ y: baselineY,
164
+ style,
165
+ region: {
166
+ x: port ? x - metrics.w / 2 : x - metrics.w,
167
+ y: baselineY - metrics.baseline,
168
+ w: metrics.w,
169
+ h: metrics.baseline,
170
+ line,
171
+ track: topHost.track,
172
+ },
173
+ };
174
+ });
175
+ return {
176
+ regions: draws.map(draw => draw.region),
177
+ paint(painter) {
178
+ for (const draw of draws)
179
+ painter.drawText(draw.text, draw.x, draw.y, draw.style);
180
+ },
181
+ };
182
+ }
183
+ }
82
184
  export const BarNode = BarFunction;
83
185
  class BarTemporalNode extends TemporalNodeBase {
84
186
  lines = [];
@@ -88,19 +190,12 @@ class BarTemporalNode extends TemporalNodeBase {
88
190
  this.ast = ast;
89
191
  this.mergeKey = ANCHOR_KEY;
90
192
  this.initLayoutBox();
91
- // 由于有最小时长,因此设置alpha依然有效
92
- this.springConfig = {
93
- alpha_L: 128,
94
- alpha_R: 128,
95
- };
96
193
  }
97
194
  /**
98
195
  * 给 bar 两侧的相邻元素增加 mu,避免被压缩到一起
99
196
  */
100
197
  prepareHorizontal(line) {
101
- let host = this;
102
- while (host.foldedInto)
103
- host = host.foldedInto;
198
+ const host = this.rootHost;
104
199
  if (!isVisualTemporalNode(host))
105
200
  return;
106
201
  const run = line.trackRuns.get(host.track);
@@ -120,6 +215,8 @@ class BarTemporalNode extends TemporalNodeBase {
120
215
  }
121
216
  }
122
217
  prepareLayout() {
218
+ // 由于有最小时长,因此设置alpha依然有效
219
+ this.springConfig = { alpha_L: 128, alpha_R: 128 };
123
220
  const { type, size } = this.ast;
124
221
  this.lines.length = 0;
125
222
  const repeatDots = [];
@@ -1,4 +1,3 @@
1
1
  import type { LoweringResult } from "../../lowering/types.js";
2
- import type { LayoutAttachment } from "../../layout/types.js";
3
2
  /** beam 域在 lowering 固化后统一生成作用域连接和相邻自动连接 */
4
- export declare function createAutomaticBeamAttachments(result: LoweringResult): LayoutAttachment[];
3
+ export declare function createAutomaticBeamAttachments(result: LoweringResult): void;