jpfun 0.4.1 → 0.6.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 (89) hide show
  1. package/README.md +53 -113
  2. package/dist/converter/midi/convert.js +14 -7
  3. package/dist/converter/musicxml/features.d.ts +57 -16
  4. package/dist/converter/musicxml/features.js +204 -42
  5. package/dist/converter/musicxml/index.d.ts +7 -2
  6. package/dist/converter/musicxml/index.js +256 -268
  7. package/dist/converter/musicxml/model.d.ts +36 -7
  8. package/dist/converter/musicxml/resolve.d.ts +10 -0
  9. package/dist/converter/musicxml/resolve.js +152 -0
  10. package/dist/core.d.ts +1 -0
  11. package/dist/core.js +1 -0
  12. package/dist/fraction.js +7 -1
  13. package/dist/functions/ASTtypes.d.ts +6 -3
  14. package/dist/functions/ASTtypes.js +30 -23
  15. package/dist/functions/adjust/index.js +10 -50
  16. package/dist/functions/arpeggio/index.js +40 -25
  17. package/dist/functions/beam/auto.js +2 -11
  18. package/dist/functions/box/index.js +132 -82
  19. package/dist/functions/dash/index.js +20 -11
  20. package/dist/functions/default.js +2 -1
  21. package/dist/functions/div/index.d.ts +1 -1
  22. package/dist/functions/div/index.js +9 -10
  23. package/dist/functions/dot/index.js +4 -5
  24. package/dist/functions/dyn/index.js +28 -32
  25. package/dist/functions/grace/index.d.ts +23 -8
  26. package/dist/functions/grace/index.js +138 -119
  27. package/dist/functions/head/index.js +11 -4
  28. package/dist/functions/key/index.js +1 -1
  29. package/dist/functions/meter/index.js +40 -7
  30. package/dist/functions/meter/shape.d.ts +3 -0
  31. package/dist/functions/meter/shape.js +43 -0
  32. package/dist/functions/note/index.d.ts +6 -1
  33. package/dist/functions/note/index.js +60 -57
  34. package/dist/functions/note/source-transform.d.ts +21 -0
  35. package/dist/functions/note/source-transform.js +164 -0
  36. package/dist/functions/page/index.d.ts +5 -0
  37. package/dist/functions/page/index.js +8 -5
  38. package/dist/functions/rest/index.d.ts +2 -0
  39. package/dist/functions/rest/index.js +233 -0
  40. package/dist/functions/set/index.js +5 -1
  41. package/dist/functions/symbol/index.d.ts +2 -0
  42. package/dist/functions/symbol/index.js +1 -1
  43. package/dist/functions/symbol/ornament.d.ts +3 -3
  44. package/dist/functions/symbol/ornament.js +21 -35
  45. package/dist/functions/symbol/symbols/accent.js +5 -5
  46. package/dist/functions/symbol/symbols/fermata.js +5 -4
  47. package/dist/functions/temporal.d.ts +21 -7
  48. package/dist/functions/temporal.js +14 -4
  49. package/dist/functions/tie/index.js +36 -42
  50. package/dist/functions/tuplet/index.js +20 -24
  51. package/dist/functions/up/index.js +54 -34
  52. package/dist/functions/voice/connections.d.ts +19 -0
  53. package/dist/functions/voice/connections.js +88 -0
  54. package/dist/functions/voice/index.js +248 -102
  55. package/dist/functions/volta/index.js +7 -12
  56. package/dist/jpfun.core.min.js +61 -41
  57. package/dist/jpfun.from-midi.min.js +5 -5
  58. package/dist/jpfun.from-musicxml.min.js +8 -8
  59. package/dist/jpfun.min.js +70 -50
  60. package/dist/layout/engine.d.ts +13 -1
  61. package/dist/layout/engine.js +458 -129
  62. package/dist/layout/model.js +22 -66
  63. package/dist/layout/page.js +3 -1
  64. package/dist/layout/range.d.ts +26 -0
  65. package/dist/layout/range.js +75 -0
  66. package/dist/layout/types.d.ts +63 -49
  67. package/dist/lowering/loweringContext.d.ts +1 -1
  68. package/dist/lowering/loweringContext.js +34 -4
  69. package/dist/lowering/types.d.ts +5 -0
  70. package/dist/parser/parse-utils/length-utils.js +2 -0
  71. package/dist/parser/parse-utils/note-utils.d.ts +2 -0
  72. package/dist/parser/parse-utils/note-utils.js +6 -2
  73. package/dist/parser/parserContext.js +5 -9
  74. package/dist/parser/preprocess.js +4 -6
  75. package/dist/playback/compile.d.ts +2 -9
  76. package/dist/playback/compile.js +297 -186
  77. package/dist/playback/event.d.ts +8 -16
  78. package/dist/playback/event.js +13 -3
  79. package/dist/playback/types.d.ts +76 -18
  80. package/dist/playback/types.js +1 -0
  81. package/dist/render/index.d.ts +1 -1
  82. package/dist/render/index.js +1 -1
  83. package/dist/render/paint.js +1 -1
  84. package/dist/render/recording.d.ts +1 -0
  85. package/dist/render/recording.js +21 -0
  86. package/dist/render/translate.d.ts +14 -0
  87. package/dist/render/translate.js +48 -0
  88. package/grammar.md +200 -250
  89. package/package.json +4 -4
@@ -1,6 +1,8 @@
1
- import type { LayoutBox, LayoutPrepareContext } from "../../layout/types.js";
1
+ import type { HorizontalLineView, LayoutBox, LayoutPrepareContext } from "../../layout/types.js";
2
2
  import type { LoweringContext } from "../../lowering/loweringContext.js";
3
- import type { Track } from "../../lowering/track.js";
3
+ import type { LoweringAttachment } from "../../lowering/types.js";
4
+ import { Fraction } from "../../fraction.js";
5
+ import { Track } from "../../lowering/track.js";
4
6
  import { TemporalNodeBase, type TimeState, type VisualTemporalNode } from "../temporal.js";
5
7
  import { GrammarNode, GrammarSugarNode } from "../../parser/grammarType.js";
6
8
  import { ParserContext } from "../../parser/parserContext.js";
@@ -31,7 +33,7 @@ declare class GraceFunction extends ASTFunctionNode {
31
33
  constructor(span: SourceSpan, args: FunctionArgs, ctx: ParserContext, parent?: ASTNodeBase | null, scale?: number);
32
34
  setGrace(node: ASTNodeBase, factor?: number): void;
33
35
  private adopt;
34
- /** 倚音与宿主各自收敛成可见 Temporal,然后整体折叠进一个复合节点 */
36
+ /** 宿主收敛成一个可见 Temporal,倚音展开成局部时间列,然后整体折叠进一个复合节点 */
35
37
  loweringEnter(ctx: LoweringContext, track: Track): GraceTemporal[];
36
38
  toString(source: string): string;
37
39
  }
@@ -41,29 +43,42 @@ export declare class GraceTemporal extends TemporalNodeBase {
41
43
  ast: GraceFunction;
42
44
  box: LayoutBox;
43
45
  readonly host: VisualTemporalNode;
46
+ /** 可见倚音按局部时间成列,同列成员分属不同声部 */
47
+ readonly graceColumns: readonly (readonly VisualTemporalNode[])[];
48
+ /** graceColumns 按列展开 */
44
49
  readonly graces: readonly VisualTemporalNode[];
45
- /** 承担节奏的倚音成员,按零时长标记切段;同段视觉上首尾相接,无条件连成一束减时线 */
50
+ /** 承担节奏的倚音成员,按声部与零时长标记切段;同段视觉上首尾相接,无条件连成一束减时线 */
46
51
  readonly graceRuns: readonly (readonly VisualTemporalNode[])[];
47
52
  readonly side: GraceSide;
53
+ /** 倚音子域的根轨:由宿主轨分出,不挂进它的分组,子域之外不可见 */
54
+ private readonly root;
55
+ /** 倚音内容里声明的附件,随子域排版 */
56
+ private readonly attachments;
57
+ /** 倚音内容的全部事件,含不可见的状态事件,按局部时间列展开 */
58
+ private readonly events;
59
+ /** 与 events 一一对应的局部起点,已随时值减半 */
60
+ private readonly starts;
48
61
  /** 成员相对本盒左上角的局部偏移,onPlaced 时同步为绝对坐标 */
49
62
  private hostOffset;
50
63
  private graceOffsets;
51
64
  /** 倚音线起点,同样是局部偏移 */
52
65
  private hookOrigin;
53
- constructor(ast: GraceFunction, host: VisualTemporalNode, graces: readonly VisualTemporalNode[], side: GraceSide);
66
+ constructor(ast: GraceFunction, host: VisualTemporalNode, columns: readonly (readonly TemporalNodeBase[])[], root: Track, attachments: readonly LoweringAttachment[], side: GraceSide);
54
67
  /** 成员共享全局时间状态,按发声顺序固化 */
55
- onTimeState(state: TimeState): void;
68
+ onTimeState(state: TimeState): void | Fraction;
69
+ prepareHorizontal(line: HorizontalLineView): void;
56
70
  /**
57
71
  * 播放时倚音从宿主借走的时值(前倚音从开头、后倚音从末尾)
58
72
  *
59
- * 比例 = 倚音字面总时值 / 四分音符,而四分音符 T=1,所以直接累加成员的 T。
73
+ * 比例 = 倚音块字面时长 / 四分音符,而四分音符 T=1,所以直接取各声部最晚的局部终点。
60
74
  * 基准取宿主经过 div 与 dot 后的实际时值;延时线是独立事件、tie 不改 T,天然不计入。
75
+ * 局部时间按同一比例映射进借走的时值,各声部因此同时起奏。
61
76
  */
62
77
  emitPlayback(emitter: PlaybackEmitter): void;
63
78
  /**
64
79
  * 宿主留在轨道基线上,倚音整体抬到它的左上或右上角
65
80
  *
66
- * 倚音成员之间自己紧排,不参与横向弹簧;整块的宽度计入复合盒,
81
+ * 倚音内容作为子域先排成一个刚性块,块内的附件同时就位;含约束留白的宽度计入复合盒,
67
82
  * 因此左右邻居会被固有宽度推开,不会在空间紧张时被压穿。
68
83
  */
69
84
  prepareLayout(context: LayoutPrepareContext): void;
@@ -1,6 +1,7 @@
1
1
  import { ErrorDiagnostic } from "../../diagnostic.js";
2
- import { prepareLayoutHost } from "../../layout/engine.js";
2
+ import { getLayoutBounds, layoutLocalSequence } from "../../layout/engine.js";
3
3
  import { Fraction } from "../../fraction.js";
4
+ import { Track } from "../../lowering/track.js";
4
5
  import { isVisualTemporalNode, TemporalNodeBase, } from "../temporal.js";
5
6
  import { ASTBraceNode, ASTFunctionNode, ASTLabelNode, ASTTextNode, } from "../ASTtypes.js";
6
7
  import { createBeamLayoutAttachment } from "../beam/layout.js";
@@ -8,10 +9,10 @@ import { DIV_ADDON_KEY } from "../div/index.js";
8
9
  /** 倚音相对宿主的字号比例 */
9
10
  const GRACE_SCALE = 0.7;
10
11
  /**
11
- * 肩线端口:要在这个盒子上方叠东西的人,应该从哪条线开始
12
+ * 肩线端口:倚音相对宿主的纵向定位基准
12
13
  *
13
- * 普通音符不发布它,读取方退化成盒顶即可(倚音本来就该避开上八度点);
14
- * 复合节点发布它,指向自己内部那个真正承载节奏的音符的顶边。
14
+ * 普通音符指向数字顶边,不含上八度点;复合节点转发内部宿主的肩线,
15
+ * 缺少端口的其他对象回退到盒顶
15
16
  */
16
17
  const SHOULDER_PORT = "shoulder";
17
18
  /**
@@ -41,7 +42,9 @@ class GraceFunction extends ASTFunctionNode {
41
42
  - \`{3 2}>1\`:一组前倚音,等价于上例
42
43
  - \`1>2>3\`:允许嵌套,\`1\` 修饰 \`2\`,\`2\` 修饰 \`3\`
43
44
 
44
- 倚音默认带一条减时线,按八分音符书写;再加 \`/\` 变为十六分音符`,
45
+ 倚音默认带一条减时线,按八分音符书写;再加 \`/\` 变为十六分音符
46
+
47
+ 倚音内容可以是换行以外的任意结构,例如 \`{1 & 3}>2\` 的两个声部同时奏出`,
45
48
  allowExtraArgs: false,
46
49
  args: [
47
50
  {
@@ -52,7 +55,7 @@ class GraceFunction extends ASTFunctionNode {
52
55
  },
53
56
  {
54
57
  name: "grace",
55
- description: "倚音内容,多个音符可用大括号组合",
58
+ description: "倚音内容,多个音符可用大括号组合,也可以含多个声部",
56
59
  type: "content",
57
60
  default: null,
58
61
  },
@@ -205,45 +208,45 @@ class GraceFunction extends ASTFunctionNode {
205
208
  this.sourceSpan.start = Math.min(this.sourceSpan.start, node.sourceSpan.start);
206
209
  this.sourceSpan.end = Math.max(this.sourceSpan.end, node.sourceSpan.end);
207
210
  }
208
- /** 倚音与宿主各自收敛成可见 Temporal,然后整体折叠进一个复合节点 */
211
+ /** 宿主收敛成一个可见 Temporal,倚音展开成局部时间列,然后整体折叠进一个复合节点 */
209
212
  loweringEnter(ctx, track) {
210
- if (!this.host || !this.grace) {
213
+ const host = this.host;
214
+ const grace = this.grace;
215
+ if (!host || !grace) {
211
216
  throw new ErrorDiagnostic("E_GRACE_MISSING_ARGS", "@grace 需要同时给出宿主和倚音", this.sourceSpan);
212
217
  }
213
- let graces = [];
214
- let host = null;
218
+ // 倚音内容是一个子域:在私有根轨上展开,根轨不挂进宿主轨的分组,其中的并行结构另开声部
219
+ const root = new Track(track);
220
+ let hostEvents = [];
221
+ let columns = [];
222
+ const attachments = [];
223
+ const lower = (content, lane) => ctx.trackedEvents(content, new Fraction(), lane);
215
224
  // 成员不是外层分组的成员,复合节点才是;否则 voice 的歌词会按下标错位
225
+ // 按发声顺序展开,事件序号随之有序
216
226
  ctx.isolateFromLoweringGroups(() => {
217
- const order = this.side === "pre"
218
- ? [this.grace, this.host]
219
- : [this.host, this.grace];
220
- for (const content of order) {
221
- const events = ctx.trackedEvents(content, new Fraction(), track).flat();
222
- const visible = events.filter(isVisualTemporalNode);
223
- // 并行分支的纵向关系由引擎解 Track 树,而折叠成员进不了引擎
224
- if (events.some(event => event.track !== track))
225
- throw new ErrorDiagnostic("E_GRACE_PARALLEL_CONTENT", "@grace 的内容不能包含多声部结构(& / @voices);要在倚音里叠音请用 ^", content.sourceSpan);
226
- if (content === this.host) {
227
- if (visible.length !== 1 || visible.length !== events.length) {
228
- throw new ErrorDiagnostic("E_GRACE_INVALID_HOST", "@grace 的宿主必须恰好产生一个可见 Temporal", content.sourceSpan);
229
- }
230
- host = visible[0];
231
- }
232
- else {
233
- if (visible.length === 0 || visible.length !== events.length) {
234
- throw new ErrorDiagnostic("E_GRACE_INVALID_CONTENT", "@grace 的倚音必须只产生可见 Temporal,且至少一个", content.sourceSpan);
235
- }
236
- graces = visible;
237
- }
238
- }
227
+ if (this.side === "post")
228
+ hostEvents = lower(host, track).flat();
229
+ ctx.beginLoweringGroup(this, { onAttachment(attachment) { attachments.push(attachment); } });
230
+ columns = lower(grace, root);
231
+ ctx.endLoweringGroup(this);
232
+ if (this.side === "pre")
233
+ hostEvents = lower(host, track).flat();
239
234
  });
240
- const composite = new GraceTemporal(this, host, graces, this.side);
235
+ const [hostNode] = hostEvents;
236
+ if (hostEvents.length !== 1 || !isVisualTemporalNode(hostNode)) {
237
+ throw new ErrorDiagnostic("E_GRACE_INVALID_HOST", "@grace 的宿主必须恰好产生一个可见 Temporal", host.sourceSpan);
238
+ }
239
+ const events = columns.flat();
240
+ if (!events.some(isVisualTemporalNode) || events.some(event => event.breakBefore > 0)) {
241
+ throw new ErrorDiagnostic("E_GRACE_INVALID_CONTENT", "@grace 的倚音至少产生一个可见对象,且不能换行", grace.sourceSpan);
242
+ }
243
+ const composite = new GraceTemporal(this, hostNode, columns, root, attachments, this.side);
241
244
  // 倚音同段无条件全部相连,只被零时长标记切断,所以 grace 一定认识 beam,允许耦合
242
245
  // 折叠成员不进全局时间列,autobeam 看不到它们,要手动调用且强制连接
243
246
  for (const run of composite.graceRuns) {
244
247
  if (run.length < 2)
245
248
  continue;
246
- ctx.addAttachment(createBeamLayoutAttachment([...run], false, this.grace.sourceSpan));
249
+ ctx.addAttachment(createBeamLayoutAttachment([...run], false, grace.sourceSpan));
247
250
  }
248
251
  return [composite];
249
252
  }
@@ -262,27 +265,39 @@ const HOOK_COMMANDS = [
262
265
  { op: "C", cx1: -0.01, cy1: 0.26, cx2: 0.14, cy2: 0.42, x: 0.29, y: 0.36 },
263
266
  ];
264
267
  const HOOK_WIDTH = 0.07;
265
- /** 倚音块底边到宿主顶边的视觉间隙;倚音线会探进这段空间指向宿主 */
268
+ /** 倚音块底边到宿主肩线的视觉间隙;倚音线会探进这段空间指向宿主 */
266
269
  const GRACE_RISE = 0.18;
267
- /** 倪音向宿主借时值的上限,防止宿主被偷光;只读 */
270
+ /** 倚音向宿主借时值的上限,防止宿主被偷光;只读 */
268
271
  const MAX_STEAL_RATIO = new Fraction(3, 4);
269
272
  /** 倚音复合体:宿主与倚音都折叠在这一个盒子里,对外只是一个可见事件 */
270
273
  export class GraceTemporal extends TemporalNodeBase {
271
274
  host;
275
+ /** 可见倚音按局部时间成列,同列成员分属不同声部 */
276
+ graceColumns;
277
+ /** graceColumns 按列展开 */
272
278
  graces;
273
- /** 承担节奏的倚音成员,按零时长标记切段;同段视觉上首尾相接,无条件连成一束减时线 */
279
+ /** 承担节奏的倚音成员,按声部与零时长标记切段;同段视觉上首尾相接,无条件连成一束减时线 */
274
280
  graceRuns;
275
281
  side;
282
+ /** 倚音子域的根轨:由宿主轨分出,不挂进它的分组,子域之外不可见 */
283
+ root;
284
+ /** 倚音内容里声明的附件,随子域排版 */
285
+ attachments;
286
+ /** 倚音内容的全部事件,含不可见的状态事件,按局部时间列展开 */
287
+ events;
288
+ /** 与 events 一一对应的局部起点,已随时值减半 */
289
+ starts;
276
290
  /** 成员相对本盒左上角的局部偏移,onPlaced 时同步为绝对坐标 */
277
291
  hostOffset = { x: 0, y: 0 };
278
292
  graceOffsets = [];
279
293
  /** 倚音线起点,同样是局部偏移 */
280
294
  hookOrigin = null;
281
- constructor(ast, host, graces, side) {
295
+ constructor(ast, host, columns, root, attachments, side) {
282
296
  super();
283
297
  this.ast = ast;
284
298
  this.host = host;
285
- this.graces = graces;
299
+ this.root = root;
300
+ this.attachments = attachments;
286
301
  this.side = side;
287
302
  this.T.copyFrom(host.T);
288
303
  this.mergeKey = host.mergeKey;
@@ -293,81 +308,101 @@ export class GraceTemporal extends TemporalNodeBase {
293
308
  host.addon = void 0;
294
309
  // 成员不进入全局 columns,对外由复合体代表:写在成员上的标签仍可做关系端点
295
310
  host.foldedInto = this;
296
- const runs = [];
297
- let run = [];
298
- for (const grace of graces) {
299
- grace.foldedInto = this;
300
- // 调号、速度这类零时长标记不承担节奏,还会在视觉上把倚音串切断
311
+ const graceColumns = [];
312
+ const events = [];
313
+ const starts = [];
314
+ for (const column of columns) {
315
+ // 倚音默认就是八分音符,局部时间整体减半;成员的 t 随后与宿主同步,所以局部起点另存
316
+ const start = column[0].t.clone().divPow2();
317
+ const visible = [];
318
+ for (const event of column) {
319
+ event.foldedInto = this;
320
+ events.push(event);
321
+ starts.push(start);
322
+ if (isVisualTemporalNode(event))
323
+ visible.push(event);
324
+ }
325
+ if (visible.length > 0)
326
+ graceColumns.push(visible);
327
+ }
328
+ this.graceColumns = graceColumns;
329
+ this.graces = graceColumns.flat();
330
+ this.events = events;
331
+ this.starts = starts;
332
+ const runs = new Map();
333
+ const graceRuns = [];
334
+ for (const grace of this.graces) {
335
+ // 调号、速度这类零时长标记不承担节奏,还会在视觉上把本声部的倚音串切断
301
336
  if (grace.T.isZero()) {
302
- if (run.length > 0)
303
- runs.push(run);
304
- run = [];
337
+ runs.delete(grace.track);
305
338
  continue;
306
339
  }
340
+ let run = runs.get(grace.track);
341
+ if (!run) {
342
+ runs.set(grace.track, run = []);
343
+ graceRuns.push(run);
344
+ }
307
345
  run.push(grace);
308
- // 倚音默认就是八分音符:补一条减时线,书面时值随之减半
346
+ // 补一条减时线,书面时值随之减半
309
347
  const addon = grace.addon = { ...grace.addon };
310
348
  addon[DIV_ADDON_KEY] = (Number(addon[DIV_ADDON_KEY]) || 0) + 1;
311
349
  grace.T.divPow2();
312
350
  }
313
- if (run.length > 0)
314
- runs.push(run);
315
- this.graceRuns = runs;
351
+ this.graceRuns = graceRuns;
316
352
  }
317
353
  /** 成员共享全局时间状态,按发声顺序固化 */
318
354
  onTimeState(state) {
319
355
  // 同步时间的修改。比如tuplet修改的是GraceTemporal.T,这里要把它传给成员
320
356
  this.host.T.copyFrom(this.T);
321
- const members = this.side === "pre"
322
- ? [...this.graces, this.host]
323
- : [this.host, ...this.graces];
324
- for (const member of members) {
357
+ let offset = undefined;
358
+ for (const member of this.side === "pre" ? [...this.events, this.host] : [this.host, ...this.events]) {
325
359
  member.t.copyFrom(this.t);
326
- member.track = this.track;
327
360
  member.layoutLine = this.layoutLine;
328
- member.onTimeState?.(state);
361
+ const result = member.onTimeState?.(state);
362
+ if (member === this.host) {
363
+ this.T.copyFrom(member.T);
364
+ offset = result;
365
+ }
329
366
  }
367
+ return offset;
368
+ }
369
+ prepareHorizontal(line) {
370
+ this.host.prepareHorizontal?.(line);
330
371
  }
331
372
  /**
332
373
  * 播放时倚音从宿主借走的时值(前倚音从开头、后倚音从末尾)
333
374
  *
334
- * 比例 = 倚音字面总时值 / 四分音符,而四分音符 T=1,所以直接累加成员的 T。
375
+ * 比例 = 倚音块字面时长 / 四分音符,而四分音符 T=1,所以直接取各声部最晚的局部终点。
335
376
  * 基准取宿主经过 div 与 dot 后的实际时值;延时线是独立事件、tie 不改 T,天然不计入。
377
+ * 局部时间按同一比例映射进借走的时值,各声部因此同时起奏。
336
378
  */
337
379
  emitPlayback(emitter) {
338
380
  const written = new Fraction();
339
- for (const grace of this.graces)
340
- written.add(grace.T);
381
+ const end = new Fraction();
382
+ this.events.forEach((event, index) => {
383
+ end.copyFrom(this.starts[index]).add(event.T);
384
+ if (end.compare(written) > 0)
385
+ written.copyFrom(end);
386
+ });
341
387
  const ratio = written.compare(MAX_STEAL_RATIO) > 0 ? MAX_STEAL_RATIO : written;
342
388
  const total = emitter.end.clone().sub(emitter.start);
343
389
  const steal = total.clone().mul(ratio);
344
390
  const hostDuration = total.sub(steal);
345
- const start = emitter.start.clone();
346
- const playGraces = (cursor) => {
347
- for (const grace of this.graces) {
348
- if (grace.T.isZero() || written.isZero()) {
349
- emitter.play(grace, cursor, new Fraction());
350
- continue;
351
- }
352
- const duration = steal.clone().mul(grace.T).div(written);
353
- emitter.play(grace, cursor, duration);
354
- cursor.add(duration);
355
- }
356
- };
357
- if (this.side === "pre") {
358
- const cursor = start.clone();
359
- playGraces(cursor);
360
- emitter.play(this.host, cursor, hostDuration);
361
- }
362
- else {
363
- emitter.play(this.host, start, hostDuration);
364
- playGraces(start.clone().add(hostDuration));
365
- }
391
+ // 全是零时长标记时块长为 0,成员都落在借时起点
392
+ const scale = written.isZero() ? new Fraction() : steal.clone().div(written);
393
+ const base = this.side === "pre" ? emitter.start.clone() : emitter.start.clone().add(hostDuration);
394
+ if (this.side === "post")
395
+ emitter.play(this.host, emitter.start.clone(), hostDuration);
396
+ this.events.forEach((event, index) => {
397
+ emitter.play(event, this.starts[index].clone().mul(scale).add(base), event.T.clone().mul(scale));
398
+ });
399
+ if (this.side === "pre")
400
+ emitter.play(this.host, base.add(steal), hostDuration);
366
401
  }
367
402
  /**
368
403
  * 宿主留在轨道基线上,倚音整体抬到它的左上或右上角
369
404
  *
370
- * 倚音成员之间自己紧排,不参与横向弹簧;整块的宽度计入复合盒,
405
+ * 倚音内容作为子域先排成一个刚性块,块内的附件同时就位;含约束留白的宽度计入复合盒,
371
406
  * 因此左右邻居会被固有宽度推开,不会在空间紧张时被压穿。
372
407
  */
373
408
  prepareLayout(context) {
@@ -377,64 +412,48 @@ export class GraceTemporal extends TemporalNodeBase {
377
412
  this.host.addon = this.addon;
378
413
  this.addon = void 0;
379
414
  }
380
- for (const grace of this.graces)
381
- prepareLayoutHost(grace, context);
382
- prepareLayoutHost(this.host, context);
383
- const em = this.ast.size;
384
- const graceEm = em * GRACE_SCALE;
385
- const memberGap = graceEm * 0.16;
415
+ const block = context.layoutSubdomain(this, this.graceColumns, this.root, this.attachments);
416
+ const hostWidth = layoutLocalSequence([this.host], context);
417
+ this.springConfig = { ...this.host.springConfig, ...this.springConfig };
418
+ const graceEm = this.ast.size * GRACE_SCALE;
386
419
  const sideGap = graceEm * 0.2;
387
420
  const rise = graceEm * GRACE_RISE;
388
- let graceWidth = 0;
389
- let graceAxis = 0;
390
- let graceHeight = 0;
391
- // 倚音之间按视觉轴对齐,否则带下八度点的成员会把数字顶得比旁边高
392
- for (const grace of this.graces)
393
- graceAxis = Math.max(graceAxis, grace.box.visualAxis);
394
- this.graceOffsets = this.graces.map(grace => {
395
- const x = graceWidth;
396
- graceWidth += grace.box.w + memberGap;
397
- const y = graceAxis - grace.box.visualAxis;
398
- graceHeight = Math.max(graceHeight, y + grace.box.h);
399
- return { x, y };
400
- });
401
- if (this.graces.length > 0)
402
- graceWidth -= memberGap;
403
421
  // 倚音贴着宿主的肩线而不是宿主盒顶:宿主本身也是复合体时,
404
422
  // 肩线由它转发上来,所以两侧倚音会落在同一高度而不是层层叠高
405
423
  const shoulder = this.host.ports[SHOULDER_PORT]?.y ?? 0;
406
- const graceTop = shoulder - rise - graceHeight;
407
- const lift = Math.max(0, -graceTop);
408
- const graceX = this.side === "pre" ? 0 : this.host.box.w + sideGap;
424
+ const graceTop = shoulder - rise - block.height;
425
+ const hostBounds = getLayoutBounds(this.host);
426
+ const lift = Math.max(0, -graceTop, -hostBounds.y);
427
+ const graceX = this.side === "pre" ? 0 : hostWidth + sideGap;
409
428
  this.hostOffset = {
410
- x: this.side === "pre" ? graceWidth + sideGap : 0,
429
+ x: this.host.box.x + (this.side === "pre" ? block.width + sideGap : 0),
411
430
  y: lift,
412
431
  };
413
- for (const offset of this.graceOffsets) {
414
- offset.x += graceX;
415
- offset.y += graceTop + lift;
416
- }
417
- this.box.w = this.host.box.w + sideGap + graceWidth;
418
- this.box.h = lift + this.host.box.h;
432
+ this.graceOffsets = block.positions.map(position => ({
433
+ x: graceX + position.x,
434
+ y: graceTop + lift + position.y,
435
+ }));
436
+ this.box.w = hostWidth + sideGap + block.width;
437
+ this.box.h = lift + hostBounds.y + hostBounds.h;
419
438
  this.box.anchor = this.hostOffset.x + this.host.box.anchor;
420
439
  this.box.visualAxis = lift + this.host.box.visualAxis;
421
440
  // 端口原样上提:减时线、歌词、连音线都接到宿主上,关系函数不需要认识倚音
422
441
  for (const name in this.host.ports) {
423
442
  const port = this.host.ports[name];
424
443
  this.ports[name] = {
444
+ ...port,
425
445
  x: this.hostOffset.x + port.x,
426
446
  y: this.hostOffset.y + port.y,
427
447
  };
428
448
  }
429
449
  // 宿主没有肩线时它自己的盒顶就是肩线;有则上面那轮转发已经带偏移抬好了
430
450
  this.ports[SHOULDER_PORT] ??= { x: this.box.anchor, y: this.hostOffset.y };
431
- // 钩形曲线挂在靠近宿主的那个倚音成员下方,零时长标记不承接它
432
- const near = this.side === "pre"
433
- ? this.graceRuns.at(-1)?.at(-1)
434
- : this.graceRuns[0]?.[0];
451
+ // 钩形曲线挂在靠近宿主的那个倚音成员下方,零时长标记不承接它;高度取成员的最低下沿,不含块内附件
452
+ const rhythmic = this.graces.filter(grace => !grace.T.isZero());
453
+ const near = this.side === "pre" ? rhythmic.at(-1) : rhythmic[0];
435
454
  this.hookOrigin = near ? {
436
455
  x: this.graceOffsets[this.graces.indexOf(near)].x + near.box.anchor,
437
- y: graceTop + lift + graceHeight,
456
+ y: Math.max(...this.graces.map((grace, index) => this.graceOffsets[index].y + grace.box.h)),
438
457
  } : null;
439
458
  }
440
459
  /** 引擎每次改变本节点坐标后,按准备阶段保存的局部偏移重算成员绝对坐标 */
@@ -452,7 +471,7 @@ export class GraceTemporal extends TemporalNodeBase {
452
471
  for (const member of [this.host, ...this.graces]) {
453
472
  member.paint(painter);
454
473
  for (const decoration of member.decorations)
455
- decoration.paint(painter);
474
+ decoration.paint?.(painter);
456
475
  }
457
476
  if (!this.hookOrigin)
458
477
  return;
@@ -82,6 +82,13 @@ class HeadBoundaryTemporal extends TemporalNodeBase {
82
82
  this.ast = ast;
83
83
  this.initLayoutBox();
84
84
  }
85
+ validateDuration() {
86
+ for (const node of this.members) {
87
+ if (!node.T.isZero()) {
88
+ throw new ErrorDiagnostic("E_HEAD_NONZERO_DURATION", "@head 的内容必须全部为零时长", node.ast.sourceSpan);
89
+ }
90
+ }
91
+ }
85
92
  prepareLayout() { this.box.w = this.box.h = this.box.anchor = this.box.visualAxis = 0; }
86
93
  prepareHorizontal(line) {
87
94
  // 只由第一个边界协调一次整块 Head,避免六个边界重复改弹簧
@@ -455,7 +462,7 @@ H.tempo: 94
455
462
  ];
456
463
  }
457
464
  loweringEnter(ctx) {
458
- // 收集真实全局事件:退出时校验时值,放置后按 AST 行归属做横向对齐
465
+ // 收集真实全局事件,供末边界校验时值和放置后的横向对齐
459
466
  const group = {
460
467
  members: [],
461
468
  onTemporal(node) { this.members.push(node); },
@@ -471,10 +478,10 @@ H.tempo: 94
471
478
  const [start, end] = ctx.getTemporalNodes(ast);
472
479
  return { ast, start, end };
473
480
  });
481
+ // right.end 是整个 Head 的末边界,按三槽的 sequence 排在所有内容之后
482
+ // 此时 rest 等延迟时值已固化,只检查当前 Head 的成员,无须再扫描全谱索引
483
+ head.slots[2].end.onTimeState = () => head.validateDuration();
474
484
  for (const node of members) {
475
- if (!node.T.isZero()) {
476
- throw new ErrorDiagnostic("E_HEAD_NONZERO_DURATION", "@head 的内容必须全部为零时长", node.ast.sourceSpan);
477
- }
478
485
  if (node.breakBefore > 0) {
479
486
  throw new ErrorDiagnostic("E_HEAD_INTERNAL_BREAK", "@head 的内部不能使用 @br;第一层内容已经表示视觉行", node.ast.sourceSpan);
480
487
  }
@@ -17,7 +17,7 @@ function normalizeTonality(tonality) {
17
17
  }
18
18
  catch { /* 落到下面归一化 */ }
19
19
  const parsed = parseNoteName(tonality.replace(/^[a-g]/, letter => letter.toUpperCase()));
20
- if (parsed instanceof Diagnostic)
20
+ if (parsed instanceof Diagnostic || parsed.next !== tonality.length)
21
21
  return null;
22
22
  const pitchClass = NoteNameMap[parsed.name];
23
23
  if (pitchClass === undefined)
@@ -1,6 +1,8 @@
1
1
  import { ErrorDiagnostic, WarningDiagnostic } from "../../diagnostic.js";
2
2
  import { Fraction } from "../../fraction.js";
3
3
  import { ANCHOR_KEY, TemporalNodeBase } from "../temporal.js";
4
+ import { paintSymbol } from "../symbol/shape.js";
5
+ import { sanbanBounds, sanbanShapes } from "./shape.js";
4
6
  import { ASTFunctionNode, } from "../ASTtypes.js";
5
7
  class MeterFunction extends ASTFunctionNode {
6
8
  static def = {
@@ -10,7 +12,8 @@ class MeterFunction extends ASTFunctionNode {
10
12
  ~~~jpfun
11
13
  @meter(6, 8)
12
14
  ~~~
13
- 设置 \`6/8\` 拍号:每小节含六个八分音符的时值`,
15
+ 设置 \`6/8\` 拍号:每小节含六个八分音符的时值。
16
+ 分子或分母为 0 时表示散板。`,
14
17
  allowExtraArgs: false,
15
18
  args: [
16
19
  {
@@ -43,9 +46,9 @@ class MeterFunction extends ASTFunctionNode {
43
46
  super(span, parent);
44
47
  let [num, den, size] = this.getArgValue(args, ctx);
45
48
  this.font = ctx.variables.numberfont;
46
- if (!Number.isSafeInteger(num) || !Number.isSafeInteger(num * 4) || num <= 0
47
- || !Number.isSafeInteger(den) || den <= 0) {
48
- const message = `@meter 的分子和分母必须是正整数`;
49
+ if (!Number.isSafeInteger(num) || !Number.isSafeInteger(num * 4) || num < 0
50
+ || !Number.isSafeInteger(den) || den < 0) {
51
+ const message = `@meter 的分子和分母必须是非负整数`;
49
52
  if (ctx.variables.strict)
50
53
  throw new ErrorDiagnostic("E_METER_INVALID", message, span);
51
54
  ctx.diagnostics.push(new WarningDiagnostic("W_METER_INVALID", `${message},已回落为 4/4`, span));
@@ -54,7 +57,8 @@ class MeterFunction extends ASTFunctionNode {
54
57
  this.numerator = num;
55
58
  this.denominator = den;
56
59
  // QN 以四分音符为 1;拍号不修改音符自身时值
57
- this.measureDuration = new Fraction(num * 4, den);
60
+ if (num !== 0 && den !== 0)
61
+ this.measureDuration = new Fraction(num * 4, den);
58
62
  this.size = ctx.length2px(size);
59
63
  this.strict = ctx.variables.strict;
60
64
  }
@@ -73,7 +77,7 @@ class MeterFunction extends ASTFunctionNode {
73
77
  // 诊断范围从上一边界之后延伸到当前关闭边界
74
78
  let sourceStart = 0;
75
79
  const closeMeasure = (end, sourceEnd) => {
76
- if (!active || end.equals(measureStart))
80
+ if (!active?.measureDuration || end.equals(measureStart))
77
81
  return;
78
82
  // Fraction 运算会原地修改接收者,不能直接对时间线字段做减法
79
83
  measureLength.copyFrom(end).sub(measureStart);
@@ -87,9 +91,22 @@ class MeterFunction extends ASTFunctionNode {
87
91
  }
88
92
  measureStart.copyFrom(end);
89
93
  };
94
+ const metersByHost = new Map();
95
+ for (const nodes of result.astToTemporal.values()) {
96
+ for (const node of nodes) {
97
+ if (!(node instanceof MeterTemporal))
98
+ continue;
99
+ let host = node;
100
+ while (host.foldedInto)
101
+ host = host.foldedInto;
102
+ if (!metersByHost.has(host))
103
+ metersByHost.set(host, node);
104
+ }
105
+ }
90
106
  for (const column of result.columns) {
91
107
  const time = column[0].t;
92
- const meter = column.find(node => node instanceof MeterTemporal);
108
+ const host = column.find(node => metersByHost.has(node));
109
+ const meter = host && metersByHost.get(host);
93
110
  if (meter) {
94
111
  closeMeasure(time, meter.ast.sourceSpan.start);
95
112
  active = meter.ast;
@@ -130,7 +147,12 @@ class MeterTemporal extends TemporalNodeBase {
130
147
  };
131
148
  this.initLayoutBox();
132
149
  }
150
+ onTimeState(state) {
151
+ state.meter = this.ast.measureDuration;
152
+ }
133
153
  emitPlayback(emitter) {
154
+ if (!this.ast.measureDuration)
155
+ return;
134
156
  emitter.emit({
135
157
  kind: "time-signature",
136
158
  at: emitter.start,
@@ -139,6 +161,13 @@ class MeterTemporal extends TemporalNodeBase {
139
161
  });
140
162
  }
141
163
  prepareLayout(context) {
164
+ if (!this.ast.measureDuration) {
165
+ this.box.w = this.ast.size * sanbanBounds.w / sanbanBounds.h;
166
+ this.box.h = this.ast.size;
167
+ this.box.anchor = this.box.w / 2;
168
+ this.box.visualAxis = this.box.h / 2;
169
+ return;
170
+ }
142
171
  const num = context.textMeasurer.measureText(String(this.ast.numerator), this.style);
143
172
  const den = context.textMeasurer.measureText(String(this.ast.denominator), this.style);
144
173
  const gap = this.ast.size * 0.08;
@@ -154,6 +183,10 @@ class MeterTemporal extends TemporalNodeBase {
154
183
  this.box.visualAxis = this.lineY + this.lineHeight / 2;
155
184
  }
156
185
  paint(painter) {
186
+ if (!this.ast.measureDuration) {
187
+ paintSymbol(painter, sanbanShapes, sanbanBounds, this.box, this.ast.size / sanbanBounds.h);
188
+ return;
189
+ }
157
190
  const center = this.box.x + this.box.anchor;
158
191
  painter.drawText(String(this.ast.numerator), center, this.box.y + this.numeratorBaseline, this.style);
159
192
  painter.drawRect(this.box.x, this.box.y + this.lineY, this.box.w, this.lineHeight, { fill: "#000" });
@@ -0,0 +1,3 @@
1
+ import { type SymbolShape } from "../symbol/shape.js";
2
+ export declare const sanbanShapes: readonly SymbolShape[];
3
+ export declare const sanbanBounds: import("../../core.js").Rect;